1184610Salfred/* $FreeBSD: stable/11/sys/dev/usb/net/usb_ethernet.c 334757 2018-06-07 07:32:51Z hselasky $ */
2184610Salfred/*-
3188412Sthompsa * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
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 *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27226709Syongari#include <sys/cdefs.h>
28226709Syongari__FBSDID("$FreeBSD: stable/11/sys/dev/usb/net/usb_ethernet.c 334757 2018-06-07 07:32:51Z hselasky $");
29226709Syongari
30194677Sthompsa#include <sys/param.h>
31194677Sthompsa#include <sys/systm.h>
32226709Syongari#include <sys/bus.h>
33226709Syongari#include <sys/condvar.h>
34194677Sthompsa#include <sys/kernel.h>
35226709Syongari#include <sys/lock.h>
36226709Syongari#include <sys/malloc.h>
37226709Syongari#include <sys/mbuf.h>
38194677Sthompsa#include <sys/module.h>
39194677Sthompsa#include <sys/mutex.h>
40226709Syongari#include <sys/socket.h>
41226709Syongari#include <sys/sockio.h>
42194677Sthompsa#include <sys/sysctl.h>
43194677Sthompsa#include <sys/sx.h>
44194677Sthompsa
45226709Syongari#include <net/if.h>
46257176Sglebius#include <net/if_var.h>
47226709Syongari#include <net/ethernet.h>
48226709Syongari#include <net/if_types.h>
49226709Syongari#include <net/if_media.h>
50226709Syongari#include <net/if_vlan_var.h>
51226709Syongari
52226709Syongari#include <dev/mii/mii.h>
53226709Syongari#include <dev/mii/miivar.h>
54226709Syongari
55188942Sthompsa#include <dev/usb/usb.h>
56194677Sthompsa#include <dev/usb/usbdi.h>
57188412Sthompsa
58188942Sthompsa#include <dev/usb/usb_process.h>
59188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
60184610Salfred
61227309Sedstatic SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0,
62227309Sed    "USB Ethernet parameters");
63184610Salfred
64188412Sthompsa#define	UE_LOCK(_ue)		mtx_lock((_ue)->ue_mtx)
65188412Sthompsa#define	UE_UNLOCK(_ue)		mtx_unlock((_ue)->ue_mtx)
66188412Sthompsa#define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
67188412Sthompsa
68188942SthompsaMODULE_DEPEND(uether, usb, 1, 1, 1);
69188942SthompsaMODULE_DEPEND(uether, miibus, 1, 1, 1);
70188552Sthompsa
71188412Sthompsastatic struct unrhdr *ueunit;
72188412Sthompsa
73193045Sthompsastatic usb_proc_callback_t ue_attach_post_task;
74193045Sthompsastatic usb_proc_callback_t ue_promisc_task;
75193045Sthompsastatic usb_proc_callback_t ue_setmulti_task;
76193045Sthompsastatic usb_proc_callback_t ue_ifmedia_task;
77193045Sthompsastatic usb_proc_callback_t ue_tick_task;
78193045Sthompsastatic usb_proc_callback_t ue_start_task;
79193045Sthompsastatic usb_proc_callback_t ue_stop_task;
80188412Sthompsa
81188412Sthompsastatic void	ue_init(void *);
82188412Sthompsastatic void	ue_start(struct ifnet *);
83188412Sthompsastatic int	ue_ifmedia_upd(struct ifnet *);
84188412Sthompsastatic void	ue_watchdog(void *);
85188412Sthompsa
86188412Sthompsa/*
87188412Sthompsa * Return values:
88188412Sthompsa *    0: success
89188412Sthompsa * Else: device has been detached
90188412Sthompsa */
91188412Sthompsauint8_t
92194228Sthompsauether_pause(struct usb_ether *ue, unsigned int _ticks)
93184610Salfred{
94194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
95188412Sthompsa		/* nothing to do */
96188412Sthompsa		return (1);
97188412Sthompsa	}
98194228Sthompsa	usb_pause_mtx(ue->ue_mtx, _ticks);
99188412Sthompsa	return (0);
100188412Sthompsa}
101184610Salfred
102188412Sthompsastatic void
103192984Sthompsaue_queue_command(struct usb_ether *ue,
104193045Sthompsa    usb_proc_callback_t *fn,
105192984Sthompsa    struct usb_proc_msg *t0, struct usb_proc_msg *t1)
106188412Sthompsa{
107192984Sthompsa	struct usb_ether_cfg_task *task;
108188412Sthompsa
109188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
110188412Sthompsa
111194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
112188412Sthompsa		return;         /* nothing to do */
113184610Salfred	}
114188412Sthompsa	/*
115188412Sthompsa	 * NOTE: The task cannot get executed before we drop the
116188412Sthompsa	 * "sc_mtx" mutex. It is safe to update fields in the message
117188412Sthompsa	 * structure after that the message got queued.
118188412Sthompsa	 */
119192984Sthompsa	task = (struct usb_ether_cfg_task *)
120194228Sthompsa	  usb_proc_msignal(&ue->ue_tq, t0, t1);
121188412Sthompsa
122188412Sthompsa	/* Setup callback and self pointers */
123188412Sthompsa	task->hdr.pm_callback = fn;
124188412Sthompsa	task->ue = ue;
125188412Sthompsa
126188412Sthompsa	/*
127188412Sthompsa	 * Start and stop must be synchronous!
128188412Sthompsa	 */
129188412Sthompsa	if ((fn == ue_start_task) || (fn == ue_stop_task))
130194228Sthompsa		usb_proc_mwait(&ue->ue_tq, t0, t1);
131184610Salfred}
132184610Salfred
133188412Sthompsastruct ifnet *
134194228Sthompsauether_getifp(struct usb_ether *ue)
135184610Salfred{
136188412Sthompsa	return (ue->ue_ifp);
137188412Sthompsa}
138184610Salfred
139188412Sthompsastruct mii_data *
140194228Sthompsauether_getmii(struct usb_ether *ue)
141188412Sthompsa{
142188412Sthompsa	return (device_get_softc(ue->ue_miibus));
143188412Sthompsa}
144188412Sthompsa
145188412Sthompsavoid *
146194228Sthompsauether_getsc(struct usb_ether *ue)
147188412Sthompsa{
148188412Sthompsa	return (ue->ue_sc);
149188412Sthompsa}
150188412Sthompsa
151188412Sthompsastatic int
152188412Sthompsaue_sysctl_parent(SYSCTL_HANDLER_ARGS)
153188412Sthompsa{
154192984Sthompsa	struct usb_ether *ue = arg1;
155188412Sthompsa	const char *name;
156188412Sthompsa
157188412Sthompsa	name = device_get_nameunit(ue->ue_dev);
158280008Sian	return SYSCTL_OUT_STR(req, name);
159188412Sthompsa}
160188412Sthompsa
161188412Sthompsaint
162194228Sthompsauether_ifattach(struct usb_ether *ue)
163188412Sthompsa{
164188412Sthompsa	int error;
165188412Sthompsa
166188412Sthompsa	/* check some critical parameters */
167188412Sthompsa	if ((ue->ue_dev == NULL) ||
168188412Sthompsa	    (ue->ue_udev == NULL) ||
169188412Sthompsa	    (ue->ue_mtx == NULL) ||
170188412Sthompsa	    (ue->ue_methods == NULL))
171188412Sthompsa		return (EINVAL);
172188412Sthompsa
173194228Sthompsa	error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
174188412Sthompsa	    device_get_nameunit(ue->ue_dev), USB_PRI_MED);
175188412Sthompsa	if (error) {
176188412Sthompsa		device_printf(ue->ue_dev, "could not setup taskqueue\n");
177188412Sthompsa		goto error;
178188412Sthompsa	}
179188412Sthompsa
180188412Sthompsa	/* fork rest of the attach code */
181188412Sthompsa	UE_LOCK(ue);
182188412Sthompsa	ue_queue_command(ue, ue_attach_post_task,
183188412Sthompsa	    &ue->ue_sync_task[0].hdr,
184188412Sthompsa	    &ue->ue_sync_task[1].hdr);
185188412Sthompsa	UE_UNLOCK(ue);
186188412Sthompsa
187188412Sthompsaerror:
188188412Sthompsa	return (error);
189188412Sthompsa}
190188412Sthompsa
191334757Shselaskyvoid
192334757Shselaskyuether_ifattach_wait(struct usb_ether *ue)
193334757Shselasky{
194334757Shselasky
195334757Shselasky	UE_LOCK(ue);
196334757Shselasky	usb_proc_mwait(&ue->ue_tq,
197334757Shselasky	    &ue->ue_sync_task[0].hdr,
198334757Shselasky	    &ue->ue_sync_task[1].hdr);
199334757Shselasky	UE_UNLOCK(ue);
200334757Shselasky}
201334757Shselasky
202188412Sthompsastatic void
203192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task)
204188412Sthompsa{
205192984Sthompsa	struct usb_ether_cfg_task *task =
206192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
207192984Sthompsa	struct usb_ether *ue = task->ue;
208188412Sthompsa	struct ifnet *ifp;
209188412Sthompsa	int error;
210188412Sthompsa	char num[14];			/* sufficient for 32 bits */
211188412Sthompsa
212188412Sthompsa	/* first call driver's post attach routine */
213188412Sthompsa	ue->ue_methods->ue_attach_post(ue);
214188412Sthompsa
215188412Sthompsa	UE_UNLOCK(ue);
216188412Sthompsa
217188412Sthompsa	ue->ue_unit = alloc_unr(ueunit);
218194228Sthompsa	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
219188412Sthompsa	sysctl_ctx_init(&ue->ue_sysctl_ctx);
220188412Sthompsa
221226709Syongari	error = 0;
222262142Srodrigc	CURVNET_SET_QUIET(vnet0);
223188412Sthompsa	ifp = if_alloc(IFT_ETHER);
224184610Salfred	if (ifp == NULL) {
225188412Sthompsa		device_printf(ue->ue_dev, "could not allocate ifnet\n");
226226709Syongari		goto fail;
227184610Salfred	}
228184610Salfred
229188412Sthompsa	ifp->if_softc = ue;
230188412Sthompsa	if_initname(ifp, "ue", ue->ue_unit);
231226709Syongari	if (ue->ue_methods->ue_attach_post_sub != NULL) {
232226709Syongari		ue->ue_ifp = ifp;
233226709Syongari		error = ue->ue_methods->ue_attach_post_sub(ue);
234226709Syongari	} else {
235226709Syongari		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
236226709Syongari		if (ue->ue_methods->ue_ioctl != NULL)
237226709Syongari			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
238226709Syongari		else
239226709Syongari			ifp->if_ioctl = uether_ioctl;
240226709Syongari		ifp->if_start = ue_start;
241226709Syongari		ifp->if_init = ue_init;
242226709Syongari		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
243226709Syongari		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
244226709Syongari		IFQ_SET_READY(&ifp->if_snd);
245226709Syongari		ue->ue_ifp = ifp;
246184610Salfred
247226709Syongari		if (ue->ue_methods->ue_mii_upd != NULL &&
248226709Syongari		    ue->ue_methods->ue_mii_sts != NULL) {
249226709Syongari			/* device_xxx() depends on this */
250226709Syongari			mtx_lock(&Giant);
251226709Syongari			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
252226709Syongari			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
253226709Syongari			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
254226709Syongari			mtx_unlock(&Giant);
255188412Sthompsa		}
256188412Sthompsa	}
257184610Salfred
258226709Syongari	if (error) {
259226709Syongari		device_printf(ue->ue_dev, "attaching PHYs failed\n");
260226709Syongari		goto fail;
261226709Syongari	}
262226709Syongari
263188412Sthompsa	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
264188412Sthompsa	ether_ifattach(ifp, ue->ue_eaddr);
265226709Syongari	/* Tell upper layer we support VLAN oversized frames. */
266226709Syongari	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
267226709Syongari		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
268188412Sthompsa
269262142Srodrigc	CURVNET_RESTORE();
270262142Srodrigc
271188412Sthompsa	snprintf(num, sizeof(num), "%u", ue->ue_unit);
272188412Sthompsa	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
273188412Sthompsa	    &SYSCTL_NODE_CHILDREN(_net, ue),
274188412Sthompsa	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
275188412Sthompsa	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
276188412Sthompsa	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
277217556Smdf	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
278188412Sthompsa	    ue_sysctl_parent, "A", "parent device");
279188412Sthompsa
280188412Sthompsa	UE_LOCK(ue);
281188412Sthompsa	return;
282188412Sthompsa
283226709Syongarifail:
284262142Srodrigc	CURVNET_RESTORE();
285188412Sthompsa	free_unr(ueunit, ue->ue_unit);
286188412Sthompsa	if (ue->ue_ifp != NULL) {
287188412Sthompsa		if_free(ue->ue_ifp);
288188412Sthompsa		ue->ue_ifp = NULL;
289184610Salfred	}
290188412Sthompsa	UE_LOCK(ue);
291188412Sthompsa	return;
292188412Sthompsa}
293184610Salfred
294188412Sthompsavoid
295194228Sthompsauether_ifdetach(struct usb_ether *ue)
296188412Sthompsa{
297188412Sthompsa	struct ifnet *ifp;
298184610Salfred
299188412Sthompsa	/* wait for any post attach or other command to complete */
300194228Sthompsa	usb_proc_drain(&ue->ue_tq);
301184610Salfred
302188412Sthompsa	/* read "ifnet" pointer after taskqueue drain */
303188412Sthompsa	ifp = ue->ue_ifp;
304184610Salfred
305188412Sthompsa	if (ifp != NULL) {
306184610Salfred
307188412Sthompsa		/* we are not running any more */
308188412Sthompsa		UE_LOCK(ue);
309188412Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
310188412Sthompsa		UE_UNLOCK(ue);
311184610Salfred
312188412Sthompsa		/* drain any callouts */
313194228Sthompsa		usb_callout_drain(&ue->ue_watchdog);
314188412Sthompsa
315188412Sthompsa		/* detach miibus */
316188412Sthompsa		if (ue->ue_miibus != NULL) {
317196403Sjhb			mtx_lock(&Giant);	/* device_xxx() depends on this */
318188412Sthompsa			device_delete_child(ue->ue_dev, ue->ue_miibus);
319196403Sjhb			mtx_unlock(&Giant);
320184610Salfred		}
321184610Salfred
322188412Sthompsa		/* detach ethernet */
323188412Sthompsa		ether_ifdetach(ifp);
324184610Salfred
325188412Sthompsa		/* free interface instance */
326188412Sthompsa		if_free(ifp);
327188412Sthompsa
328188412Sthompsa		/* free sysctl */
329188412Sthompsa		sysctl_ctx_free(&ue->ue_sysctl_ctx);
330188412Sthompsa
331188412Sthompsa		/* free unit */
332188412Sthompsa		free_unr(ueunit, ue->ue_unit);
333188412Sthompsa	}
334188412Sthompsa
335188412Sthompsa	/* free taskqueue, if any */
336194228Sthompsa	usb_proc_free(&ue->ue_tq);
337188412Sthompsa}
338188412Sthompsa
339188412Sthompsauint8_t
340194228Sthompsauether_is_gone(struct usb_ether *ue)
341188412Sthompsa{
342194228Sthompsa	return (usb_proc_is_gone(&ue->ue_tq));
343188412Sthompsa}
344188412Sthompsa
345226709Syongarivoid
346226709Syongariuether_init(void *arg)
347226709Syongari{
348226709Syongari
349226709Syongari	ue_init(arg);
350226709Syongari}
351226709Syongari
352188412Sthompsastatic void
353188412Sthompsaue_init(void *arg)
354188412Sthompsa{
355192984Sthompsa	struct usb_ether *ue = arg;
356188412Sthompsa
357188412Sthompsa	UE_LOCK(ue);
358188412Sthompsa	ue_queue_command(ue, ue_start_task,
359188412Sthompsa	    &ue->ue_sync_task[0].hdr,
360188412Sthompsa	    &ue->ue_sync_task[1].hdr);
361188412Sthompsa	UE_UNLOCK(ue);
362188412Sthompsa}
363188412Sthompsa
364188412Sthompsastatic void
365192984Sthompsaue_start_task(struct usb_proc_msg *_task)
366188412Sthompsa{
367192984Sthompsa	struct usb_ether_cfg_task *task =
368192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
369192984Sthompsa	struct usb_ether *ue = task->ue;
370188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
371188412Sthompsa
372188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
373188412Sthompsa
374188412Sthompsa	ue->ue_methods->ue_init(ue);
375188412Sthompsa
376188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
377188412Sthompsa		return;
378188412Sthompsa
379188412Sthompsa	if (ue->ue_methods->ue_tick != NULL)
380194228Sthompsa		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
381188412Sthompsa}
382188412Sthompsa
383188412Sthompsastatic void
384192984Sthompsaue_stop_task(struct usb_proc_msg *_task)
385188412Sthompsa{
386192984Sthompsa	struct usb_ether_cfg_task *task =
387192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
388192984Sthompsa	struct usb_ether *ue = task->ue;
389188412Sthompsa
390188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
391188412Sthompsa
392194228Sthompsa	usb_callout_stop(&ue->ue_watchdog);
393188412Sthompsa
394188412Sthompsa	ue->ue_methods->ue_stop(ue);
395188412Sthompsa}
396188412Sthompsa
397226709Syongarivoid
398226709Syongariuether_start(struct ifnet *ifp)
399226709Syongari{
400226709Syongari
401226709Syongari	ue_start(ifp);
402226709Syongari}
403226709Syongari
404188412Sthompsastatic void
405188412Sthompsaue_start(struct ifnet *ifp)
406188412Sthompsa{
407192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
408188412Sthompsa
409188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
410188412Sthompsa		return;
411188412Sthompsa
412188412Sthompsa	UE_LOCK(ue);
413188412Sthompsa	ue->ue_methods->ue_start(ue);
414188412Sthompsa	UE_UNLOCK(ue);
415188412Sthompsa}
416188412Sthompsa
417188412Sthompsastatic void
418192984Sthompsaue_promisc_task(struct usb_proc_msg *_task)
419188412Sthompsa{
420192984Sthompsa	struct usb_ether_cfg_task *task =
421192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
422192984Sthompsa	struct usb_ether *ue = task->ue;
423188412Sthompsa
424188412Sthompsa	ue->ue_methods->ue_setpromisc(ue);
425188412Sthompsa}
426188412Sthompsa
427188412Sthompsastatic void
428192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task)
429188412Sthompsa{
430192984Sthompsa	struct usb_ether_cfg_task *task =
431192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
432192984Sthompsa	struct usb_ether *ue = task->ue;
433188412Sthompsa
434188412Sthompsa	ue->ue_methods->ue_setmulti(ue);
435188412Sthompsa}
436188412Sthompsa
437226709Syongariint
438226709Syongariuether_ifmedia_upd(struct ifnet *ifp)
439226709Syongari{
440226709Syongari
441226709Syongari	return (ue_ifmedia_upd(ifp));
442226709Syongari}
443226709Syongari
444188412Sthompsastatic int
445188412Sthompsaue_ifmedia_upd(struct ifnet *ifp)
446188412Sthompsa{
447192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
448188412Sthompsa
449188412Sthompsa	/* Defer to process context */
450188412Sthompsa	UE_LOCK(ue);
451188412Sthompsa	ue_queue_command(ue, ue_ifmedia_task,
452188412Sthompsa	    &ue->ue_media_task[0].hdr,
453188412Sthompsa	    &ue->ue_media_task[1].hdr);
454188412Sthompsa	UE_UNLOCK(ue);
455188412Sthompsa
456188412Sthompsa	return (0);
457188412Sthompsa}
458188412Sthompsa
459188412Sthompsastatic void
460192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task)
461188412Sthompsa{
462192984Sthompsa	struct usb_ether_cfg_task *task =
463192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
464192984Sthompsa	struct usb_ether *ue = task->ue;
465188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
466188412Sthompsa
467188412Sthompsa	ue->ue_methods->ue_mii_upd(ifp);
468188412Sthompsa}
469188412Sthompsa
470188412Sthompsastatic void
471188412Sthompsaue_watchdog(void *arg)
472188412Sthompsa{
473192984Sthompsa	struct usb_ether *ue = arg;
474188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
475188412Sthompsa
476188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
477188412Sthompsa		return;
478188412Sthompsa
479188412Sthompsa	ue_queue_command(ue, ue_tick_task,
480188412Sthompsa	    &ue->ue_tick_task[0].hdr,
481188412Sthompsa	    &ue->ue_tick_task[1].hdr);
482188412Sthompsa
483194228Sthompsa	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
484188412Sthompsa}
485188412Sthompsa
486188412Sthompsastatic void
487192984Sthompsaue_tick_task(struct usb_proc_msg *_task)
488188412Sthompsa{
489192984Sthompsa	struct usb_ether_cfg_task *task =
490192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
491192984Sthompsa	struct usb_ether *ue = task->ue;
492188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
493188412Sthompsa
494188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
495188412Sthompsa		return;
496188412Sthompsa
497188412Sthompsa	ue->ue_methods->ue_tick(ue);
498188412Sthompsa}
499188412Sthompsa
500188412Sthompsaint
501194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
502188412Sthompsa{
503192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
504188412Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
505188412Sthompsa	struct mii_data *mii;
506188412Sthompsa	int error = 0;
507188412Sthompsa
508188412Sthompsa	switch (command) {
509188412Sthompsa	case SIOCSIFFLAGS:
510188412Sthompsa		UE_LOCK(ue);
511188412Sthompsa		if (ifp->if_flags & IFF_UP) {
512188412Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
513188412Sthompsa				ue_queue_command(ue, ue_promisc_task,
514188412Sthompsa				    &ue->ue_promisc_task[0].hdr,
515188412Sthompsa				    &ue->ue_promisc_task[1].hdr);
516188412Sthompsa			else
517188412Sthompsa				ue_queue_command(ue, ue_start_task,
518188412Sthompsa				    &ue->ue_sync_task[0].hdr,
519188412Sthompsa				    &ue->ue_sync_task[1].hdr);
520188412Sthompsa		} else {
521188412Sthompsa			ue_queue_command(ue, ue_stop_task,
522188412Sthompsa			    &ue->ue_sync_task[0].hdr,
523188412Sthompsa			    &ue->ue_sync_task[1].hdr);
524184610Salfred		}
525188412Sthompsa		UE_UNLOCK(ue);
526188412Sthompsa		break;
527188412Sthompsa	case SIOCADDMULTI:
528188412Sthompsa	case SIOCDELMULTI:
529188412Sthompsa		UE_LOCK(ue);
530188412Sthompsa		ue_queue_command(ue, ue_setmulti_task,
531188412Sthompsa		    &ue->ue_multi_task[0].hdr,
532188412Sthompsa		    &ue->ue_multi_task[1].hdr);
533188412Sthompsa		UE_UNLOCK(ue);
534188412Sthompsa		break;
535188412Sthompsa	case SIOCGIFMEDIA:
536188412Sthompsa	case SIOCSIFMEDIA:
537188412Sthompsa		if (ue->ue_miibus != NULL) {
538188412Sthompsa			mii = device_get_softc(ue->ue_miibus);
539188412Sthompsa			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
540188412Sthompsa		} else
541188412Sthompsa			error = ether_ioctl(ifp, command, data);
542188412Sthompsa		break;
543188412Sthompsa	default:
544188412Sthompsa		error = ether_ioctl(ifp, command, data);
545188412Sthompsa		break;
546184610Salfred	}
547188412Sthompsa	return (error);
548184610Salfred}
549188412Sthompsa
550188412Sthompsastatic int
551194228Sthompsauether_modevent(module_t mod, int type, void *data)
552188412Sthompsa{
553188412Sthompsa
554188412Sthompsa	switch (type) {
555188412Sthompsa	case MOD_LOAD:
556188412Sthompsa		ueunit = new_unrhdr(0, INT_MAX, NULL);
557188412Sthompsa		break;
558188412Sthompsa	case MOD_UNLOAD:
559188412Sthompsa		break;
560188412Sthompsa	default:
561188412Sthompsa		return (EOPNOTSUPP);
562188412Sthompsa	}
563188412Sthompsa	return (0);
564188412Sthompsa}
565194228Sthompsastatic moduledata_t uether_mod = {
566188942Sthompsa	"uether",
567194228Sthompsa	uether_modevent,
568241394Skevlo	0
569188412Sthompsa};
570188412Sthompsa
571189528Sthompsastruct mbuf *
572194228Sthompsauether_newbuf(void)
573189528Sthompsa{
574189528Sthompsa	struct mbuf *m_new;
575189528Sthompsa
576243857Sglebius	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
577189528Sthompsa	if (m_new == NULL)
578189528Sthompsa		return (NULL);
579189528Sthompsa	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
580189528Sthompsa
581189528Sthompsa	m_adj(m_new, ETHER_ALIGN);
582189528Sthompsa	return (m_new);
583189528Sthompsa}
584189528Sthompsa
585188412Sthompsaint
586194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
587188412Sthompsa    unsigned int len)
588188412Sthompsa{
589188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
590188412Sthompsa
591188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
592188412Sthompsa
593188412Sthompsa	/* finalize mbuf */
594271832Sglebius	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
595188412Sthompsa	m->m_pkthdr.rcvif = ifp;
596188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
597188412Sthompsa
598188412Sthompsa	/* enqueue for later when the lock can be released */
599188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
600188412Sthompsa	return (0);
601188412Sthompsa}
602188412Sthompsa
603188412Sthompsaint
604194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
605188412Sthompsa    unsigned int offset, unsigned int len)
606188412Sthompsa{
607188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
608188412Sthompsa	struct mbuf *m;
609188412Sthompsa
610188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
611188412Sthompsa
612189528Sthompsa	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
613188412Sthompsa		return (1);
614188412Sthompsa
615194228Sthompsa	m = uether_newbuf();
616188412Sthompsa	if (m == NULL) {
617271832Sglebius		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
618188412Sthompsa		return (ENOMEM);
619188412Sthompsa	}
620188412Sthompsa
621194228Sthompsa	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
622188412Sthompsa
623188412Sthompsa	/* finalize mbuf */
624271832Sglebius	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
625188412Sthompsa	m->m_pkthdr.rcvif = ifp;
626188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
627188412Sthompsa
628188412Sthompsa	/* enqueue for later when the lock can be released */
629188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
630188412Sthompsa	return (0);
631188412Sthompsa}
632188412Sthompsa
633188412Sthompsavoid
634194228Sthompsauether_rxflush(struct usb_ether *ue)
635188412Sthompsa{
636188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
637188412Sthompsa	struct mbuf *m;
638188412Sthompsa
639188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
640188412Sthompsa
641188412Sthompsa	for (;;) {
642188412Sthompsa		_IF_DEQUEUE(&ue->ue_rxq, m);
643188412Sthompsa		if (m == NULL)
644188412Sthompsa			break;
645188412Sthompsa
646188412Sthompsa		/*
647188412Sthompsa		 * The USB xfer has been resubmitted so its safe to unlock now.
648188412Sthompsa		 */
649188412Sthompsa		UE_UNLOCK(ue);
650188412Sthompsa		ifp->if_input(ifp, m);
651188412Sthompsa		UE_LOCK(ue);
652188412Sthompsa	}
653188412Sthompsa}
654188412Sthompsa
655302054Sbz/*
656302054Sbz * USB net drivers are run by DRIVER_MODULE() thus SI_SUB_DRIVERS,
657302054Sbz * SI_ORDER_MIDDLE.  Run uether after that.
658302054Sbz */
659302054SbzDECLARE_MODULE(uether, uether_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
660188942SthompsaMODULE_VERSION(uether, 1);
661