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