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