xref: /linux-6.15/include/linux/memstick.h (revision ed3174d9)
1 /*
2  *  Sony MemoryStick support
3  *
4  *  Copyright (C) 2007 Alex Dubov <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 
12 #ifndef _MEMSTICK_H
13 #define _MEMSTICK_H
14 
15 #include <linux/workqueue.h>
16 #include <linux/scatterlist.h>
17 #include <linux/device.h>
18 
19 /*** Hardware based structures ***/
20 
21 struct ms_status_register {
22 	unsigned char reserved;
23 	unsigned char interrupt;
24 #define MEMSTICK_INT_CMDNAK             0x0001
25 #define MEMSTICK_INT_BREQ               0x0020
26 #define MEMSTICK_INT_ERR                0x0040
27 #define MEMSTICK_INT_CED                0x0080
28 
29 	unsigned char status0;
30 #define MEMSTICK_STATUS0_WP             0x0001
31 #define MEMSTICK_STATUS0_SL             0x0002
32 #define MEMSTICK_STATUS0_BF             0x0010
33 #define MEMSTICK_STATUS0_BE             0x0020
34 #define MEMSTICK_STATUS0_FB0            0x0040
35 #define MEMSTICK_STATUS0_MB             0x0080
36 
37 	unsigned char status1;
38 #define MEMSTICK_STATUS1_UCFG           0x0001
39 #define MEMSTICK_STATUS1_FGER           0x0002
40 #define MEMSTICK_STATUS1_UCEX           0x0004
41 #define MEMSTICK_STATUS1_EXER           0x0008
42 #define MEMSTICK_STATUS1_UCDT           0x0010
43 #define MEMSTICK_STATUS1_DTER           0x0020
44 #define MEMSTICK_STATUS1_FBI            0x0040
45 #define MEMSTICK_STATUS1_MB             0x0080
46 } __attribute__((packed));
47 
48 struct ms_id_register {
49 	unsigned char type;
50 	unsigned char reserved;
51 	unsigned char category;
52 	unsigned char class;
53 } __attribute__((packed));
54 
55 struct ms_param_register {
56 	unsigned char system;
57 	unsigned char block_address_msb;
58 	unsigned short block_address;
59 	unsigned char cp;
60 #define MEMSTICK_CP_BLOCK               0x0000
61 #define MEMSTICK_CP_PAGE                0x0020
62 #define MEMSTICK_CP_EXTRA               0x0040
63 #define MEMSTICK_CP_OVERWRITE           0x0080
64 
65 	unsigned char page_address;
66 } __attribute__((packed));
67 
68 struct ms_extra_data_register {
69 	unsigned char  overwrite_flag;
70 #define MEMSTICK_OVERWRITE_UPDATA       0x0010
71 #define MEMSTICK_OVERWRITE_PAGE         0x0060
72 #define MEMSTICK_OVERWRITE_BLOCK        0x0080
73 
74 	unsigned char  management_flag;
75 #define MEMSTICK_MANAGEMENT_SYSTEM      0x0004
76 #define MEMSTICK_MANAGEMENT_TRANS_TABLE 0x0008
77 #define MEMSTICK_MANAGEMENT_COPY        0x0010
78 #define MEMSTICK_MANAGEMENT_ACCESS      0x0020
79 
80 	unsigned short logical_address;
81 } __attribute__((packed));
82 
83 struct ms_register {
84 	struct ms_status_register     status;
85 	struct ms_id_register         id;
86 	unsigned char                 reserved[8];
87 	struct ms_param_register      param;
88 	struct ms_extra_data_register extra_data;
89 } __attribute__((packed));
90 
91 struct mspro_param_register {
92 	unsigned char  system;
93 	unsigned short data_count;
94 	unsigned int   data_address;
95 	unsigned char  cmd_param;
96 } __attribute__((packed));
97 
98 struct mspro_register {
99 	struct ms_status_register    status;
100 	struct ms_id_register        id;
101 	unsigned char                reserved[8];
102 	struct mspro_param_register  param;
103 } __attribute__((packed));
104 
105 struct ms_register_addr {
106 	unsigned char r_offset;
107 	unsigned char r_length;
108 	unsigned char w_offset;
109 	unsigned char w_length;
110 } __attribute__((packed));
111 
112 enum {
113 	MS_TPC_READ_LONG_DATA   = 0x02,
114 	MS_TPC_READ_SHORT_DATA  = 0x03,
115 	MS_TPC_READ_REG         = 0x04,
116 	MS_TPC_READ_IO_DATA     = 0x05, /* unverified */
117 	MS_TPC_GET_INT          = 0x07,
118 	MS_TPC_SET_RW_REG_ADRS  = 0x08,
119 	MS_TPC_EX_SET_CMD       = 0x09,
120 	MS_TPC_WRITE_IO_DATA    = 0x0a, /* unverified */
121 	MS_TPC_WRITE_REG        = 0x0b,
122 	MS_TPC_WRITE_SHORT_DATA = 0x0c,
123 	MS_TPC_WRITE_LONG_DATA  = 0x0d,
124 	MS_TPC_SET_CMD          = 0x0e
125 };
126 
127 enum {
128 	MS_CMD_BLOCK_END     = 0x33,
129 	MS_CMD_RESET         = 0x3c,
130 	MS_CMD_BLOCK_WRITE   = 0x55,
131 	MS_CMD_SLEEP         = 0x5a,
132 	MS_CMD_BLOCK_ERASE   = 0x99,
133 	MS_CMD_BLOCK_READ    = 0xaa,
134 	MS_CMD_CLEAR_BUF     = 0xc3,
135 	MS_CMD_FLASH_STOP    = 0xcc,
136 	MSPRO_CMD_FORMAT     = 0x10,
137 	MSPRO_CMD_SLEEP      = 0x11,
138 	MSPRO_CMD_READ_DATA  = 0x20,
139 	MSPRO_CMD_WRITE_DATA = 0x21,
140 	MSPRO_CMD_READ_ATRB  = 0x24,
141 	MSPRO_CMD_STOP       = 0x25,
142 	MSPRO_CMD_ERASE      = 0x26,
143 	MSPRO_CMD_SET_IBA    = 0x46,
144 	MSPRO_CMD_SET_IBD    = 0x47
145 /*
146 	MSPRO_CMD_RESET
147 	MSPRO_CMD_WAKEUP
148 	MSPRO_CMD_IN_IO_DATA
149 	MSPRO_CMD_OUT_IO_DATA
150 	MSPRO_CMD_READ_IO_ATRB
151 	MSPRO_CMD_IN_IO_FIFO
152 	MSPRO_CMD_OUT_IO_FIFO
153 	MSPRO_CMD_IN_IOM
154 	MSPRO_CMD_OUT_IOM
155 */
156 };
157 
158 /*** Driver structures and functions ***/
159 
160 #define MEMSTICK_PART_SHIFT 3
161 
162 enum memstick_param { MEMSTICK_POWER = 1, MEMSTICK_INTERFACE };
163 
164 #define MEMSTICK_POWER_OFF 0
165 #define MEMSTICK_POWER_ON  1
166 
167 #define MEMSTICK_SERIAL   0
168 #define MEMSTICK_PARALLEL 1
169 
170 struct memstick_host;
171 struct memstick_driver;
172 
173 #define MEMSTICK_MATCH_ALL            0x01
174 
175 #define MEMSTICK_TYPE_LEGACY          0xff
176 #define MEMSTICK_TYPE_DUO             0x00
177 #define MEMSTICK_TYPE_PRO             0x01
178 
179 #define MEMSTICK_CATEGORY_STORAGE     0xff
180 #define MEMSTICK_CATEGORY_STORAGE_DUO 0x00
181 
182 #define MEMSTICK_CLASS_GENERIC        0xff
183 #define MEMSTICK_CLASS_GENERIC_DUO    0x00
184 
185 
186 struct memstick_device_id {
187 	unsigned char match_flags;
188 	unsigned char type;
189 	unsigned char category;
190 	unsigned char class;
191 };
192 
193 struct memstick_request {
194 	unsigned char tpc;
195 	unsigned char data_dir:1,
196 		      need_card_int:1,
197 		      get_int_reg:1,
198 		      io_type:2;
199 #define               MEMSTICK_IO_NONE 0
200 #define               MEMSTICK_IO_VAL  1
201 #define               MEMSTICK_IO_SG   2
202 
203 	unsigned char int_reg;
204 	int           error;
205 	union {
206 		struct scatterlist sg;
207 		struct {
208 			unsigned char data_len;
209 			unsigned char data[15];
210 		};
211 	};
212 };
213 
214 struct memstick_dev {
215 	struct memstick_device_id id;
216 	struct memstick_host     *host;
217 	struct ms_register_addr  reg_addr;
218 	struct completion        mrq_complete;
219 	struct memstick_request  current_mrq;
220 
221 	/* Check that media driver is still willing to operate the device. */
222 	int                      (*check)(struct memstick_dev *card);
223 	/* Get next request from the media driver.                         */
224 	int                      (*next_request)(struct memstick_dev *card,
225 						 struct memstick_request **mrq);
226 
227 	struct device            dev;
228 };
229 
230 struct memstick_host {
231 	struct mutex        lock;
232 	unsigned int        id;
233 	unsigned int        caps;
234 #define MEMSTICK_CAP_PARALLEL      1
235 #define MEMSTICK_CAP_AUTO_GET_INT  2
236 
237 	struct work_struct  media_checker;
238 	struct class_device cdev;
239 
240 	struct memstick_dev *card;
241 	unsigned int        retries;
242 
243 	/* Notify the host that some requests are pending. */
244 	void                (*request)(struct memstick_host *host);
245 	/* Set host IO parameters (power, clock, etc).     */
246 	void                (*set_param)(struct memstick_host *host,
247 					 enum memstick_param param,
248 					 int value);
249 	unsigned long       private[0] ____cacheline_aligned;
250 };
251 
252 struct memstick_driver {
253 	struct memstick_device_id *id_table;
254 	int                       (*probe)(struct memstick_dev *card);
255 	void                      (*remove)(struct memstick_dev *card);
256 	int                       (*suspend)(struct memstick_dev *card,
257 					     pm_message_t state);
258 	int                       (*resume)(struct memstick_dev *card);
259 
260 	struct device_driver      driver;
261 };
262 
263 int memstick_register_driver(struct memstick_driver *drv);
264 void memstick_unregister_driver(struct memstick_driver *drv);
265 
266 struct memstick_host *memstick_alloc_host(unsigned int extra,
267 					  struct device *dev);
268 
269 int memstick_add_host(struct memstick_host *host);
270 void memstick_remove_host(struct memstick_host *host);
271 void memstick_free_host(struct memstick_host *host);
272 void memstick_detect_change(struct memstick_host *host);
273 
274 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
275 			  struct scatterlist *sg);
276 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
277 		       void *buf, size_t length);
278 int memstick_next_req(struct memstick_host *host,
279 		      struct memstick_request **mrq);
280 void memstick_new_req(struct memstick_host *host);
281 
282 int memstick_set_rw_addr(struct memstick_dev *card);
283 
284 static inline void *memstick_priv(struct memstick_host *host)
285 {
286 	return (void *)host->private;
287 }
288 
289 static inline void *memstick_get_drvdata(struct memstick_dev *card)
290 {
291 	return dev_get_drvdata(&card->dev);
292 }
293 
294 static inline void memstick_set_drvdata(struct memstick_dev *card, void *data)
295 {
296 	dev_set_drvdata(&card->dev, data);
297 }
298 
299 #endif
300