f_ncm.c 45 KB

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