if_upl.c revision 1.64 1 /* $NetBSD: if_upl.c,v 1.64 2019/07/21 10:27:56 mrg Exp $ */
2 /*
3 * Copyright (c) 2000 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Lennart Augustsson (lennart (at) augustsson.net) at
8 * Carlstedt Research & Technology.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Prolific PL2301/PL2302 driver
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1.64 2019/07/21 10:27:56 mrg Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_inet.h"
41 #include "opt_usb.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51
52 #include <sys/device.h>
53 #include <sys/rndsource.h>
54
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/netisr.h>
59
60 #include <net/bpf.h>
61
62 #ifdef INET
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/if_inarp.h>
66 #endif
67
68
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 #include <dev/usb/usbdevs.h>
73
74 /*
75 * 7 6 5 4 3 2 1 0
76 * tx rx 1 0
77 * 1110 0000 rxdata
78 * 1010 0000 idle
79 * 0010 0000 tx over
80 * 0110 tx over + rxd
81 */
82
83 #define UPL_RXDATA 0x40
84 #define UPL_TXOK 0x80
85
86 #define UPL_INTR_PKTLEN 1
87
88 #define UPL_CONFIG_NO 1
89 #define UPL_IFACE_IDX 0
90
91 /***/
92
93 #define UPL_INTR_INTERVAL 20
94
95 #define UPL_BUFSZ 1024
96
97 #define UPL_RX_FRAMES 1
98 #define UPL_TX_FRAMES 1
99
100 #define UPL_RX_LIST_CNT 1
101 #define UPL_TX_LIST_CNT 1
102
103 #define UPL_ENDPT_RX 0x0
104 #define UPL_ENDPT_TX 0x1
105 #define UPL_ENDPT_INTR 0x2
106 #define UPL_ENDPT_MAX 0x3
107
108 struct upl_type {
109 uint16_t upl_vid;
110 uint16_t upl_did;
111 };
112
113 struct upl_softc;
114
115 struct upl_chain {
116 struct upl_softc *upl_sc;
117 struct usbd_xfer *upl_xfer;
118 char *upl_buf;
119 struct mbuf *upl_mbuf;
120 };
121
122 struct upl_cdata {
123 struct upl_chain upl_tx_chain[UPL_TX_LIST_CNT];
124 struct upl_chain upl_rx_chain[UPL_RX_LIST_CNT];
125 int upl_tx_prod;
126 int upl_tx_cnt;
127 };
128
129 struct upl_softc {
130 device_t sc_dev;
131
132 struct ifnet sc_if;
133 krndsource_t sc_rnd_source;
134
135 struct callout sc_stat_ch;
136
137 struct usbd_device * sc_udev;
138 struct usbd_interface * sc_iface;
139 uint16_t sc_vendor;
140 uint16_t sc_product;
141 int sc_ed[UPL_ENDPT_MAX];
142 struct usbd_pipe * sc_ep[UPL_ENDPT_MAX];
143 struct upl_cdata sc_cdata;
144
145 uByte sc_ibuf;
146
147 char sc_dying;
148 char sc_attached;
149 u_int sc_rx_errs;
150 struct timeval sc_rx_notice;
151 u_int sc_intr_errs;
152 };
153
154 #ifdef UPL_DEBUG
155 #define DPRINTF(x) if (upldebug) printf x
156 #define DPRINTFN(n,x) if (upldebug >= (n)) printf x
157 int upldebug = 0;
158 #else
159 #define DPRINTF(x)
160 #define DPRINTFN(n,x)
161 #endif
162
163 /*
164 * Various supported device vendors/products.
165 */
166 Static struct upl_type sc_devs[] = {
167 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
168 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 },
169 { 0, 0 }
170 };
171
172 int upl_match(device_t, cfdata_t, void *);
173 void upl_attach(device_t, device_t, void *);
174 int upl_detach(device_t, int);
175 int upl_activate(device_t, enum devact);
176
177 CFATTACH_DECL_NEW(upl, sizeof(struct upl_softc), upl_match, upl_attach,
178 upl_detach, upl_activate);
179
180 Static int upl_openpipes(struct upl_softc *);
181 Static int upl_tx_list_init(struct upl_softc *);
182 Static int upl_rx_list_init(struct upl_softc *);
183 Static int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *);
184 Static int upl_send(struct upl_softc *, struct mbuf *, int);
185 Static void upl_intr(struct usbd_xfer *, void *, usbd_status);
186 Static void upl_rxeof(struct usbd_xfer *, void *, usbd_status);
187 Static void upl_txeof(struct usbd_xfer *, void *, usbd_status);
188 Static void upl_start(struct ifnet *);
189 Static int upl_ioctl(struct ifnet *, u_long, void *);
190 Static void upl_init(void *);
191 Static void upl_stop(struct upl_softc *);
192 Static void upl_watchdog(struct ifnet *);
193
194 Static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
195 const struct rtentry *);
196 Static void upl_input(struct ifnet *, struct mbuf *);
197
198 /*
199 * Probe for a Prolific chip.
200 */
201 int
202 upl_match(device_t parent, cfdata_t match, void *aux)
203 {
204 struct usb_attach_arg *uaa = aux;
205 struct upl_type *t;
206
207 for (t = sc_devs; t->upl_vid != 0; t++)
208 if (uaa->uaa_vendor == t->upl_vid && uaa->uaa_product == t->upl_did)
209 return UMATCH_VENDOR_PRODUCT;
210
211 return UMATCH_NONE;
212 }
213
214 void
215 upl_attach(device_t parent, device_t self, void *aux)
216 {
217 struct upl_softc *sc = device_private(self);
218 struct usb_attach_arg *uaa = aux;
219 char *devinfop;
220 int s;
221 struct usbd_device * dev = uaa->uaa_device;
222 struct usbd_interface * iface;
223 usbd_status err;
224 struct ifnet *ifp;
225 usb_interface_descriptor_t *id;
226 usb_endpoint_descriptor_t *ed;
227 int i;
228 int rv;
229
230 DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev));
231
232 sc->sc_dev = self;
233
234 aprint_naive("\n");
235 aprint_normal("\n");
236
237 devinfop = usbd_devinfo_alloc(dev, 0);
238 aprint_normal_dev(self, "%s\n", devinfop);
239 usbd_devinfo_free(devinfop);
240
241 err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1);
242 if (err) {
243 aprint_error_dev(self, "failed to set configuration"
244 ", err=%s\n", usbd_errstr(err));
245 return;
246 }
247
248 sc->sc_udev = dev;
249 sc->sc_product = uaa->uaa_product;
250 sc->sc_vendor = uaa->uaa_vendor;
251
252 err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface);
253 if (err) {
254 aprint_error_dev(self, "getting interface handle failed\n");
255 return;
256 }
257
258 sc->sc_iface = iface;
259 id = usbd_get_interface_descriptor(iface);
260
261 /* Find endpoints. */
262 for (i = 0; i < id->bNumEndpoints; i++) {
263 ed = usbd_interface2endpoint_descriptor(iface, i);
264 if (ed == NULL) {
265 aprint_error_dev(self, "couldn't get ep %d\n", i);
266 return;
267 }
268 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
269 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
270 sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress;
271 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
272 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
273 sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress;
274 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
275 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
276 sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress;
277 }
278 }
279
280 if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 ||
281 sc->sc_ed[UPL_ENDPT_INTR] == 0) {
282 aprint_error_dev(self, "missing endpoint\n");
283 return;
284 }
285
286 s = splnet();
287
288 /* Initialize interface info.*/
289 ifp = &sc->sc_if;
290 ifp->if_softc = sc;
291 ifp->if_mtu = UPL_BUFSZ;
292 ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX;
293 ifp->if_ioctl = upl_ioctl;
294 ifp->if_start = upl_start;
295 ifp->if_watchdog = upl_watchdog;
296 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
297
298 ifp->if_type = IFT_OTHER;
299 ifp->if_addrlen = 0;
300 ifp->if_hdrlen = 0;
301 ifp->if_output = upl_output;
302 ifp->_if_input = upl_input;
303 ifp->if_baudrate = 12000000;
304 ifp->if_dlt = DLT_RAW;
305 IFQ_SET_READY(&ifp->if_snd);
306
307 /* Attach the interface. */
308 rv = if_initialize(ifp);
309 if (rv != 0) {
310 aprint_error_dev(self, "if_initialize failed(%d)\n", rv);
311 splx(s);
312 return;
313 }
314 if_register(ifp);
315 if_alloc_sadl(ifp);
316
317 bpf_attach(ifp, DLT_RAW, 0);
318 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
319 RND_TYPE_NET, RND_FLAG_DEFAULT);
320
321 sc->sc_attached = 1;
322 splx(s);
323
324 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
325
326 return;
327 }
328
329 int
330 upl_detach(device_t self, int flags)
331 {
332 struct upl_softc *sc = device_private(self);
333 struct ifnet *ifp = &sc->sc_if;
334 int s;
335
336 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
337
338 s = splusb();
339
340 if (!sc->sc_attached) {
341 /* Detached before attached finished, so just bail out. */
342 splx(s);
343 return 0;
344 }
345
346 if (ifp->if_flags & IFF_RUNNING)
347 upl_stop(sc);
348
349 rnd_detach_source(&sc->sc_rnd_source);
350 bpf_detach(ifp);
351
352 if_detach(ifp);
353
354 #ifdef DIAGNOSTIC
355 if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
356 sc->sc_ep[UPL_ENDPT_RX] != NULL ||
357 sc->sc_ep[UPL_ENDPT_INTR] != NULL)
358 aprint_debug_dev(self, "detach has active endpoints\n");
359 #endif
360
361 sc->sc_attached = 0;
362 splx(s);
363
364 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
365
366 return 0;
367 }
368
369 int
370 upl_activate(device_t self, enum devact act)
371 {
372 struct upl_softc *sc = device_private(self);
373
374 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
375
376 switch (act) {
377 case DVACT_DEACTIVATE:
378 /* Deactivate the interface. */
379 if_deactivate(&sc->sc_if);
380 sc->sc_dying = 1;
381 return 0;
382 default:
383 return EOPNOTSUPP;
384 }
385 }
386
387 /*
388 * Initialize an RX descriptor and attach an MBUF cluster.
389 */
390 Static int
391 upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m)
392 {
393 struct mbuf *m_new = NULL;
394
395 DPRINTFN(8,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
396
397 if (m == NULL) {
398 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
399 if (m_new == NULL) {
400 printf("%s: no memory for rx list "
401 "-- packet dropped!\n", device_xname(sc->sc_dev));
402 return ENOBUFS;
403 }
404
405 MCLGET(m_new, M_DONTWAIT);
406 if (!(m_new->m_flags & M_EXT)) {
407 printf("%s: no memory for rx list "
408 "-- packet dropped!\n", device_xname(sc->sc_dev));
409 m_freem(m_new);
410 return ENOBUFS;
411 }
412 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
413 } else {
414 m_new = m;
415 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
416 m_new->m_data = m_new->m_ext.ext_buf;
417 }
418
419 c->upl_mbuf = m_new;
420
421 return 0;
422 }
423
424 Static int
425 upl_rx_list_init(struct upl_softc *sc)
426 {
427 struct upl_cdata *cd;
428 struct upl_chain *c;
429 int i;
430
431 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
432
433 cd = &sc->sc_cdata;
434 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
435 c = &cd->upl_rx_chain[i];
436 c->upl_sc = sc;
437 if (upl_newbuf(sc, c, NULL) == ENOBUFS)
438 return ENOBUFS;
439 if (c->upl_xfer == NULL) {
440 int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_RX],
441 UPL_BUFSZ, 0, 0, &c->upl_xfer);
442 if (error)
443 return error;
444 c->upl_buf = usbd_get_buffer(c->upl_xfer);
445 }
446 }
447
448 return 0;
449 }
450
451 Static int
452 upl_tx_list_init(struct upl_softc *sc)
453 {
454 struct upl_cdata *cd;
455 struct upl_chain *c;
456 int i;
457
458 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
459
460 cd = &sc->sc_cdata;
461 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
462 c = &cd->upl_tx_chain[i];
463 c->upl_sc = sc;
464 c->upl_mbuf = NULL;
465 if (c->upl_xfer == NULL) {
466 int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_TX],
467 UPL_BUFSZ, 0, 0, &c->upl_xfer);
468 if (error)
469 return error;
470 c->upl_buf = usbd_get_buffer(c->upl_xfer);
471 }
472 }
473
474 return 0;
475 }
476
477 /*
478 * A frame has been uploaded: pass the resulting mbuf chain up to
479 * the higher level protocols.
480 */
481 Static void
482 upl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
483 {
484 struct upl_chain *c = priv;
485 struct upl_softc *sc = c->upl_sc;
486 struct ifnet *ifp = &sc->sc_if;
487 struct mbuf *m;
488 int total_len = 0;
489 int s;
490
491 if (sc->sc_dying)
492 return;
493
494 if (!(ifp->if_flags & IFF_RUNNING))
495 return;
496
497 if (status != USBD_NORMAL_COMPLETION) {
498 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
499 return;
500 sc->sc_rx_errs++;
501 if (usbd_ratecheck(&sc->sc_rx_notice)) {
502 printf("%s: %u usb errors on rx: %s\n",
503 device_xname(sc->sc_dev), sc->sc_rx_errs,
504 usbd_errstr(status));
505 sc->sc_rx_errs = 0;
506 }
507 if (status == USBD_STALLED)
508 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
509 goto done;
510 }
511
512 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
513
514 DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
515 device_xname(sc->sc_dev), __func__, status, total_len));
516
517 m = c->upl_mbuf;
518 memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
519
520 m->m_pkthdr.len = m->m_len = total_len;
521
522 m_set_rcvif(m, ifp);
523
524 s = splnet();
525
526 /* XXX ugly */
527 if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
528 ifp->if_ierrors++;
529 goto done1;
530 }
531
532 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
533 __func__, m->m_len));
534
535 if_input((ifp), (m));
536
537 done1:
538 splx(s);
539
540 done:
541 #if 1
542 /* Setup new transfer. */
543 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ,
544 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upl_rxeof);
545 usbd_transfer(c->upl_xfer);
546
547 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->sc_dev),
548 __func__));
549 #endif
550 }
551
552 /*
553 * A frame was downloaded to the chip. It's safe for us to clean up
554 * the list buffers.
555 */
556 Static void
557 upl_txeof(struct usbd_xfer *xfer, void *priv,
558 usbd_status status)
559 {
560 struct upl_chain *c = priv;
561 struct upl_softc *sc = c->upl_sc;
562 struct ifnet *ifp = &sc->sc_if;
563 int s;
564
565 if (sc->sc_dying)
566 return;
567
568 s = splnet();
569
570 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->sc_dev),
571 __func__, status));
572
573 ifp->if_timer = 0;
574 ifp->if_flags &= ~IFF_OACTIVE;
575
576 if (status != USBD_NORMAL_COMPLETION) {
577 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
578 splx(s);
579 return;
580 }
581 ifp->if_oerrors++;
582 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
583 usbd_errstr(status));
584 if (status == USBD_STALLED)
585 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]);
586 splx(s);
587 return;
588 }
589
590 ifp->if_opackets++;
591
592 m_freem(c->upl_mbuf);
593 c->upl_mbuf = NULL;
594
595 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
596 upl_start(ifp);
597
598 splx(s);
599 }
600
601 Static int
602 upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
603 {
604 int total_len;
605 struct upl_chain *c;
606 usbd_status err;
607
608 c = &sc->sc_cdata.upl_tx_chain[idx];
609
610 /*
611 * Copy the mbuf data into a contiguous buffer, leaving two
612 * bytes at the beginning to hold the frame length.
613 */
614 m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
615 c->upl_mbuf = m;
616
617 total_len = m->m_pkthdr.len;
618
619 DPRINTFN(10,("%s: %s: total_len=%d\n",
620 device_xname(sc->sc_dev), __func__, total_len));
621
622 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, total_len, 0,
623 USBD_DEFAULT_TIMEOUT, upl_txeof);
624
625 /* Transmit */
626 err = usbd_transfer(c->upl_xfer);
627 if (err != USBD_IN_PROGRESS) {
628 printf("%s: upl_send error=%s\n", device_xname(sc->sc_dev),
629 usbd_errstr(err));
630 upl_stop(sc);
631 return EIO;
632 }
633
634 sc->sc_cdata.upl_tx_cnt++;
635
636 return 0;
637 }
638
639 Static void
640 upl_start(struct ifnet *ifp)
641 {
642 struct upl_softc *sc = ifp->if_softc;
643 struct mbuf *m_head = NULL;
644
645 if (sc->sc_dying)
646 return;
647
648 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
649
650 if (ifp->if_flags & IFF_OACTIVE)
651 return;
652
653 IFQ_POLL(&ifp->if_snd, m_head);
654 if (m_head == NULL)
655 return;
656
657 if (upl_send(sc, m_head, 0)) {
658 ifp->if_flags |= IFF_OACTIVE;
659 return;
660 }
661
662 IFQ_DEQUEUE(&ifp->if_snd, m_head);
663
664 /*
665 * If there's a BPF listener, bounce a copy of this frame
666 * to him.
667 */
668 bpf_mtap(ifp, m_head, BPF_D_OUT);
669
670 ifp->if_flags |= IFF_OACTIVE;
671
672 /*
673 * Set a timeout in case the chip goes out to lunch.
674 */
675 ifp->if_timer = 5;
676 }
677
678 Static void
679 upl_init(void *xsc)
680 {
681 struct upl_softc *sc = xsc;
682 struct ifnet *ifp = &sc->sc_if;
683 int s;
684
685 if (sc->sc_dying)
686 return;
687
688 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
689
690 if (ifp->if_flags & IFF_RUNNING)
691 return;
692
693 s = splnet();
694
695 if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
696 if (upl_openpipes(sc)) {
697 splx(s);
698 return;
699 }
700 }
701 /* Init TX ring. */
702 if (upl_tx_list_init(sc)) {
703 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
704 splx(s);
705 return;
706 }
707
708 /* Init RX ring. */
709 if (upl_rx_list_init(sc)) {
710 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
711 splx(s);
712 return;
713 }
714
715 /* Start up the receive pipe. */
716 for (int i = 0; i < UPL_RX_LIST_CNT; i++) {
717 struct upl_chain *c = &sc->sc_cdata.upl_rx_chain[i];
718 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ,
719 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
720 upl_rxeof);
721 usbd_transfer(c->upl_xfer);
722 }
723
724 ifp->if_flags |= IFF_RUNNING;
725 ifp->if_flags &= ~IFF_OACTIVE;
726
727 splx(s);
728 }
729
730 Static int
731 upl_openpipes(struct upl_softc *sc)
732 {
733 usbd_status err;
734
735 /* Open RX and TX pipes. */
736 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
737 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
738 if (err) {
739 printf("%s: open rx pipe failed: %s\n",
740 device_xname(sc->sc_dev), usbd_errstr(err));
741 return EIO;
742 }
743 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
744 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
745 if (err) {
746 printf("%s: open tx pipe failed: %s\n",
747 device_xname(sc->sc_dev), usbd_errstr(err));
748 return EIO;
749 }
750 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
751 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
752 &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
753 UPL_INTR_INTERVAL);
754 if (err) {
755 printf("%s: open intr pipe failed: %s\n",
756 device_xname(sc->sc_dev), usbd_errstr(err));
757 return EIO;
758 }
759
760 return 0;
761 }
762
763 Static void
764 upl_intr(struct usbd_xfer *xfer, void *priv,
765 usbd_status status)
766 {
767 struct upl_softc *sc = priv;
768 struct ifnet *ifp = &sc->sc_if;
769 uByte stat;
770
771 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
772
773 if (sc->sc_dying)
774 return;
775
776 if (!(ifp->if_flags & IFF_RUNNING))
777 return;
778
779 if (status != USBD_NORMAL_COMPLETION) {
780 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
781 return;
782 }
783 sc->sc_intr_errs++;
784 if (usbd_ratecheck(&sc->sc_rx_notice)) {
785 printf("%s: %u usb errors on intr: %s\n",
786 device_xname(sc->sc_dev), sc->sc_rx_errs,
787 usbd_errstr(status));
788 sc->sc_intr_errs = 0;
789 }
790 if (status == USBD_STALLED)
791 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
792 return;
793 }
794
795 stat = sc->sc_ibuf;
796
797 if (stat == 0)
798 return;
799
800 DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(sc->sc_dev),
801 __func__, stat));
802
803 }
804
805 Static int
806 upl_ioctl(struct ifnet *ifp, u_long command, void *data)
807 {
808 struct upl_softc *sc = ifp->if_softc;
809 struct ifaddr *ifa = (struct ifaddr *)data;
810 struct ifreq *ifr = (struct ifreq *)data;
811 int s, error = 0;
812
813 if (sc->sc_dying)
814 return EIO;
815
816 DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
817 device_xname(sc->sc_dev), __func__, command));
818
819 s = splnet();
820
821 switch(command) {
822 case SIOCINITIFADDR:
823 ifp->if_flags |= IFF_UP;
824 upl_init(sc);
825
826 switch (ifa->ifa_addr->sa_family) {
827 #ifdef INET
828 case AF_INET:
829 break;
830 #endif /* INET */
831 }
832 break;
833
834 case SIOCSIFMTU:
835 if (ifr->ifr_mtu > UPL_BUFSZ)
836 error = EINVAL;
837 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
838 error = 0;
839 break;
840
841 case SIOCSIFFLAGS:
842 if ((error = ifioctl_common(ifp, command, data)) != 0)
843 break;
844 /* XXX re-use ether_ioctl() */
845 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
846 case IFF_UP:
847 upl_init(sc);
848 break;
849 case IFF_RUNNING:
850 upl_stop(sc);
851 break;
852 default:
853 break;
854 }
855 break;
856 default:
857 error = ifioctl_common(ifp, command, data);
858 break;
859 }
860
861 splx(s);
862
863 return error;
864 }
865
866 Static void
867 upl_watchdog(struct ifnet *ifp)
868 {
869 struct upl_softc *sc = ifp->if_softc;
870
871 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
872
873 if (sc->sc_dying)
874 return;
875
876 ifp->if_oerrors++;
877 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
878
879 upl_stop(sc);
880 upl_init(sc);
881
882 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
883 upl_start(ifp);
884 }
885
886 /*
887 * Stop the adapter and free any mbufs allocated to the
888 * RX and TX lists.
889 */
890 Static void
891 upl_stop(struct upl_softc *sc)
892 {
893 usbd_status err;
894 struct ifnet *ifp;
895 int i;
896
897 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
898
899 ifp = &sc->sc_if;
900 ifp->if_timer = 0;
901
902 /* Stop transfers. */
903 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
904 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
905 if (err) {
906 printf("%s: abort rx pipe failed: %s\n",
907 device_xname(sc->sc_dev), usbd_errstr(err));
908 }
909 }
910
911 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
912 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
913 if (err) {
914 printf("%s: abort tx pipe failed: %s\n",
915 device_xname(sc->sc_dev), usbd_errstr(err));
916 }
917 }
918
919 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
920 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
921 if (err) {
922 printf("%s: abort intr pipe failed: %s\n",
923 device_xname(sc->sc_dev), usbd_errstr(err));
924 }
925 }
926
927 /* Free RX resources. */
928 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
929 if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
930 m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
931 sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
932 }
933 }
934
935 /* Free TX resources. */
936 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
937 if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
938 m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
939 sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
940 }
941 if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
942 usbd_destroy_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
943 sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
944 }
945 }
946
947 /* Close pipes */
948 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
949 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
950 if (err) {
951 printf("%s: close rx pipe failed: %s\n",
952 device_xname(sc->sc_dev), usbd_errstr(err));
953 }
954 sc->sc_ep[UPL_ENDPT_RX] = NULL;
955 }
956
957 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
958 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
959 if (err) {
960 printf("%s: close tx pipe failed: %s\n",
961 device_xname(sc->sc_dev), usbd_errstr(err));
962 }
963 sc->sc_ep[UPL_ENDPT_TX] = NULL;
964 }
965
966 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
967 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
968 if (err) {
969 printf("%s: close intr pipe failed: %s\n",
970 device_xname(sc->sc_dev), usbd_errstr(err));
971 }
972 sc->sc_ep[UPL_ENDPT_INTR] = NULL;
973 }
974
975 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
976 }
977
978 Static int
979 upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
980 const struct rtentry *rt0)
981 {
982 int error;
983
984 DPRINTFN(10,("%s: %s: enter\n",
985 device_xname(((struct upl_softc *)ifp->if_softc)->sc_dev),
986 __func__));
987
988 /*
989 * if the queueing discipline needs packet classification,
990 * do it now.
991 */
992 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
993
994 /*
995 * Queue message on interface, and start output if interface
996 * not yet active.
997 */
998 error = if_transmit_lock(ifp, m);
999
1000 return error;
1001 }
1002
1003 Static void
1004 upl_input(struct ifnet *ifp, struct mbuf *m)
1005 {
1006 #ifdef INET
1007 size_t pktlen = m->m_len;
1008 int s;
1009
1010 s = splnet();
1011 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
1012 ifp->if_iqdrops++;
1013 m_freem(m);
1014 } else {
1015 ifp->if_ipackets++;
1016 ifp->if_ibytes += pktlen;
1017 }
1018 splx(s);
1019 #endif
1020 }
1021