cdc_ncm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 data interface, if different from control */
  462. if (ctx->data != ctx->control) {
  463. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  464. if (temp)
  465. goto error;
  466. }
  467. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  468. /* reset data interface */
  469. temp = usb_set_interface(dev->udev, iface_no, 0);
  470. if (temp)
  471. goto error2;
  472. /* initialize data interface */
  473. if (cdc_ncm_setup(ctx))
  474. goto error2;
  475. /* configure data interface */
  476. temp = usb_set_interface(dev->udev, iface_no, 1);
  477. if (temp)
  478. goto error2;
  479. cdc_ncm_find_endpoints(ctx, ctx->data);
  480. cdc_ncm_find_endpoints(ctx, ctx->control);
  481. if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
  482. (ctx->status_ep == NULL))
  483. goto error2;
  484. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  485. usb_set_intfdata(ctx->data, dev);
  486. usb_set_intfdata(ctx->control, dev);
  487. usb_set_intfdata(ctx->intf, dev);
  488. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  489. if (temp)
  490. goto error2;
  491. dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
  492. dev->in = usb_rcvbulkpipe(dev->udev,
  493. ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  494. dev->out = usb_sndbulkpipe(dev->udev,
  495. ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  496. dev->status = ctx->status_ep;
  497. dev->rx_urb_size = ctx->rx_max;
  498. /*
  499. * We should get an event when network connection is "connected" or
  500. * "disconnected". Set network connection in "disconnected" state
  501. * (carrier is OFF) during attach, so the IP network stack does not
  502. * start IPv6 negotiation and more.
  503. */
  504. netif_carrier_off(dev->net);
  505. ctx->tx_speed = ctx->rx_speed = 0;
  506. return 0;
  507. error2:
  508. usb_set_intfdata(ctx->control, NULL);
  509. usb_set_intfdata(ctx->data, NULL);
  510. usb_driver_release_interface(driver, ctx->data);
  511. error:
  512. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  513. dev->data[0] = 0;
  514. dev_info(&dev->udev->dev, "bind() failure\n");
  515. return -ENODEV;
  516. }
  517. static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  518. {
  519. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  520. struct usb_driver *driver = driver_of(intf);
  521. if (ctx == NULL)
  522. return; /* no setup */
  523. atomic_set(&ctx->stop, 1);
  524. if (hrtimer_active(&ctx->tx_timer))
  525. hrtimer_cancel(&ctx->tx_timer);
  526. tasklet_kill(&ctx->bh);
  527. /* handle devices with combined control and data interface */
  528. if (ctx->control == ctx->data)
  529. ctx->data = NULL;
  530. /* disconnect master --> disconnect slave */
  531. if (intf == ctx->control && ctx->data) {
  532. usb_set_intfdata(ctx->data, NULL);
  533. usb_driver_release_interface(driver, ctx->data);
  534. ctx->data = NULL;
  535. } else if (intf == ctx->data && ctx->control) {
  536. usb_set_intfdata(ctx->control, NULL);
  537. usb_driver_release_interface(driver, ctx->control);
  538. ctx->control = NULL;
  539. }
  540. usb_set_intfdata(ctx->intf, NULL);
  541. cdc_ncm_free(ctx);
  542. }
  543. static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
  544. {
  545. if (first >= max)
  546. return;
  547. if (first >= end)
  548. return;
  549. if (end > max)
  550. end = max;
  551. memset(ptr + first, 0, end - first);
  552. }
  553. static struct sk_buff *
  554. cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
  555. {
  556. struct sk_buff *skb_out;
  557. u32 rem;
  558. u32 offset;
  559. u32 last_offset;
  560. u16 n = 0, index;
  561. u8 ready2send = 0;
  562. /* if there is a remaining skb, it gets priority */
  563. if (skb != NULL)
  564. swap(skb, ctx->tx_rem_skb);
  565. else
  566. ready2send = 1;
  567. /*
  568. * +----------------+
  569. * | skb_out |
  570. * +----------------+
  571. * ^ offset
  572. * ^ last_offset
  573. */
  574. /* check if we are resuming an OUT skb */
  575. if (ctx->tx_curr_skb != NULL) {
  576. /* pop variables */
  577. skb_out = ctx->tx_curr_skb;
  578. offset = ctx->tx_curr_offset;
  579. last_offset = ctx->tx_curr_last_offset;
  580. n = ctx->tx_curr_frame_num;
  581. } else {
  582. /* reset variables */
  583. skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
  584. if (skb_out == NULL) {
  585. if (skb != NULL) {
  586. dev_kfree_skb_any(skb);
  587. ctx->netdev->stats.tx_dropped++;
  588. }
  589. goto exit_no_skb;
  590. }
  591. /* make room for NTH and NDP */
  592. offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
  593. ctx->tx_ndp_modulus) +
  594. sizeof(struct usb_cdc_ncm_ndp16) +
  595. (ctx->tx_max_datagrams + 1) *
  596. sizeof(struct usb_cdc_ncm_dpe16);
  597. /* store last valid offset before alignment */
  598. last_offset = offset;
  599. /* align first Datagram offset correctly */
  600. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  601. /* zero buffer till the first IP datagram */
  602. cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
  603. n = 0;
  604. ctx->tx_curr_frame_num = 0;
  605. }
  606. for (; n < ctx->tx_max_datagrams; n++) {
  607. /* check if end of transmit buffer is reached */
  608. if (offset >= ctx->tx_max) {
  609. ready2send = 1;
  610. break;
  611. }
  612. /* compute maximum buffer size */
  613. rem = ctx->tx_max - offset;
  614. if (skb == NULL) {
  615. skb = ctx->tx_rem_skb;
  616. ctx->tx_rem_skb = NULL;
  617. /* check for end of skb */
  618. if (skb == NULL)
  619. break;
  620. }
  621. if (skb->len > rem) {
  622. if (n == 0) {
  623. /* won't fit, MTU problem? */
  624. dev_kfree_skb_any(skb);
  625. skb = NULL;
  626. ctx->netdev->stats.tx_dropped++;
  627. } else {
  628. /* no room for skb - store for later */
  629. if (ctx->tx_rem_skb != NULL) {
  630. dev_kfree_skb_any(ctx->tx_rem_skb);
  631. ctx->netdev->stats.tx_dropped++;
  632. }
  633. ctx->tx_rem_skb = skb;
  634. skb = NULL;
  635. ready2send = 1;
  636. }
  637. break;
  638. }
  639. memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
  640. ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
  641. ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
  642. /* update offset */
  643. offset += skb->len;
  644. /* store last valid offset before alignment */
  645. last_offset = offset;
  646. /* align offset correctly */
  647. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  648. /* zero padding */
  649. cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
  650. ctx->tx_max);
  651. dev_kfree_skb_any(skb);
  652. skb = NULL;
  653. }
  654. /* free up any dangling skb */
  655. if (skb != NULL) {
  656. dev_kfree_skb_any(skb);
  657. skb = NULL;
  658. ctx->netdev->stats.tx_dropped++;
  659. }
  660. ctx->tx_curr_frame_num = n;
  661. if (n == 0) {
  662. /* wait for more frames */
  663. /* push variables */
  664. ctx->tx_curr_skb = skb_out;
  665. ctx->tx_curr_offset = offset;
  666. ctx->tx_curr_last_offset = last_offset;
  667. goto exit_no_skb;
  668. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
  669. /* wait for more frames */
  670. /* push variables */
  671. ctx->tx_curr_skb = skb_out;
  672. ctx->tx_curr_offset = offset;
  673. ctx->tx_curr_last_offset = last_offset;
  674. /* set the pending count */
  675. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  676. ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
  677. goto exit_no_skb;
  678. } else {
  679. /* frame goes out */
  680. /* variables will be reset at next call */
  681. }
  682. /* check for overflow */
  683. if (last_offset > ctx->tx_max)
  684. last_offset = ctx->tx_max;
  685. /* revert offset */
  686. offset = last_offset;
  687. /*
  688. * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
  689. * we send buffers as it is. If we get more data, it would be more
  690. * efficient for USB HS mobile device with DMA engine to receive a full
  691. * size NTB, than canceling DMA transfer and receiving a short packet.
  692. */
  693. if (offset > CDC_NCM_MIN_TX_PKT)
  694. offset = ctx->tx_max;
  695. /* final zero padding */
  696. cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
  697. /* store last offset */
  698. last_offset = offset;
  699. if (((last_offset < ctx->tx_max) && ((last_offset %
  700. le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) ||
  701. (((last_offset == ctx->tx_max) && ((ctx->tx_max %
  702. le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) &&
  703. (ctx->tx_max < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)))) {
  704. /* force short packet */
  705. *(((u8 *)skb_out->data) + last_offset) = 0;
  706. last_offset++;
  707. }
  708. /* zero the rest of the DPEs plus the last NULL entry */
  709. for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
  710. ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
  711. ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
  712. }
  713. /* fill out 16-bit NTB header */
  714. ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  715. ctx->tx_ncm.nth16.wHeaderLength =
  716. cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
  717. ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
  718. ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
  719. index = ALIGN(sizeof(struct usb_cdc_ncm_nth16), ctx->tx_ndp_modulus);
  720. ctx->tx_ncm.nth16.wNdpIndex = cpu_to_le16(index);
  721. memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
  722. ctx->tx_seq++;
  723. /* fill out 16-bit NDP table */
  724. ctx->tx_ncm.ndp16.dwSignature =
  725. cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
  726. rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
  727. sizeof(struct usb_cdc_ncm_dpe16));
  728. ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
  729. ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
  730. memcpy(((u8 *)skb_out->data) + index,
  731. &(ctx->tx_ncm.ndp16),
  732. sizeof(ctx->tx_ncm.ndp16));
  733. memcpy(((u8 *)skb_out->data) + index + sizeof(ctx->tx_ncm.ndp16),
  734. &(ctx->tx_ncm.dpe16),
  735. (ctx->tx_curr_frame_num + 1) *
  736. sizeof(struct usb_cdc_ncm_dpe16));
  737. /* set frame length */
  738. skb_put(skb_out, last_offset);
  739. /* return skb */
  740. ctx->tx_curr_skb = NULL;
  741. ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
  742. return skb_out;
  743. exit_no_skb:
  744. /* Start timer, if there is a remaining skb */
  745. if (ctx->tx_curr_skb != NULL)
  746. cdc_ncm_tx_timeout_start(ctx);
  747. return NULL;
  748. }
  749. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  750. {
  751. /* start timer, if not already started */
  752. if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
  753. hrtimer_start(&ctx->tx_timer,
  754. ktime_set(0, CDC_NCM_TIMER_INTERVAL),
  755. HRTIMER_MODE_REL);
  756. }
  757. static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
  758. {
  759. struct cdc_ncm_ctx *ctx =
  760. container_of(timer, struct cdc_ncm_ctx, tx_timer);
  761. if (!atomic_read(&ctx->stop))
  762. tasklet_schedule(&ctx->bh);
  763. return HRTIMER_NORESTART;
  764. }
  765. static void cdc_ncm_txpath_bh(unsigned long param)
  766. {
  767. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
  768. spin_lock_bh(&ctx->mtx);
  769. if (ctx->tx_timer_pending != 0) {
  770. ctx->tx_timer_pending--;
  771. cdc_ncm_tx_timeout_start(ctx);
  772. spin_unlock_bh(&ctx->mtx);
  773. } else if (ctx->netdev != NULL) {
  774. spin_unlock_bh(&ctx->mtx);
  775. netif_tx_lock_bh(ctx->netdev);
  776. usbnet_start_xmit(NULL, ctx->netdev);
  777. netif_tx_unlock_bh(ctx->netdev);
  778. }
  779. }
  780. static struct sk_buff *
  781. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  782. {
  783. struct sk_buff *skb_out;
  784. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  785. /*
  786. * The Ethernet API we are using does not support transmitting
  787. * multiple Ethernet frames in a single call. This driver will
  788. * accumulate multiple Ethernet frames and send out a larger
  789. * USB frame when the USB buffer is full or when a single jiffies
  790. * timeout happens.
  791. */
  792. if (ctx == NULL)
  793. goto error;
  794. spin_lock_bh(&ctx->mtx);
  795. skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
  796. spin_unlock_bh(&ctx->mtx);
  797. return skb_out;
  798. error:
  799. if (skb != NULL)
  800. dev_kfree_skb_any(skb);
  801. return NULL;
  802. }
  803. static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  804. {
  805. struct sk_buff *skb;
  806. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  807. int len;
  808. int nframes;
  809. int x;
  810. int offset;
  811. struct usb_cdc_ncm_nth16 *nth16;
  812. struct usb_cdc_ncm_ndp16 *ndp16;
  813. struct usb_cdc_ncm_dpe16 *dpe16;
  814. if (ctx == NULL)
  815. goto error;
  816. if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
  817. sizeof(struct usb_cdc_ncm_ndp16))) {
  818. pr_debug("frame too short\n");
  819. goto error;
  820. }
  821. nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
  822. if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
  823. pr_debug("invalid NTH16 signature <%u>\n",
  824. le32_to_cpu(nth16->dwSignature));
  825. goto error;
  826. }
  827. len = le16_to_cpu(nth16->wBlockLength);
  828. if (len > ctx->rx_max) {
  829. pr_debug("unsupported NTB block length %u/%u\n", len,
  830. ctx->rx_max);
  831. goto error;
  832. }
  833. if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
  834. (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
  835. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
  836. pr_debug("sequence number glitch prev=%d curr=%d\n",
  837. ctx->rx_seq, le16_to_cpu(nth16->wSequence));
  838. }
  839. ctx->rx_seq = le16_to_cpu(nth16->wSequence);
  840. len = le16_to_cpu(nth16->wNdpIndex);
  841. if ((len + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
  842. pr_debug("invalid DPT16 index <%u>\n",
  843. le16_to_cpu(nth16->wNdpIndex));
  844. goto error;
  845. }
  846. ndp16 = (struct usb_cdc_ncm_ndp16 *)(((u8 *)skb_in->data) + len);
  847. if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
  848. pr_debug("invalid DPT16 signature <%u>\n",
  849. le32_to_cpu(ndp16->dwSignature));
  850. goto error;
  851. }
  852. if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
  853. pr_debug("invalid DPT16 length <%u>\n",
  854. le32_to_cpu(ndp16->dwSignature));
  855. goto error;
  856. }
  857. nframes = ((le16_to_cpu(ndp16->wLength) -
  858. sizeof(struct usb_cdc_ncm_ndp16)) /
  859. sizeof(struct usb_cdc_ncm_dpe16));
  860. nframes--; /* we process NDP entries except for the last one */
  861. len += sizeof(struct usb_cdc_ncm_ndp16);
  862. if ((len + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) >
  863. skb_in->len) {
  864. pr_debug("Invalid nframes = %d\n", nframes);
  865. goto error;
  866. }
  867. dpe16 = (struct usb_cdc_ncm_dpe16 *)(((u8 *)skb_in->data) + len);
  868. for (x = 0; x < nframes; x++, dpe16++) {
  869. offset = le16_to_cpu(dpe16->wDatagramIndex);
  870. len = le16_to_cpu(dpe16->wDatagramLength);
  871. /*
  872. * CDC NCM ch. 3.7
  873. * All entries after first NULL entry are to be ignored
  874. */
  875. if ((offset == 0) || (len == 0)) {
  876. if (!x)
  877. goto error; /* empty NTB */
  878. break;
  879. }
  880. /* sanity checking */
  881. if (((offset + len) > skb_in->len) ||
  882. (len > ctx->rx_max) || (len < ETH_HLEN)) {
  883. pr_debug("invalid frame detected (ignored)"
  884. "offset[%u]=%u, length=%u, skb=%p\n",
  885. x, offset, len, skb_in);
  886. if (!x)
  887. goto error;
  888. break;
  889. } else {
  890. skb = skb_clone(skb_in, GFP_ATOMIC);
  891. if (!skb)
  892. goto error;
  893. skb->len = len;
  894. skb->data = ((u8 *)skb_in->data) + offset;
  895. skb_set_tail_pointer(skb, len);
  896. usbnet_skb_return(dev, skb);
  897. }
  898. }
  899. return 1;
  900. error:
  901. return 0;
  902. }
  903. static void
  904. cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
  905. struct usb_cdc_speed_change *data)
  906. {
  907. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  908. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  909. /*
  910. * Currently the USB-NET API does not support reporting the actual
  911. * device speed. Do print it instead.
  912. */
  913. if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
  914. ctx->tx_speed = tx_speed;
  915. ctx->rx_speed = rx_speed;
  916. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  917. printk(KERN_INFO KBUILD_MODNAME
  918. ": %s: %u mbit/s downlink "
  919. "%u mbit/s uplink\n",
  920. ctx->netdev->name,
  921. (unsigned int)(rx_speed / 1000000U),
  922. (unsigned int)(tx_speed / 1000000U));
  923. } else {
  924. printk(KERN_INFO KBUILD_MODNAME
  925. ": %s: %u kbit/s downlink "
  926. "%u kbit/s uplink\n",
  927. ctx->netdev->name,
  928. (unsigned int)(rx_speed / 1000U),
  929. (unsigned int)(tx_speed / 1000U));
  930. }
  931. }
  932. }
  933. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  934. {
  935. struct cdc_ncm_ctx *ctx;
  936. struct usb_cdc_notification *event;
  937. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  938. if (urb->actual_length < sizeof(*event))
  939. return;
  940. /* test for split data in 8-byte chunks */
  941. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  942. cdc_ncm_speed_change(ctx,
  943. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  944. return;
  945. }
  946. event = urb->transfer_buffer;
  947. switch (event->bNotificationType) {
  948. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  949. /*
  950. * According to the CDC NCM specification ch.7.1
  951. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  952. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  953. */
  954. ctx->connected = event->wValue;
  955. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  956. " %sconnected\n",
  957. ctx->netdev->name, ctx->connected ? "" : "dis");
  958. if (ctx->connected)
  959. netif_carrier_on(dev->net);
  960. else {
  961. netif_carrier_off(dev->net);
  962. ctx->tx_speed = ctx->rx_speed = 0;
  963. }
  964. break;
  965. case USB_CDC_NOTIFY_SPEED_CHANGE:
  966. if (urb->actual_length < (sizeof(*event) +
  967. sizeof(struct usb_cdc_speed_change)))
  968. set_bit(EVENT_STS_SPLIT, &dev->flags);
  969. else
  970. cdc_ncm_speed_change(ctx,
  971. (struct usb_cdc_speed_change *) &event[1]);
  972. break;
  973. default:
  974. dev_err(&dev->udev->dev, "NCM: unexpected "
  975. "notification 0x%02x!\n", event->bNotificationType);
  976. break;
  977. }
  978. }
  979. static int cdc_ncm_check_connect(struct usbnet *dev)
  980. {
  981. struct cdc_ncm_ctx *ctx;
  982. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  983. if (ctx == NULL)
  984. return 1; /* disconnected */
  985. return !ctx->connected;
  986. }
  987. static int
  988. cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  989. {
  990. return usbnet_probe(udev, prod);
  991. }
  992. static void cdc_ncm_disconnect(struct usb_interface *intf)
  993. {
  994. struct usbnet *dev = usb_get_intfdata(intf);
  995. if (dev == NULL)
  996. return; /* already disconnected */
  997. usbnet_disconnect(intf);
  998. }
  999. static int cdc_ncm_manage_power(struct usbnet *dev, int status)
  1000. {
  1001. dev->intf->needs_remote_wakeup = status;
  1002. return 0;
  1003. }
  1004. static const struct driver_info cdc_ncm_info = {
  1005. .description = "CDC NCM",
  1006. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
  1007. .bind = cdc_ncm_bind,
  1008. .unbind = cdc_ncm_unbind,
  1009. .check_connect = cdc_ncm_check_connect,
  1010. .manage_power = cdc_ncm_manage_power,
  1011. .status = cdc_ncm_status,
  1012. .rx_fixup = cdc_ncm_rx_fixup,
  1013. .tx_fixup = cdc_ncm_tx_fixup,
  1014. };
  1015. /* Same as cdc_ncm_info, but with FLAG_WWAN */
  1016. static const struct driver_info wwan_info = {
  1017. .description = "Mobile Broadband Network Device",
  1018. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1019. | FLAG_WWAN,
  1020. .bind = cdc_ncm_bind,
  1021. .unbind = cdc_ncm_unbind,
  1022. .check_connect = cdc_ncm_check_connect,
  1023. .manage_power = cdc_ncm_manage_power,
  1024. .status = cdc_ncm_status,
  1025. .rx_fixup = cdc_ncm_rx_fixup,
  1026. .tx_fixup = cdc_ncm_tx_fixup,
  1027. };
  1028. static const struct usb_device_id cdc_devs[] = {
  1029. /* Ericsson MBM devices like F5521gw */
  1030. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1031. | USB_DEVICE_ID_MATCH_VENDOR,
  1032. .idVendor = 0x0bdb,
  1033. .bInterfaceClass = USB_CLASS_COMM,
  1034. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1035. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1036. .driver_info = (unsigned long) &wwan_info,
  1037. },
  1038. /* Dell branded MBM devices like DW5550 */
  1039. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1040. | USB_DEVICE_ID_MATCH_VENDOR,
  1041. .idVendor = 0x413c,
  1042. .bInterfaceClass = USB_CLASS_COMM,
  1043. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1044. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1045. .driver_info = (unsigned long) &wwan_info,
  1046. },
  1047. /* Toshiba branded MBM devices */
  1048. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1049. | USB_DEVICE_ID_MATCH_VENDOR,
  1050. .idVendor = 0x0930,
  1051. .bInterfaceClass = USB_CLASS_COMM,
  1052. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1053. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1054. .driver_info = (unsigned long) &wwan_info,
  1055. },
  1056. /* Huawei NCM devices disguised as vendor specific */
  1057. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
  1058. .driver_info = (unsigned long)&wwan_info,
  1059. },
  1060. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
  1061. .driver_info = (unsigned long)&wwan_info,
  1062. },
  1063. /* Generic CDC-NCM devices */
  1064. { USB_INTERFACE_INFO(USB_CLASS_COMM,
  1065. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1066. .driver_info = (unsigned long)&cdc_ncm_info,
  1067. },
  1068. {
  1069. },
  1070. };
  1071. MODULE_DEVICE_TABLE(usb, cdc_devs);
  1072. static struct usb_driver cdc_ncm_driver = {
  1073. .name = "cdc_ncm",
  1074. .id_table = cdc_devs,
  1075. .probe = cdc_ncm_probe,
  1076. .disconnect = cdc_ncm_disconnect,
  1077. .suspend = usbnet_suspend,
  1078. .resume = usbnet_resume,
  1079. .reset_resume = usbnet_resume,
  1080. .supports_autosuspend = 1,
  1081. .disable_hub_initiated_lpm = 1,
  1082. };
  1083. module_usb_driver(cdc_ncm_driver);
  1084. MODULE_AUTHOR("Hans Petter Selasky");
  1085. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1086. MODULE_LICENSE("Dual BSD/GPL");