cdc_ncm.c 33 KB

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