cdc_ether.c 17 KB

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