]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_kue.c
This commit was generated by cvs2svn to compensate for changes in r170222,
[FreeBSD/FreeBSD.git] / sys / dev / usb / 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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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/module.h>
75 #include <sys/socket.h>
76
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82 #include <net/if_types.h>
83
84 #include <net/bpf.h>
85
86 #include <sys/bus.h>
87 #include <machine/bus.h>
88
89 #include <dev/usb/usb.h>
90 #include <dev/usb/usbdi.h>
91 #include <dev/usb/usbdi_util.h>
92 #include <dev/usb/usbdivar.h>
93 #include "usbdevs.h"
94 #include <dev/usb/usb_ethersubr.h>
95
96 #include <dev/usb/if_kuereg.h>
97 #include <dev/usb/kue_fw.h>
98
99 MODULE_DEPEND(kue, usb, 1, 1, 1);
100 MODULE_DEPEND(kue, ether, 1, 1, 1);
101
102 /*
103  * Various supported device vendors/products.
104  */
105 static struct kue_type kue_devs[] = {
106         { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
107         { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 },
108         { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
109         { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
110         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
111         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
112         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
113         { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
114         { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
115         { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
116         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
117         { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
118         { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
119         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
120         { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
121         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
122         { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
123         { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
124         { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE },
125         { 0, 0 }
126 };
127
128 static int kue_match(device_t);
129 static int kue_attach(device_t);
130 static int kue_detach(device_t);
131 static void kue_shutdown(device_t);
132 static int kue_encap(struct kue_softc *, struct mbuf *, int);
133 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
134 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
135 static void kue_start(struct ifnet *);
136 static void kue_rxstart(struct ifnet *);
137 static int kue_ioctl(struct ifnet *, u_long, caddr_t);
138 static void kue_init(void *);
139 static void kue_stop(struct kue_softc *);
140 static void kue_watchdog(struct ifnet *);
141
142 static void kue_setmulti(struct kue_softc *);
143 static void kue_reset(struct kue_softc *);
144
145 static usbd_status kue_do_request(usbd_device_handle,
146                                   usb_device_request_t *, void *);
147 static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
148                            u_int16_t, char *, int);
149 static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
150 static int kue_load_fw(struct kue_softc *);
151
152 static device_method_t kue_methods[] = {
153         /* Device interface */
154         DEVMETHOD(device_probe,         kue_match),
155         DEVMETHOD(device_attach,        kue_attach),
156         DEVMETHOD(device_detach,        kue_detach),
157         DEVMETHOD(device_shutdown,      kue_shutdown),
158
159         { 0, 0 }
160 };
161
162 static driver_t kue_driver = {
163         "kue",
164         kue_methods,
165         sizeof(struct kue_softc)
166 };
167
168 static devclass_t kue_devclass;
169
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->kue_ifp;
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_ADDR_LOCK(ifp);
329         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
330         {
331                 if (ifma->ifma_addr->sa_family != AF_LINK)
332                         continue;
333                 /*
334                  * If there are too many addresses for the
335                  * internal filter, switch over to allmulti mode.
336                  */
337                 if (i == KUE_MCFILTCNT(sc))
338                         break;
339                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
340                     KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
341                 i++;
342         }
343         IF_ADDR_UNLOCK(ifp);
344
345         if (i == KUE_MCFILTCNT(sc))
346                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
347         else {
348                 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
349                 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
350                     i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
351         }
352
353         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
354
355         return;
356 }
357
358 /*
359  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
360  * done after the firmware is loaded into the adapter in order to
361  * bring it into proper operation.
362  */
363 static void
364 kue_reset(struct kue_softc *sc)
365 {
366         if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
367             usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
368             &sc->kue_iface)) {
369                 printf("kue%d: getting interface handle failed\n",
370                     sc->kue_unit);
371         }
372
373         /* Wait a little while for the chip to get its brains in order. */
374         DELAY(1000);
375         return;
376 }
377
378 /*
379  * Probe for a KLSI chip.
380  */
381 USB_MATCH(kue)
382 {
383         USB_MATCH_START(kue, uaa);
384         struct kue_type                 *t;
385
386         if (!uaa->iface)
387                 return(UMATCH_NONE);
388
389         t = kue_devs;
390         while(t->kue_vid) {
391                 if (uaa->vendor == t->kue_vid &&
392                     uaa->product == t->kue_did) {
393                         return(UMATCH_VENDOR_PRODUCT);
394                 }
395                 t++;
396         }
397
398         return(UMATCH_NONE);
399 }
400
401 /*
402  * Attach the interface. Allocate softc structures, do
403  * setup and ethernet/BPF attach.
404  */
405 USB_ATTACH(kue)
406 {
407         USB_ATTACH_START(kue, sc, uaa);
408         char                    devinfo[1024];
409         struct ifnet            *ifp;
410         usbd_status             err;
411         usb_interface_descriptor_t      *id;
412         usb_endpoint_descriptor_t       *ed;
413         int                     i;
414
415         bzero(sc, sizeof(struct kue_softc));
416         sc->kue_dev = self;
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", device_get_nameunit(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         mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
448             MTX_DEF | MTX_RECURSE);
449         KUE_LOCK(sc);
450
451         /* Load the firmware into the NIC. */
452         if (kue_load_fw(sc)) {
453                 KUE_UNLOCK(sc);
454                 mtx_destroy(&sc->kue_mtx);
455                 USB_ATTACH_ERROR_RETURN;
456         }
457
458         /* Reset the adapter. */
459         kue_reset(sc);
460
461         /* Read ethernet descriptor */
462         err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
463             0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
464
465         sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
466             M_USBDEV, M_NOWAIT);
467
468         ifp = sc->kue_ifp = if_alloc(IFT_ETHER);
469         if (ifp == NULL) {
470                 printf("kue%d: can not if_alloc()\n", sc->kue_unit);
471                 KUE_UNLOCK(sc);
472                 mtx_destroy(&sc->kue_mtx);
473                 USB_ATTACH_ERROR_RETURN;
474         }
475         ifp->if_softc = sc;
476         if_initname(ifp, "kue", sc->kue_unit);
477         ifp->if_mtu = ETHERMTU;
478         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
479             IFF_NEEDSGIANT;
480         ifp->if_ioctl = kue_ioctl;
481         ifp->if_start = kue_start;
482         ifp->if_watchdog = kue_watchdog;
483         ifp->if_init = kue_init;
484         ifp->if_baudrate = 10000000;
485         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
486
487         sc->kue_qdat.ifp = ifp;
488         sc->kue_qdat.if_rxstart = kue_rxstart;
489
490         /*
491          * Call MI attach routine.
492          */
493         ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
494         usb_register_netisr();
495         sc->kue_dying = 0;
496
497         KUE_UNLOCK(sc);
498
499         USB_ATTACH_SUCCESS_RETURN;
500 }
501
502 static int
503 kue_detach(device_t dev)
504 {
505         struct kue_softc        *sc;
506         struct ifnet            *ifp;
507
508         sc = device_get_softc(dev);
509         KUE_LOCK(sc);
510         ifp = sc->kue_ifp;
511
512         sc->kue_dying = 1;
513
514         if (ifp != NULL) {
515                 ether_ifdetach(ifp);
516                 if_free(ifp);
517         }
518
519         if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
520                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
521         if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
522                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
523         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
524                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
525
526         if (sc->kue_mcfilters != NULL)
527                 free(sc->kue_mcfilters, M_USBDEV);
528
529         KUE_UNLOCK(sc);
530         mtx_destroy(&sc->kue_mtx);
531
532         return(0);
533 }
534
535 static void
536 kue_rxstart(struct ifnet *ifp)
537 {
538         struct kue_softc        *sc;
539         struct ue_chain *c;
540
541         sc = ifp->if_softc;
542         KUE_LOCK(sc);
543         c = &sc->kue_cdata.ue_rx_chain[sc->kue_cdata.ue_rx_prod];
544
545         c->ue_mbuf = usb_ether_newbuf();
546         if (c->ue_mbuf == NULL) {
547                 printf("%s: no memory for rx list "
548                     "-- packet dropped!\n", device_get_nameunit(sc->kue_dev));
549                 ifp->if_ierrors++;
550                 KUE_UNLOCK(sc);
551                 return;
552         }
553
554         /* Setup new transfer. */
555         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
556             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
557             USBD_NO_TIMEOUT, kue_rxeof);
558         usbd_transfer(c->ue_xfer);
559
560         KUE_UNLOCK(sc);
561
562         return;
563 }
564
565 /*
566  * A frame has been uploaded: pass the resulting mbuf chain up to
567  * the higher level protocols.
568  */
569 static void kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
570                       usbd_status status)
571 {
572         struct kue_softc        *sc;
573         struct ue_chain *c;
574         struct mbuf             *m;
575         struct ifnet            *ifp;
576         int                     total_len = 0;
577         u_int16_t               len;
578
579         c = priv;
580         sc = c->ue_sc;
581         KUE_LOCK(sc);
582         ifp = sc->kue_ifp;
583
584         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
585                 KUE_UNLOCK(sc);
586                 return;
587         }
588
589         if (status != USBD_NORMAL_COMPLETION) {
590                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
591                         KUE_UNLOCK(sc);
592                         return;
593                 }
594                 if (usbd_ratecheck(&sc->kue_rx_notice))
595                         printf("kue%d: usb error on rx: %s\n", sc->kue_unit,
596                             usbd_errstr(status));
597                 if (status == USBD_STALLED)
598                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
599                 goto done;
600         }
601
602         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
603         m = c->ue_mbuf;
604         if (total_len <= 1)
605                 goto done;
606
607         len = *mtod(m, u_int16_t *);
608         m_adj(m, sizeof(u_int16_t));
609
610         /* No errors; receive the packet. */
611         total_len = len;
612
613         if (len < sizeof(struct ether_header)) {
614                 ifp->if_ierrors++;
615                 goto done;
616         }
617
618         ifp->if_ipackets++;
619         m->m_pkthdr.rcvif = (void *)&sc->kue_qdat;
620         m->m_pkthdr.len = m->m_len = total_len;
621
622         /* Put the packet on the special USB input queue. */
623         usb_ether_input(m);
624         KUE_UNLOCK(sc);
625
626         return;
627 done:
628
629         /* Setup new transfer. */
630         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
631             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
632             USBD_NO_TIMEOUT, kue_rxeof);
633         usbd_transfer(c->ue_xfer);
634         KUE_UNLOCK(sc);
635
636         return;
637 }
638
639 /*
640  * A frame was downloaded to the chip. It's safe for us to clean up
641  * the list buffers.
642  */
643
644 static void
645 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
646 {
647         struct kue_softc        *sc;
648         struct ue_chain *c;
649         struct ifnet            *ifp;
650         usbd_status             err;
651
652         c = priv;
653         sc = c->ue_sc;
654         KUE_LOCK(sc);
655
656         ifp = sc->kue_ifp;
657         ifp->if_timer = 0;
658         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
659
660         if (status != USBD_NORMAL_COMPLETION) {
661                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
662                         KUE_UNLOCK(sc);
663                         return;
664                 }
665                 printf("kue%d: usb error on tx: %s\n", sc->kue_unit,
666                     usbd_errstr(status));
667                 if (status == USBD_STALLED)
668                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
669                 KUE_UNLOCK(sc);
670                 return;
671         }
672
673         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
674
675         if (c->ue_mbuf != NULL) {
676                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
677                 usb_tx_done(c->ue_mbuf);
678                 c->ue_mbuf = NULL;
679         }
680
681         if (err)
682                 ifp->if_oerrors++;
683         else
684                 ifp->if_opackets++;
685
686         KUE_UNLOCK(sc);
687
688         return;
689 }
690
691 static int
692 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
693 {
694         int                     total_len;
695         struct ue_chain *c;
696         usbd_status             err;
697
698         c = &sc->kue_cdata.ue_tx_chain[idx];
699
700         /*
701          * Copy the mbuf data into a contiguous buffer, leaving two
702          * bytes at the beginning to hold the frame length.
703          */
704         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf + 2);
705         c->ue_mbuf = m;
706
707         total_len = m->m_pkthdr.len + 2;
708         total_len += 64 - (total_len % 64);
709
710         /* Frame length is specified in the first 2 bytes of the buffer. */
711         c->ue_buf[0] = (u_int8_t)m->m_pkthdr.len;
712         c->ue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
713
714         usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_TX],
715             c, c->ue_buf, total_len, 0, 10000, kue_txeof);
716
717         /* Transmit */
718         err = usbd_transfer(c->ue_xfer);
719         if (err != USBD_IN_PROGRESS) {
720                 kue_stop(sc);
721                 return(EIO);
722         }
723
724         sc->kue_cdata.ue_tx_cnt++;
725
726         return(0);
727 }
728
729 static void
730 kue_start(struct ifnet *ifp)
731 {
732         struct kue_softc        *sc;
733         struct mbuf             *m_head = NULL;
734
735         sc = ifp->if_softc;
736         KUE_LOCK(sc);
737
738         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
739                 KUE_UNLOCK(sc);
740                 return;
741         }
742
743         IF_DEQUEUE(&ifp->if_snd, m_head);
744         if (m_head == NULL) {
745                 KUE_UNLOCK(sc);
746                 return;
747         }
748
749         if (kue_encap(sc, m_head, 0)) {
750                 IF_PREPEND(&ifp->if_snd, m_head);
751                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
752                 KUE_UNLOCK(sc);
753                 return;
754         }
755
756         /*
757          * If there's a BPF listener, bounce a copy of this frame
758          * to him.
759          */
760         BPF_MTAP(ifp, m_head);
761
762         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
763
764         /*
765          * Set a timeout in case the chip goes out to lunch.
766          */
767         ifp->if_timer = 5;
768         KUE_UNLOCK(sc);
769
770         return;
771 }
772
773 static void
774 kue_init(void *xsc)
775 {
776         struct kue_softc        *sc = xsc;
777         struct ifnet            *ifp = sc->kue_ifp;
778         struct ue_chain *c;
779         usbd_status             err;
780         int                     i;
781
782         KUE_LOCK(sc);
783
784         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
785                 KUE_UNLOCK(sc);
786                 return;
787         }
788
789         /* Set MAC address */
790         kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
791             0, IF_LLADDR(sc->kue_ifp), ETHER_ADDR_LEN);
792
793         sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
794
795          /* If we want promiscuous mode, set the allframes bit. */
796         if (ifp->if_flags & IFF_PROMISC)
797                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
798
799         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
800
801         /* I'm not sure how to tune these. */
802 #ifdef notdef
803         /*
804          * Leave this one alone for now; setting it
805          * wrong causes lockups on some machines/controllers.
806          */
807         kue_setword(sc, KUE_CMD_SET_SOFS, 1);
808 #endif
809         kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
810
811         /* Init TX ring. */
812         if (usb_ether_tx_list_init(sc, &sc->kue_cdata,
813             sc->kue_udev) == ENOBUFS) {
814                 printf("kue%d: tx list init failed\n", sc->kue_unit);
815                 KUE_UNLOCK(sc);
816                 return;
817         }
818
819         /* Init RX ring. */
820         if (usb_ether_rx_list_init(sc, &sc->kue_cdata,
821             sc->kue_udev) == ENOBUFS) {
822                 printf("kue%d: rx list init failed\n", sc->kue_unit);
823                 KUE_UNLOCK(sc);
824                 return;
825         }
826
827         /* Load the multicast filter. */
828         kue_setmulti(sc);
829
830         /* Open RX and TX pipes. */
831         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
832             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
833         if (err) {
834                 printf("kue%d: open rx pipe failed: %s\n",
835                     sc->kue_unit, usbd_errstr(err));
836                 KUE_UNLOCK(sc);
837                 return;
838         }
839
840         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
841             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
842         if (err) {
843                 printf("kue%d: open tx pipe failed: %s\n",
844                     sc->kue_unit, usbd_errstr(err));
845                 KUE_UNLOCK(sc);
846                 return;
847         }
848
849         /* Start up the receive pipe. */
850         for (i = 0; i < UE_RX_LIST_CNT; i++) {
851                 c = &sc->kue_cdata.ue_rx_chain[i];
852                 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
853                     c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
854                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
855                 usbd_transfer(c->ue_xfer);
856         }
857
858         ifp->if_drv_flags |= IFF_DRV_RUNNING;
859         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
860
861         KUE_UNLOCK(sc);
862
863         return;
864 }
865
866 static int
867 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
868 {
869         struct kue_softc        *sc = ifp->if_softc;
870         int                     error = 0;
871
872         KUE_LOCK(sc);
873
874         switch(command) {
875         case SIOCSIFFLAGS:
876                 if (ifp->if_flags & IFF_UP) {
877                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
878                             ifp->if_flags & IFF_PROMISC &&
879                             !(sc->kue_if_flags & IFF_PROMISC)) {
880                                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
881                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
882                                     sc->kue_rxfilt);
883                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
884                             !(ifp->if_flags & IFF_PROMISC) &&
885                             sc->kue_if_flags & IFF_PROMISC) {
886                                 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
887                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
888                                     sc->kue_rxfilt);
889                         } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
890                                 kue_init(sc);
891                 } else {
892                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
893                                 kue_stop(sc);
894                 }
895                 sc->kue_if_flags = ifp->if_flags;
896                 error = 0;
897                 break;
898         case SIOCADDMULTI:
899         case SIOCDELMULTI:
900                 kue_setmulti(sc);
901                 error = 0;
902                 break;
903         default:
904                 error = ether_ioctl(ifp, command, data);
905                 break;
906         }
907
908         KUE_UNLOCK(sc);
909
910         return(error);
911 }
912
913 static void
914 kue_watchdog(struct ifnet *ifp)
915 {
916         struct kue_softc        *sc;
917         struct ue_chain *c;
918         usbd_status             stat;
919
920         sc = ifp->if_softc;
921         KUE_LOCK(sc);
922         ifp->if_oerrors++;
923         printf("kue%d: watchdog timeout\n", sc->kue_unit);
924
925         c = &sc->kue_cdata.ue_tx_chain[0];
926         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
927         kue_txeof(c->ue_xfer, c, stat);
928
929         if (ifp->if_snd.ifq_head != NULL)
930                 kue_start(ifp);
931         KUE_UNLOCK(sc);
932
933         return;
934 }
935
936 /*
937  * Stop the adapter and free any mbufs allocated to the
938  * RX and TX lists.
939  */
940 static void
941 kue_stop(struct kue_softc *sc)
942 {
943         usbd_status             err;
944         struct ifnet            *ifp;
945
946         KUE_LOCK(sc);
947         ifp = sc->kue_ifp;
948         ifp->if_timer = 0;
949
950         /* Stop transfers. */
951         if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
952                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
953                 if (err) {
954                         printf("kue%d: abort rx pipe failed: %s\n",
955                         sc->kue_unit, usbd_errstr(err));
956                 }
957                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
958                 if (err) {
959                         printf("kue%d: close rx pipe failed: %s\n",
960                         sc->kue_unit, usbd_errstr(err));
961                 }
962                 sc->kue_ep[KUE_ENDPT_RX] = NULL;
963         }
964
965         if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
966                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
967                 if (err) {
968                         printf("kue%d: abort tx pipe failed: %s\n",
969                         sc->kue_unit, usbd_errstr(err));
970                 }
971                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
972                 if (err) {
973                         printf("kue%d: close tx pipe failed: %s\n",
974                             sc->kue_unit, usbd_errstr(err));
975                 }
976                 sc->kue_ep[KUE_ENDPT_TX] = NULL;
977         }
978
979         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
980                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
981                 if (err) {
982                         printf("kue%d: abort intr pipe failed: %s\n",
983                         sc->kue_unit, usbd_errstr(err));
984                 }
985                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
986                 if (err) {
987                         printf("kue%d: close intr pipe failed: %s\n",
988                             sc->kue_unit, usbd_errstr(err));
989                 }
990                 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
991         }
992
993         /* Free RX resources. */
994         usb_ether_rx_list_free(&sc->kue_cdata);
995         /* Free TX resources. */
996         usb_ether_tx_list_free(&sc->kue_cdata);
997
998         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
999         KUE_UNLOCK(sc);
1000
1001         return;
1002 }
1003
1004 /*
1005  * Stop all chip I/O so that the kernel's probe routines don't
1006  * get confused by errant DMAs when rebooting.
1007  */
1008 static void
1009 kue_shutdown(device_t dev)
1010 {
1011         struct kue_softc        *sc;
1012
1013         sc = device_get_softc(dev);
1014
1015         kue_stop(sc);
1016
1017         return;
1018 }