f_ncm.c 45 KB

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