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