if_kue.c revision 1.58.10.5 1 /* $NetBSD: if_kue.c,v 1.58.10.5 2007/06/22 10:44:56 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.5 2007/06/22 10:44:56 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 #ifdef __FreeBSD__ /* callback needs context */
721 | USBD_CALLBACK_AS_TASK
722 #endif
723 ,
724 USBD_NO_TIMEOUT, kue_rxeof);
725 usbd_transfer(c->ue_xfer);
726
727 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
728 __func__));
729 }
730
731 /*
732 * A frame was downloaded to the chip. It's safe for us to clean up
733 * the list buffers.
734 */
735
736 Static void
737 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
738 usbd_status status)
739 {
740 struct ue_chain *c = priv;
741 struct kue_softc *sc = (void *)c->ue_dev;
742 struct ifnet *ifp = GET_IFP(sc);
743 int s;
744
745 if (sc->kue_dying)
746 return;
747
748 s = splnet();
749
750 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
751 __func__, status));
752
753 ifp->if_timer = 0;
754 ifp->if_flags &= ~IFF_OACTIVE;
755
756 usbd_unmap_buffer(xfer);
757
758 if (status != USBD_NORMAL_COMPLETION) {
759 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
760 splx(s);
761 return;
762 }
763 ifp->if_oerrors++;
764 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
765 usbd_errstr(status));
766 if (status == USBD_STALLED)
767 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
768 splx(s);
769 return;
770 }
771
772 ifp->if_opackets++;
773
774 m_freem(c->ue_mbuf);
775 c->ue_mbuf = NULL;
776
777 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
778 kue_start(ifp);
779
780 splx(s);
781 }
782
783 Static int
784 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
785 {
786 int total_len;
787 struct ue_chain *c;
788 usbd_status err;
789 int ret;
790
791 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
792
793 c = &sc->kue_cdata.kue_tx_chain[idx];
794
795 /* Prepend two bytes at the beginning to hold the frame length. */
796 M_PREPEND(m, 2, M_DONTWAIT);
797 if (m != NULL)
798 m = m_pullup(m, 2); /* just in case */
799 if (m == NULL) {
800 GET_IFP(sc)->if_oerrors++;
801 return (ENOBUFS);
802 }
803
804 /*
805 * Copy the mbuf data into a contiguous buffer, leaving two
806 * bytes at the beginning to hold the frame length.
807 */
808 c->ue_mbuf = m;
809
810 /* Frame length is specified in the first 2 bytes of the buffer. */
811 USETW(mtod(m, char *), m->m_pkthdr.len - 2);
812
813 total_len = m->m_pkthdr.len;
814 /* XXX what's this? */
815 total_len += 64 - (total_len % 64);
816 m_copyback(m, total_len - 1, 1, ""); /* expand mbuf chain */
817 if (m->m_pkthdr.len != total_len) {
818 m_freem(m);
819 return (ENOBUFS);
820 }
821
822 ret = usb_ether_map_tx_buffer_mbuf(c, m);
823 if (ret) {
824 m_freem(m);
825 return (ret);
826 }
827
828 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_TX],
829 c, NULL /* XXX buf */, total_len, USBD_NO_COPY
830 #ifdef __FreeBSD__ /* callback needs context */
831 | USBD_CALLBACK_AS_TASK
832 #endif
833 , USBD_DEFAULT_TIMEOUT,
834 kue_txeof);
835
836 /* Transmit */
837 err = usbd_transfer(c->ue_xfer);
838 if (err != USBD_IN_PROGRESS) {
839 c->ue_mbuf = NULL;
840 m_freem(m);
841 printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev),
842 usbd_errstr(err));
843 kue_stop(GET_IFP(sc), 0);
844 return (EIO);
845 }
846
847 sc->kue_cdata.kue_tx_cnt++;
848
849 return (0);
850 }
851
852 Static void
853 kue_start(struct ifnet *ifp)
854 {
855 struct kue_softc *sc = ifp->if_softc;
856 struct mbuf *m_head = NULL;
857
858 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
859
860 if (sc->kue_dying)
861 return;
862
863 if (ifp->if_flags & IFF_OACTIVE)
864 return;
865
866 IFQ_POLL(&ifp->if_snd, m_head);
867 if (m_head == NULL)
868 return;
869
870 IFQ_DEQUEUE(&ifp->if_snd, m_head);
871
872 #if NBPFILTER > 0
873 /*
874 * If there's a BPF listener, bounce a copy of this frame
875 * to him.
876 */
877 if (ifp->if_bpf)
878 bpf_mtap(ifp->if_bpf, m_head);
879 #endif
880
881 if (kue_send(sc, m_head, 0)) {
882 ifp->if_flags |= IFF_OACTIVE;
883 return;
884 }
885
886 ifp->if_flags |= IFF_OACTIVE;
887
888 /*
889 * Set a timeout in case the chip goes out to lunch.
890 */
891 ifp->if_timer = 6;
892 }
893
894 Static int
895 kue_init(struct ifnet *ifp)
896 {
897 struct kue_softc *sc = ifp->if_softc;
898 int s;
899 u_char *eaddr;
900 struct ue_chain *c;
901 int i;
902
903 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
904
905 if (ifp->if_flags & IFF_RUNNING)
906 return (EIO);
907
908 s = splnet();
909
910 #if defined(__NetBSD__)
911 eaddr = LLADDR(ifp->if_sadl);
912 #else
913 eaddr = sc->arpcom.ac_enaddr;
914 #endif /* defined(__NetBSD__) */
915 /* Set MAC address */
916 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
917
918 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
919
920 /* If we want promiscuous mode, set the allframes bit. */
921 if (ifp->if_flags & IFF_PROMISC)
922 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
923
924 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
925
926 /* I'm not sure how to tune these. */
927 #if 0
928 /*
929 * Leave this one alone for now; setting it
930 * wrong causes lockups on some machines/controllers.
931 */
932 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
933 #endif
934 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
935
936 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
937 if (kue_open_pipes(sc)) {
938 splx(s);
939 return (EIO);
940 }
941 }
942
943 /* Init TX ring. */
944 if ((i = usb_ether_tx_list_init(USBDEV(sc->kue_dev),
945 sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT,
946 sc->kue_udev, sc->kue_ep[KUE_ENDPT_TX], NULL))) {
947 printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
948 splx(s);
949 return (i);
950 }
951
952 /* Init RX ring. */
953 if ((i = usb_ether_rx_list_init(USBDEV(sc->kue_dev),
954 sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT,
955 sc->kue_udev, sc->kue_ep[KUE_ENDPT_RX]))) {
956 printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
957 splx(s);
958 return (i);
959 }
960
961 /* Load the multicast filter. */
962 kue_setmulti(sc);
963
964 /* Start up the receive pipe. */
965 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
966 c = &sc->kue_cdata.kue_rx_chain[i];
967 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
968 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
969 c, NULL /* XXX buf */, KUE_BUFSZ,
970 USBD_SHORT_XFER_OK | USBD_NO_COPY
971 #ifdef __FreeBSD__ /* callback needs context */
972 | USBD_CALLBACK_AS_TASK
973 #endif
974 , USBD_NO_TIMEOUT,
975 kue_rxeof);
976 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
977 __func__));
978 usbd_transfer(c->ue_xfer);
979 }
980
981 ifp->if_flags |= IFF_RUNNING;
982 ifp->if_flags &= ~IFF_OACTIVE;
983
984 splx(s);
985 return (0);
986 }
987
988 Static int
989 kue_open_pipes(struct kue_softc *sc)
990 {
991 usbd_status err;
992
993 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
994
995 /* Open RX and TX pipes. */
996 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
997 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
998 if (err) {
999 printf("%s: open rx pipe failed: %s\n",
1000 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1001 return (EIO);
1002 }
1003
1004 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
1005 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
1006 if (err) {
1007 printf("%s: open tx pipe failed: %s\n",
1008 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1009 return (EIO);
1010 }
1011
1012 return (0);
1013 }
1014
1015 Static int
1016 kue_ioctl(struct ifnet *ifp, u_long command, usb_ioctlarg_t data)
1017 {
1018 struct kue_softc *sc = ifp->if_softc;
1019 #if 0
1020 struct ifaddr *ifa = (struct ifaddr *)data;
1021 struct ifreq *ifr = (struct ifreq *)data;
1022 #endif
1023 int s, error = 0;
1024
1025 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1026
1027 if (sc->kue_dying)
1028 return (EIO);
1029
1030 #ifdef DIAGNOSTIC
1031 if (!curproc) {
1032 printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev));
1033 return EIO;
1034 }
1035 #endif
1036
1037 s = splnet();
1038
1039 switch(command) {
1040 #if 0
1041 case SIOCSIFADDR:
1042 ifp->if_flags |= IFF_UP;
1043 kue_init(ifp);
1044
1045 switch (ifa->ifa_addr->sa_family) {
1046 #ifdef INET
1047 case AF_INET:
1048 #if defined(__NetBSD__)
1049 arp_ifinit(ifp, ifa);
1050 #else
1051 arp_ifinit(&sc->arpcom, ifa);
1052 #endif
1053 break;
1054 #endif /* INET */
1055 }
1056 break;
1057
1058 case SIOCSIFMTU:
1059 if (ifr->ifr_mtu > ETHERMTU)
1060 error = EINVAL;
1061 else
1062 ifp->if_mtu = ifr->ifr_mtu;
1063 break;
1064 #endif
1065
1066 case SIOCSIFFLAGS:
1067 if (ifp->if_flags & IFF_UP) {
1068 if (ifp->if_flags & IFF_RUNNING &&
1069 ifp->if_flags & IFF_PROMISC &&
1070 !(sc->kue_if_flags & IFF_PROMISC)) {
1071 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1072 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1073 sc->kue_rxfilt);
1074 } else if (ifp->if_flags & IFF_RUNNING &&
1075 !(ifp->if_flags & IFF_PROMISC) &&
1076 sc->kue_if_flags & IFF_PROMISC) {
1077 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1078 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1079 sc->kue_rxfilt);
1080 } else if (!(ifp->if_flags & IFF_RUNNING))
1081 kue_init(ifp);
1082 } else {
1083 if (ifp->if_flags & IFF_RUNNING)
1084 kue_stop(ifp, 1);
1085 }
1086 sc->kue_if_flags = ifp->if_flags;
1087 error = 0;
1088 break;
1089 #if 0
1090 case SIOCADDMULTI:
1091 case SIOCDELMULTI:
1092 kue_setmulti(sc);
1093 error = 0;
1094 break;
1095 #endif
1096 default:
1097 error = ether_ioctl(ifp, command, data);
1098 if (error == ENETRESET) {
1099 /*
1100 * Multicast list has changed; set the hardware
1101 * filter accordingly.
1102 */
1103 if (ifp->if_flags & IFF_RUNNING)
1104 kue_setmulti(sc);
1105 error = 0;
1106 }
1107 break;
1108 }
1109
1110 splx(s);
1111
1112 return (error);
1113 }
1114
1115 Static void
1116 kue_watchdog(struct ifnet *ifp)
1117 {
1118 struct kue_softc *sc = ifp->if_softc;
1119 struct ue_chain *c;
1120 usbd_status stat;
1121 int s;
1122
1123 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1124
1125 if (sc->kue_dying)
1126 return;
1127
1128 ifp->if_oerrors++;
1129 printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
1130
1131 s = splusb();
1132 c = &sc->kue_cdata.kue_tx_chain[0];
1133 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1134 kue_txeof(c->ue_xfer, c, stat);
1135
1136 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1137 kue_start(ifp);
1138 splx(s);
1139 }
1140
1141 /*
1142 * Stop the adapter and free any mbufs allocated to the
1143 * RX and TX lists.
1144 */
1145 Static void
1146 kue_stop(struct ifnet *ifp, int disable)
1147 {
1148 struct kue_softc *sc = ifp->if_softc;
1149 usbd_status err;
1150
1151 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1152
1153 ifp = GET_IFP(sc);
1154 ifp->if_timer = 0;
1155
1156 /* Stop transfers. */
1157 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1158 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1159 if (err) {
1160 printf("%s: abort rx pipe failed: %s\n",
1161 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1162 }
1163 }
1164
1165 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1166 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1167 if (err) {
1168 printf("%s: abort tx pipe failed: %s\n",
1169 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1170 }
1171 }
1172
1173 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1174 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1175 if (err) {
1176 printf("%s: abort intr pipe failed: %s\n",
1177 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1178 }
1179 }
1180
1181 /* Free RX/TX resources. */
1182 usb_ether_rx_list_free(sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT);
1183 usb_ether_tx_list_free(sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT);
1184
1185 /* Close pipes. */
1186 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1187 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1188 if (err) {
1189 printf("%s: close rx pipe failed: %s\n",
1190 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1191 }
1192 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1193 }
1194
1195 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1196 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1197 if (err) {
1198 printf("%s: close tx pipe failed: %s\n",
1199 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1200 }
1201 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1202 }
1203
1204 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1205 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1206 if (err) {
1207 printf("%s: close intr pipe failed: %s\n",
1208 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1209 }
1210 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1211 }
1212
1213 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1214 }
1215