2 * Sony MemoryStick support
4 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
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.
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
15 #include <linux/memstick.h>
16 #include <linux/idr.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
23 #define DRIVER_NAME "memstick"
25 static unsigned int cmd_retries
= 3;
26 module_param(cmd_retries
, uint
, 0644);
28 static struct workqueue_struct
*workqueue
;
29 static DEFINE_IDR(memstick_host_idr
);
30 static DEFINE_SPINLOCK(memstick_host_lock
);
32 static int memstick_dev_match(struct memstick_dev
*card
,
33 struct memstick_device_id
*id
)
35 if (id
->match_flags
& MEMSTICK_MATCH_ALL
) {
36 if ((id
->type
== card
->id
.type
)
37 && (id
->category
== card
->id
.category
)
38 && (id
->class == card
->id
.class))
45 static int memstick_bus_match(struct device
*dev
, struct device_driver
*drv
)
47 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
49 struct memstick_driver
*ms_drv
= container_of(drv
,
50 struct memstick_driver
,
52 struct memstick_device_id
*ids
= ms_drv
->id_table
;
55 while (ids
->match_flags
) {
56 if (memstick_dev_match(card
, ids
))
64 static int memstick_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
66 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
69 if (add_uevent_var(env
, "MEMSTICK_TYPE=%02X", card
->id
.type
))
72 if (add_uevent_var(env
, "MEMSTICK_CATEGORY=%02X", card
->id
.category
))
75 if (add_uevent_var(env
, "MEMSTICK_CLASS=%02X", card
->id
.class))
81 static int memstick_device_probe(struct device
*dev
)
83 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
85 struct memstick_driver
*drv
= container_of(dev
->driver
,
86 struct memstick_driver
,
90 if (dev
->driver
&& drv
->probe
) {
91 rc
= drv
->probe(card
);
98 static int memstick_device_remove(struct device
*dev
)
100 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
102 struct memstick_driver
*drv
= container_of(dev
->driver
,
103 struct memstick_driver
,
106 if (dev
->driver
&& drv
->remove
) {
108 card
->dev
.driver
= NULL
;
117 static int memstick_device_suspend(struct device
*dev
, pm_message_t state
)
119 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
121 struct memstick_driver
*drv
= container_of(dev
->driver
,
122 struct memstick_driver
,
125 if (dev
->driver
&& drv
->suspend
)
126 return drv
->suspend(card
, state
);
130 static int memstick_device_resume(struct device
*dev
)
132 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
134 struct memstick_driver
*drv
= container_of(dev
->driver
,
135 struct memstick_driver
,
138 if (dev
->driver
&& drv
->resume
)
139 return drv
->resume(card
);
145 #define memstick_device_suspend NULL
146 #define memstick_device_resume NULL
148 #endif /* CONFIG_PM */
150 #define MEMSTICK_ATTR(name, format) \
151 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
154 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
156 return sprintf(buf, format, card->id.name); \
158 static DEVICE_ATTR_RO(name);
160 MEMSTICK_ATTR(type
, "%02X");
161 MEMSTICK_ATTR(category
, "%02X");
162 MEMSTICK_ATTR(class, "%02X");
164 static struct attribute
*memstick_dev_attrs
[] = {
166 &dev_attr_category
.attr
,
167 &dev_attr_class
.attr
,
170 ATTRIBUTE_GROUPS(memstick_dev
);
172 static struct bus_type memstick_bus_type
= {
174 .dev_groups
= memstick_dev_groups
,
175 .match
= memstick_bus_match
,
176 .uevent
= memstick_uevent
,
177 .probe
= memstick_device_probe
,
178 .remove
= memstick_device_remove
,
179 .suspend
= memstick_device_suspend
,
180 .resume
= memstick_device_resume
183 static void memstick_free(struct device
*dev
)
185 struct memstick_host
*host
= container_of(dev
, struct memstick_host
,
190 static struct class memstick_host_class
= {
191 .name
= "memstick_host",
192 .dev_release
= memstick_free
195 static void memstick_free_card(struct device
*dev
)
197 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
202 static int memstick_dummy_check(struct memstick_dev
*card
)
208 * memstick_detect_change - schedule media detection on memstick host
209 * @host - host to use
211 void memstick_detect_change(struct memstick_host
*host
)
213 queue_work(workqueue
, &host
->media_checker
);
215 EXPORT_SYMBOL(memstick_detect_change
);
218 * memstick_next_req - called by host driver to obtain next request to process
219 * @host - host to use
220 * @mrq - pointer to stick the request to
222 * Host calls this function from idle state (*mrq == NULL) or after finishing
223 * previous request (*mrq should point to it). If previous request was
224 * unsuccessful, it is retried for predetermined number of times. Return value
225 * of 0 means that new request was assigned to the host.
227 int memstick_next_req(struct memstick_host
*host
, struct memstick_request
**mrq
)
231 if ((*mrq
) && (*mrq
)->error
&& host
->retries
) {
237 if (host
->card
&& host
->card
->next_request
)
238 rc
= host
->card
->next_request(host
->card
, mrq
);
241 host
->retries
= cmd_retries
> 1 ? cmd_retries
- 1 : 1;
247 EXPORT_SYMBOL(memstick_next_req
);
250 * memstick_new_req - notify the host that some requests are pending
251 * @host - host to use
253 void memstick_new_req(struct memstick_host
*host
)
256 host
->retries
= cmd_retries
;
257 reinit_completion(&host
->card
->mrq_complete
);
261 EXPORT_SYMBOL(memstick_new_req
);
264 * memstick_init_req_sg - set request fields needed for bulk data transfer
265 * @mrq - request to use
266 * @tpc - memstick Transport Protocol Command
269 void memstick_init_req_sg(struct memstick_request
*mrq
, unsigned char tpc
,
270 const struct scatterlist
*sg
)
274 mrq
->data_dir
= WRITE
;
276 mrq
->data_dir
= READ
;
281 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
282 mrq
->need_card_int
= 1;
284 mrq
->need_card_int
= 0;
286 EXPORT_SYMBOL(memstick_init_req_sg
);
289 * memstick_init_req - set request fields needed for short data transfer
290 * @mrq - request to use
291 * @tpc - memstick Transport Protocol Command
292 * @buf - TPC argument buffer
293 * @length - TPC argument size
295 * The intended use of this function (transfer of data items several bytes
296 * in size) allows us to just copy the value between request structure and
297 * user supplied buffer.
299 void memstick_init_req(struct memstick_request
*mrq
, unsigned char tpc
,
300 const void *buf
, size_t length
)
304 mrq
->data_dir
= WRITE
;
306 mrq
->data_dir
= READ
;
308 mrq
->data_len
= length
> sizeof(mrq
->data
) ? sizeof(mrq
->data
) : length
;
309 if (mrq
->data_dir
== WRITE
)
310 memcpy(mrq
->data
, buf
, mrq
->data_len
);
314 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
315 mrq
->need_card_int
= 1;
317 mrq
->need_card_int
= 0;
319 EXPORT_SYMBOL(memstick_init_req
);
322 * Functions prefixed with "h_" are protocol callbacks. They can be called from
323 * interrupt context. Return value of 0 means that request processing is still
324 * ongoing, while special error value of -EAGAIN means that current request is
325 * finished (and request processor should come back some time later).
328 static int h_memstick_read_dev_id(struct memstick_dev
*card
,
329 struct memstick_request
**mrq
)
331 struct ms_id_register id_reg
;
334 memstick_init_req(&card
->current_mrq
, MS_TPC_READ_REG
, &id_reg
,
335 sizeof(struct ms_id_register
));
336 *mrq
= &card
->current_mrq
;
339 if (!(*mrq
)->error
) {
340 memcpy(&id_reg
, (*mrq
)->data
, sizeof(id_reg
));
341 card
->id
.match_flags
= MEMSTICK_MATCH_ALL
;
342 card
->id
.type
= id_reg
.type
;
343 card
->id
.category
= id_reg
.category
;
344 card
->id
.class = id_reg
.class;
345 dev_dbg(&card
->dev
, "if_mode = %02x\n", id_reg
.if_mode
);
347 complete(&card
->mrq_complete
);
352 static int h_memstick_set_rw_addr(struct memstick_dev
*card
,
353 struct memstick_request
**mrq
)
356 memstick_init_req(&card
->current_mrq
, MS_TPC_SET_RW_REG_ADRS
,
357 (char *)&card
->reg_addr
,
358 sizeof(card
->reg_addr
));
359 *mrq
= &card
->current_mrq
;
362 complete(&card
->mrq_complete
);
368 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
370 * @card - media device to use
372 int memstick_set_rw_addr(struct memstick_dev
*card
)
374 card
->next_request
= h_memstick_set_rw_addr
;
375 memstick_new_req(card
->host
);
376 wait_for_completion(&card
->mrq_complete
);
378 return card
->current_mrq
.error
;
380 EXPORT_SYMBOL(memstick_set_rw_addr
);
382 static struct memstick_dev
*memstick_alloc_card(struct memstick_host
*host
)
384 struct memstick_dev
*card
= kzalloc(sizeof(struct memstick_dev
),
386 struct memstick_dev
*old_card
= host
->card
;
387 struct ms_id_register id_reg
;
391 dev_set_name(&card
->dev
, "%s", dev_name(&host
->dev
));
392 card
->dev
.parent
= &host
->dev
;
393 card
->dev
.bus
= &memstick_bus_type
;
394 card
->dev
.release
= memstick_free_card
;
395 card
->check
= memstick_dummy_check
;
397 card
->reg_addr
.r_offset
= offsetof(struct ms_register
, id
);
398 card
->reg_addr
.r_length
= sizeof(id_reg
);
399 card
->reg_addr
.w_offset
= offsetof(struct ms_register
, id
);
400 card
->reg_addr
.w_length
= sizeof(id_reg
);
402 init_completion(&card
->mrq_complete
);
405 if (memstick_set_rw_addr(card
))
408 card
->next_request
= h_memstick_read_dev_id
;
409 memstick_new_req(host
);
410 wait_for_completion(&card
->mrq_complete
);
412 if (card
->current_mrq
.error
)
415 host
->card
= old_card
;
418 host
->card
= old_card
;
423 static int memstick_power_on(struct memstick_host
*host
)
425 int rc
= host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_ON
);
428 rc
= host
->set_param(host
, MEMSTICK_INTERFACE
, MEMSTICK_SERIAL
);
433 static void memstick_check(struct work_struct
*work
)
435 struct memstick_host
*host
= container_of(work
, struct memstick_host
,
437 struct memstick_dev
*card
;
439 dev_dbg(&host
->dev
, "memstick_check started\n");
440 pm_runtime_get_noresume(host
->dev
.parent
);
441 mutex_lock(&host
->lock
);
443 if (memstick_power_on(host
))
445 } else if (host
->card
->stop
)
446 host
->card
->stop(host
->card
);
448 card
= memstick_alloc_card(host
);
452 device_unregister(&host
->card
->dev
);
456 dev_dbg(&host
->dev
, "new card %02x, %02x, %02x\n",
457 card
->id
.type
, card
->id
.category
, card
->id
.class);
459 if (memstick_set_rw_addr(host
->card
)
460 || !memstick_dev_match(host
->card
, &card
->id
)
461 || !(host
->card
->check(host
->card
))) {
462 device_unregister(&host
->card
->dev
);
464 } else if (host
->card
->start
)
465 host
->card
->start(host
->card
);
470 if (device_register(&card
->dev
)) {
471 put_device(&card
->dev
);
481 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
483 mutex_unlock(&host
->lock
);
484 pm_runtime_put(host
->dev
.parent
);
485 dev_dbg(&host
->dev
, "memstick_check finished\n");
489 * memstick_alloc_host - allocate a memstick_host structure
490 * @extra: size of the user private data to allocate
491 * @dev: parent device of the host
493 struct memstick_host
*memstick_alloc_host(unsigned int extra
,
496 struct memstick_host
*host
;
498 host
= kzalloc(sizeof(struct memstick_host
) + extra
, GFP_KERNEL
);
500 mutex_init(&host
->lock
);
501 INIT_WORK(&host
->media_checker
, memstick_check
);
502 host
->dev
.class = &memstick_host_class
;
503 host
->dev
.parent
= dev
;
504 device_initialize(&host
->dev
);
508 EXPORT_SYMBOL(memstick_alloc_host
);
511 * memstick_add_host - start request processing on memstick host
512 * @host - host to use
514 int memstick_add_host(struct memstick_host
*host
)
518 idr_preload(GFP_KERNEL
);
519 spin_lock(&memstick_host_lock
);
521 rc
= idr_alloc(&memstick_host_idr
, host
, 0, 0, GFP_NOWAIT
);
525 spin_unlock(&memstick_host_lock
);
530 dev_set_name(&host
->dev
, "memstick%u", host
->id
);
532 rc
= device_add(&host
->dev
);
534 spin_lock(&memstick_host_lock
);
535 idr_remove(&memstick_host_idr
, host
->id
);
536 spin_unlock(&memstick_host_lock
);
540 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
541 memstick_detect_change(host
);
544 EXPORT_SYMBOL(memstick_add_host
);
547 * memstick_remove_host - stop request processing on memstick host
548 * @host - host to use
550 void memstick_remove_host(struct memstick_host
*host
)
552 flush_workqueue(workqueue
);
553 mutex_lock(&host
->lock
);
555 device_unregister(&host
->card
->dev
);
557 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
558 mutex_unlock(&host
->lock
);
560 spin_lock(&memstick_host_lock
);
561 idr_remove(&memstick_host_idr
, host
->id
);
562 spin_unlock(&memstick_host_lock
);
563 device_del(&host
->dev
);
565 EXPORT_SYMBOL(memstick_remove_host
);
568 * memstick_free_host - free memstick host
569 * @host - host to use
571 void memstick_free_host(struct memstick_host
*host
)
573 mutex_destroy(&host
->lock
);
574 put_device(&host
->dev
);
576 EXPORT_SYMBOL(memstick_free_host
);
579 * memstick_suspend_host - notify bus driver of host suspension
580 * @host - host to use
582 void memstick_suspend_host(struct memstick_host
*host
)
584 mutex_lock(&host
->lock
);
585 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
586 mutex_unlock(&host
->lock
);
588 EXPORT_SYMBOL(memstick_suspend_host
);
591 * memstick_resume_host - notify bus driver of host resumption
592 * @host - host to use
594 void memstick_resume_host(struct memstick_host
*host
)
598 mutex_lock(&host
->lock
);
600 rc
= memstick_power_on(host
);
601 mutex_unlock(&host
->lock
);
604 memstick_detect_change(host
);
606 EXPORT_SYMBOL(memstick_resume_host
);
608 int memstick_register_driver(struct memstick_driver
*drv
)
610 drv
->driver
.bus
= &memstick_bus_type
;
612 return driver_register(&drv
->driver
);
614 EXPORT_SYMBOL(memstick_register_driver
);
616 void memstick_unregister_driver(struct memstick_driver
*drv
)
618 driver_unregister(&drv
->driver
);
620 EXPORT_SYMBOL(memstick_unregister_driver
);
623 static int __init
memstick_init(void)
627 workqueue
= create_freezable_workqueue("kmemstick");
631 rc
= bus_register(&memstick_bus_type
);
633 goto error_destroy_workqueue
;
635 rc
= class_register(&memstick_host_class
);
637 goto error_bus_unregister
;
641 error_bus_unregister
:
642 bus_unregister(&memstick_bus_type
);
643 error_destroy_workqueue
:
644 destroy_workqueue(workqueue
);
649 static void __exit
memstick_exit(void)
651 class_unregister(&memstick_host_class
);
652 bus_unregister(&memstick_bus_type
);
653 destroy_workqueue(workqueue
);
654 idr_destroy(&memstick_host_idr
);
657 module_init(memstick_init
);
658 module_exit(memstick_exit
);
660 MODULE_AUTHOR("Alex Dubov");
661 MODULE_LICENSE("GPL");
662 MODULE_DESCRIPTION("Sony MemoryStick core driver");