realtek_cr.c 25 KB

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