ALSA: ac97: Apply quirk for Dell Latitude D610 binding Master and Headphone controls
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / cs.c
blob2ec8ac97445c1491d73102808de25dcf8e2c26bc
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/cs.h>
37 #include <pcmcia/cistpl.h>
38 #include <pcmcia/cisreg.h>
39 #include <pcmcia/ds.h>
40 #include "cs_internal.h"
43 /* Module parameters */
45 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
46 MODULE_DESCRIPTION("Linux Kernel Card Services");
47 MODULE_LICENSE("GPL");
49 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
51 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
52 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
53 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
54 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
55 INT_MODULE_PARM(reset_time, 10); /* usecs */
56 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
57 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
58 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
60 /* Access speed for attribute memory windows */
61 INT_MODULE_PARM(cis_speed, 300); /* ns */
64 socket_state_t dead_socket = {
65 .csc_mask = SS_DETECT,
67 EXPORT_SYMBOL(dead_socket);
70 /* List of all sockets, protected by a rwsem */
71 LIST_HEAD(pcmcia_socket_list);
72 EXPORT_SYMBOL(pcmcia_socket_list);
74 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
75 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
78 struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
80 struct device *dev = get_device(&skt->dev);
81 if (!dev)
82 return NULL;
83 return dev_get_drvdata(dev);
85 EXPORT_SYMBOL(pcmcia_get_socket);
88 void pcmcia_put_socket(struct pcmcia_socket *skt)
90 put_device(&skt->dev);
92 EXPORT_SYMBOL(pcmcia_put_socket);
95 static void pcmcia_release_socket(struct device *dev)
97 struct pcmcia_socket *socket = dev_get_drvdata(dev);
99 complete(&socket->socket_released);
102 static int pccardd(void *__skt);
105 * pcmcia_register_socket - add a new pcmcia socket device
106 * @socket: the &socket to register
108 int pcmcia_register_socket(struct pcmcia_socket *socket)
110 struct task_struct *tsk;
111 int ret;
113 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
114 return -EINVAL;
116 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
118 /* try to obtain a socket number [yes, it gets ugly if we
119 * register more than 2^sizeof(unsigned int) pcmcia
120 * sockets... but the socket number is deprecated
121 * anyways, so I don't care] */
122 down_write(&pcmcia_socket_list_rwsem);
123 if (list_empty(&pcmcia_socket_list))
124 socket->sock = 0;
125 else {
126 unsigned int found, i = 1;
127 struct pcmcia_socket *tmp;
128 do {
129 found = 1;
130 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
131 if (tmp->sock == i)
132 found = 0;
134 i++;
135 } while (!found);
136 socket->sock = i - 1;
138 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
139 up_write(&pcmcia_socket_list_rwsem);
141 #ifndef CONFIG_CARDBUS
143 * If we do not support Cardbus, ensure that
144 * the Cardbus socket capability is disabled.
146 socket->features &= ~SS_CAP_CARDBUS;
147 #endif
149 /* set proper values in socket->dev */
150 dev_set_drvdata(&socket->dev, socket);
151 socket->dev.class = &pcmcia_socket_class;
152 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
154 /* base address = 0, map = 0 */
155 socket->cis_mem.flags = 0;
156 socket->cis_mem.speed = cis_speed;
158 INIT_LIST_HEAD(&socket->cis_cache);
160 init_completion(&socket->socket_released);
161 init_completion(&socket->thread_done);
162 mutex_init(&socket->skt_mutex);
163 mutex_init(&socket->ops_mutex);
164 spin_lock_init(&socket->thread_lock);
166 if (socket->resource_ops->init) {
167 mutex_lock(&socket->ops_mutex);
168 ret = socket->resource_ops->init(socket);
169 mutex_unlock(&socket->ops_mutex);
170 if (ret)
171 goto err;
174 tsk = kthread_run(pccardd, socket, "pccardd");
175 if (IS_ERR(tsk)) {
176 ret = PTR_ERR(tsk);
177 goto err;
180 wait_for_completion(&socket->thread_done);
181 if (!socket->thread) {
182 dev_printk(KERN_WARNING, &socket->dev,
183 "PCMCIA: warning: socket thread did not start\n");
184 return -EIO;
187 pcmcia_parse_events(socket, SS_DETECT);
190 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
191 * If it fails, it doesn't matter -- we still have 32-bit CardBus
192 * support to offer, so this is not a failure mode.
194 request_module_nowait("pcmcia");
196 return 0;
198 err:
199 down_write(&pcmcia_socket_list_rwsem);
200 list_del(&socket->socket_list);
201 up_write(&pcmcia_socket_list_rwsem);
202 return ret;
203 } /* pcmcia_register_socket */
204 EXPORT_SYMBOL(pcmcia_register_socket);
208 * pcmcia_unregister_socket - remove a pcmcia socket device
209 * @socket: the &socket to unregister
211 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
213 if (!socket)
214 return;
216 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
218 if (socket->thread)
219 kthread_stop(socket->thread);
221 /* remove from our own list */
222 down_write(&pcmcia_socket_list_rwsem);
223 list_del(&socket->socket_list);
224 up_write(&pcmcia_socket_list_rwsem);
226 /* wait for sysfs to drop all references */
227 if (socket->resource_ops->exit) {
228 mutex_lock(&socket->ops_mutex);
229 socket->resource_ops->exit(socket);
230 mutex_unlock(&socket->ops_mutex);
232 wait_for_completion(&socket->socket_released);
233 } /* pcmcia_unregister_socket */
234 EXPORT_SYMBOL(pcmcia_unregister_socket);
237 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
239 struct pcmcia_socket *s;
241 down_read(&pcmcia_socket_list_rwsem);
242 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
243 if (s->sock == nr) {
244 up_read(&pcmcia_socket_list_rwsem);
245 return s;
247 up_read(&pcmcia_socket_list_rwsem);
249 return NULL;
252 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
254 static int socket_reset(struct pcmcia_socket *skt)
256 int status, i;
258 dev_dbg(&skt->dev, "reset\n");
260 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
261 skt->ops->set_socket(skt, &skt->socket);
262 udelay((long)reset_time);
264 skt->socket.flags &= ~SS_RESET;
265 skt->ops->set_socket(skt, &skt->socket);
267 msleep(unreset_delay * 10);
268 for (i = 0; i < unreset_limit; i++) {
269 skt->ops->get_status(skt, &status);
271 if (!(status & SS_DETECT))
272 return -ENODEV;
274 if (status & SS_READY)
275 return 0;
277 msleep(unreset_check * 10);
280 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
281 return -ETIMEDOUT;
285 * socket_setup() and socket_shutdown() are called by the main event handler
286 * when card insertion and removal events are received.
287 * socket_setup() turns on socket power and resets the socket, in two stages.
288 * socket_shutdown() unconfigures a socket and turns off socket power.
290 static void socket_shutdown(struct pcmcia_socket *s)
292 int status;
294 dev_dbg(&s->dev, "shutdown\n");
296 if (s->callback)
297 s->callback->remove(s);
299 mutex_lock(&s->ops_mutex);
300 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
301 msleep(shutdown_delay * 10);
302 s->state &= SOCKET_INUSE;
304 /* Blank out the socket state */
305 s->socket = dead_socket;
306 s->ops->init(s);
307 s->ops->set_socket(s, &s->socket);
308 s->lock_count = 0;
309 kfree(s->fake_cis);
310 s->fake_cis = NULL;
311 s->functions = 0;
313 /* From here on we can be sure that only we (that is, the
314 * pccardd thread) accesses this socket, and all (16-bit)
315 * PCMCIA interactions are gone. Therefore, release
316 * ops_mutex so that we don't get a sysfs-related lockdep
317 * warning.
319 mutex_unlock(&s->ops_mutex);
321 #ifdef CONFIG_CARDBUS
322 cb_free(s);
323 #endif
325 /* give socket some time to power down */
326 msleep(100);
328 s->ops->get_status(s, &status);
329 if (status & SS_POWERON) {
330 dev_printk(KERN_ERR, &s->dev,
331 "*** DANGER *** unable to remove socket power\n");
334 s->state &= ~SOCKET_INUSE;
337 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
339 int status, i;
341 dev_dbg(&skt->dev, "setup\n");
343 skt->ops->get_status(skt, &status);
344 if (!(status & SS_DETECT))
345 return -ENODEV;
347 msleep(initial_delay * 10);
349 for (i = 0; i < 100; i++) {
350 skt->ops->get_status(skt, &status);
351 if (!(status & SS_DETECT))
352 return -ENODEV;
354 if (!(status & SS_PENDING))
355 break;
357 msleep(100);
360 if (status & SS_PENDING) {
361 dev_printk(KERN_ERR, &skt->dev,
362 "voltage interrogation timed out.\n");
363 return -ETIMEDOUT;
366 if (status & SS_CARDBUS) {
367 if (!(skt->features & SS_CAP_CARDBUS)) {
368 dev_printk(KERN_ERR, &skt->dev,
369 "cardbus cards are not supported.\n");
370 return -EINVAL;
372 skt->state |= SOCKET_CARDBUS;
373 } else
374 skt->state &= ~SOCKET_CARDBUS;
377 * Decode the card voltage requirements, and apply power to the card.
379 if (status & SS_3VCARD)
380 skt->socket.Vcc = skt->socket.Vpp = 33;
381 else if (!(status & SS_XVCARD))
382 skt->socket.Vcc = skt->socket.Vpp = 50;
383 else {
384 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
385 return -EIO;
388 if (skt->power_hook)
389 skt->power_hook(skt, HOOK_POWER_PRE);
391 skt->socket.flags = 0;
392 skt->ops->set_socket(skt, &skt->socket);
395 * Wait "vcc_settle" for the supply to stabilise.
397 msleep(vcc_settle * 10);
399 skt->ops->get_status(skt, &status);
400 if (!(status & SS_POWERON)) {
401 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
402 return -EIO;
405 status = socket_reset(skt);
407 if (skt->power_hook)
408 skt->power_hook(skt, HOOK_POWER_POST);
410 return status;
414 * Handle card insertion. Setup the socket, reset the card,
415 * and then tell the rest of PCMCIA that a card is present.
417 static int socket_insert(struct pcmcia_socket *skt)
419 int ret;
421 dev_dbg(&skt->dev, "insert\n");
423 mutex_lock(&skt->ops_mutex);
424 if (skt->state & SOCKET_INUSE) {
425 mutex_unlock(&skt->ops_mutex);
426 return -EINVAL;
428 skt->state |= SOCKET_INUSE;
430 ret = socket_setup(skt, setup_delay);
431 if (ret == 0) {
432 skt->state |= SOCKET_PRESENT;
434 dev_printk(KERN_NOTICE, &skt->dev,
435 "pccard: %s card inserted into slot %d\n",
436 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
437 skt->sock);
439 #ifdef CONFIG_CARDBUS
440 if (skt->state & SOCKET_CARDBUS) {
441 cb_alloc(skt);
442 skt->state |= SOCKET_CARDBUS_CONFIG;
444 #endif
445 dev_dbg(&skt->dev, "insert done\n");
446 mutex_unlock(&skt->ops_mutex);
448 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
449 skt->callback->add(skt);
450 } else {
451 mutex_unlock(&skt->ops_mutex);
452 socket_shutdown(skt);
455 return ret;
458 static int socket_suspend(struct pcmcia_socket *skt)
460 if (skt->state & SOCKET_SUSPEND)
461 return -EBUSY;
463 mutex_lock(&skt->ops_mutex);
464 skt->suspended_state = skt->state;
466 skt->socket = dead_socket;
467 skt->ops->set_socket(skt, &skt->socket);
468 if (skt->ops->suspend)
469 skt->ops->suspend(skt);
470 skt->state |= SOCKET_SUSPEND;
471 mutex_unlock(&skt->ops_mutex);
472 return 0;
475 static int socket_early_resume(struct pcmcia_socket *skt)
477 mutex_lock(&skt->ops_mutex);
478 skt->socket = dead_socket;
479 skt->ops->init(skt);
480 skt->ops->set_socket(skt, &skt->socket);
481 if (skt->state & SOCKET_PRESENT)
482 skt->resume_status = socket_setup(skt, resume_delay);
483 mutex_unlock(&skt->ops_mutex);
484 return 0;
487 static int socket_late_resume(struct pcmcia_socket *skt)
489 int ret;
491 mutex_lock(&skt->ops_mutex);
492 skt->state &= ~SOCKET_SUSPEND;
493 mutex_unlock(&skt->ops_mutex);
495 if (!(skt->state & SOCKET_PRESENT)) {
496 ret = socket_insert(skt);
497 if (ret == -ENODEV)
498 ret = 0;
499 return ret;
502 if (skt->resume_status) {
503 socket_shutdown(skt);
504 return 0;
507 if (skt->suspended_state != skt->state) {
508 dev_dbg(&skt->dev,
509 "suspend state 0x%x != resume state 0x%x\n",
510 skt->suspended_state, skt->state);
512 socket_shutdown(skt);
513 return socket_insert(skt);
516 #ifdef CONFIG_CARDBUS
517 if (skt->state & SOCKET_CARDBUS) {
518 /* We can't be sure the CardBus card is the same
519 * as the one previously inserted. Therefore, remove
520 * and re-add... */
521 cb_free(skt);
522 cb_alloc(skt);
523 return 0;
525 #endif
526 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
527 skt->callback->early_resume(skt);
528 return 0;
532 * Resume a socket. If a card is present, verify its CIS against
533 * our cached copy. If they are different, the card has been
534 * replaced, and we need to tell the drivers.
536 static int socket_resume(struct pcmcia_socket *skt)
538 if (!(skt->state & SOCKET_SUSPEND))
539 return -EBUSY;
541 socket_early_resume(skt);
542 return socket_late_resume(skt);
545 static void socket_remove(struct pcmcia_socket *skt)
547 dev_printk(KERN_NOTICE, &skt->dev,
548 "pccard: card ejected from slot %d\n", skt->sock);
549 socket_shutdown(skt);
553 * Process a socket card detect status change.
555 * If we don't have a card already present, delay the detect event for
556 * about 20ms (to be on the safe side) before reading the socket status.
558 * Some i82365-based systems send multiple SS_DETECT events during card
559 * insertion, and the "card present" status bit seems to bounce. This
560 * will probably be true with GPIO-based card detection systems after
561 * the product has aged.
563 static void socket_detect_change(struct pcmcia_socket *skt)
565 if (!(skt->state & SOCKET_SUSPEND)) {
566 int status;
568 if (!(skt->state & SOCKET_PRESENT))
569 msleep(20);
571 skt->ops->get_status(skt, &status);
572 if ((skt->state & SOCKET_PRESENT) &&
573 !(status & SS_DETECT))
574 socket_remove(skt);
575 if (!(skt->state & SOCKET_PRESENT) &&
576 (status & SS_DETECT))
577 socket_insert(skt);
581 static int pccardd(void *__skt)
583 struct pcmcia_socket *skt = __skt;
584 int ret;
586 skt->thread = current;
587 skt->socket = dead_socket;
588 skt->ops->init(skt);
589 skt->ops->set_socket(skt, &skt->socket);
591 /* register with the device core */
592 ret = device_register(&skt->dev);
593 if (ret) {
594 dev_printk(KERN_WARNING, &skt->dev,
595 "PCMCIA: unable to register socket\n");
596 skt->thread = NULL;
597 complete(&skt->thread_done);
598 return 0;
600 ret = pccard_sysfs_add_socket(&skt->dev);
601 if (ret)
602 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
604 complete(&skt->thread_done);
606 /* wait for userspace to catch up */
607 msleep(250);
609 set_freezable();
610 for (;;) {
611 unsigned long flags;
612 unsigned int events;
613 unsigned int sysfs_events;
615 set_current_state(TASK_INTERRUPTIBLE);
617 spin_lock_irqsave(&skt->thread_lock, flags);
618 events = skt->thread_events;
619 skt->thread_events = 0;
620 sysfs_events = skt->sysfs_events;
621 skt->sysfs_events = 0;
622 spin_unlock_irqrestore(&skt->thread_lock, flags);
624 mutex_lock(&skt->skt_mutex);
625 if (events & SS_DETECT)
626 socket_detect_change(skt);
628 if (sysfs_events) {
629 if (sysfs_events & PCMCIA_UEVENT_EJECT)
630 socket_remove(skt);
631 if (sysfs_events & PCMCIA_UEVENT_INSERT)
632 socket_insert(skt);
633 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
634 !(skt->state & SOCKET_CARDBUS)) {
635 if (skt->callback)
636 ret = skt->callback->suspend(skt);
637 else
638 ret = 0;
639 if (!ret) {
640 socket_suspend(skt);
641 msleep(100);
644 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
645 !(skt->state & SOCKET_CARDBUS)) {
646 ret = socket_resume(skt);
647 if (!ret && skt->callback)
648 skt->callback->resume(skt);
650 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
651 !(skt->state & SOCKET_CARDBUS)) {
652 if (!ret && skt->callback)
653 skt->callback->requery(skt);
656 mutex_unlock(&skt->skt_mutex);
658 if (events || sysfs_events)
659 continue;
661 if (kthread_should_stop())
662 break;
664 schedule();
665 try_to_freeze();
667 /* make sure we are running before we exit */
668 set_current_state(TASK_RUNNING);
670 /* shut down socket, if a device is still present */
671 if (skt->state & SOCKET_PRESENT) {
672 mutex_lock(&skt->skt_mutex);
673 socket_remove(skt);
674 mutex_unlock(&skt->skt_mutex);
677 /* remove from the device core */
678 pccard_sysfs_remove_socket(&skt->dev);
679 device_unregister(&skt->dev);
681 return 0;
685 * Yenta (at least) probes interrupts before registering the socket and
686 * starting the handler thread.
688 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
690 unsigned long flags;
691 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
692 if (s->thread) {
693 spin_lock_irqsave(&s->thread_lock, flags);
694 s->thread_events |= events;
695 spin_unlock_irqrestore(&s->thread_lock, flags);
697 wake_up_process(s->thread);
699 } /* pcmcia_parse_events */
700 EXPORT_SYMBOL(pcmcia_parse_events);
703 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
704 * @s: the PCMCIA socket we wan't to command
705 * @events: events to pass to pccardd
707 * userspace-issued insert, eject, suspend and resume commands must be
708 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
709 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
710 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
711 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
713 void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
715 unsigned long flags;
716 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
717 if (s->thread) {
718 spin_lock_irqsave(&s->thread_lock, flags);
719 s->sysfs_events |= events;
720 spin_unlock_irqrestore(&s->thread_lock, flags);
722 wake_up_process(s->thread);
725 EXPORT_SYMBOL(pcmcia_parse_uevents);
728 /* register pcmcia_callback */
729 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
731 int ret = 0;
733 /* s->skt_mutex also protects s->callback */
734 mutex_lock(&s->skt_mutex);
736 if (c) {
737 /* registration */
738 if (s->callback) {
739 ret = -EBUSY;
740 goto err;
743 s->callback = c;
745 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
746 s->callback->add(s);
747 } else
748 s->callback = NULL;
749 err:
750 mutex_unlock(&s->skt_mutex);
752 return ret;
754 EXPORT_SYMBOL(pccard_register_pcmcia);
757 /* I'm not sure which "reset" function this is supposed to use,
758 * but for now, it uses the low-level interface's reset, not the
759 * CIS register.
762 int pcmcia_reset_card(struct pcmcia_socket *skt)
764 int ret;
766 dev_dbg(&skt->dev, "resetting socket\n");
768 mutex_lock(&skt->skt_mutex);
769 do {
770 if (!(skt->state & SOCKET_PRESENT)) {
771 dev_dbg(&skt->dev, "can't reset, not present\n");
772 ret = -ENODEV;
773 break;
775 if (skt->state & SOCKET_SUSPEND) {
776 dev_dbg(&skt->dev, "can't reset, suspended\n");
777 ret = -EBUSY;
778 break;
780 if (skt->state & SOCKET_CARDBUS) {
781 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
782 ret = -EPERM;
783 break;
786 if (skt->callback)
787 skt->callback->suspend(skt);
788 mutex_lock(&skt->ops_mutex);
789 ret = socket_reset(skt);
790 mutex_unlock(&skt->ops_mutex);
791 if ((ret == 0) && (skt->callback))
792 skt->callback->resume(skt);
794 ret = 0;
795 } while (0);
796 mutex_unlock(&skt->skt_mutex);
798 return ret;
799 } /* reset_card */
800 EXPORT_SYMBOL(pcmcia_reset_card);
803 static int pcmcia_socket_uevent(struct device *dev,
804 struct kobj_uevent_env *env)
806 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
808 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
809 return -ENOMEM;
811 return 0;
815 static struct completion pcmcia_unload;
817 static void pcmcia_release_socket_class(struct class *data)
819 complete(&pcmcia_unload);
823 #ifdef CONFIG_PM
825 static int __pcmcia_pm_op(struct device *dev,
826 int (*callback) (struct pcmcia_socket *skt))
828 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
829 int ret;
831 mutex_lock(&s->skt_mutex);
832 ret = callback(s);
833 mutex_unlock(&s->skt_mutex);
835 return ret;
838 static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
840 return __pcmcia_pm_op(dev, socket_suspend);
843 static int pcmcia_socket_dev_resume_noirq(struct device *dev)
845 return __pcmcia_pm_op(dev, socket_early_resume);
848 static int pcmcia_socket_dev_resume(struct device *dev)
850 return __pcmcia_pm_op(dev, socket_late_resume);
853 static const struct dev_pm_ops pcmcia_socket_pm_ops = {
854 /* dev_resume may be called with IRQs enabled */
855 SET_SYSTEM_SLEEP_PM_OPS(NULL,
856 pcmcia_socket_dev_resume)
858 /* late suspend must be called with IRQs disabled */
859 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
860 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
861 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
863 /* early resume must be called with IRQs disabled */
864 .resume_noirq = pcmcia_socket_dev_resume_noirq,
865 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
866 .restore_noirq = pcmcia_socket_dev_resume_noirq,
869 #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
871 #else /* CONFIG_PM */
873 #define PCMCIA_SOCKET_CLASS_PM_OPS NULL
875 #endif /* CONFIG_PM */
877 struct class pcmcia_socket_class = {
878 .name = "pcmcia_socket",
879 .dev_uevent = pcmcia_socket_uevent,
880 .dev_release = pcmcia_release_socket,
881 .class_release = pcmcia_release_socket_class,
882 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
884 EXPORT_SYMBOL(pcmcia_socket_class);
887 static int __init init_pcmcia_cs(void)
889 init_completion(&pcmcia_unload);
890 return class_register(&pcmcia_socket_class);
893 static void __exit exit_pcmcia_cs(void)
895 class_unregister(&pcmcia_socket_class);
896 wait_for_completion(&pcmcia_unload);
899 subsys_initcall(init_pcmcia_cs);
900 module_exit(exit_pcmcia_cs);