pccard.c revision 116311
166200Simp/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
252506Simp/* $FreeBSD: head/sys/dev/pccard/pccard.c 116311 2003-06-13 21:30:29Z imp $ */
352506Simp
452506Simp/*
552506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
652506Simp *
752506Simp * Redistribution and use in source and binary forms, with or without
852506Simp * modification, are permitted provided that the following conditions
952506Simp * are met:
1052506Simp * 1. Redistributions of source code must retain the above copyright
1152506Simp *    notice, this list of conditions and the following disclaimer.
1252506Simp * 2. Redistributions in binary form must reproduce the above copyright
1352506Simp *    notice, this list of conditions and the following disclaimer in the
1452506Simp *    documentation and/or other materials provided with the distribution.
1552506Simp * 3. All advertising materials mentioning features or use of this software
1652506Simp *    must display the following acknowledgement:
1752506Simp *	This product includes software developed by Marc Horowitz.
1852506Simp * 4. The name of the author may not be used to endorse or promote products
1952506Simp *    derived from this software without specific prior written permission.
2052506Simp *
2152506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2252506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2352506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2452506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2552506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2652506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2752506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2852506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2952506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3052506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3152506Simp */
3252506Simp
3352506Simp#include <sys/param.h>
3452506Simp#include <sys/systm.h>
3552506Simp#include <sys/malloc.h>
3652506Simp#include <sys/module.h>
3752506Simp#include <sys/kernel.h>
3852506Simp#include <sys/queue.h>
3991786Simp#include <sys/sysctl.h>
4052506Simp#include <sys/types.h>
4152506Simp
4252506Simp#include <sys/bus.h>
4352506Simp#include <machine/bus.h>
4452506Simp#include <sys/rman.h>
4552506Simp#include <machine/resource.h>
4652506Simp
4782781Sshiba#include <net/ethernet.h>
4882781Sshiba
4952506Simp#include <dev/pccard/pccardreg.h>
5052506Simp#include <dev/pccard/pccardvar.h>
5152506Simp
5255500Simp#include "power_if.h"
5359193Simp#include "card_if.h"
5455500Simp
5555500Simp#define PCCARDDEBUG
5655500Simp
5791786Simp/* sysctl vars */
5891786SimpSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
5991786Simp
6091786Simpint	pccard_debug = 0;
6191786SimpTUNABLE_INT("hw.pccard.debug", &pccard_debug);
6291786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
6391786Simp    &pccard_debug, 0,
6491786Simp  "pccard debug");
6591786Simp
6691786Simpint	pccard_cis_debug = 0;
6791786SimpTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
6891786SimpSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
6991786Simp    &pccard_cis_debug, 0, "pccard CIS debug");
7091786Simp
7152506Simp#ifdef PCCARDDEBUG
7252506Simp#define	DPRINTF(arg) if (pccard_debug) printf arg
7355500Simp#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
7467333Simp#define PRVERBOSE(arg) printf arg
7567333Simp#define DEVPRVERBOSE(arg) device_printf arg
7652506Simp#else
7752506Simp#define	DPRINTF(arg)
7855500Simp#define	DEVPRINTF(arg)
7967333Simp#define PRVERBOSE(arg) if (bootverbose) printf arg
8067333Simp#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
8152506Simp#endif
8252506Simp
8382378Sjonstatic int	pccard_ccr_read(struct pccard_function *pf, int ccr);
8482378Sjonstatic void	pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
8582378Sjonstatic int	pccard_attach_card(device_t dev);
86106362Simpstatic int	pccard_detach_card(device_t dev);
8782378Sjonstatic int	pccard_card_gettype(device_t dev, int *type);
8882378Sjonstatic void	pccard_function_init(struct pccard_function *pf);
8982378Sjonstatic void	pccard_function_free(struct pccard_function *pf);
9082378Sjonstatic int	pccard_function_enable(struct pccard_function *pf);
9182378Sjonstatic void	pccard_function_disable(struct pccard_function *pf);
9282378Sjonstatic int	pccard_compat_do_probe(device_t bus, device_t dev);
9382378Sjonstatic int	pccard_compat_do_attach(device_t bus, device_t dev);
9482378Sjonstatic int	pccard_add_children(device_t dev, int busno);
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,
10882378Sjon		    int rid, u_int32_t flags);
10982378Sjonstatic int	pccard_set_memory_offset(device_t dev, device_t child, int rid,
11082378Sjon		    u_int32_t offset, u_int32_t *deltap);
111104641Simpstatic void	pccard_probe_nomatch(device_t cbdev, device_t child);
11282378Sjonstatic int	pccard_read_ivar(device_t bus, device_t child, int which,
11382378Sjon		    u_char *result);
11482378Sjonstatic void	pccard_driver_added(device_t dev, driver_t *driver);
11582378Sjonstatic struct resource *pccard_alloc_resource(device_t dev,
11682378Sjon		    device_t child, int type, int *rid, u_long start,
11782378Sjon		    u_long end, u_long count, u_int flags);
11882378Sjonstatic int	pccard_release_resource(device_t dev, device_t child, int type,
11982378Sjon		    int rid, struct resource *r);
12082378Sjonstatic void	pccard_child_detached(device_t parent, device_t dev);
12182378Sjonstatic void	pccard_intr(void *arg);
12282378Sjonstatic int	pccard_setup_intr(device_t dev, device_t child,
12382378Sjon		    struct resource *irq, int flags, driver_intr_t *intr,
12482378Sjon		    void *arg, void **cookiep);
12582378Sjonstatic int	pccard_teardown_intr(device_t dev, device_t child,
12682378Sjon		    struct resource *r, void *cookie);
12752506Simp
12897613Stakawatastatic const struct pccard_product *
12997613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
13097613Stakawata			 const struct pccard_product *tab, size_t ent_size,
13197613Stakawata			 pccard_product_match_fn matchfn);
13297613Stakawata
13397613Stakawata
13482378Sjonstatic int
13574632Simppccard_ccr_read(struct pccard_function *pf, int ccr)
13652506Simp{
13752506Simp	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
13852506Simp	    pf->pf_ccr_offset + ccr));
13952506Simp}
14052506Simp
14182378Sjonstatic void
14274632Simppccard_ccr_write(struct pccard_function *pf, int ccr, int val)
14352506Simp{
14452506Simp	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
14552506Simp		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
14652506Simp		    pf->pf_ccr_offset + ccr, val);
14752506Simp	}
14852506Simp}
14952506Simp
15059193Simpstatic int
151113242Simppccard_set_default_descr(device_t dev)
152113242Simp{
153113242Simp	char *vendorstr, *prodstr, *str;
154113242Simp
155113242Simp	if (pccard_get_vendor_str(dev, &vendorstr))
156113242Simp		return (0);
157113242Simp	if (pccard_get_product_str(dev, &prodstr))
158113242Simp		return (0);
159113242Simp	str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
160113242Simp	    M_WAITOK);
161113242Simp	sprintf(str, "%s %s", vendorstr, prodstr);
162113242Simp	device_set_desc_copy(dev, str);
163113242Simp	free(str, M_DEVBUF);
164113242Simp	return (0);
165113242Simp}
166113242Simp
167113242Simpstatic int
16859193Simppccard_attach_card(device_t dev)
16952506Simp{
17064850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
17152506Simp	struct pccard_function *pf;
17265917Simp	struct pccard_ivar *ivar;
17361788Simp	device_t child;
174102713Simp	int i;
17552506Simp
17652506Simp	/*
17752506Simp	 * this is here so that when socket_enable calls gettype, trt happens
17852506Simp	 */
17952506Simp	STAILQ_INIT(&sc->card.pf_head);
18052506Simp
18155500Simp	DEVPRINTF((dev, "chip_socket_enable\n"));
18255500Simp	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
18352506Simp
18455500Simp	DEVPRINTF((dev, "read_cis\n"));
18552506Simp	pccard_read_cis(sc);
18652506Simp
18755500Simp	DEVPRINTF((dev, "check_cis_quirks\n"));
18852506Simp	pccard_check_cis_quirks(dev);
18952506Simp
19052506Simp	/*
19152506Simp	 * bail now if the card has no functions, or if there was an error in
19252506Simp	 * the cis.
19352506Simp	 */
19452506Simp
19570715Sjon	if (sc->card.error) {
196102713Simp		device_printf(dev, "CARD ERROR!\n");
19752506Simp		return (1);
19870715Sjon	}
19970715Sjon	if (STAILQ_EMPTY(&sc->card.pf_head)) {
200102713Simp		device_printf(dev, "Card has no functions!\n");
20152506Simp		return (1);
20270715Sjon	}
20352506Simp
20490436Simp	if (bootverbose || pccard_debug)
20552506Simp		pccard_print_cis(dev);
20652506Simp
20755500Simp	DEVPRINTF((dev, "functions scanning\n"));
208102713Simp	i = -1;
20952506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
210102713Simp		i++;
211102713Simp		if (STAILQ_EMPTY(&pf->cfe_head)) {
212102713Simp			device_printf(dev,
213102713Simp			    "Function %d has no config entries.!\n", i);
21452506Simp			continue;
215102713Simp		}
21652506Simp		pf->sc = sc;
21752506Simp		pf->cfe = NULL;
21864927Simp		pf->dev = NULL;
21952506Simp	}
220104641Simp	DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
221102713Simp	    pccard_mfc(sc)));
22282378Sjon
22352506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
22452506Simp		if (STAILQ_EMPTY(&pf->cfe_head))
22552506Simp			continue;
22661788Simp		/*
22761788Simp		 * In NetBSD, the drivers are responsible for activating
22861788Simp		 * each function of a card.  I think that in FreeBSD we
22961788Simp		 * want to activate them enough for the usual bus_*_resource
23061788Simp		 * routines will do the right thing.  This many mean a
23161788Simp		 * departure from the current NetBSD model.
23261788Simp		 *
233104641Simp		 * This seems to work well in practice for most cards.
234104641Simp		 * However, there are two cases that are problematic.
235104641Simp		 * If a driver wishes to pick and chose which config
236104641Simp		 * entry to use, then this method falls down.  These
237104641Simp		 * are usually older cards.  In addition, there are
238104641Simp		 * some cards that have multiple hardware units on the
239104641Simp		 * cards, but presents only one CIS chain.  These cards
240104641Simp		 * are combination cards, but only one of these units
241104641Simp		 * can be on at a time.
24261788Simp		 */
24367897Sdwmalone		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
244111119Simp		    M_WAITOK | M_ZERO);
24561788Simp		child = device_add_child(dev, NULL, -1);
24665917Simp		device_set_ivars(child, ivar);
24766847Simp		ivar->fcn = pf;
24867187Simp		pf->dev = child;
24967167Simp		/*
25067167Simp		 * XXX We might want to move the next two lines into
25167167Simp		 * XXX the pccard interface layer.  For the moment, this
25267167Simp		 * XXX is OK, but some drivers want to pick the config
25367167Simp		 * XXX entry to use as well as some address tweaks (mostly
25467167Simp		 * XXX due to bugs in decode logic that makes some
25567167Simp		 * XXX addresses illegal or broken).
25667167Simp		 */
25765917Simp		pccard_function_init(pf);
25882378Sjon		if (sc->sc_enabled_count == 0)
25982378Sjon			POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
26067333Simp		if (pccard_function_enable(pf) == 0 &&
261113242Simp		    pccard_set_default_descr(child) == 0 &&
26267333Simp		    device_probe_and_attach(child) == 0) {
26355500Simp			DEVPRINTF((sc->dev, "function %d CCR at %d "
26467167Simp			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
26567167Simp			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
26667167Simp			    pccard_ccr_read(pf, 0x00),
26752506Simp			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
26852506Simp			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
26952506Simp			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
27052506Simp			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
27167167Simp		} else {
27286907Simp			if (pf->cfe != NULL)
27386907Simp				pccard_function_disable(pf);
27452506Simp		}
27552506Simp	}
27674632Simp	return (0);
27752506Simp}
27852506Simp
27959193Simpstatic int
280106362Simppccard_detach_card(device_t dev)
28152506Simp{
28264850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
28352506Simp	struct pccard_function *pf;
28482378Sjon	struct pccard_config_entry *cfe;
28552506Simp
28652506Simp	/*
28752506Simp	 * We are running on either the PCCARD socket's event thread
28852506Simp	 * or in user context detaching a device by user request.
28952506Simp	 */
29052506Simp	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
29182378Sjon		int state = device_get_state(pf->dev);
29261788Simp
29382378Sjon		if (state == DS_ATTACHED || state == DS_BUSY)
29482378Sjon			device_detach(pf->dev);
29586907Simp		if (pf->cfe != NULL)
29686907Simp			pccard_function_disable(pf);
29782378Sjon		pccard_function_free(pf);
298106896Simp		device_delete_child(dev, pf->dev);
29952506Simp	}
30082378Sjon	if (sc->sc_enabled_count == 0)
30182378Sjon		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
30282378Sjon
30382378Sjon	while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
30482378Sjon		while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
30582378Sjon			STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
30682378Sjon			free(cfe, M_DEVBUF);
30782378Sjon		}
30882378Sjon		STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
30982378Sjon		free(pf, M_DEVBUF);
31082378Sjon	}
31174632Simp	return (0);
31252506Simp}
31352506Simp
31497613Stakawatastatic const struct pccard_product *
31597613Stakawatapccard_do_product_lookup(device_t bus, device_t dev,
316112359Simp    const struct pccard_product *tab, size_t ent_size,
317112359Simp    pccard_product_match_fn matchfn)
31866200Simp{
31966200Simp	const struct pccard_product *ent;
32066200Simp	int matches;
32166200Simp	u_int32_t fcn;
32266200Simp	u_int32_t vendor;
32366200Simp	u_int32_t prod;
32466200Simp	char *vendorstr;
32566200Simp	char *prodstr;
32666200Simp
32766200Simp#ifdef DIAGNOSTIC
32866200Simp	if (sizeof *ent > ent_size)
329112359Simp		panic("pccard_product_lookup: bogus ent_size %jd",
330112359Simp		    (intmax_t) ent_size);
33166200Simp#endif
33266200Simp	if (pccard_get_vendor(dev, &vendor))
33366200Simp		return (NULL);
33466200Simp	if (pccard_get_product(dev, &prod))
33566200Simp		return (NULL);
33666200Simp	if (pccard_get_function_number(dev, &fcn))
33766200Simp		return (NULL);
33866200Simp	if (pccard_get_vendor_str(dev, &vendorstr))
33966200Simp		return (NULL);
34066200Simp	if (pccard_get_product_str(dev, &prodstr))
34166200Simp		return (NULL);
342113313Simp	for (ent = tab; ent->pp_vendor != 0; ent =
34382378Sjon	    (const struct pccard_product *) ((const char *) ent + ent_size)) {
34466200Simp		matches = 1;
34586642Simp		if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
346113078Ssanpei		    ent->pp_product == PCCARD_PRODUCT_ANY &&
34786642Simp		    ent->pp_cis[0] == NULL &&
34886642Simp		    ent->pp_cis[1] == NULL) {
349113300Simp			if (ent->pp_name)
350113300Simp				device_printf(dev,
351113300Simp				    "Total wildcard entry ignored for %s\n",
352113300Simp				    ent->pp_name);
35386642Simp			continue;
35486642Simp		}
35566200Simp		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
35666200Simp		    vendor != ent->pp_vendor)
35766200Simp			matches = 0;
35866200Simp		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
35966200Simp		    prod != ent->pp_product)
36066200Simp			matches = 0;
36166200Simp		if (matches && fcn != ent->pp_expfunc)
36266200Simp			matches = 0;
36371322Simp		if (matches && ent->pp_cis[0] &&
36471322Simp		    strcmp(ent->pp_cis[0], vendorstr) != 0)
36566200Simp			matches = 0;
36671322Simp		if (matches && ent->pp_cis[1] &&
36771322Simp		    strcmp(ent->pp_cis[1], prodstr) != 0)
36866200Simp			matches = 0;
36971322Simp		/* XXX need to match cis[2] and cis[3] also XXX */
37066200Simp		if (matchfn != NULL)
37166200Simp			matches = (*matchfn)(dev, ent, matches);
37266200Simp		if (matches)
37366200Simp			return (ent);
37466200Simp	}
37566200Simp	return (NULL);
37666200Simp}
37766200Simp
37870715Sjonstatic int
37959193Simppccard_card_gettype(device_t dev, int *type)
38052506Simp{
38164850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
38252506Simp	struct pccard_function *pf;
38352506Simp
38452506Simp	/*
38552506Simp	 * set the iftype to memory if this card has no functions (not yet
38652506Simp	 * probed), or only one function, and that is not initialized yet or
38752506Simp	 * that is memory.
38852506Simp	 */
38952506Simp	pf = STAILQ_FIRST(&sc->card.pf_head);
39052506Simp	if (pf == NULL ||
39152506Simp	    (STAILQ_NEXT(pf, pf_list) == NULL &&
39252506Simp	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
39359193Simp		*type = PCCARD_IFTYPE_MEMORY;
39452506Simp	else
39559193Simp		*type = PCCARD_IFTYPE_IO;
39674632Simp	return (0);
39752506Simp}
39852506Simp
39952506Simp/*
40052506Simp * Initialize a PCCARD function.  May be called as long as the function is
40152506Simp * disabled.
40282382Simp *
40382382Simp * Note: pccard_function_init should not keep resources allocated.  It should
40482382Simp * only set them up ala isa pnp, set the values in the rl lists, and return.
40582382Simp * Any resource held after pccard_function_init is called is a bug.  However,
40682382Simp * the bus routines to get the resources also assume that pccard_function_init
40782382Simp * does this, so they need to be fixed too.
40852506Simp */
40982378Sjonstatic void
41070715Sjonpccard_function_init(struct pccard_function *pf)
41152506Simp{
41265917Simp	struct pccard_config_entry *cfe;
41367187Simp	int i;
41467242Simp	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
41567242Simp	struct resource_list *rl = &devi->resources;
41670762Simp	struct resource_list_entry *rle;
41767242Simp	struct resource *r = 0;
41867242Simp	device_t bus;
41967424Simp	int start;
42067424Simp	int end;
42190897Simp	int spaces;
42265917Simp
42370715Sjon	if (pf->pf_flags & PFF_ENABLED) {
42470715Sjon		printf("pccard_function_init: function is enabled");
42570715Sjon		return;
42670715Sjon	}
42767242Simp	bus = device_get_parent(pf->dev);
42852506Simp	/* Remember which configuration entry we are using. */
42972012Sphk	STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
43067187Simp		for (i = 0; i < cfe->num_iospace; i++)
43167187Simp			cfe->iores[i] = NULL;
43267187Simp		cfe->irqres = NULL;
43390897Simp		spaces = 0;
43467187Simp		for (i = 0; i < cfe->num_iospace; i++) {
43567424Simp			start = cfe->iospace[i].start;
43667424Simp			if (start)
43767424Simp				end = start + cfe->iospace[i].length - 1;
43867424Simp			else
43967424Simp				end = ~0;
44067187Simp			cfe->iorid[i] = i;
44190897Simp			DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
44290897Simp			    i, start, end));
44367242Simp			r = cfe->iores[i] = bus_alloc_resource(bus,
44467424Simp			    SYS_RES_IOPORT, &cfe->iorid[i], start, end,
44570715Sjon			    cfe->iospace[i].length,
44667424Simp			    rman_make_alignment_flags(cfe->iospace[i].length));
44776424Simp			if (cfe->iores[i] == NULL)
44867187Simp				goto not_this_one;
44967242Simp			resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
45067242Simp			    rman_get_start(r), rman_get_end(r),
45167242Simp			    cfe->iospace[i].length);
45276424Simp			rle = resource_list_find(rl, SYS_RES_IOPORT,
45376424Simp			    cfe->iorid[i]);
45476424Simp			rle->res = r;
45590897Simp			spaces++;
45667187Simp		}
45767187Simp		if (cfe->num_memspace > 0) {
45882781Sshiba			/*
45982781Sshiba			 * Not implement yet, Fix me.
46082781Sshiba			 */
46190897Simp			DEVPRINTF((bus, "Memory space not yet implemented.\n"));
46267187Simp		}
46390897Simp		if (spaces == 0) {
46490897Simp			DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
46590897Simp			goto not_this_one;
46690897Simp		}
46767187Simp		if (cfe->irqmask) {
46867187Simp			cfe->irqrid = 0;
46967399Simp			r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
47067424Simp			    &cfe->irqrid, 0, ~0, 1, 0);
47176424Simp			if (cfe->irqres == NULL)
47267187Simp				goto not_this_one;
47367242Simp			resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
47467242Simp			    rman_get_start(r), rman_get_end(r), 1);
47576424Simp			rle = resource_list_find(rl, SYS_RES_IRQ,
47676424Simp			    cfe->irqrid);
47776424Simp			rle->res = r;
47867187Simp		}
47967187Simp		/* If we get to here, we've allocated all we need */
48067167Simp		pf->cfe = cfe;
48167187Simp		break;
48267187Simp	    not_this_one:;
48367424Simp		DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
48467424Simp		    cfe->number));
48567333Simp		/*
48667333Simp		 * Release resources that we partially allocated
48767333Simp		 * from this config entry.
48867333Simp		 */
48967187Simp		for (i = 0; i < cfe->num_iospace; i++) {
49076424Simp			if (cfe->iores[i] != NULL) {
49170715Sjon				bus_release_resource(bus, SYS_RES_IOPORT,
49267187Simp				    cfe->iorid[i], cfe->iores[i]);
49382378Sjon				rle = resource_list_find(rl, SYS_RES_IOPORT,
49476424Simp				    cfe->iorid[i]);
49576424Simp				rle->res = NULL;
49676424Simp				resource_list_delete(rl, SYS_RES_IOPORT,
49776424Simp				    cfe->iorid[i]);
49875756Simp			}
49967187Simp			cfe->iores[i] = NULL;
50067187Simp		}
50176424Simp		if (cfe->irqmask && cfe->irqres != NULL) {
50267242Simp			bus_release_resource(bus, SYS_RES_IRQ,
50367187Simp			    cfe->irqrid, cfe->irqres);
50476424Simp			rle = resource_list_find(rl, SYS_RES_IRQ,
50576424Simp			    cfe->irqrid);
50676424Simp			rle->res = NULL;
50776424Simp			resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
50867187Simp			cfe->irqres = NULL;
50967187Simp		}
51067167Simp	}
51152506Simp}
51252506Simp
51382378Sjon/*
51482378Sjon * Free resources allocated by pccard_function_init(), May be called as long
51582378Sjon * as the function is disabled.
51682382Simp *
51782382Simp * NOTE: This function should be unnecessary.  pccard_function_init should
51882382Simp * never keep resources initialized.
51982378Sjon */
52082378Sjonstatic void
52182378Sjonpccard_function_free(struct pccard_function *pf)
52282378Sjon{
52382378Sjon	struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
52482378Sjon	struct resource_list_entry *rle;
52582378Sjon
52682378Sjon	if (pf->pf_flags & PFF_ENABLED) {
52782378Sjon		printf("pccard_function_init: function is enabled");
52882378Sjon		return;
52982378Sjon	}
53082378Sjon
53182378Sjon	SLIST_FOREACH(rle, &devi->resources, link) {
53282378Sjon		if (rle->res) {
533113242Simp			if (rman_get_device(rle->res) != pf->sc->dev)
53482378Sjon				device_printf(pf->sc->dev,
53582378Sjon				    "function_free: Resource still owned by "
53682378Sjon				    "child, oops. "
53782378Sjon				    "(type=%d, rid=%d, addr=%lx)\n",
53882378Sjon				    rle->type, rle->rid,
53982378Sjon				    rman_get_start(rle->res));
54082378Sjon			BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
541113242Simp			    pf->sc->dev, rle->type, rle->rid, rle->res);
54282378Sjon			rle->res = NULL;
54382378Sjon		}
54482378Sjon	}
54582378Sjon	resource_list_free(&devi->resources);
54682378Sjon}
54782378Sjon
54852506Simp/* Enable a PCCARD function */
54982378Sjonstatic int
55055720Simppccard_function_enable(struct pccard_function *pf)
55152506Simp{
55252506Simp	struct pccard_function *tmp;
55352506Simp	int reg;
55455720Simp	device_t dev = pf->sc->dev;
55570746Simp
55667333Simp	if (pf->cfe == NULL) {
55767333Simp		DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
55874632Simp		return (ENOMEM);
55967333Simp	}
56052506Simp
56152506Simp	/*
56252506Simp	 * Increase the reference count on the socket, enabling power, if
56352506Simp	 * necessary.
56452506Simp	 */
56582378Sjon	pf->sc->sc_enabled_count++;
56652506Simp
56752506Simp	if (pf->pf_flags & PFF_ENABLED) {
56852506Simp		/*
56952506Simp		 * Don't do anything if we're already enabled.
57052506Simp		 */
57152506Simp		return (0);
57252506Simp	}
57352506Simp
57452506Simp	/*
57552506Simp	 * it's possible for different functions' CCRs to be in the same
57652506Simp	 * underlying page.  Check for that.
57752506Simp	 */
57852506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
57952506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
58052506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
58152506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
58282378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
58382378Sjon		    tmp->pf_ccr_realsize))) {
58452506Simp			pf->pf_ccrt = tmp->pf_ccrt;
58552506Simp			pf->pf_ccrh = tmp->pf_ccrh;
58652506Simp			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
58752506Simp
58852506Simp			/*
58952506Simp			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
59052506Simp			 * tmp->ccr_base) + pf->ccr_base;
59152506Simp			 */
59270715Sjon			/* pf->pf_ccr_offset =
59352506Simp			    (tmp->pf_ccr_offset + pf->ccr_base) -
59470715Sjon			    tmp->ccr_base; */
59552506Simp			pf->pf_ccr_window = tmp->pf_ccr_window;
59652506Simp			break;
59752506Simp		}
59852506Simp	}
59952506Simp	if (tmp == NULL) {
60055720Simp		pf->ccr_rid = 0;
60155720Simp		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
60270715Sjon		    &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
60370715Sjon		if (!pf->ccr_res)
60452506Simp			goto bad;
605106914Smux		DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
60670746Simp		    rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
60770746Simp		    pf->ccr_base));
60861788Simp		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
60961788Simp		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
61070715Sjon		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
61170748Simp		    pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
61255720Simp		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
61355720Simp		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
61455720Simp		pf->pf_ccr_realsize = 1;
61552506Simp	}
61652506Simp
61752506Simp	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
61852506Simp	reg |= PCCARD_CCR_OPTION_LEVIREQ;
61952506Simp	if (pccard_mfc(pf->sc)) {
62052506Simp		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
62152506Simp			PCCARD_CCR_OPTION_ADDR_DECODE);
62282383Simp		/* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
62352506Simp	}
62452506Simp	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
62552506Simp
62652506Simp	reg = 0;
62752506Simp	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
62852506Simp		reg |= PCCARD_CCR_STATUS_IOIS8;
62952506Simp	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
63052506Simp		reg |= PCCARD_CCR_STATUS_AUDIO;
63152506Simp	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
63252506Simp
63352506Simp	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
63452506Simp
63552506Simp	if (pccard_mfc(pf->sc)) {
63652506Simp		long tmp, iosize;
63752506Simp
63852506Simp		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
63952506Simp		/* round up to nearest (2^n)-1 */
64052506Simp		for (iosize = 1; iosize < tmp; iosize <<= 1)
64152506Simp			;
64252506Simp		iosize--;
64352506Simp
64452506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
64552506Simp				 pf->pf_mfc_iobase & 0xff);
64652506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
64752506Simp				 (pf->pf_mfc_iobase >> 8) & 0xff);
64852506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
64952506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
65052506Simp
65152506Simp		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
65252506Simp	}
65352506Simp
65452506Simp#ifdef PCCARDDEBUG
65552506Simp	if (pccard_debug) {
65652506Simp		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
65770715Sjon			device_printf(tmp->sc->dev,
65855500Simp			    "function %d CCR at %d offset %x: "
65955500Simp			    "%x %x %x %x, %x %x %x %x, %x\n",
66070715Sjon			    tmp->number, tmp->pf_ccr_window,
66155500Simp			    tmp->pf_ccr_offset,
66255500Simp			    pccard_ccr_read(tmp, 0x00),
66355500Simp			    pccard_ccr_read(tmp, 0x02),
66455500Simp			    pccard_ccr_read(tmp, 0x04),
66555500Simp			    pccard_ccr_read(tmp, 0x06),
66655500Simp			    pccard_ccr_read(tmp, 0x0A),
66770715Sjon			    pccard_ccr_read(tmp, 0x0C),
66855500Simp			    pccard_ccr_read(tmp, 0x0E),
66955500Simp			    pccard_ccr_read(tmp, 0x10),
67055500Simp			    pccard_ccr_read(tmp, 0x12));
67152506Simp		}
67252506Simp	}
67352506Simp#endif
67452506Simp	pf->pf_flags |= PFF_ENABLED;
67552506Simp	return (0);
67652506Simp
67752506Simp bad:
67852506Simp	/*
67952506Simp	 * Decrement the reference count, and power down the socket, if
68052506Simp	 * necessary.
68152506Simp	 */
68282378Sjon	pf->sc->sc_enabled_count--;
68365098Simp	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
68452506Simp
68552506Simp	return (1);
68652506Simp}
68752506Simp
68852506Simp/* Disable PCCARD function. */
68982378Sjonstatic void
69055720Simppccard_function_disable(struct pccard_function *pf)
69152506Simp{
69252506Simp	struct pccard_function *tmp;
69355720Simp	device_t dev = pf->sc->dev;
69452506Simp
69552506Simp	if (pf->cfe == NULL)
69661788Simp		panic("pccard_function_disable: function not initialized");
69752506Simp
69852506Simp	if ((pf->pf_flags & PFF_ENABLED) == 0) {
69952506Simp		/*
70052506Simp		 * Don't do anything if we're already disabled.
70152506Simp		 */
70252506Simp		return;
70352506Simp	}
70452506Simp
70570715Sjon	if (pf->intr_handler != NULL) {
70682378Sjon		struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
70782378Sjon		struct resource_list_entry *rle =
70882378Sjon		    resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
70982378Sjon		BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
71082378Sjon		    pf->intr_handler_cookie);
71170715Sjon	}
71270715Sjon
71352506Simp	/*
71452506Simp	 * it's possible for different functions' CCRs to be in the same
71552506Simp	 * underlying page.  Check for that.  Note we mark us as disabled
71652506Simp	 * first to avoid matching ourself.
71752506Simp	 */
71852506Simp
71952506Simp	pf->pf_flags &= ~PFF_ENABLED;
72052506Simp	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
72152506Simp		if ((tmp->pf_flags & PFF_ENABLED) &&
72252506Simp		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
72352506Simp		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
72482378Sjon		    (tmp->ccr_base - tmp->pf_ccr_offset +
72582378Sjon		    tmp->pf_ccr_realsize)))
72652506Simp			break;
72752506Simp	}
72852506Simp
72952506Simp	/* Not used by anyone else; unmap the CCR. */
73052506Simp	if (tmp == NULL) {
73170715Sjon		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
73255720Simp		    pf->ccr_res);
73355720Simp		pf->ccr_res = NULL;
73452506Simp	}
73552506Simp
73652506Simp	/*
73752506Simp	 * Decrement the reference count, and power down the socket, if
73852506Simp	 * necessary.
73952506Simp	 */
74082378Sjon	pf->sc->sc_enabled_count--;
74152506Simp}
74252506Simp
74366058Simp/*
74466058Simp * simulate the old "probe" routine.  In the new world order, the driver
74566058Simp * needs to grab devices while in the old they were assigned to the device by
74666058Simp * the pccardd process.  These symbols are exported to the upper layers.
74766058Simp */
74874636Simpstatic int
74974636Simppccard_compat_do_probe(device_t bus, device_t dev)
75066058Simp{
75166058Simp	return (CARD_COMPAT_MATCH(dev));
75266058Simp}
75366058Simp
75474636Simpstatic int
75574636Simppccard_compat_do_attach(device_t bus, device_t dev)
75666058Simp{
75766058Simp	int err;
75866058Simp
75966058Simp	err = CARD_COMPAT_PROBE(dev);
76066058Simp	if (err == 0)
76166058Simp		err = CARD_COMPAT_ATTACH(dev);
76266058Simp	return (err);
76366058Simp}
76466058Simp
76553873Simp#define PCCARD_NPORT	2
76653873Simp#define PCCARD_NMEM	5
76753873Simp#define PCCARD_NIRQ	1
76853873Simp#define PCCARD_NDRQ	0
76953873Simp
77052506Simpstatic int
77152506Simppccard_add_children(device_t dev, int busno)
77252506Simp{
77359193Simp	/* Call parent to scan for any current children */
77474632Simp	return (0);
77552506Simp}
77652506Simp
77752506Simpstatic int
77852506Simppccard_probe(device_t dev)
77952506Simp{
78067333Simp	device_set_desc(dev, "16-bit PCCard bus");
78174632Simp	return (pccard_add_children(dev, device_get_unit(dev)));
78252506Simp}
78352506Simp
78459193Simpstatic int
78559193Simppccard_attach(device_t dev)
78659193Simp{
78764850Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
78861788Simp
78959193Simp	sc->dev = dev;
79061788Simp	sc->sc_enabled_count = 0;
79174632Simp	return (bus_generic_attach(dev));
79259193Simp}
79359193Simp
79482378Sjonstatic int
79582378Sjonpccard_detach(device_t dev)
79682378Sjon{
797106362Simp	pccard_detach_card(dev);
79882378Sjon	return 0;
79982378Sjon}
80082378Sjon
80187975Simpstatic int
80287975Simppccard_suspend(device_t self)
80387975Simp{
804106362Simp	pccard_detach_card(self);
80587975Simp	return (0);
80687975Simp}
80787975Simp
80887975Simpstatic
80987975Simpint
81087975Simppccard_resume(device_t self)
81187975Simp{
81287975Simp	return (0);
81387975Simp}
81487975Simp
81553873Simpstatic void
81653873Simppccard_print_resources(struct resource_list *rl, const char *name, int type,
81753873Simp    int count, const char *format)
81853873Simp{
81953873Simp	struct resource_list_entry *rle;
82053873Simp	int printed;
82153873Simp	int i;
82253873Simp
82353873Simp	printed = 0;
82453873Simp	for (i = 0; i < count; i++) {
82553873Simp		rle = resource_list_find(rl, type, i);
82676424Simp		if (rle != NULL) {
82753873Simp			if (printed == 0)
82853873Simp				printf(" %s ", name);
82953873Simp			else if (printed > 0)
83053873Simp				printf(",");
83153873Simp			printed++;
83253873Simp			printf(format, rle->start);
83353873Simp			if (rle->count > 1) {
83453873Simp				printf("-");
83553873Simp				printf(format, rle->start + rle->count - 1);
83653873Simp			}
83753873Simp		} else if (i > 3) {
83853873Simp			/* check the first few regardless */
83953873Simp			break;
84053873Simp		}
84153873Simp	}
84253873Simp}
84353873Simp
84453873Simpstatic int
84553873Simppccard_print_child(device_t dev, device_t child)
84653873Simp{
84766847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
84853873Simp	struct resource_list *rl = &devi->resources;
84953873Simp	int retval = 0;
85053873Simp
85153873Simp	retval += bus_print_child_header(dev, child);
85253873Simp	retval += printf(" at");
85353873Simp
85476424Simp	if (devi != NULL) {
85553873Simp		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
85653873Simp		    PCCARD_NPORT, "%#lx");
85753873Simp		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
85853873Simp		    PCCARD_NMEM, "%#lx");
85953873Simp		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
86053873Simp		    "%ld");
86170715Sjon		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
86253873Simp		    "%ld");
86367269Simp		retval += printf(" function %d config %d", devi->fcn->number,
86467269Simp		    devi->fcn->cfe->number);
86553873Simp	}
86653873Simp
86753873Simp	retval += bus_print_child_footer(dev, child);
86853873Simp
86953873Simp	return (retval);
87053873Simp}
87153873Simp
87253873Simpstatic int
87353873Simppccard_set_resource(device_t dev, device_t child, int type, int rid,
87453873Simp		 u_long start, u_long count)
87553873Simp{
87666847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
87753873Simp	struct resource_list *rl = &devi->resources;
87853873Simp
87953873Simp	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
88053873Simp	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
88174632Simp		return (EINVAL);
88253873Simp	if (rid < 0)
88374632Simp		return (EINVAL);
88453873Simp	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
88574632Simp		return (EINVAL);
88653873Simp	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
88774632Simp		return (EINVAL);
88853873Simp	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
88974632Simp		return (EINVAL);
89053873Simp	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
89174632Simp		return (EINVAL);
89253873Simp
89353873Simp	resource_list_add(rl, type, rid, start, start + count - 1, count);
89482378Sjon	if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
89582378Sjon	    type, &rid, start, start + count - 1, count, 0))
89682378Sjon		return 0;
89782378Sjon	else
89882378Sjon		return ENOMEM;
89953873Simp}
90053873Simp
90153873Simpstatic int
90253873Simppccard_get_resource(device_t dev, device_t child, int type, int rid,
90353873Simp    u_long *startp, u_long *countp)
90453873Simp{
90566847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
90653873Simp	struct resource_list *rl = &devi->resources;
90753873Simp	struct resource_list_entry *rle;
90853873Simp
90953873Simp	rle = resource_list_find(rl, type, rid);
91076424Simp	if (rle == NULL)
91174632Simp		return (ENOENT);
91270715Sjon
91376424Simp	if (startp != NULL)
91453873Simp		*startp = rle->start;
91576424Simp	if (countp != NULL)
91653873Simp		*countp = rle->count;
91753873Simp
91874632Simp	return (0);
91953873Simp}
92053873Simp
92153873Simpstatic void
92253873Simppccard_delete_resource(device_t dev, device_t child, int type, int rid)
92353873Simp{
92466847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
92553873Simp	struct resource_list *rl = &devi->resources;
92653873Simp	resource_list_delete(rl, type, rid);
92753873Simp}
92853873Simp
92959193Simpstatic int
93059193Simppccard_set_res_flags(device_t dev, device_t child, int type, int rid,
93159193Simp    u_int32_t flags)
93259193Simp{
93374632Simp	return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
93474632Simp	    rid, flags));
93559193Simp}
93659193Simp
93759193Simpstatic int
93859193Simppccard_set_memory_offset(device_t dev, device_t child, int rid,
93982378Sjon    u_int32_t offset, u_int32_t *deltap)
94070715Sjon
94159193Simp{
94274632Simp	return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
94374632Simp	    offset, deltap));
94459193Simp}
94559193Simp
946104641Simpstatic void
947104641Simppccard_probe_nomatch(device_t bus, device_t child)
948104641Simp{
949104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
950104641Simp	struct pccard_function *func = devi->fcn;
951104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
952104641Simp
953104641Simp	device_printf(bus, "<unknown card>");
954104641Simp	printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
955104641Simp	  sc->card.manufacturer, sc->card.product, func->number);
956104641Simp	device_printf(bus, "   CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
957104641Simp	  sc->card.cis1_info[1], sc->card.cis1_info[2]);
958104641Simp	return;
959104641Simp}
960104641Simp
96166058Simpstatic int
962104641Simppccard_child_location_str(device_t bus, device_t child, char *buf,
963104641Simp    size_t buflen)
964104641Simp{
965104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
966104641Simp	struct pccard_function *func = devi->fcn;
967104641Simp
968104641Simp	snprintf(buf, buflen, "function=%d", func->number);
969104641Simp	return (0);
970104641Simp}
971104641Simp
972104641Simpstatic int
973104641Simppccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
974104641Simp    size_t buflen)
975104641Simp{
976104641Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
977104641Simp	struct pccard_function *func = devi->fcn;
978104641Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
979104641Simp
980104641Simp	snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
981104641Simp	    "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
982104641Simp	    sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
983104641Simp	    sc->card.cis1_info[1], func->function);
984104641Simp	return (0);
985104641Simp}
986104641Simp
987104641Simpstatic int
98866058Simppccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
98966058Simp{
99066847Simp	struct pccard_ivar *devi = PCCARD_IVAR(child);
99166779Simp	struct pccard_function *func = devi->fcn;
99266779Simp	struct pccard_softc *sc = PCCARD_SOFTC(bus);
99366779Simp
99466779Simp	switch (which) {
99566779Simp	default:
99666779Simp	case PCCARD_IVAR_ETHADDR:
99782781Sshiba		bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
99866779Simp		break;
99966779Simp	case PCCARD_IVAR_VENDOR:
100066779Simp		*(u_int32_t *) result = sc->card.manufacturer;
100166779Simp		break;
100266779Simp	case PCCARD_IVAR_PRODUCT:
100366779Simp		*(u_int32_t *) result = sc->card.product;
100466779Simp		break;
100590964Sshiba	case PCCARD_IVAR_PRODEXT:
100690964Sshiba		*(u_int16_t *) result = sc->card.prodext;
100790964Sshiba		break;
100875761Simp	case PCCARD_IVAR_FUNCTION:
100975761Simp		*(u_int32_t *) result = func->function;
101075761Simp		break;
101166779Simp	case PCCARD_IVAR_FUNCTION_NUMBER:
101266847Simp		if (!func) {
101366847Simp			device_printf(bus, "No function number, bug!\n");
101466847Simp			return (ENOENT);
101566847Simp		}
101666847Simp		*(u_int32_t *) result = func->number;
101766779Simp		break;
101866779Simp	case PCCARD_IVAR_VENDOR_STR:
101966779Simp		*(char **) result = sc->card.cis1_info[0];
102066779Simp		break;
102166779Simp	case PCCARD_IVAR_PRODUCT_STR:
102266779Simp		*(char **) result = sc->card.cis1_info[1];
102366779Simp		break;
102466779Simp	case PCCARD_IVAR_CIS3_STR:
102566779Simp		*(char **) result = sc->card.cis1_info[2];
102666779Simp		break;
102767167Simp	case PCCARD_IVAR_CIS4_STR:
1028104610Simp		*(char **) result = sc->card.cis1_info[3];
102967167Simp		break;
103066779Simp	}
103166779Simp	return (0);
103266058Simp}
103366058Simp
103466779Simpstatic void
103566779Simppccard_driver_added(device_t dev, driver_t *driver)
103666779Simp{
103782378Sjon	struct pccard_softc *sc = PCCARD_SOFTC(dev);
103882378Sjon	struct pccard_function *pf;
103982378Sjon	device_t child;
104082378Sjon
104182378Sjon	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
104282378Sjon		if (STAILQ_EMPTY(&pf->cfe_head))
104382378Sjon			continue;
104482378Sjon		child = pf->dev;
104582378Sjon		if (device_get_state(child) != DS_NOTPRESENT)
104682378Sjon			continue;
104782378Sjon		if (pccard_function_enable(pf) == 0 &&
104882378Sjon		    device_probe_and_attach(child) == 0) {
104982378Sjon			DEVPRINTF((sc->dev, "function %d CCR at %d "
105082378Sjon			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
105182378Sjon			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
105282378Sjon			    pccard_ccr_read(pf, 0x00),
105382378Sjon			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
105482378Sjon			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
105582378Sjon			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
105682378Sjon			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
105782378Sjon		} else {
105886907Simp			if (pf->cfe != NULL)
105986907Simp				pccard_function_disable(pf);
106082378Sjon		}
106182378Sjon	}
106282378Sjon	return;
106366779Simp}
106466058Simp
106567242Simpstatic struct resource *
106667242Simppccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
106767242Simp    u_long start, u_long end, u_long count, u_int flags)
106867242Simp{
106982378Sjon	struct pccard_ivar *dinfo;
107082378Sjon	struct resource_list_entry *rle = 0;
107182378Sjon	int passthrough = (device_get_parent(child) != dev);
1072104641Simp	struct resource *r = NULL;
107367242Simp
107482378Sjon	if (passthrough) {
107582378Sjon		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
107682378Sjon		    type, rid, start, end, count, flags));
107782378Sjon	}
107870715Sjon
107982378Sjon	dinfo = device_get_ivars(child);
108082378Sjon	rle = resource_list_find(&dinfo->resources, type, *rid);
108170715Sjon
1082104641Simp	if (rle == NULL)
1083104641Simp		return (NULL);		/* no resource of that type/rid */
108470715Sjon
1085104641Simp	if (rle->res == NULL) {
108682378Sjon		switch(type) {
108782378Sjon		case SYS_RES_IOPORT:
1088104641Simp			r = bus_alloc_resource(dev, type, rid, start, end,
1089104641Simp			    count, rman_make_alignment_flags(count));
1090104641Simp			if (r == NULL)
1091104641Simp				goto bad;
1092104641Simp			resource_list_add(&dinfo->resources, type, *rid,
1093104641Simp			    rman_get_start(r), rman_get_end(r), count);
1094104641Simp			rle = resource_list_find(&dinfo->resources, type, *rid);
1095104641Simp			if (!rle)
1096104641Simp				goto bad;
1097104641Simp			rle->res = r;
1098104641Simp			break;
109982378Sjon		case SYS_RES_MEMORY:
110082378Sjon			break;
110182378Sjon		case SYS_RES_IRQ:
110282378Sjon			break;
110382378Sjon		}
110490897Simp		return (rle->res);
110567269Simp	}
1106113242Simp	if (rman_get_device(rle->res) != dev)
1107104641Simp		return (NULL);
1108104641Simp	bus_release_resource(dev, type, *rid, rle->res);
1109104641Simp	rle->res = NULL;
1110104641Simp	switch(type) {
1111104641Simp	case SYS_RES_IOPORT:
1112104641Simp	case SYS_RES_MEMORY:
1113104641Simp		if (!(flags & RF_ALIGNMENT_MASK))
1114104641Simp			flags |= rman_make_alignment_flags(rle->count);
1115104641Simp		break;
1116104641Simp	case SYS_RES_IRQ:
1117104641Simp		flags |= RF_SHAREABLE;
1118104641Simp		break;
1119104641Simp	}
1120104641Simp	rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1121104641Simp	    type, rid, rle->start, rle->end, rle->count, flags);
1122104641Simp	return (rle->res);
1123104641Simpbad:;
1124104641Simp	device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1125104641Simp	return (NULL);
112667242Simp}
112767242Simp
112867242Simpstatic int
112967242Simppccard_release_resource(device_t dev, device_t child, int type, int rid,
113067242Simp    struct resource *r)
113167242Simp{
113282378Sjon	struct pccard_ivar *dinfo;
113382378Sjon	int passthrough = (device_get_parent(child) != dev);
113482378Sjon	struct resource_list_entry *rle = 0;
113582378Sjon	int ret;
113682378Sjon	int flags;
113770715Sjon
113882378Sjon	if (passthrough)
113982378Sjon		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
114082378Sjon		    type, rid, r);
114170715Sjon
114282378Sjon	dinfo = device_get_ivars(child);
114370715Sjon
114482378Sjon	rle = resource_list_find(&dinfo->resources, type, rid);
114570715Sjon
114682378Sjon	if (!rle) {
114782378Sjon		device_printf(dev, "Allocated resource not found, "
114882378Sjon		    "%d %x %lx %lx\n",
114982378Sjon		    type, rid, rman_get_start(r), rman_get_size(r));
115082378Sjon		return ENOENT;
115170715Sjon	}
115282378Sjon	if (!rle->res) {
115382378Sjon		device_printf(dev, "Allocated resource not recorded\n");
115482378Sjon		return ENOENT;
115570715Sjon	}
115670715Sjon
115782378Sjon	ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
115882378Sjon	    type, rid, r);
115982378Sjon	switch(type) {
116082378Sjon	case SYS_RES_IOPORT:
116182378Sjon	case SYS_RES_MEMORY:
116282378Sjon		flags = rman_make_alignment_flags(rle->count);
116382378Sjon		break;
116482378Sjon	case SYS_RES_IRQ:
116582378Sjon		flags = RF_SHAREABLE;
116682378Sjon		break;
116782378Sjon	default:
116882378Sjon		flags = 0;
116970715Sjon	}
117082378Sjon	rle->res = bus_alloc_resource(dev, type, &rid,
117182378Sjon	    rle->start, rle->end, rle->count, flags);
117282378Sjon	if (rle->res == NULL)
117382378Sjon		device_printf(dev, "release_resource: "
117482378Sjon		    "unable to reaquire resource\n");
117582378Sjon	return ret;
117667242Simp}
117767242Simp
117867333Simpstatic void
117967333Simppccard_child_detached(device_t parent, device_t dev)
118067333Simp{
118167333Simp	struct pccard_ivar *ivar = PCCARD_IVAR(dev);
118282378Sjon	struct pccard_function *pf = ivar->fcn;
118367333Simp
118482378Sjon	pccard_function_disable(pf);
118567333Simp}
118667333Simp
118770715Sjonstatic void
118870762Simppccard_intr(void *arg)
118970762Simp{
119082378Sjon	struct pccard_function *pf = (struct pccard_function*) arg;
119182378Sjon	int reg;
1192102923Simp	int doisr = 1;
119382378Sjon
119482383Simp	/*
1195102923Simp	 * MFC cards know if they interrupted, so we have to ack the
1196102923Simp	 * interrupt and call the ISR.  Non-MFC cards don't have these
1197102923Simp	 * bits, so they always get called.  Many non-MFC cards have
1198102923Simp	 * this bit set always upon read, but some do not.
1199102923Simp	 *
1200102923Simp	 * We always ack the interrupt, even if there's no ISR
1201102923Simp	 * for the card.  This is done on the theory that acking
1202102923Simp	 * the interrupt will pacify the card enough to keep an
1203102923Simp	 * interrupt storm from happening.  Of course this won't
1204102923Simp	 * help in the non-MFC case.
1205116311Simp	 *
1206116311Simp	 * This has no impact for MPSAFEness of the client drivers.
1207116311Simp	 * We register this with whatever flags the intr_handler
1208116311Simp	 * was registered with.  All these functions are MPSAFE.
120982383Simp	 */
1210102923Simp	if (pccard_mfc(pf->sc)) {
1211102923Simp		reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1212102923Simp		if (reg & PCCARD_CCR_STATUS_INTR)
1213102923Simp			pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1214102923Simp			    reg & ~PCCARD_CCR_STATUS_INTR);
1215102923Simp		else
1216102923Simp			doisr = 0;
1217102923Simp	}
1218102923Simp	if (pf->intr_handler != NULL && doisr)
121982378Sjon		pf->intr_handler(pf->intr_handler_arg);
122070715Sjon}
122170715Sjon
122270715Sjonstatic int
122370762Simppccard_setup_intr(device_t dev, device_t child, struct resource *irq,
122470762Simp    int flags, driver_intr_t *intr, void *arg, void **cookiep)
122570715Sjon{
1226102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
122770715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
122870715Sjon	struct pccard_function *func = ivar->fcn;
122990445Simp	int err;
123070715Sjon
123170715Sjon	if (func->intr_handler != NULL)
1232101762Simp		panic("Only one interrupt handler per function allowed");
123390445Simp	err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
123490445Simp	    func, cookiep);
123590445Simp	if (err != 0)
123690445Simp		return (err);
123770715Sjon	func->intr_handler = intr;
123870715Sjon	func->intr_handler_arg = arg;
123982378Sjon	func->intr_handler_cookie = *cookiep;
1240102713Simp	if (pccard_mfc(sc)) {
1241102713Simp		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1242102713Simp		    pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1243102713Simp		    PCCARD_CCR_OPTION_IREQ_ENABLE);
1244102713Simp	}
124574632Simp	return (0);
124670715Sjon}
124770715Sjon
124870715Sjonstatic int
124970762Simppccard_teardown_intr(device_t dev, device_t child, struct resource *r,
125070762Simp    void *cookie)
125170715Sjon{
1252102713Simp	struct pccard_softc *sc = PCCARD_SOFTC(dev);
125370715Sjon	struct pccard_ivar *ivar = PCCARD_IVAR(child);
125470715Sjon	struct pccard_function *func = ivar->fcn;
125582378Sjon	int ret;
125670715Sjon
1257102713Simp	if (pccard_mfc(sc)) {
1258102713Simp		pccard_ccr_write(func, PCCARD_CCR_OPTION,
1259102713Simp		    pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1260102713Simp		    ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1261102713Simp	}
126290445Simp	ret = bus_generic_teardown_intr(dev, child, r, cookie);
126382378Sjon	if (ret == 0) {
126482378Sjon		func->intr_handler = NULL;
126582378Sjon		func->intr_handler_arg = NULL;
126682378Sjon		func->intr_handler_cookie = NULL;
126782378Sjon	}
126870715Sjon
126982378Sjon	return (ret);
127070715Sjon}
127170715Sjon
127252506Simpstatic device_method_t pccard_methods[] = {
127352506Simp	/* Device interface */
127452506Simp	DEVMETHOD(device_probe,		pccard_probe),
127559193Simp	DEVMETHOD(device_attach,	pccard_attach),
127682378Sjon	DEVMETHOD(device_detach,	pccard_detach),
127752506Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
127887975Simp	DEVMETHOD(device_suspend,	pccard_suspend),
127987975Simp	DEVMETHOD(device_resume,	pccard_resume),
128052506Simp
128152506Simp	/* Bus interface */
128252506Simp	DEVMETHOD(bus_print_child,	pccard_print_child),
128366779Simp	DEVMETHOD(bus_driver_added,	pccard_driver_added),
128467333Simp	DEVMETHOD(bus_child_detached,	pccard_child_detached),
128567242Simp	DEVMETHOD(bus_alloc_resource,	pccard_alloc_resource),
128667242Simp	DEVMETHOD(bus_release_resource,	pccard_release_resource),
128782378Sjon	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
128882378Sjon	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
128970715Sjon	DEVMETHOD(bus_setup_intr,	pccard_setup_intr),
129070715Sjon	DEVMETHOD(bus_teardown_intr,	pccard_teardown_intr),
129152506Simp	DEVMETHOD(bus_set_resource,	pccard_set_resource),
129252506Simp	DEVMETHOD(bus_get_resource,	pccard_get_resource),
129352506Simp	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
1294104641Simp	DEVMETHOD(bus_probe_nomatch,	pccard_probe_nomatch),
129566058Simp	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
1296104641Simp	DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1297104641Simp	DEVMETHOD(bus_child_location_str, pccard_child_location_str),
129852506Simp
129959193Simp	/* Card Interface */
130059193Simp	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
130159193Simp	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
130259193Simp	DEVMETHOD(card_get_type,	pccard_card_gettype),
130359193Simp	DEVMETHOD(card_attach_card,	pccard_attach_card),
130459193Simp	DEVMETHOD(card_detach_card,	pccard_detach_card),
130574636Simp	DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
130674636Simp	DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
130797613Stakawata	DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
130859193Simp
130952506Simp	{ 0, 0 }
131052506Simp};
131152506Simp
131252506Simpstatic driver_t pccard_driver = {
131352506Simp	"pccard",
131452506Simp	pccard_methods,
131564850Simp	sizeof(struct pccard_softc)
131652506Simp};
131752506Simp
131852506Simpdevclass_t	pccard_devclass;
131952506Simp
1320101905Simp/* Maybe we need to have a slot device? */
132153873SimpDRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
132252506SimpDRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
1323101905SimpDRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
132453873SimpDRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
132564927SimpMODULE_VERSION(pccard, 1);
132670715Sjon/*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/
1327