cdc_ncm.c 34 KB

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