cdc_ncm.c 61 KB

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