blob: f29eed30e4f254c3d8f2daf969e681bc4557f49b [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{
745
746 strlcpy(info->driver, driver_name, sizeof(info->driver));
747}
748
749static struct ethtool_ops ops = {
750 .get_drvinfo = kaweth_get_drvinfo
751};
752
753/****************************************************************
754 * kaweth_usb_transmit_complete
755 ****************************************************************/
David Howells7d12e782006-10-05 14:55:46 +0100756static void kaweth_usb_transmit_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct kaweth_device *kaweth = urb->context;
759 struct sk_buff *skb = kaweth->tx_skb;
760
761 if (unlikely(urb->status != 0))
762 if (urb->status != -ENOENT)
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200763 dbg("%s: TX status %d.", kaweth->net->name, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 netif_wake_queue(kaweth->net);
766 dev_kfree_skb_irq(skb);
767}
768
769/****************************************************************
770 * kaweth_start_xmit
771 ****************************************************************/
772static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
773{
774 struct kaweth_device *kaweth = netdev_priv(net);
775 __le16 *private_header;
776
777 int res;
778
779 spin_lock(&kaweth->device_lock);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 kaweth_async_set_rx_mode(kaweth);
782 netif_stop_queue(net);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200783 if (IS_BLOCKED(kaweth->status)) {
784 goto skip;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* We now decide whether we can put our special header into the sk_buff */
788 if (skb_cloned(skb) || skb_headroom(skb) < 2) {
789 /* no such luck - we make our own */
790 struct sk_buff *copied_skb;
791 copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
792 dev_kfree_skb_irq(skb);
793 skb = copied_skb;
794 if (!copied_skb) {
795 kaweth->stats.tx_errors++;
796 netif_start_queue(net);
797 spin_unlock(&kaweth->device_lock);
798 return 0;
799 }
800 }
801
802 private_header = (__le16 *)__skb_push(skb, 2);
803 *private_header = cpu_to_le16(skb->len-2);
804 kaweth->tx_skb = skb;
805
806 usb_fill_bulk_urb(kaweth->tx_urb,
807 kaweth->dev,
808 usb_sndbulkpipe(kaweth->dev, 2),
809 private_header,
810 skb->len,
811 kaweth_usb_transmit_complete,
812 kaweth);
813 kaweth->end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
816 {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200817 warn("kaweth failed tx_urb %d", res);
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200818skip:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 kaweth->stats.tx_errors++;
820
821 netif_start_queue(net);
822 dev_kfree_skb_irq(skb);
823 }
824 else
825 {
826 kaweth->stats.tx_packets++;
827 kaweth->stats.tx_bytes += skb->len;
828 net->trans_start = jiffies;
829 }
830
831 spin_unlock(&kaweth->device_lock);
832
833 return 0;
834}
835
836/****************************************************************
837 * kaweth_set_rx_mode
838 ****************************************************************/
839static void kaweth_set_rx_mode(struct net_device *net)
840{
841 struct kaweth_device *kaweth = netdev_priv(net);
842
843 __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
844 KAWETH_PACKET_FILTER_BROADCAST |
845 KAWETH_PACKET_FILTER_MULTICAST;
846
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200847 dbg("Setting Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 netif_stop_queue(net);
850
851 if (net->flags & IFF_PROMISC) {
852 packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
853 }
854 else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
855 packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
856 }
857
858 kaweth->packet_filter_bitmap = packet_filter_bitmap;
859 netif_wake_queue(net);
860}
861
862/****************************************************************
863 * kaweth_async_set_rx_mode
864 ****************************************************************/
865static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
866{
867 __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
868 kaweth->packet_filter_bitmap = 0;
869 if (packet_filter_bitmap == 0)
870 return;
871
872 {
873 int result;
874 result = kaweth_control(kaweth,
875 usb_sndctrlpipe(kaweth->dev, 0),
876 KAWETH_COMMAND_SET_PACKET_FILTER,
877 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
878 packet_filter_bitmap,
879 0,
880 (void *)&kaweth->scratch,
881 0,
882 KAWETH_CONTROL_TIMEOUT);
883
884 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200885 err("Failed to set Rx mode: %d", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 else {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200888 dbg("Set Rx mode to %d", packet_filter_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 }
891}
892
893/****************************************************************
894 * kaweth_netdev_stats
895 ****************************************************************/
896static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
897{
898 struct kaweth_device *kaweth = netdev_priv(dev);
899 return &kaweth->stats;
900}
901
902/****************************************************************
903 * kaweth_tx_timeout
904 ****************************************************************/
905static void kaweth_tx_timeout(struct net_device *net)
906{
907 struct kaweth_device *kaweth = netdev_priv(net);
908
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200909 warn("%s: Tx timed out. Resetting.", net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 kaweth->stats.tx_errors++;
911 net->trans_start = jiffies;
912
913 usb_unlink_urb(kaweth->tx_urb);
914}
915
916/****************************************************************
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200917 * kaweth_suspend
918 ****************************************************************/
919static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
920{
921 struct kaweth_device *kaweth = usb_get_intfdata(intf);
922 unsigned long flags;
923
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100924 dbg("Suspending device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200925 spin_lock_irqsave(&kaweth->device_lock, flags);
926 kaweth->status |= KAWETH_STATUS_SUSPENDING;
927 spin_unlock_irqrestore(&kaweth->device_lock, flags);
928
929 kaweth_kill_urbs(kaweth);
930 return 0;
931}
932
933/****************************************************************
934 * kaweth_resume
935 ****************************************************************/
936static int kaweth_resume(struct usb_interface *intf)
937{
938 struct kaweth_device *kaweth = usb_get_intfdata(intf);
939 unsigned long flags;
940
Oliver Neukumb98b98f2007-01-16 09:47:12 +0100941 dbg("Resuming device");
Oliver Neukum1a2ea1d2006-10-03 10:30:52 +0200942 spin_lock_irqsave(&kaweth->device_lock, flags);
943 kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
944 spin_unlock_irqrestore(&kaweth->device_lock, flags);
945
946 if (!kaweth->opened)
947 return 0;
948 kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
949 kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
950
951 return 0;
952}
953
954/****************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * kaweth_probe
956 ****************************************************************/
957static int kaweth_probe(
958 struct usb_interface *intf,
959 const struct usb_device_id *id /* from id_table */
960 )
961{
962 struct usb_device *dev = interface_to_usbdev(intf);
963 struct kaweth_device *kaweth;
964 struct net_device *netdev;
965 const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
966 int result = 0;
967
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200968 dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dev->devnum,
970 le16_to_cpu(dev->descriptor.idVendor),
971 le16_to_cpu(dev->descriptor.idProduct),
972 le16_to_cpu(dev->descriptor.bcdDevice));
973
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200974 dbg("Device at %p", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200976 dbg("Descriptor length: %x type: %x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 (int)dev->descriptor.bLength,
978 (int)dev->descriptor.bDescriptorType);
979
980 netdev = alloc_etherdev(sizeof(*kaweth));
981 if (!netdev)
982 return -ENOMEM;
983
984 kaweth = netdev_priv(netdev);
985 kaweth->dev = dev;
986 kaweth->net = netdev;
987
988 spin_lock_init(&kaweth->device_lock);
989 init_waitqueue_head(&kaweth->term_wait);
990
Oliver Neukumfbe2baf2006-09-28 23:36:04 +0200991 dbg("Resetting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 kaweth_reset(kaweth);
994
995 /*
996 * If high byte of bcdDevice is nonzero, firmware is already
997 * downloaded. Don't try to do it again, or we'll hang the device.
998 */
999
1000 if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001001 info("Firmware present in device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 } else {
1003 /* Download the firmware */
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001004 info("Downloading firmware...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
1006 if ((result = kaweth_download_firmware(kaweth,
1007 kaweth_new_code,
1008 len_kaweth_new_code,
1009 100,
1010 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001011 err("Error downloading firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto err_fw;
1013 }
1014
1015 if ((result = kaweth_download_firmware(kaweth,
1016 kaweth_new_code_fix,
1017 len_kaweth_new_code_fix,
1018 100,
1019 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001020 err("Error downloading firmware fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto err_fw;
1022 }
1023
1024 if ((result = kaweth_download_firmware(kaweth,
1025 kaweth_trigger_code,
1026 len_kaweth_trigger_code,
1027 126,
1028 2)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001029 err("Error downloading trigger code (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto err_fw;
1031
1032 }
1033
1034 if ((result = kaweth_download_firmware(kaweth,
1035 kaweth_trigger_code_fix,
1036 len_kaweth_trigger_code_fix,
1037 126,
1038 3)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001039 err("Error downloading trigger code fix (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto err_fw;
1041 }
1042
1043
1044 if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001045 err("Error triggering firmware (%d)", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto err_fw;
1047 }
1048
1049 /* Device will now disappear for a moment... */
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001050 info("Firmware loaded. I'll be back...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051err_fw:
1052 free_page((unsigned long)kaweth->firmware_buf);
1053 free_netdev(netdev);
1054 return -EIO;
1055 }
1056
1057 result = kaweth_read_configuration(kaweth);
1058
1059 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001060 err("Error reading configuration (%d), no net device created", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto err_free_netdev;
1062 }
1063
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001064 info("Statistics collection: %x", kaweth->configuration.statistics_mask);
1065 info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
1066 info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
1067 info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 (int)kaweth->configuration.hw_addr[0],
1069 (int)kaweth->configuration.hw_addr[1],
1070 (int)kaweth->configuration.hw_addr[2],
1071 (int)kaweth->configuration.hw_addr[3],
1072 (int)kaweth->configuration.hw_addr[4],
1073 (int)kaweth->configuration.hw_addr[5]);
1074
1075 if(!memcmp(&kaweth->configuration.hw_addr,
1076 &bcast_addr,
1077 sizeof(bcast_addr))) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001078 err("Firmware not functioning properly, no net device created");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto err_free_netdev;
1080 }
1081
1082 if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001083 dbg("Error setting URB size");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto err_free_netdev;
1085 }
1086
1087 if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001088 err("Error setting SOFS wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto err_free_netdev;
1090 }
1091
1092 result = kaweth_set_receive_filter(kaweth,
1093 KAWETH_PACKET_FILTER_DIRECTED |
1094 KAWETH_PACKET_FILTER_BROADCAST |
1095 KAWETH_PACKET_FILTER_MULTICAST);
1096
1097 if(result < 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001098 err("Error setting receive filter");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto err_free_netdev;
1100 }
1101
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001102 dbg("Initializing net device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001104 kaweth->intf = intf;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1107 if (!kaweth->tx_urb)
1108 goto err_free_netdev;
1109 kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
1110 if (!kaweth->rx_urb)
1111 goto err_only_tx;
1112 kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1113 if (!kaweth->irq_urb)
1114 goto err_tx_and_rx;
1115
1116 kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
1117 INTBUFFERSIZE,
1118 GFP_KERNEL,
1119 &kaweth->intbufferhandle);
1120 if (!kaweth->intbuffer)
1121 goto err_tx_and_rx_and_irq;
1122 kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
1123 KAWETH_BUF_SIZE,
1124 GFP_KERNEL,
1125 &kaweth->rxbufferhandle);
1126 if (!kaweth->rx_buf)
1127 goto err_all_but_rxbuf;
1128
1129 memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
1130 memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
1131 sizeof(kaweth->configuration.hw_addr));
1132
1133 netdev->open = kaweth_open;
1134 netdev->stop = kaweth_close;
1135
1136 netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
1137 netdev->tx_timeout = kaweth_tx_timeout;
1138
1139 netdev->hard_start_xmit = kaweth_start_xmit;
1140 netdev->set_multicast_list = kaweth_set_rx_mode;
1141 netdev->get_stats = kaweth_netdev_stats;
1142 netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
1143 SET_ETHTOOL_OPS(netdev, &ops);
1144
1145 /* kaweth is zeroed as part of alloc_netdev */
1146
David Howellsc4028952006-11-22 14:57:56 +00001147 INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 SET_MODULE_OWNER(netdev);
1150
1151 usb_set_intfdata(intf, kaweth);
1152
1153#if 0
1154// dma_supported() is deeply broken on almost all architectures
1155 if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
1156 kaweth->net->features |= NETIF_F_HIGHDMA;
1157#endif
1158
1159 SET_NETDEV_DEV(netdev, &intf->dev);
1160 if (register_netdev(netdev) != 0) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001161 err("Error registering netdev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto err_intfdata;
1163 }
1164
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001165 info("kaweth interface created at %s", kaweth->net->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001167 dbg("Kaweth probe returning.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 return 0;
1170
1171err_intfdata:
1172 usb_set_intfdata(intf, NULL);
1173 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1174err_all_but_rxbuf:
1175 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1176err_tx_and_rx_and_irq:
1177 usb_free_urb(kaweth->irq_urb);
1178err_tx_and_rx:
1179 usb_free_urb(kaweth->rx_urb);
1180err_only_tx:
1181 usb_free_urb(kaweth->tx_urb);
1182err_free_netdev:
1183 free_netdev(netdev);
1184
1185 return -EIO;
1186}
1187
1188/****************************************************************
1189 * kaweth_disconnect
1190 ****************************************************************/
1191static void kaweth_disconnect(struct usb_interface *intf)
1192{
1193 struct kaweth_device *kaweth = usb_get_intfdata(intf);
1194 struct net_device *netdev;
1195
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001196 info("Unregistering");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 usb_set_intfdata(intf, NULL);
1199 if (!kaweth) {
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001200 warn("unregistering non-existant device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
1202 }
1203 netdev = kaweth->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001205 dbg("Unregistering net device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unregister_netdev(netdev);
1207
1208 usb_free_urb(kaweth->rx_urb);
1209 usb_free_urb(kaweth->tx_urb);
1210 usb_free_urb(kaweth->irq_urb);
1211
1212 usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
1213 usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
1214
1215 free_netdev(netdev);
1216}
1217
1218
1219// FIXME this completion stuff is a modified clone of
1220// an OLD version of some stuff in usb.c ...
1221struct usb_api_data {
1222 wait_queue_head_t wqh;
1223 int done;
1224};
1225
1226/*-------------------------------------------------------------------*
1227 * completion handler for compatibility wrappers (sync control/bulk) *
1228 *-------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +01001229static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct usb_api_data *awd = (struct usb_api_data *)urb->context;
1232
1233 awd->done=1;
1234 wake_up(&awd->wqh);
1235}
1236
1237/*-------------------------------------------------------------------*
1238 * COMPATIBILITY STUFF *
1239 *-------------------------------------------------------------------*/
1240
1241// Starts urb and waits for completion or timeout
1242static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
1243{
1244 struct usb_api_data awd;
1245 int status;
1246
1247 init_waitqueue_head(&awd.wqh);
1248 awd.done = 0;
1249
1250 urb->context = &awd;
1251 status = usb_submit_urb(urb, GFP_NOIO);
1252 if (status) {
1253 // something went wrong
1254 usb_free_urb(urb);
1255 return status;
1256 }
1257
1258 if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
1259 // timeout
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001260 warn("usb_control/bulk_msg: timeout");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 usb_kill_urb(urb); // remove urb safely
1262 status = -ETIMEDOUT;
1263 }
1264 else {
1265 status = urb->status;
1266 }
1267
1268 if (actual_length) {
1269 *actual_length = urb->actual_length;
1270 }
1271
1272 usb_free_urb(urb);
1273 return status;
1274}
1275
1276/*-------------------------------------------------------------------*/
1277// returns status (negative) or length (positive)
1278static int kaweth_internal_control_msg(struct usb_device *usb_dev,
1279 unsigned int pipe,
1280 struct usb_ctrlrequest *cmd, void *data,
1281 int len, int timeout)
1282{
1283 struct urb *urb;
1284 int retv;
Oliver Neukumb98b98f2007-01-16 09:47:12 +01001285 int length = 0; /* shut up GCC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 urb = usb_alloc_urb(0, GFP_NOIO);
1288 if (!urb)
1289 return -ENOMEM;
1290
1291 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
1292 len, usb_api_blocking_completion, NULL);
1293
1294 retv = usb_start_wait_urb(urb, timeout, &length);
1295 if (retv < 0) {
1296 return retv;
1297 }
1298 else {
1299 return length;
1300 }
1301}
1302
1303
1304/****************************************************************
1305 * kaweth_init
1306 ****************************************************************/
1307static int __init kaweth_init(void)
1308{
Oliver Neukumfbe2baf2006-09-28 23:36:04 +02001309 dbg("Driver loading");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return usb_register(&kaweth_driver);
1311}
1312
1313/****************************************************************
1314 * kaweth_exit
1315 ****************************************************************/
1316static void __exit kaweth_exit(void)
1317{
1318 usb_deregister(&kaweth_driver);
1319}
1320
1321module_init(kaweth_init);
1322module_exit(kaweth_exit);
1323
1324
1325
1326
1327
1328
1329
1330
1331