rtsx_pci_ms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Realtek PCI-Express Memstick Card Interface driver
  3. *
  4. * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
  5. *
  6. * Author:
  7. * Wei WANG <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/highmem.h>
  11. #include <linux/delay.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/memstick.h>
  14. #include <linux/rtsx_pci.h>
  15. #include <asm/unaligned.h>
  16. struct realtek_pci_ms {
  17. struct platform_device *pdev;
  18. struct rtsx_pcr *pcr;
  19. struct memstick_host *msh;
  20. struct memstick_request *req;
  21. struct mutex host_mutex;
  22. struct work_struct handle_req;
  23. u8 ssc_depth;
  24. unsigned int clock;
  25. unsigned char ifmode;
  26. bool eject;
  27. };
  28. static inline struct device *ms_dev(struct realtek_pci_ms *host)
  29. {
  30. return &(host->pdev->dev);
  31. }
  32. static inline void ms_clear_error(struct realtek_pci_ms *host)
  33. {
  34. rtsx_pci_write_register(host->pcr, CARD_STOP,
  35. MS_STOP | MS_CLR_ERR, MS_STOP | MS_CLR_ERR);
  36. }
  37. #ifdef DEBUG
  38. static void ms_print_debug_regs(struct realtek_pci_ms *host)
  39. {
  40. struct rtsx_pcr *pcr = host->pcr;
  41. u16 i;
  42. u8 *ptr;
  43. /* Print MS host internal registers */
  44. rtsx_pci_init_cmd(pcr);
  45. for (i = 0xFD40; i <= 0xFD44; i++)
  46. rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
  47. for (i = 0xFD52; i <= 0xFD69; i++)
  48. rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
  49. rtsx_pci_send_cmd(pcr, 100);
  50. ptr = rtsx_pci_get_cmd_data(pcr);
  51. for (i = 0xFD40; i <= 0xFD44; i++)
  52. dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
  53. for (i = 0xFD52; i <= 0xFD69; i++)
  54. dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
  55. }
  56. #else
  57. #define ms_print_debug_regs(host)
  58. #endif
  59. static int ms_power_on(struct realtek_pci_ms *host)
  60. {
  61. struct rtsx_pcr *pcr = host->pcr;
  62. int err;
  63. rtsx_pci_init_cmd(pcr);
  64. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SELECT, 0x07, MS_MOD_SEL);
  65. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SHARE_MODE,
  66. CARD_SHARE_MASK, CARD_SHARE_48_MS);
  67. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN,
  68. MS_CLK_EN, MS_CLK_EN);
  69. err = rtsx_pci_send_cmd(pcr, 100);
  70. if (err < 0)
  71. return err;
  72. err = rtsx_pci_card_pull_ctl_enable(pcr, RTSX_MS_CARD);
  73. if (err < 0)
  74. return err;
  75. err = rtsx_pci_card_power_on(pcr, RTSX_MS_CARD);
  76. if (err < 0)
  77. return err;
  78. /* Wait ms power stable */
  79. msleep(150);
  80. err = rtsx_pci_write_register(pcr, CARD_OE,
  81. MS_OUTPUT_EN, MS_OUTPUT_EN);
  82. if (err < 0)
  83. return err;
  84. return 0;
  85. }
  86. static int ms_power_off(struct realtek_pci_ms *host)
  87. {
  88. struct rtsx_pcr *pcr = host->pcr;
  89. int err;
  90. rtsx_pci_init_cmd(pcr);
  91. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, MS_CLK_EN, 0);
  92. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_OE, MS_OUTPUT_EN, 0);
  93. err = rtsx_pci_send_cmd(pcr, 100);
  94. if (err < 0)
  95. return err;
  96. err = rtsx_pci_card_power_off(pcr, RTSX_MS_CARD);
  97. if (err < 0)
  98. return err;
  99. return rtsx_pci_card_pull_ctl_disable(pcr, RTSX_MS_CARD);
  100. }
  101. static int ms_transfer_data(struct realtek_pci_ms *host, unsigned char data_dir,
  102. u8 tpc, u8 cfg, struct scatterlist *sg)
  103. {
  104. struct rtsx_pcr *pcr = host->pcr;
  105. int err;
  106. unsigned int length = sg->length;
  107. u16 sec_cnt = (u16)(length / 512);
  108. u8 val, trans_mode, dma_dir;
  109. struct memstick_dev *card = host->msh->card;
  110. bool pro_card = card->id.type == MEMSTICK_TYPE_PRO;
  111. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
  112. __func__, tpc, (data_dir == READ) ? "READ" : "WRITE",
  113. length);
  114. if (data_dir == READ) {
  115. dma_dir = DMA_DIR_FROM_CARD;
  116. trans_mode = pro_card ? MS_TM_AUTO_READ : MS_TM_NORMAL_READ;
  117. } else {
  118. dma_dir = DMA_DIR_TO_CARD;
  119. trans_mode = pro_card ? MS_TM_AUTO_WRITE : MS_TM_NORMAL_WRITE;
  120. }
  121. rtsx_pci_init_cmd(pcr);
  122. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  123. if (pro_card) {
  124. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_H,
  125. 0xFF, (u8)(sec_cnt >> 8));
  126. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_L,
  127. 0xFF, (u8)sec_cnt);
  128. }
  129. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  130. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
  131. DMA_DONE_INT, DMA_DONE_INT);
  132. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3, 0xFF, (u8)(length >> 24));
  133. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2, 0xFF, (u8)(length >> 16));
  134. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1, 0xFF, (u8)(length >> 8));
  135. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)length);
  136. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
  137. 0x03 | DMA_PACK_SIZE_MASK, dma_dir | DMA_EN | DMA_512);
  138. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  139. 0x01, RING_BUFFER);
  140. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  141. 0xFF, MS_TRANSFER_START | trans_mode);
  142. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  143. MS_TRANSFER_END, MS_TRANSFER_END);
  144. rtsx_pci_send_cmd_no_wait(pcr);
  145. err = rtsx_pci_transfer_data(pcr, sg, 1, data_dir == READ, 10000);
  146. if (err < 0) {
  147. ms_clear_error(host);
  148. return err;
  149. }
  150. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  151. if (pro_card) {
  152. if (val & (MS_INT_CMDNK | MS_INT_ERR |
  153. MS_CRC16_ERR | MS_RDY_TIMEOUT))
  154. return -EIO;
  155. } else {
  156. if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT))
  157. return -EIO;
  158. }
  159. return 0;
  160. }
  161. static int ms_write_bytes(struct realtek_pci_ms *host, u8 tpc,
  162. u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
  163. {
  164. struct rtsx_pcr *pcr = host->pcr;
  165. int err, i;
  166. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
  167. if (!data)
  168. return -EINVAL;
  169. rtsx_pci_init_cmd(pcr);
  170. for (i = 0; i < cnt; i++)
  171. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  172. PPBUF_BASE2 + i, 0xFF, data[i]);
  173. if (cnt % 2)
  174. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  175. PPBUF_BASE2 + i, 0xFF, 0xFF);
  176. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  177. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
  178. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  179. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  180. 0x01, PINGPONG_BUFFER);
  181. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  182. 0xFF, MS_TRANSFER_START | MS_TM_WRITE_BYTES);
  183. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  184. MS_TRANSFER_END, MS_TRANSFER_END);
  185. if (int_reg)
  186. rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
  187. err = rtsx_pci_send_cmd(pcr, 5000);
  188. if (err < 0) {
  189. u8 val;
  190. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  191. dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
  192. if (int_reg)
  193. *int_reg = val & 0x0F;
  194. ms_print_debug_regs(host);
  195. ms_clear_error(host);
  196. if (!(tpc & 0x08)) {
  197. if (val & MS_CRC16_ERR)
  198. return -EIO;
  199. } else {
  200. if (!(val & 0x80)) {
  201. if (val & (MS_INT_ERR | MS_INT_CMDNK))
  202. return -EIO;
  203. }
  204. }
  205. return -ETIMEDOUT;
  206. }
  207. if (int_reg) {
  208. u8 *ptr = rtsx_pci_get_cmd_data(pcr) + 1;
  209. *int_reg = *ptr & 0x0F;
  210. }
  211. return 0;
  212. }
  213. static int ms_read_bytes(struct realtek_pci_ms *host, u8 tpc,
  214. u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
  215. {
  216. struct rtsx_pcr *pcr = host->pcr;
  217. int err, i;
  218. u8 *ptr;
  219. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
  220. if (!data)
  221. return -EINVAL;
  222. rtsx_pci_init_cmd(pcr);
  223. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  224. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
  225. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  226. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  227. 0x01, PINGPONG_BUFFER);
  228. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  229. 0xFF, MS_TRANSFER_START | MS_TM_READ_BYTES);
  230. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  231. MS_TRANSFER_END, MS_TRANSFER_END);
  232. for (i = 0; i < cnt - 1; i++)
  233. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + i, 0, 0);
  234. if (cnt % 2)
  235. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + cnt, 0, 0);
  236. else
  237. rtsx_pci_add_cmd(pcr, READ_REG_CMD,
  238. PPBUF_BASE2 + cnt - 1, 0, 0);
  239. if (int_reg)
  240. rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
  241. err = rtsx_pci_send_cmd(pcr, 5000);
  242. if (err < 0) {
  243. u8 val;
  244. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  245. dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
  246. if (int_reg)
  247. *int_reg = val & 0x0F;
  248. ms_print_debug_regs(host);
  249. ms_clear_error(host);
  250. if (!(tpc & 0x08)) {
  251. if (val & MS_CRC16_ERR)
  252. return -EIO;
  253. } else {
  254. if (!(val & 0x80)) {
  255. if (val & (MS_INT_ERR | MS_INT_CMDNK))
  256. return -EIO;
  257. }
  258. }
  259. return -ETIMEDOUT;
  260. }
  261. ptr = rtsx_pci_get_cmd_data(pcr) + 1;
  262. for (i = 0; i < cnt; i++)
  263. data[i] = *ptr++;
  264. if (int_reg)
  265. *int_reg = *ptr & 0x0F;
  266. return 0;
  267. }
  268. static int rtsx_pci_ms_issue_cmd(struct realtek_pci_ms *host)
  269. {
  270. struct memstick_request *req = host->req;
  271. int err = 0;
  272. u8 cfg = 0, int_reg;
  273. dev_dbg(ms_dev(host), "%s\n", __func__);
  274. if (req->need_card_int) {
  275. if (host->ifmode != MEMSTICK_SERIAL)
  276. cfg = WAIT_INT;
  277. }
  278. if (req->long_data) {
  279. err = ms_transfer_data(host, req->data_dir,
  280. req->tpc, cfg, &(req->sg));
  281. } else {
  282. if (req->data_dir == READ) {
  283. err = ms_read_bytes(host, req->tpc, cfg,
  284. req->data_len, req->data, &int_reg);
  285. } else {
  286. err = ms_write_bytes(host, req->tpc, cfg,
  287. req->data_len, req->data, &int_reg);
  288. }
  289. }
  290. if (err < 0)
  291. return err;
  292. if (req->need_card_int && (host->ifmode == MEMSTICK_SERIAL)) {
  293. err = ms_read_bytes(host, MS_TPC_GET_INT,
  294. NO_WAIT_INT, 1, &int_reg, NULL);
  295. if (err < 0)
  296. return err;
  297. }
  298. if (req->need_card_int) {
  299. dev_dbg(ms_dev(host), "int_reg: 0x%02x\n", int_reg);
  300. if (int_reg & MS_INT_CMDNK)
  301. req->int_reg |= MEMSTICK_INT_CMDNAK;
  302. if (int_reg & MS_INT_BREQ)
  303. req->int_reg |= MEMSTICK_INT_BREQ;
  304. if (int_reg & MS_INT_ERR)
  305. req->int_reg |= MEMSTICK_INT_ERR;
  306. if (int_reg & MS_INT_CED)
  307. req->int_reg |= MEMSTICK_INT_CED;
  308. }
  309. return 0;
  310. }
  311. static void rtsx_pci_ms_handle_req(struct work_struct *work)
  312. {
  313. struct realtek_pci_ms *host = container_of(work,
  314. struct realtek_pci_ms, handle_req);
  315. struct rtsx_pcr *pcr = host->pcr;
  316. struct memstick_host *msh = host->msh;
  317. int rc;
  318. mutex_lock(&pcr->pcr_mutex);
  319. rtsx_pci_start_run(pcr);
  320. rtsx_pci_switch_clock(host->pcr, host->clock, host->ssc_depth,
  321. false, true, false);
  322. rtsx_pci_write_register(pcr, CARD_SELECT, 0x07, MS_MOD_SEL);
  323. rtsx_pci_write_register(pcr, CARD_SHARE_MODE,
  324. CARD_SHARE_MASK, CARD_SHARE_48_MS);
  325. if (!host->req) {
  326. do {
  327. rc = memstick_next_req(msh, &host->req);
  328. dev_dbg(ms_dev(host), "next req %d\n", rc);
  329. if (!rc)
  330. host->req->error = rtsx_pci_ms_issue_cmd(host);
  331. } while (!rc);
  332. }
  333. mutex_unlock(&pcr->pcr_mutex);
  334. }
  335. static void rtsx_pci_ms_request(struct memstick_host *msh)
  336. {
  337. struct realtek_pci_ms *host = memstick_priv(msh);
  338. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  339. if (rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD))
  340. return;
  341. schedule_work(&host->handle_req);
  342. }
  343. static int rtsx_pci_ms_set_param(struct memstick_host *msh,
  344. enum memstick_param param, int value)
  345. {
  346. struct realtek_pci_ms *host = memstick_priv(msh);
  347. struct rtsx_pcr *pcr = host->pcr;
  348. unsigned int clock = 0;
  349. u8 ssc_depth = 0;
  350. int err;
  351. dev_dbg(ms_dev(host), "%s: param = %d, value = %d\n",
  352. __func__, param, value);
  353. err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD);
  354. if (err)
  355. return err;
  356. switch (param) {
  357. case MEMSTICK_POWER:
  358. if (value == MEMSTICK_POWER_ON)
  359. err = ms_power_on(host);
  360. else if (value == MEMSTICK_POWER_OFF)
  361. err = ms_power_off(host);
  362. else
  363. return -EINVAL;
  364. break;
  365. case MEMSTICK_INTERFACE:
  366. if (value == MEMSTICK_SERIAL) {
  367. clock = 19000000;
  368. ssc_depth = RTSX_SSC_DEPTH_500K;
  369. err = rtsx_pci_write_register(pcr, MS_CFG, 0x58,
  370. MS_BUS_WIDTH_1 | PUSH_TIME_DEFAULT);
  371. if (err < 0)
  372. return err;
  373. } else if (value == MEMSTICK_PAR4) {
  374. clock = 39000000;
  375. ssc_depth = RTSX_SSC_DEPTH_1M;
  376. err = rtsx_pci_write_register(pcr, MS_CFG,
  377. 0x58, MS_BUS_WIDTH_4 | PUSH_TIME_ODD);
  378. if (err < 0)
  379. return err;
  380. } else {
  381. return -EINVAL;
  382. }
  383. err = rtsx_pci_switch_clock(pcr, clock,
  384. ssc_depth, false, true, false);
  385. if (err < 0)
  386. return err;
  387. host->ssc_depth = ssc_depth;
  388. host->clock = clock;
  389. host->ifmode = value;
  390. break;
  391. }
  392. return 0;
  393. }
  394. #ifdef CONFIG_PM
  395. static int rtsx_pci_ms_suspend(struct platform_device *pdev, pm_message_t state)
  396. {
  397. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  398. struct memstick_host *msh = host->msh;
  399. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  400. memstick_suspend_host(msh);
  401. return 0;
  402. }
  403. static int rtsx_pci_ms_resume(struct platform_device *pdev)
  404. {
  405. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  406. struct memstick_host *msh = host->msh;
  407. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  408. memstick_resume_host(msh);
  409. return 0;
  410. }
  411. #else /* CONFIG_PM */
  412. #define rtsx_pci_ms_suspend NULL
  413. #define rtsx_pci_ms_resume NULL
  414. #endif /* CONFIG_PM */
  415. static void rtsx_pci_ms_card_event(struct platform_device *pdev)
  416. {
  417. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  418. memstick_detect_change(host->msh);
  419. }
  420. static int rtsx_pci_ms_drv_probe(struct platform_device *pdev)
  421. {
  422. struct memstick_host *msh;
  423. struct realtek_pci_ms *host;
  424. struct rtsx_pcr *pcr;
  425. struct pcr_handle *handle = pdev->dev.platform_data;
  426. int rc;
  427. if (!handle)
  428. return -ENXIO;
  429. pcr = handle->pcr;
  430. if (!pcr)
  431. return -ENXIO;
  432. dev_dbg(&(pdev->dev),
  433. ": Realtek PCI-E Memstick controller found\n");
  434. msh = memstick_alloc_host(sizeof(*host), &pdev->dev);
  435. if (!msh)
  436. return -ENOMEM;
  437. host = memstick_priv(msh);
  438. host->pcr = pcr;
  439. host->msh = msh;
  440. host->pdev = pdev;
  441. platform_set_drvdata(pdev, host);
  442. pcr->slots[RTSX_MS_CARD].p_dev = pdev;
  443. pcr->slots[RTSX_MS_CARD].card_event = rtsx_pci_ms_card_event;
  444. mutex_init(&host->host_mutex);
  445. INIT_WORK(&host->handle_req, rtsx_pci_ms_handle_req);
  446. msh->request = rtsx_pci_ms_request;
  447. msh->set_param = rtsx_pci_ms_set_param;
  448. msh->caps = MEMSTICK_CAP_PAR4;
  449. rc = memstick_add_host(msh);
  450. if (rc) {
  451. memstick_free_host(msh);
  452. return rc;
  453. }
  454. return 0;
  455. }
  456. static int rtsx_pci_ms_drv_remove(struct platform_device *pdev)
  457. {
  458. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  459. struct rtsx_pcr *pcr;
  460. struct memstick_host *msh;
  461. int rc;
  462. if (!host)
  463. return 0;
  464. pcr = host->pcr;
  465. pcr->slots[RTSX_MS_CARD].p_dev = NULL;
  466. pcr->slots[RTSX_MS_CARD].card_event = NULL;
  467. msh = host->msh;
  468. host->eject = true;
  469. cancel_work_sync(&host->handle_req);
  470. mutex_lock(&host->host_mutex);
  471. if (host->req) {
  472. dev_dbg(&(pdev->dev),
  473. "%s: Controller removed during transfer\n",
  474. dev_name(&msh->dev));
  475. rtsx_pci_complete_unfinished_transfer(pcr);
  476. host->req->error = -ENOMEDIUM;
  477. do {
  478. rc = memstick_next_req(msh, &host->req);
  479. if (!rc)
  480. host->req->error = -ENOMEDIUM;
  481. } while (!rc);
  482. }
  483. mutex_unlock(&host->host_mutex);
  484. memstick_remove_host(msh);
  485. memstick_free_host(msh);
  486. dev_dbg(&(pdev->dev),
  487. ": Realtek PCI-E Memstick controller has been removed\n");
  488. return 0;
  489. }
  490. static struct platform_device_id rtsx_pci_ms_ids[] = {
  491. {
  492. .name = DRV_NAME_RTSX_PCI_MS,
  493. }, {
  494. /* sentinel */
  495. }
  496. };
  497. MODULE_DEVICE_TABLE(platform, rtsx_pci_ms_ids);
  498. static struct platform_driver rtsx_pci_ms_driver = {
  499. .probe = rtsx_pci_ms_drv_probe,
  500. .remove = rtsx_pci_ms_drv_remove,
  501. .id_table = rtsx_pci_ms_ids,
  502. .suspend = rtsx_pci_ms_suspend,
  503. .resume = rtsx_pci_ms_resume,
  504. .driver = {
  505. .name = DRV_NAME_RTSX_PCI_MS,
  506. },
  507. };
  508. module_platform_driver(rtsx_pci_ms_driver);
  509. MODULE_LICENSE("GPL");
  510. MODULE_AUTHOR("Wei WANG <[email protected]>");
  511. MODULE_DESCRIPTION("Realtek PCI-E Memstick Card Host Driver");