cdc_ncm.c 54 KB

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