cdc_ncm.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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/docs/devclass_docs/NCM10_012011.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/netdevice.h>
  42. #include <linux/ctype.h>
  43. #include <linux/etherdevice.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. struct cdc_ncm_stats {
  66. char stat_string[ETH_GSTRING_LEN];
  67. int sizeof_stat;
  68. int stat_offset;
  69. };
  70. #define CDC_NCM_STAT(str, m) { \
  71. .stat_string = str, \
  72. .sizeof_stat = sizeof(((struct cdc_ncm_ctx *)0)->m), \
  73. .stat_offset = offsetof(struct cdc_ncm_ctx, m) }
  74. #define CDC_NCM_SIMPLE_STAT(m) CDC_NCM_STAT(__stringify(m), m)
  75. static const struct cdc_ncm_stats cdc_ncm_gstrings_stats[] = {
  76. CDC_NCM_SIMPLE_STAT(tx_reason_ntb_full),
  77. CDC_NCM_SIMPLE_STAT(tx_reason_ndp_full),
  78. CDC_NCM_SIMPLE_STAT(tx_reason_timeout),
  79. CDC_NCM_SIMPLE_STAT(tx_reason_max_datagram),
  80. CDC_NCM_SIMPLE_STAT(tx_overhead),
  81. CDC_NCM_SIMPLE_STAT(tx_ntbs),
  82. CDC_NCM_SIMPLE_STAT(rx_overhead),
  83. CDC_NCM_SIMPLE_STAT(rx_ntbs),
  84. };
  85. static int cdc_ncm_get_sset_count(struct net_device __always_unused *netdev, int sset)
  86. {
  87. switch (sset) {
  88. case ETH_SS_STATS:
  89. return ARRAY_SIZE(cdc_ncm_gstrings_stats);
  90. default:
  91. return -EOPNOTSUPP;
  92. }
  93. }
  94. static void cdc_ncm_get_ethtool_stats(struct net_device *netdev,
  95. struct ethtool_stats __always_unused *stats,
  96. u64 *data)
  97. {
  98. struct usbnet *dev = netdev_priv(netdev);
  99. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  100. int i;
  101. char *p = NULL;
  102. for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
  103. p = (char *)ctx + cdc_ncm_gstrings_stats[i].stat_offset;
  104. data[i] = (cdc_ncm_gstrings_stats[i].sizeof_stat == sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  105. }
  106. }
  107. static void cdc_ncm_get_strings(struct net_device __always_unused *netdev, u32 stringset, u8 *data)
  108. {
  109. u8 *p = data;
  110. int i;
  111. switch (stringset) {
  112. case ETH_SS_STATS:
  113. for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
  114. memcpy(p, cdc_ncm_gstrings_stats[i].stat_string, ETH_GSTRING_LEN);
  115. p += ETH_GSTRING_LEN;
  116. }
  117. }
  118. }
  119. static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx);
  120. static const struct ethtool_ops cdc_ncm_ethtool_ops = {
  121. .get_settings = usbnet_get_settings,
  122. .set_settings = usbnet_set_settings,
  123. .get_link = usbnet_get_link,
  124. .nway_reset = usbnet_nway_reset,
  125. .get_drvinfo = usbnet_get_drvinfo,
  126. .get_msglevel = usbnet_get_msglevel,
  127. .set_msglevel = usbnet_set_msglevel,
  128. .get_ts_info = ethtool_op_get_ts_info,
  129. .get_sset_count = cdc_ncm_get_sset_count,
  130. .get_strings = cdc_ncm_get_strings,
  131. .get_ethtool_stats = cdc_ncm_get_ethtool_stats,
  132. };
  133. static u32 cdc_ncm_check_rx_max(struct usbnet *dev, u32 new_rx)
  134. {
  135. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  136. u32 val, max, min;
  137. /* clamp new_rx to sane values */
  138. min = USB_CDC_NCM_NTB_MIN_IN_SIZE;
  139. max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
  140. /* dwNtbInMaxSize spec violation? Use MIN size for both limits */
  141. if (max < min) {
  142. dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n",
  143. le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min);
  144. max = min;
  145. }
  146. val = clamp_t(u32, new_rx, min, max);
  147. if (val != new_rx)
  148. dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range\n", min, max);
  149. return val;
  150. }
  151. static u32 cdc_ncm_check_tx_max(struct usbnet *dev, u32 new_tx)
  152. {
  153. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  154. u32 val, max, min;
  155. /* clamp new_tx to sane values */
  156. min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth16);
  157. max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_TX, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
  158. /* some devices set dwNtbOutMaxSize too low for the above default */
  159. min = min(min, max);
  160. val = clamp_t(u32, new_tx, min, max);
  161. if (val != new_tx)
  162. dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range\n", min, max);
  163. return val;
  164. }
  165. static ssize_t cdc_ncm_show_min_tx_pkt(struct device *d, struct device_attribute *attr, char *buf)
  166. {
  167. struct usbnet *dev = netdev_priv(to_net_dev(d));
  168. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  169. return sprintf(buf, "%u\n", ctx->min_tx_pkt);
  170. }
  171. static ssize_t cdc_ncm_show_rx_max(struct device *d, struct device_attribute *attr, char *buf)
  172. {
  173. struct usbnet *dev = netdev_priv(to_net_dev(d));
  174. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  175. return sprintf(buf, "%u\n", ctx->rx_max);
  176. }
  177. static ssize_t cdc_ncm_show_tx_max(struct device *d, struct device_attribute *attr, char *buf)
  178. {
  179. struct usbnet *dev = netdev_priv(to_net_dev(d));
  180. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  181. return sprintf(buf, "%u\n", ctx->tx_max);
  182. }
  183. static ssize_t cdc_ncm_show_tx_timer_usecs(struct device *d, struct device_attribute *attr, char *buf)
  184. {
  185. struct usbnet *dev = netdev_priv(to_net_dev(d));
  186. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  187. return sprintf(buf, "%u\n", ctx->timer_interval / (u32)NSEC_PER_USEC);
  188. }
  189. static ssize_t cdc_ncm_store_min_tx_pkt(struct device *d, struct device_attribute *attr, const char *buf, size_t len)
  190. {
  191. struct usbnet *dev = netdev_priv(to_net_dev(d));
  192. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  193. unsigned long val;
  194. /* no need to restrict values - anything from 0 to infinity is OK */
  195. if (kstrtoul(buf, 0, &val))
  196. return -EINVAL;
  197. ctx->min_tx_pkt = val;
  198. return len;
  199. }
  200. static ssize_t cdc_ncm_store_rx_max(struct device *d, struct device_attribute *attr, const char *buf, size_t len)
  201. {
  202. struct usbnet *dev = netdev_priv(to_net_dev(d));
  203. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  204. unsigned long val;
  205. if (kstrtoul(buf, 0, &val) || cdc_ncm_check_rx_max(dev, val) != val)
  206. return -EINVAL;
  207. cdc_ncm_update_rxtx_max(dev, val, ctx->tx_max);
  208. return len;
  209. }
  210. static ssize_t cdc_ncm_store_tx_max(struct device *d, struct device_attribute *attr, const char *buf, size_t len)
  211. {
  212. struct usbnet *dev = netdev_priv(to_net_dev(d));
  213. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  214. unsigned long val;
  215. if (kstrtoul(buf, 0, &val) || cdc_ncm_check_tx_max(dev, val) != val)
  216. return -EINVAL;
  217. cdc_ncm_update_rxtx_max(dev, ctx->rx_max, val);
  218. return len;
  219. }
  220. static ssize_t cdc_ncm_store_tx_timer_usecs(struct device *d, struct device_attribute *attr, const char *buf, size_t len)
  221. {
  222. struct usbnet *dev = netdev_priv(to_net_dev(d));
  223. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  224. ssize_t ret;
  225. unsigned long val;
  226. ret = kstrtoul(buf, 0, &val);
  227. if (ret)
  228. return ret;
  229. if (val && (val < CDC_NCM_TIMER_INTERVAL_MIN || val > CDC_NCM_TIMER_INTERVAL_MAX))
  230. return -EINVAL;
  231. spin_lock_bh(&ctx->mtx);
  232. ctx->timer_interval = val * NSEC_PER_USEC;
  233. if (!ctx->timer_interval)
  234. ctx->tx_timer_pending = 0;
  235. spin_unlock_bh(&ctx->mtx);
  236. return len;
  237. }
  238. static DEVICE_ATTR(min_tx_pkt, S_IRUGO | S_IWUSR, cdc_ncm_show_min_tx_pkt, cdc_ncm_store_min_tx_pkt);
  239. static DEVICE_ATTR(rx_max, S_IRUGO | S_IWUSR, cdc_ncm_show_rx_max, cdc_ncm_store_rx_max);
  240. static DEVICE_ATTR(tx_max, S_IRUGO | S_IWUSR, cdc_ncm_show_tx_max, cdc_ncm_store_tx_max);
  241. static DEVICE_ATTR(tx_timer_usecs, S_IRUGO | S_IWUSR, cdc_ncm_show_tx_timer_usecs, cdc_ncm_store_tx_timer_usecs);
  242. #define NCM_PARM_ATTR(name, format, tocpu) \
  243. static ssize_t cdc_ncm_show_##name(struct device *d, struct device_attribute *attr, char *buf) \
  244. { \
  245. struct usbnet *dev = netdev_priv(to_net_dev(d)); \
  246. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; \
  247. return sprintf(buf, format "\n", tocpu(ctx->ncm_parm.name)); \
  248. } \
  249. static DEVICE_ATTR(name, S_IRUGO, cdc_ncm_show_##name, NULL)
  250. NCM_PARM_ATTR(bmNtbFormatsSupported, "0x%04x", le16_to_cpu);
  251. NCM_PARM_ATTR(dwNtbInMaxSize, "%u", le32_to_cpu);
  252. NCM_PARM_ATTR(wNdpInDivisor, "%u", le16_to_cpu);
  253. NCM_PARM_ATTR(wNdpInPayloadRemainder, "%u", le16_to_cpu);
  254. NCM_PARM_ATTR(wNdpInAlignment, "%u", le16_to_cpu);
  255. NCM_PARM_ATTR(dwNtbOutMaxSize, "%u", le32_to_cpu);
  256. NCM_PARM_ATTR(wNdpOutDivisor, "%u", le16_to_cpu);
  257. NCM_PARM_ATTR(wNdpOutPayloadRemainder, "%u", le16_to_cpu);
  258. NCM_PARM_ATTR(wNdpOutAlignment, "%u", le16_to_cpu);
  259. NCM_PARM_ATTR(wNtbOutMaxDatagrams, "%u", le16_to_cpu);
  260. static struct attribute *cdc_ncm_sysfs_attrs[] = {
  261. &dev_attr_min_tx_pkt.attr,
  262. &dev_attr_rx_max.attr,
  263. &dev_attr_tx_max.attr,
  264. &dev_attr_tx_timer_usecs.attr,
  265. &dev_attr_bmNtbFormatsSupported.attr,
  266. &dev_attr_dwNtbInMaxSize.attr,
  267. &dev_attr_wNdpInDivisor.attr,
  268. &dev_attr_wNdpInPayloadRemainder.attr,
  269. &dev_attr_wNdpInAlignment.attr,
  270. &dev_attr_dwNtbOutMaxSize.attr,
  271. &dev_attr_wNdpOutDivisor.attr,
  272. &dev_attr_wNdpOutPayloadRemainder.attr,
  273. &dev_attr_wNdpOutAlignment.attr,
  274. &dev_attr_wNtbOutMaxDatagrams.attr,
  275. NULL,
  276. };
  277. static struct attribute_group cdc_ncm_sysfs_attr_group = {
  278. .name = "cdc_ncm",
  279. .attrs = cdc_ncm_sysfs_attrs,
  280. };
  281. /* handle rx_max and tx_max changes */
  282. static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx)
  283. {
  284. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  285. u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
  286. u32 val;
  287. val = cdc_ncm_check_rx_max(dev, new_rx);
  288. /* inform device about NTB input size changes */
  289. if (val != ctx->rx_max) {
  290. __le32 dwNtbInMaxSize = cpu_to_le32(val);
  291. dev_info(&dev->intf->dev, "setting rx_max = %u\n", val);
  292. /* tell device to use new size */
  293. if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
  294. USB_TYPE_CLASS | USB_DIR_OUT
  295. | USB_RECIP_INTERFACE,
  296. 0, iface_no, &dwNtbInMaxSize, 4) < 0)
  297. dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
  298. else
  299. ctx->rx_max = val;
  300. }
  301. /* usbnet use these values for sizing rx queues */
  302. if (dev->rx_urb_size != ctx->rx_max) {
  303. dev->rx_urb_size = ctx->rx_max;
  304. if (netif_running(dev->net))
  305. usbnet_unlink_rx_urbs(dev);
  306. }
  307. val = cdc_ncm_check_tx_max(dev, new_tx);
  308. if (val != ctx->tx_max)
  309. dev_info(&dev->intf->dev, "setting tx_max = %u\n", val);
  310. /* Adding a pad byte here if necessary simplifies the handling
  311. * in cdc_ncm_fill_tx_frame, making tx_max always represent
  312. * the real skb max size.
  313. *
  314. * We cannot use dev->maxpacket here because this is called from
  315. * .bind which is called before usbnet sets up dev->maxpacket
  316. */
  317. if (val != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
  318. val % usb_maxpacket(dev->udev, dev->out, 1) == 0)
  319. val++;
  320. /* we might need to flush any pending tx buffers if running */
  321. if (netif_running(dev->net) && val > ctx->tx_max) {
  322. netif_tx_lock_bh(dev->net);
  323. usbnet_start_xmit(NULL, dev->net);
  324. /* make sure tx_curr_skb is reallocated if it was empty */
  325. if (ctx->tx_curr_skb) {
  326. dev_kfree_skb_any(ctx->tx_curr_skb);
  327. ctx->tx_curr_skb = NULL;
  328. }
  329. ctx->tx_max = val;
  330. netif_tx_unlock_bh(dev->net);
  331. } else {
  332. ctx->tx_max = val;
  333. }
  334. dev->hard_mtu = ctx->tx_max;
  335. /* max qlen depend on hard_mtu and rx_urb_size */
  336. usbnet_update_max_qlen(dev);
  337. /* never pad more than 3 full USB packets per transfer */
  338. ctx->min_tx_pkt = clamp_t(u16, ctx->tx_max - 3 * usb_maxpacket(dev->udev, dev->out, 1),
  339. CDC_NCM_MIN_TX_PKT, ctx->tx_max);
  340. }
  341. /* helpers for NCM and MBIM differences */
  342. static u8 cdc_ncm_flags(struct usbnet *dev)
  343. {
  344. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  345. if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
  346. return ctx->mbim_desc->bmNetworkCapabilities;
  347. if (ctx->func_desc)
  348. return ctx->func_desc->bmNetworkCapabilities;
  349. return 0;
  350. }
  351. static int cdc_ncm_eth_hlen(struct usbnet *dev)
  352. {
  353. if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
  354. return 0;
  355. return ETH_HLEN;
  356. }
  357. static u32 cdc_ncm_min_dgram_size(struct usbnet *dev)
  358. {
  359. if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
  360. return CDC_MBIM_MIN_DATAGRAM_SIZE;
  361. return CDC_NCM_MIN_DATAGRAM_SIZE;
  362. }
  363. static u32 cdc_ncm_max_dgram_size(struct usbnet *dev)
  364. {
  365. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  366. if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
  367. return le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
  368. if (ctx->ether_desc)
  369. return le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
  370. return CDC_NCM_MAX_DATAGRAM_SIZE;
  371. }
  372. /* initial one-time device setup. MUST be called with the data interface
  373. * in altsetting 0
  374. */
  375. static int cdc_ncm_init(struct usbnet *dev)
  376. {
  377. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  378. u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
  379. int err;
  380. err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
  381. USB_TYPE_CLASS | USB_DIR_IN
  382. |USB_RECIP_INTERFACE,
  383. 0, iface_no, &ctx->ncm_parm,
  384. sizeof(ctx->ncm_parm));
  385. if (err < 0) {
  386. dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
  387. return err; /* GET_NTB_PARAMETERS is required */
  388. }
  389. /* set CRC Mode */
  390. if (cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_CRC_MODE) {
  391. dev_dbg(&dev->intf->dev, "Setting CRC mode off\n");
  392. err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
  393. USB_TYPE_CLASS | USB_DIR_OUT
  394. | USB_RECIP_INTERFACE,
  395. USB_CDC_NCM_CRC_NOT_APPENDED,
  396. iface_no, NULL, 0);
  397. if (err < 0)
  398. dev_err(&dev->intf->dev, "SET_CRC_MODE failed\n");
  399. }
  400. /* set NTB format, if both formats are supported.
  401. *
  402. * "The host shall only send this command while the NCM Data
  403. * Interface is in alternate setting 0."
  404. */
  405. if (le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported) &
  406. USB_CDC_NCM_NTB32_SUPPORTED) {
  407. dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit\n");
  408. err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
  409. USB_TYPE_CLASS | USB_DIR_OUT
  410. | USB_RECIP_INTERFACE,
  411. USB_CDC_NCM_NTB16_FORMAT,
  412. iface_no, NULL, 0);
  413. if (err < 0)
  414. dev_err(&dev->intf->dev, "SET_NTB_FORMAT failed\n");
  415. }
  416. /* set initial device values */
  417. ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
  418. ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
  419. ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
  420. ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
  421. ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
  422. /* devices prior to NCM Errata shall set this field to zero */
  423. ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
  424. dev_dbg(&dev->intf->dev,
  425. "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
  426. ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
  427. ctx->tx_ndp_modulus, ctx->tx_max_datagrams, cdc_ncm_flags(dev));
  428. /* max count of tx datagrams */
  429. if ((ctx->tx_max_datagrams == 0) ||
  430. (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
  431. ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
  432. /* set up maximum NDP size */
  433. ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp16) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe16);
  434. /* initial coalescing timer interval */
  435. ctx->timer_interval = CDC_NCM_TIMER_INTERVAL_USEC * NSEC_PER_USEC;
  436. return 0;
  437. }
  438. /* set a new max datagram size */
  439. static void cdc_ncm_set_dgram_size(struct usbnet *dev, int new_size)
  440. {
  441. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  442. u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
  443. __le16 max_datagram_size;
  444. u16 mbim_mtu;
  445. int err;
  446. /* set default based on descriptors */
  447. ctx->max_datagram_size = clamp_t(u32, new_size,
  448. cdc_ncm_min_dgram_size(dev),
  449. CDC_NCM_MAX_DATAGRAM_SIZE);
  450. /* inform the device about the selected Max Datagram Size? */
  451. if (!(cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
  452. goto out;
  453. /* read current mtu value from device */
  454. err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
  455. USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
  456. 0, iface_no, &max_datagram_size, 2);
  457. if (err < 0) {
  458. dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
  459. goto out;
  460. }
  461. if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
  462. goto out;
  463. max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
  464. err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
  465. USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
  466. 0, iface_no, &max_datagram_size, 2);
  467. if (err < 0)
  468. dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
  469. out:
  470. /* set MTU to max supported by the device if necessary */
  471. dev->net->mtu = min_t(int, dev->net->mtu, ctx->max_datagram_size - cdc_ncm_eth_hlen(dev));
  472. /* do not exceed operater preferred MTU */
  473. if (ctx->mbim_extended_desc) {
  474. mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU);
  475. if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu)
  476. dev->net->mtu = mbim_mtu;
  477. }
  478. }
  479. static void cdc_ncm_fix_modulus(struct usbnet *dev)
  480. {
  481. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  482. u32 val;
  483. /*
  484. * verify that the structure alignment is:
  485. * - power of two
  486. * - not greater than the maximum transmit length
  487. * - not less than four bytes
  488. */
  489. val = ctx->tx_ndp_modulus;
  490. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  491. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  492. dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
  493. ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  494. }
  495. /*
  496. * verify that the payload alignment is:
  497. * - power of two
  498. * - not greater than the maximum transmit length
  499. * - not less than four bytes
  500. */
  501. val = ctx->tx_modulus;
  502. if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
  503. (val != ((-val) & val)) || (val >= ctx->tx_max)) {
  504. dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
  505. ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
  506. }
  507. /* verify the payload remainder */
  508. if (ctx->tx_remainder >= ctx->tx_modulus) {
  509. dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
  510. ctx->tx_remainder = 0;
  511. }
  512. /* adjust TX-remainder according to NCM specification. */
  513. ctx->tx_remainder = ((ctx->tx_remainder - cdc_ncm_eth_hlen(dev)) &
  514. (ctx->tx_modulus - 1));
  515. }
  516. static int cdc_ncm_setup(struct usbnet *dev)
  517. {
  518. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  519. u32 def_rx, def_tx;
  520. /* be conservative when selecting intial buffer size to
  521. * increase the number of hosts this will work for
  522. */
  523. def_rx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_RX,
  524. le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
  525. def_tx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_TX,
  526. le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
  527. /* clamp rx_max and tx_max and inform device */
  528. cdc_ncm_update_rxtx_max(dev, def_rx, def_tx);
  529. /* sanitize the modulus and remainder values */
  530. cdc_ncm_fix_modulus(dev);
  531. /* set max datagram size */
  532. cdc_ncm_set_dgram_size(dev, cdc_ncm_max_dgram_size(dev));
  533. return 0;
  534. }
  535. static void
  536. cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
  537. {
  538. struct usb_host_endpoint *e, *in = NULL, *out = NULL;
  539. u8 ep;
  540. for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
  541. e = intf->cur_altsetting->endpoint + ep;
  542. switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  543. case USB_ENDPOINT_XFER_INT:
  544. if (usb_endpoint_dir_in(&e->desc)) {
  545. if (!dev->status)
  546. dev->status = e;
  547. }
  548. break;
  549. case USB_ENDPOINT_XFER_BULK:
  550. if (usb_endpoint_dir_in(&e->desc)) {
  551. if (!in)
  552. in = e;
  553. } else {
  554. if (!out)
  555. out = e;
  556. }
  557. break;
  558. default:
  559. break;
  560. }
  561. }
  562. if (in && !dev->in)
  563. dev->in = usb_rcvbulkpipe(dev->udev,
  564. in->desc.bEndpointAddress &
  565. USB_ENDPOINT_NUMBER_MASK);
  566. if (out && !dev->out)
  567. dev->out = usb_sndbulkpipe(dev->udev,
  568. out->desc.bEndpointAddress &
  569. USB_ENDPOINT_NUMBER_MASK);
  570. }
  571. static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
  572. {
  573. if (ctx == NULL)
  574. return;
  575. if (ctx->tx_rem_skb != NULL) {
  576. dev_kfree_skb_any(ctx->tx_rem_skb);
  577. ctx->tx_rem_skb = NULL;
  578. }
  579. if (ctx->tx_curr_skb != NULL) {
  580. dev_kfree_skb_any(ctx->tx_curr_skb);
  581. ctx->tx_curr_skb = NULL;
  582. }
  583. kfree(ctx->delayed_ndp16);
  584. kfree(ctx);
  585. }
  586. /* we need to override the usbnet change_mtu ndo for two reasons:
  587. * - respect the negotiated maximum datagram size
  588. * - avoid unwanted changes to rx and tx buffers
  589. */
  590. int cdc_ncm_change_mtu(struct net_device *net, int new_mtu)
  591. {
  592. struct usbnet *dev = netdev_priv(net);
  593. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  594. int maxmtu = ctx->max_datagram_size - cdc_ncm_eth_hlen(dev);
  595. if (new_mtu <= 0 || new_mtu > maxmtu)
  596. return -EINVAL;
  597. net->mtu = new_mtu;
  598. return 0;
  599. }
  600. EXPORT_SYMBOL_GPL(cdc_ncm_change_mtu);
  601. static const struct net_device_ops cdc_ncm_netdev_ops = {
  602. .ndo_open = usbnet_open,
  603. .ndo_stop = usbnet_stop,
  604. .ndo_start_xmit = usbnet_start_xmit,
  605. .ndo_tx_timeout = usbnet_tx_timeout,
  606. .ndo_change_mtu = cdc_ncm_change_mtu,
  607. .ndo_set_mac_address = eth_mac_addr,
  608. .ndo_validate_addr = eth_validate_addr,
  609. };
  610. int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags)
  611. {
  612. struct cdc_ncm_ctx *ctx;
  613. struct usb_driver *driver;
  614. u8 *buf;
  615. int len;
  616. int temp;
  617. int err;
  618. u8 iface_no;
  619. struct usb_cdc_parsed_header hdr;
  620. u16 curr_ntb_format;
  621. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  622. if (!ctx)
  623. return -ENOMEM;
  624. hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  625. ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
  626. ctx->bh.data = (unsigned long)dev;
  627. ctx->bh.func = cdc_ncm_txpath_bh;
  628. atomic_set(&ctx->stop, 0);
  629. spin_lock_init(&ctx->mtx);
  630. /* store ctx pointer in device data field */
  631. dev->data[0] = (unsigned long)ctx;
  632. /* only the control interface can be successfully probed */
  633. ctx->control = intf;
  634. /* get some pointers */
  635. driver = driver_of(intf);
  636. buf = intf->cur_altsetting->extra;
  637. len = intf->cur_altsetting->extralen;
  638. /* parse through descriptors associated with control interface */
  639. cdc_parse_cdc_header(&hdr, intf, buf, len);
  640. if (hdr.usb_cdc_union_desc)
  641. ctx->data = usb_ifnum_to_if(dev->udev,
  642. hdr.usb_cdc_union_desc->bSlaveInterface0);
  643. ctx->ether_desc = hdr.usb_cdc_ether_desc;
  644. ctx->func_desc = hdr.usb_cdc_ncm_desc;
  645. ctx->mbim_desc = hdr.usb_cdc_mbim_desc;
  646. ctx->mbim_extended_desc = hdr.usb_cdc_mbim_extended_desc;
  647. /* some buggy devices have an IAD but no CDC Union */
  648. if (!hdr.usb_cdc_union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
  649. ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
  650. dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
  651. }
  652. /* check if we got everything */
  653. if (!ctx->data) {
  654. dev_dbg(&intf->dev, "CDC Union missing and no IAD found\n");
  655. goto error;
  656. }
  657. if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
  658. if (!ctx->mbim_desc) {
  659. dev_dbg(&intf->dev, "MBIM functional descriptor missing\n");
  660. goto error;
  661. }
  662. } else {
  663. if (!ctx->ether_desc || !ctx->func_desc) {
  664. dev_dbg(&intf->dev, "NCM or ECM functional descriptors missing\n");
  665. goto error;
  666. }
  667. }
  668. /* claim data interface, if different from control */
  669. if (ctx->data != ctx->control) {
  670. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  671. if (temp) {
  672. dev_dbg(&intf->dev, "failed to claim data intf\n");
  673. goto error;
  674. }
  675. }
  676. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  677. /* Reset data interface. Some devices will not reset properly
  678. * unless they are configured first. Toggle the altsetting to
  679. * force a reset
  680. */
  681. usb_set_interface(dev->udev, iface_no, data_altsetting);
  682. temp = usb_set_interface(dev->udev, iface_no, 0);
  683. if (temp) {
  684. dev_dbg(&intf->dev, "set interface failed\n");
  685. goto error2;
  686. }
  687. /* initialize basic device settings */
  688. if (cdc_ncm_init(dev))
  689. goto error2;
  690. /* Some firmwares need a pause here or they will silently fail
  691. * to set up the interface properly. This value was decided
  692. * empirically on a Sierra Wireless MC7455 running 02.08.02.00
  693. * firmware.
  694. */
  695. usleep_range(10000, 20000);
  696. /* configure data interface */
  697. temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
  698. if (temp) {
  699. dev_dbg(&intf->dev, "set interface failed\n");
  700. goto error2;
  701. }
  702. /* Device-specific flags */
  703. ctx->drvflags = drvflags;
  704. /*
  705. * Some Huawei devices have been observed to come out of reset in NDP32 mode.
  706. * Let's check if this is the case, and set the device to NDP16 mode again if
  707. * needed.
  708. */
  709. if (ctx->drvflags & CDC_NCM_FLAG_RESET_NTB16) {
  710. err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_FORMAT,
  711. USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
  712. 0, iface_no, &curr_ntb_format, 2);
  713. if (err < 0) {
  714. goto error2;
  715. }
  716. if (curr_ntb_format == USB_CDC_NCM_NTB32_FORMAT) {
  717. dev_info(&intf->dev, "resetting NTB format to 16-bit");
  718. err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
  719. USB_TYPE_CLASS | USB_DIR_OUT
  720. | USB_RECIP_INTERFACE,
  721. USB_CDC_NCM_NTB16_FORMAT,
  722. iface_no, NULL, 0);
  723. if (err < 0)
  724. goto error2;
  725. }
  726. }
  727. cdc_ncm_find_endpoints(dev, ctx->data);
  728. cdc_ncm_find_endpoints(dev, ctx->control);
  729. if (!dev->in || !dev->out || !dev->status) {
  730. dev_dbg(&intf->dev, "failed to collect endpoints\n");
  731. goto error2;
  732. }
  733. usb_set_intfdata(ctx->data, dev);
  734. usb_set_intfdata(ctx->control, dev);
  735. if (ctx->ether_desc) {
  736. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  737. if (temp) {
  738. dev_dbg(&intf->dev, "failed to get mac address\n");
  739. goto error2;
  740. }
  741. dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
  742. }
  743. /* finish setting up the device specific data */
  744. cdc_ncm_setup(dev);
  745. /* Allocate the delayed NDP if needed. */
  746. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  747. ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
  748. if (!ctx->delayed_ndp16)
  749. goto error2;
  750. dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
  751. }
  752. /* override ethtool_ops */
  753. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  754. /* add our sysfs attrs */
  755. dev->net->sysfs_groups[0] = &cdc_ncm_sysfs_attr_group;
  756. /* must handle MTU changes */
  757. dev->net->netdev_ops = &cdc_ncm_netdev_ops;
  758. return 0;
  759. error2:
  760. usb_set_intfdata(ctx->control, NULL);
  761. usb_set_intfdata(ctx->data, NULL);
  762. if (ctx->data != ctx->control)
  763. usb_driver_release_interface(driver, ctx->data);
  764. error:
  765. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  766. dev->data[0] = 0;
  767. dev_info(&intf->dev, "bind() failure\n");
  768. return -ENODEV;
  769. }
  770. EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
  771. void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  772. {
  773. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  774. struct usb_driver *driver = driver_of(intf);
  775. if (ctx == NULL)
  776. return; /* no setup */
  777. atomic_set(&ctx->stop, 1);
  778. if (hrtimer_active(&ctx->tx_timer))
  779. hrtimer_cancel(&ctx->tx_timer);
  780. tasklet_kill(&ctx->bh);
  781. /* handle devices with combined control and data interface */
  782. if (ctx->control == ctx->data)
  783. ctx->data = NULL;
  784. /* disconnect master --> disconnect slave */
  785. if (intf == ctx->control && ctx->data) {
  786. usb_set_intfdata(ctx->data, NULL);
  787. usb_driver_release_interface(driver, ctx->data);
  788. ctx->data = NULL;
  789. } else if (intf == ctx->data && ctx->control) {
  790. usb_set_intfdata(ctx->control, NULL);
  791. usb_driver_release_interface(driver, ctx->control);
  792. ctx->control = NULL;
  793. }
  794. usb_set_intfdata(intf, NULL);
  795. cdc_ncm_free(ctx);
  796. }
  797. EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
  798. /* Return the number of the MBIM control interface altsetting iff it
  799. * is preferred and available,
  800. */
  801. u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
  802. {
  803. struct usb_host_interface *alt;
  804. /* The MBIM spec defines a NCM compatible default altsetting,
  805. * which we may have matched:
  806. *
  807. * "Functions that implement both NCM 1.0 and MBIM (an
  808. * “NCM/MBIM function”) according to this recommendation
  809. * shall provide two alternate settings for the
  810. * Communication Interface. Alternate setting 0, and the
  811. * associated class and endpoint descriptors, shall be
  812. * constructed according to the rules given for the
  813. * Communication Interface in section 5 of [USBNCM10].
  814. * Alternate setting 1, and the associated class and
  815. * endpoint descriptors, shall be constructed according to
  816. * the rules given in section 6 (USB Device Model) of this
  817. * specification."
  818. */
  819. if (intf->num_altsetting < 2)
  820. return intf->cur_altsetting->desc.bAlternateSetting;
  821. if (prefer_mbim) {
  822. alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
  823. if (alt && cdc_ncm_comm_intf_is_mbim(alt))
  824. return CDC_NCM_COMM_ALTSETTING_MBIM;
  825. }
  826. return CDC_NCM_COMM_ALTSETTING_NCM;
  827. }
  828. EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
  829. static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
  830. {
  831. /* MBIM backwards compatible function? */
  832. if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
  833. return -ENODEV;
  834. /* The NCM data altsetting is fixed, so we hard-coded it.
  835. * Additionally, generic NCM devices are assumed to accept arbitrarily
  836. * placed NDP.
  837. */
  838. return cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
  839. }
  840. static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
  841. {
  842. size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
  843. if (skb->len + align > max)
  844. align = max - skb->len;
  845. if (align && skb_tailroom(skb) >= align)
  846. memset(skb_put(skb, align), 0, align);
  847. }
  848. /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
  849. * allocating a new one within skb
  850. */
  851. static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
  852. {
  853. struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
  854. struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
  855. size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
  856. /* If NDP should be moved to the end of the NCM package, we can't follow the
  857. * NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
  858. * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
  859. */
  860. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  861. if (ctx->delayed_ndp16->dwSignature == sign)
  862. return ctx->delayed_ndp16;
  863. /* We can only push a single NDP to the end. Return
  864. * NULL to send what we've already got and queue this
  865. * skb for later.
  866. */
  867. else if (ctx->delayed_ndp16->dwSignature)
  868. return NULL;
  869. }
  870. /* follow the chain of NDPs, looking for a match */
  871. while (ndpoffset) {
  872. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
  873. if (ndp16->dwSignature == sign)
  874. return ndp16;
  875. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  876. }
  877. /* align new NDP */
  878. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  879. cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
  880. /* verify that there is room for the NDP and the datagram (reserve) */
  881. if ((ctx->tx_max - skb->len - reserve) < ctx->max_ndp_size)
  882. return NULL;
  883. /* link to it */
  884. if (ndp16)
  885. ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
  886. else
  887. nth16->wNdpIndex = cpu_to_le16(skb->len);
  888. /* push a new empty NDP */
  889. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  890. ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, ctx->max_ndp_size), 0, ctx->max_ndp_size);
  891. else
  892. ndp16 = ctx->delayed_ndp16;
  893. ndp16->dwSignature = sign;
  894. ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
  895. return ndp16;
  896. }
  897. struct sk_buff *
  898. cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
  899. {
  900. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  901. struct usb_cdc_ncm_nth16 *nth16;
  902. struct usb_cdc_ncm_ndp16 *ndp16;
  903. struct sk_buff *skb_out;
  904. u16 n = 0, index, ndplen;
  905. u8 ready2send = 0;
  906. u32 delayed_ndp_size;
  907. size_t padding_count;
  908. /* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
  909. * accordingly. Otherwise, we should check here.
  910. */
  911. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
  912. delayed_ndp_size = ALIGN(ctx->max_ndp_size, ctx->tx_ndp_modulus);
  913. else
  914. delayed_ndp_size = 0;
  915. /* if there is a remaining skb, it gets priority */
  916. if (skb != NULL) {
  917. swap(skb, ctx->tx_rem_skb);
  918. swap(sign, ctx->tx_rem_sign);
  919. } else {
  920. ready2send = 1;
  921. }
  922. /* check if we are resuming an OUT skb */
  923. skb_out = ctx->tx_curr_skb;
  924. /* allocate a new OUT skb */
  925. if (!skb_out) {
  926. skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
  927. if (skb_out == NULL) {
  928. if (skb != NULL) {
  929. dev_kfree_skb_any(skb);
  930. dev->net->stats.tx_dropped++;
  931. }
  932. goto exit_no_skb;
  933. }
  934. /* fill out the initial 16-bit NTB header */
  935. nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
  936. nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  937. nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
  938. nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
  939. /* count total number of frames in this NTB */
  940. ctx->tx_curr_frame_num = 0;
  941. /* recent payload counter for this skb_out */
  942. ctx->tx_curr_frame_payload = 0;
  943. }
  944. for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
  945. /* send any remaining skb first */
  946. if (skb == NULL) {
  947. skb = ctx->tx_rem_skb;
  948. sign = ctx->tx_rem_sign;
  949. ctx->tx_rem_skb = NULL;
  950. /* check for end of skb */
  951. if (skb == NULL)
  952. break;
  953. }
  954. /* get the appropriate NDP for this skb */
  955. ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
  956. /* align beginning of next frame */
  957. cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
  958. /* check if we had enough room left for both NDP and frame */
  959. if (!ndp16 || skb_out->len + skb->len + delayed_ndp_size > ctx->tx_max) {
  960. if (n == 0) {
  961. /* won't fit, MTU problem? */
  962. dev_kfree_skb_any(skb);
  963. skb = NULL;
  964. dev->net->stats.tx_dropped++;
  965. } else {
  966. /* no room for skb - store for later */
  967. if (ctx->tx_rem_skb != NULL) {
  968. dev_kfree_skb_any(ctx->tx_rem_skb);
  969. dev->net->stats.tx_dropped++;
  970. }
  971. ctx->tx_rem_skb = skb;
  972. ctx->tx_rem_sign = sign;
  973. skb = NULL;
  974. ready2send = 1;
  975. ctx->tx_reason_ntb_full++; /* count reason for transmitting */
  976. }
  977. break;
  978. }
  979. /* calculate frame number withing this NDP */
  980. ndplen = le16_to_cpu(ndp16->wLength);
  981. index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
  982. /* OK, add this skb */
  983. ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
  984. ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
  985. ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
  986. memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
  987. ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */
  988. dev_kfree_skb_any(skb);
  989. skb = NULL;
  990. /* send now if this NDP is full */
  991. if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
  992. ready2send = 1;
  993. ctx->tx_reason_ndp_full++; /* count reason for transmitting */
  994. break;
  995. }
  996. }
  997. /* free up any dangling skb */
  998. if (skb != NULL) {
  999. dev_kfree_skb_any(skb);
  1000. skb = NULL;
  1001. dev->net->stats.tx_dropped++;
  1002. }
  1003. ctx->tx_curr_frame_num = n;
  1004. if (n == 0) {
  1005. /* wait for more frames */
  1006. /* push variables */
  1007. ctx->tx_curr_skb = skb_out;
  1008. goto exit_no_skb;
  1009. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
  1010. /* wait for more frames */
  1011. /* push variables */
  1012. ctx->tx_curr_skb = skb_out;
  1013. /* set the pending count */
  1014. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  1015. ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
  1016. goto exit_no_skb;
  1017. } else {
  1018. if (n == ctx->tx_max_datagrams)
  1019. ctx->tx_reason_max_datagram++; /* count reason for transmitting */
  1020. /* frame goes out */
  1021. /* variables will be reset at next call */
  1022. }
  1023. /* If requested, put NDP at end of frame. */
  1024. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  1025. nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1026. cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_max - ctx->max_ndp_size);
  1027. nth16->wNdpIndex = cpu_to_le16(skb_out->len);
  1028. memcpy(skb_put(skb_out, ctx->max_ndp_size), ctx->delayed_ndp16, ctx->max_ndp_size);
  1029. /* Zero out delayed NDP - signature checking will naturally fail. */
  1030. ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
  1031. }
  1032. /* If collected data size is less or equal ctx->min_tx_pkt
  1033. * bytes, we send buffers as it is. If we get more data, it
  1034. * would be more efficient for USB HS mobile device with DMA
  1035. * engine to receive a full size NTB, than canceling DMA
  1036. * transfer and receiving a short packet.
  1037. *
  1038. * This optimization support is pointless if we end up sending
  1039. * a ZLP after full sized NTBs.
  1040. */
  1041. if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
  1042. skb_out->len > ctx->min_tx_pkt) {
  1043. padding_count = ctx->tx_max - skb_out->len;
  1044. memset(skb_put(skb_out, padding_count), 0, padding_count);
  1045. } else if (skb_out->len < ctx->tx_max &&
  1046. (skb_out->len % dev->maxpacket) == 0) {
  1047. *skb_put(skb_out, 1) = 0; /* force short packet */
  1048. }
  1049. /* set final frame length */
  1050. nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1051. nth16->wBlockLength = cpu_to_le16(skb_out->len);
  1052. /* return skb */
  1053. ctx->tx_curr_skb = NULL;
  1054. /* keep private stats: framing overhead and number of NTBs */
  1055. ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
  1056. ctx->tx_ntbs++;
  1057. /* usbnet will count all the framing overhead by default.
  1058. * Adjust the stats so that the tx_bytes counter show real
  1059. * payload data instead.
  1060. */
  1061. usbnet_set_skb_tx_stats(skb_out, n,
  1062. (long)ctx->tx_curr_frame_payload - skb_out->len);
  1063. return skb_out;
  1064. exit_no_skb:
  1065. /* Start timer, if there is a remaining non-empty skb */
  1066. if (ctx->tx_curr_skb != NULL && n > 0)
  1067. cdc_ncm_tx_timeout_start(ctx);
  1068. return NULL;
  1069. }
  1070. EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
  1071. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  1072. {
  1073. /* start timer, if not already started */
  1074. if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
  1075. hrtimer_start(&ctx->tx_timer,
  1076. ktime_set(0, ctx->timer_interval),
  1077. HRTIMER_MODE_REL);
  1078. }
  1079. static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
  1080. {
  1081. struct cdc_ncm_ctx *ctx =
  1082. container_of(timer, struct cdc_ncm_ctx, tx_timer);
  1083. if (!atomic_read(&ctx->stop))
  1084. tasklet_schedule(&ctx->bh);
  1085. return HRTIMER_NORESTART;
  1086. }
  1087. static void cdc_ncm_txpath_bh(unsigned long param)
  1088. {
  1089. struct usbnet *dev = (struct usbnet *)param;
  1090. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1091. spin_lock_bh(&ctx->mtx);
  1092. if (ctx->tx_timer_pending != 0) {
  1093. ctx->tx_timer_pending--;
  1094. cdc_ncm_tx_timeout_start(ctx);
  1095. spin_unlock_bh(&ctx->mtx);
  1096. } else if (dev->net != NULL) {
  1097. ctx->tx_reason_timeout++; /* count reason for transmitting */
  1098. spin_unlock_bh(&ctx->mtx);
  1099. netif_tx_lock_bh(dev->net);
  1100. usbnet_start_xmit(NULL, dev->net);
  1101. netif_tx_unlock_bh(dev->net);
  1102. } else {
  1103. spin_unlock_bh(&ctx->mtx);
  1104. }
  1105. }
  1106. struct sk_buff *
  1107. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  1108. {
  1109. struct sk_buff *skb_out;
  1110. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1111. /*
  1112. * The Ethernet API we are using does not support transmitting
  1113. * multiple Ethernet frames in a single call. This driver will
  1114. * accumulate multiple Ethernet frames and send out a larger
  1115. * USB frame when the USB buffer is full or when a single jiffies
  1116. * timeout happens.
  1117. */
  1118. if (ctx == NULL)
  1119. goto error;
  1120. spin_lock_bh(&ctx->mtx);
  1121. skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
  1122. spin_unlock_bh(&ctx->mtx);
  1123. return skb_out;
  1124. error:
  1125. if (skb != NULL)
  1126. dev_kfree_skb_any(skb);
  1127. return NULL;
  1128. }
  1129. EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
  1130. /* verify NTB header and return offset of first NDP, or negative error */
  1131. int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
  1132. {
  1133. struct usbnet *dev = netdev_priv(skb_in->dev);
  1134. struct usb_cdc_ncm_nth16 *nth16;
  1135. int len;
  1136. int ret = -EINVAL;
  1137. if (ctx == NULL)
  1138. goto error;
  1139. if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
  1140. sizeof(struct usb_cdc_ncm_ndp16))) {
  1141. netif_dbg(dev, rx_err, dev->net, "frame too short\n");
  1142. goto error;
  1143. }
  1144. nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
  1145. if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
  1146. netif_dbg(dev, rx_err, dev->net,
  1147. "invalid NTH16 signature <%#010x>\n",
  1148. le32_to_cpu(nth16->dwSignature));
  1149. goto error;
  1150. }
  1151. len = le16_to_cpu(nth16->wBlockLength);
  1152. if (len > ctx->rx_max) {
  1153. netif_dbg(dev, rx_err, dev->net,
  1154. "unsupported NTB block length %u/%u\n", len,
  1155. ctx->rx_max);
  1156. goto error;
  1157. }
  1158. if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
  1159. (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
  1160. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
  1161. netif_dbg(dev, rx_err, dev->net,
  1162. "sequence number glitch prev=%d curr=%d\n",
  1163. ctx->rx_seq, le16_to_cpu(nth16->wSequence));
  1164. }
  1165. ctx->rx_seq = le16_to_cpu(nth16->wSequence);
  1166. ret = le16_to_cpu(nth16->wNdpIndex);
  1167. error:
  1168. return ret;
  1169. }
  1170. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
  1171. /* verify NDP header and return number of datagrams, or negative error */
  1172. int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
  1173. {
  1174. struct usbnet *dev = netdev_priv(skb_in->dev);
  1175. struct usb_cdc_ncm_ndp16 *ndp16;
  1176. int ret = -EINVAL;
  1177. if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
  1178. netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
  1179. ndpoffset);
  1180. goto error;
  1181. }
  1182. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  1183. if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
  1184. netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
  1185. le16_to_cpu(ndp16->wLength));
  1186. goto error;
  1187. }
  1188. ret = ((le16_to_cpu(ndp16->wLength) -
  1189. sizeof(struct usb_cdc_ncm_ndp16)) /
  1190. sizeof(struct usb_cdc_ncm_dpe16));
  1191. ret--; /* we process NDP entries except for the last one */
  1192. if ((sizeof(struct usb_cdc_ncm_ndp16) +
  1193. ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
  1194. netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
  1195. ret = -EINVAL;
  1196. }
  1197. error:
  1198. return ret;
  1199. }
  1200. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
  1201. int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  1202. {
  1203. struct sk_buff *skb;
  1204. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1205. int len;
  1206. int nframes;
  1207. int x;
  1208. int offset;
  1209. struct usb_cdc_ncm_ndp16 *ndp16;
  1210. struct usb_cdc_ncm_dpe16 *dpe16;
  1211. int ndpoffset;
  1212. int loopcount = 50; /* arbitrary max preventing infinite loop */
  1213. u32 payload = 0;
  1214. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  1215. if (ndpoffset < 0)
  1216. goto error;
  1217. next_ndp:
  1218. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  1219. if (nframes < 0)
  1220. goto error;
  1221. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  1222. if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
  1223. netif_dbg(dev, rx_err, dev->net,
  1224. "invalid DPT16 signature <%#010x>\n",
  1225. le32_to_cpu(ndp16->dwSignature));
  1226. goto err_ndp;
  1227. }
  1228. dpe16 = ndp16->dpe16;
  1229. for (x = 0; x < nframes; x++, dpe16++) {
  1230. offset = le16_to_cpu(dpe16->wDatagramIndex);
  1231. len = le16_to_cpu(dpe16->wDatagramLength);
  1232. /*
  1233. * CDC NCM ch. 3.7
  1234. * All entries after first NULL entry are to be ignored
  1235. */
  1236. if ((offset == 0) || (len == 0)) {
  1237. if (!x)
  1238. goto err_ndp; /* empty NTB */
  1239. break;
  1240. }
  1241. /* sanity checking */
  1242. if (((offset + len) > skb_in->len) ||
  1243. (len > ctx->rx_max) || (len < ETH_HLEN)) {
  1244. netif_dbg(dev, rx_err, dev->net,
  1245. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  1246. x, offset, len, skb_in);
  1247. if (!x)
  1248. goto err_ndp;
  1249. break;
  1250. } else {
  1251. /* create a fresh copy to reduce truesize */
  1252. skb = netdev_alloc_skb_ip_align(dev->net, len);
  1253. if (!skb)
  1254. goto error;
  1255. memcpy(skb_put(skb, len), skb_in->data + offset, len);
  1256. usbnet_skb_return(dev, skb);
  1257. payload += len; /* count payload bytes in this NTB */
  1258. }
  1259. }
  1260. err_ndp:
  1261. /* are there more NDPs to process? */
  1262. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  1263. if (ndpoffset && loopcount--)
  1264. goto next_ndp;
  1265. /* update stats */
  1266. ctx->rx_overhead += skb_in->len - payload;
  1267. ctx->rx_ntbs++;
  1268. return 1;
  1269. error:
  1270. return 0;
  1271. }
  1272. EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
  1273. static void
  1274. cdc_ncm_speed_change(struct usbnet *dev,
  1275. struct usb_cdc_speed_change *data)
  1276. {
  1277. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  1278. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  1279. /*
  1280. * Currently the USB-NET API does not support reporting the actual
  1281. * device speed. Do print it instead.
  1282. */
  1283. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  1284. netif_info(dev, link, dev->net,
  1285. "%u mbit/s downlink %u mbit/s uplink\n",
  1286. (unsigned int)(rx_speed / 1000000U),
  1287. (unsigned int)(tx_speed / 1000000U));
  1288. } else {
  1289. netif_info(dev, link, dev->net,
  1290. "%u kbit/s downlink %u kbit/s uplink\n",
  1291. (unsigned int)(rx_speed / 1000U),
  1292. (unsigned int)(tx_speed / 1000U));
  1293. }
  1294. }
  1295. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  1296. {
  1297. struct cdc_ncm_ctx *ctx;
  1298. struct usb_cdc_notification *event;
  1299. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1300. if (urb->actual_length < sizeof(*event))
  1301. return;
  1302. /* test for split data in 8-byte chunks */
  1303. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  1304. cdc_ncm_speed_change(dev,
  1305. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  1306. return;
  1307. }
  1308. event = urb->transfer_buffer;
  1309. switch (event->bNotificationType) {
  1310. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  1311. /*
  1312. * According to the CDC NCM specification ch.7.1
  1313. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  1314. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  1315. */
  1316. netif_info(dev, link, dev->net,
  1317. "network connection: %sconnected\n",
  1318. !!event->wValue ? "" : "dis");
  1319. usbnet_link_change(dev, !!event->wValue, 0);
  1320. break;
  1321. case USB_CDC_NOTIFY_SPEED_CHANGE:
  1322. if (urb->actual_length < (sizeof(*event) +
  1323. sizeof(struct usb_cdc_speed_change)))
  1324. set_bit(EVENT_STS_SPLIT, &dev->flags);
  1325. else
  1326. cdc_ncm_speed_change(dev,
  1327. (struct usb_cdc_speed_change *)&event[1]);
  1328. break;
  1329. default:
  1330. dev_dbg(&dev->udev->dev,
  1331. "NCM: unexpected notification 0x%02x!\n",
  1332. event->bNotificationType);
  1333. break;
  1334. }
  1335. }
  1336. static const struct driver_info cdc_ncm_info = {
  1337. .description = "CDC NCM",
  1338. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1339. | FLAG_LINK_INTR,
  1340. .bind = cdc_ncm_bind,
  1341. .unbind = cdc_ncm_unbind,
  1342. .manage_power = usbnet_manage_power,
  1343. .status = cdc_ncm_status,
  1344. .rx_fixup = cdc_ncm_rx_fixup,
  1345. .tx_fixup = cdc_ncm_tx_fixup,
  1346. };
  1347. /* Same as cdc_ncm_info, but with FLAG_WWAN */
  1348. static const struct driver_info wwan_info = {
  1349. .description = "Mobile Broadband Network Device",
  1350. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1351. | FLAG_LINK_INTR | FLAG_WWAN,
  1352. .bind = cdc_ncm_bind,
  1353. .unbind = cdc_ncm_unbind,
  1354. .manage_power = usbnet_manage_power,
  1355. .status = cdc_ncm_status,
  1356. .rx_fixup = cdc_ncm_rx_fixup,
  1357. .tx_fixup = cdc_ncm_tx_fixup,
  1358. };
  1359. /* Same as wwan_info, but with FLAG_NOARP */
  1360. static const struct driver_info wwan_noarp_info = {
  1361. .description = "Mobile Broadband Network Device (NO ARP)",
  1362. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1363. | FLAG_LINK_INTR | FLAG_WWAN | FLAG_NOARP,
  1364. .bind = cdc_ncm_bind,
  1365. .unbind = cdc_ncm_unbind,
  1366. .manage_power = usbnet_manage_power,
  1367. .status = cdc_ncm_status,
  1368. .rx_fixup = cdc_ncm_rx_fixup,
  1369. .tx_fixup = cdc_ncm_tx_fixup,
  1370. };
  1371. static const struct usb_device_id cdc_devs[] = {
  1372. /* Ericsson MBM devices like F5521gw */
  1373. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1374. | USB_DEVICE_ID_MATCH_VENDOR,
  1375. .idVendor = 0x0bdb,
  1376. .bInterfaceClass = USB_CLASS_COMM,
  1377. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1378. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1379. .driver_info = (unsigned long) &wwan_info,
  1380. },
  1381. /* DW5812 LTE Verizon Mobile Broadband Card
  1382. * Unlike DW5550 this device requires FLAG_NOARP
  1383. */
  1384. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bb,
  1385. USB_CLASS_COMM,
  1386. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1387. .driver_info = (unsigned long)&wwan_noarp_info,
  1388. },
  1389. /* DW5813 LTE AT&T Mobile Broadband Card
  1390. * Unlike DW5550 this device requires FLAG_NOARP
  1391. */
  1392. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bc,
  1393. USB_CLASS_COMM,
  1394. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1395. .driver_info = (unsigned long)&wwan_noarp_info,
  1396. },
  1397. /* Dell branded MBM devices like DW5550 */
  1398. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1399. | USB_DEVICE_ID_MATCH_VENDOR,
  1400. .idVendor = 0x413c,
  1401. .bInterfaceClass = USB_CLASS_COMM,
  1402. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1403. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1404. .driver_info = (unsigned long) &wwan_info,
  1405. },
  1406. /* Toshiba branded MBM devices */
  1407. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1408. | USB_DEVICE_ID_MATCH_VENDOR,
  1409. .idVendor = 0x0930,
  1410. .bInterfaceClass = USB_CLASS_COMM,
  1411. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1412. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1413. .driver_info = (unsigned long) &wwan_info,
  1414. },
  1415. /* tag Huawei devices as wwan */
  1416. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
  1417. USB_CLASS_COMM,
  1418. USB_CDC_SUBCLASS_NCM,
  1419. USB_CDC_PROTO_NONE),
  1420. .driver_info = (unsigned long)&wwan_info,
  1421. },
  1422. /* Infineon(now Intel) HSPA Modem platform */
  1423. { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
  1424. USB_CLASS_COMM,
  1425. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1426. .driver_info = (unsigned long)&wwan_noarp_info,
  1427. },
  1428. /* Generic CDC-NCM devices */
  1429. { USB_INTERFACE_INFO(USB_CLASS_COMM,
  1430. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1431. .driver_info = (unsigned long)&cdc_ncm_info,
  1432. },
  1433. {
  1434. },
  1435. };
  1436. MODULE_DEVICE_TABLE(usb, cdc_devs);
  1437. static struct usb_driver cdc_ncm_driver = {
  1438. .name = "cdc_ncm",
  1439. .id_table = cdc_devs,
  1440. .probe = usbnet_probe,
  1441. .disconnect = usbnet_disconnect,
  1442. .suspend = usbnet_suspend,
  1443. .resume = usbnet_resume,
  1444. .reset_resume = usbnet_resume,
  1445. .supports_autosuspend = 1,
  1446. .disable_hub_initiated_lpm = 1,
  1447. };
  1448. module_usb_driver(cdc_ncm_driver);
  1449. MODULE_AUTHOR("Hans Petter Selasky");
  1450. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1451. MODULE_LICENSE("Dual BSD/GPL");