| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Net1080 based USB host-to-host cables | 
 | 3 |  * Copyright (C) 2000-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 | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 23 | #include <linux/module.h> | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 24 | #include <linux/init.h> | 
 | 25 | #include <linux/netdevice.h> | 
 | 26 | #include <linux/etherdevice.h> | 
 | 27 | #include <linux/ethtool.h> | 
 | 28 | #include <linux/workqueue.h> | 
 | 29 | #include <linux/mii.h> | 
 | 30 | #include <linux/usb.h> | 
 | 31 |  | 
 | 32 | #include <asm/unaligned.h> | 
 | 33 |  | 
 | 34 | #include "usbnet.h" | 
 | 35 |  | 
 | 36 |  | 
 | 37 | /* | 
 | 38 |  * Netchip 1080 driver ... http://www.netchip.com | 
 | 39 |  * (Sept 2004:  End-of-life announcement has been sent.) | 
 | 40 |  * Used in (some) LapLink cables | 
 | 41 |  */ | 
 | 42 |  | 
 | 43 | #define frame_errors	data[1] | 
 | 44 |  | 
 | 45 | /* | 
 | 46 |  * NetChip framing of ethernet packets, supporting additional error | 
 | 47 |  * checks for links that may drop bulk packets from inside messages. | 
 | 48 |  * Odd USB length == always short read for last usb packet. | 
 | 49 |  *	- nc_header | 
 | 50 |  *	- Ethernet header (14 bytes) | 
 | 51 |  *	- payload | 
 | 52 |  *	- (optional padding byte, if needed so length becomes odd) | 
 | 53 |  *	- nc_trailer | 
 | 54 |  * | 
 | 55 |  * This framing is to be avoided for non-NetChip devices. | 
 | 56 |  */ | 
 | 57 |  | 
 | 58 | struct nc_header {		// packed: | 
 | 59 | 	__le16	hdr_len;		// sizeof nc_header (LE, all) | 
 | 60 | 	__le16	packet_len;		// payload size (including ethhdr) | 
 | 61 | 	__le16	packet_id;		// detects dropped packets | 
 | 62 | #define MIN_HEADER	6 | 
 | 63 |  | 
 | 64 | 	// all else is optional, and must start with: | 
 | 65 | 	// __le16	vendorId;	// from usb-if | 
 | 66 | 	// __le16	productId; | 
 | 67 | } __attribute__((__packed__)); | 
 | 68 |  | 
 | 69 | #define	PAD_BYTE	((unsigned char)0xAC) | 
 | 70 |  | 
 | 71 | struct nc_trailer { | 
 | 72 | 	__le16	packet_id; | 
 | 73 | } __attribute__((__packed__)); | 
 | 74 |  | 
 | 75 | // packets may use FLAG_FRAMING_NC and optional pad | 
 | 76 | #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \ | 
 | 77 | 				+ sizeof (struct ethhdr) \ | 
 | 78 | 				+ (mtu) \ | 
 | 79 | 				+ 1 \ | 
 | 80 | 				+ sizeof (struct nc_trailer)) | 
 | 81 |  | 
 | 82 | #define MIN_FRAMED	FRAMED_SIZE(0) | 
 | 83 |  | 
 | 84 | /* packets _could_ be up to 64KB... */ | 
 | 85 | #define NC_MAX_PACKET	32767 | 
 | 86 |  | 
 | 87 |  | 
 | 88 | /* | 
 | 89 |  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued | 
 | 90 |  * before the hardware drops it.  If that's done, the driver will need to | 
 | 91 |  * frame network packets to guard against the dropped USB packets.  The win32 | 
 | 92 |  * driver sets this for both sides of the link. | 
 | 93 |  */ | 
 | 94 | #define	NC_READ_TTL_MS	((u8)255)	// ms | 
 | 95 |  | 
 | 96 | /* | 
 | 97 |  * We ignore most registers and EEPROM contents. | 
 | 98 |  */ | 
 | 99 | #define	REG_USBCTL	((u8)0x04) | 
 | 100 | #define REG_TTL		((u8)0x10) | 
 | 101 | #define REG_STATUS	((u8)0x11) | 
 | 102 |  | 
 | 103 | /* | 
 | 104 |  * Vendor specific requests to read/write data | 
 | 105 |  */ | 
 | 106 | #define	REQUEST_REGISTER	((u8)0x10) | 
 | 107 | #define	REQUEST_EEPROM		((u8)0x11) | 
 | 108 |  | 
 | 109 | static int | 
 | 110 | nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr) | 
 | 111 | { | 
 | 112 | 	int status = usb_control_msg(dev->udev, | 
 | 113 | 		usb_rcvctrlpipe(dev->udev, 0), | 
 | 114 | 		req, | 
 | 115 | 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | 
 | 116 | 		0, regnum, | 
 | 117 | 		retval_ptr, sizeof *retval_ptr, | 
 | 118 | 		USB_CTRL_GET_TIMEOUT); | 
 | 119 | 	if (status > 0) | 
 | 120 | 		status = 0; | 
 | 121 | 	if (!status) | 
 | 122 | 		le16_to_cpus(retval_ptr); | 
 | 123 | 	return status; | 
 | 124 | } | 
 | 125 |  | 
 | 126 | static inline int | 
 | 127 | nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr) | 
 | 128 | { | 
 | 129 | 	return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr); | 
 | 130 | } | 
 | 131 |  | 
 | 132 | // no retval ... can become async, usable in_interrupt() | 
 | 133 | static void | 
 | 134 | nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value) | 
 | 135 | { | 
 | 136 | 	usb_control_msg(dev->udev, | 
 | 137 | 		usb_sndctrlpipe(dev->udev, 0), | 
 | 138 | 		req, | 
 | 139 | 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | 
 | 140 | 		value, regnum, | 
 | 141 | 		NULL, 0,			// data is in setup packet | 
 | 142 | 		USB_CTRL_SET_TIMEOUT); | 
 | 143 | } | 
 | 144 |  | 
 | 145 | static inline void | 
 | 146 | nc_register_write(struct usbnet *dev, u8 regnum, u16 value) | 
 | 147 | { | 
 | 148 | 	nc_vendor_write(dev, REQUEST_REGISTER, regnum, value); | 
 | 149 | } | 
 | 150 |  | 
 | 151 |  | 
 | 152 | #if 0 | 
 | 153 | static void nc_dump_registers(struct usbnet *dev) | 
 | 154 | { | 
 | 155 | 	u8	reg; | 
 | 156 | 	u16	*vp = kmalloc(sizeof (u16)); | 
 | 157 |  | 
 | 158 | 	if (!vp) { | 
 | 159 | 		dbg("no memory?"); | 
 | 160 | 		return; | 
 | 161 | 	} | 
 | 162 |  | 
 | 163 | 	dbg("%s registers:", dev->net->name); | 
 | 164 | 	for (reg = 0; reg < 0x20; reg++) { | 
 | 165 | 		int retval; | 
 | 166 |  | 
 | 167 | 		// reading some registers is trouble | 
 | 168 | 		if (reg >= 0x08 && reg <= 0xf) | 
 | 169 | 			continue; | 
 | 170 | 		if (reg >= 0x12 && reg <= 0x1e) | 
 | 171 | 			continue; | 
 | 172 |  | 
 | 173 | 		retval = nc_register_read(dev, reg, vp); | 
 | 174 | 		if (retval < 0) | 
 | 175 | 			dbg("%s reg [0x%x] ==> error %d", | 
 | 176 | 				dev->net->name, reg, retval); | 
 | 177 | 		else | 
 | 178 | 			dbg("%s reg [0x%x] = 0x%x", | 
 | 179 | 				dev->net->name, reg, *vp); | 
 | 180 | 	} | 
 | 181 | 	kfree(vp); | 
 | 182 | } | 
 | 183 | #endif | 
 | 184 |  | 
 | 185 |  | 
 | 186 | /*-------------------------------------------------------------------------*/ | 
 | 187 |  | 
 | 188 | /* | 
 | 189 |  * Control register | 
 | 190 |  */ | 
 | 191 |  | 
 | 192 | #define	USBCTL_WRITABLE_MASK	0x1f0f | 
 | 193 | // bits 15-13 reserved, r/o | 
 | 194 | #define	USBCTL_ENABLE_LANG	(1 << 12) | 
 | 195 | #define	USBCTL_ENABLE_MFGR	(1 << 11) | 
 | 196 | #define	USBCTL_ENABLE_PROD	(1 << 10) | 
 | 197 | #define	USBCTL_ENABLE_SERIAL	(1 << 9) | 
 | 198 | #define	USBCTL_ENABLE_DEFAULTS	(1 << 8) | 
 | 199 | // bits 7-4 reserved, r/o | 
 | 200 | #define	USBCTL_FLUSH_OTHER	(1 << 3) | 
 | 201 | #define	USBCTL_FLUSH_THIS	(1 << 2) | 
 | 202 | #define	USBCTL_DISCONN_OTHER	(1 << 1) | 
 | 203 | #define	USBCTL_DISCONN_THIS	(1 << 0) | 
 | 204 |  | 
 | 205 | static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl) | 
 | 206 | { | 
 | 207 | 	if (!netif_msg_link(dev)) | 
 | 208 | 		return; | 
 | 209 | 	devdbg(dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;" | 
 | 210 | 			" this%s%s;" | 
 | 211 | 			" other%s%s; r/o 0x%x", | 
 | 212 | 		dev->udev->bus->bus_name, dev->udev->devpath, | 
 | 213 | 		usbctl, | 
 | 214 | 		(usbctl & USBCTL_ENABLE_LANG) ? " lang" : "", | 
 | 215 | 		(usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "", | 
 | 216 | 		(usbctl & USBCTL_ENABLE_PROD) ? " prod" : "", | 
 | 217 | 		(usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "", | 
 | 218 | 		(usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "", | 
 | 219 |  | 
 | 220 | 		(usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "", | 
 | 221 | 		(usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "", | 
 | 222 | 		(usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "", | 
 | 223 | 		(usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "", | 
 | 224 | 		usbctl & ~USBCTL_WRITABLE_MASK | 
 | 225 | 		); | 
 | 226 | } | 
 | 227 |  | 
 | 228 | /*-------------------------------------------------------------------------*/ | 
 | 229 |  | 
 | 230 | /* | 
 | 231 |  * Status register | 
 | 232 |  */ | 
 | 233 |  | 
 | 234 | #define	STATUS_PORT_A		(1 << 15) | 
 | 235 |  | 
 | 236 | #define	STATUS_CONN_OTHER	(1 << 14) | 
 | 237 | #define	STATUS_SUSPEND_OTHER	(1 << 13) | 
 | 238 | #define	STATUS_MAILBOX_OTHER	(1 << 12) | 
| Jean Delvare | b4ee4a2 | 2006-11-09 22:02:37 +0100 | [diff] [blame] | 239 | #define	STATUS_PACKETS_OTHER(n)	(((n) >> 8) & 0x03) | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 240 |  | 
 | 241 | #define	STATUS_CONN_THIS	(1 << 6) | 
 | 242 | #define	STATUS_SUSPEND_THIS	(1 << 5) | 
 | 243 | #define	STATUS_MAILBOX_THIS	(1 << 4) | 
| Jean Delvare | b4ee4a2 | 2006-11-09 22:02:37 +0100 | [diff] [blame] | 244 | #define	STATUS_PACKETS_THIS(n)	(((n) >> 0) & 0x03) | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 245 |  | 
 | 246 | #define	STATUS_UNSPEC_MASK	0x0c8c | 
 | 247 | #define	STATUS_NOISE_MASK 	((u16)~(0x0303|STATUS_UNSPEC_MASK)) | 
 | 248 |  | 
 | 249 |  | 
 | 250 | static inline void nc_dump_status(struct usbnet *dev, u16 status) | 
 | 251 | { | 
 | 252 | 	if (!netif_msg_link(dev)) | 
 | 253 | 		return; | 
 | 254 | 	devdbg(dev, "net1080 %s-%s status 0x%x:" | 
 | 255 | 			" this (%c) PKT=%d%s%s%s;" | 
 | 256 | 			" other PKT=%d%s%s%s; unspec 0x%x", | 
 | 257 | 		dev->udev->bus->bus_name, dev->udev->devpath, | 
 | 258 | 		status, | 
 | 259 |  | 
 | 260 | 		// XXX the packet counts don't seem right | 
 | 261 | 		// (1 at reset, not 0); maybe UNSPEC too | 
 | 262 |  | 
 | 263 | 		(status & STATUS_PORT_A) ? 'A' : 'B', | 
 | 264 | 		STATUS_PACKETS_THIS(status), | 
 | 265 | 		(status & STATUS_CONN_THIS) ? " CON" : "", | 
 | 266 | 		(status & STATUS_SUSPEND_THIS) ? " SUS" : "", | 
 | 267 | 		(status & STATUS_MAILBOX_THIS) ? " MBOX" : "", | 
 | 268 |  | 
 | 269 | 		STATUS_PACKETS_OTHER(status), | 
 | 270 | 		(status & STATUS_CONN_OTHER) ? " CON" : "", | 
 | 271 | 		(status & STATUS_SUSPEND_OTHER) ? " SUS" : "", | 
 | 272 | 		(status & STATUS_MAILBOX_OTHER) ? " MBOX" : "", | 
 | 273 |  | 
 | 274 | 		status & STATUS_UNSPEC_MASK | 
 | 275 | 		); | 
 | 276 | } | 
 | 277 |  | 
 | 278 | /*-------------------------------------------------------------------------*/ | 
 | 279 |  | 
 | 280 | /* | 
 | 281 |  * TTL register | 
 | 282 |  */ | 
 | 283 |  | 
 | 284 | #define	TTL_THIS(ttl)	(0x00ff & ttl) | 
 | 285 | #define	TTL_OTHER(ttl)	(0x00ff & (ttl >> 8)) | 
 | 286 | #define MK_TTL(this,other)	((u16)(((other)<<8)|(0x00ff&(this)))) | 
 | 287 |  | 
 | 288 | static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl) | 
 | 289 | { | 
 | 290 | 	if (netif_msg_link(dev)) | 
 | 291 | 		devdbg(dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d", | 
 | 292 | 			dev->udev->bus->bus_name, dev->udev->devpath, | 
 | 293 | 			ttl, TTL_THIS(ttl), TTL_OTHER(ttl)); | 
 | 294 | } | 
 | 295 |  | 
 | 296 | /*-------------------------------------------------------------------------*/ | 
 | 297 |  | 
 | 298 | static int net1080_reset(struct usbnet *dev) | 
 | 299 | { | 
 | 300 | 	u16		usbctl, status, ttl; | 
 | 301 | 	u16		*vp = kmalloc(sizeof (u16), GFP_KERNEL); | 
 | 302 | 	int		retval; | 
 | 303 |  | 
 | 304 | 	if (!vp) | 
 | 305 | 		return -ENOMEM; | 
 | 306 |  | 
 | 307 | 	// nc_dump_registers(dev); | 
 | 308 |  | 
 | 309 | 	if ((retval = nc_register_read(dev, REG_STATUS, vp)) < 0) { | 
 | 310 | 		dbg("can't read %s-%s status: %d", | 
 | 311 | 			dev->udev->bus->bus_name, dev->udev->devpath, retval); | 
 | 312 | 		goto done; | 
 | 313 | 	} | 
 | 314 | 	status = *vp; | 
 | 315 | 	nc_dump_status(dev, status); | 
 | 316 |  | 
 | 317 | 	if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) { | 
 | 318 | 		dbg("can't read USBCTL, %d", retval); | 
 | 319 | 		goto done; | 
 | 320 | 	} | 
 | 321 | 	usbctl = *vp; | 
 | 322 | 	nc_dump_usbctl(dev, usbctl); | 
 | 323 |  | 
 | 324 | 	nc_register_write(dev, REG_USBCTL, | 
 | 325 | 			USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER); | 
 | 326 |  | 
 | 327 | 	if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) { | 
 | 328 | 		dbg("can't read TTL, %d", retval); | 
 | 329 | 		goto done; | 
 | 330 | 	} | 
 | 331 | 	ttl = *vp; | 
 | 332 | 	// nc_dump_ttl(dev, ttl); | 
 | 333 |  | 
 | 334 | 	nc_register_write(dev, REG_TTL, | 
 | 335 | 			MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) ); | 
 | 336 | 	dbg("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS); | 
 | 337 |  | 
 | 338 | 	if (netif_msg_link(dev)) | 
 | 339 | 		devinfo(dev, "port %c, peer %sconnected", | 
 | 340 | 			(status & STATUS_PORT_A) ? 'A' : 'B', | 
 | 341 | 			(status & STATUS_CONN_OTHER) ? "" : "dis" | 
 | 342 | 			); | 
 | 343 | 	retval = 0; | 
 | 344 |  | 
 | 345 | done: | 
 | 346 | 	kfree(vp); | 
 | 347 | 	return retval; | 
 | 348 | } | 
 | 349 |  | 
 | 350 | static int net1080_check_connect(struct usbnet *dev) | 
 | 351 | { | 
 | 352 | 	int			retval; | 
 | 353 | 	u16			status; | 
 | 354 | 	u16			*vp = kmalloc(sizeof (u16), GFP_KERNEL); | 
 | 355 |  | 
 | 356 | 	if (!vp) | 
 | 357 | 		return -ENOMEM; | 
 | 358 | 	retval = nc_register_read(dev, REG_STATUS, vp); | 
 | 359 | 	status = *vp; | 
 | 360 | 	kfree(vp); | 
 | 361 | 	if (retval != 0) { | 
 | 362 | 		dbg("%s net1080_check_conn read - %d", dev->net->name, retval); | 
 | 363 | 		return retval; | 
 | 364 | 	} | 
 | 365 | 	if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER) | 
 | 366 | 		return -ENOLINK; | 
 | 367 | 	return 0; | 
 | 368 | } | 
 | 369 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 370 | static void nc_flush_complete(struct urb *urb) | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 371 | { | 
 | 372 | 	kfree(urb->context); | 
 | 373 | 	usb_free_urb(urb); | 
 | 374 | } | 
 | 375 |  | 
 | 376 | static void nc_ensure_sync(struct usbnet *dev) | 
 | 377 | { | 
 | 378 | 	dev->frame_errors++; | 
 | 379 | 	if (dev->frame_errors > 5) { | 
 | 380 | 		struct urb		*urb; | 
 | 381 | 		struct usb_ctrlrequest	*req; | 
 | 382 | 		int			status; | 
 | 383 |  | 
 | 384 | 		/* Send a flush */ | 
| Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 385 | 		urb = usb_alloc_urb(0, GFP_ATOMIC); | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 386 | 		if (!urb) | 
 | 387 | 			return; | 
 | 388 |  | 
 | 389 | 		req = kmalloc(sizeof *req, GFP_ATOMIC); | 
 | 390 | 		if (!req) { | 
 | 391 | 			usb_free_urb(urb); | 
 | 392 | 			return; | 
 | 393 | 		} | 
 | 394 |  | 
 | 395 | 		req->bRequestType = USB_DIR_OUT | 
 | 396 | 			| USB_TYPE_VENDOR | 
 | 397 | 			| USB_RECIP_DEVICE; | 
 | 398 | 		req->bRequest = REQUEST_REGISTER; | 
 | 399 | 		req->wValue = cpu_to_le16(USBCTL_FLUSH_THIS | 
 | 400 | 				| USBCTL_FLUSH_OTHER); | 
 | 401 | 		req->wIndex = cpu_to_le16(REG_USBCTL); | 
 | 402 | 		req->wLength = cpu_to_le16(0); | 
 | 403 |  | 
 | 404 | 		/* queue an async control request, we don't need | 
 | 405 | 		 * to do anything when it finishes except clean up. | 
 | 406 | 		 */ | 
 | 407 | 		usb_fill_control_urb(urb, dev->udev, | 
 | 408 | 			usb_sndctrlpipe(dev->udev, 0), | 
 | 409 | 			(unsigned char *) req, | 
 | 410 | 			NULL, 0, | 
 | 411 | 			nc_flush_complete, req); | 
 | 412 | 		status = usb_submit_urb(urb, GFP_ATOMIC); | 
 | 413 | 		if (status) { | 
 | 414 | 			kfree(req); | 
 | 415 | 			usb_free_urb(urb); | 
 | 416 | 			return; | 
 | 417 | 		} | 
 | 418 |  | 
 | 419 | 		if (netif_msg_rx_err(dev)) | 
 | 420 | 			devdbg(dev, "flush net1080; too many framing errors"); | 
 | 421 | 		dev->frame_errors = 0; | 
 | 422 | 	} | 
 | 423 | } | 
 | 424 |  | 
 | 425 | static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb) | 
 | 426 | { | 
 | 427 | 	struct nc_header	*header; | 
 | 428 | 	struct nc_trailer	*trailer; | 
 | 429 | 	u16			hdr_len, packet_len; | 
 | 430 |  | 
 | 431 | 	if (!(skb->len & 0x01)) { | 
 | 432 | #ifdef DEBUG | 
 | 433 | 		struct net_device	*net = dev->net; | 
 | 434 | 		dbg("rx framesize %d range %d..%d mtu %d", skb->len, | 
 | 435 | 			net->hard_header_len, dev->hard_mtu, net->mtu); | 
 | 436 | #endif | 
 | 437 | 		dev->stats.rx_frame_errors++; | 
 | 438 | 		nc_ensure_sync(dev); | 
 | 439 | 		return 0; | 
 | 440 | 	} | 
 | 441 |  | 
 | 442 | 	header = (struct nc_header *) skb->data; | 
 | 443 | 	hdr_len = le16_to_cpup(&header->hdr_len); | 
 | 444 | 	packet_len = le16_to_cpup(&header->packet_len); | 
 | 445 | 	if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) { | 
 | 446 | 		dev->stats.rx_frame_errors++; | 
 | 447 | 		dbg("packet too big, %d", packet_len); | 
 | 448 | 		nc_ensure_sync(dev); | 
 | 449 | 		return 0; | 
 | 450 | 	} else if (hdr_len < MIN_HEADER) { | 
 | 451 | 		dev->stats.rx_frame_errors++; | 
 | 452 | 		dbg("header too short, %d", hdr_len); | 
 | 453 | 		nc_ensure_sync(dev); | 
 | 454 | 		return 0; | 
 | 455 | 	} else if (hdr_len > MIN_HEADER) { | 
 | 456 | 		// out of band data for us? | 
 | 457 | 		dbg("header OOB, %d bytes", hdr_len - MIN_HEADER); | 
 | 458 | 		nc_ensure_sync(dev); | 
 | 459 | 		// switch (vendor/product ids) { ... } | 
 | 460 | 	} | 
 | 461 | 	skb_pull(skb, hdr_len); | 
 | 462 |  | 
 | 463 | 	trailer = (struct nc_trailer *) | 
 | 464 | 		(skb->data + skb->len - sizeof *trailer); | 
 | 465 | 	skb_trim(skb, skb->len - sizeof *trailer); | 
 | 466 |  | 
 | 467 | 	if ((packet_len & 0x01) == 0) { | 
 | 468 | 		if (skb->data [packet_len] != PAD_BYTE) { | 
 | 469 | 			dev->stats.rx_frame_errors++; | 
 | 470 | 			dbg("bad pad"); | 
 | 471 | 			return 0; | 
 | 472 | 		} | 
 | 473 | 		skb_trim(skb, skb->len - 1); | 
 | 474 | 	} | 
 | 475 | 	if (skb->len != packet_len) { | 
 | 476 | 		dev->stats.rx_frame_errors++; | 
 | 477 | 		dbg("bad packet len %d (expected %d)", | 
 | 478 | 			skb->len, packet_len); | 
 | 479 | 		nc_ensure_sync(dev); | 
 | 480 | 		return 0; | 
 | 481 | 	} | 
 | 482 | 	if (header->packet_id != get_unaligned(&trailer->packet_id)) { | 
 | 483 | 		dev->stats.rx_fifo_errors++; | 
 | 484 | 		dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x", | 
 | 485 | 			le16_to_cpu(header->packet_id), | 
 | 486 | 			le16_to_cpu(trailer->packet_id)); | 
 | 487 | 		return 0; | 
 | 488 | 	} | 
 | 489 | #if 0 | 
 | 490 | 	devdbg(dev, "frame <rx h %d p %d id %d", header->hdr_len, | 
 | 491 | 		header->packet_len, header->packet_id); | 
 | 492 | #endif | 
 | 493 | 	dev->frame_errors = 0; | 
 | 494 | 	return 1; | 
 | 495 | } | 
 | 496 |  | 
 | 497 | static struct sk_buff * | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 498 | net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 499 | { | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 500 | 	struct sk_buff		*skb2; | 
 | 501 | 	struct nc_header	*header = NULL; | 
 | 502 | 	struct nc_trailer	*trailer = NULL; | 
| dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 503 | 	int			padlen = sizeof (struct nc_trailer); | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 504 | 	int			len = skb->len; | 
 | 505 |  | 
| dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 506 | 	if (!((len + padlen + sizeof (struct nc_header)) & 0x01)) | 
 | 507 | 		padlen++; | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 508 | 	if (!skb_cloned(skb)) { | 
 | 509 | 		int	headroom = skb_headroom(skb); | 
 | 510 | 		int	tailroom = skb_tailroom(skb); | 
 | 511 |  | 
| dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 512 | 		if (padlen <= tailroom && | 
 | 513 | 		    sizeof(struct nc_header) <= headroom) | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 514 | 			/* There's enough head and tail room */ | 
 | 515 | 			goto encapsulate; | 
 | 516 |  | 
| dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 517 | 		if ((sizeof (struct nc_header) + padlen) < | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 518 | 				(headroom + tailroom)) { | 
 | 519 | 			/* There's enough total room, so just readjust */ | 
 | 520 | 			skb->data = memmove(skb->head | 
 | 521 | 						+ sizeof (struct nc_header), | 
 | 522 | 					    skb->data, skb->len); | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 523 | 			skb_set_tail_pointer(skb, len); | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 524 | 			goto encapsulate; | 
 | 525 | 		} | 
 | 526 | 	} | 
 | 527 |  | 
 | 528 | 	/* Create a new skb to use with the correct size */ | 
 | 529 | 	skb2 = skb_copy_expand(skb, | 
 | 530 | 				sizeof (struct nc_header), | 
| dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 531 | 				padlen, | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 532 | 				flags); | 
 | 533 | 	dev_kfree_skb_any(skb); | 
 | 534 | 	if (!skb2) | 
 | 535 | 		return skb2; | 
 | 536 | 	skb = skb2; | 
 | 537 |  | 
 | 538 | encapsulate: | 
 | 539 | 	/* header first */ | 
 | 540 | 	header = (struct nc_header *) skb_push(skb, sizeof *header); | 
 | 541 | 	header->hdr_len = cpu_to_le16(sizeof (*header)); | 
 | 542 | 	header->packet_len = cpu_to_le16(len); | 
 | 543 | 	header->packet_id = cpu_to_le16((u16)dev->xid++); | 
 | 544 |  | 
 | 545 | 	/* maybe pad; then trailer */ | 
 | 546 | 	if (!((skb->len + sizeof *trailer) & 0x01)) | 
 | 547 | 		*skb_put(skb, 1) = PAD_BYTE; | 
 | 548 | 	trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer); | 
 | 549 | 	put_unaligned(header->packet_id, &trailer->packet_id); | 
 | 550 | #if 0 | 
 | 551 | 	devdbg(dev, "frame >tx h %d p %d id %d", | 
 | 552 | 		header->hdr_len, header->packet_len, | 
 | 553 | 		header->packet_id); | 
 | 554 | #endif | 
 | 555 | 	return skb; | 
 | 556 | } | 
 | 557 |  | 
 | 558 | static int net1080_bind(struct usbnet *dev, struct usb_interface *intf) | 
 | 559 | { | 
 | 560 | 	unsigned	extra = sizeof (struct nc_header) | 
 | 561 | 				+ 1 | 
 | 562 | 				+ sizeof (struct nc_trailer); | 
 | 563 |  | 
 | 564 | 	dev->net->hard_header_len += extra; | 
 | 565 | 	dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu; | 
 | 566 | 	dev->hard_mtu = NC_MAX_PACKET; | 
 | 567 | 	return usbnet_get_endpoints (dev, intf); | 
 | 568 | } | 
 | 569 |  | 
 | 570 | static const struct driver_info	net1080_info = { | 
 | 571 | 	.description =	"NetChip TurboCONNECT", | 
 | 572 | 	.flags =	FLAG_FRAMING_NC, | 
 | 573 | 	.bind =		net1080_bind, | 
 | 574 | 	.reset =	net1080_reset, | 
 | 575 | 	.check_connect = net1080_check_connect, | 
 | 576 | 	.rx_fixup =	net1080_rx_fixup, | 
 | 577 | 	.tx_fixup =	net1080_tx_fixup, | 
 | 578 | }; | 
 | 579 |  | 
 | 580 | static const struct usb_device_id	products [] = { | 
 | 581 | { | 
 | 582 | 	USB_DEVICE(0x0525, 0x1080),	// NetChip ref design | 
 | 583 | 	.driver_info =	(unsigned long) &net1080_info, | 
 | 584 | }, { | 
 | 585 | 	USB_DEVICE(0x06D0, 0x0622),	// Laplink Gold | 
 | 586 | 	.driver_info =	(unsigned long) &net1080_info, | 
 | 587 | }, | 
 | 588 | 	{ },		// END | 
 | 589 | }; | 
 | 590 | MODULE_DEVICE_TABLE(usb, products); | 
 | 591 |  | 
 | 592 | static struct usb_driver net1080_driver = { | 
| David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 593 | 	.name =		"net1080", | 
 | 594 | 	.id_table =	products, | 
 | 595 | 	.probe =	usbnet_probe, | 
 | 596 | 	.disconnect =	usbnet_disconnect, | 
 | 597 | 	.suspend =	usbnet_suspend, | 
 | 598 | 	.resume =	usbnet_resume, | 
 | 599 | }; | 
 | 600 |  | 
 | 601 | static int __init net1080_init(void) | 
 | 602 | { | 
 | 603 |  	return usb_register(&net1080_driver); | 
 | 604 | } | 
 | 605 | module_init(net1080_init); | 
 | 606 |  | 
 | 607 | static void __exit net1080_exit(void) | 
 | 608 | { | 
 | 609 |  	usb_deregister(&net1080_driver); | 
 | 610 | } | 
 | 611 | module_exit(net1080_exit); | 
 | 612 |  | 
 | 613 | MODULE_AUTHOR("David Brownell"); | 
 | 614 | MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links"); | 
 | 615 | MODULE_LICENSE("GPL"); |