pccard.c revision 113300
1240616Sjimharris/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */ 2253112Sjimharris/* $FreeBSD: head/sys/dev/pccard/pccard.c 113300 2003-04-09 15:54:00Z imp $ */ 3240616Sjimharris 4240616Sjimharris/* 5240616Sjimharris * Copyright (c) 1997 Marc Horowitz. All rights reserved. 6240616Sjimharris * 7240616Sjimharris * Redistribution and use in source and binary forms, with or without 8240616Sjimharris * modification, are permitted provided that the following conditions 9240616Sjimharris * are met: 10240616Sjimharris * 1. Redistributions of source code must retain the above copyright 11240616Sjimharris * notice, this list of conditions and the following disclaimer. 12240616Sjimharris * 2. Redistributions in binary form must reproduce the above copyright 13240616Sjimharris * notice, this list of conditions and the following disclaimer in the 14240616Sjimharris * documentation and/or other materials provided with the distribution. 15240616Sjimharris * 3. All advertising materials mentioning features or use of this software 16240616Sjimharris * must display the following acknowledgement: 17240616Sjimharris * This product includes software developed by Marc Horowitz. 18240616Sjimharris * 4. The name of the author may not be used to endorse or promote products 19240616Sjimharris * derived from this software without specific prior written permission. 20240616Sjimharris * 21240616Sjimharris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22240616Sjimharris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23240616Sjimharris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24240616Sjimharris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25240616Sjimharris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26240616Sjimharris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27240616Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28240616Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29240616Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30240616Sjimharris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31240616Sjimharris */ 32240616Sjimharris 33240616Sjimharris#include <sys/param.h> 34240616Sjimharris#include <sys/systm.h> 35240616Sjimharris#include <sys/malloc.h> 36240616Sjimharris#include <sys/module.h> 37240616Sjimharris#include <sys/kernel.h> 38240616Sjimharris#include <sys/queue.h> 39240616Sjimharris#include <sys/sysctl.h> 40240616Sjimharris#include <sys/types.h> 41240616Sjimharris 42240616Sjimharris#include <sys/bus.h> 43240616Sjimharris#include <machine/bus.h> 44240616Sjimharris#include <sys/rman.h> 45240616Sjimharris#include <machine/resource.h> 46240616Sjimharris 47240616Sjimharris#include <net/ethernet.h> 48240616Sjimharris 49240616Sjimharris#include <dev/pccard/pccardreg.h> 50240616Sjimharris#include <dev/pccard/pccardvar.h> 51240616Sjimharris 52240616Sjimharris#include "power_if.h" 53240616Sjimharris#include "card_if.h" 54240616Sjimharris 55240616Sjimharris#define PCCARDDEBUG 56256152Sjimharris 57240616Sjimharris/* sysctl vars */ 58240616SjimharrisSYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters"); 59240616Sjimharris 60240616Sjimharrisint pccard_debug = 0; 61240616SjimharrisTUNABLE_INT("hw.pccard.debug", &pccard_debug); 62240616SjimharrisSYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW, 63240616Sjimharris &pccard_debug, 0, 64240616Sjimharris "pccard debug"); 65240616Sjimharris 66240616Sjimharrisint pccard_cis_debug = 0; 67240616SjimharrisTUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug); 68240616SjimharrisSYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW, 69256152Sjimharris &pccard_cis_debug, 0, "pccard CIS debug"); 70240616Sjimharris 71240616Sjimharris#ifdef PCCARDDEBUG 72240616Sjimharris#define DPRINTF(arg) if (pccard_debug) printf arg 73240616Sjimharris#define DEVPRINTF(arg) if (pccard_debug) device_printf arg 74240616Sjimharris#define PRVERBOSE(arg) printf arg 75240616Sjimharris#define DEVPRVERBOSE(arg) device_printf arg 76240616Sjimharris#else 77240616Sjimharris#define DPRINTF(arg) 78240616Sjimharris#define DEVPRINTF(arg) 79240616Sjimharris#define PRVERBOSE(arg) if (bootverbose) printf arg 80240616Sjimharris#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg 81240616Sjimharris#endif 82240616Sjimharris 83240616Sjimharrisstatic int pccard_ccr_read(struct pccard_function *pf, int ccr); 84240616Sjimharrisstatic void pccard_ccr_write(struct pccard_function *pf, int ccr, int val); 85240616Sjimharrisstatic int pccard_attach_card(device_t dev); 86240616Sjimharrisstatic int pccard_detach_card(device_t dev); 87240616Sjimharrisstatic int pccard_card_gettype(device_t dev, int *type); 88240616Sjimharrisstatic void pccard_function_init(struct pccard_function *pf); 89240616Sjimharrisstatic void pccard_function_free(struct pccard_function *pf); 90240616Sjimharrisstatic int pccard_function_enable(struct pccard_function *pf); 91240616Sjimharrisstatic void pccard_function_disable(struct pccard_function *pf); 92240616Sjimharrisstatic int pccard_compat_do_probe(device_t bus, device_t dev); 93256152Sjimharrisstatic int pccard_compat_do_attach(device_t bus, device_t dev); 94256152Sjimharrisstatic int pccard_add_children(device_t dev, int busno); 95240616Sjimharrisstatic int pccard_probe(device_t dev); 96240616Sjimharrisstatic int pccard_attach(device_t dev); 97240616Sjimharrisstatic int pccard_detach(device_t dev); 98240616Sjimharrisstatic void pccard_print_resources(struct resource_list *rl, 99248770Sjimharris const char *name, int type, int count, const char *format); 100240616Sjimharrisstatic int pccard_print_child(device_t dev, device_t child); 101240616Sjimharrisstatic int pccard_set_resource(device_t dev, device_t child, int type, 102240616Sjimharris int rid, u_long start, u_long count); 103240616Sjimharrisstatic int pccard_get_resource(device_t dev, device_t child, int type, 104240616Sjimharris int rid, u_long *startp, u_long *countp); 105240616Sjimharrisstatic void pccard_delete_resource(device_t dev, device_t child, int type, 106240616Sjimharris int rid); 107240616Sjimharrisstatic int pccard_set_res_flags(device_t dev, device_t child, int type, 108240616Sjimharris int rid, u_int32_t flags); 109240616Sjimharrisstatic int pccard_set_memory_offset(device_t dev, device_t child, int rid, 110240616Sjimharris u_int32_t offset, u_int32_t *deltap); 111240616Sjimharrisstatic void pccard_probe_nomatch(device_t cbdev, device_t child); 112240616Sjimharrisstatic int pccard_read_ivar(device_t bus, device_t child, int which, 113240616Sjimharris u_char *result); 114240616Sjimharrisstatic void pccard_driver_added(device_t dev, driver_t *driver); 115240616Sjimharrisstatic struct resource *pccard_alloc_resource(device_t dev, 116240616Sjimharris device_t child, int type, int *rid, u_long start, 117240616Sjimharris u_long end, u_long count, u_int flags); 118240616Sjimharrisstatic int pccard_release_resource(device_t dev, device_t child, int type, 119240616Sjimharris int rid, struct resource *r); 120240616Sjimharrisstatic void pccard_child_detached(device_t parent, device_t dev); 121240616Sjimharrisstatic void pccard_intr(void *arg); 122240616Sjimharrisstatic int pccard_setup_intr(device_t dev, device_t child, 123240616Sjimharris struct resource *irq, int flags, driver_intr_t *intr, 124240616Sjimharris void *arg, void **cookiep); 125240616Sjimharrisstatic int pccard_teardown_intr(device_t dev, device_t child, 126240616Sjimharris struct resource *r, void *cookie); 127240616Sjimharris 128240616Sjimharrisstatic const struct pccard_product * 129240616Sjimharrispccard_do_product_lookup(device_t bus, device_t dev, 130240616Sjimharris const struct pccard_product *tab, size_t ent_size, 131240616Sjimharris pccard_product_match_fn matchfn); 132240616Sjimharris 133240616Sjimharris 134240616Sjimharrisstatic int 135240616Sjimharrispccard_ccr_read(struct pccard_function *pf, int ccr) 136240616Sjimharris{ 137240616Sjimharris return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, 138240616Sjimharris pf->pf_ccr_offset + ccr)); 139240616Sjimharris} 140240616Sjimharris 141240616Sjimharrisstatic void 142240616Sjimharrispccard_ccr_write(struct pccard_function *pf, int ccr, int val) 143240616Sjimharris{ 144240616Sjimharris if ((pf->ccr_mask) & (1 << (ccr / 2))) { 145240616Sjimharris bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, 146240616Sjimharris pf->pf_ccr_offset + ccr, val); 147240616Sjimharris } 148240616Sjimharris} 149240616Sjimharris 150240616Sjimharrisstatic int 151240616Sjimharrispccard_set_default_descr(device_t dev) 152240616Sjimharris{ 153240616Sjimharris char *vendorstr, *prodstr, *str; 154240616Sjimharris 155240616Sjimharris if (pccard_get_vendor_str(dev, &vendorstr)) 156240616Sjimharris return (0); 157240616Sjimharris if (pccard_get_product_str(dev, &prodstr)) 158240616Sjimharris return (0); 159240616Sjimharris str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF, 160240616Sjimharris M_WAITOK); 161240616Sjimharris sprintf(str, "%s %s", vendorstr, prodstr); 162240616Sjimharris device_set_desc_copy(dev, str); 163240616Sjimharris free(str, M_DEVBUF); 164240616Sjimharris return (0); 165240616Sjimharris} 166240616Sjimharris 167240616Sjimharrisstatic int 168240616Sjimharrispccard_attach_card(device_t dev) 169240616Sjimharris{ 170240616Sjimharris struct pccard_softc *sc = PCCARD_SOFTC(dev); 171240616Sjimharris struct pccard_function *pf; 172240616Sjimharris struct pccard_ivar *ivar; 173240616Sjimharris device_t child; 174240616Sjimharris int i; 175248756Sjimharris 176240616Sjimharris /* 177240616Sjimharris * this is here so that when socket_enable calls gettype, trt happens 178240616Sjimharris */ 179240616Sjimharris STAILQ_INIT(&sc->card.pf_head); 180240616Sjimharris 181240616Sjimharris DEVPRINTF((dev, "chip_socket_enable\n")); 182248756Sjimharris POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 183240616Sjimharris 184240616Sjimharris DEVPRINTF((dev, "read_cis\n")); 185240616Sjimharris pccard_read_cis(sc); 186240616Sjimharris 187240616Sjimharris DEVPRINTF((dev, "check_cis_quirks\n")); 188240616Sjimharris pccard_check_cis_quirks(dev); 189240616Sjimharris 190240616Sjimharris /* 191240616Sjimharris * bail now if the card has no functions, or if there was an error in 192240616Sjimharris * the cis. 193240616Sjimharris */ 194240616Sjimharris 195240616Sjimharris if (sc->card.error) { 196240616Sjimharris device_printf(dev, "CARD ERROR!\n"); 197240616Sjimharris return (1); 198240616Sjimharris } 199240616Sjimharris if (STAILQ_EMPTY(&sc->card.pf_head)) { 200240616Sjimharris device_printf(dev, "Card has no functions!\n"); 201240616Sjimharris return (1); 202240616Sjimharris } 203240616Sjimharris 204240616Sjimharris if (bootverbose || pccard_debug) 205240616Sjimharris pccard_print_cis(dev); 206240616Sjimharris 207240616Sjimharris DEVPRINTF((dev, "functions scanning\n")); 208240616Sjimharris i = -1; 209240616Sjimharris STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 210240616Sjimharris i++; 211240616Sjimharris if (STAILQ_EMPTY(&pf->cfe_head)) { 212240616Sjimharris device_printf(dev, 213240616Sjimharris "Function %d has no config entries.!\n", i); 214240616Sjimharris continue; 215240616Sjimharris } 216240616Sjimharris pf->sc = sc; 217240616Sjimharris pf->cfe = NULL; 218240616Sjimharris pf->dev = NULL; 219240616Sjimharris } 220248770Sjimharris DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1, 221240616Sjimharris pccard_mfc(sc))); 222240616Sjimharris 223240616Sjimharris STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 224248770Sjimharris if (STAILQ_EMPTY(&pf->cfe_head)) 225240616Sjimharris continue; 226240616Sjimharris /* 227240616Sjimharris * In NetBSD, the drivers are responsible for activating 228240616Sjimharris * each function of a card. I think that in FreeBSD we 229240616Sjimharris * want to activate them enough for the usual bus_*_resource 230240616Sjimharris * routines will do the right thing. This many mean a 231240616Sjimharris * departure from the current NetBSD model. 232240616Sjimharris * 233240616Sjimharris * This seems to work well in practice for most cards. 234240616Sjimharris * However, there are two cases that are problematic. 235240616Sjimharris * If a driver wishes to pick and chose which config 236240616Sjimharris * entry to use, then this method falls down. These 237240616Sjimharris * are usually older cards. In addition, there are 238240616Sjimharris * some cards that have multiple hardware units on the 239240616Sjimharris * cards, but presents only one CIS chain. These cards 240240616Sjimharris * are combination cards, but only one of these units 241240616Sjimharris * can be on at a time. 242240616Sjimharris */ 243240616Sjimharris ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF, 244240616Sjimharris M_WAITOK | M_ZERO); 245240616Sjimharris child = device_add_child(dev, NULL, -1); 246240616Sjimharris device_set_ivars(child, ivar); 247240616Sjimharris ivar->fcn = pf; 248240616Sjimharris pf->dev = child; 249240616Sjimharris /* 250240616Sjimharris * XXX We might want to move the next two lines into 251240616Sjimharris * XXX the pccard interface layer. For the moment, this 252240616Sjimharris * XXX is OK, but some drivers want to pick the config 253240616Sjimharris * XXX entry to use as well as some address tweaks (mostly 254240616Sjimharris * XXX due to bugs in decode logic that makes some 255240616Sjimharris * XXX addresses illegal or broken). 256240616Sjimharris */ 257240616Sjimharris pccard_function_init(pf); 258240616Sjimharris if (sc->sc_enabled_count == 0) 259240616Sjimharris POWER_ENABLE_SOCKET(device_get_parent(dev), dev); 260240616Sjimharris if (pccard_function_enable(pf) == 0 && 261240616Sjimharris pccard_set_default_descr(child) == 0 && 262240616Sjimharris device_probe_and_attach(child) == 0) { 263240616Sjimharris DEVPRINTF((sc->dev, "function %d CCR at %d " 264240616Sjimharris "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 265240616Sjimharris pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 266240616Sjimharris pccard_ccr_read(pf, 0x00), 267240616Sjimharris pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 268240616Sjimharris pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 269240616Sjimharris pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 270240616Sjimharris pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 271240616Sjimharris } else { 272248770Sjimharris if (pf->cfe != NULL) 273240616Sjimharris pccard_function_disable(pf); 274240616Sjimharris } 275240616Sjimharris } 276240616Sjimharris return (0); 277240616Sjimharris} 278240616Sjimharris 279240616Sjimharrisstatic int 280240616Sjimharrispccard_detach_card(device_t dev) 281240616Sjimharris{ 282240616Sjimharris struct pccard_softc *sc = PCCARD_SOFTC(dev); 283240616Sjimharris struct pccard_function *pf; 284240616Sjimharris struct pccard_config_entry *cfe; 285240616Sjimharris 286240616Sjimharris /* 287240616Sjimharris * We are running on either the PCCARD socket's event thread 288240616Sjimharris * or in user context detaching a device by user request. 289240616Sjimharris */ 290245136Sjimharris STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 291240616Sjimharris int state = device_get_state(pf->dev); 292240616Sjimharris 293245136Sjimharris if (state == DS_ATTACHED || state == DS_BUSY) 294240616Sjimharris device_detach(pf->dev); 295240616Sjimharris if (pf->cfe != NULL) 296240616Sjimharris pccard_function_disable(pf); 297240616Sjimharris pccard_function_free(pf); 298240616Sjimharris device_delete_child(dev, pf->dev); 299240616Sjimharris } 300240616Sjimharris if (sc->sc_enabled_count == 0) 301240616Sjimharris POWER_DISABLE_SOCKET(device_get_parent(dev), dev); 302240616Sjimharris 303240616Sjimharris while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) { 304240616Sjimharris while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) { 305240616Sjimharris STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list); 306 free(cfe, M_DEVBUF); 307 } 308 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list); 309 free(pf, M_DEVBUF); 310 } 311 return (0); 312} 313 314static const struct pccard_product * 315pccard_do_product_lookup(device_t bus, device_t dev, 316 const struct pccard_product *tab, size_t ent_size, 317 pccard_product_match_fn matchfn) 318{ 319 const struct pccard_product *ent; 320 int matches; 321 u_int32_t fcn; 322 u_int32_t vendor; 323 u_int32_t prod; 324 char *vendorstr; 325 char *prodstr; 326 327#ifdef DIAGNOSTIC 328 if (sizeof *ent > ent_size) 329 panic("pccard_product_lookup: bogus ent_size %jd", 330 (intmax_t) ent_size); 331#endif 332 if (pccard_get_vendor(dev, &vendor)) 333 return (NULL); 334 if (pccard_get_product(dev, &prod)) 335 return (NULL); 336 if (pccard_get_function_number(dev, &fcn)) 337 return (NULL); 338 if (pccard_get_vendor_str(dev, &vendorstr)) 339 return (NULL); 340 if (pccard_get_product_str(dev, &prodstr)) 341 return (NULL); 342 for (ent = tab; ent->pp_name != NULL && ent->pp_vendor != 0; ent = 343 (const struct pccard_product *) ((const char *) ent + ent_size)) { 344 matches = 1; 345 if (ent->pp_vendor == PCCARD_VENDOR_ANY && 346 ent->pp_product == PCCARD_PRODUCT_ANY && 347 ent->pp_cis[0] == NULL && 348 ent->pp_cis[1] == NULL) { 349 if (ent->pp_name) 350 device_printf(dev, 351 "Total wildcard entry ignored for %s\n", 352 ent->pp_name); 353 continue; 354 } 355 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY && 356 vendor != ent->pp_vendor) 357 matches = 0; 358 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY && 359 prod != ent->pp_product) 360 matches = 0; 361 if (matches && fcn != ent->pp_expfunc) 362 matches = 0; 363 if (matches && ent->pp_cis[0] && 364 strcmp(ent->pp_cis[0], vendorstr) != 0) 365 matches = 0; 366 if (matches && ent->pp_cis[1] && 367 strcmp(ent->pp_cis[1], prodstr) != 0) 368 matches = 0; 369 /* XXX need to match cis[2] and cis[3] also XXX */ 370 if (matchfn != NULL) 371 matches = (*matchfn)(dev, ent, matches); 372 if (matches) 373 return (ent); 374 } 375 return (NULL); 376} 377 378static int 379pccard_card_gettype(device_t dev, int *type) 380{ 381 struct pccard_softc *sc = PCCARD_SOFTC(dev); 382 struct pccard_function *pf; 383 384 /* 385 * set the iftype to memory if this card has no functions (not yet 386 * probed), or only one function, and that is not initialized yet or 387 * that is memory. 388 */ 389 pf = STAILQ_FIRST(&sc->card.pf_head); 390 if (pf == NULL || 391 (STAILQ_NEXT(pf, pf_list) == NULL && 392 (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY))) 393 *type = PCCARD_IFTYPE_MEMORY; 394 else 395 *type = PCCARD_IFTYPE_IO; 396 return (0); 397} 398 399/* 400 * Initialize a PCCARD function. May be called as long as the function is 401 * disabled. 402 * 403 * Note: pccard_function_init should not keep resources allocated. It should 404 * only set them up ala isa pnp, set the values in the rl lists, and return. 405 * Any resource held after pccard_function_init is called is a bug. However, 406 * the bus routines to get the resources also assume that pccard_function_init 407 * does this, so they need to be fixed too. 408 */ 409static void 410pccard_function_init(struct pccard_function *pf) 411{ 412 struct pccard_config_entry *cfe; 413 int i; 414 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 415 struct resource_list *rl = &devi->resources; 416 struct resource_list_entry *rle; 417 struct resource *r = 0; 418 device_t bus; 419 int start; 420 int end; 421 int spaces; 422 423 if (pf->pf_flags & PFF_ENABLED) { 424 printf("pccard_function_init: function is enabled"); 425 return; 426 } 427 bus = device_get_parent(pf->dev); 428 /* Remember which configuration entry we are using. */ 429 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) { 430 for (i = 0; i < cfe->num_iospace; i++) 431 cfe->iores[i] = NULL; 432 cfe->irqres = NULL; 433 spaces = 0; 434 for (i = 0; i < cfe->num_iospace; i++) { 435 start = cfe->iospace[i].start; 436 if (start) 437 end = start + cfe->iospace[i].length - 1; 438 else 439 end = ~0; 440 cfe->iorid[i] = i; 441 DEVPRINTF((bus, "I/O rid %d start %x end %x\n", 442 i, start, end)); 443 r = cfe->iores[i] = bus_alloc_resource(bus, 444 SYS_RES_IOPORT, &cfe->iorid[i], start, end, 445 cfe->iospace[i].length, 446 rman_make_alignment_flags(cfe->iospace[i].length)); 447 if (cfe->iores[i] == NULL) 448 goto not_this_one; 449 resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i], 450 rman_get_start(r), rman_get_end(r), 451 cfe->iospace[i].length); 452 rle = resource_list_find(rl, SYS_RES_IOPORT, 453 cfe->iorid[i]); 454 rle->res = r; 455 spaces++; 456 } 457 if (cfe->num_memspace > 0) { 458 /* 459 * Not implement yet, Fix me. 460 */ 461 DEVPRINTF((bus, "Memory space not yet implemented.\n")); 462 } 463 if (spaces == 0) { 464 DEVPRINTF((bus, "Neither memory nor I/O mampped\n")); 465 goto not_this_one; 466 } 467 if (cfe->irqmask) { 468 cfe->irqrid = 0; 469 r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ, 470 &cfe->irqrid, 0, ~0, 1, 0); 471 if (cfe->irqres == NULL) 472 goto not_this_one; 473 resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid, 474 rman_get_start(r), rman_get_end(r), 1); 475 rle = resource_list_find(rl, SYS_RES_IRQ, 476 cfe->irqrid); 477 rle->res = r; 478 } 479 /* If we get to here, we've allocated all we need */ 480 pf->cfe = cfe; 481 break; 482 not_this_one:; 483 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n", 484 cfe->number)); 485 /* 486 * Release resources that we partially allocated 487 * from this config entry. 488 */ 489 for (i = 0; i < cfe->num_iospace; i++) { 490 if (cfe->iores[i] != NULL) { 491 bus_release_resource(bus, SYS_RES_IOPORT, 492 cfe->iorid[i], cfe->iores[i]); 493 rle = resource_list_find(rl, SYS_RES_IOPORT, 494 cfe->iorid[i]); 495 rle->res = NULL; 496 resource_list_delete(rl, SYS_RES_IOPORT, 497 cfe->iorid[i]); 498 } 499 cfe->iores[i] = NULL; 500 } 501 if (cfe->irqmask && cfe->irqres != NULL) { 502 bus_release_resource(bus, SYS_RES_IRQ, 503 cfe->irqrid, cfe->irqres); 504 rle = resource_list_find(rl, SYS_RES_IRQ, 505 cfe->irqrid); 506 rle->res = NULL; 507 resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid); 508 cfe->irqres = NULL; 509 } 510 } 511} 512 513/* 514 * Free resources allocated by pccard_function_init(), May be called as long 515 * as the function is disabled. 516 * 517 * NOTE: This function should be unnecessary. pccard_function_init should 518 * never keep resources initialized. 519 */ 520static void 521pccard_function_free(struct pccard_function *pf) 522{ 523 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 524 struct resource_list_entry *rle; 525 526 if (pf->pf_flags & PFF_ENABLED) { 527 printf("pccard_function_init: function is enabled"); 528 return; 529 } 530 531 SLIST_FOREACH(rle, &devi->resources, link) { 532 if (rle->res) { 533 if (rman_get_device(rle->res) != pf->sc->dev) 534 device_printf(pf->sc->dev, 535 "function_free: Resource still owned by " 536 "child, oops. " 537 "(type=%d, rid=%d, addr=%lx)\n", 538 rle->type, rle->rid, 539 rman_get_start(rle->res)); 540 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev), 541 pf->sc->dev, rle->type, rle->rid, rle->res); 542 rle->res = NULL; 543 } 544 } 545 resource_list_free(&devi->resources); 546} 547 548/* Enable a PCCARD function */ 549static int 550pccard_function_enable(struct pccard_function *pf) 551{ 552 struct pccard_function *tmp; 553 int reg; 554 device_t dev = pf->sc->dev; 555 556 if (pf->cfe == NULL) { 557 DEVPRVERBOSE((dev, "No config entry could be allocated.\n")); 558 return (ENOMEM); 559 } 560 561 /* 562 * Increase the reference count on the socket, enabling power, if 563 * necessary. 564 */ 565 pf->sc->sc_enabled_count++; 566 567 if (pf->pf_flags & PFF_ENABLED) { 568 /* 569 * Don't do anything if we're already enabled. 570 */ 571 return (0); 572 } 573 574 /* 575 * it's possible for different functions' CCRs to be in the same 576 * underlying page. Check for that. 577 */ 578 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 579 if ((tmp->pf_flags & PFF_ENABLED) && 580 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 581 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 582 (tmp->ccr_base - tmp->pf_ccr_offset + 583 tmp->pf_ccr_realsize))) { 584 pf->pf_ccrt = tmp->pf_ccrt; 585 pf->pf_ccrh = tmp->pf_ccrh; 586 pf->pf_ccr_realsize = tmp->pf_ccr_realsize; 587 588 /* 589 * pf->pf_ccr_offset = (tmp->pf_ccr_offset - 590 * tmp->ccr_base) + pf->ccr_base; 591 */ 592 /* pf->pf_ccr_offset = 593 (tmp->pf_ccr_offset + pf->ccr_base) - 594 tmp->ccr_base; */ 595 pf->pf_ccr_window = tmp->pf_ccr_window; 596 break; 597 } 598 } 599 if (tmp == NULL) { 600 pf->ccr_rid = 0; 601 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 602 &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE); 603 if (!pf->ccr_res) 604 goto bad; 605 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n", 606 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res), 607 pf->ccr_base)); 608 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, 609 pf->ccr_rid, PCCARD_A_MEM_ATTR); 610 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev, 611 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset); 612 pf->pf_ccrt = rman_get_bustag(pf->ccr_res); 613 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res); 614 pf->pf_ccr_realsize = 1; 615 } 616 617 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX); 618 reg |= PCCARD_CCR_OPTION_LEVIREQ; 619 if (pccard_mfc(pf->sc)) { 620 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE | 621 PCCARD_CCR_OPTION_ADDR_DECODE); 622 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */ 623 } 624 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg); 625 626 reg = 0; 627 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0) 628 reg |= PCCARD_CCR_STATUS_IOIS8; 629 if (pf->cfe->flags & PCCARD_CFE_AUDIO) 630 reg |= PCCARD_CCR_STATUS_AUDIO; 631 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg); 632 633 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0); 634 635 if (pccard_mfc(pf->sc)) { 636 long tmp, iosize; 637 638 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase; 639 /* round up to nearest (2^n)-1 */ 640 for (iosize = 1; iosize < tmp; iosize <<= 1) 641 ; 642 iosize--; 643 644 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0, 645 pf->pf_mfc_iobase & 0xff); 646 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1, 647 (pf->pf_mfc_iobase >> 8) & 0xff); 648 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0); 649 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0); 650 651 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize); 652 } 653 654#ifdef PCCARDDEBUG 655 if (pccard_debug) { 656 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 657 device_printf(tmp->sc->dev, 658 "function %d CCR at %d offset %x: " 659 "%x %x %x %x, %x %x %x %x, %x\n", 660 tmp->number, tmp->pf_ccr_window, 661 tmp->pf_ccr_offset, 662 pccard_ccr_read(tmp, 0x00), 663 pccard_ccr_read(tmp, 0x02), 664 pccard_ccr_read(tmp, 0x04), 665 pccard_ccr_read(tmp, 0x06), 666 pccard_ccr_read(tmp, 0x0A), 667 pccard_ccr_read(tmp, 0x0C), 668 pccard_ccr_read(tmp, 0x0E), 669 pccard_ccr_read(tmp, 0x10), 670 pccard_ccr_read(tmp, 0x12)); 671 } 672 } 673#endif 674 pf->pf_flags |= PFF_ENABLED; 675 return (0); 676 677 bad: 678 /* 679 * Decrement the reference count, and power down the socket, if 680 * necessary. 681 */ 682 pf->sc->sc_enabled_count--; 683 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count)); 684 685 return (1); 686} 687 688/* Disable PCCARD function. */ 689static void 690pccard_function_disable(struct pccard_function *pf) 691{ 692 struct pccard_function *tmp; 693 device_t dev = pf->sc->dev; 694 695 if (pf->cfe == NULL) 696 panic("pccard_function_disable: function not initialized"); 697 698 if ((pf->pf_flags & PFF_ENABLED) == 0) { 699 /* 700 * Don't do anything if we're already disabled. 701 */ 702 return; 703 } 704 705 if (pf->intr_handler != NULL) { 706 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev); 707 struct resource_list_entry *rle = 708 resource_list_find(&devi->resources, SYS_RES_IRQ, 0); 709 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res, 710 pf->intr_handler_cookie); 711 } 712 713 /* 714 * it's possible for different functions' CCRs to be in the same 715 * underlying page. Check for that. Note we mark us as disabled 716 * first to avoid matching ourself. 717 */ 718 719 pf->pf_flags &= ~PFF_ENABLED; 720 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) { 721 if ((tmp->pf_flags & PFF_ENABLED) && 722 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) && 723 ((pf->ccr_base + PCCARD_CCR_SIZE) <= 724 (tmp->ccr_base - tmp->pf_ccr_offset + 725 tmp->pf_ccr_realsize))) 726 break; 727 } 728 729 /* Not used by anyone else; unmap the CCR. */ 730 if (tmp == NULL) { 731 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid, 732 pf->ccr_res); 733 pf->ccr_res = NULL; 734 } 735 736 /* 737 * Decrement the reference count, and power down the socket, if 738 * necessary. 739 */ 740 pf->sc->sc_enabled_count--; 741} 742 743/* 744 * simulate the old "probe" routine. In the new world order, the driver 745 * needs to grab devices while in the old they were assigned to the device by 746 * the pccardd process. These symbols are exported to the upper layers. 747 */ 748static int 749pccard_compat_do_probe(device_t bus, device_t dev) 750{ 751 return (CARD_COMPAT_MATCH(dev)); 752} 753 754static int 755pccard_compat_do_attach(device_t bus, device_t dev) 756{ 757 int err; 758 759 err = CARD_COMPAT_PROBE(dev); 760 if (err == 0) 761 err = CARD_COMPAT_ATTACH(dev); 762 return (err); 763} 764 765#define PCCARD_NPORT 2 766#define PCCARD_NMEM 5 767#define PCCARD_NIRQ 1 768#define PCCARD_NDRQ 0 769 770static int 771pccard_add_children(device_t dev, int busno) 772{ 773 /* Call parent to scan for any current children */ 774 return (0); 775} 776 777static int 778pccard_probe(device_t dev) 779{ 780 device_set_desc(dev, "16-bit PCCard bus"); 781 return (pccard_add_children(dev, device_get_unit(dev))); 782} 783 784static int 785pccard_attach(device_t dev) 786{ 787 struct pccard_softc *sc = PCCARD_SOFTC(dev); 788 789 sc->dev = dev; 790 sc->sc_enabled_count = 0; 791 return (bus_generic_attach(dev)); 792} 793 794static int 795pccard_detach(device_t dev) 796{ 797 pccard_detach_card(dev); 798 return 0; 799} 800 801static int 802pccard_suspend(device_t self) 803{ 804 pccard_detach_card(self); 805 return (0); 806} 807 808static 809int 810pccard_resume(device_t self) 811{ 812 return (0); 813} 814 815static void 816pccard_print_resources(struct resource_list *rl, const char *name, int type, 817 int count, const char *format) 818{ 819 struct resource_list_entry *rle; 820 int printed; 821 int i; 822 823 printed = 0; 824 for (i = 0; i < count; i++) { 825 rle = resource_list_find(rl, type, i); 826 if (rle != NULL) { 827 if (printed == 0) 828 printf(" %s ", name); 829 else if (printed > 0) 830 printf(","); 831 printed++; 832 printf(format, rle->start); 833 if (rle->count > 1) { 834 printf("-"); 835 printf(format, rle->start + rle->count - 1); 836 } 837 } else if (i > 3) { 838 /* check the first few regardless */ 839 break; 840 } 841 } 842} 843 844static int 845pccard_print_child(device_t dev, device_t child) 846{ 847 struct pccard_ivar *devi = PCCARD_IVAR(child); 848 struct resource_list *rl = &devi->resources; 849 int retval = 0; 850 851 retval += bus_print_child_header(dev, child); 852 retval += printf(" at"); 853 854 if (devi != NULL) { 855 pccard_print_resources(rl, "port", SYS_RES_IOPORT, 856 PCCARD_NPORT, "%#lx"); 857 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY, 858 PCCARD_NMEM, "%#lx"); 859 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ, 860 "%ld"); 861 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ, 862 "%ld"); 863 retval += printf(" function %d config %d", devi->fcn->number, 864 devi->fcn->cfe->number); 865 } 866 867 retval += bus_print_child_footer(dev, child); 868 869 return (retval); 870} 871 872static int 873pccard_set_resource(device_t dev, device_t child, int type, int rid, 874 u_long start, u_long count) 875{ 876 struct pccard_ivar *devi = PCCARD_IVAR(child); 877 struct resource_list *rl = &devi->resources; 878 879 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY 880 && type != SYS_RES_IRQ && type != SYS_RES_DRQ) 881 return (EINVAL); 882 if (rid < 0) 883 return (EINVAL); 884 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT) 885 return (EINVAL); 886 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM) 887 return (EINVAL); 888 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ) 889 return (EINVAL); 890 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ) 891 return (EINVAL); 892 893 resource_list_add(rl, type, rid, start, start + count - 1, count); 894 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev, 895 type, &rid, start, start + count - 1, count, 0)) 896 return 0; 897 else 898 return ENOMEM; 899} 900 901static int 902pccard_get_resource(device_t dev, device_t child, int type, int rid, 903 u_long *startp, u_long *countp) 904{ 905 struct pccard_ivar *devi = PCCARD_IVAR(child); 906 struct resource_list *rl = &devi->resources; 907 struct resource_list_entry *rle; 908 909 rle = resource_list_find(rl, type, rid); 910 if (rle == NULL) 911 return (ENOENT); 912 913 if (startp != NULL) 914 *startp = rle->start; 915 if (countp != NULL) 916 *countp = rle->count; 917 918 return (0); 919} 920 921static void 922pccard_delete_resource(device_t dev, device_t child, int type, int rid) 923{ 924 struct pccard_ivar *devi = PCCARD_IVAR(child); 925 struct resource_list *rl = &devi->resources; 926 resource_list_delete(rl, type, rid); 927} 928 929static int 930pccard_set_res_flags(device_t dev, device_t child, int type, int rid, 931 u_int32_t flags) 932{ 933 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type, 934 rid, flags)); 935} 936 937static int 938pccard_set_memory_offset(device_t dev, device_t child, int rid, 939 u_int32_t offset, u_int32_t *deltap) 940 941{ 942 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid, 943 offset, deltap)); 944} 945 946static void 947pccard_probe_nomatch(device_t bus, device_t child) 948{ 949 struct pccard_ivar *devi = PCCARD_IVAR(child); 950 struct pccard_function *func = devi->fcn; 951 struct pccard_softc *sc = PCCARD_SOFTC(bus); 952 953 device_printf(bus, "<unknown card>"); 954 printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n", 955 sc->card.manufacturer, sc->card.product, func->number); 956 device_printf(bus, " CIS info: %s, %s, %s\n", sc->card.cis1_info[0], 957 sc->card.cis1_info[1], sc->card.cis1_info[2]); 958 return; 959} 960 961static int 962pccard_child_location_str(device_t bus, device_t child, char *buf, 963 size_t buflen) 964{ 965 struct pccard_ivar *devi = PCCARD_IVAR(child); 966 struct pccard_function *func = devi->fcn; 967 968 snprintf(buf, buflen, "function=%d", func->number); 969 return (0); 970} 971 972static int 973pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf, 974 size_t buflen) 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 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x " 981 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d", 982 sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0], 983 sc->card.cis1_info[1], func->function); 984 return (0); 985} 986 987static int 988pccard_read_ivar(device_t bus, device_t child, int which, u_char *result) 989{ 990 struct pccard_ivar *devi = PCCARD_IVAR(child); 991 struct pccard_function *func = devi->fcn; 992 struct pccard_softc *sc = PCCARD_SOFTC(bus); 993 994 switch (which) { 995 default: 996 case PCCARD_IVAR_ETHADDR: 997 bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN); 998 break; 999 case PCCARD_IVAR_VENDOR: 1000 *(u_int32_t *) result = sc->card.manufacturer; 1001 break; 1002 case PCCARD_IVAR_PRODUCT: 1003 *(u_int32_t *) result = sc->card.product; 1004 break; 1005 case PCCARD_IVAR_PRODEXT: 1006 *(u_int16_t *) result = sc->card.prodext; 1007 break; 1008 case PCCARD_IVAR_FUNCTION: 1009 *(u_int32_t *) result = func->function; 1010 break; 1011 case PCCARD_IVAR_FUNCTION_NUMBER: 1012 if (!func) { 1013 device_printf(bus, "No function number, bug!\n"); 1014 return (ENOENT); 1015 } 1016 *(u_int32_t *) result = func->number; 1017 break; 1018 case PCCARD_IVAR_VENDOR_STR: 1019 *(char **) result = sc->card.cis1_info[0]; 1020 break; 1021 case PCCARD_IVAR_PRODUCT_STR: 1022 *(char **) result = sc->card.cis1_info[1]; 1023 break; 1024 case PCCARD_IVAR_CIS3_STR: 1025 *(char **) result = sc->card.cis1_info[2]; 1026 break; 1027 case PCCARD_IVAR_CIS4_STR: 1028 *(char **) result = sc->card.cis1_info[3]; 1029 break; 1030 } 1031 return (0); 1032} 1033 1034static void 1035pccard_driver_added(device_t dev, driver_t *driver) 1036{ 1037 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1038 struct pccard_function *pf; 1039 device_t child; 1040 1041 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) { 1042 if (STAILQ_EMPTY(&pf->cfe_head)) 1043 continue; 1044 child = pf->dev; 1045 if (device_get_state(child) != DS_NOTPRESENT) 1046 continue; 1047 if (pccard_function_enable(pf) == 0 && 1048 device_probe_and_attach(child) == 0) { 1049 DEVPRINTF((sc->dev, "function %d CCR at %d " 1050 "offset %x: %x %x %x %x, %x %x %x %x, %x\n", 1051 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset, 1052 pccard_ccr_read(pf, 0x00), 1053 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04), 1054 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A), 1055 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E), 1056 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12))); 1057 } else { 1058 if (pf->cfe != NULL) 1059 pccard_function_disable(pf); 1060 } 1061 } 1062 return; 1063} 1064 1065static struct resource * 1066pccard_alloc_resource(device_t dev, device_t child, int type, int *rid, 1067 u_long start, u_long end, u_long count, u_int flags) 1068{ 1069 struct pccard_ivar *dinfo; 1070 struct resource_list_entry *rle = 0; 1071 int passthrough = (device_get_parent(child) != dev); 1072 struct resource *r = NULL; 1073 1074 if (passthrough) { 1075 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 1076 type, rid, start, end, count, flags)); 1077 } 1078 1079 dinfo = device_get_ivars(child); 1080 rle = resource_list_find(&dinfo->resources, type, *rid); 1081 1082 if (rle == NULL) 1083 return (NULL); /* no resource of that type/rid */ 1084 1085 if (rle->res == NULL) { 1086 switch(type) { 1087 case SYS_RES_IOPORT: 1088 r = bus_alloc_resource(dev, type, rid, start, end, 1089 count, rman_make_alignment_flags(count)); 1090 if (r == NULL) 1091 goto bad; 1092 resource_list_add(&dinfo->resources, type, *rid, 1093 rman_get_start(r), rman_get_end(r), count); 1094 rle = resource_list_find(&dinfo->resources, type, *rid); 1095 if (!rle) 1096 goto bad; 1097 rle->res = r; 1098 break; 1099 case SYS_RES_MEMORY: 1100 break; 1101 case SYS_RES_IRQ: 1102 break; 1103 } 1104 return (rle->res); 1105 } 1106 if (rman_get_device(rle->res) != dev) 1107 return (NULL); 1108 bus_release_resource(dev, type, *rid, rle->res); 1109 rle->res = NULL; 1110 switch(type) { 1111 case SYS_RES_IOPORT: 1112 case SYS_RES_MEMORY: 1113 if (!(flags & RF_ALIGNMENT_MASK)) 1114 flags |= rman_make_alignment_flags(rle->count); 1115 break; 1116 case SYS_RES_IRQ: 1117 flags |= RF_SHAREABLE; 1118 break; 1119 } 1120 rle->res = resource_list_alloc(&dinfo->resources, dev, child, 1121 type, rid, rle->start, rle->end, rle->count, flags); 1122 return (rle->res); 1123bad:; 1124 device_printf(dev, "WARNING: Resource not reserved by pccard\n"); 1125 return (NULL); 1126} 1127 1128static int 1129pccard_release_resource(device_t dev, device_t child, int type, int rid, 1130 struct resource *r) 1131{ 1132 struct pccard_ivar *dinfo; 1133 int passthrough = (device_get_parent(child) != dev); 1134 struct resource_list_entry *rle = 0; 1135 int ret; 1136 int flags; 1137 1138 if (passthrough) 1139 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1140 type, rid, r); 1141 1142 dinfo = device_get_ivars(child); 1143 1144 rle = resource_list_find(&dinfo->resources, type, rid); 1145 1146 if (!rle) { 1147 device_printf(dev, "Allocated resource not found, " 1148 "%d %x %lx %lx\n", 1149 type, rid, rman_get_start(r), rman_get_size(r)); 1150 return ENOENT; 1151 } 1152 if (!rle->res) { 1153 device_printf(dev, "Allocated resource not recorded\n"); 1154 return ENOENT; 1155 } 1156 1157 ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 1158 type, rid, r); 1159 switch(type) { 1160 case SYS_RES_IOPORT: 1161 case SYS_RES_MEMORY: 1162 flags = rman_make_alignment_flags(rle->count); 1163 break; 1164 case SYS_RES_IRQ: 1165 flags = RF_SHAREABLE; 1166 break; 1167 default: 1168 flags = 0; 1169 } 1170 rle->res = bus_alloc_resource(dev, type, &rid, 1171 rle->start, rle->end, rle->count, flags); 1172 if (rle->res == NULL) 1173 device_printf(dev, "release_resource: " 1174 "unable to reaquire resource\n"); 1175 return ret; 1176} 1177 1178static void 1179pccard_child_detached(device_t parent, device_t dev) 1180{ 1181 struct pccard_ivar *ivar = PCCARD_IVAR(dev); 1182 struct pccard_function *pf = ivar->fcn; 1183 1184 pccard_function_disable(pf); 1185} 1186 1187static void 1188pccard_intr(void *arg) 1189{ 1190 struct pccard_function *pf = (struct pccard_function*) arg; 1191 int reg; 1192 int doisr = 1; 1193 1194 /* 1195 * MFC cards know if they interrupted, so we have to ack the 1196 * interrupt and call the ISR. Non-MFC cards don't have these 1197 * bits, so they always get called. Many non-MFC cards have 1198 * this bit set always upon read, but some do not. 1199 * 1200 * We always ack the interrupt, even if there's no ISR 1201 * for the card. This is done on the theory that acking 1202 * the interrupt will pacify the card enough to keep an 1203 * interrupt storm from happening. Of course this won't 1204 * help in the non-MFC case. 1205 */ 1206 if (pccard_mfc(pf->sc)) { 1207 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS); 1208 if (reg & PCCARD_CCR_STATUS_INTR) 1209 pccard_ccr_write(pf, PCCARD_CCR_STATUS, 1210 reg & ~PCCARD_CCR_STATUS_INTR); 1211 else 1212 doisr = 0; 1213 } 1214 if (pf->intr_handler != NULL && doisr) 1215 pf->intr_handler(pf->intr_handler_arg); 1216} 1217 1218static int 1219pccard_setup_intr(device_t dev, device_t child, struct resource *irq, 1220 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1221{ 1222 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1223 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1224 struct pccard_function *func = ivar->fcn; 1225 int err; 1226 1227 if (func->intr_handler != NULL) 1228 panic("Only one interrupt handler per function allowed"); 1229 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr, 1230 func, cookiep); 1231 if (err != 0) 1232 return (err); 1233 func->intr_handler = intr; 1234 func->intr_handler_arg = arg; 1235 func->intr_handler_cookie = *cookiep; 1236 if (pccard_mfc(sc)) { 1237 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1238 pccard_ccr_read(func, PCCARD_CCR_OPTION) | 1239 PCCARD_CCR_OPTION_IREQ_ENABLE); 1240 } 1241 return (0); 1242} 1243 1244static int 1245pccard_teardown_intr(device_t dev, device_t child, struct resource *r, 1246 void *cookie) 1247{ 1248 struct pccard_softc *sc = PCCARD_SOFTC(dev); 1249 struct pccard_ivar *ivar = PCCARD_IVAR(child); 1250 struct pccard_function *func = ivar->fcn; 1251 int ret; 1252 1253 if (pccard_mfc(sc)) { 1254 pccard_ccr_write(func, PCCARD_CCR_OPTION, 1255 pccard_ccr_read(func, PCCARD_CCR_OPTION) & 1256 ~PCCARD_CCR_OPTION_IREQ_ENABLE); 1257 } 1258 ret = bus_generic_teardown_intr(dev, child, r, cookie); 1259 if (ret == 0) { 1260 func->intr_handler = NULL; 1261 func->intr_handler_arg = NULL; 1262 func->intr_handler_cookie = NULL; 1263 } 1264 1265 return (ret); 1266} 1267 1268static device_method_t pccard_methods[] = { 1269 /* Device interface */ 1270 DEVMETHOD(device_probe, pccard_probe), 1271 DEVMETHOD(device_attach, pccard_attach), 1272 DEVMETHOD(device_detach, pccard_detach), 1273 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1274 DEVMETHOD(device_suspend, pccard_suspend), 1275 DEVMETHOD(device_resume, pccard_resume), 1276 1277 /* Bus interface */ 1278 DEVMETHOD(bus_print_child, pccard_print_child), 1279 DEVMETHOD(bus_driver_added, pccard_driver_added), 1280 DEVMETHOD(bus_child_detached, pccard_child_detached), 1281 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource), 1282 DEVMETHOD(bus_release_resource, pccard_release_resource), 1283 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 1284 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1285 DEVMETHOD(bus_setup_intr, pccard_setup_intr), 1286 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr), 1287 DEVMETHOD(bus_set_resource, pccard_set_resource), 1288 DEVMETHOD(bus_get_resource, pccard_get_resource), 1289 DEVMETHOD(bus_delete_resource, pccard_delete_resource), 1290 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch), 1291 DEVMETHOD(bus_read_ivar, pccard_read_ivar), 1292 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str), 1293 DEVMETHOD(bus_child_location_str, pccard_child_location_str), 1294 1295 /* Card Interface */ 1296 DEVMETHOD(card_set_res_flags, pccard_set_res_flags), 1297 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset), 1298 DEVMETHOD(card_get_type, pccard_card_gettype), 1299 DEVMETHOD(card_attach_card, pccard_attach_card), 1300 DEVMETHOD(card_detach_card, pccard_detach_card), 1301 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe), 1302 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach), 1303 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup), 1304 1305 { 0, 0 } 1306}; 1307 1308static driver_t pccard_driver = { 1309 "pccard", 1310 pccard_methods, 1311 sizeof(struct pccard_softc) 1312}; 1313 1314devclass_t pccard_devclass; 1315 1316/* Maybe we need to have a slot device? */ 1317DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0); 1318DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0); 1319DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0); 1320DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0); 1321MODULE_VERSION(pccard, 1); 1322/*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/ 1323