pccard.c revision 67187
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 67187 2000-10-16 07:51:12Z imp $ */
3
4/*
5 * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37#include <sys/kernel.h>
38#include <sys/queue.h>
39#include <sys/types.h>
40
41#include <sys/bus.h>
42#include <machine/bus.h>
43#include <sys/rman.h>
44#include <machine/resource.h>
45
46#include <dev/pccard/pccardreg.h>
47#include <dev/pccard/pccardvar.h>
48
49#include "power_if.h"
50#include "card_if.h"
51
52#define PCCARDDEBUG
53
54#ifdef PCCARDDEBUG
55int	pccard_debug = 1;
56#define	DPRINTF(arg) if (pccard_debug) printf arg
57#define	DEVPRINTF(arg) if (pccard_debug) device_printf arg
58#else
59#define	DPRINTF(arg)
60#define	DEVPRINTF(arg)
61#endif
62
63#ifdef PCCARDVERBOSE
64int	pccard_verbose = 1;
65#else
66int	pccard_verbose = 0;
67#endif
68
69int	pccard_print(void *, const char *);
70
71int
72pccard_ccr_read(pf, ccr)
73	struct pccard_function *pf;
74	int ccr;
75{
76	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
77	    pf->pf_ccr_offset + ccr));
78}
79
80void
81pccard_ccr_write(pf, ccr, val)
82	struct pccard_function *pf;
83	int ccr;
84	int val;
85{
86
87	if ((pf->ccr_mask) & (1 << (ccr / 2))) {
88		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
89		    pf->pf_ccr_offset + ccr, val);
90	}
91}
92
93static int
94pccard_attach_card(device_t dev)
95{
96	struct pccard_softc *sc = PCCARD_SOFTC(dev);
97	struct pccard_function *pf;
98	struct pccard_ivar *ivar;
99	device_t child;
100	int attached;
101
102	DEVPRINTF((dev, "pccard_card_attach\n"));
103	/*
104	 * this is here so that when socket_enable calls gettype, trt happens
105	 */
106	STAILQ_INIT(&sc->card.pf_head);
107
108	DEVPRINTF((dev, "chip_socket_enable\n"));
109	POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
110
111	DEVPRINTF((dev, "read_cis\n"));
112	pccard_read_cis(sc);
113
114	DEVPRINTF((dev, "check_cis_quirks\n"));
115	pccard_check_cis_quirks(dev);
116
117	/*
118	 * bail now if the card has no functions, or if there was an error in
119	 * the cis.
120	 */
121
122	if (sc->card.error)
123		return (1);
124	if (STAILQ_EMPTY(&sc->card.pf_head))
125		return (1);
126
127	if (1)
128		pccard_print_cis(dev);
129
130	attached = 0;
131
132	DEVPRINTF((dev, "functions scanning\n"));
133	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
134		if (STAILQ_EMPTY(&pf->cfe_head))
135			continue;
136
137		pf->sc = sc;
138		pf->cfe = NULL;
139		pf->dev = NULL;
140	}
141#if 0
142	DEVPRINTF((dev, "chip_socket_disable\n"));
143	POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
144#endif
145
146	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
147		if (STAILQ_EMPTY(&pf->cfe_head))
148			continue;
149		/*
150		 * In NetBSD, the drivers are responsible for activating
151		 * each function of a card.  I think that in FreeBSD we
152		 * want to activate them enough for the usual bus_*_resource
153		 * routines will do the right thing.  This many mean a
154		 * departure from the current NetBSD model.
155		 *
156		 * This could get really ugly for multifunction cards.  But
157		 * it might also just fall out of the FreeBSD resource model.
158		 *
159		 */
160		device_printf(dev, "Starting to attach....\n");
161		ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, M_WAITOK);
162		bzero(ivar, sizeof *ivar);
163		child = device_add_child(dev, NULL, -1);
164		device_set_ivars(child, ivar);
165		ivar->fcn = pf;
166		pf->dev = child;
167		/*
168		 * XXX We might want to move the next two lines into
169		 * XXX the pccard interface layer.  For the moment, this
170		 * XXX is OK, but some drivers want to pick the config
171		 * XXX entry to use as well as some address tweaks (mostly
172		 * XXX due to bugs in decode logic that makes some
173		 * XXX addresses illegal or broken).
174		 */
175		pccard_function_init(pf);
176		pccard_function_enable(pf);
177		device_printf(dev, "pf %p pf->sc %p\n", pf, pf->sc);
178		if (device_probe_and_attach(child) == 0) {
179			attached++;
180
181			DEVPRINTF((sc->dev, "function %d CCR at %d "
182			    "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
183			    pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
184			    pccard_ccr_read(pf, 0x00),
185			pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
186			pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
187			pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
188			pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
189		} else {
190			device_delete_child(dev, child);
191		}
192	}
193	return 0;
194}
195
196static int
197pccard_detach_card(device_t dev, int flags)
198{
199	struct pccard_softc *sc = PCCARD_SOFTC(dev);
200	struct pccard_function *pf;
201
202	/*
203	 * We are running on either the PCCARD socket's event thread
204	 * or in user context detaching a device by user request.
205	 */
206	STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
207		if (STAILQ_FIRST(&pf->cfe_head) == NULL)
208			continue;
209
210		pccard_function_disable(pf);
211		if (pf->dev)
212			device_delete_child(dev, pf->dev);
213	}
214	return 0;
215}
216
217const struct pccard_product *
218pccard_product_lookup(device_t dev, const struct pccard_product *tab,
219    size_t ent_size, pccard_product_match_fn matchfn)
220{
221	const struct pccard_product *ent;
222	int matches;
223	u_int32_t fcn;
224	u_int32_t vendor;
225	u_int32_t prod;
226	char *vendorstr;
227	char *prodstr;
228
229#ifdef DIAGNOSTIC
230	if (sizeof *ent > ent_size)
231		panic("pccard_product_lookup: bogus ent_size %ld",
232		    (long) ent_size);
233#endif
234	if (pccard_get_vendor(dev, &vendor))
235		return (NULL);
236	if (pccard_get_product(dev, &prod))
237		return (NULL);
238	if (pccard_get_function_number(dev, &fcn))
239		return (NULL);
240	if (pccard_get_vendor_str(dev, &vendorstr))
241		return (NULL);
242	if (pccard_get_product_str(dev, &prodstr))
243		return (NULL);
244        for (ent = tab; ent->pp_name != NULL;
245	     ent = (const struct pccard_product *)
246		 ((const char *) ent + ent_size)) {
247		matches = 1;
248		if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
249		    vendor != ent->pp_vendor)
250			matches = 0;
251		if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
252		    prod != ent->pp_product)
253			matches = 0;
254		if (matches && fcn != ent->pp_expfunc)
255			matches = 0;
256		if (matches && ent->pp_vendor_str &&
257		    strcmp(ent->pp_vendor_str, vendorstr) != 0)
258			matches = 0;
259		if (matches && ent->pp_product_str &&
260		    strcmp(ent->pp_product_str, prodstr) != 0)
261			matches = 0;
262		if (matchfn != NULL)
263			matches = (*matchfn)(dev, ent, matches);
264		if (matches)
265			return (ent);
266	}
267	return (NULL);
268}
269
270static int
271pccard_card_gettype(device_t dev, int *type)
272{
273	struct pccard_softc *sc = PCCARD_SOFTC(dev);
274	struct pccard_function *pf;
275
276	/*
277	 * set the iftype to memory if this card has no functions (not yet
278	 * probed), or only one function, and that is not initialized yet or
279	 * that is memory.
280	 */
281	pf = STAILQ_FIRST(&sc->card.pf_head);
282	if (pf == NULL ||
283	    (STAILQ_NEXT(pf, pf_list) == NULL &&
284	    (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
285		*type = PCCARD_IFTYPE_MEMORY;
286	else
287		*type = PCCARD_IFTYPE_IO;
288	return 0;
289}
290
291/*
292 * Initialize a PCCARD function.  May be called as long as the function is
293 * disabled.
294 */
295void
296pccard_function_init(struct pccard_function *pf)
297{
298	struct pccard_config_entry *cfe;
299	int i;
300
301	if (pf->pf_flags & PFF_ENABLED)
302		panic("pccard_function_init: function is enabled");
303
304	/* Remember which configuration entry we are using. */
305	for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
306	    cfe = STAILQ_NEXT(cfe, cfe_list)) {
307		for (i = 0; i < cfe->num_iospace; i++)
308			cfe->iores[i] = NULL;
309		cfe->irqres = NULL;
310		for (i = 0; i < cfe->num_iospace; i++) {
311			/* XXX kludge, need to not ignore start */
312			/* XXX start is a hint here, so this would break */
313			/* XXX modems */
314			cfe->iorid[i] = i;
315			cfe->iores[i] = bus_alloc_resource(pf->dev,
316			    SYS_RES_IOPORT, &cfe->iorid[i], 0x300, 0x3ff,
317			    cfe->iospace[i].length, 0);
318			if (cfe->iores[i] == 0)
319				goto not_this_one;
320
321		}
322		if (cfe->num_memspace > 0) {
323			goto not_this_one;
324		}
325		if (cfe->irqmask) {
326			cfe->irqrid = 0;
327			cfe->irqres = bus_alloc_resource(pf->dev, SYS_RES_IRQ,
328			    &cfe->irqrid, 10, 12, 1, 0);
329			if (cfe->irqres == 0)
330				goto not_this_one;
331		}
332		/* XXX Don't know how to deal with maxtwins */
333		/* If we get to here, we've allocated all we need */
334		pf->cfe = cfe;
335		break;
336	    not_this_one:;
337		for (i = 0; i < cfe->num_iospace; i++) {
338			if (cfe->iores[i])
339				bus_release_resource(pf->dev, SYS_RES_IOPORT,
340				    cfe->iorid[i], cfe->iores[i]);
341			cfe->iores[i] = NULL;
342		}
343		if (cfe->irqmask && cfe->irqres) {
344			bus_release_resource(pf->dev, SYS_RES_IRQ,
345			    cfe->irqrid, cfe->irqres);
346			cfe->irqres = NULL;
347		}
348	}
349}
350
351/* Enable a PCCARD function */
352int
353pccard_function_enable(struct pccard_function *pf)
354{
355	struct pccard_function *tmp;
356	int reg;
357	device_t dev = pf->sc->dev;
358
359	if (pf->cfe == NULL)
360		panic("pccard_function_enable: function not initialized");
361
362	/*
363	 * Increase the reference count on the socket, enabling power, if
364	 * necessary.
365	 */
366	if (pf->sc->sc_enabled_count++ == 0)
367		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
368	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
369
370	if (pf->pf_flags & PFF_ENABLED) {
371		/*
372		 * Don't do anything if we're already enabled.
373		 */
374		return (0);
375	}
376
377	/*
378	 * it's possible for different functions' CCRs to be in the same
379	 * underlying page.  Check for that.
380	 */
381	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
382		if ((tmp->pf_flags & PFF_ENABLED) &&
383		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
384		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
385		     (tmp->ccr_base - tmp->pf_ccr_offset +
386		      tmp->pf_ccr_realsize))) {
387			pf->pf_ccrt = tmp->pf_ccrt;
388			pf->pf_ccrh = tmp->pf_ccrh;
389			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
390
391			/*
392			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
393			 * tmp->ccr_base) + pf->ccr_base;
394			 */
395			pf->pf_ccr_offset =
396			    (tmp->pf_ccr_offset + pf->ccr_base) -
397			    tmp->ccr_base;
398			pf->pf_ccr_window = tmp->pf_ccr_window;
399			break;
400		}
401	}
402
403	if (tmp == NULL) {
404		pf->ccr_rid = 0;
405		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
406		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
407		if (!pf->ccr_res) {
408			DEVPRINTF((dev, "ccr_res == 0\n"));
409			goto bad;
410		}
411		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
412		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
413		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
414		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
415		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
416		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
417		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
418		pf->pf_ccr_realsize = 1;
419	}
420
421	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
422	reg |= PCCARD_CCR_OPTION_LEVIREQ;
423	if (pccard_mfc(pf->sc)) {
424		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
425			PCCARD_CCR_OPTION_ADDR_DECODE);
426		/*
427		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
428		 * XXX we have an interrupt handler, but we don't know that
429		 * XXX at this point.
430		 */
431#if 0
432		if (pf->ih_fct)
433			reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
434#endif
435	}
436	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
437
438	reg = 0;
439	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
440		reg |= PCCARD_CCR_STATUS_IOIS8;
441	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
442		reg |= PCCARD_CCR_STATUS_AUDIO;
443	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
444
445	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
446
447	if (pccard_mfc(pf->sc)) {
448		long tmp, iosize;
449
450		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
451		/* round up to nearest (2^n)-1 */
452		for (iosize = 1; iosize < tmp; iosize <<= 1)
453			;
454		iosize--;
455
456		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
457				 pf->pf_mfc_iobase & 0xff);
458		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
459				 (pf->pf_mfc_iobase >> 8) & 0xff);
460		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
461		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
462
463		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
464	}
465
466#ifdef PCCARDDEBUG
467	if (pccard_debug) {
468		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
469			device_printf(tmp->sc->dev,
470			    "function %d CCR at %d offset %x: "
471			    "%x %x %x %x, %x %x %x %x, %x\n",
472			    tmp->number, tmp->pf_ccr_window,
473			    tmp->pf_ccr_offset,
474			    pccard_ccr_read(tmp, 0x00),
475			    pccard_ccr_read(tmp, 0x02),
476			    pccard_ccr_read(tmp, 0x04),
477			    pccard_ccr_read(tmp, 0x06),
478			    pccard_ccr_read(tmp, 0x0A),
479			    pccard_ccr_read(tmp, 0x0C),
480			    pccard_ccr_read(tmp, 0x0E),
481			    pccard_ccr_read(tmp, 0x10),
482			    pccard_ccr_read(tmp, 0x12));
483		}
484	}
485#endif
486	pf->pf_flags |= PFF_ENABLED;
487	return (0);
488
489 bad:
490	/*
491	 * Decrement the reference count, and power down the socket, if
492	 * necessary.
493	 */
494	if (--pf->sc->sc_enabled_count == 0)
495		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
496	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
497
498	return (1);
499}
500
501/* Disable PCCARD function. */
502void
503pccard_function_disable(struct pccard_function *pf)
504{
505	struct pccard_function *tmp;
506	device_t dev = pf->sc->dev;
507
508	if (pf->cfe == NULL)
509		panic("pccard_function_disable: function not initialized");
510
511	if ((pf->pf_flags & PFF_ENABLED) == 0) {
512		/*
513		 * Don't do anything if we're already disabled.
514		 */
515		return;
516	}
517
518	/*
519	 * it's possible for different functions' CCRs to be in the same
520	 * underlying page.  Check for that.  Note we mark us as disabled
521	 * first to avoid matching ourself.
522	 */
523
524	pf->pf_flags &= ~PFF_ENABLED;
525	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
526		if ((tmp->pf_flags & PFF_ENABLED) &&
527		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
528		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
529		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
530			break;
531	}
532
533	/* Not used by anyone else; unmap the CCR. */
534	if (tmp == NULL) {
535		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
536		    pf->ccr_res);
537		pf->ccr_res = NULL;
538	}
539
540	/*
541	 * Decrement the reference count, and power down the socket, if
542	 * necessary.
543	 */
544	if (--pf->sc->sc_enabled_count == 0)
545		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
546	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
547}
548
549#if 0
550/* XXX These functions are needed, but not like this XXX */
551int
552pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
553    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
554{
555	int reg;
556
557	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
558	    pcihp, windowp))
559		return (1);
560
561	/*
562	 * XXX in the multifunction multi-iospace-per-function case, this
563	 * needs to cooperate with io_alloc to make sure that the spaces
564	 * don't overlap, and that the ccr's are set correctly
565	 */
566
567	if (pccard_mfc(pf->sc)) {
568		long tmp, iosize;
569
570		if (pf->pf_mfc_iomax == 0) {
571			pf->pf_mfc_iobase = pcihp->addr + offset;
572			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
573		} else {
574			/* this makes the assumption that nothing overlaps */
575			if (pf->pf_mfc_iobase > pcihp->addr + offset)
576				pf->pf_mfc_iobase = pcihp->addr + offset;
577			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
578				pf->pf_mfc_iomax = pcihp->addr + offset + size;
579		}
580
581		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
582		/* round up to nearest (2^n)-1 */
583		for (iosize = 1; iosize >= tmp; iosize <<= 1)
584			;
585		iosize--;
586
587		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
588		    pf->pf_mfc_iobase & 0xff);
589		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
590		    (pf->pf_mfc_iobase >> 8) & 0xff);
591		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
592		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
593
594		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
595
596		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
597		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
598		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
599	}
600	return (0);
601}
602
603void
604pccard_io_unmap(struct pccard_function *pf, int window)
605{
606
607	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
608
609	/* XXX Anything for multi-function cards? */
610}
611#endif
612
613/*
614 * simulate the old "probe" routine.  In the new world order, the driver
615 * needs to grab devices while in the old they were assigned to the device by
616 * the pccardd process.  These symbols are exported to the upper layers.
617 */
618int
619pccard_compat_probe(device_t dev)
620{
621	return (CARD_COMPAT_MATCH(dev));
622}
623
624int
625pccard_compat_attach(device_t dev)
626{
627	int err;
628
629	err = CARD_COMPAT_PROBE(dev);
630	if (err == 0)
631		err = CARD_COMPAT_ATTACH(dev);
632	return (err);
633}
634
635#define PCCARD_NPORT	2
636#define PCCARD_NMEM	5
637#define PCCARD_NIRQ	1
638#define PCCARD_NDRQ	0
639
640static int
641pccard_add_children(device_t dev, int busno)
642{
643	/* Call parent to scan for any current children */
644	return 0;
645}
646
647static int
648pccard_probe(device_t dev)
649{
650	device_set_desc(dev, "PC Card bus -- newconfig version");
651	return pccard_add_children(dev, device_get_unit(dev));
652}
653
654static int
655pccard_attach(device_t dev)
656{
657	struct pccard_softc *sc = PCCARD_SOFTC(dev);
658
659	sc->dev = dev;
660	sc->sc_enabled_count = 0;
661	DEVPRINTF((dev, "pccard_attach %p\n", dev));
662	return bus_generic_attach(dev);
663}
664
665static void
666pccard_print_resources(struct resource_list *rl, const char *name, int type,
667    int count, const char *format)
668{
669	struct resource_list_entry *rle;
670	int printed;
671	int i;
672
673	printed = 0;
674	for (i = 0; i < count; i++) {
675		rle = resource_list_find(rl, type, i);
676		if (rle) {
677			if (printed == 0)
678				printf(" %s ", name);
679			else if (printed > 0)
680				printf(",");
681			printed++;
682			printf(format, rle->start);
683			if (rle->count > 1) {
684				printf("-");
685				printf(format, rle->start + rle->count - 1);
686			}
687		} else if (i > 3) {
688			/* check the first few regardless */
689			break;
690		}
691	}
692}
693
694static int
695pccard_print_child(device_t dev, device_t child)
696{
697	struct pccard_ivar *devi = PCCARD_IVAR(child);
698	struct resource_list *rl = &devi->resources;
699	int retval = 0;
700
701	retval += bus_print_child_header(dev, child);
702	retval += printf(" at");
703
704	if (devi) {
705		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
706		    PCCARD_NPORT, "%#lx");
707		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
708		    PCCARD_NMEM, "%#lx");
709		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
710		    "%ld");
711		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
712		    "%ld");
713	}
714
715	retval += bus_print_child_footer(dev, child);
716
717	return (retval);
718}
719
720static int
721pccard_set_resource(device_t dev, device_t child, int type, int rid,
722		 u_long start, u_long count)
723{
724	struct pccard_ivar *devi = PCCARD_IVAR(child);
725	struct resource_list *rl = &devi->resources;
726
727	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
728	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
729		return EINVAL;
730	if (rid < 0)
731		return EINVAL;
732	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
733		return EINVAL;
734	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
735		return EINVAL;
736	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
737		return EINVAL;
738	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
739		return EINVAL;
740
741	resource_list_add(rl, type, rid, start, start + count - 1, count);
742
743	return 0;
744}
745
746static int
747pccard_get_resource(device_t dev, device_t child, int type, int rid,
748    u_long *startp, u_long *countp)
749{
750	struct pccard_ivar *devi = PCCARD_IVAR(child);
751	struct resource_list *rl = &devi->resources;
752	struct resource_list_entry *rle;
753
754	rle = resource_list_find(rl, type, rid);
755	if (!rle)
756		return ENOENT;
757
758	if (startp)
759		*startp = rle->start;
760	if (countp)
761		*countp = rle->count;
762
763	return 0;
764}
765
766static void
767pccard_delete_resource(device_t dev, device_t child, int type, int rid)
768{
769	struct pccard_ivar *devi = PCCARD_IVAR(child);
770	struct resource_list *rl = &devi->resources;
771	resource_list_delete(rl, type, rid);
772}
773
774static int
775pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
776    u_int32_t flags)
777{
778	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
779	    rid, flags);
780}
781
782static int
783pccard_set_memory_offset(device_t dev, device_t child, int rid,
784     u_int32_t offset)
785{
786	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
787	    offset);
788}
789
790static int
791pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
792{
793	struct pccard_ivar *devi = PCCARD_IVAR(child);
794	struct pccard_function *func = devi->fcn;
795	struct pccard_softc *sc = PCCARD_SOFTC(bus);
796
797	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
798	switch (which) {
799	default:
800	case PCCARD_IVAR_ETHADDR:
801		return (ENOENT);
802		break;
803	case PCCARD_IVAR_VENDOR:
804		*(u_int32_t *) result = sc->card.manufacturer;
805		break;
806	case PCCARD_IVAR_PRODUCT:
807		*(u_int32_t *) result = sc->card.product;
808		break;
809	case PCCARD_IVAR_FUNCTION_NUMBER:
810		if (!func) {
811			device_printf(bus, "No function number, bug!\n");
812			return (ENOENT);
813		}
814		*(u_int32_t *) result = func->number;
815		break;
816	case PCCARD_IVAR_VENDOR_STR:
817		*(char **) result = sc->card.cis1_info[0];
818		break;
819	case PCCARD_IVAR_PRODUCT_STR:
820		*(char **) result = sc->card.cis1_info[1];
821		break;
822	case PCCARD_IVAR_CIS3_STR:
823		*(char **) result = sc->card.cis1_info[2];
824		break;
825	case PCCARD_IVAR_CIS4_STR:
826		*(char **) result = sc->card.cis1_info[2];
827		break;
828	}
829	return (0);
830}
831
832static void
833pccard_driver_added(device_t dev, driver_t *driver)
834{
835	/*
836	 * XXX eventually we need to attach stuff when we know we
837	 * XXX have kids.  For now we do nothing because we normally
838	 * XXX add children ourselves.  We don't want to necessarily
839	 * XXX force a reprobe.
840	 */
841}
842
843static device_method_t pccard_methods[] = {
844	/* Device interface */
845	DEVMETHOD(device_probe,		pccard_probe),
846	DEVMETHOD(device_attach,	pccard_attach),
847	DEVMETHOD(device_detach,	bus_generic_detach),
848	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
849	DEVMETHOD(device_suspend,	bus_generic_suspend),
850	DEVMETHOD(device_resume,	bus_generic_resume),
851
852	/* Bus interface */
853	DEVMETHOD(bus_print_child,	pccard_print_child),
854	DEVMETHOD(bus_driver_added,	pccard_driver_added),
855	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
856	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
857	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
858	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
859	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
860	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
861	DEVMETHOD(bus_set_resource,	pccard_set_resource),
862	DEVMETHOD(bus_get_resource,	pccard_get_resource),
863	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
864	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
865
866	/* Card Interface */
867	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
868	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
869	DEVMETHOD(card_get_type,	pccard_card_gettype),
870	DEVMETHOD(card_attach_card,	pccard_attach_card),
871	DEVMETHOD(card_detach_card,	pccard_detach_card),
872
873	{ 0, 0 }
874};
875
876static driver_t pccard_driver = {
877	"pccard",
878	pccard_methods,
879	sizeof(struct pccard_softc)
880};
881
882devclass_t	pccard_devclass;
883
884DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
885DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
886DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
887DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
888MODULE_VERSION(pccard, 1);
889MODULE_DEPEND(pccard, pcic, 1, 1, 1);
890