Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / isapnp / isapnpres.c
blob3cdce02051a34d536909c9ca6b1e6a2c2e3962d9
1 /* $NetBSD: isapnpres.c,v 1.20 2009/03/14 15:36:18 dsl Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
33 * Resource parser for Plug and Play cards.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: isapnpres.c,v 1.20 2009/03/14 15:36:18 dsl Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
44 #include <sys/bus.h>
46 #include <dev/isa/isavar.h>
48 #include <dev/isapnp/isapnpreg.h>
49 #include <dev/isapnp/isapnpvar.h>
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 **);
66 /* isapnp_wait_status():
67 * Wait for the next byte of resource data to become available
69 static int
70 isapnp_wait_status(struct isapnp_softc *sc)
72 int i;
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);
80 return 1;
84 /* isapnp_newdev():
85 * Add a new logical device to the current card; expand the configuration
86 * resources of the current card if needed.
88 static struct isapnp_attach_args *
89 isapnp_newdev(struct isapnp_attach_args *card)
91 struct isapnp_attach_args *ipa, *dev = ISAPNP_MALLOC(sizeof(*dev));
93 memset(dev, 0, sizeof(*dev));
95 dev->ipa_pref = ISAPNP_DEP_ACCEPTABLE;
96 memcpy(dev->ipa_devident, card->ipa_devident,
97 sizeof(card->ipa_devident));
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;
109 return dev;
113 /* isapnp_newconf():
114 * Add a new alternate configuration to a logical device
116 static struct isapnp_attach_args *
117 isapnp_newconf(struct isapnp_attach_args *dev)
119 struct isapnp_attach_args *ipa, *conf = ISAPNP_MALLOC(sizeof(*conf));
121 memset(conf, 0, sizeof(*conf));
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));
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;
141 return conf;
145 /* isapnp_merge():
146 * Merge the common device configurations to the subconfigurations
148 static void
149 isapnp_merge(struct isapnp_attach_args *c, const struct isapnp_attach_args *d)
151 int i;
153 for (i = 0; i < d->ipa_nio; i++)
154 c->ipa_io[c->ipa_nio++] = d->ipa_io[i];
156 for (i = 0; i < d->ipa_nmem; i++)
157 c->ipa_mem[c->ipa_nmem++] = d->ipa_mem[i];
159 for (i = 0; i < d->ipa_nmem32; i++)
160 c->ipa_mem32[c->ipa_nmem32++] = d->ipa_mem32[i];
162 for (i = 0; i < d->ipa_nirq; i++)
163 c->ipa_irq[c->ipa_nirq++] = d->ipa_irq[i];
165 for (i = 0; i < d->ipa_ndrq; i++)
166 c->ipa_drq[c->ipa_ndrq++] = d->ipa_drq[i];
170 /* isapnp_flatten():
171 * Flatten the tree to a list of config entries.
173 static struct isapnp_attach_args *
174 isapnp_flatten(struct isapnp_attach_args *card)
176 struct isapnp_attach_args *dev, *conf, *d, *c, *pa;
178 dev = card->ipa_child;
179 ISAPNP_FREE(card);
181 for (conf = c = NULL, d = dev; d; d = dev) {
182 dev = d->ipa_sibling;
183 if (d->ipa_child == NULL) {
185 * No subconfigurations; all configuration info
186 * is in the device node.
188 d->ipa_sibling = NULL;
189 pa = d;
191 else {
193 * Push down device configuration info to the
194 * subconfigurations
196 for (pa = d->ipa_child; pa; pa = pa->ipa_sibling)
197 isapnp_merge(pa, d);
199 pa = d->ipa_child;
200 ISAPNP_FREE(d);
203 if (c == NULL)
204 c = conf = pa;
205 else
206 c->ipa_sibling = pa;
208 while (c->ipa_sibling)
209 c = c->ipa_sibling;
211 return conf;
215 /* isapnp_process_tag():
216 * Process a resource tag
218 static int
219 isapnp_process_tag(u_char tag, u_char len, u_char *buf, struct isapnp_attach_args **card, struct isapnp_attach_args **dev, struct isapnp_attach_args **conf)
221 char str[64];
222 struct isapnp_region *r;
223 struct isapnp_pin *p;
224 struct isapnp_attach_args *pa;
226 #define COPY(a, b) strncpy((a), (b), sizeof(a)), (a)[sizeof(a) - 1] = '\0'
228 switch (tag) {
229 case ISAPNP_TAG_VERSION_NUM:
230 DPRINTF(("PnP version %d.%d, Vendor version %d.%d\n",
231 buf[0] >> 4, buf[0] & 0xf, buf[1] >> 4, buf[1] & 0xf));
232 return 0;
234 case ISAPNP_TAG_LOGICAL_DEV_ID:
235 (void) isapnp_id_to_vendor(str, buf);
236 DPRINTF(("Logical device id %s\n", str));
238 *dev = isapnp_newdev(*card);
239 COPY((*dev)->ipa_devlogic, str);
240 return 0;
242 case ISAPNP_TAG_COMPAT_DEV_ID:
243 (void) isapnp_id_to_vendor(str, buf);
244 DPRINTF(("Compatible device id %s\n", str));
246 if (*dev == NULL)
247 return -1;
249 if (*(*dev)->ipa_devcompat == '\0')
250 COPY((*dev)->ipa_devcompat, str);
251 return 0;
253 case ISAPNP_TAG_DEP_START:
254 if (len == 0)
255 buf[0] = ISAPNP_DEP_ACCEPTABLE;
257 if (*dev == NULL)
258 return -1;
260 *conf = isapnp_newconf(*dev);
261 (*conf)->ipa_pref = buf[0];
262 #ifdef DEBUG_ISAPNP
263 isapnp_print_dep_start(">>> Start dependent function ",
264 (*conf)->ipa_pref);
265 #endif
266 return 0;
268 case ISAPNP_TAG_DEP_END:
269 DPRINTF(("<<<End dependent functions\n"));
270 *conf = NULL;
271 return 0;
273 case ISAPNP_TAG_ANSI_IDENT_STRING:
274 buf[len] = '\0';
275 DPRINTF(("ANSI Ident: %s\n", buf));
276 if (*dev == NULL)
277 COPY((*card)->ipa_devident, buf);
278 else
279 COPY((*dev)->ipa_devclass, buf);
280 return 0;
282 case ISAPNP_TAG_END:
283 *dev = NULL;
284 return 0;
286 default:
287 /* Handled below */
288 break;
293 * Decide which configuration we add the tag to
295 if (*conf)
296 pa = *conf;
297 else if (*dev)
298 pa = *dev;
299 else
300 /* error */
301 return -1;
303 switch (tag) {
304 case ISAPNP_TAG_IRQ_FORMAT:
305 if (len < 2)
306 break;
308 if (len != 3)
309 buf[2] = ISAPNP_IRQTYPE_EDGE_PLUS;
311 p = &pa->ipa_irq[pa->ipa_nirq++];
312 p->bits = buf[0] | (buf[1] << 8);
313 p->flags = buf[2];
314 #ifdef DEBUG_ISAPNP
315 isapnp_print_irq("", p);
316 #endif
317 break;
319 case ISAPNP_TAG_DMA_FORMAT:
320 if (buf[0] == 0)
321 break;
323 p = &pa->ipa_drq[pa->ipa_ndrq++];
324 p->bits = buf[0];
325 p->flags = buf[1];
326 #ifdef DEBUG_ISAPNP
327 isapnp_print_drq("", p);
328 #endif
329 break;
332 case ISAPNP_TAG_IO_PORT_DESC:
333 r = &pa->ipa_io[pa->ipa_nio++];
334 r->flags = buf[0];
335 r->minbase = (buf[2] << 8) | buf[1];
336 r->maxbase = (buf[4] << 8) | buf[3];
337 r->align = buf[5];
338 r->length = buf[6];
339 if (r->length == 0)
340 pa->ipa_nio--;
341 #ifdef DEBUG_ISAPNP
342 isapnp_print_io("", r);
343 #endif
344 break;
346 case ISAPNP_TAG_FIXED_IO_PORT_DESC:
347 r = &pa->ipa_io[pa->ipa_nio++];
348 r->flags = 0;
349 r->minbase = (buf[1] << 8) | buf[0];
350 r->maxbase = r->minbase;
351 r->align = 1;
352 r->length = buf[2];
353 if (r->length == 0)
354 pa->ipa_nio--;
355 #ifdef DEBUG_ISAPNP
356 isapnp_print_io("FIXED ", r);
357 #endif
358 break;
360 case ISAPNP_TAG_VENDOR_DEF:
361 DPRINTF(("Vendor defined (short)\n"));
362 break;
364 case ISAPNP_TAG_MEM_RANGE_DESC:
365 r = &pa->ipa_mem[pa->ipa_nmem++];
366 r->flags = buf[0];
367 r->minbase = (buf[2] << 16) | (buf[1] << 8);
368 r->maxbase = (buf[4] << 16) | (buf[3] << 8);
369 r->align = (buf[6] << 8) | buf[5];
370 r->length = (buf[8] << 16) | (buf[7] << 8);
371 if (r->length == 0)
372 pa->ipa_nmem--;
373 #ifdef DEBUG_ISAPNP
374 isapnp_print_mem("", r);
375 #endif
376 break;
379 case ISAPNP_TAG_UNICODE_IDENT_STRING:
380 DPRINTF(("Unicode Ident\n"));
381 break;
383 case ISAPNP_TAG_VENDOR_DEFINED:
384 DPRINTF(("Vendor defined (long)\n"));
385 break;
387 case ISAPNP_TAG_MEM32_RANGE_DESC:
388 r = &pa->ipa_mem32[pa->ipa_nmem32++];
389 r->flags = buf[0];
390 r->minbase = (buf[4] << 24) | (buf[3] << 16) |
391 (buf[2] << 8) | buf[1];
392 r->maxbase = (buf[8] << 24) | (buf[7] << 16) |
393 (buf[6] << 8) | buf[5];
394 r->align = (buf[12] << 24) | (buf[11] << 16) |
395 (buf[10] << 8) | buf[9];
396 r->length = (buf[16] << 24) | (buf[15] << 16) |
397 (buf[14] << 8) | buf[13];
398 if (r->length == 0)
399 pa->ipa_nmem32--;
400 #ifdef DEBUG_ISAPNP
401 isapnp_print_mem("32-bit ", r);
402 #endif
403 break;
405 case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC:
406 r = &pa->ipa_mem32[pa->ipa_nmem32++];
407 r->flags = buf[0];
408 r->minbase = (buf[4] << 24) | (buf[3] << 16) |
409 (buf[2] << 8) | buf[1];
410 r->maxbase = r->minbase;
411 r->align = 1;
412 r->length = (buf[8] << 24) | (buf[7] << 16) |
413 (buf[6] << 8) | buf[5];
414 if (r->length == 0)
415 pa->ipa_nmem32--;
416 #ifdef DEBUG_ISAPNP
417 isapnp_print_mem("FIXED 32-bit ", r);
418 #endif
419 break;
421 default:
422 #ifdef DEBUG_ISAPNP
424 int i;
425 printf("tag %.2x, len %d: ", tag, len);
426 for (i = 0; i < len; i++)
427 printf("%.2x ", buf[i]);
428 printf("\n");
430 #endif
431 break;
433 return 0;
437 /* isapnp_get_resource():
438 * Read the resources for card c
440 struct isapnp_attach_args *
441 isapnp_get_resource(struct isapnp_softc *sc, int c)
443 u_char d, tag;
444 u_short len;
445 int i;
446 int warned = 0;
447 struct isapnp_attach_args *card, *dev = NULL, *conf = NULL;
448 u_char buf[ISAPNP_MAX_TAGSIZE], *p;
450 memset(buf, 0, sizeof(buf));
452 card = ISAPNP_MALLOC(sizeof(*card));
453 memset(card, 0, sizeof(*card));
455 #define NEXT_BYTE \
456 if (isapnp_wait_status(sc)) \
457 goto bad; \
458 d = isapnp_read_reg(sc, ISAPNP_RESOURCE_DATA)
460 for (i = 0; i < ISAPNP_SERIAL_SIZE; i++) {
461 NEXT_BYTE;
463 if (d != sc->sc_id[c][i] && i != ISAPNP_SERIAL_SIZE - 1) {
464 if (!warned) {
465 aprint_error_dev(sc->sc_dev,
466 "card %d violates PnP spec; byte %d\n",
467 c + 1, i);
468 warned++;
470 if (i == 0) {
472 * Magic! If this is the first byte, we
473 * assume that the tag data begins here.
475 goto parse;
480 do {
481 NEXT_BYTE;
482 parse:
484 if (d & ISAPNP_LARGE_TAG) {
485 tag = d;
486 NEXT_BYTE;
487 buf[0] = d;
488 NEXT_BYTE;
489 buf[1] = d;
490 len = (buf[1] << 8) | buf[0];
492 else {
493 tag = (d >> 3) & 0xf;
494 len = d & 0x7;
497 for (p = buf, i = 0; i < len; i++) {
498 NEXT_BYTE;
499 if (i < ISAPNP_MAX_TAGSIZE)
500 *p++ = d;
503 if (len >= ISAPNP_MAX_TAGSIZE) {
504 aprint_error_dev(sc->sc_dev,
505 "Maximum tag size exceeded, card %d\n",
506 c + 1);
507 len = ISAPNP_MAX_TAGSIZE - 1;
508 if (++warned == 10)
509 goto bad;
512 if (isapnp_process_tag(tag, len, buf, &card, &dev,
513 &conf) == -1) {
514 aprint_error_dev(sc->sc_dev,
515 "No current device for tag, card %d\n",
516 c + 1);
517 if (++warned == 10)
518 goto bad;
521 while (tag != ISAPNP_TAG_END);
522 return isapnp_flatten(card);
524 bad:
525 for (card = isapnp_flatten(card); card; ) {
526 dev = card->ipa_sibling;
527 ISAPNP_FREE(card);
528 card = dev;
530 aprint_normal_dev(sc->sc_dev, "%s, card %d\n",
531 warned >= 10 ? "Too many tag errors" : "Resource timeout", c + 1);
532 return NULL;