if_cdce.c revision 1.38.14.12 1 1.38.14.12 skrll /* $NetBSD: if_cdce.c,v 1.38.14.12 2017/02/05 13:40:46 skrll Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul (at) windriver.com>
5 1.1 augustss * Copyright (c) 2003 Craig Boston
6 1.1 augustss * Copyright (c) 2004 Daniel Hartmeier
7 1.1 augustss * All rights reserved.
8 1.1 augustss *
9 1.1 augustss * Redistribution and use in source and binary forms, with or without
10 1.1 augustss * modification, are permitted provided that the following conditions
11 1.1 augustss * are met:
12 1.1 augustss * 1. Redistributions of source code must retain the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer.
14 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer in the
16 1.1 augustss * documentation and/or other materials provided with the distribution.
17 1.1 augustss * 3. All advertising materials mentioning features or use of this software
18 1.1 augustss * must display the following acknowledgement:
19 1.1 augustss * This product includes software developed by Bill Paul.
20 1.1 augustss * 4. Neither the name of the author nor the names of any co-contributors
21 1.1 augustss * may be used to endorse or promote products derived from this software
22 1.1 augustss * without specific prior written permission.
23 1.1 augustss *
24 1.1 augustss * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
25 1.1 augustss * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 augustss * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 augustss * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
28 1.1 augustss * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.1 augustss * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.1 augustss * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 1.1 augustss * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 1.1 augustss * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 1.1 augustss * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 1.1 augustss * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.1 augustss */
36 1.1 augustss
37 1.1 augustss /*
38 1.1 augustss * USB Communication Device Class (Ethernet Networking Control Model)
39 1.1 augustss * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
40 1.1 augustss *
41 1.1 augustss */
42 1.1 augustss
43 1.1 augustss #include <sys/cdefs.h>
44 1.38.14.12 skrll __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.38.14.12 2017/02/05 13:40:46 skrll Exp $");
45 1.36 mrg
46 1.38 christos #ifdef _KERNEL_OPT
47 1.18 ws #include "opt_inet.h"
48 1.18 ws #endif
49 1.1 augustss
50 1.1 augustss #include <sys/param.h>
51 1.1 augustss #include <sys/systm.h>
52 1.1 augustss #include <sys/sockio.h>
53 1.1 augustss #include <sys/mbuf.h>
54 1.1 augustss #include <sys/kernel.h>
55 1.1 augustss #include <sys/socket.h>
56 1.1 augustss #include <sys/device.h>
57 1.1 augustss
58 1.1 augustss #include <net/if.h>
59 1.1 augustss #include <net/if_arp.h>
60 1.1 augustss #include <net/if_dl.h>
61 1.1 augustss #include <net/if_media.h>
62 1.1 augustss
63 1.1 augustss #include <net/bpf.h>
64 1.1 augustss
65 1.1 augustss #include <net/if_ether.h>
66 1.1 augustss #ifdef INET
67 1.1 augustss #include <netinet/in.h>
68 1.1 augustss #include <netinet/if_inarp.h>
69 1.1 augustss #endif
70 1.1 augustss
71 1.1 augustss
72 1.1 augustss
73 1.1 augustss #include <dev/usb/usb.h>
74 1.1 augustss #include <dev/usb/usbdi.h>
75 1.1 augustss #include <dev/usb/usbdi_util.h>
76 1.1 augustss #include <dev/usb/usbdevs.h>
77 1.1 augustss #include <dev/usb/usbcdc.h>
78 1.1 augustss
79 1.1 augustss #include <dev/usb/if_cdcereg.h>
80 1.1 augustss
81 1.1 augustss Static int cdce_tx_list_init(struct cdce_softc *);
82 1.38.14.10 skrll Static void cdce_tx_list_free(struct cdce_softc *);
83 1.1 augustss Static int cdce_rx_list_init(struct cdce_softc *);
84 1.38.14.10 skrll Static void cdce_rx_list_free(struct cdce_softc *);
85 1.1 augustss Static int cdce_newbuf(struct cdce_softc *, struct cdce_chain *,
86 1.1 augustss struct mbuf *);
87 1.1 augustss Static int cdce_encap(struct cdce_softc *, struct mbuf *, int);
88 1.38.14.3 skrll Static void cdce_rxeof(struct usbd_xfer *, void *, usbd_status);
89 1.38.14.3 skrll Static void cdce_txeof(struct usbd_xfer *, void *, usbd_status);
90 1.1 augustss Static void cdce_start(struct ifnet *);
91 1.38.14.10 skrll Static void cdce_start_locked(struct ifnet *);
92 1.13 christos Static int cdce_ioctl(struct ifnet *, u_long, void *);
93 1.38.14.10 skrll Static int cdce_init(struct ifnet *);
94 1.38.14.10 skrll Static int cdce_init_locked(struct ifnet *);
95 1.1 augustss Static void cdce_watchdog(struct ifnet *);
96 1.38.14.10 skrll Static void cdce_stop(struct ifnet *, int);
97 1.38.14.10 skrll Static void cdce_stop_locked(struct ifnet *, int);
98 1.1 augustss
99 1.1 augustss Static const struct cdce_type cdce_devs[] = {
100 1.33 msaitoh {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION },
101 1.33 msaitoh {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
102 1.33 msaitoh {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
103 1.33 msaitoh {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION },
104 1.33 msaitoh {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION },
105 1.1 augustss {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION },
106 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS },
107 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION },
108 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION },
109 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION },
110 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION },
111 1.1 augustss };
112 1.33 msaitoh #define cdce_lookup(v, p) \
113 1.33 msaitoh ((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
114 1.1 augustss
115 1.23 dyoung int cdce_match(device_t, cfdata_t, void *);
116 1.23 dyoung void cdce_attach(device_t, device_t, void *);
117 1.23 dyoung int cdce_detach(device_t, int);
118 1.23 dyoung int cdce_activate(device_t, enum devact);
119 1.23 dyoung extern struct cfdriver cdce_cd;
120 1.23 dyoung CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach,
121 1.23 dyoung cdce_detach, cdce_activate);
122 1.1 augustss
123 1.23 dyoung int
124 1.23 dyoung cdce_match(device_t parent, cfdata_t match, void *aux)
125 1.1 augustss {
126 1.38.14.4 skrll struct usbif_attach_arg *uiaa = aux;
127 1.1 augustss
128 1.38.14.4 skrll if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
129 1.38.14.2 skrll return UMATCH_VENDOR_PRODUCT;
130 1.1 augustss
131 1.38.14.4 skrll if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
132 1.1 augustss UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
133 1.38.14.2 skrll return UMATCH_IFACECLASS_GENERIC;
134 1.1 augustss
135 1.38.14.2 skrll return UMATCH_NONE;
136 1.1 augustss }
137 1.1 augustss
138 1.23 dyoung void
139 1.23 dyoung cdce_attach(device_t parent, device_t self, void *aux)
140 1.1 augustss {
141 1.23 dyoung struct cdce_softc *sc = device_private(self);
142 1.38.14.4 skrll struct usbif_attach_arg *uiaa = aux;
143 1.33 msaitoh char *devinfop;
144 1.1 augustss struct ifnet *ifp;
145 1.38.14.4 skrll struct usbd_device *dev = uiaa->uiaa_device;
146 1.1 augustss const struct cdce_type *t;
147 1.1 augustss usb_interface_descriptor_t *id;
148 1.1 augustss usb_endpoint_descriptor_t *ed;
149 1.2 augustss const usb_cdc_union_descriptor_t *ud;
150 1.22 tron usb_config_descriptor_t *cd;
151 1.1 augustss int data_ifcno;
152 1.22 tron int i, j, numalts;
153 1.1 augustss u_char eaddr[ETHER_ADDR_LEN];
154 1.2 augustss const usb_cdc_ethernet_descriptor_t *ue;
155 1.5 augustss char eaddr_str[USB_MAX_ENCODED_STRING_LEN];
156 1.1 augustss
157 1.25 plunky sc->cdce_dev = self;
158 1.25 plunky
159 1.23 dyoung aprint_naive("\n");
160 1.23 dyoung aprint_normal("\n");
161 1.25 plunky
162 1.25 plunky devinfop = usbd_devinfo_alloc(dev, 0);
163 1.16 cube aprint_normal_dev(self, "%s\n", devinfop);
164 1.6 augustss usbd_devinfo_free(devinfop);
165 1.1 augustss
166 1.38.14.10 skrll mutex_init(&sc->cdce_lock, MUTEX_DEFAULT, IPL_NONE);
167 1.38.14.10 skrll mutex_init(&sc->cdce_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
168 1.38.14.10 skrll mutex_init(&sc->cdce_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
169 1.38.14.10 skrll
170 1.38.14.4 skrll sc->cdce_udev = uiaa->uiaa_device;
171 1.38.14.4 skrll sc->cdce_ctl_iface = uiaa->uiaa_iface;
172 1.1 augustss
173 1.38.14.4 skrll t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
174 1.1 augustss if (t)
175 1.1 augustss sc->cdce_flags = t->cdce_flags;
176 1.1 augustss
177 1.1 augustss if (sc->cdce_flags & CDCE_NO_UNION)
178 1.1 augustss sc->cdce_data_iface = sc->cdce_ctl_iface;
179 1.1 augustss else {
180 1.7 christos ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev,
181 1.2 augustss UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
182 1.1 augustss if (ud == NULL) {
183 1.16 cube aprint_error_dev(self, "no union descriptor\n");
184 1.24 dyoung return;
185 1.1 augustss }
186 1.1 augustss data_ifcno = ud->bSlaveInterface[0];
187 1.1 augustss
188 1.38.14.4 skrll for (i = 0; i < uiaa->uiaa_nifaces; i++) {
189 1.38.14.4 skrll if (uiaa->uiaa_ifaces[i] != NULL) {
190 1.1 augustss id = usbd_get_interface_descriptor(
191 1.38.14.4 skrll uiaa->uiaa_ifaces[i]);
192 1.1 augustss if (id != NULL && id->bInterfaceNumber ==
193 1.1 augustss data_ifcno) {
194 1.38.14.4 skrll sc->cdce_data_iface = uiaa->uiaa_ifaces[i];
195 1.38.14.4 skrll uiaa->uiaa_ifaces[i] = NULL;
196 1.1 augustss }
197 1.1 augustss }
198 1.1 augustss }
199 1.1 augustss }
200 1.1 augustss
201 1.1 augustss if (sc->cdce_data_iface == NULL) {
202 1.16 cube aprint_error_dev(self, "no data interface\n");
203 1.24 dyoung return;
204 1.1 augustss }
205 1.1 augustss
206 1.22 tron /*
207 1.22 tron * <quote>
208 1.22 tron * The Data Class interface of a networking device shall have a minimum
209 1.22 tron * of two interface settings. The first setting (the default interface
210 1.22 tron * setting) includes no endpoints and therefore no networking traffic is
211 1.22 tron * exchanged whenever the default interface setting is selected. One or
212 1.22 tron * more additional interface settings are used for normal operation, and
213 1.22 tron * therefore each includes a pair of endpoints (one IN, and one OUT) to
214 1.22 tron * exchange network traffic. Select an alternate interface setting to
215 1.22 tron * initialize the network aspects of the device and to enable the
216 1.22 tron * exchange of network traffic.
217 1.22 tron * </quote>
218 1.22 tron *
219 1.22 tron * Some devices, most notably cable modems, include interface settings
220 1.22 tron * that have no IN or OUT endpoint, therefore loop through the list of all
221 1.22 tron * available interface settings looking for one with both IN and OUT
222 1.22 tron * endpoints.
223 1.22 tron */
224 1.1 augustss id = usbd_get_interface_descriptor(sc->cdce_data_iface);
225 1.22 tron cd = usbd_get_config_descriptor(sc->cdce_udev);
226 1.22 tron numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
227 1.22 tron
228 1.22 tron for (j = 0; j < numalts; j++) {
229 1.22 tron if (usbd_set_interface(sc->cdce_data_iface, j)) {
230 1.22 tron aprint_error_dev(sc->cdce_dev,
231 1.22 tron "setting alternate interface failed\n");
232 1.24 dyoung return;
233 1.1 augustss }
234 1.22 tron /* Find endpoints. */
235 1.22 tron id = usbd_get_interface_descriptor(sc->cdce_data_iface);
236 1.22 tron sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1;
237 1.22 tron for (i = 0; i < id->bNumEndpoints; i++) {
238 1.22 tron ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i);
239 1.22 tron if (!ed) {
240 1.22 tron aprint_error_dev(self,
241 1.22 tron "could not read endpoint descriptor\n");
242 1.24 dyoung return;
243 1.22 tron }
244 1.22 tron if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
245 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
246 1.22 tron sc->cdce_bulkin_no = ed->bEndpointAddress;
247 1.22 tron } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
248 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
249 1.22 tron sc->cdce_bulkout_no = ed->bEndpointAddress;
250 1.22 tron } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
251 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
252 1.22 tron /* XXX: CDC spec defines an interrupt pipe, but it is not
253 1.22 tron * needed for simple host-to-host applications. */
254 1.22 tron } else {
255 1.22 tron aprint_error_dev(self, "unexpected endpoint\n");
256 1.22 tron }
257 1.1 augustss }
258 1.22 tron /* If we found something, try and use it... */
259 1.22 tron if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1))
260 1.22 tron break;
261 1.1 augustss }
262 1.1 augustss
263 1.1 augustss if (sc->cdce_bulkin_no == -1) {
264 1.16 cube aprint_error_dev(self, "could not find data bulk in\n");
265 1.24 dyoung return;
266 1.1 augustss }
267 1.1 augustss if (sc->cdce_bulkout_no == -1 ) {
268 1.16 cube aprint_error_dev(self, "could not find data bulk out\n");
269 1.24 dyoung return;
270 1.1 augustss }
271 1.1 augustss
272 1.7 christos ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
273 1.30 jakllsch UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF);
274 1.30 jakllsch if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
275 1.30 jakllsch ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) {
276 1.16 cube aprint_normal_dev(self, "faking address\n");
277 1.2 augustss eaddr[0]= 0x2a;
278 1.30 jakllsch memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t));
279 1.30 jakllsch eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev));
280 1.2 augustss }
281 1.2 augustss
282 1.16 cube aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr));
283 1.1 augustss
284 1.1 augustss ifp = GET_IFP(sc);
285 1.1 augustss ifp->if_softc = sc;
286 1.1 augustss ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287 1.38.14.10 skrll ifp->if_extflags = IFEF_START_MPSAFE;
288 1.38.14.10 skrll ifp->if_init = cdce_init;
289 1.1 augustss ifp->if_ioctl = cdce_ioctl;
290 1.1 augustss ifp->if_start = cdce_start;
291 1.1 augustss ifp->if_watchdog = cdce_watchdog;
292 1.38.14.12 skrll strlcpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ);
293 1.1 augustss
294 1.1 augustss IFQ_SET_READY(&ifp->if_snd);
295 1.1 augustss
296 1.38.14.10 skrll if_initialize(ifp);
297 1.38.14.10 skrll sc->cdce_ipq = if_percpuq_create(&sc->cdce_ec.ec_if);
298 1.24 dyoung ether_ifattach(ifp, eaddr);
299 1.38.14.10 skrll if_register(ifp);
300 1.1 augustss
301 1.1 augustss sc->cdce_attached = 1;
302 1.1 augustss
303 1.1 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev,
304 1.24 dyoung sc->cdce_dev);
305 1.1 augustss
306 1.31 jakllsch if (!pmf_device_register(self, NULL, NULL))
307 1.31 jakllsch aprint_error_dev(self, "couldn't establish power handler\n");
308 1.31 jakllsch
309 1.24 dyoung return;
310 1.1 augustss }
311 1.1 augustss
312 1.23 dyoung int
313 1.23 dyoung cdce_detach(device_t self, int flags)
314 1.1 augustss {
315 1.24 dyoung struct cdce_softc *sc = device_private(self);
316 1.1 augustss struct ifnet *ifp = GET_IFP(sc);
317 1.1 augustss int s;
318 1.1 augustss
319 1.37 plunky pmf_device_deregister(self);
320 1.31 jakllsch
321 1.1 augustss s = splusb();
322 1.1 augustss
323 1.1 augustss if (!sc->cdce_attached) {
324 1.1 augustss splx(s);
325 1.38.14.2 skrll return 0;
326 1.1 augustss }
327 1.1 augustss
328 1.1 augustss if (ifp->if_flags & IFF_RUNNING)
329 1.38.14.10 skrll cdce_stop(ifp, 1);
330 1.1 augustss
331 1.1 augustss ether_ifdetach(ifp);
332 1.1 augustss
333 1.1 augustss if_detach(ifp);
334 1.1 augustss
335 1.38.14.11 skrll mutex_destroy(&sc->cdce_rxlock);
336 1.38.14.11 skrll mutex_destroy(&sc->cdce_txlock);
337 1.38.14.11 skrll mutex_destroy(&sc->cdce_lock);
338 1.38.14.11 skrll
339 1.1 augustss sc->cdce_attached = 0;
340 1.1 augustss splx(s);
341 1.1 augustss
342 1.38.14.2 skrll return 0;
343 1.1 augustss }
344 1.1 augustss
345 1.1 augustss Static void
346 1.1 augustss cdce_start(struct ifnet *ifp)
347 1.1 augustss {
348 1.1 augustss struct cdce_softc *sc = ifp->if_softc;
349 1.38.14.10 skrll KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
350 1.38.14.10 skrll
351 1.38.14.10 skrll mutex_enter(&sc->cdce_txlock);
352 1.38.14.10 skrll cdce_start_locked(ifp);
353 1.38.14.10 skrll mutex_exit(&sc->cdce_txlock);
354 1.38.14.10 skrll }
355 1.38.14.10 skrll
356 1.38.14.10 skrll Static void
357 1.38.14.10 skrll cdce_start_locked(struct ifnet *ifp)
358 1.38.14.10 skrll {
359 1.38.14.10 skrll struct cdce_softc *sc = ifp->if_softc;
360 1.1 augustss struct mbuf *m_head = NULL;
361 1.1 augustss
362 1.1 augustss if (sc->cdce_dying || (ifp->if_flags & IFF_OACTIVE))
363 1.1 augustss return;
364 1.1 augustss
365 1.1 augustss IFQ_POLL(&ifp->if_snd, m_head);
366 1.1 augustss if (m_head == NULL)
367 1.1 augustss return;
368 1.1 augustss
369 1.1 augustss if (cdce_encap(sc, m_head, 0)) {
370 1.1 augustss return;
371 1.1 augustss }
372 1.1 augustss
373 1.1 augustss IFQ_DEQUEUE(&ifp->if_snd, m_head);
374 1.1 augustss
375 1.28 joerg bpf_mtap(ifp, m_head);
376 1.1 augustss
377 1.1 augustss ifp->if_flags |= IFF_OACTIVE;
378 1.1 augustss
379 1.1 augustss ifp->if_timer = 6;
380 1.1 augustss }
381 1.1 augustss
382 1.1 augustss Static int
383 1.1 augustss cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx)
384 1.1 augustss {
385 1.38.14.10 skrll struct cdce_chain *c = &sc->cdce_cdata.cdce_tx_chain[idx];
386 1.1 augustss int extra = 0;
387 1.1 augustss
388 1.1 augustss m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf);
389 1.1 augustss if (sc->cdce_flags & CDCE_ZAURUS) {
390 1.1 augustss /* Zaurus wants a 32-bit CRC appended to every frame */
391 1.30 jakllsch uint32_t crc;
392 1.1 augustss
393 1.30 jakllsch crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len));
394 1.30 jakllsch memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc));
395 1.30 jakllsch extra = sizeof(crc);
396 1.1 augustss }
397 1.1 augustss c->cdce_mbuf = m;
398 1.1 augustss
399 1.38.14.6 skrll usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, m->m_pkthdr.len + extra,
400 1.38.14.6 skrll USBD_FORCE_SHORT_XFER, 10000, cdce_txeof);
401 1.38.14.10 skrll usbd_status err = usbd_transfer(c->cdce_xfer);
402 1.1 augustss if (err != USBD_IN_PROGRESS) {
403 1.38.14.10 skrll cdce_stop(GET_IFP(sc), 0);
404 1.38.14.2 skrll return EIO;
405 1.1 augustss }
406 1.1 augustss
407 1.1 augustss sc->cdce_cdata.cdce_tx_cnt++;
408 1.1 augustss
409 1.38.14.2 skrll return 0;
410 1.1 augustss }
411 1.1 augustss
412 1.1 augustss Static void
413 1.38.14.10 skrll cdce_stop(struct ifnet *ifp, int disable)
414 1.1 augustss {
415 1.38.14.10 skrll struct cdce_softc * const sc = ifp->if_softc;
416 1.38.14.10 skrll
417 1.38.14.10 skrll mutex_enter(&sc->cdce_lock);
418 1.38.14.10 skrll cdce_stop_locked(ifp, disable);
419 1.38.14.10 skrll mutex_exit(&sc->cdce_lock);
420 1.38.14.10 skrll }
421 1.38.14.10 skrll
422 1.38.14.10 skrll Static void
423 1.38.14.10 skrll cdce_stop_locked(struct ifnet *ifp, int disable)
424 1.38.14.10 skrll {
425 1.38.14.10 skrll struct cdce_softc * const sc = ifp->if_softc;
426 1.1 augustss usbd_status err;
427 1.1 augustss
428 1.1 augustss ifp->if_timer = 0;
429 1.1 augustss
430 1.1 augustss if (sc->cdce_bulkin_pipe != NULL) {
431 1.1 augustss err = usbd_abort_pipe(sc->cdce_bulkin_pipe);
432 1.1 augustss if (err)
433 1.1 augustss printf("%s: abort rx pipe failed: %s\n",
434 1.24 dyoung device_xname(sc->cdce_dev), usbd_errstr(err));
435 1.1 augustss }
436 1.1 augustss
437 1.1 augustss if (sc->cdce_bulkout_pipe != NULL) {
438 1.1 augustss err = usbd_abort_pipe(sc->cdce_bulkout_pipe);
439 1.1 augustss if (err)
440 1.1 augustss printf("%s: abort tx pipe failed: %s\n",
441 1.24 dyoung device_xname(sc->cdce_dev), usbd_errstr(err));
442 1.1 augustss }
443 1.1 augustss
444 1.38.14.10 skrll cdce_rx_list_free(sc);
445 1.1 augustss
446 1.38.14.10 skrll cdce_tx_list_free(sc);
447 1.1 augustss
448 1.38.14.7 skrll if (sc->cdce_bulkin_pipe != NULL) {
449 1.38.14.7 skrll err = usbd_close_pipe(sc->cdce_bulkin_pipe);
450 1.38.14.7 skrll if (err)
451 1.38.14.7 skrll printf("%s: close rx pipe failed: %s\n",
452 1.38.14.7 skrll device_xname(sc->cdce_dev), usbd_errstr(err));
453 1.38.14.7 skrll sc->cdce_bulkin_pipe = NULL;
454 1.38.14.7 skrll }
455 1.38.14.7 skrll
456 1.38.14.7 skrll if (sc->cdce_bulkout_pipe != NULL) {
457 1.38.14.7 skrll err = usbd_close_pipe(sc->cdce_bulkout_pipe);
458 1.38.14.7 skrll if (err)
459 1.38.14.7 skrll printf("%s: close tx pipe failed: %s\n",
460 1.38.14.7 skrll device_xname(sc->cdce_dev), usbd_errstr(err));
461 1.38.14.7 skrll sc->cdce_bulkout_pipe = NULL;
462 1.38.14.7 skrll }
463 1.38.14.7 skrll
464 1.38.14.10 skrll ifp->if_timer = 0;
465 1.1 augustss ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
466 1.1 augustss }
467 1.1 augustss
468 1.1 augustss Static int
469 1.13 christos cdce_ioctl(struct ifnet *ifp, u_long command, void *data)
470 1.1 augustss {
471 1.1 augustss struct cdce_softc *sc = ifp->if_softc;
472 1.1 augustss int s, error = 0;
473 1.1 augustss
474 1.1 augustss if (sc->cdce_dying)
475 1.38.14.2 skrll return EIO;
476 1.1 augustss
477 1.1 augustss s = splnet();
478 1.38.14.10 skrll error = ether_ioctl(ifp, command, data);
479 1.1 augustss splx(s);
480 1.1 augustss
481 1.30 jakllsch if (error == ENETRESET)
482 1.30 jakllsch error = 0;
483 1.30 jakllsch
484 1.38.14.2 skrll return error;
485 1.1 augustss }
486 1.1 augustss
487 1.1 augustss Static void
488 1.1 augustss cdce_watchdog(struct ifnet *ifp)
489 1.1 augustss {
490 1.1 augustss struct cdce_softc *sc = ifp->if_softc;
491 1.1 augustss
492 1.1 augustss if (sc->cdce_dying)
493 1.1 augustss return;
494 1.1 augustss
495 1.1 augustss ifp->if_oerrors++;
496 1.24 dyoung printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev));
497 1.1 augustss }
498 1.1 augustss
499 1.38.14.10 skrll Static int
500 1.38.14.10 skrll cdce_init(struct ifnet *ifp)
501 1.1 augustss {
502 1.38.14.10 skrll struct cdce_softc * const sc = ifp->if_softc;
503 1.1 augustss
504 1.38.14.10 skrll mutex_enter(&sc->cdce_lock);
505 1.38.14.10 skrll int ret = cdce_init_locked(ifp);
506 1.38.14.10 skrll mutex_exit(&sc->cdce_lock);
507 1.1 augustss
508 1.38.14.10 skrll return ret;
509 1.38.14.10 skrll }
510 1.1 augustss
511 1.38.14.10 skrll Static int
512 1.38.14.10 skrll cdce_init_locked(struct ifnet *ifp)
513 1.38.14.10 skrll {
514 1.38.14.10 skrll struct cdce_softc * const sc = ifp->if_softc;
515 1.38.14.10 skrll struct cdce_chain *c;
516 1.38.14.10 skrll usbd_status err;
517 1.38.14.10 skrll int i;
518 1.1 augustss
519 1.1 augustss err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no,
520 1.1 augustss USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe);
521 1.1 augustss if (err) {
522 1.24 dyoung printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev),
523 1.1 augustss usbd_errstr(err));
524 1.38.14.10 skrll goto fail;
525 1.1 augustss }
526 1.1 augustss
527 1.1 augustss err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no,
528 1.1 augustss USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe);
529 1.1 augustss if (err) {
530 1.24 dyoung printf("%s: open tx pipe failed: %s\n",
531 1.24 dyoung device_xname(sc->cdce_dev), usbd_errstr(err));
532 1.38.14.10 skrll goto fail1;
533 1.1 augustss }
534 1.1 augustss
535 1.38.14.6 skrll if (cdce_tx_list_init(sc)) {
536 1.38.14.6 skrll printf("%s: tx list init failed\n", device_xname(sc->cdce_dev));
537 1.38.14.10 skrll goto fail2;
538 1.38.14.6 skrll }
539 1.38.14.6 skrll
540 1.38.14.6 skrll if (cdce_rx_list_init(sc)) {
541 1.38.14.6 skrll printf("%s: rx list init failed\n", device_xname(sc->cdce_dev));
542 1.38.14.10 skrll goto fail3;
543 1.38.14.6 skrll }
544 1.38.14.6 skrll
545 1.1 augustss for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
546 1.1 augustss c = &sc->cdce_cdata.cdce_rx_chain[i];
547 1.38.14.6 skrll
548 1.38.14.6 skrll usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
549 1.38.14.6 skrll USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
550 1.1 augustss usbd_transfer(c->cdce_xfer);
551 1.1 augustss }
552 1.1 augustss
553 1.1 augustss ifp->if_flags |= IFF_RUNNING;
554 1.1 augustss ifp->if_flags &= ~IFF_OACTIVE;
555 1.1 augustss
556 1.38.14.10 skrll return 0;
557 1.38.14.10 skrll
558 1.38.14.10 skrll fail3:
559 1.38.14.10 skrll cdce_tx_list_free(sc);
560 1.38.14.10 skrll fail2:
561 1.38.14.10 skrll usbd_close_pipe(sc->cdce_bulkout_pipe);
562 1.38.14.10 skrll fail1:
563 1.38.14.10 skrll usbd_close_pipe(sc->cdce_bulkin_pipe);
564 1.38.14.10 skrll fail:
565 1.38.14.10 skrll return EIO;
566 1.1 augustss }
567 1.1 augustss
568 1.1 augustss Static int
569 1.1 augustss cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m)
570 1.1 augustss {
571 1.1 augustss struct mbuf *m_new = NULL;
572 1.1 augustss
573 1.1 augustss if (m == NULL) {
574 1.1 augustss MGETHDR(m_new, M_DONTWAIT, MT_DATA);
575 1.1 augustss if (m_new == NULL) {
576 1.1 augustss printf("%s: no memory for rx list "
577 1.24 dyoung "-- packet dropped!\n", device_xname(sc->cdce_dev));
578 1.38.14.2 skrll return ENOBUFS;
579 1.1 augustss }
580 1.1 augustss MCLGET(m_new, M_DONTWAIT);
581 1.1 augustss if (!(m_new->m_flags & M_EXT)) {
582 1.1 augustss printf("%s: no memory for rx list "
583 1.24 dyoung "-- packet dropped!\n", device_xname(sc->cdce_dev));
584 1.1 augustss m_freem(m_new);
585 1.38.14.2 skrll return ENOBUFS;
586 1.1 augustss }
587 1.1 augustss m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
588 1.1 augustss } else {
589 1.1 augustss m_new = m;
590 1.1 augustss m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
591 1.1 augustss m_new->m_data = m_new->m_ext.ext_buf;
592 1.1 augustss }
593 1.1 augustss c->cdce_mbuf = m_new;
594 1.38.14.2 skrll return 0;
595 1.1 augustss }
596 1.1 augustss
597 1.1 augustss Static int
598 1.1 augustss cdce_rx_list_init(struct cdce_softc *sc)
599 1.1 augustss {
600 1.38.14.10 skrll struct cdce_cdata *cd = &sc->cdce_cdata;
601 1.1 augustss struct cdce_chain *c;
602 1.1 augustss int i;
603 1.1 augustss
604 1.1 augustss for (i = 0; i < CDCE_RX_LIST_CNT; i++) {
605 1.1 augustss c = &cd->cdce_rx_chain[i];
606 1.1 augustss c->cdce_sc = sc;
607 1.1 augustss c->cdce_idx = i;
608 1.1 augustss if (cdce_newbuf(sc, c, NULL) == ENOBUFS)
609 1.38.14.2 skrll return ENOBUFS;
610 1.1 augustss if (c->cdce_xfer == NULL) {
611 1.38.14.6 skrll int err = usbd_create_xfer(sc->cdce_bulkin_pipe,
612 1.38.14.6 skrll CDCE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->cdce_xfer);
613 1.38.14.6 skrll if (err)
614 1.38.14.6 skrll return err;
615 1.38.14.6 skrll c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
616 1.1 augustss }
617 1.1 augustss }
618 1.1 augustss
619 1.38.14.2 skrll return 0;
620 1.1 augustss }
621 1.1 augustss
622 1.38.14.10 skrll Static void
623 1.38.14.10 skrll cdce_rx_list_free(struct cdce_softc *sc)
624 1.38.14.10 skrll {
625 1.38.14.10 skrll /* Free RX resources */
626 1.38.14.10 skrll for (size_t i = 0; i < CDCE_RX_LIST_CNT; i++) {
627 1.38.14.10 skrll if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) {
628 1.38.14.10 skrll m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf);
629 1.38.14.10 skrll sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL;
630 1.38.14.10 skrll }
631 1.38.14.10 skrll if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) {
632 1.38.14.10 skrll usbd_destroy_xfer(sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer);
633 1.38.14.10 skrll sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL;
634 1.38.14.10 skrll }
635 1.38.14.10 skrll }
636 1.38.14.10 skrll }
637 1.38.14.10 skrll
638 1.1 augustss Static int
639 1.1 augustss cdce_tx_list_init(struct cdce_softc *sc)
640 1.1 augustss {
641 1.38.14.10 skrll struct cdce_cdata *cd = &sc->cdce_cdata;
642 1.1 augustss struct cdce_chain *c;
643 1.1 augustss int i;
644 1.1 augustss
645 1.1 augustss for (i = 0; i < CDCE_TX_LIST_CNT; i++) {
646 1.1 augustss c = &cd->cdce_tx_chain[i];
647 1.1 augustss c->cdce_sc = sc;
648 1.1 augustss c->cdce_idx = i;
649 1.1 augustss c->cdce_mbuf = NULL;
650 1.1 augustss if (c->cdce_xfer == NULL) {
651 1.38.14.6 skrll int err = usbd_create_xfer(sc->cdce_bulkout_pipe,
652 1.38.14.6 skrll CDCE_BUFSZ, USBD_FORCE_SHORT_XFER, 0,
653 1.38.14.6 skrll &c->cdce_xfer);
654 1.38.14.6 skrll if (err)
655 1.38.14.6 skrll return err;
656 1.38.14.6 skrll c->cdce_buf = usbd_get_buffer(c->cdce_xfer);
657 1.1 augustss }
658 1.1 augustss }
659 1.1 augustss
660 1.38.14.2 skrll return 0;
661 1.1 augustss }
662 1.1 augustss
663 1.1 augustss Static void
664 1.38.14.10 skrll cdce_tx_list_free(struct cdce_softc *sc)
665 1.38.14.10 skrll {
666 1.38.14.10 skrll /* Free TX resources */
667 1.38.14.10 skrll for (size_t i = 0; i < CDCE_TX_LIST_CNT; i++) {
668 1.38.14.10 skrll if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) {
669 1.38.14.10 skrll m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf);
670 1.38.14.10 skrll sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL;
671 1.38.14.10 skrll }
672 1.38.14.10 skrll if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) {
673 1.38.14.10 skrll usbd_destroy_xfer(sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer);
674 1.38.14.10 skrll sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL;
675 1.38.14.10 skrll }
676 1.38.14.10 skrll }
677 1.38.14.10 skrll }
678 1.38.14.10 skrll
679 1.38.14.10 skrll Static void
680 1.38.14.3 skrll cdce_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
681 1.1 augustss {
682 1.1 augustss struct cdce_chain *c = priv;
683 1.1 augustss struct cdce_softc *sc = c->cdce_sc;
684 1.1 augustss struct ifnet *ifp = GET_IFP(sc);
685 1.1 augustss struct mbuf *m;
686 1.1 augustss int total_len = 0;
687 1.1 augustss
688 1.1 augustss if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING))
689 1.1 augustss return;
690 1.1 augustss
691 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
692 1.1 augustss if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
693 1.1 augustss return;
694 1.1 augustss if (sc->cdce_rxeof_errors == 0)
695 1.1 augustss printf("%s: usb error on rx: %s\n",
696 1.24 dyoung device_xname(sc->cdce_dev), usbd_errstr(status));
697 1.1 augustss if (status == USBD_STALLED)
698 1.8 augustss usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe);
699 1.1 augustss DELAY(sc->cdce_rxeof_errors * 10000);
700 1.1 augustss sc->cdce_rxeof_errors++;
701 1.1 augustss goto done;
702 1.1 augustss }
703 1.1 augustss
704 1.1 augustss sc->cdce_rxeof_errors = 0;
705 1.1 augustss
706 1.1 augustss usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
707 1.1 augustss if (sc->cdce_flags & CDCE_ZAURUS)
708 1.1 augustss total_len -= 4; /* Strip off CRC added by Zaurus */
709 1.1 augustss if (total_len <= 1)
710 1.1 augustss goto done;
711 1.1 augustss
712 1.1 augustss m = c->cdce_mbuf;
713 1.1 augustss memcpy(mtod(m, char *), c->cdce_buf, total_len);
714 1.1 augustss
715 1.1 augustss if (total_len < sizeof(struct ether_header)) {
716 1.1 augustss ifp->if_ierrors++;
717 1.1 augustss goto done;
718 1.1 augustss }
719 1.1 augustss
720 1.1 augustss m->m_pkthdr.len = m->m_len = total_len;
721 1.38.14.9 skrll m_set_rcvif(m, ifp);
722 1.1 augustss
723 1.1 augustss if (cdce_newbuf(sc, c, NULL) == ENOBUFS) {
724 1.1 augustss ifp->if_ierrors++;
725 1.38.14.10 skrll goto done;
726 1.1 augustss }
727 1.1 augustss
728 1.38.14.10 skrll if_percpuq_enqueue(sc->cdce_ipq, (m));
729 1.1 augustss
730 1.1 augustss done:
731 1.1 augustss /* Setup new transfer. */
732 1.38.14.6 skrll usbd_setup_xfer(c->cdce_xfer, c, c->cdce_buf, CDCE_BUFSZ,
733 1.38.14.6 skrll USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cdce_rxeof);
734 1.1 augustss usbd_transfer(c->cdce_xfer);
735 1.1 augustss }
736 1.1 augustss
737 1.1 augustss Static void
738 1.38.14.3 skrll cdce_txeof(struct usbd_xfer *xfer, void *priv,
739 1.11 christos usbd_status status)
740 1.1 augustss {
741 1.1 augustss struct cdce_chain *c = priv;
742 1.1 augustss struct cdce_softc *sc = c->cdce_sc;
743 1.1 augustss struct ifnet *ifp = GET_IFP(sc);
744 1.1 augustss usbd_status err;
745 1.1 augustss
746 1.1 augustss if (sc->cdce_dying)
747 1.1 augustss return;
748 1.1 augustss
749 1.38.14.10 skrll int s = splnet();
750 1.1 augustss
751 1.1 augustss ifp->if_timer = 0;
752 1.1 augustss ifp->if_flags &= ~IFF_OACTIVE;
753 1.1 augustss
754 1.1 augustss if (status != USBD_NORMAL_COMPLETION) {
755 1.1 augustss if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
756 1.1 augustss splx(s);
757 1.1 augustss return;
758 1.1 augustss }
759 1.1 augustss ifp->if_oerrors++;
760 1.24 dyoung printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev),
761 1.24 dyoung usbd_errstr(status));
762 1.1 augustss if (status == USBD_STALLED)
763 1.8 augustss usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe);
764 1.1 augustss splx(s);
765 1.1 augustss return;
766 1.1 augustss }
767 1.1 augustss
768 1.1 augustss usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err);
769 1.1 augustss
770 1.1 augustss if (c->cdce_mbuf != NULL) {
771 1.1 augustss m_freem(c->cdce_mbuf);
772 1.1 augustss c->cdce_mbuf = NULL;
773 1.1 augustss }
774 1.1 augustss
775 1.1 augustss if (err)
776 1.1 augustss ifp->if_oerrors++;
777 1.1 augustss else
778 1.1 augustss ifp->if_opackets++;
779 1.1 augustss
780 1.1 augustss if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
781 1.1 augustss cdce_start(ifp);
782 1.1 augustss
783 1.1 augustss splx(s);
784 1.1 augustss }
785 1.1 augustss
786 1.4 augustss int
787 1.23 dyoung cdce_activate(device_t self, enum devact act)
788 1.1 augustss {
789 1.16 cube struct cdce_softc *sc = device_private(self);
790 1.1 augustss
791 1.1 augustss switch (act) {
792 1.1 augustss case DVACT_DEACTIVATE:
793 1.1 augustss if_deactivate(GET_IFP(sc));
794 1.1 augustss sc->cdce_dying = 1;
795 1.26 dyoung return 0;
796 1.26 dyoung default:
797 1.26 dyoung return EOPNOTSUPP;
798 1.1 augustss }
799 1.1 augustss }
800