]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pccard/pccard.c
Merge ACPICA 20180105.
[FreeBSD/FreeBSD.git] / sys / dev / pccard / pccard.c
1 /*      $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Marc Horowitz.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/types.h>
45
46 #include <sys/bus.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <machine/resource.h>
50
51 #include <net/ethernet.h>
52
53 #include <dev/pccard/pccardreg.h>
54 #include <dev/pccard/pccardvar.h>
55 #include <dev/pccard/pccardvarp.h>
56 #include <dev/pccard/pccard_cis.h>
57
58 #include "power_if.h"
59 #include "card_if.h"
60
61 #define PCCARDDEBUG
62
63 /* sysctl vars */
64 static SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
65
66 int     pccard_debug = 0;
67 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RWTUN,
68     &pccard_debug, 0,
69   "pccard debug");
70
71 int     pccard_cis_debug = 0;
72 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RWTUN,
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, rman_res_t start, rman_res_t count);
103 static int      pccard_get_resource(device_t dev, device_t child, int type,
104                     int rid, rman_res_t *startp, rman_res_t *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, rman_res_t start,
119                     rman_res_t end, rman_res_t 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 = NULL;
476         struct pccard_ce_iospace *ios;
477         struct pccard_ce_memspace *mems;
478         device_t bus;
479         rman_res_t start, end, len;
480         int i, rid, spaces;
481
482         if (pf->pf_flags & PFF_ENABLED) {
483                 printf("pccard_function_init: function is enabled");
484                 return;
485         }
486
487         /*
488          * Driver probe routine requested a specific entry already
489          * that succeeded.
490          */
491         if (pf->cfe != NULL)
492                 return;
493
494         /*
495          * walk the list of configuration entries until we find one that
496          * we can allocate all the resources to.
497          */
498         bus = device_get_parent(pf->dev);
499         STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
500                 if (cfe->iftype != PCCARD_IFTYPE_IO)
501                         continue;
502                 if (entry != -1 && cfe->number != entry)
503                         continue;
504                 spaces = 0;
505                 for (i = 0; i < cfe->num_iospace; i++) {
506                         ios = cfe->iospace + i;
507                         start = ios->start;
508                         if (start)
509                                 end = start + ios->length - 1;
510                         else
511                                 end = ~0;
512                         DEVPRINTF((bus, "I/O rid %d start %#jx end %#jx\n",
513                             i, start, end));
514                         rid = i;
515                         len = ios->length;
516                         r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
517                             start, end, len, rman_make_alignment_flags(len));
518                         if (r == NULL) {
519                                 DEVPRINTF((bus, "I/O rid %d failed\n", i));
520                                 goto not_this_one;
521                         }
522                         rle = resource_list_add(rl, SYS_RES_IOPORT,
523                             rid, rman_get_start(r), rman_get_end(r), len);
524                         if (rle == NULL)
525                                 panic("Cannot add resource rid %d IOPORT", rid);
526                         rle->res = r;
527                         spaces++;
528                 }
529                 for (i = 0; i < cfe->num_memspace; i++) {
530                         mems = cfe->memspace + i;
531                         start = mems->cardaddr + mems->hostaddr;
532                         if (start)
533                                 end = start + mems->length - 1;
534                         else
535                                 end = ~0;
536                         DEVPRINTF((bus, "Memory rid %d start %#jx end %#jx\ncardaddr %#jx hostaddr %#jx length %#jx\n",
537                             i, start, end, mems->cardaddr, mems->hostaddr,
538                             mems->length));
539                         rid = i;
540                         len = mems->length;
541                         r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
542                             start, end, len, rman_make_alignment_flags(len));
543                         if (r == NULL) {
544                                 DEVPRINTF((bus, "Memory rid %d failed\n", i));
545 //                              goto not_this_one;
546                                 continue;
547                         }
548                         rle = resource_list_add(rl, SYS_RES_MEMORY,
549                             rid, rman_get_start(r), rman_get_end(r), len);
550                         if (rle == NULL)
551                                 panic("Cannot add resource rid %d MEM", rid);
552                         rle->res = r;
553                         spaces++;
554                 }
555                 if (spaces == 0) {
556                         DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
557                         goto not_this_one;
558                 }
559                 if (cfe->irqmask) {
560                         rid = 0;
561                         r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
562                             RF_SHAREABLE);
563                         if (r == NULL) {
564                                 DEVPRINTF((bus, "IRQ rid %d failed\n", rid));
565                                 goto not_this_one;
566                         }
567                         rle = resource_list_add(rl, SYS_RES_IRQ, rid,
568                             rman_get_start(r), rman_get_end(r), 1);
569                         if (rle == NULL)
570                                 panic("Cannot add resource rid %d IRQ", rid);
571                         rle->res = r;
572                 }
573                 /* If we get to here, we've allocated all we need */
574                 pf->cfe = cfe;
575                 break;
576             not_this_one:;
577                 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
578                     cfe->number));
579                 resource_list_purge(rl);
580         }
581 }
582
583 /*
584  * Free resources allocated by pccard_function_init(), May be called as long
585  * as the function is disabled.
586  *
587  * NOTE: This function should be unnecessary.  pccard_function_init should
588  * never keep resources initialized.
589  */
590 static void
591 pccard_function_free(struct pccard_function *pf)
592 {
593         struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
594         struct resource_list_entry *rle;
595
596         if (pf->pf_flags & PFF_ENABLED) {
597                 printf("pccard_function_free: function is enabled");
598                 return;
599         }
600
601         STAILQ_FOREACH(rle, &devi->resources, link) {
602                 if (rle->res) {
603                         if (rman_get_device(rle->res) != pf->sc->dev)
604                                 device_printf(pf->sc->dev,
605                                     "function_free: Resource still owned by "
606                                     "child, oops. "
607                                     "(type=%d, rid=%d, addr=%#jx)\n",
608                                     rle->type, rle->rid,
609                                     rman_get_start(rle->res));
610                         BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
611                             pf->sc->dev, rle->type, rle->rid, rle->res);
612                         rle->res = NULL;
613                 }
614         }
615         resource_list_free(&devi->resources);
616 }
617
618 static void
619 pccard_mfc_adjust_iobase(struct pccard_function *pf, rman_res_t addr,
620     rman_res_t offset, rman_res_t size)
621 {
622         bus_size_t iosize, tmp;
623
624         if (addr != 0) {
625                 if (pf->pf_mfc_iomax == 0) {
626                         pf->pf_mfc_iobase = addr + offset;
627                         pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
628                 } else {
629                         /* this makes the assumption that nothing overlaps */
630                         if (pf->pf_mfc_iobase > addr + offset)
631                                 pf->pf_mfc_iobase = addr + offset;
632                         if (pf->pf_mfc_iomax < addr + offset + size)
633                                 pf->pf_mfc_iomax = addr + offset + size;
634                 }
635         }
636
637         tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
638         /* round up to nearest (2^n)-1 */
639         for (iosize = 1; iosize < tmp; iosize <<= 1)
640                 ;
641         iosize--;
642
643         DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
644             (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
645         pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
646             pf->pf_mfc_iobase & 0xff);
647         pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
648             (pf->pf_mfc_iobase >> 8) & 0xff);
649         pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
650         pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
651         pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
652 }
653
654 /* Enable a PCCARD function */
655 static int
656 pccard_function_enable(struct pccard_function *pf)
657 {
658         struct pccard_function *tmp;
659         int reg;
660         device_t dev = pf->sc->dev;
661
662         if (pf->cfe == NULL) {
663                 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
664                 return (ENOMEM);
665         }
666
667         if (pf->pf_flags & PFF_ENABLED)
668                 return (0);
669         pf->sc->sc_enabled_count++;
670
671         /*
672          * it's possible for different functions' CCRs to be in the same
673          * underlying page.  Check for that.
674          */
675         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
676                 if ((tmp->pf_flags & PFF_ENABLED) &&
677                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
678                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
679                     (tmp->ccr_base - tmp->pf_ccr_offset +
680                     tmp->pf_ccr_realsize))) {
681                         pf->pf_ccrt = tmp->pf_ccrt;
682                         pf->pf_ccrh = tmp->pf_ccrh;
683                         pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
684
685                         /*
686                          * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
687                          * tmp->ccr_base) + pf->ccr_base;
688                          */
689                         /* pf->pf_ccr_offset =
690                             (tmp->pf_ccr_offset + pf->ccr_base) -
691                             tmp->ccr_base; */
692                         pf->pf_ccr_window = tmp->pf_ccr_window;
693                         break;
694                 }
695         }
696         if (tmp == NULL) {
697                 pf->ccr_rid = 0;
698                 pf->ccr_res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
699                     &pf->ccr_rid, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
700                 if (!pf->ccr_res)
701                         goto bad;
702                 DEVPRINTF((dev, "ccr_res == %#jx-%#jx, base=%#x\n",
703                     rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
704                     pf->ccr_base));
705                 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
706                     pf->ccr_rid, PCCARD_A_MEM_ATTR);
707                 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
708                     pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
709                 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
710                 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
711                 pf->pf_ccr_realsize = 1;
712         }
713
714         reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
715         reg |= PCCARD_CCR_OPTION_LEVIREQ;
716         if (pccard_mfc(pf->sc)) {
717                 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
718                         PCCARD_CCR_OPTION_ADDR_DECODE);
719                 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
720         }
721         pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
722
723         reg = 0;
724         if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
725                 reg |= PCCARD_CCR_STATUS_IOIS8;
726         if (pf->cfe->flags & PCCARD_CFE_AUDIO)
727                 reg |= PCCARD_CCR_STATUS_AUDIO;
728         pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
729
730         pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
731
732         if (pccard_mfc(pf->sc))
733                 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
734
735 #ifdef PCCARDDEBUG
736         if (pccard_debug) {
737                 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
738                         device_printf(tmp->sc->dev,
739                             "function %d CCR at %d offset %#x: "
740                             "%#x %#x %#x %#x, %#x %#x %#x %#x, %#x\n",
741                             tmp->number, tmp->pf_ccr_window,
742                             tmp->pf_ccr_offset,
743                             pccard_ccr_read(tmp, 0x00),
744                             pccard_ccr_read(tmp, 0x02),
745                             pccard_ccr_read(tmp, 0x04),
746                             pccard_ccr_read(tmp, 0x06),
747                             pccard_ccr_read(tmp, 0x0A),
748                             pccard_ccr_read(tmp, 0x0C),
749                             pccard_ccr_read(tmp, 0x0E),
750                             pccard_ccr_read(tmp, 0x10),
751                             pccard_ccr_read(tmp, 0x12));
752                 }
753         }
754 #endif
755         pf->pf_flags |= PFF_ENABLED;
756         return (0);
757
758  bad:
759         /*
760          * Decrement the reference count, and power down the socket, if
761          * necessary.
762          */
763         pf->sc->sc_enabled_count--;
764         DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
765
766         return (1);
767 }
768
769 /* Disable PCCARD function. */
770 static void
771 pccard_function_disable(struct pccard_function *pf)
772 {
773         struct pccard_function *tmp;
774         device_t dev = pf->sc->dev;
775
776         if (pf->cfe == NULL)
777                 panic("pccard_function_disable: function not initialized");
778
779         if ((pf->pf_flags & PFF_ENABLED) == 0)
780                 return;
781         if (pf->intr_handler != NULL) {
782                 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
783                 struct resource_list_entry *rle =
784                     resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
785                 if (rle == NULL)
786                         panic("Can't disable an interrupt with no IRQ res\n");
787                 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
788                     pf->intr_handler_cookie);
789         }
790
791         /*
792          * it's possible for different functions' CCRs to be in the same
793          * underlying page.  Check for that.  Note we mark us as disabled
794          * first to avoid matching ourself.
795          */
796
797         pf->pf_flags &= ~PFF_ENABLED;
798         STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
799                 if ((tmp->pf_flags & PFF_ENABLED) &&
800                     (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
801                     ((pf->ccr_base + PCCARD_CCR_SIZE) <=
802                     (tmp->ccr_base - tmp->pf_ccr_offset +
803                     tmp->pf_ccr_realsize)))
804                         break;
805         }
806
807         /* Not used by anyone else; unmap the CCR. */
808         if (tmp == NULL) {
809                 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
810                     pf->ccr_res);
811                 pf->ccr_res = NULL;
812         }
813
814         /*
815          * Decrement the reference count, and power down the socket, if
816          * necessary.
817          */
818         pf->sc->sc_enabled_count--;
819 }
820
821 #define PCCARD_NPORT    2
822 #define PCCARD_NMEM     5
823 #define PCCARD_NIRQ     1
824 #define PCCARD_NDRQ     0
825
826 static int
827 pccard_probe(device_t dev)
828 {
829         device_set_desc(dev, "16-bit PCCard bus");
830         return (0);
831 }
832
833 static int
834 pccard_attach(device_t dev)
835 {
836         struct pccard_softc *sc = PCCARD_SOFTC(dev);
837         int err;
838
839         sc->dev = dev;
840         sc->sc_enabled_count = 0;
841         if ((err = pccard_device_create(sc)) != 0)
842                 return  (err);
843         STAILQ_INIT(&sc->card.pf_head);
844         return (bus_generic_attach(dev));
845 }
846
847 static int
848 pccard_detach(device_t dev)
849 {
850         pccard_detach_card(dev);
851         pccard_device_destroy(device_get_softc(dev));
852         return (0);
853 }
854
855 static int
856 pccard_suspend(device_t self)
857 {
858         pccard_detach_card(self);
859         return (0);
860 }
861
862 static
863 int
864 pccard_resume(device_t self)
865 {
866         return (0);
867 }
868
869 static void
870 pccard_print_resources(struct resource_list *rl, const char *name, int type,
871     int count, const char *format)
872 {
873         struct resource_list_entry *rle;
874         int printed;
875         int i;
876
877         printed = 0;
878         for (i = 0; i < count; i++) {
879                 rle = resource_list_find(rl, type, i);
880                 if (rle != NULL) {
881                         if (printed == 0)
882                                 printf(" %s ", name);
883                         else if (printed > 0)
884                                 printf(",");
885                         printed++;
886                         printf(format, rle->start);
887                         if (rle->count > 1) {
888                                 printf("-");
889                                 printf(format, rle->start + rle->count - 1);
890                         }
891                 } else if (i > 3) {
892                         /* check the first few regardless */
893                         break;
894                 }
895         }
896 }
897
898 static int
899 pccard_print_child(device_t dev, device_t child)
900 {
901         struct pccard_ivar *devi = PCCARD_IVAR(child);
902         struct resource_list *rl = &devi->resources;
903         int retval = 0;
904
905         retval += bus_print_child_header(dev, child);
906         retval += printf(" at");
907
908         if (devi != NULL) {
909                 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
910                     PCCARD_NPORT, "%#lx");
911                 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
912                     PCCARD_NMEM, "%#lx");
913                 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
914                     "%ld");
915                 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
916                     "%ld");
917                 retval += printf(" function %d config %d", devi->pf->number,
918                     devi->pf->cfe->number);
919         }
920
921         retval += bus_print_child_footer(dev, child);
922
923         return (retval);
924 }
925
926 static int
927 pccard_set_resource(device_t dev, device_t child, int type, int rid,
928     rman_res_t start, rman_res_t count)
929 {
930         struct pccard_ivar *devi = PCCARD_IVAR(child);
931         struct resource_list *rl = &devi->resources;
932
933         if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
934             && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
935                 return (EINVAL);
936         if (rid < 0)
937                 return (EINVAL);
938         if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
939                 return (EINVAL);
940         if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
941                 return (EINVAL);
942         if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
943                 return (EINVAL);
944         if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
945                 return (EINVAL);
946
947         resource_list_add(rl, type, rid, start, start + count - 1, count);
948         if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
949             type, &rid, start, start + count - 1, count, 0))
950                 return 0;
951         else
952                 return ENOMEM;
953 }
954
955 static int
956 pccard_get_resource(device_t dev, device_t child, int type, int rid,
957     rman_res_t *startp, rman_res_t *countp)
958 {
959         struct pccard_ivar *devi = PCCARD_IVAR(child);
960         struct resource_list *rl = &devi->resources;
961         struct resource_list_entry *rle;
962
963         rle = resource_list_find(rl, type, rid);
964         if (rle == NULL)
965                 return (ENOENT);
966
967         if (startp != NULL)
968                 *startp = rle->start;
969         if (countp != NULL)
970                 *countp = rle->count;
971
972         return (0);
973 }
974
975 static void
976 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
977 {
978         struct pccard_ivar *devi = PCCARD_IVAR(child);
979         struct resource_list *rl = &devi->resources;
980         resource_list_delete(rl, type, rid);
981 }
982
983 static int
984 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
985     u_long flags)
986 {
987         return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
988             rid, flags));
989 }
990
991 static int
992 pccard_set_memory_offset(device_t dev, device_t child, int rid,
993     uint32_t offset, uint32_t *deltap)
994
995 {
996         return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
997             offset, deltap));
998 }
999
1000 static void
1001 pccard_probe_nomatch(device_t bus, device_t child)
1002 {
1003         struct pccard_ivar *devi = PCCARD_IVAR(child);
1004         struct pccard_function *pf = devi->pf;
1005         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1006         int i;
1007
1008         device_printf(bus, "<unknown card>");
1009         printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
1010             "at function %d\n", sc->card.manufacturer, sc->card.product,
1011             pf->function, pf->number);
1012         device_printf(bus, "   CIS info: ");
1013         for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
1014                 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
1015         printf("\n");
1016         return;
1017 }
1018
1019 static int
1020 pccard_child_location_str(device_t bus, device_t child, char *buf,
1021     size_t buflen)
1022 {
1023         struct pccard_ivar *devi = PCCARD_IVAR(child);
1024         struct pccard_function *pf = devi->pf;
1025
1026         snprintf(buf, buflen, "function=%d", pf->number);
1027         return (0);
1028 }
1029
1030 static int
1031 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1032     size_t buflen)
1033 {
1034         struct pccard_ivar *devi = PCCARD_IVAR(child);
1035         struct pccard_function *pf = devi->pf;
1036         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1037         char cis0[128], cis1[128];
1038
1039         devctl_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1040         devctl_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1041         snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1042             "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1043             sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1044         return (0);
1045 }
1046
1047 static int
1048 pccard_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1049 {
1050         struct pccard_ivar *devi = PCCARD_IVAR(child);
1051         struct pccard_function *pf = devi->pf;
1052         struct pccard_softc *sc = PCCARD_SOFTC(bus);
1053
1054         if (!pf)
1055                 panic("No pccard function pointer");
1056         switch (which) {
1057         default:
1058                 return (EINVAL);
1059         case PCCARD_IVAR_FUNCE_DISK:
1060                 *(uint16_t *)result = pf->pf_funce_disk_interface |
1061                     (pf->pf_funce_disk_power << 8);
1062                 break;
1063         case PCCARD_IVAR_ETHADDR:
1064                 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1065                 break;
1066         case PCCARD_IVAR_VENDOR:
1067                 *(uint32_t *)result = sc->card.manufacturer;
1068                 break;
1069         case PCCARD_IVAR_PRODUCT:
1070                 *(uint32_t *)result = sc->card.product;
1071                 break;
1072         case PCCARD_IVAR_PRODEXT:
1073                 *(uint16_t *)result = sc->card.prodext;
1074                 break;
1075         case PCCARD_IVAR_FUNCTION:
1076                 *(uint32_t *)result = pf->function;
1077                 break;
1078         case PCCARD_IVAR_FUNCTION_NUMBER:
1079                 *(uint32_t *)result = pf->number;
1080                 break;
1081         case PCCARD_IVAR_VENDOR_STR:
1082                 *(const char **)result = sc->card.cis1_info[0];
1083                 break;
1084         case PCCARD_IVAR_PRODUCT_STR:
1085                 *(const char **)result = sc->card.cis1_info[1];
1086                 break;
1087         case PCCARD_IVAR_CIS3_STR:
1088                 *(const char **)result = sc->card.cis1_info[2];
1089                 break;
1090         case PCCARD_IVAR_CIS4_STR:
1091                 *(const char **)result = sc->card.cis1_info[3];
1092                 break;
1093         }
1094         return (0);
1095 }
1096
1097 static void
1098 pccard_driver_added(device_t dev, driver_t *driver)
1099 {
1100         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1101         struct pccard_function *pf;
1102         device_t child;
1103
1104         STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1105                 if (STAILQ_EMPTY(&pf->cfe_head))
1106                         continue;
1107                 child = pf->dev;
1108                 if (device_get_state(child) != DS_NOTPRESENT)
1109                         continue;
1110                 pccard_probe_and_attach_child(dev, child, pf);
1111         }
1112         return;
1113 }
1114
1115 static struct resource *
1116 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1117     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1118 {
1119         struct pccard_ivar *dinfo;
1120         struct resource_list_entry *rle = NULL;
1121         int passthrough = (device_get_parent(child) != dev);
1122         int isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
1123         struct resource *r = NULL;
1124
1125         /* XXX I'm no longer sure this is right */
1126         if (passthrough) {
1127                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1128                     type, rid, start, end, count, flags));
1129         }
1130
1131         dinfo = device_get_ivars(child);
1132         rle = resource_list_find(&dinfo->resources, type, *rid);
1133
1134         if (rle == NULL && isdefault)
1135                 return (NULL);  /* no resource of that type/rid */
1136         if (rle == NULL || rle->res == NULL) {
1137                 /* XXX Need to adjust flags */
1138                 r = bus_alloc_resource(dev, type, rid, start, end,
1139                   count, flags);
1140                 if (r == NULL)
1141                     goto bad;
1142                 resource_list_add(&dinfo->resources, type, *rid,
1143                   rman_get_start(r), rman_get_end(r), count);
1144                 rle = resource_list_find(&dinfo->resources, type, *rid);
1145                 if (!rle)
1146                     goto bad;
1147                 rle->res = r;
1148         }
1149         /*
1150          * If dev doesn't own the device, then we can't give this device
1151          * out.
1152          */
1153         if (rman_get_device(rle->res) != dev)
1154                 return (NULL);
1155         rman_set_device(rle->res, child);
1156         if (flags & RF_ACTIVE)
1157                 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1158         return (rle->res);
1159 bad:;
1160         device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1161         return (NULL);
1162 }
1163
1164 static int
1165 pccard_release_resource(device_t dev, device_t child, int type, int rid,
1166     struct resource *r)
1167 {
1168         struct pccard_ivar *dinfo;
1169         int passthrough = (device_get_parent(child) != dev);
1170         struct resource_list_entry *rle = NULL;
1171
1172         if (passthrough)
1173                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1174                     type, rid, r);
1175
1176         dinfo = device_get_ivars(child);
1177
1178         rle = resource_list_find(&dinfo->resources, type, rid);
1179
1180         if (!rle) {
1181                 device_printf(dev, "Allocated resource not found, "
1182                     "%d %#x %#jx %#jx\n",
1183                     type, rid, rman_get_start(r), rman_get_size(r));
1184                 return ENOENT;
1185         }
1186         if (!rle->res) {
1187                 device_printf(dev, "Allocated resource not recorded\n");
1188                 return ENOENT;
1189         }
1190         /*
1191          * Deactivate the resource (since it is being released), and
1192          * assign it to the bus.
1193          */
1194         BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1195         rman_set_device(rle->res, dev);
1196         return (0);
1197 }
1198
1199 static void
1200 pccard_child_detached(device_t parent, device_t dev)
1201 {
1202         struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1203         struct pccard_function *pf = ivar->pf;
1204
1205         pccard_function_disable(pf);
1206 }
1207
1208 static int
1209 pccard_filter(void *arg)
1210 {
1211         struct pccard_function *pf = (struct pccard_function*) arg;
1212         int reg;
1213         int doisr = 1;
1214
1215         /*
1216          * MFC cards know if they interrupted, so we have to ack the
1217          * interrupt and call the ISR.  Non-MFC cards don't have these
1218          * bits, so they always get called.  Many non-MFC cards have
1219          * this bit set always upon read, but some do not.
1220          *
1221          * We always ack the interrupt, even if there's no ISR
1222          * for the card.  This is done on the theory that acking
1223          * the interrupt will pacify the card enough to keep an
1224          * interrupt storm from happening.  Of course this won't
1225          * help in the non-MFC case.
1226          *
1227          * This has no impact for MPSAFEness of the client drivers.
1228          * We register this with whatever flags the intr_handler
1229          * was registered with.  All these functions are MPSAFE.
1230          */
1231         if (pccard_mfc(pf->sc)) {
1232                 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1233                 if (reg & PCCARD_CCR_STATUS_INTR)
1234                         pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1235                             reg & ~PCCARD_CCR_STATUS_INTR);
1236                 else
1237                         doisr = 0;
1238         }
1239         if (doisr) {
1240                 if (pf->intr_filter != NULL)
1241                         return (pf->intr_filter(pf->intr_handler_arg));
1242                 return (FILTER_SCHEDULE_THREAD);
1243         }
1244         return (FILTER_STRAY);
1245 }
1246
1247 static void
1248 pccard_intr(void *arg)
1249 {
1250         struct pccard_function *pf = (struct pccard_function*) arg;
1251         
1252         pf->intr_handler(pf->intr_handler_arg); 
1253 }
1254
1255 static int
1256 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1257     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, 
1258     void **cookiep)
1259 {
1260         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1261         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1262         struct pccard_function *pf = ivar->pf;
1263         int err;
1264
1265         if (pf->intr_filter != NULL || pf->intr_handler != NULL)
1266                 panic("Only one interrupt handler per function allowed");
1267         err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter, 
1268             intr ? pccard_intr : NULL, pf, cookiep);
1269         if (err != 0)
1270                 return (err);
1271         pf->intr_filter = filt;
1272         pf->intr_handler = intr;
1273         pf->intr_handler_arg = arg;
1274         pf->intr_handler_cookie = *cookiep;
1275         if (pccard_mfc(sc)) {
1276                 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1277                     pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1278                     PCCARD_CCR_OPTION_IREQ_ENABLE);
1279         }
1280         return (0);
1281 }
1282
1283 static int
1284 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1285     void *cookie)
1286 {
1287         struct pccard_softc *sc = PCCARD_SOFTC(dev);
1288         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1289         struct pccard_function *pf = ivar->pf;
1290         int ret;
1291
1292         if (pccard_mfc(sc)) {
1293                 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1294                     pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1295                     ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1296         }
1297         ret = bus_generic_teardown_intr(dev, child, r, cookie);
1298         if (ret == 0) {
1299                 pf->intr_handler = NULL;
1300                 pf->intr_handler_arg = NULL;
1301                 pf->intr_handler_cookie = NULL;
1302         }
1303
1304         return (ret);
1305 }
1306
1307 static int
1308 pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1309     struct resource *r)
1310 {
1311         struct pccard_ivar *ivar = PCCARD_IVAR(child);
1312         struct pccard_function *pf = ivar->pf;
1313
1314         switch(type) {
1315         case SYS_RES_IOPORT:
1316                 /*
1317                  * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1318                  * card.
1319                  */
1320                 if (pccard_mfc(pf->sc))
1321                         pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1322                             rman_get_size(r));
1323                 break;
1324         default:
1325                 break;
1326         }
1327         return (bus_generic_activate_resource(brdev, child, type, rid, r));
1328 }
1329
1330 static int
1331 pccard_deactivate_resource(device_t brdev, device_t child, int type,
1332     int rid, struct resource *r)
1333 {
1334         /* XXX undo pccard_activate_resource? XXX */
1335         return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1336 }
1337
1338 static int
1339 pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1340     uint8_t *val)
1341 {
1342         struct pccard_ivar *devi = PCCARD_IVAR(child);
1343         struct pccard_function *pf = devi->pf;
1344
1345         /*
1346          * Optimization.  Most of the time, devices want to access
1347          * the same page of the attribute memory that the CCR is in.
1348          * We take advantage of this fact here.
1349          */
1350         if (offset / PCCARD_MEM_PAGE_SIZE ==
1351             pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1352                 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1353                     offset % PCCARD_MEM_PAGE_SIZE);
1354         else {
1355                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1356                     &offset);
1357                 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1358                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1359                     &offset);
1360         }
1361         return 0;
1362 }
1363
1364 static int
1365 pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1366     uint8_t val)
1367 {
1368         struct pccard_ivar *devi = PCCARD_IVAR(child);
1369         struct pccard_function *pf = devi->pf;
1370
1371         /*
1372          * Optimization.  Most of the time, devices want to access
1373          * the same page of the attribute memory that the CCR is in.
1374          * We take advantage of this fact here.
1375          */
1376         if (offset / PCCARD_MEM_PAGE_SIZE ==
1377             pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1378                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1379                     offset % PCCARD_MEM_PAGE_SIZE, val);
1380         else {
1381                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1382                     &offset);
1383                 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1384                 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1385                     &offset);
1386         }
1387
1388         return 0;
1389 }
1390
1391 static int
1392 pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1393     uint8_t *val)
1394 {
1395         struct pccard_ivar *devi = PCCARD_IVAR(child);
1396
1397         *val = pccard_ccr_read(devi->pf, offset);
1398         DEVPRINTF((child, "ccr_read of %#x (%#x) is %#x\n", offset,
1399           devi->pf->pf_ccr_offset, *val));
1400         return 0;
1401 }
1402
1403 static int
1404 pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1405     uint8_t val)
1406 {
1407         struct pccard_ivar *devi = PCCARD_IVAR(child);
1408         struct pccard_function *pf = devi->pf;
1409
1410         /*
1411          * Can't use pccard_ccr_write since client drivers may access
1412          * registers not contained in the 'mask' if they are non-standard.
1413          */
1414         DEVPRINTF((child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1415           devi->pf->pf_ccr_offset));
1416         bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1417             val);
1418         return 0;
1419 }
1420
1421
1422 static device_method_t pccard_methods[] = {
1423         /* Device interface */
1424         DEVMETHOD(device_probe,         pccard_probe),
1425         DEVMETHOD(device_attach,        pccard_attach),
1426         DEVMETHOD(device_detach,        pccard_detach),
1427         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1428         DEVMETHOD(device_suspend,       pccard_suspend),
1429         DEVMETHOD(device_resume,        pccard_resume),
1430
1431         /* Bus interface */
1432         DEVMETHOD(bus_print_child,      pccard_print_child),
1433         DEVMETHOD(bus_driver_added,     pccard_driver_added),
1434         DEVMETHOD(bus_child_detached,   pccard_child_detached),
1435         DEVMETHOD(bus_alloc_resource,   pccard_alloc_resource),
1436         DEVMETHOD(bus_release_resource, pccard_release_resource),
1437         DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1438         DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1439         DEVMETHOD(bus_setup_intr,       pccard_setup_intr),
1440         DEVMETHOD(bus_teardown_intr,    pccard_teardown_intr),
1441         DEVMETHOD(bus_set_resource,     pccard_set_resource),
1442         DEVMETHOD(bus_get_resource,     pccard_get_resource),
1443         DEVMETHOD(bus_delete_resource,  pccard_delete_resource),
1444         DEVMETHOD(bus_probe_nomatch,    pccard_probe_nomatch),
1445         DEVMETHOD(bus_read_ivar,        pccard_read_ivar),
1446         DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1447         DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1448
1449         /* Card Interface */
1450         DEVMETHOD(card_set_res_flags,   pccard_set_res_flags),
1451         DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1452         DEVMETHOD(card_attach_card,     pccard_attach_card),
1453         DEVMETHOD(card_detach_card,     pccard_detach_card),
1454         DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1455         DEVMETHOD(card_cis_scan,        pccard_scan_cis),
1456         DEVMETHOD(card_attr_read,       pccard_attr_read_impl),
1457         DEVMETHOD(card_attr_write,      pccard_attr_write_impl),
1458         DEVMETHOD(card_ccr_read,        pccard_ccr_read_impl),
1459         DEVMETHOD(card_ccr_write,       pccard_ccr_write_impl),
1460
1461         { 0, 0 }
1462 };
1463
1464 static driver_t pccard_driver = {
1465         "pccard",
1466         pccard_methods,
1467         sizeof(struct pccard_softc)
1468 };
1469
1470 devclass_t      pccard_devclass;
1471
1472 /* Maybe we need to have a slot device? */
1473 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1474 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1475 MODULE_VERSION(pccard, 1);