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