VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 #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
481         isapnp_peek(tmp, size);
482         irq = isapnp_alloc(sizeof(struct pnp_irq));
483         if (!irq)
484                 return;
485         irq->map = (tmp[1] << 8) | tmp[0];
486         if (size > 2)
487                 irq->flags = tmp[2];
488         else
489                 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
490         pnp_register_irq_resource(option, irq);
491         return;
492 }
493
494 /*
495  *  Add DMA resource to resources list.
496  */
497
498 static void __init isapnp_parse_dma_resource(struct pnp_option *option,
499                                                int size)
500 {
501         unsigned char tmp[2];
502         struct pnp_dma *dma;
503
504         isapnp_peek(tmp, size);
505         dma = isapnp_alloc(sizeof(struct pnp_dma));
506         if (!dma)
507                 return;
508         dma->map = tmp[0];
509         dma->flags = tmp[1];
510         pnp_register_dma_resource(option, dma);
511         return;
512 }
513
514 /*
515  *  Add port resource to resources list.
516  */
517
518 static void __init isapnp_parse_port_resource(struct pnp_option *option,
519                                                 int size)
520 {
521         unsigned char tmp[7];
522         struct pnp_port *port;
523
524         isapnp_peek(tmp, size);
525         port = isapnp_alloc(sizeof(struct pnp_port));
526         if (!port)
527                 return;
528         port->min = (tmp[2] << 8) | tmp[1];
529         port->max = (tmp[4] << 8) | tmp[3];
530         port->align = tmp[5];
531         port->size = tmp[6];
532         port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
533         pnp_register_port_resource(option,port);
534         return;
535 }
536
537 /*
538  *  Add fixed port resource to resources list.
539  */
540
541 static void __init isapnp_parse_fixed_port_resource(struct pnp_option *option,
542                                                       int size)
543 {
544         unsigned char tmp[3];
545         struct pnp_port *port;
546
547         isapnp_peek(tmp, size);
548         port = isapnp_alloc(sizeof(struct pnp_port));
549         if (!port)
550                 return;
551         port->min = port->max = (tmp[1] << 8) | tmp[0];
552         port->size = tmp[2];
553         port->align = 0;
554         port->flags = PNP_PORT_FLAG_FIXED;
555         pnp_register_port_resource(option,port);
556         return;
557 }
558
559 /*
560  *  Add memory resource to resources list.
561  */
562
563 static void __init isapnp_parse_mem_resource(struct pnp_option *option,
564                                                int size)
565 {
566         unsigned char tmp[9];
567         struct pnp_mem *mem;
568
569         isapnp_peek(tmp, size);
570         mem = isapnp_alloc(sizeof(struct pnp_mem));
571         if (!mem)
572                 return;
573         mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
574         mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
575         mem->align = (tmp[6] << 8) | tmp[5];
576         mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
577         mem->flags = tmp[0];
578         pnp_register_mem_resource(option,mem);
579         return;
580 }
581
582 /*
583  *  Add 32-bit memory resource to resources list.
584  */
585
586 static void __init isapnp_parse_mem32_resource(struct pnp_option *option,
587                                                  int size)
588 {
589         unsigned char tmp[17];
590         struct pnp_mem *mem;
591
592         isapnp_peek(tmp, size);
593         mem = isapnp_alloc(sizeof(struct pnp_mem));
594         if (!mem)
595                 return;
596         mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
597         mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
598         mem->align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
599         mem->size = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
600         mem->flags = tmp[0];
601         pnp_register_mem_resource(option,mem);
602 }
603
604 /*
605  *  Add 32-bit fixed memory resource to resources list.
606  */
607
608 static void __init isapnp_parse_fixed_mem32_resource(struct pnp_option *option,
609                                                        int size)
610 {
611         unsigned char tmp[9];
612         struct pnp_mem *mem;
613
614         isapnp_peek(tmp, size);
615         mem = isapnp_alloc(sizeof(struct pnp_mem));
616         if (!mem)
617                 return;
618         mem->min = mem->max = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
619         mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
620         mem->align = 0;
621         mem->flags = tmp[0];
622         pnp_register_mem_resource(option,mem);
623 }
624
625 /*
626  *  Parse card name for ISA PnP device.
627  */ 
628
629 static void __init
630 isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
631 {
632         if (name[0] == '\0') {
633                 unsigned short size1 = *size >= name_max ? (name_max - 1) : *size;
634                 isapnp_peek(name, size1);
635                 name[size1] = '\0';
636                 *size -= size1;
637
638                 /* clean whitespace from end of string */
639                 while (size1 > 0  &&  name[--size1] == ' ')
640                         name[size1] = '\0';
641         }
642 }
643
644 /*
645  *  Parse resource map for logical device.
646  */
647
648 static int __init isapnp_create_device(struct pnp_card *card,
649                                        unsigned short size)
650 {
651         int number = 0, skip = 0, priority = 0, compat = 0;
652         unsigned char type, tmp[17];
653         struct pnp_option *option;
654         struct pnp_dev *dev;
655         if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
656                 return 1;
657         option = pnp_register_independent_option(dev);
658         if (!option)
659                 return 1;
660         pnp_add_card_device(card,dev);
661
662         while (1) {
663                 if (isapnp_read_tag(&type, &size)<0)
664                         return 1;
665                 if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
666                         goto __skip;
667                 switch (type) {
668                 case _STAG_LOGDEVID:
669                         if (size >= 5 && size <= 6) {
670                                 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
671                                         return 1;
672                                 size = 0;
673                                 skip = 0;
674                                 option = pnp_register_independent_option(dev);
675                                 if (!option)
676                                         return 1;
677                                 pnp_add_card_device(card,dev);
678                         } else {
679                                 skip = 1;
680                         }
681                         priority = 0;
682                         compat = 0;
683                         break;
684                 case _STAG_COMPATDEVID:
685                         if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
686                                 isapnp_peek(tmp, 4);
687                                 isapnp_parse_id(dev,(tmp[1] << 8) | tmp[0], (tmp[3] << 8) | tmp[2]);
688                                 compat++;
689                                 size = 0;
690                         }
691                         break;
692                 case _STAG_IRQ:
693                         if (size < 2 || size > 3)
694                                 goto __skip;
695                         isapnp_parse_irq_resource(option, size);
696                         size = 0;
697                         break;
698                 case _STAG_DMA:
699                         if (size != 2)
700                                 goto __skip;
701                         isapnp_parse_dma_resource(option, size);
702                         size = 0;
703                         break;
704                 case _STAG_STARTDEP:
705                         if (size > 1)
706                                 goto __skip;
707                         priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
708                         if (size > 0) {
709                                 isapnp_peek(tmp, size);
710                                 priority = 0x100 | tmp[0];
711                                 size = 0;
712                         }
713                         option = pnp_register_dependent_option(dev,priority);
714                         if (!option)
715                                 return 1;
716                         break;
717                 case _STAG_ENDDEP:
718                         if (size != 0)
719                                 goto __skip;
720                         priority = 0;
721                         break;
722                 case _STAG_IOPORT:
723                         if (size != 7)
724                                 goto __skip;
725                         isapnp_parse_port_resource(option, size);
726                         size = 0;
727                         break;
728                 case _STAG_FIXEDIO:
729                         if (size != 3)
730                                 goto __skip;
731                         isapnp_parse_fixed_port_resource(option, size);
732                         size = 0;
733                         break;
734                 case _STAG_VENDOR:
735                         break;
736                 case _LTAG_MEMRANGE:
737                         if (size != 9)
738                                 goto __skip;
739                         isapnp_parse_mem_resource(option, size);
740                         size = 0;
741                         break;
742                 case _LTAG_ANSISTR:
743                         isapnp_parse_name(dev->name, sizeof(dev->name), &size);
744                         break;
745                 case _LTAG_UNICODESTR:
746                         /* silently ignore */
747                         /* who use unicode for hardware identification? */
748                         break;
749                 case _LTAG_VENDOR:
750                         break;
751                 case _LTAG_MEM32RANGE:
752                         if (size != 17)
753                                 goto __skip;
754                         isapnp_parse_mem32_resource(option, size);
755                         size = 0;
756                         break;
757                 case _LTAG_FIXEDMEM32RANGE:
758                         if (size != 9)
759                                 goto __skip;
760                         isapnp_parse_fixed_mem32_resource(option, size);
761                         size = 0;
762                         break;
763                 case _STAG_END:
764                         if (size > 0)
765                                 isapnp_skip_bytes(size);
766                         return 1;
767                 default:
768                         printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->number, card->number);
769                 }
770               __skip:
771                 if (size > 0)
772                         isapnp_skip_bytes(size);
773         }
774         return 0;
775 }
776
777 /*
778  *  Parse resource map for ISA PnP card.
779  */
780
781 static void __init isapnp_parse_resource_map(struct pnp_card *card)
782 {
783         unsigned char type, tmp[17];
784         unsigned short size;
785
786         while (1) {
787                 if (isapnp_read_tag(&type, &size)<0)
788                         return;
789                 switch (type) {
790                 case _STAG_PNPVERNO:
791                         if (size != 2)
792                                 goto __skip;
793                         isapnp_peek(tmp, 2);
794                         card->pnpver = tmp[0];
795                         card->productver = tmp[1];
796                         size = 0;
797                         break;
798                 case _STAG_LOGDEVID:
799                         if (size >= 5 && size <= 6) {
800                                 if (isapnp_create_device(card, size)==1)
801                                         return;
802                                 size = 0;
803                         }
804                         break;
805                 case _STAG_VENDOR:
806                         break;
807                 case _LTAG_ANSISTR:
808                         isapnp_parse_name(card->name, sizeof(card->name), &size);
809                         break;
810                 case _LTAG_UNICODESTR:
811                         /* silently ignore */
812                         /* who use unicode for hardware identification? */
813                         break;
814                 case _LTAG_VENDOR:
815                         break;
816                 case _STAG_END:
817                         if (size > 0)
818                                 isapnp_skip_bytes(size);
819                         return;
820                 default:
821                         printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
822                 }
823               __skip:
824                 if (size > 0)
825                         isapnp_skip_bytes(size);
826         }
827 }
828
829 /*
830  *  Compute ISA PnP checksum for first eight bytes.
831  */
832
833 static unsigned char __init isapnp_checksum(unsigned char *data)
834 {
835         int i, j;
836         unsigned char checksum = 0x6a, bit, b;
837
838         for (i = 0; i < 8; i++) {
839                 b = data[i];
840                 for (j = 0; j < 8; j++) {
841                         bit = 0;
842                         if (b & (1 << j))
843                                 bit = 1;
844                         checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
845                 }
846         }
847         return checksum;
848 }
849
850 /*
851  *  Parse EISA id for ISA PnP card.
852  */
853
854 static void isapnp_parse_card_id(struct pnp_card * card, unsigned short vendor, unsigned short device)
855 {
856         struct pnp_id * id = isapnp_alloc(sizeof(struct pnp_id));
857         if (!id)
858                 return;
859         sprintf(id->id, "%c%c%c%x%x%x%x",
860                         'A' + ((vendor >> 2) & 0x3f) - 1,
861                         'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
862                         'A' + ((vendor >> 8) & 0x1f) - 1,
863                         (device >> 4) & 0x0f,
864                         device & 0x0f,
865                         (device >> 12) & 0x0f,
866                         (device >> 8) & 0x0f);
867         pnp_add_card_id(id,card);
868 }
869
870 /*
871  *  Build device list for all present ISA PnP devices.
872  */
873
874 static int __init isapnp_build_device_list(void)
875 {
876         int csn;
877         unsigned char header[9], checksum;
878         struct pnp_card *card;
879
880         isapnp_wait();
881         isapnp_key();
882         for (csn = 1; csn <= isapnp_csn_count; csn++) {
883                 isapnp_wake(csn);
884                 isapnp_peek(header, 9);
885                 checksum = isapnp_checksum(header);
886 #if 0
887                 printk(KERN_DEBUG "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
888                         header[0], header[1], header[2], header[3],
889                         header[4], header[5], header[6], header[7], header[8]);
890                 printk(KERN_DEBUG "checksum = 0x%x\n", checksum);
891 #endif
892                 if ((card = isapnp_alloc(sizeof(struct pnp_card))) == NULL)
893                         continue;
894
895                 card->number = csn;
896                 INIT_LIST_HEAD(&card->devices);
897                 isapnp_parse_card_id(card, (header[1] << 8) | header[0], (header[3] << 8) | header[2]);
898                 card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
899                 isapnp_checksum_value = 0x00;
900                 isapnp_parse_resource_map(card);
901                 if (isapnp_checksum_value != 0x00)
902                         printk(KERN_ERR "isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
903                 card->checksum = isapnp_checksum_value;
904                 card->protocol = &isapnp_protocol;
905
906                 pnp_add_card(card);
907         }
908         isapnp_wait();
909         return 0;
910 }
911
912 /*
913  *  Basic configuration routines.
914  */
915
916 int isapnp_present(void)
917 {
918         struct pnp_card *card;
919         pnp_for_each_card(card) {
920                 if (card->protocol == &isapnp_protocol)
921                         return 1;
922         }
923         return 0;
924 }
925
926 int isapnp_cfg_begin(int csn, int logdev)
927 {
928         if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
929                 return -EINVAL;
930         down(&isapnp_cfg_mutex);
931         isapnp_wait();
932         isapnp_key();
933         isapnp_wake(csn);
934 #if 0
935         /* to avoid malfunction when the isapnptools package is used */
936         /* we must set RDP to our value again */
937         /* it is possible to set RDP only in the isolation phase */
938         /*   Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
939         isapnp_write_byte(0x02, 0x04);  /* clear CSN of card */
940         mdelay(2);                      /* is this necessary? */
941         isapnp_wake(csn);               /* bring card into sleep state */
942         isapnp_wake(0);                 /* bring card into isolation state */
943         isapnp_set_rdp();               /* reset the RDP port */
944         udelay(1000);                   /* delay 1000us */
945         isapnp_write_byte(0x06, csn);   /* reset CSN to previous value */
946         udelay(250);                    /* is this necessary? */
947 #endif
948         if (logdev >= 0)
949                 isapnp_device(logdev);
950         return 0;
951 }
952
953 int isapnp_cfg_end(void)
954 {
955         isapnp_wait();
956         up(&isapnp_cfg_mutex);
957         return 0;
958 }
959
960
961 /*
962  *  Inititialization.
963  */
964
965
966 EXPORT_SYMBOL(isapnp_protocol);
967 EXPORT_SYMBOL(isapnp_present);
968 EXPORT_SYMBOL(isapnp_cfg_begin);
969 EXPORT_SYMBOL(isapnp_cfg_end);
970 EXPORT_SYMBOL(isapnp_read_byte);
971 EXPORT_SYMBOL(isapnp_read_word);
972 EXPORT_SYMBOL(isapnp_read_dword);
973 EXPORT_SYMBOL(isapnp_write_byte);
974 EXPORT_SYMBOL(isapnp_write_word);
975 EXPORT_SYMBOL(isapnp_write_dword);
976 EXPORT_SYMBOL(isapnp_wake);
977 EXPORT_SYMBOL(isapnp_device);
978
979 static int isapnp_read_resources(struct pnp_dev *dev, struct pnp_resource_table *res)
980 {
981         int tmp, ret;
982
983         dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
984         if (dev->active) {
985                 for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
986                         ret = isapnp_read_word(ISAPNP_CFG_PORT + (tmp << 1));
987                         if (!ret)
988                                 continue;
989                         res->port_resource[tmp].start = ret;
990                         res->port_resource[tmp].flags = IORESOURCE_IO;
991                 }
992                 for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
993                         ret = isapnp_read_word(ISAPNP_CFG_MEM + (tmp << 3)) << 8;
994                         if (!ret)
995                                 continue;
996                         res->mem_resource[tmp].start = ret;
997                         res->mem_resource[tmp].flags = IORESOURCE_MEM;
998                 }
999                 for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
1000                         ret = (isapnp_read_word(ISAPNP_CFG_IRQ + (tmp << 1)) >> 8);
1001                         if (!ret)
1002                                 continue;
1003                         res->irq_resource[tmp].start = res->irq_resource[tmp].end = ret;
1004                         res->irq_resource[tmp].flags = IORESOURCE_IRQ;
1005                 }
1006                 for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
1007                         ret = isapnp_read_byte(ISAPNP_CFG_DMA + tmp);
1008                         if (ret == 4)
1009                                 continue;
1010                         res->dma_resource[tmp].start = res->dma_resource[tmp].end = ret;
1011                         res->dma_resource[tmp].flags = IORESOURCE_DMA;
1012                 }
1013         }
1014         return 0;
1015 }
1016
1017 static int isapnp_get_resources(struct pnp_dev *dev, struct pnp_resource_table * res)
1018 {
1019         int ret;
1020         pnp_init_resource_table(res);
1021         isapnp_cfg_begin(dev->card->number, dev->number);
1022         ret = isapnp_read_resources(dev, res);
1023         isapnp_cfg_end();
1024         return ret;
1025 }
1026
1027 static int isapnp_set_resources(struct pnp_dev *dev, struct pnp_resource_table * res)
1028 {
1029         int tmp;
1030
1031         isapnp_cfg_begin(dev->card->number, dev->number);
1032         dev->active = 1;
1033         for (tmp = 0; tmp < PNP_MAX_PORT && (res->port_resource[tmp].flags & (IORESOURCE_IO | IORESOURCE_UNSET)) == IORESOURCE_IO; tmp++)
1034                 isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), res->port_resource[tmp].start);
1035         for (tmp = 0; tmp < PNP_MAX_IRQ && (res->irq_resource[tmp].flags & (IORESOURCE_IRQ | IORESOURCE_UNSET)) == IORESOURCE_IRQ; tmp++) {
1036                 int irq = res->irq_resource[tmp].start;
1037                 if (irq == 2)
1038                         irq = 9;
1039                 isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
1040         }
1041         for (tmp = 0; tmp < PNP_MAX_DMA && (res->dma_resource[tmp].flags & (IORESOURCE_DMA | IORESOURCE_UNSET)) == IORESOURCE_DMA; tmp++)
1042                 isapnp_write_byte(ISAPNP_CFG_DMA+tmp, res->dma_resource[tmp].start);
1043         for (tmp = 0; tmp < PNP_MAX_MEM && (res->mem_resource[tmp].flags & (IORESOURCE_MEM | IORESOURCE_UNSET)) == IORESOURCE_MEM; tmp++)
1044                 isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<3), (res->mem_resource[tmp].start >> 8) & 0xffff);
1045         /* FIXME: We aren't handling 32bit mems properly here */
1046         isapnp_activate(dev->number);
1047         isapnp_cfg_end();
1048         return 0;
1049 }
1050
1051 static int isapnp_disable_resources(struct pnp_dev *dev)
1052 {
1053         if (!dev || !dev->active)
1054                 return -EINVAL;
1055         isapnp_cfg_begin(dev->card->number, dev->number);
1056         isapnp_deactivate(dev->number);
1057         dev->active = 0;
1058         isapnp_cfg_end();
1059         return 0;
1060 }
1061
1062 struct pnp_protocol isapnp_protocol = {
1063         .name   = "ISA Plug and Play",
1064         .get    = isapnp_get_resources,
1065         .set    = isapnp_set_resources,
1066         .disable = isapnp_disable_resources,
1067 };
1068
1069 int __init isapnp_init(void)
1070 {
1071         int cards;
1072         struct pnp_card *card;
1073         struct pnp_dev *dev;
1074
1075         if (isapnp_disable) {
1076                 isapnp_detected = 0;
1077                 printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
1078                 return 0;
1079         }
1080 #ifdef ISAPNP_REGION_OK
1081         if (!request_region(_PIDXR, 1, "isapnp index")) {
1082                 printk(KERN_ERR "isapnp: Index Register 0x%x already used\n", _PIDXR);
1083                 return -EBUSY;
1084         }
1085 #endif
1086         if (!request_region(_PNPWRP, 1, "isapnp write")) {
1087                 printk(KERN_ERR "isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
1088 #ifdef ISAPNP_REGION_OK
1089                 release_region(_PIDXR, 1);
1090 #endif
1091                 return -EBUSY;
1092         }
1093
1094         if(pnp_register_protocol(&isapnp_protocol)<0)
1095                 return -EBUSY;
1096
1097         /*
1098          *      Print a message. The existing ISAPnP code is hanging machines
1099          *      so let the user know where.
1100          */
1101          
1102         printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
1103         if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
1104                 isapnp_rdp |= 3;
1105                 if (!request_region(isapnp_rdp, 1, "isapnp read")) {
1106                         printk(KERN_ERR "isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
1107 #ifdef ISAPNP_REGION_OK
1108                         release_region(_PIDXR, 1);
1109 #endif
1110                         release_region(_PNPWRP, 1);
1111                         return -EBUSY;
1112                 }
1113                 isapnp_set_rdp();
1114         }
1115         isapnp_detected = 1;
1116         if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
1117                 cards = isapnp_isolate();
1118                 if (cards < 0 || 
1119                     (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
1120 #ifdef ISAPNP_REGION_OK
1121                         release_region(_PIDXR, 1);
1122 #endif
1123                         release_region(_PNPWRP, 1);
1124                         isapnp_detected = 0;
1125                         printk(KERN_INFO "isapnp: No Plug & Play device found\n");
1126                         return 0;
1127                 }
1128                 request_region(isapnp_rdp, 1, "isapnp read");
1129         }
1130         isapnp_build_device_list();
1131         cards = 0;
1132
1133         protocol_for_each_card(&isapnp_protocol,card) {
1134                 cards++;
1135                 if (isapnp_verbose) {
1136                         printk(KERN_INFO "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
1137                         if (isapnp_verbose < 2)
1138                                 continue;
1139                         card_for_each_dev(card,dev) {
1140                                 printk(KERN_INFO "isapnp:   Device '%s'\n", dev->name[0]?dev->name:"Unknown");
1141                         }
1142                 }
1143         }
1144         if (cards) {
1145                 printk(KERN_INFO "isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
1146         } else {
1147                 printk(KERN_INFO "isapnp: No Plug & Play card found\n");
1148         }
1149
1150         isapnp_proc_init();
1151         return 0;
1152 }
1153
1154 device_initcall(isapnp_init);
1155
1156 /* format is: noisapnp */
1157
1158 static int __init isapnp_setup_disable(char *str)
1159 {
1160         isapnp_disable = 1;
1161         return 1;
1162 }
1163
1164 __setup("noisapnp", isapnp_setup_disable);
1165
1166 /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
1167
1168 static int __init isapnp_setup_isapnp(char *str)
1169 {
1170         (void)((get_option(&str,&isapnp_rdp) == 2) &&
1171                (get_option(&str,&isapnp_reset) == 2) &&
1172                (get_option(&str,&isapnp_verbose) == 2));
1173         return 1;
1174 }
1175
1176 __setup("isapnp=", isapnp_setup_isapnp);
1177