2 * Sony MemoryStick support
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
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.
15 #include <linux/workqueue.h>
16 #include <linux/scatterlist.h>
17 #include <linux/device.h>
19 /*** Hardware based structures ***/
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
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
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));
48 struct ms_id_register {
50 unsigned char reserved;
51 unsigned char category;
53 } __attribute__((packed));
55 struct ms_param_register {
57 unsigned char block_address_msb;
58 unsigned short block_address;
60 #define MEMSTICK_CP_BLOCK 0x0000
61 #define MEMSTICK_CP_PAGE 0x0020
62 #define MEMSTICK_CP_EXTRA 0x0040
63 #define MEMSTICK_CP_OVERWRITE 0x0080
65 unsigned char page_address;
66 } __attribute__((packed));
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
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
80 unsigned short logical_address;
81 } __attribute__((packed));
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));
91 struct mspro_param_register {
93 unsigned short data_count;
94 unsigned int data_address;
95 unsigned char cmd_param;
96 } __attribute__((packed));
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));
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));
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
128 MS_CMD_BLOCK_END = 0x33,
130 MS_CMD_BLOCK_WRITE = 0x55,
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
149 MSPRO_CMD_OUT_IO_DATA
150 MSPRO_CMD_READ_IO_ATRB
152 MSPRO_CMD_OUT_IO_FIFO
158 /*** Driver structures and functions ***/
160 #define MEMSTICK_PART_SHIFT 3
162 enum memstick_param { MEMSTICK_POWER = 1, MEMSTICK_INTERFACE };
164 #define MEMSTICK_POWER_OFF 0
165 #define MEMSTICK_POWER_ON 1
167 #define MEMSTICK_SERIAL 0
168 #define MEMSTICK_PARALLEL 1
170 struct memstick_host;
171 struct memstick_driver;
173 #define MEMSTICK_MATCH_ALL 0x01
175 #define MEMSTICK_TYPE_LEGACY 0xff
176 #define MEMSTICK_TYPE_DUO 0x00
177 #define MEMSTICK_TYPE_PRO 0x01
179 #define MEMSTICK_CATEGORY_STORAGE 0xff
180 #define MEMSTICK_CATEGORY_STORAGE_DUO 0x00
182 #define MEMSTICK_CLASS_GENERIC 0xff
183 #define MEMSTICK_CLASS_GENERIC_DUO 0x00
186 struct memstick_device_id {
187 unsigned char match_flags;
189 unsigned char category;
193 struct memstick_request {
195 unsigned char data_dir:1,
199 #define MEMSTICK_IO_NONE 0
200 #define MEMSTICK_IO_VAL 1
201 #define MEMSTICK_IO_SG 2
203 unsigned char int_reg;
206 struct scatterlist sg;
208 unsigned char data_len;
209 unsigned char data[15];
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;
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);
230 struct memstick_host {
234 #define MEMSTICK_CAP_PARALLEL 1
235 #define MEMSTICK_CAP_AUTO_GET_INT 2
237 struct work_struct media_checker;
238 struct class_device cdev;
240 struct memstick_dev *card;
241 unsigned int retries;
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,
249 unsigned long private[0] ____cacheline_aligned;
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,
258 int (*resume)(struct memstick_dev *card);
260 struct device_driver driver;
263 int memstick_register_driver(struct memstick_driver *drv);
264 void memstick_unregister_driver(struct memstick_driver *drv);
266 struct memstick_host *memstick_alloc_host(unsigned int extra,
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);
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);
282 int memstick_set_rw_addr(struct memstick_dev *card);
284 static inline void *memstick_priv(struct memstick_host *host)
286 return (void *)host->private;
289 static inline void *memstick_get_drvdata(struct memstick_dev *card)
291 return dev_get_drvdata(&card->dev);
294 static inline void memstick_set_drvdata(struct memstick_dev *card, void *data)
296 dev_set_drvdata(&card->dev, data);