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