Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / netif / ex / if_ex_pccard.c
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/ex/if_ex_pccard.c,v 1.2.2.1 2001/03/05 05:33:20 imp Exp $
27  *      $DragonFly: src/sys/dev/netif/ex/if_ex_pccard.c,v 1.7 2004/02/19 14:31:13 joerg Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34
35 #include <sys/module.h>
36 #include <sys/bus.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41
42 #include <net/if.h>
43 #include <net/if_arp.h>
44 #include <net/if_media.h> 
45
46
47 #include "if_exreg.h"
48 #include "if_exvar.h"
49
50 #include <bus/pccard/pccardvar.h>
51 #include <bus/pccard/pccarddevs.h>
52
53 static const struct pccard_product ex_pccard_products[] = {
54         PCMCIA_CARD(OLICOM, OC2220, 0),
55         { NULL }
56 };
57
58 /* Bus Front End Functions */
59 static int      ex_pccard_match         (device_t);
60 static int      ex_pccard_probe         (device_t);
61 static int      ex_pccard_attach        (device_t);
62 static int      ex_pccard_detach        (device_t);
63
64 static device_method_t ex_pccard_methods[] = {
65         /* Device interface */
66         DEVMETHOD(device_probe,         pccard_compat_probe),
67         DEVMETHOD(device_attach,        pccard_compat_attach),
68         DEVMETHOD(device_detach,        ex_pccard_detach),
69
70         /* Card interface */
71         DEVMETHOD(card_compat_match,    ex_pccard_match),
72         DEVMETHOD(card_compat_probe,    ex_pccard_probe),
73         DEVMETHOD(card_compat_attach,   ex_pccard_attach),
74
75         { 0, 0 }
76 };
77
78 static driver_t ex_pccard_driver = {
79         "ex",
80         ex_pccard_methods,
81         sizeof(struct ex_softc),
82 };
83
84 extern devclass_t ex_devclass;
85
86 DRIVER_MODULE(if_ex, pccard, ex_pccard_driver, ex_devclass, 0, 0);
87
88 static int
89 ex_pccard_match(device_t dev)
90 {
91         const struct pccard_product *pp;
92
93         if ((pp = pccard_product_lookup(dev, ex_pccard_products,
94             sizeof(ex_pccard_products[0]), NULL)) != NULL) {
95                 if (pp->pp_name != NULL)
96                         device_set_desc(dev, pp->pp_name);
97                 return 0;
98         }
99         return EIO;
100 }
101
102 static int
103 ex_pccard_probe(device_t dev)
104 {
105         u_int           iobase;
106         u_int           irq;
107
108         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
109         if (!iobase) {
110                 printf("ex: no iobase?\n");
111                 return(ENXIO);
112         }
113
114         if (bootverbose)
115                 printf("ex: ex_pccard_probe() found card at 0x%03x\n", iobase);
116
117         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
118
119         if (irq == 0) {
120                 printf("ex: invalid IRQ.\n");
121                 return(ENXIO);
122         }
123
124         return(0);
125 }
126
127 static int
128 ex_pccard_attach(device_t dev)
129 {
130         struct ex_softc *       sc = device_get_softc(dev);
131         int                     error = 0;
132         int                     i;
133         uint8_t                 sum;
134         uint8_t *               ether_addr;
135
136         sc->dev = dev;
137         sc->ioport_rid = 0;
138         sc->irq_rid = 0;
139
140         if ((error = ex_alloc_resources(dev)) != 0) {
141                 device_printf(dev, "ex_alloc_resources() failed!\n");
142                 goto bad;
143         }
144
145         /*
146          * Fill in several fields of the softc structure:
147          *      - I/O base address.
148          *      - Hardware Ethernet address.
149          *      - IRQ number.
150          */
151         sc->iobase = rman_get_start(sc->ioport);
152         sc->irq_no = rman_get_start(sc->irq);
153
154         ether_addr = pccard_get_ether(dev);
155         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
156                 sum |= ether_addr[i];
157         if (sum)
158                 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
159
160         if ((error = ex_attach(dev)) != 0) {
161                 device_printf(dev, "ex_attach() failed!\n");
162                 goto bad;
163         }
164
165         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
166                                 ex_intr, (void *)sc, &sc->ih);
167         if (error) {
168                 device_printf(dev, "bus_setup_intr() failed!\n");
169                 goto bad;
170         }
171
172         return(0);
173 bad:
174         ex_release_resources(dev);
175         return (error);
176 }
177
178 static int
179 ex_pccard_detach(device_t dev)
180 {
181         struct ex_softc         *sc = device_get_softc(dev);
182         struct ifnet            *ifp = &sc->arpcom.ac_if;
183
184         ex_stop(sc);
185         ifp->if_flags &= ~IFF_RUNNING;
186         if_detach(ifp);
187         ex_release_resources(dev);
188         return (0);
189 }