atmel-mci: Initialize BLKR before sending data transfer command
[linux-2.6/pdupreez.git] / drivers / pcmcia / cs.c
blobd1207393fc3e4029a8b5f6402f82c185d517ff39
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 */
64 #ifdef DEBUG
65 static int pc_debug;
67 module_param(pc_debug, int, 0644);
69 int cs_debug_level(int level)
71 return pc_debug > level;
73 #endif
76 socket_state_t dead_socket = {
77 .csc_mask = SS_DETECT,
79 EXPORT_SYMBOL(dead_socket);
82 /* List of all sockets, protected by a rwsem */
83 LIST_HEAD(pcmcia_socket_list);
84 EXPORT_SYMBOL(pcmcia_socket_list);
86 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
87 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
91 * Low-level PCMCIA socket drivers need to register with the PCCard
92 * core using pcmcia_register_socket.
94 * socket drivers are expected to use the following callbacks in their
95 * .drv struct:
96 * - pcmcia_socket_dev_suspend
97 * - pcmcia_socket_dev_resume
98 * These functions check for the appropriate struct pcmcia_soket arrays,
99 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
101 static int socket_resume(struct pcmcia_socket *skt);
102 static int socket_suspend(struct pcmcia_socket *skt);
104 int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
106 struct pcmcia_socket *socket;
108 down_read(&pcmcia_socket_list_rwsem);
109 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
110 if (socket->dev.parent != dev)
111 continue;
112 mutex_lock(&socket->skt_mutex);
113 socket_suspend(socket);
114 mutex_unlock(&socket->skt_mutex);
116 up_read(&pcmcia_socket_list_rwsem);
118 return 0;
120 EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
122 int pcmcia_socket_dev_resume(struct device *dev)
124 struct pcmcia_socket *socket;
126 down_read(&pcmcia_socket_list_rwsem);
127 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
128 if (socket->dev.parent != dev)
129 continue;
130 mutex_lock(&socket->skt_mutex);
131 socket_resume(socket);
132 mutex_unlock(&socket->skt_mutex);
134 up_read(&pcmcia_socket_list_rwsem);
136 return 0;
138 EXPORT_SYMBOL(pcmcia_socket_dev_resume);
141 struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
143 struct device *dev = get_device(&skt->dev);
144 if (!dev)
145 return NULL;
146 skt = dev_get_drvdata(dev);
147 if (!try_module_get(skt->owner)) {
148 put_device(&skt->dev);
149 return NULL;
151 return (skt);
153 EXPORT_SYMBOL(pcmcia_get_socket);
156 void pcmcia_put_socket(struct pcmcia_socket *skt)
158 module_put(skt->owner);
159 put_device(&skt->dev);
161 EXPORT_SYMBOL(pcmcia_put_socket);
164 static void pcmcia_release_socket(struct device *dev)
166 struct pcmcia_socket *socket = dev_get_drvdata(dev);
168 complete(&socket->socket_released);
171 static int pccardd(void *__skt);
174 * pcmcia_register_socket - add a new pcmcia socket device
175 * @socket: the &socket to register
177 int pcmcia_register_socket(struct pcmcia_socket *socket)
179 struct task_struct *tsk;
180 int ret;
182 if (!socket || !socket->ops || !socket->dev.parent || !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 dev_set_drvdata(&socket->dev, socket);
228 socket->dev.class = &pcmcia_socket_class;
229 snprintf(socket->dev.bus_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 mutex_init(&socket->skt_mutex);
240 spin_lock_init(&socket->thread_lock);
242 tsk = kthread_run(pccardd, socket, "pccardd");
243 if (IS_ERR(tsk)) {
244 ret = PTR_ERR(tsk);
245 goto err;
248 wait_for_completion(&socket->thread_done);
249 if (!socket->thread) {
250 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
251 return -EIO;
254 pcmcia_parse_events(socket, SS_DETECT);
256 return 0;
258 err:
259 down_write(&pcmcia_socket_list_rwsem);
260 list_del(&socket->socket_list);
261 up_write(&pcmcia_socket_list_rwsem);
262 return ret;
263 } /* pcmcia_register_socket */
264 EXPORT_SYMBOL(pcmcia_register_socket);
268 * pcmcia_unregister_socket - remove a pcmcia socket device
269 * @socket: the &socket to unregister
271 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
273 if (!socket)
274 return;
276 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
278 if (socket->thread)
279 kthread_stop(socket->thread);
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);
313 * The central event handler. Send_event() sends an event to the
314 * 16-bit subsystem, which then calls the relevant device drivers.
315 * Parse_events() interprets the event bits from
316 * a card status change report. Do_shutdown() handles the high
317 * priority stuff associated with a card removal.
320 /* NOTE: send_event needs to be called with skt->sem held. */
322 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
324 int ret;
326 if (s->state & SOCKET_CARDBUS)
327 return 0;
329 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
330 event, priority, s->callback);
332 if (!s->callback)
333 return 0;
334 if (!try_module_get(s->callback->owner))
335 return 0;
337 ret = s->callback->event(s, event, priority);
339 module_put(s->callback->owner);
341 return ret;
344 static void socket_remove_drivers(struct pcmcia_socket *skt)
346 cs_dbg(skt, 4, "remove_drivers\n");
348 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
351 static int socket_reset(struct pcmcia_socket *skt)
353 int status, i;
355 cs_dbg(skt, 4, "reset\n");
357 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
358 skt->ops->set_socket(skt, &skt->socket);
359 udelay((long)reset_time);
361 skt->socket.flags &= ~SS_RESET;
362 skt->ops->set_socket(skt, &skt->socket);
364 msleep(unreset_delay * 10);
365 for (i = 0; i < unreset_limit; i++) {
366 skt->ops->get_status(skt, &status);
368 if (!(status & SS_DETECT))
369 return CS_NO_CARD;
371 if (status & SS_READY)
372 return CS_SUCCESS;
374 msleep(unreset_check * 10);
377 cs_err(skt, "time out after reset.\n");
378 return CS_GENERAL_FAILURE;
382 * socket_setup() and socket_shutdown() are called by the main event handler
383 * when card insertion and removal events are received.
384 * socket_setup() turns on socket power and resets the socket, in two stages.
385 * socket_shutdown() unconfigures a socket and turns off socket power.
387 static void socket_shutdown(struct pcmcia_socket *s)
389 int status;
391 cs_dbg(s, 4, "shutdown\n");
393 socket_remove_drivers(s);
394 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
395 msleep(shutdown_delay * 10);
396 s->state &= SOCKET_INUSE;
398 /* Blank out the socket state */
399 s->socket = dead_socket;
400 s->ops->init(s);
401 s->ops->set_socket(s, &s->socket);
402 s->irq.AssignedIRQ = s->irq.Config = 0;
403 s->lock_count = 0;
404 destroy_cis_cache(s);
405 #ifdef CONFIG_CARDBUS
406 cb_free(s);
407 #endif
408 s->functions = 0;
410 /* give socket some time to power down */
411 msleep(100);
413 s->ops->get_status(s, &status);
414 if (status & SS_POWERON) {
415 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
418 cs_socket_put(s);
421 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
423 int status, i;
425 cs_dbg(skt, 4, "setup\n");
427 skt->ops->get_status(skt, &status);
428 if (!(status & SS_DETECT))
429 return CS_NO_CARD;
431 msleep(initial_delay * 10);
433 for (i = 0; i < 100; i++) {
434 skt->ops->get_status(skt, &status);
435 if (!(status & SS_DETECT))
436 return CS_NO_CARD;
438 if (!(status & SS_PENDING))
439 break;
441 msleep(100);
444 if (status & SS_PENDING) {
445 cs_err(skt, "voltage interrogation timed out.\n");
446 return CS_GENERAL_FAILURE;
449 if (status & SS_CARDBUS) {
450 if (!(skt->features & SS_CAP_CARDBUS)) {
451 cs_err(skt, "cardbus cards are not supported.\n");
452 return CS_BAD_TYPE;
454 skt->state |= SOCKET_CARDBUS;
458 * Decode the card voltage requirements, and apply power to the card.
460 if (status & SS_3VCARD)
461 skt->socket.Vcc = skt->socket.Vpp = 33;
462 else if (!(status & SS_XVCARD))
463 skt->socket.Vcc = skt->socket.Vpp = 50;
464 else {
465 cs_err(skt, "unsupported voltage key.\n");
466 return CS_BAD_TYPE;
469 if (skt->power_hook)
470 skt->power_hook(skt, HOOK_POWER_PRE);
472 skt->socket.flags = 0;
473 skt->ops->set_socket(skt, &skt->socket);
476 * Wait "vcc_settle" for the supply to stabilise.
478 msleep(vcc_settle * 10);
480 skt->ops->get_status(skt, &status);
481 if (!(status & SS_POWERON)) {
482 cs_err(skt, "unable to apply power.\n");
483 return CS_BAD_TYPE;
486 status = socket_reset(skt);
488 if (skt->power_hook)
489 skt->power_hook(skt, HOOK_POWER_POST);
491 return status;
495 * Handle card insertion. Setup the socket, reset the card,
496 * and then tell the rest of PCMCIA that a card is present.
498 static int socket_insert(struct pcmcia_socket *skt)
500 int ret;
502 cs_dbg(skt, 4, "insert\n");
504 if (!cs_socket_get(skt))
505 return CS_NO_CARD;
507 ret = socket_setup(skt, setup_delay);
508 if (ret == CS_SUCCESS) {
509 skt->state |= SOCKET_PRESENT;
511 printk(KERN_NOTICE "pccard: %s card inserted into slot %d\n",
512 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
513 skt->sock);
515 #ifdef CONFIG_CARDBUS
516 if (skt->state & SOCKET_CARDBUS) {
517 cb_alloc(skt);
518 skt->state |= SOCKET_CARDBUS_CONFIG;
520 #endif
521 cs_dbg(skt, 4, "insert done\n");
523 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
524 } else {
525 socket_shutdown(skt);
528 return ret;
531 static int socket_suspend(struct pcmcia_socket *skt)
533 if (skt->state & SOCKET_SUSPEND)
534 return CS_IN_USE;
536 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
537 skt->socket = dead_socket;
538 skt->ops->set_socket(skt, &skt->socket);
539 if (skt->ops->suspend)
540 skt->ops->suspend(skt);
541 skt->state |= SOCKET_SUSPEND;
543 return CS_SUCCESS;
547 * Resume a socket. If a card is present, verify its CIS against
548 * our cached copy. If they are different, the card has been
549 * replaced, and we need to tell the drivers.
551 static int socket_resume(struct pcmcia_socket *skt)
553 int ret;
555 if (!(skt->state & SOCKET_SUSPEND))
556 return CS_IN_USE;
558 skt->socket = dead_socket;
559 skt->ops->init(skt);
560 skt->ops->set_socket(skt, &skt->socket);
562 if (!(skt->state & SOCKET_PRESENT)) {
563 skt->state &= ~SOCKET_SUSPEND;
564 return socket_insert(skt);
567 ret = socket_setup(skt, resume_delay);
568 if (ret == CS_SUCCESS) {
570 * FIXME: need a better check here for cardbus cards.
572 if (verify_cis_cache(skt) != 0) {
573 cs_dbg(skt, 4, "cis mismatch - different card\n");
574 socket_remove_drivers(skt);
575 destroy_cis_cache(skt);
577 * Workaround: give DS time to schedule removal.
578 * Remove me once the 100ms delay is eliminated
579 * in ds.c
581 msleep(200);
582 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
583 } else {
584 cs_dbg(skt, 4, "cis matches cache\n");
585 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
587 } else {
588 socket_shutdown(skt);
591 skt->state &= ~SOCKET_SUSPEND;
593 return CS_SUCCESS;
596 static void socket_remove(struct pcmcia_socket *skt)
598 printk(KERN_NOTICE "pccard: card ejected from slot %d\n", skt->sock);
599 socket_shutdown(skt);
603 * Process a socket card detect status change.
605 * If we don't have a card already present, delay the detect event for
606 * about 20ms (to be on the safe side) before reading the socket status.
608 * Some i82365-based systems send multiple SS_DETECT events during card
609 * insertion, and the "card present" status bit seems to bounce. This
610 * will probably be true with GPIO-based card detection systems after
611 * the product has aged.
613 static void socket_detect_change(struct pcmcia_socket *skt)
615 if (!(skt->state & SOCKET_SUSPEND)) {
616 int status;
618 if (!(skt->state & SOCKET_PRESENT))
619 msleep(20);
621 skt->ops->get_status(skt, &status);
622 if ((skt->state & SOCKET_PRESENT) &&
623 !(status & SS_DETECT))
624 socket_remove(skt);
625 if (!(skt->state & SOCKET_PRESENT) &&
626 (status & SS_DETECT))
627 socket_insert(skt);
631 static int pccardd(void *__skt)
633 struct pcmcia_socket *skt = __skt;
634 int ret;
636 skt->thread = current;
637 skt->socket = dead_socket;
638 skt->ops->init(skt);
639 skt->ops->set_socket(skt, &skt->socket);
641 /* register with the device core */
642 ret = device_register(&skt->dev);
643 if (ret) {
644 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
645 skt);
646 skt->thread = NULL;
647 complete(&skt->thread_done);
648 return 0;
650 ret = pccard_sysfs_add_socket(&skt->dev);
651 if (ret)
652 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
654 complete(&skt->thread_done);
656 set_freezable();
657 for (;;) {
658 unsigned long flags;
659 unsigned int events;
661 set_current_state(TASK_INTERRUPTIBLE);
663 spin_lock_irqsave(&skt->thread_lock, flags);
664 events = skt->thread_events;
665 skt->thread_events = 0;
666 spin_unlock_irqrestore(&skt->thread_lock, flags);
668 if (events) {
669 mutex_lock(&skt->skt_mutex);
670 if (events & SS_DETECT)
671 socket_detect_change(skt);
672 if (events & SS_BATDEAD)
673 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
674 if (events & SS_BATWARN)
675 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
676 if (events & SS_READY)
677 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
678 mutex_unlock(&skt->skt_mutex);
679 continue;
682 if (kthread_should_stop())
683 break;
685 schedule();
686 try_to_freeze();
688 /* make sure we are running before we exit */
689 set_current_state(TASK_RUNNING);
691 /* remove from the device core */
692 pccard_sysfs_remove_socket(&skt->dev);
693 device_unregister(&skt->dev);
695 return 0;
699 * Yenta (at least) probes interrupts before registering the socket and
700 * starting the handler thread.
702 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
704 unsigned long flags;
705 cs_dbg(s, 4, "parse_events: events %08x\n", events);
706 if (s->thread) {
707 spin_lock_irqsave(&s->thread_lock, flags);
708 s->thread_events |= events;
709 spin_unlock_irqrestore(&s->thread_lock, flags);
711 wake_up_process(s->thread);
713 } /* pcmcia_parse_events */
714 EXPORT_SYMBOL(pcmcia_parse_events);
717 /* register pcmcia_callback */
718 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
720 int ret = 0;
722 /* s->skt_mutex also protects s->callback */
723 mutex_lock(&s->skt_mutex);
725 if (c) {
726 /* registration */
727 if (s->callback) {
728 ret = -EBUSY;
729 goto err;
732 s->callback = c;
734 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
735 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
736 } else
737 s->callback = NULL;
738 err:
739 mutex_unlock(&s->skt_mutex);
741 return ret;
743 EXPORT_SYMBOL(pccard_register_pcmcia);
746 /* I'm not sure which "reset" function this is supposed to use,
747 * but for now, it uses the low-level interface's reset, not the
748 * CIS register.
751 int pccard_reset_card(struct pcmcia_socket *skt)
753 int ret;
755 cs_dbg(skt, 1, "resetting socket\n");
757 mutex_lock(&skt->skt_mutex);
758 do {
759 if (!(skt->state & SOCKET_PRESENT)) {
760 ret = CS_NO_CARD;
761 break;
763 if (skt->state & SOCKET_SUSPEND) {
764 ret = CS_IN_USE;
765 break;
767 if (skt->state & SOCKET_CARDBUS) {
768 ret = CS_UNSUPPORTED_FUNCTION;
769 break;
772 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
773 if (ret == 0) {
774 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
775 if (skt->callback)
776 skt->callback->suspend(skt);
777 if (socket_reset(skt) == CS_SUCCESS) {
778 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
779 if (skt->callback)
780 skt->callback->resume(skt);
784 ret = CS_SUCCESS;
785 } while (0);
786 mutex_unlock(&skt->skt_mutex);
788 return ret;
789 } /* reset_card */
790 EXPORT_SYMBOL(pccard_reset_card);
793 /* These shut down or wake up a socket. They are sort of user
794 * initiated versions of the APM suspend and resume actions.
796 int pcmcia_suspend_card(struct pcmcia_socket *skt)
798 int ret;
800 cs_dbg(skt, 1, "suspending socket\n");
802 mutex_lock(&skt->skt_mutex);
803 do {
804 if (!(skt->state & SOCKET_PRESENT)) {
805 ret = CS_NO_CARD;
806 break;
808 if (skt->state & SOCKET_CARDBUS) {
809 ret = CS_UNSUPPORTED_FUNCTION;
810 break;
812 if (skt->callback) {
813 ret = skt->callback->suspend(skt);
814 if (ret)
815 break;
817 ret = socket_suspend(skt);
818 } while (0);
819 mutex_unlock(&skt->skt_mutex);
821 return ret;
822 } /* suspend_card */
823 EXPORT_SYMBOL(pcmcia_suspend_card);
826 int pcmcia_resume_card(struct pcmcia_socket *skt)
828 int ret;
830 cs_dbg(skt, 1, "waking up socket\n");
832 mutex_lock(&skt->skt_mutex);
833 do {
834 if (!(skt->state & SOCKET_PRESENT)) {
835 ret = CS_NO_CARD;
836 break;
838 if (skt->state & SOCKET_CARDBUS) {
839 ret = CS_UNSUPPORTED_FUNCTION;
840 break;
842 ret = socket_resume(skt);
843 if (!ret && skt->callback)
844 skt->callback->resume(skt);
845 } while (0);
846 mutex_unlock(&skt->skt_mutex);
848 return ret;
849 } /* resume_card */
850 EXPORT_SYMBOL(pcmcia_resume_card);
853 /* These handle user requests to eject or insert a card. */
854 int pcmcia_eject_card(struct pcmcia_socket *skt)
856 int ret;
858 cs_dbg(skt, 1, "user eject request\n");
860 mutex_lock(&skt->skt_mutex);
861 do {
862 if (!(skt->state & SOCKET_PRESENT)) {
863 ret = -ENODEV;
864 break;
867 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
868 if (ret != 0) {
869 ret = -EINVAL;
870 break;
873 socket_remove(skt);
874 ret = 0;
875 } while (0);
876 mutex_unlock(&skt->skt_mutex);
878 return ret;
879 } /* eject_card */
880 EXPORT_SYMBOL(pcmcia_eject_card);
883 int pcmcia_insert_card(struct pcmcia_socket *skt)
885 int ret;
887 cs_dbg(skt, 1, "user insert request\n");
889 mutex_lock(&skt->skt_mutex);
890 do {
891 if (skt->state & SOCKET_PRESENT) {
892 ret = -EBUSY;
893 break;
895 if (socket_insert(skt) == CS_NO_CARD) {
896 ret = -ENODEV;
897 break;
899 ret = 0;
900 } while (0);
901 mutex_unlock(&skt->skt_mutex);
903 return ret;
904 } /* insert_card */
905 EXPORT_SYMBOL(pcmcia_insert_card);
908 static int pcmcia_socket_uevent(struct device *dev,
909 struct kobj_uevent_env *env)
911 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
913 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
914 return -ENOMEM;
916 return 0;
920 static struct completion pcmcia_unload;
922 static void pcmcia_release_socket_class(struct class *data)
924 complete(&pcmcia_unload);
928 struct class pcmcia_socket_class = {
929 .name = "pcmcia_socket",
930 .dev_uevent = pcmcia_socket_uevent,
931 .dev_release = pcmcia_release_socket,
932 .class_release = pcmcia_release_socket_class,
934 EXPORT_SYMBOL(pcmcia_socket_class);
937 static int __init init_pcmcia_cs(void)
939 init_completion(&pcmcia_unload);
940 return class_register(&pcmcia_socket_class);
943 static void __exit exit_pcmcia_cs(void)
945 class_unregister(&pcmcia_socket_class);
946 wait_for_completion(&pcmcia_unload);
949 subsys_initcall(init_pcmcia_cs);
950 module_exit(exit_pcmcia_cs);