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