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