usbnet.c revision 1.22 1 /* $NetBSD: usbnet.c,v 1.22 2019/08/20 06:37:06 mrg Exp $ */
2
3 /*
4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * Common code shared between USB network drivers.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.22 2019/08/20 06:37:06 mrg Exp $");
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/module.h>
42
43 #include <dev/usb/usbnet.h>
44 #include <dev/usb/usbhist.h>
45
46 struct usbnet_cdata {
47 struct usbnet_chain *uncd_tx_chain;
48 struct usbnet_chain *uncd_rx_chain;
49
50 int uncd_tx_prod;
51 int uncd_tx_cnt;
52 int uncd_rx_cnt;
53 };
54
55 struct usbnet_private {
56 /*
57 * - unp_lock protects most of the structure, and the public one
58 * - unp_miilock must be held to access this device's MII bus
59 * - unp_rxlock protects the rx path and its data
60 * - unp_txlock protects the tx path and its data
61 * - unp_detachcv handles detach vs open references
62 */
63 kmutex_t unp_lock;
64 kmutex_t unp_miilock;
65 kmutex_t unp_rxlock;
66 kmutex_t unp_txlock;
67 kcondvar_t unp_detachcv;
68
69 struct usbnet_cdata unp_cdata;
70
71 struct ethercom unp_ec;
72 struct mii_data unp_mii;
73 struct usb_task unp_ticktask;
74 struct callout unp_stat_ch;
75 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX];
76
77 bool unp_dying;
78 bool unp_stopping;
79 bool unp_attached;
80 bool unp_link;
81
82 int unp_refcnt;
83 int unp_timer;
84 int unp_if_flags;
85
86 krndsource_t unp_rndsrc;
87
88 struct timeval unp_rx_notice;
89 struct timeval unp_tx_notice;
90 struct timeval unp_intr_notice;
91 };
92
93 #define un_cdata(un) (&(un)->un_pri->unp_cdata)
94
95 static int usbnet_modcmd(modcmd_t, void *);
96
97 #ifdef USB_DEBUG
98 #ifndef USBNET_DEBUG
99 #define usbnetdebug 0
100 #else
101 static int usbnetdebug = 1;
102
103 SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
104 {
105 int err;
106 const struct sysctlnode *rnode;
107 const struct sysctlnode *cnode;
108
109 err = sysctl_createv(clog, 0, NULL, &rnode,
110 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
111 SYSCTL_DESCR("usbnet global controls"),
112 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
113
114 if (err)
115 goto fail;
116
117 /* control debugging printfs */
118 err = sysctl_createv(clog, 0, &rnode, &cnode,
119 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
120 "debug", SYSCTL_DESCR("Enable debugging output"),
121 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
122 if (err)
123 goto fail;
124
125 return;
126 fail:
127 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
128 }
129
130 #endif /* USBNET_DEBUG */
131 #endif /* USB_DEBUG */
132
133 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
134 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
135 #define USBNETHIST_FUNC() USBHIST_FUNC()
136 #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug)
137 #define USBNETHIST_CALLARGS(FMT,A,B,C,D) \
138 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D)
139
140 /* Callback vectors. */
141
142 static void
143 uno_stop(struct usbnet *un, struct ifnet *ifp, int disable)
144 {
145 if (un->un_ops->uno_stop)
146 (*un->un_ops->uno_stop)(ifp, disable);
147 }
148
149 static int
150 uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
151 {
152 if (un->un_ops->uno_ioctl)
153 return (*un->un_ops->uno_ioctl)(ifp, cmd, data);
154 return 0;
155 }
156
157 static int
158 uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
159 {
160 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data);
161 }
162
163 static int
164 uno_init(struct usbnet *un, struct ifnet *ifp)
165 {
166 return (*un->un_ops->uno_init)(ifp);
167 }
168
169 static int
170 uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
171 {
172 return (*un->un_ops->uno_read_reg)(un, phy, reg, val);
173 }
174
175 static int
176 uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
177 {
178 return (*un->un_ops->uno_write_reg)(un, phy, reg, val);
179 }
180
181 static void
182 uno_mii_statchg(struct usbnet *un, struct ifnet *ifp)
183 {
184 (*un->un_ops->uno_statchg)(ifp);
185 }
186
187 static unsigned
188 uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
189 {
190 return (*un->un_ops->uno_tx_prepare)(un, m, c);
191 }
192
193 static void
194 uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
195 {
196 (*un->un_ops->uno_rx_loop)(un, c, total_len);
197 }
198
199 static void
200 uno_tick(struct usbnet *un)
201 {
202 if (un->un_ops->uno_tick)
203 (*un->un_ops->uno_tick)(un);
204 }
205
206 static void
207 uno_intr(struct usbnet *un, usbd_status status)
208 {
209 if (un->un_ops->uno_intr)
210 (*un->un_ops->uno_intr)(un, status);
211 }
212
213 /* Interrupt handling. */
214
215 static struct mbuf *
216 usbnet_newbuf(size_t buflen)
217 {
218 struct mbuf *m;
219
220 MGETHDR(m, M_DONTWAIT, MT_DATA);
221 if (m == NULL)
222 return NULL;
223
224 if (buflen > MHLEN - ETHER_ALIGN) {
225 MCLGET(m, M_DONTWAIT);
226 if (!(m->m_flags & M_EXT)) {
227 m_freem(m);
228 return NULL;
229 }
230 }
231
232 m_adj(m, ETHER_ALIGN);
233 m->m_len = m->m_pkthdr.len = buflen;
234
235 return m;
236 }
237
238 /*
239 * usbnet_rxeof() is designed to be the done callback for rx completion.
240 * it provides generic setup and finalisation, calls a different usbnet
241 * rx_loop callback in the middle, which can use usbnet_enqueue() to
242 * enqueue a packet for higher levels (or usbnet_input() if previously
243 * using if_input() path.)
244 */
245 void
246 usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
247 int csum_flags, uint32_t csum_data, int mbuf_flags)
248 {
249 USBNETHIST_FUNC(); USBNETHIST_CALLED();
250 struct ifnet * const ifp = usbnet_ifp(un);
251 struct mbuf *m;
252
253 usbnet_isowned_rx(un);
254
255 m = usbnet_newbuf(buflen);
256 if (m == NULL) {
257 ifp->if_ierrors++;
258 return;
259 }
260
261 m_set_rcvif(m, ifp);
262 m->m_pkthdr.csum_flags = csum_flags;
263 m->m_pkthdr.csum_data = csum_data;
264 m->m_flags |= mbuf_flags;
265 memcpy(mtod(m, uint8_t *), buf, buflen);
266
267 /* push the packet up */
268 if_percpuq_enqueue(ifp->if_percpuq, m);
269 }
270
271 void
272 usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
273 {
274 USBNETHIST_FUNC(); USBNETHIST_CALLED();
275 struct ifnet * const ifp = usbnet_ifp(un);
276 struct mbuf *m;
277
278 usbnet_isowned_rx(un);
279 DPRINTFN(0, "called! un %jx buf %jx len %ju",
280 (uintmax_t)(uintptr_t)un, (uintmax_t)(uintptr_t)buf, buflen, 0);
281
282 m = usbnet_newbuf(buflen);
283 if (m == NULL) {
284 ifp->if_ierrors++;
285 return;
286 }
287
288 m_set_rcvif(m, ifp);
289 memcpy(mtod(m, char *), buf, buflen);
290
291 /* push the packet up */
292 if_input(ifp, m);
293 }
294
295 /*
296 * A frame has been uploaded: pass the resulting mbuf chain up to
297 * the higher level protocols.
298 */
299 static void
300 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
301 {
302 USBNETHIST_FUNC(); USBNETHIST_CALLED();
303 struct usbnet_chain * const c = priv;
304 struct usbnet * const un = c->unc_un;
305 struct usbnet_private * const unp = un->un_pri;
306 struct ifnet * const ifp = usbnet_ifp(un);
307 uint32_t total_len;
308
309 mutex_enter(&unp->unp_rxlock);
310
311 if (unp->unp_dying || unp->unp_stopping ||
312 status == USBD_INVAL || status == USBD_NOT_STARTED ||
313 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
314 goto out;
315
316 if (status != USBD_NORMAL_COMPLETION) {
317 if (usbd_ratecheck(&unp->unp_rx_notice))
318 aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
319 usbd_errstr(status));
320 if (status == USBD_STALLED)
321 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]);
322 goto done;
323 }
324
325 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
326
327 if (total_len > un->un_rx_bufsz) {
328 aprint_error_dev(un->un_dev,
329 "rxeof: too large transfer (%u > %u)\n",
330 total_len, un->un_rx_bufsz);
331 goto done;
332 }
333
334 uno_rx_loop(un, c, total_len);
335 usbnet_isowned_rx(un);
336
337 done:
338 if (unp->unp_dying || unp->unp_stopping)
339 goto out;
340
341 mutex_exit(&unp->unp_rxlock);
342
343 /* Setup new transfer. */
344 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz,
345 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
346 usbd_transfer(xfer);
347 return;
348
349 out:
350 mutex_exit(&unp->unp_rxlock);
351 }
352
353 static void
354 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
355 {
356 USBNETHIST_FUNC(); USBNETHIST_CALLED();
357 struct usbnet_chain * const c = priv;
358 struct usbnet * const un = c->unc_un;
359 struct usbnet_cdata * const cd = un_cdata(un);
360 struct usbnet_private * const unp = un->un_pri;
361 struct ifnet * const ifp = usbnet_ifp(un);
362
363 mutex_enter(&unp->unp_txlock);
364 if (unp->unp_stopping || unp->unp_dying) {
365 mutex_exit(&unp->unp_txlock);
366 return;
367 }
368
369 KASSERT(cd->uncd_tx_cnt > 0);
370 cd->uncd_tx_cnt--;
371
372 unp->unp_timer = 0;
373
374 switch (status) {
375 case USBD_NOT_STARTED:
376 case USBD_CANCELLED:
377 break;
378
379 case USBD_NORMAL_COMPLETION:
380 ifp->if_opackets++;
381 break;
382
383 default:
384
385 ifp->if_oerrors++;
386 if (usbd_ratecheck(&unp->unp_tx_notice))
387 aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
388 usbd_errstr(status));
389 if (status == USBD_STALLED)
390 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]);
391 break;
392 }
393
394 mutex_exit(&unp->unp_txlock);
395
396 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
397 (*ifp->if_start)(ifp);
398 }
399
400 static void
401 usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
402 {
403 struct usbnet * const un = priv;
404 struct usbnet_private * const unp = un->un_pri;
405 struct usbnet_intr * const uni = un->un_intr;
406 struct ifnet * const ifp = usbnet_ifp(un);
407
408 if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
409 status == USBD_INVAL || status == USBD_NOT_STARTED ||
410 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
411 return;
412
413 if (status != USBD_NORMAL_COMPLETION) {
414 if (usbd_ratecheck(&unp->unp_intr_notice)) {
415 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
416 usbd_errstr(status));
417 }
418 if (status == USBD_STALLED)
419 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
420 return;
421 }
422
423 uno_intr(un, status);
424 }
425
426 static void
427 usbnet_start_locked(struct ifnet *ifp)
428 {
429 USBNETHIST_FUNC(); USBNETHIST_CALLED();
430 struct usbnet * const un = ifp->if_softc;
431 struct usbnet_cdata * const cd = un_cdata(un);
432 struct usbnet_private * const unp = un->un_pri;
433 struct mbuf *m;
434 unsigned length;
435 int idx;
436
437 usbnet_isowned_tx(un);
438 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
439
440 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
441 DPRINTF("start called no link (%x) or running (flags %x)",
442 unp->unp_link, ifp->if_flags, 0, 0);
443 return;
444 }
445
446 idx = cd->uncd_tx_prod;
447 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
448 IFQ_POLL(&ifp->if_snd, m);
449 if (m == NULL)
450 break;
451 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
452
453 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
454
455 length = uno_tx_prepare(un, m, c);
456 if (length == 0) {
457 ifp->if_oerrors++;
458 break;
459 }
460
461 if (__predict_false(c->unc_xfer == NULL)) {
462 ifp->if_oerrors++;
463 break;
464 }
465
466 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
467 un->un_tx_xfer_flags, 10000, usbnet_txeof);
468
469 /* Transmit */
470 usbd_status err = usbd_transfer(c->unc_xfer);
471 if (err != USBD_IN_PROGRESS) {
472 ifp->if_oerrors++;
473 break;
474 }
475
476 IFQ_DEQUEUE(&ifp->if_snd, m);
477
478 /*
479 * If there's a BPF listener, bounce a copy of this frame
480 * to him.
481 */
482 bpf_mtap(ifp, m, BPF_D_OUT);
483 m_freem(m);
484
485 idx = (idx + 1) % un->un_tx_list_cnt;
486 cd->uncd_tx_cnt++;
487 }
488 cd->uncd_tx_prod = idx;
489
490 /*
491 * Set a timeout in case the chip goes out to lunch.
492 */
493 unp->unp_timer = 5;
494 }
495
496 static void
497 usbnet_start(struct ifnet *ifp)
498 {
499 struct usbnet * const un = ifp->if_softc;
500 struct usbnet_private * const unp = un->un_pri;
501
502 mutex_enter(&unp->unp_txlock);
503 if (!unp->unp_stopping)
504 usbnet_start_locked(ifp);
505 mutex_exit(&unp->unp_txlock);
506 }
507
508 /*
509 * Chain management.
510 *
511 * RX and TX are identical. Keep them that way.
512 */
513
514 /* Start of common RX functions */
515
516 static size_t
517 usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
518 {
519 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
520 }
521
522 static void
523 usbnet_rx_list_alloc(struct usbnet * const un)
524 {
525 struct usbnet_cdata * const cd = un_cdata(un);
526
527 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
528 }
529
530 static void
531 usbnet_rx_list_free(struct usbnet * const un)
532 {
533 struct usbnet_cdata * const cd = un_cdata(un);
534
535 if (cd->uncd_rx_chain) {
536 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
537 cd->uncd_rx_chain = NULL;
538 }
539 }
540
541 static int
542 usbnet_rx_list_init(struct usbnet * const un)
543 {
544 struct usbnet_cdata * const cd = un_cdata(un);
545 struct usbnet_private * const unp = un->un_pri;
546
547 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
548 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
549
550 c->unc_un = un;
551 if (c->unc_xfer == NULL) {
552 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
553 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
554 &c->unc_xfer);
555 if (err)
556 return err;
557 c->unc_buf = usbd_get_buffer(c->unc_xfer);
558 }
559 }
560
561 return 0;
562 }
563
564 static void
565 usbnet_rx_list_fini(struct usbnet * const un)
566 {
567 struct usbnet_cdata * const cd = un_cdata(un);
568
569 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
570 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
571
572 if (c->unc_xfer != NULL) {
573 usbd_destroy_xfer(c->unc_xfer);
574 c->unc_xfer = NULL;
575 c->unc_buf = NULL;
576 }
577 }
578 }
579
580 /* End of common RX functions */
581
582 static void
583 usbnet_rx_start_pipes(struct usbnet * const un)
584 {
585 struct usbnet_cdata * const cd = un_cdata(un);
586 struct usbnet_private * const unp = un->un_pri;
587
588 mutex_enter(&unp->unp_rxlock);
589 mutex_enter(&unp->unp_txlock);
590 unp->unp_stopping = false;
591
592 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
593 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
594
595 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
596 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
597 usbd_transfer(c->unc_xfer);
598 }
599
600 mutex_exit(&unp->unp_txlock);
601 mutex_exit(&unp->unp_rxlock);
602 }
603
604 /* Start of common TX functions */
605
606 static size_t
607 usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
608 {
609 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
610 }
611
612 static void
613 usbnet_tx_list_alloc(struct usbnet * const un)
614 {
615 struct usbnet_cdata * const cd = un_cdata(un);
616
617 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
618 }
619
620 static void
621 usbnet_tx_list_free(struct usbnet * const un)
622 {
623 struct usbnet_cdata * const cd = un_cdata(un);
624
625 if (cd->uncd_tx_chain) {
626 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
627 cd->uncd_tx_chain = NULL;
628 }
629 }
630
631 static int
632 usbnet_tx_list_init(struct usbnet * const un)
633 {
634 struct usbnet_cdata * const cd = un_cdata(un);
635 struct usbnet_private * const unp = un->un_pri;
636
637 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
638 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
639
640 c->unc_un = un;
641 if (c->unc_xfer == NULL) {
642 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
643 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
644 &c->unc_xfer);
645 if (err)
646 return err;
647 c->unc_buf = usbd_get_buffer(c->unc_xfer);
648 }
649 }
650
651 return 0;
652 }
653
654 static void
655 usbnet_tx_list_fini(struct usbnet * const un)
656 {
657 struct usbnet_cdata * const cd = un_cdata(un);
658
659 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
660 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
661
662 if (c->unc_xfer != NULL) {
663 usbd_destroy_xfer(c->unc_xfer);
664 c->unc_xfer = NULL;
665 c->unc_buf = NULL;
666 }
667 }
668 }
669
670 /* End of common TX functions */
671
672 /* Endpoint pipe management. */
673
674 static void
675 usbnet_ep_close_pipes(struct usbnet * const un)
676 {
677 struct usbnet_private * const unp = un->un_pri;
678
679 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
680 if (unp->unp_ep[i] == NULL)
681 continue;
682 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
683 if (err)
684 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
685 usbd_errstr(err));
686 unp->unp_ep[i] = NULL;
687 }
688 }
689
690 static usbd_status
691 usbnet_ep_open_pipes(struct usbnet * const un)
692 {
693 struct usbnet_intr * const uni = un->un_intr;
694 struct usbnet_private * const unp = un->un_pri;
695
696 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
697 usbd_status err;
698
699 if (un->un_ed[i] == 0)
700 continue;
701
702 if (i == USBNET_ENDPT_INTR && uni) {
703 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
704 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
705 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
706 uni->uni_interval);
707 } else {
708 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
709 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
710 }
711 if (err) {
712 usbnet_ep_close_pipes(un);
713 return err;
714 }
715 }
716
717 return USBD_NORMAL_COMPLETION;
718 }
719
720 static usbd_status
721 usbnet_ep_stop_pipes(struct usbnet * const un)
722 {
723 struct usbnet_private * const unp = un->un_pri;
724 usbd_status err = USBD_NORMAL_COMPLETION;
725
726 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
727 if (unp->unp_ep[i] == NULL)
728 continue;
729 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]);
730 if (err == USBD_NORMAL_COMPLETION && err2)
731 err = err2;
732 }
733
734 return err;
735 }
736
737 int
738 usbnet_init_rx_tx(struct usbnet * const un)
739 {
740 USBNETHIST_FUNC(); USBNETHIST_CALLED();
741 struct usbnet_private * const unp = un->un_pri;
742 struct ifnet * const ifp = usbnet_ifp(un);
743 usbd_status err;
744 int error = 0;
745
746 usbnet_isowned(un);
747
748 if (unp->unp_dying) {
749 return EIO;
750 }
751 unp->unp_refcnt++;
752
753 /* Open RX and TX pipes. */
754 err = usbnet_ep_open_pipes(un);
755 if (err) {
756 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
757 usbd_errstr(err));
758 error = EIO;
759 goto out;
760 }
761
762 /* Init RX ring. */
763 if (usbnet_rx_list_init(un)) {
764 aprint_error_dev(un->un_dev, "rx list init failed\n");
765 error = ENOBUFS;
766 goto out;
767 }
768
769 /* Init TX ring. */
770 if (usbnet_tx_list_init(un)) {
771 aprint_error_dev(un->un_dev, "tx list init failed\n");
772 error = ENOBUFS;
773 goto out;
774 }
775
776 /* Start up the receive pipe(s). */
777 usbnet_rx_start_pipes(un);
778
779 /* Indicate we are up and running. */
780 #if 0
781 /* XXX if_mcast_op() can call this without ifnet locked */
782 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
783 #endif
784 ifp->if_flags |= IFF_RUNNING;
785
786 callout_schedule(&unp->unp_stat_ch, hz);
787
788 out:
789 if (error) {
790 usbnet_rx_list_fini(un);
791 usbnet_tx_list_fini(un);
792 usbnet_ep_close_pipes(un);
793 }
794 if (--unp->unp_refcnt < 0)
795 cv_broadcast(&unp->unp_detachcv);
796
797 usbnet_isowned(un);
798
799 return error;
800 }
801
802 /* MII management. */
803
804 /*
805 * Access functions for MII. Take the MII lock to call access MII regs.
806 * Two forms: usbnet (softc) lock currently held or not.
807 */
808 void
809 usbnet_lock_mii(struct usbnet *un)
810 {
811 struct usbnet_private * const unp = un->un_pri;
812
813 mutex_enter(&unp->unp_lock);
814 unp->unp_refcnt++;
815 mutex_exit(&unp->unp_lock);
816
817 mutex_enter(&unp->unp_miilock);
818 }
819
820 void
821 usbnet_lock_mii_un_locked(struct usbnet *un)
822 {
823 struct usbnet_private * const unp = un->un_pri;
824
825 usbnet_isowned(un);
826
827 unp->unp_refcnt++;
828 mutex_enter(&unp->unp_miilock);
829 }
830
831 void
832 usbnet_unlock_mii(struct usbnet *un)
833 {
834 struct usbnet_private * const unp = un->un_pri;
835
836 mutex_exit(&unp->unp_miilock);
837 mutex_enter(&unp->unp_lock);
838 if (--unp->unp_refcnt < 0)
839 cv_broadcast(&unp->unp_detachcv);
840 mutex_exit(&unp->unp_lock);
841 }
842
843 void
844 usbnet_unlock_mii_un_locked(struct usbnet *un)
845 {
846 struct usbnet_private * const unp = un->un_pri;
847
848 usbnet_isowned(un);
849
850 mutex_exit(&unp->unp_miilock);
851 if (--unp->unp_refcnt < 0)
852 cv_broadcast(&unp->unp_detachcv);
853 }
854
855 kmutex_t *
856 usbnet_mutex_mii(struct usbnet *un)
857 {
858 struct usbnet_private * const unp = un->un_pri;
859
860 return &unp->unp_miilock;
861 }
862
863 int
864 usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
865 {
866 USBNETHIST_FUNC();
867 struct usbnet * const un = device_private(dev);
868 struct usbnet_private * const unp = un->un_pri;
869 int err;
870
871 mutex_enter(&unp->unp_lock);
872 if (unp->unp_dying) {
873 mutex_exit(&unp->unp_lock);
874 return EIO;
875 }
876
877 usbnet_lock_mii_un_locked(un);
878 mutex_exit(&unp->unp_lock);
879 err = uno_read_reg(un, phy, reg, val);
880 usbnet_unlock_mii(un);
881
882 if (err) {
883 USBNETHIST_CALLARGS("read PHY failed: %d", err, 0, 0, 0);
884 return err;
885 }
886
887 return 0;
888 }
889
890 int
891 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
892 {
893 USBNETHIST_FUNC();
894 struct usbnet * const un = device_private(dev);
895 struct usbnet_private * const unp = un->un_pri;
896 int err;
897
898 mutex_enter(&unp->unp_lock);
899 if (unp->unp_dying) {
900 mutex_exit(&unp->unp_lock);
901 return EIO;
902 }
903
904 usbnet_lock_mii_un_locked(un);
905 mutex_exit(&unp->unp_lock);
906 err = uno_write_reg(un, phy, reg, val);
907 usbnet_unlock_mii(un);
908
909 if (err) {
910 USBNETHIST_CALLARGS("write PHY failed: %d", err, 0, 0, 0);
911 return err;
912 }
913
914 return 0;
915 }
916
917 void
918 usbnet_mii_statchg(struct ifnet *ifp)
919 {
920 USBNETHIST_FUNC(); USBNETHIST_CALLED();
921 struct usbnet * const un = ifp->if_softc;
922
923 uno_mii_statchg(un, ifp);
924 }
925
926 static int
927 usbnet_media_upd(struct ifnet *ifp)
928 {
929 USBNETHIST_FUNC(); USBNETHIST_CALLED();
930 struct usbnet * const un = ifp->if_softc;
931 struct usbnet_private * const unp = un->un_pri;
932 struct mii_data * const mii = usbnet_mii(un);
933
934 if (unp->unp_dying)
935 return EIO;
936
937 unp->unp_link = false;
938
939 if (mii->mii_instance) {
940 struct mii_softc *miisc;
941
942 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
943 mii_phy_reset(miisc);
944 }
945
946 return ether_mediachange(ifp);
947 }
948
949 /* ioctl */
950
951 static int
952 usbnet_ifflags_cb(struct ethercom *ec)
953 {
954 USBNETHIST_FUNC(); USBNETHIST_CALLED();
955 struct ifnet *ifp = &ec->ec_if;
956 struct usbnet *un = ifp->if_softc;
957 struct usbnet_private * const unp = un->un_pri;
958 int rv = 0;
959
960 mutex_enter(&unp->unp_lock);
961
962 const int changed = ifp->if_flags ^ unp->unp_if_flags;
963 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
964 unp->unp_if_flags = ifp->if_flags;
965 if ((changed & IFF_PROMISC) != 0)
966 rv = ENETRESET;
967 } else {
968 rv = ENETRESET;
969 }
970
971 mutex_exit(&unp->unp_lock);
972
973 return rv;
974 }
975
976 static int
977 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
978 {
979 USBNETHIST_FUNC(); USBNETHIST_CALLED();
980 struct usbnet * const un = ifp->if_softc;
981 int error;
982
983 if (un->un_ops->uno_override_ioctl)
984 return uno_override_ioctl(un, ifp, cmd, data);
985
986 error = ether_ioctl(ifp, cmd, data);
987 if (error == ENETRESET)
988 error = uno_ioctl(un, ifp, cmd, data);
989
990 return error;
991 }
992
993 /*
994 * Generic stop network function:
995 * - mark as stopping
996 * - call DD routine to stop the device
997 * - turn off running, timer, statchg callout, link
998 * - stop transfers
999 * - free RX and TX resources
1000 * - close pipes
1001 *
1002 * usbnet_stop() is exported for drivers to use, expects lock held.
1003 *
1004 * usbnet_stop_ifp() is for the if_stop handler.
1005 */
1006 void
1007 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
1008 {
1009 struct usbnet_private * const unp = un->un_pri;
1010
1011 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1012
1013 usbnet_isowned(un);
1014
1015 mutex_enter(&unp->unp_rxlock);
1016 mutex_enter(&unp->unp_txlock);
1017 unp->unp_stopping = true;
1018 mutex_exit(&unp->unp_txlock);
1019 mutex_exit(&unp->unp_rxlock);
1020
1021 uno_stop(un, ifp, disable);
1022
1023 /*
1024 * XXXSMP Would like to
1025 * KASSERT(IFNET_LOCKED(ifp))
1026 * here but the locking order is:
1027 * ifnet -> unlock -> rxlock -> txlock
1028 * and unlock is already held.
1029 */
1030 ifp->if_flags &= ~IFF_RUNNING;
1031 unp->unp_timer = 0;
1032
1033 callout_stop(&unp->unp_stat_ch);
1034
1035 /* Stop transfers. */
1036 usbnet_ep_stop_pipes(un);
1037
1038 /* Free RX/TX resources. */
1039 usbnet_rx_list_fini(un);
1040 usbnet_tx_list_fini(un);
1041
1042 /* Close pipes. */
1043 usbnet_ep_close_pipes(un);
1044 }
1045
1046 static void
1047 usbnet_stop_ifp(struct ifnet *ifp, int disable)
1048 {
1049 struct usbnet * const un = ifp->if_softc;
1050 struct usbnet_private * const unp = un->un_pri;
1051
1052 mutex_enter(&unp->unp_lock);
1053 usbnet_stop(un, ifp, disable);
1054 mutex_exit(&unp->unp_lock);
1055 }
1056
1057 /*
1058 * Generic tick task function.
1059 *
1060 * usbnet_tick() is triggered from a callout, and triggers a call to
1061 * usbnet_tick_task() from the usb_task subsystem.
1062 */
1063 static void
1064 usbnet_tick(void *arg)
1065 {
1066 struct usbnet * const un = arg;
1067 struct usbnet_private * const unp = un->un_pri;
1068
1069 mutex_enter(&unp->unp_lock);
1070 if (!unp->unp_stopping && !unp->unp_dying) {
1071 /* Perform periodic stuff in process context */
1072 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1073 }
1074 mutex_exit(&unp->unp_lock);
1075 }
1076
1077 static void
1078 usbnet_watchdog(struct ifnet *ifp)
1079 {
1080 struct usbnet * const un = ifp->if_softc;
1081 struct usbnet_private * const unp = un->un_pri;
1082 struct usbnet_cdata * const cd = un_cdata(un);
1083 usbd_status err;
1084
1085 ifp->if_oerrors++;
1086 aprint_error_dev(un->un_dev, "watchdog timeout\n");
1087
1088 if (cd->uncd_tx_cnt > 0) {
1089 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]);
1090 if (err)
1091 aprint_error_dev(un->un_dev, "pipe abort failed: %s\n",
1092 usbd_errstr(err));
1093 }
1094
1095 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1096 (*ifp->if_start)(ifp);
1097 }
1098
1099 static void
1100 usbnet_tick_task(void *arg)
1101 {
1102 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1103 struct usbnet * const un = arg;
1104 struct usbnet_private * const unp = un->un_pri;
1105
1106 mutex_enter(&unp->unp_lock);
1107 if (unp->unp_stopping || unp->unp_dying) {
1108 mutex_exit(&unp->unp_lock);
1109 return;
1110 }
1111
1112 struct ifnet * const ifp = usbnet_ifp(un);
1113 struct mii_data * const mii = usbnet_mii(un);
1114
1115 unp->unp_refcnt++;
1116 mutex_exit(&unp->unp_lock);
1117
1118 if (ifp && unp->unp_timer != 0 && --unp->unp_timer == 0)
1119 usbnet_watchdog(ifp);
1120
1121 if (mii && ifp) {
1122 mii_tick(mii);
1123
1124 if (!unp->unp_link)
1125 (*mii->mii_statchg)(ifp);
1126 }
1127
1128 /* Call driver if requested. */
1129 uno_tick(un);
1130
1131 mutex_enter(&unp->unp_lock);
1132 if (--unp->unp_refcnt < 0)
1133 cv_broadcast(&unp->unp_detachcv);
1134 if (!unp->unp_stopping && !unp->unp_dying)
1135 callout_schedule(&unp->unp_stat_ch, hz);
1136 mutex_exit(&unp->unp_lock);
1137 }
1138
1139 static int
1140 usbnet_init(struct ifnet *ifp)
1141 {
1142 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1143 struct usbnet * const un = ifp->if_softc;
1144
1145 return uno_init(un, ifp);
1146 }
1147
1148
1149 /* Various accessors. */
1150
1151 void
1152 usbnet_set_link(struct usbnet *un, bool link)
1153 {
1154 un->un_pri->unp_link = link;
1155 }
1156
1157 void
1158 usbnet_set_dying(struct usbnet *un, bool link)
1159 {
1160 un->un_pri->unp_dying = link;
1161 }
1162
1163 struct ifnet *
1164 usbnet_ifp(struct usbnet *un)
1165 {
1166 return &un->un_pri->unp_ec.ec_if;
1167 }
1168
1169 struct ethercom *
1170 usbnet_ec(struct usbnet *un)
1171 {
1172 return &un->un_pri->unp_ec;
1173 }
1174
1175 struct mii_data *
1176 usbnet_mii(struct usbnet *un)
1177 {
1178 return un->un_pri->unp_ec.ec_mii;
1179 }
1180
1181 krndsource_t *
1182 usbnet_rndsrc(struct usbnet *un)
1183 {
1184 return &un->un_pri->unp_rndsrc;
1185 }
1186
1187 void *
1188 usbnet_softc(struct usbnet *un)
1189 {
1190 //return un->un_pri->unp_sc;
1191 return un->un_sc;
1192 }
1193
1194 bool
1195 usbnet_havelink(struct usbnet *un)
1196 {
1197 return un->un_pri->unp_link;
1198 }
1199
1200 bool
1201 usbnet_isdying(struct usbnet *un)
1202 {
1203 return un->un_pri->unp_dying;
1204 }
1205
1206
1207 /* Locking. */
1208
1209 void
1210 usbnet_lock(struct usbnet *un)
1211 {
1212 mutex_enter(&un->un_pri->unp_lock);
1213 }
1214
1215 void
1216 usbnet_unlock(struct usbnet *un)
1217 {
1218 mutex_exit(&un->un_pri->unp_lock);
1219 }
1220
1221 kmutex_t *
1222 usbnet_mutex(struct usbnet *un)
1223 {
1224 return &un->un_pri->unp_lock;
1225 }
1226
1227 void
1228 usbnet_lock_rx(struct usbnet *un)
1229 {
1230 mutex_enter(&un->un_pri->unp_rxlock);
1231 }
1232
1233 void
1234 usbnet_unlock_rx(struct usbnet *un)
1235 {
1236 mutex_exit(&un->un_pri->unp_rxlock);
1237 }
1238
1239 kmutex_t *
1240 usbnet_mutex_rx(struct usbnet *un)
1241 {
1242 return &un->un_pri->unp_rxlock;
1243 }
1244
1245 void
1246 usbnet_lock_tx(struct usbnet *un)
1247 {
1248 mutex_enter(&un->un_pri->unp_txlock);
1249 }
1250
1251 void
1252 usbnet_unlock_tx(struct usbnet *un)
1253 {
1254 mutex_exit(&un->un_pri->unp_txlock);
1255 }
1256
1257 kmutex_t *
1258 usbnet_mutex_tx(struct usbnet *un)
1259 {
1260 return &un->un_pri->unp_txlock;
1261 }
1262
1263 /* Autoconf management. */
1264
1265 static bool
1266 usbnet_empty_eaddr(struct usbnet * const un)
1267 {
1268 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
1269 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
1270 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
1271 }
1272
1273 /*
1274 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
1275 * 'usbnet'. The first is enough to enable device access (eg, endpoints
1276 * are connected and commands can be sent), and the second connects the
1277 * device to the system networking.
1278 *
1279 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
1280 * Also usable as driver detach directly.
1281 *
1282 * To skip ethernet configuration (eg, point-to-point), make sure that
1283 * the un_eaddr[] is fully zero.
1284 */
1285
1286 void
1287 usbnet_attach(struct usbnet *un,
1288 const char *detname) /* detach cv name */
1289 {
1290 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1291
1292 /* Required inputs. */
1293 KASSERT(un->un_ops->uno_tx_prepare);
1294 KASSERT(un->un_ops->uno_rx_loop);
1295 KASSERT(un->un_ops->uno_init);
1296 KASSERT(un->un_rx_bufsz);
1297 KASSERT(un->un_tx_bufsz);
1298 KASSERT(un->un_rx_list_cnt);
1299 KASSERT(un->un_tx_list_cnt);
1300
1301 /* Unfortunate fact. */
1302 KASSERT(un == device_private(un->un_dev));
1303
1304 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
1305 struct usbnet_private * const unp = un->un_pri;
1306
1307 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
1308 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
1309 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
1310
1311 mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE);
1312 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1313 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1314 mutex_init(&unp->unp_lock, MUTEX_DEFAULT, IPL_NONE);
1315 cv_init(&unp->unp_detachcv, detname);
1316
1317 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
1318 RND_TYPE_NET, RND_FLAG_DEFAULT);
1319
1320 usbnet_rx_list_alloc(un);
1321 usbnet_tx_list_alloc(un);
1322
1323 unp->unp_attached = true;
1324 }
1325
1326 static void
1327 usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm)
1328 {
1329 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1330 struct usbnet_private * const unp = un->un_pri;
1331 struct mii_data * const mii = &unp->unp_mii;
1332 struct ifnet * const ifp = usbnet_ifp(un);
1333
1334 KASSERT(un->un_ops->uno_read_reg);
1335 KASSERT(un->un_ops->uno_write_reg);
1336 KASSERT(un->un_ops->uno_statchg);
1337
1338 mii->mii_ifp = ifp;
1339 mii->mii_readreg = usbnet_mii_readreg;
1340 mii->mii_writereg = usbnet_mii_writereg;
1341 mii->mii_statchg = usbnet_mii_statchg;
1342 mii->mii_flags = MIIF_AUTOTSLEEP;
1343
1344 usbnet_ec(un)->ec_mii = mii;
1345 ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
1346 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc,
1347 unm->un_mii_offset, unm->un_mii_flags);
1348
1349 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1350 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1351 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1352 } else
1353 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1354 }
1355
1356 void
1357 usbnet_attach_ifp(struct usbnet *un,
1358 unsigned if_flags, /* additional if_flags */
1359 unsigned if_extflags, /* additional if_extflags */
1360 const struct usbnet_mii *unm) /* additional mii_attach flags */
1361 {
1362 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1363 struct usbnet_private * const unp = un->un_pri;
1364 struct ifnet * const ifp = usbnet_ifp(un);
1365
1366 KASSERT(unp->unp_attached);
1367
1368 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1369 ifp->if_flags = if_flags;
1370 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1371 ifp->if_ioctl = usbnet_ioctl;
1372 ifp->if_start = usbnet_start;
1373 ifp->if_init = usbnet_init;
1374 ifp->if_stop = usbnet_stop_ifp;
1375
1376 if (unm)
1377 usbnet_attach_mii(un, unm);
1378 else
1379 unp->unp_link = true;
1380
1381 /* Attach the interface. */
1382 int rv = if_initialize(ifp);
1383 if (rv != 0) {
1384 aprint_error_dev(un->un_dev, "if_initialize failed(%d)\n", rv);
1385 return;
1386 }
1387 if (ifp->_if_input == NULL)
1388 ifp->if_percpuq = if_percpuq_create(ifp);
1389 if_register(ifp);
1390
1391 /*
1392 * If ethernet address is all zero, skip ether_ifattach() and
1393 * instead attach bpf here..
1394 */
1395 if (!usbnet_empty_eaddr(un)) {
1396 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
1397 aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1398 ether_sprintf(un->un_eaddr));
1399 ether_ifattach(ifp, un->un_eaddr);
1400 } else {
1401 if_alloc_sadl(ifp);
1402 bpf_attach(ifp, DLT_RAW, 0);
1403 }
1404
1405 /* Now ready, and attached. */
1406 IFQ_SET_READY(&ifp->if_snd);
1407 ifp->if_softc = un;
1408
1409 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1410
1411 if (!pmf_device_register(un->un_dev, NULL, NULL))
1412 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1413 }
1414
1415 int
1416 usbnet_detach(device_t self, int flags)
1417 {
1418 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1419 struct usbnet * const un = device_private(self);
1420 struct ifnet * const ifp = usbnet_ifp(un);
1421 struct mii_data * const mii = usbnet_mii(un);
1422 struct usbnet_private * const unp = un->un_pri;
1423
1424 mutex_enter(&unp->unp_lock);
1425 unp->unp_dying = true;
1426 mutex_exit(&unp->unp_lock);
1427
1428 /* Detached before attached finished, so just bail out. */
1429 if (!unp->unp_attached)
1430 return 0;
1431
1432 callout_halt(&unp->unp_stat_ch, NULL);
1433 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL);
1434
1435 if (ifp->if_flags & IFF_RUNNING) {
1436 IFNET_LOCK(ifp);
1437 usbnet_stop_ifp(ifp, 1);
1438 IFNET_UNLOCK(ifp);
1439 }
1440
1441 mutex_enter(&unp->unp_lock);
1442 unp->unp_refcnt--;
1443 while (unp->unp_refcnt > 0) {
1444 /* Wait for processes to go away */
1445 cv_wait(&unp->unp_detachcv, &unp->unp_lock);
1446 }
1447 mutex_exit(&unp->unp_lock);
1448
1449 usbnet_rx_list_free(un);
1450 usbnet_tx_list_free(un);
1451
1452 callout_destroy(&unp->unp_stat_ch);
1453 rnd_detach_source(&unp->unp_rndsrc);
1454
1455 if (mii) {
1456 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1457 ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
1458 }
1459 if (ifp->if_softc) {
1460 if (!usbnet_empty_eaddr(un))
1461 ether_ifdetach(ifp);
1462 else
1463 bpf_detach(ifp);
1464 if_detach(ifp);
1465 }
1466
1467 cv_destroy(&unp->unp_detachcv);
1468 mutex_destroy(&unp->unp_lock);
1469 mutex_destroy(&unp->unp_rxlock);
1470 mutex_destroy(&unp->unp_txlock);
1471 mutex_destroy(&unp->unp_miilock);
1472
1473 pmf_device_deregister(un->un_dev);
1474
1475 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1476
1477 kmem_free(unp, sizeof(*unp));
1478
1479 return 0;
1480 }
1481
1482 int
1483 usbnet_activate(device_t self, devact_t act)
1484 {
1485 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1486 struct usbnet * const un = device_private(self);
1487 struct usbnet_private * const unp = un->un_pri;
1488 struct ifnet * const ifp = usbnet_ifp(un);
1489
1490 switch (act) {
1491 case DVACT_DEACTIVATE:
1492 if_deactivate(ifp);
1493
1494 mutex_enter(&unp->unp_lock);
1495 unp->unp_dying = true;
1496 mutex_exit(&unp->unp_lock);
1497
1498 mutex_enter(&unp->unp_rxlock);
1499 mutex_enter(&unp->unp_txlock);
1500 unp->unp_stopping = true;
1501 mutex_exit(&unp->unp_txlock);
1502 mutex_exit(&unp->unp_rxlock);
1503
1504 return 0;
1505 default:
1506 return EOPNOTSUPP;
1507 }
1508 }
1509
1510 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1511
1512 static int
1513 usbnet_modcmd(modcmd_t cmd, void *arg)
1514 {
1515 switch (cmd) {
1516 case MODULE_CMD_INIT:
1517 return 0;
1518 case MODULE_CMD_FINI:
1519 return 0;
1520 case MODULE_CMD_STAT:
1521 case MODULE_CMD_AUTOUNLOAD:
1522 default:
1523 return ENOTTY;
1524 }
1525 }
1526