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>
21 #define DRIVER_NAME "memstick"
23 static unsigned int cmd_retries
= 3;
24 module_param(cmd_retries
, uint
, 0644);
26 static struct workqueue_struct
*workqueue
;
27 static DEFINE_IDR(memstick_host_idr
);
28 static DEFINE_SPINLOCK(memstick_host_lock
);
30 static int memstick_dev_match(struct memstick_dev
*card
,
31 struct memstick_device_id
*id
)
33 if (id
->match_flags
& MEMSTICK_MATCH_ALL
) {
34 if ((id
->type
== card
->id
.type
)
35 && (id
->category
== card
->id
.category
)
36 && (id
->class == card
->id
.class))
43 static int memstick_bus_match(struct device
*dev
, struct device_driver
*drv
)
45 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
47 struct memstick_driver
*ms_drv
= container_of(drv
,
48 struct memstick_driver
,
50 struct memstick_device_id
*ids
= ms_drv
->id_table
;
53 while (ids
->match_flags
) {
54 if (memstick_dev_match(card
, ids
))
62 static int memstick_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
64 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
67 if (add_uevent_var(env
, "MEMSTICK_TYPE=%02X", card
->id
.type
))
70 if (add_uevent_var(env
, "MEMSTICK_CATEGORY=%02X", card
->id
.category
))
73 if (add_uevent_var(env
, "MEMSTICK_CLASS=%02X", card
->id
.class))
79 static int memstick_device_probe(struct device
*dev
)
81 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
83 struct memstick_driver
*drv
= container_of(dev
->driver
,
84 struct memstick_driver
,
88 if (dev
->driver
&& drv
->probe
) {
89 rc
= drv
->probe(card
);
96 static int memstick_device_remove(struct device
*dev
)
98 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
100 struct memstick_driver
*drv
= container_of(dev
->driver
,
101 struct memstick_driver
,
104 if (dev
->driver
&& drv
->remove
) {
106 card
->dev
.driver
= NULL
;
115 static int memstick_device_suspend(struct device
*dev
, pm_message_t state
)
117 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
119 struct memstick_driver
*drv
= container_of(dev
->driver
,
120 struct memstick_driver
,
123 if (dev
->driver
&& drv
->suspend
)
124 return drv
->suspend(card
, state
);
128 static int memstick_device_resume(struct device
*dev
)
130 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
132 struct memstick_driver
*drv
= container_of(dev
->driver
,
133 struct memstick_driver
,
136 if (dev
->driver
&& drv
->resume
)
137 return drv
->resume(card
);
143 #define memstick_device_suspend NULL
144 #define memstick_device_resume NULL
146 #endif /* CONFIG_PM */
148 #define MEMSTICK_ATTR(name, format) \
149 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
152 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
154 return sprintf(buf, format, card->id.name); \
157 MEMSTICK_ATTR(type
, "%02X");
158 MEMSTICK_ATTR(category
, "%02X");
159 MEMSTICK_ATTR(class, "%02X");
161 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
163 static struct device_attribute memstick_dev_attrs
[] = {
164 MEMSTICK_ATTR_RO(type
),
165 MEMSTICK_ATTR_RO(category
),
166 MEMSTICK_ATTR_RO(class),
170 static struct bus_type memstick_bus_type
= {
172 .dev_attrs
= memstick_dev_attrs
,
173 .match
= memstick_bus_match
,
174 .uevent
= memstick_uevent
,
175 .probe
= memstick_device_probe
,
176 .remove
= memstick_device_remove
,
177 .suspend
= memstick_device_suspend
,
178 .resume
= memstick_device_resume
181 static void memstick_free(struct device
*dev
)
183 struct memstick_host
*host
= container_of(dev
, struct memstick_host
,
188 static struct class memstick_host_class
= {
189 .name
= "memstick_host",
190 .dev_release
= memstick_free
193 static void memstick_free_card(struct device
*dev
)
195 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
200 static int memstick_dummy_check(struct memstick_dev
*card
)
206 * memstick_detect_change - schedule media detection on memstick host
207 * @host - host to use
209 void memstick_detect_change(struct memstick_host
*host
)
211 queue_work(workqueue
, &host
->media_checker
);
213 EXPORT_SYMBOL(memstick_detect_change
);
216 * memstick_next_req - called by host driver to obtain next request to process
217 * @host - host to use
218 * @mrq - pointer to stick the request to
220 * Host calls this function from idle state (*mrq == NULL) or after finishing
221 * previous request (*mrq should point to it). If previous request was
222 * unsuccessful, it is retried for predetermined number of times. Return value
223 * of 0 means that new request was assigned to the host.
225 int memstick_next_req(struct memstick_host
*host
, struct memstick_request
**mrq
)
229 if ((*mrq
) && (*mrq
)->error
&& host
->retries
) {
235 if (host
->card
&& host
->card
->next_request
)
236 rc
= host
->card
->next_request(host
->card
, mrq
);
239 host
->retries
= cmd_retries
> 1 ? cmd_retries
- 1 : 1;
245 EXPORT_SYMBOL(memstick_next_req
);
248 * memstick_new_req - notify the host that some requests are pending
249 * @host - host to use
251 void memstick_new_req(struct memstick_host
*host
)
254 host
->retries
= cmd_retries
;
255 INIT_COMPLETION(host
->card
->mrq_complete
);
259 EXPORT_SYMBOL(memstick_new_req
);
262 * memstick_init_req_sg - set request fields needed for bulk data transfer
263 * @mrq - request to use
264 * @tpc - memstick Transport Protocol Command
267 void memstick_init_req_sg(struct memstick_request
*mrq
, unsigned char tpc
,
268 const struct scatterlist
*sg
)
272 mrq
->data_dir
= WRITE
;
274 mrq
->data_dir
= READ
;
279 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
280 mrq
->need_card_int
= 1;
282 mrq
->need_card_int
= 0;
284 EXPORT_SYMBOL(memstick_init_req_sg
);
287 * memstick_init_req - set request fields needed for short data transfer
288 * @mrq - request to use
289 * @tpc - memstick Transport Protocol Command
290 * @buf - TPC argument buffer
291 * @length - TPC argument size
293 * The intended use of this function (transfer of data items several bytes
294 * in size) allows us to just copy the value between request structure and
295 * user supplied buffer.
297 void memstick_init_req(struct memstick_request
*mrq
, unsigned char tpc
,
298 const void *buf
, size_t length
)
302 mrq
->data_dir
= WRITE
;
304 mrq
->data_dir
= READ
;
306 mrq
->data_len
= length
> sizeof(mrq
->data
) ? sizeof(mrq
->data
) : length
;
307 if (mrq
->data_dir
== WRITE
)
308 memcpy(mrq
->data
, buf
, mrq
->data_len
);
312 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
313 mrq
->need_card_int
= 1;
315 mrq
->need_card_int
= 0;
317 EXPORT_SYMBOL(memstick_init_req
);
320 * Functions prefixed with "h_" are protocol callbacks. They can be called from
321 * interrupt context. Return value of 0 means that request processing is still
322 * ongoing, while special error value of -EAGAIN means that current request is
323 * finished (and request processor should come back some time later).
326 static int h_memstick_read_dev_id(struct memstick_dev
*card
,
327 struct memstick_request
**mrq
)
329 struct ms_id_register id_reg
;
332 memstick_init_req(&card
->current_mrq
, MS_TPC_READ_REG
, NULL
,
333 sizeof(struct ms_id_register
));
334 *mrq
= &card
->current_mrq
;
337 if (!(*mrq
)->error
) {
338 memcpy(&id_reg
, (*mrq
)->data
, sizeof(id_reg
));
339 card
->id
.match_flags
= MEMSTICK_MATCH_ALL
;
340 card
->id
.type
= id_reg
.type
;
341 card
->id
.category
= id_reg
.category
;
342 card
->id
.class = id_reg
.class;
343 dev_dbg(&card
->dev
, "if_mode = %02x\n", id_reg
.if_mode
);
345 complete(&card
->mrq_complete
);
350 static int h_memstick_set_rw_addr(struct memstick_dev
*card
,
351 struct memstick_request
**mrq
)
354 memstick_init_req(&card
->current_mrq
, MS_TPC_SET_RW_REG_ADRS
,
355 (char *)&card
->reg_addr
,
356 sizeof(card
->reg_addr
));
357 *mrq
= &card
->current_mrq
;
360 complete(&card
->mrq_complete
);
366 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
368 * @card - media device to use
370 int memstick_set_rw_addr(struct memstick_dev
*card
)
372 card
->next_request
= h_memstick_set_rw_addr
;
373 memstick_new_req(card
->host
);
374 wait_for_completion(&card
->mrq_complete
);
376 return card
->current_mrq
.error
;
378 EXPORT_SYMBOL(memstick_set_rw_addr
);
380 static struct memstick_dev
*memstick_alloc_card(struct memstick_host
*host
)
382 struct memstick_dev
*card
= kzalloc(sizeof(struct memstick_dev
),
384 struct memstick_dev
*old_card
= host
->card
;
385 struct ms_id_register id_reg
;
389 dev_set_name(&card
->dev
, "%s", dev_name(&host
->dev
));
390 card
->dev
.parent
= &host
->dev
;
391 card
->dev
.bus
= &memstick_bus_type
;
392 card
->dev
.release
= memstick_free_card
;
393 card
->check
= memstick_dummy_check
;
395 card
->reg_addr
.r_offset
= offsetof(struct ms_register
, id
);
396 card
->reg_addr
.r_length
= sizeof(id_reg
);
397 card
->reg_addr
.w_offset
= offsetof(struct ms_register
, id
);
398 card
->reg_addr
.w_length
= sizeof(id_reg
);
400 init_completion(&card
->mrq_complete
);
403 if (memstick_set_rw_addr(card
))
406 card
->next_request
= h_memstick_read_dev_id
;
407 memstick_new_req(host
);
408 wait_for_completion(&card
->mrq_complete
);
410 if (card
->current_mrq
.error
)
413 host
->card
= old_card
;
416 host
->card
= old_card
;
421 static int memstick_power_on(struct memstick_host
*host
)
423 int rc
= host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_ON
);
426 rc
= host
->set_param(host
, MEMSTICK_INTERFACE
, MEMSTICK_SERIAL
);
431 static void memstick_check(struct work_struct
*work
)
433 struct memstick_host
*host
= container_of(work
, struct memstick_host
,
435 struct memstick_dev
*card
;
437 dev_dbg(&host
->dev
, "memstick_check started\n");
438 mutex_lock(&host
->lock
);
440 if (memstick_power_on(host
))
442 } else if (host
->card
->stop
)
443 host
->card
->stop(host
->card
);
445 card
= memstick_alloc_card(host
);
449 device_unregister(&host
->card
->dev
);
453 dev_dbg(&host
->dev
, "new card %02x, %02x, %02x\n",
454 card
->id
.type
, card
->id
.category
, card
->id
.class);
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
);
461 } else if (host
->card
->start
)
462 host
->card
->start(host
->card
);
467 if (device_register(&card
->dev
)) {
468 put_device(&card
->dev
);
478 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
480 mutex_unlock(&host
->lock
);
481 dev_dbg(&host
->dev
, "memstick_check finished\n");
485 * memstick_alloc_host - allocate a memstick_host structure
486 * @extra: size of the user private data to allocate
487 * @dev: parent device of the host
489 struct memstick_host
*memstick_alloc_host(unsigned int extra
,
492 struct memstick_host
*host
;
494 host
= kzalloc(sizeof(struct memstick_host
) + extra
, GFP_KERNEL
);
496 mutex_init(&host
->lock
);
497 INIT_WORK(&host
->media_checker
, memstick_check
);
498 host
->dev
.class = &memstick_host_class
;
499 host
->dev
.parent
= dev
;
500 device_initialize(&host
->dev
);
504 EXPORT_SYMBOL(memstick_alloc_host
);
507 * memstick_add_host - start request processing on memstick host
508 * @host - host to use
510 int memstick_add_host(struct memstick_host
*host
)
515 if (!idr_pre_get(&memstick_host_idr
, GFP_KERNEL
))
518 spin_lock(&memstick_host_lock
);
519 rc
= idr_get_new(&memstick_host_idr
, host
, &host
->id
);
520 spin_unlock(&memstick_host_lock
);
523 else if (rc
!= -EAGAIN
)
527 dev_set_name(&host
->dev
, "memstick%u", host
->id
);
529 rc
= device_add(&host
->dev
);
531 spin_lock(&memstick_host_lock
);
532 idr_remove(&memstick_host_idr
, host
->id
);
533 spin_unlock(&memstick_host_lock
);
537 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
538 memstick_detect_change(host
);
541 EXPORT_SYMBOL(memstick_add_host
);
544 * memstick_remove_host - stop request processing on memstick host
545 * @host - host to use
547 void memstick_remove_host(struct memstick_host
*host
)
549 flush_workqueue(workqueue
);
550 mutex_lock(&host
->lock
);
552 device_unregister(&host
->card
->dev
);
554 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
555 mutex_unlock(&host
->lock
);
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
);
562 EXPORT_SYMBOL(memstick_remove_host
);
565 * memstick_free_host - free memstick host
566 * @host - host to use
568 void memstick_free_host(struct memstick_host
*host
)
570 mutex_destroy(&host
->lock
);
571 put_device(&host
->dev
);
573 EXPORT_SYMBOL(memstick_free_host
);
576 * memstick_suspend_host - notify bus driver of host suspension
577 * @host - host to use
579 void memstick_suspend_host(struct memstick_host
*host
)
581 mutex_lock(&host
->lock
);
582 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
583 mutex_unlock(&host
->lock
);
585 EXPORT_SYMBOL(memstick_suspend_host
);
588 * memstick_resume_host - notify bus driver of host resumption
589 * @host - host to use
591 void memstick_resume_host(struct memstick_host
*host
)
595 mutex_lock(&host
->lock
);
597 rc
= memstick_power_on(host
);
598 mutex_unlock(&host
->lock
);
601 memstick_detect_change(host
);
603 EXPORT_SYMBOL(memstick_resume_host
);
605 int memstick_register_driver(struct memstick_driver
*drv
)
607 drv
->driver
.bus
= &memstick_bus_type
;
609 return driver_register(&drv
->driver
);
611 EXPORT_SYMBOL(memstick_register_driver
);
613 void memstick_unregister_driver(struct memstick_driver
*drv
)
615 driver_unregister(&drv
->driver
);
617 EXPORT_SYMBOL(memstick_unregister_driver
);
620 static int __init
memstick_init(void)
624 workqueue
= create_freezable_workqueue("kmemstick");
628 rc
= bus_register(&memstick_bus_type
);
630 rc
= class_register(&memstick_host_class
);
635 bus_unregister(&memstick_bus_type
);
636 destroy_workqueue(workqueue
);
641 static void __exit
memstick_exit(void)
643 class_unregister(&memstick_host_class
);
644 bus_unregister(&memstick_bus_type
);
645 destroy_workqueue(workqueue
);
646 idr_destroy(&memstick_host_idr
);
649 module_init(memstick_init
);
650 module_exit(memstick_exit
);
652 MODULE_AUTHOR("Alex Dubov");
653 MODULE_LICENSE("GPL");
654 MODULE_DESCRIPTION("Sony MemoryStick core driver");