if_rue.c revision 207077
1139749Simp/*-
2127215Smarcel * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3127215Smarcel * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
4127215Smarcel * All rights reserved.
5127215Smarcel *
6127215Smarcel * Redistribution and use in source and binary forms, with or without
7127215Smarcel * modification, are permitted provided that the following conditions
8127215Smarcel * are met:
9127215Smarcel * 1. Redistributions of source code must retain the above copyright
10127215Smarcel *    notice, this list of conditions and the following disclaimer.
11127215Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12127215Smarcel *    notice, this list of conditions and the following disclaimer in the
13127215Smarcel *    documentation and/or other materials provided with the distribution.
14127215Smarcel *
15127215Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16127215Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17127215Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18127215Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19127215Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20127215Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21127215Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22127215Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23127215Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24127215Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25127215Smarcel * SUCH DAMAGE.
26127215Smarcel */
27127215Smarcel/*-
28127215Smarcel * Copyright (c) 1997, 1998, 1999, 2000
29127215Smarcel *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
30127215Smarcel *
31127215Smarcel * Redistribution and use in source and binary forms, with or without
32127215Smarcel * modification, are permitted provided that the following conditions
33127215Smarcel * are met:
34127215Smarcel * 1. Redistributions of source code must retain the above copyright
35127215Smarcel *    notice, this list of conditions and the following disclaimer.
36127215Smarcel * 2. Redistributions in binary form must reproduce the above copyright
37127215Smarcel *    notice, this list of conditions and the following disclaimer in the
38127215Smarcel *    documentation and/or other materials provided with the distribution.
39127215Smarcel * 3. All advertising materials mentioning features or use of this software
40127215Smarcel *    must display the following acknowledgement:
41127215Smarcel *	This product includes software developed by Bill Paul.
42127215Smarcel * 4. Neither the name of the author nor the names of any co-contributors
43127215Smarcel *    may be used to endorse or promote products derived from this software
44127215Smarcel *    without specific prior written permission.
45127215Smarcel *
46127215Smarcel * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47127215Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48127215Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49127215Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
50127215Smarcel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51168281Smarcel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52168281Smarcel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53168281Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54168281Smarcel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55249765Snyan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56252394Sray * THE POSSIBILITY OF SUCH DAMAGE.
57249765Snyan */
58168281Smarcel
59168281Smarcel#include <sys/cdefs.h>
60168281Smarcel__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_rue.c 207077 2010-04-22 21:31:34Z thompsa $");
61127215Smarcel
62228468Sed/*
63127215Smarcel * RealTek RTL8150 USB to fast ethernet controller driver.
64127215Smarcel * Datasheet is available from
65127215Smarcel * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
66127215Smarcel */
67168281Smarcel
68228468Sed#include <sys/stdint.h>
69168281Smarcel#include <sys/stddef.h>
70168281Smarcel#include <sys/param.h>
71168281Smarcel#include <sys/queue.h>
72168281Smarcel#include <sys/types.h>
73168281Smarcel#include <sys/systm.h>
74168281Smarcel#include <sys/kernel.h>
75168281Smarcel#include <sys/bus.h>
76168281Smarcel#include <sys/linker_set.h>
77168281Smarcel#include <sys/module.h>
78168281Smarcel#include <sys/lock.h>
79168281Smarcel#include <sys/mutex.h>
80168281Smarcel#include <sys/condvar.h>
81168281Smarcel#include <sys/sysctl.h>
82168281Smarcel#include <sys/sx.h>
83168281Smarcel#include <sys/unistd.h>
84168281Smarcel#include <sys/callout.h>
85168281Smarcel#include <sys/malloc.h>
86168281Smarcel#include <sys/priv.h>
87168281Smarcel
88168281Smarcel#include <dev/usb/usb.h>
89127215Smarcel#include <dev/usb/usbdi.h>
90228468Sed#include <dev/usb/usbdi_util.h>
91127215Smarcel#include "usbdevs.h"
92127215Smarcel
93127215Smarcel#define	USB_DEBUG_VAR rue_debug
94127215Smarcel#include <dev/usb/usb_debug.h>
95127215Smarcel#include <dev/usb/usb_process.h>
96228468Sed
97127215Smarcel#include <dev/usb/net/usb_ethernet.h>
98127215Smarcel#include <dev/usb/net/if_ruereg.h>
99127215Smarcel
100127215Smarcel#ifdef USB_DEBUG
101127215Smarcelstatic int rue_debug = 0;
102127215Smarcel
103127215SmarcelSYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
104127215SmarcelSYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
105127215Smarcel    &rue_debug, 0, "Debug level");
106127215Smarcel#endif
107127215Smarcel
108127215Smarcel/*
109127215Smarcel * Various supported device vendors/products.
110127215Smarcel */
111127215Smarcel
112127215Smarcelstatic const struct usb_device_id rue_devs[] = {
113127215Smarcel	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)},
114127215Smarcel	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)},
115127215Smarcel};
116127215Smarcel
117127215Smarcel/* prototypes */
118127215Smarcel
119127215Smarcelstatic device_probe_t rue_probe;
120127215Smarcelstatic device_attach_t rue_attach;
121127215Smarcelstatic device_detach_t rue_detach;
122228468Sed
123127215Smarcelstatic miibus_readreg_t rue_miibus_readreg;
124127215Smarcelstatic miibus_writereg_t rue_miibus_writereg;
125127215Smarcelstatic miibus_statchg_t rue_miibus_statchg;
126127215Smarcel
127127215Smarcelstatic usb_callback_t rue_intr_callback;
128127215Smarcelstatic usb_callback_t rue_bulk_read_callback;
129127215Smarcelstatic usb_callback_t rue_bulk_write_callback;
130127215Smarcel
131127215Smarcelstatic uether_fn_t rue_attach_post;
132127215Smarcelstatic uether_fn_t rue_init;
133127215Smarcelstatic uether_fn_t rue_stop;
134127215Smarcelstatic uether_fn_t rue_start;
135127215Smarcelstatic uether_fn_t rue_tick;
136127215Smarcelstatic uether_fn_t rue_setmulti;
137127215Smarcelstatic uether_fn_t rue_setpromisc;
138127215Smarcel
139127215Smarcelstatic int	rue_read_mem(struct rue_softc *, uint16_t, void *, int);
140127215Smarcelstatic int	rue_write_mem(struct rue_softc *, uint16_t, void *, int);
141127215Smarcelstatic uint8_t	rue_csr_read_1(struct rue_softc *, uint16_t);
142127215Smarcelstatic uint16_t	rue_csr_read_2(struct rue_softc *, uint16_t);
143127215Smarcelstatic int	rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t);
144127215Smarcelstatic int	rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t);
145127215Smarcelstatic int	rue_csr_write_4(struct rue_softc *, int, uint32_t);
146127215Smarcel
147127215Smarcelstatic void	rue_reset(struct rue_softc *);
148127215Smarcelstatic int	rue_ifmedia_upd(struct ifnet *);
149127215Smarcelstatic void	rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
150127215Smarcel
151127215Smarcelstatic const struct usb_config rue_config[RUE_N_TRANSFER] = {
152127215Smarcel
153127215Smarcel	[RUE_BULK_DT_WR] = {
154127215Smarcel		.type = UE_BULK,
155127215Smarcel		.endpoint = UE_ADDR_ANY,
156127215Smarcel		.direction = UE_DIR_OUT,
157127215Smarcel		.bufsize = MCLBYTES,
158127215Smarcel		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
159127215Smarcel		.callback = rue_bulk_write_callback,
160127215Smarcel		.timeout = 10000,	/* 10 seconds */
161127215Smarcel	},
162127215Smarcel
163127215Smarcel	[RUE_BULK_DT_RD] = {
164127215Smarcel		.type = UE_BULK,
165127215Smarcel		.endpoint = UE_ADDR_ANY,
166127215Smarcel		.direction = UE_DIR_IN,
167127215Smarcel		.bufsize = (MCLBYTES + 4),
168127215Smarcel		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169127215Smarcel		.callback = rue_bulk_read_callback,
170127215Smarcel		.timeout = 0,	/* no timeout */
171127215Smarcel	},
172127215Smarcel
173127215Smarcel	[RUE_INTR_DT_RD] = {
174127215Smarcel		.type = UE_INTERRUPT,
175127215Smarcel		.endpoint = UE_ADDR_ANY,
176127215Smarcel		.direction = UE_DIR_IN,
177127215Smarcel		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
178215034Sbrucec		.bufsize = 0,	/* use wMaxPacketSize */
179215034Sbrucec		.callback = rue_intr_callback,
180127215Smarcel	},
181127215Smarcel};
182127215Smarcel
183127215Smarcelstatic device_method_t rue_methods[] = {
184127215Smarcel	/* Device interface */
185127215Smarcel	DEVMETHOD(device_probe, rue_probe),
186127215Smarcel	DEVMETHOD(device_attach, rue_attach),
187127215Smarcel	DEVMETHOD(device_detach, rue_detach),
188127215Smarcel
189127215Smarcel	/* Bus interface */
190127215Smarcel	DEVMETHOD(bus_print_child, bus_generic_print_child),
191127215Smarcel	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
192127215Smarcel
193127215Smarcel	/* MII interface */
194127215Smarcel	DEVMETHOD(miibus_readreg, rue_miibus_readreg),
195127215Smarcel	DEVMETHOD(miibus_writereg, rue_miibus_writereg),
196168281Smarcel	DEVMETHOD(miibus_statchg, rue_miibus_statchg),
197127215Smarcel
198228468Sed	{0, 0}
199127215Smarcel};
200168281Smarcel
201127215Smarcelstatic driver_t rue_driver = {
202127215Smarcel	.name = "rue",
203168281Smarcel	.methods = rue_methods,
204168281Smarcel	.size = sizeof(struct rue_softc),
205168281Smarcel};
206168281Smarcel
207168281Smarcelstatic devclass_t rue_devclass;
208168281Smarcel
209168281SmarcelDRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, 0);
210127215SmarcelDRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
211127215SmarcelMODULE_DEPEND(rue, uether, 1, 1, 1);
212127215SmarcelMODULE_DEPEND(rue, usb, 1, 1, 1);
213127215SmarcelMODULE_DEPEND(rue, ether, 1, 1, 1);
214127215SmarcelMODULE_DEPEND(rue, miibus, 1, 1, 1);
215127215Smarcel
216127215Smarcelstatic const struct usb_ether_methods rue_ue_methods = {
217127215Smarcel	.ue_attach_post = rue_attach_post,
218127215Smarcel	.ue_start = rue_start,
219127215Smarcel	.ue_init = rue_init,
220127226Smarcel	.ue_stop = rue_stop,
221127226Smarcel	.ue_tick = rue_tick,
222127215Smarcel	.ue_setmulti = rue_setmulti,
223127215Smarcel	.ue_setpromisc = rue_setpromisc,
224127215Smarcel	.ue_mii_upd = rue_ifmedia_upd,
225127215Smarcel	.ue_mii_sts = rue_ifmedia_sts,
226127215Smarcel};
227127215Smarcel
228127215Smarcel#define	RUE_SETBIT(sc, reg, x) \
229127215Smarcel	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
230127215Smarcel
231127215Smarcel#define	RUE_CLRBIT(sc, reg, x) \
232127215Smarcel	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
233127215Smarcel
234127215Smarcelstatic int
235127215Smarcelrue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
236127215Smarcel{
237127215Smarcel	struct usb_device_request req;
238127215Smarcel
239127215Smarcel	req.bmRequestType = UT_READ_VENDOR_DEVICE;
240127215Smarcel	req.bRequest = UR_SET_ADDRESS;
241127215Smarcel	USETW(req.wValue, addr);
242127215Smarcel	USETW(req.wIndex, 0);
243127215Smarcel	USETW(req.wLength, len);
244127215Smarcel
245127215Smarcel	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
246168281Smarcel}
247127215Smarcel
248127215Smarcelstatic int
249127215Smarcelrue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
250127215Smarcel{
251127215Smarcel	struct usb_device_request req;
252127215Smarcel
253127215Smarcel	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
254127215Smarcel	req.bRequest = UR_SET_ADDRESS;
255127215Smarcel	USETW(req.wValue, addr);
256127215Smarcel	USETW(req.wIndex, 0);
257127215Smarcel	USETW(req.wLength, len);
258127215Smarcel
259127215Smarcel	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
260127215Smarcel}
261127215Smarcel
262127215Smarcelstatic uint8_t
263127215Smarcelrue_csr_read_1(struct rue_softc *sc, uint16_t reg)
264127215Smarcel{
265127215Smarcel	uint8_t val;
266127215Smarcel
267127215Smarcel	rue_read_mem(sc, reg, &val, 1);
268127215Smarcel	return (val);
269127215Smarcel}
270127215Smarcel
271127215Smarcelstatic uint16_t
272127215Smarcelrue_csr_read_2(struct rue_softc *sc, uint16_t reg)
273127215Smarcel{
274127215Smarcel	uint8_t val[2];
275127215Smarcel
276127215Smarcel	rue_read_mem(sc, reg, &val, 2);
277127215Smarcel	return (UGETW(val));
278127215Smarcel}
279127215Smarcel
280127215Smarcelstatic int
281127215Smarcelrue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val)
282127215Smarcel{
283127215Smarcel	return (rue_write_mem(sc, reg, &val, 1));
284137704Smarcel}
285137704Smarcel
286137704Smarcelstatic int
287137704Smarcelrue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val)
288137704Smarcel{
289137704Smarcel	uint8_t temp[2];
290137704Smarcel
291137704Smarcel	USETW(temp, val);
292137704Smarcel	return (rue_write_mem(sc, reg, &temp, 2));
293137704Smarcel}
294137704Smarcel
295137704Smarcelstatic int
296137704Smarcelrue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val)
297137704Smarcel{
298137704Smarcel	uint8_t temp[4];
299137704Smarcel
300137704Smarcel	USETDW(temp, val);
301137704Smarcel	return (rue_write_mem(sc, reg, &temp, 4));
302137704Smarcel}
303137704Smarcel
304168281Smarcelstatic int
305168281Smarcelrue_miibus_readreg(device_t dev, int phy, int reg)
306168281Smarcel{
307168281Smarcel	struct rue_softc *sc = device_get_softc(dev);
308168281Smarcel	uint16_t rval;
309127215Smarcel	uint16_t ruereg;
310	int locked;
311
312	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
313		return (0);
314
315	locked = mtx_owned(&sc->sc_mtx);
316	if (!locked)
317		RUE_LOCK(sc);
318
319	switch (reg) {
320	case MII_BMCR:
321		ruereg = RUE_BMCR;
322		break;
323	case MII_BMSR:
324		ruereg = RUE_BMSR;
325		break;
326	case MII_ANAR:
327		ruereg = RUE_ANAR;
328		break;
329	case MII_ANER:
330		ruereg = RUE_AER;
331		break;
332	case MII_ANLPAR:
333		ruereg = RUE_ANLP;
334		break;
335	case MII_PHYIDR1:
336	case MII_PHYIDR2:
337		rval = 0;
338		goto done;
339	default:
340		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
341			rval = rue_csr_read_1(sc, reg);
342			goto done;
343		}
344		device_printf(sc->sc_ue.ue_dev, "bad phy register\n");
345		rval = 0;
346		goto done;
347	}
348
349	rval = rue_csr_read_2(sc, ruereg);
350done:
351	if (!locked)
352		RUE_UNLOCK(sc);
353	return (rval);
354}
355
356static int
357rue_miibus_writereg(device_t dev, int phy, int reg, int data)
358{
359	struct rue_softc *sc = device_get_softc(dev);
360	uint16_t ruereg;
361	int locked;
362
363	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
364		return (0);
365
366	locked = mtx_owned(&sc->sc_mtx);
367	if (!locked)
368		RUE_LOCK(sc);
369
370	switch (reg) {
371	case MII_BMCR:
372		ruereg = RUE_BMCR;
373		break;
374	case MII_BMSR:
375		ruereg = RUE_BMSR;
376		break;
377	case MII_ANAR:
378		ruereg = RUE_ANAR;
379		break;
380	case MII_ANER:
381		ruereg = RUE_AER;
382		break;
383	case MII_ANLPAR:
384		ruereg = RUE_ANLP;
385		break;
386	case MII_PHYIDR1:
387	case MII_PHYIDR2:
388		goto done;
389	default:
390		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
391			rue_csr_write_1(sc, reg, data);
392			goto done;
393		}
394		device_printf(sc->sc_ue.ue_dev, " bad phy register\n");
395		goto done;
396	}
397	rue_csr_write_2(sc, ruereg, data);
398done:
399	if (!locked)
400		RUE_UNLOCK(sc);
401	return (0);
402}
403
404static void
405rue_miibus_statchg(device_t dev)
406{
407	/*
408	 * When the code below is enabled the card starts doing weird
409	 * things after link going from UP to DOWN and back UP.
410	 *
411	 * Looks like some of register writes below messes up PHY
412	 * interface.
413	 *
414	 * No visible regressions were found after commenting this code
415	 * out, so that disable it for good.
416	 */
417#if 0
418	struct rue_softc *sc = device_get_softc(dev);
419	struct mii_data *mii = GET_MII(sc);
420	uint16_t bmcr;
421	int locked;
422
423	locked = mtx_owned(&sc->sc_mtx);
424	if (!locked)
425		RUE_LOCK(sc);
426
427	RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
428
429	bmcr = rue_csr_read_2(sc, RUE_BMCR);
430
431	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
432		bmcr |= RUE_BMCR_SPD_SET;
433	else
434		bmcr &= ~RUE_BMCR_SPD_SET;
435
436	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
437		bmcr |= RUE_BMCR_DUPLEX;
438	else
439		bmcr &= ~RUE_BMCR_DUPLEX;
440
441	rue_csr_write_2(sc, RUE_BMCR, bmcr);
442
443	RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
444
445	if (!locked)
446		RUE_UNLOCK(sc);
447#endif
448}
449
450static void
451rue_setpromisc(struct usb_ether *ue)
452{
453	struct rue_softc *sc = uether_getsc(ue);
454	struct ifnet *ifp = uether_getifp(ue);
455
456	RUE_LOCK_ASSERT(sc, MA_OWNED);
457
458	/* If we want promiscuous mode, set the allframes bit. */
459	if (ifp->if_flags & IFF_PROMISC)
460		RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP);
461	else
462		RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP);
463}
464
465/*
466 * Program the 64-bit multicast hash filter.
467 */
468static void
469rue_setmulti(struct usb_ether *ue)
470{
471	struct rue_softc *sc = uether_getsc(ue);
472	struct ifnet *ifp = uether_getifp(ue);
473	uint16_t rxcfg;
474	int h = 0;
475	uint32_t hashes[2] = { 0, 0 };
476	struct ifmultiaddr *ifma;
477	int mcnt = 0;
478
479	RUE_LOCK_ASSERT(sc, MA_OWNED);
480
481	rxcfg = rue_csr_read_2(sc, RUE_RCR);
482
483	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
484		rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
485		rxcfg &= ~RUE_RCR_AM;
486		rue_csr_write_2(sc, RUE_RCR, rxcfg);
487		rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
488		rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
489		return;
490	}
491
492	/* first, zot all the existing hash bits */
493	rue_csr_write_4(sc, RUE_MAR0, 0);
494	rue_csr_write_4(sc, RUE_MAR4, 0);
495
496	/* now program new ones */
497	if_maddr_rlock(ifp);
498	TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
499	{
500		if (ifma->ifma_addr->sa_family != AF_LINK)
501			continue;
502		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
503		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
504		if (h < 32)
505			hashes[0] |= (1 << h);
506		else
507			hashes[1] |= (1 << (h - 32));
508		mcnt++;
509	}
510	if_maddr_runlock(ifp);
511
512	if (mcnt)
513		rxcfg |= RUE_RCR_AM;
514	else
515		rxcfg &= ~RUE_RCR_AM;
516
517	rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
518
519	rue_csr_write_2(sc, RUE_RCR, rxcfg);
520	rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
521	rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
522}
523
524static void
525rue_reset(struct rue_softc *sc)
526{
527	int i;
528
529	rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
530
531	for (i = 0; i != RUE_TIMEOUT; i++) {
532		if (uether_pause(&sc->sc_ue, hz / 1000))
533			break;
534		if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
535			break;
536	}
537	if (i == RUE_TIMEOUT)
538		device_printf(sc->sc_ue.ue_dev, "reset never completed\n");
539
540	uether_pause(&sc->sc_ue, hz / 100);
541}
542
543static void
544rue_attach_post(struct usb_ether *ue)
545{
546	struct rue_softc *sc = uether_getsc(ue);
547
548	/* reset the adapter */
549	rue_reset(sc);
550
551	/* get station address from the EEPROM */
552	rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN);
553}
554
555/*
556 * Probe for a RTL8150 chip.
557 */
558static int
559rue_probe(device_t dev)
560{
561	struct usb_attach_arg *uaa = device_get_ivars(dev);
562
563	if (uaa->usb_mode != USB_MODE_HOST)
564		return (ENXIO);
565	if (uaa->info.bConfigIndex != RUE_CONFIG_IDX)
566		return (ENXIO);
567	if (uaa->info.bIfaceIndex != RUE_IFACE_IDX)
568		return (ENXIO);
569
570	return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa));
571}
572
573/*
574 * Attach the interface. Allocate softc structures, do ifmedia
575 * setup and ethernet/BPF attach.
576 */
577static int
578rue_attach(device_t dev)
579{
580	struct usb_attach_arg *uaa = device_get_ivars(dev);
581	struct rue_softc *sc = device_get_softc(dev);
582	struct usb_ether *ue = &sc->sc_ue;
583	uint8_t iface_index;
584	int error;
585
586	device_set_usb_desc(dev);
587	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
588
589	iface_index = RUE_IFACE_IDX;
590	error = usbd_transfer_setup(uaa->device, &iface_index,
591	    sc->sc_xfer, rue_config, RUE_N_TRANSFER,
592	    sc, &sc->sc_mtx);
593	if (error) {
594		device_printf(dev, "allocating USB transfers failed\n");
595		goto detach;
596	}
597
598	ue->ue_sc = sc;
599	ue->ue_dev = dev;
600	ue->ue_udev = uaa->device;
601	ue->ue_mtx = &sc->sc_mtx;
602	ue->ue_methods = &rue_ue_methods;
603
604	error = uether_ifattach(ue);
605	if (error) {
606		device_printf(dev, "could not attach interface\n");
607		goto detach;
608	}
609	return (0);			/* success */
610
611detach:
612	rue_detach(dev);
613	return (ENXIO);			/* failure */
614}
615
616static int
617rue_detach(device_t dev)
618{
619	struct rue_softc *sc = device_get_softc(dev);
620	struct usb_ether *ue = &sc->sc_ue;
621
622	usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER);
623	uether_ifdetach(ue);
624	mtx_destroy(&sc->sc_mtx);
625
626	return (0);
627}
628
629static void
630rue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
631{
632	struct rue_softc *sc = usbd_xfer_softc(xfer);
633	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
634	struct rue_intrpkt pkt;
635	struct usb_page_cache *pc;
636	int actlen;
637
638	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
639
640	switch (USB_GET_STATE(xfer)) {
641	case USB_ST_TRANSFERRED:
642
643		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
644		    actlen >= sizeof(pkt)) {
645
646			pc = usbd_xfer_get_frame(xfer, 0);
647			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
648
649			ifp->if_ierrors += pkt.rue_rxlost_cnt;
650			ifp->if_ierrors += pkt.rue_crcerr_cnt;
651			ifp->if_collisions += pkt.rue_col_cnt;
652		}
653		/* FALLTHROUGH */
654	case USB_ST_SETUP:
655tr_setup:
656		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
657		usbd_transfer_submit(xfer);
658		return;
659
660	default:			/* Error */
661		if (error != USB_ERR_CANCELLED) {
662			/* try to clear stall first */
663			usbd_xfer_set_stall(xfer);
664			goto tr_setup;
665		}
666		return;
667	}
668}
669
670static void
671rue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
672{
673	struct rue_softc *sc = usbd_xfer_softc(xfer);
674	struct usb_ether *ue = &sc->sc_ue;
675	struct ifnet *ifp = uether_getifp(ue);
676	struct usb_page_cache *pc;
677	uint16_t status;
678	int actlen;
679
680	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
681
682	switch (USB_GET_STATE(xfer)) {
683	case USB_ST_TRANSFERRED:
684
685		if (actlen < 4) {
686			ifp->if_ierrors++;
687			goto tr_setup;
688		}
689		pc = usbd_xfer_get_frame(xfer, 0);
690		usbd_copy_out(pc, actlen - 4, &status, sizeof(status));
691		actlen -= 4;
692
693		/* check recieve packet was valid or not */
694		status = le16toh(status);
695		if ((status & RUE_RXSTAT_VALID) == 0) {
696			ifp->if_ierrors++;
697			goto tr_setup;
698		}
699		uether_rxbuf(ue, pc, 0, actlen);
700		/* FALLTHROUGH */
701	case USB_ST_SETUP:
702tr_setup:
703		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
704		usbd_transfer_submit(xfer);
705		uether_rxflush(ue);
706		return;
707
708	default:			/* Error */
709		DPRINTF("bulk read error, %s\n",
710		    usbd_errstr(error));
711
712		if (error != USB_ERR_CANCELLED) {
713			/* try to clear stall first */
714			usbd_xfer_set_stall(xfer);
715			goto tr_setup;
716		}
717		return;
718	}
719}
720
721static void
722rue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
723{
724	struct rue_softc *sc = usbd_xfer_softc(xfer);
725	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
726	struct usb_page_cache *pc;
727	struct mbuf *m;
728	int temp_len;
729
730	switch (USB_GET_STATE(xfer)) {
731	case USB_ST_TRANSFERRED:
732		DPRINTFN(11, "transfer complete\n");
733		ifp->if_opackets++;
734
735		/* FALLTHROUGH */
736	case USB_ST_SETUP:
737tr_setup:
738		if ((sc->sc_flags & RUE_FLAG_LINK) == 0) {
739			/*
740			 * don't send anything if there is no link !
741			 */
742			return;
743		}
744		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
745
746		if (m == NULL)
747			return;
748		if (m->m_pkthdr.len > MCLBYTES)
749			m->m_pkthdr.len = MCLBYTES;
750		temp_len = m->m_pkthdr.len;
751
752		pc = usbd_xfer_get_frame(xfer, 0);
753		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
754
755		/*
756		 * This is an undocumented behavior.
757		 * RTL8150 chip doesn't send frame length smaller than
758		 * RUE_MIN_FRAMELEN (60) byte packet.
759		 */
760		if (temp_len < RUE_MIN_FRAMELEN) {
761			usbd_frame_zero(pc, temp_len,
762			    RUE_MIN_FRAMELEN - temp_len);
763			temp_len = RUE_MIN_FRAMELEN;
764		}
765		usbd_xfer_set_frame_len(xfer, 0, temp_len);
766
767		/*
768		 * if there's a BPF listener, bounce a copy
769		 * of this frame to him:
770		 */
771		BPF_MTAP(ifp, m);
772
773		m_freem(m);
774
775		usbd_transfer_submit(xfer);
776
777		return;
778
779	default:			/* Error */
780		DPRINTFN(11, "transfer error, %s\n",
781		    usbd_errstr(error));
782
783		ifp->if_oerrors++;
784
785		if (error != USB_ERR_CANCELLED) {
786			/* try to clear stall first */
787			usbd_xfer_set_stall(xfer);
788			goto tr_setup;
789		}
790		return;
791	}
792}
793
794static void
795rue_tick(struct usb_ether *ue)
796{
797	struct rue_softc *sc = uether_getsc(ue);
798	struct mii_data *mii = GET_MII(sc);
799
800	RUE_LOCK_ASSERT(sc, MA_OWNED);
801
802	mii_tick(mii);
803	if ((sc->sc_flags & RUE_FLAG_LINK) == 0
804	    && mii->mii_media_status & IFM_ACTIVE &&
805	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
806		sc->sc_flags |= RUE_FLAG_LINK;
807		rue_start(ue);
808	}
809}
810
811static void
812rue_start(struct usb_ether *ue)
813{
814	struct rue_softc *sc = uether_getsc(ue);
815
816	/*
817	 * start the USB transfers, if not already started:
818	 */
819	usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]);
820	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]);
821	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]);
822}
823
824static void
825rue_init(struct usb_ether *ue)
826{
827	struct rue_softc *sc = uether_getsc(ue);
828	struct ifnet *ifp = uether_getifp(ue);
829
830	RUE_LOCK_ASSERT(sc, MA_OWNED);
831
832	/*
833	 * Cancel pending I/O
834	 */
835	rue_reset(sc);
836
837	/* Set MAC address */
838	rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
839
840	rue_stop(ue);
841
842	/*
843	 * Set the initial TX and RX configuration.
844	 */
845	rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
846	rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB);
847
848	/* Load the multicast filter */
849	rue_setpromisc(ue);
850	/* Load the multicast filter. */
851	rue_setmulti(ue);
852
853	/* Enable RX and TX */
854	rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
855
856	usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]);
857
858	ifp->if_drv_flags |= IFF_DRV_RUNNING;
859	rue_start(ue);
860}
861
862/*
863 * Set media options.
864 */
865static int
866rue_ifmedia_upd(struct ifnet *ifp)
867{
868	struct rue_softc *sc = ifp->if_softc;
869	struct mii_data *mii = GET_MII(sc);
870
871	RUE_LOCK_ASSERT(sc, MA_OWNED);
872
873        sc->sc_flags &= ~RUE_FLAG_LINK;
874	if (mii->mii_instance) {
875		struct mii_softc *miisc;
876
877		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
878			mii_phy_reset(miisc);
879	}
880	mii_mediachg(mii);
881	return (0);
882}
883
884/*
885 * Report current media status.
886 */
887static void
888rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
889{
890	struct rue_softc *sc = ifp->if_softc;
891	struct mii_data *mii = GET_MII(sc);
892
893	RUE_LOCK(sc);
894	mii_pollstat(mii);
895	RUE_UNLOCK(sc);
896	ifmr->ifm_active = mii->mii_media_active;
897	ifmr->ifm_status = mii->mii_media_status;
898}
899
900static void
901rue_stop(struct usb_ether *ue)
902{
903	struct rue_softc *sc = uether_getsc(ue);
904	struct ifnet *ifp = uether_getifp(ue);
905
906	RUE_LOCK_ASSERT(sc, MA_OWNED);
907
908	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
909	sc->sc_flags &= ~RUE_FLAG_LINK;
910
911	/*
912	 * stop all the transfers, if not already stopped:
913	 */
914	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]);
915	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]);
916	usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]);
917
918	rue_csr_write_1(sc, RUE_CR, 0x00);
919
920	rue_reset(sc);
921}
922