realtek_cr.c 27 KB

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