Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / memstick / core / memstick.c
blob12bc3f5a6cbbd594c56013526b973659d0defe9a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Sony MemoryStick support
5 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
7 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
8 * that made this driver possible.
9 */
11 #include <linux/memstick.h>
12 #include <linux/idr.h>
13 #include <linux/fs.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
19 #define DRIVER_NAME "memstick"
21 static unsigned int cmd_retries = 3;
22 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);
28 static int memstick_dev_match(struct memstick_dev *card,
29 struct memstick_device_id *id)
31 if (id->match_flags & MEMSTICK_MATCH_ALL) {
32 if ((id->type == card->id.type)
33 && (id->category == card->id.category)
34 && (id->class == card->id.class))
35 return 1;
38 return 0;
41 static int memstick_bus_match(struct device *dev, struct device_driver *drv)
43 struct memstick_dev *card = container_of(dev, struct memstick_dev,
44 dev);
45 struct memstick_driver *ms_drv = container_of(drv,
46 struct memstick_driver,
47 driver);
48 struct memstick_device_id *ids = ms_drv->id_table;
50 if (ids) {
51 while (ids->match_flags) {
52 if (memstick_dev_match(card, ids))
53 return 1;
54 ++ids;
57 return 0;
60 static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
62 struct memstick_dev *card = container_of(dev, struct memstick_dev,
63 dev);
65 if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
66 return -ENOMEM;
68 if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
69 return -ENOMEM;
71 if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
72 return -ENOMEM;
74 return 0;
77 static int memstick_device_probe(struct device *dev)
79 struct memstick_dev *card = container_of(dev, struct memstick_dev,
80 dev);
81 struct memstick_driver *drv = container_of(dev->driver,
82 struct memstick_driver,
83 driver);
84 int rc = -ENODEV;
86 if (dev->driver && drv->probe) {
87 rc = drv->probe(card);
88 if (!rc)
89 get_device(dev);
91 return rc;
94 static int memstick_device_remove(struct device *dev)
96 struct memstick_dev *card = container_of(dev, struct memstick_dev,
97 dev);
98 struct memstick_driver *drv = container_of(dev->driver,
99 struct memstick_driver,
100 driver);
102 if (dev->driver && drv->remove) {
103 drv->remove(card);
104 card->dev.driver = NULL;
107 put_device(dev);
108 return 0;
111 #ifdef CONFIG_PM
113 static int memstick_device_suspend(struct device *dev, pm_message_t state)
115 struct memstick_dev *card = container_of(dev, struct memstick_dev,
116 dev);
117 struct memstick_driver *drv = container_of(dev->driver,
118 struct memstick_driver,
119 driver);
121 if (dev->driver && drv->suspend)
122 return drv->suspend(card, state);
123 return 0;
126 static int memstick_device_resume(struct device *dev)
128 struct memstick_dev *card = container_of(dev, struct memstick_dev,
129 dev);
130 struct memstick_driver *drv = container_of(dev->driver,
131 struct memstick_driver,
132 driver);
134 if (dev->driver && drv->resume)
135 return drv->resume(card);
136 return 0;
139 #else
141 #define memstick_device_suspend NULL
142 #define memstick_device_resume NULL
144 #endif /* CONFIG_PM */
146 #define MEMSTICK_ATTR(name, format) \
147 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
148 char *buf) \
150 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
151 dev); \
152 return sprintf(buf, format, card->id.name); \
154 static DEVICE_ATTR_RO(name);
156 MEMSTICK_ATTR(type, "%02X");
157 MEMSTICK_ATTR(category, "%02X");
158 MEMSTICK_ATTR(class, "%02X");
160 static struct attribute *memstick_dev_attrs[] = {
161 &dev_attr_type.attr,
162 &dev_attr_category.attr,
163 &dev_attr_class.attr,
164 NULL,
166 ATTRIBUTE_GROUPS(memstick_dev);
168 static struct bus_type memstick_bus_type = {
169 .name = "memstick",
170 .dev_groups = memstick_dev_groups,
171 .match = memstick_bus_match,
172 .uevent = memstick_uevent,
173 .probe = memstick_device_probe,
174 .remove = memstick_device_remove,
175 .suspend = memstick_device_suspend,
176 .resume = memstick_device_resume
179 static void memstick_free(struct device *dev)
181 struct memstick_host *host = container_of(dev, struct memstick_host,
182 dev);
183 kfree(host);
186 static struct class memstick_host_class = {
187 .name = "memstick_host",
188 .dev_release = memstick_free
191 static void memstick_free_card(struct device *dev)
193 struct memstick_dev *card = container_of(dev, struct memstick_dev,
194 dev);
195 kfree(card);
198 static int memstick_dummy_check(struct memstick_dev *card)
200 return 0;
204 * memstick_detect_change - schedule media detection on memstick host
205 * @host - host to use
207 void memstick_detect_change(struct memstick_host *host)
209 queue_work(workqueue, &host->media_checker);
211 EXPORT_SYMBOL(memstick_detect_change);
214 * memstick_next_req - called by host driver to obtain next request to process
215 * @host - host to use
216 * @mrq - pointer to stick the request to
218 * Host calls this function from idle state (*mrq == NULL) or after finishing
219 * previous request (*mrq should point to it). If previous request was
220 * unsuccessful, it is retried for predetermined number of times. Return value
221 * of 0 means that new request was assigned to the host.
223 int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
225 int rc = -ENXIO;
227 if ((*mrq) && (*mrq)->error && host->retries) {
228 (*mrq)->error = rc;
229 host->retries--;
230 return 0;
233 if (host->card && host->card->next_request)
234 rc = host->card->next_request(host->card, mrq);
236 if (!rc)
237 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
238 else
239 *mrq = NULL;
241 return rc;
243 EXPORT_SYMBOL(memstick_next_req);
246 * memstick_new_req - notify the host that some requests are pending
247 * @host - host to use
249 void memstick_new_req(struct memstick_host *host)
251 if (host->card) {
252 host->retries = cmd_retries;
253 reinit_completion(&host->card->mrq_complete);
254 host->request(host);
257 EXPORT_SYMBOL(memstick_new_req);
260 * memstick_init_req_sg - set request fields needed for bulk data transfer
261 * @mrq - request to use
262 * @tpc - memstick Transport Protocol Command
263 * @sg - TPC argument
265 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
266 const struct scatterlist *sg)
268 mrq->tpc = tpc;
269 if (tpc & 8)
270 mrq->data_dir = WRITE;
271 else
272 mrq->data_dir = READ;
274 mrq->sg = *sg;
275 mrq->long_data = 1;
277 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
278 mrq->need_card_int = 1;
279 else
280 mrq->need_card_int = 0;
282 EXPORT_SYMBOL(memstick_init_req_sg);
285 * memstick_init_req - set request fields needed for short data transfer
286 * @mrq - request to use
287 * @tpc - memstick Transport Protocol Command
288 * @buf - TPC argument buffer
289 * @length - TPC argument size
291 * The intended use of this function (transfer of data items several bytes
292 * in size) allows us to just copy the value between request structure and
293 * user supplied buffer.
295 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
296 const void *buf, size_t length)
298 mrq->tpc = tpc;
299 if (tpc & 8)
300 mrq->data_dir = WRITE;
301 else
302 mrq->data_dir = READ;
304 mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
305 if (mrq->data_dir == WRITE)
306 memcpy(mrq->data, buf, mrq->data_len);
308 mrq->long_data = 0;
310 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
311 mrq->need_card_int = 1;
312 else
313 mrq->need_card_int = 0;
315 EXPORT_SYMBOL(memstick_init_req);
318 * Functions prefixed with "h_" are protocol callbacks. They can be called from
319 * interrupt context. Return value of 0 means that request processing is still
320 * ongoing, while special error value of -EAGAIN means that current request is
321 * finished (and request processor should come back some time later).
324 static int h_memstick_read_dev_id(struct memstick_dev *card,
325 struct memstick_request **mrq)
327 struct ms_id_register id_reg;
329 if (!(*mrq)) {
330 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg,
331 sizeof(struct ms_id_register));
332 *mrq = &card->current_mrq;
333 return 0;
334 } else {
335 if (!(*mrq)->error) {
336 memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
337 card->id.match_flags = MEMSTICK_MATCH_ALL;
338 card->id.type = id_reg.type;
339 card->id.category = id_reg.category;
340 card->id.class = id_reg.class;
341 dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
343 complete(&card->mrq_complete);
344 return -EAGAIN;
348 static int h_memstick_set_rw_addr(struct memstick_dev *card,
349 struct memstick_request **mrq)
351 if (!(*mrq)) {
352 memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
353 (char *)&card->reg_addr,
354 sizeof(card->reg_addr));
355 *mrq = &card->current_mrq;
356 return 0;
357 } else {
358 complete(&card->mrq_complete);
359 return -EAGAIN;
364 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
365 * complete
366 * @card - media device to use
368 int memstick_set_rw_addr(struct memstick_dev *card)
370 card->next_request = h_memstick_set_rw_addr;
371 memstick_new_req(card->host);
372 wait_for_completion(&card->mrq_complete);
374 return card->current_mrq.error;
376 EXPORT_SYMBOL(memstick_set_rw_addr);
378 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
380 struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
381 GFP_KERNEL);
382 struct memstick_dev *old_card = host->card;
383 struct ms_id_register id_reg;
385 if (card) {
386 card->host = host;
387 dev_set_name(&card->dev, "%s", dev_name(&host->dev));
388 card->dev.parent = &host->dev;
389 card->dev.bus = &memstick_bus_type;
390 card->dev.release = memstick_free_card;
391 card->check = memstick_dummy_check;
393 card->reg_addr.r_offset = offsetof(struct ms_register, id);
394 card->reg_addr.r_length = sizeof(id_reg);
395 card->reg_addr.w_offset = offsetof(struct ms_register, id);
396 card->reg_addr.w_length = sizeof(id_reg);
398 init_completion(&card->mrq_complete);
400 host->card = card;
401 if (memstick_set_rw_addr(card))
402 goto err_out;
404 card->next_request = h_memstick_read_dev_id;
405 memstick_new_req(host);
406 wait_for_completion(&card->mrq_complete);
408 if (card->current_mrq.error)
409 goto err_out;
411 host->card = old_card;
412 return card;
413 err_out:
414 host->card = old_card;
415 kfree(card);
416 return NULL;
419 static int memstick_power_on(struct memstick_host *host)
421 int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
423 if (!rc)
424 rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
426 return rc;
429 static void memstick_check(struct work_struct *work)
431 struct memstick_host *host = container_of(work, struct memstick_host,
432 media_checker);
433 struct memstick_dev *card;
435 dev_dbg(&host->dev, "memstick_check started\n");
436 pm_runtime_get_noresume(host->dev.parent);
437 mutex_lock(&host->lock);
438 if (!host->card) {
439 if (memstick_power_on(host))
440 goto out_power_off;
441 } else if (host->card->stop)
442 host->card->stop(host->card);
444 if (host->removing)
445 goto out_power_off;
447 card = memstick_alloc_card(host);
449 if (!card) {
450 if (host->card) {
451 device_unregister(&host->card->dev);
452 host->card = NULL;
454 } else {
455 dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
456 card->id.type, card->id.category, card->id.class);
457 if (host->card) {
458 if (memstick_set_rw_addr(host->card)
459 || !memstick_dev_match(host->card, &card->id)
460 || !(host->card->check(host->card))) {
461 device_unregister(&host->card->dev);
462 host->card = NULL;
463 } else if (host->card->start)
464 host->card->start(host->card);
467 if (!host->card) {
468 host->card = card;
469 if (device_register(&card->dev)) {
470 put_device(&card->dev);
471 host->card = NULL;
473 } else
474 kfree(card);
477 out_power_off:
478 if (!host->card)
479 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
481 mutex_unlock(&host->lock);
482 pm_runtime_put(host->dev.parent);
483 dev_dbg(&host->dev, "memstick_check finished\n");
487 * memstick_alloc_host - allocate a memstick_host structure
488 * @extra: size of the user private data to allocate
489 * @dev: parent device of the host
491 struct memstick_host *memstick_alloc_host(unsigned int extra,
492 struct device *dev)
494 struct memstick_host *host;
496 host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
497 if (host) {
498 mutex_init(&host->lock);
499 INIT_WORK(&host->media_checker, memstick_check);
500 host->dev.class = &memstick_host_class;
501 host->dev.parent = dev;
502 device_initialize(&host->dev);
504 return host;
506 EXPORT_SYMBOL(memstick_alloc_host);
509 * memstick_add_host - start request processing on memstick host
510 * @host - host to use
512 int memstick_add_host(struct memstick_host *host)
514 int rc;
516 idr_preload(GFP_KERNEL);
517 spin_lock(&memstick_host_lock);
519 rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
520 if (rc >= 0)
521 host->id = rc;
523 spin_unlock(&memstick_host_lock);
524 idr_preload_end();
525 if (rc < 0)
526 return rc;
528 dev_set_name(&host->dev, "memstick%u", host->id);
530 rc = device_add(&host->dev);
531 if (rc) {
532 spin_lock(&memstick_host_lock);
533 idr_remove(&memstick_host_idr, host->id);
534 spin_unlock(&memstick_host_lock);
535 return rc;
538 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
539 memstick_detect_change(host);
540 return 0;
542 EXPORT_SYMBOL(memstick_add_host);
545 * memstick_remove_host - stop request processing on memstick host
546 * @host - host to use
548 void memstick_remove_host(struct memstick_host *host)
550 host->removing = 1;
551 flush_workqueue(workqueue);
552 mutex_lock(&host->lock);
553 if (host->card)
554 device_unregister(&host->card->dev);
555 host->card = NULL;
556 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
557 mutex_unlock(&host->lock);
559 spin_lock(&memstick_host_lock);
560 idr_remove(&memstick_host_idr, host->id);
561 spin_unlock(&memstick_host_lock);
562 device_del(&host->dev);
564 EXPORT_SYMBOL(memstick_remove_host);
567 * memstick_free_host - free memstick host
568 * @host - host to use
570 void memstick_free_host(struct memstick_host *host)
572 mutex_destroy(&host->lock);
573 put_device(&host->dev);
575 EXPORT_SYMBOL(memstick_free_host);
578 * memstick_suspend_host - notify bus driver of host suspension
579 * @host - host to use
581 void memstick_suspend_host(struct memstick_host *host)
583 mutex_lock(&host->lock);
584 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
585 mutex_unlock(&host->lock);
587 EXPORT_SYMBOL(memstick_suspend_host);
590 * memstick_resume_host - notify bus driver of host resumption
591 * @host - host to use
593 void memstick_resume_host(struct memstick_host *host)
595 int rc = 0;
597 mutex_lock(&host->lock);
598 if (host->card)
599 rc = memstick_power_on(host);
600 mutex_unlock(&host->lock);
602 if (!rc)
603 memstick_detect_change(host);
605 EXPORT_SYMBOL(memstick_resume_host);
607 int memstick_register_driver(struct memstick_driver *drv)
609 drv->driver.bus = &memstick_bus_type;
611 return driver_register(&drv->driver);
613 EXPORT_SYMBOL(memstick_register_driver);
615 void memstick_unregister_driver(struct memstick_driver *drv)
617 driver_unregister(&drv->driver);
619 EXPORT_SYMBOL(memstick_unregister_driver);
622 static int __init memstick_init(void)
624 int rc;
626 workqueue = create_freezable_workqueue("kmemstick");
627 if (!workqueue)
628 return -ENOMEM;
630 rc = bus_register(&memstick_bus_type);
631 if (rc)
632 goto error_destroy_workqueue;
634 rc = class_register(&memstick_host_class);
635 if (rc)
636 goto error_bus_unregister;
638 return 0;
640 error_bus_unregister:
641 bus_unregister(&memstick_bus_type);
642 error_destroy_workqueue:
643 destroy_workqueue(workqueue);
645 return rc;
648 static void __exit memstick_exit(void)
650 class_unregister(&memstick_host_class);
651 bus_unregister(&memstick_bus_type);
652 destroy_workqueue(workqueue);
653 idr_destroy(&memstick_host_idr);
656 module_init(memstick_init);
657 module_exit(memstick_exit);
659 MODULE_AUTHOR("Alex Dubov");
660 MODULE_LICENSE("GPL");
661 MODULE_DESCRIPTION("Sony MemoryStick core driver");