cdc_ncm.c 36 KB

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