f_ncm.c 47 KB

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