pccard.c revision 119462
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 119462 2003-08-25 18:20:03Z 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 int	pccard_card_gettype(device_t dev, int *type);
90static void	pccard_function_init(struct pccard_function *pf);
91static void	pccard_function_free(struct pccard_function *pf);
92static int	pccard_function_enable(struct pccard_function *pf);
93static void	pccard_function_disable(struct pccard_function *pf);
94static int	pccard_compat_do_probe(device_t bus, device_t dev);
95static int	pccard_compat_do_attach(device_t bus, device_t dev);
96static int	pccard_add_children(device_t dev, int busno);
97static int	pccard_probe(device_t dev);
98static int	pccard_attach(device_t dev);
99static int	pccard_detach(device_t dev);
100static void	pccard_print_resources(struct resource_list *rl,
101		    const char *name, int type, int count, const char *format);
102static int	pccard_print_child(device_t dev, device_t child);
103static int	pccard_set_resource(device_t dev, device_t child, int type,
104		    int rid, u_long start, u_long count);
105static int	pccard_get_resource(device_t dev, device_t child, int type,
106		    int rid, u_long *startp, u_long *countp);
107static void	pccard_delete_resource(device_t dev, device_t child, int type,
108		    int rid);
109static int	pccard_set_res_flags(device_t dev, device_t child, int type,
110		    int rid, u_int32_t flags);
111static int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
112		    u_int32_t offset, u_int32_t *deltap);
113static void	pccard_probe_nomatch(device_t cbdev, device_t child);
114static int	pccard_read_ivar(device_t bus, device_t child, int which,
115		    u_char *result);
116static void	pccard_driver_added(device_t dev, driver_t *driver);
117static struct resource *pccard_alloc_resource(device_t dev,
118		    device_t child, int type, int *rid, u_long start,
119		    u_long end, u_long count, u_int flags);
120static int	pccard_release_resource(device_t dev, device_t child, int type,
121		    int rid, struct resource *r);
122static void	pccard_child_detached(device_t parent, device_t dev);
123static void	pccard_intr(void *arg);
124static int	pccard_setup_intr(device_t dev, device_t child,
125		    struct resource *irq, int flags, driver_intr_t *intr,
126		    void *arg, void **cookiep);
127static int	pccard_teardown_intr(device_t dev, device_t child,
128		    struct resource *r, void *cookie);
129
130static const struct pccard_product *
131pccard_do_product_lookup(device_t bus, device_t dev,
132			 const struct pccard_product *tab, size_t ent_size,
133			 pccard_product_match_fn matchfn);
134
135
136static int
137pccard_ccr_read(struct pccard_function *pf, int ccr)
138{
139	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
140	    pf->pf_ccr_offset + ccr));
141}
142
143static void
144pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
145{
146	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
147		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
148		    pf->pf_ccr_offset + ccr, val);
149	}
150}
151
152static int
153pccard_set_default_descr(device_t dev)
154{
155	char *vendorstr, *prodstr, *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: %x %x %x %x, %x %x %x %x, %x\n",
267			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
268			    pccard_ccr_read(pf, 0x00),
269			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
270			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
271			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
272			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
273		} else {
274			if (pf->cfe != NULL)
275				pccard_function_disable(pf);
276		}
277	}
278	return (0);
279}
280
281static int
282pccard_detach_card(device_t dev)
283{
284	struct pccard_softc *sc = PCCARD_SOFTC(dev);
285	struct pccard_function *pf;
286	struct pccard_config_entry *cfe;
287
288	/*
289	 * We are running on either the PCCARD socket's event thread
290	 * or in user context detaching a device by user request.
291	 */
292	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
293		int state = device_get_state(pf->dev);
294
295		if (state == DS_ATTACHED || state == DS_BUSY)
296			device_detach(pf->dev);
297		if (pf->cfe != NULL)
298			pccard_function_disable(pf);
299		pccard_function_free(pf);
300		device_delete_child(dev, pf->dev);
301	}
302	if (sc->sc_enabled_count == 0)
303		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
304
305	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
306		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
307			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
308			free(cfe, M_DEVBUF);
309		}
310		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
311		free(pf, M_DEVBUF);
312	}
313	return (0);
314}
315
316static const struct pccard_product *
317pccard_do_product_lookup(device_t bus, device_t dev,
318    const struct pccard_product *tab, size_t ent_size,
319    pccard_product_match_fn matchfn)
320{
321	const struct pccard_product *ent;
322	int matches;
323	u_int32_t fcn;
324	u_int32_t vendor;
325	u_int32_t prod;
326	char *vendorstr;
327	char *prodstr;
328
329#ifdef DIAGNOSTIC
330	if (sizeof *ent > ent_size)
331		panic("pccard_product_lookup: bogus ent_size %jd",
332		    (intmax_t) ent_size);
333#endif
334	if (pccard_get_vendor(dev, &vendor))
335		return (NULL);
336	if (pccard_get_product(dev, &prod))
337		return (NULL);
338	if (pccard_get_function_number(dev, &fcn))
339		return (NULL);
340	if (pccard_get_vendor_str(dev, &vendorstr))
341		return (NULL);
342	if (pccard_get_product_str(dev, &prodstr))
343		return (NULL);
344	for (ent = tab; ent->pp_vendor != 0; ent =
345	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
346		matches = 1;
347		if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
348		    ent->pp_product == PCCARD_PRODUCT_ANY &&
349		    ent->pp_cis[0] == NULL &&
350		    ent->pp_cis[1] == NULL) {
351			if (ent->pp_name)
352				device_printf(dev,
353				    "Total wildcard entry ignored for %s\n",
354				    ent->pp_name);
355			continue;
356		}
357		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
358		    vendor != ent->pp_vendor)
359			matches = 0;
360		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
361		    prod != ent->pp_product)
362			matches = 0;
363		if (matches && fcn != ent->pp_expfunc)
364			matches = 0;
365		if (matches && ent->pp_cis[0] &&
366		    strcmp(ent->pp_cis[0], vendorstr) != 0)
367			matches = 0;
368		if (matches && ent->pp_cis[1] &&
369		    strcmp(ent->pp_cis[1], prodstr) != 0)
370			matches = 0;
371		/* XXX need to match cis[2] and cis[3] also XXX */
372		if (matchfn != NULL)
373			matches = (*matchfn)(dev, ent, matches);
374		if (matches)
375			return (ent);
376	}
377	return (NULL);
378}
379
380static int
381pccard_card_gettype(device_t dev, int *type)
382{
383	struct pccard_softc *sc = PCCARD_SOFTC(dev);
384	struct pccard_function *pf;
385
386	/*
387	 * set the iftype to memory if this card has no functions (not yet
388	 * probed), or only one function, and that is not initialized yet or
389	 * that is memory.
390	 */
391	pf = STAILQ_FIRST(&sc->card.pf_head);
392	if (pf == NULL ||
393	    (STAILQ_NEXT(pf, pf_list) == NULL &&
394	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
395		*type = PCCARD_IFTYPE_MEMORY;
396	else
397		*type = PCCARD_IFTYPE_IO;
398	return (0);
399}
400
401/*
402 * Initialize a PCCARD function.  May be called as long as the function is
403 * disabled.
404 *
405 * Note: pccard_function_init should not keep resources allocated.  It should
406 * only set them up ala isa pnp, set the values in the rl lists, and return.
407 * Any resource held after pccard_function_init is called is a bug.  However,
408 * the bus routines to get the resources also assume that pccard_function_init
409 * does this, so they need to be fixed too.
410 */
411static void
412pccard_function_init(struct pccard_function *pf)
413{
414	struct pccard_config_entry *cfe;
415	int i;
416	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
417	struct resource_list *rl = &devi->resources;
418	struct resource_list_entry *rle;
419	struct resource *r = 0;
420	device_t bus;
421	int start;
422	int end;
423	int spaces;
424
425	if (pf->pf_flags & PFF_ENABLED) {
426		printf("pccard_function_init: function is enabled");
427		return;
428	}
429	bus = device_get_parent(pf->dev);
430	/* Remember which configuration entry we are using. */
431	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
432		for (i = 0; i < cfe->num_iospace; i++)
433			cfe->iores[i] = NULL;
434		cfe->irqres = NULL;
435		spaces = 0;
436		for (i = 0; i < cfe->num_iospace; i++) {
437			start = cfe->iospace[i].start;
438			if (start)
439				end = start + cfe->iospace[i].length - 1;
440			else
441				end = ~0;
442			cfe->iorid[i] = i;
443			DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
444			    i, start, end));
445			r = cfe->iores[i] = bus_alloc_resource(bus,
446			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
447			    cfe->iospace[i].length,
448			    rman_make_alignment_flags(cfe->iospace[i].length));
449			if (cfe->iores[i] == NULL)
450				goto not_this_one;
451			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
452			    rman_get_start(r), rman_get_end(r),
453			    cfe->iospace[i].length);
454			rle = resource_list_find(rl, SYS_RES_IOPORT,
455			    cfe->iorid[i]);
456			rle->res = r;
457			spaces++;
458		}
459		if (cfe->num_memspace > 0) {
460			/*
461			 * Not implement yet, Fix me.
462			 */
463			DEVPRINTF((bus, "Memory space not yet implemented.\n"));
464		}
465		if (spaces == 0) {
466			DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
467			goto not_this_one;
468		}
469		if (cfe->irqmask) {
470			cfe->irqrid = 0;
471			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
472			    &cfe->irqrid, 0, ~0, 1, 0);
473			if (cfe->irqres == NULL)
474				goto not_this_one;
475			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
476			    rman_get_start(r), rman_get_end(r), 1);
477			rle = resource_list_find(rl, SYS_RES_IRQ,
478			    cfe->irqrid);
479			rle->res = r;
480		}
481		/* If we get to here, we've allocated all we need */
482		pf->cfe = cfe;
483		break;
484	    not_this_one:;
485		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
486		    cfe->number));
487		/*
488		 * Release resources that we partially allocated
489		 * from this config entry.
490		 */
491		for (i = 0; i < cfe->num_iospace; i++) {
492			if (cfe->iores[i] != NULL) {
493				bus_release_resource(bus, SYS_RES_IOPORT,
494				    cfe->iorid[i], cfe->iores[i]);
495				rle = resource_list_find(rl, SYS_RES_IOPORT,
496				    cfe->iorid[i]);
497				rle->res = NULL;
498				resource_list_delete(rl, SYS_RES_IOPORT,
499				    cfe->iorid[i]);
500			}
501			cfe->iores[i] = NULL;
502		}
503		if (cfe->irqmask && cfe->irqres != NULL) {
504			bus_release_resource(bus, SYS_RES_IRQ,
505			    cfe->irqrid, cfe->irqres);
506			rle = resource_list_find(rl, SYS_RES_IRQ,
507			    cfe->irqrid);
508			rle->res = NULL;
509			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
510			cfe->irqres = NULL;
511		}
512	}
513}
514
515/*
516 * Free resources allocated by pccard_function_init(), May be called as long
517 * as the function is disabled.
518 *
519 * NOTE: This function should be unnecessary.  pccard_function_init should
520 * never keep resources initialized.
521 */
522static void
523pccard_function_free(struct pccard_function *pf)
524{
525	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
526	struct resource_list_entry *rle;
527
528	if (pf->pf_flags & PFF_ENABLED) {
529		printf("pccard_function_init: function is enabled");
530		return;
531	}
532
533	SLIST_FOREACH(rle, &devi->resources, link) {
534		if (rle->res) {
535			if (rman_get_device(rle->res) != pf->sc->dev)
536				device_printf(pf->sc->dev,
537				    "function_free: Resource still owned by "
538				    "child, oops. "
539				    "(type=%d, rid=%d, addr=%lx)\n",
540				    rle->type, rle->rid,
541				    rman_get_start(rle->res));
542			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
543			    pf->sc->dev, rle->type, rle->rid, rle->res);
544			rle->res = NULL;
545		}
546	}
547	resource_list_free(&devi->resources);
548}
549
550/* Enable a PCCARD function */
551static int
552pccard_function_enable(struct pccard_function *pf)
553{
554	struct pccard_function *tmp;
555	int reg;
556	device_t dev = pf->sc->dev;
557
558	if (pf->cfe == NULL) {
559		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
560		return (ENOMEM);
561	}
562
563	/*
564	 * Increase the reference count on the socket, enabling power, if
565	 * necessary.
566	 */
567	pf->sc->sc_enabled_count++;
568
569	if (pf->pf_flags & PFF_ENABLED) {
570		/*
571		 * Don't do anything if we're already enabled.
572		 */
573		return (0);
574	}
575
576	/*
577	 * it's possible for different functions' CCRs to be in the same
578	 * underlying page.  Check for that.
579	 */
580	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
581		if ((tmp->pf_flags & PFF_ENABLED) &&
582		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
583		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
584		    (tmp->ccr_base - tmp->pf_ccr_offset +
585		    tmp->pf_ccr_realsize))) {
586			pf->pf_ccrt = tmp->pf_ccrt;
587			pf->pf_ccrh = tmp->pf_ccrh;
588			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
589
590			/*
591			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
592			 * tmp->ccr_base) + pf->ccr_base;
593			 */
594			/* pf->pf_ccr_offset =
595			    (tmp->pf_ccr_offset + pf->ccr_base) -
596			    tmp->ccr_base; */
597			pf->pf_ccr_window = tmp->pf_ccr_window;
598			break;
599		}
600	}
601	if (tmp == NULL) {
602		pf->ccr_rid = 0;
603		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
604		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
605		if (!pf->ccr_res)
606			goto bad;
607		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
608		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
609		    pf->ccr_base));
610		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
611		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
612		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
613		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
614		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
615		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
616		pf->pf_ccr_realsize = 1;
617	}
618
619	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
620	reg |= PCCARD_CCR_OPTION_LEVIREQ;
621	if (pccard_mfc(pf->sc)) {
622		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
623			PCCARD_CCR_OPTION_ADDR_DECODE);
624		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
625	}
626	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
627
628	reg = 0;
629	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
630		reg |= PCCARD_CCR_STATUS_IOIS8;
631	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
632		reg |= PCCARD_CCR_STATUS_AUDIO;
633	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
634
635	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
636
637	if (pccard_mfc(pf->sc)) {
638		long tmp, iosize;
639
640		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
641		/* round up to nearest (2^n)-1 */
642		for (iosize = 1; iosize < tmp; iosize <<= 1)
643			;
644		iosize--;
645
646		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
647				 pf->pf_mfc_iobase & 0xff);
648		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
649				 (pf->pf_mfc_iobase >> 8) & 0xff);
650		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
651		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
652
653		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
654	}
655
656#ifdef PCCARDDEBUG
657	if (pccard_debug) {
658		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
659			device_printf(tmp->sc->dev,
660			    "function %d CCR at %d offset %x: "
661			    "%x %x %x %x, %x %x %x %x, %x\n",
662			    tmp->number, tmp->pf_ccr_window,
663			    tmp->pf_ccr_offset,
664			    pccard_ccr_read(tmp, 0x00),
665			    pccard_ccr_read(tmp, 0x02),
666			    pccard_ccr_read(tmp, 0x04),
667			    pccard_ccr_read(tmp, 0x06),
668			    pccard_ccr_read(tmp, 0x0A),
669			    pccard_ccr_read(tmp, 0x0C),
670			    pccard_ccr_read(tmp, 0x0E),
671			    pccard_ccr_read(tmp, 0x10),
672			    pccard_ccr_read(tmp, 0x12));
673		}
674	}
675#endif
676	pf->pf_flags |= PFF_ENABLED;
677	return (0);
678
679 bad:
680	/*
681	 * Decrement the reference count, and power down the socket, if
682	 * necessary.
683	 */
684	pf->sc->sc_enabled_count--;
685	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
686
687	return (1);
688}
689
690/* Disable PCCARD function. */
691static void
692pccard_function_disable(struct pccard_function *pf)
693{
694	struct pccard_function *tmp;
695	device_t dev = pf->sc->dev;
696
697	if (pf->cfe == NULL)
698		panic("pccard_function_disable: function not initialized");
699
700	if ((pf->pf_flags & PFF_ENABLED) == 0) {
701		/*
702		 * Don't do anything if we're already disabled.
703		 */
704		return;
705	}
706
707	if (pf->intr_handler != NULL) {
708		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
709		struct resource_list_entry *rle =
710		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
711		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
712		    pf->intr_handler_cookie);
713	}
714
715	/*
716	 * it's possible for different functions' CCRs to be in the same
717	 * underlying page.  Check for that.  Note we mark us as disabled
718	 * first to avoid matching ourself.
719	 */
720
721	pf->pf_flags &= ~PFF_ENABLED;
722	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
723		if ((tmp->pf_flags & PFF_ENABLED) &&
724		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
725		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
726		    (tmp->ccr_base - tmp->pf_ccr_offset +
727		    tmp->pf_ccr_realsize)))
728			break;
729	}
730
731	/* Not used by anyone else; unmap the CCR. */
732	if (tmp == NULL) {
733		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
734		    pf->ccr_res);
735		pf->ccr_res = NULL;
736	}
737
738	/*
739	 * Decrement the reference count, and power down the socket, if
740	 * necessary.
741	 */
742	pf->sc->sc_enabled_count--;
743}
744
745/*
746 * simulate the old "probe" routine.  In the new world order, the driver
747 * needs to grab devices while in the old they were assigned to the device by
748 * the pccardd process.  These symbols are exported to the upper layers.
749 */
750static int
751pccard_compat_do_probe(device_t bus, device_t dev)
752{
753	return (CARD_COMPAT_MATCH(dev));
754}
755
756static int
757pccard_compat_do_attach(device_t bus, device_t dev)
758{
759	int err;
760
761	err = CARD_COMPAT_PROBE(dev);
762	if (err <= 0)
763		err = CARD_COMPAT_ATTACH(dev);
764	return (err);
765}
766
767#define PCCARD_NPORT	2
768#define PCCARD_NMEM	5
769#define PCCARD_NIRQ	1
770#define PCCARD_NDRQ	0
771
772static int
773pccard_add_children(device_t dev, int busno)
774{
775	/* Call parent to scan for any current children */
776	return (0);
777}
778
779static int
780pccard_probe(device_t dev)
781{
782	device_set_desc(dev, "16-bit PCCard bus");
783	return (pccard_add_children(dev, device_get_unit(dev)));
784}
785
786static int
787pccard_attach(device_t dev)
788{
789	struct pccard_softc *sc = PCCARD_SOFTC(dev);
790
791	sc->dev = dev;
792	sc->sc_enabled_count = 0;
793	return (bus_generic_attach(dev));
794}
795
796static int
797pccard_detach(device_t dev)
798{
799	pccard_detach_card(dev);
800	return 0;
801}
802
803static int
804pccard_suspend(device_t self)
805{
806	pccard_detach_card(self);
807	return (0);
808}
809
810static
811int
812pccard_resume(device_t self)
813{
814	return (0);
815}
816
817static void
818pccard_print_resources(struct resource_list *rl, const char *name, int type,
819    int count, const char *format)
820{
821	struct resource_list_entry *rle;
822	int printed;
823	int i;
824
825	printed = 0;
826	for (i = 0; i < count; i++) {
827		rle = resource_list_find(rl, type, i);
828		if (rle != NULL) {
829			if (printed == 0)
830				printf(" %s ", name);
831			else if (printed > 0)
832				printf(",");
833			printed++;
834			printf(format, rle->start);
835			if (rle->count > 1) {
836				printf("-");
837				printf(format, rle->start + rle->count - 1);
838			}
839		} else if (i > 3) {
840			/* check the first few regardless */
841			break;
842		}
843	}
844}
845
846static int
847pccard_print_child(device_t dev, device_t child)
848{
849	struct pccard_ivar *devi = PCCARD_IVAR(child);
850	struct resource_list *rl = &devi->resources;
851	int retval = 0;
852
853	retval += bus_print_child_header(dev, child);
854	retval += printf(" at");
855
856	if (devi != NULL) {
857		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
858		    PCCARD_NPORT, "%#lx");
859		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
860		    PCCARD_NMEM, "%#lx");
861		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
862		    "%ld");
863		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
864		    "%ld");
865		retval += printf(" function %d config %d", devi->fcn->number,
866		    devi->fcn->cfe->number);
867	}
868
869	retval += bus_print_child_footer(dev, child);
870
871	return (retval);
872}
873
874static int
875pccard_set_resource(device_t dev, device_t child, int type, int rid,
876		 u_long start, u_long count)
877{
878	struct pccard_ivar *devi = PCCARD_IVAR(child);
879	struct resource_list *rl = &devi->resources;
880
881	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
882	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
883		return (EINVAL);
884	if (rid < 0)
885		return (EINVAL);
886	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
887		return (EINVAL);
888	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
889		return (EINVAL);
890	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
891		return (EINVAL);
892	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
893		return (EINVAL);
894
895	resource_list_add(rl, type, rid, start, start + count - 1, count);
896	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
897	    type, &rid, start, start + count - 1, count, 0))
898		return 0;
899	else
900		return ENOMEM;
901}
902
903static int
904pccard_get_resource(device_t dev, device_t child, int type, int rid,
905    u_long *startp, u_long *countp)
906{
907	struct pccard_ivar *devi = PCCARD_IVAR(child);
908	struct resource_list *rl = &devi->resources;
909	struct resource_list_entry *rle;
910
911	rle = resource_list_find(rl, type, rid);
912	if (rle == NULL)
913		return (ENOENT);
914
915	if (startp != NULL)
916		*startp = rle->start;
917	if (countp != NULL)
918		*countp = rle->count;
919
920	return (0);
921}
922
923static void
924pccard_delete_resource(device_t dev, device_t child, int type, int rid)
925{
926	struct pccard_ivar *devi = PCCARD_IVAR(child);
927	struct resource_list *rl = &devi->resources;
928	resource_list_delete(rl, type, rid);
929}
930
931static int
932pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
933    u_int32_t flags)
934{
935	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
936	    rid, flags));
937}
938
939static int
940pccard_set_memory_offset(device_t dev, device_t child, int rid,
941    u_int32_t offset, u_int32_t *deltap)
942
943{
944	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
945	    offset, deltap));
946}
947
948static void
949pccard_probe_nomatch(device_t bus, device_t child)
950{
951	struct pccard_ivar *devi = PCCARD_IVAR(child);
952	struct pccard_function *func = devi->fcn;
953	struct pccard_softc *sc = PCCARD_SOFTC(bus);
954
955	device_printf(bus, "<unknown card>");
956	printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
957	  sc->card.manufacturer, sc->card.product, func->number);
958	device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
959	  sc->card.cis1_info[1], sc->card.cis1_info[2]);
960	return;
961}
962
963static int
964pccard_child_location_str(device_t bus, device_t child, char *buf,
965    size_t buflen)
966{
967	struct pccard_ivar *devi = PCCARD_IVAR(child);
968	struct pccard_function *func = devi->fcn;
969
970	snprintf(buf, buflen, "function=%d", func->number);
971	return (0);
972}
973
974static int
975pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
976    size_t buflen)
977{
978	struct pccard_ivar *devi = PCCARD_IVAR(child);
979	struct pccard_function *func = devi->fcn;
980	struct pccard_softc *sc = PCCARD_SOFTC(bus);
981
982	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
983	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
984	    sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
985	    sc->card.cis1_info[1], func->function);
986	return (0);
987}
988
989static int
990pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
991{
992	struct pccard_ivar *devi = PCCARD_IVAR(child);
993	struct pccard_function *func = devi->fcn;
994	struct pccard_softc *sc = PCCARD_SOFTC(bus);
995
996	switch (which) {
997	default:
998	case PCCARD_IVAR_ETHADDR:
999		bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1000		break;
1001	case PCCARD_IVAR_VENDOR:
1002		*(u_int32_t *) result = sc->card.manufacturer;
1003		break;
1004	case PCCARD_IVAR_PRODUCT:
1005		*(u_int32_t *) result = sc->card.product;
1006		break;
1007	case PCCARD_IVAR_PRODEXT:
1008		*(u_int16_t *) result = sc->card.prodext;
1009		break;
1010	case PCCARD_IVAR_FUNCTION:
1011		*(u_int32_t *) result = func->function;
1012		break;
1013	case PCCARD_IVAR_FUNCTION_NUMBER:
1014		if (!func) {
1015			device_printf(bus, "No function number, bug!\n");
1016			return (ENOENT);
1017		}
1018		*(u_int32_t *) result = func->number;
1019		break;
1020	case PCCARD_IVAR_VENDOR_STR:
1021		*(char **) result = sc->card.cis1_info[0];
1022		break;
1023	case PCCARD_IVAR_PRODUCT_STR:
1024		*(char **) result = sc->card.cis1_info[1];
1025		break;
1026	case PCCARD_IVAR_CIS3_STR:
1027		*(char **) result = sc->card.cis1_info[2];
1028		break;
1029	case PCCARD_IVAR_CIS4_STR:
1030		*(char **) result = sc->card.cis1_info[3];
1031		break;
1032	}
1033	return (0);
1034}
1035
1036static void
1037pccard_driver_added(device_t dev, driver_t *driver)
1038{
1039	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1040	struct pccard_function *pf;
1041	device_t child;
1042
1043	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1044		if (STAILQ_EMPTY(&pf->cfe_head))
1045			continue;
1046		child = pf->dev;
1047		if (device_get_state(child) != DS_NOTPRESENT)
1048			continue;
1049		if (pccard_function_enable(pf) == 0 &&
1050		    device_probe_and_attach(child) == 0) {
1051			DEVPRINTF((sc->dev, "function %d CCR at %d "
1052			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1053			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1054			    pccard_ccr_read(pf, 0x00),
1055			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1056			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1057			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1058			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1059		} else {
1060			if (pf->cfe != NULL)
1061				pccard_function_disable(pf);
1062		}
1063	}
1064	return;
1065}
1066
1067static struct resource *
1068pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1069    u_long start, u_long end, u_long count, u_int flags)
1070{
1071	struct pccard_ivar *dinfo;
1072	struct resource_list_entry *rle = 0;
1073	int passthrough = (device_get_parent(child) != dev);
1074	struct resource *r = NULL;
1075
1076	if (passthrough) {
1077		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1078		    type, rid, start, end, count, flags));
1079	}
1080
1081	dinfo = device_get_ivars(child);
1082	rle = resource_list_find(&dinfo->resources, type, *rid);
1083
1084	if (rle == NULL)
1085		return (NULL);		/* no resource of that type/rid */
1086
1087	if (rle->res == NULL) {
1088		switch(type) {
1089		case SYS_RES_IOPORT:
1090			r = bus_alloc_resource(dev, type, rid, start, end,
1091			    count, rman_make_alignment_flags(count));
1092			if (r == NULL)
1093				goto bad;
1094			resource_list_add(&dinfo->resources, type, *rid,
1095			    rman_get_start(r), rman_get_end(r), count);
1096			rle = resource_list_find(&dinfo->resources, type, *rid);
1097			if (!rle)
1098				goto bad;
1099			rle->res = r;
1100			break;
1101		case SYS_RES_MEMORY:
1102			break;
1103		case SYS_RES_IRQ:
1104			break;
1105		}
1106		return (rle->res);
1107	}
1108	if (rman_get_device(rle->res) != dev)
1109		return (NULL);
1110	bus_release_resource(dev, type, *rid, rle->res);
1111	rle->res = NULL;
1112	switch(type) {
1113	case SYS_RES_IOPORT:
1114	case SYS_RES_MEMORY:
1115		if (!(flags & RF_ALIGNMENT_MASK))
1116			flags |= rman_make_alignment_flags(rle->count);
1117		break;
1118	case SYS_RES_IRQ:
1119		flags |= RF_SHAREABLE;
1120		break;
1121	}
1122	rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1123	    type, rid, rle->start, rle->end, rle->count, flags);
1124	return (rle->res);
1125bad:;
1126	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1127	return (NULL);
1128}
1129
1130static int
1131pccard_release_resource(device_t dev, device_t child, int type, int rid,
1132    struct resource *r)
1133{
1134	struct pccard_ivar *dinfo;
1135	int passthrough = (device_get_parent(child) != dev);
1136	struct resource_list_entry *rle = 0;
1137	int ret;
1138	int flags;
1139
1140	if (passthrough)
1141		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1142		    type, rid, r);
1143
1144	dinfo = device_get_ivars(child);
1145
1146	rle = resource_list_find(&dinfo->resources, type, rid);
1147
1148	if (!rle) {
1149		device_printf(dev, "Allocated resource not found, "
1150		    "%d %x %lx %lx\n",
1151		    type, rid, rman_get_start(r), rman_get_size(r));
1152		return ENOENT;
1153	}
1154	if (!rle->res) {
1155		device_printf(dev, "Allocated resource not recorded\n");
1156		return ENOENT;
1157	}
1158
1159	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1160	    type, rid, r);
1161	switch(type) {
1162	case SYS_RES_IOPORT:
1163	case SYS_RES_MEMORY:
1164		flags = rman_make_alignment_flags(rle->count);
1165		break;
1166	case SYS_RES_IRQ:
1167		flags = RF_SHAREABLE;
1168		break;
1169	default:
1170		flags = 0;
1171	}
1172	rle->res = bus_alloc_resource(dev, type, &rid,
1173	    rle->start, rle->end, rle->count, flags);
1174	if (rle->res == NULL)
1175		device_printf(dev, "release_resource: "
1176		    "unable to reaquire resource\n");
1177	return ret;
1178}
1179
1180static void
1181pccard_child_detached(device_t parent, device_t dev)
1182{
1183	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1184	struct pccard_function *pf = ivar->fcn;
1185
1186	pccard_function_disable(pf);
1187}
1188
1189static void
1190pccard_intr(void *arg)
1191{
1192	struct pccard_function *pf = (struct pccard_function*) arg;
1193	int reg;
1194	int doisr = 1;
1195
1196	/*
1197	 * MFC cards know if they interrupted, so we have to ack the
1198	 * interrupt and call the ISR.  Non-MFC cards don't have these
1199	 * bits, so they always get called.  Many non-MFC cards have
1200	 * this bit set always upon read, but some do not.
1201	 *
1202	 * We always ack the interrupt, even if there's no ISR
1203	 * for the card.  This is done on the theory that acking
1204	 * the interrupt will pacify the card enough to keep an
1205	 * interrupt storm from happening.  Of course this won't
1206	 * help in the non-MFC case.
1207	 *
1208	 * This has no impact for MPSAFEness of the client drivers.
1209	 * We register this with whatever flags the intr_handler
1210	 * was registered with.  All these functions are MPSAFE.
1211	 */
1212	if (pccard_mfc(pf->sc)) {
1213		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1214		if (reg & PCCARD_CCR_STATUS_INTR)
1215			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1216			    reg & ~PCCARD_CCR_STATUS_INTR);
1217		else
1218			doisr = 0;
1219	}
1220	if (pf->intr_handler != NULL && doisr)
1221		pf->intr_handler(pf->intr_handler_arg);
1222}
1223
1224static int
1225pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1226    int flags, driver_intr_t *intr, void *arg, void **cookiep)
1227{
1228	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1229	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1230	struct pccard_function *func = ivar->fcn;
1231	int err;
1232
1233	if (func->intr_handler != NULL)
1234		panic("Only one interrupt handler per function allowed");
1235	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1236	    func, cookiep);
1237	if (err != 0)
1238		return (err);
1239	func->intr_handler = intr;
1240	func->intr_handler_arg = arg;
1241	func->intr_handler_cookie = *cookiep;
1242	if (pccard_mfc(sc)) {
1243		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1244		    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1245		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1246	}
1247	return (0);
1248}
1249
1250static int
1251pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1252    void *cookie)
1253{
1254	struct pccard_softc *sc = PCCARD_SOFTC(dev);
1255	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1256	struct pccard_function *func = ivar->fcn;
1257	int ret;
1258
1259	if (pccard_mfc(sc)) {
1260		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1261		    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1262		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1263	}
1264	ret = bus_generic_teardown_intr(dev, child, r, cookie);
1265	if (ret == 0) {
1266		func->intr_handler = NULL;
1267		func->intr_handler_arg = NULL;
1268		func->intr_handler_cookie = NULL;
1269	}
1270
1271	return (ret);
1272}
1273
1274static device_method_t pccard_methods[] = {
1275	/* Device interface */
1276	DEVMETHOD(device_probe,		pccard_probe),
1277	DEVMETHOD(device_attach,	pccard_attach),
1278	DEVMETHOD(device_detach,	pccard_detach),
1279	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1280	DEVMETHOD(device_suspend,	pccard_suspend),
1281	DEVMETHOD(device_resume,	pccard_resume),
1282
1283	/* Bus interface */
1284	DEVMETHOD(bus_print_child,	pccard_print_child),
1285	DEVMETHOD(bus_driver_added,	pccard_driver_added),
1286	DEVMETHOD(bus_child_detached,	pccard_child_detached),
1287	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
1288	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1289	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1290	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1291	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
1292	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
1293	DEVMETHOD(bus_set_resource,	pccard_set_resource),
1294	DEVMETHOD(bus_get_resource,	pccard_get_resource),
1295	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1296	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
1297	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1298	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1299	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1300
1301	/* Card Interface */
1302	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
1303	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1304	DEVMETHOD(card_get_type,	pccard_card_gettype),
1305	DEVMETHOD(card_attach_card,	pccard_attach_card),
1306	DEVMETHOD(card_detach_card,	pccard_detach_card),
1307	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1308	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1309	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1310
1311	{ 0, 0 }
1312};
1313
1314static driver_t pccard_driver = {
1315	"pccard",
1316	pccard_methods,
1317	sizeof(struct pccard_softc)
1318};
1319
1320devclass_t	pccard_devclass;
1321
1322/* Maybe we need to have a slot device? */
1323DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1324DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1325MODULE_VERSION(pccard, 1);
1326