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>
22 #define DRIVER_NAME "memstick"
24 static unsigned int cmd_retries
= 3;
25 module_param(cmd_retries
, uint
, 0644);
27 static struct workqueue_struct
*workqueue
;
28 static DEFINE_IDR(memstick_host_idr
);
29 static DEFINE_SPINLOCK(memstick_host_lock
);
31 static int memstick_dev_match(struct memstick_dev
*card
,
32 struct memstick_device_id
*id
)
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))
44 static int memstick_bus_match(struct device
*dev
, struct device_driver
*drv
)
46 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
48 struct memstick_driver
*ms_drv
= container_of(drv
,
49 struct memstick_driver
,
51 struct memstick_device_id
*ids
= ms_drv
->id_table
;
54 while (ids
->match_flags
) {
55 if (memstick_dev_match(card
, ids
))
63 static int memstick_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
65 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
68 if (add_uevent_var(env
, "MEMSTICK_TYPE=%02X", card
->id
.type
))
71 if (add_uevent_var(env
, "MEMSTICK_CATEGORY=%02X", card
->id
.category
))
74 if (add_uevent_var(env
, "MEMSTICK_CLASS=%02X", card
->id
.class))
80 static int memstick_device_probe(struct device
*dev
)
82 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
84 struct memstick_driver
*drv
= container_of(dev
->driver
,
85 struct memstick_driver
,
89 if (dev
->driver
&& drv
->probe
) {
90 rc
= drv
->probe(card
);
97 static int memstick_device_remove(struct device
*dev
)
99 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
101 struct memstick_driver
*drv
= container_of(dev
->driver
,
102 struct memstick_driver
,
105 if (dev
->driver
&& drv
->remove
) {
107 card
->dev
.driver
= NULL
;
116 static int memstick_device_suspend(struct device
*dev
, pm_message_t state
)
118 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
120 struct memstick_driver
*drv
= container_of(dev
->driver
,
121 struct memstick_driver
,
124 if (dev
->driver
&& drv
->suspend
)
125 return drv
->suspend(card
, state
);
129 static int memstick_device_resume(struct device
*dev
)
131 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
133 struct memstick_driver
*drv
= container_of(dev
->driver
,
134 struct memstick_driver
,
137 if (dev
->driver
&& drv
->resume
)
138 return drv
->resume(card
);
144 #define memstick_device_suspend NULL
145 #define memstick_device_resume NULL
147 #endif /* CONFIG_PM */
149 #define MEMSTICK_ATTR(name, format) \
150 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
153 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
155 return sprintf(buf, format, card->id.name); \
158 MEMSTICK_ATTR(type
, "%02X");
159 MEMSTICK_ATTR(category
, "%02X");
160 MEMSTICK_ATTR(class, "%02X");
162 #define MEMSTICK_ATTR_RO(name) __ATTR(name, S_IRUGO, name##_show, NULL)
164 static struct device_attribute memstick_dev_attrs
[] = {
165 MEMSTICK_ATTR_RO(type
),
166 MEMSTICK_ATTR_RO(category
),
167 MEMSTICK_ATTR_RO(class),
171 static struct bus_type memstick_bus_type
= {
173 .dev_attrs
= memstick_dev_attrs
,
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
182 static void memstick_free(struct device
*dev
)
184 struct memstick_host
*host
= container_of(dev
, struct memstick_host
,
189 static struct class memstick_host_class
= {
190 .name
= "memstick_host",
191 .dev_release
= memstick_free
194 static void memstick_free_card(struct device
*dev
)
196 struct memstick_dev
*card
= container_of(dev
, struct memstick_dev
,
201 static int memstick_dummy_check(struct memstick_dev
*card
)
207 * memstick_detect_change - schedule media detection on memstick host
208 * @host - host to use
210 void memstick_detect_change(struct memstick_host
*host
)
212 queue_work(workqueue
, &host
->media_checker
);
214 EXPORT_SYMBOL(memstick_detect_change
);
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
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.
226 int memstick_next_req(struct memstick_host
*host
, struct memstick_request
**mrq
)
230 if ((*mrq
) && (*mrq
)->error
&& host
->retries
) {
236 if (host
->card
&& host
->card
->next_request
)
237 rc
= host
->card
->next_request(host
->card
, mrq
);
240 host
->retries
= cmd_retries
> 1 ? cmd_retries
- 1 : 1;
246 EXPORT_SYMBOL(memstick_next_req
);
249 * memstick_new_req - notify the host that some requests are pending
250 * @host - host to use
252 void memstick_new_req(struct memstick_host
*host
)
255 host
->retries
= cmd_retries
;
256 INIT_COMPLETION(host
->card
->mrq_complete
);
260 EXPORT_SYMBOL(memstick_new_req
);
263 * memstick_init_req_sg - set request fields needed for bulk data transfer
264 * @mrq - request to use
265 * @tpc - memstick Transport Protocol Command
268 void memstick_init_req_sg(struct memstick_request
*mrq
, unsigned char tpc
,
269 const struct scatterlist
*sg
)
273 mrq
->data_dir
= WRITE
;
275 mrq
->data_dir
= READ
;
280 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
281 mrq
->need_card_int
= 1;
283 mrq
->need_card_int
= 0;
285 EXPORT_SYMBOL(memstick_init_req_sg
);
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
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.
298 void memstick_init_req(struct memstick_request
*mrq
, unsigned char tpc
,
299 const void *buf
, size_t length
)
303 mrq
->data_dir
= WRITE
;
305 mrq
->data_dir
= READ
;
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
);
313 if (tpc
== MS_TPC_SET_CMD
|| tpc
== MS_TPC_EX_SET_CMD
)
314 mrq
->need_card_int
= 1;
316 mrq
->need_card_int
= 0;
318 EXPORT_SYMBOL(memstick_init_req
);
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).
327 static int h_memstick_read_dev_id(struct memstick_dev
*card
,
328 struct memstick_request
**mrq
)
330 struct ms_id_register id_reg
;
333 memstick_init_req(&card
->current_mrq
, MS_TPC_READ_REG
, NULL
,
334 sizeof(struct ms_id_register
));
335 *mrq
= &card
->current_mrq
;
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
);
346 complete(&card
->mrq_complete
);
351 static int h_memstick_set_rw_addr(struct memstick_dev
*card
,
352 struct memstick_request
**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
;
361 complete(&card
->mrq_complete
);
367 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
369 * @card - media device to use
371 int memstick_set_rw_addr(struct memstick_dev
*card
)
373 card
->next_request
= h_memstick_set_rw_addr
;
374 memstick_new_req(card
->host
);
375 wait_for_completion(&card
->mrq_complete
);
377 return card
->current_mrq
.error
;
379 EXPORT_SYMBOL(memstick_set_rw_addr
);
381 static struct memstick_dev
*memstick_alloc_card(struct memstick_host
*host
)
383 struct memstick_dev
*card
= kzalloc(sizeof(struct memstick_dev
),
385 struct memstick_dev
*old_card
= host
->card
;
386 struct ms_id_register id_reg
;
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
;
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
);
401 init_completion(&card
->mrq_complete
);
404 if (memstick_set_rw_addr(card
))
407 card
->next_request
= h_memstick_read_dev_id
;
408 memstick_new_req(host
);
409 wait_for_completion(&card
->mrq_complete
);
411 if (card
->current_mrq
.error
)
414 host
->card
= old_card
;
417 host
->card
= old_card
;
422 static int memstick_power_on(struct memstick_host
*host
)
424 int rc
= host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_ON
);
427 rc
= host
->set_param(host
, MEMSTICK_INTERFACE
, MEMSTICK_SERIAL
);
432 static void memstick_check(struct work_struct
*work
)
434 struct memstick_host
*host
= container_of(work
, struct memstick_host
,
436 struct memstick_dev
*card
;
438 dev_dbg(&host
->dev
, "memstick_check started\n");
439 mutex_lock(&host
->lock
);
441 if (memstick_power_on(host
))
443 } else if (host
->card
->stop
)
444 host
->card
->stop(host
->card
);
446 card
= memstick_alloc_card(host
);
450 device_unregister(&host
->card
->dev
);
454 dev_dbg(&host
->dev
, "new card %02x, %02x, %02x\n",
455 card
->id
.type
, card
->id
.category
, card
->id
.class);
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
);
462 } else if (host
->card
->start
)
463 host
->card
->start(host
->card
);
468 if (device_register(&card
->dev
)) {
469 put_device(&card
->dev
);
479 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
481 mutex_unlock(&host
->lock
);
482 dev_dbg(&host
->dev
, "memstick_check finished\n");
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
490 struct memstick_host
*memstick_alloc_host(unsigned int extra
,
493 struct memstick_host
*host
;
495 host
= kzalloc(sizeof(struct memstick_host
) + extra
, GFP_KERNEL
);
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
);
505 EXPORT_SYMBOL(memstick_alloc_host
);
508 * memstick_add_host - start request processing on memstick host
509 * @host - host to use
511 int memstick_add_host(struct memstick_host
*host
)
516 if (!idr_pre_get(&memstick_host_idr
, GFP_KERNEL
))
519 spin_lock(&memstick_host_lock
);
520 rc
= idr_get_new(&memstick_host_idr
, host
, &host
->id
);
521 spin_unlock(&memstick_host_lock
);
524 else if (rc
!= -EAGAIN
)
528 dev_set_name(&host
->dev
, "memstick%u", host
->id
);
530 rc
= device_add(&host
->dev
);
532 spin_lock(&memstick_host_lock
);
533 idr_remove(&memstick_host_idr
, host
->id
);
534 spin_unlock(&memstick_host_lock
);
538 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
539 memstick_detect_change(host
);
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 flush_workqueue(workqueue
);
551 mutex_lock(&host
->lock
);
553 device_unregister(&host
->card
->dev
);
555 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
556 mutex_unlock(&host
->lock
);
558 spin_lock(&memstick_host_lock
);
559 idr_remove(&memstick_host_idr
, host
->id
);
560 spin_unlock(&memstick_host_lock
);
561 device_del(&host
->dev
);
563 EXPORT_SYMBOL(memstick_remove_host
);
566 * memstick_free_host - free memstick host
567 * @host - host to use
569 void memstick_free_host(struct memstick_host
*host
)
571 mutex_destroy(&host
->lock
);
572 put_device(&host
->dev
);
574 EXPORT_SYMBOL(memstick_free_host
);
577 * memstick_suspend_host - notify bus driver of host suspension
578 * @host - host to use
580 void memstick_suspend_host(struct memstick_host
*host
)
582 mutex_lock(&host
->lock
);
583 host
->set_param(host
, MEMSTICK_POWER
, MEMSTICK_POWER_OFF
);
584 mutex_unlock(&host
->lock
);
586 EXPORT_SYMBOL(memstick_suspend_host
);
589 * memstick_resume_host - notify bus driver of host resumption
590 * @host - host to use
592 void memstick_resume_host(struct memstick_host
*host
)
596 mutex_lock(&host
->lock
);
598 rc
= memstick_power_on(host
);
599 mutex_unlock(&host
->lock
);
602 memstick_detect_change(host
);
604 EXPORT_SYMBOL(memstick_resume_host
);
606 int memstick_register_driver(struct memstick_driver
*drv
)
608 drv
->driver
.bus
= &memstick_bus_type
;
610 return driver_register(&drv
->driver
);
612 EXPORT_SYMBOL(memstick_register_driver
);
614 void memstick_unregister_driver(struct memstick_driver
*drv
)
616 driver_unregister(&drv
->driver
);
618 EXPORT_SYMBOL(memstick_unregister_driver
);
621 static int __init
memstick_init(void)
625 workqueue
= create_freezable_workqueue("kmemstick");
629 rc
= bus_register(&memstick_bus_type
);
631 rc
= class_register(&memstick_host_class
);
636 bus_unregister(&memstick_bus_type
);
637 destroy_workqueue(workqueue
);
642 static void __exit
memstick_exit(void)
644 class_unregister(&memstick_host_class
);
645 bus_unregister(&memstick_bus_type
);
646 destroy_workqueue(workqueue
);
647 idr_destroy(&memstick_host_idr
);
650 module_init(memstick_init
);
651 module_exit(memstick_exit
);
653 MODULE_AUTHOR("Alex Dubov");
654 MODULE_LICENSE("GPL");
655 MODULE_DESCRIPTION("Sony MemoryStick core driver");