realtek_cr.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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) ? 1 << 7 : 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) ? 1 << 7 : 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 = kmalloc(len, GFP_NOIO);
  333. if (buf == NULL)
  334. return USB_STOR_TRANSPORT_ERROR;
  335. memcpy(buf, data, len);
  336. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
  337. cmnd[0] = 0xF0;
  338. cmnd[1] = 0x0E;
  339. cmnd[2] = (u8) (addr >> 8);
  340. cmnd[3] = (u8) addr;
  341. cmnd[4] = (u8) (len >> 8);
  342. cmnd[5] = (u8) len;
  343. retval = rts51x_bulk_transport(us, 0, cmnd, 12,
  344. buf, len, DMA_TO_DEVICE, NULL);
  345. kfree(buf);
  346. if (retval != USB_STOR_TRANSPORT_GOOD)
  347. return -EIO;
  348. return 0;
  349. }
  350. static int rts51x_read_status(struct us_data *us,
  351. u8 lun, u8 *status, int len, int *actlen)
  352. {
  353. int retval;
  354. u8 cmnd[12] = { 0 };
  355. u8 *buf;
  356. buf = kmalloc(len, GFP_NOIO);
  357. if (buf == NULL)
  358. return USB_STOR_TRANSPORT_ERROR;
  359. US_DEBUGP("%s, lun = %d\n", __func__, lun);
  360. cmnd[0] = 0xF0;
  361. cmnd[1] = 0x09;
  362. retval = rts51x_bulk_transport(us, lun, cmnd, 12,
  363. buf, len, DMA_FROM_DEVICE, actlen);
  364. if (retval != USB_STOR_TRANSPORT_GOOD) {
  365. kfree(buf);
  366. return -EIO;
  367. }
  368. memcpy(status, buf, len);
  369. kfree(buf);
  370. return 0;
  371. }
  372. static int rts51x_check_status(struct us_data *us, u8 lun)
  373. {
  374. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  375. int retval;
  376. u8 buf[16];
  377. retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
  378. if (retval < 0)
  379. return -EIO;
  380. US_DEBUGP("chip->status_len = %d\n", chip->status_len);
  381. chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
  382. chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
  383. chip->status[lun].cur_lun = buf[4];
  384. chip->status[lun].card_type = buf[5];
  385. chip->status[lun].total_lun = buf[6];
  386. chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
  387. chip->status[lun].phy_exist = buf[9];
  388. chip->status[lun].multi_flag = buf[10];
  389. chip->status[lun].multi_card = buf[11];
  390. chip->status[lun].log_exist = buf[12];
  391. if (chip->status_len == 16) {
  392. chip->status[lun].detailed_type.detailed_type1 = buf[13];
  393. chip->status[lun].function[0] = buf[14];
  394. chip->status[lun].function[1] = buf[15];
  395. }
  396. return 0;
  397. }
  398. static int enable_oscillator(struct us_data *us)
  399. {
  400. int retval;
  401. u8 value;
  402. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  403. if (retval < 0)
  404. return -EIO;
  405. value |= 0x04;
  406. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  407. if (retval < 0)
  408. return -EIO;
  409. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  410. if (retval < 0)
  411. return -EIO;
  412. if (!(value & 0x04))
  413. return -EIO;
  414. return 0;
  415. }
  416. static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
  417. {
  418. int retval;
  419. u8 cmnd[12] = {0};
  420. US_DEBUGP("%s, addr = 0xfe47, len = %d\n", __FUNCTION__, len);
  421. cmnd[0] = 0xF0;
  422. cmnd[1] = 0x0E;
  423. cmnd[2] = 0xfe;
  424. cmnd[3] = 0x47;
  425. cmnd[4] = (u8)(len >> 8);
  426. cmnd[5] = (u8)len;
  427. retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, data, len, DMA_TO_DEVICE, NULL);
  428. if (retval != USB_STOR_TRANSPORT_GOOD) {
  429. return -EIO;
  430. }
  431. return 0;
  432. }
  433. static int do_config_autodelink(struct us_data *us, int enable, int force)
  434. {
  435. int retval;
  436. u8 value;
  437. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  438. if (retval < 0)
  439. return -EIO;
  440. if (enable) {
  441. if (force)
  442. value |= 0x03;
  443. else
  444. value |= 0x01;
  445. } else {
  446. value &= ~0x03;
  447. }
  448. US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
  449. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  450. retval = __do_config_autodelink(us, &value, 1);
  451. if (retval < 0)
  452. return -EIO;
  453. return 0;
  454. }
  455. static int config_autodelink_after_power_on(struct us_data *us)
  456. {
  457. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  458. int retval;
  459. u8 value;
  460. US_DEBUGP("%s: <---\n", __func__);
  461. if (!CHK_AUTO_DELINK(chip))
  462. return 0;
  463. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  464. if (retval < 0)
  465. return -EIO;
  466. if (auto_delink_en) {
  467. CLR_BIT(value, 0);
  468. CLR_BIT(value, 1);
  469. SET_BIT(value, 2);
  470. if (CHECK_ID(chip, 0x0138, 0x3882))
  471. CLR_BIT(value, 2);
  472. SET_BIT(value, 7);
  473. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  474. retval = __do_config_autodelink(us, &value, 1);
  475. if (retval < 0)
  476. return -EIO;
  477. retval = enable_oscillator(us);
  478. if (retval == 0)
  479. (void)do_config_autodelink(us, 1, 0);
  480. } else {
  481. /* Autodelink controlled by firmware */
  482. SET_BIT(value, 2);
  483. if (CHECK_ID(chip, 0x0138, 0x3882))
  484. CLR_BIT(value, 2);
  485. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  486. CHECK_ID(chip, 0x0138, 0x3880)) {
  487. CLR_BIT(value, 0);
  488. CLR_BIT(value, 7);
  489. }
  490. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  491. retval = __do_config_autodelink(us, &value, 1);
  492. if (retval < 0)
  493. return -EIO;
  494. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  495. value = 0xFF;
  496. retval = rts51x_write_mem(us, 0xFE79, &value, 1);
  497. if (retval < 0)
  498. return -EIO;
  499. value = 0x01;
  500. retval = rts51x_write_mem(us, 0x48, &value, 1);
  501. if (retval < 0)
  502. return -EIO;
  503. }
  504. }
  505. US_DEBUGP("%s: --->\n", __func__);
  506. return 0;
  507. }
  508. static int config_autodelink_before_power_down(struct us_data *us)
  509. {
  510. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  511. int retval;
  512. u8 value;
  513. US_DEBUGP("%s: <---\n", __func__);
  514. if (!CHK_AUTO_DELINK(chip))
  515. return 0;
  516. if (auto_delink_en) {
  517. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  518. if (retval < 0)
  519. return -EIO;
  520. SET_BIT(value, 2);
  521. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  522. if (retval < 0)
  523. return -EIO;
  524. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  525. value = 0x01;
  526. retval = rts51x_write_mem(us, 0x48, &value, 1);
  527. if (retval < 0)
  528. return -EIO;
  529. }
  530. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  531. if (retval < 0)
  532. return -EIO;
  533. SET_BIT(value, 0);
  534. if (CHECK_ID(chip, 0x0138, 0x3882))
  535. SET_BIT(value, 2);
  536. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  537. if (retval < 0)
  538. return -EIO;
  539. } else {
  540. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  541. CHECK_ID(chip, 0x0138, 0x3880) ||
  542. CHECK_ID(chip, 0x0138, 0x3882)) {
  543. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  544. if (retval < 0)
  545. return -EIO;
  546. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  547. CHECK_ID(chip, 0x0138, 0x3880)) {
  548. SET_BIT(value, 0);
  549. SET_BIT(value, 7);
  550. }
  551. if (CHECK_ID(chip, 0x0138, 0x3882))
  552. SET_BIT(value, 2);
  553. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  554. retval = __do_config_autodelink(us, &value, 1);
  555. if (retval < 0)
  556. return -EIO;
  557. }
  558. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  559. value = 0x01;
  560. retval = rts51x_write_mem(us, 0x48, &value, 1);
  561. if (retval < 0)
  562. return -EIO;
  563. }
  564. }
  565. US_DEBUGP("%s: --->\n", __func__);
  566. return 0;
  567. }
  568. static void fw5895_init(struct us_data *us)
  569. {
  570. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  571. int retval;
  572. u8 val;
  573. US_DEBUGP("%s: <---\n", __func__);
  574. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  575. US_DEBUGP("Not the specified device, return immediately!\n");
  576. } else {
  577. retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
  578. if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
  579. val = 0x1F;
  580. retval = rts51x_write_mem(us, 0xFD70, &val, 1);
  581. if (retval != STATUS_SUCCESS)
  582. US_DEBUGP("Write memory fail\n");
  583. } else {
  584. US_DEBUGP("Read memory fail, OR (val & 0x1F) != 0\n");
  585. }
  586. }
  587. US_DEBUGP("%s: --->\n", __func__);
  588. }
  589. #ifdef CONFIG_REALTEK_AUTOPM
  590. static void fw5895_set_mmc_wp(struct us_data *us)
  591. {
  592. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  593. int retval;
  594. u8 buf[13];
  595. US_DEBUGP("%s: <---\n", __func__);
  596. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  597. US_DEBUGP("Not the specified device, return immediately!\n");
  598. } else {
  599. retval = rts51x_read_mem(us, 0xFD6F, buf, 1);
  600. if (retval == STATUS_SUCCESS && (buf[0] & 0x24) == 0x24) {
  601. /* SD Exist and SD WP */
  602. retval = rts51x_read_mem(us, 0xD04E, buf, 1);
  603. if (retval == STATUS_SUCCESS) {
  604. buf[0] |= 0x04;
  605. retval = rts51x_write_mem(us, 0xFD70, buf, 1);
  606. if (retval != STATUS_SUCCESS)
  607. US_DEBUGP("Write memory fail\n");
  608. } else {
  609. US_DEBUGP("Read memory fail\n");
  610. }
  611. } else {
  612. US_DEBUGP("Read memory fail, OR (buf[0]&0x24)!=0x24\n");
  613. }
  614. }
  615. US_DEBUGP("%s: --->\n", __func__);
  616. }
  617. static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
  618. {
  619. US_DEBUGP("%s: <---, state:%d\n", __func__, rts51x_get_stat(chip));
  620. chip->timer_expires = jiffies + msecs_to_jiffies(1000*ss_delay);
  621. mod_timer(&chip->rts51x_suspend_timer, chip->timer_expires);
  622. US_DEBUGP("%s: --->\n", __func__);
  623. }
  624. static void rts51x_suspend_timer_fn(unsigned long data)
  625. {
  626. struct rts51x_chip *chip = (struct rts51x_chip *)data;
  627. struct us_data *us = chip->us;
  628. US_DEBUGP("%s: <---\n", __func__);
  629. switch (rts51x_get_stat(chip)) {
  630. case RTS51X_STAT_INIT:
  631. case RTS51X_STAT_RUN:
  632. rts51x_modi_suspend_timer(chip);
  633. break;
  634. case RTS51X_STAT_IDLE:
  635. case RTS51X_STAT_SS:
  636. US_DEBUGP("%s: RTS51X_STAT_SS, intf->pm_usage_cnt:%d,"
  637. "power.usage:%d\n", __func__,
  638. atomic_read(&us->pusb_intf->pm_usage_cnt),
  639. atomic_read(&us->pusb_intf->dev.power.usage_count));
  640. if (atomic_read(&us->pusb_intf->pm_usage_cnt) > 0) {
  641. US_DEBUGP("%s: Ready to enter SS state.\n",
  642. __func__);
  643. rts51x_set_stat(chip, RTS51X_STAT_SS);
  644. /* ignore mass storage interface's children */
  645. pm_suspend_ignore_children(&us->pusb_intf->dev, true);
  646. usb_autopm_put_interface(us->pusb_intf);
  647. US_DEBUGP("%s: RTS51X_STAT_SS 01,"
  648. "intf->pm_usage_cnt:%d, power.usage:%d\n",
  649. __func__,
  650. atomic_read(&us->pusb_intf->pm_usage_cnt),
  651. atomic_read(
  652. &us->pusb_intf->dev.power.usage_count));
  653. }
  654. break;
  655. default:
  656. US_DEBUGP("%s: Unknonwn state !!!\n", __func__);
  657. break;
  658. }
  659. US_DEBUGP("%s: --->\n", __func__);
  660. }
  661. static inline int working_scsi(struct scsi_cmnd *srb)
  662. {
  663. if ((srb->cmnd[0] == TEST_UNIT_READY) ||
  664. (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
  665. return 0;
  666. }
  667. return 1;
  668. }
  669. static void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  670. {
  671. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  672. static int card_first_show = 1;
  673. static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
  674. 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
  675. };
  676. static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
  677. 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
  678. };
  679. int ret;
  680. US_DEBUGP("%s: <---\n", __func__);
  681. if (working_scsi(srb)) {
  682. US_DEBUGP("%s: working scsi, intf->pm_usage_cnt:%d,"
  683. "power.usage:%d\n", __func__,
  684. atomic_read(&us->pusb_intf->pm_usage_cnt),
  685. atomic_read(&us->pusb_intf->dev.power.usage_count));
  686. if (atomic_read(&us->pusb_intf->pm_usage_cnt) <= 0) {
  687. ret = usb_autopm_get_interface(us->pusb_intf);
  688. US_DEBUGP("%s: working scsi, ret=%d\n", __func__, ret);
  689. }
  690. if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
  691. rts51x_set_stat(chip, RTS51X_STAT_RUN);
  692. chip->proto_handler_backup(srb, us);
  693. } else {
  694. if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
  695. US_DEBUGP("%s: NOT working scsi\n", __func__);
  696. if ((srb->cmnd[0] == TEST_UNIT_READY) &&
  697. (chip->pwr_state == US_SUSPEND)) {
  698. if (TST_LUN_READY(chip, srb->device->lun)) {
  699. srb->result = SAM_STAT_GOOD;
  700. } else {
  701. srb->result = SAM_STAT_CHECK_CONDITION;
  702. memcpy(srb->sense_buffer,
  703. media_not_present,
  704. US_SENSE_SIZE);
  705. }
  706. US_DEBUGP("%s: TEST_UNIT_READY--->\n",
  707. __func__);
  708. goto out;
  709. }
  710. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  711. int prevent = srb->cmnd[4] & 0x1;
  712. if (prevent) {
  713. srb->result = SAM_STAT_CHECK_CONDITION;
  714. memcpy(srb->sense_buffer,
  715. invalid_cmd_field,
  716. US_SENSE_SIZE);
  717. } else {
  718. srb->result = SAM_STAT_GOOD;
  719. }
  720. US_DEBUGP("%s: ALLOW_MEDIUM_REMOVAL--->\n",
  721. __func__);
  722. goto out;
  723. }
  724. } else {
  725. US_DEBUGP("%s: NOT working scsi, not SS\n", __func__);
  726. chip->proto_handler_backup(srb, us);
  727. /* Check wether card is plugged in */
  728. if (srb->cmnd[0] == TEST_UNIT_READY) {
  729. if (srb->result == SAM_STAT_GOOD) {
  730. SET_LUN_READY(chip, srb->device->lun);
  731. if (card_first_show) {
  732. card_first_show = 0;
  733. fw5895_set_mmc_wp(us);
  734. }
  735. } else {
  736. CLR_LUN_READY(chip, srb->device->lun);
  737. card_first_show = 1;
  738. }
  739. }
  740. if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
  741. rts51x_set_stat(chip, RTS51X_STAT_IDLE);
  742. }
  743. }
  744. out:
  745. US_DEBUGP("%s: state:%d\n", __func__, rts51x_get_stat(chip));
  746. if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
  747. rts51x_modi_suspend_timer(chip);
  748. US_DEBUGP("%s: --->\n", __func__);
  749. }
  750. static int realtek_cr_autosuspend_setup(struct us_data *us)
  751. {
  752. struct rts51x_chip *chip;
  753. struct rts51x_status *status = NULL;
  754. u8 buf[16];
  755. int retval;
  756. chip = (struct rts51x_chip *)us->extra;
  757. chip->support_auto_delink = 0;
  758. chip->pwr_state = US_RESUME;
  759. chip->lun_ready = 0;
  760. rts51x_set_stat(chip, RTS51X_STAT_INIT);
  761. retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
  762. if (retval != STATUS_SUCCESS) {
  763. US_DEBUGP("Read status fail\n");
  764. return -EIO;
  765. }
  766. status = chip->status;
  767. status->vid = ((u16) buf[0] << 8) | buf[1];
  768. status->pid = ((u16) buf[2] << 8) | buf[3];
  769. status->cur_lun = buf[4];
  770. status->card_type = buf[5];
  771. status->total_lun = buf[6];
  772. status->fw_ver = ((u16) buf[7] << 8) | buf[8];
  773. status->phy_exist = buf[9];
  774. status->multi_flag = buf[10];
  775. status->multi_card = buf[11];
  776. status->log_exist = buf[12];
  777. if (chip->status_len == 16) {
  778. status->detailed_type.detailed_type1 = buf[13];
  779. status->function[0] = buf[14];
  780. status->function[1] = buf[15];
  781. }
  782. /* back up the proto_handler in us->extra */
  783. chip = (struct rts51x_chip *)(us->extra);
  784. chip->proto_handler_backup = us->proto_handler;
  785. /* Set the autosuspend_delay to 0 */
  786. pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
  787. /* override us->proto_handler setted in get_protocol() */
  788. us->proto_handler = rts51x_invoke_transport;
  789. chip->timer_expires = 0;
  790. setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
  791. (unsigned long)chip);
  792. fw5895_init(us);
  793. /* enable autosuspend funciton of the usb device */
  794. usb_enable_autosuspend(us->pusb_dev);
  795. return 0;
  796. }
  797. #endif
  798. static void realtek_cr_destructor(void *extra)
  799. {
  800. struct rts51x_chip *chip = (struct rts51x_chip *)extra;
  801. US_DEBUGP("%s: <---\n", __func__);
  802. if (!chip)
  803. return;
  804. #ifdef CONFIG_REALTEK_AUTOPM
  805. if (ss_en) {
  806. del_timer(&chip->rts51x_suspend_timer);
  807. chip->timer_expires = 0;
  808. }
  809. #endif
  810. kfree(chip->status);
  811. }
  812. #ifdef CONFIG_PM
  813. static int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
  814. {
  815. struct us_data *us = usb_get_intfdata(iface);
  816. US_DEBUGP("%s: <---\n", __func__);
  817. /* wait until no command is running */
  818. mutex_lock(&us->dev_mutex);
  819. config_autodelink_before_power_down(us);
  820. mutex_unlock(&us->dev_mutex);
  821. US_DEBUGP("%s: --->\n", __func__);
  822. return 0;
  823. }
  824. static int realtek_cr_resume(struct usb_interface *iface)
  825. {
  826. struct us_data *us = usb_get_intfdata(iface);
  827. US_DEBUGP("%s: <---\n", __func__);
  828. fw5895_init(us);
  829. config_autodelink_after_power_on(us);
  830. US_DEBUGP("%s: --->\n", __func__);
  831. return 0;
  832. }
  833. #else
  834. #define realtek_cr_suspend NULL
  835. #define realtek_cr_resume NULL
  836. #endif
  837. static int init_realtek_cr(struct us_data *us)
  838. {
  839. struct rts51x_chip *chip;
  840. int size, i, retval;
  841. chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
  842. if (!chip)
  843. return -ENOMEM;
  844. us->extra = chip;
  845. us->extra_destructor = realtek_cr_destructor;
  846. us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
  847. US_DEBUGP("chip->max_lun = %d\n", chip->max_lun);
  848. size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
  849. chip->status = kzalloc(size, GFP_KERNEL);
  850. if (!chip->status)
  851. goto INIT_FAIL;
  852. for (i = 0; i <= (int)(chip->max_lun); i++) {
  853. retval = rts51x_check_status(us, (u8) i);
  854. if (retval < 0)
  855. goto INIT_FAIL;
  856. }
  857. if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
  858. CHECK_FW_VER(chip, 0x5901))
  859. SET_AUTO_DELINK(chip);
  860. if (STATUS_LEN(chip) == 16) {
  861. if (SUPPORT_AUTO_DELINK(chip))
  862. SET_AUTO_DELINK(chip);
  863. }
  864. #ifdef CONFIG_REALTEK_AUTOPM
  865. if (ss_en) {
  866. chip->us = us;
  867. realtek_cr_autosuspend_setup(us);
  868. }
  869. #endif
  870. US_DEBUGP("chip->flag = 0x%x\n", chip->flag);
  871. (void)config_autodelink_after_power_on(us);
  872. return 0;
  873. INIT_FAIL:
  874. if (us->extra) {
  875. kfree(chip->status);
  876. kfree(us->extra);
  877. us->extra = NULL;
  878. }
  879. return -EIO;
  880. }
  881. static int realtek_cr_probe(struct usb_interface *intf,
  882. const struct usb_device_id *id)
  883. {
  884. struct us_data *us;
  885. int result;
  886. US_DEBUGP("Probe Realtek Card Reader!\n");
  887. result = usb_stor_probe1(&us, intf, id,
  888. (id - realtek_cr_ids) +
  889. realtek_cr_unusual_dev_list);
  890. if (result)
  891. return result;
  892. result = usb_stor_probe2(us);
  893. return result;
  894. }
  895. static struct usb_driver realtek_cr_driver = {
  896. .name = "ums-realtek",
  897. .probe = realtek_cr_probe,
  898. .disconnect = usb_stor_disconnect,
  899. /* .suspend = usb_stor_suspend, */
  900. /* .resume = usb_stor_resume, */
  901. .reset_resume = usb_stor_reset_resume,
  902. .suspend = realtek_cr_suspend,
  903. .resume = realtek_cr_resume,
  904. .pre_reset = usb_stor_pre_reset,
  905. .post_reset = usb_stor_post_reset,
  906. .id_table = realtek_cr_ids,
  907. .soft_unbind = 1,
  908. .supports_autosuspend = 1,
  909. };
  910. static int __init realtek_cr_init(void)
  911. {
  912. return usb_register(&realtek_cr_driver);
  913. }
  914. static void __exit realtek_cr_exit(void)
  915. {
  916. usb_deregister(&realtek_cr_driver);
  917. }
  918. module_init(realtek_cr_init);
  919. module_exit(realtek_cr_exit);