blob: b8baf085851542f59d999c3172943659f9e4265b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/****************************************************************
2 *
3 * kaweth.c - driver for KL5KUSB101 based USB->Ethernet
4 *
5 * (c) 2000 Interlan Communications
6 * (c) 2000 Stephane Alnet
7 * (C) 2001 Brad Hards
8 * (C) 2002 Oliver Neukum
9 *
10 * Original author: The Zapman <zapman@interlan.net>
11 * Inspired by, and much credit goes to Michael Rothwell
12 * <rothwell@interlan.net> for the test equipment, help, and patience
13 * Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
14 * Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
15 * for providing the firmware and driver resources.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 *
31 ****************************************************************/
32
33/* TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Develop test procedures for USB net interfaces
35 * Run test procedures
36 * Fix bugs from previous two steps
37 * Snoop other OSs for any tricks we're not doing
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Reduce arbitrary timeouts
39 * Smart multicast support
40 * Temporary MAC change support
41 * Tunable SOFs parameter - ioctl()?
42 * Ethernet stats collection
43 * Code formatting improvements
44 */
45
46#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/slab.h>
48#include <linux/string.h>
49#include <linux/init.h>
50#include <linux/delay.h>
51#include <linux/netdevice.h>
52#include <linux/etherdevice.h>
53#include <linux/usb.h>
54#include <linux/types.h>
55#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/dma-mapping.h>
57#include <linux/wait.h>
David Woodhouse79682492008-05-29 17:17:17 +030058#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/byteorder.h>
61
62#undef DEBUG
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define KAWETH_MTU 1514
65#define KAWETH_BUF_SIZE 1664
66#define KAWETH_TX_TIMEOUT (5 * HZ)
67#define KAWETH_SCRATCH_SIZE 32
68#define KAWETH_FIRMWARE_BUF_SIZE 4096
Russ Dill2b2b2e32008-01-07 21:48:12 -080069#define KAWETH_CONTROL_TIMEOUT (30000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define KAWETH_STATUS_BROKEN 0x0000001
72#define KAWETH_STATUS_CLOSING 0x0000002
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +020073#define KAWETH_STATUS_SUSPENDING 0x0000004
74
75#define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
78#define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
79#define KAWETH_PACKET_FILTER_DIRECTED 0x04
80#define KAWETH_PACKET_FILTER_BROADCAST 0x08
81#define KAWETH_PACKET_FILTER_MULTICAST 0x10
82
83/* Table 7 */
84#define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
85#define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
86#define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
87#define KAWETH_COMMAND_STATISTICS 0x03
88#define KAWETH_COMMAND_SET_TEMP_MAC 0x06
89#define KAWETH_COMMAND_GET_TEMP_MAC 0x07
90#define KAWETH_COMMAND_SET_URB_SIZE 0x08
91#define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
92#define KAWETH_COMMAND_SCAN 0xFF
93
94#define KAWETH_SOFS_TO_WAIT 0x05
95
96#define INTBUFFERSIZE 4
97
98#define STATE_OFFSET 0
99#define STATE_MASK 0x40
100#define STATE_SHIFT 5
101
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200102#define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
106MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
107MODULE_LICENSE("GPL");
David Woodhouse79682492008-05-29 17:17:17 +0300108MODULE_FIRMWARE("kaweth/new_code.bin");
109MODULE_FIRMWARE("kaweth/new_code_fix.bin");
110MODULE_FIRMWARE("kaweth/trigger_code.bin");
111MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static const char driver_name[] = "kaweth";
114
115static int kaweth_probe(
116 struct usb_interface *intf,
117 const struct usb_device_id *id /* from id_table */
118 );
119static void kaweth_disconnect(struct usb_interface *intf);
120static int kaweth_internal_control_msg(struct usb_device *usb_dev,
121 unsigned int pipe,
122 struct usb_ctrlrequest *cmd, void *data,
123 int len, int timeout);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200124static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
125static int kaweth_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/****************************************************************
128 * usb_device_id
129 ****************************************************************/
130static struct usb_device_id usb_klsi_table[] = {
131 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
132 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
133 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
134 { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
135 { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
136 { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
137 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
138 { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
139 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
140 { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
141 { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
142 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
143 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
144 { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
145 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
146 { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
147 { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
Andreas Hartmanndacf4fc2010-04-27 14:39:33 -0700148 { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
150 { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
151 { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
152 { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
153 { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
154 { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
155 { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
156 { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
157 { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
158 { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
159 { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
160 { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
161 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
162 { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
163 { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
Oliver Neukum486ba2a2006-09-28 22:21:19 +0200164 { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
166 {} /* Null terminator */
167};
168
169MODULE_DEVICE_TABLE (usb, usb_klsi_table);
170
171/****************************************************************
172 * kaweth_driver
173 ****************************************************************/
174static struct usb_driver kaweth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .name = driver_name,
176 .probe = kaweth_probe,
177 .disconnect = kaweth_disconnect,
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200178 .suspend = kaweth_suspend,
179 .resume = kaweth_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .id_table = usb_klsi_table,
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100181 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
184typedef __u8 eth_addr_t[6];
185
186/****************************************************************
187 * usb_eth_dev
188 ****************************************************************/
189struct usb_eth_dev {
190 char *name;
191 __u16 vendor;
192 __u16 device;
193 void *pdata;
194};
195
196/****************************************************************
197 * kaweth_ethernet_configuration
198 * Refer Table 8
199 ****************************************************************/
200struct kaweth_ethernet_configuration
201{
202 __u8 size;
203 __u8 reserved1;
204 __u8 reserved2;
205 eth_addr_t hw_addr;
206 __u32 statistics_mask;
207 __le16 segment_size;
208 __u16 max_multicast_filters;
209 __u8 reserved3;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000210} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/****************************************************************
213 * kaweth_device
214 ****************************************************************/
215struct kaweth_device
216{
217 spinlock_t device_lock;
218
219 __u32 status;
220 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int suspend_lowmem_rx;
222 int suspend_lowmem_ctrl;
223 int linkstate;
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200224 int opened;
David Howellsc4028952006-11-22 14:57:56 +0000225 struct delayed_work lowmem_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 struct usb_device *dev;
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100228 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct net_device *net;
230 wait_queue_head_t term_wait;
231
232 struct urb *rx_urb;
233 struct urb *tx_urb;
234 struct urb *irq_urb;
235
236 dma_addr_t intbufferhandle;
237 __u8 *intbuffer;
238 dma_addr_t rxbufferhandle;
239 __u8 *rx_buf;
240
241
242 struct sk_buff *tx_skb;
243
244 __u8 *firmware_buf;
245 __u8 scratch[KAWETH_SCRATCH_SIZE];
246 __u16 packet_filter_bitmap;
247
248 struct kaweth_ethernet_configuration configuration;
249
250 struct net_device_stats stats;
251};
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/****************************************************************
254 * kaweth_control
255 ****************************************************************/
256static int kaweth_control(struct kaweth_device *kaweth,
257 unsigned int pipe,
258 __u8 request,
259 __u8 requesttype,
260 __u16 value,
261 __u16 index,
262 void *data,
263 __u16 size,
264 int timeout)
265{
266 struct usb_ctrlrequest *dr;
Kevin Cernekee051b9822009-09-19 11:18:21 +0000267 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200269 dbg("kaweth_control()");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if(in_interrupt()) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200272 dbg("in_interrupt()");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EBUSY;
274 }
275
276 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
277
278 if (!dr) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200279 dbg("kmalloc() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENOMEM;
281 }
282
Kevin Cernekee051b9822009-09-19 11:18:21 +0000283 dr->bRequestType = requesttype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 dr->bRequest = request;
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700285 dr->wValue = cpu_to_le16(value);
286 dr->wIndex = cpu_to_le16(index);
287 dr->wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kevin Cernekee051b9822009-09-19 11:18:21 +0000289 retval = kaweth_internal_control_msg(kaweth->dev,
290 pipe,
291 dr,
292 data,
293 size,
294 timeout);
295
296 kfree(dr);
297 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/****************************************************************
301 * kaweth_read_configuration
302 ****************************************************************/
303static int kaweth_read_configuration(struct kaweth_device *kaweth)
304{
305 int retval;
306
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200307 dbg("Reading kaweth configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 retval = kaweth_control(kaweth,
310 usb_rcvctrlpipe(kaweth->dev, 0),
311 KAWETH_COMMAND_GET_ETHERNET_DESC,
312 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
313 0,
314 0,
315 (void *)&kaweth->configuration,
316 sizeof(kaweth->configuration),
317 KAWETH_CONTROL_TIMEOUT);
318
319 return retval;
320}
321
322/****************************************************************
323 * kaweth_set_urb_size
324 ****************************************************************/
325static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
326{
327 int retval;
328
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200329 dbg("Setting URB size to %d", (unsigned)urb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 retval = kaweth_control(kaweth,
332 usb_sndctrlpipe(kaweth->dev, 0),
333 KAWETH_COMMAND_SET_URB_SIZE,
334 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
335 urb_size,
336 0,
337 (void *)&kaweth->scratch,
338 0,
339 KAWETH_CONTROL_TIMEOUT);
340
341 return retval;
342}
343
344/****************************************************************
345 * kaweth_set_sofs_wait
346 ****************************************************************/
347static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
348{
349 int retval;
350
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200351 dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 retval = kaweth_control(kaweth,
354 usb_sndctrlpipe(kaweth->dev, 0),
355 KAWETH_COMMAND_SET_SOFS_WAIT,
356 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
357 sofs_wait,
358 0,
359 (void *)&kaweth->scratch,
360 0,
361 KAWETH_CONTROL_TIMEOUT);
362
363 return retval;
364}
365
366/****************************************************************
367 * kaweth_set_receive_filter
368 ****************************************************************/
369static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
370 __u16 receive_filter)
371{
372 int retval;
373
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200374 dbg("Set receive filter to %d", (unsigned)receive_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 retval = kaweth_control(kaweth,
377 usb_sndctrlpipe(kaweth->dev, 0),
378 KAWETH_COMMAND_SET_PACKET_FILTER,
379 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
380 receive_filter,
381 0,
382 (void *)&kaweth->scratch,
383 0,
384 KAWETH_CONTROL_TIMEOUT);
385
386 return retval;
387}
388
389/****************************************************************
390 * kaweth_download_firmware
391 ****************************************************************/
392static int kaweth_download_firmware(struct kaweth_device *kaweth,
David Woodhouse79682492008-05-29 17:17:17 +0300393 const char *fwname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 __u8 interrupt,
395 __u8 type)
396{
David Woodhouse79682492008-05-29 17:17:17 +0300397 const struct firmware *fw;
398 int data_len;
399 int ret;
400
401 ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
402 if (ret) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700403 dev_err(&kaweth->intf->dev, "Firmware request failed\n");
David Woodhouse79682492008-05-29 17:17:17 +0300404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
David Woodhouse79682492008-05-29 17:17:17 +0300407 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700408 dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
409 fw->size);
Jesper Juhl986e3f62011-01-23 12:19:55 +0000410 release_firmware(fw);
David Woodhouse79682492008-05-29 17:17:17 +0300411 return -ENOSPC;
412 }
413 data_len = fw->size;
414 memcpy(kaweth->firmware_buf, fw->data, fw->size);
415
416 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
419 kaweth->firmware_buf[3] = data_len >> 8;
420 kaweth->firmware_buf[4] = type;
421 kaweth->firmware_buf[5] = interrupt;
422
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200423 dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 kaweth->firmware_buf[2]);
425
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200426 dbg("Downloading firmware at %p to kaweth device at %p",
David Woodhouse79682492008-05-29 17:17:17 +0300427 fw->data, kaweth);
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200428 dbg("Firmware length: %d", data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return kaweth_control(kaweth,
431 usb_sndctrlpipe(kaweth->dev, 0),
432 KAWETH_COMMAND_SCAN,
433 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
434 0,
435 0,
436 (void *)kaweth->firmware_buf,
437 data_len,
438 KAWETH_CONTROL_TIMEOUT);
439}
440
441/****************************************************************
442 * kaweth_trigger_firmware
443 ****************************************************************/
444static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
445 __u8 interrupt)
446{
447 kaweth->firmware_buf[0] = 0xB6;
448 kaweth->firmware_buf[1] = 0xC3;
449 kaweth->firmware_buf[2] = 0x01;
450 kaweth->firmware_buf[3] = 0x00;
451 kaweth->firmware_buf[4] = 0x06;
452 kaweth->firmware_buf[5] = interrupt;
453 kaweth->firmware_buf[6] = 0x00;
454 kaweth->firmware_buf[7] = 0x00;
455
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200456 dbg("Triggering firmware");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return kaweth_control(kaweth,
459 usb_sndctrlpipe(kaweth->dev, 0),
460 KAWETH_COMMAND_SCAN,
461 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
462 0,
463 0,
464 (void *)kaweth->firmware_buf,
465 8,
466 KAWETH_CONTROL_TIMEOUT);
467}
468
469/****************************************************************
470 * kaweth_reset
471 ****************************************************************/
472static int kaweth_reset(struct kaweth_device *kaweth)
473{
474 int result;
475
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200476 dbg("kaweth_reset(%p)", kaweth);
Oliver Neukumf0816ce2009-10-29 05:07:12 +0000477 result = usb_reset_configuration(kaweth->dev);
Guillaume GOURAT /f2d45cd2005-10-21 14:01:35 +0200478 mdelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200480 dbg("kaweth_reset() returns %d.",result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return result;
483}
484
David Howells7d12e782006-10-05 14:55:46 +0100485static void kaweth_usb_receive(struct urb *);
Al Viro55016f12005-10-21 03:21:58 -0400486static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/****************************************************************
489 int_callback
490*****************************************************************/
491
Al Viro55016f12005-10-21 03:21:58 -0400492static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int status;
495
496 status = usb_submit_urb (kaweth->irq_urb, mf);
497 if (unlikely(status == -ENOMEM)) {
498 kaweth->suspend_lowmem_ctrl = 1;
499 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
500 } else {
501 kaweth->suspend_lowmem_ctrl = 0;
502 }
503
504 if (status)
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700505 dev_err(&kaweth->intf->dev,
506 "can't resubmit intr, %s-%s, status %d\n",
507 kaweth->dev->bus->bus_name,
508 kaweth->dev->devpath, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
David Howells7d12e782006-10-05 14:55:46 +0100511static void int_callback(struct urb *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct kaweth_device *kaweth = u->context;
514 int act_state;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800515 int status = u->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Oliver Neukumc94cb312008-12-18 23:00:59 -0800517 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 case 0: /* success */
519 break;
520 case -ECONNRESET: /* unlink */
521 case -ENOENT:
522 case -ESHUTDOWN:
523 return;
524 /* -EPIPE: should clear the halt */
525 default: /* error */
526 goto resubmit;
527 }
528
529 /* we check the link state to report changes */
530 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
Oliver Neukum58125f92005-06-15 22:26:38 -0700531 if (act_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 netif_carrier_on(kaweth->net);
533 else
534 netif_carrier_off(kaweth->net);
535
536 kaweth->linkstate = act_state;
537 }
538resubmit:
539 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
540}
541
David Howellsc4028952006-11-22 14:57:56 +0000542static void kaweth_resubmit_tl(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
David Howellsc4028952006-11-22 14:57:56 +0000544 struct kaweth_device *kaweth =
545 container_of(work, struct kaweth_device, lowmem_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200547 if (IS_BLOCKED(kaweth->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return;
549
550 if (kaweth->suspend_lowmem_rx)
551 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
552
553 if (kaweth->suspend_lowmem_ctrl)
554 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
555}
556
557
558/****************************************************************
559 * kaweth_resubmit_rx_urb
560 ****************************************************************/
561static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
Al Viro55016f12005-10-21 03:21:58 -0400562 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int result;
565
566 usb_fill_bulk_urb(kaweth->rx_urb,
567 kaweth->dev,
568 usb_rcvbulkpipe(kaweth->dev, 1),
569 kaweth->rx_buf,
570 KAWETH_BUF_SIZE,
571 kaweth_usb_receive,
572 kaweth);
573 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
574 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
575
576 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
577 if (result == -ENOMEM) {
578 kaweth->suspend_lowmem_rx = 1;
579 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
580 }
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700581 dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
582 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } else {
584 kaweth->suspend_lowmem_rx = 0;
585 }
586
587 return result;
588}
589
590static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
591
592/****************************************************************
593 * kaweth_usb_receive
594 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100595static void kaweth_usb_receive(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct kaweth_device *kaweth = urb->context;
598 struct net_device *net = kaweth->net;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800599 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 int count = urb->actual_length;
602 int count2 = urb->transfer_buffer_length;
603
604 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
605
606 struct sk_buff *skb;
607
Larry Finger710b5232009-04-09 07:11:55 +0000608 if (unlikely(status == -EPIPE)) {
609 kaweth->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 kaweth->end = 1;
611 wake_up(&kaweth->term_wait);
Larry Finger710b5232009-04-09 07:11:55 +0000612 dbg("Status was -EPIPE.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return;
614 }
Larry Finger710b5232009-04-09 07:11:55 +0000615 if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
616 /* we are killed - set a flag and wake the disconnect handler */
617 kaweth->end = 1;
618 wake_up(&kaweth->term_wait);
619 dbg("Status was -ECONNRESET or -ESHUTDOWN.");
620 return;
621 }
622 if (unlikely(status == -EPROTO || status == -ETIME ||
623 status == -EILSEQ)) {
624 kaweth->stats.rx_errors++;
625 dbg("Status was -EPROTO, -ETIME, or -EILSEQ.");
626 return;
627 }
628 if (unlikely(status == -EOVERFLOW)) {
629 kaweth->stats.rx_errors++;
630 dbg("Status was -EOVERFLOW.");
631 }
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200632 spin_lock(&kaweth->device_lock);
633 if (IS_BLOCKED(kaweth->status)) {
634 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return;
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200636 }
637 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Oliver Neukumc94cb312008-12-18 23:00:59 -0800639 if(status && status != -EREMOTEIO && count != 1) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700640 dev_err(&kaweth->intf->dev,
641 "%s RX status: %d count: %d packet_len: %d\n",
642 net->name, status, count, (int)pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
644 return;
645 }
646
647 if(kaweth->net && (count > 2)) {
648 if(pkt_len > (count - 2)) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700649 dev_err(&kaweth->intf->dev,
650 "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
651 pkt_len, count);
652 dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
653 pkt_len & 2047);
654 dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
656 return;
657 }
658
659 if(!(skb = dev_alloc_skb(pkt_len+2))) {
660 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
661 return;
662 }
663
664 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
665
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700666 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 skb_put(skb, pkt_len);
669
670 skb->protocol = eth_type_trans(skb, net);
671
672 netif_rx(skb);
673
674 kaweth->stats.rx_packets++;
675 kaweth->stats.rx_bytes += pkt_len;
676 }
677
678 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
679}
680
681/****************************************************************
682 * kaweth_open
683 ****************************************************************/
684static int kaweth_open(struct net_device *net)
685{
686 struct kaweth_device *kaweth = netdev_priv(net);
687 int res;
688
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200689 dbg("Opening network device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100691 res = usb_autopm_get_interface(kaweth->intf);
692 if (res) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700693 dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100694 return -EIO;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
697 if (res)
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100698 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 usb_fill_int_urb(
701 kaweth->irq_urb,
702 kaweth->dev,
703 usb_rcvintpipe(kaweth->dev, 3),
704 kaweth->intbuffer,
705 INTBUFFERSIZE,
706 int_callback,
707 kaweth,
708 250); /* overriding the descriptor */
709 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
710 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
711
712 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
713 if (res) {
714 usb_kill_urb(kaweth->rx_urb);
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100715 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200717 kaweth->opened = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 netif_start_queue(net);
720
721 kaweth_async_set_rx_mode(kaweth);
722 return 0;
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100723
724err_out:
Oliver Neukumceb0c772009-12-11 21:43:00 -0800725 usb_autopm_put_interface(kaweth->intf);
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100726 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/****************************************************************
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100730 * kaweth_kill_urbs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ****************************************************************/
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200732static void kaweth_kill_urbs(struct kaweth_device *kaweth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 usb_kill_urb(kaweth->irq_urb);
735 usb_kill_urb(kaweth->rx_urb);
Herbert Xud23b5362005-11-17 09:47:45 -0800736 usb_kill_urb(kaweth->tx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
David S. Miller4bb073c2008-06-12 02:22:02 -0700738 cancel_delayed_work_sync(&kaweth->lowmem_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* a scheduled work may have resubmitted,
741 we hit them again */
742 usb_kill_urb(kaweth->irq_urb);
743 usb_kill_urb(kaweth->rx_urb);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200744}
745
746/****************************************************************
747 * kaweth_close
748 ****************************************************************/
749static int kaweth_close(struct net_device *net)
750{
751 struct kaweth_device *kaweth = netdev_priv(net);
752
753 netif_stop_queue(net);
754 kaweth->opened = 0;
755
756 kaweth->status |= KAWETH_STATUS_CLOSING;
757
758 kaweth_kill_urbs(kaweth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 kaweth->status &= ~KAWETH_STATUS_CLOSING;
761
Oliver Neukumceb0c772009-12-11 21:43:00 -0800762 usb_autopm_put_interface(kaweth->intf);
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
765}
766
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100767static u32 kaweth_get_link(struct net_device *dev)
768{
769 struct kaweth_device *kaweth = netdev_priv(dev);
770
771 return kaweth->linkstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700774static const struct ethtool_ops ops = {
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100775 .get_link = kaweth_get_link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
777
778/****************************************************************
779 * kaweth_usb_transmit_complete
780 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100781static void kaweth_usb_transmit_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct kaweth_device *kaweth = urb->context;
784 struct sk_buff *skb = kaweth->tx_skb;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800785 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Oliver Neukumc94cb312008-12-18 23:00:59 -0800787 if (unlikely(status != 0))
788 if (status != -ENOENT)
789 dbg("%s: TX status %d.", kaweth->net->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 netif_wake_queue(kaweth->net);
792 dev_kfree_skb_irq(skb);
793}
794
795/****************************************************************
796 * kaweth_start_xmit
797 ****************************************************************/
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000798static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
799 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct kaweth_device *kaweth = netdev_priv(net);
802 __le16 *private_header;
803
804 int res;
805
Larry Finger4ff61c82009-04-02 01:09:43 -0700806 spin_lock_irq(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 kaweth_async_set_rx_mode(kaweth);
809 netif_stop_queue(net);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200810 if (IS_BLOCKED(kaweth->status)) {
811 goto skip;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* We now decide whether we can put our special header into the sk_buff */
815 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
816 /* no such luck - we make our own */
817 struct sk_buff *copied_skb;
818 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
819 dev_kfree_skb_irq(skb);
820 skb = copied_skb;
821 if (!copied_skb) {
822 kaweth->stats.tx_errors++;
823 netif_start_queue(net);
Larry Finger4ff61c82009-04-02 01:09:43 -0700824 spin_unlock_irq(&kaweth->device_lock);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000825 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 }
828
829 private_header = (__le16 *)__skb_push(skb, 2);
830 *private_header = cpu_to_le16(skb->len-2);
831 kaweth->tx_skb = skb;
832
833 usb_fill_bulk_urb(kaweth->tx_urb,
834 kaweth->dev,
835 usb_sndbulkpipe(kaweth->dev, 2),
836 private_header,
837 skb->len,
838 kaweth_usb_transmit_complete,
839 kaweth);
840 kaweth->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
843 {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700844 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200845skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 kaweth->stats.tx_errors++;
847
848 netif_start_queue(net);
849 dev_kfree_skb_irq(skb);
850 }
851 else
852 {
853 kaweth->stats.tx_packets++;
854 kaweth->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Larry Finger4ff61c82009-04-02 01:09:43 -0700857 spin_unlock_irq(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Patrick McHardy6ed10652009-06-23 06:03:08 +0000859 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/****************************************************************
863 * kaweth_set_rx_mode
864 ****************************************************************/
865static void kaweth_set_rx_mode(struct net_device *net)
866{
867 struct kaweth_device *kaweth = netdev_priv(net);
868
869 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
870 KAWETH_PACKET_FILTER_BROADCAST |
871 KAWETH_PACKET_FILTER_MULTICAST;
872
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200873 dbg("Setting Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 netif_stop_queue(net);
876
877 if (net->flags & IFF_PROMISC) {
878 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
879 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000880 else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
882 }
883
884 kaweth->packet_filter_bitmap = packet_filter_bitmap;
885 netif_wake_queue(net);
886}
887
888/****************************************************************
889 * kaweth_async_set_rx_mode
890 ****************************************************************/
891static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
892{
Larry Finger710b5232009-04-09 07:11:55 +0000893 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
Larry Finger710b5232009-04-09 07:11:55 +0000895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 kaweth->packet_filter_bitmap = 0;
897 if (packet_filter_bitmap == 0)
898 return;
899
Larry Finger710b5232009-04-09 07:11:55 +0000900 if (in_interrupt())
901 return;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 result = kaweth_control(kaweth,
904 usb_sndctrlpipe(kaweth->dev, 0),
905 KAWETH_COMMAND_SET_PACKET_FILTER,
906 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
907 packet_filter_bitmap,
908 0,
909 (void *)&kaweth->scratch,
910 0,
911 KAWETH_CONTROL_TIMEOUT);
912
913 if(result < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -0700914 dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
915 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 else {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200918 dbg("Set Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/****************************************************************
923 * kaweth_netdev_stats
924 ****************************************************************/
925static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
926{
927 struct kaweth_device *kaweth = netdev_priv(dev);
928 return &kaweth->stats;
929}
930
931/****************************************************************
932 * kaweth_tx_timeout
933 ****************************************************************/
934static void kaweth_tx_timeout(struct net_device *net)
935{
936 struct kaweth_device *kaweth = netdev_priv(net);
937
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700938 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 kaweth->stats.tx_errors++;
940 net->trans_start = jiffies;
941
942 usb_unlink_urb(kaweth->tx_urb);
943}
944
945/****************************************************************
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200946 * kaweth_suspend
947 ****************************************************************/
948static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
949{
950 struct kaweth_device *kaweth = usb_get_intfdata(intf);
951 unsigned long flags;
952
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100953 dbg("Suspending device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200954 spin_lock_irqsave(&kaweth->device_lock, flags);
955 kaweth->status |= KAWETH_STATUS_SUSPENDING;
956 spin_unlock_irqrestore(&kaweth->device_lock, flags);
957
958 kaweth_kill_urbs(kaweth);
959 return 0;
960}
961
962/****************************************************************
963 * kaweth_resume
964 ****************************************************************/
965static int kaweth_resume(struct usb_interface *intf)
966{
967 struct kaweth_device *kaweth = usb_get_intfdata(intf);
968 unsigned long flags;
969
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100970 dbg("Resuming device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200971 spin_lock_irqsave(&kaweth->device_lock, flags);
972 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
973 spin_unlock_irqrestore(&kaweth->device_lock, flags);
974
975 if (!kaweth->opened)
976 return 0;
977 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
978 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
979
980 return 0;
981}
982
983/****************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * kaweth_probe
985 ****************************************************************/
Oliver Neukum321ea8b2009-01-08 11:20:22 -0800986
987
988static const struct net_device_ops kaweth_netdev_ops = {
989 .ndo_open = kaweth_open,
990 .ndo_stop = kaweth_close,
991 .ndo_start_xmit = kaweth_start_xmit,
992 .ndo_tx_timeout = kaweth_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000993 .ndo_set_rx_mode = kaweth_set_rx_mode,
Oliver Neukum321ea8b2009-01-08 11:20:22 -0800994 .ndo_get_stats = kaweth_netdev_stats,
Ben Hutchings635ecaa2009-07-09 17:59:01 +0000995 .ndo_change_mtu = eth_change_mtu,
Ben Hutchings240c1022009-07-09 17:54:35 +0000996 .ndo_set_mac_address = eth_mac_addr,
997 .ndo_validate_addr = eth_validate_addr,
Oliver Neukum321ea8b2009-01-08 11:20:22 -0800998};
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000static int kaweth_probe(
1001 struct usb_interface *intf,
1002 const struct usb_device_id *id /* from id_table */
1003 )
1004{
1005 struct usb_device *dev = interface_to_usbdev(intf);
1006 struct kaweth_device *kaweth;
1007 struct net_device *netdev;
1008 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1009 int result = 0;
1010
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001011 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dev->devnum,
1013 le16_to_cpu(dev->descriptor.idVendor),
1014 le16_to_cpu(dev->descriptor.idProduct),
1015 le16_to_cpu(dev->descriptor.bcdDevice));
1016
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001017 dbg("Device at %p", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001019 dbg("Descriptor length: %x type: %x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 (int)dev->descriptor.bLength,
1021 (int)dev->descriptor.bDescriptorType);
1022
1023 netdev = alloc_etherdev(sizeof(*kaweth));
1024 if (!netdev)
1025 return -ENOMEM;
1026
1027 kaweth = netdev_priv(netdev);
1028 kaweth->dev = dev;
1029 kaweth->net = netdev;
1030
1031 spin_lock_init(&kaweth->device_lock);
1032 init_waitqueue_head(&kaweth->term_wait);
1033
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001034 dbg("Resetting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 kaweth_reset(kaweth);
1037
1038 /*
1039 * If high byte of bcdDevice is nonzero, firmware is already
1040 * downloaded. Don't try to do it again, or we'll hang the device.
1041 */
1042
1043 if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001044 dev_info(&intf->dev, "Firmware present in device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 } else {
1046 /* Download the firmware */
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001047 dev_info(&intf->dev, "Downloading firmware...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1049 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001050 "kaweth/new_code.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 100,
1052 2)) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001053 dev_err(&intf->dev, "Error downloading firmware (%d)\n",
1054 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto err_fw;
1056 }
1057
1058 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001059 "kaweth/new_code_fix.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 100,
1061 3)) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001062 dev_err(&intf->dev,
1063 "Error downloading firmware fix (%d)\n",
1064 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 goto err_fw;
1066 }
1067
1068 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001069 "kaweth/trigger_code.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 126,
1071 2)) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001072 dev_err(&intf->dev,
1073 "Error downloading trigger code (%d)\n",
1074 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto err_fw;
1076
1077 }
1078
1079 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001080 "kaweth/trigger_code_fix.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 126,
1082 3)) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001083 dev_err(&intf->dev, "Error downloading trigger code fix (%d)\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto err_fw;
1085 }
1086
1087
1088 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001089 dev_err(&intf->dev, "Error triggering firmware (%d)\n",
1090 result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto err_fw;
1092 }
1093
1094 /* Device will now disappear for a moment... */
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001095 dev_info(&intf->dev, "Firmware loaded. I'll be back...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096err_fw:
1097 free_page((unsigned long)kaweth->firmware_buf);
1098 free_netdev(netdev);
1099 return -EIO;
1100 }
1101
1102 result = kaweth_read_configuration(kaweth);
1103
1104 if(result < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001105 dev_err(&intf->dev, "Error reading configuration (%d), no net device created\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto err_free_netdev;
1107 }
1108
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001109 dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1110 dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1111 dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
Danny Kukawka6ac8f062012-02-24 03:46:00 +00001112 dev_info(&intf->dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if(!memcmp(&kaweth->configuration.hw_addr,
1115 &bcast_addr,
1116 sizeof(bcast_addr))) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001117 dev_err(&intf->dev, "Firmware not functioning properly, no net device created\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto err_free_netdev;
1119 }
1120
1121 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001122 dbg("Error setting URB size");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto err_free_netdev;
1124 }
1125
1126 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001127 dev_err(&intf->dev, "Error setting SOFS wait\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto err_free_netdev;
1129 }
1130
1131 result = kaweth_set_receive_filter(kaweth,
1132 KAWETH_PACKET_FILTER_DIRECTED |
1133 KAWETH_PACKET_FILTER_BROADCAST |
1134 KAWETH_PACKET_FILTER_MULTICAST);
1135
1136 if(result < 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001137 dev_err(&intf->dev, "Error setting receive filter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto err_free_netdev;
1139 }
1140
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001141 dbg("Initializing net device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001143 kaweth->intf = intf;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1146 if (!kaweth->tx_urb)
1147 goto err_free_netdev;
1148 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1149 if (!kaweth->rx_urb)
1150 goto err_only_tx;
1151 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1152 if (!kaweth->irq_urb)
1153 goto err_tx_and_rx;
1154
Daniel Mack997ea582010-04-12 13:17:25 +02001155 kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 INTBUFFERSIZE,
1157 GFP_KERNEL,
1158 &kaweth->intbufferhandle);
1159 if (!kaweth->intbuffer)
1160 goto err_tx_and_rx_and_irq;
Daniel Mack997ea582010-04-12 13:17:25 +02001161 kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 KAWETH_BUF_SIZE,
1163 GFP_KERNEL,
1164 &kaweth->rxbufferhandle);
1165 if (!kaweth->rx_buf)
1166 goto err_all_but_rxbuf;
1167
1168 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1169 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1170 sizeof(kaweth->configuration.hw_addr));
1171
Oliver Neukum321ea8b2009-01-08 11:20:22 -08001172 netdev->netdev_ops = &kaweth_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1175 SET_ETHTOOL_OPS(netdev, &ops);
1176
1177 /* kaweth is zeroed as part of alloc_netdev */
David Howellsc4028952006-11-22 14:57:56 +00001178 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 usb_set_intfdata(intf, kaweth);
1180
1181#if 0
1182// dma_supported() is deeply broken on almost all architectures
1183 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1184 kaweth->net->features |= NETIF_F_HIGHDMA;
1185#endif
1186
1187 SET_NETDEV_DEV(netdev, &intf->dev);
1188 if (register_netdev(netdev) != 0) {
Greg Kroah-Hartmandb6b2212012-04-25 12:37:48 -07001189 dev_err(&intf->dev, "Error registering netdev.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto err_intfdata;
1191 }
1192
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001193 dev_info(&intf->dev, "kaweth interface created at %s\n",
1194 kaweth->net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001196 dbg("Kaweth probe returning.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 return 0;
1199
1200err_intfdata:
1201 usb_set_intfdata(intf, NULL);
Daniel Mack997ea582010-04-12 13:17:25 +02001202 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203err_all_but_rxbuf:
Daniel Mack997ea582010-04-12 13:17:25 +02001204 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205err_tx_and_rx_and_irq:
1206 usb_free_urb(kaweth->irq_urb);
1207err_tx_and_rx:
1208 usb_free_urb(kaweth->rx_urb);
1209err_only_tx:
1210 usb_free_urb(kaweth->tx_urb);
1211err_free_netdev:
1212 free_netdev(netdev);
1213
1214 return -EIO;
1215}
1216
1217/****************************************************************
1218 * kaweth_disconnect
1219 ****************************************************************/
1220static void kaweth_disconnect(struct usb_interface *intf)
1221{
1222 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1223 struct net_device *netdev;
1224
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001225 dev_info(&intf->dev, "Unregistering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 usb_set_intfdata(intf, NULL);
1228 if (!kaweth) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001229 dev_warn(&intf->dev, "unregistering non-existent device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
1231 }
1232 netdev = kaweth->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001234 dbg("Unregistering net device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 unregister_netdev(netdev);
1236
1237 usb_free_urb(kaweth->rx_urb);
1238 usb_free_urb(kaweth->tx_urb);
1239 usb_free_urb(kaweth->irq_urb);
1240
Daniel Mack997ea582010-04-12 13:17:25 +02001241 usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1242 usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 free_netdev(netdev);
1245}
1246
1247
1248// FIXME this completion stuff is a modified clone of
1249// an OLD version of some stuff in usb.c ...
1250struct usb_api_data {
1251 wait_queue_head_t wqh;
1252 int done;
1253};
1254
1255/*-------------------------------------------------------------------*
1256 * completion handler for compatibility wrappers (sync control/bulk) *
1257 *-------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +01001258static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1261
1262 awd->done=1;
1263 wake_up(&awd->wqh);
1264}
1265
1266/*-------------------------------------------------------------------*
1267 * COMPATIBILITY STUFF *
1268 *-------------------------------------------------------------------*/
1269
1270// Starts urb and waits for completion or timeout
1271static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1272{
1273 struct usb_api_data awd;
1274 int status;
1275
1276 init_waitqueue_head(&awd.wqh);
1277 awd.done = 0;
1278
1279 urb->context = &awd;
1280 status = usb_submit_urb(urb, GFP_NOIO);
1281 if (status) {
1282 // something went wrong
1283 usb_free_urb(urb);
1284 return status;
1285 }
1286
1287 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1288 // timeout
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -07001289 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 usb_kill_urb(urb); // remove urb safely
1291 status = -ETIMEDOUT;
1292 }
1293 else {
1294 status = urb->status;
1295 }
1296
1297 if (actual_length) {
1298 *actual_length = urb->actual_length;
1299 }
1300
1301 usb_free_urb(urb);
1302 return status;
1303}
1304
1305/*-------------------------------------------------------------------*/
1306// returns status (negative) or length (positive)
1307static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1308 unsigned int pipe,
1309 struct usb_ctrlrequest *cmd, void *data,
1310 int len, int timeout)
1311{
1312 struct urb *urb;
1313 int retv;
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001314 int length = 0; /* shut up GCC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 urb = usb_alloc_urb(0, GFP_NOIO);
1317 if (!urb)
1318 return -ENOMEM;
1319
1320 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1321 len, usb_api_blocking_completion, NULL);
1322
1323 retv = usb_start_wait_urb(urb, timeout, &length);
1324 if (retv < 0) {
1325 return retv;
1326 }
1327 else {
1328 return length;
1329 }
1330}
1331
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001332module_usb_driver(kaweth_driver);