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