realtek_cr.c 26 KB

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