isapnpres.c revision 1.20 1 /* $NetBSD: isapnpres.c,v 1.20 2009/03/14 15:36:18 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Resource parser for Plug and Play cards.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: isapnpres.c,v 1.20 2009/03/14 15:36:18 dsl Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43
44 #include <sys/bus.h>
45
46 #include <dev/isa/isavar.h>
47
48 #include <dev/isapnp/isapnpreg.h>
49 #include <dev/isapnp/isapnpvar.h>
50
51
52 static int isapnp_wait_status(struct isapnp_softc *);
53 static struct isapnp_attach_args *
54 isapnp_newdev(struct isapnp_attach_args *);
55 static struct isapnp_attach_args *
56 isapnp_newconf(struct isapnp_attach_args *);
57 static void isapnp_merge(struct isapnp_attach_args *,
58 const struct isapnp_attach_args *);
59 static struct isapnp_attach_args *
60 isapnp_flatten(struct isapnp_attach_args *);
61 static int isapnp_process_tag(u_char, u_char, u_char *,
62 struct isapnp_attach_args **, struct isapnp_attach_args **,
63 struct isapnp_attach_args **);
64
65
66 /* isapnp_wait_status():
67 * Wait for the next byte of resource data to become available
68 */
69 static int
70 isapnp_wait_status(struct isapnp_softc *sc)
71 {
72 int i;
73
74 /* wait up to 1 ms for each resource byte */
75 for (i = 0; i < 10; i++) {
76 if (isapnp_read_reg(sc, ISAPNP_STATUS) & 1)
77 return 0;
78 DELAY(100);
79 }
80 return 1;
81 }
82
83
84 /* isapnp_newdev():
85 * Add a new logical device to the current card; expand the configuration
86 * resources of the current card if needed.
87 */
88 static struct isapnp_attach_args *
89 isapnp_newdev(struct isapnp_attach_args *card)
90 {
91 struct isapnp_attach_args *ipa, *dev = ISAPNP_MALLOC(sizeof(*dev));
92
93 memset(dev, 0, sizeof(*dev));
94
95 dev->ipa_pref = ISAPNP_DEP_ACCEPTABLE;
96 memcpy(dev->ipa_devident, card->ipa_devident,
97 sizeof(card->ipa_devident));
98
99 if (card->ipa_child == NULL)
100 card->ipa_child = dev;
101 else {
102 for (ipa = card->ipa_child; ipa->ipa_sibling != NULL;
103 ipa = ipa->ipa_sibling)
104 continue;
105 ipa->ipa_sibling = dev;
106 }
107
108
109 return dev;
110 }
111
112
113 /* isapnp_newconf():
114 * Add a new alternate configuration to a logical device
115 */
116 static struct isapnp_attach_args *
117 isapnp_newconf(struct isapnp_attach_args *dev)
118 {
119 struct isapnp_attach_args *ipa, *conf = ISAPNP_MALLOC(sizeof(*conf));
120
121 memset(conf, 0, sizeof(*conf));
122
123 memcpy(conf->ipa_devident, dev->ipa_devident,
124 sizeof(conf->ipa_devident));
125 memcpy(conf->ipa_devlogic, dev->ipa_devlogic,
126 sizeof(conf->ipa_devlogic));
127 memcpy(conf->ipa_devcompat, dev->ipa_devcompat,
128 sizeof(conf->ipa_devcompat));
129 memcpy(conf->ipa_devclass, dev->ipa_devclass,
130 sizeof(conf->ipa_devclass));
131
132 if (dev->ipa_child == NULL)
133 dev->ipa_child = conf;
134 else {
135 for (ipa = dev->ipa_child; ipa->ipa_sibling;
136 ipa = ipa->ipa_sibling)
137 continue;
138 ipa->ipa_sibling = conf;
139 }
140
141 return conf;
142 }
143
144
145 /* isapnp_merge():
146 * Merge the common device configurations to the subconfigurations
147 */
148 static void
149 isapnp_merge(struct isapnp_attach_args *c, const struct isapnp_attach_args *d)
150 {
151 int i;
152
153 for (i = 0; i < d->ipa_nio; i++)
154 c->ipa_io[c->ipa_nio++] = d->ipa_io[i];
155
156 for (i = 0; i < d->ipa_nmem; i++)
157 c->ipa_mem[c->ipa_nmem++] = d->ipa_mem[i];
158
159 for (i = 0; i < d->ipa_nmem32; i++)
160 c->ipa_mem32[c->ipa_nmem32++] = d->ipa_mem32[i];
161
162 for (i = 0; i < d->ipa_nirq; i++)
163 c->ipa_irq[c->ipa_nirq++] = d->ipa_irq[i];
164
165 for (i = 0; i < d->ipa_ndrq; i++)
166 c->ipa_drq[c->ipa_ndrq++] = d->ipa_drq[i];
167 }
168
169
170 /* isapnp_flatten():
171 * Flatten the tree to a list of config entries.
172 */
173 static struct isapnp_attach_args *
174 isapnp_flatten(struct isapnp_attach_args *card)
175 {
176 struct isapnp_attach_args *dev, *conf, *d, *c, *pa;
177
178 dev = card->ipa_child;
179 ISAPNP_FREE(card);
180
181 for (conf = c = NULL, d = dev; d; d = dev) {
182 dev = d->ipa_sibling;
183 if (d->ipa_child == NULL) {
184 /*
185 * No subconfigurations; all configuration info
186 * is in the device node.
187 */
188 d->ipa_sibling = NULL;
189 pa = d;
190 }
191 else {
192 /*
193 * Push down device configuration info to the
194 * subconfigurations
195 */
196 for (pa = d->ipa_child; pa; pa = pa->ipa_sibling)
197 isapnp_merge(pa, d);
198
199 pa = d->ipa_child;
200 ISAPNP_FREE(d);
201 }
202
203 if (c == NULL)
204 c = conf = pa;
205 else
206 c->ipa_sibling = pa;
207
208 while (c->ipa_sibling)
209 c = c->ipa_sibling;
210 }
211 return conf;
212 }
213
214
215 /* isapnp_process_tag():
216 * Process a resource tag
217 */
218 static int
219 isapnp_process_tag(tag, len, buf, card, dev, conf)
220 u_char tag, len, *buf;
221 struct isapnp_attach_args **card, **dev, **conf;
222 {
223 char str[64];
224 struct isapnp_region *r;
225 struct isapnp_pin *p;
226 struct isapnp_attach_args *pa;
227
228 #define COPY(a, b) strncpy((a), (b), sizeof(a)), (a)[sizeof(a) - 1] = '\0'
229
230 switch (tag) {
231 case ISAPNP_TAG_VERSION_NUM:
232 DPRINTF(("PnP version %d.%d, Vendor version %d.%d\n",
233 buf[0] >> 4, buf[0] & 0xf, buf[1] >> 4, buf[1] & 0xf));
234 return 0;
235
236 case ISAPNP_TAG_LOGICAL_DEV_ID:
237 (void) isapnp_id_to_vendor(str, buf);
238 DPRINTF(("Logical device id %s\n", str));
239
240 *dev = isapnp_newdev(*card);
241 COPY((*dev)->ipa_devlogic, str);
242 return 0;
243
244 case ISAPNP_TAG_COMPAT_DEV_ID:
245 (void) isapnp_id_to_vendor(str, buf);
246 DPRINTF(("Compatible device id %s\n", str));
247
248 if (*dev == NULL)
249 return -1;
250
251 if (*(*dev)->ipa_devcompat == '\0')
252 COPY((*dev)->ipa_devcompat, str);
253 return 0;
254
255 case ISAPNP_TAG_DEP_START:
256 if (len == 0)
257 buf[0] = ISAPNP_DEP_ACCEPTABLE;
258
259 if (*dev == NULL)
260 return -1;
261
262 *conf = isapnp_newconf(*dev);
263 (*conf)->ipa_pref = buf[0];
264 #ifdef DEBUG_ISAPNP
265 isapnp_print_dep_start(">>> Start dependent function ",
266 (*conf)->ipa_pref);
267 #endif
268 return 0;
269
270 case ISAPNP_TAG_DEP_END:
271 DPRINTF(("<<<End dependent functions\n"));
272 *conf = NULL;
273 return 0;
274
275 case ISAPNP_TAG_ANSI_IDENT_STRING:
276 buf[len] = '\0';
277 DPRINTF(("ANSI Ident: %s\n", buf));
278 if (*dev == NULL)
279 COPY((*card)->ipa_devident, buf);
280 else
281 COPY((*dev)->ipa_devclass, buf);
282 return 0;
283
284 case ISAPNP_TAG_END:
285 *dev = NULL;
286 return 0;
287
288 default:
289 /* Handled below */
290 break;
291 }
292
293
294 /*
295 * Decide which configuration we add the tag to
296 */
297 if (*conf)
298 pa = *conf;
299 else if (*dev)
300 pa = *dev;
301 else
302 /* error */
303 return -1;
304
305 switch (tag) {
306 case ISAPNP_TAG_IRQ_FORMAT:
307 if (len < 2)
308 break;
309
310 if (len != 3)
311 buf[2] = ISAPNP_IRQTYPE_EDGE_PLUS;
312
313 p = &pa->ipa_irq[pa->ipa_nirq++];
314 p->bits = buf[0] | (buf[1] << 8);
315 p->flags = buf[2];
316 #ifdef DEBUG_ISAPNP
317 isapnp_print_irq("", p);
318 #endif
319 break;
320
321 case ISAPNP_TAG_DMA_FORMAT:
322 if (buf[0] == 0)
323 break;
324
325 p = &pa->ipa_drq[pa->ipa_ndrq++];
326 p->bits = buf[0];
327 p->flags = buf[1];
328 #ifdef DEBUG_ISAPNP
329 isapnp_print_drq("", p);
330 #endif
331 break;
332
333
334 case ISAPNP_TAG_IO_PORT_DESC:
335 r = &pa->ipa_io[pa->ipa_nio++];
336 r->flags = buf[0];
337 r->minbase = (buf[2] << 8) | buf[1];
338 r->maxbase = (buf[4] << 8) | buf[3];
339 r->align = buf[5];
340 r->length = buf[6];
341 if (r->length == 0)
342 pa->ipa_nio--;
343 #ifdef DEBUG_ISAPNP
344 isapnp_print_io("", r);
345 #endif
346 break;
347
348 case ISAPNP_TAG_FIXED_IO_PORT_DESC:
349 r = &pa->ipa_io[pa->ipa_nio++];
350 r->flags = 0;
351 r->minbase = (buf[1] << 8) | buf[0];
352 r->maxbase = r->minbase;
353 r->align = 1;
354 r->length = buf[2];
355 if (r->length == 0)
356 pa->ipa_nio--;
357 #ifdef DEBUG_ISAPNP
358 isapnp_print_io("FIXED ", r);
359 #endif
360 break;
361
362 case ISAPNP_TAG_VENDOR_DEF:
363 DPRINTF(("Vendor defined (short)\n"));
364 break;
365
366 case ISAPNP_TAG_MEM_RANGE_DESC:
367 r = &pa->ipa_mem[pa->ipa_nmem++];
368 r->flags = buf[0];
369 r->minbase = (buf[2] << 16) | (buf[1] << 8);
370 r->maxbase = (buf[4] << 16) | (buf[3] << 8);
371 r->align = (buf[6] << 8) | buf[5];
372 r->length = (buf[8] << 16) | (buf[7] << 8);
373 if (r->length == 0)
374 pa->ipa_nmem--;
375 #ifdef DEBUG_ISAPNP
376 isapnp_print_mem("", r);
377 #endif
378 break;
379
380
381 case ISAPNP_TAG_UNICODE_IDENT_STRING:
382 DPRINTF(("Unicode Ident\n"));
383 break;
384
385 case ISAPNP_TAG_VENDOR_DEFINED:
386 DPRINTF(("Vendor defined (long)\n"));
387 break;
388
389 case ISAPNP_TAG_MEM32_RANGE_DESC:
390 r = &pa->ipa_mem32[pa->ipa_nmem32++];
391 r->flags = buf[0];
392 r->minbase = (buf[4] << 24) | (buf[3] << 16) |
393 (buf[2] << 8) | buf[1];
394 r->maxbase = (buf[8] << 24) | (buf[7] << 16) |
395 (buf[6] << 8) | buf[5];
396 r->align = (buf[12] << 24) | (buf[11] << 16) |
397 (buf[10] << 8) | buf[9];
398 r->length = (buf[16] << 24) | (buf[15] << 16) |
399 (buf[14] << 8) | buf[13];
400 if (r->length == 0)
401 pa->ipa_nmem32--;
402 #ifdef DEBUG_ISAPNP
403 isapnp_print_mem("32-bit ", r);
404 #endif
405 break;
406
407 case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC:
408 r = &pa->ipa_mem32[pa->ipa_nmem32++];
409 r->flags = buf[0];
410 r->minbase = (buf[4] << 24) | (buf[3] << 16) |
411 (buf[2] << 8) | buf[1];
412 r->maxbase = r->minbase;
413 r->align = 1;
414 r->length = (buf[8] << 24) | (buf[7] << 16) |
415 (buf[6] << 8) | buf[5];
416 if (r->length == 0)
417 pa->ipa_nmem32--;
418 #ifdef DEBUG_ISAPNP
419 isapnp_print_mem("FIXED 32-bit ", r);
420 #endif
421 break;
422
423 default:
424 #ifdef DEBUG_ISAPNP
425 {
426 int i;
427 printf("tag %.2x, len %d: ", tag, len);
428 for (i = 0; i < len; i++)
429 printf("%.2x ", buf[i]);
430 printf("\n");
431 }
432 #endif
433 break;
434 }
435 return 0;
436 }
437
438
439 /* isapnp_get_resource():
440 * Read the resources for card c
441 */
442 struct isapnp_attach_args *
443 isapnp_get_resource(struct isapnp_softc *sc, int c)
444 {
445 u_char d, tag;
446 u_short len;
447 int i;
448 int warned = 0;
449 struct isapnp_attach_args *card, *dev = NULL, *conf = NULL;
450 u_char buf[ISAPNP_MAX_TAGSIZE], *p;
451
452 memset(buf, 0, sizeof(buf));
453
454 card = ISAPNP_MALLOC(sizeof(*card));
455 memset(card, 0, sizeof(*card));
456
457 #define NEXT_BYTE \
458 if (isapnp_wait_status(sc)) \
459 goto bad; \
460 d = isapnp_read_reg(sc, ISAPNP_RESOURCE_DATA)
461
462 for (i = 0; i < ISAPNP_SERIAL_SIZE; i++) {
463 NEXT_BYTE;
464
465 if (d != sc->sc_id[c][i] && i != ISAPNP_SERIAL_SIZE - 1) {
466 if (!warned) {
467 aprint_error_dev(sc->sc_dev,
468 "card %d violates PnP spec; byte %d\n",
469 c + 1, i);
470 warned++;
471 }
472 if (i == 0) {
473 /*
474 * Magic! If this is the first byte, we
475 * assume that the tag data begins here.
476 */
477 goto parse;
478 }
479 }
480 }
481
482 do {
483 NEXT_BYTE;
484 parse:
485
486 if (d & ISAPNP_LARGE_TAG) {
487 tag = d;
488 NEXT_BYTE;
489 buf[0] = d;
490 NEXT_BYTE;
491 buf[1] = d;
492 len = (buf[1] << 8) | buf[0];
493 }
494 else {
495 tag = (d >> 3) & 0xf;
496 len = d & 0x7;
497 }
498
499 for (p = buf, i = 0; i < len; i++) {
500 NEXT_BYTE;
501 if (i < ISAPNP_MAX_TAGSIZE)
502 *p++ = d;
503 }
504
505 if (len >= ISAPNP_MAX_TAGSIZE) {
506 aprint_error_dev(sc->sc_dev,
507 "Maximum tag size exceeded, card %d\n",
508 c + 1);
509 len = ISAPNP_MAX_TAGSIZE - 1;
510 if (++warned == 10)
511 goto bad;
512 }
513
514 if (isapnp_process_tag(tag, len, buf, &card, &dev,
515 &conf) == -1) {
516 aprint_error_dev(sc->sc_dev,
517 "No current device for tag, card %d\n",
518 c + 1);
519 if (++warned == 10)
520 goto bad;
521 }
522 }
523 while (tag != ISAPNP_TAG_END);
524 return isapnp_flatten(card);
525
526 bad:
527 for (card = isapnp_flatten(card); card; ) {
528 dev = card->ipa_sibling;
529 ISAPNP_FREE(card);
530 card = dev;
531 }
532 aprint_normal_dev(sc->sc_dev, "%s, card %d\n",
533 warned >= 10 ? "Too many tag errors" : "Resource timeout", c + 1);
534 return NULL;
535 }
536