[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / pcmcia / cs.c
blob03fc885db1c57e9325915640dacc023eef3c829f
1 /*
2 * cs.c -- Kernel Card Services - core services
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * The initial developer of the original code is David A. Hinds
9 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
12 * (C) 1999 David A. Hinds
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/config.h>
20 #include <linux/string.h>
21 #include <linux/major.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/timer.h>
27 #include <linux/ioport.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/pci.h>
31 #include <linux/device.h>
32 #include <asm/system.h>
33 #include <asm/irq.h>
35 #define IN_CARD_SERVICES
36 #include <pcmcia/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"
46 #ifdef CONFIG_PCI
47 #define PCI_OPT " [pci]"
48 #else
49 #define PCI_OPT ""
50 #endif
51 #ifdef CONFIG_CARDBUS
52 #define CB_OPT " [cardbus]"
53 #else
54 #define CB_OPT ""
55 #endif
56 #ifdef CONFIG_PM
57 #define PM_OPT " [pm]"
58 #else
59 #define PM_OPT ""
60 #endif
61 #if !defined(CONFIG_CARDBUS) && !defined(CONFIG_PCI) && !defined(CONFIG_PM)
62 #define OPTIONS " none"
63 #else
64 #define OPTIONS PCI_OPT CB_OPT PM_OPT
65 #endif
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 */
95 #ifdef DEBUG
96 static int pc_debug;
98 module_param(pc_debug, int, 0644);
100 int cs_debug_level(int level)
102 return pc_debug > level;
104 #endif
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];
123 #endif
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
134 * .drv struct:
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)
150 continue;
151 down(&socket->skt_sem);
152 socket_suspend(socket);
153 up(&socket->skt_sem);
155 up_read(&pcmcia_socket_list_rwsem);
157 return 0;
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)
168 continue;
169 down(&socket->skt_sem);
170 socket_resume(socket);
171 up(&socket->skt_sem);
173 up_read(&pcmcia_socket_list_rwsem);
175 return 0;
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);
183 if (!cl_dev)
184 return NULL;
185 skt = class_get_devdata(cl_dev);
186 if (!try_module_get(skt->owner)) {
187 class_device_put(&skt->dev);
188 return NULL;
190 return (skt);
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)
217 int ret;
219 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
220 return -EINVAL;
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);
228 if (ret)
229 return (ret);
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))
238 socket->sock = 0;
239 else {
240 unsigned int found, i = 1;
241 struct pcmcia_socket *tmp;
242 do {
243 found = 1;
244 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
245 if (tmp->sock == i)
246 found = 0;
248 i++;
249 } while (!found);
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);
274 if (ret < 0)
275 goto err;
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);
280 return -EIO;
282 pcmcia_parse_events(socket, SS_DETECT);
284 return 0;
286 err:
287 down_write(&pcmcia_socket_list_rwsem);
288 list_del(&socket->socket_list);
289 up_write(&pcmcia_socket_list_rwsem);
290 return ret;
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)
300 if (!socket)
301 return;
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)
331 if (s->sock == nr) {
332 up_read(&pcmcia_socket_list_rwsem);
333 return s;
335 up_read(&pcmcia_socket_list_rwsem);
337 return NULL;
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;
358 s->ops->init(s);
359 s->ops->set_socket(s, &s->socket);
360 s->irq.AssignedIRQ = s->irq.Config = 0;
361 s->lock_count = 0;
362 destroy_cis_cache(s);
363 #ifdef CONFIG_CARDBUS
364 cb_free(s);
365 #endif
366 s->functions = 0;
367 if (s->config) {
368 kfree(s->config);
369 s->config = NULL;
373 int status;
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)
396 int ret;
398 if (s->state & SOCKET_CARDBUS)
399 return 0;
401 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
402 event, priority, s->callback);
404 if (!s->callback)
405 return 0;
406 if (!try_module_get(s->callback->owner))
407 return 0;
409 ret = s->callback->event(s, event, priority);
411 module_put(s->callback->owner);
413 return ret;
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)
436 int status, i;
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))
452 return CS_NO_CARD;
454 if (status & SS_READY)
455 return CS_SUCCESS;
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)
466 int status, i;
468 cs_dbg(skt, 4, "setup\n");
470 skt->ops->get_status(skt, &status);
471 if (!(status & SS_DETECT))
472 return CS_NO_CARD;
474 msleep(initial_delay * 10);
476 for (i = 0; i < 100; i++) {
477 skt->ops->get_status(skt, &status);
478 if (!(status & SS_DETECT))
479 return CS_NO_CARD;
481 if (!(status & SS_PENDING))
482 break;
484 msleep(100);
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");
496 return CS_BAD_TYPE;
497 #endif
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;
507 else {
508 cs_err(skt, "unsupported voltage key.\n");
509 return CS_BAD_TYPE;
511 skt->socket.flags = 0;
512 skt->ops->set_socket(skt, &skt->socket);
515 * Wait "vcc_settle" for the supply to stabilise.
517 msleep(vcc_settle * 10);
519 skt->ops->get_status(skt, &status);
520 if (!(status & SS_POWERON)) {
521 cs_err(skt, "unable to apply power.\n");
522 return CS_BAD_TYPE;
525 return socket_reset(skt);
529 * Handle card insertion. Setup the socket, reset the card,
530 * and then tell the rest of PCMCIA that a card is present.
532 static int socket_insert(struct pcmcia_socket *skt)
534 int ret;
536 cs_dbg(skt, 4, "insert\n");
538 if (!cs_socket_get(skt))
539 return CS_NO_CARD;
541 ret = socket_setup(skt, setup_delay);
542 if (ret == CS_SUCCESS) {
543 skt->state |= SOCKET_PRESENT;
544 #ifdef CONFIG_CARDBUS
545 if (skt->state & SOCKET_CARDBUS) {
546 cb_alloc(skt);
547 skt->state |= SOCKET_CARDBUS_CONFIG;
549 #endif
550 cs_dbg(skt, 4, "insert done\n");
552 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
553 } else {
554 socket_shutdown(skt);
555 cs_socket_put(skt);
558 return ret;
561 static int socket_suspend(struct pcmcia_socket *skt)
563 if (skt->state & SOCKET_SUSPEND)
564 return CS_IN_USE;
566 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
567 skt->socket = dead_socket;
568 skt->ops->set_socket(skt, &skt->socket);
569 if (skt->ops->suspend)
570 skt->ops->suspend(skt);
571 skt->state |= SOCKET_SUSPEND;
573 return CS_SUCCESS;
577 * Resume a socket. If a card is present, verify its CIS against
578 * our cached copy. If they are different, the card has been
579 * replaced, and we need to tell the drivers.
581 static int socket_resume(struct pcmcia_socket *skt)
583 int ret;
585 if (!(skt->state & SOCKET_SUSPEND))
586 return CS_IN_USE;
588 skt->socket = dead_socket;
589 skt->ops->init(skt);
590 skt->ops->set_socket(skt, &skt->socket);
592 if (!(skt->state & SOCKET_PRESENT)) {
593 skt->state &= ~SOCKET_SUSPEND;
594 return socket_insert(skt);
597 ret = socket_setup(skt, resume_delay);
598 if (ret == CS_SUCCESS) {
600 * FIXME: need a better check here for cardbus cards.
602 if (verify_cis_cache(skt) != 0) {
603 cs_dbg(skt, 4, "cis mismatch - different card\n");
604 socket_remove_drivers(skt);
605 destroy_cis_cache(skt);
607 * Workaround: give DS time to schedule removal.
608 * Remove me once the 100ms delay is eliminated
609 * in ds.c
611 msleep(200);
612 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
613 } else {
614 cs_dbg(skt, 4, "cis matches cache\n");
615 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
617 } else {
618 socket_shutdown(skt);
619 cs_socket_put(skt);
622 skt->state &= ~SOCKET_SUSPEND;
624 return CS_SUCCESS;
627 static void socket_remove(struct pcmcia_socket *skt)
629 socket_shutdown(skt);
630 cs_socket_put(skt);
634 * Process a socket card detect status change.
636 * If we don't have a card already present, delay the detect event for
637 * about 20ms (to be on the safe side) before reading the socket status.
639 * Some i82365-based systems send multiple SS_DETECT events during card
640 * insertion, and the "card present" status bit seems to bounce. This
641 * will probably be true with GPIO-based card detection systems after
642 * the product has aged.
644 static void socket_detect_change(struct pcmcia_socket *skt)
646 if (!(skt->state & SOCKET_SUSPEND)) {
647 int status;
649 if (!(skt->state & SOCKET_PRESENT))
650 msleep(20);
652 skt->ops->get_status(skt, &status);
653 if ((skt->state & SOCKET_PRESENT) &&
654 !(status & SS_DETECT))
655 socket_remove(skt);
656 if (!(skt->state & SOCKET_PRESENT) &&
657 (status & SS_DETECT))
658 socket_insert(skt);
662 static int pccardd(void *__skt)
664 struct pcmcia_socket *skt = __skt;
665 DECLARE_WAITQUEUE(wait, current);
666 int ret;
668 daemonize("pccardd");
670 skt->thread = current;
671 skt->socket = dead_socket;
672 skt->ops->init(skt);
673 skt->ops->set_socket(skt, &skt->socket);
675 /* register with the device core */
676 ret = class_device_register(&skt->dev);
677 if (ret) {
678 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
679 skt);
680 skt->thread = NULL;
681 complete_and_exit(&skt->thread_done, 0);
683 complete(&skt->thread_done);
685 add_wait_queue(&skt->thread_wait, &wait);
686 for (;;) {
687 unsigned long flags;
688 unsigned int events;
690 set_current_state(TASK_INTERRUPTIBLE);
692 spin_lock_irqsave(&skt->thread_lock, flags);
693 events = skt->thread_events;
694 skt->thread_events = 0;
695 spin_unlock_irqrestore(&skt->thread_lock, flags);
697 if (events) {
698 down(&skt->skt_sem);
699 if (events & SS_DETECT)
700 socket_detect_change(skt);
701 if (events & SS_BATDEAD)
702 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
703 if (events & SS_BATWARN)
704 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
705 if (events & SS_READY)
706 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
707 up(&skt->skt_sem);
708 continue;
711 schedule();
712 try_to_freeze(PF_FREEZE);
714 if (!skt->thread)
715 break;
717 remove_wait_queue(&skt->thread_wait, &wait);
719 /* remove from the device core */
720 class_device_unregister(&skt->dev);
722 complete_and_exit(&skt->thread_done, 0);
726 * Yenta (at least) probes interrupts before registering the socket and
727 * starting the handler thread.
729 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
731 cs_dbg(s, 4, "parse_events: events %08x\n", events);
732 if (s->thread) {
733 spin_lock(&s->thread_lock);
734 s->thread_events |= events;
735 spin_unlock(&s->thread_lock);
737 wake_up(&s->thread_wait);
739 } /* pcmcia_parse_events */
742 /*======================================================================
744 Special stuff for managing IO windows, because they are scarce.
746 ======================================================================*/
748 static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
749 ioaddr_t num, u_int lines)
751 int i;
752 kio_addr_t try, align;
754 align = (*base) ? (lines ? 1<<lines : 0) : 1;
755 if (align && (align < num)) {
756 if (*base) {
757 cs_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
758 num, align);
759 align = 0;
760 } else
761 while (align && (align < num)) align <<= 1;
763 if (*base & ~(align-1)) {
764 cs_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
765 *base, align);
766 align = 0;
768 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
769 *base = s->io_offset | (*base & 0x0fff);
770 return 0;
772 /* Check for an already-allocated window that must conflict with
773 what was asked for. It is a hack because it does not catch all
774 potential conflicts, just the most obvious ones. */
775 for (i = 0; i < MAX_IO_WIN; i++)
776 if ((s->io[i].NumPorts != 0) &&
777 ((s->io[i].BasePort & (align-1)) == *base))
778 return 1;
779 for (i = 0; i < MAX_IO_WIN; i++) {
780 if (s->io[i].NumPorts == 0) {
781 s->io[i].res = find_io_region(*base, num, align, s);
782 if (s->io[i].res) {
783 s->io[i].Attributes = attr;
784 s->io[i].BasePort = *base = s->io[i].res->start;
785 s->io[i].NumPorts = s->io[i].InUse = num;
786 break;
787 } else
788 return 1;
789 } else if (s->io[i].Attributes != attr)
790 continue;
791 /* Try to extend top of window */
792 try = s->io[i].BasePort + s->io[i].NumPorts;
793 if ((*base == 0) || (*base == try))
794 if (adjust_io_region(s->io[i].res, s->io[i].res->start,
795 s->io[i].res->end + num, s) == 0) {
796 *base = try;
797 s->io[i].NumPorts += num;
798 s->io[i].InUse += num;
799 break;
801 /* Try to extend bottom of window */
802 try = s->io[i].BasePort - num;
803 if ((*base == 0) || (*base == try))
804 if (adjust_io_region(s->io[i].res, s->io[i].res->start - num,
805 s->io[i].res->end, s) == 0) {
806 s->io[i].BasePort = *base = try;
807 s->io[i].NumPorts += num;
808 s->io[i].InUse += num;
809 break;
812 return (i == MAX_IO_WIN);
813 } /* alloc_io_space */
815 static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
816 ioaddr_t num)
818 int i;
820 for (i = 0; i < MAX_IO_WIN; i++) {
821 if ((s->io[i].BasePort <= base) &&
822 (s->io[i].BasePort+s->io[i].NumPorts >= base+num)) {
823 s->io[i].InUse -= num;
824 /* Free the window if no one else is using it */
825 if (s->io[i].InUse == 0) {
826 s->io[i].NumPorts = 0;
827 release_resource(s->io[i].res);
828 kfree(s->io[i].res);
829 s->io[i].res = NULL;
835 /*======================================================================
837 Access_configuration_register() reads and writes configuration
838 registers in attribute memory. Memory window 0 is reserved for
839 this and the tuple reading services.
841 ======================================================================*/
843 int pccard_access_configuration_register(struct pcmcia_socket *s,
844 unsigned int function,
845 conf_reg_t *reg)
847 config_t *c;
848 int addr;
849 u_char val;
851 if (!s || !s->config)
852 return CS_NO_CARD;
854 c = &s->config[function];
856 if (c == NULL)
857 return CS_NO_CARD;
859 if (!(c->state & CONFIG_LOCKED))
860 return CS_CONFIGURATION_LOCKED;
862 addr = (c->ConfigBase + reg->Offset) >> 1;
864 switch (reg->Action) {
865 case CS_READ:
866 read_cis_mem(s, 1, addr, 1, &val);
867 reg->Value = val;
868 break;
869 case CS_WRITE:
870 val = reg->Value;
871 write_cis_mem(s, 1, addr, 1, &val);
872 break;
873 default:
874 return CS_BAD_ARGS;
875 break;
877 return CS_SUCCESS;
878 } /* access_configuration_register */
879 EXPORT_SYMBOL(pccard_access_configuration_register);
882 /*====================================================================*/
884 int pccard_get_configuration_info(struct pcmcia_socket *s,
885 unsigned int function,
886 config_info_t *config)
888 config_t *c;
890 if (!(s->state & SOCKET_PRESENT))
891 return CS_NO_CARD;
893 config->Function = function;
895 #ifdef CONFIG_CARDBUS
896 if (s->state & SOCKET_CARDBUS) {
897 memset(config, 0, sizeof(config_info_t));
898 config->Vcc = s->socket.Vcc;
899 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
900 config->Option = s->cb_dev->subordinate->number;
901 if (s->state & SOCKET_CARDBUS_CONFIG) {
902 config->Attributes = CONF_VALID_CLIENT;
903 config->IntType = INT_CARDBUS;
904 config->AssignedIRQ = s->irq.AssignedIRQ;
905 if (config->AssignedIRQ)
906 config->Attributes |= CONF_ENABLE_IRQ;
907 config->BasePort1 = s->io[0].BasePort;
908 config->NumPorts1 = s->io[0].NumPorts;
910 return CS_SUCCESS;
912 #endif
914 c = (s->config != NULL) ? &s->config[function] : NULL;
916 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
917 config->Attributes = 0;
918 config->Vcc = s->socket.Vcc;
919 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
920 return CS_SUCCESS;
923 /* !!! This is a hack !!! */
924 memcpy(&config->Attributes, &c->Attributes, sizeof(config_t));
925 config->Attributes |= CONF_VALID_CLIENT;
926 config->CardValues = c->CardValues;
927 config->IRQAttributes = c->irq.Attributes;
928 config->AssignedIRQ = s->irq.AssignedIRQ;
929 config->BasePort1 = c->io.BasePort1;
930 config->NumPorts1 = c->io.NumPorts1;
931 config->Attributes1 = c->io.Attributes1;
932 config->BasePort2 = c->io.BasePort2;
933 config->NumPorts2 = c->io.NumPorts2;
934 config->Attributes2 = c->io.Attributes2;
935 config->IOAddrLines = c->io.IOAddrLines;
937 return CS_SUCCESS;
938 } /* get_configuration_info */
939 EXPORT_SYMBOL(pccard_get_configuration_info);
941 /*======================================================================
943 Return information about this version of Card Services.
945 ======================================================================*/
947 int pcmcia_get_card_services_info(servinfo_t *info)
949 unsigned int socket_count = 0;
950 struct list_head *tmp;
951 info->Signature[0] = 'C';
952 info->Signature[1] = 'S';
953 down_read(&pcmcia_socket_list_rwsem);
954 list_for_each(tmp, &pcmcia_socket_list)
955 socket_count++;
956 up_read(&pcmcia_socket_list_rwsem);
957 info->Count = socket_count;
958 info->Revision = CS_RELEASE_CODE;
959 info->CSLevel = 0x0210;
960 info->VendorString = (char *)release;
961 return CS_SUCCESS;
962 } /* get_card_services_info */
965 /*====================================================================*/
967 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle, int idx, win_req_t *req)
969 window_t *win;
970 int w;
972 if (!s || !(s->state & SOCKET_PRESENT))
973 return CS_NO_CARD;
974 for (w = idx; w < MAX_WIN; w++)
975 if (s->state & SOCKET_WIN_REQ(w)) break;
976 if (w == MAX_WIN)
977 return CS_NO_MORE_ITEMS;
978 win = &s->win[w];
979 req->Base = win->ctl.res->start;
980 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
981 req->AccessSpeed = win->ctl.speed;
982 req->Attributes = 0;
983 if (win->ctl.flags & MAP_ATTRIB)
984 req->Attributes |= WIN_MEMORY_TYPE_AM;
985 if (win->ctl.flags & MAP_ACTIVE)
986 req->Attributes |= WIN_ENABLE;
987 if (win->ctl.flags & MAP_16BIT)
988 req->Attributes |= WIN_DATA_WIDTH_16;
989 if (win->ctl.flags & MAP_USE_WAIT)
990 req->Attributes |= WIN_USE_WAIT;
991 *handle = win;
992 return CS_SUCCESS;
993 } /* get_window */
994 EXPORT_SYMBOL(pcmcia_get_window);
996 /*=====================================================================
998 Return the PCI device associated with a card..
1000 ======================================================================*/
1002 #ifdef CONFIG_CARDBUS
1004 struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
1006 if (!s || !(s->state & SOCKET_CARDBUS))
1007 return NULL;
1009 return s->cb_dev->subordinate;
1012 EXPORT_SYMBOL(pcmcia_lookup_bus);
1014 #endif
1016 /*======================================================================
1018 Get the current socket state bits. We don't support the latched
1019 SocketState yet: I haven't seen any point for it.
1021 ======================================================================*/
1023 int pccard_get_status(struct pcmcia_socket *s, unsigned int function, cs_status_t *status)
1025 config_t *c;
1026 int val;
1028 s->ops->get_status(s, &val);
1029 status->CardState = status->SocketState = 0;
1030 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
1031 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
1032 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
1033 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
1034 if (s->state & SOCKET_SUSPEND)
1035 status->CardState |= CS_EVENT_PM_SUSPEND;
1036 if (!(s->state & SOCKET_PRESENT))
1037 return CS_NO_CARD;
1039 c = (s->config != NULL) ? &s->config[function] : NULL;
1040 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
1041 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
1042 u_char reg;
1043 if (c->Present & PRESENT_PIN_REPLACE) {
1044 read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
1045 status->CardState |=
1046 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
1047 status->CardState |=
1048 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
1049 status->CardState |=
1050 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
1051 status->CardState |=
1052 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
1053 } else {
1054 /* No PRR? Then assume we're always ready */
1055 status->CardState |= CS_EVENT_READY_CHANGE;
1057 if (c->Present & PRESENT_EXT_STATUS) {
1058 read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
1059 status->CardState |=
1060 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
1062 return CS_SUCCESS;
1064 status->CardState |=
1065 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
1066 status->CardState |=
1067 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
1068 status->CardState |=
1069 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
1070 status->CardState |=
1071 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
1072 return CS_SUCCESS;
1073 } /* get_status */
1074 EXPORT_SYMBOL(pccard_get_status);
1076 /*======================================================================
1078 Change the card address of an already open memory window.
1080 ======================================================================*/
1082 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
1084 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1085 return CS_BAD_HANDLE;
1086 req->Page = 0;
1087 req->CardOffset = win->ctl.card_start;
1088 return CS_SUCCESS;
1089 } /* get_mem_page */
1091 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
1093 struct pcmcia_socket *s;
1094 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1095 return CS_BAD_HANDLE;
1096 if (req->Page != 0)
1097 return CS_BAD_PAGE;
1098 s = win->sock;
1099 win->ctl.card_start = req->CardOffset;
1100 if (s->ops->set_mem_map(s, &win->ctl) != 0)
1101 return CS_BAD_OFFSET;
1102 return CS_SUCCESS;
1103 } /* map_mem_page */
1105 /*======================================================================
1107 Modify a locked socket configuration
1109 ======================================================================*/
1111 int pcmcia_modify_configuration(client_handle_t handle,
1112 modconf_t *mod)
1114 struct pcmcia_socket *s;
1115 config_t *c;
1117 if (CHECK_HANDLE(handle))
1118 return CS_BAD_HANDLE;
1119 s = SOCKET(handle); c = CONFIG(handle);
1120 if (!(s->state & SOCKET_PRESENT))
1121 return CS_NO_CARD;
1122 if (!(c->state & CONFIG_LOCKED))
1123 return CS_CONFIGURATION_LOCKED;
1125 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
1126 if (mod->Attributes & CONF_ENABLE_IRQ) {
1127 c->Attributes |= CONF_ENABLE_IRQ;
1128 s->socket.io_irq = s->irq.AssignedIRQ;
1129 } else {
1130 c->Attributes &= ~CONF_ENABLE_IRQ;
1131 s->socket.io_irq = 0;
1133 s->ops->set_socket(s, &s->socket);
1136 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
1137 return CS_BAD_VCC;
1139 /* We only allow changing Vpp1 and Vpp2 to the same value */
1140 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
1141 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
1142 if (mod->Vpp1 != mod->Vpp2)
1143 return CS_BAD_VPP;
1144 c->Vpp1 = c->Vpp2 = s->socket.Vpp = mod->Vpp1;
1145 if (s->ops->set_socket(s, &s->socket))
1146 return CS_BAD_VPP;
1147 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
1148 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
1149 return CS_BAD_VPP;
1151 return CS_SUCCESS;
1152 } /* modify_configuration */
1154 /* register pcmcia_callback */
1155 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
1157 int ret = 0;
1159 /* s->skt_sem also protects s->callback */
1160 down(&s->skt_sem);
1162 if (c) {
1163 /* registration */
1164 if (s->callback) {
1165 ret = -EBUSY;
1166 goto err;
1169 s->callback = c;
1171 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
1172 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1173 } else
1174 s->callback = NULL;
1175 err:
1176 up(&s->skt_sem);
1178 return ret;
1180 EXPORT_SYMBOL(pccard_register_pcmcia);
1182 /*====================================================================*/
1184 int pcmcia_release_configuration(client_handle_t handle)
1186 pccard_io_map io = { 0, 0, 0, 0, 1 };
1187 struct pcmcia_socket *s;
1188 int i;
1190 if (CHECK_HANDLE(handle) ||
1191 !(handle->state & CLIENT_CONFIG_LOCKED))
1192 return CS_BAD_HANDLE;
1193 handle->state &= ~CLIENT_CONFIG_LOCKED;
1194 s = SOCKET(handle);
1196 #ifdef CONFIG_CARDBUS
1197 if (handle->state & CLIENT_CARDBUS)
1198 return CS_SUCCESS;
1199 #endif
1201 if (!(handle->state & CLIENT_STALE)) {
1202 config_t *c = CONFIG(handle);
1203 if (--(s->lock_count) == 0) {
1204 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
1205 s->socket.Vpp = 0;
1206 s->socket.io_irq = 0;
1207 s->ops->set_socket(s, &s->socket);
1209 if (c->state & CONFIG_IO_REQ)
1210 for (i = 0; i < MAX_IO_WIN; i++) {
1211 if (s->io[i].NumPorts == 0)
1212 continue;
1213 s->io[i].Config--;
1214 if (s->io[i].Config != 0)
1215 continue;
1216 io.map = i;
1217 s->ops->set_io_map(s, &io);
1219 c->state &= ~CONFIG_LOCKED;
1222 return CS_SUCCESS;
1223 } /* release_configuration */
1225 /*======================================================================
1227 Release_io() releases the I/O ranges allocated by a client. This
1228 may be invoked some time after a card ejection has already dumped
1229 the actual socket configuration, so if the client is "stale", we
1230 don't bother checking the port ranges against the current socket
1231 values.
1233 ======================================================================*/
1235 int pcmcia_release_io(client_handle_t handle, io_req_t *req)
1237 struct pcmcia_socket *s;
1239 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IO_REQ))
1240 return CS_BAD_HANDLE;
1241 handle->state &= ~CLIENT_IO_REQ;
1242 s = SOCKET(handle);
1244 #ifdef CONFIG_CARDBUS
1245 if (handle->state & CLIENT_CARDBUS)
1246 return CS_SUCCESS;
1247 #endif
1249 if (!(handle->state & CLIENT_STALE)) {
1250 config_t *c = CONFIG(handle);
1251 if (c->state & CONFIG_LOCKED)
1252 return CS_CONFIGURATION_LOCKED;
1253 if ((c->io.BasePort1 != req->BasePort1) ||
1254 (c->io.NumPorts1 != req->NumPorts1) ||
1255 (c->io.BasePort2 != req->BasePort2) ||
1256 (c->io.NumPorts2 != req->NumPorts2))
1257 return CS_BAD_ARGS;
1258 c->state &= ~CONFIG_IO_REQ;
1261 release_io_space(s, req->BasePort1, req->NumPorts1);
1262 if (req->NumPorts2)
1263 release_io_space(s, req->BasePort2, req->NumPorts2);
1265 return CS_SUCCESS;
1266 } /* release_io */
1268 /*====================================================================*/
1270 int pcmcia_release_irq(client_handle_t handle, irq_req_t *req)
1272 struct pcmcia_socket *s;
1273 if (CHECK_HANDLE(handle) || !(handle->state & CLIENT_IRQ_REQ))
1274 return CS_BAD_HANDLE;
1275 handle->state &= ~CLIENT_IRQ_REQ;
1276 s = SOCKET(handle);
1278 if (!(handle->state & CLIENT_STALE)) {
1279 config_t *c = CONFIG(handle);
1280 if (c->state & CONFIG_LOCKED)
1281 return CS_CONFIGURATION_LOCKED;
1282 if (c->irq.Attributes != req->Attributes)
1283 return CS_BAD_ATTRIBUTE;
1284 if (s->irq.AssignedIRQ != req->AssignedIRQ)
1285 return CS_BAD_IRQ;
1286 if (--s->irq.Config == 0) {
1287 c->state &= ~CONFIG_IRQ_REQ;
1288 s->irq.AssignedIRQ = 0;
1292 if (req->Attributes & IRQ_HANDLE_PRESENT) {
1293 free_irq(req->AssignedIRQ, req->Instance);
1296 #ifdef CONFIG_PCMCIA_PROBE
1297 pcmcia_used_irq[req->AssignedIRQ]--;
1298 #endif
1300 return CS_SUCCESS;
1301 } /* cs_release_irq */
1303 /*====================================================================*/
1305 int pcmcia_release_window(window_handle_t win)
1307 struct pcmcia_socket *s;
1309 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
1310 return CS_BAD_HANDLE;
1311 s = win->sock;
1312 if (!(win->handle->state & CLIENT_WIN_REQ(win->index)))
1313 return CS_BAD_HANDLE;
1315 /* Shut down memory window */
1316 win->ctl.flags &= ~MAP_ACTIVE;
1317 s->ops->set_mem_map(s, &win->ctl);
1318 s->state &= ~SOCKET_WIN_REQ(win->index);
1320 /* Release system memory */
1321 if (win->ctl.res) {
1322 release_resource(win->ctl.res);
1323 kfree(win->ctl.res);
1324 win->ctl.res = NULL;
1326 win->handle->state &= ~CLIENT_WIN_REQ(win->index);
1328 win->magic = 0;
1330 return CS_SUCCESS;
1331 } /* release_window */
1333 /*====================================================================*/
1335 int pcmcia_request_configuration(client_handle_t handle,
1336 config_req_t *req)
1338 int i;
1339 u_int base;
1340 struct pcmcia_socket *s;
1341 config_t *c;
1342 pccard_io_map iomap;
1344 if (CHECK_HANDLE(handle))
1345 return CS_BAD_HANDLE;
1346 s = SOCKET(handle);
1347 if (!(s->state & SOCKET_PRESENT))
1348 return CS_NO_CARD;
1350 #ifdef CONFIG_CARDBUS
1351 if (handle->state & CLIENT_CARDBUS)
1352 return CS_UNSUPPORTED_MODE;
1353 #endif
1355 if (req->IntType & INT_CARDBUS)
1356 return CS_UNSUPPORTED_MODE;
1357 c = CONFIG(handle);
1358 if (c->state & CONFIG_LOCKED)
1359 return CS_CONFIGURATION_LOCKED;
1361 /* Do power control. We don't allow changes in Vcc. */
1362 if (s->socket.Vcc != req->Vcc)
1363 return CS_BAD_VCC;
1364 if (req->Vpp1 != req->Vpp2)
1365 return CS_BAD_VPP;
1366 s->socket.Vpp = req->Vpp1;
1367 if (s->ops->set_socket(s, &s->socket))
1368 return CS_BAD_VPP;
1370 c->Vcc = req->Vcc; c->Vpp1 = c->Vpp2 = req->Vpp1;
1372 /* Pick memory or I/O card, DMA mode, interrupt */
1373 c->IntType = req->IntType;
1374 c->Attributes = req->Attributes;
1375 if (req->IntType & INT_MEMORY_AND_IO)
1376 s->socket.flags |= SS_IOCARD;
1377 if (req->IntType & INT_ZOOMED_VIDEO)
1378 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
1379 if (req->Attributes & CONF_ENABLE_DMA)
1380 s->socket.flags |= SS_DMA_MODE;
1381 if (req->Attributes & CONF_ENABLE_SPKR)
1382 s->socket.flags |= SS_SPKR_ENA;
1383 if (req->Attributes & CONF_ENABLE_IRQ)
1384 s->socket.io_irq = s->irq.AssignedIRQ;
1385 else
1386 s->socket.io_irq = 0;
1387 s->ops->set_socket(s, &s->socket);
1388 s->lock_count++;
1390 /* Set up CIS configuration registers */
1391 base = c->ConfigBase = req->ConfigBase;
1392 c->Present = c->CardValues = req->Present;
1393 if (req->Present & PRESENT_COPY) {
1394 c->Copy = req->Copy;
1395 write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
1397 if (req->Present & PRESENT_OPTION) {
1398 if (s->functions == 1) {
1399 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
1400 } else {
1401 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
1402 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
1403 if (req->Present & PRESENT_IOBASE_0)
1404 c->Option |= COR_ADDR_DECODE;
1406 if (c->state & CONFIG_IRQ_REQ)
1407 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
1408 c->Option |= COR_LEVEL_REQ;
1409 write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
1410 mdelay(40);
1412 if (req->Present & PRESENT_STATUS) {
1413 c->Status = req->Status;
1414 write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
1416 if (req->Present & PRESENT_PIN_REPLACE) {
1417 c->Pin = req->Pin;
1418 write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
1420 if (req->Present & PRESENT_EXT_STATUS) {
1421 c->ExtStatus = req->ExtStatus;
1422 write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
1424 if (req->Present & PRESENT_IOBASE_0) {
1425 u_char b = c->io.BasePort1 & 0xff;
1426 write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
1427 b = (c->io.BasePort1 >> 8) & 0xff;
1428 write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
1430 if (req->Present & PRESENT_IOSIZE) {
1431 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
1432 write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
1435 /* Configure I/O windows */
1436 if (c->state & CONFIG_IO_REQ) {
1437 iomap.speed = io_speed;
1438 for (i = 0; i < MAX_IO_WIN; i++)
1439 if (s->io[i].NumPorts != 0) {
1440 iomap.map = i;
1441 iomap.flags = MAP_ACTIVE;
1442 switch (s->io[i].Attributes & IO_DATA_PATH_WIDTH) {
1443 case IO_DATA_PATH_WIDTH_16:
1444 iomap.flags |= MAP_16BIT; break;
1445 case IO_DATA_PATH_WIDTH_AUTO:
1446 iomap.flags |= MAP_AUTOSZ; break;
1447 default:
1448 break;
1450 iomap.start = s->io[i].BasePort;
1451 iomap.stop = iomap.start + s->io[i].NumPorts - 1;
1452 s->ops->set_io_map(s, &iomap);
1453 s->io[i].Config++;
1457 c->state |= CONFIG_LOCKED;
1458 handle->state |= CLIENT_CONFIG_LOCKED;
1459 return CS_SUCCESS;
1460 } /* request_configuration */
1462 /*======================================================================
1464 Request_io() reserves ranges of port addresses for a socket.
1465 I have not implemented range sharing or alias addressing.
1467 ======================================================================*/
1469 int pcmcia_request_io(client_handle_t handle, io_req_t *req)
1471 struct pcmcia_socket *s;
1472 config_t *c;
1474 if (CHECK_HANDLE(handle))
1475 return CS_BAD_HANDLE;
1476 s = SOCKET(handle);
1477 if (!(s->state & SOCKET_PRESENT))
1478 return CS_NO_CARD;
1480 if (handle->state & CLIENT_CARDBUS) {
1481 #ifdef CONFIG_CARDBUS
1482 handle->state |= CLIENT_IO_REQ;
1483 return CS_SUCCESS;
1484 #else
1485 return CS_UNSUPPORTED_FUNCTION;
1486 #endif
1489 if (!req)
1490 return CS_UNSUPPORTED_MODE;
1491 c = CONFIG(handle);
1492 if (c->state & CONFIG_LOCKED)
1493 return CS_CONFIGURATION_LOCKED;
1494 if (c->state & CONFIG_IO_REQ)
1495 return CS_IN_USE;
1496 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
1497 return CS_BAD_ATTRIBUTE;
1498 if ((req->NumPorts2 > 0) &&
1499 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
1500 return CS_BAD_ATTRIBUTE;
1502 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
1503 req->NumPorts1, req->IOAddrLines))
1504 return CS_IN_USE;
1506 if (req->NumPorts2) {
1507 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
1508 req->NumPorts2, req->IOAddrLines)) {
1509 release_io_space(s, req->BasePort1, req->NumPorts1);
1510 return CS_IN_USE;
1514 c->io = *req;
1515 c->state |= CONFIG_IO_REQ;
1516 handle->state |= CLIENT_IO_REQ;
1517 return CS_SUCCESS;
1518 } /* request_io */
1520 /*======================================================================
1522 Request_irq() reserves an irq for this client.
1524 Also, since Linux only reserves irq's when they are actually
1525 hooked, we don't guarantee that an irq will still be available
1526 when the configuration is locked. Now that I think about it,
1527 there might be a way to fix this using a dummy handler.
1529 ======================================================================*/
1531 #ifdef CONFIG_PCMCIA_PROBE
1532 static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
1534 return IRQ_NONE;
1536 #endif
1538 int pcmcia_request_irq(client_handle_t handle, irq_req_t *req)
1540 struct pcmcia_socket *s;
1541 config_t *c;
1542 int ret = CS_IN_USE, irq = 0;
1543 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1545 if (CHECK_HANDLE(handle))
1546 return CS_BAD_HANDLE;
1547 s = SOCKET(handle);
1548 if (!(s->state & SOCKET_PRESENT))
1549 return CS_NO_CARD;
1550 c = CONFIG(handle);
1551 if (c->state & CONFIG_LOCKED)
1552 return CS_CONFIGURATION_LOCKED;
1553 if (c->state & CONFIG_IRQ_REQ)
1554 return CS_IN_USE;
1556 #ifdef CONFIG_PCMCIA_PROBE
1557 if (s->irq.AssignedIRQ != 0) {
1558 /* If the interrupt is already assigned, it must be the same */
1559 irq = s->irq.AssignedIRQ;
1560 } else {
1561 int try;
1562 u32 mask = s->irq_mask;
1563 void *data = NULL;
1565 for (try = 0; try < 64; try++) {
1566 irq = try % 32;
1568 /* marked as available by driver, and not blocked by userspace? */
1569 if (!((mask >> irq) & 1))
1570 continue;
1572 /* avoid an IRQ which is already used by a PCMCIA card */
1573 if ((try < 32) && pcmcia_used_irq[irq])
1574 continue;
1576 /* register the correct driver, if possible, of check whether
1577 * registering a dummy handle works, i.e. if the IRQ isn't
1578 * marked as used by the kernel resource management core */
1579 ret = request_irq(irq,
1580 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
1581 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
1582 (s->functions > 1) ||
1583 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
1584 p_dev->dev.bus_id,
1585 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
1586 if (!ret) {
1587 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
1588 free_irq(irq, data);
1589 break;
1593 #endif
1594 if (ret) {
1595 if (!s->pci_irq)
1596 return ret;
1597 irq = s->pci_irq;
1600 if (ret && req->Attributes & IRQ_HANDLE_PRESENT) {
1601 if (request_irq(irq, req->Handler,
1602 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
1603 (s->functions > 1) ||
1604 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
1605 p_dev->dev.bus_id, req->Instance))
1606 return CS_IN_USE;
1609 c->irq.Attributes = req->Attributes;
1610 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
1611 s->irq.Config++;
1613 c->state |= CONFIG_IRQ_REQ;
1614 handle->state |= CLIENT_IRQ_REQ;
1616 #ifdef CONFIG_PCMCIA_PROBE
1617 pcmcia_used_irq[irq]++;
1618 #endif
1620 return CS_SUCCESS;
1621 } /* pcmcia_request_irq */
1623 /*======================================================================
1625 Request_window() establishes a mapping between card memory space
1626 and system memory space.
1628 ======================================================================*/
1630 int pcmcia_request_window(client_handle_t *handle, win_req_t *req, window_handle_t *wh)
1632 struct pcmcia_socket *s;
1633 window_t *win;
1634 u_long align;
1635 int w;
1637 if (CHECK_HANDLE(*handle))
1638 return CS_BAD_HANDLE;
1639 s = (*handle)->Socket;
1640 if (!(s->state & SOCKET_PRESENT))
1641 return CS_NO_CARD;
1642 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
1643 return CS_BAD_ATTRIBUTE;
1645 /* Window size defaults to smallest available */
1646 if (req->Size == 0)
1647 req->Size = s->map_size;
1648 align = (((s->features & SS_CAP_MEM_ALIGN) ||
1649 (req->Attributes & WIN_STRICT_ALIGN)) ?
1650 req->Size : s->map_size);
1651 if (req->Size & (s->map_size-1))
1652 return CS_BAD_SIZE;
1653 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
1654 (req->Base & (align-1)))
1655 return CS_BAD_BASE;
1656 if (req->Base)
1657 align = 0;
1659 /* Allocate system memory window */
1660 for (w = 0; w < MAX_WIN; w++)
1661 if (!(s->state & SOCKET_WIN_REQ(w))) break;
1662 if (w == MAX_WIN)
1663 return CS_OUT_OF_RESOURCE;
1665 win = &s->win[w];
1666 win->magic = WINDOW_MAGIC;
1667 win->index = w;
1668 win->handle = *handle;
1669 win->sock = s;
1671 if (!(s->features & SS_CAP_STATIC_MAP)) {
1672 win->ctl.res = find_mem_region(req->Base, req->Size, align,
1673 (req->Attributes & WIN_MAP_BELOW_1MB), s);
1674 if (!win->ctl.res)
1675 return CS_IN_USE;
1677 (*handle)->state |= CLIENT_WIN_REQ(w);
1679 /* Configure the socket controller */
1680 win->ctl.map = w+1;
1681 win->ctl.flags = 0;
1682 win->ctl.speed = req->AccessSpeed;
1683 if (req->Attributes & WIN_MEMORY_TYPE)
1684 win->ctl.flags |= MAP_ATTRIB;
1685 if (req->Attributes & WIN_ENABLE)
1686 win->ctl.flags |= MAP_ACTIVE;
1687 if (req->Attributes & WIN_DATA_WIDTH_16)
1688 win->ctl.flags |= MAP_16BIT;
1689 if (req->Attributes & WIN_USE_WAIT)
1690 win->ctl.flags |= MAP_USE_WAIT;
1691 win->ctl.card_start = 0;
1692 if (s->ops->set_mem_map(s, &win->ctl) != 0)
1693 return CS_BAD_ARGS;
1694 s->state |= SOCKET_WIN_REQ(w);
1696 /* Return window handle */
1697 if (s->features & SS_CAP_STATIC_MAP) {
1698 req->Base = win->ctl.static_start;
1699 } else {
1700 req->Base = win->ctl.res->start;
1702 *wh = win;
1704 return CS_SUCCESS;
1705 } /* request_window */
1707 /*======================================================================
1709 I'm not sure which "reset" function this is supposed to use,
1710 but for now, it uses the low-level interface's reset, not the
1711 CIS register.
1713 ======================================================================*/
1715 int pccard_reset_card(struct pcmcia_socket *skt)
1717 int ret;
1719 cs_dbg(skt, 1, "resetting socket\n");
1721 down(&skt->skt_sem);
1722 do {
1723 if (!(skt->state & SOCKET_PRESENT)) {
1724 ret = CS_NO_CARD;
1725 break;
1727 if (skt->state & SOCKET_SUSPEND) {
1728 ret = CS_IN_USE;
1729 break;
1731 if (skt->state & SOCKET_CARDBUS) {
1732 ret = CS_UNSUPPORTED_FUNCTION;
1733 break;
1736 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
1737 if (ret == 0) {
1738 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
1739 if (socket_reset(skt) == CS_SUCCESS)
1740 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
1743 ret = CS_SUCCESS;
1744 } while (0);
1745 up(&skt->skt_sem);
1747 return ret;
1748 } /* reset_card */
1749 EXPORT_SYMBOL(pccard_reset_card);
1751 /*======================================================================
1753 These shut down or wake up a socket. They are sort of user
1754 initiated versions of the APM suspend and resume actions.
1756 ======================================================================*/
1758 int pcmcia_suspend_card(struct pcmcia_socket *skt)
1760 int ret;
1762 cs_dbg(skt, 1, "suspending socket\n");
1764 down(&skt->skt_sem);
1765 do {
1766 if (!(skt->state & SOCKET_PRESENT)) {
1767 ret = CS_NO_CARD;
1768 break;
1770 if (skt->state & SOCKET_CARDBUS) {
1771 ret = CS_UNSUPPORTED_FUNCTION;
1772 break;
1774 ret = socket_suspend(skt);
1775 } while (0);
1776 up(&skt->skt_sem);
1778 return ret;
1779 } /* suspend_card */
1781 int pcmcia_resume_card(struct pcmcia_socket *skt)
1783 int ret;
1785 cs_dbg(skt, 1, "waking up socket\n");
1787 down(&skt->skt_sem);
1788 do {
1789 if (!(skt->state & SOCKET_PRESENT)) {
1790 ret = CS_NO_CARD;
1791 break;
1793 if (skt->state & SOCKET_CARDBUS) {
1794 ret = CS_UNSUPPORTED_FUNCTION;
1795 break;
1797 ret = socket_resume(skt);
1798 } while (0);
1799 up(&skt->skt_sem);
1801 return ret;
1802 } /* resume_card */
1804 /*======================================================================
1806 These handle user requests to eject or insert a card.
1808 ======================================================================*/
1810 int pcmcia_eject_card(struct pcmcia_socket *skt)
1812 int ret;
1814 cs_dbg(skt, 1, "user eject request\n");
1816 down(&skt->skt_sem);
1817 do {
1818 if (!(skt->state & SOCKET_PRESENT)) {
1819 ret = -ENODEV;
1820 break;
1823 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
1824 if (ret != 0) {
1825 ret = -EINVAL;
1826 break;
1829 socket_remove(skt);
1830 ret = 0;
1831 } while (0);
1832 up(&skt->skt_sem);
1834 return ret;
1835 } /* eject_card */
1837 int pcmcia_insert_card(struct pcmcia_socket *skt)
1839 int ret;
1841 cs_dbg(skt, 1, "user insert request\n");
1843 down(&skt->skt_sem);
1844 do {
1845 if (skt->state & SOCKET_PRESENT) {
1846 ret = -EBUSY;
1847 break;
1849 if (socket_insert(skt) == CS_NO_CARD) {
1850 ret = -ENODEV;
1851 break;
1853 ret = 0;
1854 } while (0);
1855 up(&skt->skt_sem);
1857 return ret;
1858 } /* insert_card */
1860 /*======================================================================
1862 OS-specific module glue goes here
1864 ======================================================================*/
1865 /* in alpha order */
1866 EXPORT_SYMBOL(pcmcia_eject_card);
1867 EXPORT_SYMBOL(pcmcia_get_card_services_info);
1868 EXPORT_SYMBOL(pcmcia_get_mem_page);
1869 EXPORT_SYMBOL(pcmcia_insert_card);
1870 EXPORT_SYMBOL(pcmcia_map_mem_page);
1871 EXPORT_SYMBOL(pcmcia_modify_configuration);
1872 EXPORT_SYMBOL(pcmcia_release_configuration);
1873 EXPORT_SYMBOL(pcmcia_release_io);
1874 EXPORT_SYMBOL(pcmcia_release_irq);
1875 EXPORT_SYMBOL(pcmcia_release_window);
1876 EXPORT_SYMBOL(pcmcia_replace_cis);
1877 EXPORT_SYMBOL(pcmcia_request_configuration);
1878 EXPORT_SYMBOL(pcmcia_request_io);
1879 EXPORT_SYMBOL(pcmcia_request_irq);
1880 EXPORT_SYMBOL(pcmcia_request_window);
1881 EXPORT_SYMBOL(pcmcia_resume_card);
1882 EXPORT_SYMBOL(pcmcia_suspend_card);
1884 EXPORT_SYMBOL(dead_socket);
1885 EXPORT_SYMBOL(pcmcia_parse_events);
1887 struct class pcmcia_socket_class = {
1888 .name = "pcmcia_socket",
1889 .release = pcmcia_release_socket,
1891 EXPORT_SYMBOL(pcmcia_socket_class);
1894 static int __init init_pcmcia_cs(void)
1896 int ret;
1897 printk(KERN_INFO "%s\n", release);
1898 printk(KERN_INFO " %s\n", options);
1900 ret = class_register(&pcmcia_socket_class);
1901 if (ret)
1902 return (ret);
1903 return class_interface_register(&pccard_sysfs_interface);
1906 static void __exit exit_pcmcia_cs(void)
1908 printk(KERN_INFO "unloading Kernel Card Services\n");
1909 class_interface_unregister(&pccard_sysfs_interface);
1910 class_unregister(&pcmcia_socket_class);
1913 subsys_initcall(init_pcmcia_cs);
1914 module_exit(exit_pcmcia_cs);
1916 /*====================================================================*/