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