usb_ethernet.c revision 262142
1184610Salfred/* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 262142 2014-02-18 01:20:26Z rodrigc $ */
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: head/sys/dev/usb/net/usb_ethernet.c 262142 2014-02-18 01:20:26Z rodrigc $");
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);
158188412Sthompsa	return SYSCTL_OUT(req, name, strlen(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
191188412Sthompsastatic void
192192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task)
193188412Sthompsa{
194192984Sthompsa	struct usb_ether_cfg_task *task =
195192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
196192984Sthompsa	struct usb_ether *ue = task->ue;
197188412Sthompsa	struct ifnet *ifp;
198188412Sthompsa	int error;
199188412Sthompsa	char num[14];			/* sufficient for 32 bits */
200188412Sthompsa
201188412Sthompsa	/* first call driver's post attach routine */
202188412Sthompsa	ue->ue_methods->ue_attach_post(ue);
203188412Sthompsa
204188412Sthompsa	UE_UNLOCK(ue);
205188412Sthompsa
206188412Sthompsa	ue->ue_unit = alloc_unr(ueunit);
207194228Sthompsa	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
208188412Sthompsa	sysctl_ctx_init(&ue->ue_sysctl_ctx);
209188412Sthompsa
210226709Syongari	error = 0;
211262142Srodrigc	CURVNET_SET_QUIET(vnet0);
212188412Sthompsa	ifp = if_alloc(IFT_ETHER);
213184610Salfred	if (ifp == NULL) {
214188412Sthompsa		device_printf(ue->ue_dev, "could not allocate ifnet\n");
215226709Syongari		goto fail;
216184610Salfred	}
217184610Salfred
218188412Sthompsa	ifp->if_softc = ue;
219188412Sthompsa	if_initname(ifp, "ue", ue->ue_unit);
220226709Syongari	if (ue->ue_methods->ue_attach_post_sub != NULL) {
221226709Syongari		ue->ue_ifp = ifp;
222226709Syongari		error = ue->ue_methods->ue_attach_post_sub(ue);
223226709Syongari	} else {
224226709Syongari		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
225226709Syongari		if (ue->ue_methods->ue_ioctl != NULL)
226226709Syongari			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
227226709Syongari		else
228226709Syongari			ifp->if_ioctl = uether_ioctl;
229226709Syongari		ifp->if_start = ue_start;
230226709Syongari		ifp->if_init = ue_init;
231226709Syongari		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
232226709Syongari		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
233226709Syongari		IFQ_SET_READY(&ifp->if_snd);
234226709Syongari		ue->ue_ifp = ifp;
235184610Salfred
236226709Syongari		if (ue->ue_methods->ue_mii_upd != NULL &&
237226709Syongari		    ue->ue_methods->ue_mii_sts != NULL) {
238226709Syongari			/* device_xxx() depends on this */
239226709Syongari			mtx_lock(&Giant);
240226709Syongari			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
241226709Syongari			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
242226709Syongari			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
243226709Syongari			mtx_unlock(&Giant);
244188412Sthompsa		}
245188412Sthompsa	}
246184610Salfred
247226709Syongari	if (error) {
248226709Syongari		device_printf(ue->ue_dev, "attaching PHYs failed\n");
249226709Syongari		goto fail;
250226709Syongari	}
251226709Syongari
252188412Sthompsa	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
253188412Sthompsa	ether_ifattach(ifp, ue->ue_eaddr);
254226709Syongari	/* Tell upper layer we support VLAN oversized frames. */
255226709Syongari	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
256226709Syongari		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
257188412Sthompsa
258262142Srodrigc	CURVNET_RESTORE();
259262142Srodrigc
260188412Sthompsa	snprintf(num, sizeof(num), "%u", ue->ue_unit);
261188412Sthompsa	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
262188412Sthompsa	    &SYSCTL_NODE_CHILDREN(_net, ue),
263188412Sthompsa	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
264188412Sthompsa	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
265188412Sthompsa	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
266217556Smdf	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
267188412Sthompsa	    ue_sysctl_parent, "A", "parent device");
268188412Sthompsa
269188412Sthompsa	UE_LOCK(ue);
270188412Sthompsa	return;
271188412Sthompsa
272226709Syongarifail:
273262142Srodrigc	CURVNET_RESTORE();
274188412Sthompsa	free_unr(ueunit, ue->ue_unit);
275188412Sthompsa	if (ue->ue_ifp != NULL) {
276188412Sthompsa		if_free(ue->ue_ifp);
277188412Sthompsa		ue->ue_ifp = NULL;
278184610Salfred	}
279188412Sthompsa	UE_LOCK(ue);
280188412Sthompsa	return;
281188412Sthompsa}
282184610Salfred
283188412Sthompsavoid
284194228Sthompsauether_ifdetach(struct usb_ether *ue)
285188412Sthompsa{
286188412Sthompsa	struct ifnet *ifp;
287184610Salfred
288188412Sthompsa	/* wait for any post attach or other command to complete */
289194228Sthompsa	usb_proc_drain(&ue->ue_tq);
290184610Salfred
291188412Sthompsa	/* read "ifnet" pointer after taskqueue drain */
292188412Sthompsa	ifp = ue->ue_ifp;
293184610Salfred
294188412Sthompsa	if (ifp != NULL) {
295184610Salfred
296188412Sthompsa		/* we are not running any more */
297188412Sthompsa		UE_LOCK(ue);
298188412Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
299188412Sthompsa		UE_UNLOCK(ue);
300184610Salfred
301188412Sthompsa		/* drain any callouts */
302194228Sthompsa		usb_callout_drain(&ue->ue_watchdog);
303188412Sthompsa
304188412Sthompsa		/* detach miibus */
305188412Sthompsa		if (ue->ue_miibus != NULL) {
306196403Sjhb			mtx_lock(&Giant);	/* device_xxx() depends on this */
307188412Sthompsa			device_delete_child(ue->ue_dev, ue->ue_miibus);
308196403Sjhb			mtx_unlock(&Giant);
309184610Salfred		}
310184610Salfred
311188412Sthompsa		/* detach ethernet */
312188412Sthompsa		ether_ifdetach(ifp);
313184610Salfred
314188412Sthompsa		/* free interface instance */
315188412Sthompsa		if_free(ifp);
316188412Sthompsa
317188412Sthompsa		/* free sysctl */
318188412Sthompsa		sysctl_ctx_free(&ue->ue_sysctl_ctx);
319188412Sthompsa
320188412Sthompsa		/* free unit */
321188412Sthompsa		free_unr(ueunit, ue->ue_unit);
322188412Sthompsa	}
323188412Sthompsa
324188412Sthompsa	/* free taskqueue, if any */
325194228Sthompsa	usb_proc_free(&ue->ue_tq);
326188412Sthompsa}
327188412Sthompsa
328188412Sthompsauint8_t
329194228Sthompsauether_is_gone(struct usb_ether *ue)
330188412Sthompsa{
331194228Sthompsa	return (usb_proc_is_gone(&ue->ue_tq));
332188412Sthompsa}
333188412Sthompsa
334226709Syongarivoid
335226709Syongariuether_init(void *arg)
336226709Syongari{
337226709Syongari
338226709Syongari	ue_init(arg);
339226709Syongari}
340226709Syongari
341188412Sthompsastatic void
342188412Sthompsaue_init(void *arg)
343188412Sthompsa{
344192984Sthompsa	struct usb_ether *ue = arg;
345188412Sthompsa
346188412Sthompsa	UE_LOCK(ue);
347188412Sthompsa	ue_queue_command(ue, ue_start_task,
348188412Sthompsa	    &ue->ue_sync_task[0].hdr,
349188412Sthompsa	    &ue->ue_sync_task[1].hdr);
350188412Sthompsa	UE_UNLOCK(ue);
351188412Sthompsa}
352188412Sthompsa
353188412Sthompsastatic void
354192984Sthompsaue_start_task(struct usb_proc_msg *_task)
355188412Sthompsa{
356192984Sthompsa	struct usb_ether_cfg_task *task =
357192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
358192984Sthompsa	struct usb_ether *ue = task->ue;
359188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
360188412Sthompsa
361188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
362188412Sthompsa
363188412Sthompsa	ue->ue_methods->ue_init(ue);
364188412Sthompsa
365188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
366188412Sthompsa		return;
367188412Sthompsa
368188412Sthompsa	if (ue->ue_methods->ue_tick != NULL)
369194228Sthompsa		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
370188412Sthompsa}
371188412Sthompsa
372188412Sthompsastatic void
373192984Sthompsaue_stop_task(struct usb_proc_msg *_task)
374188412Sthompsa{
375192984Sthompsa	struct usb_ether_cfg_task *task =
376192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
377192984Sthompsa	struct usb_ether *ue = task->ue;
378188412Sthompsa
379188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
380188412Sthompsa
381194228Sthompsa	usb_callout_stop(&ue->ue_watchdog);
382188412Sthompsa
383188412Sthompsa	ue->ue_methods->ue_stop(ue);
384188412Sthompsa}
385188412Sthompsa
386226709Syongarivoid
387226709Syongariuether_start(struct ifnet *ifp)
388226709Syongari{
389226709Syongari
390226709Syongari	ue_start(ifp);
391226709Syongari}
392226709Syongari
393188412Sthompsastatic void
394188412Sthompsaue_start(struct ifnet *ifp)
395188412Sthompsa{
396192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
397188412Sthompsa
398188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
399188412Sthompsa		return;
400188412Sthompsa
401188412Sthompsa	UE_LOCK(ue);
402188412Sthompsa	ue->ue_methods->ue_start(ue);
403188412Sthompsa	UE_UNLOCK(ue);
404188412Sthompsa}
405188412Sthompsa
406188412Sthompsastatic void
407192984Sthompsaue_promisc_task(struct usb_proc_msg *_task)
408188412Sthompsa{
409192984Sthompsa	struct usb_ether_cfg_task *task =
410192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
411192984Sthompsa	struct usb_ether *ue = task->ue;
412188412Sthompsa
413188412Sthompsa	ue->ue_methods->ue_setpromisc(ue);
414188412Sthompsa}
415188412Sthompsa
416188412Sthompsastatic void
417192984Sthompsaue_setmulti_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_setmulti(ue);
424188412Sthompsa}
425188412Sthompsa
426226709Syongariint
427226709Syongariuether_ifmedia_upd(struct ifnet *ifp)
428226709Syongari{
429226709Syongari
430226709Syongari	return (ue_ifmedia_upd(ifp));
431226709Syongari}
432226709Syongari
433188412Sthompsastatic int
434188412Sthompsaue_ifmedia_upd(struct ifnet *ifp)
435188412Sthompsa{
436192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
437188412Sthompsa
438188412Sthompsa	/* Defer to process context */
439188412Sthompsa	UE_LOCK(ue);
440188412Sthompsa	ue_queue_command(ue, ue_ifmedia_task,
441188412Sthompsa	    &ue->ue_media_task[0].hdr,
442188412Sthompsa	    &ue->ue_media_task[1].hdr);
443188412Sthompsa	UE_UNLOCK(ue);
444188412Sthompsa
445188412Sthompsa	return (0);
446188412Sthompsa}
447188412Sthompsa
448188412Sthompsastatic void
449192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task)
450188412Sthompsa{
451192984Sthompsa	struct usb_ether_cfg_task *task =
452192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
453192984Sthompsa	struct usb_ether *ue = task->ue;
454188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
455188412Sthompsa
456188412Sthompsa	ue->ue_methods->ue_mii_upd(ifp);
457188412Sthompsa}
458188412Sthompsa
459188412Sthompsastatic void
460188412Sthompsaue_watchdog(void *arg)
461188412Sthompsa{
462192984Sthompsa	struct usb_ether *ue = arg;
463188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
464188412Sthompsa
465188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
466188412Sthompsa		return;
467188412Sthompsa
468188412Sthompsa	ue_queue_command(ue, ue_tick_task,
469188412Sthompsa	    &ue->ue_tick_task[0].hdr,
470188412Sthompsa	    &ue->ue_tick_task[1].hdr);
471188412Sthompsa
472194228Sthompsa	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
473188412Sthompsa}
474188412Sthompsa
475188412Sthompsastatic void
476192984Sthompsaue_tick_task(struct usb_proc_msg *_task)
477188412Sthompsa{
478192984Sthompsa	struct usb_ether_cfg_task *task =
479192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
480192984Sthompsa	struct usb_ether *ue = task->ue;
481188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
482188412Sthompsa
483188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
484188412Sthompsa		return;
485188412Sthompsa
486188412Sthompsa	ue->ue_methods->ue_tick(ue);
487188412Sthompsa}
488188412Sthompsa
489188412Sthompsaint
490194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
491188412Sthompsa{
492192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
493188412Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
494188412Sthompsa	struct mii_data *mii;
495188412Sthompsa	int error = 0;
496188412Sthompsa
497188412Sthompsa	switch (command) {
498188412Sthompsa	case SIOCSIFFLAGS:
499188412Sthompsa		UE_LOCK(ue);
500188412Sthompsa		if (ifp->if_flags & IFF_UP) {
501188412Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
502188412Sthompsa				ue_queue_command(ue, ue_promisc_task,
503188412Sthompsa				    &ue->ue_promisc_task[0].hdr,
504188412Sthompsa				    &ue->ue_promisc_task[1].hdr);
505188412Sthompsa			else
506188412Sthompsa				ue_queue_command(ue, ue_start_task,
507188412Sthompsa				    &ue->ue_sync_task[0].hdr,
508188412Sthompsa				    &ue->ue_sync_task[1].hdr);
509188412Sthompsa		} else {
510188412Sthompsa			ue_queue_command(ue, ue_stop_task,
511188412Sthompsa			    &ue->ue_sync_task[0].hdr,
512188412Sthompsa			    &ue->ue_sync_task[1].hdr);
513184610Salfred		}
514188412Sthompsa		UE_UNLOCK(ue);
515188412Sthompsa		break;
516188412Sthompsa	case SIOCADDMULTI:
517188412Sthompsa	case SIOCDELMULTI:
518188412Sthompsa		UE_LOCK(ue);
519188412Sthompsa		ue_queue_command(ue, ue_setmulti_task,
520188412Sthompsa		    &ue->ue_multi_task[0].hdr,
521188412Sthompsa		    &ue->ue_multi_task[1].hdr);
522188412Sthompsa		UE_UNLOCK(ue);
523188412Sthompsa		break;
524188412Sthompsa	case SIOCGIFMEDIA:
525188412Sthompsa	case SIOCSIFMEDIA:
526188412Sthompsa		if (ue->ue_miibus != NULL) {
527188412Sthompsa			mii = device_get_softc(ue->ue_miibus);
528188412Sthompsa			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
529188412Sthompsa		} else
530188412Sthompsa			error = ether_ioctl(ifp, command, data);
531188412Sthompsa		break;
532188412Sthompsa	default:
533188412Sthompsa		error = ether_ioctl(ifp, command, data);
534188412Sthompsa		break;
535184610Salfred	}
536188412Sthompsa	return (error);
537184610Salfred}
538188412Sthompsa
539188412Sthompsastatic int
540194228Sthompsauether_modevent(module_t mod, int type, void *data)
541188412Sthompsa{
542188412Sthompsa
543188412Sthompsa	switch (type) {
544188412Sthompsa	case MOD_LOAD:
545188412Sthompsa		ueunit = new_unrhdr(0, INT_MAX, NULL);
546188412Sthompsa		break;
547188412Sthompsa	case MOD_UNLOAD:
548188412Sthompsa		break;
549188412Sthompsa	default:
550188412Sthompsa		return (EOPNOTSUPP);
551188412Sthompsa	}
552188412Sthompsa	return (0);
553188412Sthompsa}
554194228Sthompsastatic moduledata_t uether_mod = {
555188942Sthompsa	"uether",
556194228Sthompsa	uether_modevent,
557241394Skevlo	0
558188412Sthompsa};
559188412Sthompsa
560189528Sthompsastruct mbuf *
561194228Sthompsauether_newbuf(void)
562189528Sthompsa{
563189528Sthompsa	struct mbuf *m_new;
564189528Sthompsa
565243857Sglebius	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
566189528Sthompsa	if (m_new == NULL)
567189528Sthompsa		return (NULL);
568189528Sthompsa	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
569189528Sthompsa
570189528Sthompsa	m_adj(m_new, ETHER_ALIGN);
571189528Sthompsa	return (m_new);
572189528Sthompsa}
573189528Sthompsa
574188412Sthompsaint
575194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
576188412Sthompsa    unsigned int len)
577188412Sthompsa{
578188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
579188412Sthompsa
580188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
581188412Sthompsa
582188412Sthompsa	/* finalize mbuf */
583188412Sthompsa	ifp->if_ipackets++;
584188412Sthompsa	m->m_pkthdr.rcvif = ifp;
585188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
586188412Sthompsa
587188412Sthompsa	/* enqueue for later when the lock can be released */
588188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
589188412Sthompsa	return (0);
590188412Sthompsa}
591188412Sthompsa
592188412Sthompsaint
593194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
594188412Sthompsa    unsigned int offset, unsigned int len)
595188412Sthompsa{
596188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
597188412Sthompsa	struct mbuf *m;
598188412Sthompsa
599188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
600188412Sthompsa
601189528Sthompsa	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
602188412Sthompsa		return (1);
603188412Sthompsa
604194228Sthompsa	m = uether_newbuf();
605188412Sthompsa	if (m == NULL) {
606213438Syongari		ifp->if_iqdrops++;
607188412Sthompsa		return (ENOMEM);
608188412Sthompsa	}
609188412Sthompsa
610194228Sthompsa	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
611188412Sthompsa
612188412Sthompsa	/* finalize mbuf */
613188412Sthompsa	ifp->if_ipackets++;
614188412Sthompsa	m->m_pkthdr.rcvif = ifp;
615188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
616188412Sthompsa
617188412Sthompsa	/* enqueue for later when the lock can be released */
618188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
619188412Sthompsa	return (0);
620188412Sthompsa}
621188412Sthompsa
622188412Sthompsavoid
623194228Sthompsauether_rxflush(struct usb_ether *ue)
624188412Sthompsa{
625188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
626188412Sthompsa	struct mbuf *m;
627188412Sthompsa
628188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
629188412Sthompsa
630188412Sthompsa	for (;;) {
631188412Sthompsa		_IF_DEQUEUE(&ue->ue_rxq, m);
632188412Sthompsa		if (m == NULL)
633188412Sthompsa			break;
634188412Sthompsa
635188412Sthompsa		/*
636188412Sthompsa		 * The USB xfer has been resubmitted so its safe to unlock now.
637188412Sthompsa		 */
638188412Sthompsa		UE_UNLOCK(ue);
639188412Sthompsa		ifp->if_input(ifp, m);
640188412Sthompsa		UE_LOCK(ue);
641188412Sthompsa	}
642188412Sthompsa}
643188412Sthompsa
644194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
645188942SthompsaMODULE_VERSION(uether, 1);
646