cdc_ncm.c 36 KB

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