cdc_ncm.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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_obj(*ctx);
  707. if (!ctx)
  708. return -ENOMEM;
  709. ctx->dev = dev;
  710. hrtimer_setup(&ctx->tx_timer, &cdc_ncm_tx_timer_cb, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  711. tasklet_setup(&ctx->bh, cdc_ncm_txpath_bh);
  712. atomic_set(&ctx->stop, 0);
  713. spin_lock_init(&ctx->mtx);
  714. /* store ctx pointer in device data field */
  715. dev->data[0] = (unsigned long)ctx;
  716. /* only the control interface can be successfully probed */
  717. ctx->control = intf;
  718. /* get some pointers */
  719. driver = driver_of(intf);
  720. buf = intf->cur_altsetting->extra;
  721. len = intf->cur_altsetting->extralen;
  722. /* parse through descriptors associated with control interface */
  723. cdc_parse_cdc_header(&hdr, intf, buf, len);
  724. if (hdr.usb_cdc_union_desc)
  725. ctx->data = usb_ifnum_to_if(dev->udev,
  726. hdr.usb_cdc_union_desc->bSlaveInterface0);
  727. ctx->ether_desc = hdr.usb_cdc_ether_desc;
  728. ctx->func_desc = hdr.usb_cdc_ncm_desc;
  729. ctx->mbim_desc = hdr.usb_cdc_mbim_desc;
  730. ctx->mbim_extended_desc = hdr.usb_cdc_mbim_extended_desc;
  731. /* some buggy devices have an IAD but no CDC Union */
  732. if (!hdr.usb_cdc_union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
  733. ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
  734. dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
  735. }
  736. /* check if we got everything */
  737. if (!ctx->data) {
  738. dev_err(&intf->dev, "CDC Union missing and no IAD found\n");
  739. goto error;
  740. }
  741. if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
  742. if (!ctx->mbim_desc) {
  743. dev_err(&intf->dev, "MBIM functional descriptor missing\n");
  744. goto error;
  745. }
  746. } else {
  747. if (!ctx->ether_desc || !ctx->func_desc) {
  748. dev_err(&intf->dev, "NCM or ECM functional descriptors missing\n");
  749. goto error;
  750. }
  751. }
  752. /* claim data interface, if different from control */
  753. if (ctx->data != ctx->control) {
  754. temp = usb_driver_claim_interface(driver, ctx->data, dev);
  755. if (temp) {
  756. dev_err(&intf->dev, "failed to claim data intf\n");
  757. goto error;
  758. }
  759. }
  760. if (ctx->func_desc)
  761. ctx->filtering_supported = !!(ctx->func_desc->bmNetworkCapabilities
  762. & USB_CDC_NCM_NCAP_ETH_FILTER);
  763. iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
  764. /* Device-specific flags */
  765. ctx->drvflags = drvflags;
  766. /* Reset data interface. Some devices will not reset properly
  767. * unless they are configured first. Toggle the altsetting to
  768. * force a reset.
  769. * Some other devices do not work properly with this procedure
  770. * that can be avoided using quirk CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
  771. */
  772. if (!(ctx->drvflags & CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE))
  773. usb_set_interface(dev->udev, iface_no, data_altsetting);
  774. temp = usb_set_interface(dev->udev, iface_no, 0);
  775. if (temp) {
  776. dev_dbg(&intf->dev, "set interface failed\n");
  777. goto error2;
  778. }
  779. /* initialize basic device settings */
  780. if (cdc_ncm_init(dev))
  781. goto error2;
  782. /* Some firmwares need a pause here or they will silently fail
  783. * to set up the interface properly. This value was decided
  784. * empirically on a Sierra Wireless MC7455 running 02.08.02.00
  785. * firmware.
  786. */
  787. usleep_range(10000, 20000);
  788. /* configure data interface */
  789. temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
  790. if (temp) {
  791. dev_dbg(&intf->dev, "set interface failed\n");
  792. goto error2;
  793. }
  794. cdc_ncm_find_endpoints(dev, ctx->data);
  795. cdc_ncm_find_endpoints(dev, ctx->control);
  796. if (!dev->in || !dev->out ||
  797. (!dev->status && dev->driver_info->flags & FLAG_LINK_INTR)) {
  798. dev_dbg(&intf->dev, "failed to collect endpoints\n");
  799. goto error2;
  800. }
  801. usb_set_intfdata(ctx->control, dev);
  802. if (ctx->ether_desc) {
  803. temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
  804. if (temp) {
  805. dev_err(&intf->dev, "failed to get mac address\n");
  806. goto error2;
  807. }
  808. dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
  809. }
  810. /* finish setting up the device specific data */
  811. cdc_ncm_setup(dev);
  812. /* Allocate the delayed NDP if needed. */
  813. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  814. if (ctx->is_ndp16) {
  815. ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
  816. if (!ctx->delayed_ndp16)
  817. goto error2;
  818. } else {
  819. ctx->delayed_ndp32 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
  820. if (!ctx->delayed_ndp32)
  821. goto error2;
  822. }
  823. dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
  824. }
  825. /* override ethtool_ops */
  826. dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
  827. /* add our sysfs attrs */
  828. dev->net->sysfs_groups[0] = &cdc_ncm_sysfs_attr_group;
  829. /* must handle MTU changes */
  830. dev->net->netdev_ops = &cdc_ncm_netdev_ops;
  831. dev->net->max_mtu = cdc_ncm_max_dgram_size(dev) - cdc_ncm_eth_hlen(dev);
  832. return 0;
  833. error2:
  834. usb_set_intfdata(ctx->control, NULL);
  835. usb_set_intfdata(ctx->data, NULL);
  836. if (ctx->data != ctx->control)
  837. usb_driver_release_interface(driver, ctx->data);
  838. error:
  839. cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
  840. dev->data[0] = 0;
  841. dev_info(&intf->dev, "bind() failure\n");
  842. return -ENODEV;
  843. }
  844. EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
  845. void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
  846. {
  847. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  848. struct usb_driver *driver = driver_of(intf);
  849. if (ctx == NULL)
  850. return; /* no setup */
  851. atomic_set(&ctx->stop, 1);
  852. hrtimer_cancel(&ctx->tx_timer);
  853. tasklet_kill(&ctx->bh);
  854. /* handle devices with combined control and data interface */
  855. if (ctx->control == ctx->data)
  856. ctx->data = NULL;
  857. /* disconnect master --> disconnect slave */
  858. if (intf == ctx->control && ctx->data) {
  859. usb_set_intfdata(ctx->data, NULL);
  860. usb_driver_release_interface(driver, ctx->data);
  861. ctx->data = NULL;
  862. } else if (intf == ctx->data && ctx->control) {
  863. usb_set_intfdata(ctx->control, NULL);
  864. usb_driver_release_interface(driver, ctx->control);
  865. ctx->control = NULL;
  866. }
  867. usb_set_intfdata(intf, NULL);
  868. cdc_ncm_free(ctx);
  869. }
  870. EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
  871. /* Return the number of the MBIM control interface altsetting iff it
  872. * is preferred and available,
  873. */
  874. u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
  875. {
  876. struct usb_host_interface *alt;
  877. /* The MBIM spec defines a NCM compatible default altsetting,
  878. * which we may have matched:
  879. *
  880. * "Functions that implement both NCM 1.0 and MBIM (an
  881. * “NCM/MBIM function”) according to this recommendation
  882. * shall provide two alternate settings for the
  883. * Communication Interface. Alternate setting 0, and the
  884. * associated class and endpoint descriptors, shall be
  885. * constructed according to the rules given for the
  886. * Communication Interface in section 5 of [USBNCM10].
  887. * Alternate setting 1, and the associated class and
  888. * endpoint descriptors, shall be constructed according to
  889. * the rules given in section 6 (USB Device Model) of this
  890. * specification."
  891. */
  892. if (intf->num_altsetting < 2)
  893. return intf->cur_altsetting->desc.bAlternateSetting;
  894. if (prefer_mbim) {
  895. alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
  896. if (alt && cdc_ncm_comm_intf_is_mbim(alt))
  897. return CDC_NCM_COMM_ALTSETTING_MBIM;
  898. }
  899. return CDC_NCM_COMM_ALTSETTING_NCM;
  900. }
  901. EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
  902. static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
  903. {
  904. /* MBIM backwards compatible function? */
  905. if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
  906. return -ENODEV;
  907. /* The NCM data altsetting is fixed, so we hard-coded it.
  908. * Additionally, generic NCM devices are assumed to accept arbitrarily
  909. * placed NDP.
  910. */
  911. return cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
  912. }
  913. static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
  914. {
  915. size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
  916. if (skb->len + align > max)
  917. align = max - skb->len;
  918. if (align && skb_tailroom(skb) >= align)
  919. skb_put_zero(skb, align);
  920. }
  921. /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
  922. * allocating a new one within skb
  923. */
  924. static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
  925. {
  926. struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
  927. struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
  928. size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
  929. /* If NDP should be moved to the end of the NCM package, we can't follow the
  930. * NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
  931. * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
  932. */
  933. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  934. if (ctx->delayed_ndp16->dwSignature == sign)
  935. return ctx->delayed_ndp16;
  936. /* We can only push a single NDP to the end. Return
  937. * NULL to send what we've already got and queue this
  938. * skb for later.
  939. */
  940. else if (ctx->delayed_ndp16->dwSignature)
  941. return NULL;
  942. }
  943. /* follow the chain of NDPs, looking for a match */
  944. while (ndpoffset) {
  945. ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
  946. if (ndp16->dwSignature == sign)
  947. return ndp16;
  948. ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
  949. }
  950. /* align new NDP */
  951. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  952. cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size);
  953. /* verify that there is room for the NDP and the datagram (reserve) */
  954. if ((ctx->tx_curr_size - skb->len - reserve) < ctx->max_ndp_size)
  955. return NULL;
  956. /* link to it */
  957. if (ndp16)
  958. ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
  959. else
  960. nth16->wNdpIndex = cpu_to_le16(skb->len);
  961. /* push a new empty NDP */
  962. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  963. ndp16 = skb_put_zero(skb, ctx->max_ndp_size);
  964. else
  965. ndp16 = ctx->delayed_ndp16;
  966. ndp16->dwSignature = sign;
  967. ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
  968. return ndp16;
  969. }
  970. static struct usb_cdc_ncm_ndp32 *cdc_ncm_ndp32(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
  971. {
  972. struct usb_cdc_ncm_ndp32 *ndp32 = NULL;
  973. struct usb_cdc_ncm_nth32 *nth32 = (void *)skb->data;
  974. size_t ndpoffset = le32_to_cpu(nth32->dwNdpIndex);
  975. /* If NDP should be moved to the end of the NCM package, we can't follow the
  976. * NTH32 header as we would normally do. NDP isn't written to the SKB yet, and
  977. * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
  978. */
  979. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  980. if (ctx->delayed_ndp32->dwSignature == sign)
  981. return ctx->delayed_ndp32;
  982. /* We can only push a single NDP to the end. Return
  983. * NULL to send what we've already got and queue this
  984. * skb for later.
  985. */
  986. else if (ctx->delayed_ndp32->dwSignature)
  987. return NULL;
  988. }
  989. /* follow the chain of NDPs, looking for a match */
  990. while (ndpoffset) {
  991. ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb->data + ndpoffset);
  992. if (ndp32->dwSignature == sign)
  993. return ndp32;
  994. ndpoffset = le32_to_cpu(ndp32->dwNextNdpIndex);
  995. }
  996. /* align new NDP */
  997. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  998. cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size);
  999. /* verify that there is room for the NDP and the datagram (reserve) */
  1000. if ((ctx->tx_curr_size - skb->len - reserve) < ctx->max_ndp_size)
  1001. return NULL;
  1002. /* link to it */
  1003. if (ndp32)
  1004. ndp32->dwNextNdpIndex = cpu_to_le32(skb->len);
  1005. else
  1006. nth32->dwNdpIndex = cpu_to_le32(skb->len);
  1007. /* push a new empty NDP */
  1008. if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
  1009. ndp32 = skb_put_zero(skb, ctx->max_ndp_size);
  1010. else
  1011. ndp32 = ctx->delayed_ndp32;
  1012. ndp32->dwSignature = sign;
  1013. ndp32->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp32) + sizeof(struct usb_cdc_ncm_dpe32));
  1014. return ndp32;
  1015. }
  1016. struct sk_buff *
  1017. cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
  1018. {
  1019. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1020. union {
  1021. struct usb_cdc_ncm_nth16 *nth16;
  1022. struct usb_cdc_ncm_nth32 *nth32;
  1023. } nth;
  1024. union {
  1025. struct usb_cdc_ncm_ndp16 *ndp16;
  1026. struct usb_cdc_ncm_ndp32 *ndp32;
  1027. } ndp;
  1028. struct sk_buff *skb_out;
  1029. u16 n = 0, index, ndplen;
  1030. u8 ready2send = 0;
  1031. u32 delayed_ndp_size;
  1032. size_t padding_count;
  1033. /* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
  1034. * accordingly. Otherwise, we should check here.
  1035. */
  1036. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
  1037. delayed_ndp_size = ctx->max_ndp_size +
  1038. max_t(u32,
  1039. ctx->tx_ndp_modulus,
  1040. ctx->tx_modulus + ctx->tx_remainder) - 1;
  1041. else
  1042. delayed_ndp_size = 0;
  1043. /* if there is a remaining skb, it gets priority */
  1044. if (skb != NULL) {
  1045. swap(skb, ctx->tx_rem_skb);
  1046. swap(sign, ctx->tx_rem_sign);
  1047. } else {
  1048. ready2send = 1;
  1049. }
  1050. /* check if we are resuming an OUT skb */
  1051. skb_out = ctx->tx_curr_skb;
  1052. /* allocate a new OUT skb */
  1053. if (!skb_out) {
  1054. if (ctx->tx_low_mem_val == 0) {
  1055. ctx->tx_curr_size = ctx->tx_max;
  1056. skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
  1057. /* If the memory allocation fails we will wait longer
  1058. * each time before attempting another full size
  1059. * allocation again to not overload the system
  1060. * further.
  1061. */
  1062. if (skb_out == NULL) {
  1063. /* If even the smallest allocation fails, abort. */
  1064. if (ctx->tx_curr_size == USB_CDC_NCM_NTB_MIN_OUT_SIZE)
  1065. goto alloc_failed;
  1066. ctx->tx_low_mem_max_cnt = min(ctx->tx_low_mem_max_cnt + 1,
  1067. (unsigned)CDC_NCM_LOW_MEM_MAX_CNT);
  1068. ctx->tx_low_mem_val = ctx->tx_low_mem_max_cnt;
  1069. }
  1070. }
  1071. if (skb_out == NULL) {
  1072. /* See if a very small allocation is possible.
  1073. * We will send this packet immediately and hope
  1074. * that there is more memory available later.
  1075. */
  1076. if (skb)
  1077. ctx->tx_curr_size = max(skb->len,
  1078. (u32)USB_CDC_NCM_NTB_MIN_OUT_SIZE);
  1079. else
  1080. ctx->tx_curr_size = USB_CDC_NCM_NTB_MIN_OUT_SIZE;
  1081. skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
  1082. /* No allocation possible so we will abort */
  1083. if (!skb_out)
  1084. goto alloc_failed;
  1085. ctx->tx_low_mem_val--;
  1086. }
  1087. if (ctx->is_ndp16) {
  1088. /* fill out the initial 16-bit NTB header */
  1089. nth.nth16 = skb_put_zero(skb_out, sizeof(struct usb_cdc_ncm_nth16));
  1090. nth.nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
  1091. nth.nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
  1092. nth.nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
  1093. } else {
  1094. /* fill out the initial 32-bit NTB header */
  1095. nth.nth32 = skb_put_zero(skb_out, sizeof(struct usb_cdc_ncm_nth32));
  1096. nth.nth32->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH32_SIGN);
  1097. nth.nth32->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth32));
  1098. nth.nth32->wSequence = cpu_to_le16(ctx->tx_seq++);
  1099. }
  1100. /* count total number of frames in this NTB */
  1101. ctx->tx_curr_frame_num = 0;
  1102. /* recent payload counter for this skb_out */
  1103. ctx->tx_curr_frame_payload = 0;
  1104. }
  1105. for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
  1106. /* send any remaining skb first */
  1107. if (skb == NULL) {
  1108. skb = ctx->tx_rem_skb;
  1109. sign = ctx->tx_rem_sign;
  1110. ctx->tx_rem_skb = NULL;
  1111. /* check for end of skb */
  1112. if (skb == NULL)
  1113. break;
  1114. }
  1115. /* get the appropriate NDP for this skb */
  1116. if (ctx->is_ndp16)
  1117. ndp.ndp16 = cdc_ncm_ndp16(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
  1118. else
  1119. ndp.ndp32 = cdc_ncm_ndp32(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
  1120. /* align beginning of next frame */
  1121. cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_curr_size);
  1122. /* check if we had enough room left for both NDP and frame */
  1123. if ((ctx->is_ndp16 && !ndp.ndp16) || (!ctx->is_ndp16 && !ndp.ndp32) ||
  1124. skb_out->len + skb->len + delayed_ndp_size > ctx->tx_curr_size) {
  1125. if (n == 0) {
  1126. /* won't fit, MTU problem? */
  1127. dev_kfree_skb_any(skb);
  1128. skb = NULL;
  1129. dev->net->stats.tx_dropped++;
  1130. } else {
  1131. /* no room for skb - store for later */
  1132. if (ctx->tx_rem_skb != NULL) {
  1133. dev_kfree_skb_any(ctx->tx_rem_skb);
  1134. dev->net->stats.tx_dropped++;
  1135. }
  1136. ctx->tx_rem_skb = skb;
  1137. ctx->tx_rem_sign = sign;
  1138. skb = NULL;
  1139. ready2send = 1;
  1140. ctx->tx_reason_ntb_full++; /* count reason for transmitting */
  1141. }
  1142. break;
  1143. }
  1144. /* calculate frame number within this NDP */
  1145. if (ctx->is_ndp16) {
  1146. ndplen = le16_to_cpu(ndp.ndp16->wLength);
  1147. index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
  1148. /* OK, add this skb */
  1149. ndp.ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
  1150. ndp.ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
  1151. ndp.ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
  1152. } else {
  1153. ndplen = le16_to_cpu(ndp.ndp32->wLength);
  1154. index = (ndplen - sizeof(struct usb_cdc_ncm_ndp32)) / sizeof(struct usb_cdc_ncm_dpe32) - 1;
  1155. ndp.ndp32->dpe32[index].dwDatagramLength = cpu_to_le32(skb->len);
  1156. ndp.ndp32->dpe32[index].dwDatagramIndex = cpu_to_le32(skb_out->len);
  1157. ndp.ndp32->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe32));
  1158. }
  1159. skb_put_data(skb_out, skb->data, skb->len);
  1160. ctx->tx_curr_frame_payload += skb->len; /* count real tx payload data */
  1161. dev_kfree_skb_any(skb);
  1162. skb = NULL;
  1163. /* send now if this NDP is full */
  1164. if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
  1165. ready2send = 1;
  1166. ctx->tx_reason_ndp_full++; /* count reason for transmitting */
  1167. break;
  1168. }
  1169. }
  1170. /* free up any dangling skb */
  1171. if (skb != NULL) {
  1172. dev_kfree_skb_any(skb);
  1173. skb = NULL;
  1174. dev->net->stats.tx_dropped++;
  1175. }
  1176. ctx->tx_curr_frame_num = n;
  1177. if (n == 0) {
  1178. /* wait for more frames */
  1179. /* push variables */
  1180. ctx->tx_curr_skb = skb_out;
  1181. goto exit_no_skb;
  1182. } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
  1183. /* wait for more frames */
  1184. /* push variables */
  1185. ctx->tx_curr_skb = skb_out;
  1186. /* set the pending count */
  1187. if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
  1188. ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
  1189. goto exit_no_skb;
  1190. } else {
  1191. if (n == ctx->tx_max_datagrams)
  1192. ctx->tx_reason_max_datagram++; /* count reason for transmitting */
  1193. /* frame goes out */
  1194. /* variables will be reset at next call */
  1195. }
  1196. /* If requested, put NDP at end of frame. */
  1197. if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
  1198. if (ctx->is_ndp16) {
  1199. nth.nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1200. cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size - ctx->max_ndp_size);
  1201. nth.nth16->wNdpIndex = cpu_to_le16(skb_out->len);
  1202. skb_put_data(skb_out, ctx->delayed_ndp16, ctx->max_ndp_size);
  1203. /* Zero out delayed NDP - signature checking will naturally fail. */
  1204. ndp.ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
  1205. } else {
  1206. nth.nth32 = (struct usb_cdc_ncm_nth32 *)skb_out->data;
  1207. cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size - ctx->max_ndp_size);
  1208. nth.nth32->dwNdpIndex = cpu_to_le32(skb_out->len);
  1209. skb_put_data(skb_out, ctx->delayed_ndp32, ctx->max_ndp_size);
  1210. ndp.ndp32 = memset(ctx->delayed_ndp32, 0, ctx->max_ndp_size);
  1211. }
  1212. }
  1213. /* If collected data size is less or equal ctx->min_tx_pkt
  1214. * bytes, we send buffers as it is. If we get more data, it
  1215. * would be more efficient for USB HS mobile device with DMA
  1216. * engine to receive a full size NTB, than canceling DMA
  1217. * transfer and receiving a short packet.
  1218. *
  1219. * This optimization support is pointless if we end up sending
  1220. * a ZLP after full sized NTBs.
  1221. */
  1222. if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
  1223. skb_out->len > ctx->min_tx_pkt) {
  1224. padding_count = ctx->tx_curr_size - skb_out->len;
  1225. if (!WARN_ON(padding_count > ctx->tx_curr_size))
  1226. skb_put_zero(skb_out, padding_count);
  1227. } else if (skb_out->len < ctx->tx_curr_size &&
  1228. (skb_out->len % dev->maxpacket) == 0) {
  1229. skb_put_u8(skb_out, 0); /* force short packet */
  1230. }
  1231. /* set final frame length */
  1232. if (ctx->is_ndp16) {
  1233. nth.nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
  1234. nth.nth16->wBlockLength = cpu_to_le16(skb_out->len);
  1235. } else {
  1236. nth.nth32 = (struct usb_cdc_ncm_nth32 *)skb_out->data;
  1237. nth.nth32->dwBlockLength = cpu_to_le32(skb_out->len);
  1238. }
  1239. /* return skb */
  1240. ctx->tx_curr_skb = NULL;
  1241. /* keep private stats: framing overhead and number of NTBs */
  1242. ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
  1243. ctx->tx_ntbs++;
  1244. /* usbnet will count all the framing overhead by default.
  1245. * Adjust the stats so that the tx_bytes counter show real
  1246. * payload data instead.
  1247. */
  1248. usbnet_set_skb_tx_stats(skb_out, n,
  1249. (long)ctx->tx_curr_frame_payload - skb_out->len);
  1250. return skb_out;
  1251. alloc_failed:
  1252. if (skb) {
  1253. dev_kfree_skb_any(skb);
  1254. dev->net->stats.tx_dropped++;
  1255. }
  1256. exit_no_skb:
  1257. /* Start timer, if there is a remaining non-empty skb */
  1258. if (ctx->tx_curr_skb != NULL && n > 0)
  1259. cdc_ncm_tx_timeout_start(ctx);
  1260. return NULL;
  1261. }
  1262. EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
  1263. static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
  1264. {
  1265. /* start timer, if not already started */
  1266. if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
  1267. hrtimer_start(&ctx->tx_timer,
  1268. ctx->timer_interval,
  1269. HRTIMER_MODE_REL);
  1270. }
  1271. static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
  1272. {
  1273. struct cdc_ncm_ctx *ctx =
  1274. container_of(timer, struct cdc_ncm_ctx, tx_timer);
  1275. if (!atomic_read(&ctx->stop))
  1276. tasklet_schedule(&ctx->bh);
  1277. return HRTIMER_NORESTART;
  1278. }
  1279. static void cdc_ncm_txpath_bh(struct tasklet_struct *t)
  1280. {
  1281. struct cdc_ncm_ctx *ctx = from_tasklet(ctx, t, bh);
  1282. struct usbnet *dev = ctx->dev;
  1283. spin_lock(&ctx->mtx);
  1284. if (ctx->tx_timer_pending != 0) {
  1285. ctx->tx_timer_pending--;
  1286. cdc_ncm_tx_timeout_start(ctx);
  1287. spin_unlock(&ctx->mtx);
  1288. } else if (dev->net != NULL) {
  1289. ctx->tx_reason_timeout++; /* count reason for transmitting */
  1290. spin_unlock(&ctx->mtx);
  1291. netif_tx_lock_bh(dev->net);
  1292. usbnet_start_xmit(NULL, dev->net);
  1293. netif_tx_unlock_bh(dev->net);
  1294. } else {
  1295. spin_unlock(&ctx->mtx);
  1296. }
  1297. }
  1298. struct sk_buff *
  1299. cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  1300. {
  1301. struct sk_buff *skb_out;
  1302. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1303. /*
  1304. * The Ethernet API we are using does not support transmitting
  1305. * multiple Ethernet frames in a single call. This driver will
  1306. * accumulate multiple Ethernet frames and send out a larger
  1307. * USB frame when the USB buffer is full or when a single jiffies
  1308. * timeout happens.
  1309. */
  1310. if (ctx == NULL)
  1311. goto error;
  1312. spin_lock_bh(&ctx->mtx);
  1313. if (ctx->is_ndp16)
  1314. skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
  1315. else
  1316. skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP32_NOCRC_SIGN));
  1317. spin_unlock_bh(&ctx->mtx);
  1318. return skb_out;
  1319. error:
  1320. if (skb != NULL)
  1321. dev_kfree_skb_any(skb);
  1322. return NULL;
  1323. }
  1324. EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
  1325. /* verify NTB header and return offset of first NDP, or negative error */
  1326. int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
  1327. {
  1328. struct usbnet *dev = netdev_priv(skb_in->dev);
  1329. struct usb_cdc_ncm_nth16 *nth16;
  1330. int len;
  1331. int ret = -EINVAL;
  1332. if (ctx == NULL)
  1333. goto error;
  1334. if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
  1335. sizeof(struct usb_cdc_ncm_ndp16))) {
  1336. netif_dbg(dev, rx_err, dev->net, "frame too short\n");
  1337. goto error;
  1338. }
  1339. nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
  1340. if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
  1341. netif_dbg(dev, rx_err, dev->net,
  1342. "invalid NTH16 signature <%#010x>\n",
  1343. le32_to_cpu(nth16->dwSignature));
  1344. goto error;
  1345. }
  1346. len = le16_to_cpu(nth16->wBlockLength);
  1347. if (len > ctx->rx_max) {
  1348. netif_dbg(dev, rx_err, dev->net,
  1349. "unsupported NTB block length %u/%u\n", len,
  1350. ctx->rx_max);
  1351. goto error;
  1352. }
  1353. if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
  1354. (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
  1355. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
  1356. netif_dbg(dev, rx_err, dev->net,
  1357. "sequence number glitch prev=%d curr=%d\n",
  1358. ctx->rx_seq, le16_to_cpu(nth16->wSequence));
  1359. }
  1360. ctx->rx_seq = le16_to_cpu(nth16->wSequence);
  1361. ret = le16_to_cpu(nth16->wNdpIndex);
  1362. error:
  1363. return ret;
  1364. }
  1365. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
  1366. int cdc_ncm_rx_verify_nth32(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
  1367. {
  1368. struct usbnet *dev = netdev_priv(skb_in->dev);
  1369. struct usb_cdc_ncm_nth32 *nth32;
  1370. int len;
  1371. int ret = -EINVAL;
  1372. if (ctx == NULL)
  1373. goto error;
  1374. if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth32) +
  1375. sizeof(struct usb_cdc_ncm_ndp32))) {
  1376. netif_dbg(dev, rx_err, dev->net, "frame too short\n");
  1377. goto error;
  1378. }
  1379. nth32 = (struct usb_cdc_ncm_nth32 *)skb_in->data;
  1380. if (nth32->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH32_SIGN)) {
  1381. netif_dbg(dev, rx_err, dev->net,
  1382. "invalid NTH32 signature <%#010x>\n",
  1383. le32_to_cpu(nth32->dwSignature));
  1384. goto error;
  1385. }
  1386. len = le32_to_cpu(nth32->dwBlockLength);
  1387. if (len > ctx->rx_max) {
  1388. netif_dbg(dev, rx_err, dev->net,
  1389. "unsupported NTB block length %u/%u\n", len,
  1390. ctx->rx_max);
  1391. goto error;
  1392. }
  1393. if ((ctx->rx_seq + 1) != le16_to_cpu(nth32->wSequence) &&
  1394. (ctx->rx_seq || le16_to_cpu(nth32->wSequence)) &&
  1395. !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth32->wSequence))) {
  1396. netif_dbg(dev, rx_err, dev->net,
  1397. "sequence number glitch prev=%d curr=%d\n",
  1398. ctx->rx_seq, le16_to_cpu(nth32->wSequence));
  1399. }
  1400. ctx->rx_seq = le16_to_cpu(nth32->wSequence);
  1401. ret = le32_to_cpu(nth32->dwNdpIndex);
  1402. error:
  1403. return ret;
  1404. }
  1405. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth32);
  1406. /* verify NDP header and return number of datagrams, or negative error */
  1407. int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
  1408. {
  1409. struct usbnet *dev = netdev_priv(skb_in->dev);
  1410. struct usb_cdc_ncm_ndp16 *ndp16;
  1411. int ret = -EINVAL;
  1412. size_t ndp_len;
  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. ndp_len = struct_size_t(struct usb_cdc_ncm_ndp16, dpe16, ret);
  1429. if (ndpoffset + ndp_len > 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. size_t ndp_len;
  1444. if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp32)) > skb_in->len) {
  1445. netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
  1446. ndpoffset);
  1447. goto error;
  1448. }
  1449. ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb_in->data + ndpoffset);
  1450. if (le16_to_cpu(ndp32->wLength) < USB_CDC_NCM_NDP32_LENGTH_MIN) {
  1451. netif_dbg(dev, rx_err, dev->net, "invalid DPT32 length <%u>\n",
  1452. le16_to_cpu(ndp32->wLength));
  1453. goto error;
  1454. }
  1455. ret = ((le16_to_cpu(ndp32->wLength) -
  1456. sizeof(struct usb_cdc_ncm_ndp32)) /
  1457. sizeof(struct usb_cdc_ncm_dpe32));
  1458. ret--; /* we process NDP entries except for the last one */
  1459. ndp_len = struct_size_t(struct usb_cdc_ncm_ndp32, dpe32, ret);
  1460. if (ndpoffset + ndp_len > skb_in->len) {
  1461. netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
  1462. ret = -EINVAL;
  1463. }
  1464. error:
  1465. return ret;
  1466. }
  1467. EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp32);
  1468. int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
  1469. {
  1470. struct sk_buff *skb;
  1471. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1472. unsigned int len;
  1473. int nframes;
  1474. int x;
  1475. unsigned int offset;
  1476. union {
  1477. struct usb_cdc_ncm_ndp16 *ndp16;
  1478. struct usb_cdc_ncm_ndp32 *ndp32;
  1479. } ndp;
  1480. union {
  1481. struct usb_cdc_ncm_dpe16 *dpe16;
  1482. struct usb_cdc_ncm_dpe32 *dpe32;
  1483. } dpe;
  1484. int ndpoffset;
  1485. int loopcount = 50; /* arbitrary max preventing infinite loop */
  1486. u32 payload = 0;
  1487. if (ctx->is_ndp16)
  1488. ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
  1489. else
  1490. ndpoffset = cdc_ncm_rx_verify_nth32(ctx, skb_in);
  1491. if (ndpoffset < 0)
  1492. goto error;
  1493. next_ndp:
  1494. if (ctx->is_ndp16) {
  1495. nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
  1496. if (nframes < 0)
  1497. goto error;
  1498. ndp.ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
  1499. if (ndp.ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
  1500. netif_dbg(dev, rx_err, dev->net,
  1501. "invalid DPT16 signature <%#010x>\n",
  1502. le32_to_cpu(ndp.ndp16->dwSignature));
  1503. goto err_ndp;
  1504. }
  1505. dpe.dpe16 = ndp.ndp16->dpe16;
  1506. } else {
  1507. nframes = cdc_ncm_rx_verify_ndp32(skb_in, ndpoffset);
  1508. if (nframes < 0)
  1509. goto error;
  1510. ndp.ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb_in->data + ndpoffset);
  1511. if (ndp.ndp32->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP32_NOCRC_SIGN)) {
  1512. netif_dbg(dev, rx_err, dev->net,
  1513. "invalid DPT32 signature <%#010x>\n",
  1514. le32_to_cpu(ndp.ndp32->dwSignature));
  1515. goto err_ndp;
  1516. }
  1517. dpe.dpe32 = ndp.ndp32->dpe32;
  1518. }
  1519. for (x = 0; x < nframes; x++) {
  1520. if (ctx->is_ndp16) {
  1521. offset = le16_to_cpu(dpe.dpe16->wDatagramIndex);
  1522. len = le16_to_cpu(dpe.dpe16->wDatagramLength);
  1523. } else {
  1524. offset = le32_to_cpu(dpe.dpe32->dwDatagramIndex);
  1525. len = le32_to_cpu(dpe.dpe32->dwDatagramLength);
  1526. }
  1527. /*
  1528. * CDC NCM ch. 3.7
  1529. * All entries after first NULL entry are to be ignored
  1530. */
  1531. if ((offset == 0) || (len == 0)) {
  1532. if (!x)
  1533. goto err_ndp; /* empty NTB */
  1534. break;
  1535. }
  1536. /* sanity checking - watch out for integer wrap*/
  1537. if ((offset > skb_in->len) || (len > skb_in->len - offset) ||
  1538. (len > ctx->rx_max) || (len < ETH_HLEN)) {
  1539. netif_dbg(dev, rx_err, dev->net,
  1540. "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
  1541. x, offset, len, skb_in);
  1542. if (!x)
  1543. goto err_ndp;
  1544. break;
  1545. } else {
  1546. /* create a fresh copy to reduce truesize */
  1547. skb = netdev_alloc_skb_ip_align(dev->net, len);
  1548. if (!skb)
  1549. goto error;
  1550. skb_put_data(skb, skb_in->data + offset, len);
  1551. usbnet_skb_return(dev, skb);
  1552. payload += len; /* count payload bytes in this NTB */
  1553. }
  1554. if (ctx->is_ndp16)
  1555. dpe.dpe16++;
  1556. else
  1557. dpe.dpe32++;
  1558. }
  1559. err_ndp:
  1560. /* are there more NDPs to process? */
  1561. if (ctx->is_ndp16)
  1562. ndpoffset = le16_to_cpu(ndp.ndp16->wNextNdpIndex);
  1563. else
  1564. ndpoffset = le32_to_cpu(ndp.ndp32->dwNextNdpIndex);
  1565. if (ndpoffset && loopcount--)
  1566. goto next_ndp;
  1567. /* update stats */
  1568. ctx->rx_overhead += skb_in->len - payload;
  1569. ctx->rx_ntbs++;
  1570. return 1;
  1571. error:
  1572. return 0;
  1573. }
  1574. EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
  1575. static void
  1576. cdc_ncm_speed_change(struct usbnet *dev,
  1577. struct usb_cdc_speed_change *data)
  1578. {
  1579. /* RTL8156 shipped before 2021 sends notification about every 32ms. */
  1580. dev->rx_speed = le32_to_cpu(data->DLBitRRate);
  1581. dev->tx_speed = le32_to_cpu(data->ULBitRate);
  1582. }
  1583. static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
  1584. {
  1585. struct usb_cdc_notification *event;
  1586. if (urb->actual_length < sizeof(*event))
  1587. return;
  1588. /* test for split data in 8-byte chunks */
  1589. if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
  1590. cdc_ncm_speed_change(dev,
  1591. (struct usb_cdc_speed_change *)urb->transfer_buffer);
  1592. return;
  1593. }
  1594. event = urb->transfer_buffer;
  1595. switch (event->bNotificationType) {
  1596. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  1597. /*
  1598. * According to the CDC NCM specification ch.7.1
  1599. * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
  1600. * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
  1601. */
  1602. /* RTL8156 shipped before 2021 sends notification about
  1603. * every 32ms. Don't forward notification if state is same.
  1604. */
  1605. if (netif_carrier_ok(dev->net) != !!event->wValue)
  1606. usbnet_link_change(dev, !!event->wValue, 0);
  1607. break;
  1608. case USB_CDC_NOTIFY_SPEED_CHANGE:
  1609. if (urb->actual_length < (sizeof(*event) +
  1610. sizeof(struct usb_cdc_speed_change)))
  1611. set_bit(EVENT_STS_SPLIT, &dev->flags);
  1612. else
  1613. cdc_ncm_speed_change(dev,
  1614. (struct usb_cdc_speed_change *)&event[1]);
  1615. break;
  1616. default:
  1617. dev_dbg(&dev->udev->dev,
  1618. "NCM: unexpected notification 0x%02x!\n",
  1619. event->bNotificationType);
  1620. break;
  1621. }
  1622. }
  1623. static void cdc_ncm_update_filter(struct usbnet *dev)
  1624. {
  1625. struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
  1626. if (ctx->filtering_supported)
  1627. usbnet_cdc_update_filter(dev);
  1628. }
  1629. static const struct driver_info cdc_ncm_info = {
  1630. .description = "CDC NCM (NO ZLP)",
  1631. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1632. | FLAG_LINK_INTR | FLAG_ETHER,
  1633. .bind = cdc_ncm_bind,
  1634. .unbind = cdc_ncm_unbind,
  1635. .manage_power = usbnet_manage_power,
  1636. .status = cdc_ncm_status,
  1637. .rx_fixup = cdc_ncm_rx_fixup,
  1638. .tx_fixup = cdc_ncm_tx_fixup,
  1639. .set_rx_mode = cdc_ncm_update_filter,
  1640. };
  1641. /* Same as cdc_ncm_info, but with FLAG_SEND_ZLP */
  1642. static const struct driver_info cdc_ncm_zlp_info = {
  1643. .description = "CDC NCM (SEND ZLP)",
  1644. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1645. | FLAG_LINK_INTR | FLAG_ETHER | FLAG_SEND_ZLP,
  1646. .bind = cdc_ncm_bind,
  1647. .unbind = cdc_ncm_unbind,
  1648. .manage_power = usbnet_manage_power,
  1649. .status = cdc_ncm_status,
  1650. .rx_fixup = cdc_ncm_rx_fixup,
  1651. .tx_fixup = cdc_ncm_tx_fixup,
  1652. .set_rx_mode = cdc_ncm_update_filter,
  1653. };
  1654. /* Same as cdc_ncm_info, but with FLAG_SEND_ZLP */
  1655. static const struct driver_info apple_tethering_interface_info = {
  1656. .description = "CDC NCM (Apple Tethering)",
  1657. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1658. | FLAG_LINK_INTR | FLAG_ETHER | FLAG_SEND_ZLP,
  1659. .bind = cdc_ncm_bind,
  1660. .unbind = cdc_ncm_unbind,
  1661. .manage_power = usbnet_manage_power,
  1662. .status = cdc_ncm_status,
  1663. .rx_fixup = cdc_ncm_rx_fixup,
  1664. .tx_fixup = cdc_ncm_tx_fixup,
  1665. .set_rx_mode = usbnet_cdc_update_filter,
  1666. };
  1667. /* Same as apple_tethering_interface_info, but without FLAG_LINK_INTR */
  1668. static const struct driver_info apple_private_interface_info = {
  1669. .description = "CDC NCM (Apple Private)",
  1670. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1671. | FLAG_ETHER | FLAG_SEND_ZLP,
  1672. .bind = cdc_ncm_bind,
  1673. .unbind = cdc_ncm_unbind,
  1674. .manage_power = usbnet_manage_power,
  1675. .status = cdc_ncm_status,
  1676. .rx_fixup = cdc_ncm_rx_fixup,
  1677. .tx_fixup = cdc_ncm_tx_fixup,
  1678. .set_rx_mode = usbnet_cdc_update_filter,
  1679. };
  1680. /* Same as cdc_ncm_info, but with FLAG_WWAN */
  1681. static const struct driver_info wwan_info = {
  1682. .description = "Mobile Broadband Network Device",
  1683. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1684. | FLAG_LINK_INTR | FLAG_WWAN,
  1685. .bind = cdc_ncm_bind,
  1686. .unbind = cdc_ncm_unbind,
  1687. .manage_power = usbnet_manage_power,
  1688. .status = cdc_ncm_status,
  1689. .rx_fixup = cdc_ncm_rx_fixup,
  1690. .tx_fixup = cdc_ncm_tx_fixup,
  1691. .set_rx_mode = cdc_ncm_update_filter,
  1692. };
  1693. /* Same as wwan_info, but with FLAG_NOARP */
  1694. static const struct driver_info wwan_noarp_info = {
  1695. .description = "Mobile Broadband Network Device (NO ARP)",
  1696. .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
  1697. | FLAG_LINK_INTR | FLAG_WWAN | FLAG_NOARP,
  1698. .bind = cdc_ncm_bind,
  1699. .unbind = cdc_ncm_unbind,
  1700. .manage_power = usbnet_manage_power,
  1701. .status = cdc_ncm_status,
  1702. .rx_fixup = cdc_ncm_rx_fixup,
  1703. .tx_fixup = cdc_ncm_tx_fixup,
  1704. .set_rx_mode = cdc_ncm_update_filter,
  1705. };
  1706. static const struct usb_device_id cdc_devs[] = {
  1707. /* iPhone */
  1708. { USB_DEVICE_INTERFACE_NUMBER(0x05ac, 0x12a8, 2),
  1709. .driver_info = (unsigned long)&apple_tethering_interface_info,
  1710. },
  1711. { USB_DEVICE_INTERFACE_NUMBER(0x05ac, 0x12a8, 4),
  1712. .driver_info = (unsigned long)&apple_private_interface_info,
  1713. },
  1714. /* iPad */
  1715. { USB_DEVICE_INTERFACE_NUMBER(0x05ac, 0x12ab, 2),
  1716. .driver_info = (unsigned long)&apple_tethering_interface_info,
  1717. },
  1718. { USB_DEVICE_INTERFACE_NUMBER(0x05ac, 0x12ab, 4),
  1719. .driver_info = (unsigned long)&apple_private_interface_info,
  1720. },
  1721. /* Ericsson MBM devices like F5521gw */
  1722. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1723. | USB_DEVICE_ID_MATCH_VENDOR,
  1724. .idVendor = 0x0bdb,
  1725. .bInterfaceClass = USB_CLASS_COMM,
  1726. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1727. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1728. .driver_info = (unsigned long) &wwan_info,
  1729. },
  1730. /* Telit LE910 V2 */
  1731. { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x0036,
  1732. USB_CLASS_COMM,
  1733. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1734. .driver_info = (unsigned long)&wwan_noarp_info,
  1735. },
  1736. /* DW5812 LTE Verizon Mobile Broadband Card
  1737. * Unlike DW5550 this device requires FLAG_NOARP
  1738. */
  1739. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bb,
  1740. USB_CLASS_COMM,
  1741. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1742. .driver_info = (unsigned long)&wwan_noarp_info,
  1743. },
  1744. /* DW5813 LTE AT&T Mobile Broadband Card
  1745. * Unlike DW5550 this device requires FLAG_NOARP
  1746. */
  1747. { USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bc,
  1748. USB_CLASS_COMM,
  1749. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1750. .driver_info = (unsigned long)&wwan_noarp_info,
  1751. },
  1752. /* Dell branded MBM devices like DW5550 */
  1753. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1754. | USB_DEVICE_ID_MATCH_VENDOR,
  1755. .idVendor = 0x413c,
  1756. .bInterfaceClass = USB_CLASS_COMM,
  1757. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1758. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1759. .driver_info = (unsigned long) &wwan_info,
  1760. },
  1761. /* Toshiba branded MBM devices */
  1762. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1763. | USB_DEVICE_ID_MATCH_VENDOR,
  1764. .idVendor = 0x0930,
  1765. .bInterfaceClass = USB_CLASS_COMM,
  1766. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1767. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1768. .driver_info = (unsigned long) &wwan_info,
  1769. },
  1770. /* tag Huawei devices as wwan */
  1771. { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
  1772. USB_CLASS_COMM,
  1773. USB_CDC_SUBCLASS_NCM,
  1774. USB_CDC_PROTO_NONE),
  1775. .driver_info = (unsigned long)&wwan_info,
  1776. },
  1777. /* Infineon(now Intel) HSPA Modem platform */
  1778. { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
  1779. USB_CLASS_COMM,
  1780. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1781. .driver_info = (unsigned long)&wwan_noarp_info,
  1782. },
  1783. /* u-blox TOBY-L4 */
  1784. { USB_DEVICE_AND_INTERFACE_INFO(0x1546, 0x1010,
  1785. USB_CLASS_COMM,
  1786. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1787. .driver_info = (unsigned long)&wwan_info,
  1788. },
  1789. /* Intel modem (label from OEM reads Fibocom L850-GL) */
  1790. { USB_DEVICE_AND_INTERFACE_INFO(0x8087, 0x095a,
  1791. USB_CLASS_COMM,
  1792. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1793. .driver_info = (unsigned long)&wwan_info,
  1794. },
  1795. /* DisplayLink docking stations */
  1796. { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  1797. | USB_DEVICE_ID_MATCH_VENDOR,
  1798. .idVendor = 0x17e9,
  1799. .bInterfaceClass = USB_CLASS_COMM,
  1800. .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
  1801. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  1802. .driver_info = (unsigned long)&cdc_ncm_zlp_info,
  1803. },
  1804. /* Generic CDC-NCM devices */
  1805. { USB_INTERFACE_INFO(USB_CLASS_COMM,
  1806. USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
  1807. .driver_info = (unsigned long)&cdc_ncm_info,
  1808. },
  1809. {
  1810. },
  1811. };
  1812. MODULE_DEVICE_TABLE(usb, cdc_devs);
  1813. static struct usb_driver cdc_ncm_driver = {
  1814. .name = "cdc_ncm",
  1815. .id_table = cdc_devs,
  1816. .probe = usbnet_probe,
  1817. .disconnect = usbnet_disconnect,
  1818. .suspend = usbnet_suspend,
  1819. .resume = usbnet_resume,
  1820. .reset_resume = usbnet_resume,
  1821. .supports_autosuspend = 1,
  1822. .disable_hub_initiated_lpm = 1,
  1823. };
  1824. module_usb_driver(cdc_ncm_driver);
  1825. MODULE_AUTHOR("Hans Petter Selasky");
  1826. MODULE_DESCRIPTION("USB CDC NCM host driver");
  1827. MODULE_LICENSE("Dual BSD/GPL");