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