xref: /dragonfly/sys/dev/pccard/cardbus/cardbus.c (revision 8a7bdfea)
1 /*-
2  * Copyright (c) 2003 M. Warner Losh.  All Rights Reserved.
3  * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/cardbus/cardbus.c,v 1.28 2002/11/27 17:30:41 imp Exp $
27  * $DragonFly: src/sys/dev/pccard/cardbus/cardbus.c,v 1.13 2008/01/11 10:53:46 sephe Exp $
28  */
29 
30 /*
31  * Cardbus Bus Driver
32  *
33  * much of the bus code was stolen directly from sys/pci/pci.c
34  *   (Copyright (c) 1997, Stefan Esser <se@freebsd.org>)
35  *
36  * Written by Jonathan Chen <jon@freebsd.org>
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 
46 #include <sys/bus.h>
47 #include <sys/rman.h>
48 
49 #include <sys/pciio.h>
50 #include <bus/pci/pcivar.h>
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pci_private.h>
53 
54 #include <dev/pccard/cardbus/cardbusreg.h>
55 #include <dev/pccard/cardbus/cardbusvar.h>
56 #include <dev/pccard/cardbus/cardbus_cis.h>
57 #include <bus/pccard/pccard_cis.h>
58 #include <bus/pccard/pccardvar.h>
59 
60 #include "power_if.h"
61 #include "pcib_if.h"
62 
63 /* sysctl vars */
64 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
65 
66 int    cardbus_debug = 0;
67 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
68 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
69     &cardbus_debug, 0,
70   "CardBus debug");
71 
72 int    cardbus_cis_debug = 0;
73 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
74 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
75     &cardbus_cis_debug, 0,
76   "CardBus CIS debug");
77 
78 #define	DPRINTF(a) if (cardbus_debug) kprintf a
79 #define	DEVPRINTF(x) if (cardbus_debug) device_printf x
80 
81 
82 static void	cardbus_add_map(device_t cbdev, device_t child, int reg);
83 static int	cardbus_alloc_resources(device_t cbdev, device_t child);
84 static int	cardbus_attach(device_t cbdev);
85 static int	cardbus_attach_card(device_t cbdev);
86 static int	cardbus_barsort(const void *a, const void *b);
87 static int	cardbus_detach(device_t cbdev);
88 static int	cardbus_detach_card(device_t cbdev);
89 static void	cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
90 		    pcicfgregs *cfg);
91 static void	cardbus_driver_added(device_t cbdev, driver_t *driver);
92 static void	cardbus_pickup_maps(device_t cbdev, device_t child);
93 static int	cardbus_probe(device_t cbdev);
94 static int	cardbus_read_ivar(device_t cbdev, device_t child, int which,
95 		    uintptr_t *result);
96 static void	cardbus_release_all_resources(device_t cbdev,
97 		    struct cardbus_devinfo *dinfo);
98 static int	cardbus_write_ivar(device_t cbdev, device_t child, int which,
99 		    uintptr_t value);
100 
101 /*
102  * Resource allocation
103  */
104 /*
105  * Adding a memory/io resource (sans CIS)
106  */
107 
108 static void
109 cardbus_add_map(device_t cbdev, device_t child, int reg)
110 {
111 	struct cardbus_devinfo *dinfo = device_get_ivars(child);
112 	struct resource_list_entry *rle;
113 	uint32_t size;
114 	uint32_t testval;
115 	int type;
116 
117 	SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
118 		if (rle->rid == reg)
119 			return;
120 	}
121 
122 	if (reg == CARDBUS_ROM_REG)
123 		testval = CARDBUS_ROM_ADDRMASK;
124 	else
125 		testval = ~0;
126 
127 	pci_write_config(child, reg, testval, 4);
128 	testval = pci_read_config(child, reg, 4);
129 
130 	if (testval == ~0 || testval == 0)
131 		return;
132 
133 	if ((testval & 1) == 0)
134 		type = SYS_RES_MEMORY;
135 	else
136 		type = SYS_RES_IOPORT;
137 
138 	size = CARDBUS_MAPREG_MEM_SIZE(testval);
139 	if (bootverbose) {
140 		device_printf(cbdev, "Resource not specified in CIS: "
141 			      "id=%x, size=%x\n", reg, size);
142 	}
143 	resource_list_add(&dinfo->pci.resources, type, reg, 0UL, ~0UL, size);
144 }
145 
146 static void
147 cardbus_pickup_maps(device_t cbdev, device_t child)
148 {
149 	struct cardbus_devinfo *dinfo = device_get_ivars(child);
150 	int reg;
151 
152 	/*
153 	 * Try to pick up any resources that was not specified in CIS.
154 	 * Maybe this isn't any longer necessary now that we have fixed
155 	 * CIS parsing and we should filter things here?  XXX
156 	 */
157 	for (reg = 0; reg < dinfo->pci.cfg.nummaps; reg++)
158 		cardbus_add_map(cbdev, child, PCIR_BAR(reg));
159 }
160 
161 static void
162 cardbus_do_res(struct resource_list_entry *rle, device_t child, uint32_t start)
163 {
164 	rle->start = start;
165 	rle->end = start + rle->count - 1;
166 	pci_write_config(child, rle->rid, rle->start, 4);
167 }
168 
169 static int
170 cardbus_barsort(const void *a, const void *b)
171 {
172 	return ((*(const struct resource_list_entry * const *)b)->count -
173 	    (*(const struct resource_list_entry * const *)a)->count);
174 }
175 
176 /* XXX this function is too long */
177 static int
178 cardbus_alloc_resources(device_t cbdev, device_t child)
179 {
180 	struct cardbus_devinfo *dinfo = device_get_ivars(child);
181 	int count;
182 	struct resource_list_entry *rle;
183 	struct resource_list_entry **barlist;
184 	int tmp;
185 	uint32_t mem_psize = 0, mem_nsize = 0, io_size = 0;
186 	struct resource *res;
187 	uint32_t start,end;
188 	int rid, flags;
189 
190 	count = 0;
191 	SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
192 		count++;
193 	}
194 	if (count == 0)
195 		return (0);
196 	barlist = kmalloc(sizeof(struct resource_list_entry*) * count, M_DEVBUF,
197 	    M_WAITOK);
198 	count = 0;
199 	SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
200 		barlist[count] = rle;
201 		if (rle->type == SYS_RES_IOPORT) {
202 			io_size += rle->count;
203 		} else if (rle->type == SYS_RES_MEMORY) {
204 			if (dinfo->mprefetchable & BARBIT(rle->rid))
205 				mem_psize += rle->count;
206 			else
207 				mem_nsize += rle->count;
208 		}
209 		count++;
210 	}
211 
212 	/*
213 	 * We want to allocate the largest resource first, so that our
214 	 * allocated memory is packed.
215 	 */
216 	kqsort(barlist, count, sizeof(struct resource_list_entry *),
217 	    cardbus_barsort);
218 
219 	/* Allocate prefetchable memory */
220 	flags = 0;
221 	for (tmp = 0; tmp < count; tmp++) {
222 		rle = barlist[tmp];
223 		if (rle->res == NULL &&
224 		    rle->type == SYS_RES_MEMORY &&
225 		    dinfo->mprefetchable & BARBIT(rle->rid)) {
226 			flags = rman_make_alignment_flags(rle->count);
227 			break;
228 		}
229 	}
230 	if (flags > 0) { /* If any prefetchable memory is requested... */
231 		/*
232 		 * First we allocate one big space for all resources of this
233 		 * type.  We do this because our parent, pccbb, needs to open
234 		 * a window to forward all addresses within the window, and
235 		 * it would be best if nobody else has resources allocated
236 		 * within the window.
237 		 * (XXX: Perhaps there might be a better way to do this?)
238 		 */
239 		rid = 0;
240 		res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
241 		    (dinfo->mprefetchable & dinfo->mbelow1mb)?0xFFFFF:~0UL,
242 		    mem_psize, flags);
243 		if (res == NULL) {
244 			device_printf(cbdev,
245 			    "Can't get memory for prefetch mem\n");
246 			kfree(barlist, M_DEVBUF);
247 			return (EIO);
248 		}
249 		start = rman_get_start(res);
250 		end = rman_get_end(res);
251 		DEVPRINTF((cbdev, "Prefetchable memory at %x-%x\n", start, end));
252 		/*
253 		 * Now that we know the region is free, release it and hand it
254 		 * out piece by piece.
255 		 */
256 		bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
257 		for (tmp = 0; tmp < count; tmp++) {
258 			rle = barlist[tmp];
259 			if (rle->type == SYS_RES_MEMORY &&
260 			    dinfo->mprefetchable & BARBIT(rle->rid)) {
261 				cardbus_do_res(rle, child, start);
262 				start += rle->count;
263 			}
264 		}
265 	}
266 
267 	/* Allocate non-prefetchable memory */
268 	flags = 0;
269 	for (tmp = 0; tmp < count; tmp++) {
270 		rle = barlist[tmp];
271 		if (rle->type == SYS_RES_MEMORY &&
272 		    (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
273 			flags = rman_make_alignment_flags(rle->count);
274 			break;
275 		}
276 	}
277 	if (flags > 0) { /* If any non-prefetchable memory is requested... */
278 		/*
279 		 * First we allocate one big space for all resources of this
280 		 * type.  We do this because our parent, pccbb, needs to open
281 		 * a window to forward all addresses within the window, and
282 		 * it would be best if nobody else has resources allocated
283 		 * within the window.
284 		 * (XXX: Perhaps there might be a better way to do this?)
285 		 */
286 		rid = 0;
287 		res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
288 		    ((~dinfo->mprefetchable) & dinfo->mbelow1mb)?0xFFFFF:~0UL,
289 		    mem_nsize, flags);
290 		if (res == NULL) {
291 			device_printf(cbdev,
292 			    "Can't get memory for non-prefetch mem\n");
293 			kfree(barlist, M_DEVBUF);
294 			return (EIO);
295 		}
296 		start = rman_get_start(res);
297 		end = rman_get_end(res);
298 		DEVPRINTF((cbdev, "Non-prefetchable memory at %x-%x\n",
299 		    start, end));
300 		/*
301 		 * Now that we know the region is free, release it and hand it
302 		 * out piece by piece.
303 		 */
304 		bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
305 		for (tmp = 0; tmp < count; tmp++) {
306 			rle = barlist[tmp];
307 			if (rle->type == SYS_RES_MEMORY &&
308 			    (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
309 				cardbus_do_res(rle, child, start);
310 				start += rle->count;
311 			}
312 		}
313 	}
314 
315 	/* Allocate IO ports */
316 	flags = 0;
317 	for (tmp = 0; tmp < count; tmp++) {
318 		rle = barlist[tmp];
319 		if (rle->type == SYS_RES_IOPORT) {
320 			flags = rman_make_alignment_flags(rle->count);
321 			break;
322 		}
323 	}
324 	if (flags > 0) { /* If any IO port is requested... */
325 		/*
326 		 * First we allocate one big space for all resources of this
327 		 * type.  We do this because our parent, pccbb, needs to open
328 		 * a window to forward all addresses within the window, and
329 		 * it would be best if nobody else has resources allocated
330 		 * within the window.
331 		 * (XXX: Perhaps there might be a better way to do this?)
332 		 */
333 		rid = 0;
334 		res = bus_alloc_resource(cbdev, SYS_RES_IOPORT, &rid, 0,
335 		    (dinfo->ibelow1mb)?0xFFFFF:~0UL, io_size, flags);
336 		if (res == NULL) {
337 			device_printf(cbdev,
338 			    "Can't get memory for IO ports\n");
339 			kfree(barlist, M_DEVBUF);
340 			return (EIO);
341 		}
342 		start = rman_get_start(res);
343 		end = rman_get_end(res);
344 		DEVPRINTF((cbdev, "IO port at %x-%x\n", start, end));
345 		/*
346 		 * Now that we know the region is free, release it and hand it
347 		 * out piece by piece.
348 		 */
349 		bus_release_resource(cbdev, SYS_RES_IOPORT, rid, res);
350 		for (tmp = 0; tmp < count; tmp++) {
351 			rle = barlist[tmp];
352 			if (rle->type == SYS_RES_IOPORT) {
353 				cardbus_do_res(rle, child, start);
354 				start += rle->count;
355 			}
356 		}
357 	}
358 
359 	/* Allocate IRQ */
360 	rid = 0;
361 	res = bus_alloc_resource_any(cbdev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
362 	if (res == NULL) {
363 		device_printf(cbdev, "Can't get memory for irq\n");
364 		kfree(barlist, M_DEVBUF);
365 		return (EIO);
366 	}
367 	start = rman_get_start(res);
368 	end = rman_get_end(res);
369 	bus_release_resource(cbdev, SYS_RES_IRQ, rid, res);
370 	resource_list_add(&dinfo->pci.resources, SYS_RES_IRQ, rid, start, end,
371 	    1);
372 	dinfo->pci.cfg.intline = start;
373 	pci_write_config(child, PCIR_INTLINE, start, 1);
374 
375 	kfree(barlist, M_DEVBUF);
376 	return (0);
377 }
378 
379 /************************************************************************/
380 /* Probe/Attach								*/
381 /************************************************************************/
382 
383 static int
384 cardbus_probe(device_t cbdev)
385 {
386 	device_set_desc(cbdev, "CardBus bus");
387 	return 0;
388 }
389 
390 static int
391 cardbus_attach(device_t cbdev)
392 {
393 	return 0;
394 }
395 
396 static int
397 cardbus_detach(device_t cbdev)
398 {
399 	cardbus_detach_card(cbdev);
400 	return 0;
401 }
402 
403 static int
404 cardbus_suspend(device_t self)
405 {
406 	cardbus_detach_card(self);
407 	return (0);
408 }
409 
410 static int
411 cardbus_resume(device_t self)
412 {
413 	return (0);
414 }
415 
416 /************************************************************************/
417 /* Attach/Detach card							*/
418 /************************************************************************/
419 
420 static void
421 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
422 {
423 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
424 	    pci_get_irq(device_get_parent(brdev)), 1);
425 	cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
426 
427 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
428 	cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
429 
430 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
431 	cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
432 
433 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
434 	cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
435 
436 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
437 	cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
438 }
439 
440 static int
441 cardbus_attach_card(device_t cbdev)
442 {
443 	device_t brdev = device_get_parent(cbdev);
444 	device_t child;
445 	int cardattached = 0;
446 	int bus, slot, func;
447 
448 	cardbus_detach_card(cbdev); /* detach existing cards */
449 	POWER_ENABLE_SOCKET(brdev, cbdev);
450 	bus = pcib_get_bus(brdev);
451 	/* For each function, set it up and try to attach a driver to it */
452 	for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
453 		int cardbusfunchigh = 0;
454 		for (func = 0; func <= cardbusfunchigh; func++) {
455 			struct cardbus_devinfo *dinfo;
456 
457 			dinfo = (struct cardbus_devinfo *)
458 			    pci_read_device(brdev, bus, slot, func,
459 				sizeof(struct cardbus_devinfo));
460 			if (dinfo == NULL)
461 				continue;
462 			if (dinfo->pci.cfg.mfdev)
463 				cardbusfunchigh = CARDBUS_FUNCMAX;
464 
465 			cardbus_device_setup_regs(brdev, bus, slot, func,
466 			    &dinfo->pci.cfg);
467 			child = device_add_child(cbdev, NULL, -1);
468 			if (child == NULL) {
469 				DEVPRINTF((cbdev, "Cannot add child!\n"));
470 				pci_freecfg((struct pci_devinfo *)dinfo);
471 				continue;
472 			}
473 			dinfo->pci.cfg.dev = child;
474 			resource_list_init(&dinfo->pci.resources);
475 			device_set_ivars(child, dinfo);
476 			if (cardbus_do_cis(cbdev, child) != 0) {
477 				DEVPRINTF((cbdev, "Can't parse cis\n"));
478 				pci_freecfg((struct pci_devinfo *)dinfo);
479 				continue;
480 			}
481 			cardbus_pickup_maps(cbdev, child);
482 			cardbus_alloc_resources(cbdev, child);
483 			pci_print_verbose(&dinfo->pci);
484 			if (device_probe_and_attach(child) != 0)
485 				cardbus_release_all_resources(cbdev, dinfo);
486 			else
487 				cardattached++;
488 		}
489 	}
490 
491 	if (cardattached > 0)
492 		return (0);
493 	POWER_DISABLE_SOCKET(brdev, cbdev);
494 	return (ENOENT);
495 }
496 
497 static int
498 cardbus_detach_card(device_t cbdev)
499 {
500 	int numdevs;
501 	device_t *devlist;
502 	int tmp;
503 	int err = 0;
504 
505 	device_get_children(cbdev, &devlist, &numdevs);
506 
507 	if (numdevs == 0) {
508 		kfree(devlist, M_TEMP);
509 		return (ENOENT);
510 	}
511 
512 	for (tmp = 0; tmp < numdevs; tmp++) {
513 		struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
514 		int status = device_get_state(devlist[tmp]);
515 
516 		if (dinfo->pci.cfg.dev != devlist[tmp])
517 			device_printf(cbdev, "devinfo dev mismatch\n");
518 		if (status == DS_ATTACHED || status == DS_BUSY)
519 			device_detach(devlist[tmp]);
520 		cardbus_release_all_resources(cbdev, dinfo);
521 		device_delete_child(cbdev, devlist[tmp]);
522 		pci_freecfg((struct pci_devinfo *)dinfo);
523 	}
524 	POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
525 	kfree(devlist, M_TEMP);
526 	return (err);
527 }
528 
529 static void
530 cardbus_driver_added(device_t cbdev, driver_t *driver)
531 {
532 	int numdevs;
533 	device_t *devlist;
534 	device_t dev;
535 	int i;
536 	struct cardbus_devinfo *dinfo;
537 
538 	DEVICE_IDENTIFY(driver, cbdev);
539 	device_get_children(cbdev, &devlist, &numdevs);
540 	/*
541 	 * If there are no drivers attached, but there are children,
542 	 * then power the card up.
543 	 */
544 	for (i = 0; i < numdevs; i++) {
545 		dev = devlist[i];
546 		if (device_get_state(dev) != DS_NOTPRESENT)
547 		    break;
548 	}
549 	if (i > 0 && i == numdevs)
550 		POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
551 	for (i = 0; i < numdevs; i++) {
552 		dev = devlist[i];
553 		if (device_get_state(dev) != DS_NOTPRESENT)
554 			continue;
555 		dinfo = device_get_ivars(dev);
556 		pci_print_verbose(&dinfo->pci);
557 		resource_list_init(&dinfo->pci.resources);
558 		cardbus_do_cis(cbdev, dev);
559 		cardbus_pickup_maps(cbdev, dev);
560 		cardbus_alloc_resources(cbdev, dev);
561 		if (device_probe_and_attach(dev) != 0)
562 			cardbus_release_all_resources(cbdev, dinfo);
563 	}
564 	kfree(devlist, M_TEMP);
565 }
566 
567 static void
568 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
569 {
570 	struct resource_list_entry *rle;
571 
572 	/* Free all allocated resources */
573 	SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
574 		if (rle->res) {
575 			if (rman_get_device(rle->res) != cbdev)
576 				device_printf(cbdev, "release_all_resource: "
577 				    "Resource still owned by child, oops. "
578 				    "(type=%d, rid=%d, addr=%lx)\n",
579 				    rle->type, rle->rid,
580 				    rman_get_start(rle->res));
581 			BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
582 			    cbdev, rle->type, rle->rid, rle->res);
583 			rle->res = NULL;
584 			/*
585 			 * zero out config so the card won't acknowledge
586 			 * access to the space anymore
587 			 */
588 			pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
589 		}
590 	}
591 	resource_list_free(&dinfo->pci.resources);
592 }
593 
594 /************************************************************************/
595 /* Other Bus Methods							*/
596 /************************************************************************/
597 
598 static int
599 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
600 {
601 	struct cardbus_devinfo *dinfo;
602 	pcicfgregs *cfg;
603 
604 	dinfo = device_get_ivars(child);
605 	cfg = &dinfo->pci.cfg;
606 
607 	switch (which) {
608 	case PCI_IVAR_ETHADDR:
609 		/*
610 		 * The generic accessor doesn't deal with failure, so
611 		 * we set the return value, then return an error.
612 		 */
613 		if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
614 			*((uint8_t **) result) = dinfo->funce.lan.nid;
615 			break;
616 		}
617 		*((uint8_t **) result) = NULL;
618 		return (EINVAL);
619 	default:
620 		return (pci_read_ivar(cbdev, child, which, result));
621 	}
622 	return 0;
623 }
624 
625 static int
626 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
627 {
628 	return(pci_write_ivar(cbdev, child, which, value));
629 }
630 
631 static device_method_t cardbus_methods[] = {
632 	/* Device interface */
633 	DEVMETHOD(device_probe,		cardbus_probe),
634 	DEVMETHOD(device_attach,	cardbus_attach),
635 	DEVMETHOD(device_detach,	cardbus_detach),
636 	DEVMETHOD(device_suspend,	cardbus_suspend),
637 	DEVMETHOD(device_resume,	cardbus_resume),
638 
639 	/* Bus interface */
640 	DEVMETHOD(bus_read_ivar,	cardbus_read_ivar),
641 	DEVMETHOD(bus_write_ivar,	cardbus_write_ivar),
642 	DEVMETHOD(bus_driver_added,	cardbus_driver_added),
643 
644 	/* Card Interface */
645 	DEVMETHOD(card_attach_card,	cardbus_attach_card),
646 	DEVMETHOD(card_detach_card,	cardbus_detach_card),
647 
648 	{0,0}
649 };
650 
651 DECLARE_CLASS(pci_driver);
652 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 0, pci_driver);
653 
654 static devclass_t cardbus_devclass;
655 
656 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
657 MODULE_VERSION(cardbus, 1);
658