cdc_ncm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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_in);
  844. if (!x)
  845. goto error;
  846. break;
  847. } else {
  848. skb = skb_clone(skb_in, GFP_ATOMIC);
  849. if (!skb)
  850. goto error;
  851. skb->len = temp;
  852. skb->data = ((u8 *)skb_in->data) + offset;
  853. skb_set_tail_pointer(skb, temp);
  854. usbnet_skb_return(dev, skb);
  855. }
  856. }
  857. return 1;
  858. error:
  859. return 0;
  860. }
  861. static void
  862. cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
  863. struct connection_speed_change *data)
  864. {
  865. uint32_t rx_speed = le32_to_cpu(data->USBitRate);
  866. uint32_t tx_speed = le32_to_cpu(data->DSBitRate);
  867. /*
  868. * Currently the USB-NET API does not support reporting the actual
  869. * device speed. Do print it instead.
  870. */
  871. if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
  872. ctx->tx_speed = tx_speed;
  873. ctx->rx_speed = rx_speed;
  874. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  875. printk(KERN_INFO KBUILD_MODNAME
  876. ": %s: %u mbit/s downlink "
  877. "%u mbit/s uplink\n",
  878. ctx->netdev->name,
  879. (unsigned int)(rx_speed / 1000000U),
  880. (unsigned int)(tx_speed / 1000000U));
  881. } else {
  882. printk(KERN_INFO KBUILD_MODNAME
  883. ": %s: %u kbit/s downlink "
  884. "%u kbit/s uplink\n",
  885. ctx->netdev->name,
  886. (unsigned int)(rx_speed / 1000U),
  887. (unsigned int)(tx_speed / 1000U));
  888. }
  889. }
  890. }
  891. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  892. {
  893. struct cdc_ncm_ctx *ctx;
  894. struct usb_cdc_notification *event;
  895. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  896. if (urb->actual_length < sizeof(*event))
  897. return;
  898. /* test for split data in 8-byte chunks */
  899. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  900. cdc_ncm_speed_change(ctx,
  901. (struct connection_speed_change *)urb->transfer_buffer);
  902. return;
  903. }
  904. event = urb->transfer_buffer;
  905. switch (event->bNotificationType) {
  906. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  907. /*
  908. * According to the CDC NCM specification ch.7.1
  909. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  910. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  911. */
  912. ctx->connected = event->wValue;
  913. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  914. " %sconnected\n",
  915. ctx->netdev->name, ctx->connected ? "" : "dis");
  916. if (ctx->connected)
  917. netif_carrier_on(dev->net);
  918. else {
  919. netif_carrier_off(dev->net);
  920. ctx->tx_speed = ctx->rx_speed = 0;
  921. }
  922. break;
  923. case USB_CDC_NOTIFY_SPEED_CHANGE:
  924. if (urb->actual_length <
  925. (sizeof(*event) + sizeof(struct connection_speed_change)))
  926. set_bit(EVENT_STS_SPLIT, &dev->flags);
  927. else
  928. cdc_ncm_speed_change(ctx,
  929. (struct connection_speed_change *) &event[1]);
  930. break;
  931. default:
  932. dev_err(&dev->udev->dev, "NCM: unexpected "
  933. "notification 0x%02x!\n", event->bNotificationType);
  934. break;
  935. }
  936. }
  937. static int cdc_ncm_check_connect(struct usbnet *dev)
  938. {
  939. struct cdc_ncm_ctx *ctx;
  940. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  941. if (ctx == NULL)
  942. return 1; /* disconnected */
  943. return !ctx->connected;
  944. }
  945. static int
  946. cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  947. {
  948. return usbnet_probe(udev, prod);
  949. }
  950. static void cdc_ncm_disconnect(struct usb_interface *intf)
  951. {
  952. struct usbnet *dev = usb_get_intfdata(intf);
  953. if (dev == NULL)
  954. return; /* already disconnected */
  955. usbnet_disconnect(intf);
  956. }
  957. static int cdc_ncm_manage_power(struct usbnet *dev, int status)
  958. {
  959. dev->intf->needs_remote_wakeup = status;
  960. return 0;
  961. }
  962. static const struct driver_info cdc_ncm_info = {
  963. .description = "CDC NCM",
  964. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
  965. .bind = cdc_ncm_bind,
  966. .unbind = cdc_ncm_unbind,
  967. .check_connect = cdc_ncm_check_connect,
  968. .manage_power = cdc_ncm_manage_power,
  969. .status = cdc_ncm_status,
  970. .rx_fixup = cdc_ncm_rx_fixup,
  971. .tx_fixup = cdc_ncm_tx_fixup,
  972. };
  973. static struct usb_driver cdc_ncm_driver = {
  974. .name = "cdc_ncm",
  975. .id_table = cdc_devs,
  976. .probe = cdc_ncm_probe,
  977. .disconnect = cdc_ncm_disconnect,
  978. .suspend = usbnet_suspend,
  979. .resume = usbnet_resume,
  980. .supports_autosuspend = 1,
  981. };
  982. static struct ethtool_ops cdc_ncm_ethtool_ops = {
  983. .get_drvinfo = cdc_ncm_get_drvinfo,
  984. .get_link = usbnet_get_link,
  985. .get_msglevel = usbnet_get_msglevel,
  986. .set_msglevel = usbnet_set_msglevel,
  987. .get_settings = usbnet_get_settings,
  988. .set_settings = usbnet_set_settings,
  989. .nway_reset = usbnet_nway_reset,
  990. };
  991. static int __init cdc_ncm_init(void)
  992. {
  993. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
  994. return usb_register(&cdc_ncm_driver);
  995. }
  996. module_init(cdc_ncm_init);
  997. static void __exit cdc_ncm_exit(void)
  998. {
  999. usb_deregister(&cdc_ncm_driver);
  1000. }
  1001. module_exit(cdc_ncm_exit);
  1002. MODULE_AUTHOR("Hans Petter Selasky");
  1003. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1004. MODULE_LICENSE("Dual BSD/GPL");