e00f4ee20f144b10766b5c01f7f36ce3f1617f90
[dragonfly.git] / sys / dev / netif / cue / if_cue.c
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000
3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 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.
19  *
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.
31  *
32  * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.45 2003/12/08 07:54:14 obrien Exp $
33  */
34
35 /*
36  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
37  * adapters and others.
38  *
39  * Written by Bill Paul <wpaul@ee.columbia.edu>
40  * Electrical Engineering Department
41  * Columbia University, New York City
42  */
43
44 /*
45  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
46  * RX filter uses a 512-bit multicast hash table, single perfect entry
47  * for the station address, and promiscuous mode. Unlike the ADMtek
48  * and KLSI chips, the CATC ASIC supports read and write combining
49  * mode where multiple packets can be transfered using a single bulk
50  * transaction, which helps performance a great deal.
51  */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/sockio.h>
56 #include <sys/mbuf.h>
57 #include <sys/malloc.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/bus.h>
61
62 #include <net/if.h>
63 #include <net/ifq_var.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/if_dl.h>
67 #include <net/bpf.h>
68
69 #include <bus/usb/usb.h>
70 #include <bus/usb/usbdi.h>
71 #include <bus/usb/usbdi_util.h>
72 #include <bus/usb/usbdivar.h>
73 #include <bus/usb/usb_ethersubr.h>
74
75 #include "if_cuereg.h"
76
77 /*
78  * Various supported device vendors/products.
79  */
80 static struct usb_devno cue_devs[] = {
81         { USB_DEVICE(0x0423, 0x000a) }, /* CATC Netmate */
82         { USB_DEVICE(0x0423, 0x000c) }, /* CATC Netmate2 */
83         { USB_DEVICE(0x08d1, 0x0001) }, /* SmartBridges SmartLink */
84 };
85
86 static int cue_match(device_t);
87 static int cue_attach(device_t);
88 static int cue_detach(device_t);
89
90 static int cue_tx_list_init(struct cue_softc *);
91 static int cue_rx_list_init(struct cue_softc *);
92 static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
93 static int cue_encap(struct cue_softc *, struct mbuf *, int);
94 static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
95 static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
96 static void cue_tick(void *);
97 static void cue_rxstart(struct ifnet *);
98 static void cue_start(struct ifnet *, struct ifaltq_subque *);
99 static int cue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
100 static void cue_init(void *);
101 static void cue_stop(struct cue_softc *);
102 static void cue_watchdog(struct ifnet *);
103 static void cue_shutdown(device_t);
104
105 static void cue_setmulti(struct cue_softc *);
106 static void cue_reset(struct cue_softc *);
107
108 static int cue_csr_read_1(struct cue_softc *, int);
109 static int cue_csr_write_1(struct cue_softc *, int, int);
110 static int cue_csr_read_2(struct cue_softc *, int);
111 #ifdef notdef
112 static int cue_csr_write_2(struct cue_softc *, int, int);
113 #endif
114 static int cue_mem(struct cue_softc *, int, int, void *, int);
115 static int cue_getmac(struct cue_softc *, void *);
116
117 static device_method_t cue_methods[] = {
118         /* Device interface */
119         DEVMETHOD(device_probe,         cue_match),
120         DEVMETHOD(device_attach,        cue_attach),
121         DEVMETHOD(device_detach,        cue_detach),
122         DEVMETHOD(device_shutdown,      cue_shutdown),
123
124         { 0, 0 }
125 };
126
127 static driver_t cue_driver = {
128         "cue",
129         cue_methods,
130         sizeof(struct cue_softc)
131 };
132
133 static devclass_t cue_devclass;
134
135 DECLARE_DUMMY_MODULE(if_cue);
136 DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, usbd_driver_load, NULL);
137 MODULE_DEPEND(cue, usb, 1, 1, 1);
138
139 #define CUE_SETBIT(sc, reg, x)                          \
140         cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
141
142 #define CUE_CLRBIT(sc, reg, x)                          \
143         cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
144
145 static int
146 cue_csr_read_1(struct cue_softc *sc, int reg)
147 {
148         usb_device_request_t    req;
149         usbd_status             err;
150         u_int8_t                val = 0;
151
152         if (sc->cue_dying)
153                 return(0);
154
155         CUE_LOCK(sc);
156
157         req.bmRequestType = UT_READ_VENDOR_DEVICE;
158         req.bRequest = CUE_CMD_READREG;
159         USETW(req.wValue, 0);
160         USETW(req.wIndex, reg);
161         USETW(req.wLength, 1);
162
163         err = usbd_do_request(sc->cue_udev, &req, &val);
164
165         CUE_UNLOCK(sc);
166
167         if (err)
168                 return(0);
169
170         return(val);
171 }
172
173 static int
174 cue_csr_read_2(struct cue_softc *sc, int reg)
175 {
176         usb_device_request_t    req;
177         usbd_status             err;
178         u_int16_t               val = 0;
179
180         if (sc->cue_dying)
181                 return(0);
182
183         CUE_LOCK(sc);
184
185         req.bmRequestType = UT_READ_VENDOR_DEVICE;
186         req.bRequest = CUE_CMD_READREG;
187         USETW(req.wValue, 0);
188         USETW(req.wIndex, reg);
189         USETW(req.wLength, 2);
190
191         err = usbd_do_request(sc->cue_udev, &req, &val);
192
193         CUE_UNLOCK(sc);
194
195         if (err)
196                 return(0);
197
198         return(val);
199 }
200
201 static int
202 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
203 {
204         usb_device_request_t    req;
205         usbd_status             err;
206
207         if (sc->cue_dying)
208                 return(0);
209
210         CUE_LOCK(sc);
211
212         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
213         req.bRequest = CUE_CMD_WRITEREG;
214         USETW(req.wValue, val);
215         USETW(req.wIndex, reg);
216         USETW(req.wLength, 0);
217
218         err = usbd_do_request(sc->cue_udev, &req, NULL);
219
220         CUE_UNLOCK(sc);
221
222         if (err)
223                 return(-1);
224
225         return(0);
226 }
227
228 #ifdef notdef
229 static int
230 cue_csr_write_2(struct cue_softc *sc, int reg, int val)
231 {
232         usb_device_request_t    req;
233         usbd_status             err;
234
235         if (sc->cue_dying)
236                 return(0);
237
238         CUE_LOCK(sc);
239
240         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
241         req.bRequest = CUE_CMD_WRITEREG;
242         USETW(req.wValue, val);
243         USETW(req.wIndex, reg);
244         USETW(req.wLength, 0);
245
246         err = usbd_do_request(sc->cue_udev, &req, NULL);
247
248         CUE_UNLOCK(sc);
249
250         if (err)
251                 return(-1);
252
253         return(0);
254 }
255 #endif
256
257 static int
258 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
259 {
260         usb_device_request_t    req;
261         usbd_status             err;
262
263         if (sc->cue_dying)
264                 return(0);
265
266         CUE_LOCK(sc);
267
268         if (cmd == CUE_CMD_READSRAM)
269                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
270         else
271                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
272         req.bRequest = cmd;
273         USETW(req.wValue, 0);
274         USETW(req.wIndex, addr);
275         USETW(req.wLength, len);
276
277         err = usbd_do_request(sc->cue_udev, &req, buf);
278
279         CUE_UNLOCK(sc);
280
281         if (err)
282                 return(-1);
283
284         return(0);
285 }
286
287 static int
288 cue_getmac(struct cue_softc *sc, void *buf)
289 {
290         usb_device_request_t    req;
291         usbd_status             err;
292
293         if (sc->cue_dying)
294                 return(0);
295
296         CUE_LOCK(sc);
297
298         req.bmRequestType = UT_READ_VENDOR_DEVICE;
299         req.bRequest = CUE_CMD_GET_MACADDR;
300         USETW(req.wValue, 0);
301         USETW(req.wIndex, 0);
302         USETW(req.wLength, ETHER_ADDR_LEN);
303
304         err = usbd_do_request(sc->cue_udev, &req, buf);
305
306         CUE_UNLOCK(sc);
307
308         if (err) {
309                 if_printf(&sc->arpcom.ac_if, "read MAC address failed\n");
310                 return(-1);
311         }
312
313         return(0);
314 }
315
316 #define CUE_BITS        9
317
318 static void
319 cue_setmulti(struct cue_softc *sc)
320 {
321         struct ifnet            *ifp;
322         struct ifmultiaddr      *ifma;
323         u_int32_t               h = 0, i;
324
325         ifp = &sc->arpcom.ac_if;
326
327         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
328                 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
329                         sc->cue_mctab[i] = 0xFF;
330                 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
331                     &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
332                 return;
333         }
334
335         /* first, zot all the existing hash bits */
336         for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
337                 sc->cue_mctab[i] = 0;
338
339         /* now program new ones */
340         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
341         {
342                 if (ifma->ifma_addr->sa_family != AF_LINK)
343                         continue;
344                 h = ether_crc32_le(
345                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
346                     ETHER_ADDR_LEN) & ((1 << CUE_BITS) - 1);
347                 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
348         }
349
350         /*
351          * Also include the broadcast address in the filter
352          * so we can receive broadcast frames.
353          */
354         if (ifp->if_flags & IFF_BROADCAST) {
355                 h = ether_crc32_le(ifp->if_broadcastaddr, ETHER_ADDR_LEN) &
356                     ((1 << CUE_BITS) - 1);
357                 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
358         }
359
360         cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
361             &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
362
363         return;
364 }
365
366 static void
367 cue_reset(struct cue_softc *sc)
368 {
369         usb_device_request_t    req;
370         usbd_status             err;
371
372         if (sc->cue_dying)
373                 return;
374
375         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
376         req.bRequest = CUE_CMD_RESET;
377         USETW(req.wValue, 0);
378         USETW(req.wIndex, 0);
379         USETW(req.wLength, 0);
380         err = usbd_do_request(sc->cue_udev, &req, NULL);
381         if (err)
382                 if_printf(&sc->arpcom.ac_if, "reset failed\n");
383
384         /* Wait a little while for the chip to get its brains in order. */
385         DELAY(1000);
386         return;
387 }
388
389 /*
390  * Probe for a Pegasus chip.
391  */
392 static int
393 cue_match(device_t self)
394 {
395         struct usb_attach_arg *uaa = device_get_ivars(self);
396
397         if (uaa->iface == NULL)
398                 return(UMATCH_NONE);
399
400         return (usb_lookup(cue_devs, uaa->vendor, uaa->product) != NULL) ?
401             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
402 }
403
404 /*
405  * Attach the interface. Allocate softc structures, do ifmedia
406  * setup and ethernet/BPF attach.
407  */
408 static int
409 cue_attach(device_t self)
410 {
411         struct cue_softc *sc = device_get_softc(self);
412         struct usb_attach_arg *uaa = device_get_ivars(self);
413         u_char                  eaddr[ETHER_ADDR_LEN];
414         struct ifnet            *ifp;
415         usb_interface_descriptor_t      *id;
416         usb_endpoint_descriptor_t       *ed;
417         int                     i;
418
419         sc->cue_iface = uaa->iface;
420         sc->cue_udev = uaa->device;
421         callout_init(&sc->cue_stat_timer);
422
423         if (usbd_set_config_no(sc->cue_udev, CUE_CONFIG_NO, 0)) {
424                 device_printf(self, "setting config no %d failed\n",
425                               CUE_CONFIG_NO);
426                 return ENXIO;
427         }
428
429         id = usbd_get_interface_descriptor(uaa->iface);
430
431         /* Find endpoints. */
432         for (i = 0; i < id->bNumEndpoints; i++) {
433                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
434                 if (!ed) {
435                         device_printf(self, "couldn't get ep %d\n", i);
436                         return ENXIO;
437                 }
438                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
439                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
440                         sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
441                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
442                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
443                         sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
444                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
445                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
446                         sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
447                 }
448         }
449
450         CUE_LOCK(sc);
451
452         ifp = &sc->arpcom.ac_if;
453         if_initname(ifp, device_get_name(self), device_get_unit(self));
454
455 #ifdef notdef
456         /* Reset the adapter. */
457         cue_reset(sc);
458 #endif
459         /*
460          * Get station address.
461          */
462         cue_getmac(sc, &eaddr);
463
464         ifp->if_softc = sc;
465         ifp->if_mtu = ETHERMTU;
466         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
467         ifp->if_ioctl = cue_ioctl;
468         ifp->if_start = cue_start;
469         ifp->if_watchdog = cue_watchdog;
470         ifp->if_init = cue_init;
471         ifp->if_baudrate = 10000000;
472         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
473         ifq_set_ready(&ifp->if_snd);
474
475         /*
476          * Call MI attach routine.
477          */
478         ether_ifattach(ifp, eaddr, NULL);
479         usb_register_netisr();
480         sc->cue_dying = 0;
481
482         CUE_UNLOCK(sc);
483         return 0;
484 }
485
486 static int
487 cue_detach(device_t dev)
488 {
489         struct cue_softc        *sc;
490         struct ifnet            *ifp;
491
492         sc = device_get_softc(dev);
493         CUE_LOCK(sc);
494         ifp = &sc->arpcom.ac_if;
495
496         sc->cue_dying = 1;
497         callout_stop(&sc->cue_stat_timer);
498         ether_ifdetach(ifp);
499
500         if (sc->cue_ep[CUE_ENDPT_TX] != NULL)
501                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
502         if (sc->cue_ep[CUE_ENDPT_RX] != NULL)
503                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
504         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL)
505                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
506
507         CUE_UNLOCK(sc);
508
509         return(0);
510 }
511
512 /*
513  * Initialize an RX descriptor and attach an MBUF cluster.
514  */
515 static int
516 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
517 {
518         struct mbuf             *m_new = NULL;
519
520         if (m == NULL) {
521                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
522                 if (m_new == NULL) {
523                         if_printf(&sc->arpcom.ac_if, "no memory for rx list "
524                                   "-- packet dropped!\n");
525                         return(ENOBUFS);
526                 }
527
528                 MCLGET(m_new, MB_DONTWAIT);
529                 if (!(m_new->m_flags & M_EXT)) {
530                         if_printf(&sc->arpcom.ac_if, "no memory for rx list "
531                                   "-- packet dropped!\n");
532                         m_freem(m_new);
533                         return(ENOBUFS);
534                 }
535                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
536         } else {
537                 m_new = m;
538                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
539                 m_new->m_data = m_new->m_ext.ext_buf;
540         }
541
542         m_adj(m_new, ETHER_ALIGN);
543         c->cue_mbuf = m_new;
544
545         return(0);
546 }
547
548 static int
549 cue_rx_list_init(struct cue_softc *sc)
550 {
551         struct cue_cdata        *cd;
552         struct cue_chain        *c;
553         int                     i;
554
555         cd = &sc->cue_cdata;
556         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
557                 c = &cd->cue_rx_chain[i];
558                 c->cue_sc = sc;
559                 c->cue_idx = i;
560                 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
561                         return(ENOBUFS);
562                 if (c->cue_xfer == NULL) {
563                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
564                         if (c->cue_xfer == NULL)
565                                 return(ENOBUFS);
566                 }
567         }
568
569         return(0);
570 }
571
572 static int
573 cue_tx_list_init(struct cue_softc *sc)
574 {
575         struct cue_cdata        *cd;
576         struct cue_chain        *c;
577         int                     i;
578
579         cd = &sc->cue_cdata;
580         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
581                 c = &cd->cue_tx_chain[i];
582                 c->cue_sc = sc;
583                 c->cue_idx = i;
584                 c->cue_mbuf = NULL;
585                 if (c->cue_xfer == NULL) {
586                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
587                         if (c->cue_xfer == NULL)
588                                 return(ENOBUFS);
589                 }
590                 c->cue_buf = kmalloc(CUE_BUFSZ, M_USBDEV, M_WAITOK);
591         }
592
593         return(0);
594 }
595
596 static void
597 cue_rxstart(struct ifnet *ifp)
598 {
599         struct cue_softc        *sc;
600         struct cue_chain        *c;
601
602         sc = ifp->if_softc;
603         CUE_LOCK(sc);
604         c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
605
606         if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
607                 ifp->if_ierrors++;
608                 CUE_UNLOCK(sc);
609                 return;
610         }
611
612         /* Setup new transfer. */
613         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
614             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
615             USBD_NO_TIMEOUT, cue_rxeof);
616         usbd_transfer(c->cue_xfer);
617         CUE_UNLOCK(sc);
618
619         return;
620 }
621
622 /*
623  * A frame has been uploaded: pass the resulting mbuf chain up to
624  * the higher level protocols.
625  */
626 static void
627 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
628 {
629         struct cue_softc        *sc;
630         struct cue_chain        *c;
631         struct mbuf             *m;
632         struct ifnet            *ifp;
633         int                     total_len = 0;
634         u_int16_t               len;
635
636         c = priv;
637         sc = c->cue_sc;
638         CUE_LOCK(sc);
639         ifp = &sc->arpcom.ac_if;
640
641         if (!(ifp->if_flags & IFF_RUNNING)) {
642                 CUE_UNLOCK(sc);
643                 return;
644         }
645
646         if (status != USBD_NORMAL_COMPLETION) {
647                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
648                         CUE_UNLOCK(sc);
649                         return;
650                 }
651                 if (usbd_ratecheck(&sc->cue_rx_notice)) {
652                         if_printf(ifp, "usb error on rx: %s\n",
653                                   usbd_errstr(status));
654                 }
655                 if (status == USBD_STALLED)
656                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
657                 goto done;
658         }
659
660         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
661
662         m = c->cue_mbuf;
663         len = *mtod(m, u_int16_t *);
664
665         /* No errors; receive the packet. */
666         total_len = len;
667
668         if (len < sizeof(struct ether_header)) {
669                 ifp->if_ierrors++;
670                 goto done;
671         }
672
673         ifp->if_ipackets++;
674         m_adj(m, sizeof(u_int16_t));
675         m->m_pkthdr.rcvif = ifp;
676         m->m_pkthdr.len = m->m_len = total_len;
677
678         /* Put the packet on the special USB input queue. */
679         usb_ether_input(m);
680         cue_rxstart(ifp);
681
682         CUE_UNLOCK(sc);
683
684         return;
685 done:
686         /* Setup new transfer. */
687         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
688             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
689             USBD_NO_TIMEOUT, cue_rxeof);
690         usbd_transfer(c->cue_xfer);
691         CUE_UNLOCK(sc);
692
693         return;
694 }
695
696 /*
697  * A frame was downloaded to the chip. It's safe for us to clean up
698  * the list buffers.
699  */
700
701 static void
702 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
703 {
704         struct cue_softc        *sc;
705         struct cue_chain        *c;
706         struct ifnet            *ifp;
707         usbd_status             err;
708
709         c = priv;
710         sc = c->cue_sc;
711         CUE_LOCK(sc);
712         ifp = &sc->arpcom.ac_if;
713
714         if (status != USBD_NORMAL_COMPLETION) {
715                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
716                         CUE_UNLOCK(sc);
717                         return;
718                 }
719                 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
720                 if (status == USBD_STALLED)
721                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
722                 CUE_UNLOCK(sc);
723                 return;
724         }
725
726         ifp->if_timer = 0;
727         ifq_clr_oactive(&ifp->if_snd);
728         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err);
729
730         if (c->cue_mbuf != NULL) {
731                 m_freem(c->cue_mbuf);
732                 c->cue_mbuf = NULL;
733         }
734
735         if (err)
736                 ifp->if_oerrors++;
737         else
738                 ifp->if_opackets++;
739
740         if (!ifq_is_empty(&ifp->if_snd))
741                 if_devstart(ifp);
742
743         CUE_UNLOCK(sc);
744
745         return;
746 }
747
748 static void
749 cue_tick(void *xsc)
750 {
751         struct cue_softc        *sc;
752         struct ifnet            *ifp;
753
754         sc = xsc;
755
756         if (sc == NULL)
757                 return;
758
759         CUE_LOCK(sc);
760
761         ifp = &sc->arpcom.ac_if;
762
763         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
764         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
765         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
766
767         if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
768                 ifp->if_ierrors++;
769
770         callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
771
772         CUE_UNLOCK(sc);
773
774         return;
775 }
776
777 static int
778 cue_encap(struct cue_softc *sc, struct mbuf *m, int idx)
779 {
780         int                     total_len;
781         struct cue_chain        *c;
782         usbd_status             err;
783
784         c = &sc->cue_cdata.cue_tx_chain[idx];
785
786         /*
787          * Copy the mbuf data into a contiguous buffer, leaving two
788          * bytes at the beginning to hold the frame length.
789          */
790         m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
791         c->cue_mbuf = m;
792
793         total_len = m->m_pkthdr.len + 2;
794
795         /* The first two bytes are the frame length */
796         c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
797         c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
798
799         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
800             c, c->cue_buf, total_len, 0, 10000, cue_txeof);
801
802         /* Transmit */
803         err = usbd_transfer(c->cue_xfer);
804         if (err != USBD_IN_PROGRESS) {
805                 cue_stop(sc);
806                 return(EIO);
807         }
808
809         sc->cue_cdata.cue_tx_cnt++;
810
811         return(0);
812 }
813
814 static void
815 cue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
816 {
817         struct cue_softc        *sc;
818         struct mbuf             *m_head = NULL;
819
820         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
821
822         sc = ifp->if_softc;
823         CUE_LOCK(sc);
824
825         if (ifq_is_oactive(&ifp->if_snd)) {
826                 CUE_UNLOCK(sc);
827                 return;
828         }
829
830         m_head = ifq_dequeue(&ifp->if_snd, NULL);
831         if (m_head == NULL) {
832                 CUE_UNLOCK(sc);
833                 return;
834         }
835
836         if (cue_encap(sc, m_head, 0)) {
837                 /* cue_encap() will free m_head, if we reach here */
838                 ifq_set_oactive(&ifp->if_snd);
839                 CUE_UNLOCK(sc);
840                 return;
841         }
842
843         /*
844          * If there's a BPF listener, bounce a copy of this frame
845          * to him.
846          */
847         BPF_MTAP(ifp, m_head);
848
849         ifq_set_oactive(&ifp->if_snd);
850
851         /*
852          * Set a timeout in case the chip goes out to lunch.
853          */
854         ifp->if_timer = 5;
855         CUE_UNLOCK(sc);
856
857         return;
858 }
859
860 static void
861 cue_init(void *xsc)
862 {
863         struct cue_softc        *sc = xsc;
864         struct ifnet            *ifp = &sc->arpcom.ac_if;
865         struct cue_chain        *c;
866         usbd_status             err;
867         int                     i;
868
869         if (ifp->if_flags & IFF_RUNNING)
870                 return;
871
872         CUE_LOCK(sc);
873
874         /*
875          * Cancel pending I/O and free all RX/TX buffers.
876          */
877 #ifdef foo
878         cue_reset(sc);
879 #endif
880
881         /* Set MAC address */
882         for (i = 0; i < ETHER_ADDR_LEN; i++)
883                 cue_csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
884
885         /* Enable RX logic. */
886         cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
887
888          /* If we want promiscuous mode, set the allframes bit. */
889         if (ifp->if_flags & IFF_PROMISC) {
890                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
891         } else {
892                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
893         }
894
895         /* Init TX ring. */
896         if (cue_tx_list_init(sc) == ENOBUFS) {
897                 if_printf(ifp, "tx list init failed\n");
898                 CUE_UNLOCK(sc);
899                 return;
900         }
901
902         /* Init RX ring. */
903         if (cue_rx_list_init(sc) == ENOBUFS) {
904                 if_printf(ifp, "rx list init failed\n");
905                 CUE_UNLOCK(sc);
906                 return;
907         }
908
909         /* Load the multicast filter. */
910         cue_setmulti(sc);
911
912         /*
913          * Set the number of RX and TX buffers that we want
914          * to reserve inside the ASIC.
915          */
916         cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
917         cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
918
919         /* Set advanced operation modes. */
920         cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
921             CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
922
923         /* Program the LED operation. */
924         cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
925
926         /* Open RX and TX pipes. */
927         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
928             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
929         if (err) {
930                 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
931                 CUE_UNLOCK(sc);
932                 return;
933         }
934         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
935             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
936         if (err) {
937                 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
938                 CUE_UNLOCK(sc);
939                 return;
940         }
941
942         /* Start up the receive pipe. */
943         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
944                 c = &sc->cue_cdata.cue_rx_chain[i];
945                 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
946                     c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
947                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
948                 usbd_transfer(c->cue_xfer);
949         }
950
951         ifp->if_flags |= IFF_RUNNING;
952         ifq_clr_oactive(&ifp->if_snd);
953
954         CUE_UNLOCK(sc);
955
956         callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
957 }
958
959 static int
960 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
961 {
962         struct cue_softc        *sc = ifp->if_softc;
963         int                     error = 0;
964
965         CUE_LOCK(sc);
966
967         switch(command) {
968         case SIOCSIFFLAGS:
969                 if (ifp->if_flags & IFF_UP) {
970                         if (ifp->if_flags & IFF_RUNNING &&
971                             ifp->if_flags & IFF_PROMISC &&
972                             !(sc->cue_if_flags & IFF_PROMISC)) {
973                                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
974                                 cue_setmulti(sc);
975                         } else if (ifp->if_flags & IFF_RUNNING &&
976                             !(ifp->if_flags & IFF_PROMISC) &&
977                             sc->cue_if_flags & IFF_PROMISC) {
978                                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
979                                 cue_setmulti(sc);
980                         } else if (!(ifp->if_flags & IFF_RUNNING))
981                                 cue_init(sc);
982                 } else {
983                         if (ifp->if_flags & IFF_RUNNING)
984                                 cue_stop(sc);
985                 }
986                 sc->cue_if_flags = ifp->if_flags;
987                 error = 0;
988                 break;
989         case SIOCADDMULTI:
990         case SIOCDELMULTI:
991                 cue_setmulti(sc);
992                 error = 0;
993                 break;
994         default:
995                 error = ether_ioctl(ifp, command, data);
996                 break;
997         }
998
999         CUE_UNLOCK(sc);
1000
1001         return(error);
1002 }
1003
1004 static void
1005 cue_watchdog(struct ifnet *ifp)
1006 {
1007         struct cue_softc        *sc;
1008         struct cue_chain        *c;
1009         usbd_status             stat;
1010
1011         sc = ifp->if_softc;
1012         CUE_LOCK(sc);
1013
1014         ifp->if_oerrors++;
1015         if_printf(ifp, "watchdog timeout\n");
1016
1017         c = &sc->cue_cdata.cue_tx_chain[0];
1018         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1019         cue_txeof(c->cue_xfer, c, stat);
1020
1021         if (!ifq_is_empty(&ifp->if_snd))
1022                 if_devstart(ifp);
1023         CUE_UNLOCK(sc);
1024
1025         return;
1026 }
1027
1028 /*
1029  * Stop the adapter and free any mbufs allocated to the
1030  * RX and TX lists.
1031  */
1032 static void
1033 cue_stop(struct cue_softc *sc)
1034 {
1035         usbd_status             err;
1036         struct ifnet            *ifp;
1037         int                     i;
1038
1039         CUE_LOCK(sc);
1040
1041         ifp = &sc->arpcom.ac_if;
1042         ifp->if_timer = 0;
1043
1044         cue_csr_write_1(sc, CUE_ETHCTL, 0);
1045         cue_reset(sc);
1046         callout_stop(&sc->cue_stat_timer);
1047
1048         /* Stop transfers. */
1049         if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1050                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1051                 if (err) {
1052                         if_printf(ifp, "abort rx pipe failed: %s\n",
1053                                   usbd_errstr(err));
1054                 }
1055                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1056                 if (err) {
1057                         if_printf(ifp, "close rx pipe failed: %s\n",
1058                                   usbd_errstr(err));
1059                 }
1060                 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1061         }
1062
1063         if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1064                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1065                 if (err) {
1066                         if_printf(ifp, "abort tx pipe failed: %s\n",
1067                                   usbd_errstr(err));
1068                 }
1069                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1070                 if (err) {
1071                         if_printf(ifp, "close tx pipe failed: %s\n",
1072                                   usbd_errstr(err));
1073                 }
1074                 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1075         }
1076
1077         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1078                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1079                 if (err) {
1080                         if_printf(ifp, "abort intr pipe failed: %s\n",
1081                                   usbd_errstr(err));
1082                 }
1083                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1084                 if (err) {
1085                         if_printf(ifp, "close intr pipe failed: %s\n",
1086                                   usbd_errstr(err));
1087                 }
1088                 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1089         }
1090
1091         /* Free RX resources. */
1092         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1093                 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
1094                         kfree(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
1095                         sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1096                 }
1097                 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1098                         m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1099                         sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1100                 }
1101                 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1102                         usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1103                         sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1104                 }
1105         }
1106
1107         /* Free TX resources. */
1108         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1109                 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
1110                         kfree(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
1111                         sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1112                 }
1113                 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1114                         m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1115                         sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1116                 }
1117                 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1118                         usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1119                         sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1120                 }
1121         }
1122
1123         ifp->if_flags &= ~IFF_RUNNING;
1124         ifq_clr_oactive(&ifp->if_snd);
1125         CUE_UNLOCK(sc);
1126
1127         return;
1128 }
1129
1130 /*
1131  * Stop all chip I/O so that the kernel's probe routines don't
1132  * get confused by errant DMAs when rebooting.
1133  */
1134 static void
1135 cue_shutdown(device_t dev)
1136 {
1137         struct cue_softc        *sc;
1138
1139         sc = device_get_softc(dev);
1140
1141         CUE_LOCK(sc);
1142         cue_reset(sc);
1143         cue_stop(sc);
1144         CUE_UNLOCK(sc);
1145
1146         return;
1147 }