pccard.c revision 67167
1/*	$NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $	*/
2/* $FreeBSD: head/sys/dev/pccard/pccard.c 67167 2000-10-15 17:21:50Z 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		/*
167		 * XXX We might want to move the next two lines into
168		 * XXX the pccard interface layer.  For the moment, this
169		 * XXX is OK, but some drivers want to pick the config
170		 * XXX entry to use as well as some address tweaks (mostly
171		 * XXX due to bugs in decode logic that makes some
172		 * XXX addresses illegal or broken).
173		 */
174		pccard_function_init(pf);
175		pccard_function_enable(pf);
176		device_printf(dev, "pf %p pf->sc %p\n", pf, pf->sc);
177		if (device_probe_and_attach(child) == 0) {
178			attached++;
179			pf->dev = child;
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
300	if (pf->pf_flags & PFF_ENABLED)
301		panic("pccard_function_init: function is enabled");
302
303	/* Remember which configuration entry we are using. */
304	/* XXX
305	 * need to look for one we can allocate the resources
306	 * and then set them so the alloc_resources work later.
307	 */
308	for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
309	    cfe = STAILQ_NEXT(cfe, cfe_list)) {
310		pf->cfe = cfe;
311		/*
312		 * XXX Need to try to allocate resources for this cfe and
313		 * XXX if we succeed in getting ALL of them, we will break
314		 * XXX out of the loop and use the reset of the resource
315		 * XXX mechanism to make sure that the values that we get
316		 * XXX here to work later.
317		 */
318		printf("%d: f %d type %d iospace %d 0x%lx-0x%lx mask 0x%lx memspace %d 0x%lx-0x%lx irqmask 0x%x\n",
319		    cfe->number, cfe->flags, cfe->iftype,
320		    cfe->num_iospace, cfe->iospace[0].start,
321		    cfe->iospace[0].start + cfe->iospace[0].length - 1,
322		    cfe->iomask,
323		    cfe->num_memspace, cfe->memspace[0].hostaddr,
324		    cfe->memspace[0].hostaddr + cfe->memspace[0].length - 1,
325		    cfe->irqmask);
326	}
327}
328
329/* Enable a PCCARD function */
330int
331pccard_function_enable(struct pccard_function *pf)
332{
333	struct pccard_function *tmp;
334	int reg;
335	device_t dev = pf->sc->dev;
336
337	if (pf->cfe == NULL)
338		panic("pccard_function_enable: function not initialized");
339
340	/*
341	 * Increase the reference count on the socket, enabling power, if
342	 * necessary.
343	 */
344	if (pf->sc->sc_enabled_count++ == 0)
345		POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
346	DEVPRINTF((dev, "++enabled_count = %d\n", pf->sc->sc_enabled_count));
347
348	if (pf->pf_flags & PFF_ENABLED) {
349		/*
350		 * Don't do anything if we're already enabled.
351		 */
352		return (0);
353	}
354
355	/*
356	 * it's possible for different functions' CCRs to be in the same
357	 * underlying page.  Check for that.
358	 */
359	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
360		if ((tmp->pf_flags & PFF_ENABLED) &&
361		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
362		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
363		     (tmp->ccr_base - tmp->pf_ccr_offset +
364		      tmp->pf_ccr_realsize))) {
365			pf->pf_ccrt = tmp->pf_ccrt;
366			pf->pf_ccrh = tmp->pf_ccrh;
367			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
368
369			/*
370			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
371			 * tmp->ccr_base) + pf->ccr_base;
372			 */
373			pf->pf_ccr_offset =
374			    (tmp->pf_ccr_offset + pf->ccr_base) -
375			    tmp->ccr_base;
376			pf->pf_ccr_window = tmp->pf_ccr_window;
377			break;
378		}
379	}
380
381	if (tmp == NULL) {
382		pf->ccr_rid = 0;
383		pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
384		    &pf->ccr_rid, 0xa0000, 0xdffff, 1 << 10, RF_ACTIVE);
385		if (!pf->ccr_res) {
386			DEVPRINTF((dev, "ccr_res == 0\n"));
387			goto bad;
388		}
389		CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
390		    pf->ccr_rid, PCCARD_A_MEM_ATTR);
391		CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
392		    pf->ccr_rid, (pf->ccr_rid >> 10) << 10);
393		pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
394		pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
395		pf->pf_ccr_offset = rman_get_start(pf->ccr_res);
396		pf->pf_ccr_realsize = 1;
397	}
398
399	reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
400	reg |= PCCARD_CCR_OPTION_LEVIREQ;
401	if (pccard_mfc(pf->sc)) {
402		reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
403			PCCARD_CCR_OPTION_ADDR_DECODE);
404		/*
405		 * XXX Need to enable PCCARD_CCR_OPTION_IRQ_ENABLE if
406		 * XXX we have an interrupt handler, but we don't know that
407		 * XXX at this point.
408		 */
409#if 0
410		if (pf->ih_fct)
411			reg |= PCCARD_CCR_OPTION_IREQ_ENABLE;
412#endif
413	}
414	pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
415
416	reg = 0;
417	if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
418		reg |= PCCARD_CCR_STATUS_IOIS8;
419	if (pf->cfe->flags & PCCARD_CFE_AUDIO)
420		reg |= PCCARD_CCR_STATUS_AUDIO;
421	pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
422
423	pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
424
425	if (pccard_mfc(pf->sc)) {
426		long tmp, iosize;
427
428		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
429		/* round up to nearest (2^n)-1 */
430		for (iosize = 1; iosize < tmp; iosize <<= 1)
431			;
432		iosize--;
433
434		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
435				 pf->pf_mfc_iobase & 0xff);
436		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
437				 (pf->pf_mfc_iobase >> 8) & 0xff);
438		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
439		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
440
441		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
442	}
443
444#ifdef PCCARDDEBUG
445	if (pccard_debug) {
446		STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
447			device_printf(tmp->sc->dev,
448			    "function %d CCR at %d offset %x: "
449			    "%x %x %x %x, %x %x %x %x, %x\n",
450			    tmp->number, tmp->pf_ccr_window,
451			    tmp->pf_ccr_offset,
452			    pccard_ccr_read(tmp, 0x00),
453			    pccard_ccr_read(tmp, 0x02),
454			    pccard_ccr_read(tmp, 0x04),
455			    pccard_ccr_read(tmp, 0x06),
456			    pccard_ccr_read(tmp, 0x0A),
457			    pccard_ccr_read(tmp, 0x0C),
458			    pccard_ccr_read(tmp, 0x0E),
459			    pccard_ccr_read(tmp, 0x10),
460			    pccard_ccr_read(tmp, 0x12));
461		}
462	}
463#endif
464	pf->pf_flags |= PFF_ENABLED;
465	return (0);
466
467 bad:
468	/*
469	 * Decrement the reference count, and power down the socket, if
470	 * necessary.
471	 */
472	if (--pf->sc->sc_enabled_count == 0)
473		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
474	DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
475
476	return (1);
477}
478
479/* Disable PCCARD function. */
480void
481pccard_function_disable(struct pccard_function *pf)
482{
483	struct pccard_function *tmp;
484	device_t dev = pf->sc->dev;
485
486	if (pf->cfe == NULL)
487		panic("pccard_function_disable: function not initialized");
488
489	if ((pf->pf_flags & PFF_ENABLED) == 0) {
490		/*
491		 * Don't do anything if we're already disabled.
492		 */
493		return;
494	}
495
496	/*
497	 * it's possible for different functions' CCRs to be in the same
498	 * underlying page.  Check for that.  Note we mark us as disabled
499	 * first to avoid matching ourself.
500	 */
501
502	pf->pf_flags &= ~PFF_ENABLED;
503	STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
504		if ((tmp->pf_flags & PFF_ENABLED) &&
505		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
506		    ((pf->ccr_base + PCCARD_CCR_SIZE) <=
507		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
508			break;
509	}
510
511	/* Not used by anyone else; unmap the CCR. */
512	if (tmp == NULL) {
513		bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
514		    pf->ccr_res);
515		pf->ccr_res = NULL;
516	}
517
518	/*
519	 * Decrement the reference count, and power down the socket, if
520	 * necessary.
521	 */
522	if (--pf->sc->sc_enabled_count == 0)
523		POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
524	DEVPRINTF((dev, "--enabled_count = %d\n", pf->sc->sc_enabled_count));
525}
526
527#if 0
528/* XXX These functions are needed, but not like this XXX */
529int
530pccard_io_map(struct pccard_function *pf, int width, bus_addr_t offset,
531    bus_size_t size, struct pccard_io_handle *pcihp, int *windowp)
532{
533	int reg;
534
535	if (pccard_chip_io_map(pf->sc->pct, pf->sc->pch, width, offset, size,
536	    pcihp, windowp))
537		return (1);
538
539	/*
540	 * XXX in the multifunction multi-iospace-per-function case, this
541	 * needs to cooperate with io_alloc to make sure that the spaces
542	 * don't overlap, and that the ccr's are set correctly
543	 */
544
545	if (pccard_mfc(pf->sc)) {
546		long tmp, iosize;
547
548		if (pf->pf_mfc_iomax == 0) {
549			pf->pf_mfc_iobase = pcihp->addr + offset;
550			pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
551		} else {
552			/* this makes the assumption that nothing overlaps */
553			if (pf->pf_mfc_iobase > pcihp->addr + offset)
554				pf->pf_mfc_iobase = pcihp->addr + offset;
555			if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
556				pf->pf_mfc_iomax = pcihp->addr + offset + size;
557		}
558
559		tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
560		/* round up to nearest (2^n)-1 */
561		for (iosize = 1; iosize >= tmp; iosize <<= 1)
562			;
563		iosize--;
564
565		pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
566		    pf->pf_mfc_iobase & 0xff);
567		pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
568		    (pf->pf_mfc_iobase >> 8) & 0xff);
569		pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
570		pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
571
572		pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
573
574		reg = pccard_ccr_read(pf, PCCARD_CCR_OPTION);
575		reg |= PCCARD_CCR_OPTION_ADDR_DECODE;
576		pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
577	}
578	return (0);
579}
580
581void
582pccard_io_unmap(struct pccard_function *pf, int window)
583{
584
585	pccard_chip_io_unmap(pf->sc->pct, pf->sc->pch, window);
586
587	/* XXX Anything for multi-function cards? */
588}
589#endif
590
591/*
592 * simulate the old "probe" routine.  In the new world order, the driver
593 * needs to grab devices while in the old they were assigned to the device by
594 * the pccardd process.  These symbols are exported to the upper layers.
595 */
596int
597pccard_compat_probe(device_t dev)
598{
599	return (CARD_COMPAT_MATCH(dev));
600}
601
602int
603pccard_compat_attach(device_t dev)
604{
605	int err;
606
607	err = CARD_COMPAT_PROBE(dev);
608	if (err == 0)
609		err = CARD_COMPAT_ATTACH(dev);
610	return (err);
611}
612
613#define PCCARD_NPORT	2
614#define PCCARD_NMEM	5
615#define PCCARD_NIRQ	1
616#define PCCARD_NDRQ	0
617
618static int
619pccard_add_children(device_t dev, int busno)
620{
621	/* Call parent to scan for any current children */
622	return 0;
623}
624
625static int
626pccard_probe(device_t dev)
627{
628	device_set_desc(dev, "PC Card bus -- newconfig version");
629	return pccard_add_children(dev, device_get_unit(dev));
630}
631
632static int
633pccard_attach(device_t dev)
634{
635	struct pccard_softc *sc = PCCARD_SOFTC(dev);
636
637	sc->dev = dev;
638	sc->sc_enabled_count = 0;
639	DEVPRINTF((dev, "pccard_attach %p\n", dev));
640	return bus_generic_attach(dev);
641}
642
643static void
644pccard_print_resources(struct resource_list *rl, const char *name, int type,
645    int count, const char *format)
646{
647	struct resource_list_entry *rle;
648	int printed;
649	int i;
650
651	printed = 0;
652	for (i = 0; i < count; i++) {
653		rle = resource_list_find(rl, type, i);
654		if (rle) {
655			if (printed == 0)
656				printf(" %s ", name);
657			else if (printed > 0)
658				printf(",");
659			printed++;
660			printf(format, rle->start);
661			if (rle->count > 1) {
662				printf("-");
663				printf(format, rle->start + rle->count - 1);
664			}
665		} else if (i > 3) {
666			/* check the first few regardless */
667			break;
668		}
669	}
670}
671
672static int
673pccard_print_child(device_t dev, device_t child)
674{
675	struct pccard_ivar *devi = PCCARD_IVAR(child);
676	struct resource_list *rl = &devi->resources;
677	int retval = 0;
678
679	retval += bus_print_child_header(dev, child);
680	retval += printf(" at");
681
682	if (devi) {
683		pccard_print_resources(rl, "port", SYS_RES_IOPORT,
684		    PCCARD_NPORT, "%#lx");
685		pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
686		    PCCARD_NMEM, "%#lx");
687		pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
688		    "%ld");
689		pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
690		    "%ld");
691	}
692
693	retval += bus_print_child_footer(dev, child);
694
695	return (retval);
696}
697
698static int
699pccard_set_resource(device_t dev, device_t child, int type, int rid,
700		 u_long start, u_long count)
701{
702	struct pccard_ivar *devi = PCCARD_IVAR(child);
703	struct resource_list *rl = &devi->resources;
704
705	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
706	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
707		return EINVAL;
708	if (rid < 0)
709		return EINVAL;
710	if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
711		return EINVAL;
712	if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
713		return EINVAL;
714	if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
715		return EINVAL;
716	if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
717		return EINVAL;
718
719	resource_list_add(rl, type, rid, start, start + count - 1, count);
720
721	return 0;
722}
723
724static int
725pccard_get_resource(device_t dev, device_t child, int type, int rid,
726    u_long *startp, u_long *countp)
727{
728	struct pccard_ivar *devi = PCCARD_IVAR(child);
729	struct resource_list *rl = &devi->resources;
730	struct resource_list_entry *rle;
731
732	rle = resource_list_find(rl, type, rid);
733	if (!rle)
734		return ENOENT;
735
736	if (startp)
737		*startp = rle->start;
738	if (countp)
739		*countp = rle->count;
740
741	return 0;
742}
743
744static void
745pccard_delete_resource(device_t dev, device_t child, int type, int rid)
746{
747	struct pccard_ivar *devi = PCCARD_IVAR(child);
748	struct resource_list *rl = &devi->resources;
749	resource_list_delete(rl, type, rid);
750}
751
752static int
753pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
754    u_int32_t flags)
755{
756	return CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
757	    rid, flags);
758}
759
760static int
761pccard_set_memory_offset(device_t dev, device_t child, int rid,
762     u_int32_t offset)
763{
764	return CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
765	    offset);
766}
767
768static int
769pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
770{
771	struct pccard_ivar *devi = PCCARD_IVAR(child);
772	struct pccard_function *func = devi->fcn;
773	struct pccard_softc *sc = PCCARD_SOFTC(bus);
774
775	/* PCCARD_IVAR_ETHADDR unhandled from oldcard */
776	switch (which) {
777	default:
778	case PCCARD_IVAR_ETHADDR:
779		return (ENOENT);
780		break;
781	case PCCARD_IVAR_VENDOR:
782		*(u_int32_t *) result = sc->card.manufacturer;
783		break;
784	case PCCARD_IVAR_PRODUCT:
785		*(u_int32_t *) result = sc->card.product;
786		break;
787	case PCCARD_IVAR_FUNCTION_NUMBER:
788		if (!func) {
789			device_printf(bus, "No function number, bug!\n");
790			return (ENOENT);
791		}
792		*(u_int32_t *) result = func->number;
793		break;
794	case PCCARD_IVAR_VENDOR_STR:
795		*(char **) result = sc->card.cis1_info[0];
796		break;
797	case PCCARD_IVAR_PRODUCT_STR:
798		*(char **) result = sc->card.cis1_info[1];
799		break;
800	case PCCARD_IVAR_CIS3_STR:
801		*(char **) result = sc->card.cis1_info[2];
802		break;
803	case PCCARD_IVAR_CIS4_STR:
804		*(char **) result = sc->card.cis1_info[2];
805		break;
806	}
807	return (0);
808}
809
810static void
811pccard_driver_added(device_t dev, driver_t *driver)
812{
813	/*
814	 * XXX eventually we need to attach stuff when we know we
815	 * XXX have kids.  For now we do nothing because we normally
816	 * XXX add children ourselves.  We don't want to necessarily
817	 * XXX force a reprobe.
818	 */
819}
820
821static device_method_t pccard_methods[] = {
822	/* Device interface */
823	DEVMETHOD(device_probe,		pccard_probe),
824	DEVMETHOD(device_attach,	pccard_attach),
825	DEVMETHOD(device_detach,	bus_generic_detach),
826	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
827	DEVMETHOD(device_suspend,	bus_generic_suspend),
828	DEVMETHOD(device_resume,	bus_generic_resume),
829
830	/* Bus interface */
831	DEVMETHOD(bus_print_child,	pccard_print_child),
832	DEVMETHOD(bus_driver_added,	pccard_driver_added),
833	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
834	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
835	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
836	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
837	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
838	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
839	DEVMETHOD(bus_set_resource,	pccard_set_resource),
840	DEVMETHOD(bus_get_resource,	pccard_get_resource),
841	DEVMETHOD(bus_delete_resource,	pccard_delete_resource),
842	DEVMETHOD(bus_read_ivar,	pccard_read_ivar),
843
844	/* Card Interface */
845	DEVMETHOD(card_set_res_flags,	pccard_set_res_flags),
846	DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
847	DEVMETHOD(card_get_type,	pccard_card_gettype),
848	DEVMETHOD(card_attach_card,	pccard_attach_card),
849	DEVMETHOD(card_detach_card,	pccard_detach_card),
850
851	{ 0, 0 }
852};
853
854static driver_t pccard_driver = {
855	"pccard",
856	pccard_methods,
857	sizeof(struct pccard_softc)
858};
859
860devclass_t	pccard_devclass;
861
862DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
863DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
864DRIVER_MODULE(pccard, pccbb, pccard_driver, pccard_devclass, 0, 0);
865DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
866MODULE_VERSION(pccard, 1);
867MODULE_DEPEND(pccard, pcic, 1, 1, 1);
868