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