cdc_ncm.c 36 KB

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