18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/****************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *     kaweth.c - driver for KL5KUSB101 based USB->Ethernet
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *     (c) 2000 Interlan Communications
78c2ecf20Sopenharmony_ci *     (c) 2000 Stephane Alnet
88c2ecf20Sopenharmony_ci *     (C) 2001 Brad Hards
98c2ecf20Sopenharmony_ci *     (C) 2002 Oliver Neukum
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *     Original author: The Zapman <zapman@interlan.net>
128c2ecf20Sopenharmony_ci *     Inspired by, and much credit goes to Michael Rothwell
138c2ecf20Sopenharmony_ci *     <rothwell@interlan.net> for the test equipment, help, and patience
148c2ecf20Sopenharmony_ci *     Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
158c2ecf20Sopenharmony_ci *     Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
168c2ecf20Sopenharmony_ci *     for providing the firmware and driver resources.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci ****************************************************************/
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* TODO:
218c2ecf20Sopenharmony_ci * Develop test procedures for USB net interfaces
228c2ecf20Sopenharmony_ci * Run test procedures
238c2ecf20Sopenharmony_ci * Fix bugs from previous two steps
248c2ecf20Sopenharmony_ci * Snoop other OSs for any tricks we're not doing
258c2ecf20Sopenharmony_ci * Reduce arbitrary timeouts
268c2ecf20Sopenharmony_ci * Smart multicast support
278c2ecf20Sopenharmony_ci * Temporary MAC change support
288c2ecf20Sopenharmony_ci * Tunable SOFs parameter - ioctl()?
298c2ecf20Sopenharmony_ci * Ethernet stats collection
308c2ecf20Sopenharmony_ci * Code formatting improvements
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/module.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci#include <linux/string.h>
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
388c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
398c2ecf20Sopenharmony_ci#include <linux/usb.h>
408c2ecf20Sopenharmony_ci#include <linux/types.h>
418c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
428c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
438c2ecf20Sopenharmony_ci#include <linux/wait.h>
448c2ecf20Sopenharmony_ci#include <linux/firmware.h>
458c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
468c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#undef DEBUG
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define KAWETH_MTU			1514
518c2ecf20Sopenharmony_ci#define KAWETH_BUF_SIZE			1664
528c2ecf20Sopenharmony_ci#define KAWETH_TX_TIMEOUT		(5 * HZ)
538c2ecf20Sopenharmony_ci#define KAWETH_SCRATCH_SIZE		32
548c2ecf20Sopenharmony_ci#define KAWETH_FIRMWARE_BUF_SIZE	4096
558c2ecf20Sopenharmony_ci#define KAWETH_CONTROL_TIMEOUT		(30000)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define KAWETH_STATUS_BROKEN		0x0000001
588c2ecf20Sopenharmony_ci#define KAWETH_STATUS_CLOSING		0x0000002
598c2ecf20Sopenharmony_ci#define KAWETH_STATUS_SUSPENDING	0x0000004
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define KAWETH_PACKET_FILTER_PROMISCUOUS	0x01
648c2ecf20Sopenharmony_ci#define KAWETH_PACKET_FILTER_ALL_MULTICAST	0x02
658c2ecf20Sopenharmony_ci#define KAWETH_PACKET_FILTER_DIRECTED		0x04
668c2ecf20Sopenharmony_ci#define KAWETH_PACKET_FILTER_BROADCAST		0x08
678c2ecf20Sopenharmony_ci#define KAWETH_PACKET_FILTER_MULTICAST		0x10
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Table 7 */
708c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_GET_ETHERNET_DESC	0x00
718c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
728c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_SET_PACKET_FILTER	0x02
738c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_STATISTICS               0x03
748c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_SET_TEMP_MAC     	0x06
758c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_GET_TEMP_MAC             0x07
768c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_SET_URB_SIZE		0x08
778c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_SET_SOFS_WAIT		0x09
788c2ecf20Sopenharmony_ci#define KAWETH_COMMAND_SCAN			0xFF
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define KAWETH_SOFS_TO_WAIT			0x05
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define INTBUFFERSIZE				4
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define STATE_OFFSET				0
858c2ecf20Sopenharmony_ci#define STATE_MASK				0x40
868c2ecf20Sopenharmony_ci#define	STATE_SHIFT				5
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
948c2ecf20Sopenharmony_ciMODULE_FIRMWARE("kaweth/new_code.bin");
958c2ecf20Sopenharmony_ciMODULE_FIRMWARE("kaweth/new_code_fix.bin");
968c2ecf20Sopenharmony_ciMODULE_FIRMWARE("kaweth/trigger_code.bin");
978c2ecf20Sopenharmony_ciMODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic const char driver_name[] = "kaweth";
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int kaweth_probe(
1028c2ecf20Sopenharmony_ci		struct usb_interface *intf,
1038c2ecf20Sopenharmony_ci		const struct usb_device_id *id	/* from id_table */
1048c2ecf20Sopenharmony_ci	);
1058c2ecf20Sopenharmony_cistatic void kaweth_disconnect(struct usb_interface *intf);
1068c2ecf20Sopenharmony_cistatic int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
1078c2ecf20Sopenharmony_cistatic int kaweth_resume(struct usb_interface *intf);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/****************************************************************
1108c2ecf20Sopenharmony_ci *     usb_device_id
1118c2ecf20Sopenharmony_ci ****************************************************************/
1128c2ecf20Sopenharmony_cistatic const struct usb_device_id usb_klsi_table[] = {
1138c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
1148c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
1158c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
1168c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
1178c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
1188c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
1198c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
1208c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
1218c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
1228c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
1238c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
1248c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
1258c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
1268c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
1278c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
1288c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
1298c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
1308c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
1318c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
1328c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
1338c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
1348c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
1358c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
1368c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
1378c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
1388c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
1398c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
1408c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
1418c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1485, 0x0001) },	/* Silicom U2E */
1428c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
1438c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
1448c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
1458c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
1468c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
1478c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
1488c2ecf20Sopenharmony_ci	{} /* Null terminator */
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE (usb, usb_klsi_table);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/****************************************************************
1548c2ecf20Sopenharmony_ci *     kaweth_driver
1558c2ecf20Sopenharmony_ci ****************************************************************/
1568c2ecf20Sopenharmony_cistatic struct usb_driver kaweth_driver = {
1578c2ecf20Sopenharmony_ci	.name =		driver_name,
1588c2ecf20Sopenharmony_ci	.probe =	kaweth_probe,
1598c2ecf20Sopenharmony_ci	.disconnect =	kaweth_disconnect,
1608c2ecf20Sopenharmony_ci	.suspend =	kaweth_suspend,
1618c2ecf20Sopenharmony_ci	.resume =	kaweth_resume,
1628c2ecf20Sopenharmony_ci	.id_table =     usb_klsi_table,
1638c2ecf20Sopenharmony_ci	.supports_autosuspend =	1,
1648c2ecf20Sopenharmony_ci	.disable_hub_initiated_lpm = 1,
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_citypedef __u8 eth_addr_t[6];
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/****************************************************************
1708c2ecf20Sopenharmony_ci *     usb_eth_dev
1718c2ecf20Sopenharmony_ci ****************************************************************/
1728c2ecf20Sopenharmony_cistruct usb_eth_dev {
1738c2ecf20Sopenharmony_ci	char *name;
1748c2ecf20Sopenharmony_ci	__u16 vendor;
1758c2ecf20Sopenharmony_ci	__u16 device;
1768c2ecf20Sopenharmony_ci	void *pdata;
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/****************************************************************
1808c2ecf20Sopenharmony_ci *     kaweth_ethernet_configuration
1818c2ecf20Sopenharmony_ci *     Refer Table 8
1828c2ecf20Sopenharmony_ci ****************************************************************/
1838c2ecf20Sopenharmony_cistruct kaweth_ethernet_configuration
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	__u8 size;
1868c2ecf20Sopenharmony_ci	__u8 reserved1;
1878c2ecf20Sopenharmony_ci	__u8 reserved2;
1888c2ecf20Sopenharmony_ci	eth_addr_t hw_addr;
1898c2ecf20Sopenharmony_ci	__u32 statistics_mask;
1908c2ecf20Sopenharmony_ci	__le16 segment_size;
1918c2ecf20Sopenharmony_ci	__u16 max_multicast_filters;
1928c2ecf20Sopenharmony_ci	__u8 reserved3;
1938c2ecf20Sopenharmony_ci} __packed;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/****************************************************************
1968c2ecf20Sopenharmony_ci *     kaweth_device
1978c2ecf20Sopenharmony_ci ****************************************************************/
1988c2ecf20Sopenharmony_cistruct kaweth_device
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	spinlock_t device_lock;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	__u32 status;
2038c2ecf20Sopenharmony_ci	int end;
2048c2ecf20Sopenharmony_ci	int suspend_lowmem_rx;
2058c2ecf20Sopenharmony_ci	int suspend_lowmem_ctrl;
2068c2ecf20Sopenharmony_ci	int linkstate;
2078c2ecf20Sopenharmony_ci	int opened;
2088c2ecf20Sopenharmony_ci	struct delayed_work lowmem_work;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	struct usb_device *dev;
2118c2ecf20Sopenharmony_ci	struct usb_interface *intf;
2128c2ecf20Sopenharmony_ci	struct net_device *net;
2138c2ecf20Sopenharmony_ci	wait_queue_head_t term_wait;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	struct urb *rx_urb;
2168c2ecf20Sopenharmony_ci	struct urb *tx_urb;
2178c2ecf20Sopenharmony_ci	struct urb *irq_urb;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	dma_addr_t intbufferhandle;
2208c2ecf20Sopenharmony_ci	__u8 *intbuffer;
2218c2ecf20Sopenharmony_ci	dma_addr_t rxbufferhandle;
2228c2ecf20Sopenharmony_ci	__u8 *rx_buf;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	__u8 *firmware_buf;
2288c2ecf20Sopenharmony_ci	__u8 scratch[KAWETH_SCRATCH_SIZE];
2298c2ecf20Sopenharmony_ci	__u16 packet_filter_bitmap;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	struct kaweth_ethernet_configuration configuration;
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/****************************************************************
2358c2ecf20Sopenharmony_ci *     kaweth_read_configuration
2368c2ecf20Sopenharmony_ci ****************************************************************/
2378c2ecf20Sopenharmony_cistatic int kaweth_read_configuration(struct kaweth_device *kaweth)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_rcvctrlpipe(kaweth->dev, 0),
2408c2ecf20Sopenharmony_ci				KAWETH_COMMAND_GET_ETHERNET_DESC,
2418c2ecf20Sopenharmony_ci				USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
2428c2ecf20Sopenharmony_ci				0, 0,
2438c2ecf20Sopenharmony_ci				&kaweth->configuration,
2448c2ecf20Sopenharmony_ci				sizeof(kaweth->configuration),
2458c2ecf20Sopenharmony_ci				KAWETH_CONTROL_TIMEOUT);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/****************************************************************
2498c2ecf20Sopenharmony_ci *     kaweth_set_urb_size
2508c2ecf20Sopenharmony_ci ****************************************************************/
2518c2ecf20Sopenharmony_cistatic int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
2568c2ecf20Sopenharmony_ci			       KAWETH_COMMAND_SET_URB_SIZE,
2578c2ecf20Sopenharmony_ci			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
2588c2ecf20Sopenharmony_ci			       urb_size, 0,
2598c2ecf20Sopenharmony_ci			       &kaweth->scratch, 0,
2608c2ecf20Sopenharmony_ci			       KAWETH_CONTROL_TIMEOUT);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/****************************************************************
2648c2ecf20Sopenharmony_ci *     kaweth_set_sofs_wait
2658c2ecf20Sopenharmony_ci ****************************************************************/
2668c2ecf20Sopenharmony_cistatic int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
2718c2ecf20Sopenharmony_ci			       KAWETH_COMMAND_SET_SOFS_WAIT,
2728c2ecf20Sopenharmony_ci			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
2738c2ecf20Sopenharmony_ci			       sofs_wait, 0,
2748c2ecf20Sopenharmony_ci			       &kaweth->scratch, 0,
2758c2ecf20Sopenharmony_ci			       KAWETH_CONTROL_TIMEOUT);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci/****************************************************************
2798c2ecf20Sopenharmony_ci *     kaweth_set_receive_filter
2808c2ecf20Sopenharmony_ci ****************************************************************/
2818c2ecf20Sopenharmony_cistatic int kaweth_set_receive_filter(struct kaweth_device *kaweth,
2828c2ecf20Sopenharmony_ci				     __u16 receive_filter)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "Set receive filter to %d\n",
2858c2ecf20Sopenharmony_ci		   (unsigned)receive_filter);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
2888c2ecf20Sopenharmony_ci			       KAWETH_COMMAND_SET_PACKET_FILTER,
2898c2ecf20Sopenharmony_ci			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
2908c2ecf20Sopenharmony_ci			       receive_filter, 0,
2918c2ecf20Sopenharmony_ci			       &kaweth->scratch, 0,
2928c2ecf20Sopenharmony_ci			       KAWETH_CONTROL_TIMEOUT);
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/****************************************************************
2968c2ecf20Sopenharmony_ci *     kaweth_download_firmware
2978c2ecf20Sopenharmony_ci ****************************************************************/
2988c2ecf20Sopenharmony_cistatic int kaweth_download_firmware(struct kaweth_device *kaweth,
2998c2ecf20Sopenharmony_ci				    const char *fwname,
3008c2ecf20Sopenharmony_ci				    __u8 interrupt,
3018c2ecf20Sopenharmony_ci				    __u8 type)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	const struct firmware *fw;
3048c2ecf20Sopenharmony_ci	int data_len;
3058c2ecf20Sopenharmony_ci	int ret;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
3088c2ecf20Sopenharmony_ci	if (ret) {
3098c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev, "Firmware request failed\n");
3108c2ecf20Sopenharmony_ci		return ret;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
3148c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
3158c2ecf20Sopenharmony_ci			fw->size);
3168c2ecf20Sopenharmony_ci		release_firmware(fw);
3178c2ecf20Sopenharmony_ci		return -ENOSPC;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci	data_len = fw->size;
3208c2ecf20Sopenharmony_ci	memcpy(kaweth->firmware_buf, fw->data, fw->size);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	release_firmware(fw);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
3258c2ecf20Sopenharmony_ci	kaweth->firmware_buf[3] = data_len >> 8;
3268c2ecf20Sopenharmony_ci	kaweth->firmware_buf[4] = type;
3278c2ecf20Sopenharmony_ci	kaweth->firmware_buf[5] = interrupt;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
3308c2ecf20Sopenharmony_ci		   kaweth->firmware_buf[2]);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net,
3338c2ecf20Sopenharmony_ci		   "Downloading firmware at %p to kaweth device at %p\n",
3348c2ecf20Sopenharmony_ci		   kaweth->firmware_buf, kaweth);
3358c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3388c2ecf20Sopenharmony_ci			      KAWETH_COMMAND_SCAN,
3398c2ecf20Sopenharmony_ci			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
3408c2ecf20Sopenharmony_ci			      0, 0,
3418c2ecf20Sopenharmony_ci			      kaweth->firmware_buf, data_len,
3428c2ecf20Sopenharmony_ci			      KAWETH_CONTROL_TIMEOUT);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/****************************************************************
3468c2ecf20Sopenharmony_ci *     kaweth_trigger_firmware
3478c2ecf20Sopenharmony_ci ****************************************************************/
3488c2ecf20Sopenharmony_cistatic int kaweth_trigger_firmware(struct kaweth_device *kaweth,
3498c2ecf20Sopenharmony_ci				   __u8 interrupt)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	kaweth->firmware_buf[0] = 0xB6;
3528c2ecf20Sopenharmony_ci	kaweth->firmware_buf[1] = 0xC3;
3538c2ecf20Sopenharmony_ci	kaweth->firmware_buf[2] = 0x01;
3548c2ecf20Sopenharmony_ci	kaweth->firmware_buf[3] = 0x00;
3558c2ecf20Sopenharmony_ci	kaweth->firmware_buf[4] = 0x06;
3568c2ecf20Sopenharmony_ci	kaweth->firmware_buf[5] = interrupt;
3578c2ecf20Sopenharmony_ci	kaweth->firmware_buf[6] = 0x00;
3588c2ecf20Sopenharmony_ci	kaweth->firmware_buf[7] = 0x00;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
3618c2ecf20Sopenharmony_ci			       KAWETH_COMMAND_SCAN,
3628c2ecf20Sopenharmony_ci			       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
3638c2ecf20Sopenharmony_ci			       0, 0,
3648c2ecf20Sopenharmony_ci			       (void *)kaweth->firmware_buf, 8,
3658c2ecf20Sopenharmony_ci			       KAWETH_CONTROL_TIMEOUT);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/****************************************************************
3698c2ecf20Sopenharmony_ci *     kaweth_reset
3708c2ecf20Sopenharmony_ci ****************************************************************/
3718c2ecf20Sopenharmony_cistatic int kaweth_reset(struct kaweth_device *kaweth)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	int result;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	result = usb_reset_configuration(kaweth->dev);
3768c2ecf20Sopenharmony_ci	mdelay(10);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return result;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic void kaweth_usb_receive(struct urb *);
3848c2ecf20Sopenharmony_cistatic int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/****************************************************************
3878c2ecf20Sopenharmony_ci	int_callback
3888c2ecf20Sopenharmony_ci*****************************************************************/
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	int status;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	status = usb_submit_urb (kaweth->irq_urb, mf);
3958c2ecf20Sopenharmony_ci	if (unlikely(status == -ENOMEM)) {
3968c2ecf20Sopenharmony_ci		kaweth->suspend_lowmem_ctrl = 1;
3978c2ecf20Sopenharmony_ci		schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
3988c2ecf20Sopenharmony_ci	} else {
3998c2ecf20Sopenharmony_ci		kaweth->suspend_lowmem_ctrl = 0;
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	if (status)
4038c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev,
4048c2ecf20Sopenharmony_ci			"can't resubmit intr, %s-%s, status %d\n",
4058c2ecf20Sopenharmony_ci			kaweth->dev->bus->bus_name,
4068c2ecf20Sopenharmony_ci			kaweth->dev->devpath, status);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic void int_callback(struct urb *u)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = u->context;
4128c2ecf20Sopenharmony_ci	int act_state;
4138c2ecf20Sopenharmony_ci	int status = u->status;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	switch (status) {
4168c2ecf20Sopenharmony_ci	case 0:			/* success */
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	case -ECONNRESET:	/* unlink */
4198c2ecf20Sopenharmony_ci	case -ENOENT:
4208c2ecf20Sopenharmony_ci	case -ESHUTDOWN:
4218c2ecf20Sopenharmony_ci		return;
4228c2ecf20Sopenharmony_ci	/* -EPIPE:  should clear the halt */
4238c2ecf20Sopenharmony_ci	default:		/* error */
4248c2ecf20Sopenharmony_ci		goto resubmit;
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* we check the link state to report changes */
4288c2ecf20Sopenharmony_ci	if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
4298c2ecf20Sopenharmony_ci		if (act_state)
4308c2ecf20Sopenharmony_ci			netif_carrier_on(kaweth->net);
4318c2ecf20Sopenharmony_ci		else
4328c2ecf20Sopenharmony_ci			netif_carrier_off(kaweth->net);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		kaweth->linkstate = act_state;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ciresubmit:
4378c2ecf20Sopenharmony_ci	kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic void kaweth_resubmit_tl(struct work_struct *work)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth =
4438c2ecf20Sopenharmony_ci		container_of(work, struct kaweth_device, lowmem_work.work);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (IS_BLOCKED(kaweth->status))
4468c2ecf20Sopenharmony_ci		return;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (kaweth->suspend_lowmem_rx)
4498c2ecf20Sopenharmony_ci		kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (kaweth->suspend_lowmem_ctrl)
4528c2ecf20Sopenharmony_ci		kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci/****************************************************************
4578c2ecf20Sopenharmony_ci *     kaweth_resubmit_rx_urb
4588c2ecf20Sopenharmony_ci ****************************************************************/
4598c2ecf20Sopenharmony_cistatic int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
4608c2ecf20Sopenharmony_ci						gfp_t mem_flags)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	int result;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(kaweth->rx_urb,
4658c2ecf20Sopenharmony_ci		      kaweth->dev,
4668c2ecf20Sopenharmony_ci		      usb_rcvbulkpipe(kaweth->dev, 1),
4678c2ecf20Sopenharmony_ci		      kaweth->rx_buf,
4688c2ecf20Sopenharmony_ci		      KAWETH_BUF_SIZE,
4698c2ecf20Sopenharmony_ci		      kaweth_usb_receive,
4708c2ecf20Sopenharmony_ci		      kaweth);
4718c2ecf20Sopenharmony_ci	kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
4728c2ecf20Sopenharmony_ci	kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
4758c2ecf20Sopenharmony_ci		if (result == -ENOMEM) {
4768c2ecf20Sopenharmony_ci			kaweth->suspend_lowmem_rx = 1;
4778c2ecf20Sopenharmony_ci			schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
4788c2ecf20Sopenharmony_ci		}
4798c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
4808c2ecf20Sopenharmony_ci			result);
4818c2ecf20Sopenharmony_ci	} else {
4828c2ecf20Sopenharmony_ci		kaweth->suspend_lowmem_rx = 0;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return result;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
4898c2ecf20Sopenharmony_ci				     bool may_sleep);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci/****************************************************************
4928c2ecf20Sopenharmony_ci *     kaweth_usb_receive
4938c2ecf20Sopenharmony_ci ****************************************************************/
4948c2ecf20Sopenharmony_cistatic void kaweth_usb_receive(struct urb *urb)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct device *dev = &urb->dev->dev;
4978c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = urb->context;
4988c2ecf20Sopenharmony_ci	struct net_device *net = kaweth->net;
4998c2ecf20Sopenharmony_ci	int status = urb->status;
5008c2ecf20Sopenharmony_ci	unsigned long flags;
5018c2ecf20Sopenharmony_ci	int count = urb->actual_length;
5028c2ecf20Sopenharmony_ci	int count2 = urb->transfer_buffer_length;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	__u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (unlikely(status == -EPIPE)) {
5098c2ecf20Sopenharmony_ci		net->stats.rx_errors++;
5108c2ecf20Sopenharmony_ci		kaweth->end = 1;
5118c2ecf20Sopenharmony_ci		wake_up(&kaweth->term_wait);
5128c2ecf20Sopenharmony_ci		dev_dbg(dev, "Status was -EPIPE.\n");
5138c2ecf20Sopenharmony_ci		return;
5148c2ecf20Sopenharmony_ci	}
5158c2ecf20Sopenharmony_ci	if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
5168c2ecf20Sopenharmony_ci		/* we are killed - set a flag and wake the disconnect handler */
5178c2ecf20Sopenharmony_ci		kaweth->end = 1;
5188c2ecf20Sopenharmony_ci		wake_up(&kaweth->term_wait);
5198c2ecf20Sopenharmony_ci		dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
5208c2ecf20Sopenharmony_ci		return;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci	if (unlikely(status == -EPROTO || status == -ETIME ||
5238c2ecf20Sopenharmony_ci		     status == -EILSEQ)) {
5248c2ecf20Sopenharmony_ci		net->stats.rx_errors++;
5258c2ecf20Sopenharmony_ci		dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
5268c2ecf20Sopenharmony_ci		return;
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci	if (unlikely(status == -EOVERFLOW)) {
5298c2ecf20Sopenharmony_ci		net->stats.rx_errors++;
5308c2ecf20Sopenharmony_ci		dev_dbg(dev, "Status was -EOVERFLOW.\n");
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci	spin_lock_irqsave(&kaweth->device_lock, flags);
5338c2ecf20Sopenharmony_ci	if (IS_BLOCKED(kaweth->status)) {
5348c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&kaweth->device_lock, flags);
5358c2ecf20Sopenharmony_ci		return;
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&kaweth->device_lock, flags);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	if(status && status != -EREMOTEIO && count != 1) {
5408c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev,
5418c2ecf20Sopenharmony_ci			"%s RX status: %d count: %d packet_len: %d\n",
5428c2ecf20Sopenharmony_ci			net->name, status, count, (int)pkt_len);
5438c2ecf20Sopenharmony_ci		kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
5448c2ecf20Sopenharmony_ci                return;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	if(kaweth->net && (count > 2)) {
5488c2ecf20Sopenharmony_ci		if(pkt_len > (count - 2)) {
5498c2ecf20Sopenharmony_ci			dev_err(&kaweth->intf->dev,
5508c2ecf20Sopenharmony_ci				"Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
5518c2ecf20Sopenharmony_ci				pkt_len, count);
5528c2ecf20Sopenharmony_ci			dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
5538c2ecf20Sopenharmony_ci				pkt_len & 2047);
5548c2ecf20Sopenharmony_ci			dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
5558c2ecf20Sopenharmony_ci		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
5568c2ecf20Sopenharmony_ci                        return;
5578c2ecf20Sopenharmony_ci                }
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci		if(!(skb = dev_alloc_skb(pkt_len+2))) {
5608c2ecf20Sopenharmony_ci		        kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
5618c2ecf20Sopenharmony_ci                        return;
5628c2ecf20Sopenharmony_ci		}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci		skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci		skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci		skb_put(skb, pkt_len);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, net);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		netif_rx(skb);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		net->stats.rx_packets++;
5758c2ecf20Sopenharmony_ci		net->stats.rx_bytes += pkt_len;
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci/****************************************************************
5828c2ecf20Sopenharmony_ci *     kaweth_open
5838c2ecf20Sopenharmony_ci ****************************************************************/
5848c2ecf20Sopenharmony_cistatic int kaweth_open(struct net_device *net)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(net);
5878c2ecf20Sopenharmony_ci	int res;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	res = usb_autopm_get_interface(kaweth->intf);
5908c2ecf20Sopenharmony_ci	if (res) {
5918c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
5928c2ecf20Sopenharmony_ci		return -EIO;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci	res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
5958c2ecf20Sopenharmony_ci	if (res)
5968c2ecf20Sopenharmony_ci		goto err_out;
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	usb_fill_int_urb(
5998c2ecf20Sopenharmony_ci		kaweth->irq_urb,
6008c2ecf20Sopenharmony_ci		kaweth->dev,
6018c2ecf20Sopenharmony_ci		usb_rcvintpipe(kaweth->dev, 3),
6028c2ecf20Sopenharmony_ci		kaweth->intbuffer,
6038c2ecf20Sopenharmony_ci		INTBUFFERSIZE,
6048c2ecf20Sopenharmony_ci		int_callback,
6058c2ecf20Sopenharmony_ci		kaweth,
6068c2ecf20Sopenharmony_ci		250); /* overriding the descriptor */
6078c2ecf20Sopenharmony_ci	kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
6088c2ecf20Sopenharmony_ci	kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
6118c2ecf20Sopenharmony_ci	if (res) {
6128c2ecf20Sopenharmony_ci		usb_kill_urb(kaweth->rx_urb);
6138c2ecf20Sopenharmony_ci		goto err_out;
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci	kaweth->opened = 1;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	netif_start_queue(net);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	kaweth_async_set_rx_mode(kaweth, true);
6208c2ecf20Sopenharmony_ci	return 0;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cierr_out:
6238c2ecf20Sopenharmony_ci	usb_autopm_put_interface(kaweth->intf);
6248c2ecf20Sopenharmony_ci	return -EIO;
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci/****************************************************************
6288c2ecf20Sopenharmony_ci *     kaweth_kill_urbs
6298c2ecf20Sopenharmony_ci ****************************************************************/
6308c2ecf20Sopenharmony_cistatic void kaweth_kill_urbs(struct kaweth_device *kaweth)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	usb_kill_urb(kaweth->irq_urb);
6338c2ecf20Sopenharmony_ci	usb_kill_urb(kaweth->rx_urb);
6348c2ecf20Sopenharmony_ci	usb_kill_urb(kaweth->tx_urb);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&kaweth->lowmem_work);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* a scheduled work may have resubmitted,
6398c2ecf20Sopenharmony_ci	   we hit them again */
6408c2ecf20Sopenharmony_ci	usb_kill_urb(kaweth->irq_urb);
6418c2ecf20Sopenharmony_ci	usb_kill_urb(kaweth->rx_urb);
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci/****************************************************************
6458c2ecf20Sopenharmony_ci *     kaweth_close
6468c2ecf20Sopenharmony_ci ****************************************************************/
6478c2ecf20Sopenharmony_cistatic int kaweth_close(struct net_device *net)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(net);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	netif_stop_queue(net);
6528c2ecf20Sopenharmony_ci	kaweth->opened = 0;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	kaweth->status |= KAWETH_STATUS_CLOSING;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	kaweth_kill_urbs(kaweth);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	kaweth->status &= ~KAWETH_STATUS_CLOSING;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	usb_autopm_put_interface(kaweth->intf);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	return 0;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic u32 kaweth_get_link(struct net_device *dev)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(dev);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	return kaweth->linkstate;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic const struct ethtool_ops ops = {
6738c2ecf20Sopenharmony_ci	.get_link	= kaweth_get_link
6748c2ecf20Sopenharmony_ci};
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci/****************************************************************
6778c2ecf20Sopenharmony_ci *     kaweth_usb_transmit_complete
6788c2ecf20Sopenharmony_ci ****************************************************************/
6798c2ecf20Sopenharmony_cistatic void kaweth_usb_transmit_complete(struct urb *urb)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = urb->context;
6828c2ecf20Sopenharmony_ci	struct sk_buff *skb = kaweth->tx_skb;
6838c2ecf20Sopenharmony_ci	int status = urb->status;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (unlikely(status != 0))
6868c2ecf20Sopenharmony_ci		if (status != -ENOENT)
6878c2ecf20Sopenharmony_ci			dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
6888c2ecf20Sopenharmony_ci				kaweth->net->name, status);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	netif_wake_queue(kaweth->net);
6918c2ecf20Sopenharmony_ci	dev_kfree_skb_irq(skb);
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci/****************************************************************
6958c2ecf20Sopenharmony_ci *     kaweth_start_xmit
6968c2ecf20Sopenharmony_ci ****************************************************************/
6978c2ecf20Sopenharmony_cistatic netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
6988c2ecf20Sopenharmony_ci					   struct net_device *net)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(net);
7018c2ecf20Sopenharmony_ci	__le16 *private_header;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	int res;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	spin_lock_irq(&kaweth->device_lock);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	kaweth_async_set_rx_mode(kaweth, false);
7088c2ecf20Sopenharmony_ci	netif_stop_queue(net);
7098c2ecf20Sopenharmony_ci	if (IS_BLOCKED(kaweth->status)) {
7108c2ecf20Sopenharmony_ci		goto skip;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	/* We now decide whether we can put our special header into the sk_buff */
7148c2ecf20Sopenharmony_ci	if (skb_cow_head(skb, 2)) {
7158c2ecf20Sopenharmony_ci		net->stats.tx_errors++;
7168c2ecf20Sopenharmony_ci		netif_start_queue(net);
7178c2ecf20Sopenharmony_ci		spin_unlock_irq(&kaweth->device_lock);
7188c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
7198c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
7208c2ecf20Sopenharmony_ci	}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	private_header = __skb_push(skb, 2);
7238c2ecf20Sopenharmony_ci	*private_header = cpu_to_le16(skb->len-2);
7248c2ecf20Sopenharmony_ci	kaweth->tx_skb = skb;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(kaweth->tx_urb,
7278c2ecf20Sopenharmony_ci		      kaweth->dev,
7288c2ecf20Sopenharmony_ci		      usb_sndbulkpipe(kaweth->dev, 2),
7298c2ecf20Sopenharmony_ci		      private_header,
7308c2ecf20Sopenharmony_ci		      skb->len,
7318c2ecf20Sopenharmony_ci		      kaweth_usb_transmit_complete,
7328c2ecf20Sopenharmony_ci		      kaweth);
7338c2ecf20Sopenharmony_ci	kaweth->end = 0;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
7368c2ecf20Sopenharmony_ci	{
7378c2ecf20Sopenharmony_ci		dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
7388c2ecf20Sopenharmony_ciskip:
7398c2ecf20Sopenharmony_ci		net->stats.tx_errors++;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci		netif_start_queue(net);
7428c2ecf20Sopenharmony_ci		dev_kfree_skb_irq(skb);
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci	else
7458c2ecf20Sopenharmony_ci	{
7468c2ecf20Sopenharmony_ci		net->stats.tx_packets++;
7478c2ecf20Sopenharmony_ci		net->stats.tx_bytes += skb->len;
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	spin_unlock_irq(&kaweth->device_lock);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci/****************************************************************
7568c2ecf20Sopenharmony_ci *     kaweth_set_rx_mode
7578c2ecf20Sopenharmony_ci ****************************************************************/
7588c2ecf20Sopenharmony_cistatic void kaweth_set_rx_mode(struct net_device *net)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(net);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	__u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
7638c2ecf20Sopenharmony_ci                                     KAWETH_PACKET_FILTER_BROADCAST |
7648c2ecf20Sopenharmony_ci		                     KAWETH_PACKET_FILTER_MULTICAST;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	netif_stop_queue(net);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	if (net->flags & IFF_PROMISC) {
7718c2ecf20Sopenharmony_ci		packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci	else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
7748c2ecf20Sopenharmony_ci		packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	kaweth->packet_filter_bitmap = packet_filter_bitmap;
7788c2ecf20Sopenharmony_ci	netif_wake_queue(net);
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci/****************************************************************
7828c2ecf20Sopenharmony_ci *     kaweth_async_set_rx_mode
7838c2ecf20Sopenharmony_ci ****************************************************************/
7848c2ecf20Sopenharmony_cistatic void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
7858c2ecf20Sopenharmony_ci				     bool may_sleep)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	int ret;
7888c2ecf20Sopenharmony_ci	__u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	kaweth->packet_filter_bitmap = 0;
7918c2ecf20Sopenharmony_ci	if (packet_filter_bitmap == 0)
7928c2ecf20Sopenharmony_ci		return;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (!may_sleep)
7958c2ecf20Sopenharmony_ci		return;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	ret = usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
7988c2ecf20Sopenharmony_ci			      KAWETH_COMMAND_SET_PACKET_FILTER,
7998c2ecf20Sopenharmony_ci			      USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
8008c2ecf20Sopenharmony_ci			      packet_filter_bitmap, 0,
8018c2ecf20Sopenharmony_ci			      &kaweth->scratch, 0,
8028c2ecf20Sopenharmony_ci			      KAWETH_CONTROL_TIMEOUT);
8038c2ecf20Sopenharmony_ci	if (ret < 0)
8048c2ecf20Sopenharmony_ci		dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
8058c2ecf20Sopenharmony_ci			ret);
8068c2ecf20Sopenharmony_ci	else
8078c2ecf20Sopenharmony_ci		netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
8088c2ecf20Sopenharmony_ci			   packet_filter_bitmap);
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci/****************************************************************
8128c2ecf20Sopenharmony_ci *     kaweth_tx_timeout
8138c2ecf20Sopenharmony_ci ****************************************************************/
8148c2ecf20Sopenharmony_cistatic void kaweth_tx_timeout(struct net_device *net, unsigned int txqueue)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = netdev_priv(net);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
8198c2ecf20Sopenharmony_ci	net->stats.tx_errors++;
8208c2ecf20Sopenharmony_ci	netif_trans_update(net);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	usb_unlink_urb(kaweth->tx_urb);
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci/****************************************************************
8268c2ecf20Sopenharmony_ci *     kaweth_suspend
8278c2ecf20Sopenharmony_ci ****************************************************************/
8288c2ecf20Sopenharmony_cistatic int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
8298c2ecf20Sopenharmony_ci{
8308c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = usb_get_intfdata(intf);
8318c2ecf20Sopenharmony_ci	unsigned long flags;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	spin_lock_irqsave(&kaweth->device_lock, flags);
8348c2ecf20Sopenharmony_ci	kaweth->status |= KAWETH_STATUS_SUSPENDING;
8358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&kaweth->device_lock, flags);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	kaweth_kill_urbs(kaweth);
8388c2ecf20Sopenharmony_ci	return 0;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci/****************************************************************
8428c2ecf20Sopenharmony_ci *     kaweth_resume
8438c2ecf20Sopenharmony_ci ****************************************************************/
8448c2ecf20Sopenharmony_cistatic int kaweth_resume(struct usb_interface *intf)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = usb_get_intfdata(intf);
8478c2ecf20Sopenharmony_ci	unsigned long flags;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	spin_lock_irqsave(&kaweth->device_lock, flags);
8508c2ecf20Sopenharmony_ci	kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
8518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&kaweth->device_lock, flags);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	if (!kaweth->opened)
8548c2ecf20Sopenharmony_ci		return 0;
8558c2ecf20Sopenharmony_ci	kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
8568c2ecf20Sopenharmony_ci	kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	return 0;
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci/****************************************************************
8628c2ecf20Sopenharmony_ci *     kaweth_probe
8638c2ecf20Sopenharmony_ci ****************************************************************/
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_cistatic const struct net_device_ops kaweth_netdev_ops = {
8678c2ecf20Sopenharmony_ci	.ndo_open =			kaweth_open,
8688c2ecf20Sopenharmony_ci	.ndo_stop =			kaweth_close,
8698c2ecf20Sopenharmony_ci	.ndo_start_xmit =		kaweth_start_xmit,
8708c2ecf20Sopenharmony_ci	.ndo_tx_timeout =		kaweth_tx_timeout,
8718c2ecf20Sopenharmony_ci	.ndo_set_rx_mode =		kaweth_set_rx_mode,
8728c2ecf20Sopenharmony_ci	.ndo_set_mac_address =		eth_mac_addr,
8738c2ecf20Sopenharmony_ci	.ndo_validate_addr =		eth_validate_addr,
8748c2ecf20Sopenharmony_ci};
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cistatic int kaweth_probe(
8778c2ecf20Sopenharmony_ci		struct usb_interface *intf,
8788c2ecf20Sopenharmony_ci		const struct usb_device_id *id      /* from id_table */
8798c2ecf20Sopenharmony_ci	)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	struct device *dev = &intf->dev;
8828c2ecf20Sopenharmony_ci	struct usb_device *udev = interface_to_usbdev(intf);
8838c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth;
8848c2ecf20Sopenharmony_ci	struct net_device *netdev;
8858c2ecf20Sopenharmony_ci	const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
8868c2ecf20Sopenharmony_ci	int result = 0;
8878c2ecf20Sopenharmony_ci	int rv = -EIO;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	dev_dbg(dev,
8908c2ecf20Sopenharmony_ci		"Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
8918c2ecf20Sopenharmony_ci		udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
8928c2ecf20Sopenharmony_ci		le16_to_cpu(udev->descriptor.idProduct),
8938c2ecf20Sopenharmony_ci		le16_to_cpu(udev->descriptor.bcdDevice));
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	dev_dbg(dev, "Device at %p\n", udev);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	dev_dbg(dev, "Descriptor length: %x type: %x\n",
8988c2ecf20Sopenharmony_ci		(int)udev->descriptor.bLength,
8998c2ecf20Sopenharmony_ci		(int)udev->descriptor.bDescriptorType);
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	netdev = alloc_etherdev(sizeof(*kaweth));
9028c2ecf20Sopenharmony_ci	if (!netdev)
9038c2ecf20Sopenharmony_ci		return -ENOMEM;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	kaweth = netdev_priv(netdev);
9068c2ecf20Sopenharmony_ci	kaweth->dev = udev;
9078c2ecf20Sopenharmony_ci	kaweth->net = netdev;
9088c2ecf20Sopenharmony_ci	kaweth->intf = intf;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	spin_lock_init(&kaweth->device_lock);
9118c2ecf20Sopenharmony_ci	init_waitqueue_head(&kaweth->term_wait);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	dev_dbg(dev, "Resetting.\n");
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	kaweth_reset(kaweth);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	/*
9188c2ecf20Sopenharmony_ci	 * If high byte of bcdDevice is nonzero, firmware is already
9198c2ecf20Sopenharmony_ci	 * downloaded. Don't try to do it again, or we'll hang the device.
9208c2ecf20Sopenharmony_ci	 */
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
9238c2ecf20Sopenharmony_ci		dev_info(dev, "Firmware present in device.\n");
9248c2ecf20Sopenharmony_ci	} else {
9258c2ecf20Sopenharmony_ci		/* Download the firmware */
9268c2ecf20Sopenharmony_ci		dev_info(dev, "Downloading firmware...\n");
9278c2ecf20Sopenharmony_ci		kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
9288c2ecf20Sopenharmony_ci		if (!kaweth->firmware_buf) {
9298c2ecf20Sopenharmony_ci			rv = -ENOMEM;
9308c2ecf20Sopenharmony_ci			goto err_free_netdev;
9318c2ecf20Sopenharmony_ci		}
9328c2ecf20Sopenharmony_ci		if ((result = kaweth_download_firmware(kaweth,
9338c2ecf20Sopenharmony_ci						      "kaweth/new_code.bin",
9348c2ecf20Sopenharmony_ci						      100,
9358c2ecf20Sopenharmony_ci						      2)) < 0) {
9368c2ecf20Sopenharmony_ci			dev_err(dev, "Error downloading firmware (%d)\n",
9378c2ecf20Sopenharmony_ci				result);
9388c2ecf20Sopenharmony_ci			goto err_fw;
9398c2ecf20Sopenharmony_ci		}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci		if ((result = kaweth_download_firmware(kaweth,
9428c2ecf20Sopenharmony_ci						      "kaweth/new_code_fix.bin",
9438c2ecf20Sopenharmony_ci						      100,
9448c2ecf20Sopenharmony_ci						      3)) < 0) {
9458c2ecf20Sopenharmony_ci			dev_err(dev, "Error downloading firmware fix (%d)\n",
9468c2ecf20Sopenharmony_ci				result);
9478c2ecf20Sopenharmony_ci			goto err_fw;
9488c2ecf20Sopenharmony_ci		}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci		if ((result = kaweth_download_firmware(kaweth,
9518c2ecf20Sopenharmony_ci						      "kaweth/trigger_code.bin",
9528c2ecf20Sopenharmony_ci						      126,
9538c2ecf20Sopenharmony_ci						      2)) < 0) {
9548c2ecf20Sopenharmony_ci			dev_err(dev, "Error downloading trigger code (%d)\n",
9558c2ecf20Sopenharmony_ci				result);
9568c2ecf20Sopenharmony_ci			goto err_fw;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci		}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci		if ((result = kaweth_download_firmware(kaweth,
9618c2ecf20Sopenharmony_ci						      "kaweth/trigger_code_fix.bin",
9628c2ecf20Sopenharmony_ci						      126,
9638c2ecf20Sopenharmony_ci						      3)) < 0) {
9648c2ecf20Sopenharmony_ci			dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
9658c2ecf20Sopenharmony_ci			goto err_fw;
9668c2ecf20Sopenharmony_ci		}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci		if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
9708c2ecf20Sopenharmony_ci			dev_err(dev, "Error triggering firmware (%d)\n", result);
9718c2ecf20Sopenharmony_ci			goto err_fw;
9728c2ecf20Sopenharmony_ci		}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci		/* Device will now disappear for a moment...  */
9758c2ecf20Sopenharmony_ci		dev_info(dev, "Firmware loaded.  I'll be back...\n");
9768c2ecf20Sopenharmony_cierr_fw:
9778c2ecf20Sopenharmony_ci		free_page((unsigned long)kaweth->firmware_buf);
9788c2ecf20Sopenharmony_ci		free_netdev(netdev);
9798c2ecf20Sopenharmony_ci		return -EIO;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	result = kaweth_read_configuration(kaweth);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	if(result < 0) {
9858c2ecf20Sopenharmony_ci		dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
9868c2ecf20Sopenharmony_ci		goto err_free_netdev;
9878c2ecf20Sopenharmony_ci	}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
9908c2ecf20Sopenharmony_ci	dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
9918c2ecf20Sopenharmony_ci	dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
9928c2ecf20Sopenharmony_ci	dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	if(!memcmp(&kaweth->configuration.hw_addr,
9958c2ecf20Sopenharmony_ci                   &bcast_addr,
9968c2ecf20Sopenharmony_ci		   sizeof(bcast_addr))) {
9978c2ecf20Sopenharmony_ci		dev_err(dev, "Firmware not functioning properly, no net device created\n");
9988c2ecf20Sopenharmony_ci		goto err_free_netdev;
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
10028c2ecf20Sopenharmony_ci		dev_dbg(dev, "Error setting URB size\n");
10038c2ecf20Sopenharmony_ci		goto err_free_netdev;
10048c2ecf20Sopenharmony_ci	}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
10078c2ecf20Sopenharmony_ci		dev_err(dev, "Error setting SOFS wait\n");
10088c2ecf20Sopenharmony_ci		goto err_free_netdev;
10098c2ecf20Sopenharmony_ci	}
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	result = kaweth_set_receive_filter(kaweth,
10128c2ecf20Sopenharmony_ci                                           KAWETH_PACKET_FILTER_DIRECTED |
10138c2ecf20Sopenharmony_ci                                           KAWETH_PACKET_FILTER_BROADCAST |
10148c2ecf20Sopenharmony_ci                                           KAWETH_PACKET_FILTER_MULTICAST);
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	if(result < 0) {
10178c2ecf20Sopenharmony_ci		dev_err(dev, "Error setting receive filter\n");
10188c2ecf20Sopenharmony_ci		goto err_free_netdev;
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	dev_dbg(dev, "Initializing net device.\n");
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
10248c2ecf20Sopenharmony_ci	if (!kaweth->tx_urb)
10258c2ecf20Sopenharmony_ci		goto err_free_netdev;
10268c2ecf20Sopenharmony_ci	kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
10278c2ecf20Sopenharmony_ci	if (!kaweth->rx_urb)
10288c2ecf20Sopenharmony_ci		goto err_only_tx;
10298c2ecf20Sopenharmony_ci	kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
10308c2ecf20Sopenharmony_ci	if (!kaweth->irq_urb)
10318c2ecf20Sopenharmony_ci		goto err_tx_and_rx;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	kaweth->intbuffer = usb_alloc_coherent(	kaweth->dev,
10348c2ecf20Sopenharmony_ci						INTBUFFERSIZE,
10358c2ecf20Sopenharmony_ci						GFP_KERNEL,
10368c2ecf20Sopenharmony_ci						&kaweth->intbufferhandle);
10378c2ecf20Sopenharmony_ci	if (!kaweth->intbuffer)
10388c2ecf20Sopenharmony_ci		goto err_tx_and_rx_and_irq;
10398c2ecf20Sopenharmony_ci	kaweth->rx_buf = usb_alloc_coherent(	kaweth->dev,
10408c2ecf20Sopenharmony_ci						KAWETH_BUF_SIZE,
10418c2ecf20Sopenharmony_ci						GFP_KERNEL,
10428c2ecf20Sopenharmony_ci						&kaweth->rxbufferhandle);
10438c2ecf20Sopenharmony_ci	if (!kaweth->rx_buf)
10448c2ecf20Sopenharmony_ci		goto err_all_but_rxbuf;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
10478c2ecf20Sopenharmony_ci	memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
10488c2ecf20Sopenharmony_ci               sizeof(kaweth->configuration.hw_addr));
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	netdev->netdev_ops = &kaweth_netdev_ops;
10518c2ecf20Sopenharmony_ci	netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
10528c2ecf20Sopenharmony_ci	netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
10538c2ecf20Sopenharmony_ci	netdev->ethtool_ops = &ops;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	/* kaweth is zeroed as part of alloc_netdev */
10568c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
10578c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, kaweth);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(netdev, dev);
10608c2ecf20Sopenharmony_ci	if (register_netdev(netdev) != 0) {
10618c2ecf20Sopenharmony_ci		dev_err(dev, "Error registering netdev.\n");
10628c2ecf20Sopenharmony_ci		goto err_intfdata;
10638c2ecf20Sopenharmony_ci	}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	dev_info(dev, "kaweth interface created at %s\n",
10668c2ecf20Sopenharmony_ci		 kaweth->net->name);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	return 0;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cierr_intfdata:
10718c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
10728c2ecf20Sopenharmony_ci	usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
10738c2ecf20Sopenharmony_cierr_all_but_rxbuf:
10748c2ecf20Sopenharmony_ci	usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
10758c2ecf20Sopenharmony_cierr_tx_and_rx_and_irq:
10768c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->irq_urb);
10778c2ecf20Sopenharmony_cierr_tx_and_rx:
10788c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->rx_urb);
10798c2ecf20Sopenharmony_cierr_only_tx:
10808c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->tx_urb);
10818c2ecf20Sopenharmony_cierr_free_netdev:
10828c2ecf20Sopenharmony_ci	free_netdev(netdev);
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	return rv;
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci/****************************************************************
10888c2ecf20Sopenharmony_ci *     kaweth_disconnect
10898c2ecf20Sopenharmony_ci ****************************************************************/
10908c2ecf20Sopenharmony_cistatic void kaweth_disconnect(struct usb_interface *intf)
10918c2ecf20Sopenharmony_ci{
10928c2ecf20Sopenharmony_ci	struct kaweth_device *kaweth = usb_get_intfdata(intf);
10938c2ecf20Sopenharmony_ci	struct net_device *netdev;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
10968c2ecf20Sopenharmony_ci	if (!kaweth) {
10978c2ecf20Sopenharmony_ci		dev_warn(&intf->dev, "unregistering non-existent device\n");
10988c2ecf20Sopenharmony_ci		return;
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci	netdev = kaweth->net;
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	netdev_dbg(kaweth->net, "Unregistering net device\n");
11038c2ecf20Sopenharmony_ci	unregister_netdev(netdev);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->rx_urb);
11068c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->tx_urb);
11078c2ecf20Sopenharmony_ci	usb_free_urb(kaweth->irq_urb);
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
11108c2ecf20Sopenharmony_ci	usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	free_netdev(netdev);
11138c2ecf20Sopenharmony_ci}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_cimodule_usb_driver(kaweth_driver);
1117