File: /usr/src/linux/drivers/pnp/isapnp.c
1 /*
2 * ISA Plug & Play support
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Changelog:
21 * 2000-01-01 Added quirks handling for buggy hardware
22 * Peter Denison <peterd@pnd-pc.demon.co.uk>
23 * 2000-06-14 Added isapnp_probe_devs() and isapnp_activate_dev()
24 * Christoph Hellwig <hch@caldera.de>
25 * 2001-06-03 Added release_region calls to correspond with
26 * request_region calls when a failure occurs. Also
27 * added KERN_* constants to printk() calls.
28 */
29
30 #include <linux/config.h>
31 #include <linux/version.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/ioport.h>
36 #include <linux/string.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
39 #include <asm/io.h>
40 #include <asm/dma.h>
41 #include <asm/irq.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/isapnp.h>
45
46 LIST_HEAD(isapnp_cards);
47 LIST_HEAD(isapnp_devices);
48
49 #if 0
50 #define ISAPNP_REGION_OK
51 #endif
52 #if 0
53 #define ISAPNP_DEBUG
54 #endif
55
56 struct resource *pidxr_res;
57 struct resource *pnpwrp_res;
58 struct resource *isapnp_rdp_res;
59
60 int isapnp_disable; /* Disable ISA PnP */
61 int isapnp_rdp; /* Read Data Port */
62 int isapnp_reset = 1; /* reset all PnP cards (deactivate) */
63 int isapnp_skip_pci_scan; /* skip PCI resource scanning */
64 int isapnp_verbose = 1; /* verbose mode */
65 int isapnp_reserve_irq[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
66 int isapnp_reserve_dma[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
67 int isapnp_reserve_io[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
68 int isapnp_reserve_mem[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
69
70 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
71 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
72 MODULE_PARM(isapnp_disable, "i");
73 MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
74 MODULE_PARM(isapnp_rdp, "i");
75 MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
76 MODULE_PARM(isapnp_reset, "i");
77 MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
78 MODULE_PARM(isapnp_skip_pci_scan, "i");
79 MODULE_PARM_DESC(isapnp_skip_pci_scan, "ISA Plug & Play skip PCI resource scanning");
80 MODULE_PARM(isapnp_verbose, "i");
81 MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
82 MODULE_PARM(isapnp_reserve_irq, "1-16i");
83 MODULE_PARM_DESC(isapnp_reserve_irq, "ISA Plug & Play - reserve IRQ line(s)");
84 MODULE_PARM(isapnp_reserve_dma, "1-8i");
85 MODULE_PARM_DESC(isapnp_reserve_dma, "ISA Plug & Play - reserve DMA channel(s)");
86 MODULE_PARM(isapnp_reserve_io, "1-16i");
87 MODULE_PARM_DESC(isapnp_reserve_io, "ISA Plug & Play - reserve I/O region(s) - port,size");
88 MODULE_PARM(isapnp_reserve_mem, "1-16i");
89 MODULE_PARM_DESC(isapnp_reserve_mem, "ISA Plug & Play - reserve memory region(s) - address,size");
90
91 #define _PIDXR 0x279
92 #define _PNPWRP 0xa79
93
94 /* short tags */
95 #define _STAG_PNPVERNO 0x01
96 #define _STAG_LOGDEVID 0x02
97 #define _STAG_COMPATDEVID 0x03
98 #define _STAG_IRQ 0x04
99 #define _STAG_DMA 0x05
100 #define _STAG_STARTDEP 0x06
101 #define _STAG_ENDDEP 0x07
102 #define _STAG_IOPORT 0x08
103 #define _STAG_FIXEDIO 0x09
104 #define _STAG_VENDOR 0x0e
105 #define _STAG_END 0x0f
106 /* long tags */
107 #define _LTAG_MEMRANGE 0x81
108 #define _LTAG_ANSISTR 0x82
109 #define _LTAG_UNICODESTR 0x83
110 #define _LTAG_VENDOR 0x84
111 #define _LTAG_MEM32RANGE 0x85
112 #define _LTAG_FIXEDMEM32RANGE 0x86
113
114 static unsigned char isapnp_checksum_value;
115 static DECLARE_MUTEX(isapnp_cfg_mutex);
116 static int isapnp_detected;
117
118 /* some prototypes */
119
120 static int isapnp_config_prepare(struct pci_dev *dev);
121 static int isapnp_config_activate(struct pci_dev *dev);
122 static int isapnp_config_deactivate(struct pci_dev *dev);
123
124 static inline void write_data(unsigned char x)
125 {
126 outb(x, _PNPWRP);
127 }
128
129 static inline void write_address(unsigned char x)
130 {
131 outb(x, _PIDXR);
132 udelay(20);
133 }
134
135 static inline unsigned char read_data(void)
136 {
137 unsigned char val = inb(isapnp_rdp);
138 return val;
139 }
140
141 unsigned char isapnp_read_byte(unsigned char idx)
142 {
143 write_address(idx);
144 return read_data();
145 }
146
147 unsigned short isapnp_read_word(unsigned char idx)
148 {
149 unsigned short val;
150
151 val = isapnp_read_byte(idx);
152 val = (val << 8) + isapnp_read_byte(idx+1);
153 return val;
154 }
155
156 unsigned int isapnp_read_dword(unsigned char idx)
157 {
158 unsigned int val;
159
160 val = isapnp_read_byte(idx);
161 val = (val << 8) + isapnp_read_byte(idx+1);
162 val = (val << 8) + isapnp_read_byte(idx+2);
163 val = (val << 8) + isapnp_read_byte(idx+3);
164 return val;
165 }
166
167 void isapnp_write_byte(unsigned char idx, unsigned char val)
168 {
169 write_address(idx);
170 write_data(val);
171 }
172
173 void isapnp_write_word(unsigned char idx, unsigned short val)
174 {
175 isapnp_write_byte(idx, val >> 8);
176 isapnp_write_byte(idx+1, val);
177 }
178
179 void isapnp_write_dword(unsigned char idx, unsigned int val)
180 {
181 isapnp_write_byte(idx, val >> 24);
182 isapnp_write_byte(idx+1, val >> 16);
183 isapnp_write_byte(idx+2, val >> 8);
184 isapnp_write_byte(idx+3, val);
185 }
186
187 void *isapnp_alloc(long size)
188 {
189 void *result;
190
191 result = kmalloc(size, GFP_KERNEL);
192 if (!result)
193 return NULL;
194 memset(result, 0, size);
195 return result;
196 }
197
198 static void isapnp_key(void)
199 {
200 unsigned char code = 0x6a, msb;
201 int i;
202
203 mdelay(1);
204 write_address(0x00);
205 write_address(0x00);
206
207 write_address(code);
208
209 for (i = 1; i < 32; i++) {
210 msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
211 code = (code >> 1) | msb;
212 write_address(code);
213 }
214 }
215
216 /* place all pnp cards in wait-for-key state */
217 static void isapnp_wait(void)
218 {
219 isapnp_write_byte(0x02, 0x02);
220 }
221
222 void isapnp_wake(unsigned char csn)
223 {
224 isapnp_write_byte(0x03, csn);
225 }
226
227 void isapnp_device(unsigned char logdev)
228 {
229 isapnp_write_byte(0x07, logdev);
230 }
231
232 void isapnp_activate(unsigned char logdev)
233 {
234 isapnp_device(logdev);
235 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
236 udelay(250);
237 }
238
239 void isapnp_deactivate(unsigned char logdev)
240 {
241 isapnp_device(logdev);
242 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
243 udelay(500);
244 }
245
246 static void __init isapnp_peek(unsigned char *data, int bytes)
247 {
248 int i, j;
249 unsigned char d=0;
250
251 for (i = 1; i <= bytes; i++) {
252 for (j = 0; j < 20; j++) {
253 d = isapnp_read_byte(0x05);
254 if (d & 1)
255 break;
256 udelay(100);
257 }
258 if (!(d & 1)) {
259 if (data != NULL)
260 *data++ = 0xff;
261 continue;
262 }
263 d = isapnp_read_byte(0x04); /* PRESDI */
264 isapnp_checksum_value += d;
265 if (data != NULL)
266 *data++ = d;
267 }
268 }
269
270 #define RDP_STEP 32 /* minimum is 4 */
271
272 static int isapnp_next_rdp(void)
273 {
274 int rdp = isapnp_rdp;
275 while (rdp <= 0x3ff) {
276 /*
277 * We cannot use NE2000 probe spaces for ISAPnP or we
278 * will lock up machines.
279 */
280 if ((rdp < 0x280 || rdp > 0x380) && !check_region(rdp, 1))
281 {
282 isapnp_rdp = rdp;
283 return 0;
284 }
285 rdp += RDP_STEP;
286 }
287 return -1;
288 }
289
290 /* Set read port address */
291 static inline void isapnp_set_rdp(void)
292 {
293 isapnp_write_byte(0x00, isapnp_rdp >> 2);
294 udelay(100);
295 }
296
297 /*
298 * Perform an isolation. The port selection code now tries to avoid
299 * "dangerous to read" ports.
300 */
301
302 static int __init isapnp_isolate_rdp_select(void)
303 {
304 isapnp_wait();
305 isapnp_key();
306
307 /* Control: reset CSN and conditionally everything else too */
308 isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
309 mdelay(2);
310
311 isapnp_wait();
312 isapnp_key();
313 isapnp_wake(0x00);
314
315 if (isapnp_next_rdp() < 0) {
316 isapnp_wait();
317 return -1;
318 }
319
320 isapnp_set_rdp();
321 udelay(1000);
322 write_address(0x01);
323 udelay(1000);
324 return 0;
325 }
326
327 /*
328 * Isolate (assign uniqued CSN) to all ISA PnP devices.
329 */
330
331 static int __init isapnp_isolate(void)
332 {
333 unsigned char checksum = 0x6a;
334 unsigned char chksum = 0x00;
335 unsigned char bit = 0x00;
336 int data;
337 int csn = 0;
338 int i;
339 int iteration = 1;
340
341 isapnp_rdp = 0x213;
342 if (isapnp_isolate_rdp_select() < 0)
343 return -1;
344
345 while (1) {
346 for (i = 1; i <= 64; i++) {
347 data = read_data() << 8;
348 udelay(250);
349 data = data | read_data();
350 udelay(250);
351 if (data == 0x55aa)
352 bit = 0x01;
353 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
354 bit = 0x00;
355 }
356 for (i = 65; i <= 72; i++) {
357 data = read_data() << 8;
358 udelay(250);
359 data = data | read_data();
360 udelay(250);
361 if (data == 0x55aa)
362 chksum |= (1 << (i - 65));
363 }
364 if (checksum != 0x00 && checksum == chksum) {
365 csn++;
366
367 isapnp_write_byte(0x06, csn);
368 udelay(250);
369 iteration++;
370 isapnp_wake(0x00);
371 isapnp_set_rdp();
372 udelay(1000);
373 write_address(0x01);
374 udelay(1000);
375 goto __next;
376 }
377 if (iteration == 1) {
378 isapnp_rdp += RDP_STEP;
379 if (isapnp_isolate_rdp_select() < 0)
380 return -1;
381 } else if (iteration > 1) {
382 break;
383 }
384 __next:
385 checksum = 0x6a;
386 chksum = 0x00;
387 bit = 0x00;
388 }
389 isapnp_wait();
390 return csn;
391 }
392
393 /*
394 * Read one tag from stream.
395 */
396
397 static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
398 {
399 unsigned char tag, tmp[2];
400
401 isapnp_peek(&tag, 1);
402 if (tag == 0) /* invalid tag */
403 return -1;
404 if (tag & 0x80) { /* large item */
405 *type = tag;
406 isapnp_peek(tmp, 2);
407 *size = (tmp[1] << 8) | tmp[0];
408 } else {
409 *type = (tag >> 3) & 0x0f;
410 *size = tag & 0x07;
411 }
412 #if 0
413 printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type, *size);
414 #endif
415 if (type == 0) /* wrong type */
416 return -1;
417 if (*type == 0xff && *size == 0xffff) /* probably invalid data */
418 return -1;
419 return 0;
420 }
421
422 /*
423 * Skip specified number of bytes from stream.
424 */
425
426 static void __init isapnp_skip_bytes(int count)
427 {
428 isapnp_peek(NULL, count);
429 }
430
431 /*
432 * Parse logical device tag.
433 */
434
435 static struct pci_dev * __init isapnp_parse_device(struct pci_bus *card, int size, int number)
436 {
437 unsigned char tmp[6];
438 struct pci_dev *dev;
439
440 isapnp_peek(tmp, size);
441 dev = isapnp_alloc(sizeof(struct pci_dev));
442 if (!dev)
443 return NULL;
444 dev->dma_mask = 0x00ffffff;
445 dev->devfn = number;
446 dev->vendor = (tmp[1] << 8) | tmp[0];
447 dev->device = (tmp[3] << 8) | tmp[2];
448 dev->regs = tmp[4];
449 dev->bus = card;
450 if (size > 5)
451 dev->regs |= tmp[5] << 8;
452 dev->prepare = isapnp_config_prepare;
453 dev->activate = isapnp_config_activate;
454 dev->deactivate = isapnp_config_deactivate;
455 return dev;
456 }
457
458 /*
459 * Build new resources structure
460 */
461
462 static struct isapnp_resources * __init isapnp_build_resources(struct pci_dev *dev, int dependent)
463 {
464 struct isapnp_resources *res, *ptr, *ptra;
465
466 res = isapnp_alloc(sizeof(struct isapnp_resources));
467 if (!res)
468 return NULL;
469 res->dev = dev;
470 ptr = (struct isapnp_resources *)dev->sysdata;
471 while (ptr && ptr->next)
472 ptr = ptr->next;
473 if (ptr && ptr->dependent && dependent) { /* add to another list */
474 ptra = ptr->alt;
475 while (ptra && ptra->alt)
476 ptra = ptra->alt;
477 if (!ptra)
478 ptr->alt = res;
479 else
480 ptra->alt = res;
481 } else {
482 if (!ptr)
483 dev->sysdata = res;
484 else
485 ptr->next = res;
486 }
487 if (dependent) {
488 res->priority = dependent & 0xff;
489 if (res->priority > ISAPNP_RES_PRIORITY_FUNCTIONAL)
490 res->priority = ISAPNP_RES_PRIORITY_INVALID;
491 res->dependent = 1;
492 } else {
493 res->priority = ISAPNP_RES_PRIORITY_PREFERRED;
494 res->dependent = 0;
495 }
496 return res;
497 }
498
499 /*
500 * Add IRQ resource to resources list.
501 */
502
503 static void __init isapnp_add_irq_resource(struct pci_dev *dev,
504 struct isapnp_resources **res,
505 int dependent, int size)
506 {
507 unsigned char tmp[3];
508 int i;
509 struct isapnp_irq *irq, *ptr;
510
511 isapnp_peek(tmp, size);
512 irq = isapnp_alloc(sizeof(struct isapnp_irq));
513 if (!irq)
514 return;
515 if (*res == NULL) {
516 *res = isapnp_build_resources(dev, dependent);
517 if (*res == NULL) {
518 kfree(irq);
519 return;
520 }
521 }
522 irq->map = (tmp[1] << 8) | tmp[0];
523 if (size > 2)
524 irq->flags = tmp[2];
525 else
526 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
527 irq->res = *res;
528 ptr = (*res)->irq;
529 while (ptr && ptr->next)
530 ptr = ptr->next;
531 if (ptr)
532 ptr->next = irq;
533 else
534 (*res)->irq = irq;
535 #ifdef CONFIG_PCI
536 for (i=0; i<16; i++)
537 if (irq->map & (1<<i))
538 pcibios_penalize_isa_irq(i);
539 #endif
540 }
541
542 /*
543 * Add DMA resource to resources list.
544 */
545
546 static void __init isapnp_add_dma_resource(struct pci_dev *dev,
547 struct isapnp_resources **res,
548 int dependent, int size)
549 {
550 unsigned char tmp[2];
551 struct isapnp_dma *dma, *ptr;
552
553 isapnp_peek(tmp, size);
554 dma = isapnp_alloc(sizeof(struct isapnp_dma));
555 if (!dma)
556 return;
557 if (*res == NULL) {
558 *res = isapnp_build_resources(dev, dependent);
559 if (*res == NULL) {
560 kfree(dma);
561 return;
562 }
563 }
564 dma->map = tmp[0];
565 dma->flags = tmp[1];
566 dma->res = *res;
567 ptr = (*res)->dma;
568 while (ptr && ptr->next)
569 ptr = ptr->next;
570 if (ptr)
571 ptr->next = dma;
572 else
573 (*res)->dma = dma;
574 }
575
576 /*
577 * Add port resource to resources list.
578 */
579
580 static void __init isapnp_add_port_resource(struct pci_dev *dev,
581 struct isapnp_resources **res,
582 int dependent, int size)
583 {
584 unsigned char tmp[7];
585 struct isapnp_port *port, *ptr;
586
587 isapnp_peek(tmp, size);
588 port = isapnp_alloc(sizeof(struct isapnp_port));
589 if (!port)
590 return;
591 if (*res == NULL) {
592 *res = isapnp_build_resources(dev, dependent);
593 if (*res == NULL) {
594 kfree(port);
595 return;
596 }
597 }
598 port->min = (tmp[2] << 8) | tmp[1];
599 port->max = (tmp[4] << 8) | tmp[3];
600 port->align = tmp[5];
601 port->size = tmp[6];
602 port->flags = tmp[0] ? ISAPNP_PORT_FLAG_16BITADDR : 0;
603 port->res = *res;
604 ptr = (*res)->port;
605 while (ptr && ptr->next)
606 ptr = ptr->next;
607 if (ptr)
608 ptr->next = port;
609 else
610 (*res)->port = port;
611 }
612
613 /*
614 * Add fixed port resource to resources list.
615 */
616
617 static void __init isapnp_add_fixed_port_resource(struct pci_dev *dev,
618 struct isapnp_resources **res,
619 int dependent, int size)
620 {
621 unsigned char tmp[3];
622 struct isapnp_port *port, *ptr;
623
624 isapnp_peek(tmp, size);
625 port = isapnp_alloc(sizeof(struct isapnp_port));
626 if (!port)
627 return;
628 if (*res == NULL) {
629 *res = isapnp_build_resources(dev, dependent);
630 if (*res == NULL) {
631 kfree(port);
632 return;
633 }
634 }
635 port->min = port->max = (tmp[1] << 8) | tmp[0];
636 port->size = tmp[2];
637 port->align = 0;
638 port->flags = ISAPNP_PORT_FLAG_FIXED;
639 port->res = *res;
640 ptr = (*res)->port;
641 while (ptr && ptr->next)
642 ptr = ptr->next;
643 if (ptr)
644 ptr->next = port;
645 else
646 (*res)->port = port;
647 }
648
649 /*
650 * Add memory resource to resources list.
651 */
652
653 static void __init isapnp_add_mem_resource(struct pci_dev *dev,
654 struct isapnp_resources **res,
655 int dependent, int size)
656 {
657 unsigned char tmp[9];
658 struct isapnp_mem *mem, *ptr;
659
660 isapnp_peek(tmp, size);
661 mem = isapnp_alloc(sizeof(struct isapnp_mem));
662 if (!mem)
663 return;
664 if (*res == NULL) {
665 *res = isapnp_build_resources(dev, dependent);
666 if (*res == NULL) {
667 kfree(mem);
668 return;
669 }
670 }
671 mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
672 mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
673 mem->align = (tmp[6] << 8) | tmp[5];
674 mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
675 mem->flags = tmp[0];
676 mem->res = *res;
677 ptr = (*res)->mem;
678 while (ptr && ptr->next)
679 ptr = ptr->next;
680 if (ptr)
681 ptr->next = mem;
682 else
683 (*res)->mem = mem;
684 }
685
686 /*
687 * Add 32-bit memory resource to resources list.
688 */
689
690 static void __init isapnp_add_mem32_resource(struct pci_dev *dev,
691 struct isapnp_resources **res,
692 int dependent, int size)
693 {
694 unsigned char tmp[17];
695 struct isapnp_mem32 *mem32, *ptr;
696
697 isapnp_peek(tmp, size);
698 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
699 if (!mem32)
700 return;
701 if (*res == NULL) {
702 *res = isapnp_build_resources(dev, dependent);
703 if (*res == NULL) {
704 kfree(mem32);
705 return;
706 }
707 }
708 memcpy(mem32->data, tmp, 17);
709 mem32->res = *res;
710 ptr = (*res)->mem32;
711 while (ptr && ptr->next)
712 ptr = ptr->next;
713 if (ptr)
714 ptr->next = mem32;
715 else
716 (*res)->mem32 = mem32;
717 }
718
719 /*
720 * Add 32-bit fixed memory resource to resources list.
721 */
722
723 static void __init isapnp_add_fixed_mem32_resource(struct pci_dev *dev,
724 struct isapnp_resources **res,
725 int dependent, int size)
726 {
727 unsigned char tmp[17];
728 struct isapnp_mem32 *mem32, *ptr;
729
730 isapnp_peek(tmp, size);
731 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
732 if (!mem32)
733 return;
734 if (*res == NULL) {
735 *res = isapnp_build_resources(dev, dependent);
736 if (*res == NULL) {
737 kfree(mem32);
738 return;
739 }
740 }
741 memcpy(mem32->data, tmp, 17);
742 mem32->res = *res;
743 ptr = (*res)->mem32;
744 while (ptr && ptr->next)
745 ptr = ptr->next;
746 if (ptr)
747 ptr->next = mem32;
748 else
749 (*res)->mem32 = mem32;
750 }
751
752 /*
753 * Parse card name for ISA PnP device.
754 */
755
756 static void __init
757 isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
758 {
759 if (name[0] == '\0') {
760 unsigned short size1 = *size >= name_max ? (name_max - 1) : *size;
761 isapnp_peek(name, size1);
762 name[size1] = '\0';
763 *size -= size1;
764
765 /* clean whitespace from end of string */
766 while (size1 > 0 && name[--size1] == ' ')
767 name[size1] = '\0';
768 }
769 }
770
771 /*
772 * Parse resource map for logical device.
773 */
774
775 static int __init isapnp_create_device(struct pci_bus *card,
776 unsigned short size)
777 {
778 int number = 0, skip = 0, dependent = 0, compat = 0;
779 unsigned char type, tmp[17];
780 struct pci_dev *dev;
781 struct isapnp_resources *res = NULL;
782
783 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
784 return 1;
785 list_add(&dev->bus_list, &card->devices);
786 list_add_tail(&dev->global_list, &isapnp_devices);
787 while (1) {
788 if (isapnp_read_tag(&type, &size)<0)
789 return 1;
790 if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
791 goto __skip;
792 switch (type) {
793 case _STAG_LOGDEVID:
794 if (size >= 5 && size <= 6) {
795 isapnp_config_prepare(dev);
796 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
797 return 1;
798 list_add_tail(&dev->bus_list, &card->devices);
799 list_add_tail(&dev->global_list, &isapnp_devices);
800 size = 0;
801 skip = 0;
802 } else {
803 skip = 1;
804 }
805 res = NULL;
806 dependent = 0;
807 compat = 0;
808 break;
809 case _STAG_COMPATDEVID:
810 if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
811 isapnp_peek(tmp, 4);
812 dev->vendor_compatible[compat] = (tmp[1] << 8) | tmp[0];
813 dev->device_compatible[compat] = (tmp[3] << 8) | tmp[2];
814 compat++;
815 size = 0;
816 }
817 break;
818 case _STAG_IRQ:
819 if (size < 2 || size > 3)
820 goto __skip;
821 isapnp_add_irq_resource(dev, &res, dependent, size);
822 size = 0;
823 break;
824 case _STAG_DMA:
825 if (size != 2)
826 goto __skip;
827 isapnp_add_dma_resource(dev, &res, dependent, size);
828 size = 0;
829 break;
830 case _STAG_STARTDEP:
831 if (size > 1)
832 goto __skip;
833 res = NULL;
834 dependent = 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE;
835 if (size > 0) {
836 isapnp_peek(tmp, size);
837 dependent = 0x100 | tmp[0];
838 size = 0;
839 }
840 break;
841 case _STAG_ENDDEP:
842 if (size != 0)
843 goto __skip;
844 res = NULL;
845 dependent = 0;
846 break;
847 case _STAG_IOPORT:
848 if (size != 7)
849 goto __skip;
850 isapnp_add_port_resource(dev, &res, dependent, size);
851 size = 0;
852 break;
853 case _STAG_FIXEDIO:
854 if (size != 3)
855 goto __skip;
856 isapnp_add_fixed_port_resource(dev, &res, dependent, size);
857 size = 0;
858 break;
859 case _STAG_VENDOR:
860 break;
861 case _LTAG_MEMRANGE:
862 if (size != 9)
863 goto __skip;
864 isapnp_add_mem_resource(dev, &res, dependent, size);
865 size = 0;
866 break;
867 case _LTAG_ANSISTR:
868 isapnp_parse_name(dev->name, sizeof(dev->name), &size);
869 break;
870 case _LTAG_UNICODESTR:
871 /* silently ignore */
872 /* who use unicode for hardware identification? */
873 break;
874 case _LTAG_VENDOR:
875 break;
876 case _LTAG_MEM32RANGE:
877 if (size != 17)
878 goto __skip;
879 isapnp_add_mem32_resource(dev, &res, dependent, size);
880 size = 0;
881 break;
882 case _LTAG_FIXEDMEM32RANGE:
883 if (size != 17)
884 goto __skip;
885 isapnp_add_fixed_mem32_resource(dev, &res, dependent, size);
886 size = 0;
887 break;
888 case _STAG_END:
889 if (size > 0)
890 isapnp_skip_bytes(size);
891 return 1;
892 default:
893 printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->devfn, card->number);
894 }
895 __skip:
896 if (size > 0)
897 isapnp_skip_bytes(size);
898 }
899 isapnp_config_prepare(dev);
900 return 0;
901 }
902
903 /*
904 * Parse resource map for ISA PnP card.
905 */
906
907 static void __init isapnp_parse_resource_map(struct pci_bus *card)
908 {
909 unsigned char type, tmp[17];
910 unsigned short size;
911
912 while (1) {
913 if (isapnp_read_tag(&type, &size)<0)
914 return;
915 switch (type) {
916 case _STAG_PNPVERNO:
917 if (size != 2)
918 goto __skip;
919 isapnp_peek(tmp, 2);
920 card->pnpver = tmp[0];
921 card->productver = tmp[1];
922 size = 0;
923 break;
924 case _STAG_LOGDEVID:
925 if (size >= 5 && size <= 6) {
926 if (isapnp_create_device(card, size)==1)
927 return;
928 size = 0;
929 }
930 break;
931 case _STAG_VENDOR:
932 break;
933 case _LTAG_ANSISTR:
934 isapnp_parse_name(card->name, sizeof(card->name), &size);
935 break;
936 case _LTAG_UNICODESTR:
937 /* silently ignore */
938 /* who use unicode for hardware identification? */
939 break;
940 case _LTAG_VENDOR:
941 break;
942 case _STAG_END:
943 if (size > 0)
944 isapnp_skip_bytes(size);
945 return;
946 default:
947 printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
948 }
949 __skip:
950 if (size > 0)
951 isapnp_skip_bytes(size);
952 }
953 }
954
955 /*
956 * Compute ISA PnP checksum for first eight bytes.
957 */
958
959 static unsigned char __init isapnp_checksum(unsigned char *data)
960 {
961 int i, j;
962 unsigned char checksum = 0x6a, bit, b;
963
964 for (i = 0; i < 8; i++) {
965 b = data[i];
966 for (j = 0; j < 8; j++) {
967 bit = 0;
968 if (b & (1 << j))
969 bit = 1;
970 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
971 }
972 }
973 return checksum;
974 }
975
976 /*
977 * Build device list for all present ISA PnP devices.
978 */
979
980 static int __init isapnp_build_device_list(void)
981 {
982 int csn;
983 unsigned char header[9], checksum;
984 struct pci_bus *card;
985 struct pci_dev *dev;
986
987 isapnp_wait();
988 isapnp_key();
989 for (csn = 1; csn <= 10; csn++) {
990 isapnp_wake(csn);
991 isapnp_peek(header, 9);
992 checksum = isapnp_checksum(header);
993 #if 0
994 printk(KERN_DEBUG "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
995 header[0], header[1], header[2], header[3],
996 header[4], header[5], header[6], header[7], header[8]);
997 printk(KERN_DEBUG "checksum = 0x%x\n", checksum);
998 #endif
999 /* Don't be strict on the checksum, here !
1000 e.g. 'SCM SwapBox Plug and Play' has header[8]==0 (should be: b7)*/
1001 if (header[8] == 0)
1002 ;
1003 else if (checksum == 0x00 || checksum != header[8]) /* not valid CSN */
1004 continue;
1005 if ((card = isapnp_alloc(sizeof(struct pci_bus))) == NULL)
1006 continue;
1007
1008 card->number = csn;
1009 card->vendor = (header[1] << 8) | header[0];
1010 card->device = (header[3] << 8) | header[2];
1011 card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
1012 isapnp_checksum_value = 0x00;
1013 INIT_LIST_HEAD(&card->children);
1014 INIT_LIST_HEAD(&card->devices);
1015 isapnp_parse_resource_map(card);
1016 if (isapnp_checksum_value != 0x00)
1017 printk(KERN_ERR "isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
1018 card->checksum = isapnp_checksum_value;
1019
1020 list_add_tail(&card->node, &isapnp_cards);
1021 }
1022 isapnp_for_each_dev(dev) {
1023 isapnp_fixup_device(dev);
1024 }
1025 return 0;
1026 }
1027
1028 /*
1029 * Basic configuration routines.
1030 */
1031
1032 int isapnp_present(void)
1033 {
1034 return !list_empty(&isapnp_devices);
1035 }
1036
1037 int isapnp_cfg_begin(int csn, int logdev)
1038 {
1039 if (csn < 1 || csn > 10 || logdev > 10)
1040 return -EINVAL;
1041 MOD_INC_USE_COUNT;
1042 down(&isapnp_cfg_mutex);
1043 isapnp_wait();
1044 isapnp_key();
1045 isapnp_wake(csn);
1046 #if 1 /* to avoid malfunction when the isapnptools package is used */
1047 isapnp_set_rdp();
1048 udelay(1000); /* delay 1000us */
1049 write_address(0x01);
1050 udelay(1000); /* delay 1000us */
1051 #endif
1052 if (logdev >= 0)
1053 isapnp_device(logdev);
1054 return 0;
1055 }
1056
1057 int isapnp_cfg_end(void)
1058 {
1059 isapnp_wait();
1060 up(&isapnp_cfg_mutex);
1061 MOD_DEC_USE_COUNT;
1062 return 0;
1063 }
1064
1065 /*
1066 * Resource manager.
1067 */
1068
1069 static struct isapnp_port *isapnp_find_port(struct pci_dev *dev, int index)
1070 {
1071 struct isapnp_resources *res;
1072 struct isapnp_port *port;
1073
1074 if (!dev || index < 0 || index > 7)
1075 return NULL;
1076 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1077 for (port = res->port; port; port = port->next) {
1078 if (!index)
1079 return port;
1080 index--;
1081 }
1082 }
1083 return NULL;
1084 }
1085
1086 struct isapnp_irq *isapnp_find_irq(struct pci_dev *dev, int index)
1087 {
1088 struct isapnp_resources *res, *resa;
1089 struct isapnp_irq *irq;
1090 int index1, index2, index3;
1091
1092 if (!dev || index < 0 || index > 7)
1093 return NULL;
1094 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1095 index3 = 0;
1096 for (resa = res; resa; resa = resa->alt) {
1097 index1 = index;
1098 index2 = 0;
1099 for (irq = resa->irq; irq; irq = irq->next) {
1100 if (!index1)
1101 return irq;
1102 index1--;
1103 index2++;
1104 }
1105 if (index3 < index2)
1106 index3 = index2;
1107 }
1108 index -= index3;
1109 }
1110 return NULL;
1111 }
1112
1113 struct isapnp_dma *isapnp_find_dma(struct pci_dev *dev, int index)
1114 {
1115 struct isapnp_resources *res;
1116 struct isapnp_dma *dma;
1117
1118 if (!dev || index < 0 || index > 7)
1119 return NULL;
1120 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1121 for (dma = res->dma; dma; dma = dma->next) {
1122 if (!index)
1123 return dma;
1124 index--;
1125 }
1126 }
1127 return NULL;
1128 }
1129
1130 struct isapnp_mem *isapnp_find_mem(struct pci_dev *dev, int index)
1131 {
1132 struct isapnp_resources *res;
1133 struct isapnp_mem *mem;
1134
1135 if (!dev || index < 0 || index > 7)
1136 return NULL;
1137 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1138 for (mem = res->mem; mem; mem = mem->next) {
1139 if (!index)
1140 return mem;
1141 index--;
1142 }
1143 }
1144 return NULL;
1145 }
1146
1147 struct isapnp_mem32 *isapnp_find_mem32(struct pci_dev *dev, int index)
1148 {
1149 struct isapnp_resources *res;
1150 struct isapnp_mem32 *mem32;
1151
1152 if (!dev || index < 0 || index > 7)
1153 return NULL;
1154 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1155 for (mem32 = res->mem32; mem32; mem32 = mem32->next) {
1156 if (!index)
1157 return mem32;
1158 index--;
1159 }
1160 }
1161 return NULL;
1162 }
1163
1164 /*
1165 * Device manager.
1166 */
1167
1168 struct pci_bus *isapnp_find_card(unsigned short vendor,
1169 unsigned short device,
1170 struct pci_bus *from)
1171 {
1172 struct list_head *list;
1173
1174 list = isapnp_cards.next;
1175 if (from)
1176 list = from->node.next;
1177
1178 while (list != &isapnp_cards) {
1179 struct pci_bus *card = pci_bus_b(list);
1180 if (card->vendor == vendor && card->device == device)
1181 return card;
1182 list = list->next;
1183 }
1184 return NULL;
1185 }
1186
1187 struct pci_dev *isapnp_find_dev(struct pci_bus *card,
1188 unsigned short vendor,
1189 unsigned short function,
1190 struct pci_dev *from)
1191 {
1192 if (card == NULL) { /* look for a logical device from all cards */
1193 struct list_head *list;
1194
1195 list = isapnp_devices.next;
1196 if (from)
1197 list = from->global_list.next;
1198
1199 while (list != &isapnp_devices) {
1200 int idx;
1201 struct pci_dev *dev = pci_dev_g(list);
1202
1203 if (dev->vendor == vendor && dev->device == function)
1204 return dev;
1205 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1206 if (dev->vendor_compatible[idx] == vendor &&
1207 dev->device_compatible[idx] == function)
1208 return dev;
1209 list = list->next;
1210 }
1211 } else {
1212 struct list_head *list;
1213
1214 list = card->devices.next;
1215 if (from) {
1216 list = from->bus_list.next;
1217 if (from->bus != card) /* something is wrong */
1218 return NULL;
1219 }
1220 while (list != &card->devices) {
1221 int idx;
1222 struct pci_dev *dev = pci_dev_b(list);
1223
1224 if (dev->vendor == vendor && dev->device == function)
1225 return dev;
1226 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1227 if (dev->vendor_compatible[idx] == vendor &&
1228 dev->device_compatible[idx] == function)
1229 return dev;
1230 list = list->next;
1231 }
1232 }
1233 return NULL;
1234 }
1235
1236 static const struct isapnp_card_id *
1237 isapnp_match_card(const struct isapnp_card_id *ids, struct pci_bus *card)
1238 {
1239 int idx;
1240
1241 while (ids->card_vendor || ids->card_device) {
1242 if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == card->vendor) &&
1243 (ids->card_device == ISAPNP_ANY_ID || ids->card_device == card->device)) {
1244 for (idx = 0; idx < ISAPNP_CARD_DEVS; idx++) {
1245 if (ids->devs[idx].vendor == 0 &&
1246 ids->devs[idx].function == 0)
1247 return ids;
1248 if (isapnp_find_dev(card,
1249 ids->devs[idx].vendor,
1250 ids->devs[idx].function,
1251 NULL) == NULL)
1252 goto __next;
1253 }
1254 return ids;
1255 }
1256 __next:
1257 ids++;
1258 }
1259 return NULL;
1260 }
1261
1262 int isapnp_probe_cards(const struct isapnp_card_id *ids,
1263 int (*probe)(struct pci_bus *_card,
1264 const struct isapnp_card_id *_id))
1265 {
1266 struct pci_bus *card;
1267 const struct isapnp_card_id *id;
1268 int count = 0;
1269
1270 if (ids == NULL || probe == NULL)
1271 return -EINVAL;
1272 isapnp_for_each_card(card) {
1273 id = isapnp_match_card(ids, card);
1274 if (id != NULL && probe(card, id) >= 0)
1275 count++;
1276 }
1277 return count;
1278 }
1279
1280 static const struct isapnp_device_id *
1281 isapnp_match_dev(const struct isapnp_device_id *ids, struct pci_dev *dev)
1282 {
1283 while (ids->card_vendor || ids->card_device) {
1284 if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == dev->bus->vendor) &&
1285 (ids->card_device == ISAPNP_ANY_ID || ids->card_device == dev->bus->device) &&
1286 (ids->vendor == ISAPNP_ANY_ID || ids->vendor == dev->vendor) &&
1287 (ids->function == ISAPNP_ANY_ID || ids->function == dev->device))
1288 return ids;
1289 ids++;
1290 }
1291 return NULL;
1292 }
1293
1294 int isapnp_probe_devs(const struct isapnp_device_id *ids,
1295 int (*probe)(struct pci_dev *dev,
1296 const struct isapnp_device_id *id))
1297 {
1298
1299 struct pci_dev *dev;
1300 const struct isapnp_device_id *id;
1301 int count = 0;
1302
1303 if (ids == NULL || probe == NULL)
1304 return -EINVAL;
1305 isapnp_for_each_dev(dev) {
1306 id = isapnp_match_dev(ids, dev);
1307 if (id != NULL && probe(dev, id) >= 0)
1308 count++;
1309 }
1310 return count;
1311 }
1312
1313 int isapnp_activate_dev(struct pci_dev *dev, const char *name)
1314 {
1315 int err;
1316
1317 /* Device already active? Let's use it and inform the caller */
1318 if (dev->active)
1319 return -EBUSY;
1320
1321 if ((err = dev->activate(dev)) < 0) {
1322 printk(KERN_ERR "isapnp: config of %s failed (out of resources?)[%d]\n", name, err);
1323 dev->deactivate(dev);
1324 return err;
1325 }
1326
1327 return 0;
1328 }
1329
1330 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma *dma)
1331 {
1332 return dma->flags | IORESOURCE_DMA | IORESOURCE_AUTO;
1333 }
1334
1335 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem *mem)
1336 {
1337 unsigned int result;
1338
1339 result = mem->flags | IORESOURCE_MEM | IORESOURCE_AUTO;
1340 if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
1341 result |= IORESOURCE_READONLY;
1342 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
1343 result |= IORESOURCE_CACHEABLE;
1344 if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
1345 result |= IORESOURCE_RANGELENGTH;
1346 if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
1347 result |= IORESOURCE_SHADOWABLE;
1348 return result;
1349 }
1350
1351 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq *irq)
1352 {
1353 return irq->flags | IORESOURCE_IRQ | IORESOURCE_AUTO;
1354 }
1355
1356 static unsigned int isapnp_port_resource_flags(struct isapnp_port *port)
1357 {
1358 return port->flags | IORESOURCE_IO | IORESOURCE_AUTO;
1359 }
1360
1361 static int isapnp_config_prepare(struct pci_dev *dev)
1362 {
1363 struct isapnp_resources *res, *resa;
1364 struct isapnp_port *port;
1365 struct isapnp_irq *irq;
1366 struct isapnp_dma *dma;
1367 struct isapnp_mem *mem;
1368 int port_count, port_count1;
1369 int irq_count, irq_count1;
1370 int dma_count, dma_count1;
1371 int mem_count, mem_count1;
1372 int idx;
1373
1374 if (dev == NULL)
1375 return -EINVAL;
1376 if (dev->active || dev->ro)
1377 return -EBUSY;
1378 for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
1379 dev->irq_resource[idx].name = NULL;
1380 dev->irq_resource[idx].start = 0;
1381 dev->irq_resource[idx].end = 0;
1382 dev->irq_resource[idx].flags = 0;
1383 }
1384 for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
1385 dev->dma_resource[idx].name = NULL;
1386 dev->dma_resource[idx].start = 0;
1387 dev->dma_resource[idx].end = 0;
1388 dev->dma_resource[idx].flags = 0;
1389 }
1390 for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
1391 dev->resource[idx].name = NULL;
1392 dev->resource[idx].start = 0;
1393 dev->resource[idx].end = 0;
1394 dev->resource[idx].flags = 0;
1395 }
1396 port_count = irq_count = dma_count = mem_count = 0;
1397 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1398 port_count1 = irq_count1 = dma_count1 = mem_count1 = 0;
1399 for (resa = res; resa; resa = resa->alt) {
1400 for (port = resa->port, idx = 0; port; port = port->next, idx++) {
1401 if (dev->resource[port_count + idx].flags == 0) {
1402 dev->resource[port_count + idx].flags = isapnp_port_resource_flags(port);
1403 dev->resource[port_count + idx].end = port->size;
1404 }
1405 }
1406 if (port_count1 < idx)
1407 port_count1 = idx;
1408 for (irq = resa->irq, idx = 0; irq; irq = irq->next, idx++) {
1409 int count = irq_count + idx;
1410 if (count < DEVICE_COUNT_IRQ) {
1411 if (dev->irq_resource[count].flags == 0) {
1412 dev->irq_resource[count].flags = isapnp_irq_resource_flags(irq);
1413 }
1414 }
1415
1416 }
1417 if (irq_count1 < idx)
1418 irq_count1 = idx;
1419 for (dma = resa->dma, idx = 0; dma; dma = dma->next, idx++)
1420 if (dev->dma_resource[idx].flags == 0) {
1421 dev->dma_resource[idx].flags = isapnp_dma_resource_flags(dma);
1422 }
1423 if (dma_count1 < idx)
1424 dma_count1 = idx;
1425 for (mem = resa->mem, idx = 0; mem; mem = mem->next, idx++)
1426 if (dev->resource[mem_count + idx + 8].flags == 0) {
1427 dev->resource[mem_count + idx + 8].flags = isapnp_mem_resource_flags(mem);
1428 }
1429 if (mem_count1 < idx)
1430 mem_count1 = idx;
1431 }
1432 port_count += port_count1;
1433 irq_count += irq_count1;
1434 dma_count += dma_count1;
1435 mem_count += mem_count1;
1436 }
1437 return 0;
1438 }
1439
1440 struct isapnp_cfgtmp {
1441 struct isapnp_port *port[8];
1442 struct isapnp_irq *irq[2];
1443 struct isapnp_dma *dma[2];
1444 struct isapnp_mem *mem[4];
1445 struct pci_dev *request;
1446 struct pci_dev result;
1447 };
1448
1449 static int isapnp_alternative_switch(struct isapnp_cfgtmp *cfg,
1450 struct isapnp_resources *from,
1451 struct isapnp_resources *to)
1452 {
1453 int tmp, tmp1;
1454 struct isapnp_port *port;
1455 struct isapnp_irq *irq;
1456 struct isapnp_dma *dma;
1457 struct isapnp_mem *mem;
1458
1459 if (!cfg)
1460 return -EINVAL;
1461 /* process port settings */
1462 for (tmp = 0; tmp < 8; tmp++) {
1463 if (!(cfg->request->resource[tmp].flags & IORESOURCE_AUTO))
1464 continue; /* don't touch */
1465 port = cfg->port[tmp];
1466 if (!port) {
1467 cfg->port[tmp] = port = isapnp_find_port(cfg->request, tmp);
1468 if (!port)
1469 return -EINVAL;
1470 }
1471 if (from && port->res == from) {
1472 while (port->res != to) {
1473 if (!port->res->alt)
1474 return -EINVAL;
1475 port = port->res->alt->port;
1476 for (tmp1 = tmp; tmp1 > 0 && port; tmp1--)
1477 port = port->next;
1478 cfg->port[tmp] = port;
1479 if (!port)
1480 return -ENOENT;
1481 cfg->result.resource[tmp].flags = isapnp_port_resource_flags(port);
1482 }
1483 }
1484 }
1485 /* process irq settings */
1486 for (tmp = 0; tmp < 2; tmp++) {
1487 if (!(cfg->request->irq_resource[tmp].flags & IORESOURCE_AUTO))
1488 continue; /* don't touch */
1489 irq = cfg->irq[tmp];
1490 if (!irq) {
1491 cfg->irq[tmp] = irq = isapnp_find_irq(cfg->request, tmp);
1492 if (!irq)
1493 return -EINVAL;
1494 }
1495 if (from && irq->res == from) {
1496 while (irq->res != to) {
1497 if (!irq->res->alt)
1498 return -EINVAL;
1499 irq = irq->res->alt->irq;
1500 for (tmp1 = tmp; tmp1 > 0 && irq; tmp1--)
1501 irq = irq->next;
1502 cfg->irq[tmp] = irq;
1503 if (!irq)
1504 return -ENOENT;
1505 cfg->result.irq_resource[tmp].flags = isapnp_irq_resource_flags(irq);
1506 }
1507 }
1508 }
1509 /* process dma settings */
1510 for (tmp = 0; tmp < 2; tmp++) {
1511 if (!(cfg->request->dma_resource[tmp].flags & IORESOURCE_AUTO))
1512 continue; /* don't touch */
1513 dma = cfg->dma[tmp];
1514 if (!dma) {
1515 cfg->dma[tmp] = dma = isapnp_find_dma(cfg->request, tmp);
1516 if (!dma)
1517 return -EINVAL;
1518 }
1519 if (from && dma->res == from) {
1520 while (dma->res != to) {
1521 if (!dma->res->alt)
1522 return -EINVAL;
1523 dma = dma->res->alt->dma;
1524 for (tmp1 = tmp; tmp1 > 0 && dma; tmp1--)
1525 dma = dma->next;
1526 cfg->dma[tmp] = dma;
1527 if (!dma)
1528 return -ENOENT;
1529 cfg->result.dma_resource[tmp].flags = isapnp_dma_resource_flags(dma);
1530 }
1531 }
1532 }
1533 /* process memory settings */
1534 for (tmp = 0; tmp < 4; tmp++) {
1535 if (!(cfg->request->resource[tmp + 8].flags & IORESOURCE_AUTO))
1536 continue; /* don't touch */
1537 mem = cfg->mem[tmp];
1538 if (!mem) {
1539 cfg->mem[tmp] = mem = isapnp_find_mem(cfg->request, tmp);
1540 if (!mem)
1541 return -EINVAL;
1542 }
1543 if (from && mem->res == from) {
1544 while (mem->res != to) {
1545 if (!mem->res->alt)
1546 return -EINVAL;
1547 mem = mem->res->alt->mem;
1548 for (tmp1 = tmp; tmp1 > 0 && mem; tmp1--)
1549 mem = mem->next;
1550 cfg->mem[tmp] = mem;
1551 if (!mem)
1552 return -ENOENT;
1553 cfg->result.resource[tmp + 8].flags = isapnp_mem_resource_flags(mem);
1554 }
1555 }
1556 }
1557 return 0;
1558 }
1559
1560 static int isapnp_check_port(struct isapnp_cfgtmp *cfg, int port, int size, int idx)
1561 {
1562 int i, tmp, rport, rsize;
1563 struct isapnp_port *xport;
1564 struct pci_dev *dev;
1565
1566 if (check_region(port, size))
1567 return 1;
1568 for (i = 0; i < 8; i++) {
1569 rport = isapnp_reserve_io[i << 1];
1570 rsize = isapnp_reserve_io[(i << 1) + 1];
1571 if (port >= rport && port < rport + rsize)
1572 return 1;
1573 if (port + size > rport && port + size < (rport + rsize) - 1)
1574 return 1;
1575 }
1576
1577 isapnp_for_each_dev(dev) {
1578 if (dev->active) {
1579 for (tmp = 0; tmp < 8; tmp++) {
1580 if (dev->resource[tmp].flags) {
1581 rport = dev->resource[tmp].start;
1582 rsize = (dev->resource[tmp].end - rport) + 1;
1583 if (port >= rport && port < rport + rsize)
1584 return 1;
1585 if (port + size > rport && port + size < (rport + rsize) - 1)
1586 return 1;
1587 }
1588 }
1589 }
1590 }
1591 for (i = 0; i < 8; i++) {
1592 unsigned int flags;
1593 if (i == idx)
1594 continue;
1595 flags = cfg->request->resource[i].flags;
1596 if (!flags)
1597 continue;
1598 tmp = cfg->request->resource[i].start;
1599 if (flags & IORESOURCE_AUTO) { /* auto */
1600 xport = cfg->port[i];
1601 if (!xport)
1602 return 1;
1603 if (cfg->result.resource[i].flags & IORESOURCE_AUTO)
1604 continue;
1605 tmp = cfg->result.resource[i].start;
1606 if (tmp + xport->size >= port && tmp <= port + xport->size)
1607 return 1;
1608 continue;
1609 }
1610 if (port == tmp)
1611 return 1;
1612 xport = isapnp_find_port(cfg->request, i);
1613 if (!xport)
1614 return 1;
1615 if (tmp + xport->size >= port && tmp <= port + xport->size)
1616 return 1;
1617 }
1618 return 0;
1619 }
1620
1621 static int isapnp_valid_port(struct isapnp_cfgtmp *cfg, int idx)
1622 {
1623 int err;
1624 unsigned long *value1, *value2;
1625 struct isapnp_port *port;
1626
1627 if (!cfg || idx < 0 || idx > 7)
1628 return -EINVAL;
1629 if (!(cfg->result.resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1630 return 0;
1631 __again:
1632 port = cfg->port[idx];
1633 if (!port)
1634 return -EINVAL;
1635 value1 = &cfg->result.resource[idx].start;
1636 value2 = &cfg->result.resource[idx].end;
1637 if (cfg->result.resource[idx].flags & IORESOURCE_AUTO) {
1638 cfg->result.resource[idx].flags &= ~IORESOURCE_AUTO;
1639 *value1 = port->min;
1640 *value2 = port->min + port->size - 1;
1641 if (!isapnp_check_port(cfg, *value1, port->size, idx))
1642 return 0;
1643 }
1644 do {
1645 *value1 += port->align;
1646 *value2 = *value1 + port->size - 1;
1647 if (*value1 > port->max || !port->align) {
1648 if (port->res && port->res->alt) {
1649 if ((err = isapnp_alternative_switch(cfg, port->res, port->res->alt))<0)
1650 return err;
1651 goto __again;
1652 }
1653 return -ENOENT;
1654 }
1655 } while (isapnp_check_port(cfg, *value1, port->size, idx));
1656 return 0;
1657 }
1658
1659 static void isapnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
1660 {
1661 }
1662
1663 static int isapnp_check_interrupt(struct isapnp_cfgtmp *cfg, int irq, int idx)
1664 {
1665 int i;
1666 struct pci_dev *dev;
1667
1668 if (irq < 0 || irq > 15)
1669 return 1;
1670 for (i = 0; i < 16; i++) {
1671 if (isapnp_reserve_irq[i] == irq)
1672 return 1;
1673 }
1674 isapnp_for_each_dev(dev) {
1675 if (dev->active) {
1676 if (dev->irq_resource[0].start == irq ||
1677 dev->irq_resource[1].start == irq)
1678 return 1;
1679 }
1680 }
1681 #ifdef CONFIG_PCI
1682 if (!isapnp_skip_pci_scan) {
1683 pci_for_each_dev(dev) {
1684 if (dev->irq == irq)
1685 return 1;
1686 }
1687 }
1688 #endif
1689 if (request_irq(irq, isapnp_test_handler, SA_INTERRUPT, "isapnp", NULL))
1690 return 1;
1691 free_irq(irq, NULL);
1692 for (i = 0; i < DEVICE_COUNT_IRQ; i++) {
1693 if (i == idx)
1694 continue;
1695 if (!cfg->result.irq_resource[i].flags)
1696 continue;
1697 if (cfg->result.irq_resource[i].flags & IORESOURCE_AUTO)
1698 continue;
1699 if (cfg->result.irq_resource[i].start == irq)
1700 return 1;
1701 }
1702 return 0;
1703 }
1704
1705 static int isapnp_valid_irq(struct isapnp_cfgtmp *cfg, int idx)
1706 {
1707 /* IRQ priority: this table is good for i386 */
1708 static unsigned short xtab[16] = {
1709 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1710 };
1711 int err, i;
1712 unsigned long *value1, *value2;
1713 struct isapnp_irq *irq;
1714
1715 if (!cfg || idx < 0 || idx > 1)
1716 return -EINVAL;
1717 if (!(cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO))
1718 return 0;
1719 __again:
1720 irq = cfg->irq[idx];
1721 if (!irq)
1722 return -EINVAL;
1723 value1 = &cfg->result.irq_resource[idx].start;
1724 value2 = &cfg->result.irq_resource[idx].end;
1725 if (cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO) {
1726 for (i = 0; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1727 if (i >= 16)
1728 return -ENOENT;
1729 cfg->result.irq_resource[idx].flags &= ~IORESOURCE_AUTO;
1730 if (!isapnp_check_interrupt(cfg, *value1 = *value2 = xtab[i], idx))
1731 return 0;
1732 }
1733 do {
1734 for (i = 0; i < 16 && xtab[i] != *value1; i++);
1735 for (i++; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1736 if (i >= 16) {
1737 if (irq->res && irq->res->alt) {
1738 if ((err = isapnp_alternative_switch(cfg, irq->res, irq->res->alt))<0)
1739 return err;
1740 goto __again;
1741 }
1742 return -ENOENT;
1743 } else {
1744 *value1 = *value2 = xtab[i];
1745 }
1746 } while (isapnp_check_interrupt(cfg, *value1, idx));
1747 return 0;
1748 }
1749
1750 static int isapnp_check_dma(struct isapnp_cfgtmp *cfg, int dma, int idx)
1751 {
1752 int i;
1753 struct pci_dev *dev;
1754
1755 /* Some machines allow DMA 0, but others don't. In fact on some
1756 boxes DMA 0 is the memory refresh. Play safe */
1757
1758 if (dma < 1 || dma == 4 || dma > 7)
1759 return 1;
1760 for (i = 0; i < 8; i++) {
1761 if (isapnp_reserve_dma[i] == dma)
1762 return 1;
1763 }
1764 isapnp_for_each_dev(dev) {
1765 if (dev->active) {
1766 if (dev->dma_resource[0].start == dma || dev->dma_resource[1].start == dma)
1767 return 1;
1768 }
1769 }
1770 if (request_dma(dma, "isapnp"))
1771 return 1;
1772 free_dma(dma);
1773 for (i = 0; i < 2; i++) {
1774 if (i == idx)
1775 continue;
1776 if (!cfg->result.dma_resource[i].flags ||
1777 (cfg->result.dma_resource[i].flags & IORESOURCE_AUTO))
1778 continue;
1779 if (cfg->result.dma_resource[i].start == dma)
1780 return 1;
1781 }
1782 return 0;
1783 }
1784
1785 static int isapnp_valid_dma(struct isapnp_cfgtmp *cfg, int idx)
1786 {
1787 int err, i;
1788 unsigned long *value1, *value2;
1789 struct isapnp_dma *dma;
1790
1791 if (!cfg || idx < 0 || idx > 1)
1792 return -EINVAL;
1793 if (!(cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1794 return 0;
1795 __again:
1796 dma = cfg->dma[idx];
1797 if (!dma)
1798 return -EINVAL;
1799 value1 = &cfg->result.dma_resource[idx].start;
1800 value2 = &cfg->result.dma_resource[idx].end;
1801 if (cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO) {
1802 for (i = 0; i < 8 && !(dma->map & (1<<i)); i++);
1803 if (i >= 8)
1804 return -ENOENT;
1805 cfg->result.dma_resource[idx].flags &= ~IORESOURCE_AUTO;
1806 if (!isapnp_check_dma(cfg, *value1 = *value2 = i, idx))
1807 return 0;
1808 }
1809 do {
1810 for (i = *value1 + 1; i < 8 && !(dma->map & (1<<i)); i++);
1811 if (i >= 8) {
1812 if (dma->res && dma->res->alt) {
1813 if ((err = isapnp_alternative_switch(cfg, dma->res, dma->res->alt))<0)
1814 return err;
1815 goto __again;
1816 }
1817 return -ENOENT;
1818 } else {
1819 *value1 = *value2 = i;
1820 }
1821 } while (isapnp_check_dma(cfg, *value1, idx));
1822 return 0;
1823 }
1824
1825 static int isapnp_check_mem(struct isapnp_cfgtmp *cfg, unsigned int addr, unsigned int size, int idx)
1826 {
1827 int i, tmp;
1828 unsigned int raddr, rsize;
1829 struct isapnp_mem *xmem;
1830 struct pci_dev *dev;
1831
1832 for (i = 0; i < 8; i++) {
1833 raddr = (unsigned int)isapnp_reserve_mem[i << 1];
1834 rsize = (unsigned int)isapnp_reserve_mem[(i << 1) + 1];
1835 if (addr >= raddr && addr < raddr + rsize)
1836 return 1;
1837 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1838 return 1;
1839 if (__check_region(&iomem_resource, addr, size))
1840 return 1;
1841 }
1842 isapnp_for_each_dev(dev) {
1843 if (dev->active) {
1844 for (tmp = 0; tmp < 4; tmp++) {
1845 if (dev->resource[tmp].flags) {
1846 raddr = dev->resource[tmp + 8].start;
1847 rsize = (dev->resource[tmp + 8].end - raddr) + 1;
1848 if (addr >= raddr && addr < raddr + rsize)
1849 return 1;
1850 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1851 return 1;
1852 }
1853 }
1854 }
1855 }
1856 for (i = 0; i < 4; i++) {
1857 unsigned int flags = cfg->request->resource[i + 8].flags;
1858 if (i == idx)
1859 continue;
1860 if (!flags)
1861 continue;
1862 tmp = cfg->result.resource[i + 8].start;
1863 if (flags & IORESOURCE_AUTO) { /* auto */
1864 xmem = cfg->mem[i];
1865 if (!xmem)
1866 return 1;
1867 if (cfg->result.resource[i + 8].flags & IORESOURCE_AUTO)
1868 continue;
1869 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1870 return 1;
1871 continue;
1872 }
1873 if (addr == tmp)
1874 return 1;
1875 xmem = isapnp_find_mem(cfg->request, i);
1876 if (!xmem)
1877 return 1;
1878 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1879 return 1;
1880 }
1881 return 0;
1882 }
1883
1884 static int isapnp_valid_mem(struct isapnp_cfgtmp *cfg, int idx)
1885 {
1886 int err;
1887 unsigned long *value1, *value2;
1888 struct isapnp_mem *mem;
1889
1890 if (!cfg || idx < 0 || idx > 3)
1891 return -EINVAL;
1892 if (!(cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO)) /* don't touch */
1893 return 0;
1894 __again:
1895 mem = cfg->mem[idx];
1896 if (!mem)
1897 return -EINVAL;
1898 value1 = &cfg->result.resource[idx + 8].start;
1899 value2 = &cfg->result.resource[idx + 8].end;
1900 if (cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO) {
1901 cfg->result.resource[idx + 8].flags &= ~IORESOURCE_AUTO;
1902 *value1 = mem->min;
1903 *value2 = mem->min + mem->size - 1;
1904 if (!isapnp_check_mem(cfg, *value1, mem->size, idx))
1905 return 0;
1906 }
1907 do {
1908 *value1 += mem->align;
1909 *value2 = *value1 + mem->size - 1;
1910 if (*value1 > mem->max || !mem->align) {
1911 if (mem->res && mem->res->alt) {
1912 if ((err = isapnp_alternative_switch(cfg, mem->res, mem->res->alt))<0)
1913 return err;
1914 goto __again;
1915 }
1916 return -ENOENT;
1917 }
1918 } while (isapnp_check_mem(cfg, *value1, mem->size, idx));
1919 return 0;
1920 }
1921
1922 static int isapnp_check_valid(struct isapnp_cfgtmp *cfg)
1923 {
1924 int tmp;
1925
1926 for (tmp = 0; tmp < 8; tmp++)
1927 if (cfg->result.resource[tmp].flags & IORESOURCE_AUTO)
1928 return -EAGAIN;
1929 for (tmp = 0; tmp < 2; tmp++)
1930 if (cfg->result.irq_resource[tmp].flags & IORESOURCE_AUTO)
1931 return -EAGAIN;
1932 for (tmp = 0; tmp < 2; tmp++)
1933 if (cfg->result.dma_resource[tmp].flags & IORESOURCE_AUTO)
1934 return -EAGAIN;
1935 for (tmp = 0; tmp < 4; tmp++)
1936 if (cfg->result.resource[tmp + 8].flags & IORESOURCE_AUTO)
1937 return -EAGAIN;
1938 return 0;
1939 }
1940
1941 static int isapnp_config_activate(struct pci_dev *dev)
1942 {
1943 struct isapnp_cfgtmp cfg;
1944 int tmp, fauto, err;
1945
1946 if (!dev)
1947 return -EINVAL;
1948 if (dev->active)
1949 return -EBUSY;
1950 memset(&cfg, 0, sizeof(cfg));
1951 cfg.request = dev;
1952 memcpy(&cfg.result, dev, sizeof(struct pci_dev));
1953 /* check if all values are set, otherwise try auto-configuration */
1954 for (tmp = fauto = 0; !fauto && tmp < 8; tmp++) {
1955 if (dev->resource[tmp].flags & IORESOURCE_AUTO)
1956 fauto++;
1957 }
1958 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1959 if (dev->irq_resource[tmp].flags & IORESOURCE_AUTO)
1960 fauto++;
1961 }
1962 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1963 if (dev->dma_resource[tmp].flags & IORESOURCE_AUTO)
1964 fauto++;
1965 }
1966 for (tmp = 0; !fauto && tmp < 4; tmp++) {
1967 if (dev->resource[tmp + 8].flags & IORESOURCE_AUTO)
1968 fauto++;
1969 }
1970 if (!fauto)
1971 goto __skip_auto;
1972 /* set variables to initial values */
1973 if ((err = isapnp_alternative_switch(&cfg, NULL, NULL))<0)
1974 return err;
1975 /* find first valid configuration */
1976 fauto = 0;
1977 do {
1978 for (tmp = 0; tmp < 8 && cfg.result.resource[tmp].flags; tmp++)
1979 if ((err = isapnp_valid_port(&cfg, tmp))<0)
1980 return err;
1981 for (tmp = 0; tmp < 2 && cfg.result.irq_resource[tmp].flags; tmp++)
1982 if ((err = isapnp_valid_irq(&cfg, tmp))<0)
1983 return err;
1984 for (tmp = 0; tmp < 2 && cfg.result.dma_resource[tmp].flags; tmp++)
1985 if ((err = isapnp_valid_dma(&cfg, tmp))<0)
1986 return err;
1987 for (tmp = 0; tmp < 4 && cfg.result.resource[tmp + 8].flags; tmp++)
1988 if ((err = isapnp_valid_mem(&cfg, tmp))<0)
1989 return err;
1990 } while (isapnp_check_valid(&cfg)<0 && fauto++ < 20);
1991 if (fauto >= 20)
1992 return -EAGAIN;
1993 __skip_auto:
1994 /* we have valid configuration, try configure hardware */
1995 isapnp_cfg_begin(dev->bus->number, dev->devfn);
1996 dev->active = 1;
1997 dev->irq_resource[0] = cfg.result.irq_resource[0];
1998 dev->irq_resource[1] = cfg.result.irq_resource[1];
1999 dev->dma_resource[0] = cfg.result.dma_resource[0];
2000 dev->dma_resource[1] = cfg.result.dma_resource[1];
2001 for (tmp = 0; tmp < 12; tmp++) {
2002 dev->resource[tmp] = cfg.result.resource[tmp];
2003 }
2004 for (tmp = 0; tmp < 8 && dev->resource[tmp].flags; tmp++)
2005 isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), dev->resource[tmp].start);
2006 for (tmp = 0; tmp < 2 && dev->irq_resource[tmp].flags; tmp++) {
2007 int irq = dev->irq_resource[tmp].start;
2008 if (irq == 2)
2009 irq = 9;
2010 isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
2011 }
2012 for (tmp = 0; tmp < 2 && dev->dma_resource[tmp].flags; tmp++)
2013 isapnp_write_byte(ISAPNP_CFG_DMA+tmp, dev->dma_resource[tmp].start);
2014 for (tmp = 0; tmp < 4 && dev->resource[tmp+8].flags; tmp++)
2015 isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<2), (dev->resource[tmp + 8].start >> 8) & 0xffff);
2016 isapnp_activate(dev->devfn);
2017 isapnp_cfg_end();
2018 return 0;
2019 }
2020
2021 static int isapnp_config_deactivate(struct pci_dev *dev)
2022 {
2023 if (!dev || !dev->active)
2024 return -EINVAL;
2025 isapnp_cfg_begin(dev->bus->number, dev->devfn);
2026 isapnp_deactivate(dev->devfn);
2027 dev->active = 0;
2028 isapnp_cfg_end();
2029 return 0;
2030 }
2031
2032 void isapnp_resource_change(struct resource *resource,
2033 unsigned long start,
2034 unsigned long size)
2035 {
2036 if (resource == NULL)
2037 return;
2038 resource->flags &= ~IORESOURCE_AUTO;
2039 resource->start = start;
2040 resource->end = start + size - 1;
2041 }
2042
2043 /*
2044 * Inititialization.
2045 */
2046
2047 #ifdef MODULE
2048
2049 static void isapnp_free_port(struct isapnp_port *port)
2050 {
2051 struct isapnp_port *next;
2052
2053 while (port) {
2054 next = port->next;
2055 kfree(port);
2056 port = next;
2057 }
2058 }
2059
2060 static void isapnp_free_irq(struct isapnp_irq *irq)
2061 {
2062 struct isapnp_irq *next;
2063
2064 while (irq) {
2065 next = irq->next;
2066 kfree(irq);
2067 irq = next;
2068 }
2069 }
2070
2071 static void isapnp_free_dma(struct isapnp_dma *dma)
2072 {
2073 struct isapnp_dma *next;
2074
2075 while (dma) {
2076 next = dma->next;
2077 kfree(dma);
2078 dma = next;
2079 }
2080 }
2081
2082 static void isapnp_free_mem(struct isapnp_mem *mem)
2083 {
2084 struct isapnp_mem *next;
2085
2086 while (mem) {
2087 next = mem->next;
2088 kfree(mem);
2089 mem = next;
2090 }
2091 }
2092
2093 static void isapnp_free_mem32(struct isapnp_mem32 *mem32)
2094 {
2095 struct isapnp_mem32 *next;
2096
2097 while (mem32) {
2098 next = mem32->next;
2099 kfree(mem32);
2100 mem32 = next;
2101 }
2102 }
2103
2104 static void isapnp_free_resources(struct isapnp_resources *resources, int alt)
2105 {
2106 struct isapnp_resources *next;
2107
2108 while (resources) {
2109 next = alt ? resources->alt : resources->next;
2110 isapnp_free_port(resources->port);
2111 isapnp_free_irq(resources->irq);
2112 isapnp_free_dma(resources->dma);
2113 isapnp_free_mem(resources->mem);
2114 isapnp_free_mem32(resources->mem32);
2115 if (!alt && resources->alt)
2116 isapnp_free_resources(resources->alt, 1);
2117 kfree(resources);
2118 resources = next;
2119 }
2120 }
2121
2122 static void isapnp_free_card(struct pci_bus *card)
2123 {
2124 while (!list_empty(&card->devices)) {
2125 struct list_head *list = card->devices.next;
2126 struct pci_dev *dev = pci_dev_b(list);
2127 list_del(list);
2128 isapnp_free_resources((struct isapnp_resources *)dev->sysdata, 0);
2129 kfree(dev);
2130 }
2131 kfree(card);
2132 }
2133
2134 #endif /* MODULE */
2135
2136 static void isapnp_free_all_resources(void)
2137 {
2138 #ifdef ISAPNP_REGION_OK
2139 if (pidxr_res)
2140 release_resource(pidxr_res);
2141 #endif
2142 if (pnpwrp_res)
2143 release_resource(pnpwrp_res);
2144 if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff && isapnp_rdp_res)
2145 release_resource(isapnp_rdp_res);
2146 #ifdef MODULE
2147 #ifdef CONFIG_PROC_FS
2148 isapnp_proc_done();
2149 #endif
2150 while (!list_empty(&isapnp_cards)) {
2151 struct list_head *list = isapnp_cards.next;
2152 list_del(list);
2153 isapnp_free_card(pci_bus_b(list));
2154 }
2155 #endif
2156 }
2157
2158 EXPORT_SYMBOL(isapnp_cards);
2159 EXPORT_SYMBOL(isapnp_devices);
2160 EXPORT_SYMBOL(isapnp_present);
2161 EXPORT_SYMBOL(isapnp_cfg_begin);
2162 EXPORT_SYMBOL(isapnp_cfg_end);
2163 EXPORT_SYMBOL(isapnp_read_byte);
2164 EXPORT_SYMBOL(isapnp_read_word);
2165 EXPORT_SYMBOL(isapnp_read_dword);
2166 EXPORT_SYMBOL(isapnp_write_byte);
2167 EXPORT_SYMBOL(isapnp_write_word);
2168 EXPORT_SYMBOL(isapnp_write_dword);
2169 EXPORT_SYMBOL(isapnp_wake);
2170 EXPORT_SYMBOL(isapnp_device);
2171 EXPORT_SYMBOL(isapnp_activate);
2172 EXPORT_SYMBOL(isapnp_deactivate);
2173 EXPORT_SYMBOL(isapnp_find_card);
2174 EXPORT_SYMBOL(isapnp_find_dev);
2175 EXPORT_SYMBOL(isapnp_probe_cards);
2176 EXPORT_SYMBOL(isapnp_probe_devs);
2177 EXPORT_SYMBOL(isapnp_activate_dev);
2178 EXPORT_SYMBOL(isapnp_resource_change);
2179
2180 int __init isapnp_init(void)
2181 {
2182 int cards;
2183 struct pci_bus *card;
2184
2185 if (isapnp_disable) {
2186 isapnp_detected = 0;
2187 printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
2188 return 0;
2189 }
2190 #ifdef ISAPNP_REGION_OK
2191 pidxr_res=request_region(_PIDXR, 1, "isapnp index");
2192 if(!pidxr_res) {
2193 printk(KERN_ERR "isapnp: Index Register 0x%x already used\n", _PIDXR);
2194 return -EBUSY;
2195 }
2196 #endif
2197 pnpwrp_res=request_region(_PNPWRP, 1, "isapnp write");
2198 if(!pnpwrp_res) {
2199 printk(KERN_ERR "isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
2200 #ifdef ISAPNP_REGION_OK
2201 release_region(_PIDXR, 1);
2202 #endif
2203 return -EBUSY;
2204 }
2205
2206 /*
2207 * Print a message. The existing ISAPnP code is hanging machines
2208 * so let the user know where.
2209 */
2210
2211 printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
2212 if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
2213 isapnp_rdp |= 3;
2214 isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
2215 if(!isapnp_rdp_res) {
2216 printk(KERN_ERR "isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
2217 #ifdef ISAPNP_REGION_OK
2218 release_region(_PIDXR, 1);
2219 #endif
2220 release_region(isapnp_rdp, 1);
2221 return -EBUSY;
2222 }
2223 isapnp_set_rdp();
2224 }
2225 isapnp_detected = 1;
2226 if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
2227 cards = isapnp_isolate();
2228 if (cards < 0 ||
2229 (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
2230 isapnp_free_all_resources();
2231 isapnp_detected = 0;
2232 printk(KERN_INFO "isapnp: No Plug & Play device found\n");
2233 return 0;
2234 }
2235 isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
2236 }
2237 isapnp_build_device_list();
2238 cards = 0;
2239
2240 isapnp_for_each_card(card) {
2241 cards++;
2242 if (isapnp_verbose) {
2243 struct list_head *devlist;
2244 printk(KERN_INFO "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
2245 if (isapnp_verbose < 2)
2246 continue;
2247 for (devlist = card->devices.next; devlist != &card->devices; devlist = devlist->next) {
2248 struct pci_dev *dev = pci_dev_b(devlist);
2249 printk(KERN_INFO "isapnp: Device '%s'\n", dev->name[0]?card->name:"Unknown");
2250 }
2251 }
2252 }
2253 if (cards) {
2254 printk(KERN_INFO "isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
2255 } else {
2256 printk(KERN_INFO "isapnp: No Plug & Play card found\n");
2257 }
2258 #ifdef CONFIG_PROC_FS
2259 isapnp_proc_init();
2260 #endif
2261 return 0;
2262 }
2263
2264 #ifdef MODULE
2265
2266 int init_module(void)
2267 {
2268 return isapnp_init();
2269 }
2270
2271 void cleanup_module(void)
2272 {
2273 if (isapnp_detected)
2274 isapnp_free_all_resources();
2275 }
2276
2277 #else
2278
2279 /* format is: noisapnp */
2280
2281 static int __init isapnp_setup_disable(char *str)
2282 {
2283 isapnp_disable = 1;
2284 return 1;
2285 }
2286
2287 __setup("noisapnp", isapnp_setup_disable);
2288
2289 /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
2290
2291 static int __init isapnp_setup_isapnp(char *str)
2292 {
2293 (void)((get_option(&str,&isapnp_rdp) == 2) &&
2294 (get_option(&str,&isapnp_reset) == 2) &&
2295 (get_option(&str,&isapnp_skip_pci_scan) == 2) &&
2296 (get_option(&str,&isapnp_verbose) == 2));
2297 return 1;
2298 }
2299
2300 __setup("isapnp=", isapnp_setup_isapnp);
2301
2302 /* format is: isapnp_reserve_irq=irq1[,irq2] .... */
2303
2304 static int __init isapnp_setup_reserve_irq(char *str)
2305 {
2306 int i;
2307
2308 for (i = 0; i < 16; i++)
2309 if (get_option(&str,&isapnp_reserve_irq[i]) != 2)
2310 break;
2311 return 1;
2312 }
2313
2314 __setup("isapnp_reserve_irq=", isapnp_setup_reserve_irq);
2315
2316 /* format is: isapnp_reserve_dma=dma1[,dma2] .... */
2317
2318 static int __init isapnp_setup_reserve_dma(char *str)
2319 {
2320 int i;
2321
2322 for (i = 0; i < 8; i++)
2323 if (get_option(&str,&isapnp_reserve_dma[i]) != 2)
2324 break;
2325 return 1;
2326 }
2327
2328 __setup("isapnp_reserve_dma=", isapnp_setup_reserve_dma);
2329
2330 /* format is: isapnp_reserve_io=io1,size1[,io2,size2] .... */
2331
2332 static int __init isapnp_setup_reserve_io(char *str)
2333 {
2334 int i;
2335
2336 for (i = 0; i < 16; i++)
2337 if (get_option(&str,&isapnp_reserve_io[i]) != 2)
2338 break;
2339 return 1;
2340 }
2341
2342 __setup("isapnp_reserve_io=", isapnp_setup_reserve_io);
2343
2344 /* format is: isapnp_reserve_mem=mem1,size1[,mem2,size2] .... */
2345
2346 static int __init isapnp_setup_reserve_mem(char *str)
2347 {
2348 int i;
2349
2350 for (i = 0; i < 16; i++)
2351 if (get_option(&str,&isapnp_reserve_mem[i]) != 2)
2352 break;
2353 return 1;
2354 }
2355
2356 __setup("isapnp_reserve_mem=", isapnp_setup_reserve_mem);
2357
2358 #endif
2359