xref: /linux/drivers/net/usb/rndis_host.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Host Side support for RNDIS Networking Links
4  * Copyright (C) 2005 by David Brownell
5  */
6 #include <linux/module.h>
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/workqueue.h>
11 #include <linux/slab.h>
12 #include <linux/mii.h>
13 #include <linux/usb.h>
14 #include <linux/usb/cdc.h>
15 #include <linux/usb/usbnet.h>
16 #include <linux/usb/rndis_host.h>
17 #include <linux/overflow.h>
18 
19 
20 /*
21  * RNDIS is NDIS remoted over USB.  It's a MSFT variant of CDC ACM ... of
22  * course ACM was intended for modems, not Ethernet links!  USB's standard
23  * for Ethernet links is "CDC Ethernet", which is significantly simpler.
24  *
25  * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete.  Issues
26  * include:
27  *    - Power management in particular relies on information that's scattered
28  *	through other documentation, and which is incomplete or incorrect even
29  *	there.
30  *    - There are various undocumented protocol requirements, such as the
31  *	need to send unused garbage in control-OUT messages.
32  *    - In some cases, MS-Windows will emit undocumented requests; this
33  *	matters more to peripheral implementations than host ones.
34  *
35  * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
36  *
37  * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
38  * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
39  * currently rare) "Ethernet Emulation Model" (EEM).
40  */
41 
42 /*
43  * RNDIS notifications from device: command completion; "reverse"
44  * keepalives; etc
45  */
rndis_status(struct usbnet * dev,struct urb * urb)46 void rndis_status(struct usbnet *dev, struct urb *urb)
47 {
48 	netdev_dbg(dev->net, "rndis status urb, len %d stat %d\n",
49 		   urb->actual_length, urb->status);
50 	// FIXME for keepalives, respond immediately (asynchronously)
51 	// if not an RNDIS status, do like cdc_status(dev,urb) does
52 }
53 EXPORT_SYMBOL_GPL(rndis_status);
54 
55 /*
56  * RNDIS indicate messages.
57  */
rndis_msg_indicate(struct usbnet * dev,struct rndis_indicate * msg,int buflen)58 static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg,
59 				int buflen)
60 {
61 	struct cdc_state *info = (void *)&dev->data;
62 	struct device *udev = &info->control->dev;
63 
64 	if (dev->driver_info->indication) {
65 		dev->driver_info->indication(dev, msg, buflen);
66 	} else {
67 		u32 status = le32_to_cpu(msg->status);
68 
69 		switch (status) {
70 		case RNDIS_STATUS_MEDIA_CONNECT:
71 			dev_info(udev, "rndis media connect\n");
72 			break;
73 		case RNDIS_STATUS_MEDIA_DISCONNECT:
74 			dev_info(udev, "rndis media disconnect\n");
75 			break;
76 		default:
77 			dev_info(udev, "rndis indication: 0x%08x\n", status);
78 		}
79 	}
80 }
81 
82 /*
83  * RPC done RNDIS-style.  Caller guarantees:
84  * - message is properly byteswapped
85  * - there's no other request pending
86  * - buf can hold up to 1KB response (required by RNDIS spec)
87  * On return, the first few entries are already byteswapped.
88  *
89  * Call context is likely probe(), before interface name is known,
90  * which is why we won't try to use it in the diagnostics.
91  */
rndis_command(struct usbnet * dev,struct rndis_msg_hdr * buf,int buflen)92 int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen)
93 {
94 	struct cdc_state	*info = (void *) &dev->data;
95 	struct usb_cdc_notification notification;
96 	int			master_ifnum;
97 	int			retval;
98 	int			partial;
99 	unsigned		count;
100 	u32			xid = 0, msg_len, request_id, msg_type, rsp,
101 				status;
102 
103 	/* REVISIT when this gets called from contexts other than probe() or
104 	 * disconnect(): either serialize, or dispatch responses on xid
105 	 */
106 
107 	msg_type = le32_to_cpu(buf->msg_type);
108 
109 	/* Issue the request; xid is unique, don't bother byteswapping it */
110 	if (likely(msg_type != RNDIS_MSG_HALT && msg_type != RNDIS_MSG_RESET)) {
111 		xid = dev->xid++;
112 		if (!xid)
113 			xid = dev->xid++;
114 		buf->request_id = (__force __le32) xid;
115 	}
116 	master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
117 	retval = usb_control_msg(dev->udev,
118 		usb_sndctrlpipe(dev->udev, 0),
119 		USB_CDC_SEND_ENCAPSULATED_COMMAND,
120 		USB_TYPE_CLASS | USB_RECIP_INTERFACE,
121 		0, master_ifnum,
122 		buf, le32_to_cpu(buf->msg_len),
123 		RNDIS_CONTROL_TIMEOUT_MS);
124 	if (unlikely(retval < 0 || xid == 0))
125 		return retval;
126 
127 	/* Some devices don't respond on the control channel until
128 	 * polled on the status channel, so do that first. */
129 	if (dev->driver_info->data & RNDIS_DRIVER_DATA_POLL_STATUS) {
130 		retval = usb_interrupt_msg(
131 			dev->udev,
132 			usb_rcvintpipe(dev->udev,
133 				       dev->status->desc.bEndpointAddress),
134 			&notification, sizeof(notification), &partial,
135 			RNDIS_CONTROL_TIMEOUT_MS);
136 		if (unlikely(retval < 0))
137 			return retval;
138 	}
139 
140 	/* Poll the control channel; the request probably completed immediately */
141 	rsp = le32_to_cpu(buf->msg_type) | RNDIS_MSG_COMPLETION;
142 	for (count = 0; count < 10; count++) {
143 		memset(buf, 0, CONTROL_BUFFER_SIZE);
144 		retval = usb_control_msg(dev->udev,
145 			usb_rcvctrlpipe(dev->udev, 0),
146 			USB_CDC_GET_ENCAPSULATED_RESPONSE,
147 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
148 			0, master_ifnum,
149 			buf, buflen,
150 			RNDIS_CONTROL_TIMEOUT_MS);
151 		if (likely(retval >= 8)) {
152 			msg_type = le32_to_cpu(buf->msg_type);
153 			msg_len = le32_to_cpu(buf->msg_len);
154 			status = le32_to_cpu(buf->status);
155 			request_id = (__force u32) buf->request_id;
156 			if (likely(msg_type == rsp)) {
157 				if (likely(request_id == xid)) {
158 					if (unlikely(rsp == RNDIS_MSG_RESET_C))
159 						return 0;
160 					if (likely(RNDIS_STATUS_SUCCESS ==
161 							status))
162 						return 0;
163 					dev_dbg(&info->control->dev,
164 						"rndis reply status %08x\n",
165 						status);
166 					return -EL3RST;
167 				}
168 				dev_dbg(&info->control->dev,
169 					"rndis reply id %d expected %d\n",
170 					request_id, xid);
171 				/* then likely retry */
172 			} else switch (msg_type) {
173 			case RNDIS_MSG_INDICATE: /* fault/event */
174 				rndis_msg_indicate(dev, (void *)buf, buflen);
175 				break;
176 			case RNDIS_MSG_KEEPALIVE: { /* ping */
177 				struct rndis_keepalive_c *msg = (void *)buf;
178 
179 				msg->msg_type = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
180 				msg->msg_len = cpu_to_le32(sizeof *msg);
181 				msg->status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
182 				retval = usb_control_msg(dev->udev,
183 					usb_sndctrlpipe(dev->udev, 0),
184 					USB_CDC_SEND_ENCAPSULATED_COMMAND,
185 					USB_TYPE_CLASS | USB_RECIP_INTERFACE,
186 					0, master_ifnum,
187 					msg, sizeof *msg,
188 					RNDIS_CONTROL_TIMEOUT_MS);
189 				if (unlikely(retval < 0))
190 					dev_dbg(&info->control->dev,
191 						"rndis keepalive err %d\n",
192 						retval);
193 				}
194 				break;
195 			default:
196 				dev_dbg(&info->control->dev,
197 					"unexpected rndis msg %08x len %d\n",
198 					le32_to_cpu(buf->msg_type), msg_len);
199 			}
200 		} else {
201 			/* device probably issued a protocol stall; ignore */
202 			dev_dbg(&info->control->dev,
203 				"rndis response error, code %d\n", retval);
204 		}
205 		msleep(40);
206 	}
207 	dev_dbg(&info->control->dev, "rndis response timeout\n");
208 	return -ETIMEDOUT;
209 }
210 EXPORT_SYMBOL_GPL(rndis_command);
211 
212 /*
213  * rndis_query:
214  *
215  * Performs a query for @oid along with 0 or more bytes of payload as
216  * specified by @in_len. If @reply_len is not set to -1 then the reply
217  * length is checked against this value, resulting in an error if it
218  * doesn't match.
219  *
220  * NOTE: Adding a payload exactly or greater than the size of the expected
221  * response payload is an evident requirement MSFT added for ActiveSync.
222  *
223  * The only exception is for OIDs that return a variably sized response,
224  * in which case no payload should be added.  This undocumented (and
225  * nonsensical!) issue was found by sniffing protocol requests from the
226  * ActiveSync 4.1 Windows driver.
227  */
rndis_query(struct usbnet * dev,struct usb_interface * intf,void * buf,u32 oid,u32 in_len,void ** reply,int * reply_len)228 static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
229 		void *buf, u32 oid, u32 in_len,
230 		void **reply, int *reply_len)
231 {
232 	int retval;
233 	union {
234 		void			*buf;
235 		struct rndis_msg_hdr	*header;
236 		struct rndis_query	*get;
237 		struct rndis_query_c	*get_c;
238 	} u;
239 	u32 off, len;
240 
241 	u.buf = buf;
242 
243 	memset(u.get, 0, sizeof *u.get + in_len);
244 	u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
245 	u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
246 	u.get->oid = cpu_to_le32(oid);
247 	u.get->len = cpu_to_le32(in_len);
248 	u.get->offset = cpu_to_le32(20);
249 
250 	retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
251 	if (unlikely(retval < 0)) {
252 		dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
253 				oid, retval);
254 		return retval;
255 	}
256 
257 	off = le32_to_cpu(u.get_c->offset);
258 	len = le32_to_cpu(u.get_c->len);
259 	if (unlikely((off > CONTROL_BUFFER_SIZE - 8) ||
260 		     (len > CONTROL_BUFFER_SIZE - 8 - off)))
261 		goto response_error;
262 
263 	if (*reply_len != -1 && len != *reply_len)
264 		goto response_error;
265 
266 	*reply = (unsigned char *) &u.get_c->request_id + off;
267 	*reply_len = len;
268 
269 	return retval;
270 
271 response_error:
272 	dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
273 			"invalid response - off %d len %d\n",
274 		oid, off, len);
275 	return -EDOM;
276 }
277 
278 /* same as usbnet_netdev_ops but MTU change not allowed */
279 static const struct net_device_ops rndis_netdev_ops = {
280 	.ndo_open		= usbnet_open,
281 	.ndo_stop		= usbnet_stop,
282 	.ndo_start_xmit		= usbnet_start_xmit,
283 	.ndo_tx_timeout		= usbnet_tx_timeout,
284 	.ndo_get_stats64	= dev_get_tstats64,
285 	.ndo_set_mac_address 	= eth_mac_addr,
286 	.ndo_validate_addr	= eth_validate_addr,
287 };
288 
289 int
generic_rndis_bind(struct usbnet * dev,struct usb_interface * intf,int flags)290 generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
291 {
292 	int			retval;
293 	struct net_device	*net = dev->net;
294 	struct cdc_state	*info = (void *) &dev->data;
295 	union {
296 		void			*buf;
297 		struct rndis_msg_hdr	*header;
298 		struct rndis_init	*init;
299 		struct rndis_init_c	*init_c;
300 		struct rndis_query	*get;
301 		struct rndis_query_c	*get_c;
302 		struct rndis_set	*set;
303 		struct rndis_set_c	*set_c;
304 		struct rndis_halt	*halt;
305 	} u;
306 	u32			tmp;
307 	__le32			phym_unspec, *phym;
308 	int			reply_len;
309 	unsigned char		*bp;
310 
311 	/* we can't rely on i/o from stack working, or stack allocation */
312 	u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
313 	if (!u.buf)
314 		return -ENOMEM;
315 	retval = usbnet_generic_cdc_bind(dev, intf);
316 	if (retval < 0)
317 		goto fail;
318 
319 	u.init->msg_type = cpu_to_le32(RNDIS_MSG_INIT);
320 	u.init->msg_len = cpu_to_le32(sizeof *u.init);
321 	u.init->major_version = cpu_to_le32(1);
322 	u.init->minor_version = cpu_to_le32(0);
323 
324 	/* max transfer (in spec) is 0x4000 at full speed, but for
325 	 * TX we'll stick to one Ethernet packet plus RNDIS framing.
326 	 * For RX we handle drivers that zero-pad to end-of-packet.
327 	 * Don't let userspace change these settings.
328 	 *
329 	 * NOTE: there still seems to be weirdness here, as if we need
330 	 * to do some more things to make sure WinCE targets accept this.
331 	 * They default to jumbograms of 8KB or 16KB, which is absurd
332 	 * for such low data rates and which is also more than Linux
333 	 * can usually expect to allocate for SKB data...
334 	 */
335 	net->hard_header_len += sizeof (struct rndis_data_hdr);
336 	dev->hard_mtu = net->mtu + net->hard_header_len;
337 
338 	dev->maxpacket = usb_maxpacket(dev->udev, dev->out);
339 	if (dev->maxpacket == 0) {
340 		netif_dbg(dev, probe, dev->net,
341 			  "dev->maxpacket can't be 0\n");
342 		retval = -EINVAL;
343 		goto fail_and_release;
344 	}
345 
346 	dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
347 	dev->rx_urb_size &= ~(dev->maxpacket - 1);
348 	u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
349 
350 	net->netdev_ops = &rndis_netdev_ops;
351 
352 	retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
353 	if (unlikely(retval < 0)) {
354 		/* it might not even be an RNDIS device!! */
355 		dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
356 		goto fail_and_release;
357 	}
358 	tmp = le32_to_cpu(u.init_c->max_transfer_size);
359 	if (tmp < dev->hard_mtu) {
360 		if (tmp <= net->hard_header_len) {
361 			dev_err(&intf->dev,
362 				"dev can't take %u byte packets (max %u)\n",
363 				dev->hard_mtu, tmp);
364 			retval = -EINVAL;
365 			goto halt_fail_and_release;
366 		}
367 		dev_warn(&intf->dev,
368 			 "dev can't take %u byte packets (max %u), "
369 			 "adjusting MTU to %u\n",
370 			 dev->hard_mtu, tmp, tmp - net->hard_header_len);
371 		dev->hard_mtu = tmp;
372 		net->mtu = dev->hard_mtu - net->hard_header_len;
373 	}
374 
375 	/* REVISIT:  peripheral "alignment" request is ignored ... */
376 	dev_dbg(&intf->dev,
377 		"hard mtu %u (%u from dev), rx buflen %zu, align %d\n",
378 		dev->hard_mtu, tmp, dev->rx_urb_size,
379 		1 << le32_to_cpu(u.init_c->packet_alignment));
380 
381 	/* module has some device initialization code needs to be done right
382 	 * after RNDIS_INIT */
383 	if (dev->driver_info->early_init &&
384 			dev->driver_info->early_init(dev) != 0)
385 		goto halt_fail_and_release;
386 
387 	/* Check physical medium */
388 	phym = NULL;
389 	reply_len = sizeof *phym;
390 	retval = rndis_query(dev, intf, u.buf,
391 			     RNDIS_OID_GEN_PHYSICAL_MEDIUM,
392 			     reply_len, (void **)&phym, &reply_len);
393 	if (retval != 0 || !phym) {
394 		/* OID is optional so don't fail here. */
395 		phym_unspec = cpu_to_le32(RNDIS_PHYSICAL_MEDIUM_UNSPECIFIED);
396 		phym = &phym_unspec;
397 	}
398 	if ((flags & FLAG_RNDIS_PHYM_WIRELESS) &&
399 	    le32_to_cpup(phym) != RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
400 		netif_dbg(dev, probe, dev->net,
401 			  "driver requires wireless physical medium, but device is not\n");
402 		retval = -ENODEV;
403 		goto halt_fail_and_release;
404 	}
405 	if ((flags & FLAG_RNDIS_PHYM_NOT_WIRELESS) &&
406 	    le32_to_cpup(phym) == RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
407 		netif_dbg(dev, probe, dev->net,
408 			  "driver requires non-wireless physical medium, but device is wireless.\n");
409 		retval = -ENODEV;
410 		goto halt_fail_and_release;
411 	}
412 
413 	/* Get designated host ethernet address */
414 	reply_len = ETH_ALEN;
415 	retval = rndis_query(dev, intf, u.buf,
416 			     RNDIS_OID_802_3_PERMANENT_ADDRESS,
417 			     48, (void **) &bp, &reply_len);
418 	if (unlikely(retval< 0)) {
419 		dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
420 		goto halt_fail_and_release;
421 	}
422 
423 	eth_hw_addr_set(net, bp);
424 
425 	/* set a nonzero filter to enable data transfers */
426 	memset(u.set, 0, sizeof *u.set);
427 	u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
428 	u.set->msg_len = cpu_to_le32(4 + sizeof *u.set);
429 	u.set->oid = cpu_to_le32(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
430 	u.set->len = cpu_to_le32(4);
431 	u.set->offset = cpu_to_le32((sizeof *u.set) - 8);
432 	*(__le32 *)(u.buf + sizeof *u.set) = cpu_to_le32(RNDIS_DEFAULT_FILTER);
433 
434 	retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
435 	if (unlikely(retval < 0)) {
436 		dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
437 		goto halt_fail_and_release;
438 	}
439 
440 	retval = 0;
441 
442 	kfree(u.buf);
443 	return retval;
444 
445 halt_fail_and_release:
446 	memset(u.halt, 0, sizeof *u.halt);
447 	u.halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT);
448 	u.halt->msg_len = cpu_to_le32(sizeof *u.halt);
449 	(void) rndis_command(dev, (void *)u.halt, CONTROL_BUFFER_SIZE);
450 fail_and_release:
451 	usb_set_intfdata(info->data, NULL);
452 	usb_driver_release_interface(driver_of(intf), info->data);
453 	info->data = NULL;
454 fail:
455 	kfree(u.buf);
456 	return retval;
457 }
458 EXPORT_SYMBOL_GPL(generic_rndis_bind);
459 
rndis_bind(struct usbnet * dev,struct usb_interface * intf)460 static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
461 {
462 	return generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_NOT_WIRELESS);
463 }
464 
zte_rndis_bind(struct usbnet * dev,struct usb_interface * intf)465 static int zte_rndis_bind(struct usbnet *dev, struct usb_interface *intf)
466 {
467 	int status = rndis_bind(dev, intf);
468 
469 	if (!status && (dev->net->dev_addr[0] & 0x02))
470 		eth_hw_addr_random(dev->net);
471 
472 	return status;
473 }
474 
rndis_unbind(struct usbnet * dev,struct usb_interface * intf)475 void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
476 {
477 	struct rndis_halt	*halt;
478 
479 	/* try to clear any rndis state/activity (no i/o from stack!) */
480 	halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
481 	if (halt) {
482 		halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT);
483 		halt->msg_len = cpu_to_le32(sizeof *halt);
484 		(void) rndis_command(dev, (void *)halt, CONTROL_BUFFER_SIZE);
485 		kfree(halt);
486 	}
487 
488 	usbnet_cdc_unbind(dev, intf);
489 }
490 EXPORT_SYMBOL_GPL(rndis_unbind);
491 
492 /*
493  * DATA -- host must not write zlps
494  */
rndis_rx_fixup(struct usbnet * dev,struct sk_buff * skb)495 int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
496 {
497 	bool dst_mac_fixup;
498 
499 	/* This check is no longer done by usbnet */
500 	if (skb->len < dev->net->hard_header_len)
501 		return 0;
502 
503 	dst_mac_fixup = !!(dev->driver_info->data & RNDIS_DRIVER_DATA_DST_MAC_FIXUP);
504 
505 	/* peripheral may have batched packets to us... */
506 	while (likely(skb->len)) {
507 		struct rndis_data_hdr	*hdr = (void *)skb->data;
508 		struct sk_buff		*skb2;
509 		u32			msg_type, msg_len, data_offset, data_len;
510 		u32			overflow_check;
511 
512 		msg_type = le32_to_cpu(hdr->msg_type);
513 		msg_len = le32_to_cpu(hdr->msg_len);
514 		data_offset = le32_to_cpu(hdr->data_offset);
515 		data_len = le32_to_cpu(hdr->data_len);
516 
517 		/* don't choke if we see oob, per-packet data, etc */
518 		if (unlikely(msg_type != RNDIS_MSG_PACKET || skb->len < msg_len
519 				|| (data_offset + data_len + 8) > msg_len
520 				|| check_add_overflow(data_offset, data_len, &overflow_check)
521 				|| check_add_overflow(overflow_check, 8, &overflow_check))) {
522 			dev->net->stats.rx_frame_errors++;
523 			netdev_dbg(dev->net, "bad rndis message %d/%d/%d/%d, len %d\n",
524 				   le32_to_cpu(hdr->msg_type),
525 				   msg_len, data_offset, data_len, skb->len);
526 			return 0;
527 		}
528 		skb_pull(skb, 8 + data_offset);
529 
530 		/* at most one packet left? */
531 		if (likely((data_len - skb->len) <= sizeof *hdr)) {
532 			skb_trim(skb, data_len);
533 			break;
534 		}
535 
536 		/* try to return all the packets in the batch */
537 		skb2 = skb_clone(skb, GFP_ATOMIC);
538 		if (unlikely(!skb2))
539 			break;
540 		skb_pull(skb, msg_len - sizeof *hdr);
541 		skb_trim(skb2, data_len);
542 
543 		if (unlikely(dst_mac_fixup))
544 			usbnet_cdc_zte_rx_fixup(dev, skb2);
545 
546 		usbnet_skb_return(dev, skb2);
547 	}
548 
549 	/* caller will usbnet_skb_return the remaining packet */
550 	if (unlikely(dst_mac_fixup))
551 		usbnet_cdc_zte_rx_fixup(dev, skb);
552 
553 	return 1;
554 }
555 EXPORT_SYMBOL_GPL(rndis_rx_fixup);
556 
557 struct sk_buff *
rndis_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)558 rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
559 {
560 	struct rndis_data_hdr	*hdr;
561 	struct sk_buff		*skb2;
562 	unsigned		len = skb->len;
563 
564 	if (likely(!skb_cloned(skb))) {
565 		int	room = skb_headroom(skb);
566 
567 		/* enough head room as-is? */
568 		if (unlikely((sizeof *hdr) <= room))
569 			goto fill;
570 
571 		/* enough room, but needs to be readjusted? */
572 		room += skb_tailroom(skb);
573 		if (likely((sizeof *hdr) <= room)) {
574 			skb->data = memmove(skb->head + sizeof *hdr,
575 					    skb->data, len);
576 			skb_set_tail_pointer(skb, len);
577 			goto fill;
578 		}
579 	}
580 
581 	/* create a new skb, with the correct size (and tailpad) */
582 	skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
583 	dev_kfree_skb_any(skb);
584 	if (unlikely(!skb2))
585 		return skb2;
586 	skb = skb2;
587 
588 	/* fill out the RNDIS header.  we won't bother trying to batch
589 	 * packets; Linux minimizes wasted bandwidth through tx queues.
590 	 */
591 fill:
592 	hdr = __skb_push(skb, sizeof *hdr);
593 	memset(hdr, 0, sizeof *hdr);
594 	hdr->msg_type = cpu_to_le32(RNDIS_MSG_PACKET);
595 	hdr->msg_len = cpu_to_le32(skb->len);
596 	hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8);
597 	hdr->data_len = cpu_to_le32(len);
598 
599 	/* FIXME make the last packet always be short ... */
600 	return skb;
601 }
602 EXPORT_SYMBOL_GPL(rndis_tx_fixup);
603 
604 
605 static const struct driver_info	rndis_info = {
606 	.description =	"RNDIS device",
607 	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
608 	.bind =		rndis_bind,
609 	.unbind =	rndis_unbind,
610 	.status =	rndis_status,
611 	.rx_fixup =	rndis_rx_fixup,
612 	.tx_fixup =	rndis_tx_fixup,
613 };
614 
615 static const struct driver_info	rndis_poll_status_info = {
616 	.description =	"RNDIS device (poll status before control)",
617 	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
618 	.data =		RNDIS_DRIVER_DATA_POLL_STATUS,
619 	.bind =		rndis_bind,
620 	.unbind =	rndis_unbind,
621 	.status =	rndis_status,
622 	.rx_fixup =	rndis_rx_fixup,
623 	.tx_fixup =	rndis_tx_fixup,
624 };
625 
626 static const struct driver_info	zte_rndis_info = {
627 	.description =	"ZTE RNDIS device",
628 	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
629 	.data =		RNDIS_DRIVER_DATA_DST_MAC_FIXUP,
630 	.bind =		zte_rndis_bind,
631 	.unbind =	rndis_unbind,
632 	.status =	rndis_status,
633 	.rx_fixup =	rndis_rx_fixup,
634 	.tx_fixup =	rndis_tx_fixup,
635 };
636 
637 static const struct driver_info	rndis_info_lowpower = {
638 	.description =	"RNDIS device",
639 	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
640 	.bind =		rndis_bind,
641 	.unbind =	rndis_unbind,
642 	.status =	rndis_status,
643 	.rx_fixup =	rndis_rx_fixup,
644 	.tx_fixup =	rndis_tx_fixup,
645 	.manage_power =	usbnet_manage_power,
646 };
647 
648 /*-------------------------------------------------------------------------*/
649 
650 static const struct usb_device_id	products [] = {
651 {
652 	/* 2Wire HomePortal 1000SW */
653 	USB_DEVICE_AND_INTERFACE_INFO(0x1630, 0x0042,
654 				      USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
655 	.driver_info = (unsigned long) &rndis_poll_status_info,
656 }, {
657 	/* Hytera Communications DMR radios' "Radio to PC Network" */
658 	USB_VENDOR_AND_INTERFACE_INFO(0x238b,
659 				      USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
660 	.driver_info = (unsigned long)&rndis_info,
661 }, {
662 	/* ZTE WWAN modules */
663 	USB_VENDOR_AND_INTERFACE_INFO(0x19d2,
664 				      USB_CLASS_WIRELESS_CONTROLLER, 1, 3),
665 	.driver_info = (unsigned long)&zte_rndis_info,
666 }, {
667 	/* ZTE WWAN modules, ACM flavour */
668 	USB_VENDOR_AND_INTERFACE_INFO(0x19d2,
669 				      USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
670 	.driver_info = (unsigned long)&zte_rndis_info,
671 }, {
672 	/* RNDIS is MSFT's un-official variant of CDC ACM */
673 	USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
674 	.driver_info = (unsigned long) &rndis_info,
675 }, {
676 	/* Telit Cinterion LE310X1 RNDIS */
677 	USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x7030,
678 				      USB_CLASS_WIRELESS_CONTROLLER, 1, 3),
679 	.driver_info = (unsigned long)&rndis_info_lowpower,
680 }, {
681 	/* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
682 	USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
683 	.driver_info = (unsigned long) &rndis_poll_status_info,
684 }, {
685 	/* RNDIS for tethering */
686 	USB_INTERFACE_INFO(USB_CLASS_WIRELESS_CONTROLLER, 1, 3),
687 	.driver_info = (unsigned long) &rndis_info,
688 }, {
689 	/* Novatel Verizon USB730L */
690 	USB_INTERFACE_INFO(USB_CLASS_MISC, 4, 1),
691 	.driver_info = (unsigned long) &rndis_info,
692 },
693 	{ },		// END
694 };
695 MODULE_DEVICE_TABLE(usb, products);
696 
697 static struct usb_driver rndis_driver = {
698 	.name =		"rndis_host",
699 	.id_table =	products,
700 	.probe =	usbnet_probe,
701 	.disconnect =	usbnet_disconnect,
702 	.suspend =	usbnet_suspend,
703 	.resume =	usbnet_resume,
704 	.disable_hub_initiated_lpm = 1,
705 };
706 
707 module_usb_driver(rndis_driver);
708 
709 MODULE_AUTHOR("David Brownell");
710 MODULE_DESCRIPTION("USB Host side RNDIS driver");
711 MODULE_LICENSE("GPL");
712