memstick.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Sony MemoryStick support
  3. *
  4. * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Special thanks to Carlos Corbacho for providing various MemoryStick cards
  11. * that made this driver possible.
  12. *
  13. */
  14. #include <linux/memstick.h>
  15. #include <linux/idr.h>
  16. #include <linux/fs.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/pm_runtime.h>
  21. #define DRIVER_NAME "memstick"
  22. static unsigned int cmd_retries = 3;
  23. module_param(cmd_retries, uint, 0644);
  24. static struct workqueue_struct *workqueue;
  25. static DEFINE_IDR(memstick_host_idr);
  26. static DEFINE_SPINLOCK(memstick_host_lock);
  27. static int memstick_dev_match(struct memstick_dev *card,
  28. struct memstick_device_id *id)
  29. {
  30. if (id->match_flags & MEMSTICK_MATCH_ALL) {
  31. if ((id->type == card->id.type)
  32. && (id->category == card->id.category)
  33. && (id->class == card->id.class))
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. static int memstick_bus_match(struct device *dev, struct device_driver *drv)
  39. {
  40. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  41. dev);
  42. struct memstick_driver *ms_drv = container_of(drv,
  43. struct memstick_driver,
  44. driver);
  45. struct memstick_device_id *ids = ms_drv->id_table;
  46. if (ids) {
  47. while (ids->match_flags) {
  48. if (memstick_dev_match(card, ids))
  49. return 1;
  50. ++ids;
  51. }
  52. }
  53. return 0;
  54. }
  55. static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
  56. {
  57. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  58. dev);
  59. if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
  60. return -ENOMEM;
  61. if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
  62. return -ENOMEM;
  63. if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
  64. return -ENOMEM;
  65. return 0;
  66. }
  67. static int memstick_device_probe(struct device *dev)
  68. {
  69. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  70. dev);
  71. struct memstick_driver *drv = container_of(dev->driver,
  72. struct memstick_driver,
  73. driver);
  74. int rc = -ENODEV;
  75. if (dev->driver && drv->probe) {
  76. rc = drv->probe(card);
  77. if (!rc)
  78. get_device(dev);
  79. }
  80. return rc;
  81. }
  82. static int memstick_device_remove(struct device *dev)
  83. {
  84. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  85. dev);
  86. struct memstick_driver *drv = container_of(dev->driver,
  87. struct memstick_driver,
  88. driver);
  89. if (dev->driver && drv->remove) {
  90. drv->remove(card);
  91. card->dev.driver = NULL;
  92. }
  93. put_device(dev);
  94. return 0;
  95. }
  96. #ifdef CONFIG_PM
  97. static int memstick_device_suspend(struct device *dev, pm_message_t state)
  98. {
  99. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  100. dev);
  101. struct memstick_driver *drv = container_of(dev->driver,
  102. struct memstick_driver,
  103. driver);
  104. if (dev->driver && drv->suspend)
  105. return drv->suspend(card, state);
  106. return 0;
  107. }
  108. static int memstick_device_resume(struct device *dev)
  109. {
  110. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  111. dev);
  112. struct memstick_driver *drv = container_of(dev->driver,
  113. struct memstick_driver,
  114. driver);
  115. if (dev->driver && drv->resume)
  116. return drv->resume(card);
  117. return 0;
  118. }
  119. #else
  120. #define memstick_device_suspend NULL
  121. #define memstick_device_resume NULL
  122. #endif /* CONFIG_PM */
  123. #define MEMSTICK_ATTR(name, format) \
  124. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
  125. char *buf) \
  126. { \
  127. struct memstick_dev *card = container_of(dev, struct memstick_dev, \
  128. dev); \
  129. return sprintf(buf, format, card->id.name); \
  130. } \
  131. static DEVICE_ATTR_RO(name);
  132. MEMSTICK_ATTR(type, "%02X");
  133. MEMSTICK_ATTR(category, "%02X");
  134. MEMSTICK_ATTR(class, "%02X");
  135. static struct attribute *memstick_dev_attrs[] = {
  136. &dev_attr_type.attr,
  137. &dev_attr_category.attr,
  138. &dev_attr_class.attr,
  139. NULL,
  140. };
  141. ATTRIBUTE_GROUPS(memstick_dev);
  142. static struct bus_type memstick_bus_type = {
  143. .name = "memstick",
  144. .dev_groups = memstick_dev_groups,
  145. .match = memstick_bus_match,
  146. .uevent = memstick_uevent,
  147. .probe = memstick_device_probe,
  148. .remove = memstick_device_remove,
  149. .suspend = memstick_device_suspend,
  150. .resume = memstick_device_resume
  151. };
  152. static void memstick_free(struct device *dev)
  153. {
  154. struct memstick_host *host = container_of(dev, struct memstick_host,
  155. dev);
  156. kfree(host);
  157. }
  158. static struct class memstick_host_class = {
  159. .name = "memstick_host",
  160. .dev_release = memstick_free
  161. };
  162. static void memstick_free_card(struct device *dev)
  163. {
  164. struct memstick_dev *card = container_of(dev, struct memstick_dev,
  165. dev);
  166. kfree(card);
  167. }
  168. static int memstick_dummy_check(struct memstick_dev *card)
  169. {
  170. return 0;
  171. }
  172. /**
  173. * memstick_detect_change - schedule media detection on memstick host
  174. * @host - host to use
  175. */
  176. void memstick_detect_change(struct memstick_host *host)
  177. {
  178. queue_work(workqueue, &host->media_checker);
  179. }
  180. EXPORT_SYMBOL(memstick_detect_change);
  181. /**
  182. * memstick_next_req - called by host driver to obtain next request to process
  183. * @host - host to use
  184. * @mrq - pointer to stick the request to
  185. *
  186. * Host calls this function from idle state (*mrq == NULL) or after finishing
  187. * previous request (*mrq should point to it). If previous request was
  188. * unsuccessful, it is retried for predetermined number of times. Return value
  189. * of 0 means that new request was assigned to the host.
  190. */
  191. int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
  192. {
  193. int rc = -ENXIO;
  194. if ((*mrq) && (*mrq)->error && host->retries) {
  195. (*mrq)->error = rc;
  196. host->retries--;
  197. return 0;
  198. }
  199. if (host->card && host->card->next_request)
  200. rc = host->card->next_request(host->card, mrq);
  201. if (!rc)
  202. host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
  203. else
  204. *mrq = NULL;
  205. return rc;
  206. }
  207. EXPORT_SYMBOL(memstick_next_req);
  208. /**
  209. * memstick_new_req - notify the host that some requests are pending
  210. * @host - host to use
  211. */
  212. void memstick_new_req(struct memstick_host *host)
  213. {
  214. if (host->card) {
  215. host->retries = cmd_retries;
  216. reinit_completion(&host->card->mrq_complete);
  217. host->request(host);
  218. }
  219. }
  220. EXPORT_SYMBOL(memstick_new_req);
  221. /**
  222. * memstick_init_req_sg - set request fields needed for bulk data transfer
  223. * @mrq - request to use
  224. * @tpc - memstick Transport Protocol Command
  225. * @sg - TPC argument
  226. */
  227. void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
  228. const struct scatterlist *sg)
  229. {
  230. mrq->tpc = tpc;
  231. if (tpc & 8)
  232. mrq->data_dir = WRITE;
  233. else
  234. mrq->data_dir = READ;
  235. mrq->sg = *sg;
  236. mrq->long_data = 1;
  237. if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
  238. mrq->need_card_int = 1;
  239. else
  240. mrq->need_card_int = 0;
  241. }
  242. EXPORT_SYMBOL(memstick_init_req_sg);
  243. /**
  244. * memstick_init_req - set request fields needed for short data transfer
  245. * @mrq - request to use
  246. * @tpc - memstick Transport Protocol Command
  247. * @buf - TPC argument buffer
  248. * @length - TPC argument size
  249. *
  250. * The intended use of this function (transfer of data items several bytes
  251. * in size) allows us to just copy the value between request structure and
  252. * user supplied buffer.
  253. */
  254. void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
  255. const void *buf, size_t length)
  256. {
  257. mrq->tpc = tpc;
  258. if (tpc & 8)
  259. mrq->data_dir = WRITE;
  260. else
  261. mrq->data_dir = READ;
  262. mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
  263. if (mrq->data_dir == WRITE)
  264. memcpy(mrq->data, buf, mrq->data_len);
  265. mrq->long_data = 0;
  266. if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
  267. mrq->need_card_int = 1;
  268. else
  269. mrq->need_card_int = 0;
  270. }
  271. EXPORT_SYMBOL(memstick_init_req);
  272. /*
  273. * Functions prefixed with "h_" are protocol callbacks. They can be called from
  274. * interrupt context. Return value of 0 means that request processing is still
  275. * ongoing, while special error value of -EAGAIN means that current request is
  276. * finished (and request processor should come back some time later).
  277. */
  278. static int h_memstick_read_dev_id(struct memstick_dev *card,
  279. struct memstick_request **mrq)
  280. {
  281. struct ms_id_register id_reg;
  282. if (!(*mrq)) {
  283. memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg,
  284. sizeof(struct ms_id_register));
  285. *mrq = &card->current_mrq;
  286. return 0;
  287. } else {
  288. if (!(*mrq)->error) {
  289. memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
  290. card->id.match_flags = MEMSTICK_MATCH_ALL;
  291. card->id.type = id_reg.type;
  292. card->id.category = id_reg.category;
  293. card->id.class = id_reg.class;
  294. dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
  295. }
  296. complete(&card->mrq_complete);
  297. return -EAGAIN;
  298. }
  299. }
  300. static int h_memstick_set_rw_addr(struct memstick_dev *card,
  301. struct memstick_request **mrq)
  302. {
  303. if (!(*mrq)) {
  304. memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
  305. (char *)&card->reg_addr,
  306. sizeof(card->reg_addr));
  307. *mrq = &card->current_mrq;
  308. return 0;
  309. } else {
  310. complete(&card->mrq_complete);
  311. return -EAGAIN;
  312. }
  313. }
  314. /**
  315. * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
  316. * complete
  317. * @card - media device to use
  318. */
  319. int memstick_set_rw_addr(struct memstick_dev *card)
  320. {
  321. card->next_request = h_memstick_set_rw_addr;
  322. memstick_new_req(card->host);
  323. wait_for_completion(&card->mrq_complete);
  324. return card->current_mrq.error;
  325. }
  326. EXPORT_SYMBOL(memstick_set_rw_addr);
  327. static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
  328. {
  329. struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
  330. GFP_KERNEL);
  331. struct memstick_dev *old_card = host->card;
  332. struct ms_id_register id_reg;
  333. if (card) {
  334. card->host = host;
  335. dev_set_name(&card->dev, "%s", dev_name(&host->dev));
  336. card->dev.parent = &host->dev;
  337. card->dev.bus = &memstick_bus_type;
  338. card->dev.release = memstick_free_card;
  339. card->check = memstick_dummy_check;
  340. card->reg_addr.r_offset = offsetof(struct ms_register, id);
  341. card->reg_addr.r_length = sizeof(id_reg);
  342. card->reg_addr.w_offset = offsetof(struct ms_register, id);
  343. card->reg_addr.w_length = sizeof(id_reg);
  344. init_completion(&card->mrq_complete);
  345. host->card = card;
  346. if (memstick_set_rw_addr(card))
  347. goto err_out;
  348. card->next_request = h_memstick_read_dev_id;
  349. memstick_new_req(host);
  350. wait_for_completion(&card->mrq_complete);
  351. if (card->current_mrq.error)
  352. goto err_out;
  353. }
  354. host->card = old_card;
  355. return card;
  356. err_out:
  357. host->card = old_card;
  358. kfree(card);
  359. return NULL;
  360. }
  361. static int memstick_power_on(struct memstick_host *host)
  362. {
  363. int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
  364. if (!rc)
  365. rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
  366. return rc;
  367. }
  368. static void memstick_check(struct work_struct *work)
  369. {
  370. struct memstick_host *host = container_of(work, struct memstick_host,
  371. media_checker);
  372. struct memstick_dev *card;
  373. dev_dbg(&host->dev, "memstick_check started\n");
  374. pm_runtime_get_noresume(host->dev.parent);
  375. mutex_lock(&host->lock);
  376. if (!host->card) {
  377. if (memstick_power_on(host))
  378. goto out_power_off;
  379. } else if (host->card->stop)
  380. host->card->stop(host->card);
  381. card = memstick_alloc_card(host);
  382. if (!card) {
  383. if (host->card) {
  384. device_unregister(&host->card->dev);
  385. host->card = NULL;
  386. }
  387. } else {
  388. dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
  389. card->id.type, card->id.category, card->id.class);
  390. if (host->card) {
  391. if (memstick_set_rw_addr(host->card)
  392. || !memstick_dev_match(host->card, &card->id)
  393. || !(host->card->check(host->card))) {
  394. device_unregister(&host->card->dev);
  395. host->card = NULL;
  396. } else if (host->card->start)
  397. host->card->start(host->card);
  398. }
  399. if (!host->card) {
  400. host->card = card;
  401. if (device_register(&card->dev)) {
  402. put_device(&card->dev);
  403. kfree(host->card);
  404. host->card = NULL;
  405. }
  406. } else
  407. kfree(card);
  408. }
  409. out_power_off:
  410. if (!host->card)
  411. host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  412. mutex_unlock(&host->lock);
  413. pm_runtime_put(host->dev.parent);
  414. dev_dbg(&host->dev, "memstick_check finished\n");
  415. }
  416. /**
  417. * memstick_alloc_host - allocate a memstick_host structure
  418. * @extra: size of the user private data to allocate
  419. * @dev: parent device of the host
  420. */
  421. struct memstick_host *memstick_alloc_host(unsigned int extra,
  422. struct device *dev)
  423. {
  424. struct memstick_host *host;
  425. host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
  426. if (host) {
  427. mutex_init(&host->lock);
  428. INIT_WORK(&host->media_checker, memstick_check);
  429. host->dev.class = &memstick_host_class;
  430. host->dev.parent = dev;
  431. device_initialize(&host->dev);
  432. }
  433. return host;
  434. }
  435. EXPORT_SYMBOL(memstick_alloc_host);
  436. /**
  437. * memstick_add_host - start request processing on memstick host
  438. * @host - host to use
  439. */
  440. int memstick_add_host(struct memstick_host *host)
  441. {
  442. int rc;
  443. idr_preload(GFP_KERNEL);
  444. spin_lock(&memstick_host_lock);
  445. rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
  446. if (rc >= 0)
  447. host->id = rc;
  448. spin_unlock(&memstick_host_lock);
  449. idr_preload_end();
  450. if (rc < 0)
  451. return rc;
  452. dev_set_name(&host->dev, "memstick%u", host->id);
  453. rc = device_add(&host->dev);
  454. if (rc) {
  455. spin_lock(&memstick_host_lock);
  456. idr_remove(&memstick_host_idr, host->id);
  457. spin_unlock(&memstick_host_lock);
  458. return rc;
  459. }
  460. host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  461. memstick_detect_change(host);
  462. return 0;
  463. }
  464. EXPORT_SYMBOL(memstick_add_host);
  465. /**
  466. * memstick_remove_host - stop request processing on memstick host
  467. * @host - host to use
  468. */
  469. void memstick_remove_host(struct memstick_host *host)
  470. {
  471. flush_workqueue(workqueue);
  472. mutex_lock(&host->lock);
  473. if (host->card)
  474. device_unregister(&host->card->dev);
  475. host->card = NULL;
  476. host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  477. mutex_unlock(&host->lock);
  478. spin_lock(&memstick_host_lock);
  479. idr_remove(&memstick_host_idr, host->id);
  480. spin_unlock(&memstick_host_lock);
  481. device_del(&host->dev);
  482. }
  483. EXPORT_SYMBOL(memstick_remove_host);
  484. /**
  485. * memstick_free_host - free memstick host
  486. * @host - host to use
  487. */
  488. void memstick_free_host(struct memstick_host *host)
  489. {
  490. mutex_destroy(&host->lock);
  491. put_device(&host->dev);
  492. }
  493. EXPORT_SYMBOL(memstick_free_host);
  494. /**
  495. * memstick_suspend_host - notify bus driver of host suspension
  496. * @host - host to use
  497. */
  498. void memstick_suspend_host(struct memstick_host *host)
  499. {
  500. mutex_lock(&host->lock);
  501. host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  502. mutex_unlock(&host->lock);
  503. }
  504. EXPORT_SYMBOL(memstick_suspend_host);
  505. /**
  506. * memstick_resume_host - notify bus driver of host resumption
  507. * @host - host to use
  508. */
  509. void memstick_resume_host(struct memstick_host *host)
  510. {
  511. int rc = 0;
  512. mutex_lock(&host->lock);
  513. if (host->card)
  514. rc = memstick_power_on(host);
  515. mutex_unlock(&host->lock);
  516. if (!rc)
  517. memstick_detect_change(host);
  518. }
  519. EXPORT_SYMBOL(memstick_resume_host);
  520. int memstick_register_driver(struct memstick_driver *drv)
  521. {
  522. drv->driver.bus = &memstick_bus_type;
  523. return driver_register(&drv->driver);
  524. }
  525. EXPORT_SYMBOL(memstick_register_driver);
  526. void memstick_unregister_driver(struct memstick_driver *drv)
  527. {
  528. driver_unregister(&drv->driver);
  529. }
  530. EXPORT_SYMBOL(memstick_unregister_driver);
  531. static int __init memstick_init(void)
  532. {
  533. int rc;
  534. workqueue = create_freezable_workqueue("kmemstick");
  535. if (!workqueue)
  536. return -ENOMEM;
  537. rc = bus_register(&memstick_bus_type);
  538. if (rc)
  539. goto error_destroy_workqueue;
  540. rc = class_register(&memstick_host_class);
  541. if (rc)
  542. goto error_bus_unregister;
  543. return 0;
  544. error_bus_unregister:
  545. bus_unregister(&memstick_bus_type);
  546. error_destroy_workqueue:
  547. destroy_workqueue(workqueue);
  548. return rc;
  549. }
  550. static void __exit memstick_exit(void)
  551. {
  552. class_unregister(&memstick_host_class);
  553. bus_unregister(&memstick_bus_type);
  554. destroy_workqueue(workqueue);
  555. idr_destroy(&memstick_host_idr);
  556. }
  557. module_init(memstick_init);
  558. module_exit(memstick_exit);
  559. MODULE_AUTHOR("Alex Dubov");
  560. MODULE_LICENSE("GPL");
  561. MODULE_DESCRIPTION("Sony MemoryStick core driver");