cdc_ncm.c 33 KB

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