f_ncm.c 45 KB

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