[ARM] 3035/1: RISCOS compat code fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / cs.c
blobd5e76423a0ee89b15a3888e0b411f688e49b05fb
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/config.h>
20 #include <linux/string.h>
21 #include <linux/major.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/timer.h>
27 #include <linux/ioport.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/pci.h>
31 #include <linux/device.h>
32 #include <asm/system.h>
33 #include <asm/irq.h>
35 #define IN_CARD_SERVICES
36 #include <pcmcia/cs_types.h>
37 #include <pcmcia/ss.h>
38 #include <pcmcia/cs.h>
39 #include <pcmcia/bulkmem.h>
40 #include <pcmcia/cistpl.h>
41 #include <pcmcia/cisreg.h>
42 #include <pcmcia/ds.h>
43 #include "cs_internal.h"
46 /* Module parameters */
48 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
49 MODULE_DESCRIPTION("Linux Kernel Card Services");
50 MODULE_LICENSE("GPL");
52 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
54 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
55 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
56 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
57 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
58 INT_MODULE_PARM(reset_time, 10); /* usecs */
59 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
60 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
61 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
63 /* Access speed for attribute memory windows */
64 INT_MODULE_PARM(cis_speed, 300); /* ns */
66 #ifdef DEBUG
67 static int pc_debug;
69 module_param(pc_debug, int, 0644);
71 int cs_debug_level(int level)
73 return pc_debug > level;
75 #endif
78 socket_state_t dead_socket = {
79 .csc_mask = SS_DETECT,
81 EXPORT_SYMBOL(dead_socket);
84 /* List of all sockets, protected by a rwsem */
85 LIST_HEAD(pcmcia_socket_list);
86 EXPORT_SYMBOL(pcmcia_socket_list);
88 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
89 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
92 /**
93 * Low-level PCMCIA socket drivers need to register with the PCCard
94 * core using pcmcia_register_socket.
96 * socket drivers are expected to use the following callbacks in their
97 * .drv struct:
98 * - pcmcia_socket_dev_suspend
99 * - pcmcia_socket_dev_resume
100 * These functions check for the appropriate struct pcmcia_soket arrays,
101 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
103 static int socket_resume(struct pcmcia_socket *skt);
104 static int socket_suspend(struct pcmcia_socket *skt);
106 int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
108 struct pcmcia_socket *socket;
110 down_read(&pcmcia_socket_list_rwsem);
111 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
112 if (socket->dev.dev != dev)
113 continue;
114 down(&socket->skt_sem);
115 socket_suspend(socket);
116 up(&socket->skt_sem);
118 up_read(&pcmcia_socket_list_rwsem);
120 return 0;
122 EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
124 int pcmcia_socket_dev_resume(struct device *dev)
126 struct pcmcia_socket *socket;
128 down_read(&pcmcia_socket_list_rwsem);
129 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
130 if (socket->dev.dev != dev)
131 continue;
132 down(&socket->skt_sem);
133 socket_resume(socket);
134 up(&socket->skt_sem);
136 up_read(&pcmcia_socket_list_rwsem);
138 return 0;
140 EXPORT_SYMBOL(pcmcia_socket_dev_resume);
143 struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
145 struct class_device *cl_dev = class_device_get(&skt->dev);
146 if (!cl_dev)
147 return NULL;
148 skt = class_get_devdata(cl_dev);
149 if (!try_module_get(skt->owner)) {
150 class_device_put(&skt->dev);
151 return NULL;
153 return (skt);
155 EXPORT_SYMBOL(pcmcia_get_socket);
158 void pcmcia_put_socket(struct pcmcia_socket *skt)
160 module_put(skt->owner);
161 class_device_put(&skt->dev);
163 EXPORT_SYMBOL(pcmcia_put_socket);
166 static void pcmcia_release_socket(struct class_device *class_dev)
168 struct pcmcia_socket *socket = class_get_devdata(class_dev);
170 complete(&socket->socket_released);
173 static int pccardd(void *__skt);
176 * pcmcia_register_socket - add a new pcmcia socket device
178 int pcmcia_register_socket(struct pcmcia_socket *socket)
180 int ret;
182 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
183 return -EINVAL;
185 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
187 spin_lock_init(&socket->lock);
189 if (socket->resource_ops->init) {
190 ret = socket->resource_ops->init(socket);
191 if (ret)
192 return (ret);
195 /* try to obtain a socket number [yes, it gets ugly if we
196 * register more than 2^sizeof(unsigned int) pcmcia
197 * sockets... but the socket number is deprecated
198 * anyways, so I don't care] */
199 down_write(&pcmcia_socket_list_rwsem);
200 if (list_empty(&pcmcia_socket_list))
201 socket->sock = 0;
202 else {
203 unsigned int found, i = 1;
204 struct pcmcia_socket *tmp;
205 do {
206 found = 1;
207 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
208 if (tmp->sock == i)
209 found = 0;
211 i++;
212 } while (!found);
213 socket->sock = i - 1;
215 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
216 up_write(&pcmcia_socket_list_rwsem);
218 #ifndef CONFIG_CARDBUS
220 * If we do not support Cardbus, ensure that
221 * the Cardbus socket capability is disabled.
223 socket->features &= ~SS_CAP_CARDBUS;
224 #endif
226 /* set proper values in socket->dev */
227 socket->dev.class_data = socket;
228 socket->dev.class = &pcmcia_socket_class;
229 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
231 /* base address = 0, map = 0 */
232 socket->cis_mem.flags = 0;
233 socket->cis_mem.speed = cis_speed;
235 INIT_LIST_HEAD(&socket->cis_cache);
237 init_completion(&socket->socket_released);
238 init_completion(&socket->thread_done);
239 init_waitqueue_head(&socket->thread_wait);
240 init_MUTEX(&socket->skt_sem);
241 spin_lock_init(&socket->thread_lock);
243 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
244 if (ret < 0)
245 goto err;
247 wait_for_completion(&socket->thread_done);
248 if(!socket->thread) {
249 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
250 return -EIO;
252 pcmcia_parse_events(socket, SS_DETECT);
254 return 0;
256 err:
257 down_write(&pcmcia_socket_list_rwsem);
258 list_del(&socket->socket_list);
259 up_write(&pcmcia_socket_list_rwsem);
260 return ret;
261 } /* pcmcia_register_socket */
262 EXPORT_SYMBOL(pcmcia_register_socket);
266 * pcmcia_unregister_socket - remove a pcmcia socket device
268 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
270 if (!socket)
271 return;
273 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
275 if (socket->thread) {
276 init_completion(&socket->thread_done);
277 socket->thread = NULL;
278 wake_up(&socket->thread_wait);
279 wait_for_completion(&socket->thread_done);
281 release_cis_mem(socket);
283 /* remove from our own list */
284 down_write(&pcmcia_socket_list_rwsem);
285 list_del(&socket->socket_list);
286 up_write(&pcmcia_socket_list_rwsem);
288 /* wait for sysfs to drop all references */
289 release_resource_db(socket);
290 wait_for_completion(&socket->socket_released);
291 } /* pcmcia_unregister_socket */
292 EXPORT_SYMBOL(pcmcia_unregister_socket);
295 struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
297 struct pcmcia_socket *s;
299 down_read(&pcmcia_socket_list_rwsem);
300 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
301 if (s->sock == nr) {
302 up_read(&pcmcia_socket_list_rwsem);
303 return s;
305 up_read(&pcmcia_socket_list_rwsem);
307 return NULL;
310 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
314 * socket_setup() and shutdown_socket() are called by the main event
315 * handler when card insertion and removal events are received.
316 * socket_setup() turns on socket power and resets the socket, in two stages.
317 * shutdown_socket() unconfigures a socket and turns off socket power.
319 static void shutdown_socket(struct pcmcia_socket *s)
321 cs_dbg(s, 1, "shutdown_socket\n");
323 /* Blank out the socket state */
324 s->socket = dead_socket;
325 s->ops->init(s);
326 s->ops->set_socket(s, &s->socket);
327 s->irq.AssignedIRQ = s->irq.Config = 0;
328 s->lock_count = 0;
329 destroy_cis_cache(s);
330 #ifdef CONFIG_CARDBUS
331 cb_free(s);
332 #endif
333 s->functions = 0;
334 if (s->config) {
335 kfree(s->config);
336 s->config = NULL;
340 int status;
341 s->ops->get_status(s, &status);
342 if (status & SS_POWERON) {
343 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
346 } /* shutdown_socket */
350 * The central event handler. Send_event() sends an event to the
351 * 16-bit subsystem, which then calls the relevant device drivers.
352 * Parse_events() interprets the event bits from
353 * a card status change report. Do_shutdown() handles the high
354 * priority stuff associated with a card removal.
357 /* NOTE: send_event needs to be called with skt->sem held. */
359 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
361 int ret;
363 if (s->state & SOCKET_CARDBUS)
364 return 0;
366 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
367 event, priority, s->callback);
369 if (!s->callback)
370 return 0;
371 if (!try_module_get(s->callback->owner))
372 return 0;
374 ret = s->callback->event(s, event, priority);
376 module_put(s->callback->owner);
378 return ret;
381 static void socket_remove_drivers(struct pcmcia_socket *skt)
383 cs_dbg(skt, 4, "remove_drivers\n");
385 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
388 static void socket_shutdown(struct pcmcia_socket *skt)
390 cs_dbg(skt, 4, "shutdown\n");
392 socket_remove_drivers(skt);
393 skt->state &= SOCKET_INUSE|SOCKET_PRESENT;
394 msleep(shutdown_delay * 10);
395 skt->state &= SOCKET_INUSE;
396 shutdown_socket(skt);
399 static int socket_reset(struct pcmcia_socket *skt)
401 int status, i;
403 cs_dbg(skt, 4, "reset\n");
405 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
406 skt->ops->set_socket(skt, &skt->socket);
407 udelay((long)reset_time);
409 skt->socket.flags &= ~SS_RESET;
410 skt->ops->set_socket(skt, &skt->socket);
412 msleep(unreset_delay * 10);
413 for (i = 0; i < unreset_limit; i++) {
414 skt->ops->get_status(skt, &status);
416 if (!(status & SS_DETECT))
417 return CS_NO_CARD;
419 if (status & SS_READY)
420 return CS_SUCCESS;
422 msleep(unreset_check * 10);
425 cs_err(skt, "time out after reset.\n");
426 return CS_GENERAL_FAILURE;
429 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
431 int status, i;
433 cs_dbg(skt, 4, "setup\n");
435 skt->ops->get_status(skt, &status);
436 if (!(status & SS_DETECT))
437 return CS_NO_CARD;
439 msleep(initial_delay * 10);
441 for (i = 0; i < 100; i++) {
442 skt->ops->get_status(skt, &status);
443 if (!(status & SS_DETECT))
444 return CS_NO_CARD;
446 if (!(status & SS_PENDING))
447 break;
449 msleep(100);
452 if (status & SS_PENDING) {
453 cs_err(skt, "voltage interrogation timed out.\n");
454 return CS_GENERAL_FAILURE;
457 if (status & SS_CARDBUS) {
458 if (!(skt->features & SS_CAP_CARDBUS)) {
459 cs_err(skt, "cardbus cards are not supported.\n");
460 return CS_BAD_TYPE;
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 cs_err(skt, "unsupported voltage key.\n");
474 return CS_BAD_TYPE;
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 cs_err(skt, "unable to apply power.\n");
491 return CS_BAD_TYPE;
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 cs_dbg(skt, 4, "insert\n");
512 if (!cs_socket_get(skt))
513 return CS_NO_CARD;
515 ret = socket_setup(skt, setup_delay);
516 if (ret == CS_SUCCESS) {
517 skt->state |= SOCKET_PRESENT;
518 #ifdef CONFIG_CARDBUS
519 if (skt->state & SOCKET_CARDBUS) {
520 cb_alloc(skt);
521 skt->state |= SOCKET_CARDBUS_CONFIG;
523 #endif
524 cs_dbg(skt, 4, "insert done\n");
526 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
527 } else {
528 socket_shutdown(skt);
529 cs_socket_put(skt);
532 return ret;
535 static int socket_suspend(struct pcmcia_socket *skt)
537 if (skt->state & SOCKET_SUSPEND)
538 return CS_IN_USE;
540 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
541 skt->socket = dead_socket;
542 skt->ops->set_socket(skt, &skt->socket);
543 if (skt->ops->suspend)
544 skt->ops->suspend(skt);
545 skt->state |= SOCKET_SUSPEND;
547 return CS_SUCCESS;
551 * Resume a socket. If a card is present, verify its CIS against
552 * our cached copy. If they are different, the card has been
553 * replaced, and we need to tell the drivers.
555 static int socket_resume(struct pcmcia_socket *skt)
557 int ret;
559 if (!(skt->state & SOCKET_SUSPEND))
560 return CS_IN_USE;
562 skt->socket = dead_socket;
563 skt->ops->init(skt);
564 skt->ops->set_socket(skt, &skt->socket);
566 if (!(skt->state & SOCKET_PRESENT)) {
567 skt->state &= ~SOCKET_SUSPEND;
568 return socket_insert(skt);
571 ret = socket_setup(skt, resume_delay);
572 if (ret == CS_SUCCESS) {
574 * FIXME: need a better check here for cardbus cards.
576 if (verify_cis_cache(skt) != 0) {
577 cs_dbg(skt, 4, "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 cs_dbg(skt, 4, "cis matches cache\n");
589 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
591 } else {
592 socket_shutdown(skt);
593 cs_socket_put(skt);
596 skt->state &= ~SOCKET_SUSPEND;
598 return CS_SUCCESS;
601 static void socket_remove(struct pcmcia_socket *skt)
603 socket_shutdown(skt);
604 cs_socket_put(skt);
608 * Process a socket card detect status change.
610 * If we don't have a card already present, delay the detect event for
611 * about 20ms (to be on the safe side) before reading the socket status.
613 * Some i82365-based systems send multiple SS_DETECT events during card
614 * insertion, and the "card present" status bit seems to bounce. This
615 * will probably be true with GPIO-based card detection systems after
616 * the product has aged.
618 static void socket_detect_change(struct pcmcia_socket *skt)
620 if (!(skt->state & SOCKET_SUSPEND)) {
621 int status;
623 if (!(skt->state & SOCKET_PRESENT))
624 msleep(20);
626 skt->ops->get_status(skt, &status);
627 if ((skt->state & SOCKET_PRESENT) &&
628 !(status & SS_DETECT))
629 socket_remove(skt);
630 if (!(skt->state & SOCKET_PRESENT) &&
631 (status & SS_DETECT))
632 socket_insert(skt);
636 static int pccardd(void *__skt)
638 struct pcmcia_socket *skt = __skt;
639 DECLARE_WAITQUEUE(wait, current);
640 int ret;
642 daemonize("pccardd");
644 skt->thread = current;
645 skt->socket = dead_socket;
646 skt->ops->init(skt);
647 skt->ops->set_socket(skt, &skt->socket);
649 /* register with the device core */
650 ret = class_device_register(&skt->dev);
651 if (ret) {
652 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
653 skt);
654 skt->thread = NULL;
655 complete_and_exit(&skt->thread_done, 0);
658 add_wait_queue(&skt->thread_wait, &wait);
659 complete(&skt->thread_done);
661 for (;;) {
662 unsigned long flags;
663 unsigned int events;
665 set_current_state(TASK_INTERRUPTIBLE);
667 spin_lock_irqsave(&skt->thread_lock, flags);
668 events = skt->thread_events;
669 skt->thread_events = 0;
670 spin_unlock_irqrestore(&skt->thread_lock, flags);
672 if (events) {
673 down(&skt->skt_sem);
674 if (events & SS_DETECT)
675 socket_detect_change(skt);
676 if (events & SS_BATDEAD)
677 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
678 if (events & SS_BATWARN)
679 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
680 if (events & SS_READY)
681 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
682 up(&skt->skt_sem);
683 continue;
686 if (!skt->thread)
687 break;
689 schedule();
690 try_to_freeze();
692 /* make sure we are running before we exit */
693 set_current_state(TASK_RUNNING);
695 remove_wait_queue(&skt->thread_wait, &wait);
697 /* remove from the device core */
698 class_device_unregister(&skt->dev);
700 complete_and_exit(&skt->thread_done, 0);
704 * Yenta (at least) probes interrupts before registering the socket and
705 * starting the handler thread.
707 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
709 cs_dbg(s, 4, "parse_events: events %08x\n", events);
710 if (s->thread) {
711 spin_lock(&s->thread_lock);
712 s->thread_events |= events;
713 spin_unlock(&s->thread_lock);
715 wake_up(&s->thread_wait);
717 } /* pcmcia_parse_events */
718 EXPORT_SYMBOL(pcmcia_parse_events);
721 /* register pcmcia_callback */
722 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
724 int ret = 0;
726 /* s->skt_sem also protects s->callback */
727 down(&s->skt_sem);
729 if (c) {
730 /* registration */
731 if (s->callback) {
732 ret = -EBUSY;
733 goto err;
736 s->callback = c;
738 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
739 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
740 } else
741 s->callback = NULL;
742 err:
743 up(&s->skt_sem);
745 return ret;
747 EXPORT_SYMBOL(pccard_register_pcmcia);
750 /* I'm not sure which "reset" function this is supposed to use,
751 * but for now, it uses the low-level interface's reset, not the
752 * CIS register.
755 int pccard_reset_card(struct pcmcia_socket *skt)
757 int ret;
759 cs_dbg(skt, 1, "resetting socket\n");
761 down(&skt->skt_sem);
762 do {
763 if (!(skt->state & SOCKET_PRESENT)) {
764 ret = CS_NO_CARD;
765 break;
767 if (skt->state & SOCKET_SUSPEND) {
768 ret = CS_IN_USE;
769 break;
771 if (skt->state & SOCKET_CARDBUS) {
772 ret = CS_UNSUPPORTED_FUNCTION;
773 break;
776 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
777 if (ret == 0) {
778 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
779 if (socket_reset(skt) == CS_SUCCESS)
780 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
783 ret = CS_SUCCESS;
784 } while (0);
785 up(&skt->skt_sem);
787 return ret;
788 } /* reset_card */
789 EXPORT_SYMBOL(pccard_reset_card);
792 /* These shut down or wake up a socket. They are sort of user
793 * initiated versions of the APM suspend and resume actions.
795 int pcmcia_suspend_card(struct pcmcia_socket *skt)
797 int ret;
799 cs_dbg(skt, 1, "suspending socket\n");
801 down(&skt->skt_sem);
802 do {
803 if (!(skt->state & SOCKET_PRESENT)) {
804 ret = CS_NO_CARD;
805 break;
807 if (skt->state & SOCKET_CARDBUS) {
808 ret = CS_UNSUPPORTED_FUNCTION;
809 break;
811 ret = socket_suspend(skt);
812 } while (0);
813 up(&skt->skt_sem);
815 return ret;
816 } /* suspend_card */
817 EXPORT_SYMBOL(pcmcia_suspend_card);
820 int pcmcia_resume_card(struct pcmcia_socket *skt)
822 int ret;
824 cs_dbg(skt, 1, "waking up socket\n");
826 down(&skt->skt_sem);
827 do {
828 if (!(skt->state & SOCKET_PRESENT)) {
829 ret = CS_NO_CARD;
830 break;
832 if (skt->state & SOCKET_CARDBUS) {
833 ret = CS_UNSUPPORTED_FUNCTION;
834 break;
836 ret = socket_resume(skt);
837 } while (0);
838 up(&skt->skt_sem);
840 return ret;
841 } /* resume_card */
842 EXPORT_SYMBOL(pcmcia_resume_card);
845 /* These handle user requests to eject or insert a card. */
846 int pcmcia_eject_card(struct pcmcia_socket *skt)
848 int ret;
850 cs_dbg(skt, 1, "user eject request\n");
852 down(&skt->skt_sem);
853 do {
854 if (!(skt->state & SOCKET_PRESENT)) {
855 ret = -ENODEV;
856 break;
859 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
860 if (ret != 0) {
861 ret = -EINVAL;
862 break;
865 socket_remove(skt);
866 ret = 0;
867 } while (0);
868 up(&skt->skt_sem);
870 return ret;
871 } /* eject_card */
872 EXPORT_SYMBOL(pcmcia_eject_card);
875 int pcmcia_insert_card(struct pcmcia_socket *skt)
877 int ret;
879 cs_dbg(skt, 1, "user insert request\n");
881 down(&skt->skt_sem);
882 do {
883 if (skt->state & SOCKET_PRESENT) {
884 ret = -EBUSY;
885 break;
887 if (socket_insert(skt) == CS_NO_CARD) {
888 ret = -ENODEV;
889 break;
891 ret = 0;
892 } while (0);
893 up(&skt->skt_sem);
895 return ret;
896 } /* insert_card */
897 EXPORT_SYMBOL(pcmcia_insert_card);
900 static int pcmcia_socket_hotplug(struct class_device *dev, char **envp,
901 int num_envp, char *buffer, int buffer_size)
903 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
904 int i = 0, length = 0;
906 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
907 &length, "SOCKET_NO=%u", s->sock))
908 return -ENOMEM;
910 envp[i] = NULL;
912 return 0;
916 static struct completion pcmcia_unload;
918 static void pcmcia_release_socket_class(struct class *data)
920 complete(&pcmcia_unload);
924 struct class pcmcia_socket_class = {
925 .name = "pcmcia_socket",
926 .hotplug = pcmcia_socket_hotplug,
927 .release = pcmcia_release_socket,
928 .class_release = pcmcia_release_socket_class,
930 EXPORT_SYMBOL(pcmcia_socket_class);
933 static int __init init_pcmcia_cs(void)
935 int ret;
937 init_completion(&pcmcia_unload);
938 ret = class_register(&pcmcia_socket_class);
939 if (ret)
940 return (ret);
941 return class_interface_register(&pccard_sysfs_interface);
944 static void __exit exit_pcmcia_cs(void)
946 class_interface_unregister(&pccard_sysfs_interface);
947 class_unregister(&pcmcia_socket_class);
949 wait_for_completion(&pcmcia_unload);
952 subsys_initcall(init_pcmcia_cs);
953 module_exit(exit_pcmcia_cs);