cdc_ncm.c 52 KB

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