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