realtek_cr.c 27 KB

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