md: change a few 'int' to 'size_t' in md
[pv_ops_mirror.git] / drivers / pcmcia / cs.c
blob56230dbd347aee88f9d6eb9fc1a32ec9642d7799
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 #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);
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.parent != dev)
113 continue;
114 mutex_lock(&socket->skt_mutex);
115 socket_suspend(socket);
116 mutex_unlock(&socket->skt_mutex);
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.parent != dev)
131 continue;
132 mutex_lock(&socket->skt_mutex);
133 socket_resume(socket);
134 mutex_unlock(&socket->skt_mutex);
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 device *dev = get_device(&skt->dev);
146 if (!dev)
147 return NULL;
148 skt = dev_get_drvdata(dev);
149 if (!try_module_get(skt->owner)) {
150 put_device(&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 put_device(&skt->dev);
163 EXPORT_SYMBOL(pcmcia_put_socket);
166 static void pcmcia_release_socket(struct device *dev)
168 struct pcmcia_socket *socket = dev_get_drvdata(dev);
170 complete(&socket->socket_released);
173 static int pccardd(void *__skt);
176 * pcmcia_register_socket - add a new pcmcia socket device
177 * @socket: the &socket to register
179 int pcmcia_register_socket(struct pcmcia_socket *socket)
181 struct task_struct *tsk;
182 int ret;
184 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
185 return -EINVAL;
187 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
189 spin_lock_init(&socket->lock);
191 if (socket->resource_ops->init) {
192 ret = socket->resource_ops->init(socket);
193 if (ret)
194 return (ret);
197 /* try to obtain a socket number [yes, it gets ugly if we
198 * register more than 2^sizeof(unsigned int) pcmcia
199 * sockets... but the socket number is deprecated
200 * anyways, so I don't care] */
201 down_write(&pcmcia_socket_list_rwsem);
202 if (list_empty(&pcmcia_socket_list))
203 socket->sock = 0;
204 else {
205 unsigned int found, i = 1;
206 struct pcmcia_socket *tmp;
207 do {
208 found = 1;
209 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
210 if (tmp->sock == i)
211 found = 0;
213 i++;
214 } while (!found);
215 socket->sock = i - 1;
217 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
218 up_write(&pcmcia_socket_list_rwsem);
220 #ifndef CONFIG_CARDBUS
222 * If we do not support Cardbus, ensure that
223 * the Cardbus socket capability is disabled.
225 socket->features &= ~SS_CAP_CARDBUS;
226 #endif
228 /* set proper values in socket->dev */
229 dev_set_drvdata(&socket->dev, socket);
230 socket->dev.class = &pcmcia_socket_class;
231 snprintf(socket->dev.bus_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
233 /* base address = 0, map = 0 */
234 socket->cis_mem.flags = 0;
235 socket->cis_mem.speed = cis_speed;
237 INIT_LIST_HEAD(&socket->cis_cache);
239 init_completion(&socket->socket_released);
240 init_completion(&socket->thread_done);
241 init_waitqueue_head(&socket->thread_wait);
242 mutex_init(&socket->skt_mutex);
243 spin_lock_init(&socket->thread_lock);
245 tsk = kthread_run(pccardd, socket, "pccardd");
246 if (IS_ERR(tsk)) {
247 ret = PTR_ERR(tsk);
248 goto err;
251 wait_for_completion(&socket->thread_done);
252 if (!socket->thread) {
253 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
254 return -EIO;
257 pcmcia_parse_events(socket, SS_DETECT);
259 return 0;
261 err:
262 down_write(&pcmcia_socket_list_rwsem);
263 list_del(&socket->socket_list);
264 up_write(&pcmcia_socket_list_rwsem);
265 return ret;
266 } /* pcmcia_register_socket */
267 EXPORT_SYMBOL(pcmcia_register_socket);
271 * pcmcia_unregister_socket - remove a pcmcia socket device
272 * @socket: the &socket to unregister
274 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
276 if (!socket)
277 return;
279 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
281 if (socket->thread) {
282 wake_up(&socket->thread_wait);
283 kthread_stop(socket->thread);
285 release_cis_mem(socket);
287 /* remove from our own list */
288 down_write(&pcmcia_socket_list_rwsem);
289 list_del(&socket->socket_list);
290 up_write(&pcmcia_socket_list_rwsem);
292 /* wait for sysfs to drop all references */
293 release_resource_db(socket);
294 wait_for_completion(&socket->socket_released);
295 } /* pcmcia_unregister_socket */
296 EXPORT_SYMBOL(pcmcia_unregister_socket);
299 struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
301 struct pcmcia_socket *s;
303 down_read(&pcmcia_socket_list_rwsem);
304 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
305 if (s->sock == nr) {
306 up_read(&pcmcia_socket_list_rwsem);
307 return s;
309 up_read(&pcmcia_socket_list_rwsem);
311 return NULL;
314 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
317 * The central event handler. Send_event() sends an event to the
318 * 16-bit subsystem, which then calls the relevant device drivers.
319 * Parse_events() interprets the event bits from
320 * a card status change report. Do_shutdown() handles the high
321 * priority stuff associated with a card removal.
324 /* NOTE: send_event needs to be called with skt->sem held. */
326 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
328 int ret;
330 if (s->state & SOCKET_CARDBUS)
331 return 0;
333 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
334 event, priority, s->callback);
336 if (!s->callback)
337 return 0;
338 if (!try_module_get(s->callback->owner))
339 return 0;
341 ret = s->callback->event(s, event, priority);
343 module_put(s->callback->owner);
345 return ret;
348 static void socket_remove_drivers(struct pcmcia_socket *skt)
350 cs_dbg(skt, 4, "remove_drivers\n");
352 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
355 static int socket_reset(struct pcmcia_socket *skt)
357 int status, i;
359 cs_dbg(skt, 4, "reset\n");
361 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
362 skt->ops->set_socket(skt, &skt->socket);
363 udelay((long)reset_time);
365 skt->socket.flags &= ~SS_RESET;
366 skt->ops->set_socket(skt, &skt->socket);
368 msleep(unreset_delay * 10);
369 for (i = 0; i < unreset_limit; i++) {
370 skt->ops->get_status(skt, &status);
372 if (!(status & SS_DETECT))
373 return CS_NO_CARD;
375 if (status & SS_READY)
376 return CS_SUCCESS;
378 msleep(unreset_check * 10);
381 cs_err(skt, "time out after reset.\n");
382 return CS_GENERAL_FAILURE;
386 * socket_setup() and socket_shutdown() are called by the main event handler
387 * when card insertion and removal events are received.
388 * socket_setup() turns on socket power and resets the socket, in two stages.
389 * socket_shutdown() unconfigures a socket and turns off socket power.
391 static void socket_shutdown(struct pcmcia_socket *s)
393 int status;
395 cs_dbg(s, 4, "shutdown\n");
397 socket_remove_drivers(s);
398 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
399 msleep(shutdown_delay * 10);
400 s->state &= SOCKET_INUSE;
402 /* Blank out the socket state */
403 s->socket = dead_socket;
404 s->ops->init(s);
405 s->ops->set_socket(s, &s->socket);
406 s->irq.AssignedIRQ = s->irq.Config = 0;
407 s->lock_count = 0;
408 destroy_cis_cache(s);
409 #ifdef CONFIG_CARDBUS
410 cb_free(s);
411 #endif
412 s->functions = 0;
414 /* give socket some time to power down */
415 msleep(100);
417 s->ops->get_status(s, &status);
418 if (status & SS_POWERON) {
419 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
422 cs_socket_put(s);
425 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
427 int status, i;
429 cs_dbg(skt, 4, "setup\n");
431 skt->ops->get_status(skt, &status);
432 if (!(status & SS_DETECT))
433 return CS_NO_CARD;
435 msleep(initial_delay * 10);
437 for (i = 0; i < 100; i++) {
438 skt->ops->get_status(skt, &status);
439 if (!(status & SS_DETECT))
440 return CS_NO_CARD;
442 if (!(status & SS_PENDING))
443 break;
445 msleep(100);
448 if (status & SS_PENDING) {
449 cs_err(skt, "voltage interrogation timed out.\n");
450 return CS_GENERAL_FAILURE;
453 if (status & SS_CARDBUS) {
454 if (!(skt->features & SS_CAP_CARDBUS)) {
455 cs_err(skt, "cardbus cards are not supported.\n");
456 return CS_BAD_TYPE;
458 skt->state |= SOCKET_CARDBUS;
462 * Decode the card voltage requirements, and apply power to the card.
464 if (status & SS_3VCARD)
465 skt->socket.Vcc = skt->socket.Vpp = 33;
466 else if (!(status & SS_XVCARD))
467 skt->socket.Vcc = skt->socket.Vpp = 50;
468 else {
469 cs_err(skt, "unsupported voltage key.\n");
470 return CS_BAD_TYPE;
473 if (skt->power_hook)
474 skt->power_hook(skt, HOOK_POWER_PRE);
476 skt->socket.flags = 0;
477 skt->ops->set_socket(skt, &skt->socket);
480 * Wait "vcc_settle" for the supply to stabilise.
482 msleep(vcc_settle * 10);
484 skt->ops->get_status(skt, &status);
485 if (!(status & SS_POWERON)) {
486 cs_err(skt, "unable to apply power.\n");
487 return CS_BAD_TYPE;
490 status = socket_reset(skt);
492 if (skt->power_hook)
493 skt->power_hook(skt, HOOK_POWER_POST);
495 return status;
499 * Handle card insertion. Setup the socket, reset the card,
500 * and then tell the rest of PCMCIA that a card is present.
502 static int socket_insert(struct pcmcia_socket *skt)
504 int ret;
506 cs_dbg(skt, 4, "insert\n");
508 if (!cs_socket_get(skt))
509 return CS_NO_CARD;
511 ret = socket_setup(skt, setup_delay);
512 if (ret == CS_SUCCESS) {
513 skt->state |= SOCKET_PRESENT;
515 printk(KERN_NOTICE "pccard: %s card inserted into slot %d\n",
516 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
517 skt->sock);
519 #ifdef CONFIG_CARDBUS
520 if (skt->state & SOCKET_CARDBUS) {
521 cb_alloc(skt);
522 skt->state |= SOCKET_CARDBUS_CONFIG;
524 #endif
525 cs_dbg(skt, 4, "insert done\n");
527 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
528 } else {
529 socket_shutdown(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);
595 skt->state &= ~SOCKET_SUSPEND;
597 return CS_SUCCESS;
600 static void socket_remove(struct pcmcia_socket *skt)
602 printk(KERN_NOTICE "pccard: card ejected from slot %d\n", skt->sock);
603 socket_shutdown(skt);
607 * Process a socket card detect status change.
609 * If we don't have a card already present, delay the detect event for
610 * about 20ms (to be on the safe side) before reading the socket status.
612 * Some i82365-based systems send multiple SS_DETECT events during card
613 * insertion, and the "card present" status bit seems to bounce. This
614 * will probably be true with GPIO-based card detection systems after
615 * the product has aged.
617 static void socket_detect_change(struct pcmcia_socket *skt)
619 if (!(skt->state & SOCKET_SUSPEND)) {
620 int status;
622 if (!(skt->state & SOCKET_PRESENT))
623 msleep(20);
625 skt->ops->get_status(skt, &status);
626 if ((skt->state & SOCKET_PRESENT) &&
627 !(status & SS_DETECT))
628 socket_remove(skt);
629 if (!(skt->state & SOCKET_PRESENT) &&
630 (status & SS_DETECT))
631 socket_insert(skt);
635 static int pccardd(void *__skt)
637 struct pcmcia_socket *skt = __skt;
638 DECLARE_WAITQUEUE(wait, current);
639 int ret;
641 skt->thread = current;
642 skt->socket = dead_socket;
643 skt->ops->init(skt);
644 skt->ops->set_socket(skt, &skt->socket);
646 /* register with the device core */
647 ret = device_register(&skt->dev);
648 if (ret) {
649 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
650 skt);
651 skt->thread = NULL;
652 complete(&skt->thread_done);
653 return 0;
656 add_wait_queue(&skt->thread_wait, &wait);
657 complete(&skt->thread_done);
659 set_freezable();
660 for (;;) {
661 unsigned long flags;
662 unsigned int events;
664 set_current_state(TASK_INTERRUPTIBLE);
666 spin_lock_irqsave(&skt->thread_lock, flags);
667 events = skt->thread_events;
668 skt->thread_events = 0;
669 spin_unlock_irqrestore(&skt->thread_lock, flags);
671 if (events) {
672 mutex_lock(&skt->skt_mutex);
673 if (events & SS_DETECT)
674 socket_detect_change(skt);
675 if (events & SS_BATDEAD)
676 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
677 if (events & SS_BATWARN)
678 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
679 if (events & SS_READY)
680 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
681 mutex_unlock(&skt->skt_mutex);
682 continue;
685 if (kthread_should_stop())
686 break;
688 schedule();
689 try_to_freeze();
691 /* make sure we are running before we exit */
692 set_current_state(TASK_RUNNING);
694 remove_wait_queue(&skt->thread_wait, &wait);
696 /* remove from the device core */
697 device_unregister(&skt->dev);
699 return 0;
703 * Yenta (at least) probes interrupts before registering the socket and
704 * starting the handler thread.
706 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
708 unsigned long flags;
709 cs_dbg(s, 4, "parse_events: events %08x\n", events);
710 if (s->thread) {
711 spin_lock_irqsave(&s->thread_lock, flags);
712 s->thread_events |= events;
713 spin_unlock_irqrestore(&s->thread_lock, flags);
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_mutex also protects s->callback */
727 mutex_lock(&s->skt_mutex);
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 mutex_unlock(&s->skt_mutex);
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 mutex_lock(&skt->skt_mutex);
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 (skt->callback)
780 skt->callback->suspend(skt);
781 if (socket_reset(skt) == CS_SUCCESS) {
782 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
783 if (skt->callback)
784 skt->callback->resume(skt);
788 ret = CS_SUCCESS;
789 } while (0);
790 mutex_unlock(&skt->skt_mutex);
792 return ret;
793 } /* reset_card */
794 EXPORT_SYMBOL(pccard_reset_card);
797 /* These shut down or wake up a socket. They are sort of user
798 * initiated versions of the APM suspend and resume actions.
800 int pcmcia_suspend_card(struct pcmcia_socket *skt)
802 int ret;
804 cs_dbg(skt, 1, "suspending socket\n");
806 mutex_lock(&skt->skt_mutex);
807 do {
808 if (!(skt->state & SOCKET_PRESENT)) {
809 ret = CS_NO_CARD;
810 break;
812 if (skt->state & SOCKET_CARDBUS) {
813 ret = CS_UNSUPPORTED_FUNCTION;
814 break;
816 if (skt->callback) {
817 ret = skt->callback->suspend(skt);
818 if (ret)
819 break;
821 ret = socket_suspend(skt);
822 } while (0);
823 mutex_unlock(&skt->skt_mutex);
825 return ret;
826 } /* suspend_card */
827 EXPORT_SYMBOL(pcmcia_suspend_card);
830 int pcmcia_resume_card(struct pcmcia_socket *skt)
832 int ret;
834 cs_dbg(skt, 1, "waking up socket\n");
836 mutex_lock(&skt->skt_mutex);
837 do {
838 if (!(skt->state & SOCKET_PRESENT)) {
839 ret = CS_NO_CARD;
840 break;
842 if (skt->state & SOCKET_CARDBUS) {
843 ret = CS_UNSUPPORTED_FUNCTION;
844 break;
846 ret = socket_resume(skt);
847 if (!ret && skt->callback)
848 skt->callback->resume(skt);
849 } while (0);
850 mutex_unlock(&skt->skt_mutex);
852 return ret;
853 } /* resume_card */
854 EXPORT_SYMBOL(pcmcia_resume_card);
857 /* These handle user requests to eject or insert a card. */
858 int pcmcia_eject_card(struct pcmcia_socket *skt)
860 int ret;
862 cs_dbg(skt, 1, "user eject request\n");
864 mutex_lock(&skt->skt_mutex);
865 do {
866 if (!(skt->state & SOCKET_PRESENT)) {
867 ret = -ENODEV;
868 break;
871 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
872 if (ret != 0) {
873 ret = -EINVAL;
874 break;
877 socket_remove(skt);
878 ret = 0;
879 } while (0);
880 mutex_unlock(&skt->skt_mutex);
882 return ret;
883 } /* eject_card */
884 EXPORT_SYMBOL(pcmcia_eject_card);
887 int pcmcia_insert_card(struct pcmcia_socket *skt)
889 int ret;
891 cs_dbg(skt, 1, "user insert request\n");
893 mutex_lock(&skt->skt_mutex);
894 do {
895 if (skt->state & SOCKET_PRESENT) {
896 ret = -EBUSY;
897 break;
899 if (socket_insert(skt) == CS_NO_CARD) {
900 ret = -ENODEV;
901 break;
903 ret = 0;
904 } while (0);
905 mutex_unlock(&skt->skt_mutex);
907 return ret;
908 } /* insert_card */
909 EXPORT_SYMBOL(pcmcia_insert_card);
912 static int pcmcia_socket_uevent(struct device *dev,
913 struct kobj_uevent_env *env)
915 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
917 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
918 return -ENOMEM;
920 return 0;
924 static struct completion pcmcia_unload;
926 static void pcmcia_release_socket_class(struct class *data)
928 complete(&pcmcia_unload);
932 struct class pcmcia_socket_class = {
933 .name = "pcmcia_socket",
934 .dev_uevent = pcmcia_socket_uevent,
935 .dev_release = pcmcia_release_socket,
936 .class_release = pcmcia_release_socket_class,
938 EXPORT_SYMBOL(pcmcia_socket_class);
941 static int __init init_pcmcia_cs(void)
943 int ret;
945 init_completion(&pcmcia_unload);
946 ret = class_register(&pcmcia_socket_class);
947 if (ret)
948 return (ret);
949 return class_interface_register(&pccard_sysfs_interface);
952 static void __exit exit_pcmcia_cs(void)
954 class_interface_unregister(&pccard_sysfs_interface);
955 class_unregister(&pcmcia_socket_class);
957 wait_for_completion(&pcmcia_unload);
960 subsys_initcall(init_pcmcia_cs);
961 module_exit(exit_pcmcia_cs);