cdc_ncm.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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. ctx->bh.data = (unsigned long)dev;
  664. ctx->bh.func = cdc_ncm_txpath_bh;
  665. atomic_set(&ctx->stop, 0);
  666. spin_lock_init(&ctx->mtx);
  667. /* store ctx pointer in device data field */
  668. dev->data[0] = (unsigned long)ctx;
  669. /* only the control interface can be successfully probed */
  670. ctx->control = intf;
  671. /* get some pointers */
  672. driver = driver_of(intf);
  673. buf = intf->cur_altsetting->extra;
  674. len = intf->cur_altsetting->extralen;
  675. /* parse through descriptors associated with control interface */
  676. cdc_parse_cdc_header(&hdr, intf, buf, len);
  677. if (hdr.usb_cdc_union_desc)
  678. ctx->data = usb_ifnum_to_if(dev->udev,
  679. hdr.usb_cdc_union_desc->bSlaveInterface0);
  680. ctx->ether_desc = hdr.usb_cdc_ether_desc;
  681. ctx->func_desc = hdr.usb_cdc_ncm_desc;
  682. ctx->mbim_desc = hdr.usb_cdc_mbim_desc;
  683. ctx->mbim_extended_desc = hdr.usb_cdc_mbim_extended_desc;
  684. /* some buggy devices have an IAD but no CDC Union */
  685. if (!hdr.usb_cdc_union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
  686. ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
  687. dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
  688. }
  689. /* check if we got everything */
  690. if (!ctx->data) {
  691. dev_dbg(&intf->dev, "CDC Union missing and no IAD found\n");
  692. goto error;
  693. }
  694. if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
  695. if (!ctx->mbim_desc) {
  696. dev_dbg(&intf->dev, "MBIM functional descriptor missing\n");
  697. goto error;
  698. }
  699. } else {
  700. if (!ctx->ether_desc || !ctx->func_desc) {
  701. dev_dbg(&intf->dev, "NCM or ECM functional descriptors missing\n");
  702. goto error;
  703. }
  704. }
  705. /* claim data interface, if different from control */
  706. if (ctx->data != ctx->control) {
  707. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  708. if (temp) {
  709. dev_dbg(&intf->dev, "failed to claim data intf\n");
  710. goto error;
  711. }
  712. }
  713. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  714. /* Device-specific flags */
  715. ctx->drvflags = drvflags;
  716. /* Reset data interface. Some devices will not reset properly
  717. * unless they are configured first. Toggle the altsetting to
  718. * force a reset.
  719. * Some other devices do not work properly with this procedure
  720. * that can be avoided using quirk CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
  721. */
  722. if (!(ctx->drvflags & CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE))
  723. usb_set_interface(dev->udev, iface_no, data_altsetting);
  724. temp = usb_set_interface(dev->udev, iface_no, 0);
  725. if (temp) {
  726. dev_dbg(&intf->dev, "set interface failed\n");
  727. goto error2;
  728. }
  729. /* initialize basic device settings */
  730. if (cdc_ncm_init(dev))
  731. goto error2;
  732. /* Some firmwares need a pause here or they will silently fail
  733. * to set up the interface properly. This value was decided
  734. * empirically on a Sierra Wireless MC7455 running 02.08.02.00
  735. * firmware.
  736. */
  737. usleep_range(10000, 20000);
  738. /* configure data interface */
  739. temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
  740. if (temp) {
  741. dev_dbg(&intf->dev, "set interface failed\n");
  742. goto error2;
  743. }
  744. /*
  745. * Some Huawei devices have been observed to come out of reset in NDP32 mode.
  746. * Let's check if this is the case, and set the device to NDP16 mode again if
  747. * needed.
  748. */
  749. if (ctx->drvflags & CDC_NCM_FLAG_RESET_NTB16) {
  750. err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_FORMAT,
  751. USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
  752. 0, iface_no, &curr_ntb_format, 2);
  753. if (err < 0) {
  754. goto error2;
  755. }
  756. if (curr_ntb_format == cpu_to_le16(USB_CDC_NCM_NTB32_FORMAT)) {
  757. dev_info(&intf->dev, "resetting NTB format to 16-bit");
  758. err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
  759. USB_TYPE_CLASS | USB_DIR_OUT
  760. | USB_RECIP_INTERFACE,
  761. USB_CDC_NCM_NTB16_FORMAT,
  762. iface_no, NULL, 0);
  763. if (err < 0)
  764. goto error2;
  765. }
  766. }
  767. cdc_ncm_find_endpoints(dev, ctx->data);
  768. cdc_ncm_find_endpoints(dev, ctx->control);
  769. if (!dev->in || !dev->out /*|| !dev->status*/) {
  770. dev_dbg(&intf->dev, "failed to collect endpoints\n");
  771. goto error2;
  772. }
  773. usb_set_intfdata(ctx->data, dev);
  774. usb_set_intfdata(ctx->control, dev);
  775. if (ctx->ether_desc) {
  776. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  777. if (temp) {
  778. dev_dbg(&intf->dev, "failed to get mac address\n");
  779. #ifndef CONFIG_USB_CONFIGFS_F_APPLE_PTP_SIM
  780. goto error2;
  781. #endif
  782. }
  783. dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
  784. }
  785. /* finish setting up the device specific data */
  786. cdc_ncm_setup(dev);
  787. /* Allocate the delayed NDP if needed. */
  788. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  789. ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
  790. if (!ctx->delayed_ndp16)
  791. goto error2;
  792. dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
  793. }
  794. /* override ethtool_ops */
  795. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  796. /* add our sysfs attrs */
  797. dev->net->sysfs_groups[0] = &cdc_ncm_sysfs_attr_group;
  798. /* must handle MTU changes */
  799. dev->net->netdev_ops = &cdc_ncm_netdev_ops;
  800. dev->net->max_mtu = cdc_ncm_max_dgram_size(dev) - cdc_ncm_eth_hlen(dev);
  801. return 0;
  802. error2:
  803. usb_set_intfdata(ctx->control, NULL);
  804. usb_set_intfdata(ctx->data, NULL);
  805. if (ctx->data != ctx->control)
  806. usb_driver_release_interface(driver, ctx->data);
  807. error:
  808. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  809. dev->data[0] = 0;
  810. dev_info(&intf->dev, "bind() failure\n");
  811. return -ENODEV;
  812. }
  813. EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
  814. void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  815. {
  816. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  817. struct usb_driver *driver = driver_of(intf);
  818. if (ctx == NULL)
  819. return; /* no setup */
  820. atomic_set(&ctx->stop, 1);
  821. hrtimer_cancel(&ctx->tx_timer);
  822. tasklet_kill(&ctx->bh);
  823. /* handle devices with combined control and data interface */
  824. if (ctx->control == ctx->data)
  825. ctx->data = NULL;
  826. /* disconnect master --> disconnect slave */
  827. if (intf == ctx->control && ctx->data) {
  828. usb_set_intfdata(ctx->data, NULL);
  829. usb_driver_release_interface(driver, ctx->data);
  830. ctx->data = NULL;
  831. } else if (intf == ctx->data && ctx->control) {
  832. usb_set_intfdata(ctx->control, NULL);
  833. usb_driver_release_interface(driver, ctx->control);
  834. ctx->control = NULL;
  835. }
  836. usb_set_intfdata(intf, NULL);
  837. cdc_ncm_free(ctx);
  838. }
  839. EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
  840. /* Return the number of the MBIM control interface altsetting iff it
  841. * is preferred and available,
  842. */
  843. u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
  844. {
  845. struct usb_host_interface *alt;
  846. /* The MBIM spec defines a NCM compatible default altsetting,
  847. * which we may have matched:
  848. *
  849. * "Functions that implement both NCM 1.0 and MBIM (an
  850. * “NCM/MBIM function”) according to this recommendation
  851. * shall provide two alternate settings for the
  852. * Communication Interface. Alternate setting 0, and the
  853. * associated class and endpoint descriptors, shall be
  854. * constructed according to the rules given for the
  855. * Communication Interface in section 5 of [USBNCM10].
  856. * Alternate setting 1, and the associated class and
  857. * endpoint descriptors, shall be constructed according to
  858. * the rules given in section 6 (USB Device Model) of this
  859. * specification."
  860. */
  861. if (intf->num_altsetting < 2)
  862. return intf->cur_altsetting->desc.bAlternateSetting;
  863. if (prefer_mbim) {
  864. alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
  865. if (alt && cdc_ncm_comm_intf_is_mbim(alt))
  866. return CDC_NCM_COMM_ALTSETTING_MBIM;
  867. }
  868. return CDC_NCM_COMM_ALTSETTING_NCM;
  869. }
  870. EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
  871. static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
  872. {
  873. /* MBIM backwards compatible function? */
  874. if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
  875. return -ENODEV;
  876. /* The NCM data altsetting is fixed, so we hard-coded it.
  877. * Additionally, generic NCM devices are assumed to accept arbitrarily
  878. * placed NDP.
  879. */
  880. return cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
  881. }
  882. static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
  883. {
  884. size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
  885. if (skb->len + align > max)
  886. align = max - skb->len;
  887. if (align && skb_tailroom(skb) >= align)
  888. skb_put_zero(skb, align);
  889. }
  890. /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
  891. * allocating a new one within skb
  892. */
  893. static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
  894. {
  895. struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
  896. struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
  897. size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
  898. /* If NDP should be moved to the end of the NCM package, we can't follow the
  899. * NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
  900. * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
  901. */
  902. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  903. if (ctx->delayed_ndp16->dwSignature == sign)
  904. return ctx->delayed_ndp16;
  905. /* We can only push a single NDP to the end. Return
  906. * NULL to send what we've already got and queue this
  907. * skb for later.
  908. */
  909. else if (ctx->delayed_ndp16->dwSignature)
  910. return NULL;
  911. }
  912. /* follow the chain of NDPs, looking for a match */
  913. while (ndpoffset) {
  914. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
  915. if (ndp16->dwSignature == sign)
  916. return ndp16;
  917. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  918. }
  919. /* align new NDP */
  920. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  921. cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size);
  922. /* verify that there is room for the NDP and the datagram (reserve) */
  923. if ((ctx->tx_curr_size - skb->len - reserve) < ctx->max_ndp_size)
  924. return NULL;
  925. /* link to it */
  926. if (ndp16)
  927. ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
  928. else
  929. nth16->wNdpIndex = cpu_to_le16(skb->len);
  930. /* push a new empty NDP */
  931. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  932. ndp16 = skb_put_zero(skb, ctx->max_ndp_size);
  933. else
  934. ndp16 = ctx->delayed_ndp16;
  935. ndp16->dwSignature = sign;
  936. ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
  937. return ndp16;
  938. }
  939. struct sk_buff *
  940. cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
  941. {
  942. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  943. struct usb_cdc_ncm_nth16 *nth16;
  944. struct usb_cdc_ncm_ndp16 *ndp16;
  945. struct sk_buff *skb_out;
  946. u16 n = 0, index, ndplen;
  947. u8 ready2send = 0;
  948. u32 delayed_ndp_size;
  949. size_t padding_count;
  950. /* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
  951. * accordingly. Otherwise, we should check here.
  952. */
  953. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
  954. delayed_ndp_size = ctx->max_ndp_size +
  955. max_t(u32,
  956. ctx->tx_ndp_modulus,
  957. ctx->tx_modulus + ctx->tx_remainder) - 1;
  958. else
  959. delayed_ndp_size = 0;
  960. /* if there is a remaining skb, it gets priority */
  961. if (skb != NULL) {
  962. swap(skb, ctx->tx_rem_skb);
  963. swap(sign, ctx->tx_rem_sign);
  964. } else {
  965. ready2send = 1;
  966. }
  967. /* check if we are resuming an OUT skb */
  968. skb_out = ctx->tx_curr_skb;
  969. /* allocate a new OUT skb */
  970. if (!skb_out) {
  971. if (ctx->tx_low_mem_val == 0) {
  972. ctx->tx_curr_size = ctx->tx_max;
  973. skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
  974. /* If the memory allocation fails we will wait longer
  975. * each time before attempting another full size
  976. * allocation again to not overload the system
  977. * further.
  978. */
  979. if (skb_out == NULL) {
  980. ctx->tx_low_mem_max_cnt = min(ctx->tx_low_mem_max_cnt + 1,
  981. (unsigned)CDC_NCM_LOW_MEM_MAX_CNT);
  982. ctx->tx_low_mem_val = ctx->tx_low_mem_max_cnt;
  983. }
  984. }
  985. if (skb_out == NULL) {
  986. /* See if a very small allocation is possible.
  987. * We will send this packet immediately and hope
  988. * that there is more memory available later.
  989. */
  990. if (skb)
  991. ctx->tx_curr_size = max(skb->len,
  992. (u32)USB_CDC_NCM_NTB_MIN_OUT_SIZE);
  993. else
  994. ctx->tx_curr_size = USB_CDC_NCM_NTB_MIN_OUT_SIZE;
  995. skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
  996. /* No allocation possible so we will abort */
  997. if (skb_out == NULL) {
  998. if (skb != NULL) {
  999. dev_kfree_skb_any(skb);
  1000. dev->net->stats.tx_dropped++;
  1001. }
  1002. goto exit_no_skb;
  1003. }
  1004. ctx->tx_low_mem_val--;
  1005. }
  1006. /* fill out the initial 16-bit NTB header */
  1007. nth16 = skb_put_zero(skb_out, sizeof(struct usb_cdc_ncm_nth16));
  1008. nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  1009. nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
  1010. nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
  1011. /* count total number of frames in this NTB */
  1012. ctx->tx_curr_frame_num = 0;
  1013. /* recent payload counter for this skb_out */
  1014. ctx->tx_curr_frame_payload = 0;
  1015. }
  1016. for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
  1017. /* send any remaining skb first */
  1018. if (skb == NULL) {
  1019. skb = ctx->tx_rem_skb;
  1020. sign = ctx->tx_rem_sign;
  1021. ctx->tx_rem_skb = NULL;
  1022. /* check for end of skb */
  1023. if (skb == NULL)
  1024. break;
  1025. }
  1026. /* get the appropriate NDP for this skb */
  1027. ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
  1028. /* align beginning of next frame */
  1029. cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_curr_size);
  1030. /* check if we had enough room left for both NDP and frame */
  1031. if (!ndp16 || skb_out->len + skb->len + delayed_ndp_size > ctx->tx_curr_size) {
  1032. if (n == 0) {
  1033. /* won't fit, MTU problem? */
  1034. dev_kfree_skb_any(skb);
  1035. skb = NULL;
  1036. dev->net->stats.tx_dropped++;
  1037. } else {
  1038. /* no room for skb - store for later */
  1039. if (ctx->tx_rem_skb != NULL) {
  1040. dev_kfree_skb_any(ctx->tx_rem_skb);
  1041. dev->net->stats.tx_dropped++;
  1042. }
  1043. ctx->tx_rem_skb = skb;
  1044. ctx->tx_rem_sign = sign;
  1045. skb = NULL;
  1046. ready2send = 1;
  1047. ctx->tx_reason_ntb_full++; /* count reason for transmitting */
  1048. }
  1049. break;
  1050. }
  1051. /* calculate frame number withing this NDP */
  1052. ndplen = le16_to_cpu(ndp16->wLength);
  1053. index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
  1054. /* OK, add this skb */
  1055. ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
  1056. ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
  1057. ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
  1058. skb_put_data(skb_out, skb->data, skb->len);
  1059. ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */
  1060. dev_kfree_skb_any(skb);
  1061. skb = NULL;
  1062. /* send now if this NDP is full */
  1063. if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
  1064. ready2send = 1;
  1065. ctx->tx_reason_ndp_full++; /* count reason for transmitting */
  1066. break;
  1067. }
  1068. }
  1069. /* free up any dangling skb */
  1070. if (skb != NULL) {
  1071. dev_kfree_skb_any(skb);
  1072. skb = NULL;
  1073. dev->net->stats.tx_dropped++;
  1074. }
  1075. ctx->tx_curr_frame_num = n;
  1076. if (n == 0) {
  1077. /* wait for more frames */
  1078. /* push variables */
  1079. ctx->tx_curr_skb = skb_out;
  1080. goto exit_no_skb;
  1081. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
  1082. /* wait for more frames */
  1083. /* push variables */
  1084. ctx->tx_curr_skb = skb_out;
  1085. /* set the pending count */
  1086. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  1087. ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
  1088. goto exit_no_skb;
  1089. } else {
  1090. if (n == ctx->tx_max_datagrams)
  1091. ctx->tx_reason_max_datagram++; /* count reason for transmitting */
  1092. /* frame goes out */
  1093. /* variables will be reset at next call */
  1094. }
  1095. /* If requested, put NDP at end of frame. */
  1096. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  1097. nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1098. cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size - ctx->max_ndp_size);
  1099. nth16->wNdpIndex = cpu_to_le16(skb_out->len);
  1100. skb_put_data(skb_out, ctx->delayed_ndp16, ctx->max_ndp_size);
  1101. /* Zero out delayed NDP - signature checking will naturally fail. */
  1102. ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
  1103. }
  1104. /* If collected data size is less or equal ctx->min_tx_pkt
  1105. * bytes, we send buffers as it is. If we get more data, it
  1106. * would be more efficient for USB HS mobile device with DMA
  1107. * engine to receive a full size NTB, than canceling DMA
  1108. * transfer and receiving a short packet.
  1109. *
  1110. * This optimization support is pointless if we end up sending
  1111. * a ZLP after full sized NTBs.
  1112. */
  1113. if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
  1114. skb_out->len > ctx->min_tx_pkt) {
  1115. padding_count = ctx->tx_curr_size - skb_out->len;
  1116. if (!WARN_ON(padding_count > ctx->tx_curr_size))
  1117. skb_put_zero(skb_out, padding_count);
  1118. } else if (skb_out->len < ctx->tx_curr_size &&
  1119. (skb_out->len % dev->maxpacket) == 0) {
  1120. skb_put_u8(skb_out, 0); /* force short packet */
  1121. }
  1122. /* set final frame length */
  1123. nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1124. nth16->wBlockLength = cpu_to_le16(skb_out->len);
  1125. /* return skb */
  1126. ctx->tx_curr_skb = NULL;
  1127. /* keep private stats: framing overhead and number of NTBs */
  1128. ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
  1129. ctx->tx_ntbs++;
  1130. /* usbnet will count all the framing overhead by default.
  1131. * Adjust the stats so that the tx_bytes counter show real
  1132. * payload data instead.
  1133. */
  1134. usbnet_set_skb_tx_stats(skb_out, n,
  1135. (long)ctx->tx_curr_frame_payload - skb_out->len);
  1136. return skb_out;
  1137. exit_no_skb:
  1138. /* Start timer, if there is a remaining non-empty skb */
  1139. if (ctx->tx_curr_skb != NULL && n > 0)
  1140. cdc_ncm_tx_timeout_start(ctx);
  1141. return NULL;
  1142. }
  1143. EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
  1144. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  1145. {
  1146. /* start timer, if not already started */
  1147. if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
  1148. hrtimer_start(&ctx->tx_timer,
  1149. ctx->timer_interval,
  1150. HRTIMER_MODE_REL);
  1151. }
  1152. static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
  1153. {
  1154. struct cdc_ncm_ctx *ctx =
  1155. container_of(timer, struct cdc_ncm_ctx, tx_timer);
  1156. if (!atomic_read(&ctx->stop))
  1157. tasklet_schedule(&ctx->bh);
  1158. return HRTIMER_NORESTART;
  1159. }
  1160. static void cdc_ncm_txpath_bh(unsigned long param)
  1161. {
  1162. struct usbnet *dev = (struct usbnet *)param;
  1163. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1164. spin_lock_bh(&ctx->mtx);
  1165. if (ctx->tx_timer_pending != 0) {
  1166. ctx->tx_timer_pending--;
  1167. cdc_ncm_tx_timeout_start(ctx);
  1168. spin_unlock_bh(&ctx->mtx);
  1169. } else if (dev->net != NULL) {
  1170. ctx->tx_reason_timeout++; /* count reason for transmitting */
  1171. spin_unlock_bh(&ctx->mtx);
  1172. netif_tx_lock_bh(dev->net);
  1173. usbnet_start_xmit(NULL, dev->net);
  1174. netif_tx_unlock_bh(dev->net);
  1175. } else {
  1176. spin_unlock_bh(&ctx->mtx);
  1177. }
  1178. }
  1179. struct sk_buff *
  1180. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  1181. {
  1182. struct sk_buff *skb_out;
  1183. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1184. /*
  1185. * The Ethernet API we are using does not support transmitting
  1186. * multiple Ethernet frames in a single call. This driver will
  1187. * accumulate multiple Ethernet frames and send out a larger
  1188. * USB frame when the USB buffer is full or when a single jiffies
  1189. * timeout happens.
  1190. */
  1191. if (ctx == NULL)
  1192. goto error;
  1193. spin_lock_bh(&ctx->mtx);
  1194. skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
  1195. spin_unlock_bh(&ctx->mtx);
  1196. return skb_out;
  1197. error:
  1198. if (skb != NULL)
  1199. dev_kfree_skb_any(skb);
  1200. return NULL;
  1201. }
  1202. EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
  1203. /* verify NTB header and return offset of first NDP, or negative error */
  1204. int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
  1205. {
  1206. struct usbnet *dev = netdev_priv(skb_in->dev);
  1207. struct usb_cdc_ncm_nth16 *nth16;
  1208. int len;
  1209. int ret = -EINVAL;
  1210. if (ctx == NULL)
  1211. goto error;
  1212. if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
  1213. sizeof(struct usb_cdc_ncm_ndp16))) {
  1214. netif_dbg(dev, rx_err, dev->net, "frame too short\n");
  1215. goto error;
  1216. }
  1217. nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
  1218. if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
  1219. netif_dbg(dev, rx_err, dev->net,
  1220. "invalid NTH16 signature <%#010x>\n",
  1221. le32_to_cpu(nth16->dwSignature));
  1222. goto error;
  1223. }
  1224. len = le16_to_cpu(nth16->wBlockLength);
  1225. if (len > ctx->rx_max) {
  1226. netif_dbg(dev, rx_err, dev->net,
  1227. "unsupported NTB block length %u/%u\n", len,
  1228. ctx->rx_max);
  1229. goto error;
  1230. }
  1231. if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
  1232. (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
  1233. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
  1234. netif_dbg(dev, rx_err, dev->net,
  1235. "sequence number glitch prev=%d curr=%d\n",
  1236. ctx->rx_seq, le16_to_cpu(nth16->wSequence));
  1237. }
  1238. ctx->rx_seq = le16_to_cpu(nth16->wSequence);
  1239. ret = le16_to_cpu(nth16->wNdpIndex);
  1240. error:
  1241. return ret;
  1242. }
  1243. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
  1244. /* verify NDP header and return number of datagrams, or negative error */
  1245. int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
  1246. {
  1247. struct usbnet *dev = netdev_priv(skb_in->dev);
  1248. struct usb_cdc_ncm_ndp16 *ndp16;
  1249. int ret = -EINVAL;
  1250. if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
  1251. netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
  1252. ndpoffset);
  1253. goto error;
  1254. }
  1255. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  1256. if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
  1257. netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
  1258. le16_to_cpu(ndp16->wLength));
  1259. goto error;
  1260. }
  1261. ret = ((le16_to_cpu(ndp16->wLength) -
  1262. sizeof(struct usb_cdc_ncm_ndp16)) /
  1263. sizeof(struct usb_cdc_ncm_dpe16));
  1264. ret--; /* we process NDP entries except for the last one */
  1265. if ((sizeof(struct usb_cdc_ncm_ndp16) +
  1266. ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
  1267. netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
  1268. ret = -EINVAL;
  1269. }
  1270. error:
  1271. return ret;
  1272. }
  1273. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
  1274. int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  1275. {
  1276. struct sk_buff *skb;
  1277. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1278. int len;
  1279. int nframes;
  1280. int x;
  1281. int offset;
  1282. struct usb_cdc_ncm_ndp16 *ndp16;
  1283. struct usb_cdc_ncm_dpe16 *dpe16;
  1284. int ndpoffset;
  1285. int loopcount = 50; /* arbitrary max preventing infinite loop */
  1286. u32 payload = 0;
  1287. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  1288. if (ndpoffset < 0)
  1289. goto error;
  1290. next_ndp:
  1291. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  1292. if (nframes < 0)
  1293. goto error;
  1294. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  1295. if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
  1296. netif_dbg(dev, rx_err, dev->net,
  1297. "invalid DPT16 signature <%#010x>\n",
  1298. le32_to_cpu(ndp16->dwSignature));
  1299. goto err_ndp;
  1300. }
  1301. dpe16 = ndp16->dpe16;
  1302. for (x = 0; x < nframes; x++, dpe16++) {
  1303. offset = le16_to_cpu(dpe16->wDatagramIndex);
  1304. len = le16_to_cpu(dpe16->wDatagramLength);
  1305. /*
  1306. * CDC NCM ch. 3.7
  1307. * All entries after first NULL entry are to be ignored
  1308. */
  1309. if ((offset == 0) || (len == 0)) {
  1310. if (!x)
  1311. goto err_ndp; /* empty NTB */
  1312. break;
  1313. }
  1314. /* sanity checking */
  1315. if (((offset + len) > skb_in->len) ||
  1316. (len > ctx->rx_max) || (len < ETH_HLEN)) {
  1317. netif_dbg(dev, rx_err, dev->net,
  1318. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  1319. x, offset, len, skb_in);
  1320. if (!x)
  1321. goto err_ndp;
  1322. break;
  1323. } else {
  1324. /* create a fresh copy to reduce truesize */
  1325. skb = netdev_alloc_skb_ip_align(dev->net, len);
  1326. if (!skb)
  1327. goto error;
  1328. skb_put_data(skb, skb_in->data + offset, len);
  1329. usbnet_skb_return(dev, skb);
  1330. payload += len; /* count payload bytes in this NTB */
  1331. }
  1332. }
  1333. err_ndp:
  1334. /* are there more NDPs to process? */
  1335. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  1336. if (ndpoffset && loopcount--)
  1337. goto next_ndp;
  1338. /* update stats */
  1339. ctx->rx_overhead += skb_in->len - payload;
  1340. ctx->rx_ntbs++;
  1341. return 1;
  1342. error:
  1343. return 0;
  1344. }
  1345. EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
  1346. static void
  1347. cdc_ncm_speed_change(struct usbnet *dev,
  1348. struct usb_cdc_speed_change *data)
  1349. {
  1350. uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
  1351. uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
  1352. /*
  1353. * Currently the USB-NET API does not support reporting the actual
  1354. * device speed. Do print it instead.
  1355. */
  1356. if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
  1357. netif_info(dev, link, dev->net,
  1358. "%u mbit/s downlink %u mbit/s uplink\n",
  1359. (unsigned int)(rx_speed / 1000000U),
  1360. (unsigned int)(tx_speed / 1000000U));
  1361. } else {
  1362. netif_info(dev, link, dev->net,
  1363. "%u kbit/s downlink %u kbit/s uplink\n",
  1364. (unsigned int)(rx_speed / 1000U),
  1365. (unsigned int)(tx_speed / 1000U));
  1366. }
  1367. }
  1368. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  1369. {
  1370. struct cdc_ncm_ctx *ctx;
  1371. struct usb_cdc_notification *event;
  1372. ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1373. if (urb->actual_length < sizeof(*event))
  1374. return;
  1375. /* test for split data in 8-byte chunks */
  1376. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  1377. cdc_ncm_speed_change(dev,
  1378. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  1379. return;
  1380. }
  1381. event = urb->transfer_buffer;
  1382. switch (event->bNotificationType) {
  1383. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  1384. /*
  1385. * According to the CDC NCM specification ch.7.1
  1386. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  1387. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  1388. */
  1389. usbnet_link_change(dev, !!event->wValue, 0);
  1390. break;
  1391. case USB_CDC_NOTIFY_SPEED_CHANGE:
  1392. if (urb->actual_length < (sizeof(*event) +
  1393. sizeof(struct usb_cdc_speed_change)))
  1394. set_bit(EVENT_STS_SPLIT, &dev->flags);
  1395. else
  1396. cdc_ncm_speed_change(dev,
  1397. (struct usb_cdc_speed_change *)&event[1]);
  1398. break;
  1399. default:
  1400. dev_dbg(&dev->udev->dev,
  1401. "NCM: unexpected notification 0x%02x!\n",
  1402. event->bNotificationType);
  1403. break;
  1404. }
  1405. }
  1406. static const struct driver_info cdc_ncm_info = {
  1407. .description = "CDC NCM",
  1408. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1409. | FLAG_LINK_INTR,
  1410. .bind = cdc_ncm_bind,
  1411. .unbind = cdc_ncm_unbind,
  1412. .manage_power = usbnet_manage_power,
  1413. .status = cdc_ncm_status,
  1414. .rx_fixup = cdc_ncm_rx_fixup,
  1415. .tx_fixup = cdc_ncm_tx_fixup,
  1416. };
  1417. /* Same as cdc_ncm_info, but with FLAG_WWAN */
  1418. static const struct driver_info wwan_info = {
  1419. .description = "Mobile Broadband Network Device",
  1420. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1421. | FLAG_LINK_INTR | FLAG_WWAN,
  1422. .bind = cdc_ncm_bind,
  1423. .unbind = cdc_ncm_unbind,
  1424. .manage_power = usbnet_manage_power,
  1425. .status = cdc_ncm_status,
  1426. .rx_fixup = cdc_ncm_rx_fixup,
  1427. .tx_fixup = cdc_ncm_tx_fixup,
  1428. };
  1429. /* Same as wwan_info, but with FLAG_NOARP */
  1430. static const struct driver_info wwan_noarp_info = {
  1431. .description = "Mobile Broadband Network Device (NO ARP)",
  1432. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1433. | FLAG_LINK_INTR | FLAG_WWAN | FLAG_NOARP,
  1434. .bind = cdc_ncm_bind,
  1435. .unbind = cdc_ncm_unbind,
  1436. .manage_power = usbnet_manage_power,
  1437. .status = cdc_ncm_status,
  1438. .rx_fixup = cdc_ncm_rx_fixup,
  1439. .tx_fixup = cdc_ncm_tx_fixup,
  1440. };
  1441. static const struct usb_device_id cdc_devs[] = {
  1442. /* Ericsson MBM devices like F5521gw */
  1443. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1444. | USB_DEVICE_ID_MATCH_VENDOR,
  1445. .idVendor = 0x0bdb,
  1446. .bInterfaceClass = USB_CLASS_COMM,
  1447. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1448. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1449. .driver_info = (unsigned long) &wwan_info,
  1450. },
  1451. /* Telit LE910 V2 */
  1452. { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x0036,
  1453. USB_CLASS_COMM,
  1454. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1455. .driver_info = (unsigned long)&wwan_noarp_info,
  1456. },
  1457. /* DW5812 LTE Verizon Mobile Broadband Card
  1458. * Unlike DW5550 this device requires FLAG_NOARP
  1459. */
  1460. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bb,
  1461. USB_CLASS_COMM,
  1462. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1463. .driver_info = (unsigned long)&wwan_noarp_info,
  1464. },
  1465. /* DW5813 LTE AT&T Mobile Broadband Card
  1466. * Unlike DW5550 this device requires FLAG_NOARP
  1467. */
  1468. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bc,
  1469. USB_CLASS_COMM,
  1470. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1471. .driver_info = (unsigned long)&wwan_noarp_info,
  1472. },
  1473. /* Dell branded MBM devices like DW5550 */
  1474. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1475. | USB_DEVICE_ID_MATCH_VENDOR,
  1476. .idVendor = 0x413c,
  1477. .bInterfaceClass = USB_CLASS_COMM,
  1478. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1479. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1480. .driver_info = (unsigned long) &wwan_info,
  1481. },
  1482. /* Toshiba branded MBM devices */
  1483. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1484. | USB_DEVICE_ID_MATCH_VENDOR,
  1485. .idVendor = 0x0930,
  1486. .bInterfaceClass = USB_CLASS_COMM,
  1487. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1488. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1489. .driver_info = (unsigned long) &wwan_info,
  1490. },
  1491. /* tag Huawei devices as wwan */
  1492. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
  1493. USB_CLASS_COMM,
  1494. USB_CDC_SUBCLASS_NCM,
  1495. USB_CDC_PROTO_NONE),
  1496. .driver_info = (unsigned long)&wwan_info,
  1497. },
  1498. /* Infineon(now Intel) HSPA Modem platform */
  1499. { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
  1500. USB_CLASS_COMM,
  1501. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1502. .driver_info = (unsigned long)&wwan_noarp_info,
  1503. },
  1504. /* u-blox TOBY-L4 */
  1505. { USB_DEVICE_AND_INTERFACE_INFO(0x1546, 0x1010,
  1506. USB_CLASS_COMM,
  1507. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1508. .driver_info = (unsigned long)&wwan_info,
  1509. },
  1510. /* Generic CDC-NCM devices */
  1511. { USB_INTERFACE_INFO(USB_CLASS_COMM,
  1512. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1513. .driver_info = (unsigned long)&cdc_ncm_info,
  1514. },
  1515. {
  1516. },
  1517. };
  1518. MODULE_DEVICE_TABLE(usb, cdc_devs);
  1519. static struct usb_driver cdc_ncm_driver = {
  1520. .name = "cdc_ncm",
  1521. .id_table = cdc_devs,
  1522. .probe = usbnet_probe,
  1523. .disconnect = usbnet_disconnect,
  1524. .suspend = usbnet_suspend,
  1525. .resume = usbnet_resume,
  1526. .reset_resume = usbnet_resume,
  1527. .supports_autosuspend = 1,
  1528. .disable_hub_initiated_lpm = 1,
  1529. };
  1530. module_usb_driver(cdc_ncm_driver);
  1531. MODULE_AUTHOR("Hans Petter Selasky");
  1532. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1533. MODULE_LICENSE("Dual BSD/GPL");