Remove unused variable.
[dragonfly.git] / sys / bus / isa / pnp.c
1 /*
2  * Copyright (c) 1996, Sujal M. Patel
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/isa/pnp.c,v 1.5.2.1 2002/10/14 09:31:09 nyan Exp $
27  *      $DragonFly: src/sys/bus/isa/pnp.c,v 1.14 2008/08/10 18:57:03 swildner Exp $
28  *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include "isavar.h"
38 #include "pnpreg.h"
39 #include "pnpvar.h"
40 #include <machine/clock.h>
41
42 typedef struct _pnp_id {
43         u_int32_t vendor_id;
44         u_int32_t serial;
45         u_char checksum;
46 } pnp_id;
47
48 struct pnp_set_config_arg {
49         int     csn;            /* Card number to configure */
50         int     ldn;            /* Logical device on card */
51 };
52
53 struct pnp_quirk {
54         u_int32_t vendor_id;    /* Vendor of the card */
55         u_int32_t logical_id;   /* ID of the device with quirk */
56         int     type;
57         int     arg1;
58         int     arg2;
59 };
60
61 #define PNP_QUIRK_WRITE_REG     1 /* Need to write a pnp register  */
62 #define PNP_QUIRK_EXTRA_IO      2 /* Has extra io ports */
63
64 struct pnp_quirk pnp_quirks[] = {
65         /*
66          * The Gravis UltraSound needs register 0xf2 to be set to 0xff
67          * to enable power.
68          * XXX need to know the logical device id.
69          */
70         { 0x0100561e /* GRV0001 */,     0,
71           PNP_QUIRK_WRITE_REG,  0xf2,    0xff },
72         /*
73          * An emu8000 does not give us other than the first
74          * port.
75          */
76         { 0x0100561e /* GRV0001 */,     0,
77           PNP_QUIRK_WRITE_REG,  0xf2,    0xff },
78         /*
79          * An emu8000 does not give us other than the first
80          * port.
81          */
82         { 0x26008c0e /* SB16 */,        0x21008c0e,
83           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
84         { 0x42008c0e /* SB32(CTL0042) */,       0x21008c0e,
85           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
86         { 0x44008c0e /* SB32(CTL0044) */,       0x21008c0e,
87           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
88         { 0x49008c0e /* SB32(CTL0049) */,       0x21008c0e,
89           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
90         { 0xf1008c0e /* SB32(CTL00f1) */,       0x21008c0e,
91           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
92         { 0xc1008c0e /* SB64(CTL00c1) */,       0x22008c0e,
93           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
94         { 0xc5008c0e /* SB64(CTL00c5) */,       0x22008c0e,
95           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
96         { 0xe4008c0e /* SB64(CTL00e4) */,       0x22008c0e,
97           PNP_QUIRK_EXTRA_IO,   0x400,   0x800 },
98
99         { 0 }
100 };
101
102 #if 0
103 /*
104  * these entries are initialized using the autoconfig menu
105  * The struct is invalid (and must be initialized) if the first
106  * CSN is zero. The init code fills invalid entries with CSN 255
107  * which is not a supported value.
108  */
109
110 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
111     { 0 }
112 };
113 #endif
114
115 /* The READ_DATA port that we are using currently */
116 static int pnp_rd_port;
117
118 static void   pnp_send_initiation_key(void);
119 static int    pnp_get_serial(pnp_id *p);
120 static int    pnp_isolation_protocol(device_t parent);
121
122 char *
123 pnp_eisaformat(u_int32_t id)
124 {
125         u_int8_t *data = (u_int8_t *) &id;
126         static char idbuf[8];
127         const char  hextoascii[] = "0123456789abcdef";
128
129         idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
130         idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
131         idbuf[2] = '@' + (data[1] & 0x1f);
132         idbuf[3] = hextoascii[(data[2] >> 4)];
133         idbuf[4] = hextoascii[(data[2] & 0xf)];
134         idbuf[5] = hextoascii[(data[3] >> 4)];
135         idbuf[6] = hextoascii[(data[3] & 0xf)];
136         idbuf[7] = 0;
137         return(idbuf);
138 }
139
140 static void
141 pnp_write(int d, u_char r)
142 {
143         outb (_PNP_ADDRESS, d);
144         outb (_PNP_WRITE_DATA, r);
145 }
146
147 #if 0
148
149 static u_char
150 pnp_read(int d)
151 {
152         outb (_PNP_ADDRESS, d);
153         return (inb(3 | (pnp_rd_port <<2)));
154 }
155
156 #endif
157
158 /*
159  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
160  * Intel May 94.
161  */
162 static void
163 pnp_send_initiation_key(void)
164 {
165         int cur, i;
166
167         /* Reset the LSFR */
168         outb(_PNP_ADDRESS, 0);
169         outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
170
171         cur = 0x6a;
172         outb(_PNP_ADDRESS, cur);
173
174         for (i = 1; i < 32; i++) {
175                 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
176                 outb(_PNP_ADDRESS, cur);
177         }
178 }
179
180
181 /*
182  * Get the device's serial number.  Returns 1 if the serial is valid.
183  */
184 static int
185 pnp_get_serial(pnp_id *p)
186 {
187         int i, bit, valid = 0, sum = 0x6a;
188         u_char *data = (u_char *)p;
189
190         bzero(data, sizeof(char) * 9);
191         outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
192         for (i = 0; i < 72; i++) {
193                 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
194                 DELAY(250);     /* Delay 250 usec */
195
196                 /* Can't Short Circuit the next evaluation, so 'and' is last */
197                 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
198                 DELAY(250);     /* Delay 250 usec */
199
200                 valid = valid || bit;
201
202                 if (i < 64)
203                         sum = (sum >> 1) |
204                                 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
205
206                 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
207         }
208
209         valid = valid && (data[8] == sum);
210
211         return valid;
212 }
213
214 /*
215  * Fill's the buffer with resource info from the device.
216  * Returns the number of characters read.
217  */
218 static int
219 pnp_get_resource_info(u_char *buffer, int len)
220 {
221         int i, j, count;
222         u_char temp;
223
224         count = 0;
225         for (i = 0; i < len; i++) {
226                 outb(_PNP_ADDRESS, PNP_STATUS);
227                 for (j = 0; j < 100; j++) {
228                         if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
229                                 break;
230                         DELAY(1);
231                 }
232                 if (j == 100) {
233                         kprintf("PnP device failed to report resource data\n");
234                         return count;
235                 }
236                 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
237                 temp = inb((pnp_rd_port << 2) | 0x3);
238                 if (buffer != NULL)
239                         buffer[i] = temp;
240                 count++;
241         }
242         return count;
243 }
244
245 #if 0
246 /*
247  * write_pnp_parms initializes a logical device with the parms
248  * in d, and then activates the board if the last parameter is 1.
249  */
250
251 static int
252 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
253 {
254     int i, empty = -1 ;
255
256     pnp_write (SET_LDN, ldn );
257     i = pnp_read(SET_LDN) ;
258     if (i != ldn) {
259         kprintf("Warning: LDN %d does not exist\n", ldn);
260     }
261     for (i = 0; i < 8; i++) {
262         pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
263         pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
264     }
265     for (i = 0; i < 4; i++) {
266         pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
267         pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
268         pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
269         pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
270         pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
271     }
272     for (i = 0; i < 2; i++) {
273         pnp_write(IRQ_CONFIG + i*2    , d->irq[i] );
274         pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
275         pnp_write(DRQ_CONFIG + i, d->drq[i] );
276     }
277     /*
278      * store parameters read into the current kernel
279      * so manual editing next time is easier
280      */
281     for (i = 0 ; i < MAX_PNP_LDN; i++) {
282         if (pnp_ldn_overrides[i].csn == d->csn &&
283                 pnp_ldn_overrides[i].ldn == ldn) {
284             d->flags = pnp_ldn_overrides[i].flags ;
285             pnp_ldn_overrides[i] = *d ;
286             break ;
287         } else if (pnp_ldn_overrides[i].csn < 1 ||
288                 pnp_ldn_overrides[i].csn == 255)
289             empty = i ;
290     }
291     if (i== MAX_PNP_LDN && empty != -1)
292         pnp_ldn_overrides[empty] = *d;
293
294     /*
295      * Here should really perform the range check, and
296      * return a failure if not successful.
297      */
298     pnp_write (IO_RANGE_CHECK, 0);
299     DELAY(1000); /* XXX is it really necessary ? */
300     pnp_write (ACTIVATE, d->enable ? 1 : 0);
301     DELAY(1000); /* XXX is it really necessary ? */
302     return 1 ;
303 }
304 #endif
305
306 /*
307  * This function is called after the bus has assigned resource
308  * locations for a logical device.
309  */
310 static void
311 pnp_set_config(void *arg, struct isa_config *config, int enable)
312 {
313         int csn = ((struct pnp_set_config_arg *) arg)->csn;
314         int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
315         int i;
316
317         /*
318          * First put all cards into Sleep state with the initiation
319          * key, then put our card into Config state.
320          */
321         pnp_send_initiation_key();
322         pnp_write(PNP_WAKE, csn);
323
324         /*
325          * Select our logical device so that we can program it.
326          */
327         pnp_write(PNP_SET_LDN, ldn);
328
329         /*
330          * Now program the resources.
331          */
332         for (i = 0; i < config->ic_nmem; i++) {
333                 u_int32_t start = config->ic_mem[i].ir_start;
334                 u_int32_t size =  config->ic_mem[i].ir_size;
335                 if (start & 0xff)
336                         panic("pnp_set_config: bogus memory assignment");
337                 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
338                 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
339                 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
340                 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
341         }
342         for (; i < ISA_NMEM; i++) {
343                 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
344                 pnp_write(PNP_MEM_BASE_LOW(i), 0);
345                 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
346                 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
347         }
348
349         for (i = 0; i < config->ic_nport; i++) {
350                 u_int32_t start = config->ic_port[i].ir_start;
351                 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
352                 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
353         }
354         for (; i < ISA_NPORT; i++) {
355                 pnp_write(PNP_IO_BASE_HIGH(i), 0);
356                 pnp_write(PNP_IO_BASE_LOW(i), 0);
357         }
358
359         for (i = 0; i < config->ic_nirq; i++) {
360                 int irq = ffs(config->ic_irqmask[i]) - 1;
361                 pnp_write(PNP_IRQ_LEVEL(i), irq);
362                 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
363         }
364         for (; i < ISA_NIRQ; i++) {
365                 /*
366                  * IRQ 0 is not a valid interrupt selection and
367                  * represents no interrupt selection.
368                  */
369                 pnp_write(PNP_IRQ_LEVEL(i), 0);
370         }               
371
372         for (i = 0; i < config->ic_ndrq; i++) {
373                 int drq = ffs(config->ic_drqmask[i]) - 1;
374                 pnp_write(PNP_DMA_CHANNEL(i), drq);
375         }
376         for (; i < ISA_NDRQ; i++) {
377                 /*
378                  * DMA channel 4, the cascade channel is used to
379                  * indicate no DMA channel is active.
380                  */
381                 pnp_write(PNP_DMA_CHANNEL(i), 4);
382         }               
383
384         pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
385
386         /*
387          * Wake everyone up again, we are finished.
388          */
389         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
390 }
391
392 /*
393  * Process quirks for a logical device.. The card must be in Config state.
394  */
395 void
396 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id,
397     int ldn, struct isa_config *config)
398 {
399         struct pnp_quirk *qp;
400
401         for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
402                 if (qp->vendor_id == vendor_id
403                     && (qp->logical_id == 0
404                         || qp->logical_id == logical_id)) {
405                         switch (qp->type) {
406                         case PNP_QUIRK_WRITE_REG:
407                                 pnp_write(PNP_SET_LDN, ldn);
408                                 pnp_write(qp->arg1, qp->arg2);
409                                 break;
410                         case PNP_QUIRK_EXTRA_IO:
411                                 if (config == NULL)
412                                         break;
413                                 if (qp->arg1 != 0) {
414                                         config->ic_nport++;
415                                         config->ic_port[config->ic_nport - 1] = config->ic_port[0];
416                                         config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
417                                         config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
418                                 }
419                                 if (qp->arg2 != 0) {
420                                         config->ic_nport++;
421                                         config->ic_port[config->ic_nport - 1] = config->ic_port[0];
422                                         config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
423                                         config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
424                                 }
425                                 break;
426
427                         }
428                 }
429         }
430 }
431
432 /*
433  * Scan Resource Data for Logical Devices.
434  *
435  * This function exits as soon as it gets an error reading *ANY*
436  * Resource Data or it reaches the end of Resource Data.  In the first
437  * case the return value will be TRUE, FALSE otherwise.
438  */
439 static int
440 pnp_create_devices(device_t parent, pnp_id *p, int csn,
441                    u_char *resources, int len)
442 {
443         u_char tag, *resp, *resinfo, *startres = 0;
444         int large_len, scanning = len, retval = FALSE;
445         u_int32_t logical_id;
446         device_t dev = 0;
447         int ldn = 0;
448         struct pnp_set_config_arg *csnldn;
449         char buf[100];
450         char *desc = 0;
451
452         resp = resources;
453         while (scanning > 0) {
454                 tag = *resp++;
455                 scanning--;
456                 if (PNP_RES_TYPE(tag) != 0) {
457                         /* Large resource */
458                         if (scanning < 2) {
459                                 scanning = 0;
460                                 continue;
461                         }
462                         large_len = resp[0] + (resp[1] << 8);
463                         resp += 2;
464
465                         if (scanning < large_len) {
466                                 scanning = 0;
467                                 continue;
468                         }
469                         resinfo = resp;
470                         resp += large_len;
471                         scanning -= large_len;
472
473                         if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
474                                 if (large_len > sizeof(buf) - 1)
475                                         large_len = sizeof(buf) - 1;
476                                 bcopy(resinfo, buf, large_len);
477
478                                 /*
479                                  * Trim trailing spaces.
480                                  */
481                                 while (buf[large_len-1] == ' ')
482                                         large_len--;
483                                 buf[large_len] = '\0';
484                                 desc = buf;
485                                 if (dev)
486                                         device_set_desc_copy(dev, desc);
487                                 continue;
488                         }
489
490                         continue;
491                 }
492                 
493                 /* Small resource */
494                 if (scanning < PNP_SRES_LEN(tag)) {
495                         scanning = 0;
496                         continue;
497                 }
498                 resinfo = resp;
499                 resp += PNP_SRES_LEN(tag);
500                 scanning -= PNP_SRES_LEN(tag);
501                         
502                 switch (PNP_SRES_NUM(tag)) {
503                 case PNP_TAG_LOGICAL_DEVICE:
504                         /*
505                          * Parse the resources for the previous
506                          * logical device (if any).
507                          */
508                         if (startres) {
509                                 pnp_parse_resources(dev, startres,
510                                             resinfo - startres - 1, ldn);
511                                 dev = 0;
512                                 startres = 0;
513                         }
514
515                         /* 
516                          * A new logical device. Scan for end of
517                          * resources.
518                          */
519                         bcopy(resinfo, &logical_id, 4);
520                         pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
521                         dev = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP,
522                                             NULL, -1);
523                         if (desc)
524                                 device_set_desc_copy(dev, desc);
525                         isa_set_vendorid(dev, p->vendor_id);
526                         isa_set_serial(dev, p->serial);
527                         isa_set_logicalid(dev, logical_id);
528                         csnldn = kmalloc(sizeof *csnldn, M_DEVBUF, M_WAITOK);
529                         csnldn->csn = csn;
530                         csnldn->ldn = ldn;
531                         ISA_SET_CONFIG_CALLBACK(parent, dev,
532                                                 pnp_set_config, csnldn);
533                         ldn++;
534                         startres = resp;
535                         break;
536                     
537                 case PNP_TAG_END:
538                         if (!startres) {
539                                 device_printf(parent,
540                                               "malformed resources\n");
541                                 scanning = 0;
542                                 break;
543                         }
544                         pnp_parse_resources(dev, startres,
545                                             resinfo - startres - 1, ldn);
546                         dev = 0;
547                         startres = 0;
548                         scanning = 0;
549                         break;
550
551                 default:
552                         /* Skip this resource */
553                         break;
554                 }
555         }
556
557         return retval;
558 }
559
560 /*
561  * Read 'amount' bytes of resources from the card, allocating memory
562  * as needed. If a buffer is already available, it should be passed in
563  * '*resourcesp' and its length in '*spacep'. The number of resource
564  * bytes already in the buffer should be passed in '*lenp'. The memory
565  * allocated will be returned in '*resourcesp' with its size and the
566  * number of bytes of resources in '*spacep' and '*lenp' respectively.
567  */
568 static int
569 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
570 {
571         u_char *resources = *resourcesp;
572         u_char *newres;
573         int space = *spacep;
574         int len = *lenp;
575
576         if (space == 0) {
577                 space = 1024;
578                 resources = kmalloc(space, M_TEMP, M_WAITOK);
579         }
580         
581         if (len + amount > space) {
582                 int extra = 1024;
583                 while (len + amount > space + extra)
584                         extra += 1024;
585                 newres = kmalloc(space + extra, M_TEMP, M_WAITOK);
586                 bcopy(resources, newres, len);
587                 kfree(resources, M_TEMP);
588                 resources = newres;
589                 space += extra;
590         }
591
592         if (pnp_get_resource_info(resources + len, amount) != amount)
593                 return EINVAL;
594         len += amount;
595
596         *resourcesp = resources;
597         *spacep = space;
598         *lenp = len;
599
600         return 0;
601 }
602
603 /*
604  * Read all resources from the card, allocating memory as needed. If a
605  * buffer is already available, it should be passed in '*resourcesp'
606  * and its length in '*spacep'. The memory allocated will be returned
607  * in '*resourcesp' with its size and the number of bytes of resources
608  * in '*spacep' and '*lenp' respectively.
609  */
610 static int
611 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
612 {
613         u_char *resources = *resourcesp;
614         int space = *spacep;
615         int len = 0;
616         int error, done;
617         u_char tag;
618
619         error = 0;
620         done = 0;
621         while (!done) {
622                 error = pnp_read_bytes(1, &resources, &space, &len);
623                 if (error)
624                         goto out;
625                 tag = resources[len-1];
626                 if (PNP_RES_TYPE(tag) == 0) {
627                         /*
628                          * Small resource, read contents.
629                          */
630                         error = pnp_read_bytes(PNP_SRES_LEN(tag),
631                                                &resources, &space, &len);
632                         if (error)
633                                 goto out;
634                         if (PNP_SRES_NUM(tag) == PNP_TAG_END)
635                                 done = 1;
636                 } else {
637                         /*
638                          * Large resource, read length and contents.
639                          */
640                         error = pnp_read_bytes(2, &resources, &space, &len);
641                         if (error)
642                                 goto out;
643                         error = pnp_read_bytes(resources[len-2]
644                                                + (resources[len-1] << 8),
645                                                &resources, &space, &len);
646                         if (error)
647                                 goto out;
648                 }
649         }
650
651  out:
652         *resourcesp = resources;
653         *spacep = space;
654         *lenp = len;
655         return error;
656 }
657
658 /*
659  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
660  * value (caller should try multiple READ_DATA locations before giving
661  * up). Upon exiting, all cards are aware that they should use
662  * pnp_rd_port as the READ_DATA port.
663  *
664  * In the first pass, a csn is assigned to each board and pnp_id's
665  * are saved to an array, pnp_devices. In the second pass, each
666  * card is woken up and the device configuration is called.
667  */
668 static int
669 pnp_isolation_protocol(device_t parent)
670 {
671         int csn;
672         pnp_id id;
673         int found = 0, len;
674         u_char *resources = 0;
675         int space = 0;
676         int error;
677
678         /*
679          * Put all cards into the Sleep state so that we can clear
680          * their CSNs.
681          */
682         pnp_send_initiation_key();
683
684         /*
685          * Clear the CSN for all cards.
686          */
687         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
688
689         /*
690          * Move all cards to the Isolation state.
691          */
692         pnp_write(PNP_WAKE, 0);
693
694         /*
695          * Tell them where the read point is going to be this time.
696          */
697         pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
698
699         for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
700                 /*
701                  * Start the serial isolation protocol.
702                  */
703                 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
704                 DELAY(1000);    /* Delay 1 msec */
705
706                 if (pnp_get_serial(&id)) {
707                         /*
708                          * We have read the id from a card
709                          * successfully. The card which won the
710                          * isolation protocol will be in Isolation
711                          * mode and all others will be in Sleep.
712                          * Program the CSN of the isolated card
713                          * (taking it to Config state) and read its
714                          * resources, creating devices as we find
715                          * logical devices on the card.
716                          */
717                         pnp_write(PNP_SET_CSN, csn);
718
719                         error = pnp_read_resources(&resources,
720                                                    &space,
721                                                    &len);
722                         if (error)
723                                 break;
724                         pnp_create_devices(parent, &id, csn,
725                                            resources, len);
726                         found++;
727                 } else
728                         break;
729
730                 /*
731                  * Put this card back to the Sleep state and
732                  * simultaneously move all cards which don't have a
733                  * CSN yet to Isolation state.
734                  */
735                 pnp_write(PNP_WAKE, 0);
736         }
737
738         /*
739          * Unless we have chosen the wrong read port, all cards will
740          * be in Sleep state. Put them back into WaitForKey for
741          * now. Their resources will be programmed later.
742          */
743         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
744
745         /*
746          * Cleanup.
747          */
748         if (resources)
749                 kfree(resources, M_TEMP);
750
751         return found;
752 }
753
754
755 /*
756  * pnp_identify()
757  *
758  * autoconfiguration of pnp devices. This routine just runs the
759  * isolation protocol over several ports, until one is successful.
760  *
761  * may be called more than once ?
762  *
763  */
764 static int
765 pnp_identify(driver_t *driver, device_t parent)
766 {
767         int num_pnp_devs;
768
769         /*
770          * We do not support rescanning PNP devices, just return
771          * success (leave the previously scanned devices intact).
772          */
773         if (device_get_state(parent) == DS_ATTACHED)
774                 return (0);
775         if (device_get_state(parent) == DS_INPROGRESS)
776                 return (0);
777
778 #if 0
779         if (pnp_ldn_overrides[0].csn == 0) {
780                 if (bootverbose)
781                         kprintf("Initializing PnP override table\n");
782                 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
783                 pnp_ldn_overrides[0].csn = 255 ;
784         }
785 #endif
786
787         /* Try various READ_DATA ports from 0x203-0x3ff */
788         for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
789                 if (bootverbose)
790                         kprintf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
791
792                 num_pnp_devs = pnp_isolation_protocol(parent);
793                 if (num_pnp_devs)
794                         break;
795         }
796         return (num_pnp_devs ? 0 : ENXIO);
797 }
798
799 /*
800  * This causes pnp_identify() to be called for any attached ISA bus in
801  * the system.
802  */
803 static device_method_t pnp_methods[] = {
804         /* Device interface */
805         DEVMETHOD(device_identify,      pnp_identify),
806
807         { 0, 0 }
808 };
809
810 static driver_t pnp_driver = {
811         "pnp",
812         pnp_methods,
813         1,                      /* no softc */
814 };
815
816 static devclass_t pnp_devclass;
817
818 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);