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