cdc_ncm.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 "17-Jan-2011"
  56. /* CDC NCM subclass 3.2.1 */
  57. #define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
  58. /* Maximum NTB length */
  59. #define CDC_NCM_NTB_MAX_SIZE_TX 16384 /* bytes */
  60. #define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */
  61. /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
  62. #define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
  63. #define CDC_NCM_MIN_TX_PKT 512 /* bytes */
  64. /* Default value for MaxDatagramSize */
  65. #define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */
  66. /*
  67. * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
  68. * the last NULL entry. Any additional datagrams in NTB would be discarded.
  69. */
  70. #define CDC_NCM_DPT_DATAGRAMS_MAX 32
  71. /* 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. }
  722. spin_unlock(&ctx->mtx);
  723. if (restart) {
  724. spin_lock(&ctx->mtx);
  725. cdc_ncm_tx_timeout_start(ctx);
  726. spin_unlock(&ctx->mtx);
  727. } else if (ctx->netdev != NULL) {
  728. usbnet_start_xmit(NULL, ctx->netdev);
  729. }
  730. }
  731. static struct sk_buff *
  732. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  733. {
  734. struct sk_buff *skb_out;
  735. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  736. u8 need_timer = 0;
  737. /*
  738. * The Ethernet API we are using does not support transmitting
  739. * multiple Ethernet frames in a single call. This driver will
  740. * accumulate multiple Ethernet frames and send out a larger
  741. * USB frame when the USB buffer is full or when a single jiffies
  742. * timeout happens.
  743. */
  744. if (ctx == NULL)
  745. goto error;
  746. spin_lock(&ctx->mtx);
  747. skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
  748. if (ctx->tx_curr_skb != NULL)
  749. need_timer = 1;
  750. /* Start timer, if there is a remaining skb */
  751. if (need_timer)
  752. cdc_ncm_tx_timeout_start(ctx);
  753. if (skb_out)
  754. dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
  755. spin_unlock(&ctx->mtx);
  756. return skb_out;
  757. error:
  758. if (skb != NULL)
  759. dev_kfree_skb_any(skb);
  760. return NULL;
  761. }
  762. static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  763. {
  764. struct sk_buff *skb;
  765. struct cdc_ncm_ctx *ctx;
  766. int sumlen;
  767. int actlen;
  768. int temp;
  769. int nframes;
  770. int x;
  771. int offset;
  772. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  773. if (ctx == NULL)
  774. goto error;
  775. actlen = skb_in->len;
  776. sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
  777. if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
  778. pr_debug("frame too short\n");
  779. goto error;
  780. }
  781. memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
  782. sizeof(ctx->rx_ncm.nth16));
  783. if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
  784. USB_CDC_NCM_NTH16_SIGN) {
  785. pr_debug("invalid NTH16 signature <%u>\n",
  786. le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
  787. goto error;
  788. }
  789. temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
  790. if (temp > sumlen) {
  791. pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
  792. goto error;
  793. }
  794. temp = le16_to_cpu(ctx->rx_ncm.nth16.wFpIndex);
  795. if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
  796. pr_debug("invalid DPT16 index\n");
  797. goto error;
  798. }
  799. memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
  800. sizeof(ctx->rx_ncm.ndp16));
  801. if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
  802. USB_CDC_NCM_NDP16_NOCRC_SIGN) {
  803. pr_debug("invalid DPT16 signature <%u>\n",
  804. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  805. goto error;
  806. }
  807. if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
  808. USB_CDC_NCM_NDP16_LENGTH_MIN) {
  809. pr_debug("invalid DPT16 length <%u>\n",
  810. le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
  811. goto error;
  812. }
  813. nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
  814. sizeof(struct usb_cdc_ncm_ndp16)) /
  815. sizeof(struct usb_cdc_ncm_dpe16));
  816. nframes--; /* we process NDP entries except for the last one */
  817. pr_debug("nframes = %u\n", nframes);
  818. temp += sizeof(ctx->rx_ncm.ndp16);
  819. if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
  820. pr_debug("Invalid nframes = %d\n", nframes);
  821. goto error;
  822. }
  823. if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
  824. pr_debug("Truncating number of frames from %u to %u\n",
  825. nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
  826. nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
  827. }
  828. memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
  829. nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
  830. for (x = 0; x < nframes; x++) {
  831. offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
  832. temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
  833. /*
  834. * CDC NCM ch. 3.7
  835. * All entries after first NULL entry are to be ignored
  836. */
  837. if ((offset == 0) || (temp == 0)) {
  838. if (!x)
  839. goto error; /* empty NTB */
  840. break;
  841. }
  842. /* sanity checking */
  843. if (((offset + temp) > actlen) ||
  844. (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
  845. pr_debug("invalid frame detected (ignored)"
  846. "offset[%u]=%u, length=%u, skb=%p\n",
  847. x, offset, temp, skb_in);
  848. if (!x)
  849. goto error;
  850. break;
  851. } else {
  852. skb = skb_clone(skb_in, GFP_ATOMIC);
  853. if (!skb)
  854. goto error;
  855. skb->len = temp;
  856. skb->data = ((u8 *)skb_in->data) + offset;
  857. skb_set_tail_pointer(skb, temp);
  858. usbnet_skb_return(dev, skb);
  859. }
  860. }
  861. return 1;
  862. error:
  863. return 0;
  864. }
  865. static void
  866. cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
  867. struct connection_speed_change *data)
  868. {
  869. uint32_t rx_speed = le32_to_cpu(data->USBitRate);
  870. uint32_t tx_speed = le32_to_cpu(data->DSBitRate);
  871. /*
  872. * Currently the USB-NET API does not support reporting the actual
  873. * device speed. Do print it instead.
  874. */
  875. if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
  876. ctx->tx_speed = tx_speed;
  877. ctx->rx_speed = rx_speed;
  878. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  879. printk(KERN_INFO KBUILD_MODNAME
  880. ": %s: %u mbit/s downlink "
  881. "%u mbit/s uplink\n",
  882. ctx->netdev->name,
  883. (unsigned int)(rx_speed / 1000000U),
  884. (unsigned int)(tx_speed / 1000000U));
  885. } else {
  886. printk(KERN_INFO KBUILD_MODNAME
  887. ": %s: %u kbit/s downlink "
  888. "%u kbit/s uplink\n",
  889. ctx->netdev->name,
  890. (unsigned int)(rx_speed / 1000U),
  891. (unsigned int)(tx_speed / 1000U));
  892. }
  893. }
  894. }
  895. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  896. {
  897. struct cdc_ncm_ctx *ctx;
  898. struct usb_cdc_notification *event;
  899. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  900. if (urb->actual_length < sizeof(*event))
  901. return;
  902. /* test for split data in 8-byte chunks */
  903. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  904. cdc_ncm_speed_change(ctx,
  905. (struct connection_speed_change *)urb->transfer_buffer);
  906. return;
  907. }
  908. event = urb->transfer_buffer;
  909. switch (event->bNotificationType) {
  910. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  911. /*
  912. * According to the CDC NCM specification ch.7.1
  913. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  914. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  915. */
  916. ctx->connected = event->wValue;
  917. printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
  918. " %sconnected\n",
  919. ctx->netdev->name, ctx->connected ? "" : "dis");
  920. if (ctx->connected)
  921. netif_carrier_on(dev->net);
  922. else {
  923. netif_carrier_off(dev->net);
  924. ctx->tx_speed = ctx->rx_speed = 0;
  925. }
  926. break;
  927. case USB_CDC_NOTIFY_SPEED_CHANGE:
  928. if (urb->actual_length <
  929. (sizeof(*event) + sizeof(struct connection_speed_change)))
  930. set_bit(EVENT_STS_SPLIT, &dev->flags);
  931. else
  932. cdc_ncm_speed_change(ctx,
  933. (struct connection_speed_change *) &event[1]);
  934. break;
  935. default:
  936. dev_err(&dev->udev->dev, "NCM: unexpected "
  937. "notification 0x%02x!\n", event->bNotificationType);
  938. break;
  939. }
  940. }
  941. static int cdc_ncm_check_connect(struct usbnet *dev)
  942. {
  943. struct cdc_ncm_ctx *ctx;
  944. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  945. if (ctx == NULL)
  946. return 1; /* disconnected */
  947. return !ctx->connected;
  948. }
  949. static int
  950. cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  951. {
  952. return usbnet_probe(udev, prod);
  953. }
  954. static void cdc_ncm_disconnect(struct usb_interface *intf)
  955. {
  956. struct usbnet *dev = usb_get_intfdata(intf);
  957. if (dev == NULL)
  958. return; /* already disconnected */
  959. usbnet_disconnect(intf);
  960. }
  961. static int cdc_ncm_manage_power(struct usbnet *dev, int status)
  962. {
  963. dev->intf->needs_remote_wakeup = status;
  964. return 0;
  965. }
  966. static const struct driver_info cdc_ncm_info = {
  967. .description = "CDC NCM",
  968. .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
  969. .bind = cdc_ncm_bind,
  970. .unbind = cdc_ncm_unbind,
  971. .check_connect = cdc_ncm_check_connect,
  972. .manage_power = cdc_ncm_manage_power,
  973. .status = cdc_ncm_status,
  974. .rx_fixup = cdc_ncm_rx_fixup,
  975. .tx_fixup = cdc_ncm_tx_fixup,
  976. };
  977. static struct usb_driver cdc_ncm_driver = {
  978. .name = "cdc_ncm",
  979. .id_table = cdc_devs,
  980. .probe = cdc_ncm_probe,
  981. .disconnect = cdc_ncm_disconnect,
  982. .suspend = usbnet_suspend,
  983. .resume = usbnet_resume,
  984. .supports_autosuspend = 1,
  985. };
  986. static struct ethtool_ops cdc_ncm_ethtool_ops = {
  987. .get_drvinfo = cdc_ncm_get_drvinfo,
  988. .get_link = usbnet_get_link,
  989. .get_msglevel = usbnet_get_msglevel,
  990. .set_msglevel = usbnet_set_msglevel,
  991. .get_settings = usbnet_get_settings,
  992. .set_settings = usbnet_set_settings,
  993. .nway_reset = usbnet_nway_reset,
  994. };
  995. static int __init cdc_ncm_init(void)
  996. {
  997. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
  998. return usb_register(&cdc_ncm_driver);
  999. }
  1000. module_init(cdc_ncm_init);
  1001. static void __exit cdc_ncm_exit(void)
  1002. {
  1003. usb_deregister(&cdc_ncm_driver);
  1004. }
  1005. module_exit(cdc_ncm_exit);
  1006. MODULE_AUTHOR("Hans Petter Selasky");
  1007. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1008. MODULE_LICENSE("Dual BSD/GPL");