2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.45 2003/12/08 07:54:14 obrien Exp $
33 * $DragonFly: src/sys/dev/netif/cue/if_cue.c,v 1.18 2005/02/21 18:40:36 joerg Exp $
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
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.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
58 #include <sys/malloc.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
63 #include <net/ifq_var.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/if_dl.h>
71 #include <machine/bus.h>
72 #if defined(__DragonFly__) || __FreeBSD_version < 500000
73 #include <machine/clock.h>
76 #include <bus/usb/usb.h>
77 #include <bus/usb/usbdi.h>
78 #include <bus/usb/usbdi_util.h>
79 #include <bus/usb/usbdivar.h>
80 #include <bus/usb/usbdevs.h>
81 #include <bus/usb/usb_ethersubr.h>
83 #include "if_cuereg.h"
86 * Various supported device vendors/products.
88 Static struct cue_type cue_devs[] = {
89 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
90 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
91 { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
95 Static int cue_match(device_ptr_t);
96 Static int cue_attach(device_ptr_t);
97 Static int cue_detach(device_ptr_t);
99 Static int cue_tx_list_init(struct cue_softc *);
100 Static int cue_rx_list_init(struct cue_softc *);
101 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
102 Static int cue_encap(struct cue_softc *, struct mbuf *, int);
103 Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
104 Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
105 Static void cue_tick(void *);
106 Static void cue_rxstart(struct ifnet *);
107 Static void cue_start(struct ifnet *);
108 Static int cue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
109 Static void cue_init(void *);
110 Static void cue_stop(struct cue_softc *);
111 Static void cue_watchdog(struct ifnet *);
112 Static void cue_shutdown(device_ptr_t);
114 Static void cue_setmulti(struct cue_softc *);
115 Static uint32_t cue_mchash(const uint8_t *);
116 Static void cue_reset(struct cue_softc *);
118 Static int cue_csr_read_1(struct cue_softc *, int);
119 Static int cue_csr_write_1(struct cue_softc *, int, int);
120 Static int cue_csr_read_2(struct cue_softc *, int);
122 Static int cue_csr_write_2(struct cue_softc *, int, int);
124 Static int cue_mem(struct cue_softc *, int, int, void *, int);
125 Static int cue_getmac(struct cue_softc *, void *);
127 Static device_method_t cue_methods[] = {
128 /* Device interface */
129 DEVMETHOD(device_probe, cue_match),
130 DEVMETHOD(device_attach, cue_attach),
131 DEVMETHOD(device_detach, cue_detach),
132 DEVMETHOD(device_shutdown, cue_shutdown),
137 Static driver_t cue_driver = {
140 sizeof(struct cue_softc)
143 Static devclass_t cue_devclass;
145 DECLARE_DUMMY_MODULE(if_cue);
146 DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, usbd_driver_load, 0);
147 MODULE_DEPEND(cue, usb, 1, 1, 1);
149 #define CUE_SETBIT(sc, reg, x) \
150 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
152 #define CUE_CLRBIT(sc, reg, x) \
153 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
156 cue_csr_read_1(struct cue_softc *sc, int reg)
158 usb_device_request_t req;
167 req.bmRequestType = UT_READ_VENDOR_DEVICE;
168 req.bRequest = CUE_CMD_READREG;
169 USETW(req.wValue, 0);
170 USETW(req.wIndex, reg);
171 USETW(req.wLength, 1);
173 err = usbd_do_request(sc->cue_udev, &req, &val);
184 cue_csr_read_2(struct cue_softc *sc, int reg)
186 usb_device_request_t req;
195 req.bmRequestType = UT_READ_VENDOR_DEVICE;
196 req.bRequest = CUE_CMD_READREG;
197 USETW(req.wValue, 0);
198 USETW(req.wIndex, reg);
199 USETW(req.wLength, 2);
201 err = usbd_do_request(sc->cue_udev, &req, &val);
212 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
214 usb_device_request_t req;
222 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
223 req.bRequest = CUE_CMD_WRITEREG;
224 USETW(req.wValue, val);
225 USETW(req.wIndex, reg);
226 USETW(req.wLength, 0);
228 err = usbd_do_request(sc->cue_udev, &req, NULL);
240 cue_csr_write_2(struct cue_softc *sc, int reg, int val)
242 usb_device_request_t req;
250 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
251 req.bRequest = CUE_CMD_WRITEREG;
252 USETW(req.wValue, val);
253 USETW(req.wIndex, reg);
254 USETW(req.wLength, 0);
256 err = usbd_do_request(sc->cue_udev, &req, NULL);
268 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
270 usb_device_request_t req;
278 if (cmd == CUE_CMD_READSRAM)
279 req.bmRequestType = UT_READ_VENDOR_DEVICE;
281 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
283 USETW(req.wValue, 0);
284 USETW(req.wIndex, addr);
285 USETW(req.wLength, len);
287 err = usbd_do_request(sc->cue_udev, &req, buf);
298 cue_getmac(struct cue_softc *sc, void *buf)
300 usb_device_request_t req;
308 req.bmRequestType = UT_READ_VENDOR_DEVICE;
309 req.bRequest = CUE_CMD_GET_MACADDR;
310 USETW(req.wValue, 0);
311 USETW(req.wIndex, 0);
312 USETW(req.wLength, ETHER_ADDR_LEN);
314 err = usbd_do_request(sc->cue_udev, &req, buf);
319 printf("cue%d: read MAC address failed\n", sc->cue_unit);
326 #define CUE_POLY 0xEDB88320
330 cue_mchash(const uint8_t *addr)
336 /* Compute CRC for the address value. */
337 crc = 0xFFFFFFFF; /* initial value */
339 for (idx = 0; idx < 6; idx++) {
340 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
341 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
344 return (crc & ((1 << CUE_BITS) - 1));
348 cue_setmulti(struct cue_softc *sc)
351 struct ifmultiaddr *ifma;
354 ifp = &sc->arpcom.ac_if;
356 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
357 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
358 sc->cue_mctab[i] = 0xFF;
359 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
360 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
364 /* first, zot all the existing hash bits */
365 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
366 sc->cue_mctab[i] = 0;
368 /* now program new ones */
369 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
370 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
372 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
375 if (ifma->ifma_addr->sa_family != AF_LINK)
377 h = cue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
378 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
382 * Also include the broadcast address in the filter
383 * so we can receive broadcast frames.
385 if (ifp->if_flags & IFF_BROADCAST) {
386 h = cue_mchash(ifp->if_broadcastaddr);
387 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
390 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
391 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
397 cue_reset(struct cue_softc *sc)
399 usb_device_request_t req;
405 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
406 req.bRequest = CUE_CMD_RESET;
407 USETW(req.wValue, 0);
408 USETW(req.wIndex, 0);
409 USETW(req.wLength, 0);
410 err = usbd_do_request(sc->cue_udev, &req, NULL);
412 printf("cue%d: reset failed\n", sc->cue_unit);
414 /* Wait a little while for the chip to get its brains in order. */
420 * Probe for a Pegasus chip.
424 USB_MATCH_START(cue, uaa);
432 if (uaa->vendor == t->cue_vid &&
433 uaa->product == t->cue_did) {
434 return(UMATCH_VENDOR_PRODUCT);
443 * Attach the interface. Allocate softc structures, do ifmedia
444 * setup and ethernet/BPF attach.
448 USB_ATTACH_START(cue, sc, uaa);
450 u_char eaddr[ETHER_ADDR_LEN];
452 usb_interface_descriptor_t *id;
453 usb_endpoint_descriptor_t *ed;
456 bzero(sc, sizeof(struct cue_softc));
457 sc->cue_iface = uaa->iface;
458 sc->cue_udev = uaa->device;
459 sc->cue_unit = device_get_unit(self);
460 callout_init(&sc->cue_stat_timer);
462 if (usbd_set_config_no(sc->cue_udev, CUE_CONFIG_NO, 0)) {
463 printf("cue%d: getting interface handle failed\n",
465 USB_ATTACH_ERROR_RETURN;
468 id = usbd_get_interface_descriptor(uaa->iface);
470 usbd_devinfo(uaa->device, 0, devinfo);
471 device_set_desc_copy(self, devinfo);
472 printf("%s: %s\n", USBDEVNAME(self), devinfo);
474 /* Find endpoints. */
475 for (i = 0; i < id->bNumEndpoints; i++) {
476 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
478 printf("cue%d: couldn't get ep %d\n",
480 USB_ATTACH_ERROR_RETURN;
482 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
483 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
484 sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
485 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
486 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
487 sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
488 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
489 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
490 sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
494 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
495 mtx_init(&sc->cue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
496 MTX_DEF | MTX_RECURSE);
501 /* Reset the adapter. */
505 * Get station address.
507 cue_getmac(sc, &eaddr);
509 ifp = &sc->arpcom.ac_if;
511 if_initname(ifp, "cue", sc->cue_unit);
512 ifp->if_mtu = ETHERMTU;
513 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
514 ifp->if_ioctl = cue_ioctl;
515 ifp->if_start = cue_start;
516 ifp->if_watchdog = cue_watchdog;
517 ifp->if_init = cue_init;
518 ifp->if_baudrate = 10000000;
519 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
520 ifq_set_ready(&ifp->if_snd);
523 * Call MI attach routine.
525 ether_ifattach(ifp, eaddr);
526 usb_register_netisr();
530 USB_ATTACH_SUCCESS_RETURN;
534 cue_detach(device_ptr_t dev)
536 struct cue_softc *sc;
539 sc = device_get_softc(dev);
541 ifp = &sc->arpcom.ac_if;
544 callout_stop(&sc->cue_stat_timer);
547 if (sc->cue_ep[CUE_ENDPT_TX] != NULL)
548 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
549 if (sc->cue_ep[CUE_ENDPT_RX] != NULL)
550 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
551 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL)
552 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
555 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
556 mtx_destroy(&sc->cue_mtx);
563 * Initialize an RX descriptor and attach an MBUF cluster.
566 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
568 struct mbuf *m_new = NULL;
571 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
573 printf("cue%d: no memory for rx list "
574 "-- packet dropped!\n", sc->cue_unit);
578 MCLGET(m_new, MB_DONTWAIT);
579 if (!(m_new->m_flags & M_EXT)) {
580 printf("cue%d: no memory for rx list "
581 "-- packet dropped!\n", sc->cue_unit);
585 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
588 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
589 m_new->m_data = m_new->m_ext.ext_buf;
592 m_adj(m_new, ETHER_ALIGN);
599 cue_rx_list_init(struct cue_softc *sc)
601 struct cue_cdata *cd;
606 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
607 c = &cd->cue_rx_chain[i];
610 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
612 if (c->cue_xfer == NULL) {
613 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
614 if (c->cue_xfer == NULL)
623 cue_tx_list_init(struct cue_softc *sc)
625 struct cue_cdata *cd;
630 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
631 c = &cd->cue_tx_chain[i];
635 if (c->cue_xfer == NULL) {
636 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
637 if (c->cue_xfer == NULL)
640 c->cue_buf = malloc(CUE_BUFSZ, M_USBDEV, M_WAITOK);
647 cue_rxstart(struct ifnet *ifp)
649 struct cue_softc *sc;
654 c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
656 if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
662 /* Setup new transfer. */
663 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
664 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
665 USBD_NO_TIMEOUT, cue_rxeof);
666 usbd_transfer(c->cue_xfer);
673 * A frame has been uploaded: pass the resulting mbuf chain up to
674 * the higher level protocols.
677 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
679 struct cue_softc *sc;
689 ifp = &sc->arpcom.ac_if;
691 if (!(ifp->if_flags & IFF_RUNNING)) {
696 if (status != USBD_NORMAL_COMPLETION) {
697 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
701 if (usbd_ratecheck(&sc->cue_rx_notice))
702 printf("cue%d: usb error on rx: %s\n", sc->cue_unit,
703 usbd_errstr(status));
704 if (status == USBD_STALLED)
705 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
709 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
712 len = *mtod(m, u_int16_t *);
714 /* No errors; receive the packet. */
717 if (len < sizeof(struct ether_header)) {
723 m_adj(m, sizeof(u_int16_t));
724 m->m_pkthdr.rcvif = ifp;
725 m->m_pkthdr.len = m->m_len = total_len;
727 /* Put the packet on the special USB input queue. */
735 /* Setup new transfer. */
736 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
737 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
738 USBD_NO_TIMEOUT, cue_rxeof);
739 usbd_transfer(c->cue_xfer);
746 * A frame was downloaded to the chip. It's safe for us to clean up
751 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
753 struct cue_softc *sc;
761 ifp = &sc->arpcom.ac_if;
763 if (status != USBD_NORMAL_COMPLETION) {
764 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
768 printf("cue%d: usb error on tx: %s\n", sc->cue_unit,
769 usbd_errstr(status));
770 if (status == USBD_STALLED)
771 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
777 ifp->if_flags &= ~IFF_OACTIVE;
778 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err);
780 if (c->cue_mbuf != NULL) {
781 m_freem(c->cue_mbuf);
790 if (!ifq_is_empty(&ifp->if_snd))
791 (*ifp->if_start)(ifp);
801 struct cue_softc *sc;
811 ifp = &sc->arpcom.ac_if;
813 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
814 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
815 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
817 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
820 callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
828 cue_encap(struct cue_softc *sc, struct mbuf *m, int idx)
834 c = &sc->cue_cdata.cue_tx_chain[idx];
837 * Copy the mbuf data into a contiguous buffer, leaving two
838 * bytes at the beginning to hold the frame length.
840 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
843 total_len = m->m_pkthdr.len + 2;
845 /* The first two bytes are the frame length */
846 c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
847 c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
849 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
850 c, c->cue_buf, total_len, 0, 10000, cue_txeof);
853 err = usbd_transfer(c->cue_xfer);
854 if (err != USBD_IN_PROGRESS) {
859 sc->cue_cdata.cue_tx_cnt++;
865 cue_start(struct ifnet *ifp)
867 struct cue_softc *sc;
868 struct mbuf *m_head = NULL;
873 if (ifp->if_flags & IFF_OACTIVE) {
878 m_head = ifq_poll(&ifp->if_snd);
879 if (m_head == NULL) {
884 if (cue_encap(sc, m_head, 0)) {
885 ifp->if_flags |= IFF_OACTIVE;
889 m_head = ifq_dequeue(&ifp->if_snd);
892 * If there's a BPF listener, bounce a copy of this frame
895 BPF_MTAP(ifp, m_head);
897 ifp->if_flags |= IFF_OACTIVE;
900 * Set a timeout in case the chip goes out to lunch.
911 struct cue_softc *sc = xsc;
912 struct ifnet *ifp = &sc->arpcom.ac_if;
917 if (ifp->if_flags & IFF_RUNNING)
923 * Cancel pending I/O and free all RX/TX buffers.
929 /* Set MAC address */
930 for (i = 0; i < ETHER_ADDR_LEN; i++)
931 cue_csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
933 /* Enable RX logic. */
934 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
936 /* If we want promiscuous mode, set the allframes bit. */
937 if (ifp->if_flags & IFF_PROMISC) {
938 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
940 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
944 if (cue_tx_list_init(sc) == ENOBUFS) {
945 printf("cue%d: tx list init failed\n", sc->cue_unit);
951 if (cue_rx_list_init(sc) == ENOBUFS) {
952 printf("cue%d: rx list init failed\n", sc->cue_unit);
957 /* Load the multicast filter. */
961 * Set the number of RX and TX buffers that we want
962 * to reserve inside the ASIC.
964 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
965 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
967 /* Set advanced operation modes. */
968 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
969 CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
971 /* Program the LED operation. */
972 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
974 /* Open RX and TX pipes. */
975 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
976 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
978 printf("cue%d: open rx pipe failed: %s\n",
979 sc->cue_unit, usbd_errstr(err));
983 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
984 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
986 printf("cue%d: open tx pipe failed: %s\n",
987 sc->cue_unit, usbd_errstr(err));
992 /* Start up the receive pipe. */
993 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
994 c = &sc->cue_cdata.cue_rx_chain[i];
995 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
996 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
997 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
998 usbd_transfer(c->cue_xfer);
1001 ifp->if_flags |= IFF_RUNNING;
1002 ifp->if_flags &= ~IFF_OACTIVE;
1006 callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
1010 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1012 struct cue_softc *sc = ifp->if_softc;
1019 if (ifp->if_flags & IFF_UP) {
1020 if (ifp->if_flags & IFF_RUNNING &&
1021 ifp->if_flags & IFF_PROMISC &&
1022 !(sc->cue_if_flags & IFF_PROMISC)) {
1023 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1025 } else if (ifp->if_flags & IFF_RUNNING &&
1026 !(ifp->if_flags & IFF_PROMISC) &&
1027 sc->cue_if_flags & IFF_PROMISC) {
1028 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1030 } else if (!(ifp->if_flags & IFF_RUNNING))
1033 if (ifp->if_flags & IFF_RUNNING)
1036 sc->cue_if_flags = ifp->if_flags;
1045 error = ether_ioctl(ifp, command, data);
1055 cue_watchdog(struct ifnet *ifp)
1057 struct cue_softc *sc;
1058 struct cue_chain *c;
1065 printf("cue%d: watchdog timeout\n", sc->cue_unit);
1067 c = &sc->cue_cdata.cue_tx_chain[0];
1068 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1069 cue_txeof(c->cue_xfer, c, stat);
1071 if (!ifq_is_empty(&ifp->if_snd))
1079 * Stop the adapter and free any mbufs allocated to the
1083 cue_stop(struct cue_softc *sc)
1091 ifp = &sc->arpcom.ac_if;
1094 cue_csr_write_1(sc, CUE_ETHCTL, 0);
1096 callout_stop(&sc->cue_stat_timer);
1098 /* Stop transfers. */
1099 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1100 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1102 printf("cue%d: abort rx pipe failed: %s\n",
1103 sc->cue_unit, usbd_errstr(err));
1105 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1107 printf("cue%d: close rx pipe failed: %s\n",
1108 sc->cue_unit, usbd_errstr(err));
1110 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1113 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1114 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1116 printf("cue%d: abort tx pipe failed: %s\n",
1117 sc->cue_unit, usbd_errstr(err));
1119 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1121 printf("cue%d: close tx pipe failed: %s\n",
1122 sc->cue_unit, usbd_errstr(err));
1124 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1127 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1128 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1130 printf("cue%d: abort intr pipe failed: %s\n",
1131 sc->cue_unit, usbd_errstr(err));
1133 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1135 printf("cue%d: close intr pipe failed: %s\n",
1136 sc->cue_unit, usbd_errstr(err));
1138 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1141 /* Free RX resources. */
1142 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1143 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
1144 free(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
1145 sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1147 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1148 m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1149 sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1151 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1152 usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1153 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1157 /* Free TX resources. */
1158 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1159 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
1160 free(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
1161 sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1163 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1164 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1165 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1167 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1168 usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1169 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1173 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1180 * Stop all chip I/O so that the kernel's probe routines don't
1181 * get confused by errant DMAs when rebooting.
1184 cue_shutdown(device_ptr_t dev)
1186 struct cue_softc *sc;
1188 sc = device_get_softc(dev);