rtsx_pci_ms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. struct memstick_dev *card = host->msh->card;
  122. bool pro_card = card->id.type == MEMSTICK_TYPE_PRO;
  123. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
  124. __func__, tpc, (data_dir == READ) ? "READ" : "WRITE",
  125. length);
  126. if (data_dir == READ) {
  127. dma_dir = DMA_DIR_FROM_CARD;
  128. trans_mode = pro_card ? MS_TM_AUTO_READ : MS_TM_NORMAL_READ;
  129. } else {
  130. dma_dir = DMA_DIR_TO_CARD;
  131. trans_mode = pro_card ? MS_TM_AUTO_WRITE : MS_TM_NORMAL_WRITE;
  132. }
  133. rtsx_pci_init_cmd(pcr);
  134. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  135. if (pro_card) {
  136. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_H,
  137. 0xFF, (u8)(sec_cnt >> 8));
  138. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_L,
  139. 0xFF, (u8)sec_cnt);
  140. }
  141. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  142. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
  143. DMA_DONE_INT, DMA_DONE_INT);
  144. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3, 0xFF, (u8)(length >> 24));
  145. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2, 0xFF, (u8)(length >> 16));
  146. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1, 0xFF, (u8)(length >> 8));
  147. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)length);
  148. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
  149. 0x03 | DMA_PACK_SIZE_MASK, dma_dir | DMA_EN | DMA_512);
  150. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  151. 0x01, RING_BUFFER);
  152. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  153. 0xFF, MS_TRANSFER_START | trans_mode);
  154. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  155. MS_TRANSFER_END, MS_TRANSFER_END);
  156. rtsx_pci_send_cmd_no_wait(pcr);
  157. err = rtsx_pci_transfer_data(pcr, sg, 1, data_dir == READ, 10000);
  158. if (err < 0) {
  159. ms_clear_error(host);
  160. return err;
  161. }
  162. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  163. if (pro_card) {
  164. if (val & (MS_INT_CMDNK | MS_INT_ERR |
  165. MS_CRC16_ERR | MS_RDY_TIMEOUT))
  166. return -EIO;
  167. } else {
  168. if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT))
  169. return -EIO;
  170. }
  171. return 0;
  172. }
  173. static int ms_write_bytes(struct realtek_pci_ms *host, u8 tpc,
  174. u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
  175. {
  176. struct rtsx_pcr *pcr = host->pcr;
  177. int err, i;
  178. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
  179. if (!data)
  180. return -EINVAL;
  181. rtsx_pci_init_cmd(pcr);
  182. for (i = 0; i < cnt; i++)
  183. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  184. PPBUF_BASE2 + i, 0xFF, data[i]);
  185. if (cnt % 2)
  186. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
  187. PPBUF_BASE2 + i, 0xFF, 0xFF);
  188. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  189. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
  190. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  191. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  192. 0x01, PINGPONG_BUFFER);
  193. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  194. 0xFF, MS_TRANSFER_START | MS_TM_WRITE_BYTES);
  195. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  196. MS_TRANSFER_END, MS_TRANSFER_END);
  197. if (int_reg)
  198. rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
  199. err = rtsx_pci_send_cmd(pcr, 5000);
  200. if (err < 0) {
  201. u8 val;
  202. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  203. dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
  204. if (int_reg)
  205. *int_reg = val & 0x0F;
  206. ms_print_debug_regs(host);
  207. ms_clear_error(host);
  208. if (!(tpc & 0x08)) {
  209. if (val & MS_CRC16_ERR)
  210. return -EIO;
  211. } else {
  212. if (!(val & 0x80)) {
  213. if (val & (MS_INT_ERR | MS_INT_CMDNK))
  214. return -EIO;
  215. }
  216. }
  217. return -ETIMEDOUT;
  218. }
  219. if (int_reg) {
  220. u8 *ptr = rtsx_pci_get_cmd_data(pcr) + 1;
  221. *int_reg = *ptr & 0x0F;
  222. }
  223. return 0;
  224. }
  225. static int ms_read_bytes(struct realtek_pci_ms *host, u8 tpc,
  226. u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
  227. {
  228. struct rtsx_pcr *pcr = host->pcr;
  229. int err, i;
  230. u8 *ptr;
  231. dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
  232. if (!data)
  233. return -EINVAL;
  234. rtsx_pci_init_cmd(pcr);
  235. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
  236. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
  237. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
  238. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
  239. 0x01, PINGPONG_BUFFER);
  240. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
  241. 0xFF, MS_TRANSFER_START | MS_TM_READ_BYTES);
  242. rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
  243. MS_TRANSFER_END, MS_TRANSFER_END);
  244. for (i = 0; i < cnt - 1; i++)
  245. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + i, 0, 0);
  246. if (cnt % 2)
  247. rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + cnt, 0, 0);
  248. else
  249. rtsx_pci_add_cmd(pcr, READ_REG_CMD,
  250. PPBUF_BASE2 + cnt - 1, 0, 0);
  251. if (int_reg)
  252. rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
  253. err = rtsx_pci_send_cmd(pcr, 5000);
  254. if (err < 0) {
  255. u8 val;
  256. rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
  257. dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
  258. if (int_reg)
  259. *int_reg = val & 0x0F;
  260. ms_print_debug_regs(host);
  261. ms_clear_error(host);
  262. if (!(tpc & 0x08)) {
  263. if (val & MS_CRC16_ERR)
  264. return -EIO;
  265. } else {
  266. if (!(val & 0x80)) {
  267. if (val & (MS_INT_ERR | MS_INT_CMDNK))
  268. return -EIO;
  269. }
  270. }
  271. return -ETIMEDOUT;
  272. }
  273. ptr = rtsx_pci_get_cmd_data(pcr) + 1;
  274. for (i = 0; i < cnt; i++)
  275. data[i] = *ptr++;
  276. if (int_reg)
  277. *int_reg = *ptr & 0x0F;
  278. return 0;
  279. }
  280. static int rtsx_pci_ms_issue_cmd(struct realtek_pci_ms *host)
  281. {
  282. struct memstick_request *req = host->req;
  283. int err = 0;
  284. u8 cfg = 0, int_reg;
  285. dev_dbg(ms_dev(host), "%s\n", __func__);
  286. if (req->need_card_int) {
  287. if (host->ifmode != MEMSTICK_SERIAL)
  288. cfg = WAIT_INT;
  289. }
  290. if (req->long_data) {
  291. err = ms_transfer_data(host, req->data_dir,
  292. req->tpc, cfg, &(req->sg));
  293. } else {
  294. if (req->data_dir == READ) {
  295. err = ms_read_bytes(host, req->tpc, cfg,
  296. req->data_len, req->data, &int_reg);
  297. } else {
  298. err = ms_write_bytes(host, req->tpc, cfg,
  299. req->data_len, req->data, &int_reg);
  300. }
  301. }
  302. if (err < 0)
  303. return err;
  304. if (req->need_card_int && (host->ifmode == MEMSTICK_SERIAL)) {
  305. err = ms_read_bytes(host, MS_TPC_GET_INT,
  306. NO_WAIT_INT, 1, &int_reg, NULL);
  307. if (err < 0)
  308. return err;
  309. }
  310. if (req->need_card_int) {
  311. dev_dbg(ms_dev(host), "int_reg: 0x%02x\n", int_reg);
  312. if (int_reg & MS_INT_CMDNK)
  313. req->int_reg |= MEMSTICK_INT_CMDNAK;
  314. if (int_reg & MS_INT_BREQ)
  315. req->int_reg |= MEMSTICK_INT_BREQ;
  316. if (int_reg & MS_INT_ERR)
  317. req->int_reg |= MEMSTICK_INT_ERR;
  318. if (int_reg & MS_INT_CED)
  319. req->int_reg |= MEMSTICK_INT_CED;
  320. }
  321. return 0;
  322. }
  323. static void rtsx_pci_ms_handle_req(struct work_struct *work)
  324. {
  325. struct realtek_pci_ms *host = container_of(work,
  326. struct realtek_pci_ms, handle_req);
  327. struct rtsx_pcr *pcr = host->pcr;
  328. struct memstick_host *msh = host->msh;
  329. int rc;
  330. mutex_lock(&pcr->pcr_mutex);
  331. rtsx_pci_start_run(pcr);
  332. rtsx_pci_switch_clock(host->pcr, host->clock, host->ssc_depth,
  333. false, true, false);
  334. rtsx_pci_write_register(pcr, CARD_SELECT, 0x07, MS_MOD_SEL);
  335. rtsx_pci_write_register(pcr, CARD_SHARE_MODE,
  336. CARD_SHARE_MASK, CARD_SHARE_48_MS);
  337. if (!host->req) {
  338. do {
  339. rc = memstick_next_req(msh, &host->req);
  340. dev_dbg(ms_dev(host), "next req %d\n", rc);
  341. if (!rc)
  342. host->req->error = rtsx_pci_ms_issue_cmd(host);
  343. } while (!rc);
  344. }
  345. mutex_unlock(&pcr->pcr_mutex);
  346. }
  347. static void rtsx_pci_ms_request(struct memstick_host *msh)
  348. {
  349. struct realtek_pci_ms *host = memstick_priv(msh);
  350. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  351. if (rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD))
  352. return;
  353. schedule_work(&host->handle_req);
  354. }
  355. static int rtsx_pci_ms_set_param(struct memstick_host *msh,
  356. enum memstick_param param, int value)
  357. {
  358. struct realtek_pci_ms *host = memstick_priv(msh);
  359. struct rtsx_pcr *pcr = host->pcr;
  360. unsigned int clock = 0;
  361. u8 ssc_depth = 0;
  362. int err;
  363. dev_dbg(ms_dev(host), "%s: param = %d, value = %d\n",
  364. __func__, param, value);
  365. err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD);
  366. if (err)
  367. return err;
  368. switch (param) {
  369. case MEMSTICK_POWER:
  370. if (value == MEMSTICK_POWER_ON)
  371. err = ms_power_on(host);
  372. else if (value == MEMSTICK_POWER_OFF)
  373. err = ms_power_off(host);
  374. else
  375. return -EINVAL;
  376. break;
  377. case MEMSTICK_INTERFACE:
  378. if (value == MEMSTICK_SERIAL) {
  379. clock = 19000000;
  380. ssc_depth = RTSX_SSC_DEPTH_500K;
  381. err = rtsx_pci_write_register(pcr, MS_CFG, 0x58,
  382. MS_BUS_WIDTH_1 | PUSH_TIME_DEFAULT);
  383. if (err < 0)
  384. return err;
  385. } else if (value == MEMSTICK_PAR4) {
  386. clock = 39000000;
  387. ssc_depth = RTSX_SSC_DEPTH_1M;
  388. err = rtsx_pci_write_register(pcr, MS_CFG,
  389. 0x58, MS_BUS_WIDTH_4 | PUSH_TIME_ODD);
  390. if (err < 0)
  391. return err;
  392. } else {
  393. return -EINVAL;
  394. }
  395. err = rtsx_pci_switch_clock(pcr, clock,
  396. ssc_depth, false, true, false);
  397. if (err < 0)
  398. return err;
  399. host->ssc_depth = ssc_depth;
  400. host->clock = clock;
  401. host->ifmode = value;
  402. break;
  403. }
  404. return 0;
  405. }
  406. #ifdef CONFIG_PM
  407. static int rtsx_pci_ms_suspend(struct platform_device *pdev, pm_message_t state)
  408. {
  409. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  410. struct memstick_host *msh = host->msh;
  411. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  412. memstick_suspend_host(msh);
  413. return 0;
  414. }
  415. static int rtsx_pci_ms_resume(struct platform_device *pdev)
  416. {
  417. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  418. struct memstick_host *msh = host->msh;
  419. dev_dbg(ms_dev(host), "--> %s\n", __func__);
  420. memstick_resume_host(msh);
  421. return 0;
  422. }
  423. #else /* CONFIG_PM */
  424. #define rtsx_pci_ms_suspend NULL
  425. #define rtsx_pci_ms_resume NULL
  426. #endif /* CONFIG_PM */
  427. static void rtsx_pci_ms_card_event(struct platform_device *pdev)
  428. {
  429. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  430. memstick_detect_change(host->msh);
  431. }
  432. static int rtsx_pci_ms_drv_probe(struct platform_device *pdev)
  433. {
  434. struct memstick_host *msh;
  435. struct realtek_pci_ms *host;
  436. struct rtsx_pcr *pcr;
  437. struct pcr_handle *handle = pdev->dev.platform_data;
  438. int rc;
  439. if (!handle)
  440. return -ENXIO;
  441. pcr = handle->pcr;
  442. if (!pcr)
  443. return -ENXIO;
  444. dev_dbg(&(pdev->dev),
  445. ": Realtek PCI-E Memstick controller found\n");
  446. msh = memstick_alloc_host(sizeof(*host), &pdev->dev);
  447. if (!msh)
  448. return -ENOMEM;
  449. host = memstick_priv(msh);
  450. host->pcr = pcr;
  451. host->msh = msh;
  452. host->pdev = pdev;
  453. platform_set_drvdata(pdev, host);
  454. pcr->slots[RTSX_MS_CARD].p_dev = pdev;
  455. pcr->slots[RTSX_MS_CARD].card_event = rtsx_pci_ms_card_event;
  456. mutex_init(&host->host_mutex);
  457. INIT_WORK(&host->handle_req, rtsx_pci_ms_handle_req);
  458. msh->request = rtsx_pci_ms_request;
  459. msh->set_param = rtsx_pci_ms_set_param;
  460. msh->caps = MEMSTICK_CAP_PAR4;
  461. rc = memstick_add_host(msh);
  462. if (rc) {
  463. memstick_free_host(msh);
  464. return rc;
  465. }
  466. return 0;
  467. }
  468. static int rtsx_pci_ms_drv_remove(struct platform_device *pdev)
  469. {
  470. struct realtek_pci_ms *host = platform_get_drvdata(pdev);
  471. struct rtsx_pcr *pcr;
  472. struct memstick_host *msh;
  473. int rc;
  474. if (!host)
  475. return 0;
  476. pcr = host->pcr;
  477. pcr->slots[RTSX_MS_CARD].p_dev = NULL;
  478. pcr->slots[RTSX_MS_CARD].card_event = NULL;
  479. msh = host->msh;
  480. host->eject = true;
  481. cancel_work_sync(&host->handle_req);
  482. mutex_lock(&host->host_mutex);
  483. if (host->req) {
  484. dev_dbg(&(pdev->dev),
  485. "%s: Controller removed during transfer\n",
  486. dev_name(&msh->dev));
  487. rtsx_pci_complete_unfinished_transfer(pcr);
  488. host->req->error = -ENOMEDIUM;
  489. do {
  490. rc = memstick_next_req(msh, &host->req);
  491. if (!rc)
  492. host->req->error = -ENOMEDIUM;
  493. } while (!rc);
  494. }
  495. mutex_unlock(&host->host_mutex);
  496. memstick_remove_host(msh);
  497. memstick_free_host(msh);
  498. dev_dbg(&(pdev->dev),
  499. ": Realtek PCI-E Memstick controller has been removed\n");
  500. return 0;
  501. }
  502. static struct platform_device_id rtsx_pci_ms_ids[] = {
  503. {
  504. .name = DRV_NAME_RTSX_PCI_MS,
  505. }, {
  506. /* sentinel */
  507. }
  508. };
  509. MODULE_DEVICE_TABLE(platform, rtsx_pci_ms_ids);
  510. static struct platform_driver rtsx_pci_ms_driver = {
  511. .probe = rtsx_pci_ms_drv_probe,
  512. .remove = rtsx_pci_ms_drv_remove,
  513. .id_table = rtsx_pci_ms_ids,
  514. .suspend = rtsx_pci_ms_suspend,
  515. .resume = rtsx_pci_ms_resume,
  516. .driver = {
  517. .name = DRV_NAME_RTSX_PCI_MS,
  518. },
  519. };
  520. module_platform_driver(rtsx_pci_ms_driver);
  521. MODULE_LICENSE("GPL");
  522. MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
  523. MODULE_DESCRIPTION("Realtek PCI-E Memstick Card Host Driver");