f_ncm.c 37 KB

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