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