cdc_ether.c 15 KB

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