usbnet.c revision 1.11 1 /* $NetBSD: usbnet.c,v 1.11 2019/08/10 02:17:36 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 ethernet drivers.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.11 2019/08/10 02:17:36 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 KASSERT(mutex_owned(&un->un_pri->unp_rxlock));
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 KASSERT(mutex_owned(&un->un_pri->unp_rxlock));
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 KASSERT(mutex_owned(&unp->unp_rxlock));
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 KASSERT(mutex_owned(&unp->unp_txlock));
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
442 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
443
444 length = uno_tx_prepare(un, m, c);
445 if (length == 0) {
446 ifp->if_oerrors++;
447 break;
448 }
449
450 if (__predict_false(c->unc_xfer == NULL)) {
451 ifp->if_oerrors++;
452 break;
453 }
454
455 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
456 un->un_tx_xfer_flags, 10000, usbnet_txeof);
457
458 /* Transmit */
459 usbd_status err = usbd_transfer(c->unc_xfer);
460 if (err != USBD_IN_PROGRESS) {
461 ifp->if_oerrors++;
462 break;
463 }
464
465 IFQ_DEQUEUE(&ifp->if_snd, m);
466
467 /*
468 * If there's a BPF listener, bounce a copy of this frame
469 * to him.
470 */
471 bpf_mtap(ifp, m, BPF_D_OUT);
472 m_freem(m);
473
474 idx = (idx + 1) % un->un_tx_list_cnt;
475 cd->uncd_tx_cnt++;
476 }
477 cd->uncd_tx_prod = idx;
478
479 /*
480 * Set a timeout in case the chip goes out to lunch.
481 */
482 unp->unp_timer = 5;
483 }
484
485 static void
486 usbnet_start(struct ifnet *ifp)
487 {
488 struct usbnet * const un = ifp->if_softc;
489 struct usbnet_private * const unp = un->un_pri;
490
491 mutex_enter(&unp->unp_txlock);
492 if (!unp->unp_stopping)
493 usbnet_start_locked(ifp);
494 mutex_exit(&unp->unp_txlock);
495 }
496
497 /*
498 * Chain management.
499 *
500 * RX and TX are identical. Keep them that way.
501 */
502
503 /* Start of common RX functions */
504
505 static size_t
506 usbnet_rx_list_size(struct usbnet_cdata *cd, struct usbnet *un)
507 {
508 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
509 }
510
511 static void
512 usbnet_rx_list_alloc(struct usbnet *un)
513 {
514 struct usbnet_cdata * const cd = un_cdata(un);
515
516 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
517 }
518
519 static void
520 usbnet_rx_list_free(struct usbnet *un)
521 {
522 struct usbnet_cdata * const cd = un_cdata(un);
523
524 if (cd->uncd_rx_chain) {
525 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
526 cd->uncd_rx_chain = NULL;
527 }
528 }
529
530 static int
531 usbnet_rx_list_init(struct usbnet *un)
532 {
533 struct usbnet_cdata * const cd = un_cdata(un);
534 struct usbnet_private * const unp = un->un_pri;
535
536 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
537 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
538
539 c->unc_un = un;
540 if (c->unc_xfer == NULL) {
541 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
542 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
543 &c->unc_xfer);
544 if (err)
545 return err;
546 c->unc_buf = usbd_get_buffer(c->unc_xfer);
547 }
548 }
549
550 return 0;
551 }
552
553 static void
554 usbnet_rx_list_fini(struct usbnet *un)
555 {
556 struct usbnet_cdata * const cd = un_cdata(un);
557
558 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
559 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
560
561 if (c->unc_xfer != NULL) {
562 usbd_destroy_xfer(c->unc_xfer);
563 c->unc_xfer = NULL;
564 c->unc_buf = NULL;
565 }
566 }
567 }
568
569 /* End of common RX functions */
570
571 static void
572 usbnet_rx_start_pipes(struct usbnet *un, usbd_callback cb)
573 {
574 struct usbnet_cdata * const cd = un_cdata(un);
575 struct usbnet_private * const unp = un->un_pri;
576
577 mutex_enter(&unp->unp_rxlock);
578 mutex_enter(&unp->unp_txlock);
579 unp->unp_stopping = false;
580
581 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
582 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
583
584 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
585 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, cb);
586 usbd_transfer(c->unc_xfer);
587 }
588
589 mutex_exit(&unp->unp_txlock);
590 mutex_exit(&unp->unp_rxlock);
591 }
592
593 /* Start of common TX functions */
594
595 static size_t
596 usbnet_tx_list_size(struct usbnet_cdata *cd, struct usbnet *un)
597 {
598 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
599 }
600
601 static void
602 usbnet_tx_list_alloc(struct usbnet *un)
603 {
604 struct usbnet_cdata * const cd = un_cdata(un);
605
606 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
607 }
608
609 static void
610 usbnet_tx_list_free(struct usbnet *un)
611 {
612 struct usbnet_cdata * const cd = un_cdata(un);
613
614 if (cd->uncd_tx_chain) {
615 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
616 cd->uncd_tx_chain = NULL;
617 }
618 }
619
620 static int
621 usbnet_tx_list_init(struct usbnet *un)
622 {
623 struct usbnet_cdata * const cd = un_cdata(un);
624 struct usbnet_private * const unp = un->un_pri;
625
626 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
627 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
628
629 c->unc_un = un;
630 if (c->unc_xfer == NULL) {
631 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
632 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
633 &c->unc_xfer);
634 if (err)
635 return err;
636 c->unc_buf = usbd_get_buffer(c->unc_xfer);
637 }
638 }
639
640 return 0;
641 }
642
643 static void
644 usbnet_tx_list_fini(struct usbnet *un)
645 {
646 struct usbnet_cdata * const cd = un_cdata(un);
647
648 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
649 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
650
651 if (c->unc_xfer != NULL) {
652 usbd_destroy_xfer(c->unc_xfer);
653 c->unc_xfer = NULL;
654 c->unc_buf = NULL;
655 }
656 }
657 }
658
659 /* End of common TX functions */
660
661 /* Endpoint pipe management. */
662
663 static void
664 usbnet_ep_close_pipes(struct usbnet *un)
665 {
666 struct usbnet_private * const unp = un->un_pri;
667
668 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
669 if (unp->unp_ep[i] == NULL)
670 continue;
671 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
672 if (err)
673 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
674 usbd_errstr(err));
675 unp->unp_ep[i] = NULL;
676 }
677 }
678
679 static usbd_status
680 usbnet_ep_open_pipes(struct usbnet *un)
681 {
682 struct usbnet_intr * const uni = un->un_intr;
683 struct usbnet_private * const unp = un->un_pri;
684
685 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
686 usbd_status err;
687
688 if (un->un_ed[i] == 0)
689 continue;
690
691 if (i == USBNET_ENDPT_INTR && uni) {
692 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
693 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
694 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
695 uni->uni_interval);
696 } else {
697 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
698 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
699 }
700 if (err) {
701 usbnet_ep_close_pipes(un);
702 return err;
703 }
704 }
705
706 return USBD_NORMAL_COMPLETION;
707 }
708
709 static usbd_status
710 usbnet_ep_stop_pipes(struct usbnet *un)
711 {
712 struct usbnet_private * const unp = un->un_pri;
713
714 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
715 if (unp->unp_ep[i] == NULL)
716 continue;
717 usbd_status err = usbd_abort_pipe(unp->unp_ep[i]);
718 if (err)
719 return err;
720 }
721
722 return USBD_NORMAL_COMPLETION;
723 }
724
725 int
726 usbnet_init_rx_tx(struct usbnet * const un)
727 {
728 USBNETHIST_FUNC(); USBNETHIST_CALLED();
729 struct usbnet_private * const unp = un->un_pri;
730 struct ifnet * const ifp = usbnet_ifp(un);
731 usbd_status err;
732 int error = 0;
733
734 usbnet_isowned(un);
735
736 if (unp->unp_dying) {
737 return EIO;
738 }
739 unp->unp_refcnt++;
740
741 /* Open RX and TX pipes. */
742 err = usbnet_ep_open_pipes(un);
743 if (err) {
744 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
745 usbd_errstr(err));
746 error = EIO;
747 goto out;
748 }
749
750 /* Init RX ring. */
751 if (usbnet_rx_list_init(un)) {
752 aprint_error_dev(un->un_dev, "rx list init failed\n");
753 error = ENOBUFS;
754 goto out;
755 }
756
757 /* Init TX ring. */
758 if (usbnet_tx_list_init(un)) {
759 aprint_error_dev(un->un_dev, "tx list init failed\n");
760 error = ENOBUFS;
761 goto out;
762 }
763
764 /* Start up the receive pipe(s). */
765 usbnet_rx_start_pipes(un, usbnet_rxeof);
766
767 /* Indicate we are up and running. */
768 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
769 ifp->if_flags |= IFF_RUNNING;
770
771 callout_schedule(&unp->unp_stat_ch, hz);
772
773 out:
774 if (error) {
775 usbnet_rx_list_fini(un);
776 usbnet_tx_list_fini(un);
777 usbnet_ep_close_pipes(un);
778 }
779 if (--unp->unp_refcnt < 0)
780 cv_broadcast(&unp->unp_detachcv);
781
782 usbnet_isowned(un);
783
784 return error;
785 }
786
787 /* MII management. */
788
789 /*
790 * Access functions for MII. Take the MII lock to call access MII regs.
791 * Two forms: usbnet (softc) lock currently held or not.
792 */
793 void
794 usbnet_lock_mii(struct usbnet *un)
795 {
796 struct usbnet_private * const unp = un->un_pri;
797
798 mutex_enter(&unp->unp_lock);
799 unp->unp_refcnt++;
800 mutex_exit(&unp->unp_lock);
801
802 mutex_enter(&unp->unp_miilock);
803 }
804
805 void
806 usbnet_lock_mii_un_locked(struct usbnet *un)
807 {
808 struct usbnet_private * const unp = un->un_pri;
809
810 KASSERT(mutex_owned(&unp->unp_lock));
811
812 unp->unp_refcnt++;
813 mutex_enter(&unp->unp_miilock);
814 }
815
816 void
817 usbnet_unlock_mii(struct usbnet *un)
818 {
819 struct usbnet_private * const unp = un->un_pri;
820
821 mutex_exit(&unp->unp_miilock);
822 mutex_enter(&unp->unp_lock);
823 if (--unp->unp_refcnt < 0)
824 cv_broadcast(&unp->unp_detachcv);
825 mutex_exit(&unp->unp_lock);
826 }
827
828 void
829 usbnet_unlock_mii_un_locked(struct usbnet *un)
830 {
831 struct usbnet_private * const unp = un->un_pri;
832
833 KASSERT(mutex_owned(&unp->unp_lock));
834
835 mutex_exit(&unp->unp_miilock);
836 if (--unp->unp_refcnt < 0)
837 cv_broadcast(&unp->unp_detachcv);
838 }
839
840 kmutex_t *
841 usbnet_mutex_mii(struct usbnet *un)
842 {
843 struct usbnet_private * const unp = un->un_pri;
844
845 return &unp->unp_miilock;
846 }
847
848 int
849 usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
850 {
851 struct usbnet * const un = device_private(dev);
852 struct usbnet_private * const unp = un->un_pri;
853 usbd_status err;
854
855 mutex_enter(&unp->unp_lock);
856 if (unp->unp_dying || un->un_phyno != phy) {
857 mutex_exit(&unp->unp_lock);
858 return EIO;
859 }
860 mutex_exit(&unp->unp_lock);
861
862 usbnet_lock_mii(un);
863 err = uno_read_reg(un, phy, reg, val);
864 usbnet_unlock_mii(un);
865
866 if (err) {
867 aprint_error_dev(un->un_dev, "read PHY failed: %d\n", err);
868 return EIO;
869 }
870
871 return 0;
872 }
873
874 int
875 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
876 {
877 struct usbnet * const un = device_private(dev);
878 struct usbnet_private * const unp = un->un_pri;
879 usbd_status err;
880
881 mutex_enter(&unp->unp_lock);
882 if (unp->unp_dying || un->un_phyno != phy) {
883 mutex_exit(&unp->unp_lock);
884 return EIO;
885 }
886 mutex_exit(&unp->unp_lock);
887
888 usbnet_lock_mii(un);
889 err = uno_write_reg(un, phy, reg, val);
890 usbnet_unlock_mii(un);
891
892 if (err) {
893 aprint_error_dev(un->un_dev, "write PHY failed: %d\n", err);
894 return EIO;
895 }
896
897 return 0;
898 }
899
900 void
901 usbnet_mii_statchg(struct ifnet *ifp)
902 {
903 USBNETHIST_FUNC(); USBNETHIST_CALLED();
904 struct usbnet * const un = ifp->if_softc;
905
906 uno_mii_statchg(un, ifp);
907 }
908
909 static int
910 usbnet_media_upd(struct ifnet *ifp)
911 {
912 USBNETHIST_FUNC(); USBNETHIST_CALLED();
913 struct usbnet * const un = ifp->if_softc;
914 struct usbnet_private * const unp = un->un_pri;
915 struct mii_data * const mii = usbnet_mii(un);
916
917 if (unp->unp_dying)
918 return EIO;
919
920 unp->unp_link = false;
921
922 if (mii->mii_instance) {
923 struct mii_softc *miisc;
924
925 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
926 mii_phy_reset(miisc);
927 }
928
929 return ether_mediachange(ifp);
930 }
931
932 /* ioctl */
933
934 static int
935 usbnet_ifflags_cb(struct ethercom *ec)
936 {
937 USBNETHIST_FUNC(); USBNETHIST_CALLED();
938 struct ifnet *ifp = &ec->ec_if;
939 struct usbnet *un = ifp->if_softc;
940 struct usbnet_private * const unp = un->un_pri;
941 int rv = 0;
942
943 mutex_enter(&unp->unp_lock);
944
945 const int changed = ifp->if_flags ^ unp->unp_if_flags;
946 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
947 unp->unp_if_flags = ifp->if_flags;
948 if ((changed & IFF_PROMISC) != 0)
949 rv = ENETRESET;
950 } else {
951 rv = ENETRESET;
952 }
953
954 mutex_exit(&unp->unp_lock);
955
956 return rv;
957 }
958
959 static int
960 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
961 {
962 USBNETHIST_FUNC(); USBNETHIST_CALLED();
963 struct usbnet * const un = ifp->if_softc;
964 int error;
965
966 if (un->un_ops->uno_override_ioctl)
967 return uno_override_ioctl(un, ifp, cmd, data);
968
969 error = ether_ioctl(ifp, cmd, data);
970 if (error == ENETRESET)
971 error = uno_ioctl(un, ifp, cmd, data);
972
973 return error;
974 }
975
976 /*
977 * Generic stop network function:
978 * - mark as stopping
979 * - call DD routine to stop the device
980 * - turn off running, timer, statchg callout, link
981 * - stop transfers
982 * - free RX and TX resources
983 * - close pipes
984 *
985 * usbnet_stop() is exported for drivers to use, expects lock held.
986 *
987 * usbnet_stop_ifp() is for the if_stop handler.
988 */
989 void
990 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
991 {
992 struct usbnet_private * const unp = un->un_pri;
993
994 USBNETHIST_FUNC(); USBNETHIST_CALLED();
995
996 KASSERT(mutex_owned(&unp->unp_lock));
997
998 mutex_enter(&unp->unp_rxlock);
999 mutex_enter(&unp->unp_txlock);
1000 unp->unp_stopping = true;
1001 mutex_exit(&unp->unp_txlock);
1002 mutex_exit(&unp->unp_rxlock);
1003
1004 uno_stop(un, ifp, disable);
1005
1006 /*
1007 * XXXSMP Would like to
1008 * KASSERT(IFNET_LOCKED(ifp))
1009 * here but the locking order is:
1010 * ifnet -> unlock -> rxlock -> txlock
1011 * and unlock is already held.
1012 */
1013 ifp->if_flags &= ~IFF_RUNNING;
1014 unp->unp_timer = 0;
1015
1016 callout_stop(&unp->unp_stat_ch);
1017 unp->unp_link = false;
1018
1019 /* Stop transfers. */
1020 usbnet_ep_stop_pipes(un);
1021
1022 /* Free RX/TX resources. */
1023 usbnet_rx_list_fini(un);
1024 usbnet_tx_list_fini(un);
1025
1026 /* Close pipes. */
1027 usbnet_ep_close_pipes(un);
1028 }
1029
1030 static void
1031 usbnet_stop_ifp(struct ifnet *ifp, int disable)
1032 {
1033 struct usbnet * const un = ifp->if_softc;
1034 struct usbnet_private * const unp = un->un_pri;
1035
1036 mutex_enter(&unp->unp_lock);
1037 usbnet_stop(un, ifp, disable);
1038 mutex_exit(&unp->unp_lock);
1039 }
1040
1041 /*
1042 * Generic tick task function.
1043 *
1044 * usbnet_tick() is triggered from a callout, and triggers a call to
1045 * usbnet_tick_task() from the usb_task subsystem.
1046 */
1047 static void
1048 usbnet_tick(void *arg)
1049 {
1050 struct usbnet * const un = arg;
1051 struct usbnet_private * const unp = un->un_pri;
1052
1053 mutex_enter(&unp->unp_lock);
1054 if (!unp->unp_stopping && !unp->unp_dying) {
1055 /* Perform periodic stuff in process context */
1056 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1057 }
1058 mutex_exit(&unp->unp_lock);
1059 }
1060
1061 static void
1062 usbnet_watchdog(struct ifnet *ifp)
1063 {
1064 struct usbnet * const un = ifp->if_softc;
1065 struct usbnet_cdata * const cd = un_cdata(un);
1066 usbd_status stat;
1067
1068 ifp->if_oerrors++;
1069 aprint_error_dev(un->un_dev, "watchdog timeout\n");
1070
1071 if (cd->uncd_tx_cnt > 0) {
1072 /*
1073 * XXX index 0
1074 */
1075 struct usbnet_chain *c = &un_cdata(un)->uncd_tx_chain[0];
1076 usbd_get_xfer_status(c->unc_xfer, NULL, NULL, NULL, &stat);
1077 usbnet_txeof(c->unc_xfer, c, stat);
1078 }
1079
1080 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1081 (*ifp->if_start)(ifp);
1082 }
1083
1084 static void
1085 usbnet_tick_task(void *arg)
1086 {
1087 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1088 struct usbnet * const un = arg;
1089 struct usbnet_private * const unp = un->un_pri;
1090
1091 mutex_enter(&unp->unp_lock);
1092 if (unp->unp_stopping || unp->unp_dying) {
1093 mutex_exit(&unp->unp_lock);
1094 return;
1095 }
1096
1097 struct ifnet * const ifp = usbnet_ifp(un);
1098 struct mii_data * const mii = usbnet_mii(un);
1099
1100 unp->unp_refcnt++;
1101 mutex_exit(&unp->unp_lock);
1102
1103 if (ifp && unp->unp_timer != 0 && --unp->unp_timer == 0)
1104 usbnet_watchdog(ifp);
1105
1106 if (mii && ifp) {
1107 mii_tick(mii);
1108
1109 if (!unp->unp_link)
1110 (*mii->mii_statchg)(ifp);
1111 }
1112
1113 mutex_enter(&unp->unp_lock);
1114 if (--unp->unp_refcnt < 0)
1115 cv_broadcast(&unp->unp_detachcv);
1116 if (!unp->unp_stopping && !unp->unp_dying)
1117 callout_schedule(&unp->unp_stat_ch, hz);
1118 mutex_exit(&unp->unp_lock);
1119 }
1120
1121 static int
1122 usbnet_init(struct ifnet *ifp)
1123 {
1124 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1125 struct usbnet * const un = ifp->if_softc;
1126
1127 return uno_init(un, ifp);
1128 }
1129
1130 /* Various accessors. */
1131
1132 void
1133 usbnet_set_link(struct usbnet *un, bool link)
1134 {
1135 un->un_pri->unp_link = link;
1136 }
1137
1138 struct ifnet *
1139 usbnet_ifp(struct usbnet *un)
1140 {
1141 return &un->un_pri->unp_ec.ec_if;
1142 }
1143
1144 struct ethercom *
1145 usbnet_ec(struct usbnet *un)
1146 {
1147 return &un->un_pri->unp_ec;
1148 }
1149
1150 struct mii_data *
1151 usbnet_mii(struct usbnet *un)
1152 {
1153 return un->un_pri->unp_ec.ec_mii;
1154 }
1155
1156 krndsource_t *
1157 usbnet_rndsrc(struct usbnet *un)
1158 {
1159 return &un->un_pri->unp_rndsrc;
1160 }
1161
1162 void *
1163 usbnet_softc(struct usbnet *un)
1164 {
1165 //return un->un_pri->unp_sc;
1166 return un->un_sc;
1167 }
1168
1169 bool
1170 usbnet_havelink(struct usbnet *un)
1171 {
1172 return un->un_pri->unp_link;
1173 }
1174
1175 bool
1176 usbnet_isdying(struct usbnet *un)
1177 {
1178 return un->un_pri->unp_dying;
1179 }
1180
1181
1182 /* Locking. */
1183
1184 void
1185 usbnet_lock(struct usbnet *un)
1186 {
1187 mutex_enter(&un->un_pri->unp_lock);
1188 }
1189
1190 void
1191 usbnet_unlock(struct usbnet *un)
1192 {
1193 mutex_exit(&un->un_pri->unp_lock);
1194 }
1195
1196 kmutex_t *
1197 usbnet_mutex(struct usbnet *un)
1198 {
1199 return &un->un_pri->unp_lock;
1200 }
1201
1202 void
1203 usbnet_lock_rx(struct usbnet *un)
1204 {
1205 mutex_enter(&un->un_pri->unp_rxlock);
1206 }
1207
1208 void
1209 usbnet_unlock_rx(struct usbnet *un)
1210 {
1211 mutex_exit(&un->un_pri->unp_rxlock);
1212 }
1213
1214 kmutex_t *
1215 usbnet_mutex_rx(struct usbnet *un)
1216 {
1217 return &un->un_pri->unp_rxlock;
1218 }
1219
1220 void
1221 usbnet_lock_tx(struct usbnet *un)
1222 {
1223 mutex_enter(&un->un_pri->unp_txlock);
1224 }
1225
1226 void
1227 usbnet_unlock_tx(struct usbnet *un)
1228 {
1229 mutex_exit(&un->un_pri->unp_txlock);
1230 }
1231
1232 kmutex_t *
1233 usbnet_mutex_tx(struct usbnet *un)
1234 {
1235 return &un->un_pri->unp_txlock;
1236 }
1237
1238 /* Autoconf management. */
1239
1240 static bool
1241 usbnet_empty_eaddr(struct usbnet *un)
1242 {
1243 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
1244 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
1245 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
1246 }
1247
1248 /*
1249 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
1250 * 'usbnet'. The first is enough to enable device access (eg, endpoints
1251 * are connected and commands can be sent), and the second connects the
1252 * device to the system networking.
1253 *
1254 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
1255 * Also usable as driver detach directly.
1256 *
1257 * To skip ethernet configuration (eg, point-to-point), make sure that
1258 * the un_eaddr[] is fully zero.
1259 */
1260
1261 void
1262 usbnet_attach(struct usbnet *un,
1263 const char *detname) /* detach cv name */
1264 {
1265 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1266
1267 /* Required inputs. */
1268 KASSERT(un->un_ops->uno_tx_prepare);
1269 KASSERT(un->un_ops->uno_rx_loop);
1270 KASSERT(un->un_ops->uno_init);
1271 KASSERT(un->un_rx_bufsz);
1272 KASSERT(un->un_tx_bufsz);
1273 KASSERT(un->un_rx_list_cnt);
1274 KASSERT(un->un_tx_list_cnt);
1275
1276 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
1277 struct usbnet_private * const unp = un->un_pri;
1278
1279 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
1280 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
1281 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
1282
1283 mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE);
1284 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1285 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1286 mutex_init(&unp->unp_lock, MUTEX_DEFAULT, IPL_NONE);
1287 cv_init(&unp->unp_detachcv, detname);
1288
1289 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
1290 RND_TYPE_NET, RND_FLAG_DEFAULT);
1291
1292 usbnet_rx_list_alloc(un);
1293 usbnet_tx_list_alloc(un);
1294
1295 unp->unp_attached = true;
1296 }
1297
1298 static void
1299 usbnet_attach_mii(struct usbnet *un, int mii_flags)
1300 {
1301 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1302 struct usbnet_private * const unp = un->un_pri;
1303 struct mii_data * const mii = &unp->unp_mii;
1304 struct ifnet * const ifp = usbnet_ifp(un);
1305
1306 KASSERT(un->un_ops->uno_read_reg);
1307 KASSERT(un->un_ops->uno_write_reg);
1308 KASSERT(un->un_ops->uno_statchg);
1309
1310 mii->mii_ifp = ifp;
1311 mii->mii_readreg = usbnet_mii_readreg;
1312 mii->mii_writereg = usbnet_mii_writereg;
1313 mii->mii_statchg = usbnet_mii_statchg;
1314 mii->mii_flags = MIIF_AUTOTSLEEP;
1315
1316 usbnet_ec(un)->ec_mii = mii;
1317 ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
1318 mii_attach(un->un_dev, mii, 0xffffffff, MII_PHY_ANY,
1319 MII_OFFSET_ANY, mii_flags);
1320
1321 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1322 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1323 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1324 } else
1325 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1326 }
1327
1328 void
1329 usbnet_attach_ifp(struct usbnet *un,
1330 bool have_mii, /* setup MII */
1331 unsigned if_flags, /* additional if_flags */
1332 unsigned if_extflags, /* additional if_extflags */
1333 int mii_flags) /* additional mii_attach flags */
1334 {
1335 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1336 struct usbnet_private * const unp = un->un_pri;
1337 struct ifnet * const ifp = usbnet_ifp(un);
1338
1339 KASSERT(unp->unp_attached);
1340
1341 IFQ_SET_READY(&ifp->if_snd);
1342
1343 ifp->if_softc = un;
1344 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1345 ifp->if_flags = if_flags;
1346 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1347 ifp->if_ioctl = usbnet_ioctl;
1348 ifp->if_start = usbnet_start;
1349 ifp->if_init = usbnet_init;
1350 ifp->if_stop = usbnet_stop_ifp;
1351
1352 IFQ_SET_READY(&ifp->if_snd);
1353
1354 if (have_mii)
1355 usbnet_attach_mii(un, mii_flags);
1356 else
1357 unp->unp_link = true;
1358
1359 /* Attach the interface. */
1360 if_attach(ifp);
1361
1362 /*
1363 * If ethernet address is all zero, skip ether_ifattach() and
1364 * instead attach bpf here..
1365 */
1366 if (!usbnet_empty_eaddr(un)) {
1367 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
1368 aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1369 ether_sprintf(un->un_eaddr));
1370 ether_ifattach(ifp, un->un_eaddr);
1371 } else {
1372 if_alloc_sadl(ifp);
1373 bpf_attach(ifp, DLT_RAW, 0);
1374 }
1375
1376 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1377
1378 if (!pmf_device_register(un->un_dev, NULL, NULL))
1379 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1380 }
1381
1382 int
1383 usbnet_detach(device_t self, int flags)
1384 {
1385 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1386 struct usbnet * const un = device_private(self);
1387 struct ifnet * const ifp = usbnet_ifp(un);
1388 struct mii_data * const mii = usbnet_mii(un);
1389 struct usbnet_private * const unp = un->un_pri;
1390
1391 mutex_enter(&unp->unp_lock);
1392 unp->unp_dying = true;
1393 mutex_exit(&unp->unp_lock);
1394
1395 /* Detached before attached finished, so just bail out. */
1396 if (!unp->unp_attached)
1397 return 0;
1398
1399 callout_halt(&unp->unp_stat_ch, NULL);
1400 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL);
1401
1402 if (ifp->if_flags & IFF_RUNNING) {
1403 IFNET_LOCK(ifp);
1404 usbnet_stop_ifp(ifp, 1);
1405 IFNET_UNLOCK(ifp);
1406 }
1407
1408 mutex_enter(&unp->unp_lock);
1409 unp->unp_refcnt--;
1410 while (unp->unp_refcnt > 0) {
1411 /* Wait for processes to go away */
1412 cv_wait(&unp->unp_detachcv, &unp->unp_lock);
1413 }
1414 mutex_exit(&unp->unp_lock);
1415
1416 usbnet_rx_list_free(un);
1417 usbnet_tx_list_free(un);
1418
1419 callout_destroy(&unp->unp_stat_ch);
1420 rnd_detach_source(&unp->unp_rndsrc);
1421
1422 if (mii) {
1423 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1424 ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
1425 }
1426 if (ifp->if_softc) {
1427 if (!usbnet_empty_eaddr(un))
1428 ether_ifdetach(ifp);
1429 else
1430 bpf_detach(ifp);
1431 if_detach(ifp);
1432 }
1433
1434 cv_destroy(&unp->unp_detachcv);
1435 mutex_destroy(&unp->unp_lock);
1436 mutex_destroy(&unp->unp_rxlock);
1437 mutex_destroy(&unp->unp_txlock);
1438 mutex_destroy(&unp->unp_miilock);
1439
1440 pmf_device_deregister(un->un_dev);
1441
1442 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1443
1444 kmem_free(unp, sizeof(*unp));
1445
1446 return 0;
1447 }
1448
1449 int
1450 usbnet_activate(device_t self, devact_t act)
1451 {
1452 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1453 struct usbnet * const un = device_private(self);
1454 struct usbnet_private * const unp = un->un_pri;
1455 struct ifnet * const ifp = usbnet_ifp(un);
1456
1457 switch (act) {
1458 case DVACT_DEACTIVATE:
1459 if_deactivate(ifp);
1460
1461 mutex_enter(&unp->unp_lock);
1462 unp->unp_dying = true;
1463 mutex_exit(&unp->unp_lock);
1464
1465 mutex_enter(&unp->unp_rxlock);
1466 mutex_enter(&unp->unp_txlock);
1467 unp->unp_stopping = true;
1468 mutex_exit(&unp->unp_txlock);
1469 mutex_exit(&unp->unp_rxlock);
1470
1471 return 0;
1472 default:
1473 return EOPNOTSUPP;
1474 }
1475 }
1476
1477 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1478
1479 static int
1480 usbnet_modcmd(modcmd_t cmd, void *arg)
1481 {
1482 switch (cmd) {
1483 case MODULE_CMD_INIT:
1484 return 0;
1485 case MODULE_CMD_FINI:
1486 return 0;
1487 case MODULE_CMD_STAT:
1488 case MODULE_CMD_AUTOUNLOAD:
1489 default:
1490 return ENOTTY;
1491 }
1492 }
1493