f_eem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * f_eem.c -- USB CDC Ethernet (EEM) link function driver
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Copyright (C) 2009 EF Johnson Technologies
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/crc32.h>
  17. #include <linux/slab.h>
  18. #include "u_ether.h"
  19. #define EEM_HLEN 2
  20. /*
  21. * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
  22. * Ethernet link.
  23. */
  24. struct f_eem {
  25. struct gether port;
  26. u8 ctrl_id;
  27. };
  28. static inline struct f_eem *func_to_eem(struct usb_function *f)
  29. {
  30. return container_of(f, struct f_eem, port.func);
  31. }
  32. /*-------------------------------------------------------------------------*/
  33. /* interface descriptor: */
  34. static struct usb_interface_descriptor eem_intf __initdata = {
  35. .bLength = sizeof eem_intf,
  36. .bDescriptorType = USB_DT_INTERFACE,
  37. /* .bInterfaceNumber = DYNAMIC */
  38. .bNumEndpoints = 2,
  39. .bInterfaceClass = USB_CLASS_COMM,
  40. .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
  41. .bInterfaceProtocol = USB_CDC_PROTO_EEM,
  42. /* .iInterface = DYNAMIC */
  43. };
  44. /* full speed support: */
  45. static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = {
  46. .bLength = USB_DT_ENDPOINT_SIZE,
  47. .bDescriptorType = USB_DT_ENDPOINT,
  48. .bEndpointAddress = USB_DIR_IN,
  49. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  50. };
  51. static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = {
  52. .bLength = USB_DT_ENDPOINT_SIZE,
  53. .bDescriptorType = USB_DT_ENDPOINT,
  54. .bEndpointAddress = USB_DIR_OUT,
  55. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  56. };
  57. static struct usb_descriptor_header *eem_fs_function[] __initdata = {
  58. /* CDC EEM control descriptors */
  59. (struct usb_descriptor_header *) &eem_intf,
  60. (struct usb_descriptor_header *) &eem_fs_in_desc,
  61. (struct usb_descriptor_header *) &eem_fs_out_desc,
  62. NULL,
  63. };
  64. /* high speed support: */
  65. static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = {
  66. .bLength = USB_DT_ENDPOINT_SIZE,
  67. .bDescriptorType = USB_DT_ENDPOINT,
  68. .bEndpointAddress = USB_DIR_IN,
  69. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  70. .wMaxPacketSize = cpu_to_le16(512),
  71. };
  72. static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = {
  73. .bLength = USB_DT_ENDPOINT_SIZE,
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bEndpointAddress = USB_DIR_OUT,
  76. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  77. .wMaxPacketSize = cpu_to_le16(512),
  78. };
  79. static struct usb_descriptor_header *eem_hs_function[] __initdata = {
  80. /* CDC EEM control descriptors */
  81. (struct usb_descriptor_header *) &eem_intf,
  82. (struct usb_descriptor_header *) &eem_hs_in_desc,
  83. (struct usb_descriptor_header *) &eem_hs_out_desc,
  84. NULL,
  85. };
  86. /* super speed support: */
  87. static struct usb_endpoint_descriptor eem_ss_in_desc __initdata = {
  88. .bLength = USB_DT_ENDPOINT_SIZE,
  89. .bDescriptorType = USB_DT_ENDPOINT,
  90. .bEndpointAddress = USB_DIR_IN,
  91. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  92. .wMaxPacketSize = cpu_to_le16(1024),
  93. };
  94. static struct usb_endpoint_descriptor eem_ss_out_desc __initdata = {
  95. .bLength = USB_DT_ENDPOINT_SIZE,
  96. .bDescriptorType = USB_DT_ENDPOINT,
  97. .bEndpointAddress = USB_DIR_OUT,
  98. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  99. .wMaxPacketSize = cpu_to_le16(1024),
  100. };
  101. static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc __initdata = {
  102. .bLength = sizeof eem_ss_bulk_comp_desc,
  103. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  104. /* the following 2 values can be tweaked if necessary */
  105. /* .bMaxBurst = 0, */
  106. /* .bmAttributes = 0, */
  107. };
  108. static struct usb_descriptor_header *eem_ss_function[] __initdata = {
  109. /* CDC EEM control descriptors */
  110. (struct usb_descriptor_header *) &eem_intf,
  111. (struct usb_descriptor_header *) &eem_ss_in_desc,
  112. (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
  113. (struct usb_descriptor_header *) &eem_ss_out_desc,
  114. (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
  115. NULL,
  116. };
  117. /* string descriptors: */
  118. static struct usb_string eem_string_defs[] = {
  119. [0].s = "CDC Ethernet Emulation Model (EEM)",
  120. { } /* end of list */
  121. };
  122. static struct usb_gadget_strings eem_string_table = {
  123. .language = 0x0409, /* en-us */
  124. .strings = eem_string_defs,
  125. };
  126. static struct usb_gadget_strings *eem_strings[] = {
  127. &eem_string_table,
  128. NULL,
  129. };
  130. /*-------------------------------------------------------------------------*/
  131. static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  132. {
  133. struct usb_composite_dev *cdev = f->config->cdev;
  134. int value = -EOPNOTSUPP;
  135. u16 w_index = le16_to_cpu(ctrl->wIndex);
  136. u16 w_value = le16_to_cpu(ctrl->wValue);
  137. u16 w_length = le16_to_cpu(ctrl->wLength);
  138. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  139. ctrl->bRequestType, ctrl->bRequest,
  140. w_value, w_index, w_length);
  141. /* device either stalls (value < 0) or reports success */
  142. return value;
  143. }
  144. static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  145. {
  146. struct f_eem *eem = func_to_eem(f);
  147. struct usb_composite_dev *cdev = f->config->cdev;
  148. struct net_device *net;
  149. /* we know alt == 0, so this is an activation or a reset */
  150. if (alt != 0)
  151. goto fail;
  152. if (intf == eem->ctrl_id) {
  153. if (eem->port.in_ep->driver_data) {
  154. DBG(cdev, "reset eem\n");
  155. gether_disconnect(&eem->port);
  156. }
  157. if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
  158. DBG(cdev, "init eem\n");
  159. if (config_ep_by_speed(cdev->gadget, f,
  160. eem->port.in_ep) ||
  161. config_ep_by_speed(cdev->gadget, f,
  162. eem->port.out_ep)) {
  163. eem->port.in_ep->desc = NULL;
  164. eem->port.out_ep->desc = NULL;
  165. goto fail;
  166. }
  167. }
  168. /* zlps should not occur because zero-length EEM packets
  169. * will be inserted in those cases where they would occur
  170. */
  171. eem->port.is_zlp_ok = 1;
  172. eem->port.cdc_filter = DEFAULT_FILTER;
  173. DBG(cdev, "activate eem\n");
  174. net = gether_connect(&eem->port);
  175. if (IS_ERR(net))
  176. return PTR_ERR(net);
  177. } else
  178. goto fail;
  179. return 0;
  180. fail:
  181. return -EINVAL;
  182. }
  183. static void eem_disable(struct usb_function *f)
  184. {
  185. struct f_eem *eem = func_to_eem(f);
  186. struct usb_composite_dev *cdev = f->config->cdev;
  187. DBG(cdev, "eem deactivated\n");
  188. if (eem->port.in_ep->driver_data)
  189. gether_disconnect(&eem->port);
  190. }
  191. /*-------------------------------------------------------------------------*/
  192. /* EEM function driver setup/binding */
  193. static int __init
  194. eem_bind(struct usb_configuration *c, struct usb_function *f)
  195. {
  196. struct usb_composite_dev *cdev = c->cdev;
  197. struct f_eem *eem = func_to_eem(f);
  198. int status;
  199. struct usb_ep *ep;
  200. /* allocate instance-specific interface IDs */
  201. status = usb_interface_id(c, f);
  202. if (status < 0)
  203. goto fail;
  204. eem->ctrl_id = status;
  205. eem_intf.bInterfaceNumber = status;
  206. status = -ENODEV;
  207. /* allocate instance-specific endpoints */
  208. ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
  209. if (!ep)
  210. goto fail;
  211. eem->port.in_ep = ep;
  212. ep->driver_data = cdev; /* claim */
  213. ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
  214. if (!ep)
  215. goto fail;
  216. eem->port.out_ep = ep;
  217. ep->driver_data = cdev; /* claim */
  218. status = -ENOMEM;
  219. /* copy descriptors, and track endpoint copies */
  220. f->descriptors = usb_copy_descriptors(eem_fs_function);
  221. if (!f->descriptors)
  222. goto fail;
  223. /* support all relevant hardware speeds... we expect that when
  224. * hardware is dual speed, all bulk-capable endpoints work at
  225. * both speeds
  226. */
  227. if (gadget_is_dualspeed(c->cdev->gadget)) {
  228. eem_hs_in_desc.bEndpointAddress =
  229. eem_fs_in_desc.bEndpointAddress;
  230. eem_hs_out_desc.bEndpointAddress =
  231. eem_fs_out_desc.bEndpointAddress;
  232. /* copy descriptors, and track endpoint copies */
  233. f->hs_descriptors = usb_copy_descriptors(eem_hs_function);
  234. if (!f->hs_descriptors)
  235. goto fail;
  236. }
  237. if (gadget_is_superspeed(c->cdev->gadget)) {
  238. eem_ss_in_desc.bEndpointAddress =
  239. eem_fs_in_desc.bEndpointAddress;
  240. eem_ss_out_desc.bEndpointAddress =
  241. eem_fs_out_desc.bEndpointAddress;
  242. /* copy descriptors, and track endpoint copies */
  243. f->ss_descriptors = usb_copy_descriptors(eem_ss_function);
  244. if (!f->ss_descriptors)
  245. goto fail;
  246. }
  247. DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
  248. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  249. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  250. eem->port.in_ep->name, eem->port.out_ep->name);
  251. return 0;
  252. fail:
  253. if (f->descriptors)
  254. usb_free_descriptors(f->descriptors);
  255. if (f->hs_descriptors)
  256. usb_free_descriptors(f->hs_descriptors);
  257. /* we might as well release our claims on endpoints */
  258. if (eem->port.out_ep->desc)
  259. eem->port.out_ep->driver_data = NULL;
  260. if (eem->port.in_ep->desc)
  261. eem->port.in_ep->driver_data = NULL;
  262. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  263. return status;
  264. }
  265. static void
  266. eem_unbind(struct usb_configuration *c, struct usb_function *f)
  267. {
  268. struct f_eem *eem = func_to_eem(f);
  269. DBG(c->cdev, "eem unbind\n");
  270. if (gadget_is_superspeed(c->cdev->gadget))
  271. usb_free_descriptors(f->ss_descriptors);
  272. if (gadget_is_dualspeed(c->cdev->gadget))
  273. usb_free_descriptors(f->hs_descriptors);
  274. usb_free_descriptors(f->descriptors);
  275. kfree(eem);
  276. }
  277. static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  278. {
  279. struct sk_buff *skb = (struct sk_buff *)req->context;
  280. dev_kfree_skb_any(skb);
  281. }
  282. /*
  283. * Add the EEM header and ethernet checksum.
  284. * We currently do not attempt to put multiple ethernet frames
  285. * into a single USB transfer
  286. */
  287. static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
  288. {
  289. struct sk_buff *skb2 = NULL;
  290. struct usb_ep *in = port->in_ep;
  291. int padlen = 0;
  292. u16 len = skb->len;
  293. if (!skb_cloned(skb)) {
  294. int headroom = skb_headroom(skb);
  295. int tailroom = skb_tailroom(skb);
  296. /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
  297. * stick two bytes of zero-length EEM packet on the end.
  298. */
  299. if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
  300. padlen += 2;
  301. if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
  302. (headroom >= EEM_HLEN))
  303. goto done;
  304. }
  305. skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
  306. dev_kfree_skb_any(skb);
  307. skb = skb2;
  308. if (!skb)
  309. return skb;
  310. done:
  311. /* use the "no CRC" option */
  312. put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
  313. /* EEM packet header format:
  314. * b0..13: length of ethernet frame
  315. * b14: bmCRC (0 == sentinel CRC)
  316. * b15: bmType (0 == data)
  317. */
  318. len = skb->len;
  319. put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
  320. /* add a zero-length EEM packet, if needed */
  321. if (padlen)
  322. put_unaligned_le16(0, skb_put(skb, 2));
  323. return skb;
  324. }
  325. /*
  326. * Remove the EEM header. Note that there can be many EEM packets in a single
  327. * USB transfer, so we need to break them out and handle them independently.
  328. */
  329. static int eem_unwrap(struct gether *port,
  330. struct sk_buff *skb,
  331. struct sk_buff_head *list)
  332. {
  333. struct usb_composite_dev *cdev = port->func.config->cdev;
  334. int status = 0;
  335. do {
  336. struct sk_buff *skb2;
  337. u16 header;
  338. u16 len = 0;
  339. if (skb->len < EEM_HLEN) {
  340. status = -EINVAL;
  341. DBG(cdev, "invalid EEM header\n");
  342. goto error;
  343. }
  344. /* remove the EEM header */
  345. header = get_unaligned_le16(skb->data);
  346. skb_pull(skb, EEM_HLEN);
  347. /* EEM packet header format:
  348. * b0..14: EEM type dependent (data or command)
  349. * b15: bmType (0 == data, 1 == command)
  350. */
  351. if (header & BIT(15)) {
  352. struct usb_request *req = cdev->req;
  353. u16 bmEEMCmd;
  354. /* EEM command packet format:
  355. * b0..10: bmEEMCmdParam
  356. * b11..13: bmEEMCmd
  357. * b14: reserved (must be zero)
  358. * b15: bmType (1 == command)
  359. */
  360. if (header & BIT(14))
  361. continue;
  362. bmEEMCmd = (header >> 11) & 0x7;
  363. switch (bmEEMCmd) {
  364. case 0: /* echo */
  365. len = header & 0x7FF;
  366. if (skb->len < len) {
  367. status = -EOVERFLOW;
  368. goto error;
  369. }
  370. skb2 = skb_clone(skb, GFP_ATOMIC);
  371. if (unlikely(!skb2)) {
  372. DBG(cdev, "EEM echo response error\n");
  373. goto next;
  374. }
  375. skb_trim(skb2, len);
  376. put_unaligned_le16(BIT(15) | BIT(11) | len,
  377. skb_push(skb2, 2));
  378. skb_copy_bits(skb2, 0, req->buf, skb2->len);
  379. req->length = skb2->len;
  380. req->complete = eem_cmd_complete;
  381. req->zero = 1;
  382. req->context = skb2;
  383. if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
  384. DBG(cdev, "echo response queue fail\n");
  385. break;
  386. case 1: /* echo response */
  387. case 2: /* suspend hint */
  388. case 3: /* response hint */
  389. case 4: /* response complete hint */
  390. case 5: /* tickle */
  391. default: /* reserved */
  392. continue;
  393. }
  394. } else {
  395. u32 crc, crc2;
  396. struct sk_buff *skb3;
  397. /* check for zero-length EEM packet */
  398. if (header == 0)
  399. continue;
  400. /* EEM data packet format:
  401. * b0..13: length of ethernet frame
  402. * b14: bmCRC (0 == sentinel, 1 == calculated)
  403. * b15: bmType (0 == data)
  404. */
  405. len = header & 0x3FFF;
  406. if ((skb->len < len)
  407. || (len < (ETH_HLEN + ETH_FCS_LEN))) {
  408. status = -EINVAL;
  409. goto error;
  410. }
  411. /* validate CRC */
  412. if (header & BIT(14)) {
  413. crc = get_unaligned_le32(skb->data + len
  414. - ETH_FCS_LEN);
  415. crc2 = ~crc32_le(~0,
  416. skb->data, len - ETH_FCS_LEN);
  417. } else {
  418. crc = get_unaligned_be32(skb->data + len
  419. - ETH_FCS_LEN);
  420. crc2 = 0xdeadbeef;
  421. }
  422. if (crc != crc2) {
  423. DBG(cdev, "invalid EEM CRC\n");
  424. goto next;
  425. }
  426. skb2 = skb_clone(skb, GFP_ATOMIC);
  427. if (unlikely(!skb2)) {
  428. DBG(cdev, "unable to unframe EEM packet\n");
  429. continue;
  430. }
  431. skb_trim(skb2, len - ETH_FCS_LEN);
  432. skb3 = skb_copy_expand(skb2,
  433. NET_IP_ALIGN,
  434. 0,
  435. GFP_ATOMIC);
  436. if (unlikely(!skb3)) {
  437. DBG(cdev, "unable to realign EEM packet\n");
  438. dev_kfree_skb_any(skb2);
  439. continue;
  440. }
  441. dev_kfree_skb_any(skb2);
  442. skb_queue_tail(list, skb3);
  443. }
  444. next:
  445. skb_pull(skb, len);
  446. } while (skb->len);
  447. error:
  448. dev_kfree_skb_any(skb);
  449. return status;
  450. }
  451. /**
  452. * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
  453. * @c: the configuration to support the network link
  454. * Context: single threaded during gadget setup
  455. *
  456. * Returns zero on success, else negative errno.
  457. *
  458. * Caller must have called @gether_setup(). Caller is also responsible
  459. * for calling @gether_cleanup() before module unload.
  460. */
  461. int __init eem_bind_config(struct usb_configuration *c)
  462. {
  463. struct f_eem *eem;
  464. int status;
  465. /* maybe allocate device-global string IDs */
  466. if (eem_string_defs[0].id == 0) {
  467. /* control interface label */
  468. status = usb_string_id(c->cdev);
  469. if (status < 0)
  470. return status;
  471. eem_string_defs[0].id = status;
  472. eem_intf.iInterface = status;
  473. }
  474. /* allocate and initialize one new instance */
  475. eem = kzalloc(sizeof *eem, GFP_KERNEL);
  476. if (!eem)
  477. return -ENOMEM;
  478. eem->port.cdc_filter = DEFAULT_FILTER;
  479. eem->port.func.name = "cdc_eem";
  480. eem->port.func.strings = eem_strings;
  481. /* descriptors are per-instance copies */
  482. eem->port.func.bind = eem_bind;
  483. eem->port.func.unbind = eem_unbind;
  484. eem->port.func.set_alt = eem_set_alt;
  485. eem->port.func.setup = eem_setup;
  486. eem->port.func.disable = eem_disable;
  487. eem->port.wrap = eem_wrap;
  488. eem->port.unwrap = eem_unwrap;
  489. eem->port.header_len = EEM_HLEN;
  490. status = usb_add_function(c, &eem->port.func);
  491. if (status)
  492. kfree(eem);
  493. return status;
  494. }