pccard.c revision 143815
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2 3/*- 4 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Marc Horowitz. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 143815 2005-03-18 16:37:51Z imp $"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/malloc.h> 38#include <sys/module.h> 39#include <sys/kernel.h> 40#include <sys/queue.h> 41#include <sys/sysctl.h> 42#include <sys/types.h> 43 44#include <sys/bus.h> 45#include <machine/bus.h> 46#include <sys/rman.h> 47#include <machine/resource.h> 48 49#include <net/ethernet.h> 50 51#include <dev/pccard/pccardreg.h> 52#include <dev/pccard/pccardvar.h> 53 54#include "power_if.h" 55#include "card_if.h" 56 57#define PCCARDDEBUG 58 59/* sysctl vars */ 60SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 61 62int pccard_debug = 0; 63TUNABLE_INT("hw.pccard.debug", &pccard_debug); 64SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 65 &pccard_debug, 0, 66 "pccard debug"); 67 68int pccard_cis_debug = 0; 69TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 70SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 71 &pccard_cis_debug, 0, "pccard CIS debug"); 72 73#ifdef PCCARDDEBUG 74#define DPRINTF(arg) if (pccard_debug) printf arg 75#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 76#define PRVERBOSE(arg) printf arg 77#define DEVPRVERBOSE(arg) device_printf arg 78#else 79#define DPRINTF(arg) 80#define DEVPRINTF(arg) 81#define PRVERBOSE(arg) if (bootverbose) printf arg 82#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 83#endif 84 85static int pccard_ccr_read(struct pccard_function *pf, int ccr); 86static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 87static int pccard_attach_card(device_t dev); 88static int pccard_detach_card(device_t dev); 89static void pccard_function_init(struct pccard_function *pf); 90static void pccard_function_free(struct pccard_function *pf); 91static int pccard_function_enable(struct pccard_function *pf); 92static void pccard_function_disable(struct pccard_function *pf); 93static int pccard_compat_do_probe(device_t bus, device_t dev); 94static int pccard_compat_do_attach(device_t bus, device_t dev); 95static int pccard_add_children(device_t dev, int busno); 96static int pccard_probe(device_t dev); 97static int pccard_attach(device_t dev); 98static int pccard_detach(device_t dev); 99static void pccard_print_resources(struct resource_list *rl, 100 const char *name, int type, int count, const char *format); 101static int pccard_print_child(device_t dev, device_t child); 102static int pccard_set_resource(device_t dev, device_t child, int type, 103 int rid, u_long start, u_long count); 104static int pccard_get_resource(device_t dev, device_t child, int type, 105 int rid, u_long *startp, u_long *countp); 106static void pccard_delete_resource(device_t dev, device_t child, int type, 107 int rid); 108static int pccard_set_res_flags(device_t dev, device_t child, int type, 109 int rid, uint32_t flags); 110static int pccard_set_memory_offset(device_t dev, device_t child, int rid, 111 uint32_t offset, uint32_t *deltap); 112static void pccard_probe_nomatch(device_t cbdev, device_t child); 113static int pccard_read_ivar(device_t bus, device_t child, int which, 114 u_char *result); 115static void pccard_driver_added(device_t dev, driver_t *driver); 116static struct resource *pccard_alloc_resource(device_t dev, 117 device_t child, int type, int *rid, u_long start, 118 u_long end, u_long count, u_int flags); 119static int pccard_release_resource(device_t dev, device_t child, int type, 120 int rid, struct resource *r); 121static void pccard_child_detached(device_t parent, device_t dev); 122static void pccard_intr(void *arg); 123static int pccard_setup_intr(device_t dev, device_t child, 124 struct resource *irq, int flags, driver_intr_t *intr, 125 void *arg, void **cookiep); 126static int pccard_teardown_intr(device_t dev, device_t child, 127 struct resource *r, void *cookie); 128 129static const struct pccard_product * 130pccard_do_product_lookup(device_t bus, device_t dev, 131 const struct pccard_product *tab, size_t ent_size, 132 pccard_product_match_fn matchfn); 133 134 135static int 136pccard_ccr_read(struct pccard_function *pf, int ccr) 137{ 138 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 139 pf->pf_ccr_offset + ccr)); 140} 141 142static void 143pccard_ccr_write(struct pccard_function *pf, int ccr, int val) 144{ 145 if ((pf->ccr_mask) & (1 << (ccr / 2))) { 146 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 147 pf->pf_ccr_offset + ccr, val); 148 } 149} 150 151static int 152pccard_set_default_descr(device_t dev) 153{ 154 const char *vendorstr, *prodstr; 155 uint32_t vendor, prod; 156 char *str; 157 158 if (pccard_get_vendor_str(dev, &vendorstr)) 159 return (0); 160 if (pccard_get_product_str(dev, &prodstr)) 161 return (0); 162 if (vendorstr != NULL && prodstr != NULL) { 163 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 164 M_WAITOK); 165 sprintf(str, "%s %s", vendorstr, prodstr); 166 device_set_desc_copy(dev, str); 167 free(str, M_DEVBUF); 168 } else { 169 if (pccard_get_vendor(dev, &vendor)) 170 return (0); 171 if (pccard_get_product(dev, &prod)) 172 return (0); 173 str = malloc(100, M_DEVBUF, M_WAITOK); 174 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod); 175 device_set_desc_copy(dev, str); 176 free(str, M_DEVBUF); 177 } 178 return (0); 179} 180 181static int 182pccard_attach_card(device_t dev) 183{ 184 struct pccard_softc *sc = PCCARD_SOFTC(dev); 185 struct pccard_function *pf; 186 struct pccard_ivar *ivar; 187 device_t child; 188 int i; 189 190 /* 191 * this is here so that when socket_enable calls gettype, trt happens 192 */ 193 STAILQ_INIT(&sc->card.pf_head); 194 195 DEVPRINTF((dev, "chip_socket_enable\n")); 196 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 197 198 DEVPRINTF((dev, "read_cis\n")); 199 pccard_read_cis(sc); 200 201 DEVPRINTF((dev, "check_cis_quirks\n")); 202 pccard_check_cis_quirks(dev); 203 204 /* 205 * bail now if the card has no functions, or if there was an error in 206 * the cis. 207 */ 208 209 if (sc->card.error) { 210 device_printf(dev, "CARD ERROR!\n"); 211 return (1); 212 } 213 if (STAILQ_EMPTY(&sc->card.pf_head)) { 214 device_printf(dev, "Card has no functions!\n"); 215 return (1); 216 } 217 218 if (bootverbose || pccard_debug) 219 pccard_print_cis(dev); 220 221 DEVPRINTF((dev, "functions scanning\n")); 222 i = -1; 223 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 224 i++; 225 if (STAILQ_EMPTY(&pf->cfe_head)) { 226 device_printf(dev, 227 "Function %d has no config entries.!\n", i); 228 continue; 229 } 230 pf->sc = sc; 231 pf->cfe = NULL; 232 pf->dev = NULL; 233 } 234 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 235 pccard_mfc(sc))); 236 237 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 238 if (STAILQ_EMPTY(&pf->cfe_head)) 239 continue; 240 /* 241 * In NetBSD, the drivers are responsible for activating 242 * each function of a card. I think that in FreeBSD we 243 * want to activate them enough for the usual bus_*_resource 244 * routines will do the right thing. This many mean a 245 * departure from the current NetBSD model. 246 * 247 * This seems to work well in practice for most cards. 248 * However, there are two cases that are problematic. 249 * If a driver wishes to pick and chose which config 250 * entry to use, then this method falls down. These 251 * are usually older cards. In addition, there are 252 * some cards that have multiple hardware units on the 253 * cards, but presents only one CIS chain. These cards 254 * are combination cards, but only one of these units 255 * can be on at a time. 256 */ 257 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 258 M_WAITOK | M_ZERO); 259 resource_list_init(&ivar->resources); 260 child = device_add_child(dev, NULL, -1); 261 device_set_ivars(child, ivar); 262 ivar->fcn = pf; 263 pf->dev = child; 264 /* 265 * XXX We might want to move the next two lines into 266 * XXX the pccard interface layer. For the moment, this 267 * XXX is OK, but some drivers want to pick the config 268 * XXX entry to use as well as some address tweaks (mostly 269 * XXX due to bugs in decode logic that makes some 270 * XXX addresses illegal or broken). 271 */ 272 pccard_function_init(pf); 273 if (sc->sc_enabled_count == 0) 274 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 275 if (pccard_function_enable(pf) == 0 && 276 pccard_set_default_descr(child) == 0 && 277 device_probe_and_attach(child) == 0) { 278 DEVPRINTF((sc->dev, "function %d CCR at %d " 279 "offset %x mask %x: " 280 "%x %x %x %x, %x %x %x %x, %x\n", 281 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 282 pf->ccr_mask, pccard_ccr_read(pf, 0x00), 283 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 284 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 285 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 286 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 287 } else { 288 if (pf->cfe != NULL) 289 pccard_function_disable(pf); 290 } 291 } 292 return (0); 293} 294 295static int 296pccard_detach_card(device_t dev) 297{ 298 struct pccard_softc *sc = PCCARD_SOFTC(dev); 299 struct pccard_function *pf; 300 struct pccard_config_entry *cfe; 301 int state; 302 303 /* 304 * We are running on either the PCCARD socket's event thread 305 * or in user context detaching a device by user request. 306 */ 307 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 308 if (pf->dev == NULL) 309 continue; 310 state = device_get_state(pf->dev); 311 if (state == DS_ATTACHED || state == DS_BUSY) 312 device_detach(pf->dev); 313 if (pf->cfe != NULL) 314 pccard_function_disable(pf); 315 pccard_function_free(pf); 316 device_delete_child(dev, pf->dev); 317 } 318 if (sc->sc_enabled_count == 0) 319 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 320 321 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 322 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 323 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 324 free(cfe, M_DEVBUF); 325 } 326 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 327 free(pf, M_DEVBUF); 328 } 329 return (0); 330} 331 332static const struct pccard_product * 333pccard_do_product_lookup(device_t bus, device_t dev, 334 const struct pccard_product *tab, size_t ent_size, 335 pccard_product_match_fn matchfn) 336{ 337 const struct pccard_product *ent; 338 int matches; 339 uint32_t vendor; 340 uint32_t prod; 341 const char *vendorstr; 342 const char *prodstr; 343 const char *cis3str; 344 const char *cis4str; 345 346#ifdef DIAGNOSTIC 347 if (sizeof *ent > ent_size) 348 panic("pccard_product_lookup: bogus ent_size %jd", 349 (intmax_t) ent_size); 350#endif 351 if (pccard_get_vendor(dev, &vendor)) 352 return (NULL); 353 if (pccard_get_product(dev, &prod)) 354 return (NULL); 355 if (pccard_get_vendor_str(dev, &vendorstr)) 356 return (NULL); 357 if (pccard_get_product_str(dev, &prodstr)) 358 return (NULL); 359 if (pccard_get_cis3_str(dev, &cis3str)) 360 return (NULL); 361 if (pccard_get_cis4_str(dev, &cis4str)) 362 return (NULL); 363 for (ent = tab; ent->pp_vendor != 0; ent = 364 (const struct pccard_product *) ((const char *) ent + ent_size)) { 365 matches = 1; 366 if (ent->pp_vendor == PCCARD_VENDOR_ANY && 367 ent->pp_product == PCCARD_PRODUCT_ANY && 368 ent->pp_cis[0] == NULL && 369 ent->pp_cis[1] == NULL) { 370 if (ent->pp_name) 371 device_printf(dev, 372 "Total wildcard entry ignored for %s\n", 373 ent->pp_name); 374 continue; 375 } 376 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 377 vendor != ent->pp_vendor) 378 matches = 0; 379 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 380 prod != ent->pp_product) 381 matches = 0; 382 if (matches && ent->pp_cis[0] && 383 (vendorstr == NULL || 384 strcmp(ent->pp_cis[0], vendorstr) != 0)) 385 matches = 0; 386 if (matches && ent->pp_cis[1] && 387 (prodstr == NULL || 388 strcmp(ent->pp_cis[1], prodstr) != 0)) 389 matches = 0; 390 if (matches && ent->pp_cis[2] && 391 (cis3str == NULL || 392 strcmp(ent->pp_cis[2], cis3str) != 0)) 393 matches = 0; 394 if (matches && ent->pp_cis[3] && 395 (cis4str == NULL || 396 strcmp(ent->pp_cis[3], cis4str) != 0)) 397 matches = 0; 398 if (matchfn != NULL) 399 matches = (*matchfn)(dev, ent, matches); 400 if (matches) 401 return (ent); 402 } 403 return (NULL); 404} 405 406/* 407 * Initialize a PCCARD function. May be called as long as the function is 408 * disabled. 409 * 410 * Note: pccard_function_init should not keep resources allocated. It should 411 * only set them up ala isa pnp, set the values in the rl lists, and return. 412 * Any resource held after pccard_function_init is called is a bug. However, 413 * the bus routines to get the resources also assume that pccard_function_init 414 * does this, so they need to be fixed too. 415 */ 416static void 417pccard_function_init(struct pccard_function *pf) 418{ 419 struct pccard_config_entry *cfe; 420 int i; 421 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 422 struct resource_list *rl = &devi->resources; 423 struct resource_list_entry *rle; 424 struct resource *r = 0; 425 device_t bus; 426 int start; 427 int end; 428 int spaces; 429 430 if (pf->pf_flags & PFF_ENABLED) { 431 printf("pccard_function_init: function is enabled"); 432 return; 433 } 434 bus = device_get_parent(pf->dev); 435 /* Remember which configuration entry we are using. */ 436 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 437 for (i = 0; i < cfe->num_iospace; i++) 438 cfe->iores[i] = NULL; 439 cfe->irqres = NULL; 440 spaces = 0; 441 for (i = 0; i < cfe->num_iospace; i++) { 442 start = cfe->iospace[i].start; 443 if (start) 444 end = start + cfe->iospace[i].length - 1; 445 else 446 end = ~0; 447 cfe->iorid[i] = i; 448 DEVPRINTF((bus, "I/O rid %d start %x end %x\n", 449 i, start, end)); 450 r = cfe->iores[i] = bus_alloc_resource(bus, 451 SYS_RES_IOPORT, &cfe->iorid[i], start, end, 452 cfe->iospace[i].length, 453 rman_make_alignment_flags(cfe->iospace[i].length)); 454 if (cfe->iores[i] == NULL) 455 goto not_this_one; 456 resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i], 457 rman_get_start(r), rman_get_end(r), 458 cfe->iospace[i].length); 459 rle = resource_list_find(rl, SYS_RES_IOPORT, 460 cfe->iorid[i]); 461 rle->res = r; 462 spaces++; 463 } 464 if (cfe->num_memspace > 0) { 465 /* 466 * Not implement yet, Fix me. 467 */ 468 DEVPRINTF((bus, "Memory space not yet implemented.\n")); 469 } 470 if (spaces == 0) { 471 DEVPRINTF((bus, "Neither memory nor I/O mapped\n")); 472 goto not_this_one; 473 } 474 if (cfe->irqmask) { 475 cfe->irqrid = 0; 476 r = cfe->irqres = bus_alloc_resource_any(bus, 477 SYS_RES_IRQ, &cfe->irqrid, 0); 478 if (cfe->irqres == NULL) 479 goto not_this_one; 480 resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid, 481 rman_get_start(r), rman_get_end(r), 1); 482 rle = resource_list_find(rl, SYS_RES_IRQ, 483 cfe->irqrid); 484 rle->res = r; 485 } 486 /* If we get to here, we've allocated all we need */ 487 pf->cfe = cfe; 488 break; 489 not_this_one:; 490 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 491 cfe->number)); 492 /* 493 * Release resources that we partially allocated 494 * from this config entry. 495 */ 496 for (i = 0; i < cfe->num_iospace; i++) { 497 if (cfe->iores[i] != NULL) { 498 bus_release_resource(bus, SYS_RES_IOPORT, 499 cfe->iorid[i], cfe->iores[i]); 500 rle = resource_list_find(rl, SYS_RES_IOPORT, 501 cfe->iorid[i]); 502 rle->res = NULL; 503 resource_list_delete(rl, SYS_RES_IOPORT, 504 cfe->iorid[i]); 505 } 506 cfe->iores[i] = NULL; 507 } 508 if (cfe->irqmask && cfe->irqres != NULL) { 509 bus_release_resource(bus, SYS_RES_IRQ, 510 cfe->irqrid, cfe->irqres); 511 rle = resource_list_find(rl, SYS_RES_IRQ, 512 cfe->irqrid); 513 rle->res = NULL; 514 resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid); 515 cfe->irqres = NULL; 516 } 517 } 518} 519 520/* 521 * Free resources allocated by pccard_function_init(), May be called as long 522 * as the function is disabled. 523 * 524 * NOTE: This function should be unnecessary. pccard_function_init should 525 * never keep resources initialized. 526 */ 527static void 528pccard_function_free(struct pccard_function *pf) 529{ 530 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 531 struct resource_list_entry *rle; 532 533 if (pf->pf_flags & PFF_ENABLED) { 534 printf("pccard_function_init: function is enabled"); 535 return; 536 } 537 538 STAILQ_FOREACH(rle, &devi->resources, link) { 539 if (rle->res) { 540 if (rman_get_device(rle->res) != pf->sc->dev) 541 device_printf(pf->sc->dev, 542 "function_free: Resource still owned by " 543 "child, oops. " 544 "(type=%d, rid=%d, addr=%lx)\n", 545 rle->type, rle->rid, 546 rman_get_start(rle->res)); 547 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 548 pf->sc->dev, rle->type, rle->rid, rle->res); 549 rle->res = NULL; 550 } 551 } 552 resource_list_free(&devi->resources); 553} 554 555static void 556pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr, 557 bus_addr_t offset, bus_size_t size) 558{ 559 bus_size_t iosize, tmp; 560 561 if (addr != 0) { 562 if (pf->pf_mfc_iomax == 0) { 563 pf->pf_mfc_iobase = addr + offset; 564 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 565 } else { 566 /* this makes the assumption that nothing overlaps */ 567 if (pf->pf_mfc_iobase > addr + offset) 568 pf->pf_mfc_iobase = addr + offset; 569 if (pf->pf_mfc_iomax < addr + offset + size) 570 pf->pf_mfc_iomax = addr + offset + size; 571 } 572 } 573 574 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 575 /* round up to nearest (2^n)-1 */ 576 for (iosize = 1; iosize < tmp; iosize <<= 1) 577 ; 578 iosize--; 579 580 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 581 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 582 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 583 pf->pf_mfc_iobase & 0xff); 584 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 585 (pf->pf_mfc_iobase >> 8) & 0xff); 586 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 587 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 588 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 589} 590 591/* Enable a PCCARD function */ 592static int 593pccard_function_enable(struct pccard_function *pf) 594{ 595 struct pccard_function *tmp; 596 int reg; 597 device_t dev = pf->sc->dev; 598 599 if (pf->cfe == NULL) { 600 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 601 return (ENOMEM); 602 } 603 604 /* 605 * Increase the reference count on the socket, enabling power, if 606 * necessary. 607 */ 608 pf->sc->sc_enabled_count++; 609 610 if (pf->pf_flags & PFF_ENABLED) { 611 /* 612 * Don't do anything if we're already enabled. 613 */ 614 return (0); 615 } 616 617 /* 618 * it's possible for different functions' CCRs to be in the same 619 * underlying page. Check for that. 620 */ 621 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 622 if ((tmp->pf_flags & PFF_ENABLED) && 623 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 624 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 625 (tmp->ccr_base - tmp->pf_ccr_offset + 626 tmp->pf_ccr_realsize))) { 627 pf->pf_ccrt = tmp->pf_ccrt; 628 pf->pf_ccrh = tmp->pf_ccrh; 629 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 630 631 /* 632 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 633 * tmp->ccr_base) + pf->ccr_base; 634 */ 635 /* pf->pf_ccr_offset = 636 (tmp->pf_ccr_offset + pf->ccr_base) - 637 tmp->ccr_base; */ 638 pf->pf_ccr_window = tmp->pf_ccr_window; 639 break; 640 } 641 } 642 if (tmp == NULL) { 643 pf->ccr_rid = 0; 644 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 645 &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE); 646 if (!pf->ccr_res) 647 goto bad; 648 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 649 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 650 pf->ccr_base)); 651 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 652 pf->ccr_rid, PCCARD_A_MEM_ATTR); 653 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 654 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 655 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 656 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 657 pf->pf_ccr_realsize = 1; 658 } 659 660 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 661 reg |= PCCARD_CCR_OPTION_LEVIREQ; 662 if (pccard_mfc(pf->sc)) { 663 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 664 PCCARD_CCR_OPTION_ADDR_DECODE); 665 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 666 } 667 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 668 669 reg = 0; 670 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 671 reg |= PCCARD_CCR_STATUS_IOIS8; 672 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 673 reg |= PCCARD_CCR_STATUS_AUDIO; 674 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 675 676 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 677 678 if (pccard_mfc(pf->sc)) 679 pccard_mfc_adjust_iobase(pf, 0, 0, 0); 680 681#ifdef PCCARDDEBUG 682 if (pccard_debug) { 683 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 684 device_printf(tmp->sc->dev, 685 "function %d CCR at %d offset %x: " 686 "%x %x %x %x, %x %x %x %x, %x\n", 687 tmp->number, tmp->pf_ccr_window, 688 tmp->pf_ccr_offset, 689 pccard_ccr_read(tmp, 0x00), 690 pccard_ccr_read(tmp, 0x02), 691 pccard_ccr_read(tmp, 0x04), 692 pccard_ccr_read(tmp, 0x06), 693 pccard_ccr_read(tmp, 0x0A), 694 pccard_ccr_read(tmp, 0x0C), 695 pccard_ccr_read(tmp, 0x0E), 696 pccard_ccr_read(tmp, 0x10), 697 pccard_ccr_read(tmp, 0x12)); 698 } 699 } 700#endif 701 pf->pf_flags |= PFF_ENABLED; 702 return (0); 703 704 bad: 705 /* 706 * Decrement the reference count, and power down the socket, if 707 * necessary. 708 */ 709 pf->sc->sc_enabled_count--; 710 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 711 712 return (1); 713} 714 715/* Disable PCCARD function. */ 716static void 717pccard_function_disable(struct pccard_function *pf) 718{ 719 struct pccard_function *tmp; 720 device_t dev = pf->sc->dev; 721 722 if (pf->cfe == NULL) 723 panic("pccard_function_disable: function not initialized"); 724 725 if ((pf->pf_flags & PFF_ENABLED) == 0) { 726 /* 727 * Don't do anything if we're already disabled. 728 */ 729 return; 730 } 731 732 if (pf->intr_handler != NULL) { 733 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 734 struct resource_list_entry *rle = 735 resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 736 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 737 pf->intr_handler_cookie); 738 } 739 740 /* 741 * it's possible for different functions' CCRs to be in the same 742 * underlying page. Check for that. Note we mark us as disabled 743 * first to avoid matching ourself. 744 */ 745 746 pf->pf_flags &= ~PFF_ENABLED; 747 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 748 if ((tmp->pf_flags & PFF_ENABLED) && 749 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 750 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 751 (tmp->ccr_base - tmp->pf_ccr_offset + 752 tmp->pf_ccr_realsize))) 753 break; 754 } 755 756 /* Not used by anyone else; unmap the CCR. */ 757 if (tmp == NULL) { 758 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 759 pf->ccr_res); 760 pf->ccr_res = NULL; 761 } 762 763 /* 764 * Decrement the reference count, and power down the socket, if 765 * necessary. 766 */ 767 pf->sc->sc_enabled_count--; 768} 769 770/* 771 * simulate the old "probe" routine. In the new world order, the driver 772 * needs to grab devices while in the old they were assigned to the device by 773 * the pccardd process. These symbols are exported to the upper layers. 774 */ 775static int 776pccard_compat_do_probe(device_t bus, device_t dev) 777{ 778 return (CARD_COMPAT_MATCH(dev)); 779} 780 781static int 782pccard_compat_do_attach(device_t bus, device_t dev) 783{ 784 int err; 785 786 err = CARD_COMPAT_PROBE(dev); 787 if (err <= 0) 788 err = CARD_COMPAT_ATTACH(dev); 789 return (err); 790} 791 792#define PCCARD_NPORT 2 793#define PCCARD_NMEM 5 794#define PCCARD_NIRQ 1 795#define PCCARD_NDRQ 0 796 797static int 798pccard_add_children(device_t dev, int busno) 799{ 800 /* Call parent to scan for any current children */ 801 return (0); 802} 803 804static int 805pccard_probe(device_t dev) 806{ 807 device_set_desc(dev, "16-bit PCCard bus"); 808 return (pccard_add_children(dev, device_get_unit(dev))); 809} 810 811static int 812pccard_attach(device_t dev) 813{ 814 struct pccard_softc *sc = PCCARD_SOFTC(dev); 815 816 sc->dev = dev; 817 sc->sc_enabled_count = 0; 818 return (bus_generic_attach(dev)); 819} 820 821static int 822pccard_detach(device_t dev) 823{ 824 pccard_detach_card(dev); 825 return 0; 826} 827 828static int 829pccard_suspend(device_t self) 830{ 831 pccard_detach_card(self); 832 return (0); 833} 834 835static 836int 837pccard_resume(device_t self) 838{ 839 return (0); 840} 841 842static void 843pccard_print_resources(struct resource_list *rl, const char *name, int type, 844 int count, const char *format) 845{ 846 struct resource_list_entry *rle; 847 int printed; 848 int i; 849 850 printed = 0; 851 for (i = 0; i < count; i++) { 852 rle = resource_list_find(rl, type, i); 853 if (rle != NULL) { 854 if (printed == 0) 855 printf(" %s ", name); 856 else if (printed > 0) 857 printf(","); 858 printed++; 859 printf(format, rle->start); 860 if (rle->count > 1) { 861 printf("-"); 862 printf(format, rle->start + rle->count - 1); 863 } 864 } else if (i > 3) { 865 /* check the first few regardless */ 866 break; 867 } 868 } 869} 870 871static int 872pccard_print_child(device_t dev, device_t child) 873{ 874 struct pccard_ivar *devi = PCCARD_IVAR(child); 875 struct resource_list *rl = &devi->resources; 876 int retval = 0; 877 878 retval += bus_print_child_header(dev, child); 879 retval += printf(" at"); 880 881 if (devi != NULL) { 882 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 883 PCCARD_NPORT, "%#lx"); 884 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 885 PCCARD_NMEM, "%#lx"); 886 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 887 "%ld"); 888 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 889 "%ld"); 890 retval += printf(" function %d config %d", devi->fcn->number, 891 devi->fcn->cfe->number); 892 } 893 894 retval += bus_print_child_footer(dev, child); 895 896 return (retval); 897} 898 899static int 900pccard_set_resource(device_t dev, device_t child, int type, int rid, 901 u_long start, u_long count) 902{ 903 struct pccard_ivar *devi = PCCARD_IVAR(child); 904 struct resource_list *rl = &devi->resources; 905 906 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 907 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 908 return (EINVAL); 909 if (rid < 0) 910 return (EINVAL); 911 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 912 return (EINVAL); 913 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 914 return (EINVAL); 915 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 916 return (EINVAL); 917 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 918 return (EINVAL); 919 920 resource_list_add(rl, type, rid, start, start + count - 1, count); 921 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 922 type, &rid, start, start + count - 1, count, 0)) 923 return 0; 924 else 925 return ENOMEM; 926} 927 928static int 929pccard_get_resource(device_t dev, device_t child, int type, int rid, 930 u_long *startp, u_long *countp) 931{ 932 struct pccard_ivar *devi = PCCARD_IVAR(child); 933 struct resource_list *rl = &devi->resources; 934 struct resource_list_entry *rle; 935 936 rle = resource_list_find(rl, type, rid); 937 if (rle == NULL) 938 return (ENOENT); 939 940 if (startp != NULL) 941 *startp = rle->start; 942 if (countp != NULL) 943 *countp = rle->count; 944 945 return (0); 946} 947 948static void 949pccard_delete_resource(device_t dev, device_t child, int type, int rid) 950{ 951 struct pccard_ivar *devi = PCCARD_IVAR(child); 952 struct resource_list *rl = &devi->resources; 953 resource_list_delete(rl, type, rid); 954} 955 956static int 957pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 958 uint32_t flags) 959{ 960 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 961 rid, flags)); 962} 963 964static int 965pccard_set_memory_offset(device_t dev, device_t child, int rid, 966 uint32_t offset, uint32_t *deltap) 967 968{ 969 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 970 offset, deltap)); 971} 972 973static void 974pccard_probe_nomatch(device_t bus, device_t child) 975{ 976 struct pccard_ivar *devi = PCCARD_IVAR(child); 977 struct pccard_function *func = devi->fcn; 978 struct pccard_softc *sc = PCCARD_SOFTC(bus); 979 980 device_printf(bus, "<unknown card>"); 981 printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n", 982 sc->card.manufacturer, sc->card.product, func->number); 983 device_printf(bus, " CIS info: %s, %s, %s\n", sc->card.cis1_info[0], 984 sc->card.cis1_info[1], sc->card.cis1_info[2]); 985 return; 986} 987 988static int 989pccard_child_location_str(device_t bus, device_t child, char *buf, 990 size_t buflen) 991{ 992 struct pccard_ivar *devi = PCCARD_IVAR(child); 993 struct pccard_function *func = devi->fcn; 994 995 snprintf(buf, buflen, "function=%d", func->number); 996 return (0); 997} 998 999static int 1000pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 1001 size_t buflen) 1002{ 1003 struct pccard_ivar *devi = PCCARD_IVAR(child); 1004 struct pccard_function *func = devi->fcn; 1005 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1006 1007 /* XXX need to make sure that we've quoted the " in strings! */ 1008 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 1009 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 1010 sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0], 1011 sc->card.cis1_info[1], func->function); 1012 return (0); 1013} 1014 1015static int 1016pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 1017{ 1018 struct pccard_ivar *devi = PCCARD_IVAR(child); 1019 struct pccard_function *func = devi->fcn; 1020 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1021 1022 switch (which) { 1023 default: 1024 case PCCARD_IVAR_ETHADDR: 1025 bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 1026 break; 1027 case PCCARD_IVAR_VENDOR: 1028 *(uint32_t *) result = sc->card.manufacturer; 1029 break; 1030 case PCCARD_IVAR_PRODUCT: 1031 *(uint32_t *) result = sc->card.product; 1032 break; 1033 case PCCARD_IVAR_PRODEXT: 1034 *(uint16_t *) result = sc->card.prodext; 1035 break; 1036 case PCCARD_IVAR_FUNCTION: 1037 *(uint32_t *) result = func->function; 1038 break; 1039 case PCCARD_IVAR_FUNCTION_NUMBER: 1040 if (!func) { 1041 device_printf(bus, "No function number, bug!\n"); 1042 return (ENOENT); 1043 } 1044 *(uint32_t *) result = func->number; 1045 break; 1046 case PCCARD_IVAR_VENDOR_STR: 1047 *(char **) result = sc->card.cis1_info[0]; 1048 break; 1049 case PCCARD_IVAR_PRODUCT_STR: 1050 *(char **) result = sc->card.cis1_info[1]; 1051 break; 1052 case PCCARD_IVAR_CIS3_STR: 1053 *(char **) result = sc->card.cis1_info[2]; 1054 break; 1055 case PCCARD_IVAR_CIS4_STR: 1056 *(char **) result = sc->card.cis1_info[3]; 1057 break; 1058 } 1059 return (0); 1060} 1061 1062static void 1063pccard_driver_added(device_t dev, driver_t *driver) 1064{ 1065 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1066 struct pccard_function *pf; 1067 device_t child; 1068 1069 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1070 if (STAILQ_EMPTY(&pf->cfe_head)) 1071 continue; 1072 child = pf->dev; 1073 if (device_get_state(child) != DS_NOTPRESENT) 1074 continue; 1075 if (pccard_function_enable(pf) == 0 && 1076 device_probe_and_attach(child) == 0) { 1077 DEVPRINTF((sc->dev, "function %d CCR at %d " 1078 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 1079 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 1080 pccard_ccr_read(pf, 0x00), 1081 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 1082 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 1083 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 1084 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 1085 } else { 1086 if (pf->cfe != NULL) 1087 pccard_function_disable(pf); 1088 } 1089 } 1090 return; 1091} 1092 1093static struct resource * 1094pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1095 u_long start, u_long end, u_long count, u_int flags) 1096{ 1097 struct pccard_ivar *dinfo; 1098 struct resource_list_entry *rle = 0; 1099 int passthrough = (device_get_parent(child) != dev); 1100 int isdefault = (start == 0 && end == ~0UL && count == 1); 1101 struct resource *r = NULL; 1102 1103 /* XXX I'm no longer sure this is right */ 1104 if (passthrough) { 1105 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1106 type, rid, start, end, count, flags)); 1107 } 1108 1109 dinfo = device_get_ivars(child); 1110 rle = resource_list_find(&dinfo->resources, type, *rid); 1111 1112 if (rle == NULL && isdefault) 1113 return (NULL); /* no resource of that type/rid */ 1114 if (rle == NULL || rle->res == NULL) { 1115 /* Do we want this device to own it? */ 1116 /* XXX I think so, but that might be lame XXX */ 1117 r = bus_alloc_resource(dev, type, rid, start, end, 1118 count, flags /* XXX aligment? */); 1119 if (r == NULL) 1120 goto bad; 1121 resource_list_add(&dinfo->resources, type, *rid, 1122 rman_get_start(r), rman_get_end(r), count); 1123 rle = resource_list_find(&dinfo->resources, type, *rid); 1124 if (!rle) 1125 goto bad; 1126 rle->res = r; 1127 } 1128 /* 1129 * XXX the following looks wrong, in theory, but likely it is 1130 * XXX needed because of how the CIS code allocates resources 1131 * XXX for this device. 1132 */ 1133 if (rman_get_device(rle->res) != dev) 1134 return (NULL); 1135 bus_release_resource(dev, type, *rid, rle->res); 1136 rle->res = NULL; 1137 switch(type) { 1138 case SYS_RES_IOPORT: 1139 case SYS_RES_MEMORY: 1140 if (!(flags & RF_ALIGNMENT_MASK)) 1141 flags |= rman_make_alignment_flags(rle->count); 1142 break; 1143 case SYS_RES_IRQ: 1144 flags |= RF_SHAREABLE; 1145 break; 1146 } 1147 rle->res = resource_list_alloc(&dinfo->resources, dev, child, 1148 type, rid, rle->start, rle->end, rle->count, flags); 1149 return (rle->res); 1150bad:; 1151 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1152 return (NULL); 1153} 1154 1155static int 1156pccard_release_resource(device_t dev, device_t child, int type, int rid, 1157 struct resource *r) 1158{ 1159 struct pccard_ivar *dinfo; 1160 int passthrough = (device_get_parent(child) != dev); 1161 struct resource_list_entry *rle = 0; 1162 int ret; 1163 int flags; 1164 1165 if (passthrough) 1166 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1167 type, rid, r); 1168 1169 dinfo = device_get_ivars(child); 1170 1171 rle = resource_list_find(&dinfo->resources, type, rid); 1172 1173 if (!rle) { 1174 device_printf(dev, "Allocated resource not found, " 1175 "%d %x %lx %lx\n", 1176 type, rid, rman_get_start(r), rman_get_size(r)); 1177 return ENOENT; 1178 } 1179 if (!rle->res) { 1180 device_printf(dev, "Allocated resource not recorded\n"); 1181 return ENOENT; 1182 } 1183 1184 ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1185 type, rid, r); 1186 switch(type) { 1187 case SYS_RES_IOPORT: 1188 case SYS_RES_MEMORY: 1189 flags = rman_make_alignment_flags(rle->count); 1190 break; 1191 case SYS_RES_IRQ: 1192 flags = RF_SHAREABLE; 1193 break; 1194 default: 1195 flags = 0; 1196 } 1197 rle->res = bus_alloc_resource(dev, type, &rid, 1198 rle->start, rle->end, rle->count, flags); 1199 if (rle->res == NULL) 1200 device_printf(dev, "release_resource: " 1201 "unable to reaquire resource\n"); 1202 return ret; 1203} 1204 1205static void 1206pccard_child_detached(device_t parent, device_t dev) 1207{ 1208 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1209 struct pccard_function *pf = ivar->fcn; 1210 1211 pccard_function_disable(pf); 1212} 1213 1214static void 1215pccard_intr(void *arg) 1216{ 1217 struct pccard_function *pf = (struct pccard_function*) arg; 1218 int reg; 1219 int doisr = 1; 1220 1221 /* 1222 * MFC cards know if they interrupted, so we have to ack the 1223 * interrupt and call the ISR. Non-MFC cards don't have these 1224 * bits, so they always get called. Many non-MFC cards have 1225 * this bit set always upon read, but some do not. 1226 * 1227 * We always ack the interrupt, even if there's no ISR 1228 * for the card. This is done on the theory that acking 1229 * the interrupt will pacify the card enough to keep an 1230 * interrupt storm from happening. Of course this won't 1231 * help in the non-MFC case. 1232 * 1233 * This has no impact for MPSAFEness of the client drivers. 1234 * We register this with whatever flags the intr_handler 1235 * was registered with. All these functions are MPSAFE. 1236 */ 1237 if (pccard_mfc(pf->sc)) { 1238 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1239 if (reg & PCCARD_CCR_STATUS_INTR) 1240 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1241 reg & ~PCCARD_CCR_STATUS_INTR); 1242 else 1243 doisr = 0; 1244 } 1245 if (pf->intr_handler != NULL && doisr) 1246 pf->intr_handler(pf->intr_handler_arg); 1247} 1248 1249static int 1250pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1251 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1252{ 1253 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1254 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1255 struct pccard_function *func = ivar->fcn; 1256 int err; 1257 1258 if (func->intr_handler != NULL) 1259 panic("Only one interrupt handler per function allowed"); 1260 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 1261 func, cookiep); 1262 if (err != 0) 1263 return (err); 1264 func->intr_handler = intr; 1265 func->intr_handler_arg = arg; 1266 func->intr_handler_cookie = *cookiep; 1267 if (pccard_mfc(sc)) { 1268 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1269 pccard_ccr_read(func, PCCARD_CCR_OPTION) | 1270 PCCARD_CCR_OPTION_IREQ_ENABLE); 1271 } 1272 return (0); 1273} 1274 1275static int 1276pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1277 void *cookie) 1278{ 1279 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1280 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1281 struct pccard_function *func = ivar->fcn; 1282 int ret; 1283 1284 if (pccard_mfc(sc)) { 1285 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1286 pccard_ccr_read(func, PCCARD_CCR_OPTION) & 1287 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1288 } 1289 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1290 if (ret == 0) { 1291 func->intr_handler = NULL; 1292 func->intr_handler_arg = NULL; 1293 func->intr_handler_cookie = NULL; 1294 } 1295 1296 return (ret); 1297} 1298 1299static int 1300pccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1301 struct resource *r) 1302{ 1303 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1304 struct pccard_function *pf = ivar->fcn; 1305 1306 switch(type) { 1307 case SYS_RES_IOPORT: 1308 /* 1309 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1310 * card. 1311 */ 1312 if (pccard_mfc(pf->sc)) 1313 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1314 rman_get_size(r)); 1315 break; 1316 default: 1317 break; 1318 } 1319 return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1320} 1321 1322static int 1323pccard_deactivate_resource(device_t brdev, device_t child, int type, 1324 int rid, struct resource *r) 1325{ 1326 /* XXX undo pccard_activate_resource? XXX */ 1327 return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1328} 1329 1330static device_method_t pccard_methods[] = { 1331 /* Device interface */ 1332 DEVMETHOD(device_probe, pccard_probe), 1333 DEVMETHOD(device_attach, pccard_attach), 1334 DEVMETHOD(device_detach, pccard_detach), 1335 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1336 DEVMETHOD(device_suspend, pccard_suspend), 1337 DEVMETHOD(device_resume, pccard_resume), 1338 1339 /* Bus interface */ 1340 DEVMETHOD(bus_print_child, pccard_print_child), 1341 DEVMETHOD(bus_driver_added, pccard_driver_added), 1342 DEVMETHOD(bus_child_detached, pccard_child_detached), 1343 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1344 DEVMETHOD(bus_release_resource, pccard_release_resource), 1345 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1346 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1347 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1348 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1349 DEVMETHOD(bus_set_resource, pccard_set_resource), 1350 DEVMETHOD(bus_get_resource, pccard_get_resource), 1351 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1352 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1353 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1354 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1355 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1356 1357 /* Card Interface */ 1358 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1359 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1360 DEVMETHOD(card_attach_card, pccard_attach_card), 1361 DEVMETHOD(card_detach_card, pccard_detach_card), 1362 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe), 1363 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach), 1364 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1365 1366 { 0, 0 } 1367}; 1368 1369static driver_t pccard_driver = { 1370 "pccard", 1371 pccard_methods, 1372 sizeof(struct pccard_softc) 1373}; 1374 1375devclass_t pccard_devclass; 1376 1377/* Maybe we need to have a slot device? */ 1378DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1379DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1380MODULE_VERSION(pccard, 1); 1381