realtek_cr.c 26 KB

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