realtek_cr.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 = kmalloc(len, GFP_NOIO);
  333. if (buf == NULL)
  334. return USB_STOR_TRANSPORT_ERROR;
  335. memcpy(buf, data, len);
  336. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
  337. cmnd[0] = 0xF0;
  338. cmnd[1] = 0x0E;
  339. cmnd[2] = (u8) (addr >> 8);
  340. cmnd[3] = (u8) addr;
  341. cmnd[4] = (u8) (len >> 8);
  342. cmnd[5] = (u8) len;
  343. retval = rts51x_bulk_transport(us, 0, cmnd, 12,
  344. buf, len, DMA_TO_DEVICE, NULL);
  345. kfree(buf);
  346. if (retval != USB_STOR_TRANSPORT_GOOD)
  347. return -EIO;
  348. return 0;
  349. }
  350. static int rts51x_read_status(struct us_data *us,
  351. u8 lun, u8 *status, int len, int *actlen)
  352. {
  353. int retval;
  354. u8 cmnd[12] = { 0 };
  355. u8 *buf;
  356. buf = kmalloc(len, GFP_NOIO);
  357. if (buf == NULL)
  358. return USB_STOR_TRANSPORT_ERROR;
  359. US_DEBUGP("%s, lun = %d\n", __func__, lun);
  360. cmnd[0] = 0xF0;
  361. cmnd[1] = 0x09;
  362. retval = rts51x_bulk_transport(us, lun, cmnd, 12,
  363. buf, len, DMA_FROM_DEVICE, actlen);
  364. if (retval != USB_STOR_TRANSPORT_GOOD) {
  365. kfree(buf);
  366. return -EIO;
  367. }
  368. memcpy(status, buf, len);
  369. kfree(buf);
  370. return 0;
  371. }
  372. static int rts51x_check_status(struct us_data *us, u8 lun)
  373. {
  374. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  375. int retval;
  376. u8 buf[16];
  377. retval = rts51x_read_status(us, lun, buf, 16, &(chip->status_len));
  378. if (retval < 0)
  379. return -EIO;
  380. US_DEBUGP("chip->status_len = %d\n", chip->status_len);
  381. chip->status[lun].vid = ((u16) buf[0] << 8) | buf[1];
  382. chip->status[lun].pid = ((u16) buf[2] << 8) | buf[3];
  383. chip->status[lun].cur_lun = buf[4];
  384. chip->status[lun].card_type = buf[5];
  385. chip->status[lun].total_lun = buf[6];
  386. chip->status[lun].fw_ver = ((u16) buf[7] << 8) | buf[8];
  387. chip->status[lun].phy_exist = buf[9];
  388. chip->status[lun].multi_flag = buf[10];
  389. chip->status[lun].multi_card = buf[11];
  390. chip->status[lun].log_exist = buf[12];
  391. if (chip->status_len == 16) {
  392. chip->status[lun].detailed_type.detailed_type1 = buf[13];
  393. chip->status[lun].function[0] = buf[14];
  394. chip->status[lun].function[1] = buf[15];
  395. }
  396. return 0;
  397. }
  398. static int enable_oscillator(struct us_data *us)
  399. {
  400. int retval;
  401. u8 value;
  402. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  403. if (retval < 0)
  404. return -EIO;
  405. value |= 0x04;
  406. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  407. if (retval < 0)
  408. return -EIO;
  409. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  410. if (retval < 0)
  411. return -EIO;
  412. if (!(value & 0x04))
  413. return -EIO;
  414. return 0;
  415. }
  416. static int __do_config_autodelink(struct us_data *us, u8 *data, u16 len)
  417. {
  418. int retval;
  419. u16 addr = 0xFE47;
  420. u8 cmnd[12] = {0};
  421. US_DEBUGP("%s, addr = 0x%x, len = %d\n", __FUNCTION__, addr, len);
  422. cmnd[0] = 0xF0;
  423. cmnd[1] = 0x0E;
  424. cmnd[2] = (u8)(addr >> 8);
  425. cmnd[3] = (u8)addr;
  426. cmnd[4] = (u8)(len >> 8);
  427. cmnd[5] = (u8)len;
  428. retval = rts51x_bulk_transport_special(us, 0, cmnd, 12, data, len, DMA_TO_DEVICE, NULL);
  429. if (retval != USB_STOR_TRANSPORT_GOOD) {
  430. return -EIO;
  431. }
  432. return 0;
  433. }
  434. static int do_config_autodelink(struct us_data *us, int enable, int force)
  435. {
  436. int retval;
  437. u8 value;
  438. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  439. if (retval < 0)
  440. return -EIO;
  441. if (enable) {
  442. if (force)
  443. value |= 0x03;
  444. else
  445. value |= 0x01;
  446. } else {
  447. value &= ~0x03;
  448. }
  449. US_DEBUGP("In %s,set 0xfe47 to 0x%x\n", __func__, value);
  450. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  451. retval = __do_config_autodelink(us, &value, 1);
  452. if (retval < 0)
  453. return -EIO;
  454. return 0;
  455. }
  456. static int config_autodelink_after_power_on(struct us_data *us)
  457. {
  458. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  459. int retval;
  460. u8 value;
  461. US_DEBUGP("%s: <---\n", __func__);
  462. if (!CHK_AUTO_DELINK(chip))
  463. return 0;
  464. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  465. if (retval < 0)
  466. return -EIO;
  467. if (auto_delink_en) {
  468. CLR_BIT(value, 0);
  469. CLR_BIT(value, 1);
  470. SET_BIT(value, 2);
  471. if (CHECK_ID(chip, 0x0138, 0x3882))
  472. CLR_BIT(value, 2);
  473. SET_BIT(value, 7);
  474. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  475. retval = __do_config_autodelink(us, &value, 1);
  476. if (retval < 0)
  477. return -EIO;
  478. retval = enable_oscillator(us);
  479. if (retval == 0)
  480. (void)do_config_autodelink(us, 1, 0);
  481. } else {
  482. /* Autodelink controlled by firmware */
  483. SET_BIT(value, 2);
  484. if (CHECK_ID(chip, 0x0138, 0x3882))
  485. CLR_BIT(value, 2);
  486. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  487. CHECK_ID(chip, 0x0138, 0x3880)) {
  488. CLR_BIT(value, 0);
  489. CLR_BIT(value, 7);
  490. }
  491. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  492. retval = __do_config_autodelink(us, &value, 1);
  493. if (retval < 0)
  494. return -EIO;
  495. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  496. value = 0xFF;
  497. retval = rts51x_write_mem(us, 0xFE79, &value, 1);
  498. if (retval < 0)
  499. return -EIO;
  500. value = 0x01;
  501. retval = rts51x_write_mem(us, 0x48, &value, 1);
  502. if (retval < 0)
  503. return -EIO;
  504. }
  505. }
  506. US_DEBUGP("%s: --->\n", __func__);
  507. return 0;
  508. }
  509. static int config_autodelink_before_power_down(struct us_data *us)
  510. {
  511. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  512. int retval;
  513. u8 value;
  514. US_DEBUGP("%s: <---\n", __func__);
  515. if (!CHK_AUTO_DELINK(chip))
  516. return 0;
  517. if (auto_delink_en) {
  518. retval = rts51x_read_mem(us, 0xFE77, &value, 1);
  519. if (retval < 0)
  520. return -EIO;
  521. SET_BIT(value, 2);
  522. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  523. if (retval < 0)
  524. return -EIO;
  525. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  526. value = 0x01;
  527. retval = rts51x_write_mem(us, 0x48, &value, 1);
  528. if (retval < 0)
  529. return -EIO;
  530. }
  531. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  532. if (retval < 0)
  533. return -EIO;
  534. SET_BIT(value, 0);
  535. if (CHECK_ID(chip, 0x0138, 0x3882))
  536. SET_BIT(value, 2);
  537. retval = rts51x_write_mem(us, 0xFE77, &value, 1);
  538. if (retval < 0)
  539. return -EIO;
  540. } else {
  541. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  542. CHECK_ID(chip, 0x0138, 0x3880) ||
  543. CHECK_ID(chip, 0x0138, 0x3882)) {
  544. retval = rts51x_read_mem(us, 0xFE47, &value, 1);
  545. if (retval < 0)
  546. return -EIO;
  547. if (CHECK_ID(chip, 0x0159, 0x5889) ||
  548. CHECK_ID(chip, 0x0138, 0x3880)) {
  549. SET_BIT(value, 0);
  550. SET_BIT(value, 7);
  551. }
  552. if (CHECK_ID(chip, 0x0138, 0x3882))
  553. SET_BIT(value, 2);
  554. /* retval = rts51x_write_mem(us, 0xFE47, &value, 1); */
  555. retval = __do_config_autodelink(us, &value, 1);
  556. if (retval < 0)
  557. return -EIO;
  558. }
  559. if (CHECK_ID(chip, 0x0159, 0x5888)) {
  560. value = 0x01;
  561. retval = rts51x_write_mem(us, 0x48, &value, 1);
  562. if (retval < 0)
  563. return -EIO;
  564. }
  565. }
  566. US_DEBUGP("%s: --->\n", __func__);
  567. return 0;
  568. }
  569. static void fw5895_init(struct us_data *us)
  570. {
  571. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  572. int retval;
  573. u8 val;
  574. US_DEBUGP("%s: <---\n", __func__);
  575. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  576. US_DEBUGP("Not the specified device, return immediately!\n");
  577. } else {
  578. retval = rts51x_read_mem(us, 0xFD6F, &val, 1);
  579. if (retval == STATUS_SUCCESS && (val & 0x1F) == 0) {
  580. val = 0x1F;
  581. retval = rts51x_write_mem(us, 0xFD70, &val, 1);
  582. if (retval != STATUS_SUCCESS)
  583. US_DEBUGP("Write memory fail\n");
  584. } else {
  585. US_DEBUGP("Read memory fail, OR (val & 0x1F) != 0\n");
  586. }
  587. }
  588. US_DEBUGP("%s: --->\n", __func__);
  589. }
  590. #ifdef CONFIG_REALTEK_AUTOPM
  591. static void fw5895_set_mmc_wp(struct us_data *us)
  592. {
  593. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  594. int retval;
  595. u8 buf[13];
  596. US_DEBUGP("%s: <---\n", __func__);
  597. if ((PRODUCT_ID(chip) != 0x0158) || (FW_VERSION(chip) != 0x5895)) {
  598. US_DEBUGP("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. US_DEBUGP("Write memory fail\n");
  609. } else {
  610. US_DEBUGP("Read memory fail\n");
  611. }
  612. } else {
  613. US_DEBUGP("Read memory fail, OR (buf[0]&0x24)!=0x24\n");
  614. }
  615. }
  616. US_DEBUGP("%s: --->\n", __func__);
  617. }
  618. static void rts51x_modi_suspend_timer(struct rts51x_chip *chip)
  619. {
  620. US_DEBUGP("%s: <---, state:%d\n", __func__, 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. US_DEBUGP("%s: --->\n", __func__);
  624. }
  625. static void rts51x_suspend_timer_fn(unsigned long data)
  626. {
  627. struct rts51x_chip *chip = (struct rts51x_chip *)data;
  628. struct us_data *us = chip->us;
  629. US_DEBUGP("%s: <---\n", __func__);
  630. switch (rts51x_get_stat(chip)) {
  631. case RTS51X_STAT_INIT:
  632. case RTS51X_STAT_RUN:
  633. rts51x_modi_suspend_timer(chip);
  634. break;
  635. case RTS51X_STAT_IDLE:
  636. case RTS51X_STAT_SS:
  637. US_DEBUGP("%s: RTS51X_STAT_SS, intf->pm_usage_cnt:%d,"
  638. "power.usage:%d\n", __func__,
  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. US_DEBUGP("%s: Ready to enter SS state.\n",
  643. __func__);
  644. rts51x_set_stat(chip, RTS51X_STAT_SS);
  645. /* ignore mass storage interface's children */
  646. pm_suspend_ignore_children(&us->pusb_intf->dev, true);
  647. usb_autopm_put_interface(us->pusb_intf);
  648. US_DEBUGP("%s: RTS51X_STAT_SS 01,"
  649. "intf->pm_usage_cnt:%d, power.usage:%d\n",
  650. __func__,
  651. atomic_read(&us->pusb_intf->pm_usage_cnt),
  652. atomic_read(
  653. &us->pusb_intf->dev.power.usage_count));
  654. }
  655. break;
  656. default:
  657. US_DEBUGP("%s: Unknonwn state !!!\n", __func__);
  658. break;
  659. }
  660. US_DEBUGP("%s: --->\n", __func__);
  661. }
  662. static inline int working_scsi(struct scsi_cmnd *srb)
  663. {
  664. if ((srb->cmnd[0] == TEST_UNIT_READY) ||
  665. (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)) {
  666. return 0;
  667. }
  668. return 1;
  669. }
  670. void rts51x_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  671. {
  672. struct rts51x_chip *chip = (struct rts51x_chip *)(us->extra);
  673. static int card_first_show = 1;
  674. static u8 media_not_present[] = { 0x70, 0, 0x02, 0, 0, 0, 0,
  675. 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0
  676. };
  677. static u8 invalid_cmd_field[] = { 0x70, 0, 0x05, 0, 0, 0, 0,
  678. 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0
  679. };
  680. int ret;
  681. US_DEBUGP("%s: <---\n", __func__);
  682. if (working_scsi(srb)) {
  683. US_DEBUGP("%s: working scsi, intf->pm_usage_cnt:%d,"
  684. "power.usage:%d\n", __func__,
  685. atomic_read(&us->pusb_intf->pm_usage_cnt),
  686. atomic_read(&us->pusb_intf->dev.power.usage_count));
  687. if (atomic_read(&us->pusb_intf->pm_usage_cnt) <= 0) {
  688. ret = usb_autopm_get_interface(us->pusb_intf);
  689. US_DEBUGP("%s: working scsi, ret=%d\n", __func__, ret);
  690. }
  691. if (rts51x_get_stat(chip) != RTS51X_STAT_RUN)
  692. rts51x_set_stat(chip, RTS51X_STAT_RUN);
  693. chip->proto_handler_backup(srb, us);
  694. } else {
  695. if (rts51x_get_stat(chip) == RTS51X_STAT_SS) {
  696. US_DEBUGP("%s: NOT working scsi\n", __func__);
  697. if ((srb->cmnd[0] == TEST_UNIT_READY) &&
  698. (chip->pwr_state == US_SUSPEND)) {
  699. if (TST_LUN_READY(chip, srb->device->lun)) {
  700. srb->result = SAM_STAT_GOOD;
  701. } else {
  702. srb->result = SAM_STAT_CHECK_CONDITION;
  703. memcpy(srb->sense_buffer,
  704. media_not_present,
  705. US_SENSE_SIZE);
  706. }
  707. US_DEBUGP("%s: TEST_UNIT_READY--->\n",
  708. __func__);
  709. goto out;
  710. }
  711. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  712. int prevent = srb->cmnd[4] & 0x1;
  713. if (prevent) {
  714. srb->result = SAM_STAT_CHECK_CONDITION;
  715. memcpy(srb->sense_buffer,
  716. invalid_cmd_field,
  717. US_SENSE_SIZE);
  718. } else {
  719. srb->result = SAM_STAT_GOOD;
  720. }
  721. US_DEBUGP("%s: ALLOW_MEDIUM_REMOVAL--->\n",
  722. __func__);
  723. goto out;
  724. }
  725. } else {
  726. US_DEBUGP("%s: NOT working scsi, not SS\n", __func__);
  727. chip->proto_handler_backup(srb, us);
  728. /* Check wether card is plugged in */
  729. if (srb->cmnd[0] == TEST_UNIT_READY) {
  730. if (srb->result == SAM_STAT_GOOD) {
  731. SET_LUN_READY(chip, srb->device->lun);
  732. if (card_first_show) {
  733. card_first_show = 0;
  734. fw5895_set_mmc_wp(us);
  735. }
  736. } else {
  737. CLR_LUN_READY(chip, srb->device->lun);
  738. card_first_show = 1;
  739. }
  740. }
  741. if (rts51x_get_stat(chip) != RTS51X_STAT_IDLE)
  742. rts51x_set_stat(chip, RTS51X_STAT_IDLE);
  743. }
  744. }
  745. out:
  746. US_DEBUGP("%s: state:%d\n", __func__, rts51x_get_stat(chip));
  747. if (rts51x_get_stat(chip) == RTS51X_STAT_RUN)
  748. rts51x_modi_suspend_timer(chip);
  749. US_DEBUGP("%s: --->\n", __func__);
  750. }
  751. static int realtek_cr_autosuspend_setup(struct us_data *us)
  752. {
  753. struct rts51x_chip *chip;
  754. struct rts51x_status *status = NULL;
  755. u8 buf[16];
  756. int retval;
  757. chip = (struct rts51x_chip *)us->extra;
  758. chip->support_auto_delink = 0;
  759. chip->pwr_state = US_RESUME;
  760. chip->lun_ready = 0;
  761. rts51x_set_stat(chip, RTS51X_STAT_INIT);
  762. retval = rts51x_read_status(us, 0, buf, 16, &(chip->status_len));
  763. if (retval != STATUS_SUCCESS) {
  764. US_DEBUGP("Read status fail\n");
  765. return -EIO;
  766. }
  767. status = chip->status;
  768. status->vid = ((u16) buf[0] << 8) | buf[1];
  769. status->pid = ((u16) buf[2] << 8) | buf[3];
  770. status->cur_lun = buf[4];
  771. status->card_type = buf[5];
  772. status->total_lun = buf[6];
  773. status->fw_ver = ((u16) buf[7] << 8) | buf[8];
  774. status->phy_exist = buf[9];
  775. status->multi_flag = buf[10];
  776. status->multi_card = buf[11];
  777. status->log_exist = buf[12];
  778. if (chip->status_len == 16) {
  779. status->detailed_type.detailed_type1 = buf[13];
  780. status->function[0] = buf[14];
  781. status->function[1] = buf[15];
  782. }
  783. /* back up the proto_handler in us->extra */
  784. chip = (struct rts51x_chip *)(us->extra);
  785. chip->proto_handler_backup = us->proto_handler;
  786. /* Set the autosuspend_delay to 0 */
  787. pm_runtime_set_autosuspend_delay(&us->pusb_dev->dev, 0);
  788. /* override us->proto_handler setted in get_protocol() */
  789. us->proto_handler = rts51x_invoke_transport;
  790. chip->timer_expires = 0;
  791. setup_timer(&chip->rts51x_suspend_timer, rts51x_suspend_timer_fn,
  792. (unsigned long)chip);
  793. fw5895_init(us);
  794. /* enable autosuspend funciton of the usb device */
  795. usb_enable_autosuspend(us->pusb_dev);
  796. return 0;
  797. }
  798. #endif
  799. static void realtek_cr_destructor(void *extra)
  800. {
  801. struct rts51x_chip *chip = (struct rts51x_chip *)extra;
  802. US_DEBUGP("%s: <---\n", __func__);
  803. if (!chip)
  804. return;
  805. #ifdef CONFIG_REALTEK_AUTOPM
  806. if (ss_en) {
  807. del_timer(&chip->rts51x_suspend_timer);
  808. chip->timer_expires = 0;
  809. }
  810. #endif
  811. kfree(chip->status);
  812. }
  813. #ifdef CONFIG_PM
  814. int realtek_cr_suspend(struct usb_interface *iface, pm_message_t message)
  815. {
  816. struct us_data *us = usb_get_intfdata(iface);
  817. US_DEBUGP("%s: <---\n", __func__);
  818. /* wait until no command is running */
  819. mutex_lock(&us->dev_mutex);
  820. config_autodelink_before_power_down(us);
  821. mutex_unlock(&us->dev_mutex);
  822. US_DEBUGP("%s: --->\n", __func__);
  823. return 0;
  824. }
  825. static int realtek_cr_resume(struct usb_interface *iface)
  826. {
  827. struct us_data *us = usb_get_intfdata(iface);
  828. US_DEBUGP("%s: <---\n", __func__);
  829. fw5895_init(us);
  830. config_autodelink_after_power_on(us);
  831. US_DEBUGP("%s: --->\n", __func__);
  832. return 0;
  833. }
  834. #else
  835. #define realtek_cr_suspend NULL
  836. #define realtek_cr_resume NULL
  837. #endif
  838. static int init_realtek_cr(struct us_data *us)
  839. {
  840. struct rts51x_chip *chip;
  841. int size, i, retval;
  842. chip = kzalloc(sizeof(struct rts51x_chip), GFP_KERNEL);
  843. if (!chip)
  844. return -ENOMEM;
  845. us->extra = chip;
  846. us->extra_destructor = realtek_cr_destructor;
  847. us->max_lun = chip->max_lun = rts51x_get_max_lun(us);
  848. US_DEBUGP("chip->max_lun = %d\n", chip->max_lun);
  849. size = (chip->max_lun + 1) * sizeof(struct rts51x_status);
  850. chip->status = kzalloc(size, GFP_KERNEL);
  851. if (!chip->status)
  852. goto INIT_FAIL;
  853. for (i = 0; i <= (int)(chip->max_lun); i++) {
  854. retval = rts51x_check_status(us, (u8) i);
  855. if (retval < 0)
  856. goto INIT_FAIL;
  857. }
  858. if (CHECK_FW_VER(chip, 0x5888) || CHECK_FW_VER(chip, 0x5889) ||
  859. CHECK_FW_VER(chip, 0x5901))
  860. SET_AUTO_DELINK(chip);
  861. if (STATUS_LEN(chip) == 16) {
  862. if (SUPPORT_AUTO_DELINK(chip))
  863. SET_AUTO_DELINK(chip);
  864. }
  865. #ifdef CONFIG_REALTEK_AUTOPM
  866. if (ss_en) {
  867. chip->us = us;
  868. realtek_cr_autosuspend_setup(us);
  869. }
  870. #endif
  871. US_DEBUGP("chip->flag = 0x%x\n", chip->flag);
  872. (void)config_autodelink_after_power_on(us);
  873. return 0;
  874. INIT_FAIL:
  875. if (us->extra) {
  876. kfree(chip->status);
  877. kfree(us->extra);
  878. us->extra = NULL;
  879. }
  880. return -EIO;
  881. }
  882. static int realtek_cr_probe(struct usb_interface *intf,
  883. const struct usb_device_id *id)
  884. {
  885. struct us_data *us;
  886. int result;
  887. US_DEBUGP("Probe Realtek Card Reader!\n");
  888. result = usb_stor_probe1(&us, intf, id,
  889. (id - realtek_cr_ids) +
  890. realtek_cr_unusual_dev_list);
  891. if (result)
  892. return result;
  893. result = usb_stor_probe2(us);
  894. return result;
  895. }
  896. static struct usb_driver realtek_cr_driver = {
  897. .name = "ums-realtek",
  898. .probe = realtek_cr_probe,
  899. .disconnect = usb_stor_disconnect,
  900. /* .suspend = usb_stor_suspend, */
  901. /* .resume = usb_stor_resume, */
  902. .reset_resume = usb_stor_reset_resume,
  903. .suspend = realtek_cr_suspend,
  904. .resume = realtek_cr_resume,
  905. .pre_reset = usb_stor_pre_reset,
  906. .post_reset = usb_stor_post_reset,
  907. .id_table = realtek_cr_ids,
  908. .soft_unbind = 1,
  909. .supports_autosuspend = 1,
  910. };
  911. static int __init realtek_cr_init(void)
  912. {
  913. return usb_register(&realtek_cr_driver);
  914. }
  915. static void __exit realtek_cr_exit(void)
  916. {
  917. usb_deregister(&realtek_cr_driver);
  918. }
  919. module_init(realtek_cr_init);
  920. module_exit(realtek_cr_exit);