if_upl.c revision 1.9.2.5 1 1.9.2.5 bouyer /* $NetBSD: if_upl.c,v 1.9.2.5 2001/04/21 17:49:54 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.5 bouyer s = splnet();
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.4 bouyer ifp->if_dlt = DLT_RAW;
317 1.9.2.2 bouyer
318 1.9.2.2 bouyer /* Attach the interface. */
319 1.9.2.2 bouyer if_attach(ifp);
320 1.9.2.4 bouyer if_alloc_sadl(ifp);
321 1.9.2.2 bouyer
322 1.9.2.2 bouyer #if NBPFILTER > 0
323 1.9.2.4 bouyer bpfattach(ifp, DLT_RAW, 0);
324 1.9.2.2 bouyer #endif
325 1.9.2.2 bouyer #if NRND > 0
326 1.9.2.2 bouyer rnd_attach_source(&sc->sc_rnd_source, USBDEVNAME(sc->sc_dev),
327 1.9.2.2 bouyer RND_TYPE_NET, 0);
328 1.9.2.2 bouyer #endif
329 1.9.2.2 bouyer
330 1.9.2.2 bouyer sc->sc_attached = 1;
331 1.9.2.2 bouyer splx(s);
332 1.9.2.2 bouyer
333 1.9.2.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
334 1.9.2.2 bouyer USBDEV(sc->sc_dev));
335 1.9.2.2 bouyer
336 1.9.2.2 bouyer USB_ATTACH_SUCCESS_RETURN;
337 1.9.2.2 bouyer }
338 1.9.2.2 bouyer
339 1.9.2.2 bouyer USB_DETACH(upl)
340 1.9.2.2 bouyer {
341 1.9.2.2 bouyer USB_DETACH_START(upl, sc);
342 1.9.2.2 bouyer struct ifnet *ifp = &sc->sc_if;
343 1.9.2.2 bouyer int s;
344 1.9.2.2 bouyer
345 1.9.2.2 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
346 1.9.2.2 bouyer
347 1.9.2.2 bouyer s = splusb();
348 1.9.2.2 bouyer
349 1.9.2.2 bouyer if (!sc->sc_attached) {
350 1.9.2.2 bouyer /* Detached before attached finished, so just bail out. */
351 1.9.2.2 bouyer splx(s);
352 1.9.2.2 bouyer return (0);
353 1.9.2.2 bouyer }
354 1.9.2.2 bouyer
355 1.9.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
356 1.9.2.2 bouyer upl_stop(sc);
357 1.9.2.2 bouyer
358 1.9.2.2 bouyer #if NRND > 0
359 1.9.2.2 bouyer rnd_detach_source(&sc->sc_rnd_source);
360 1.9.2.2 bouyer #endif
361 1.9.2.2 bouyer #if NBPFILTER > 0
362 1.9.2.2 bouyer bpfdetach(ifp);
363 1.9.2.2 bouyer #endif
364 1.9.2.2 bouyer
365 1.9.2.2 bouyer if_detach(ifp);
366 1.9.2.2 bouyer
367 1.9.2.2 bouyer #ifdef DIAGNOSTIC
368 1.9.2.2 bouyer if (sc->sc_ep[UPL_ENDPT_TX] != NULL ||
369 1.9.2.2 bouyer sc->sc_ep[UPL_ENDPT_RX] != NULL ||
370 1.9.2.2 bouyer sc->sc_ep[UPL_ENDPT_INTR] != NULL)
371 1.9.2.2 bouyer printf("%s: detach has active endpoints\n",
372 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev));
373 1.9.2.2 bouyer #endif
374 1.9.2.2 bouyer
375 1.9.2.2 bouyer sc->sc_attached = 0;
376 1.9.2.2 bouyer splx(s);
377 1.9.2.2 bouyer
378 1.9.2.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
379 1.9.2.2 bouyer USBDEV(sc->sc_dev));
380 1.9.2.2 bouyer
381 1.9.2.2 bouyer return (0);
382 1.9.2.2 bouyer }
383 1.9.2.2 bouyer
384 1.9.2.2 bouyer int
385 1.9.2.2 bouyer upl_activate(device_ptr_t self, enum devact act)
386 1.9.2.2 bouyer {
387 1.9.2.2 bouyer struct upl_softc *sc = (struct upl_softc *)self;
388 1.9.2.2 bouyer
389 1.9.2.2 bouyer DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
390 1.9.2.2 bouyer
391 1.9.2.2 bouyer switch (act) {
392 1.9.2.2 bouyer case DVACT_ACTIVATE:
393 1.9.2.2 bouyer return (EOPNOTSUPP);
394 1.9.2.2 bouyer break;
395 1.9.2.2 bouyer
396 1.9.2.2 bouyer case DVACT_DEACTIVATE:
397 1.9.2.2 bouyer /* Deactivate the interface. */
398 1.9.2.2 bouyer if_deactivate(&sc->sc_if);
399 1.9.2.2 bouyer sc->sc_dying = 1;
400 1.9.2.2 bouyer break;
401 1.9.2.2 bouyer }
402 1.9.2.2 bouyer return (0);
403 1.9.2.2 bouyer }
404 1.9.2.2 bouyer
405 1.9.2.2 bouyer /*
406 1.9.2.2 bouyer * Initialize an RX descriptor and attach an MBUF cluster.
407 1.9.2.2 bouyer */
408 1.9.2.2 bouyer Static int
409 1.9.2.2 bouyer upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m)
410 1.9.2.2 bouyer {
411 1.9.2.2 bouyer struct mbuf *m_new = NULL;
412 1.9.2.2 bouyer
413 1.9.2.2 bouyer DPRINTFN(8,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
414 1.9.2.2 bouyer
415 1.9.2.2 bouyer if (m == NULL) {
416 1.9.2.2 bouyer MGETHDR(m_new, M_DONTWAIT, MT_DATA);
417 1.9.2.2 bouyer if (m_new == NULL) {
418 1.9.2.2 bouyer printf("%s: no memory for rx list "
419 1.9.2.2 bouyer "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
420 1.9.2.2 bouyer return (ENOBUFS);
421 1.9.2.2 bouyer }
422 1.9.2.2 bouyer
423 1.9.2.2 bouyer MCLGET(m_new, M_DONTWAIT);
424 1.9.2.2 bouyer if (!(m_new->m_flags & M_EXT)) {
425 1.9.2.2 bouyer printf("%s: no memory for rx list "
426 1.9.2.2 bouyer "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
427 1.9.2.2 bouyer m_freem(m_new);
428 1.9.2.2 bouyer return (ENOBUFS);
429 1.9.2.2 bouyer }
430 1.9.2.2 bouyer m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
431 1.9.2.2 bouyer } else {
432 1.9.2.2 bouyer m_new = m;
433 1.9.2.2 bouyer m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
434 1.9.2.2 bouyer m_new->m_data = m_new->m_ext.ext_buf;
435 1.9.2.2 bouyer }
436 1.9.2.2 bouyer
437 1.9.2.2 bouyer c->upl_mbuf = m_new;
438 1.9.2.2 bouyer
439 1.9.2.2 bouyer return (0);
440 1.9.2.2 bouyer }
441 1.9.2.2 bouyer
442 1.9.2.2 bouyer Static int
443 1.9.2.2 bouyer upl_rx_list_init(struct upl_softc *sc)
444 1.9.2.2 bouyer {
445 1.9.2.2 bouyer struct upl_cdata *cd;
446 1.9.2.2 bouyer struct upl_chain *c;
447 1.9.2.2 bouyer int i;
448 1.9.2.2 bouyer
449 1.9.2.2 bouyer DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
450 1.9.2.2 bouyer
451 1.9.2.2 bouyer cd = &sc->sc_cdata;
452 1.9.2.2 bouyer for (i = 0; i < UPL_RX_LIST_CNT; i++) {
453 1.9.2.2 bouyer c = &cd->upl_rx_chain[i];
454 1.9.2.2 bouyer c->upl_sc = sc;
455 1.9.2.2 bouyer c->upl_idx = i;
456 1.9.2.2 bouyer if (upl_newbuf(sc, c, NULL) == ENOBUFS)
457 1.9.2.2 bouyer return (ENOBUFS);
458 1.9.2.2 bouyer if (c->upl_xfer == NULL) {
459 1.9.2.2 bouyer c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
460 1.9.2.2 bouyer if (c->upl_xfer == NULL)
461 1.9.2.2 bouyer return (ENOBUFS);
462 1.9.2.2 bouyer c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
463 1.9.2.2 bouyer if (c->upl_buf == NULL) {
464 1.9.2.2 bouyer usbd_free_xfer(c->upl_xfer);
465 1.9.2.2 bouyer return (ENOBUFS);
466 1.9.2.2 bouyer }
467 1.9.2.2 bouyer }
468 1.9.2.2 bouyer }
469 1.9.2.2 bouyer
470 1.9.2.2 bouyer return (0);
471 1.9.2.2 bouyer }
472 1.9.2.2 bouyer
473 1.9.2.2 bouyer Static int
474 1.9.2.2 bouyer upl_tx_list_init(struct upl_softc *sc)
475 1.9.2.2 bouyer {
476 1.9.2.2 bouyer struct upl_cdata *cd;
477 1.9.2.2 bouyer struct upl_chain *c;
478 1.9.2.2 bouyer int i;
479 1.9.2.2 bouyer
480 1.9.2.2 bouyer DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __FUNCTION__));
481 1.9.2.2 bouyer
482 1.9.2.2 bouyer cd = &sc->sc_cdata;
483 1.9.2.2 bouyer for (i = 0; i < UPL_TX_LIST_CNT; i++) {
484 1.9.2.2 bouyer c = &cd->upl_tx_chain[i];
485 1.9.2.2 bouyer c->upl_sc = sc;
486 1.9.2.2 bouyer c->upl_idx = i;
487 1.9.2.2 bouyer c->upl_mbuf = NULL;
488 1.9.2.2 bouyer if (c->upl_xfer == NULL) {
489 1.9.2.2 bouyer c->upl_xfer = usbd_alloc_xfer(sc->sc_udev);
490 1.9.2.2 bouyer if (c->upl_xfer == NULL)
491 1.9.2.2 bouyer return (ENOBUFS);
492 1.9.2.2 bouyer c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ);
493 1.9.2.2 bouyer if (c->upl_buf == NULL) {
494 1.9.2.2 bouyer usbd_free_xfer(c->upl_xfer);
495 1.9.2.2 bouyer return (ENOBUFS);
496 1.9.2.2 bouyer }
497 1.9.2.2 bouyer }
498 1.9.2.2 bouyer }
499 1.9.2.2 bouyer
500 1.9.2.2 bouyer return (0);
501 1.9.2.2 bouyer }
502 1.9.2.2 bouyer
503 1.9.2.2 bouyer /*
504 1.9.2.2 bouyer * A frame has been uploaded: pass the resulting mbuf chain up to
505 1.9.2.2 bouyer * the higher level protocols.
506 1.9.2.2 bouyer */
507 1.9.2.2 bouyer Static void
508 1.9.2.2 bouyer upl_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
509 1.9.2.2 bouyer {
510 1.9.2.2 bouyer struct upl_chain *c = priv;
511 1.9.2.2 bouyer struct upl_softc *sc = c->upl_sc;
512 1.9.2.2 bouyer struct ifnet *ifp = &sc->sc_if;
513 1.9.2.2 bouyer struct mbuf *m;
514 1.9.2.2 bouyer int total_len = 0;
515 1.9.2.2 bouyer int s;
516 1.9.2.2 bouyer
517 1.9.2.2 bouyer if (sc->sc_dying)
518 1.9.2.2 bouyer return;
519 1.9.2.2 bouyer
520 1.9.2.2 bouyer if (!(ifp->if_flags & IFF_RUNNING))
521 1.9.2.2 bouyer return;
522 1.9.2.2 bouyer
523 1.9.2.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
524 1.9.2.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
525 1.9.2.2 bouyer return;
526 1.9.2.2 bouyer sc->sc_rx_errs++;
527 1.9.2.2 bouyer if (usbd_ratecheck(&sc->sc_rx_notice)) {
528 1.9.2.2 bouyer printf("%s: %u usb errors on rx: %s\n",
529 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
530 1.9.2.2 bouyer usbd_errstr(status));
531 1.9.2.2 bouyer sc->sc_rx_errs = 0;
532 1.9.2.2 bouyer }
533 1.9.2.2 bouyer if (status == USBD_STALLED)
534 1.9.2.2 bouyer usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_RX]);
535 1.9.2.2 bouyer goto done;
536 1.9.2.2 bouyer }
537 1.9.2.2 bouyer
538 1.9.2.2 bouyer usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
539 1.9.2.2 bouyer
540 1.9.2.2 bouyer DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
541 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), __FUNCTION__, status, total_len));
542 1.9.2.2 bouyer
543 1.9.2.2 bouyer m = c->upl_mbuf;
544 1.9.2.2 bouyer memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len);
545 1.9.2.2 bouyer
546 1.9.2.2 bouyer ifp->if_ipackets++;
547 1.9.2.2 bouyer m->m_pkthdr.len = m->m_len = total_len;
548 1.9.2.2 bouyer
549 1.9.2.2 bouyer m->m_pkthdr.rcvif = ifp;
550 1.9.2.2 bouyer
551 1.9.2.5 bouyer s = splnet();
552 1.9.2.2 bouyer
553 1.9.2.2 bouyer /* XXX ugly */
554 1.9.2.2 bouyer if (upl_newbuf(sc, c, NULL) == ENOBUFS) {
555 1.9.2.2 bouyer ifp->if_ierrors++;
556 1.9.2.2 bouyer goto done1;
557 1.9.2.2 bouyer }
558 1.9.2.2 bouyer
559 1.9.2.2 bouyer #if NBPFILTER > 0
560 1.9.2.2 bouyer /*
561 1.9.2.2 bouyer * Handle BPF listeners. Let the BPF user see the packet, but
562 1.9.2.2 bouyer * don't pass it up to the ether_input() layer unless it's
563 1.9.2.2 bouyer * a broadcast packet, multicast packet, matches our ethernet
564 1.9.2.2 bouyer * address or the interface is in promiscuous mode.
565 1.9.2.2 bouyer */
566 1.9.2.2 bouyer if (ifp->if_bpf) {
567 1.9.2.2 bouyer BPF_MTAP(ifp, m);
568 1.9.2.2 bouyer }
569 1.9.2.2 bouyer #endif
570 1.9.2.2 bouyer
571 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
572 1.9.2.2 bouyer __FUNCTION__, m->m_len));
573 1.9.2.2 bouyer
574 1.9.2.2 bouyer IF_INPUT(ifp, m);
575 1.9.2.2 bouyer
576 1.9.2.2 bouyer done1:
577 1.9.2.2 bouyer splx(s);
578 1.9.2.2 bouyer
579 1.9.2.2 bouyer done:
580 1.9.2.2 bouyer #if 1
581 1.9.2.2 bouyer /* Setup new transfer. */
582 1.9.2.2 bouyer usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
583 1.9.2.2 bouyer c, c->upl_buf, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
584 1.9.2.2 bouyer USBD_NO_TIMEOUT, upl_rxeof);
585 1.9.2.2 bouyer usbd_transfer(c->upl_xfer);
586 1.9.2.2 bouyer
587 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev),
588 1.9.2.2 bouyer __FUNCTION__));
589 1.9.2.2 bouyer #endif
590 1.9.2.2 bouyer }
591 1.9.2.2 bouyer
592 1.9.2.2 bouyer /*
593 1.9.2.2 bouyer * A frame was downloaded to the chip. It's safe for us to clean up
594 1.9.2.2 bouyer * the list buffers.
595 1.9.2.2 bouyer */
596 1.9.2.2 bouyer Static void
597 1.9.2.2 bouyer upl_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
598 1.9.2.2 bouyer {
599 1.9.2.2 bouyer struct upl_chain *c = priv;
600 1.9.2.2 bouyer struct upl_softc *sc = c->upl_sc;
601 1.9.2.2 bouyer struct ifnet *ifp = &sc->sc_if;
602 1.9.2.2 bouyer int s;
603 1.9.2.2 bouyer
604 1.9.2.2 bouyer if (sc->sc_dying)
605 1.9.2.2 bouyer return;
606 1.9.2.2 bouyer
607 1.9.2.5 bouyer s = splnet();
608 1.9.2.2 bouyer
609 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->sc_dev),
610 1.9.2.2 bouyer __FUNCTION__, status));
611 1.9.2.2 bouyer
612 1.9.2.2 bouyer ifp->if_timer = 0;
613 1.9.2.2 bouyer ifp->if_flags &= ~IFF_OACTIVE;
614 1.9.2.2 bouyer
615 1.9.2.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
616 1.9.2.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
617 1.9.2.2 bouyer splx(s);
618 1.9.2.2 bouyer return;
619 1.9.2.2 bouyer }
620 1.9.2.2 bouyer ifp->if_oerrors++;
621 1.9.2.2 bouyer printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
622 1.9.2.2 bouyer usbd_errstr(status));
623 1.9.2.2 bouyer if (status == USBD_STALLED)
624 1.9.2.2 bouyer usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_TX]);
625 1.9.2.2 bouyer splx(s);
626 1.9.2.2 bouyer return;
627 1.9.2.2 bouyer }
628 1.9.2.2 bouyer
629 1.9.2.2 bouyer ifp->if_opackets++;
630 1.9.2.2 bouyer
631 1.9.2.2 bouyer m_freem(c->upl_mbuf);
632 1.9.2.2 bouyer c->upl_mbuf = NULL;
633 1.9.2.2 bouyer
634 1.9.2.2 bouyer if (ifp->if_snd.ifq_head != NULL)
635 1.9.2.2 bouyer upl_start(ifp);
636 1.9.2.2 bouyer
637 1.9.2.2 bouyer splx(s);
638 1.9.2.2 bouyer }
639 1.9.2.2 bouyer
640 1.9.2.2 bouyer Static int
641 1.9.2.2 bouyer upl_send(struct upl_softc *sc, struct mbuf *m, int idx)
642 1.9.2.2 bouyer {
643 1.9.2.2 bouyer int total_len;
644 1.9.2.2 bouyer struct upl_chain *c;
645 1.9.2.2 bouyer usbd_status err;
646 1.9.2.2 bouyer
647 1.9.2.2 bouyer c = &sc->sc_cdata.upl_tx_chain[idx];
648 1.9.2.2 bouyer
649 1.9.2.2 bouyer /*
650 1.9.2.2 bouyer * Copy the mbuf data into a contiguous buffer, leaving two
651 1.9.2.2 bouyer * bytes at the beginning to hold the frame length.
652 1.9.2.2 bouyer */
653 1.9.2.2 bouyer m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf);
654 1.9.2.2 bouyer c->upl_mbuf = m;
655 1.9.2.2 bouyer
656 1.9.2.2 bouyer total_len = m->m_pkthdr.len;
657 1.9.2.2 bouyer
658 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: total_len=%d\n",
659 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), __FUNCTION__, total_len));
660 1.9.2.2 bouyer
661 1.9.2.2 bouyer usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_TX],
662 1.9.2.2 bouyer c, c->upl_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
663 1.9.2.2 bouyer upl_txeof);
664 1.9.2.2 bouyer
665 1.9.2.2 bouyer /* Transmit */
666 1.9.2.2 bouyer err = usbd_transfer(c->upl_xfer);
667 1.9.2.2 bouyer if (err != USBD_IN_PROGRESS) {
668 1.9.2.2 bouyer printf("%s: upl_send error=%s\n", USBDEVNAME(sc->sc_dev),
669 1.9.2.2 bouyer usbd_errstr(err));
670 1.9.2.2 bouyer upl_stop(sc);
671 1.9.2.2 bouyer return (EIO);
672 1.9.2.2 bouyer }
673 1.9.2.2 bouyer
674 1.9.2.2 bouyer sc->sc_cdata.upl_tx_cnt++;
675 1.9.2.2 bouyer
676 1.9.2.2 bouyer return (0);
677 1.9.2.2 bouyer }
678 1.9.2.2 bouyer
679 1.9.2.2 bouyer Static void
680 1.9.2.2 bouyer upl_start(struct ifnet *ifp)
681 1.9.2.2 bouyer {
682 1.9.2.2 bouyer struct upl_softc *sc = ifp->if_softc;
683 1.9.2.2 bouyer struct mbuf *m_head = NULL;
684 1.9.2.2 bouyer
685 1.9.2.2 bouyer if (sc->sc_dying)
686 1.9.2.2 bouyer return;
687 1.9.2.2 bouyer
688 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
689 1.9.2.2 bouyer
690 1.9.2.2 bouyer if (ifp->if_flags & IFF_OACTIVE)
691 1.9.2.2 bouyer return;
692 1.9.2.2 bouyer
693 1.9.2.2 bouyer IF_DEQUEUE(&ifp->if_snd, m_head);
694 1.9.2.2 bouyer if (m_head == NULL)
695 1.9.2.2 bouyer return;
696 1.9.2.2 bouyer
697 1.9.2.2 bouyer if (upl_send(sc, m_head, 0)) {
698 1.9.2.2 bouyer IF_PREPEND(&ifp->if_snd, m_head);
699 1.9.2.2 bouyer ifp->if_flags |= IFF_OACTIVE;
700 1.9.2.2 bouyer return;
701 1.9.2.2 bouyer }
702 1.9.2.2 bouyer
703 1.9.2.2 bouyer #if NBPFILTER > 0
704 1.9.2.2 bouyer /*
705 1.9.2.2 bouyer * If there's a BPF listener, bounce a copy of this frame
706 1.9.2.2 bouyer * to him.
707 1.9.2.2 bouyer */
708 1.9.2.2 bouyer if (ifp->if_bpf)
709 1.9.2.2 bouyer BPF_MTAP(ifp, m_head);
710 1.9.2.2 bouyer #endif
711 1.9.2.2 bouyer
712 1.9.2.2 bouyer ifp->if_flags |= IFF_OACTIVE;
713 1.9.2.2 bouyer
714 1.9.2.2 bouyer /*
715 1.9.2.2 bouyer * Set a timeout in case the chip goes out to lunch.
716 1.9.2.2 bouyer */
717 1.9.2.2 bouyer ifp->if_timer = 5;
718 1.9.2.2 bouyer }
719 1.9.2.2 bouyer
720 1.9.2.2 bouyer Static void
721 1.9.2.2 bouyer upl_init(void *xsc)
722 1.9.2.2 bouyer {
723 1.9.2.2 bouyer struct upl_softc *sc = xsc;
724 1.9.2.2 bouyer struct ifnet *ifp = &sc->sc_if;
725 1.9.2.2 bouyer int s;
726 1.9.2.2 bouyer
727 1.9.2.2 bouyer if (sc->sc_dying)
728 1.9.2.2 bouyer return;
729 1.9.2.2 bouyer
730 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
731 1.9.2.2 bouyer
732 1.9.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
733 1.9.2.2 bouyer return;
734 1.9.2.2 bouyer
735 1.9.2.5 bouyer s = splnet();
736 1.9.2.2 bouyer
737 1.9.2.2 bouyer /* Init TX ring. */
738 1.9.2.2 bouyer if (upl_tx_list_init(sc) == ENOBUFS) {
739 1.9.2.2 bouyer printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
740 1.9.2.2 bouyer splx(s);
741 1.9.2.2 bouyer return;
742 1.9.2.2 bouyer }
743 1.9.2.2 bouyer
744 1.9.2.2 bouyer /* Init RX ring. */
745 1.9.2.2 bouyer if (upl_rx_list_init(sc) == ENOBUFS) {
746 1.9.2.2 bouyer printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
747 1.9.2.2 bouyer splx(s);
748 1.9.2.2 bouyer return;
749 1.9.2.2 bouyer }
750 1.9.2.2 bouyer
751 1.9.2.2 bouyer if (sc->sc_ep[UPL_ENDPT_RX] == NULL) {
752 1.9.2.2 bouyer if (upl_openpipes(sc)) {
753 1.9.2.2 bouyer splx(s);
754 1.9.2.2 bouyer return;
755 1.9.2.2 bouyer }
756 1.9.2.2 bouyer }
757 1.9.2.2 bouyer
758 1.9.2.2 bouyer ifp->if_flags |= IFF_RUNNING;
759 1.9.2.2 bouyer ifp->if_flags &= ~IFF_OACTIVE;
760 1.9.2.2 bouyer
761 1.9.2.2 bouyer splx(s);
762 1.9.2.2 bouyer }
763 1.9.2.2 bouyer
764 1.9.2.2 bouyer Static int
765 1.9.2.2 bouyer upl_openpipes(struct upl_softc *sc)
766 1.9.2.2 bouyer {
767 1.9.2.2 bouyer struct upl_chain *c;
768 1.9.2.2 bouyer usbd_status err;
769 1.9.2.2 bouyer int i;
770 1.9.2.2 bouyer
771 1.9.2.2 bouyer /* Open RX and TX pipes. */
772 1.9.2.2 bouyer err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX],
773 1.9.2.2 bouyer USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]);
774 1.9.2.2 bouyer if (err) {
775 1.9.2.2 bouyer printf("%s: open rx pipe failed: %s\n",
776 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
777 1.9.2.2 bouyer return (EIO);
778 1.9.2.2 bouyer }
779 1.9.2.2 bouyer err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX],
780 1.9.2.2 bouyer USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]);
781 1.9.2.2 bouyer if (err) {
782 1.9.2.2 bouyer printf("%s: open tx pipe failed: %s\n",
783 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
784 1.9.2.2 bouyer return (EIO);
785 1.9.2.2 bouyer }
786 1.9.2.2 bouyer err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR],
787 1.9.2.2 bouyer USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc,
788 1.9.2.2 bouyer &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr,
789 1.9.2.2 bouyer UPL_INTR_INTERVAL);
790 1.9.2.2 bouyer if (err) {
791 1.9.2.2 bouyer printf("%s: open intr pipe failed: %s\n",
792 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
793 1.9.2.2 bouyer return (EIO);
794 1.9.2.2 bouyer }
795 1.9.2.2 bouyer
796 1.9.2.2 bouyer
797 1.9.2.2 bouyer #if 1
798 1.9.2.2 bouyer /* Start up the receive pipe. */
799 1.9.2.2 bouyer for (i = 0; i < UPL_RX_LIST_CNT; i++) {
800 1.9.2.2 bouyer c = &sc->sc_cdata.upl_rx_chain[i];
801 1.9.2.2 bouyer usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX],
802 1.9.2.2 bouyer c, c->upl_buf, UPL_BUFSZ,
803 1.9.2.2 bouyer USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
804 1.9.2.2 bouyer upl_rxeof);
805 1.9.2.2 bouyer usbd_transfer(c->upl_xfer);
806 1.9.2.2 bouyer }
807 1.9.2.2 bouyer #endif
808 1.9.2.2 bouyer
809 1.9.2.2 bouyer return (0);
810 1.9.2.2 bouyer }
811 1.9.2.2 bouyer
812 1.9.2.2 bouyer Static void
813 1.9.2.2 bouyer upl_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
814 1.9.2.2 bouyer {
815 1.9.2.2 bouyer struct upl_softc *sc = priv;
816 1.9.2.2 bouyer struct ifnet *ifp = &sc->sc_if;
817 1.9.2.2 bouyer uByte stat;
818 1.9.2.2 bouyer
819 1.9.2.2 bouyer DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
820 1.9.2.2 bouyer
821 1.9.2.2 bouyer if (sc->sc_dying)
822 1.9.2.2 bouyer return;
823 1.9.2.2 bouyer
824 1.9.2.2 bouyer if (!(ifp->if_flags & IFF_RUNNING))
825 1.9.2.2 bouyer return;
826 1.9.2.2 bouyer
827 1.9.2.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
828 1.9.2.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
829 1.9.2.2 bouyer return;
830 1.9.2.2 bouyer }
831 1.9.2.2 bouyer sc->sc_intr_errs++;
832 1.9.2.2 bouyer if (usbd_ratecheck(&sc->sc_rx_notice)) {
833 1.9.2.2 bouyer printf("%s: %u usb errors on intr: %s\n",
834 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
835 1.9.2.2 bouyer usbd_errstr(status));
836 1.9.2.2 bouyer sc->sc_intr_errs = 0;
837 1.9.2.2 bouyer }
838 1.9.2.2 bouyer if (status == USBD_STALLED)
839 1.9.2.2 bouyer usbd_clear_endpoint_stall(sc->sc_ep[UPL_ENDPT_RX]);
840 1.9.2.2 bouyer return;
841 1.9.2.2 bouyer }
842 1.9.2.2 bouyer
843 1.9.2.2 bouyer stat = sc->sc_ibuf;
844 1.9.2.2 bouyer
845 1.9.2.2 bouyer if (stat == 0)
846 1.9.2.2 bouyer return;
847 1.9.2.2 bouyer
848 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: stat=0x%02x\n", USBDEVNAME(sc->sc_dev),
849 1.9.2.2 bouyer __FUNCTION__, stat));
850 1.9.2.2 bouyer
851 1.9.2.2 bouyer }
852 1.9.2.2 bouyer
853 1.9.2.2 bouyer Static int
854 1.9.2.2 bouyer upl_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
855 1.9.2.2 bouyer {
856 1.9.2.2 bouyer struct upl_softc *sc = ifp->if_softc;
857 1.9.2.2 bouyer struct ifaddr *ifa = (struct ifaddr *)data;
858 1.9.2.2 bouyer struct ifreq *ifr = (struct ifreq *)data;
859 1.9.2.2 bouyer int s, error = 0;
860 1.9.2.2 bouyer
861 1.9.2.2 bouyer if (sc->sc_dying)
862 1.9.2.2 bouyer return (EIO);
863 1.9.2.2 bouyer
864 1.9.2.2 bouyer DPRINTFN(5,("%s: %s: cmd=0x%08lx\n",
865 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), __FUNCTION__, command));
866 1.9.2.2 bouyer
867 1.9.2.5 bouyer s = splnet();
868 1.9.2.2 bouyer
869 1.9.2.2 bouyer switch(command) {
870 1.9.2.2 bouyer case SIOCSIFADDR:
871 1.9.2.2 bouyer ifp->if_flags |= IFF_UP;
872 1.9.2.2 bouyer upl_init(sc);
873 1.9.2.2 bouyer
874 1.9.2.2 bouyer switch (ifa->ifa_addr->sa_family) {
875 1.9.2.2 bouyer #ifdef INET
876 1.9.2.2 bouyer case AF_INET:
877 1.9.2.2 bouyer break;
878 1.9.2.2 bouyer #endif /* INET */
879 1.9.2.2 bouyer #ifdef NS
880 1.9.2.2 bouyer case AF_NS:
881 1.9.2.2 bouyer {
882 1.9.2.2 bouyer struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
883 1.9.2.2 bouyer
884 1.9.2.2 bouyer if (ns_nullhost(*ina))
885 1.9.2.2 bouyer ina->x_host = *(union ns_host *)
886 1.9.2.2 bouyer LLADDR(ifp->if_sadl);
887 1.9.2.2 bouyer else
888 1.9.2.2 bouyer memcpy(LLADDR(ifp->if_sadl),
889 1.9.2.2 bouyer ina->x_host.c_host,
890 1.9.2.2 bouyer ifp->if_addrlen);
891 1.9.2.2 bouyer break;
892 1.9.2.2 bouyer }
893 1.9.2.2 bouyer #endif /* NS */
894 1.9.2.2 bouyer }
895 1.9.2.2 bouyer break;
896 1.9.2.2 bouyer
897 1.9.2.2 bouyer case SIOCSIFMTU:
898 1.9.2.2 bouyer if (ifr->ifr_mtu > UPL_BUFSZ)
899 1.9.2.2 bouyer error = EINVAL;
900 1.9.2.2 bouyer else
901 1.9.2.2 bouyer ifp->if_mtu = ifr->ifr_mtu;
902 1.9.2.2 bouyer break;
903 1.9.2.2 bouyer
904 1.9.2.2 bouyer case SIOCSIFFLAGS:
905 1.9.2.2 bouyer if (ifp->if_flags & IFF_UP) {
906 1.9.2.2 bouyer if (!(ifp->if_flags & IFF_RUNNING))
907 1.9.2.2 bouyer upl_init(sc);
908 1.9.2.2 bouyer } else {
909 1.9.2.2 bouyer if (ifp->if_flags & IFF_RUNNING)
910 1.9.2.2 bouyer upl_stop(sc);
911 1.9.2.2 bouyer }
912 1.9.2.2 bouyer error = 0;
913 1.9.2.2 bouyer break;
914 1.9.2.2 bouyer default:
915 1.9.2.2 bouyer error = EINVAL;
916 1.9.2.2 bouyer break;
917 1.9.2.2 bouyer }
918 1.9.2.2 bouyer
919 1.9.2.2 bouyer splx(s);
920 1.9.2.2 bouyer
921 1.9.2.2 bouyer return (error);
922 1.9.2.2 bouyer }
923 1.9.2.2 bouyer
924 1.9.2.2 bouyer Static void
925 1.9.2.2 bouyer upl_watchdog(struct ifnet *ifp)
926 1.9.2.2 bouyer {
927 1.9.2.2 bouyer struct upl_softc *sc = ifp->if_softc;
928 1.9.2.2 bouyer
929 1.9.2.2 bouyer DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
930 1.9.2.2 bouyer
931 1.9.2.2 bouyer if (sc->sc_dying)
932 1.9.2.2 bouyer return;
933 1.9.2.2 bouyer
934 1.9.2.2 bouyer ifp->if_oerrors++;
935 1.9.2.2 bouyer printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
936 1.9.2.2 bouyer
937 1.9.2.2 bouyer upl_stop(sc);
938 1.9.2.2 bouyer upl_init(sc);
939 1.9.2.2 bouyer
940 1.9.2.2 bouyer if (ifp->if_snd.ifq_head != NULL)
941 1.9.2.2 bouyer upl_start(ifp);
942 1.9.2.2 bouyer }
943 1.9.2.2 bouyer
944 1.9.2.2 bouyer /*
945 1.9.2.2 bouyer * Stop the adapter and free any mbufs allocated to the
946 1.9.2.2 bouyer * RX and TX lists.
947 1.9.2.2 bouyer */
948 1.9.2.2 bouyer Static void
949 1.9.2.2 bouyer upl_stop(struct upl_softc *sc)
950 1.9.2.2 bouyer {
951 1.9.2.2 bouyer usbd_status err;
952 1.9.2.2 bouyer struct ifnet *ifp;
953 1.9.2.2 bouyer int i;
954 1.9.2.2 bouyer
955 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__FUNCTION__));
956 1.9.2.2 bouyer
957 1.9.2.2 bouyer ifp = &sc->sc_if;
958 1.9.2.2 bouyer ifp->if_timer = 0;
959 1.9.2.2 bouyer
960 1.9.2.2 bouyer /* Stop transfers. */
961 1.9.2.2 bouyer if (sc->sc_ep[UPL_ENDPT_RX] != NULL) {
962 1.9.2.2 bouyer err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]);
963 1.9.2.2 bouyer if (err) {
964 1.9.2.2 bouyer printf("%s: abort rx pipe failed: %s\n",
965 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
966 1.9.2.2 bouyer }
967 1.9.2.2 bouyer err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]);
968 1.9.2.2 bouyer if (err) {
969 1.9.2.2 bouyer printf("%s: close rx pipe failed: %s\n",
970 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
971 1.9.2.2 bouyer }
972 1.9.2.2 bouyer sc->sc_ep[UPL_ENDPT_RX] = NULL;
973 1.9.2.2 bouyer }
974 1.9.2.2 bouyer
975 1.9.2.2 bouyer if (sc->sc_ep[UPL_ENDPT_TX] != NULL) {
976 1.9.2.2 bouyer err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]);
977 1.9.2.2 bouyer if (err) {
978 1.9.2.2 bouyer printf("%s: abort tx pipe failed: %s\n",
979 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
980 1.9.2.2 bouyer }
981 1.9.2.2 bouyer err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]);
982 1.9.2.2 bouyer if (err) {
983 1.9.2.2 bouyer printf("%s: close tx pipe failed: %s\n",
984 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
985 1.9.2.2 bouyer }
986 1.9.2.2 bouyer sc->sc_ep[UPL_ENDPT_TX] = NULL;
987 1.9.2.2 bouyer }
988 1.9.2.2 bouyer
989 1.9.2.2 bouyer if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) {
990 1.9.2.2 bouyer err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
991 1.9.2.2 bouyer if (err) {
992 1.9.2.2 bouyer printf("%s: abort intr pipe failed: %s\n",
993 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
994 1.9.2.2 bouyer }
995 1.9.2.2 bouyer err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]);
996 1.9.2.2 bouyer if (err) {
997 1.9.2.2 bouyer printf("%s: close intr pipe failed: %s\n",
998 1.9.2.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
999 1.9.2.2 bouyer }
1000 1.9.2.2 bouyer sc->sc_ep[UPL_ENDPT_INTR] = NULL;
1001 1.9.2.2 bouyer }
1002 1.9.2.2 bouyer
1003 1.9.2.2 bouyer /* Free RX resources. */
1004 1.9.2.2 bouyer for (i = 0; i < UPL_RX_LIST_CNT; i++) {
1005 1.9.2.2 bouyer if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) {
1006 1.9.2.2 bouyer m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf);
1007 1.9.2.2 bouyer sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL;
1008 1.9.2.2 bouyer }
1009 1.9.2.2 bouyer if (sc->sc_cdata.upl_rx_chain[i].upl_xfer != NULL) {
1010 1.9.2.2 bouyer usbd_free_xfer(sc->sc_cdata.upl_rx_chain[i].upl_xfer);
1011 1.9.2.2 bouyer sc->sc_cdata.upl_rx_chain[i].upl_xfer = NULL;
1012 1.9.2.2 bouyer }
1013 1.9.2.2 bouyer }
1014 1.9.2.2 bouyer
1015 1.9.2.2 bouyer /* Free TX resources. */
1016 1.9.2.2 bouyer for (i = 0; i < UPL_TX_LIST_CNT; i++) {
1017 1.9.2.2 bouyer if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) {
1018 1.9.2.2 bouyer m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf);
1019 1.9.2.2 bouyer sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL;
1020 1.9.2.2 bouyer }
1021 1.9.2.2 bouyer if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) {
1022 1.9.2.2 bouyer usbd_free_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer);
1023 1.9.2.2 bouyer sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL;
1024 1.9.2.2 bouyer }
1025 1.9.2.2 bouyer }
1026 1.9.2.2 bouyer
1027 1.9.2.2 bouyer ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1028 1.9.2.2 bouyer }
1029 1.9.2.2 bouyer
1030 1.9.2.2 bouyer Static int
1031 1.9.2.2 bouyer upl_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1032 1.9.2.2 bouyer struct rtentry *rt0)
1033 1.9.2.2 bouyer {
1034 1.9.2.2 bouyer int s;
1035 1.9.2.2 bouyer
1036 1.9.2.2 bouyer DPRINTFN(10,("%s: %s: enter\n",
1037 1.9.2.2 bouyer USBDEVNAME(((struct upl_softc *)ifp->if_softc)->sc_dev),
1038 1.9.2.2 bouyer __FUNCTION__));
1039 1.9.2.2 bouyer
1040 1.9.2.5 bouyer s = splnet();
1041 1.9.2.2 bouyer /*
1042 1.9.2.2 bouyer * Queue message on interface, and start output if interface
1043 1.9.2.2 bouyer * not yet active.
1044 1.9.2.2 bouyer */
1045 1.9.2.2 bouyer if (IF_QFULL(&ifp->if_snd)) {
1046 1.9.2.2 bouyer IF_DROP(&ifp->if_snd);
1047 1.9.2.2 bouyer splx(s);
1048 1.9.2.2 bouyer return (ENOBUFS);
1049 1.9.2.2 bouyer }
1050 1.9.2.2 bouyer ifp->if_obytes += m->m_pkthdr.len;
1051 1.9.2.2 bouyer IF_ENQUEUE(&ifp->if_snd, m);
1052 1.9.2.2 bouyer if ((ifp->if_flags & IFF_OACTIVE) == 0)
1053 1.9.2.2 bouyer (*ifp->if_start)(ifp);
1054 1.9.2.2 bouyer splx(s);
1055 1.9.2.2 bouyer
1056 1.9.2.2 bouyer return (0);
1057 1.9.2.2 bouyer }
1058 1.9.2.2 bouyer
1059 1.9.2.2 bouyer Static void
1060 1.9.2.2 bouyer upl_input(struct ifnet *ifp, struct mbuf *m)
1061 1.9.2.2 bouyer {
1062 1.9.2.2 bouyer struct ifqueue *inq;
1063 1.9.2.2 bouyer int s;
1064 1.9.2.2 bouyer
1065 1.9.2.2 bouyer /* XXX Assume all traffic is IP */
1066 1.9.2.2 bouyer
1067 1.9.2.2 bouyer schednetisr(NETISR_IP);
1068 1.9.2.2 bouyer inq = &ipintrq;
1069 1.9.2.2 bouyer
1070 1.9.2.5 bouyer s = splnet();
1071 1.9.2.2 bouyer if (IF_QFULL(inq)) {
1072 1.9.2.2 bouyer IF_DROP(inq);
1073 1.9.2.2 bouyer splx(s);
1074 1.9.2.2 bouyer #if 0
1075 1.9.2.2 bouyer if (sc->sc_flags & SC_DEBUG)
1076 1.9.2.2 bouyer printf("%s: input queue full\n", ifp->if_xname);
1077 1.9.2.2 bouyer #endif
1078 1.9.2.2 bouyer ifp->if_iqdrops++;
1079 1.9.2.2 bouyer return;
1080 1.9.2.2 bouyer }
1081 1.9.2.2 bouyer IF_ENQUEUE(inq, m);
1082 1.9.2.2 bouyer splx(s);
1083 1.9.2.2 bouyer ifp->if_ipackets++;
1084 1.9.2.2 bouyer ifp->if_ibytes += m->m_len;
1085 1.9.2.2 bouyer ifp->if_lastchange = time;
1086 1.9.2.2 bouyer }
1087