cdc_ncm.c 64 KB

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