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