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