blob: be888d2d813ca4293d5b74a816fcbc40915728ca [file] [log] [blame]
David Brownell64e04912005-08-31 09:54:36 -07001/*
2 * Host Side support for RNDIS Networking Links
3 * Copyright (C) 2005 by David Brownell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20// #define DEBUG // error path messages, extra info
21// #define VERBOSE // more; success messages
22
David Brownell64e04912005-08-31 09:54:36 -070023#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/init.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/ethtool.h>
29#include <linux/workqueue.h>
30#include <linux/mii.h>
31#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070032#include <linux/usb/cdc.h>
David Brownell64e04912005-08-31 09:54:36 -070033
34#include "usbnet.h"
35
36
37/*
38 * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
39 * course ACM was intended for modems, not Ethernet links! USB's standard
40 * for Ethernet links is "CDC Ethernet", which is significantly simpler.
David Brownell51400f12006-04-02 10:19:08 -080041 *
42 * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete. Issues
43 * include:
44 * - Power management in particular relies on information that's scattered
45 * through other documentation, and which is incomplete or incorrect even
46 * there.
47 * - There are various undocumented protocol requirements, such as the
48 * need to send unused garbage in control-OUT messages.
49 * - In some cases, MS-Windows will emit undocumented requests; this
50 * matters more to peripheral implementations than host ones.
51 *
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -080052 * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
53 *
David Brownell51400f12006-04-02 10:19:08 -080054 * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
55 * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
56 * currently rare) "Ethernet Emulation Model" (EEM).
David Brownell64e04912005-08-31 09:54:36 -070057 */
58
59/*
60 * CONTROL uses CDC "encapsulated commands" with funky notifications.
61 * - control-out: SEND_ENCAPSULATED
62 * - interrupt-in: RESPONSE_AVAILABLE
63 * - control-in: GET_ENCAPSULATED
64 *
65 * We'll try to ignore the RESPONSE_AVAILABLE notifications.
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -080066 *
67 * REVISIT some RNDIS implementations seem to have curious issues still
68 * to be resolved.
David Brownell64e04912005-08-31 09:54:36 -070069 */
70struct rndis_msg_hdr {
71 __le32 msg_type; /* RNDIS_MSG_* */
72 __le32 msg_len;
73 // followed by data that varies between messages
74 __le32 request_id;
75 __le32 status;
76 // ... and more
77} __attribute__ ((packed));
78
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -080079/* MS-Windows uses this strange size, but RNDIS spec says 1024 minimum */
80#define CONTROL_BUFFER_SIZE 1025
81
82/* RNDIS defines an (absurdly huge) 10 second control timeout,
83 * but ActiveSync seems to use a more usual 5 second timeout
84 * (which matches the USB 2.0 spec).
85 */
86#define RNDIS_CONTROL_TIMEOUT_MS (5 * 1000)
David Brownell64e04912005-08-31 09:54:36 -070087
88
89#define ccpu2 __constant_cpu_to_le32
90
91#define RNDIS_MSG_COMPLETION ccpu2(0x80000000)
92
93/* codes for "msg_type" field of rndis messages;
94 * only the data channel uses packet messages (maybe batched);
95 * everything else goes on the control channel.
96 */
97#define RNDIS_MSG_PACKET ccpu2(0x00000001) /* 1-N packets */
98#define RNDIS_MSG_INIT ccpu2(0x00000002)
David Brownell51400f12006-04-02 10:19:08 -080099#define RNDIS_MSG_INIT_C (RNDIS_MSG_INIT|RNDIS_MSG_COMPLETION)
David Brownell64e04912005-08-31 09:54:36 -0700100#define RNDIS_MSG_HALT ccpu2(0x00000003)
101#define RNDIS_MSG_QUERY ccpu2(0x00000004)
David Brownell51400f12006-04-02 10:19:08 -0800102#define RNDIS_MSG_QUERY_C (RNDIS_MSG_QUERY|RNDIS_MSG_COMPLETION)
David Brownell64e04912005-08-31 09:54:36 -0700103#define RNDIS_MSG_SET ccpu2(0x00000005)
David Brownell51400f12006-04-02 10:19:08 -0800104#define RNDIS_MSG_SET_C (RNDIS_MSG_SET|RNDIS_MSG_COMPLETION)
David Brownell64e04912005-08-31 09:54:36 -0700105#define RNDIS_MSG_RESET ccpu2(0x00000006)
David Brownell51400f12006-04-02 10:19:08 -0800106#define RNDIS_MSG_RESET_C (RNDIS_MSG_RESET|RNDIS_MSG_COMPLETION)
David Brownell64e04912005-08-31 09:54:36 -0700107#define RNDIS_MSG_INDICATE ccpu2(0x00000007)
108#define RNDIS_MSG_KEEPALIVE ccpu2(0x00000008)
David Brownell51400f12006-04-02 10:19:08 -0800109#define RNDIS_MSG_KEEPALIVE_C (RNDIS_MSG_KEEPALIVE|RNDIS_MSG_COMPLETION)
David Brownell64e04912005-08-31 09:54:36 -0700110
111/* codes for "status" field of completion messages */
112#define RNDIS_STATUS_SUCCESS ccpu2(0x00000000)
113#define RNDIS_STATUS_FAILURE ccpu2(0xc0000001)
114#define RNDIS_STATUS_INVALID_DATA ccpu2(0xc0010015)
115#define RNDIS_STATUS_NOT_SUPPORTED ccpu2(0xc00000bb)
116#define RNDIS_STATUS_MEDIA_CONNECT ccpu2(0x4001000b)
117#define RNDIS_STATUS_MEDIA_DISCONNECT ccpu2(0x4001000c)
118
119
120struct rndis_data_hdr {
121 __le32 msg_type; /* RNDIS_MSG_PACKET */
122 __le32 msg_len; // rndis_data_hdr + data_len + pad
123 __le32 data_offset; // 36 -- right after header
124 __le32 data_len; // ... real packet size
125
126 __le32 oob_data_offset; // zero
127 __le32 oob_data_len; // zero
128 __le32 num_oob; // zero
129 __le32 packet_data_offset; // zero
130
131 __le32 packet_data_len; // zero
132 __le32 vc_handle; // zero
133 __le32 reserved; // zero
134} __attribute__ ((packed));
135
136struct rndis_init { /* OUT */
137 // header and:
138 __le32 msg_type; /* RNDIS_MSG_INIT */
139 __le32 msg_len; // 24
140 __le32 request_id;
141 __le32 major_version; // of rndis (1.0)
142 __le32 minor_version;
143 __le32 max_transfer_size;
144} __attribute__ ((packed));
145
146struct rndis_init_c { /* IN */
147 // header and:
148 __le32 msg_type; /* RNDIS_MSG_INIT_C */
149 __le32 msg_len;
150 __le32 request_id;
151 __le32 status;
152 __le32 major_version; // of rndis (1.0)
153 __le32 minor_version;
154 __le32 device_flags;
155 __le32 medium; // zero == 802.3
156 __le32 max_packets_per_message;
157 __le32 max_transfer_size;
158 __le32 packet_alignment; // max 7; (1<<n) bytes
159 __le32 af_list_offset; // zero
160 __le32 af_list_size; // zero
161} __attribute__ ((packed));
162
163struct rndis_halt { /* OUT (no reply) */
164 // header and:
165 __le32 msg_type; /* RNDIS_MSG_HALT */
166 __le32 msg_len;
167 __le32 request_id;
168} __attribute__ ((packed));
169
170struct rndis_query { /* OUT */
171 // header and:
172 __le32 msg_type; /* RNDIS_MSG_QUERY */
173 __le32 msg_len;
174 __le32 request_id;
175 __le32 oid;
176 __le32 len;
177 __le32 offset;
178/*?*/ __le32 handle; // zero
179} __attribute__ ((packed));
180
181struct rndis_query_c { /* IN */
182 // header and:
183 __le32 msg_type; /* RNDIS_MSG_QUERY_C */
184 __le32 msg_len;
185 __le32 request_id;
186 __le32 status;
187 __le32 len;
188 __le32 offset;
189} __attribute__ ((packed));
190
191struct rndis_set { /* OUT */
192 // header and:
193 __le32 msg_type; /* RNDIS_MSG_SET */
194 __le32 msg_len;
195 __le32 request_id;
196 __le32 oid;
197 __le32 len;
198 __le32 offset;
199/*?*/ __le32 handle; // zero
200} __attribute__ ((packed));
201
202struct rndis_set_c { /* IN */
203 // header and:
204 __le32 msg_type; /* RNDIS_MSG_SET_C */
205 __le32 msg_len;
206 __le32 request_id;
207 __le32 status;
208} __attribute__ ((packed));
209
210struct rndis_reset { /* IN */
211 // header and:
212 __le32 msg_type; /* RNDIS_MSG_RESET */
213 __le32 msg_len;
214 __le32 reserved;
215} __attribute__ ((packed));
216
217struct rndis_reset_c { /* OUT */
218 // header and:
219 __le32 msg_type; /* RNDIS_MSG_RESET_C */
220 __le32 msg_len;
221 __le32 status;
222 __le32 addressing_lost;
223} __attribute__ ((packed));
224
225struct rndis_indicate { /* IN (unrequested) */
226 // header and:
227 __le32 msg_type; /* RNDIS_MSG_INDICATE */
228 __le32 msg_len;
229 __le32 status;
230 __le32 length;
231 __le32 offset;
232/**/ __le32 diag_status;
233 __le32 error_offset;
234/**/ __le32 message;
235} __attribute__ ((packed));
236
237struct rndis_keepalive { /* OUT (optionally IN) */
238 // header and:
239 __le32 msg_type; /* RNDIS_MSG_KEEPALIVE */
240 __le32 msg_len;
241 __le32 request_id;
242} __attribute__ ((packed));
243
244struct rndis_keepalive_c { /* IN (optionally OUT) */
245 // header and:
246 __le32 msg_type; /* RNDIS_MSG_KEEPALIVE_C */
247 __le32 msg_len;
248 __le32 request_id;
249 __le32 status;
250} __attribute__ ((packed));
251
252/* NOTE: about 30 OIDs are "mandatory" for peripherals to support ... and
253 * there are gobs more that may optionally be supported. We'll avoid as much
254 * of that mess as possible.
255 */
256#define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
257#define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e)
258
259/*
260 * RNDIS notifications from device: command completion; "reverse"
261 * keepalives; etc
262 */
263static void rndis_status(struct usbnet *dev, struct urb *urb)
264{
265 devdbg(dev, "rndis status urb, len %d stat %d",
266 urb->actual_length, urb->status);
267 // FIXME for keepalives, respond immediately (asynchronously)
268 // if not an RNDIS status, do like cdc_status(dev,urb) does
269}
270
271/*
272 * RPC done RNDIS-style. Caller guarantees:
273 * - message is properly byteswapped
274 * - there's no other request pending
275 * - buf can hold up to 1KB response (required by RNDIS spec)
276 * On return, the first few entries are already byteswapped.
277 *
278 * Call context is likely probe(), before interface name is known,
279 * which is why we won't try to use it in the diagnostics.
280 */
281static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
282{
283 struct cdc_state *info = (void *) &dev->data;
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800284 int master_ifnum;
David Brownell64e04912005-08-31 09:54:36 -0700285 int retval;
286 unsigned count;
287 __le32 rsp;
288 u32 xid = 0, msg_len, request_id;
289
290 /* REVISIT when this gets called from contexts other than probe() or
291 * disconnect(): either serialize, or dispatch responses on xid
292 */
293
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800294 /* Issue the request; xid is unique, don't bother byteswapping it */
David Brownell64e04912005-08-31 09:54:36 -0700295 if (likely(buf->msg_type != RNDIS_MSG_HALT
296 && buf->msg_type != RNDIS_MSG_RESET)) {
297 xid = dev->xid++;
298 if (!xid)
299 xid = dev->xid++;
300 buf->request_id = (__force __le32) xid;
301 }
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800302 master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
David Brownell64e04912005-08-31 09:54:36 -0700303 retval = usb_control_msg(dev->udev,
304 usb_sndctrlpipe(dev->udev, 0),
305 USB_CDC_SEND_ENCAPSULATED_COMMAND,
306 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800307 0, master_ifnum,
David Brownell64e04912005-08-31 09:54:36 -0700308 buf, le32_to_cpu(buf->msg_len),
309 RNDIS_CONTROL_TIMEOUT_MS);
310 if (unlikely(retval < 0 || xid == 0))
311 return retval;
312
313 // FIXME Seems like some devices discard responses when
314 // we time out and cancel our "get response" requests...
315 // so, this is fragile. Probably need to poll for status.
316
317 /* ignore status endpoint, just poll the control channel;
318 * the request probably completed immediately
319 */
320 rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
321 for (count = 0; count < 10; count++) {
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800322 memset(buf, 0, CONTROL_BUFFER_SIZE);
David Brownell64e04912005-08-31 09:54:36 -0700323 retval = usb_control_msg(dev->udev,
324 usb_rcvctrlpipe(dev->udev, 0),
325 USB_CDC_GET_ENCAPSULATED_RESPONSE,
326 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800327 0, master_ifnum,
328 buf, CONTROL_BUFFER_SIZE,
David Brownell64e04912005-08-31 09:54:36 -0700329 RNDIS_CONTROL_TIMEOUT_MS);
330 if (likely(retval >= 8)) {
331 msg_len = le32_to_cpu(buf->msg_len);
332 request_id = (__force u32) buf->request_id;
333 if (likely(buf->msg_type == rsp)) {
334 if (likely(request_id == xid)) {
335 if (unlikely(rsp == RNDIS_MSG_RESET_C))
336 return 0;
337 if (likely(RNDIS_STATUS_SUCCESS
338 == buf->status))
339 return 0;
340 dev_dbg(&info->control->dev,
341 "rndis reply status %08x\n",
342 le32_to_cpu(buf->status));
343 return -EL3RST;
344 }
345 dev_dbg(&info->control->dev,
346 "rndis reply id %d expected %d\n",
347 request_id, xid);
348 /* then likely retry */
349 } else switch (buf->msg_type) {
350 case RNDIS_MSG_INDICATE: { /* fault */
351 // struct rndis_indicate *msg = (void *)buf;
352 dev_info(&info->control->dev,
353 "rndis fault indication\n");
354 }
355 break;
356 case RNDIS_MSG_KEEPALIVE: { /* ping */
357 struct rndis_keepalive_c *msg = (void *)buf;
358
359 msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
360 msg->msg_len = ccpu2(sizeof *msg);
361 msg->status = RNDIS_STATUS_SUCCESS;
362 retval = usb_control_msg(dev->udev,
363 usb_sndctrlpipe(dev->udev, 0),
364 USB_CDC_SEND_ENCAPSULATED_COMMAND,
365 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800366 0, master_ifnum,
David Brownell64e04912005-08-31 09:54:36 -0700367 msg, sizeof *msg,
368 RNDIS_CONTROL_TIMEOUT_MS);
369 if (unlikely(retval < 0))
370 dev_dbg(&info->control->dev,
371 "rndis keepalive err %d\n",
372 retval);
373 }
374 break;
375 default:
376 dev_dbg(&info->control->dev,
377 "unexpected rndis msg %08x len %d\n",
378 le32_to_cpu(buf->msg_type), msg_len);
379 }
380 } else {
381 /* device probably issued a protocol stall; ignore */
382 dev_dbg(&info->control->dev,
383 "rndis response error, code %d\n", retval);
384 }
385 msleep(2);
386 }
387 dev_dbg(&info->control->dev, "rndis response timeout\n");
388 return -ETIMEDOUT;
389}
390
391static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
392{
393 int retval;
394 struct net_device *net = dev->net;
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100395 struct cdc_state *info = (void *) &dev->data;
David Brownell64e04912005-08-31 09:54:36 -0700396 union {
397 void *buf;
398 struct rndis_msg_hdr *header;
399 struct rndis_init *init;
400 struct rndis_init_c *init_c;
401 struct rndis_query *get;
402 struct rndis_query_c *get_c;
403 struct rndis_set *set;
404 struct rndis_set_c *set_c;
405 } u;
406 u32 tmp;
407
408 /* we can't rely on i/o from stack working, or stack allocation */
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800409 u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
David Brownell64e04912005-08-31 09:54:36 -0700410 if (!u.buf)
411 return -ENOMEM;
412 retval = usbnet_generic_cdc_bind(dev, intf);
413 if (retval < 0)
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100414 goto fail;
David Brownell64e04912005-08-31 09:54:36 -0700415
David Brownell64e04912005-08-31 09:54:36 -0700416 u.init->msg_type = RNDIS_MSG_INIT;
417 u.init->msg_len = ccpu2(sizeof *u.init);
418 u.init->major_version = ccpu2(1);
419 u.init->minor_version = ccpu2(0);
David Brownell64e04912005-08-31 09:54:36 -0700420
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800421 /* max transfer (in spec) is 0x4000 at full speed, but for
422 * TX we'll stick to one Ethernet packet plus RNDIS framing.
423 * For RX we handle drivers that zero-pad to end-of-packet.
424 * Don't let userspace change these settings.
425 */
426 net->hard_header_len += sizeof (struct rndis_data_hdr);
427 dev->hard_mtu = net->mtu + net->hard_header_len;
428
429 dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
430 dev->rx_urb_size &= ~(dev->maxpacket - 1);
431 u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
432
433 net->change_mtu = NULL;
David Brownell64e04912005-08-31 09:54:36 -0700434 retval = rndis_command(dev, u.header);
435 if (unlikely(retval < 0)) {
436 /* it might not even be an RNDIS device!! */
437 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800438 goto fail_and_release;
439 }
440 tmp = le32_to_cpu(u.init_c->max_transfer_size);
441 if (tmp < dev->hard_mtu) {
442 dev_err(&intf->dev,
443 "dev can't take %u byte packets (max %u)\n",
444 dev->hard_mtu, tmp);
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100445 goto fail_and_release;
David Brownell64e04912005-08-31 09:54:36 -0700446 }
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800447
David Brownell64e04912005-08-31 09:54:36 -0700448 /* REVISIT: peripheral "alignment" request is ignored ... */
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800449 dev_dbg(&intf->dev,
450 "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
451 dev->hard_mtu, tmp, dev->rx_urb_size,
David Brownell64e04912005-08-31 09:54:36 -0700452 1 << le32_to_cpu(u.init_c->packet_alignment));
453
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800454 /* Get designated host ethernet address.
455 *
456 * Adding a payload exactly the same size as the expected response
457 * payload is an evident requirement MSFT added for ActiveSync.
458 * This undocumented (and nonsensical) issue was found by sniffing
459 * protocol requests from the ActiveSync 4.1 Windows driver.
460 */
461 memset(u.get, 0, sizeof *u.get + 48);
David Brownell64e04912005-08-31 09:54:36 -0700462 u.get->msg_type = RNDIS_MSG_QUERY;
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800463 u.get->msg_len = ccpu2(sizeof *u.get + 48);
David Brownell64e04912005-08-31 09:54:36 -0700464 u.get->oid = OID_802_3_PERMANENT_ADDRESS;
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800465 u.get->len = ccpu2(48);
466 u.get->offset = ccpu2(20);
David Brownell64e04912005-08-31 09:54:36 -0700467
468 retval = rndis_command(dev, u.header);
469 if (unlikely(retval < 0)) {
470 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100471 goto fail_and_release;
David Brownell64e04912005-08-31 09:54:36 -0700472 }
473 tmp = le32_to_cpu(u.get_c->offset);
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800474 if (unlikely((tmp + 8) > (CONTROL_BUFFER_SIZE - ETH_ALEN)
David Brownell64e04912005-08-31 09:54:36 -0700475 || u.get_c->len != ccpu2(ETH_ALEN))) {
476 dev_err(&intf->dev, "rndis ethaddr off %d len %d ?\n",
477 tmp, le32_to_cpu(u.get_c->len));
478 retval = -EDOM;
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100479 goto fail_and_release;
David Brownell64e04912005-08-31 09:54:36 -0700480 }
481 memcpy(net->dev_addr, tmp + (char *)&u.get_c->request_id, ETH_ALEN);
482
483 /* set a nonzero filter to enable data transfers */
484 memset(u.set, 0, sizeof *u.set);
485 u.set->msg_type = RNDIS_MSG_SET;
486 u.set->msg_len = ccpu2(4 + sizeof *u.set);
487 u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
488 u.set->len = ccpu2(4);
489 u.set->offset = ccpu2((sizeof *u.set) - 8);
490 *(__le32 *)(u.buf + sizeof *u.set) = ccpu2(DEFAULT_FILTER);
491
492 retval = rndis_command(dev, u.header);
493 if (unlikely(retval < 0)) {
494 dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100495 goto fail_and_release;
David Brownell64e04912005-08-31 09:54:36 -0700496 }
497
498 retval = 0;
Daniel Gollubdeb31f12007-01-16 11:03:01 +0100499
500 kfree(u.buf);
501 return retval;
502
503fail_and_release:
504 usb_set_intfdata(info->data, NULL);
505 usb_driver_release_interface(driver_of(intf), info->data);
506fail:
David Brownell64e04912005-08-31 09:54:36 -0700507 kfree(u.buf);
508 return retval;
509}
510
511static void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
512{
513 struct rndis_halt *halt;
514
515 /* try to clear any rndis state/activity (no i/o from stack!) */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800516 halt = kzalloc(sizeof *halt, GFP_KERNEL);
David Brownell64e04912005-08-31 09:54:36 -0700517 if (halt) {
518 halt->msg_type = RNDIS_MSG_HALT;
519 halt->msg_len = ccpu2(sizeof *halt);
520 (void) rndis_command(dev, (void *)halt);
521 kfree(halt);
522 }
523
524 return usbnet_cdc_unbind(dev, intf);
525}
526
527/*
528 * DATA -- host must not write zlps
529 */
530static int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
531{
532 /* peripheral may have batched packets to us... */
533 while (likely(skb->len)) {
534 struct rndis_data_hdr *hdr = (void *)skb->data;
535 struct sk_buff *skb2;
536 u32 msg_len, data_offset, data_len;
537
538 msg_len = le32_to_cpu(hdr->msg_len);
539 data_offset = le32_to_cpu(hdr->data_offset);
540 data_len = le32_to_cpu(hdr->data_len);
541
542 /* don't choke if we see oob, per-packet data, etc */
543 if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET
544 || skb->len < msg_len
545 || (data_offset + data_len + 8) > msg_len)) {
546 dev->stats.rx_frame_errors++;
547 devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d",
548 le32_to_cpu(hdr->msg_type),
549 msg_len, data_offset, data_len, skb->len);
550 return 0;
551 }
552 skb_pull(skb, 8 + data_offset);
553
554 /* at most one packet left? */
555 if (likely((data_len - skb->len) <= sizeof *hdr)) {
556 skb_trim(skb, data_len);
557 break;
558 }
559
560 /* try to return all the packets in the batch */
561 skb2 = skb_clone(skb, GFP_ATOMIC);
562 if (unlikely(!skb2))
563 break;
564 skb_pull(skb, msg_len - sizeof *hdr);
565 skb_trim(skb2, data_len);
566 usbnet_skb_return(dev, skb2);
567 }
568
569 /* caller will usbnet_skb_return the remaining packet */
570 return 1;
571}
572
573static struct sk_buff *
Al Viro55016f12005-10-21 03:21:58 -0400574rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
David Brownell64e04912005-08-31 09:54:36 -0700575{
576 struct rndis_data_hdr *hdr;
577 struct sk_buff *skb2;
578 unsigned len = skb->len;
579
580 if (likely(!skb_cloned(skb))) {
581 int room = skb_headroom(skb);
582
583 /* enough head room as-is? */
584 if (unlikely((sizeof *hdr) <= room))
585 goto fill;
586
587 /* enough room, but needs to be readjusted? */
588 room += skb_tailroom(skb);
589 if (likely((sizeof *hdr) <= room)) {
590 skb->data = memmove(skb->head + sizeof *hdr,
591 skb->data, len);
592 skb->tail = skb->data + len;
593 goto fill;
594 }
595 }
596
597 /* create a new skb, with the correct size (and tailpad) */
598 skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
599 dev_kfree_skb_any(skb);
600 if (unlikely(!skb2))
601 return skb2;
602 skb = skb2;
603
604 /* fill out the RNDIS header. we won't bother trying to batch
605 * packets; Linux minimizes wasted bandwidth through tx queues.
606 */
607fill:
608 hdr = (void *) __skb_push(skb, sizeof *hdr);
609 memset(hdr, 0, sizeof *hdr);
610 hdr->msg_type = RNDIS_MSG_PACKET;
611 hdr->msg_len = cpu_to_le32(skb->len);
612 hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
613 hdr->data_len = cpu_to_le32(len);
614
615 /* FIXME make the last packet always be short ... */
616 return skb;
617}
618
619
620static const struct driver_info rndis_info = {
621 .description = "RNDIS device",
622 .flags = FLAG_ETHER | FLAG_FRAMING_RN,
623 .bind = rndis_bind,
624 .unbind = rndis_unbind,
625 .status = rndis_status,
626 .rx_fixup = rndis_rx_fixup,
627 .tx_fixup = rndis_tx_fixup,
628};
629
630#undef ccpu2
631
632
633/*-------------------------------------------------------------------------*/
634
635static const struct usb_device_id products [] = {
636{
637 /* RNDIS is MSFT's un-official variant of CDC ACM */
638 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
639 .driver_info = (unsigned long) &rndis_info,
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800640}, {
641 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
642 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
643 .driver_info = (unsigned long) &rndis_info,
David Brownell64e04912005-08-31 09:54:36 -0700644},
645 { }, // END
646};
647MODULE_DEVICE_TABLE(usb, products);
648
649static struct usb_driver rndis_driver = {
David Brownell64e04912005-08-31 09:54:36 -0700650 .name = "rndis_host",
651 .id_table = products,
652 .probe = usbnet_probe,
653 .disconnect = usbnet_disconnect,
654 .suspend = usbnet_suspend,
655 .resume = usbnet_resume,
656};
657
658static int __init rndis_init(void)
659{
David Brownell51400f12006-04-02 10:19:08 -0800660 return usb_register(&rndis_driver);
David Brownell64e04912005-08-31 09:54:36 -0700661}
662module_init(rndis_init);
663
664static void __exit rndis_exit(void)
665{
David Brownell51400f12006-04-02 10:19:08 -0800666 usb_deregister(&rndis_driver);
David Brownell64e04912005-08-31 09:54:36 -0700667}
668module_exit(rndis_exit);
669
670MODULE_AUTHOR("David Brownell");
671MODULE_DESCRIPTION("USB Host side RNDIS driver");
672MODULE_LICENSE("GPL");