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