if_upl.c revision 1.50 1 /* $NetBSD: if_upl.c,v 1.50 2016/04/20 09:01:04 knakahara 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.50 2016/04/20 09:01:04 knakahara Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_inet.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/sockio.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.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 u_int16_t upl_vid;
110 u_int16_t upl_did;
111 };
112
113 struct upl_softc;
114
115 struct upl_chain {
116 struct upl_softc *upl_sc;
117 usbd_xfer_handle 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 usbd_device_handle sc_udev;
141 usbd_interface_handle sc_iface;
142 u_int16_t sc_vendor;
143 u_int16_t sc_product;
144 int sc_ed[UPL_ENDPT_MAX];
145 usbd_pipe_handle 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, upl_detach, upl_activate);
181
182 Static int upl_openpipes(struct upl_softc *);
183 Static int upl_tx_list_init(struct upl_softc *);
184 Static int upl_rx_list_init(struct upl_softc *);
185 Static int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *);
186 Static int upl_send(struct upl_softc *, struct mbuf *, int);
187 Static void upl_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
188 Static void upl_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
189 Static void upl_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
190 Static void upl_start(struct ifnet *);
191 Static int upl_ioctl(struct ifnet *, u_long, void *);
192 Static void upl_init(void *);
193 Static void upl_stop(struct upl_softc *);
194 Static void upl_watchdog(struct ifnet *);
195
196 Static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
197 struct rtentry *);
198 Static void upl_input(struct ifnet *, struct mbuf *);
199
200 /*
201 * Probe for a Prolific chip.
202 */
203 int
204 upl_match(device_t parent, cfdata_t match, void *aux)
205 {
206 struct usb_attach_arg *uaa = aux;
207 struct upl_type *t;
208
209 for (t = sc_devs; t->upl_vid != 0; t++)
210 if (uaa->vendor == t->upl_vid && uaa->product == t->upl_did)
211 return (UMATCH_VENDOR_PRODUCT);
212
213 return (UMATCH_NONE);
214 }
215
216 void
217 upl_attach(device_t parent, device_t self, void *aux)
218 {
219 struct upl_softc *sc = device_private(self);
220 struct usb_attach_arg *uaa = aux;
221 char *devinfop;
222 int s;
223 usbd_device_handle dev = uaa->device;
224 usbd_interface_handle 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->product;
251 sc->sc_vendor = 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 s = splnet();
288
289 /* Initialize interface info.*/
290 ifp = &sc->sc_if;
291 ifp->if_softc = sc;
292 ifp->if_mtu = UPL_BUFSZ;
293 ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX;
294 ifp->if_ioctl = upl_ioctl;
295 ifp->if_start = upl_start;
296 ifp->if_watchdog = upl_watchdog;
297 strncpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
298
299 ifp->if_type = IFT_OTHER;
300 ifp->if_addrlen = 0;
301 ifp->if_hdrlen = 0;
302 ifp->if_output = upl_output;
303 ifp->_if_input = upl_input;
304 ifp->if_baudrate = 12000000;
305 ifp->if_dlt = DLT_RAW;
306 IFQ_SET_READY(&ifp->if_snd);
307
308 /* Attach the interface. */
309 if_initialize(ifp);
310 if_register(ifp);
311 if_alloc_sadl(ifp);
312
313 bpf_attach(ifp, DLT_RAW, 0);
314 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
315 RND_TYPE_NET, RND_FLAG_DEFAULT);
316
317 sc->sc_attached = 1;
318 splx(s);
319
320 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
321 sc->sc_dev);
322
323 return;
324 }
325
326 int
327 upl_detach(device_t self, int flags)
328 {
329 struct upl_softc *sc = device_private(self);
330 struct ifnet *ifp = &sc->sc_if;
331 int s;
332
333 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
334
335 s = splusb();
336
337 if (!sc->sc_attached) {
338 /* Detached before attached finished, so just bail out. */
339 splx(s);
340 return (0);
341 }
342
343 if (ifp->if_flags & IFF_RUNNING)
344 upl_stop(sc);
345
346 rnd_detach_source(&sc->sc_rnd_source);
347 bpf_detach(ifp);
348
349 if_detach(ifp);
350
351 #ifdef DIAGNOSTIC
352 if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
353 sc->sc_ep[UPL_ENDPT_RX] != NULL ||
354 sc->sc_ep[UPL_ENDPT_INTR] != NULL)
355 aprint_debug_dev(self, "detach has active endpoints\n");
356 #endif
357
358 sc->sc_attached = 0;
359 splx(s);
360
361 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
362 sc->sc_dev);
363
364 return (0);
365 }
366
367 int
368 upl_activate(device_t self, enum devact act)
369 {
370 struct upl_softc *sc = device_private(self);
371
372 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
373
374 switch (act) {
375 case DVACT_DEACTIVATE:
376 /* Deactivate the interface. */
377 if_deactivate(&sc->sc_if);
378 sc->sc_dying = 1;
379 return 0;
380 default:
381 return EOPNOTSUPP;
382 }
383 }
384
385 /*
386 * Initialize an RX descriptor and attach an MBUF cluster.
387 */
388 Static int
389 upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m)
390 {
391 struct mbuf *m_new = NULL;
392
393 DPRINTFN(8,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
394
395 if (m == NULL) {
396 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
397 if (m_new == NULL) {
398 printf("%s: no memory for rx list "
399 "-- packet dropped!\n", device_xname(sc->sc_dev));
400 return (ENOBUFS);
401 }
402
403 MCLGET(m_new, M_DONTWAIT);
404 if (!(m_new->m_flags & M_EXT)) {
405 printf("%s: no memory for rx list "
406 "-- packet dropped!\n", device_xname(sc->sc_dev));
407 m_freem(m_new);
408 return (ENOBUFS);
409 }
410 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
411 } else {
412 m_new = m;
413 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
414 m_new->m_data = m_new->m_ext.ext_buf;
415 }
416
417 c->upl_mbuf = m_new;
418
419 return (0);
420 }
421
422 Static int
423 upl_rx_list_init(struct upl_softc *sc)
424 {
425 struct upl_cdata *cd;
426 struct upl_chain *c;
427 int i;
428
429 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
430
431 cd = &sc->sc_cdata;
432 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
433 c = &cd->upl_rx_chain[i];
434 c->upl_sc = sc;
435 c->upl_idx = i;
436 if (upl_newbuf(sc, c, NULL) == ENOBUFS)
437 return (ENOBUFS);
438 if (c->upl_xfer == NULL) {
439 c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
440 if (c->upl_xfer == NULL)
441 return (ENOBUFS);
442 c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
443 if (c->upl_buf == NULL) {
444 usbd_free_xfer(c->upl_xfer);
445 return (ENOBUFS);
446 }
447 }
448 }
449
450 return (0);
451 }
452
453 Static int
454 upl_tx_list_init(struct upl_softc *sc)
455 {
456 struct upl_cdata *cd;
457 struct upl_chain *c;
458 int i;
459
460 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
461
462 cd = &sc->sc_cdata;
463 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
464 c = &cd->upl_tx_chain[i];
465 c->upl_sc = sc;
466 c->upl_idx = i;
467 c->upl_mbuf = NULL;
468 if (c->upl_xfer == NULL) {
469 c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
470 if (c->upl_xfer == NULL)
471 return (ENOBUFS);
472 c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
473 if (c->upl_buf == NULL) {
474 usbd_free_xfer(c->upl_xfer);
475 return (ENOBUFS);
476 }
477 }
478 }
479
480 return (0);
481 }
482
483 /*
484 * A frame has been uploaded: pass the resulting mbuf chain up to
485 * the higher level protocols.
486 */
487 Static void
488 upl_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
489 {
490 struct upl_chain *c = priv;
491 struct upl_softc *sc = c->upl_sc;
492 struct ifnet *ifp = &sc->sc_if;
493 struct mbuf *m;
494 int total_len = 0;
495 int s;
496
497 if (sc->sc_dying)
498 return;
499
500 if (!(ifp->if_flags & IFF_RUNNING))
501 return;
502
503 if (status != USBD_NORMAL_COMPLETION) {
504 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
505 return;
506 sc->sc_rx_errs++;
507 if (usbd_ratecheck(&sc->sc_rx_notice)) {
508 printf("%s: %u usb errors on rx: %s\n",
509 device_xname(sc->sc_dev), sc->sc_rx_errs,
510 usbd_errstr(status));
511 sc->sc_rx_errs = 0;
512 }
513 if (status == USBD_STALLED)
514 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
515 goto done;
516 }
517
518 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
519
520 DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
521 device_xname(sc->sc_dev), __func__, status, total_len));
522
523 m = c->upl_mbuf;
524 memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
525
526 ifp->if_ipackets++;
527 m->m_pkthdr.len = m->m_len = total_len;
528
529 m->m_pkthdr.rcvif = ifp;
530
531 s = splnet();
532
533 /* XXX ugly */
534 if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
535 ifp->if_ierrors++;
536 goto done1;
537 }
538
539 /*
540 * Handle BPF listeners. Let the BPF user see the packet, but
541 * don't pass it up to the ether_input() layer unless it's
542 * a broadcast packet, multicast packet, matches our ethernet
543 * address or the interface is in promiscuous mode.
544 */
545 bpf_mtap(ifp, m);
546
547 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
548 __func__, m->m_len));
549
550 if_input((ifp), (m));
551
552 done1:
553 splx(s);
554
555 done:
556 #if 1
557 /* Setup new transfer. */
558 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
559 c, c->upl_buf, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
560 USBD_NO_TIMEOUT, upl_rxeof);
561 usbd_transfer(c->upl_xfer);
562
563 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->sc_dev),
564 __func__));
565 #endif
566 }
567
568 /*
569 * A frame was downloaded to the chip. It's safe for us to clean up
570 * the list buffers.
571 */
572 Static void
573 upl_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
574 usbd_status status)
575 {
576 struct upl_chain *c = priv;
577 struct upl_softc *sc = c->upl_sc;
578 struct ifnet *ifp = &sc->sc_if;
579 int s;
580
581 if (sc->sc_dying)
582 return;
583
584 s = splnet();
585
586 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->sc_dev),
587 __func__, status));
588
589 ifp->if_timer = 0;
590 ifp->if_flags &= ~IFF_OACTIVE;
591
592 if (status != USBD_NORMAL_COMPLETION) {
593 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
594 splx(s);
595 return;
596 }
597 ifp->if_oerrors++;
598 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
599 usbd_errstr(status));
600 if (status == USBD_STALLED)
601 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]);
602 splx(s);
603 return;
604 }
605
606 ifp->if_opackets++;
607
608 m_freem(c->upl_mbuf);
609 c->upl_mbuf = NULL;
610
611 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
612 upl_start(ifp);
613
614 splx(s);
615 }
616
617 Static int
618 upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
619 {
620 int total_len;
621 struct upl_chain *c;
622 usbd_status err;
623
624 c = &sc->sc_cdata.upl_tx_chain[idx];
625
626 /*
627 * Copy the mbuf data into a contiguous buffer, leaving two
628 * bytes at the beginning to hold the frame length.
629 */
630 m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
631 c->upl_mbuf = m;
632
633 total_len = m->m_pkthdr.len;
634
635 DPRINTFN(10,("%s: %s: total_len=%d\n",
636 device_xname(sc->sc_dev), __func__, total_len));
637
638 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_TX],
639 c, c->upl_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
640 upl_txeof);
641
642 /* Transmit */
643 err = usbd_transfer(c->upl_xfer);
644 if (err != USBD_IN_PROGRESS) {
645 printf("%s: upl_send error=%s\n", device_xname(sc->sc_dev),
646 usbd_errstr(err));
647 upl_stop(sc);
648 return (EIO);
649 }
650
651 sc->sc_cdata.upl_tx_cnt++;
652
653 return (0);
654 }
655
656 Static void
657 upl_start(struct ifnet *ifp)
658 {
659 struct upl_softc *sc = ifp->if_softc;
660 struct mbuf *m_head = NULL;
661
662 if (sc->sc_dying)
663 return;
664
665 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
666
667 if (ifp->if_flags & IFF_OACTIVE)
668 return;
669
670 IFQ_POLL(&ifp->if_snd, m_head);
671 if (m_head == NULL)
672 return;
673
674 if (upl_send(sc, m_head, 0)) {
675 ifp->if_flags |= IFF_OACTIVE;
676 return;
677 }
678
679 IFQ_DEQUEUE(&ifp->if_snd, m_head);
680
681 /*
682 * If there's a BPF listener, bounce a copy of this frame
683 * to him.
684 */
685 bpf_mtap(ifp, m_head);
686
687 ifp->if_flags |= IFF_OACTIVE;
688
689 /*
690 * Set a timeout in case the chip goes out to lunch.
691 */
692 ifp->if_timer = 5;
693 }
694
695 Static void
696 upl_init(void *xsc)
697 {
698 struct upl_softc *sc = xsc;
699 struct ifnet *ifp = &sc->sc_if;
700 int s;
701
702 if (sc->sc_dying)
703 return;
704
705 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
706
707 if (ifp->if_flags & IFF_RUNNING)
708 return;
709
710 s = splnet();
711
712 /* Init TX ring. */
713 if (upl_tx_list_init(sc) == ENOBUFS) {
714 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
715 splx(s);
716 return;
717 }
718
719 /* Init RX ring. */
720 if (upl_rx_list_init(sc) == ENOBUFS) {
721 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
722 splx(s);
723 return;
724 }
725
726 if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
727 if (upl_openpipes(sc)) {
728 splx(s);
729 return;
730 }
731 }
732
733 ifp->if_flags |= IFF_RUNNING;
734 ifp->if_flags &= ~IFF_OACTIVE;
735
736 splx(s);
737 }
738
739 Static int
740 upl_openpipes(struct upl_softc *sc)
741 {
742 struct upl_chain *c;
743 usbd_status err;
744 int i;
745
746 /* Open RX and TX pipes. */
747 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
748 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
749 if (err) {
750 printf("%s: open rx pipe failed: %s\n",
751 device_xname(sc->sc_dev), usbd_errstr(err));
752 return (EIO);
753 }
754 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
755 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
756 if (err) {
757 printf("%s: open tx pipe failed: %s\n",
758 device_xname(sc->sc_dev), usbd_errstr(err));
759 return (EIO);
760 }
761 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
762 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
763 &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
764 UPL_INTR_INTERVAL);
765 if (err) {
766 printf("%s: open intr pipe failed: %s\n",
767 device_xname(sc->sc_dev), usbd_errstr(err));
768 return (EIO);
769 }
770
771
772 #if 1
773 /* Start up the receive pipe. */
774 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
775 c = &sc->sc_cdata.upl_rx_chain[i];
776 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
777 c, c->upl_buf, UPL_BUFSZ,
778 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
779 upl_rxeof);
780 usbd_transfer(c->upl_xfer);
781 }
782 #endif
783
784 return (0);
785 }
786
787 Static void
788 upl_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
789 usbd_status status)
790 {
791 struct upl_softc *sc = priv;
792 struct ifnet *ifp = &sc->sc_if;
793 uByte stat;
794
795 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
796
797 if (sc->sc_dying)
798 return;
799
800 if (!(ifp->if_flags & IFF_RUNNING))
801 return;
802
803 if (status != USBD_NORMAL_COMPLETION) {
804 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
805 return;
806 }
807 sc->sc_intr_errs++;
808 if (usbd_ratecheck(&sc->sc_rx_notice)) {
809 printf("%s: %u usb errors on intr: %s\n",
810 device_xname(sc->sc_dev), sc->sc_rx_errs,
811 usbd_errstr(status));
812 sc->sc_intr_errs = 0;
813 }
814 if (status == USBD_STALLED)
815 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
816 return;
817 }
818
819 stat = sc->sc_ibuf;
820
821 if (stat == 0)
822 return;
823
824 DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(sc->sc_dev),
825 __func__, stat));
826
827 }
828
829 Static int
830 upl_ioctl(struct ifnet *ifp, u_long command, void *data)
831 {
832 struct upl_softc *sc = ifp->if_softc;
833 struct ifaddr *ifa = (struct ifaddr *)data;
834 struct ifreq *ifr = (struct ifreq *)data;
835 int s, error = 0;
836
837 if (sc->sc_dying)
838 return (EIO);
839
840 DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
841 device_xname(sc->sc_dev), __func__, command));
842
843 s = splnet();
844
845 switch(command) {
846 case SIOCINITIFADDR:
847 ifp->if_flags |= IFF_UP;
848 upl_init(sc);
849
850 switch (ifa->ifa_addr->sa_family) {
851 #ifdef INET
852 case AF_INET:
853 break;
854 #endif /* INET */
855 }
856 break;
857
858 case SIOCSIFMTU:
859 if (ifr->ifr_mtu > UPL_BUFSZ)
860 error = EINVAL;
861 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
862 error = 0;
863 break;
864
865 case SIOCSIFFLAGS:
866 if ((error = ifioctl_common(ifp, command, data)) != 0)
867 break;
868 /* XXX re-use ether_ioctl() */
869 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
870 case IFF_UP:
871 upl_init(sc);
872 break;
873 case IFF_RUNNING:
874 upl_stop(sc);
875 break;
876 default:
877 break;
878 }
879 break;
880 default:
881 error = ifioctl_common(ifp, command, data);
882 break;
883 }
884
885 splx(s);
886
887 return (error);
888 }
889
890 Static void
891 upl_watchdog(struct ifnet *ifp)
892 {
893 struct upl_softc *sc = ifp->if_softc;
894
895 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
896
897 if (sc->sc_dying)
898 return;
899
900 ifp->if_oerrors++;
901 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
902
903 upl_stop(sc);
904 upl_init(sc);
905
906 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
907 upl_start(ifp);
908 }
909
910 /*
911 * Stop the adapter and free any mbufs allocated to the
912 * RX and TX lists.
913 */
914 Static void
915 upl_stop(struct upl_softc *sc)
916 {
917 usbd_status err;
918 struct ifnet *ifp;
919 int i;
920
921 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
922
923 ifp = &sc->sc_if;
924 ifp->if_timer = 0;
925
926 /* Stop transfers. */
927 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
928 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
929 if (err) {
930 printf("%s: abort rx pipe failed: %s\n",
931 device_xname(sc->sc_dev), usbd_errstr(err));
932 }
933 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
934 if (err) {
935 printf("%s: close rx pipe failed: %s\n",
936 device_xname(sc->sc_dev), usbd_errstr(err));
937 }
938 sc->sc_ep[UPL_ENDPT_RX] = NULL;
939 }
940
941 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
942 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
943 if (err) {
944 printf("%s: abort tx pipe failed: %s\n",
945 device_xname(sc->sc_dev), usbd_errstr(err));
946 }
947 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
948 if (err) {
949 printf("%s: close tx pipe failed: %s\n",
950 device_xname(sc->sc_dev), usbd_errstr(err));
951 }
952 sc->sc_ep[UPL_ENDPT_TX] = NULL;
953 }
954
955 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
956 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
957 if (err) {
958 printf("%s: abort intr pipe failed: %s\n",
959 device_xname(sc->sc_dev), usbd_errstr(err));
960 }
961 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
962 if (err) {
963 printf("%s: close intr pipe failed: %s\n",
964 device_xname(sc->sc_dev), usbd_errstr(err));
965 }
966 sc->sc_ep[UPL_ENDPT_INTR] = NULL;
967 }
968
969 /* Free RX resources. */
970 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
971 if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
972 m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
973 sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
974 }
975 if (sc->sc_cdata.upl_rx_chain[i].upl_xfer != NULL) {
976 usbd_free_xfer(sc->sc_cdata.upl_rx_chain[i].upl_xfer);
977 sc->sc_cdata.upl_rx_chain[i].upl_xfer = NULL;
978 }
979 }
980
981 /* Free TX resources. */
982 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
983 if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
984 m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
985 sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
986 }
987 if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
988 usbd_free_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
989 sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
990 }
991 }
992
993 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
994 }
995
996 Static int
997 upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
998 struct rtentry *rt0)
999 {
1000 int s, len, error;
1001
1002 DPRINTFN(10,("%s: %s: enter\n",
1003 device_xname(((struct upl_softc *)ifp->if_softc)->sc_dev),
1004 __func__));
1005
1006 /*
1007 * if the queueing discipline needs packet classification,
1008 * do it now.
1009 */
1010 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
1011
1012 len = m->m_pkthdr.len;
1013 s = splnet();
1014 /*
1015 * Queue message on interface, and start output if interface
1016 * not yet active.
1017 */
1018 IFQ_ENQUEUE(&ifp->if_snd, m, error);
1019 if (error) {
1020 /* mbuf is already freed */
1021 splx(s);
1022 return (error);
1023 }
1024 ifp->if_obytes += len;
1025 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1026 (*ifp->if_start)(ifp);
1027 splx(s);
1028
1029 return (0);
1030 }
1031
1032 Static void
1033 upl_input(struct ifnet *ifp, struct mbuf *m)
1034 {
1035 #ifdef INET
1036 size_t pktlen = m->m_len;
1037 int s;
1038
1039 s = splnet();
1040 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
1041 ifp->if_iqdrops++;
1042 m_freem(m);
1043 } else {
1044 ifp->if_ipackets++;
1045 ifp->if_ibytes += pktlen;
1046 }
1047 splx(s);
1048 #endif
1049 }
1050