xref: /openbsd/sys/dev/cardbus/cardbus.c (revision 73471bf0)
1 /*	$OpenBSD: cardbus.c,v 1.52 2019/11/30 18:19:51 cheloha Exp $	*/
2 /*	$NetBSD: cardbus.c,v 1.24 2000/04/02 19:11:37 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1997, 1998, 1999 and 2000
6  *     HAYAKAWA Koichi.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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  *
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 
37 #include <machine/bus.h>
38 
39 #include <dev/cardbus/cardbusvar.h>
40 #include <dev/pci/pcidevs.h>
41 
42 #include <dev/cardbus/cardbus_exrom.h>
43 
44 #include <dev/pci/pcivar.h>	/* XXX */
45 #include <dev/pci/pcireg.h>	/* XXX */
46 
47 #include <dev/pcmcia/pcmciareg.h>
48 
49 #ifdef CARDBUS_DEBUG
50 #define STATIC
51 #define DPRINTF(a) printf a
52 #else
53 #ifdef DDB
54 #define STATIC
55 #else
56 #define STATIC static
57 #endif
58 #define DPRINTF(a)
59 #endif
60 
61 STATIC void cardbusattach(struct device *, struct device *, void *);
62 /* STATIC int cardbusprint(void *, const char *); */
63 
64 STATIC int cardbusmatch(struct device *, void *, void *);
65 STATIC int cardbussubmatch(struct device *, void *, void *);
66 STATIC int cardbusprint(void *, const char *);
67 
68 typedef void (*tuple_decode_func)(u_int8_t *, int, void *);
69 
70 STATIC int decode_tuples(u_int8_t *, int, tuple_decode_func, void *);
71 STATIC void parse_tuple(u_int8_t *, int, void *);
72 #ifdef CARDBUS_DEBUG
73 static void print_tuple(u_int8_t *, int, void *);
74 #endif
75 
76 STATIC int cardbus_read_tuples(struct cardbus_attach_args *,
77     pcireg_t, u_int8_t *, size_t);
78 
79 STATIC void enable_function(struct cardbus_softc *, int, int);
80 STATIC void disable_function(struct cardbus_softc *, int);
81 
82 
83 struct cfattach cardbus_ca = {
84 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
85 };
86 
87 struct cfdriver cardbus_cd = {
88 	NULL, "cardbus", DV_DULL
89 };
90 
91 STATIC int
92 cardbusmatch(struct device *parent, void *match, void *aux)
93 {
94 	struct cfdata *cf = match;
95 	struct cbslot_attach_args *cba = aux;
96 
97 	if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
98 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
99 		    cba->cba_busname, cf->cf_driver->cd_name));
100 		return (0);
101 	}
102 
103 	return (1);
104 }
105 
106 STATIC void
107 cardbusattach(struct device *parent, struct device *self, void *aux)
108 {
109 	struct cardbus_softc *sc = (void *)self;
110 	struct cbslot_attach_args *cba = aux;
111 	int cdstatus;
112 
113 	sc->sc_bus = cba->cba_bus;
114 	sc->sc_device = 0;
115 	sc->sc_intrline = cba->cba_intrline;
116 	sc->sc_cacheline = cba->cba_cacheline;
117 	sc->sc_lattimer = cba->cba_lattimer;
118 
119 	printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
120 	printf(" cacheline 0x%x, lattimer 0x%x\n",
121 	    sc->sc_cacheline,sc->sc_lattimer);
122 
123 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
124 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
125 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
126 	sc->sc_cc = cba->cba_cc;
127 	sc->sc_pc = cba->cba_pc;
128 	sc->sc_cf = cba->cba_cf;
129 	sc->sc_rbus_iot = cba->cba_rbus_iot;
130 	sc->sc_rbus_memt = cba->cba_rbus_memt;
131 
132 	cdstatus = 0;
133 }
134 
135 STATIC int
136 cardbus_read_tuples(struct cardbus_attach_args *ca, pcireg_t cis_ptr,
137     u_int8_t *tuples, size_t len)
138 {
139 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
140 	pci_chipset_tag_t pc = ca->ca_pc;
141 	pcitag_t tag = ca->ca_tag;
142 	pcireg_t command;
143 	int found = 0;
144 
145 	int i, j;
146 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
147 	bus_space_tag_t bar_tag;
148 	bus_space_handle_t bar_memh;
149 	bus_size_t bar_size;
150 	bus_addr_t bar_addr;
151 
152 	int reg;
153 
154 	memset(tuples, 0, len);
155 
156 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
157 
158 	switch (cardbus_space) {
159 	case CARDBUS_CIS_ASI_TUPLE:
160 		DPRINTF(("%s: reading CIS data from configuration space\n",
161 		    sc->sc_dev.dv_xname));
162 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
163 			u_int32_t e = pci_conf_read(pc, tag, i);
164 			tuples[j] = 0xff & e;
165 			e >>= 8;
166 			tuples[j + 1] = 0xff & e;
167 			e >>= 8;
168 			tuples[j + 2] = 0xff & e;
169 			e >>= 8;
170 			tuples[j + 3] = 0xff & e;
171 			j += 4;
172 		}
173 		found++;
174 		break;
175 
176 	case CARDBUS_CIS_ASI_BAR0:
177 	case CARDBUS_CIS_ASI_BAR1:
178 	case CARDBUS_CIS_ASI_BAR2:
179 	case CARDBUS_CIS_ASI_BAR3:
180 	case CARDBUS_CIS_ASI_BAR4:
181 	case CARDBUS_CIS_ASI_BAR5:
182 	case CARDBUS_CIS_ASI_ROM:
183 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
184 			reg = CARDBUS_ROM_REG;
185 			DPRINTF(("%s: reading CIS data from ROM\n",
186 			    sc->sc_dev.dv_xname));
187 		} else {
188 			reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
189 			DPRINTF(("%s: reading CIS data from BAR%d\n",
190 			    sc->sc_dev.dv_xname, cardbus_space - 1));
191 		}
192 
193 		/* XXX zero register so mapreg_map doesn't get confused by old
194 		   contents */
195 		pci_conf_write(pc, tag, reg, 0);
196 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
197 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
198 		    &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
199 			printf("%s: can't map memory\n",
200 			    sc->sc_dev.dv_xname);
201 			return (1);
202 		}
203 
204 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
205 			pcireg_t exrom;
206 			int save;
207 			struct cardbus_rom_image_head rom_image;
208 			struct cardbus_rom_image *p;
209 
210 			save = splhigh();
211 			/* enable rom address decoder */
212 			exrom = pci_conf_read(pc, tag, reg);
213 			pci_conf_write(pc, tag, reg, exrom | 1);
214 
215 			command = pci_conf_read(pc, tag,
216 			    PCI_COMMAND_STATUS_REG);
217 			pci_conf_write(pc, tag,
218 			    PCI_COMMAND_STATUS_REG,
219 			    command | PCI_COMMAND_MEM_ENABLE);
220 
221 			if (cardbus_read_exrom(ca->ca_memt, bar_memh,
222 			    &rom_image))
223 				goto out;
224 
225 			for (p = SIMPLEQ_FIRST(&rom_image); p;
226 			    p = SIMPLEQ_NEXT(p, next)) {
227 				if (p->rom_image ==
228 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
229 					bus_space_read_region_1(p->romt,
230 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
231 					    tuples, MIN(p->image_size, len));
232 					found++;
233 					break;
234 				}
235 			}
236 
237 		out:
238 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
239 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
240 				free(p, M_DEVBUF, sizeof(*p));
241 			}
242 			exrom = pci_conf_read(pc, tag, reg);
243 			pci_conf_write(pc, tag, reg, exrom & ~1);
244 			splx(save);
245 		} else {
246 			command = pci_conf_read(pc, tag,
247 			    PCI_COMMAND_STATUS_REG);
248 			pci_conf_write(pc, tag,
249 			    PCI_COMMAND_STATUS_REG,
250     			    command | PCI_COMMAND_MEM_ENABLE);
251 			/* XXX byte order? */
252 			bus_space_read_region_1(ca->ca_memt, bar_memh,
253 			    cis_ptr, tuples, 256);
254 			found++;
255 		}
256 		command = pci_conf_read(pc, tag,
257 		    PCI_COMMAND_STATUS_REG);
258 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
259 		    command & ~PCI_COMMAND_MEM_ENABLE);
260 		pci_conf_write(pc, tag, reg, 0);
261 
262 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
263 		    bar_size);
264 		break;
265 
266 #ifdef DIAGNOSTIC
267 		default:
268 			panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
269 			    cardbus_space);
270 #endif
271 	}
272 	return (!found);
273 }
274 
275 STATIC void
276 parse_tuple(u_int8_t *tuple, int len, void *data)
277 {
278 	struct cardbus_cis_info *cis = data;
279 	int bar_index;
280 	int i;
281 	char *p;
282 
283 	switch (tuple[0]) {
284 	case PCMCIA_CISTPL_MANFID:
285 		if (tuple[1] < 4) {
286 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
287 			    __func__, tuple[1]));
288 			break;
289 		}
290 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
291 		cis->product = tuple[4] | (tuple[5] << 8);
292 		break;
293 	case PCMCIA_CISTPL_VERS_1:
294 		bcopy(tuple + 2, cis->cis1_info_buf, tuple[1]);
295 		i = 0;
296 		p = cis->cis1_info_buf + 2;
297 		while (i <
298 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
299 			if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
300 				break;
301 			cis->cis1_info[i++] = p;
302 			while (*p != '\0' && *p != '\xff')
303 				p++;
304 			if (*p == '\0')
305 				p++;
306 		}
307 		break;
308 	case PCMCIA_CISTPL_BAR:
309 		if (tuple[1] != 6) {
310 			DPRINTF(("%s: BAR with short length (%d)\n",
311 			    __func__, tuple[1]));
312 			break;
313 		}
314 		bar_index = tuple[2] & 7;
315 		if (bar_index == 0) {
316 			DPRINTF(("%s: invalid ASI in BAR tuple\n",
317 			    __func__));
318 			break;
319 		}
320 		bar_index--;
321 		cis->bar[bar_index].flags = tuple[2];
322 		cis->bar[bar_index].size = (tuple[4] << 0) |
323 		    (tuple[5] << 8) | (tuple[6] << 16) | (tuple[7] << 24);
324 		break;
325     case PCMCIA_CISTPL_FUNCID:
326 		cis->funcid = tuple[2];
327 		break;
328 
329     case PCMCIA_CISTPL_FUNCE:
330 		switch (cis->funcid) {
331 		case PCMCIA_FUNCTION_SERIAL:
332 			if (tuple[1] >= 2 &&
333 			    tuple[2] == 0
334 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) {
335 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
336 				cis->funce.serial.uart_present = 1;
337 			}
338 			break;
339 		case PCMCIA_FUNCTION_NETWORK:
340 			if (tuple[1] >= 8 && tuple[2] ==
341 			    PCMCIA_TPLFE_TYPE_LAN_NID) {
342 				if (tuple[3] >
343 				    sizeof(cis->funce.network.netid)) {
344 					DPRINTF(("%s: unknown network id type"
345 					    " (len = %d)\n", __func__,
346 					    tuple[3]));
347 				} else {
348 					cis->funce.network.netid_present = 1;
349 					bcopy(tuple + 4,
350 					    cis->funce.network.netid, tuple[3]);
351 				}
352 			}
353 		}
354 		break;
355 	}
356 }
357 
358 /*
359  * int cardbus_attach_card(struct cardbus_softc *sc)
360  *
361  *    This function attaches the card on the slot: turns on power,
362  *    reads and analyses tuple, sets configuration index.
363  *
364  *    This function returns the number of recognised device functions.
365  *    If no functions are recognised, return 0.
366  */
367 int
368 cardbus_attach_card(struct cardbus_softc *sc)
369 {
370 	cardbus_chipset_tag_t cc;
371 	cardbus_function_tag_t cf;
372 	int cdstatus;
373 	pcitag_t tag;
374 	pcireg_t id, class, cis_ptr;
375 	pcireg_t bhlc;
376 	u_int8_t *tuple;
377 	int function, nfunction;
378 	struct device *csc;
379 	int no_work_funcs = 0;
380 	cardbus_devfunc_t ct;
381 	pci_chipset_tag_t pc = sc->sc_pc;
382 	int i;
383 
384 	cc = sc->sc_cc;
385 	cf = sc->sc_cf;
386 
387 	DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
388 
389 	/* inspect initial voltage */
390 	if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
391 		DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
392 		    sc->sc_dev.dv_unit));
393 		return (0);
394 	}
395 
396 	/* XXX use fake function 8 to keep power on during whole configuration */
397 	enable_function(sc, cdstatus, 8);
398 
399 	function = 0;
400 
401 	tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, function);
402 
403 	/* Wait until power comes up.  Maximum 500 ms. */
404 	for (i = 0; i < 5; ++i) {
405 		id = pci_conf_read(pc, tag, PCI_ID_REG);
406 		if (id != 0xffffffff && id != 0)
407 			break;
408 		if (cold) {	/* before kernel thread invoked */
409 			delay(100*1000);
410 		} else {	/* thread context */
411 			if (tsleep_nsec(sc, PCATCH, "cardbus",
412 			    MSEC_TO_NSEC(100)) != EWOULDBLOCK) {
413 				break;
414 			}
415 		}
416 	}
417 	if (i == 5)
418 		return (0);
419 
420 	bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
421 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
422 	nfunction = PCI_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
423 
424 	tuple = malloc(2048, M_TEMP, M_NOWAIT);
425 	if (tuple == NULL)
426 		panic("no room for cardbus tuples");
427 
428 	for (function = 0; function < nfunction; function++) {
429 		struct cardbus_attach_args ca;
430 
431 		tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device,
432 		    function);
433 
434 		id = pci_conf_read(pc, tag, PCI_ID_REG);
435 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
436 		cis_ptr = pci_conf_read(pc, tag, CARDBUS_CIS_REG);
437 
438 		/* Invalid vendor ID value? */
439 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
440 			continue;
441 
442 		DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, "
443 		    "CIS 0x%x\n", PCI_VENDOR(id), PCI_PRODUCT(id),
444 		    cis_ptr));
445 
446 		enable_function(sc, cdstatus, function);
447 
448 		/* clean up every BAR */
449 		pci_conf_write(pc, tag, CARDBUS_BASE0_REG, 0);
450 		pci_conf_write(pc, tag, CARDBUS_BASE1_REG, 0);
451 		pci_conf_write(pc, tag, CARDBUS_BASE2_REG, 0);
452 		pci_conf_write(pc, tag, CARDBUS_BASE3_REG, 0);
453 		pci_conf_write(pc, tag, CARDBUS_BASE4_REG, 0);
454 		pci_conf_write(pc, tag, CARDBUS_BASE5_REG, 0);
455 		pci_conf_write(pc, tag, CARDBUS_ROM_REG, 0);
456 
457 		/* set initial latency and cacheline size */
458 		bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
459 		DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
460 		    function, bhlc));
461 		bhlc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
462 		    (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
463 		bhlc |= ((sc->sc_cacheline & PCI_CACHELINE_MASK) <<
464 		    PCI_CACHELINE_SHIFT);
465 		bhlc |= ((sc->sc_lattimer & PCI_LATTIMER_MASK) <<
466 		    PCI_LATTIMER_SHIFT);
467 
468 		pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
469 		bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
470 		DPRINTF(("0x%08x\n", bhlc));
471 
472 		if (PCI_LATTIMER(bhlc) < 0x10) {
473 			bhlc &= ~(PCI_LATTIMER_MASK <<
474 			    PCI_LATTIMER_SHIFT);
475 			bhlc |= (0x10 << PCI_LATTIMER_SHIFT);
476 			pci_conf_write(pc, tag, PCI_BHLC_REG,
477 			    bhlc);
478 		}
479 
480 		/*
481 		 * We need to allocate the ct here, since we might
482 		 * need it when reading the CIS
483 		 */
484 		if ((ct =
485 		    (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
486 		    M_DEVBUF, M_NOWAIT)) == NULL)
487 			panic("no room for cardbus_tag");
488 
489 		ct->ct_cc = sc->sc_cc;
490 		ct->ct_cf = sc->sc_cf;
491 		ct->ct_bus = sc->sc_bus;
492 		ct->ct_dev = sc->sc_device;
493 		ct->ct_func = function;
494 		ct->ct_sc = sc;
495 		sc->sc_funcs[function] = ct;
496 
497 		memset(&ca, 0, sizeof(ca));
498 
499 		ca.ca_unit = sc->sc_dev.dv_unit;
500 		ca.ca_ct = ct;
501 
502 		ca.ca_iot = sc->sc_iot;
503 		ca.ca_memt = sc->sc_memt;
504 		ca.ca_dmat = sc->sc_dmat;
505 		ca.ca_rbus_iot = sc->sc_rbus_iot;
506 		ca.ca_rbus_memt = sc->sc_rbus_memt;
507 		ca.ca_tag = tag;
508 		ca.ca_bus = sc->sc_bus;
509 		ca.ca_device = sc->sc_device;
510 		ca.ca_function = function;
511 		ca.ca_id = id;
512 		ca.ca_class = class;
513 		ca.ca_pc = sc->sc_pc;
514 
515 		ca.ca_intrline = sc->sc_intrline;
516 
517 		if (cis_ptr != 0) {
518 			if (cardbus_read_tuples(&ca, cis_ptr, tuple, 2048)) {
519 				printf("cardbus_attach_card: failed to "
520 				    "read CIS\n");
521 			} else {
522 #ifdef CARDBUS_DEBUG
523 				decode_tuples(tuple, 2048, print_tuple, NULL);
524 #endif
525 				decode_tuples(tuple, 2048, parse_tuple,
526 				    &ca.ca_cis);
527 			}
528 		}
529 
530 		if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
531 		    cardbussubmatch)) == NULL) {
532 			/* do not match */
533 			disable_function(sc, function);
534 			sc->sc_funcs[function] = NULL;
535 			free(ct, M_DEVBUF, sizeof(struct cardbus_devfunc));
536 		} else {
537 			/* found */
538 			ct->ct_device = csc;
539 			++no_work_funcs;
540 		}
541 	}
542 	/*
543 	 * XXX power down pseudo function 8 (this will power down the card
544 	 * if no functions were attached).
545 	 */
546 	disable_function(sc, 8);
547 	free(tuple, M_TEMP, 2048);
548 
549 	return (no_work_funcs);
550 }
551 
552 STATIC int
553 cardbussubmatch(struct device *parent, void *match, void *aux)
554 {
555 	struct cfdata *cf = match;
556 	struct cardbus_attach_args *ca = aux;
557 
558 	if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
559 		cf->cardbuscf_dev != ca->ca_unit) {
560 		return (0);
561 	}
562 	if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
563 	    cf->cardbuscf_function != ca->ca_function) {
564 		return (0);
565 	}
566 
567 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
568 }
569 
570 STATIC int
571 cardbusprint(void *aux, const char *pnp)
572 {
573 	struct cardbus_attach_args *ca = aux;
574 	char devinfo[256];
575 
576 	if (pnp) {
577 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
578 		    sizeof(devinfo));
579 		printf("%s at %s", devinfo, pnp);
580 	}
581 	printf(" dev %d function %d", ca->ca_device, ca->ca_function);
582 	if (!pnp) {
583 		pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo,
584 		    sizeof(devinfo));
585 		printf(" %s", devinfo);
586 	}
587 
588 	return (UNCONF);
589 }
590 
591 /*
592  * void cardbus_detach_card(struct cardbus_softc *sc)
593  *
594  *    This function detaches the card on the slot: detach device data
595  *    structure and turns off the power.
596  *
597  *    This function must not be called under interrupt context.
598  */
599 void
600 cardbus_detach_card(struct cardbus_softc *sc)
601 {
602 	struct cardbus_devfunc *ct;
603 	int f;
604 
605 	for (f = 0; f < 8; f++) {
606 		ct = sc->sc_funcs[f];
607 		if (ct == NULL)
608 			continue;
609 
610 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
611 		    ct->ct_device->dv_xname));
612 
613 		if (config_detach(ct->ct_device, 0) != 0) {
614 			printf("%s: cannot detach dev %s, function %d\n",
615 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
616 			    ct->ct_func);
617 		} else {
618 			sc->sc_poweron_func &= ~(1 << ct->ct_func);
619 			sc->sc_funcs[ct->ct_func] = NULL;
620 			free(ct, M_DEVBUF, sizeof(struct cardbus_devfunc));
621 		}
622 	}
623 
624 	sc->sc_poweron_func = 0;
625 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
626 }
627 
628 /*
629  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg, name)
630  *   Interrupt handler of pccard.
631  *  args:
632  *   cardbus_chipset_tag_t *cc
633  *   int irq:
634  */
635 void *
636 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
637     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg,
638     const char *name)
639 {
640 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
641 
642 	return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg, name);
643 }
644 
645 /*
646  * void cardbus_intr_disestablish(cc, cf, handler)
647  *   Interrupt handler of pccard.
648  *  args:
649  *   cardbus_chipset_tag_t *cc
650  */
651 void
652 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
653     void *handler)
654 {
655 	DPRINTF(("- pccard_intr_disestablish\n"));
656 
657 	(*cf->cardbus_intr_disestablish)(cc, handler);
658 }
659 
660 /* XXX this should be merged with cardbus_function_{enable,disable},
661    but we don't have a ct when these functions are called */
662 
663 STATIC void
664 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
665 {
666 	if (sc->sc_poweron_func == 0) {
667 		/* switch to 3V and/or wait for power to stabilize */
668 		if (cdstatus & CARDBUS_3V_CARD) {
669 			sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
670 		} else {
671 			/* No cards other than 3.3V cards. */
672 			return;
673 		}
674 		(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
675 	}
676 	sc->sc_poweron_func |= (1 << function);
677 }
678 
679 STATIC void
680 disable_function(struct cardbus_softc *sc, int function)
681 {
682 	sc->sc_poweron_func &= ~(1 << function);
683 	if (sc->sc_poweron_func == 0) {
684 		/* power-off because no functions are enabled */
685 		sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
686 	}
687 }
688 
689 /*
690  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
691  *
692  *   This function enables a function on a card.  When no power is
693  *  applied on the card, power will be applied on it.
694  */
695 int
696 cardbus_function_enable(struct cardbus_softc *sc, int func)
697 {
698 	pci_chipset_tag_t pc = sc->sc_pc;
699 	pcireg_t command;
700 	pcitag_t tag;
701 
702 	DPRINTF(("entering cardbus_function_enable...  "));
703 
704 	/* entering critical area */
705 
706 	/* XXX: sc_vold should be used */
707 	enable_function(sc, CARDBUS_3V_CARD, func);
708 
709 	/* exiting critical area */
710 
711 	tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, func);
712 
713 	command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
714 	command |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_IO_ENABLE |
715 	    PCI_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
716 
717 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, command);
718 
719 	DPRINTF(("%x\n", sc->sc_poweron_func));
720 
721 	return (0);
722 }
723 
724 /*
725  * int cardbus_function_disable(struct cardbus_softc *, int func)
726  *
727  *   This function disable a function on a card.  When no functions are
728  *  enabled, it turns off the power.
729  */
730 int
731 cardbus_function_disable(struct cardbus_softc *sc, int func)
732 {
733 	DPRINTF(("entering cardbus_function_disable...  "));
734 
735 	disable_function(sc, func);
736 
737 	return (0);
738 }
739 
740 int
741 cardbus_matchbyid(struct cardbus_attach_args *ca,
742     const struct pci_matchid *ids, int nent)
743 {
744 	const struct pci_matchid *pm;
745 	int i;
746 
747 	for (i = 0, pm = ids; i < nent; i++, pm++)
748 		if (PCI_VENDOR(ca->ca_id) == pm->pm_vid &&
749 		    PCI_PRODUCT(ca->ca_id) == pm->pm_pid)
750 			return (1);
751 	return (0);
752 }
753 
754 /*
755  * below this line, there are some functions for decoding tuples.
756  * They should go out from this file.
757  */
758 
759 STATIC u_int8_t *
760 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
761 
762 STATIC int
763 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
764 {
765 	u_int8_t *tp = tuple;
766 
767 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
768 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
769 		return (0);
770 	}
771 
772 	while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
773 		;
774 
775 	return (1);
776 }
777 
778 STATIC u_int8_t *
779 decode_tuple(u_int8_t *tuple, u_int8_t *end, tuple_decode_func func,
780     void *data)
781 {
782 	u_int8_t type;
783 	u_int8_t len;
784 
785 	type = tuple[0];
786 	switch (type) {
787 	case PCMCIA_CISTPL_NULL:
788 	case PCMCIA_CISTPL_END:
789 		len = 1;
790 		break;
791 	default:
792 		if (tuple + 2 > end)
793 			return (NULL);
794 		len = tuple[1] + 2;
795 		break;
796 	}
797 
798 	if (tuple + len > end)
799 		return (NULL);
800 
801 	(*func)(tuple, len, data);
802 
803 	if (PCMCIA_CISTPL_END == type || tuple + len == end)
804 		return (NULL);
805 
806 	return (tuple + len);
807 }
808 
809 #ifdef CARDBUS_DEBUG
810 static char *tuple_name(int type);
811 
812 static char *
813 tuple_name(int type)
814 {
815 	static char *tuple_name_s [] = {
816 	    "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved",	  /* 0-3 */
817 	    "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR",	  /* 4-7 */
818 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 8-B */
819 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* C-F */
820 	    "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
821 	    "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",		  /* 14-17 */
822 	    "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",	  /* 18-1B */
823 	    "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO",		  /* 1C-1E */
824 	    "DEVICE_GEO_A", "MANFID", "FUNCID", "FUNCE", "SWIL",  /* 1F-23 */
825 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 24-27 */
826 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 28-2B */
827 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 2C-2F */
828 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 30-33 */
829 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 34-37 */
830 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 38-3B */
831 	    "Reserved", "Reserved", "Reserved", "Reserved",	  /* 3C-3F */
832 	    "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",	  /* 40-43 */
833 	    "DATE", "BATTERY", "ORG", "FORMAT_A"		  /* 44-47 */
834 	};
835 
836 	if (type > 0 && type < nitems(tuple_name_s))
837 		return (tuple_name_s[type]);
838 	else if (0xff == type)
839 		return ("END");
840 	else
841 		return ("Reserved");
842 }
843 
844 static void
845 print_tuple(u_int8_t *tuple, int len, void *data)
846 {
847 	int i;
848 
849 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
850 
851 	for (i = 0; i < len; ++i) {
852 		if (i % 16 == 0)
853 			printf("  0x%02x:", i);
854 		printf(" %x",tuple[i]);
855 		if (i % 16 == 15)
856 			printf("\n");
857 	}
858 	if (i % 16 != 0)
859 		printf("\n");
860 }
861 #endif
862