cdc_ether.c 16 KB

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