memstick.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 0x0001
  21. #define MEMSTICK_INT_IOREQ 0x0008
  22. #define MEMSTICK_INT_IOBREQ 0x0010
  23. #define MEMSTICK_INT_BREQ 0x0020
  24. #define MEMSTICK_INT_ERR 0x0040
  25. #define MEMSTICK_INT_CED 0x0080
  26. unsigned char status0;
  27. #define MEMSTICK_STATUS0_WP 0x0001
  28. #define MEMSTICK_STATUS0_SL 0x0002
  29. #define MEMSTICK_STATUS0_BF 0x0010
  30. #define MEMSTICK_STATUS0_BE 0x0020
  31. #define MEMSTICK_STATUS0_FB0 0x0040
  32. #define MEMSTICK_STATUS0_MB 0x0080
  33. unsigned char status1;
  34. #define MEMSTICK_STATUS1_UCFG 0x0001
  35. #define MEMSTICK_STATUS1_FGER 0x0002
  36. #define MEMSTICK_STATUS1_UCEX 0x0004
  37. #define MEMSTICK_STATUS1_EXER 0x0008
  38. #define MEMSTICK_STATUS1_UCDT 0x0010
  39. #define MEMSTICK_STATUS1_DTER 0x0020
  40. #define MEMSTICK_STATUS1_FBI 0x0040
  41. #define MEMSTICK_STATUS1_MB 0x0080
  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_ATEN 0xc0
  52. #define MEMSTICK_SYS_BAMD 0x80
  53. #define MEMSTICK_SYS_PAM 0x08
  54. unsigned char block_address_msb;
  55. unsigned short block_address;
  56. unsigned char cp;
  57. #define MEMSTICK_CP_BLOCK 0x0000
  58. #define MEMSTICK_CP_PAGE 0x0020
  59. #define MEMSTICK_CP_EXTRA 0x0040
  60. #define MEMSTICK_CP_OVERWRITE 0x0080
  61. unsigned char page_address;
  62. } __attribute__((packed));
  63. struct ms_extra_data_register {
  64. unsigned char overwrite_flag;
  65. #define MEMSTICK_OVERWRITE_UPDATA 0x0010
  66. #define MEMSTICK_OVERWRITE_PAGE 0x0060
  67. #define MEMSTICK_OVERWRITE_BLOCK 0x0080
  68. unsigned char management_flag;
  69. #define MEMSTICK_MANAGEMENT_SYSTEM 0x0004
  70. #define MEMSTICK_MANAGEMENT_TRANS_TABLE 0x0008
  71. #define MEMSTICK_MANAGEMENT_COPY 0x0010
  72. #define MEMSTICK_MANAGEMENT_ACCESS 0x0020
  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_SERIAL 0x80
  85. #define MEMSTICK_SYS_PAR4 0x00
  86. #define MEMSTICK_SYS_PAR8 0x40
  87. unsigned short data_count;
  88. unsigned int 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 {
  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 {
  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. #define MEMSTICK_PART_SHIFT 3
  180. enum memstick_param { MEMSTICK_POWER = 1, MEMSTICK_INTERFACE };
  181. #define MEMSTICK_POWER_OFF 0
  182. #define MEMSTICK_POWER_ON 1
  183. #define MEMSTICK_SERIAL 0
  184. #define MEMSTICK_PAR4 1
  185. #define MEMSTICK_PAR8 2
  186. struct memstick_host;
  187. struct memstick_driver;
  188. #define MEMSTICK_MATCH_ALL 0x01
  189. #define MEMSTICK_TYPE_LEGACY 0xff
  190. #define MEMSTICK_TYPE_DUO 0x00
  191. #define MEMSTICK_TYPE_PRO 0x01
  192. #define MEMSTICK_CATEGORY_STORAGE 0xff
  193. #define MEMSTICK_CATEGORY_STORAGE_DUO 0x00
  194. #define MEMSTICK_CLASS_GENERIC 0xff
  195. #define MEMSTICK_CLASS_GENERIC_DUO 0x00
  196. struct memstick_device_id {
  197. unsigned char match_flags;
  198. unsigned char type;
  199. unsigned char category;
  200. unsigned char class;
  201. };
  202. struct memstick_request {
  203. unsigned char tpc;
  204. unsigned char data_dir:1,
  205. need_card_int:1,
  206. long_data:1;
  207. unsigned char int_reg;
  208. int error;
  209. union {
  210. struct scatterlist sg;
  211. struct {
  212. unsigned char data_len;
  213. unsigned char data[15];
  214. };
  215. };
  216. };
  217. struct memstick_dev {
  218. struct memstick_device_id id;
  219. struct memstick_host *host;
  220. struct ms_register_addr reg_addr;
  221. struct completion mrq_complete;
  222. struct memstick_request current_mrq;
  223. /* Check that media driver is still willing to operate the device. */
  224. int (*check)(struct memstick_dev *card);
  225. /* Get next request from the media driver. */
  226. int (*next_request)(struct memstick_dev *card,
  227. struct memstick_request **mrq);
  228. struct device dev;
  229. };
  230. struct memstick_host {
  231. struct mutex lock;
  232. unsigned int id;
  233. unsigned int caps;
  234. #define MEMSTICK_CAP_AUTO_GET_INT 1
  235. #define MEMSTICK_CAP_PAR4 2
  236. #define MEMSTICK_CAP_PAR8 4
  237. struct work_struct media_checker;
  238. struct device dev;
  239. struct memstick_dev *card;
  240. unsigned int retries;
  241. /* Notify the host that some requests are pending. */
  242. void (*request)(struct memstick_host *host);
  243. /* Set host IO parameters (power, clock, etc). */
  244. int (*set_param)(struct memstick_host *host,
  245. enum memstick_param param,
  246. int value);
  247. unsigned long private[0] ____cacheline_aligned;
  248. };
  249. struct memstick_driver {
  250. struct memstick_device_id *id_table;
  251. int (*probe)(struct memstick_dev *card);
  252. void (*remove)(struct memstick_dev *card);
  253. int (*suspend)(struct memstick_dev *card,
  254. pm_message_t state);
  255. int (*resume)(struct memstick_dev *card);
  256. struct device_driver driver;
  257. };
  258. int memstick_register_driver(struct memstick_driver *drv);
  259. void memstick_unregister_driver(struct memstick_driver *drv);
  260. struct memstick_host *memstick_alloc_host(unsigned int extra,
  261. struct device *dev);
  262. int memstick_add_host(struct memstick_host *host);
  263. void memstick_remove_host(struct memstick_host *host);
  264. void memstick_free_host(struct memstick_host *host);
  265. void memstick_detect_change(struct memstick_host *host);
  266. void memstick_suspend_host(struct memstick_host *host);
  267. void memstick_resume_host(struct memstick_host *host);
  268. void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
  269. struct scatterlist *sg);
  270. void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
  271. void *buf, size_t length);
  272. int memstick_next_req(struct memstick_host *host,
  273. struct memstick_request **mrq);
  274. void memstick_new_req(struct memstick_host *host);
  275. int memstick_set_rw_addr(struct memstick_dev *card);
  276. static inline void *memstick_priv(struct memstick_host *host)
  277. {
  278. return (void *)host->private;
  279. }
  280. static inline void *memstick_get_drvdata(struct memstick_dev *card)
  281. {
  282. return dev_get_drvdata(&card->dev);
  283. }
  284. static inline void memstick_set_drvdata(struct memstick_dev *card, void *data)
  285. {
  286. dev_set_drvdata(&card->dev, data);
  287. }
  288. #endif