/kern_oII/drivers/net/usb/cdc_ether.c

http://omnia2droid.googlecode.com/ · C · 593 lines · 405 code · 55 blank · 133 comment · 86 complexity · e4d47df3edc67e11e5069e2a4aede6fe MD5 · raw file

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