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