usb_ethernet.c revision 196403
1184610Salfred/* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 196403 2009-08-20 19:17:53Z jhb $ */
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
27194677Sthompsa#include <sys/stdint.h>
28194677Sthompsa#include <sys/stddef.h>
29194677Sthompsa#include <sys/param.h>
30194677Sthompsa#include <sys/queue.h>
31194677Sthompsa#include <sys/types.h>
32194677Sthompsa#include <sys/systm.h>
33194677Sthompsa#include <sys/kernel.h>
34194677Sthompsa#include <sys/bus.h>
35194677Sthompsa#include <sys/linker_set.h>
36194677Sthompsa#include <sys/module.h>
37194677Sthompsa#include <sys/lock.h>
38194677Sthompsa#include <sys/mutex.h>
39194677Sthompsa#include <sys/condvar.h>
40194677Sthompsa#include <sys/sysctl.h>
41194677Sthompsa#include <sys/sx.h>
42194677Sthompsa#include <sys/unistd.h>
43194677Sthompsa#include <sys/callout.h>
44194677Sthompsa#include <sys/malloc.h>
45194677Sthompsa#include <sys/priv.h>
46194677Sthompsa
47188942Sthompsa#include <dev/usb/usb.h>
48194677Sthompsa#include <dev/usb/usbdi.h>
49188412Sthompsa
50188942Sthompsa#include <dev/usb/usb_process.h>
51188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
52184610Salfred
53188412SthompsaSYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, "USB Ethernet parameters");
54184610Salfred
55188412Sthompsa#define	UE_LOCK(_ue)		mtx_lock((_ue)->ue_mtx)
56188412Sthompsa#define	UE_UNLOCK(_ue)		mtx_unlock((_ue)->ue_mtx)
57188412Sthompsa#define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
58188412Sthompsa
59188942SthompsaMODULE_DEPEND(uether, usb, 1, 1, 1);
60188942SthompsaMODULE_DEPEND(uether, miibus, 1, 1, 1);
61188552Sthompsa
62188412Sthompsastatic struct unrhdr *ueunit;
63188412Sthompsa
64193045Sthompsastatic usb_proc_callback_t ue_attach_post_task;
65193045Sthompsastatic usb_proc_callback_t ue_promisc_task;
66193045Sthompsastatic usb_proc_callback_t ue_setmulti_task;
67193045Sthompsastatic usb_proc_callback_t ue_ifmedia_task;
68193045Sthompsastatic usb_proc_callback_t ue_tick_task;
69193045Sthompsastatic usb_proc_callback_t ue_start_task;
70193045Sthompsastatic usb_proc_callback_t ue_stop_task;
71188412Sthompsa
72188412Sthompsastatic void	ue_init(void *);
73188412Sthompsastatic void	ue_start(struct ifnet *);
74188412Sthompsastatic int	ue_ifmedia_upd(struct ifnet *);
75188412Sthompsastatic void	ue_watchdog(void *);
76188412Sthompsa
77188412Sthompsa/*
78188412Sthompsa * Return values:
79188412Sthompsa *    0: success
80188412Sthompsa * Else: device has been detached
81188412Sthompsa */
82188412Sthompsauint8_t
83194228Sthompsauether_pause(struct usb_ether *ue, unsigned int _ticks)
84184610Salfred{
85194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
86188412Sthompsa		/* nothing to do */
87188412Sthompsa		return (1);
88188412Sthompsa	}
89194228Sthompsa	usb_pause_mtx(ue->ue_mtx, _ticks);
90188412Sthompsa	return (0);
91188412Sthompsa}
92184610Salfred
93188412Sthompsastatic void
94192984Sthompsaue_queue_command(struct usb_ether *ue,
95193045Sthompsa    usb_proc_callback_t *fn,
96192984Sthompsa    struct usb_proc_msg *t0, struct usb_proc_msg *t1)
97188412Sthompsa{
98192984Sthompsa	struct usb_ether_cfg_task *task;
99188412Sthompsa
100188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
101188412Sthompsa
102194228Sthompsa	if (usb_proc_is_gone(&ue->ue_tq)) {
103188412Sthompsa		return;         /* nothing to do */
104184610Salfred	}
105188412Sthompsa	/*
106188412Sthompsa	 * NOTE: The task cannot get executed before we drop the
107188412Sthompsa	 * "sc_mtx" mutex. It is safe to update fields in the message
108188412Sthompsa	 * structure after that the message got queued.
109188412Sthompsa	 */
110192984Sthompsa	task = (struct usb_ether_cfg_task *)
111194228Sthompsa	  usb_proc_msignal(&ue->ue_tq, t0, t1);
112188412Sthompsa
113188412Sthompsa	/* Setup callback and self pointers */
114188412Sthompsa	task->hdr.pm_callback = fn;
115188412Sthompsa	task->ue = ue;
116188412Sthompsa
117188412Sthompsa	/*
118188412Sthompsa	 * Start and stop must be synchronous!
119188412Sthompsa	 */
120188412Sthompsa	if ((fn == ue_start_task) || (fn == ue_stop_task))
121194228Sthompsa		usb_proc_mwait(&ue->ue_tq, t0, t1);
122184610Salfred}
123184610Salfred
124188412Sthompsastruct ifnet *
125194228Sthompsauether_getifp(struct usb_ether *ue)
126184610Salfred{
127188412Sthompsa	return (ue->ue_ifp);
128188412Sthompsa}
129184610Salfred
130188412Sthompsastruct mii_data *
131194228Sthompsauether_getmii(struct usb_ether *ue)
132188412Sthompsa{
133188412Sthompsa	return (device_get_softc(ue->ue_miibus));
134188412Sthompsa}
135188412Sthompsa
136188412Sthompsavoid *
137194228Sthompsauether_getsc(struct usb_ether *ue)
138188412Sthompsa{
139188412Sthompsa	return (ue->ue_sc);
140188412Sthompsa}
141188412Sthompsa
142188412Sthompsastatic int
143188412Sthompsaue_sysctl_parent(SYSCTL_HANDLER_ARGS)
144188412Sthompsa{
145192984Sthompsa	struct usb_ether *ue = arg1;
146188412Sthompsa	const char *name;
147188412Sthompsa
148188412Sthompsa	name = device_get_nameunit(ue->ue_dev);
149188412Sthompsa	return SYSCTL_OUT(req, name, strlen(name));
150188412Sthompsa}
151188412Sthompsa
152188412Sthompsaint
153194228Sthompsauether_ifattach(struct usb_ether *ue)
154188412Sthompsa{
155188412Sthompsa	int error;
156188412Sthompsa
157188412Sthompsa	/* check some critical parameters */
158188412Sthompsa	if ((ue->ue_dev == NULL) ||
159188412Sthompsa	    (ue->ue_udev == NULL) ||
160188412Sthompsa	    (ue->ue_mtx == NULL) ||
161188412Sthompsa	    (ue->ue_methods == NULL))
162188412Sthompsa		return (EINVAL);
163188412Sthompsa
164194228Sthompsa	error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
165188412Sthompsa	    device_get_nameunit(ue->ue_dev), USB_PRI_MED);
166188412Sthompsa	if (error) {
167188412Sthompsa		device_printf(ue->ue_dev, "could not setup taskqueue\n");
168188412Sthompsa		goto error;
169188412Sthompsa	}
170188412Sthompsa
171188412Sthompsa	/* fork rest of the attach code */
172188412Sthompsa	UE_LOCK(ue);
173188412Sthompsa	ue_queue_command(ue, ue_attach_post_task,
174188412Sthompsa	    &ue->ue_sync_task[0].hdr,
175188412Sthompsa	    &ue->ue_sync_task[1].hdr);
176188412Sthompsa	UE_UNLOCK(ue);
177188412Sthompsa
178188412Sthompsaerror:
179188412Sthompsa	return (error);
180188412Sthompsa}
181188412Sthompsa
182188412Sthompsastatic void
183192984Sthompsaue_attach_post_task(struct usb_proc_msg *_task)
184188412Sthompsa{
185192984Sthompsa	struct usb_ether_cfg_task *task =
186192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
187192984Sthompsa	struct usb_ether *ue = task->ue;
188188412Sthompsa	struct ifnet *ifp;
189188412Sthompsa	int error;
190188412Sthompsa	char num[14];			/* sufficient for 32 bits */
191188412Sthompsa
192188412Sthompsa	/* first call driver's post attach routine */
193188412Sthompsa	ue->ue_methods->ue_attach_post(ue);
194188412Sthompsa
195188412Sthompsa	UE_UNLOCK(ue);
196188412Sthompsa
197188412Sthompsa	ue->ue_unit = alloc_unr(ueunit);
198194228Sthompsa	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
199188412Sthompsa	sysctl_ctx_init(&ue->ue_sysctl_ctx);
200188412Sthompsa
201188412Sthompsa	ifp = if_alloc(IFT_ETHER);
202184610Salfred	if (ifp == NULL) {
203188412Sthompsa		device_printf(ue->ue_dev, "could not allocate ifnet\n");
204188412Sthompsa		goto error;
205184610Salfred	}
206184610Salfred
207188412Sthompsa	ifp->if_softc = ue;
208188412Sthompsa	if_initname(ifp, "ue", ue->ue_unit);
209188412Sthompsa	ifp->if_mtu = ETHERMTU;
210188412Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
211188412Sthompsa	if (ue->ue_methods->ue_ioctl != NULL)
212188412Sthompsa		ifp->if_ioctl = ue->ue_methods->ue_ioctl;
213188412Sthompsa	else
214194228Sthompsa		ifp->if_ioctl = uether_ioctl;
215188412Sthompsa	ifp->if_start = ue_start;
216188412Sthompsa	ifp->if_init = ue_init;
217188412Sthompsa	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
218188412Sthompsa	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
219188412Sthompsa	IFQ_SET_READY(&ifp->if_snd);
220188412Sthompsa	ue->ue_ifp = ifp;
221184610Salfred
222188412Sthompsa	if (ue->ue_methods->ue_mii_upd != NULL &&
223188412Sthompsa	    ue->ue_methods->ue_mii_sts != NULL) {
224196403Sjhb		mtx_lock(&Giant);	/* device_xxx() depends on this */
225188412Sthompsa		error = mii_phy_probe(ue->ue_dev, &ue->ue_miibus,
226188412Sthompsa		    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts);
227196403Sjhb		mtx_unlock(&Giant);
228188412Sthompsa		if (error) {
229188412Sthompsa			device_printf(ue->ue_dev, "MII without any PHY\n");
230188412Sthompsa			goto error;
231188412Sthompsa		}
232188412Sthompsa	}
233184610Salfred
234188412Sthompsa	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
235188412Sthompsa	ether_ifattach(ifp, ue->ue_eaddr);
236188412Sthompsa
237188412Sthompsa	snprintf(num, sizeof(num), "%u", ue->ue_unit);
238188412Sthompsa	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
239188412Sthompsa	    &SYSCTL_NODE_CHILDREN(_net, ue),
240188412Sthompsa	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
241188412Sthompsa	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
242188412Sthompsa	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
243188412Sthompsa	    "%parent", CTLFLAG_RD, ue, 0,
244188412Sthompsa	    ue_sysctl_parent, "A", "parent device");
245188412Sthompsa
246188412Sthompsa	UE_LOCK(ue);
247188412Sthompsa	return;
248188412Sthompsa
249188412Sthompsaerror:
250188412Sthompsa	free_unr(ueunit, ue->ue_unit);
251188412Sthompsa	if (ue->ue_ifp != NULL) {
252188412Sthompsa		if_free(ue->ue_ifp);
253188412Sthompsa		ue->ue_ifp = NULL;
254184610Salfred	}
255188412Sthompsa	UE_LOCK(ue);
256188412Sthompsa	return;
257188412Sthompsa}
258184610Salfred
259188412Sthompsavoid
260194228Sthompsauether_ifdetach(struct usb_ether *ue)
261188412Sthompsa{
262188412Sthompsa	struct ifnet *ifp;
263184610Salfred
264188412Sthompsa	/* wait for any post attach or other command to complete */
265194228Sthompsa	usb_proc_drain(&ue->ue_tq);
266184610Salfred
267188412Sthompsa	/* read "ifnet" pointer after taskqueue drain */
268188412Sthompsa	ifp = ue->ue_ifp;
269184610Salfred
270188412Sthompsa	if (ifp != NULL) {
271184610Salfred
272188412Sthompsa		/* we are not running any more */
273188412Sthompsa		UE_LOCK(ue);
274188412Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
275188412Sthompsa		UE_UNLOCK(ue);
276184610Salfred
277188412Sthompsa		/* drain any callouts */
278194228Sthompsa		usb_callout_drain(&ue->ue_watchdog);
279188412Sthompsa
280188412Sthompsa		/* detach miibus */
281188412Sthompsa		if (ue->ue_miibus != NULL) {
282196403Sjhb			mtx_lock(&Giant);	/* device_xxx() depends on this */
283188412Sthompsa			device_delete_child(ue->ue_dev, ue->ue_miibus);
284196403Sjhb			mtx_unlock(&Giant);
285184610Salfred		}
286184610Salfred
287188412Sthompsa		/* detach ethernet */
288188412Sthompsa		ether_ifdetach(ifp);
289184610Salfred
290188412Sthompsa		/* free interface instance */
291188412Sthompsa		if_free(ifp);
292188412Sthompsa
293188412Sthompsa		/* free sysctl */
294188412Sthompsa		sysctl_ctx_free(&ue->ue_sysctl_ctx);
295188412Sthompsa
296188412Sthompsa		/* free unit */
297188412Sthompsa		free_unr(ueunit, ue->ue_unit);
298188412Sthompsa	}
299188412Sthompsa
300188412Sthompsa	/* free taskqueue, if any */
301194228Sthompsa	usb_proc_free(&ue->ue_tq);
302188412Sthompsa}
303188412Sthompsa
304188412Sthompsauint8_t
305194228Sthompsauether_is_gone(struct usb_ether *ue)
306188412Sthompsa{
307194228Sthompsa	return (usb_proc_is_gone(&ue->ue_tq));
308188412Sthompsa}
309188412Sthompsa
310188412Sthompsastatic void
311188412Sthompsaue_init(void *arg)
312188412Sthompsa{
313192984Sthompsa	struct usb_ether *ue = arg;
314188412Sthompsa
315188412Sthompsa	UE_LOCK(ue);
316188412Sthompsa	ue_queue_command(ue, ue_start_task,
317188412Sthompsa	    &ue->ue_sync_task[0].hdr,
318188412Sthompsa	    &ue->ue_sync_task[1].hdr);
319188412Sthompsa	UE_UNLOCK(ue);
320188412Sthompsa}
321188412Sthompsa
322188412Sthompsastatic void
323192984Sthompsaue_start_task(struct usb_proc_msg *_task)
324188412Sthompsa{
325192984Sthompsa	struct usb_ether_cfg_task *task =
326192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
327192984Sthompsa	struct usb_ether *ue = task->ue;
328188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
329188412Sthompsa
330188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
331188412Sthompsa
332188412Sthompsa	ue->ue_methods->ue_init(ue);
333188412Sthompsa
334188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
335188412Sthompsa		return;
336188412Sthompsa
337188412Sthompsa	if (ue->ue_methods->ue_tick != NULL)
338194228Sthompsa		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
339188412Sthompsa}
340188412Sthompsa
341188412Sthompsastatic void
342192984Sthompsaue_stop_task(struct usb_proc_msg *_task)
343188412Sthompsa{
344192984Sthompsa	struct usb_ether_cfg_task *task =
345192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
346192984Sthompsa	struct usb_ether *ue = task->ue;
347188412Sthompsa
348188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
349188412Sthompsa
350194228Sthompsa	usb_callout_stop(&ue->ue_watchdog);
351188412Sthompsa
352188412Sthompsa	ue->ue_methods->ue_stop(ue);
353188412Sthompsa}
354188412Sthompsa
355188412Sthompsastatic void
356188412Sthompsaue_start(struct ifnet *ifp)
357188412Sthompsa{
358192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
359188412Sthompsa
360188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
361188412Sthompsa		return;
362188412Sthompsa
363188412Sthompsa	UE_LOCK(ue);
364188412Sthompsa	ue->ue_methods->ue_start(ue);
365188412Sthompsa	UE_UNLOCK(ue);
366188412Sthompsa}
367188412Sthompsa
368188412Sthompsastatic void
369192984Sthompsaue_promisc_task(struct usb_proc_msg *_task)
370188412Sthompsa{
371192984Sthompsa	struct usb_ether_cfg_task *task =
372192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
373192984Sthompsa	struct usb_ether *ue = task->ue;
374188412Sthompsa
375188412Sthompsa	ue->ue_methods->ue_setpromisc(ue);
376188412Sthompsa}
377188412Sthompsa
378188412Sthompsastatic void
379192984Sthompsaue_setmulti_task(struct usb_proc_msg *_task)
380188412Sthompsa{
381192984Sthompsa	struct usb_ether_cfg_task *task =
382192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
383192984Sthompsa	struct usb_ether *ue = task->ue;
384188412Sthompsa
385188412Sthompsa	ue->ue_methods->ue_setmulti(ue);
386188412Sthompsa}
387188412Sthompsa
388188412Sthompsastatic int
389188412Sthompsaue_ifmedia_upd(struct ifnet *ifp)
390188412Sthompsa{
391192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
392188412Sthompsa
393188412Sthompsa	/* Defer to process context */
394188412Sthompsa	UE_LOCK(ue);
395188412Sthompsa	ue_queue_command(ue, ue_ifmedia_task,
396188412Sthompsa	    &ue->ue_media_task[0].hdr,
397188412Sthompsa	    &ue->ue_media_task[1].hdr);
398188412Sthompsa	UE_UNLOCK(ue);
399188412Sthompsa
400188412Sthompsa	return (0);
401188412Sthompsa}
402188412Sthompsa
403188412Sthompsastatic void
404192984Sthompsaue_ifmedia_task(struct usb_proc_msg *_task)
405188412Sthompsa{
406192984Sthompsa	struct usb_ether_cfg_task *task =
407192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
408192984Sthompsa	struct usb_ether *ue = task->ue;
409188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
410188412Sthompsa
411188412Sthompsa	ue->ue_methods->ue_mii_upd(ifp);
412188412Sthompsa}
413188412Sthompsa
414188412Sthompsastatic void
415188412Sthompsaue_watchdog(void *arg)
416188412Sthompsa{
417192984Sthompsa	struct usb_ether *ue = arg;
418188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
419188412Sthompsa
420188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
421188412Sthompsa		return;
422188412Sthompsa
423188412Sthompsa	ue_queue_command(ue, ue_tick_task,
424188412Sthompsa	    &ue->ue_tick_task[0].hdr,
425188412Sthompsa	    &ue->ue_tick_task[1].hdr);
426188412Sthompsa
427194228Sthompsa	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
428188412Sthompsa}
429188412Sthompsa
430188412Sthompsastatic void
431192984Sthompsaue_tick_task(struct usb_proc_msg *_task)
432188412Sthompsa{
433192984Sthompsa	struct usb_ether_cfg_task *task =
434192984Sthompsa	    (struct usb_ether_cfg_task *)_task;
435192984Sthompsa	struct usb_ether *ue = task->ue;
436188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
437188412Sthompsa
438188412Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
439188412Sthompsa		return;
440188412Sthompsa
441188412Sthompsa	ue->ue_methods->ue_tick(ue);
442188412Sthompsa}
443188412Sthompsa
444188412Sthompsaint
445194228Sthompsauether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
446188412Sthompsa{
447192984Sthompsa	struct usb_ether *ue = ifp->if_softc;
448188412Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
449188412Sthompsa	struct mii_data *mii;
450188412Sthompsa	int error = 0;
451188412Sthompsa
452188412Sthompsa	switch (command) {
453188412Sthompsa	case SIOCSIFFLAGS:
454188412Sthompsa		UE_LOCK(ue);
455188412Sthompsa		if (ifp->if_flags & IFF_UP) {
456188412Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
457188412Sthompsa				ue_queue_command(ue, ue_promisc_task,
458188412Sthompsa				    &ue->ue_promisc_task[0].hdr,
459188412Sthompsa				    &ue->ue_promisc_task[1].hdr);
460188412Sthompsa			else
461188412Sthompsa				ue_queue_command(ue, ue_start_task,
462188412Sthompsa				    &ue->ue_sync_task[0].hdr,
463188412Sthompsa				    &ue->ue_sync_task[1].hdr);
464188412Sthompsa		} else {
465188412Sthompsa			ue_queue_command(ue, ue_stop_task,
466188412Sthompsa			    &ue->ue_sync_task[0].hdr,
467188412Sthompsa			    &ue->ue_sync_task[1].hdr);
468184610Salfred		}
469188412Sthompsa		UE_UNLOCK(ue);
470188412Sthompsa		break;
471188412Sthompsa	case SIOCADDMULTI:
472188412Sthompsa	case SIOCDELMULTI:
473188412Sthompsa		UE_LOCK(ue);
474188412Sthompsa		ue_queue_command(ue, ue_setmulti_task,
475188412Sthompsa		    &ue->ue_multi_task[0].hdr,
476188412Sthompsa		    &ue->ue_multi_task[1].hdr);
477188412Sthompsa		UE_UNLOCK(ue);
478188412Sthompsa		break;
479188412Sthompsa	case SIOCGIFMEDIA:
480188412Sthompsa	case SIOCSIFMEDIA:
481188412Sthompsa		if (ue->ue_miibus != NULL) {
482188412Sthompsa			mii = device_get_softc(ue->ue_miibus);
483188412Sthompsa			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
484188412Sthompsa		} else
485188412Sthompsa			error = ether_ioctl(ifp, command, data);
486188412Sthompsa		break;
487188412Sthompsa	default:
488188412Sthompsa		error = ether_ioctl(ifp, command, data);
489188412Sthompsa		break;
490184610Salfred	}
491188412Sthompsa	return (error);
492184610Salfred}
493188412Sthompsa
494188412Sthompsastatic int
495194228Sthompsauether_modevent(module_t mod, int type, void *data)
496188412Sthompsa{
497188412Sthompsa
498188412Sthompsa	switch (type) {
499188412Sthompsa	case MOD_LOAD:
500188412Sthompsa		ueunit = new_unrhdr(0, INT_MAX, NULL);
501188412Sthompsa		break;
502188412Sthompsa	case MOD_UNLOAD:
503188412Sthompsa		break;
504188412Sthompsa	default:
505188412Sthompsa		return (EOPNOTSUPP);
506188412Sthompsa	}
507188412Sthompsa	return (0);
508188412Sthompsa}
509194228Sthompsastatic moduledata_t uether_mod = {
510188942Sthompsa	"uether",
511194228Sthompsa	uether_modevent,
512188412Sthompsa	0
513188412Sthompsa};
514188412Sthompsa
515189528Sthompsastruct mbuf *
516194228Sthompsauether_newbuf(void)
517189528Sthompsa{
518189528Sthompsa	struct mbuf *m_new;
519189528Sthompsa
520189528Sthompsa	m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
521189528Sthompsa	if (m_new == NULL)
522189528Sthompsa		return (NULL);
523189528Sthompsa	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
524189528Sthompsa
525189528Sthompsa	m_adj(m_new, ETHER_ALIGN);
526189528Sthompsa	return (m_new);
527189528Sthompsa}
528189528Sthompsa
529188412Sthompsaint
530194228Sthompsauether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
531188412Sthompsa    unsigned int len)
532188412Sthompsa{
533188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
534188412Sthompsa
535188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
536188412Sthompsa
537188412Sthompsa	/* finalize mbuf */
538188412Sthompsa	ifp->if_ipackets++;
539188412Sthompsa	m->m_pkthdr.rcvif = ifp;
540188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
541188412Sthompsa
542188412Sthompsa	/* enqueue for later when the lock can be released */
543188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
544188412Sthompsa	return (0);
545188412Sthompsa}
546188412Sthompsa
547188412Sthompsaint
548194228Sthompsauether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
549188412Sthompsa    unsigned int offset, unsigned int len)
550188412Sthompsa{
551188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
552188412Sthompsa	struct mbuf *m;
553188412Sthompsa
554188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
555188412Sthompsa
556189528Sthompsa	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
557188412Sthompsa		return (1);
558188412Sthompsa
559194228Sthompsa	m = uether_newbuf();
560188412Sthompsa	if (m == NULL) {
561188412Sthompsa		ifp->if_ierrors++;
562188412Sthompsa		return (ENOMEM);
563188412Sthompsa	}
564188412Sthompsa
565194228Sthompsa	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
566188412Sthompsa
567188412Sthompsa	/* finalize mbuf */
568188412Sthompsa	ifp->if_ipackets++;
569188412Sthompsa	m->m_pkthdr.rcvif = ifp;
570188412Sthompsa	m->m_pkthdr.len = m->m_len = len;
571188412Sthompsa
572188412Sthompsa	/* enqueue for later when the lock can be released */
573188412Sthompsa	_IF_ENQUEUE(&ue->ue_rxq, m);
574188412Sthompsa	return (0);
575188412Sthompsa}
576188412Sthompsa
577188412Sthompsavoid
578194228Sthompsauether_rxflush(struct usb_ether *ue)
579188412Sthompsa{
580188412Sthompsa	struct ifnet *ifp = ue->ue_ifp;
581188412Sthompsa	struct mbuf *m;
582188412Sthompsa
583188412Sthompsa	UE_LOCK_ASSERT(ue, MA_OWNED);
584188412Sthompsa
585188412Sthompsa	for (;;) {
586188412Sthompsa		_IF_DEQUEUE(&ue->ue_rxq, m);
587188412Sthompsa		if (m == NULL)
588188412Sthompsa			break;
589188412Sthompsa
590188412Sthompsa		/*
591188412Sthompsa		 * The USB xfer has been resubmitted so its safe to unlock now.
592188412Sthompsa		 */
593188412Sthompsa		UE_UNLOCK(ue);
594188412Sthompsa		ifp->if_input(ifp, m);
595188412Sthompsa		UE_LOCK(ue);
596188412Sthompsa	}
597188412Sthompsa}
598188412Sthompsa
599194228SthompsaDECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
600188942SthompsaMODULE_VERSION(uether, 1);
601