realtek_cr.c 27 KB

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