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