1 /* $NetBSD: pcmcia_cis.c,v 1.17 2000/02/10 09:01:52 chopps Exp $ */
5 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
42 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <dev/pccard/pccardreg.h>
47 #include <dev/pccard/pccardvar.h>
48 #include <dev/pccard/pccardvarp.h>
49 #include <dev/pccard/pccard_cis.h>
53 extern int pccard_cis_debug;
55 #define PCCARDCISDEBUG
57 #define DPRINTF(arg) do { if (pccard_cis_debug) printf arg; } while (0)
58 #define DEVPRINTF(arg) do { if (pccard_cis_debug) device_printf arg; } while (0)
61 #define DEVPRINTF(arg)
64 #define PCCARD_CIS_SIZE 4096
69 struct pccard_config_entry temp_cfe;
70 struct pccard_config_entry *default_cfe;
71 struct pccard_card *card;
72 struct pccard_function *pf;
75 static int pccard_parse_cis_tuple(const struct pccard_tuple *, void *);
76 static int decode_funce(const struct pccard_tuple *, struct pccard_function *);
79 pccard_read_cis(struct pccard_softc *sc)
81 struct cis_state state;
83 bzero(&state, sizeof state);
84 state.card = &sc->card;
85 state.card->error = 0;
86 state.card->cis1_major = -1;
87 state.card->cis1_minor = -1;
88 state.card->cis1_info[0] = NULL;
89 state.card->cis1_info[1] = NULL;
90 state.card->cis1_info[2] = NULL;
91 state.card->cis1_info[3] = NULL;
92 state.card->manufacturer = PCMCIA_VENDOR_INVALID;
93 state.card->product = PCMCIA_PRODUCT_INVALID;
94 STAILQ_INIT(&state.card->pf_head);
98 * XXX The following shouldn't be needed, but some slow cards
99 * XXX seem to need it still. Need to investigate if there's
100 * XXX a way to tell if the card is 'ready' or not rather than
101 * XXX sleeping like this. We're called just after the power
102 * XXX up of the socket. The standard timing diagrams don't
103 * XXX seem to indicate that a delay is required. The old
104 * XXX delay was 1s. This delay is .1s.
106 pause("pccard", hz / 10);
107 if (pccard_scan_cis(device_get_parent(sc->dev), sc->dev,
108 pccard_parse_cis_tuple, &state) == -1)
113 pccard_scan_cis(device_t bus, device_t dev, pccard_scan_t fct, void *arg)
115 struct resource *res;
117 struct pccard_tuple tuple;
118 int longlink_present;
120 u_long longlink_addr; /* Type suspect */
123 #ifdef PCCARDCISDEBUG
124 int cis_none_cnt = 10; /* Only report 10 CIS_NONEs */
134 /* allocate some memory */
137 * Some reports from the field suggest that a 64k memory boundary
138 * helps card CIS being able to be read. Try it here and see what
139 * the results actually are. I'm not sure I understand why this
140 * would make cards work better, but it is easy enough to test.
143 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0,
144 PCCARD_CIS_SIZE, RF_ACTIVE | rman_make_alignment_flags(64*1024));
146 device_printf(dev, "can't alloc memory to read attributes\n");
149 CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
150 tuple.memt = rman_get_bustag(res);
151 tuple.memh = rman_get_bushandle(res);
154 DPRINTF(("cis mem map 0x%x (resource: 0x%lx)\n",
155 (unsigned int) tuple.memh, rman_get_start(res)));
159 longlink_present = 1;
166 DEVPRINTF((dev, "CIS tuple chain:\n"));
171 * Perform boundary check for insane cards.
172 * If CIS is too long, simulate CIS end.
173 * (This check may not be sufficient for
176 if (tuple.mult * tuple.ptr >= PCCARD_CIS_SIZE - 1
177 - 32 /* ad hoc value */ ) {
178 printf("CIS is too long -- truncating\n");
179 tuple.code = CISTPL_END;
181 /* get the tuple code */
182 tuple.code = pccard_cis_read_1(&tuple, tuple.ptr);
185 /* two special-case tuples */
187 if (tuple.code == CISTPL_NULL) {
188 #ifdef PCCARDCISDEBUG
189 if (cis_none_cnt > 0)
190 DPRINTF(("CISTPL_NONE\n 00\n"));
191 else if (cis_none_cnt == 0)
192 DPRINTF(("TOO MANY CIS_NONE\n"));
195 if ((*fct)(&tuple, arg)) {
201 } else if (tuple.code == CISTPL_END) {
202 DPRINTF(("CISTPL_END\n ff\n"));
203 /* Call the function for the END tuple, since
204 the CIS semantics depend on it */
205 if ((*fct)(&tuple, arg)) {
212 /* now all the normal tuples */
214 tuple.length = pccard_cis_read_1(&tuple, tuple.ptr + 1);
215 switch (tuple.code) {
216 case CISTPL_LONGLINK_A:
217 case CISTPL_LONGLINK_C:
218 if ((*fct)(&tuple, arg)) {
222 if (tuple.length < 4) {
223 DPRINTF(("CISTPL_LONGLINK_%s too "
225 longlink_common ? "C" : "A",
229 longlink_present = 1;
230 longlink_common = (tuple.code ==
231 CISTPL_LONGLINK_C) ? 1 : 0;
232 longlink_addr = pccard_tuple_read_4(&tuple, 0);
233 DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
234 longlink_common ? "C" : "A",
238 if ((*fct)(&tuple, arg)) {
242 longlink_present = 0;
243 DPRINTF(("CISTPL_NO_LINK\n"));
245 case CISTPL_CHECKSUM:
246 if ((*fct)(&tuple, arg)) {
250 if (tuple.length < 5) {
251 DPRINTF(("CISTPL_CHECKSUM too "
252 "short %d\n", tuple.length));
261 pccard_tuple_read_2(&tuple, 0);
262 length = pccard_tuple_read_2(&tuple, 2);
263 cksum = pccard_tuple_read_1(&tuple, 4);
265 addr = tuple.ptr + offset;
267 DPRINTF(("CISTPL_CHECKSUM addr=%lx "
269 addr, length, cksum));
272 * XXX do more work to deal with
275 if ((addr >= PCCARD_CIS_SIZE) ||
278 DPRINTF((" skipped, "
283 for (i = 0; i < length; i++)
285 bus_space_read_1(tuple.memt,
287 addr + tuple.mult * i);
288 if (cksum != (sum & 0xff)) {
289 DPRINTF((" failed sum=%x\n",
292 "CIS checksum failed\n");
295 * XXX Some working cards have
296 * XXX bad checksums!!
305 case CISTPL_LONGLINK_MFC:
306 if (tuple.length < 1) {
307 DPRINTF(("CISTPL_LONGLINK_MFC too "
308 "short %d\n", tuple.length));
311 if (((tuple.length - 1) % 5) != 0) {
312 DPRINTF(("CISTPL_LONGLINK_MFC bogus "
313 "length %d\n", tuple.length));
317 * this is kind of ad hoc, as I don't have
318 * any real documentation
324 * put count into tmp var so that
325 * if we have to bail (because it's
326 * a bogus count) it won't be
327 * remembered for later use.
330 pccard_tuple_read_1(&tuple, 0);
332 DPRINTF(("CISTPL_LONGLINK_MFC %d",
336 * make _sure_ it's the right size;
337 * if too short, it may be a weird
338 * (unknown/undefined) format
340 if (tuple.length != (tmp_count*5 + 1)) {
341 DPRINTF((" bogus length %d\n",
346 * sanity check for a programming
347 * error which is difficult to find
351 howmany(sizeof mfc, sizeof mfc[0]))
352 panic("CISTPL_LONGLINK_MFC mfc "
353 "count would blow stack");
354 mfc_count = tmp_count;
355 for (i = 0; i < mfc_count; i++) {
357 (pccard_tuple_read_1(&tuple,
359 PCCARD_MFC_MEM_COMMON) ?
362 pccard_tuple_read_4(&tuple,
365 mfc[i].common ? "common" :
366 "attr", mfc[i].addr));
371 * for LONGLINK_MFC, fall through to the
372 * function. This tuple has structural and
377 if ((*fct)(&tuple, arg)) {
384 #ifdef PCCARDCISDEBUG
385 /* print the tuple */
389 DPRINTF((" %02x %02x", tuple.code,
392 for (i = 0; i < tuple.length; i++) {
394 pccard_tuple_read_1(&tuple, i)));
403 /* skip to the next tuple */
404 tuple.ptr += 2 + tuple.length;
408 * the chain is done. Clean up and move onto the next one,
409 * if any. The loop is here in the case that there is an MFC
410 * card with no longlink (which defaults to existing, == 0).
411 * In general, this means that if one pointer fails, it will
412 * try the next one, instead of just bailing.
415 if (longlink_present) {
416 CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY,
417 rid, longlink_common ?
418 PCCARD_A_MEM_COM : PCCARD_A_MEM_ATTR);
419 DPRINTF(("cis mem map %x\n",
420 (unsigned int) tuple.memh));
421 tuple.mult = longlink_common ? 1 : 2;
422 tuple.ptr = longlink_addr;
423 longlink_present = 0;
426 } else if (mfc_count && (mfc_index < mfc_count)) {
427 CARD_SET_RES_FLAGS(bus, dev, SYS_RES_MEMORY,
428 rid, mfc[mfc_index].common ?
429 PCCARD_A_MEM_COM : PCCARD_A_MEM_ATTR);
430 DPRINTF(("cis mem map %x\n",
431 (unsigned int) tuple.memh));
432 /* set parse state, and point at the next one */
433 tuple.mult = mfc[mfc_index].common ? 1 : 2;
434 tuple.ptr = mfc[mfc_index].addr;
440 /* make sure that the link is valid */
441 tuple.code = pccard_cis_read_1(&tuple, tuple.ptr);
442 if (tuple.code != CISTPL_LINKTARGET) {
443 DPRINTF(("CISTPL_LINKTARGET expected, "
444 "code %02x observed\n", tuple.code));
447 tuple.length = pccard_cis_read_1(&tuple, tuple.ptr + 1);
448 if (tuple.length < 3) {
449 DPRINTF(("CISTPL_LINKTARGET too short %d\n",
453 if ((pccard_tuple_read_1(&tuple, 0) != 'C') ||
454 (pccard_tuple_read_1(&tuple, 1) != 'I') ||
455 (pccard_tuple_read_1(&tuple, 2) != 'S')) {
456 DPRINTF(("CISTPL_LINKTARGET magic "
457 "%02x%02x%02x incorrect\n",
458 pccard_tuple_read_1(&tuple, 0),
459 pccard_tuple_read_1(&tuple, 1),
460 pccard_tuple_read_1(&tuple, 2)));
463 tuple.ptr += 2 + tuple.length;
469 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
474 /* XXX this is incredibly verbose. Not sure what trt is */
477 pccard_print_cis(device_t dev)
479 struct pccard_softc *sc = PCCARD_SOFTC(dev);
480 struct pccard_card *card = &sc->card;
481 struct pccard_function *pf;
482 struct pccard_config_entry *cfe;
485 device_printf(dev, "CIS version ");
486 if (card->cis1_major == 4) {
487 if (card->cis1_minor == 0)
488 printf("PCCARD 1.0\n");
489 else if (card->cis1_minor == 1)
490 printf("PCCARD 2.0 or 2.1\n");
491 } else if (card->cis1_major >= 5)
492 printf("PC Card Standard %d.%d\n", card->cis1_major, card->cis1_minor);
494 printf("unknown (major=%d, minor=%d)\n",
495 card->cis1_major, card->cis1_minor);
497 device_printf(dev, "CIS info: ");
498 for (i = 0; i < 4; i++) {
499 if (card->cis1_info[i] == NULL)
503 printf("%s", card->cis1_info[i]);
507 device_printf(dev, "Manufacturer code 0x%x, product 0x%x\n",
508 card->manufacturer, card->product);
510 STAILQ_FOREACH(pf, &card->pf_head, pf_list) {
511 device_printf(dev, "function %d: ", pf->number);
513 switch (pf->function) {
514 case PCCARD_FUNCTION_UNSPEC:
515 printf("unspecified");
517 case PCCARD_FUNCTION_MULTIFUNCTION:
518 printf("multi-function");
520 case PCCARD_FUNCTION_MEMORY:
523 case PCCARD_FUNCTION_SERIAL:
524 printf("serial port");
526 case PCCARD_FUNCTION_PARALLEL:
527 printf("parallel port");
529 case PCCARD_FUNCTION_DISK:
530 printf("fixed disk");
532 case PCCARD_FUNCTION_VIDEO:
533 printf("video adapter");
535 case PCCARD_FUNCTION_NETWORK:
536 printf("network adapter");
538 case PCCARD_FUNCTION_AIMS:
539 printf("auto incrementing mass storage");
541 case PCCARD_FUNCTION_SCSI:
542 printf("SCSI bridge");
544 case PCCARD_FUNCTION_SECURITY:
545 printf("Security services");
547 case PCCARD_FUNCTION_INSTRUMENT:
548 printf("Instrument");
551 printf("unknown (%d)", pf->function);
555 printf(", ccr addr %x mask %x\n", pf->ccr_base, pf->ccr_mask);
557 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
558 device_printf(dev, "function %d, config table entry "
559 "%d: ", pf->number, cfe->number);
561 switch (cfe->iftype) {
562 case PCCARD_IFTYPE_MEMORY:
563 printf("memory card");
565 case PCCARD_IFTYPE_IO:
569 printf("card type unknown");
573 printf("; irq mask %x", cfe->irqmask);
575 if (cfe->num_iospace) {
576 printf("; iomask %lx, iospace", cfe->iomask);
578 for (i = 0; i < cfe->num_iospace; i++) {
579 printf(" %lx", cfe->iospace[i].start);
580 if (cfe->iospace[i].length)
582 cfe->iospace[i].start +
583 cfe->iospace[i].length - 1);
586 if (cfe->num_memspace) {
587 printf("; memspace");
589 for (i = 0; i < cfe->num_memspace; i++) {
591 cfe->memspace[i].cardaddr);
592 if (cfe->memspace[i].length)
594 cfe->memspace[i].cardaddr +
595 cfe->memspace[i].length - 1);
596 if (cfe->memspace[i].hostaddr)
598 cfe->memspace[i].hostaddr);
602 printf("; maxtwins %d", cfe->maxtwins);
606 if (cfe->flags & PCCARD_CFE_MWAIT_REQUIRED)
607 printf(" mwait_required");
608 if (cfe->flags & PCCARD_CFE_RDYBSY_ACTIVE)
609 printf(" rdybsy_active");
610 if (cfe->flags & PCCARD_CFE_WP_ACTIVE)
611 printf(" wp_active");
612 if (cfe->flags & PCCARD_CFE_BVD_ACTIVE)
613 printf(" bvd_active");
614 if (cfe->flags & PCCARD_CFE_IO8)
616 if (cfe->flags & PCCARD_CFE_IO16)
618 if (cfe->flags & PCCARD_CFE_IRQSHARE)
620 if (cfe->flags & PCCARD_CFE_IRQPULSE)
622 if (cfe->flags & PCCARD_CFE_IRQLEVEL)
624 if (cfe->flags & PCCARD_CFE_POWERDOWN)
625 printf(" powerdown");
626 if (cfe->flags & PCCARD_CFE_READONLY)
628 if (cfe->flags & PCCARD_CFE_AUDIO)
636 device_printf(dev, "%d errors found while parsing CIS\n",
641 pccard_parse_cis_tuple(const struct pccard_tuple *tuple, void *arg)
643 /* most of these are educated guesses */
644 static struct pccard_config_entry init_cfe = {
645 -1, PCCARD_CFE_RDYBSY_ACTIVE | PCCARD_CFE_WP_ACTIVE |
646 PCCARD_CFE_BVD_ACTIVE, PCCARD_IFTYPE_MEMORY,
649 struct cis_state *state = arg;
651 switch (tuple->code) {
653 /* if we've seen a LONGLINK_MFC, and this is the first
654 * END after it, reset the function list.
656 * XXX This might also be the right place to start a
657 * new function, but that assumes that a function
658 * definition never crosses any longlink, and I'm not
659 * sure about that. This is probably safe for MFC
660 * cards, but what we have now isn't broken, so I'd
661 * rather not change it.
663 if (state->gotmfc == 1) {
664 struct pccard_function *pf, *pfnext;
666 for (pf = STAILQ_FIRST(&state->card->pf_head);
667 pf != NULL; pf = pfnext) {
668 pfnext = STAILQ_NEXT(pf, pf_list);
672 STAILQ_INIT(&state->card->pf_head);
679 case CISTPL_LONGLINK_MFC:
681 * this tuple's structure was dealt with in scan_cis. here,
682 * record the fact that the MFC tuple was seen, so that
683 * functions declared before the MFC link can be cleaned
688 #ifdef PCCARDCISDEBUG
690 case CISTPL_DEVICE_A:
692 u_int reg, dtype, dspeed;
694 reg = pccard_tuple_read_1(tuple, 0);
695 dtype = reg & PCCARD_DTYPE_MASK;
696 dspeed = reg & PCCARD_DSPEED_MASK;
698 DPRINTF(("CISTPL_DEVICE%s type=",
699 (tuple->code == CISTPL_DEVICE) ? "" : "_A"));
701 case PCCARD_DTYPE_NULL:
704 case PCCARD_DTYPE_ROM:
707 case PCCARD_DTYPE_OTPROM:
710 case PCCARD_DTYPE_EPROM:
713 case PCCARD_DTYPE_EEPROM:
716 case PCCARD_DTYPE_FLASH:
719 case PCCARD_DTYPE_SRAM:
722 case PCCARD_DTYPE_DRAM:
725 case PCCARD_DTYPE_FUNCSPEC:
726 DPRINTF(("funcspec"));
728 case PCCARD_DTYPE_EXTEND:
732 DPRINTF(("reserved"));
735 DPRINTF((" speed="));
737 case PCCARD_DSPEED_NULL:
740 case PCCARD_DSPEED_250NS:
743 case PCCARD_DSPEED_200NS:
746 case PCCARD_DSPEED_150NS:
749 case PCCARD_DSPEED_100NS:
752 case PCCARD_DSPEED_EXT:
756 DPRINTF(("reserved"));
764 if (tuple->length < 6) {
765 DPRINTF(("CISTPL_VERS_1 too short %d\n",
769 int start, i, ch, count;
771 state->card->cis1_major = pccard_tuple_read_1(tuple, 0);
772 state->card->cis1_minor = pccard_tuple_read_1(tuple, 1);
774 for (count = 0, start = 0, i = 0;
775 (count < 4) && ((i + 4) < 256); i++) {
776 ch = pccard_tuple_read_1(tuple, 2 + i);
779 state->card->cis1_info_buf[i] = ch;
781 state->card->cis1_info[count] =
782 state->card->cis1_info_buf + start;
787 DPRINTF(("CISTPL_VERS_1\n"));
791 if (tuple->length < 4) {
792 DPRINTF(("CISTPL_MANFID too short %d\n",
796 state->card->manufacturer = pccard_tuple_read_2(tuple, 0);
797 state->card->product = pccard_tuple_read_2(tuple, 2);
799 * This is for xe driver. But not limited to that driver.
800 * In PC Card Standard,
801 * Manufacturer ID: 2byte.
802 * Product ID: typically 2bytes, but there's no limit on its
803 * size. prodext is a two byte field, so maybe we should
804 * also handle the '6' case. So far no cards have surfaced
805 * with a length of '6'.
807 if (tuple->length == 5 )
808 state->card->prodext = pccard_tuple_read_1(tuple, 4);
809 DPRINTF(("CISTPL_MANFID\n"));
812 if (tuple->length < 1) {
813 DPRINTF(("CISTPL_FUNCID too short %d\n",
817 if ((state->pf == NULL) || (state->gotmfc == 2)) {
818 state->pf = malloc(sizeof(*state->pf), M_DEVBUF,
820 state->pf->number = state->count++;
821 state->pf->last_config_index = -1;
822 STAILQ_INIT(&state->pf->cfe_head);
824 STAILQ_INSERT_TAIL(&state->card->pf_head, state->pf,
827 state->pf->function = pccard_tuple_read_1(tuple, 0);
829 DPRINTF(("CISTPL_FUNCID\n"));
832 if (state->pf == NULL || state->pf->function <= 0) {
833 DPRINTF(("CISTPL_FUNCE is not followed by "
834 "valid CISTPL_FUNCID\n"));
837 if (tuple->length >= 2)
838 decode_funce(tuple, state->pf);
839 DPRINTF(("CISTPL_FUNCE\n"));
842 if (tuple->length < 3) {
843 DPRINTF(("CISTPL_CONFIG too short %d\n",
847 u_int reg, rasz, rmsz, rfsz;
850 reg = pccard_tuple_read_1(tuple, 0);
851 rasz = 1 + ((reg & PCCARD_TPCC_RASZ_MASK) >>
852 PCCARD_TPCC_RASZ_SHIFT);
853 rmsz = 1 + ((reg & PCCARD_TPCC_RMSZ_MASK) >>
854 PCCARD_TPCC_RMSZ_SHIFT);
855 rfsz = ((reg & PCCARD_TPCC_RFSZ_MASK) >>
856 PCCARD_TPCC_RFSZ_SHIFT);
858 if (tuple->length < (rasz + rmsz + rfsz)) {
859 DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
860 "short %d\n", rasz, rmsz, rfsz,
864 if (state->pf == NULL) {
865 state->pf = malloc(sizeof(*state->pf),
866 M_DEVBUF, M_NOWAIT | M_ZERO);
867 state->pf->number = state->count++;
868 state->pf->last_config_index = -1;
869 STAILQ_INIT(&state->pf->cfe_head);
871 STAILQ_INSERT_TAIL(&state->card->pf_head,
874 state->pf->function = PCCARD_FUNCTION_UNSPEC;
876 state->pf->last_config_index =
877 pccard_tuple_read_1(tuple, 1);
879 state->pf->ccr_base = 0;
880 for (i = 0; i < rasz; i++)
881 state->pf->ccr_base |=
882 ((pccard_tuple_read_1(tuple, 2 + i)) <<
885 state->pf->ccr_mask = 0;
886 for (i = 0; i < rmsz; i++)
887 state->pf->ccr_mask |=
888 ((pccard_tuple_read_1(tuple,
889 2 + rasz + i)) << (i * 8));
891 /* skip the reserved area and subtuples */
893 /* reset the default cfe for each cfe list */
894 state->temp_cfe = init_cfe;
895 state->default_cfe = &state->temp_cfe;
897 DPRINTF(("CISTPL_CONFIG\n"));
899 case CISTPL_CFTABLE_ENTRY:
903 u_int intface, def, num;
904 u_int power, timing, iospace, irq, memspace, misc;
905 struct pccard_config_entry *cfe;
909 reg = pccard_tuple_read_1(tuple, idx++);
910 intface = reg & PCCARD_TPCE_INDX_INTFACE;
911 def = reg & PCCARD_TPCE_INDX_DEFAULT;
912 num = reg & PCCARD_TPCE_INDX_NUM_MASK;
915 * this is a little messy. Some cards have only a
916 * cfentry with the default bit set. So, as we go
917 * through the list, we add new indexes to the queue,
918 * and keep a pointer to the last one with the
919 * default bit set. if we see a record with the same
920 * index, as the default, we stash the default and
921 * replace the queue entry. otherwise, we just add
922 * new entries to the queue, pointing the default ptr
923 * at them if the default bit is set. if we get to
924 * the end with the default pointer pointing at a
925 * record which hasn't had a matching index, that's
926 * ok; it just becomes a cfentry like any other.
930 * if the index in the cis differs from the default
931 * cis, create new entry in the queue and start it
932 * with the current default
934 if (num != state->default_cfe->number) {
935 cfe = (struct pccard_config_entry *)
936 malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
938 DPRINTF(("no memory for config entry\n"));
941 *cfe = *state->default_cfe;
943 STAILQ_INSERT_TAIL(&state->pf->cfe_head,
949 * if the default bit is set in the cis, then
950 * point the new default at whatever is being
954 state->default_cfe = cfe;
957 * the cis index matches the default index,
958 * fill in the default cfentry. It is
959 * assumed that the cfdefault index is in the
960 * queue. For it to be otherwise, the cis
961 * index would have to be -1 (initial
962 * condition) which is not possible, or there
963 * would have to be a preceding cis entry
964 * which had the same cis index and had the
965 * default bit unset. Neither condition
966 * should happen. If it does, this cfentry
967 * is lost (written into temp space), which
968 * is an acceptable failure mode.
971 cfe = state->default_cfe;
974 * if the cis entry does not have the default
975 * bit set, copy the default out of the way
979 state->temp_cfe = *state->default_cfe;
980 state->default_cfe = &state->temp_cfe;
985 reg = pccard_tuple_read_1(tuple, idx++);
986 cfe->flags &= ~(PCCARD_CFE_MWAIT_REQUIRED
987 | PCCARD_CFE_RDYBSY_ACTIVE
988 | PCCARD_CFE_WP_ACTIVE
989 | PCCARD_CFE_BVD_ACTIVE);
990 if (reg & PCCARD_TPCE_IF_MWAIT)
991 cfe->flags |= PCCARD_CFE_MWAIT_REQUIRED;
992 if (reg & PCCARD_TPCE_IF_RDYBSY)
993 cfe->flags |= PCCARD_CFE_RDYBSY_ACTIVE;
994 if (reg & PCCARD_TPCE_IF_WP)
995 cfe->flags |= PCCARD_CFE_WP_ACTIVE;
996 if (reg & PCCARD_TPCE_IF_BVD)
997 cfe->flags |= PCCARD_CFE_BVD_ACTIVE;
998 cfe->iftype = reg & PCCARD_TPCE_IF_IFTYPE;
1000 reg = pccard_tuple_read_1(tuple, idx++);
1002 power = reg & PCCARD_TPCE_FS_POWER_MASK;
1003 timing = reg & PCCARD_TPCE_FS_TIMING;
1004 iospace = reg & PCCARD_TPCE_FS_IOSPACE;
1005 irq = reg & PCCARD_TPCE_FS_IRQ;
1006 memspace = reg & PCCARD_TPCE_FS_MEMSPACE_MASK;
1007 misc = reg & PCCARD_TPCE_FS_MISC;
1010 /* skip over power, don't save */
1011 /* for each parameter selection byte */
1012 for (i = 0; i < power; i++) {
1013 reg = pccard_tuple_read_1(tuple, idx++);
1014 for (; reg; reg >>= 1)
1016 /* set bit -> read */
1019 /* skip over bytes */
1021 reg2 = pccard_tuple_read_1(tuple, idx++);
1023 * until non-extension
1026 } while (reg2 & 0x80);
1031 /* skip over timing, don't save */
1032 reg = pccard_tuple_read_1(tuple, idx++);
1034 if ((reg & PCCARD_TPCE_TD_RESERVED_MASK) !=
1035 PCCARD_TPCE_TD_RESERVED_MASK)
1037 if ((reg & PCCARD_TPCE_TD_RDYBSY_MASK) !=
1038 PCCARD_TPCE_TD_RDYBSY_MASK)
1040 if ((reg & PCCARD_TPCE_TD_WAIT_MASK) !=
1041 PCCARD_TPCE_TD_WAIT_MASK)
1045 if (tuple->length <= idx) {
1046 DPRINTF(("ran out of space before TCPE_IO\n"));
1050 reg = pccard_tuple_read_1(tuple, idx++);
1052 ~(PCCARD_CFE_IO8 | PCCARD_CFE_IO16);
1053 if (reg & PCCARD_TPCE_IO_BUSWIDTH_8BIT)
1054 cfe->flags |= PCCARD_CFE_IO8;
1055 if (reg & PCCARD_TPCE_IO_BUSWIDTH_16BIT)
1056 cfe->flags |= PCCARD_CFE_IO16;
1058 reg & PCCARD_TPCE_IO_IOADDRLINES_MASK;
1060 if (reg & PCCARD_TPCE_IO_HASRANGE) {
1061 reg = pccard_tuple_read_1(tuple, idx++);
1062 cfe->num_iospace = 1 + (reg &
1063 PCCARD_TPCE_IO_RANGE_COUNT);
1065 if (cfe->num_iospace >
1066 (sizeof(cfe->iospace) /
1067 sizeof(cfe->iospace[0]))) {
1068 DPRINTF(("too many io "
1071 state->card->error++;
1074 for (i = 0; i < cfe->num_iospace; i++) {
1075 switch (reg & PCCARD_TPCE_IO_RANGE_ADDRSIZE_MASK) {
1076 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_ONE:
1077 cfe->iospace[i].start =
1078 pccard_tuple_read_1(tuple, idx++);
1080 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_TWO:
1081 cfe->iospace[i].start =
1082 pccard_tuple_read_2(tuple, idx);
1085 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_FOUR:
1086 cfe->iospace[i].start =
1087 pccard_tuple_read_4(tuple, idx);
1092 PCCARD_TPCE_IO_RANGE_LENGTHSIZE_MASK) {
1093 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_ONE:
1094 cfe->iospace[i].length =
1095 pccard_tuple_read_1(tuple, idx++);
1097 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_TWO:
1098 cfe->iospace[i].length =
1099 pccard_tuple_read_2(tuple, idx);
1102 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_FOUR:
1103 cfe->iospace[i].length =
1104 pccard_tuple_read_4(tuple, idx);
1108 cfe->iospace[i].length++;
1111 cfe->num_iospace = 1;
1112 cfe->iospace[0].start = 0;
1113 cfe->iospace[0].length =
1118 if (tuple->length <= idx) {
1119 DPRINTF(("ran out of space before TCPE_IR\n"));
1123 reg = pccard_tuple_read_1(tuple, idx++);
1124 cfe->flags &= ~(PCCARD_CFE_IRQSHARE
1125 | PCCARD_CFE_IRQPULSE
1126 | PCCARD_CFE_IRQLEVEL);
1127 if (reg & PCCARD_TPCE_IR_SHARE)
1128 cfe->flags |= PCCARD_CFE_IRQSHARE;
1129 if (reg & PCCARD_TPCE_IR_PULSE)
1130 cfe->flags |= PCCARD_CFE_IRQPULSE;
1131 if (reg & PCCARD_TPCE_IR_LEVEL)
1132 cfe->flags |= PCCARD_CFE_IRQLEVEL;
1134 if (reg & PCCARD_TPCE_IR_HASMASK) {
1136 * it's legal to ignore the
1137 * special-interrupt bits, so I will
1141 pccard_tuple_read_2(tuple, idx);
1145 (1 << (reg & PCCARD_TPCE_IR_IRQ));
1148 cfe->irqmask = 0xffff;
1151 if (tuple->length <= idx) {
1152 DPRINTF(("ran out of space before TCPE_MS\n"));
1156 if (memspace == PCCARD_TPCE_FS_MEMSPACE_LENGTH) {
1157 cfe->num_memspace = 1;
1158 cfe->memspace[0].length = 256 *
1159 pccard_tuple_read_2(tuple, idx);
1161 cfe->memspace[0].cardaddr = 0;
1162 cfe->memspace[0].hostaddr = 0;
1163 } else if (memspace ==
1164 PCCARD_TPCE_FS_MEMSPACE_LENGTHADDR) {
1165 cfe->num_memspace = 1;
1166 cfe->memspace[0].length = 256 *
1167 pccard_tuple_read_2(tuple, idx);
1169 cfe->memspace[0].cardaddr = 256 *
1170 pccard_tuple_read_2(tuple, idx);
1172 cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr;
1178 reg = pccard_tuple_read_1(tuple, idx++);
1179 cfe->num_memspace = (reg &
1180 PCCARD_TPCE_MS_COUNT) + 1;
1181 if (cfe->num_memspace >
1182 (sizeof(cfe->memspace) /
1183 sizeof(cfe->memspace[0]))) {
1184 DPRINTF(("too many mem "
1186 cfe->num_memspace));
1187 state->card->error++;
1191 ((reg & PCCARD_TPCE_MS_LENGTH_SIZE_MASK) >>
1192 PCCARD_TPCE_MS_LENGTH_SIZE_SHIFT);
1194 ((reg & PCCARD_TPCE_MS_CARDADDR_SIZE_MASK) >>
1195 PCCARD_TPCE_MS_CARDADDR_SIZE_SHIFT);
1197 (reg & PCCARD_TPCE_MS_HOSTADDR) ? cardaddrsize : 0;
1199 if (lengthsize == 0) {
1200 DPRINTF(("cfe memspace "
1201 "lengthsize == 0"));
1202 state->card->error++;
1204 for (i = 0; i < cfe->num_memspace; i++) {
1206 cfe->memspace[i].length =
1207 256 * pccard_tuple_read_n(tuple, lengthsize,
1211 cfe->memspace[i].length = 0;
1213 if (cfe->memspace[i].length == 0) {
1214 DPRINTF(("cfe->memspace[%d].length == 0",
1216 state->card->error++;
1219 cfe->memspace[i].cardaddr =
1220 256 * pccard_tuple_read_n(tuple, cardaddrsize,
1222 idx += cardaddrsize;
1224 cfe->memspace[i].cardaddr = 0;
1227 cfe->memspace[i].hostaddr =
1228 256 * pccard_tuple_read_n(tuple, hostaddrsize,
1230 idx += hostaddrsize;
1232 cfe->memspace[i].hostaddr = 0;
1237 cfe->num_memspace = 0;
1239 if (tuple->length <= idx) {
1240 DPRINTF(("ran out of space before TCPE_MI\n"));
1244 reg = pccard_tuple_read_1(tuple, idx++);
1245 cfe->flags &= ~(PCCARD_CFE_POWERDOWN
1246 | PCCARD_CFE_READONLY
1247 | PCCARD_CFE_AUDIO);
1248 if (reg & PCCARD_TPCE_MI_PWRDOWN)
1249 cfe->flags |= PCCARD_CFE_POWERDOWN;
1250 if (reg & PCCARD_TPCE_MI_READONLY)
1251 cfe->flags |= PCCARD_CFE_READONLY;
1252 if (reg & PCCARD_TPCE_MI_AUDIO)
1253 cfe->flags |= PCCARD_CFE_AUDIO;
1254 cfe->maxtwins = reg & PCCARD_TPCE_MI_MAXTWINS;
1256 while (reg & PCCARD_TPCE_MI_EXT) {
1257 reg = pccard_tuple_read_1(tuple, idx++);
1260 /* skip all the subtuples */
1264 DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
1267 DPRINTF(("unhandled CISTPL %x\n", tuple->code));
1275 decode_funce(const struct pccard_tuple *tuple, struct pccard_function *pf)
1279 int type = pccard_tuple_read_1(tuple, 0);
1281 switch (pf->function) {
1282 case PCCARD_FUNCTION_DISK:
1283 if (type == PCCARD_TPLFE_TYPE_DISK_DEVICE_INTERFACE) {
1284 pf->pf_funce_disk_interface
1285 = pccard_tuple_read_1(tuple, 1);
1288 case PCCARD_FUNCTION_NETWORK:
1289 if (type == PCCARD_TPLFE_TYPE_LAN_NID) {
1290 len = pccard_tuple_read_1(tuple, 1);
1291 if (tuple->length < 2 + len || len > 8) {
1292 /* tuple length not enough or nid too long */
1295 for (i = 0; i < len; i++) {
1296 pf->pf_funce_lan_nid[i]
1297 = pccard_tuple_read_1(tuple, i + 2);
1299 pf->pf_funce_lan_nidlen = len;