cdc_ether.c 16 KB

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