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