realtek_cr.c 27 KB

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