cdc_ncm.c 35 KB

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