f_ncm.c 44 KB

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