f_eem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. if (eem->port.out_ep)
  258. eem->port.out_ep->driver_data = NULL;
  259. if (eem->port.in_ep)
  260. eem->port.in_ep->driver_data = NULL;
  261. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  262. return status;
  263. }
  264. static void
  265. eem_unbind(struct usb_configuration *c, struct usb_function *f)
  266. {
  267. struct f_eem *eem = func_to_eem(f);
  268. DBG(c->cdev, "eem unbind\n");
  269. if (gadget_is_superspeed(c->cdev->gadget))
  270. usb_free_descriptors(f->ss_descriptors);
  271. if (gadget_is_dualspeed(c->cdev->gadget))
  272. usb_free_descriptors(f->hs_descriptors);
  273. usb_free_descriptors(f->descriptors);
  274. kfree(eem);
  275. }
  276. static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  277. {
  278. struct sk_buff *skb = (struct sk_buff *)req->context;
  279. dev_kfree_skb_any(skb);
  280. }
  281. /*
  282. * Add the EEM header and ethernet checksum.
  283. * We currently do not attempt to put multiple ethernet frames
  284. * into a single USB transfer
  285. */
  286. static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
  287. {
  288. struct sk_buff *skb2 = NULL;
  289. struct usb_ep *in = port->in_ep;
  290. int padlen = 0;
  291. u16 len = skb->len;
  292. if (!skb_cloned(skb)) {
  293. int headroom = skb_headroom(skb);
  294. int tailroom = skb_tailroom(skb);
  295. /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
  296. * stick two bytes of zero-length EEM packet on the end.
  297. */
  298. if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
  299. padlen += 2;
  300. if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
  301. (headroom >= EEM_HLEN))
  302. goto done;
  303. }
  304. skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
  305. dev_kfree_skb_any(skb);
  306. skb = skb2;
  307. if (!skb)
  308. return skb;
  309. done:
  310. /* use the "no CRC" option */
  311. put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
  312. /* EEM packet header format:
  313. * b0..13: length of ethernet frame
  314. * b14: bmCRC (0 == sentinel CRC)
  315. * b15: bmType (0 == data)
  316. */
  317. len = skb->len;
  318. put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
  319. /* add a zero-length EEM packet, if needed */
  320. if (padlen)
  321. put_unaligned_le16(0, skb_put(skb, 2));
  322. return skb;
  323. }
  324. /*
  325. * Remove the EEM header. Note that there can be many EEM packets in a single
  326. * USB transfer, so we need to break them out and handle them independently.
  327. */
  328. static int eem_unwrap(struct gether *port,
  329. struct sk_buff *skb,
  330. struct sk_buff_head *list)
  331. {
  332. struct usb_composite_dev *cdev = port->func.config->cdev;
  333. int status = 0;
  334. do {
  335. struct sk_buff *skb2;
  336. u16 header;
  337. u16 len = 0;
  338. if (skb->len < EEM_HLEN) {
  339. status = -EINVAL;
  340. DBG(cdev, "invalid EEM header\n");
  341. goto error;
  342. }
  343. /* remove the EEM header */
  344. header = get_unaligned_le16(skb->data);
  345. skb_pull(skb, EEM_HLEN);
  346. /* EEM packet header format:
  347. * b0..14: EEM type dependent (data or command)
  348. * b15: bmType (0 == data, 1 == command)
  349. */
  350. if (header & BIT(15)) {
  351. struct usb_request *req = cdev->req;
  352. u16 bmEEMCmd;
  353. /* EEM command packet format:
  354. * b0..10: bmEEMCmdParam
  355. * b11..13: bmEEMCmd
  356. * b14: reserved (must be zero)
  357. * b15: bmType (1 == command)
  358. */
  359. if (header & BIT(14))
  360. continue;
  361. bmEEMCmd = (header >> 11) & 0x7;
  362. switch (bmEEMCmd) {
  363. case 0: /* echo */
  364. len = header & 0x7FF;
  365. if (skb->len < len) {
  366. status = -EOVERFLOW;
  367. goto error;
  368. }
  369. skb2 = skb_clone(skb, GFP_ATOMIC);
  370. if (unlikely(!skb2)) {
  371. DBG(cdev, "EEM echo response error\n");
  372. goto next;
  373. }
  374. skb_trim(skb2, len);
  375. put_unaligned_le16(BIT(15) | BIT(11) | len,
  376. skb_push(skb2, 2));
  377. skb_copy_bits(skb2, 0, req->buf, skb2->len);
  378. req->length = skb2->len;
  379. req->complete = eem_cmd_complete;
  380. req->zero = 1;
  381. req->context = skb2;
  382. if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
  383. DBG(cdev, "echo response queue fail\n");
  384. break;
  385. case 1: /* echo response */
  386. case 2: /* suspend hint */
  387. case 3: /* response hint */
  388. case 4: /* response complete hint */
  389. case 5: /* tickle */
  390. default: /* reserved */
  391. continue;
  392. }
  393. } else {
  394. u32 crc, crc2;
  395. struct sk_buff *skb3;
  396. /* check for zero-length EEM packet */
  397. if (header == 0)
  398. continue;
  399. /* EEM data packet format:
  400. * b0..13: length of ethernet frame
  401. * b14: bmCRC (0 == sentinel, 1 == calculated)
  402. * b15: bmType (0 == data)
  403. */
  404. len = header & 0x3FFF;
  405. if ((skb->len < len)
  406. || (len < (ETH_HLEN + ETH_FCS_LEN))) {
  407. status = -EINVAL;
  408. goto error;
  409. }
  410. /* validate CRC */
  411. if (header & BIT(14)) {
  412. crc = get_unaligned_le32(skb->data + len
  413. - ETH_FCS_LEN);
  414. crc2 = ~crc32_le(~0,
  415. skb->data, len - ETH_FCS_LEN);
  416. } else {
  417. crc = get_unaligned_be32(skb->data + len
  418. - ETH_FCS_LEN);
  419. crc2 = 0xdeadbeef;
  420. }
  421. if (crc != crc2) {
  422. DBG(cdev, "invalid EEM CRC\n");
  423. goto next;
  424. }
  425. skb2 = skb_clone(skb, GFP_ATOMIC);
  426. if (unlikely(!skb2)) {
  427. DBG(cdev, "unable to unframe EEM packet\n");
  428. continue;
  429. }
  430. skb_trim(skb2, len - ETH_FCS_LEN);
  431. skb3 = skb_copy_expand(skb2,
  432. NET_IP_ALIGN,
  433. 0,
  434. GFP_ATOMIC);
  435. if (unlikely(!skb3)) {
  436. DBG(cdev, "unable to realign EEM packet\n");
  437. dev_kfree_skb_any(skb2);
  438. continue;
  439. }
  440. dev_kfree_skb_any(skb2);
  441. skb_queue_tail(list, skb3);
  442. }
  443. next:
  444. skb_pull(skb, len);
  445. } while (skb->len);
  446. error:
  447. dev_kfree_skb_any(skb);
  448. return status;
  449. }
  450. /**
  451. * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
  452. * @c: the configuration to support the network link
  453. * Context: single threaded during gadget setup
  454. *
  455. * Returns zero on success, else negative errno.
  456. *
  457. * Caller must have called @gether_setup(). Caller is also responsible
  458. * for calling @gether_cleanup() before module unload.
  459. */
  460. int __init eem_bind_config(struct usb_configuration *c)
  461. {
  462. struct f_eem *eem;
  463. int status;
  464. /* maybe allocate device-global string IDs */
  465. if (eem_string_defs[0].id == 0) {
  466. /* control interface label */
  467. status = usb_string_id(c->cdev);
  468. if (status < 0)
  469. return status;
  470. eem_string_defs[0].id = status;
  471. eem_intf.iInterface = status;
  472. }
  473. /* allocate and initialize one new instance */
  474. eem = kzalloc(sizeof *eem, GFP_KERNEL);
  475. if (!eem)
  476. return -ENOMEM;
  477. eem->port.cdc_filter = DEFAULT_FILTER;
  478. eem->port.func.name = "cdc_eem";
  479. eem->port.func.strings = eem_strings;
  480. /* descriptors are per-instance copies */
  481. eem->port.func.bind = eem_bind;
  482. eem->port.func.unbind = eem_unbind;
  483. eem->port.func.set_alt = eem_set_alt;
  484. eem->port.func.setup = eem_setup;
  485. eem->port.func.disable = eem_disable;
  486. eem->port.wrap = eem_wrap;
  487. eem->port.unwrap = eem_unwrap;
  488. eem->port.header_len = EEM_HLEN;
  489. status = usb_add_function(c, &eem->port.func);
  490. if (status)
  491. kfree(eem);
  492. return status;
  493. }