cdc_ether.c 14 KB

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