cdc_ncm.c 35 KB

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