cdc_ncm.c 31 KB

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