f_ncm.c 36 KB

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