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