blob: 694a8790f69513485bb8d0e9418af62a61cdc4e1 [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
Bjørn Mork146a08d2014-05-11 10:47:12 +020027/* alternative VLAN for IP session 0 if not untagged */
28#define MBIM_IPS0_VID 4094
29
Greg Suarez9bf211a32012-10-22 10:56:36 +000030/* driver specific data - must match cdc_ncm usage */
31struct cdc_mbim_state {
32 struct cdc_ncm_ctx *ctx;
33 atomic_t pmcount;
34 struct usb_driver *subdriver;
Bjørn Mork146a08d2014-05-11 10:47:12 +020035 unsigned long _unused;
36 unsigned long flags;
37};
38
39/* flags for the cdc_mbim_state.flags field */
40enum cdc_mbim_flags {
41 FLAG_IPS0_VLAN = 1 << 0, /* IP session 0 is tagged */
Greg Suarez9bf211a32012-10-22 10:56:36 +000042};
43
44/* using a counter to merge subdriver requests with our own into a combined state */
45static int cdc_mbim_manage_power(struct usbnet *dev, int on)
46{
47 struct cdc_mbim_state *info = (void *)&dev->data;
48 int rv = 0;
49
50 dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
51
52 if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) {
53 /* need autopm_get/put here to ensure the usbcore sees the new value */
54 rv = usb_autopm_get_interface(dev->intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000055 dev->intf->needs_remote_wakeup = on;
Bjørn Mork04d948a2013-11-01 14:18:52 +010056 if (!rv)
57 usb_autopm_put_interface(dev->intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +000058 }
Bjørn Mork04d948a2013-11-01 14:18:52 +010059 return 0;
Greg Suarez9bf211a32012-10-22 10:56:36 +000060}
61
62static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
63{
64 struct usbnet *dev = usb_get_intfdata(intf);
65
66 /* can be called while disconnecting */
67 if (!dev)
68 return 0;
69
70 return cdc_mbim_manage_power(dev, status);
71}
72
Bjørn Mork146a08d2014-05-11 10:47:12 +020073static int cdc_mbim_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
74{
75 struct usbnet *dev = netdev_priv(netdev);
76 struct cdc_mbim_state *info = (void *)&dev->data;
77
78 /* creation of this VLAN is a request to tag IP session 0 */
79 if (vid == MBIM_IPS0_VID)
80 info->flags |= FLAG_IPS0_VLAN;
81 else
82 if (vid >= 512) /* we don't map these to MBIM session */
83 return -EINVAL;
84 return 0;
85}
86
87static int cdc_mbim_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
88{
89 struct usbnet *dev = netdev_priv(netdev);
90 struct cdc_mbim_state *info = (void *)&dev->data;
91
92 /* this is a request for an untagged IP session 0 */
93 if (vid == MBIM_IPS0_VID)
94 info->flags &= ~FLAG_IPS0_VLAN;
95 return 0;
96}
97
98static const struct net_device_ops cdc_mbim_netdev_ops = {
99 .ndo_open = usbnet_open,
100 .ndo_stop = usbnet_stop,
101 .ndo_start_xmit = usbnet_start_xmit,
102 .ndo_tx_timeout = usbnet_tx_timeout,
103 .ndo_change_mtu = usbnet_change_mtu,
104 .ndo_set_mac_address = eth_mac_addr,
105 .ndo_validate_addr = eth_validate_addr,
106 .ndo_vlan_rx_add_vid = cdc_mbim_rx_add_vid,
107 .ndo_vlan_rx_kill_vid = cdc_mbim_rx_kill_vid,
108};
Greg Suarez9bf211a32012-10-22 10:56:36 +0000109
110static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
111{
112 struct cdc_ncm_ctx *ctx;
113 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
114 int ret = -ENODEV;
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000115 u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000116 struct cdc_mbim_state *info = (void *)&dev->data;
117
Greg Suarez9bf211a32012-10-22 10:56:36 +0000118 /* Probably NCM, defer for cdc_ncm_bind */
119 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
120 goto err;
121
122 ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
123 if (ret)
124 goto err;
125
126 ctx = info->ctx;
127
128 /* The MBIM descriptor and the status endpoint are required */
129 if (ctx->mbim_desc && dev->status)
130 subdriver = usb_cdc_wdm_register(ctx->control,
131 &dev->status->desc,
132 le16_to_cpu(ctx->mbim_desc->wMaxControlMessage),
133 cdc_mbim_wdm_manage_power);
134 if (IS_ERR(subdriver)) {
135 ret = PTR_ERR(subdriver);
136 cdc_ncm_unbind(dev, intf);
137 goto err;
138 }
139
140 /* can't let usbnet use the interrupt endpoint */
141 dev->status = NULL;
142 info->subdriver = subdriver;
143
144 /* MBIM cannot do ARP */
145 dev->net->flags |= IFF_NOARP;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000146
147 /* no need to put the VLAN tci in the packet headers */
Bjørn Mork146a08d2014-05-11 10:47:12 +0200148 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_FILTER;
149
150 /* monitor VLAN additions and removals */
151 dev->net->netdev_ops = &cdc_mbim_netdev_ops;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000152err:
153 return ret;
154}
155
156static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
157{
158 struct cdc_mbim_state *info = (void *)&dev->data;
159 struct cdc_ncm_ctx *ctx = info->ctx;
160
161 /* disconnect subdriver from control interface */
162 if (info->subdriver && info->subdriver->disconnect)
163 info->subdriver->disconnect(ctx->control);
164 info->subdriver = NULL;
165
166 /* let NCM unbind clean up both control and data interface */
167 cdc_ncm_unbind(dev, intf);
168}
169
Bjørn Mork6b5eeb72014-05-09 14:45:00 +0200170/* verify that the ethernet protocol is IPv4 or IPv6 */
171static bool is_ip_proto(__be16 proto)
172{
173 switch (proto) {
174 case htons(ETH_P_IP):
175 case htons(ETH_P_IPV6):
176 return true;
177 }
178 return false;
179}
Greg Suarez9bf211a32012-10-22 10:56:36 +0000180
181static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
182{
183 struct sk_buff *skb_out;
184 struct cdc_mbim_state *info = (void *)&dev->data;
185 struct cdc_ncm_ctx *ctx = info->ctx;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000186 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
187 u16 tci = 0;
Bjørn Mork6b5eeb72014-05-09 14:45:00 +0200188 bool is_ip;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000189 u8 *c;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000190
191 if (!ctx)
192 goto error;
193
194 if (skb) {
Bjørn Mork32b161a2013-04-16 00:17:07 +0000195 if (skb->len <= ETH_HLEN)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000196 goto error;
197
Bjørn Mork6b5eeb72014-05-09 14:45:00 +0200198 /* Some applications using e.g. packet sockets will
199 * bypass the VLAN acceleration and create tagged
200 * ethernet frames directly. We primarily look for
201 * the accelerated out-of-band tag, but fall back if
202 * required
203 */
204 skb_reset_mac_header(skb);
205 if (vlan_get_tag(skb, &tci) < 0 && skb->len > VLAN_ETH_HLEN &&
206 __vlan_get_tag(skb, &tci) == 0) {
207 is_ip = is_ip_proto(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
208 skb_pull(skb, VLAN_ETH_HLEN);
209 } else {
210 is_ip = is_ip_proto(eth_hdr(skb)->h_proto);
211 skb_pull(skb, ETH_HLEN);
212 }
213
Bjørn Mork146a08d2014-05-11 10:47:12 +0200214 /* Is IP session <0> tagged too? */
215 if (info->flags & FLAG_IPS0_VLAN) {
216 /* drop all untagged packets */
217 if (!tci)
218 goto error;
219 /* map MBIM_IPS0_VID to IPS<0> */
220 if (tci == MBIM_IPS0_VID)
221 tci = 0;
222 }
223
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000224 /* mapping VLANs to MBIM sessions:
Bjørn Mork146a08d2014-05-11 10:47:12 +0200225 * no tag => IPS session <0> if !FLAG_IPS0_VLAN
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000226 * 1 - 255 => IPS session <vlanid>
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000227 * 256 - 511 => DSS session <vlanid - 256>
Bjørn Mork146a08d2014-05-11 10:47:12 +0200228 * 512 - 4093 => unsupported, drop
229 * 4094 => IPS session <0> if FLAG_IPS0_VLAN
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000230 */
Bjørn Mork146a08d2014-05-11 10:47:12 +0200231
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000232 switch (tci & 0x0f00) {
233 case 0x0000: /* VLAN ID 0 - 255 */
Bjørn Mork6b5eeb72014-05-09 14:45:00 +0200234 if (!is_ip)
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000235 goto error;
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000236 c = (u8 *)&sign;
237 c[3] = tci;
238 break;
239 case 0x0100: /* VLAN ID 256 - 511 */
240 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000241 c = (u8 *)&sign;
242 c[3] = tci;
243 break;
244 default:
245 netif_err(dev, tx_err, dev->net,
246 "unsupported tci=0x%04x\n", tci);
247 goto error;
248 }
Greg Suarez9bf211a32012-10-22 10:56:36 +0000249 }
250
251 spin_lock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100252 skb_out = cdc_ncm_fill_tx_frame(dev, skb, sign);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000253 spin_unlock_bh(&ctx->mtx);
254 return skb_out;
255
256error:
257 if (skb)
258 dev_kfree_skb_any(skb);
259
260 return NULL;
261}
262
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100263/* Some devices are known to send Neigbor Solicitation messages and
264 * require Neigbor Advertisement replies. The IPv6 core will not
265 * respond since IFF_NOARP is set, so we must handle them ourselves.
266 */
267static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
268{
269 struct ipv6hdr *iph = (void *)buf;
270 struct nd_msg *msg = (void *)(iph + 1);
271 struct net_device *netdev;
272 struct inet6_dev *in6_dev;
273 bool is_router;
274
275 /* we'll only respond to requests from unicast addresses to
276 * our solicited node addresses.
277 */
278 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
279 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
280 return;
281
282 /* need to send the NA on the VLAN dev, if any */
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200283 rcu_read_lock();
284 if (tci) {
dingtianhongf06c7f9f2014-05-09 14:58:05 +0800285 netdev = __vlan_find_dev_deep_rcu(dev->net, htons(ETH_P_8021Q),
286 tci);
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200287 if (!netdev) {
288 rcu_read_unlock();
289 return;
290 }
291 } else {
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100292 netdev = dev->net;
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200293 }
294 dev_hold(netdev);
295 rcu_read_unlock();
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100296
297 in6_dev = in6_dev_get(netdev);
298 if (!in6_dev)
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200299 goto out;
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100300 is_router = !!in6_dev->cnf.forwarding;
301 in6_dev_put(in6_dev);
302
303 /* ipv6_stub != NULL if in6_dev_get returned an inet6_dev */
304 ipv6_stub->ndisc_send_na(netdev, NULL, &iph->saddr, &msg->target,
305 is_router /* router */,
306 true /* solicited */,
307 false /* override */,
308 true /* inc_opt */);
Bjørn Mork4f4178f2014-05-03 16:12:47 +0200309out:
310 dev_put(netdev);
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100311}
312
313static bool is_neigh_solicit(u8 *buf, size_t len)
314{
315 struct ipv6hdr *iph = (void *)buf;
316 struct nd_msg *msg = (void *)(iph + 1);
317
318 return (len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
319 iph->nexthdr == IPPROTO_ICMPV6 &&
320 msg->icmph.icmp6_code == 0 &&
321 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION);
322}
323
324
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000325static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000326{
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000327 __be16 proto = htons(ETH_P_802_3);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000328 struct sk_buff *skb = NULL;
329
Bjørn Mork146a08d2014-05-11 10:47:12 +0200330 if (tci < 256 || tci == MBIM_IPS0_VID) { /* IPS session? */
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000331 if (len < sizeof(struct iphdr))
332 goto err;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000333
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000334 switch (*buf & 0xf0) {
335 case 0x40:
336 proto = htons(ETH_P_IP);
337 break;
338 case 0x60:
Bjørn Mork5b8f15f2013-10-31 15:56:10 +0100339 if (is_neigh_solicit(buf, len))
340 do_neigh_solicit(dev, buf, tci);
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000341 proto = htons(ETH_P_IPV6);
342 break;
343 default:
344 goto err;
345 }
Greg Suarez9bf211a32012-10-22 10:56:36 +0000346 }
347
348 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
349 if (!skb)
350 goto err;
351
352 /* add an ethernet header */
353 skb_put(skb, ETH_HLEN);
354 skb_reset_mac_header(skb);
355 eth_hdr(skb)->h_proto = proto;
356 memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
357 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
358
359 /* add datagram */
360 memcpy(skb_put(skb, len), buf, len);
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000361
362 /* map MBIM session to VLAN */
363 if (tci)
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000364 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000365err:
366 return skb;
367}
368
369static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
370{
371 struct sk_buff *skb;
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000372 struct cdc_mbim_state *info = (void *)&dev->data;
373 struct cdc_ncm_ctx *ctx = info->ctx;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000374 int len;
375 int nframes;
376 int x;
377 int offset;
378 struct usb_cdc_ncm_ndp16 *ndp16;
379 struct usb_cdc_ncm_dpe16 *dpe16;
380 int ndpoffset;
381 int loopcount = 50; /* arbitrary max preventing infinite loop */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000382 u8 *c;
383 u16 tci;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000384
385 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
386 if (ndpoffset < 0)
387 goto error;
388
389next_ndp:
390 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
391 if (nframes < 0)
392 goto error;
393
394 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
395
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000396 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000397 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000398 c = (u8 *)&ndp16->dwSignature;
399 tci = c[3];
Bjørn Mork146a08d2014-05-11 10:47:12 +0200400 /* tag IPS<0> packets too if MBIM_IPS0_VID exists */
401 if (!tci && info->flags & FLAG_IPS0_VLAN)
402 tci = MBIM_IPS0_VID;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000403 break;
Bjørn Mork9f651ca2012-10-22 10:56:40 +0000404 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
405 c = (u8 *)&ndp16->dwSignature;
406 tci = c[3] + 256;
407 break;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000408 default:
409 netif_dbg(dev, rx_err, dev->net,
410 "unsupported NDP signature <0x%08x>\n",
411 le32_to_cpu(ndp16->dwSignature));
412 goto err_ndp;
413
414 }
415
416 dpe16 = ndp16->dpe16;
417 for (x = 0; x < nframes; x++, dpe16++) {
418 offset = le16_to_cpu(dpe16->wDatagramIndex);
419 len = le16_to_cpu(dpe16->wDatagramLength);
420
421 /*
422 * CDC NCM ch. 3.7
423 * All entries after first NULL entry are to be ignored
424 */
425 if ((offset == 0) || (len == 0)) {
426 if (!x)
427 goto err_ndp; /* empty NTB */
428 break;
429 }
430
431 /* sanity checking */
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000432 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
Greg Suarez9bf211a32012-10-22 10:56:36 +0000433 netif_dbg(dev, rx_err, dev->net,
434 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
435 x, offset, len, skb_in);
436 if (!x)
437 goto err_ndp;
438 break;
439 } else {
Bjørn Morka82c7ce2012-10-22 10:56:39 +0000440 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
Greg Suarez9bf211a32012-10-22 10:56:36 +0000441 if (!skb)
442 goto error;
443 usbnet_skb_return(dev, skb);
444 }
445 }
446err_ndp:
447 /* are there more NDPs to process? */
448 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
449 if (ndpoffset && loopcount--)
450 goto next_ndp;
451
452 return 1;
453error:
454 return 0;
455}
456
457static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
458{
Bjørn Morke62416e2013-11-01 14:18:56 +0100459 int ret = -ENODEV;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000460 struct usbnet *dev = usb_get_intfdata(intf);
461 struct cdc_mbim_state *info = (void *)&dev->data;
462 struct cdc_ncm_ctx *ctx = info->ctx;
463
Bjørn Morke62416e2013-11-01 14:18:56 +0100464 if (!ctx)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000465 goto error;
Greg Suarez9bf211a32012-10-22 10:56:36 +0000466
Ming Lei9a443122013-03-15 12:08:56 +0800467 /*
468 * Both usbnet_suspend() and subdriver->suspend() MUST return 0
469 * in system sleep context, otherwise, the resume callback has
470 * to recover device from previous suspend failure.
471 */
Greg Suarez9bf211a32012-10-22 10:56:36 +0000472 ret = usbnet_suspend(intf, message);
473 if (ret < 0)
474 goto error;
475
476 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
477 ret = info->subdriver->suspend(intf, message);
478 if (ret < 0)
479 usbnet_resume(intf);
480
481error:
482 return ret;
483}
484
485static int cdc_mbim_resume(struct usb_interface *intf)
486{
487 int ret = 0;
488 struct usbnet *dev = usb_get_intfdata(intf);
489 struct cdc_mbim_state *info = (void *)&dev->data;
490 struct cdc_ncm_ctx *ctx = info->ctx;
491 bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
492
493 if (callsub)
494 ret = info->subdriver->resume(intf);
495 if (ret < 0)
496 goto err;
497 ret = usbnet_resume(intf);
Bjørn Morkc37ac9b2013-11-01 14:18:55 +0100498 if (ret < 0 && callsub)
Greg Suarez9bf211a32012-10-22 10:56:36 +0000499 info->subdriver->suspend(intf, PMSG_SUSPEND);
500err:
501 return ret;
502}
503
504static const struct driver_info cdc_mbim_info = {
505 .description = "CDC MBIM",
Bjørn Mork844e88f2013-01-23 00:57:02 +0000506 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
507 .bind = cdc_mbim_bind,
508 .unbind = cdc_mbim_unbind,
509 .manage_power = cdc_mbim_manage_power,
510 .rx_fixup = cdc_mbim_rx_fixup,
511 .tx_fixup = cdc_mbim_tx_fixup,
512};
513
514/* MBIM and NCM devices should not need a ZLP after NTBs with
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100515 * dwNtbOutMaxSize length. Nevertheless, a number of devices from
516 * different vendor IDs will fail unless we send ZLPs, forcing us
517 * to make this the default.
518 *
519 * This default may cause a performance penalty for spec conforming
520 * devices wanting to take advantage of optimizations possible without
521 * ZLPs. A whitelist is added in an attempt to avoid this for devices
522 * known to conform to the MBIM specification.
523 *
524 * All known devices supporting NCM compatibility mode are also
525 * conforming to the NCM and MBIM specifications. For this reason, the
526 * NCM subclass entry is also in the ZLP whitelist.
Bjørn Mork844e88f2013-01-23 00:57:02 +0000527 */
528static const struct driver_info cdc_mbim_info_zlp = {
529 .description = "CDC MBIM",
Bjørn Mork328d7b82013-01-21 05:50:39 +0000530 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
Greg Suarez9bf211a32012-10-22 10:56:36 +0000531 .bind = cdc_mbim_bind,
532 .unbind = cdc_mbim_unbind,
533 .manage_power = cdc_mbim_manage_power,
534 .rx_fixup = cdc_mbim_rx_fixup,
535 .tx_fixup = cdc_mbim_tx_fixup,
536};
537
538static const struct usb_device_id mbim_devs[] = {
539 /* This duplicate NCM entry is intentional. MBIM devices can
540 * be disguised as NCM by default, and this is necessary to
541 * allow us to bind the correct driver_info to such devices.
542 *
543 * bind() will sort out this for us, selecting the correct
544 * entry and reject the other
545 */
546 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
547 .driver_info = (unsigned long)&cdc_mbim_info,
548 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100549 /* ZLP conformance whitelist: All Ericsson MBIM devices */
550 { USB_VENDOR_AND_INTERFACE_INFO(0x0bdb, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
Greg Suarez9bf211a32012-10-22 10:56:36 +0000551 .driver_info = (unsigned long)&cdc_mbim_info,
552 },
Bjørn Morkc1a2e9542013-10-31 15:56:11 +0100553 /* default entry */
554 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
555 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
556 },
Greg Suarez9bf211a32012-10-22 10:56:36 +0000557 {
558 },
559};
560MODULE_DEVICE_TABLE(usb, mbim_devs);
561
562static struct usb_driver cdc_mbim_driver = {
563 .name = "cdc_mbim",
564 .id_table = mbim_devs,
565 .probe = usbnet_probe,
566 .disconnect = usbnet_disconnect,
567 .suspend = cdc_mbim_suspend,
568 .resume = cdc_mbim_resume,
569 .reset_resume = cdc_mbim_resume,
570 .supports_autosuspend = 1,
571 .disable_hub_initiated_lpm = 1,
572};
573module_usb_driver(cdc_mbim_driver);
574
575MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
576MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
577MODULE_DESCRIPTION("USB CDC MBIM host driver");
578MODULE_LICENSE("GPL");