blob: 13f7705fd679a11115327d5cd5a3d59a5cf699c0 [file] [log] [blame]
Greg Suarez9bf211a32012-10-22 10:56:36 +00001/*
2 * Copyright (c) 2012 Smith Micro Software, Inc.
3 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
4 *
5 * This driver is based on and reuse most of cdc_ncm, which is
6 * Copyright (C) ST-Ericsson 2010-2012
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/ethtool.h>
Bjørn Morka82c7ce2012-10-22 10:56:39 +000016#include <linux/if_vlan.h>
Greg Suarez9bf211a32012-10-22 10:56:36 +000017#include <linux/ip.h>
18#include <linux/mii.h>
19#include <linux/usb.h>
20#include <linux/usb/cdc.h>
21#include <linux/usb/usbnet.h>
22#include <linux/usb/cdc-wdm.h>
23#include <linux/usb/cdc_ncm.h>
Bjørn Mork5b8f15f2013-10-31 15:56:10 +010024#include <net/ipv6.h>
25#include <net/addrconf.h>
Greg Suarez9bf211a32012-10-22 10:56:36 +000026
27/* driver specific data - must match cdc_ncm usage */
28struct cdc_mbim_state {
29 struct cdc_ncm_ctx *ctx;
30 atomic_t pmcount;
31 struct usb_driver *subdriver;
32 struct usb_interface *control;
33 struct usb_interface *data;
34};
35
36/* using a counter to merge subdriver requests with our own into a combined state */
37static int cdc_mbim_manage_power(struct usbnet *dev, int on)
38{
39 struct cdc_mbim_state *info = (void *)&dev->data;
40 int rv = 0;
41
42 dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
43
44 if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
45 /* need autopm_get/put here to ensure the usbcore sees the new value */
46 rv = usb_autopm_get_interface(dev->intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000047 dev->intf->needs_remote_wakeup = on;
Bjørn Mork04d948a2013-11-01 14:18:52 +010048 if (!rv)
49 usb_autopm_put_interface(dev->intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000050 }
Bjørn Mork04d948a2013-11-01 14:18:52 +010051 return 0;
Greg Suarez9bf211a32012-10-22 10:56:36 +000052}
53
54static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
55{
56 struct usbnet *dev = usb_get_intfdata(intf);
57
58 /* can be called while disconnecting */
59 if (!dev)
60 return 0;
61
62 return cdc_mbim_manage_power(dev, status);
63}
64
65
66static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
67{
68 struct cdc_ncm_ctx *ctx;
69 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
70 int ret = -ENODEV;
Bjørn Mork1e8bbe62013-03-14 01:05:13 +000071 u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000072 struct cdc_mbim_state *info = (void *)&dev->data;
73
Greg Suarez9bf211a32012-10-22 10:56:36 +000074 /* Probably NCM, defer for cdc_ncm_bind */
75 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
76 goto err;
77
78 ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
79 if (ret)
80 goto err;
81
82 ctx = info->ctx;
83
84 /* The MBIM descriptor and the status endpoint are required */
85 if (ctx->mbim_desc && dev->status)
86 subdriver = usb_cdc_wdm_register(ctx->control,
87 &dev->status->desc,
88 le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
89 cdc_mbim_wdm_manage_power);
90 if (IS_ERR(subdriver)) {
91 ret = PTR_ERR(subdriver);
92 cdc_ncm_unbind(dev, intf);
93 goto err;
94 }
95
96 /* can't let usbnet use the interrupt endpoint */
97 dev->status = NULL;
98 info->subdriver = subdriver;
99
100 /* MBIM cannot do ARP */
101 dev->net->flags |= IFF_NOARP;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000102
103 /* no need to put the VLAN tci in the packet headers */
Patrick McHardyf6469682013-04-19 02:04:27 +0000104 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000105err:
106 return ret;
107}
108
109static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
110{
111 struct cdc_mbim_state *info = (void *)&dev->data;
112 struct cdc_ncm_ctx *ctx = info->ctx;
113
114 /* disconnect subdriver from control interface */
115 if (info->subdriver && info->subdriver->disconnect)
116 info->subdriver->disconnect(ctx->control);
117 info->subdriver = NULL;
118
119 /* let NCM unbind clean up both control and data interface */
120 cdc_ncm_unbind(dev, intf);
121}
122
123
124static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
125{
126 struct sk_buff *skb_out;
127 struct cdc_mbim_state *info = (void *)&dev->data;
128 struct cdc_ncm_ctx *ctx = info->ctx;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000129 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
130 u16 tci = 0;
131 u8 *c;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000132
133 if (!ctx)
134 goto error;
135
136 if (skb) {
Bjørn Mork32b161a2013-04-16 00:17:07 +0000137 if (skb->len <= ETH_HLEN)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000138 goto error;
139
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000140 /* mapping VLANs to MBIM sessions:
141 * no tag => IPS session <0>
142 * 1 - 255 => IPS session <vlanid>
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000143 * 256 - 511 => DSS session <vlanid - 256>
144 * 512 - 4095 => unsupported, drop
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000145 */
146 vlan_get_tag(skb, &tci);
147
148 switch (tci & 0x0f00) {
149 case 0x0000: /* VLAN ID 0 - 255 */
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000150 /* verify that datagram is IPv4 or IPv6 */
151 skb_reset_mac_header(skb);
152 switch (eth_hdr(skb)->h_proto) {
153 case htons(ETH_P_IP):
154 case htons(ETH_P_IPV6):
155 break;
156 default:
157 goto error;
158 }
159 c = (u8 *)&sign;
160 c[3] = tci;
161 break;
162 case 0x0100: /* VLAN ID 256 - 511 */
163 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000164 c = (u8 *)&sign;
165 c[3] = tci;
166 break;
167 default:
168 netif_err(dev, tx_err, dev->net,
169 "unsupported tci=0x%04x\n", tci);
170 goto error;
171 }
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000172 skb_pull(skb, ETH_HLEN);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000173 }
174
175 spin_lock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100176 skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000177 spin_unlock_bh(&ctx->mtx);
178 return skb_out;
179
180error:
181 if (skb)
182 dev_kfree_skb_any(skb);
183
184 return NULL;
185}
186
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100187/* Some devices are known to send Neigbor Solicitation messages and
188 * require Neigbor Advertisement replies. The IPv6 core will not
189 * respond since IFF_NOARP is set, so we must handle them ourselves.
190 */
191static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
192{
193 struct ipv6hdr *iph = (void *)buf;
194 struct nd_msg *msg = (void *)(iph + 1);
195 struct net_device *netdev;
196 struct inet6_dev *in6_dev;
197 bool is_router;
198
199 /* we'll only respond to requests from unicast addresses to
200 * our solicited node addresses.
201 */
202 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
203 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
204 return;
205
206 /* need to send the NA on the VLAN dev, if any */
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200207 rcu_read_lock();
208 if (tci) {
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100209 netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
210 tci);
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200211 if (!netdev) {
212 rcu_read_unlock();
213 return;
214 }
215 } else {
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100216 netdev = dev->net;
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200217 }
218 dev_hold(netdev);
219 rcu_read_unlock();
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100220
221 in6_dev = in6_dev_get(netdev);
222 if (!in6_dev)
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200223 goto out;
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100224 is_router = !!in6_dev->cnf.forwarding;
225 in6_dev_put(in6_dev);
226
227 /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
228 ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
229 is_router /* router */,
230 true /* solicited */,
231 false /* override */,
232 true /* inc_opt */);
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200233out:
234 dev_put(netdev);
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100235}
236
237static bool is_neigh_solicit(u8 *buf, size_t len)
238{
239 struct ipv6hdr *iph = (void *)buf;
240 struct nd_msg *msg = (void *)(iph + 1);
241
242 return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
243 iph->nexthdr == IPPROTO_ICMPV6 &&
244 msg->icmph.icmp6_code == 0 &&
245 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
246}
247
248
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000249static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000250{
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000251 __be16 proto = htons(ETH_P_802_3);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000252 struct sk_buff *skb = NULL;
253
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000254 if (tci < 256) { /* IPS session? */
255 if (len < sizeof(struct iphdr))
256 goto err;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000257
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000258 switch (*buf & 0xf0) {
259 case 0x40:
260 proto = htons(ETH_P_IP);
261 break;
262 case 0x60:
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100263 if (is_neigh_solicit(buf, len))
264 do_neigh_solicit(dev, buf, tci);
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000265 proto = htons(ETH_P_IPV6);
266 break;
267 default:
268 goto err;
269 }
Greg Suarez9bf211a32012-10-22 10:56:36 +0000270 }
271
272 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
273 if (!skb)
274 goto err;
275
276 /* add an ethernet header */
277 skb_put(skb, ETH_HLEN);
278 skb_reset_mac_header(skb);
279 eth_hdr(skb)->h_proto = proto;
280 memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
281 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
282
283 /* add datagram */
284 memcpy(skb_put(skb, len), buf, len);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000285
286 /* map MBIM session to VLAN */
287 if (tci)
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000288 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000289err:
290 return skb;
291}
292
293static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
294{
295 struct sk_buff *skb;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000296 struct cdc_mbim_state *info = (void *)&dev->data;
297 struct cdc_ncm_ctx *ctx = info->ctx;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000298 int len;
299 int nframes;
300 int x;
301 int offset;
302 struct usb_cdc_ncm_ndp16 *ndp16;
303 struct usb_cdc_ncm_dpe16 *dpe16;
304 int ndpoffset;
305 int loopcount = 50; /* arbitrary max preventing infinite loop */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000306 u8 *c;
307 u16 tci;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000308
309 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
310 if (ndpoffset < 0)
311 goto error;
312
313next_ndp:
314 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
315 if (nframes < 0)
316 goto error;
317
318 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
319
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000320 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000321 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000322 c = (u8 *)&ndp16->dwSignature;
323 tci = c[3];
Greg Suarez9bf211a32012-10-22 10:56:36 +0000324 break;
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000325 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
326 c = (u8 *)&ndp16->dwSignature;
327 tci = c[3] + 256;
328 break;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000329 default:
330 netif_dbg(dev, rx_err, dev->net,
331 "unsupported NDP signature <0x%08x>\n",
332 le32_to_cpu(ndp16->dwSignature));
333 goto err_ndp;
334
335 }
336
337 dpe16 = ndp16->dpe16;
338 for (x = 0; x < nframes; x++, dpe16++) {
339 offset = le16_to_cpu(dpe16->wDatagramIndex);
340 len = le16_to_cpu(dpe16->wDatagramLength);
341
342 /*
343 * CDC NCM ch. 3.7
344 * All entries after first NULL entry are to be ignored
345 */
346 if ((offset == 0) || (len == 0)) {
347 if (!x)
348 goto err_ndp; /* empty NTB */
349 break;
350 }
351
352 /* sanity checking */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000353 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000354 netif_dbg(dev, rx_err, dev->net,
355 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
356 x, offset, len, skb_in);
357 if (!x)
358 goto err_ndp;
359 break;
360 } else {
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000361 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000362 if (!skb)
363 goto error;
364 usbnet_skb_return(dev, skb);
365 }
366 }
367err_ndp:
368 /* are there more NDPs to process? */
369 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
370 if (ndpoffset && loopcount--)
371 goto next_ndp;
372
373 return 1;
374error:
375 return 0;
376}
377
378static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
379{
Bjørn Morke62416e2013-11-01 14:18:56 +0100380 int ret = -ENODEV;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000381 struct usbnet *dev = usb_get_intfdata(intf);
382 struct cdc_mbim_state *info = (void *)&dev->data;
383 struct cdc_ncm_ctx *ctx = info->ctx;
384
Bjørn Morke62416e2013-11-01 14:18:56 +0100385 if (!ctx)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000386 goto error;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000387
Ming Lei9a443122013-03-15 12:08:56 +0800388 /*
389 * Both usbnet_suspend() and subdriver->suspend() MUST return 0
390 * in system sleep context, otherwise, the resume callback has
391 * to recover device from previous suspend failure.
392 */
Greg Suarez9bf211a32012-10-22 10:56:36 +0000393 ret = usbnet_suspend(intf, message);
394 if (ret < 0)
395 goto error;
396
397 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
398 ret = info->subdriver->suspend(intf, message);
399 if (ret < 0)
400 usbnet_resume(intf);
401
402error:
403 return ret;
404}
405
406static int cdc_mbim_resume(struct usb_interface *intf)
407{
408 int ret = 0;
409 struct usbnet *dev = usb_get_intfdata(intf);
410 struct cdc_mbim_state *info = (void *)&dev->data;
411 struct cdc_ncm_ctx *ctx = info->ctx;
412 bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
413
414 if (callsub)
415 ret = info->subdriver->resume(intf);
416 if (ret < 0)
417 goto err;
418 ret = usbnet_resume(intf);
Bjørn Morkc37ac9b2013-11-01 14:18:55 +0100419 if (ret < 0 && callsub)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000420 info->subdriver->suspend(intf, PMSG_SUSPEND);
421err:
422 return ret;
423}
424
425static const struct driver_info cdc_mbim_info = {
426 .description = "CDC MBIM",
Bjørn Mork844e88f2013-01-23 00:57:02 +0000427 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
428 .bind = cdc_mbim_bind,
429 .unbind = cdc_mbim_unbind,
430 .manage_power = cdc_mbim_manage_power,
431 .rx_fixup = cdc_mbim_rx_fixup,
432 .tx_fixup = cdc_mbim_tx_fixup,
433};
434
435/* MBIM and NCM devices should not need a ZLP after NTBs with
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100436 * dwNtbOutMaxSize length. Nevertheless, a number of devices from
437 * different vendor IDs will fail unless we send ZLPs, forcing us
438 * to make this the default.
439 *
440 * This default may cause a performance penalty for spec conforming
441 * devices wanting to take advantage of optimizations possible without
442 * ZLPs. A whitelist is added in an attempt to avoid this for devices
443 * known to conform to the MBIM specification.
444 *
445 * All known devices supporting NCM compatibility mode are also
446 * conforming to the NCM and MBIM specifications. For this reason, the
447 * NCM subclass entry is also in the ZLP whitelist.
Bjørn Mork844e88f2013-01-23 00:57:02 +0000448 */
449static const struct driver_info cdc_mbim_info_zlp = {
450 .description = "CDC MBIM",
Bjørn Mork328d7b82013-01-21 05:50:39 +0000451 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
Greg Suarez9bf211a32012-10-22 10:56:36 +0000452 .bind = cdc_mbim_bind,
453 .unbind = cdc_mbim_unbind,
454 .manage_power = cdc_mbim_manage_power,
455 .rx_fixup = cdc_mbim_rx_fixup,
456 .tx_fixup = cdc_mbim_tx_fixup,
457};
458
459static const struct usb_device_id mbim_devs[] = {
460 /* This duplicate NCM entry is intentional. MBIM devices can
461 * be disguised as NCM by default, and this is necessary to
462 * allow us to bind the correct driver_info to such devices.
463 *
464 * bind() will sort out this for us, selecting the correct
465 * entry and reject the other
466 */
467 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
468 .driver_info = (unsigned long)&cdc_mbim_info,
469 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100470 /* ZLP conformance whitelist: All Ericsson MBIM devices */
471 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
Greg Suarez9bf211a32012-10-22 10:56:36 +0000472 .driver_info = (unsigned long)&cdc_mbim_info,
473 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100474 /* default entry */
475 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
476 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
477 },
Greg Suarez9bf211a32012-10-22 10:56:36 +0000478 {
479 },
480};
481MODULE_DEVICE_TABLE(usb, mbim_devs);
482
483static struct usb_driver cdc_mbim_driver = {
484 .name = "cdc_mbim",
485 .id_table = mbim_devs,
486 .probe = usbnet_probe,
487 .disconnect = usbnet_disconnect,
488 .suspend = cdc_mbim_suspend,
489 .resume = cdc_mbim_resume,
490 .reset_resume = cdc_mbim_resume,
491 .supports_autosuspend = 1,
492 .disable_hub_initiated_lpm = 1,
493};
494module_usb_driver(cdc_mbim_driver);
495
496MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
497MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
498MODULE_DESCRIPTION("USB CDC MBIM host driver");
499MODULE_LICENSE("GPL");