f_ncm.c 41 KB

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