if_upl.c revision 1.47.8.1 1 /* $NetBSD: if_upl.c,v 1.47.8.1 2016/09/06 20:33:08 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.8.1 2016/09/06 20:33:08 skrll 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/kernel.h>
49 #include <sys/socket.h>
50
51 #include <sys/device.h>
52 #include <sys/rnd.h>
53
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/netisr.h>
58
59 #include <net/bpf.h>
60
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_inarp.h>
65 #endif
66
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72
73 /*
74 * 7 6 5 4 3 2 1 0
75 * tx rx 1 0
76 * 1110 0000 rxdata
77 * 1010 0000 idle
78 * 0010 0000 tx over
79 * 0110 tx over + rxd
80 */
81
82 #define UPL_RXDATA 0x40
83 #define UPL_TXOK 0x80
84
85 #define UPL_INTR_PKTLEN 1
86
87 #define UPL_CONFIG_NO 1
88 #define UPL_IFACE_IDX 0
89
90 /***/
91
92 #define UPL_INTR_INTERVAL 20
93
94 #define UPL_BUFSZ 1024
95
96 #define UPL_RX_FRAMES 1
97 #define UPL_TX_FRAMES 1
98
99 #define UPL_RX_LIST_CNT 1
100 #define UPL_TX_LIST_CNT 1
101
102 #define UPL_ENDPT_RX 0x0
103 #define UPL_ENDPT_TX 0x1
104 #define UPL_ENDPT_INTR 0x2
105 #define UPL_ENDPT_MAX 0x3
106
107 struct upl_type {
108 uint16_t upl_vid;
109 uint16_t upl_did;
110 };
111
112 struct upl_softc;
113
114 struct upl_chain {
115 struct upl_softc *upl_sc;
116 struct usbd_xfer *upl_xfer;
117 char *upl_buf;
118 struct mbuf *upl_mbuf;
119 int upl_idx;
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_cons;
127 int upl_tx_cnt;
128 int upl_rx_prod;
129 };
130
131 struct upl_softc {
132 device_t sc_dev;
133
134 struct ifnet sc_if;
135 krndsource_t sc_rnd_source;
136
137 struct callout sc_stat_ch;
138
139 struct usbd_device * sc_udev;
140 struct usbd_interface * sc_iface;
141 uint16_t sc_vendor;
142 uint16_t sc_product;
143 int sc_ed[UPL_ENDPT_MAX];
144 struct usbd_pipe * sc_ep[UPL_ENDPT_MAX];
145 struct upl_cdata sc_cdata;
146
147 uByte sc_ibuf;
148
149 char sc_dying;
150 char sc_attached;
151 u_int sc_rx_errs;
152 struct timeval sc_rx_notice;
153 u_int sc_intr_errs;
154 };
155
156 #ifdef UPL_DEBUG
157 #define DPRINTF(x) if (upldebug) printf x
158 #define DPRINTFN(n,x) if (upldebug >= (n)) printf x
159 int upldebug = 0;
160 #else
161 #define DPRINTF(x)
162 #define DPRINTFN(n,x)
163 #endif
164
165 /*
166 * Various supported device vendors/products.
167 */
168 Static struct upl_type sc_devs[] = {
169 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
170 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 },
171 { 0, 0 }
172 };
173
174 int upl_match(device_t, cfdata_t, void *);
175 void upl_attach(device_t, device_t, void *);
176 int upl_detach(device_t, int);
177 int upl_activate(device_t, enum devact);
178 extern struct cfdriver upl_cd;
179 CFATTACH_DECL_NEW(upl, sizeof(struct upl_softc), upl_match, upl_attach,
180 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(struct usbd_xfer *, void *, usbd_status);
188 Static void upl_rxeof(struct usbd_xfer *, void *, usbd_status);
189 Static void upl_txeof(struct usbd_xfer *, void *, 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->uaa_vendor == t->upl_vid && uaa->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 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 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_attach(ifp);
310 if_alloc_sadl(ifp);
311
312 bpf_attach(ifp, DLT_RAW, 0);
313 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
314 RND_TYPE_NET, RND_FLAG_DEFAULT);
315
316 sc->sc_attached = 1;
317 splx(s);
318
319 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
320
321 return;
322 }
323
324 int
325 upl_detach(device_t self, int flags)
326 {
327 struct upl_softc *sc = device_private(self);
328 struct ifnet *ifp = &sc->sc_if;
329 int s;
330
331 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
332
333 s = splusb();
334
335 if (!sc->sc_attached) {
336 /* Detached before attached finished, so just bail out. */
337 splx(s);
338 return 0;
339 }
340
341 if (ifp->if_flags & IFF_RUNNING)
342 upl_stop(sc);
343
344 rnd_detach_source(&sc->sc_rnd_source);
345 bpf_detach(ifp);
346
347 if_detach(ifp);
348
349 #ifdef DIAGNOSTIC
350 if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
351 sc->sc_ep[UPL_ENDPT_RX] != NULL ||
352 sc->sc_ep[UPL_ENDPT_INTR] != NULL)
353 aprint_debug_dev(self, "detach has active endpoints\n");
354 #endif
355
356 sc->sc_attached = 0;
357 splx(s);
358
359 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
360
361 return 0;
362 }
363
364 int
365 upl_activate(device_t self, enum devact act)
366 {
367 struct upl_softc *sc = device_private(self);
368
369 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
370
371 switch (act) {
372 case DVACT_DEACTIVATE:
373 /* Deactivate the interface. */
374 if_deactivate(&sc->sc_if);
375 sc->sc_dying = 1;
376 return 0;
377 default:
378 return EOPNOTSUPP;
379 }
380 }
381
382 /*
383 * Initialize an RX descriptor and attach an MBUF cluster.
384 */
385 Static int
386 upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m)
387 {
388 struct mbuf *m_new = NULL;
389
390 DPRINTFN(8,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
391
392 if (m == NULL) {
393 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
394 if (m_new == NULL) {
395 printf("%s: no memory for rx list "
396 "-- packet dropped!\n", device_xname(sc->sc_dev));
397 return ENOBUFS;
398 }
399
400 MCLGET(m_new, M_DONTWAIT);
401 if (!(m_new->m_flags & M_EXT)) {
402 printf("%s: no memory for rx list "
403 "-- packet dropped!\n", device_xname(sc->sc_dev));
404 m_freem(m_new);
405 return ENOBUFS;
406 }
407 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
408 } else {
409 m_new = m;
410 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
411 m_new->m_data = m_new->m_ext.ext_buf;
412 }
413
414 c->upl_mbuf = m_new;
415
416 return 0;
417 }
418
419 Static int
420 upl_rx_list_init(struct upl_softc *sc)
421 {
422 struct upl_cdata *cd;
423 struct upl_chain *c;
424 int i;
425
426 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
427
428 cd = &sc->sc_cdata;
429 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
430 c = &cd->upl_rx_chain[i];
431 c->upl_sc = sc;
432 c->upl_idx = i;
433 if (upl_newbuf(sc, c, NULL) == ENOBUFS)
434 return ENOBUFS;
435 if (c->upl_xfer == NULL) {
436 int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_RX],
437 UPL_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->upl_xfer);
438 if (error)
439 return error;
440 c->upl_buf = usbd_get_buffer(c->upl_xfer);
441 }
442 }
443
444 return 0;
445 }
446
447 Static int
448 upl_tx_list_init(struct upl_softc *sc)
449 {
450 struct upl_cdata *cd;
451 struct upl_chain *c;
452 int i;
453
454 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
455
456 cd = &sc->sc_cdata;
457 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
458 c = &cd->upl_tx_chain[i];
459 c->upl_sc = sc;
460 c->upl_idx = i;
461 c->upl_mbuf = NULL;
462 if (c->upl_xfer == NULL) {
463 int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_TX],
464 UPL_BUFSZ, 0, 0, &c->upl_xfer);
465 if (error)
466 return error;
467 c->upl_buf = usbd_get_buffer(c->upl_xfer);
468 }
469 }
470
471 return 0;
472 }
473
474 /*
475 * A frame has been uploaded: pass the resulting mbuf chain up to
476 * the higher level protocols.
477 */
478 Static void
479 upl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
480 {
481 struct upl_chain *c = priv;
482 struct upl_softc *sc = c->upl_sc;
483 struct ifnet *ifp = &sc->sc_if;
484 struct mbuf *m;
485 int total_len = 0;
486 int s;
487
488 if (sc->sc_dying)
489 return;
490
491 if (!(ifp->if_flags & IFF_RUNNING))
492 return;
493
494 if (status != USBD_NORMAL_COMPLETION) {
495 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
496 return;
497 sc->sc_rx_errs++;
498 if (usbd_ratecheck(&sc->sc_rx_notice)) {
499 printf("%s: %u usb errors on rx: %s\n",
500 device_xname(sc->sc_dev), sc->sc_rx_errs,
501 usbd_errstr(status));
502 sc->sc_rx_errs = 0;
503 }
504 if (status == USBD_STALLED)
505 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
506 goto done;
507 }
508
509 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
510
511 DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
512 device_xname(sc->sc_dev), __func__, status, total_len));
513
514 m = c->upl_mbuf;
515 memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
516
517 ifp->if_ipackets++;
518 m->m_pkthdr.len = m->m_len = total_len;
519
520 m->m_pkthdr.rcvif = ifp;
521
522 s = splnet();
523
524 /* XXX ugly */
525 if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
526 ifp->if_ierrors++;
527 goto done1;
528 }
529
530 /*
531 * Handle BPF listeners. Let the BPF user see the packet, but
532 * don't pass it up to the ether_input() layer unless it's
533 * a broadcast packet, multicast packet, matches our ethernet
534 * address or the interface is in promiscuous mode.
535 */
536 bpf_mtap(ifp, m);
537
538 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
539 __func__, m->m_len));
540
541 (*(ifp)->if_input)((ifp), (m));
542
543 done1:
544 splx(s);
545
546 done:
547 #if 1
548 /* Setup new transfer. */
549 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ,
550 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upl_rxeof);
551 usbd_transfer(c->upl_xfer);
552
553 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->sc_dev),
554 __func__));
555 #endif
556 }
557
558 /*
559 * A frame was downloaded to the chip. It's safe for us to clean up
560 * the list buffers.
561 */
562 Static void
563 upl_txeof(struct usbd_xfer *xfer, void *priv,
564 usbd_status status)
565 {
566 struct upl_chain *c = priv;
567 struct upl_softc *sc = c->upl_sc;
568 struct ifnet *ifp = &sc->sc_if;
569 int s;
570
571 if (sc->sc_dying)
572 return;
573
574 s = splnet();
575
576 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->sc_dev),
577 __func__, status));
578
579 ifp->if_timer = 0;
580 ifp->if_flags &= ~IFF_OACTIVE;
581
582 if (status != USBD_NORMAL_COMPLETION) {
583 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
584 splx(s);
585 return;
586 }
587 ifp->if_oerrors++;
588 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
589 usbd_errstr(status));
590 if (status == USBD_STALLED)
591 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]);
592 splx(s);
593 return;
594 }
595
596 ifp->if_opackets++;
597
598 m_freem(c->upl_mbuf);
599 c->upl_mbuf = NULL;
600
601 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
602 upl_start(ifp);
603
604 splx(s);
605 }
606
607 Static int
608 upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
609 {
610 int total_len;
611 struct upl_chain *c;
612 usbd_status err;
613
614 c = &sc->sc_cdata.upl_tx_chain[idx];
615
616 /*
617 * Copy the mbuf data into a contiguous buffer, leaving two
618 * bytes at the beginning to hold the frame length.
619 */
620 m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
621 c->upl_mbuf = m;
622
623 total_len = m->m_pkthdr.len;
624
625 DPRINTFN(10,("%s: %s: total_len=%d\n",
626 device_xname(sc->sc_dev), __func__, total_len));
627
628 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, total_len, 0,
629 USBD_DEFAULT_TIMEOUT, upl_txeof);
630
631 /* Transmit */
632 err = usbd_transfer(c->upl_xfer);
633 if (err != USBD_IN_PROGRESS) {
634 printf("%s: upl_send error=%s\n", device_xname(sc->sc_dev),
635 usbd_errstr(err));
636 upl_stop(sc);
637 return EIO;
638 }
639
640 sc->sc_cdata.upl_tx_cnt++;
641
642 return 0;
643 }
644
645 Static void
646 upl_start(struct ifnet *ifp)
647 {
648 struct upl_softc *sc = ifp->if_softc;
649 struct mbuf *m_head = NULL;
650
651 if (sc->sc_dying)
652 return;
653
654 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
655
656 if (ifp->if_flags & IFF_OACTIVE)
657 return;
658
659 IFQ_POLL(&ifp->if_snd, m_head);
660 if (m_head == NULL)
661 return;
662
663 if (upl_send(sc, m_head, 0)) {
664 ifp->if_flags |= IFF_OACTIVE;
665 return;
666 }
667
668 IFQ_DEQUEUE(&ifp->if_snd, m_head);
669
670 /*
671 * If there's a BPF listener, bounce a copy of this frame
672 * to him.
673 */
674 bpf_mtap(ifp, m_head);
675
676 ifp->if_flags |= IFF_OACTIVE;
677
678 /*
679 * Set a timeout in case the chip goes out to lunch.
680 */
681 ifp->if_timer = 5;
682 }
683
684 Static void
685 upl_init(void *xsc)
686 {
687 struct upl_softc *sc = xsc;
688 struct ifnet *ifp = &sc->sc_if;
689 int s;
690
691 if (sc->sc_dying)
692 return;
693
694 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
695
696 if (ifp->if_flags & IFF_RUNNING)
697 return;
698
699 s = splnet();
700
701 if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
702 if (upl_openpipes(sc)) {
703 splx(s);
704 return;
705 }
706 }
707 /* Init TX ring. */
708 if (upl_tx_list_init(sc)) {
709 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
710 splx(s);
711 return;
712 }
713
714 /* Init RX ring. */
715 if (upl_rx_list_init(sc)) {
716 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
717 splx(s);
718 return;
719 }
720
721 /* Start up the receive pipe. */
722 for (int i = 0; i < UPL_RX_LIST_CNT; i++) {
723 struct upl_chain *c = &sc->sc_cdata.upl_rx_chain[i];
724 usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ,
725 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
726 upl_rxeof);
727 usbd_transfer(c->upl_xfer);
728 }
729
730 ifp->if_flags |= IFF_RUNNING;
731 ifp->if_flags &= ~IFF_OACTIVE;
732
733 splx(s);
734 }
735
736 Static int
737 upl_openpipes(struct upl_softc *sc)
738 {
739 usbd_status err;
740
741 /* Open RX and TX pipes. */
742 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
743 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
744 if (err) {
745 printf("%s: open rx pipe failed: %s\n",
746 device_xname(sc->sc_dev), usbd_errstr(err));
747 return EIO;
748 }
749 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
750 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
751 if (err) {
752 printf("%s: open tx pipe failed: %s\n",
753 device_xname(sc->sc_dev), usbd_errstr(err));
754 return EIO;
755 }
756 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
757 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
758 &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
759 UPL_INTR_INTERVAL);
760 if (err) {
761 printf("%s: open intr pipe failed: %s\n",
762 device_xname(sc->sc_dev), usbd_errstr(err));
763 return EIO;
764 }
765
766 return 0;
767 }
768
769 Static void
770 upl_intr(struct usbd_xfer *xfer, void *priv,
771 usbd_status status)
772 {
773 struct upl_softc *sc = priv;
774 struct ifnet *ifp = &sc->sc_if;
775 uByte stat;
776
777 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
778
779 if (sc->sc_dying)
780 return;
781
782 if (!(ifp->if_flags & IFF_RUNNING))
783 return;
784
785 if (status != USBD_NORMAL_COMPLETION) {
786 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
787 return;
788 }
789 sc->sc_intr_errs++;
790 if (usbd_ratecheck(&sc->sc_rx_notice)) {
791 printf("%s: %u usb errors on intr: %s\n",
792 device_xname(sc->sc_dev), sc->sc_rx_errs,
793 usbd_errstr(status));
794 sc->sc_intr_errs = 0;
795 }
796 if (status == USBD_STALLED)
797 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]);
798 return;
799 }
800
801 stat = sc->sc_ibuf;
802
803 if (stat == 0)
804 return;
805
806 DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(sc->sc_dev),
807 __func__, stat));
808
809 }
810
811 Static int
812 upl_ioctl(struct ifnet *ifp, u_long command, void *data)
813 {
814 struct upl_softc *sc = ifp->if_softc;
815 struct ifaddr *ifa = (struct ifaddr *)data;
816 struct ifreq *ifr = (struct ifreq *)data;
817 int s, error = 0;
818
819 if (sc->sc_dying)
820 return EIO;
821
822 DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
823 device_xname(sc->sc_dev), __func__, command));
824
825 s = splnet();
826
827 switch(command) {
828 case SIOCINITIFADDR:
829 ifp->if_flags |= IFF_UP;
830 upl_init(sc);
831
832 switch (ifa->ifa_addr->sa_family) {
833 #ifdef INET
834 case AF_INET:
835 break;
836 #endif /* INET */
837 }
838 break;
839
840 case SIOCSIFMTU:
841 if (ifr->ifr_mtu > UPL_BUFSZ)
842 error = EINVAL;
843 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
844 error = 0;
845 break;
846
847 case SIOCSIFFLAGS:
848 if ((error = ifioctl_common(ifp, command, data)) != 0)
849 break;
850 /* XXX re-use ether_ioctl() */
851 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
852 case IFF_UP:
853 upl_init(sc);
854 break;
855 case IFF_RUNNING:
856 upl_stop(sc);
857 break;
858 default:
859 break;
860 }
861 break;
862 default:
863 error = ifioctl_common(ifp, command, data);
864 break;
865 }
866
867 splx(s);
868
869 return error;
870 }
871
872 Static void
873 upl_watchdog(struct ifnet *ifp)
874 {
875 struct upl_softc *sc = ifp->if_softc;
876
877 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
878
879 if (sc->sc_dying)
880 return;
881
882 ifp->if_oerrors++;
883 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
884
885 upl_stop(sc);
886 upl_init(sc);
887
888 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
889 upl_start(ifp);
890 }
891
892 /*
893 * Stop the adapter and free any mbufs allocated to the
894 * RX and TX lists.
895 */
896 Static void
897 upl_stop(struct upl_softc *sc)
898 {
899 usbd_status err;
900 struct ifnet *ifp;
901 int i;
902
903 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
904
905 ifp = &sc->sc_if;
906 ifp->if_timer = 0;
907
908 /* Stop transfers. */
909 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
910 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
911 if (err) {
912 printf("%s: abort rx pipe failed: %s\n",
913 device_xname(sc->sc_dev), usbd_errstr(err));
914 }
915 }
916
917 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
918 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
919 if (err) {
920 printf("%s: abort tx pipe failed: %s\n",
921 device_xname(sc->sc_dev), usbd_errstr(err));
922 }
923 }
924
925 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
926 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
927 if (err) {
928 printf("%s: abort intr pipe failed: %s\n",
929 device_xname(sc->sc_dev), usbd_errstr(err));
930 }
931 }
932
933 /* Free RX resources. */
934 for (i = 0; i < UPL_RX_LIST_CNT; i++) {
935 if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
936 m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
937 sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
938 }
939 }
940
941 /* Free TX resources. */
942 for (i = 0; i < UPL_TX_LIST_CNT; i++) {
943 if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
944 m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
945 sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
946 }
947 if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
948 usbd_destroy_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
949 sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
950 }
951 }
952
953 /* Close pipes */
954 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
955 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
956 if (err) {
957 printf("%s: close rx pipe failed: %s\n",
958 device_xname(sc->sc_dev), usbd_errstr(err));
959 }
960 sc->sc_ep[UPL_ENDPT_RX] = NULL;
961 }
962
963 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
964 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
965 if (err) {
966 printf("%s: close tx pipe failed: %s\n",
967 device_xname(sc->sc_dev), usbd_errstr(err));
968 }
969 sc->sc_ep[UPL_ENDPT_TX] = NULL;
970 }
971
972 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
973 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
974 if (err) {
975 printf("%s: close intr pipe failed: %s\n",
976 device_xname(sc->sc_dev), usbd_errstr(err));
977 }
978 sc->sc_ep[UPL_ENDPT_INTR] = NULL;
979 }
980
981 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
982 }
983
984 Static int
985 upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
986 struct rtentry *rt0)
987 {
988 int s, len, error;
989 ALTQ_DECL(struct altq_pktattr pktattr;)
990
991 DPRINTFN(10,("%s: %s: enter\n",
992 device_xname(((struct upl_softc *)ifp->if_softc)->sc_dev),
993 __func__));
994
995 /*
996 * if the queueing discipline needs packet classification,
997 * do it now.
998 */
999 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
1000
1001 len = m->m_pkthdr.len;
1002 s = splnet();
1003 /*
1004 * Queue message on interface, and start output if interface
1005 * not yet active.
1006 */
1007 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
1008 if (error) {
1009 /* mbuf is already freed */
1010 splx(s);
1011 return (error);
1012 }
1013 ifp->if_obytes += len;
1014 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1015 (*ifp->if_start)(ifp);
1016 splx(s);
1017
1018 return (0);
1019 }
1020
1021 Static void
1022 upl_input(struct ifnet *ifp, struct mbuf *m)
1023 {
1024 #ifdef INET
1025 size_t pktlen = m->m_len;
1026 int s;
1027
1028 s = splnet();
1029 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
1030 ifp->if_iqdrops++;
1031 m_freem(m);
1032 } else {
1033 ifp->if_ipackets++;
1034 ifp->if_ibytes += pktlen;
1035 }
1036 splx(s);
1037 #endif
1038 }
1039