1 /* $FreeBSD: stable/9/sys/dev/usb/net/usb_ethernet.c 262594 2014-02-28 01:35:24Z rodrigc $ */
2 /*-
3 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: stable/9/sys/dev/usb/net/usb_ethernet.c 262594 2014-02-28 01:35:24Z rodrigc $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/condvar.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/sx.h>
44
45 #include <net/if.h>
46 #include <net/ethernet.h>
47 #include <net/if_types.h>
48 #include <net/if_media.h>
49 #include <net/if_vlan_var.h>
50
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/net/usb_ethernet.h>
59
60 static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0,
61 "USB Ethernet parameters");
62
63 #define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx)
64 #define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx)
65 #define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t)
66
67 MODULE_DEPEND(uether, usb, 1, 1, 1);
68 MODULE_DEPEND(uether, miibus, 1, 1, 1);
69
70 static struct unrhdr *ueunit;
71
72 static usb_proc_callback_t ue_attach_post_task;
73 static usb_proc_callback_t ue_promisc_task;
74 static usb_proc_callback_t ue_setmulti_task;
75 static usb_proc_callback_t ue_ifmedia_task;
76 static usb_proc_callback_t ue_tick_task;
77 static usb_proc_callback_t ue_start_task;
78 static usb_proc_callback_t ue_stop_task;
79
80 static void ue_init(void *);
81 static void ue_start(struct ifnet *);
82 static int ue_ifmedia_upd(struct ifnet *);
83 static void ue_watchdog(void *);
84
85 /*
86 * Return values:
87 * 0: success
88 * Else: device has been detached
89 */
90 uint8_t
uether_pause(struct usb_ether * ue,unsigned int _ticks)91 uether_pause(struct usb_ether *ue, unsigned int _ticks)
92 {
93 if (usb_proc_is_gone(&ue->ue_tq)) {
94 /* nothing to do */
95 return (1);
96 }
97 usb_pause_mtx(ue->ue_mtx, _ticks);
98 return (0);
99 }
100
101 static void
ue_queue_command(struct usb_ether * ue,usb_proc_callback_t * fn,struct usb_proc_msg * t0,struct usb_proc_msg * t1)102 ue_queue_command(struct usb_ether *ue,
103 usb_proc_callback_t *fn,
104 struct usb_proc_msg *t0, struct usb_proc_msg *t1)
105 {
106 struct usb_ether_cfg_task *task;
107
108 UE_LOCK_ASSERT(ue, MA_OWNED);
109
110 if (usb_proc_is_gone(&ue->ue_tq)) {
111 return; /* nothing to do */
112 }
113 /*
114 * NOTE: The task cannot get executed before we drop the
115 * "sc_mtx" mutex. It is safe to update fields in the message
116 * structure after that the message got queued.
117 */
118 task = (struct usb_ether_cfg_task *)
119 usb_proc_msignal(&ue->ue_tq, t0, t1);
120
121 /* Setup callback and self pointers */
122 task->hdr.pm_callback = fn;
123 task->ue = ue;
124
125 /*
126 * Start and stop must be synchronous!
127 */
128 if ((fn == ue_start_task) || (fn == ue_stop_task))
129 usb_proc_mwait(&ue->ue_tq, t0, t1);
130 }
131
132 struct ifnet *
uether_getifp(struct usb_ether * ue)133 uether_getifp(struct usb_ether *ue)
134 {
135 return (ue->ue_ifp);
136 }
137
138 struct mii_data *
uether_getmii(struct usb_ether * ue)139 uether_getmii(struct usb_ether *ue)
140 {
141 return (device_get_softc(ue->ue_miibus));
142 }
143
144 void *
uether_getsc(struct usb_ether * ue)145 uether_getsc(struct usb_ether *ue)
146 {
147 return (ue->ue_sc);
148 }
149
150 static int
ue_sysctl_parent(SYSCTL_HANDLER_ARGS)151 ue_sysctl_parent(SYSCTL_HANDLER_ARGS)
152 {
153 struct usb_ether *ue = arg1;
154 const char *name;
155
156 name = device_get_nameunit(ue->ue_dev);
157 return SYSCTL_OUT(req, name, strlen(name));
158 }
159
160 int
uether_ifattach(struct usb_ether * ue)161 uether_ifattach(struct usb_ether *ue)
162 {
163 int error;
164
165 /* check some critical parameters */
166 if ((ue->ue_dev == NULL) ||
167 (ue->ue_udev == NULL) ||
168 (ue->ue_mtx == NULL) ||
169 (ue->ue_methods == NULL))
170 return (EINVAL);
171
172 error = usb_proc_create(&ue->ue_tq, ue->ue_mtx,
173 device_get_nameunit(ue->ue_dev), USB_PRI_MED);
174 if (error) {
175 device_printf(ue->ue_dev, "could not setup taskqueue\n");
176 goto error;
177 }
178
179 /* fork rest of the attach code */
180 UE_LOCK(ue);
181 ue_queue_command(ue, ue_attach_post_task,
182 &ue->ue_sync_task[0].hdr,
183 &ue->ue_sync_task[1].hdr);
184 UE_UNLOCK(ue);
185
186 error:
187 return (error);
188 }
189
190 static void
ue_attach_post_task(struct usb_proc_msg * _task)191 ue_attach_post_task(struct usb_proc_msg *_task)
192 {
193 struct usb_ether_cfg_task *task =
194 (struct usb_ether_cfg_task *)_task;
195 struct usb_ether *ue = task->ue;
196 struct ifnet *ifp;
197 int error;
198 char num[14]; /* sufficient for 32 bits */
199
200 /* first call driver's post attach routine */
201 ue->ue_methods->ue_attach_post(ue);
202
203 UE_UNLOCK(ue);
204
205 ue->ue_unit = alloc_unr(ueunit);
206 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
207 sysctl_ctx_init(&ue->ue_sysctl_ctx);
208
209 error = 0;
210 CURVNET_SET_QUIET(vnet0);
211 ifp = if_alloc(IFT_ETHER);
212 if (ifp == NULL) {
213 device_printf(ue->ue_dev, "could not allocate ifnet\n");
214 goto fail;
215 }
216
217 ifp->if_softc = ue;
218 if_initname(ifp, "ue", ue->ue_unit);
219 if (ue->ue_methods->ue_attach_post_sub != NULL) {
220 ue->ue_ifp = ifp;
221 error = ue->ue_methods->ue_attach_post_sub(ue);
222 } else {
223 ifp->if_mtu = ETHERMTU;
224 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
225 if (ue->ue_methods->ue_ioctl != NULL)
226 ifp->if_ioctl = ue->ue_methods->ue_ioctl;
227 else
228 ifp->if_ioctl = uether_ioctl;
229 ifp->if_start = ue_start;
230 ifp->if_init = ue_init;
231 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
232 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
233 IFQ_SET_READY(&ifp->if_snd);
234 ue->ue_ifp = ifp;
235
236 if (ue->ue_methods->ue_mii_upd != NULL &&
237 ue->ue_methods->ue_mii_sts != NULL) {
238 /* device_xxx() depends on this */
239 mtx_lock(&Giant);
240 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
241 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
242 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
243 mtx_unlock(&Giant);
244 }
245 }
246
247 if (error) {
248 device_printf(ue->ue_dev, "attaching PHYs failed\n");
249 goto fail;
250 }
251
252 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
253 ether_ifattach(ifp, ue->ue_eaddr);
254 /* Tell upper layer we support VLAN oversized frames. */
255 if (ifp->if_capabilities & IFCAP_VLAN_MTU)
256 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
257
258 CURVNET_RESTORE();
259
260 snprintf(num, sizeof(num), "%u", ue->ue_unit);
261 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
262 &SYSCTL_NODE_CHILDREN(_net, ue),
263 OID_AUTO, num, CTLFLAG_RD, NULL, "");
264 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
265 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
266 "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
267 ue_sysctl_parent, "A", "parent device");
268
269 UE_LOCK(ue);
270 return;
271
272 fail:
273 CURVNET_RESTORE();
274 free_unr(ueunit, ue->ue_unit);
275 if (ue->ue_ifp != NULL) {
276 if_free(ue->ue_ifp);
277 ue->ue_ifp = NULL;
278 }
279 UE_LOCK(ue);
280 return;
281 }
282
283 void
uether_ifdetach(struct usb_ether * ue)284 uether_ifdetach(struct usb_ether *ue)
285 {
286 struct ifnet *ifp;
287
288 /* wait for any post attach or other command to complete */
289 usb_proc_drain(&ue->ue_tq);
290
291 /* read "ifnet" pointer after taskqueue drain */
292 ifp = ue->ue_ifp;
293
294 if (ifp != NULL) {
295
296 /* we are not running any more */
297 UE_LOCK(ue);
298 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
299 UE_UNLOCK(ue);
300
301 /* drain any callouts */
302 usb_callout_drain(&ue->ue_watchdog);
303
304 /* detach miibus */
305 if (ue->ue_miibus != NULL) {
306 mtx_lock(&Giant); /* device_xxx() depends on this */
307 device_delete_child(ue->ue_dev, ue->ue_miibus);
308 mtx_unlock(&Giant);
309 }
310
311 /* detach ethernet */
312 ether_ifdetach(ifp);
313
314 /* free interface instance */
315 if_free(ifp);
316
317 /* free sysctl */
318 sysctl_ctx_free(&ue->ue_sysctl_ctx);
319
320 /* free unit */
321 free_unr(ueunit, ue->ue_unit);
322 }
323
324 /* free taskqueue, if any */
325 usb_proc_free(&ue->ue_tq);
326 }
327
328 uint8_t
uether_is_gone(struct usb_ether * ue)329 uether_is_gone(struct usb_ether *ue)
330 {
331 return (usb_proc_is_gone(&ue->ue_tq));
332 }
333
334 void
uether_init(void * arg)335 uether_init(void *arg)
336 {
337
338 ue_init(arg);
339 }
340
341 static void
ue_init(void * arg)342 ue_init(void *arg)
343 {
344 struct usb_ether *ue = arg;
345
346 UE_LOCK(ue);
347 ue_queue_command(ue, ue_start_task,
348 &ue->ue_sync_task[0].hdr,
349 &ue->ue_sync_task[1].hdr);
350 UE_UNLOCK(ue);
351 }
352
353 static void
ue_start_task(struct usb_proc_msg * _task)354 ue_start_task(struct usb_proc_msg *_task)
355 {
356 struct usb_ether_cfg_task *task =
357 (struct usb_ether_cfg_task *)_task;
358 struct usb_ether *ue = task->ue;
359 struct ifnet *ifp = ue->ue_ifp;
360
361 UE_LOCK_ASSERT(ue, MA_OWNED);
362
363 ue->ue_methods->ue_init(ue);
364
365 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
366 return;
367
368 if (ue->ue_methods->ue_tick != NULL)
369 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
370 }
371
372 static void
ue_stop_task(struct usb_proc_msg * _task)373 ue_stop_task(struct usb_proc_msg *_task)
374 {
375 struct usb_ether_cfg_task *task =
376 (struct usb_ether_cfg_task *)_task;
377 struct usb_ether *ue = task->ue;
378
379 UE_LOCK_ASSERT(ue, MA_OWNED);
380
381 usb_callout_stop(&ue->ue_watchdog);
382
383 ue->ue_methods->ue_stop(ue);
384 }
385
386 void
uether_start(struct ifnet * ifp)387 uether_start(struct ifnet *ifp)
388 {
389
390 ue_start(ifp);
391 }
392
393 static void
ue_start(struct ifnet * ifp)394 ue_start(struct ifnet *ifp)
395 {
396 struct usb_ether *ue = ifp->if_softc;
397
398 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
399 return;
400
401 UE_LOCK(ue);
402 ue->ue_methods->ue_start(ue);
403 UE_UNLOCK(ue);
404 }
405
406 static void
ue_promisc_task(struct usb_proc_msg * _task)407 ue_promisc_task(struct usb_proc_msg *_task)
408 {
409 struct usb_ether_cfg_task *task =
410 (struct usb_ether_cfg_task *)_task;
411 struct usb_ether *ue = task->ue;
412
413 ue->ue_methods->ue_setpromisc(ue);
414 }
415
416 static void
ue_setmulti_task(struct usb_proc_msg * _task)417 ue_setmulti_task(struct usb_proc_msg *_task)
418 {
419 struct usb_ether_cfg_task *task =
420 (struct usb_ether_cfg_task *)_task;
421 struct usb_ether *ue = task->ue;
422
423 ue->ue_methods->ue_setmulti(ue);
424 }
425
426 int
uether_ifmedia_upd(struct ifnet * ifp)427 uether_ifmedia_upd(struct ifnet *ifp)
428 {
429
430 return (ue_ifmedia_upd(ifp));
431 }
432
433 static int
ue_ifmedia_upd(struct ifnet * ifp)434 ue_ifmedia_upd(struct ifnet *ifp)
435 {
436 struct usb_ether *ue = ifp->if_softc;
437
438 /* Defer to process context */
439 UE_LOCK(ue);
440 ue_queue_command(ue, ue_ifmedia_task,
441 &ue->ue_media_task[0].hdr,
442 &ue->ue_media_task[1].hdr);
443 UE_UNLOCK(ue);
444
445 return (0);
446 }
447
448 static void
ue_ifmedia_task(struct usb_proc_msg * _task)449 ue_ifmedia_task(struct usb_proc_msg *_task)
450 {
451 struct usb_ether_cfg_task *task =
452 (struct usb_ether_cfg_task *)_task;
453 struct usb_ether *ue = task->ue;
454 struct ifnet *ifp = ue->ue_ifp;
455
456 ue->ue_methods->ue_mii_upd(ifp);
457 }
458
459 static void
ue_watchdog(void * arg)460 ue_watchdog(void *arg)
461 {
462 struct usb_ether *ue = arg;
463 struct ifnet *ifp = ue->ue_ifp;
464
465 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
466 return;
467
468 ue_queue_command(ue, ue_tick_task,
469 &ue->ue_tick_task[0].hdr,
470 &ue->ue_tick_task[1].hdr);
471
472 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
473 }
474
475 static void
ue_tick_task(struct usb_proc_msg * _task)476 ue_tick_task(struct usb_proc_msg *_task)
477 {
478 struct usb_ether_cfg_task *task =
479 (struct usb_ether_cfg_task *)_task;
480 struct usb_ether *ue = task->ue;
481 struct ifnet *ifp = ue->ue_ifp;
482
483 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
484 return;
485
486 ue->ue_methods->ue_tick(ue);
487 }
488
489 int
uether_ioctl(struct ifnet * ifp,u_long command,caddr_t data)490 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
491 {
492 struct usb_ether *ue = ifp->if_softc;
493 struct ifreq *ifr = (struct ifreq *)data;
494 struct mii_data *mii;
495 int error = 0;
496
497 switch (command) {
498 case SIOCSIFFLAGS:
499 UE_LOCK(ue);
500 if (ifp->if_flags & IFF_UP) {
501 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
502 ue_queue_command(ue, ue_promisc_task,
503 &ue->ue_promisc_task[0].hdr,
504 &ue->ue_promisc_task[1].hdr);
505 else
506 ue_queue_command(ue, ue_start_task,
507 &ue->ue_sync_task[0].hdr,
508 &ue->ue_sync_task[1].hdr);
509 } else {
510 ue_queue_command(ue, ue_stop_task,
511 &ue->ue_sync_task[0].hdr,
512 &ue->ue_sync_task[1].hdr);
513 }
514 UE_UNLOCK(ue);
515 break;
516 case SIOCADDMULTI:
517 case SIOCDELMULTI:
518 UE_LOCK(ue);
519 ue_queue_command(ue, ue_setmulti_task,
520 &ue->ue_multi_task[0].hdr,
521 &ue->ue_multi_task[1].hdr);
522 UE_UNLOCK(ue);
523 break;
524 case SIOCGIFMEDIA:
525 case SIOCSIFMEDIA:
526 if (ue->ue_miibus != NULL) {
527 mii = device_get_softc(ue->ue_miibus);
528 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
529 } else
530 error = ether_ioctl(ifp, command, data);
531 break;
532 default:
533 error = ether_ioctl(ifp, command, data);
534 break;
535 }
536 return (error);
537 }
538
539 static int
uether_modevent(module_t mod,int type,void * data)540 uether_modevent(module_t mod, int type, void *data)
541 {
542
543 switch (type) {
544 case MOD_LOAD:
545 ueunit = new_unrhdr(0, INT_MAX, NULL);
546 break;
547 case MOD_UNLOAD:
548 break;
549 default:
550 return (EOPNOTSUPP);
551 }
552 return (0);
553 }
554 static moduledata_t uether_mod = {
555 "uether",
556 uether_modevent,
557 0
558 };
559
560 struct mbuf *
uether_newbuf(void)561 uether_newbuf(void)
562 {
563 struct mbuf *m_new;
564
565 m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
566 if (m_new == NULL)
567 return (NULL);
568 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
569
570 m_adj(m_new, ETHER_ALIGN);
571 return (m_new);
572 }
573
574 int
uether_rxmbuf(struct usb_ether * ue,struct mbuf * m,unsigned int len)575 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
576 unsigned int len)
577 {
578 struct ifnet *ifp = ue->ue_ifp;
579
580 UE_LOCK_ASSERT(ue, MA_OWNED);
581
582 /* finalize mbuf */
583 ifp->if_ipackets++;
584 m->m_pkthdr.rcvif = ifp;
585 m->m_pkthdr.len = m->m_len = len;
586
587 /* enqueue for later when the lock can be released */
588 _IF_ENQUEUE(&ue->ue_rxq, m);
589 return (0);
590 }
591
592 int
uether_rxbuf(struct usb_ether * ue,struct usb_page_cache * pc,unsigned int offset,unsigned int len)593 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
594 unsigned int offset, unsigned int len)
595 {
596 struct ifnet *ifp = ue->ue_ifp;
597 struct mbuf *m;
598
599 UE_LOCK_ASSERT(ue, MA_OWNED);
600
601 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
602 return (1);
603
604 m = uether_newbuf();
605 if (m == NULL) {
606 ifp->if_iqdrops++;
607 return (ENOMEM);
608 }
609
610 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
611
612 /* finalize mbuf */
613 ifp->if_ipackets++;
614 m->m_pkthdr.rcvif = ifp;
615 m->m_pkthdr.len = m->m_len = len;
616
617 /* enqueue for later when the lock can be released */
618 _IF_ENQUEUE(&ue->ue_rxq, m);
619 return (0);
620 }
621
622 void
uether_rxflush(struct usb_ether * ue)623 uether_rxflush(struct usb_ether *ue)
624 {
625 struct ifnet *ifp = ue->ue_ifp;
626 struct mbuf *m;
627
628 UE_LOCK_ASSERT(ue, MA_OWNED);
629
630 for (;;) {
631 _IF_DEQUEUE(&ue->ue_rxq, m);
632 if (m == NULL)
633 break;
634
635 /*
636 * The USB xfer has been resubmitted so its safe to unlock now.
637 */
638 UE_UNLOCK(ue);
639 ifp->if_input(ifp, m);
640 UE_LOCK(ue);
641 }
642 }
643
644 DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
645 MODULE_VERSION(uether, 1);
646