cdc_ncm.c 33 KB

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