cdc_ncm.c 36 KB

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