if_cdce.c revision 1.60 1 1.60 mrg /* $NetBSD: if_cdce.c,v 1.60 2019/08/09 02:52:59 mrg 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 #include <sys/cdefs.h>
43 1.60 mrg __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.60 2019/08/09 02:52:59 mrg Exp $");
44 1.1 augustss
45 1.1 augustss #include <sys/param.h>
46 1.1 augustss #include <sys/kernel.h>
47 1.54 mrg
48 1.54 mrg #include <dev/usb/usbnet.h>
49 1.1 augustss #include <dev/usb/usbcdc.h>
50 1.1 augustss
51 1.1 augustss #include <dev/usb/if_cdcereg.h>
52 1.1 augustss
53 1.48 mrg struct cdce_type {
54 1.48 mrg struct usb_devno cdce_dev;
55 1.48 mrg uint16_t cdce_flags;
56 1.48 mrg #define CDCE_ZAURUS 1
57 1.48 mrg #define CDCE_NO_UNION 2
58 1.48 mrg };
59 1.48 mrg
60 1.49 mrg static const struct cdce_type cdce_devs[] = {
61 1.33 msaitoh {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION },
62 1.33 msaitoh {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION },
63 1.33 msaitoh {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION },
64 1.33 msaitoh {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION },
65 1.33 msaitoh {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION },
66 1.1 augustss {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION },
67 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS },
68 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION },
69 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION },
70 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION },
71 1.1 augustss {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION },
72 1.1 augustss };
73 1.33 msaitoh #define cdce_lookup(v, p) \
74 1.33 msaitoh ((const struct cdce_type *)usb_lookup(cdce_devs, v, p))
75 1.1 augustss
76 1.54 mrg static int cdce_match(device_t, cfdata_t, void *);
77 1.54 mrg static void cdce_attach(device_t, device_t, void *);
78 1.59 mrg
79 1.60 mrg CFATTACH_DECL_NEW(cdce, sizeof(struct usbnet), cdce_match, cdce_attach,
80 1.59 mrg usbnet_detach, usbnet_activate);
81 1.59 mrg
82 1.57 mrg static void cdce_rx_loop(struct usbnet *, struct usbd_xfer *,
83 1.57 mrg struct usbnet_chain *, uint32_t);
84 1.54 mrg static unsigned cdce_tx_prepare(struct usbnet *, struct mbuf *,
85 1.54 mrg struct usbnet_chain *);
86 1.59 mrg static int cdce_init(struct ifnet *);
87 1.47 mrg
88 1.59 mrg static struct usbnet_ops cdce_ops = {
89 1.59 mrg .uno_tx_prepare = cdce_tx_prepare,
90 1.59 mrg .uno_rx_loop = cdce_rx_loop,
91 1.59 mrg .uno_init = cdce_init,
92 1.59 mrg };
93 1.1 augustss
94 1.54 mrg static int
95 1.23 dyoung cdce_match(device_t parent, cfdata_t match, void *aux)
96 1.1 augustss {
97 1.41 skrll struct usbif_attach_arg *uiaa = aux;
98 1.1 augustss
99 1.41 skrll if (cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
100 1.41 skrll return UMATCH_VENDOR_PRODUCT;
101 1.1 augustss
102 1.41 skrll if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
103 1.1 augustss UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL)
104 1.41 skrll return UMATCH_IFACECLASS_GENERIC;
105 1.1 augustss
106 1.41 skrll return UMATCH_NONE;
107 1.1 augustss }
108 1.1 augustss
109 1.54 mrg static void
110 1.23 dyoung cdce_attach(device_t parent, device_t self, void *aux)
111 1.1 augustss {
112 1.60 mrg struct usbnet * const un = device_private(self);
113 1.54 mrg struct usbif_attach_arg *uiaa = aux;
114 1.33 msaitoh char *devinfop;
115 1.41 skrll struct usbd_device *dev = uiaa->uiaa_device;
116 1.1 augustss const struct cdce_type *t;
117 1.1 augustss usb_interface_descriptor_t *id;
118 1.1 augustss usb_endpoint_descriptor_t *ed;
119 1.2 augustss const usb_cdc_union_descriptor_t *ud;
120 1.22 tron usb_config_descriptor_t *cd;
121 1.1 augustss int data_ifcno;
122 1.22 tron int i, j, numalts;
123 1.2 augustss const usb_cdc_ethernet_descriptor_t *ue;
124 1.5 augustss char eaddr_str[USB_MAX_ENCODED_STRING_LEN];
125 1.1 augustss
126 1.23 dyoung aprint_naive("\n");
127 1.23 dyoung aprint_normal("\n");
128 1.25 plunky devinfop = usbd_devinfo_alloc(dev, 0);
129 1.16 cube aprint_normal_dev(self, "%s\n", devinfop);
130 1.6 augustss usbd_devinfo_free(devinfop);
131 1.1 augustss
132 1.54 mrg un->un_dev = self;
133 1.54 mrg un->un_udev = dev;
134 1.60 mrg un->un_sc = un;
135 1.59 mrg un->un_ops = &cdce_ops;
136 1.1 augustss
137 1.41 skrll t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
138 1.1 augustss if (t)
139 1.60 mrg un->un_flags = t->cdce_flags;
140 1.1 augustss
141 1.60 mrg if (un->un_flags & CDCE_NO_UNION)
142 1.54 mrg un->un_iface = uiaa->uiaa_iface;
143 1.1 augustss else {
144 1.54 mrg ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(un->un_udev,
145 1.2 augustss UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION);
146 1.1 augustss if (ud == NULL) {
147 1.16 cube aprint_error_dev(self, "no union descriptor\n");
148 1.24 dyoung return;
149 1.1 augustss }
150 1.1 augustss data_ifcno = ud->bSlaveInterface[0];
151 1.1 augustss
152 1.41 skrll for (i = 0; i < uiaa->uiaa_nifaces; i++) {
153 1.41 skrll if (uiaa->uiaa_ifaces[i] != NULL) {
154 1.1 augustss id = usbd_get_interface_descriptor(
155 1.41 skrll uiaa->uiaa_ifaces[i]);
156 1.1 augustss if (id != NULL && id->bInterfaceNumber ==
157 1.1 augustss data_ifcno) {
158 1.54 mrg un->un_iface = uiaa->uiaa_ifaces[i];
159 1.41 skrll uiaa->uiaa_ifaces[i] = NULL;
160 1.1 augustss }
161 1.1 augustss }
162 1.1 augustss }
163 1.1 augustss }
164 1.54 mrg if (un->un_iface == NULL) {
165 1.16 cube aprint_error_dev(self, "no data interface\n");
166 1.24 dyoung return;
167 1.1 augustss }
168 1.1 augustss
169 1.22 tron /*
170 1.22 tron * <quote>
171 1.22 tron * The Data Class interface of a networking device shall have a minimum
172 1.22 tron * of two interface settings. The first setting (the default interface
173 1.22 tron * setting) includes no endpoints and therefore no networking traffic is
174 1.22 tron * exchanged whenever the default interface setting is selected. One or
175 1.22 tron * more additional interface settings are used for normal operation, and
176 1.22 tron * therefore each includes a pair of endpoints (one IN, and one OUT) to
177 1.22 tron * exchange network traffic. Select an alternate interface setting to
178 1.22 tron * initialize the network aspects of the device and to enable the
179 1.22 tron * exchange of network traffic.
180 1.22 tron * </quote>
181 1.22 tron *
182 1.22 tron * Some devices, most notably cable modems, include interface settings
183 1.22 tron * that have no IN or OUT endpoint, therefore loop through the list of all
184 1.22 tron * available interface settings looking for one with both IN and OUT
185 1.22 tron * endpoints.
186 1.22 tron */
187 1.54 mrg id = usbd_get_interface_descriptor(un->un_iface);
188 1.54 mrg cd = usbd_get_config_descriptor(un->un_udev);
189 1.22 tron numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
190 1.22 tron
191 1.22 tron for (j = 0; j < numalts; j++) {
192 1.54 mrg if (usbd_set_interface(un->un_iface, j)) {
193 1.54 mrg aprint_error_dev(un->un_dev,
194 1.22 tron "setting alternate interface failed\n");
195 1.24 dyoung return;
196 1.1 augustss }
197 1.22 tron /* Find endpoints. */
198 1.54 mrg id = usbd_get_interface_descriptor(un->un_iface);
199 1.54 mrg un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = -1;
200 1.22 tron for (i = 0; i < id->bNumEndpoints; i++) {
201 1.54 mrg ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
202 1.22 tron if (!ed) {
203 1.22 tron aprint_error_dev(self,
204 1.22 tron "could not read endpoint descriptor\n");
205 1.24 dyoung return;
206 1.22 tron }
207 1.22 tron if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
208 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
209 1.54 mrg un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
210 1.22 tron } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
211 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
212 1.54 mrg un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
213 1.22 tron } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
214 1.22 tron UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
215 1.22 tron /* XXX: CDC spec defines an interrupt pipe, but it is not
216 1.22 tron * needed for simple host-to-host applications. */
217 1.22 tron } else {
218 1.22 tron aprint_error_dev(self, "unexpected endpoint\n");
219 1.22 tron }
220 1.1 augustss }
221 1.22 tron /* If we found something, try and use it... */
222 1.54 mrg if (un->un_ed[USBNET_ENDPT_RX] != -1 && un->un_ed[USBNET_ENDPT_TX] != -1)
223 1.22 tron break;
224 1.1 augustss }
225 1.1 augustss
226 1.54 mrg if (un->un_ed[USBNET_ENDPT_RX] == -1) {
227 1.16 cube aprint_error_dev(self, "could not find data bulk in\n");
228 1.24 dyoung return;
229 1.1 augustss }
230 1.54 mrg if (un->un_ed[USBNET_ENDPT_TX] == -1 ) {
231 1.16 cube aprint_error_dev(self, "could not find data bulk out\n");
232 1.24 dyoung return;
233 1.1 augustss }
234 1.1 augustss
235 1.7 christos ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev,
236 1.30 jakllsch UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF);
237 1.30 jakllsch if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
238 1.54 mrg ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) {
239 1.16 cube aprint_normal_dev(self, "faking address\n");
240 1.54 mrg un->un_eaddr[0] = 0x2a;
241 1.54 mrg memcpy(&un->un_eaddr[1], &hardclock_ticks, sizeof(uint32_t));
242 1.54 mrg un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev));
243 1.2 augustss }
244 1.2 augustss
245 1.59 mrg usbnet_attach(un, "cdcedet", CDCE_RX_LIST_CNT, CDCE_TX_LIST_CNT,
246 1.59 mrg USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER,
247 1.59 mrg CDCE_BUFSZ, CDCE_BUFSZ);
248 1.54 mrg usbnet_attach_ifp(un, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
249 1.55 mrg 0, 0);
250 1.1 augustss }
251 1.1 augustss
252 1.54 mrg static int
253 1.54 mrg cdce_init(struct ifnet *ifp)
254 1.1 augustss {
255 1.54 mrg struct usbnet *un = ifp->if_softc;
256 1.54 mrg int rv;
257 1.50 mrg
258 1.56 mrg usbnet_lock(un);
259 1.59 mrg if (usbnet_isdying(un))
260 1.54 mrg rv = EIO;
261 1.54 mrg else {
262 1.54 mrg usbnet_stop(un, ifp, 1);
263 1.59 mrg rv = usbnet_init_rx_tx(un);
264 1.54 mrg if (rv == 0)
265 1.54 mrg un->un_link = true;
266 1.51 mrg }
267 1.56 mrg usbnet_unlock(un);
268 1.1 augustss
269 1.54 mrg return rv;
270 1.1 augustss }
271 1.1 augustss
272 1.49 mrg static void
273 1.57 mrg cdce_rx_loop(struct usbnet * un, struct usbd_xfer *xfer,
274 1.57 mrg struct usbnet_chain *c, uint32_t total_len)
275 1.1 augustss {
276 1.54 mrg struct ifnet *ifp = usbnet_ifp(un);
277 1.1 augustss
278 1.56 mrg usbnet_isowned_rx(un);
279 1.51 mrg
280 1.54 mrg /* Strip off CRC added by Zaurus, if present */
281 1.60 mrg if (un->un_flags & CDCE_ZAURUS && total_len > 4)
282 1.54 mrg total_len -= 4;
283 1.1 augustss
284 1.54 mrg if (total_len < sizeof(struct ether_header)) {
285 1.54 mrg ifp->if_ierrors++;
286 1.1 augustss return;
287 1.1 augustss }
288 1.1 augustss
289 1.57 mrg usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0);
290 1.50 mrg }
291 1.50 mrg
292 1.54 mrg static unsigned
293 1.54 mrg cdce_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
294 1.1 augustss {
295 1.1 augustss int extra = 0;
296 1.1 augustss
297 1.56 mrg usbnet_isowned_tx(un);
298 1.50 mrg
299 1.54 mrg m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
300 1.60 mrg if (un->un_flags & CDCE_ZAURUS) {
301 1.1 augustss /* Zaurus wants a 32-bit CRC appended to every frame */
302 1.30 jakllsch uint32_t crc;
303 1.1 augustss
304 1.54 mrg crc = htole32(~ether_crc32_le(c->unc_buf, m->m_pkthdr.len));
305 1.54 mrg memcpy(c->unc_buf + m->m_pkthdr.len, &crc, sizeof(crc));
306 1.30 jakllsch extra = sizeof(crc);
307 1.1 augustss }
308 1.51 mrg
309 1.54 mrg return m->m_pkthdr.len + extra;
310 1.1 augustss }
311