f_ncm.c 36 KB

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