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