1 /* $NetBSD: isapnpres.c,v 1.19 2008/04/28 20:23:53 martin 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.19 2008/04/28 20:23:53 martin 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(sc) 71 struct isapnp_softc *sc; 72 { 73 int i; 74 75 /* wait up to 1 ms for each resource byte */ 76 for (i = 0; i < 10; i++) { 77 if (isapnp_read_reg(sc, ISAPNP_STATUS) & 1) 78 return 0; 79 DELAY(100); 80 } 81 return 1; 82 } 83 84 85 /* isapnp_newdev(): 86 * Add a new logical device to the current card; expand the configuration 87 * resources of the current card if needed. 88 */ 89 static struct isapnp_attach_args * 90 isapnp_newdev(card) 91 struct isapnp_attach_args *card; 92 { 93 struct isapnp_attach_args *ipa, *dev = ISAPNP_MALLOC(sizeof(*dev)); 94 95 memset(dev, 0, sizeof(*dev)); 96 97 dev->ipa_pref = ISAPNP_DEP_ACCEPTABLE; 98 memcpy(dev->ipa_devident, card->ipa_devident, 99 sizeof(card->ipa_devident)); 100 101 if (card->ipa_child == NULL) 102 card->ipa_child = dev; 103 else { 104 for (ipa = card->ipa_child; ipa->ipa_sibling != NULL; 105 ipa = ipa->ipa_sibling) 106 continue; 107 ipa->ipa_sibling = dev; 108 } 109 110 111 return dev; 112 } 113 114 115 /* isapnp_newconf(): 116 * Add a new alternate configuration to a logical device 117 */ 118 static struct isapnp_attach_args * 119 isapnp_newconf(dev) 120 struct isapnp_attach_args *dev; 121 { 122 struct isapnp_attach_args *ipa, *conf = ISAPNP_MALLOC(sizeof(*conf)); 123 124 memset(conf, 0, sizeof(*conf)); 125 126 memcpy(conf->ipa_devident, dev->ipa_devident, 127 sizeof(conf->ipa_devident)); 128 memcpy(conf->ipa_devlogic, dev->ipa_devlogic, 129 sizeof(conf->ipa_devlogic)); 130 memcpy(conf->ipa_devcompat, dev->ipa_devcompat, 131 sizeof(conf->ipa_devcompat)); 132 memcpy(conf->ipa_devclass, dev->ipa_devclass, 133 sizeof(conf->ipa_devclass)); 134 135 if (dev->ipa_child == NULL) 136 dev->ipa_child = conf; 137 else { 138 for (ipa = dev->ipa_child; ipa->ipa_sibling; 139 ipa = ipa->ipa_sibling) 140 continue; 141 ipa->ipa_sibling = conf; 142 } 143 144 return conf; 145 } 146 147 148 /* isapnp_merge(): 149 * Merge the common device configurations to the subconfigurations 150 */ 151 static void 152 isapnp_merge(c, d) 153 struct isapnp_attach_args *c; 154 const struct isapnp_attach_args *d; 155 { 156 int i; 157 158 for (i = 0; i < d->ipa_nio; i++) 159 c->ipa_io[c->ipa_nio++] = d->ipa_io[i]; 160 161 for (i = 0; i < d->ipa_nmem; i++) 162 c->ipa_mem[c->ipa_nmem++] = d->ipa_mem[i]; 163 164 for (i = 0; i < d->ipa_nmem32; i++) 165 c->ipa_mem32[c->ipa_nmem32++] = d->ipa_mem32[i]; 166 167 for (i = 0; i < d->ipa_nirq; i++) 168 c->ipa_irq[c->ipa_nirq++] = d->ipa_irq[i]; 169 170 for (i = 0; i < d->ipa_ndrq; i++) 171 c->ipa_drq[c->ipa_ndrq++] = d->ipa_drq[i]; 172 } 173 174 175 /* isapnp_flatten(): 176 * Flatten the tree to a list of config entries. 177 */ 178 static struct isapnp_attach_args * 179 isapnp_flatten(card) 180 struct isapnp_attach_args *card; 181 { 182 struct isapnp_attach_args *dev, *conf, *d, *c, *pa; 183 184 dev = card->ipa_child; 185 ISAPNP_FREE(card); 186 187 for (conf = c = NULL, d = dev; d; d = dev) { 188 dev = d->ipa_sibling; 189 if (d->ipa_child == NULL) { 190 /* 191 * No subconfigurations; all configuration info 192 * is in the device node. 193 */ 194 d->ipa_sibling = NULL; 195 pa = d; 196 } 197 else { 198 /* 199 * Push down device configuration info to the 200 * subconfigurations 201 */ 202 for (pa = d->ipa_child; pa; pa = pa->ipa_sibling) 203 isapnp_merge(pa, d); 204 205 pa = d->ipa_child; 206 ISAPNP_FREE(d); 207 } 208 209 if (c == NULL) 210 c = conf = pa; 211 else 212 c->ipa_sibling = pa; 213 214 while (c->ipa_sibling) 215 c = c->ipa_sibling; 216 } 217 return conf; 218 } 219 220 221 /* isapnp_process_tag(): 222 * Process a resource tag 223 */ 224 static int 225 isapnp_process_tag(tag, len, buf, card, dev, conf) 226 u_char tag, len, *buf; 227 struct isapnp_attach_args **card, **dev, **conf; 228 { 229 char str[64]; 230 struct isapnp_region *r; 231 struct isapnp_pin *p; 232 struct isapnp_attach_args *pa; 233 234 #define COPY(a, b) strncpy((a), (b), sizeof(a)), (a)[sizeof(a) - 1] = '\0' 235 236 switch (tag) { 237 case ISAPNP_TAG_VERSION_NUM: 238 DPRINTF(("PnP version %d.%d, Vendor version %d.%d\n", 239 buf[0] >> 4, buf[0] & 0xf, buf[1] >> 4, buf[1] & 0xf)); 240 return 0; 241 242 case ISAPNP_TAG_LOGICAL_DEV_ID: 243 (void) isapnp_id_to_vendor(str, buf); 244 DPRINTF(("Logical device id %s\n", str)); 245 246 *dev = isapnp_newdev(*card); 247 COPY((*dev)->ipa_devlogic, str); 248 return 0; 249 250 case ISAPNP_TAG_COMPAT_DEV_ID: 251 (void) isapnp_id_to_vendor(str, buf); 252 DPRINTF(("Compatible device id %s\n", str)); 253 254 if (*dev == NULL) 255 return -1; 256 257 if (*(*dev)->ipa_devcompat == '\0') 258 COPY((*dev)->ipa_devcompat, str); 259 return 0; 260 261 case ISAPNP_TAG_DEP_START: 262 if (len == 0) 263 buf[0] = ISAPNP_DEP_ACCEPTABLE; 264 265 if (*dev == NULL) 266 return -1; 267 268 *conf = isapnp_newconf(*dev); 269 (*conf)->ipa_pref = buf[0]; 270 #ifdef DEBUG_ISAPNP 271 isapnp_print_dep_start(">>> Start dependent function ", 272 (*conf)->ipa_pref); 273 #endif 274 return 0; 275 276 case ISAPNP_TAG_DEP_END: 277 DPRINTF(("<<<End dependent functions\n")); 278 *conf = NULL; 279 return 0; 280 281 case ISAPNP_TAG_ANSI_IDENT_STRING: 282 buf[len] = '\0'; 283 DPRINTF(("ANSI Ident: %s\n", buf)); 284 if (*dev == NULL) 285 COPY((*card)->ipa_devident, buf); 286 else 287 COPY((*dev)->ipa_devclass, buf); 288 return 0; 289 290 case ISAPNP_TAG_END: 291 *dev = NULL; 292 return 0; 293 294 default: 295 /* Handled below */ 296 break; 297 } 298 299 300 /* 301 * Decide which configuration we add the tag to 302 */ 303 if (*conf) 304 pa = *conf; 305 else if (*dev) 306 pa = *dev; 307 else 308 /* error */ 309 return -1; 310 311 switch (tag) { 312 case ISAPNP_TAG_IRQ_FORMAT: 313 if (len < 2) 314 break; 315 316 if (len != 3) 317 buf[2] = ISAPNP_IRQTYPE_EDGE_PLUS; 318 319 p = &pa->ipa_irq[pa->ipa_nirq++]; 320 p->bits = buf[0] | (buf[1] << 8); 321 p->flags = buf[2]; 322 #ifdef DEBUG_ISAPNP 323 isapnp_print_irq("", p); 324 #endif 325 break; 326 327 case ISAPNP_TAG_DMA_FORMAT: 328 if (buf[0] == 0) 329 break; 330 331 p = &pa->ipa_drq[pa->ipa_ndrq++]; 332 p->bits = buf[0]; 333 p->flags = buf[1]; 334 #ifdef DEBUG_ISAPNP 335 isapnp_print_drq("", p); 336 #endif 337 break; 338 339 340 case ISAPNP_TAG_IO_PORT_DESC: 341 r = &pa->ipa_io[pa->ipa_nio++]; 342 r->flags = buf[0]; 343 r->minbase = (buf[2] << 8) | buf[1]; 344 r->maxbase = (buf[4] << 8) | buf[3]; 345 r->align = buf[5]; 346 r->length = buf[6]; 347 if (r->length == 0) 348 pa->ipa_nio--; 349 #ifdef DEBUG_ISAPNP 350 isapnp_print_io("", r); 351 #endif 352 break; 353 354 case ISAPNP_TAG_FIXED_IO_PORT_DESC: 355 r = &pa->ipa_io[pa->ipa_nio++]; 356 r->flags = 0; 357 r->minbase = (buf[1] << 8) | buf[0]; 358 r->maxbase = r->minbase; 359 r->align = 1; 360 r->length = buf[2]; 361 if (r->length == 0) 362 pa->ipa_nio--; 363 #ifdef DEBUG_ISAPNP 364 isapnp_print_io("FIXED ", r); 365 #endif 366 break; 367 368 case ISAPNP_TAG_VENDOR_DEF: 369 DPRINTF(("Vendor defined (short)\n")); 370 break; 371 372 case ISAPNP_TAG_MEM_RANGE_DESC: 373 r = &pa->ipa_mem[pa->ipa_nmem++]; 374 r->flags = buf[0]; 375 r->minbase = (buf[2] << 16) | (buf[1] << 8); 376 r->maxbase = (buf[4] << 16) | (buf[3] << 8); 377 r->align = (buf[6] << 8) | buf[5]; 378 r->length = (buf[8] << 16) | (buf[7] << 8); 379 if (r->length == 0) 380 pa->ipa_nmem--; 381 #ifdef DEBUG_ISAPNP 382 isapnp_print_mem("", r); 383 #endif 384 break; 385 386 387 case ISAPNP_TAG_UNICODE_IDENT_STRING: 388 DPRINTF(("Unicode Ident\n")); 389 break; 390 391 case ISAPNP_TAG_VENDOR_DEFINED: 392 DPRINTF(("Vendor defined (long)\n")); 393 break; 394 395 case ISAPNP_TAG_MEM32_RANGE_DESC: 396 r = &pa->ipa_mem32[pa->ipa_nmem32++]; 397 r->flags = buf[0]; 398 r->minbase = (buf[4] << 24) | (buf[3] << 16) | 399 (buf[2] << 8) | buf[1]; 400 r->maxbase = (buf[8] << 24) | (buf[7] << 16) | 401 (buf[6] << 8) | buf[5]; 402 r->align = (buf[12] << 24) | (buf[11] << 16) | 403 (buf[10] << 8) | buf[9]; 404 r->length = (buf[16] << 24) | (buf[15] << 16) | 405 (buf[14] << 8) | buf[13]; 406 if (r->length == 0) 407 pa->ipa_nmem32--; 408 #ifdef DEBUG_ISAPNP 409 isapnp_print_mem("32-bit ", r); 410 #endif 411 break; 412 413 case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC: 414 r = &pa->ipa_mem32[pa->ipa_nmem32++]; 415 r->flags = buf[0]; 416 r->minbase = (buf[4] << 24) | (buf[3] << 16) | 417 (buf[2] << 8) | buf[1]; 418 r->maxbase = r->minbase; 419 r->align = 1; 420 r->length = (buf[8] << 24) | (buf[7] << 16) | 421 (buf[6] << 8) | buf[5]; 422 if (r->length == 0) 423 pa->ipa_nmem32--; 424 #ifdef DEBUG_ISAPNP 425 isapnp_print_mem("FIXED 32-bit ", r); 426 #endif 427 break; 428 429 default: 430 #ifdef DEBUG_ISAPNP 431 { 432 int i; 433 printf("tag %.2x, len %d: ", tag, len); 434 for (i = 0; i < len; i++) 435 printf("%.2x ", buf[i]); 436 printf("\n"); 437 } 438 #endif 439 break; 440 } 441 return 0; 442 } 443 444 445 /* isapnp_get_resource(): 446 * Read the resources for card c 447 */ 448 struct isapnp_attach_args * 449 isapnp_get_resource(sc, c) 450 struct isapnp_softc *sc; 451 int c; 452 { 453 u_char d, tag; 454 u_short len; 455 int i; 456 int warned = 0; 457 struct isapnp_attach_args *card, *dev = NULL, *conf = NULL; 458 u_char buf[ISAPNP_MAX_TAGSIZE], *p; 459 460 memset(buf, 0, sizeof(buf)); 461 462 card = ISAPNP_MALLOC(sizeof(*card)); 463 memset(card, 0, sizeof(*card)); 464 465 #define NEXT_BYTE \ 466 if (isapnp_wait_status(sc)) \ 467 goto bad; \ 468 d = isapnp_read_reg(sc, ISAPNP_RESOURCE_DATA) 469 470 for (i = 0; i < ISAPNP_SERIAL_SIZE; i++) { 471 NEXT_BYTE; 472 473 if (d != sc->sc_id[c][i] && i != ISAPNP_SERIAL_SIZE - 1) { 474 if (!warned) { 475 aprint_error_dev(sc->sc_dev, 476 "card %d violates PnP spec; byte %d\n", 477 c + 1, i); 478 warned++; 479 } 480 if (i == 0) { 481 /* 482 * Magic! If this is the first byte, we 483 * assume that the tag data begins here. 484 */ 485 goto parse; 486 } 487 } 488 } 489 490 do { 491 NEXT_BYTE; 492 parse: 493 494 if (d & ISAPNP_LARGE_TAG) { 495 tag = d; 496 NEXT_BYTE; 497 buf[0] = d; 498 NEXT_BYTE; 499 buf[1] = d; 500 len = (buf[1] << 8) | buf[0]; 501 } 502 else { 503 tag = (d >> 3) & 0xf; 504 len = d & 0x7; 505 } 506 507 for (p = buf, i = 0; i < len; i++) { 508 NEXT_BYTE; 509 if (i < ISAPNP_MAX_TAGSIZE) 510 *p++ = d; 511 } 512 513 if (len >= ISAPNP_MAX_TAGSIZE) { 514 aprint_error_dev(sc->sc_dev, 515 "Maximum tag size exceeded, card %d\n", 516 c + 1); 517 len = ISAPNP_MAX_TAGSIZE - 1; 518 if (++warned == 10) 519 goto bad; 520 } 521 522 if (isapnp_process_tag(tag, len, buf, &card, &dev, 523 &conf) == -1) { 524 aprint_error_dev(sc->sc_dev, 525 "No current device for tag, card %d\n", 526 c + 1); 527 if (++warned == 10) 528 goto bad; 529 } 530 } 531 while (tag != ISAPNP_TAG_END); 532 return isapnp_flatten(card); 533 534 bad: 535 for (card = isapnp_flatten(card); card; ) { 536 dev = card->ipa_sibling; 537 ISAPNP_FREE(card); 538 card = dev; 539 } 540 aprint_normal_dev(sc->sc_dev, "%s, card %d\n", 541 warned >= 10 ? "Too many tag errors" : "Resource timeout", c + 1); 542 return NULL; 543 } 544