f_rndis.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * f_rndis.c -- RNDIS link function driver
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6. * Copyright (C) 2008 Nokia Corporation
  7. * Copyright (C) 2009 Samsung Electronics
  8. * Author: Michal Nazarewicz (mina86@mina86.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. /* #define VERBOSE_DEBUG */
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/atomic.h>
  21. #include "u_ether.h"
  22. #include "rndis.h"
  23. /*
  24. * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
  25. * been promoted instead of the standard CDC Ethernet. The published RNDIS
  26. * spec is ambiguous, incomplete, and needlessly complex. Variants such as
  27. * ActiveSync have even worse status in terms of specification.
  28. *
  29. * In short: it's a protocol controlled by (and for) Microsoft, not for an
  30. * Open ecosystem or markets. Linux supports it *only* because Microsoft
  31. * doesn't support the CDC Ethernet standard.
  32. *
  33. * The RNDIS data transfer model is complex, with multiple Ethernet packets
  34. * per USB message, and out of band data. The control model is built around
  35. * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
  36. * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
  37. * useless (they're ignored). RNDIS expects to be the only function in its
  38. * configuration, so it's no real help if you need composite devices; and
  39. * it expects to be the first configuration too.
  40. *
  41. * There is a single technical advantage of RNDIS over CDC Ethernet, if you
  42. * discount the fluff that its RPC can be made to deliver: it doesn't need
  43. * a NOP altsetting for the data interface. That lets it work on some of the
  44. * "so smart it's stupid" hardware which takes over configuration changes
  45. * from the software, and adds restrictions like "no altsettings".
  46. *
  47. * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
  48. * have all sorts of contrary-to-specification oddities that can prevent
  49. * them from working sanely. Since bugfixes (or accurate specs, letting
  50. * Linux work around those bugs) are unlikely to ever come from MSFT, you
  51. * may want to avoid using RNDIS on purely operational grounds.
  52. *
  53. * Omissions from the RNDIS 1.0 specification include:
  54. *
  55. * - Power management ... references data that's scattered around lots
  56. * of other documentation, which is incorrect/incomplete there too.
  57. *
  58. * - There are various undocumented protocol requirements, like the need
  59. * to send garbage in some control-OUT messages.
  60. *
  61. * - MS-Windows drivers sometimes emit undocumented requests.
  62. */
  63. static unsigned int rndis_dl_max_pkt_per_xfer = 3;
  64. module_param(rndis_dl_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
  65. MODULE_PARM_DESC(rndis_dl_max_pkt_per_xfer,
  66. "Maximum packets per transfer for DL aggregation");
  67. static unsigned int rndis_ul_max_pkt_per_xfer = 3;
  68. module_param(rndis_ul_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
  69. MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer,
  70. "Maximum packets per transfer for UL aggregation");
  71. struct f_rndis {
  72. struct gether port;
  73. u8 ctrl_id, data_id;
  74. u8 ethaddr[ETH_ALEN];
  75. u32 vendorID;
  76. const char *manufacturer;
  77. int config;
  78. struct usb_ep *notify;
  79. struct usb_request *notify_req;
  80. atomic_t notify_count;
  81. };
  82. static inline struct f_rndis *func_to_rndis(struct usb_function *f)
  83. {
  84. return container_of(f, struct f_rndis, port.func);
  85. }
  86. /* peak (theoretical) bulk transfer rate in bits-per-second */
  87. static unsigned int bitrate(struct usb_gadget *g)
  88. {
  89. if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
  90. return 13 * 1024 * 8 * 1000 * 8;
  91. else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  92. return 13 * 512 * 8 * 1000 * 8;
  93. else
  94. return 19 * 64 * 1 * 1000 * 8;
  95. }
  96. /*-------------------------------------------------------------------------*/
  97. /*
  98. */
  99. #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  100. #define STATUS_BYTECOUNT 8 /* 8 bytes data */
  101. /* interface descriptor: */
  102. static struct usb_interface_descriptor rndis_control_intf = {
  103. .bLength = sizeof rndis_control_intf,
  104. .bDescriptorType = USB_DT_INTERFACE,
  105. /* .bInterfaceNumber = DYNAMIC */
  106. /* status endpoint is optional; this could be patched later */
  107. .bNumEndpoints = 1,
  108. .bInterfaceClass = USB_CLASS_COMM,
  109. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  110. .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
  111. /* .iInterface = DYNAMIC */
  112. };
  113. static struct usb_cdc_header_desc header_desc = {
  114. .bLength = sizeof header_desc,
  115. .bDescriptorType = USB_DT_CS_INTERFACE,
  116. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  117. .bcdCDC = cpu_to_le16(0x0110),
  118. };
  119. static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
  120. .bLength = sizeof call_mgmt_descriptor,
  121. .bDescriptorType = USB_DT_CS_INTERFACE,
  122. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  123. .bmCapabilities = 0x00,
  124. .bDataInterface = 0x01,
  125. };
  126. static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
  127. .bLength = sizeof rndis_acm_descriptor,
  128. .bDescriptorType = USB_DT_CS_INTERFACE,
  129. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  130. .bmCapabilities = 0x00,
  131. };
  132. #if defined(CONFIG_MACH_MILLETLTE_VZW) || defined(CONFIG_MACH_MATISSELTE_VZW) \
  133. || defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_SLTE_VZW) \
  134. || defined(CONFIG_MACH_CHAGALL_VZW) || defined(CONFIG_MACH_KLIMT_VZW)
  135. /* In VZW Models size of MTU is fixed using Devguru AVD Descriptor */
  136. struct usb_rndis_mtu_avd_descriptor {
  137. __u8 bLength;
  138. __u8 bDescriptorType;
  139. __u8 bDescriptorSubType;
  140. __u16 bDAU1_Type;
  141. __u16 bDAU1_Length;
  142. __u32 bDAU1_Value;
  143. __u16 bDAU2_Type;
  144. __u16 bDAU2_Length;
  145. __u8 bDAU2_Value;
  146. } __attribute__ ((packed));
  147. static struct usb_rndis_mtu_avd_descriptor rndis_avd_descriptor = {
  148. .bLength = 0x10,
  149. .bDescriptorType = 0x24,
  150. .bDescriptorSubType = 0x80,
  151. /* First DAU = MTU Size */
  152. .bDAU1_Type = 0x000A,
  153. .bDAU1_Length = 0x0004,
  154. .bDAU1_Value = 0x00000594, /* 1428Byte */
  155. /* Second DAU = Rndis version */
  156. .bDAU2_Type = 0x000B,
  157. .bDAU2_Length = 0x0001,
  158. .bDAU2_Value = 0x01, /* Rndis5.1 */
  159. };
  160. #endif
  161. static struct usb_cdc_union_desc rndis_union_desc = {
  162. .bLength = sizeof(rndis_union_desc),
  163. .bDescriptorType = USB_DT_CS_INTERFACE,
  164. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  165. /* .bMasterInterface0 = DYNAMIC */
  166. /* .bSlaveInterface0 = DYNAMIC */
  167. };
  168. /* the data interface has two bulk endpoints */
  169. static struct usb_interface_descriptor rndis_data_intf = {
  170. .bLength = sizeof rndis_data_intf,
  171. .bDescriptorType = USB_DT_INTERFACE,
  172. /* .bInterfaceNumber = DYNAMIC */
  173. .bNumEndpoints = 2,
  174. .bInterfaceClass = USB_CLASS_CDC_DATA,
  175. .bInterfaceSubClass = 0,
  176. .bInterfaceProtocol = 0,
  177. /* .iInterface = DYNAMIC */
  178. };
  179. static struct usb_interface_assoc_descriptor
  180. rndis_iad_descriptor = {
  181. .bLength = sizeof rndis_iad_descriptor,
  182. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  183. .bFirstInterface = 0, /* XXX, hardcoded */
  184. .bInterfaceCount = 2, // control + data
  185. .bFunctionClass = USB_CLASS_COMM,
  186. .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
  187. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  188. /* .iFunction = DYNAMIC */
  189. };
  190. /* full speed support: */
  191. static struct usb_endpoint_descriptor fs_notify_desc = {
  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(STATUS_BYTECOUNT),
  197. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  198. };
  199. static struct usb_endpoint_descriptor fs_in_desc = {
  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_out_desc = {
  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 *eth_fs_function[] = {
  212. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  213. /* control interface matches ACM, not Ethernet */
  214. (struct usb_descriptor_header *) &rndis_control_intf,
  215. (struct usb_descriptor_header *) &header_desc,
  216. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  217. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  218. (struct usb_descriptor_header *) &rndis_union_desc,
  219. (struct usb_descriptor_header *) &fs_notify_desc,
  220. /* data interface has no altsetting */
  221. (struct usb_descriptor_header *) &rndis_data_intf,
  222. (struct usb_descriptor_header *) &fs_in_desc,
  223. (struct usb_descriptor_header *) &fs_out_desc,
  224. #if defined(CONFIG_MACH_MILLETLTE_VZW) || defined(CONFIG_MACH_MATISSELTE_VZW) \
  225. || defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_SLTE_VZW) \
  226. || defined(CONFIG_MACH_CHAGALL_VZW) || defined(CONFIG_MACH_KLIMT_VZW)
  227. (struct usb_descriptor_header *) &rndis_avd_descriptor,
  228. #endif
  229. NULL,
  230. };
  231. /* high speed support: */
  232. static struct usb_endpoint_descriptor hs_notify_desc = {
  233. .bLength = USB_DT_ENDPOINT_SIZE,
  234. .bDescriptorType = USB_DT_ENDPOINT,
  235. .bEndpointAddress = USB_DIR_IN,
  236. .bmAttributes = USB_ENDPOINT_XFER_INT,
  237. .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
  238. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  239. };
  240. static struct usb_endpoint_descriptor hs_in_desc = {
  241. .bLength = USB_DT_ENDPOINT_SIZE,
  242. .bDescriptorType = USB_DT_ENDPOINT,
  243. .bEndpointAddress = USB_DIR_IN,
  244. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  245. .wMaxPacketSize = cpu_to_le16(512),
  246. };
  247. static struct usb_endpoint_descriptor hs_out_desc = {
  248. .bLength = USB_DT_ENDPOINT_SIZE,
  249. .bDescriptorType = USB_DT_ENDPOINT,
  250. .bEndpointAddress = USB_DIR_OUT,
  251. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  252. .wMaxPacketSize = cpu_to_le16(512),
  253. };
  254. static struct usb_descriptor_header *eth_hs_function[] = {
  255. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  256. /* control interface matches ACM, not Ethernet */
  257. (struct usb_descriptor_header *) &rndis_control_intf,
  258. (struct usb_descriptor_header *) &header_desc,
  259. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  260. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  261. (struct usb_descriptor_header *) &rndis_union_desc,
  262. (struct usb_descriptor_header *) &hs_notify_desc,
  263. /* data interface has no altsetting */
  264. (struct usb_descriptor_header *) &rndis_data_intf,
  265. (struct usb_descriptor_header *) &hs_in_desc,
  266. (struct usb_descriptor_header *) &hs_out_desc,
  267. #if defined(CONFIG_MACH_MILLETLTE_VZW) || defined(CONFIG_MACH_MATISSELTE_VZW) \
  268. || defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_SLTE_VZW) \
  269. || defined(CONFIG_MACH_CHAGALL_VZW) || defined(CONFIG_MACH_KLIMT_VZW)
  270. (struct usb_descriptor_header *) &rndis_avd_descriptor,
  271. #endif
  272. NULL,
  273. };
  274. /* super speed support: */
  275. static struct usb_endpoint_descriptor ss_notify_desc = {
  276. .bLength = USB_DT_ENDPOINT_SIZE,
  277. .bDescriptorType = USB_DT_ENDPOINT,
  278. .bEndpointAddress = USB_DIR_IN,
  279. .bmAttributes = USB_ENDPOINT_XFER_INT,
  280. .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
  281. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  282. };
  283. static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
  284. .bLength = sizeof ss_intr_comp_desc,
  285. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  286. /* the following 3 values can be tweaked if necessary */
  287. /* .bMaxBurst = 0, */
  288. /* .bmAttributes = 0, */
  289. .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
  290. };
  291. static struct usb_endpoint_descriptor ss_in_desc = {
  292. .bLength = USB_DT_ENDPOINT_SIZE,
  293. .bDescriptorType = USB_DT_ENDPOINT,
  294. .bEndpointAddress = USB_DIR_IN,
  295. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  296. .wMaxPacketSize = cpu_to_le16(1024),
  297. };
  298. static struct usb_endpoint_descriptor ss_out_desc = {
  299. .bLength = USB_DT_ENDPOINT_SIZE,
  300. .bDescriptorType = USB_DT_ENDPOINT,
  301. .bEndpointAddress = USB_DIR_OUT,
  302. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  303. .wMaxPacketSize = cpu_to_le16(1024),
  304. };
  305. static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
  306. .bLength = sizeof ss_bulk_comp_desc,
  307. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  308. /* the following 2 values can be tweaked if necessary */
  309. /* .bMaxBurst = 0, */
  310. /* .bmAttributes = 0, */
  311. };
  312. static struct usb_descriptor_header *eth_ss_function[] = {
  313. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  314. /* control interface matches ACM, not Ethernet */
  315. (struct usb_descriptor_header *) &rndis_control_intf,
  316. (struct usb_descriptor_header *) &header_desc,
  317. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  318. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  319. (struct usb_descriptor_header *) &rndis_union_desc,
  320. (struct usb_descriptor_header *) &ss_notify_desc,
  321. (struct usb_descriptor_header *) &ss_intr_comp_desc,
  322. /* data interface has no altsetting */
  323. (struct usb_descriptor_header *) &rndis_data_intf,
  324. (struct usb_descriptor_header *) &ss_in_desc,
  325. (struct usb_descriptor_header *) &ss_bulk_comp_desc,
  326. (struct usb_descriptor_header *) &ss_out_desc,
  327. (struct usb_descriptor_header *) &ss_bulk_comp_desc,
  328. #if defined(CONFIG_MACH_MILLETLTE_VZW) || defined(CONFIG_MACH_MATISSELTE_VZW) \
  329. || defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_SLTE_VZW) \
  330. || defined(CONFIG_MACH_CHAGALL_VZW) || defined(CONFIG_MACH_KLIMT_VZW)
  331. (struct usb_descriptor_header *) &rndis_avd_descriptor,
  332. #endif
  333. NULL,
  334. };
  335. /* string descriptors: */
  336. static struct usb_string rndis_string_defs[] = {
  337. [0].s = "RNDIS Communications Control",
  338. [1].s = "RNDIS Ethernet Data",
  339. [2].s = "RNDIS",
  340. { } /* end of list */
  341. };
  342. static struct usb_gadget_strings rndis_string_table = {
  343. .language = 0x0409, /* en-us */
  344. .strings = rndis_string_defs,
  345. };
  346. static struct usb_gadget_strings *rndis_strings[] = {
  347. &rndis_string_table,
  348. NULL,
  349. };
  350. /*-------------------------------------------------------------------------*/
  351. static struct sk_buff *rndis_add_header(struct gether *port,
  352. struct sk_buff *skb)
  353. {
  354. struct sk_buff *skb2;
  355. struct rndis_packet_msg_type *header = NULL;
  356. struct f_rndis *rndis = func_to_rndis(&port->func);
  357. if (rndis->port.multi_pkt_xfer) {
  358. if (port->header) {
  359. header = port->header;
  360. memset(header, 0, sizeof(*header));
  361. header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
  362. header->MessageLength = cpu_to_le32(skb->len +
  363. sizeof(*header));
  364. header->DataOffset = cpu_to_le32(36);
  365. header->DataLength = cpu_to_le32(skb->len);
  366. pr_debug("MessageLength:%d DataLength:%d\n",
  367. header->MessageLength,
  368. header->DataLength);
  369. return skb;
  370. } else {
  371. pr_err("RNDIS header is NULL.\n");
  372. return NULL;
  373. }
  374. } else {
  375. skb2 = skb_realloc_headroom(skb,
  376. sizeof(struct rndis_packet_msg_type));
  377. if (skb2)
  378. rndis_add_hdr(skb2);
  379. dev_kfree_skb_any(skb);
  380. return skb2;
  381. }
  382. }
  383. static void rndis_response_available(void *_rndis)
  384. {
  385. struct f_rndis *rndis = _rndis;
  386. struct usb_request *req = rndis->notify_req;
  387. struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
  388. __le32 *data = req->buf;
  389. int status;
  390. if (atomic_inc_return(&rndis->notify_count) != 1)
  391. return;
  392. if (!rndis->notify->driver_data)
  393. return;
  394. /* Send RNDIS RESPONSE_AVAILABLE notification; a
  395. * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
  396. *
  397. * This is the only notification defined by RNDIS.
  398. */
  399. data[0] = cpu_to_le32(1);
  400. data[1] = cpu_to_le32(0);
  401. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  402. if (status) {
  403. atomic_dec(&rndis->notify_count);
  404. DBG(cdev, "notify/0 --> %d\n", status);
  405. }
  406. }
  407. static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
  408. {
  409. struct f_rndis *rndis = req->context;
  410. struct usb_composite_dev *cdev;
  411. int status = req->status;
  412. if (!rndis->port.func.config || !rndis->port.func.config->cdev)
  413. return;
  414. else
  415. cdev = rndis->port.func.config->cdev;
  416. /* after TX:
  417. * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
  418. * - RNDIS_RESPONSE_AVAILABLE (status/irq)
  419. */
  420. switch (status) {
  421. case -ECONNRESET:
  422. case -ESHUTDOWN:
  423. /* connection gone */
  424. atomic_set(&rndis->notify_count, 0);
  425. break;
  426. default:
  427. DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
  428. ep->name, status,
  429. req->actual, req->length);
  430. /* FALLTHROUGH */
  431. case 0:
  432. if (ep != rndis->notify)
  433. break;
  434. /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
  435. * notifications by resending until we're done
  436. */
  437. if (atomic_dec_and_test(&rndis->notify_count))
  438. break;
  439. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  440. if (status) {
  441. atomic_dec(&rndis->notify_count);
  442. DBG(cdev, "notify/1 --> %d\n", status);
  443. }
  444. break;
  445. }
  446. }
  447. static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
  448. {
  449. struct f_rndis *rndis = req->context;
  450. struct usb_composite_dev *cdev;
  451. int status;
  452. rndis_init_msg_type *buf;
  453. if (!rndis->port.func.config || !rndis->port.func.config->cdev)
  454. return;
  455. else
  456. cdev = rndis->port.func.config->cdev;
  457. /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
  458. // spin_lock(&dev->lock);
  459. status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
  460. if (status < 0)
  461. ERROR(cdev, "RNDIS command error %d, %d/%d\n",
  462. status, req->actual, req->length);
  463. buf = (rndis_init_msg_type *)req->buf;
  464. if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
  465. if (buf->MaxTransferSize > 2048)
  466. rndis->port.multi_pkt_xfer = 1;
  467. else
  468. rndis->port.multi_pkt_xfer = 0;
  469. DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
  470. __func__, buf->MaxTransferSize,
  471. rndis->port.multi_pkt_xfer ? "enabled" :
  472. "disabled");
  473. if (rndis_dl_max_pkt_per_xfer <= 1)
  474. rndis->port.multi_pkt_xfer = 0;
  475. }
  476. // spin_unlock(&dev->lock);
  477. }
  478. static int
  479. rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  480. {
  481. struct f_rndis *rndis = func_to_rndis(f);
  482. struct usb_composite_dev *cdev = f->config->cdev;
  483. struct usb_request *req = cdev->req;
  484. int value = -EOPNOTSUPP;
  485. u16 w_index = le16_to_cpu(ctrl->wIndex);
  486. u16 w_value = le16_to_cpu(ctrl->wValue);
  487. u16 w_length = le16_to_cpu(ctrl->wLength);
  488. /* composite driver infrastructure handles everything except
  489. * CDC class messages; interface activation uses set_alt().
  490. */
  491. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  492. /* RNDIS uses the CDC command encapsulation mechanism to implement
  493. * an RPC scheme, with much getting/setting of attributes by OID.
  494. */
  495. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  496. | USB_CDC_SEND_ENCAPSULATED_COMMAND:
  497. if (w_value || w_index != rndis->ctrl_id)
  498. goto invalid;
  499. /* read the request; process it later */
  500. value = w_length;
  501. req->complete = rndis_command_complete;
  502. req->context = rndis;
  503. /* later, rndis_response_available() sends a notification */
  504. break;
  505. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  506. | USB_CDC_GET_ENCAPSULATED_RESPONSE:
  507. if (w_value || w_index != rndis->ctrl_id)
  508. goto invalid;
  509. else {
  510. u8 *buf;
  511. u32 n;
  512. /* return the result */
  513. buf = rndis_get_next_response(rndis->config, &n);
  514. if (buf) {
  515. memcpy(req->buf, buf, n);
  516. req->complete = rndis_response_complete;
  517. req->context = rndis;
  518. rndis_free_response(rndis->config, buf);
  519. value = n;
  520. }
  521. /* else stalls ... spec says to avoid that */
  522. }
  523. break;
  524. default:
  525. invalid:
  526. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  527. ctrl->bRequestType, ctrl->bRequest,
  528. w_value, w_index, w_length);
  529. }
  530. /* respond with data transfer or status phase? */
  531. if (value >= 0) {
  532. DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
  533. ctrl->bRequestType, ctrl->bRequest,
  534. w_value, w_index, w_length);
  535. req->zero = (value < w_length);
  536. req->length = value;
  537. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  538. if (value < 0)
  539. ERROR(cdev, "rndis response on err %d\n", value);
  540. }
  541. /* device either stalls (value < 0) or reports success */
  542. return value;
  543. }
  544. static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  545. {
  546. struct f_rndis *rndis = func_to_rndis(f);
  547. struct usb_composite_dev *cdev = f->config->cdev;
  548. /* we know alt == 0 */
  549. if (intf == rndis->ctrl_id) {
  550. if (rndis->notify->driver_data) {
  551. VDBG(cdev, "reset rndis control %d\n", intf);
  552. usb_ep_disable(rndis->notify);
  553. }
  554. if (!rndis->notify->desc) {
  555. VDBG(cdev, "init rndis ctrl %d\n", intf);
  556. if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
  557. goto fail;
  558. }
  559. usb_ep_enable(rndis->notify);
  560. rndis->notify->driver_data = rndis;
  561. } else if (intf == rndis->data_id) {
  562. struct net_device *net;
  563. if (rndis->port.in_ep->driver_data) {
  564. DBG(cdev, "reset rndis\n");
  565. gether_disconnect(&rndis->port);
  566. }
  567. if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
  568. DBG(cdev, "init rndis\n");
  569. if (config_ep_by_speed(cdev->gadget, f,
  570. rndis->port.in_ep) ||
  571. config_ep_by_speed(cdev->gadget, f,
  572. rndis->port.out_ep)) {
  573. rndis->port.in_ep->desc = NULL;
  574. rndis->port.out_ep->desc = NULL;
  575. goto fail;
  576. }
  577. }
  578. /* Avoid ZLPs; they can be troublesome. */
  579. rndis->port.is_zlp_ok = false;
  580. /* RNDIS should be in the "RNDIS uninitialized" state,
  581. * either never activated or after rndis_uninit().
  582. *
  583. * We don't want data to flow here until a nonzero packet
  584. * filter is set, at which point it enters "RNDIS data
  585. * initialized" state ... but we do want the endpoints
  586. * to be activated. It's a strange little state.
  587. *
  588. * REVISIT the RNDIS gadget code has done this wrong for a
  589. * very long time. We need another call to the link layer
  590. * code -- gether_updown(...bool) maybe -- to do it right.
  591. */
  592. rndis->port.cdc_filter = 0;
  593. DBG(cdev, "RNDIS RX/TX early activation ... \n");
  594. net = gether_connect(&rndis->port);
  595. if (IS_ERR(net))
  596. return PTR_ERR(net);
  597. rndis_set_param_dev(rndis->config, net,
  598. &rndis->port.cdc_filter);
  599. } else
  600. goto fail;
  601. return 0;
  602. fail:
  603. return -EINVAL;
  604. }
  605. static void rndis_disable(struct usb_function *f)
  606. {
  607. struct f_rndis *rndis = func_to_rndis(f);
  608. struct usb_composite_dev *cdev = f->config->cdev;
  609. if (!rndis->notify->driver_data)
  610. return;
  611. DBG(cdev, "rndis deactivated\n");
  612. rndis_uninit(rndis->config);
  613. gether_disconnect(&rndis->port);
  614. usb_ep_disable(rndis->notify);
  615. rndis->notify->driver_data = NULL;
  616. rndis->notify->desc = NULL;
  617. }
  618. /*-------------------------------------------------------------------------*/
  619. /*
  620. * This isn't quite the same mechanism as CDC Ethernet, since the
  621. * notification scheme passes less data, but the same set of link
  622. * states must be tested. A key difference is that altsettings are
  623. * not used to tell whether the link should send packets or not.
  624. */
  625. static void rndis_open(struct gether *geth)
  626. {
  627. struct f_rndis *rndis = func_to_rndis(&geth->func);
  628. struct usb_composite_dev *cdev = geth->func.config->cdev;
  629. DBG(cdev, "%s\n", __func__);
  630. rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
  631. bitrate(cdev->gadget) / 100);
  632. rndis_signal_connect(rndis->config);
  633. }
  634. static void rndis_close(struct gether *geth)
  635. {
  636. struct f_rndis *rndis = func_to_rndis(&geth->func);
  637. DBG(geth->func.config->cdev, "%s\n", __func__);
  638. rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
  639. rndis_signal_disconnect(rndis->config);
  640. }
  641. /*-------------------------------------------------------------------------*/
  642. /* ethernet function driver setup/binding */
  643. static int
  644. rndis_bind(struct usb_configuration *c, struct usb_function *f)
  645. {
  646. struct usb_composite_dev *cdev = c->cdev;
  647. struct f_rndis *rndis = func_to_rndis(f);
  648. int status;
  649. struct usb_ep *ep;
  650. /* allocate instance-specific interface IDs */
  651. status = usb_interface_id(c, f);
  652. if (status < 0)
  653. goto fail;
  654. rndis->ctrl_id = status;
  655. rndis_iad_descriptor.bFirstInterface = status;
  656. rndis_control_intf.bInterfaceNumber = status;
  657. rndis_union_desc.bMasterInterface0 = status;
  658. status = usb_interface_id(c, f);
  659. if (status < 0)
  660. goto fail;
  661. rndis->data_id = status;
  662. rndis_data_intf.bInterfaceNumber = status;
  663. rndis_union_desc.bSlaveInterface0 = status;
  664. status = -ENODEV;
  665. /* allocate instance-specific endpoints */
  666. ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
  667. if (!ep)
  668. goto fail;
  669. rndis->port.in_ep = ep;
  670. ep->driver_data = cdev; /* claim */
  671. ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
  672. if (!ep)
  673. goto fail;
  674. rndis->port.out_ep = ep;
  675. ep->driver_data = cdev; /* claim */
  676. /* NOTE: a status/notification endpoint is, strictly speaking,
  677. * optional. We don't treat it that way though! It's simpler,
  678. * and some newer profiles don't treat it as optional.
  679. */
  680. ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
  681. if (!ep)
  682. goto fail;
  683. rndis->notify = ep;
  684. ep->driver_data = cdev; /* claim */
  685. status = -ENOMEM;
  686. /* allocate notification request and buffer */
  687. rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  688. if (!rndis->notify_req)
  689. goto fail;
  690. rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
  691. if (!rndis->notify_req->buf)
  692. goto fail;
  693. rndis->notify_req->length = STATUS_BYTECOUNT;
  694. rndis->notify_req->context = rndis;
  695. rndis->notify_req->complete = rndis_response_complete;
  696. /* support all relevant hardware speeds... we expect that when
  697. * hardware is dual speed, all bulk-capable endpoints work at
  698. * both speeds
  699. */
  700. hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  701. hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  702. hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  703. ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  704. ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  705. ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  706. status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
  707. eth_ss_function);
  708. if (status)
  709. goto fail;
  710. rndis->port.open = rndis_open;
  711. rndis->port.close = rndis_close;
  712. status = rndis_register(rndis_response_available, rndis);
  713. if (status < 0)
  714. goto fail;
  715. rndis->config = status;
  716. rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
  717. rndis_set_host_mac(rndis->config, rndis->ethaddr);
  718. rndis_set_max_pkt_xfer(rndis->config, rndis_ul_max_pkt_per_xfer);
  719. if (rndis->manufacturer && rndis->vendorID &&
  720. rndis_set_param_vendor(rndis->config, rndis->vendorID,
  721. rndis->manufacturer))
  722. goto fail;
  723. /* NOTE: all that is done without knowing or caring about
  724. * the network link ... which is unavailable to this code
  725. * until we're activated via set_alt().
  726. */
  727. DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  728. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  729. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  730. rndis->port.in_ep->name, rndis->port.out_ep->name,
  731. rndis->notify->name);
  732. return 0;
  733. fail:
  734. usb_free_all_descriptors(f);
  735. if (rndis->notify_req) {
  736. kfree(rndis->notify_req->buf);
  737. usb_ep_free_request(rndis->notify, rndis->notify_req);
  738. }
  739. /* we might as well release our claims on endpoints */
  740. if (rndis->notify)
  741. rndis->notify->driver_data = NULL;
  742. if (rndis->port.out_ep)
  743. rndis->port.out_ep->driver_data = NULL;
  744. if (rndis->port.in_ep)
  745. rndis->port.in_ep->driver_data = NULL;
  746. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  747. return status;
  748. }
  749. static void
  750. rndis_unbind(struct usb_configuration *c, struct usb_function *f)
  751. {
  752. struct f_rndis *rndis = func_to_rndis(f);
  753. rndis_deregister(rndis->config);
  754. rndis_exit();
  755. usb_free_all_descriptors(f);
  756. kfree(rndis->notify_req->buf);
  757. usb_ep_free_request(rndis->notify, rndis->notify_req);
  758. kfree(rndis);
  759. }
  760. /* Some controllers can't support RNDIS ... */
  761. static inline bool can_support_rndis(struct usb_configuration *c)
  762. {
  763. /* everything else is *presumably* fine */
  764. return true;
  765. }
  766. /**
  767. * rndis_bind_config - add RNDIS network link to a configuration
  768. * @c: the configuration to support the network link
  769. * @ethaddr: a buffer in which the ethernet address of the host side
  770. * side of the link was recorded
  771. * Context: single threaded during gadget setup
  772. *
  773. * Returns zero on success, else negative errno.
  774. *
  775. * Caller must have called @gether_setup(). Caller is also responsible
  776. * for calling @gether_cleanup() before module unload.
  777. */
  778. int
  779. rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  780. {
  781. return rndis_bind_config_vendor(c, ethaddr, 0, NULL);
  782. }
  783. int
  784. rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  785. u32 vendorID, const char *manufacturer)
  786. {
  787. struct f_rndis *rndis;
  788. int status;
  789. if (!can_support_rndis(c) || !ethaddr)
  790. return -EINVAL;
  791. /* setup RNDIS itself */
  792. status = rndis_init();
  793. if (status < 0)
  794. return status;
  795. /* maybe allocate device-global string IDs */
  796. if (rndis_string_defs[0].id == 0) {
  797. /* control interface label */
  798. status = usb_string_id(c->cdev);
  799. if (status < 0)
  800. return status;
  801. rndis_string_defs[0].id = status;
  802. rndis_control_intf.iInterface = status;
  803. /* data interface label */
  804. status = usb_string_id(c->cdev);
  805. if (status < 0)
  806. return status;
  807. rndis_string_defs[1].id = status;
  808. rndis_data_intf.iInterface = status;
  809. /* IAD iFunction label */
  810. status = usb_string_id(c->cdev);
  811. if (status < 0)
  812. return status;
  813. rndis_string_defs[2].id = status;
  814. rndis_iad_descriptor.iFunction = status;
  815. }
  816. /* allocate and initialize one new instance */
  817. status = -ENOMEM;
  818. rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
  819. if (!rndis)
  820. goto fail;
  821. memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
  822. rndis->vendorID = vendorID;
  823. rndis->manufacturer = manufacturer;
  824. /* RNDIS activates when the host changes this filter */
  825. rndis->port.cdc_filter = 0;
  826. /* RNDIS has special (and complex) framing */
  827. rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
  828. rndis->port.wrap = rndis_add_header;
  829. rndis->port.unwrap = rndis_rm_hdr;
  830. rndis->port.ul_max_pkts_per_xfer = rndis_ul_max_pkt_per_xfer;
  831. rndis->port.dl_max_pkts_per_xfer = rndis_dl_max_pkt_per_xfer;
  832. rndis->port.func.name = "rndis";
  833. rndis->port.func.strings = rndis_strings;
  834. /* descriptors are per-instance copies */
  835. rndis->port.func.bind = rndis_bind;
  836. rndis->port.func.unbind = rndis_unbind;
  837. rndis->port.func.set_alt = rndis_set_alt;
  838. rndis->port.func.setup = rndis_setup;
  839. rndis->port.func.disable = rndis_disable;
  840. status = usb_add_function(c, &rndis->port.func);
  841. if (status) {
  842. kfree(rndis);
  843. fail:
  844. rndis_exit();
  845. }
  846. return status;
  847. }