f_ncm.c 42 KB

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