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