blob: fdbf3be24fda499e5065b7b3c691ade64f56397e [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:
34 * Fix in_interrupt() problem
35 * Develop test procedures for USB net interfaces
36 * Run test procedures
37 * Fix bugs from previous two steps
38 * Snoop other OSs for any tricks we're not doing
39 * SMP locking
40 * Reduce arbitrary timeouts
41 * Smart multicast support
42 * Temporary MAC change support
43 * Tunable SOFs parameter - ioctl()?
44 * Ethernet stats collection
45 * Code formatting improvements
46 */
47
48#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/init.h>
52#include <linux/delay.h>
53#include <linux/netdevice.h>
54#include <linux/etherdevice.h>
55#include <linux/usb.h>
56#include <linux/types.h>
57#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/dma-mapping.h>
59#include <linux/wait.h>
David Woodhouse79682492008-05-29 17:17:17 +030060#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/byteorder.h>
63
64#undef DEBUG
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define KAWETH_MTU 1514
67#define KAWETH_BUF_SIZE 1664
68#define KAWETH_TX_TIMEOUT (5 * HZ)
69#define KAWETH_SCRATCH_SIZE 32
70#define KAWETH_FIRMWARE_BUF_SIZE 4096
Russ Dill2b2b2e32008-01-07 21:48:12 -080071#define KAWETH_CONTROL_TIMEOUT (30000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define KAWETH_STATUS_BROKEN 0x0000001
74#define KAWETH_STATUS_CLOSING 0x0000002
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +020075#define KAWETH_STATUS_SUSPENDING 0x0000004
76
77#define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
80#define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
81#define KAWETH_PACKET_FILTER_DIRECTED 0x04
82#define KAWETH_PACKET_FILTER_BROADCAST 0x08
83#define KAWETH_PACKET_FILTER_MULTICAST 0x10
84
85/* Table 7 */
86#define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
87#define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
88#define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
89#define KAWETH_COMMAND_STATISTICS 0x03
90#define KAWETH_COMMAND_SET_TEMP_MAC 0x06
91#define KAWETH_COMMAND_GET_TEMP_MAC 0x07
92#define KAWETH_COMMAND_SET_URB_SIZE 0x08
93#define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
94#define KAWETH_COMMAND_SCAN 0xFF
95
96#define KAWETH_SOFS_TO_WAIT 0x05
97
98#define INTBUFFERSIZE 4
99
100#define STATE_OFFSET 0
101#define STATE_MASK 0x40
102#define STATE_SHIFT 5
103
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200104#define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
108MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
109MODULE_LICENSE("GPL");
David Woodhouse79682492008-05-29 17:17:17 +0300110MODULE_FIRMWARE("kaweth/new_code.bin");
111MODULE_FIRMWARE("kaweth/new_code_fix.bin");
112MODULE_FIRMWARE("kaweth/trigger_code.bin");
113MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static const char driver_name[] = "kaweth";
116
117static int kaweth_probe(
118 struct usb_interface *intf,
119 const struct usb_device_id *id /* from id_table */
120 );
121static void kaweth_disconnect(struct usb_interface *intf);
122static int kaweth_internal_control_msg(struct usb_device *usb_dev,
123 unsigned int pipe,
124 struct usb_ctrlrequest *cmd, void *data,
125 int len, int timeout);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200126static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
127static int kaweth_resume(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/****************************************************************
130 * usb_device_id
131 ****************************************************************/
132static struct usb_device_id usb_klsi_table[] = {
133 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
134 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
135 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
136 { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
137 { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
138 { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
139 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
140 { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
141 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
142 { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
143 { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
144 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
145 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
146 { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
147 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
148 { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
149 { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
150 { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
151 { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
152 { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
153 { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
154 { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
155 { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
156 { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
157 { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
158 { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
159 { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
160 { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
161 { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
162 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
163 { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
164 { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
Oliver Neukum486ba2a2006-09-28 22:21:19 +0200165 { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
167 {} /* Null terminator */
168};
169
170MODULE_DEVICE_TABLE (usb, usb_klsi_table);
171
172/****************************************************************
173 * kaweth_driver
174 ****************************************************************/
175static struct usb_driver kaweth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .name = driver_name,
177 .probe = kaweth_probe,
178 .disconnect = kaweth_disconnect,
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200179 .suspend = kaweth_suspend,
180 .resume = kaweth_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .id_table = usb_klsi_table,
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100182 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
185typedef __u8 eth_addr_t[6];
186
187/****************************************************************
188 * usb_eth_dev
189 ****************************************************************/
190struct usb_eth_dev {
191 char *name;
192 __u16 vendor;
193 __u16 device;
194 void *pdata;
195};
196
197/****************************************************************
198 * kaweth_ethernet_configuration
199 * Refer Table 8
200 ****************************************************************/
201struct kaweth_ethernet_configuration
202{
203 __u8 size;
204 __u8 reserved1;
205 __u8 reserved2;
206 eth_addr_t hw_addr;
207 __u32 statistics_mask;
208 __le16 segment_size;
209 __u16 max_multicast_filters;
210 __u8 reserved3;
211} __attribute__ ((packed));
212
213/****************************************************************
214 * kaweth_device
215 ****************************************************************/
216struct kaweth_device
217{
218 spinlock_t device_lock;
219
220 __u32 status;
221 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int suspend_lowmem_rx;
223 int suspend_lowmem_ctrl;
224 int linkstate;
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200225 int opened;
David Howellsc4028952006-11-22 14:57:56 +0000226 struct delayed_work lowmem_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 struct usb_device *dev;
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100229 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct net_device *net;
231 wait_queue_head_t term_wait;
232
233 struct urb *rx_urb;
234 struct urb *tx_urb;
235 struct urb *irq_urb;
236
237 dma_addr_t intbufferhandle;
238 __u8 *intbuffer;
239 dma_addr_t rxbufferhandle;
240 __u8 *rx_buf;
241
242
243 struct sk_buff *tx_skb;
244
245 __u8 *firmware_buf;
246 __u8 scratch[KAWETH_SCRATCH_SIZE];
247 __u16 packet_filter_bitmap;
248
249 struct kaweth_ethernet_configuration configuration;
250
251 struct net_device_stats stats;
252};
253
254
255/****************************************************************
256 * kaweth_control
257 ****************************************************************/
258static int kaweth_control(struct kaweth_device *kaweth,
259 unsigned int pipe,
260 __u8 request,
261 __u8 requesttype,
262 __u16 value,
263 __u16 index,
264 void *data,
265 __u16 size,
266 int timeout)
267{
268 struct usb_ctrlrequest *dr;
269
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200270 dbg("kaweth_control()");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if(in_interrupt()) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200273 dbg("in_interrupt()");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EBUSY;
275 }
276
277 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
278
279 if (!dr) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200280 dbg("kmalloc() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -ENOMEM;
282 }
283
284 dr->bRequestType= requesttype;
285 dr->bRequest = request;
286 dr->wValue = cpu_to_le16p(&value);
287 dr->wIndex = cpu_to_le16p(&index);
288 dr->wLength = cpu_to_le16p(&size);
289
290 return kaweth_internal_control_msg(kaweth->dev,
291 pipe,
292 dr,
293 data,
294 size,
295 timeout);
296}
297
298/****************************************************************
299 * kaweth_read_configuration
300 ****************************************************************/
301static int kaweth_read_configuration(struct kaweth_device *kaweth)
302{
303 int retval;
304
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200305 dbg("Reading kaweth configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 retval = kaweth_control(kaweth,
308 usb_rcvctrlpipe(kaweth->dev, 0),
309 KAWETH_COMMAND_GET_ETHERNET_DESC,
310 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
311 0,
312 0,
313 (void *)&kaweth->configuration,
314 sizeof(kaweth->configuration),
315 KAWETH_CONTROL_TIMEOUT);
316
317 return retval;
318}
319
320/****************************************************************
321 * kaweth_set_urb_size
322 ****************************************************************/
323static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
324{
325 int retval;
326
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200327 dbg("Setting URB size to %d", (unsigned)urb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 retval = kaweth_control(kaweth,
330 usb_sndctrlpipe(kaweth->dev, 0),
331 KAWETH_COMMAND_SET_URB_SIZE,
332 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
333 urb_size,
334 0,
335 (void *)&kaweth->scratch,
336 0,
337 KAWETH_CONTROL_TIMEOUT);
338
339 return retval;
340}
341
342/****************************************************************
343 * kaweth_set_sofs_wait
344 ****************************************************************/
345static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
346{
347 int retval;
348
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200349 dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 retval = kaweth_control(kaweth,
352 usb_sndctrlpipe(kaweth->dev, 0),
353 KAWETH_COMMAND_SET_SOFS_WAIT,
354 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
355 sofs_wait,
356 0,
357 (void *)&kaweth->scratch,
358 0,
359 KAWETH_CONTROL_TIMEOUT);
360
361 return retval;
362}
363
364/****************************************************************
365 * kaweth_set_receive_filter
366 ****************************************************************/
367static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
368 __u16 receive_filter)
369{
370 int retval;
371
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200372 dbg("Set receive filter to %d", (unsigned)receive_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 retval = kaweth_control(kaweth,
375 usb_sndctrlpipe(kaweth->dev, 0),
376 KAWETH_COMMAND_SET_PACKET_FILTER,
377 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
378 receive_filter,
379 0,
380 (void *)&kaweth->scratch,
381 0,
382 KAWETH_CONTROL_TIMEOUT);
383
384 return retval;
385}
386
387/****************************************************************
388 * kaweth_download_firmware
389 ****************************************************************/
390static int kaweth_download_firmware(struct kaweth_device *kaweth,
David Woodhouse79682492008-05-29 17:17:17 +0300391 const char *fwname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 __u8 interrupt,
393 __u8 type)
394{
David Woodhouse79682492008-05-29 17:17:17 +0300395 const struct firmware *fw;
396 int data_len;
397 int ret;
398
399 ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
400 if (ret) {
401 err("Firmware request failed\n");
402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
David Woodhouse79682492008-05-29 17:17:17 +0300405 if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
406 err("Firmware too big: %zu", fw->size);
407 return -ENOSPC;
408 }
409 data_len = fw->size;
410 memcpy(kaweth->firmware_buf, fw->data, fw->size);
411
412 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
415 kaweth->firmware_buf[3] = data_len >> 8;
416 kaweth->firmware_buf[4] = type;
417 kaweth->firmware_buf[5] = interrupt;
418
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200419 dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 kaweth->firmware_buf[2]);
421
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200422 dbg("Downloading firmware at %p to kaweth device at %p",
David Woodhouse79682492008-05-29 17:17:17 +0300423 fw->data, kaweth);
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200424 dbg("Firmware length: %d", data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return kaweth_control(kaweth,
427 usb_sndctrlpipe(kaweth->dev, 0),
428 KAWETH_COMMAND_SCAN,
429 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
430 0,
431 0,
432 (void *)kaweth->firmware_buf,
433 data_len,
434 KAWETH_CONTROL_TIMEOUT);
435}
436
437/****************************************************************
438 * kaweth_trigger_firmware
439 ****************************************************************/
440static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
441 __u8 interrupt)
442{
443 kaweth->firmware_buf[0] = 0xB6;
444 kaweth->firmware_buf[1] = 0xC3;
445 kaweth->firmware_buf[2] = 0x01;
446 kaweth->firmware_buf[3] = 0x00;
447 kaweth->firmware_buf[4] = 0x06;
448 kaweth->firmware_buf[5] = interrupt;
449 kaweth->firmware_buf[6] = 0x00;
450 kaweth->firmware_buf[7] = 0x00;
451
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200452 dbg("Triggering firmware");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 return kaweth_control(kaweth,
455 usb_sndctrlpipe(kaweth->dev, 0),
456 KAWETH_COMMAND_SCAN,
457 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
458 0,
459 0,
460 (void *)kaweth->firmware_buf,
461 8,
462 KAWETH_CONTROL_TIMEOUT);
463}
464
465/****************************************************************
466 * kaweth_reset
467 ****************************************************************/
468static int kaweth_reset(struct kaweth_device *kaweth)
469{
470 int result;
471
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200472 dbg("kaweth_reset(%p)", kaweth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 result = kaweth_control(kaweth,
474 usb_sndctrlpipe(kaweth->dev, 0),
475 USB_REQ_SET_CONFIGURATION,
476 0,
477 kaweth->dev->config[0].desc.bConfigurationValue,
478 0,
479 NULL,
480 0,
481 KAWETH_CONTROL_TIMEOUT);
482
Guillaume GOURAT /f2d45cd2005-10-21 14:01:35 +0200483 mdelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200485 dbg("kaweth_reset() returns %d.",result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 return result;
488}
489
David Howells7d12e782006-10-05 14:55:46 +0100490static void kaweth_usb_receive(struct urb *);
Al Viro55016f12005-10-21 03:21:58 -0400491static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/****************************************************************
494 int_callback
495*****************************************************************/
496
Al Viro55016f12005-10-21 03:21:58 -0400497static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 int status;
500
501 status = usb_submit_urb (kaweth->irq_urb, mf);
502 if (unlikely(status == -ENOMEM)) {
503 kaweth->suspend_lowmem_ctrl = 1;
504 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
505 } else {
506 kaweth->suspend_lowmem_ctrl = 0;
507 }
508
509 if (status)
510 err ("can't resubmit intr, %s-%s, status %d",
511 kaweth->dev->bus->bus_name,
512 kaweth->dev->devpath, status);
513}
514
David Howells7d12e782006-10-05 14:55:46 +0100515static void int_callback(struct urb *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct kaweth_device *kaweth = u->context;
518 int act_state;
519
520 switch (u->status) {
521 case 0: /* success */
522 break;
523 case -ECONNRESET: /* unlink */
524 case -ENOENT:
525 case -ESHUTDOWN:
526 return;
527 /* -EPIPE: should clear the halt */
528 default: /* error */
529 goto resubmit;
530 }
531
532 /* we check the link state to report changes */
533 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
Oliver Neukum58125f92005-06-15 22:26:38 -0700534 if (act_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 netif_carrier_on(kaweth->net);
536 else
537 netif_carrier_off(kaweth->net);
538
539 kaweth->linkstate = act_state;
540 }
541resubmit:
542 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
543}
544
David Howellsc4028952006-11-22 14:57:56 +0000545static void kaweth_resubmit_tl(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
David Howellsc4028952006-11-22 14:57:56 +0000547 struct kaweth_device *kaweth =
548 container_of(work, struct kaweth_device, lowmem_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200550 if (IS_BLOCKED(kaweth->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
552
553 if (kaweth->suspend_lowmem_rx)
554 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
555
556 if (kaweth->suspend_lowmem_ctrl)
557 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
558}
559
560
561/****************************************************************
562 * kaweth_resubmit_rx_urb
563 ****************************************************************/
564static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
Al Viro55016f12005-10-21 03:21:58 -0400565 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 int result;
568
569 usb_fill_bulk_urb(kaweth->rx_urb,
570 kaweth->dev,
571 usb_rcvbulkpipe(kaweth->dev, 1),
572 kaweth->rx_buf,
573 KAWETH_BUF_SIZE,
574 kaweth_usb_receive,
575 kaweth);
576 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
577 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
578
579 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
580 if (result == -ENOMEM) {
581 kaweth->suspend_lowmem_rx = 1;
582 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
583 }
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200584 err("resubmitting rx_urb %d failed", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
586 kaweth->suspend_lowmem_rx = 0;
587 }
588
589 return result;
590}
591
592static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
593
594/****************************************************************
595 * kaweth_usb_receive
596 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100597static void kaweth_usb_receive(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct kaweth_device *kaweth = urb->context;
600 struct net_device *net = kaweth->net;
601
602 int count = urb->actual_length;
603 int count2 = urb->transfer_buffer_length;
604
605 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
606
607 struct sk_buff *skb;
608
609 if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
610 /* we are killed - set a flag and wake the disconnect handler */
611 {
612 kaweth->end = 1;
613 wake_up(&kaweth->term_wait);
614 return;
615 }
616
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200617 spin_lock(&kaweth->device_lock);
618 if (IS_BLOCKED(kaweth->status)) {
619 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return;
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200621 }
622 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if(urb->status && urb->status != -EREMOTEIO && count != 1) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200625 err("%s RX status: %d count: %d packet_len: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 net->name,
627 urb->status,
628 count,
629 (int)pkt_len);
630 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
631 return;
632 }
633
634 if(kaweth->net && (count > 2)) {
635 if(pkt_len > (count - 2)) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200636 err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
637 err("Packet len & 2047: %x", pkt_len & 2047);
638 err("Count 2: %x", count2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
640 return;
641 }
642
643 if(!(skb = dev_alloc_skb(pkt_len+2))) {
644 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
645 return;
646 }
647
648 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
649
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700650 skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 skb_put(skb, pkt_len);
653
654 skb->protocol = eth_type_trans(skb, net);
655
656 netif_rx(skb);
657
658 kaweth->stats.rx_packets++;
659 kaweth->stats.rx_bytes += pkt_len;
660 }
661
662 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
663}
664
665/****************************************************************
666 * kaweth_open
667 ****************************************************************/
668static int kaweth_open(struct net_device *net)
669{
670 struct kaweth_device *kaweth = netdev_priv(net);
671 int res;
672
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200673 dbg("Opening network device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100675 res = usb_autopm_get_interface(kaweth->intf);
676 if (res) {
677 err("Interface cannot be resumed.");
678 return -EIO;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
681 if (res)
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100682 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 usb_fill_int_urb(
685 kaweth->irq_urb,
686 kaweth->dev,
687 usb_rcvintpipe(kaweth->dev, 3),
688 kaweth->intbuffer,
689 INTBUFFERSIZE,
690 int_callback,
691 kaweth,
692 250); /* overriding the descriptor */
693 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
694 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
695
696 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
697 if (res) {
698 usb_kill_urb(kaweth->rx_urb);
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100699 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200701 kaweth->opened = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 netif_start_queue(net);
704
705 kaweth_async_set_rx_mode(kaweth);
706 return 0;
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100707
708err_out:
709 usb_autopm_enable(kaweth->intf);
710 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/****************************************************************
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100714 * kaweth_kill_urbs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ****************************************************************/
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200716static void kaweth_kill_urbs(struct kaweth_device *kaweth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 usb_kill_urb(kaweth->irq_urb);
719 usb_kill_urb(kaweth->rx_urb);
Herbert Xud23b5362005-11-17 09:47:45 -0800720 usb_kill_urb(kaweth->tx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David S. Miller4bb073c2008-06-12 02:22:02 -0700722 cancel_delayed_work_sync(&kaweth->lowmem_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* a scheduled work may have resubmitted,
725 we hit them again */
726 usb_kill_urb(kaweth->irq_urb);
727 usb_kill_urb(kaweth->rx_urb);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200728}
729
730/****************************************************************
731 * kaweth_close
732 ****************************************************************/
733static int kaweth_close(struct net_device *net)
734{
735 struct kaweth_device *kaweth = netdev_priv(net);
736
737 netif_stop_queue(net);
738 kaweth->opened = 0;
739
740 kaweth->status |= KAWETH_STATUS_CLOSING;
741
742 kaweth_kill_urbs(kaweth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 kaweth->status &= ~KAWETH_STATUS_CLOSING;
745
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100746 usb_autopm_enable(kaweth->intf);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return 0;
749}
750
751static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
752{
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100753 struct kaweth_device *kaweth = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 strlcpy(info->driver, driver_name, sizeof(info->driver));
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100756 usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
757}
758
759static u32 kaweth_get_link(struct net_device *dev)
760{
761 struct kaweth_device *kaweth = netdev_priv(dev);
762
763 return kaweth->linkstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static struct ethtool_ops ops = {
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100767 .get_drvinfo = kaweth_get_drvinfo,
768 .get_link = kaweth_get_link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769};
770
771/****************************************************************
772 * kaweth_usb_transmit_complete
773 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100774static void kaweth_usb_transmit_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct kaweth_device *kaweth = urb->context;
777 struct sk_buff *skb = kaweth->tx_skb;
778
779 if (unlikely(urb->status != 0))
780 if (urb->status != -ENOENT)
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200781 dbg("%s: TX status %d.", kaweth->net->name, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 netif_wake_queue(kaweth->net);
784 dev_kfree_skb_irq(skb);
785}
786
787/****************************************************************
788 * kaweth_start_xmit
789 ****************************************************************/
790static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
791{
792 struct kaweth_device *kaweth = netdev_priv(net);
793 __le16 *private_header;
794
795 int res;
796
797 spin_lock(&kaweth->device_lock);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 kaweth_async_set_rx_mode(kaweth);
800 netif_stop_queue(net);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200801 if (IS_BLOCKED(kaweth->status)) {
802 goto skip;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* We now decide whether we can put our special header into the sk_buff */
806 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
807 /* no such luck - we make our own */
808 struct sk_buff *copied_skb;
809 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
810 dev_kfree_skb_irq(skb);
811 skb = copied_skb;
812 if (!copied_skb) {
813 kaweth->stats.tx_errors++;
814 netif_start_queue(net);
815 spin_unlock(&kaweth->device_lock);
816 return 0;
817 }
818 }
819
820 private_header = (__le16 *)__skb_push(skb, 2);
821 *private_header = cpu_to_le16(skb->len-2);
822 kaweth->tx_skb = skb;
823
824 usb_fill_bulk_urb(kaweth->tx_urb,
825 kaweth->dev,
826 usb_sndbulkpipe(kaweth->dev, 2),
827 private_header,
828 skb->len,
829 kaweth_usb_transmit_complete,
830 kaweth);
831 kaweth->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
834 {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700835 dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200836skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 kaweth->stats.tx_errors++;
838
839 netif_start_queue(net);
840 dev_kfree_skb_irq(skb);
841 }
842 else
843 {
844 kaweth->stats.tx_packets++;
845 kaweth->stats.tx_bytes += skb->len;
846 net->trans_start = jiffies;
847 }
848
849 spin_unlock(&kaweth->device_lock);
850
851 return 0;
852}
853
854/****************************************************************
855 * kaweth_set_rx_mode
856 ****************************************************************/
857static void kaweth_set_rx_mode(struct net_device *net)
858{
859 struct kaweth_device *kaweth = netdev_priv(net);
860
861 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
862 KAWETH_PACKET_FILTER_BROADCAST |
863 KAWETH_PACKET_FILTER_MULTICAST;
864
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200865 dbg("Setting Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 netif_stop_queue(net);
868
869 if (net->flags & IFF_PROMISC) {
870 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
871 }
872 else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
873 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
874 }
875
876 kaweth->packet_filter_bitmap = packet_filter_bitmap;
877 netif_wake_queue(net);
878}
879
880/****************************************************************
881 * kaweth_async_set_rx_mode
882 ****************************************************************/
883static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
884{
885 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
886 kaweth->packet_filter_bitmap = 0;
887 if (packet_filter_bitmap == 0)
888 return;
889
890 {
891 int result;
892 result = kaweth_control(kaweth,
893 usb_sndctrlpipe(kaweth->dev, 0),
894 KAWETH_COMMAND_SET_PACKET_FILTER,
895 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
896 packet_filter_bitmap,
897 0,
898 (void *)&kaweth->scratch,
899 0,
900 KAWETH_CONTROL_TIMEOUT);
901
902 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200903 err("Failed to set Rx mode: %d", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 else {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200906 dbg("Set Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 }
909}
910
911/****************************************************************
912 * kaweth_netdev_stats
913 ****************************************************************/
914static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
915{
916 struct kaweth_device *kaweth = netdev_priv(dev);
917 return &kaweth->stats;
918}
919
920/****************************************************************
921 * kaweth_tx_timeout
922 ****************************************************************/
923static void kaweth_tx_timeout(struct net_device *net)
924{
925 struct kaweth_device *kaweth = netdev_priv(net);
926
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700927 dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 kaweth->stats.tx_errors++;
929 net->trans_start = jiffies;
930
931 usb_unlink_urb(kaweth->tx_urb);
932}
933
934/****************************************************************
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200935 * kaweth_suspend
936 ****************************************************************/
937static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
938{
939 struct kaweth_device *kaweth = usb_get_intfdata(intf);
940 unsigned long flags;
941
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100942 dbg("Suspending device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200943 spin_lock_irqsave(&kaweth->device_lock, flags);
944 kaweth->status |= KAWETH_STATUS_SUSPENDING;
945 spin_unlock_irqrestore(&kaweth->device_lock, flags);
946
947 kaweth_kill_urbs(kaweth);
948 return 0;
949}
950
951/****************************************************************
952 * kaweth_resume
953 ****************************************************************/
954static int kaweth_resume(struct usb_interface *intf)
955{
956 struct kaweth_device *kaweth = usb_get_intfdata(intf);
957 unsigned long flags;
958
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100959 dbg("Resuming device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200960 spin_lock_irqsave(&kaweth->device_lock, flags);
961 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
962 spin_unlock_irqrestore(&kaweth->device_lock, flags);
963
964 if (!kaweth->opened)
965 return 0;
966 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
967 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
968
969 return 0;
970}
971
972/****************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * kaweth_probe
974 ****************************************************************/
975static int kaweth_probe(
976 struct usb_interface *intf,
977 const struct usb_device_id *id /* from id_table */
978 )
979{
980 struct usb_device *dev = interface_to_usbdev(intf);
981 struct kaweth_device *kaweth;
982 struct net_device *netdev;
983 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
984 int result = 0;
985
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200986 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 dev->devnum,
988 le16_to_cpu(dev->descriptor.idVendor),
989 le16_to_cpu(dev->descriptor.idProduct),
990 le16_to_cpu(dev->descriptor.bcdDevice));
991
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200992 dbg("Device at %p", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200994 dbg("Descriptor length: %x type: %x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 (int)dev->descriptor.bLength,
996 (int)dev->descriptor.bDescriptorType);
997
998 netdev = alloc_etherdev(sizeof(*kaweth));
999 if (!netdev)
1000 return -ENOMEM;
1001
1002 kaweth = netdev_priv(netdev);
1003 kaweth->dev = dev;
1004 kaweth->net = netdev;
1005
1006 spin_lock_init(&kaweth->device_lock);
1007 init_waitqueue_head(&kaweth->term_wait);
1008
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001009 dbg("Resetting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 kaweth_reset(kaweth);
1012
1013 /*
1014 * If high byte of bcdDevice is nonzero, firmware is already
1015 * downloaded. Don't try to do it again, or we'll hang the device.
1016 */
1017
1018 if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001019 dev_info(&intf->dev, "Firmware present in device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else {
1021 /* Download the firmware */
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001022 dev_info(&intf->dev, "Downloading firmware...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1024 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001025 "kaweth/new_code.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 100,
1027 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001028 err("Error downloading firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto err_fw;
1030 }
1031
1032 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001033 "kaweth/new_code_fix.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 100,
1035 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001036 err("Error downloading firmware fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto err_fw;
1038 }
1039
1040 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001041 "kaweth/trigger_code.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 126,
1043 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001044 err("Error downloading trigger code (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto err_fw;
1046
1047 }
1048
1049 if ((result = kaweth_download_firmware(kaweth,
David Woodhouse79682492008-05-29 17:17:17 +03001050 "kaweth/trigger_code_fix.bin",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 126,
1052 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001053 err("Error downloading trigger code fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 goto err_fw;
1055 }
1056
1057
1058 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001059 err("Error triggering firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto err_fw;
1061 }
1062
1063 /* Device will now disappear for a moment... */
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001064 dev_info(&intf->dev, "Firmware loaded. I'll be back...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065err_fw:
1066 free_page((unsigned long)kaweth->firmware_buf);
1067 free_netdev(netdev);
1068 return -EIO;
1069 }
1070
1071 result = kaweth_read_configuration(kaweth);
1072
1073 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001074 err("Error reading configuration (%d), no net device created", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto err_free_netdev;
1076 }
1077
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001078 dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
1079 dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1080 dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
1081 dev_info(&intf->dev, "Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 (int)kaweth->configuration.hw_addr[0],
1083 (int)kaweth->configuration.hw_addr[1],
1084 (int)kaweth->configuration.hw_addr[2],
1085 (int)kaweth->configuration.hw_addr[3],
1086 (int)kaweth->configuration.hw_addr[4],
1087 (int)kaweth->configuration.hw_addr[5]);
1088
1089 if(!memcmp(&kaweth->configuration.hw_addr,
1090 &bcast_addr,
1091 sizeof(bcast_addr))) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001092 err("Firmware not functioning properly, no net device created");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto err_free_netdev;
1094 }
1095
1096 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001097 dbg("Error setting URB size");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto err_free_netdev;
1099 }
1100
1101 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001102 err("Error setting SOFS wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 goto err_free_netdev;
1104 }
1105
1106 result = kaweth_set_receive_filter(kaweth,
1107 KAWETH_PACKET_FILTER_DIRECTED |
1108 KAWETH_PACKET_FILTER_BROADCAST |
1109 KAWETH_PACKET_FILTER_MULTICAST);
1110
1111 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001112 err("Error setting receive filter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto err_free_netdev;
1114 }
1115
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001116 dbg("Initializing net device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001118 kaweth->intf = intf;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1121 if (!kaweth->tx_urb)
1122 goto err_free_netdev;
1123 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1124 if (!kaweth->rx_urb)
1125 goto err_only_tx;
1126 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1127 if (!kaweth->irq_urb)
1128 goto err_tx_and_rx;
1129
1130 kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
1131 INTBUFFERSIZE,
1132 GFP_KERNEL,
1133 &kaweth->intbufferhandle);
1134 if (!kaweth->intbuffer)
1135 goto err_tx_and_rx_and_irq;
1136 kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
1137 KAWETH_BUF_SIZE,
1138 GFP_KERNEL,
1139 &kaweth->rxbufferhandle);
1140 if (!kaweth->rx_buf)
1141 goto err_all_but_rxbuf;
1142
1143 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1144 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1145 sizeof(kaweth->configuration.hw_addr));
1146
1147 netdev->open = kaweth_open;
1148 netdev->stop = kaweth_close;
1149
1150 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1151 netdev->tx_timeout = kaweth_tx_timeout;
1152
1153 netdev->hard_start_xmit = kaweth_start_xmit;
1154 netdev->set_multicast_list = kaweth_set_rx_mode;
1155 netdev->get_stats = kaweth_netdev_stats;
1156 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1157 SET_ETHTOOL_OPS(netdev, &ops);
1158
1159 /* kaweth is zeroed as part of alloc_netdev */
1160
David Howellsc4028952006-11-22 14:57:56 +00001161 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 usb_set_intfdata(intf, kaweth);
1164
1165#if 0
1166// dma_supported() is deeply broken on almost all architectures
1167 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1168 kaweth->net->features |= NETIF_F_HIGHDMA;
1169#endif
1170
1171 SET_NETDEV_DEV(netdev, &intf->dev);
1172 if (register_netdev(netdev) != 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001173 err("Error registering netdev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto err_intfdata;
1175 }
1176
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001177 dev_info(&intf->dev, "kaweth interface created at %s\n",
1178 kaweth->net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001180 dbg("Kaweth probe returning.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 return 0;
1183
1184err_intfdata:
1185 usb_set_intfdata(intf, NULL);
1186 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1187err_all_but_rxbuf:
1188 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1189err_tx_and_rx_and_irq:
1190 usb_free_urb(kaweth->irq_urb);
1191err_tx_and_rx:
1192 usb_free_urb(kaweth->rx_urb);
1193err_only_tx:
1194 usb_free_urb(kaweth->tx_urb);
1195err_free_netdev:
1196 free_netdev(netdev);
1197
1198 return -EIO;
1199}
1200
1201/****************************************************************
1202 * kaweth_disconnect
1203 ****************************************************************/
1204static void kaweth_disconnect(struct usb_interface *intf)
1205{
1206 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1207 struct net_device *netdev;
1208
Greg Kroah-Hartman880c9c62008-08-18 13:21:04 -07001209 dev_info(&intf->dev, "Unregistering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 usb_set_intfdata(intf, NULL);
1212 if (!kaweth) {
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -07001213 dev_warn(&intf->dev, "unregistering non-existant device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return;
1215 }
1216 netdev = kaweth->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001218 dbg("Unregistering net device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unregister_netdev(netdev);
1220
1221 usb_free_urb(kaweth->rx_urb);
1222 usb_free_urb(kaweth->tx_urb);
1223 usb_free_urb(kaweth->irq_urb);
1224
1225 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1226 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1227
1228 free_netdev(netdev);
1229}
1230
1231
1232// FIXME this completion stuff is a modified clone of
1233// an OLD version of some stuff in usb.c ...
1234struct usb_api_data {
1235 wait_queue_head_t wqh;
1236 int done;
1237};
1238
1239/*-------------------------------------------------------------------*
1240 * completion handler for compatibility wrappers (sync control/bulk) *
1241 *-------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +01001242static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1245
1246 awd->done=1;
1247 wake_up(&awd->wqh);
1248}
1249
1250/*-------------------------------------------------------------------*
1251 * COMPATIBILITY STUFF *
1252 *-------------------------------------------------------------------*/
1253
1254// Starts urb and waits for completion or timeout
1255static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1256{
1257 struct usb_api_data awd;
1258 int status;
1259
1260 init_waitqueue_head(&awd.wqh);
1261 awd.done = 0;
1262
1263 urb->context = &awd;
1264 status = usb_submit_urb(urb, GFP_NOIO);
1265 if (status) {
1266 // something went wrong
1267 usb_free_urb(urb);
1268 return status;
1269 }
1270
1271 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1272 // timeout
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -07001273 dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 usb_kill_urb(urb); // remove urb safely
1275 status = -ETIMEDOUT;
1276 }
1277 else {
1278 status = urb->status;
1279 }
1280
1281 if (actual_length) {
1282 *actual_length = urb->actual_length;
1283 }
1284
1285 usb_free_urb(urb);
1286 return status;
1287}
1288
1289/*-------------------------------------------------------------------*/
1290// returns status (negative) or length (positive)
1291static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1292 unsigned int pipe,
1293 struct usb_ctrlrequest *cmd, void *data,
1294 int len, int timeout)
1295{
1296 struct urb *urb;
1297 int retv;
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001298 int length = 0; /* shut up GCC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 urb = usb_alloc_urb(0, GFP_NOIO);
1301 if (!urb)
1302 return -ENOMEM;
1303
1304 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1305 len, usb_api_blocking_completion, NULL);
1306
1307 retv = usb_start_wait_urb(urb, timeout, &length);
1308 if (retv < 0) {
1309 return retv;
1310 }
1311 else {
1312 return length;
1313 }
1314}
1315
1316
1317/****************************************************************
1318 * kaweth_init
1319 ****************************************************************/
1320static int __init kaweth_init(void)
1321{
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001322 dbg("Driver loading");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return usb_register(&kaweth_driver);
1324}
1325
1326/****************************************************************
1327 * kaweth_exit
1328 ****************************************************************/
1329static void __exit kaweth_exit(void)
1330{
1331 usb_deregister(&kaweth_driver);
1332}
1333
1334module_init(kaweth_init);
1335module_exit(kaweth_exit);
1336
1337
1338
1339
1340
1341
1342
1343
1344