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