cdc_ncm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. pr_debug("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. pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
  110. "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
  111. "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
  112. ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
  113. ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
  114. /* max count of tx datagrams */
  115. if ((ctx->tx_max_datagrams == 0) ||
  116. (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
  117. ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
  118. /* verify maximum size of received NTB in bytes */
  119. if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
  120. pr_debug("Using min receive length=%d\n",
  121. USB_CDC_NCM_NTB_MIN_IN_SIZE);
  122. ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
  123. }
  124. if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
  125. pr_debug("Using default maximum receive length=%d\n",
  126. CDC_NCM_NTB_MAX_SIZE_RX);
  127. ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
  128. }
  129. /* inform device about NTB input size changes */
  130. if (ctx->rx_max != le32_to_cpu(ncm_parm.dwNtbInMaxSize)) {
  131. __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
  132. err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
  133. USB_TYPE_CLASS | USB_DIR_OUT
  134. | USB_RECIP_INTERFACE,
  135. 0, iface_no, &dwNtbInMaxSize, 4);
  136. if (err < 0)
  137. pr_debug("Setting NTB Input Size failed\n");
  138. }
  139. /* verify maximum size of transmitted NTB in bytes */
  140. if ((ctx->tx_max <
  141. (min_hdr_size + min_dgram_size)) ||
  142. (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
  143. pr_debug("Using default maximum transmit length=%d\n",
  144. CDC_NCM_NTB_MAX_SIZE_TX);
  145. ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
  146. /* Adding a pad byte here simplifies the handling in
  147. * cdc_ncm_fill_tx_frame, by making tx_max always
  148. * represent the real skb max size.
  149. */
  150. if (ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
  151. ctx->tx_max++;
  152. }
  153. /*
  154. * verify that the structure alignment is:
  155. * - power of two
  156. * - not greater than the maximum transmit length
  157. * - not less than four bytes
  158. */
  159. val = ctx->tx_ndp_modulus;
  160. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  161. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  162. pr_debug("Using default alignment: 4 bytes\n");
  163. ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  164. }
  165. /*
  166. * verify that the payload alignment is:
  167. * - power of two
  168. * - not greater than the maximum transmit length
  169. * - not less than four bytes
  170. */
  171. val = ctx->tx_modulus;
  172. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  173. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  174. pr_debug("Using default transmit modulus: 4 bytes\n");
  175. ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  176. }
  177. /* verify the payload remainder */
  178. if (ctx->tx_remainder >= ctx->tx_modulus) {
  179. pr_debug("Using default transmit remainder: 0 bytes\n");
  180. ctx->tx_remainder = 0;
  181. }
  182. /* adjust TX-remainder according to NCM specification. */
  183. ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
  184. (ctx->tx_modulus - 1));
  185. /* additional configuration */
  186. /* set CRC Mode */
  187. if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
  188. err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
  189. USB_TYPE_CLASS | USB_DIR_OUT
  190. | USB_RECIP_INTERFACE,
  191. USB_CDC_NCM_CRC_NOT_APPENDED,
  192. iface_no, NULL, 0);
  193. if (err < 0)
  194. pr_debug("Setting CRC mode off failed\n");
  195. }
  196. /* set NTB format, if both formats are supported */
  197. if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
  198. err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
  199. USB_TYPE_CLASS | USB_DIR_OUT
  200. | USB_RECIP_INTERFACE,
  201. USB_CDC_NCM_NTB16_FORMAT,
  202. iface_no, NULL, 0);
  203. if (err < 0)
  204. pr_debug("Setting NTB format to 16-bit failed\n");
  205. }
  206. ctx->max_datagram_size = min_dgram_size;
  207. /* set Max Datagram Size (MTU) */
  208. if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
  209. __le16 max_datagram_size;
  210. u16 eth_max_sz;
  211. if (ctx->ether_desc != NULL)
  212. eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
  213. else if (ctx->mbim_desc != NULL)
  214. eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
  215. else
  216. goto max_dgram_err;
  217. err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
  218. USB_TYPE_CLASS | USB_DIR_IN
  219. | USB_RECIP_INTERFACE,
  220. 0, iface_no, &max_datagram_size, 2);
  221. if (err < 0) {
  222. pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
  223. min_dgram_size);
  224. } else {
  225. ctx->max_datagram_size =
  226. le16_to_cpu(max_datagram_size);
  227. /* Check Eth descriptor value */
  228. if (ctx->max_datagram_size > eth_max_sz)
  229. ctx->max_datagram_size = eth_max_sz;
  230. if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
  231. ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
  232. if (ctx->max_datagram_size < min_dgram_size)
  233. ctx->max_datagram_size = min_dgram_size;
  234. /* if value changed, update device */
  235. if (ctx->max_datagram_size !=
  236. le16_to_cpu(max_datagram_size)) {
  237. max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
  238. err = usbnet_write_cmd(dev,
  239. USB_CDC_SET_MAX_DATAGRAM_SIZE,
  240. USB_TYPE_CLASS | USB_DIR_OUT
  241. | USB_RECIP_INTERFACE,
  242. 0,
  243. iface_no, &max_datagram_size,
  244. 2);
  245. if (err < 0)
  246. pr_debug("SET_MAX_DGRAM_SIZE failed\n");
  247. }
  248. }
  249. }
  250. max_dgram_err:
  251. if (dev->net->mtu != (ctx->max_datagram_size - eth_hlen))
  252. dev->net->mtu = ctx->max_datagram_size - eth_hlen;
  253. return 0;
  254. }
  255. static void
  256. cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
  257. {
  258. struct usb_host_endpoint *e, *in = NULL, *out = NULL;
  259. u8 ep;
  260. for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
  261. e = intf->cur_altsetting->endpoint + ep;
  262. switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  263. case USB_ENDPOINT_XFER_INT:
  264. if (usb_endpoint_dir_in(&e->desc)) {
  265. if (!dev->status)
  266. dev->status = e;
  267. }
  268. break;
  269. case USB_ENDPOINT_XFER_BULK:
  270. if (usb_endpoint_dir_in(&e->desc)) {
  271. if (!in)
  272. in = e;
  273. } else {
  274. if (!out)
  275. out = e;
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. if (in && !dev->in)
  283. dev->in = usb_rcvbulkpipe(dev->udev,
  284. in->desc.bEndpointAddress &
  285. USB_ENDPOINT_NUMBER_MASK);
  286. if (out && !dev->out)
  287. dev->out = usb_sndbulkpipe(dev->udev,
  288. out->desc.bEndpointAddress &
  289. USB_ENDPOINT_NUMBER_MASK);
  290. }
  291. static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
  292. {
  293. if (ctx == NULL)
  294. return;
  295. if (ctx->tx_rem_skb != NULL) {
  296. dev_kfree_skb_any(ctx->tx_rem_skb);
  297. ctx->tx_rem_skb = NULL;
  298. }
  299. if (ctx->tx_curr_skb != NULL) {
  300. dev_kfree_skb_any(ctx->tx_curr_skb);
  301. ctx->tx_curr_skb = NULL;
  302. }
  303. kfree(ctx);
  304. }
  305. int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
  306. {
  307. const struct usb_cdc_union_desc *union_desc = NULL;
  308. struct cdc_ncm_ctx *ctx;
  309. struct usb_driver *driver;
  310. u8 *buf;
  311. int len;
  312. int temp;
  313. u8 iface_no;
  314. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  315. if (!ctx)
  316. return -ENOMEM;
  317. hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  318. ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
  319. ctx->bh.data = (unsigned long)dev;
  320. ctx->bh.func = cdc_ncm_txpath_bh;
  321. atomic_set(&ctx->stop, 0);
  322. spin_lock_init(&ctx->mtx);
  323. /* store ctx pointer in device data field */
  324. dev->data[0] = (unsigned long)ctx;
  325. /* only the control interface can be successfully probed */
  326. ctx->control = intf;
  327. /* get some pointers */
  328. driver = driver_of(intf);
  329. buf = intf->cur_altsetting->extra;
  330. len = intf->cur_altsetting->extralen;
  331. /* parse through descriptors associated with control interface */
  332. while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
  333. if (buf[1] != USB_DT_CS_INTERFACE)
  334. goto advance;
  335. switch (buf[2]) {
  336. case USB_CDC_UNION_TYPE:
  337. if (buf[0] < sizeof(*union_desc))
  338. break;
  339. union_desc = (const struct usb_cdc_union_desc *)buf;
  340. /* the master must be the interface we are probing */
  341. if (intf->cur_altsetting->desc.bInterfaceNumber !=
  342. union_desc->bMasterInterface0)
  343. goto error;
  344. ctx->data = usb_ifnum_to_if(dev->udev,
  345. union_desc->bSlaveInterface0);
  346. break;
  347. case USB_CDC_ETHERNET_TYPE:
  348. if (buf[0] < sizeof(*(ctx->ether_desc)))
  349. break;
  350. ctx->ether_desc =
  351. (const struct usb_cdc_ether_desc *)buf;
  352. break;
  353. case USB_CDC_NCM_TYPE:
  354. if (buf[0] < sizeof(*(ctx->func_desc)))
  355. break;
  356. ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
  357. break;
  358. case USB_CDC_MBIM_TYPE:
  359. if (buf[0] < sizeof(*(ctx->mbim_desc)))
  360. break;
  361. ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
  362. break;
  363. default:
  364. break;
  365. }
  366. advance:
  367. /* advance to next descriptor */
  368. temp = buf[0];
  369. buf += temp;
  370. len -= temp;
  371. }
  372. /* some buggy devices have an IAD but no CDC Union */
  373. if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
  374. ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
  375. dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
  376. }
  377. /* check if we got everything */
  378. if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc))
  379. goto error;
  380. /* claim data interface, if different from control */
  381. if (ctx->data != ctx->control) {
  382. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  383. if (temp)
  384. goto error;
  385. }
  386. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  387. /* reset data interface */
  388. temp = usb_set_interface(dev->udev, iface_no, 0);
  389. if (temp)
  390. goto error2;
  391. /* configure data interface */
  392. temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
  393. if (temp)
  394. goto error2;
  395. cdc_ncm_find_endpoints(dev, ctx->data);
  396. cdc_ncm_find_endpoints(dev, ctx->control);
  397. if (!dev->in || !dev->out || !dev->status)
  398. goto error2;
  399. /* initialize data interface */
  400. if (cdc_ncm_setup(dev))
  401. goto error2;
  402. usb_set_intfdata(ctx->data, dev);
  403. usb_set_intfdata(ctx->control, dev);
  404. if (ctx->ether_desc) {
  405. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  406. if (temp)
  407. goto error2;
  408. dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
  409. }
  410. /* usbnet use these values for sizing tx/rx queues */
  411. dev->hard_mtu = ctx->tx_max;
  412. dev->rx_urb_size = ctx->rx_max;
  413. return 0;
  414. error2:
  415. usb_set_intfdata(ctx->control, NULL);
  416. usb_set_intfdata(ctx->data, NULL);
  417. if (ctx->data != ctx->control)
  418. usb_driver_release_interface(driver, ctx->data);
  419. error:
  420. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  421. dev->data[0] = 0;
  422. dev_info(&dev->udev->dev, "bind() failure\n");
  423. return -ENODEV;
  424. }
  425. EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
  426. void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  427. {
  428. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  429. struct usb_driver *driver = driver_of(intf);
  430. if (ctx == NULL)
  431. return; /* no setup */
  432. atomic_set(&ctx->stop, 1);
  433. if (hrtimer_active(&ctx->tx_timer))
  434. hrtimer_cancel(&ctx->tx_timer);
  435. tasklet_kill(&ctx->bh);
  436. /* handle devices with combined control and data interface */
  437. if (ctx->control == ctx->data)
  438. ctx->data = NULL;
  439. /* disconnect master --> disconnect slave */
  440. if (intf == ctx->control && ctx->data) {
  441. usb_set_intfdata(ctx->data, NULL);
  442. usb_driver_release_interface(driver, ctx->data);
  443. ctx->data = NULL;
  444. } else if (intf == ctx->data && ctx->control) {
  445. usb_set_intfdata(ctx->control, NULL);
  446. usb_driver_release_interface(driver, ctx->control);
  447. ctx->control = NULL;
  448. }
  449. usb_set_intfdata(intf, NULL);
  450. cdc_ncm_free(ctx);
  451. }
  452. EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
  453. /* Select the MBIM altsetting iff it is preferred and available,
  454. * returning the number of the corresponding data interface altsetting
  455. */
  456. u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
  457. {
  458. struct usb_host_interface *alt;
  459. /* The MBIM spec defines a NCM compatible default altsetting,
  460. * which we may have matched:
  461. *
  462. * "Functions that implement both NCM 1.0 and MBIM (an
  463. * “NCM/MBIM function”) according to this recommendation
  464. * shall provide two alternate settings for the
  465. * Communication Interface. Alternate setting 0, and the
  466. * associated class and endpoint descriptors, shall be
  467. * constructed according to the rules given for the
  468. * Communication Interface in section 5 of [USBNCM10].
  469. * Alternate setting 1, and the associated class and
  470. * endpoint descriptors, shall be constructed according to
  471. * the rules given in section 6 (USB Device Model) of this
  472. * specification."
  473. */
  474. if (prefer_mbim && intf->num_altsetting == 2) {
  475. alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
  476. if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
  477. !usb_set_interface(dev->udev,
  478. intf->cur_altsetting->desc.bInterfaceNumber,
  479. CDC_NCM_COMM_ALTSETTING_MBIM))
  480. return CDC_NCM_DATA_ALTSETTING_MBIM;
  481. }
  482. return CDC_NCM_DATA_ALTSETTING_NCM;
  483. }
  484. EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
  485. static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
  486. {
  487. int ret;
  488. /* MBIM backwards compatible function? */
  489. cdc_ncm_select_altsetting(dev, intf);
  490. if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
  491. return -ENODEV;
  492. /* NCM data altsetting is always 1 */
  493. ret = cdc_ncm_bind_common(dev, intf, 1);
  494. /*
  495. * We should get an event when network connection is "connected" or
  496. * "disconnected". Set network connection in "disconnected" state
  497. * (carrier is OFF) during attach, so the IP network stack does not
  498. * start IPv6 negotiation and more.
  499. */
  500. usbnet_link_change(dev, 0, 0);
  501. return ret;
  502. }
  503. static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
  504. {
  505. size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
  506. if (skb->len + align > max)
  507. align = max - skb->len;
  508. if (align && skb_tailroom(skb) >= align)
  509. memset(skb_put(skb, align), 0, align);
  510. }
  511. /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
  512. * allocating a new one within skb
  513. */
  514. static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
  515. {
  516. struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
  517. struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
  518. size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
  519. /* follow the chain of NDPs, looking for a match */
  520. while (ndpoffset) {
  521. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
  522. if (ndp16->dwSignature == sign)
  523. return ndp16;
  524. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  525. }
  526. /* align new NDP */
  527. cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
  528. /* verify that there is room for the NDP and the datagram (reserve) */
  529. if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
  530. return NULL;
  531. /* link to it */
  532. if (ndp16)
  533. ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
  534. else
  535. nth16->wNdpIndex = cpu_to_le16(skb->len);
  536. /* push a new empty NDP */
  537. ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
  538. ndp16->dwSignature = sign;
  539. ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
  540. return ndp16;
  541. }
  542. struct sk_buff *
  543. cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
  544. {
  545. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  546. struct usb_cdc_ncm_nth16 *nth16;
  547. struct usb_cdc_ncm_ndp16 *ndp16;
  548. struct sk_buff *skb_out;
  549. u16 n = 0, index, ndplen;
  550. u8 ready2send = 0;
  551. /* if there is a remaining skb, it gets priority */
  552. if (skb != NULL) {
  553. swap(skb, ctx->tx_rem_skb);
  554. swap(sign, ctx->tx_rem_sign);
  555. } else {
  556. ready2send = 1;
  557. }
  558. /* check if we are resuming an OUT skb */
  559. skb_out = ctx->tx_curr_skb;
  560. /* allocate a new OUT skb */
  561. if (!skb_out) {
  562. skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
  563. if (skb_out == NULL) {
  564. if (skb != NULL) {
  565. dev_kfree_skb_any(skb);
  566. dev->net->stats.tx_dropped++;
  567. }
  568. goto exit_no_skb;
  569. }
  570. /* fill out the initial 16-bit NTB header */
  571. nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
  572. nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  573. nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
  574. nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
  575. /* count total number of frames in this NTB */
  576. ctx->tx_curr_frame_num = 0;
  577. }
  578. for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
  579. /* send any remaining skb first */
  580. if (skb == NULL) {
  581. skb = ctx->tx_rem_skb;
  582. sign = ctx->tx_rem_sign;
  583. ctx->tx_rem_skb = NULL;
  584. /* check for end of skb */
  585. if (skb == NULL)
  586. break;
  587. }
  588. /* get the appropriate NDP for this skb */
  589. ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
  590. /* align beginning of next frame */
  591. cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
  592. /* check if we had enough room left for both NDP and frame */
  593. if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
  594. if (n == 0) {
  595. /* won't fit, MTU problem? */
  596. dev_kfree_skb_any(skb);
  597. skb = NULL;
  598. dev->net->stats.tx_dropped++;
  599. } else {
  600. /* no room for skb - store for later */
  601. if (ctx->tx_rem_skb != NULL) {
  602. dev_kfree_skb_any(ctx->tx_rem_skb);
  603. dev->net->stats.tx_dropped++;
  604. }
  605. ctx->tx_rem_skb = skb;
  606. ctx->tx_rem_sign = sign;
  607. skb = NULL;
  608. ready2send = 1;
  609. }
  610. break;
  611. }
  612. /* calculate frame number withing this NDP */
  613. ndplen = le16_to_cpu(ndp16->wLength);
  614. index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
  615. /* OK, add this skb */
  616. ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
  617. ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
  618. ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
  619. memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
  620. dev_kfree_skb_any(skb);
  621. skb = NULL;
  622. /* send now if this NDP is full */
  623. if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
  624. ready2send = 1;
  625. break;
  626. }
  627. }
  628. /* free up any dangling skb */
  629. if (skb != NULL) {
  630. dev_kfree_skb_any(skb);
  631. skb = NULL;
  632. dev->net->stats.tx_dropped++;
  633. }
  634. ctx->tx_curr_frame_num = n;
  635. if (n == 0) {
  636. /* wait for more frames */
  637. /* push variables */
  638. ctx->tx_curr_skb = skb_out;
  639. goto exit_no_skb;
  640. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
  641. /* wait for more frames */
  642. /* push variables */
  643. ctx->tx_curr_skb = skb_out;
  644. /* set the pending count */
  645. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  646. ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
  647. goto exit_no_skb;
  648. } else {
  649. /* frame goes out */
  650. /* variables will be reset at next call */
  651. }
  652. /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
  653. * bytes, we send buffers as it is. If we get more data, it
  654. * would be more efficient for USB HS mobile device with DMA
  655. * engine to receive a full size NTB, than canceling DMA
  656. * transfer and receiving a short packet.
  657. *
  658. * This optimization support is pointless if we end up sending
  659. * a ZLP after full sized NTBs.
  660. */
  661. if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
  662. skb_out->len > CDC_NCM_MIN_TX_PKT)
  663. memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
  664. ctx->tx_max - skb_out->len);
  665. else if ((skb_out->len % dev->maxpacket) == 0)
  666. *skb_put(skb_out, 1) = 0; /* force short packet */
  667. /* set final frame length */
  668. nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  669. nth16->wBlockLength = cpu_to_le16(skb_out->len);
  670. /* return skb */
  671. ctx->tx_curr_skb = NULL;
  672. dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
  673. return skb_out;
  674. exit_no_skb:
  675. /* Start timer, if there is a remaining skb */
  676. if (ctx->tx_curr_skb != NULL)
  677. cdc_ncm_tx_timeout_start(ctx);
  678. return NULL;
  679. }
  680. EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
  681. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  682. {
  683. /* start timer, if not already started */
  684. if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
  685. hrtimer_start(&ctx->tx_timer,
  686. ktime_set(0, CDC_NCM_TIMER_INTERVAL),
  687. HRTIMER_MODE_REL);
  688. }
  689. static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
  690. {
  691. struct cdc_ncm_ctx *ctx =
  692. container_of(timer, struct cdc_ncm_ctx, tx_timer);
  693. if (!atomic_read(&ctx->stop))
  694. tasklet_schedule(&ctx->bh);
  695. return HRTIMER_NORESTART;
  696. }
  697. static void cdc_ncm_txpath_bh(unsigned long param)
  698. {
  699. struct usbnet *dev = (struct usbnet *)param;
  700. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  701. spin_lock_bh(&ctx->mtx);
  702. if (ctx->tx_timer_pending != 0) {
  703. ctx->tx_timer_pending--;
  704. cdc_ncm_tx_timeout_start(ctx);
  705. spin_unlock_bh(&ctx->mtx);
  706. } else if (dev->net != NULL) {
  707. spin_unlock_bh(&ctx->mtx);
  708. netif_tx_lock_bh(dev->net);
  709. usbnet_start_xmit(NULL, dev->net);
  710. netif_tx_unlock_bh(dev->net);
  711. } else {
  712. spin_unlock_bh(&ctx->mtx);
  713. }
  714. }
  715. static struct sk_buff *
  716. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  717. {
  718. struct sk_buff *skb_out;
  719. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  720. /*
  721. * The Ethernet API we are using does not support transmitting
  722. * multiple Ethernet frames in a single call. This driver will
  723. * accumulate multiple Ethernet frames and send out a larger
  724. * USB frame when the USB buffer is full or when a single jiffies
  725. * timeout happens.
  726. */
  727. if (ctx == NULL)
  728. goto error;
  729. spin_lock_bh(&ctx->mtx);
  730. skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
  731. spin_unlock_bh(&ctx->mtx);
  732. return skb_out;
  733. error:
  734. if (skb != NULL)
  735. dev_kfree_skb_any(skb);
  736. return NULL;
  737. }
  738. /* verify NTB header and return offset of first NDP, or negative error */
  739. int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
  740. {
  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. pr_debug("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. pr_debug("unsupported NTB block length %u/%u\n", len,
  760. ctx->rx_max);
  761. goto error;
  762. }
  763. if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
  764. (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
  765. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
  766. pr_debug("sequence number glitch prev=%d curr=%d\n",
  767. ctx->rx_seq, le16_to_cpu(nth16->wSequence));
  768. }
  769. ctx->rx_seq = le16_to_cpu(nth16->wSequence);
  770. ret = le16_to_cpu(nth16->wNdpIndex);
  771. error:
  772. return ret;
  773. }
  774. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
  775. /* verify NDP header and return number of datagrams, or negative error */
  776. int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
  777. {
  778. struct usb_cdc_ncm_ndp16 *ndp16;
  779. int ret = -EINVAL;
  780. if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
  781. pr_debug("invalid NDP offset <%u>\n", ndpoffset);
  782. goto error;
  783. }
  784. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  785. if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
  786. pr_debug("invalid DPT16 length <%u>\n",
  787. le32_to_cpu(ndp16->dwSignature));
  788. goto error;
  789. }
  790. ret = ((le16_to_cpu(ndp16->wLength) -
  791. sizeof(struct usb_cdc_ncm_ndp16)) /
  792. sizeof(struct usb_cdc_ncm_dpe16));
  793. ret--; /* we process NDP entries except for the last one */
  794. if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
  795. skb_in->len) {
  796. pr_debug("Invalid nframes = %d\n", ret);
  797. ret = -EINVAL;
  798. }
  799. error:
  800. return ret;
  801. }
  802. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
  803. static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  804. {
  805. struct sk_buff *skb;
  806. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  807. int len;
  808. int nframes;
  809. int x;
  810. int offset;
  811. struct usb_cdc_ncm_ndp16 *ndp16;
  812. struct usb_cdc_ncm_dpe16 *dpe16;
  813. int ndpoffset;
  814. int loopcount = 50; /* arbitrary max preventing infinite loop */
  815. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  816. if (ndpoffset < 0)
  817. goto error;
  818. next_ndp:
  819. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  820. if (nframes < 0)
  821. goto error;
  822. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  823. if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
  824. pr_debug("invalid DPT16 signature <%u>\n",
  825. le32_to_cpu(ndp16->dwSignature));
  826. goto err_ndp;
  827. }
  828. dpe16 = ndp16->dpe16;
  829. for (x = 0; x < nframes; x++, dpe16++) {
  830. offset = le16_to_cpu(dpe16->wDatagramIndex);
  831. len = le16_to_cpu(dpe16->wDatagramLength);
  832. /*
  833. * CDC NCM ch. 3.7
  834. * All entries after first NULL entry are to be ignored
  835. */
  836. if ((offset == 0) || (len == 0)) {
  837. if (!x)
  838. goto err_ndp; /* empty NTB */
  839. break;
  840. }
  841. /* sanity checking */
  842. if (((offset + len) > skb_in->len) ||
  843. (len > ctx->rx_max) || (len < ETH_HLEN)) {
  844. pr_debug("invalid frame detected (ignored)"
  845. "offset[%u]=%u, length=%u, skb=%p\n",
  846. x, offset, len, skb_in);
  847. if (!x)
  848. goto err_ndp;
  849. break;
  850. } else {
  851. skb = skb_clone(skb_in, GFP_ATOMIC);
  852. if (!skb)
  853. goto error;
  854. skb->len = len;
  855. skb->data = ((u8 *)skb_in->data) + offset;
  856. skb_set_tail_pointer(skb, len);
  857. usbnet_skb_return(dev, skb);
  858. }
  859. }
  860. err_ndp:
  861. /* are there more NDPs to process? */
  862. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  863. if (ndpoffset && loopcount--)
  864. goto next_ndp;
  865. return 1;
  866. error:
  867. return 0;
  868. }
  869. static void
  870. cdc_ncm_speed_change(struct usbnet *dev,
  871. struct usb_cdc_speed_change *data)
  872. {
  873. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  874. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  875. /*
  876. * Currently the USB-NET API does not support reporting the actual
  877. * device speed. Do print it instead.
  878. */
  879. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  880. printk(KERN_INFO KBUILD_MODNAME
  881. ": %s: %u mbit/s downlink "
  882. "%u mbit/s uplink\n",
  883. dev->net->name,
  884. (unsigned int)(rx_speed / 1000000U),
  885. (unsigned int)(tx_speed / 1000000U));
  886. } else {
  887. printk(KERN_INFO KBUILD_MODNAME
  888. ": %s: %u kbit/s downlink "
  889. "%u kbit/s uplink\n",
  890. dev->net->name,
  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. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  918. " %sconnected\n",
  919. dev->net->name, 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");