xref: /openbmc/linux/drivers/net/usb/kaweth.c (revision af3563be9d09ea2c371974ee238169a680c9398a)
1aaf4989bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25b2fc499SJeff Garzik /****************************************************************
35b2fc499SJeff Garzik  *
45b2fc499SJeff Garzik  *     kaweth.c - driver for KL5KUSB101 based USB->Ethernet
55b2fc499SJeff Garzik  *
65b2fc499SJeff Garzik  *     (c) 2000 Interlan Communications
75b2fc499SJeff Garzik  *     (c) 2000 Stephane Alnet
85b2fc499SJeff Garzik  *     (C) 2001 Brad Hards
95b2fc499SJeff Garzik  *     (C) 2002 Oliver Neukum
105b2fc499SJeff Garzik  *
115b2fc499SJeff Garzik  *     Original author: The Zapman <zapman@interlan.net>
125b2fc499SJeff Garzik  *     Inspired by, and much credit goes to Michael Rothwell
135b2fc499SJeff Garzik  *     <rothwell@interlan.net> for the test equipment, help, and patience
145b2fc499SJeff Garzik  *     Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
155b2fc499SJeff Garzik  *     Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
165b2fc499SJeff Garzik  *     for providing the firmware and driver resources.
175b2fc499SJeff Garzik  *
185b2fc499SJeff Garzik  ****************************************************************/
195b2fc499SJeff Garzik 
205b2fc499SJeff Garzik /* TODO:
215b2fc499SJeff Garzik  * Develop test procedures for USB net interfaces
225b2fc499SJeff Garzik  * Run test procedures
235b2fc499SJeff Garzik  * Fix bugs from previous two steps
245b2fc499SJeff Garzik  * Snoop other OSs for any tricks we're not doing
255b2fc499SJeff Garzik  * Reduce arbitrary timeouts
265b2fc499SJeff Garzik  * Smart multicast support
275b2fc499SJeff Garzik  * Temporary MAC change support
285b2fc499SJeff Garzik  * Tunable SOFs parameter - ioctl()?
295b2fc499SJeff Garzik  * Ethernet stats collection
305b2fc499SJeff Garzik  * Code formatting improvements
315b2fc499SJeff Garzik  */
325b2fc499SJeff Garzik 
335b2fc499SJeff Garzik #include <linux/module.h>
345b2fc499SJeff Garzik #include <linux/slab.h>
355b2fc499SJeff Garzik #include <linux/string.h>
365b2fc499SJeff Garzik #include <linux/delay.h>
375b2fc499SJeff Garzik #include <linux/netdevice.h>
385b2fc499SJeff Garzik #include <linux/etherdevice.h>
395b2fc499SJeff Garzik #include <linux/usb.h>
405b2fc499SJeff Garzik #include <linux/types.h>
415b2fc499SJeff Garzik #include <linux/ethtool.h>
425b2fc499SJeff Garzik #include <linux/dma-mapping.h>
435b2fc499SJeff Garzik #include <linux/wait.h>
4479682499SDavid Woodhouse #include <linux/firmware.h>
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
465b2fc499SJeff Garzik #include <asm/byteorder.h>
475b2fc499SJeff Garzik 
485b2fc499SJeff Garzik #undef DEBUG
495b2fc499SJeff Garzik 
505b2fc499SJeff Garzik #define KAWETH_MTU			1514
515b2fc499SJeff Garzik #define KAWETH_BUF_SIZE			1664
525b2fc499SJeff Garzik #define KAWETH_TX_TIMEOUT		(5 * HZ)
535b2fc499SJeff Garzik #define KAWETH_SCRATCH_SIZE		32
545b2fc499SJeff Garzik #define KAWETH_FIRMWARE_BUF_SIZE	4096
552b2b2e35SRuss Dill #define KAWETH_CONTROL_TIMEOUT		(30000)
565b2fc499SJeff Garzik 
575b2fc499SJeff Garzik #define KAWETH_STATUS_BROKEN		0x0000001
585b2fc499SJeff Garzik #define KAWETH_STATUS_CLOSING		0x0000002
595b2fc499SJeff Garzik #define KAWETH_STATUS_SUSPENDING	0x0000004
605b2fc499SJeff Garzik 
615b2fc499SJeff Garzik #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
625b2fc499SJeff Garzik 
635b2fc499SJeff Garzik #define KAWETH_PACKET_FILTER_PROMISCUOUS	0x01
645b2fc499SJeff Garzik #define KAWETH_PACKET_FILTER_ALL_MULTICAST	0x02
655b2fc499SJeff Garzik #define KAWETH_PACKET_FILTER_DIRECTED		0x04
665b2fc499SJeff Garzik #define KAWETH_PACKET_FILTER_BROADCAST		0x08
675b2fc499SJeff Garzik #define KAWETH_PACKET_FILTER_MULTICAST		0x10
685b2fc499SJeff Garzik 
695b2fc499SJeff Garzik /* Table 7 */
705b2fc499SJeff Garzik #define KAWETH_COMMAND_GET_ETHERNET_DESC	0x00
715b2fc499SJeff Garzik #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
725b2fc499SJeff Garzik #define KAWETH_COMMAND_SET_PACKET_FILTER	0x02
735b2fc499SJeff Garzik #define KAWETH_COMMAND_STATISTICS               0x03
745b2fc499SJeff Garzik #define KAWETH_COMMAND_SET_TEMP_MAC     	0x06
755b2fc499SJeff Garzik #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
765b2fc499SJeff Garzik #define KAWETH_COMMAND_SET_URB_SIZE		0x08
775b2fc499SJeff Garzik #define KAWETH_COMMAND_SET_SOFS_WAIT		0x09
785b2fc499SJeff Garzik #define KAWETH_COMMAND_SCAN			0xFF
795b2fc499SJeff Garzik 
805b2fc499SJeff Garzik #define KAWETH_SOFS_TO_WAIT			0x05
815b2fc499SJeff Garzik 
825b2fc499SJeff Garzik #define INTBUFFERSIZE				4
835b2fc499SJeff Garzik 
845b2fc499SJeff Garzik #define STATE_OFFSET				0
855b2fc499SJeff Garzik #define STATE_MASK				0x40
865b2fc499SJeff Garzik #define	STATE_SHIFT				5
875b2fc499SJeff Garzik 
885b2fc499SJeff Garzik #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
895b2fc499SJeff Garzik 
905b2fc499SJeff Garzik 
915b2fc499SJeff Garzik MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
925b2fc499SJeff Garzik MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
935b2fc499SJeff Garzik MODULE_LICENSE("GPL");
9479682499SDavid Woodhouse MODULE_FIRMWARE("kaweth/new_code.bin");
9579682499SDavid Woodhouse MODULE_FIRMWARE("kaweth/new_code_fix.bin");
9679682499SDavid Woodhouse MODULE_FIRMWARE("kaweth/trigger_code.bin");
9779682499SDavid Woodhouse MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
985b2fc499SJeff Garzik 
995b2fc499SJeff Garzik static const char driver_name[] = "kaweth";
1005b2fc499SJeff Garzik 
1015b2fc499SJeff Garzik static int kaweth_probe(
1025b2fc499SJeff Garzik 		struct usb_interface *intf,
1035b2fc499SJeff Garzik 		const struct usb_device_id *id	/* from id_table */
1045b2fc499SJeff Garzik 	);
1055b2fc499SJeff Garzik static void kaweth_disconnect(struct usb_interface *intf);
1065b2fc499SJeff Garzik static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1075b2fc499SJeff Garzik 				       unsigned int pipe,
1085b2fc499SJeff Garzik 				       struct usb_ctrlrequest *cmd, void *data,
1095b2fc499SJeff Garzik 				       int len, int timeout);
1105b2fc499SJeff Garzik static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
1115b2fc499SJeff Garzik static int kaweth_resume(struct usb_interface *intf);
1125b2fc499SJeff Garzik 
1135b2fc499SJeff Garzik /****************************************************************
1145b2fc499SJeff Garzik  *     usb_device_id
1155b2fc499SJeff Garzik  ****************************************************************/
11606fa59a0SArvind Yadav static const struct usb_device_id usb_klsi_table[] = {
1175b2fc499SJeff Garzik 	{ USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
1185b2fc499SJeff Garzik 	{ USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
1195b2fc499SJeff Garzik 	{ USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
1205b2fc499SJeff Garzik 	{ USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
1215b2fc499SJeff Garzik 	{ USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
1225b2fc499SJeff Garzik 	{ USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
1235b2fc499SJeff Garzik 	{ USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
1245b2fc499SJeff Garzik 	{ USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
1255b2fc499SJeff Garzik 	{ USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
1265b2fc499SJeff Garzik 	{ USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
1275b2fc499SJeff Garzik 	{ USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
1285b2fc499SJeff Garzik 	{ USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
1295b2fc499SJeff Garzik 	{ USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
1305b2fc499SJeff Garzik 	{ USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
1315b2fc499SJeff Garzik 	{ USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
1325b2fc499SJeff Garzik 	{ USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
1335b2fc499SJeff Garzik 	{ USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
134dacf4fc8SAndreas Hartmann 	{ USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
1355b2fc499SJeff Garzik 	{ USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
1365b2fc499SJeff Garzik 	{ USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
1375b2fc499SJeff Garzik 	{ USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
1385b2fc499SJeff Garzik 	{ USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
1395b2fc499SJeff Garzik 	{ USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
1405b2fc499SJeff Garzik 	{ USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
1415b2fc499SJeff Garzik 	{ USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
1425b2fc499SJeff Garzik 	{ USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
1435b2fc499SJeff Garzik 	{ USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
1445b2fc499SJeff Garzik 	{ USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
1455b2fc499SJeff Garzik 	{ USB_DEVICE(0x1485, 0x0001) },	/* Silicom U2E */
1465b2fc499SJeff Garzik 	{ USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
1475b2fc499SJeff Garzik 	{ USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
1485b2fc499SJeff Garzik 	{ USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
1495b2fc499SJeff Garzik 	{ USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
1505b2fc499SJeff Garzik 	{ USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
1515b2fc499SJeff Garzik 	{ USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
1525b2fc499SJeff Garzik 	{} /* Null terminator */
1535b2fc499SJeff Garzik };
1545b2fc499SJeff Garzik 
1555b2fc499SJeff Garzik MODULE_DEVICE_TABLE (usb, usb_klsi_table);
1565b2fc499SJeff Garzik 
1575b2fc499SJeff Garzik /****************************************************************
1585b2fc499SJeff Garzik  *     kaweth_driver
1595b2fc499SJeff Garzik  ****************************************************************/
1605b2fc499SJeff Garzik static struct usb_driver kaweth_driver = {
1615b2fc499SJeff Garzik 	.name =		driver_name,
1625b2fc499SJeff Garzik 	.probe =	kaweth_probe,
1635b2fc499SJeff Garzik 	.disconnect =	kaweth_disconnect,
1645b2fc499SJeff Garzik 	.suspend =	kaweth_suspend,
1655b2fc499SJeff Garzik 	.resume =	kaweth_resume,
1665b2fc499SJeff Garzik 	.id_table =     usb_klsi_table,
1675b2fc499SJeff Garzik 	.supports_autosuspend =	1,
168e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
1695b2fc499SJeff Garzik };
1705b2fc499SJeff Garzik 
1715b2fc499SJeff Garzik typedef __u8 eth_addr_t[6];
1725b2fc499SJeff Garzik 
1735b2fc499SJeff Garzik /****************************************************************
1745b2fc499SJeff Garzik  *     usb_eth_dev
1755b2fc499SJeff Garzik  ****************************************************************/
1765b2fc499SJeff Garzik struct usb_eth_dev {
1775b2fc499SJeff Garzik 	char *name;
1785b2fc499SJeff Garzik 	__u16 vendor;
1795b2fc499SJeff Garzik 	__u16 device;
1805b2fc499SJeff Garzik 	void *pdata;
1815b2fc499SJeff Garzik };
1825b2fc499SJeff Garzik 
1835b2fc499SJeff Garzik /****************************************************************
1845b2fc499SJeff Garzik  *     kaweth_ethernet_configuration
1855b2fc499SJeff Garzik  *     Refer Table 8
1865b2fc499SJeff Garzik  ****************************************************************/
1875b2fc499SJeff Garzik struct kaweth_ethernet_configuration
1885b2fc499SJeff Garzik {
1895b2fc499SJeff Garzik 	__u8 size;
1905b2fc499SJeff Garzik 	__u8 reserved1;
1915b2fc499SJeff Garzik 	__u8 reserved2;
1925b2fc499SJeff Garzik 	eth_addr_t hw_addr;
1935b2fc499SJeff Garzik 	__u32 statistics_mask;
1945b2fc499SJeff Garzik 	__le16 segment_size;
1955b2fc499SJeff Garzik 	__u16 max_multicast_filters;
1965b2fc499SJeff Garzik 	__u8 reserved3;
197ba2d3587SEric Dumazet } __packed;
1985b2fc499SJeff Garzik 
1995b2fc499SJeff Garzik /****************************************************************
2005b2fc499SJeff Garzik  *     kaweth_device
2015b2fc499SJeff Garzik  ****************************************************************/
2025b2fc499SJeff Garzik struct kaweth_device
2035b2fc499SJeff Garzik {
2045b2fc499SJeff Garzik 	spinlock_t device_lock;
2055b2fc499SJeff Garzik 
2065b2fc499SJeff Garzik 	__u32 status;
2075b2fc499SJeff Garzik 	int end;
2085b2fc499SJeff Garzik 	int suspend_lowmem_rx;
2095b2fc499SJeff Garzik 	int suspend_lowmem_ctrl;
2105b2fc499SJeff Garzik 	int linkstate;
2115b2fc499SJeff Garzik 	int opened;
2125b2fc499SJeff Garzik 	struct delayed_work lowmem_work;
2135b2fc499SJeff Garzik 
2145b2fc499SJeff Garzik 	struct usb_device *dev;
2155b2fc499SJeff Garzik 	struct usb_interface *intf;
2165b2fc499SJeff Garzik 	struct net_device *net;
2175b2fc499SJeff Garzik 	wait_queue_head_t term_wait;
2185b2fc499SJeff Garzik 
2195b2fc499SJeff Garzik 	struct urb *rx_urb;
2205b2fc499SJeff Garzik 	struct urb *tx_urb;
2215b2fc499SJeff Garzik 	struct urb *irq_urb;
2225b2fc499SJeff Garzik 
2235b2fc499SJeff Garzik 	dma_addr_t intbufferhandle;
2245b2fc499SJeff Garzik 	__u8 *intbuffer;
2255b2fc499SJeff Garzik 	dma_addr_t rxbufferhandle;
2265b2fc499SJeff Garzik 	__u8 *rx_buf;
2275b2fc499SJeff Garzik 
2285b2fc499SJeff Garzik 
2295b2fc499SJeff Garzik 	struct sk_buff *tx_skb;
2305b2fc499SJeff Garzik 
2315b2fc499SJeff Garzik 	__u8 *firmware_buf;
2325b2fc499SJeff Garzik 	__u8 scratch[KAWETH_SCRATCH_SIZE];
2335b2fc499SJeff Garzik 	__u16 packet_filter_bitmap;
2345b2fc499SJeff Garzik 
2355b2fc499SJeff Garzik 	struct kaweth_ethernet_configuration configuration;
2365b2fc499SJeff Garzik };
2375b2fc499SJeff Garzik 
2385b2fc499SJeff Garzik /****************************************************************
2395b2fc499SJeff Garzik  *     kaweth_control
2405b2fc499SJeff Garzik  ****************************************************************/
2415b2fc499SJeff Garzik static int kaweth_control(struct kaweth_device *kaweth,
2425b2fc499SJeff Garzik 			  unsigned int pipe,
2435b2fc499SJeff Garzik 			  __u8 request,
2445b2fc499SJeff Garzik 			  __u8 requesttype,
2455b2fc499SJeff Garzik 			  __u16 value,
2465b2fc499SJeff Garzik 			  __u16 index,
2475b2fc499SJeff Garzik 			  void *data,
2485b2fc499SJeff Garzik 			  __u16 size,
2495b2fc499SJeff Garzik 			  int timeout)
2505b2fc499SJeff Garzik {
2515b2fc499SJeff Garzik 	struct usb_ctrlrequest *dr;
252051b982bSKevin Cernekee 	int retval;
2535b2fc499SJeff Garzik 
2545b2fc499SJeff Garzik 	if(in_interrupt()) {
25549ae25b0SGreg Kroah-Hartman 		netdev_dbg(kaweth->net, "in_interrupt()\n");
2565b2fc499SJeff Garzik 		return -EBUSY;
2575b2fc499SJeff Garzik 	}
2585b2fc499SJeff Garzik 
2595b2fc499SJeff Garzik 	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
26049ae25b0SGreg Kroah-Hartman 	if (!dr)
2615b2fc499SJeff Garzik 		return -ENOMEM;
2625b2fc499SJeff Garzik 
2635b2fc499SJeff Garzik 	dr->bRequestType = requesttype;
2645b2fc499SJeff Garzik 	dr->bRequest = request;
265da2bbdccSHarvey Harrison 	dr->wValue = cpu_to_le16(value);
266da2bbdccSHarvey Harrison 	dr->wIndex = cpu_to_le16(index);
267da2bbdccSHarvey Harrison 	dr->wLength = cpu_to_le16(size);
2685b2fc499SJeff Garzik 
269051b982bSKevin Cernekee 	retval = kaweth_internal_control_msg(kaweth->dev,
2705b2fc499SJeff Garzik 					     pipe,
2715b2fc499SJeff Garzik 					     dr,
2725b2fc499SJeff Garzik 					     data,
2735b2fc499SJeff Garzik 					     size,
2745b2fc499SJeff Garzik 					     timeout);
275051b982bSKevin Cernekee 
276051b982bSKevin Cernekee 	kfree(dr);
277051b982bSKevin Cernekee 	return retval;
2785b2fc499SJeff Garzik }
2795b2fc499SJeff Garzik 
2805b2fc499SJeff Garzik /****************************************************************
2815b2fc499SJeff Garzik  *     kaweth_read_configuration
2825b2fc499SJeff Garzik  ****************************************************************/
2835b2fc499SJeff Garzik static int kaweth_read_configuration(struct kaweth_device *kaweth)
2845b2fc499SJeff Garzik {
285*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_rcvctrlpipe(kaweth->dev, 0),
2865b2fc499SJeff Garzik 				KAWETH_COMMAND_GET_ETHERNET_DESC,
2875b2fc499SJeff Garzik 				USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
288*af3563beSSebastian Andrzej Siewior 				0, 0,
289*af3563beSSebastian Andrzej Siewior 				&kaweth->configuration,
2905b2fc499SJeff Garzik 				sizeof(kaweth->configuration),
2915b2fc499SJeff Garzik 				KAWETH_CONTROL_TIMEOUT);
2925b2fc499SJeff Garzik }
2935b2fc499SJeff Garzik 
2945b2fc499SJeff Garzik /****************************************************************
2955b2fc499SJeff Garzik  *     kaweth_set_urb_size
2965b2fc499SJeff Garzik  ****************************************************************/
2975b2fc499SJeff Garzik static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
2985b2fc499SJeff Garzik {
29949ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
3005b2fc499SJeff Garzik 
301*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3025b2fc499SJeff Garzik 			       KAWETH_COMMAND_SET_URB_SIZE,
3035b2fc499SJeff Garzik 			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
304*af3563beSSebastian Andrzej Siewior 			       urb_size, 0,
305*af3563beSSebastian Andrzej Siewior 			       &kaweth->scratch, 0,
3065b2fc499SJeff Garzik 			       KAWETH_CONTROL_TIMEOUT);
3075b2fc499SJeff Garzik }
3085b2fc499SJeff Garzik 
3095b2fc499SJeff Garzik /****************************************************************
3105b2fc499SJeff Garzik  *     kaweth_set_sofs_wait
3115b2fc499SJeff Garzik  ****************************************************************/
3125b2fc499SJeff Garzik static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
3135b2fc499SJeff Garzik {
31449ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
3155b2fc499SJeff Garzik 
316*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3175b2fc499SJeff Garzik 			       KAWETH_COMMAND_SET_SOFS_WAIT,
3185b2fc499SJeff Garzik 			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
319*af3563beSSebastian Andrzej Siewior 			       sofs_wait, 0,
320*af3563beSSebastian Andrzej Siewior 			       &kaweth->scratch, 0,
3215b2fc499SJeff Garzik 			       KAWETH_CONTROL_TIMEOUT);
3225b2fc499SJeff Garzik }
3235b2fc499SJeff Garzik 
3245b2fc499SJeff Garzik /****************************************************************
3255b2fc499SJeff Garzik  *     kaweth_set_receive_filter
3265b2fc499SJeff Garzik  ****************************************************************/
3275b2fc499SJeff Garzik static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
3285b2fc499SJeff Garzik 				     __u16 receive_filter)
3295b2fc499SJeff Garzik {
33049ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "Set receive filter to %d\n",
33149ae25b0SGreg Kroah-Hartman 		   (unsigned)receive_filter);
3325b2fc499SJeff Garzik 
333*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3345b2fc499SJeff Garzik 			       KAWETH_COMMAND_SET_PACKET_FILTER,
3355b2fc499SJeff Garzik 			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
336*af3563beSSebastian Andrzej Siewior 			       receive_filter, 0,
337*af3563beSSebastian Andrzej Siewior 			       &kaweth->scratch, 0,
3385b2fc499SJeff Garzik 			       KAWETH_CONTROL_TIMEOUT);
3395b2fc499SJeff Garzik }
3405b2fc499SJeff Garzik 
3415b2fc499SJeff Garzik /****************************************************************
3425b2fc499SJeff Garzik  *     kaweth_download_firmware
3435b2fc499SJeff Garzik  ****************************************************************/
3445b2fc499SJeff Garzik static int kaweth_download_firmware(struct kaweth_device *kaweth,
34579682499SDavid Woodhouse 				    const char *fwname,
3465b2fc499SJeff Garzik 				    __u8 interrupt,
3475b2fc499SJeff Garzik 				    __u8 type)
3485b2fc499SJeff Garzik {
34979682499SDavid Woodhouse 	const struct firmware *fw;
35079682499SDavid Woodhouse 	int data_len;
35179682499SDavid Woodhouse 	int ret;
35279682499SDavid Woodhouse 
35379682499SDavid Woodhouse 	ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
35479682499SDavid Woodhouse 	if (ret) {
355db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev, "Firmware request failed\n");
35679682499SDavid Woodhouse 		return ret;
3575b2fc499SJeff Garzik 	}
3585b2fc499SJeff Garzik 
35979682499SDavid Woodhouse 	if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
360db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
361db6b2219SGreg Kroah-Hartman 			fw->size);
362986e3f6eSJesper Juhl 		release_firmware(fw);
36379682499SDavid Woodhouse 		return -ENOSPC;
36479682499SDavid Woodhouse 	}
36579682499SDavid Woodhouse 	data_len = fw->size;
36679682499SDavid Woodhouse 	memcpy(kaweth->firmware_buf, fw->data, fw->size);
36779682499SDavid Woodhouse 
36879682499SDavid Woodhouse 	release_firmware(fw);
3695b2fc499SJeff Garzik 
3705b2fc499SJeff Garzik 	kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
3715b2fc499SJeff Garzik 	kaweth->firmware_buf[3] = data_len >> 8;
3725b2fc499SJeff Garzik 	kaweth->firmware_buf[4] = type;
3735b2fc499SJeff Garzik 	kaweth->firmware_buf[5] = interrupt;
3745b2fc499SJeff Garzik 
37549ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
3765b2fc499SJeff Garzik 		   kaweth->firmware_buf[2]);
3775b2fc499SJeff Garzik 
37849ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net,
37949ae25b0SGreg Kroah-Hartman 		   "Downloading firmware at %p to kaweth device at %p\n",
3800abc1ceeSAlan Cox 		   kaweth->firmware_buf, kaweth);
38149ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
3825b2fc499SJeff Garzik 
383*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3845b2fc499SJeff Garzik 			      KAWETH_COMMAND_SCAN,
3855b2fc499SJeff Garzik 			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
386*af3563beSSebastian Andrzej Siewior 			      0, 0,
387*af3563beSSebastian Andrzej Siewior 			      kaweth->firmware_buf, data_len,
3885b2fc499SJeff Garzik 			      KAWETH_CONTROL_TIMEOUT);
3895b2fc499SJeff Garzik }
3905b2fc499SJeff Garzik 
3915b2fc499SJeff Garzik /****************************************************************
3925b2fc499SJeff Garzik  *     kaweth_trigger_firmware
3935b2fc499SJeff Garzik  ****************************************************************/
3945b2fc499SJeff Garzik static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
3955b2fc499SJeff Garzik 				   __u8 interrupt)
3965b2fc499SJeff Garzik {
3975b2fc499SJeff Garzik 	kaweth->firmware_buf[0] = 0xB6;
3985b2fc499SJeff Garzik 	kaweth->firmware_buf[1] = 0xC3;
3995b2fc499SJeff Garzik 	kaweth->firmware_buf[2] = 0x01;
4005b2fc499SJeff Garzik 	kaweth->firmware_buf[3] = 0x00;
4015b2fc499SJeff Garzik 	kaweth->firmware_buf[4] = 0x06;
4025b2fc499SJeff Garzik 	kaweth->firmware_buf[5] = interrupt;
4035b2fc499SJeff Garzik 	kaweth->firmware_buf[6] = 0x00;
4045b2fc499SJeff Garzik 	kaweth->firmware_buf[7] = 0x00;
4055b2fc499SJeff Garzik 
406*af3563beSSebastian Andrzej Siewior 	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
4075b2fc499SJeff Garzik 			       KAWETH_COMMAND_SCAN,
4085b2fc499SJeff Garzik 			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
409*af3563beSSebastian Andrzej Siewior 			       0, 0,
410*af3563beSSebastian Andrzej Siewior 			       (void *)kaweth->firmware_buf, 8,
4115b2fc499SJeff Garzik 			       KAWETH_CONTROL_TIMEOUT);
4125b2fc499SJeff Garzik }
4135b2fc499SJeff Garzik 
4145b2fc499SJeff Garzik /****************************************************************
4155b2fc499SJeff Garzik  *     kaweth_reset
4165b2fc499SJeff Garzik  ****************************************************************/
4175b2fc499SJeff Garzik static int kaweth_reset(struct kaweth_device *kaweth)
4185b2fc499SJeff Garzik {
4195b2fc499SJeff Garzik 	int result;
4205b2fc499SJeff Garzik 
421f0816ce3SOliver Neukum 	result = usb_reset_configuration(kaweth->dev);
4225b2fc499SJeff Garzik 	mdelay(10);
4235b2fc499SJeff Garzik 
42449ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
4255b2fc499SJeff Garzik 
4265b2fc499SJeff Garzik 	return result;
4275b2fc499SJeff Garzik }
4285b2fc499SJeff Garzik 
4295b2fc499SJeff Garzik static void kaweth_usb_receive(struct urb *);
4305b2fc499SJeff Garzik static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
4315b2fc499SJeff Garzik 
4325b2fc499SJeff Garzik /****************************************************************
4335b2fc499SJeff Garzik 	int_callback
4345b2fc499SJeff Garzik *****************************************************************/
4355b2fc499SJeff Garzik 
4365b2fc499SJeff Garzik static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
4375b2fc499SJeff Garzik {
4385b2fc499SJeff Garzik 	int status;
4395b2fc499SJeff Garzik 
4405b2fc499SJeff Garzik 	status = usb_submit_urb (kaweth->irq_urb, mf);
4415b2fc499SJeff Garzik 	if (unlikely(status == -ENOMEM)) {
4425b2fc499SJeff Garzik 		kaweth->suspend_lowmem_ctrl = 1;
4435b2fc499SJeff Garzik 		schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
4445b2fc499SJeff Garzik 	} else {
4455b2fc499SJeff Garzik 		kaweth->suspend_lowmem_ctrl = 0;
4465b2fc499SJeff Garzik 	}
4475b2fc499SJeff Garzik 
4485b2fc499SJeff Garzik 	if (status)
449db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev,
450db6b2219SGreg Kroah-Hartman 			"can't resubmit intr, %s-%s, status %d\n",
4515b2fc499SJeff Garzik 			kaweth->dev->bus->bus_name,
4525b2fc499SJeff Garzik 			kaweth->dev->devpath, status);
4535b2fc499SJeff Garzik }
4545b2fc499SJeff Garzik 
4555b2fc499SJeff Garzik static void int_callback(struct urb *u)
4565b2fc499SJeff Garzik {
4575b2fc499SJeff Garzik 	struct kaweth_device *kaweth = u->context;
4585b2fc499SJeff Garzik 	int act_state;
459c94cb314SOliver Neukum 	int status = u->status;
4605b2fc499SJeff Garzik 
461c94cb314SOliver Neukum 	switch (status) {
4625b2fc499SJeff Garzik 	case 0:			/* success */
4635b2fc499SJeff Garzik 		break;
4645b2fc499SJeff Garzik 	case -ECONNRESET:	/* unlink */
4655b2fc499SJeff Garzik 	case -ENOENT:
4665b2fc499SJeff Garzik 	case -ESHUTDOWN:
4675b2fc499SJeff Garzik 		return;
4685b2fc499SJeff Garzik 	/* -EPIPE:  should clear the halt */
4695b2fc499SJeff Garzik 	default:		/* error */
4705b2fc499SJeff Garzik 		goto resubmit;
4715b2fc499SJeff Garzik 	}
4725b2fc499SJeff Garzik 
4735b2fc499SJeff Garzik 	/* we check the link state to report changes */
4745b2fc499SJeff Garzik 	if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
4755b2fc499SJeff Garzik 		if (act_state)
4765b2fc499SJeff Garzik 			netif_carrier_on(kaweth->net);
4775b2fc499SJeff Garzik 		else
4785b2fc499SJeff Garzik 			netif_carrier_off(kaweth->net);
4795b2fc499SJeff Garzik 
4805b2fc499SJeff Garzik 		kaweth->linkstate = act_state;
4815b2fc499SJeff Garzik 	}
4825b2fc499SJeff Garzik resubmit:
4835b2fc499SJeff Garzik 	kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
4845b2fc499SJeff Garzik }
4855b2fc499SJeff Garzik 
4865b2fc499SJeff Garzik static void kaweth_resubmit_tl(struct work_struct *work)
4875b2fc499SJeff Garzik {
4885b2fc499SJeff Garzik 	struct kaweth_device *kaweth =
4895b2fc499SJeff Garzik 		container_of(work, struct kaweth_device, lowmem_work.work);
4905b2fc499SJeff Garzik 
4915b2fc499SJeff Garzik 	if (IS_BLOCKED(kaweth->status))
4925b2fc499SJeff Garzik 		return;
4935b2fc499SJeff Garzik 
4945b2fc499SJeff Garzik 	if (kaweth->suspend_lowmem_rx)
4955b2fc499SJeff Garzik 		kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
4965b2fc499SJeff Garzik 
4975b2fc499SJeff Garzik 	if (kaweth->suspend_lowmem_ctrl)
4985b2fc499SJeff Garzik 		kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
4995b2fc499SJeff Garzik }
5005b2fc499SJeff Garzik 
5015b2fc499SJeff Garzik 
5025b2fc499SJeff Garzik /****************************************************************
5035b2fc499SJeff Garzik  *     kaweth_resubmit_rx_urb
5045b2fc499SJeff Garzik  ****************************************************************/
5055b2fc499SJeff Garzik static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
5065b2fc499SJeff Garzik 						gfp_t mem_flags)
5075b2fc499SJeff Garzik {
5085b2fc499SJeff Garzik 	int result;
5095b2fc499SJeff Garzik 
5105b2fc499SJeff Garzik 	usb_fill_bulk_urb(kaweth->rx_urb,
5115b2fc499SJeff Garzik 		      kaweth->dev,
5125b2fc499SJeff Garzik 		      usb_rcvbulkpipe(kaweth->dev, 1),
5135b2fc499SJeff Garzik 		      kaweth->rx_buf,
5145b2fc499SJeff Garzik 		      KAWETH_BUF_SIZE,
5155b2fc499SJeff Garzik 		      kaweth_usb_receive,
5165b2fc499SJeff Garzik 		      kaweth);
5175b2fc499SJeff Garzik 	kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
5185b2fc499SJeff Garzik 	kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
5195b2fc499SJeff Garzik 
5205b2fc499SJeff Garzik 	if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
5215b2fc499SJeff Garzik 		if (result == -ENOMEM) {
5225b2fc499SJeff Garzik 			kaweth->suspend_lowmem_rx = 1;
5235b2fc499SJeff Garzik 			schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
5245b2fc499SJeff Garzik 		}
525db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
526db6b2219SGreg Kroah-Hartman 			result);
5275b2fc499SJeff Garzik 	} else {
5285b2fc499SJeff Garzik 		kaweth->suspend_lowmem_rx = 0;
5295b2fc499SJeff Garzik 	}
5305b2fc499SJeff Garzik 
5315b2fc499SJeff Garzik 	return result;
5325b2fc499SJeff Garzik }
5335b2fc499SJeff Garzik 
5345b2fc499SJeff Garzik static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
5355b2fc499SJeff Garzik 
5365b2fc499SJeff Garzik /****************************************************************
5375b2fc499SJeff Garzik  *     kaweth_usb_receive
5385b2fc499SJeff Garzik  ****************************************************************/
5395b2fc499SJeff Garzik static void kaweth_usb_receive(struct urb *urb)
5405b2fc499SJeff Garzik {
54149ae25b0SGreg Kroah-Hartman 	struct device *dev = &urb->dev->dev;
5425b2fc499SJeff Garzik 	struct kaweth_device *kaweth = urb->context;
5435b2fc499SJeff Garzik 	struct net_device *net = kaweth->net;
544c94cb314SOliver Neukum 	int status = urb->status;
545ba49fed8SSebastian Andrzej Siewior 	unsigned long flags;
5465b2fc499SJeff Garzik 	int count = urb->actual_length;
5475b2fc499SJeff Garzik 	int count2 = urb->transfer_buffer_length;
5485b2fc499SJeff Garzik 
5495b2fc499SJeff Garzik 	__u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
5505b2fc499SJeff Garzik 
5515b2fc499SJeff Garzik 	struct sk_buff *skb;
5525b2fc499SJeff Garzik 
553710b523aSLarry Finger 	if (unlikely(status == -EPIPE)) {
5546ffa770eSTobias Klauser 		net->stats.rx_errors++;
5555b2fc499SJeff Garzik 		kaweth->end = 1;
5565b2fc499SJeff Garzik 		wake_up(&kaweth->term_wait);
55749ae25b0SGreg Kroah-Hartman 		dev_dbg(dev, "Status was -EPIPE.\n");
5585b2fc499SJeff Garzik 		return;
5595b2fc499SJeff Garzik 	}
560710b523aSLarry Finger 	if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
561710b523aSLarry Finger 		/* we are killed - set a flag and wake the disconnect handler */
562710b523aSLarry Finger 		kaweth->end = 1;
563710b523aSLarry Finger 		wake_up(&kaweth->term_wait);
56449ae25b0SGreg Kroah-Hartman 		dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
565710b523aSLarry Finger 		return;
566710b523aSLarry Finger 	}
567710b523aSLarry Finger 	if (unlikely(status == -EPROTO || status == -ETIME ||
568710b523aSLarry Finger 		     status == -EILSEQ)) {
5696ffa770eSTobias Klauser 		net->stats.rx_errors++;
57049ae25b0SGreg Kroah-Hartman 		dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
571710b523aSLarry Finger 		return;
572710b523aSLarry Finger 	}
573710b523aSLarry Finger 	if (unlikely(status == -EOVERFLOW)) {
5746ffa770eSTobias Klauser 		net->stats.rx_errors++;
57549ae25b0SGreg Kroah-Hartman 		dev_dbg(dev, "Status was -EOVERFLOW.\n");
576710b523aSLarry Finger 	}
577ba49fed8SSebastian Andrzej Siewior 	spin_lock_irqsave(&kaweth->device_lock, flags);
5785b2fc499SJeff Garzik 	if (IS_BLOCKED(kaweth->status)) {
579ba49fed8SSebastian Andrzej Siewior 		spin_unlock_irqrestore(&kaweth->device_lock, flags);
5805b2fc499SJeff Garzik 		return;
5815b2fc499SJeff Garzik 	}
582ba49fed8SSebastian Andrzej Siewior 	spin_unlock_irqrestore(&kaweth->device_lock, flags);
5835b2fc499SJeff Garzik 
584c94cb314SOliver Neukum 	if(status && status != -EREMOTEIO && count != 1) {
585db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev,
586db6b2219SGreg Kroah-Hartman 			"%s RX status: %d count: %d packet_len: %d\n",
587db6b2219SGreg Kroah-Hartman 			net->name, status, count, (int)pkt_len);
5885b2fc499SJeff Garzik 		kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
5895b2fc499SJeff Garzik                 return;
5905b2fc499SJeff Garzik 	}
5915b2fc499SJeff Garzik 
5925b2fc499SJeff Garzik 	if(kaweth->net && (count > 2)) {
5935b2fc499SJeff Garzik 		if(pkt_len > (count - 2)) {
594db6b2219SGreg Kroah-Hartman 			dev_err(&kaweth->intf->dev,
595db6b2219SGreg Kroah-Hartman 				"Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
596db6b2219SGreg Kroah-Hartman 				pkt_len, count);
597db6b2219SGreg Kroah-Hartman 			dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
598db6b2219SGreg Kroah-Hartman 				pkt_len & 2047);
599db6b2219SGreg Kroah-Hartman 			dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
6005b2fc499SJeff Garzik 		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
6015b2fc499SJeff Garzik                         return;
6025b2fc499SJeff Garzik                 }
6035b2fc499SJeff Garzik 
6045b2fc499SJeff Garzik 		if(!(skb = dev_alloc_skb(pkt_len+2))) {
6055b2fc499SJeff Garzik 		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
6065b2fc499SJeff Garzik                         return;
6075b2fc499SJeff Garzik 		}
6085b2fc499SJeff Garzik 
6095b2fc499SJeff Garzik 		skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
6105b2fc499SJeff Garzik 
6118c7b7faaSDavid S. Miller 		skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
6125b2fc499SJeff Garzik 
6135b2fc499SJeff Garzik 		skb_put(skb, pkt_len);
6145b2fc499SJeff Garzik 
6155b2fc499SJeff Garzik 		skb->protocol = eth_type_trans(skb, net);
6165b2fc499SJeff Garzik 
6175b2fc499SJeff Garzik 		netif_rx(skb);
6185b2fc499SJeff Garzik 
6196ffa770eSTobias Klauser 		net->stats.rx_packets++;
6206ffa770eSTobias Klauser 		net->stats.rx_bytes += pkt_len;
6215b2fc499SJeff Garzik 	}
6225b2fc499SJeff Garzik 
6235b2fc499SJeff Garzik 	kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
6245b2fc499SJeff Garzik }
6255b2fc499SJeff Garzik 
6265b2fc499SJeff Garzik /****************************************************************
6275b2fc499SJeff Garzik  *     kaweth_open
6285b2fc499SJeff Garzik  ****************************************************************/
6295b2fc499SJeff Garzik static int kaweth_open(struct net_device *net)
6305b2fc499SJeff Garzik {
6315b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(net);
6325b2fc499SJeff Garzik 	int res;
6335b2fc499SJeff Garzik 
6345b2fc499SJeff Garzik 	res = usb_autopm_get_interface(kaweth->intf);
6355b2fc499SJeff Garzik 	if (res) {
636db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
6375b2fc499SJeff Garzik 		return -EIO;
6385b2fc499SJeff Garzik 	}
6395b2fc499SJeff Garzik 	res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
6405b2fc499SJeff Garzik 	if (res)
6415b2fc499SJeff Garzik 		goto err_out;
6425b2fc499SJeff Garzik 
6435b2fc499SJeff Garzik 	usb_fill_int_urb(
6445b2fc499SJeff Garzik 		kaweth->irq_urb,
6455b2fc499SJeff Garzik 		kaweth->dev,
6465b2fc499SJeff Garzik 		usb_rcvintpipe(kaweth->dev, 3),
6475b2fc499SJeff Garzik 		kaweth->intbuffer,
6485b2fc499SJeff Garzik 		INTBUFFERSIZE,
6495b2fc499SJeff Garzik 		int_callback,
6505b2fc499SJeff Garzik 		kaweth,
6515b2fc499SJeff Garzik 		250); /* overriding the descriptor */
6525b2fc499SJeff Garzik 	kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
6535b2fc499SJeff Garzik 	kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
6545b2fc499SJeff Garzik 
6555b2fc499SJeff Garzik 	res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
6565b2fc499SJeff Garzik 	if (res) {
6575b2fc499SJeff Garzik 		usb_kill_urb(kaweth->rx_urb);
6585b2fc499SJeff Garzik 		goto err_out;
6595b2fc499SJeff Garzik 	}
6605b2fc499SJeff Garzik 	kaweth->opened = 1;
6615b2fc499SJeff Garzik 
6625b2fc499SJeff Garzik 	netif_start_queue(net);
6635b2fc499SJeff Garzik 
6645b2fc499SJeff Garzik 	kaweth_async_set_rx_mode(kaweth);
6655b2fc499SJeff Garzik 	return 0;
6665b2fc499SJeff Garzik 
6675b2fc499SJeff Garzik err_out:
668ceb0c77eSOliver Neukum 	usb_autopm_put_interface(kaweth->intf);
6695b2fc499SJeff Garzik 	return -EIO;
6705b2fc499SJeff Garzik }
6715b2fc499SJeff Garzik 
6725b2fc499SJeff Garzik /****************************************************************
6735b2fc499SJeff Garzik  *     kaweth_kill_urbs
6745b2fc499SJeff Garzik  ****************************************************************/
6755b2fc499SJeff Garzik static void kaweth_kill_urbs(struct kaweth_device *kaweth)
6765b2fc499SJeff Garzik {
6775b2fc499SJeff Garzik 	usb_kill_urb(kaweth->irq_urb);
6785b2fc499SJeff Garzik 	usb_kill_urb(kaweth->rx_urb);
6795b2fc499SJeff Garzik 	usb_kill_urb(kaweth->tx_urb);
6805b2fc499SJeff Garzik 
6814bb073c0SDavid S. Miller 	cancel_delayed_work_sync(&kaweth->lowmem_work);
6825b2fc499SJeff Garzik 
6835b2fc499SJeff Garzik 	/* a scheduled work may have resubmitted,
6845b2fc499SJeff Garzik 	   we hit them again */
6855b2fc499SJeff Garzik 	usb_kill_urb(kaweth->irq_urb);
6865b2fc499SJeff Garzik 	usb_kill_urb(kaweth->rx_urb);
6875b2fc499SJeff Garzik }
6885b2fc499SJeff Garzik 
6895b2fc499SJeff Garzik /****************************************************************
6905b2fc499SJeff Garzik  *     kaweth_close
6915b2fc499SJeff Garzik  ****************************************************************/
6925b2fc499SJeff Garzik static int kaweth_close(struct net_device *net)
6935b2fc499SJeff Garzik {
6945b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(net);
6955b2fc499SJeff Garzik 
6965b2fc499SJeff Garzik 	netif_stop_queue(net);
6975b2fc499SJeff Garzik 	kaweth->opened = 0;
6985b2fc499SJeff Garzik 
6995b2fc499SJeff Garzik 	kaweth->status |= KAWETH_STATUS_CLOSING;
7005b2fc499SJeff Garzik 
7015b2fc499SJeff Garzik 	kaweth_kill_urbs(kaweth);
7025b2fc499SJeff Garzik 
7035b2fc499SJeff Garzik 	kaweth->status &= ~KAWETH_STATUS_CLOSING;
7045b2fc499SJeff Garzik 
705ceb0c77eSOliver Neukum 	usb_autopm_put_interface(kaweth->intf);
7065b2fc499SJeff Garzik 
7075b2fc499SJeff Garzik 	return 0;
7085b2fc499SJeff Garzik }
7095b2fc499SJeff Garzik 
7105b2fc499SJeff Garzik static u32 kaweth_get_link(struct net_device *dev)
7115b2fc499SJeff Garzik {
7125b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(dev);
7135b2fc499SJeff Garzik 
7145b2fc499SJeff Garzik 	return kaweth->linkstate;
7155b2fc499SJeff Garzik }
7165b2fc499SJeff Garzik 
7170fc0b732SStephen Hemminger static const struct ethtool_ops ops = {
7185b2fc499SJeff Garzik 	.get_link	= kaweth_get_link
7195b2fc499SJeff Garzik };
7205b2fc499SJeff Garzik 
7215b2fc499SJeff Garzik /****************************************************************
7225b2fc499SJeff Garzik  *     kaweth_usb_transmit_complete
7235b2fc499SJeff Garzik  ****************************************************************/
7245b2fc499SJeff Garzik static void kaweth_usb_transmit_complete(struct urb *urb)
7255b2fc499SJeff Garzik {
7265b2fc499SJeff Garzik 	struct kaweth_device *kaweth = urb->context;
7275b2fc499SJeff Garzik 	struct sk_buff *skb = kaweth->tx_skb;
728c94cb314SOliver Neukum 	int status = urb->status;
7295b2fc499SJeff Garzik 
730c94cb314SOliver Neukum 	if (unlikely(status != 0))
731c94cb314SOliver Neukum 		if (status != -ENOENT)
73249ae25b0SGreg Kroah-Hartman 			dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
73349ae25b0SGreg Kroah-Hartman 				kaweth->net->name, status);
7345b2fc499SJeff Garzik 
7355b2fc499SJeff Garzik 	netif_wake_queue(kaweth->net);
7365b2fc499SJeff Garzik 	dev_kfree_skb_irq(skb);
7375b2fc499SJeff Garzik }
7385b2fc499SJeff Garzik 
7395b2fc499SJeff Garzik /****************************************************************
7405b2fc499SJeff Garzik  *     kaweth_start_xmit
7415b2fc499SJeff Garzik  ****************************************************************/
74225a79c41SStephen Hemminger static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
74325a79c41SStephen Hemminger 					   struct net_device *net)
7445b2fc499SJeff Garzik {
7455b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(net);
7465b2fc499SJeff Garzik 	__le16 *private_header;
7475b2fc499SJeff Garzik 
7485b2fc499SJeff Garzik 	int res;
7495b2fc499SJeff Garzik 
7504ff61c8fSLarry Finger 	spin_lock_irq(&kaweth->device_lock);
7515b2fc499SJeff Garzik 
7525b2fc499SJeff Garzik 	kaweth_async_set_rx_mode(kaweth);
7535b2fc499SJeff Garzik 	netif_stop_queue(net);
7545b2fc499SJeff Garzik 	if (IS_BLOCKED(kaweth->status)) {
7555b2fc499SJeff Garzik 		goto skip;
7565b2fc499SJeff Garzik 	}
7575b2fc499SJeff Garzik 
7585b2fc499SJeff Garzik 	/* We now decide whether we can put our special header into the sk_buff */
75939fba783SEric Dumazet 	if (skb_cow_head(skb, 2)) {
7606ffa770eSTobias Klauser 		net->stats.tx_errors++;
7615b2fc499SJeff Garzik 		netif_start_queue(net);
7624ff61c8fSLarry Finger 		spin_unlock_irq(&kaweth->device_lock);
76339fba783SEric Dumazet 		dev_kfree_skb_any(skb);
7646ed10654SPatrick McHardy 		return NETDEV_TX_OK;
7655b2fc499SJeff Garzik 	}
7665b2fc499SJeff Garzik 
767d58ff351SJohannes Berg 	private_header = __skb_push(skb, 2);
7685b2fc499SJeff Garzik 	*private_header = cpu_to_le16(skb->len-2);
7695b2fc499SJeff Garzik 	kaweth->tx_skb = skb;
7705b2fc499SJeff Garzik 
7715b2fc499SJeff Garzik 	usb_fill_bulk_urb(kaweth->tx_urb,
7725b2fc499SJeff Garzik 		      kaweth->dev,
7735b2fc499SJeff Garzik 		      usb_sndbulkpipe(kaweth->dev, 2),
7745b2fc499SJeff Garzik 		      private_header,
7755b2fc499SJeff Garzik 		      skb->len,
7765b2fc499SJeff Garzik 		      kaweth_usb_transmit_complete,
7775b2fc499SJeff Garzik 		      kaweth);
7785b2fc499SJeff Garzik 	kaweth->end = 0;
7795b2fc499SJeff Garzik 
7805b2fc499SJeff Garzik 	if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
7815b2fc499SJeff Garzik 	{
7824dc89948SGreg Kroah-Hartman 		dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
7835b2fc499SJeff Garzik skip:
7846ffa770eSTobias Klauser 		net->stats.tx_errors++;
7855b2fc499SJeff Garzik 
7865b2fc499SJeff Garzik 		netif_start_queue(net);
7875b2fc499SJeff Garzik 		dev_kfree_skb_irq(skb);
7885b2fc499SJeff Garzik 	}
7895b2fc499SJeff Garzik 	else
7905b2fc499SJeff Garzik 	{
7916ffa770eSTobias Klauser 		net->stats.tx_packets++;
7926ffa770eSTobias Klauser 		net->stats.tx_bytes += skb->len;
7935b2fc499SJeff Garzik 	}
7945b2fc499SJeff Garzik 
7954ff61c8fSLarry Finger 	spin_unlock_irq(&kaweth->device_lock);
7965b2fc499SJeff Garzik 
7976ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7985b2fc499SJeff Garzik }
7995b2fc499SJeff Garzik 
8005b2fc499SJeff Garzik /****************************************************************
8015b2fc499SJeff Garzik  *     kaweth_set_rx_mode
8025b2fc499SJeff Garzik  ****************************************************************/
8035b2fc499SJeff Garzik static void kaweth_set_rx_mode(struct net_device *net)
8045b2fc499SJeff Garzik {
8055b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(net);
8065b2fc499SJeff Garzik 
8075b2fc499SJeff Garzik 	__u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
8085b2fc499SJeff Garzik                                      KAWETH_PACKET_FILTER_BROADCAST |
8095b2fc499SJeff Garzik 		                     KAWETH_PACKET_FILTER_MULTICAST;
8105b2fc499SJeff Garzik 
81149ae25b0SGreg Kroah-Hartman 	netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
8125b2fc499SJeff Garzik 
8135b2fc499SJeff Garzik 	netif_stop_queue(net);
8145b2fc499SJeff Garzik 
8155b2fc499SJeff Garzik 	if (net->flags & IFF_PROMISC) {
8165b2fc499SJeff Garzik 		packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
8175b2fc499SJeff Garzik 	}
8184cd24eafSJiri Pirko 	else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
8195b2fc499SJeff Garzik 		packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
8205b2fc499SJeff Garzik 	}
8215b2fc499SJeff Garzik 
8225b2fc499SJeff Garzik 	kaweth->packet_filter_bitmap = packet_filter_bitmap;
8235b2fc499SJeff Garzik 	netif_wake_queue(net);
8245b2fc499SJeff Garzik }
8255b2fc499SJeff Garzik 
8265b2fc499SJeff Garzik /****************************************************************
8275b2fc499SJeff Garzik  *     kaweth_async_set_rx_mode
8285b2fc499SJeff Garzik  ****************************************************************/
8295b2fc499SJeff Garzik static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
8305b2fc499SJeff Garzik {
831710b523aSLarry Finger 	int result;
8325b2fc499SJeff Garzik 	__u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
833710b523aSLarry Finger 
8345b2fc499SJeff Garzik 	kaweth->packet_filter_bitmap = 0;
8355b2fc499SJeff Garzik 	if (packet_filter_bitmap == 0)
8365b2fc499SJeff Garzik 		return;
8375b2fc499SJeff Garzik 
838710b523aSLarry Finger 	if (in_interrupt())
839710b523aSLarry Finger 		return;
840710b523aSLarry Finger 
8415b2fc499SJeff Garzik 	result = kaweth_control(kaweth,
8425b2fc499SJeff Garzik 				usb_sndctrlpipe(kaweth->dev, 0),
8435b2fc499SJeff Garzik 				KAWETH_COMMAND_SET_PACKET_FILTER,
8445b2fc499SJeff Garzik 				USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
8455b2fc499SJeff Garzik 				packet_filter_bitmap,
8465b2fc499SJeff Garzik 				0,
8475b2fc499SJeff Garzik 				(void *)&kaweth->scratch,
8485b2fc499SJeff Garzik 				0,
8495b2fc499SJeff Garzik 				KAWETH_CONTROL_TIMEOUT);
8505b2fc499SJeff Garzik 
8515b2fc499SJeff Garzik 	if(result < 0) {
852db6b2219SGreg Kroah-Hartman 		dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
853db6b2219SGreg Kroah-Hartman 			result);
8545b2fc499SJeff Garzik 	}
8555b2fc499SJeff Garzik 	else {
85649ae25b0SGreg Kroah-Hartman 		netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
85749ae25b0SGreg Kroah-Hartman 			   packet_filter_bitmap);
8585b2fc499SJeff Garzik 	}
8595b2fc499SJeff Garzik }
8605b2fc499SJeff Garzik 
8615b2fc499SJeff Garzik /****************************************************************
8625b2fc499SJeff Garzik  *     kaweth_tx_timeout
8635b2fc499SJeff Garzik  ****************************************************************/
8640290bd29SMichael S. Tsirkin static void kaweth_tx_timeout(struct net_device *net, unsigned int txqueue)
8655b2fc499SJeff Garzik {
8665b2fc499SJeff Garzik 	struct kaweth_device *kaweth = netdev_priv(net);
8675b2fc499SJeff Garzik 
8684dc89948SGreg Kroah-Hartman 	dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
8696ffa770eSTobias Klauser 	net->stats.tx_errors++;
870860e9538SFlorian Westphal 	netif_trans_update(net);
8715b2fc499SJeff Garzik 
8725b2fc499SJeff Garzik 	usb_unlink_urb(kaweth->tx_urb);
8735b2fc499SJeff Garzik }
8745b2fc499SJeff Garzik 
8755b2fc499SJeff Garzik /****************************************************************
8765b2fc499SJeff Garzik  *     kaweth_suspend
8775b2fc499SJeff Garzik  ****************************************************************/
8785b2fc499SJeff Garzik static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
8795b2fc499SJeff Garzik {
8805b2fc499SJeff Garzik 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
8815b2fc499SJeff Garzik 	unsigned long flags;
8825b2fc499SJeff Garzik 
8835b2fc499SJeff Garzik 	spin_lock_irqsave(&kaweth->device_lock, flags);
8845b2fc499SJeff Garzik 	kaweth->status |= KAWETH_STATUS_SUSPENDING;
8855b2fc499SJeff Garzik 	spin_unlock_irqrestore(&kaweth->device_lock, flags);
8865b2fc499SJeff Garzik 
8875b2fc499SJeff Garzik 	kaweth_kill_urbs(kaweth);
8885b2fc499SJeff Garzik 	return 0;
8895b2fc499SJeff Garzik }
8905b2fc499SJeff Garzik 
8915b2fc499SJeff Garzik /****************************************************************
8925b2fc499SJeff Garzik  *     kaweth_resume
8935b2fc499SJeff Garzik  ****************************************************************/
8945b2fc499SJeff Garzik static int kaweth_resume(struct usb_interface *intf)
8955b2fc499SJeff Garzik {
8965b2fc499SJeff Garzik 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
8975b2fc499SJeff Garzik 	unsigned long flags;
8985b2fc499SJeff Garzik 
8995b2fc499SJeff Garzik 	spin_lock_irqsave(&kaweth->device_lock, flags);
9005b2fc499SJeff Garzik 	kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
9015b2fc499SJeff Garzik 	spin_unlock_irqrestore(&kaweth->device_lock, flags);
9025b2fc499SJeff Garzik 
9035b2fc499SJeff Garzik 	if (!kaweth->opened)
9045b2fc499SJeff Garzik 		return 0;
9055b2fc499SJeff Garzik 	kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
9065b2fc499SJeff Garzik 	kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
9075b2fc499SJeff Garzik 
9085b2fc499SJeff Garzik 	return 0;
9095b2fc499SJeff Garzik }
9105b2fc499SJeff Garzik 
9115b2fc499SJeff Garzik /****************************************************************
9125b2fc499SJeff Garzik  *     kaweth_probe
9135b2fc499SJeff Garzik  ****************************************************************/
914321ea8b2SOliver Neukum 
915321ea8b2SOliver Neukum 
916321ea8b2SOliver Neukum static const struct net_device_ops kaweth_netdev_ops = {
917321ea8b2SOliver Neukum 	.ndo_open =			kaweth_open,
918321ea8b2SOliver Neukum 	.ndo_stop =			kaweth_close,
919321ea8b2SOliver Neukum 	.ndo_start_xmit =		kaweth_start_xmit,
920321ea8b2SOliver Neukum 	.ndo_tx_timeout =		kaweth_tx_timeout,
921afc4b13dSJiri Pirko 	.ndo_set_rx_mode =		kaweth_set_rx_mode,
922240c102dSBen Hutchings 	.ndo_set_mac_address =		eth_mac_addr,
923240c102dSBen Hutchings 	.ndo_validate_addr =		eth_validate_addr,
924321ea8b2SOliver Neukum };
925321ea8b2SOliver Neukum 
9265b2fc499SJeff Garzik static int kaweth_probe(
9275b2fc499SJeff Garzik 		struct usb_interface *intf,
9285b2fc499SJeff Garzik 		const struct usb_device_id *id      /* from id_table */
9295b2fc499SJeff Garzik 	)
9305b2fc499SJeff Garzik {
93149ae25b0SGreg Kroah-Hartman 	struct device *dev = &intf->dev;
93249ae25b0SGreg Kroah-Hartman 	struct usb_device *udev = interface_to_usbdev(intf);
9335b2fc499SJeff Garzik 	struct kaweth_device *kaweth;
9345b2fc499SJeff Garzik 	struct net_device *netdev;
9355b2fc499SJeff Garzik 	const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
9365b2fc499SJeff Garzik 	int result = 0;
937575ced7fSOliver Neukum 	int rv = -EIO;
9385b2fc499SJeff Garzik 
93949ae25b0SGreg Kroah-Hartman 	dev_dbg(dev,
94049ae25b0SGreg Kroah-Hartman 		"Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
94149ae25b0SGreg Kroah-Hartman 		udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
94249ae25b0SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct),
94349ae25b0SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.bcdDevice));
9445b2fc499SJeff Garzik 
94549ae25b0SGreg Kroah-Hartman 	dev_dbg(dev, "Device at %p\n", udev);
9465b2fc499SJeff Garzik 
94749ae25b0SGreg Kroah-Hartman 	dev_dbg(dev, "Descriptor length: %x type: %x\n",
94849ae25b0SGreg Kroah-Hartman 		(int)udev->descriptor.bLength,
94949ae25b0SGreg Kroah-Hartman 		(int)udev->descriptor.bDescriptorType);
9505b2fc499SJeff Garzik 
9515b2fc499SJeff Garzik 	netdev = alloc_etherdev(sizeof(*kaweth));
9525b2fc499SJeff Garzik 	if (!netdev)
9535b2fc499SJeff Garzik 		return -ENOMEM;
9545b2fc499SJeff Garzik 
9555b2fc499SJeff Garzik 	kaweth = netdev_priv(netdev);
95649ae25b0SGreg Kroah-Hartman 	kaweth->dev = udev;
9575b2fc499SJeff Garzik 	kaweth->net = netdev;
95860bcabd0SOliver Neukum 	kaweth->intf = intf;
9595b2fc499SJeff Garzik 
9605b2fc499SJeff Garzik 	spin_lock_init(&kaweth->device_lock);
9615b2fc499SJeff Garzik 	init_waitqueue_head(&kaweth->term_wait);
9625b2fc499SJeff Garzik 
96349ae25b0SGreg Kroah-Hartman 	dev_dbg(dev, "Resetting.\n");
9645b2fc499SJeff Garzik 
9655b2fc499SJeff Garzik 	kaweth_reset(kaweth);
9665b2fc499SJeff Garzik 
9675b2fc499SJeff Garzik 	/*
9685b2fc499SJeff Garzik 	 * If high byte of bcdDevice is nonzero, firmware is already
9695b2fc499SJeff Garzik 	 * downloaded. Don't try to do it again, or we'll hang the device.
9705b2fc499SJeff Garzik 	 */
9715b2fc499SJeff Garzik 
97249ae25b0SGreg Kroah-Hartman 	if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
97349ae25b0SGreg Kroah-Hartman 		dev_info(dev, "Firmware present in device.\n");
9745b2fc499SJeff Garzik 	} else {
9755b2fc499SJeff Garzik 		/* Download the firmware */
97649ae25b0SGreg Kroah-Hartman 		dev_info(dev, "Downloading firmware...\n");
9775b2fc499SJeff Garzik 		kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
978575ced7fSOliver Neukum 		if (!kaweth->firmware_buf) {
979575ced7fSOliver Neukum 			rv = -ENOMEM;
980575ced7fSOliver Neukum 			goto err_free_netdev;
981575ced7fSOliver Neukum 		}
9825b2fc499SJeff Garzik 		if ((result = kaweth_download_firmware(kaweth,
98379682499SDavid Woodhouse 						      "kaweth/new_code.bin",
9845b2fc499SJeff Garzik 						      100,
9855b2fc499SJeff Garzik 						      2)) < 0) {
98649ae25b0SGreg Kroah-Hartman 			dev_err(dev, "Error downloading firmware (%d)\n",
987db6b2219SGreg Kroah-Hartman 				result);
9885b2fc499SJeff Garzik 			goto err_fw;
9895b2fc499SJeff Garzik 		}
9905b2fc499SJeff Garzik 
9915b2fc499SJeff Garzik 		if ((result = kaweth_download_firmware(kaweth,
99279682499SDavid Woodhouse 						      "kaweth/new_code_fix.bin",
9935b2fc499SJeff Garzik 						      100,
9945b2fc499SJeff Garzik 						      3)) < 0) {
99549ae25b0SGreg Kroah-Hartman 			dev_err(dev, "Error downloading firmware fix (%d)\n",
996db6b2219SGreg Kroah-Hartman 				result);
9975b2fc499SJeff Garzik 			goto err_fw;
9985b2fc499SJeff Garzik 		}
9995b2fc499SJeff Garzik 
10005b2fc499SJeff Garzik 		if ((result = kaweth_download_firmware(kaweth,
100179682499SDavid Woodhouse 						      "kaweth/trigger_code.bin",
10025b2fc499SJeff Garzik 						      126,
10035b2fc499SJeff Garzik 						      2)) < 0) {
100449ae25b0SGreg Kroah-Hartman 			dev_err(dev, "Error downloading trigger code (%d)\n",
1005db6b2219SGreg Kroah-Hartman 				result);
10065b2fc499SJeff Garzik 			goto err_fw;
10075b2fc499SJeff Garzik 
10085b2fc499SJeff Garzik 		}
10095b2fc499SJeff Garzik 
10105b2fc499SJeff Garzik 		if ((result = kaweth_download_firmware(kaweth,
101179682499SDavid Woodhouse 						      "kaweth/trigger_code_fix.bin",
10125b2fc499SJeff Garzik 						      126,
10135b2fc499SJeff Garzik 						      3)) < 0) {
101449ae25b0SGreg Kroah-Hartman 			dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
10155b2fc499SJeff Garzik 			goto err_fw;
10165b2fc499SJeff Garzik 		}
10175b2fc499SJeff Garzik 
10185b2fc499SJeff Garzik 
10195b2fc499SJeff Garzik 		if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
102049ae25b0SGreg Kroah-Hartman 			dev_err(dev, "Error triggering firmware (%d)\n", result);
10215b2fc499SJeff Garzik 			goto err_fw;
10225b2fc499SJeff Garzik 		}
10235b2fc499SJeff Garzik 
10245b2fc499SJeff Garzik 		/* Device will now disappear for a moment...  */
102549ae25b0SGreg Kroah-Hartman 		dev_info(dev, "Firmware loaded.  I'll be back...\n");
10265b2fc499SJeff Garzik err_fw:
10275b2fc499SJeff Garzik 		free_page((unsigned long)kaweth->firmware_buf);
10285b2fc499SJeff Garzik 		free_netdev(netdev);
10295b2fc499SJeff Garzik 		return -EIO;
10305b2fc499SJeff Garzik 	}
10315b2fc499SJeff Garzik 
10325b2fc499SJeff Garzik 	result = kaweth_read_configuration(kaweth);
10335b2fc499SJeff Garzik 
10345b2fc499SJeff Garzik 	if(result < 0) {
103549ae25b0SGreg Kroah-Hartman 		dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
10365b2fc499SJeff Garzik 		goto err_free_netdev;
10375b2fc499SJeff Garzik 	}
10385b2fc499SJeff Garzik 
103949ae25b0SGreg Kroah-Hartman 	dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
104049ae25b0SGreg Kroah-Hartman 	dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
104149ae25b0SGreg Kroah-Hartman 	dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
104249ae25b0SGreg Kroah-Hartman 	dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
10435b2fc499SJeff Garzik 
10445b2fc499SJeff Garzik 	if(!memcmp(&kaweth->configuration.hw_addr,
10455b2fc499SJeff Garzik                    &bcast_addr,
10465b2fc499SJeff Garzik 		   sizeof(bcast_addr))) {
104749ae25b0SGreg Kroah-Hartman 		dev_err(dev, "Firmware not functioning properly, no net device created\n");
10485b2fc499SJeff Garzik 		goto err_free_netdev;
10495b2fc499SJeff Garzik 	}
10505b2fc499SJeff Garzik 
10515b2fc499SJeff Garzik 	if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
105249ae25b0SGreg Kroah-Hartman 		dev_dbg(dev, "Error setting URB size\n");
10535b2fc499SJeff Garzik 		goto err_free_netdev;
10545b2fc499SJeff Garzik 	}
10555b2fc499SJeff Garzik 
10565b2fc499SJeff Garzik 	if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
105749ae25b0SGreg Kroah-Hartman 		dev_err(dev, "Error setting SOFS wait\n");
10585b2fc499SJeff Garzik 		goto err_free_netdev;
10595b2fc499SJeff Garzik 	}
10605b2fc499SJeff Garzik 
10615b2fc499SJeff Garzik 	result = kaweth_set_receive_filter(kaweth,
10625b2fc499SJeff Garzik                                            KAWETH_PACKET_FILTER_DIRECTED |
10635b2fc499SJeff Garzik                                            KAWETH_PACKET_FILTER_BROADCAST |
10645b2fc499SJeff Garzik                                            KAWETH_PACKET_FILTER_MULTICAST);
10655b2fc499SJeff Garzik 
10665b2fc499SJeff Garzik 	if(result < 0) {
106749ae25b0SGreg Kroah-Hartman 		dev_err(dev, "Error setting receive filter\n");
10685b2fc499SJeff Garzik 		goto err_free_netdev;
10695b2fc499SJeff Garzik 	}
10705b2fc499SJeff Garzik 
107149ae25b0SGreg Kroah-Hartman 	dev_dbg(dev, "Initializing net device.\n");
10725b2fc499SJeff Garzik 
10735b2fc499SJeff Garzik 	kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
10745b2fc499SJeff Garzik 	if (!kaweth->tx_urb)
10755b2fc499SJeff Garzik 		goto err_free_netdev;
10765b2fc499SJeff Garzik 	kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
10775b2fc499SJeff Garzik 	if (!kaweth->rx_urb)
10785b2fc499SJeff Garzik 		goto err_only_tx;
10795b2fc499SJeff Garzik 	kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
10805b2fc499SJeff Garzik 	if (!kaweth->irq_urb)
10815b2fc499SJeff Garzik 		goto err_tx_and_rx;
10825b2fc499SJeff Garzik 
1083997ea58eSDaniel Mack 	kaweth->intbuffer = usb_alloc_coherent(	kaweth->dev,
10845b2fc499SJeff Garzik 						INTBUFFERSIZE,
10855b2fc499SJeff Garzik 						GFP_KERNEL,
10865b2fc499SJeff Garzik 						&kaweth->intbufferhandle);
10875b2fc499SJeff Garzik 	if (!kaweth->intbuffer)
10885b2fc499SJeff Garzik 		goto err_tx_and_rx_and_irq;
1089997ea58eSDaniel Mack 	kaweth->rx_buf = usb_alloc_coherent(	kaweth->dev,
10905b2fc499SJeff Garzik 						KAWETH_BUF_SIZE,
10915b2fc499SJeff Garzik 						GFP_KERNEL,
10925b2fc499SJeff Garzik 						&kaweth->rxbufferhandle);
10935b2fc499SJeff Garzik 	if (!kaweth->rx_buf)
10945b2fc499SJeff Garzik 		goto err_all_but_rxbuf;
10955b2fc499SJeff Garzik 
10965b2fc499SJeff Garzik 	memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
10975b2fc499SJeff Garzik 	memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
10985b2fc499SJeff Garzik                sizeof(kaweth->configuration.hw_addr));
10995b2fc499SJeff Garzik 
1100321ea8b2SOliver Neukum 	netdev->netdev_ops = &kaweth_netdev_ops;
11015b2fc499SJeff Garzik 	netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
11025b2fc499SJeff Garzik 	netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
11037ad24ea4SWilfried Klaebe 	netdev->ethtool_ops = &ops;
11045b2fc499SJeff Garzik 
11055b2fc499SJeff Garzik 	/* kaweth is zeroed as part of alloc_netdev */
11065b2fc499SJeff Garzik 	INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
11075b2fc499SJeff Garzik 	usb_set_intfdata(intf, kaweth);
11085b2fc499SJeff Garzik 
110949ae25b0SGreg Kroah-Hartman 	SET_NETDEV_DEV(netdev, dev);
11105b2fc499SJeff Garzik 	if (register_netdev(netdev) != 0) {
111149ae25b0SGreg Kroah-Hartman 		dev_err(dev, "Error registering netdev.\n");
11125b2fc499SJeff Garzik 		goto err_intfdata;
11135b2fc499SJeff Garzik 	}
11145b2fc499SJeff Garzik 
111549ae25b0SGreg Kroah-Hartman 	dev_info(dev, "kaweth interface created at %s\n",
1116880c9c66SGreg Kroah-Hartman 		 kaweth->net->name);
11175b2fc499SJeff Garzik 
11185b2fc499SJeff Garzik 	return 0;
11195b2fc499SJeff Garzik 
11205b2fc499SJeff Garzik err_intfdata:
11215b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
1122997ea58eSDaniel Mack 	usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
11235b2fc499SJeff Garzik err_all_but_rxbuf:
1124997ea58eSDaniel Mack 	usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
11255b2fc499SJeff Garzik err_tx_and_rx_and_irq:
11265b2fc499SJeff Garzik 	usb_free_urb(kaweth->irq_urb);
11275b2fc499SJeff Garzik err_tx_and_rx:
11285b2fc499SJeff Garzik 	usb_free_urb(kaweth->rx_urb);
11295b2fc499SJeff Garzik err_only_tx:
11305b2fc499SJeff Garzik 	usb_free_urb(kaweth->tx_urb);
11315b2fc499SJeff Garzik err_free_netdev:
11325b2fc499SJeff Garzik 	free_netdev(netdev);
11335b2fc499SJeff Garzik 
1134575ced7fSOliver Neukum 	return rv;
11355b2fc499SJeff Garzik }
11365b2fc499SJeff Garzik 
11375b2fc499SJeff Garzik /****************************************************************
11385b2fc499SJeff Garzik  *     kaweth_disconnect
11395b2fc499SJeff Garzik  ****************************************************************/
11405b2fc499SJeff Garzik static void kaweth_disconnect(struct usb_interface *intf)
11415b2fc499SJeff Garzik {
11425b2fc499SJeff Garzik 	struct kaweth_device *kaweth = usb_get_intfdata(intf);
11435b2fc499SJeff Garzik 	struct net_device *netdev;
11445b2fc499SJeff Garzik 
11455b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
11465b2fc499SJeff Garzik 	if (!kaweth) {
114725985edcSLucas De Marchi 		dev_warn(&intf->dev, "unregistering non-existent device\n");
11485b2fc499SJeff Garzik 		return;
11495b2fc499SJeff Garzik 	}
11505b2fc499SJeff Garzik 	netdev = kaweth->net;
11515b2fc499SJeff Garzik 
115249ae25b0SGreg Kroah-Hartman 	netdev_dbg(kaweth->net, "Unregistering net device\n");
11535b2fc499SJeff Garzik 	unregister_netdev(netdev);
11545b2fc499SJeff Garzik 
11555b2fc499SJeff Garzik 	usb_free_urb(kaweth->rx_urb);
11565b2fc499SJeff Garzik 	usb_free_urb(kaweth->tx_urb);
11575b2fc499SJeff Garzik 	usb_free_urb(kaweth->irq_urb);
11585b2fc499SJeff Garzik 
1159997ea58eSDaniel Mack 	usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1160997ea58eSDaniel Mack 	usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
11615b2fc499SJeff Garzik 
11625b2fc499SJeff Garzik 	free_netdev(netdev);
11635b2fc499SJeff Garzik }
11645b2fc499SJeff Garzik 
11655b2fc499SJeff Garzik 
11665b2fc499SJeff Garzik // FIXME this completion stuff is a modified clone of
11675b2fc499SJeff Garzik // an OLD version of some stuff in usb.c ...
11685b2fc499SJeff Garzik struct usb_api_data {
11695b2fc499SJeff Garzik 	wait_queue_head_t wqh;
11705b2fc499SJeff Garzik 	int done;
11715b2fc499SJeff Garzik };
11725b2fc499SJeff Garzik 
11735b2fc499SJeff Garzik /*-------------------------------------------------------------------*
11745b2fc499SJeff Garzik  * completion handler for compatibility wrappers (sync control/bulk) *
11755b2fc499SJeff Garzik  *-------------------------------------------------------------------*/
11765b2fc499SJeff Garzik static void usb_api_blocking_completion(struct urb *urb)
11775b2fc499SJeff Garzik {
11785b2fc499SJeff Garzik         struct usb_api_data *awd = (struct usb_api_data *)urb->context;
11795b2fc499SJeff Garzik 
11805b2fc499SJeff Garzik 	awd->done=1;
11815b2fc499SJeff Garzik 	wake_up(&awd->wqh);
11825b2fc499SJeff Garzik }
11835b2fc499SJeff Garzik 
11845b2fc499SJeff Garzik /*-------------------------------------------------------------------*
11855b2fc499SJeff Garzik  *                         COMPATIBILITY STUFF                       *
11865b2fc499SJeff Garzik  *-------------------------------------------------------------------*/
11875b2fc499SJeff Garzik 
11885b2fc499SJeff Garzik // Starts urb and waits for completion or timeout
11895b2fc499SJeff Garzik static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
11905b2fc499SJeff Garzik {
11915b2fc499SJeff Garzik 	struct usb_api_data awd;
11925b2fc499SJeff Garzik         int status;
11935b2fc499SJeff Garzik 
11945b2fc499SJeff Garzik         init_waitqueue_head(&awd.wqh);
11955b2fc499SJeff Garzik         awd.done = 0;
11965b2fc499SJeff Garzik 
11975b2fc499SJeff Garzik         urb->context = &awd;
119849c9e7c2SAlexey Khoroshilov         status = usb_submit_urb(urb, GFP_ATOMIC);
11995b2fc499SJeff Garzik         if (status) {
12005b2fc499SJeff Garzik                 // something went wrong
12015b2fc499SJeff Garzik                 usb_free_urb(urb);
12025b2fc499SJeff Garzik                 return status;
12035b2fc499SJeff Garzik         }
12045b2fc499SJeff Garzik 
12055b2fc499SJeff Garzik 	if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
12065b2fc499SJeff Garzik                 // timeout
12074dc89948SGreg Kroah-Hartman                 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
12085b2fc499SJeff Garzik                 usb_kill_urb(urb);  // remove urb safely
12095b2fc499SJeff Garzik                 status = -ETIMEDOUT;
12105b2fc499SJeff Garzik         }
12115b2fc499SJeff Garzik 	else {
12125b2fc499SJeff Garzik                 status = urb->status;
12135b2fc499SJeff Garzik 	}
12145b2fc499SJeff Garzik 
12155b2fc499SJeff Garzik         if (actual_length) {
12165b2fc499SJeff Garzik                 *actual_length = urb->actual_length;
12175b2fc499SJeff Garzik 	}
12185b2fc499SJeff Garzik 
12195b2fc499SJeff Garzik         usb_free_urb(urb);
12205b2fc499SJeff Garzik         return status;
12215b2fc499SJeff Garzik }
12225b2fc499SJeff Garzik 
12235b2fc499SJeff Garzik /*-------------------------------------------------------------------*/
12245b2fc499SJeff Garzik // returns status (negative) or length (positive)
12255b2fc499SJeff Garzik static int kaweth_internal_control_msg(struct usb_device *usb_dev,
12265b2fc499SJeff Garzik 				       unsigned int pipe,
12275b2fc499SJeff Garzik 				       struct usb_ctrlrequest *cmd, void *data,
12285b2fc499SJeff Garzik 				       int len, int timeout)
12295b2fc499SJeff Garzik {
12305b2fc499SJeff Garzik         struct urb *urb;
12315b2fc499SJeff Garzik         int retv;
12325b2fc499SJeff Garzik         int length = 0; /* shut up GCC */
12335b2fc499SJeff Garzik 
1234e4c7f259SDan Carpenter 	urb = usb_alloc_urb(0, GFP_ATOMIC);
12355b2fc499SJeff Garzik         if (!urb)
12365b2fc499SJeff Garzik                 return -ENOMEM;
12375b2fc499SJeff Garzik 
12385b2fc499SJeff Garzik         usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
12395b2fc499SJeff Garzik 			 len, usb_api_blocking_completion, NULL);
12405b2fc499SJeff Garzik 
12415b2fc499SJeff Garzik         retv = usb_start_wait_urb(urb, timeout, &length);
12425b2fc499SJeff Garzik         if (retv < 0) {
12435b2fc499SJeff Garzik                 return retv;
12445b2fc499SJeff Garzik 	}
12455b2fc499SJeff Garzik         else {
12465b2fc499SJeff Garzik                 return length;
12475b2fc499SJeff Garzik 	}
12485b2fc499SJeff Garzik }
12495b2fc499SJeff Garzik 
1250d632eb1bSGreg Kroah-Hartman module_usb_driver(kaweth_driver);
1251