cdc_ncm.c 32 KB

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