1184610Salfred/* $FreeBSD: stable/10/sys/dev/usb/net/usb_ethernet.c 334758 2018-06-07 07:33:46Z 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/10/sys/dev/usb/net/usb_ethernet.c 334758 2018-06-07 07:33:46Z 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>
46226709Syongari#include <net/ethernet.h>
47226709Syongari#include <net/if_types.h>
48226709Syongari#include <net/if_media.h>
49226709Syongari#include <net/if_vlan_var.h>
50226709Syongari
51226709Syongari#include <dev/mii/mii.h>
52226709Syongari#include <dev/mii/miivar.h>
53226709Syongari
54188942Sthompsa#include <dev/usb/usb.h>
55194677Sthompsa#include <dev/usb/usbdi.h>
56188412Sthompsa
57188942Sthompsa#include <dev/usb/usb_process.h>
58188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
59184610Salfred
60227309Sedstatic SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0,
61227309Sed    "USB Ethernet parameters");
62184610Salfred
63188412Sthompsa#define	UE_LOCK(_ue)		mtx_lock((_ue)->ue_mtx)
64188412Sthompsa#define	UE_UNLOCK(_ue)		mtx_unlock((_ue)->ue_mtx)
65188412Sthompsa#define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
66188412Sthompsa
67188942SthompsaMODULE_DEPEND(uether, usb, 1, 1, 1);
68188942SthompsaMODULE_DEPEND(uether, miibus, 1, 1, 1);
69188552Sthompsa
70188412Sthompsastatic struct unrhdr *ueunit;
71188412Sthompsa
72193045Sthompsastatic usb_proc_callback_t ue_attach_post_task;
73193045Sthompsastatic usb_proc_callback_t ue_promisc_task;
74193045Sthompsastatic usb_proc_callback_t ue_setmulti_task;
75193045Sthompsastatic usb_proc_callback_t ue_ifmedia_task;
76193045Sthompsastatic usb_proc_callback_t ue_tick_task;
77193045Sthompsastatic usb_proc_callback_t ue_start_task;
78193045Sthompsastatic usb_proc_callback_t ue_stop_task;
79188412Sthompsa
80188412Sthompsastatic void	ue_init(void *);
81188412Sthompsastatic void	ue_start(struct ifnet *);
82188412Sthompsastatic int	ue_ifmedia_upd(struct ifnet *);
83188412Sthompsastatic void	ue_watchdog(void *);
84188412Sthompsa
85188412Sthompsa/*
86188412Sthompsa * Return values:
87188412Sthompsa *    0: success
88188412Sthompsa * Else: device has been detached
89188412Sthompsa */
90188412Sthompsauint8_t
91194228Sthompsauether_pause(struct usb_ether *ue, unsigned int _ticks)
92184610Salfred{
93194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
94188412Sthompsa		/* nothing to do */
95188412Sthompsa		return (1);
96188412Sthompsa	}
97194228Sthompsa	usb_pause_mtx(ue->ue_mtx, _ticks);
98188412Sthompsa	return (0);
99188412Sthompsa}
100184610Salfred
101188412Sthompsastatic void
102192984Sthompsaue_queue_command(struct usb_ether *ue,
103193045Sthompsa    usb_proc_callback_t *fn,
104192984Sthompsa    struct usb_proc_msg *t0, struct usb_proc_msg *t1)
105188412Sthompsa{
106192984Sthompsa	struct usb_ether_cfg_task *task;
107188412Sthompsa
108188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
109188412Sthompsa
110194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
111188412Sthompsa		return;         /* nothing to do */
112184610Salfred	}
113188412Sthompsa	/*
114188412Sthompsa	 * NOTE: The task cannot get executed before we drop the
115188412Sthompsa	 * "sc_mtx" mutex. It is safe to update fields in the message
116188412Sthompsa	 * structure after that the message got queued.
117188412Sthompsa	 */
118192984Sthompsa	task = (struct usb_ether_cfg_task *)
119194228Sthompsa	  usb_proc_msignal(&ue->ue_tq, t0, t1);
120188412Sthompsa
121188412Sthompsa	/* Setup callback and self pointers */
122188412Sthompsa	task->hdr.pm_callback = fn;
123188412Sthompsa	task->ue = ue;
124188412Sthompsa
125188412Sthompsa	/*
126188412Sthompsa	 * Start and stop must be synchronous!
127188412Sthompsa	 */
128188412Sthompsa	if ((fn == ue_start_task) || (fn == ue_stop_task))
129194228Sthompsa		usb_proc_mwait(&ue->ue_tq, t0, t1);
130184610Salfred}
131184610Salfred
132188412Sthompsastruct ifnet *
133194228Sthompsauether_getifp(struct usb_ether *ue)
134184610Salfred{
135188412Sthompsa	return (ue->ue_ifp);
136188412Sthompsa}
137184610Salfred
138188412Sthompsastruct mii_data *
139194228Sthompsauether_getmii(struct usb_ether *ue)
140188412Sthompsa{
141188412Sthompsa	return (device_get_softc(ue->ue_miibus));
142188412Sthompsa}
143188412Sthompsa
144188412Sthompsavoid *
145194228Sthompsauether_getsc(struct usb_ether *ue)
146188412Sthompsa{
147188412Sthompsa	return (ue->ue_sc);
148188412Sthompsa}
149188412Sthompsa
150188412Sthompsastatic int
151188412Sthompsaue_sysctl_parent(SYSCTL_HANDLER_ARGS)
152188412Sthompsa{
153192984Sthompsa	struct usb_ether *ue = arg1;
154188412Sthompsa	const char *name;
155188412Sthompsa
156188412Sthompsa	name = device_get_nameunit(ue->ue_dev);
157188412Sthompsa	return SYSCTL_OUT(req, name, strlen(name));
158188412Sthompsa}
159188412Sthompsa
160188412Sthompsaint
161194228Sthompsauether_ifattach(struct usb_ether *ue)
162188412Sthompsa{
163188412Sthompsa	int error;
164188412Sthompsa
165188412Sthompsa	/* check some critical parameters */
166188412Sthompsa	if ((ue->ue_dev == NULL) ||
167188412Sthompsa	    (ue->ue_udev == NULL) ||
168188412Sthompsa	    (ue->ue_mtx == NULL) ||
169188412Sthompsa	    (ue->ue_methods == NULL))
170188412Sthompsa		return (EINVAL);
171188412Sthompsa
172194228Sthompsa	error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
173188412Sthompsa	    device_get_nameunit(ue->ue_dev), USB_PRI_MED);
174188412Sthompsa	if (error) {
175188412Sthompsa		device_printf(ue->ue_dev, "could not setup taskqueue\n");
176188412Sthompsa		goto error;
177188412Sthompsa	}
178188412Sthompsa
179188412Sthompsa	/* fork rest of the attach code */
180188412Sthompsa	UE_LOCK(ue);
181188412Sthompsa	ue_queue_command(ue, ue_attach_post_task,
182188412Sthompsa	    &ue->ue_sync_task[0].hdr,
183188412Sthompsa	    &ue->ue_sync_task[1].hdr);
184188412Sthompsa	UE_UNLOCK(ue);
185188412Sthompsa
186188412Sthompsaerror:
187188412Sthompsa	return (error);
188188412Sthompsa}
189188412Sthompsa
190334758Shselaskyvoid
191334758Shselaskyuether_ifattach_wait(struct usb_ether *ue)
192334758Shselasky{
193334758Shselasky
194334758Shselasky	UE_LOCK(ue);
195334758Shselasky	usb_proc_mwait(&ue->ue_tq,
196334758Shselasky	    &ue->ue_sync_task[0].hdr,
197334758Shselasky	    &ue->ue_sync_task[1].hdr);
198334758Shselasky	UE_UNLOCK(ue);
199334758Shselasky}
200334758Shselasky
201188412Sthompsastatic void
202192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task)
203188412Sthompsa{
204192984Sthompsa	struct usb_ether_cfg_task *task =
205192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
206192984Sthompsa	struct usb_ether *ue = task->ue;
207188412Sthompsa	struct ifnet *ifp;
208188412Sthompsa	int error;
209188412Sthompsa	char num[14];			/* sufficient for 32 bits */
210188412Sthompsa
211188412Sthompsa	/* first call driver's post attach routine */
212188412Sthompsa	ue->ue_methods->ue_attach_post(ue);
213188412Sthompsa
214188412Sthompsa	UE_UNLOCK(ue);
215188412Sthompsa
216188412Sthompsa	ue->ue_unit = alloc_unr(ueunit);
217194228Sthompsa	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
218188412Sthompsa	sysctl_ctx_init(&ue->ue_sysctl_ctx);
219188412Sthompsa
220226709Syongari	error = 0;
221262436Srodrigc	CURVNET_SET_QUIET(vnet0);
222188412Sthompsa	ifp = if_alloc(IFT_ETHER);
223184610Salfred	if (ifp == NULL) {
224188412Sthompsa		device_printf(ue->ue_dev, "could not allocate ifnet\n");
225226709Syongari		goto fail;
226184610Salfred	}
227184610Salfred
228188412Sthompsa	ifp->if_softc = ue;
229188412Sthompsa	if_initname(ifp, "ue", ue->ue_unit);
230226709Syongari	if (ue->ue_methods->ue_attach_post_sub != NULL) {
231226709Syongari		ue->ue_ifp = ifp;
232226709Syongari		error = ue->ue_methods->ue_attach_post_sub(ue);
233226709Syongari	} else {
234226709Syongari		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
235226709Syongari		if (ue->ue_methods->ue_ioctl != NULL)
236226709Syongari			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
237226709Syongari		else
238226709Syongari			ifp->if_ioctl = uether_ioctl;
239226709Syongari		ifp->if_start = ue_start;
240226709Syongari		ifp->if_init = ue_init;
241226709Syongari		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
242226709Syongari		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
243226709Syongari		IFQ_SET_READY(&ifp->if_snd);
244226709Syongari		ue->ue_ifp = ifp;
245184610Salfred
246226709Syongari		if (ue->ue_methods->ue_mii_upd != NULL &&
247226709Syongari		    ue->ue_methods->ue_mii_sts != NULL) {
248226709Syongari			/* device_xxx() depends on this */
249226709Syongari			mtx_lock(&Giant);
250226709Syongari			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
251226709Syongari			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
252226709Syongari			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
253226709Syongari			mtx_unlock(&Giant);
254188412Sthompsa		}
255188412Sthompsa	}
256184610Salfred
257226709Syongari	if (error) {
258226709Syongari		device_printf(ue->ue_dev, "attaching PHYs failed\n");
259226709Syongari		goto fail;
260226709Syongari	}
261226709Syongari
262188412Sthompsa	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
263188412Sthompsa	ether_ifattach(ifp, ue->ue_eaddr);
264226709Syongari	/* Tell upper layer we support VLAN oversized frames. */
265226709Syongari	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
266226709Syongari		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
267188412Sthompsa
268262436Srodrigc	CURVNET_RESTORE();
269262436Srodrigc
270188412Sthompsa	snprintf(num, sizeof(num), "%u", ue->ue_unit);
271188412Sthompsa	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
272188412Sthompsa	    &SYSCTL_NODE_CHILDREN(_net, ue),
273188412Sthompsa	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
274188412Sthompsa	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
275188412Sthompsa	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
276217556Smdf	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
277188412Sthompsa	    ue_sysctl_parent, "A", "parent device");
278188412Sthompsa
279188412Sthompsa	UE_LOCK(ue);
280188412Sthompsa	return;
281188412Sthompsa
282226709Syongarifail:
283262436Srodrigc	CURVNET_RESTORE();
284188412Sthompsa	free_unr(ueunit, ue->ue_unit);
285188412Sthompsa	if (ue->ue_ifp != NULL) {
286188412Sthompsa		if_free(ue->ue_ifp);
287188412Sthompsa		ue->ue_ifp = NULL;
288184610Salfred	}
289188412Sthompsa	UE_LOCK(ue);
290188412Sthompsa	return;
291188412Sthompsa}
292184610Salfred
293188412Sthompsavoid
294194228Sthompsauether_ifdetach(struct usb_ether *ue)
295188412Sthompsa{
296188412Sthompsa	struct ifnet *ifp;
297184610Salfred
298188412Sthompsa	/* wait for any post attach or other command to complete */
299194228Sthompsa	usb_proc_drain(&ue->ue_tq);
300184610Salfred
301188412Sthompsa	/* read "ifnet" pointer after taskqueue drain */
302188412Sthompsa	ifp = ue->ue_ifp;
303184610Salfred
304188412Sthompsa	if (ifp != NULL) {
305184610Salfred
306188412Sthompsa		/* we are not running any more */
307188412Sthompsa		UE_LOCK(ue);
308188412Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
309188412Sthompsa		UE_UNLOCK(ue);
310184610Salfred
311188412Sthompsa		/* drain any callouts */
312194228Sthompsa		usb_callout_drain(&ue->ue_watchdog);
313188412Sthompsa
314188412Sthompsa		/* detach miibus */
315188412Sthompsa		if (ue->ue_miibus != NULL) {
316196403Sjhb			mtx_lock(&Giant);	/* device_xxx() depends on this */
317188412Sthompsa			device_delete_child(ue->ue_dev, ue->ue_miibus);
318196403Sjhb			mtx_unlock(&Giant);
319184610Salfred		}
320184610Salfred
321188412Sthompsa		/* detach ethernet */
322188412Sthompsa		ether_ifdetach(ifp);
323184610Salfred
324188412Sthompsa		/* free interface instance */
325188412Sthompsa		if_free(ifp);
326188412Sthompsa
327188412Sthompsa		/* free sysctl */
328188412Sthompsa		sysctl_ctx_free(&ue->ue_sysctl_ctx);
329188412Sthompsa
330188412Sthompsa		/* free unit */
331188412Sthompsa		free_unr(ueunit, ue->ue_unit);
332188412Sthompsa	}
333188412Sthompsa
334188412Sthompsa	/* free taskqueue, if any */
335194228Sthompsa	usb_proc_free(&ue->ue_tq);
336188412Sthompsa}
337188412Sthompsa
338188412Sthompsauint8_t
339194228Sthompsauether_is_gone(struct usb_ether *ue)
340188412Sthompsa{
341194228Sthompsa	return (usb_proc_is_gone(&ue->ue_tq));
342188412Sthompsa}
343188412Sthompsa
344226709Syongarivoid
345226709Syongariuether_init(void *arg)
346226709Syongari{
347226709Syongari
348226709Syongari	ue_init(arg);
349226709Syongari}
350226709Syongari
351188412Sthompsastatic void
352188412Sthompsaue_init(void *arg)
353188412Sthompsa{
354192984Sthompsa	struct usb_ether *ue = arg;
355188412Sthompsa
356188412Sthompsa	UE_LOCK(ue);
357188412Sthompsa	ue_queue_command(ue, ue_start_task,
358188412Sthompsa	    &ue->ue_sync_task[0].hdr,
359188412Sthompsa	    &ue->ue_sync_task[1].hdr);
360188412Sthompsa	UE_UNLOCK(ue);
361188412Sthompsa}
362188412Sthompsa
363188412Sthompsastatic void
364192984Sthompsaue_start_task(struct usb_proc_msg *_task)
365188412Sthompsa{
366192984Sthompsa	struct usb_ether_cfg_task *task =
367192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
368192984Sthompsa	struct usb_ether *ue = task->ue;
369188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
370188412Sthompsa
371188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
372188412Sthompsa
373188412Sthompsa	ue->ue_methods->ue_init(ue);
374188412Sthompsa
375188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
376188412Sthompsa		return;
377188412Sthompsa
378188412Sthompsa	if (ue->ue_methods->ue_tick != NULL)
379194228Sthompsa		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
380188412Sthompsa}
381188412Sthompsa
382188412Sthompsastatic void
383192984Sthompsaue_stop_task(struct usb_proc_msg *_task)
384188412Sthompsa{
385192984Sthompsa	struct usb_ether_cfg_task *task =
386192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
387192984Sthompsa	struct usb_ether *ue = task->ue;
388188412Sthompsa
389188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
390188412Sthompsa
391194228Sthompsa	usb_callout_stop(&ue->ue_watchdog);
392188412Sthompsa
393188412Sthompsa	ue->ue_methods->ue_stop(ue);
394188412Sthompsa}
395188412Sthompsa
396226709Syongarivoid
397226709Syongariuether_start(struct ifnet *ifp)
398226709Syongari{
399226709Syongari
400226709Syongari	ue_start(ifp);
401226709Syongari}
402226709Syongari
403188412Sthompsastatic void
404188412Sthompsaue_start(struct ifnet *ifp)
405188412Sthompsa{
406192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
407188412Sthompsa
408188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
409188412Sthompsa		return;
410188412Sthompsa
411188412Sthompsa	UE_LOCK(ue);
412188412Sthompsa	ue->ue_methods->ue_start(ue);
413188412Sthompsa	UE_UNLOCK(ue);
414188412Sthompsa}
415188412Sthompsa
416188412Sthompsastatic void
417192984Sthompsaue_promisc_task(struct usb_proc_msg *_task)
418188412Sthompsa{
419192984Sthompsa	struct usb_ether_cfg_task *task =
420192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
421192984Sthompsa	struct usb_ether *ue = task->ue;
422188412Sthompsa
423188412Sthompsa	ue->ue_methods->ue_setpromisc(ue);
424188412Sthompsa}
425188412Sthompsa
426188412Sthompsastatic void
427192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task)
428188412Sthompsa{
429192984Sthompsa	struct usb_ether_cfg_task *task =
430192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
431192984Sthompsa	struct usb_ether *ue = task->ue;
432188412Sthompsa
433188412Sthompsa	ue->ue_methods->ue_setmulti(ue);
434188412Sthompsa}
435188412Sthompsa
436226709Syongariint
437226709Syongariuether_ifmedia_upd(struct ifnet *ifp)
438226709Syongari{
439226709Syongari
440226709Syongari	return (ue_ifmedia_upd(ifp));
441226709Syongari}
442226709Syongari
443188412Sthompsastatic int
444188412Sthompsaue_ifmedia_upd(struct ifnet *ifp)
445188412Sthompsa{
446192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
447188412Sthompsa
448188412Sthompsa	/* Defer to process context */
449188412Sthompsa	UE_LOCK(ue);
450188412Sthompsa	ue_queue_command(ue, ue_ifmedia_task,
451188412Sthompsa	    &ue->ue_media_task[0].hdr,
452188412Sthompsa	    &ue->ue_media_task[1].hdr);
453188412Sthompsa	UE_UNLOCK(ue);
454188412Sthompsa
455188412Sthompsa	return (0);
456188412Sthompsa}
457188412Sthompsa
458188412Sthompsastatic void
459192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task)
460188412Sthompsa{
461192984Sthompsa	struct usb_ether_cfg_task *task =
462192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
463192984Sthompsa	struct usb_ether *ue = task->ue;
464188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
465188412Sthompsa
466188412Sthompsa	ue->ue_methods->ue_mii_upd(ifp);
467188412Sthompsa}
468188412Sthompsa
469188412Sthompsastatic void
470188412Sthompsaue_watchdog(void *arg)
471188412Sthompsa{
472192984Sthompsa	struct usb_ether *ue = arg;
473188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
474188412Sthompsa
475188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
476188412Sthompsa		return;
477188412Sthompsa
478188412Sthompsa	ue_queue_command(ue, ue_tick_task,
479188412Sthompsa	    &ue->ue_tick_task[0].hdr,
480188412Sthompsa	    &ue->ue_tick_task[1].hdr);
481188412Sthompsa
482194228Sthompsa	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
483188412Sthompsa}
484188412Sthompsa
485188412Sthompsastatic void
486192984Sthompsaue_tick_task(struct usb_proc_msg *_task)
487188412Sthompsa{
488192984Sthompsa	struct usb_ether_cfg_task *task =
489192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
490192984Sthompsa	struct usb_ether *ue = task->ue;
491188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
492188412Sthompsa
493188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
494188412Sthompsa		return;
495188412Sthompsa
496188412Sthompsa	ue->ue_methods->ue_tick(ue);
497188412Sthompsa}
498188412Sthompsa
499188412Sthompsaint
500194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
501188412Sthompsa{
502192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
503188412Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
504188412Sthompsa	struct mii_data *mii;
505188412Sthompsa	int error = 0;
506188412Sthompsa
507188412Sthompsa	switch (command) {
508188412Sthompsa	case SIOCSIFFLAGS:
509188412Sthompsa		UE_LOCK(ue);
510188412Sthompsa		if (ifp->if_flags & IFF_UP) {
511188412Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
512188412Sthompsa				ue_queue_command(ue, ue_promisc_task,
513188412Sthompsa				    &ue->ue_promisc_task[0].hdr,
514188412Sthompsa				    &ue->ue_promisc_task[1].hdr);
515188412Sthompsa			else
516188412Sthompsa				ue_queue_command(ue, ue_start_task,
517188412Sthompsa				    &ue->ue_sync_task[0].hdr,
518188412Sthompsa				    &ue->ue_sync_task[1].hdr);
519188412Sthompsa		} else {
520188412Sthompsa			ue_queue_command(ue, ue_stop_task,
521188412Sthompsa			    &ue->ue_sync_task[0].hdr,
522188412Sthompsa			    &ue->ue_sync_task[1].hdr);
523184610Salfred		}
524188412Sthompsa		UE_UNLOCK(ue);
525188412Sthompsa		break;
526188412Sthompsa	case SIOCADDMULTI:
527188412Sthompsa	case SIOCDELMULTI:
528188412Sthompsa		UE_LOCK(ue);
529188412Sthompsa		ue_queue_command(ue, ue_setmulti_task,
530188412Sthompsa		    &ue->ue_multi_task[0].hdr,
531188412Sthompsa		    &ue->ue_multi_task[1].hdr);
532188412Sthompsa		UE_UNLOCK(ue);
533188412Sthompsa		break;
534188412Sthompsa	case SIOCGIFMEDIA:
535188412Sthompsa	case SIOCSIFMEDIA:
536188412Sthompsa		if (ue->ue_miibus != NULL) {
537188412Sthompsa			mii = device_get_softc(ue->ue_miibus);
538188412Sthompsa			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
539188412Sthompsa		} else
540188412Sthompsa			error = ether_ioctl(ifp, command, data);
541188412Sthompsa		break;
542188412Sthompsa	default:
543188412Sthompsa		error = ether_ioctl(ifp, command, data);
544188412Sthompsa		break;
545184610Salfred	}
546188412Sthompsa	return (error);
547184610Salfred}
548188412Sthompsa
549188412Sthompsastatic int
550194228Sthompsauether_modevent(module_t mod, int type, void *data)
551188412Sthompsa{
552188412Sthompsa
553188412Sthompsa	switch (type) {
554188412Sthompsa	case MOD_LOAD:
555188412Sthompsa		ueunit = new_unrhdr(0, INT_MAX, NULL);
556188412Sthompsa		break;
557188412Sthompsa	case MOD_UNLOAD:
558188412Sthompsa		break;
559188412Sthompsa	default:
560188412Sthompsa		return (EOPNOTSUPP);
561188412Sthompsa	}
562188412Sthompsa	return (0);
563188412Sthompsa}
564194228Sthompsastatic moduledata_t uether_mod = {
565188942Sthompsa	"uether",
566194228Sthompsa	uether_modevent,
567241394Skevlo	0
568188412Sthompsa};
569188412Sthompsa
570189528Sthompsastruct mbuf *
571194228Sthompsauether_newbuf(void)
572189528Sthompsa{
573189528Sthompsa	struct mbuf *m_new;
574189528Sthompsa
575243857Sglebius	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
576189528Sthompsa	if (m_new == NULL)
577189528Sthompsa		return (NULL);
578189528Sthompsa	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
579189528Sthompsa
580189528Sthompsa	m_adj(m_new, ETHER_ALIGN);
581189528Sthompsa	return (m_new);
582189528Sthompsa}
583189528Sthompsa
584188412Sthompsaint
585194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
586188412Sthompsa    unsigned int len)
587188412Sthompsa{
588188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
589188412Sthompsa
590188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
591188412Sthompsa
592188412Sthompsa	/* finalize mbuf */
593188412Sthompsa	ifp->if_ipackets++;
594188412Sthompsa	m->m_pkthdr.rcvif = ifp;
595188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
596188412Sthompsa
597188412Sthompsa	/* enqueue for later when the lock can be released */
598188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
599188412Sthompsa	return (0);
600188412Sthompsa}
601188412Sthompsa
602188412Sthompsaint
603194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
604188412Sthompsa    unsigned int offset, unsigned int len)
605188412Sthompsa{
606188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
607188412Sthompsa	struct mbuf *m;
608188412Sthompsa
609188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
610188412Sthompsa
611189528Sthompsa	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
612188412Sthompsa		return (1);
613188412Sthompsa
614194228Sthompsa	m = uether_newbuf();
615188412Sthompsa	if (m == NULL) {
616213438Syongari		ifp->if_iqdrops++;
617188412Sthompsa		return (ENOMEM);
618188412Sthompsa	}
619188412Sthompsa
620194228Sthompsa	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
621188412Sthompsa
622188412Sthompsa	/* finalize mbuf */
623188412Sthompsa	ifp->if_ipackets++;
624188412Sthompsa	m->m_pkthdr.rcvif = ifp;
625188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
626188412Sthompsa
627188412Sthompsa	/* enqueue for later when the lock can be released */
628188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
629188412Sthompsa	return (0);
630188412Sthompsa}
631188412Sthompsa
632188412Sthompsavoid
633194228Sthompsauether_rxflush(struct usb_ether *ue)
634188412Sthompsa{
635188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
636188412Sthompsa	struct mbuf *m;
637188412Sthompsa
638188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
639188412Sthompsa
640188412Sthompsa	for (;;) {
641188412Sthompsa		_IF_DEQUEUE(&ue->ue_rxq, m);
642188412Sthompsa		if (m == NULL)
643188412Sthompsa			break;
644188412Sthompsa
645188412Sthompsa		/*
646188412Sthompsa		 * The USB xfer has been resubmitted so its safe to unlock now.
647188412Sthompsa		 */
648188412Sthompsa		UE_UNLOCK(ue);
649188412Sthompsa		ifp->if_input(ifp, m);
650188412Sthompsa		UE_LOCK(ue);
651188412Sthompsa	}
652188412Sthompsa}
653188412Sthompsa
654194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
655188942SthompsaMODULE_VERSION(uether, 1);
656