blob: 253472a8a9836e15b53bf22418a021b091060faa [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);
47 if (rv < 0)
48 goto err;
49 dev->intf->needs_remote_wakeup = on;
50 usb_autopm_put_interface(dev->intf);
51 }
52err:
53 return rv;
54}
55
56static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
57{
58 struct usbnet *dev = usb_get_intfdata(intf);
59
60 /* can be called while disconnecting */
61 if (!dev)
62 return 0;
63
64 return cdc_mbim_manage_power(dev, status);
65}
66
67
68static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
69{
70 struct cdc_ncm_ctx *ctx;
71 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
72 int ret = -ENODEV;
Bjørn Mork1e8bbe62013-03-14 01:05:13 +000073 u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000074 struct cdc_mbim_state *info = (void *)&dev->data;
75
Greg Suarez9bf211a32012-10-22 10:56:36 +000076 /* Probably NCM, defer for cdc_ncm_bind */
77 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
78 goto err;
79
80 ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
81 if (ret)
82 goto err;
83
84 ctx = info->ctx;
85
86 /* The MBIM descriptor and the status endpoint are required */
87 if (ctx->mbim_desc && dev->status)
88 subdriver = usb_cdc_wdm_register(ctx->control,
89 &dev->status->desc,
90 le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
91 cdc_mbim_wdm_manage_power);
92 if (IS_ERR(subdriver)) {
93 ret = PTR_ERR(subdriver);
94 cdc_ncm_unbind(dev, intf);
95 goto err;
96 }
97
98 /* can't let usbnet use the interrupt endpoint */
99 dev->status = NULL;
100 info->subdriver = subdriver;
101
102 /* MBIM cannot do ARP */
103 dev->net->flags |= IFF_NOARP;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000104
105 /* no need to put the VLAN tci in the packet headers */
Patrick McHardyf6469682013-04-19 02:04:27 +0000106 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000107err:
108 return ret;
109}
110
111static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
112{
113 struct cdc_mbim_state *info = (void *)&dev->data;
114 struct cdc_ncm_ctx *ctx = info->ctx;
115
116 /* disconnect subdriver from control interface */
117 if (info->subdriver && info->subdriver->disconnect)
118 info->subdriver->disconnect(ctx->control);
119 info->subdriver = NULL;
120
121 /* let NCM unbind clean up both control and data interface */
122 cdc_ncm_unbind(dev, intf);
123}
124
125
126static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
127{
128 struct sk_buff *skb_out;
129 struct cdc_mbim_state *info = (void *)&dev->data;
130 struct cdc_ncm_ctx *ctx = info->ctx;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000131 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
132 u16 tci = 0;
133 u8 *c;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000134
135 if (!ctx)
136 goto error;
137
138 if (skb) {
Bjørn Mork32b161a2013-04-16 00:17:07 +0000139 if (skb->len <= ETH_HLEN)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000140 goto error;
141
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000142 /* mapping VLANs to MBIM sessions:
143 * no tag => IPS session <0>
144 * 1 - 255 => IPS session <vlanid>
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000145 * 256 - 511 => DSS session <vlanid - 256>
146 * 512 - 4095 => unsupported, drop
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000147 */
148 vlan_get_tag(skb, &tci);
149
150 switch (tci & 0x0f00) {
151 case 0x0000: /* VLAN ID 0 - 255 */
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000152 /* verify that datagram is IPv4 or IPv6 */
153 skb_reset_mac_header(skb);
154 switch (eth_hdr(skb)->h_proto) {
155 case htons(ETH_P_IP):
156 case htons(ETH_P_IPV6):
157 break;
158 default:
159 goto error;
160 }
161 c = (u8 *)&sign;
162 c[3] = tci;
163 break;
164 case 0x0100: /* VLAN ID 256 - 511 */
165 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000166 c = (u8 *)&sign;
167 c[3] = tci;
168 break;
169 default:
170 netif_err(dev, tx_err, dev->net,
171 "unsupported tci=0x%04x\n", tci);
172 goto error;
173 }
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000174 skb_pull(skb, ETH_HLEN);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000175 }
176
177 spin_lock_bh(&ctx->mtx);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000178 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000179 spin_unlock_bh(&ctx->mtx);
180 return skb_out;
181
182error:
183 if (skb)
184 dev_kfree_skb_any(skb);
185
186 return NULL;
187}
188
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100189/* Some devices are known to send Neigbor Solicitation messages and
190 * require Neigbor Advertisement replies. The IPv6 core will not
191 * respond since IFF_NOARP is set, so we must handle them ourselves.
192 */
193static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
194{
195 struct ipv6hdr *iph = (void *)buf;
196 struct nd_msg *msg = (void *)(iph + 1);
197 struct net_device *netdev;
198 struct inet6_dev *in6_dev;
199 bool is_router;
200
201 /* we'll only respond to requests from unicast addresses to
202 * our solicited node addresses.
203 */
204 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
205 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
206 return;
207
208 /* need to send the NA on the VLAN dev, if any */
209 if (tci)
210 netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
211 tci);
212 else
213 netdev = dev->net;
214 if (!netdev)
215 return;
216
217 in6_dev = in6_dev_get(netdev);
218 if (!in6_dev)
219 return;
220 is_router = !!in6_dev->cnf.forwarding;
221 in6_dev_put(in6_dev);
222
223 /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
224 ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
225 is_router /* router */,
226 true /* solicited */,
227 false /* override */,
228 true /* inc_opt */);
229}
230
231static bool is_neigh_solicit(u8 *buf, size_t len)
232{
233 struct ipv6hdr *iph = (void *)buf;
234 struct nd_msg *msg = (void *)(iph + 1);
235
236 return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
237 iph->nexthdr == IPPROTO_ICMPV6 &&
238 msg->icmph.icmp6_code == 0 &&
239 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
240}
241
242
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000243static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000244{
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000245 __be16 proto = htons(ETH_P_802_3);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000246 struct sk_buff *skb = NULL;
247
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000248 if (tci < 256) { /* IPS session? */
249 if (len < sizeof(struct iphdr))
250 goto err;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000251
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000252 switch (*buf & 0xf0) {
253 case 0x40:
254 proto = htons(ETH_P_IP);
255 break;
256 case 0x60:
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100257 if (is_neigh_solicit(buf, len))
258 do_neigh_solicit(dev, buf, tci);
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000259 proto = htons(ETH_P_IPV6);
260 break;
261 default:
262 goto err;
263 }
Greg Suarez9bf211a32012-10-22 10:56:36 +0000264 }
265
266 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
267 if (!skb)
268 goto err;
269
270 /* add an ethernet header */
271 skb_put(skb, ETH_HLEN);
272 skb_reset_mac_header(skb);
273 eth_hdr(skb)->h_proto = proto;
274 memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
275 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
276
277 /* add datagram */
278 memcpy(skb_put(skb, len), buf, len);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000279
280 /* map MBIM session to VLAN */
281 if (tci)
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000282 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000283err:
284 return skb;
285}
286
287static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
288{
289 struct sk_buff *skb;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000290 struct cdc_mbim_state *info = (void *)&dev->data;
291 struct cdc_ncm_ctx *ctx = info->ctx;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000292 int len;
293 int nframes;
294 int x;
295 int offset;
296 struct usb_cdc_ncm_ndp16 *ndp16;
297 struct usb_cdc_ncm_dpe16 *dpe16;
298 int ndpoffset;
299 int loopcount = 50; /* arbitrary max preventing infinite loop */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000300 u8 *c;
301 u16 tci;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000302
303 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
304 if (ndpoffset < 0)
305 goto error;
306
307next_ndp:
308 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
309 if (nframes < 0)
310 goto error;
311
312 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
313
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000314 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000315 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000316 c = (u8 *)&ndp16->dwSignature;
317 tci = c[3];
Greg Suarez9bf211a32012-10-22 10:56:36 +0000318 break;
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000319 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
320 c = (u8 *)&ndp16->dwSignature;
321 tci = c[3] + 256;
322 break;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000323 default:
324 netif_dbg(dev, rx_err, dev->net,
325 "unsupported NDP signature <0x%08x>\n",
326 le32_to_cpu(ndp16->dwSignature));
327 goto err_ndp;
328
329 }
330
331 dpe16 = ndp16->dpe16;
332 for (x = 0; x < nframes; x++, dpe16++) {
333 offset = le16_to_cpu(dpe16->wDatagramIndex);
334 len = le16_to_cpu(dpe16->wDatagramLength);
335
336 /*
337 * CDC NCM ch. 3.7
338 * All entries after first NULL entry are to be ignored
339 */
340 if ((offset == 0) || (len == 0)) {
341 if (!x)
342 goto err_ndp; /* empty NTB */
343 break;
344 }
345
346 /* sanity checking */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000347 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000348 netif_dbg(dev, rx_err, dev->net,
349 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
350 x, offset, len, skb_in);
351 if (!x)
352 goto err_ndp;
353 break;
354 } else {
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000355 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000356 if (!skb)
357 goto error;
358 usbnet_skb_return(dev, skb);
359 }
360 }
361err_ndp:
362 /* are there more NDPs to process? */
363 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
364 if (ndpoffset && loopcount--)
365 goto next_ndp;
366
367 return 1;
368error:
369 return 0;
370}
371
372static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
373{
374 int ret = 0;
375 struct usbnet *dev = usb_get_intfdata(intf);
376 struct cdc_mbim_state *info = (void *)&dev->data;
377 struct cdc_ncm_ctx *ctx = info->ctx;
378
379 if (ctx == NULL) {
380 ret = -1;
381 goto error;
382 }
383
Ming Lei9a443122013-03-15 12:08:56 +0800384 /*
385 * Both usbnet_suspend() and subdriver->suspend() MUST return 0
386 * in system sleep context, otherwise, the resume callback has
387 * to recover device from previous suspend failure.
388 */
Greg Suarez9bf211a32012-10-22 10:56:36 +0000389 ret = usbnet_suspend(intf, message);
390 if (ret < 0)
391 goto error;
392
393 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
394 ret = info->subdriver->suspend(intf, message);
395 if (ret < 0)
396 usbnet_resume(intf);
397
398error:
399 return ret;
400}
401
402static int cdc_mbim_resume(struct usb_interface *intf)
403{
404 int ret = 0;
405 struct usbnet *dev = usb_get_intfdata(intf);
406 struct cdc_mbim_state *info = (void *)&dev->data;
407 struct cdc_ncm_ctx *ctx = info->ctx;
408 bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
409
410 if (callsub)
411 ret = info->subdriver->resume(intf);
412 if (ret < 0)
413 goto err;
414 ret = usbnet_resume(intf);
415 if (ret < 0 && callsub && info->subdriver->suspend)
416 info->subdriver->suspend(intf, PMSG_SUSPEND);
417err:
418 return ret;
419}
420
421static const struct driver_info cdc_mbim_info = {
422 .description = "CDC MBIM",
Bjørn Mork844e88f2013-01-23 00:57:02 +0000423 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
424 .bind = cdc_mbim_bind,
425 .unbind = cdc_mbim_unbind,
426 .manage_power = cdc_mbim_manage_power,
427 .rx_fixup = cdc_mbim_rx_fixup,
428 .tx_fixup = cdc_mbim_tx_fixup,
429};
430
431/* MBIM and NCM devices should not need a ZLP after NTBs with
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100432 * dwNtbOutMaxSize length. Nevertheless, a number of devices from
433 * different vendor IDs will fail unless we send ZLPs, forcing us
434 * to make this the default.
435 *
436 * This default may cause a performance penalty for spec conforming
437 * devices wanting to take advantage of optimizations possible without
438 * ZLPs. A whitelist is added in an attempt to avoid this for devices
439 * known to conform to the MBIM specification.
440 *
441 * All known devices supporting NCM compatibility mode are also
442 * conforming to the NCM and MBIM specifications. For this reason, the
443 * NCM subclass entry is also in the ZLP whitelist.
Bjørn Mork844e88f2013-01-23 00:57:02 +0000444 */
445static const struct driver_info cdc_mbim_info_zlp = {
446 .description = "CDC MBIM",
Bjørn Mork328d7b82013-01-21 05:50:39 +0000447 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
Greg Suarez9bf211a32012-10-22 10:56:36 +0000448 .bind = cdc_mbim_bind,
449 .unbind = cdc_mbim_unbind,
450 .manage_power = cdc_mbim_manage_power,
451 .rx_fixup = cdc_mbim_rx_fixup,
452 .tx_fixup = cdc_mbim_tx_fixup,
453};
454
455static const struct usb_device_id mbim_devs[] = {
456 /* This duplicate NCM entry is intentional. MBIM devices can
457 * be disguised as NCM by default, and this is necessary to
458 * allow us to bind the correct driver_info to such devices.
459 *
460 * bind() will sort out this for us, selecting the correct
461 * entry and reject the other
462 */
463 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
464 .driver_info = (unsigned long)&cdc_mbim_info,
465 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100466 /* ZLP conformance whitelist: All Ericsson MBIM devices */
467 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
Greg Suarez9bf211a32012-10-22 10:56:36 +0000468 .driver_info = (unsigned long)&cdc_mbim_info,
469 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100470 /* default entry */
471 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
472 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
473 },
Greg Suarez9bf211a32012-10-22 10:56:36 +0000474 {
475 },
476};
477MODULE_DEVICE_TABLE(usb, mbim_devs);
478
479static struct usb_driver cdc_mbim_driver = {
480 .name = "cdc_mbim",
481 .id_table = mbim_devs,
482 .probe = usbnet_probe,
483 .disconnect = usbnet_disconnect,
484 .suspend = cdc_mbim_suspend,
485 .resume = cdc_mbim_resume,
486 .reset_resume = cdc_mbim_resume,
487 .supports_autosuspend = 1,
488 .disable_hub_initiated_lpm = 1,
489};
490module_usb_driver(cdc_mbim_driver);
491
492MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
493MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
494MODULE_DESCRIPTION("USB CDC MBIM host driver");
495MODULE_LICENSE("GPL");