[PATCH] pcmcia: remove references to pcmcia/version.h
[linux-2.6/mini2440.git] / drivers / pcmcia / cs.c
blob4d1cc5304e173d814ced277746b77bc95e712aec
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);
219 /* set proper values in socket->dev */
220 socket->dev.class_data = socket;
221 socket->dev.class = &pcmcia_socket_class;
222 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
224 /* base address = 0, map = 0 */
225 socket->cis_mem.flags = 0;
226 socket->cis_mem.speed = cis_speed;
228 INIT_LIST_HEAD(&socket->cis_cache);
230 init_completion(&socket->socket_released);
231 init_completion(&socket->thread_done);
232 init_waitqueue_head(&socket->thread_wait);
233 init_MUTEX(&socket->skt_sem);
234 spin_lock_init(&socket->thread_lock);
236 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
237 if (ret < 0)
238 goto err;
240 wait_for_completion(&socket->thread_done);
241 if(!socket->thread) {
242 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
243 return -EIO;
245 pcmcia_parse_events(socket, SS_DETECT);
247 return 0;
249 err:
250 down_write(&pcmcia_socket_list_rwsem);
251 list_del(&socket->socket_list);
252 up_write(&pcmcia_socket_list_rwsem);
253 return ret;
254 } /* pcmcia_register_socket */
255 EXPORT_SYMBOL(pcmcia_register_socket);
259 * pcmcia_unregister_socket - remove a pcmcia socket device
261 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
263 if (!socket)
264 return;
266 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
268 if (socket->thread) {
269 init_completion(&socket->thread_done);
270 socket->thread = NULL;
271 wake_up(&socket->thread_wait);
272 wait_for_completion(&socket->thread_done);
274 release_cis_mem(socket);
276 /* remove from our own list */
277 down_write(&pcmcia_socket_list_rwsem);
278 list_del(&socket->socket_list);
279 up_write(&pcmcia_socket_list_rwsem);
281 /* wait for sysfs to drop all references */
282 release_resource_db(socket);
283 wait_for_completion(&socket->socket_released);
284 } /* pcmcia_unregister_socket */
285 EXPORT_SYMBOL(pcmcia_unregister_socket);
288 struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
290 struct pcmcia_socket *s;
292 down_read(&pcmcia_socket_list_rwsem);
293 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
294 if (s->sock == nr) {
295 up_read(&pcmcia_socket_list_rwsem);
296 return s;
298 up_read(&pcmcia_socket_list_rwsem);
300 return NULL;
303 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
307 * socket_setup() and shutdown_socket() are called by the main event
308 * handler when card insertion and removal events are received.
309 * socket_setup() turns on socket power and resets the socket, in two stages.
310 * shutdown_socket() unconfigures a socket and turns off socket power.
312 static void shutdown_socket(struct pcmcia_socket *s)
314 cs_dbg(s, 1, "shutdown_socket\n");
316 /* Blank out the socket state */
317 s->socket = dead_socket;
318 s->ops->init(s);
319 s->ops->set_socket(s, &s->socket);
320 s->irq.AssignedIRQ = s->irq.Config = 0;
321 s->lock_count = 0;
322 destroy_cis_cache(s);
323 #ifdef CONFIG_CARDBUS
324 cb_free(s);
325 #endif
326 s->functions = 0;
327 if (s->config) {
328 kfree(s->config);
329 s->config = NULL;
333 int status;
334 s->ops->get_status(s, &status);
335 if (status & SS_POWERON) {
336 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
339 } /* shutdown_socket */
343 * The central event handler. Send_event() sends an event to the
344 * 16-bit subsystem, which then calls the relevant device drivers.
345 * Parse_events() interprets the event bits from
346 * a card status change report. Do_shutdown() handles the high
347 * priority stuff associated with a card removal.
350 /* NOTE: send_event needs to be called with skt->sem held. */
352 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
354 int ret;
356 if (s->state & SOCKET_CARDBUS)
357 return 0;
359 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
360 event, priority, s->callback);
362 if (!s->callback)
363 return 0;
364 if (!try_module_get(s->callback->owner))
365 return 0;
367 ret = s->callback->event(s, event, priority);
369 module_put(s->callback->owner);
371 return ret;
374 static void socket_remove_drivers(struct pcmcia_socket *skt)
376 cs_dbg(skt, 4, "remove_drivers\n");
378 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
381 static void socket_shutdown(struct pcmcia_socket *skt)
383 cs_dbg(skt, 4, "shutdown\n");
385 socket_remove_drivers(skt);
386 skt->state &= SOCKET_INUSE|SOCKET_PRESENT;
387 msleep(shutdown_delay * 10);
388 skt->state &= SOCKET_INUSE;
389 shutdown_socket(skt);
392 static int socket_reset(struct pcmcia_socket *skt)
394 int status, i;
396 cs_dbg(skt, 4, "reset\n");
398 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
399 skt->ops->set_socket(skt, &skt->socket);
400 udelay((long)reset_time);
402 skt->socket.flags &= ~SS_RESET;
403 skt->ops->set_socket(skt, &skt->socket);
405 msleep(unreset_delay * 10);
406 for (i = 0; i < unreset_limit; i++) {
407 skt->ops->get_status(skt, &status);
409 if (!(status & SS_DETECT))
410 return CS_NO_CARD;
412 if (status & SS_READY)
413 return CS_SUCCESS;
415 msleep(unreset_check * 10);
418 cs_err(skt, "time out after reset.\n");
419 return CS_GENERAL_FAILURE;
422 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
424 int status, i;
426 cs_dbg(skt, 4, "setup\n");
428 skt->ops->get_status(skt, &status);
429 if (!(status & SS_DETECT))
430 return CS_NO_CARD;
432 msleep(initial_delay * 10);
434 for (i = 0; i < 100; i++) {
435 skt->ops->get_status(skt, &status);
436 if (!(status & SS_DETECT))
437 return CS_NO_CARD;
439 if (!(status & SS_PENDING))
440 break;
442 msleep(100);
445 if (status & SS_PENDING) {
446 cs_err(skt, "voltage interrogation timed out.\n");
447 return CS_GENERAL_FAILURE;
450 if (status & SS_CARDBUS) {
451 skt->state |= SOCKET_CARDBUS;
452 #ifndef CONFIG_CARDBUS
453 cs_err(skt, "cardbus cards are not supported.\n");
454 return CS_BAD_TYPE;
455 #endif
459 * Decode the card voltage requirements, and apply power to the card.
461 if (status & SS_3VCARD)
462 skt->socket.Vcc = skt->socket.Vpp = 33;
463 else if (!(status & SS_XVCARD))
464 skt->socket.Vcc = skt->socket.Vpp = 50;
465 else {
466 cs_err(skt, "unsupported voltage key.\n");
467 return CS_BAD_TYPE;
470 if (skt->power_hook)
471 skt->power_hook(skt, HOOK_POWER_PRE);
473 skt->socket.flags = 0;
474 skt->ops->set_socket(skt, &skt->socket);
477 * Wait "vcc_settle" for the supply to stabilise.
479 msleep(vcc_settle * 10);
481 skt->ops->get_status(skt, &status);
482 if (!(status & SS_POWERON)) {
483 cs_err(skt, "unable to apply power.\n");
484 return CS_BAD_TYPE;
487 status = socket_reset(skt);
489 if (skt->power_hook)
490 skt->power_hook(skt, HOOK_POWER_POST);
492 return status;
496 * Handle card insertion. Setup the socket, reset the card,
497 * and then tell the rest of PCMCIA that a card is present.
499 static int socket_insert(struct pcmcia_socket *skt)
501 int ret;
503 cs_dbg(skt, 4, "insert\n");
505 if (!cs_socket_get(skt))
506 return CS_NO_CARD;
508 ret = socket_setup(skt, setup_delay);
509 if (ret == CS_SUCCESS) {
510 skt->state |= SOCKET_PRESENT;
511 #ifdef CONFIG_CARDBUS
512 if (skt->state & SOCKET_CARDBUS) {
513 cb_alloc(skt);
514 skt->state |= SOCKET_CARDBUS_CONFIG;
516 #endif
517 cs_dbg(skt, 4, "insert done\n");
519 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
520 } else {
521 socket_shutdown(skt);
522 cs_socket_put(skt);
525 return ret;
528 static int socket_suspend(struct pcmcia_socket *skt)
530 if (skt->state & SOCKET_SUSPEND)
531 return CS_IN_USE;
533 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
534 skt->socket = dead_socket;
535 skt->ops->set_socket(skt, &skt->socket);
536 if (skt->ops->suspend)
537 skt->ops->suspend(skt);
538 skt->state |= SOCKET_SUSPEND;
540 return CS_SUCCESS;
544 * Resume a socket. If a card is present, verify its CIS against
545 * our cached copy. If they are different, the card has been
546 * replaced, and we need to tell the drivers.
548 static int socket_resume(struct pcmcia_socket *skt)
550 int ret;
552 if (!(skt->state & SOCKET_SUSPEND))
553 return CS_IN_USE;
555 skt->socket = dead_socket;
556 skt->ops->init(skt);
557 skt->ops->set_socket(skt, &skt->socket);
559 if (!(skt->state & SOCKET_PRESENT)) {
560 skt->state &= ~SOCKET_SUSPEND;
561 return socket_insert(skt);
564 ret = socket_setup(skt, resume_delay);
565 if (ret == CS_SUCCESS) {
567 * FIXME: need a better check here for cardbus cards.
569 if (verify_cis_cache(skt) != 0) {
570 cs_dbg(skt, 4, "cis mismatch - different card\n");
571 socket_remove_drivers(skt);
572 destroy_cis_cache(skt);
574 * Workaround: give DS time to schedule removal.
575 * Remove me once the 100ms delay is eliminated
576 * in ds.c
578 msleep(200);
579 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
580 } else {
581 cs_dbg(skt, 4, "cis matches cache\n");
582 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
584 } else {
585 socket_shutdown(skt);
586 cs_socket_put(skt);
589 skt->state &= ~SOCKET_SUSPEND;
591 return CS_SUCCESS;
594 static void socket_remove(struct pcmcia_socket *skt)
596 socket_shutdown(skt);
597 cs_socket_put(skt);
601 * Process a socket card detect status change.
603 * If we don't have a card already present, delay the detect event for
604 * about 20ms (to be on the safe side) before reading the socket status.
606 * Some i82365-based systems send multiple SS_DETECT events during card
607 * insertion, and the "card present" status bit seems to bounce. This
608 * will probably be true with GPIO-based card detection systems after
609 * the product has aged.
611 static void socket_detect_change(struct pcmcia_socket *skt)
613 if (!(skt->state & SOCKET_SUSPEND)) {
614 int status;
616 if (!(skt->state & SOCKET_PRESENT))
617 msleep(20);
619 skt->ops->get_status(skt, &status);
620 if ((skt->state & SOCKET_PRESENT) &&
621 !(status & SS_DETECT))
622 socket_remove(skt);
623 if (!(skt->state & SOCKET_PRESENT) &&
624 (status & SS_DETECT))
625 socket_insert(skt);
629 static int pccardd(void *__skt)
631 struct pcmcia_socket *skt = __skt;
632 DECLARE_WAITQUEUE(wait, current);
633 int ret;
635 daemonize("pccardd");
637 skt->thread = current;
638 skt->socket = dead_socket;
639 skt->ops->init(skt);
640 skt->ops->set_socket(skt, &skt->socket);
642 /* register with the device core */
643 ret = class_device_register(&skt->dev);
644 if (ret) {
645 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
646 skt);
647 skt->thread = NULL;
648 complete_and_exit(&skt->thread_done, 0);
650 complete(&skt->thread_done);
652 add_wait_queue(&skt->thread_wait, &wait);
653 for (;;) {
654 unsigned long flags;
655 unsigned int events;
657 set_current_state(TASK_INTERRUPTIBLE);
659 spin_lock_irqsave(&skt->thread_lock, flags);
660 events = skt->thread_events;
661 skt->thread_events = 0;
662 spin_unlock_irqrestore(&skt->thread_lock, flags);
664 if (events) {
665 down(&skt->skt_sem);
666 if (events & SS_DETECT)
667 socket_detect_change(skt);
668 if (events & SS_BATDEAD)
669 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
670 if (events & SS_BATWARN)
671 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
672 if (events & SS_READY)
673 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
674 up(&skt->skt_sem);
675 continue;
678 schedule();
679 try_to_freeze();
681 if (!skt->thread)
682 break;
684 remove_wait_queue(&skt->thread_wait, &wait);
686 /* remove from the device core */
687 class_device_unregister(&skt->dev);
689 complete_and_exit(&skt->thread_done, 0);
693 * Yenta (at least) probes interrupts before registering the socket and
694 * starting the handler thread.
696 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
698 cs_dbg(s, 4, "parse_events: events %08x\n", events);
699 if (s->thread) {
700 spin_lock(&s->thread_lock);
701 s->thread_events |= events;
702 spin_unlock(&s->thread_lock);
704 wake_up(&s->thread_wait);
706 } /* pcmcia_parse_events */
707 EXPORT_SYMBOL(pcmcia_parse_events);
710 /* register pcmcia_callback */
711 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
713 int ret = 0;
715 /* s->skt_sem also protects s->callback */
716 down(&s->skt_sem);
718 if (c) {
719 /* registration */
720 if (s->callback) {
721 ret = -EBUSY;
722 goto err;
725 s->callback = c;
727 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
728 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
729 } else
730 s->callback = NULL;
731 err:
732 up(&s->skt_sem);
734 return ret;
736 EXPORT_SYMBOL(pccard_register_pcmcia);
739 /* I'm not sure which "reset" function this is supposed to use,
740 * but for now, it uses the low-level interface's reset, not the
741 * CIS register.
744 int pccard_reset_card(struct pcmcia_socket *skt)
746 int ret;
748 cs_dbg(skt, 1, "resetting socket\n");
750 down(&skt->skt_sem);
751 do {
752 if (!(skt->state & SOCKET_PRESENT)) {
753 ret = CS_NO_CARD;
754 break;
756 if (skt->state & SOCKET_SUSPEND) {
757 ret = CS_IN_USE;
758 break;
760 if (skt->state & SOCKET_CARDBUS) {
761 ret = CS_UNSUPPORTED_FUNCTION;
762 break;
765 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
766 if (ret == 0) {
767 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
768 if (socket_reset(skt) == CS_SUCCESS)
769 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
772 ret = CS_SUCCESS;
773 } while (0);
774 up(&skt->skt_sem);
776 return ret;
777 } /* reset_card */
778 EXPORT_SYMBOL(pccard_reset_card);
781 /* These shut down or wake up a socket. They are sort of user
782 * initiated versions of the APM suspend and resume actions.
784 int pcmcia_suspend_card(struct pcmcia_socket *skt)
786 int ret;
788 cs_dbg(skt, 1, "suspending socket\n");
790 down(&skt->skt_sem);
791 do {
792 if (!(skt->state & SOCKET_PRESENT)) {
793 ret = CS_NO_CARD;
794 break;
796 if (skt->state & SOCKET_CARDBUS) {
797 ret = CS_UNSUPPORTED_FUNCTION;
798 break;
800 ret = socket_suspend(skt);
801 } while (0);
802 up(&skt->skt_sem);
804 return ret;
805 } /* suspend_card */
806 EXPORT_SYMBOL(pcmcia_suspend_card);
809 int pcmcia_resume_card(struct pcmcia_socket *skt)
811 int ret;
813 cs_dbg(skt, 1, "waking up socket\n");
815 down(&skt->skt_sem);
816 do {
817 if (!(skt->state & SOCKET_PRESENT)) {
818 ret = CS_NO_CARD;
819 break;
821 if (skt->state & SOCKET_CARDBUS) {
822 ret = CS_UNSUPPORTED_FUNCTION;
823 break;
825 ret = socket_resume(skt);
826 } while (0);
827 up(&skt->skt_sem);
829 return ret;
830 } /* resume_card */
831 EXPORT_SYMBOL(pcmcia_resume_card);
834 /* These handle user requests to eject or insert a card. */
835 int pcmcia_eject_card(struct pcmcia_socket *skt)
837 int ret;
839 cs_dbg(skt, 1, "user eject request\n");
841 down(&skt->skt_sem);
842 do {
843 if (!(skt->state & SOCKET_PRESENT)) {
844 ret = -ENODEV;
845 break;
848 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
849 if (ret != 0) {
850 ret = -EINVAL;
851 break;
854 socket_remove(skt);
855 ret = 0;
856 } while (0);
857 up(&skt->skt_sem);
859 return ret;
860 } /* eject_card */
861 EXPORT_SYMBOL(pcmcia_eject_card);
864 int pcmcia_insert_card(struct pcmcia_socket *skt)
866 int ret;
868 cs_dbg(skt, 1, "user insert request\n");
870 down(&skt->skt_sem);
871 do {
872 if (skt->state & SOCKET_PRESENT) {
873 ret = -EBUSY;
874 break;
876 if (socket_insert(skt) == CS_NO_CARD) {
877 ret = -ENODEV;
878 break;
880 ret = 0;
881 } while (0);
882 up(&skt->skt_sem);
884 return ret;
885 } /* insert_card */
886 EXPORT_SYMBOL(pcmcia_insert_card);
889 static int pcmcia_socket_hotplug(struct class_device *dev, char **envp,
890 int num_envp, char *buffer, int buffer_size)
892 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
893 int i = 0, length = 0;
895 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
896 &length, "SOCKET_NO=%u", s->sock))
897 return -ENOMEM;
899 envp[i] = NULL;
901 return 0;
905 static struct completion pcmcia_unload;
907 static void pcmcia_release_socket_class(struct class *data)
909 complete(&pcmcia_unload);
913 struct class pcmcia_socket_class = {
914 .name = "pcmcia_socket",
915 .hotplug = pcmcia_socket_hotplug,
916 .release = pcmcia_release_socket,
917 .class_release = pcmcia_release_socket_class,
919 EXPORT_SYMBOL(pcmcia_socket_class);
922 static int __init init_pcmcia_cs(void)
924 int ret;
926 init_completion(&pcmcia_unload);
927 ret = class_register(&pcmcia_socket_class);
928 if (ret)
929 return (ret);
930 return class_interface_register(&pccard_sysfs_interface);
933 static void __exit exit_pcmcia_cs(void)
935 class_interface_unregister(&pccard_sysfs_interface);
936 class_unregister(&pcmcia_socket_class);
938 wait_for_completion(&pcmcia_unload);
941 subsys_initcall(init_pcmcia_cs);
942 module_exit(exit_pcmcia_cs);