cdc_ncm.c 33 KB

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