f_ncm.c 37 KB

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