f_ncm.c 38 KB

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