blob: 82ce0358d9a3e5f13e44a76089c319e28622b662 [file] [log] [blame]
David Brownell4324fd42005-08-31 09:54:20 -07001/*
2 * CDC Ethernet based networking peripherals
3 * Copyright (C) 2003-2005 by David Brownell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20// #define DEBUG // error path messages, extra info
21// #define VERBOSE // more; success messages
22
David Brownell4324fd42005-08-31 09:54:20 -070023#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/init.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/ctype.h>
29#include <linux/ethtool.h>
30#include <linux/workqueue.h>
31#include <linux/mii.h>
32#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070033#include <linux/usb/cdc.h>
David Brownell4324fd42005-08-31 09:54:20 -070034
35#include "usbnet.h"
36
37
38/*
39 * probes control interface, claims data interface, collects the bulk
40 * endpoints, activates data interface (if needed), maybe sets MTU.
41 * all pure cdc, except for certain firmware workarounds, and knowing
42 * that rndis uses one different rule.
43 */
44int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
45{
46 u8 *buf = intf->cur_altsetting->extra;
47 int len = intf->cur_altsetting->extralen;
48 struct usb_interface_descriptor *d;
49 struct cdc_state *info = (void *) &dev->data;
50 int status;
51 int rndis;
52 struct usb_driver *driver = driver_of(intf);
53
54 if (sizeof dev->data < sizeof *info)
55 return -EDOM;
56
57 /* expect strict spec conformance for the descriptors, but
58 * cope with firmware which stores them in the wrong place
59 */
60 if (len == 0 && dev->udev->actconfig->extralen) {
61 /* Motorola SB4100 (and others: Brad Hards says it's
62 * from a Broadcom design) put CDC descriptors here
63 */
64 buf = dev->udev->actconfig->extra;
65 len = dev->udev->actconfig->extralen;
66 if (len)
67 dev_dbg(&intf->dev,
68 "CDC descriptors on config\n");
69 }
70
71 /* this assumes that if there's a non-RNDIS vendor variant
72 * of cdc-acm, it'll fail RNDIS requests cleanly.
73 */
74 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
75
76 memset(info, 0, sizeof *info);
77 info->control = intf;
78 while (len > 3) {
79 if (buf [1] != USB_DT_CS_INTERFACE)
80 goto next_desc;
81
82 /* use bDescriptorSubType to identify the CDC descriptors.
83 * We expect devices with CDC header and union descriptors.
84 * For CDC Ethernet we need the ethernet descriptor.
85 * For RNDIS, ignore two (pointless) CDC modem descriptors
86 * in favor of a complicated OID-based RPC scheme doing what
87 * CDC Ethernet achieves with a simple descriptor.
88 */
89 switch (buf [2]) {
90 case USB_CDC_HEADER_TYPE:
91 if (info->header) {
92 dev_dbg(&intf->dev, "extra CDC header\n");
93 goto bad_desc;
94 }
95 info->header = (void *) buf;
96 if (info->header->bLength != sizeof *info->header) {
97 dev_dbg(&intf->dev, "CDC header len %u\n",
98 info->header->bLength);
99 goto bad_desc;
100 }
101 break;
102 case USB_CDC_UNION_TYPE:
103 if (info->u) {
104 dev_dbg(&intf->dev, "extra CDC union\n");
105 goto bad_desc;
106 }
107 info->u = (void *) buf;
108 if (info->u->bLength != sizeof *info->u) {
109 dev_dbg(&intf->dev, "CDC union len %u\n",
110 info->u->bLength);
111 goto bad_desc;
112 }
113
114 /* we need a master/control interface (what we're
115 * probed with) and a slave/data interface; union
116 * descriptors sort this all out.
117 */
118 info->control = usb_ifnum_to_if(dev->udev,
119 info->u->bMasterInterface0);
120 info->data = usb_ifnum_to_if(dev->udev,
121 info->u->bSlaveInterface0);
122 if (!info->control || !info->data) {
123 dev_dbg(&intf->dev,
124 "master #%u/%p slave #%u/%p\n",
125 info->u->bMasterInterface0,
126 info->control,
127 info->u->bSlaveInterface0,
128 info->data);
129 goto bad_desc;
130 }
131 if (info->control != intf) {
132 dev_dbg(&intf->dev, "bogus CDC Union\n");
133 /* Ambit USB Cable Modem (and maybe others)
134 * interchanges master and slave interface.
135 */
136 if (info->data == intf) {
137 info->data = info->control;
138 info->control = intf;
139 } else
140 goto bad_desc;
141 }
142
143 /* a data interface altsetting does the real i/o */
144 d = &info->data->cur_altsetting->desc;
145 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
146 dev_dbg(&intf->dev, "slave class %u\n",
147 d->bInterfaceClass);
148 goto bad_desc;
149 }
150 break;
151 case USB_CDC_ETHERNET_TYPE:
152 if (info->ether) {
153 dev_dbg(&intf->dev, "extra CDC ether\n");
154 goto bad_desc;
155 }
156 info->ether = (void *) buf;
157 if (info->ether->bLength != sizeof *info->ether) {
158 dev_dbg(&intf->dev, "CDC ether len %u\n",
159 info->ether->bLength);
160 goto bad_desc;
161 }
162 dev->hard_mtu = le16_to_cpu(
163 info->ether->wMaxSegmentSize);
164 /* because of Zaurus, we may be ignoring the host
165 * side link address we were given.
166 */
167 break;
168 }
169next_desc:
170 len -= buf [0]; /* bLength */
171 buf += buf [0];
172 }
173
174 if (!info->header || !info->u || (!rndis && !info->ether)) {
175 dev_dbg(&intf->dev, "missing cdc %s%s%sdescriptor\n",
176 info->header ? "" : "header ",
177 info->u ? "" : "union ",
178 info->ether ? "" : "ether ");
179 goto bad_desc;
180 }
181
182 /* claim data interface and set it up ... with side effects.
183 * network traffic can't flow until an altsetting is enabled.
184 */
185 status = usb_driver_claim_interface(driver, info->data, dev);
186 if (status < 0)
187 return status;
188 status = usbnet_get_endpoints(dev, info->data);
189 if (status < 0) {
190 /* ensure immediate exit from usbnet_disconnect */
191 usb_set_intfdata(info->data, NULL);
192 usb_driver_release_interface(driver, info->data);
193 return status;
194 }
195
196 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
197 dev->status = NULL;
198 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
199 struct usb_endpoint_descriptor *desc;
200
201 dev->status = &info->control->cur_altsetting->endpoint [0];
202 desc = &dev->status->desc;
203 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
204 || !(desc->bEndpointAddress & USB_DIR_IN)
205 || (le16_to_cpu(desc->wMaxPacketSize)
206 < sizeof(struct usb_cdc_notification))
207 || !desc->bInterval) {
208 dev_dbg(&intf->dev, "bad notification endpoint\n");
209 dev->status = NULL;
210 }
211 }
212 if (rndis && !dev->status) {
213 dev_dbg(&intf->dev, "missing RNDIS status endpoint\n");
214 usb_set_intfdata(info->data, NULL);
215 usb_driver_release_interface(driver, info->data);
216 return -ENODEV;
217 }
218 return 0;
219
220bad_desc:
221 dev_info(&dev->udev->dev, "bad CDC descriptors\n");
222 return -ENODEV;
223}
224EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
225
226void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
227{
228 struct cdc_state *info = (void *) &dev->data;
229 struct usb_driver *driver = driver_of(intf);
230
231 /* disconnect master --> disconnect slave */
232 if (intf == info->control && info->data) {
233 /* ensure immediate exit from usbnet_disconnect */
234 usb_set_intfdata(info->data, NULL);
235 usb_driver_release_interface(driver, info->data);
236 info->data = NULL;
237 }
238
239 /* and vice versa (just in case) */
240 else if (intf == info->data && info->control) {
241 /* ensure immediate exit from usbnet_disconnect */
242 usb_set_intfdata(info->control, NULL);
243 usb_driver_release_interface(driver, info->control);
244 info->control = NULL;
245 }
246}
247EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
248
249
250/*-------------------------------------------------------------------------
251 *
252 * Communications Device Class, Ethernet Control model
253 *
254 * Takes two interfaces. The DATA interface is inactive till an altsetting
255 * is selected. Configuration data includes class descriptors. There's
256 * an optional status endpoint on the control interface.
257 *
258 * This should interop with whatever the 2.4 "CDCEther.c" driver
259 * (by Brad Hards) talked with, with more functionality.
260 *
261 *-------------------------------------------------------------------------*/
262
263static void dumpspeed(struct usbnet *dev, __le32 *speeds)
264{
265 if (netif_msg_timer(dev))
266 devinfo(dev, "link speeds: %u kbps up, %u kbps down",
267 __le32_to_cpu(speeds[0]) / 1000,
268 __le32_to_cpu(speeds[1]) / 1000);
269}
270
271static void cdc_status(struct usbnet *dev, struct urb *urb)
272{
273 struct usb_cdc_notification *event;
274
275 if (urb->actual_length < sizeof *event)
276 return;
277
278 /* SPEED_CHANGE can get split into two 8-byte packets */
279 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
280 dumpspeed(dev, (__le32 *) urb->transfer_buffer);
281 return;
282 }
283
284 event = urb->transfer_buffer;
285 switch (event->bNotificationType) {
286 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
287 if (netif_msg_timer(dev))
288 devdbg(dev, "CDC: carrier %s",
289 event->wValue ? "on" : "off");
290 if (event->wValue)
291 netif_carrier_on(dev->net);
292 else
293 netif_carrier_off(dev->net);
294 break;
295 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
296 if (netif_msg_timer(dev))
297 devdbg(dev, "CDC: speed change (len %d)",
298 urb->actual_length);
299 if (urb->actual_length != (sizeof *event + 8))
300 set_bit(EVENT_STS_SPLIT, &dev->flags);
301 else
302 dumpspeed(dev, (__le32 *) &event[1]);
303 break;
304 /* USB_CDC_NOTIFY_RESPONSE_AVAILABLE can happen too (e.g. RNDIS),
305 * but there are no standard formats for the response data.
306 */
307 default:
308 deverr(dev, "CDC: unexpected notification %02x!",
309 event->bNotificationType);
310 break;
311 }
312}
313
314static u8 nibble(unsigned char c)
315{
316 if (likely(isdigit(c)))
317 return c - '0';
318 c = toupper(c);
319 if (likely(isxdigit(c)))
320 return 10 + c - 'A';
321 return 0;
322}
323
324static inline int
325get_ethernet_addr(struct usbnet *dev, struct usb_cdc_ether_desc *e)
326{
327 int tmp, i;
328 unsigned char buf [13];
329
330 tmp = usb_string(dev->udev, e->iMACAddress, buf, sizeof buf);
331 if (tmp != 12) {
332 dev_dbg(&dev->udev->dev,
333 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
334 if (tmp >= 0)
335 tmp = -EINVAL;
336 return tmp;
337 }
338 for (i = tmp = 0; i < 6; i++, tmp += 2)
339 dev->net->dev_addr [i] =
340 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
341 return 0;
342}
343
344static int cdc_bind(struct usbnet *dev, struct usb_interface *intf)
345{
346 int status;
347 struct cdc_state *info = (void *) &dev->data;
348
349 status = usbnet_generic_cdc_bind(dev, intf);
350 if (status < 0)
351 return status;
352
353 status = get_ethernet_addr(dev, info->ether);
354 if (status < 0) {
355 usb_set_intfdata(info->data, NULL);
356 usb_driver_release_interface(driver_of(intf), info->data);
357 return status;
358 }
359
360 /* FIXME cdc-ether has some multicast code too, though it complains
361 * in routine cases. info->ether describes the multicast support.
362 * Implement that here, manipulating the cdc filter as needed.
363 */
364 return 0;
365}
366
367static const struct driver_info cdc_info = {
368 .description = "CDC Ethernet Device",
369 .flags = FLAG_ETHER,
370 // .check_connect = cdc_check_connect,
371 .bind = cdc_bind,
372 .unbind = usbnet_cdc_unbind,
373 .status = cdc_status,
374};
375
376/*-------------------------------------------------------------------------*/
377
378
379static const struct usb_device_id products [] = {
380/*
381 * BLACKLIST !!
382 *
383 * First blacklist any products that are egregiously nonconformant
384 * with the CDC Ethernet specs. Minor braindamage we cope with; when
385 * they're not even trying, needing a separate driver is only the first
386 * of the differences to show up.
387 */
388
389#define ZAURUS_MASTER_INTERFACE \
390 .bInterfaceClass = USB_CLASS_COMM, \
391 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
392 .bInterfaceProtocol = USB_CDC_PROTO_NONE
393
394/* SA-1100 based Sharp Zaurus ("collie"), or compatible;
395 * wire-incompatible with true CDC Ethernet implementations.
396 * (And, it seems, needlessly so...)
397 */
398{
399 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
400 | USB_DEVICE_ID_MATCH_DEVICE,
401 .idVendor = 0x04DD,
402 .idProduct = 0x8004,
403 ZAURUS_MASTER_INTERFACE,
404 .driver_info = 0,
405},
406
407/* PXA-25x based Sharp Zaurii. Note that it seems some of these
408 * (later models especially) may have shipped only with firmware
409 * advertising false "CDC MDLM" compatibility ... but we're not
410 * clear which models did that, so for now let's assume the worst.
411 */
412{
413 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
414 | USB_DEVICE_ID_MATCH_DEVICE,
415 .idVendor = 0x04DD,
416 .idProduct = 0x8005, /* A-300 */
417 ZAURUS_MASTER_INTERFACE,
418 .driver_info = 0,
419}, {
420 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
421 | USB_DEVICE_ID_MATCH_DEVICE,
422 .idVendor = 0x04DD,
423 .idProduct = 0x8006, /* B-500/SL-5600 */
424 ZAURUS_MASTER_INTERFACE,
425 .driver_info = 0,
426}, {
427 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
428 | USB_DEVICE_ID_MATCH_DEVICE,
429 .idVendor = 0x04DD,
430 .idProduct = 0x8007, /* C-700 */
431 ZAURUS_MASTER_INTERFACE,
432 .driver_info = 0,
433}, {
434 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
435 | USB_DEVICE_ID_MATCH_DEVICE,
436 .idVendor = 0x04DD,
437 .idProduct = 0x9031, /* C-750 C-760 */
438 ZAURUS_MASTER_INTERFACE,
439 .driver_info = 0,
440}, {
441 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
442 | USB_DEVICE_ID_MATCH_DEVICE,
443 .idVendor = 0x04DD,
444 .idProduct = 0x9032, /* SL-6000 */
445 ZAURUS_MASTER_INTERFACE,
446 .driver_info = 0,
447}, {
448 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
449 | USB_DEVICE_ID_MATCH_DEVICE,
450 .idVendor = 0x04DD,
451 /* reported with some C860 units */
452 .idProduct = 0x9050, /* C-860 */
453 ZAURUS_MASTER_INTERFACE,
454 .driver_info = 0,
455},
456
David Brownellefcaa202006-05-30 20:49:29 -0700457/* Olympus has some models with a Zaurus-compatible option.
458 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
459 */
460{
461 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
462 | USB_DEVICE_ID_MATCH_DEVICE,
463 .idVendor = 0x07B4,
464 .idProduct = 0x0F02, /* R-1000 */
465 ZAURUS_MASTER_INTERFACE,
466 .driver_info = 0,
467},
468
David Brownell4324fd42005-08-31 09:54:20 -0700469/*
470 * WHITELIST!!!
471 *
472 * CDC Ether uses two interfaces, not necessarily consecutive.
473 * We match the main interface, ignoring the optional device
474 * class so we could handle devices that aren't exclusively
475 * CDC ether.
476 *
477 * NOTE: this match must come AFTER entries blacklisting devices
478 * because of bugs/quirks in a given product (like Zaurus, above).
479 */
480{
481 USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
482 USB_CDC_PROTO_NONE),
483 .driver_info = (unsigned long) &cdc_info,
484},
485 { }, // END
486};
487MODULE_DEVICE_TABLE(usb, products);
488
489static struct usb_driver cdc_driver = {
David Brownell4324fd42005-08-31 09:54:20 -0700490 .name = "cdc_ether",
491 .id_table = products,
492 .probe = usbnet_probe,
493 .disconnect = usbnet_disconnect,
494 .suspend = usbnet_suspend,
495 .resume = usbnet_resume,
496};
497
498
499static int __init cdc_init(void)
500{
501 BUG_ON((sizeof(((struct usbnet *)0)->data)
502 < sizeof(struct cdc_state)));
503
504 return usb_register(&cdc_driver);
505}
506module_init(cdc_init);
507
508static void __exit cdc_exit(void)
509{
510 usb_deregister(&cdc_driver);
511}
512module_exit(cdc_exit);
513
514MODULE_AUTHOR("David Brownell");
515MODULE_DESCRIPTION("USB CDC Ethernet devices");
516MODULE_LICENSE("GPL");