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