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