cdc_ncm.c 34 KB

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