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