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