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