rtsx_pci_ms.c 15 KB

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