cdc_ncm.c 35 KB

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