1 /* $OpenBSD: pcmcia_cis.c,v 1.15 2007/09/11 13:39:34 gilles Exp $ */ 2 /* $NetBSD: pcmcia_cis.c,v 1.9 1998/08/22 23:41:48 msaitoh Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Marc Horowitz. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/malloc.h> 38 39 #include <dev/pcmcia/pcmciareg.h> 40 #include <dev/pcmcia/pcmciachip.h> 41 #include <dev/pcmcia/pcmciavar.h> 42 43 #ifdef PCMCIACISDEBUG 44 #define DPRINTF(arg) printf arg 45 #else 46 #define DPRINTF(arg) 47 #endif 48 49 #define PCMCIA_CIS_SIZE 1024 50 51 struct cis_state { 52 int count; 53 int gotmfc; 54 struct pcmcia_config_entry temp_cfe; 55 struct pcmcia_config_entry *default_cfe; 56 struct pcmcia_card *card; 57 struct pcmcia_function *pf; 58 }; 59 60 int pcmcia_parse_cis_tuple(struct pcmcia_tuple *, void *); 61 62 void 63 pcmcia_read_cis(sc) 64 struct pcmcia_softc *sc; 65 { 66 struct cis_state state; 67 68 memset(&state, 0, sizeof state); 69 70 state.card = &sc->card; 71 72 state.card->error = 0; 73 state.card->cis1_major = -1; 74 state.card->cis1_minor = -1; 75 state.card->cis1_info[0] = NULL; 76 state.card->cis1_info[1] = NULL; 77 state.card->cis1_info[2] = NULL; 78 state.card->cis1_info[3] = NULL; 79 state.card->manufacturer = PCMCIA_VENDOR_INVALID; 80 state.card->product = PCMCIA_PRODUCT_INVALID; 81 SIMPLEQ_INIT(&state.card->pf_head); 82 83 state.pf = NULL; 84 85 if (pcmcia_scan_cis((struct device *)sc, pcmcia_parse_cis_tuple, 86 &state) == -1) 87 state.card->error++; 88 } 89 90 int 91 pcmcia_scan_cis(dev, fct, arg) 92 struct device *dev; 93 int (*fct)(struct pcmcia_tuple *, void *); 94 void *arg; 95 { 96 struct pcmcia_softc *sc = (struct pcmcia_softc *) dev; 97 pcmcia_chipset_tag_t pct; 98 pcmcia_chipset_handle_t pch; 99 int window; 100 struct pcmcia_mem_handle pcmh; 101 struct pcmcia_tuple tuple; 102 int longlink_present; 103 int longlink_common; 104 u_long longlink_addr; 105 int mfc_count; 106 int mfc_index; 107 struct { 108 int common; 109 u_long addr; 110 } mfc[256 / 5]; 111 int ret; 112 113 ret = 0; 114 115 pct = sc->pct; 116 pch = sc->pch; 117 118 /* allocate some memory */ 119 120 if (pcmcia_chip_mem_alloc(pct, pch, PCMCIA_CIS_SIZE, &pcmh)) { 121 #ifdef DIAGNOSTIC 122 printf("%s: can't alloc memory to read attributes\n", 123 sc->dev.dv_xname); 124 #endif 125 return -1; 126 } 127 128 /* initialize state for the primary tuple chain */ 129 if (pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_ATTR, 0, 130 PCMCIA_CIS_SIZE, &pcmh, &tuple.ptr, &window)) { 131 pcmcia_chip_mem_free(pct, pch, &pcmh); 132 #ifdef DIAGNOSTIC 133 printf("%s: can't map memory to read attributes\n", 134 sc->dev.dv_xname); 135 #endif 136 return -1; 137 } 138 tuple.memt = pcmh.memt; 139 tuple.memh = pcmh.memh; 140 141 DPRINTF(("cis mem map %x\n", (unsigned int) tuple.memh)); 142 143 tuple.mult = 2; 144 145 longlink_present = 1; 146 longlink_common = 1; 147 longlink_addr = 0; 148 149 mfc_count = 0; 150 mfc_index = 0; 151 152 DPRINTF(("%s: CIS tuple chain:\n", sc->dev.dv_xname)); 153 154 while (1) { 155 while (1) { 156 /* 157 * Perform boundary check for insane cards. 158 * If CIS is too long, simulate CIS end. 159 * (This check may not be sufficient for 160 * malicious cards.) 161 */ 162 if (tuple.mult * tuple.ptr >= PCMCIA_CIS_SIZE - 1 163 - 32 /* ad hoc value */ ) { 164 DPRINTF(("CISTPL_END (too long CIS)\n")); 165 tuple.code = PCMCIA_CISTPL_END; 166 goto cis_end; 167 } 168 169 /* get the tuple code */ 170 171 tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr); 172 173 /* two special-case tuples */ 174 175 if (tuple.code == PCMCIA_CISTPL_NULL) { 176 DPRINTF(("CISTPL_NONE\n 00\n")); 177 tuple.ptr++; 178 continue; 179 } else if (tuple.code == PCMCIA_CISTPL_END) { 180 DPRINTF(("CISTPL_END\n ff\n")); 181 cis_end: 182 /* Call the function for the END tuple, since 183 the CIS semantics depend on it */ 184 if ((*fct) (&tuple, arg)) { 185 pcmcia_chip_mem_unmap(pct, pch, 186 window); 187 ret = 1; 188 goto done; 189 } 190 tuple.ptr++; 191 break; 192 } 193 /* now all the normal tuples */ 194 195 tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1); 196 switch (tuple.code) { 197 case PCMCIA_CISTPL_LONGLINK_A: 198 case PCMCIA_CISTPL_LONGLINK_C: 199 if (tuple.length < 4) { 200 DPRINTF(("CISTPL_LONGLINK_%s too " 201 "short %d\n", 202 longlink_common ? "C" : "A", 203 tuple.length)); 204 break; 205 } 206 longlink_present = 1; 207 longlink_common = (tuple.code == 208 PCMCIA_CISTPL_LONGLINK_C) ? 1 : 0; 209 longlink_addr = pcmcia_tuple_read_4(&tuple, 0); 210 DPRINTF(("CISTPL_LONGLINK_%s %lx\n", 211 longlink_common ? "C" : "A", 212 longlink_addr)); 213 break; 214 case PCMCIA_CISTPL_NO_LINK: 215 longlink_present = 0; 216 DPRINTF(("CISTPL_NO_LINK\n")); 217 break; 218 case PCMCIA_CISTPL_CHECKSUM: 219 if (tuple.length < 5) { 220 DPRINTF(("CISTPL_CHECKSUM too " 221 "short %d\n", tuple.length)); 222 break; 223 } { 224 int16_t offset; 225 u_long addr, length; 226 u_int cksum, sum; 227 int i; 228 229 *((u_int16_t *) & offset) = 230 pcmcia_tuple_read_2(&tuple, 0); 231 length = pcmcia_tuple_read_2(&tuple, 2); 232 cksum = pcmcia_tuple_read_1(&tuple, 4); 233 234 addr = tuple.ptr + offset; 235 236 DPRINTF(("CISTPL_CHECKSUM addr=%lx " 237 "len=%lx cksum=%x", 238 addr, length, cksum)); 239 240 /* 241 * XXX do more work to deal with 242 * distant regions 243 */ 244 if ((addr >= PCMCIA_CIS_SIZE) || 245 ((addr + length) < 0) || 246 ((addr + length) >= 247 PCMCIA_CIS_SIZE)) { 248 DPRINTF((" skipped, " 249 "too distant\n")); 250 break; 251 } 252 sum = 0; 253 for (i = 0; i < length; i++) 254 sum += 255 bus_space_read_1(tuple.memt, 256 tuple.memh, 257 addr + tuple.mult * i); 258 if (cksum != (sum & 0xff)) { 259 DPRINTF((" failed sum=%x\n", 260 sum)); 261 printf("%s: CIS checksum " 262 "failed\n", 263 sc->dev.dv_xname); 264 #if 0 265 /* 266 * XXX Some working cards have 267 * XXX bad checksums!! 268 */ 269 ret = -1; 270 #endif 271 } else { 272 DPRINTF((" ok\n")); 273 } 274 } 275 break; 276 case PCMCIA_CISTPL_LONGLINK_MFC: 277 if (tuple.length < 6) { 278 DPRINTF(("CISTPL_LONGLINK_MFC too " 279 "short %d\n", tuple.length)); 280 break; 281 } 282 if (((tuple.length - 1) % 5) != 0) { 283 DPRINTF(("CISTPL_LONGLINK_MFC bogus " 284 "length %d\n", tuple.length)); 285 break; 286 } 287 { 288 int i, tmp_count; 289 290 /* 291 * put count into tmp var so that 292 * if we have to bail (because it's 293 * a bogus count) it won't be 294 * remembered for later use. 295 */ 296 tmp_count = 297 pcmcia_tuple_read_1(&tuple, 0); 298 DPRINTF(("CISTPL_LONGLINK_MFC %d", 299 tmp_count)); 300 301 /* 302 * make _sure_ it's the right size; 303 * if too short, it may be a weird 304 * (unknown/undefined) format 305 */ 306 if (tuple.length != (tmp_count*5 + 1)) { 307 DPRINTF((" bogus length %d\n", 308 tuple.length)); 309 break; 310 } 311 312 #ifdef PCMCIACISDEBUG /* maybe enable all the time? */ 313 /* 314 * sanity check for a programming 315 * error which is difficult to find 316 * when debugging. 317 */ 318 if (tmp_count > 319 howmany(sizeof mfc, sizeof mfc[0])) 320 panic("CISTPL_LONGLINK_MFC mfc " 321 "count would blow stack"); 322 #endif 323 324 mfc_count = tmp_count; 325 for (i = 0; i < mfc_count; i++) { 326 mfc[i].common = 327 (pcmcia_tuple_read_1(&tuple, 328 1 + 5 * i) == 329 PCMCIA_MFC_MEM_COMMON) ? 330 1 : 0; 331 mfc[i].addr = 332 pcmcia_tuple_read_4(&tuple, 333 1 + 5 * i + 1); 334 DPRINTF((" %s:%lx", 335 mfc[i].common ? "common" : 336 "attr", mfc[i].addr)); 337 } 338 DPRINTF(("\n")); 339 } 340 /* 341 * for LONGLINK_MFC, fall through to the 342 * function. This tuple has structural and 343 * semantic content. 344 */ 345 default: 346 { 347 if ((*fct) (&tuple, arg)) { 348 pcmcia_chip_mem_unmap(pct, 349 pch, window); 350 ret = 1; 351 goto done; 352 } 353 } 354 break; 355 } /* switch */ 356 #ifdef PCMCIACISDEBUG 357 /* print the tuple */ 358 { 359 int i; 360 361 DPRINTF((" %02x %02x", tuple.code, 362 tuple.length)); 363 364 for (i = 0; i < tuple.length; i++) { 365 DPRINTF((" %02x", 366 pcmcia_tuple_read_1(&tuple, i))); 367 if ((i % 16) == 13) 368 DPRINTF(("\n")); 369 } 370 if ((i % 16) != 14) 371 DPRINTF(("\n")); 372 } 373 #endif 374 /* skip to the next tuple */ 375 tuple.ptr += 2 + tuple.length; 376 } 377 378 /* 379 * the chain is done. Clean up and move onto the next one, 380 * if any. The loop is here in the case that there is an MFC 381 * card with no longlink (which defaults to existing, == 0). 382 * In general, this means that if one pointer fails, it will 383 * try the next one, instead of just bailing. 384 */ 385 386 while (1) { 387 pcmcia_chip_mem_unmap(pct, pch, window); 388 389 if (longlink_present) { 390 /* 391 * if the longlink is to attribute memory, 392 * then it is unindexed. That is, if the 393 * link value is 0x100, then the actual 394 * memory address is 0x200. This means that 395 * we need to multiply by 2 before calling 396 * mem_map, and then divide the resulting ptr 397 * by 2 after. 398 */ 399 400 if (!longlink_common) 401 longlink_addr *= 2; 402 403 pcmcia_chip_mem_map(pct, pch, longlink_common ? 404 PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR, 405 longlink_addr, PCMCIA_CIS_SIZE, 406 &pcmh, &tuple.ptr, &window); 407 408 if (!longlink_common) 409 tuple.ptr /= 2; 410 411 DPRINTF(("cis mem map %x\n", 412 (unsigned int) tuple.memh)); 413 414 tuple.mult = longlink_common ? 1 : 2; 415 longlink_present = 0; 416 longlink_common = 1; 417 longlink_addr = 0; 418 } else if (mfc_count && (mfc_index < mfc_count)) { 419 if (!mfc[mfc_index].common) 420 mfc[mfc_index].addr *= 2; 421 422 pcmcia_chip_mem_map(pct, pch, 423 mfc[mfc_index].common ? 424 PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR, 425 mfc[mfc_index].addr, PCMCIA_CIS_SIZE, 426 &pcmh, &tuple.ptr, &window); 427 428 if (!mfc[mfc_index].common) 429 tuple.ptr /= 2; 430 431 DPRINTF(("cis mem map %x\n", 432 (unsigned int) tuple.memh)); 433 434 /* set parse state, and point at the next one */ 435 436 tuple.mult = mfc[mfc_index].common ? 1 : 2; 437 438 mfc_index++; 439 } else { 440 goto done; 441 } 442 443 /* make sure that the link is valid */ 444 tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr); 445 if (tuple.code != PCMCIA_CISTPL_LINKTARGET) { 446 DPRINTF(("CISTPL_LINKTARGET expected, " 447 "code %02x observed\n", tuple.code)); 448 continue; 449 } 450 tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1); 451 if (tuple.length < 3) { 452 DPRINTF(("CISTPL_LINKTARGET too short %d\n", 453 tuple.length)); 454 continue; 455 } 456 if ((pcmcia_tuple_read_1(&tuple, 0) != 'C') || 457 (pcmcia_tuple_read_1(&tuple, 1) != 'I') || 458 (pcmcia_tuple_read_1(&tuple, 2) != 'S')) { 459 DPRINTF(("CISTPL_LINKTARGET magic " 460 "%02x%02x%02x incorrect\n", 461 pcmcia_tuple_read_1(&tuple, 0), 462 pcmcia_tuple_read_1(&tuple, 1), 463 pcmcia_tuple_read_1(&tuple, 2))); 464 continue; 465 } 466 tuple.ptr += 2 + tuple.length; 467 468 break; 469 } 470 } 471 472 pcmcia_chip_mem_unmap(pct, pch, window); 473 474 done: 475 /* Last, free the allocated memory block */ 476 pcmcia_chip_mem_free(pct, pch, &pcmh); 477 478 return (ret); 479 } 480 481 /* XXX this is incredibly verbose. Not sure what trt is */ 482 483 void 484 pcmcia_print_cis(sc) 485 struct pcmcia_softc *sc; 486 { 487 struct pcmcia_card *card = &sc->card; 488 struct pcmcia_function *pf; 489 struct pcmcia_config_entry *cfe; 490 int i; 491 492 printf("%s: CIS version ", sc->dev.dv_xname); 493 if (card->cis1_major == 4) { 494 if (card->cis1_minor == 0) 495 printf("PCMCIA 1.0\n"); 496 else if (card->cis1_minor == 1) 497 printf("PCMCIA 2.0 or 2.1\n"); 498 } else if (card->cis1_major >= 5) 499 printf("PC Card Standard %d.%d\n", card->cis1_major, 500 card->cis1_minor); 501 else 502 printf("unknown (major=%d, minor=%d)\n", 503 card->cis1_major, card->cis1_minor); 504 505 printf("%s: CIS info: ", sc->dev.dv_xname); 506 for (i = 0; i < 4; i++) { 507 if (card->cis1_info[i] == NULL) 508 break; 509 if (i) 510 printf(", "); 511 printf("%s", card->cis1_info[i]); 512 } 513 printf("\n"); 514 515 printf("%s: Manufacturer code 0x%x, product 0x%x\n", 516 sc->dev.dv_xname, card->manufacturer, card->product); 517 518 SIMPLEQ_FOREACH(pf, &card->pf_head, pf_list) { 519 printf("%s: function %d: ", sc->dev.dv_xname, pf->number); 520 521 switch (pf->function) { 522 case PCMCIA_FUNCTION_UNSPEC: 523 printf("unspecified"); 524 break; 525 case PCMCIA_FUNCTION_MULTIFUNCTION: 526 printf("multi-function"); 527 break; 528 case PCMCIA_FUNCTION_MEMORY: 529 printf("memory"); 530 break; 531 case PCMCIA_FUNCTION_SERIAL: 532 printf("serial port"); 533 break; 534 case PCMCIA_FUNCTION_PARALLEL: 535 printf("parallel port"); 536 break; 537 case PCMCIA_FUNCTION_DISK: 538 printf("fixed disk"); 539 break; 540 case PCMCIA_FUNCTION_VIDEO: 541 printf("video adapter"); 542 break; 543 case PCMCIA_FUNCTION_NETWORK: 544 printf("network adapter"); 545 break; 546 case PCMCIA_FUNCTION_AIMS: 547 printf("auto incrementing mass storage"); 548 break; 549 case PCMCIA_FUNCTION_SCSI: 550 printf("SCSI bridge"); 551 break; 552 case PCMCIA_FUNCTION_SECURITY: 553 printf("Security services"); 554 break; 555 case PCMCIA_FUNCTION_INSTRUMENT: 556 printf("Instrument"); 557 break; 558 case PCMCIA_FUNCTION_IOBUS: 559 printf("Serial I/O Bus Adapter"); 560 break; 561 default: 562 printf("unknown (%d)", pf->function); 563 break; 564 } 565 566 printf(", ccr addr %lx mask %lx\n", pf->ccr_base, pf->ccr_mask); 567 568 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 569 printf("%s: function %d, config table entry %d: ", 570 sc->dev.dv_xname, pf->number, cfe->number); 571 572 switch (cfe->iftype) { 573 case PCMCIA_IFTYPE_MEMORY: 574 printf("memory card"); 575 break; 576 case PCMCIA_IFTYPE_IO: 577 printf("I/O card"); 578 break; 579 default: 580 printf("card type unknown"); 581 break; 582 } 583 584 printf("; irq mask %x", cfe->irqmask); 585 586 if (cfe->num_iospace) { 587 printf("; iomask %lx, iospace", cfe->iomask); 588 589 for (i = 0; i < cfe->num_iospace; i++) 590 printf(" %lx%s%lx", 591 cfe->iospace[i].start, 592 cfe->iospace[i].length ? "-" : "", 593 cfe->iospace[i].start + 594 cfe->iospace[i].length - 1); 595 } 596 if (cfe->num_memspace) { 597 printf("; memspace"); 598 599 for (i = 0; i < cfe->num_memspace; i++) 600 printf(" %lx%s%lx%s%lx", 601 cfe->memspace[i].cardaddr, 602 cfe->memspace[i].length ? "-" : "", 603 cfe->memspace[i].cardaddr + 604 cfe->memspace[i].length - 1, 605 cfe->memspace[i].hostaddr ? 606 "@" : "", 607 cfe->memspace[i].hostaddr); 608 } 609 if (cfe->maxtwins) 610 printf("; maxtwins %d", cfe->maxtwins); 611 612 printf(";"); 613 614 if (cfe->flags & PCMCIA_CFE_MWAIT_REQUIRED) 615 printf(" mwait_required"); 616 if (cfe->flags & PCMCIA_CFE_RDYBSY_ACTIVE) 617 printf(" rdybsy_active"); 618 if (cfe->flags & PCMCIA_CFE_WP_ACTIVE) 619 printf(" wp_active"); 620 if (cfe->flags & PCMCIA_CFE_BVD_ACTIVE) 621 printf(" bvd_active"); 622 if (cfe->flags & PCMCIA_CFE_IO8) 623 printf(" io8"); 624 if (cfe->flags & PCMCIA_CFE_IO16) 625 printf(" io16"); 626 if (cfe->flags & PCMCIA_CFE_IRQSHARE) 627 printf(" irqshare"); 628 if (cfe->flags & PCMCIA_CFE_IRQPULSE) 629 printf(" irqpulse"); 630 if (cfe->flags & PCMCIA_CFE_IRQLEVEL) 631 printf(" irqlevel"); 632 if (cfe->flags & PCMCIA_CFE_POWERDOWN) 633 printf(" powerdown"); 634 if (cfe->flags & PCMCIA_CFE_READONLY) 635 printf(" readonly"); 636 if (cfe->flags & PCMCIA_CFE_AUDIO) 637 printf(" audio"); 638 639 printf("\n"); 640 } 641 } 642 643 if (card->error) 644 printf("%s: %d errors found while parsing CIS\n", 645 sc->dev.dv_xname, card->error); 646 } 647 648 int 649 pcmcia_parse_cis_tuple(tuple, arg) 650 struct pcmcia_tuple *tuple; 651 void *arg; 652 { 653 /* most of these are educated guesses */ 654 static struct pcmcia_config_entry init_cfe = { 655 -1, PCMCIA_CFE_RDYBSY_ACTIVE | PCMCIA_CFE_WP_ACTIVE | 656 PCMCIA_CFE_BVD_ACTIVE, PCMCIA_IFTYPE_MEMORY, 657 }; 658 659 struct cis_state *state = arg; 660 661 switch (tuple->code) { 662 case PCMCIA_CISTPL_END: 663 /* 664 * If we've seen a LONGLINK_MFC, and this is the first 665 * END after it, reset the function list. 666 * 667 * XXX This might also be the right place to start a 668 * new function, but that assumes that a function 669 * definition never crosses any longlink, and I'm not 670 * sure about that. This is probably safe for MFC 671 * cards, but what we have now isn't broken, so I'd 672 * rather not change it. 673 */ 674 if (state->gotmfc == 1) { 675 struct pcmcia_function *pf, *pfnext; 676 677 for (pf = SIMPLEQ_FIRST(&state->card->pf_head); 678 pf != NULL; pf = pfnext) { 679 pfnext = SIMPLEQ_NEXT(pf, pf_list); 680 free(pf, M_DEVBUF); 681 } 682 683 SIMPLEQ_INIT(&state->card->pf_head); 684 685 state->count = 0; 686 state->gotmfc = 2; 687 state->pf = NULL; 688 } 689 break; 690 691 case PCMCIA_CISTPL_LONGLINK_MFC: 692 /* 693 * This tuple's structure was dealt with in scan_cis. here, 694 * record the fact that the MFC tuple was seen, so that 695 * functions declared before the MFC link can be cleaned 696 * up. 697 */ 698 state->gotmfc = 1; 699 break; 700 701 #ifdef PCMCIACISDEBUG 702 case PCMCIA_CISTPL_DEVICE: 703 case PCMCIA_CISTPL_DEVICE_A: 704 { 705 u_int reg, dtype, dspeed; 706 707 reg = pcmcia_tuple_read_1(tuple, 0); 708 dtype = reg & PCMCIA_DTYPE_MASK; 709 dspeed = reg & PCMCIA_DSPEED_MASK; 710 711 DPRINTF(("CISTPL_DEVICE%s type=", 712 (tuple->code == PCMCIA_CISTPL_DEVICE) ? "" : "_A")); 713 switch (dtype) { 714 case PCMCIA_DTYPE_NULL: 715 DPRINTF(("null")); 716 break; 717 case PCMCIA_DTYPE_ROM: 718 DPRINTF(("rom")); 719 break; 720 case PCMCIA_DTYPE_OTPROM: 721 DPRINTF(("otprom")); 722 break; 723 case PCMCIA_DTYPE_EPROM: 724 DPRINTF(("eprom")); 725 break; 726 case PCMCIA_DTYPE_EEPROM: 727 DPRINTF(("eeprom")); 728 break; 729 case PCMCIA_DTYPE_FLASH: 730 DPRINTF(("flash")); 731 break; 732 case PCMCIA_DTYPE_SRAM: 733 DPRINTF(("sram")); 734 break; 735 case PCMCIA_DTYPE_DRAM: 736 DPRINTF(("dram")); 737 break; 738 case PCMCIA_DTYPE_FUNCSPEC: 739 DPRINTF(("funcspec")); 740 break; 741 case PCMCIA_DTYPE_EXTEND: 742 DPRINTF(("extend")); 743 break; 744 default: 745 DPRINTF(("reserved")); 746 break; 747 } 748 DPRINTF((" speed=")); 749 switch (dspeed) { 750 case PCMCIA_DSPEED_NULL: 751 DPRINTF(("null")); 752 break; 753 case PCMCIA_DSPEED_250NS: 754 DPRINTF(("250ns")); 755 break; 756 case PCMCIA_DSPEED_200NS: 757 DPRINTF(("200ns")); 758 break; 759 case PCMCIA_DSPEED_150NS: 760 DPRINTF(("150ns")); 761 break; 762 case PCMCIA_DSPEED_100NS: 763 DPRINTF(("100ns")); 764 break; 765 case PCMCIA_DSPEED_EXT: 766 DPRINTF(("ext")); 767 break; 768 default: 769 DPRINTF(("reserved")); 770 break; 771 } 772 } 773 DPRINTF(("\n")); 774 break; 775 #endif 776 777 case PCMCIA_CISTPL_VERS_1: 778 if (tuple->length < 6) { 779 DPRINTF(("CISTPL_VERS_1 too short %d\n", 780 tuple->length)); 781 break; 782 } { 783 int start, i, ch, count; 784 785 state->card->cis1_major = pcmcia_tuple_read_1(tuple, 0); 786 state->card->cis1_minor = pcmcia_tuple_read_1(tuple, 1); 787 788 for (count = 0, start = 0, i = 0; 789 (count < 4) && ((i + 4) < 256); i++) { 790 ch = pcmcia_tuple_read_1(tuple, 2 + i); 791 if (ch == 0xff) 792 break; 793 state->card->cis1_info_buf[i] = ch; 794 if (ch == 0) { 795 state->card->cis1_info[count] = 796 state->card->cis1_info_buf + start; 797 start = i + 1; 798 count++; 799 } 800 } 801 DPRINTF(("CISTPL_VERS_1\n")); 802 } 803 break; 804 805 case PCMCIA_CISTPL_MANFID: 806 if (tuple->length < 4) { 807 DPRINTF(("CISTPL_MANFID too short %d\n", 808 tuple->length)); 809 break; 810 } 811 state->card->manufacturer = pcmcia_tuple_read_2(tuple, 0); 812 state->card->product = pcmcia_tuple_read_2(tuple, 2); 813 DPRINTF(("CISTPL_MANFID\n")); 814 break; 815 816 case PCMCIA_CISTPL_FUNCID: 817 if (tuple->length < 2) { 818 DPRINTF(("CISTPL_FUNCID too short %d\n", 819 tuple->length)); 820 break; 821 } 822 823 /* 824 * As far as I understand this, manufacturers do multifunction 825 * cards in various ways. Sadly enough I do not have the 826 * PC-Card standard (donate!) so I can only guess what can 827 * be done. 828 * The original code implies FUNCID nodes are above CONFIG 829 * nodes in the CIS tree, however Xircom does it the other 830 * way round, which of course makes things a bit hard. 831 * --niklas@openbsd.org 832 */ 833 if (state->pf) { 834 if (state->pf->function == PCMCIA_FUNCTION_UNSPEC) { 835 /* 836 * This looks like a opportunistic function 837 * created by a CONFIG tuple. Just keep it. 838 */ 839 } else { 840 /* 841 * A function is being defined, end it. 842 */ 843 state->pf = NULL; 844 } 845 } 846 if (state->pf == NULL) { 847 state->pf = malloc(sizeof(*state->pf), M_DEVBUF, 848 M_NOWAIT | M_ZERO); 849 if (state->pf == NULL) 850 panic("pcmcia_parse_cis_tuple"); 851 state->pf->number = state->count++; 852 state->pf->last_config_index = -1; 853 SIMPLEQ_INIT(&state->pf->cfe_head); 854 855 SIMPLEQ_INSERT_TAIL(&state->card->pf_head, state->pf, 856 pf_list); 857 } 858 state->pf->function = pcmcia_tuple_read_1(tuple, 0); 859 860 DPRINTF(("CISTPL_FUNCID\n")); 861 break; 862 863 case PCMCIA_CISTPL_CONFIG: 864 if (tuple->length < 3) { 865 DPRINTF(("CISTPL_CONFIG too short %d\n", 866 tuple->length)); 867 break; 868 } { 869 u_int reg, rasz, rmsz, rfsz; 870 int i; 871 872 reg = pcmcia_tuple_read_1(tuple, 0); 873 rasz = 1 + ((reg & PCMCIA_TPCC_RASZ_MASK) >> 874 PCMCIA_TPCC_RASZ_SHIFT); 875 rmsz = 1 + ((reg & PCMCIA_TPCC_RMSZ_MASK) >> 876 PCMCIA_TPCC_RMSZ_SHIFT); 877 rfsz = ((reg & PCMCIA_TPCC_RFSZ_MASK) >> 878 PCMCIA_TPCC_RFSZ_SHIFT); 879 880 if (tuple->length < (rasz + rmsz + rfsz)) { 881 DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too " 882 "short %d\n", rasz, rmsz, rfsz, 883 tuple->length)); 884 break; 885 } 886 if (state->pf == NULL) { 887 state->pf = malloc(sizeof(*state->pf), 888 M_DEVBUF, M_NOWAIT | M_ZERO); 889 if (state->pf == NULL) 890 panic("pcmcia_parse_cis_tuple"); 891 state->pf->number = state->count++; 892 state->pf->last_config_index = -1; 893 SIMPLEQ_INIT(&state->pf->cfe_head); 894 895 SIMPLEQ_INSERT_TAIL(&state->card->pf_head, 896 state->pf, pf_list); 897 898 state->pf->function = PCMCIA_FUNCTION_UNSPEC; 899 } 900 state->pf->last_config_index = 901 pcmcia_tuple_read_1(tuple, 1); 902 903 state->pf->ccr_base = 0; 904 for (i = 0; i < rasz; i++) 905 state->pf->ccr_base |= 906 ((pcmcia_tuple_read_1(tuple, 2 + i)) << 907 (i * 8)); 908 909 state->pf->ccr_mask = 0; 910 for (i = 0; i < rmsz; i++) 911 state->pf->ccr_mask |= 912 ((pcmcia_tuple_read_1(tuple, 913 2 + rasz + i)) << (i * 8)); 914 915 /* skip the reserved area and subtuples */ 916 917 /* reset the default cfe for each cfe list */ 918 state->temp_cfe = init_cfe; 919 state->default_cfe = &state->temp_cfe; 920 } 921 DPRINTF(("CISTPL_CONFIG\n")); 922 break; 923 924 case PCMCIA_CISTPL_CFTABLE_ENTRY: 925 if (tuple->length < 2) { 926 DPRINTF(("CISTPL_CFTABLE_ENTRY too short %d\n", 927 tuple->length)); 928 break; 929 } { 930 int idx, i, j; 931 u_int reg, reg2; 932 u_int intface, def, num; 933 u_int power, timing, iospace, irq, memspace, misc; 934 struct pcmcia_config_entry *cfe; 935 936 idx = 0; 937 938 reg = pcmcia_tuple_read_1(tuple, idx); 939 idx++; 940 intface = reg & PCMCIA_TPCE_INDX_INTFACE; 941 def = reg & PCMCIA_TPCE_INDX_DEFAULT; 942 num = reg & PCMCIA_TPCE_INDX_NUM_MASK; 943 944 /* 945 * this is a little messy. Some cards have only a 946 * cfentry with the default bit set. So, as we go 947 * through the list, we add new indexes to the queue, 948 * and keep a pointer to the last one with the 949 * default bit set. if we see a record with the same 950 * index, as the default, we stash the default and 951 * replace the queue entry. otherwise, we just add 952 * new entries to the queue, pointing the default ptr 953 * at them if the default bit is set. if we get to 954 * the end with the default pointer pointing at a 955 * record which hasn't had a matching index, that's 956 * ok; it just becomes a cfentry like any other. 957 */ 958 959 /* 960 * if the index in the cis differs from the default 961 * cis, create new entry in the queue and start it 962 * with the current default 963 */ 964 if (state->default_cfe == NULL) { 965 DPRINTF(("CISTPL_CFTABLE_ENTRY with no " 966 "default\n")); 967 break; 968 } 969 if (num != state->default_cfe->number) { 970 cfe = (struct pcmcia_config_entry *) 971 malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT); 972 if (cfe == NULL) 973 panic("pcmcia_parse_cis_tuple"); 974 975 *cfe = *state->default_cfe; 976 977 SIMPLEQ_INSERT_TAIL(&state->pf->cfe_head, 978 cfe, cfe_list); 979 980 cfe->number = num; 981 982 /* 983 * if the default bit is set in the cis, then 984 * point the new default at whatever is being 985 * filled in 986 */ 987 if (def) 988 state->default_cfe = cfe; 989 } else { 990 /* 991 * the cis index matches the default index, 992 * fill in the default cfentry. It is 993 * assumed that the cfdefault index is in the 994 * queue. For it to be otherwise, the cis 995 * index would have to be -1 (initial 996 * condition) which is not possible, or there 997 * would have to be a preceding cis entry 998 * which had the same cis index and had the 999 * default bit unset. Neither condition 1000 * should happen. If it does, this cfentry 1001 * is lost (written into temp space), which 1002 * is an acceptable failure mode. 1003 */ 1004 1005 cfe = state->default_cfe; 1006 1007 /* 1008 * if the cis entry does not have the default 1009 * bit set, copy the default out of the way 1010 * first. 1011 */ 1012 if (!def) { 1013 state->temp_cfe = *state->default_cfe; 1014 state->default_cfe = &state->temp_cfe; 1015 } 1016 } 1017 1018 if (intface) { 1019 reg = pcmcia_tuple_read_1(tuple, idx); 1020 idx++; 1021 cfe->flags &= ~(PCMCIA_CFE_MWAIT_REQUIRED 1022 | PCMCIA_CFE_RDYBSY_ACTIVE 1023 | PCMCIA_CFE_WP_ACTIVE 1024 | PCMCIA_CFE_BVD_ACTIVE); 1025 if (reg & PCMCIA_TPCE_IF_MWAIT) 1026 cfe->flags |= PCMCIA_CFE_MWAIT_REQUIRED; 1027 if (reg & PCMCIA_TPCE_IF_RDYBSY) 1028 cfe->flags |= PCMCIA_CFE_RDYBSY_ACTIVE; 1029 if (reg & PCMCIA_TPCE_IF_WP) 1030 cfe->flags |= PCMCIA_CFE_WP_ACTIVE; 1031 if (reg & PCMCIA_TPCE_IF_BVD) 1032 cfe->flags |= PCMCIA_CFE_BVD_ACTIVE; 1033 cfe->iftype = reg & PCMCIA_TPCE_IF_IFTYPE; 1034 } 1035 reg = pcmcia_tuple_read_1(tuple, idx); 1036 idx++; 1037 1038 power = reg & PCMCIA_TPCE_FS_POWER_MASK; 1039 timing = reg & PCMCIA_TPCE_FS_TIMING; 1040 iospace = reg & PCMCIA_TPCE_FS_IOSPACE; 1041 irq = reg & PCMCIA_TPCE_FS_IRQ; 1042 memspace = reg & PCMCIA_TPCE_FS_MEMSPACE_MASK; 1043 misc = reg & PCMCIA_TPCE_FS_MISC; 1044 1045 if (power) { 1046 /* skip over power, don't save */ 1047 /* for each parameter selection byte */ 1048 for (i = 0; i < power; i++) { 1049 reg = pcmcia_tuple_read_1(tuple, idx); 1050 idx++; 1051 /* for each bit */ 1052 for (j = 0; j < 7; j++) { 1053 /* if the bit is set */ 1054 if ((reg >> j) & 0x01) { 1055 /* skip over bytes */ 1056 do { 1057 reg2 = pcmcia_tuple_read_1(tuple, idx); 1058 idx++; 1059 /* 1060 * until 1061 * non- 1062 * extension 1063 * byte 1064 */ 1065 } while (reg2 & 0x80); 1066 } 1067 } 1068 } 1069 } 1070 if (timing) { 1071 /* skip over timing, don't save */ 1072 reg = pcmcia_tuple_read_1(tuple, idx); 1073 idx++; 1074 1075 if ((reg & PCMCIA_TPCE_TD_RESERVED_MASK) != 1076 PCMCIA_TPCE_TD_RESERVED_MASK) 1077 idx++; 1078 if ((reg & PCMCIA_TPCE_TD_RDYBSY_MASK) != 1079 PCMCIA_TPCE_TD_RDYBSY_MASK) 1080 idx++; 1081 if ((reg & PCMCIA_TPCE_TD_WAIT_MASK) != 1082 PCMCIA_TPCE_TD_WAIT_MASK) 1083 idx++; 1084 } 1085 if (iospace) { 1086 if (tuple->length <= idx) { 1087 DPRINTF(("ran out of space before TPCE_IO\n")); 1088 1089 goto abort_cfe; 1090 } 1091 1092 reg = pcmcia_tuple_read_1(tuple, idx); 1093 idx++; 1094 1095 cfe->flags &= 1096 ~(PCMCIA_CFE_IO8 | PCMCIA_CFE_IO16); 1097 if (reg & PCMCIA_TPCE_IO_BUSWIDTH_8BIT) 1098 cfe->flags |= PCMCIA_CFE_IO8; 1099 if (reg & PCMCIA_TPCE_IO_BUSWIDTH_16BIT) 1100 cfe->flags |= PCMCIA_CFE_IO16; 1101 cfe->iomask = 1102 reg & PCMCIA_TPCE_IO_IOADDRLINES_MASK; 1103 1104 if (reg & PCMCIA_TPCE_IO_HASRANGE) { 1105 reg = pcmcia_tuple_read_1(tuple, idx); 1106 idx++; 1107 1108 cfe->num_iospace = 1 + (reg & 1109 PCMCIA_TPCE_IO_RANGE_COUNT); 1110 1111 if (cfe->num_iospace > 1112 (sizeof(cfe->iospace) / 1113 sizeof(cfe->iospace[0]))) { 1114 DPRINTF(("too many io " 1115 "spaces %d", 1116 cfe->num_iospace)); 1117 state->card->error++; 1118 break; 1119 } 1120 for (i = 0; i < cfe->num_iospace; i++) { 1121 switch (reg & PCMCIA_TPCE_IO_RANGE_ADDRSIZE_MASK) { 1122 case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_ONE: 1123 cfe->iospace[i].start = 1124 pcmcia_tuple_read_1(tuple, idx); 1125 idx++; 1126 break; 1127 case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_TWO: 1128 cfe->iospace[i].start = 1129 pcmcia_tuple_read_2(tuple, idx); 1130 idx += 2; 1131 break; 1132 case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_FOUR: 1133 cfe->iospace[i].start = 1134 pcmcia_tuple_read_4(tuple, idx); 1135 idx += 4; 1136 break; 1137 } 1138 switch (reg & 1139 PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_MASK) { 1140 case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_ONE: 1141 cfe->iospace[i].length = 1142 pcmcia_tuple_read_1(tuple, idx); 1143 idx++; 1144 break; 1145 case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_TWO: 1146 cfe->iospace[i].length = 1147 pcmcia_tuple_read_2(tuple, idx); 1148 idx += 2; 1149 break; 1150 case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_FOUR: 1151 cfe->iospace[i].length = 1152 pcmcia_tuple_read_4(tuple, idx); 1153 idx += 4; 1154 break; 1155 } 1156 cfe->iospace[i].length++; 1157 } 1158 } else { 1159 cfe->num_iospace = 1; 1160 cfe->iospace[0].start = 0; 1161 cfe->iospace[0].length = 1162 (1 << cfe->iomask); 1163 } 1164 } 1165 1166 if (irq) { 1167 if (tuple->length <= idx) { 1168 DPRINTF(("ran out of space before TPCE_IR\n")); 1169 1170 goto abort_cfe; 1171 } 1172 1173 reg = pcmcia_tuple_read_1(tuple, idx); 1174 idx++; 1175 1176 cfe->flags &= ~(PCMCIA_CFE_IRQSHARE 1177 | PCMCIA_CFE_IRQPULSE 1178 | PCMCIA_CFE_IRQLEVEL); 1179 if (reg & PCMCIA_TPCE_IR_SHARE) 1180 cfe->flags |= PCMCIA_CFE_IRQSHARE; 1181 if (reg & PCMCIA_TPCE_IR_PULSE) 1182 cfe->flags |= PCMCIA_CFE_IRQPULSE; 1183 if (reg & PCMCIA_TPCE_IR_LEVEL) 1184 cfe->flags |= PCMCIA_CFE_IRQLEVEL; 1185 1186 if (reg & PCMCIA_TPCE_IR_HASMASK) { 1187 /* 1188 * it's legal to ignore the 1189 * special-interrupt bits, so I will 1190 */ 1191 1192 cfe->irqmask = 1193 pcmcia_tuple_read_2(tuple, idx); 1194 idx += 2; 1195 } else { 1196 cfe->irqmask = 1197 (1 << (reg & PCMCIA_TPCE_IR_IRQ)); 1198 } 1199 } 1200 if (memspace) { 1201 if (tuple->length <= idx) { 1202 DPRINTF(("ran out of space before TPCE_MS\n")); 1203 goto abort_cfe; 1204 } 1205 1206 if (memspace == PCMCIA_TPCE_FS_MEMSPACE_NONE) { 1207 cfe->num_memspace = 0; 1208 } else if (memspace == PCMCIA_TPCE_FS_MEMSPACE_LENGTH) { 1209 cfe->num_memspace = 1; 1210 cfe->memspace[0].length = 256 * 1211 pcmcia_tuple_read_2(tuple, idx); 1212 idx += 2; 1213 cfe->memspace[0].cardaddr = 0; 1214 cfe->memspace[0].hostaddr = 0; 1215 } else if (memspace == 1216 PCMCIA_TPCE_FS_MEMSPACE_LENGTHADDR) { 1217 cfe->num_memspace = 1; 1218 cfe->memspace[0].length = 256 * 1219 pcmcia_tuple_read_2(tuple, idx); 1220 idx += 2; 1221 cfe->memspace[0].cardaddr = 256 * 1222 pcmcia_tuple_read_2(tuple, idx); 1223 idx += 2; 1224 cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr; 1225 } else { 1226 int lengthsize; 1227 int cardaddrsize; 1228 int hostaddrsize; 1229 1230 reg = pcmcia_tuple_read_1(tuple, idx); 1231 idx++; 1232 1233 cfe->num_memspace = (reg & 1234 PCMCIA_TPCE_MS_COUNT) + 1; 1235 1236 if (cfe->num_memspace > 1237 (sizeof(cfe->memspace) / 1238 sizeof(cfe->memspace[0]))) { 1239 DPRINTF(("too many mem " 1240 "spaces %d", 1241 cfe->num_memspace)); 1242 state->card->error++; 1243 break; 1244 } 1245 lengthsize = 1246 ((reg & PCMCIA_TPCE_MS_LENGTH_SIZE_MASK) >> 1247 PCMCIA_TPCE_MS_LENGTH_SIZE_SHIFT); 1248 cardaddrsize = 1249 ((reg & PCMCIA_TPCE_MS_CARDADDR_SIZE_MASK) >> 1250 PCMCIA_TPCE_MS_CARDADDR_SIZE_SHIFT); 1251 hostaddrsize = 1252 (reg & PCMCIA_TPCE_MS_HOSTADDR) ? cardaddrsize : 0; 1253 1254 if (lengthsize == 0) { 1255 DPRINTF(("cfe memspace " 1256 "lengthsize == 0")); 1257 state->card->error++; 1258 } 1259 for (i = 0; i < cfe->num_memspace; i++) { 1260 if (lengthsize) { 1261 cfe->memspace[i].length = 1262 256 * pcmcia_tuple_read_n(tuple, lengthsize, 1263 idx); 1264 idx += lengthsize; 1265 } else { 1266 cfe->memspace[i].length = 0; 1267 } 1268 if (cfe->memspace[i].length == 0) { 1269 DPRINTF(("cfe->memspace[%d].length == 0", 1270 i)); 1271 state->card->error++; 1272 } 1273 if (cardaddrsize) { 1274 cfe->memspace[i].cardaddr = 1275 256 * pcmcia_tuple_read_n(tuple, cardaddrsize, 1276 idx); 1277 idx += cardaddrsize; 1278 } else { 1279 cfe->memspace[i].cardaddr = 0; 1280 } 1281 if (hostaddrsize) { 1282 cfe->memspace[i].hostaddr = 1283 256 * pcmcia_tuple_read_n(tuple, hostaddrsize, 1284 idx); 1285 idx += hostaddrsize; 1286 } else { 1287 cfe->memspace[i].hostaddr = 0; 1288 } 1289 } 1290 } 1291 } 1292 if (misc) { 1293 if (tuple->length <= idx) { 1294 DPRINTF(("ran out of space before TPCE_MI\n")); 1295 1296 goto abort_cfe; 1297 } 1298 1299 reg = pcmcia_tuple_read_1(tuple, idx); 1300 idx++; 1301 1302 cfe->flags &= ~(PCMCIA_CFE_POWERDOWN 1303 | PCMCIA_CFE_READONLY 1304 | PCMCIA_CFE_AUDIO); 1305 if (reg & PCMCIA_TPCE_MI_PWRDOWN) 1306 cfe->flags |= PCMCIA_CFE_POWERDOWN; 1307 if (reg & PCMCIA_TPCE_MI_READONLY) 1308 cfe->flags |= PCMCIA_CFE_READONLY; 1309 if (reg & PCMCIA_TPCE_MI_AUDIO) 1310 cfe->flags |= PCMCIA_CFE_AUDIO; 1311 cfe->maxtwins = reg & PCMCIA_TPCE_MI_MAXTWINS; 1312 1313 while (reg & PCMCIA_TPCE_MI_EXT) { 1314 reg = pcmcia_tuple_read_1(tuple, idx); 1315 idx++; 1316 } 1317 } 1318 /* skip all the subtuples */ 1319 } 1320 1321 abort_cfe: 1322 DPRINTF(("CISTPL_CFTABLE_ENTRY\n")); 1323 break; 1324 1325 default: 1326 DPRINTF(("unhandled CISTPL %x\n", tuple->code)); 1327 break; 1328 } 1329 1330 return (0); 1331 } 1332