realtek_cr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* Driver for Realtek RTS51xx USB card reader
  2. *
  3. * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2, or (at your option) any
  8. * later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author:
  19. * wwang (wei_wang@realsil.com.cn)
  20. * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
  21. */
  22. #include <linux/module.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/kthread.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/version.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <linux/cdrom.h>
  32. #include <linux/usb.h>
  33. #include <linux/slab.h>
  34. #include <linux/usb_usual.h>
  35. #include "usb.h"
  36. #include "transport.h"
  37. #include "protocol.h"
  38. #include "debug.h"
  39. MODULE_DESCRIPTION("Driver for Realtek USB Card Reader");
  40. MODULE_AUTHOR("wwang <wei_wang@realsil.com.cn>");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION("1.03");
  43. static int auto_delink_en = 1;
  44. module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
  45. MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
  46. #ifdef CONFIG_REALTEK_AUTOPM
  47. static int ss_en = 1;
  48. module_param(ss_en, int, S_IRUGO | S_IWUSR);
  49. MODULE_PARM_DESC(ss_en, "enable selective suspend");
  50. static int ss_delay = 50;
  51. module_param(ss_delay, int, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(ss_delay,
  53. "seconds to delay before entering selective suspend");
  54. enum RTS51X_STAT {
  55. RTS51X_STAT_INIT,
  56. RTS51X_STAT_IDLE,
  57. RTS51X_STAT_RUN,
  58. RTS51X_STAT_SS
  59. };
  60. #define POLLING_INTERVAL 50
  61. #define rts51x_set_stat(chip, stat) \
  62. ((chip)->state = (enum RTS51X_STAT)(stat))
  63. #define rts51x_get_stat(chip) ((chip)->state)
  64. #define SET_LUN_READY(chip, lun) ((chip)->lun_ready |= ((u8)1 << (lun)))
  65. #define CLR_LUN_READY(chip, lun) ((chip)->lun_ready &= ~((u8)1 << (lun)))
  66. #define TST_LUN_READY(chip, lun) ((chip)->lun_ready & ((u8)1 << (lun)))
  67. #endif
  68. struct rts51x_status {
  69. u16 vid;
  70. u16 pid;
  71. u8 cur_lun;
  72. u8 card_type;
  73. u8 total_lun;
  74. u16 fw_ver;
  75. u8 phy_exist;
  76. u8 multi_flag;
  77. u8 multi_card;
  78. u8 log_exist;
  79. union {
  80. u8 detailed_type1;
  81. u8 detailed_type2;
  82. } detailed_type;
  83. u8 function[2];
  84. };
  85. struct rts51x_chip {
  86. u16 vendor_id;
  87. u16 product_id;
  88. char max_lun;
  89. struct rts51x_status *status;
  90. int status_len;
  91. u32 flag;
  92. #ifdef CONFIG_REALTEK_AUTOPM
  93. struct us_data *us;
  94. struct timer_list rts51x_suspend_timer;
  95. unsigned long timer_expires;
  96. int pwr_state;
  97. u8 lun_ready;
  98. enum RTS51X_STAT state;
  99. int support_auto_delink;
  100. #endif
  101. /* used to back up the protocal choosen in probe1 phase */
  102. proto_cmnd proto_handler_backup;
  103. };
  104. /* flag definition */
  105. #define FLIDX_AUTO_DELINK 0x01
  106. #define SCSI_LUN(srb) ((srb)->device->lun)
  107. /* Bit Operation */
  108. #define SET_BIT(data, idx) ((data) |= 1 << (idx))
  109. #define CLR_BIT(data, idx) ((data) &= ~(1 << (idx)))
  110. #define CHK_BIT(data, idx) ((data) & (1 << (idx)))
  111. #define SET_AUTO_DELINK(chip) ((chip)->flag |= FLIDX_AUTO_DELINK)
  112. #define CLR_AUTO_DELINK(chip) ((chip)->flag &= ~FLIDX_AUTO_DELINK)
  113. #define CHK_AUTO_DELINK(chip) ((chip)->flag & FLIDX_AUTO_DELINK)
  114. #define RTS51X_GET_VID(chip) ((chip)->vendor_id)
  115. #define RTS51X_GET_PID(chip) ((chip)->product_id)
  116. #define VENDOR_ID(chip) ((chip)->status[0].vid)
  117. #define PRODUCT_ID(chip) ((chip)->status[0].pid)
  118. #define FW_VERSION(chip) ((chip)->status[0].fw_ver)
  119. #define STATUS_LEN(chip) ((chip)->status_len)
  120. #define STATUS_SUCCESS 0
  121. #define STATUS_FAIL 1
  122. /* Check card reader function */
  123. #define SUPPORT_DETAILED_TYPE1(chip) \
  124. CHK_BIT((chip)->status[0].function[0], 1)
  125. #define SUPPORT_OT(chip) \
  126. CHK_BIT((chip)->status[0].function[0], 2)
  127. #define SUPPORT_OC(chip) \
  128. CHK_BIT((chip)->status[0].function[0], 3)
  129. #define SUPPORT_AUTO_DELINK(chip) \
  130. CHK_BIT((chip)->status[0].function[0], 4)
  131. #define SUPPORT_SDIO(chip) \
  132. CHK_BIT((chip)->status[0].function[1], 0)
  133. #define SUPPORT_DETAILED_TYPE2(chip) \
  134. CHK_BIT((chip)->status[0].function[1], 1)
  135. #define CHECK_PID(chip, pid) (RTS51X_GET_PID(chip) == (pid))
  136. #define CHECK_FW_VER(chip, fw_ver) (FW_VERSION(chip) == (fw_ver))
  137. #define CHECK_ID(chip, pid, fw_ver) \
  138. (CHECK_PID((chip), (pid)) && CHECK_FW_VER((chip), (fw_ver)))
  139. static int init_realtek_cr(struct us_data *us);
  140. /*
  141. * The table of devices
  142. */
  143. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  144. vendorName, productName, useProtocol, useTransport, \
  145. initFunction, flags) \
  146. {\
  147. USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  148. .driver_info = (flags)|(USB_US_TYPE_STOR<<24)\
  149. }
  150. static const struct usb_device_id realtek_cr_ids[] = {
  151. # include "unusual_realtek.h"
  152. {} /* Terminating entry */
  153. };
  154. MODULE_DEVICE_TABLE(usb, realtek_cr_ids);
  155. #undef UNUSUAL_DEV
  156. /*
  157. * The flags table
  158. */
  159. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  160. vendor_name, product_name, use_protocol, use_transport, \
  161. init_function, Flags) \
  162. { \
  163. .vendorName = vendor_name, \
  164. .productName = product_name, \
  165. .useProtocol = use_protocol, \
  166. .useTransport = use_transport, \
  167. .initFunction = init_function, \
  168. }
  169. static struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
  170. # include "unusual_realtek.h"
  171. {} /* Terminating entry */
  172. };
  173. #undef UNUSUAL_DEV
  174. static int rts51x_bulk_transport(struct us_data *us, u8 lun,
  175. u8 *cmd, int cmd_len, u8 *buf, int buf_len,
  176. enum dma_data_direction dir, int *act_len)
  177. {
  178. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;
  179. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *)us->iobuf;
  180. int result;
  181. unsigned int residue;
  182. unsigned int cswlen;
  183. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  184. /* set up the command wrapper */
  185. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  186. bcb->DataTransferLength = cpu_to_le32(buf_len);
  187. bcb->Flags = (dir == DMA_FROM_DEVICE) ? 1 << 7 : 0;
  188. bcb->Tag = ++us->tag;
  189. bcb->Lun = lun;
  190. bcb->Length = cmd_len;
  191. /* copy the command payload */
  192. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  193. memcpy(bcb->CDB, cmd, bcb->Length);
  194. /* send it to out endpoint */
  195. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  196. bcb, cbwlen, NULL);
  197. if (result != USB_STOR_XFER_GOOD)
  198. return USB_STOR_TRANSPORT_ERROR;
  199. /* DATA STAGE */
  200. /* send/receive data payload, if there is any */
  201. if (buf && buf_len) {
  202. unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
  203. us->recv_bulk_pipe : us->send_bulk_pipe;
  204. result = usb_stor_bulk_transfer_buf(us, pipe,
  205. buf, buf_len, NULL);
  206. if (result == USB_STOR_XFER_ERROR)
  207. return USB_STOR_TRANSPORT_ERROR;
  208. }
  209. /* get CSW for device status */
  210. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  211. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  212. if (result != USB_STOR_XFER_GOOD)
  213. return USB_STOR_TRANSPORT_ERROR;
  214. /* check bulk status */
  215. if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN)) {
  216. US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
  217. le32_to_cpu(bcs->Signature), US_BULK_CS_SIGN);
  218. return USB_STOR_TRANSPORT_ERROR;
  219. }
  220. residue = bcs->Residue;
  221. if (bcs->Tag != us->tag)
  222. return USB_STOR_TRANSPORT_ERROR;
  223. /* try to compute the actual residue, based on how much data
  224. * was really transferred and what the device tells us */
  225. if (residue)
  226. residue = residue < buf_len ? residue : buf_len;
  227. if (act_len)
  228. *act_len = buf_len - residue;
  229. /* based on the status code, we report good or bad */
  230. switch (bcs->Status) {
  231. case US_BULK_STAT_OK:
  232. /* command good -- note that data could be short */
  233. return USB_STOR_TRANSPORT_GOOD;
  234. case US_BULK_STAT_FAIL:
  235. /* command failed */
  236. return USB_STOR_TRANSPORT_FAILED;
  237. case US_BULK_STAT_PHASE:
  238. /* phase error -- note that a transport reset will be
  239. * invoked by the invoke_transport() function
  240. */
  241. return USB_STOR_TRANSPORT_ERROR;
  242. }
  243. /* we should never get here, but if we do, we're in trouble */
  244. return USB_STOR_TRANSPORT_ERROR;
  245. }
  246. static int rts51x_bulk_transport_special(struct us_data *us, u8 lun,
  247. u8 *cmd, int cmd_len, u8 *buf, int buf_len,
  248. enum dma_data_direction dir, int *act_len)
  249. {
  250. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  251. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  252. int result;
  253. unsigned int cswlen;
  254. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  255. /* set up the command wrapper */
  256. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  257. bcb->DataTransferLength = cpu_to_le32(buf_len);
  258. bcb->Flags = (dir == DMA_FROM_DEVICE) ? 1 << 7 : 0;
  259. bcb->Tag = ++us->tag;
  260. bcb->Lun = lun;
  261. bcb->Length = cmd_len;
  262. /* copy the command payload */
  263. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  264. memcpy(bcb->CDB, cmd, bcb->Length);
  265. /* send it to out endpoint */
  266. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  267. bcb, cbwlen, NULL);
  268. if (result != USB_STOR_XFER_GOOD)
  269. return USB_STOR_TRANSPORT_ERROR;
  270. /* DATA STAGE */
  271. /* send/receive data payload, if there is any */
  272. if (buf && buf_len) {
  273. unsigned int pipe = (dir == DMA_FROM_DEVICE) ?
  274. us->recv_bulk_pipe : us->send_bulk_pipe;
  275. result = usb_stor_bulk_transfer_buf(us, pipe,
  276. buf, buf_len, NULL);
  277. if (result == USB_STOR_XFER_ERROR)
  278. return USB_STOR_TRANSPORT_ERROR;
  279. }
  280. /* get CSW for device status */
  281. result = usb_bulk_msg(us->pusb_dev, us->recv_bulk_pipe, bcs,
  282. US_BULK_CS_WRAP_LEN, &cswlen, 250);
  283. return result;
  284. }
  285. /* Determine what the maximum LUN supported is */
  286. static int rts51x_get_max_lun(struct us_data *us)
  287. {
  288. int result;
  289. /* issue the command */
  290. us->iobuf[0] = 0;
  291. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  292. US_BULK_GET_MAX_LUN,
  293. USB_DIR_IN | USB_TYPE_CLASS |
  294. USB_RECIP_INTERFACE,
  295. 0, us->ifnum, us->iobuf, 1, 10 * HZ);
  296. US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
  297. result, us->iobuf[0]);
  298. /* if we have a successful request, return the result */
  299. if (result > 0)
  300. return us->iobuf[0];
  301. return 0;
  302. }
  303. static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
  304. {
  305. int retval;
  306. u8 cmnd[12] = { 0 };
  307. u8 *buf;
  308. buf = kmalloc(len, GFP_NOIO);
  309. if (buf == NULL)
  310. return USB_STOR_TRANSPORT_ERROR;
  311. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
  312. cmnd[0] = 0xF0;
  313. cmnd[1] = 0x0D;
  314. cmnd[2] = (u8) (addr >> 8);
  315. cmnd[3] = (u8) addr;
  316. cmnd[4] = (u8) (len >> 8);
  317. cmnd[5] = (u8) len;
  318. retval = rts51x_bulk_transport(us, 0, cmnd, 12,
  319. buf, len, DMA_FROM_DEVICE, NULL);
  320. if (retval != USB_STOR_TRANSPORT_GOOD) {
  321. kfree(buf);
  322. return -EIO;
  323. }
  324. memcpy(data, buf, len);
  325. kfree(buf);
  326. return 0;
  327. }
  328. static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
  329. {
  330. int retval;
  331. u8 cmnd[12] = { 0 };
  332. u8 *buf;
  333. buf = kmalloc(len, GFP_NOIO);
  334. if (buf == NULL)
  335. return USB_STOR_TRANSPORT_ERROR;
  336. memcpy(buf, data, len);
  337. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
  338. cmnd[0] = 0xF0;
  339. cmnd[1] = 0x0E;
  340. cmnd[2] = (u8) (addr >> 8);
  341. cmnd[3] = (u8) addr;
  342. cmnd[4] = (u8) (len >> 8);
  343. cmnd[5] = (u8) len;
  344. retval = rts51x_bulk_transport(us, 0, cmnd, 12,
  345. buf, len, DMA_TO_DEVICE, NULL);
  346. kfree(buf);
  347. if (retval != USB_STOR_TRANSPORT_GOOD)
  348. return -EIO;
  349. return 0;
  350. }
  351. static int rts51x_read_status(struct us_data *us,
  352. u8 lun, u8 *status, int len, int *actlen)
  353. {
  354. int retval;
  355. u8 cmnd[12] = { 0 };
  356. u8 *buf;
  357. buf = kmalloc(len, GFP_NOIO);
  358. if (buf == NULL)
  359. return USB_STOR_TRANSPORT_ERROR;
  360. US_DEBUGP("%s, lun = %d\n", __func__, lun);
  361. cmnd[0] = 0xF0;
  362. cmnd[1] = 0x09;
  363. retval = rts51x_bulk_transport(us, lun, cmnd, 12,
  364. buf, len, DMA_FROM_DEVICE, actlen);
  365. if (retval != USB_STOR_TRANSPORT_GOOD) {
  366. kfree(buf);
  367. return -EIO;
  368. }
  369. memcpy(status, buf, len);
  370. kfree(buf);
  371. return 0;
  372. }
  373. static int rts51x_check_status(struct us_data *us, u8 lun)
  374. {
  375. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  376. int retval;
  377. u8 buf[16];
  378. retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
  379. if (retval < 0)
  380. return -EIO;
  381. US_DEBUGP("chip->status_len = %d\n", chip->status_len);
  382. chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
  383. chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
  384. chip->status[lun].cur_lun = buf[4];
  385. chip->status[lun].card_type = buf[5];
  386. chip->status[lun].total_lun = buf[6];
  387. chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
  388. chip->status[lun].phy_exist = buf[9];
  389. chip->status[lun].multi_flag = buf[10];
  390. chip->status[lun].multi_card = buf[11];
  391. chip->status[lun].log_exist = buf[12];
  392. if (chip->status_len == 16) {
  393. chip->status[lun].detailed_type.detailed_type1 = buf[13];
  394. chip->status[lun].function[0] = buf[14];
  395. chip->status[lun].function[1] = buf[15];
  396. }
  397. return 0;
  398. }
  399. static int enable_oscillator(struct us_data *us)
  400. {
  401. int retval;
  402. u8 value;
  403. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  404. if (retval < 0)
  405. return -EIO;
  406. value |= 0x04;
  407. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  408. if (retval < 0)
  409. return -EIO;
  410. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  411. if (retval < 0)
  412. return -EIO;
  413. if (!(value & 0x04))
  414. return -EIO;
  415. return 0;
  416. }
  417. static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
  418. {
  419. int retval;
  420. u16 addr = 0xFE47;
  421. u8 cmnd[12] = {0};
  422. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __FUNCTION__, addr, len);
  423. cmnd[0] = 0xF0;
  424. cmnd[1] = 0x0E;
  425. cmnd[2] = (u8)(addr >> 8);
  426. cmnd[3] = (u8)addr;
  427. cmnd[4] = (u8)(len >> 8);
  428. cmnd[5] = (u8)len;
  429. retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, data, len, DMA_TO_DEVICE, NULL);
  430. if (retval != USB_STOR_TRANSPORT_GOOD) {
  431. return -EIO;
  432. }
  433. return 0;
  434. }
  435. static int do_config_autodelink(struct us_data *us, int enable, int force)
  436. {
  437. int retval;
  438. u8 value;
  439. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  440. if (retval < 0)
  441. return -EIO;
  442. if (enable) {
  443. if (force)
  444. value |= 0x03;
  445. else
  446. value |= 0x01;
  447. } else {
  448. value &= ~0x03;
  449. }
  450. US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
  451. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  452. retval = __do_config_autodelink(us, &value, 1);
  453. if (retval < 0)
  454. return -EIO;
  455. return 0;
  456. }
  457. static int config_autodelink_after_power_on(struct us_data *us)
  458. {
  459. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  460. int retval;
  461. u8 value;
  462. US_DEBUGP("%s: <---\n", __func__);
  463. if (!CHK_AUTO_DELINK(chip))
  464. return 0;
  465. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  466. if (retval < 0)
  467. return -EIO;
  468. if (auto_delink_en) {
  469. CLR_BIT(value, 0);
  470. CLR_BIT(value, 1);
  471. SET_BIT(value, 2);
  472. if (CHECK_ID(chip, 0x0138, 0x3882))
  473. CLR_BIT(value, 2);
  474. SET_BIT(value, 7);
  475. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  476. retval = __do_config_autodelink(us, &value, 1);
  477. if (retval < 0)
  478. return -EIO;
  479. retval = enable_oscillator(us);
  480. if (retval == 0)
  481. (void)do_config_autodelink(us, 1, 0);
  482. } else {
  483. /* Autodelink controlled by firmware */
  484. SET_BIT(value, 2);
  485. if (CHECK_ID(chip, 0x0138, 0x3882))
  486. CLR_BIT(value, 2);
  487. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  488. CHECK_ID(chip, 0x0138, 0x3880)) {
  489. CLR_BIT(value, 0);
  490. CLR_BIT(value, 7);
  491. }
  492. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  493. retval = __do_config_autodelink(us, &value, 1);
  494. if (retval < 0)
  495. return -EIO;
  496. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  497. value = 0xFF;
  498. retval = rts51x_write_mem(us, 0xFE79, &value, 1);
  499. if (retval < 0)
  500. return -EIO;
  501. value = 0x01;
  502. retval = rts51x_write_mem(us, 0x48, &value, 1);
  503. if (retval < 0)
  504. return -EIO;
  505. }
  506. }
  507. US_DEBUGP("%s: --->\n", __func__);
  508. return 0;
  509. }
  510. static int config_autodelink_before_power_down(struct us_data *us)
  511. {
  512. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  513. int retval;
  514. u8 value;
  515. US_DEBUGP("%s: <---\n", __func__);
  516. if (!CHK_AUTO_DELINK(chip))
  517. return 0;
  518. if (auto_delink_en) {
  519. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  520. if (retval < 0)
  521. return -EIO;
  522. SET_BIT(value, 2);
  523. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  524. if (retval < 0)
  525. return -EIO;
  526. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  527. value = 0x01;
  528. retval = rts51x_write_mem(us, 0x48, &value, 1);
  529. if (retval < 0)
  530. return -EIO;
  531. }
  532. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  533. if (retval < 0)
  534. return -EIO;
  535. SET_BIT(value, 0);
  536. if (CHECK_ID(chip, 0x0138, 0x3882))
  537. SET_BIT(value, 2);
  538. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  539. if (retval < 0)
  540. return -EIO;
  541. } else {
  542. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  543. CHECK_ID(chip, 0x0138, 0x3880) ||
  544. CHECK_ID(chip, 0x0138, 0x3882)) {
  545. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  546. if (retval < 0)
  547. return -EIO;
  548. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  549. CHECK_ID(chip, 0x0138, 0x3880)) {
  550. SET_BIT(value, 0);
  551. SET_BIT(value, 7);
  552. }
  553. if (CHECK_ID(chip, 0x0138, 0x3882))
  554. SET_BIT(value, 2);
  555. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  556. retval = __do_config_autodelink(us, &value, 1);
  557. if (retval < 0)
  558. return -EIO;
  559. }
  560. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  561. value = 0x01;
  562. retval = rts51x_write_mem(us, 0x48, &value, 1);
  563. if (retval < 0)
  564. return -EIO;
  565. }
  566. }
  567. US_DEBUGP("%s: --->\n", __func__);
  568. return 0;
  569. }
  570. static void fw5895_init(struct us_data *us)
  571. {
  572. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  573. int retval;
  574. u8 val;
  575. US_DEBUGP("%s: <---\n", __func__);
  576. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  577. US_DEBUGP("Not the specified device, return immediately!\n");
  578. } else {
  579. retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
  580. if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
  581. val = 0x1F;
  582. retval = rts51x_write_mem(us, 0xFD70, &val, 1);
  583. if (retval != STATUS_SUCCESS)
  584. US_DEBUGP("Write memory fail\n");
  585. } else {
  586. US_DEBUGP("Read memory fail, OR (val & 0x1F) != 0\n");
  587. }
  588. }
  589. US_DEBUGP("%s: --->\n", __func__);
  590. }
  591. #ifdef CONFIG_REALTEK_AUTOPM
  592. static void fw5895_set_mmc_wp(struct us_data *us)
  593. {
  594. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  595. int retval;
  596. u8 buf[13];
  597. US_DEBUGP("%s: <---\n", __func__);
  598. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  599. US_DEBUGP("Not the specified device, return immediately!\n");
  600. } else {
  601. retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
  602. if (retval == STATUS_SUCCESS && (buf[0] & 0x24) == 0x24) {
  603. /* SD Exist and SD WP */
  604. retval = rts51x_read_mem(us, 0xD04E, buf, 1);
  605. if (retval == STATUS_SUCCESS) {
  606. buf[0] |= 0x04;
  607. retval = rts51x_write_mem(us, 0xFD70, buf, 1);
  608. if (retval != STATUS_SUCCESS)
  609. US_DEBUGP("Write memory fail\n");
  610. } else {
  611. US_DEBUGP("Read memory fail\n");
  612. }
  613. } else {
  614. US_DEBUGP("Read memory fail, OR (buf[0]&0x24)!=0x24\n");
  615. }
  616. }
  617. US_DEBUGP("%s: --->\n", __func__);
  618. }
  619. static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
  620. {
  621. US_DEBUGP("%s: <---, state:%d\n", __func__, rts51x_get_stat(chip));
  622. chip->timer_expires = jiffies + msecs_to_jiffies(1000*ss_delay);
  623. mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
  624. US_DEBUGP("%s: --->\n", __func__);
  625. }
  626. static void rts51x_suspend_timer_fn(unsigned long data)
  627. {
  628. struct rts51x_chip *chip = (struct rts51x_chip *)data;
  629. struct us_data *us = chip->us;
  630. US_DEBUGP("%s: <---\n", __func__);
  631. switch (rts51x_get_stat(chip)) {
  632. case RTS51X_STAT_INIT:
  633. case RTS51X_STAT_RUN:
  634. rts51x_modi_suspend_timer(chip);
  635. break;
  636. case RTS51X_STAT_IDLE:
  637. case RTS51X_STAT_SS:
  638. US_DEBUGP("%s: RTS51X_STAT_SS, intf->pm_usage_cnt:%d,"
  639. "power.usage:%d\n", __func__,
  640. atomic_read(&us->pusb_intf->pm_usage_cnt),
  641. atomic_read(&us->pusb_intf->dev.power.usage_count));
  642. if (atomic_read(&us->pusb_intf->pm_usage_cnt) > 0) {
  643. US_DEBUGP("%s: Ready to enter SS state.\n",
  644. __func__);
  645. rts51x_set_stat(chip, RTS51X_STAT_SS);
  646. /* ignore mass storage interface's children */
  647. pm_suspend_ignore_children(&us->pusb_intf->dev, true);
  648. usb_autopm_put_interface(us->pusb_intf);
  649. US_DEBUGP("%s: RTS51X_STAT_SS 01,"
  650. "intf->pm_usage_cnt:%d, power.usage:%d\n",
  651. __func__,
  652. atomic_read(&us->pusb_intf->pm_usage_cnt),
  653. atomic_read(
  654. &us->pusb_intf->dev.power.usage_count));
  655. }
  656. break;
  657. default:
  658. US_DEBUGP("%s: Unknonwn state !!!\n", __func__);
  659. break;
  660. }
  661. US_DEBUGP("%s: --->\n", __func__);
  662. }
  663. static inline int working_scsi(struct scsi_cmnd *srb)
  664. {
  665. if ((srb->cmnd[0] == TEST_UNIT_READY) ||
  666. (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
  667. return 0;
  668. }
  669. return 1;
  670. }
  671. void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  672. {
  673. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  674. static int card_first_show = 1;
  675. static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
  676. 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
  677. };
  678. static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
  679. 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
  680. };
  681. int ret;
  682. US_DEBUGP("%s: <---\n", __func__);
  683. if (working_scsi(srb)) {
  684. US_DEBUGP("%s: working scsi, intf->pm_usage_cnt:%d,"
  685. "power.usage:%d\n", __func__,
  686. atomic_read(&us->pusb_intf->pm_usage_cnt),
  687. atomic_read(&us->pusb_intf->dev.power.usage_count));
  688. if (atomic_read(&us->pusb_intf->pm_usage_cnt) <= 0) {
  689. ret = usb_autopm_get_interface(us->pusb_intf);
  690. US_DEBUGP("%s: working scsi, ret=%d\n", __func__, ret);
  691. }
  692. if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
  693. rts51x_set_stat(chip, RTS51X_STAT_RUN);
  694. chip->proto_handler_backup(srb, us);
  695. } else {
  696. if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
  697. US_DEBUGP("%s: NOT working scsi\n", __func__);
  698. if ((srb->cmnd[0] == TEST_UNIT_READY) &&
  699. (chip->pwr_state == US_SUSPEND)) {
  700. if (TST_LUN_READY(chip, srb->device->lun)) {
  701. srb->result = SAM_STAT_GOOD;
  702. } else {
  703. srb->result = SAM_STAT_CHECK_CONDITION;
  704. memcpy(srb->sense_buffer,
  705. media_not_present,
  706. US_SENSE_SIZE);
  707. }
  708. US_DEBUGP("%s: TEST_UNIT_READY--->\n",
  709. __func__);
  710. goto out;
  711. }
  712. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  713. int prevent = srb->cmnd[4] & 0x1;
  714. if (prevent) {
  715. srb->result = SAM_STAT_CHECK_CONDITION;
  716. memcpy(srb->sense_buffer,
  717. invalid_cmd_field,
  718. US_SENSE_SIZE);
  719. } else {
  720. srb->result = SAM_STAT_GOOD;
  721. }
  722. US_DEBUGP("%s: ALLOW_MEDIUM_REMOVAL--->\n",
  723. __func__);
  724. goto out;
  725. }
  726. } else {
  727. US_DEBUGP("%s: NOT working scsi, not SS\n", __func__);
  728. chip->proto_handler_backup(srb, us);
  729. /* Check wether card is plugged in */
  730. if (srb->cmnd[0] == TEST_UNIT_READY) {
  731. if (srb->result == SAM_STAT_GOOD) {
  732. SET_LUN_READY(chip, srb->device->lun);
  733. if (card_first_show) {
  734. card_first_show = 0;
  735. fw5895_set_mmc_wp(us);
  736. }
  737. } else {
  738. CLR_LUN_READY(chip, srb->device->lun);
  739. card_first_show = 1;
  740. }
  741. }
  742. if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
  743. rts51x_set_stat(chip, RTS51X_STAT_IDLE);
  744. }
  745. }
  746. out:
  747. US_DEBUGP("%s: state:%d\n", __func__, rts51x_get_stat(chip));
  748. if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
  749. rts51x_modi_suspend_timer(chip);
  750. US_DEBUGP("%s: --->\n", __func__);
  751. }
  752. static int realtek_cr_autosuspend_setup(struct us_data *us)
  753. {
  754. struct rts51x_chip *chip;
  755. struct rts51x_status *status = NULL;
  756. u8 buf[16];
  757. int retval;
  758. chip = (struct rts51x_chip *)us->extra;
  759. chip->support_auto_delink = 0;
  760. chip->pwr_state = US_RESUME;
  761. chip->lun_ready = 0;
  762. rts51x_set_stat(chip, RTS51X_STAT_INIT);
  763. retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
  764. if (retval != STATUS_SUCCESS) {
  765. US_DEBUGP("Read status fail\n");
  766. return -EIO;
  767. }
  768. status = chip->status;
  769. status->vid = ((u16) buf[0] << 8) | buf[1];
  770. status->pid = ((u16) buf[2] << 8) | buf[3];
  771. status->cur_lun = buf[4];
  772. status->card_type = buf[5];
  773. status->total_lun = buf[6];
  774. status->fw_ver = ((u16) buf[7] << 8) | buf[8];
  775. status->phy_exist = buf[9];
  776. status->multi_flag = buf[10];
  777. status->multi_card = buf[11];
  778. status->log_exist = buf[12];
  779. if (chip->status_len == 16) {
  780. status->detailed_type.detailed_type1 = buf[13];
  781. status->function[0] = buf[14];
  782. status->function[1] = buf[15];
  783. }
  784. /* back up the proto_handler in us->extra */
  785. chip = (struct rts51x_chip *)(us->extra);
  786. chip->proto_handler_backup = us->proto_handler;
  787. /* Set the autosuspend_delay to 0 */
  788. pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
  789. /* override us->proto_handler setted in get_protocol() */
  790. us->proto_handler = rts51x_invoke_transport;
  791. chip->timer_expires = 0;
  792. setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
  793. (unsigned long)chip);
  794. fw5895_init(us);
  795. /* enable autosuspend funciton of the usb device */
  796. usb_enable_autosuspend(us->pusb_dev);
  797. return 0;
  798. }
  799. #endif
  800. static void realtek_cr_destructor(void *extra)
  801. {
  802. struct rts51x_chip *chip = (struct rts51x_chip *)extra;
  803. US_DEBUGP("%s: <---\n", __func__);
  804. if (!chip)
  805. return;
  806. #ifdef CONFIG_REALTEK_AUTOPM
  807. if (ss_en) {
  808. del_timer(&chip->rts51x_suspend_timer);
  809. chip->timer_expires = 0;
  810. }
  811. #endif
  812. kfree(chip->status);
  813. }
  814. #ifdef CONFIG_PM
  815. int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
  816. {
  817. struct us_data *us = usb_get_intfdata(iface);
  818. US_DEBUGP("%s: <---\n", __func__);
  819. /* wait until no command is running */
  820. mutex_lock(&us->dev_mutex);
  821. config_autodelink_before_power_down(us);
  822. mutex_unlock(&us->dev_mutex);
  823. US_DEBUGP("%s: --->\n", __func__);
  824. return 0;
  825. }
  826. static int realtek_cr_resume(struct usb_interface *iface)
  827. {
  828. struct us_data *us = usb_get_intfdata(iface);
  829. US_DEBUGP("%s: <---\n", __func__);
  830. fw5895_init(us);
  831. config_autodelink_after_power_on(us);
  832. US_DEBUGP("%s: --->\n", __func__);
  833. return 0;
  834. }
  835. #else
  836. #define realtek_cr_suspend NULL
  837. #define realtek_cr_resume NULL
  838. #endif
  839. static int init_realtek_cr(struct us_data *us)
  840. {
  841. struct rts51x_chip *chip;
  842. int size, i, retval;
  843. chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
  844. if (!chip)
  845. return -ENOMEM;
  846. us->extra = chip;
  847. us->extra_destructor = realtek_cr_destructor;
  848. us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
  849. US_DEBUGP("chip->max_lun = %d\n", chip->max_lun);
  850. size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
  851. chip->status = kzalloc(size, GFP_KERNEL);
  852. if (!chip->status)
  853. goto INIT_FAIL;
  854. for (i = 0; i <= (int)(chip->max_lun); i++) {
  855. retval = rts51x_check_status(us, (u8) i);
  856. if (retval < 0)
  857. goto INIT_FAIL;
  858. }
  859. if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
  860. CHECK_FW_VER(chip, 0x5901))
  861. SET_AUTO_DELINK(chip);
  862. if (STATUS_LEN(chip) == 16) {
  863. if (SUPPORT_AUTO_DELINK(chip))
  864. SET_AUTO_DELINK(chip);
  865. }
  866. #ifdef CONFIG_REALTEK_AUTOPM
  867. if (ss_en) {
  868. chip->us = us;
  869. realtek_cr_autosuspend_setup(us);
  870. }
  871. #endif
  872. US_DEBUGP("chip->flag = 0x%x\n", chip->flag);
  873. (void)config_autodelink_after_power_on(us);
  874. return 0;
  875. INIT_FAIL:
  876. if (us->extra) {
  877. kfree(chip->status);
  878. kfree(us->extra);
  879. us->extra = NULL;
  880. }
  881. return -EIO;
  882. }
  883. static int realtek_cr_probe(struct usb_interface *intf,
  884. const struct usb_device_id *id)
  885. {
  886. struct us_data *us;
  887. int result;
  888. US_DEBUGP("Probe Realtek Card Reader!\n");
  889. result = usb_stor_probe1(&us, intf, id,
  890. (id - realtek_cr_ids) +
  891. realtek_cr_unusual_dev_list);
  892. if (result)
  893. return result;
  894. result = usb_stor_probe2(us);
  895. return result;
  896. }
  897. static struct usb_driver realtek_cr_driver = {
  898. .name = "ums-realtek",
  899. .probe = realtek_cr_probe,
  900. .disconnect = usb_stor_disconnect,
  901. /* .suspend = usb_stor_suspend, */
  902. /* .resume = usb_stor_resume, */
  903. .reset_resume = usb_stor_reset_resume,
  904. .suspend = realtek_cr_suspend,
  905. .resume = realtek_cr_resume,
  906. .pre_reset = usb_stor_pre_reset,
  907. .post_reset = usb_stor_post_reset,
  908. .id_table = realtek_cr_ids,
  909. .soft_unbind = 1,
  910. .supports_autosuspend = 1,
  911. };
  912. static int __init realtek_cr_init(void)
  913. {
  914. return usb_register(&realtek_cr_driver);
  915. }
  916. static void __exit realtek_cr_exit(void)
  917. {
  918. usb_deregister(&realtek_cr_driver);
  919. }
  920. module_init(realtek_cr_init);
  921. module_exit(realtek_cr_exit);