pccard.c revision 158086
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 158086 2006-04-27 20:47:13Z 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#include <dev/pccard/pccardvarp.h> 54#include <dev/pccard/pccard_cis.h> 55 56#include "power_if.h" 57#include "card_if.h" 58 59#define PCCARDDEBUG 60 61/* sysctl vars */ 62SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 63 64int pccard_debug = 0; 65TUNABLE_INT("hw.pccard.debug", &pccard_debug); 66SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 67 &pccard_debug, 0, 68 "pccard debug"); 69 70int pccard_cis_debug = 0; 71TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 72SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 73 &pccard_cis_debug, 0, "pccard CIS debug"); 74 75#ifdef PCCARDDEBUG 76#define DPRINTF(arg) if (pccard_debug) printf arg 77#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 78#define PRVERBOSE(arg) printf arg 79#define DEVPRVERBOSE(arg) device_printf arg 80#else 81#define DPRINTF(arg) 82#define DEVPRINTF(arg) 83#define PRVERBOSE(arg) if (bootverbose) printf arg 84#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 85#endif 86 87static int pccard_ccr_read(struct pccard_function *pf, int ccr); 88static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 89static int pccard_attach_card(device_t dev); 90static int pccard_detach_card(device_t dev); 91static void pccard_function_init(struct pccard_function *pf); 92static void pccard_function_free(struct pccard_function *pf); 93static int pccard_function_enable(struct pccard_function *pf); 94static void pccard_function_disable(struct pccard_function *pf); 95static int pccard_probe(device_t dev); 96static int pccard_attach(device_t dev); 97static int pccard_detach(device_t dev); 98static void pccard_print_resources(struct resource_list *rl, 99 const char *name, int type, int count, const char *format); 100static int pccard_print_child(device_t dev, device_t child); 101static int pccard_set_resource(device_t dev, device_t child, int type, 102 int rid, u_long start, u_long count); 103static int pccard_get_resource(device_t dev, device_t child, int type, 104 int rid, u_long *startp, u_long *countp); 105static void pccard_delete_resource(device_t dev, device_t child, int type, 106 int rid); 107static int pccard_set_res_flags(device_t dev, device_t child, int type, 108 int rid, uint32_t flags); 109static int pccard_set_memory_offset(device_t dev, device_t child, int rid, 110 uint32_t offset, uint32_t *deltap); 111static void pccard_probe_nomatch(device_t cbdev, device_t child); 112static int pccard_read_ivar(device_t bus, device_t child, int which, 113 u_char *result); 114static void pccard_driver_added(device_t dev, driver_t *driver); 115static struct resource *pccard_alloc_resource(device_t dev, 116 device_t child, int type, int *rid, u_long start, 117 u_long end, u_long count, u_int flags); 118static int pccard_release_resource(device_t dev, device_t child, int type, 119 int rid, struct resource *r); 120static void pccard_child_detached(device_t parent, device_t dev); 121static void pccard_intr(void *arg); 122static int pccard_setup_intr(device_t dev, device_t child, 123 struct resource *irq, int flags, driver_intr_t *intr, 124 void *arg, void **cookiep); 125static int pccard_teardown_intr(device_t dev, device_t child, 126 struct resource *r, void *cookie); 127 128static const struct pccard_product * 129pccard_do_product_lookup(device_t bus, device_t dev, 130 const struct pccard_product *tab, size_t ent_size, 131 pccard_product_match_fn matchfn); 132 133 134static int 135pccard_ccr_read(struct pccard_function *pf, int ccr) 136{ 137 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 138 pf->pf_ccr_offset + ccr)); 139} 140 141static void 142pccard_ccr_write(struct pccard_function *pf, int ccr, int val) 143{ 144 if ((pf->ccr_mask) & (1 << (ccr / 2))) { 145 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 146 pf->pf_ccr_offset + ccr, val); 147 } 148} 149 150static int 151pccard_set_default_descr(device_t dev) 152{ 153 const char *vendorstr, *prodstr; 154 uint32_t vendor, prod; 155 char *str; 156 157 if (pccard_get_vendor_str(dev, &vendorstr)) 158 return (0); 159 if (pccard_get_product_str(dev, &prodstr)) 160 return (0); 161 if (vendorstr != NULL && prodstr != NULL) { 162 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 163 M_WAITOK); 164 sprintf(str, "%s %s", vendorstr, prodstr); 165 device_set_desc_copy(dev, str); 166 free(str, M_DEVBUF); 167 } else { 168 if (pccard_get_vendor(dev, &vendor)) 169 return (0); 170 if (pccard_get_product(dev, &prod)) 171 return (0); 172 str = malloc(100, M_DEVBUF, M_WAITOK); 173 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod); 174 device_set_desc_copy(dev, str); 175 free(str, M_DEVBUF); 176 } 177 return (0); 178} 179 180static int 181pccard_attach_card(device_t dev) 182{ 183 struct pccard_softc *sc = PCCARD_SOFTC(dev); 184 struct pccard_function *pf; 185 struct pccard_ivar *ivar; 186 device_t child; 187 int i; 188 189 if (!STAILQ_EMPTY(&sc->card.pf_head)) { 190 if (bootverbose || pccard_debug) 191 device_printf(dev, "Card already inserted.\n"); 192 } 193 194 DEVPRINTF((dev, "chip_socket_enable\n")); 195 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 196 197 DEVPRINTF((dev, "read_cis\n")); 198 pccard_read_cis(sc); 199 200 DEVPRINTF((dev, "check_cis_quirks\n")); 201 pccard_check_cis_quirks(dev); 202 203 /* 204 * bail now if the card has no functions, or if there was an error in 205 * the cis. 206 */ 207 208 if (sc->card.error) { 209 device_printf(dev, "CARD ERROR!\n"); 210 return (1); 211 } 212 if (STAILQ_EMPTY(&sc->card.pf_head)) { 213 device_printf(dev, "Card has no functions!\n"); 214 return (1); 215 } 216 217 if (bootverbose || pccard_debug) 218 pccard_print_cis(dev); 219 220 DEVPRINTF((dev, "functions scanning\n")); 221 i = -1; 222 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 223 i++; 224 if (STAILQ_EMPTY(&pf->cfe_head)) { 225 device_printf(dev, 226 "Function %d has no config entries.!\n", i); 227 continue; 228 } 229 pf->sc = sc; 230 pf->cfe = NULL; 231 pf->dev = NULL; 232 } 233 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 234 pccard_mfc(sc))); 235 236 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 237 if (STAILQ_EMPTY(&pf->cfe_head)) 238 continue; 239 /* 240 * In NetBSD, the drivers are responsible for activating 241 * each function of a card. I think that in FreeBSD we 242 * want to activate them enough for the usual bus_*_resource 243 * routines will do the right thing. This many mean a 244 * departure from the current NetBSD model. 245 * 246 * This seems to work well in practice for most cards. 247 * However, there are two cases that are problematic. 248 * If a driver wishes to pick and chose which config 249 * entry to use, then this method falls down. These 250 * are usually older cards. In addition, there are 251 * some cards that have multiple hardware units on the 252 * cards, but presents only one CIS chain. These cards 253 * are combination cards, but only one of these units 254 * can be on at a time. 255 */ 256 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 257 M_WAITOK | M_ZERO); 258 resource_list_init(&ivar->resources); 259 child = device_add_child(dev, NULL, -1); 260 device_set_ivars(child, ivar); 261 ivar->pf = pf; 262 pf->dev = child; 263 /* 264 * XXX We might want to move the next three lines into 265 * XXX the pccard interface layer. For the moment, this 266 * XXX is OK, but some drivers want to pick the config 267 * XXX entry to use as well as some address tweaks (mostly 268 * XXX due to bugs in decode logic that makes some 269 * XXX addresses illegal or broken). 270 */ 271 pccard_function_init(pf); 272 if (sc->sc_enabled_count == 0) 273 POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 274 if (pccard_function_enable(pf) == 0 && 275 pccard_set_default_descr(child) == 0 && 276 device_probe_and_attach(child) == 0) { 277 DEVPRINTF((sc->dev, "function %d CCR at %d " 278 "offset %x mask %x: " 279 "%x %x %x %x, %x %x %x %x, %x\n", 280 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 281 pf->ccr_mask, pccard_ccr_read(pf, 0x00), 282 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 283 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 284 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 285 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 286 } else { 287 if (pf->cfe != NULL) 288 pccard_function_disable(pf); 289 } 290 } 291 return (0); 292} 293 294static int 295pccard_detach_card(device_t dev) 296{ 297 struct pccard_softc *sc = PCCARD_SOFTC(dev); 298 struct pccard_function *pf; 299 struct pccard_config_entry *cfe; 300 int state; 301 302 /* 303 * We are running on either the PCCARD socket's event thread 304 * or in user context detaching a device by user request. 305 */ 306 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 307 if (pf->dev == NULL) 308 continue; 309 state = device_get_state(pf->dev); 310 if (state == DS_ATTACHED || state == DS_BUSY) 311 device_detach(pf->dev); 312 if (pf->cfe != NULL) 313 pccard_function_disable(pf); 314 pccard_function_free(pf); 315 device_delete_child(dev, pf->dev); 316 } 317 if (sc->sc_enabled_count == 0) 318 POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 319 320 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 321 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 322 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 323 free(cfe, M_DEVBUF); 324 } 325 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 326 free(pf, M_DEVBUF); 327 } 328 STAILQ_INIT(&sc->card.pf_head); 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 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 421 struct resource_list *rl = &devi->resources; 422 struct resource_list_entry *rle; 423 struct resource *r = 0; 424 device_t bus; 425 u_long start, end, len; 426 int i, rid, spaces; 427 428 if (pf->pf_flags & PFF_ENABLED) { 429 printf("pccard_function_init: function is enabled"); 430 return; 431 } 432 bus = device_get_parent(pf->dev); 433 /* Remember which configuration entry we are using. */ 434 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 435 if (cfe->iftype != PCCARD_IFTYPE_IO) 436 continue; 437 spaces = 0; 438 for (i = 0; i < cfe->num_iospace; i++) { 439 start = cfe->iospace[i].start; 440 if (start) 441 end = start + cfe->iospace[i].length - 1; 442 else 443 end = ~0UL; 444 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n", 445 i, start, end)); 446 rid = i; 447 len = cfe->iospace[i].length; 448 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid, 449 start, end, len, rman_make_alignment_flags(len)); 450 if (r == NULL) 451 goto not_this_one; 452 rle = resource_list_add(rl, SYS_RES_IOPORT, 453 rid, rman_get_start(r), rman_get_end(r), 454 cfe->iospace[i].length); 455 if (rle == NULL) 456 panic("Cannot add resource rid %d IOPORT", rid); 457 rle->res = r; 458 spaces++; 459 } 460 for (i = 0; i < cfe->num_memspace; i++) { 461 start = cfe->memspace[i].hostaddr; 462 if (start) 463 end = start + cfe->memspace[i].length - 1; 464 else 465 end = ~0UL; 466 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n", 467 i, start, end)); 468 rid = i; 469 len = cfe->memspace[i].length; 470 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid, 471 start, end, len, rman_make_alignment_flags(len)); 472 if (r == NULL) 473 goto not_this_one; 474 rle = resource_list_add(rl, SYS_RES_MEMORY, 475 rid, rman_get_start(r), rman_get_end(r), 476 cfe->memspace[i].length); 477 if (rle == NULL) 478 panic("Cannot add resource rid %d MEM", rid); 479 rle->res = r; 480 spaces++; 481 } 482 if (spaces == 0) { 483 DEVPRINTF((bus, "Neither memory nor I/O mapped\n")); 484 goto not_this_one; 485 } 486 if (cfe->irqmask) { 487 rid = 0; 488 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid, 489 RF_SHAREABLE); 490 if (r == NULL) 491 goto not_this_one; 492 rle = resource_list_add(rl, SYS_RES_IRQ, rid, 493 rman_get_start(r), rman_get_end(r), 1); 494 if (rle == NULL) 495 panic("Cannot add resource rid %d IRQ", rid); 496 rle->res = r; 497 } 498 /* If we get to here, we've allocated all we need */ 499 pf->cfe = cfe; 500 break; 501 not_this_one:; 502 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 503 cfe->number)); 504 resource_list_purge(rl); 505 } 506} 507 508/* 509 * Free resources allocated by pccard_function_init(), May be called as long 510 * as the function is disabled. 511 * 512 * NOTE: This function should be unnecessary. pccard_function_init should 513 * never keep resources initialized. 514 */ 515static void 516pccard_function_free(struct pccard_function *pf) 517{ 518 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 519 struct resource_list_entry *rle; 520 521 if (pf->pf_flags & PFF_ENABLED) { 522 printf("pccard_function_init: function is enabled"); 523 return; 524 } 525 526 STAILQ_FOREACH(rle, &devi->resources, link) { 527 if (rle->res) { 528 if (rman_get_device(rle->res) != pf->sc->dev) 529 device_printf(pf->sc->dev, 530 "function_free: Resource still owned by " 531 "child, oops. " 532 "(type=%d, rid=%d, addr=%lx)\n", 533 rle->type, rle->rid, 534 rman_get_start(rle->res)); 535 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 536 pf->sc->dev, rle->type, rle->rid, rle->res); 537 rle->res = NULL; 538 } 539 } 540 resource_list_free(&devi->resources); 541} 542 543static void 544pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr, 545 bus_addr_t offset, bus_size_t size) 546{ 547 bus_size_t iosize, tmp; 548 549 if (addr != 0) { 550 if (pf->pf_mfc_iomax == 0) { 551 pf->pf_mfc_iobase = addr + offset; 552 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size; 553 } else { 554 /* this makes the assumption that nothing overlaps */ 555 if (pf->pf_mfc_iobase > addr + offset) 556 pf->pf_mfc_iobase = addr + offset; 557 if (pf->pf_mfc_iomax < addr + offset + size) 558 pf->pf_mfc_iomax = addr + offset + size; 559 } 560 } 561 562 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 563 /* round up to nearest (2^n)-1 */ 564 for (iosize = 1; iosize < tmp; iosize <<= 1) 565 ; 566 iosize--; 567 568 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n", 569 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1))); 570 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 571 pf->pf_mfc_iobase & 0xff); 572 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 573 (pf->pf_mfc_iobase >> 8) & 0xff); 574 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 575 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 576 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 577} 578 579/* Enable a PCCARD function */ 580static int 581pccard_function_enable(struct pccard_function *pf) 582{ 583 struct pccard_function *tmp; 584 int reg; 585 device_t dev = pf->sc->dev; 586 587 if (pf->cfe == NULL) { 588 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 589 return (ENOMEM); 590 } 591 592 /* 593 * Increase the reference count on the socket, enabling power, if 594 * necessary. 595 */ 596 pf->sc->sc_enabled_count++; 597 598 if (pf->pf_flags & PFF_ENABLED) { 599 /* 600 * Don't do anything if we're already enabled. 601 */ 602 return (0); 603 } 604 605 /* 606 * it's possible for different functions' CCRs to be in the same 607 * underlying page. Check for that. 608 */ 609 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 610 if ((tmp->pf_flags & PFF_ENABLED) && 611 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 612 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 613 (tmp->ccr_base - tmp->pf_ccr_offset + 614 tmp->pf_ccr_realsize))) { 615 pf->pf_ccrt = tmp->pf_ccrt; 616 pf->pf_ccrh = tmp->pf_ccrh; 617 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 618 619 /* 620 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 621 * tmp->ccr_base) + pf->ccr_base; 622 */ 623 /* pf->pf_ccr_offset = 624 (tmp->pf_ccr_offset + pf->ccr_base) - 625 tmp->ccr_base; */ 626 pf->pf_ccr_window = tmp->pf_ccr_window; 627 break; 628 } 629 } 630 if (tmp == NULL) { 631 pf->ccr_rid = 0; 632 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 633 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE); 634 if (!pf->ccr_res) 635 goto bad; 636 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 637 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 638 pf->ccr_base)); 639 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 640 pf->ccr_rid, PCCARD_A_MEM_ATTR); 641 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 642 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 643 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 644 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 645 pf->pf_ccr_realsize = 1; 646 } 647 648 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 649 reg |= PCCARD_CCR_OPTION_LEVIREQ; 650 if (pccard_mfc(pf->sc)) { 651 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 652 PCCARD_CCR_OPTION_ADDR_DECODE); 653 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 654 } 655 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 656 657 reg = 0; 658 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 659 reg |= PCCARD_CCR_STATUS_IOIS8; 660 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 661 reg |= PCCARD_CCR_STATUS_AUDIO; 662 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 663 664 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 665 666 if (pccard_mfc(pf->sc)) 667 pccard_mfc_adjust_iobase(pf, 0, 0, 0); 668 669#ifdef PCCARDDEBUG 670 if (pccard_debug) { 671 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 672 device_printf(tmp->sc->dev, 673 "function %d CCR at %d offset %x: " 674 "%x %x %x %x, %x %x %x %x, %x\n", 675 tmp->number, tmp->pf_ccr_window, 676 tmp->pf_ccr_offset, 677 pccard_ccr_read(tmp, 0x00), 678 pccard_ccr_read(tmp, 0x02), 679 pccard_ccr_read(tmp, 0x04), 680 pccard_ccr_read(tmp, 0x06), 681 pccard_ccr_read(tmp, 0x0A), 682 pccard_ccr_read(tmp, 0x0C), 683 pccard_ccr_read(tmp, 0x0E), 684 pccard_ccr_read(tmp, 0x10), 685 pccard_ccr_read(tmp, 0x12)); 686 } 687 } 688#endif 689 pf->pf_flags |= PFF_ENABLED; 690 return (0); 691 692 bad: 693 /* 694 * Decrement the reference count, and power down the socket, if 695 * necessary. 696 */ 697 pf->sc->sc_enabled_count--; 698 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 699 700 return (1); 701} 702 703/* Disable PCCARD function. */ 704static void 705pccard_function_disable(struct pccard_function *pf) 706{ 707 struct pccard_function *tmp; 708 device_t dev = pf->sc->dev; 709 710 if (pf->cfe == NULL) 711 panic("pccard_function_disable: function not initialized"); 712 713 if ((pf->pf_flags & PFF_ENABLED) == 0) { 714 /* 715 * Don't do anything if we're already disabled. 716 */ 717 return; 718 } 719 720 if (pf->intr_handler != NULL) { 721 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 722 struct resource_list_entry *rle = 723 resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 724 if (rle == NULL) 725 panic("Can't disable an interrupt with no IRQ res\n"); 726 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 727 pf->intr_handler_cookie); 728 } 729 730 /* 731 * it's possible for different functions' CCRs to be in the same 732 * underlying page. Check for that. Note we mark us as disabled 733 * first to avoid matching ourself. 734 */ 735 736 pf->pf_flags &= ~PFF_ENABLED; 737 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 738 if ((tmp->pf_flags & PFF_ENABLED) && 739 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 740 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 741 (tmp->ccr_base - tmp->pf_ccr_offset + 742 tmp->pf_ccr_realsize))) 743 break; 744 } 745 746 /* Not used by anyone else; unmap the CCR. */ 747 if (tmp == NULL) { 748 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 749 pf->ccr_res); 750 pf->ccr_res = NULL; 751 } 752 753 /* 754 * Decrement the reference count, and power down the socket, if 755 * necessary. 756 */ 757 pf->sc->sc_enabled_count--; 758} 759 760#define PCCARD_NPORT 2 761#define PCCARD_NMEM 5 762#define PCCARD_NIRQ 1 763#define PCCARD_NDRQ 0 764 765static int 766pccard_probe(device_t dev) 767{ 768 device_set_desc(dev, "16-bit PCCard bus"); 769 return (0); 770} 771 772static int 773pccard_attach(device_t dev) 774{ 775 struct pccard_softc *sc = PCCARD_SOFTC(dev); 776 int err; 777 778 sc->dev = dev; 779 sc->sc_enabled_count = 0; 780 if ((err = pccard_device_create(sc)) != 0) 781 return (err); 782 STAILQ_INIT(&sc->card.pf_head); 783 return (bus_generic_attach(dev)); 784} 785 786static int 787pccard_detach(device_t dev) 788{ 789 pccard_detach_card(dev); 790 pccard_device_destroy(device_get_softc(dev)); 791 return (0); 792} 793 794static int 795pccard_suspend(device_t self) 796{ 797 pccard_detach_card(self); 798 return (0); 799} 800 801static 802int 803pccard_resume(device_t self) 804{ 805 return (0); 806} 807 808static void 809pccard_print_resources(struct resource_list *rl, const char *name, int type, 810 int count, const char *format) 811{ 812 struct resource_list_entry *rle; 813 int printed; 814 int i; 815 816 printed = 0; 817 for (i = 0; i < count; i++) { 818 rle = resource_list_find(rl, type, i); 819 if (rle != NULL) { 820 if (printed == 0) 821 printf(" %s ", name); 822 else if (printed > 0) 823 printf(","); 824 printed++; 825 printf(format, rle->start); 826 if (rle->count > 1) { 827 printf("-"); 828 printf(format, rle->start + rle->count - 1); 829 } 830 } else if (i > 3) { 831 /* check the first few regardless */ 832 break; 833 } 834 } 835} 836 837static int 838pccard_print_child(device_t dev, device_t child) 839{ 840 struct pccard_ivar *devi = PCCARD_IVAR(child); 841 struct resource_list *rl = &devi->resources; 842 int retval = 0; 843 844 retval += bus_print_child_header(dev, child); 845 retval += printf(" at"); 846 847 if (devi != NULL) { 848 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 849 PCCARD_NPORT, "%#lx"); 850 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 851 PCCARD_NMEM, "%#lx"); 852 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 853 "%ld"); 854 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 855 "%ld"); 856 retval += printf(" function %d config %d", devi->pf->number, 857 devi->pf->cfe->number); 858 } 859 860 retval += bus_print_child_footer(dev, child); 861 862 return (retval); 863} 864 865static int 866pccard_set_resource(device_t dev, device_t child, int type, int rid, 867 u_long start, u_long count) 868{ 869 struct pccard_ivar *devi = PCCARD_IVAR(child); 870 struct resource_list *rl = &devi->resources; 871 872 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 873 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 874 return (EINVAL); 875 if (rid < 0) 876 return (EINVAL); 877 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 878 return (EINVAL); 879 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 880 return (EINVAL); 881 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 882 return (EINVAL); 883 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 884 return (EINVAL); 885 886 resource_list_add(rl, type, rid, start, start + count - 1, count); 887 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 888 type, &rid, start, start + count - 1, count, 0)) 889 return 0; 890 else 891 return ENOMEM; 892} 893 894static int 895pccard_get_resource(device_t dev, device_t child, int type, int rid, 896 u_long *startp, u_long *countp) 897{ 898 struct pccard_ivar *devi = PCCARD_IVAR(child); 899 struct resource_list *rl = &devi->resources; 900 struct resource_list_entry *rle; 901 902 rle = resource_list_find(rl, type, rid); 903 if (rle == NULL) 904 return (ENOENT); 905 906 if (startp != NULL) 907 *startp = rle->start; 908 if (countp != NULL) 909 *countp = rle->count; 910 911 return (0); 912} 913 914static void 915pccard_delete_resource(device_t dev, device_t child, int type, int rid) 916{ 917 struct pccard_ivar *devi = PCCARD_IVAR(child); 918 struct resource_list *rl = &devi->resources; 919 resource_list_delete(rl, type, rid); 920} 921 922static int 923pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 924 uint32_t flags) 925{ 926 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 927 rid, flags)); 928} 929 930static int 931pccard_set_memory_offset(device_t dev, device_t child, int rid, 932 uint32_t offset, uint32_t *deltap) 933 934{ 935 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 936 offset, deltap)); 937} 938 939static void 940pccard_probe_nomatch(device_t bus, device_t child) 941{ 942 struct pccard_ivar *devi = PCCARD_IVAR(child); 943 struct pccard_function *pf = devi->pf; 944 struct pccard_softc *sc = PCCARD_SOFTC(bus); 945 int i; 946 947 device_printf(bus, "<unknown card>"); 948 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) " 949 "at function %d\n", sc->card.manufacturer, sc->card.product, 950 pf->function, pf->number); 951 device_printf(bus, " CIS info: "); 952 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++) 953 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]); 954 printf("\n"); 955 return; 956} 957 958static int 959pccard_child_location_str(device_t bus, device_t child, char *buf, 960 size_t buflen) 961{ 962 struct pccard_ivar *devi = PCCARD_IVAR(child); 963 struct pccard_function *pf = devi->pf; 964 965 snprintf(buf, buflen, "function=%d", pf->number); 966 return (0); 967} 968 969/* XXX Maybe this should be in subr_bus? */ 970static void 971pccard_safe_quote(char *dst, const char *src, size_t len) 972{ 973 char *walker = dst, *ep = dst + len - 1; 974 975 if (len == 0) 976 return; 977 while (src != NULL && walker < ep) 978 { 979 if (*src == '"') { 980 if (ep - walker < 2) 981 break; 982 *walker++ = '\\'; 983 } 984 *walker++ = *src++; 985 } 986 *walker = '\0'; 987} 988 989static int 990pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 991 size_t buflen) 992{ 993 struct pccard_ivar *devi = PCCARD_IVAR(child); 994 struct pccard_function *pf = devi->pf; 995 struct pccard_softc *sc = PCCARD_SOFTC(bus); 996 char cis0[128], cis1[128]; 997 998 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0)); 999 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1)); 1000 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 1001 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 1002 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function); 1003 return (0); 1004} 1005 1006static int 1007pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 1008{ 1009 struct pccard_ivar *devi = PCCARD_IVAR(child); 1010 struct pccard_function *pf = devi->pf; 1011 struct pccard_softc *sc = PCCARD_SOFTC(bus); 1012 1013 if (!pf) 1014 panic("No pccard function pointer"); 1015 switch (which) { 1016 default: 1017 return (EINVAL); 1018 case PCCARD_IVAR_ETHADDR: 1019 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 1020 break; 1021 case PCCARD_IVAR_VENDOR: 1022 *(uint32_t *)result = sc->card.manufacturer; 1023 break; 1024 case PCCARD_IVAR_PRODUCT: 1025 *(uint32_t *)result = sc->card.product; 1026 break; 1027 case PCCARD_IVAR_PRODEXT: 1028 *(uint16_t *)result = sc->card.prodext; 1029 break; 1030 case PCCARD_IVAR_FUNCTION: 1031 *(uint32_t *)result = pf->function; 1032 break; 1033 case PCCARD_IVAR_FUNCTION_NUMBER: 1034 *(uint32_t *)result = pf->number; 1035 break; 1036 case PCCARD_IVAR_VENDOR_STR: 1037 *(const char **)result = sc->card.cis1_info[0]; 1038 break; 1039 case PCCARD_IVAR_PRODUCT_STR: 1040 *(const char **)result = sc->card.cis1_info[1]; 1041 break; 1042 case PCCARD_IVAR_CIS3_STR: 1043 *(const char **)result = sc->card.cis1_info[2]; 1044 break; 1045 case PCCARD_IVAR_CIS4_STR: 1046 *(const char **)result = sc->card.cis1_info[3]; 1047 break; 1048 } 1049 return (0); 1050} 1051 1052static void 1053pccard_driver_added(device_t dev, driver_t *driver) 1054{ 1055 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1056 struct pccard_function *pf; 1057 device_t child; 1058 1059 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1060 if (STAILQ_EMPTY(&pf->cfe_head)) 1061 continue; 1062 child = pf->dev; 1063 if (device_get_state(child) != DS_NOTPRESENT) 1064 continue; 1065 if (pccard_function_enable(pf) == 0 && 1066 device_probe_and_attach(child) == 0) { 1067 DEVPRINTF((sc->dev, "function %d CCR at %d " 1068 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 1069 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 1070 pccard_ccr_read(pf, 0x00), 1071 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 1072 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 1073 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 1074 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 1075 } else { 1076 if (pf->cfe != NULL) 1077 pccard_function_disable(pf); 1078 } 1079 } 1080 return; 1081} 1082 1083static struct resource * 1084pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1085 u_long start, u_long end, u_long count, u_int flags) 1086{ 1087 struct pccard_ivar *dinfo; 1088 struct resource_list_entry *rle = 0; 1089 int passthrough = (device_get_parent(child) != dev); 1090 int isdefault = (start == 0 && end == ~0UL && count == 1); 1091 struct resource *r = NULL; 1092 1093 /* XXX I'm no longer sure this is right */ 1094 if (passthrough) { 1095 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1096 type, rid, start, end, count, flags)); 1097 } 1098 1099 dinfo = device_get_ivars(child); 1100 rle = resource_list_find(&dinfo->resources, type, *rid); 1101 1102 if (rle == NULL && isdefault) 1103 return (NULL); /* no resource of that type/rid */ 1104 if (rle == NULL || rle->res == NULL) { 1105 /* XXX Need to adjust flags */ 1106 r = bus_alloc_resource(dev, type, rid, start, end, 1107 count, flags); 1108 if (r == NULL) 1109 goto bad; 1110 resource_list_add(&dinfo->resources, type, *rid, 1111 rman_get_start(r), rman_get_end(r), count); 1112 rle = resource_list_find(&dinfo->resources, type, *rid); 1113 if (!rle) 1114 goto bad; 1115 rle->res = r; 1116 } 1117 /* 1118 * If dev doesn't own the device, then we can't give this device 1119 * out. 1120 */ 1121 if (rman_get_device(rle->res) != dev) 1122 return (NULL); 1123 rman_set_device(rle->res, child); 1124 if (flags & RF_ACTIVE) 1125 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res); 1126 return (rle->res); 1127bad:; 1128 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1129 return (NULL); 1130} 1131 1132static int 1133pccard_release_resource(device_t dev, device_t child, int type, int rid, 1134 struct resource *r) 1135{ 1136 struct pccard_ivar *dinfo; 1137 int passthrough = (device_get_parent(child) != dev); 1138 struct resource_list_entry *rle = 0; 1139 1140 if (passthrough) 1141 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1142 type, rid, r); 1143 1144 dinfo = device_get_ivars(child); 1145 1146 rle = resource_list_find(&dinfo->resources, type, rid); 1147 1148 if (!rle) { 1149 device_printf(dev, "Allocated resource not found, " 1150 "%d %x %lx %lx\n", 1151 type, rid, rman_get_start(r), rman_get_size(r)); 1152 return ENOENT; 1153 } 1154 if (!rle->res) { 1155 device_printf(dev, "Allocated resource not recorded\n"); 1156 return ENOENT; 1157 } 1158 /* 1159 * Deactivate the resource (since it is being released), and 1160 * assign it to the bus. 1161 */ 1162 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res); 1163 rman_set_device(rle->res, dev); 1164 return (0); 1165} 1166 1167static void 1168pccard_child_detached(device_t parent, device_t dev) 1169{ 1170 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1171 struct pccard_function *pf = ivar->pf; 1172 1173 pccard_function_disable(pf); 1174} 1175 1176static void 1177pccard_intr(void *arg) 1178{ 1179 struct pccard_function *pf = (struct pccard_function*) arg; 1180 int reg; 1181 int doisr = 1; 1182 1183 /* 1184 * MFC cards know if they interrupted, so we have to ack the 1185 * interrupt and call the ISR. Non-MFC cards don't have these 1186 * bits, so they always get called. Many non-MFC cards have 1187 * this bit set always upon read, but some do not. 1188 * 1189 * We always ack the interrupt, even if there's no ISR 1190 * for the card. This is done on the theory that acking 1191 * the interrupt will pacify the card enough to keep an 1192 * interrupt storm from happening. Of course this won't 1193 * help in the non-MFC case. 1194 * 1195 * This has no impact for MPSAFEness of the client drivers. 1196 * We register this with whatever flags the intr_handler 1197 * was registered with. All these functions are MPSAFE. 1198 */ 1199 if (pccard_mfc(pf->sc)) { 1200 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1201 if (reg & PCCARD_CCR_STATUS_INTR) 1202 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1203 reg & ~PCCARD_CCR_STATUS_INTR); 1204 else 1205 doisr = 0; 1206 } 1207 if (pf->intr_handler != NULL && doisr) 1208 pf->intr_handler(pf->intr_handler_arg); 1209} 1210 1211static int 1212pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1213 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1214{ 1215 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1216 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1217 struct pccard_function *pf = ivar->pf; 1218 int err; 1219 1220 if (pf->intr_handler != NULL) 1221 panic("Only one interrupt handler per function allowed"); 1222 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 1223 pf, cookiep); 1224 if (err != 0) 1225 return (err); 1226 pf->intr_handler = intr; 1227 pf->intr_handler_arg = arg; 1228 pf->intr_handler_cookie = *cookiep; 1229 if (pccard_mfc(sc)) { 1230 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1231 pccard_ccr_read(pf, PCCARD_CCR_OPTION) | 1232 PCCARD_CCR_OPTION_IREQ_ENABLE); 1233 } 1234 return (0); 1235} 1236 1237static int 1238pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1239 void *cookie) 1240{ 1241 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1242 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1243 struct pccard_function *pf = ivar->pf; 1244 int ret; 1245 1246 if (pccard_mfc(sc)) { 1247 pccard_ccr_write(pf, PCCARD_CCR_OPTION, 1248 pccard_ccr_read(pf, PCCARD_CCR_OPTION) & 1249 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1250 } 1251 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1252 if (ret == 0) { 1253 pf->intr_handler = NULL; 1254 pf->intr_handler_arg = NULL; 1255 pf->intr_handler_cookie = NULL; 1256 } 1257 1258 return (ret); 1259} 1260 1261static int 1262pccard_activate_resource(device_t brdev, device_t child, int type, int rid, 1263 struct resource *r) 1264{ 1265 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1266 struct pccard_function *pf = ivar->pf; 1267 1268 switch(type) { 1269 case SYS_RES_IOPORT: 1270 /* 1271 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC 1272 * card. 1273 */ 1274 if (pccard_mfc(pf->sc)) 1275 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0, 1276 rman_get_size(r)); 1277 break; 1278 default: 1279 break; 1280 } 1281 return (bus_generic_activate_resource(brdev, child, type, rid, r)); 1282} 1283 1284static int 1285pccard_deactivate_resource(device_t brdev, device_t child, int type, 1286 int rid, struct resource *r) 1287{ 1288 /* XXX undo pccard_activate_resource? XXX */ 1289 return (bus_generic_deactivate_resource(brdev, child, type, rid, r)); 1290} 1291 1292static int 1293pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset, 1294 uint8_t *val) 1295{ 1296 struct pccard_ivar *devi = PCCARD_IVAR(child); 1297 struct pccard_function *pf = devi->pf; 1298 1299 /* 1300 * Optimization. Most of the time, devices want to access 1301 * the same page of the attribute memory that the CCR is in. 1302 * We take advantage of this fact here. 1303 */ 1304 if (offset / PCCARD_MEM_PAGE_SIZE == 1305 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1306 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 1307 offset % PCCARD_MEM_PAGE_SIZE); 1308 else { 1309 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1310 &offset); 1311 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset); 1312 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1313 &offset); 1314 } 1315 return 0; 1316} 1317 1318static int 1319pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset, 1320 uint8_t val) 1321{ 1322 struct pccard_ivar *devi = PCCARD_IVAR(child); 1323 struct pccard_function *pf = devi->pf; 1324 1325 /* 1326 * Optimization. Most of the time, devices want to access 1327 * the same page of the attribute memory that the CCR is in. 1328 * We take advantage of this fact here. 1329 */ 1330 if (offset / PCCARD_MEM_PAGE_SIZE == 1331 pf->ccr_base / PCCARD_MEM_PAGE_SIZE) 1332 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 1333 offset % PCCARD_MEM_PAGE_SIZE, val); 1334 else { 1335 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset, 1336 &offset); 1337 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val); 1338 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base, 1339 &offset); 1340 } 1341 1342 return 0; 1343} 1344 1345static int 1346pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset, 1347 uint8_t *val) 1348{ 1349 struct pccard_ivar *devi = PCCARD_IVAR(child); 1350 1351 *val = pccard_ccr_read(devi->pf, offset); 1352 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset, 1353 devi->pf->pf_ccr_offset, *val); 1354 return 0; 1355} 1356 1357static int 1358pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset, 1359 uint8_t val) 1360{ 1361 struct pccard_ivar *devi = PCCARD_IVAR(child); 1362 struct pccard_function *pf = devi->pf; 1363 1364 /* 1365 * Can't use pccard_ccr_write since client drivers may access 1366 * registers not contained in the 'mask' if they are non-standard. 1367 */ 1368 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset, 1369 devi->pf->pf_ccr_offset); 1370 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset, 1371 val); 1372 return 0; 1373} 1374 1375 1376static device_method_t pccard_methods[] = { 1377 /* Device interface */ 1378 DEVMETHOD(device_probe, pccard_probe), 1379 DEVMETHOD(device_attach, pccard_attach), 1380 DEVMETHOD(device_detach, pccard_detach), 1381 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1382 DEVMETHOD(device_suspend, pccard_suspend), 1383 DEVMETHOD(device_resume, pccard_resume), 1384 1385 /* Bus interface */ 1386 DEVMETHOD(bus_print_child, pccard_print_child), 1387 DEVMETHOD(bus_driver_added, pccard_driver_added), 1388 DEVMETHOD(bus_child_detached, pccard_child_detached), 1389 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1390 DEVMETHOD(bus_release_resource, pccard_release_resource), 1391 DEVMETHOD(bus_activate_resource, pccard_activate_resource), 1392 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource), 1393 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1394 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1395 DEVMETHOD(bus_set_resource, pccard_set_resource), 1396 DEVMETHOD(bus_get_resource, pccard_get_resource), 1397 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1398 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1399 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1400 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1401 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1402 1403 /* Card Interface */ 1404 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1405 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1406 DEVMETHOD(card_attach_card, pccard_attach_card), 1407 DEVMETHOD(card_detach_card, pccard_detach_card), 1408 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1409 DEVMETHOD(card_cis_scan, pccard_scan_cis), 1410 DEVMETHOD(card_attr_read, pccard_attr_read_impl), 1411 DEVMETHOD(card_attr_write, pccard_attr_write_impl), 1412 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl), 1413 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl), 1414 1415 { 0, 0 } 1416}; 1417 1418static driver_t pccard_driver = { 1419 "pccard", 1420 pccard_methods, 1421 sizeof(struct pccard_softc) 1422}; 1423 1424devclass_t pccard_devclass; 1425 1426/* Maybe we need to have a slot device? */ 1427DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1428DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1429MODULE_VERSION(pccard, 1); 1430