cdc_ncm.c 35 KB

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