Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / pcmcia / cs.c
blobd9ea192c4001ed9e315d21bac96b2ab37361376f
1 /*
2 * cs.c -- Kernel Card Services - core services
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * The initial developer of the original code is David A. Hinds
9 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
12 * (C) 1999 David A. Hinds
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/major.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/timer.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/pm.h>
29 #include <linux/device.h>
30 #include <linux/kthread.h>
31 #include <linux/freezer.h>
32 #include <asm/system.h>
33 #include <asm/irq.h>
35 #include <pcmcia/ss.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/cisreg.h>
38 #include <pcmcia/ds.h>
39 #include "cs_internal.h"
42 /* Module parameters */
44 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
45 MODULE_DESCRIPTION("Linux Kernel Card Services");
46 MODULE_LICENSE("GPL");
48 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
50 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
51 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
52 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
53 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
54 INT_MODULE_PARM(reset_time, 10); /* usecs */
55 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
56 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
57 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
59 /* Access speed for attribute memory windows */
60 INT_MODULE_PARM(cis_speed, 300); /* ns */
63 socket_state_t dead_socket = {
64 .csc_mask = SS_DETECT,
66 EXPORT_SYMBOL(dead_socket);
69 /* List of all sockets, protected by a rwsem */
70 LIST_HEAD(pcmcia_socket_list);
71 EXPORT_SYMBOL(pcmcia_socket_list);
73 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
74 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
77 struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
79 struct device *dev = get_device(&skt->dev);
80 if (!dev)
81 return NULL;
82 return dev_get_drvdata(dev);
84 EXPORT_SYMBOL(pcmcia_get_socket);
87 void pcmcia_put_socket(struct pcmcia_socket *skt)
89 put_device(&skt->dev);
91 EXPORT_SYMBOL(pcmcia_put_socket);
94 static void pcmcia_release_socket(struct device *dev)
96 struct pcmcia_socket *socket = dev_get_drvdata(dev);
98 complete(&socket->socket_released);
101 static int pccardd(void *__skt);
104 * pcmcia_register_socket - add a new pcmcia socket device
105 * @socket: the &socket to register
107 int pcmcia_register_socket(struct pcmcia_socket *socket)
109 struct task_struct *tsk;
110 int ret;
112 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
113 return -EINVAL;
115 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
117 /* try to obtain a socket number [yes, it gets ugly if we
118 * register more than 2^sizeof(unsigned int) pcmcia
119 * sockets... but the socket number is deprecated
120 * anyways, so I don't care] */
121 down_write(&pcmcia_socket_list_rwsem);
122 if (list_empty(&pcmcia_socket_list))
123 socket->sock = 0;
124 else {
125 unsigned int found, i = 1;
126 struct pcmcia_socket *tmp;
127 do {
128 found = 1;
129 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
130 if (tmp->sock == i)
131 found = 0;
133 i++;
134 } while (!found);
135 socket->sock = i - 1;
137 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
138 up_write(&pcmcia_socket_list_rwsem);
140 #ifndef CONFIG_CARDBUS
142 * If we do not support Cardbus, ensure that
143 * the Cardbus socket capability is disabled.
145 socket->features &= ~SS_CAP_CARDBUS;
146 #endif
148 /* set proper values in socket->dev */
149 dev_set_drvdata(&socket->dev, socket);
150 socket->dev.class = &pcmcia_socket_class;
151 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
153 /* base address = 0, map = 0 */
154 socket->cis_mem.flags = 0;
155 socket->cis_mem.speed = cis_speed;
157 INIT_LIST_HEAD(&socket->cis_cache);
159 init_completion(&socket->socket_released);
160 init_completion(&socket->thread_done);
161 mutex_init(&socket->skt_mutex);
162 mutex_init(&socket->ops_mutex);
163 spin_lock_init(&socket->thread_lock);
165 if (socket->resource_ops->init) {
166 mutex_lock(&socket->ops_mutex);
167 ret = socket->resource_ops->init(socket);
168 mutex_unlock(&socket->ops_mutex);
169 if (ret)
170 goto err;
173 tsk = kthread_run(pccardd, socket, "pccardd");
174 if (IS_ERR(tsk)) {
175 ret = PTR_ERR(tsk);
176 goto err;
179 wait_for_completion(&socket->thread_done);
180 if (!socket->thread) {
181 dev_printk(KERN_WARNING, &socket->dev,
182 "PCMCIA: warning: socket thread did not start\n");
183 return -EIO;
186 pcmcia_parse_events(socket, SS_DETECT);
189 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
190 * If it fails, it doesn't matter -- we still have 32-bit CardBus
191 * support to offer, so this is not a failure mode.
193 request_module_nowait("pcmcia");
195 return 0;
197 err:
198 down_write(&pcmcia_socket_list_rwsem);
199 list_del(&socket->socket_list);
200 up_write(&pcmcia_socket_list_rwsem);
201 return ret;
202 } /* pcmcia_register_socket */
203 EXPORT_SYMBOL(pcmcia_register_socket);
207 * pcmcia_unregister_socket - remove a pcmcia socket device
208 * @socket: the &socket to unregister
210 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
212 if (!socket)
213 return;
215 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
217 if (socket->thread)
218 kthread_stop(socket->thread);
220 /* remove from our own list */
221 down_write(&pcmcia_socket_list_rwsem);
222 list_del(&socket->socket_list);
223 up_write(&pcmcia_socket_list_rwsem);
225 /* wait for sysfs to drop all references */
226 if (socket->resource_ops->exit) {
227 mutex_lock(&socket->ops_mutex);
228 socket->resource_ops->exit(socket);
229 mutex_unlock(&socket->ops_mutex);
231 wait_for_completion(&socket->socket_released);
232 } /* pcmcia_unregister_socket */
233 EXPORT_SYMBOL(pcmcia_unregister_socket);
236 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
238 struct pcmcia_socket *s;
240 down_read(&pcmcia_socket_list_rwsem);
241 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
242 if (s->sock == nr) {
243 up_read(&pcmcia_socket_list_rwsem);
244 return s;
246 up_read(&pcmcia_socket_list_rwsem);
248 return NULL;
251 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
253 static int socket_reset(struct pcmcia_socket *skt)
255 int status, i;
257 dev_dbg(&skt->dev, "reset\n");
259 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
260 skt->ops->set_socket(skt, &skt->socket);
261 udelay((long)reset_time);
263 skt->socket.flags &= ~SS_RESET;
264 skt->ops->set_socket(skt, &skt->socket);
266 msleep(unreset_delay * 10);
267 for (i = 0; i < unreset_limit; i++) {
268 skt->ops->get_status(skt, &status);
270 if (!(status & SS_DETECT))
271 return -ENODEV;
273 if (status & SS_READY)
274 return 0;
276 msleep(unreset_check * 10);
279 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
280 return -ETIMEDOUT;
284 * socket_setup() and socket_shutdown() are called by the main event handler
285 * when card insertion and removal events are received.
286 * socket_setup() turns on socket power and resets the socket, in two stages.
287 * socket_shutdown() unconfigures a socket and turns off socket power.
289 static void socket_shutdown(struct pcmcia_socket *s)
291 int status;
293 dev_dbg(&s->dev, "shutdown\n");
295 if (s->callback)
296 s->callback->remove(s);
298 mutex_lock(&s->ops_mutex);
299 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
300 msleep(shutdown_delay * 10);
301 s->state &= SOCKET_INUSE;
303 /* Blank out the socket state */
304 s->socket = dead_socket;
305 s->ops->init(s);
306 s->ops->set_socket(s, &s->socket);
307 s->lock_count = 0;
308 kfree(s->fake_cis);
309 s->fake_cis = NULL;
310 s->functions = 0;
312 /* From here on we can be sure that only we (that is, the
313 * pccardd thread) accesses this socket, and all (16-bit)
314 * PCMCIA interactions are gone. Therefore, release
315 * ops_mutex so that we don't get a sysfs-related lockdep
316 * warning.
318 mutex_unlock(&s->ops_mutex);
320 #ifdef CONFIG_CARDBUS
321 cb_free(s);
322 #endif
324 /* give socket some time to power down */
325 msleep(100);
327 s->ops->get_status(s, &status);
328 if (status & SS_POWERON) {
329 dev_printk(KERN_ERR, &s->dev,
330 "*** DANGER *** unable to remove socket power\n");
333 s->state &= ~SOCKET_INUSE;
336 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
338 int status, i;
340 dev_dbg(&skt->dev, "setup\n");
342 skt->ops->get_status(skt, &status);
343 if (!(status & SS_DETECT))
344 return -ENODEV;
346 msleep(initial_delay * 10);
348 for (i = 0; i < 100; i++) {
349 skt->ops->get_status(skt, &status);
350 if (!(status & SS_DETECT))
351 return -ENODEV;
353 if (!(status & SS_PENDING))
354 break;
356 msleep(100);
359 if (status & SS_PENDING) {
360 dev_printk(KERN_ERR, &skt->dev,
361 "voltage interrogation timed out.\n");
362 return -ETIMEDOUT;
365 if (status & SS_CARDBUS) {
366 if (!(skt->features & SS_CAP_CARDBUS)) {
367 dev_printk(KERN_ERR, &skt->dev,
368 "cardbus cards are not supported.\n");
369 return -EINVAL;
371 skt->state |= SOCKET_CARDBUS;
372 } else
373 skt->state &= ~SOCKET_CARDBUS;
376 * Decode the card voltage requirements, and apply power to the card.
378 if (status & SS_3VCARD)
379 skt->socket.Vcc = skt->socket.Vpp = 33;
380 else if (!(status & SS_XVCARD))
381 skt->socket.Vcc = skt->socket.Vpp = 50;
382 else {
383 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
384 return -EIO;
387 if (skt->power_hook)
388 skt->power_hook(skt, HOOK_POWER_PRE);
390 skt->socket.flags = 0;
391 skt->ops->set_socket(skt, &skt->socket);
394 * Wait "vcc_settle" for the supply to stabilise.
396 msleep(vcc_settle * 10);
398 skt->ops->get_status(skt, &status);
399 if (!(status & SS_POWERON)) {
400 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
401 return -EIO;
404 status = socket_reset(skt);
406 if (skt->power_hook)
407 skt->power_hook(skt, HOOK_POWER_POST);
409 return status;
413 * Handle card insertion. Setup the socket, reset the card,
414 * and then tell the rest of PCMCIA that a card is present.
416 static int socket_insert(struct pcmcia_socket *skt)
418 int ret;
420 dev_dbg(&skt->dev, "insert\n");
422 mutex_lock(&skt->ops_mutex);
423 if (skt->state & SOCKET_INUSE) {
424 mutex_unlock(&skt->ops_mutex);
425 return -EINVAL;
427 skt->state |= SOCKET_INUSE;
429 ret = socket_setup(skt, setup_delay);
430 if (ret == 0) {
431 skt->state |= SOCKET_PRESENT;
433 dev_printk(KERN_NOTICE, &skt->dev,
434 "pccard: %s card inserted into slot %d\n",
435 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
436 skt->sock);
438 #ifdef CONFIG_CARDBUS
439 if (skt->state & SOCKET_CARDBUS) {
440 cb_alloc(skt);
441 skt->state |= SOCKET_CARDBUS_CONFIG;
443 #endif
444 dev_dbg(&skt->dev, "insert done\n");
445 mutex_unlock(&skt->ops_mutex);
447 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
448 skt->callback->add(skt);
449 } else {
450 mutex_unlock(&skt->ops_mutex);
451 socket_shutdown(skt);
454 return ret;
457 static int socket_suspend(struct pcmcia_socket *skt)
459 if (skt->state & SOCKET_SUSPEND)
460 return -EBUSY;
462 mutex_lock(&skt->ops_mutex);
463 skt->suspended_state = skt->state;
465 skt->socket = dead_socket;
466 skt->ops->set_socket(skt, &skt->socket);
467 if (skt->ops->suspend)
468 skt->ops->suspend(skt);
469 skt->state |= SOCKET_SUSPEND;
470 mutex_unlock(&skt->ops_mutex);
471 return 0;
474 static int socket_early_resume(struct pcmcia_socket *skt)
476 mutex_lock(&skt->ops_mutex);
477 skt->socket = dead_socket;
478 skt->ops->init(skt);
479 skt->ops->set_socket(skt, &skt->socket);
480 if (skt->state & SOCKET_PRESENT)
481 skt->resume_status = socket_setup(skt, resume_delay);
482 mutex_unlock(&skt->ops_mutex);
483 return 0;
486 static int socket_late_resume(struct pcmcia_socket *skt)
488 int ret;
490 mutex_lock(&skt->ops_mutex);
491 skt->state &= ~SOCKET_SUSPEND;
492 mutex_unlock(&skt->ops_mutex);
494 if (!(skt->state & SOCKET_PRESENT)) {
495 ret = socket_insert(skt);
496 if (ret == -ENODEV)
497 ret = 0;
498 return ret;
501 if (skt->resume_status) {
502 socket_shutdown(skt);
503 return 0;
506 if (skt->suspended_state != skt->state) {
507 dev_dbg(&skt->dev,
508 "suspend state 0x%x != resume state 0x%x\n",
509 skt->suspended_state, skt->state);
511 socket_shutdown(skt);
512 return socket_insert(skt);
515 #ifdef CONFIG_CARDBUS
516 if (skt->state & SOCKET_CARDBUS) {
517 /* We can't be sure the CardBus card is the same
518 * as the one previously inserted. Therefore, remove
519 * and re-add... */
520 cb_free(skt);
521 cb_alloc(skt);
522 return 0;
524 #endif
525 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
526 skt->callback->early_resume(skt);
527 return 0;
531 * Resume a socket. If a card is present, verify its CIS against
532 * our cached copy. If they are different, the card has been
533 * replaced, and we need to tell the drivers.
535 static int socket_resume(struct pcmcia_socket *skt)
537 if (!(skt->state & SOCKET_SUSPEND))
538 return -EBUSY;
540 socket_early_resume(skt);
541 return socket_late_resume(skt);
544 static void socket_remove(struct pcmcia_socket *skt)
546 dev_printk(KERN_NOTICE, &skt->dev,
547 "pccard: card ejected from slot %d\n", skt->sock);
548 socket_shutdown(skt);
552 * Process a socket card detect status change.
554 * If we don't have a card already present, delay the detect event for
555 * about 20ms (to be on the safe side) before reading the socket status.
557 * Some i82365-based systems send multiple SS_DETECT events during card
558 * insertion, and the "card present" status bit seems to bounce. This
559 * will probably be true with GPIO-based card detection systems after
560 * the product has aged.
562 static void socket_detect_change(struct pcmcia_socket *skt)
564 if (!(skt->state & SOCKET_SUSPEND)) {
565 int status;
567 if (!(skt->state & SOCKET_PRESENT))
568 msleep(20);
570 skt->ops->get_status(skt, &status);
571 if ((skt->state & SOCKET_PRESENT) &&
572 !(status & SS_DETECT))
573 socket_remove(skt);
574 if (!(skt->state & SOCKET_PRESENT) &&
575 (status & SS_DETECT))
576 socket_insert(skt);
580 static int pccardd(void *__skt)
582 struct pcmcia_socket *skt = __skt;
583 int ret;
585 skt->thread = current;
586 skt->socket = dead_socket;
587 skt->ops->init(skt);
588 skt->ops->set_socket(skt, &skt->socket);
590 /* register with the device core */
591 ret = device_register(&skt->dev);
592 if (ret) {
593 dev_printk(KERN_WARNING, &skt->dev,
594 "PCMCIA: unable to register socket\n");
595 skt->thread = NULL;
596 complete(&skt->thread_done);
597 return 0;
599 ret = pccard_sysfs_add_socket(&skt->dev);
600 if (ret)
601 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
603 complete(&skt->thread_done);
605 /* wait for userspace to catch up */
606 msleep(250);
608 set_freezable();
609 for (;;) {
610 unsigned long flags;
611 unsigned int events;
612 unsigned int sysfs_events;
614 set_current_state(TASK_INTERRUPTIBLE);
616 spin_lock_irqsave(&skt->thread_lock, flags);
617 events = skt->thread_events;
618 skt->thread_events = 0;
619 sysfs_events = skt->sysfs_events;
620 skt->sysfs_events = 0;
621 spin_unlock_irqrestore(&skt->thread_lock, flags);
623 mutex_lock(&skt->skt_mutex);
624 if (events & SS_DETECT)
625 socket_detect_change(skt);
627 if (sysfs_events) {
628 if (sysfs_events & PCMCIA_UEVENT_EJECT)
629 socket_remove(skt);
630 if (sysfs_events & PCMCIA_UEVENT_INSERT)
631 socket_insert(skt);
632 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
633 !(skt->state & SOCKET_CARDBUS)) {
634 if (skt->callback)
635 ret = skt->callback->suspend(skt);
636 else
637 ret = 0;
638 if (!ret) {
639 socket_suspend(skt);
640 msleep(100);
643 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
644 !(skt->state & SOCKET_CARDBUS)) {
645 ret = socket_resume(skt);
646 if (!ret && skt->callback)
647 skt->callback->resume(skt);
649 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
650 !(skt->state & SOCKET_CARDBUS)) {
651 if (!ret && skt->callback)
652 skt->callback->requery(skt);
655 mutex_unlock(&skt->skt_mutex);
657 if (events || sysfs_events)
658 continue;
660 if (kthread_should_stop())
661 break;
663 schedule();
664 try_to_freeze();
666 /* make sure we are running before we exit */
667 set_current_state(TASK_RUNNING);
669 /* shut down socket, if a device is still present */
670 if (skt->state & SOCKET_PRESENT) {
671 mutex_lock(&skt->skt_mutex);
672 socket_remove(skt);
673 mutex_unlock(&skt->skt_mutex);
676 /* remove from the device core */
677 pccard_sysfs_remove_socket(&skt->dev);
678 device_unregister(&skt->dev);
680 return 0;
684 * Yenta (at least) probes interrupts before registering the socket and
685 * starting the handler thread.
687 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
689 unsigned long flags;
690 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
691 if (s->thread) {
692 spin_lock_irqsave(&s->thread_lock, flags);
693 s->thread_events |= events;
694 spin_unlock_irqrestore(&s->thread_lock, flags);
696 wake_up_process(s->thread);
698 } /* pcmcia_parse_events */
699 EXPORT_SYMBOL(pcmcia_parse_events);
702 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
703 * @s: the PCMCIA socket we wan't to command
704 * @events: events to pass to pccardd
706 * userspace-issued insert, eject, suspend and resume commands must be
707 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
708 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
709 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
710 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
712 void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
714 unsigned long flags;
715 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
716 if (s->thread) {
717 spin_lock_irqsave(&s->thread_lock, flags);
718 s->sysfs_events |= events;
719 spin_unlock_irqrestore(&s->thread_lock, flags);
721 wake_up_process(s->thread);
724 EXPORT_SYMBOL(pcmcia_parse_uevents);
727 /* register pcmcia_callback */
728 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
730 int ret = 0;
732 /* s->skt_mutex also protects s->callback */
733 mutex_lock(&s->skt_mutex);
735 if (c) {
736 /* registration */
737 if (s->callback) {
738 ret = -EBUSY;
739 goto err;
742 s->callback = c;
744 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
745 s->callback->add(s);
746 } else
747 s->callback = NULL;
748 err:
749 mutex_unlock(&s->skt_mutex);
751 return ret;
753 EXPORT_SYMBOL(pccard_register_pcmcia);
756 /* I'm not sure which "reset" function this is supposed to use,
757 * but for now, it uses the low-level interface's reset, not the
758 * CIS register.
761 int pcmcia_reset_card(struct pcmcia_socket *skt)
763 int ret;
765 dev_dbg(&skt->dev, "resetting socket\n");
767 mutex_lock(&skt->skt_mutex);
768 do {
769 if (!(skt->state & SOCKET_PRESENT)) {
770 dev_dbg(&skt->dev, "can't reset, not present\n");
771 ret = -ENODEV;
772 break;
774 if (skt->state & SOCKET_SUSPEND) {
775 dev_dbg(&skt->dev, "can't reset, suspended\n");
776 ret = -EBUSY;
777 break;
779 if (skt->state & SOCKET_CARDBUS) {
780 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
781 ret = -EPERM;
782 break;
785 if (skt->callback)
786 skt->callback->suspend(skt);
787 mutex_lock(&skt->ops_mutex);
788 ret = socket_reset(skt);
789 mutex_unlock(&skt->ops_mutex);
790 if ((ret == 0) && (skt->callback))
791 skt->callback->resume(skt);
793 ret = 0;
794 } while (0);
795 mutex_unlock(&skt->skt_mutex);
797 return ret;
798 } /* reset_card */
799 EXPORT_SYMBOL(pcmcia_reset_card);
802 static int pcmcia_socket_uevent(struct device *dev,
803 struct kobj_uevent_env *env)
805 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
807 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
808 return -ENOMEM;
810 return 0;
814 static struct completion pcmcia_unload;
816 static void pcmcia_release_socket_class(struct class *data)
818 complete(&pcmcia_unload);
822 #ifdef CONFIG_PM
824 static int __pcmcia_pm_op(struct device *dev,
825 int (*callback) (struct pcmcia_socket *skt))
827 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
828 int ret;
830 mutex_lock(&s->skt_mutex);
831 ret = callback(s);
832 mutex_unlock(&s->skt_mutex);
834 return ret;
837 static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
839 return __pcmcia_pm_op(dev, socket_suspend);
842 static int pcmcia_socket_dev_resume_noirq(struct device *dev)
844 return __pcmcia_pm_op(dev, socket_early_resume);
847 static int __used pcmcia_socket_dev_resume(struct device *dev)
849 return __pcmcia_pm_op(dev, socket_late_resume);
852 static const struct dev_pm_ops pcmcia_socket_pm_ops = {
853 /* dev_resume may be called with IRQs enabled */
854 SET_SYSTEM_SLEEP_PM_OPS(NULL,
855 pcmcia_socket_dev_resume)
857 /* late suspend must be called with IRQs disabled */
858 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
859 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
860 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
862 /* early resume must be called with IRQs disabled */
863 .resume_noirq = pcmcia_socket_dev_resume_noirq,
864 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
865 .restore_noirq = pcmcia_socket_dev_resume_noirq,
868 #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
870 #else /* CONFIG_PM */
872 #define PCMCIA_SOCKET_CLASS_PM_OPS NULL
874 #endif /* CONFIG_PM */
876 struct class pcmcia_socket_class = {
877 .name = "pcmcia_socket",
878 .dev_uevent = pcmcia_socket_uevent,
879 .dev_release = pcmcia_release_socket,
880 .class_release = pcmcia_release_socket_class,
881 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
883 EXPORT_SYMBOL(pcmcia_socket_class);
886 static int __init init_pcmcia_cs(void)
888 init_completion(&pcmcia_unload);
889 return class_register(&pcmcia_socket_class);
892 static void __exit exit_pcmcia_cs(void)
894 class_unregister(&pcmcia_socket_class);
895 wait_for_completion(&pcmcia_unload);
898 subsys_initcall(init_pcmcia_cs);
899 module_exit(exit_pcmcia_cs);