memstick.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Sony MemoryStick support
  3. *
  4. * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
  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. #ifndef _MEMSTICK_H
  12. #define _MEMSTICK_H
  13. #include <linux/workqueue.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/device.h>
  16. /*** Hardware based structures ***/
  17. struct ms_status_register {
  18. unsigned char reserved;
  19. unsigned char interrupt;
  20. #define MEMSTICK_INT_CMDNAK 0x01
  21. #define MEMSTICK_INT_IOREQ 0x08
  22. #define MEMSTICK_INT_IOBREQ 0x10
  23. #define MEMSTICK_INT_BREQ 0x20
  24. #define MEMSTICK_INT_ERR 0x40
  25. #define MEMSTICK_INT_CED 0x80
  26. unsigned char status0;
  27. #define MEMSTICK_STATUS0_WP 0x01
  28. #define MEMSTICK_STATUS0_SL 0x02
  29. #define MEMSTICK_STATUS0_BF 0x10
  30. #define MEMSTICK_STATUS0_BE 0x20
  31. #define MEMSTICK_STATUS0_FB0 0x40
  32. #define MEMSTICK_STATUS0_MB 0x80
  33. unsigned char status1;
  34. #define MEMSTICK_STATUS1_UCFG 0x01
  35. #define MEMSTICK_STATUS1_FGER 0x02
  36. #define MEMSTICK_STATUS1_UCEX 0x04
  37. #define MEMSTICK_STATUS1_EXER 0x08
  38. #define MEMSTICK_STATUS1_UCDT 0x10
  39. #define MEMSTICK_STATUS1_DTER 0x20
  40. #define MEMSTICK_STATUS1_FB1 0x40
  41. #define MEMSTICK_STATUS1_MB 0x80
  42. } __attribute__((packed));
  43. struct ms_id_register {
  44. unsigned char type;
  45. unsigned char if_mode;
  46. unsigned char category;
  47. unsigned char class;
  48. } __attribute__((packed));
  49. struct ms_param_register {
  50. unsigned char system;
  51. #define MEMSTICK_SYS_PAM 0x08
  52. #define MEMSTICK_SYS_BAMD 0x80
  53. unsigned char block_address_msb;
  54. unsigned short block_address;
  55. unsigned char cp;
  56. #define MEMSTICK_CP_BLOCK 0x00
  57. #define MEMSTICK_CP_PAGE 0x20
  58. #define MEMSTICK_CP_EXTRA 0x40
  59. #define MEMSTICK_CP_OVERWRITE 0x80
  60. unsigned char page_address;
  61. } __attribute__((packed));
  62. struct ms_extra_data_register {
  63. unsigned char overwrite_flag;
  64. #define MEMSTICK_OVERWRITE_UDST 0x10
  65. #define MEMSTICK_OVERWRITE_PGST1 0x20
  66. #define MEMSTICK_OVERWRITE_PGST0 0x40
  67. #define MEMSTICK_OVERWRITE_BKST 0x80
  68. unsigned char management_flag;
  69. #define MEMSTICK_MANAGEMENT_SYSFLG 0x04
  70. #define MEMSTICK_MANAGEMENT_ATFLG 0x08
  71. #define MEMSTICK_MANAGEMENT_SCMS1 0x10
  72. #define MEMSTICK_MANAGEMENT_SCMS0 0x20
  73. unsigned short logical_address;
  74. } __attribute__((packed));
  75. struct ms_register {
  76. struct ms_status_register status;
  77. struct ms_id_register id;
  78. unsigned char reserved[8];
  79. struct ms_param_register param;
  80. struct ms_extra_data_register extra_data;
  81. } __attribute__((packed));
  82. struct mspro_param_register {
  83. unsigned char system;
  84. #define MEMSTICK_SYS_PAR4 0x00
  85. #define MEMSTICK_SYS_PAR8 0x40
  86. #define MEMSTICK_SYS_SERIAL 0x80
  87. __be16 data_count;
  88. __be32 data_address;
  89. unsigned char tpc_param;
  90. } __attribute__((packed));
  91. struct mspro_io_info_register {
  92. unsigned char version;
  93. unsigned char io_category;
  94. unsigned char current_req;
  95. unsigned char card_opt_info;
  96. unsigned char rdy_wait_time;
  97. } __attribute__((packed));
  98. struct mspro_io_func_register {
  99. unsigned char func_enable;
  100. unsigned char func_select;
  101. unsigned char func_intmask;
  102. unsigned char transfer_mode;
  103. } __attribute__((packed));
  104. struct mspro_io_cmd_register {
  105. unsigned short tpc_param;
  106. unsigned short data_count;
  107. unsigned int data_address;
  108. } __attribute__((packed));
  109. struct mspro_register {
  110. struct ms_status_register status;
  111. struct ms_id_register id;
  112. unsigned char reserved0[8];
  113. struct mspro_param_register param;
  114. unsigned char reserved1[8];
  115. struct mspro_io_info_register io_info;
  116. struct mspro_io_func_register io_func;
  117. unsigned char reserved2[7];
  118. struct mspro_io_cmd_register io_cmd;
  119. unsigned char io_int;
  120. unsigned char io_int_func;
  121. } __attribute__((packed));
  122. struct ms_register_addr {
  123. unsigned char r_offset;
  124. unsigned char r_length;
  125. unsigned char w_offset;
  126. unsigned char w_length;
  127. } __attribute__((packed));
  128. enum memstick_tpc {
  129. MS_TPC_READ_MG_STATUS = 0x01,
  130. MS_TPC_READ_LONG_DATA = 0x02,
  131. MS_TPC_READ_SHORT_DATA = 0x03,
  132. MS_TPC_READ_MG_DATA = 0x03,
  133. MS_TPC_READ_REG = 0x04,
  134. MS_TPC_READ_QUAD_DATA = 0x05,
  135. MS_TPC_READ_IO_DATA = 0x05,
  136. MS_TPC_GET_INT = 0x07,
  137. MS_TPC_SET_RW_REG_ADRS = 0x08,
  138. MS_TPC_EX_SET_CMD = 0x09,
  139. MS_TPC_WRITE_QUAD_DATA = 0x0a,
  140. MS_TPC_WRITE_IO_DATA = 0x0a,
  141. MS_TPC_WRITE_REG = 0x0b,
  142. MS_TPC_WRITE_SHORT_DATA = 0x0c,
  143. MS_TPC_WRITE_MG_DATA = 0x0c,
  144. MS_TPC_WRITE_LONG_DATA = 0x0d,
  145. MS_TPC_SET_CMD = 0x0e
  146. };
  147. enum memstick_command {
  148. MS_CMD_BLOCK_END = 0x33,
  149. MS_CMD_RESET = 0x3c,
  150. MS_CMD_BLOCK_WRITE = 0x55,
  151. MS_CMD_SLEEP = 0x5a,
  152. MS_CMD_BLOCK_ERASE = 0x99,
  153. MS_CMD_BLOCK_READ = 0xaa,
  154. MS_CMD_CLEAR_BUF = 0xc3,
  155. MS_CMD_FLASH_STOP = 0xcc,
  156. MS_CMD_LOAD_ID = 0x60,
  157. MS_CMD_CMP_ICV = 0x7f,
  158. MSPRO_CMD_FORMAT = 0x10,
  159. MSPRO_CMD_SLEEP = 0x11,
  160. MSPRO_CMD_WAKEUP = 0x12,
  161. MSPRO_CMD_READ_DATA = 0x20,
  162. MSPRO_CMD_WRITE_DATA = 0x21,
  163. MSPRO_CMD_READ_ATRB = 0x24,
  164. MSPRO_CMD_STOP = 0x25,
  165. MSPRO_CMD_ERASE = 0x26,
  166. MSPRO_CMD_READ_QUAD = 0x27,
  167. MSPRO_CMD_WRITE_QUAD = 0x28,
  168. MSPRO_CMD_SET_IBD = 0x46,
  169. MSPRO_CMD_GET_IBD = 0x47,
  170. MSPRO_CMD_IN_IO_DATA = 0xb0,
  171. MSPRO_CMD_OUT_IO_DATA = 0xb1,
  172. MSPRO_CMD_READ_IO_ATRB = 0xb2,
  173. MSPRO_CMD_IN_IO_FIFO = 0xb3,
  174. MSPRO_CMD_OUT_IO_FIFO = 0xb4,
  175. MSPRO_CMD_IN_IOM = 0xb5,
  176. MSPRO_CMD_OUT_IOM = 0xb6,
  177. };
  178. /*** Driver structures and functions ***/
  179. enum memstick_param { MEMSTICK_POWER = 1, MEMSTICK_INTERFACE };
  180. #define MEMSTICK_POWER_OFF 0
  181. #define MEMSTICK_POWER_ON 1
  182. #define MEMSTICK_SERIAL 0
  183. #define MEMSTICK_PAR4 1
  184. #define MEMSTICK_PAR8 2
  185. struct memstick_host;
  186. struct memstick_driver;
  187. struct memstick_device_id {
  188. unsigned char match_flags;
  189. #define MEMSTICK_MATCH_ALL 0x01
  190. unsigned char type;
  191. #define MEMSTICK_TYPE_LEGACY 0xff
  192. #define MEMSTICK_TYPE_DUO 0x00
  193. #define MEMSTICK_TYPE_PRO 0x01
  194. unsigned char category;
  195. #define MEMSTICK_CATEGORY_STORAGE 0xff
  196. #define MEMSTICK_CATEGORY_STORAGE_DUO 0x00
  197. #define MEMSTICK_CATEGORY_IO 0x01
  198. #define MEMSTICK_CATEGORY_IO_PRO 0x10
  199. unsigned char class;
  200. #define MEMSTICK_CLASS_FLASH 0xff
  201. #define MEMSTICK_CLASS_DUO 0x00
  202. #define MEMSTICK_CLASS_ROM 0x01
  203. #define MEMSTICK_CLASS_RO 0x02
  204. #define MEMSTICK_CLASS_WP 0x03
  205. };
  206. struct memstick_request {
  207. unsigned char tpc;
  208. unsigned char data_dir:1,
  209. need_card_int:1,
  210. long_data:1;
  211. unsigned char int_reg;
  212. int error;
  213. union {
  214. struct scatterlist sg;
  215. struct {
  216. unsigned char data_len;
  217. unsigned char data[15];
  218. };
  219. };
  220. };
  221. struct memstick_dev {
  222. struct memstick_device_id id;
  223. struct memstick_host *host;
  224. struct ms_register_addr reg_addr;
  225. struct completion mrq_complete;
  226. struct memstick_request current_mrq;
  227. /* Check that media driver is still willing to operate the device. */
  228. int (*check)(struct memstick_dev *card);
  229. /* Get next request from the media driver. */
  230. int (*next_request)(struct memstick_dev *card,
  231. struct memstick_request **mrq);
  232. /* Tell the media driver to stop doing things */
  233. void (*stop)(struct memstick_dev *card);
  234. /* Allow the media driver to continue */
  235. void (*start)(struct memstick_dev *card);
  236. struct device dev;
  237. };
  238. struct memstick_host {
  239. struct mutex lock;
  240. unsigned int id;
  241. unsigned int caps;
  242. #define MEMSTICK_CAP_AUTO_GET_INT 1
  243. #define MEMSTICK_CAP_PAR4 2
  244. #define MEMSTICK_CAP_PAR8 4
  245. struct work_struct media_checker;
  246. struct device dev;
  247. struct memstick_dev *card;
  248. unsigned int retries;
  249. /* Notify the host that some requests are pending. */
  250. void (*request)(struct memstick_host *host);
  251. /* Set host IO parameters (power, clock, etc). */
  252. int (*set_param)(struct memstick_host *host,
  253. enum memstick_param param,
  254. int value);
  255. unsigned long private[0] ____cacheline_aligned;
  256. };
  257. struct memstick_driver {
  258. struct memstick_device_id *id_table;
  259. int (*probe)(struct memstick_dev *card);
  260. void (*remove)(struct memstick_dev *card);
  261. int (*suspend)(struct memstick_dev *card,
  262. pm_message_t state);
  263. int (*resume)(struct memstick_dev *card);
  264. struct device_driver driver;
  265. };
  266. int memstick_register_driver(struct memstick_driver *drv);
  267. void memstick_unregister_driver(struct memstick_driver *drv);
  268. struct memstick_host *memstick_alloc_host(unsigned int extra,
  269. struct device *dev);
  270. int memstick_add_host(struct memstick_host *host);
  271. void memstick_remove_host(struct memstick_host *host);
  272. void memstick_free_host(struct memstick_host *host);
  273. void memstick_detect_change(struct memstick_host *host);
  274. void memstick_suspend_host(struct memstick_host *host);
  275. void memstick_resume_host(struct memstick_host *host);
  276. void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
  277. const struct scatterlist *sg);
  278. void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
  279. const void *buf, size_t length);
  280. int memstick_next_req(struct memstick_host *host,
  281. struct memstick_request **mrq);
  282. void memstick_new_req(struct memstick_host *host);
  283. int memstick_set_rw_addr(struct memstick_dev *card);
  284. static inline void *memstick_priv(struct memstick_host *host)
  285. {
  286. return (void *)host->private;
  287. }
  288. static inline void *memstick_get_drvdata(struct memstick_dev *card)
  289. {
  290. return dev_get_drvdata(&card->dev);
  291. }
  292. static inline void memstick_set_drvdata(struct memstick_dev *card, void *data)
  293. {
  294. dev_set_drvdata(&card->dev, data);
  295. }
  296. #endif