Linux 2.6.33-rc8
[linux-2.6/lguest.git] / drivers / pcmcia / cs.c
blob6d6f82b38a68c9ffaf341504a3dbdbac882a5496
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/cs_types.h>
36 #include <pcmcia/ss.h>
37 #include <pcmcia/cs.h>
38 #include <pcmcia/cistpl.h>
39 #include <pcmcia/cisreg.h>
40 #include <pcmcia/ds.h>
41 #include "cs_internal.h"
44 /* Module parameters */
46 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
47 MODULE_DESCRIPTION("Linux Kernel Card Services");
48 MODULE_LICENSE("GPL");
50 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
52 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
53 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
54 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
55 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
56 INT_MODULE_PARM(reset_time, 10); /* usecs */
57 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
58 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
59 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
61 /* Access speed for attribute memory windows */
62 INT_MODULE_PARM(cis_speed, 300); /* ns */
65 socket_state_t dead_socket = {
66 .csc_mask = SS_DETECT,
68 EXPORT_SYMBOL(dead_socket);
71 /* List of all sockets, protected by a rwsem */
72 LIST_HEAD(pcmcia_socket_list);
73 EXPORT_SYMBOL(pcmcia_socket_list);
75 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
76 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
80 * Low-level PCMCIA socket drivers need to register with the PCCard
81 * core using pcmcia_register_socket.
83 * socket drivers are expected to use the following callbacks in their
84 * .drv struct:
85 * - pcmcia_socket_dev_suspend
86 * - pcmcia_socket_dev_resume
87 * These functions check for the appropriate struct pcmcia_soket arrays,
88 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
90 static int socket_early_resume(struct pcmcia_socket *skt);
91 static int socket_late_resume(struct pcmcia_socket *skt);
92 static int socket_resume(struct pcmcia_socket *skt);
93 static int socket_suspend(struct pcmcia_socket *skt);
95 static void pcmcia_socket_dev_run(struct device *dev,
96 int (*cb)(struct pcmcia_socket *))
98 struct pcmcia_socket *socket;
100 down_read(&pcmcia_socket_list_rwsem);
101 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
102 if (socket->dev.parent != dev)
103 continue;
104 mutex_lock(&socket->skt_mutex);
105 cb(socket);
106 mutex_unlock(&socket->skt_mutex);
108 up_read(&pcmcia_socket_list_rwsem);
111 int pcmcia_socket_dev_suspend(struct device *dev)
113 pcmcia_socket_dev_run(dev, socket_suspend);
114 return 0;
116 EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
118 void pcmcia_socket_dev_early_resume(struct device *dev)
120 pcmcia_socket_dev_run(dev, socket_early_resume);
122 EXPORT_SYMBOL(pcmcia_socket_dev_early_resume);
124 void pcmcia_socket_dev_late_resume(struct device *dev)
126 pcmcia_socket_dev_run(dev, socket_late_resume);
128 EXPORT_SYMBOL(pcmcia_socket_dev_late_resume);
130 int pcmcia_socket_dev_resume(struct device *dev)
132 pcmcia_socket_dev_run(dev, socket_resume);
133 return 0;
135 EXPORT_SYMBOL(pcmcia_socket_dev_resume);
138 struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
140 struct device *dev = get_device(&skt->dev);
141 if (!dev)
142 return NULL;
143 skt = dev_get_drvdata(dev);
144 if (!try_module_get(skt->owner)) {
145 put_device(&skt->dev);
146 return NULL;
148 return skt;
150 EXPORT_SYMBOL(pcmcia_get_socket);
153 void pcmcia_put_socket(struct pcmcia_socket *skt)
155 module_put(skt->owner);
156 put_device(&skt->dev);
158 EXPORT_SYMBOL(pcmcia_put_socket);
161 static void pcmcia_release_socket(struct device *dev)
163 struct pcmcia_socket *socket = dev_get_drvdata(dev);
165 complete(&socket->socket_released);
168 static int pccardd(void *__skt);
171 * pcmcia_register_socket - add a new pcmcia socket device
172 * @socket: the &socket to register
174 int pcmcia_register_socket(struct pcmcia_socket *socket)
176 struct task_struct *tsk;
177 int ret;
179 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
180 return -EINVAL;
182 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
184 spin_lock_init(&socket->lock);
186 /* try to obtain a socket number [yes, it gets ugly if we
187 * register more than 2^sizeof(unsigned int) pcmcia
188 * sockets... but the socket number is deprecated
189 * anyways, so I don't care] */
190 down_write(&pcmcia_socket_list_rwsem);
191 if (list_empty(&pcmcia_socket_list))
192 socket->sock = 0;
193 else {
194 unsigned int found, i = 1;
195 struct pcmcia_socket *tmp;
196 do {
197 found = 1;
198 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
199 if (tmp->sock == i)
200 found = 0;
202 i++;
203 } while (!found);
204 socket->sock = i - 1;
206 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
207 up_write(&pcmcia_socket_list_rwsem);
209 #ifndef CONFIG_CARDBUS
211 * If we do not support Cardbus, ensure that
212 * the Cardbus socket capability is disabled.
214 socket->features &= ~SS_CAP_CARDBUS;
215 #endif
217 /* set proper values in socket->dev */
218 dev_set_drvdata(&socket->dev, socket);
219 socket->dev.class = &pcmcia_socket_class;
220 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
222 /* base address = 0, map = 0 */
223 socket->cis_mem.flags = 0;
224 socket->cis_mem.speed = cis_speed;
226 INIT_LIST_HEAD(&socket->cis_cache);
228 init_completion(&socket->socket_released);
229 init_completion(&socket->thread_done);
230 mutex_init(&socket->skt_mutex);
231 spin_lock_init(&socket->thread_lock);
233 if (socket->resource_ops->init) {
234 ret = socket->resource_ops->init(socket);
235 if (ret)
236 goto err;
239 tsk = kthread_run(pccardd, socket, "pccardd");
240 if (IS_ERR(tsk)) {
241 ret = PTR_ERR(tsk);
242 goto err;
245 wait_for_completion(&socket->thread_done);
246 if (!socket->thread) {
247 dev_printk(KERN_WARNING, &socket->dev,
248 "PCMCIA: warning: socket thread did not start\n");
249 return -EIO;
252 pcmcia_parse_events(socket, SS_DETECT);
255 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
256 * If it fails, it doesn't matter -- we still have 32-bit CardBus
257 * support to offer, so this is not a failure mode.
259 request_module_nowait("pcmcia");
261 return 0;
263 err:
264 down_write(&pcmcia_socket_list_rwsem);
265 list_del(&socket->socket_list);
266 up_write(&pcmcia_socket_list_rwsem);
267 return ret;
268 } /* pcmcia_register_socket */
269 EXPORT_SYMBOL(pcmcia_register_socket);
273 * pcmcia_unregister_socket - remove a pcmcia socket device
274 * @socket: the &socket to unregister
276 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
278 if (!socket)
279 return;
281 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
283 if (socket->thread)
284 kthread_stop(socket->thread);
286 release_cis_mem(socket);
288 /* remove from our own list */
289 down_write(&pcmcia_socket_list_rwsem);
290 list_del(&socket->socket_list);
291 up_write(&pcmcia_socket_list_rwsem);
293 /* wait for sysfs to drop all references */
294 release_resource_db(socket);
295 wait_for_completion(&socket->socket_released);
296 } /* pcmcia_unregister_socket */
297 EXPORT_SYMBOL(pcmcia_unregister_socket);
300 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
302 struct pcmcia_socket *s;
304 down_read(&pcmcia_socket_list_rwsem);
305 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
306 if (s->sock == nr) {
307 up_read(&pcmcia_socket_list_rwsem);
308 return s;
310 up_read(&pcmcia_socket_list_rwsem);
312 return NULL;
315 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
318 * The central event handler. Send_event() sends an event to the
319 * 16-bit subsystem, which then calls the relevant device drivers.
320 * Parse_events() interprets the event bits from
321 * a card status change report. Do_shutdown() handles the high
322 * priority stuff associated with a card removal.
325 /* NOTE: send_event needs to be called with skt->sem held. */
327 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
329 int ret;
331 if (s->state & SOCKET_CARDBUS)
332 return 0;
334 dev_dbg(&s->dev, "send_event(event %d, pri %d, callback 0x%p)\n",
335 event, priority, s->callback);
337 if (!s->callback)
338 return 0;
339 if (!try_module_get(s->callback->owner))
340 return 0;
342 ret = s->callback->event(s, event, priority);
344 module_put(s->callback->owner);
346 return ret;
349 static void socket_remove_drivers(struct pcmcia_socket *skt)
351 dev_dbg(&skt->dev, "remove_drivers\n");
353 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
356 static int socket_reset(struct pcmcia_socket *skt)
358 int status, i;
360 dev_dbg(&skt->dev, "reset\n");
362 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
363 skt->ops->set_socket(skt, &skt->socket);
364 udelay((long)reset_time);
366 skt->socket.flags &= ~SS_RESET;
367 skt->ops->set_socket(skt, &skt->socket);
369 msleep(unreset_delay * 10);
370 for (i = 0; i < unreset_limit; i++) {
371 skt->ops->get_status(skt, &status);
373 if (!(status & SS_DETECT))
374 return -ENODEV;
376 if (status & SS_READY)
377 return 0;
379 msleep(unreset_check * 10);
382 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
383 return -ETIMEDOUT;
387 * socket_setup() and socket_shutdown() are called by the main event handler
388 * when card insertion and removal events are received.
389 * socket_setup() turns on socket power and resets the socket, in two stages.
390 * socket_shutdown() unconfigures a socket and turns off socket power.
392 static void socket_shutdown(struct pcmcia_socket *s)
394 int status;
396 dev_dbg(&s->dev, "shutdown\n");
398 socket_remove_drivers(s);
399 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
400 msleep(shutdown_delay * 10);
401 s->state &= SOCKET_INUSE;
403 /* Blank out the socket state */
404 s->socket = dead_socket;
405 s->ops->init(s);
406 s->ops->set_socket(s, &s->socket);
407 s->irq.AssignedIRQ = s->irq.Config = 0;
408 s->lock_count = 0;
409 destroy_cis_cache(s);
410 #ifdef CONFIG_CARDBUS
411 cb_free(s);
412 #endif
413 s->functions = 0;
415 /* give socket some time to power down */
416 msleep(100);
418 s->ops->get_status(s, &status);
419 if (status & SS_POWERON) {
420 dev_printk(KERN_ERR, &s->dev,
421 "*** DANGER *** unable to remove socket power\n");
424 cs_socket_put(s);
427 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
429 int status, i;
431 dev_dbg(&skt->dev, "setup\n");
433 skt->ops->get_status(skt, &status);
434 if (!(status & SS_DETECT))
435 return -ENODEV;
437 msleep(initial_delay * 10);
439 for (i = 0; i < 100; i++) {
440 skt->ops->get_status(skt, &status);
441 if (!(status & SS_DETECT))
442 return -ENODEV;
444 if (!(status & SS_PENDING))
445 break;
447 msleep(100);
450 if (status & SS_PENDING) {
451 dev_printk(KERN_ERR, &skt->dev,
452 "voltage interrogation timed out.\n");
453 return -ETIMEDOUT;
456 if (status & SS_CARDBUS) {
457 if (!(skt->features & SS_CAP_CARDBUS)) {
458 dev_printk(KERN_ERR, &skt->dev,
459 "cardbus cards are not supported.\n");
460 return -EINVAL;
462 skt->state |= SOCKET_CARDBUS;
466 * Decode the card voltage requirements, and apply power to the card.
468 if (status & SS_3VCARD)
469 skt->socket.Vcc = skt->socket.Vpp = 33;
470 else if (!(status & SS_XVCARD))
471 skt->socket.Vcc = skt->socket.Vpp = 50;
472 else {
473 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
474 return -EIO;
477 if (skt->power_hook)
478 skt->power_hook(skt, HOOK_POWER_PRE);
480 skt->socket.flags = 0;
481 skt->ops->set_socket(skt, &skt->socket);
484 * Wait "vcc_settle" for the supply to stabilise.
486 msleep(vcc_settle * 10);
488 skt->ops->get_status(skt, &status);
489 if (!(status & SS_POWERON)) {
490 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
491 return -EIO;
494 status = socket_reset(skt);
496 if (skt->power_hook)
497 skt->power_hook(skt, HOOK_POWER_POST);
499 return status;
503 * Handle card insertion. Setup the socket, reset the card,
504 * and then tell the rest of PCMCIA that a card is present.
506 static int socket_insert(struct pcmcia_socket *skt)
508 int ret;
510 dev_dbg(&skt->dev, "insert\n");
512 if (!cs_socket_get(skt))
513 return -ENODEV;
515 ret = socket_setup(skt, setup_delay);
516 if (ret == 0) {
517 skt->state |= SOCKET_PRESENT;
519 dev_printk(KERN_NOTICE, &skt->dev,
520 "pccard: %s card inserted into slot %d\n",
521 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
522 skt->sock);
524 #ifdef CONFIG_CARDBUS
525 if (skt->state & SOCKET_CARDBUS) {
526 cb_alloc(skt);
527 skt->state |= SOCKET_CARDBUS_CONFIG;
529 #endif
530 dev_dbg(&skt->dev, "insert done\n");
532 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
533 } else {
534 socket_shutdown(skt);
537 return ret;
540 static int socket_suspend(struct pcmcia_socket *skt)
542 if (skt->state & SOCKET_SUSPEND)
543 return -EBUSY;
545 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
546 skt->socket = dead_socket;
547 skt->ops->set_socket(skt, &skt->socket);
548 if (skt->ops->suspend)
549 skt->ops->suspend(skt);
550 skt->state |= SOCKET_SUSPEND;
552 return 0;
555 static int socket_early_resume(struct pcmcia_socket *skt)
557 skt->socket = dead_socket;
558 skt->ops->init(skt);
559 skt->ops->set_socket(skt, &skt->socket);
560 if (skt->state & SOCKET_PRESENT)
561 skt->resume_status = socket_setup(skt, resume_delay);
562 return 0;
565 static int socket_late_resume(struct pcmcia_socket *skt)
567 if (!(skt->state & SOCKET_PRESENT)) {
568 skt->state &= ~SOCKET_SUSPEND;
569 return socket_insert(skt);
572 if (skt->resume_status == 0) {
574 * FIXME: need a better check here for cardbus cards.
576 if (verify_cis_cache(skt) != 0) {
577 dev_dbg(&skt->dev, "cis mismatch - different card\n");
578 socket_remove_drivers(skt);
579 destroy_cis_cache(skt);
581 * Workaround: give DS time to schedule removal.
582 * Remove me once the 100ms delay is eliminated
583 * in ds.c
585 msleep(200);
586 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
587 } else {
588 dev_dbg(&skt->dev, "cis matches cache\n");
589 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
591 } else {
592 socket_shutdown(skt);
595 skt->state &= ~SOCKET_SUSPEND;
597 return 0;
601 * Resume a socket. If a card is present, verify its CIS against
602 * our cached copy. If they are different, the card has been
603 * replaced, and we need to tell the drivers.
605 static int socket_resume(struct pcmcia_socket *skt)
607 if (!(skt->state & SOCKET_SUSPEND))
608 return -EBUSY;
610 socket_early_resume(skt);
611 return socket_late_resume(skt);
614 static void socket_remove(struct pcmcia_socket *skt)
616 dev_printk(KERN_NOTICE, &skt->dev,
617 "pccard: card ejected from slot %d\n", skt->sock);
618 socket_shutdown(skt);
622 * Process a socket card detect status change.
624 * If we don't have a card already present, delay the detect event for
625 * about 20ms (to be on the safe side) before reading the socket status.
627 * Some i82365-based systems send multiple SS_DETECT events during card
628 * insertion, and the "card present" status bit seems to bounce. This
629 * will probably be true with GPIO-based card detection systems after
630 * the product has aged.
632 static void socket_detect_change(struct pcmcia_socket *skt)
634 if (!(skt->state & SOCKET_SUSPEND)) {
635 int status;
637 if (!(skt->state & SOCKET_PRESENT))
638 msleep(20);
640 skt->ops->get_status(skt, &status);
641 if ((skt->state & SOCKET_PRESENT) &&
642 !(status & SS_DETECT))
643 socket_remove(skt);
644 if (!(skt->state & SOCKET_PRESENT) &&
645 (status & SS_DETECT))
646 socket_insert(skt);
650 static int pccardd(void *__skt)
652 struct pcmcia_socket *skt = __skt;
653 int ret;
655 skt->thread = current;
656 skt->socket = dead_socket;
657 skt->ops->init(skt);
658 skt->ops->set_socket(skt, &skt->socket);
660 /* register with the device core */
661 ret = device_register(&skt->dev);
662 if (ret) {
663 dev_printk(KERN_WARNING, &skt->dev,
664 "PCMCIA: unable to register socket\n");
665 skt->thread = NULL;
666 complete(&skt->thread_done);
667 return 0;
669 ret = pccard_sysfs_add_socket(&skt->dev);
670 if (ret)
671 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
673 complete(&skt->thread_done);
675 set_freezable();
676 for (;;) {
677 unsigned long flags;
678 unsigned int events;
680 set_current_state(TASK_INTERRUPTIBLE);
682 spin_lock_irqsave(&skt->thread_lock, flags);
683 events = skt->thread_events;
684 skt->thread_events = 0;
685 spin_unlock_irqrestore(&skt->thread_lock, flags);
687 if (events) {
688 mutex_lock(&skt->skt_mutex);
689 if (events & SS_DETECT)
690 socket_detect_change(skt);
691 if (events & SS_BATDEAD)
692 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
693 if (events & SS_BATWARN)
694 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
695 if (events & SS_READY)
696 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
697 mutex_unlock(&skt->skt_mutex);
698 continue;
701 if (kthread_should_stop())
702 break;
704 schedule();
705 try_to_freeze();
707 /* make sure we are running before we exit */
708 set_current_state(TASK_RUNNING);
710 /* remove from the device core */
711 pccard_sysfs_remove_socket(&skt->dev);
712 device_unregister(&skt->dev);
714 return 0;
718 * Yenta (at least) probes interrupts before registering the socket and
719 * starting the handler thread.
721 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
723 unsigned long flags;
724 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
725 if (s->thread) {
726 spin_lock_irqsave(&s->thread_lock, flags);
727 s->thread_events |= events;
728 spin_unlock_irqrestore(&s->thread_lock, flags);
730 wake_up_process(s->thread);
732 } /* pcmcia_parse_events */
733 EXPORT_SYMBOL(pcmcia_parse_events);
736 /* register pcmcia_callback */
737 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
739 int ret = 0;
741 /* s->skt_mutex also protects s->callback */
742 mutex_lock(&s->skt_mutex);
744 if (c) {
745 /* registration */
746 if (s->callback) {
747 ret = -EBUSY;
748 goto err;
751 s->callback = c;
753 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
754 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
755 } else
756 s->callback = NULL;
757 err:
758 mutex_unlock(&s->skt_mutex);
760 return ret;
762 EXPORT_SYMBOL(pccard_register_pcmcia);
765 /* I'm not sure which "reset" function this is supposed to use,
766 * but for now, it uses the low-level interface's reset, not the
767 * CIS register.
770 int pcmcia_reset_card(struct pcmcia_socket *skt)
772 int ret;
774 dev_dbg(&skt->dev, "resetting socket\n");
776 mutex_lock(&skt->skt_mutex);
777 do {
778 if (!(skt->state & SOCKET_PRESENT)) {
779 dev_dbg(&skt->dev, "can't reset, not present\n");
780 ret = -ENODEV;
781 break;
783 if (skt->state & SOCKET_SUSPEND) {
784 dev_dbg(&skt->dev, "can't reset, suspended\n");
785 ret = -EBUSY;
786 break;
788 if (skt->state & SOCKET_CARDBUS) {
789 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
790 ret = -EPERM;
791 break;
794 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
795 if (ret == 0) {
796 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
797 if (skt->callback)
798 skt->callback->suspend(skt);
799 if (socket_reset(skt) == 0) {
800 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
801 if (skt->callback)
802 skt->callback->resume(skt);
806 ret = 0;
807 } while (0);
808 mutex_unlock(&skt->skt_mutex);
810 return ret;
811 } /* reset_card */
812 EXPORT_SYMBOL(pcmcia_reset_card);
815 /* These shut down or wake up a socket. They are sort of user
816 * initiated versions of the APM suspend and resume actions.
818 int pcmcia_suspend_card(struct pcmcia_socket *skt)
820 int ret;
822 dev_dbg(&skt->dev, "suspending socket\n");
824 mutex_lock(&skt->skt_mutex);
825 do {
826 if (!(skt->state & SOCKET_PRESENT)) {
827 ret = -ENODEV;
828 break;
830 if (skt->state & SOCKET_CARDBUS) {
831 ret = -EPERM;
832 break;
834 if (skt->callback) {
835 ret = skt->callback->suspend(skt);
836 if (ret)
837 break;
839 ret = socket_suspend(skt);
840 } while (0);
841 mutex_unlock(&skt->skt_mutex);
843 return ret;
844 } /* suspend_card */
845 EXPORT_SYMBOL(pcmcia_suspend_card);
848 int pcmcia_resume_card(struct pcmcia_socket *skt)
850 int ret;
852 dev_dbg(&skt->dev, "waking up socket\n");
854 mutex_lock(&skt->skt_mutex);
855 do {
856 if (!(skt->state & SOCKET_PRESENT)) {
857 ret = -ENODEV;
858 break;
860 if (skt->state & SOCKET_CARDBUS) {
861 ret = -EPERM;
862 break;
864 ret = socket_resume(skt);
865 if (!ret && skt->callback)
866 skt->callback->resume(skt);
867 } while (0);
868 mutex_unlock(&skt->skt_mutex);
870 return ret;
871 } /* resume_card */
872 EXPORT_SYMBOL(pcmcia_resume_card);
875 /* These handle user requests to eject or insert a card. */
876 int pcmcia_eject_card(struct pcmcia_socket *skt)
878 int ret;
880 dev_dbg(&skt->dev, "user eject request\n");
882 mutex_lock(&skt->skt_mutex);
883 do {
884 if (!(skt->state & SOCKET_PRESENT)) {
885 ret = -ENODEV;
886 break;
889 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
890 if (ret != 0) {
891 ret = -EINVAL;
892 break;
895 socket_remove(skt);
896 ret = 0;
897 } while (0);
898 mutex_unlock(&skt->skt_mutex);
900 return ret;
901 } /* eject_card */
902 EXPORT_SYMBOL(pcmcia_eject_card);
905 int pcmcia_insert_card(struct pcmcia_socket *skt)
907 int ret;
909 dev_dbg(&skt->dev, "user insert request\n");
911 mutex_lock(&skt->skt_mutex);
912 do {
913 if (skt->state & SOCKET_PRESENT) {
914 ret = -EBUSY;
915 break;
917 if (socket_insert(skt) == -ENODEV) {
918 ret = -ENODEV;
919 break;
921 ret = 0;
922 } while (0);
923 mutex_unlock(&skt->skt_mutex);
925 return ret;
926 } /* insert_card */
927 EXPORT_SYMBOL(pcmcia_insert_card);
930 static int pcmcia_socket_uevent(struct device *dev,
931 struct kobj_uevent_env *env)
933 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
935 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
936 return -ENOMEM;
938 return 0;
942 static struct completion pcmcia_unload;
944 static void pcmcia_release_socket_class(struct class *data)
946 complete(&pcmcia_unload);
950 struct class pcmcia_socket_class = {
951 .name = "pcmcia_socket",
952 .dev_uevent = pcmcia_socket_uevent,
953 .dev_release = pcmcia_release_socket,
954 .class_release = pcmcia_release_socket_class,
956 EXPORT_SYMBOL(pcmcia_socket_class);
959 static int __init init_pcmcia_cs(void)
961 init_completion(&pcmcia_unload);
962 return class_register(&pcmcia_socket_class);
965 static void __exit exit_pcmcia_cs(void)
967 class_unregister(&pcmcia_socket_class);
968 wait_for_completion(&pcmcia_unload);
971 subsys_initcall(init_pcmcia_cs);
972 module_exit(exit_pcmcia_cs);