1184610Salfred/*-
2184610Salfred * Copyright (c) 1997, 1998, 1999, 2000
3184610Salfred *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred * 3. All advertising materials mentioning features or use of this software
14184610Salfred *    must display the following acknowledgement:
15184610Salfred *	This product includes software developed by Bill Paul.
16184610Salfred * 4. Neither the name of the author nor the names of any co-contributors
17184610Salfred *    may be used to endorse or promote products derived from this software
18184610Salfred *    without specific prior written permission.
19184610Salfred *
20184610Salfred * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30184610Salfred * THE POSSIBILITY OF SUCH DAMAGE.
31184610Salfred */
32184610Salfred
33184610Salfred#include <sys/cdefs.h>
34184610Salfred__FBSDID("$FreeBSD$");
35184610Salfred
36184610Salfred/*
37184610Salfred * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38184610Salfred *
39184610Salfred * Written by Bill Paul <wpaul@ee.columbia.edu>
40184610Salfred * Electrical Engineering Department
41184610Salfred * Columbia University, New York City
42184610Salfred */
43184610Salfred
44184610Salfred/*
45184610Salfred * The KLSI USB to ethernet adapter chip contains an USB serial interface,
46184610Salfred * ethernet MAC and embedded microcontroller (called the QT Engine).
47184610Salfred * The chip must have firmware loaded into it before it will operate.
48184610Salfred * Packets are passed between the chip and host via bulk transfers.
49184610Salfred * There is an interrupt endpoint mentioned in the software spec, however
50184610Salfred * it's currently unused. This device is 10Mbps half-duplex only, hence
51184610Salfred * there is no media selection logic. The MAC supports a 128 entry
52184610Salfred * multicast filter, though the exact size of the filter can depend
53184610Salfred * on the firmware. Curiously, while the software spec describes various
54184610Salfred * ethernet statistics counters, my sample adapter and firmware combination
55184610Salfred * claims not to support any statistics counters at all.
56184610Salfred *
57184610Salfred * Note that once we load the firmware in the device, we have to be
58184610Salfred * careful not to load it again: if you restart your computer but
59184610Salfred * leave the adapter attached to the USB controller, it may remain
60184610Salfred * powered on and retain its firmware. In this case, we don't need
61184610Salfred * to load the firmware a second time.
62184610Salfred *
63184610Salfred * Special thanks to Rob Furr for providing an ADS Technologies
64184610Salfred * adapter for development and testing. No monkeys were harmed during
65184610Salfred * the development of this driver.
66184610Salfred */
67184610Salfred
68194677Sthompsa#include <sys/stdint.h>
69194677Sthompsa#include <sys/stddef.h>
70194677Sthompsa#include <sys/param.h>
71194677Sthompsa#include <sys/queue.h>
72194677Sthompsa#include <sys/types.h>
73194677Sthompsa#include <sys/systm.h>
74257176Sglebius#include <sys/socket.h>
75194677Sthompsa#include <sys/kernel.h>
76194677Sthompsa#include <sys/bus.h>
77194677Sthompsa#include <sys/module.h>
78194677Sthompsa#include <sys/lock.h>
79194677Sthompsa#include <sys/mutex.h>
80194677Sthompsa#include <sys/condvar.h>
81194677Sthompsa#include <sys/sysctl.h>
82194677Sthompsa#include <sys/sx.h>
83194677Sthompsa#include <sys/unistd.h>
84194677Sthompsa#include <sys/callout.h>
85194677Sthompsa#include <sys/malloc.h>
86194677Sthompsa#include <sys/priv.h>
87194677Sthompsa
88257176Sglebius#include <net/if.h>
89257176Sglebius#include <net/if_var.h>
90257176Sglebius
91194677Sthompsa#include <dev/usb/usb.h>
92194677Sthompsa#include <dev/usb/usbdi.h>
93194677Sthompsa#include <dev/usb/usbdi_util.h>
94188746Sthompsa#include "usbdevs.h"
95184610Salfred
96184610Salfred#define	USB_DEBUG_VAR kue_debug
97194677Sthompsa#include <dev/usb/usb_debug.h>
98188942Sthompsa#include <dev/usb/usb_process.h>
99184610Salfred
100188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
101188942Sthompsa#include <dev/usb/net/if_kuereg.h>
102188942Sthompsa#include <dev/usb/net/if_kuefw.h>
103184610Salfred
104184610Salfred/*
105184610Salfred * Various supported device vendors/products.
106184610Salfred */
107223486Shselaskystatic const STRUCT_USB_HOST_ID kue_devs[] = {
108201028Sthompsa#define	KUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
109201028Sthompsa	KUE_DEV(3COM, 3C19250),
110201028Sthompsa	KUE_DEV(3COM, 3C460),
111201028Sthompsa	KUE_DEV(ABOCOM, URE450),
112201028Sthompsa	KUE_DEV(ADS, UBS10BT),
113201028Sthompsa	KUE_DEV(ADS, UBS10BTX),
114201028Sthompsa	KUE_DEV(AOX, USB101),
115201028Sthompsa	KUE_DEV(ASANTE, EA),
116201028Sthompsa	KUE_DEV(ATEN, DSB650C),
117201028Sthompsa	KUE_DEV(ATEN, UC10T),
118201028Sthompsa	KUE_DEV(COREGA, ETHER_USB_T),
119201028Sthompsa	KUE_DEV(DLINK, DSB650C),
120201028Sthompsa	KUE_DEV(ENTREGA, E45),
121201028Sthompsa	KUE_DEV(ENTREGA, XX1),
122201028Sthompsa	KUE_DEV(ENTREGA, XX2),
123201028Sthompsa	KUE_DEV(IODATA, USBETT),
124201028Sthompsa	KUE_DEV(JATON, EDA),
125201028Sthompsa	KUE_DEV(KINGSTON, XX1),
126201028Sthompsa	KUE_DEV(KLSI, DUH3E10BT),
127201028Sthompsa	KUE_DEV(KLSI, DUH3E10BTN),
128201028Sthompsa	KUE_DEV(LINKSYS, USB10T),
129201028Sthompsa	KUE_DEV(MOBILITY, EA),
130201028Sthompsa	KUE_DEV(NETGEAR, EA101),
131201028Sthompsa	KUE_DEV(NETGEAR, EA101X),
132201028Sthompsa	KUE_DEV(PERACOM, ENET),
133201028Sthompsa	KUE_DEV(PERACOM, ENET2),
134201028Sthompsa	KUE_DEV(PERACOM, ENET3),
135201028Sthompsa	KUE_DEV(PORTGEAR, EA8),
136201028Sthompsa	KUE_DEV(PORTGEAR, EA9),
137201028Sthompsa	KUE_DEV(PORTSMITH, EEA),
138201028Sthompsa	KUE_DEV(SHARK, PA),
139201028Sthompsa	KUE_DEV(SILICOM, GPE),
140201028Sthompsa	KUE_DEV(SILICOM, U2E),
141201028Sthompsa	KUE_DEV(SMC, 2102USB),
142201028Sthompsa#undef KUE_DEV
143184610Salfred};
144184610Salfred
145184610Salfred/* prototypes */
146184610Salfred
147184610Salfredstatic device_probe_t kue_probe;
148184610Salfredstatic device_attach_t kue_attach;
149184610Salfredstatic device_detach_t kue_detach;
150184610Salfred
151193045Sthompsastatic usb_callback_t kue_bulk_read_callback;
152193045Sthompsastatic usb_callback_t kue_bulk_write_callback;
153184610Salfred
154193045Sthompsastatic uether_fn_t kue_attach_post;
155193045Sthompsastatic uether_fn_t kue_init;
156193045Sthompsastatic uether_fn_t kue_stop;
157193045Sthompsastatic uether_fn_t kue_start;
158193045Sthompsastatic uether_fn_t kue_setmulti;
159193045Sthompsastatic uether_fn_t kue_setpromisc;
160184610Salfred
161188412Sthompsastatic int	kue_do_request(struct kue_softc *,
162192984Sthompsa		    struct usb_device_request *, void *);
163188412Sthompsastatic int	kue_setword(struct kue_softc *, uint8_t, uint16_t);
164188412Sthompsastatic int	kue_ctl(struct kue_softc *, uint8_t, uint8_t, uint16_t,
165188412Sthompsa		    void *, int);
166188412Sthompsastatic int	kue_load_fw(struct kue_softc *);
167188412Sthompsastatic void	kue_reset(struct kue_softc *);
168184610Salfred
169207077Sthompsa#ifdef USB_DEBUG
170184610Salfredstatic int kue_debug = 0;
171184610Salfred
172227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, kue, CTLFLAG_RW, 0, "USB kue");
173276701ShselaskySYSCTL_INT(_hw_usb_kue, OID_AUTO, debug, CTLFLAG_RWTUN, &kue_debug, 0,
174184610Salfred    "Debug level");
175184610Salfred#endif
176184610Salfred
177192984Sthompsastatic const struct usb_config kue_config[KUE_N_TRANSFER] = {
178184610Salfred
179187259Sthompsa	[KUE_BULK_DT_WR] = {
180184610Salfred		.type = UE_BULK,
181184610Salfred		.endpoint = UE_ADDR_ANY,
182184610Salfred		.direction = UE_DIR_OUT,
183190734Sthompsa		.bufsize = (MCLBYTES + 2 + 64),
184190734Sthompsa		.flags = {.pipe_bof = 1,},
185190734Sthompsa		.callback = kue_bulk_write_callback,
186190734Sthompsa		.timeout = 10000,	/* 10 seconds */
187184610Salfred	},
188184610Salfred
189187259Sthompsa	[KUE_BULK_DT_RD] = {
190184610Salfred		.type = UE_BULK,
191184610Salfred		.endpoint = UE_ADDR_ANY,
192184610Salfred		.direction = UE_DIR_IN,
193190734Sthompsa		.bufsize = (MCLBYTES + 2),
194190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
195190734Sthompsa		.callback = kue_bulk_read_callback,
196190734Sthompsa		.timeout = 0,	/* no timeout */
197184610Salfred	},
198184610Salfred};
199184610Salfred
200184610Salfredstatic device_method_t kue_methods[] = {
201184610Salfred	/* Device interface */
202184610Salfred	DEVMETHOD(device_probe, kue_probe),
203184610Salfred	DEVMETHOD(device_attach, kue_attach),
204184610Salfred	DEVMETHOD(device_detach, kue_detach),
205184610Salfred
206246128Ssbz	DEVMETHOD_END
207184610Salfred};
208184610Salfred
209184610Salfredstatic driver_t kue_driver = {
210184610Salfred	.name = "kue",
211184610Salfred	.methods = kue_methods,
212184610Salfred	.size = sizeof(struct kue_softc),
213184610Salfred};
214184610Salfred
215184610Salfredstatic devclass_t kue_devclass;
216184610Salfred
217189275SthompsaDRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, NULL, 0);
218188942SthompsaMODULE_DEPEND(kue, uether, 1, 1, 1);
219188942SthompsaMODULE_DEPEND(kue, usb, 1, 1, 1);
220184610SalfredMODULE_DEPEND(kue, ether, 1, 1, 1);
221212122SthompsaMODULE_VERSION(kue, 1);
222292080SimpUSB_PNP_HOST_INFO(kue_devs);
223184610Salfred
224192984Sthompsastatic const struct usb_ether_methods kue_ue_methods = {
225188412Sthompsa	.ue_attach_post = kue_attach_post,
226188412Sthompsa	.ue_start = kue_start,
227188412Sthompsa	.ue_init = kue_init,
228188412Sthompsa	.ue_stop = kue_stop,
229188412Sthompsa	.ue_setmulti = kue_setmulti,
230188412Sthompsa	.ue_setpromisc = kue_setpromisc,
231188412Sthompsa};
232188412Sthompsa
233184610Salfred/*
234184610Salfred * We have a custom do_request function which is almost like the
235184610Salfred * regular do_request function, except it has a much longer timeout.
236184610Salfred * Why? Because we need to make requests over the control endpoint
237184610Salfred * to download the firmware to the device, which can take longer
238184610Salfred * than the default timeout.
239184610Salfred */
240188412Sthompsastatic int
241192984Sthompsakue_do_request(struct kue_softc *sc, struct usb_device_request *req,
242184610Salfred    void *data)
243184610Salfred{
244193045Sthompsa	usb_error_t err;
245184610Salfred
246194228Sthompsa	err = uether_do_request(&sc->sc_ue, req, data, 60000);
247184610Salfred
248188412Sthompsa	return (err);
249184610Salfred}
250184610Salfred
251188412Sthompsastatic int
252188412Sthompsakue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
253184610Salfred{
254192984Sthompsa	struct usb_device_request req;
255184610Salfred
256184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
257184610Salfred	req.bRequest = breq;
258184610Salfred	USETW(req.wValue, word);
259184610Salfred	USETW(req.wIndex, 0);
260184610Salfred	USETW(req.wLength, 0);
261184610Salfred
262188412Sthompsa	return (kue_do_request(sc, &req, NULL));
263184610Salfred}
264184610Salfred
265188412Sthompsastatic int
266188412Sthompsakue_ctl(struct kue_softc *sc, uint8_t rw, uint8_t breq,
267188412Sthompsa    uint16_t val, void *data, int len)
268184610Salfred{
269192984Sthompsa	struct usb_device_request req;
270184610Salfred
271188412Sthompsa	if (rw == KUE_CTL_WRITE)
272184610Salfred		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
273188412Sthompsa	else
274184610Salfred		req.bmRequestType = UT_READ_VENDOR_DEVICE;
275184610Salfred
276188412Sthompsa
277184610Salfred	req.bRequest = breq;
278184610Salfred	USETW(req.wValue, val);
279184610Salfred	USETW(req.wIndex, 0);
280184610Salfred	USETW(req.wLength, len);
281184610Salfred
282188412Sthompsa	return (kue_do_request(sc, &req, data));
283184610Salfred}
284184610Salfred
285188412Sthompsastatic int
286188412Sthompsakue_load_fw(struct kue_softc *sc)
287184610Salfred{
288192984Sthompsa	struct usb_device_descriptor *dd;
289184610Salfred	uint16_t hwrev;
290193045Sthompsa	usb_error_t err;
291184610Salfred
292194228Sthompsa	dd = usbd_get_device_descriptor(sc->sc_ue.ue_udev);
293184610Salfred	hwrev = UGETW(dd->bcdDevice);
294184610Salfred
295184610Salfred	/*
296184610Salfred	 * First, check if we even need to load the firmware.
297184610Salfred	 * If the device was still attached when the system was
298184610Salfred	 * rebooted, it may already have firmware loaded in it.
299184610Salfred	 * If this is the case, we don't need to do it again.
300184610Salfred	 * And in fact, if we try to load it again, we'll hang,
301184610Salfred	 * so we have to avoid this condition if we don't want
302184610Salfred	 * to look stupid.
303184610Salfred	 *
304184610Salfred	 * We can test this quickly by checking the bcdRevision
305184610Salfred	 * code. The NIC will return a different revision code if
306184610Salfred	 * it's probed while the firmware is still loaded and
307184610Salfred	 * running.
308184610Salfred	 */
309188412Sthompsa	if (hwrev == 0x0202)
310188412Sthompsa		return(0);
311188412Sthompsa
312188412Sthompsa	/* Load code segment */
313188412Sthompsa	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
314188412Sthompsa	    0, kue_code_seg, sizeof(kue_code_seg));
315188412Sthompsa	if (err) {
316188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "failed to load code segment: %s\n",
317194228Sthompsa		    usbd_errstr(err));
318188412Sthompsa		return(ENXIO);
319184610Salfred	}
320184610Salfred
321188412Sthompsa	/* Load fixup segment */
322188412Sthompsa	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
323184610Salfred	    0, kue_fix_seg, sizeof(kue_fix_seg));
324188412Sthompsa	if (err) {
325188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "failed to load fixup segment: %s\n",
326194228Sthompsa		    usbd_errstr(err));
327188412Sthompsa		return(ENXIO);
328188412Sthompsa	}
329184610Salfred
330188412Sthompsa	/* Send trigger command. */
331188412Sthompsa	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
332184610Salfred	    0, kue_trig_seg, sizeof(kue_trig_seg));
333188412Sthompsa	if (err) {
334188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "failed to load trigger segment: %s\n",
335194228Sthompsa		    usbd_errstr(err));
336188412Sthompsa		return(ENXIO);
337188412Sthompsa	}
338188412Sthompsa
339188412Sthompsa	return (0);
340184610Salfred}
341184610Salfred
342184610Salfredstatic void
343192984Sthompsakue_setpromisc(struct usb_ether *ue)
344184610Salfred{
345194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
346194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
347184610Salfred
348188412Sthompsa	KUE_LOCK_ASSERT(sc, MA_OWNED);
349188412Sthompsa
350188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
351188412Sthompsa		sc->sc_rxfilt |= KUE_RXFILT_PROMISC;
352188412Sthompsa	else
353188412Sthompsa		sc->sc_rxfilt &= ~KUE_RXFILT_PROMISC;
354188412Sthompsa
355188412Sthompsa	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
356184610Salfred}
357184610Salfred
358184610Salfredstatic void
359192984Sthompsakue_setmulti(struct usb_ether *ue)
360184610Salfred{
361194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
362194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
363188412Sthompsa	struct ifmultiaddr *ifma;
364188412Sthompsa	int i = 0;
365184610Salfred
366188412Sthompsa	KUE_LOCK_ASSERT(sc, MA_OWNED);
367184610Salfred
368188412Sthompsa	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
369188412Sthompsa		sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
370188412Sthompsa		sc->sc_rxfilt &= ~KUE_RXFILT_MULTICAST;
371188412Sthompsa		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
372188412Sthompsa		return;
373184610Salfred	}
374184610Salfred
375188412Sthompsa	sc->sc_rxfilt &= ~KUE_RXFILT_ALLMULTI;
376184610Salfred
377195049Srwatson	if_maddr_rlock(ifp);
378188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
379188412Sthompsa	{
380188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
381188412Sthompsa			continue;
382188412Sthompsa		/*
383188412Sthompsa		 * If there are too many addresses for the
384188412Sthompsa		 * internal filter, switch over to allmulti mode.
385188412Sthompsa		 */
386188412Sthompsa		if (i == KUE_MCFILTCNT(sc))
387188412Sthompsa			break;
388227461Shselasky		memcpy(KUE_MCFILT(sc, i),
389227461Shselasky		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
390227461Shselasky		    ETHER_ADDR_LEN);
391188412Sthompsa		i++;
392188412Sthompsa	}
393195049Srwatson	if_maddr_runlock(ifp);
394184610Salfred
395188412Sthompsa	if (i == KUE_MCFILTCNT(sc))
396188412Sthompsa		sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
397188412Sthompsa	else {
398188412Sthompsa		sc->sc_rxfilt |= KUE_RXFILT_MULTICAST;
399188412Sthompsa		kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
400188412Sthompsa		    i, sc->sc_mcfilters, i * ETHER_ADDR_LEN);
401188412Sthompsa	}
402184610Salfred
403188412Sthompsa	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
404184610Salfred}
405184610Salfred
406184610Salfred/*
407184610Salfred * Issue a SET_CONFIGURATION command to reset the MAC. This should be
408184610Salfred * done after the firmware is loaded into the adapter in order to
409184610Salfred * bring it into proper operation.
410184610Salfred */
411184610Salfredstatic void
412188412Sthompsakue_reset(struct kue_softc *sc)
413184610Salfred{
414192984Sthompsa	struct usb_config_descriptor *cd;
415193045Sthompsa	usb_error_t err;
416184610Salfred
417194228Sthompsa	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
418184610Salfred
419194228Sthompsa	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
420184610Salfred	    cd->bConfigurationValue);
421188412Sthompsa	if (err)
422184610Salfred		DPRINTF("reset failed (ignored)\n");
423188412Sthompsa
424188412Sthompsa	/* wait a little while for the chip to get its brains in order */
425194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
426188412Sthompsa}
427188412Sthompsa
428188412Sthompsastatic void
429192984Sthompsakue_attach_post(struct usb_ether *ue)
430188412Sthompsa{
431194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
432188412Sthompsa	int error;
433188412Sthompsa
434188412Sthompsa	/* load the firmware into the NIC */
435188412Sthompsa	error = kue_load_fw(sc);
436188412Sthompsa	if (error) {
437188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "could not load firmware\n");
438188412Sthompsa		/* ignore the error */
439184610Salfred	}
440188412Sthompsa
441188412Sthompsa	/* reset the adapter */
442188412Sthompsa	kue_reset(sc);
443188412Sthompsa
444188412Sthompsa	/* read ethernet descriptor */
445188412Sthompsa	kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
446188412Sthompsa	    0, &sc->sc_desc, sizeof(sc->sc_desc));
447188412Sthompsa
448188412Sthompsa	/* copy in ethernet address */
449188412Sthompsa	memcpy(ue->ue_eaddr, sc->sc_desc.kue_macaddr, sizeof(ue->ue_eaddr));
450184610Salfred}
451184610Salfred
452184610Salfred/*
453184610Salfred * Probe for a KLSI chip.
454184610Salfred */
455184610Salfredstatic int
456184610Salfredkue_probe(device_t dev)
457184610Salfred{
458192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
459184610Salfred
460192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
461184610Salfred		return (ENXIO);
462188412Sthompsa	if (uaa->info.bConfigIndex != KUE_CONFIG_IDX)
463184610Salfred		return (ENXIO);
464188412Sthompsa	if (uaa->info.bIfaceIndex != KUE_IFACE_IDX)
465184610Salfred		return (ENXIO);
466188412Sthompsa
467194228Sthompsa	return (usbd_lookup_id_by_uaa(kue_devs, sizeof(kue_devs), uaa));
468184610Salfred}
469184610Salfred
470184610Salfred/*
471184610Salfred * Attach the interface. Allocate softc structures, do
472184610Salfred * setup and ethernet/BPF attach.
473184610Salfred */
474184610Salfredstatic int
475184610Salfredkue_attach(device_t dev)
476184610Salfred{
477192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
478184610Salfred	struct kue_softc *sc = device_get_softc(dev);
479192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
480184610Salfred	uint8_t iface_index;
481188412Sthompsa	int error;
482184610Salfred
483194228Sthompsa	device_set_usb_desc(dev);
484188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
485184610Salfred
486184610Salfred	iface_index = KUE_IFACE_IDX;
487194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
488187259Sthompsa	    sc->sc_xfer, kue_config, KUE_N_TRANSFER, sc, &sc->sc_mtx);
489184610Salfred	if (error) {
490199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
491184610Salfred		goto detach;
492184610Salfred	}
493188412Sthompsa
494188412Sthompsa	sc->sc_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
495188412Sthompsa	    M_USBDEV, M_WAITOK);
496188412Sthompsa	if (sc->sc_mcfilters == NULL) {
497199816Sthompsa		device_printf(dev, "failed allocating USB memory\n");
498184610Salfred		goto detach;
499184610Salfred	}
500184610Salfred
501188412Sthompsa	ue->ue_sc = sc;
502188412Sthompsa	ue->ue_dev = dev;
503188412Sthompsa	ue->ue_udev = uaa->device;
504188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
505188412Sthompsa	ue->ue_methods = &kue_ue_methods;
506184610Salfred
507194228Sthompsa	error = uether_ifattach(ue);
508188412Sthompsa	if (error) {
509188412Sthompsa		device_printf(dev, "could not attach interface\n");
510188412Sthompsa		goto detach;
511188412Sthompsa	}
512184610Salfred	return (0);			/* success */
513184610Salfred
514184610Salfreddetach:
515184610Salfred	kue_detach(dev);
516184610Salfred	return (ENXIO);			/* failure */
517184610Salfred}
518184610Salfred
519184610Salfredstatic int
520184610Salfredkue_detach(device_t dev)
521184610Salfred{
522184610Salfred	struct kue_softc *sc = device_get_softc(dev);
523192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
524184610Salfred
525194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, KUE_N_TRANSFER);
526194228Sthompsa	uether_ifdetach(ue);
527184610Salfred	mtx_destroy(&sc->sc_mtx);
528188412Sthompsa	free(sc->sc_mcfilters, M_USBDEV);
529184610Salfred
530184610Salfred	return (0);
531184610Salfred}
532184610Salfred
533184610Salfred/*
534184610Salfred * A frame has been uploaded: pass the resulting mbuf chain up to
535184610Salfred * the higher level protocols.
536184610Salfred */
537184610Salfredstatic void
538194677Sthompsakue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
539184610Salfred{
540194677Sthompsa	struct kue_softc *sc = usbd_xfer_softc(xfer);
541192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
542194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
543194677Sthompsa	struct usb_page_cache *pc;
544184610Salfred	uint8_t buf[2];
545188412Sthompsa	int len;
546194677Sthompsa	int actlen;
547184610Salfred
548194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
549194677Sthompsa
550184610Salfred	switch (USB_GET_STATE(xfer)) {
551184610Salfred	case USB_ST_TRANSFERRED:
552184610Salfred
553233774Shselasky		if (actlen <= (int)(2 + sizeof(struct ether_header))) {
554271832Sglebius			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
555184610Salfred			goto tr_setup;
556184610Salfred		}
557194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
558194677Sthompsa		usbd_copy_out(pc, 0, buf, 2);
559194677Sthompsa		actlen -= 2;
560184610Salfred		len = buf[0] | (buf[1] << 8);
561194677Sthompsa		len = min(actlen, len);
562184610Salfred
563194677Sthompsa		uether_rxbuf(ue, pc, 2, len);
564188412Sthompsa		/* FALLTHROUGH */
565184610Salfred	case USB_ST_SETUP:
566184610Salfredtr_setup:
567194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
568194228Sthompsa		usbd_transfer_submit(xfer);
569194228Sthompsa		uether_rxflush(ue);
570184610Salfred		return;
571184610Salfred
572184610Salfred	default:			/* Error */
573188412Sthompsa		DPRINTF("bulk read error, %s\n",
574194677Sthompsa		    usbd_errstr(error));
575188412Sthompsa
576194677Sthompsa		if (error != USB_ERR_CANCELLED) {
577184610Salfred			/* try to clear stall first */
578194677Sthompsa			usbd_xfer_set_stall(xfer);
579188412Sthompsa			goto tr_setup;
580184610Salfred		}
581184610Salfred		return;
582184610Salfred
583184610Salfred	}
584184610Salfred}
585184610Salfred
586184610Salfredstatic void
587194677Sthompsakue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
588184610Salfred{
589194677Sthompsa	struct kue_softc *sc = usbd_xfer_softc(xfer);
590194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
591194677Sthompsa	struct usb_page_cache *pc;
592184610Salfred	struct mbuf *m;
593188412Sthompsa	int total_len;
594188412Sthompsa	int temp_len;
595184610Salfred	uint8_t buf[2];
596184610Salfred
597184610Salfred	switch (USB_GET_STATE(xfer)) {
598184610Salfred	case USB_ST_TRANSFERRED:
599184610Salfred		DPRINTFN(11, "transfer complete\n");
600271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
601184610Salfred
602188412Sthompsa		/* FALLTHROUGH */
603184610Salfred	case USB_ST_SETUP:
604188412Sthompsatr_setup:
605184610Salfred		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
606184610Salfred
607188412Sthompsa		if (m == NULL)
608188412Sthompsa			return;
609188412Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
610184610Salfred			m->m_pkthdr.len = MCLBYTES;
611184610Salfred		temp_len = (m->m_pkthdr.len + 2);
612184610Salfred		total_len = (temp_len + (64 - (temp_len % 64)));
613184610Salfred
614184610Salfred		/* the first two bytes are the frame length */
615184610Salfred
616184610Salfred		buf[0] = (uint8_t)(m->m_pkthdr.len);
617184610Salfred		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
618184610Salfred
619194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
620194677Sthompsa		usbd_copy_in(pc, 0, buf, 2);
621194677Sthompsa		usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
622184610Salfred
623194677Sthompsa		usbd_frame_zero(pc, temp_len, total_len - temp_len);
624194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, total_len);
625184610Salfred
626184610Salfred		/*
627184610Salfred		 * if there's a BPF listener, bounce a copy
628184610Salfred		 * of this frame to him:
629184610Salfred		 */
630184610Salfred		BPF_MTAP(ifp, m);
631184610Salfred
632184610Salfred		m_freem(m);
633184610Salfred
634194228Sthompsa		usbd_transfer_submit(xfer);
635184610Salfred
636184610Salfred		return;
637184610Salfred
638184610Salfred	default:			/* Error */
639184610Salfred		DPRINTFN(11, "transfer error, %s\n",
640194677Sthompsa		    usbd_errstr(error));
641184610Salfred
642271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
643188412Sthompsa
644194677Sthompsa		if (error != USB_ERR_CANCELLED) {
645184610Salfred			/* try to clear stall first */
646194677Sthompsa			usbd_xfer_set_stall(xfer);
647188412Sthompsa			goto tr_setup;
648184610Salfred		}
649184610Salfred		return;
650184610Salfred
651184610Salfred	}
652184610Salfred}
653184610Salfred
654184610Salfredstatic void
655192984Sthompsakue_start(struct usb_ether *ue)
656184610Salfred{
657194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
658184610Salfred
659188412Sthompsa	/*
660188412Sthompsa	 * start the USB transfers, if not already started:
661188412Sthompsa	 */
662194228Sthompsa	usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_RD]);
663194228Sthompsa	usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_WR]);
664184610Salfred}
665184610Salfred
666184610Salfredstatic void
667192984Sthompsakue_init(struct usb_ether *ue)
668184610Salfred{
669194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
670194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
671184610Salfred
672188412Sthompsa	KUE_LOCK_ASSERT(sc, MA_OWNED);
673184610Salfred
674184610Salfred	/* set MAC address */
675188412Sthompsa	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
676188412Sthompsa	    0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
677184610Salfred
678184610Salfred	/* I'm not sure how to tune these. */
679184610Salfred#if 0
680184610Salfred	/*
681184610Salfred	 * Leave this one alone for now; setting it
682184610Salfred	 * wrong causes lockups on some machines/controllers.
683184610Salfred	 */
684188412Sthompsa	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
685184610Salfred#endif
686188412Sthompsa	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
687184610Salfred
688184610Salfred	/* load the multicast filter */
689188412Sthompsa	kue_setpromisc(ue);
690184610Salfred
691194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[KUE_BULK_DT_WR]);
692184610Salfred
693188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
694188412Sthompsa	kue_start(ue);
695184610Salfred}
696184610Salfred
697184610Salfredstatic void
698192984Sthompsakue_stop(struct usb_ether *ue)
699184610Salfred{
700194228Sthompsa	struct kue_softc *sc = uether_getsc(ue);
701194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
702184610Salfred
703188412Sthompsa	KUE_LOCK_ASSERT(sc, MA_OWNED);
704184610Salfred
705188412Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
706184610Salfred
707184610Salfred	/*
708184610Salfred	 * stop all the transfers, if not already stopped:
709184610Salfred	 */
710194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_WR]);
711194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_RD]);
712184610Salfred}
713