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$");
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 */
62227309Sedstatic SYSCTL_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);
176188212Swkoszek		snprintf(str, 100, "vendor=%#x product=%#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) {
300188212Swkoszek		DEVPRINTF((sc->dev, "function %d CCR at %d offset %#x "
301188212Swkoszek		    "mask %#x: %#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;
476237692Simp	struct pccard_ce_iospace *ios;
477237692Simp	struct pccard_ce_memspace *mems;
47867242Simp	device_t bus;
479144955Simp	u_long start, end, len;
480144955Simp	int i, rid, spaces;
48165917Simp
48270715Sjon	if (pf->pf_flags & PFF_ENABLED) {
48370715Sjon		printf("pccard_function_init: function is enabled");
48470715Sjon		return;
48570715Sjon	}
486181342Simp
487181342Simp	/*
488181342Simp	 * Driver probe routine requested a specific entry already
489181342Simp	 * that succeeded.
490181342Simp	 */
491181342Simp	if (pf->cfe != NULL)
492181342Simp		return;
493181342Simp
494181342Simp	/*
495181342Simp	 * walk the list of configuration entries until we find one that
496181342Simp	 * we can allocate all the resources to.
497181342Simp	 */
49867242Simp	bus = device_get_parent(pf->dev);
49972012Sphk	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
500144930Simp		if (cfe->iftype != PCCARD_IFTYPE_IO)
501144930Simp			continue;
502181342Simp		if (entry != -1 && cfe->number != entry)
503181342Simp			continue;
50490897Simp		spaces = 0;
50567187Simp		for (i = 0; i < cfe->num_iospace; i++) {
506237692Simp			ios = cfe->iospace + i;
507237692Simp			start = ios->start;
50867424Simp			if (start)
509237692Simp				end = start + ios->length - 1;
51067424Simp			else
511144930Simp				end = ~0UL;
512188212Swkoszek			DEVPRINTF((bus, "I/O rid %d start %#lx end %#lx\n",
51390897Simp			    i, start, end));
514144930Simp			rid = i;
515237692Simp			len = ios->length;
516144955Simp			r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
517144955Simp			    start, end, len, rman_make_alignment_flags(len));
518237692Simp			if (r == NULL) {
519237692Simp				DEVPRINTF((bus, "I/O rid %d failed\n", i));
52067187Simp				goto not_this_one;
521237692Simp			}
522144927Simp			rle = resource_list_add(rl, SYS_RES_IOPORT,
523237692Simp			    rid, rman_get_start(r), rman_get_end(r), len);
524144927Simp			if (rle == NULL)
525144930Simp				panic("Cannot add resource rid %d IOPORT", rid);
52676424Simp			rle->res = r;
52790897Simp			spaces++;
52867187Simp		}
529144930Simp		for (i = 0; i < cfe->num_memspace; i++) {
530237692Simp			mems = cfe->memspace + i;
531237692Simp			start = mems->cardaddr + mems->hostaddr;
532144930Simp			if (start)
533237692Simp				end = start + mems->length - 1;
534144930Simp			else
535144930Simp				end = ~0UL;
536237692Simp			DEVPRINTF((bus, "Memory rid %d start %#lx end %#lx\ncardaddr %#lx hostaddr %#lx length %#lx\n",
537237692Simp			    i, start, end, mems->cardaddr, mems->hostaddr,
538237692Simp			    mems->length));
539144930Simp			rid = i;
540237692Simp			len = mems->length;
541144955Simp			r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
542144955Simp			    start, end, len, rman_make_alignment_flags(len));
543237692Simp			if (r == NULL) {
544237692Simp				DEVPRINTF((bus, "Memory rid %d failed\n", i));
545237692Simp//				goto not_this_one;
546237692Simp				continue;
547237692Simp			}
548144930Simp			rle = resource_list_add(rl, SYS_RES_MEMORY,
549237692Simp			    rid, rman_get_start(r), rman_get_end(r), len);
550144930Simp			if (rle == NULL)
551144930Simp				panic("Cannot add resource rid %d MEM", rid);
552144930Simp			rle->res = r;
553144930Simp			spaces++;
55467187Simp		}
55590897Simp		if (spaces == 0) {
556140488Simp			DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
55790897Simp			goto not_this_one;
55890897Simp		}
55967187Simp		if (cfe->irqmask) {
560144930Simp			rid = 0;
561144955Simp			r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
562144955Simp			    RF_SHAREABLE);
563237692Simp			if (r == NULL) {
564237692Simp				DEVPRINTF((bus, "IRQ rid %d failed\n", rid));
56567187Simp				goto not_this_one;
566237692Simp			}
567144930Simp			rle = resource_list_add(rl, SYS_RES_IRQ, rid,
56867242Simp			    rman_get_start(r), rman_get_end(r), 1);
569144927Simp			if (rle == NULL)
570144930Simp				panic("Cannot add resource rid %d IRQ", rid);
57176424Simp			rle->res = r;
57267187Simp		}
57367187Simp		/* If we get to here, we've allocated all we need */
57467167Simp		pf->cfe = cfe;
57567187Simp		break;
57667187Simp	    not_this_one:;
57767424Simp		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
57867424Simp		    cfe->number));
579144955Simp		resource_list_purge(rl);
58067167Simp	}
58152506Simp}
58252506Simp
58382378Sjon/*
58482378Sjon * Free resources allocated by pccard_function_init(), May be called as long
58582378Sjon * as the function is disabled.
58682382Simp *
58782382Simp * NOTE: This function should be unnecessary.  pccard_function_init should
58882382Simp * never keep resources initialized.
58982378Sjon */
59082378Sjonstatic void
59182378Sjonpccard_function_free(struct pccard_function *pf)
59282378Sjon{
59382378Sjon	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
59482378Sjon	struct resource_list_entry *rle;
59582378Sjon
59682378Sjon	if (pf->pf_flags & PFF_ENABLED) {
597181342Simp		printf("pccard_function_free: function is enabled");
59882378Sjon		return;
59982378Sjon	}
60082378Sjon
601143785Simp	STAILQ_FOREACH(rle, &devi->resources, link) {
60282378Sjon		if (rle->res) {
603113242Simp			if (rman_get_device(rle->res) != pf->sc->dev)
60482378Sjon				device_printf(pf->sc->dev,
60582378Sjon				    "function_free: Resource still owned by "
60682378Sjon				    "child, oops. "
607188212Swkoszek				    "(type=%d, rid=%d, addr=%#lx)\n",
60882378Sjon				    rle->type, rle->rid,
60982378Sjon				    rman_get_start(rle->res));
61082378Sjon			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
611113242Simp			    pf->sc->dev, rle->type, rle->rid, rle->res);
61282378Sjon			rle->res = NULL;
61382378Sjon		}
61482378Sjon	}
61582378Sjon	resource_list_free(&devi->resources);
61682378Sjon}
61782378Sjon
618121905Simpstatic void
619121905Simppccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
620121905Simp    bus_addr_t offset, bus_size_t size)
621121905Simp{
622121958Simp	bus_size_t iosize, tmp;
623121905Simp
624121905Simp	if (addr != 0) {
625121905Simp		if (pf->pf_mfc_iomax == 0) {
626121905Simp			pf->pf_mfc_iobase = addr + offset;
627121905Simp			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
628121905Simp		} else {
629121905Simp			/* this makes the assumption that nothing overlaps */
630121905Simp			if (pf->pf_mfc_iobase > addr + offset)
631121905Simp				pf->pf_mfc_iobase = addr + offset;
632121905Simp			if (pf->pf_mfc_iomax < addr + offset + size)
633121905Simp				pf->pf_mfc_iomax = addr + offset + size;
634121905Simp		}
635121905Simp	}
636121905Simp
637121905Simp	tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
638121905Simp	/* round up to nearest (2^n)-1 */
639121905Simp	for (iosize = 1; iosize < tmp; iosize <<= 1)
640121905Simp		;
641121905Simp	iosize--;
642121905Simp
643122032Simp	DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
644122032Simp	    (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
645121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
646121905Simp	    pf->pf_mfc_iobase & 0xff);
647121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
648121905Simp	    (pf->pf_mfc_iobase >> 8) & 0xff);
649121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
650121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
651121905Simp	pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
652121905Simp}
653121905Simp
65452506Simp/* Enable a PCCARD function */
65582378Sjonstatic int
65655720Simppccard_function_enable(struct pccard_function *pf)
65752506Simp{
65852506Simp	struct pccard_function *tmp;
65952506Simp	int reg;
66055720Simp	device_t dev = pf->sc->dev;
66170746Simp
66267333Simp	if (pf->cfe == NULL) {
66367333Simp		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
66474632Simp		return (ENOMEM);
66567333Simp	}
66652506Simp
667181392Simp	if (pf->pf_flags & PFF_ENABLED)
668181392Simp		return (0);
66982378Sjon	pf->sc->sc_enabled_count++;
67052506Simp
67152506Simp	/*
67252506Simp	 * it's possible for different functions' CCRs to be in the same
67352506Simp	 * underlying page.  Check for that.
67452506Simp	 */
67552506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
67652506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
67752506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
67852506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
67982378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
68082378Sjon		    tmp->pf_ccr_realsize))) {
68152506Simp			pf->pf_ccrt = tmp->pf_ccrt;
68252506Simp			pf->pf_ccrh = tmp->pf_ccrh;
68352506Simp			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
68452506Simp
68552506Simp			/*
68652506Simp			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
68752506Simp			 * tmp->ccr_base) + pf->ccr_base;
68852506Simp			 */
68970715Sjon			/* pf->pf_ccr_offset =
69052506Simp			    (tmp->pf_ccr_offset + pf->ccr_base) -
69170715Sjon			    tmp->ccr_base; */
69252506Simp			pf->pf_ccr_window = tmp->pf_ccr_window;
69352506Simp			break;
69452506Simp		}
69552506Simp	}
69652506Simp	if (tmp == NULL) {
69755720Simp		pf->ccr_rid = 0;
69855720Simp		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
699150097Simp		    &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
70070715Sjon		if (!pf->ccr_res)
70152506Simp			goto bad;
702188212Swkoszek		DEVPRINTF((dev, "ccr_res == %#lx-%#lx, base=%#x\n",
70370746Simp		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
70470746Simp		    pf->ccr_base));
70561788Simp		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
70661788Simp		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
70770715Sjon		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
70870748Simp		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
70955720Simp		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
71055720Simp		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
71155720Simp		pf->pf_ccr_realsize = 1;
71252506Simp	}
71352506Simp
71452506Simp	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
71552506Simp	reg |= PCCARD_CCR_OPTION_LEVIREQ;
71652506Simp	if (pccard_mfc(pf->sc)) {
71752506Simp		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
71852506Simp			PCCARD_CCR_OPTION_ADDR_DECODE);
71982383Simp		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
72052506Simp	}
72152506Simp	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
72252506Simp
72352506Simp	reg = 0;
72452506Simp	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
72552506Simp		reg |= PCCARD_CCR_STATUS_IOIS8;
72652506Simp	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
72752506Simp		reg |= PCCARD_CCR_STATUS_AUDIO;
72852506Simp	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
72952506Simp
73052506Simp	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
73152506Simp
732121905Simp	if (pccard_mfc(pf->sc))
733121905Simp		pccard_mfc_adjust_iobase(pf, 0, 0, 0);
73452506Simp
73552506Simp#ifdef PCCARDDEBUG
73652506Simp	if (pccard_debug) {
73752506Simp		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
73870715Sjon			device_printf(tmp->sc->dev,
739188212Swkoszek			    "function %d CCR at %d offset %#x: "
740188212Swkoszek			    "%#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n",
74170715Sjon			    tmp->number, tmp->pf_ccr_window,
74255500Simp			    tmp->pf_ccr_offset,
74355500Simp			    pccard_ccr_read(tmp, 0x00),
74455500Simp			    pccard_ccr_read(tmp, 0x02),
74555500Simp			    pccard_ccr_read(tmp, 0x04),
74655500Simp			    pccard_ccr_read(tmp, 0x06),
74755500Simp			    pccard_ccr_read(tmp, 0x0A),
74870715Sjon			    pccard_ccr_read(tmp, 0x0C),
74955500Simp			    pccard_ccr_read(tmp, 0x0E),
75055500Simp			    pccard_ccr_read(tmp, 0x10),
75155500Simp			    pccard_ccr_read(tmp, 0x12));
75252506Simp		}
75352506Simp	}
75452506Simp#endif
75552506Simp	pf->pf_flags |= PFF_ENABLED;
75652506Simp	return (0);
75752506Simp
75852506Simp bad:
75952506Simp	/*
76052506Simp	 * Decrement the reference count, and power down the socket, if
76152506Simp	 * necessary.
76252506Simp	 */
76382378Sjon	pf->sc->sc_enabled_count--;
76465098Simp	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
76552506Simp
76652506Simp	return (1);
76752506Simp}
76852506Simp
76952506Simp/* Disable PCCARD function. */
77082378Sjonstatic void
77155720Simppccard_function_disable(struct pccard_function *pf)
77252506Simp{
77352506Simp	struct pccard_function *tmp;
77455720Simp	device_t dev = pf->sc->dev;
77552506Simp
77652506Simp	if (pf->cfe == NULL)
77761788Simp		panic("pccard_function_disable: function not initialized");
77852506Simp
779181392Simp	if ((pf->pf_flags & PFF_ENABLED) == 0)
78052506Simp		return;
78170715Sjon	if (pf->intr_handler != NULL) {
78282378Sjon		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
78382378Sjon		struct resource_list_entry *rle =
78482378Sjon		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
785144927Simp		if (rle == NULL)
786144927Simp			panic("Can't disable an interrupt with no IRQ res\n");
78782378Sjon		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
78882378Sjon		    pf->intr_handler_cookie);
78970715Sjon	}
79070715Sjon
79152506Simp	/*
79252506Simp	 * it's possible for different functions' CCRs to be in the same
79352506Simp	 * underlying page.  Check for that.  Note we mark us as disabled
79452506Simp	 * first to avoid matching ourself.
79552506Simp	 */
79652506Simp
79752506Simp	pf->pf_flags &= ~PFF_ENABLED;
79852506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
79952506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
80052506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
80152506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
80282378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
80382378Sjon		    tmp->pf_ccr_realsize)))
80452506Simp			break;
80552506Simp	}
80652506Simp
80752506Simp	/* Not used by anyone else; unmap the CCR. */
80852506Simp	if (tmp == NULL) {
80970715Sjon		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
81055720Simp		    pf->ccr_res);
81155720Simp		pf->ccr_res = NULL;
81252506Simp	}
81352506Simp
81452506Simp	/*
81552506Simp	 * Decrement the reference count, and power down the socket, if
81652506Simp	 * necessary.
81752506Simp	 */
81882378Sjon	pf->sc->sc_enabled_count--;
81952506Simp}
82052506Simp
82153873Simp#define PCCARD_NPORT	2
82253873Simp#define PCCARD_NMEM	5
82353873Simp#define PCCARD_NIRQ	1
82453873Simp#define PCCARD_NDRQ	0
82553873Simp
82652506Simpstatic int
82752506Simppccard_probe(device_t dev)
82852506Simp{
82967333Simp	device_set_desc(dev, "16-bit PCCard bus");
830150391Simp	return (0);
83152506Simp}
83252506Simp
83359193Simpstatic int
83459193Simppccard_attach(device_t dev)
83559193Simp{
83664850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
837150362Simp	int err;
83861788Simp
83959193Simp	sc->dev = dev;
84061788Simp	sc->sc_enabled_count = 0;
841150362Simp	if ((err = pccard_device_create(sc)) != 0)
842150362Simp		return  (err);
843153773Simp	STAILQ_INIT(&sc->card.pf_head);
84474632Simp	return (bus_generic_attach(dev));
84559193Simp}
84659193Simp
84782378Sjonstatic int
84882378Sjonpccard_detach(device_t dev)
84982378Sjon{
850106362Simp	pccard_detach_card(dev);
851150362Simp	pccard_device_destroy(device_get_softc(dev));
852150362Simp	return (0);
85382378Sjon}
85482378Sjon
85587975Simpstatic int
85687975Simppccard_suspend(device_t self)
85787975Simp{
858106362Simp	pccard_detach_card(self);
85987975Simp	return (0);
86087975Simp}
86187975Simp
86287975Simpstatic
86387975Simpint
86487975Simppccard_resume(device_t self)
86587975Simp{
86687975Simp	return (0);
86787975Simp}
86887975Simp
86953873Simpstatic void
87053873Simppccard_print_resources(struct resource_list *rl, const char *name, int type,
87153873Simp    int count, const char *format)
87253873Simp{
87353873Simp	struct resource_list_entry *rle;
87453873Simp	int printed;
87553873Simp	int i;
87653873Simp
87753873Simp	printed = 0;
87853873Simp	for (i = 0; i < count; i++) {
87953873Simp		rle = resource_list_find(rl, type, i);
88076424Simp		if (rle != NULL) {
88153873Simp			if (printed == 0)
88253873Simp				printf(" %s ", name);
88353873Simp			else if (printed > 0)
88453873Simp				printf(",");
88553873Simp			printed++;
88653873Simp			printf(format, rle->start);
88753873Simp			if (rle->count > 1) {
88853873Simp				printf("-");
88953873Simp				printf(format, rle->start + rle->count - 1);
89053873Simp			}
89153873Simp		} else if (i > 3) {
89253873Simp			/* check the first few regardless */
89353873Simp			break;
89453873Simp		}
89553873Simp	}
89653873Simp}
89753873Simp
89853873Simpstatic int
89953873Simppccard_print_child(device_t dev, device_t child)
90053873Simp{
90166847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
90253873Simp	struct resource_list *rl = &devi->resources;
90353873Simp	int retval = 0;
90453873Simp
90553873Simp	retval += bus_print_child_header(dev, child);
90653873Simp	retval += printf(" at");
90753873Simp
90876424Simp	if (devi != NULL) {
90953873Simp		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
91053873Simp		    PCCARD_NPORT, "%#lx");
91153873Simp		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
91253873Simp		    PCCARD_NMEM, "%#lx");
91353873Simp		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
91453873Simp		    "%ld");
91570715Sjon		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
91653873Simp		    "%ld");
917147963Simp		retval += printf(" function %d config %d", devi->pf->number,
918147963Simp		    devi->pf->cfe->number);
91953873Simp	}
92053873Simp
92153873Simp	retval += bus_print_child_footer(dev, child);
92253873Simp
92353873Simp	return (retval);
92453873Simp}
92553873Simp
92653873Simpstatic int
92753873Simppccard_set_resource(device_t dev, device_t child, int type, int rid,
928147963Simp    u_long start, u_long count)
92953873Simp{
93066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
93153873Simp	struct resource_list *rl = &devi->resources;
93253873Simp
93353873Simp	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
93453873Simp	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
93574632Simp		return (EINVAL);
93653873Simp	if (rid < 0)
93774632Simp		return (EINVAL);
93853873Simp	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
93974632Simp		return (EINVAL);
94053873Simp	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
94174632Simp		return (EINVAL);
94253873Simp	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
94374632Simp		return (EINVAL);
94453873Simp	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
94574632Simp		return (EINVAL);
94653873Simp
94753873Simp	resource_list_add(rl, type, rid, start, start + count - 1, count);
94882378Sjon	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
94982378Sjon	    type, &rid, start, start + count - 1, count, 0))
95082378Sjon		return 0;
95182378Sjon	else
95282378Sjon		return ENOMEM;
95353873Simp}
95453873Simp
95553873Simpstatic int
95653873Simppccard_get_resource(device_t dev, device_t child, int type, int rid,
95753873Simp    u_long *startp, u_long *countp)
95853873Simp{
95966847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
96053873Simp	struct resource_list *rl = &devi->resources;
96153873Simp	struct resource_list_entry *rle;
96253873Simp
96353873Simp	rle = resource_list_find(rl, type, rid);
96476424Simp	if (rle == NULL)
96574632Simp		return (ENOENT);
96670715Sjon
96776424Simp	if (startp != NULL)
96853873Simp		*startp = rle->start;
96976424Simp	if (countp != NULL)
97053873Simp		*countp = rle->count;
97153873Simp
97274632Simp	return (0);
97353873Simp}
97453873Simp
97553873Simpstatic void
97653873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid)
97753873Simp{
97866847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
97953873Simp	struct resource_list *rl = &devi->resources;
98053873Simp	resource_list_delete(rl, type, rid);
98153873Simp}
98253873Simp
98359193Simpstatic int
98459193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid,
985188179Simp    u_long flags)
98659193Simp{
98774632Simp	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
98874632Simp	    rid, flags));
98959193Simp}
99059193Simp
99159193Simpstatic int
99259193Simppccard_set_memory_offset(device_t dev, device_t child, int rid,
993140692Simp    uint32_t offset, uint32_t *deltap)
99470715Sjon
99559193Simp{
99674632Simp	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
99774632Simp	    offset, deltap));
99859193Simp}
99959193Simp
1000104641Simpstatic void
1001104641Simppccard_probe_nomatch(device_t bus, device_t child)
1002104641Simp{
1003104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1004147963Simp	struct pccard_function *pf = devi->pf;
1005104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
1006148019Simp	int i;
1007104641Simp
1008104641Simp	device_printf(bus, "<unknown card>");
1009148012Simp	printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
1010148012Simp	    "at function %d\n", sc->card.manufacturer, sc->card.product,
1011148012Simp	    pf->function, pf->number);
1012148012Simp	device_printf(bus, "   CIS info: ");
1013148012Simp	for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
1014148012Simp		printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
1015148012Simp	printf("\n");
1016104641Simp	return;
1017104641Simp}
1018104641Simp
101966058Simpstatic int
1020104641Simppccard_child_location_str(device_t bus, device_t child, char *buf,
1021104641Simp    size_t buflen)
1022104641Simp{
1023104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1024147963Simp	struct pccard_function *pf = devi->pf;
1025104641Simp
1026147963Simp	snprintf(buf, buflen, "function=%d", pf->number);
1027104641Simp	return (0);
1028104641Simp}
1029104641Simp
1030147963Simp/* XXX Maybe this should be in subr_bus? */
1031147963Simpstatic void
1032147963Simppccard_safe_quote(char *dst, const char *src, size_t len)
1033147963Simp{
1034147963Simp	char *walker = dst, *ep = dst + len - 1;
1035147963Simp
1036147963Simp	if (len == 0)
1037147963Simp		return;
1038158086Simp	while (src != NULL && walker < ep)
1039147963Simp	{
1040147963Simp		if (*src == '"') {
1041147963Simp			if (ep - walker < 2)
1042147963Simp				break;
1043147963Simp			*walker++ = '\\';
1044147963Simp		}
1045147963Simp		*walker++ = *src++;
1046147963Simp	}
1047147963Simp	*walker = '\0';
1048147963Simp}
1049147963Simp
1050104641Simpstatic int
1051104641Simppccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1052104641Simp    size_t buflen)
1053104641Simp{
1054104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1055147963Simp	struct pccard_function *pf = devi->pf;
1056104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
1057147963Simp	char cis0[128], cis1[128];
1058104641Simp
1059147963Simp	pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1060147963Simp	pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1061104641Simp	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1062104641Simp	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1063147963Simp	    sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1064104641Simp	return (0);
1065104641Simp}
1066104641Simp
1067104641Simpstatic int
1068188179Simppccard_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
106966058Simp{
107066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1071147963Simp	struct pccard_function *pf = devi->pf;
107266779Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
107366779Simp
1074147963Simp	if (!pf)
1075147963Simp		panic("No pccard function pointer");
107666779Simp	switch (which) {
107766779Simp	default:
1078147963Simp		return (EINVAL);
1079189318Simp	case PCCARD_IVAR_FUNCE_DISK:
1080189318Simp		*(uint16_t *)result = pf->pf_funce_disk_interface |
1081189318Simp		    (pf->pf_funce_disk_power << 8);
1082189318Simp		break;
108366779Simp	case PCCARD_IVAR_ETHADDR:
1084147963Simp		bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
108566779Simp		break;
108666779Simp	case PCCARD_IVAR_VENDOR:
1087147963Simp		*(uint32_t *)result = sc->card.manufacturer;
108866779Simp		break;
108966779Simp	case PCCARD_IVAR_PRODUCT:
1090147963Simp		*(uint32_t *)result = sc->card.product;
109166779Simp		break;
109290964Sshiba	case PCCARD_IVAR_PRODEXT:
1093147963Simp		*(uint16_t *)result = sc->card.prodext;
109490964Sshiba		break;
109575761Simp	case PCCARD_IVAR_FUNCTION:
1096147963Simp		*(uint32_t *)result = pf->function;
109775761Simp		break;
109866779Simp	case PCCARD_IVAR_FUNCTION_NUMBER:
1099147963Simp		*(uint32_t *)result = pf->number;
110066779Simp		break;
110166779Simp	case PCCARD_IVAR_VENDOR_STR:
1102147963Simp		*(const char **)result = sc->card.cis1_info[0];
110366779Simp		break;
110466779Simp	case PCCARD_IVAR_PRODUCT_STR:
1105147963Simp		*(const char **)result = sc->card.cis1_info[1];
110666779Simp		break;
110766779Simp	case PCCARD_IVAR_CIS3_STR:
1108147963Simp		*(const char **)result = sc->card.cis1_info[2];
110966779Simp		break;
111067167Simp	case PCCARD_IVAR_CIS4_STR:
1111147963Simp		*(const char **)result = sc->card.cis1_info[3];
111267167Simp		break;
111366779Simp	}
111466779Simp	return (0);
111566058Simp}
111666058Simp
111766779Simpstatic void
111866779Simppccard_driver_added(device_t dev, driver_t *driver)
111966779Simp{
112082378Sjon	struct pccard_softc *sc = PCCARD_SOFTC(dev);
112182378Sjon	struct pccard_function *pf;
112282378Sjon	device_t child;
112382378Sjon
112482378Sjon	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
112582378Sjon		if (STAILQ_EMPTY(&pf->cfe_head))
112682378Sjon			continue;
112782378Sjon		child = pf->dev;
112882378Sjon		if (device_get_state(child) != DS_NOTPRESENT)
112982378Sjon			continue;
1130181342Simp		pccard_probe_and_attach_child(dev, child, pf);
113182378Sjon	}
113282378Sjon	return;
113366779Simp}
113466058Simp
113567242Simpstatic struct resource *
113667242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
113767242Simp    u_long start, u_long end, u_long count, u_int flags)
113867242Simp{
113982378Sjon	struct pccard_ivar *dinfo;
114082378Sjon	struct resource_list_entry *rle = 0;
114182378Sjon	int passthrough = (device_get_parent(child) != dev);
1142121905Simp	int isdefault = (start == 0 && end == ~0UL && count == 1);
1143104641Simp	struct resource *r = NULL;
114467242Simp
1145121905Simp	/* XXX I'm no longer sure this is right */
114682378Sjon	if (passthrough) {
114782378Sjon		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
114882378Sjon		    type, rid, start, end, count, flags));
114982378Sjon	}
115070715Sjon
115182378Sjon	dinfo = device_get_ivars(child);
115282378Sjon	rle = resource_list_find(&dinfo->resources, type, *rid);
115370715Sjon
1154121905Simp	if (rle == NULL && isdefault)
1155121905Simp		return (NULL);	/* no resource of that type/rid */
1156121905Simp	if (rle == NULL || rle->res == NULL) {
1157144955Simp		/* XXX Need to adjust flags */
1158121905Simp		r = bus_alloc_resource(dev, type, rid, start, end,
1159144955Simp		  count, flags);
1160121905Simp		if (r == NULL)
1161121905Simp		    goto bad;
1162121905Simp		resource_list_add(&dinfo->resources, type, *rid,
1163121905Simp		  rman_get_start(r), rman_get_end(r), count);
1164121905Simp		rle = resource_list_find(&dinfo->resources, type, *rid);
1165121905Simp		if (!rle)
1166121905Simp		    goto bad;
1167121905Simp		rle->res = r;
116867269Simp	}
1169121905Simp	/*
1170144955Simp	 * If dev doesn't own the device, then we can't give this device
1171144955Simp	 * out.
1172121905Simp	 */
1173113242Simp	if (rman_get_device(rle->res) != dev)
1174104641Simp		return (NULL);
1175144955Simp	rman_set_device(rle->res, child);
1176144955Simp	if (flags & RF_ACTIVE)
1177144955Simp		BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1178104641Simp	return (rle->res);
1179104641Simpbad:;
1180104641Simp	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1181104641Simp	return (NULL);
118267242Simp}
118367242Simp
118467242Simpstatic int
118567242Simppccard_release_resource(device_t dev, device_t child, int type, int rid,
118667242Simp    struct resource *r)
118767242Simp{
118882378Sjon	struct pccard_ivar *dinfo;
118982378Sjon	int passthrough = (device_get_parent(child) != dev);
119082378Sjon	struct resource_list_entry *rle = 0;
119170715Sjon
119282378Sjon	if (passthrough)
119382378Sjon		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
119482378Sjon		    type, rid, r);
119570715Sjon
119682378Sjon	dinfo = device_get_ivars(child);
119770715Sjon
119882378Sjon	rle = resource_list_find(&dinfo->resources, type, rid);
119970715Sjon
120082378Sjon	if (!rle) {
120182378Sjon		device_printf(dev, "Allocated resource not found, "
1202188212Swkoszek		    "%d %#x %#lx %#lx\n",
120382378Sjon		    type, rid, rman_get_start(r), rman_get_size(r));
120482378Sjon		return ENOENT;
120570715Sjon	}
120682378Sjon	if (!rle->res) {
120782378Sjon		device_printf(dev, "Allocated resource not recorded\n");
120882378Sjon		return ENOENT;
120970715Sjon	}
1210144955Simp	/*
1211144955Simp	 * Deactivate the resource (since it is being released), and
1212144955Simp	 * assign it to the bus.
1213144955Simp	 */
1214144955Simp	BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1215144955Simp	rman_set_device(rle->res, dev);
1216144955Simp	return (0);
121767242Simp}
121867242Simp
121967333Simpstatic void
122067333Simppccard_child_detached(device_t parent, device_t dev)
122167333Simp{
122267333Simp	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1223147963Simp	struct pccard_function *pf = ivar->pf;
122467333Simp
122582378Sjon	pccard_function_disable(pf);
122667333Simp}
122767333Simp
1228166901Spisostatic int
1229170163Spisopccard_filter(void *arg)
123070762Simp{
123182378Sjon	struct pccard_function *pf = (struct pccard_function*) arg;
123282378Sjon	int reg;
1233102923Simp	int doisr = 1;
123482378Sjon
123582383Simp	/*
1236102923Simp	 * MFC cards know if they interrupted, so we have to ack the
1237102923Simp	 * interrupt and call the ISR.  Non-MFC cards don't have these
1238102923Simp	 * bits, so they always get called.  Many non-MFC cards have
1239102923Simp	 * this bit set always upon read, but some do not.
1240102923Simp	 *
1241102923Simp	 * We always ack the interrupt, even if there's no ISR
1242102923Simp	 * for the card.  This is done on the theory that acking
1243102923Simp	 * the interrupt will pacify the card enough to keep an
1244102923Simp	 * interrupt storm from happening.  Of course this won't
1245102923Simp	 * help in the non-MFC case.
1246116311Simp	 *
1247116311Simp	 * This has no impact for MPSAFEness of the client drivers.
1248116311Simp	 * We register this with whatever flags the intr_handler
1249116311Simp	 * was registered with.  All these functions are MPSAFE.
125082383Simp	 */
1251102923Simp	if (pccard_mfc(pf->sc)) {
1252102923Simp		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1253102923Simp		if (reg & PCCARD_CCR_STATUS_INTR)
1254102923Simp			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1255102923Simp			    reg & ~PCCARD_CCR_STATUS_INTR);
1256102923Simp		else
1257102923Simp			doisr = 0;
1258102923Simp	}
1259166901Spiso	if (doisr) {
1260170163Spiso		if (pf->intr_filter != NULL)
1261170163Spiso			return (pf->intr_filter(pf->intr_handler_arg));
1262170849Simp		return (FILTER_SCHEDULE_THREAD);
1263166901Spiso	}
1264170163Spiso	return (FILTER_STRAY);
126570715Sjon}
126670715Sjon
1267170163Spisostatic void
1268170163Spisopccard_intr(void *arg)
1269170163Spiso{
1270170163Spiso	struct pccard_function *pf = (struct pccard_function*) arg;
1271170163Spiso
1272170163Spiso	pf->intr_handler(pf->intr_handler_arg);
1273170163Spiso}
1274170163Spiso
127570715Sjonstatic int
127670762Simppccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1277166901Spiso    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1278166901Spiso    void **cookiep)
127970715Sjon{
1280102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
128170715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1282147963Simp	struct pccard_function *pf = ivar->pf;
128390445Simp	int err;
128470715Sjon
1285170163Spiso	if (pf->intr_filter != NULL || pf->intr_handler != NULL)
1286101762Simp		panic("Only one interrupt handler per function allowed");
1287170163Spiso	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter,
1288170849Simp	    intr ? pccard_intr : NULL, pf, cookiep);
128990445Simp	if (err != 0)
129090445Simp		return (err);
1291170163Spiso	pf->intr_filter = filt;
1292147963Simp	pf->intr_handler = intr;
1293147963Simp	pf->intr_handler_arg = arg;
1294147963Simp	pf->intr_handler_cookie = *cookiep;
1295102713Simp	if (pccard_mfc(sc)) {
1296147963Simp		pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1297147963Simp		    pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1298102713Simp		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1299102713Simp	}
130074632Simp	return (0);
130170715Sjon}
130270715Sjon
130370715Sjonstatic int
130470762Simppccard_teardown_intr(device_t dev, device_t child, struct resource *r,
130570762Simp    void *cookie)
130670715Sjon{
1307102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
130870715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1309147963Simp	struct pccard_function *pf = ivar->pf;
131082378Sjon	int ret;
131170715Sjon
1312102713Simp	if (pccard_mfc(sc)) {
1313147963Simp		pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1314147963Simp		    pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1315102713Simp		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1316102713Simp	}
131790445Simp	ret = bus_generic_teardown_intr(dev, child, r, cookie);
131882378Sjon	if (ret == 0) {
1319147963Simp		pf->intr_handler = NULL;
1320147963Simp		pf->intr_handler_arg = NULL;
1321147963Simp		pf->intr_handler_cookie = NULL;
132282378Sjon	}
132370715Sjon
132482378Sjon	return (ret);
132570715Sjon}
132670715Sjon
1327121905Simpstatic int
1328121905Simppccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1329121905Simp    struct resource *r)
1330121905Simp{
1331121905Simp	struct pccard_ivar *ivar = PCCARD_IVAR(child);
1332147963Simp	struct pccard_function *pf = ivar->pf;
1333121905Simp
1334121905Simp	switch(type) {
1335121905Simp	case SYS_RES_IOPORT:
1336121905Simp		/*
1337121905Simp		 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1338121905Simp		 * card.
1339121905Simp		 */
1340121905Simp		if (pccard_mfc(pf->sc))
1341121905Simp			pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1342121905Simp			    rman_get_size(r));
1343121905Simp		break;
1344121905Simp	default:
1345121905Simp		break;
1346121905Simp	}
1347121905Simp	return (bus_generic_activate_resource(brdev, child, type, rid, r));
1348121905Simp}
1349121905Simp
1350121905Simpstatic int
1351121905Simppccard_deactivate_resource(device_t brdev, device_t child, int type,
1352121905Simp    int rid, struct resource *r)
1353121905Simp{
1354121905Simp	/* XXX undo pccard_activate_resource? XXX */
1355121905Simp	return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1356121905Simp}
1357121905Simp
1358150098Simpstatic int
1359150098Simppccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1360150098Simp    uint8_t *val)
1361150098Simp{
1362150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1363150098Simp	struct pccard_function *pf = devi->pf;
1364150098Simp
1365150098Simp	/*
1366150098Simp	 * Optimization.  Most of the time, devices want to access
1367150098Simp	 * the same page of the attribute memory that the CCR is in.
1368150098Simp	 * We take advantage of this fact here.
1369150098Simp	 */
1370150098Simp	if (offset / PCCARD_MEM_PAGE_SIZE ==
1371150098Simp	    pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1372150098Simp		*val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1373150098Simp		    offset % PCCARD_MEM_PAGE_SIZE);
1374150098Simp	else {
1375150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1376150098Simp		    &offset);
1377150098Simp		*val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1378150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1379150098Simp		    &offset);
1380150098Simp	}
1381150098Simp	return 0;
1382150098Simp}
1383150098Simp
1384150098Simpstatic int
1385150098Simppccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1386150098Simp    uint8_t val)
1387150098Simp{
1388150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1389150098Simp	struct pccard_function *pf = devi->pf;
1390150098Simp
1391150098Simp	/*
1392150098Simp	 * Optimization.  Most of the time, devices want to access
1393150098Simp	 * the same page of the attribute memory that the CCR is in.
1394150098Simp	 * We take advantage of this fact here.
1395150098Simp	 */
1396150098Simp	if (offset / PCCARD_MEM_PAGE_SIZE ==
1397150098Simp	    pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1398150098Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1399150098Simp		    offset % PCCARD_MEM_PAGE_SIZE, val);
1400150098Simp	else {
1401150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1402150098Simp		    &offset);
1403150098Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1404150098Simp		CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1405150098Simp		    &offset);
1406150098Simp	}
1407150098Simp
1408150098Simp	return 0;
1409150098Simp}
1410150098Simp
1411150098Simpstatic int
1412150098Simppccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1413150098Simp    uint8_t *val)
1414150098Simp{
1415150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1416150098Simp
1417150098Simp	*val = pccard_ccr_read(devi->pf, offset);
1418222764Simp	DEVPRINTF((child, "ccr_read of %#x (%#x) is %#x\n", offset,
1419222764Simp	  devi->pf->pf_ccr_offset, *val));
1420150098Simp	return 0;
1421150098Simp}
1422150098Simp
1423150098Simpstatic int
1424150098Simppccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1425150098Simp    uint8_t val)
1426150098Simp{
1427150098Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
1428150098Simp	struct pccard_function *pf = devi->pf;
1429150098Simp
1430150098Simp	/*
1431150098Simp	 * Can't use pccard_ccr_write since client drivers may access
1432150098Simp	 * registers not contained in the 'mask' if they are non-standard.
1433150098Simp	 */
1434222764Simp	DEVPRINTF((child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1435222764Simp	  devi->pf->pf_ccr_offset));
1436150098Simp	bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1437150098Simp	    val);
1438150098Simp	return 0;
1439150098Simp}
1440150098Simp
1441150098Simp
144252506Simpstatic device_method_t pccard_methods[] = {
144352506Simp	/* Device interface */
144452506Simp	DEVMETHOD(device_probe,		pccard_probe),
144559193Simp	DEVMETHOD(device_attach,	pccard_attach),
144682378Sjon	DEVMETHOD(device_detach,	pccard_detach),
144752506Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
144887975Simp	DEVMETHOD(device_suspend,	pccard_suspend),
144987975Simp	DEVMETHOD(device_resume,	pccard_resume),
145052506Simp
145152506Simp	/* Bus interface */
145252506Simp	DEVMETHOD(bus_print_child,	pccard_print_child),
145366779Simp	DEVMETHOD(bus_driver_added,	pccard_driver_added),
145467333Simp	DEVMETHOD(bus_child_detached,	pccard_child_detached),
145567242Simp	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
145667242Simp	DEVMETHOD(bus_release_resource,	pccard_release_resource),
1457121905Simp	DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1458121905Simp	DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
145970715Sjon	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
146070715Sjon	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
146152506Simp	DEVMETHOD(bus_set_resource,	pccard_set_resource),
146252506Simp	DEVMETHOD(bus_get_resource,	pccard_get_resource),
146352506Simp	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1464104641Simp	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
146566058Simp	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1466104641Simp	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1467104641Simp	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
146852506Simp
146959193Simp	/* Card Interface */
147059193Simp	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
147159193Simp	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
147259193Simp	DEVMETHOD(card_attach_card,	pccard_attach_card),
147359193Simp	DEVMETHOD(card_detach_card,	pccard_detach_card),
1474150098Simp	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1475147711Simp	DEVMETHOD(card_cis_scan,	pccard_scan_cis),
1476150098Simp	DEVMETHOD(card_attr_read,	pccard_attr_read_impl),
1477150098Simp	DEVMETHOD(card_attr_write,	pccard_attr_write_impl),
1478150098Simp	DEVMETHOD(card_ccr_read,	pccard_ccr_read_impl),
1479150098Simp	DEVMETHOD(card_ccr_write,	pccard_ccr_write_impl),
148059193Simp
148152506Simp	{ 0, 0 }
148252506Simp};
148352506Simp
148452506Simpstatic driver_t pccard_driver = {
148552506Simp	"pccard",
148652506Simp	pccard_methods,
148764850Simp	sizeof(struct pccard_softc)
148852506Simp};
148952506Simp
149052506Simpdevclass_t	pccard_devclass;
149152506Simp
1492101905Simp/* Maybe we need to have a slot device? */
149353873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1494101905SimpDRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
149564927SimpMODULE_VERSION(pccard, 1);
1496