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