realtek_cr.c 27 KB

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