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