rtsx_pci_ms.c 15 KB

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