if_cue.c revision 1.26.2.5 1 1.26.2.5 bouyer /* $NetBSD: if_cue.c,v 1.26.2.5 2001/02/11 19:16:22 bouyer Exp $ */
2 1.26.2.2 bouyer /*
3 1.26.2.2 bouyer * Copyright (c) 1997, 1998, 1999, 2000
4 1.26.2.2 bouyer * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 1.26.2.2 bouyer *
6 1.26.2.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.26.2.2 bouyer * modification, are permitted provided that the following conditions
8 1.26.2.2 bouyer * are met:
9 1.26.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.26.2.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.26.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.26.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.26.2.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.26.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.26.2.2 bouyer * must display the following acknowledgement:
16 1.26.2.2 bouyer * This product includes software developed by Bill Paul.
17 1.26.2.2 bouyer * 4. Neither the name of the author nor the names of any co-contributors
18 1.26.2.2 bouyer * may be used to endorse or promote products derived from this software
19 1.26.2.2 bouyer * without specific prior written permission.
20 1.26.2.2 bouyer *
21 1.26.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 1.26.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.26.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.26.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 1.26.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.26.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.26.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.26.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.26.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.26.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.26.2.2 bouyer * THE POSSIBILITY OF SUCH DAMAGE.
32 1.26.2.2 bouyer *
33 1.26.2.2 bouyer * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
34 1.26.2.2 bouyer */
35 1.26.2.2 bouyer
36 1.26.2.2 bouyer /*
37 1.26.2.2 bouyer * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 1.26.2.2 bouyer * adapters and others.
39 1.26.2.2 bouyer *
40 1.26.2.2 bouyer * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 1.26.2.2 bouyer * Electrical Engineering Department
42 1.26.2.2 bouyer * Columbia University, New York City
43 1.26.2.2 bouyer */
44 1.26.2.2 bouyer
45 1.26.2.2 bouyer /*
46 1.26.2.2 bouyer * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 1.26.2.2 bouyer * RX filter uses a 512-bit multicast hash table, single perfect entry
48 1.26.2.2 bouyer * for the station address, and promiscuous mode. Unlike the ADMtek
49 1.26.2.2 bouyer * and KLSI chips, the CATC ASIC supports read and write combining
50 1.26.2.2 bouyer * mode where multiple packets can be transfered using a single bulk
51 1.26.2.2 bouyer * transaction, which helps performance a great deal.
52 1.26.2.2 bouyer */
53 1.26.2.2 bouyer
54 1.26.2.2 bouyer /*
55 1.26.2.2 bouyer * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
56 1.26.2.2 bouyer */
57 1.26.2.2 bouyer
58 1.26.2.2 bouyer #if defined(__NetBSD__)
59 1.26.2.2 bouyer #include "opt_inet.h"
60 1.26.2.2 bouyer #include "opt_ns.h"
61 1.26.2.2 bouyer #include "bpfilter.h"
62 1.26.2.2 bouyer #include "rnd.h"
63 1.26.2.2 bouyer #elif defined(__OpenBSD__)
64 1.26.2.2 bouyer #include "bpfilter.h"
65 1.26.2.2 bouyer #endif /* defined(__OpenBSD__) */
66 1.26.2.2 bouyer
67 1.26.2.2 bouyer #include <sys/param.h>
68 1.26.2.2 bouyer #include <sys/systm.h>
69 1.26.2.2 bouyer #if !defined(__OpenBSD__)
70 1.26.2.2 bouyer #include <sys/callout.h>
71 1.26.2.2 bouyer #endif
72 1.26.2.2 bouyer #include <sys/sockio.h>
73 1.26.2.2 bouyer #include <sys/mbuf.h>
74 1.26.2.2 bouyer #include <sys/malloc.h>
75 1.26.2.2 bouyer #include <sys/kernel.h>
76 1.26.2.2 bouyer #include <sys/socket.h>
77 1.26.2.2 bouyer
78 1.26.2.2 bouyer #include <sys/device.h>
79 1.26.2.2 bouyer #if NRND > 0
80 1.26.2.2 bouyer #include <sys/rnd.h>
81 1.26.2.2 bouyer #endif
82 1.26.2.2 bouyer
83 1.26.2.2 bouyer #include <net/if.h>
84 1.26.2.5 bouyer #if defined(__NetBSD__)
85 1.26.2.2 bouyer #include <net/if_arp.h>
86 1.26.2.2 bouyer #endif
87 1.26.2.2 bouyer #include <net/if_dl.h>
88 1.26.2.2 bouyer
89 1.26.2.2 bouyer #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
90 1.26.2.2 bouyer
91 1.26.2.5 bouyer #if NBPFILTER > 0
92 1.26.2.2 bouyer #include <net/bpf.h>
93 1.26.2.2 bouyer #endif
94 1.26.2.2 bouyer
95 1.26.2.2 bouyer #if defined(__NetBSD__)
96 1.26.2.2 bouyer #include <net/if_ether.h>
97 1.26.2.2 bouyer #ifdef INET
98 1.26.2.2 bouyer #include <netinet/in.h>
99 1.26.2.2 bouyer #include <netinet/if_inarp.h>
100 1.26.2.2 bouyer #endif
101 1.26.2.2 bouyer #endif /* defined(__NetBSD__) */
102 1.26.2.2 bouyer
103 1.26.2.2 bouyer #if defined(__OpenBSD__)
104 1.26.2.2 bouyer #ifdef INET
105 1.26.2.2 bouyer #include <netinet/in.h>
106 1.26.2.2 bouyer #include <netinet/in_systm.h>
107 1.26.2.2 bouyer #include <netinet/in_var.h>
108 1.26.2.2 bouyer #include <netinet/ip.h>
109 1.26.2.2 bouyer #include <netinet/if_ether.h>
110 1.26.2.2 bouyer #endif
111 1.26.2.2 bouyer #endif /* defined(__OpenBSD__) */
112 1.26.2.2 bouyer
113 1.26.2.2 bouyer #ifdef NS
114 1.26.2.2 bouyer #include <netns/ns.h>
115 1.26.2.2 bouyer #include <netns/ns_if.h>
116 1.26.2.2 bouyer #endif
117 1.26.2.2 bouyer
118 1.26.2.2 bouyer #include <dev/usb/usb.h>
119 1.26.2.2 bouyer #include <dev/usb/usbdi.h>
120 1.26.2.2 bouyer #include <dev/usb/usbdi_util.h>
121 1.26.2.2 bouyer #include <dev/usb/usbdevs.h>
122 1.26.2.2 bouyer
123 1.26.2.2 bouyer #include <dev/usb/if_cuereg.h>
124 1.26.2.2 bouyer
125 1.26.2.2 bouyer #ifdef CUE_DEBUG
126 1.26.2.2 bouyer #define DPRINTF(x) if (cuedebug) logprintf x
127 1.26.2.2 bouyer #define DPRINTFN(n,x) if (cuedebug >= (n)) logprintf x
128 1.26.2.2 bouyer int cuedebug = 0;
129 1.26.2.2 bouyer #else
130 1.26.2.2 bouyer #define DPRINTF(x)
131 1.26.2.2 bouyer #define DPRINTFN(n,x)
132 1.26.2.2 bouyer #endif
133 1.26.2.2 bouyer
134 1.26.2.2 bouyer /*
135 1.26.2.2 bouyer * Various supported device vendors/products.
136 1.26.2.2 bouyer */
137 1.26.2.2 bouyer Static struct cue_type cue_devs[] = {
138 1.26.2.2 bouyer { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
139 1.26.2.2 bouyer { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
140 1.26.2.2 bouyer { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
141 1.26.2.2 bouyer /* Belkin F5U111 adapter covered by NETMATE entry */
142 1.26.2.2 bouyer { 0, 0 }
143 1.26.2.2 bouyer };
144 1.26.2.2 bouyer
145 1.26.2.2 bouyer USB_DECLARE_DRIVER(cue);
146 1.26.2.2 bouyer
147 1.26.2.2 bouyer Static int cue_open_pipes(struct cue_softc *);
148 1.26.2.2 bouyer Static int cue_tx_list_init(struct cue_softc *);
149 1.26.2.2 bouyer Static int cue_rx_list_init(struct cue_softc *);
150 1.26.2.2 bouyer Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
151 1.26.2.2 bouyer Static int cue_send(struct cue_softc *, struct mbuf *, int);
152 1.26.2.2 bouyer Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
153 1.26.2.2 bouyer Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
154 1.26.2.2 bouyer Static void cue_tick(void *);
155 1.26.2.5 bouyer Static void cue_tick_task(void *);
156 1.26.2.2 bouyer Static void cue_start(struct ifnet *);
157 1.26.2.2 bouyer Static int cue_ioctl(struct ifnet *, u_long, caddr_t);
158 1.26.2.2 bouyer Static void cue_init(void *);
159 1.26.2.2 bouyer Static void cue_stop(struct cue_softc *);
160 1.26.2.2 bouyer Static void cue_watchdog(struct ifnet *);
161 1.26.2.2 bouyer
162 1.26.2.2 bouyer Static void cue_setmulti(struct cue_softc *);
163 1.26.2.2 bouyer Static u_int32_t cue_crc(caddr_t);
164 1.26.2.2 bouyer Static void cue_reset(struct cue_softc *);
165 1.26.2.2 bouyer
166 1.26.2.2 bouyer Static int cue_csr_read_1(struct cue_softc *, int);
167 1.26.2.2 bouyer Static int cue_csr_write_1(struct cue_softc *, int, int);
168 1.26.2.2 bouyer Static int cue_csr_read_2(struct cue_softc *, int);
169 1.26.2.2 bouyer #if 0
170 1.26.2.2 bouyer Static int cue_csr_write_2(struct cue_softc *, int, int);
171 1.26.2.2 bouyer #endif
172 1.26.2.2 bouyer Static int cue_mem(struct cue_softc *, int, int, void *, int);
173 1.26.2.2 bouyer Static int cue_getmac(struct cue_softc *, void *);
174 1.26.2.2 bouyer
175 1.26.2.2 bouyer #define CUE_SETBIT(sc, reg, x) \
176 1.26.2.2 bouyer cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
177 1.26.2.2 bouyer
178 1.26.2.2 bouyer #define CUE_CLRBIT(sc, reg, x) \
179 1.26.2.2 bouyer cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
180 1.26.2.2 bouyer
181 1.26.2.2 bouyer Static int
182 1.26.2.2 bouyer cue_csr_read_1(struct cue_softc *sc, int reg)
183 1.26.2.2 bouyer {
184 1.26.2.2 bouyer usb_device_request_t req;
185 1.26.2.2 bouyer usbd_status err;
186 1.26.2.2 bouyer u_int8_t val = 0;
187 1.26.2.2 bouyer
188 1.26.2.2 bouyer if (sc->cue_dying)
189 1.26.2.2 bouyer return (0);
190 1.26.2.2 bouyer
191 1.26.2.2 bouyer req.bmRequestType = UT_READ_VENDOR_DEVICE;
192 1.26.2.2 bouyer req.bRequest = CUE_CMD_READREG;
193 1.26.2.2 bouyer USETW(req.wValue, 0);
194 1.26.2.2 bouyer USETW(req.wIndex, reg);
195 1.26.2.2 bouyer USETW(req.wLength, 1);
196 1.26.2.2 bouyer
197 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, &val);
198 1.26.2.2 bouyer
199 1.26.2.2 bouyer if (err) {
200 1.26.2.2 bouyer DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
201 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
202 1.26.2.2 bouyer return (0);
203 1.26.2.2 bouyer }
204 1.26.2.2 bouyer
205 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
206 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, val));
207 1.26.2.2 bouyer
208 1.26.2.2 bouyer return (val);
209 1.26.2.2 bouyer }
210 1.26.2.2 bouyer
211 1.26.2.2 bouyer Static int
212 1.26.2.2 bouyer cue_csr_read_2(struct cue_softc *sc, int reg)
213 1.26.2.2 bouyer {
214 1.26.2.2 bouyer usb_device_request_t req;
215 1.26.2.2 bouyer usbd_status err;
216 1.26.2.2 bouyer uWord val;
217 1.26.2.2 bouyer
218 1.26.2.2 bouyer if (sc->cue_dying)
219 1.26.2.2 bouyer return (0);
220 1.26.2.2 bouyer
221 1.26.2.2 bouyer req.bmRequestType = UT_READ_VENDOR_DEVICE;
222 1.26.2.2 bouyer req.bRequest = CUE_CMD_READREG;
223 1.26.2.2 bouyer USETW(req.wValue, 0);
224 1.26.2.2 bouyer USETW(req.wIndex, reg);
225 1.26.2.2 bouyer USETW(req.wLength, 2);
226 1.26.2.2 bouyer
227 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, &val);
228 1.26.2.2 bouyer
229 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
230 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, UGETW(val)));
231 1.26.2.2 bouyer
232 1.26.2.2 bouyer if (err) {
233 1.26.2.2 bouyer DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
234 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
235 1.26.2.2 bouyer return (0);
236 1.26.2.2 bouyer }
237 1.26.2.2 bouyer
238 1.26.2.2 bouyer return (UGETW(val));
239 1.26.2.2 bouyer }
240 1.26.2.2 bouyer
241 1.26.2.2 bouyer Static int
242 1.26.2.2 bouyer cue_csr_write_1(struct cue_softc *sc, int reg, int val)
243 1.26.2.2 bouyer {
244 1.26.2.2 bouyer usb_device_request_t req;
245 1.26.2.2 bouyer usbd_status err;
246 1.26.2.2 bouyer
247 1.26.2.2 bouyer if (sc->cue_dying)
248 1.26.2.2 bouyer return (0);
249 1.26.2.2 bouyer
250 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
251 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, val));
252 1.26.2.2 bouyer
253 1.26.2.2 bouyer req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
254 1.26.2.2 bouyer req.bRequest = CUE_CMD_WRITEREG;
255 1.26.2.2 bouyer USETW(req.wValue, val);
256 1.26.2.2 bouyer USETW(req.wIndex, reg);
257 1.26.2.2 bouyer USETW(req.wLength, 0);
258 1.26.2.2 bouyer
259 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, NULL);
260 1.26.2.2 bouyer
261 1.26.2.2 bouyer if (err) {
262 1.26.2.2 bouyer DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
263 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
264 1.26.2.2 bouyer return (-1);
265 1.26.2.2 bouyer }
266 1.26.2.2 bouyer
267 1.26.2.2 bouyer DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
268 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
269 1.26.2.2 bouyer
270 1.26.2.2 bouyer return (0);
271 1.26.2.2 bouyer }
272 1.26.2.2 bouyer
273 1.26.2.2 bouyer #if 0
274 1.26.2.2 bouyer Static int
275 1.26.2.2 bouyer cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
276 1.26.2.2 bouyer {
277 1.26.2.2 bouyer usb_device_request_t req;
278 1.26.2.2 bouyer usbd_status err;
279 1.26.2.2 bouyer uWord val;
280 1.26.2.2 bouyer int s;
281 1.26.2.2 bouyer
282 1.26.2.2 bouyer if (sc->cue_dying)
283 1.26.2.2 bouyer return (0);
284 1.26.2.2 bouyer
285 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
286 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, aval));
287 1.26.2.2 bouyer
288 1.26.2.2 bouyer USETW(val, aval);
289 1.26.2.2 bouyer req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
290 1.26.2.2 bouyer req.bRequest = CUE_CMD_WRITEREG;
291 1.26.2.2 bouyer USETW(req.wValue, val);
292 1.26.2.2 bouyer USETW(req.wIndex, reg);
293 1.26.2.2 bouyer USETW(req.wLength, 0);
294 1.26.2.2 bouyer
295 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, NULL);
296 1.26.2.2 bouyer
297 1.26.2.2 bouyer if (err) {
298 1.26.2.2 bouyer DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
299 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
300 1.26.2.2 bouyer return (-1);
301 1.26.2.2 bouyer }
302 1.26.2.2 bouyer
303 1.26.2.2 bouyer return (0);
304 1.26.2.2 bouyer }
305 1.26.2.2 bouyer #endif
306 1.26.2.2 bouyer
307 1.26.2.2 bouyer Static int
308 1.26.2.2 bouyer cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
309 1.26.2.2 bouyer {
310 1.26.2.2 bouyer usb_device_request_t req;
311 1.26.2.2 bouyer usbd_status err;
312 1.26.2.2 bouyer
313 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
314 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), cmd, addr, len));
315 1.26.2.2 bouyer
316 1.26.2.2 bouyer if (cmd == CUE_CMD_READSRAM)
317 1.26.2.2 bouyer req.bmRequestType = UT_READ_VENDOR_DEVICE;
318 1.26.2.2 bouyer else
319 1.26.2.2 bouyer req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
320 1.26.2.2 bouyer req.bRequest = cmd;
321 1.26.2.2 bouyer USETW(req.wValue, 0);
322 1.26.2.2 bouyer USETW(req.wIndex, addr);
323 1.26.2.2 bouyer USETW(req.wLength, len);
324 1.26.2.2 bouyer
325 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, buf);
326 1.26.2.2 bouyer
327 1.26.2.2 bouyer if (err) {
328 1.26.2.2 bouyer DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
329 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), addr, usbd_errstr(err)));
330 1.26.2.2 bouyer return (-1);
331 1.26.2.2 bouyer }
332 1.26.2.2 bouyer
333 1.26.2.2 bouyer return (0);
334 1.26.2.2 bouyer }
335 1.26.2.2 bouyer
336 1.26.2.2 bouyer Static int
337 1.26.2.2 bouyer cue_getmac(struct cue_softc *sc, void *buf)
338 1.26.2.2 bouyer {
339 1.26.2.2 bouyer usb_device_request_t req;
340 1.26.2.2 bouyer usbd_status err;
341 1.26.2.2 bouyer
342 1.26.2.2 bouyer DPRINTFN(10,("%s: cue_getmac\n", USBDEVNAME(sc->cue_dev)));
343 1.26.2.2 bouyer
344 1.26.2.2 bouyer req.bmRequestType = UT_READ_VENDOR_DEVICE;
345 1.26.2.2 bouyer req.bRequest = CUE_CMD_GET_MACADDR;
346 1.26.2.2 bouyer USETW(req.wValue, 0);
347 1.26.2.2 bouyer USETW(req.wIndex, 0);
348 1.26.2.2 bouyer USETW(req.wLength, ETHER_ADDR_LEN);
349 1.26.2.2 bouyer
350 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, buf);
351 1.26.2.2 bouyer
352 1.26.2.2 bouyer if (err) {
353 1.26.2.5 bouyer printf("%s: read MAC address failed\n",USBDEVNAME(sc->cue_dev));
354 1.26.2.2 bouyer return (-1);
355 1.26.2.2 bouyer }
356 1.26.2.2 bouyer
357 1.26.2.2 bouyer return (0);
358 1.26.2.2 bouyer }
359 1.26.2.2 bouyer
360 1.26.2.2 bouyer #define CUE_POLY 0xEDB88320
361 1.26.2.2 bouyer #define CUE_BITS 9
362 1.26.2.2 bouyer
363 1.26.2.2 bouyer Static u_int32_t
364 1.26.2.2 bouyer cue_crc(caddr_t addr)
365 1.26.2.2 bouyer {
366 1.26.2.2 bouyer u_int32_t idx, bit, data, crc;
367 1.26.2.2 bouyer
368 1.26.2.2 bouyer /* Compute CRC for the address value. */
369 1.26.2.2 bouyer crc = 0xFFFFFFFF; /* initial value */
370 1.26.2.2 bouyer
371 1.26.2.2 bouyer for (idx = 0; idx < 6; idx++) {
372 1.26.2.2 bouyer for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
373 1.26.2.2 bouyer crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
374 1.26.2.2 bouyer }
375 1.26.2.2 bouyer
376 1.26.2.2 bouyer return (crc & ((1 << CUE_BITS) - 1));
377 1.26.2.2 bouyer }
378 1.26.2.2 bouyer
379 1.26.2.2 bouyer Static void
380 1.26.2.2 bouyer cue_setmulti(struct cue_softc *sc)
381 1.26.2.2 bouyer {
382 1.26.2.2 bouyer struct ifnet *ifp;
383 1.26.2.2 bouyer struct ether_multi *enm;
384 1.26.2.2 bouyer struct ether_multistep step;
385 1.26.2.2 bouyer u_int32_t h, i;
386 1.26.2.2 bouyer
387 1.26.2.2 bouyer ifp = GET_IFP(sc);
388 1.26.2.2 bouyer
389 1.26.2.2 bouyer DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
390 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), ifp->if_flags));
391 1.26.2.2 bouyer
392 1.26.2.5 bouyer if (ifp->if_flags & IFF_PROMISC) {
393 1.26.2.5 bouyer allmulti:
394 1.26.2.5 bouyer ifp->if_flags |= IFF_ALLMULTI;
395 1.26.2.2 bouyer for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
396 1.26.2.2 bouyer sc->cue_mctab[i] = 0xFF;
397 1.26.2.2 bouyer cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
398 1.26.2.2 bouyer &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
399 1.26.2.2 bouyer return;
400 1.26.2.2 bouyer }
401 1.26.2.2 bouyer
402 1.26.2.2 bouyer /* first, zot all the existing hash bits */
403 1.26.2.2 bouyer for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
404 1.26.2.2 bouyer sc->cue_mctab[i] = 0;
405 1.26.2.2 bouyer
406 1.26.2.2 bouyer /* now program new ones */
407 1.26.2.2 bouyer #if defined(__NetBSD__)
408 1.26.2.2 bouyer ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
409 1.26.2.2 bouyer #else
410 1.26.2.2 bouyer ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
411 1.26.2.2 bouyer #endif
412 1.26.2.2 bouyer while (enm != NULL) {
413 1.26.2.2 bouyer if (memcmp(enm->enm_addrlo,
414 1.26.2.5 bouyer enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
415 1.26.2.5 bouyer goto allmulti;
416 1.26.2.5 bouyer
417 1.26.2.2 bouyer h = cue_crc(enm->enm_addrlo);
418 1.26.2.2 bouyer sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
419 1.26.2.2 bouyer ETHER_NEXT_MULTI(step, enm);
420 1.26.2.2 bouyer }
421 1.26.2.5 bouyer
422 1.26.2.5 bouyer ifp->if_flags &= ~IFF_ALLMULTI;
423 1.26.2.2 bouyer
424 1.26.2.2 bouyer /*
425 1.26.2.2 bouyer * Also include the broadcast address in the filter
426 1.26.2.2 bouyer * so we can receive broadcast frames.
427 1.26.2.2 bouyer */
428 1.26.2.2 bouyer if (ifp->if_flags & IFF_BROADCAST) {
429 1.26.2.2 bouyer h = cue_crc(etherbroadcastaddr);
430 1.26.2.2 bouyer sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
431 1.26.2.2 bouyer }
432 1.26.2.2 bouyer
433 1.26.2.2 bouyer cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
434 1.26.2.2 bouyer &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
435 1.26.2.2 bouyer }
436 1.26.2.2 bouyer
437 1.26.2.2 bouyer Static void
438 1.26.2.2 bouyer cue_reset(struct cue_softc *sc)
439 1.26.2.2 bouyer {
440 1.26.2.2 bouyer usb_device_request_t req;
441 1.26.2.2 bouyer usbd_status err;
442 1.26.2.2 bouyer
443 1.26.2.2 bouyer DPRINTFN(2,("%s: cue_reset\n", USBDEVNAME(sc->cue_dev)));
444 1.26.2.2 bouyer
445 1.26.2.2 bouyer if (sc->cue_dying)
446 1.26.2.2 bouyer return;
447 1.26.2.2 bouyer
448 1.26.2.2 bouyer req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
449 1.26.2.2 bouyer req.bRequest = CUE_CMD_RESET;
450 1.26.2.2 bouyer USETW(req.wValue, 0);
451 1.26.2.2 bouyer USETW(req.wIndex, 0);
452 1.26.2.2 bouyer USETW(req.wLength, 0);
453 1.26.2.2 bouyer
454 1.26.2.5 bouyer err = usbd_do_request(sc->cue_udev, &req, NULL);
455 1.26.2.2 bouyer
456 1.26.2.2 bouyer if (err)
457 1.26.2.2 bouyer printf("%s: reset failed\n", USBDEVNAME(sc->cue_dev));
458 1.26.2.2 bouyer
459 1.26.2.2 bouyer /* Wait a little while for the chip to get its brains in order. */
460 1.26.2.5 bouyer usbd_delay_ms(sc->cue_udev, 1);
461 1.26.2.2 bouyer }
462 1.26.2.2 bouyer
463 1.26.2.2 bouyer /*
464 1.26.2.2 bouyer * Probe for a CATC chip.
465 1.26.2.2 bouyer */
466 1.26.2.2 bouyer USB_MATCH(cue)
467 1.26.2.2 bouyer {
468 1.26.2.2 bouyer USB_MATCH_START(cue, uaa);
469 1.26.2.2 bouyer struct cue_type *t;
470 1.26.2.2 bouyer
471 1.26.2.2 bouyer if (uaa->iface != NULL)
472 1.26.2.2 bouyer return (UMATCH_NONE);
473 1.26.2.2 bouyer
474 1.26.2.2 bouyer for (t = cue_devs; t->cue_vid != 0; t++)
475 1.26.2.2 bouyer if (uaa->vendor == t->cue_vid && uaa->product == t->cue_did)
476 1.26.2.2 bouyer return (UMATCH_VENDOR_PRODUCT);
477 1.26.2.2 bouyer
478 1.26.2.2 bouyer return (UMATCH_NONE);
479 1.26.2.2 bouyer }
480 1.26.2.2 bouyer
481 1.26.2.2 bouyer /*
482 1.26.2.2 bouyer * Attach the interface. Allocate softc structures, do ifmedia
483 1.26.2.2 bouyer * setup and ethernet/BPF attach.
484 1.26.2.2 bouyer */
485 1.26.2.2 bouyer USB_ATTACH(cue)
486 1.26.2.2 bouyer {
487 1.26.2.2 bouyer USB_ATTACH_START(cue, sc, uaa);
488 1.26.2.2 bouyer char devinfo[1024];
489 1.26.2.2 bouyer int s;
490 1.26.2.2 bouyer u_char eaddr[ETHER_ADDR_LEN];
491 1.26.2.2 bouyer usbd_device_handle dev = uaa->device;
492 1.26.2.2 bouyer usbd_interface_handle iface;
493 1.26.2.2 bouyer usbd_status err;
494 1.26.2.2 bouyer struct ifnet *ifp;
495 1.26.2.2 bouyer usb_interface_descriptor_t *id;
496 1.26.2.2 bouyer usb_endpoint_descriptor_t *ed;
497 1.26.2.2 bouyer int i;
498 1.26.2.2 bouyer
499 1.26.2.2 bouyer DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
500 1.26.2.2 bouyer
501 1.26.2.2 bouyer usbd_devinfo(dev, 0, devinfo);
502 1.26.2.2 bouyer USB_ATTACH_SETUP;
503 1.26.2.2 bouyer printf("%s: %s\n", USBDEVNAME(sc->cue_dev), devinfo);
504 1.26.2.2 bouyer
505 1.26.2.2 bouyer err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
506 1.26.2.2 bouyer if (err) {
507 1.26.2.2 bouyer printf("%s: setting config no failed\n",
508 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev));
509 1.26.2.2 bouyer USB_ATTACH_ERROR_RETURN;
510 1.26.2.2 bouyer }
511 1.26.2.2 bouyer
512 1.26.2.2 bouyer sc->cue_udev = dev;
513 1.26.2.2 bouyer sc->cue_product = uaa->product;
514 1.26.2.2 bouyer sc->cue_vendor = uaa->vendor;
515 1.26.2.2 bouyer
516 1.26.2.5 bouyer usb_init_task(&sc->cue_tick_task, cue_tick_task, sc);
517 1.26.2.5 bouyer usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc);
518 1.26.2.5 bouyer
519 1.26.2.2 bouyer err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
520 1.26.2.2 bouyer if (err) {
521 1.26.2.2 bouyer printf("%s: getting interface handle failed\n",
522 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev));
523 1.26.2.2 bouyer USB_ATTACH_ERROR_RETURN;
524 1.26.2.2 bouyer }
525 1.26.2.2 bouyer
526 1.26.2.2 bouyer sc->cue_iface = iface;
527 1.26.2.2 bouyer id = usbd_get_interface_descriptor(iface);
528 1.26.2.2 bouyer
529 1.26.2.2 bouyer /* Find endpoints. */
530 1.26.2.2 bouyer for (i = 0; i < id->bNumEndpoints; i++) {
531 1.26.2.2 bouyer ed = usbd_interface2endpoint_descriptor(iface, i);
532 1.26.2.2 bouyer if (ed == NULL) {
533 1.26.2.2 bouyer printf("%s: couldn't get ep %d\n",
534 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), i);
535 1.26.2.2 bouyer USB_ATTACH_ERROR_RETURN;
536 1.26.2.2 bouyer }
537 1.26.2.2 bouyer if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
538 1.26.2.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
539 1.26.2.2 bouyer sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
540 1.26.2.2 bouyer } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
541 1.26.2.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
542 1.26.2.2 bouyer sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
543 1.26.2.2 bouyer } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
544 1.26.2.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
545 1.26.2.2 bouyer sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
546 1.26.2.2 bouyer }
547 1.26.2.2 bouyer }
548 1.26.2.2 bouyer
549 1.26.2.2 bouyer #if 0
550 1.26.2.2 bouyer /* Reset the adapter. */
551 1.26.2.2 bouyer cue_reset(sc);
552 1.26.2.2 bouyer #endif
553 1.26.2.2 bouyer /*
554 1.26.2.2 bouyer * Get station address.
555 1.26.2.2 bouyer */
556 1.26.2.2 bouyer cue_getmac(sc, &eaddr);
557 1.26.2.2 bouyer
558 1.26.2.2 bouyer s = splimp();
559 1.26.2.2 bouyer
560 1.26.2.2 bouyer /*
561 1.26.2.2 bouyer * A CATC chip was detected. Inform the world.
562 1.26.2.2 bouyer */
563 1.26.2.2 bouyer printf("%s: Ethernet address %s\n", USBDEVNAME(sc->cue_dev),
564 1.26.2.2 bouyer ether_sprintf(eaddr));
565 1.26.2.2 bouyer
566 1.26.2.2 bouyer /* Initialize interface info.*/
567 1.26.2.2 bouyer ifp = GET_IFP(sc);
568 1.26.2.2 bouyer ifp->if_softc = sc;
569 1.26.2.2 bouyer ifp->if_mtu = ETHERMTU;
570 1.26.2.2 bouyer ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
571 1.26.2.2 bouyer ifp->if_ioctl = cue_ioctl;
572 1.26.2.2 bouyer ifp->if_start = cue_start;
573 1.26.2.2 bouyer ifp->if_watchdog = cue_watchdog;
574 1.26.2.2 bouyer #if defined(__OpenBSD__)
575 1.26.2.2 bouyer ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
576 1.26.2.2 bouyer #endif
577 1.26.2.2 bouyer strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ);
578 1.26.2.2 bouyer
579 1.26.2.4 bouyer IFQ_SET_READY(&ifp->if_snd);
580 1.26.2.4 bouyer
581 1.26.2.2 bouyer /* Attach the interface. */
582 1.26.2.2 bouyer if_attach(ifp);
583 1.26.2.2 bouyer Ether_ifattach(ifp, eaddr);
584 1.26.2.2 bouyer #if NRND > 0
585 1.26.2.2 bouyer rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
586 1.26.2.2 bouyer RND_TYPE_NET, 0);
587 1.26.2.2 bouyer #endif
588 1.26.2.2 bouyer
589 1.26.2.2 bouyer usb_callout_init(sc->cue_stat_ch);
590 1.26.2.2 bouyer
591 1.26.2.2 bouyer sc->cue_attached = 1;
592 1.26.2.2 bouyer splx(s);
593 1.26.2.2 bouyer
594 1.26.2.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
595 1.26.2.2 bouyer USBDEV(sc->cue_dev));
596 1.26.2.2 bouyer
597 1.26.2.2 bouyer USB_ATTACH_SUCCESS_RETURN;
598 1.26.2.2 bouyer }
599 1.26.2.2 bouyer
600 1.26.2.2 bouyer USB_DETACH(cue)
601 1.26.2.2 bouyer {
602 1.26.2.2 bouyer USB_DETACH_START(cue, sc);
603 1.26.2.2 bouyer struct ifnet *ifp = GET_IFP(sc);
604 1.26.2.2 bouyer int s;
605 1.26.2.2 bouyer
606 1.26.2.2 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
607 1.26.2.2 bouyer
608 1.26.2.2 bouyer usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
609 1.26.2.5 bouyer /*
610 1.26.2.5 bouyer * Remove any pending task. It cannot be executing because it run
611 1.26.2.5 bouyer * in the same thread as detach.
612 1.26.2.5 bouyer */
613 1.26.2.5 bouyer usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
614 1.26.2.2 bouyer
615 1.26.2.2 bouyer if (!sc->cue_attached) {
616 1.26.2.2 bouyer /* Detached before attached finished, so just bail out. */
617 1.26.2.2 bouyer return (0);
618 1.26.2.2 bouyer }
619 1.26.2.2 bouyer
620 1.26.2.5 bouyer s = splusb();
621 1.26.2.5 bouyer
622 1.26.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
623 1.26.2.2 bouyer cue_stop(sc);
624 1.26.2.2 bouyer
625 1.26.2.2 bouyer #if defined(__NetBSD__)
626 1.26.2.2 bouyer #if NRND > 0
627 1.26.2.2 bouyer rnd_detach_source(&sc->rnd_source);
628 1.26.2.2 bouyer #endif
629 1.26.2.2 bouyer ether_ifdetach(ifp);
630 1.26.2.2 bouyer #endif /* __NetBSD__ */
631 1.26.2.2 bouyer
632 1.26.2.2 bouyer if_detach(ifp);
633 1.26.2.2 bouyer
634 1.26.2.2 bouyer #ifdef DIAGNOSTIC
635 1.26.2.2 bouyer if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
636 1.26.2.2 bouyer sc->cue_ep[CUE_ENDPT_RX] != NULL ||
637 1.26.2.2 bouyer sc->cue_ep[CUE_ENDPT_INTR] != NULL)
638 1.26.2.2 bouyer printf("%s: detach has active endpoints\n",
639 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev));
640 1.26.2.2 bouyer #endif
641 1.26.2.2 bouyer
642 1.26.2.2 bouyer sc->cue_attached = 0;
643 1.26.2.2 bouyer splx(s);
644 1.26.2.2 bouyer
645 1.26.2.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
646 1.26.2.2 bouyer USBDEV(sc->cue_dev));
647 1.26.2.2 bouyer
648 1.26.2.2 bouyer return (0);
649 1.26.2.2 bouyer }
650 1.26.2.2 bouyer
651 1.26.2.2 bouyer int
652 1.26.2.2 bouyer cue_activate(device_ptr_t self, enum devact act)
653 1.26.2.2 bouyer {
654 1.26.2.2 bouyer struct cue_softc *sc = (struct cue_softc *)self;
655 1.26.2.2 bouyer
656 1.26.2.2 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
657 1.26.2.2 bouyer
658 1.26.2.2 bouyer switch (act) {
659 1.26.2.2 bouyer case DVACT_ACTIVATE:
660 1.26.2.2 bouyer return (EOPNOTSUPP);
661 1.26.2.2 bouyer break;
662 1.26.2.2 bouyer
663 1.26.2.2 bouyer case DVACT_DEACTIVATE:
664 1.26.2.2 bouyer /* Deactivate the interface. */
665 1.26.2.2 bouyer if_deactivate(&sc->cue_ec.ec_if);
666 1.26.2.2 bouyer sc->cue_dying = 1;
667 1.26.2.2 bouyer break;
668 1.26.2.2 bouyer }
669 1.26.2.2 bouyer return (0);
670 1.26.2.2 bouyer }
671 1.26.2.2 bouyer
672 1.26.2.2 bouyer /*
673 1.26.2.2 bouyer * Initialize an RX descriptor and attach an MBUF cluster.
674 1.26.2.2 bouyer */
675 1.26.2.2 bouyer Static int
676 1.26.2.2 bouyer cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
677 1.26.2.2 bouyer {
678 1.26.2.2 bouyer struct mbuf *m_new = NULL;
679 1.26.2.2 bouyer
680 1.26.2.2 bouyer if (m == NULL) {
681 1.26.2.2 bouyer MGETHDR(m_new, M_DONTWAIT, MT_DATA);
682 1.26.2.2 bouyer if (m_new == NULL) {
683 1.26.2.2 bouyer printf("%s: no memory for rx list "
684 1.26.2.2 bouyer "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
685 1.26.2.2 bouyer return (ENOBUFS);
686 1.26.2.2 bouyer }
687 1.26.2.2 bouyer
688 1.26.2.2 bouyer MCLGET(m_new, M_DONTWAIT);
689 1.26.2.2 bouyer if (!(m_new->m_flags & M_EXT)) {
690 1.26.2.2 bouyer printf("%s: no memory for rx list "
691 1.26.2.2 bouyer "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
692 1.26.2.2 bouyer m_freem(m_new);
693 1.26.2.2 bouyer return (ENOBUFS);
694 1.26.2.2 bouyer }
695 1.26.2.2 bouyer m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
696 1.26.2.2 bouyer } else {
697 1.26.2.2 bouyer m_new = m;
698 1.26.2.2 bouyer m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
699 1.26.2.2 bouyer m_new->m_data = m_new->m_ext.ext_buf;
700 1.26.2.2 bouyer }
701 1.26.2.2 bouyer
702 1.26.2.2 bouyer m_adj(m_new, ETHER_ALIGN);
703 1.26.2.2 bouyer c->cue_mbuf = m_new;
704 1.26.2.2 bouyer
705 1.26.2.2 bouyer return (0);
706 1.26.2.2 bouyer }
707 1.26.2.2 bouyer
708 1.26.2.2 bouyer Static int
709 1.26.2.2 bouyer cue_rx_list_init(struct cue_softc *sc)
710 1.26.2.2 bouyer {
711 1.26.2.2 bouyer struct cue_cdata *cd;
712 1.26.2.2 bouyer struct cue_chain *c;
713 1.26.2.2 bouyer int i;
714 1.26.2.2 bouyer
715 1.26.2.2 bouyer cd = &sc->cue_cdata;
716 1.26.2.2 bouyer for (i = 0; i < CUE_RX_LIST_CNT; i++) {
717 1.26.2.2 bouyer c = &cd->cue_rx_chain[i];
718 1.26.2.2 bouyer c->cue_sc = sc;
719 1.26.2.2 bouyer c->cue_idx = i;
720 1.26.2.2 bouyer if (cue_newbuf(sc, c, NULL) == ENOBUFS)
721 1.26.2.2 bouyer return (ENOBUFS);
722 1.26.2.2 bouyer if (c->cue_xfer == NULL) {
723 1.26.2.2 bouyer c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
724 1.26.2.2 bouyer if (c->cue_xfer == NULL)
725 1.26.2.2 bouyer return (ENOBUFS);
726 1.26.2.2 bouyer c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
727 1.26.2.2 bouyer if (c->cue_buf == NULL) {
728 1.26.2.2 bouyer usbd_free_xfer(c->cue_xfer);
729 1.26.2.2 bouyer return (ENOBUFS);
730 1.26.2.2 bouyer }
731 1.26.2.2 bouyer }
732 1.26.2.2 bouyer }
733 1.26.2.2 bouyer
734 1.26.2.2 bouyer return (0);
735 1.26.2.2 bouyer }
736 1.26.2.2 bouyer
737 1.26.2.2 bouyer Static int
738 1.26.2.2 bouyer cue_tx_list_init(struct cue_softc *sc)
739 1.26.2.2 bouyer {
740 1.26.2.2 bouyer struct cue_cdata *cd;
741 1.26.2.2 bouyer struct cue_chain *c;
742 1.26.2.2 bouyer int i;
743 1.26.2.2 bouyer
744 1.26.2.2 bouyer cd = &sc->cue_cdata;
745 1.26.2.2 bouyer for (i = 0; i < CUE_TX_LIST_CNT; i++) {
746 1.26.2.2 bouyer c = &cd->cue_tx_chain[i];
747 1.26.2.2 bouyer c->cue_sc = sc;
748 1.26.2.2 bouyer c->cue_idx = i;
749 1.26.2.2 bouyer c->cue_mbuf = NULL;
750 1.26.2.2 bouyer if (c->cue_xfer == NULL) {
751 1.26.2.2 bouyer c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
752 1.26.2.2 bouyer if (c->cue_xfer == NULL)
753 1.26.2.2 bouyer return (ENOBUFS);
754 1.26.2.2 bouyer c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
755 1.26.2.2 bouyer if (c->cue_buf == NULL) {
756 1.26.2.2 bouyer usbd_free_xfer(c->cue_xfer);
757 1.26.2.2 bouyer return (ENOBUFS);
758 1.26.2.2 bouyer }
759 1.26.2.2 bouyer }
760 1.26.2.2 bouyer }
761 1.26.2.2 bouyer
762 1.26.2.2 bouyer return (0);
763 1.26.2.2 bouyer }
764 1.26.2.2 bouyer
765 1.26.2.2 bouyer /*
766 1.26.2.2 bouyer * A frame has been uploaded: pass the resulting mbuf chain up to
767 1.26.2.2 bouyer * the higher level protocols.
768 1.26.2.2 bouyer */
769 1.26.2.2 bouyer Static void
770 1.26.2.2 bouyer cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
771 1.26.2.2 bouyer {
772 1.26.2.2 bouyer struct cue_chain *c = priv;
773 1.26.2.2 bouyer struct cue_softc *sc = c->cue_sc;
774 1.26.2.2 bouyer struct ifnet *ifp = GET_IFP(sc);
775 1.26.2.2 bouyer struct mbuf *m;
776 1.26.2.2 bouyer int total_len = 0;
777 1.26.2.2 bouyer u_int16_t len;
778 1.26.2.2 bouyer int s;
779 1.26.2.2 bouyer
780 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
781 1.26.2.2 bouyer __FUNCTION__, status));
782 1.26.2.2 bouyer
783 1.26.2.2 bouyer if (sc->cue_dying)
784 1.26.2.2 bouyer return;
785 1.26.2.2 bouyer
786 1.26.2.2 bouyer if (!(ifp->if_flags & IFF_RUNNING))
787 1.26.2.2 bouyer return;
788 1.26.2.2 bouyer
789 1.26.2.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
790 1.26.2.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
791 1.26.2.2 bouyer return;
792 1.26.2.2 bouyer sc->cue_rx_errs++;
793 1.26.2.2 bouyer if (usbd_ratecheck(&sc->cue_rx_notice)) {
794 1.26.2.2 bouyer printf("%s: %u usb errors on rx: %s\n",
795 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
796 1.26.2.2 bouyer usbd_errstr(status));
797 1.26.2.2 bouyer sc->cue_rx_errs = 0;
798 1.26.2.2 bouyer }
799 1.26.2.2 bouyer if (status == USBD_STALLED)
800 1.26.2.2 bouyer usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
801 1.26.2.2 bouyer goto done;
802 1.26.2.2 bouyer }
803 1.26.2.2 bouyer
804 1.26.2.2 bouyer usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
805 1.26.2.2 bouyer
806 1.26.2.2 bouyer memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
807 1.26.2.2 bouyer
808 1.26.2.2 bouyer m = c->cue_mbuf;
809 1.26.2.2 bouyer len = UGETW(mtod(m, u_int8_t *));
810 1.26.2.2 bouyer
811 1.26.2.2 bouyer /* No errors; receive the packet. */
812 1.26.2.2 bouyer total_len = len;
813 1.26.2.2 bouyer
814 1.26.2.2 bouyer if (len < sizeof(struct ether_header)) {
815 1.26.2.2 bouyer ifp->if_ierrors++;
816 1.26.2.2 bouyer goto done;
817 1.26.2.2 bouyer }
818 1.26.2.2 bouyer
819 1.26.2.2 bouyer ifp->if_ipackets++;
820 1.26.2.2 bouyer m_adj(m, sizeof(u_int16_t));
821 1.26.2.2 bouyer m->m_pkthdr.len = m->m_len = total_len;
822 1.26.2.2 bouyer
823 1.26.2.2 bouyer m->m_pkthdr.rcvif = ifp;
824 1.26.2.2 bouyer
825 1.26.2.2 bouyer s = splimp();
826 1.26.2.2 bouyer
827 1.26.2.2 bouyer /* XXX ugly */
828 1.26.2.2 bouyer if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
829 1.26.2.2 bouyer ifp->if_ierrors++;
830 1.26.2.2 bouyer goto done1;
831 1.26.2.2 bouyer }
832 1.26.2.2 bouyer
833 1.26.2.2 bouyer #if NBPFILTER > 0
834 1.26.2.2 bouyer /*
835 1.26.2.2 bouyer * Handle BPF listeners. Let the BPF user see the packet, but
836 1.26.2.2 bouyer * don't pass it up to the ether_input() layer unless it's
837 1.26.2.2 bouyer * a broadcast packet, multicast packet, matches our ethernet
838 1.26.2.2 bouyer * address or the interface is in promiscuous mode.
839 1.26.2.2 bouyer */
840 1.26.2.2 bouyer if (ifp->if_bpf)
841 1.26.2.2 bouyer BPF_MTAP(ifp, m);
842 1.26.2.2 bouyer #endif
843 1.26.2.2 bouyer
844 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
845 1.26.2.2 bouyer __FUNCTION__, m->m_len));
846 1.26.2.2 bouyer IF_INPUT(ifp, m);
847 1.26.2.2 bouyer done1:
848 1.26.2.2 bouyer splx(s);
849 1.26.2.2 bouyer
850 1.26.2.2 bouyer done:
851 1.26.2.2 bouyer /* Setup new transfer. */
852 1.26.2.2 bouyer usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
853 1.26.2.2 bouyer c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
854 1.26.2.2 bouyer USBD_NO_TIMEOUT, cue_rxeof);
855 1.26.2.2 bouyer usbd_transfer(c->cue_xfer);
856 1.26.2.2 bouyer
857 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
858 1.26.2.2 bouyer __FUNCTION__));
859 1.26.2.2 bouyer }
860 1.26.2.2 bouyer
861 1.26.2.2 bouyer /*
862 1.26.2.2 bouyer * A frame was downloaded to the chip. It's safe for us to clean up
863 1.26.2.2 bouyer * the list buffers.
864 1.26.2.2 bouyer */
865 1.26.2.2 bouyer Static void
866 1.26.2.2 bouyer cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
867 1.26.2.2 bouyer {
868 1.26.2.2 bouyer struct cue_chain *c = priv;
869 1.26.2.2 bouyer struct cue_softc *sc = c->cue_sc;
870 1.26.2.2 bouyer struct ifnet *ifp = GET_IFP(sc);
871 1.26.2.2 bouyer int s;
872 1.26.2.2 bouyer
873 1.26.2.2 bouyer if (sc->cue_dying)
874 1.26.2.2 bouyer return;
875 1.26.2.2 bouyer
876 1.26.2.2 bouyer s = splimp();
877 1.26.2.2 bouyer
878 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
879 1.26.2.2 bouyer __FUNCTION__, status));
880 1.26.2.2 bouyer
881 1.26.2.2 bouyer ifp->if_timer = 0;
882 1.26.2.2 bouyer ifp->if_flags &= ~IFF_OACTIVE;
883 1.26.2.2 bouyer
884 1.26.2.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
885 1.26.2.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
886 1.26.2.2 bouyer splx(s);
887 1.26.2.2 bouyer return;
888 1.26.2.2 bouyer }
889 1.26.2.2 bouyer ifp->if_oerrors++;
890 1.26.2.2 bouyer printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
891 1.26.2.2 bouyer usbd_errstr(status));
892 1.26.2.2 bouyer if (status == USBD_STALLED)
893 1.26.2.2 bouyer usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
894 1.26.2.2 bouyer splx(s);
895 1.26.2.2 bouyer return;
896 1.26.2.2 bouyer }
897 1.26.2.2 bouyer
898 1.26.2.2 bouyer ifp->if_opackets++;
899 1.26.2.2 bouyer
900 1.26.2.2 bouyer m_freem(c->cue_mbuf);
901 1.26.2.2 bouyer c->cue_mbuf = NULL;
902 1.26.2.2 bouyer
903 1.26.2.4 bouyer if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
904 1.26.2.2 bouyer cue_start(ifp);
905 1.26.2.2 bouyer
906 1.26.2.2 bouyer splx(s);
907 1.26.2.2 bouyer }
908 1.26.2.2 bouyer
909 1.26.2.2 bouyer Static void
910 1.26.2.2 bouyer cue_tick(void *xsc)
911 1.26.2.2 bouyer {
912 1.26.2.2 bouyer struct cue_softc *sc = xsc;
913 1.26.2.2 bouyer
914 1.26.2.2 bouyer if (sc == NULL)
915 1.26.2.2 bouyer return;
916 1.26.2.2 bouyer
917 1.26.2.2 bouyer if (sc->cue_dying)
918 1.26.2.2 bouyer return;
919 1.26.2.2 bouyer
920 1.26.2.2 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
921 1.26.2.2 bouyer
922 1.26.2.5 bouyer /* Perform statistics update in process context. */
923 1.26.2.5 bouyer usb_add_task(sc->cue_udev, &sc->cue_tick_task);
924 1.26.2.5 bouyer }
925 1.26.2.5 bouyer
926 1.26.2.5 bouyer Static void
927 1.26.2.5 bouyer cue_tick_task(void *xsc)
928 1.26.2.5 bouyer {
929 1.26.2.5 bouyer struct cue_softc *sc = xsc;
930 1.26.2.5 bouyer struct ifnet *ifp;
931 1.26.2.5 bouyer
932 1.26.2.5 bouyer if (sc->cue_dying)
933 1.26.2.5 bouyer return;
934 1.26.2.5 bouyer
935 1.26.2.5 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __FUNCTION__));
936 1.26.2.2 bouyer
937 1.26.2.2 bouyer ifp = GET_IFP(sc);
938 1.26.2.2 bouyer
939 1.26.2.2 bouyer ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
940 1.26.2.2 bouyer ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
941 1.26.2.2 bouyer ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
942 1.26.2.2 bouyer
943 1.26.2.2 bouyer if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
944 1.26.2.2 bouyer ifp->if_ierrors++;
945 1.26.2.2 bouyer }
946 1.26.2.2 bouyer
947 1.26.2.2 bouyer Static int
948 1.26.2.2 bouyer cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
949 1.26.2.2 bouyer {
950 1.26.2.2 bouyer int total_len;
951 1.26.2.2 bouyer struct cue_chain *c;
952 1.26.2.2 bouyer usbd_status err;
953 1.26.2.2 bouyer
954 1.26.2.2 bouyer c = &sc->cue_cdata.cue_tx_chain[idx];
955 1.26.2.2 bouyer
956 1.26.2.2 bouyer /*
957 1.26.2.2 bouyer * Copy the mbuf data into a contiguous buffer, leaving two
958 1.26.2.2 bouyer * bytes at the beginning to hold the frame length.
959 1.26.2.2 bouyer */
960 1.26.2.2 bouyer m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
961 1.26.2.2 bouyer c->cue_mbuf = m;
962 1.26.2.2 bouyer
963 1.26.2.2 bouyer total_len = m->m_pkthdr.len + 2;
964 1.26.2.2 bouyer
965 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: total_len=%d\n",
966 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), __FUNCTION__, total_len));
967 1.26.2.2 bouyer
968 1.26.2.2 bouyer /* The first two bytes are the frame length */
969 1.26.2.2 bouyer c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
970 1.26.2.2 bouyer c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
971 1.26.2.2 bouyer
972 1.26.2.2 bouyer /* XXX 10000 */
973 1.26.2.2 bouyer usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
974 1.26.2.2 bouyer c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
975 1.26.2.2 bouyer
976 1.26.2.2 bouyer /* Transmit */
977 1.26.2.2 bouyer err = usbd_transfer(c->cue_xfer);
978 1.26.2.2 bouyer if (err != USBD_IN_PROGRESS) {
979 1.26.2.2 bouyer printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
980 1.26.2.2 bouyer usbd_errstr(err));
981 1.26.2.5 bouyer /* Stop the interface from process context. */
982 1.26.2.5 bouyer usb_add_task(sc->cue_udev, &sc->cue_stop_task);
983 1.26.2.2 bouyer return (EIO);
984 1.26.2.2 bouyer }
985 1.26.2.2 bouyer
986 1.26.2.2 bouyer sc->cue_cdata.cue_tx_cnt++;
987 1.26.2.2 bouyer
988 1.26.2.2 bouyer return (0);
989 1.26.2.2 bouyer }
990 1.26.2.2 bouyer
991 1.26.2.2 bouyer Static void
992 1.26.2.2 bouyer cue_start(struct ifnet *ifp)
993 1.26.2.2 bouyer {
994 1.26.2.2 bouyer struct cue_softc *sc = ifp->if_softc;
995 1.26.2.2 bouyer struct mbuf *m_head = NULL;
996 1.26.2.2 bouyer
997 1.26.2.2 bouyer if (sc->cue_dying)
998 1.26.2.2 bouyer return;
999 1.26.2.2 bouyer
1000 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1001 1.26.2.2 bouyer
1002 1.26.2.2 bouyer if (ifp->if_flags & IFF_OACTIVE)
1003 1.26.2.2 bouyer return;
1004 1.26.2.2 bouyer
1005 1.26.2.4 bouyer IFQ_POLL(&ifp->if_snd, m_head);
1006 1.26.2.2 bouyer if (m_head == NULL)
1007 1.26.2.2 bouyer return;
1008 1.26.2.2 bouyer
1009 1.26.2.2 bouyer if (cue_send(sc, m_head, 0)) {
1010 1.26.2.2 bouyer ifp->if_flags |= IFF_OACTIVE;
1011 1.26.2.2 bouyer return;
1012 1.26.2.2 bouyer }
1013 1.26.2.2 bouyer
1014 1.26.2.4 bouyer IFQ_DEQUEUE(&ifp->if_snd, m_head);
1015 1.26.2.4 bouyer
1016 1.26.2.2 bouyer #if NBPFILTER > 0
1017 1.26.2.2 bouyer /*
1018 1.26.2.2 bouyer * If there's a BPF listener, bounce a copy of this frame
1019 1.26.2.2 bouyer * to him.
1020 1.26.2.2 bouyer */
1021 1.26.2.2 bouyer if (ifp->if_bpf)
1022 1.26.2.2 bouyer BPF_MTAP(ifp, m_head);
1023 1.26.2.2 bouyer #endif
1024 1.26.2.2 bouyer
1025 1.26.2.2 bouyer ifp->if_flags |= IFF_OACTIVE;
1026 1.26.2.2 bouyer
1027 1.26.2.2 bouyer /*
1028 1.26.2.2 bouyer * Set a timeout in case the chip goes out to lunch.
1029 1.26.2.2 bouyer */
1030 1.26.2.2 bouyer ifp->if_timer = 5;
1031 1.26.2.2 bouyer }
1032 1.26.2.2 bouyer
1033 1.26.2.2 bouyer Static void
1034 1.26.2.2 bouyer cue_init(void *xsc)
1035 1.26.2.2 bouyer {
1036 1.26.2.2 bouyer struct cue_softc *sc = xsc;
1037 1.26.2.2 bouyer struct ifnet *ifp = GET_IFP(sc);
1038 1.26.2.2 bouyer int i, s, ctl;
1039 1.26.2.2 bouyer u_char *eaddr;
1040 1.26.2.2 bouyer
1041 1.26.2.2 bouyer if (sc->cue_dying)
1042 1.26.2.2 bouyer return;
1043 1.26.2.2 bouyer
1044 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1045 1.26.2.2 bouyer
1046 1.26.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
1047 1.26.2.2 bouyer return;
1048 1.26.2.2 bouyer
1049 1.26.2.2 bouyer s = splimp();
1050 1.26.2.2 bouyer
1051 1.26.2.2 bouyer /*
1052 1.26.2.2 bouyer * Cancel pending I/O and free all RX/TX buffers.
1053 1.26.2.2 bouyer */
1054 1.26.2.2 bouyer #if 1
1055 1.26.2.2 bouyer cue_reset(sc);
1056 1.26.2.2 bouyer #endif
1057 1.26.2.2 bouyer
1058 1.26.2.2 bouyer /* Set advanced operation modes. */
1059 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1060 1.26.2.2 bouyer CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1061 1.26.2.2 bouyer
1062 1.26.2.5 bouyer #if defined(__OpenBSD__)
1063 1.26.2.2 bouyer eaddr = sc->arpcom.ac_enaddr;
1064 1.26.2.2 bouyer #elif defined(__NetBSD__)
1065 1.26.2.2 bouyer eaddr = LLADDR(ifp->if_sadl);
1066 1.26.2.5 bouyer #endif
1067 1.26.2.2 bouyer /* Set MAC address */
1068 1.26.2.2 bouyer for (i = 0; i < ETHER_ADDR_LEN; i++)
1069 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1070 1.26.2.2 bouyer
1071 1.26.2.2 bouyer /* Enable RX logic. */
1072 1.26.2.2 bouyer ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1073 1.26.2.2 bouyer if (ifp->if_flags & IFF_PROMISC)
1074 1.26.2.2 bouyer ctl |= CUE_ETHCTL_PROMISC;
1075 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1076 1.26.2.2 bouyer
1077 1.26.2.2 bouyer /* Init TX ring. */
1078 1.26.2.2 bouyer if (cue_tx_list_init(sc) == ENOBUFS) {
1079 1.26.2.2 bouyer printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
1080 1.26.2.2 bouyer splx(s);
1081 1.26.2.2 bouyer return;
1082 1.26.2.2 bouyer }
1083 1.26.2.2 bouyer
1084 1.26.2.2 bouyer /* Init RX ring. */
1085 1.26.2.2 bouyer if (cue_rx_list_init(sc) == ENOBUFS) {
1086 1.26.2.2 bouyer printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
1087 1.26.2.2 bouyer splx(s);
1088 1.26.2.2 bouyer return;
1089 1.26.2.2 bouyer }
1090 1.26.2.2 bouyer
1091 1.26.2.2 bouyer /* Load the multicast filter. */
1092 1.26.2.2 bouyer cue_setmulti(sc);
1093 1.26.2.2 bouyer
1094 1.26.2.2 bouyer /*
1095 1.26.2.2 bouyer * Set the number of RX and TX buffers that we want
1096 1.26.2.2 bouyer * to reserve inside the ASIC.
1097 1.26.2.2 bouyer */
1098 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1099 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1100 1.26.2.2 bouyer
1101 1.26.2.2 bouyer /* Set advanced operation modes. */
1102 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1103 1.26.2.2 bouyer CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1104 1.26.2.2 bouyer
1105 1.26.2.2 bouyer /* Program the LED operation. */
1106 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1107 1.26.2.2 bouyer
1108 1.26.2.2 bouyer if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1109 1.26.2.2 bouyer if (cue_open_pipes(sc)) {
1110 1.26.2.2 bouyer splx(s);
1111 1.26.2.2 bouyer return;
1112 1.26.2.2 bouyer }
1113 1.26.2.2 bouyer }
1114 1.26.2.2 bouyer
1115 1.26.2.2 bouyer ifp->if_flags |= IFF_RUNNING;
1116 1.26.2.2 bouyer ifp->if_flags &= ~IFF_OACTIVE;
1117 1.26.2.2 bouyer
1118 1.26.2.2 bouyer splx(s);
1119 1.26.2.2 bouyer
1120 1.26.2.2 bouyer usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
1121 1.26.2.2 bouyer }
1122 1.26.2.2 bouyer
1123 1.26.2.2 bouyer Static int
1124 1.26.2.2 bouyer cue_open_pipes(struct cue_softc *sc)
1125 1.26.2.2 bouyer {
1126 1.26.2.2 bouyer struct cue_chain *c;
1127 1.26.2.2 bouyer usbd_status err;
1128 1.26.2.2 bouyer int i;
1129 1.26.2.2 bouyer
1130 1.26.2.2 bouyer /* Open RX and TX pipes. */
1131 1.26.2.2 bouyer err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1132 1.26.2.2 bouyer USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1133 1.26.2.2 bouyer if (err) {
1134 1.26.2.2 bouyer printf("%s: open rx pipe failed: %s\n",
1135 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1136 1.26.2.2 bouyer return (EIO);
1137 1.26.2.2 bouyer }
1138 1.26.2.2 bouyer err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1139 1.26.2.2 bouyer USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1140 1.26.2.2 bouyer if (err) {
1141 1.26.2.2 bouyer printf("%s: open tx pipe failed: %s\n",
1142 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1143 1.26.2.2 bouyer return (EIO);
1144 1.26.2.2 bouyer }
1145 1.26.2.2 bouyer
1146 1.26.2.2 bouyer /* Start up the receive pipe. */
1147 1.26.2.2 bouyer for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1148 1.26.2.2 bouyer c = &sc->cue_cdata.cue_rx_chain[i];
1149 1.26.2.2 bouyer usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1150 1.26.2.2 bouyer c, c->cue_buf, CUE_BUFSZ,
1151 1.26.2.2 bouyer USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1152 1.26.2.2 bouyer cue_rxeof);
1153 1.26.2.2 bouyer usbd_transfer(c->cue_xfer);
1154 1.26.2.2 bouyer }
1155 1.26.2.2 bouyer
1156 1.26.2.2 bouyer return (0);
1157 1.26.2.2 bouyer }
1158 1.26.2.2 bouyer
1159 1.26.2.2 bouyer Static int
1160 1.26.2.2 bouyer cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1161 1.26.2.2 bouyer {
1162 1.26.2.2 bouyer struct cue_softc *sc = ifp->if_softc;
1163 1.26.2.2 bouyer struct ifaddr *ifa = (struct ifaddr *)data;
1164 1.26.2.2 bouyer struct ifreq *ifr = (struct ifreq *)data;
1165 1.26.2.2 bouyer int s, error = 0;
1166 1.26.2.2 bouyer
1167 1.26.2.2 bouyer if (sc->cue_dying)
1168 1.26.2.2 bouyer return (EIO);
1169 1.26.2.2 bouyer
1170 1.26.2.2 bouyer s = splimp();
1171 1.26.2.2 bouyer
1172 1.26.2.2 bouyer switch(command) {
1173 1.26.2.2 bouyer case SIOCSIFADDR:
1174 1.26.2.2 bouyer ifp->if_flags |= IFF_UP;
1175 1.26.2.2 bouyer cue_init(sc);
1176 1.26.2.2 bouyer
1177 1.26.2.2 bouyer switch (ifa->ifa_addr->sa_family) {
1178 1.26.2.2 bouyer #ifdef INET
1179 1.26.2.2 bouyer case AF_INET:
1180 1.26.2.2 bouyer #if defined(__NetBSD__)
1181 1.26.2.2 bouyer arp_ifinit(ifp, ifa);
1182 1.26.2.2 bouyer #else
1183 1.26.2.2 bouyer arp_ifinit(&sc->arpcom, ifa);
1184 1.26.2.2 bouyer #endif
1185 1.26.2.2 bouyer break;
1186 1.26.2.2 bouyer #endif /* INET */
1187 1.26.2.2 bouyer #ifdef NS
1188 1.26.2.2 bouyer case AF_NS:
1189 1.26.2.2 bouyer {
1190 1.26.2.2 bouyer struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1191 1.26.2.2 bouyer
1192 1.26.2.2 bouyer if (ns_nullhost(*ina))
1193 1.26.2.2 bouyer ina->x_host = *(union ns_host *)
1194 1.26.2.2 bouyer LLADDR(ifp->if_sadl);
1195 1.26.2.2 bouyer else
1196 1.26.2.2 bouyer memcpy(LLADDR(ifp->if_sadl),
1197 1.26.2.2 bouyer ina->x_host.c_host,
1198 1.26.2.2 bouyer ifp->if_addrlen);
1199 1.26.2.2 bouyer break;
1200 1.26.2.2 bouyer }
1201 1.26.2.2 bouyer #endif /* NS */
1202 1.26.2.2 bouyer }
1203 1.26.2.2 bouyer break;
1204 1.26.2.2 bouyer
1205 1.26.2.2 bouyer case SIOCSIFMTU:
1206 1.26.2.2 bouyer if (ifr->ifr_mtu > ETHERMTU)
1207 1.26.2.2 bouyer error = EINVAL;
1208 1.26.2.2 bouyer else
1209 1.26.2.2 bouyer ifp->if_mtu = ifr->ifr_mtu;
1210 1.26.2.2 bouyer break;
1211 1.26.2.2 bouyer
1212 1.26.2.2 bouyer case SIOCSIFFLAGS:
1213 1.26.2.2 bouyer if (ifp->if_flags & IFF_UP) {
1214 1.26.2.2 bouyer if (ifp->if_flags & IFF_RUNNING &&
1215 1.26.2.2 bouyer ifp->if_flags & IFF_PROMISC &&
1216 1.26.2.2 bouyer !(sc->cue_if_flags & IFF_PROMISC)) {
1217 1.26.2.2 bouyer CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1218 1.26.2.2 bouyer cue_setmulti(sc);
1219 1.26.2.2 bouyer } else if (ifp->if_flags & IFF_RUNNING &&
1220 1.26.2.2 bouyer !(ifp->if_flags & IFF_PROMISC) &&
1221 1.26.2.2 bouyer sc->cue_if_flags & IFF_PROMISC) {
1222 1.26.2.2 bouyer CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1223 1.26.2.2 bouyer cue_setmulti(sc);
1224 1.26.2.2 bouyer } else if (!(ifp->if_flags & IFF_RUNNING))
1225 1.26.2.2 bouyer cue_init(sc);
1226 1.26.2.2 bouyer } else {
1227 1.26.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
1228 1.26.2.2 bouyer cue_stop(sc);
1229 1.26.2.2 bouyer }
1230 1.26.2.2 bouyer sc->cue_if_flags = ifp->if_flags;
1231 1.26.2.2 bouyer error = 0;
1232 1.26.2.2 bouyer break;
1233 1.26.2.2 bouyer case SIOCADDMULTI:
1234 1.26.2.2 bouyer case SIOCDELMULTI:
1235 1.26.2.2 bouyer cue_setmulti(sc);
1236 1.26.2.2 bouyer error = 0;
1237 1.26.2.2 bouyer break;
1238 1.26.2.2 bouyer default:
1239 1.26.2.2 bouyer error = EINVAL;
1240 1.26.2.2 bouyer break;
1241 1.26.2.2 bouyer }
1242 1.26.2.2 bouyer
1243 1.26.2.2 bouyer splx(s);
1244 1.26.2.2 bouyer
1245 1.26.2.2 bouyer return (error);
1246 1.26.2.2 bouyer }
1247 1.26.2.2 bouyer
1248 1.26.2.2 bouyer Static void
1249 1.26.2.2 bouyer cue_watchdog(struct ifnet *ifp)
1250 1.26.2.2 bouyer {
1251 1.26.2.2 bouyer struct cue_softc *sc = ifp->if_softc;
1252 1.26.2.5 bouyer struct cue_chain *c;
1253 1.26.2.5 bouyer usbd_status stat;
1254 1.26.2.5 bouyer int s;
1255 1.26.2.2 bouyer
1256 1.26.2.2 bouyer DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1257 1.26.2.2 bouyer
1258 1.26.2.2 bouyer if (sc->cue_dying)
1259 1.26.2.2 bouyer return;
1260 1.26.2.2 bouyer
1261 1.26.2.2 bouyer ifp->if_oerrors++;
1262 1.26.2.2 bouyer printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
1263 1.26.2.2 bouyer
1264 1.26.2.5 bouyer s = splusb();
1265 1.26.2.5 bouyer c = &sc->cue_cdata.cue_tx_chain[0];
1266 1.26.2.5 bouyer usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1267 1.26.2.5 bouyer cue_txeof(c->cue_xfer, c, stat);
1268 1.26.2.2 bouyer
1269 1.26.2.4 bouyer if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1270 1.26.2.2 bouyer cue_start(ifp);
1271 1.26.2.5 bouyer splx(s);
1272 1.26.2.2 bouyer }
1273 1.26.2.2 bouyer
1274 1.26.2.2 bouyer /*
1275 1.26.2.2 bouyer * Stop the adapter and free any mbufs allocated to the
1276 1.26.2.2 bouyer * RX and TX lists.
1277 1.26.2.2 bouyer */
1278 1.26.2.2 bouyer Static void
1279 1.26.2.2 bouyer cue_stop(struct cue_softc *sc)
1280 1.26.2.2 bouyer {
1281 1.26.2.2 bouyer usbd_status err;
1282 1.26.2.2 bouyer struct ifnet *ifp;
1283 1.26.2.2 bouyer int i;
1284 1.26.2.2 bouyer
1285 1.26.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__FUNCTION__));
1286 1.26.2.2 bouyer
1287 1.26.2.2 bouyer ifp = GET_IFP(sc);
1288 1.26.2.2 bouyer ifp->if_timer = 0;
1289 1.26.2.2 bouyer
1290 1.26.2.2 bouyer cue_csr_write_1(sc, CUE_ETHCTL, 0);
1291 1.26.2.2 bouyer cue_reset(sc);
1292 1.26.2.2 bouyer usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
1293 1.26.2.2 bouyer
1294 1.26.2.2 bouyer /* Stop transfers. */
1295 1.26.2.2 bouyer if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1296 1.26.2.2 bouyer err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1297 1.26.2.2 bouyer if (err) {
1298 1.26.2.2 bouyer printf("%s: abort rx pipe failed: %s\n",
1299 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1300 1.26.2.2 bouyer }
1301 1.26.2.2 bouyer err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1302 1.26.2.2 bouyer if (err) {
1303 1.26.2.2 bouyer printf("%s: close rx pipe failed: %s\n",
1304 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1305 1.26.2.2 bouyer }
1306 1.26.2.2 bouyer sc->cue_ep[CUE_ENDPT_RX] = NULL;
1307 1.26.2.2 bouyer }
1308 1.26.2.2 bouyer
1309 1.26.2.2 bouyer if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1310 1.26.2.2 bouyer err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1311 1.26.2.2 bouyer if (err) {
1312 1.26.2.2 bouyer printf("%s: abort tx pipe failed: %s\n",
1313 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1314 1.26.2.2 bouyer }
1315 1.26.2.2 bouyer err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1316 1.26.2.2 bouyer if (err) {
1317 1.26.2.2 bouyer printf("%s: close tx pipe failed: %s\n",
1318 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1319 1.26.2.2 bouyer }
1320 1.26.2.2 bouyer sc->cue_ep[CUE_ENDPT_TX] = NULL;
1321 1.26.2.2 bouyer }
1322 1.26.2.2 bouyer
1323 1.26.2.2 bouyer if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1324 1.26.2.2 bouyer err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1325 1.26.2.2 bouyer if (err) {
1326 1.26.2.2 bouyer printf("%s: abort intr pipe failed: %s\n",
1327 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1328 1.26.2.2 bouyer }
1329 1.26.2.2 bouyer err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1330 1.26.2.2 bouyer if (err) {
1331 1.26.2.2 bouyer printf("%s: close intr pipe failed: %s\n",
1332 1.26.2.2 bouyer USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1333 1.26.2.2 bouyer }
1334 1.26.2.2 bouyer sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1335 1.26.2.2 bouyer }
1336 1.26.2.2 bouyer
1337 1.26.2.2 bouyer /* Free RX resources. */
1338 1.26.2.2 bouyer for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1339 1.26.2.2 bouyer if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1340 1.26.2.2 bouyer m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1341 1.26.2.2 bouyer sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1342 1.26.2.2 bouyer }
1343 1.26.2.2 bouyer if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1344 1.26.2.2 bouyer usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1345 1.26.2.2 bouyer sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1346 1.26.2.2 bouyer }
1347 1.26.2.2 bouyer }
1348 1.26.2.2 bouyer
1349 1.26.2.2 bouyer /* Free TX resources. */
1350 1.26.2.2 bouyer for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1351 1.26.2.2 bouyer if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1352 1.26.2.2 bouyer m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1353 1.26.2.2 bouyer sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1354 1.26.2.2 bouyer }
1355 1.26.2.2 bouyer if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1356 1.26.2.2 bouyer usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1357 1.26.2.2 bouyer sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1358 1.26.2.2 bouyer }
1359 1.26.2.2 bouyer }
1360 1.26.2.2 bouyer
1361 1.26.2.2 bouyer ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1362 1.26.2.2 bouyer }
1363