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