realtek_cr.c 27 KB

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