1 /* $NetBSD: pcmcia_cis.c,v 1.17 2000/02/10 09:01:52 chopps Exp $ */
2 /* $FreeBSD: src/sys/dev/pccard/pccard_cis.c,v 1.36 2005/07/17 20:16:22 imp Exp $ */
3 /* $DragonFly: src/sys/bus/pccard/pccard_cis.c,v 1.8 2007/07/05 12:08:53 sephe Exp $ */
6 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Marc Horowitz.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/kernel.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
45 #include <bus/pccard/pccardreg.h>
46 #include <bus/pccard/pccardvar.h>
47 #include <bus/pccard/pccard_cis.h>
51 extern int pccard_cis_debug
;
53 #define PCCARDCISDEBUG
55 #define DPRINTF(arg) do { if (pccard_cis_debug) kprintf arg; } while (0)
56 #define DEVPRINTF(arg) do { if (pccard_cis_debug) device_printf arg; } while (0)
59 #define DEVPRINTF(arg)
62 #define PCCARD_CIS_SIZE 4096
67 struct pccard_config_entry temp_cfe
;
68 struct pccard_config_entry
*default_cfe
;
69 struct pccard_card
*card
;
70 struct pccard_function
*pf
;
73 static int pccard_parse_cis_tuple(const struct pccard_tuple
*, void *);
74 static int decode_funce(const struct pccard_tuple
*, struct pccard_function
*);
77 pccard_read_cis(struct pccard_softc
*sc
)
79 struct cis_state state
;
81 bzero(&state
, sizeof state
);
82 state
.card
= &sc
->card
;
83 state
.card
->error
= 0;
84 state
.card
->cis1_major
= -1;
85 state
.card
->cis1_minor
= -1;
86 state
.card
->cis1_info
[0] = NULL
;
87 state
.card
->cis1_info
[1] = NULL
;
88 state
.card
->cis1_info
[2] = NULL
;
89 state
.card
->cis1_info
[3] = NULL
;
90 state
.card
->manufacturer
= PCMCIA_VENDOR_INVALID
;
91 state
.card
->product
= PCMCIA_PRODUCT_INVALID
;
92 STAILQ_INIT(&state
.card
->pf_head
);
95 if (pccard_scan_cis(sc
->dev
, pccard_parse_cis_tuple
, &state
) == -1)
100 pccard_scan_cis(device_t dev
, pccard_scan_t fct
, void *arg
)
102 struct resource
*res
;
104 struct pccard_tuple tuple
;
105 int longlink_present
;
107 u_long longlink_addr
; /* Type suspect */
110 #ifdef PCCARDCISDEBUG
111 int cis_none_cnt
= 10; /* Only report 10 CIS_NONEs */
121 /* allocate some memory */
124 * Some reports from the field suggest that a 64k memory boundary
125 * helps card CIS being able to be read. Try it here and see what
126 * the results actually are. I'm not sure I understand why this
127 * would make cards work better, but it is easy enough to test.
130 res
= bus_alloc_resource(dev
, SYS_RES_MEMORY
, &rid
, 0, ~0,
131 PCCARD_CIS_SIZE
, RF_ACTIVE
| rman_make_alignment_flags(64*1024));
133 device_printf(dev
, "can't alloc memory to read attributes\n");
136 CARD_SET_RES_FLAGS(device_get_parent(dev
), dev
, SYS_RES_MEMORY
,
137 rid
, PCCARD_A_MEM_ATTR
);
138 tuple
.memt
= rman_get_bustag(res
);
139 tuple
.memh
= rman_get_bushandle(res
);
142 DPRINTF(("cis mem map 0x%x (resource: 0x%lx)\n",
143 (unsigned int) tuple
.memh
, rman_get_start(res
)));
147 longlink_present
= 1;
154 DEVPRINTF((dev
, "CIS tuple chain:\n"));
159 * Perform boundary check for insane cards.
160 * If CIS is too long, simulate CIS end.
161 * (This check may not be sufficient for
164 if (tuple
.mult
* tuple
.ptr
>= PCCARD_CIS_SIZE
- 1
165 - 32 /* ad hoc value */ ) {
166 kprintf("CIS is too long -- truncating\n");
167 tuple
.code
= CISTPL_END
;
169 /* get the tuple code */
170 tuple
.code
= pccard_cis_read_1(&tuple
, tuple
.ptr
);
173 /* two special-case tuples */
175 if (tuple
.code
== CISTPL_NULL
) {
176 #ifdef PCCARDCISDEBUG
177 if (cis_none_cnt
> 0)
178 DPRINTF(("CISTPL_NONE\n 00\n"));
179 else if (cis_none_cnt
== 0)
180 DPRINTF(("TOO MANY CIS_NONE\n"));
185 } else if (tuple
.code
== CISTPL_END
) {
186 DPRINTF(("CISTPL_END\n ff\n"));
187 /* Call the function for the END tuple, since
188 the CIS semantics depend on it */
189 if ((*fct
)(&tuple
, arg
)) {
196 /* now all the normal tuples */
198 tuple
.length
= pccard_cis_read_1(&tuple
, tuple
.ptr
+ 1);
199 switch (tuple
.code
) {
200 case CISTPL_LONGLINK_A
:
201 case CISTPL_LONGLINK_C
:
202 if (tuple
.length
< 4) {
203 DPRINTF(("CISTPL_LONGLINK_%s too "
205 longlink_common
? "C" : "A",
209 longlink_present
= 1;
210 longlink_common
= (tuple
.code
==
211 CISTPL_LONGLINK_C
) ? 1 : 0;
212 longlink_addr
= pccard_tuple_read_4(&tuple
, 0);
213 DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
214 longlink_common
? "C" : "A",
218 longlink_present
= 0;
219 DPRINTF(("CISTPL_NO_LINK\n"));
221 case CISTPL_CHECKSUM
:
222 if (tuple
.length
< 5) {
223 DPRINTF(("CISTPL_CHECKSUM too "
224 "short %d\n", tuple
.length
));
233 pccard_tuple_read_2(&tuple
, 0);
234 length
= pccard_tuple_read_2(&tuple
, 2);
235 cksum
= pccard_tuple_read_1(&tuple
, 4);
237 addr
= tuple
.ptr
+ offset
;
239 DPRINTF(("CISTPL_CHECKSUM addr=%lx "
241 addr
, length
, cksum
));
244 * XXX do more work to deal with
247 if ((addr
>= PCCARD_CIS_SIZE
) ||
250 DPRINTF((" skipped, "
255 for (i
= 0; i
< length
; i
++)
257 bus_space_read_1(tuple
.memt
,
259 addr
+ tuple
.mult
* i
);
260 if (cksum
!= (sum
& 0xff)) {
261 DPRINTF((" failed sum=%x\n",
264 "CIS checksum failed\n");
267 * XXX Some working cards have
268 * XXX bad checksums!!
277 case CISTPL_LONGLINK_MFC
:
278 if (tuple
.length
< 1) {
279 DPRINTF(("CISTPL_LONGLINK_MFC too "
280 "short %d\n", tuple
.length
));
283 if (((tuple
.length
- 1) % 5) != 0) {
284 DPRINTF(("CISTPL_LONGLINK_MFC bogus "
285 "length %d\n", tuple
.length
));
289 * this is kind of ad hoc, as I don't have
290 * any real documentation
296 * put count into tmp var so that
297 * if we have to bail (because it's
298 * a bogus count) it won't be
299 * remembered for later use.
302 pccard_tuple_read_1(&tuple
, 0);
304 DPRINTF(("CISTPL_LONGLINK_MFC %d",
308 * make _sure_ it's the right size;
309 * if too short, it may be a weird
310 * (unknown/undefined) format
312 if (tuple
.length
!= (tmp_count
*5 + 1)) {
313 DPRINTF((" bogus length %d\n",
318 * sanity check for a programming
319 * error which is difficult to find
323 howmany(sizeof mfc
, sizeof mfc
[0]))
324 panic("CISTPL_LONGLINK_MFC mfc "
325 "count would blow stack");
326 mfc_count
= tmp_count
;
327 for (i
= 0; i
< mfc_count
; i
++) {
329 (pccard_tuple_read_1(&tuple
,
331 PCCARD_MFC_MEM_COMMON
) ?
334 pccard_tuple_read_4(&tuple
,
337 mfc
[i
].common
? "common" :
338 "attr", mfc
[i
].addr
));
343 * for LONGLINK_MFC, fall through to the
344 * function. This tuple has structural and
349 if ((*fct
)(&tuple
, arg
)) {
356 #ifdef PCCARDCISDEBUG
357 /* print the tuple */
361 DPRINTF((" %02x %02x", tuple
.code
,
364 for (i
= 0; i
< tuple
.length
; i
++) {
366 pccard_tuple_read_1(&tuple
, i
)));
375 /* skip to the next tuple */
376 tuple
.ptr
+= 2 + tuple
.length
;
380 * the chain is done. Clean up and move onto the next one,
381 * if any. The loop is here in the case that there is an MFC
382 * card with no longlink (which defaults to existing, == 0).
383 * In general, this means that if one pointer fails, it will
384 * try the next one, instead of just bailing.
387 if (longlink_present
) {
388 CARD_SET_RES_FLAGS(device_get_parent(dev
), dev
,
389 SYS_RES_MEMORY
, rid
, longlink_common
?
390 PCCARD_A_MEM_COM
: PCCARD_A_MEM_ATTR
);
391 DPRINTF(("cis mem map %x\n",
392 (unsigned int) tuple
.memh
));
393 tuple
.mult
= longlink_common
? 1 : 2;
394 tuple
.ptr
= longlink_addr
;
395 longlink_present
= 0;
398 } else if (mfc_count
&& (mfc_index
< mfc_count
)) {
399 CARD_SET_RES_FLAGS(device_get_parent(dev
), dev
,
400 SYS_RES_MEMORY
, rid
, mfc
[mfc_index
].common
401 ? PCCARD_A_MEM_COM
: PCCARD_A_MEM_ATTR
);
402 DPRINTF(("cis mem map %x\n",
403 (unsigned int) tuple
.memh
));
404 /* set parse state, and point at the next one */
405 tuple
.mult
= mfc
[mfc_index
].common
? 1 : 2;
406 tuple
.ptr
= mfc
[mfc_index
].addr
;
412 /* make sure that the link is valid */
413 tuple
.code
= pccard_cis_read_1(&tuple
, tuple
.ptr
);
414 if (tuple
.code
!= CISTPL_LINKTARGET
) {
415 DPRINTF(("CISTPL_LINKTARGET expected, "
416 "code %02x observed\n", tuple
.code
));
419 tuple
.length
= pccard_cis_read_1(&tuple
, tuple
.ptr
+ 1);
420 if (tuple
.length
< 3) {
421 DPRINTF(("CISTPL_LINKTARGET too short %d\n",
425 if ((pccard_tuple_read_1(&tuple
, 0) != 'C') ||
426 (pccard_tuple_read_1(&tuple
, 1) != 'I') ||
427 (pccard_tuple_read_1(&tuple
, 2) != 'S')) {
428 DPRINTF(("CISTPL_LINKTARGET magic "
429 "%02x%02x%02x incorrect\n",
430 pccard_tuple_read_1(&tuple
, 0),
431 pccard_tuple_read_1(&tuple
, 1),
432 pccard_tuple_read_1(&tuple
, 2)));
435 tuple
.ptr
+= 2 + tuple
.length
;
441 bus_release_resource(dev
, SYS_RES_MEMORY
, rid
, res
);
446 /* XXX this is incredibly verbose. Not sure what trt is */
449 pccard_print_cis(device_t dev
)
451 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
452 struct pccard_card
*card
= &sc
->card
;
453 struct pccard_function
*pf
;
454 struct pccard_config_entry
*cfe
;
457 device_printf(dev
, "CIS version ");
458 if (card
->cis1_major
== 4) {
459 if (card
->cis1_minor
== 0)
460 kprintf("PCCARD 1.0\n");
461 else if (card
->cis1_minor
== 1)
462 kprintf("PCCARD 2.0 or 2.1\n");
463 } else if (card
->cis1_major
>= 5)
464 kprintf("PC Card Standard %d.%d\n", card
->cis1_major
, card
->cis1_minor
);
466 kprintf("unknown (major=%d, minor=%d)\n",
467 card
->cis1_major
, card
->cis1_minor
);
469 device_printf(dev
, "CIS info: ");
470 for (i
= 0; i
< 4; i
++) {
471 if (card
->cis1_info
[i
] == NULL
)
475 kprintf("%s", card
->cis1_info
[i
]);
479 device_printf(dev
, "Manufacturer code 0x%x, product 0x%x\n",
480 card
->manufacturer
, card
->product
);
482 STAILQ_FOREACH(pf
, &card
->pf_head
, pf_list
) {
483 device_printf(dev
, "function %d: ", pf
->number
);
485 switch (pf
->function
) {
486 case PCCARD_FUNCTION_UNSPEC
:
487 kprintf("unspecified");
489 case PCCARD_FUNCTION_MULTIFUNCTION
:
490 kprintf("multi-function");
492 case PCCARD_FUNCTION_MEMORY
:
495 case PCCARD_FUNCTION_SERIAL
:
496 kprintf("serial port");
498 case PCCARD_FUNCTION_PARALLEL
:
499 kprintf("parallel port");
501 case PCCARD_FUNCTION_DISK
:
502 kprintf("fixed disk");
504 case PCCARD_FUNCTION_VIDEO
:
505 kprintf("video adapter");
507 case PCCARD_FUNCTION_NETWORK
:
508 kprintf("network adapter");
510 case PCCARD_FUNCTION_AIMS
:
511 kprintf("auto incrementing mass storage");
513 case PCCARD_FUNCTION_SCSI
:
514 kprintf("SCSI bridge");
516 case PCCARD_FUNCTION_SECURITY
:
517 kprintf("Security services");
519 case PCCARD_FUNCTION_INSTRUMENT
:
520 kprintf("Instrument");
523 kprintf("unknown (%d)", pf
->function
);
527 kprintf(", ccr addr %x mask %x\n", pf
->ccr_base
, pf
->ccr_mask
);
529 STAILQ_FOREACH(cfe
, &pf
->cfe_head
, cfe_list
) {
530 device_printf(dev
, "function %d, config table entry "
531 "%d: ", pf
->number
, cfe
->number
);
533 switch (cfe
->iftype
) {
534 case PCCARD_IFTYPE_MEMORY
:
535 kprintf("memory card");
537 case PCCARD_IFTYPE_IO
:
541 kprintf("card type unknown");
545 kprintf("; irq mask %x", cfe
->irqmask
);
547 if (cfe
->num_iospace
) {
548 kprintf("; iomask %lx, iospace", cfe
->iomask
);
550 for (i
= 0; i
< cfe
->num_iospace
; i
++) {
551 kprintf(" %lx", cfe
->iospace
[i
].start
);
552 if (cfe
->iospace
[i
].length
)
554 cfe
->iospace
[i
].start
+
555 cfe
->iospace
[i
].length
- 1);
558 if (cfe
->num_memspace
) {
559 kprintf("; memspace");
561 for (i
= 0; i
< cfe
->num_memspace
; i
++) {
563 cfe
->memspace
[i
].cardaddr
);
564 if (cfe
->memspace
[i
].length
)
566 cfe
->memspace
[i
].cardaddr
+
567 cfe
->memspace
[i
].length
- 1);
568 if (cfe
->memspace
[i
].hostaddr
)
570 cfe
->memspace
[i
].hostaddr
);
574 kprintf("; maxtwins %d", cfe
->maxtwins
);
578 if (cfe
->flags
& PCCARD_CFE_MWAIT_REQUIRED
)
579 kprintf(" mwait_required");
580 if (cfe
->flags
& PCCARD_CFE_RDYBSY_ACTIVE
)
581 kprintf(" rdybsy_active");
582 if (cfe
->flags
& PCCARD_CFE_WP_ACTIVE
)
583 kprintf(" wp_active");
584 if (cfe
->flags
& PCCARD_CFE_BVD_ACTIVE
)
585 kprintf(" bvd_active");
586 if (cfe
->flags
& PCCARD_CFE_IO8
)
588 if (cfe
->flags
& PCCARD_CFE_IO16
)
590 if (cfe
->flags
& PCCARD_CFE_IRQSHARE
)
591 kprintf(" irqshare");
592 if (cfe
->flags
& PCCARD_CFE_IRQPULSE
)
593 kprintf(" irqpulse");
594 if (cfe
->flags
& PCCARD_CFE_IRQLEVEL
)
595 kprintf(" irqlevel");
596 if (cfe
->flags
& PCCARD_CFE_POWERDOWN
)
597 kprintf(" powerdown");
598 if (cfe
->flags
& PCCARD_CFE_READONLY
)
599 kprintf(" readonly");
600 if (cfe
->flags
& PCCARD_CFE_AUDIO
)
608 device_printf(dev
, "%d errors found while parsing CIS\n",
613 pccard_parse_cis_tuple(const struct pccard_tuple
*tuple
, void *arg
)
615 /* most of these are educated guesses */
616 static struct pccard_config_entry init_cfe
= {
617 -1, PCCARD_CFE_RDYBSY_ACTIVE
| PCCARD_CFE_WP_ACTIVE
|
618 PCCARD_CFE_BVD_ACTIVE
, PCCARD_IFTYPE_MEMORY
,
621 struct cis_state
*state
= arg
;
623 switch (tuple
->code
) {
625 /* if we've seen a LONGLINK_MFC, and this is the first
626 * END after it, reset the function list.
628 * XXX This might also be the right place to start a
629 * new function, but that assumes that a function
630 * definition never crosses any longlink, and I'm not
631 * sure about that. This is probably safe for MFC
632 * cards, but what we have now isn't broken, so I'd
633 * rather not change it.
635 if (state
->gotmfc
== 1) {
636 struct pccard_function
*pf
, *pfnext
;
638 for (pf
= STAILQ_FIRST(&state
->card
->pf_head
);
639 pf
!= NULL
; pf
= pfnext
) {
640 pfnext
= STAILQ_NEXT(pf
, pf_list
);
644 STAILQ_INIT(&state
->card
->pf_head
);
651 case CISTPL_LONGLINK_MFC
:
653 * this tuple's structure was dealt with in scan_cis. here,
654 * record the fact that the MFC tuple was seen, so that
655 * functions declared before the MFC link can be cleaned
660 #ifdef PCCARDCISDEBUG
662 case CISTPL_DEVICE_A
:
664 u_int reg
, dtype
, dspeed
;
666 reg
= pccard_tuple_read_1(tuple
, 0);
667 dtype
= reg
& PCCARD_DTYPE_MASK
;
668 dspeed
= reg
& PCCARD_DSPEED_MASK
;
670 DPRINTF(("CISTPL_DEVICE%s type=",
671 (tuple
->code
== CISTPL_DEVICE
) ? "" : "_A"));
673 case PCCARD_DTYPE_NULL
:
676 case PCCARD_DTYPE_ROM
:
679 case PCCARD_DTYPE_OTPROM
:
682 case PCCARD_DTYPE_EPROM
:
685 case PCCARD_DTYPE_EEPROM
:
688 case PCCARD_DTYPE_FLASH
:
691 case PCCARD_DTYPE_SRAM
:
694 case PCCARD_DTYPE_DRAM
:
697 case PCCARD_DTYPE_FUNCSPEC
:
698 DPRINTF(("funcspec"));
700 case PCCARD_DTYPE_EXTEND
:
704 DPRINTF(("reserved"));
707 DPRINTF((" speed="));
709 case PCCARD_DSPEED_NULL
:
712 case PCCARD_DSPEED_250NS
:
715 case PCCARD_DSPEED_200NS
:
718 case PCCARD_DSPEED_150NS
:
721 case PCCARD_DSPEED_100NS
:
724 case PCCARD_DSPEED_EXT
:
728 DPRINTF(("reserved"));
736 if (tuple
->length
< 6) {
737 DPRINTF(("CISTPL_VERS_1 too short %d\n",
741 int start
, i
, ch
, count
;
743 state
->card
->cis1_major
= pccard_tuple_read_1(tuple
, 0);
744 state
->card
->cis1_minor
= pccard_tuple_read_1(tuple
, 1);
746 for (count
= 0, start
= 0, i
= 0;
747 (count
< 4) && ((i
+ 4) < 256); i
++) {
748 ch
= pccard_tuple_read_1(tuple
, 2 + i
);
751 state
->card
->cis1_info_buf
[i
] = ch
;
753 state
->card
->cis1_info
[count
] =
754 state
->card
->cis1_info_buf
+ start
;
759 DPRINTF(("CISTPL_VERS_1\n"));
763 if (tuple
->length
< 4) {
764 DPRINTF(("CISTPL_MANFID too short %d\n",
768 state
->card
->manufacturer
= pccard_tuple_read_2(tuple
, 0);
769 state
->card
->product
= pccard_tuple_read_2(tuple
, 2);
771 * This is for xe driver. But not limited to that driver.
772 * In PC Card Standard,
773 * Manufacturer ID: 2byte.
774 * Product ID: typically 2bytes, but there's no limit on its
775 * size. prodext is a two byte field, so maybe we should
776 * also handle the '6' case. So far no cards have surfaced
777 * with a length of '6'.
779 if (tuple
->length
== 5 )
780 state
->card
->prodext
= pccard_tuple_read_1(tuple
, 4);
781 DPRINTF(("CISTPL_MANFID\n"));
784 if (tuple
->length
< 1) {
785 DPRINTF(("CISTPL_FUNCID too short %d\n",
789 if ((state
->pf
== NULL
) || (state
->gotmfc
== 2)) {
790 state
->pf
= kmalloc(sizeof(*state
->pf
), M_DEVBUF
,
792 state
->pf
->number
= state
->count
++;
793 state
->pf
->last_config_index
= -1;
794 STAILQ_INIT(&state
->pf
->cfe_head
);
796 STAILQ_INSERT_TAIL(&state
->card
->pf_head
, state
->pf
,
799 state
->pf
->function
= pccard_tuple_read_1(tuple
, 0);
801 DPRINTF(("CISTPL_FUNCID\n"));
804 if (state
->pf
== NULL
|| state
->pf
->function
<= 0) {
805 DPRINTF(("CISTPL_FUNCE is not followed by "
806 "valid CISTPL_FUNCID\n"));
809 if (tuple
->length
>= 2)
810 decode_funce(tuple
, state
->pf
);
811 DPRINTF(("CISTPL_FUNCE\n"));
814 if (tuple
->length
< 3) {
815 DPRINTF(("CISTPL_CONFIG too short %d\n",
819 u_int reg
, rasz
, rmsz
, rfsz
;
822 reg
= pccard_tuple_read_1(tuple
, 0);
823 rasz
= 1 + ((reg
& PCCARD_TPCC_RASZ_MASK
) >>
824 PCCARD_TPCC_RASZ_SHIFT
);
825 rmsz
= 1 + ((reg
& PCCARD_TPCC_RMSZ_MASK
) >>
826 PCCARD_TPCC_RMSZ_SHIFT
);
827 rfsz
= ((reg
& PCCARD_TPCC_RFSZ_MASK
) >>
828 PCCARD_TPCC_RFSZ_SHIFT
);
830 if (tuple
->length
< (rasz
+ rmsz
+ rfsz
)) {
831 DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
832 "short %d\n", rasz
, rmsz
, rfsz
,
836 if (state
->pf
== NULL
) {
837 state
->pf
= kmalloc(sizeof(*state
->pf
),
838 M_DEVBUF
, M_NOWAIT
| M_ZERO
);
839 state
->pf
->number
= state
->count
++;
840 state
->pf
->last_config_index
= -1;
841 STAILQ_INIT(&state
->pf
->cfe_head
);
843 STAILQ_INSERT_TAIL(&state
->card
->pf_head
,
846 state
->pf
->function
= PCCARD_FUNCTION_UNSPEC
;
848 state
->pf
->last_config_index
=
849 pccard_tuple_read_1(tuple
, 1);
851 state
->pf
->ccr_base
= 0;
852 for (i
= 0; i
< rasz
; i
++)
853 state
->pf
->ccr_base
|=
854 ((pccard_tuple_read_1(tuple
, 2 + i
)) <<
857 state
->pf
->ccr_mask
= 0;
858 for (i
= 0; i
< rmsz
; i
++)
859 state
->pf
->ccr_mask
|=
860 ((pccard_tuple_read_1(tuple
,
861 2 + rasz
+ i
)) << (i
* 8));
863 /* skip the reserved area and subtuples */
865 /* reset the default cfe for each cfe list */
866 state
->temp_cfe
= init_cfe
;
867 state
->default_cfe
= &state
->temp_cfe
;
869 DPRINTF(("CISTPL_CONFIG\n"));
871 case CISTPL_CFTABLE_ENTRY
:
875 u_int intface
, def
, num
;
876 u_int power
, timing
, iospace
, irq
, memspace
, misc
;
877 struct pccard_config_entry
*cfe
;
881 reg
= pccard_tuple_read_1(tuple
, idx
);
883 intface
= reg
& PCCARD_TPCE_INDX_INTFACE
;
884 def
= reg
& PCCARD_TPCE_INDX_DEFAULT
;
885 num
= reg
& PCCARD_TPCE_INDX_NUM_MASK
;
888 * this is a little messy. Some cards have only a
889 * cfentry with the default bit set. So, as we go
890 * through the list, we add new indexes to the queue,
891 * and keep a pointer to the last one with the
892 * default bit set. if we see a record with the same
893 * index, as the default, we stash the default and
894 * replace the queue entry. otherwise, we just add
895 * new entries to the queue, pointing the default ptr
896 * at them if the default bit is set. if we get to
897 * the end with the default pointer pointing at a
898 * record which hasn't had a matching index, that's
899 * ok; it just becomes a cfentry like any other.
903 * if the index in the cis differs from the default
904 * cis, create new entry in the queue and start it
905 * with the current default
907 if (num
!= state
->default_cfe
->number
) {
908 cfe
= (struct pccard_config_entry
*)
909 kmalloc(sizeof(*cfe
), M_DEVBUF
, M_NOWAIT
);
911 DPRINTF(("no memory for config entry\n"));
914 *cfe
= *state
->default_cfe
;
916 STAILQ_INSERT_TAIL(&state
->pf
->cfe_head
,
922 * if the default bit is set in the cis, then
923 * point the new default at whatever is being
927 state
->default_cfe
= cfe
;
930 * the cis index matches the default index,
931 * fill in the default cfentry. It is
932 * assumed that the cfdefault index is in the
933 * queue. For it to be otherwise, the cis
934 * index would have to be -1 (initial
935 * condition) which is not possible, or there
936 * would have to be a preceding cis entry
937 * which had the same cis index and had the
938 * default bit unset. Neither condition
939 * should happen. If it does, this cfentry
940 * is lost (written into temp space), which
941 * is an acceptable failure mode.
944 cfe
= state
->default_cfe
;
947 * if the cis entry does not have the default
948 * bit set, copy the default out of the way
952 state
->temp_cfe
= *state
->default_cfe
;
953 state
->default_cfe
= &state
->temp_cfe
;
958 reg
= pccard_tuple_read_1(tuple
, idx
);
960 cfe
->flags
&= ~(PCCARD_CFE_MWAIT_REQUIRED
961 | PCCARD_CFE_RDYBSY_ACTIVE
962 | PCCARD_CFE_WP_ACTIVE
963 | PCCARD_CFE_BVD_ACTIVE
);
964 if (reg
& PCCARD_TPCE_IF_MWAIT
)
965 cfe
->flags
|= PCCARD_CFE_MWAIT_REQUIRED
;
966 if (reg
& PCCARD_TPCE_IF_RDYBSY
)
967 cfe
->flags
|= PCCARD_CFE_RDYBSY_ACTIVE
;
968 if (reg
& PCCARD_TPCE_IF_WP
)
969 cfe
->flags
|= PCCARD_CFE_WP_ACTIVE
;
970 if (reg
& PCCARD_TPCE_IF_BVD
)
971 cfe
->flags
|= PCCARD_CFE_BVD_ACTIVE
;
972 cfe
->iftype
= reg
& PCCARD_TPCE_IF_IFTYPE
;
974 reg
= pccard_tuple_read_1(tuple
, idx
);
977 power
= reg
& PCCARD_TPCE_FS_POWER_MASK
;
978 timing
= reg
& PCCARD_TPCE_FS_TIMING
;
979 iospace
= reg
& PCCARD_TPCE_FS_IOSPACE
;
980 irq
= reg
& PCCARD_TPCE_FS_IRQ
;
981 memspace
= reg
& PCCARD_TPCE_FS_MEMSPACE_MASK
;
982 misc
= reg
& PCCARD_TPCE_FS_MISC
;
985 /* skip over power, don't save */
986 /* for each parameter selection byte */
987 for (i
= 0; i
< power
; i
++) {
988 reg
= pccard_tuple_read_1(tuple
, idx
);
991 for (j
= 0; j
< 7; j
++) {
992 /* if the bit is set */
993 if ((reg
>> j
) & 0x01) {
994 /* skip over bytes */
996 reg2
= pccard_tuple_read_1(tuple
, idx
);
1003 } while (reg2
& 0x80);
1009 /* skip over timing, don't save */
1010 reg
= pccard_tuple_read_1(tuple
, idx
);
1013 if ((reg
& PCCARD_TPCE_TD_RESERVED_MASK
) !=
1014 PCCARD_TPCE_TD_RESERVED_MASK
)
1016 if ((reg
& PCCARD_TPCE_TD_RDYBSY_MASK
) !=
1017 PCCARD_TPCE_TD_RDYBSY_MASK
)
1019 if ((reg
& PCCARD_TPCE_TD_WAIT_MASK
) !=
1020 PCCARD_TPCE_TD_WAIT_MASK
)
1024 if (tuple
->length
<= idx
) {
1025 DPRINTF(("ran out of space before TCPE_IO\n"));
1029 reg
= pccard_tuple_read_1(tuple
, idx
);
1032 ~(PCCARD_CFE_IO8
| PCCARD_CFE_IO16
);
1033 if (reg
& PCCARD_TPCE_IO_BUSWIDTH_8BIT
)
1034 cfe
->flags
|= PCCARD_CFE_IO8
;
1035 if (reg
& PCCARD_TPCE_IO_BUSWIDTH_16BIT
)
1036 cfe
->flags
|= PCCARD_CFE_IO16
;
1038 reg
& PCCARD_TPCE_IO_IOADDRLINES_MASK
;
1040 if (reg
& PCCARD_TPCE_IO_HASRANGE
) {
1041 reg
= pccard_tuple_read_1(tuple
, idx
);
1044 cfe
->num_iospace
= 1 + (reg
&
1045 PCCARD_TPCE_IO_RANGE_COUNT
);
1047 if (cfe
->num_iospace
>
1048 (sizeof(cfe
->iospace
) /
1049 sizeof(cfe
->iospace
[0]))) {
1050 DPRINTF(("too many io "
1053 state
->card
->error
++;
1056 for (i
= 0; i
< cfe
->num_iospace
; i
++) {
1057 switch (reg
& PCCARD_TPCE_IO_RANGE_ADDRSIZE_MASK
) {
1058 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_ONE
:
1059 cfe
->iospace
[i
].start
=
1060 pccard_tuple_read_1(tuple
, idx
);
1063 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_TWO
:
1064 cfe
->iospace
[i
].start
=
1065 pccard_tuple_read_2(tuple
, idx
);
1068 case PCCARD_TPCE_IO_RANGE_ADDRSIZE_FOUR
:
1069 cfe
->iospace
[i
].start
=
1070 pccard_tuple_read_4(tuple
, idx
);
1075 PCCARD_TPCE_IO_RANGE_LENGTHSIZE_MASK
) {
1076 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_ONE
:
1077 cfe
->iospace
[i
].length
=
1078 pccard_tuple_read_1(tuple
, idx
);
1081 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_TWO
:
1082 cfe
->iospace
[i
].length
=
1083 pccard_tuple_read_2(tuple
, idx
);
1086 case PCCARD_TPCE_IO_RANGE_LENGTHSIZE_FOUR
:
1087 cfe
->iospace
[i
].length
=
1088 pccard_tuple_read_4(tuple
, idx
);
1092 cfe
->iospace
[i
].length
++;
1095 cfe
->num_iospace
= 1;
1096 cfe
->iospace
[0].start
= 0;
1097 cfe
->iospace
[0].length
=
1102 if (tuple
->length
<= idx
) {
1103 DPRINTF(("ran out of space before TCPE_IR\n"));
1107 reg
= pccard_tuple_read_1(tuple
, idx
);
1109 cfe
->flags
&= ~(PCCARD_CFE_IRQSHARE
1110 | PCCARD_CFE_IRQPULSE
1111 | PCCARD_CFE_IRQLEVEL
);
1112 if (reg
& PCCARD_TPCE_IR_SHARE
)
1113 cfe
->flags
|= PCCARD_CFE_IRQSHARE
;
1114 if (reg
& PCCARD_TPCE_IR_PULSE
)
1115 cfe
->flags
|= PCCARD_CFE_IRQPULSE
;
1116 if (reg
& PCCARD_TPCE_IR_LEVEL
)
1117 cfe
->flags
|= PCCARD_CFE_IRQLEVEL
;
1119 if (reg
& PCCARD_TPCE_IR_HASMASK
) {
1121 * it's legal to ignore the
1122 * special-interrupt bits, so I will
1126 pccard_tuple_read_2(tuple
, idx
);
1130 (1 << (reg
& PCCARD_TPCE_IR_IRQ
));
1134 if (tuple
->length
<= idx
) {
1135 DPRINTF(("ran out of space before TCPE_MS\n"));
1139 if (memspace
== PCCARD_TPCE_FS_MEMSPACE_LENGTH
) {
1140 cfe
->num_memspace
= 1;
1141 cfe
->memspace
[0].length
= 256 *
1142 pccard_tuple_read_2(tuple
, idx
);
1144 cfe
->memspace
[0].cardaddr
= 0;
1145 cfe
->memspace
[0].hostaddr
= 0;
1146 } else if (memspace
==
1147 PCCARD_TPCE_FS_MEMSPACE_LENGTHADDR
) {
1148 cfe
->num_memspace
= 1;
1149 cfe
->memspace
[0].length
= 256 *
1150 pccard_tuple_read_2(tuple
, idx
);
1152 cfe
->memspace
[0].cardaddr
= 256 *
1153 pccard_tuple_read_2(tuple
, idx
);
1155 cfe
->memspace
[0].hostaddr
= cfe
->memspace
[0].cardaddr
;
1161 reg
= pccard_tuple_read_1(tuple
, idx
);
1164 cfe
->num_memspace
= (reg
&
1165 PCCARD_TPCE_MS_COUNT
) + 1;
1167 if (cfe
->num_memspace
>
1168 (sizeof(cfe
->memspace
) /
1169 sizeof(cfe
->memspace
[0]))) {
1170 DPRINTF(("too many mem "
1172 cfe
->num_memspace
));
1173 state
->card
->error
++;
1177 ((reg
& PCCARD_TPCE_MS_LENGTH_SIZE_MASK
) >>
1178 PCCARD_TPCE_MS_LENGTH_SIZE_SHIFT
);
1180 ((reg
& PCCARD_TPCE_MS_CARDADDR_SIZE_MASK
) >>
1181 PCCARD_TPCE_MS_CARDADDR_SIZE_SHIFT
);
1183 (reg
& PCCARD_TPCE_MS_HOSTADDR
) ? cardaddrsize
: 0;
1185 if (lengthsize
== 0) {
1186 DPRINTF(("cfe memspace "
1187 "lengthsize == 0"));
1188 state
->card
->error
++;
1190 for (i
= 0; i
< cfe
->num_memspace
; i
++) {
1192 cfe
->memspace
[i
].length
=
1193 256 * pccard_tuple_read_n(tuple
, lengthsize
,
1197 cfe
->memspace
[i
].length
= 0;
1199 if (cfe
->memspace
[i
].length
== 0) {
1200 DPRINTF(("cfe->memspace[%d].length == 0",
1202 state
->card
->error
++;
1205 cfe
->memspace
[i
].cardaddr
=
1206 256 * pccard_tuple_read_n(tuple
, cardaddrsize
,
1208 idx
+= cardaddrsize
;
1210 cfe
->memspace
[i
].cardaddr
= 0;
1213 cfe
->memspace
[i
].hostaddr
=
1214 256 * pccard_tuple_read_n(tuple
, hostaddrsize
,
1216 idx
+= hostaddrsize
;
1218 cfe
->memspace
[i
].hostaddr
= 0;
1223 cfe
->num_memspace
= 0;
1225 if (tuple
->length
<= idx
) {
1226 DPRINTF(("ran out of space before TCPE_MI\n"));
1230 reg
= pccard_tuple_read_1(tuple
, idx
);
1232 cfe
->flags
&= ~(PCCARD_CFE_POWERDOWN
1233 | PCCARD_CFE_READONLY
1234 | PCCARD_CFE_AUDIO
);
1235 if (reg
& PCCARD_TPCE_MI_PWRDOWN
)
1236 cfe
->flags
|= PCCARD_CFE_POWERDOWN
;
1237 if (reg
& PCCARD_TPCE_MI_READONLY
)
1238 cfe
->flags
|= PCCARD_CFE_READONLY
;
1239 if (reg
& PCCARD_TPCE_MI_AUDIO
)
1240 cfe
->flags
|= PCCARD_CFE_AUDIO
;
1241 cfe
->maxtwins
= reg
& PCCARD_TPCE_MI_MAXTWINS
;
1243 while (reg
& PCCARD_TPCE_MI_EXT
) {
1244 reg
= pccard_tuple_read_1(tuple
, idx
);
1248 /* skip all the subtuples */
1252 DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
1255 DPRINTF(("unhandled CISTPL %x\n", tuple
->code
));
1263 decode_funce(const struct pccard_tuple
*tuple
, struct pccard_function
*pf
)
1267 int type
= pccard_tuple_read_1(tuple
, 0);
1269 switch (pf
->function
) {
1270 case PCCARD_FUNCTION_DISK
:
1271 if (type
== PCCARD_TPLFE_TYPE_DISK_DEVICE_INTERFACE
) {
1272 pf
->pf_funce_disk_interface
1273 = pccard_tuple_read_1(tuple
, 1);
1276 case PCCARD_FUNCTION_NETWORK
:
1277 if (type
== PCCARD_TPLFE_TYPE_LAN_NID
) {
1278 len
= pccard_tuple_read_1(tuple
, 1);
1279 if (tuple
->length
< 2 + len
|| len
> 8) {
1280 /* tuple length not enough or nid too long */
1283 for (i
= 0; i
< len
; i
++) {
1284 pf
->pf_funce_lan_nid
[i
]
1285 = pccard_tuple_read_1(tuple
, i
+ 2);
1287 pf
->pf_funce_lan_nidlen
= len
;