cdc_ncm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /*
  2. * cdc_ncm.c
  3. *
  4. * Copyright (C) ST-Ericsson 2010-2011
  5. * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
  6. * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
  7. *
  8. * USB Host Driver for Network Control Model (NCM)
  9. * http://www.usb.org/developers/devclass_docs/NCM10.zip
  10. *
  11. * The NCM encoding, decoding and initialization logic
  12. * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
  13. *
  14. * This software is available to you under a choice of one of two
  15. * licenses. You may choose this file to be licensed under the terms
  16. * of the GNU General Public License (GPL) Version 2 or the 2-clause
  17. * BSD license listed below:
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/init.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/ctype.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/mii.h>
  47. #include <linux/crc32.h>
  48. #include <linux/usb.h>
  49. #include <linux/version.h>
  50. #include <linux/timer.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/atomic.h>
  53. #include <linux/usb/usbnet.h>
  54. #include <linux/usb/cdc.h>
  55. #define DRIVER_VERSION "7-Feb-2011"
  56. /* CDC NCM subclass 3.2.1 */
  57. #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
  58. /* Maximum NTB length */
  59. #define CDC_NCM_NTB_MAX_SIZE_TX 16384 /* bytes */
  60. #define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */
  61. /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
  62. #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
  63. #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
  64. /* Default value for MaxDatagramSize */
  65. #define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */
  66. /*
  67. * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
  68. * the last NULL entry. Any additional datagrams in NTB would be discarded.
  69. */
  70. #define CDC_NCM_DPT_DATAGRAMS_MAX 32
  71. /* Maximum amount of IN datagrams in NTB */
  72. #define CDC_NCM_DPT_DATAGRAMS_IN_MAX 0 /* unlimited */
  73. /* Restart the timer, if amount of datagrams is less than given value */
  74. #define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
  75. /* The following macro defines the minimum header space */
  76. #define CDC_NCM_MIN_HDR_SIZE \
  77. (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
  78. (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
  79. struct cdc_ncm_data {
  80. struct usb_cdc_ncm_nth16 nth16;
  81. struct usb_cdc_ncm_ndp16 ndp16;
  82. struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1];
  83. };
  84. struct cdc_ncm_ctx {
  85. struct cdc_ncm_data rx_ncm;
  86. struct cdc_ncm_data tx_ncm;
  87. struct usb_cdc_ncm_ntb_parameters ncm_parm;
  88. struct timer_list tx_timer;
  89. const struct usb_cdc_ncm_desc *func_desc;
  90. const struct usb_cdc_header_desc *header_desc;
  91. const struct usb_cdc_union_desc *union_desc;
  92. const struct usb_cdc_ether_desc *ether_desc;
  93. struct net_device *netdev;
  94. struct usb_device *udev;
  95. struct usb_host_endpoint *in_ep;
  96. struct usb_host_endpoint *out_ep;
  97. struct usb_host_endpoint *status_ep;
  98. struct usb_interface *intf;
  99. struct usb_interface *control;
  100. struct usb_interface *data;
  101. struct sk_buff *tx_curr_skb;
  102. struct sk_buff *tx_rem_skb;
  103. spinlock_t mtx;
  104. u32 tx_timer_pending;
  105. u32 tx_curr_offset;
  106. u32 tx_curr_last_offset;
  107. u32 tx_curr_frame_num;
  108. u32 rx_speed;
  109. u32 tx_speed;
  110. u32 rx_max;
  111. u32 tx_max;
  112. u32 max_datagram_size;
  113. u16 tx_max_datagrams;
  114. u16 tx_remainder;
  115. u16 tx_modulus;
  116. u16 tx_ndp_modulus;
  117. u16 tx_seq;
  118. u16 connected;
  119. u8 data_claimed;
  120. u8 control_claimed;
  121. };
  122. static void cdc_ncm_tx_timeout(unsigned long arg);
  123. static const struct driver_info cdc_ncm_info;
  124. static struct usb_driver cdc_ncm_driver;
  125. static struct ethtool_ops cdc_ncm_ethtool_ops;
  126. static const struct usb_device_id cdc_devs[] = {
  127. { USB_INTERFACE_INFO(USB_CLASS_COMM,
  128. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  129. .driver_info = (unsigned long)&cdc_ncm_info,
  130. },
  131. {
  132. },
  133. };
  134. MODULE_DEVICE_TABLE(usb, cdc_devs);
  135. static void
  136. cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  137. {
  138. struct usbnet *dev = netdev_priv(net);
  139. strncpy(info->driver, dev->driver_name, sizeof(info->driver));
  140. strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
  141. strncpy(info->fw_version, dev->driver_info->description,
  142. sizeof(info->fw_version));
  143. usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
  144. }
  145. static int
  146. cdc_ncm_do_request(struct cdc_ncm_ctx *ctx, struct usb_cdc_notification *req,
  147. void *data, u16 flags, u16 *actlen, u16 timeout)
  148. {
  149. int err;
  150. err = usb_control_msg(ctx->udev, (req->bmRequestType & USB_DIR_IN) ?
  151. usb_rcvctrlpipe(ctx->udev, 0) :
  152. usb_sndctrlpipe(ctx->udev, 0),
  153. req->bNotificationType, req->bmRequestType,
  154. req->wValue,
  155. req->wIndex, data,
  156. req->wLength, timeout);
  157. if (err < 0) {
  158. if (actlen)
  159. *actlen = 0;
  160. return err;
  161. }
  162. if (actlen)
  163. *actlen = err;
  164. return 0;
  165. }
  166. static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
  167. {
  168. struct usb_cdc_notification req;
  169. u32 val;
  170. u8 flags;
  171. u8 iface_no;
  172. int err;
  173. u16 ntb_fmt_supported;
  174. iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
  175. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
  176. req.bNotificationType = USB_CDC_GET_NTB_PARAMETERS;
  177. req.wValue = 0;
  178. req.wIndex = cpu_to_le16(iface_no);
  179. req.wLength = cpu_to_le16(sizeof(ctx->ncm_parm));
  180. err = cdc_ncm_do_request(ctx, &req, &ctx->ncm_parm, 0, NULL, 1000);
  181. if (err) {
  182. pr_debug("failed GET_NTB_PARAMETERS\n");
  183. return 1;
  184. }
  185. /* read correct set of parameters according to device mode */
  186. ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
  187. ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
  188. ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
  189. ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
  190. ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
  191. /* devices prior to NCM Errata shall set this field to zero */
  192. ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
  193. ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
  194. if (ctx->func_desc != NULL)
  195. flags = ctx->func_desc->bmNetworkCapabilities;
  196. else
  197. flags = 0;
  198. pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
  199. "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
  200. "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
  201. ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
  202. ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
  203. /* max count of tx datagrams */
  204. if ((ctx->tx_max_datagrams == 0) ||
  205. (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
  206. ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
  207. /* verify maximum size of received NTB in bytes */
  208. if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
  209. pr_debug("Using min receive length=%d\n",
  210. USB_CDC_NCM_NTB_MIN_IN_SIZE);
  211. ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
  212. }
  213. if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
  214. pr_debug("Using default maximum receive length=%d\n",
  215. CDC_NCM_NTB_MAX_SIZE_RX);
  216. ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
  217. }
  218. /* inform device about NTB input size changes */
  219. if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
  220. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
  221. USB_RECIP_INTERFACE;
  222. req.bNotificationType = USB_CDC_SET_NTB_INPUT_SIZE;
  223. req.wValue = 0;
  224. req.wIndex = cpu_to_le16(iface_no);
  225. if (flags & USB_CDC_NCM_NCAP_NTB_INPUT_SIZE) {
  226. struct usb_cdc_ncm_ndp_input_size ndp_in_sz;
  227. req.wLength = 8;
  228. ndp_in_sz.dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
  229. ndp_in_sz.wNtbInMaxDatagrams =
  230. cpu_to_le16(CDC_NCM_DPT_DATAGRAMS_MAX);
  231. ndp_in_sz.wReserved = 0;
  232. err = cdc_ncm_do_request(ctx, &req, &ndp_in_sz, 0, NULL,
  233. 1000);
  234. } else {
  235. __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
  236. req.wLength = 4;
  237. err = cdc_ncm_do_request(ctx, &req, &dwNtbInMaxSize, 0,
  238. NULL, 1000);
  239. }
  240. if (err)
  241. pr_debug("Setting NTB Input Size failed\n");
  242. }
  243. /* verify maximum size of transmitted NTB in bytes */
  244. if ((ctx->tx_max <
  245. (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
  246. (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
  247. pr_debug("Using default maximum transmit length=%d\n",
  248. CDC_NCM_NTB_MAX_SIZE_TX);
  249. ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
  250. }
  251. /*
  252. * verify that the structure alignment is:
  253. * - power of two
  254. * - not greater than the maximum transmit length
  255. * - not less than four bytes
  256. */
  257. val = ctx->tx_ndp_modulus;
  258. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  259. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  260. pr_debug("Using default alignment: 4 bytes\n");
  261. ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  262. }
  263. /*
  264. * verify that the payload alignment is:
  265. * - power of two
  266. * - not greater than the maximum transmit length
  267. * - not less than four bytes
  268. */
  269. val = ctx->tx_modulus;
  270. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  271. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  272. pr_debug("Using default transmit modulus: 4 bytes\n");
  273. ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  274. }
  275. /* verify the payload remainder */
  276. if (ctx->tx_remainder >= ctx->tx_modulus) {
  277. pr_debug("Using default transmit remainder: 0 bytes\n");
  278. ctx->tx_remainder = 0;
  279. }
  280. /* adjust TX-remainder according to NCM specification. */
  281. ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) &
  282. (ctx->tx_modulus - 1));
  283. /* additional configuration */
  284. /* set CRC Mode */
  285. if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
  286. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
  287. USB_RECIP_INTERFACE;
  288. req.bNotificationType = USB_CDC_SET_CRC_MODE;
  289. req.wValue = cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED);
  290. req.wIndex = cpu_to_le16(iface_no);
  291. req.wLength = 0;
  292. err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
  293. if (err)
  294. pr_debug("Setting CRC mode off failed\n");
  295. }
  296. /* set NTB format, if both formats are supported */
  297. if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
  298. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
  299. USB_RECIP_INTERFACE;
  300. req.bNotificationType = USB_CDC_SET_NTB_FORMAT;
  301. req.wValue = cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT);
  302. req.wIndex = cpu_to_le16(iface_no);
  303. req.wLength = 0;
  304. err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
  305. if (err)
  306. pr_debug("Setting NTB format to 16-bit failed\n");
  307. }
  308. ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
  309. /* set Max Datagram Size (MTU) */
  310. if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
  311. __le16 max_datagram_size;
  312. u16 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
  313. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN |
  314. USB_RECIP_INTERFACE;
  315. req.bNotificationType = USB_CDC_GET_MAX_DATAGRAM_SIZE;
  316. req.wValue = 0;
  317. req.wIndex = cpu_to_le16(iface_no);
  318. req.wLength = cpu_to_le16(2);
  319. err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 0, NULL,
  320. 1000);
  321. if (err) {
  322. pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
  323. CDC_NCM_MIN_DATAGRAM_SIZE);
  324. } else {
  325. ctx->max_datagram_size = le16_to_cpu(max_datagram_size);
  326. /* Check Eth descriptor value */
  327. if (eth_max_sz < CDC_NCM_MAX_DATAGRAM_SIZE) {
  328. if (ctx->max_datagram_size > eth_max_sz)
  329. ctx->max_datagram_size = eth_max_sz;
  330. } else {
  331. if (ctx->max_datagram_size >
  332. CDC_NCM_MAX_DATAGRAM_SIZE)
  333. ctx->max_datagram_size =
  334. CDC_NCM_MAX_DATAGRAM_SIZE;
  335. }
  336. if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
  337. ctx->max_datagram_size =
  338. CDC_NCM_MIN_DATAGRAM_SIZE;
  339. /* if value changed, update device */
  340. req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
  341. USB_RECIP_INTERFACE;
  342. req.bNotificationType = USB_CDC_SET_MAX_DATAGRAM_SIZE;
  343. req.wValue = 0;
  344. req.wIndex = cpu_to_le16(iface_no);
  345. req.wLength = 2;
  346. max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
  347. err = cdc_ncm_do_request(ctx, &req, &max_datagram_size,
  348. 0, NULL, 1000);
  349. if (err)
  350. pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
  351. }
  352. }
  353. if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
  354. ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
  355. return 0;
  356. }
  357. static void
  358. cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
  359. {
  360. struct usb_host_endpoint *e;
  361. u8 ep;
  362. for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
  363. e = intf->cur_altsetting->endpoint + ep;
  364. switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  365. case USB_ENDPOINT_XFER_INT:
  366. if (usb_endpoint_dir_in(&e->desc)) {
  367. if (ctx->status_ep == NULL)
  368. ctx->status_ep = e;
  369. }
  370. break;
  371. case USB_ENDPOINT_XFER_BULK:
  372. if (usb_endpoint_dir_in(&e->desc)) {
  373. if (ctx->in_ep == NULL)
  374. ctx->in_ep = e;
  375. } else {
  376. if (ctx->out_ep == NULL)
  377. ctx->out_ep = e;
  378. }
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. }
  385. static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
  386. {
  387. if (ctx == NULL)
  388. return;
  389. del_timer_sync(&ctx->tx_timer);
  390. if (ctx->data_claimed) {
  391. usb_set_intfdata(ctx->data, NULL);
  392. usb_driver_release_interface(driver_of(ctx->intf), ctx->data);
  393. }
  394. if (ctx->control_claimed) {
  395. usb_set_intfdata(ctx->control, NULL);
  396. usb_driver_release_interface(driver_of(ctx->intf),
  397. ctx->control);
  398. }
  399. if (ctx->tx_rem_skb != NULL) {
  400. dev_kfree_skb_any(ctx->tx_rem_skb);
  401. ctx->tx_rem_skb = NULL;
  402. }
  403. if (ctx->tx_curr_skb != NULL) {
  404. dev_kfree_skb_any(ctx->tx_curr_skb);
  405. ctx->tx_curr_skb = NULL;
  406. }
  407. kfree(ctx);
  408. }
  409. static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
  410. {
  411. struct cdc_ncm_ctx *ctx;
  412. struct usb_driver *driver;
  413. u8 *buf;
  414. int len;
  415. int temp;
  416. u8 iface_no;
  417. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  418. if (ctx == NULL)
  419. goto error;
  420. memset(ctx, 0, sizeof(*ctx));
  421. init_timer(&ctx->tx_timer);
  422. spin_lock_init(&ctx->mtx);
  423. ctx->netdev = dev->net;
  424. /* store ctx pointer in device data field */
  425. dev->data[0] = (unsigned long)ctx;
  426. /* get some pointers */
  427. driver = driver_of(intf);
  428. buf = intf->cur_altsetting->extra;
  429. len = intf->cur_altsetting->extralen;
  430. ctx->udev = dev->udev;
  431. ctx->intf = intf;
  432. /* parse through descriptors associated with control interface */
  433. while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
  434. if (buf[1] != USB_DT_CS_INTERFACE)
  435. goto advance;
  436. switch (buf[2]) {
  437. case USB_CDC_UNION_TYPE:
  438. if (buf[0] < sizeof(*(ctx->union_desc)))
  439. break;
  440. ctx->union_desc =
  441. (const struct usb_cdc_union_desc *)buf;
  442. ctx->control = usb_ifnum_to_if(dev->udev,
  443. ctx->union_desc->bMasterInterface0);
  444. ctx->data = usb_ifnum_to_if(dev->udev,
  445. ctx->union_desc->bSlaveInterface0);
  446. break;
  447. case USB_CDC_ETHERNET_TYPE:
  448. if (buf[0] < sizeof(*(ctx->ether_desc)))
  449. break;
  450. ctx->ether_desc =
  451. (const struct usb_cdc_ether_desc *)buf;
  452. dev->hard_mtu =
  453. le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
  454. if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
  455. dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
  456. else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
  457. dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
  458. break;
  459. case USB_CDC_NCM_TYPE:
  460. if (buf[0] < sizeof(*(ctx->func_desc)))
  461. break;
  462. ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
  463. break;
  464. default:
  465. break;
  466. }
  467. advance:
  468. /* advance to next descriptor */
  469. temp = buf[0];
  470. buf += temp;
  471. len -= temp;
  472. }
  473. /* check if we got everything */
  474. if ((ctx->control == NULL) || (ctx->data == NULL) ||
  475. (ctx->ether_desc == NULL))
  476. goto error;
  477. /* claim interfaces, if any */
  478. if (ctx->data != intf) {
  479. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  480. if (temp)
  481. goto error;
  482. ctx->data_claimed = 1;
  483. }
  484. if (ctx->control != intf) {
  485. temp = usb_driver_claim_interface(driver, ctx->control, dev);
  486. if (temp)
  487. goto error;
  488. ctx->control_claimed = 1;
  489. }
  490. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  491. /* reset data interface */
  492. temp = usb_set_interface(dev->udev, iface_no, 0);
  493. if (temp)
  494. goto error;
  495. /* initialize data interface */
  496. if (cdc_ncm_setup(ctx))
  497. goto error;
  498. /* configure data interface */
  499. temp = usb_set_interface(dev->udev, iface_no, 1);
  500. if (temp)
  501. goto error;
  502. cdc_ncm_find_endpoints(ctx, ctx->data);
  503. cdc_ncm_find_endpoints(ctx, ctx->control);
  504. if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
  505. (ctx->status_ep == NULL))
  506. goto error;
  507. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  508. usb_set_intfdata(ctx->data, dev);
  509. usb_set_intfdata(ctx->control, dev);
  510. usb_set_intfdata(ctx->intf, dev);
  511. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  512. if (temp)
  513. goto error;
  514. dev_info(&dev->udev->dev, "MAC-Address: "
  515. "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
  516. dev->net->dev_addr[0], dev->net->dev_addr[1],
  517. dev->net->dev_addr[2], dev->net->dev_addr[3],
  518. dev->net->dev_addr[4], dev->net->dev_addr[5]);
  519. dev->in = usb_rcvbulkpipe(dev->udev,
  520. ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  521. dev->out = usb_sndbulkpipe(dev->udev,
  522. ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  523. dev->status = ctx->status_ep;
  524. dev->rx_urb_size = ctx->rx_max;
  525. /*
  526. * We should get an event when network connection is "connected" or
  527. * "disconnected". Set network connection in "disconnected" state
  528. * (carrier is OFF) during attach, so the IP network stack does not
  529. * start IPv6 negotiation and more.
  530. */
  531. netif_carrier_off(dev->net);
  532. ctx->tx_speed = ctx->rx_speed = 0;
  533. return 0;
  534. error:
  535. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  536. dev->data[0] = 0;
  537. dev_info(&dev->udev->dev, "Descriptor failure\n");
  538. return -ENODEV;
  539. }
  540. static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  541. {
  542. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  543. struct usb_driver *driver;
  544. if (ctx == NULL)
  545. return; /* no setup */
  546. driver = driver_of(intf);
  547. usb_set_intfdata(ctx->data, NULL);
  548. usb_set_intfdata(ctx->control, NULL);
  549. usb_set_intfdata(ctx->intf, NULL);
  550. /* release interfaces, if any */
  551. if (ctx->data_claimed) {
  552. usb_driver_release_interface(driver, ctx->data);
  553. ctx->data_claimed = 0;
  554. }
  555. if (ctx->control_claimed) {
  556. usb_driver_release_interface(driver, ctx->control);
  557. ctx->control_claimed = 0;
  558. }
  559. cdc_ncm_free(ctx);
  560. }
  561. static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
  562. {
  563. if (first >= max)
  564. return;
  565. if (first >= end)
  566. return;
  567. if (end > max)
  568. end = max;
  569. memset(ptr + first, 0, end - first);
  570. }
  571. static struct sk_buff *
  572. cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
  573. {
  574. struct sk_buff *skb_out;
  575. u32 rem;
  576. u32 offset;
  577. u32 last_offset;
  578. u16 n = 0;
  579. u8 ready2send = 0;
  580. /* if there is a remaining skb, it gets priority */
  581. if (skb != NULL)
  582. swap(skb, ctx->tx_rem_skb);
  583. else
  584. ready2send = 1;
  585. /*
  586. * +----------------+
  587. * | skb_out |
  588. * +----------------+
  589. * ^ offset
  590. * ^ last_offset
  591. */
  592. /* check if we are resuming an OUT skb */
  593. if (ctx->tx_curr_skb != NULL) {
  594. /* pop variables */
  595. skb_out = ctx->tx_curr_skb;
  596. offset = ctx->tx_curr_offset;
  597. last_offset = ctx->tx_curr_last_offset;
  598. n = ctx->tx_curr_frame_num;
  599. } else {
  600. /* reset variables */
  601. skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
  602. if (skb_out == NULL) {
  603. if (skb != NULL) {
  604. dev_kfree_skb_any(skb);
  605. ctx->netdev->stats.tx_dropped++;
  606. }
  607. goto exit_no_skb;
  608. }
  609. /* make room for NTH and NDP */
  610. offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
  611. ctx->tx_ndp_modulus) +
  612. sizeof(struct usb_cdc_ncm_ndp16) +
  613. (ctx->tx_max_datagrams + 1) *
  614. sizeof(struct usb_cdc_ncm_dpe16);
  615. /* store last valid offset before alignment */
  616. last_offset = offset;
  617. /* align first Datagram offset correctly */
  618. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  619. /* zero buffer till the first IP datagram */
  620. cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
  621. n = 0;
  622. ctx->tx_curr_frame_num = 0;
  623. }
  624. for (; n < ctx->tx_max_datagrams; n++) {
  625. /* check if end of transmit buffer is reached */
  626. if (offset >= ctx->tx_max) {
  627. ready2send = 1;
  628. break;
  629. }
  630. /* compute maximum buffer size */
  631. rem = ctx->tx_max - offset;
  632. if (skb == NULL) {
  633. skb = ctx->tx_rem_skb;
  634. ctx->tx_rem_skb = NULL;
  635. /* check for end of skb */
  636. if (skb == NULL)
  637. break;
  638. }
  639. if (skb->len > rem) {
  640. if (n == 0) {
  641. /* won't fit, MTU problem? */
  642. dev_kfree_skb_any(skb);
  643. skb = NULL;
  644. ctx->netdev->stats.tx_dropped++;
  645. } else {
  646. /* no room for skb - store for later */
  647. if (ctx->tx_rem_skb != NULL) {
  648. dev_kfree_skb_any(ctx->tx_rem_skb);
  649. ctx->netdev->stats.tx_dropped++;
  650. }
  651. ctx->tx_rem_skb = skb;
  652. skb = NULL;
  653. ready2send = 1;
  654. }
  655. break;
  656. }
  657. memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
  658. ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
  659. ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
  660. /* update offset */
  661. offset += skb->len;
  662. /* store last valid offset before alignment */
  663. last_offset = offset;
  664. /* align offset correctly */
  665. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  666. /* zero padding */
  667. cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
  668. ctx->tx_max);
  669. dev_kfree_skb_any(skb);
  670. skb = NULL;
  671. }
  672. /* free up any dangling skb */
  673. if (skb != NULL) {
  674. dev_kfree_skb_any(skb);
  675. skb = NULL;
  676. ctx->netdev->stats.tx_dropped++;
  677. }
  678. ctx->tx_curr_frame_num = n;
  679. if (n == 0) {
  680. /* wait for more frames */
  681. /* push variables */
  682. ctx->tx_curr_skb = skb_out;
  683. ctx->tx_curr_offset = offset;
  684. ctx->tx_curr_last_offset = last_offset;
  685. goto exit_no_skb;
  686. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
  687. /* wait for more frames */
  688. /* push variables */
  689. ctx->tx_curr_skb = skb_out;
  690. ctx->tx_curr_offset = offset;
  691. ctx->tx_curr_last_offset = last_offset;
  692. /* set the pending count */
  693. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  694. ctx->tx_timer_pending = 2;
  695. goto exit_no_skb;
  696. } else {
  697. /* frame goes out */
  698. /* variables will be reset at next call */
  699. }
  700. /* check for overflow */
  701. if (last_offset > ctx->tx_max)
  702. last_offset = ctx->tx_max;
  703. /* revert offset */
  704. offset = last_offset;
  705. /*
  706. * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
  707. * we send buffers as it is. If we get more data, it would be more
  708. * efficient for USB HS mobile device with DMA engine to receive a full
  709. * size NTB, than canceling DMA transfer and receiving a short packet.
  710. */
  711. if (offset > CDC_NCM_MIN_TX_PKT)
  712. offset = ctx->tx_max;
  713. /* final zero padding */
  714. cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
  715. /* store last offset */
  716. last_offset = offset;
  717. if ((last_offset < ctx->tx_max) && ((last_offset %
  718. le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) {
  719. /* force short packet */
  720. *(((u8 *)skb_out->data) + last_offset) = 0;
  721. last_offset++;
  722. }
  723. /* zero the rest of the DPEs plus the last NULL entry */
  724. for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
  725. ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
  726. ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
  727. }
  728. /* fill out 16-bit NTB header */
  729. ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  730. ctx->tx_ncm.nth16.wHeaderLength =
  731. cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
  732. ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
  733. ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
  734. ctx->tx_ncm.nth16.wNdpIndex = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
  735. ctx->tx_ndp_modulus);
  736. memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
  737. ctx->tx_seq++;
  738. /* fill out 16-bit NDP table */
  739. ctx->tx_ncm.ndp16.dwSignature =
  740. cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
  741. rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
  742. sizeof(struct usb_cdc_ncm_dpe16));
  743. ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
  744. ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
  745. memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex,
  746. &(ctx->tx_ncm.ndp16),
  747. sizeof(ctx->tx_ncm.ndp16));
  748. memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex +
  749. sizeof(ctx->tx_ncm.ndp16),
  750. &(ctx->tx_ncm.dpe16),
  751. (ctx->tx_curr_frame_num + 1) *
  752. sizeof(struct usb_cdc_ncm_dpe16));
  753. /* set frame length */
  754. skb_put(skb_out, last_offset);
  755. /* return skb */
  756. ctx->tx_curr_skb = NULL;
  757. return skb_out;
  758. exit_no_skb:
  759. return NULL;
  760. }
  761. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  762. {
  763. /* start timer, if not already started */
  764. if (timer_pending(&ctx->tx_timer) == 0) {
  765. ctx->tx_timer.function = &cdc_ncm_tx_timeout;
  766. ctx->tx_timer.data = (unsigned long)ctx;
  767. ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
  768. add_timer(&ctx->tx_timer);
  769. }
  770. }
  771. static void cdc_ncm_tx_timeout(unsigned long arg)
  772. {
  773. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
  774. u8 restart;
  775. spin_lock(&ctx->mtx);
  776. if (ctx->tx_timer_pending != 0) {
  777. ctx->tx_timer_pending--;
  778. restart = 1;
  779. } else {
  780. restart = 0;
  781. }
  782. spin_unlock(&ctx->mtx);
  783. if (restart) {
  784. spin_lock(&ctx->mtx);
  785. cdc_ncm_tx_timeout_start(ctx);
  786. spin_unlock(&ctx->mtx);
  787. } else if (ctx->netdev != NULL) {
  788. usbnet_start_xmit(NULL, ctx->netdev);
  789. }
  790. }
  791. static struct sk_buff *
  792. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  793. {
  794. struct sk_buff *skb_out;
  795. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  796. u8 need_timer = 0;
  797. /*
  798. * The Ethernet API we are using does not support transmitting
  799. * multiple Ethernet frames in a single call. This driver will
  800. * accumulate multiple Ethernet frames and send out a larger
  801. * USB frame when the USB buffer is full or when a single jiffies
  802. * timeout happens.
  803. */
  804. if (ctx == NULL)
  805. goto error;
  806. spin_lock(&ctx->mtx);
  807. skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
  808. if (ctx->tx_curr_skb != NULL)
  809. need_timer = 1;
  810. /* Start timer, if there is a remaining skb */
  811. if (need_timer)
  812. cdc_ncm_tx_timeout_start(ctx);
  813. if (skb_out)
  814. dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
  815. spin_unlock(&ctx->mtx);
  816. return skb_out;
  817. error:
  818. if (skb != NULL)
  819. dev_kfree_skb_any(skb);
  820. return NULL;
  821. }
  822. static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  823. {
  824. struct sk_buff *skb;
  825. struct cdc_ncm_ctx *ctx;
  826. int sumlen;
  827. int actlen;
  828. int temp;
  829. int nframes;
  830. int x;
  831. int offset;
  832. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  833. if (ctx == NULL)
  834. goto error;
  835. actlen = skb_in->len;
  836. sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
  837. if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
  838. pr_debug("frame too short\n");
  839. goto error;
  840. }
  841. memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
  842. sizeof(ctx->rx_ncm.nth16));
  843. if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
  844. USB_CDC_NCM_NTH16_SIGN) {
  845. pr_debug("invalid NTH16 signature <%u>\n",
  846. le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
  847. goto error;
  848. }
  849. temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
  850. if (temp > sumlen) {
  851. pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
  852. goto error;
  853. }
  854. temp = le16_to_cpu(ctx->rx_ncm.nth16.wNdpIndex);
  855. if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
  856. pr_debug("invalid DPT16 index\n");
  857. goto error;
  858. }
  859. memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
  860. sizeof(ctx->rx_ncm.ndp16));
  861. if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
  862. USB_CDC_NCM_NDP16_NOCRC_SIGN) {
  863. pr_debug("invalid DPT16 signature <%u>\n",
  864. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  865. goto error;
  866. }
  867. if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
  868. USB_CDC_NCM_NDP16_LENGTH_MIN) {
  869. pr_debug("invalid DPT16 length <%u>\n",
  870. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  871. goto error;
  872. }
  873. nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
  874. sizeof(struct usb_cdc_ncm_ndp16)) /
  875. sizeof(struct usb_cdc_ncm_dpe16));
  876. nframes--; /* we process NDP entries except for the last one */
  877. pr_debug("nframes = %u\n", nframes);
  878. temp += sizeof(ctx->rx_ncm.ndp16);
  879. if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
  880. pr_debug("Invalid nframes = %d\n", nframes);
  881. goto error;
  882. }
  883. if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
  884. pr_debug("Truncating number of frames from %u to %u\n",
  885. nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
  886. nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
  887. }
  888. memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
  889. nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
  890. for (x = 0; x < nframes; x++) {
  891. offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
  892. temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
  893. /*
  894. * CDC NCM ch. 3.7
  895. * All entries after first NULL entry are to be ignored
  896. */
  897. if ((offset == 0) || (temp == 0)) {
  898. if (!x)
  899. goto error; /* empty NTB */
  900. break;
  901. }
  902. /* sanity checking */
  903. if (((offset + temp) > actlen) ||
  904. (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
  905. pr_debug("invalid frame detected (ignored)"
  906. "offset[%u]=%u, length=%u, skb=%p\n",
  907. x, offset, temp, skb_in);
  908. if (!x)
  909. goto error;
  910. break;
  911. } else {
  912. skb = skb_clone(skb_in, GFP_ATOMIC);
  913. if (!skb)
  914. goto error;
  915. skb->len = temp;
  916. skb->data = ((u8 *)skb_in->data) + offset;
  917. skb_set_tail_pointer(skb, temp);
  918. usbnet_skb_return(dev, skb);
  919. }
  920. }
  921. return 1;
  922. error:
  923. return 0;
  924. }
  925. static void
  926. cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
  927. struct usb_cdc_speed_change *data)
  928. {
  929. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  930. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  931. /*
  932. * Currently the USB-NET API does not support reporting the actual
  933. * device speed. Do print it instead.
  934. */
  935. if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
  936. ctx->tx_speed = tx_speed;
  937. ctx->rx_speed = rx_speed;
  938. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  939. printk(KERN_INFO KBUILD_MODNAME
  940. ": %s: %u mbit/s downlink "
  941. "%u mbit/s uplink\n",
  942. ctx->netdev->name,
  943. (unsigned int)(rx_speed / 1000000U),
  944. (unsigned int)(tx_speed / 1000000U));
  945. } else {
  946. printk(KERN_INFO KBUILD_MODNAME
  947. ": %s: %u kbit/s downlink "
  948. "%u kbit/s uplink\n",
  949. ctx->netdev->name,
  950. (unsigned int)(rx_speed / 1000U),
  951. (unsigned int)(tx_speed / 1000U));
  952. }
  953. }
  954. }
  955. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  956. {
  957. struct cdc_ncm_ctx *ctx;
  958. struct usb_cdc_notification *event;
  959. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  960. if (urb->actual_length < sizeof(*event))
  961. return;
  962. /* test for split data in 8-byte chunks */
  963. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  964. cdc_ncm_speed_change(ctx,
  965. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  966. return;
  967. }
  968. event = urb->transfer_buffer;
  969. switch (event->bNotificationType) {
  970. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  971. /*
  972. * According to the CDC NCM specification ch.7.1
  973. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  974. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  975. */
  976. ctx->connected = event->wValue;
  977. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  978. " %sconnected\n",
  979. ctx->netdev->name, ctx->connected ? "" : "dis");
  980. if (ctx->connected)
  981. netif_carrier_on(dev->net);
  982. else {
  983. netif_carrier_off(dev->net);
  984. ctx->tx_speed = ctx->rx_speed = 0;
  985. }
  986. break;
  987. case USB_CDC_NOTIFY_SPEED_CHANGE:
  988. if (urb->actual_length < (sizeof(*event) +
  989. sizeof(struct usb_cdc_speed_change)))
  990. set_bit(EVENT_STS_SPLIT, &dev->flags);
  991. else
  992. cdc_ncm_speed_change(ctx,
  993. (struct usb_cdc_speed_change *) &event[1]);
  994. break;
  995. default:
  996. dev_err(&dev->udev->dev, "NCM: unexpected "
  997. "notification 0x%02x!\n", event->bNotificationType);
  998. break;
  999. }
  1000. }
  1001. static int cdc_ncm_check_connect(struct usbnet *dev)
  1002. {
  1003. struct cdc_ncm_ctx *ctx;
  1004. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1005. if (ctx == NULL)
  1006. return 1; /* disconnected */
  1007. return !ctx->connected;
  1008. }
  1009. static int
  1010. cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  1011. {
  1012. return usbnet_probe(udev, prod);
  1013. }
  1014. static void cdc_ncm_disconnect(struct usb_interface *intf)
  1015. {
  1016. struct usbnet *dev = usb_get_intfdata(intf);
  1017. if (dev == NULL)
  1018. return; /* already disconnected */
  1019. usbnet_disconnect(intf);
  1020. }
  1021. static int cdc_ncm_manage_power(struct usbnet *dev, int status)
  1022. {
  1023. dev->intf->needs_remote_wakeup = status;
  1024. return 0;
  1025. }
  1026. static const struct driver_info cdc_ncm_info = {
  1027. .description = "CDC NCM",
  1028. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
  1029. .bind = cdc_ncm_bind,
  1030. .unbind = cdc_ncm_unbind,
  1031. .check_connect = cdc_ncm_check_connect,
  1032. .manage_power = cdc_ncm_manage_power,
  1033. .status = cdc_ncm_status,
  1034. .rx_fixup = cdc_ncm_rx_fixup,
  1035. .tx_fixup = cdc_ncm_tx_fixup,
  1036. };
  1037. static struct usb_driver cdc_ncm_driver = {
  1038. .name = "cdc_ncm",
  1039. .id_table = cdc_devs,
  1040. .probe = cdc_ncm_probe,
  1041. .disconnect = cdc_ncm_disconnect,
  1042. .suspend = usbnet_suspend,
  1043. .resume = usbnet_resume,
  1044. .supports_autosuspend = 1,
  1045. };
  1046. static struct ethtool_ops cdc_ncm_ethtool_ops = {
  1047. .get_drvinfo = cdc_ncm_get_drvinfo,
  1048. .get_link = usbnet_get_link,
  1049. .get_msglevel = usbnet_get_msglevel,
  1050. .set_msglevel = usbnet_set_msglevel,
  1051. .get_settings = usbnet_get_settings,
  1052. .set_settings = usbnet_set_settings,
  1053. .nway_reset = usbnet_nway_reset,
  1054. };
  1055. static int __init cdc_ncm_init(void)
  1056. {
  1057. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
  1058. return usb_register(&cdc_ncm_driver);
  1059. }
  1060. module_init(cdc_ncm_init);
  1061. static void __exit cdc_ncm_exit(void)
  1062. {
  1063. usb_deregister(&cdc_ncm_driver);
  1064. }
  1065. module_exit(cdc_ncm_exit);
  1066. MODULE_AUTHOR("Hans Petter Selasky");
  1067. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1068. MODULE_LICENSE("Dual BSD/GPL");