cdc_ncm.c 51 KB

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