]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ex/if_ex_pccard.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / 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  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35
36 #include <sys/module.h>
37 #include <sys/bus.h>
38
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_media.h> 
46
47 #include <dev/ex/if_exreg.h>
48 #include <dev/ex/if_exvar.h>
49
50 #include <dev/pccard/pccardvar.h>
51 #include <dev/pccard/pccard_cis.h>
52
53 #include "pccarddevs.h"
54
55 static const struct pccard_product ex_pccard_products[] = {
56         PCMCIA_CARD(OLICOM, OC2220),
57         PCMCIA_CARD(OLICOM, OC2231),
58         PCMCIA_CARD(OLICOM, OC2232),
59         PCMCIA_CARD(INTEL, ETHEREXPPRO),
60         { NULL }
61 };
62
63 /* Bus Front End Functions */
64 static int      ex_pccard_probe(device_t);
65 static int      ex_pccard_attach(device_t);
66
67 static int
68 ex_pccard_enet_ok(u_char *enaddr)
69 {
70         int                     i;
71         u_char                  sum;
72
73         if (enaddr[0] == 0xff)
74                 return (0);
75         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
76                 sum |= enaddr[i];
77         return (sum != 0);
78 }
79
80 static int
81 ex_pccard_silicom_cb(const struct pccard_tuple *tuple, void *arg)
82 {
83         u_char *enaddr = arg;
84         int i;
85
86         if (tuple->code != CISTPL_FUNCE)
87                 return (0);
88         if (tuple->length != 15)
89                 return (0);
90         if (pccard_tuple_read_1(tuple, 6) != 6)
91                 return (0);
92         for (i = 0; i < 6; i++)
93                 enaddr[i] = pccard_tuple_read_1(tuple, 7 + i);
94         return (1);
95 }
96
97 static void
98 ex_pccard_get_silicom_mac(device_t dev, u_char *ether_addr)
99 {
100         pccard_cis_scan(dev, ex_pccard_silicom_cb, ether_addr);
101 }
102
103 static int
104 ex_pccard_probe(device_t dev)
105 {
106         const struct pccard_product *pp;
107         int error;
108         uint32_t        fcn = PCCARD_FUNCTION_UNSPEC;
109
110         /* Make sure we're a network function */
111         error = pccard_get_function(dev, &fcn);
112         if (error != 0)
113                 return (error);
114         if (fcn != PCCARD_FUNCTION_NETWORK)
115                 return (ENXIO);
116
117         if ((pp = pccard_product_lookup(dev, ex_pccard_products,
118             sizeof(ex_pccard_products[0]), NULL)) != NULL) {
119                 if (pp->pp_name != NULL)
120                         device_set_desc(dev, pp->pp_name);
121                 return 0;
122         }
123         return EIO;
124 }
125
126 static int
127 ex_pccard_attach(device_t dev)
128 {
129         struct ex_softc *       sc = device_get_softc(dev);
130         int                     error = 0;
131         u_char                  ether_addr[ETHER_ADDR_LEN];
132
133         sc->dev = dev;
134         sc->ioport_rid = 0;
135         sc->irq_rid = 0;
136
137         if ((error = ex_alloc_resources(dev)) != 0) {
138                 device_printf(dev, "ex_alloc_resources() failed!\n");
139                 goto bad;
140         }
141
142         /*
143          * Fill in several fields of the softc structure:
144          *      - Hardware Ethernet address.
145          *      - IRQ number.
146          */
147         sc->irq_no = rman_get_start(sc->irq);
148
149         /* Try to get the ethernet address from the chip, then the CIS */
150         ex_get_address(sc, ether_addr);
151         if (!ex_pccard_enet_ok(ether_addr))
152                 pccard_get_ether(dev, ether_addr);
153         if (!ex_pccard_enet_ok(ether_addr))
154                 ex_pccard_get_silicom_mac(dev, ether_addr);
155         if (!ex_pccard_enet_ok(ether_addr)) {
156                 device_printf(dev, "No NIC address found.\n");
157                 error = ENXIO;
158                 goto bad;
159         }
160         bcopy(ether_addr, sc->enaddr, ETHER_ADDR_LEN);
161
162         if ((error = ex_attach(dev)) != 0) {
163                 device_printf(dev, "ex_attach() failed!\n");
164                 goto bad;
165         }
166
167         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
168             NULL, ex_intr, (void *)sc, &sc->ih);
169         if (error) {
170                 device_printf(dev, "bus_setup_intr() failed!\n");
171                 goto bad;
172         }
173
174         return(0);
175 bad:
176         ex_release_resources(dev);
177         return (error);
178 }
179 static device_method_t ex_pccard_methods[] = {
180         /* Device interface */
181         DEVMETHOD(device_probe,         ex_pccard_probe),
182         DEVMETHOD(device_attach,        ex_pccard_attach),
183         DEVMETHOD(device_detach,        ex_detach),
184
185         { 0, 0 }
186 };
187
188 static driver_t ex_pccard_driver = {
189         "ex",
190         ex_pccard_methods,
191         sizeof(struct ex_softc),
192 };
193
194 DRIVER_MODULE(ex, pccard, ex_pccard_driver, ex_devclass, 0, 0);