cdc_ncm.c 32 KB

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