aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2013-11-02 02:02:19 -0400
committerDavid S. Miller <davem@davemloft.net>2013-11-02 02:02:19 -0400
commitf99f4f8b094065601f87953b6d372d1e082bdf8b (patch)
treeb5bd37a8edb1e3ff5f0c261daddd8b3362824d9f
parentc1a2e95435976cb040db1f6c0afbe4287c3d2e28 (diff)
parenta6fe67087d7cb916e41b4ad1b3a57c91150edb88 (diff)
Merge branch 'cdc_ncm'
Bjørn Mork says: ==================== cdc_ncm: many small and mostly trivial fixes This series ended up longer than expected, and it is still not complete. There is more to come when time allows... Most changes are trivial. Notable non-trivial changes are - removed filtering of identical speed notifications - tx_max calulation is changed to count the pad byte if necessary, and respect the device limit as an absolute upper limit even if it is too low according to the spec - remove the bug preventing SET_MAX_DATAGRAM_SIZE from having any effect - drop the pad-to-max if ZLPs are enabled - the driver specific VERSION is dropped - dev->hard_mtu is set to tx_max instead of max_datagram_size causing usbnet to calculate the qlen based on the real max size of tx skbs This series has been tested, along with the previously posted cdc_mbim series, on the NCM and MBIM devices I have: - Ericsson F5521gw (NCM) - Huawei E367 (MBIM) - D-Link DWM-156 A7 (MBIM w/ too low dwNtb{In,Out}MaxSize bug) - Sierra Wireless MC7710 (MBIM w/ ZLP and CDC Union bugs) Apart from the D-Link modem dropping a lot less oversized frames with the fix dedicated to it, there are no end user noticable functional changes as a result of this series. But all the non-trivial changes I listed above are of course detectable by users looking at that specific area (except maybe the removed speed notification, which requires a device sending duplicates to be noticable - I don't have any such device). ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/usb/cdc_mbim.c2
-rw-r--r--drivers/net/usb/cdc_ncm.c490
-rw-r--r--include/linux/usb/cdc_ncm.h30
3 files changed, 233 insertions, 289 deletions
diff --git a/drivers/net/usb/cdc_mbim.c b/drivers/net/usb/cdc_mbim.c
index 253472a8a983..af76aaf08b6b 100644
--- a/drivers/net/usb/cdc_mbim.c
+++ b/drivers/net/usb/cdc_mbim.c
@@ -175,7 +175,7 @@ static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb
175 } 175 }
176 176
177 spin_lock_bh(&ctx->mtx); 177 spin_lock_bh(&ctx->mtx);
178 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign); 178 skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
179 spin_unlock_bh(&ctx->mtx); 179 spin_unlock_bh(&ctx->mtx);
180 return skb_out; 180 return skb_out;
181 181
diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
index 43afde8f48d2..11c703337577 100644
--- a/drivers/net/usb/cdc_ncm.c
+++ b/drivers/net/usb/cdc_ncm.c
@@ -53,8 +53,6 @@
53#include <linux/usb/cdc.h> 53#include <linux/usb/cdc.h>
54#include <linux/usb/cdc_ncm.h> 54#include <linux/usb/cdc_ncm.h>
55 55
56#define DRIVER_VERSION "14-Mar-2012"
57
58#if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM) 56#if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
59static bool prefer_mbim = true; 57static bool prefer_mbim = true;
60#else 58#else
@@ -68,71 +66,67 @@ static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
68static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer); 66static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
69static struct usb_driver cdc_ncm_driver; 67static struct usb_driver cdc_ncm_driver;
70 68
71static void 69static u8 cdc_ncm_setup(struct usbnet *dev)
72cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
73{
74 struct usbnet *dev = netdev_priv(net);
75
76 strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
77 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
78 strlcpy(info->fw_version, dev->driver_info->description,
79 sizeof(info->fw_version));
80 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
81}
82
83static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
84{ 70{
71 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
72 struct usb_cdc_ncm_ntb_parameters ncm_parm;
85 u32 val; 73 u32 val;
86 u8 flags; 74 u8 flags;
87 u8 iface_no; 75 u8 iface_no;
88 int err; 76 int err;
89 int eth_hlen; 77 int eth_hlen;
90 u16 ntb_fmt_supported; 78 u16 ntb_fmt_supported;
91 u32 min_dgram_size; 79 __le16 max_datagram_size;
92 u32 min_hdr_size;
93 struct usbnet *dev = netdev_priv(ctx->netdev);
94 80
95 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber; 81 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
96 82
97 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS, 83 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
98 USB_TYPE_CLASS | USB_DIR_IN 84 USB_TYPE_CLASS | USB_DIR_IN
99 |USB_RECIP_INTERFACE, 85 |USB_RECIP_INTERFACE,
100 0, iface_no, &ctx->ncm_parm, 86 0, iface_no, &ncm_parm,
101 sizeof(ctx->ncm_parm)); 87 sizeof(ncm_parm));
102 if (err < 0) { 88 if (err < 0) {
103 pr_debug("failed GET_NTB_PARAMETERS\n"); 89 dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
104 return 1; 90 return err; /* GET_NTB_PARAMETERS is required */
105 } 91 }
106 92
107 /* read correct set of parameters according to device mode */ 93 /* read correct set of parameters according to device mode */
108 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize); 94 ctx->rx_max = le32_to_cpu(ncm_parm.dwNtbInMaxSize);
109 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize); 95 ctx->tx_max = le32_to_cpu(ncm_parm.dwNtbOutMaxSize);
110 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder); 96 ctx->tx_remainder = le16_to_cpu(ncm_parm.wNdpOutPayloadRemainder);
111 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor); 97 ctx->tx_modulus = le16_to_cpu(ncm_parm.wNdpOutDivisor);
112 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment); 98 ctx->tx_ndp_modulus = le16_to_cpu(ncm_parm.wNdpOutAlignment);
113 /* devices prior to NCM Errata shall set this field to zero */ 99 /* devices prior to NCM Errata shall set this field to zero */
114 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams); 100 ctx->tx_max_datagrams = le16_to_cpu(ncm_parm.wNtbOutMaxDatagrams);
115 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported); 101 ntb_fmt_supported = le16_to_cpu(ncm_parm.bmNtbFormatsSupported);
116 102
117 eth_hlen = ETH_HLEN; 103 /* there are some minor differences in NCM and MBIM defaults */
118 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE; 104 if (cdc_ncm_comm_intf_is_mbim(ctx->control->cur_altsetting)) {
119 min_hdr_size = CDC_NCM_MIN_HDR_SIZE; 105 if (!ctx->mbim_desc)
120 if (ctx->mbim_desc != NULL) { 106 return -EINVAL;
121 flags = ctx->mbim_desc->bmNetworkCapabilities;
122 eth_hlen = 0; 107 eth_hlen = 0;
123 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE; 108 flags = ctx->mbim_desc->bmNetworkCapabilities;
124 min_hdr_size = 0; 109 ctx->max_datagram_size = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
125 } else if (ctx->func_desc != NULL) { 110 if (ctx->max_datagram_size < CDC_MBIM_MIN_DATAGRAM_SIZE)
126 flags = ctx->func_desc->bmNetworkCapabilities; 111 ctx->max_datagram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
127 } else { 112 } else {
128 flags = 0; 113 if (!ctx->func_desc)
114 return -EINVAL;
115 eth_hlen = ETH_HLEN;
116 flags = ctx->func_desc->bmNetworkCapabilities;
117 ctx->max_datagram_size = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
118 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
119 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
129 } 120 }
130 121
131 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u " 122 /* common absolute max for NCM and MBIM */
132 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u " 123 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
133 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n", 124 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
134 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus, 125
135 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags); 126 dev_dbg(&dev->intf->dev,
127 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
128 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
129 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
136 130
137 /* max count of tx datagrams */ 131 /* max count of tx datagrams */
138 if ((ctx->tx_max_datagrams == 0) || 132 if ((ctx->tx_max_datagrams == 0) ||
@@ -141,19 +135,19 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
141 135
142 /* verify maximum size of received NTB in bytes */ 136 /* verify maximum size of received NTB in bytes */
143 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) { 137 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
144 pr_debug("Using min receive length=%d\n", 138 dev_dbg(&dev->intf->dev, "Using min receive length=%d\n",
145 USB_CDC_NCM_NTB_MIN_IN_SIZE); 139 USB_CDC_NCM_NTB_MIN_IN_SIZE);
146 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE; 140 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
147 } 141 }
148 142
149 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) { 143 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
150 pr_debug("Using default maximum receive length=%d\n", 144 dev_dbg(&dev->intf->dev, "Using default maximum receive length=%d\n",
151 CDC_NCM_NTB_MAX_SIZE_RX); 145 CDC_NCM_NTB_MAX_SIZE_RX);
152 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX; 146 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
153 } 147 }
154 148
155 /* inform device about NTB input size changes */ 149 /* inform device about NTB input size changes */
156 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) { 150 if (ctx->rx_max != le32_to_cpu(ncm_parm.dwNtbInMaxSize)) {
157 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max); 151 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
158 152
159 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE, 153 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
@@ -161,16 +155,22 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
161 | USB_RECIP_INTERFACE, 155 | USB_RECIP_INTERFACE,
162 0, iface_no, &dwNtbInMaxSize, 4); 156 0, iface_no, &dwNtbInMaxSize, 4);
163 if (err < 0) 157 if (err < 0)
164 pr_debug("Setting NTB Input Size failed\n"); 158 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
165 } 159 }
166 160
167 /* verify maximum size of transmitted NTB in bytes */ 161 /* verify maximum size of transmitted NTB in bytes */
168 if ((ctx->tx_max < 162 if (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX) {
169 (min_hdr_size + min_dgram_size)) || 163 dev_dbg(&dev->intf->dev, "Using default maximum transmit length=%d\n",
170 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) { 164 CDC_NCM_NTB_MAX_SIZE_TX);
171 pr_debug("Using default maximum transmit length=%d\n",
172 CDC_NCM_NTB_MAX_SIZE_TX);
173 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX; 165 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
166
167 /* Adding a pad byte here simplifies the handling in
168 * cdc_ncm_fill_tx_frame, by making tx_max always
169 * represent the real skb max size.
170 */
171 if (ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
172 ctx->tx_max++;
173
174 } 174 }
175 175
176 /* 176 /*
@@ -183,7 +183,7 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
183 183
184 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || 184 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
185 (val != ((-val) & val)) || (val >= ctx->tx_max)) { 185 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
186 pr_debug("Using default alignment: 4 bytes\n"); 186 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
187 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; 187 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
188 } 188 }
189 189
@@ -197,13 +197,13 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
197 197
198 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) || 198 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
199 (val != ((-val) & val)) || (val >= ctx->tx_max)) { 199 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
200 pr_debug("Using default transmit modulus: 4 bytes\n"); 200 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
201 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE; 201 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
202 } 202 }
203 203
204 /* verify the payload remainder */ 204 /* verify the payload remainder */
205 if (ctx->tx_remainder >= ctx->tx_modulus) { 205 if (ctx->tx_remainder >= ctx->tx_modulus) {
206 pr_debug("Using default transmit remainder: 0 bytes\n"); 206 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
207 ctx->tx_remainder = 0; 207 ctx->tx_remainder = 0;
208 } 208 }
209 209
@@ -221,7 +221,7 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
221 USB_CDC_NCM_CRC_NOT_APPENDED, 221 USB_CDC_NCM_CRC_NOT_APPENDED,
222 iface_no, NULL, 0); 222 iface_no, NULL, 0);
223 if (err < 0) 223 if (err < 0)
224 pr_debug("Setting CRC mode off failed\n"); 224 dev_dbg(&dev->intf->dev, "Setting CRC mode off failed\n");
225 } 225 }
226 226
227 /* set NTB format, if both formats are supported */ 227 /* set NTB format, if both formats are supported */
@@ -232,69 +232,43 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
232 USB_CDC_NCM_NTB16_FORMAT, 232 USB_CDC_NCM_NTB16_FORMAT,
233 iface_no, NULL, 0); 233 iface_no, NULL, 0);
234 if (err < 0) 234 if (err < 0)
235 pr_debug("Setting NTB format to 16-bit failed\n"); 235 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit failed\n");
236 } 236 }
237 237
238 ctx->max_datagram_size = min_dgram_size; 238 /* inform the device about the selected Max Datagram Size */
239 if (!(flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
240 goto out;
239 241
240 /* set Max Datagram Size (MTU) */ 242 /* read current mtu value from device */
241 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) { 243 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
242 __le16 max_datagram_size; 244 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
243 u16 eth_max_sz; 245 0, iface_no, &max_datagram_size, 2);
244 if (ctx->ether_desc != NULL) 246 if (err < 0) {
245 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize); 247 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
246 else if (ctx->mbim_desc != NULL) 248 goto out;
247 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
248 else
249 goto max_dgram_err;
250
251 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
252 USB_TYPE_CLASS | USB_DIR_IN
253 | USB_RECIP_INTERFACE,
254 0, iface_no, &max_datagram_size, 2);
255 if (err < 0) {
256 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
257 min_dgram_size);
258 } else {
259 ctx->max_datagram_size =
260 le16_to_cpu(max_datagram_size);
261 /* Check Eth descriptor value */
262 if (ctx->max_datagram_size > eth_max_sz)
263 ctx->max_datagram_size = eth_max_sz;
264
265 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
266 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
267
268 if (ctx->max_datagram_size < min_dgram_size)
269 ctx->max_datagram_size = min_dgram_size;
270
271 /* if value changed, update device */
272 if (ctx->max_datagram_size !=
273 le16_to_cpu(max_datagram_size)) {
274 err = usbnet_write_cmd(dev,
275 USB_CDC_SET_MAX_DATAGRAM_SIZE,
276 USB_TYPE_CLASS | USB_DIR_OUT
277 | USB_RECIP_INTERFACE,
278 0,
279 iface_no, &max_datagram_size,
280 2);
281 if (err < 0)
282 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
283 }
284 }
285 } 249 }
286 250
287max_dgram_err: 251 if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
288 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen)) 252 goto out;
289 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
290 253
254 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
255 err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
256 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
257 0, iface_no, &max_datagram_size, 2);
258 if (err < 0)
259 dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
260
261out:
262 /* set MTU to max supported by the device if necessary */
263 if (dev->net->mtu > ctx->max_datagram_size - eth_hlen)
264 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
291 return 0; 265 return 0;
292} 266}
293 267
294static void 268static void
295cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf) 269cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
296{ 270{
297 struct usb_host_endpoint *e; 271 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
298 u8 ep; 272 u8 ep;
299 273
300 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) { 274 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
@@ -303,18 +277,18 @@ cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
303 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 277 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
304 case USB_ENDPOINT_XFER_INT: 278 case USB_ENDPOINT_XFER_INT:
305 if (usb_endpoint_dir_in(&e->desc)) { 279 if (usb_endpoint_dir_in(&e->desc)) {
306 if (ctx->status_ep == NULL) 280 if (!dev->status)
307 ctx->status_ep = e; 281 dev->status = e;
308 } 282 }
309 break; 283 break;
310 284
311 case USB_ENDPOINT_XFER_BULK: 285 case USB_ENDPOINT_XFER_BULK:
312 if (usb_endpoint_dir_in(&e->desc)) { 286 if (usb_endpoint_dir_in(&e->desc)) {
313 if (ctx->in_ep == NULL) 287 if (!in)
314 ctx->in_ep = e; 288 in = e;
315 } else { 289 } else {
316 if (ctx->out_ep == NULL) 290 if (!out)
317 ctx->out_ep = e; 291 out = e;
318 } 292 }
319 break; 293 break;
320 294
@@ -322,6 +296,14 @@ cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
322 break; 296 break;
323 } 297 }
324 } 298 }
299 if (in && !dev->in)
300 dev->in = usb_rcvbulkpipe(dev->udev,
301 in->desc.bEndpointAddress &
302 USB_ENDPOINT_NUMBER_MASK);
303 if (out && !dev->out)
304 dev->out = usb_sndbulkpipe(dev->udev,
305 out->desc.bEndpointAddress &
306 USB_ENDPOINT_NUMBER_MASK);
325} 307}
326 308
327static void cdc_ncm_free(struct cdc_ncm_ctx *ctx) 309static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
@@ -342,18 +324,9 @@ static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
342 kfree(ctx); 324 kfree(ctx);
343} 325}
344 326
345static const struct ethtool_ops cdc_ncm_ethtool_ops = {
346 .get_drvinfo = cdc_ncm_get_drvinfo,
347 .get_link = usbnet_get_link,
348 .get_msglevel = usbnet_get_msglevel,
349 .set_msglevel = usbnet_set_msglevel,
350 .get_settings = usbnet_get_settings,
351 .set_settings = usbnet_set_settings,
352 .nway_reset = usbnet_nway_reset,
353};
354
355int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting) 327int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
356{ 328{
329 const struct usb_cdc_union_desc *union_desc = NULL;
357 struct cdc_ncm_ctx *ctx; 330 struct cdc_ncm_ctx *ctx;
358 struct usb_driver *driver; 331 struct usb_driver *driver;
359 u8 *buf; 332 u8 *buf;
@@ -367,23 +340,22 @@ int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_
367 340
368 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 341 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
369 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb; 342 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
370 ctx->bh.data = (unsigned long)ctx; 343 ctx->bh.data = (unsigned long)dev;
371 ctx->bh.func = cdc_ncm_txpath_bh; 344 ctx->bh.func = cdc_ncm_txpath_bh;
372 atomic_set(&ctx->stop, 0); 345 atomic_set(&ctx->stop, 0);
373 spin_lock_init(&ctx->mtx); 346 spin_lock_init(&ctx->mtx);
374 ctx->netdev = dev->net;
375 347
376 /* store ctx pointer in device data field */ 348 /* store ctx pointer in device data field */
377 dev->data[0] = (unsigned long)ctx; 349 dev->data[0] = (unsigned long)ctx;
378 350
351 /* only the control interface can be successfully probed */
352 ctx->control = intf;
353
379 /* get some pointers */ 354 /* get some pointers */
380 driver = driver_of(intf); 355 driver = driver_of(intf);
381 buf = intf->cur_altsetting->extra; 356 buf = intf->cur_altsetting->extra;
382 len = intf->cur_altsetting->extralen; 357 len = intf->cur_altsetting->extralen;
383 358
384 ctx->udev = dev->udev;
385 ctx->intf = intf;
386
387 /* parse through descriptors associated with control interface */ 359 /* parse through descriptors associated with control interface */
388 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) { 360 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
389 361
@@ -392,16 +364,18 @@ int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_
392 364
393 switch (buf[2]) { 365 switch (buf[2]) {
394 case USB_CDC_UNION_TYPE: 366 case USB_CDC_UNION_TYPE:
395 if (buf[0] < sizeof(*(ctx->union_desc))) 367 if (buf[0] < sizeof(*union_desc))
396 break; 368 break;
397 369
398 ctx->union_desc = 370 union_desc = (const struct usb_cdc_union_desc *)buf;
399 (const struct usb_cdc_union_desc *)buf; 371 /* the master must be the interface we are probing */
400 372 if (intf->cur_altsetting->desc.bInterfaceNumber !=
401 ctx->control = usb_ifnum_to_if(dev->udev, 373 union_desc->bMasterInterface0) {
402 ctx->union_desc->bMasterInterface0); 374 dev_dbg(&intf->dev, "bogus CDC Union\n");
375 goto error;
376 }
403 ctx->data = usb_ifnum_to_if(dev->udev, 377 ctx->data = usb_ifnum_to_if(dev->udev,
404 ctx->union_desc->bSlaveInterface0); 378 union_desc->bSlaveInterface0);
405 break; 379 break;
406 380
407 case USB_CDC_ETHERNET_TYPE: 381 case USB_CDC_ETHERNET_TYPE:
@@ -410,13 +384,6 @@ int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_
410 384
411 ctx->ether_desc = 385 ctx->ether_desc =
412 (const struct usb_cdc_ether_desc *)buf; 386 (const struct usb_cdc_ether_desc *)buf;
413 dev->hard_mtu =
414 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
415
416 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
417 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
418 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
419 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
420 break; 387 break;
421 388
422 case USB_CDC_NCM_TYPE: 389 case USB_CDC_NCM_TYPE:
@@ -444,69 +411,71 @@ advance:
444 } 411 }
445 412
446 /* some buggy devices have an IAD but no CDC Union */ 413 /* some buggy devices have an IAD but no CDC Union */
447 if (!ctx->union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) { 414 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
448 ctx->control = intf;
449 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1); 415 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
450 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n"); 416 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
451 } 417 }
452 418
453 /* check if we got everything */ 419 /* check if we got everything */
454 if ((ctx->control == NULL) || (ctx->data == NULL) || 420 if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc)) {
455 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf)))) 421 dev_dbg(&intf->dev, "CDC descriptors missing\n");
456 goto error; 422 goto error;
423 }
457 424
458 /* claim data interface, if different from control */ 425 /* claim data interface, if different from control */
459 if (ctx->data != ctx->control) { 426 if (ctx->data != ctx->control) {
460 temp = usb_driver_claim_interface(driver, ctx->data, dev); 427 temp = usb_driver_claim_interface(driver, ctx->data, dev);
461 if (temp) 428 if (temp) {
429 dev_dbg(&intf->dev, "failed to claim data intf\n");
462 goto error; 430 goto error;
431 }
463 } 432 }
464 433
465 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber; 434 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
466 435
467 /* reset data interface */ 436 /* reset data interface */
468 temp = usb_set_interface(dev->udev, iface_no, 0); 437 temp = usb_set_interface(dev->udev, iface_no, 0);
469 if (temp) 438 if (temp) {
470 goto error2; 439 dev_dbg(&intf->dev, "set interface failed\n");
471
472 /* initialize data interface */
473 if (cdc_ncm_setup(ctx))
474 goto error2; 440 goto error2;
441 }
475 442
476 /* configure data interface */ 443 /* configure data interface */
477 temp = usb_set_interface(dev->udev, iface_no, data_altsetting); 444 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
478 if (temp) 445 if (temp) {
446 dev_dbg(&intf->dev, "set interface failed\n");
479 goto error2; 447 goto error2;
448 }
480 449
481 cdc_ncm_find_endpoints(ctx, ctx->data); 450 cdc_ncm_find_endpoints(dev, ctx->data);
482 cdc_ncm_find_endpoints(ctx, ctx->control); 451 cdc_ncm_find_endpoints(dev, ctx->control);
483 452 if (!dev->in || !dev->out || !dev->status) {
484 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) || 453 dev_dbg(&intf->dev, "failed to collect endpoints\n");
485 (ctx->status_ep == NULL))
486 goto error2; 454 goto error2;
455 }
487 456
488 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops; 457 /* initialize data interface */
458 if (cdc_ncm_setup(dev)) {
459 dev_dbg(&intf->dev, "cdc_ncm_setup() failed\n");
460 goto error2;
461 }
489 462
490 usb_set_intfdata(ctx->data, dev); 463 usb_set_intfdata(ctx->data, dev);
491 usb_set_intfdata(ctx->control, dev); 464 usb_set_intfdata(ctx->control, dev);
492 usb_set_intfdata(ctx->intf, dev);
493 465
494 if (ctx->ether_desc) { 466 if (ctx->ether_desc) {
495 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress); 467 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
496 if (temp) 468 if (temp) {
469 dev_dbg(&intf->dev, "failed to get mac address\n");
497 goto error2; 470 goto error2;
498 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr); 471 }
472 dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
499 } 473 }
500 474
501 475 /* usbnet use these values for sizing tx/rx queues */
502 dev->in = usb_rcvbulkpipe(dev->udev, 476 dev->hard_mtu = ctx->tx_max;
503 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
504 dev->out = usb_sndbulkpipe(dev->udev,
505 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
506 dev->status = ctx->status_ep;
507 dev->rx_urb_size = ctx->rx_max; 477 dev->rx_urb_size = ctx->rx_max;
508 478
509 ctx->tx_speed = ctx->rx_speed = 0;
510 return 0; 479 return 0;
511 480
512error2: 481error2:
@@ -517,7 +486,7 @@ error2:
517error: 486error:
518 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]); 487 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
519 dev->data[0] = 0; 488 dev->data[0] = 0;
520 dev_info(&dev->udev->dev, "bind() failure\n"); 489 dev_info(&intf->dev, "bind() failure\n");
521 return -ENODEV; 490 return -ENODEV;
522} 491}
523EXPORT_SYMBOL_GPL(cdc_ncm_bind_common); 492EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
@@ -553,7 +522,7 @@ void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
553 ctx->control = NULL; 522 ctx->control = NULL;
554 } 523 }
555 524
556 usb_set_intfdata(ctx->intf, NULL); 525 usb_set_intfdata(intf, NULL);
557 cdc_ncm_free(ctx); 526 cdc_ncm_free(ctx);
558} 527}
559EXPORT_SYMBOL_GPL(cdc_ncm_unbind); 528EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
@@ -662,8 +631,9 @@ static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_
662} 631}
663 632
664struct sk_buff * 633struct sk_buff *
665cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign) 634cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
666{ 635{
636 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
667 struct usb_cdc_ncm_nth16 *nth16; 637 struct usb_cdc_ncm_nth16 *nth16;
668 struct usb_cdc_ncm_ndp16 *ndp16; 638 struct usb_cdc_ncm_ndp16 *ndp16;
669 struct sk_buff *skb_out; 639 struct sk_buff *skb_out;
@@ -683,11 +653,11 @@ cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
683 653
684 /* allocate a new OUT skb */ 654 /* allocate a new OUT skb */
685 if (!skb_out) { 655 if (!skb_out) {
686 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC); 656 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
687 if (skb_out == NULL) { 657 if (skb_out == NULL) {
688 if (skb != NULL) { 658 if (skb != NULL) {
689 dev_kfree_skb_any(skb); 659 dev_kfree_skb_any(skb);
690 ctx->netdev->stats.tx_dropped++; 660 dev->net->stats.tx_dropped++;
691 } 661 }
692 goto exit_no_skb; 662 goto exit_no_skb;
693 } 663 }
@@ -725,12 +695,12 @@ cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
725 /* won't fit, MTU problem? */ 695 /* won't fit, MTU problem? */
726 dev_kfree_skb_any(skb); 696 dev_kfree_skb_any(skb);
727 skb = NULL; 697 skb = NULL;
728 ctx->netdev->stats.tx_dropped++; 698 dev->net->stats.tx_dropped++;
729 } else { 699 } else {
730 /* no room for skb - store for later */ 700 /* no room for skb - store for later */
731 if (ctx->tx_rem_skb != NULL) { 701 if (ctx->tx_rem_skb != NULL) {
732 dev_kfree_skb_any(ctx->tx_rem_skb); 702 dev_kfree_skb_any(ctx->tx_rem_skb);
733 ctx->netdev->stats.tx_dropped++; 703 dev->net->stats.tx_dropped++;
734 } 704 }
735 ctx->tx_rem_skb = skb; 705 ctx->tx_rem_skb = skb;
736 ctx->tx_rem_sign = sign; 706 ctx->tx_rem_sign = sign;
@@ -763,7 +733,7 @@ cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
763 if (skb != NULL) { 733 if (skb != NULL) {
764 dev_kfree_skb_any(skb); 734 dev_kfree_skb_any(skb);
765 skb = NULL; 735 skb = NULL;
766 ctx->netdev->stats.tx_dropped++; 736 dev->net->stats.tx_dropped++;
767 } 737 }
768 738
769 ctx->tx_curr_frame_num = n; 739 ctx->tx_curr_frame_num = n;
@@ -788,19 +758,20 @@ cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
788 /* variables will be reset at next call */ 758 /* variables will be reset at next call */
789 } 759 }
790 760
791 /* 761 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
792 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes, 762 * bytes, we send buffers as it is. If we get more data, it
793 * we send buffers as it is. If we get more data, it would be more 763 * would be more efficient for USB HS mobile device with DMA
794 * efficient for USB HS mobile device with DMA engine to receive a full 764 * engine to receive a full size NTB, than canceling DMA
795 * size NTB, than canceling DMA transfer and receiving a short packet. 765 * transfer and receiving a short packet.
766 *
767 * This optimization support is pointless if we end up sending
768 * a ZLP after full sized NTBs.
796 */ 769 */
797 if (skb_out->len > CDC_NCM_MIN_TX_PKT) 770 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
798 /* final zero padding */ 771 skb_out->len > CDC_NCM_MIN_TX_PKT)
799 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len); 772 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
800 773 ctx->tx_max - skb_out->len);
801 /* do we need to prevent a ZLP? */ 774 else if ((skb_out->len % dev->maxpacket) == 0)
802 if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
803 (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
804 *skb_put(skb_out, 1) = 0; /* force short packet */ 775 *skb_put(skb_out, 1) = 0; /* force short packet */
805 776
806 /* set final frame length */ 777 /* set final frame length */
@@ -809,7 +780,7 @@ cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
809 780
810 /* return skb */ 781 /* return skb */
811 ctx->tx_curr_skb = NULL; 782 ctx->tx_curr_skb = NULL;
812 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num; 783 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
813 return skb_out; 784 return skb_out;
814 785
815exit_no_skb: 786exit_no_skb:
@@ -841,18 +812,19 @@ static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
841 812
842static void cdc_ncm_txpath_bh(unsigned long param) 813static void cdc_ncm_txpath_bh(unsigned long param)
843{ 814{
844 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param; 815 struct usbnet *dev = (struct usbnet *)param;
816 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
845 817
846 spin_lock_bh(&ctx->mtx); 818 spin_lock_bh(&ctx->mtx);
847 if (ctx->tx_timer_pending != 0) { 819 if (ctx->tx_timer_pending != 0) {
848 ctx->tx_timer_pending--; 820 ctx->tx_timer_pending--;
849 cdc_ncm_tx_timeout_start(ctx); 821 cdc_ncm_tx_timeout_start(ctx);
850 spin_unlock_bh(&ctx->mtx); 822 spin_unlock_bh(&ctx->mtx);
851 } else if (ctx->netdev != NULL) { 823 } else if (dev->net != NULL) {
852 spin_unlock_bh(&ctx->mtx); 824 spin_unlock_bh(&ctx->mtx);
853 netif_tx_lock_bh(ctx->netdev); 825 netif_tx_lock_bh(dev->net);
854 usbnet_start_xmit(NULL, ctx->netdev); 826 usbnet_start_xmit(NULL, dev->net);
855 netif_tx_unlock_bh(ctx->netdev); 827 netif_tx_unlock_bh(dev->net);
856 } else { 828 } else {
857 spin_unlock_bh(&ctx->mtx); 829 spin_unlock_bh(&ctx->mtx);
858 } 830 }
@@ -875,7 +847,7 @@ cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
875 goto error; 847 goto error;
876 848
877 spin_lock_bh(&ctx->mtx); 849 spin_lock_bh(&ctx->mtx);
878 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)); 850 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
879 spin_unlock_bh(&ctx->mtx); 851 spin_unlock_bh(&ctx->mtx);
880 return skb_out; 852 return skb_out;
881 853
@@ -889,6 +861,7 @@ error:
889/* verify NTB header and return offset of first NDP, or negative error */ 861/* verify NTB header and return offset of first NDP, or negative error */
890int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in) 862int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
891{ 863{
864 struct usbnet *dev = netdev_priv(skb_in->dev);
892 struct usb_cdc_ncm_nth16 *nth16; 865 struct usb_cdc_ncm_nth16 *nth16;
893 int len; 866 int len;
894 int ret = -EINVAL; 867 int ret = -EINVAL;
@@ -898,30 +871,33 @@ int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
898 871
899 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) + 872 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
900 sizeof(struct usb_cdc_ncm_ndp16))) { 873 sizeof(struct usb_cdc_ncm_ndp16))) {
901 pr_debug("frame too short\n"); 874 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
902 goto error; 875 goto error;
903 } 876 }
904 877
905 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data; 878 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
906 879
907 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) { 880 if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
908 pr_debug("invalid NTH16 signature <%u>\n", 881 netif_dbg(dev, rx_err, dev->net,
909 le32_to_cpu(nth16->dwSignature)); 882 "invalid NTH16 signature <%#010x>\n",
883 le32_to_cpu(nth16->dwSignature));
910 goto error; 884 goto error;
911 } 885 }
912 886
913 len = le16_to_cpu(nth16->wBlockLength); 887 len = le16_to_cpu(nth16->wBlockLength);
914 if (len > ctx->rx_max) { 888 if (len > ctx->rx_max) {
915 pr_debug("unsupported NTB block length %u/%u\n", len, 889 netif_dbg(dev, rx_err, dev->net,
916 ctx->rx_max); 890 "unsupported NTB block length %u/%u\n", len,
891 ctx->rx_max);
917 goto error; 892 goto error;
918 } 893 }
919 894
920 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) && 895 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
921 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) && 896 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
922 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) { 897 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
923 pr_debug("sequence number glitch prev=%d curr=%d\n", 898 netif_dbg(dev, rx_err, dev->net,
924 ctx->rx_seq, le16_to_cpu(nth16->wSequence)); 899 "sequence number glitch prev=%d curr=%d\n",
900 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
925 } 901 }
926 ctx->rx_seq = le16_to_cpu(nth16->wSequence); 902 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
927 903
@@ -934,18 +910,20 @@ EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
934/* verify NDP header and return number of datagrams, or negative error */ 910/* verify NDP header and return number of datagrams, or negative error */
935int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset) 911int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
936{ 912{
913 struct usbnet *dev = netdev_priv(skb_in->dev);
937 struct usb_cdc_ncm_ndp16 *ndp16; 914 struct usb_cdc_ncm_ndp16 *ndp16;
938 int ret = -EINVAL; 915 int ret = -EINVAL;
939 916
940 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) { 917 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
941 pr_debug("invalid NDP offset <%u>\n", ndpoffset); 918 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
919 ndpoffset);
942 goto error; 920 goto error;
943 } 921 }
944 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); 922 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
945 923
946 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) { 924 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
947 pr_debug("invalid DPT16 length <%u>\n", 925 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
948 le32_to_cpu(ndp16->dwSignature)); 926 le16_to_cpu(ndp16->wLength));
949 goto error; 927 goto error;
950 } 928 }
951 929
@@ -954,9 +932,9 @@ int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
954 sizeof(struct usb_cdc_ncm_dpe16)); 932 sizeof(struct usb_cdc_ncm_dpe16));
955 ret--; /* we process NDP entries except for the last one */ 933 ret--; /* we process NDP entries except for the last one */
956 934
957 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) > 935 if ((sizeof(struct usb_cdc_ncm_ndp16) +
958 skb_in->len) { 936 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
959 pr_debug("Invalid nframes = %d\n", ret); 937 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
960 ret = -EINVAL; 938 ret = -EINVAL;
961 } 939 }
962 940
@@ -989,9 +967,10 @@ next_ndp:
989 967
990 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); 968 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
991 969
992 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) { 970 if (ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
993 pr_debug("invalid DPT16 signature <%u>\n", 971 netif_dbg(dev, rx_err, dev->net,
994 le32_to_cpu(ndp16->dwSignature)); 972 "invalid DPT16 signature <%#010x>\n",
973 le32_to_cpu(ndp16->dwSignature));
995 goto err_ndp; 974 goto err_ndp;
996 } 975 }
997 dpe16 = ndp16->dpe16; 976 dpe16 = ndp16->dpe16;
@@ -1013,9 +992,9 @@ next_ndp:
1013 /* sanity checking */ 992 /* sanity checking */
1014 if (((offset + len) > skb_in->len) || 993 if (((offset + len) > skb_in->len) ||
1015 (len > ctx->rx_max) || (len < ETH_HLEN)) { 994 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1016 pr_debug("invalid frame detected (ignored)" 995 netif_dbg(dev, rx_err, dev->net,
1017 "offset[%u]=%u, length=%u, skb=%p\n", 996 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1018 x, offset, len, skb_in); 997 x, offset, len, skb_in);
1019 if (!x) 998 if (!x)
1020 goto err_ndp; 999 goto err_ndp;
1021 break; 1000 break;
@@ -1042,7 +1021,7 @@ error:
1042} 1021}
1043 1022
1044static void 1023static void
1045cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx, 1024cdc_ncm_speed_change(struct usbnet *dev,
1046 struct usb_cdc_speed_change *data) 1025 struct usb_cdc_speed_change *data)
1047{ 1026{
1048 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate); 1027 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
@@ -1052,25 +1031,16 @@ cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1052 * Currently the USB-NET API does not support reporting the actual 1031 * Currently the USB-NET API does not support reporting the actual
1053 * device speed. Do print it instead. 1032 * device speed. Do print it instead.
1054 */ 1033 */
1055 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) { 1034 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1056 ctx->tx_speed = tx_speed; 1035 netif_info(dev, link, dev->net,
1057 ctx->rx_speed = rx_speed; 1036 "%u mbit/s downlink %u mbit/s uplink\n",
1058 1037 (unsigned int)(rx_speed / 1000000U),
1059 if ((tx_speed > 1000000) && (rx_speed > 1000000)) { 1038 (unsigned int)(tx_speed / 1000000U));
1060 printk(KERN_INFO KBUILD_MODNAME 1039 } else {
1061 ": %s: %u mbit/s downlink " 1040 netif_info(dev, link, dev->net,
1062 "%u mbit/s uplink\n", 1041 "%u kbit/s downlink %u kbit/s uplink\n",
1063 ctx->netdev->name, 1042 (unsigned int)(rx_speed / 1000U),
1064 (unsigned int)(rx_speed / 1000000U), 1043 (unsigned int)(tx_speed / 1000U));
1065 (unsigned int)(tx_speed / 1000000U));
1066 } else {
1067 printk(KERN_INFO KBUILD_MODNAME
1068 ": %s: %u kbit/s downlink "
1069 "%u kbit/s uplink\n",
1070 ctx->netdev->name,
1071 (unsigned int)(rx_speed / 1000U),
1072 (unsigned int)(tx_speed / 1000U));
1073 }
1074 } 1044 }
1075} 1045}
1076 1046
@@ -1086,7 +1056,7 @@ static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1086 1056
1087 /* test for split data in 8-byte chunks */ 1057 /* test for split data in 8-byte chunks */
1088 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) { 1058 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1089 cdc_ncm_speed_change(ctx, 1059 cdc_ncm_speed_change(dev,
1090 (struct usb_cdc_speed_change *)urb->transfer_buffer); 1060 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1091 return; 1061 return;
1092 } 1062 }
@@ -1101,14 +1071,10 @@ static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1101 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE. 1071 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1102 */ 1072 */
1103 ctx->connected = le16_to_cpu(event->wValue); 1073 ctx->connected = le16_to_cpu(event->wValue);
1104 1074 netif_info(dev, link, dev->net,
1105 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:" 1075 "network connection: %sconnected\n",
1106 " %sconnected\n", 1076 ctx->connected ? "" : "dis");
1107 ctx->netdev->name, ctx->connected ? "" : "dis");
1108
1109 usbnet_link_change(dev, ctx->connected, 0); 1077 usbnet_link_change(dev, ctx->connected, 0);
1110 if (!ctx->connected)
1111 ctx->tx_speed = ctx->rx_speed = 0;
1112 break; 1078 break;
1113 1079
1114 case USB_CDC_NOTIFY_SPEED_CHANGE: 1080 case USB_CDC_NOTIFY_SPEED_CHANGE:
@@ -1116,8 +1082,8 @@ static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1116 sizeof(struct usb_cdc_speed_change))) 1082 sizeof(struct usb_cdc_speed_change)))
1117 set_bit(EVENT_STS_SPLIT, &dev->flags); 1083 set_bit(EVENT_STS_SPLIT, &dev->flags);
1118 else 1084 else
1119 cdc_ncm_speed_change(ctx, 1085 cdc_ncm_speed_change(dev,
1120 (struct usb_cdc_speed_change *) &event[1]); 1086 (struct usb_cdc_speed_change *)&event[1]);
1121 break; 1087 break;
1122 1088
1123 default: 1089 default:
@@ -1139,22 +1105,6 @@ static int cdc_ncm_check_connect(struct usbnet *dev)
1139 return !ctx->connected; 1105 return !ctx->connected;
1140} 1106}
1141 1107
1142static int
1143cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1144{
1145 return usbnet_probe(udev, prod);
1146}
1147
1148static void cdc_ncm_disconnect(struct usb_interface *intf)
1149{
1150 struct usbnet *dev = usb_get_intfdata(intf);
1151
1152 if (dev == NULL)
1153 return; /* already disconnected */
1154
1155 usbnet_disconnect(intf);
1156}
1157
1158static const struct driver_info cdc_ncm_info = { 1108static const struct driver_info cdc_ncm_info = {
1159 .description = "CDC NCM", 1109 .description = "CDC NCM",
1160 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET, 1110 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
@@ -1265,8 +1215,8 @@ MODULE_DEVICE_TABLE(usb, cdc_devs);
1265static struct usb_driver cdc_ncm_driver = { 1215static struct usb_driver cdc_ncm_driver = {
1266 .name = "cdc_ncm", 1216 .name = "cdc_ncm",
1267 .id_table = cdc_devs, 1217 .id_table = cdc_devs,
1268 .probe = cdc_ncm_probe, 1218 .probe = usbnet_probe,
1269 .disconnect = cdc_ncm_disconnect, 1219 .disconnect = usbnet_disconnect,
1270 .suspend = usbnet_suspend, 1220 .suspend = usbnet_suspend,
1271 .resume = usbnet_resume, 1221 .resume = usbnet_resume,
1272 .reset_resume = usbnet_resume, 1222 .reset_resume = usbnet_resume,
diff --git a/include/linux/usb/cdc_ncm.h b/include/linux/usb/cdc_ncm.h
index cc25b70af33c..2300f7492927 100644
--- a/include/linux/usb/cdc_ncm.h
+++ b/include/linux/usb/cdc_ncm.h
@@ -36,6 +36,9 @@
36 * SUCH DAMAGE. 36 * SUCH DAMAGE.
37 */ 37 */
38 38
39#ifndef __LINUX_USB_CDC_NCM_H
40#define __LINUX_USB_CDC_NCM_H
41
39#define CDC_NCM_COMM_ALTSETTING_NCM 0 42#define CDC_NCM_COMM_ALTSETTING_NCM 0
40#define CDC_NCM_COMM_ALTSETTING_MBIM 1 43#define CDC_NCM_COMM_ALTSETTING_MBIM 1
41 44
@@ -85,22 +88,13 @@
85#define cdc_ncm_data_intf_is_mbim(x) ((x)->desc.bInterfaceProtocol == USB_CDC_MBIM_PROTO_NTB) 88#define cdc_ncm_data_intf_is_mbim(x) ((x)->desc.bInterfaceProtocol == USB_CDC_MBIM_PROTO_NTB)
86 89
87struct cdc_ncm_ctx { 90struct cdc_ncm_ctx {
88 struct usb_cdc_ncm_ntb_parameters ncm_parm;
89 struct hrtimer tx_timer; 91 struct hrtimer tx_timer;
90 struct tasklet_struct bh; 92 struct tasklet_struct bh;
91 93
92 const struct usb_cdc_ncm_desc *func_desc; 94 const struct usb_cdc_ncm_desc *func_desc;
93 const struct usb_cdc_mbim_desc *mbim_desc; 95 const struct usb_cdc_mbim_desc *mbim_desc;
94 const struct usb_cdc_header_desc *header_desc;
95 const struct usb_cdc_union_desc *union_desc;
96 const struct usb_cdc_ether_desc *ether_desc; 96 const struct usb_cdc_ether_desc *ether_desc;
97 97
98 struct net_device *netdev;
99 struct usb_device *udev;
100 struct usb_host_endpoint *in_ep;
101 struct usb_host_endpoint *out_ep;
102 struct usb_host_endpoint *status_ep;
103 struct usb_interface *intf;
104 struct usb_interface *control; 98 struct usb_interface *control;
105 struct usb_interface *data; 99 struct usb_interface *data;
106 100
@@ -113,8 +107,6 @@ struct cdc_ncm_ctx {
113 107
114 u32 tx_timer_pending; 108 u32 tx_timer_pending;
115 u32 tx_curr_frame_num; 109 u32 tx_curr_frame_num;
116 u32 rx_speed;
117 u32 tx_speed;
118 u32 rx_max; 110 u32 rx_max;
119 u32 tx_max; 111 u32 tx_max;
120 u32 max_datagram_size; 112 u32 max_datagram_size;
@@ -127,9 +119,11 @@ struct cdc_ncm_ctx {
127 u16 connected; 119 u16 connected;
128}; 120};
129 121
130extern u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf); 122u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf);
131extern int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting); 123int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting);
132extern void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf); 124void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf);
133extern struct sk_buff *cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign); 125struct sk_buff *cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign);
134extern int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in); 126int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in);
135extern int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset); 127int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset);
128
129#endif /* __LINUX_USB_CDC_NCM_H */