Commit | Line | Data |
---|---|---|
9bf211a3 GS |
1 | /* |
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> | |
a82c7ce5 | 16 | #include <linux/if_vlan.h> |
9bf211a3 GS |
17 | #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> | |
24 | ||
25 | /* driver specific data - must match cdc_ncm usage */ | |
26 | struct cdc_mbim_state { | |
27 | struct cdc_ncm_ctx *ctx; | |
28 | atomic_t pmcount; | |
29 | struct usb_driver *subdriver; | |
30 | struct usb_interface *control; | |
31 | struct usb_interface *data; | |
32 | }; | |
33 | ||
34 | /* using a counter to merge subdriver requests with our own into a combined state */ | |
35 | static int cdc_mbim_manage_power(struct usbnet *dev, int on) | |
36 | { | |
37 | struct cdc_mbim_state *info = (void *)&dev->data; | |
38 | int rv = 0; | |
39 | ||
40 | dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on); | |
41 | ||
42 | if ((on && atomic_add_return(1, &info->pmcount) == 1) || (!on && atomic_dec_and_test(&info->pmcount))) { | |
43 | /* need autopm_get/put here to ensure the usbcore sees the new value */ | |
44 | rv = usb_autopm_get_interface(dev->intf); | |
45 | if (rv < 0) | |
46 | goto err; | |
47 | dev->intf->needs_remote_wakeup = on; | |
48 | usb_autopm_put_interface(dev->intf); | |
49 | } | |
50 | err: | |
51 | return rv; | |
52 | } | |
53 | ||
54 | static 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 | ||
66 | static 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; | |
1e8bbe6c | 71 | u8 data_altsetting = cdc_ncm_select_altsetting(dev, intf); |
9bf211a3 GS |
72 | struct cdc_mbim_state *info = (void *)&dev->data; |
73 | ||
9bf211a3 GS |
74 | /* 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; | |
a82c7ce5 BM |
102 | |
103 | /* no need to put the VLAN tci in the packet headers */ | |
104 | dev->net->features |= NETIF_F_HW_VLAN_TX; | |
9bf211a3 GS |
105 | err: |
106 | return ret; | |
107 | } | |
108 | ||
109 | static 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 | ||
124 | static 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; | |
a82c7ce5 BM |
129 | __le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN); |
130 | u16 tci = 0; | |
131 | u8 *c; | |
9bf211a3 GS |
132 | |
133 | if (!ctx) | |
134 | goto error; | |
135 | ||
136 | if (skb) { | |
137 | if (skb->len <= sizeof(ETH_HLEN)) | |
138 | goto error; | |
139 | ||
a82c7ce5 BM |
140 | /* mapping VLANs to MBIM sessions: |
141 | * no tag => IPS session <0> | |
142 | * 1 - 255 => IPS session <vlanid> | |
9f651cac BM |
143 | * 256 - 511 => DSS session <vlanid - 256> |
144 | * 512 - 4095 => unsupported, drop | |
a82c7ce5 BM |
145 | */ |
146 | vlan_get_tag(skb, &tci); | |
147 | ||
148 | switch (tci & 0x0f00) { | |
149 | case 0x0000: /* VLAN ID 0 - 255 */ | |
9f651cac BM |
150 | /* 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); | |
a82c7ce5 BM |
164 | 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 | } | |
9f651cac | 172 | skb_pull(skb, ETH_HLEN); |
9bf211a3 GS |
173 | } |
174 | ||
175 | spin_lock_bh(&ctx->mtx); | |
a82c7ce5 | 176 | skb_out = cdc_ncm_fill_tx_frame(ctx, skb, sign); |
9bf211a3 GS |
177 | spin_unlock_bh(&ctx->mtx); |
178 | return skb_out; | |
179 | ||
180 | error: | |
181 | if (skb) | |
182 | dev_kfree_skb_any(skb); | |
183 | ||
184 | return NULL; | |
185 | } | |
186 | ||
a82c7ce5 | 187 | static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_t len, u16 tci) |
9bf211a3 | 188 | { |
9f651cac | 189 | __be16 proto = htons(ETH_P_802_3); |
9bf211a3 GS |
190 | struct sk_buff *skb = NULL; |
191 | ||
9f651cac BM |
192 | if (tci < 256) { /* IPS session? */ |
193 | if (len < sizeof(struct iphdr)) | |
194 | goto err; | |
a82c7ce5 | 195 | |
9f651cac BM |
196 | switch (*buf & 0xf0) { |
197 | case 0x40: | |
198 | proto = htons(ETH_P_IP); | |
199 | break; | |
200 | case 0x60: | |
201 | proto = htons(ETH_P_IPV6); | |
202 | break; | |
203 | default: | |
204 | goto err; | |
205 | } | |
9bf211a3 GS |
206 | } |
207 | ||
208 | skb = netdev_alloc_skb_ip_align(dev->net, len + ETH_HLEN); | |
209 | if (!skb) | |
210 | goto err; | |
211 | ||
212 | /* add an ethernet header */ | |
213 | skb_put(skb, ETH_HLEN); | |
214 | skb_reset_mac_header(skb); | |
215 | eth_hdr(skb)->h_proto = proto; | |
216 | memset(eth_hdr(skb)->h_source, 0, ETH_ALEN); | |
217 | memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN); | |
218 | ||
219 | /* add datagram */ | |
220 | memcpy(skb_put(skb, len), buf, len); | |
a82c7ce5 BM |
221 | |
222 | /* map MBIM session to VLAN */ | |
223 | if (tci) | |
224 | vlan_put_tag(skb, tci); | |
9bf211a3 GS |
225 | err: |
226 | return skb; | |
227 | } | |
228 | ||
229 | static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in) | |
230 | { | |
231 | struct sk_buff *skb; | |
a82c7ce5 BM |
232 | struct cdc_mbim_state *info = (void *)&dev->data; |
233 | struct cdc_ncm_ctx *ctx = info->ctx; | |
9bf211a3 GS |
234 | int len; |
235 | int nframes; | |
236 | int x; | |
237 | int offset; | |
238 | struct usb_cdc_ncm_ndp16 *ndp16; | |
239 | struct usb_cdc_ncm_dpe16 *dpe16; | |
240 | int ndpoffset; | |
241 | int loopcount = 50; /* arbitrary max preventing infinite loop */ | |
a82c7ce5 BM |
242 | u8 *c; |
243 | u16 tci; | |
9bf211a3 GS |
244 | |
245 | ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in); | |
246 | if (ndpoffset < 0) | |
247 | goto error; | |
248 | ||
249 | next_ndp: | |
250 | nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset); | |
251 | if (nframes < 0) | |
252 | goto error; | |
253 | ||
254 | ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset); | |
255 | ||
a82c7ce5 | 256 | switch (ndp16->dwSignature & cpu_to_le32(0x00ffffff)) { |
9bf211a3 | 257 | case cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN): |
a82c7ce5 BM |
258 | c = (u8 *)&ndp16->dwSignature; |
259 | tci = c[3]; | |
9bf211a3 | 260 | break; |
9f651cac BM |
261 | case cpu_to_le32(USB_CDC_MBIM_NDP16_DSS_SIGN): |
262 | c = (u8 *)&ndp16->dwSignature; | |
263 | tci = c[3] + 256; | |
264 | break; | |
9bf211a3 GS |
265 | default: |
266 | netif_dbg(dev, rx_err, dev->net, | |
267 | "unsupported NDP signature <0x%08x>\n", | |
268 | le32_to_cpu(ndp16->dwSignature)); | |
269 | goto err_ndp; | |
270 | ||
271 | } | |
272 | ||
273 | dpe16 = ndp16->dpe16; | |
274 | for (x = 0; x < nframes; x++, dpe16++) { | |
275 | offset = le16_to_cpu(dpe16->wDatagramIndex); | |
276 | len = le16_to_cpu(dpe16->wDatagramLength); | |
277 | ||
278 | /* | |
279 | * CDC NCM ch. 3.7 | |
280 | * All entries after first NULL entry are to be ignored | |
281 | */ | |
282 | if ((offset == 0) || (len == 0)) { | |
283 | if (!x) | |
284 | goto err_ndp; /* empty NTB */ | |
285 | break; | |
286 | } | |
287 | ||
288 | /* sanity checking */ | |
a82c7ce5 | 289 | if (((offset + len) > skb_in->len) || (len > ctx->rx_max)) { |
9bf211a3 GS |
290 | netif_dbg(dev, rx_err, dev->net, |
291 | "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n", | |
292 | x, offset, len, skb_in); | |
293 | if (!x) | |
294 | goto err_ndp; | |
295 | break; | |
296 | } else { | |
a82c7ce5 | 297 | skb = cdc_mbim_process_dgram(dev, skb_in->data + offset, len, tci); |
9bf211a3 GS |
298 | if (!skb) |
299 | goto error; | |
300 | usbnet_skb_return(dev, skb); | |
301 | } | |
302 | } | |
303 | err_ndp: | |
304 | /* are there more NDPs to process? */ | |
305 | ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex); | |
306 | if (ndpoffset && loopcount--) | |
307 | goto next_ndp; | |
308 | ||
309 | return 1; | |
310 | error: | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message) | |
315 | { | |
316 | int ret = 0; | |
317 | struct usbnet *dev = usb_get_intfdata(intf); | |
318 | struct cdc_mbim_state *info = (void *)&dev->data; | |
319 | struct cdc_ncm_ctx *ctx = info->ctx; | |
320 | ||
321 | if (ctx == NULL) { | |
322 | ret = -1; | |
323 | goto error; | |
324 | } | |
325 | ||
326 | ret = usbnet_suspend(intf, message); | |
327 | if (ret < 0) | |
328 | goto error; | |
329 | ||
330 | if (intf == ctx->control && info->subdriver && info->subdriver->suspend) | |
331 | ret = info->subdriver->suspend(intf, message); | |
332 | if (ret < 0) | |
333 | usbnet_resume(intf); | |
334 | ||
335 | error: | |
336 | return ret; | |
337 | } | |
338 | ||
339 | static int cdc_mbim_resume(struct usb_interface *intf) | |
340 | { | |
341 | int ret = 0; | |
342 | struct usbnet *dev = usb_get_intfdata(intf); | |
343 | struct cdc_mbim_state *info = (void *)&dev->data; | |
344 | struct cdc_ncm_ctx *ctx = info->ctx; | |
345 | bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume); | |
346 | ||
347 | if (callsub) | |
348 | ret = info->subdriver->resume(intf); | |
349 | if (ret < 0) | |
350 | goto err; | |
351 | ret = usbnet_resume(intf); | |
352 | if (ret < 0 && callsub && info->subdriver->suspend) | |
353 | info->subdriver->suspend(intf, PMSG_SUSPEND); | |
354 | err: | |
355 | return ret; | |
356 | } | |
357 | ||
358 | static const struct driver_info cdc_mbim_info = { | |
844e88f0 BM |
359 | .description = "CDC MBIM", |
360 | .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN, | |
361 | .bind = cdc_mbim_bind, | |
362 | .unbind = cdc_mbim_unbind, | |
363 | .manage_power = cdc_mbim_manage_power, | |
364 | .rx_fixup = cdc_mbim_rx_fixup, | |
365 | .tx_fixup = cdc_mbim_tx_fixup, | |
366 | }; | |
367 | ||
368 | /* MBIM and NCM devices should not need a ZLP after NTBs with | |
369 | * dwNtbOutMaxSize length. This driver_info is for the exceptional | |
370 | * devices requiring it anyway, allowing them to be supported without | |
371 | * forcing the performance penalty on all the sane devices. | |
372 | */ | |
373 | static const struct driver_info cdc_mbim_info_zlp = { | |
9bf211a3 | 374 | .description = "CDC MBIM", |
328d7b8a | 375 | .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN | FLAG_SEND_ZLP, |
9bf211a3 GS |
376 | .bind = cdc_mbim_bind, |
377 | .unbind = cdc_mbim_unbind, | |
378 | .manage_power = cdc_mbim_manage_power, | |
379 | .rx_fixup = cdc_mbim_rx_fixup, | |
380 | .tx_fixup = cdc_mbim_tx_fixup, | |
381 | }; | |
382 | ||
383 | static const struct usb_device_id mbim_devs[] = { | |
384 | /* This duplicate NCM entry is intentional. MBIM devices can | |
385 | * be disguised as NCM by default, and this is necessary to | |
386 | * allow us to bind the correct driver_info to such devices. | |
387 | * | |
388 | * bind() will sort out this for us, selecting the correct | |
389 | * entry and reject the other | |
390 | */ | |
391 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE), | |
392 | .driver_info = (unsigned long)&cdc_mbim_info, | |
393 | }, | |
844e88f0 BM |
394 | /* Sierra Wireless MC7710 need ZLPs */ |
395 | { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68a2, USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE), | |
396 | .driver_info = (unsigned long)&cdc_mbim_info_zlp, | |
397 | }, | |
9bf211a3 GS |
398 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_MBIM, USB_CDC_PROTO_NONE), |
399 | .driver_info = (unsigned long)&cdc_mbim_info, | |
400 | }, | |
401 | { | |
402 | }, | |
403 | }; | |
404 | MODULE_DEVICE_TABLE(usb, mbim_devs); | |
405 | ||
406 | static struct usb_driver cdc_mbim_driver = { | |
407 | .name = "cdc_mbim", | |
408 | .id_table = mbim_devs, | |
409 | .probe = usbnet_probe, | |
410 | .disconnect = usbnet_disconnect, | |
411 | .suspend = cdc_mbim_suspend, | |
412 | .resume = cdc_mbim_resume, | |
413 | .reset_resume = cdc_mbim_resume, | |
414 | .supports_autosuspend = 1, | |
415 | .disable_hub_initiated_lpm = 1, | |
416 | }; | |
417 | module_usb_driver(cdc_mbim_driver); | |
418 | ||
419 | MODULE_AUTHOR("Greg Suarez <gsuarez@smithmicro.com>"); | |
420 | MODULE_AUTHOR("Bjørn Mork <bjorn@mork.no>"); | |
421 | MODULE_DESCRIPTION("USB CDC MBIM host driver"); | |
422 | MODULE_LICENSE("GPL"); |