cdc_ncm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 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 = kmalloc(sizeof(*ctx), GFP_KERNEL);
  401. if (ctx == NULL)
  402. return -ENODEV;
  403. memset(ctx, 0, sizeof(*ctx));
  404. init_timer(&ctx->tx_timer);
  405. spin_lock_init(&ctx->mtx);
  406. ctx->netdev = dev->net;
  407. /* store ctx pointer in device data field */
  408. dev->data[0] = (unsigned long)ctx;
  409. /* get some pointers */
  410. driver = driver_of(intf);
  411. buf = intf->cur_altsetting->extra;
  412. len = intf->cur_altsetting->extralen;
  413. ctx->udev = dev->udev;
  414. ctx->intf = intf;
  415. /* parse through descriptors associated with control interface */
  416. while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
  417. if (buf[1] != USB_DT_CS_INTERFACE)
  418. goto advance;
  419. switch (buf[2]) {
  420. case USB_CDC_UNION_TYPE:
  421. if (buf[0] < sizeof(*(ctx->union_desc)))
  422. break;
  423. ctx->union_desc =
  424. (const struct usb_cdc_union_desc *)buf;
  425. ctx->control = usb_ifnum_to_if(dev->udev,
  426. ctx->union_desc->bMasterInterface0);
  427. ctx->data = usb_ifnum_to_if(dev->udev,
  428. ctx->union_desc->bSlaveInterface0);
  429. break;
  430. case USB_CDC_ETHERNET_TYPE:
  431. if (buf[0] < sizeof(*(ctx->ether_desc)))
  432. break;
  433. ctx->ether_desc =
  434. (const struct usb_cdc_ether_desc *)buf;
  435. dev->hard_mtu =
  436. le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
  437. if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
  438. dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
  439. else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
  440. dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
  441. break;
  442. case USB_CDC_NCM_TYPE:
  443. if (buf[0] < sizeof(*(ctx->func_desc)))
  444. break;
  445. ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
  446. break;
  447. default:
  448. break;
  449. }
  450. advance:
  451. /* advance to next descriptor */
  452. temp = buf[0];
  453. buf += temp;
  454. len -= temp;
  455. }
  456. /* check if we got everything */
  457. if ((ctx->control == NULL) || (ctx->data == NULL) ||
  458. (ctx->ether_desc == NULL) || (ctx->control != intf))
  459. goto error;
  460. /* claim interfaces, if any */
  461. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  462. if (temp)
  463. goto error;
  464. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  465. /* reset data interface */
  466. temp = usb_set_interface(dev->udev, iface_no, 0);
  467. if (temp)
  468. goto error2;
  469. /* initialize data interface */
  470. if (cdc_ncm_setup(ctx))
  471. goto error2;
  472. /* configure data interface */
  473. temp = usb_set_interface(dev->udev, iface_no, 1);
  474. if (temp)
  475. goto error2;
  476. cdc_ncm_find_endpoints(ctx, ctx->data);
  477. cdc_ncm_find_endpoints(ctx, ctx->control);
  478. if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
  479. (ctx->status_ep == NULL))
  480. goto error2;
  481. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  482. usb_set_intfdata(ctx->data, dev);
  483. usb_set_intfdata(ctx->control, dev);
  484. usb_set_intfdata(ctx->intf, dev);
  485. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  486. if (temp)
  487. goto error2;
  488. dev_info(&dev->udev->dev, "MAC-Address: "
  489. "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
  490. dev->net->dev_addr[0], dev->net->dev_addr[1],
  491. dev->net->dev_addr[2], dev->net->dev_addr[3],
  492. dev->net->dev_addr[4], dev->net->dev_addr[5]);
  493. dev->in = usb_rcvbulkpipe(dev->udev,
  494. ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  495. dev->out = usb_sndbulkpipe(dev->udev,
  496. ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  497. dev->status = ctx->status_ep;
  498. dev->rx_urb_size = ctx->rx_max;
  499. /*
  500. * We should get an event when network connection is "connected" or
  501. * "disconnected". Set network connection in "disconnected" state
  502. * (carrier is OFF) during attach, so the IP network stack does not
  503. * start IPv6 negotiation and more.
  504. */
  505. netif_carrier_off(dev->net);
  506. ctx->tx_speed = ctx->rx_speed = 0;
  507. return 0;
  508. error2:
  509. usb_set_intfdata(ctx->control, NULL);
  510. usb_set_intfdata(ctx->data, NULL);
  511. usb_driver_release_interface(driver, ctx->data);
  512. error:
  513. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  514. dev->data[0] = 0;
  515. dev_info(&dev->udev->dev, "bind() failure\n");
  516. return -ENODEV;
  517. }
  518. static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  519. {
  520. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  521. struct usb_driver *driver = driver_of(intf);
  522. if (ctx == NULL)
  523. return; /* no setup */
  524. /* disconnect master --> disconnect slave */
  525. if (intf == ctx->control && ctx->data) {
  526. usb_set_intfdata(ctx->data, NULL);
  527. usb_driver_release_interface(driver, ctx->data);
  528. ctx->data = NULL;
  529. } else if (intf == ctx->data && ctx->control) {
  530. usb_set_intfdata(ctx->control, NULL);
  531. usb_driver_release_interface(driver, ctx->control);
  532. ctx->control = NULL;
  533. }
  534. usb_set_intfdata(ctx->intf, NULL);
  535. cdc_ncm_free(ctx);
  536. }
  537. static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
  538. {
  539. if (first >= max)
  540. return;
  541. if (first >= end)
  542. return;
  543. if (end > max)
  544. end = max;
  545. memset(ptr + first, 0, end - first);
  546. }
  547. static struct sk_buff *
  548. cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
  549. {
  550. struct sk_buff *skb_out;
  551. u32 rem;
  552. u32 offset;
  553. u32 last_offset;
  554. u16 n = 0, index;
  555. u8 ready2send = 0;
  556. /* if there is a remaining skb, it gets priority */
  557. if (skb != NULL)
  558. swap(skb, ctx->tx_rem_skb);
  559. else
  560. ready2send = 1;
  561. /*
  562. * +----------------+
  563. * | skb_out |
  564. * +----------------+
  565. * ^ offset
  566. * ^ last_offset
  567. */
  568. /* check if we are resuming an OUT skb */
  569. if (ctx->tx_curr_skb != NULL) {
  570. /* pop variables */
  571. skb_out = ctx->tx_curr_skb;
  572. offset = ctx->tx_curr_offset;
  573. last_offset = ctx->tx_curr_last_offset;
  574. n = ctx->tx_curr_frame_num;
  575. } else {
  576. /* reset variables */
  577. skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
  578. if (skb_out == NULL) {
  579. if (skb != NULL) {
  580. dev_kfree_skb_any(skb);
  581. ctx->netdev->stats.tx_dropped++;
  582. }
  583. goto exit_no_skb;
  584. }
  585. /* make room for NTH and NDP */
  586. offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
  587. ctx->tx_ndp_modulus) +
  588. sizeof(struct usb_cdc_ncm_ndp16) +
  589. (ctx->tx_max_datagrams + 1) *
  590. sizeof(struct usb_cdc_ncm_dpe16);
  591. /* store last valid offset before alignment */
  592. last_offset = offset;
  593. /* align first Datagram offset correctly */
  594. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  595. /* zero buffer till the first IP datagram */
  596. cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
  597. n = 0;
  598. ctx->tx_curr_frame_num = 0;
  599. }
  600. for (; n < ctx->tx_max_datagrams; n++) {
  601. /* check if end of transmit buffer is reached */
  602. if (offset >= ctx->tx_max) {
  603. ready2send = 1;
  604. break;
  605. }
  606. /* compute maximum buffer size */
  607. rem = ctx->tx_max - offset;
  608. if (skb == NULL) {
  609. skb = ctx->tx_rem_skb;
  610. ctx->tx_rem_skb = NULL;
  611. /* check for end of skb */
  612. if (skb == NULL)
  613. break;
  614. }
  615. if (skb->len > rem) {
  616. if (n == 0) {
  617. /* won't fit, MTU problem? */
  618. dev_kfree_skb_any(skb);
  619. skb = NULL;
  620. ctx->netdev->stats.tx_dropped++;
  621. } else {
  622. /* no room for skb - store for later */
  623. if (ctx->tx_rem_skb != NULL) {
  624. dev_kfree_skb_any(ctx->tx_rem_skb);
  625. ctx->netdev->stats.tx_dropped++;
  626. }
  627. ctx->tx_rem_skb = skb;
  628. skb = NULL;
  629. ready2send = 1;
  630. }
  631. break;
  632. }
  633. memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
  634. ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
  635. ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
  636. /* update offset */
  637. offset += skb->len;
  638. /* store last valid offset before alignment */
  639. last_offset = offset;
  640. /* align offset correctly */
  641. offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
  642. /* zero padding */
  643. cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
  644. ctx->tx_max);
  645. dev_kfree_skb_any(skb);
  646. skb = NULL;
  647. }
  648. /* free up any dangling skb */
  649. if (skb != NULL) {
  650. dev_kfree_skb_any(skb);
  651. skb = NULL;
  652. ctx->netdev->stats.tx_dropped++;
  653. }
  654. ctx->tx_curr_frame_num = n;
  655. if (n == 0) {
  656. /* wait for more frames */
  657. /* push variables */
  658. ctx->tx_curr_skb = skb_out;
  659. ctx->tx_curr_offset = offset;
  660. ctx->tx_curr_last_offset = last_offset;
  661. goto exit_no_skb;
  662. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
  663. /* wait for more frames */
  664. /* push variables */
  665. ctx->tx_curr_skb = skb_out;
  666. ctx->tx_curr_offset = offset;
  667. ctx->tx_curr_last_offset = last_offset;
  668. /* set the pending count */
  669. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  670. ctx->tx_timer_pending = 2;
  671. goto exit_no_skb;
  672. } else {
  673. /* frame goes out */
  674. /* variables will be reset at next call */
  675. }
  676. /* check for overflow */
  677. if (last_offset > ctx->tx_max)
  678. last_offset = ctx->tx_max;
  679. /* revert offset */
  680. offset = last_offset;
  681. /*
  682. * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
  683. * we send buffers as it is. If we get more data, it would be more
  684. * efficient for USB HS mobile device with DMA engine to receive a full
  685. * size NTB, than canceling DMA transfer and receiving a short packet.
  686. */
  687. if (offset > CDC_NCM_MIN_TX_PKT)
  688. offset = ctx->tx_max;
  689. /* final zero padding */
  690. cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
  691. /* store last offset */
  692. last_offset = offset;
  693. if (((last_offset < ctx->tx_max) && ((last_offset %
  694. le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) ||
  695. (((last_offset == ctx->tx_max) && ((ctx->tx_max %
  696. le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) &&
  697. (ctx->tx_max < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)))) {
  698. /* force short packet */
  699. *(((u8 *)skb_out->data) + last_offset) = 0;
  700. last_offset++;
  701. }
  702. /* zero the rest of the DPEs plus the last NULL entry */
  703. for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
  704. ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
  705. ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
  706. }
  707. /* fill out 16-bit NTB header */
  708. ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  709. ctx->tx_ncm.nth16.wHeaderLength =
  710. cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
  711. ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
  712. ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
  713. index = ALIGN(sizeof(struct usb_cdc_ncm_nth16), ctx->tx_ndp_modulus);
  714. ctx->tx_ncm.nth16.wNdpIndex = cpu_to_le16(index);
  715. memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
  716. ctx->tx_seq++;
  717. /* fill out 16-bit NDP table */
  718. ctx->tx_ncm.ndp16.dwSignature =
  719. cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
  720. rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
  721. sizeof(struct usb_cdc_ncm_dpe16));
  722. ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
  723. ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
  724. memcpy(((u8 *)skb_out->data) + index,
  725. &(ctx->tx_ncm.ndp16),
  726. sizeof(ctx->tx_ncm.ndp16));
  727. memcpy(((u8 *)skb_out->data) + index + sizeof(ctx->tx_ncm.ndp16),
  728. &(ctx->tx_ncm.dpe16),
  729. (ctx->tx_curr_frame_num + 1) *
  730. sizeof(struct usb_cdc_ncm_dpe16));
  731. /* set frame length */
  732. skb_put(skb_out, last_offset);
  733. /* return skb */
  734. ctx->tx_curr_skb = NULL;
  735. return skb_out;
  736. exit_no_skb:
  737. return NULL;
  738. }
  739. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  740. {
  741. /* start timer, if not already started */
  742. if (timer_pending(&ctx->tx_timer) == 0) {
  743. ctx->tx_timer.function = &cdc_ncm_tx_timeout;
  744. ctx->tx_timer.data = (unsigned long)ctx;
  745. ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
  746. add_timer(&ctx->tx_timer);
  747. }
  748. }
  749. static void cdc_ncm_tx_timeout(unsigned long arg)
  750. {
  751. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
  752. u8 restart;
  753. spin_lock(&ctx->mtx);
  754. if (ctx->tx_timer_pending != 0) {
  755. ctx->tx_timer_pending--;
  756. restart = 1;
  757. } else {
  758. restart = 0;
  759. }
  760. spin_unlock(&ctx->mtx);
  761. if (restart) {
  762. spin_lock(&ctx->mtx);
  763. cdc_ncm_tx_timeout_start(ctx);
  764. spin_unlock(&ctx->mtx);
  765. } else if (ctx->netdev != NULL) {
  766. usbnet_start_xmit(NULL, ctx->netdev);
  767. }
  768. }
  769. static struct sk_buff *
  770. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  771. {
  772. struct sk_buff *skb_out;
  773. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  774. u8 need_timer = 0;
  775. /*
  776. * The Ethernet API we are using does not support transmitting
  777. * multiple Ethernet frames in a single call. This driver will
  778. * accumulate multiple Ethernet frames and send out a larger
  779. * USB frame when the USB buffer is full or when a single jiffies
  780. * timeout happens.
  781. */
  782. if (ctx == NULL)
  783. goto error;
  784. spin_lock(&ctx->mtx);
  785. skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
  786. if (ctx->tx_curr_skb != NULL)
  787. need_timer = 1;
  788. /* Start timer, if there is a remaining skb */
  789. if (need_timer)
  790. cdc_ncm_tx_timeout_start(ctx);
  791. if (skb_out)
  792. dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
  793. spin_unlock(&ctx->mtx);
  794. return skb_out;
  795. error:
  796. if (skb != NULL)
  797. dev_kfree_skb_any(skb);
  798. return NULL;
  799. }
  800. static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  801. {
  802. struct sk_buff *skb;
  803. struct cdc_ncm_ctx *ctx;
  804. int sumlen;
  805. int actlen;
  806. int temp;
  807. int nframes;
  808. int x;
  809. int offset;
  810. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  811. if (ctx == NULL)
  812. goto error;
  813. actlen = skb_in->len;
  814. sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
  815. if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
  816. pr_debug("frame too short\n");
  817. goto error;
  818. }
  819. memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
  820. sizeof(ctx->rx_ncm.nth16));
  821. if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
  822. USB_CDC_NCM_NTH16_SIGN) {
  823. pr_debug("invalid NTH16 signature <%u>\n",
  824. le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
  825. goto error;
  826. }
  827. temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
  828. if (temp > sumlen) {
  829. pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
  830. goto error;
  831. }
  832. temp = le16_to_cpu(ctx->rx_ncm.nth16.wNdpIndex);
  833. if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
  834. pr_debug("invalid DPT16 index\n");
  835. goto error;
  836. }
  837. memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
  838. sizeof(ctx->rx_ncm.ndp16));
  839. if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
  840. USB_CDC_NCM_NDP16_NOCRC_SIGN) {
  841. pr_debug("invalid DPT16 signature <%u>\n",
  842. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  843. goto error;
  844. }
  845. if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
  846. USB_CDC_NCM_NDP16_LENGTH_MIN) {
  847. pr_debug("invalid DPT16 length <%u>\n",
  848. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  849. goto error;
  850. }
  851. nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
  852. sizeof(struct usb_cdc_ncm_ndp16)) /
  853. sizeof(struct usb_cdc_ncm_dpe16));
  854. nframes--; /* we process NDP entries except for the last one */
  855. pr_debug("nframes = %u\n", nframes);
  856. temp += sizeof(ctx->rx_ncm.ndp16);
  857. if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
  858. pr_debug("Invalid nframes = %d\n", nframes);
  859. goto error;
  860. }
  861. if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
  862. pr_debug("Truncating number of frames from %u to %u\n",
  863. nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
  864. nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
  865. }
  866. memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
  867. nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
  868. for (x = 0; x < nframes; x++) {
  869. offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
  870. temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
  871. /*
  872. * CDC NCM ch. 3.7
  873. * All entries after first NULL entry are to be ignored
  874. */
  875. if ((offset == 0) || (temp == 0)) {
  876. if (!x)
  877. goto error; /* empty NTB */
  878. break;
  879. }
  880. /* sanity checking */
  881. if (((offset + temp) > actlen) ||
  882. (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
  883. pr_debug("invalid frame detected (ignored)"
  884. "offset[%u]=%u, length=%u, skb=%p\n",
  885. x, offset, temp, skb_in);
  886. if (!x)
  887. goto error;
  888. break;
  889. } else {
  890. skb = skb_clone(skb_in, GFP_ATOMIC);
  891. if (!skb)
  892. goto error;
  893. skb->len = temp;
  894. skb->data = ((u8 *)skb_in->data) + offset;
  895. skb_set_tail_pointer(skb, temp);
  896. usbnet_skb_return(dev, skb);
  897. }
  898. }
  899. return 1;
  900. error:
  901. return 0;
  902. }
  903. static void
  904. cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
  905. struct usb_cdc_speed_change *data)
  906. {
  907. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  908. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  909. /*
  910. * Currently the USB-NET API does not support reporting the actual
  911. * device speed. Do print it instead.
  912. */
  913. if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
  914. ctx->tx_speed = tx_speed;
  915. ctx->rx_speed = rx_speed;
  916. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  917. printk(KERN_INFO KBUILD_MODNAME
  918. ": %s: %u mbit/s downlink "
  919. "%u mbit/s uplink\n",
  920. ctx->netdev->name,
  921. (unsigned int)(rx_speed / 1000000U),
  922. (unsigned int)(tx_speed / 1000000U));
  923. } else {
  924. printk(KERN_INFO KBUILD_MODNAME
  925. ": %s: %u kbit/s downlink "
  926. "%u kbit/s uplink\n",
  927. ctx->netdev->name,
  928. (unsigned int)(rx_speed / 1000U),
  929. (unsigned int)(tx_speed / 1000U));
  930. }
  931. }
  932. }
  933. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  934. {
  935. struct cdc_ncm_ctx *ctx;
  936. struct usb_cdc_notification *event;
  937. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  938. if (urb->actual_length < sizeof(*event))
  939. return;
  940. /* test for split data in 8-byte chunks */
  941. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  942. cdc_ncm_speed_change(ctx,
  943. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  944. return;
  945. }
  946. event = urb->transfer_buffer;
  947. switch (event->bNotificationType) {
  948. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  949. /*
  950. * According to the CDC NCM specification ch.7.1
  951. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  952. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  953. */
  954. ctx->connected = event->wValue;
  955. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  956. " %sconnected\n",
  957. ctx->netdev->name, ctx->connected ? "" : "dis");
  958. if (ctx->connected)
  959. netif_carrier_on(dev->net);
  960. else {
  961. netif_carrier_off(dev->net);
  962. ctx->tx_speed = ctx->rx_speed = 0;
  963. }
  964. break;
  965. case USB_CDC_NOTIFY_SPEED_CHANGE:
  966. if (urb->actual_length < (sizeof(*event) +
  967. sizeof(struct usb_cdc_speed_change)))
  968. set_bit(EVENT_STS_SPLIT, &dev->flags);
  969. else
  970. cdc_ncm_speed_change(ctx,
  971. (struct usb_cdc_speed_change *) &event[1]);
  972. break;
  973. default:
  974. dev_err(&dev->udev->dev, "NCM: unexpected "
  975. "notification 0x%02x!\n", event->bNotificationType);
  976. break;
  977. }
  978. }
  979. static int cdc_ncm_check_connect(struct usbnet *dev)
  980. {
  981. struct cdc_ncm_ctx *ctx;
  982. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  983. if (ctx == NULL)
  984. return 1; /* disconnected */
  985. return !ctx->connected;
  986. }
  987. static int
  988. cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  989. {
  990. return usbnet_probe(udev, prod);
  991. }
  992. static void cdc_ncm_disconnect(struct usb_interface *intf)
  993. {
  994. struct usbnet *dev = usb_get_intfdata(intf);
  995. if (dev == NULL)
  996. return; /* already disconnected */
  997. usbnet_disconnect(intf);
  998. }
  999. static int cdc_ncm_manage_power(struct usbnet *dev, int status)
  1000. {
  1001. dev->intf->needs_remote_wakeup = status;
  1002. return 0;
  1003. }
  1004. static const struct driver_info cdc_ncm_info = {
  1005. .description = "CDC NCM",
  1006. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
  1007. .bind = cdc_ncm_bind,
  1008. .unbind = cdc_ncm_unbind,
  1009. .check_connect = cdc_ncm_check_connect,
  1010. .manage_power = cdc_ncm_manage_power,
  1011. .status = cdc_ncm_status,
  1012. .rx_fixup = cdc_ncm_rx_fixup,
  1013. .tx_fixup = cdc_ncm_tx_fixup,
  1014. };
  1015. static struct usb_driver cdc_ncm_driver = {
  1016. .name = "cdc_ncm",
  1017. .id_table = cdc_devs,
  1018. .probe = cdc_ncm_probe,
  1019. .disconnect = cdc_ncm_disconnect,
  1020. .suspend = usbnet_suspend,
  1021. .resume = usbnet_resume,
  1022. .reset_resume = usbnet_resume,
  1023. .supports_autosuspend = 1,
  1024. };
  1025. static struct ethtool_ops cdc_ncm_ethtool_ops = {
  1026. .get_drvinfo = cdc_ncm_get_drvinfo,
  1027. .get_link = usbnet_get_link,
  1028. .get_msglevel = usbnet_get_msglevel,
  1029. .set_msglevel = usbnet_set_msglevel,
  1030. .get_settings = usbnet_get_settings,
  1031. .set_settings = usbnet_set_settings,
  1032. .nway_reset = usbnet_nway_reset,
  1033. };
  1034. static int __init cdc_ncm_init(void)
  1035. {
  1036. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
  1037. return usb_register(&cdc_ncm_driver);
  1038. }
  1039. module_init(cdc_ncm_init);
  1040. static void __exit cdc_ncm_exit(void)
  1041. {
  1042. usb_deregister(&cdc_ncm_driver);
  1043. }
  1044. module_exit(cdc_ncm_exit);
  1045. MODULE_AUTHOR("Hans Petter Selasky");
  1046. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1047. MODULE_LICENSE("Dual BSD/GPL");