cdc_ncm.c 48 KB

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