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