f_eem.c 14 KB

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