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