f_eem.c 15 KB

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