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>
25 #include <linux/interrupt.h>
26 #include <linux/timer.h>
27 #include <linux/ioport.h>
28 #include <linux/delay.h>
30 #include <linux/pci.h>
31 #include <linux/device.h>
32 #include <asm/system.h>
35 #define IN_CARD_SERVICES
36 #include <pcmcia/version.h>
37 #include <pcmcia/cs_types.h>
38 #include <pcmcia/ss.h>
39 #include <pcmcia/cs.h>
40 #include <pcmcia/bulkmem.h>
41 #include <pcmcia/cistpl.h>
42 #include <pcmcia/cisreg.h>
43 #include <pcmcia/ds.h>
44 #include "cs_internal.h"
47 #define PCI_OPT " [pci]"
52 #define CB_OPT " [cardbus]"
57 #define PM_OPT " [pm]"
61 #if !defined(CONFIG_CARDBUS) && !defined(CONFIG_PCI) && !defined(CONFIG_PM)
62 #define OPTIONS " none"
64 #define OPTIONS PCI_OPT CB_OPT PM_OPT
67 static const char *release = "Linux Kernel Card Services";
68 static const char *options = "options: " OPTIONS;
70 /*====================================================================*/
72 /* Module parameters */
74 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
75 MODULE_DESCRIPTION("Linux Kernel Card Services\noptions:" OPTIONS);
76 MODULE_LICENSE("GPL");
78 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
80 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
81 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
82 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
83 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
84 INT_MODULE_PARM(reset_time, 10); /* usecs */
85 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
86 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
87 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
89 /* Access speed for attribute memory windows */
90 INT_MODULE_PARM(cis_speed, 300); /* ns */
92 /* Access speed for IO windows */
93 INT_MODULE_PARM(io_speed, 0); /* ns */
98 module_param(pc_debug, int, 0644);
100 int cs_debug_level(int level)
102 return pc_debug > level;
106 /*====================================================================*/
108 socket_state_t dead_socket = {
109 .csc_mask = SS_DETECT,
113 /* List of all sockets, protected by a rwsem */
114 LIST_HEAD(pcmcia_socket_list);
115 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
116 EXPORT_SYMBOL(pcmcia_socket_list);
117 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
120 #ifdef CONFIG_PCMCIA_PROBE
121 /* mask ofIRQs already reserved by other cards, we should avoid using them */
122 static u8 pcmcia_used_irq[NR_IRQS];
125 /*====================================================================
127 Low-level PC Card interface drivers need to register with Card
128 Services using these calls.
130 ======================================================================*/
133 * socket drivers are expected to use the following callbacks in their
135 * - pcmcia_socket_dev_suspend
136 * - pcmcia_socket_dev_resume
137 * These functions check for the appropriate struct pcmcia_soket arrays,
138 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
140 static int socket_resume(struct pcmcia_socket *skt);
141 static int socket_suspend(struct pcmcia_socket *skt);
143 int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
145 struct pcmcia_socket *socket;
147 down_read(&pcmcia_socket_list_rwsem);
148 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
149 if (socket->dev.dev != dev)
151 down(&socket->skt_sem);
152 socket_suspend(socket);
153 up(&socket->skt_sem);
155 up_read(&pcmcia_socket_list_rwsem);
159 EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
161 int pcmcia_socket_dev_resume(struct device *dev)
163 struct pcmcia_socket *socket;
165 down_read(&pcmcia_socket_list_rwsem);
166 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
167 if (socket->dev.dev != dev)
169 down(&socket->skt_sem);
170 socket_resume(socket);
171 up(&socket->skt_sem);
173 up_read(&pcmcia_socket_list_rwsem);
177 EXPORT_SYMBOL(pcmcia_socket_dev_resume);
180 struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
182 struct class_device *cl_dev = class_device_get(&skt->dev);
185 skt = class_get_devdata(cl_dev);
186 if (!try_module_get(skt->owner)) {
187 class_device_put(&skt->dev);
192 EXPORT_SYMBOL(pcmcia_get_socket);
195 void pcmcia_put_socket(struct pcmcia_socket *skt)
197 module_put(skt->owner);
198 class_device_put(&skt->dev);
200 EXPORT_SYMBOL(pcmcia_put_socket);
203 static void pcmcia_release_socket(struct class_device *class_dev)
205 struct pcmcia_socket *socket = class_get_devdata(class_dev);
207 complete(&socket->socket_released);
210 static int pccardd(void *__skt);
213 * pcmcia_register_socket - add a new pcmcia socket device
215 int pcmcia_register_socket(struct pcmcia_socket *socket)
219 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
222 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
224 spin_lock_init(&socket->lock);
226 if (socket->resource_ops->init) {
227 ret = socket->resource_ops->init(socket);
232 /* try to obtain a socket number [yes, it gets ugly if we
233 * register more than 2^sizeof(unsigned int) pcmcia
234 * sockets... but the socket number is deprecated
235 * anyways, so I don't care] */
236 down_write(&pcmcia_socket_list_rwsem);
237 if (list_empty(&pcmcia_socket_list))
240 unsigned int found, i = 1;
241 struct pcmcia_socket *tmp;
244 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
250 socket->sock = i - 1;
252 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
253 up_write(&pcmcia_socket_list_rwsem);
256 /* set proper values in socket->dev */
257 socket->dev.class_data = socket;
258 socket->dev.class = &pcmcia_socket_class;
259 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
261 /* base address = 0, map = 0 */
262 socket->cis_mem.flags = 0;
263 socket->cis_mem.speed = cis_speed;
265 INIT_LIST_HEAD(&socket->cis_cache);
267 init_completion(&socket->socket_released);
268 init_completion(&socket->thread_done);
269 init_waitqueue_head(&socket->thread_wait);
270 init_MUTEX(&socket->skt_sem);
271 spin_lock_init(&socket->thread_lock);
273 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
277 wait_for_completion(&socket->thread_done);
278 if(!socket->thread) {
279 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
282 pcmcia_parse_events(socket, SS_DETECT);
287 down_write(&pcmcia_socket_list_rwsem);
288 list_del(&socket->socket_list);
289 up_write(&pcmcia_socket_list_rwsem);
291 } /* pcmcia_register_socket */
292 EXPORT_SYMBOL(pcmcia_register_socket);
296 * pcmcia_unregister_socket - remove a pcmcia socket device
298 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
303 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
305 if (socket->thread) {
306 init_completion(&socket->thread_done);
307 socket->thread = NULL;
308 wake_up(&socket->thread_wait);
309 wait_for_completion(&socket->thread_done);
311 release_cis_mem(socket);
313 /* remove from our own list */
314 down_write(&pcmcia_socket_list_rwsem);
315 list_del(&socket->socket_list);
316 up_write(&pcmcia_socket_list_rwsem);
318 /* wait for sysfs to drop all references */
319 release_resource_db(socket);
320 wait_for_completion(&socket->socket_released);
321 } /* pcmcia_unregister_socket */
322 EXPORT_SYMBOL(pcmcia_unregister_socket);
325 struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
327 struct pcmcia_socket *s;
329 down_read(&pcmcia_socket_list_rwsem);
330 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
332 up_read(&pcmcia_socket_list_rwsem);
335 up_read(&pcmcia_socket_list_rwsem);
340 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
343 /*======================================================================
345 socket_setup() and shutdown_socket() are called by the main event
346 handler when card insertion and removal events are received.
347 socket_setup() turns on socket power and resets the socket, in two stages.
348 shutdown_socket() unconfigures a socket and turns off socket power.
350 ======================================================================*/
352 static void shutdown_socket(struct pcmcia_socket *s)
354 cs_dbg(s, 1, "shutdown_socket\n");
356 /* Blank out the socket state */
357 s->socket = dead_socket;
359 s->ops->set_socket(s, &s->socket);
360 s->irq.AssignedIRQ = s->irq.Config = 0;
362 destroy_cis_cache(s);
363 #ifdef CONFIG_CARDBUS
374 s->ops->get_status(s, &status);
375 if (status & SS_POWERON) {
376 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
379 } /* shutdown_socket */
381 /*======================================================================
383 The central event handler. Send_event() sends an event to the
384 16-bit subsystem, which then calls the relevant device drivers.
385 Parse_events() interprets the event bits from
386 a card status change report. Do_shutdown() handles the high
387 priority stuff associated with a card removal.
389 ======================================================================*/
392 /* NOTE: send_event needs to be called with skt->sem held. */
394 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
398 if (s->state & SOCKET_CARDBUS)
401 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
402 event, priority, s->callback);
406 if (!try_module_get(s->callback->owner))
409 ret = s->callback->event(s, event, priority);
411 module_put(s->callback->owner);
416 static void socket_remove_drivers(struct pcmcia_socket *skt)
418 cs_dbg(skt, 4, "remove_drivers\n");
420 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
423 static void socket_shutdown(struct pcmcia_socket *skt)
425 cs_dbg(skt, 4, "shutdown\n");
427 socket_remove_drivers(skt);
428 skt->state &= SOCKET_INUSE|SOCKET_PRESENT;
429 msleep(shutdown_delay * 10);
430 skt->state &= SOCKET_INUSE;
431 shutdown_socket(skt);
434 static int socket_reset(struct pcmcia_socket *skt)
438 cs_dbg(skt, 4, "reset\n");
440 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
441 skt->ops->set_socket(skt, &skt->socket);
442 udelay((long)reset_time);
444 skt->socket.flags &= ~SS_RESET;
445 skt->ops->set_socket(skt, &skt->socket);
447 msleep(unreset_delay * 10);
448 for (i = 0; i < unreset_limit; i++) {
449 skt->ops->get_status(skt, &status);
451 if (!(status & SS_DETECT))
454 if (status & SS_READY)
457 msleep(unreset_check * 10);
460 cs_err(skt, "time out after reset.\n");
461 return CS_GENERAL_FAILURE;
464 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
468 cs_dbg(skt, 4, "setup\n");
470 skt->ops->get_status(skt, &status);
471 if (!(status & SS_DETECT))
474 msleep(initial_delay * 10);
476 for (i = 0; i < 100; i++) {
477 skt->ops->get_status(skt, &status);
478 if (!(status & SS_DETECT))
481 if (!(status & SS_PENDING))
487 if (status & SS_PENDING) {
488 cs_err(skt, "voltage interrogation timed out.\n");
489 return CS_GENERAL_FAILURE;
492 if (status & SS_CARDBUS) {
493 skt->state |= SOCKET_CARDBUS;
494 #ifndef CONFIG_CARDBUS
495 cs_err(skt, "cardbus cards are not supported.\n");
501 * Decode the card voltage requirements, and apply power to the card.
503 if (status & SS_3VCARD)
504 skt->socket.Vcc = skt->socket.Vpp = 33;
505 else if (!(status & SS_XVCARD))
506 skt->socket.Vcc = skt->socket.Vpp = 50;
508 cs_err(skt, "unsupported voltage key.\n");
513 skt->power_hook(skt, HOOK_POWER_PRE);
515 skt->socket.flags = 0;
516 skt->ops->set_socket(skt, &skt->socket);
519 * Wait "vcc_settle" for the supply to stabilise.
521 msleep(vcc_settle * 10);
523 skt->ops->get_status(skt, &status);
524 if (!(status & SS_POWERON)) {
525 cs_err(skt, "unable to apply power.\n");
529 status = socket_reset(skt);
532 skt->power_hook(skt, HOOK_POWER_POST);
538 * Handle card insertion. Setup the socket, reset the card,
539 * and then tell the rest of PCMCIA that a card is present.
541 static int socket_insert(struct pcmcia_socket *skt)
545 cs_dbg(skt, 4, "insert\n");
547 if (!cs_socket_get(skt))
550 ret = socket_setup(skt, setup_delay);
551 if (ret == CS_SUCCESS) {
552 skt->state |= SOCKET_PRESENT;
553 #ifdef CONFIG_CARDBUS
554 if (skt->state & SOCKET_CARDBUS) {
556 skt->state |= SOCKET_CARDBUS_CONFIG;
559 cs_dbg(skt, 4, "insert done\n");
561 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
563 socket_shutdown(skt);
570 static int socket_suspend(struct pcmcia_socket *skt)
572 if (skt->state & SOCKET_SUSPEND)
575 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
576 skt->socket = dead_socket;
577 skt->ops->set_socket(skt, &skt->socket);
578 if (skt->ops->suspend)
579 skt->ops->suspend(skt);
580 skt->state |= SOCKET_SUSPEND;
586 * Resume a socket. If a card is present, verify its CIS against
587 * our cached copy. If they are different, the card has been
588 * replaced, and we need to tell the drivers.
590 static int socket_resume(struct pcmcia_socket *skt)
594 if (!(skt->state & SOCKET_SUSPEND))
597 skt->socket = dead_socket;
599 skt->ops->set_socket(skt, &skt->socket);
601 if (!(skt->state & SOCKET_PRESENT)) {
602 skt->state &= ~SOCKET_SUSPEND;
603 return socket_insert(skt);
606 ret = socket_setup(skt, resume_delay);
607 if (ret == CS_SUCCESS) {
609 * FIXME: need a better check here for cardbus cards.
611 if (verify_cis_cache(skt) != 0) {
612 cs_dbg(skt, 4, "cis mismatch - different card\n");
613 socket_remove_drivers(skt);
614 destroy_cis_cache(skt);
616 * Workaround: give DS time to schedule removal.
617 * Remove me once the 100ms delay is eliminated
621 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
623 cs_dbg(skt, 4, "cis matches cache\n");
624 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
627 socket_shutdown(skt);
631 skt->state &= ~SOCKET_SUSPEND;
636 static void socket_remove(struct pcmcia_socket *skt)
638 socket_shutdown(skt);
643 * Process a socket card detect status change.
645 * If we don't have a card already present, delay the detect event for
646 * about 20ms (to be on the safe side) before reading the socket status.
648 * Some i82365-based systems send multiple SS_DETECT events during card
649 * insertion, and the "card present" status bit seems to bounce. This
650 * will probably be true with GPIO-based card detection systems after
651 * the product has aged.
653 static void socket_detect_change(struct pcmcia_socket *skt)
655 if (!(skt->state & SOCKET_SUSPEND)) {
658 if (!(skt->state & SOCKET_PRESENT))
661 skt->ops->get_status(skt, &status);
662 if ((skt->state & SOCKET_PRESENT) &&
663 !(status & SS_DETECT))
665 if (!(skt->state & SOCKET_PRESENT) &&
666 (status & SS_DETECT))
671 static int pccardd(void *__skt)
673 struct pcmcia_socket *skt = __skt;
674 DECLARE_WAITQUEUE(wait, current);
677 daemonize("pccardd");
679 skt->thread = current;
680 skt->socket = dead_socket;
682 skt->ops->set_socket(skt, &skt->socket);
684 /* register with the device core */
685 ret = class_device_register(&skt->dev);
687 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
690 complete_and_exit(&skt->thread_done, 0);
692 complete(&skt->thread_done);
694 add_wait_queue(&skt->thread_wait, &wait);
699 set_current_state(TASK_INTERRUPTIBLE);
701 spin_lock_irqsave(&skt->thread_lock, flags);
702 events = skt->thread_events;
703 skt->thread_events = 0;
704 spin_unlock_irqrestore(&skt->thread_lock, flags);
708 if (events & SS_DETECT)
709 socket_detect_change(skt);
710 if (events & SS_BATDEAD)
711 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
712 if (events & SS_BATWARN)
713 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
714 if (events & SS_READY)
715 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
726 remove_wait_queue(&skt->thread_wait, &wait);
728 /* remove from the device core */
729 class_device_unregister(&skt->dev);
731 complete_and_exit(&skt->thread_done, 0);
735 * Yenta (at least) probes interrupts before registering the socket and
736 * starting the handler thread.
738 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
740 cs_dbg(s, 4, "parse_events: events %08x\n", events);
742 spin_lock(&s->thread_lock);
743 s->thread_events |= events;
744 spin_unlock(&s->thread_lock);
746 wake_up(&s->thread_wait);
748 } /* pcmcia_parse_events */
751 /*======================================================================
753 Special stuff for managing IO windows, because they are scarce.
755 ======================================================================*/
757 static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
758 ioaddr_t num, u_int lines)
761 kio_addr_t try, align;
763 align = (*base) ? (lines ? 1<<lines : 0) : 1;
764 if (align && (align < num)) {
766 cs_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
770 while (align && (align < num)) align <<= 1;
772 if (*base & ~(align-1)) {
773 cs_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
777 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
778 *base = s->io_offset | (*base & 0x0fff);
781 /* Check for an already-allocated window that must conflict with
782 what was asked for. It is a hack because it does not catch all
783 potential conflicts, just the most obvious ones. */
784 for (i = 0; i < MAX_IO_WIN; i++)
785 if ((s->io[i].NumPorts != 0) &&
786 ((s->io[i].BasePort & (align-1)) == *base))
788 for (i = 0; i < MAX_IO_WIN; i++) {
789 if (s->io[i].NumPorts == 0) {
790 s->io[i].res = find_io_region(*base, num, align, s);
792 s->io[i].Attributes = attr;
793 s->io[i].BasePort = *base = s->io[i].res->start;
794 s->io[i].NumPorts = s->io[i].InUse = num;
798 } else if (s->io[i].Attributes != attr)
800 /* Try to extend top of window */
801 try = s->io[i].BasePort + s->io[i].NumPorts;
802 if ((*base == 0) || (*base == try))
803 if (adjust_io_region(s->io[i].res, s->io[i].res->start,
804 s->io[i].res->end + num, s) == 0) {
806 s->io[i].NumPorts += num;
807 s->io[i].InUse += num;
810 /* Try to extend bottom of window */
811 try = s->io[i].BasePort - num;
812 if ((*base == 0) || (*base == try))
813 if (adjust_io_region(s->io[i].res, s->io[i].res->start - num,
814 s->io[i].res->end, s) == 0) {
815 s->io[i].BasePort = *base = try;
816 s->io[i].NumPorts += num;
817 s->io[i].InUse += num;
821 return (i == MAX_IO_WIN);
822 } /* alloc_io_space */
824 static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
829 for (i = 0; i < MAX_IO_WIN; i++) {
830 if ((s->io[i].BasePort <= base) &&
831 (s->io[i].BasePort+s->io[i].NumPorts >= base+num)) {
832 s->io[i].InUse -= num;
833 /* Free the window if no one else is using it */
834 if (s->io[i].InUse == 0) {
835 s->io[i].NumPorts = 0;
836 release_resource(s->io[i].res);
844 /*======================================================================
846 Access_configuration_register() reads and writes configuration
847 registers in attribute memory. Memory window 0 is reserved for
848 this and the tuple reading services.
850 ======================================================================*/
852 int pccard_access_configuration_register(struct pcmcia_socket *s,
853 unsigned int function,
860 if (!s || !s->config)
863 c = &s->config[function];
868 if (!(c->state & CONFIG_LOCKED))
869 return CS_CONFIGURATION_LOCKED;
871 addr = (c->ConfigBase + reg->Offset) >> 1;
873 switch (reg->Action) {
875 read_cis_mem(s, 1, addr, 1, &val);
880 write_cis_mem(s, 1, addr, 1, &val);
887 } /* access_configuration_register */
888 EXPORT_SYMBOL(pccard_access_configuration_register);
891 /*====================================================================*/
893 int pccard_get_configuration_info(struct pcmcia_socket *s,
894 unsigned int function,
895 config_info_t *config)
899 if (!(s->state & SOCKET_PRESENT))
902 config->Function = function;
904 #ifdef CONFIG_CARDBUS
905 if (s->state & SOCKET_CARDBUS) {
906 memset(config, 0, sizeof(config_info_t));
907 config->Vcc = s->socket.Vcc;
908 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
909 config->Option = s->cb_dev->subordinate->number;
910 if (s->state & SOCKET_CARDBUS_CONFIG) {
911 config->Attributes = CONF_VALID_CLIENT;
912 config->IntType = INT_CARDBUS;
913 config->AssignedIRQ = s->irq.AssignedIRQ;
914 if (config->AssignedIRQ)
915 config->Attributes |= CONF_ENABLE_IRQ;
916 config->BasePort1 = s->io[0].BasePort;
917 config->NumPorts1 = s->io[0].NumPorts;
923 c = (s->config != NULL) ? &s->config[function] : NULL;
925 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
926 config->Attributes = 0;
927 config->Vcc = s->socket.Vcc;
928 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
932 /* !!! This is a hack !!! */
933 memcpy(&config->Attributes, &c->Attributes, sizeof(config_t));
934 config->Attributes |= CONF_VALID_CLIENT;
935 config->CardValues = c->CardValues;
936 config->IRQAttributes = c->irq.Attributes;
937 config->AssignedIRQ = s->irq.AssignedIRQ;
938 config->BasePort1 = c->io.BasePort1;
939 config->NumPorts1 = c->io.NumPorts1;
940 config->Attributes1 = c->io.Attributes1;
941 config->BasePort2 = c->io.BasePort2;
942 config->NumPorts2 = c->io.NumPorts2;
943 config->Attributes2 = c->io.Attributes2;
944 config->IOAddrLines = c->io.IOAddrLines;
947 } /* get_configuration_info */
948 EXPORT_SYMBOL(pccard_get_configuration_info);
950 /*======================================================================
952 Return information about this version of Card Services.
954 ======================================================================*/
956 int pcmcia_get_card_services_info(servinfo_t *info)
958 unsigned int socket_count = 0;
959 struct list_head *tmp;
960 info->Signature[0] = 'C';
961 info->Signature[1] = 'S';
962 down_read(&pcmcia_socket_list_rwsem);
963 list_for_each(tmp, &pcmcia_socket_list)
965 up_read(&pcmcia_socket_list_rwsem);
966 info->Count = socket_count;
967 info->Revision = CS_RELEASE_CODE;
968 info->CSLevel = 0x0210;
969 info->VendorString = (char *)release;
971 } /* get_card_services_info */
974 /*====================================================================*/
976 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle, int idx, win_req_t *req)
981 if (!s || !(s->state & SOCKET_PRESENT))
983 for (w = idx; w < MAX_WIN; w++)
984 if (s->state & SOCKET_WIN_REQ(w)) break;
986 return CS_NO_MORE_ITEMS;
988 req->Base = win->ctl.res->start;
989 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
990 req->AccessSpeed = win->ctl.speed;
992 if (win->ctl.flags & MAP_ATTRIB)
993 req->Attributes |= WIN_MEMORY_TYPE_AM;
994 if (win->ctl.flags & MAP_ACTIVE)
995 req->Attributes |= WIN_ENABLE;
996 if (win->ctl.flags & MAP_16BIT)
997 req->Attributes |= WIN_DATA_WIDTH_16;
998 if (win->ctl.flags & MAP_USE_WAIT)
999 req->Attributes |= WIN_USE_WAIT;
1003 EXPORT_SYMBOL(pcmcia_get_window);
1005 /*=====================================================================
1007 Return the PCI device associated with a card..
1009 ======================================================================*/
1011 #ifdef CONFIG_CARDBUS
1013 struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
1015 if (!s || !(s->state & SOCKET_CARDBUS))
1018 return s->cb_dev->subordinate;
1021 EXPORT_SYMBOL(pcmcia_lookup_bus);
1025 /*======================================================================
1027 Get the current socket state bits. We don't support the latched
1028 SocketState yet: I haven't seen any point for it.
1030 ======================================================================*/
1032 int pccard_get_status(struct pcmcia_socket *s, unsigned int function, cs_status_t *status)
1037 s->ops->get_status(s, &val);
1038 status->CardState = status->SocketState = 0;
1039 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
1040 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
1041 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
1042 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
1043 if (s->state & SOCKET_SUSPEND)
1044 status->CardState |= CS_EVENT_PM_SUSPEND;
1045 if (!(s->state & SOCKET_PRESENT))
1048 c = (s->config != NULL) ? &s->config[function] : NULL;
1049 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
1050 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
1052 if (c->Present & PRESENT_PIN_REPLACE) {
1053 read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, ®);
1054 status->CardState |=
1055 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
1056 status->CardState |=
1057 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
1058 status->CardState |=
1059 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
1060 status->CardState |=
1061 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
1063 /* No PRR? Then assume we're always ready */
1064 status->CardState |= CS_EVENT_READY_CHANGE;
1066 if (c->Present & PRESENT_EXT_STATUS) {
1067 read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, ®);
1068 status->CardState |=
1069 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
1073 status->CardState |=
1074 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
1075 status->CardState |=
1076 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
1077 status->CardState |=
1078 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
1079 status->CardState |=
1080 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
1083 EXPORT_SYMBOL(pccard_get_status);
1085 /*======================================================================
1087 Change the card address of an already open memory window.
1089 ======================================================================*/
1091 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
1093 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1094 return CS_BAD_HANDLE;
1096 req->CardOffset = win->ctl.card_start;
1098 } /* get_mem_page */
1100 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
1102 struct pcmcia_socket *s;
1103 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1104 return CS_BAD_HANDLE;
1108 win->ctl.card_start = req->CardOffset;
1109 if (s->ops->set_mem_map(s, &win->ctl) != 0)
1110 return CS_BAD_OFFSET;
1112 } /* map_mem_page */
1114 /*======================================================================
1116 Modify a locked socket configuration
1118 ======================================================================*/
1120 int pcmcia_modify_configuration(client_handle_t handle,
1123 struct pcmcia_socket *s;
1126 if (CHECK_HANDLE(handle))
1127 return CS_BAD_HANDLE;
1128 s = SOCKET(handle); c = CONFIG(handle);
1129 if (!(s->state & SOCKET_PRESENT))
1131 if (!(c->state & CONFIG_LOCKED))
1132 return CS_CONFIGURATION_LOCKED;
1134 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
1135 if (mod->Attributes & CONF_ENABLE_IRQ) {
1136 c->Attributes |= CONF_ENABLE_IRQ;
1137 s->socket.io_irq = s->irq.AssignedIRQ;
1139 c->Attributes &= ~CONF_ENABLE_IRQ;
1140 s->socket.io_irq = 0;
1142 s->ops->set_socket(s, &s->socket);
1145 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
1148 /* We only allow changing Vpp1 and Vpp2 to the same value */
1149 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
1150 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
1151 if (mod->Vpp1 != mod->Vpp2)
1153 c->Vpp1 = c->Vpp2 = s->socket.Vpp = mod->Vpp1;
1154 if (s->ops->set_socket(s, &s->socket))
1156 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
1157 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
1161 } /* modify_configuration */
1163 /* register pcmcia_callback */
1164 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
1168 /* s->skt_sem also protects s->callback */
1180 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
1181 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1189 EXPORT_SYMBOL(pccard_register_pcmcia);
1191 /*====================================================================*/
1193 int pcmcia_release_configuration(client_handle_t handle)
1195 pccard_io_map io = { 0, 0, 0, 0, 1 };
1196 struct pcmcia_socket *s;
1199 if (CHECK_HANDLE(handle) ||
1200 !(handle->state & CLIENT_CONFIG_LOCKED))
1201 return CS_BAD_HANDLE;
1202 handle->state &= ~CLIENT_CONFIG_LOCKED;
1205 #ifdef CONFIG_CARDBUS
1206 if (handle->state & CLIENT_CARDBUS)
1210 if (!(handle->state & CLIENT_STALE)) {
1211 config_t *c = CONFIG(handle);
1212 if (--(s->lock_count) == 0) {
1213 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
1215 s->socket.io_irq = 0;
1216 s->ops->set_socket(s, &s->socket);
1218 if (c->state & CONFIG_IO_REQ)
1219 for (i = 0; i < MAX_IO_WIN; i++) {
1220 if (s->io[i].NumPorts == 0)
1223 if (s->io[i].Config != 0)
1226 s->ops->set_io_map(s, &io);
1228 c->state &= ~CONFIG_LOCKED;
1232 } /* release_configuration */
1234 /*======================================================================
1236 Release_io() releases the I/O ranges allocated by a client. This
1237 may be invoked some time after a card ejection has already dumped
1238 the actual socket configuration, so if the client is "stale", we
1239 don't bother checking the port ranges against the current socket
1242 ======================================================================*/
1244 int pcmcia_release_io(client_handle_t handle, io_req_t *req)
1246 struct pcmcia_socket *s;
1248 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IO_REQ))
1249 return CS_BAD_HANDLE;
1250 handle->state &= ~CLIENT_IO_REQ;
1253 #ifdef CONFIG_CARDBUS
1254 if (handle->state & CLIENT_CARDBUS)
1258 if (!(handle->state & CLIENT_STALE)) {
1259 config_t *c = CONFIG(handle);
1260 if (c->state & CONFIG_LOCKED)
1261 return CS_CONFIGURATION_LOCKED;
1262 if ((c->io.BasePort1 != req->BasePort1) ||
1263 (c->io.NumPorts1 != req->NumPorts1) ||
1264 (c->io.BasePort2 != req->BasePort2) ||
1265 (c->io.NumPorts2 != req->NumPorts2))
1267 c->state &= ~CONFIG_IO_REQ;
1270 release_io_space(s, req->BasePort1, req->NumPorts1);
1272 release_io_space(s, req->BasePort2, req->NumPorts2);
1277 /*====================================================================*/
1279 int pcmcia_release_irq(client_handle_t handle, irq_req_t *req)
1281 struct pcmcia_socket *s;
1282 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IRQ_REQ))
1283 return CS_BAD_HANDLE;
1284 handle->state &= ~CLIENT_IRQ_REQ;
1287 if (!(handle->state & CLIENT_STALE)) {
1288 config_t *c = CONFIG(handle);
1289 if (c->state & CONFIG_LOCKED)
1290 return CS_CONFIGURATION_LOCKED;
1291 if (c->irq.Attributes != req->Attributes)
1292 return CS_BAD_ATTRIBUTE;
1293 if (s->irq.AssignedIRQ != req->AssignedIRQ)
1295 if (--s->irq.Config == 0) {
1296 c->state &= ~CONFIG_IRQ_REQ;
1297 s->irq.AssignedIRQ = 0;
1301 if (req->Attributes & IRQ_HANDLE_PRESENT) {
1302 free_irq(req->AssignedIRQ, req->Instance);
1305 #ifdef CONFIG_PCMCIA_PROBE
1306 pcmcia_used_irq[req->AssignedIRQ]--;
1310 } /* cs_release_irq */
1312 /*====================================================================*/
1314 int pcmcia_release_window(window_handle_t win)
1316 struct pcmcia_socket *s;
1318 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1319 return CS_BAD_HANDLE;
1321 if (!(win->handle->state & CLIENT_WIN_REQ(win->index)))
1322 return CS_BAD_HANDLE;
1324 /* Shut down memory window */
1325 win->ctl.flags &= ~MAP_ACTIVE;
1326 s->ops->set_mem_map(s, &win->ctl);
1327 s->state &= ~SOCKET_WIN_REQ(win->index);
1329 /* Release system memory */
1331 release_resource(win->ctl.res);
1332 kfree(win->ctl.res);
1333 win->ctl.res = NULL;
1335 win->handle->state &= ~CLIENT_WIN_REQ(win->index);
1340 } /* release_window */
1342 /*====================================================================*/
1344 int pcmcia_request_configuration(client_handle_t handle,
1349 struct pcmcia_socket *s;
1351 pccard_io_map iomap;
1353 if (CHECK_HANDLE(handle))
1354 return CS_BAD_HANDLE;
1356 if (!(s->state & SOCKET_PRESENT))
1359 #ifdef CONFIG_CARDBUS
1360 if (handle->state & CLIENT_CARDBUS)
1361 return CS_UNSUPPORTED_MODE;
1364 if (req->IntType & INT_CARDBUS)
1365 return CS_UNSUPPORTED_MODE;
1367 if (c->state & CONFIG_LOCKED)
1368 return CS_CONFIGURATION_LOCKED;
1370 /* Do power control. We don't allow changes in Vcc. */
1371 if (s->socket.Vcc != req->Vcc)
1373 if (req->Vpp1 != req->Vpp2)
1375 s->socket.Vpp = req->Vpp1;
1376 if (s->ops->set_socket(s, &s->socket))
1379 c->Vcc = req->Vcc; c->Vpp1 = c->Vpp2 = req->Vpp1;
1381 /* Pick memory or I/O card, DMA mode, interrupt */
1382 c->IntType = req->IntType;
1383 c->Attributes = req->Attributes;
1384 if (req->IntType & INT_MEMORY_AND_IO)
1385 s->socket.flags |= SS_IOCARD;
1386 if (req->IntType & INT_ZOOMED_VIDEO)
1387 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
1388 if (req->Attributes & CONF_ENABLE_DMA)
1389 s->socket.flags |= SS_DMA_MODE;
1390 if (req->Attributes & CONF_ENABLE_SPKR)
1391 s->socket.flags |= SS_SPKR_ENA;
1392 if (req->Attributes & CONF_ENABLE_IRQ)
1393 s->socket.io_irq = s->irq.AssignedIRQ;
1395 s->socket.io_irq = 0;
1396 s->ops->set_socket(s, &s->socket);
1399 /* Set up CIS configuration registers */
1400 base = c->ConfigBase = req->ConfigBase;
1401 c->Present = c->CardValues = req->Present;
1402 if (req->Present & PRESENT_COPY) {
1403 c->Copy = req->Copy;
1404 write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
1406 if (req->Present & PRESENT_OPTION) {
1407 if (s->functions == 1) {
1408 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
1410 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
1411 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
1412 if (req->Present & PRESENT_IOBASE_0)
1413 c->Option |= COR_ADDR_DECODE;
1415 if (c->state & CONFIG_IRQ_REQ)
1416 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
1417 c->Option |= COR_LEVEL_REQ;
1418 write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
1421 if (req->Present & PRESENT_STATUS) {
1422 c->Status = req->Status;
1423 write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
1425 if (req->Present & PRESENT_PIN_REPLACE) {
1427 write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
1429 if (req->Present & PRESENT_EXT_STATUS) {
1430 c->ExtStatus = req->ExtStatus;
1431 write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
1433 if (req->Present & PRESENT_IOBASE_0) {
1434 u_char b = c->io.BasePort1 & 0xff;
1435 write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
1436 b = (c->io.BasePort1 >> 8) & 0xff;
1437 write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
1439 if (req->Present & PRESENT_IOSIZE) {
1440 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
1441 write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
1444 /* Configure I/O windows */
1445 if (c->state & CONFIG_IO_REQ) {
1446 iomap.speed = io_speed;
1447 for (i = 0; i < MAX_IO_WIN; i++)
1448 if (s->io[i].NumPorts != 0) {
1450 iomap.flags = MAP_ACTIVE;
1451 switch (s->io[i].Attributes & IO_DATA_PATH_WIDTH) {
1452 case IO_DATA_PATH_WIDTH_16:
1453 iomap.flags |= MAP_16BIT; break;
1454 case IO_DATA_PATH_WIDTH_AUTO:
1455 iomap.flags |= MAP_AUTOSZ; break;
1459 iomap.start = s->io[i].BasePort;
1460 iomap.stop = iomap.start + s->io[i].NumPorts - 1;
1461 s->ops->set_io_map(s, &iomap);
1466 c->state |= CONFIG_LOCKED;
1467 handle->state |= CLIENT_CONFIG_LOCKED;
1469 } /* request_configuration */
1471 /*======================================================================
1473 Request_io() reserves ranges of port addresses for a socket.
1474 I have not implemented range sharing or alias addressing.
1476 ======================================================================*/
1478 int pcmcia_request_io(client_handle_t handle, io_req_t *req)
1480 struct pcmcia_socket *s;
1483 if (CHECK_HANDLE(handle))
1484 return CS_BAD_HANDLE;
1486 if (!(s->state & SOCKET_PRESENT))
1489 if (handle->state & CLIENT_CARDBUS) {
1490 #ifdef CONFIG_CARDBUS
1491 handle->state |= CLIENT_IO_REQ;
1494 return CS_UNSUPPORTED_FUNCTION;
1499 return CS_UNSUPPORTED_MODE;
1501 if (c->state & CONFIG_LOCKED)
1502 return CS_CONFIGURATION_LOCKED;
1503 if (c->state & CONFIG_IO_REQ)
1505 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
1506 return CS_BAD_ATTRIBUTE;
1507 if ((req->NumPorts2 > 0) &&
1508 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
1509 return CS_BAD_ATTRIBUTE;
1511 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
1512 req->NumPorts1, req->IOAddrLines))
1515 if (req->NumPorts2) {
1516 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
1517 req->NumPorts2, req->IOAddrLines)) {
1518 release_io_space(s, req->BasePort1, req->NumPorts1);
1524 c->state |= CONFIG_IO_REQ;
1525 handle->state |= CLIENT_IO_REQ;
1529 /*======================================================================
1531 Request_irq() reserves an irq for this client.
1533 Also, since Linux only reserves irq's when they are actually
1534 hooked, we don't guarantee that an irq will still be available
1535 when the configuration is locked. Now that I think about it,
1536 there might be a way to fix this using a dummy handler.
1538 ======================================================================*/
1540 #ifdef CONFIG_PCMCIA_PROBE
1541 static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
1547 int pcmcia_request_irq(client_handle_t handle, irq_req_t *req)
1549 struct pcmcia_socket *s;
1551 int ret = CS_IN_USE, irq = 0;
1552 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1554 if (CHECK_HANDLE(handle))
1555 return CS_BAD_HANDLE;
1557 if (!(s->state & SOCKET_PRESENT))
1560 if (c->state & CONFIG_LOCKED)
1561 return CS_CONFIGURATION_LOCKED;
1562 if (c->state & CONFIG_IRQ_REQ)
1565 #ifdef CONFIG_PCMCIA_PROBE
1566 if (s->irq.AssignedIRQ != 0) {
1567 /* If the interrupt is already assigned, it must be the same */
1568 irq = s->irq.AssignedIRQ;
1571 u32 mask = s->irq_mask;
1574 for (try = 0; try < 64; try++) {
1577 /* marked as available by driver, and not blocked by userspace? */
1578 if (!((mask >> irq) & 1))
1581 /* avoid an IRQ which is already used by a PCMCIA card */
1582 if ((try < 32) && pcmcia_used_irq[irq])
1585 /* register the correct driver, if possible, of check whether
1586 * registering a dummy handle works, i.e. if the IRQ isn't
1587 * marked as used by the kernel resource management core */
1588 ret = request_irq(irq,
1589 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
1590 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
1591 (s->functions > 1) ||
1592 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
1594 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
1596 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
1597 free_irq(irq, data);
1609 if (ret && req->Attributes & IRQ_HANDLE_PRESENT) {
1610 if (request_irq(irq, req->Handler,
1611 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
1612 (s->functions > 1) ||
1613 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
1614 p_dev->dev.bus_id, req->Instance))
1618 c->irq.Attributes = req->Attributes;
1619 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
1622 c->state |= CONFIG_IRQ_REQ;
1623 handle->state |= CLIENT_IRQ_REQ;
1625 #ifdef CONFIG_PCMCIA_PROBE
1626 pcmcia_used_irq[irq]++;
1630 } /* pcmcia_request_irq */
1632 /*======================================================================
1634 Request_window() establishes a mapping between card memory space
1635 and system memory space.
1637 ======================================================================*/
1639 int pcmcia_request_window(client_handle_t *handle, win_req_t *req, window_handle_t *wh)
1641 struct pcmcia_socket *s;
1646 if (CHECK_HANDLE(*handle))
1647 return CS_BAD_HANDLE;
1648 s = (*handle)->Socket;
1649 if (!(s->state & SOCKET_PRESENT))
1651 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
1652 return CS_BAD_ATTRIBUTE;
1654 /* Window size defaults to smallest available */
1656 req->Size = s->map_size;
1657 align = (((s->features & SS_CAP_MEM_ALIGN) ||
1658 (req->Attributes & WIN_STRICT_ALIGN)) ?
1659 req->Size : s->map_size);
1660 if (req->Size & (s->map_size-1))
1662 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
1663 (req->Base & (align-1)))
1668 /* Allocate system memory window */
1669 for (w = 0; w < MAX_WIN; w++)
1670 if (!(s->state & SOCKET_WIN_REQ(w))) break;
1672 return CS_OUT_OF_RESOURCE;
1675 win->magic = WINDOW_MAGIC;
1677 win->handle = *handle;
1680 if (!(s->features & SS_CAP_STATIC_MAP)) {
1681 win->ctl.res = find_mem_region(req->Base, req->Size, align,
1682 (req->Attributes & WIN_MAP_BELOW_1MB), s);
1686 (*handle)->state |= CLIENT_WIN_REQ(w);
1688 /* Configure the socket controller */
1691 win->ctl.speed = req->AccessSpeed;
1692 if (req->Attributes & WIN_MEMORY_TYPE)
1693 win->ctl.flags |= MAP_ATTRIB;
1694 if (req->Attributes & WIN_ENABLE)
1695 win->ctl.flags |= MAP_ACTIVE;
1696 if (req->Attributes & WIN_DATA_WIDTH_16)
1697 win->ctl.flags |= MAP_16BIT;
1698 if (req->Attributes & WIN_USE_WAIT)
1699 win->ctl.flags |= MAP_USE_WAIT;
1700 win->ctl.card_start = 0;
1701 if (s->ops->set_mem_map(s, &win->ctl) != 0)
1703 s->state |= SOCKET_WIN_REQ(w);
1705 /* Return window handle */
1706 if (s->features & SS_CAP_STATIC_MAP) {
1707 req->Base = win->ctl.static_start;
1709 req->Base = win->ctl.res->start;
1714 } /* request_window */
1716 /*======================================================================
1718 I'm not sure which "reset" function this is supposed to use,
1719 but for now, it uses the low-level interface's reset, not the
1722 ======================================================================*/
1724 int pccard_reset_card(struct pcmcia_socket *skt)
1728 cs_dbg(skt, 1, "resetting socket\n");
1730 down(&skt->skt_sem);
1732 if (!(skt->state & SOCKET_PRESENT)) {
1736 if (skt->state & SOCKET_SUSPEND) {
1740 if (skt->state & SOCKET_CARDBUS) {
1741 ret = CS_UNSUPPORTED_FUNCTION;
1745 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
1747 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
1748 if (socket_reset(skt) == CS_SUCCESS)
1749 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
1758 EXPORT_SYMBOL(pccard_reset_card);
1760 /*======================================================================
1762 These shut down or wake up a socket. They are sort of user
1763 initiated versions of the APM suspend and resume actions.
1765 ======================================================================*/
1767 int pcmcia_suspend_card(struct pcmcia_socket *skt)
1771 cs_dbg(skt, 1, "suspending socket\n");
1773 down(&skt->skt_sem);
1775 if (!(skt->state & SOCKET_PRESENT)) {
1779 if (skt->state & SOCKET_CARDBUS) {
1780 ret = CS_UNSUPPORTED_FUNCTION;
1783 ret = socket_suspend(skt);
1788 } /* suspend_card */
1790 int pcmcia_resume_card(struct pcmcia_socket *skt)
1794 cs_dbg(skt, 1, "waking up socket\n");
1796 down(&skt->skt_sem);
1798 if (!(skt->state & SOCKET_PRESENT)) {
1802 if (skt->state & SOCKET_CARDBUS) {
1803 ret = CS_UNSUPPORTED_FUNCTION;
1806 ret = socket_resume(skt);
1813 /*======================================================================
1815 These handle user requests to eject or insert a card.
1817 ======================================================================*/
1819 int pcmcia_eject_card(struct pcmcia_socket *skt)
1823 cs_dbg(skt, 1, "user eject request\n");
1825 down(&skt->skt_sem);
1827 if (!(skt->state & SOCKET_PRESENT)) {
1832 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
1846 int pcmcia_insert_card(struct pcmcia_socket *skt)
1850 cs_dbg(skt, 1, "user insert request\n");
1852 down(&skt->skt_sem);
1854 if (skt->state & SOCKET_PRESENT) {
1858 if (socket_insert(skt) == CS_NO_CARD) {
1869 static int pcmcia_socket_hotplug(struct class_device *dev, char **envp,
1870 int num_envp, char *buffer, int buffer_size)
1872 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
1873 int i = 0, length = 0;
1875 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
1876 &length, "SOCKET_NO=%u", s->sock))
1884 /*======================================================================
1886 OS-specific module glue goes here
1888 ======================================================================*/
1889 /* in alpha order */
1890 EXPORT_SYMBOL(pcmcia_eject_card);
1891 EXPORT_SYMBOL(pcmcia_get_card_services_info);
1892 EXPORT_SYMBOL(pcmcia_get_mem_page);
1893 EXPORT_SYMBOL(pcmcia_insert_card);
1894 EXPORT_SYMBOL(pcmcia_map_mem_page);
1895 EXPORT_SYMBOL(pcmcia_modify_configuration);
1896 EXPORT_SYMBOL(pcmcia_release_configuration);
1897 EXPORT_SYMBOL(pcmcia_release_io);
1898 EXPORT_SYMBOL(pcmcia_release_irq);
1899 EXPORT_SYMBOL(pcmcia_release_window);
1900 EXPORT_SYMBOL(pcmcia_replace_cis);
1901 EXPORT_SYMBOL(pcmcia_request_configuration);
1902 EXPORT_SYMBOL(pcmcia_request_io);
1903 EXPORT_SYMBOL(pcmcia_request_irq);
1904 EXPORT_SYMBOL(pcmcia_request_window);
1905 EXPORT_SYMBOL(pcmcia_resume_card);
1906 EXPORT_SYMBOL(pcmcia_suspend_card);
1908 EXPORT_SYMBOL(dead_socket);
1909 EXPORT_SYMBOL(pcmcia_parse_events);
1911 struct class pcmcia_socket_class = {
1912 .name = "pcmcia_socket",
1913 .hotplug = pcmcia_socket_hotplug,
1914 .release = pcmcia_release_socket,
1916 EXPORT_SYMBOL(pcmcia_socket_class);
1919 static int __init init_pcmcia_cs(void)
1922 printk(KERN_INFO "%s\n", release);
1923 printk(KERN_INFO " %s\n", options);
1925 ret = class_register(&pcmcia_socket_class);
1928 return class_interface_register(&pccard_sysfs_interface);
1931 static void __exit exit_pcmcia_cs(void)
1933 printk(KERN_INFO "unloading Kernel Card Services\n");
1934 class_interface_unregister(&pccard_sysfs_interface);
1935 class_unregister(&pcmcia_socket_class);
1938 subsys_initcall(init_pcmcia_cs);
1939 module_exit(exit_pcmcia_cs);
1941 /*====================================================================*/