cdc_ether.c 16 KB

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