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