1/*-
2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3 * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
4 * 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27/*-
28 * Copyright (c) 1997, 1998, 1999, 2000
29 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 *    must display the following acknowledgement:
41 *	This product includes software developed by Bill Paul.
42 * 4. Neither the name of the author nor the names of any co-contributors
43 *    may be used to endorse or promote products derived from this software
44 *    without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56 * THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD$");
61
62/*
63 * RealTek RTL8150 USB to fast ethernet controller driver.
64 * Datasheet is available from
65 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
66 */
67
68#include <sys/stdint.h>
69#include <sys/stddef.h>
70#include <sys/param.h>
71#include <sys/queue.h>
72#include <sys/types.h>
73#include <sys/systm.h>
74#include <sys/socket.h>
75#include <sys/kernel.h>
76#include <sys/bus.h>
77#include <sys/module.h>
78#include <sys/lock.h>
79#include <sys/mutex.h>
80#include <sys/condvar.h>
81#include <sys/sysctl.h>
82#include <sys/sx.h>
83#include <sys/unistd.h>
84#include <sys/callout.h>
85#include <sys/malloc.h>
86#include <sys/priv.h>
87
88#include <net/if.h>
89#include <net/if_var.h>
90
91#include <dev/usb/usb.h>
92#include <dev/usb/usbdi.h>
93#include <dev/usb/usbdi_util.h>
94#include "usbdevs.h"
95
96#define	USB_DEBUG_VAR rue_debug
97#include <dev/usb/usb_debug.h>
98#include <dev/usb/usb_process.h>
99
100#include <dev/usb/net/usb_ethernet.h>
101#include <dev/usb/net/if_ruereg.h>
102
103#ifdef USB_DEBUG
104static int rue_debug = 0;
105
106static SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
107SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RWTUN,
108    &rue_debug, 0, "Debug level");
109#endif
110
111/*
112 * Various supported device vendors/products.
113 */
114
115static const STRUCT_USB_HOST_ID rue_devs[] = {
116	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)},
117	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)},
118	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 0)},
119};
120
121/* prototypes */
122
123static device_probe_t rue_probe;
124static device_attach_t rue_attach;
125static device_detach_t rue_detach;
126
127static miibus_readreg_t rue_miibus_readreg;
128static miibus_writereg_t rue_miibus_writereg;
129static miibus_statchg_t rue_miibus_statchg;
130
131static usb_callback_t rue_intr_callback;
132static usb_callback_t rue_bulk_read_callback;
133static usb_callback_t rue_bulk_write_callback;
134
135static uether_fn_t rue_attach_post;
136static uether_fn_t rue_init;
137static uether_fn_t rue_stop;
138static uether_fn_t rue_start;
139static uether_fn_t rue_tick;
140static uether_fn_t rue_setmulti;
141static uether_fn_t rue_setpromisc;
142
143static int	rue_read_mem(struct rue_softc *, uint16_t, void *, int);
144static int	rue_write_mem(struct rue_softc *, uint16_t, void *, int);
145static uint8_t	rue_csr_read_1(struct rue_softc *, uint16_t);
146static uint16_t	rue_csr_read_2(struct rue_softc *, uint16_t);
147static int	rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t);
148static int	rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t);
149static int	rue_csr_write_4(struct rue_softc *, int, uint32_t);
150
151static void	rue_reset(struct rue_softc *);
152static int	rue_ifmedia_upd(struct ifnet *);
153static void	rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
154
155static const struct usb_config rue_config[RUE_N_TRANSFER] = {
156
157	[RUE_BULK_DT_WR] = {
158		.type = UE_BULK,
159		.endpoint = UE_ADDR_ANY,
160		.direction = UE_DIR_OUT,
161		.bufsize = MCLBYTES,
162		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
163		.callback = rue_bulk_write_callback,
164		.timeout = 10000,	/* 10 seconds */
165	},
166
167	[RUE_BULK_DT_RD] = {
168		.type = UE_BULK,
169		.endpoint = UE_ADDR_ANY,
170		.direction = UE_DIR_IN,
171		.bufsize = (MCLBYTES + 4),
172		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
173		.callback = rue_bulk_read_callback,
174		.timeout = 0,	/* no timeout */
175	},
176
177	[RUE_INTR_DT_RD] = {
178		.type = UE_INTERRUPT,
179		.endpoint = UE_ADDR_ANY,
180		.direction = UE_DIR_IN,
181		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
182		.bufsize = 0,	/* use wMaxPacketSize */
183		.callback = rue_intr_callback,
184	},
185};
186
187static device_method_t rue_methods[] = {
188	/* Device interface */
189	DEVMETHOD(device_probe, rue_probe),
190	DEVMETHOD(device_attach, rue_attach),
191	DEVMETHOD(device_detach, rue_detach),
192
193	/* MII interface */
194	DEVMETHOD(miibus_readreg, rue_miibus_readreg),
195	DEVMETHOD(miibus_writereg, rue_miibus_writereg),
196	DEVMETHOD(miibus_statchg, rue_miibus_statchg),
197
198	DEVMETHOD_END
199};
200
201static driver_t rue_driver = {
202	.name = "rue",
203	.methods = rue_methods,
204	.size = sizeof(struct rue_softc),
205};
206
207static devclass_t rue_devclass;
208
209DRIVER_MODULE_ORDERED(rue, uhub, rue_driver, rue_devclass, NULL, NULL,
210    SI_ORDER_ANY);
211DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, NULL, NULL);
212MODULE_DEPEND(rue, uether, 1, 1, 1);
213MODULE_DEPEND(rue, usb, 1, 1, 1);
214MODULE_DEPEND(rue, ether, 1, 1, 1);
215MODULE_DEPEND(rue, miibus, 1, 1, 1);
216MODULE_VERSION(rue, 1);
217USB_PNP_HOST_INFO(rue_devs);
218
219static const struct usb_ether_methods rue_ue_methods = {
220	.ue_attach_post = rue_attach_post,
221	.ue_start = rue_start,
222	.ue_init = rue_init,
223	.ue_stop = rue_stop,
224	.ue_tick = rue_tick,
225	.ue_setmulti = rue_setmulti,
226	.ue_setpromisc = rue_setpromisc,
227	.ue_mii_upd = rue_ifmedia_upd,
228	.ue_mii_sts = rue_ifmedia_sts,
229};
230
231#define	RUE_SETBIT(sc, reg, x) \
232	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
233
234#define	RUE_CLRBIT(sc, reg, x) \
235	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
236
237static int
238rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
239{
240	struct usb_device_request req;
241
242	req.bmRequestType = UT_READ_VENDOR_DEVICE;
243	req.bRequest = UR_SET_ADDRESS;
244	USETW(req.wValue, addr);
245	USETW(req.wIndex, 0);
246	USETW(req.wLength, len);
247
248	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
249}
250
251static int
252rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
253{
254	struct usb_device_request req;
255
256	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
257	req.bRequest = UR_SET_ADDRESS;
258	USETW(req.wValue, addr);
259	USETW(req.wIndex, 0);
260	USETW(req.wLength, len);
261
262	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
263}
264
265static uint8_t
266rue_csr_read_1(struct rue_softc *sc, uint16_t reg)
267{
268	uint8_t val;
269
270	rue_read_mem(sc, reg, &val, 1);
271	return (val);
272}
273
274static uint16_t
275rue_csr_read_2(struct rue_softc *sc, uint16_t reg)
276{
277	uint8_t val[2];
278
279	rue_read_mem(sc, reg, &val, 2);
280	return (UGETW(val));
281}
282
283static int
284rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val)
285{
286	return (rue_write_mem(sc, reg, &val, 1));
287}
288
289static int
290rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val)
291{
292	uint8_t temp[2];
293
294	USETW(temp, val);
295	return (rue_write_mem(sc, reg, &temp, 2));
296}
297
298static int
299rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val)
300{
301	uint8_t temp[4];
302
303	USETDW(temp, val);
304	return (rue_write_mem(sc, reg, &temp, 4));
305}
306
307static int
308rue_miibus_readreg(device_t dev, int phy, int reg)
309{
310	struct rue_softc *sc = device_get_softc(dev);
311	uint16_t rval;
312	uint16_t ruereg;
313	int locked;
314
315	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
316		return (0);
317
318	locked = mtx_owned(&sc->sc_mtx);
319	if (!locked)
320		RUE_LOCK(sc);
321
322	switch (reg) {
323	case MII_BMCR:
324		ruereg = RUE_BMCR;
325		break;
326	case MII_BMSR:
327		ruereg = RUE_BMSR;
328		break;
329	case MII_ANAR:
330		ruereg = RUE_ANAR;
331		break;
332	case MII_ANER:
333		ruereg = RUE_AER;
334		break;
335	case MII_ANLPAR:
336		ruereg = RUE_ANLP;
337		break;
338	case MII_PHYIDR1:
339	case MII_PHYIDR2:
340		rval = 0;
341		goto done;
342	default:
343		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
344			rval = rue_csr_read_1(sc, reg);
345			goto done;
346		}
347		device_printf(sc->sc_ue.ue_dev, "bad phy register\n");
348		rval = 0;
349		goto done;
350	}
351
352	rval = rue_csr_read_2(sc, ruereg);
353done:
354	if (!locked)
355		RUE_UNLOCK(sc);
356	return (rval);
357}
358
359static int
360rue_miibus_writereg(device_t dev, int phy, int reg, int data)
361{
362	struct rue_softc *sc = device_get_softc(dev);
363	uint16_t ruereg;
364	int locked;
365
366	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
367		return (0);
368
369	locked = mtx_owned(&sc->sc_mtx);
370	if (!locked)
371		RUE_LOCK(sc);
372
373	switch (reg) {
374	case MII_BMCR:
375		ruereg = RUE_BMCR;
376		break;
377	case MII_BMSR:
378		ruereg = RUE_BMSR;
379		break;
380	case MII_ANAR:
381		ruereg = RUE_ANAR;
382		break;
383	case MII_ANER:
384		ruereg = RUE_AER;
385		break;
386	case MII_ANLPAR:
387		ruereg = RUE_ANLP;
388		break;
389	case MII_PHYIDR1:
390	case MII_PHYIDR2:
391		goto done;
392	default:
393		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
394			rue_csr_write_1(sc, reg, data);
395			goto done;
396		}
397		device_printf(sc->sc_ue.ue_dev, " bad phy register\n");
398		goto done;
399	}
400	rue_csr_write_2(sc, ruereg, data);
401done:
402	if (!locked)
403		RUE_UNLOCK(sc);
404	return (0);
405}
406
407static void
408rue_miibus_statchg(device_t dev)
409{
410	/*
411	 * When the code below is enabled the card starts doing weird
412	 * things after link going from UP to DOWN and back UP.
413	 *
414	 * Looks like some of register writes below messes up PHY
415	 * interface.
416	 *
417	 * No visible regressions were found after commenting this code
418	 * out, so that disable it for good.
419	 */
420#if 0
421	struct rue_softc *sc = device_get_softc(dev);
422	struct mii_data *mii = GET_MII(sc);
423	uint16_t bmcr;
424	int locked;
425
426	locked = mtx_owned(&sc->sc_mtx);
427	if (!locked)
428		RUE_LOCK(sc);
429
430	RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
431
432	bmcr = rue_csr_read_2(sc, RUE_BMCR);
433
434	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
435		bmcr |= RUE_BMCR_SPD_SET;
436	else
437		bmcr &= ~RUE_BMCR_SPD_SET;
438
439	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
440		bmcr |= RUE_BMCR_DUPLEX;
441	else
442		bmcr &= ~RUE_BMCR_DUPLEX;
443
444	rue_csr_write_2(sc, RUE_BMCR, bmcr);
445
446	RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
447
448	if (!locked)
449		RUE_UNLOCK(sc);
450#endif
451}
452
453static void
454rue_setpromisc(struct usb_ether *ue)
455{
456	struct rue_softc *sc = uether_getsc(ue);
457	struct ifnet *ifp = uether_getifp(ue);
458
459	RUE_LOCK_ASSERT(sc, MA_OWNED);
460
461	/* If we want promiscuous mode, set the allframes bit. */
462	if (ifp->if_flags & IFF_PROMISC)
463		RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP);
464	else
465		RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP);
466}
467
468/*
469 * Program the 64-bit multicast hash filter.
470 */
471static void
472rue_setmulti(struct usb_ether *ue)
473{
474	struct rue_softc *sc = uether_getsc(ue);
475	struct ifnet *ifp = uether_getifp(ue);
476	uint16_t rxcfg;
477	int h = 0;
478	uint32_t hashes[2] = { 0, 0 };
479	struct ifmultiaddr *ifma;
480	int mcnt = 0;
481
482	RUE_LOCK_ASSERT(sc, MA_OWNED);
483
484	rxcfg = rue_csr_read_2(sc, RUE_RCR);
485
486	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
487		rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
488		rxcfg &= ~RUE_RCR_AM;
489		rue_csr_write_2(sc, RUE_RCR, rxcfg);
490		rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
491		rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
492		return;
493	}
494
495	/* first, zot all the existing hash bits */
496	rue_csr_write_4(sc, RUE_MAR0, 0);
497	rue_csr_write_4(sc, RUE_MAR4, 0);
498
499	/* now program new ones */
500	if_maddr_rlock(ifp);
501	TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
502	{
503		if (ifma->ifma_addr->sa_family != AF_LINK)
504			continue;
505		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
506		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
507		if (h < 32)
508			hashes[0] |= (1 << h);
509		else
510			hashes[1] |= (1 << (h - 32));
511		mcnt++;
512	}
513	if_maddr_runlock(ifp);
514
515	if (mcnt)
516		rxcfg |= RUE_RCR_AM;
517	else
518		rxcfg &= ~RUE_RCR_AM;
519
520	rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
521
522	rue_csr_write_2(sc, RUE_RCR, rxcfg);
523	rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
524	rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
525}
526
527static void
528rue_reset(struct rue_softc *sc)
529{
530	int i;
531
532	rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
533
534	for (i = 0; i != RUE_TIMEOUT; i++) {
535		if (uether_pause(&sc->sc_ue, hz / 1000))
536			break;
537		if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
538			break;
539	}
540	if (i == RUE_TIMEOUT)
541		device_printf(sc->sc_ue.ue_dev, "reset never completed\n");
542
543	uether_pause(&sc->sc_ue, hz / 100);
544}
545
546static void
547rue_attach_post(struct usb_ether *ue)
548{
549	struct rue_softc *sc = uether_getsc(ue);
550
551	/* reset the adapter */
552	rue_reset(sc);
553
554	/* get station address from the EEPROM */
555	rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN);
556}
557
558/*
559 * Probe for a RTL8150 chip.
560 */
561static int
562rue_probe(device_t dev)
563{
564	struct usb_attach_arg *uaa = device_get_ivars(dev);
565
566	if (uaa->usb_mode != USB_MODE_HOST)
567		return (ENXIO);
568	if (uaa->info.bConfigIndex != RUE_CONFIG_IDX)
569		return (ENXIO);
570	if (uaa->info.bIfaceIndex != RUE_IFACE_IDX)
571		return (ENXIO);
572
573	return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa));
574}
575
576/*
577 * Attach the interface. Allocate softc structures, do ifmedia
578 * setup and ethernet/BPF attach.
579 */
580static int
581rue_attach(device_t dev)
582{
583	struct usb_attach_arg *uaa = device_get_ivars(dev);
584	struct rue_softc *sc = device_get_softc(dev);
585	struct usb_ether *ue = &sc->sc_ue;
586	uint8_t iface_index;
587	int error;
588
589	device_set_usb_desc(dev);
590	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
591
592	iface_index = RUE_IFACE_IDX;
593	error = usbd_transfer_setup(uaa->device, &iface_index,
594	    sc->sc_xfer, rue_config, RUE_N_TRANSFER,
595	    sc, &sc->sc_mtx);
596	if (error) {
597		device_printf(dev, "allocating USB transfers failed\n");
598		goto detach;
599	}
600
601	ue->ue_sc = sc;
602	ue->ue_dev = dev;
603	ue->ue_udev = uaa->device;
604	ue->ue_mtx = &sc->sc_mtx;
605	ue->ue_methods = &rue_ue_methods;
606
607	error = uether_ifattach(ue);
608	if (error) {
609		device_printf(dev, "could not attach interface\n");
610		goto detach;
611	}
612	return (0);			/* success */
613
614detach:
615	rue_detach(dev);
616	return (ENXIO);			/* failure */
617}
618
619static int
620rue_detach(device_t dev)
621{
622	struct rue_softc *sc = device_get_softc(dev);
623	struct usb_ether *ue = &sc->sc_ue;
624
625	usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER);
626	uether_ifdetach(ue);
627	mtx_destroy(&sc->sc_mtx);
628
629	return (0);
630}
631
632static void
633rue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
634{
635	struct rue_softc *sc = usbd_xfer_softc(xfer);
636	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
637	struct rue_intrpkt pkt;
638	struct usb_page_cache *pc;
639	int actlen;
640
641	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
642
643	switch (USB_GET_STATE(xfer)) {
644	case USB_ST_TRANSFERRED:
645
646		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
647		    actlen >= (int)sizeof(pkt)) {
648
649			pc = usbd_xfer_get_frame(xfer, 0);
650			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
651
652			if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_rxlost_cnt);
653			if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_crcerr_cnt);
654			if_inc_counter(ifp, IFCOUNTER_COLLISIONS, pkt.rue_col_cnt);
655		}
656		/* FALLTHROUGH */
657	case USB_ST_SETUP:
658tr_setup:
659		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
660		usbd_transfer_submit(xfer);
661		return;
662
663	default:			/* Error */
664		if (error != USB_ERR_CANCELLED) {
665			/* try to clear stall first */
666			usbd_xfer_set_stall(xfer);
667			goto tr_setup;
668		}
669		return;
670	}
671}
672
673static void
674rue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
675{
676	struct rue_softc *sc = usbd_xfer_softc(xfer);
677	struct usb_ether *ue = &sc->sc_ue;
678	struct ifnet *ifp = uether_getifp(ue);
679	struct usb_page_cache *pc;
680	uint16_t status;
681	int actlen;
682
683	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
684
685	switch (USB_GET_STATE(xfer)) {
686	case USB_ST_TRANSFERRED:
687
688		if (actlen < 4) {
689			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
690			goto tr_setup;
691		}
692		pc = usbd_xfer_get_frame(xfer, 0);
693		usbd_copy_out(pc, actlen - 4, &status, sizeof(status));
694		actlen -= 4;
695
696		/* check receive packet was valid or not */
697		status = le16toh(status);
698		if ((status & RUE_RXSTAT_VALID) == 0) {
699			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
700			goto tr_setup;
701		}
702		uether_rxbuf(ue, pc, 0, actlen);
703		/* FALLTHROUGH */
704	case USB_ST_SETUP:
705tr_setup:
706		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
707		usbd_transfer_submit(xfer);
708		uether_rxflush(ue);
709		return;
710
711	default:			/* Error */
712		DPRINTF("bulk read error, %s\n",
713		    usbd_errstr(error));
714
715		if (error != USB_ERR_CANCELLED) {
716			/* try to clear stall first */
717			usbd_xfer_set_stall(xfer);
718			goto tr_setup;
719		}
720		return;
721	}
722}
723
724static void
725rue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
726{
727	struct rue_softc *sc = usbd_xfer_softc(xfer);
728	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
729	struct usb_page_cache *pc;
730	struct mbuf *m;
731	int temp_len;
732
733	switch (USB_GET_STATE(xfer)) {
734	case USB_ST_TRANSFERRED:
735		DPRINTFN(11, "transfer complete\n");
736		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
737
738		/* FALLTHROUGH */
739	case USB_ST_SETUP:
740tr_setup:
741		if ((sc->sc_flags & RUE_FLAG_LINK) == 0) {
742			/*
743			 * don't send anything if there is no link !
744			 */
745			return;
746		}
747		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
748
749		if (m == NULL)
750			return;
751		if (m->m_pkthdr.len > MCLBYTES)
752			m->m_pkthdr.len = MCLBYTES;
753		temp_len = m->m_pkthdr.len;
754
755		pc = usbd_xfer_get_frame(xfer, 0);
756		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
757
758		/*
759		 * This is an undocumented behavior.
760		 * RTL8150 chip doesn't send frame length smaller than
761		 * RUE_MIN_FRAMELEN (60) byte packet.
762		 */
763		if (temp_len < RUE_MIN_FRAMELEN) {
764			usbd_frame_zero(pc, temp_len,
765			    RUE_MIN_FRAMELEN - temp_len);
766			temp_len = RUE_MIN_FRAMELEN;
767		}
768		usbd_xfer_set_frame_len(xfer, 0, temp_len);
769
770		/*
771		 * if there's a BPF listener, bounce a copy
772		 * of this frame to him:
773		 */
774		BPF_MTAP(ifp, m);
775
776		m_freem(m);
777
778		usbd_transfer_submit(xfer);
779
780		return;
781
782	default:			/* Error */
783		DPRINTFN(11, "transfer error, %s\n",
784		    usbd_errstr(error));
785
786		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
787
788		if (error != USB_ERR_CANCELLED) {
789			/* try to clear stall first */
790			usbd_xfer_set_stall(xfer);
791			goto tr_setup;
792		}
793		return;
794	}
795}
796
797static void
798rue_tick(struct usb_ether *ue)
799{
800	struct rue_softc *sc = uether_getsc(ue);
801	struct mii_data *mii = GET_MII(sc);
802
803	RUE_LOCK_ASSERT(sc, MA_OWNED);
804
805	mii_tick(mii);
806	if ((sc->sc_flags & RUE_FLAG_LINK) == 0
807	    && mii->mii_media_status & IFM_ACTIVE &&
808	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
809		sc->sc_flags |= RUE_FLAG_LINK;
810		rue_start(ue);
811	}
812}
813
814static void
815rue_start(struct usb_ether *ue)
816{
817	struct rue_softc *sc = uether_getsc(ue);
818
819	/*
820	 * start the USB transfers, if not already started:
821	 */
822	usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]);
823	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]);
824	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]);
825}
826
827static void
828rue_init(struct usb_ether *ue)
829{
830	struct rue_softc *sc = uether_getsc(ue);
831	struct ifnet *ifp = uether_getifp(ue);
832
833	RUE_LOCK_ASSERT(sc, MA_OWNED);
834
835	/*
836	 * Cancel pending I/O
837	 */
838	rue_reset(sc);
839
840	/* Set MAC address */
841	rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
842
843	rue_stop(ue);
844
845	/*
846	 * Set the initial TX and RX configuration.
847	 */
848	rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
849	rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB);
850
851	/* Load the multicast filter */
852	rue_setpromisc(ue);
853	/* Load the multicast filter. */
854	rue_setmulti(ue);
855
856	/* Enable RX and TX */
857	rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
858
859	usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]);
860
861	ifp->if_drv_flags |= IFF_DRV_RUNNING;
862	rue_start(ue);
863}
864
865/*
866 * Set media options.
867 */
868static int
869rue_ifmedia_upd(struct ifnet *ifp)
870{
871	struct rue_softc *sc = ifp->if_softc;
872	struct mii_data *mii = GET_MII(sc);
873	struct mii_softc *miisc;
874	int error;
875
876	RUE_LOCK_ASSERT(sc, MA_OWNED);
877
878        sc->sc_flags &= ~RUE_FLAG_LINK;
879	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
880		PHY_RESET(miisc);
881	error = mii_mediachg(mii);
882	return (error);
883}
884
885/*
886 * Report current media status.
887 */
888static void
889rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
890{
891	struct rue_softc *sc = ifp->if_softc;
892	struct mii_data *mii = GET_MII(sc);
893
894	RUE_LOCK(sc);
895	mii_pollstat(mii);
896	ifmr->ifm_active = mii->mii_media_active;
897	ifmr->ifm_status = mii->mii_media_status;
898	RUE_UNLOCK(sc);
899}
900
901static void
902rue_stop(struct usb_ether *ue)
903{
904	struct rue_softc *sc = uether_getsc(ue);
905	struct ifnet *ifp = uether_getifp(ue);
906
907	RUE_LOCK_ASSERT(sc, MA_OWNED);
908
909	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
910	sc->sc_flags &= ~RUE_FLAG_LINK;
911
912	/*
913	 * stop all the transfers, if not already stopped:
914	 */
915	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]);
916	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]);
917	usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]);
918
919	rue_csr_write_1(sc, RUE_CR, 0x00);
920
921	rue_reset(sc);
922}
923