pcmcia: allow for extension of resource interval
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / cs.c
blobe679e708db630e3c8a93a7f6d87ded908a2c397e
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 return dev_get_drvdata(dev);
145 EXPORT_SYMBOL(pcmcia_get_socket);
148 void pcmcia_put_socket(struct pcmcia_socket *skt)
150 put_device(&skt->dev);
152 EXPORT_SYMBOL(pcmcia_put_socket);
155 static void pcmcia_release_socket(struct device *dev)
157 struct pcmcia_socket *socket = dev_get_drvdata(dev);
159 complete(&socket->socket_released);
162 static int pccardd(void *__skt);
165 * pcmcia_register_socket - add a new pcmcia socket device
166 * @socket: the &socket to register
168 int pcmcia_register_socket(struct pcmcia_socket *socket)
170 struct task_struct *tsk;
171 int ret;
173 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
174 return -EINVAL;
176 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
178 /* try to obtain a socket number [yes, it gets ugly if we
179 * register more than 2^sizeof(unsigned int) pcmcia
180 * sockets... but the socket number is deprecated
181 * anyways, so I don't care] */
182 down_write(&pcmcia_socket_list_rwsem);
183 if (list_empty(&pcmcia_socket_list))
184 socket->sock = 0;
185 else {
186 unsigned int found, i = 1;
187 struct pcmcia_socket *tmp;
188 do {
189 found = 1;
190 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
191 if (tmp->sock == i)
192 found = 0;
194 i++;
195 } while (!found);
196 socket->sock = i - 1;
198 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
199 up_write(&pcmcia_socket_list_rwsem);
201 #ifndef CONFIG_CARDBUS
203 * If we do not support Cardbus, ensure that
204 * the Cardbus socket capability is disabled.
206 socket->features &= ~SS_CAP_CARDBUS;
207 #endif
209 /* set proper values in socket->dev */
210 dev_set_drvdata(&socket->dev, socket);
211 socket->dev.class = &pcmcia_socket_class;
212 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
214 /* base address = 0, map = 0 */
215 socket->cis_mem.flags = 0;
216 socket->cis_mem.speed = cis_speed;
218 INIT_LIST_HEAD(&socket->cis_cache);
220 init_completion(&socket->socket_released);
221 init_completion(&socket->thread_done);
222 mutex_init(&socket->skt_mutex);
223 mutex_init(&socket->ops_mutex);
224 spin_lock_init(&socket->thread_lock);
226 if (socket->resource_ops->init) {
227 mutex_lock(&socket->ops_mutex);
228 ret = socket->resource_ops->init(socket);
229 mutex_unlock(&socket->ops_mutex);
230 if (ret)
231 goto err;
234 tsk = kthread_run(pccardd, socket, "pccardd");
235 if (IS_ERR(tsk)) {
236 ret = PTR_ERR(tsk);
237 goto err;
240 wait_for_completion(&socket->thread_done);
241 if (!socket->thread) {
242 dev_printk(KERN_WARNING, &socket->dev,
243 "PCMCIA: warning: socket thread did not start\n");
244 return -EIO;
247 pcmcia_parse_events(socket, SS_DETECT);
250 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
251 * If it fails, it doesn't matter -- we still have 32-bit CardBus
252 * support to offer, so this is not a failure mode.
254 request_module_nowait("pcmcia");
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 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
278 if (socket->thread)
279 kthread_stop(socket->thread);
281 /* remove from our own list */
282 down_write(&pcmcia_socket_list_rwsem);
283 list_del(&socket->socket_list);
284 up_write(&pcmcia_socket_list_rwsem);
286 /* wait for sysfs to drop all references */
287 if (socket->resource_ops->exit) {
288 mutex_lock(&socket->ops_mutex);
289 socket->resource_ops->exit(socket);
290 mutex_unlock(&socket->ops_mutex);
292 wait_for_completion(&socket->socket_released);
293 } /* pcmcia_unregister_socket */
294 EXPORT_SYMBOL(pcmcia_unregister_socket);
297 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
299 struct pcmcia_socket *s;
301 down_read(&pcmcia_socket_list_rwsem);
302 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
303 if (s->sock == nr) {
304 up_read(&pcmcia_socket_list_rwsem);
305 return s;
307 up_read(&pcmcia_socket_list_rwsem);
309 return NULL;
312 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
315 * The central event handler. Send_event() sends an event to the
316 * 16-bit subsystem, which then calls the relevant device drivers.
317 * Parse_events() interprets the event bits from
318 * a card status change report. Do_shutdown() handles the high
319 * priority stuff associated with a card removal.
322 /* NOTE: send_event needs to be called with skt->sem held. */
324 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
326 int ret;
328 if ((s->state & SOCKET_CARDBUS) && (event != CS_EVENT_CARD_REMOVAL))
329 return 0;
331 dev_dbg(&s->dev, "send_event(event %d, pri %d, callback 0x%p)\n",
332 event, priority, s->callback);
334 if (!s->callback)
335 return 0;
336 if (!try_module_get(s->callback->owner))
337 return 0;
339 ret = s->callback->event(s, event, priority);
341 module_put(s->callback->owner);
343 return ret;
346 static int socket_reset(struct pcmcia_socket *skt)
348 int status, i;
350 dev_dbg(&skt->dev, "reset\n");
352 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
353 skt->ops->set_socket(skt, &skt->socket);
354 udelay((long)reset_time);
356 skt->socket.flags &= ~SS_RESET;
357 skt->ops->set_socket(skt, &skt->socket);
359 msleep(unreset_delay * 10);
360 for (i = 0; i < unreset_limit; i++) {
361 skt->ops->get_status(skt, &status);
363 if (!(status & SS_DETECT))
364 return -ENODEV;
366 if (status & SS_READY)
367 return 0;
369 msleep(unreset_check * 10);
372 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
373 return -ETIMEDOUT;
377 * socket_setup() and socket_shutdown() are called by the main event handler
378 * when card insertion and removal events are received.
379 * socket_setup() turns on socket power and resets the socket, in two stages.
380 * socket_shutdown() unconfigures a socket and turns off socket power.
382 static void socket_shutdown(struct pcmcia_socket *s)
384 int status;
386 dev_dbg(&s->dev, "shutdown\n");
388 send_event(s, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
390 mutex_lock(&s->ops_mutex);
391 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
392 msleep(shutdown_delay * 10);
393 s->state &= SOCKET_INUSE;
395 /* Blank out the socket state */
396 s->socket = dead_socket;
397 s->ops->init(s);
398 s->ops->set_socket(s, &s->socket);
399 s->irq.AssignedIRQ = s->irq.Config = 0;
400 s->lock_count = 0;
401 kfree(s->fake_cis);
402 s->fake_cis = NULL;
403 s->functions = 0;
405 /* From here on we can be sure that only we (that is, the
406 * pccardd thread) accesses this socket, and all (16-bit)
407 * PCMCIA interactions are gone. Therefore, release
408 * ops_mutex so that we don't get a sysfs-related lockdep
409 * warning.
411 mutex_unlock(&s->ops_mutex);
413 #ifdef CONFIG_CARDBUS
414 cb_free(s);
415 #endif
417 /* give socket some time to power down */
418 msleep(100);
420 s->ops->get_status(s, &status);
421 if (status & SS_POWERON) {
422 dev_printk(KERN_ERR, &s->dev,
423 "*** DANGER *** unable to remove socket power\n");
426 s->state &= ~SOCKET_INUSE;
429 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
431 int status, i;
433 dev_dbg(&skt->dev, "setup\n");
435 skt->ops->get_status(skt, &status);
436 if (!(status & SS_DETECT))
437 return -ENODEV;
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 -ENODEV;
446 if (!(status & SS_PENDING))
447 break;
449 msleep(100);
452 if (status & SS_PENDING) {
453 dev_printk(KERN_ERR, &skt->dev,
454 "voltage interrogation timed out.\n");
455 return -ETIMEDOUT;
458 if (status & SS_CARDBUS) {
459 if (!(skt->features & SS_CAP_CARDBUS)) {
460 dev_printk(KERN_ERR, &skt->dev,
461 "cardbus cards are not supported.\n");
462 return -EINVAL;
464 skt->state |= SOCKET_CARDBUS;
465 } else
466 skt->state &= ~SOCKET_CARDBUS;
469 * Decode the card voltage requirements, and apply power to the card.
471 if (status & SS_3VCARD)
472 skt->socket.Vcc = skt->socket.Vpp = 33;
473 else if (!(status & SS_XVCARD))
474 skt->socket.Vcc = skt->socket.Vpp = 50;
475 else {
476 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
477 return -EIO;
480 if (skt->power_hook)
481 skt->power_hook(skt, HOOK_POWER_PRE);
483 skt->socket.flags = 0;
484 skt->ops->set_socket(skt, &skt->socket);
487 * Wait "vcc_settle" for the supply to stabilise.
489 msleep(vcc_settle * 10);
491 skt->ops->get_status(skt, &status);
492 if (!(status & SS_POWERON)) {
493 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
494 return -EIO;
497 status = socket_reset(skt);
499 if (skt->power_hook)
500 skt->power_hook(skt, HOOK_POWER_POST);
502 return status;
506 * Handle card insertion. Setup the socket, reset the card,
507 * and then tell the rest of PCMCIA that a card is present.
509 static int socket_insert(struct pcmcia_socket *skt)
511 int ret;
513 dev_dbg(&skt->dev, "insert\n");
515 mutex_lock(&skt->ops_mutex);
516 if (skt->state & SOCKET_INUSE) {
517 mutex_unlock(&skt->ops_mutex);
518 return -EINVAL;
520 skt->state |= SOCKET_INUSE;
522 ret = socket_setup(skt, setup_delay);
523 if (ret == 0) {
524 skt->state |= SOCKET_PRESENT;
526 dev_printk(KERN_NOTICE, &skt->dev,
527 "pccard: %s card inserted into slot %d\n",
528 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
529 skt->sock);
531 #ifdef CONFIG_CARDBUS
532 if (skt->state & SOCKET_CARDBUS) {
533 cb_alloc(skt);
534 skt->state |= SOCKET_CARDBUS_CONFIG;
536 #endif
537 dev_dbg(&skt->dev, "insert done\n");
538 mutex_unlock(&skt->ops_mutex);
540 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
541 } else {
542 mutex_unlock(&skt->ops_mutex);
543 socket_shutdown(skt);
546 return ret;
549 static int socket_suspend(struct pcmcia_socket *skt)
551 if (skt->state & SOCKET_SUSPEND)
552 return -EBUSY;
554 mutex_lock(&skt->ops_mutex);
555 skt->suspended_state = skt->state;
557 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
558 skt->socket = dead_socket;
559 skt->ops->set_socket(skt, &skt->socket);
560 if (skt->ops->suspend)
561 skt->ops->suspend(skt);
562 skt->state |= SOCKET_SUSPEND;
563 mutex_unlock(&skt->ops_mutex);
564 return 0;
567 static int socket_early_resume(struct pcmcia_socket *skt)
569 mutex_lock(&skt->ops_mutex);
570 skt->socket = dead_socket;
571 skt->ops->init(skt);
572 skt->ops->set_socket(skt, &skt->socket);
573 if (skt->state & SOCKET_PRESENT)
574 skt->resume_status = socket_setup(skt, resume_delay);
575 mutex_unlock(&skt->ops_mutex);
576 return 0;
579 static int socket_late_resume(struct pcmcia_socket *skt)
581 mutex_lock(&skt->ops_mutex);
582 skt->state &= ~SOCKET_SUSPEND;
583 mutex_unlock(&skt->ops_mutex);
585 if (!(skt->state & SOCKET_PRESENT))
586 return socket_insert(skt);
588 if (skt->resume_status) {
589 socket_shutdown(skt);
590 return 0;
593 if (skt->suspended_state != skt->state) {
594 dev_dbg(&skt->dev,
595 "suspend state 0x%x != resume state 0x%x\n",
596 skt->suspended_state, skt->state);
598 socket_shutdown(skt);
599 return socket_insert(skt);
602 #ifdef CONFIG_CARDBUS
603 if (skt->state & SOCKET_CARDBUS) {
604 /* We can't be sure the CardBus card is the same
605 * as the one previously inserted. Therefore, remove
606 * and re-add... */
607 cb_free(skt);
608 cb_alloc(skt);
609 return 0;
611 #endif
613 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
614 return 0;
618 * Resume a socket. If a card is present, verify its CIS against
619 * our cached copy. If they are different, the card has been
620 * replaced, and we need to tell the drivers.
622 static int socket_resume(struct pcmcia_socket *skt)
624 if (!(skt->state & SOCKET_SUSPEND))
625 return -EBUSY;
627 socket_early_resume(skt);
628 return socket_late_resume(skt);
631 static void socket_remove(struct pcmcia_socket *skt)
633 dev_printk(KERN_NOTICE, &skt->dev,
634 "pccard: card ejected from slot %d\n", skt->sock);
635 socket_shutdown(skt);
639 * Process a socket card detect status change.
641 * If we don't have a card already present, delay the detect event for
642 * about 20ms (to be on the safe side) before reading the socket status.
644 * Some i82365-based systems send multiple SS_DETECT events during card
645 * insertion, and the "card present" status bit seems to bounce. This
646 * will probably be true with GPIO-based card detection systems after
647 * the product has aged.
649 static void socket_detect_change(struct pcmcia_socket *skt)
651 if (!(skt->state & SOCKET_SUSPEND)) {
652 int status;
654 if (!(skt->state & SOCKET_PRESENT))
655 msleep(20);
657 skt->ops->get_status(skt, &status);
658 if ((skt->state & SOCKET_PRESENT) &&
659 !(status & SS_DETECT))
660 socket_remove(skt);
661 if (!(skt->state & SOCKET_PRESENT) &&
662 (status & SS_DETECT))
663 socket_insert(skt);
667 static int pccardd(void *__skt)
669 struct pcmcia_socket *skt = __skt;
670 int ret;
672 skt->thread = current;
673 skt->socket = dead_socket;
674 skt->ops->init(skt);
675 skt->ops->set_socket(skt, &skt->socket);
677 /* register with the device core */
678 ret = device_register(&skt->dev);
679 if (ret) {
680 dev_printk(KERN_WARNING, &skt->dev,
681 "PCMCIA: unable to register socket\n");
682 skt->thread = NULL;
683 complete(&skt->thread_done);
684 return 0;
686 ret = pccard_sysfs_add_socket(&skt->dev);
687 if (ret)
688 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
690 complete(&skt->thread_done);
692 /* wait for userspace to catch up */
693 msleep(250);
695 set_freezable();
696 for (;;) {
697 unsigned long flags;
698 unsigned int events;
699 unsigned int sysfs_events;
701 set_current_state(TASK_INTERRUPTIBLE);
703 spin_lock_irqsave(&skt->thread_lock, flags);
704 events = skt->thread_events;
705 skt->thread_events = 0;
706 sysfs_events = skt->sysfs_events;
707 skt->sysfs_events = 0;
708 spin_unlock_irqrestore(&skt->thread_lock, flags);
710 mutex_lock(&skt->skt_mutex);
711 if (events) {
712 if (events & SS_DETECT)
713 socket_detect_change(skt);
714 if (events & SS_BATDEAD)
715 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
716 if (events & SS_BATWARN)
717 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
718 if (events & SS_READY)
719 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
722 if (sysfs_events) {
723 if (sysfs_events & PCMCIA_UEVENT_EJECT)
724 socket_remove(skt);
725 if (sysfs_events & PCMCIA_UEVENT_INSERT)
726 socket_insert(skt);
727 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
728 !(skt->state & SOCKET_CARDBUS)) {
729 ret = socket_resume(skt);
730 if (!ret && skt->callback)
731 skt->callback->resume(skt);
733 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
734 !(skt->state & SOCKET_CARDBUS)) {
735 if (skt->callback)
736 ret = skt->callback->suspend(skt);
737 else
738 ret = 0;
739 if (!ret)
740 socket_suspend(skt);
742 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
743 !(skt->state & SOCKET_CARDBUS)) {
744 if (!ret && skt->callback)
745 skt->callback->requery(skt);
748 mutex_unlock(&skt->skt_mutex);
750 if (events || sysfs_events)
751 continue;
753 if (kthread_should_stop())
754 break;
756 schedule();
757 try_to_freeze();
759 /* make sure we are running before we exit */
760 set_current_state(TASK_RUNNING);
762 /* shut down socket, if a device is still present */
763 if (skt->state & SOCKET_PRESENT) {
764 mutex_lock(&skt->skt_mutex);
765 socket_remove(skt);
766 mutex_unlock(&skt->skt_mutex);
769 /* remove from the device core */
770 pccard_sysfs_remove_socket(&skt->dev);
771 device_unregister(&skt->dev);
773 return 0;
777 * Yenta (at least) probes interrupts before registering the socket and
778 * starting the handler thread.
780 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
782 unsigned long flags;
783 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
784 if (s->thread) {
785 spin_lock_irqsave(&s->thread_lock, flags);
786 s->thread_events |= events;
787 spin_unlock_irqrestore(&s->thread_lock, flags);
789 wake_up_process(s->thread);
791 } /* pcmcia_parse_events */
792 EXPORT_SYMBOL(pcmcia_parse_events);
795 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
796 * @s: the PCMCIA socket we wan't to command
797 * @events: events to pass to pccardd
799 * userspace-issued insert, eject, suspend and resume commands must be
800 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
801 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
802 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
803 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
805 void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
807 unsigned long flags;
808 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
809 if (s->thread) {
810 spin_lock_irqsave(&s->thread_lock, flags);
811 s->sysfs_events |= events;
812 spin_unlock_irqrestore(&s->thread_lock, flags);
814 wake_up_process(s->thread);
817 EXPORT_SYMBOL(pcmcia_parse_uevents);
820 /* register pcmcia_callback */
821 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
823 int ret = 0;
825 /* s->skt_mutex also protects s->callback */
826 mutex_lock(&s->skt_mutex);
828 if (c) {
829 /* registration */
830 if (s->callback) {
831 ret = -EBUSY;
832 goto err;
835 s->callback = c;
837 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
838 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
839 } else
840 s->callback = NULL;
841 err:
842 mutex_unlock(&s->skt_mutex);
844 return ret;
846 EXPORT_SYMBOL(pccard_register_pcmcia);
849 /* I'm not sure which "reset" function this is supposed to use,
850 * but for now, it uses the low-level interface's reset, not the
851 * CIS register.
854 int pcmcia_reset_card(struct pcmcia_socket *skt)
856 int ret;
858 dev_dbg(&skt->dev, "resetting socket\n");
860 mutex_lock(&skt->skt_mutex);
861 do {
862 if (!(skt->state & SOCKET_PRESENT)) {
863 dev_dbg(&skt->dev, "can't reset, not present\n");
864 ret = -ENODEV;
865 break;
867 if (skt->state & SOCKET_SUSPEND) {
868 dev_dbg(&skt->dev, "can't reset, suspended\n");
869 ret = -EBUSY;
870 break;
872 if (skt->state & SOCKET_CARDBUS) {
873 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
874 ret = -EPERM;
875 break;
878 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
879 if (ret == 0) {
880 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
881 if (skt->callback)
882 skt->callback->suspend(skt);
883 mutex_lock(&skt->ops_mutex);
884 ret = socket_reset(skt);
885 mutex_unlock(&skt->ops_mutex);
886 if (ret == 0) {
887 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
888 if (skt->callback)
889 skt->callback->resume(skt);
893 ret = 0;
894 } while (0);
895 mutex_unlock(&skt->skt_mutex);
897 return ret;
898 } /* reset_card */
899 EXPORT_SYMBOL(pcmcia_reset_card);
902 static int pcmcia_socket_uevent(struct device *dev,
903 struct kobj_uevent_env *env)
905 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
907 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
908 return -ENOMEM;
910 return 0;
914 static struct completion pcmcia_unload;
916 static void pcmcia_release_socket_class(struct class *data)
918 complete(&pcmcia_unload);
922 struct class pcmcia_socket_class = {
923 .name = "pcmcia_socket",
924 .dev_uevent = pcmcia_socket_uevent,
925 .dev_release = pcmcia_release_socket,
926 .class_release = pcmcia_release_socket_class,
928 EXPORT_SYMBOL(pcmcia_socket_class);
931 static int __init init_pcmcia_cs(void)
933 init_completion(&pcmcia_unload);
934 return class_register(&pcmcia_socket_class);
937 static void __exit exit_pcmcia_cs(void)
939 class_unregister(&pcmcia_socket_class);
940 wait_for_completion(&pcmcia_unload);
943 subsys_initcall(init_pcmcia_cs);
944 module_exit(exit_pcmcia_cs);