2 * Copyright (c) 2012 Smith Micro Software, Inc.
3 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
5 * This driver is based on and reuse most of cdc_ncm, which is
6 * Copyright (C) ST-Ericsson 2010-2012
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.
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/if_vlan.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>
25 #include <net/addrconf.h>
27 /* driver specific data - must match cdc_ncm usage */
28 struct cdc_mbim_state {
29 struct cdc_ncm_ctx *ctx;
31 struct usb_driver *subdriver;
32 struct usb_interface *control;
33 struct usb_interface *data;
36 /* using a counter to merge subdriver requests with our own into a combined state */
37 static int cdc_mbim_manage_power(struct usbnet *dev, int on)
39 struct cdc_mbim_state *info = (void *)&dev->data;
42 dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
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);
49 dev->intf->needs_remote_wakeup = on;
50 usb_autopm_put_interface(dev->intf);
56 static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
58 struct usbnet *dev = usb_get_intfdata(intf);
60 /* can be called while disconnecting */
64 return cdc_mbim_manage_power(dev, status);
68 static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
70 struct cdc_ncm_ctx *ctx;
71 struct usb_driver *subdriver = ERR_PTR(-ENODEV);
73 u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf);
74 struct cdc_mbim_state *info = (void *)&dev->data;
76 /* Probably NCM, defer for cdc_ncm_bind */
77 if (!cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
80 ret = cdc_ncm_bind_common(dev, intf, data_altsetting);
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,
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);
98 /* can't let usbnet use the interrupt endpoint */
100 info->subdriver = subdriver;
102 /* MBIM cannot do ARP */
103 dev->net->flags |= IFF_NOARP;
105 /* no need to put the VLAN tci in the packet headers */
106 dev->net->features |= NETIF_F_HW_VLAN_CTAG_TX;
111 static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
113 struct cdc_mbim_state *info = (void *)&dev->data;
114 struct cdc_ncm_ctx *ctx = info->ctx;
116 /* disconnect subdriver from control interface */
117 if (info->subdriver && info->subdriver->disconnect)
118 info->subdriver->disconnect(ctx->control);
119 info->subdriver = NULL;
121 /* let NCM unbind clean up both control and data interface */
122 cdc_ncm_unbind(dev, intf);
126 static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
128 struct sk_buff *skb_out;
129 struct cdc_mbim_state *info = (void *)&dev->data;
130 struct cdc_ncm_ctx *ctx = info->ctx;
131 __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
139 if (skb->len <= ETH_HLEN)
142 /* mapping VLANs to MBIM sessions:
143 * no tag => IPS session <0>
144 * 1 - 255 => IPS session <vlanid>
145 * 256 - 511 => DSS session <vlanid - 256>
146 * 512 - 4095 => unsupported, drop
148 vlan_get_tag(skb, &tci);
150 switch (tci & 0x0f00) {
151 case 0x0000: /* VLAN ID 0 - 255 */
152 /* 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):
164 case 0x0100: /* VLAN ID 256 - 511 */
165 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN);
170 netif_err(dev, tx_err, dev->net,
171 "unsupported tci=0x%04x\n", tci);
174 skb_pull(skb, ETH_HLEN);
177 spin_lock_bh(&ctx->mtx);
178 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign);
179 spin_unlock_bh(&ctx->mtx);
184 dev_kfree_skb_any(skb);
189 /* 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.
193 static void do_neigh_solicit(struct usbnet *dev, u8 *buf, u16 tci)
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;
201 /* we'll only respond to requests from unicast addresses to
202 * our solicited node addresses.
204 if (!ipv6_addr_is_solict_mult(&iph->daddr) ||
205 !(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST))
208 /* need to send the NA on the VLAN dev, if any */
210 netdev = __vlan_find_dev_deep(dev->net, htons(ETH_P_8021Q),
217 in6_dev = in6_dev_get(netdev);
220 is_router = !!in6_dev->cnf.forwarding;
221 in6_dev_put(in6_dev);
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 */,
231 static bool is_neigh_solicit(u8 *buf, size_t len)
233 struct ipv6hdr *iph = (void *)buf;
234 struct nd_msg *msg = (void *)(iph + 1);
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);
243 static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci)
245 __be16 proto = htons(ETH_P_802_3);
246 struct sk_buff *skb = NULL;
248 if (tci < 256) { /* IPS session? */
249 if (len < sizeof(struct iphdr))
252 switch (*buf & 0xf0) {
254 proto = htons(ETH_P_IP);
257 if (is_neigh_solicit(buf, len))
258 do_neigh_solicit(dev, buf, tci);
259 proto = htons(ETH_P_IPV6);
266 skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN);
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);
278 memcpy(skb_put(skb, len), buf, len);
280 /* map MBIM session to VLAN */
282 vlan_put_tag(skb, htons(ETH_P_8021Q), tci);
287 static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
290 struct cdc_mbim_state *info = (void *)&dev->data;
291 struct cdc_ncm_ctx *ctx = info->ctx;
296 struct usb_cdc_ncm_ndp16 *ndp16;
297 struct usb_cdc_ncm_dpe16 *dpe16;
299 int loopcount = 50; /* arbitrary max preventing infinite loop */
303 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
308 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
312 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
314 switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) {
315 case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN):
316 c = (u8 *)&ndp16->dwSignature;
319 case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN):
320 c = (u8 *)&ndp16->dwSignature;
324 netif_dbg(dev, rx_err, dev->net,
325 "unsupported NDP signature <0x%08x>\n",
326 le32_to_cpu(ndp16->dwSignature));
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);
338 * All entries after first NULL entry are to be ignored
340 if ((offset == 0) || (len == 0)) {
342 goto err_ndp; /* empty NTB */
346 /* sanity checking */
347 if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) {
348 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);
355 skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci);
358 usbnet_skb_return(dev, skb);
362 /* are there more NDPs to process? */
363 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
364 if (ndpoffset && loopcount--)
372 static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
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;
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.
389 ret = usbnet_suspend(intf, message);
393 if (intf == ctx->control && info->subdriver && info->subdriver->suspend)
394 ret = info->subdriver->suspend(intf, message);
402 static int cdc_mbim_resume(struct usb_interface *intf)
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);
411 ret = info->subdriver->resume(intf);
414 ret = usbnet_resume(intf);
415 if (ret < 0 && callsub && info->subdriver->suspend)
416 info->subdriver->suspend(intf, PMSG_SUSPEND);
421 static const struct driver_info cdc_mbim_info = {
422 .description = "CDC MBIM",
423 .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,
431 /* MBIM and NCM devices should not need a ZLP after NTBs with
432 * dwNtbOutMaxSize length. This driver_info is for the exceptional
433 * devices requiring it anyway, allowing them to be supported without
434 * forcing the performance penalty on all the sane devices.
436 static const struct driver_info cdc_mbim_info_zlp = {
437 .description = "CDC MBIM",
438 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP,
439 .bind = cdc_mbim_bind,
440 .unbind = cdc_mbim_unbind,
441 .manage_power = cdc_mbim_manage_power,
442 .rx_fixup = cdc_mbim_rx_fixup,
443 .tx_fixup = cdc_mbim_tx_fixup,
446 static const struct usb_device_id mbim_devs[] = {
447 /* This duplicate NCM entry is intentional. MBIM devices can
448 * be disguised as NCM by default, and this is necessary to
449 * allow us to bind the correct driver_info to such devices.
451 * bind() will sort out this for us, selecting the correct
452 * entry and reject the other
454 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
455 .driver_info = (unsigned long)&cdc_mbim_info,
457 /* Sierra Wireless MC7710 need ZLPs */
458 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68a2, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
459 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
461 /* HP hs2434 Mobile Broadband Module needs ZLPs */
462 { USB_DEVICE_AND_INTERFACE_INFO(0x3f0, 0x4b1d, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
463 .driver_info = (unsigned long)&cdc_mbim_info_zlp,
465 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE),
466 .driver_info = (unsigned long)&cdc_mbim_info,
471 MODULE_DEVICE_TABLE(usb, mbim_devs);
473 static struct usb_driver cdc_mbim_driver = {
475 .id_table = mbim_devs,
476 .probe = usbnet_probe,
477 .disconnect = usbnet_disconnect,
478 .suspend = cdc_mbim_suspend,
479 .resume = cdc_mbim_resume,
480 .reset_resume = cdc_mbim_resume,
481 .supports_autosuspend = 1,
482 .disable_hub_initiated_lpm = 1,
484 module_usb_driver(cdc_mbim_driver);
486 MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>");
487 MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>");
488 MODULE_DESCRIPTION("USB CDC MBIM host driver");
489 MODULE_LICENSE("GPL");