f_eem.c 14 KB

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