realtek_cr.c 25 KB

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