]> err.no Git - linux-2.6/blob - drivers/pnp/isapnp/core.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / pnp / isapnp / core.c
1 /*
2  *  ISA Plug & Play support
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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@infradead.org>
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  *  2001-11-07  Added isapnp_{,un}register_driver calls along the lines
29  *              of the pci driver interface
30  *              Kai Germaschewski <kai.germaschewski@gmx.de>
31  *  2002-06-06  Made the use of dma channel 0 configurable
32  *              Gerald Teschl <gerald.teschl@univie.ac.at>
33  *  2002-10-06  Ported to PnP Layer - Adam Belay <ambx1@neo.rr.com>
34  *  2003-08-11  Resource Management Updates - Adam Belay <ambx1@neo.rr.com>
35  */
36
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/slab.h>
41 #include <linux/delay.h>
42 #include <linux/init.h>
43 #include <linux/isapnp.h>
44 #include <linux/mutex.h>
45 #include <asm/io.h>
46
47 #if 0
48 #define ISAPNP_REGION_OK
49 #endif
50
51 int isapnp_disable;             /* Disable ISA PnP */
52 static int isapnp_rdp;          /* Read Data Port */
53 static int isapnp_reset = 1;    /* reset all PnP cards (deactivate) */
54 static int isapnp_verbose = 1;  /* verbose mode */
55
56 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
57 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
58 module_param(isapnp_disable, int, 0);
59 MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
60 module_param(isapnp_rdp, int, 0);
61 MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
62 module_param(isapnp_reset, int, 0);
63 MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
64 module_param(isapnp_verbose, int, 0);
65 MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
66 MODULE_LICENSE("GPL");
67
68 #define _PIDXR          0x279
69 #define _PNPWRP         0xa79
70
71 /* short tags */
72 #define _STAG_PNPVERNO          0x01
73 #define _STAG_LOGDEVID          0x02
74 #define _STAG_COMPATDEVID       0x03
75 #define _STAG_IRQ               0x04
76 #define _STAG_DMA               0x05
77 #define _STAG_STARTDEP          0x06
78 #define _STAG_ENDDEP            0x07
79 #define _STAG_IOPORT            0x08
80 #define _STAG_FIXEDIO           0x09
81 #define _STAG_VENDOR            0x0e
82 #define _STAG_END               0x0f
83 /* long tags */
84 #define _LTAG_MEMRANGE          0x81
85 #define _LTAG_ANSISTR           0x82
86 #define _LTAG_UNICODESTR        0x83
87 #define _LTAG_VENDOR            0x84
88 #define _LTAG_MEM32RANGE        0x85
89 #define _LTAG_FIXEDMEM32RANGE   0x86
90
91 /*
92  * Sizes of ISAPNP logical device configuration register sets.
93  * See PNP-ISA-v1.0a.pdf, Appendix A.
94  */
95 #define ISAPNP_MAX_MEM          4
96 #define ISAPNP_MAX_PORT         8
97 #define ISAPNP_MAX_IRQ          2
98 #define ISAPNP_MAX_DMA          2
99
100 static unsigned char isapnp_checksum_value;
101 static DEFINE_MUTEX(isapnp_cfg_mutex);
102 static int isapnp_csn_count;
103
104 /* some prototypes */
105
106 static inline void write_data(unsigned char x)
107 {
108         outb(x, _PNPWRP);
109 }
110
111 static inline void write_address(unsigned char x)
112 {
113         outb(x, _PIDXR);
114         udelay(20);
115 }
116
117 static inline unsigned char read_data(void)
118 {
119         unsigned char val = inb(isapnp_rdp);
120         return val;
121 }
122
123 unsigned char isapnp_read_byte(unsigned char idx)
124 {
125         write_address(idx);
126         return read_data();
127 }
128
129 static unsigned short isapnp_read_word(unsigned char idx)
130 {
131         unsigned short val;
132
133         val = isapnp_read_byte(idx);
134         val = (val << 8) + isapnp_read_byte(idx + 1);
135         return val;
136 }
137
138 void isapnp_write_byte(unsigned char idx, unsigned char val)
139 {
140         write_address(idx);
141         write_data(val);
142 }
143
144 static void isapnp_write_word(unsigned char idx, unsigned short val)
145 {
146         isapnp_write_byte(idx, val >> 8);
147         isapnp_write_byte(idx + 1, val);
148 }
149
150 static void isapnp_key(void)
151 {
152         unsigned char code = 0x6a, msb;
153         int i;
154
155         mdelay(1);
156         write_address(0x00);
157         write_address(0x00);
158
159         write_address(code);
160
161         for (i = 1; i < 32; i++) {
162                 msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
163                 code = (code >> 1) | msb;
164                 write_address(code);
165         }
166 }
167
168 /* place all pnp cards in wait-for-key state */
169 static void isapnp_wait(void)
170 {
171         isapnp_write_byte(0x02, 0x02);
172 }
173
174 static void isapnp_wake(unsigned char csn)
175 {
176         isapnp_write_byte(0x03, csn);
177 }
178
179 static void isapnp_device(unsigned char logdev)
180 {
181         isapnp_write_byte(0x07, logdev);
182 }
183
184 static void isapnp_activate(unsigned char logdev)
185 {
186         isapnp_device(logdev);
187         isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
188         udelay(250);
189 }
190
191 static void isapnp_deactivate(unsigned char logdev)
192 {
193         isapnp_device(logdev);
194         isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
195         udelay(500);
196 }
197
198 static void __init isapnp_peek(unsigned char *data, int bytes)
199 {
200         int i, j;
201         unsigned char d = 0;
202
203         for (i = 1; i <= bytes; i++) {
204                 for (j = 0; j < 20; j++) {
205                         d = isapnp_read_byte(0x05);
206                         if (d & 1)
207                                 break;
208                         udelay(100);
209                 }
210                 if (!(d & 1)) {
211                         if (data != NULL)
212                                 *data++ = 0xff;
213                         continue;
214                 }
215                 d = isapnp_read_byte(0x04);     /* PRESDI */
216                 isapnp_checksum_value += d;
217                 if (data != NULL)
218                         *data++ = d;
219         }
220 }
221
222 #define RDP_STEP        32      /* minimum is 4 */
223
224 static int isapnp_next_rdp(void)
225 {
226         int rdp = isapnp_rdp;
227         static int old_rdp = 0;
228
229         if (old_rdp) {
230                 release_region(old_rdp, 1);
231                 old_rdp = 0;
232         }
233         while (rdp <= 0x3ff) {
234                 /*
235                  *      We cannot use NE2000 probe spaces for ISAPnP or we
236                  *      will lock up machines.
237                  */
238                 if ((rdp < 0x280 || rdp > 0x380)
239                     && request_region(rdp, 1, "ISAPnP")) {
240                         isapnp_rdp = rdp;
241                         old_rdp = rdp;
242                         return 0;
243                 }
244                 rdp += RDP_STEP;
245         }
246         return -1;
247 }
248
249 /* Set read port address */
250 static inline void isapnp_set_rdp(void)
251 {
252         isapnp_write_byte(0x00, isapnp_rdp >> 2);
253         udelay(100);
254 }
255
256 /*
257  *      Perform an isolation. The port selection code now tries to avoid
258  *      "dangerous to read" ports.
259  */
260 static int __init isapnp_isolate_rdp_select(void)
261 {
262         isapnp_wait();
263         isapnp_key();
264
265         /* Control: reset CSN and conditionally everything else too */
266         isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
267         mdelay(2);
268
269         isapnp_wait();
270         isapnp_key();
271         isapnp_wake(0x00);
272
273         if (isapnp_next_rdp() < 0) {
274                 isapnp_wait();
275                 return -1;
276         }
277
278         isapnp_set_rdp();
279         udelay(1000);
280         write_address(0x01);
281         udelay(1000);
282         return 0;
283 }
284
285 /*
286  *  Isolate (assign uniqued CSN) to all ISA PnP devices.
287  */
288 static int __init isapnp_isolate(void)
289 {
290         unsigned char checksum = 0x6a;
291         unsigned char chksum = 0x00;
292         unsigned char bit = 0x00;
293         int data;
294         int csn = 0;
295         int i;
296         int iteration = 1;
297
298         isapnp_rdp = 0x213;
299         if (isapnp_isolate_rdp_select() < 0)
300                 return -1;
301
302         while (1) {
303                 for (i = 1; i <= 64; i++) {
304                         data = read_data() << 8;
305                         udelay(250);
306                         data = data | read_data();
307                         udelay(250);
308                         if (data == 0x55aa)
309                                 bit = 0x01;
310                         checksum =
311                             ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
312                             | (checksum >> 1);
313                         bit = 0x00;
314                 }
315                 for (i = 65; i <= 72; i++) {
316                         data = read_data() << 8;
317                         udelay(250);
318                         data = data | read_data();
319                         udelay(250);
320                         if (data == 0x55aa)
321                                 chksum |= (1 << (i - 65));
322                 }
323                 if (checksum != 0x00 && checksum == chksum) {
324                         csn++;
325
326                         isapnp_write_byte(0x06, csn);
327                         udelay(250);
328                         iteration++;
329                         isapnp_wake(0x00);
330                         isapnp_set_rdp();
331                         udelay(1000);
332                         write_address(0x01);
333                         udelay(1000);
334                         goto __next;
335                 }
336                 if (iteration == 1) {
337                         isapnp_rdp += RDP_STEP;
338                         if (isapnp_isolate_rdp_select() < 0)
339                                 return -1;
340                 } else if (iteration > 1) {
341                         break;
342                 }
343 __next:
344                 if (csn == 255)
345                         break;
346                 checksum = 0x6a;
347                 chksum = 0x00;
348                 bit = 0x00;
349         }
350         isapnp_wait();
351         isapnp_csn_count = csn;
352         return csn;
353 }
354
355 /*
356  *  Read one tag from stream.
357  */
358 static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
359 {
360         unsigned char tag, tmp[2];
361
362         isapnp_peek(&tag, 1);
363         if (tag == 0)           /* invalid tag */
364                 return -1;
365         if (tag & 0x80) {       /* large item */
366                 *type = tag;
367                 isapnp_peek(tmp, 2);
368                 *size = (tmp[1] << 8) | tmp[0];
369         } else {
370                 *type = (tag >> 3) & 0x0f;
371                 *size = tag & 0x07;
372         }
373 #if 0
374         printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type,
375                *size);
376 #endif
377         if (*type == 0xff && *size == 0xffff)   /* probably invalid data */
378                 return -1;
379         return 0;
380 }
381
382 /*
383  *  Skip specified number of bytes from stream.
384  */
385 static void __init isapnp_skip_bytes(int count)
386 {
387         isapnp_peek(NULL, count);
388 }
389
390 /*
391  *  Parse EISA id.
392  */
393 static void isapnp_parse_id(struct pnp_dev *dev, unsigned short vendor,
394                             unsigned short device)
395 {
396         struct pnp_id *id;
397
398         if (!dev)
399                 return;
400         id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
401         if (!id)
402                 return;
403         sprintf(id->id, "%c%c%c%x%x%x%x",
404                 'A' + ((vendor >> 2) & 0x3f) - 1,
405                 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
406                 'A' + ((vendor >> 8) & 0x1f) - 1,
407                 (device >> 4) & 0x0f,
408                 device & 0x0f, (device >> 12) & 0x0f, (device >> 8) & 0x0f);
409         pnp_add_id(id, dev);
410 }
411
412 /*
413  *  Parse logical device tag.
414  */
415 static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
416                                                   int size, int number)
417 {
418         unsigned char tmp[6];
419         struct pnp_dev *dev;
420
421         isapnp_peek(tmp, size);
422         dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
423         if (!dev)
424                 return NULL;
425         dev->number = number;
426         isapnp_parse_id(dev, (tmp[1] << 8) | tmp[0], (tmp[3] << 8) | tmp[2]);
427         dev->regs = tmp[4];
428         dev->card = card;
429         if (size > 5)
430                 dev->regs |= tmp[5] << 8;
431         dev->protocol = &isapnp_protocol;
432         dev->capabilities |= PNP_CONFIGURABLE;
433         dev->capabilities |= PNP_READ;
434         dev->capabilities |= PNP_WRITE;
435         dev->capabilities |= PNP_DISABLE;
436         pnp_init_resource_table(&dev->res);
437         return dev;
438 }
439
440 /*
441  *  Add IRQ resource to resources list.
442  */
443 static void __init isapnp_parse_irq_resource(struct pnp_option *option,
444                                              int size)
445 {
446         unsigned char tmp[3];
447         struct pnp_irq *irq;
448         unsigned long bits;
449
450         isapnp_peek(tmp, size);
451         irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
452         if (!irq)
453                 return;
454         bits = (tmp[1] << 8) | tmp[0];
455         bitmap_copy(irq->map, &bits, 16);
456         if (size > 2)
457                 irq->flags = tmp[2];
458         else
459                 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
460         pnp_register_irq_resource(option, irq);
461 }
462
463 /*
464  *  Add DMA resource to resources list.
465  */
466 static void __init isapnp_parse_dma_resource(struct pnp_option *option,
467                                              int size)
468 {
469         unsigned char tmp[2];
470         struct pnp_dma *dma;
471
472         isapnp_peek(tmp, size);
473         dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
474         if (!dma)
475                 return;
476         dma->map = tmp[0];
477         dma->flags = tmp[1];
478         pnp_register_dma_resource(option, dma);
479 }
480
481 /*
482  *  Add port resource to resources list.
483  */
484 static void __init isapnp_parse_port_resource(struct pnp_option *option,
485                                               int size)
486 {
487         unsigned char tmp[7];
488         struct pnp_port *port;
489
490         isapnp_peek(tmp, size);
491         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
492         if (!port)
493                 return;
494         port->min = (tmp[2] << 8) | tmp[1];
495         port->max = (tmp[4] << 8) | tmp[3];
496         port->align = tmp[5];
497         port->size = tmp[6];
498         port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
499         pnp_register_port_resource(option, port);
500 }
501
502 /*
503  *  Add fixed port resource to resources list.
504  */
505 static void __init isapnp_parse_fixed_port_resource(struct pnp_option *option,
506                                                     int size)
507 {
508         unsigned char tmp[3];
509         struct pnp_port *port;
510
511         isapnp_peek(tmp, size);
512         port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
513         if (!port)
514                 return;
515         port->min = port->max = (tmp[1] << 8) | tmp[0];
516         port->size = tmp[2];
517         port->align = 0;
518         port->flags = PNP_PORT_FLAG_FIXED;
519         pnp_register_port_resource(option, port);
520 }
521
522 /*
523  *  Add memory resource to resources list.
524  */
525 static void __init isapnp_parse_mem_resource(struct pnp_option *option,
526                                              int size)
527 {
528         unsigned char tmp[9];
529         struct pnp_mem *mem;
530
531         isapnp_peek(tmp, size);
532         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
533         if (!mem)
534                 return;
535         mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
536         mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
537         mem->align = (tmp[6] << 8) | tmp[5];
538         mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
539         mem->flags = tmp[0];
540         pnp_register_mem_resource(option, mem);
541 }
542
543 /*
544  *  Add 32-bit memory resource to resources list.
545  */
546 static void __init isapnp_parse_mem32_resource(struct pnp_option *option,
547                                                int size)
548 {
549         unsigned char tmp[17];
550         struct pnp_mem *mem;
551
552         isapnp_peek(tmp, size);
553         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
554         if (!mem)
555                 return;
556         mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
557         mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
558         mem->align =
559             (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
560         mem->size =
561             (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
562         mem->flags = tmp[0];
563         pnp_register_mem_resource(option, mem);
564 }
565
566 /*
567  *  Add 32-bit fixed memory resource to resources list.
568  */
569 static void __init isapnp_parse_fixed_mem32_resource(struct pnp_option *option,
570                                                      int size)
571 {
572         unsigned char tmp[9];
573         struct pnp_mem *mem;
574
575         isapnp_peek(tmp, size);
576         mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
577         if (!mem)
578                 return;
579         mem->min = mem->max =
580             (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
581         mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
582         mem->align = 0;
583         mem->flags = tmp[0];
584         pnp_register_mem_resource(option, mem);
585 }
586
587 /*
588  *  Parse card name for ISA PnP device.
589  */
590 static void __init
591 isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
592 {
593         if (name[0] == '\0') {
594                 unsigned short size1 =
595                     *size >= name_max ? (name_max - 1) : *size;
596                 isapnp_peek(name, size1);
597                 name[size1] = '\0';
598                 *size -= size1;
599
600                 /* clean whitespace from end of string */
601                 while (size1 > 0 && name[--size1] == ' ')
602                         name[size1] = '\0';
603         }
604 }
605
606 /*
607  *  Parse resource map for logical device.
608  */
609 static int __init isapnp_create_device(struct pnp_card *card,
610                                        unsigned short size)
611 {
612         int number = 0, skip = 0, priority = 0, compat = 0;
613         unsigned char type, tmp[17];
614         struct pnp_option *option;
615         struct pnp_dev *dev;
616
617         if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
618                 return 1;
619         option = pnp_register_independent_option(dev);
620         if (!option) {
621                 kfree(dev);
622                 return 1;
623         }
624         pnp_add_card_device(card, dev);
625
626         while (1) {
627                 if (isapnp_read_tag(&type, &size) < 0)
628                         return 1;
629                 if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
630                         goto __skip;
631                 switch (type) {
632                 case _STAG_LOGDEVID:
633                         if (size >= 5 && size <= 6) {
634                                 if ((dev =
635                                      isapnp_parse_device(card, size,
636                                                          number++)) == NULL)
637                                         return 1;
638                                 size = 0;
639                                 skip = 0;
640                                 option = pnp_register_independent_option(dev);
641                                 if (!option) {
642                                         kfree(dev);
643                                         return 1;
644                                 }
645                                 pnp_add_card_device(card, dev);
646                         } else {
647                                 skip = 1;
648                         }
649                         priority = 0;
650                         compat = 0;
651                         break;
652                 case _STAG_COMPATDEVID:
653                         if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
654                                 isapnp_peek(tmp, 4);
655                                 isapnp_parse_id(dev, (tmp[1] << 8) | tmp[0],
656                                                 (tmp[3] << 8) | tmp[2]);
657                                 compat++;
658                                 size = 0;
659                         }
660                         break;
661                 case _STAG_IRQ:
662                         if (size < 2 || size > 3)
663                                 goto __skip;
664                         isapnp_parse_irq_resource(option, size);
665                         size = 0;
666                         break;
667                 case _STAG_DMA:
668                         if (size != 2)
669                                 goto __skip;
670                         isapnp_parse_dma_resource(option, size);
671                         size = 0;
672                         break;
673                 case _STAG_STARTDEP:
674                         if (size > 1)
675                                 goto __skip;
676                         priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
677                         if (size > 0) {
678                                 isapnp_peek(tmp, size);
679                                 priority = 0x100 | tmp[0];
680                                 size = 0;
681                         }
682                         option = pnp_register_dependent_option(dev, priority);
683                         if (!option)
684                                 return 1;
685                         break;
686                 case _STAG_ENDDEP:
687                         if (size != 0)
688                                 goto __skip;
689                         priority = 0;
690                         break;
691                 case _STAG_IOPORT:
692                         if (size != 7)
693                                 goto __skip;
694                         isapnp_parse_port_resource(option, size);
695                         size = 0;
696                         break;
697                 case _STAG_FIXEDIO:
698                         if (size != 3)
699                                 goto __skip;
700                         isapnp_parse_fixed_port_resource(option, size);
701                         size = 0;
702                         break;
703                 case _STAG_VENDOR:
704                         break;
705                 case _LTAG_MEMRANGE:
706                         if (size != 9)
707                                 goto __skip;
708                         isapnp_parse_mem_resource(option, size);
709                         size = 0;
710                         break;
711                 case _LTAG_ANSISTR:
712                         isapnp_parse_name(dev->name, sizeof(dev->name), &size);
713                         break;
714                 case _LTAG_UNICODESTR:
715                         /* silently ignore */
716                         /* who use unicode for hardware identification? */
717                         break;
718                 case _LTAG_VENDOR:
719                         break;
720                 case _LTAG_MEM32RANGE:
721                         if (size != 17)
722                                 goto __skip;
723                         isapnp_parse_mem32_resource(option, size);
724                         size = 0;
725                         break;
726                 case _LTAG_FIXEDMEM32RANGE:
727                         if (size != 9)
728                                 goto __skip;
729                         isapnp_parse_fixed_mem32_resource(option, size);
730                         size = 0;
731                         break;
732                 case _STAG_END:
733                         if (size > 0)
734                                 isapnp_skip_bytes(size);
735                         return 1;
736                 default:
737                         printk(KERN_ERR
738                                "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n",
739                                type, dev->number, card->number);
740                 }
741 __skip:
742                 if (size > 0)
743                         isapnp_skip_bytes(size);
744         }
745         return 0;
746 }
747
748 /*
749  *  Parse resource map for ISA PnP card.
750  */
751 static void __init isapnp_parse_resource_map(struct pnp_card *card)
752 {
753         unsigned char type, tmp[17];
754         unsigned short size;
755
756         while (1) {
757                 if (isapnp_read_tag(&type, &size) < 0)
758                         return;
759                 switch (type) {
760                 case _STAG_PNPVERNO:
761                         if (size != 2)
762                                 goto __skip;
763                         isapnp_peek(tmp, 2);
764                         card->pnpver = tmp[0];
765                         card->productver = tmp[1];
766                         size = 0;
767                         break;
768                 case _STAG_LOGDEVID:
769                         if (size >= 5 && size <= 6) {
770                                 if (isapnp_create_device(card, size) == 1)
771                                         return;
772                                 size = 0;
773                         }
774                         break;
775                 case _STAG_VENDOR:
776                         break;
777                 case _LTAG_ANSISTR:
778                         isapnp_parse_name(card->name, sizeof(card->name),
779                                           &size);
780                         break;
781                 case _LTAG_UNICODESTR:
782                         /* silently ignore */
783                         /* who use unicode for hardware identification? */
784                         break;
785                 case _LTAG_VENDOR:
786                         break;
787                 case _STAG_END:
788                         if (size > 0)
789                                 isapnp_skip_bytes(size);
790                         return;
791                 default:
792                         printk(KERN_ERR
793                                "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n",
794                                type, card->number);
795                 }
796 __skip:
797                 if (size > 0)
798                         isapnp_skip_bytes(size);
799         }
800 }
801
802 /*
803  *  Compute ISA PnP checksum for first eight bytes.
804  */
805 static unsigned char __init isapnp_checksum(unsigned char *data)
806 {
807         int i, j;
808         unsigned char checksum = 0x6a, bit, b;
809
810         for (i = 0; i < 8; i++) {
811                 b = data[i];
812                 for (j = 0; j < 8; j++) {
813                         bit = 0;
814                         if (b & (1 << j))
815                                 bit = 1;
816                         checksum =
817                             ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
818                             | (checksum >> 1);
819                 }
820         }
821         return checksum;
822 }
823
824 /*
825  *  Parse EISA id for ISA PnP card.
826  */
827 static void isapnp_parse_card_id(struct pnp_card *card, unsigned short vendor,
828                                  unsigned short device)
829 {
830         struct pnp_id *id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
831
832         if (!id)
833                 return;
834         sprintf(id->id, "%c%c%c%x%x%x%x",
835                 'A' + ((vendor >> 2) & 0x3f) - 1,
836                 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
837                 'A' + ((vendor >> 8) & 0x1f) - 1,
838                 (device >> 4) & 0x0f,
839                 device & 0x0f, (device >> 12) & 0x0f, (device >> 8) & 0x0f);
840         pnp_add_card_id(id, card);
841 }
842
843 /*
844  *  Build device list for all present ISA PnP devices.
845  */
846 static int __init isapnp_build_device_list(void)
847 {
848         int csn;
849         unsigned char header[9], checksum;
850         struct pnp_card *card;
851
852         isapnp_wait();
853         isapnp_key();
854         for (csn = 1; csn <= isapnp_csn_count; csn++) {
855                 isapnp_wake(csn);
856                 isapnp_peek(header, 9);
857                 checksum = isapnp_checksum(header);
858 #if 0
859                 printk(KERN_DEBUG
860                        "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
861                        header[0], header[1], header[2], header[3], header[4],
862                        header[5], header[6], header[7], header[8]);
863                 printk(KERN_DEBUG "checksum = 0x%x\n", checksum);
864 #endif
865                 if ((card =
866                      kzalloc(sizeof(struct pnp_card), GFP_KERNEL)) == NULL)
867                         continue;
868
869                 card->number = csn;
870                 INIT_LIST_HEAD(&card->devices);
871                 isapnp_parse_card_id(card, (header[1] << 8) | header[0],
872                                      (header[3] << 8) | header[2]);
873                 card->serial =
874                     (header[7] << 24) | (header[6] << 16) | (header[5] << 8) |
875                     header[4];
876                 isapnp_checksum_value = 0x00;
877                 isapnp_parse_resource_map(card);
878                 if (isapnp_checksum_value != 0x00)
879                         printk(KERN_ERR
880                                "isapnp: checksum for device %i is not valid (0x%x)\n",
881                                csn, isapnp_checksum_value);
882                 card->checksum = isapnp_checksum_value;
883                 card->protocol = &isapnp_protocol;
884
885                 pnp_add_card(card);
886         }
887         isapnp_wait();
888         return 0;
889 }
890
891 /*
892  *  Basic configuration routines.
893  */
894
895 int isapnp_present(void)
896 {
897         struct pnp_card *card;
898
899         pnp_for_each_card(card) {
900                 if (card->protocol == &isapnp_protocol)
901                         return 1;
902         }
903         return 0;
904 }
905
906 int isapnp_cfg_begin(int csn, int logdev)
907 {
908         if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
909                 return -EINVAL;
910         mutex_lock(&isapnp_cfg_mutex);
911         isapnp_wait();
912         isapnp_key();
913         isapnp_wake(csn);
914 #if 0
915         /* to avoid malfunction when the isapnptools package is used */
916         /* we must set RDP to our value again */
917         /* it is possible to set RDP only in the isolation phase */
918         /*   Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
919         isapnp_write_byte(0x02, 0x04);  /* clear CSN of card */
920         mdelay(2);              /* is this necessary? */
921         isapnp_wake(csn);       /* bring card into sleep state */
922         isapnp_wake(0);         /* bring card into isolation state */
923         isapnp_set_rdp();       /* reset the RDP port */
924         udelay(1000);           /* delay 1000us */
925         isapnp_write_byte(0x06, csn);   /* reset CSN to previous value */
926         udelay(250);            /* is this necessary? */
927 #endif
928         if (logdev >= 0)
929                 isapnp_device(logdev);
930         return 0;
931 }
932
933 int isapnp_cfg_end(void)
934 {
935         isapnp_wait();
936         mutex_unlock(&isapnp_cfg_mutex);
937         return 0;
938 }
939
940 /*
941  *  Initialization.
942  */
943
944 EXPORT_SYMBOL(isapnp_protocol);
945 EXPORT_SYMBOL(isapnp_present);
946 EXPORT_SYMBOL(isapnp_cfg_begin);
947 EXPORT_SYMBOL(isapnp_cfg_end);
948 EXPORT_SYMBOL(isapnp_write_byte);
949
950 static int isapnp_read_resources(struct pnp_dev *dev,
951                                  struct pnp_resource_table *res)
952 {
953         int tmp, ret;
954
955         dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
956         if (dev->active) {
957                 for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
958                         ret = isapnp_read_word(ISAPNP_CFG_PORT + (tmp << 1));
959                         if (!ret)
960                                 continue;
961                         res->port_resource[tmp].start = ret;
962                         res->port_resource[tmp].flags = IORESOURCE_IO;
963                 }
964                 for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
965                         ret =
966                             isapnp_read_word(ISAPNP_CFG_MEM + (tmp << 3)) << 8;
967                         if (!ret)
968                                 continue;
969                         res->mem_resource[tmp].start = ret;
970                         res->mem_resource[tmp].flags = IORESOURCE_MEM;
971                 }
972                 for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
973                         ret =
974                             (isapnp_read_word(ISAPNP_CFG_IRQ + (tmp << 1)) >>
975                              8);
976                         if (!ret)
977                                 continue;
978                         res->irq_resource[tmp].start =
979                             res->irq_resource[tmp].end = ret;
980                         res->irq_resource[tmp].flags = IORESOURCE_IRQ;
981                 }
982                 for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
983                         ret = isapnp_read_byte(ISAPNP_CFG_DMA + tmp);
984                         if (ret == 4)
985                                 continue;
986                         res->dma_resource[tmp].start =
987                             res->dma_resource[tmp].end = ret;
988                         res->dma_resource[tmp].flags = IORESOURCE_DMA;
989                 }
990         }
991         return 0;
992 }
993
994 static int isapnp_get_resources(struct pnp_dev *dev,
995                                 struct pnp_resource_table *res)
996 {
997         int ret;
998
999         pnp_init_resource_table(res);
1000         isapnp_cfg_begin(dev->card->number, dev->number);
1001         ret = isapnp_read_resources(dev, res);
1002         isapnp_cfg_end();
1003         return ret;
1004 }
1005
1006 static int isapnp_set_resources(struct pnp_dev *dev,
1007                                 struct pnp_resource_table *res)
1008 {
1009         int tmp;
1010
1011         isapnp_cfg_begin(dev->card->number, dev->number);
1012         dev->active = 1;
1013         for (tmp = 0;
1014              tmp < ISAPNP_MAX_PORT
1015              && (res->port_resource[tmp].
1016                  flags & (IORESOURCE_IO | IORESOURCE_UNSET)) == IORESOURCE_IO;
1017              tmp++)
1018                 isapnp_write_word(ISAPNP_CFG_PORT + (tmp << 1),
1019                                   res->port_resource[tmp].start);
1020         for (tmp = 0;
1021              tmp < ISAPNP_MAX_IRQ
1022              && (res->irq_resource[tmp].
1023                  flags & (IORESOURCE_IRQ | IORESOURCE_UNSET)) == IORESOURCE_IRQ;
1024              tmp++) {
1025                 int irq = res->irq_resource[tmp].start;
1026                 if (irq == 2)
1027                         irq = 9;
1028                 isapnp_write_byte(ISAPNP_CFG_IRQ + (tmp << 1), irq);
1029         }
1030         for (tmp = 0;
1031              tmp < ISAPNP_MAX_DMA
1032              && (res->dma_resource[tmp].
1033                  flags & (IORESOURCE_DMA | IORESOURCE_UNSET)) == IORESOURCE_DMA;
1034              tmp++)
1035                 isapnp_write_byte(ISAPNP_CFG_DMA + tmp,
1036                                   res->dma_resource[tmp].start);
1037         for (tmp = 0;
1038              tmp < ISAPNP_MAX_MEM
1039              && (res->mem_resource[tmp].
1040                  flags & (IORESOURCE_MEM | IORESOURCE_UNSET)) == IORESOURCE_MEM;
1041              tmp++)
1042                 isapnp_write_word(ISAPNP_CFG_MEM + (tmp << 3),
1043                                   (res->mem_resource[tmp].start >> 8) & 0xffff);
1044         /* FIXME: We aren't handling 32bit mems properly here */
1045         isapnp_activate(dev->number);
1046         isapnp_cfg_end();
1047         return 0;
1048 }
1049
1050 static int isapnp_disable_resources(struct pnp_dev *dev)
1051 {
1052         if (!dev->active)
1053                 return -EINVAL;
1054         isapnp_cfg_begin(dev->card->number, dev->number);
1055         isapnp_deactivate(dev->number);
1056         dev->active = 0;
1057         isapnp_cfg_end();
1058         return 0;
1059 }
1060
1061 struct pnp_protocol isapnp_protocol = {
1062         .name = "ISA Plug and Play",
1063         .get = isapnp_get_resources,
1064         .set = isapnp_set_resources,
1065         .disable = isapnp_disable_resources,
1066 };
1067
1068 static int __init isapnp_init(void)
1069 {
1070         int cards;
1071         struct pnp_card *card;
1072         struct pnp_dev *dev;
1073
1074         if (isapnp_disable) {
1075                 printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
1076                 return 0;
1077         }
1078 #ifdef CONFIG_PPC_MERGE
1079         if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
1080                 return -EINVAL;
1081 #endif
1082 #ifdef ISAPNP_REGION_OK
1083         if (!request_region(_PIDXR, 1, "isapnp index")) {
1084                 printk(KERN_ERR "isapnp: Index Register 0x%x already used\n",
1085                        _PIDXR);
1086                 return -EBUSY;
1087         }
1088 #endif
1089         if (!request_region(_PNPWRP, 1, "isapnp write")) {
1090                 printk(KERN_ERR
1091                        "isapnp: Write Data Register 0x%x already used\n",
1092                        _PNPWRP);
1093 #ifdef ISAPNP_REGION_OK
1094                 release_region(_PIDXR, 1);
1095 #endif
1096                 return -EBUSY;
1097         }
1098
1099         if (pnp_register_protocol(&isapnp_protocol) < 0)
1100                 return -EBUSY;
1101
1102         /*
1103          *      Print a message. The existing ISAPnP code is hanging machines
1104          *      so let the user know where.
1105          */
1106
1107         printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
1108         if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
1109                 isapnp_rdp |= 3;
1110                 if (!request_region(isapnp_rdp, 1, "isapnp read")) {
1111                         printk(KERN_ERR
1112                                "isapnp: Read Data Register 0x%x already used\n",
1113                                isapnp_rdp);
1114 #ifdef ISAPNP_REGION_OK
1115                         release_region(_PIDXR, 1);
1116 #endif
1117                         release_region(_PNPWRP, 1);
1118                         return -EBUSY;
1119                 }
1120                 isapnp_set_rdp();
1121         }
1122         if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
1123                 cards = isapnp_isolate();
1124                 if (cards < 0 || (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
1125 #ifdef ISAPNP_REGION_OK
1126                         release_region(_PIDXR, 1);
1127 #endif
1128                         release_region(_PNPWRP, 1);
1129                         printk(KERN_INFO
1130                                "isapnp: No Plug & Play device found\n");
1131                         return 0;
1132                 }
1133                 request_region(isapnp_rdp, 1, "isapnp read");
1134         }
1135         isapnp_build_device_list();
1136         cards = 0;
1137
1138         protocol_for_each_card(&isapnp_protocol, card) {
1139                 cards++;
1140                 if (isapnp_verbose) {
1141                         printk(KERN_INFO "isapnp: Card '%s'\n",
1142                                card->name[0] ? card->name : "Unknown");
1143                         if (isapnp_verbose < 2)
1144                                 continue;
1145                         card_for_each_dev(card, dev) {
1146                                 printk(KERN_INFO "isapnp:   Device '%s'\n",
1147                                        dev->name[0] ? dev->name : "Unknown");
1148                         }
1149                 }
1150         }
1151         if (cards)
1152                 printk(KERN_INFO
1153                        "isapnp: %i Plug & Play card%s detected total\n", cards,
1154                        cards > 1 ? "s" : "");
1155         else
1156                 printk(KERN_INFO "isapnp: No Plug & Play card found\n");
1157
1158         isapnp_proc_init();
1159         return 0;
1160 }
1161
1162 device_initcall(isapnp_init);
1163
1164 /* format is: noisapnp */
1165
1166 static int __init isapnp_setup_disable(char *str)
1167 {
1168         isapnp_disable = 1;
1169         return 1;
1170 }
1171
1172 __setup("noisapnp", isapnp_setup_disable);
1173
1174 /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
1175
1176 static int __init isapnp_setup_isapnp(char *str)
1177 {
1178         (void)((get_option(&str, &isapnp_rdp) == 2) &&
1179                (get_option(&str, &isapnp_reset) == 2) &&
1180                (get_option(&str, &isapnp_verbose) == 2));
1181         return 1;
1182 }
1183
1184 __setup("isapnp=", isapnp_setup_isapnp);