blob: 4d673a626db4ed80d96eae715d020a4448d64877 [file] [log] [blame]
Alex Dubovbaf85322008-02-09 10:20:54 -08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Paul Gortmakerc47e7892011-07-03 15:12:37 -040020#include <linux/module.h>
Kai-Heng Feng4bde07d2018-11-05 16:45:04 +080021#include <linux/pm_runtime.h>
Alex Dubovbaf85322008-02-09 10:20:54 -080022
23#define DRIVER_NAME "memstick"
Alex Dubovbaf85322008-02-09 10:20:54 -080024
25static unsigned int cmd_retries = 3;
26module_param(cmd_retries, uint, 0644);
27
28static struct workqueue_struct *workqueue;
29static DEFINE_IDR(memstick_host_idr);
30static DEFINE_SPINLOCK(memstick_host_lock);
31
32static int memstick_dev_match(struct memstick_dev *card,
33 struct memstick_device_id *id)
34{
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))
39 return 1;
40 }
41
42 return 0;
43}
44
45static int memstick_bus_match(struct device *dev, struct device_driver *drv)
46{
47 struct memstick_dev *card = container_of(dev, struct memstick_dev,
48 dev);
49 struct memstick_driver *ms_drv = container_of(drv,
50 struct memstick_driver,
51 driver);
52 struct memstick_device_id *ids = ms_drv->id_table;
53
54 if (ids) {
55 while (ids->match_flags) {
56 if (memstick_dev_match(card, ids))
57 return 1;
58 ++ids;
59 }
60 }
61 return 0;
62}
63
64static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
65{
66 struct memstick_dev *card = container_of(dev, struct memstick_dev,
67 dev);
68
69 if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
70 return -ENOMEM;
71
72 if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
73 return -ENOMEM;
74
75 if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
76 return -ENOMEM;
77
78 return 0;
79}
80
81static int memstick_device_probe(struct device *dev)
82{
83 struct memstick_dev *card = container_of(dev, struct memstick_dev,
84 dev);
85 struct memstick_driver *drv = container_of(dev->driver,
86 struct memstick_driver,
87 driver);
88 int rc = -ENODEV;
89
90 if (dev->driver && drv->probe) {
91 rc = drv->probe(card);
92 if (!rc)
93 get_device(dev);
94 }
95 return rc;
96}
97
98static int memstick_device_remove(struct device *dev)
99{
100 struct memstick_dev *card = container_of(dev, struct memstick_dev,
101 dev);
102 struct memstick_driver *drv = container_of(dev->driver,
103 struct memstick_driver,
104 driver);
105
106 if (dev->driver && drv->remove) {
107 drv->remove(card);
108 card->dev.driver = NULL;
109 }
110
111 put_device(dev);
112 return 0;
113}
114
115#ifdef CONFIG_PM
116
117static int memstick_device_suspend(struct device *dev, pm_message_t state)
118{
119 struct memstick_dev *card = container_of(dev, struct memstick_dev,
120 dev);
121 struct memstick_driver *drv = container_of(dev->driver,
122 struct memstick_driver,
123 driver);
124
125 if (dev->driver && drv->suspend)
126 return drv->suspend(card, state);
127 return 0;
128}
129
130static int memstick_device_resume(struct device *dev)
131{
132 struct memstick_dev *card = container_of(dev, struct memstick_dev,
133 dev);
134 struct memstick_driver *drv = container_of(dev->driver,
135 struct memstick_driver,
136 driver);
137
138 if (dev->driver && drv->resume)
139 return drv->resume(card);
140 return 0;
141}
142
143#else
144
145#define memstick_device_suspend NULL
146#define memstick_device_resume NULL
147
148#endif /* CONFIG_PM */
149
150#define MEMSTICK_ATTR(name, format) \
151static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
152 char *buf) \
153{ \
154 struct memstick_dev *card = container_of(dev, struct memstick_dev, \
155 dev); \
156 return sprintf(buf, format, card->id.name); \
Greg Kroah-Hartmanb7854642013-10-07 18:27:41 -0700157} \
158static DEVICE_ATTR_RO(name);
Alex Dubovbaf85322008-02-09 10:20:54 -0800159
160MEMSTICK_ATTR(type, "%02X");
161MEMSTICK_ATTR(category, "%02X");
162MEMSTICK_ATTR(class, "%02X");
163
Greg Kroah-Hartmanb7854642013-10-07 18:27:41 -0700164static struct attribute *memstick_dev_attrs[] = {
165 &dev_attr_type.attr,
166 &dev_attr_category.attr,
167 &dev_attr_class.attr,
168 NULL,
Alex Dubovbaf85322008-02-09 10:20:54 -0800169};
Greg Kroah-Hartmanb7854642013-10-07 18:27:41 -0700170ATTRIBUTE_GROUPS(memstick_dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800171
172static struct bus_type memstick_bus_type = {
173 .name = "memstick",
Greg Kroah-Hartmanb7854642013-10-07 18:27:41 -0700174 .dev_groups = memstick_dev_groups,
Alex Dubovbaf85322008-02-09 10:20:54 -0800175 .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
181};
182
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100183static void memstick_free(struct device *dev)
Alex Dubovbaf85322008-02-09 10:20:54 -0800184{
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100185 struct memstick_host *host = container_of(dev, struct memstick_host,
186 dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800187 kfree(host);
188}
189
190static struct class memstick_host_class = {
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700191 .name = "memstick_host",
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100192 .dev_release = memstick_free
Alex Dubovbaf85322008-02-09 10:20:54 -0800193};
194
195static void memstick_free_card(struct device *dev)
196{
197 struct memstick_dev *card = container_of(dev, struct memstick_dev,
198 dev);
199 kfree(card);
200}
201
202static int memstick_dummy_check(struct memstick_dev *card)
203{
204 return 0;
205}
206
207/**
208 * memstick_detect_change - schedule media detection on memstick host
209 * @host - host to use
210 */
211void memstick_detect_change(struct memstick_host *host)
212{
213 queue_work(workqueue, &host->media_checker);
214}
215EXPORT_SYMBOL(memstick_detect_change);
216
217/**
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
221 *
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.
226 */
227int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
228{
229 int rc = -ENXIO;
230
231 if ((*mrq) && (*mrq)->error && host->retries) {
232 (*mrq)->error = rc;
233 host->retries--;
234 return 0;
235 }
236
237 if (host->card && host->card->next_request)
238 rc = host->card->next_request(host->card, mrq);
239
240 if (!rc)
Alex Dubov29196dc2008-03-10 11:43:38 -0700241 host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800242 else
243 *mrq = NULL;
244
245 return rc;
246}
247EXPORT_SYMBOL(memstick_next_req);
248
249/**
250 * memstick_new_req - notify the host that some requests are pending
251 * @host - host to use
252 */
253void memstick_new_req(struct memstick_host *host)
254{
Alex Dubovf1d82692008-07-25 19:45:02 -0700255 if (host->card) {
256 host->retries = cmd_retries;
Wolfram Sang16735d02013-11-14 14:32:02 -0800257 reinit_completion(&host->card->mrq_complete);
Alex Dubovf1d82692008-07-25 19:45:02 -0700258 host->request(host);
259 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800260}
261EXPORT_SYMBOL(memstick_new_req);
262
263/**
264 * memstick_init_req_sg - set request fields needed for bulk data transfer
265 * @mrq - request to use
266 * @tpc - memstick Transport Protocol Command
267 * @sg - TPC argument
268 */
269void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700270 const struct scatterlist *sg)
Alex Dubovbaf85322008-02-09 10:20:54 -0800271{
272 mrq->tpc = tpc;
273 if (tpc & 8)
274 mrq->data_dir = WRITE;
275 else
276 mrq->data_dir = READ;
277
278 mrq->sg = *sg;
Alex Dubove1f19992008-03-10 11:43:37 -0700279 mrq->long_data = 1;
Alex Dubovbaf85322008-02-09 10:20:54 -0800280
281 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
282 mrq->need_card_int = 1;
283 else
284 mrq->need_card_int = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800285}
286EXPORT_SYMBOL(memstick_init_req_sg);
287
288/**
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
294 *
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.
298 */
299void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700300 const void *buf, size_t length)
Alex Dubovbaf85322008-02-09 10:20:54 -0800301{
302 mrq->tpc = tpc;
303 if (tpc & 8)
304 mrq->data_dir = WRITE;
305 else
306 mrq->data_dir = READ;
307
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);
311
Alex Dubove1f19992008-03-10 11:43:37 -0700312 mrq->long_data = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800313
314 if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
315 mrq->need_card_int = 1;
316 else
317 mrq->need_card_int = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800318}
319EXPORT_SYMBOL(memstick_init_req);
320
321/*
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).
326 */
327
328static int h_memstick_read_dev_id(struct memstick_dev *card,
329 struct memstick_request **mrq)
330{
331 struct ms_id_register id_reg;
332
333 if (!(*mrq)) {
334 memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, NULL,
335 sizeof(struct ms_id_register));
336 *mrq = &card->current_mrq;
337 return 0;
338 } else {
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;
Jiri Slabyb873c2f2009-09-23 15:57:31 -0700345 dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
Alex Dubovbaf85322008-02-09 10:20:54 -0800346 }
347 complete(&card->mrq_complete);
348 return -EAGAIN;
349 }
350}
351
352static int h_memstick_set_rw_addr(struct memstick_dev *card,
353 struct memstick_request **mrq)
354{
355 if (!(*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;
360 return 0;
361 } else {
362 complete(&card->mrq_complete);
363 return -EAGAIN;
364 }
365}
366
367/**
368 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
369 * complete
370 * @card - media device to use
371 */
372int memstick_set_rw_addr(struct memstick_dev *card)
373{
374 card->next_request = h_memstick_set_rw_addr;
375 memstick_new_req(card->host);
376 wait_for_completion(&card->mrq_complete);
377
378 return card->current_mrq.error;
379}
380EXPORT_SYMBOL(memstick_set_rw_addr);
381
382static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
383{
384 struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
385 GFP_KERNEL);
386 struct memstick_dev *old_card = host->card;
387 struct ms_id_register id_reg;
388
389 if (card) {
390 card->host = host;
Kay Sievers0252c3b2009-01-06 10:44:38 -0800391 dev_set_name(&card->dev, "%s", dev_name(&host->dev));
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100392 card->dev.parent = &host->dev;
Alex Dubovbaf85322008-02-09 10:20:54 -0800393 card->dev.bus = &memstick_bus_type;
394 card->dev.release = memstick_free_card;
395 card->check = memstick_dummy_check;
396
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);
401
402 init_completion(&card->mrq_complete);
403
404 host->card = card;
405 if (memstick_set_rw_addr(card))
406 goto err_out;
407
408 card->next_request = h_memstick_read_dev_id;
409 memstick_new_req(host);
410 wait_for_completion(&card->mrq_complete);
411
412 if (card->current_mrq.error)
413 goto err_out;
414 }
415 host->card = old_card;
416 return card;
417err_out:
418 host->card = old_card;
419 kfree(card);
420 return NULL;
421}
422
Alex Dubovb7789992008-07-25 19:45:00 -0700423static int memstick_power_on(struct memstick_host *host)
Alex Dubovbaf85322008-02-09 10:20:54 -0800424{
Alex Dubovb7789992008-07-25 19:45:00 -0700425 int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
426
427 if (!rc)
428 rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
429
430 return rc;
Alex Dubovbaf85322008-02-09 10:20:54 -0800431}
432
433static void memstick_check(struct work_struct *work)
434{
435 struct memstick_host *host = container_of(work, struct memstick_host,
436 media_checker);
437 struct memstick_dev *card;
438
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100439 dev_dbg(&host->dev, "memstick_check started\n");
Kai-Heng Feng4bde07d2018-11-05 16:45:04 +0800440 pm_runtime_get_noresume(host->dev.parent);
Alex Dubovbaf85322008-02-09 10:20:54 -0800441 mutex_lock(&host->lock);
Alex Dubov17017d82008-07-25 19:45:01 -0700442 if (!host->card) {
443 if (memstick_power_on(host))
444 goto out_power_off;
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700445 } else if (host->card->stop)
Alex Dubov17017d82008-07-25 19:45:01 -0700446 host->card->stop(host->card);
Alex Dubovbaf85322008-02-09 10:20:54 -0800447
448 card = memstick_alloc_card(host);
449
450 if (!card) {
451 if (host->card) {
452 device_unregister(&host->card->dev);
453 host->card = NULL;
454 }
455 } else {
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100456 dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
Alex Dubovbaf85322008-02-09 10:20:54 -0800457 card->id.type, card->id.category, card->id.class);
458 if (host->card) {
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);
463 host->card = NULL;
Alex Dubov8e82f8c2008-09-13 02:33:26 -0700464 } else if (host->card->start)
Alex Dubov17017d82008-07-25 19:45:01 -0700465 host->card->start(host->card);
Alex Dubovbaf85322008-02-09 10:20:54 -0800466 }
467
468 if (!host->card) {
469 host->card = card;
470 if (device_register(&card->dev)) {
Vasiliy Kulikov01933832011-01-12 17:01:00 -0800471 put_device(&card->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800472 kfree(host->card);
473 host->card = NULL;
474 }
475 } else
476 kfree(card);
477 }
478
Alex Dubov17017d82008-07-25 19:45:01 -0700479out_power_off:
Alex Dubovbaf85322008-02-09 10:20:54 -0800480 if (!host->card)
481 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
482
483 mutex_unlock(&host->lock);
Kai-Heng Feng4bde07d2018-11-05 16:45:04 +0800484 pm_runtime_put(host->dev.parent);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100485 dev_dbg(&host->dev, "memstick_check finished\n");
Alex Dubovbaf85322008-02-09 10:20:54 -0800486}
487
488/**
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
492 */
493struct memstick_host *memstick_alloc_host(unsigned int extra,
494 struct device *dev)
495{
496 struct memstick_host *host;
497
498 host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
499 if (host) {
500 mutex_init(&host->lock);
501 INIT_WORK(&host->media_checker, memstick_check);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100502 host->dev.class = &memstick_host_class;
503 host->dev.parent = dev;
504 device_initialize(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800505 }
506 return host;
507}
508EXPORT_SYMBOL(memstick_alloc_host);
509
510/**
511 * memstick_add_host - start request processing on memstick host
512 * @host - host to use
513 */
514int memstick_add_host(struct memstick_host *host)
515{
516 int rc;
517
Tejun Heo7b51f472013-02-27 17:04:28 -0800518 idr_preload(GFP_KERNEL);
519 spin_lock(&memstick_host_lock);
Alex Dubovbaf85322008-02-09 10:20:54 -0800520
Tejun Heo7b51f472013-02-27 17:04:28 -0800521 rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
522 if (rc >= 0)
523 host->id = rc;
524
525 spin_unlock(&memstick_host_lock);
526 idr_preload_end();
527 if (rc < 0)
528 return rc;
Alex Dubovbaf85322008-02-09 10:20:54 -0800529
Kay Sievers0252c3b2009-01-06 10:44:38 -0800530 dev_set_name(&host->dev, "memstick%u", host->id);
Alex Dubovbaf85322008-02-09 10:20:54 -0800531
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100532 rc = device_add(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800533 if (rc) {
534 spin_lock(&memstick_host_lock);
535 idr_remove(&memstick_host_idr, host->id);
536 spin_unlock(&memstick_host_lock);
537 return rc;
538 }
539
540 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
541 memstick_detect_change(host);
542 return 0;
543}
544EXPORT_SYMBOL(memstick_add_host);
545
546/**
547 * memstick_remove_host - stop request processing on memstick host
548 * @host - host to use
549 */
550void memstick_remove_host(struct memstick_host *host)
551{
552 flush_workqueue(workqueue);
553 mutex_lock(&host->lock);
554 if (host->card)
555 device_unregister(&host->card->dev);
556 host->card = NULL;
557 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
558 mutex_unlock(&host->lock);
559
560 spin_lock(&memstick_host_lock);
561 idr_remove(&memstick_host_idr, host->id);
562 spin_unlock(&memstick_host_lock);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100563 device_del(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800564}
565EXPORT_SYMBOL(memstick_remove_host);
566
567/**
568 * memstick_free_host - free memstick host
569 * @host - host to use
570 */
571void memstick_free_host(struct memstick_host *host)
572{
573 mutex_destroy(&host->lock);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100574 put_device(&host->dev);
Alex Dubovbaf85322008-02-09 10:20:54 -0800575}
576EXPORT_SYMBOL(memstick_free_host);
577
Alex Dubovd114ad52008-03-10 11:43:38 -0700578/**
579 * memstick_suspend_host - notify bus driver of host suspension
580 * @host - host to use
581 */
582void memstick_suspend_host(struct memstick_host *host)
583{
584 mutex_lock(&host->lock);
585 host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
586 mutex_unlock(&host->lock);
587}
588EXPORT_SYMBOL(memstick_suspend_host);
589
590/**
591 * memstick_resume_host - notify bus driver of host resumption
592 * @host - host to use
593 */
594void memstick_resume_host(struct memstick_host *host)
595{
Alex Dubovb7789992008-07-25 19:45:00 -0700596 int rc = 0;
597
Alex Dubovd114ad52008-03-10 11:43:38 -0700598 mutex_lock(&host->lock);
Alex Dubovead70772008-03-19 17:01:06 -0700599 if (host->card)
Alex Dubovb7789992008-07-25 19:45:00 -0700600 rc = memstick_power_on(host);
Alex Dubovd114ad52008-03-10 11:43:38 -0700601 mutex_unlock(&host->lock);
Alex Dubovb7789992008-07-25 19:45:00 -0700602
603 if (!rc)
604 memstick_detect_change(host);
Alex Dubovd114ad52008-03-10 11:43:38 -0700605}
606EXPORT_SYMBOL(memstick_resume_host);
607
Alex Dubovbaf85322008-02-09 10:20:54 -0800608int memstick_register_driver(struct memstick_driver *drv)
609{
610 drv->driver.bus = &memstick_bus_type;
611
612 return driver_register(&drv->driver);
613}
614EXPORT_SYMBOL(memstick_register_driver);
615
616void memstick_unregister_driver(struct memstick_driver *drv)
617{
618 driver_unregister(&drv->driver);
619}
620EXPORT_SYMBOL(memstick_unregister_driver);
621
622
623static int __init memstick_init(void)
624{
625 int rc;
626
Tejun Heo58a69cb2011-02-16 09:25:31 +0100627 workqueue = create_freezable_workqueue("kmemstick");
Alex Dubovbaf85322008-02-09 10:20:54 -0800628 if (!workqueue)
629 return -ENOMEM;
630
631 rc = bus_register(&memstick_bus_type);
632 if (!rc)
633 rc = class_register(&memstick_host_class);
634
635 if (!rc)
636 return 0;
637
638 bus_unregister(&memstick_bus_type);
639 destroy_workqueue(workqueue);
640
641 return rc;
642}
643
644static void __exit memstick_exit(void)
645{
646 class_unregister(&memstick_host_class);
647 bus_unregister(&memstick_bus_type);
648 destroy_workqueue(workqueue);
649 idr_destroy(&memstick_host_idr);
650}
651
652module_init(memstick_init);
653module_exit(memstick_exit);
654
655MODULE_AUTHOR("Alex Dubov");
656MODULE_LICENSE("GPL");
657MODULE_DESCRIPTION("Sony MemoryStick core driver");