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