blob: 36a989160a684ccba108d06a706008d5500a8941 [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>
49#include <linux/sched.h>
50#include <linux/slab.h>
51#include <linux/string.h>
52#include <linux/init.h>
53#include <linux/delay.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/usb.h>
57#include <linux/types.h>
58#include <linux/ethtool.h>
59#include <linux/pci.h>
60#include <linux/dma-mapping.h>
61#include <linux/wait.h>
62#include <asm/uaccess.h>
63#include <asm/semaphore.h>
64#include <asm/byteorder.h>
65
66#undef DEBUG
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "kawethfw.h"
69
70#define KAWETH_MTU 1514
71#define KAWETH_BUF_SIZE 1664
72#define KAWETH_TX_TIMEOUT (5 * HZ)
73#define KAWETH_SCRATCH_SIZE 32
74#define KAWETH_FIRMWARE_BUF_SIZE 4096
75#define KAWETH_CONTROL_TIMEOUT (30 * HZ)
76
77#define KAWETH_STATUS_BROKEN 0x0000001
78#define KAWETH_STATUS_CLOSING 0x0000002
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +020079#define KAWETH_STATUS_SUSPENDING 0x0000004
80
81#define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
84#define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
85#define KAWETH_PACKET_FILTER_DIRECTED 0x04
86#define KAWETH_PACKET_FILTER_BROADCAST 0x08
87#define KAWETH_PACKET_FILTER_MULTICAST 0x10
88
89/* Table 7 */
90#define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
91#define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
92#define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
93#define KAWETH_COMMAND_STATISTICS 0x03
94#define KAWETH_COMMAND_SET_TEMP_MAC 0x06
95#define KAWETH_COMMAND_GET_TEMP_MAC 0x07
96#define KAWETH_COMMAND_SET_URB_SIZE 0x08
97#define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
98#define KAWETH_COMMAND_SCAN 0xFF
99
100#define KAWETH_SOFS_TO_WAIT 0x05
101
102#define INTBUFFERSIZE 4
103
104#define STATE_OFFSET 0
105#define STATE_MASK 0x40
106#define STATE_SHIFT 5
107
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200108#define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
112MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
113MODULE_LICENSE("GPL");
114
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,
391 __u8 *data,
392 __u16 data_len,
393 __u8 interrupt,
394 __u8 type)
395{
396 if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200397 err("Firmware too big: %d", data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENOSPC;
399 }
400
401 memcpy(kaweth->firmware_buf, data, data_len);
402
403 kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
404 kaweth->firmware_buf[3] = data_len >> 8;
405 kaweth->firmware_buf[4] = type;
406 kaweth->firmware_buf[5] = interrupt;
407
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200408 dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 kaweth->firmware_buf[2]);
410
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200411 dbg("Downloading firmware at %p to kaweth device at %p",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 data,
413 kaweth);
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200414 dbg("Firmware length: %d", data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return kaweth_control(kaweth,
417 usb_sndctrlpipe(kaweth->dev, 0),
418 KAWETH_COMMAND_SCAN,
419 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
420 0,
421 0,
422 (void *)kaweth->firmware_buf,
423 data_len,
424 KAWETH_CONTROL_TIMEOUT);
425}
426
427/****************************************************************
428 * kaweth_trigger_firmware
429 ****************************************************************/
430static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
431 __u8 interrupt)
432{
433 kaweth->firmware_buf[0] = 0xB6;
434 kaweth->firmware_buf[1] = 0xC3;
435 kaweth->firmware_buf[2] = 0x01;
436 kaweth->firmware_buf[3] = 0x00;
437 kaweth->firmware_buf[4] = 0x06;
438 kaweth->firmware_buf[5] = interrupt;
439 kaweth->firmware_buf[6] = 0x00;
440 kaweth->firmware_buf[7] = 0x00;
441
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200442 dbg("Triggering firmware");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return kaweth_control(kaweth,
445 usb_sndctrlpipe(kaweth->dev, 0),
446 KAWETH_COMMAND_SCAN,
447 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
448 0,
449 0,
450 (void *)kaweth->firmware_buf,
451 8,
452 KAWETH_CONTROL_TIMEOUT);
453}
454
455/****************************************************************
456 * kaweth_reset
457 ****************************************************************/
458static int kaweth_reset(struct kaweth_device *kaweth)
459{
460 int result;
461
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200462 dbg("kaweth_reset(%p)", kaweth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 result = kaweth_control(kaweth,
464 usb_sndctrlpipe(kaweth->dev, 0),
465 USB_REQ_SET_CONFIGURATION,
466 0,
467 kaweth->dev->config[0].desc.bConfigurationValue,
468 0,
469 NULL,
470 0,
471 KAWETH_CONTROL_TIMEOUT);
472
Guillaume GOURAT /f2d45cd2005-10-21 14:01:35 +0200473 mdelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200475 dbg("kaweth_reset() returns %d.",result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return result;
478}
479
David Howells7d12e782006-10-05 14:55:46 +0100480static void kaweth_usb_receive(struct urb *);
Al Viro55016f12005-10-21 03:21:58 -0400481static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/****************************************************************
484 int_callback
485*****************************************************************/
486
Al Viro55016f12005-10-21 03:21:58 -0400487static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 int status;
490
491 status = usb_submit_urb (kaweth->irq_urb, mf);
492 if (unlikely(status == -ENOMEM)) {
493 kaweth->suspend_lowmem_ctrl = 1;
494 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
495 } else {
496 kaweth->suspend_lowmem_ctrl = 0;
497 }
498
499 if (status)
500 err ("can't resubmit intr, %s-%s, status %d",
501 kaweth->dev->bus->bus_name,
502 kaweth->dev->devpath, status);
503}
504
David Howells7d12e782006-10-05 14:55:46 +0100505static void int_callback(struct urb *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct kaweth_device *kaweth = u->context;
508 int act_state;
509
510 switch (u->status) {
511 case 0: /* success */
512 break;
513 case -ECONNRESET: /* unlink */
514 case -ENOENT:
515 case -ESHUTDOWN:
516 return;
517 /* -EPIPE: should clear the halt */
518 default: /* error */
519 goto resubmit;
520 }
521
522 /* we check the link state to report changes */
523 if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
Oliver Neukum58125f92005-06-15 22:26:38 -0700524 if (act_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 netif_carrier_on(kaweth->net);
526 else
527 netif_carrier_off(kaweth->net);
528
529 kaweth->linkstate = act_state;
530 }
531resubmit:
532 kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
533}
534
David Howellsc4028952006-11-22 14:57:56 +0000535static void kaweth_resubmit_tl(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
David Howellsc4028952006-11-22 14:57:56 +0000537 struct kaweth_device *kaweth =
538 container_of(work, struct kaweth_device, lowmem_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200540 if (IS_BLOCKED(kaweth->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return;
542
543 if (kaweth->suspend_lowmem_rx)
544 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
545
546 if (kaweth->suspend_lowmem_ctrl)
547 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
548}
549
550
551/****************************************************************
552 * kaweth_resubmit_rx_urb
553 ****************************************************************/
554static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
Al Viro55016f12005-10-21 03:21:58 -0400555 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 int result;
558
559 usb_fill_bulk_urb(kaweth->rx_urb,
560 kaweth->dev,
561 usb_rcvbulkpipe(kaweth->dev, 1),
562 kaweth->rx_buf,
563 KAWETH_BUF_SIZE,
564 kaweth_usb_receive,
565 kaweth);
566 kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
567 kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
568
569 if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
570 if (result == -ENOMEM) {
571 kaweth->suspend_lowmem_rx = 1;
572 schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
573 }
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200574 err("resubmitting rx_urb %d failed", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 } else {
576 kaweth->suspend_lowmem_rx = 0;
577 }
578
579 return result;
580}
581
582static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
583
584/****************************************************************
585 * kaweth_usb_receive
586 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100587static void kaweth_usb_receive(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct kaweth_device *kaweth = urb->context;
590 struct net_device *net = kaweth->net;
591
592 int count = urb->actual_length;
593 int count2 = urb->transfer_buffer_length;
594
595 __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
596
597 struct sk_buff *skb;
598
599 if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
600 /* we are killed - set a flag and wake the disconnect handler */
601 {
602 kaweth->end = 1;
603 wake_up(&kaweth->term_wait);
604 return;
605 }
606
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200607 spin_lock(&kaweth->device_lock);
608 if (IS_BLOCKED(kaweth->status)) {
609 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200611 }
612 spin_unlock(&kaweth->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if(urb->status && urb->status != -EREMOTEIO && count != 1) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200615 err("%s RX status: %d count: %d packet_len: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 net->name,
617 urb->status,
618 count,
619 (int)pkt_len);
620 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
621 return;
622 }
623
624 if(kaweth->net && (count > 2)) {
625 if(pkt_len > (count - 2)) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200626 err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
627 err("Packet len & 2047: %x", pkt_len & 2047);
628 err("Count 2: %x", count2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
630 return;
631 }
632
633 if(!(skb = dev_alloc_skb(pkt_len+2))) {
634 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
635 return;
636 }
637
638 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
639
640 skb->dev = net;
641
642 eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
643
644 skb_put(skb, pkt_len);
645
646 skb->protocol = eth_type_trans(skb, net);
647
648 netif_rx(skb);
649
650 kaweth->stats.rx_packets++;
651 kaweth->stats.rx_bytes += pkt_len;
652 }
653
654 kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
655}
656
657/****************************************************************
658 * kaweth_open
659 ****************************************************************/
660static int kaweth_open(struct net_device *net)
661{
662 struct kaweth_device *kaweth = netdev_priv(net);
663 int res;
664
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200665 dbg("Opening network device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100667 res = usb_autopm_get_interface(kaweth->intf);
668 if (res) {
669 err("Interface cannot be resumed.");
670 return -EIO;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
673 if (res)
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100674 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 usb_fill_int_urb(
677 kaweth->irq_urb,
678 kaweth->dev,
679 usb_rcvintpipe(kaweth->dev, 3),
680 kaweth->intbuffer,
681 INTBUFFERSIZE,
682 int_callback,
683 kaweth,
684 250); /* overriding the descriptor */
685 kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
686 kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
687
688 res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
689 if (res) {
690 usb_kill_urb(kaweth->rx_urb);
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100691 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200693 kaweth->opened = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 netif_start_queue(net);
696
697 kaweth_async_set_rx_mode(kaweth);
698 return 0;
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100699
700err_out:
701 usb_autopm_enable(kaweth->intf);
702 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/****************************************************************
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100706 * kaweth_kill_urbs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ****************************************************************/
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200708static void kaweth_kill_urbs(struct kaweth_device *kaweth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 usb_kill_urb(kaweth->irq_urb);
711 usb_kill_urb(kaweth->rx_urb);
Herbert Xud23b5362005-11-17 09:47:45 -0800712 usb_kill_urb(kaweth->tx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 flush_scheduled_work();
715
716 /* a scheduled work may have resubmitted,
717 we hit them again */
718 usb_kill_urb(kaweth->irq_urb);
719 usb_kill_urb(kaweth->rx_urb);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200720}
721
722/****************************************************************
723 * kaweth_close
724 ****************************************************************/
725static int kaweth_close(struct net_device *net)
726{
727 struct kaweth_device *kaweth = netdev_priv(net);
728
729 netif_stop_queue(net);
730 kaweth->opened = 0;
731
732 kaweth->status |= KAWETH_STATUS_CLOSING;
733
734 kaweth_kill_urbs(kaweth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 kaweth->status &= ~KAWETH_STATUS_CLOSING;
737
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100738 usb_autopm_enable(kaweth->intf);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741}
742
743static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
744{
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100745 struct kaweth_device *kaweth = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 strlcpy(info->driver, driver_name, sizeof(info->driver));
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100748 usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
749}
750
751static u32 kaweth_get_link(struct net_device *dev)
752{
753 struct kaweth_device *kaweth = netdev_priv(dev);
754
755 return kaweth->linkstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static struct ethtool_ops ops = {
Oliver Neukumb3ebd522007-01-16 12:01:26 +0100759 .get_drvinfo = kaweth_get_drvinfo,
760 .get_link = kaweth_get_link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761};
762
763/****************************************************************
764 * kaweth_usb_transmit_complete
765 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100766static void kaweth_usb_transmit_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct kaweth_device *kaweth = urb->context;
769 struct sk_buff *skb = kaweth->tx_skb;
770
771 if (unlikely(urb->status != 0))
772 if (urb->status != -ENOENT)
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200773 dbg("%s: TX status %d.", kaweth->net->name, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 netif_wake_queue(kaweth->net);
776 dev_kfree_skb_irq(skb);
777}
778
779/****************************************************************
780 * kaweth_start_xmit
781 ****************************************************************/
782static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
783{
784 struct kaweth_device *kaweth = netdev_priv(net);
785 __le16 *private_header;
786
787 int res;
788
789 spin_lock(&kaweth->device_lock);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kaweth_async_set_rx_mode(kaweth);
792 netif_stop_queue(net);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200793 if (IS_BLOCKED(kaweth->status)) {
794 goto skip;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* We now decide whether we can put our special header into the sk_buff */
798 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
799 /* no such luck - we make our own */
800 struct sk_buff *copied_skb;
801 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
802 dev_kfree_skb_irq(skb);
803 skb = copied_skb;
804 if (!copied_skb) {
805 kaweth->stats.tx_errors++;
806 netif_start_queue(net);
807 spin_unlock(&kaweth->device_lock);
808 return 0;
809 }
810 }
811
812 private_header = (__le16 *)__skb_push(skb, 2);
813 *private_header = cpu_to_le16(skb->len-2);
814 kaweth->tx_skb = skb;
815
816 usb_fill_bulk_urb(kaweth->tx_urb,
817 kaweth->dev,
818 usb_sndbulkpipe(kaweth->dev, 2),
819 private_header,
820 skb->len,
821 kaweth_usb_transmit_complete,
822 kaweth);
823 kaweth->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
826 {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200827 warn("kaweth failed tx_urb %d", res);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200828skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 kaweth->stats.tx_errors++;
830
831 netif_start_queue(net);
832 dev_kfree_skb_irq(skb);
833 }
834 else
835 {
836 kaweth->stats.tx_packets++;
837 kaweth->stats.tx_bytes += skb->len;
838 net->trans_start = jiffies;
839 }
840
841 spin_unlock(&kaweth->device_lock);
842
843 return 0;
844}
845
846/****************************************************************
847 * kaweth_set_rx_mode
848 ****************************************************************/
849static void kaweth_set_rx_mode(struct net_device *net)
850{
851 struct kaweth_device *kaweth = netdev_priv(net);
852
853 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
854 KAWETH_PACKET_FILTER_BROADCAST |
855 KAWETH_PACKET_FILTER_MULTICAST;
856
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200857 dbg("Setting Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 netif_stop_queue(net);
860
861 if (net->flags & IFF_PROMISC) {
862 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
863 }
864 else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
865 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
866 }
867
868 kaweth->packet_filter_bitmap = packet_filter_bitmap;
869 netif_wake_queue(net);
870}
871
872/****************************************************************
873 * kaweth_async_set_rx_mode
874 ****************************************************************/
875static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
876{
877 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
878 kaweth->packet_filter_bitmap = 0;
879 if (packet_filter_bitmap == 0)
880 return;
881
882 {
883 int result;
884 result = kaweth_control(kaweth,
885 usb_sndctrlpipe(kaweth->dev, 0),
886 KAWETH_COMMAND_SET_PACKET_FILTER,
887 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
888 packet_filter_bitmap,
889 0,
890 (void *)&kaweth->scratch,
891 0,
892 KAWETH_CONTROL_TIMEOUT);
893
894 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200895 err("Failed to set Rx mode: %d", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 else {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200898 dbg("Set Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 }
901}
902
903/****************************************************************
904 * kaweth_netdev_stats
905 ****************************************************************/
906static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
907{
908 struct kaweth_device *kaweth = netdev_priv(dev);
909 return &kaweth->stats;
910}
911
912/****************************************************************
913 * kaweth_tx_timeout
914 ****************************************************************/
915static void kaweth_tx_timeout(struct net_device *net)
916{
917 struct kaweth_device *kaweth = netdev_priv(net);
918
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200919 warn("%s: Tx timed out. Resetting.", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 kaweth->stats.tx_errors++;
921 net->trans_start = jiffies;
922
923 usb_unlink_urb(kaweth->tx_urb);
924}
925
926/****************************************************************
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200927 * kaweth_suspend
928 ****************************************************************/
929static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
930{
931 struct kaweth_device *kaweth = usb_get_intfdata(intf);
932 unsigned long flags;
933
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100934 dbg("Suspending device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200935 spin_lock_irqsave(&kaweth->device_lock, flags);
936 kaweth->status |= KAWETH_STATUS_SUSPENDING;
937 spin_unlock_irqrestore(&kaweth->device_lock, flags);
938
939 kaweth_kill_urbs(kaweth);
940 return 0;
941}
942
943/****************************************************************
944 * kaweth_resume
945 ****************************************************************/
946static int kaweth_resume(struct usb_interface *intf)
947{
948 struct kaweth_device *kaweth = usb_get_intfdata(intf);
949 unsigned long flags;
950
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100951 dbg("Resuming device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200952 spin_lock_irqsave(&kaweth->device_lock, flags);
953 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
954 spin_unlock_irqrestore(&kaweth->device_lock, flags);
955
956 if (!kaweth->opened)
957 return 0;
958 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
959 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
960
961 return 0;
962}
963
964/****************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * kaweth_probe
966 ****************************************************************/
967static int kaweth_probe(
968 struct usb_interface *intf,
969 const struct usb_device_id *id /* from id_table */
970 )
971{
972 struct usb_device *dev = interface_to_usbdev(intf);
973 struct kaweth_device *kaweth;
974 struct net_device *netdev;
975 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
976 int result = 0;
977
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200978 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 dev->devnum,
980 le16_to_cpu(dev->descriptor.idVendor),
981 le16_to_cpu(dev->descriptor.idProduct),
982 le16_to_cpu(dev->descriptor.bcdDevice));
983
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200984 dbg("Device at %p", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200986 dbg("Descriptor length: %x type: %x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 (int)dev->descriptor.bLength,
988 (int)dev->descriptor.bDescriptorType);
989
990 netdev = alloc_etherdev(sizeof(*kaweth));
991 if (!netdev)
992 return -ENOMEM;
993
994 kaweth = netdev_priv(netdev);
995 kaweth->dev = dev;
996 kaweth->net = netdev;
997
998 spin_lock_init(&kaweth->device_lock);
999 init_waitqueue_head(&kaweth->term_wait);
1000
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001001 dbg("Resetting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 kaweth_reset(kaweth);
1004
1005 /*
1006 * If high byte of bcdDevice is nonzero, firmware is already
1007 * downloaded. Don't try to do it again, or we'll hang the device.
1008 */
1009
1010 if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001011 info("Firmware present in device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 } else {
1013 /* Download the firmware */
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001014 info("Downloading firmware...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1016 if ((result = kaweth_download_firmware(kaweth,
1017 kaweth_new_code,
1018 len_kaweth_new_code,
1019 100,
1020 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001021 err("Error downloading firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto err_fw;
1023 }
1024
1025 if ((result = kaweth_download_firmware(kaweth,
1026 kaweth_new_code_fix,
1027 len_kaweth_new_code_fix,
1028 100,
1029 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001030 err("Error downloading firmware fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto err_fw;
1032 }
1033
1034 if ((result = kaweth_download_firmware(kaweth,
1035 kaweth_trigger_code,
1036 len_kaweth_trigger_code,
1037 126,
1038 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001039 err("Error downloading trigger code (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto err_fw;
1041
1042 }
1043
1044 if ((result = kaweth_download_firmware(kaweth,
1045 kaweth_trigger_code_fix,
1046 len_kaweth_trigger_code_fix,
1047 126,
1048 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001049 err("Error downloading trigger code fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto err_fw;
1051 }
1052
1053
1054 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001055 err("Error triggering firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto err_fw;
1057 }
1058
1059 /* Device will now disappear for a moment... */
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001060 info("Firmware loaded. I'll be back...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061err_fw:
1062 free_page((unsigned long)kaweth->firmware_buf);
1063 free_netdev(netdev);
1064 return -EIO;
1065 }
1066
1067 result = kaweth_read_configuration(kaweth);
1068
1069 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001070 err("Error reading configuration (%d), no net device created", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto err_free_netdev;
1072 }
1073
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001074 info("Statistics collection: %x", kaweth->configuration.statistics_mask);
1075 info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1076 info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
1077 info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 (int)kaweth->configuration.hw_addr[0],
1079 (int)kaweth->configuration.hw_addr[1],
1080 (int)kaweth->configuration.hw_addr[2],
1081 (int)kaweth->configuration.hw_addr[3],
1082 (int)kaweth->configuration.hw_addr[4],
1083 (int)kaweth->configuration.hw_addr[5]);
1084
1085 if(!memcmp(&kaweth->configuration.hw_addr,
1086 &bcast_addr,
1087 sizeof(bcast_addr))) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001088 err("Firmware not functioning properly, no net device created");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto err_free_netdev;
1090 }
1091
1092 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001093 dbg("Error setting URB size");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto err_free_netdev;
1095 }
1096
1097 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001098 err("Error setting SOFS wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto err_free_netdev;
1100 }
1101
1102 result = kaweth_set_receive_filter(kaweth,
1103 KAWETH_PACKET_FILTER_DIRECTED |
1104 KAWETH_PACKET_FILTER_BROADCAST |
1105 KAWETH_PACKET_FILTER_MULTICAST);
1106
1107 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001108 err("Error setting receive filter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto err_free_netdev;
1110 }
1111
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001112 dbg("Initializing net device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001114 kaweth->intf = intf;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1117 if (!kaweth->tx_urb)
1118 goto err_free_netdev;
1119 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1120 if (!kaweth->rx_urb)
1121 goto err_only_tx;
1122 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1123 if (!kaweth->irq_urb)
1124 goto err_tx_and_rx;
1125
1126 kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
1127 INTBUFFERSIZE,
1128 GFP_KERNEL,
1129 &kaweth->intbufferhandle);
1130 if (!kaweth->intbuffer)
1131 goto err_tx_and_rx_and_irq;
1132 kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
1133 KAWETH_BUF_SIZE,
1134 GFP_KERNEL,
1135 &kaweth->rxbufferhandle);
1136 if (!kaweth->rx_buf)
1137 goto err_all_but_rxbuf;
1138
1139 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1140 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1141 sizeof(kaweth->configuration.hw_addr));
1142
1143 netdev->open = kaweth_open;
1144 netdev->stop = kaweth_close;
1145
1146 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1147 netdev->tx_timeout = kaweth_tx_timeout;
1148
1149 netdev->hard_start_xmit = kaweth_start_xmit;
1150 netdev->set_multicast_list = kaweth_set_rx_mode;
1151 netdev->get_stats = kaweth_netdev_stats;
1152 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1153 SET_ETHTOOL_OPS(netdev, &ops);
1154
1155 /* kaweth is zeroed as part of alloc_netdev */
1156
David Howellsc4028952006-11-22 14:57:56 +00001157 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 SET_MODULE_OWNER(netdev);
1160
1161 usb_set_intfdata(intf, kaweth);
1162
1163#if 0
1164// dma_supported() is deeply broken on almost all architectures
1165 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1166 kaweth->net->features |= NETIF_F_HIGHDMA;
1167#endif
1168
1169 SET_NETDEV_DEV(netdev, &intf->dev);
1170 if (register_netdev(netdev) != 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001171 err("Error registering netdev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto err_intfdata;
1173 }
1174
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001175 info("kaweth interface created at %s", kaweth->net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001177 dbg("Kaweth probe returning.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 return 0;
1180
1181err_intfdata:
1182 usb_set_intfdata(intf, NULL);
1183 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1184err_all_but_rxbuf:
1185 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1186err_tx_and_rx_and_irq:
1187 usb_free_urb(kaweth->irq_urb);
1188err_tx_and_rx:
1189 usb_free_urb(kaweth->rx_urb);
1190err_only_tx:
1191 usb_free_urb(kaweth->tx_urb);
1192err_free_netdev:
1193 free_netdev(netdev);
1194
1195 return -EIO;
1196}
1197
1198/****************************************************************
1199 * kaweth_disconnect
1200 ****************************************************************/
1201static void kaweth_disconnect(struct usb_interface *intf)
1202{
1203 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1204 struct net_device *netdev;
1205
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001206 info("Unregistering");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 usb_set_intfdata(intf, NULL);
1209 if (!kaweth) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001210 warn("unregistering non-existant device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 }
1213 netdev = kaweth->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001215 dbg("Unregistering net device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unregister_netdev(netdev);
1217
1218 usb_free_urb(kaweth->rx_urb);
1219 usb_free_urb(kaweth->tx_urb);
1220 usb_free_urb(kaweth->irq_urb);
1221
1222 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1223 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1224
1225 free_netdev(netdev);
1226}
1227
1228
1229// FIXME this completion stuff is a modified clone of
1230// an OLD version of some stuff in usb.c ...
1231struct usb_api_data {
1232 wait_queue_head_t wqh;
1233 int done;
1234};
1235
1236/*-------------------------------------------------------------------*
1237 * completion handler for compatibility wrappers (sync control/bulk) *
1238 *-------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +01001239static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1242
1243 awd->done=1;
1244 wake_up(&awd->wqh);
1245}
1246
1247/*-------------------------------------------------------------------*
1248 * COMPATIBILITY STUFF *
1249 *-------------------------------------------------------------------*/
1250
1251// Starts urb and waits for completion or timeout
1252static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1253{
1254 struct usb_api_data awd;
1255 int status;
1256
1257 init_waitqueue_head(&awd.wqh);
1258 awd.done = 0;
1259
1260 urb->context = &awd;
1261 status = usb_submit_urb(urb, GFP_NOIO);
1262 if (status) {
1263 // something went wrong
1264 usb_free_urb(urb);
1265 return status;
1266 }
1267
1268 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1269 // timeout
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001270 warn("usb_control/bulk_msg: timeout");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 usb_kill_urb(urb); // remove urb safely
1272 status = -ETIMEDOUT;
1273 }
1274 else {
1275 status = urb->status;
1276 }
1277
1278 if (actual_length) {
1279 *actual_length = urb->actual_length;
1280 }
1281
1282 usb_free_urb(urb);
1283 return status;
1284}
1285
1286/*-------------------------------------------------------------------*/
1287// returns status (negative) or length (positive)
1288static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1289 unsigned int pipe,
1290 struct usb_ctrlrequest *cmd, void *data,
1291 int len, int timeout)
1292{
1293 struct urb *urb;
1294 int retv;
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001295 int length = 0; /* shut up GCC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 urb = usb_alloc_urb(0, GFP_NOIO);
1298 if (!urb)
1299 return -ENOMEM;
1300
1301 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1302 len, usb_api_blocking_completion, NULL);
1303
1304 retv = usb_start_wait_urb(urb, timeout, &length);
1305 if (retv < 0) {
1306 return retv;
1307 }
1308 else {
1309 return length;
1310 }
1311}
1312
1313
1314/****************************************************************
1315 * kaweth_init
1316 ****************************************************************/
1317static int __init kaweth_init(void)
1318{
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001319 dbg("Driver loading");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return usb_register(&kaweth_driver);
1321}
1322
1323/****************************************************************
1324 * kaweth_exit
1325 ****************************************************************/
1326static void __exit kaweth_exit(void)
1327{
1328 usb_deregister(&kaweth_driver);
1329}
1330
1331module_init(kaweth_init);
1332module_exit(kaweth_exit);
1333
1334
1335
1336
1337
1338
1339
1340
1341