memstick.c 16 KB

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