f_ncm.c 35 KB

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