pccard.c revision 121905
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 121905 2003-11-02 20:18:19Z 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_addr_t iosize;
537	bus_size_t tmp;
538
539	if (addr != 0) {
540		if (pf->pf_mfc_iomax == 0) {
541			pf->pf_mfc_iobase = addr + offset;
542			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
543		} else {
544			/* this makes the assumption that nothing overlaps */
545			if (pf->pf_mfc_iobase > addr + offset)
546				pf->pf_mfc_iobase = addr + offset;
547			if (pf->pf_mfc_iomax < addr + offset + size)
548				pf->pf_mfc_iomax = addr + offset + size;
549		}
550	}
551
552	tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
553	/* round up to nearest (2^n)-1 */
554	for (iosize = 1; iosize < tmp; iosize <<= 1)
555		;
556	iosize--;
557
558	DEVPRINTF((pf->dev, "MFC: I/O base 0x%x IOSIZE %lld\n",
559		      pf->pf_mfc_iobase, (uint64_t) iosize));
560	pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
561	    pf->pf_mfc_iobase & 0xff);
562	pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
563	    (pf->pf_mfc_iobase >> 8) & 0xff);
564	pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
565	pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
566	pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
567}
568
569/* Enable a PCCARD function */
570static int
571pccard_function_enable(struct pccard_function *pf)
572{
573	struct pccard_function *tmp;
574	int reg;
575	device_t dev = pf->sc->dev;
576
577	if (pf->cfe == NULL) {
578		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
579		return (ENOMEM);
580	}
581
582	/*
583	 * Increase the reference count on the socket, enabling power, if
584	 * necessary.
585	 */
586	pf->sc->sc_enabled_count++;
587
588	if (pf->pf_flags & PFF_ENABLED) {
589		/*
590		 * Don't do anything if we're already enabled.
591		 */
592		return (0);
593	}
594
595	/*
596	 * it's possible for different functions' CCRs to be in the same
597	 * underlying page.  Check for that.
598	 */
599	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
600		if ((tmp->pf_flags & PFF_ENABLED) &&
601		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
602		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
603		    (tmp->ccr_base - tmp->pf_ccr_offset +
604		    tmp->pf_ccr_realsize))) {
605			pf->pf_ccrt = tmp->pf_ccrt;
606			pf->pf_ccrh = tmp->pf_ccrh;
607			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
608
609			/*
610			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
611			 * tmp->ccr_base) + pf->ccr_base;
612			 */
613			/* pf->pf_ccr_offset =
614			    (tmp->pf_ccr_offset + pf->ccr_base) -
615			    tmp->ccr_base; */
616			pf->pf_ccr_window = tmp->pf_ccr_window;
617			break;
618		}
619	}
620	if (tmp == NULL) {
621		pf->ccr_rid = 0;
622		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
623		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
624		if (!pf->ccr_res)
625			goto bad;
626		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
627		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
628		    pf->ccr_base));
629		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
630		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
631		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
632		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
633		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
634		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
635		pf->pf_ccr_realsize = 1;
636	}
637
638	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
639	reg |= PCCARD_CCR_OPTION_LEVIREQ;
640	if (pccard_mfc(pf->sc)) {
641		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
642			PCCARD_CCR_OPTION_ADDR_DECODE);
643		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
644	}
645	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
646
647	reg = 0;
648	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
649		reg |= PCCARD_CCR_STATUS_IOIS8;
650	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
651		reg |= PCCARD_CCR_STATUS_AUDIO;
652	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
653
654	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
655
656	if (pccard_mfc(pf->sc))
657		pccard_mfc_adjust_iobase(pf, 0, 0, 0);
658
659#ifdef PCCARDDEBUG
660	if (pccard_debug) {
661		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
662			device_printf(tmp->sc->dev,
663			    "function %d CCR at %d offset %x: "
664			    "%x %x %x %x, %x %x %x %x, %x\n",
665			    tmp->number, tmp->pf_ccr_window,
666			    tmp->pf_ccr_offset,
667			    pccard_ccr_read(tmp, 0x00),
668			    pccard_ccr_read(tmp, 0x02),
669			    pccard_ccr_read(tmp, 0x04),
670			    pccard_ccr_read(tmp, 0x06),
671			    pccard_ccr_read(tmp, 0x0A),
672			    pccard_ccr_read(tmp, 0x0C),
673			    pccard_ccr_read(tmp, 0x0E),
674			    pccard_ccr_read(tmp, 0x10),
675			    pccard_ccr_read(tmp, 0x12));
676		}
677	}
678#endif
679	pf->pf_flags |= PFF_ENABLED;
680	return (0);
681
682 bad:
683	/*
684	 * Decrement the reference count, and power down the socket, if
685	 * necessary.
686	 */
687	pf->sc->sc_enabled_count--;
688	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
689
690	return (1);
691}
692
693/* Disable PCCARD function. */
694static void
695pccard_function_disable(struct pccard_function *pf)
696{
697	struct pccard_function *tmp;
698	device_t dev = pf->sc->dev;
699
700	if (pf->cfe == NULL)
701		panic("pccard_function_disable: function not initialized");
702
703	if ((pf->pf_flags & PFF_ENABLED) == 0) {
704		/*
705		 * Don't do anything if we're already disabled.
706		 */
707		return;
708	}
709
710	if (pf->intr_handler != NULL) {
711		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
712		struct resource_list_entry *rle =
713		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
714		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
715		    pf->intr_handler_cookie);
716	}
717
718	/*
719	 * it's possible for different functions' CCRs to be in the same
720	 * underlying page.  Check for that.  Note we mark us as disabled
721	 * first to avoid matching ourself.
722	 */
723
724	pf->pf_flags &= ~PFF_ENABLED;
725	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
726		if ((tmp->pf_flags & PFF_ENABLED) &&
727		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
728		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
729		    (tmp->ccr_base - tmp->pf_ccr_offset +
730		    tmp->pf_ccr_realsize)))
731			break;
732	}
733
734	/* Not used by anyone else; unmap the CCR. */
735	if (tmp == NULL) {
736		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
737		    pf->ccr_res);
738		pf->ccr_res = NULL;
739	}
740
741	/*
742	 * Decrement the reference count, and power down the socket, if
743	 * necessary.
744	 */
745	pf->sc->sc_enabled_count--;
746}
747
748/*
749 * simulate the old "probe" routine.  In the new world order, the driver
750 * needs to grab devices while in the old they were assigned to the device by
751 * the pccardd process.  These symbols are exported to the upper layers.
752 */
753static int
754pccard_compat_do_probe(device_t bus, device_t dev)
755{
756	return (CARD_COMPAT_MATCH(dev));
757}
758
759static int
760pccard_compat_do_attach(device_t bus, device_t dev)
761{
762	int err;
763
764	err = CARD_COMPAT_PROBE(dev);
765	if (err <= 0)
766		err = CARD_COMPAT_ATTACH(dev);
767	return (err);
768}
769
770#define PCCARD_NPORT	2
771#define PCCARD_NMEM	5
772#define PCCARD_NIRQ	1
773#define PCCARD_NDRQ	0
774
775static int
776pccard_add_children(device_t dev, int busno)
777{
778	/* Call parent to scan for any current children */
779	return (0);
780}
781
782static int
783pccard_probe(device_t dev)
784{
785	device_set_desc(dev, "16-bit PCCard bus");
786	return (pccard_add_children(dev, device_get_unit(dev)));
787}
788
789static int
790pccard_attach(device_t dev)
791{
792	struct pccard_softc *sc = PCCARD_SOFTC(dev);
793
794	sc->dev = dev;
795	sc->sc_enabled_count = 0;
796	return (bus_generic_attach(dev));
797}
798
799static int
800pccard_detach(device_t dev)
801{
802	pccard_detach_card(dev);
803	return 0;
804}
805
806static int
807pccard_suspend(device_t self)
808{
809	pccard_detach_card(self);
810	return (0);
811}
812
813static
814int
815pccard_resume(device_t self)
816{
817	return (0);
818}
819
820static void
821pccard_print_resources(struct resource_list *rl, const char *name, int type,
822    int count, const char *format)
823{
824	struct resource_list_entry *rle;
825	int printed;
826	int i;
827
828	printed = 0;
829	for (i = 0; i < count; i++) {
830		rle = resource_list_find(rl, type, i);
831		if (rle != NULL) {
832			if (printed == 0)
833				printf(" %s ", name);
834			else if (printed > 0)
835				printf(",");
836			printed++;
837			printf(format, rle->start);
838			if (rle->count > 1) {
839				printf("-");
840				printf(format, rle->start + rle->count - 1);
841			}
842		} else if (i > 3) {
843			/* check the first few regardless */
844			break;
845		}
846	}
847}
848
849static int
850pccard_print_child(device_t dev, device_t child)
851{
852	struct pccard_ivar *devi = PCCARD_IVAR(child);
853	struct resource_list *rl = &devi->resources;
854	int retval = 0;
855
856	retval += bus_print_child_header(dev, child);
857	retval += printf(" at");
858
859	if (devi != NULL) {
860		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
861		    PCCARD_NPORT, "%#lx");
862		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
863		    PCCARD_NMEM, "%#lx");
864		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
865		    "%ld");
866		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
867		    "%ld");
868		retval += printf(" function %d config %d", devi->fcn->number,
869		    devi->fcn->cfe->number);
870	}
871
872	retval += bus_print_child_footer(dev, child);
873
874	return (retval);
875}
876
877static int
878pccard_set_resource(device_t dev, device_t child, int type, int rid,
879		 u_long start, u_long count)
880{
881	struct pccard_ivar *devi = PCCARD_IVAR(child);
882	struct resource_list *rl = &devi->resources;
883
884	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
885	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
886		return (EINVAL);
887	if (rid < 0)
888		return (EINVAL);
889	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
890		return (EINVAL);
891	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
892		return (EINVAL);
893	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
894		return (EINVAL);
895	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
896		return (EINVAL);
897
898	resource_list_add(rl, type, rid, start, start + count - 1, count);
899	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
900	    type, &rid, start, start + count - 1, count, 0))
901		return 0;
902	else
903		return ENOMEM;
904}
905
906static int
907pccard_get_resource(device_t dev, device_t child, int type, int rid,
908    u_long *startp, u_long *countp)
909{
910	struct pccard_ivar *devi = PCCARD_IVAR(child);
911	struct resource_list *rl = &devi->resources;
912	struct resource_list_entry *rle;
913
914	rle = resource_list_find(rl, type, rid);
915	if (rle == NULL)
916		return (ENOENT);
917
918	if (startp != NULL)
919		*startp = rle->start;
920	if (countp != NULL)
921		*countp = rle->count;
922
923	return (0);
924}
925
926static void
927pccard_delete_resource(device_t dev, device_t child, int type, int rid)
928{
929	struct pccard_ivar *devi = PCCARD_IVAR(child);
930	struct resource_list *rl = &devi->resources;
931	resource_list_delete(rl, type, rid);
932}
933
934static int
935pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
936    u_int32_t flags)
937{
938	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
939	    rid, flags));
940}
941
942static int
943pccard_set_memory_offset(device_t dev, device_t child, int rid,
944    u_int32_t offset, u_int32_t *deltap)
945
946{
947	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
948	    offset, deltap));
949}
950
951static void
952pccard_probe_nomatch(device_t bus, device_t child)
953{
954	struct pccard_ivar *devi = PCCARD_IVAR(child);
955	struct pccard_function *func = devi->fcn;
956	struct pccard_softc *sc = PCCARD_SOFTC(bus);
957
958	device_printf(bus, "<unknown card>");
959	printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
960	  sc->card.manufacturer, sc->card.product, func->number);
961	device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
962	  sc->card.cis1_info[1], sc->card.cis1_info[2]);
963	return;
964}
965
966static int
967pccard_child_location_str(device_t bus, device_t child, char *buf,
968    size_t buflen)
969{
970	struct pccard_ivar *devi = PCCARD_IVAR(child);
971	struct pccard_function *func = devi->fcn;
972
973	snprintf(buf, buflen, "function=%d", func->number);
974	return (0);
975}
976
977static int
978pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
979    size_t buflen)
980{
981	struct pccard_ivar *devi = PCCARD_IVAR(child);
982	struct pccard_function *func = devi->fcn;
983	struct pccard_softc *sc = PCCARD_SOFTC(bus);
984
985	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
986	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
987	    sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
988	    sc->card.cis1_info[1], func->function);
989	return (0);
990}
991
992static int
993pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
994{
995	struct pccard_ivar *devi = PCCARD_IVAR(child);
996	struct pccard_function *func = devi->fcn;
997	struct pccard_softc *sc = PCCARD_SOFTC(bus);
998
999	switch (which) {
1000	default:
1001	case PCCARD_IVAR_ETHADDR:
1002		bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1003		break;
1004	case PCCARD_IVAR_VENDOR:
1005		*(u_int32_t *) result = sc->card.manufacturer;
1006		break;
1007	case PCCARD_IVAR_PRODUCT:
1008		*(u_int32_t *) result = sc->card.product;
1009		break;
1010	case PCCARD_IVAR_PRODEXT:
1011		*(u_int16_t *) result = sc->card.prodext;
1012		break;
1013	case PCCARD_IVAR_FUNCTION:
1014		*(u_int32_t *) result = func->function;
1015		break;
1016	case PCCARD_IVAR_FUNCTION_NUMBER:
1017		if (!func) {
1018			device_printf(bus, "No function number, bug!\n");
1019			return (ENOENT);
1020		}
1021		*(u_int32_t *) result = func->number;
1022		break;
1023	case PCCARD_IVAR_VENDOR_STR:
1024		*(char **) result = sc->card.cis1_info[0];
1025		break;
1026	case PCCARD_IVAR_PRODUCT_STR:
1027		*(char **) result = sc->card.cis1_info[1];
1028		break;
1029	case PCCARD_IVAR_CIS3_STR:
1030		*(char **) result = sc->card.cis1_info[2];
1031		break;
1032	case PCCARD_IVAR_CIS4_STR:
1033		*(char **) result = sc->card.cis1_info[3];
1034		break;
1035	}
1036	return (0);
1037}
1038
1039static void
1040pccard_driver_added(device_t dev, driver_t *driver)
1041{
1042	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1043	struct pccard_function *pf;
1044	device_t child;
1045
1046	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1047		if (STAILQ_EMPTY(&pf->cfe_head))
1048			continue;
1049		child = pf->dev;
1050		if (device_get_state(child) != DS_NOTPRESENT)
1051			continue;
1052		if (pccard_function_enable(pf) == 0 &&
1053		    device_probe_and_attach(child) == 0) {
1054			DEVPRINTF((sc->dev, "function %d CCR at %d "
1055			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1056			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1057			    pccard_ccr_read(pf, 0x00),
1058			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1059			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1060			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1061			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1062		} else {
1063			if (pf->cfe != NULL)
1064				pccard_function_disable(pf);
1065		}
1066	}
1067	return;
1068}
1069
1070static struct resource *
1071pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1072    u_long start, u_long end, u_long count, u_int flags)
1073{
1074	struct pccard_ivar *dinfo;
1075	struct resource_list_entry *rle = 0;
1076	int passthrough = (device_get_parent(child) != dev);
1077	int isdefault = (start == 0 && end == ~0UL && count == 1);
1078	struct resource *r = NULL;
1079
1080	/* XXX I'm no longer sure this is right */
1081	if (passthrough) {
1082		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1083		    type, rid, start, end, count, flags));
1084	}
1085
1086	dinfo = device_get_ivars(child);
1087	rle = resource_list_find(&dinfo->resources, type, *rid);
1088
1089	if (rle == NULL && isdefault)
1090		return (NULL);	/* no resource of that type/rid */
1091	if (rle == NULL || rle->res == NULL) {
1092		/* Do we want this device to own it? */
1093		/* XXX I think so, but that might be lame XXX */
1094		r = bus_alloc_resource(dev, type, rid, start, end,
1095		  count, flags /* XXX aligment? */);
1096		if (r == NULL)
1097		    goto bad;
1098		resource_list_add(&dinfo->resources, type, *rid,
1099		  rman_get_start(r), rman_get_end(r), count);
1100		rle = resource_list_find(&dinfo->resources, type, *rid);
1101		if (!rle)
1102		    goto bad;
1103		rle->res = r;
1104	}
1105	/*
1106	 * XXX the following looks wrong, in theory, but likely it is
1107	 * XXX needed because of how the CIS code allocates resources
1108	 * XXX for this device.
1109	 */
1110	if (rman_get_device(rle->res) != dev)
1111		return (NULL);
1112	bus_release_resource(dev, type, *rid, rle->res);
1113	rle->res = NULL;
1114	switch(type) {
1115	case SYS_RES_IOPORT:
1116	case SYS_RES_MEMORY:
1117		if (!(flags & RF_ALIGNMENT_MASK))
1118			flags |= rman_make_alignment_flags(rle->count);
1119		break;
1120	case SYS_RES_IRQ:
1121		flags |= RF_SHAREABLE;
1122		break;
1123	}
1124	rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1125	    type, rid, rle->start, rle->end, rle->count, flags);
1126	return (rle->res);
1127bad:;
1128	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1129	return (NULL);
1130}
1131
1132static int
1133pccard_release_resource(device_t dev, device_t child, int type, int rid,
1134    struct resource *r)
1135{
1136	struct pccard_ivar *dinfo;
1137	int passthrough = (device_get_parent(child) != dev);
1138	struct resource_list_entry *rle = 0;
1139	int ret;
1140	int flags;
1141
1142	if (passthrough)
1143		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1144		    type, rid, r);
1145
1146	dinfo = device_get_ivars(child);
1147
1148	rle = resource_list_find(&dinfo->resources, type, rid);
1149
1150	if (!rle) {
1151		device_printf(dev, "Allocated resource not found, "
1152		    "%d %x %lx %lx\n",
1153		    type, rid, rman_get_start(r), rman_get_size(r));
1154		return ENOENT;
1155	}
1156	if (!rle->res) {
1157		device_printf(dev, "Allocated resource not recorded\n");
1158		return ENOENT;
1159	}
1160
1161	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1162	    type, rid, r);
1163	switch(type) {
1164	case SYS_RES_IOPORT:
1165	case SYS_RES_MEMORY:
1166		flags = rman_make_alignment_flags(rle->count);
1167		break;
1168	case SYS_RES_IRQ:
1169		flags = RF_SHAREABLE;
1170		break;
1171	default:
1172		flags = 0;
1173	}
1174	rle->res = bus_alloc_resource(dev, type, &rid,
1175	    rle->start, rle->end, rle->count, flags);
1176	if (rle->res == NULL)
1177		device_printf(dev, "release_resource: "
1178		    "unable to reaquire resource\n");
1179	return ret;
1180}
1181
1182static void
1183pccard_child_detached(device_t parent, device_t dev)
1184{
1185	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1186	struct pccard_function *pf = ivar->fcn;
1187
1188	pccard_function_disable(pf);
1189}
1190
1191static void
1192pccard_intr(void *arg)
1193{
1194	struct pccard_function *pf = (struct pccard_function*) arg;
1195	int reg;
1196	int doisr = 1;
1197
1198	/*
1199	 * MFC cards know if they interrupted, so we have to ack the
1200	 * interrupt and call the ISR.  Non-MFC cards don't have these
1201	 * bits, so they always get called.  Many non-MFC cards have
1202	 * this bit set always upon read, but some do not.
1203	 *
1204	 * We always ack the interrupt, even if there's no ISR
1205	 * for the card.  This is done on the theory that acking
1206	 * the interrupt will pacify the card enough to keep an
1207	 * interrupt storm from happening.  Of course this won't
1208	 * help in the non-MFC case.
1209	 *
1210	 * This has no impact for MPSAFEness of the client drivers.
1211	 * We register this with whatever flags the intr_handler
1212	 * was registered with.  All these functions are MPSAFE.
1213	 */
1214	if (pccard_mfc(pf->sc)) {
1215		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1216		if (reg & PCCARD_CCR_STATUS_INTR)
1217			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1218			    reg & ~PCCARD_CCR_STATUS_INTR);
1219		else
1220			doisr = 0;
1221	}
1222	if (pf->intr_handler != NULL && doisr)
1223		pf->intr_handler(pf->intr_handler_arg);
1224}
1225
1226static int
1227pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1228    int flags, driver_intr_t *intr, void *arg, void **cookiep)
1229{
1230	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1231	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1232	struct pccard_function *func = ivar->fcn;
1233	int err;
1234
1235	if (func->intr_handler != NULL)
1236		panic("Only one interrupt handler per function allowed");
1237	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1238	    func, cookiep);
1239	if (err != 0)
1240		return (err);
1241	func->intr_handler = intr;
1242	func->intr_handler_arg = arg;
1243	func->intr_handler_cookie = *cookiep;
1244	if (pccard_mfc(sc)) {
1245		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1246		    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1247		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1248	}
1249	return (0);
1250}
1251
1252static int
1253pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1254    void *cookie)
1255{
1256	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1257	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1258	struct pccard_function *func = ivar->fcn;
1259	int ret;
1260
1261	if (pccard_mfc(sc)) {
1262		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1263		    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1264		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1265	}
1266	ret = bus_generic_teardown_intr(dev, child, r, cookie);
1267	if (ret == 0) {
1268		func->intr_handler = NULL;
1269		func->intr_handler_arg = NULL;
1270		func->intr_handler_cookie = NULL;
1271	}
1272
1273	return (ret);
1274}
1275
1276static int
1277pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1278    struct resource *r)
1279{
1280	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1281	struct pccard_function *pf = ivar->fcn;
1282
1283	switch(type) {
1284	case SYS_RES_IOPORT:
1285		/*
1286		 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1287		 * card.
1288		 */
1289		if (pccard_mfc(pf->sc))
1290			pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1291			    rman_get_size(r));
1292		break;
1293	default:
1294		break;
1295	}
1296	return (bus_generic_activate_resource(brdev, child, type, rid, r));
1297}
1298
1299static int
1300pccard_deactivate_resource(device_t brdev, device_t child, int type,
1301    int rid, struct resource *r)
1302{
1303	/* XXX undo pccard_activate_resource? XXX */
1304	return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1305}
1306
1307static device_method_t pccard_methods[] = {
1308	/* Device interface */
1309	DEVMETHOD(device_probe,		pccard_probe),
1310	DEVMETHOD(device_attach,	pccard_attach),
1311	DEVMETHOD(device_detach,	pccard_detach),
1312	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1313	DEVMETHOD(device_suspend,	pccard_suspend),
1314	DEVMETHOD(device_resume,	pccard_resume),
1315
1316	/* Bus interface */
1317	DEVMETHOD(bus_print_child,	pccard_print_child),
1318	DEVMETHOD(bus_driver_added,	pccard_driver_added),
1319	DEVMETHOD(bus_child_detached,	pccard_child_detached),
1320	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
1321	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1322	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1323	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1324	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
1325	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
1326	DEVMETHOD(bus_set_resource,	pccard_set_resource),
1327	DEVMETHOD(bus_get_resource,	pccard_get_resource),
1328	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1329	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
1330	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1331	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1332	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1333
1334	/* Card Interface */
1335	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
1336	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1337	DEVMETHOD(card_attach_card,	pccard_attach_card),
1338	DEVMETHOD(card_detach_card,	pccard_detach_card),
1339	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1340	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1341	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1342
1343	{ 0, 0 }
1344};
1345
1346static driver_t pccard_driver = {
1347	"pccard",
1348	pccard_methods,
1349	sizeof(struct pccard_softc)
1350};
1351
1352devclass_t	pccard_devclass;
1353
1354/* Maybe we need to have a slot device? */
1355DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1356DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1357MODULE_VERSION(pccard, 1);
1358