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