cdc_ncm.c 35 KB

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