f_ncm.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * f_ncm.c -- USB CDC Network (NCM) link function driver
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
  6. *
  7. * The driver borrows from f_ecm.c which is:
  8. *
  9. * Copyright (C) 2003-2005,2008 David Brownell
  10. * Copyright (C) 2008 Nokia Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/crc32.h>
  22. #include <linux/usb/cdc.h>
  23. #include "u_ether.h"
  24. #include "u_ether_configfs.h"
  25. #include "u_ncm.h"
  26. /*
  27. * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
  28. * NCM is intended to be used with high-speed network attachments.
  29. *
  30. * Note that NCM requires the use of "alternate settings" for its data
  31. * interface. This means that the set_alt() method has real work to do,
  32. * and also means that a get_alt() method is required.
  33. */
  34. /* to trigger crc/non-crc ndp signature */
  35. #define NCM_NDP_HDR_CRC_MASK 0x01000000
  36. #define NCM_NDP_HDR_CRC 0x01000000
  37. #define NCM_NDP_HDR_NOCRC 0x00000000
  38. enum ncm_notify_state {
  39. NCM_NOTIFY_NONE, /* don't notify */
  40. NCM_NOTIFY_CONNECT, /* issue CONNECT next */
  41. NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
  42. };
  43. struct f_ncm {
  44. struct gether port;
  45. u8 ctrl_id, data_id;
  46. char ethaddr[14];
  47. struct usb_ep *notify;
  48. struct usb_request *notify_req;
  49. u8 notify_state;
  50. bool is_open;
  51. const struct ndp_parser_opts *parser_opts;
  52. bool is_crc;
  53. u32 ndp_sign;
  54. /*
  55. * for notification, it is accessed from both
  56. * callback and ethernet open/close
  57. */
  58. spinlock_t lock;
  59. struct net_device *netdev;
  60. /* For multi-frame NDP TX */
  61. struct sk_buff *skb_tx_data;
  62. struct sk_buff *skb_tx_ndp;
  63. u16 ndp_dgram_count;
  64. bool timer_force_tx;
  65. struct tasklet_struct tx_tasklet;
  66. struct hrtimer task_timer;
  67. bool timer_stopping;
  68. };
  69. static inline struct f_ncm *func_to_ncm(struct usb_function *f)
  70. {
  71. return container_of(f, struct f_ncm, port.func);
  72. }
  73. /* peak (theoretical) bulk transfer rate in bits-per-second */
  74. static inline unsigned ncm_bitrate(struct usb_gadget *g)
  75. {
  76. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  77. return 13 * 512 * 8 * 1000 * 8;
  78. else
  79. return 19 * 64 * 1 * 1000 * 8;
  80. }
  81. /*-------------------------------------------------------------------------*/
  82. /*
  83. * We cannot group frames so use just the minimal size which ok to put
  84. * one max-size ethernet frame.
  85. * If the host can group frames, allow it to do that, 16K is selected,
  86. * because it's used by default by the current linux host driver
  87. */
  88. #define NTB_DEFAULT_IN_SIZE 16384
  89. #define NTB_OUT_SIZE 16384
  90. /* Allocation for storing the NDP, 32 should suffice for a
  91. * 16k packet. This allows a maximum of 32 * 507 Byte packets to
  92. * be transmitted in a single 16kB skb, though when sending full size
  93. * packets this limit will be plenty.
  94. * Smaller packets are not likely to be trying to maximize the
  95. * throughput and will be mstly sending smaller infrequent frames.
  96. */
  97. #define TX_MAX_NUM_DPE 32
  98. /* Delay for the transmit to wait before sending an unfilled NTB frame. */
  99. #define TX_TIMEOUT_NSECS 300000
  100. #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
  101. USB_CDC_NCM_NTB32_SUPPORTED)
  102. static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
  103. .wLength = cpu_to_le16(sizeof(ntb_parameters)),
  104. .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
  105. .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
  106. .wNdpInDivisor = cpu_to_le16(4),
  107. .wNdpInPayloadRemainder = cpu_to_le16(0),
  108. .wNdpInAlignment = cpu_to_le16(4),
  109. .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
  110. .wNdpOutDivisor = cpu_to_le16(4),
  111. .wNdpOutPayloadRemainder = cpu_to_le16(0),
  112. .wNdpOutAlignment = cpu_to_le16(4),
  113. };
  114. /*
  115. * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  116. * packet, to simplify cancellation; and a big transfer interval, to
  117. * waste less bandwidth.
  118. */
  119. #define NCM_STATUS_INTERVAL_MS 32
  120. #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
  121. static struct usb_interface_assoc_descriptor ncm_iad_desc = {
  122. .bLength = sizeof ncm_iad_desc,
  123. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  124. /* .bFirstInterface = DYNAMIC, */
  125. .bInterfaceCount = 2, /* control + data */
  126. .bFunctionClass = USB_CLASS_COMM,
  127. .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
  128. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  129. /* .iFunction = DYNAMIC */
  130. };
  131. /* interface descriptor: */
  132. static struct usb_interface_descriptor ncm_control_intf = {
  133. .bLength = sizeof ncm_control_intf,
  134. .bDescriptorType = USB_DT_INTERFACE,
  135. /* .bInterfaceNumber = DYNAMIC */
  136. .bNumEndpoints = 1,
  137. .bInterfaceClass = USB_CLASS_COMM,
  138. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  139. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  140. /* .iInterface = DYNAMIC */
  141. };
  142. static struct usb_cdc_header_desc ncm_header_desc = {
  143. .bLength = sizeof ncm_header_desc,
  144. .bDescriptorType = USB_DT_CS_INTERFACE,
  145. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  146. .bcdCDC = cpu_to_le16(0x0110),
  147. };
  148. static struct usb_cdc_union_desc ncm_union_desc = {
  149. .bLength = sizeof(ncm_union_desc),
  150. .bDescriptorType = USB_DT_CS_INTERFACE,
  151. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  152. /* .bMasterInterface0 = DYNAMIC */
  153. /* .bSlaveInterface0 = DYNAMIC */
  154. };
  155. static struct usb_cdc_ether_desc ecm_desc = {
  156. .bLength = sizeof ecm_desc,
  157. .bDescriptorType = USB_DT_CS_INTERFACE,
  158. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  159. /* this descriptor actually adds value, surprise! */
  160. /* .iMACAddress = DYNAMIC */
  161. .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
  162. .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
  163. .wNumberMCFilters = cpu_to_le16(0),
  164. .bNumberPowerFilters = 0,
  165. };
  166. #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
  167. static struct usb_cdc_ncm_desc ncm_desc = {
  168. .bLength = sizeof ncm_desc,
  169. .bDescriptorType = USB_DT_CS_INTERFACE,
  170. .bDescriptorSubType = USB_CDC_NCM_TYPE,
  171. .bcdNcmVersion = cpu_to_le16(0x0100),
  172. /* can process SetEthernetPacketFilter */
  173. .bmNetworkCapabilities = NCAPS,
  174. };
  175. /* the default data interface has no endpoints ... */
  176. static struct usb_interface_descriptor ncm_data_nop_intf = {
  177. .bLength = sizeof ncm_data_nop_intf,
  178. .bDescriptorType = USB_DT_INTERFACE,
  179. .bInterfaceNumber = 1,
  180. .bAlternateSetting = 0,
  181. .bNumEndpoints = 0,
  182. .bInterfaceClass = USB_CLASS_CDC_DATA,
  183. .bInterfaceSubClass = 0,
  184. .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
  185. /* .iInterface = DYNAMIC */
  186. };
  187. /* ... but the "real" data interface has two bulk endpoints */
  188. static struct usb_interface_descriptor ncm_data_intf = {
  189. .bLength = sizeof ncm_data_intf,
  190. .bDescriptorType = USB_DT_INTERFACE,
  191. .bInterfaceNumber = 1,
  192. .bAlternateSetting = 1,
  193. .bNumEndpoints = 2,
  194. .bInterfaceClass = USB_CLASS_CDC_DATA,
  195. .bInterfaceSubClass = 0,
  196. .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
  197. /* .iInterface = DYNAMIC */
  198. };
  199. /* full speed support: */
  200. static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
  201. .bLength = USB_DT_ENDPOINT_SIZE,
  202. .bDescriptorType = USB_DT_ENDPOINT,
  203. .bEndpointAddress = USB_DIR_IN,
  204. .bmAttributes = USB_ENDPOINT_XFER_INT,
  205. .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
  206. .bInterval = NCM_STATUS_INTERVAL_MS,
  207. };
  208. static struct usb_endpoint_descriptor fs_ncm_in_desc = {
  209. .bLength = USB_DT_ENDPOINT_SIZE,
  210. .bDescriptorType = USB_DT_ENDPOINT,
  211. .bEndpointAddress = USB_DIR_IN,
  212. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  213. };
  214. static struct usb_endpoint_descriptor fs_ncm_out_desc = {
  215. .bLength = USB_DT_ENDPOINT_SIZE,
  216. .bDescriptorType = USB_DT_ENDPOINT,
  217. .bEndpointAddress = USB_DIR_OUT,
  218. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  219. };
  220. static struct usb_descriptor_header *ncm_fs_function[] = {
  221. (struct usb_descriptor_header *) &ncm_iad_desc,
  222. /* CDC NCM control descriptors */
  223. (struct usb_descriptor_header *) &ncm_control_intf,
  224. (struct usb_descriptor_header *) &ncm_header_desc,
  225. (struct usb_descriptor_header *) &ncm_union_desc,
  226. (struct usb_descriptor_header *) &ecm_desc,
  227. (struct usb_descriptor_header *) &ncm_desc,
  228. (struct usb_descriptor_header *) &fs_ncm_notify_desc,
  229. /* data interface, altsettings 0 and 1 */
  230. (struct usb_descriptor_header *) &ncm_data_nop_intf,
  231. (struct usb_descriptor_header *) &ncm_data_intf,
  232. (struct usb_descriptor_header *) &fs_ncm_in_desc,
  233. (struct usb_descriptor_header *) &fs_ncm_out_desc,
  234. NULL,
  235. };
  236. /* high speed support: */
  237. static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
  238. .bLength = USB_DT_ENDPOINT_SIZE,
  239. .bDescriptorType = USB_DT_ENDPOINT,
  240. .bEndpointAddress = USB_DIR_IN,
  241. .bmAttributes = USB_ENDPOINT_XFER_INT,
  242. .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
  243. .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
  244. };
  245. static struct usb_endpoint_descriptor hs_ncm_in_desc = {
  246. .bLength = USB_DT_ENDPOINT_SIZE,
  247. .bDescriptorType = USB_DT_ENDPOINT,
  248. .bEndpointAddress = USB_DIR_IN,
  249. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  250. .wMaxPacketSize = cpu_to_le16(512),
  251. };
  252. static struct usb_endpoint_descriptor hs_ncm_out_desc = {
  253. .bLength = USB_DT_ENDPOINT_SIZE,
  254. .bDescriptorType = USB_DT_ENDPOINT,
  255. .bEndpointAddress = USB_DIR_OUT,
  256. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  257. .wMaxPacketSize = cpu_to_le16(512),
  258. };
  259. static struct usb_descriptor_header *ncm_hs_function[] = {
  260. (struct usb_descriptor_header *) &ncm_iad_desc,
  261. /* CDC NCM control descriptors */
  262. (struct usb_descriptor_header *) &ncm_control_intf,
  263. (struct usb_descriptor_header *) &ncm_header_desc,
  264. (struct usb_descriptor_header *) &ncm_union_desc,
  265. (struct usb_descriptor_header *) &ecm_desc,
  266. (struct usb_descriptor_header *) &ncm_desc,
  267. (struct usb_descriptor_header *) &hs_ncm_notify_desc,
  268. /* data interface, altsettings 0 and 1 */
  269. (struct usb_descriptor_header *) &ncm_data_nop_intf,
  270. (struct usb_descriptor_header *) &ncm_data_intf,
  271. (struct usb_descriptor_header *) &hs_ncm_in_desc,
  272. (struct usb_descriptor_header *) &hs_ncm_out_desc,
  273. NULL,
  274. };
  275. /* string descriptors: */
  276. #define STRING_CTRL_IDX 0
  277. #define STRING_MAC_IDX 1
  278. #define STRING_DATA_IDX 2
  279. #define STRING_IAD_IDX 3
  280. static struct usb_string ncm_string_defs[] = {
  281. [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
  282. [STRING_MAC_IDX].s = "",
  283. [STRING_DATA_IDX].s = "CDC Network Data",
  284. [STRING_IAD_IDX].s = "CDC NCM",
  285. { } /* end of list */
  286. };
  287. static struct usb_gadget_strings ncm_string_table = {
  288. .language = 0x0409, /* en-us */
  289. .strings = ncm_string_defs,
  290. };
  291. static struct usb_gadget_strings *ncm_strings[] = {
  292. &ncm_string_table,
  293. NULL,
  294. };
  295. /*
  296. * Here are options for NCM Datagram Pointer table (NDP) parser.
  297. * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
  298. * in NDP16 offsets and sizes fields are 1 16bit word wide,
  299. * in NDP32 -- 2 16bit words wide. Also signatures are different.
  300. * To make the parser code the same, put the differences in the structure,
  301. * and switch pointers to the structures when the format is changed.
  302. */
  303. struct ndp_parser_opts {
  304. u32 nth_sign;
  305. u32 ndp_sign;
  306. unsigned nth_size;
  307. unsigned ndp_size;
  308. unsigned dpe_size;
  309. unsigned ndplen_align;
  310. /* sizes in u16 units */
  311. unsigned dgram_item_len; /* index or length */
  312. unsigned block_length;
  313. unsigned ndp_index;
  314. unsigned reserved1;
  315. unsigned reserved2;
  316. unsigned next_ndp_index;
  317. };
  318. #define INIT_NDP16_OPTS { \
  319. .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
  320. .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
  321. .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
  322. .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
  323. .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
  324. .ndplen_align = 4, \
  325. .dgram_item_len = 1, \
  326. .block_length = 1, \
  327. .ndp_index = 1, \
  328. .reserved1 = 0, \
  329. .reserved2 = 0, \
  330. .next_ndp_index = 1, \
  331. }
  332. #define INIT_NDP32_OPTS { \
  333. .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
  334. .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
  335. .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
  336. .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
  337. .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
  338. .ndplen_align = 8, \
  339. .dgram_item_len = 2, \
  340. .block_length = 2, \
  341. .ndp_index = 2, \
  342. .reserved1 = 1, \
  343. .reserved2 = 2, \
  344. .next_ndp_index = 2, \
  345. }
  346. static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
  347. static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
  348. static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
  349. {
  350. switch (size) {
  351. case 1:
  352. put_unaligned_le16((u16)val, *p);
  353. break;
  354. case 2:
  355. put_unaligned_le32((u32)val, *p);
  356. break;
  357. default:
  358. BUG();
  359. }
  360. *p += size;
  361. }
  362. static inline unsigned get_ncm(__le16 **p, unsigned size)
  363. {
  364. unsigned tmp;
  365. switch (size) {
  366. case 1:
  367. tmp = get_unaligned_le16(*p);
  368. break;
  369. case 2:
  370. tmp = get_unaligned_le32(*p);
  371. break;
  372. default:
  373. BUG();
  374. }
  375. *p += size;
  376. return tmp;
  377. }
  378. /*-------------------------------------------------------------------------*/
  379. static inline void ncm_reset_values(struct f_ncm *ncm)
  380. {
  381. ncm->parser_opts = &ndp16_opts;
  382. ncm->is_crc = false;
  383. ncm->port.cdc_filter = DEFAULT_FILTER;
  384. /* doesn't make sense for ncm, fixed size used */
  385. ncm->port.header_len = 0;
  386. ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
  387. ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
  388. }
  389. /*
  390. * Context: ncm->lock held
  391. */
  392. static void ncm_do_notify(struct f_ncm *ncm)
  393. {
  394. struct usb_request *req = ncm->notify_req;
  395. struct usb_cdc_notification *event;
  396. struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
  397. __le32 *data;
  398. int status;
  399. /* notification already in flight? */
  400. if (!req)
  401. return;
  402. event = req->buf;
  403. switch (ncm->notify_state) {
  404. case NCM_NOTIFY_NONE:
  405. return;
  406. case NCM_NOTIFY_CONNECT:
  407. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  408. if (ncm->is_open)
  409. event->wValue = cpu_to_le16(1);
  410. else
  411. event->wValue = cpu_to_le16(0);
  412. event->wLength = 0;
  413. req->length = sizeof *event;
  414. DBG(cdev, "notify connect %s\n",
  415. ncm->is_open ? "true" : "false");
  416. ncm->notify_state = NCM_NOTIFY_NONE;
  417. break;
  418. case NCM_NOTIFY_SPEED:
  419. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  420. event->wValue = cpu_to_le16(0);
  421. event->wLength = cpu_to_le16(8);
  422. req->length = NCM_STATUS_BYTECOUNT;
  423. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  424. data = req->buf + sizeof *event;
  425. data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
  426. data[1] = data[0];
  427. DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
  428. ncm->notify_state = NCM_NOTIFY_CONNECT;
  429. break;
  430. }
  431. event->bmRequestType = 0xA1;
  432. event->wIndex = cpu_to_le16(ncm->ctrl_id);
  433. ncm->notify_req = NULL;
  434. /*
  435. * In double buffering if there is a space in FIFO,
  436. * completion callback can be called right after the call,
  437. * so unlocking
  438. */
  439. spin_unlock(&ncm->lock);
  440. status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
  441. spin_lock(&ncm->lock);
  442. if (status < 0) {
  443. ncm->notify_req = req;
  444. DBG(cdev, "notify --> %d\n", status);
  445. }
  446. }
  447. /*
  448. * Context: ncm->lock held
  449. */
  450. static void ncm_notify(struct f_ncm *ncm)
  451. {
  452. /*
  453. * NOTE on most versions of Linux, host side cdc-ethernet
  454. * won't listen for notifications until its netdevice opens.
  455. * The first notification then sits in the FIFO for a long
  456. * time, and the second one is queued.
  457. *
  458. * If ncm_notify() is called before the second (CONNECT)
  459. * notification is sent, then it will reset to send the SPEED
  460. * notificaion again (and again, and again), but it's not a problem
  461. */
  462. ncm->notify_state = NCM_NOTIFY_SPEED;
  463. ncm_do_notify(ncm);
  464. }
  465. static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
  466. {
  467. struct f_ncm *ncm = req->context;
  468. struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
  469. struct usb_cdc_notification *event = req->buf;
  470. spin_lock(&ncm->lock);
  471. switch (req->status) {
  472. case 0:
  473. VDBG(cdev, "Notification %02x sent\n",
  474. event->bNotificationType);
  475. break;
  476. case -ECONNRESET:
  477. case -ESHUTDOWN:
  478. ncm->notify_state = NCM_NOTIFY_NONE;
  479. break;
  480. default:
  481. DBG(cdev, "event %02x --> %d\n",
  482. event->bNotificationType, req->status);
  483. break;
  484. }
  485. ncm->notify_req = req;
  486. ncm_do_notify(ncm);
  487. spin_unlock(&ncm->lock);
  488. }
  489. static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
  490. {
  491. /* now for SET_NTB_INPUT_SIZE only */
  492. unsigned in_size;
  493. struct usb_function *f = req->context;
  494. struct f_ncm *ncm = func_to_ncm(f);
  495. struct usb_composite_dev *cdev = ep->driver_data;
  496. req->context = NULL;
  497. if (req->status || req->actual != req->length) {
  498. DBG(cdev, "Bad control-OUT transfer\n");
  499. goto invalid;
  500. }
  501. in_size = get_unaligned_le32(req->buf);
  502. if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
  503. in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
  504. DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
  505. goto invalid;
  506. }
  507. ncm->port.fixed_in_len = in_size;
  508. VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
  509. return;
  510. invalid:
  511. usb_ep_set_halt(ep);
  512. return;
  513. }
  514. static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  515. {
  516. struct f_ncm *ncm = func_to_ncm(f);
  517. struct usb_composite_dev *cdev = f->config->cdev;
  518. struct usb_request *req = cdev->req;
  519. int value = -EOPNOTSUPP;
  520. u16 w_index = le16_to_cpu(ctrl->wIndex);
  521. u16 w_value = le16_to_cpu(ctrl->wValue);
  522. u16 w_length = le16_to_cpu(ctrl->wLength);
  523. /*
  524. * composite driver infrastructure handles everything except
  525. * CDC class messages; interface activation uses set_alt().
  526. */
  527. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  528. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  529. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  530. /*
  531. * see 6.2.30: no data, wIndex = interface,
  532. * wValue = packet filter bitmap
  533. */
  534. if (w_length != 0 || w_index != ncm->ctrl_id)
  535. goto invalid;
  536. DBG(cdev, "packet filter %02x\n", w_value);
  537. /*
  538. * REVISIT locking of cdc_filter. This assumes the UDC
  539. * driver won't have a concurrent packet TX irq running on
  540. * another CPU; or that if it does, this write is atomic...
  541. */
  542. ncm->port.cdc_filter = w_value;
  543. value = 0;
  544. break;
  545. /*
  546. * and optionally:
  547. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  548. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  549. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  550. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  551. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  552. * case USB_CDC_GET_ETHERNET_STATISTIC:
  553. */
  554. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  555. | USB_CDC_GET_NTB_PARAMETERS:
  556. if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
  557. goto invalid;
  558. value = w_length > sizeof ntb_parameters ?
  559. sizeof ntb_parameters : w_length;
  560. memcpy(req->buf, &ntb_parameters, value);
  561. VDBG(cdev, "Host asked NTB parameters\n");
  562. break;
  563. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  564. | USB_CDC_GET_NTB_INPUT_SIZE:
  565. if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
  566. goto invalid;
  567. put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
  568. value = 4;
  569. VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
  570. ncm->port.fixed_in_len);
  571. break;
  572. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  573. | USB_CDC_SET_NTB_INPUT_SIZE:
  574. {
  575. if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
  576. goto invalid;
  577. req->complete = ncm_ep0out_complete;
  578. req->length = w_length;
  579. req->context = f;
  580. value = req->length;
  581. break;
  582. }
  583. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  584. | USB_CDC_GET_NTB_FORMAT:
  585. {
  586. uint16_t format;
  587. if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
  588. goto invalid;
  589. format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
  590. put_unaligned_le16(format, req->buf);
  591. value = 2;
  592. VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
  593. break;
  594. }
  595. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  596. | USB_CDC_SET_NTB_FORMAT:
  597. {
  598. if (w_length != 0 || w_index != ncm->ctrl_id)
  599. goto invalid;
  600. switch (w_value) {
  601. case 0x0000:
  602. ncm->parser_opts = &ndp16_opts;
  603. DBG(cdev, "NCM16 selected\n");
  604. break;
  605. case 0x0001:
  606. ncm->parser_opts = &ndp32_opts;
  607. DBG(cdev, "NCM32 selected\n");
  608. break;
  609. default:
  610. goto invalid;
  611. }
  612. value = 0;
  613. break;
  614. }
  615. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  616. | USB_CDC_GET_CRC_MODE:
  617. {
  618. uint16_t is_crc;
  619. if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
  620. goto invalid;
  621. is_crc = ncm->is_crc ? 0x0001 : 0x0000;
  622. put_unaligned_le16(is_crc, req->buf);
  623. value = 2;
  624. VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
  625. break;
  626. }
  627. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  628. | USB_CDC_SET_CRC_MODE:
  629. {
  630. int ndp_hdr_crc = 0;
  631. if (w_length != 0 || w_index != ncm->ctrl_id)
  632. goto invalid;
  633. switch (w_value) {
  634. case 0x0000:
  635. ncm->is_crc = false;
  636. ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
  637. DBG(cdev, "non-CRC mode selected\n");
  638. break;
  639. case 0x0001:
  640. ncm->is_crc = true;
  641. ndp_hdr_crc = NCM_NDP_HDR_CRC;
  642. DBG(cdev, "CRC mode selected\n");
  643. break;
  644. default:
  645. goto invalid;
  646. }
  647. ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
  648. value = 0;
  649. break;
  650. }
  651. /* and disabled in ncm descriptor: */
  652. /* case USB_CDC_GET_NET_ADDRESS: */
  653. /* case USB_CDC_SET_NET_ADDRESS: */
  654. /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
  655. /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
  656. default:
  657. invalid:
  658. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  659. ctrl->bRequestType, ctrl->bRequest,
  660. w_value, w_index, w_length);
  661. }
  662. /* respond with data transfer or status phase? */
  663. if (value >= 0) {
  664. DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
  665. ctrl->bRequestType, ctrl->bRequest,
  666. w_value, w_index, w_length);
  667. req->zero = 0;
  668. req->length = value;
  669. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  670. if (value < 0)
  671. ERROR(cdev, "ncm req %02x.%02x response err %d\n",
  672. ctrl->bRequestType, ctrl->bRequest,
  673. value);
  674. }
  675. /* device either stalls (value < 0) or reports success */
  676. return value;
  677. }
  678. static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  679. {
  680. struct f_ncm *ncm = func_to_ncm(f);
  681. struct usb_composite_dev *cdev = f->config->cdev;
  682. /* Control interface has only altsetting 0 */
  683. if (intf == ncm->ctrl_id) {
  684. if (alt != 0)
  685. goto fail;
  686. if (ncm->notify->driver_data) {
  687. DBG(cdev, "reset ncm control %d\n", intf);
  688. usb_ep_disable(ncm->notify);
  689. }
  690. if (!(ncm->notify->desc)) {
  691. DBG(cdev, "init ncm ctrl %d\n", intf);
  692. if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
  693. goto fail;
  694. }
  695. usb_ep_enable(ncm->notify);
  696. ncm->notify->driver_data = ncm;
  697. /* Data interface has two altsettings, 0 and 1 */
  698. } else if (intf == ncm->data_id) {
  699. if (alt > 1)
  700. goto fail;
  701. if (ncm->port.in_ep->driver_data) {
  702. DBG(cdev, "reset ncm\n");
  703. ncm->timer_stopping = true;
  704. ncm->netdev = NULL;
  705. gether_disconnect(&ncm->port);
  706. ncm_reset_values(ncm);
  707. }
  708. /*
  709. * CDC Network only sends data in non-default altsettings.
  710. * Changing altsettings resets filters, statistics, etc.
  711. */
  712. if (alt == 1) {
  713. struct net_device *net;
  714. if (!ncm->port.in_ep->desc ||
  715. !ncm->port.out_ep->desc) {
  716. DBG(cdev, "init ncm\n");
  717. if (config_ep_by_speed(cdev->gadget, f,
  718. ncm->port.in_ep) ||
  719. config_ep_by_speed(cdev->gadget, f,
  720. ncm->port.out_ep)) {
  721. ncm->port.in_ep->desc = NULL;
  722. ncm->port.out_ep->desc = NULL;
  723. goto fail;
  724. }
  725. }
  726. /* TODO */
  727. /* Enable zlps by default for NCM conformance;
  728. * override for musb_hdrc (avoids txdma ovhead)
  729. */
  730. ncm->port.is_zlp_ok = !(
  731. gadget_is_musbhdrc(cdev->gadget)
  732. );
  733. ncm->port.cdc_filter = DEFAULT_FILTER;
  734. DBG(cdev, "activate ncm\n");
  735. net = gether_connect(&ncm->port);
  736. if (IS_ERR(net))
  737. return PTR_ERR(net);
  738. ncm->netdev = net;
  739. ncm->timer_stopping = false;
  740. }
  741. spin_lock(&ncm->lock);
  742. ncm_notify(ncm);
  743. spin_unlock(&ncm->lock);
  744. } else
  745. goto fail;
  746. return 0;
  747. fail:
  748. return -EINVAL;
  749. }
  750. /*
  751. * Because the data interface supports multiple altsettings,
  752. * this NCM function *MUST* implement a get_alt() method.
  753. */
  754. static int ncm_get_alt(struct usb_function *f, unsigned intf)
  755. {
  756. struct f_ncm *ncm = func_to_ncm(f);
  757. if (intf == ncm->ctrl_id)
  758. return 0;
  759. return ncm->port.in_ep->driver_data ? 1 : 0;
  760. }
  761. static struct sk_buff *package_for_tx(struct f_ncm *ncm)
  762. {
  763. __le16 *ntb_iter;
  764. struct sk_buff *skb2 = NULL;
  765. unsigned ndp_pad;
  766. unsigned ndp_index;
  767. unsigned new_len;
  768. const struct ndp_parser_opts *opts = ncm->parser_opts;
  769. const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
  770. const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
  771. /* Stop the timer */
  772. hrtimer_try_to_cancel(&ncm->task_timer);
  773. ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
  774. ncm->skb_tx_data->len;
  775. ndp_index = ncm->skb_tx_data->len + ndp_pad;
  776. new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
  777. /* Set the final BlockLength and wNdpIndex */
  778. ntb_iter = (void *) ncm->skb_tx_data->data;
  779. /* Increment pointer to BlockLength */
  780. ntb_iter += 2 + 1 + 1;
  781. put_ncm(&ntb_iter, opts->block_length, new_len);
  782. put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
  783. /* Set the final NDP wLength */
  784. new_len = opts->ndp_size +
  785. (ncm->ndp_dgram_count * dgram_idx_len);
  786. ncm->ndp_dgram_count = 0;
  787. /* Increment from start to wLength */
  788. ntb_iter = (void *) ncm->skb_tx_ndp->data;
  789. ntb_iter += 2;
  790. put_unaligned_le16(new_len, ntb_iter);
  791. /* Merge the skbs */
  792. swap(skb2, ncm->skb_tx_data);
  793. if (ncm->skb_tx_data) {
  794. dev_kfree_skb_any(ncm->skb_tx_data);
  795. ncm->skb_tx_data = NULL;
  796. }
  797. /* Insert NDP alignment. */
  798. ntb_iter = (void *) skb_put(skb2, ndp_pad);
  799. memset(ntb_iter, 0, ndp_pad);
  800. /* Copy NTB across. */
  801. ntb_iter = (void *) skb_put(skb2, ncm->skb_tx_ndp->len);
  802. memcpy(ntb_iter, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
  803. dev_kfree_skb_any(ncm->skb_tx_ndp);
  804. ncm->skb_tx_ndp = NULL;
  805. /* Insert zero'd datagram. */
  806. ntb_iter = (void *) skb_put(skb2, dgram_idx_len);
  807. memset(ntb_iter, 0, dgram_idx_len);
  808. return skb2;
  809. }
  810. static struct sk_buff *ncm_wrap_ntb(struct gether *port,
  811. struct sk_buff *skb)
  812. {
  813. struct f_ncm *ncm = func_to_ncm(&port->func);
  814. struct sk_buff *skb2 = NULL;
  815. int ncb_len = 0;
  816. __le16 *ntb_data;
  817. __le16 *ntb_ndp;
  818. int dgram_pad;
  819. unsigned max_size = ncm->port.fixed_in_len;
  820. const struct ndp_parser_opts *opts = ncm->parser_opts;
  821. const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
  822. const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
  823. const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
  824. const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
  825. if (!skb && !ncm->skb_tx_data)
  826. return NULL;
  827. if (skb) {
  828. /* Add the CRC if required up front */
  829. if (ncm->is_crc) {
  830. uint32_t crc;
  831. __le16 *crc_pos;
  832. crc = ~crc32_le(~0,
  833. skb->data,
  834. skb->len);
  835. crc_pos = (void *) skb_put(skb, sizeof(uint32_t));
  836. put_unaligned_le32(crc, crc_pos);
  837. }
  838. /* If the new skb is too big for the current NCM NTB then
  839. * set the current stored skb to be sent now and clear it
  840. * ready for new data.
  841. * NOTE: Assume maximum align for speed of calculation.
  842. */
  843. if (ncm->skb_tx_data
  844. && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
  845. || (ncm->skb_tx_data->len +
  846. div + rem + skb->len +
  847. ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
  848. > max_size)) {
  849. skb2 = package_for_tx(ncm);
  850. if (!skb2)
  851. goto err;
  852. }
  853. if (!ncm->skb_tx_data) {
  854. ncb_len = opts->nth_size;
  855. dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
  856. ncb_len += dgram_pad;
  857. /* Create a new skb for the NTH and datagrams. */
  858. ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
  859. if (!ncm->skb_tx_data)
  860. goto err;
  861. ntb_data = (void *) skb_put(ncm->skb_tx_data, ncb_len);
  862. memset(ntb_data, 0, ncb_len);
  863. /* dwSignature */
  864. put_unaligned_le32(opts->nth_sign, ntb_data);
  865. ntb_data += 2;
  866. /* wHeaderLength */
  867. put_unaligned_le16(opts->nth_size, ntb_data++);
  868. /* Allocate an skb for storing the NDP,
  869. * TX_MAX_NUM_DPE should easily suffice for a
  870. * 16k packet.
  871. */
  872. ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
  873. + opts->dpe_size
  874. * TX_MAX_NUM_DPE),
  875. GFP_ATOMIC);
  876. if (!ncm->skb_tx_ndp)
  877. goto err;
  878. ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp,
  879. opts->ndp_size);
  880. memset(ntb_ndp, 0, ncb_len);
  881. /* dwSignature */
  882. put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
  883. ntb_ndp += 2;
  884. /* There is always a zeroed entry */
  885. ncm->ndp_dgram_count = 1;
  886. /* Note: we skip opts->next_ndp_index */
  887. }
  888. /* Delay the timer. */
  889. hrtimer_start(&ncm->task_timer,
  890. ktime_set(0, TX_TIMEOUT_NSECS),
  891. HRTIMER_MODE_REL);
  892. /* Add the datagram position entries */
  893. ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp, dgram_idx_len);
  894. memset(ntb_ndp, 0, dgram_idx_len);
  895. ncb_len = ncm->skb_tx_data->len;
  896. dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
  897. ncb_len += dgram_pad;
  898. /* (d)wDatagramIndex */
  899. put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
  900. /* (d)wDatagramLength */
  901. put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
  902. ncm->ndp_dgram_count++;
  903. /* Add the new data to the skb */
  904. ntb_data = (void *) skb_put(ncm->skb_tx_data, dgram_pad);
  905. memset(ntb_data, 0, dgram_pad);
  906. ntb_data = (void *) skb_put(ncm->skb_tx_data, skb->len);
  907. memcpy(ntb_data, skb->data, skb->len);
  908. dev_kfree_skb_any(skb);
  909. skb = NULL;
  910. } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
  911. /* If the tx was requested because of a timeout then send */
  912. skb2 = package_for_tx(ncm);
  913. if (!skb2)
  914. goto err;
  915. }
  916. return skb2;
  917. err:
  918. ncm->netdev->stats.tx_dropped++;
  919. if (skb)
  920. dev_kfree_skb_any(skb);
  921. if (ncm->skb_tx_data)
  922. dev_kfree_skb_any(ncm->skb_tx_data);
  923. if (ncm->skb_tx_ndp)
  924. dev_kfree_skb_any(ncm->skb_tx_ndp);
  925. return NULL;
  926. }
  927. /*
  928. * This transmits the NTB if there are frames waiting.
  929. */
  930. static void ncm_tx_tasklet(unsigned long data)
  931. {
  932. struct f_ncm *ncm = (void *)data;
  933. if (ncm->timer_stopping)
  934. return;
  935. /* Only send if data is available. */
  936. if (ncm->skb_tx_data) {
  937. ncm->timer_force_tx = true;
  938. /* XXX This allowance of a NULL skb argument to ndo_start_xmit
  939. * XXX is not sane. The gadget layer should be redesigned so
  940. * XXX that the dev->wrap() invocations to build SKBs is transparent
  941. * XXX and performed in some way outside of the ndo_start_xmit
  942. * XXX interface.
  943. */
  944. ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
  945. ncm->timer_force_tx = false;
  946. }
  947. }
  948. /*
  949. * The transmit should only be run if no skb data has been sent
  950. * for a certain duration.
  951. */
  952. static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
  953. {
  954. struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
  955. tasklet_schedule(&ncm->tx_tasklet);
  956. return HRTIMER_NORESTART;
  957. }
  958. static int ncm_unwrap_ntb(struct gether *port,
  959. struct sk_buff *skb,
  960. struct sk_buff_head *list)
  961. {
  962. struct f_ncm *ncm = func_to_ncm(&port->func);
  963. __le16 *tmp = (void *) skb->data;
  964. unsigned index, index2;
  965. int ndp_index;
  966. unsigned dg_len, dg_len2;
  967. unsigned ndp_len;
  968. struct sk_buff *skb2;
  969. int ret = -EINVAL;
  970. unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
  971. const struct ndp_parser_opts *opts = ncm->parser_opts;
  972. unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
  973. int dgram_counter;
  974. /* dwSignature */
  975. if (get_unaligned_le32(tmp) != opts->nth_sign) {
  976. INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
  977. skb->len);
  978. print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
  979. skb->data, 32, false);
  980. goto err;
  981. }
  982. tmp += 2;
  983. /* wHeaderLength */
  984. if (get_unaligned_le16(tmp++) != opts->nth_size) {
  985. INFO(port->func.config->cdev, "Wrong NTB headersize\n");
  986. goto err;
  987. }
  988. tmp++; /* skip wSequence */
  989. /* (d)wBlockLength */
  990. if (get_ncm(&tmp, opts->block_length) > max_size) {
  991. INFO(port->func.config->cdev, "OUT size exceeded\n");
  992. goto err;
  993. }
  994. ndp_index = get_ncm(&tmp, opts->ndp_index);
  995. /* Run through all the NDP's in the NTB */
  996. do {
  997. /* NCM 3.2 */
  998. if (((ndp_index % 4) != 0) &&
  999. (ndp_index < opts->nth_size)) {
  1000. INFO(port->func.config->cdev, "Bad index: %#X\n",
  1001. ndp_index);
  1002. goto err;
  1003. }
  1004. /* walk through NDP */
  1005. tmp = (void *)(skb->data + ndp_index);
  1006. if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
  1007. INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
  1008. goto err;
  1009. }
  1010. tmp += 2;
  1011. ndp_len = get_unaligned_le16(tmp++);
  1012. /*
  1013. * NCM 3.3.1
  1014. * entry is 2 items
  1015. * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
  1016. * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
  1017. * Each entry is a dgram index and a dgram length.
  1018. */
  1019. if ((ndp_len < opts->ndp_size
  1020. + 2 * 2 * (opts->dgram_item_len * 2))
  1021. || (ndp_len % opts->ndplen_align != 0)) {
  1022. INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
  1023. ndp_len);
  1024. goto err;
  1025. }
  1026. tmp += opts->reserved1;
  1027. /* Check for another NDP (d)wNextNdpIndex */
  1028. ndp_index = get_ncm(&tmp, opts->next_ndp_index);
  1029. tmp += opts->reserved2;
  1030. ndp_len -= opts->ndp_size;
  1031. index2 = get_ncm(&tmp, opts->dgram_item_len);
  1032. dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
  1033. dgram_counter = 0;
  1034. do {
  1035. index = index2;
  1036. dg_len = dg_len2;
  1037. if (dg_len < 14 + crc_len) { /* ethernet hdr + crc */
  1038. INFO(port->func.config->cdev,
  1039. "Bad dgram length: %#X\n", dg_len);
  1040. goto err;
  1041. }
  1042. if (ncm->is_crc) {
  1043. uint32_t crc, crc2;
  1044. crc = get_unaligned_le32(skb->data +
  1045. index + dg_len -
  1046. crc_len);
  1047. crc2 = ~crc32_le(~0,
  1048. skb->data + index,
  1049. dg_len - crc_len);
  1050. if (crc != crc2) {
  1051. INFO(port->func.config->cdev,
  1052. "Bad CRC\n");
  1053. goto err;
  1054. }
  1055. }
  1056. index2 = get_ncm(&tmp, opts->dgram_item_len);
  1057. dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
  1058. /*
  1059. * Copy the data into a new skb.
  1060. * This ensures the truesize is correct
  1061. */
  1062. skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
  1063. dg_len - crc_len);
  1064. if (skb2 == NULL)
  1065. goto err;
  1066. memcpy(skb_put(skb2, dg_len - crc_len),
  1067. skb->data + index, dg_len - crc_len);
  1068. skb_queue_tail(list, skb2);
  1069. ndp_len -= 2 * (opts->dgram_item_len * 2);
  1070. dgram_counter++;
  1071. if (index2 == 0 || dg_len2 == 0)
  1072. break;
  1073. } while (ndp_len > 2 * (opts->dgram_item_len * 2));
  1074. } while (ndp_index);
  1075. dev_kfree_skb_any(skb);
  1076. VDBG(port->func.config->cdev,
  1077. "Parsed NTB with %d frames\n", dgram_counter);
  1078. return 0;
  1079. err:
  1080. skb_queue_purge(list);
  1081. dev_kfree_skb_any(skb);
  1082. return ret;
  1083. }
  1084. static void ncm_disable(struct usb_function *f)
  1085. {
  1086. struct f_ncm *ncm = func_to_ncm(f);
  1087. struct usb_composite_dev *cdev = f->config->cdev;
  1088. DBG(cdev, "ncm deactivated\n");
  1089. if (ncm->port.in_ep->driver_data) {
  1090. ncm->timer_stopping = true;
  1091. ncm->netdev = NULL;
  1092. gether_disconnect(&ncm->port);
  1093. }
  1094. if (ncm->notify->driver_data) {
  1095. usb_ep_disable(ncm->notify);
  1096. ncm->notify->driver_data = NULL;
  1097. ncm->notify->desc = NULL;
  1098. }
  1099. }
  1100. /*-------------------------------------------------------------------------*/
  1101. /*
  1102. * Callbacks let us notify the host about connect/disconnect when the
  1103. * net device is opened or closed.
  1104. *
  1105. * For testing, note that link states on this side include both opened
  1106. * and closed variants of:
  1107. *
  1108. * - disconnected/unconfigured
  1109. * - configured but inactive (data alt 0)
  1110. * - configured and active (data alt 1)
  1111. *
  1112. * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
  1113. * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
  1114. * imply the host is actually polling the notification endpoint, and
  1115. * likewise that "active" doesn't imply it's actually using the data
  1116. * endpoints for traffic.
  1117. */
  1118. static void ncm_open(struct gether *geth)
  1119. {
  1120. struct f_ncm *ncm = func_to_ncm(&geth->func);
  1121. DBG(ncm->port.func.config->cdev, "%s\n", __func__);
  1122. spin_lock(&ncm->lock);
  1123. ncm->is_open = true;
  1124. ncm_notify(ncm);
  1125. spin_unlock(&ncm->lock);
  1126. }
  1127. static void ncm_close(struct gether *geth)
  1128. {
  1129. struct f_ncm *ncm = func_to_ncm(&geth->func);
  1130. DBG(ncm->port.func.config->cdev, "%s\n", __func__);
  1131. spin_lock(&ncm->lock);
  1132. ncm->is_open = false;
  1133. ncm_notify(ncm);
  1134. spin_unlock(&ncm->lock);
  1135. }
  1136. /*-------------------------------------------------------------------------*/
  1137. /* ethernet function driver setup/binding */
  1138. static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
  1139. {
  1140. struct usb_composite_dev *cdev = c->cdev;
  1141. struct f_ncm *ncm = func_to_ncm(f);
  1142. struct usb_string *us;
  1143. int status;
  1144. struct usb_ep *ep;
  1145. struct f_ncm_opts *ncm_opts;
  1146. if (!can_support_ecm(cdev->gadget))
  1147. return -EINVAL;
  1148. ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
  1149. /*
  1150. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  1151. * configurations are bound in sequence with list_for_each_entry,
  1152. * in each configuration its functions are bound in sequence
  1153. * with list_for_each_entry, so we assume no race condition
  1154. * with regard to ncm_opts->bound access
  1155. */
  1156. if (!ncm_opts->bound) {
  1157. mutex_lock(&ncm_opts->lock);
  1158. gether_set_gadget(ncm_opts->net, cdev->gadget);
  1159. status = gether_register_netdev(ncm_opts->net);
  1160. mutex_unlock(&ncm_opts->lock);
  1161. if (status)
  1162. return status;
  1163. ncm_opts->bound = true;
  1164. }
  1165. us = usb_gstrings_attach(cdev, ncm_strings,
  1166. ARRAY_SIZE(ncm_string_defs));
  1167. if (IS_ERR(us))
  1168. return PTR_ERR(us);
  1169. ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
  1170. ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
  1171. ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
  1172. ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
  1173. ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
  1174. /* allocate instance-specific interface IDs */
  1175. status = usb_interface_id(c, f);
  1176. if (status < 0)
  1177. goto fail;
  1178. ncm->ctrl_id = status;
  1179. ncm_iad_desc.bFirstInterface = status;
  1180. ncm_control_intf.bInterfaceNumber = status;
  1181. ncm_union_desc.bMasterInterface0 = status;
  1182. status = usb_interface_id(c, f);
  1183. if (status < 0)
  1184. goto fail;
  1185. ncm->data_id = status;
  1186. ncm_data_nop_intf.bInterfaceNumber = status;
  1187. ncm_data_intf.bInterfaceNumber = status;
  1188. ncm_union_desc.bSlaveInterface0 = status;
  1189. status = -ENODEV;
  1190. /* allocate instance-specific endpoints */
  1191. ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
  1192. if (!ep)
  1193. goto fail;
  1194. ncm->port.in_ep = ep;
  1195. ep->driver_data = cdev; /* claim */
  1196. ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
  1197. if (!ep)
  1198. goto fail;
  1199. ncm->port.out_ep = ep;
  1200. ep->driver_data = cdev; /* claim */
  1201. ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
  1202. if (!ep)
  1203. goto fail;
  1204. ncm->notify = ep;
  1205. ep->driver_data = cdev; /* claim */
  1206. status = -ENOMEM;
  1207. /* allocate notification request and buffer */
  1208. ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1209. if (!ncm->notify_req)
  1210. goto fail;
  1211. ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
  1212. if (!ncm->notify_req->buf)
  1213. goto fail;
  1214. ncm->notify_req->context = ncm;
  1215. ncm->notify_req->complete = ncm_notify_complete;
  1216. /*
  1217. * support all relevant hardware speeds... we expect that when
  1218. * hardware is dual speed, all bulk-capable endpoints work at
  1219. * both speeds
  1220. */
  1221. hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
  1222. hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
  1223. hs_ncm_notify_desc.bEndpointAddress =
  1224. fs_ncm_notify_desc.bEndpointAddress;
  1225. status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
  1226. NULL);
  1227. if (status)
  1228. goto fail;
  1229. /*
  1230. * NOTE: all that is done without knowing or caring about
  1231. * the network link ... which is unavailable to this code
  1232. * until we're activated via set_alt().
  1233. */
  1234. ncm->port.open = ncm_open;
  1235. ncm->port.close = ncm_close;
  1236. tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
  1237. hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1238. ncm->task_timer.function = ncm_tx_timeout;
  1239. DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  1240. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  1241. ncm->port.in_ep->name, ncm->port.out_ep->name,
  1242. ncm->notify->name);
  1243. return 0;
  1244. fail:
  1245. if (ncm->notify_req) {
  1246. kfree(ncm->notify_req->buf);
  1247. usb_ep_free_request(ncm->notify, ncm->notify_req);
  1248. }
  1249. /* we might as well release our claims on endpoints */
  1250. if (ncm->notify)
  1251. ncm->notify->driver_data = NULL;
  1252. if (ncm->port.out_ep)
  1253. ncm->port.out_ep->driver_data = NULL;
  1254. if (ncm->port.in_ep)
  1255. ncm->port.in_ep->driver_data = NULL;
  1256. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  1257. return status;
  1258. }
  1259. static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
  1260. {
  1261. return container_of(to_config_group(item), struct f_ncm_opts,
  1262. func_inst.group);
  1263. }
  1264. /* f_ncm_item_ops */
  1265. USB_ETHERNET_CONFIGFS_ITEM(ncm);
  1266. /* f_ncm_opts_dev_addr */
  1267. USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
  1268. /* f_ncm_opts_host_addr */
  1269. USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
  1270. /* f_ncm_opts_qmult */
  1271. USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
  1272. /* f_ncm_opts_ifname */
  1273. USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
  1274. static struct configfs_attribute *ncm_attrs[] = {
  1275. &f_ncm_opts_dev_addr.attr,
  1276. &f_ncm_opts_host_addr.attr,
  1277. &f_ncm_opts_qmult.attr,
  1278. &f_ncm_opts_ifname.attr,
  1279. NULL,
  1280. };
  1281. static struct config_item_type ncm_func_type = {
  1282. .ct_item_ops = &ncm_item_ops,
  1283. .ct_attrs = ncm_attrs,
  1284. .ct_owner = THIS_MODULE,
  1285. };
  1286. static void ncm_free_inst(struct usb_function_instance *f)
  1287. {
  1288. struct f_ncm_opts *opts;
  1289. opts = container_of(f, struct f_ncm_opts, func_inst);
  1290. if (opts->bound)
  1291. gether_cleanup(netdev_priv(opts->net));
  1292. else
  1293. free_netdev(opts->net);
  1294. kfree(opts);
  1295. }
  1296. static struct usb_function_instance *ncm_alloc_inst(void)
  1297. {
  1298. struct f_ncm_opts *opts;
  1299. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1300. if (!opts)
  1301. return ERR_PTR(-ENOMEM);
  1302. mutex_init(&opts->lock);
  1303. opts->func_inst.free_func_inst = ncm_free_inst;
  1304. opts->net = gether_setup_default();
  1305. if (IS_ERR(opts->net)) {
  1306. struct net_device *net = opts->net;
  1307. kfree(opts);
  1308. return ERR_CAST(net);
  1309. }
  1310. config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
  1311. return &opts->func_inst;
  1312. }
  1313. static void ncm_free(struct usb_function *f)
  1314. {
  1315. struct f_ncm *ncm;
  1316. struct f_ncm_opts *opts;
  1317. ncm = func_to_ncm(f);
  1318. opts = container_of(f->fi, struct f_ncm_opts, func_inst);
  1319. kfree(ncm);
  1320. mutex_lock(&opts->lock);
  1321. opts->refcnt--;
  1322. mutex_unlock(&opts->lock);
  1323. }
  1324. static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
  1325. {
  1326. struct f_ncm *ncm = func_to_ncm(f);
  1327. DBG(c->cdev, "ncm unbind\n");
  1328. hrtimer_cancel(&ncm->task_timer);
  1329. tasklet_kill(&ncm->tx_tasklet);
  1330. ncm_string_defs[0].id = 0;
  1331. usb_free_all_descriptors(f);
  1332. kfree(ncm->notify_req->buf);
  1333. usb_ep_free_request(ncm->notify, ncm->notify_req);
  1334. }
  1335. static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
  1336. {
  1337. struct f_ncm *ncm;
  1338. struct f_ncm_opts *opts;
  1339. int status;
  1340. /* allocate and initialize one new instance */
  1341. ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
  1342. if (!ncm)
  1343. return ERR_PTR(-ENOMEM);
  1344. opts = container_of(fi, struct f_ncm_opts, func_inst);
  1345. mutex_lock(&opts->lock);
  1346. opts->refcnt++;
  1347. /* export host's Ethernet address in CDC format */
  1348. status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
  1349. sizeof(ncm->ethaddr));
  1350. if (status < 12) { /* strlen("01234567890a") */
  1351. kfree(ncm);
  1352. mutex_unlock(&opts->lock);
  1353. return ERR_PTR(-EINVAL);
  1354. }
  1355. ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
  1356. spin_lock_init(&ncm->lock);
  1357. ncm_reset_values(ncm);
  1358. ncm->port.ioport = netdev_priv(opts->net);
  1359. mutex_unlock(&opts->lock);
  1360. ncm->port.is_fixed = true;
  1361. ncm->port.supports_multi_frame = true;
  1362. ncm->port.func.name = "cdc_network";
  1363. /* descriptors are per-instance copies */
  1364. ncm->port.func.bind = ncm_bind;
  1365. ncm->port.func.unbind = ncm_unbind;
  1366. ncm->port.func.set_alt = ncm_set_alt;
  1367. ncm->port.func.get_alt = ncm_get_alt;
  1368. ncm->port.func.setup = ncm_setup;
  1369. ncm->port.func.disable = ncm_disable;
  1370. ncm->port.func.free_func = ncm_free;
  1371. ncm->port.wrap = ncm_wrap_ntb;
  1372. ncm->port.unwrap = ncm_unwrap_ntb;
  1373. return &ncm->port.func;
  1374. }
  1375. DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
  1376. MODULE_LICENSE("GPL");
  1377. MODULE_AUTHOR("Yauheni Kaliuta");