f_eem.c 14 KB

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