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