cdc_ncm.c 35 KB

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