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