3 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usb_process.h>
51 #include <dev/usb/net/usb_ethernet.h>
53 SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, "USB Ethernet parameters");
55 #define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx)
56 #define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx)
57 #define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t)
59 MODULE_DEPEND(uether, usb, 1, 1, 1);
60 MODULE_DEPEND(uether, miibus, 1, 1, 1);
62 static struct unrhdr *ueunit;
64 static usb_proc_callback_t ue_attach_post_task;
65 static usb_proc_callback_t ue_promisc_task;
66 static usb_proc_callback_t ue_setmulti_task;
67 static usb_proc_callback_t ue_ifmedia_task;
68 static usb_proc_callback_t ue_tick_task;
69 static usb_proc_callback_t ue_start_task;
70 static usb_proc_callback_t ue_stop_task;
72 static void ue_init(void *);
73 static void ue_start(struct ifnet *);
74 static int ue_ifmedia_upd(struct ifnet *);
75 static void ue_watchdog(void *);
80 * Else: device has been detached
83 uether_pause(struct usb_ether *ue, unsigned int _ticks)
85 if (usb_proc_is_gone(&ue->ue_tq)) {
89 usb_pause_mtx(ue->ue_mtx, _ticks);
94 ue_queue_command(struct usb_ether *ue,
95 usb_proc_callback_t *fn,
96 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
98 struct usb_ether_cfg_task *task;
100 UE_LOCK_ASSERT(ue, MA_OWNED);
102 if (usb_proc_is_gone(&ue->ue_tq)) {
103 return; /* nothing to do */
106 * NOTE: The task cannot get executed before we drop the
107 * "sc_mtx" mutex. It is safe to update fields in the message
108 * structure after that the message got queued.
110 task = (struct usb_ether_cfg_task *)
111 usb_proc_msignal(&ue->ue_tq, t0, t1);
113 /* Setup callback and self pointers */
114 task->hdr.pm_callback = fn;
118 * Start and stop must be synchronous!
120 if ((fn == ue_start_task) || (fn == ue_stop_task))
121 usb_proc_mwait(&ue->ue_tq, t0, t1);
125 uether_getifp(struct usb_ether *ue)
131 uether_getmii(struct usb_ether *ue)
133 return (device_get_softc(ue->ue_miibus));
137 uether_getsc(struct usb_ether *ue)
143 ue_sysctl_parent(SYSCTL_HANDLER_ARGS)
145 struct usb_ether *ue = arg1;
148 name = device_get_nameunit(ue->ue_dev);
149 return SYSCTL_OUT(req, name, strlen(name));
153 uether_ifattach(struct usb_ether *ue)
157 /* check some critical parameters */
158 if ((ue->ue_dev == NULL) ||
159 (ue->ue_udev == NULL) ||
160 (ue->ue_mtx == NULL) ||
161 (ue->ue_methods == NULL))
164 error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
165 device_get_nameunit(ue->ue_dev), USB_PRI_MED);
167 device_printf(ue->ue_dev, "could not setup taskqueue\n");
171 /* fork rest of the attach code */
173 ue_queue_command(ue, ue_attach_post_task,
174 &ue->ue_sync_task[0].hdr,
175 &ue->ue_sync_task[1].hdr);
183 ue_attach_post_task(struct usb_proc_msg *_task)
185 struct usb_ether_cfg_task *task =
186 (struct usb_ether_cfg_task *)_task;
187 struct usb_ether *ue = task->ue;
190 char num[14]; /* sufficient for 32 bits */
192 /* first call driver's post attach routine */
193 ue->ue_methods->ue_attach_post(ue);
197 ue->ue_unit = alloc_unr(ueunit);
198 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
199 sysctl_ctx_init(&ue->ue_sysctl_ctx);
201 ifp = if_alloc(IFT_ETHER);
203 device_printf(ue->ue_dev, "could not allocate ifnet\n");
208 if_initname(ifp, "ue", ue->ue_unit);
209 ifp->if_mtu = ETHERMTU;
210 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
211 if (ue->ue_methods->ue_ioctl != NULL)
212 ifp->if_ioctl = ue->ue_methods->ue_ioctl;
214 ifp->if_ioctl = uether_ioctl;
215 ifp->if_start = ue_start;
216 ifp->if_init = ue_init;
217 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
218 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
219 IFQ_SET_READY(&ifp->if_snd);
222 if (ue->ue_methods->ue_mii_upd != NULL &&
223 ue->ue_methods->ue_mii_sts != NULL) {
224 mtx_lock(&Giant); /* device_xxx() depends on this */
225 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
226 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
227 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
230 device_printf(ue->ue_dev, "attaching PHYs failed\n");
235 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
236 ether_ifattach(ifp, ue->ue_eaddr);
238 snprintf(num, sizeof(num), "%u", ue->ue_unit);
239 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
240 &SYSCTL_NODE_CHILDREN(_net, ue),
241 OID_AUTO, num, CTLFLAG_RD, NULL, "");
242 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
243 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
244 "%parent", CTLFLAG_RD, ue, 0,
245 ue_sysctl_parent, "A", "parent device");
251 free_unr(ueunit, ue->ue_unit);
252 if (ue->ue_ifp != NULL) {
261 uether_ifdetach(struct usb_ether *ue)
265 /* wait for any post attach or other command to complete */
266 usb_proc_drain(&ue->ue_tq);
268 /* read "ifnet" pointer after taskqueue drain */
273 /* we are not running any more */
275 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
278 /* drain any callouts */
279 usb_callout_drain(&ue->ue_watchdog);
282 if (ue->ue_miibus != NULL) {
283 mtx_lock(&Giant); /* device_xxx() depends on this */
284 device_delete_child(ue->ue_dev, ue->ue_miibus);
288 /* detach ethernet */
291 /* free interface instance */
295 sysctl_ctx_free(&ue->ue_sysctl_ctx);
298 free_unr(ueunit, ue->ue_unit);
301 /* free taskqueue, if any */
302 usb_proc_free(&ue->ue_tq);
306 uether_is_gone(struct usb_ether *ue)
308 return (usb_proc_is_gone(&ue->ue_tq));
314 struct usb_ether *ue = arg;
317 ue_queue_command(ue, ue_start_task,
318 &ue->ue_sync_task[0].hdr,
319 &ue->ue_sync_task[1].hdr);
324 ue_start_task(struct usb_proc_msg *_task)
326 struct usb_ether_cfg_task *task =
327 (struct usb_ether_cfg_task *)_task;
328 struct usb_ether *ue = task->ue;
329 struct ifnet *ifp = ue->ue_ifp;
331 UE_LOCK_ASSERT(ue, MA_OWNED);
333 ue->ue_methods->ue_init(ue);
335 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
338 if (ue->ue_methods->ue_tick != NULL)
339 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
343 ue_stop_task(struct usb_proc_msg *_task)
345 struct usb_ether_cfg_task *task =
346 (struct usb_ether_cfg_task *)_task;
347 struct usb_ether *ue = task->ue;
349 UE_LOCK_ASSERT(ue, MA_OWNED);
351 usb_callout_stop(&ue->ue_watchdog);
353 ue->ue_methods->ue_stop(ue);
357 ue_start(struct ifnet *ifp)
359 struct usb_ether *ue = ifp->if_softc;
361 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
365 ue->ue_methods->ue_start(ue);
370 ue_promisc_task(struct usb_proc_msg *_task)
372 struct usb_ether_cfg_task *task =
373 (struct usb_ether_cfg_task *)_task;
374 struct usb_ether *ue = task->ue;
376 ue->ue_methods->ue_setpromisc(ue);
380 ue_setmulti_task(struct usb_proc_msg *_task)
382 struct usb_ether_cfg_task *task =
383 (struct usb_ether_cfg_task *)_task;
384 struct usb_ether *ue = task->ue;
386 ue->ue_methods->ue_setmulti(ue);
390 ue_ifmedia_upd(struct ifnet *ifp)
392 struct usb_ether *ue = ifp->if_softc;
394 /* Defer to process context */
396 ue_queue_command(ue, ue_ifmedia_task,
397 &ue->ue_media_task[0].hdr,
398 &ue->ue_media_task[1].hdr);
405 ue_ifmedia_task(struct usb_proc_msg *_task)
407 struct usb_ether_cfg_task *task =
408 (struct usb_ether_cfg_task *)_task;
409 struct usb_ether *ue = task->ue;
410 struct ifnet *ifp = ue->ue_ifp;
412 ue->ue_methods->ue_mii_upd(ifp);
416 ue_watchdog(void *arg)
418 struct usb_ether *ue = arg;
419 struct ifnet *ifp = ue->ue_ifp;
421 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
424 ue_queue_command(ue, ue_tick_task,
425 &ue->ue_tick_task[0].hdr,
426 &ue->ue_tick_task[1].hdr);
428 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
432 ue_tick_task(struct usb_proc_msg *_task)
434 struct usb_ether_cfg_task *task =
435 (struct usb_ether_cfg_task *)_task;
436 struct usb_ether *ue = task->ue;
437 struct ifnet *ifp = ue->ue_ifp;
439 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
442 ue->ue_methods->ue_tick(ue);
446 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
448 struct usb_ether *ue = ifp->if_softc;
449 struct ifreq *ifr = (struct ifreq *)data;
450 struct mii_data *mii;
456 if (ifp->if_flags & IFF_UP) {
457 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
458 ue_queue_command(ue, ue_promisc_task,
459 &ue->ue_promisc_task[0].hdr,
460 &ue->ue_promisc_task[1].hdr);
462 ue_queue_command(ue, ue_start_task,
463 &ue->ue_sync_task[0].hdr,
464 &ue->ue_sync_task[1].hdr);
466 ue_queue_command(ue, ue_stop_task,
467 &ue->ue_sync_task[0].hdr,
468 &ue->ue_sync_task[1].hdr);
475 ue_queue_command(ue, ue_setmulti_task,
476 &ue->ue_multi_task[0].hdr,
477 &ue->ue_multi_task[1].hdr);
482 if (ue->ue_miibus != NULL) {
483 mii = device_get_softc(ue->ue_miibus);
484 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
486 error = ether_ioctl(ifp, command, data);
489 error = ether_ioctl(ifp, command, data);
496 uether_modevent(module_t mod, int type, void *data)
501 ueunit = new_unrhdr(0, INT_MAX, NULL);
510 static moduledata_t uether_mod = {
521 m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
524 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
526 m_adj(m_new, ETHER_ALIGN);
531 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
534 struct ifnet *ifp = ue->ue_ifp;
536 UE_LOCK_ASSERT(ue, MA_OWNED);
540 m->m_pkthdr.rcvif = ifp;
541 m->m_pkthdr.len = m->m_len = len;
543 /* enqueue for later when the lock can be released */
544 _IF_ENQUEUE(&ue->ue_rxq, m);
549 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
550 unsigned int offset, unsigned int len)
552 struct ifnet *ifp = ue->ue_ifp;
555 UE_LOCK_ASSERT(ue, MA_OWNED);
557 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
566 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
570 m->m_pkthdr.rcvif = ifp;
571 m->m_pkthdr.len = m->m_len = len;
573 /* enqueue for later when the lock can be released */
574 _IF_ENQUEUE(&ue->ue_rxq, m);
579 uether_rxflush(struct usb_ether *ue)
581 struct ifnet *ifp = ue->ue_ifp;
584 UE_LOCK_ASSERT(ue, MA_OWNED);
587 _IF_DEQUEUE(&ue->ue_rxq, m);
592 * The USB xfer has been resubmitted so its safe to unlock now.
595 ifp->if_input(ifp, m);
600 DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
601 MODULE_VERSION(uether, 1);