| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 23 | #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 Brownell | a8c28f2 | 2006-06-13 09:57:47 -0700 | [diff] [blame] | 32 | #include <linux/usb/cdc.h> | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 33 |  | 
|  | 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 Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 41 | * | 
|  | 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 | * | 
|  | 52 | * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in | 
|  | 53 | * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and | 
|  | 54 | * currently rare) "Ethernet Emulation Model" (EEM). | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 55 | */ | 
|  | 56 |  | 
|  | 57 | /* | 
|  | 58 | * CONTROL uses CDC "encapsulated commands" with funky notifications. | 
|  | 59 | *  - control-out:  SEND_ENCAPSULATED | 
|  | 60 | *  - interrupt-in:  RESPONSE_AVAILABLE | 
|  | 61 | *  - control-in:  GET_ENCAPSULATED | 
|  | 62 | * | 
|  | 63 | * We'll try to ignore the RESPONSE_AVAILABLE notifications. | 
|  | 64 | */ | 
|  | 65 | struct rndis_msg_hdr { | 
|  | 66 | __le32	msg_type;			/* RNDIS_MSG_* */ | 
|  | 67 | __le32	msg_len; | 
|  | 68 | // followed by data that varies between messages | 
|  | 69 | __le32	request_id; | 
|  | 70 | __le32	status; | 
|  | 71 | // ... and more | 
|  | 72 | } __attribute__ ((packed)); | 
|  | 73 |  | 
|  | 74 | /* RNDIS defines this (absurdly huge) control timeout */ | 
|  | 75 | #define	RNDIS_CONTROL_TIMEOUT_MS	(10 * 1000) | 
|  | 76 |  | 
|  | 77 |  | 
|  | 78 | #define ccpu2 __constant_cpu_to_le32 | 
|  | 79 |  | 
|  | 80 | #define RNDIS_MSG_COMPLETION	ccpu2(0x80000000) | 
|  | 81 |  | 
|  | 82 | /* codes for "msg_type" field of rndis messages; | 
|  | 83 | * only the data channel uses packet messages (maybe batched); | 
|  | 84 | * everything else goes on the control channel. | 
|  | 85 | */ | 
|  | 86 | #define RNDIS_MSG_PACKET	ccpu2(0x00000001)	/* 1-N packets */ | 
|  | 87 | #define RNDIS_MSG_INIT		ccpu2(0x00000002) | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 88 | #define RNDIS_MSG_INIT_C	(RNDIS_MSG_INIT|RNDIS_MSG_COMPLETION) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 89 | #define RNDIS_MSG_HALT		ccpu2(0x00000003) | 
|  | 90 | #define RNDIS_MSG_QUERY		ccpu2(0x00000004) | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 91 | #define RNDIS_MSG_QUERY_C	(RNDIS_MSG_QUERY|RNDIS_MSG_COMPLETION) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 92 | #define RNDIS_MSG_SET		ccpu2(0x00000005) | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 93 | #define RNDIS_MSG_SET_C		(RNDIS_MSG_SET|RNDIS_MSG_COMPLETION) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 94 | #define RNDIS_MSG_RESET		ccpu2(0x00000006) | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 95 | #define RNDIS_MSG_RESET_C	(RNDIS_MSG_RESET|RNDIS_MSG_COMPLETION) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 96 | #define RNDIS_MSG_INDICATE	ccpu2(0x00000007) | 
|  | 97 | #define RNDIS_MSG_KEEPALIVE	ccpu2(0x00000008) | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 98 | #define RNDIS_MSG_KEEPALIVE_C	(RNDIS_MSG_KEEPALIVE|RNDIS_MSG_COMPLETION) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | /* codes for "status" field of completion messages */ | 
|  | 101 | #define	RNDIS_STATUS_SUCCESS		ccpu2(0x00000000) | 
|  | 102 | #define	RNDIS_STATUS_FAILURE		ccpu2(0xc0000001) | 
|  | 103 | #define	RNDIS_STATUS_INVALID_DATA	ccpu2(0xc0010015) | 
|  | 104 | #define	RNDIS_STATUS_NOT_SUPPORTED	ccpu2(0xc00000bb) | 
|  | 105 | #define	RNDIS_STATUS_MEDIA_CONNECT	ccpu2(0x4001000b) | 
|  | 106 | #define	RNDIS_STATUS_MEDIA_DISCONNECT	ccpu2(0x4001000c) | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | struct rndis_data_hdr { | 
|  | 110 | __le32	msg_type;		/* RNDIS_MSG_PACKET */ | 
|  | 111 | __le32	msg_len;		// rndis_data_hdr + data_len + pad | 
|  | 112 | __le32	data_offset;		// 36 -- right after header | 
|  | 113 | __le32	data_len;		// ... real packet size | 
|  | 114 |  | 
|  | 115 | __le32	oob_data_offset;	// zero | 
|  | 116 | __le32	oob_data_len;		// zero | 
|  | 117 | __le32	num_oob;		// zero | 
|  | 118 | __le32	packet_data_offset;	// zero | 
|  | 119 |  | 
|  | 120 | __le32	packet_data_len;	// zero | 
|  | 121 | __le32	vc_handle;		// zero | 
|  | 122 | __le32	reserved;		// zero | 
|  | 123 | } __attribute__ ((packed)); | 
|  | 124 |  | 
|  | 125 | struct rndis_init {		/* OUT */ | 
|  | 126 | // header and: | 
|  | 127 | __le32	msg_type;			/* RNDIS_MSG_INIT */ | 
|  | 128 | __le32	msg_len;			// 24 | 
|  | 129 | __le32	request_id; | 
|  | 130 | __le32	major_version;			// of rndis (1.0) | 
|  | 131 | __le32	minor_version; | 
|  | 132 | __le32	max_transfer_size; | 
|  | 133 | } __attribute__ ((packed)); | 
|  | 134 |  | 
|  | 135 | struct rndis_init_c {		/* IN */ | 
|  | 136 | // header and: | 
|  | 137 | __le32	msg_type;			/* RNDIS_MSG_INIT_C */ | 
|  | 138 | __le32	msg_len; | 
|  | 139 | __le32	request_id; | 
|  | 140 | __le32	status; | 
|  | 141 | __le32	major_version;			// of rndis (1.0) | 
|  | 142 | __le32	minor_version; | 
|  | 143 | __le32	device_flags; | 
|  | 144 | __le32	medium;				// zero == 802.3 | 
|  | 145 | __le32	max_packets_per_message; | 
|  | 146 | __le32	max_transfer_size; | 
|  | 147 | __le32	packet_alignment;		// max 7; (1<<n) bytes | 
|  | 148 | __le32	af_list_offset;			// zero | 
|  | 149 | __le32	af_list_size;			// zero | 
|  | 150 | } __attribute__ ((packed)); | 
|  | 151 |  | 
|  | 152 | struct rndis_halt {		/* OUT (no reply) */ | 
|  | 153 | // header and: | 
|  | 154 | __le32	msg_type;			/* RNDIS_MSG_HALT */ | 
|  | 155 | __le32	msg_len; | 
|  | 156 | __le32	request_id; | 
|  | 157 | } __attribute__ ((packed)); | 
|  | 158 |  | 
|  | 159 | struct rndis_query {		/* OUT */ | 
|  | 160 | // header and: | 
|  | 161 | __le32	msg_type;			/* RNDIS_MSG_QUERY */ | 
|  | 162 | __le32	msg_len; | 
|  | 163 | __le32	request_id; | 
|  | 164 | __le32	oid; | 
|  | 165 | __le32	len; | 
|  | 166 | __le32	offset; | 
|  | 167 | /*?*/	__le32	handle;				// zero | 
|  | 168 | } __attribute__ ((packed)); | 
|  | 169 |  | 
|  | 170 | struct rndis_query_c {		/* IN */ | 
|  | 171 | // header and: | 
|  | 172 | __le32	msg_type;			/* RNDIS_MSG_QUERY_C */ | 
|  | 173 | __le32	msg_len; | 
|  | 174 | __le32	request_id; | 
|  | 175 | __le32	status; | 
|  | 176 | __le32	len; | 
|  | 177 | __le32	offset; | 
|  | 178 | } __attribute__ ((packed)); | 
|  | 179 |  | 
|  | 180 | struct rndis_set {		/* OUT */ | 
|  | 181 | // header and: | 
|  | 182 | __le32	msg_type;			/* RNDIS_MSG_SET */ | 
|  | 183 | __le32	msg_len; | 
|  | 184 | __le32	request_id; | 
|  | 185 | __le32	oid; | 
|  | 186 | __le32	len; | 
|  | 187 | __le32	offset; | 
|  | 188 | /*?*/	__le32	handle;				// zero | 
|  | 189 | } __attribute__ ((packed)); | 
|  | 190 |  | 
|  | 191 | struct rndis_set_c {		/* IN */ | 
|  | 192 | // header and: | 
|  | 193 | __le32	msg_type;			/* RNDIS_MSG_SET_C */ | 
|  | 194 | __le32	msg_len; | 
|  | 195 | __le32	request_id; | 
|  | 196 | __le32	status; | 
|  | 197 | } __attribute__ ((packed)); | 
|  | 198 |  | 
|  | 199 | struct rndis_reset {		/* IN */ | 
|  | 200 | // header and: | 
|  | 201 | __le32	msg_type;			/* RNDIS_MSG_RESET */ | 
|  | 202 | __le32	msg_len; | 
|  | 203 | __le32	reserved; | 
|  | 204 | } __attribute__ ((packed)); | 
|  | 205 |  | 
|  | 206 | struct rndis_reset_c {		/* OUT */ | 
|  | 207 | // header and: | 
|  | 208 | __le32	msg_type;			/* RNDIS_MSG_RESET_C */ | 
|  | 209 | __le32	msg_len; | 
|  | 210 | __le32	status; | 
|  | 211 | __le32	addressing_lost; | 
|  | 212 | } __attribute__ ((packed)); | 
|  | 213 |  | 
|  | 214 | struct rndis_indicate {		/* IN (unrequested) */ | 
|  | 215 | // header and: | 
|  | 216 | __le32	msg_type;			/* RNDIS_MSG_INDICATE */ | 
|  | 217 | __le32	msg_len; | 
|  | 218 | __le32	status; | 
|  | 219 | __le32	length; | 
|  | 220 | __le32	offset; | 
|  | 221 | /**/	__le32	diag_status; | 
|  | 222 | __le32	error_offset; | 
|  | 223 | /**/	__le32	message; | 
|  | 224 | } __attribute__ ((packed)); | 
|  | 225 |  | 
|  | 226 | struct rndis_keepalive {	/* OUT (optionally IN) */ | 
|  | 227 | // header and: | 
|  | 228 | __le32	msg_type;			/* RNDIS_MSG_KEEPALIVE */ | 
|  | 229 | __le32	msg_len; | 
|  | 230 | __le32	request_id; | 
|  | 231 | } __attribute__ ((packed)); | 
|  | 232 |  | 
|  | 233 | struct rndis_keepalive_c {	/* IN (optionally OUT) */ | 
|  | 234 | // header and: | 
|  | 235 | __le32	msg_type;			/* RNDIS_MSG_KEEPALIVE_C */ | 
|  | 236 | __le32	msg_len; | 
|  | 237 | __le32	request_id; | 
|  | 238 | __le32	status; | 
|  | 239 | } __attribute__ ((packed)); | 
|  | 240 |  | 
|  | 241 | /* NOTE:  about 30 OIDs are "mandatory" for peripherals to support ... and | 
|  | 242 | * there are gobs more that may optionally be supported.  We'll avoid as much | 
|  | 243 | * of that mess as possible. | 
|  | 244 | */ | 
|  | 245 | #define OID_802_3_PERMANENT_ADDRESS	ccpu2(0x01010101) | 
|  | 246 | #define OID_GEN_CURRENT_PACKET_FILTER	ccpu2(0x0001010e) | 
|  | 247 |  | 
|  | 248 | /* | 
|  | 249 | * RNDIS notifications from device: command completion; "reverse" | 
|  | 250 | * keepalives; etc | 
|  | 251 | */ | 
|  | 252 | static void rndis_status(struct usbnet *dev, struct urb *urb) | 
|  | 253 | { | 
|  | 254 | devdbg(dev, "rndis status urb, len %d stat %d", | 
|  | 255 | urb->actual_length, urb->status); | 
|  | 256 | // FIXME for keepalives, respond immediately (asynchronously) | 
|  | 257 | // if not an RNDIS status, do like cdc_status(dev,urb) does | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | /* | 
|  | 261 | * RPC done RNDIS-style.  Caller guarantees: | 
|  | 262 | * - message is properly byteswapped | 
|  | 263 | * - there's no other request pending | 
|  | 264 | * - buf can hold up to 1KB response (required by RNDIS spec) | 
|  | 265 | * On return, the first few entries are already byteswapped. | 
|  | 266 | * | 
|  | 267 | * Call context is likely probe(), before interface name is known, | 
|  | 268 | * which is why we won't try to use it in the diagnostics. | 
|  | 269 | */ | 
|  | 270 | static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf) | 
|  | 271 | { | 
|  | 272 | struct cdc_state	*info = (void *) &dev->data; | 
|  | 273 | int			retval; | 
|  | 274 | unsigned		count; | 
|  | 275 | __le32			rsp; | 
|  | 276 | u32			xid = 0, msg_len, request_id; | 
|  | 277 |  | 
|  | 278 | /* REVISIT when this gets called from contexts other than probe() or | 
|  | 279 | * disconnect(): either serialize, or dispatch responses on xid | 
|  | 280 | */ | 
|  | 281 |  | 
|  | 282 | /* Issue the request; don't bother byteswapping our xid */ | 
|  | 283 | if (likely(buf->msg_type != RNDIS_MSG_HALT | 
|  | 284 | && buf->msg_type != RNDIS_MSG_RESET)) { | 
|  | 285 | xid = dev->xid++; | 
|  | 286 | if (!xid) | 
|  | 287 | xid = dev->xid++; | 
|  | 288 | buf->request_id = (__force __le32) xid; | 
|  | 289 | } | 
|  | 290 | retval = usb_control_msg(dev->udev, | 
|  | 291 | usb_sndctrlpipe(dev->udev, 0), | 
|  | 292 | USB_CDC_SEND_ENCAPSULATED_COMMAND, | 
|  | 293 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | 
|  | 294 | 0, info->u->bMasterInterface0, | 
|  | 295 | buf, le32_to_cpu(buf->msg_len), | 
|  | 296 | RNDIS_CONTROL_TIMEOUT_MS); | 
|  | 297 | if (unlikely(retval < 0 || xid == 0)) | 
|  | 298 | return retval; | 
|  | 299 |  | 
|  | 300 | // FIXME Seems like some devices discard responses when | 
|  | 301 | // we time out and cancel our "get response" requests... | 
|  | 302 | // so, this is fragile.  Probably need to poll for status. | 
|  | 303 |  | 
|  | 304 | /* ignore status endpoint, just poll the control channel; | 
|  | 305 | * the request probably completed immediately | 
|  | 306 | */ | 
|  | 307 | rsp = buf->msg_type | RNDIS_MSG_COMPLETION; | 
|  | 308 | for (count = 0; count < 10; count++) { | 
|  | 309 | memset(buf, 0, 1024); | 
|  | 310 | retval = usb_control_msg(dev->udev, | 
|  | 311 | usb_rcvctrlpipe(dev->udev, 0), | 
|  | 312 | USB_CDC_GET_ENCAPSULATED_RESPONSE, | 
|  | 313 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | 
|  | 314 | 0, info->u->bMasterInterface0, | 
|  | 315 | buf, 1024, | 
|  | 316 | RNDIS_CONTROL_TIMEOUT_MS); | 
|  | 317 | if (likely(retval >= 8)) { | 
|  | 318 | msg_len = le32_to_cpu(buf->msg_len); | 
|  | 319 | request_id = (__force u32) buf->request_id; | 
|  | 320 | if (likely(buf->msg_type == rsp)) { | 
|  | 321 | if (likely(request_id == xid)) { | 
|  | 322 | if (unlikely(rsp == RNDIS_MSG_RESET_C)) | 
|  | 323 | return 0; | 
|  | 324 | if (likely(RNDIS_STATUS_SUCCESS | 
|  | 325 | == buf->status)) | 
|  | 326 | return 0; | 
|  | 327 | dev_dbg(&info->control->dev, | 
|  | 328 | "rndis reply status %08x\n", | 
|  | 329 | le32_to_cpu(buf->status)); | 
|  | 330 | return -EL3RST; | 
|  | 331 | } | 
|  | 332 | dev_dbg(&info->control->dev, | 
|  | 333 | "rndis reply id %d expected %d\n", | 
|  | 334 | request_id, xid); | 
|  | 335 | /* then likely retry */ | 
|  | 336 | } else switch (buf->msg_type) { | 
|  | 337 | case RNDIS_MSG_INDICATE: {	/* fault */ | 
|  | 338 | // struct rndis_indicate *msg = (void *)buf; | 
|  | 339 | dev_info(&info->control->dev, | 
|  | 340 | "rndis fault indication\n"); | 
|  | 341 | } | 
|  | 342 | break; | 
|  | 343 | case RNDIS_MSG_KEEPALIVE: {	/* ping */ | 
|  | 344 | struct rndis_keepalive_c *msg = (void *)buf; | 
|  | 345 |  | 
|  | 346 | msg->msg_type = RNDIS_MSG_KEEPALIVE_C; | 
|  | 347 | msg->msg_len = ccpu2(sizeof *msg); | 
|  | 348 | msg->status = RNDIS_STATUS_SUCCESS; | 
|  | 349 | retval = usb_control_msg(dev->udev, | 
|  | 350 | usb_sndctrlpipe(dev->udev, 0), | 
|  | 351 | USB_CDC_SEND_ENCAPSULATED_COMMAND, | 
|  | 352 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | 
|  | 353 | 0, info->u->bMasterInterface0, | 
|  | 354 | msg, sizeof *msg, | 
|  | 355 | RNDIS_CONTROL_TIMEOUT_MS); | 
|  | 356 | if (unlikely(retval < 0)) | 
|  | 357 | dev_dbg(&info->control->dev, | 
|  | 358 | "rndis keepalive err %d\n", | 
|  | 359 | retval); | 
|  | 360 | } | 
|  | 361 | break; | 
|  | 362 | default: | 
|  | 363 | dev_dbg(&info->control->dev, | 
|  | 364 | "unexpected rndis msg %08x len %d\n", | 
|  | 365 | le32_to_cpu(buf->msg_type), msg_len); | 
|  | 366 | } | 
|  | 367 | } else { | 
|  | 368 | /* device probably issued a protocol stall; ignore */ | 
|  | 369 | dev_dbg(&info->control->dev, | 
|  | 370 | "rndis response error, code %d\n", retval); | 
|  | 371 | } | 
|  | 372 | msleep(2); | 
|  | 373 | } | 
|  | 374 | dev_dbg(&info->control->dev, "rndis response timeout\n"); | 
|  | 375 | return -ETIMEDOUT; | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | static int rndis_bind(struct usbnet *dev, struct usb_interface *intf) | 
|  | 379 | { | 
|  | 380 | int			retval; | 
|  | 381 | struct net_device	*net = dev->net; | 
|  | 382 | union { | 
|  | 383 | void			*buf; | 
|  | 384 | struct rndis_msg_hdr	*header; | 
|  | 385 | struct rndis_init	*init; | 
|  | 386 | struct rndis_init_c	*init_c; | 
|  | 387 | struct rndis_query	*get; | 
|  | 388 | struct rndis_query_c	*get_c; | 
|  | 389 | struct rndis_set	*set; | 
|  | 390 | struct rndis_set_c	*set_c; | 
|  | 391 | } u; | 
|  | 392 | u32			tmp; | 
|  | 393 |  | 
|  | 394 | /* we can't rely on i/o from stack working, or stack allocation */ | 
|  | 395 | u.buf = kmalloc(1024, GFP_KERNEL); | 
|  | 396 | if (!u.buf) | 
|  | 397 | return -ENOMEM; | 
|  | 398 | retval = usbnet_generic_cdc_bind(dev, intf); | 
|  | 399 | if (retval < 0) | 
|  | 400 | goto done; | 
|  | 401 |  | 
|  | 402 | net->hard_header_len += sizeof (struct rndis_data_hdr); | 
|  | 403 |  | 
|  | 404 | /* initialize; max transfer is 16KB at full speed */ | 
|  | 405 | u.init->msg_type = RNDIS_MSG_INIT; | 
|  | 406 | u.init->msg_len = ccpu2(sizeof *u.init); | 
|  | 407 | u.init->major_version = ccpu2(1); | 
|  | 408 | u.init->minor_version = ccpu2(0); | 
|  | 409 | u.init->max_transfer_size = ccpu2(net->mtu + net->hard_header_len); | 
|  | 410 |  | 
|  | 411 | retval = rndis_command(dev, u.header); | 
|  | 412 | if (unlikely(retval < 0)) { | 
|  | 413 | /* it might not even be an RNDIS device!! */ | 
|  | 414 | dev_err(&intf->dev, "RNDIS init failed, %d\n", retval); | 
|  | 415 | fail: | 
|  | 416 | usb_driver_release_interface(driver_of(intf), | 
|  | 417 | ((struct cdc_state *)&(dev->data))->data); | 
|  | 418 | goto done; | 
|  | 419 | } | 
|  | 420 | dev->hard_mtu = le32_to_cpu(u.init_c->max_transfer_size); | 
|  | 421 | /* REVISIT:  peripheral "alignment" request is ignored ... */ | 
|  | 422 | dev_dbg(&intf->dev, "hard mtu %u, align %d\n", dev->hard_mtu, | 
|  | 423 | 1 << le32_to_cpu(u.init_c->packet_alignment)); | 
|  | 424 |  | 
|  | 425 | /* get designated host ethernet address */ | 
|  | 426 | memset(u.get, 0, sizeof *u.get); | 
|  | 427 | u.get->msg_type = RNDIS_MSG_QUERY; | 
|  | 428 | u.get->msg_len = ccpu2(sizeof *u.get); | 
|  | 429 | u.get->oid = OID_802_3_PERMANENT_ADDRESS; | 
|  | 430 |  | 
|  | 431 | retval = rndis_command(dev, u.header); | 
|  | 432 | if (unlikely(retval < 0)) { | 
|  | 433 | dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval); | 
|  | 434 | goto fail; | 
|  | 435 | } | 
|  | 436 | tmp = le32_to_cpu(u.get_c->offset); | 
|  | 437 | if (unlikely((tmp + 8) > (1024 - ETH_ALEN) | 
|  | 438 | || u.get_c->len != ccpu2(ETH_ALEN))) { | 
|  | 439 | dev_err(&intf->dev, "rndis ethaddr off %d len %d ?\n", | 
|  | 440 | tmp, le32_to_cpu(u.get_c->len)); | 
|  | 441 | retval = -EDOM; | 
|  | 442 | goto fail; | 
|  | 443 | } | 
|  | 444 | memcpy(net->dev_addr, tmp + (char *)&u.get_c->request_id, ETH_ALEN); | 
|  | 445 |  | 
|  | 446 | /* set a nonzero filter to enable data transfers */ | 
|  | 447 | memset(u.set, 0, sizeof *u.set); | 
|  | 448 | u.set->msg_type = RNDIS_MSG_SET; | 
|  | 449 | u.set->msg_len = ccpu2(4 + sizeof *u.set); | 
|  | 450 | u.set->oid = OID_GEN_CURRENT_PACKET_FILTER; | 
|  | 451 | u.set->len = ccpu2(4); | 
|  | 452 | u.set->offset = ccpu2((sizeof *u.set) - 8); | 
|  | 453 | *(__le32 *)(u.buf + sizeof *u.set) = ccpu2(DEFAULT_FILTER); | 
|  | 454 |  | 
|  | 455 | retval = rndis_command(dev, u.header); | 
|  | 456 | if (unlikely(retval < 0)) { | 
|  | 457 | dev_err(&intf->dev, "rndis set packet filter, %d\n", retval); | 
|  | 458 | goto fail; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | retval = 0; | 
|  | 462 | done: | 
|  | 463 | kfree(u.buf); | 
|  | 464 | return retval; | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | static void rndis_unbind(struct usbnet *dev, struct usb_interface *intf) | 
|  | 468 | { | 
|  | 469 | struct rndis_halt	*halt; | 
|  | 470 |  | 
|  | 471 | /* try to clear any rndis state/activity (no i/o from stack!) */ | 
|  | 472 | halt = kcalloc(1, sizeof *halt, SLAB_KERNEL); | 
|  | 473 | if (halt) { | 
|  | 474 | halt->msg_type = RNDIS_MSG_HALT; | 
|  | 475 | halt->msg_len = ccpu2(sizeof *halt); | 
|  | 476 | (void) rndis_command(dev, (void *)halt); | 
|  | 477 | kfree(halt); | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | return usbnet_cdc_unbind(dev, intf); | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | /* | 
|  | 484 | * DATA -- host must not write zlps | 
|  | 485 | */ | 
|  | 486 | static int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb) | 
|  | 487 | { | 
|  | 488 | /* peripheral may have batched packets to us... */ | 
|  | 489 | while (likely(skb->len)) { | 
|  | 490 | struct rndis_data_hdr	*hdr = (void *)skb->data; | 
|  | 491 | struct sk_buff		*skb2; | 
|  | 492 | u32			msg_len, data_offset, data_len; | 
|  | 493 |  | 
|  | 494 | msg_len = le32_to_cpu(hdr->msg_len); | 
|  | 495 | data_offset = le32_to_cpu(hdr->data_offset); | 
|  | 496 | data_len = le32_to_cpu(hdr->data_len); | 
|  | 497 |  | 
|  | 498 | /* don't choke if we see oob, per-packet data, etc */ | 
|  | 499 | if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET | 
|  | 500 | || skb->len < msg_len | 
|  | 501 | || (data_offset + data_len + 8) > msg_len)) { | 
|  | 502 | dev->stats.rx_frame_errors++; | 
|  | 503 | devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d", | 
|  | 504 | le32_to_cpu(hdr->msg_type), | 
|  | 505 | msg_len, data_offset, data_len, skb->len); | 
|  | 506 | return 0; | 
|  | 507 | } | 
|  | 508 | skb_pull(skb, 8 + data_offset); | 
|  | 509 |  | 
|  | 510 | /* at most one packet left? */ | 
|  | 511 | if (likely((data_len - skb->len) <= sizeof *hdr)) { | 
|  | 512 | skb_trim(skb, data_len); | 
|  | 513 | break; | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | /* try to return all the packets in the batch */ | 
|  | 517 | skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 518 | if (unlikely(!skb2)) | 
|  | 519 | break; | 
|  | 520 | skb_pull(skb, msg_len - sizeof *hdr); | 
|  | 521 | skb_trim(skb2, data_len); | 
|  | 522 | usbnet_skb_return(dev, skb2); | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | /* caller will usbnet_skb_return the remaining packet */ | 
|  | 526 | return 1; | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | static struct sk_buff * | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 530 | rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 531 | { | 
|  | 532 | struct rndis_data_hdr	*hdr; | 
|  | 533 | struct sk_buff		*skb2; | 
|  | 534 | unsigned		len = skb->len; | 
|  | 535 |  | 
|  | 536 | if (likely(!skb_cloned(skb))) { | 
|  | 537 | int	room = skb_headroom(skb); | 
|  | 538 |  | 
|  | 539 | /* enough head room as-is? */ | 
|  | 540 | if (unlikely((sizeof *hdr) <= room)) | 
|  | 541 | goto fill; | 
|  | 542 |  | 
|  | 543 | /* enough room, but needs to be readjusted? */ | 
|  | 544 | room += skb_tailroom(skb); | 
|  | 545 | if (likely((sizeof *hdr) <= room)) { | 
|  | 546 | skb->data = memmove(skb->head + sizeof *hdr, | 
|  | 547 | skb->data, len); | 
|  | 548 | skb->tail = skb->data + len; | 
|  | 549 | goto fill; | 
|  | 550 | } | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 | /* create a new skb, with the correct size (and tailpad) */ | 
|  | 554 | skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags); | 
|  | 555 | dev_kfree_skb_any(skb); | 
|  | 556 | if (unlikely(!skb2)) | 
|  | 557 | return skb2; | 
|  | 558 | skb = skb2; | 
|  | 559 |  | 
|  | 560 | /* fill out the RNDIS header.  we won't bother trying to batch | 
|  | 561 | * packets; Linux minimizes wasted bandwidth through tx queues. | 
|  | 562 | */ | 
|  | 563 | fill: | 
|  | 564 | hdr = (void *) __skb_push(skb, sizeof *hdr); | 
|  | 565 | memset(hdr, 0, sizeof *hdr); | 
|  | 566 | hdr->msg_type = RNDIS_MSG_PACKET; | 
|  | 567 | hdr->msg_len = cpu_to_le32(skb->len); | 
|  | 568 | hdr->data_offset = ccpu2(sizeof(*hdr) - 8); | 
|  | 569 | hdr->data_len = cpu_to_le32(len); | 
|  | 570 |  | 
|  | 571 | /* FIXME make the last packet always be short ... */ | 
|  | 572 | return skb; | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 |  | 
|  | 576 | static const struct driver_info	rndis_info = { | 
|  | 577 | .description =	"RNDIS device", | 
|  | 578 | .flags =	FLAG_ETHER | FLAG_FRAMING_RN, | 
|  | 579 | .bind =		rndis_bind, | 
|  | 580 | .unbind =	rndis_unbind, | 
|  | 581 | .status =	rndis_status, | 
|  | 582 | .rx_fixup =	rndis_rx_fixup, | 
|  | 583 | .tx_fixup =	rndis_tx_fixup, | 
|  | 584 | }; | 
|  | 585 |  | 
|  | 586 | #undef ccpu2 | 
|  | 587 |  | 
|  | 588 |  | 
|  | 589 | /*-------------------------------------------------------------------------*/ | 
|  | 590 |  | 
|  | 591 | static const struct usb_device_id	products [] = { | 
|  | 592 | { | 
|  | 593 | /* RNDIS is MSFT's un-official variant of CDC ACM */ | 
|  | 594 | USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff), | 
|  | 595 | .driver_info = (unsigned long) &rndis_info, | 
|  | 596 | }, | 
|  | 597 | { },		// END | 
|  | 598 | }; | 
|  | 599 | MODULE_DEVICE_TABLE(usb, products); | 
|  | 600 |  | 
|  | 601 | static struct usb_driver rndis_driver = { | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 602 | .name =		"rndis_host", | 
|  | 603 | .id_table =	products, | 
|  | 604 | .probe =	usbnet_probe, | 
|  | 605 | .disconnect =	usbnet_disconnect, | 
|  | 606 | .suspend =	usbnet_suspend, | 
|  | 607 | .resume =	usbnet_resume, | 
|  | 608 | }; | 
|  | 609 |  | 
|  | 610 | static int __init rndis_init(void) | 
|  | 611 | { | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 612 | return usb_register(&rndis_driver); | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 613 | } | 
|  | 614 | module_init(rndis_init); | 
|  | 615 |  | 
|  | 616 | static void __exit rndis_exit(void) | 
|  | 617 | { | 
| David Brownell | 51400f1 | 2006-04-02 10:19:08 -0800 | [diff] [blame] | 618 | usb_deregister(&rndis_driver); | 
| David Brownell | 64e0491 | 2005-08-31 09:54:36 -0700 | [diff] [blame] | 619 | } | 
|  | 620 | module_exit(rndis_exit); | 
|  | 621 |  | 
|  | 622 | MODULE_AUTHOR("David Brownell"); | 
|  | 623 | MODULE_DESCRIPTION("USB Host side RNDIS driver"); | 
|  | 624 | MODULE_LICENSE("GPL"); |