f_ncm.c.orig 47 KB

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