f_ncm.c 46 KB

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