f_eem.c 14 KB

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