cdc_ncm.c 36 KB

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