cdc_ncm.c 33 KB

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