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: releng/5.3/sys/dev/ex/if_ex_pccard.c 131192 2004-06-27 13:10:20Z imp $");
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
52 #include "pccarddevs.h"
53
54 static const struct pccard_product ex_pccard_products[] = {
55 PCMCIA_CARD(OLICOM, OC2220, 0),
56 PCMCIA_CARD(INTEL, ETHEREXPPRO, 0),
57 { NULL }
58 };
59
60 /* Bus Front End Functions */
61 static int ex_pccard_match(device_t);
62 static int ex_pccard_probe(device_t);
63 static int ex_pccard_attach(device_t);
64
65 static device_method_t ex_pccard_methods[] = {
66 /* Device interface */
67 DEVMETHOD(device_probe, pccard_compat_probe),
68 DEVMETHOD(device_attach, pccard_compat_attach),
69 DEVMETHOD(device_detach, ex_detach),
70
71 /* Card interface */
72 DEVMETHOD(card_compat_match, ex_pccard_match),
73 DEVMETHOD(card_compat_probe, ex_pccard_probe),
74 DEVMETHOD(card_compat_attach, ex_pccard_attach),
75
76 { 0, 0 }
77 };
78
79 static driver_t ex_pccard_driver = {
80 "ex",
81 ex_pccard_methods,
82 sizeof(struct ex_softc),
83 };
84
85 DRIVER_MODULE(ex, pccard, ex_pccard_driver, ex_devclass, 0, 0);
86
87 static int
88 ex_pccard_match(device_t dev)
89 {
90 const struct pccard_product *pp;
91
92 if ((pp = pccard_product_lookup(dev, ex_pccard_products,
93 sizeof(ex_pccard_products[0]), NULL)) != NULL) {
94 if (pp->pp_name != NULL)
95 device_set_desc(dev, pp->pp_name);
96 return 0;
97 }
98 return EIO;
99 }
100
101 static int
102 ex_pccard_probe(device_t dev)
103 {
104 u_int iobase;
105 u_int irq;
106
107 iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
108 if (!iobase) {
109 printf("ex: no iobase?\n");
110 return(ENXIO);
111 }
112
113 if (bootverbose)
114 printf("ex: ex_pccard_probe() found card at 0x%03x\n", iobase);
115
116 irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
117
118 if (irq == 0) {
119 printf("ex: invalid IRQ.\n");
120 return(ENXIO);
121 }
122
123 return(0);
124 }
125
126 static int
127 ex_pccard_enet_ok(u_char *enaddr)
128 {
129 int i;
130 u_char sum;
131
132 if (enaddr[0] == 0xff)
133 return (0);
134 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
135 sum |= enaddr[i];
136 return (sum != 0);
137 }
138
139 #if 0
140 #ifdef NETBSD_LIKE
141 static int
142 ex_pccard_silicom_cb(struct pccard_tuple *tuple, void *arg)
143 {
144 u_char *enaddr = arg;
145
146 if (tuple->code != PCMCIA_CISTPL_FUNCE)
147 return (0);
148 if (tuple->length != 15)
149 return (0);
150 if (CARD_CIS_READ_1(tuple->dev, tuple, 6) != 6)
151 return (0);
152 for (i = 0; i < 6; i++)
153 enaddr[i] = CARD_CIS_READ_1(tuple->dev, tuple, 7 + i);
154 return (1);
155 }
156 #endif
157 #endif
158
159 static void
160 ex_pccard_get_silicom_mac(device_t dev, u_char *ether_addr)
161 {
162 #if 0
163 #ifdef NETBSD_LIKE
164 CARD_CIS_SCAN(dev, ex_pccard_silicom_cb, ether_addr);
165 #endif
166 #ifdef CS_LIKE
167 uint8_t buffer[64];
168 tuple_t tuple;
169 int i;
170
171 tuple.TupleData = buffer;
172 tuple.TupleDataMax = sizeof(buffer);
173 tuple.TupleOffset = 0;
174 tuple.DesiredTuple = CISTPL_FUNCE;
175 tuple.Attributes = TUPLE_RETURN_COMMON;
176 if (CARD_SERVICE(dev, GetFirstTuple, &tuple) != CS_SUCCESS)
177 return;
178 if (CARD_SERVICES(dev, GetTupleData, &tuple) != CS_SUCCESS)
179 return;
180 if (tuple.TupleLength != 15)
181 return;
182 if (buffer[6] != 6)
183 return;
184 for (i = 0; i < 6; i++)
185 ether_addr[i] = buffer[7 + i);
186 #endif
187 #endif
188 }
189
190 static int
191 ex_pccard_attach(device_t dev)
192 {
193 struct ex_softc * sc = device_get_softc(dev);
194 int error = 0;
195 u_char ether_addr[ETHER_ADDR_LEN];
196
197 sc->dev = dev;
198 sc->ioport_rid = 0;
199 sc->irq_rid = 0;
200
201 if ((error = ex_alloc_resources(dev)) != 0) {
202 device_printf(dev, "ex_alloc_resources() failed!\n");
203 goto bad;
204 }
205
206 /*
207 * Fill in several fields of the softc structure:
208 * - Hardware Ethernet address.
209 * - IRQ number.
210 */
211 sc->irq_no = rman_get_start(sc->irq);
212
213 /* Try to get the ethernet address from the chip, then the CIS */
214 ex_get_address(sc, ether_addr);
215 if (!ex_pccard_enet_ok(ether_addr))
216 pccard_get_ether(dev, ether_addr);
217 if (!ex_pccard_enet_ok(ether_addr))
218 ex_pccard_get_silicom_mac(dev, ether_addr);
219 if (!ex_pccard_enet_ok(ether_addr)) {
220 device_printf(dev, "No NIC address found.\n");
221 error = ENXIO;
222 goto bad;
223 }
224 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
225
226 if ((error = ex_attach(dev)) != 0) {
227 device_printf(dev, "ex_attach() failed!\n");
228 goto bad;
229 }
230
231 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
232 ex_intr, (void *)sc, &sc->ih);
233 if (error) {
234 device_printf(dev, "bus_setup_intr() failed!\n");
235 goto bad;
236 }
237
238 return(0);
239 bad:
240 ex_release_resources(dev);
241 return (error);
242 }
Cache object: 5981a07e3223ae01995d8189eed88da8
|