if_kue.c revision 1.58.10.2 1 /* $NetBSD: if_kue.c,v 1.58.10.2 2007/06/13 04:14:16 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.2 2007/06/13 04:14:16 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, caddr_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 if (uaa->iface != NULL)
406 return (UMATCH_NONE);
407
408 return (kue_lookup(uaa->vendor, uaa->product) != NULL ?
409 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
410 }
411
412 /*
413 * Attach the interface. Allocate softc structures, do
414 * setup and ethernet/BPF attach.
415 */
416 USB_ATTACH(kue)
417 {
418 USB_ATTACH_START(kue, sc, uaa);
419 char *devinfop;
420 int s;
421 struct ifnet *ifp;
422 usbd_device_handle dev = uaa->device;
423 usbd_interface_handle iface;
424 usbd_status err;
425 usb_interface_descriptor_t *id;
426 usb_endpoint_descriptor_t *ed;
427 int i;
428
429 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
430
431 devinfop = usbd_devinfo_alloc(dev, 0);
432 USB_ATTACH_SETUP;
433 printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfop);
434 usbd_devinfo_free(devinfop);
435
436 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
437 if (err) {
438 printf("%s: setting config no failed\n",
439 USBDEVNAME(sc->kue_dev));
440 USB_ATTACH_ERROR_RETURN;
441 }
442
443 sc->kue_udev = dev;
444 sc->kue_product = uaa->product;
445 sc->kue_vendor = uaa->vendor;
446
447 /* Load the firmware into the NIC. */
448 if (kue_load_fw(sc)) {
449 printf("%s: loading firmware failed\n",
450 USBDEVNAME(sc->kue_dev));
451 USB_ATTACH_ERROR_RETURN;
452 }
453
454 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
455 if (err) {
456 printf("%s: getting interface handle failed\n",
457 USBDEVNAME(sc->kue_dev));
458 USB_ATTACH_ERROR_RETURN;
459 }
460
461 sc->kue_iface = iface;
462 id = usbd_get_interface_descriptor(iface);
463
464 /* Find endpoints. */
465 for (i = 0; i < id->bNumEndpoints; i++) {
466 ed = usbd_interface2endpoint_descriptor(iface, i);
467 if (ed == NULL) {
468 printf("%s: couldn't get ep %d\n",
469 USBDEVNAME(sc->kue_dev), i);
470 USB_ATTACH_ERROR_RETURN;
471 }
472 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
473 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
474 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
475 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
476 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
477 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
478 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
479 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
480 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
481 }
482 }
483
484 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
485 printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev));
486 USB_ATTACH_ERROR_RETURN;
487 }
488
489 /* Read ethernet descriptor */
490 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
491 0, &sc->kue_desc, sizeof(sc->kue_desc));
492 if (err) {
493 printf("%s: could not read Ethernet descriptor\n",
494 USBDEVNAME(sc->kue_dev));
495 USB_ATTACH_ERROR_RETURN;
496 }
497
498 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
499 M_USBDEV, M_NOWAIT);
500 if (sc->kue_mcfilters == NULL) {
501 printf("%s: no memory for multicast filter buffer\n",
502 USBDEVNAME(sc->kue_dev));
503 USB_ATTACH_ERROR_RETURN;
504 }
505
506 s = splnet();
507
508 /*
509 * A KLSI chip was detected. Inform the world.
510 */
511 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->kue_dev),
512 ether_sprintf(sc->kue_desc.kue_macaddr));
513
514 /* Initialize interface info.*/
515 ifp = GET_IFP(sc);
516 ifp->if_softc = sc;
517 ifp->if_mtu = ETHERMTU;
518 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
519 ifp->if_ioctl = kue_ioctl;
520 ifp->if_start = kue_start;
521 ifp->if_init = kue_init;
522 ifp->if_stop = kue_stop;
523 ifp->if_watchdog = kue_watchdog;
524 #if defined(__OpenBSD__)
525 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
526 #endif
527 strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
528
529 IFQ_SET_READY(&ifp->if_snd);
530
531 /* Attach the interface. */
532 if_attach(ifp);
533 Ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
534 #if NRND > 0
535 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
536 RND_TYPE_NET, 0);
537 #endif
538
539 sc->kue_attached = 1;
540 splx(s);
541
542 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
543 USBDEV(sc->kue_dev));
544
545 USB_ATTACH_SUCCESS_RETURN;
546 }
547
548 USB_DETACH(kue)
549 {
550 USB_DETACH_START(kue, sc);
551 struct ifnet *ifp = GET_IFP(sc);
552 int s;
553
554 s = splusb(); /* XXX why? */
555
556 if (sc->kue_mcfilters != NULL) {
557 free(sc->kue_mcfilters, M_USBDEV);
558 sc->kue_mcfilters = NULL;
559 }
560
561 if (!sc->kue_attached) {
562 /* Detached before attached finished, so just bail out. */
563 splx(s);
564 return (0);
565 }
566
567 if (ifp->if_flags & IFF_RUNNING)
568 kue_stop(ifp, 1);
569
570 #if defined(__NetBSD__)
571 #if NRND > 0
572 rnd_detach_source(&sc->rnd_source);
573 #endif
574 ether_ifdetach(ifp);
575 #endif /* __NetBSD__ */
576
577 if_detach(ifp);
578
579 #ifdef DIAGNOSTIC
580 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
581 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
582 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
583 printf("%s: detach has active endpoints\n",
584 USBDEVNAME(sc->kue_dev));
585 #endif
586
587 sc->kue_attached = 0;
588 splx(s);
589
590 return (0);
591 }
592
593 int
594 kue_activate(device_ptr_t self, enum devact act)
595 {
596 struct kue_softc *sc = (struct kue_softc *)self;
597
598 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
599
600 switch (act) {
601 case DVACT_ACTIVATE:
602 return (EOPNOTSUPP);
603 break;
604
605 case DVACT_DEACTIVATE:
606 #if defined(__NetBSD__)
607 /* Deactivate the interface. */
608 if_deactivate(&sc->kue_ec.ec_if);
609 #endif
610 sc->kue_dying = 1;
611 break;
612 }
613 return (0);
614 }
615
616 /*
617 * A frame has been uploaded: pass the resulting mbuf chain up to
618 * the higher level protocols.
619 */
620 Static void
621 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
622 {
623 struct ue_chain *c = priv;
624 struct kue_softc *sc = (void *)c->ue_dev;
625 struct ifnet *ifp = GET_IFP(sc);
626 struct mbuf *m;
627 int total_len = 0;
628 int s;
629
630 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
631 __func__, status));
632
633 if (sc->kue_dying)
634 return;
635
636 if (!(ifp->if_flags & IFF_RUNNING))
637 return;
638
639 usbd_unmap_buffer(xfer);
640
641 if (status != USBD_NORMAL_COMPLETION) {
642 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
643 return;
644 sc->kue_rx_errs++;
645 if (usbd_ratecheck(&sc->kue_rx_notice)) {
646 printf("%s: %u usb errors on rx: %s\n",
647 USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
648 usbd_errstr(status));
649 sc->kue_rx_errs = 0;
650 }
651 if (status == USBD_STALLED)
652 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
653 goto done;
654 }
655
656 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
657
658 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
659 __func__, total_len,
660 UGETW(mtod(c->ue_mbuf, u_int8_t *))));
661
662 if (total_len <= 1)
663 goto done;
664
665 m = c->ue_mbuf;
666
667 /* No errors; receive the packet. */
668 total_len = UGETW(mtod(m, u_int8_t *));
669
670 if (total_len < sizeof(struct ether_header)) {
671 ifp->if_ierrors++;
672 goto done;
673 }
674
675 m_adj(m, sizeof(u_int16_t));
676
677 /*
678 * Allocate new mbuf cluster for the next transfer.
679 * If that failed, discard current packet and recycle the mbuf.
680 */
681 if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
682 printf("%s: no memory for rx list -- packet dropped!\n",
683 USBDEVNAME(sc->kue_dev));
684 ifp->if_ierrors++;
685 c->ue_mbuf = usb_ether_newbuf(m);
686 goto done;
687 }
688
689 ifp->if_ipackets++;
690 m->m_pkthdr.len = m->m_len = total_len;
691
692 m->m_pkthdr.rcvif = ifp;
693
694 s = splnet();
695
696 #if NBPFILTER > 0
697 /*
698 * Handle BPF listeners. Let the BPF user see the packet, but
699 * don't pass it up to the ether_input() layer unless it's
700 * a broadcast packet, multicast packet, matches our ethernet
701 * address or the interface is in promiscuous mode.
702 */
703 if (ifp->if_bpf)
704 bpf_mtap(ifp->if_bpf, m);
705 #endif
706
707 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
708 __func__, m->m_len));
709 IF_INPUT(ifp, m);
710 splx(s);
711
712 done:
713
714 /* Setup new transfer. */
715 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
716 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
717 c, NULL /* XXX buf */, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
718 USBD_NO_TIMEOUT, kue_rxeof);
719 usbd_transfer(c->ue_xfer);
720
721 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
722 __func__));
723 }
724
725 /*
726 * A frame was downloaded to the chip. It's safe for us to clean up
727 * the list buffers.
728 */
729
730 Static void
731 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
732 usbd_status status)
733 {
734 struct ue_chain *c = priv;
735 struct kue_softc *sc = (void *)c->ue_dev;
736 struct ifnet *ifp = GET_IFP(sc);
737 int s;
738
739 if (sc->kue_dying)
740 return;
741
742 s = splnet();
743
744 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
745 __func__, status));
746
747 ifp->if_timer = 0;
748 ifp->if_flags &= ~IFF_OACTIVE;
749
750 usbd_unmap_buffer(xfer);
751
752 if (status != USBD_NORMAL_COMPLETION) {
753 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
754 splx(s);
755 return;
756 }
757 ifp->if_oerrors++;
758 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
759 usbd_errstr(status));
760 if (status == USBD_STALLED)
761 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
762 splx(s);
763 return;
764 }
765
766 ifp->if_opackets++;
767
768 m_freem(c->ue_mbuf);
769 c->ue_mbuf = NULL;
770
771 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
772 kue_start(ifp);
773
774 splx(s);
775 }
776
777 Static int
778 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
779 {
780 int total_len;
781 struct ue_chain *c;
782 usbd_status err;
783 int ret;
784
785 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
786
787 c = &sc->kue_cdata.kue_tx_chain[idx];
788
789 /* Prepend two bytes at the beginning to hold the frame length. */
790 M_PREPEND(m, 2, M_DONTWAIT);
791 if (m != NULL)
792 m = m_pullup(m, 2); /* just in case */
793 if (m == NULL) {
794 GET_IFP(sc)->if_oerrors++;
795 return (ENOBUFS);
796 }
797
798 /*
799 * Copy the mbuf data into a contiguous buffer, leaving two
800 * bytes at the beginning to hold the frame length.
801 */
802 c->ue_mbuf = m;
803
804 /* Frame length is specified in the first 2 bytes of the buffer. */
805 USETW(mtod(m, char *), m->m_pkthdr.len - 2);
806
807 total_len = m->m_pkthdr.len;
808 /* XXX what's this? */
809 total_len += 64 - (total_len % 64);
810 m_copyback(m, total_len - 1, 1, ""); /* expand mbuf chain */
811 if (m->m_pkthdr.len != total_len) {
812 m_freem(m);
813 return (ENOBUFS);
814 }
815
816 ret = usb_ether_map_tx_buffer_mbuf(c, m);
817 if (ret) {
818 m_freem(m);
819 return (ret);
820 }
821
822 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_TX],
823 c, NULL /* XXX buf */, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
824 kue_txeof);
825
826 /* Transmit */
827 err = usbd_transfer(c->ue_xfer);
828 if (err != USBD_IN_PROGRESS) {
829 c->ue_mbuf = NULL;
830 m_freem(m);
831 printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev),
832 usbd_errstr(err));
833 kue_stop(GET_IFP(sc), 0);
834 return (EIO);
835 }
836
837 sc->kue_cdata.kue_tx_cnt++;
838
839 return (0);
840 }
841
842 Static void
843 kue_start(struct ifnet *ifp)
844 {
845 struct kue_softc *sc = ifp->if_softc;
846 struct mbuf *m_head = NULL;
847
848 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
849
850 if (sc->kue_dying)
851 return;
852
853 if (ifp->if_flags & IFF_OACTIVE)
854 return;
855
856 IFQ_POLL(&ifp->if_snd, m_head);
857 if (m_head == NULL)
858 return;
859
860 IFQ_DEQUEUE(&ifp->if_snd, m_head);
861
862 #if NBPFILTER > 0
863 /*
864 * If there's a BPF listener, bounce a copy of this frame
865 * to him.
866 */
867 if (ifp->if_bpf)
868 bpf_mtap(ifp->if_bpf, m_head);
869 #endif
870
871 if (kue_send(sc, m_head, 0)) {
872 ifp->if_flags |= IFF_OACTIVE;
873 return;
874 }
875
876 ifp->if_flags |= IFF_OACTIVE;
877
878 /*
879 * Set a timeout in case the chip goes out to lunch.
880 */
881 ifp->if_timer = 6;
882 }
883
884 Static int
885 kue_init(struct ifnet *ifp)
886 {
887 struct kue_softc *sc = ifp->if_softc;
888 int s;
889 u_char *eaddr;
890 struct ue_chain *c;
891 int i;
892
893 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
894
895 if (ifp->if_flags & IFF_RUNNING)
896 return (EIO);
897
898 s = splnet();
899
900 #if defined(__NetBSD__)
901 eaddr = LLADDR(ifp->if_sadl);
902 #else
903 eaddr = sc->arpcom.ac_enaddr;
904 #endif /* defined(__NetBSD__) */
905 /* Set MAC address */
906 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
907
908 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
909
910 /* If we want promiscuous mode, set the allframes bit. */
911 if (ifp->if_flags & IFF_PROMISC)
912 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
913
914 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
915
916 /* I'm not sure how to tune these. */
917 #if 0
918 /*
919 * Leave this one alone for now; setting it
920 * wrong causes lockups on some machines/controllers.
921 */
922 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
923 #endif
924 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
925
926 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
927 if (kue_open_pipes(sc)) {
928 splx(s);
929 return (EIO);
930 }
931 }
932
933 /* Init TX ring. */
934 if ((i = usb_ether_tx_list_init(USBDEV(sc->kue_dev),
935 sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT,
936 sc->kue_udev, sc->kue_ep[KUE_ENDPT_TX], NULL))) {
937 printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
938 splx(s);
939 return (i);
940 }
941
942 /* Init RX ring. */
943 if ((i = usb_ether_rx_list_init(USBDEV(sc->kue_dev),
944 sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT,
945 sc->kue_udev, sc->kue_ep[KUE_ENDPT_RX]))) {
946 printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
947 splx(s);
948 return (i);
949 }
950
951 /* Load the multicast filter. */
952 kue_setmulti(sc);
953
954 /* Start up the receive pipe. */
955 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
956 c = &sc->kue_cdata.kue_rx_chain[i];
957 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
958 usbd_setup_xfer(c->ue_xfer, sc->kue_ep[KUE_ENDPT_RX],
959 c, NULL /* XXX buf */, KUE_BUFSZ,
960 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
961 kue_rxeof);
962 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
963 __func__));
964 usbd_transfer(c->ue_xfer);
965 }
966
967 ifp->if_flags |= IFF_RUNNING;
968 ifp->if_flags &= ~IFF_OACTIVE;
969
970 splx(s);
971 return (0);
972 }
973
974 Static int
975 kue_open_pipes(struct kue_softc *sc)
976 {
977 usbd_status err;
978
979 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
980
981 /* Open RX and TX pipes. */
982 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
983 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
984 if (err) {
985 printf("%s: open rx pipe failed: %s\n",
986 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
987 return (EIO);
988 }
989
990 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
991 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
992 if (err) {
993 printf("%s: open tx pipe failed: %s\n",
994 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
995 return (EIO);
996 }
997
998 return (0);
999 }
1000
1001 Static int
1002 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1003 {
1004 struct kue_softc *sc = ifp->if_softc;
1005 #if 0
1006 struct ifaddr *ifa = (struct ifaddr *)data;
1007 struct ifreq *ifr = (struct ifreq *)data;
1008 #endif
1009 int s, error = 0;
1010
1011 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1012
1013 if (sc->kue_dying)
1014 return (EIO);
1015
1016 #ifdef DIAGNOSTIC
1017 if (!curproc) {
1018 printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev));
1019 return EIO;
1020 }
1021 #endif
1022
1023 s = splnet();
1024
1025 switch(command) {
1026 #if 0
1027 case SIOCSIFADDR:
1028 ifp->if_flags |= IFF_UP;
1029 kue_init(ifp);
1030
1031 switch (ifa->ifa_addr->sa_family) {
1032 #ifdef INET
1033 case AF_INET:
1034 #if defined(__NetBSD__)
1035 arp_ifinit(ifp, ifa);
1036 #else
1037 arp_ifinit(&sc->arpcom, ifa);
1038 #endif
1039 break;
1040 #endif /* INET */
1041 }
1042 break;
1043
1044 case SIOCSIFMTU:
1045 if (ifr->ifr_mtu > ETHERMTU)
1046 error = EINVAL;
1047 else
1048 ifp->if_mtu = ifr->ifr_mtu;
1049 break;
1050 #endif
1051
1052 case SIOCSIFFLAGS:
1053 if (ifp->if_flags & IFF_UP) {
1054 if (ifp->if_flags & IFF_RUNNING &&
1055 ifp->if_flags & IFF_PROMISC &&
1056 !(sc->kue_if_flags & IFF_PROMISC)) {
1057 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1058 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1059 sc->kue_rxfilt);
1060 } else if (ifp->if_flags & IFF_RUNNING &&
1061 !(ifp->if_flags & IFF_PROMISC) &&
1062 sc->kue_if_flags & IFF_PROMISC) {
1063 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1064 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1065 sc->kue_rxfilt);
1066 } else if (!(ifp->if_flags & IFF_RUNNING))
1067 kue_init(ifp);
1068 } else {
1069 if (ifp->if_flags & IFF_RUNNING)
1070 kue_stop(ifp, 1);
1071 }
1072 sc->kue_if_flags = ifp->if_flags;
1073 error = 0;
1074 break;
1075 #if 0
1076 case SIOCADDMULTI:
1077 case SIOCDELMULTI:
1078 kue_setmulti(sc);
1079 error = 0;
1080 break;
1081 #endif
1082 default:
1083 error = ether_ioctl(ifp, command, data);
1084 if (error == ENETRESET) {
1085 /*
1086 * Multicast list has changed; set the hardware
1087 * filter accordingly.
1088 */
1089 if (ifp->if_flags & IFF_RUNNING)
1090 kue_setmulti(sc);
1091 error = 0;
1092 }
1093 break;
1094 }
1095
1096 splx(s);
1097
1098 return (error);
1099 }
1100
1101 Static void
1102 kue_watchdog(struct ifnet *ifp)
1103 {
1104 struct kue_softc *sc = ifp->if_softc;
1105 struct ue_chain *c;
1106 usbd_status stat;
1107 int s;
1108
1109 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1110
1111 if (sc->kue_dying)
1112 return;
1113
1114 ifp->if_oerrors++;
1115 printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
1116
1117 s = splusb();
1118 c = &sc->kue_cdata.kue_tx_chain[0];
1119 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1120 kue_txeof(c->ue_xfer, c, stat);
1121
1122 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1123 kue_start(ifp);
1124 splx(s);
1125 }
1126
1127 /*
1128 * Stop the adapter and free any mbufs allocated to the
1129 * RX and TX lists.
1130 */
1131 Static void
1132 kue_stop(struct ifnet *ifp, int disable)
1133 {
1134 struct kue_softc *sc = ifp->if_softc;
1135 usbd_status err;
1136
1137 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1138
1139 ifp = GET_IFP(sc);
1140 ifp->if_timer = 0;
1141
1142 /* Stop transfers. */
1143 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1144 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1145 if (err) {
1146 printf("%s: abort rx pipe failed: %s\n",
1147 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1148 }
1149 }
1150
1151 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1152 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1153 if (err) {
1154 printf("%s: abort tx pipe failed: %s\n",
1155 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1156 }
1157 }
1158
1159 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1160 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1161 if (err) {
1162 printf("%s: abort intr pipe failed: %s\n",
1163 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1164 }
1165 }
1166
1167 /* Free RX/TX resources. */
1168 usb_ether_rx_list_free(sc->kue_cdata.kue_rx_chain, KUE_RX_LIST_CNT);
1169 usb_ether_tx_list_free(sc->kue_cdata.kue_tx_chain, KUE_TX_LIST_CNT);
1170
1171 /* Close pipes. */
1172 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1173 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1174 if (err) {
1175 printf("%s: close rx pipe failed: %s\n",
1176 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1177 }
1178 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1179 }
1180
1181 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1182 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1183 if (err) {
1184 printf("%s: close tx pipe failed: %s\n",
1185 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1186 }
1187 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1188 }
1189
1190 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1191 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1192 if (err) {
1193 printf("%s: close intr pipe failed: %s\n",
1194 USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1195 }
1196 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1197 }
1198
1199 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1200 }
1201