pccard.c revision 188179
166200Simp/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
252506Simp
3139749Simp/*-
452506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
552506Simp *
652506Simp * Redistribution and use in source and binary forms, with or without
752506Simp * modification, are permitted provided that the following conditions
852506Simp * are met:
952506Simp * 1. Redistributions of source code must retain the above copyright
1052506Simp *    notice, this list of conditions and the following disclaimer.
1152506Simp * 2. Redistributions in binary form must reproduce the above copyright
1252506Simp *    notice, this list of conditions and the following disclaimer in the
1352506Simp *    documentation and/or other materials provided with the distribution.
1452506Simp * 3. All advertising materials mentioning features or use of this software
1552506Simp *    must display the following acknowledgement:
1652506Simp *	This product includes software developed by Marc Horowitz.
1752506Simp * 4. The name of the author may not be used to endorse or promote products
1852506Simp *    derived from this software without specific prior written permission.
1952506Simp *
2052506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2152506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2252506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2352506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2452506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2552506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2652506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2752506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2852506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2952506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3052506Simp */
3152506Simp
32119418Sobrien#include <sys/cdefs.h>
33119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 188179 2009-02-05 19:38:31Z imp $");
34119418Sobrien
3552506Simp#include <sys/param.h>
3652506Simp#include <sys/systm.h>
3752506Simp#include <sys/malloc.h>
3852506Simp#include <sys/module.h>
3952506Simp#include <sys/kernel.h>
4052506Simp#include <sys/queue.h>
4191786Simp#include <sys/sysctl.h>
4252506Simp#include <sys/types.h>
4352506Simp
4452506Simp#include <sys/bus.h>
4552506Simp#include <machine/bus.h>
4652506Simp#include <sys/rman.h>
4752506Simp#include <machine/resource.h>
4852506Simp
4982781Sshiba#include <net/ethernet.h>
5082781Sshiba
5152506Simp#include <dev/pccard/pccardreg.h>
5252506Simp#include <dev/pccard/pccardvar.h>
53150362Simp#include <dev/pccard/pccardvarp.h>
54144930Simp#include <dev/pccard/pccard_cis.h>
5552506Simp
5655500Simp#include "power_if.h"
5759193Simp#include "card_if.h"
5855500Simp
5955500Simp#define PCCARDDEBUG
6055500Simp
6191786Simp/* sysctl vars */
6291786SimpSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
6391786Simp
6491786Simpint	pccard_debug = 0;
6591786SimpTUNABLE_INT("hw.pccard.debug", &pccard_debug);
6691786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
6791786Simp    &pccard_debug, 0,
6891786Simp  "pccard debug");
6991786Simp
7091786Simpint	pccard_cis_debug = 0;
7191786SimpTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
7291786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
7391786Simp    &pccard_cis_debug, 0, "pccard CIS debug");
7491786Simp
7552506Simp#ifdef PCCARDDEBUG
7652506Simp#define	DPRINTF(arg) if (pccard_debug) printf arg
7755500Simp#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
7867333Simp#define PRVERBOSE(arg) printf arg
7967333Simp#define DEVPRVERBOSE(arg) device_printf arg
8052506Simp#else
8152506Simp#define	DPRINTF(arg)
8255500Simp#define	DEVPRINTF(arg)
8367333Simp#define PRVERBOSE(arg) if (bootverbose) printf arg
8467333Simp#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
8552506Simp#endif
8652506Simp
8782378Sjonstatic int	pccard_ccr_read(struct pccard_function *pf, int ccr);
8882378Sjonstatic void	pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
8982378Sjonstatic int	pccard_attach_card(device_t dev);
90106362Simpstatic int	pccard_detach_card(device_t dev);
91181342Simpstatic void	pccard_function_init(struct pccard_function *pf, int entry);
9282378Sjonstatic void	pccard_function_free(struct pccard_function *pf);
9382378Sjonstatic int	pccard_function_enable(struct pccard_function *pf);
9482378Sjonstatic void	pccard_function_disable(struct pccard_function *pf);
9582378Sjonstatic int	pccard_probe(device_t dev);
9682378Sjonstatic int	pccard_attach(device_t dev);
9782378Sjonstatic int	pccard_detach(device_t dev);
9882378Sjonstatic void	pccard_print_resources(struct resource_list *rl,
9982378Sjon		    const char *name, int type, int count, const char *format);
10082378Sjonstatic int	pccard_print_child(device_t dev, device_t child);
10182378Sjonstatic int	pccard_set_resource(device_t dev, device_t child, int type,
10282378Sjon		    int rid, u_long start, u_long count);
10382378Sjonstatic int	pccard_get_resource(device_t dev, device_t child, int type,
10482378Sjon		    int rid, u_long *startp, u_long *countp);
10582378Sjonstatic void	pccard_delete_resource(device_t dev, device_t child, int type,
10682378Sjon		    int rid);
10782378Sjonstatic int	pccard_set_res_flags(device_t dev, device_t child, int type,
108188179Simp		    int rid, u_long flags);
10982378Sjonstatic int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
110140692Simp		    uint32_t offset, uint32_t *deltap);
111181342Simpstatic int	pccard_probe_and_attach_child(device_t dev, device_t child,
112181342Simp		    struct pccard_function *pf);
113104641Simpstatic void	pccard_probe_nomatch(device_t cbdev, device_t child);
11482378Sjonstatic int	pccard_read_ivar(device_t bus, device_t child, int which,
115188179Simp		    uintptr_t *result);
11682378Sjonstatic void	pccard_driver_added(device_t dev, driver_t *driver);
11782378Sjonstatic struct resource *pccard_alloc_resource(device_t dev,
11882378Sjon		    device_t child, int type, int *rid, u_long start,
11982378Sjon		    u_long end, u_long count, u_int flags);
12082378Sjonstatic int	pccard_release_resource(device_t dev, device_t child, int type,
12182378Sjon		    int rid, struct resource *r);
12282378Sjonstatic void	pccard_child_detached(device_t parent, device_t dev);
123170163Spisostatic int      pccard_filter(void *arg);
124170163Spisostatic void	pccard_intr(void *arg);
12582378Sjonstatic int	pccard_setup_intr(device_t dev, device_t child,
126166901Spiso		    struct resource *irq, int flags, driver_filter_t *filt,
127166901Spiso		    driver_intr_t *intr, void *arg, void **cookiep);
12882378Sjonstatic int	pccard_teardown_intr(device_t dev, device_t child,
12982378Sjon		    struct resource *r, void *cookie);
13052506Simp
13197613Stakawatastatic const struct pccard_product *
13297613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
13397613Stakawata			 const struct pccard_product *tab, size_t ent_size,
13497613Stakawata			 pccard_product_match_fn matchfn);
13597613Stakawata
13697613Stakawata
13782378Sjonstatic int
13874632Simppccard_ccr_read(struct pccard_function *pf, int ccr)
13952506Simp{
14052506Simp	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
14152506Simp	    pf->pf_ccr_offset + ccr));
14252506Simp}
14352506Simp
14482378Sjonstatic void
14574632Simppccard_ccr_write(struct pccard_function *pf, int ccr, int val)
14652506Simp{
14752506Simp	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
14852506Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
14952506Simp		    pf->pf_ccr_offset + ccr, val);
15052506Simp	}
15152506Simp}
15252506Simp
15359193Simpstatic int
154113242Simppccard_set_default_descr(device_t dev)
155113242Simp{
156121521Simp	const char *vendorstr, *prodstr;
157133865Simp	uint32_t vendor, prod;
158121521Simp	char *str;
159113242Simp
160113242Simp	if (pccard_get_vendor_str(dev, &vendorstr))
161113242Simp		return (0);
162113242Simp	if (pccard_get_product_str(dev, &prodstr))
163113242Simp		return (0);
164133865Simp	if (vendorstr != NULL && prodstr != NULL) {
165133865Simp		str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
166133865Simp		    M_WAITOK);
167133865Simp		sprintf(str, "%s %s", vendorstr, prodstr);
168133865Simp		device_set_desc_copy(dev, str);
169133865Simp		free(str, M_DEVBUF);
170133865Simp	} else {
171133865Simp		if (pccard_get_vendor(dev, &vendor))
172133865Simp			return (0);
173133865Simp		if (pccard_get_product(dev, &prod))
174133865Simp			return (0);
175133865Simp		str = malloc(100, M_DEVBUF, M_WAITOK);
176133865Simp		snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod);
177133865Simp		device_set_desc_copy(dev, str);
178133865Simp		free(str, M_DEVBUF);
179133865Simp	}
180113242Simp	return (0);
181113242Simp}
182113242Simp
183113242Simpstatic int
18459193Simppccard_attach_card(device_t dev)
18552506Simp{
18664850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
18752506Simp	struct pccard_function *pf;
18865917Simp	struct pccard_ivar *ivar;
18961788Simp	device_t child;
190102713Simp	int i;
19152506Simp
192153773Simp	if (!STAILQ_EMPTY(&sc->card.pf_head)) {
193153773Simp		if (bootverbose || pccard_debug)
194153773Simp			device_printf(dev, "Card already inserted.\n");
195153773Simp	}
19652506Simp
19755500Simp	DEVPRINTF((dev, "chip_socket_enable\n"));
19855500Simp	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
19952506Simp
20055500Simp	DEVPRINTF((dev, "read_cis\n"));
20152506Simp	pccard_read_cis(sc);
20252506Simp
20355500Simp	DEVPRINTF((dev, "check_cis_quirks\n"));
20452506Simp	pccard_check_cis_quirks(dev);
20552506Simp
20652506Simp	/*
20752506Simp	 * bail now if the card has no functions, or if there was an error in
20852506Simp	 * the cis.
20952506Simp	 */
21052506Simp
21170715Sjon	if (sc->card.error) {
212102713Simp		device_printf(dev, "CARD ERROR!\n");
21352506Simp		return (1);
21470715Sjon	}
21570715Sjon	if (STAILQ_EMPTY(&sc->card.pf_head)) {
216102713Simp		device_printf(dev, "Card has no functions!\n");
21752506Simp		return (1);
21870715Sjon	}
21952506Simp
22090436Simp	if (bootverbose || pccard_debug)
22152506Simp		pccard_print_cis(dev);
22252506Simp
22355500Simp	DEVPRINTF((dev, "functions scanning\n"));
224102713Simp	i = -1;
22552506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
226102713Simp		i++;
227102713Simp		if (STAILQ_EMPTY(&pf->cfe_head)) {
228102713Simp			device_printf(dev,
229102713Simp			    "Function %d has no config entries.!\n", i);
23052506Simp			continue;
231102713Simp		}
23252506Simp		pf->sc = sc;
23352506Simp		pf->cfe = NULL;
23464927Simp		pf->dev = NULL;
23552506Simp	}
236104641Simp	DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
237102713Simp	    pccard_mfc(sc)));
23882378Sjon
23952506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
24052506Simp		if (STAILQ_EMPTY(&pf->cfe_head))
24152506Simp			continue;
24267897Sdwmalone		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
243111119Simp		    M_WAITOK | M_ZERO);
244143815Simp		resource_list_init(&ivar->resources);
24561788Simp		child = device_add_child(dev, NULL, -1);
24665917Simp		device_set_ivars(child, ivar);
247147963Simp		ivar->pf = pf;
24867187Simp		pf->dev = child;
249181342Simp		pccard_probe_and_attach_child(dev, child, pf);
25052506Simp	}
25174632Simp	return (0);
25252506Simp}
25352506Simp
25459193Simpstatic int
255181342Simppccard_probe_and_attach_child(device_t dev, device_t child,
256181342Simp    struct pccard_function *pf)
257181342Simp{
258181342Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
259181342Simp	int error;
260181342Simp
261181342Simp	/*
262181342Simp	 * In NetBSD, the drivers are responsible for activating each
263181342Simp	 * function of a card and selecting the config to use.  In
264181342Simp	 * FreeBSD, all that's done automatically in the typical lazy
265181342Simp	 * way we do device resoruce allocation (except we pick the
266181342Simp	 * cfe up front).  This is the biggest depature from the
267181395Simp	 * inherited NetBSD model, apart from the FreeBSD resource code.
268181342Simp	 *
269181342Simp	 * This seems to work well in practice for most cards.
270181342Simp	 * However, there are two cases that are problematic.  If a
271181342Simp	 * driver wishes to pick and chose which config entry to use,
272181342Simp	 * then this method falls down.  These are usually older
273181342Simp	 * cards.  In addition, there are some cards that have
274181342Simp	 * multiple hardware units on the cards, but presents only one
275181342Simp	 * CIS chain.  These cards are combination cards, but only one
276181342Simp	 * of these units can be on at a time.
277181342Simp	 *
278181342Simp	 * To overcome this limitation, while preserving the basic
279181342Simp	 * model, the probe routine can select a cfe and try to
280181342Simp	 * activate it.  If that succeeds, then we'll keep track of
281181342Simp	 * and let that information persist until we attach the card.
282181342Simp	 * Probe routines that do this MUST return 0, and cannot
283181342Simp	 * participate in the bidding process for a device.  This
284181342Simp	 * seems harsh until you realize that if a probe routine knows
285181342Simp	 * enough to override the cfe we pick, then chances are very
286181342Simp	 * very good that it is the only driver that could hope to
287181342Simp	 * cope with the card.  Bidding is for generic drivers, and
288181342Simp	 * while some of them may also match, none of them will do
289181342Simp	 * configuration override.
290181342Simp	 */
291181342Simp	error = device_probe(child);
292181342Simp	if (error != 0)
293181342Simp		goto out;
294181342Simp	pccard_function_init(pf, -1);
295181342Simp	if (sc->sc_enabled_count == 0)
296181342Simp		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
297181342Simp	if (pccard_function_enable(pf) == 0 &&
298181342Simp	    pccard_set_default_descr(child) == 0 &&
299181342Simp	    device_attach(child) == 0) {
300181342Simp		DEVPRINTF((sc->dev, "function %d CCR at %d offset %x mask %x: "
301181342Simp		    "%x %x %x %x, %x %x %x %x, %x\n",
302181342Simp		    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
303181342Simp		    pf->ccr_mask, pccard_ccr_read(pf, 0x00),
304181342Simp		    pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
305181342Simp		    pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
306181342Simp		    pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
307181342Simp		    pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
308181342Simp		return (0);
309181342Simp	}
310181342Simp	error = ENXIO;
311181342Simpout:;
312181342Simp	/*
313181342Simp	 * Probe may fail AND also try to select a cfe, if so, free
314181342Simp	 * it.  This is how we do cfe override.  Or the attach fails.
315181342Simp	 * Either way, we have to clean up.
316181342Simp	 */
317181342Simp	if (pf->cfe != NULL)
318181342Simp		pccard_function_disable(pf);
319181342Simp	pf->cfe = NULL;
320181342Simp	pccard_function_free(pf);
321181342Simp	return error;
322181342Simp}
323181342Simp
324181342Simpstatic int
325106362Simppccard_detach_card(device_t dev)
32652506Simp{
32764850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
32852506Simp	struct pccard_function *pf;
32982378Sjon	struct pccard_config_entry *cfe;
330166453Simp	struct pccard_ivar *devi;
331119755Simp	int state;
33252506Simp
33352506Simp	/*
33452506Simp	 * We are running on either the PCCARD socket's event thread
33552506Simp	 * or in user context detaching a device by user request.
33652506Simp	 */
33752506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
338119755Simp		if (pf->dev == NULL)
339119755Simp			continue;
340119755Simp		state = device_get_state(pf->dev);
34182378Sjon		if (state == DS_ATTACHED || state == DS_BUSY)
34282378Sjon			device_detach(pf->dev);
34386907Simp		if (pf->cfe != NULL)
34486907Simp			pccard_function_disable(pf);
34582378Sjon		pccard_function_free(pf);
346166453Simp		devi = PCCARD_IVAR(pf->dev);
347106896Simp		device_delete_child(dev, pf->dev);
348166453Simp		free(devi, M_DEVBUF);
34952506Simp	}
35082378Sjon	if (sc->sc_enabled_count == 0)
35182378Sjon		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
35282378Sjon
35382378Sjon	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
35482378Sjon		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
35582378Sjon			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
35682378Sjon			free(cfe, M_DEVBUF);
35782378Sjon		}
35882378Sjon		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
35982378Sjon		free(pf, M_DEVBUF);
36082378Sjon	}
361153773Simp	STAILQ_INIT(&sc->card.pf_head);
36274632Simp	return (0);
36352506Simp}
36452506Simp
36597613Stakawatastatic const struct pccard_product *
36697613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
367112359Simp    const struct pccard_product *tab, size_t ent_size,
368112359Simp    pccard_product_match_fn matchfn)
36966200Simp{
37066200Simp	const struct pccard_product *ent;
37166200Simp	int matches;
372140692Simp	uint32_t vendor;
373140692Simp	uint32_t prod;
374121521Simp	const char *vendorstr;
375121521Simp	const char *prodstr;
376140366Simp	const char *cis3str;
377140366Simp	const char *cis4str;
37866200Simp
37966200Simp#ifdef DIAGNOSTIC
38066200Simp	if (sizeof *ent > ent_size)
381112359Simp		panic("pccard_product_lookup: bogus ent_size %jd",
382112359Simp		    (intmax_t) ent_size);
38366200Simp#endif
38466200Simp	if (pccard_get_vendor(dev, &vendor))
38566200Simp		return (NULL);
38666200Simp	if (pccard_get_product(dev, &prod))
38766200Simp		return (NULL);
38866200Simp	if (pccard_get_vendor_str(dev, &vendorstr))
38966200Simp		return (NULL);
39066200Simp	if (pccard_get_product_str(dev, &prodstr))
39166200Simp		return (NULL);
392140366Simp	if (pccard_get_cis3_str(dev, &cis3str))
393140366Simp		return (NULL);
394140366Simp	if (pccard_get_cis4_str(dev, &cis4str))
395140366Simp		return (NULL);
396113313Simp	for (ent = tab; ent->pp_vendor != 0; ent =
39782378Sjon	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
39866200Simp		matches = 1;
39986642Simp		if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
400113078Ssanpei		    ent->pp_product == PCCARD_PRODUCT_ANY &&
40186642Simp		    ent->pp_cis[0] == NULL &&
40286642Simp		    ent->pp_cis[1] == NULL) {
403113300Simp			if (ent->pp_name)
404113300Simp				device_printf(dev,
405113300Simp				    "Total wildcard entry ignored for %s\n",
406113300Simp				    ent->pp_name);
40786642Simp			continue;
40886642Simp		}
40966200Simp		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
41066200Simp		    vendor != ent->pp_vendor)
41166200Simp			matches = 0;
41266200Simp		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
41366200Simp		    prod != ent->pp_product)
41466200Simp			matches = 0;
41571322Simp		if (matches && ent->pp_cis[0] &&
416133865Simp		    (vendorstr == NULL ||
417133865Simp		    strcmp(ent->pp_cis[0], vendorstr) != 0))
41866200Simp			matches = 0;
41971322Simp		if (matches && ent->pp_cis[1] &&
420133865Simp		    (prodstr == NULL ||
421133865Simp		    strcmp(ent->pp_cis[1], prodstr) != 0))
42266200Simp			matches = 0;
423140366Simp		if (matches && ent->pp_cis[2] &&
424140366Simp		    (cis3str == NULL ||
425140366Simp		    strcmp(ent->pp_cis[2], cis3str) != 0))
426140366Simp			matches = 0;
427140366Simp		if (matches && ent->pp_cis[3] &&
428140366Simp		    (cis4str == NULL ||
429140366Simp		    strcmp(ent->pp_cis[3], cis4str) != 0))
430140366Simp			matches = 0;
43166200Simp		if (matchfn != NULL)
43266200Simp			matches = (*matchfn)(dev, ent, matches);
43366200Simp		if (matches)
43466200Simp			return (ent);
43566200Simp	}
43666200Simp	return (NULL);
43766200Simp}
43866200Simp
439181342Simp/**
440181342Simp * @brief pccard_select_cfe
441181342Simp *
442181342Simp * Select a cfe entry to use.  Should be called from the pccard's probe
443181342Simp * routine after it knows for sure that it wants this card.
444181342Simp *
445181342Simp * XXX I think we need to make this symbol be static, ala the kobj stuff
446181342Simp * we do for everything else.  This is a quick hack.
447181342Simp */
448181342Simpint
449181342Simppccard_select_cfe(device_t dev, int entry)
450181342Simp{
451181342Simp	struct pccard_ivar *devi = PCCARD_IVAR(dev);
452181342Simp	struct pccard_function *pf = devi->pf;
453181342Simp
454181342Simp	pccard_function_init(pf, entry);
455181342Simp	return (pf->cfe ? 0 : ENOMEM);
456181342Simp}
457181342Simp
45852506Simp/*
45952506Simp * Initialize a PCCARD function.  May be called as long as the function is
46052506Simp * disabled.
46182382Simp *
46282382Simp * Note: pccard_function_init should not keep resources allocated.  It should
46382382Simp * only set them up ala isa pnp, set the values in the rl lists, and return.
46482382Simp * Any resource held after pccard_function_init is called is a bug.  However,
46582382Simp * the bus routines to get the resources also assume that pccard_function_init
46682382Simp * does this, so they need to be fixed too.
46752506Simp */
46882378Sjonstatic void
469181342Simppccard_function_init(struct pccard_function *pf, int entry)
47052506Simp{
47165917Simp	struct pccard_config_entry *cfe;
47267242Simp	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
47367242Simp	struct resource_list *rl = &devi->resources;
47470762Simp	struct resource_list_entry *rle;
47567242Simp	struct resource *r = 0;
47667242Simp	device_t bus;
477144955Simp	u_long start, end, len;
478144955Simp	int i, rid, spaces;
47965917Simp
48070715Sjon	if (pf->pf_flags & PFF_ENABLED) {
48170715Sjon		printf("pccard_function_init: function is enabled");
48270715Sjon		return;
48370715Sjon	}
484181342Simp
485181342Simp	/*
486181342Simp	 * Driver probe routine requested a specific entry already
487181342Simp	 * that succeeded.
488181342Simp	 */
489181342Simp	if (pf->cfe != NULL)
490181342Simp		return;
491181342Simp
492181342Simp	/*
493181342Simp	 * walk the list of configuration entries until we find one that
494181342Simp	 * we can allocate all the resources to.
495181342Simp	 */
49667242Simp	bus = device_get_parent(pf->dev);
49772012Sphk	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
498144930Simp		if (cfe->iftype != PCCARD_IFTYPE_IO)
499144930Simp			continue;
500181342Simp		if (entry != -1 && cfe->number != entry)
501181342Simp			continue;
50290897Simp		spaces = 0;
50367187Simp		for (i = 0; i < cfe->num_iospace; i++) {
50467424Simp			start = cfe->iospace[i].start;
50567424Simp			if (start)
50667424Simp				end = start + cfe->iospace[i].length - 1;
50767424Simp			else
508144930Simp				end = ~0UL;
509144955Simp			DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n",
51090897Simp			    i, start, end));
511144930Simp			rid = i;
512144955Simp			len = cfe->iospace[i].length;
513144955Simp			r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
514144955Simp			    start, end, len, rman_make_alignment_flags(len));
515144955Simp			if (r == NULL)
51667187Simp				goto not_this_one;
517144927Simp			rle = resource_list_add(rl, SYS_RES_IOPORT,
518144930Simp			    rid, rman_get_start(r), rman_get_end(r),
51967242Simp			    cfe->iospace[i].length);
520144927Simp			if (rle == NULL)
521144930Simp				panic("Cannot add resource rid %d IOPORT", rid);
52276424Simp			rle->res = r;
52390897Simp			spaces++;
52467187Simp		}
525144930Simp		for (i = 0; i < cfe->num_memspace; i++) {
526144930Simp			start = cfe->memspace[i].hostaddr;
527144930Simp			if (start)
528144930Simp				end = start + cfe->memspace[i].length - 1;
529144930Simp			else
530144930Simp				end = ~0UL;
531144955Simp			DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n",
532144930Simp			    i, start, end));
533144930Simp			rid = i;
534144955Simp			len = cfe->memspace[i].length;
535144955Simp			r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
536144955Simp			    start, end, len, rman_make_alignment_flags(len));
537144955Simp			if (r == NULL)
538144930Simp				goto not_this_one;
539144930Simp			rle = resource_list_add(rl, SYS_RES_MEMORY,
540144930Simp			    rid, rman_get_start(r), rman_get_end(r),
541144930Simp			    cfe->memspace[i].length);
542144930Simp			if (rle == NULL)
543144930Simp				panic("Cannot add resource rid %d MEM", rid);
544144930Simp			rle->res = r;
545144930Simp			spaces++;
54667187Simp		}
54790897Simp		if (spaces == 0) {
548140488Simp			DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
54990897Simp			goto not_this_one;
55090897Simp		}
55167187Simp		if (cfe->irqmask) {
552144930Simp			rid = 0;
553144955Simp			r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
554144955Simp			    RF_SHAREABLE);
555144955Simp			if (r == NULL)
55667187Simp				goto not_this_one;
557144930Simp			rle = resource_list_add(rl, SYS_RES_IRQ, rid,
55867242Simp			    rman_get_start(r), rman_get_end(r), 1);
559144927Simp			if (rle == NULL)
560144930Simp				panic("Cannot add resource rid %d IRQ", rid);
56176424Simp			rle->res = r;
56267187Simp		}
56367187Simp		/* If we get to here, we've allocated all we need */
56467167Simp		pf->cfe = cfe;
56567187Simp		break;
56667187Simp	    not_this_one:;
56767424Simp		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
56867424Simp		    cfe->number));
569144955Simp		resource_list_purge(rl);
57067167Simp	}
57152506Simp}
57252506Simp
57382378Sjon/*
57482378Sjon * Free resources allocated by pccard_function_init(), May be called as long
57582378Sjon * as the function is disabled.
57682382Simp *
57782382Simp * NOTE: This function should be unnecessary.  pccard_function_init should
57882382Simp * never keep resources initialized.
57982378Sjon */
58082378Sjonstatic void
58182378Sjonpccard_function_free(struct pccard_function *pf)
58282378Sjon{
58382378Sjon	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
58482378Sjon	struct resource_list_entry *rle;
58582378Sjon
58682378Sjon	if (pf->pf_flags & PFF_ENABLED) {
587181342Simp		printf("pccard_function_free: function is enabled");
58882378Sjon		return;
58982378Sjon	}
59082378Sjon
591143785Simp	STAILQ_FOREACH(rle, &devi->resources, link) {
59282378Sjon		if (rle->res) {
593113242Simp			if (rman_get_device(rle->res) != pf->sc->dev)
59482378Sjon				device_printf(pf->sc->dev,
59582378Sjon				    "function_free: Resource still owned by "
59682378Sjon				    "child, oops. "
59782378Sjon				    "(type=%d, rid=%d, addr=%lx)\n",
59882378Sjon				    rle->type, rle->rid,
59982378Sjon				    rman_get_start(rle->res));
60082378Sjon			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
601113242Simp			    pf->sc->dev, rle->type, rle->rid, rle->res);
60282378Sjon			rle->res = NULL;
60382378Sjon		}
60482378Sjon	}
60582378Sjon	resource_list_free(&devi->resources);
60682378Sjon}
60782378Sjon
608121905Simpstatic void
609121905Simppccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
610121905Simp    bus_addr_t offset, bus_size_t size)
611121905Simp{
612121958Simp	bus_size_t iosize, tmp;
613121905Simp
614121905Simp	if (addr != 0) {
615121905Simp		if (pf->pf_mfc_iomax == 0) {
616121905Simp			pf->pf_mfc_iobase = addr + offset;
617121905Simp			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
618121905Simp		} else {
619121905Simp			/* this makes the assumption that nothing overlaps */
620121905Simp			if (pf->pf_mfc_iobase > addr + offset)
621121905Simp				pf->pf_mfc_iobase = addr + offset;
622121905Simp			if (pf->pf_mfc_iomax < addr + offset + size)
623121905Simp				pf->pf_mfc_iomax = addr + offset + size;
624121905Simp		}
625121905Simp	}
626121905Simp
627121905Simp	tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
628121905Simp	/* round up to nearest (2^n)-1 */
629121905Simp	for (iosize = 1; iosize < tmp; iosize <<= 1)
630121905Simp		;
631121905Simp	iosize--;
632121905Simp
633122032Simp	DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
634122032Simp	    (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
635121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
636121905Simp	    pf->pf_mfc_iobase & 0xff);
637121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
638121905Simp	    (pf->pf_mfc_iobase >> 8) & 0xff);
639121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
640121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
641121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
642121905Simp}
643121905Simp
64452506Simp/* Enable a PCCARD function */
64582378Sjonstatic int
64655720Simppccard_function_enable(struct pccard_function *pf)
64752506Simp{
64852506Simp	struct pccard_function *tmp;
64952506Simp	int reg;
65055720Simp	device_t dev = pf->sc->dev;
65170746Simp
65267333Simp	if (pf->cfe == NULL) {
65367333Simp		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
65474632Simp		return (ENOMEM);
65567333Simp	}
65652506Simp
657181392Simp	if (pf->pf_flags & PFF_ENABLED)
658181392Simp		return (0);
65982378Sjon	pf->sc->sc_enabled_count++;
66052506Simp
66152506Simp	/*
66252506Simp	 * it's possible for different functions' CCRs to be in the same
66352506Simp	 * underlying page.  Check for that.
66452506Simp	 */
66552506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
66652506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
66752506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
66852506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
66982378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
67082378Sjon		    tmp->pf_ccr_realsize))) {
67152506Simp			pf->pf_ccrt = tmp->pf_ccrt;
67252506Simp			pf->pf_ccrh = tmp->pf_ccrh;
67352506Simp			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
67452506Simp
67552506Simp			/*
67652506Simp			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
67752506Simp			 * tmp->ccr_base) + pf->ccr_base;
67852506Simp			 */
67970715Sjon			/* pf->pf_ccr_offset =
68052506Simp			    (tmp->pf_ccr_offset + pf->ccr_base) -
68170715Sjon			    tmp->ccr_base; */
68252506Simp			pf->pf_ccr_window = tmp->pf_ccr_window;
68352506Simp			break;
68452506Simp		}
68552506Simp	}
68652506Simp	if (tmp == NULL) {
68755720Simp		pf->ccr_rid = 0;
68855720Simp		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
689150097Simp		    &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
69070715Sjon		if (!pf->ccr_res)
69152506Simp			goto bad;
692106914Smux		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
69370746Simp		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
69470746Simp		    pf->ccr_base));
69561788Simp		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
69661788Simp		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
69770715Sjon		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
69870748Simp		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
69955720Simp		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
70055720Simp		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
70155720Simp		pf->pf_ccr_realsize = 1;
70252506Simp	}
70352506Simp
70452506Simp	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
70552506Simp	reg |= PCCARD_CCR_OPTION_LEVIREQ;
70652506Simp	if (pccard_mfc(pf->sc)) {
70752506Simp		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
70852506Simp			PCCARD_CCR_OPTION_ADDR_DECODE);
70982383Simp		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
71052506Simp	}
71152506Simp	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
71252506Simp
71352506Simp	reg = 0;
71452506Simp	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
71552506Simp		reg |= PCCARD_CCR_STATUS_IOIS8;
71652506Simp	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
71752506Simp		reg |= PCCARD_CCR_STATUS_AUDIO;
71852506Simp	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
71952506Simp
72052506Simp	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
72152506Simp
722121905Simp	if (pccard_mfc(pf->sc))
723121905Simp		pccard_mfc_adjust_iobase(pf, 0, 0, 0);
72452506Simp
72552506Simp#ifdef PCCARDDEBUG
72652506Simp	if (pccard_debug) {
72752506Simp		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
72870715Sjon			device_printf(tmp->sc->dev,
72955500Simp			    "function %d CCR at %d offset %x: "
73055500Simp			    "%x %x %x %x, %x %x %x %x, %x\n",
73170715Sjon			    tmp->number, tmp->pf_ccr_window,
73255500Simp			    tmp->pf_ccr_offset,
73355500Simp			    pccard_ccr_read(tmp, 0x00),
73455500Simp			    pccard_ccr_read(tmp, 0x02),
73555500Simp			    pccard_ccr_read(tmp, 0x04),
73655500Simp			    pccard_ccr_read(tmp, 0x06),
73755500Simp			    pccard_ccr_read(tmp, 0x0A),
73870715Sjon			    pccard_ccr_read(tmp, 0x0C),
73955500Simp			    pccard_ccr_read(tmp, 0x0E),
74055500Simp			    pccard_ccr_read(tmp, 0x10),
74155500Simp			    pccard_ccr_read(tmp, 0x12));
74252506Simp		}
74352506Simp	}
74452506Simp#endif
74552506Simp	pf->pf_flags |= PFF_ENABLED;
74652506Simp	return (0);
74752506Simp
74852506Simp bad:
74952506Simp	/*
75052506Simp	 * Decrement the reference count, and power down the socket, if
75152506Simp	 * necessary.
75252506Simp	 */
75382378Sjon	pf->sc->sc_enabled_count--;
75465098Simp	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
75552506Simp
75652506Simp	return (1);
75752506Simp}
75852506Simp
75952506Simp/* Disable PCCARD function. */
76082378Sjonstatic void
76155720Simppccard_function_disable(struct pccard_function *pf)
76252506Simp{
76352506Simp	struct pccard_function *tmp;
76455720Simp	device_t dev = pf->sc->dev;
76552506Simp
76652506Simp	if (pf->cfe == NULL)
76761788Simp		panic("pccard_function_disable: function not initialized");
76852506Simp
769181392Simp	if ((pf->pf_flags & PFF_ENABLED) == 0)
77052506Simp		return;
77170715Sjon	if (pf->intr_handler != NULL) {
77282378Sjon		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
77382378Sjon		struct resource_list_entry *rle =
77482378Sjon		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
775144927Simp		if (rle == NULL)
776144927Simp			panic("Can't disable an interrupt with no IRQ res\n");
77782378Sjon		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
77882378Sjon		    pf->intr_handler_cookie);
77970715Sjon	}
78070715Sjon
78152506Simp	/*
78252506Simp	 * it's possible for different functions' CCRs to be in the same
78352506Simp	 * underlying page.  Check for that.  Note we mark us as disabled
78452506Simp	 * first to avoid matching ourself.
78552506Simp	 */
78652506Simp
78752506Simp	pf->pf_flags &= ~PFF_ENABLED;
78852506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
78952506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
79052506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
79152506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
79282378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
79382378Sjon		    tmp->pf_ccr_realsize)))
79452506Simp			break;
79552506Simp	}
79652506Simp
79752506Simp	/* Not used by anyone else; unmap the CCR. */
79852506Simp	if (tmp == NULL) {
79970715Sjon		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
80055720Simp		    pf->ccr_res);
80155720Simp		pf->ccr_res = NULL;
80252506Simp	}
80352506Simp
80452506Simp	/*
80552506Simp	 * Decrement the reference count, and power down the socket, if
80652506Simp	 * necessary.
80752506Simp	 */
80882378Sjon	pf->sc->sc_enabled_count--;
80952506Simp}
81052506Simp
81153873Simp#define PCCARD_NPORT	2
81253873Simp#define PCCARD_NMEM	5
81353873Simp#define PCCARD_NIRQ	1
81453873Simp#define PCCARD_NDRQ	0
81553873Simp
81652506Simpstatic int
81752506Simppccard_probe(device_t dev)
81852506Simp{
81967333Simp	device_set_desc(dev, "16-bit PCCard bus");
820150391Simp	return (0);
82152506Simp}
82252506Simp
82359193Simpstatic int
82459193Simppccard_attach(device_t dev)
82559193Simp{
82664850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
827150362Simp	int err;
82861788Simp
82959193Simp	sc->dev = dev;
83061788Simp	sc->sc_enabled_count = 0;
831150362Simp	if ((err = pccard_device_create(sc)) != 0)
832150362Simp		return  (err);
833153773Simp	STAILQ_INIT(&sc->card.pf_head);
83474632Simp	return (bus_generic_attach(dev));
83559193Simp}
83659193Simp
83782378Sjonstatic int
83882378Sjonpccard_detach(device_t dev)
83982378Sjon{
840106362Simp	pccard_detach_card(dev);
841150362Simp	pccard_device_destroy(device_get_softc(dev));
842150362Simp	return (0);
84382378Sjon}
84482378Sjon
84587975Simpstatic int
84687975Simppccard_suspend(device_t self)
84787975Simp{
848106362Simp	pccard_detach_card(self);
84987975Simp	return (0);
85087975Simp}
85187975Simp
85287975Simpstatic
85387975Simpint
85487975Simppccard_resume(device_t self)
85587975Simp{
85687975Simp	return (0);
85787975Simp}
85887975Simp
85953873Simpstatic void
86053873Simppccard_print_resources(struct resource_list *rl, const char *name, int type,
86153873Simp    int count, const char *format)
86253873Simp{
86353873Simp	struct resource_list_entry *rle;
86453873Simp	int printed;
86553873Simp	int i;
86653873Simp
86753873Simp	printed = 0;
86853873Simp	for (i = 0; i < count; i++) {
86953873Simp		rle = resource_list_find(rl, type, i);
87076424Simp		if (rle != NULL) {
87153873Simp			if (printed == 0)
87253873Simp				printf(" %s ", name);
87353873Simp			else if (printed > 0)
87453873Simp				printf(",");
87553873Simp			printed++;
87653873Simp			printf(format, rle->start);
87753873Simp			if (rle->count > 1) {
87853873Simp				printf("-");
87953873Simp				printf(format, rle->start + rle->count - 1);
88053873Simp			}
88153873Simp		} else if (i > 3) {
88253873Simp			/* check the first few regardless */
88353873Simp			break;
88453873Simp		}
88553873Simp	}
88653873Simp}
88753873Simp
88853873Simpstatic int
88953873Simppccard_print_child(device_t dev, device_t child)
89053873Simp{
89166847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
89253873Simp	struct resource_list *rl = &devi->resources;
89353873Simp	int retval = 0;
89453873Simp
89553873Simp	retval += bus_print_child_header(dev, child);
89653873Simp	retval += printf(" at");
89753873Simp
89876424Simp	if (devi != NULL) {
89953873Simp		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
90053873Simp		    PCCARD_NPORT, "%#lx");
90153873Simp		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
90253873Simp		    PCCARD_NMEM, "%#lx");
90353873Simp		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
90453873Simp		    "%ld");
90570715Sjon		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
90653873Simp		    "%ld");
907147963Simp		retval += printf(" function %d config %d", devi->pf->number,
908147963Simp		    devi->pf->cfe->number);
90953873Simp	}
91053873Simp
91153873Simp	retval += bus_print_child_footer(dev, child);
91253873Simp
91353873Simp	return (retval);
91453873Simp}
91553873Simp
91653873Simpstatic int
91753873Simppccard_set_resource(device_t dev, device_t child, int type, int rid,
918147963Simp    u_long start, u_long count)
91953873Simp{
92066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
92153873Simp	struct resource_list *rl = &devi->resources;
92253873Simp
92353873Simp	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
92453873Simp	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
92574632Simp		return (EINVAL);
92653873Simp	if (rid < 0)
92774632Simp		return (EINVAL);
92853873Simp	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
92974632Simp		return (EINVAL);
93053873Simp	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
93174632Simp		return (EINVAL);
93253873Simp	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
93374632Simp		return (EINVAL);
93453873Simp	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
93574632Simp		return (EINVAL);
93653873Simp
93753873Simp	resource_list_add(rl, type, rid, start, start + count - 1, count);
93882378Sjon	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
93982378Sjon	    type, &rid, start, start + count - 1, count, 0))
94082378Sjon		return 0;
94182378Sjon	else
94282378Sjon		return ENOMEM;
94353873Simp}
94453873Simp
94553873Simpstatic int
94653873Simppccard_get_resource(device_t dev, device_t child, int type, int rid,
94753873Simp    u_long *startp, u_long *countp)
94853873Simp{
94966847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
95053873Simp	struct resource_list *rl = &devi->resources;
95153873Simp	struct resource_list_entry *rle;
95253873Simp
95353873Simp	rle = resource_list_find(rl, type, rid);
95476424Simp	if (rle == NULL)
95574632Simp		return (ENOENT);
95670715Sjon
95776424Simp	if (startp != NULL)
95853873Simp		*startp = rle->start;
95976424Simp	if (countp != NULL)
96053873Simp		*countp = rle->count;
96153873Simp
96274632Simp	return (0);
96353873Simp}
96453873Simp
96553873Simpstatic void
96653873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid)
96753873Simp{
96866847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
96953873Simp	struct resource_list *rl = &devi->resources;
97053873Simp	resource_list_delete(rl, type, rid);
97153873Simp}
97253873Simp
97359193Simpstatic int
97459193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid,
975188179Simp    u_long flags)
97659193Simp{
97774632Simp	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
97874632Simp	    rid, flags));
97959193Simp}
98059193Simp
98159193Simpstatic int
98259193Simppccard_set_memory_offset(device_t dev, device_t child, int rid,
983140692Simp    uint32_t offset, uint32_t *deltap)
98470715Sjon
98559193Simp{
98674632Simp	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
98774632Simp	    offset, deltap));
98859193Simp}
98959193Simp
990104641Simpstatic void
991104641Simppccard_probe_nomatch(device_t bus, device_t child)
992104641Simp{
993104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
994147963Simp	struct pccard_function *pf = devi->pf;
995104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
996148019Simp	int i;
997104641Simp
998104641Simp	device_printf(bus, "<unknown card>");
999148012Simp	printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
1000148012Simp	    "at function %d\n", sc->card.manufacturer, sc->card.product,
1001148012Simp	    pf->function, pf->number);
1002148012Simp	device_printf(bus, "   CIS info: ");
1003148012Simp	for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
1004148012Simp		printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
1005148012Simp	printf("\n");
1006104641Simp	return;
1007104641Simp}
1008104641Simp
100966058Simpstatic int
1010104641Simppccard_child_location_str(device_t bus, device_t child, char *buf,
1011104641Simp    size_t buflen)
1012104641Simp{
1013104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1014147963Simp	struct pccard_function *pf = devi->pf;
1015104641Simp
1016147963Simp	snprintf(buf, buflen, "function=%d", pf->number);
1017104641Simp	return (0);
1018104641Simp}
1019104641Simp
1020147963Simp/* XXX Maybe this should be in subr_bus? */
1021147963Simpstatic void
1022147963Simppccard_safe_quote(char *dst, const char *src, size_t len)
1023147963Simp{
1024147963Simp	char *walker = dst, *ep = dst + len - 1;
1025147963Simp
1026147963Simp	if (len == 0)
1027147963Simp		return;
1028158086Simp	while (src != NULL && walker < ep)
1029147963Simp	{
1030147963Simp		if (*src == '"') {
1031147963Simp			if (ep - walker < 2)
1032147963Simp				break;
1033147963Simp			*walker++ = '\\';
1034147963Simp		}
1035147963Simp		*walker++ = *src++;
1036147963Simp	}
1037147963Simp	*walker = '\0';
1038147963Simp}
1039147963Simp
1040104641Simpstatic int
1041104641Simppccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1042104641Simp    size_t buflen)
1043104641Simp{
1044104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1045147963Simp	struct pccard_function *pf = devi->pf;
1046104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
1047147963Simp	char cis0[128], cis1[128];
1048104641Simp
1049147963Simp	pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1050147963Simp	pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1051104641Simp	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1052104641Simp	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1053147963Simp	    sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1054104641Simp	return (0);
1055104641Simp}
1056104641Simp
1057104641Simpstatic int
1058188179Simppccard_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
105966058Simp{
106066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1061147963Simp	struct pccard_function *pf = devi->pf;
106266779Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
106366779Simp
1064147963Simp	if (!pf)
1065147963Simp		panic("No pccard function pointer");
106666779Simp	switch (which) {
106766779Simp	default:
1068147963Simp		return (EINVAL);
106966779Simp	case PCCARD_IVAR_ETHADDR:
1070147963Simp		bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
107166779Simp		break;
107266779Simp	case PCCARD_IVAR_VENDOR:
1073147963Simp		*(uint32_t *)result = sc->card.manufacturer;
107466779Simp		break;
107566779Simp	case PCCARD_IVAR_PRODUCT:
1076147963Simp		*(uint32_t *)result = sc->card.product;
107766779Simp		break;
107890964Sshiba	case PCCARD_IVAR_PRODEXT:
1079147963Simp		*(uint16_t *)result = sc->card.prodext;
108090964Sshiba		break;
108175761Simp	case PCCARD_IVAR_FUNCTION:
1082147963Simp		*(uint32_t *)result = pf->function;
108375761Simp		break;
108466779Simp	case PCCARD_IVAR_FUNCTION_NUMBER:
1085147963Simp		*(uint32_t *)result = pf->number;
108666779Simp		break;
108766779Simp	case PCCARD_IVAR_VENDOR_STR:
1088147963Simp		*(const char **)result = sc->card.cis1_info[0];
108966779Simp		break;
109066779Simp	case PCCARD_IVAR_PRODUCT_STR:
1091147963Simp		*(const char **)result = sc->card.cis1_info[1];
109266779Simp		break;
109366779Simp	case PCCARD_IVAR_CIS3_STR:
1094147963Simp		*(const char **)result = sc->card.cis1_info[2];
109566779Simp		break;
109667167Simp	case PCCARD_IVAR_CIS4_STR:
1097147963Simp		*(const char **)result = sc->card.cis1_info[3];
109867167Simp		break;
109966779Simp	}
110066779Simp	return (0);
110166058Simp}
110266058Simp
110366779Simpstatic void
110466779Simppccard_driver_added(device_t dev, driver_t *driver)
110566779Simp{
110682378Sjon	struct pccard_softc *sc = PCCARD_SOFTC(dev);
110782378Sjon	struct pccard_function *pf;
110882378Sjon	device_t child;
110982378Sjon
111082378Sjon	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
111182378Sjon		if (STAILQ_EMPTY(&pf->cfe_head))
111282378Sjon			continue;
111382378Sjon		child = pf->dev;
111482378Sjon		if (device_get_state(child) != DS_NOTPRESENT)
111582378Sjon			continue;
1116181342Simp		pccard_probe_and_attach_child(dev, child, pf);
111782378Sjon	}
111882378Sjon	return;
111966779Simp}
112066058Simp
112167242Simpstatic struct resource *
112267242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
112367242Simp    u_long start, u_long end, u_long count, u_int flags)
112467242Simp{
112582378Sjon	struct pccard_ivar *dinfo;
112682378Sjon	struct resource_list_entry *rle = 0;
112782378Sjon	int passthrough = (device_get_parent(child) != dev);
1128121905Simp	int isdefault = (start == 0 && end == ~0UL && count == 1);
1129104641Simp	struct resource *r = NULL;
113067242Simp
1131121905Simp	/* XXX I'm no longer sure this is right */
113282378Sjon	if (passthrough) {
113382378Sjon		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
113482378Sjon		    type, rid, start, end, count, flags));
113582378Sjon	}
113670715Sjon
113782378Sjon	dinfo = device_get_ivars(child);
113882378Sjon	rle = resource_list_find(&dinfo->resources, type, *rid);
113970715Sjon
1140121905Simp	if (rle == NULL && isdefault)
1141121905Simp		return (NULL);	/* no resource of that type/rid */
1142121905Simp	if (rle == NULL || rle->res == NULL) {
1143144955Simp		/* XXX Need to adjust flags */
1144121905Simp		r = bus_alloc_resource(dev, type, rid, start, end,
1145144955Simp		  count, flags);
1146121905Simp		if (r == NULL)
1147121905Simp		    goto bad;
1148121905Simp		resource_list_add(&dinfo->resources, type, *rid,
1149121905Simp		  rman_get_start(r), rman_get_end(r), count);
1150121905Simp		rle = resource_list_find(&dinfo->resources, type, *rid);
1151121905Simp		if (!rle)
1152121905Simp		    goto bad;
1153121905Simp		rle->res = r;
115467269Simp	}
1155121905Simp	/*
1156144955Simp	 * If dev doesn't own the device, then we can't give this device
1157144955Simp	 * out.
1158121905Simp	 */
1159113242Simp	if (rman_get_device(rle->res) != dev)
1160104641Simp		return (NULL);
1161144955Simp	rman_set_device(rle->res, child);
1162144955Simp	if (flags & RF_ACTIVE)
1163144955Simp		BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1164104641Simp	return (rle->res);
1165104641Simpbad:;
1166104641Simp	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1167104641Simp	return (NULL);
116867242Simp}
116967242Simp
117067242Simpstatic int
117167242Simppccard_release_resource(device_t dev, device_t child, int type, int rid,
117267242Simp    struct resource *r)
117367242Simp{
117482378Sjon	struct pccard_ivar *dinfo;
117582378Sjon	int passthrough = (device_get_parent(child) != dev);
117682378Sjon	struct resource_list_entry *rle = 0;
117770715Sjon
117882378Sjon	if (passthrough)
117982378Sjon		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
118082378Sjon		    type, rid, r);
118170715Sjon
118282378Sjon	dinfo = device_get_ivars(child);
118370715Sjon
118482378Sjon	rle = resource_list_find(&dinfo->resources, type, rid);
118570715Sjon
118682378Sjon	if (!rle) {
118782378Sjon		device_printf(dev, "Allocated resource not found, "
118882378Sjon		    "%d %x %lx %lx\n",
118982378Sjon		    type, rid, rman_get_start(r), rman_get_size(r));
119082378Sjon		return ENOENT;
119170715Sjon	}
119282378Sjon	if (!rle->res) {
119382378Sjon		device_printf(dev, "Allocated resource not recorded\n");
119482378Sjon		return ENOENT;
119570715Sjon	}
1196144955Simp	/*
1197144955Simp	 * Deactivate the resource (since it is being released), and
1198144955Simp	 * assign it to the bus.
1199144955Simp	 */
1200144955Simp	BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1201144955Simp	rman_set_device(rle->res, dev);
1202144955Simp	return (0);
120367242Simp}
120467242Simp
120567333Simpstatic void
120667333Simppccard_child_detached(device_t parent, device_t dev)
120767333Simp{
120867333Simp	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1209147963Simp	struct pccard_function *pf = ivar->pf;
121067333Simp
121182378Sjon	pccard_function_disable(pf);
121267333Simp}
121367333Simp
1214166901Spisostatic int
1215170163Spisopccard_filter(void *arg)
121670762Simp{
121782378Sjon	struct pccard_function *pf = (struct pccard_function*) arg;
121882378Sjon	int reg;
1219102923Simp	int doisr = 1;
122082378Sjon
122182383Simp	/*
1222102923Simp	 * MFC cards know if they interrupted, so we have to ack the
1223102923Simp	 * interrupt and call the ISR.  Non-MFC cards don't have these
1224102923Simp	 * bits, so they always get called.  Many non-MFC cards have
1225102923Simp	 * this bit set always upon read, but some do not.
1226102923Simp	 *
1227102923Simp	 * We always ack the interrupt, even if there's no ISR
1228102923Simp	 * for the card.  This is done on the theory that acking
1229102923Simp	 * the interrupt will pacify the card enough to keep an
1230102923Simp	 * interrupt storm from happening.  Of course this won't
1231102923Simp	 * help in the non-MFC case.
1232116311Simp	 *
1233116311Simp	 * This has no impact for MPSAFEness of the client drivers.
1234116311Simp	 * We register this with whatever flags the intr_handler
1235116311Simp	 * was registered with.  All these functions are MPSAFE.
123682383Simp	 */
1237102923Simp	if (pccard_mfc(pf->sc)) {
1238102923Simp		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1239102923Simp		if (reg & PCCARD_CCR_STATUS_INTR)
1240102923Simp			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1241102923Simp			    reg & ~PCCARD_CCR_STATUS_INTR);
1242102923Simp		else
1243102923Simp			doisr = 0;
1244102923Simp	}
1245166901Spiso	if (doisr) {
1246170163Spiso		if (pf->intr_filter != NULL)
1247170163Spiso			return (pf->intr_filter(pf->intr_handler_arg));
1248170849Simp		return (FILTER_SCHEDULE_THREAD);
1249166901Spiso	}
1250170163Spiso	return (FILTER_STRAY);
125170715Sjon}
125270715Sjon
1253170163Spisostatic void
1254170163Spisopccard_intr(void *arg)
1255170163Spiso{
1256170163Spiso	struct pccard_function *pf = (struct pccard_function*) arg;
1257170163Spiso
1258170163Spiso	pf->intr_handler(pf->intr_handler_arg);
1259170163Spiso}
1260170163Spiso
126170715Sjonstatic int
126270762Simppccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1263166901Spiso    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1264166901Spiso    void **cookiep)
126570715Sjon{
1266102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
126770715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1268147963Simp	struct pccard_function *pf = ivar->pf;
126990445Simp	int err;
127070715Sjon
1271170163Spiso	if (pf->intr_filter != NULL || pf->intr_handler != NULL)
1272101762Simp		panic("Only one interrupt handler per function allowed");
1273170163Spiso	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter,
1274170849Simp	    intr ? pccard_intr : NULL, pf, cookiep);
127590445Simp	if (err != 0)
127690445Simp		return (err);
1277170163Spiso	pf->intr_filter = filt;
1278147963Simp	pf->intr_handler = intr;
1279147963Simp	pf->intr_handler_arg = arg;
1280147963Simp	pf->intr_handler_cookie = *cookiep;
1281102713Simp	if (pccard_mfc(sc)) {
1282147963Simp		pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1283147963Simp		    pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1284102713Simp		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1285102713Simp	}
128674632Simp	return (0);
128770715Sjon}
128870715Sjon
128970715Sjonstatic int
129070762Simppccard_teardown_intr(device_t dev, device_t child, struct resource *r,
129170762Simp    void *cookie)
129270715Sjon{
1293102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
129470715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1295147963Simp	struct pccard_function *pf = ivar->pf;
129682378Sjon	int ret;
129770715Sjon
1298102713Simp	if (pccard_mfc(sc)) {
1299147963Simp		pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1300147963Simp		    pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1301102713Simp		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1302102713Simp	}
130390445Simp	ret = bus_generic_teardown_intr(dev, child, r, cookie);
130482378Sjon	if (ret == 0) {
1305147963Simp		pf->intr_handler = NULL;
1306147963Simp		pf->intr_handler_arg = NULL;
1307147963Simp		pf->intr_handler_cookie = NULL;
130882378Sjon	}
130970715Sjon
131082378Sjon	return (ret);
131170715Sjon}
131270715Sjon
1313121905Simpstatic int
1314121905Simppccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1315121905Simp    struct resource *r)
1316121905Simp{
1317121905Simp	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1318147963Simp	struct pccard_function *pf = ivar->pf;
1319121905Simp
1320121905Simp	switch(type) {
1321121905Simp	case SYS_RES_IOPORT:
1322121905Simp		/*
1323121905Simp		 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1324121905Simp		 * card.
1325121905Simp		 */
1326121905Simp		if (pccard_mfc(pf->sc))
1327121905Simp			pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1328121905Simp			    rman_get_size(r));
1329121905Simp		break;
1330121905Simp	default:
1331121905Simp		break;
1332121905Simp	}
1333121905Simp	return (bus_generic_activate_resource(brdev, child, type, rid, r));
1334121905Simp}
1335121905Simp
1336121905Simpstatic int
1337121905Simppccard_deactivate_resource(device_t brdev, device_t child, int type,
1338121905Simp    int rid, struct resource *r)
1339121905Simp{
1340121905Simp	/* XXX undo pccard_activate_resource? XXX */
1341121905Simp	return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1342121905Simp}
1343121905Simp
1344150098Simpstatic int
1345150098Simppccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1346150098Simp    uint8_t *val)
1347150098Simp{
1348150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1349150098Simp	struct pccard_function *pf = devi->pf;
1350150098Simp
1351150098Simp	/*
1352150098Simp	 * Optimization.  Most of the time, devices want to access
1353150098Simp	 * the same page of the attribute memory that the CCR is in.
1354150098Simp	 * We take advantage of this fact here.
1355150098Simp	 */
1356150098Simp	if (offset / PCCARD_MEM_PAGE_SIZE ==
1357150098Simp	    pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1358150098Simp		*val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1359150098Simp		    offset % PCCARD_MEM_PAGE_SIZE);
1360150098Simp	else {
1361150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1362150098Simp		    &offset);
1363150098Simp		*val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1364150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1365150098Simp		    &offset);
1366150098Simp	}
1367150098Simp	return 0;
1368150098Simp}
1369150098Simp
1370150098Simpstatic int
1371150098Simppccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1372150098Simp    uint8_t val)
1373150098Simp{
1374150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1375150098Simp	struct pccard_function *pf = devi->pf;
1376150098Simp
1377150098Simp	/*
1378150098Simp	 * Optimization.  Most of the time, devices want to access
1379150098Simp	 * the same page of the attribute memory that the CCR is in.
1380150098Simp	 * We take advantage of this fact here.
1381150098Simp	 */
1382150098Simp	if (offset / PCCARD_MEM_PAGE_SIZE ==
1383150098Simp	    pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1384150098Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1385150098Simp		    offset % PCCARD_MEM_PAGE_SIZE, val);
1386150098Simp	else {
1387150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1388150098Simp		    &offset);
1389150098Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1390150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1391150098Simp		    &offset);
1392150098Simp	}
1393150098Simp
1394150098Simp	return 0;
1395150098Simp}
1396150098Simp
1397150098Simpstatic int
1398150098Simppccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1399150098Simp    uint8_t *val)
1400150098Simp{
1401150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1402150098Simp
1403150098Simp	*val = pccard_ccr_read(devi->pf, offset);
1404150098Simp	device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset,
1405150098Simp	  devi->pf->pf_ccr_offset, *val);
1406150098Simp	return 0;
1407150098Simp}
1408150098Simp
1409150098Simpstatic int
1410150098Simppccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1411150098Simp    uint8_t val)
1412150098Simp{
1413150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1414150098Simp	struct pccard_function *pf = devi->pf;
1415150098Simp
1416150098Simp	/*
1417150098Simp	 * Can't use pccard_ccr_write since client drivers may access
1418150098Simp	 * registers not contained in the 'mask' if they are non-standard.
1419150098Simp	 */
1420150098Simp	device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1421150098Simp	  devi->pf->pf_ccr_offset);
1422150098Simp	bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1423150098Simp	    val);
1424150098Simp	return 0;
1425150098Simp}
1426150098Simp
1427150098Simp
142852506Simpstatic device_method_t pccard_methods[] = {
142952506Simp	/* Device interface */
143052506Simp	DEVMETHOD(device_probe,		pccard_probe),
143159193Simp	DEVMETHOD(device_attach,	pccard_attach),
143282378Sjon	DEVMETHOD(device_detach,	pccard_detach),
143352506Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
143487975Simp	DEVMETHOD(device_suspend,	pccard_suspend),
143587975Simp	DEVMETHOD(device_resume,	pccard_resume),
143652506Simp
143752506Simp	/* Bus interface */
143852506Simp	DEVMETHOD(bus_print_child,	pccard_print_child),
143966779Simp	DEVMETHOD(bus_driver_added,	pccard_driver_added),
144067333Simp	DEVMETHOD(bus_child_detached,	pccard_child_detached),
144167242Simp	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
144267242Simp	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1443121905Simp	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1444121905Simp	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
144570715Sjon	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
144670715Sjon	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
144752506Simp	DEVMETHOD(bus_set_resource,	pccard_set_resource),
144852506Simp	DEVMETHOD(bus_get_resource,	pccard_get_resource),
144952506Simp	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1450104641Simp	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
145166058Simp	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1452104641Simp	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1453104641Simp	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
145452506Simp
145559193Simp	/* Card Interface */
145659193Simp	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
145759193Simp	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
145859193Simp	DEVMETHOD(card_attach_card,	pccard_attach_card),
145959193Simp	DEVMETHOD(card_detach_card,	pccard_detach_card),
1460150098Simp	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1461147711Simp	DEVMETHOD(card_cis_scan,	pccard_scan_cis),
1462150098Simp	DEVMETHOD(card_attr_read,	pccard_attr_read_impl),
1463150098Simp	DEVMETHOD(card_attr_write,	pccard_attr_write_impl),
1464150098Simp	DEVMETHOD(card_ccr_read,	pccard_ccr_read_impl),
1465150098Simp	DEVMETHOD(card_ccr_write,	pccard_ccr_write_impl),
146659193Simp
146752506Simp	{ 0, 0 }
146852506Simp};
146952506Simp
147052506Simpstatic driver_t pccard_driver = {
147152506Simp	"pccard",
147252506Simp	pccard_methods,
147364850Simp	sizeof(struct pccard_softc)
147452506Simp};
147552506Simp
147652506Simpdevclass_t	pccard_devclass;
147752506Simp
1478101905Simp/* Maybe we need to have a slot device? */
147953873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1480101905SimpDRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
148164927SimpMODULE_VERSION(pccard, 1);
1482