pccard.c revision 122032
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2
3/*
4 * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Marc Horowitz.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 122032 2003-11-04 06:30:59Z imp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/kernel.h>
40#include <sys/queue.h>
41#include <sys/sysctl.h>
42#include <sys/types.h>
43
44#include <sys/bus.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#include <machine/resource.h>
48
49#include <net/ethernet.h>
50
51#include <dev/pccard/pccardreg.h>
52#include <dev/pccard/pccardvar.h>
53
54#include "power_if.h"
55#include "card_if.h"
56
57#define PCCARDDEBUG
58
59/* sysctl vars */
60SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
61
62int	pccard_debug = 0;
63TUNABLE_INT("hw.pccard.debug", &pccard_debug);
64SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
65    &pccard_debug, 0,
66  "pccard debug");
67
68int	pccard_cis_debug = 0;
69TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
70SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
71    &pccard_cis_debug, 0, "pccard CIS debug");
72
73#ifdef PCCARDDEBUG
74#define	DPRINTF(arg) if (pccard_debug) printf arg
75#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
76#define PRVERBOSE(arg) printf arg
77#define DEVPRVERBOSE(arg) device_printf arg
78#else
79#define	DPRINTF(arg)
80#define	DEVPRINTF(arg)
81#define PRVERBOSE(arg) if (bootverbose) printf arg
82#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
83#endif
84
85static int	pccard_ccr_read(struct pccard_function *pf, int ccr);
86static void	pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
87static int	pccard_attach_card(device_t dev);
88static int	pccard_detach_card(device_t dev);
89static void	pccard_function_init(struct pccard_function *pf);
90static void	pccard_function_free(struct pccard_function *pf);
91static int	pccard_function_enable(struct pccard_function *pf);
92static void	pccard_function_disable(struct pccard_function *pf);
93static int	pccard_compat_do_probe(device_t bus, device_t dev);
94static int	pccard_compat_do_attach(device_t bus, device_t dev);
95static int	pccard_add_children(device_t dev, int busno);
96static int	pccard_probe(device_t dev);
97static int	pccard_attach(device_t dev);
98static int	pccard_detach(device_t dev);
99static void	pccard_print_resources(struct resource_list *rl,
100		    const char *name, int type, int count, const char *format);
101static int	pccard_print_child(device_t dev, device_t child);
102static int	pccard_set_resource(device_t dev, device_t child, int type,
103		    int rid, u_long start, u_long count);
104static int	pccard_get_resource(device_t dev, device_t child, int type,
105		    int rid, u_long *startp, u_long *countp);
106static void	pccard_delete_resource(device_t dev, device_t child, int type,
107		    int rid);
108static int	pccard_set_res_flags(device_t dev, device_t child, int type,
109		    int rid, u_int32_t flags);
110static int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
111		    u_int32_t offset, u_int32_t *deltap);
112static void	pccard_probe_nomatch(device_t cbdev, device_t child);
113static int	pccard_read_ivar(device_t bus, device_t child, int which,
114		    u_char *result);
115static void	pccard_driver_added(device_t dev, driver_t *driver);
116static struct resource *pccard_alloc_resource(device_t dev,
117		    device_t child, int type, int *rid, u_long start,
118		    u_long end, u_long count, u_int flags);
119static int	pccard_release_resource(device_t dev, device_t child, int type,
120		    int rid, struct resource *r);
121static void	pccard_child_detached(device_t parent, device_t dev);
122static void	pccard_intr(void *arg);
123static int	pccard_setup_intr(device_t dev, device_t child,
124		    struct resource *irq, int flags, driver_intr_t *intr,
125		    void *arg, void **cookiep);
126static int	pccard_teardown_intr(device_t dev, device_t child,
127		    struct resource *r, void *cookie);
128
129static const struct pccard_product *
130pccard_do_product_lookup(device_t bus, device_t dev,
131			 const struct pccard_product *tab, size_t ent_size,
132			 pccard_product_match_fn matchfn);
133
134
135static int
136pccard_ccr_read(struct pccard_function *pf, int ccr)
137{
138	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
139	    pf->pf_ccr_offset + ccr));
140}
141
142static void
143pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
144{
145	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
146		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
147		    pf->pf_ccr_offset + ccr, val);
148	}
149}
150
151static int
152pccard_set_default_descr(device_t dev)
153{
154	const char *vendorstr, *prodstr;
155	char *str;
156
157	if (pccard_get_vendor_str(dev, &vendorstr))
158		return (0);
159	if (pccard_get_product_str(dev, &prodstr))
160		return (0);
161	str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
162	    M_WAITOK);
163	sprintf(str, "%s %s", vendorstr, prodstr);
164	device_set_desc_copy(dev, str);
165	free(str, M_DEVBUF);
166	return (0);
167}
168
169static int
170pccard_attach_card(device_t dev)
171{
172	struct pccard_softc *sc = PCCARD_SOFTC(dev);
173	struct pccard_function *pf;
174	struct pccard_ivar *ivar;
175	device_t child;
176	int i;
177
178	/*
179	 * this is here so that when socket_enable calls gettype, trt happens
180	 */
181	STAILQ_INIT(&sc->card.pf_head);
182
183	DEVPRINTF((dev, "chip_socket_enable\n"));
184	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
185
186	DEVPRINTF((dev, "read_cis\n"));
187	pccard_read_cis(sc);
188
189	DEVPRINTF((dev, "check_cis_quirks\n"));
190	pccard_check_cis_quirks(dev);
191
192	/*
193	 * bail now if the card has no functions, or if there was an error in
194	 * the cis.
195	 */
196
197	if (sc->card.error) {
198		device_printf(dev, "CARD ERROR!\n");
199		return (1);
200	}
201	if (STAILQ_EMPTY(&sc->card.pf_head)) {
202		device_printf(dev, "Card has no functions!\n");
203		return (1);
204	}
205
206	if (bootverbose || pccard_debug)
207		pccard_print_cis(dev);
208
209	DEVPRINTF((dev, "functions scanning\n"));
210	i = -1;
211	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
212		i++;
213		if (STAILQ_EMPTY(&pf->cfe_head)) {
214			device_printf(dev,
215			    "Function %d has no config entries.!\n", i);
216			continue;
217		}
218		pf->sc = sc;
219		pf->cfe = NULL;
220		pf->dev = NULL;
221	}
222	DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
223	    pccard_mfc(sc)));
224
225	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
226		if (STAILQ_EMPTY(&pf->cfe_head))
227			continue;
228		/*
229		 * In NetBSD, the drivers are responsible for activating
230		 * each function of a card.  I think that in FreeBSD we
231		 * want to activate them enough for the usual bus_*_resource
232		 * routines will do the right thing.  This many mean a
233		 * departure from the current NetBSD model.
234		 *
235		 * This seems to work well in practice for most cards.
236		 * However, there are two cases that are problematic.
237		 * If a driver wishes to pick and chose which config
238		 * entry to use, then this method falls down.  These
239		 * are usually older cards.  In addition, there are
240		 * some cards that have multiple hardware units on the
241		 * cards, but presents only one CIS chain.  These cards
242		 * are combination cards, but only one of these units
243		 * can be on at a time.
244		 */
245		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
246		    M_WAITOK | M_ZERO);
247		child = device_add_child(dev, NULL, -1);
248		device_set_ivars(child, ivar);
249		ivar->fcn = pf;
250		pf->dev = child;
251		/*
252		 * XXX We might want to move the next two lines into
253		 * XXX the pccard interface layer.  For the moment, this
254		 * XXX is OK, but some drivers want to pick the config
255		 * XXX entry to use as well as some address tweaks (mostly
256		 * XXX due to bugs in decode logic that makes some
257		 * XXX addresses illegal or broken).
258		 */
259		pccard_function_init(pf);
260		if (sc->sc_enabled_count == 0)
261			POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
262		if (pccard_function_enable(pf) == 0 &&
263		    pccard_set_default_descr(child) == 0 &&
264		    device_probe_and_attach(child) == 0) {
265			DEVPRINTF((sc->dev, "function %d CCR at %d "
266			    "offset %x mask %x: "
267			    "%x %x %x %x, %x %x %x %x, %x\n",
268			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
269			    pf->ccr_mask, pccard_ccr_read(pf, 0x00),
270			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
271			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
272			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
273			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
274		} else {
275			if (pf->cfe != NULL)
276				pccard_function_disable(pf);
277		}
278	}
279	return (0);
280}
281
282static int
283pccard_detach_card(device_t dev)
284{
285	struct pccard_softc *sc = PCCARD_SOFTC(dev);
286	struct pccard_function *pf;
287	struct pccard_config_entry *cfe;
288	int state;
289
290	/*
291	 * We are running on either the PCCARD socket's event thread
292	 * or in user context detaching a device by user request.
293	 */
294	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
295		if (pf->dev == NULL)
296			continue;
297		state = device_get_state(pf->dev);
298		if (state == DS_ATTACHED || state == DS_BUSY)
299			device_detach(pf->dev);
300		if (pf->cfe != NULL)
301			pccard_function_disable(pf);
302		pccard_function_free(pf);
303		device_delete_child(dev, pf->dev);
304	}
305	if (sc->sc_enabled_count == 0)
306		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
307
308	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
309		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
310			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
311			free(cfe, M_DEVBUF);
312		}
313		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
314		free(pf, M_DEVBUF);
315	}
316	return (0);
317}
318
319static const struct pccard_product *
320pccard_do_product_lookup(device_t bus, device_t dev,
321    const struct pccard_product *tab, size_t ent_size,
322    pccard_product_match_fn matchfn)
323{
324	const struct pccard_product *ent;
325	int matches;
326	u_int32_t fcn;
327	u_int32_t vendor;
328	u_int32_t prod;
329	const char *vendorstr;
330	const char *prodstr;
331
332#ifdef DIAGNOSTIC
333	if (sizeof *ent > ent_size)
334		panic("pccard_product_lookup: bogus ent_size %jd",
335		    (intmax_t) ent_size);
336#endif
337	if (pccard_get_vendor(dev, &vendor))
338		return (NULL);
339	if (pccard_get_product(dev, &prod))
340		return (NULL);
341	if (pccard_get_function_number(dev, &fcn))
342		return (NULL);
343	if (pccard_get_vendor_str(dev, &vendorstr))
344		return (NULL);
345	if (pccard_get_product_str(dev, &prodstr))
346		return (NULL);
347	for (ent = tab; ent->pp_vendor != 0; ent =
348	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
349		matches = 1;
350		if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
351		    ent->pp_product == PCCARD_PRODUCT_ANY &&
352		    ent->pp_cis[0] == NULL &&
353		    ent->pp_cis[1] == NULL) {
354			if (ent->pp_name)
355				device_printf(dev,
356				    "Total wildcard entry ignored for %s\n",
357				    ent->pp_name);
358			continue;
359		}
360		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
361		    vendor != ent->pp_vendor)
362			matches = 0;
363		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
364		    prod != ent->pp_product)
365			matches = 0;
366		if (matches && fcn != ent->pp_expfunc)
367			matches = 0;
368		if (matches && ent->pp_cis[0] &&
369		    strcmp(ent->pp_cis[0], vendorstr) != 0)
370			matches = 0;
371		if (matches && ent->pp_cis[1] &&
372		    strcmp(ent->pp_cis[1], prodstr) != 0)
373			matches = 0;
374		/* XXX need to match cis[2] and cis[3] also XXX */
375		if (matchfn != NULL)
376			matches = (*matchfn)(dev, ent, matches);
377		if (matches)
378			return (ent);
379	}
380	return (NULL);
381}
382
383/*
384 * Initialize a PCCARD function.  May be called as long as the function is
385 * disabled.
386 *
387 * Note: pccard_function_init should not keep resources allocated.  It should
388 * only set them up ala isa pnp, set the values in the rl lists, and return.
389 * Any resource held after pccard_function_init is called is a bug.  However,
390 * the bus routines to get the resources also assume that pccard_function_init
391 * does this, so they need to be fixed too.
392 */
393static void
394pccard_function_init(struct pccard_function *pf)
395{
396	struct pccard_config_entry *cfe;
397	int i;
398	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
399	struct resource_list *rl = &devi->resources;
400	struct resource_list_entry *rle;
401	struct resource *r = 0;
402	device_t bus;
403	int start;
404	int end;
405	int spaces;
406
407	if (pf->pf_flags & PFF_ENABLED) {
408		printf("pccard_function_init: function is enabled");
409		return;
410	}
411	bus = device_get_parent(pf->dev);
412	/* Remember which configuration entry we are using. */
413	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
414		for (i = 0; i < cfe->num_iospace; i++)
415			cfe->iores[i] = NULL;
416		cfe->irqres = NULL;
417		spaces = 0;
418		for (i = 0; i < cfe->num_iospace; i++) {
419			start = cfe->iospace[i].start;
420			if (start)
421				end = start + cfe->iospace[i].length - 1;
422			else
423				end = ~0;
424			cfe->iorid[i] = i;
425			DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
426			    i, start, end));
427			r = cfe->iores[i] = bus_alloc_resource(bus,
428			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
429			    cfe->iospace[i].length,
430			    rman_make_alignment_flags(cfe->iospace[i].length));
431			if (cfe->iores[i] == NULL)
432				goto not_this_one;
433			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
434			    rman_get_start(r), rman_get_end(r),
435			    cfe->iospace[i].length);
436			rle = resource_list_find(rl, SYS_RES_IOPORT,
437			    cfe->iorid[i]);
438			rle->res = r;
439			spaces++;
440		}
441		if (cfe->num_memspace > 0) {
442			/*
443			 * Not implement yet, Fix me.
444			 */
445			DEVPRINTF((bus, "Memory space not yet implemented.\n"));
446		}
447		if (spaces == 0) {
448			DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
449			goto not_this_one;
450		}
451		if (cfe->irqmask) {
452			cfe->irqrid = 0;
453			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
454			    &cfe->irqrid, 0, ~0, 1, 0);
455			if (cfe->irqres == NULL)
456				goto not_this_one;
457			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
458			    rman_get_start(r), rman_get_end(r), 1);
459			rle = resource_list_find(rl, SYS_RES_IRQ,
460			    cfe->irqrid);
461			rle->res = r;
462		}
463		/* If we get to here, we've allocated all we need */
464		pf->cfe = cfe;
465		break;
466	    not_this_one:;
467		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
468		    cfe->number));
469		/*
470		 * Release resources that we partially allocated
471		 * from this config entry.
472		 */
473		for (i = 0; i < cfe->num_iospace; i++) {
474			if (cfe->iores[i] != NULL) {
475				bus_release_resource(bus, SYS_RES_IOPORT,
476				    cfe->iorid[i], cfe->iores[i]);
477				rle = resource_list_find(rl, SYS_RES_IOPORT,
478				    cfe->iorid[i]);
479				rle->res = NULL;
480				resource_list_delete(rl, SYS_RES_IOPORT,
481				    cfe->iorid[i]);
482			}
483			cfe->iores[i] = NULL;
484		}
485		if (cfe->irqmask && cfe->irqres != NULL) {
486			bus_release_resource(bus, SYS_RES_IRQ,
487			    cfe->irqrid, cfe->irqres);
488			rle = resource_list_find(rl, SYS_RES_IRQ,
489			    cfe->irqrid);
490			rle->res = NULL;
491			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
492			cfe->irqres = NULL;
493		}
494	}
495}
496
497/*
498 * Free resources allocated by pccard_function_init(), May be called as long
499 * as the function is disabled.
500 *
501 * NOTE: This function should be unnecessary.  pccard_function_init should
502 * never keep resources initialized.
503 */
504static void
505pccard_function_free(struct pccard_function *pf)
506{
507	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
508	struct resource_list_entry *rle;
509
510	if (pf->pf_flags & PFF_ENABLED) {
511		printf("pccard_function_init: function is enabled");
512		return;
513	}
514
515	SLIST_FOREACH(rle, &devi->resources, link) {
516		if (rle->res) {
517			if (rman_get_device(rle->res) != pf->sc->dev)
518				device_printf(pf->sc->dev,
519				    "function_free: Resource still owned by "
520				    "child, oops. "
521				    "(type=%d, rid=%d, addr=%lx)\n",
522				    rle->type, rle->rid,
523				    rman_get_start(rle->res));
524			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
525			    pf->sc->dev, rle->type, rle->rid, rle->res);
526			rle->res = NULL;
527		}
528	}
529	resource_list_free(&devi->resources);
530}
531
532static void
533pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
534    bus_addr_t offset, bus_size_t size)
535{
536	bus_size_t iosize, tmp;
537
538	if (addr != 0) {
539		if (pf->pf_mfc_iomax == 0) {
540			pf->pf_mfc_iobase = addr + offset;
541			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
542		} else {
543			/* this makes the assumption that nothing overlaps */
544			if (pf->pf_mfc_iobase > addr + offset)
545				pf->pf_mfc_iobase = addr + offset;
546			if (pf->pf_mfc_iomax < addr + offset + size)
547				pf->pf_mfc_iomax = addr + offset + size;
548		}
549	}
550
551	tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
552	/* round up to nearest (2^n)-1 */
553	for (iosize = 1; iosize < tmp; iosize <<= 1)
554		;
555	iosize--;
556
557	DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
558	    (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
559	pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
560	    pf->pf_mfc_iobase & 0xff);
561	pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
562	    (pf->pf_mfc_iobase >> 8) & 0xff);
563	pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
564	pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
565	pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
566}
567
568/* Enable a PCCARD function */
569static int
570pccard_function_enable(struct pccard_function *pf)
571{
572	struct pccard_function *tmp;
573	int reg;
574	device_t dev = pf->sc->dev;
575
576	if (pf->cfe == NULL) {
577		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
578		return (ENOMEM);
579	}
580
581	/*
582	 * Increase the reference count on the socket, enabling power, if
583	 * necessary.
584	 */
585	pf->sc->sc_enabled_count++;
586
587	if (pf->pf_flags & PFF_ENABLED) {
588		/*
589		 * Don't do anything if we're already enabled.
590		 */
591		return (0);
592	}
593
594	/*
595	 * it's possible for different functions' CCRs to be in the same
596	 * underlying page.  Check for that.
597	 */
598	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
599		if ((tmp->pf_flags & PFF_ENABLED) &&
600		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
601		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
602		    (tmp->ccr_base - tmp->pf_ccr_offset +
603		    tmp->pf_ccr_realsize))) {
604			pf->pf_ccrt = tmp->pf_ccrt;
605			pf->pf_ccrh = tmp->pf_ccrh;
606			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
607
608			/*
609			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
610			 * tmp->ccr_base) + pf->ccr_base;
611			 */
612			/* pf->pf_ccr_offset =
613			    (tmp->pf_ccr_offset + pf->ccr_base) -
614			    tmp->ccr_base; */
615			pf->pf_ccr_window = tmp->pf_ccr_window;
616			break;
617		}
618	}
619	if (tmp == NULL) {
620		pf->ccr_rid = 0;
621		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
622		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
623		if (!pf->ccr_res)
624			goto bad;
625		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
626		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
627		    pf->ccr_base));
628		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
629		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
630		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
631		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
632		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
633		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
634		pf->pf_ccr_realsize = 1;
635	}
636
637	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
638	reg |= PCCARD_CCR_OPTION_LEVIREQ;
639	if (pccard_mfc(pf->sc)) {
640		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
641			PCCARD_CCR_OPTION_ADDR_DECODE);
642		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
643	}
644	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
645
646	reg = 0;
647	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
648		reg |= PCCARD_CCR_STATUS_IOIS8;
649	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
650		reg |= PCCARD_CCR_STATUS_AUDIO;
651	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
652
653	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
654
655	if (pccard_mfc(pf->sc))
656		pccard_mfc_adjust_iobase(pf, 0, 0, 0);
657
658#ifdef PCCARDDEBUG
659	if (pccard_debug) {
660		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
661			device_printf(tmp->sc->dev,
662			    "function %d CCR at %d offset %x: "
663			    "%x %x %x %x, %x %x %x %x, %x\n",
664			    tmp->number, tmp->pf_ccr_window,
665			    tmp->pf_ccr_offset,
666			    pccard_ccr_read(tmp, 0x00),
667			    pccard_ccr_read(tmp, 0x02),
668			    pccard_ccr_read(tmp, 0x04),
669			    pccard_ccr_read(tmp, 0x06),
670			    pccard_ccr_read(tmp, 0x0A),
671			    pccard_ccr_read(tmp, 0x0C),
672			    pccard_ccr_read(tmp, 0x0E),
673			    pccard_ccr_read(tmp, 0x10),
674			    pccard_ccr_read(tmp, 0x12));
675		}
676	}
677#endif
678	pf->pf_flags |= PFF_ENABLED;
679	return (0);
680
681 bad:
682	/*
683	 * Decrement the reference count, and power down the socket, if
684	 * necessary.
685	 */
686	pf->sc->sc_enabled_count--;
687	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
688
689	return (1);
690}
691
692/* Disable PCCARD function. */
693static void
694pccard_function_disable(struct pccard_function *pf)
695{
696	struct pccard_function *tmp;
697	device_t dev = pf->sc->dev;
698
699	if (pf->cfe == NULL)
700		panic("pccard_function_disable: function not initialized");
701
702	if ((pf->pf_flags & PFF_ENABLED) == 0) {
703		/*
704		 * Don't do anything if we're already disabled.
705		 */
706		return;
707	}
708
709	if (pf->intr_handler != NULL) {
710		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
711		struct resource_list_entry *rle =
712		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
713		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
714		    pf->intr_handler_cookie);
715	}
716
717	/*
718	 * it's possible for different functions' CCRs to be in the same
719	 * underlying page.  Check for that.  Note we mark us as disabled
720	 * first to avoid matching ourself.
721	 */
722
723	pf->pf_flags &= ~PFF_ENABLED;
724	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
725		if ((tmp->pf_flags & PFF_ENABLED) &&
726		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
727		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
728		    (tmp->ccr_base - tmp->pf_ccr_offset +
729		    tmp->pf_ccr_realsize)))
730			break;
731	}
732
733	/* Not used by anyone else; unmap the CCR. */
734	if (tmp == NULL) {
735		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
736		    pf->ccr_res);
737		pf->ccr_res = NULL;
738	}
739
740	/*
741	 * Decrement the reference count, and power down the socket, if
742	 * necessary.
743	 */
744	pf->sc->sc_enabled_count--;
745}
746
747/*
748 * simulate the old "probe" routine.  In the new world order, the driver
749 * needs to grab devices while in the old they were assigned to the device by
750 * the pccardd process.  These symbols are exported to the upper layers.
751 */
752static int
753pccard_compat_do_probe(device_t bus, device_t dev)
754{
755	return (CARD_COMPAT_MATCH(dev));
756}
757
758static int
759pccard_compat_do_attach(device_t bus, device_t dev)
760{
761	int err;
762
763	err = CARD_COMPAT_PROBE(dev);
764	if (err <= 0)
765		err = CARD_COMPAT_ATTACH(dev);
766	return (err);
767}
768
769#define PCCARD_NPORT	2
770#define PCCARD_NMEM	5
771#define PCCARD_NIRQ	1
772#define PCCARD_NDRQ	0
773
774static int
775pccard_add_children(device_t dev, int busno)
776{
777	/* Call parent to scan for any current children */
778	return (0);
779}
780
781static int
782pccard_probe(device_t dev)
783{
784	device_set_desc(dev, "16-bit PCCard bus");
785	return (pccard_add_children(dev, device_get_unit(dev)));
786}
787
788static int
789pccard_attach(device_t dev)
790{
791	struct pccard_softc *sc = PCCARD_SOFTC(dev);
792
793	sc->dev = dev;
794	sc->sc_enabled_count = 0;
795	return (bus_generic_attach(dev));
796}
797
798static int
799pccard_detach(device_t dev)
800{
801	pccard_detach_card(dev);
802	return 0;
803}
804
805static int
806pccard_suspend(device_t self)
807{
808	pccard_detach_card(self);
809	return (0);
810}
811
812static
813int
814pccard_resume(device_t self)
815{
816	return (0);
817}
818
819static void
820pccard_print_resources(struct resource_list *rl, const char *name, int type,
821    int count, const char *format)
822{
823	struct resource_list_entry *rle;
824	int printed;
825	int i;
826
827	printed = 0;
828	for (i = 0; i < count; i++) {
829		rle = resource_list_find(rl, type, i);
830		if (rle != NULL) {
831			if (printed == 0)
832				printf(" %s ", name);
833			else if (printed > 0)
834				printf(",");
835			printed++;
836			printf(format, rle->start);
837			if (rle->count > 1) {
838				printf("-");
839				printf(format, rle->start + rle->count - 1);
840			}
841		} else if (i > 3) {
842			/* check the first few regardless */
843			break;
844		}
845	}
846}
847
848static int
849pccard_print_child(device_t dev, device_t child)
850{
851	struct pccard_ivar *devi = PCCARD_IVAR(child);
852	struct resource_list *rl = &devi->resources;
853	int retval = 0;
854
855	retval += bus_print_child_header(dev, child);
856	retval += printf(" at");
857
858	if (devi != NULL) {
859		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
860		    PCCARD_NPORT, "%#lx");
861		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
862		    PCCARD_NMEM, "%#lx");
863		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
864		    "%ld");
865		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
866		    "%ld");
867		retval += printf(" function %d config %d", devi->fcn->number,
868		    devi->fcn->cfe->number);
869	}
870
871	retval += bus_print_child_footer(dev, child);
872
873	return (retval);
874}
875
876static int
877pccard_set_resource(device_t dev, device_t child, int type, int rid,
878		 u_long start, u_long count)
879{
880	struct pccard_ivar *devi = PCCARD_IVAR(child);
881	struct resource_list *rl = &devi->resources;
882
883	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
884	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
885		return (EINVAL);
886	if (rid < 0)
887		return (EINVAL);
888	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
889		return (EINVAL);
890	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
891		return (EINVAL);
892	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
893		return (EINVAL);
894	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
895		return (EINVAL);
896
897	resource_list_add(rl, type, rid, start, start + count - 1, count);
898	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
899	    type, &rid, start, start + count - 1, count, 0))
900		return 0;
901	else
902		return ENOMEM;
903}
904
905static int
906pccard_get_resource(device_t dev, device_t child, int type, int rid,
907    u_long *startp, u_long *countp)
908{
909	struct pccard_ivar *devi = PCCARD_IVAR(child);
910	struct resource_list *rl = &devi->resources;
911	struct resource_list_entry *rle;
912
913	rle = resource_list_find(rl, type, rid);
914	if (rle == NULL)
915		return (ENOENT);
916
917	if (startp != NULL)
918		*startp = rle->start;
919	if (countp != NULL)
920		*countp = rle->count;
921
922	return (0);
923}
924
925static void
926pccard_delete_resource(device_t dev, device_t child, int type, int rid)
927{
928	struct pccard_ivar *devi = PCCARD_IVAR(child);
929	struct resource_list *rl = &devi->resources;
930	resource_list_delete(rl, type, rid);
931}
932
933static int
934pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
935    u_int32_t flags)
936{
937	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
938	    rid, flags));
939}
940
941static int
942pccard_set_memory_offset(device_t dev, device_t child, int rid,
943    u_int32_t offset, u_int32_t *deltap)
944
945{
946	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
947	    offset, deltap));
948}
949
950static void
951pccard_probe_nomatch(device_t bus, device_t child)
952{
953	struct pccard_ivar *devi = PCCARD_IVAR(child);
954	struct pccard_function *func = devi->fcn;
955	struct pccard_softc *sc = PCCARD_SOFTC(bus);
956
957	device_printf(bus, "<unknown card>");
958	printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
959	  sc->card.manufacturer, sc->card.product, func->number);
960	device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
961	  sc->card.cis1_info[1], sc->card.cis1_info[2]);
962	return;
963}
964
965static int
966pccard_child_location_str(device_t bus, device_t child, char *buf,
967    size_t buflen)
968{
969	struct pccard_ivar *devi = PCCARD_IVAR(child);
970	struct pccard_function *func = devi->fcn;
971
972	snprintf(buf, buflen, "function=%d", func->number);
973	return (0);
974}
975
976static int
977pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
978    size_t buflen)
979{
980	struct pccard_ivar *devi = PCCARD_IVAR(child);
981	struct pccard_function *func = devi->fcn;
982	struct pccard_softc *sc = PCCARD_SOFTC(bus);
983
984	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
985	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
986	    sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
987	    sc->card.cis1_info[1], func->function);
988	return (0);
989}
990
991static int
992pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
993{
994	struct pccard_ivar *devi = PCCARD_IVAR(child);
995	struct pccard_function *func = devi->fcn;
996	struct pccard_softc *sc = PCCARD_SOFTC(bus);
997
998	switch (which) {
999	default:
1000	case PCCARD_IVAR_ETHADDR:
1001		bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1002		break;
1003	case PCCARD_IVAR_VENDOR:
1004		*(u_int32_t *) result = sc->card.manufacturer;
1005		break;
1006	case PCCARD_IVAR_PRODUCT:
1007		*(u_int32_t *) result = sc->card.product;
1008		break;
1009	case PCCARD_IVAR_PRODEXT:
1010		*(u_int16_t *) result = sc->card.prodext;
1011		break;
1012	case PCCARD_IVAR_FUNCTION:
1013		*(u_int32_t *) result = func->function;
1014		break;
1015	case PCCARD_IVAR_FUNCTION_NUMBER:
1016		if (!func) {
1017			device_printf(bus, "No function number, bug!\n");
1018			return (ENOENT);
1019		}
1020		*(u_int32_t *) result = func->number;
1021		break;
1022	case PCCARD_IVAR_VENDOR_STR:
1023		*(char **) result = sc->card.cis1_info[0];
1024		break;
1025	case PCCARD_IVAR_PRODUCT_STR:
1026		*(char **) result = sc->card.cis1_info[1];
1027		break;
1028	case PCCARD_IVAR_CIS3_STR:
1029		*(char **) result = sc->card.cis1_info[2];
1030		break;
1031	case PCCARD_IVAR_CIS4_STR:
1032		*(char **) result = sc->card.cis1_info[3];
1033		break;
1034	}
1035	return (0);
1036}
1037
1038static void
1039pccard_driver_added(device_t dev, driver_t *driver)
1040{
1041	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1042	struct pccard_function *pf;
1043	device_t child;
1044
1045	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1046		if (STAILQ_EMPTY(&pf->cfe_head))
1047			continue;
1048		child = pf->dev;
1049		if (device_get_state(child) != DS_NOTPRESENT)
1050			continue;
1051		if (pccard_function_enable(pf) == 0 &&
1052		    device_probe_and_attach(child) == 0) {
1053			DEVPRINTF((sc->dev, "function %d CCR at %d "
1054			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1055			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1056			    pccard_ccr_read(pf, 0x00),
1057			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1058			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1059			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1060			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1061		} else {
1062			if (pf->cfe != NULL)
1063				pccard_function_disable(pf);
1064		}
1065	}
1066	return;
1067}
1068
1069static struct resource *
1070pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1071    u_long start, u_long end, u_long count, u_int flags)
1072{
1073	struct pccard_ivar *dinfo;
1074	struct resource_list_entry *rle = 0;
1075	int passthrough = (device_get_parent(child) != dev);
1076	int isdefault = (start == 0 && end == ~0UL && count == 1);
1077	struct resource *r = NULL;
1078
1079	/* XXX I'm no longer sure this is right */
1080	if (passthrough) {
1081		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1082		    type, rid, start, end, count, flags));
1083	}
1084
1085	dinfo = device_get_ivars(child);
1086	rle = resource_list_find(&dinfo->resources, type, *rid);
1087
1088	if (rle == NULL && isdefault)
1089		return (NULL);	/* no resource of that type/rid */
1090	if (rle == NULL || rle->res == NULL) {
1091		/* Do we want this device to own it? */
1092		/* XXX I think so, but that might be lame XXX */
1093		r = bus_alloc_resource(dev, type, rid, start, end,
1094		  count, flags /* XXX aligment? */);
1095		if (r == NULL)
1096		    goto bad;
1097		resource_list_add(&dinfo->resources, type, *rid,
1098		  rman_get_start(r), rman_get_end(r), count);
1099		rle = resource_list_find(&dinfo->resources, type, *rid);
1100		if (!rle)
1101		    goto bad;
1102		rle->res = r;
1103	}
1104	/*
1105	 * XXX the following looks wrong, in theory, but likely it is
1106	 * XXX needed because of how the CIS code allocates resources
1107	 * XXX for this device.
1108	 */
1109	if (rman_get_device(rle->res) != dev)
1110		return (NULL);
1111	bus_release_resource(dev, type, *rid, rle->res);
1112	rle->res = NULL;
1113	switch(type) {
1114	case SYS_RES_IOPORT:
1115	case SYS_RES_MEMORY:
1116		if (!(flags & RF_ALIGNMENT_MASK))
1117			flags |= rman_make_alignment_flags(rle->count);
1118		break;
1119	case SYS_RES_IRQ:
1120		flags |= RF_SHAREABLE;
1121		break;
1122	}
1123	rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1124	    type, rid, rle->start, rle->end, rle->count, flags);
1125	return (rle->res);
1126bad:;
1127	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1128	return (NULL);
1129}
1130
1131static int
1132pccard_release_resource(device_t dev, device_t child, int type, int rid,
1133    struct resource *r)
1134{
1135	struct pccard_ivar *dinfo;
1136	int passthrough = (device_get_parent(child) != dev);
1137	struct resource_list_entry *rle = 0;
1138	int ret;
1139	int flags;
1140
1141	if (passthrough)
1142		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1143		    type, rid, r);
1144
1145	dinfo = device_get_ivars(child);
1146
1147	rle = resource_list_find(&dinfo->resources, type, rid);
1148
1149	if (!rle) {
1150		device_printf(dev, "Allocated resource not found, "
1151		    "%d %x %lx %lx\n",
1152		    type, rid, rman_get_start(r), rman_get_size(r));
1153		return ENOENT;
1154	}
1155	if (!rle->res) {
1156		device_printf(dev, "Allocated resource not recorded\n");
1157		return ENOENT;
1158	}
1159
1160	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1161	    type, rid, r);
1162	switch(type) {
1163	case SYS_RES_IOPORT:
1164	case SYS_RES_MEMORY:
1165		flags = rman_make_alignment_flags(rle->count);
1166		break;
1167	case SYS_RES_IRQ:
1168		flags = RF_SHAREABLE;
1169		break;
1170	default:
1171		flags = 0;
1172	}
1173	rle->res = bus_alloc_resource(dev, type, &rid,
1174	    rle->start, rle->end, rle->count, flags);
1175	if (rle->res == NULL)
1176		device_printf(dev, "release_resource: "
1177		    "unable to reaquire resource\n");
1178	return ret;
1179}
1180
1181static void
1182pccard_child_detached(device_t parent, device_t dev)
1183{
1184	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1185	struct pccard_function *pf = ivar->fcn;
1186
1187	pccard_function_disable(pf);
1188}
1189
1190static void
1191pccard_intr(void *arg)
1192{
1193	struct pccard_function *pf = (struct pccard_function*) arg;
1194	int reg;
1195	int doisr = 1;
1196
1197	/*
1198	 * MFC cards know if they interrupted, so we have to ack the
1199	 * interrupt and call the ISR.  Non-MFC cards don't have these
1200	 * bits, so they always get called.  Many non-MFC cards have
1201	 * this bit set always upon read, but some do not.
1202	 *
1203	 * We always ack the interrupt, even if there's no ISR
1204	 * for the card.  This is done on the theory that acking
1205	 * the interrupt will pacify the card enough to keep an
1206	 * interrupt storm from happening.  Of course this won't
1207	 * help in the non-MFC case.
1208	 *
1209	 * This has no impact for MPSAFEness of the client drivers.
1210	 * We register this with whatever flags the intr_handler
1211	 * was registered with.  All these functions are MPSAFE.
1212	 */
1213	if (pccard_mfc(pf->sc)) {
1214		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1215		if (reg & PCCARD_CCR_STATUS_INTR)
1216			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1217			    reg & ~PCCARD_CCR_STATUS_INTR);
1218		else
1219			doisr = 0;
1220	}
1221	if (pf->intr_handler != NULL && doisr)
1222		pf->intr_handler(pf->intr_handler_arg);
1223}
1224
1225static int
1226pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1227    int flags, driver_intr_t *intr, void *arg, void **cookiep)
1228{
1229	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1230	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1231	struct pccard_function *func = ivar->fcn;
1232	int err;
1233
1234	if (func->intr_handler != NULL)
1235		panic("Only one interrupt handler per function allowed");
1236	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1237	    func, cookiep);
1238	if (err != 0)
1239		return (err);
1240	func->intr_handler = intr;
1241	func->intr_handler_arg = arg;
1242	func->intr_handler_cookie = *cookiep;
1243	if (pccard_mfc(sc)) {
1244		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1245		    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1246		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1247	}
1248	return (0);
1249}
1250
1251static int
1252pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1253    void *cookie)
1254{
1255	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1256	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1257	struct pccard_function *func = ivar->fcn;
1258	int ret;
1259
1260	if (pccard_mfc(sc)) {
1261		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1262		    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1263		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1264	}
1265	ret = bus_generic_teardown_intr(dev, child, r, cookie);
1266	if (ret == 0) {
1267		func->intr_handler = NULL;
1268		func->intr_handler_arg = NULL;
1269		func->intr_handler_cookie = NULL;
1270	}
1271
1272	return (ret);
1273}
1274
1275static int
1276pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1277    struct resource *r)
1278{
1279	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1280	struct pccard_function *pf = ivar->fcn;
1281
1282	switch(type) {
1283	case SYS_RES_IOPORT:
1284		/*
1285		 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1286		 * card.
1287		 */
1288		if (pccard_mfc(pf->sc))
1289			pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1290			    rman_get_size(r));
1291		break;
1292	default:
1293		break;
1294	}
1295	return (bus_generic_activate_resource(brdev, child, type, rid, r));
1296}
1297
1298static int
1299pccard_deactivate_resource(device_t brdev, device_t child, int type,
1300    int rid, struct resource *r)
1301{
1302	/* XXX undo pccard_activate_resource? XXX */
1303	return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1304}
1305
1306static device_method_t pccard_methods[] = {
1307	/* Device interface */
1308	DEVMETHOD(device_probe,		pccard_probe),
1309	DEVMETHOD(device_attach,	pccard_attach),
1310	DEVMETHOD(device_detach,	pccard_detach),
1311	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1312	DEVMETHOD(device_suspend,	pccard_suspend),
1313	DEVMETHOD(device_resume,	pccard_resume),
1314
1315	/* Bus interface */
1316	DEVMETHOD(bus_print_child,	pccard_print_child),
1317	DEVMETHOD(bus_driver_added,	pccard_driver_added),
1318	DEVMETHOD(bus_child_detached,	pccard_child_detached),
1319	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
1320	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1321	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1322	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1323	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
1324	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
1325	DEVMETHOD(bus_set_resource,	pccard_set_resource),
1326	DEVMETHOD(bus_get_resource,	pccard_get_resource),
1327	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1328	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
1329	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1330	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1331	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1332
1333	/* Card Interface */
1334	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
1335	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1336	DEVMETHOD(card_attach_card,	pccard_attach_card),
1337	DEVMETHOD(card_detach_card,	pccard_detach_card),
1338	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1339	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1340	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1341
1342	{ 0, 0 }
1343};
1344
1345static driver_t pccard_driver = {
1346	"pccard",
1347	pccard_methods,
1348	sizeof(struct pccard_softc)
1349};
1350
1351devclass_t	pccard_devclass;
1352
1353/* Maybe we need to have a slot device? */
1354DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1355DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1356MODULE_VERSION(pccard, 1);
1357