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