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