cdc_ether.c 14 KB

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