f_ncm.c 45 KB

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