blob: 03fc885db1c57e9325915640dacc023eef3c829f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cs.c -- Kernel Card Services - core services
3 *
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.
7 *
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.
11 *
12 * (C) 1999 David A. Hinds
13 */
14
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>
34
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"
45
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
66
67static const char *release = "Linux Kernel Card Services";
68static const char *options = "options: " OPTIONS;
69
70/*====================================================================*/
71
72/* Module parameters */
73
74MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
75MODULE_DESCRIPTION("Linux Kernel Card Services\noptions:" OPTIONS);
76MODULE_LICENSE("GPL");
77
78#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
79
80INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
81INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
82INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
83INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
84INT_MODULE_PARM(reset_time, 10); /* usecs */
85INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
86INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
87INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
88
89/* Access speed for attribute memory windows */
90INT_MODULE_PARM(cis_speed, 300); /* ns */
91
92/* Access speed for IO windows */
93INT_MODULE_PARM(io_speed, 0); /* ns */
94
95#ifdef DEBUG
96static int pc_debug;
97
98module_param(pc_debug, int, 0644);
99
100int cs_debug_level(int level)
101{
102 return pc_debug > level;
103}
104#endif
105
106/*====================================================================*/
107
108socket_state_t dead_socket = {
109 .csc_mask = SS_DETECT,
110};
111
112
113/* List of all sockets, protected by a rwsem */
114LIST_HEAD(pcmcia_socket_list);
115DECLARE_RWSEM(pcmcia_socket_list_rwsem);
116EXPORT_SYMBOL(pcmcia_socket_list);
117EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
118
119
120#ifdef CONFIG_PCMCIA_PROBE
121/* mask ofIRQs already reserved by other cards, we should avoid using them */
122static u8 pcmcia_used_irq[NR_IRQS];
123#endif
124
125/*====================================================================
126
127 Low-level PC Card interface drivers need to register with Card
128 Services using these calls.
129
130======================================================================*/
131
132/**
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
139 */
140static int socket_resume(struct pcmcia_socket *skt);
141static int socket_suspend(struct pcmcia_socket *skt);
142
143int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
144{
145 struct pcmcia_socket *socket;
146
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);
154 }
155 up_read(&pcmcia_socket_list_rwsem);
156
157 return 0;
158}
159EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
160
161int pcmcia_socket_dev_resume(struct device *dev)
162{
163 struct pcmcia_socket *socket;
164
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);
172 }
173 up_read(&pcmcia_socket_list_rwsem);
174
175 return 0;
176}
177EXPORT_SYMBOL(pcmcia_socket_dev_resume);
178
179
180struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
181{
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;
189 }
190 return (skt);
191}
192EXPORT_SYMBOL(pcmcia_get_socket);
193
194
195void pcmcia_put_socket(struct pcmcia_socket *skt)
196{
197 module_put(skt->owner);
198 class_device_put(&skt->dev);
199}
200EXPORT_SYMBOL(pcmcia_put_socket);
201
202
203static void pcmcia_release_socket(struct class_device *class_dev)
204{
205 struct pcmcia_socket *socket = class_get_devdata(class_dev);
206
207 complete(&socket->socket_released);
208}
209
210static int pccardd(void *__skt);
211
212/**
213 * pcmcia_register_socket - add a new pcmcia socket device
214 */
215int pcmcia_register_socket(struct pcmcia_socket *socket)
216{
217 int ret;
218
219 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
220 return -EINVAL;
221
222 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
223
224 spin_lock_init(&socket->lock);
225
226 if (socket->resource_ops->init) {
227 ret = socket->resource_ops->init(socket);
228 if (ret)
229 return (ret);
230 }
231
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;
247 }
248 i++;
249 } while (!found);
250 socket->sock = i - 1;
251 }
252 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
253 up_write(&pcmcia_socket_list_rwsem);
254
255
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);
260
261 /* base address = 0, map = 0 */
262 socket->cis_mem.flags = 0;
263 socket->cis_mem.speed = cis_speed;
264
265 INIT_LIST_HEAD(&socket->cis_cache);
266
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);
272
273 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
274 if (ret < 0)
275 goto err;
276
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;
281 }
282 pcmcia_parse_events(socket, SS_DETECT);
283
284 return 0;
285
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 */
292EXPORT_SYMBOL(pcmcia_register_socket);
293
294
295/**
296 * pcmcia_unregister_socket - remove a pcmcia socket device
297 */
298void pcmcia_unregister_socket(struct pcmcia_socket *socket)
299{
300 if (!socket)
301 return;
302
303 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
304
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);
310 }
311 release_cis_mem(socket);
312
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);
317
318 /* wait for sysfs to drop all references */
319 release_resource_db(socket);
320 wait_for_completion(&socket->socket_released);
321} /* pcmcia_unregister_socket */
322EXPORT_SYMBOL(pcmcia_unregister_socket);
323
324
325struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
326{
327 struct pcmcia_socket *s;
328
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;
334 }
335 up_read(&pcmcia_socket_list_rwsem);
336
337 return NULL;
338
339}
340EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
341
342
343/*======================================================================
344
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.
349
350======================================================================*/
351
352static void shutdown_socket(struct pcmcia_socket *s)
353{
354 cs_dbg(s, 1, "shutdown_socket\n");
355
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;
370 }
371
372 {
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);
377 }
378 }
379} /* shutdown_socket */
380
381/*======================================================================
382
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.
388
389======================================================================*/
390
391
392/* NOTE: send_event needs to be called with skt->sem held. */
393
394static int send_event(struct pcmcia_socket *s, event_t event, int priority)
395{
396 int ret;
397
398 if (s->state & SOCKET_CARDBUS)
399 return 0;
400
401 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
402 event, priority, s->callback);
403
404 if (!s->callback)
405 return 0;
406 if (!try_module_get(s->callback->owner))
407 return 0;
408
409 ret = s->callback->event(s, event, priority);
410
411 module_put(s->callback->owner);
412
413 return ret;
414}
415
416static void socket_remove_drivers(struct pcmcia_socket *skt)
417{
418 cs_dbg(skt, 4, "remove_drivers\n");
419
420 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
421}
422
423static void socket_shutdown(struct pcmcia_socket *skt)
424{
425 cs_dbg(skt, 4, "shutdown\n");
426
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);
432}
433
434static int socket_reset(struct pcmcia_socket *skt)
435{
436 int status, i;
437
438 cs_dbg(skt, 4, "reset\n");
439
440 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
441 skt->ops->set_socket(skt, &skt->socket);
442 udelay((long)reset_time);
443
444 skt->socket.flags &= ~SS_RESET;
445 skt->ops->set_socket(skt, &skt->socket);
446
447 msleep(unreset_delay * 10);
448 for (i = 0; i < unreset_limit; i++) {
449 skt->ops->get_status(skt, &status);
450
451 if (!(status & SS_DETECT))
452 return CS_NO_CARD;
453
454 if (status & SS_READY)
455 return CS_SUCCESS;
456
457 msleep(unreset_check * 10);
458 }
459
460 cs_err(skt, "time out after reset.\n");
461 return CS_GENERAL_FAILURE;
462}
463
464static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
465{
466 int status, i;
467
468 cs_dbg(skt, 4, "setup\n");
469
470 skt->ops->get_status(skt, &status);
471 if (!(status & SS_DETECT))
472 return CS_NO_CARD;
473
474 msleep(initial_delay * 10);
475
476 for (i = 0; i < 100; i++) {
477 skt->ops->get_status(skt, &status);
478 if (!(status & SS_DETECT))
479 return CS_NO_CARD;
480
481 if (!(status & SS_PENDING))
482 break;
483
484 msleep(100);
485 }
486
487 if (status & SS_PENDING) {
488 cs_err(skt, "voltage interrogation timed out.\n");
489 return CS_GENERAL_FAILURE;
490 }
491
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
498 }
499
500 /*
501 * Decode the card voltage requirements, and apply power to the card.
502 */
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;
510 }
511 skt->socket.flags = 0;
512 skt->ops->set_socket(skt, &skt->socket);
513
514 /*
515 * Wait "vcc_settle" for the supply to stabilise.
516 */
517 msleep(vcc_settle * 10);
518
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;
523 }
524
525 return socket_reset(skt);
526}
527
528/*
529 * Handle card insertion. Setup the socket, reset the card,
530 * and then tell the rest of PCMCIA that a card is present.
531 */
532static int socket_insert(struct pcmcia_socket *skt)
533{
534 int ret;
535
536 cs_dbg(skt, 4, "insert\n");
537
538 if (!cs_socket_get(skt))
539 return CS_NO_CARD;
540
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;
548 }
549#endif
550 cs_dbg(skt, 4, "insert done\n");
551
552 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
553 } else {
554 socket_shutdown(skt);
555 cs_socket_put(skt);
556 }
557
558 return ret;
559}
560
561static int socket_suspend(struct pcmcia_socket *skt)
562{
563 if (skt->state & SOCKET_SUSPEND)
564 return CS_IN_USE;
565
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;
572
573 return CS_SUCCESS;
574}
575
576/*
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.
580 */
581static int socket_resume(struct pcmcia_socket *skt)
582{
583 int ret;
584
585 if (!(skt->state & SOCKET_SUSPEND))
586 return CS_IN_USE;
587
588 skt->socket = dead_socket;
589 skt->ops->init(skt);
590 skt->ops->set_socket(skt, &skt->socket);
591
592 if (!(skt->state & SOCKET_PRESENT)) {
593 skt->state &= ~SOCKET_SUSPEND;
594 return socket_insert(skt);
595 }
596
597 ret = socket_setup(skt, resume_delay);
598 if (ret == CS_SUCCESS) {
599 /*
600 * FIXME: need a better check here for cardbus cards.
601 */
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);
606 /*
607 * Workaround: give DS time to schedule removal.
608 * Remove me once the 100ms delay is eliminated
609 * in ds.c
610 */
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);
616 }
617 } else {
618 socket_shutdown(skt);
619 cs_socket_put(skt);
620 }
621
622 skt->state &= ~SOCKET_SUSPEND;
623
624 return CS_SUCCESS;
625}
626
627static void socket_remove(struct pcmcia_socket *skt)
628{
629 socket_shutdown(skt);
630 cs_socket_put(skt);
631}
632
633/*
634 * Process a socket card detect status change.
635 *
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.
638 *
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.
643 */
644static void socket_detect_change(struct pcmcia_socket *skt)
645{
646 if (!(skt->state & SOCKET_SUSPEND)) {
647 int status;
648
649 if (!(skt->state & SOCKET_PRESENT))
650 msleep(20);
651
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);
659 }
660}
661
662static int pccardd(void *__skt)
663{
664 struct pcmcia_socket *skt = __skt;
665 DECLARE_WAITQUEUE(wait, current);
666 int ret;
667
668 daemonize("pccardd");
669
670 skt->thread = current;
671 skt->socket = dead_socket;
672 skt->ops->init(skt);
673 skt->ops->set_socket(skt, &skt->socket);
674
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);
682 }
683 complete(&skt->thread_done);
684
685 add_wait_queue(&skt->thread_wait, &wait);
686 for (;;) {
687 unsigned long flags;
688 unsigned int events;
689
690 set_current_state(TASK_INTERRUPTIBLE);
691
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);
696
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;
709 }
710
711 schedule();
712 try_to_freeze(PF_FREEZE);
713
714 if (!skt->thread)
715 break;
716 }
717 remove_wait_queue(&skt->thread_wait, &wait);
718
719 /* remove from the device core */
720 class_device_unregister(&skt->dev);
721
722 complete_and_exit(&skt->thread_done, 0);
723}
724
725/*
726 * Yenta (at least) probes interrupts before registering the socket and
727 * starting the handler thread.
728 */
729void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
730{
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);
736
737 wake_up(&s->thread_wait);
738 }
739} /* pcmcia_parse_events */
740
741
742/*======================================================================
743
744 Special stuff for managing IO windows, because they are scarce.
745
746======================================================================*/
747
748static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
749 ioaddr_t num, u_int lines)
750{
751 int i;
752 kio_addr_t try, align;
753
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;
762 }
763 if (*base & ~(align-1)) {
764 cs_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
765 *base, align);
766 align = 0;
767 }
768 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
769 *base = s->io_offset | (*base & 0x0fff);
770 return 0;
771 }
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;
800 }
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;
810 }
811 }
812 return (i == MAX_IO_WIN);
813} /* alloc_io_space */
814
815static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
816 ioaddr_t num)
817{
818 int i;
819
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;
830 }
831 }
832 }
833}
834
835/*======================================================================
836
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.
840
841======================================================================*/
842
843int pccard_access_configuration_register(struct pcmcia_socket *s,
844 unsigned int function,
845 conf_reg_t *reg)
846{
847 config_t *c;
848 int addr;
849 u_char val;
850
851 if (!s || !s->config)
852 return CS_NO_CARD;
853
854 c = &s->config[function];
855
856 if (c == NULL)
857 return CS_NO_CARD;
858
859 if (!(c->state & CONFIG_LOCKED))
860 return CS_CONFIGURATION_LOCKED;
861
862 addr = (c->ConfigBase + reg->Offset) >> 1;
863
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;
876 }
877 return CS_SUCCESS;
878} /* access_configuration_register */
879EXPORT_SYMBOL(pccard_access_configuration_register);
880
881
882/*====================================================================*/
883
884int pccard_get_configuration_info(struct pcmcia_socket *s,
885 unsigned int function,
886 config_info_t *config)
887{
888 config_t *c;
889
890 if (!(s->state & SOCKET_PRESENT))
891 return CS_NO_CARD;
892
893 config->Function = function;
894
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;
909 }
910 return CS_SUCCESS;
911 }
912#endif
913
914 c = (s->config != NULL) ? &s->config[function] : NULL;
915
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;
921 }
922
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;
936
937 return CS_SUCCESS;
938} /* get_configuration_info */
939EXPORT_SYMBOL(pccard_get_configuration_info);
940
941/*======================================================================
942
943 Return information about this version of Card Services.
944
945======================================================================*/
946
947int pcmcia_get_card_services_info(servinfo_t *info)
948{
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 */
963
964
965/*====================================================================*/
966
967int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle, int idx, win_req_t *req)
968{
969 window_t *win;
970 int w;
971
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 */
994EXPORT_SYMBOL(pcmcia_get_window);
995
996/*=====================================================================
997
998 Return the PCI device associated with a card..
999
1000======================================================================*/
1001
1002#ifdef CONFIG_CARDBUS
1003
1004struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
1005{
1006 if (!s || !(s->state & SOCKET_CARDBUS))
1007 return NULL;
1008
1009 return s->cb_dev->subordinate;
1010}
1011
1012EXPORT_SYMBOL(pcmcia_lookup_bus);
1013
1014#endif
1015
1016/*======================================================================
1017
1018 Get the current socket state bits. We don't support the latched
1019 SocketState yet: I haven't seen any point for it.
1020
1021======================================================================*/
1022
1023int pccard_get_status(struct pcmcia_socket *s, unsigned int function, cs_status_t *status)
1024{
1025 config_t *c;
1026 int val;
1027
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;
1038
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;
1056 }
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;
1061 }
1062 return CS_SUCCESS;
1063 }
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 */
1074EXPORT_SYMBOL(pccard_get_status);
1075
1076/*======================================================================
1077
1078 Change the card address of an already open memory window.
1079
1080======================================================================*/
1081
1082int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
1083{
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 */
1090
1091int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
1092{
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 */
1104
1105/*======================================================================
1106
1107 Modify a locked socket configuration
1108
1109======================================================================*/
1110
1111int pcmcia_modify_configuration(client_handle_t handle,
1112 modconf_t *mod)
1113{
1114 struct pcmcia_socket *s;
1115 config_t *c;
1116
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;
1124
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;
1132 }
1133 s->ops->set_socket(s, &s->socket);
1134 }
1135
1136 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
1137 return CS_BAD_VCC;
1138
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;
1150
1151 return CS_SUCCESS;
1152} /* modify_configuration */
1153
1154/* register pcmcia_callback */
1155int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
1156{
1157 int ret = 0;
1158
1159 /* s->skt_sem also protects s->callback */
1160 down(&s->skt_sem);
1161
1162 if (c) {
1163 /* registration */
1164 if (s->callback) {
1165 ret = -EBUSY;
1166 goto err;
1167 }
1168
1169 s->callback = c;
1170
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);
1177
1178 return ret;
1179}
1180EXPORT_SYMBOL(pccard_register_pcmcia);
1181
1182/*====================================================================*/
1183
1184int pcmcia_release_configuration(client_handle_t handle)
1185{
1186 pccard_io_map io = { 0, 0, 0, 0, 1 };
1187 struct pcmcia_socket *s;
1188 int i;
1189
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);
1195
1196#ifdef CONFIG_CARDBUS
1197 if (handle->state & CLIENT_CARDBUS)
1198 return CS_SUCCESS;
1199#endif
1200
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);
1208 }
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);
1218 }
1219 c->state &= ~CONFIG_LOCKED;
1220 }
1221
1222 return CS_SUCCESS;
1223} /* release_configuration */
1224
1225/*======================================================================
1226
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.
1232
1233======================================================================*/
1234
1235int pcmcia_release_io(client_handle_t handle, io_req_t *req)
1236{
1237 struct pcmcia_socket *s;
1238
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);
1243
1244#ifdef CONFIG_CARDBUS
1245 if (handle->state & CLIENT_CARDBUS)
1246 return CS_SUCCESS;
1247#endif
1248
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;
1259 }
1260
1261 release_io_space(s, req->BasePort1, req->NumPorts1);
1262 if (req->NumPorts2)
1263 release_io_space(s, req->BasePort2, req->NumPorts2);
1264
1265 return CS_SUCCESS;
1266} /* release_io */
1267
1268/*====================================================================*/
1269
1270int pcmcia_release_irq(client_handle_t handle, irq_req_t *req)
1271{
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);
1277
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;
1289 }
1290 }
1291
1292 if (req->Attributes & IRQ_HANDLE_PRESENT) {
1293 free_irq(req->AssignedIRQ, req->Instance);
1294 }
1295
1296#ifdef CONFIG_PCMCIA_PROBE
1297 pcmcia_used_irq[req->AssignedIRQ]--;
1298#endif
1299
1300 return CS_SUCCESS;
1301} /* cs_release_irq */
1302
1303/*====================================================================*/
1304
1305int pcmcia_release_window(window_handle_t win)
1306{
1307 struct pcmcia_socket *s;
1308
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;
1314
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);
1319
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;
1325 }
1326 win->handle->state &= ~CLIENT_WIN_REQ(win->index);
1327
1328 win->magic = 0;
1329
1330 return CS_SUCCESS;
1331} /* release_window */
1332
1333/*====================================================================*/
1334
1335int pcmcia_request_configuration(client_handle_t handle,
1336 config_req_t *req)
1337{
1338 int i;
1339 u_int base;
1340 struct pcmcia_socket *s;
1341 config_t *c;
1342 pccard_io_map iomap;
1343
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;
1349
1350#ifdef CONFIG_CARDBUS
1351 if (handle->state & CLIENT_CARDBUS)
1352 return CS_UNSUPPORTED_MODE;
1353#endif
1354
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;
1360
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;
1369
1370 c->Vcc = req->Vcc; c->Vpp1 = c->Vpp2 = req->Vpp1;
1371
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++;
1389
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);
1396 }
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;
1405 }
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);
1411 }
1412 if (req->Present & PRESENT_STATUS) {
1413 c->Status = req->Status;
1414 write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
1415 }
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);
1419 }
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);
1423 }
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);
1429 }
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);
1433 }
1434
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;
1449 }
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++;
1454 }
1455 }
1456
1457 c->state |= CONFIG_LOCKED;
1458 handle->state |= CLIENT_CONFIG_LOCKED;
1459 return CS_SUCCESS;
1460} /* request_configuration */
1461
1462/*======================================================================
1463
1464 Request_io() reserves ranges of port addresses for a socket.
1465 I have not implemented range sharing or alias addressing.
1466
1467======================================================================*/
1468
1469int pcmcia_request_io(client_handle_t handle, io_req_t *req)
1470{
1471 struct pcmcia_socket *s;
1472 config_t *c;
1473
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;
1479
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
1487 }
1488
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;
1501
1502 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
1503 req->NumPorts1, req->IOAddrLines))
1504 return CS_IN_USE;
1505
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;
1511 }
1512 }
1513
1514 c->io = *req;
1515 c->state |= CONFIG_IO_REQ;
1516 handle->state |= CLIENT_IO_REQ;
1517 return CS_SUCCESS;
1518} /* request_io */
1519
1520/*======================================================================
1521
1522 Request_irq() reserves an irq for this client.
1523
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.
1528
1529======================================================================*/
1530
1531#ifdef CONFIG_PCMCIA_PROBE
1532static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
1533{
1534 return IRQ_NONE;
1535}
1536#endif
1537
1538int pcmcia_request_irq(client_handle_t handle, irq_req_t *req)
1539{
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);
1544
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;
1555
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;
1564
1565 for (try = 0; try < 64; try++) {
1566 irq = try % 32;
1567
1568 /* marked as available by driver, and not blocked by userspace? */
1569 if (!((mask >> irq) & 1))
1570 continue;
1571
1572 /* avoid an IRQ which is already used by a PCMCIA card */
1573 if ((try < 32) && pcmcia_used_irq[irq])
1574 continue;
1575
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;
1590 }
1591 }
1592 }
1593#endif
1594 if (ret) {
1595 if (!s->pci_irq)
1596 return ret;
1597 irq = s->pci_irq;
1598 }
1599
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;
1607 }
1608
1609 c->irq.Attributes = req->Attributes;
1610 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
1611 s->irq.Config++;
1612
1613 c->state |= CONFIG_IRQ_REQ;
1614 handle->state |= CLIENT_IRQ_REQ;
1615
1616#ifdef CONFIG_PCMCIA_PROBE
1617 pcmcia_used_irq[irq]++;
1618#endif
1619
1620 return CS_SUCCESS;
1621} /* pcmcia_request_irq */
1622
1623/*======================================================================
1624
1625 Request_window() establishes a mapping between card memory space
1626 and system memory space.
1627
1628======================================================================*/
1629
1630int pcmcia_request_window(client_handle_t *handle, win_req_t *req, window_handle_t *wh)
1631{
1632 struct pcmcia_socket *s;
1633 window_t *win;
1634 u_long align;
1635 int w;
1636
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;
1644
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;
1658
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;
1664
1665 win = &s->win[w];
1666 win->magic = WINDOW_MAGIC;
1667 win->index = w;
1668 win->handle = *handle;
1669 win->sock = s;
1670
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;
1676 }
1677 (*handle)->state |= CLIENT_WIN_REQ(w);
1678
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);
1695
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;
1701 }
1702 *wh = win;
1703
1704 return CS_SUCCESS;
1705} /* request_window */
1706
1707/*======================================================================
1708
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.
1712
1713======================================================================*/
1714
1715int pccard_reset_card(struct pcmcia_socket *skt)
1716{
1717 int ret;
1718
1719 cs_dbg(skt, 1, "resetting socket\n");
1720
1721 down(&skt->skt_sem);
1722 do {
1723 if (!(skt->state & SOCKET_PRESENT)) {
1724 ret = CS_NO_CARD;
1725 break;
1726 }
1727 if (skt->state & SOCKET_SUSPEND) {
1728 ret = CS_IN_USE;
1729 break;
1730 }
1731 if (skt->state & SOCKET_CARDBUS) {
1732 ret = CS_UNSUPPORTED_FUNCTION;
1733 break;
1734 }
1735
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);
1741 }
1742
1743 ret = CS_SUCCESS;
1744 } while (0);
1745 up(&skt->skt_sem);
1746
1747 return ret;
1748} /* reset_card */
1749EXPORT_SYMBOL(pccard_reset_card);
1750
1751/*======================================================================
1752
1753 These shut down or wake up a socket. They are sort of user
1754 initiated versions of the APM suspend and resume actions.
1755
1756======================================================================*/
1757
1758int pcmcia_suspend_card(struct pcmcia_socket *skt)
1759{
1760 int ret;
1761
1762 cs_dbg(skt, 1, "suspending socket\n");
1763
1764 down(&skt->skt_sem);
1765 do {
1766 if (!(skt->state & SOCKET_PRESENT)) {
1767 ret = CS_NO_CARD;
1768 break;
1769 }
1770 if (skt->state & SOCKET_CARDBUS) {
1771 ret = CS_UNSUPPORTED_FUNCTION;
1772 break;
1773 }
1774 ret = socket_suspend(skt);
1775 } while (0);
1776 up(&skt->skt_sem);
1777
1778 return ret;
1779} /* suspend_card */
1780
1781int pcmcia_resume_card(struct pcmcia_socket *skt)
1782{
1783 int ret;
1784
1785 cs_dbg(skt, 1, "waking up socket\n");
1786
1787 down(&skt->skt_sem);
1788 do {
1789 if (!(skt->state & SOCKET_PRESENT)) {
1790 ret = CS_NO_CARD;
1791 break;
1792 }
1793 if (skt->state & SOCKET_CARDBUS) {
1794 ret = CS_UNSUPPORTED_FUNCTION;
1795 break;
1796 }
1797 ret = socket_resume(skt);
1798 } while (0);
1799 up(&skt->skt_sem);
1800
1801 return ret;
1802} /* resume_card */
1803
1804/*======================================================================
1805
1806 These handle user requests to eject or insert a card.
1807
1808======================================================================*/
1809
1810int pcmcia_eject_card(struct pcmcia_socket *skt)
1811{
1812 int ret;
1813
1814 cs_dbg(skt, 1, "user eject request\n");
1815
1816 down(&skt->skt_sem);
1817 do {
1818 if (!(skt->state & SOCKET_PRESENT)) {
1819 ret = -ENODEV;
1820 break;
1821 }
1822
1823 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
1824 if (ret != 0) {
1825 ret = -EINVAL;
1826 break;
1827 }
1828
1829 socket_remove(skt);
1830 ret = 0;
1831 } while (0);
1832 up(&skt->skt_sem);
1833
1834 return ret;
1835} /* eject_card */
1836
1837int pcmcia_insert_card(struct pcmcia_socket *skt)
1838{
1839 int ret;
1840
1841 cs_dbg(skt, 1, "user insert request\n");
1842
1843 down(&skt->skt_sem);
1844 do {
1845 if (skt->state & SOCKET_PRESENT) {
1846 ret = -EBUSY;
1847 break;
1848 }
1849 if (socket_insert(skt) == CS_NO_CARD) {
1850 ret = -ENODEV;
1851 break;
1852 }
1853 ret = 0;
1854 } while (0);
1855 up(&skt->skt_sem);
1856
1857 return ret;
1858} /* insert_card */
1859
1860/*======================================================================
1861
1862 OS-specific module glue goes here
1863
1864======================================================================*/
1865/* in alpha order */
1866EXPORT_SYMBOL(pcmcia_eject_card);
1867EXPORT_SYMBOL(pcmcia_get_card_services_info);
1868EXPORT_SYMBOL(pcmcia_get_mem_page);
1869EXPORT_SYMBOL(pcmcia_insert_card);
1870EXPORT_SYMBOL(pcmcia_map_mem_page);
1871EXPORT_SYMBOL(pcmcia_modify_configuration);
1872EXPORT_SYMBOL(pcmcia_release_configuration);
1873EXPORT_SYMBOL(pcmcia_release_io);
1874EXPORT_SYMBOL(pcmcia_release_irq);
1875EXPORT_SYMBOL(pcmcia_release_window);
1876EXPORT_SYMBOL(pcmcia_replace_cis);
1877EXPORT_SYMBOL(pcmcia_request_configuration);
1878EXPORT_SYMBOL(pcmcia_request_io);
1879EXPORT_SYMBOL(pcmcia_request_irq);
1880EXPORT_SYMBOL(pcmcia_request_window);
1881EXPORT_SYMBOL(pcmcia_resume_card);
1882EXPORT_SYMBOL(pcmcia_suspend_card);
1883
1884EXPORT_SYMBOL(dead_socket);
1885EXPORT_SYMBOL(pcmcia_parse_events);
1886
1887struct class pcmcia_socket_class = {
1888 .name = "pcmcia_socket",
1889 .release = pcmcia_release_socket,
1890};
1891EXPORT_SYMBOL(pcmcia_socket_class);
1892
1893
1894static int __init init_pcmcia_cs(void)
1895{
1896 int ret;
1897 printk(KERN_INFO "%s\n", release);
1898 printk(KERN_INFO " %s\n", options);
1899
1900 ret = class_register(&pcmcia_socket_class);
1901 if (ret)
1902 return (ret);
1903 return class_interface_register(&pccard_sysfs_interface);
1904}
1905
1906static void __exit exit_pcmcia_cs(void)
1907{
1908 printk(KERN_INFO "unloading Kernel Card Services\n");
1909 class_interface_unregister(&pccard_sysfs_interface);
1910 class_unregister(&pcmcia_socket_class);
1911}
1912
1913subsys_initcall(init_pcmcia_cs);
1914module_exit(exit_pcmcia_cs);
1915
1916/*====================================================================*/
1917