if_kue.c revision 1.58.10.4 1 /* $NetBSD: if_kue.c,v 1.58.10.4 2007/06/18 13:42:04 itohy Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
34 */
35
36 /*
37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38 *
39 * Written by Bill Paul <wpaul (at) 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 /*
69 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.58.10.4 2007/06/18 13:42:04 itohy Exp $");
74
75 #if defined(__NetBSD__)
76 #include "opt_inet.h"
77 #include "bpfilter.h"
78 #include "rnd.h"
79 #elif defined(__OpenBSD__)
80 #include "bpfilter.h"
81 #endif
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/sockio.h>
86 #include <sys/mbuf.h>
87 #include <sys/malloc.h>
88 #include <sys/kernel.h>
89 #include <sys/socket.h>
90 #include <sys/device.h>
91 #include <sys/proc.h>
92
93 #if NRND > 0
94 #include <sys/rnd.h>
95 #endif
96
97 #include <net/if.h>
98 #if defined(__NetBSD__)
99 #include <net/if_arp.h>
100 #endif
101 #include <net/if_dl.h>
102
103 #if NBPFILTER > 0
104 #include <net/bpf.h>
105 #endif
106
107 #if defined(__NetBSD__)
108 #include <net/if_ether.h>
109 #ifdef INET
110 #include <netinet/in.h>
111 #include <netinet/if_inarp.h>
112 #endif
113 #endif /* defined (__NetBSD__) */
114
115 #if defined(__OpenBSD__)
116 #ifdef INET
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip.h>
121 #include <netinet/if_ether.h>
122 #endif
123 #endif /* defined (__OpenBSD__) */
124
125
126 #include <dev/usb/usb.h>
127 #include <dev/usb/usbdi.h>
128 #include <dev/usb/usbdi_util.h>
129 #include <dev/usb/usbdevs.h>
130 #include <dev/usb/usb_ethersubr.h>
131
132 #include <dev/usb/if_kuereg.h>
133 #include <dev/usb/kue_fw.h>
134
135 #ifdef KUE_DEBUG
136 #define DPRINTF(x) if (kuedebug) logprintf x
137 #define DPRINTFN(n,x) if (kuedebug >= (n)) logprintf x
138 int kuedebug = 0;
139 #else
140 #define DPRINTF(x)
141 #define DPRINTFN(n,x)
142 #endif
143
144 /*
145 * Various supported device vendors/products.
146 */
147 Static const struct usb_devno kue_devs[] = {
148 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
149 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
150 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
151 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
152 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
153 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
154 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
155 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
156 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
157 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
158 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
159 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
160 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
161 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
162 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
163 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
164 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
165 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
166 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
167 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
168 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
169 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
170 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
171 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
172 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
173 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
174 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
175 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
176 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
177 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
178 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
179 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
180 };
181 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
182
183 USB_DECLARE_DRIVER(kue);
184
185 Static int kue_send(struct kue_softc *, struct mbuf *, int);
186 Static int kue_open_pipes(struct kue_softc *);
187 Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
188 Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
189 Static void kue_start(struct ifnet *);
190 Static int kue_ioctl(struct ifnet *, u_long, usb_ioctlarg_t);
191 Static int kue_init(struct ifnet *);
192 Static void kue_stop(struct ifnet *, int);
193 Static void kue_watchdog(struct ifnet *);
194
195 Static void kue_setmulti(struct kue_softc *);
196 Static void kue_reset(struct kue_softc *);
197
198 Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
199 u_int16_t, void *, u_int32_t);
200 Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
201 Static int kue_load_fw(struct kue_softc *);
202
203 Static usbd_status
204 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
205 {
206 usb_device_request_t req;
207
208 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
209
210 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
211 req.bRequest = breq;
212 USETW(req.wValue, word);
213 USETW(req.wIndex, 0);
214 USETW(req.wLength, 0);
215
216 return (usbd_do_request(sc->kue_udev, &req, NULL));
217 }
218
219 Static usbd_status
220 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
221 void *data, u_int32_t len)
222 {
223 usb_device_request_t req;
224
225 DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc->kue_dev),
226 __func__, len));
227
228 if (rw == KUE_CTL_WRITE)
229 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
230 else
231 req.bmRequestType = UT_READ_VENDOR_DEVICE;
232
233 req.bRequest = breq;
234 USETW(req.wValue, val);
235 USETW(req.wIndex, 0);
236 USETW(req.wLength, len);
237
238 return (usbd_do_request(sc->kue_udev, &req, data));
239 }
240
241 Static int
242 kue_load_fw(struct kue_softc *sc)
243 {
244 usb_device_descriptor_t dd;
245 usbd_status err;
246
247 DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
248
249 /*
250 * First, check if we even need to load the firmware.
251 * If the device was still attached when the system was
252 * rebooted, it may already have firmware loaded in it.
253 * If this is the case, we don't need to do it again.
254 * And in fact, if we try to load it again, we'll hang,
255 * so we have to avoid this condition if we don't want
256 * to look stupid.
257 *
258 * We can test this quickly by checking the bcdRevision
259 * code. The NIC will return a different revision code if
260 * it's probed while the firmware is still loaded and
261 * running.
262 */
263 if (usbd_get_device_desc(sc->kue_udev, &dd))
264 return (EIO);
265 if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
266 printf("%s: warm boot, no firmware download\n",
267 USBDEVNAME(sc->kue_dev));
268 return (0);
269 }
270
271 printf("%s: cold boot, downloading firmware\n",
272 USBDEVNAME(sc->kue_dev));
273
274 /* Load code segment */
275 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
276 USBDEVNAME(sc->kue_dev)));
277 /*XXXUNCONST*/
278 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
279 0, __UNCONST(kue_code_seg), sizeof(kue_code_seg));
280 if (err) {
281 printf("%s: failed to load code segment: %s\n",
282 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
283 return (EIO);
284 }
285
286 /* Load fixup segment */
287 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
288 USBDEVNAME(sc->kue_dev)));
289 /*XXXUNCONST*/
290 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
291 0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg));
292 if (err) {
293 printf("%s: failed to load fixup segment: %s\n",
294 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
295 return (EIO);
296 }
297
298 /* Send trigger command. */
299 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
300 USBDEVNAME(sc->kue_dev)));
301 /*XXXUNCONST*/
302 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
303 0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg));
304 if (err) {
305 printf("%s: failed to load trigger segment: %s\n",
306 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
307 return (EIO);
308 }
309
310 usbd_delay_ms(sc->kue_udev, 10);
311
312 /*
313 * Reload device descriptor.
314 * Why? The chip without the firmware loaded returns
315 * one revision code. The chip with the firmware
316 * loaded and running returns a *different* revision
317 * code. This confuses the quirk mechanism, which is
318 * dependent on the revision data.
319 */
320 (void)usbd_reload_device_desc(sc->kue_udev);
321
322 DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc->kue_dev), __func__));
323
324 /* Reset the adapter. */
325 kue_reset(sc);
326
327 return (0);
328 }
329
330 Static void
331 kue_setmulti(struct kue_softc *sc)
332 {
333 struct ifnet *ifp = GET_IFP(sc);
334 struct ether_multi *enm;
335 struct ether_multistep step;
336 int i;
337
338 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
339
340 if (ifp->if_flags & IFF_PROMISC) {
341 allmulti:
342 ifp->if_flags |= IFF_ALLMULTI;
343 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
344 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
345 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
346 return;
347 }
348
349 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
350
351 i = 0;
352 #if defined (__NetBSD__)
353 ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
354 #else
355 ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
356 #endif
357 while (enm != NULL) {
358 if (i == KUE_MCFILTCNT(sc) ||
359 memcmp(enm->enm_addrlo, enm->enm_addrhi,
360 ETHER_ADDR_LEN) != 0)
361 goto allmulti;
362
363 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
364 ETHER_NEXT_MULTI(step, enm);
365 i++;
366 }
367
368 ifp->if_flags &= ~IFF_ALLMULTI;
369
370 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
371 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
372 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
373
374 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
375 }
376
377 /*
378 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
379 * done after the firmware is loaded into the adapter in order to
380 * bring it into proper operation.
381 */
382 Static void
383 kue_reset(struct kue_softc *sc)
384 {
385 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
386
387 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
388 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
389 &sc->kue_iface))
390 printf("%s: reset failed\n", USBDEVNAME(sc->kue_dev));
391
392 /* Wait a little while for the chip to get its brains in order. */
393 usbd_delay_ms(sc->kue_udev, 10);
394 }
395
396 /*
397 * Probe for a KLSI chip.
398 */
399 USB_MATCH(kue)
400 {
401 USB_MATCH_START(kue, uaa);
402
403 DPRINTFN(25,("kue_match: enter\n"));
404
405 #ifndef USB_USE_IFATTACH
406 if (uaa->iface != NULL)
407 return (UMATCH_NONE);
408 #endif /* USB_USE_IFATTACH */
409
410 return (kue_lookup(uaa->vendor, uaa->product) != NULL ?
411 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
412 }
413
414 /*
415 * Attach the interface. Allocate softc structures, do
416 * setup and ethernet/BPF attach.
417 */
418 USB_ATTACH(kue)
419 {
420 USB_ATTACH_START(kue, sc, uaa);
421 char *devinfop;
422 int s;
423 struct ifnet *ifp;
424 usbd_device_handle dev = uaa->device;
425 usbd_interface_handle iface;
426 usbd_status err;
427 usb_interface_descriptor_t *id;
428 usb_endpoint_descriptor_t *ed;
429 int i;
430
431 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
432
433 devinfop = usbd_devinfo_alloc(dev, 0);
434 USB_ATTACH_SETUP;
435 printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfop);
436 usbd_devinfo_free(devinfop);
437
438 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
439 if (err) {
440 printf("%s: setting config no failed\n",
441 USBDEVNAME(sc->kue_dev));
442 USB_ATTACH_ERROR_RETURN;
443 }
444
445 sc->kue_udev = dev;
446 sc->kue_product = uaa->product;
447 sc->kue_vendor = uaa->vendor;
448
449 /* Load the firmware into the NIC. */
450 if (kue_load_fw(sc)) {
451 printf("%s: loading firmware failed\n",
452 USBDEVNAME(sc->kue_dev));
453 USB_ATTACH_ERROR_RETURN;
454 }
455
456 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
457 if (err) {
458 printf("%s: getting interface handle failed\n",
459 USBDEVNAME(sc->kue_dev));
460 USB_ATTACH_ERROR_RETURN;
461 }
462
463 sc->kue_iface = iface;
464 id = usbd_get_interface_descriptor(iface);
465
466 /* Find endpoints. */
467 for (i = 0; i < id->bNumEndpoints; i++) {
468 ed = usbd_interface2endpoint_descriptor(iface, i);
469 if (ed == NULL) {
470 printf("%s: couldn't get ep %d\n",
471 USBDEVNAME(sc->kue_dev), i);
472 USB_ATTACH_ERROR_RETURN;
473 }
474 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
475 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
476 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
477 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
478 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
479 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
480 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
481 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
482 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
483 }
484 }
485
486 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
487 printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
488 USB_ATTACH_ERROR_RETURN;
489 }
490
491 /* Read ethernet descriptor */
492 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
493 0, &sc->kue_desc, sizeof(sc->kue_desc));
494 if (err) {
495 printf("%s: could not read Ethernet descriptor\n",
496 USBDEVNAME(sc->kue_dev));
497 USB_ATTACH_ERROR_RETURN;
498 }
499
500 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
501 M_USBDEV, M_NOWAIT);
502 if (sc->kue_mcfilters == NULL) {
503 printf("%s: no memory for multicast filter buffer\n",
504 USBDEVNAME(sc->kue_dev));
505 USB_ATTACH_ERROR_RETURN;
506 }
507
508 s = splnet();
509
510 /*
511 * A KLSI chip was detected. Inform the world.
512 */
513 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
514 ether_sprintf(sc->kue_desc.kue_macaddr));
515
516 /* Initialize interface info.*/
517 ifp = GET_IFP(sc);
518 ifp->if_softc = sc;
519 ifp->if_mtu = ETHERMTU;
520 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
521 ifp->if_ioctl = kue_ioctl;
522 ifp->if_start = kue_start;
523 ifp->if_init = kue_init;
524 ifp->if_stop = kue_stop;
525 ifp->if_watchdog = kue_watchdog;
526 #if defined(__OpenBSD__)
527 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
528 #endif
529 strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
530
531 IFQ_SET_READY(&ifp->if_snd);
532
533 /* Attach the interface. */
534 if_attach(ifp);
535 Ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
536 #if NRND > 0
537 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
538 RND_TYPE_NET, 0);
539 #endif
540
541 sc->kue_attached = 1;
542 splx(s);
543
544 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
545 USBDEV(sc->kue_dev));
546
547 USB_ATTACH_SUCCESS_RETURN;
548 }
549
550 USB_DETACH(kue)
551 {
552 USB_DETACH_START(kue, sc);
553 struct ifnet *ifp = GET_IFP(sc);
554 int s;
555
556 s = splusb(); /* XXX why? */
557
558 if (sc->kue_mcfilters != NULL) {
559 free(sc->kue_mcfilters, M_USBDEV);
560 sc->kue_mcfilters = NULL;
561 }
562
563 if (!sc->kue_attached) {
564 /* Detached before attached finished, so just bail out. */
565 splx(s);
566 return (0);
567 }
568
569 if (ifp->if_flags & IFF_RUNNING)
570 kue_stop(ifp, 1);
571
572 #if defined(__NetBSD__)
573 #if NRND > 0
574 rnd_detach_source(&sc->rnd_source);
575 #endif
576 ether_ifdetach(ifp);
577 #endif /* __NetBSD__ */
578
579 if_detach(ifp);
580
581 #ifdef DIAGNOSTIC
582 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
583 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
584 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
585 printf("%s: detach has active endpoints\n",
586 USBDEVNAME(sc->kue_dev));
587 #endif
588
589 sc->kue_attached = 0;
590 splx(s);
591
592 return (0);
593 }
594
595 int
596 kue_activate(device_ptr_t self, enum devact act)
597 {
598 struct kue_softc *sc = (struct kue_softc *)self;
599
600 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
601
602 switch (act) {
603 case DVACT_ACTIVATE:
604 return (EOPNOTSUPP);
605 break;
606
607 case DVACT_DEACTIVATE:
608 #if defined(__NetBSD__)
609 /* Deactivate the interface. */
610 if_deactivate(&sc->kue_ec.ec_if);
611 #endif
612 sc->kue_dying = 1;
613 break;
614 }
615 return (0);
616 }
617
618 /*
619 * A frame has been uploaded: pass the resulting mbuf chain up to
620 * the higher level protocols.
621 */
622 Static void
623 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
624 {
625 struct ue_chain *c = priv;
626 struct kue_softc *sc = (void *)c->ue_dev;
627 struct ifnet *ifp = GET_IFP(sc);
628 struct mbuf *m;
629 int total_len = 0;
630 int s;
631
632 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
633 __func__, status));
634
635 if (sc->kue_dying)
636 return;
637
638 if (!(ifp->if_flags & IFF_RUNNING))
639 return;
640
641 usbd_unmap_buffer(xfer);
642
643 if (status != USBD_NORMAL_COMPLETION) {
644 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
645 return;
646 sc->kue_rx_errs++;
647 if (usbd_ratecheck(&sc->kue_rx_notice)) {
648 printf("%s: %u usb errors on rx: %s\n",
649 USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
650 usbd_errstr(status));
651 sc->kue_rx_errs = 0;
652 }
653 if (status == USBD_STALLED)
654 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
655 goto done;
656 }
657
658 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
659
660 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
661 __func__, total_len,
662 UGETW(mtod(c->ue_mbuf, u_int8_t *))));
663
664 if (total_len <= 1)
665 goto done;
666
667 m = c->ue_mbuf;
668
669 /* No errors; receive the packet. */
670 total_len = UGETW(mtod(m, u_int8_t *));
671
672 if (total_len < sizeof(struct ether_header)) {
673 ifp->if_ierrors++;
674 goto done;
675 }
676
677 m_adj(m, sizeof(u_int16_t));
678
679 /*
680 * Allocate new mbuf cluster for the next transfer.
681 * If that failed, discard current packet and recycle the mbuf.
682 */
683 if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
684 printf("%s: no memory for rx list -- packet dropped!\n",
685 USBDEVNAME(sc->kue_dev));
686 ifp->if_ierrors++;
687 c->ue_mbuf = usb_ether_newbuf(m);
688 goto done;
689 }
690
691 ifp->if_ipackets++;
692 m->m_pkthdr.len = m->m_len = total_len;
693
694 m->m_pkthdr.rcvif = ifp;
695
696 s = splnet();
697
698 #if NBPFILTER > 0
699 /*
700 * Handle BPF listeners. Let the BPF user see the packet, but
701 * don't pass it up to the ether_input() layer unless it's
702 * a broadcast packet, multicast packet, matches our ethernet
703 * address or the interface is in promiscuous mode.
704 */
705 if (ifp->if_bpf)
706 bpf_mtap(ifp->if_bpf, m);
707 #endif
708
709 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
710 __func__, m->m_len));
711 IF_INPUT(ifp, m);
712 splx(s);
713
714 done:
715
716 /* Setup new transfer. */
717 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
718 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
719 c, NULL /* XXX buf */, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
720 USBD_NO_TIMEOUT, kue_rxeof);
721 usbd_transfer(c->ue_xfer);
722
723 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
724 __func__));
725 }
726
727 /*
728 * A frame was downloaded to the chip. It's safe for us to clean up
729 * the list buffers.
730 */
731
732 Static void
733 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
734 usbd_status status)
735 {
736 struct ue_chain *c = priv;
737 struct kue_softc *sc = (void *)c->ue_dev;
738 struct ifnet *ifp = GET_IFP(sc);
739 int s;
740
741 if (sc->kue_dying)
742 return;
743
744 s = splnet();
745
746 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
747 __func__, status));
748
749 ifp->if_timer = 0;
750 ifp->if_flags &= ~IFF_OACTIVE;
751
752 usbd_unmap_buffer(xfer);
753
754 if (status != USBD_NORMAL_COMPLETION) {
755 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
756 splx(s);
757 return;
758 }
759 ifp->if_oerrors++;
760 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
761 usbd_errstr(status));
762 if (status == USBD_STALLED)
763 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
764 splx(s);
765 return;
766 }
767
768 ifp->if_opackets++;
769
770 m_freem(c->ue_mbuf);
771 c->ue_mbuf = NULL;
772
773 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
774 kue_start(ifp);
775
776 splx(s);
777 }
778
779 Static int
780 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
781 {
782 int total_len;
783 struct ue_chain *c;
784 usbd_status err;
785 int ret;
786
787 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
788
789 c = &sc->kue_cdata.kue_tx_chain[idx];
790
791 /* Prepend two bytes at the beginning to hold the frame length. */
792 M_PREPEND(m, 2, M_DONTWAIT);
793 if (m != NULL)
794 m = m_pullup(m, 2); /* just in case */
795 if (m == NULL) {
796 GET_IFP(sc)->if_oerrors++;
797 return (ENOBUFS);
798 }
799
800 /*
801 * Copy the mbuf data into a contiguous buffer, leaving two
802 * bytes at the beginning to hold the frame length.
803 */
804 c->ue_mbuf = m;
805
806 /* Frame length is specified in the first 2 bytes of the buffer. */
807 USETW(mtod(m, char *), m->m_pkthdr.len - 2);
808
809 total_len = m->m_pkthdr.len;
810 /* XXX what's this? */
811 total_len += 64 - (total_len % 64);
812 m_copyback(m, total_len - 1, 1, ""); /* expand mbuf chain */
813 if (m->m_pkthdr.len != total_len) {
814 m_freem(m);
815 return (ENOBUFS);
816 }
817
818 ret = usb_ether_map_tx_buffer_mbuf(c, m);
819 if (ret) {
820 m_freem(m);
821 return (ret);
822 }
823
824 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_TX],
825 c, NULL /* XXX buf */, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
826 kue_txeof);
827
828 /* Transmit */
829 err = usbd_transfer(c->ue_xfer);
830 if (err != USBD_IN_PROGRESS) {
831 c->ue_mbuf = NULL;
832 m_freem(m);
833 printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev),
834 usbd_errstr(err));
835 kue_stop(GET_IFP(sc), 0);
836 return (EIO);
837 }
838
839 sc->kue_cdata.kue_tx_cnt++;
840
841 return (0);
842 }
843
844 Static void
845 kue_start(struct ifnet *ifp)
846 {
847 struct kue_softc *sc = ifp->if_softc;
848 struct mbuf *m_head = NULL;
849
850 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
851
852 if (sc->kue_dying)
853 return;
854
855 if (ifp->if_flags & IFF_OACTIVE)
856 return;
857
858 IFQ_POLL(&ifp->if_snd, m_head);
859 if (m_head == NULL)
860 return;
861
862 IFQ_DEQUEUE(&ifp->if_snd, m_head);
863
864 #if NBPFILTER > 0
865 /*
866 * If there's a BPF listener, bounce a copy of this frame
867 * to him.
868 */
869 if (ifp->if_bpf)
870 bpf_mtap(ifp->if_bpf, m_head);
871 #endif
872
873 if (kue_send(sc, m_head, 0)) {
874 ifp->if_flags |= IFF_OACTIVE;
875 return;
876 }
877
878 ifp->if_flags |= IFF_OACTIVE;
879
880 /*
881 * Set a timeout in case the chip goes out to lunch.
882 */
883 ifp->if_timer = 6;
884 }
885
886 Static int
887 kue_init(struct ifnet *ifp)
888 {
889 struct kue_softc *sc = ifp->if_softc;
890 int s;
891 u_char *eaddr;
892 struct ue_chain *c;
893 int i;
894
895 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
896
897 if (ifp->if_flags & IFF_RUNNING)
898 return (EIO);
899
900 s = splnet();
901
902 #if defined(__NetBSD__)
903 eaddr = LLADDR(ifp->if_sadl);
904 #else
905 eaddr = sc->arpcom.ac_enaddr;
906 #endif /* defined(__NetBSD__) */
907 /* Set MAC address */
908 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
909
910 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
911
912 /* If we want promiscuous mode, set the allframes bit. */
913 if (ifp->if_flags & IFF_PROMISC)
914 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
915
916 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
917
918 /* I'm not sure how to tune these. */
919 #if 0
920 /*
921 * Leave this one alone for now; setting it
922 * wrong causes lockups on some machines/controllers.
923 */
924 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
925 #endif
926 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
927
928 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
929 if (kue_open_pipes(sc)) {
930 splx(s);
931 return (EIO);
932 }
933 }
934
935 /* Init TX ring. */
936 if ((i = usb_ether_tx_list_init(USBDEV(sc->kue_dev),
937 sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT,
938 sc->kue_udev, sc->kue_ep[KUE_ENDPT_TX], NULL))) {
939 printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
940 splx(s);
941 return (i);
942 }
943
944 /* Init RX ring. */
945 if ((i = usb_ether_rx_list_init(USBDEV(sc->kue_dev),
946 sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT,
947 sc->kue_udev, sc->kue_ep[KUE_ENDPT_RX]))) {
948 printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
949 splx(s);
950 return (i);
951 }
952
953 /* Load the multicast filter. */
954 kue_setmulti(sc);
955
956 /* Start up the receive pipe. */
957 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
958 c = &sc->kue_cdata.kue_rx_chain[i];
959 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
960 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
961 c, NULL /* XXX buf */, KUE_BUFSZ,
962 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
963 kue_rxeof);
964 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
965 __func__));
966 usbd_transfer(c->ue_xfer);
967 }
968
969 ifp->if_flags |= IFF_RUNNING;
970 ifp->if_flags &= ~IFF_OACTIVE;
971
972 splx(s);
973 return (0);
974 }
975
976 Static int
977 kue_open_pipes(struct kue_softc *sc)
978 {
979 usbd_status err;
980
981 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
982
983 /* Open RX and TX pipes. */
984 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
985 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
986 if (err) {
987 printf("%s: open rx pipe failed: %s\n",
988 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
989 return (EIO);
990 }
991
992 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
993 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
994 if (err) {
995 printf("%s: open tx pipe failed: %s\n",
996 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
997 return (EIO);
998 }
999
1000 return (0);
1001 }
1002
1003 Static int
1004 kue_ioctl(struct ifnet *ifp, u_long command, usb_ioctlarg_t data)
1005 {
1006 struct kue_softc *sc = ifp->if_softc;
1007 #if 0
1008 struct ifaddr *ifa = (struct ifaddr *)data;
1009 struct ifreq *ifr = (struct ifreq *)data;
1010 #endif
1011 int s, error = 0;
1012
1013 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1014
1015 if (sc->kue_dying)
1016 return (EIO);
1017
1018 #ifdef DIAGNOSTIC
1019 if (!curproc) {
1020 printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev));
1021 return EIO;
1022 }
1023 #endif
1024
1025 s = splnet();
1026
1027 switch(command) {
1028 #if 0
1029 case SIOCSIFADDR:
1030 ifp->if_flags |= IFF_UP;
1031 kue_init(ifp);
1032
1033 switch (ifa->ifa_addr->sa_family) {
1034 #ifdef INET
1035 case AF_INET:
1036 #if defined(__NetBSD__)
1037 arp_ifinit(ifp, ifa);
1038 #else
1039 arp_ifinit(&sc->arpcom, ifa);
1040 #endif
1041 break;
1042 #endif /* INET */
1043 }
1044 break;
1045
1046 case SIOCSIFMTU:
1047 if (ifr->ifr_mtu > ETHERMTU)
1048 error = EINVAL;
1049 else
1050 ifp->if_mtu = ifr->ifr_mtu;
1051 break;
1052 #endif
1053
1054 case SIOCSIFFLAGS:
1055 if (ifp->if_flags & IFF_UP) {
1056 if (ifp->if_flags & IFF_RUNNING &&
1057 ifp->if_flags & IFF_PROMISC &&
1058 !(sc->kue_if_flags & IFF_PROMISC)) {
1059 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1060 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1061 sc->kue_rxfilt);
1062 } else if (ifp->if_flags & IFF_RUNNING &&
1063 !(ifp->if_flags & IFF_PROMISC) &&
1064 sc->kue_if_flags & IFF_PROMISC) {
1065 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1066 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1067 sc->kue_rxfilt);
1068 } else if (!(ifp->if_flags & IFF_RUNNING))
1069 kue_init(ifp);
1070 } else {
1071 if (ifp->if_flags & IFF_RUNNING)
1072 kue_stop(ifp, 1);
1073 }
1074 sc->kue_if_flags = ifp->if_flags;
1075 error = 0;
1076 break;
1077 #if 0
1078 case SIOCADDMULTI:
1079 case SIOCDELMULTI:
1080 kue_setmulti(sc);
1081 error = 0;
1082 break;
1083 #endif
1084 default:
1085 error = ether_ioctl(ifp, command, data);
1086 if (error == ENETRESET) {
1087 /*
1088 * Multicast list has changed; set the hardware
1089 * filter accordingly.
1090 */
1091 if (ifp->if_flags & IFF_RUNNING)
1092 kue_setmulti(sc);
1093 error = 0;
1094 }
1095 break;
1096 }
1097
1098 splx(s);
1099
1100 return (error);
1101 }
1102
1103 Static void
1104 kue_watchdog(struct ifnet *ifp)
1105 {
1106 struct kue_softc *sc = ifp->if_softc;
1107 struct ue_chain *c;
1108 usbd_status stat;
1109 int s;
1110
1111 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1112
1113 if (sc->kue_dying)
1114 return;
1115
1116 ifp->if_oerrors++;
1117 printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
1118
1119 s = splusb();
1120 c = &sc->kue_cdata.kue_tx_chain[0];
1121 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1122 kue_txeof(c->ue_xfer, c, stat);
1123
1124 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1125 kue_start(ifp);
1126 splx(s);
1127 }
1128
1129 /*
1130 * Stop the adapter and free any mbufs allocated to the
1131 * RX and TX lists.
1132 */
1133 Static void
1134 kue_stop(struct ifnet *ifp, int disable)
1135 {
1136 struct kue_softc *sc = ifp->if_softc;
1137 usbd_status err;
1138
1139 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1140
1141 ifp = GET_IFP(sc);
1142 ifp->if_timer = 0;
1143
1144 /* Stop transfers. */
1145 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1146 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1147 if (err) {
1148 printf("%s: abort rx pipe failed: %s\n",
1149 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1150 }
1151 }
1152
1153 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1154 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1155 if (err) {
1156 printf("%s: abort tx pipe failed: %s\n",
1157 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1158 }
1159 }
1160
1161 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1162 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1163 if (err) {
1164 printf("%s: abort intr pipe failed: %s\n",
1165 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1166 }
1167 }
1168
1169 /* Free RX/TX resources. */
1170 usb_ether_rx_list_free(sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT);
1171 usb_ether_tx_list_free(sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT);
1172
1173 /* Close pipes. */
1174 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1175 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1176 if (err) {
1177 printf("%s: close rx pipe failed: %s\n",
1178 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1179 }
1180 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1181 }
1182
1183 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1184 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1185 if (err) {
1186 printf("%s: close tx pipe failed: %s\n",
1187 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1188 }
1189 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1190 }
1191
1192 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1193 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1194 if (err) {
1195 printf("%s: close intr pipe failed: %s\n",
1196 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1197 }
1198 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1199 }
1200
1201 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1202 }
1203