f_eem.c 15 KB

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