Merge from vendor branch FILE:
[dragonfly.git] / sys / dev / netif / kue / if_kue.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_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
33  * $DragonFly: src/sys/dev/netif/kue/if_kue.c,v 1.22 2006/10/25 20:55:57 dillon Exp $
34  */
35
36 /*
37  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38  *
39  * Written by Bill Paul <wpaul@ee.columbia.edu>
40  * Electrical Engineering Department
41  * Columbia University, New York City
42  */
43
44 /*
45  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
46  * ethernet MAC and embedded microcontroller (called the QT Engine).
47  * The chip must have firmware loaded into it before it will operate.
48  * Packets are passed between the chip and host via bulk transfers.
49  * There is an interrupt endpoint mentioned in the software spec, however
50  * it's currently unused. This device is 10Mbps half-duplex only, hence
51  * there is no media selection logic. The MAC supports a 128 entry
52  * multicast filter, though the exact size of the filter can depend
53  * on the firmware. Curiously, while the software spec describes various
54  * ethernet statistics counters, my sample adapter and firmware combination
55  * claims not to support any statistics counters at all.
56  *
57  * Note that once we load the firmware in the device, we have to be
58  * careful not to load it again: if you restart your computer but
59  * leave the adapter attached to the USB controller, it may remain
60  * powered on and retain its firmware. In this case, we don't need
61  * to load the firmware a second time.
62  *
63  * Special thanks to Rob Furr for providing an ADS Technologies
64  * adapter for development and testing. No monkeys were harmed during
65  * the development of this driver.
66  */
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/sockio.h>
71 #include <sys/mbuf.h>
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #include <sys/socket.h>
75 #include <sys/bus.h>
76
77 #include <net/if.h>
78 #include <net/ifq_var.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83 #include <net/bpf.h>
84
85 #include <machine/clock.h>
86
87 #include <bus/usb/usb.h>
88 #include <bus/usb/usbdi.h>
89 #include <bus/usb/usbdi_util.h>
90 #include <bus/usb/usbdivar.h>
91 #include <bus/usb/usbdevs.h>
92 #include <bus/usb/usb_ethersubr.h>
93
94 #include "if_kuereg.h"
95 #include <bus/usb/kue_fw.h>
96
97 MODULE_DEPEND(kue, usb, 1, 1, 1);
98
99 /*
100  * Various supported device vendors/products.
101  */
102 Static struct kue_type kue_devs[] = {
103         { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
104         { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 },
105         { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
106         { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
107         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
108         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
109         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
110         { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
111         { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
112         { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
113         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
114         { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
115         { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
116         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
117         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
118         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
119         { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
120         { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
121         { 0, 0 }
122 };
123
124 Static int kue_match(device_ptr_t);
125 Static int kue_attach(device_ptr_t);
126 Static int kue_detach(device_ptr_t);
127 Static void kue_shutdown(device_ptr_t);
128 Static int kue_tx_list_init(struct kue_softc *);
129 Static int kue_rx_list_init(struct kue_softc *);
130 Static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
131 Static int kue_encap(struct kue_softc *, struct mbuf *, int);
132 Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
133 Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
134 Static void kue_start(struct ifnet *);
135 Static void kue_rxstart(struct ifnet *);
136 Static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
137 Static void kue_init(void *);
138 Static void kue_stop(struct kue_softc *);
139 Static void kue_watchdog(struct ifnet *);
140
141 Static void kue_setmulti(struct kue_softc *);
142 Static void kue_reset(struct kue_softc *);
143
144 Static usbd_status kue_do_request(usbd_device_handle,
145                                   usb_device_request_t *, void *);
146 Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
147                            u_int16_t, char *, int);
148 Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
149 Static int kue_load_fw(struct kue_softc *);
150
151 Static device_method_t kue_methods[] = {
152         /* Device interface */
153         DEVMETHOD(device_probe,         kue_match),
154         DEVMETHOD(device_attach,        kue_attach),
155         DEVMETHOD(device_detach,        kue_detach),
156         DEVMETHOD(device_shutdown,      kue_shutdown),
157
158         { 0, 0 }
159 };
160
161 Static driver_t kue_driver = {
162         "kue",
163         kue_methods,
164         sizeof(struct kue_softc)
165 };
166
167 Static devclass_t kue_devclass;
168
169 DECLARE_DUMMY_MODULE(if_kue);
170 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
171
172 /*
173  * We have a custom do_request function which is almost like the
174  * regular do_request function, except it has a much longer timeout.
175  * Why? Because we need to make requests over the control endpoint
176  * to download the firmware to the device, which can take longer
177  * than the default timeout.
178  */
179 Static usbd_status
180 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
181 {
182         usbd_xfer_handle        xfer;
183         usbd_status             err;
184
185         xfer = usbd_alloc_xfer(dev);
186         usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
187             data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
188         err = usbd_sync_transfer(xfer);
189         usbd_free_xfer(xfer);
190         return(err);
191 }
192
193 Static usbd_status
194 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
195 {
196         usbd_device_handle      dev;
197         usb_device_request_t    req;
198         usbd_status             err;
199
200         if (sc->kue_dying)
201                 return(USBD_NORMAL_COMPLETION);
202
203         dev = sc->kue_udev;
204
205         KUE_LOCK(sc);
206
207         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
208
209         req.bRequest = breq;
210         USETW(req.wValue, word);
211         USETW(req.wIndex, 0);
212         USETW(req.wLength, 0);
213
214         err = kue_do_request(dev, &req, NULL);
215
216         KUE_UNLOCK(sc);
217
218         return(err);
219 }
220
221 Static usbd_status
222 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
223         char *data, int len)
224 {
225         usbd_device_handle      dev;
226         usb_device_request_t    req;
227         usbd_status             err;
228
229         dev = sc->kue_udev;
230
231         if (sc->kue_dying)
232                 return(USBD_NORMAL_COMPLETION);
233
234         KUE_LOCK(sc);
235
236         if (rw == KUE_CTL_WRITE)
237                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
238         else
239                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
240
241         req.bRequest = breq;
242         USETW(req.wValue, val);
243         USETW(req.wIndex, 0);
244         USETW(req.wLength, len);
245
246         err = kue_do_request(dev, &req, data);
247
248         KUE_UNLOCK(sc);
249
250         return(err);
251 }
252
253 Static int
254 kue_load_fw(struct kue_softc *sc)
255 {
256         usbd_status             err;
257         usb_device_descriptor_t *dd;
258         int                     hwrev;
259
260         dd = &sc->kue_udev->ddesc;
261         hwrev = UGETW(dd->bcdDevice);
262
263         /*
264          * First, check if we even need to load the firmware.
265          * If the device was still attached when the system was
266          * rebooted, it may already have firmware loaded in it.
267          * If this is the case, we don't need to do it again.
268          * And in fact, if we try to load it again, we'll hang,
269          * so we have to avoid this condition if we don't want
270          * to look stupid.
271          *
272          * We can test this quickly by checking the bcdRevision
273          * code. The NIC will return a different revision code if
274          * it's probed while the firmware is still loaded and
275          * running.
276          */
277         if (hwrev == 0x0202)
278                 return(0);
279
280         /* Load code segment */
281         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
282             0, kue_code_seg, sizeof(kue_code_seg));
283         if (err) {
284                 printf("kue%d: failed to load code segment: %s\n",
285                     sc->kue_unit, usbd_errstr(err));
286                         return(ENXIO);
287         }
288
289         /* Load fixup segment */
290         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
291             0, kue_fix_seg, sizeof(kue_fix_seg));
292         if (err) {
293                 printf("kue%d: failed to load fixup segment: %s\n",
294                     sc->kue_unit, usbd_errstr(err));
295                         return(ENXIO);
296         }
297
298         /* Send trigger command. */
299         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
300             0, kue_trig_seg, sizeof(kue_trig_seg));
301         if (err) {
302                 printf("kue%d: failed to load trigger segment: %s\n",
303                     sc->kue_unit, usbd_errstr(err));
304                         return(ENXIO);
305         }
306
307         return(0);
308 }
309
310 Static void
311 kue_setmulti(struct kue_softc *sc)
312 {
313         struct ifnet            *ifp;
314         struct ifmultiaddr      *ifma;
315         int                     i = 0;
316
317         ifp = &sc->arpcom.ac_if;
318
319         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
320                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
321                 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
322                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
323                 return;
324         }
325
326         sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
327
328 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
329         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
330 #else
331         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
332 #endif
333         {
334                 if (ifma->ifma_addr->sa_family != AF_LINK)
335                         continue;
336                 /*
337                  * If there are too many addresses for the
338                  * internal filter, switch over to allmulti mode.
339                  */
340                 if (i == KUE_MCFILTCNT(sc))
341                         break;
342                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
343                     KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
344                 i++;
345         }
346
347         if (i == KUE_MCFILTCNT(sc))
348                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
349         else {
350                 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
351                 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
352                     i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
353         }
354
355         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
356
357         return;
358 }
359
360 /*
361  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
362  * done after the firmware is loaded into the adapter in order to
363  * bring it into proper operation.
364  */
365 Static void
366 kue_reset(struct kue_softc *sc)
367 {
368         if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
369             usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
370             &sc->kue_iface)) {
371                 printf("kue%d: getting interface handle failed\n",
372                     sc->kue_unit);
373         }
374
375         /* Wait a little while for the chip to get its brains in order. */
376         DELAY(1000);
377         return;
378 }
379
380 /*
381  * Probe for a KLSI chip.
382  */
383 USB_MATCH(kue)
384 {
385         USB_MATCH_START(kue, uaa);
386         struct kue_type                 *t;
387
388         if (!uaa->iface)
389                 return(UMATCH_NONE);
390
391         t = kue_devs;
392         while(t->kue_vid) {
393                 if (uaa->vendor == t->kue_vid &&
394                     uaa->product == t->kue_did) {
395                         return(UMATCH_VENDOR_PRODUCT);
396                 }
397                 t++;
398         }
399
400         return(UMATCH_NONE);
401 }
402
403 /*
404  * Attach the interface. Allocate softc structures, do
405  * setup and ethernet/BPF attach.
406  */
407 USB_ATTACH(kue)
408 {
409         USB_ATTACH_START(kue, sc, uaa);
410         char                    devinfo[1024];
411         struct ifnet            *ifp;
412         usbd_status             err;
413         usb_interface_descriptor_t      *id;
414         usb_endpoint_descriptor_t       *ed;
415         int                     i;
416
417         sc->kue_iface = uaa->iface;
418         sc->kue_udev = uaa->device;
419         sc->kue_unit = device_get_unit(self);
420
421         id = usbd_get_interface_descriptor(uaa->iface);
422
423         usbd_devinfo(uaa->device, 0, devinfo);
424         device_set_desc_copy(self, devinfo);
425         printf("%s: %s\n", USBDEVNAME(self), devinfo);
426
427         /* Find endpoints. */
428         for (i = 0; i < id->bNumEndpoints; i++) {
429                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
430                 if (!ed) {
431                         printf("kue%d: couldn't get ep %d\n",
432                             sc->kue_unit, i);
433                         USB_ATTACH_ERROR_RETURN;
434                 }
435                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
436                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
437                         sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
438                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
439                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
440                         sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
441                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
442                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
443                         sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
444                 }
445         }
446
447 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
448         mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
449             MTX_DEF | MTX_RECURSE);
450 #endif
451         KUE_LOCK(sc);
452
453         /* Load the firmware into the NIC. */
454         if (kue_load_fw(sc)) {
455                 KUE_UNLOCK(sc);
456 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
457                 mtx_destroy(&sc->kue_mtx);
458 #endif
459                 USB_ATTACH_ERROR_RETURN;
460         }
461
462         /* Reset the adapter. */
463         kue_reset(sc);
464
465         /* Read ethernet descriptor */
466         err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
467             0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
468
469         sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
470             M_USBDEV, M_WAITOK);
471
472         ifp = &sc->arpcom.ac_if;
473         ifp->if_softc = sc;
474         if_initname(ifp, "kue", sc->kue_unit);
475         ifp->if_mtu = ETHERMTU;
476         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
477         ifp->if_ioctl = kue_ioctl;
478         ifp->if_start = kue_start;
479         ifp->if_watchdog = kue_watchdog;
480         ifp->if_init = kue_init;
481         ifp->if_baudrate = 10000000;
482         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
483         ifq_set_ready(&ifp->if_snd);
484
485         /*
486          * Call MI attach routine.
487          */
488         ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
489         usb_register_netisr();
490         sc->kue_dying = 0;
491
492         KUE_UNLOCK(sc);
493
494         USB_ATTACH_SUCCESS_RETURN;
495 }
496
497 Static int
498 kue_detach(device_ptr_t dev)
499 {
500         struct kue_softc        *sc;
501         struct ifnet            *ifp;
502
503         sc = device_get_softc(dev);
504         KUE_LOCK(sc);
505         ifp = &sc->arpcom.ac_if;
506
507         sc->kue_dying = 1;
508
509         if (ifp != NULL)
510                 ether_ifdetach(ifp);
511
512         if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
513                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
514         if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
515                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
516         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
517                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
518
519         if (sc->kue_mcfilters != NULL)
520                 kfree(sc->kue_mcfilters, M_USBDEV);
521
522         KUE_UNLOCK(sc);
523 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
524         mtx_destroy(&sc->kue_mtx);
525 #endif
526
527         return(0);
528 }
529
530 /*
531  * Initialize an RX descriptor and attach an MBUF cluster.
532  */
533 Static int
534 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
535 {
536         struct mbuf             *m_new = NULL;
537
538         if (m == NULL) {
539                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
540                 if (m_new == NULL) {
541                         printf("kue%d: no memory for rx list "
542                             "-- packet dropped!\n", sc->kue_unit);
543                         return(ENOBUFS);
544                 }
545
546                 MCLGET(m_new, MB_DONTWAIT);
547                 if (!(m_new->m_flags & M_EXT)) {
548                         printf("kue%d: no memory for rx list "
549                             "-- packet dropped!\n", sc->kue_unit);
550                         m_freem(m_new);
551                         return(ENOBUFS);
552                 }
553                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
554         } else {
555                 m_new = m;
556                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
557                 m_new->m_data = m_new->m_ext.ext_buf;
558         }
559
560         c->kue_mbuf = m_new;
561
562         return(0);
563 }
564
565 Static int
566 kue_rx_list_init(struct kue_softc *sc)
567 {
568         struct kue_cdata        *cd;
569         struct kue_chain        *c;
570         int                     i;
571
572         cd = &sc->kue_cdata;
573         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
574                 c = &cd->kue_rx_chain[i];
575                 c->kue_sc = sc;
576                 c->kue_idx = i;
577                 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
578                         return(ENOBUFS);
579                 if (c->kue_xfer == NULL) {
580                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
581                         if (c->kue_xfer == NULL)
582                                 return(ENOBUFS);
583                 }
584         }
585
586         return(0);
587 }
588
589 Static int
590 kue_tx_list_init(struct kue_softc *sc)
591 {
592         struct kue_cdata        *cd;
593         struct kue_chain        *c;
594         int                     i;
595
596         cd = &sc->kue_cdata;
597         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
598                 c = &cd->kue_tx_chain[i];
599                 c->kue_sc = sc;
600                 c->kue_idx = i;
601                 c->kue_mbuf = NULL;
602                 if (c->kue_xfer == NULL) {
603                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
604                         if (c->kue_xfer == NULL)
605                                 return(ENOBUFS);
606                 }
607                 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
608                 if (c->kue_buf == NULL)
609                         return(ENOBUFS);
610         }
611
612         return(0);
613 }
614
615 Static void
616 kue_rxstart(struct ifnet *ifp)
617 {
618         struct kue_softc        *sc;
619         struct kue_chain        *c;
620
621         sc = ifp->if_softc;
622         KUE_LOCK(sc);
623         c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
624
625         if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
626                 ifp->if_ierrors++;
627                 return;
628         }
629
630         /* Setup new transfer. */
631         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
632             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
633             USBD_NO_TIMEOUT, kue_rxeof);
634         usbd_transfer(c->kue_xfer);
635
636         KUE_UNLOCK(sc);
637
638         return;
639 }
640
641 /*
642  * A frame has been uploaded: pass the resulting mbuf chain up to
643  * the higher level protocols.
644  */
645 Static void
646 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
647 {
648         struct kue_softc        *sc;
649         struct kue_chain        *c;
650         struct mbuf             *m;
651         struct ifnet            *ifp;
652         int                     total_len = 0;
653         u_int16_t               len;
654
655         c = priv;
656         sc = c->kue_sc;
657         KUE_LOCK(sc);
658         ifp = &sc->arpcom.ac_if;
659
660         if (!(ifp->if_flags & IFF_RUNNING)) {
661                 KUE_UNLOCK(sc);
662                 return;
663         }
664
665         if (status != USBD_NORMAL_COMPLETION) {
666                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
667                         KUE_UNLOCK(sc);
668                         return;
669                 }
670                 if (usbd_ratecheck(&sc->kue_rx_notice))
671                         printf("kue%d: usb error on rx: %s\n", sc->kue_unit,
672                             usbd_errstr(status));
673                 if (status == USBD_STALLED)
674                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
675                 goto done;
676         }
677
678         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
679         m = c->kue_mbuf;
680         if (total_len <= 1)
681                 goto done;
682
683         len = *mtod(m, u_int16_t *);
684         m_adj(m, sizeof(u_int16_t));
685
686         /* No errors; receive the packet. */
687         total_len = len;
688
689         if (len < sizeof(struct ether_header)) {
690                 ifp->if_ierrors++;
691                 goto done;
692         }
693
694         ifp->if_ipackets++;
695         m->m_pkthdr.rcvif = ifp;
696         m->m_pkthdr.len = m->m_len = total_len;
697
698         /* Put the packet on the special USB input queue. */
699         usb_ether_input(m);
700         kue_rxstart(ifp);
701
702         KUE_UNLOCK(sc);
703
704         return;
705 done:
706
707         /* Setup new transfer. */
708         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
709             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
710             USBD_NO_TIMEOUT, kue_rxeof);
711         usbd_transfer(c->kue_xfer);
712         KUE_UNLOCK(sc);
713
714         return;
715 }
716
717 /*
718  * A frame was downloaded to the chip. It's safe for us to clean up
719  * the list buffers.
720  */
721
722 Static void
723 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
724 {
725         struct kue_softc        *sc;
726         struct kue_chain        *c;
727         struct ifnet            *ifp;
728         usbd_status             err;
729
730         c = priv;
731         sc = c->kue_sc;
732         KUE_LOCK(sc);
733
734         ifp = &sc->arpcom.ac_if;
735         ifp->if_timer = 0;
736         ifp->if_flags &= ~IFF_OACTIVE;
737
738         if (status != USBD_NORMAL_COMPLETION) {
739                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
740                         KUE_UNLOCK(sc);
741                         return;
742                 }
743                 printf("kue%d: usb error on tx: %s\n", sc->kue_unit,
744                     usbd_errstr(status));
745                 if (status == USBD_STALLED)
746                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
747                 KUE_UNLOCK(sc);
748                 return;
749         }
750
751         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
752
753         if (c->kue_mbuf != NULL) {
754                 m_freem(c->kue_mbuf);
755                 c->kue_mbuf = NULL;
756         }
757
758         if (err)
759                 ifp->if_oerrors++;
760         else
761                 ifp->if_opackets++;
762
763         if (!ifq_is_empty(&ifp->if_snd))
764                 (*ifp->if_start)(ifp);
765
766         KUE_UNLOCK(sc);
767
768         return;
769 }
770
771 Static int
772 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
773 {
774         int                     total_len;
775         struct kue_chain        *c;
776         usbd_status             err;
777
778         c = &sc->kue_cdata.kue_tx_chain[idx];
779
780         /*
781          * Copy the mbuf data into a contiguous buffer, leaving two
782          * bytes at the beginning to hold the frame length.
783          */
784         m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
785         c->kue_mbuf = m;
786
787         total_len = m->m_pkthdr.len + 2;
788         total_len += 64 - (total_len % 64);
789
790         /* Frame length is specified in the first 2 bytes of the buffer. */
791         c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
792         c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
793
794         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
795             c, c->kue_buf, total_len, 0, 10000, kue_txeof);
796
797         /* Transmit */
798         err = usbd_transfer(c->kue_xfer);
799         if (err != USBD_IN_PROGRESS) {
800                 kue_stop(sc);
801                 return(EIO);
802         }
803
804         sc->kue_cdata.kue_tx_cnt++;
805
806         return(0);
807 }
808
809 Static void
810 kue_start(struct ifnet *ifp)
811 {
812         struct kue_softc        *sc;
813         struct mbuf             *m_head = NULL;
814
815         sc = ifp->if_softc;
816         KUE_LOCK(sc);
817
818         if (ifp->if_flags & IFF_OACTIVE) {
819                 KUE_UNLOCK(sc);
820                 return;
821         }
822
823         m_head = ifq_poll(&ifp->if_snd);
824         if (m_head == NULL) {
825                 KUE_UNLOCK(sc);
826                 return;
827         }
828
829         if (kue_encap(sc, m_head, 0)) {
830                 ifp->if_flags |= IFF_OACTIVE;
831                 KUE_UNLOCK(sc);
832                 return;
833         }
834         ifq_dequeue(&ifp->if_snd, m_head);
835
836         /*
837          * If there's a BPF listener, bounce a copy of this frame
838          * to him.
839          */
840         BPF_MTAP(ifp, m_head);
841
842         ifp->if_flags |= IFF_OACTIVE;
843
844         /*
845          * Set a timeout in case the chip goes out to lunch.
846          */
847         ifp->if_timer = 5;
848         KUE_UNLOCK(sc);
849
850         return;
851 }
852
853 Static void
854 kue_init(void *xsc)
855 {
856         struct kue_softc        *sc = xsc;
857         struct ifnet            *ifp = &sc->arpcom.ac_if;
858         struct kue_chain        *c;
859         usbd_status             err;
860         int                     i;
861
862         KUE_LOCK(sc);
863
864         if (ifp->if_flags & IFF_RUNNING) {
865                 KUE_UNLOCK(sc);
866                 return;
867         }
868
869         /* Set MAC address */
870         kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
871             0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
872
873         sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
874
875          /* If we want promiscuous mode, set the allframes bit. */
876         if (ifp->if_flags & IFF_PROMISC)
877                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
878
879         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
880
881         /* I'm not sure how to tune these. */
882 #ifdef notdef
883         /*
884          * Leave this one alone for now; setting it
885          * wrong causes lockups on some machines/controllers.
886          */
887         kue_setword(sc, KUE_CMD_SET_SOFS, 1);
888 #endif
889         kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
890
891         /* Init TX ring. */
892         if (kue_tx_list_init(sc) == ENOBUFS) {
893                 printf("kue%d: tx list init failed\n", sc->kue_unit);
894                 KUE_UNLOCK(sc);
895                 return;
896         }
897
898         /* Init RX ring. */
899         if (kue_rx_list_init(sc) == ENOBUFS) {
900                 printf("kue%d: rx list init failed\n", sc->kue_unit);
901                 KUE_UNLOCK(sc);
902                 return;
903         }
904
905         /* Load the multicast filter. */
906         kue_setmulti(sc);
907
908         /* Open RX and TX pipes. */
909         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
910             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
911         if (err) {
912                 printf("kue%d: open rx pipe failed: %s\n",
913                     sc->kue_unit, usbd_errstr(err));
914                 KUE_UNLOCK(sc);
915                 return;
916         }
917
918         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
919             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
920         if (err) {
921                 printf("kue%d: open tx pipe failed: %s\n",
922                     sc->kue_unit, usbd_errstr(err));
923                 KUE_UNLOCK(sc);
924                 return;
925         }
926
927         /* Start up the receive pipe. */
928         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
929                 c = &sc->kue_cdata.kue_rx_chain[i];
930                 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
931                     c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
932                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
933                 usbd_transfer(c->kue_xfer);
934         }
935
936         ifp->if_flags |= IFF_RUNNING;
937         ifp->if_flags &= ~IFF_OACTIVE;
938
939         KUE_UNLOCK(sc);
940
941         return;
942 }
943
944 Static int
945 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
946 {
947         struct kue_softc        *sc = ifp->if_softc;
948         int                     error = 0;
949
950         KUE_LOCK(sc);
951
952         switch(command) {
953         case SIOCSIFFLAGS:
954                 if (ifp->if_flags & IFF_UP) {
955                         if (ifp->if_flags & IFF_RUNNING &&
956                             ifp->if_flags & IFF_PROMISC &&
957                             !(sc->kue_if_flags & IFF_PROMISC)) {
958                                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
959                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
960                                     sc->kue_rxfilt);
961                         } else if (ifp->if_flags & IFF_RUNNING &&
962                             !(ifp->if_flags & IFF_PROMISC) &&
963                             sc->kue_if_flags & IFF_PROMISC) {
964                                 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
965                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
966                                     sc->kue_rxfilt);
967                         } else if (!(ifp->if_flags & IFF_RUNNING))
968                                 kue_init(sc);
969                 } else {
970                         if (ifp->if_flags & IFF_RUNNING)
971                                 kue_stop(sc);
972                 }
973                 sc->kue_if_flags = ifp->if_flags;
974                 error = 0;
975                 break;
976         case SIOCADDMULTI:
977         case SIOCDELMULTI:
978                 kue_setmulti(sc);
979                 error = 0;
980                 break;
981         default:
982                 error = ether_ioctl(ifp, command, data);
983                 break;
984         }
985
986         KUE_UNLOCK(sc);
987
988         return(error);
989 }
990
991 Static void
992 kue_watchdog(struct ifnet *ifp)
993 {
994         struct kue_softc        *sc;
995         struct kue_chain        *c;
996         usbd_status             stat;
997
998         sc = ifp->if_softc;
999         KUE_LOCK(sc);
1000         ifp->if_oerrors++;
1001         printf("kue%d: watchdog timeout\n", sc->kue_unit);
1002
1003         c = &sc->kue_cdata.kue_tx_chain[0];
1004         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1005         kue_txeof(c->kue_xfer, c, stat);
1006
1007         if (ifq_is_empty(&ifp->if_snd))
1008                 kue_start(ifp);
1009         KUE_UNLOCK(sc);
1010
1011         return;
1012 }
1013
1014 /*
1015  * Stop the adapter and free any mbufs allocated to the
1016  * RX and TX lists.
1017  */
1018 Static void
1019 kue_stop(struct kue_softc *sc)
1020 {
1021         usbd_status             err;
1022         struct ifnet            *ifp;
1023         int                     i;
1024
1025         KUE_LOCK(sc);
1026         ifp = &sc->arpcom.ac_if;
1027         ifp->if_timer = 0;
1028
1029         /* Stop transfers. */
1030         if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1031                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1032                 if (err) {
1033                         printf("kue%d: abort rx pipe failed: %s\n",
1034                         sc->kue_unit, usbd_errstr(err));
1035                 }
1036                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1037                 if (err) {
1038                         printf("kue%d: close rx pipe failed: %s\n",
1039                         sc->kue_unit, usbd_errstr(err));
1040                 }
1041                 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1042         }
1043
1044         if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1045                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1046                 if (err) {
1047                         printf("kue%d: abort tx pipe failed: %s\n",
1048                         sc->kue_unit, usbd_errstr(err));
1049                 }
1050                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1051                 if (err) {
1052                         printf("kue%d: close tx pipe failed: %s\n",
1053                             sc->kue_unit, usbd_errstr(err));
1054                 }
1055                 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1056         }
1057
1058         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1059                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1060                 if (err) {
1061                         printf("kue%d: abort intr pipe failed: %s\n",
1062                         sc->kue_unit, usbd_errstr(err));
1063                 }
1064                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1065                 if (err) {
1066                         printf("kue%d: close intr pipe failed: %s\n",
1067                             sc->kue_unit, usbd_errstr(err));
1068                 }
1069                 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1070         }
1071
1072         /* Free RX resources. */
1073         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1074                 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
1075                         kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
1076                         sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1077                 }
1078                 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1079                         m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1080                         sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1081                 }
1082                 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1083                         usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1084                         sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1085                 }
1086         }
1087
1088         /* Free TX resources. */
1089         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1090                 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
1091                         kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
1092                         sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1093                 }
1094                 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1095                         m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1096                         sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1097                 }
1098                 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1099                         usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1100                         sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1101                 }
1102         }
1103
1104         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1105         KUE_UNLOCK(sc);
1106
1107         return;
1108 }
1109
1110 /*
1111  * Stop all chip I/O so that the kernel's probe routines don't
1112  * get confused by errant DMAs when rebooting.
1113  */
1114 Static void
1115 kue_shutdown(device_ptr_t dev)
1116 {
1117         struct kue_softc        *sc;
1118
1119         sc = device_get_softc(dev);
1120
1121         kue_stop(sc);
1122
1123         return;
1124 }