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