f_ncm.c 37 KB

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