Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /**************************************************************** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #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 |
| 79 | |
| 80 | #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01 |
| 81 | #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02 |
| 82 | #define KAWETH_PACKET_FILTER_DIRECTED 0x04 |
| 83 | #define KAWETH_PACKET_FILTER_BROADCAST 0x08 |
| 84 | #define KAWETH_PACKET_FILTER_MULTICAST 0x10 |
| 85 | |
| 86 | /* Table 7 */ |
| 87 | #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00 |
| 88 | #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01 |
| 89 | #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02 |
| 90 | #define KAWETH_COMMAND_STATISTICS 0x03 |
| 91 | #define KAWETH_COMMAND_SET_TEMP_MAC 0x06 |
| 92 | #define KAWETH_COMMAND_GET_TEMP_MAC 0x07 |
| 93 | #define KAWETH_COMMAND_SET_URB_SIZE 0x08 |
| 94 | #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09 |
| 95 | #define KAWETH_COMMAND_SCAN 0xFF |
| 96 | |
| 97 | #define KAWETH_SOFS_TO_WAIT 0x05 |
| 98 | |
| 99 | #define INTBUFFERSIZE 4 |
| 100 | |
| 101 | #define STATE_OFFSET 0 |
| 102 | #define STATE_MASK 0x40 |
| 103 | #define STATE_SHIFT 5 |
| 104 | |
| 105 | |
| 106 | MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>"); |
| 107 | MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver"); |
| 108 | MODULE_LICENSE("GPL"); |
| 109 | |
| 110 | static const char driver_name[] = "kaweth"; |
| 111 | |
| 112 | static int kaweth_probe( |
| 113 | struct usb_interface *intf, |
| 114 | const struct usb_device_id *id /* from id_table */ |
| 115 | ); |
| 116 | static void kaweth_disconnect(struct usb_interface *intf); |
| 117 | static int kaweth_internal_control_msg(struct usb_device *usb_dev, |
| 118 | unsigned int pipe, |
| 119 | struct usb_ctrlrequest *cmd, void *data, |
| 120 | int len, int timeout); |
| 121 | |
| 122 | /**************************************************************** |
| 123 | * usb_device_id |
| 124 | ****************************************************************/ |
| 125 | static struct usb_device_id usb_klsi_table[] = { |
| 126 | { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */ |
| 127 | { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */ |
| 128 | { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */ |
| 129 | { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */ |
| 130 | { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */ |
| 131 | { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */ |
| 132 | { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */ |
| 133 | { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */ |
| 134 | { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */ |
| 135 | { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */ |
| 136 | { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */ |
| 137 | { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */ |
| 138 | { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */ |
| 139 | { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */ |
| 140 | { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */ |
| 141 | { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */ |
| 142 | { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */ |
| 143 | { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */ |
| 144 | { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */ |
| 145 | { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */ |
| 146 | { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */ |
| 147 | { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */ |
| 148 | { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */ |
| 149 | { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */ |
| 150 | { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */ |
| 151 | { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */ |
| 152 | { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */ |
| 153 | { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */ |
| 154 | { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */ |
| 155 | { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */ |
| 156 | { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */ |
| 157 | { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */ |
Oliver Neukum | 486ba2a | 2006-09-28 22:21:19 +0200 | [diff] [blame] | 158 | { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */ |
| 160 | {} /* Null terminator */ |
| 161 | }; |
| 162 | |
| 163 | MODULE_DEVICE_TABLE (usb, usb_klsi_table); |
| 164 | |
| 165 | /**************************************************************** |
| 166 | * kaweth_driver |
| 167 | ****************************************************************/ |
| 168 | static struct usb_driver kaweth_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | .name = driver_name, |
| 170 | .probe = kaweth_probe, |
| 171 | .disconnect = kaweth_disconnect, |
| 172 | .id_table = usb_klsi_table, |
| 173 | }; |
| 174 | |
| 175 | typedef __u8 eth_addr_t[6]; |
| 176 | |
| 177 | /**************************************************************** |
| 178 | * usb_eth_dev |
| 179 | ****************************************************************/ |
| 180 | struct usb_eth_dev { |
| 181 | char *name; |
| 182 | __u16 vendor; |
| 183 | __u16 device; |
| 184 | void *pdata; |
| 185 | }; |
| 186 | |
| 187 | /**************************************************************** |
| 188 | * kaweth_ethernet_configuration |
| 189 | * Refer Table 8 |
| 190 | ****************************************************************/ |
| 191 | struct kaweth_ethernet_configuration |
| 192 | { |
| 193 | __u8 size; |
| 194 | __u8 reserved1; |
| 195 | __u8 reserved2; |
| 196 | eth_addr_t hw_addr; |
| 197 | __u32 statistics_mask; |
| 198 | __le16 segment_size; |
| 199 | __u16 max_multicast_filters; |
| 200 | __u8 reserved3; |
| 201 | } __attribute__ ((packed)); |
| 202 | |
| 203 | /**************************************************************** |
| 204 | * kaweth_device |
| 205 | ****************************************************************/ |
| 206 | struct kaweth_device |
| 207 | { |
| 208 | spinlock_t device_lock; |
| 209 | |
| 210 | __u32 status; |
| 211 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | int suspend_lowmem_rx; |
| 213 | int suspend_lowmem_ctrl; |
| 214 | int linkstate; |
| 215 | struct work_struct lowmem_work; |
| 216 | |
| 217 | struct usb_device *dev; |
| 218 | struct net_device *net; |
| 219 | wait_queue_head_t term_wait; |
| 220 | |
| 221 | struct urb *rx_urb; |
| 222 | struct urb *tx_urb; |
| 223 | struct urb *irq_urb; |
| 224 | |
| 225 | dma_addr_t intbufferhandle; |
| 226 | __u8 *intbuffer; |
| 227 | dma_addr_t rxbufferhandle; |
| 228 | __u8 *rx_buf; |
| 229 | |
| 230 | |
| 231 | struct sk_buff *tx_skb; |
| 232 | |
| 233 | __u8 *firmware_buf; |
| 234 | __u8 scratch[KAWETH_SCRATCH_SIZE]; |
| 235 | __u16 packet_filter_bitmap; |
| 236 | |
| 237 | struct kaweth_ethernet_configuration configuration; |
| 238 | |
| 239 | struct net_device_stats stats; |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | /**************************************************************** |
| 244 | * kaweth_control |
| 245 | ****************************************************************/ |
| 246 | static int kaweth_control(struct kaweth_device *kaweth, |
| 247 | unsigned int pipe, |
| 248 | __u8 request, |
| 249 | __u8 requesttype, |
| 250 | __u16 value, |
| 251 | __u16 index, |
| 252 | void *data, |
| 253 | __u16 size, |
| 254 | int timeout) |
| 255 | { |
| 256 | struct usb_ctrlrequest *dr; |
| 257 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 258 | dbg("kaweth_control()"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | if(in_interrupt()) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 261 | dbg("in_interrupt()"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return -EBUSY; |
| 263 | } |
| 264 | |
| 265 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); |
| 266 | |
| 267 | if (!dr) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 268 | dbg("kmalloc() failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return -ENOMEM; |
| 270 | } |
| 271 | |
| 272 | dr->bRequestType= requesttype; |
| 273 | dr->bRequest = request; |
| 274 | dr->wValue = cpu_to_le16p(&value); |
| 275 | dr->wIndex = cpu_to_le16p(&index); |
| 276 | dr->wLength = cpu_to_le16p(&size); |
| 277 | |
| 278 | return kaweth_internal_control_msg(kaweth->dev, |
| 279 | pipe, |
| 280 | dr, |
| 281 | data, |
| 282 | size, |
| 283 | timeout); |
| 284 | } |
| 285 | |
| 286 | /**************************************************************** |
| 287 | * kaweth_read_configuration |
| 288 | ****************************************************************/ |
| 289 | static int kaweth_read_configuration(struct kaweth_device *kaweth) |
| 290 | { |
| 291 | int retval; |
| 292 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 293 | dbg("Reading kaweth configuration"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | retval = kaweth_control(kaweth, |
| 296 | usb_rcvctrlpipe(kaweth->dev, 0), |
| 297 | KAWETH_COMMAND_GET_ETHERNET_DESC, |
| 298 | USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, |
| 299 | 0, |
| 300 | 0, |
| 301 | (void *)&kaweth->configuration, |
| 302 | sizeof(kaweth->configuration), |
| 303 | KAWETH_CONTROL_TIMEOUT); |
| 304 | |
| 305 | return retval; |
| 306 | } |
| 307 | |
| 308 | /**************************************************************** |
| 309 | * kaweth_set_urb_size |
| 310 | ****************************************************************/ |
| 311 | static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size) |
| 312 | { |
| 313 | int retval; |
| 314 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 315 | dbg("Setting URB size to %d", (unsigned)urb_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | retval = kaweth_control(kaweth, |
| 318 | usb_sndctrlpipe(kaweth->dev, 0), |
| 319 | KAWETH_COMMAND_SET_URB_SIZE, |
| 320 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 321 | urb_size, |
| 322 | 0, |
| 323 | (void *)&kaweth->scratch, |
| 324 | 0, |
| 325 | KAWETH_CONTROL_TIMEOUT); |
| 326 | |
| 327 | return retval; |
| 328 | } |
| 329 | |
| 330 | /**************************************************************** |
| 331 | * kaweth_set_sofs_wait |
| 332 | ****************************************************************/ |
| 333 | static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait) |
| 334 | { |
| 335 | int retval; |
| 336 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 337 | dbg("Set SOFS wait to %d", (unsigned)sofs_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | retval = kaweth_control(kaweth, |
| 340 | usb_sndctrlpipe(kaweth->dev, 0), |
| 341 | KAWETH_COMMAND_SET_SOFS_WAIT, |
| 342 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 343 | sofs_wait, |
| 344 | 0, |
| 345 | (void *)&kaweth->scratch, |
| 346 | 0, |
| 347 | KAWETH_CONTROL_TIMEOUT); |
| 348 | |
| 349 | return retval; |
| 350 | } |
| 351 | |
| 352 | /**************************************************************** |
| 353 | * kaweth_set_receive_filter |
| 354 | ****************************************************************/ |
| 355 | static int kaweth_set_receive_filter(struct kaweth_device *kaweth, |
| 356 | __u16 receive_filter) |
| 357 | { |
| 358 | int retval; |
| 359 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 360 | dbg("Set receive filter to %d", (unsigned)receive_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | retval = kaweth_control(kaweth, |
| 363 | usb_sndctrlpipe(kaweth->dev, 0), |
| 364 | KAWETH_COMMAND_SET_PACKET_FILTER, |
| 365 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 366 | receive_filter, |
| 367 | 0, |
| 368 | (void *)&kaweth->scratch, |
| 369 | 0, |
| 370 | KAWETH_CONTROL_TIMEOUT); |
| 371 | |
| 372 | return retval; |
| 373 | } |
| 374 | |
| 375 | /**************************************************************** |
| 376 | * kaweth_download_firmware |
| 377 | ****************************************************************/ |
| 378 | static int kaweth_download_firmware(struct kaweth_device *kaweth, |
| 379 | __u8 *data, |
| 380 | __u16 data_len, |
| 381 | __u8 interrupt, |
| 382 | __u8 type) |
| 383 | { |
| 384 | if(data_len > KAWETH_FIRMWARE_BUF_SIZE) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 385 | err("Firmware too big: %d", data_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return -ENOSPC; |
| 387 | } |
| 388 | |
| 389 | memcpy(kaweth->firmware_buf, data, data_len); |
| 390 | |
| 391 | kaweth->firmware_buf[2] = (data_len & 0xFF) - 7; |
| 392 | kaweth->firmware_buf[3] = data_len >> 8; |
| 393 | kaweth->firmware_buf[4] = type; |
| 394 | kaweth->firmware_buf[5] = interrupt; |
| 395 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 396 | dbg("High: %i, Low:%i", kaweth->firmware_buf[3], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | kaweth->firmware_buf[2]); |
| 398 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 399 | dbg("Downloading firmware at %p to kaweth device at %p", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | data, |
| 401 | kaweth); |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 402 | dbg("Firmware length: %d", data_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | return kaweth_control(kaweth, |
| 405 | usb_sndctrlpipe(kaweth->dev, 0), |
| 406 | KAWETH_COMMAND_SCAN, |
| 407 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 408 | 0, |
| 409 | 0, |
| 410 | (void *)kaweth->firmware_buf, |
| 411 | data_len, |
| 412 | KAWETH_CONTROL_TIMEOUT); |
| 413 | } |
| 414 | |
| 415 | /**************************************************************** |
| 416 | * kaweth_trigger_firmware |
| 417 | ****************************************************************/ |
| 418 | static int kaweth_trigger_firmware(struct kaweth_device *kaweth, |
| 419 | __u8 interrupt) |
| 420 | { |
| 421 | kaweth->firmware_buf[0] = 0xB6; |
| 422 | kaweth->firmware_buf[1] = 0xC3; |
| 423 | kaweth->firmware_buf[2] = 0x01; |
| 424 | kaweth->firmware_buf[3] = 0x00; |
| 425 | kaweth->firmware_buf[4] = 0x06; |
| 426 | kaweth->firmware_buf[5] = interrupt; |
| 427 | kaweth->firmware_buf[6] = 0x00; |
| 428 | kaweth->firmware_buf[7] = 0x00; |
| 429 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 430 | dbg("Triggering firmware"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | return kaweth_control(kaweth, |
| 433 | usb_sndctrlpipe(kaweth->dev, 0), |
| 434 | KAWETH_COMMAND_SCAN, |
| 435 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 436 | 0, |
| 437 | 0, |
| 438 | (void *)kaweth->firmware_buf, |
| 439 | 8, |
| 440 | KAWETH_CONTROL_TIMEOUT); |
| 441 | } |
| 442 | |
| 443 | /**************************************************************** |
| 444 | * kaweth_reset |
| 445 | ****************************************************************/ |
| 446 | static int kaweth_reset(struct kaweth_device *kaweth) |
| 447 | { |
| 448 | int result; |
| 449 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 450 | dbg("kaweth_reset(%p)", kaweth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | result = kaweth_control(kaweth, |
| 452 | usb_sndctrlpipe(kaweth->dev, 0), |
| 453 | USB_REQ_SET_CONFIGURATION, |
| 454 | 0, |
| 455 | kaweth->dev->config[0].desc.bConfigurationValue, |
| 456 | 0, |
| 457 | NULL, |
| 458 | 0, |
| 459 | KAWETH_CONTROL_TIMEOUT); |
| 460 | |
Guillaume GOURAT / | f2d45cd | 2005-10-21 14:01:35 +0200 | [diff] [blame] | 461 | mdelay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 463 | dbg("kaweth_reset() returns %d.",result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | return result; |
| 466 | } |
| 467 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 468 | static void kaweth_usb_receive(struct urb *); |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 469 | static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /**************************************************************** |
| 472 | int_callback |
| 473 | *****************************************************************/ |
| 474 | |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 475 | static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | int status; |
| 478 | |
| 479 | status = usb_submit_urb (kaweth->irq_urb, mf); |
| 480 | if (unlikely(status == -ENOMEM)) { |
| 481 | kaweth->suspend_lowmem_ctrl = 1; |
| 482 | schedule_delayed_work(&kaweth->lowmem_work, HZ/4); |
| 483 | } else { |
| 484 | kaweth->suspend_lowmem_ctrl = 0; |
| 485 | } |
| 486 | |
| 487 | if (status) |
| 488 | err ("can't resubmit intr, %s-%s, status %d", |
| 489 | kaweth->dev->bus->bus_name, |
| 490 | kaweth->dev->devpath, status); |
| 491 | } |
| 492 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 493 | static void int_callback(struct urb *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct kaweth_device *kaweth = u->context; |
| 496 | int act_state; |
| 497 | |
| 498 | switch (u->status) { |
| 499 | case 0: /* success */ |
| 500 | break; |
| 501 | case -ECONNRESET: /* unlink */ |
| 502 | case -ENOENT: |
| 503 | case -ESHUTDOWN: |
| 504 | return; |
| 505 | /* -EPIPE: should clear the halt */ |
| 506 | default: /* error */ |
| 507 | goto resubmit; |
| 508 | } |
| 509 | |
| 510 | /* we check the link state to report changes */ |
| 511 | if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) { |
Oliver Neukum | 58125f9 | 2005-06-15 22:26:38 -0700 | [diff] [blame] | 512 | if (act_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | netif_carrier_on(kaweth->net); |
| 514 | else |
| 515 | netif_carrier_off(kaweth->net); |
| 516 | |
| 517 | kaweth->linkstate = act_state; |
| 518 | } |
| 519 | resubmit: |
| 520 | kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC); |
| 521 | } |
| 522 | |
| 523 | static void kaweth_resubmit_tl(void *d) |
| 524 | { |
| 525 | struct kaweth_device *kaweth = (struct kaweth_device *)d; |
| 526 | |
| 527 | if (kaweth->status | KAWETH_STATUS_CLOSING) |
| 528 | return; |
| 529 | |
| 530 | if (kaweth->suspend_lowmem_rx) |
| 531 | kaweth_resubmit_rx_urb(kaweth, GFP_NOIO); |
| 532 | |
| 533 | if (kaweth->suspend_lowmem_ctrl) |
| 534 | kaweth_resubmit_int_urb(kaweth, GFP_NOIO); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /**************************************************************** |
| 539 | * kaweth_resubmit_rx_urb |
| 540 | ****************************************************************/ |
| 541 | static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 542 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | int result; |
| 545 | |
| 546 | usb_fill_bulk_urb(kaweth->rx_urb, |
| 547 | kaweth->dev, |
| 548 | usb_rcvbulkpipe(kaweth->dev, 1), |
| 549 | kaweth->rx_buf, |
| 550 | KAWETH_BUF_SIZE, |
| 551 | kaweth_usb_receive, |
| 552 | kaweth); |
| 553 | kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 554 | kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle; |
| 555 | |
| 556 | if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) { |
| 557 | if (result == -ENOMEM) { |
| 558 | kaweth->suspend_lowmem_rx = 1; |
| 559 | schedule_delayed_work(&kaweth->lowmem_work, HZ/4); |
| 560 | } |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 561 | err("resubmitting rx_urb %d failed", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } else { |
| 563 | kaweth->suspend_lowmem_rx = 0; |
| 564 | } |
| 565 | |
| 566 | return result; |
| 567 | } |
| 568 | |
| 569 | static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth); |
| 570 | |
| 571 | /**************************************************************** |
| 572 | * kaweth_usb_receive |
| 573 | ****************************************************************/ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 574 | static void kaweth_usb_receive(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
| 576 | struct kaweth_device *kaweth = urb->context; |
| 577 | struct net_device *net = kaweth->net; |
| 578 | |
| 579 | int count = urb->actual_length; |
| 580 | int count2 = urb->transfer_buffer_length; |
| 581 | |
| 582 | __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf); |
| 583 | |
| 584 | struct sk_buff *skb; |
| 585 | |
| 586 | if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN)) |
| 587 | /* we are killed - set a flag and wake the disconnect handler */ |
| 588 | { |
| 589 | kaweth->end = 1; |
| 590 | wake_up(&kaweth->term_wait); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | if (kaweth->status & KAWETH_STATUS_CLOSING) |
| 595 | return; |
| 596 | |
| 597 | if(urb->status && urb->status != -EREMOTEIO && count != 1) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 598 | err("%s RX status: %d count: %d packet_len: %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | net->name, |
| 600 | urb->status, |
| 601 | count, |
| 602 | (int)pkt_len); |
| 603 | kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | if(kaweth->net && (count > 2)) { |
| 608 | if(pkt_len > (count - 2)) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 609 | err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count); |
| 610 | err("Packet len & 2047: %x", pkt_len & 2047); |
| 611 | err("Count 2: %x", count2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | if(!(skb = dev_alloc_skb(pkt_len+2))) { |
| 617 | kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */ |
| 622 | |
| 623 | skb->dev = net; |
| 624 | |
| 625 | eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0); |
| 626 | |
| 627 | skb_put(skb, pkt_len); |
| 628 | |
| 629 | skb->protocol = eth_type_trans(skb, net); |
| 630 | |
| 631 | netif_rx(skb); |
| 632 | |
| 633 | kaweth->stats.rx_packets++; |
| 634 | kaweth->stats.rx_bytes += pkt_len; |
| 635 | } |
| 636 | |
| 637 | kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC); |
| 638 | } |
| 639 | |
| 640 | /**************************************************************** |
| 641 | * kaweth_open |
| 642 | ****************************************************************/ |
| 643 | static int kaweth_open(struct net_device *net) |
| 644 | { |
| 645 | struct kaweth_device *kaweth = netdev_priv(net); |
| 646 | int res; |
| 647 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 648 | dbg("Opening network device."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL); |
| 651 | if (res) |
| 652 | return -EIO; |
| 653 | |
| 654 | usb_fill_int_urb( |
| 655 | kaweth->irq_urb, |
| 656 | kaweth->dev, |
| 657 | usb_rcvintpipe(kaweth->dev, 3), |
| 658 | kaweth->intbuffer, |
| 659 | INTBUFFERSIZE, |
| 660 | int_callback, |
| 661 | kaweth, |
| 662 | 250); /* overriding the descriptor */ |
| 663 | kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle; |
| 664 | kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 665 | |
| 666 | res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL); |
| 667 | if (res) { |
| 668 | usb_kill_urb(kaweth->rx_urb); |
| 669 | return -EIO; |
| 670 | } |
| 671 | |
| 672 | netif_start_queue(net); |
| 673 | |
| 674 | kaweth_async_set_rx_mode(kaweth); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /**************************************************************** |
| 679 | * kaweth_close |
| 680 | ****************************************************************/ |
| 681 | static int kaweth_close(struct net_device *net) |
| 682 | { |
| 683 | struct kaweth_device *kaweth = netdev_priv(net); |
| 684 | |
| 685 | netif_stop_queue(net); |
| 686 | |
| 687 | kaweth->status |= KAWETH_STATUS_CLOSING; |
| 688 | |
| 689 | usb_kill_urb(kaweth->irq_urb); |
| 690 | usb_kill_urb(kaweth->rx_urb); |
Herbert Xu | d23b536 | 2005-11-17 09:47:45 -0800 | [diff] [blame] | 691 | usb_kill_urb(kaweth->tx_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | flush_scheduled_work(); |
| 694 | |
| 695 | /* a scheduled work may have resubmitted, |
| 696 | we hit them again */ |
| 697 | usb_kill_urb(kaweth->irq_urb); |
| 698 | usb_kill_urb(kaweth->rx_urb); |
| 699 | |
| 700 | kaweth->status &= ~KAWETH_STATUS_CLOSING; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 706 | { |
| 707 | |
| 708 | strlcpy(info->driver, driver_name, sizeof(info->driver)); |
| 709 | } |
| 710 | |
| 711 | static struct ethtool_ops ops = { |
| 712 | .get_drvinfo = kaweth_get_drvinfo |
| 713 | }; |
| 714 | |
| 715 | /**************************************************************** |
| 716 | * kaweth_usb_transmit_complete |
| 717 | ****************************************************************/ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 718 | static void kaweth_usb_transmit_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
| 720 | struct kaweth_device *kaweth = urb->context; |
| 721 | struct sk_buff *skb = kaweth->tx_skb; |
| 722 | |
| 723 | if (unlikely(urb->status != 0)) |
| 724 | if (urb->status != -ENOENT) |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 725 | dbg("%s: TX status %d.", kaweth->net->name, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | netif_wake_queue(kaweth->net); |
| 728 | dev_kfree_skb_irq(skb); |
| 729 | } |
| 730 | |
| 731 | /**************************************************************** |
| 732 | * kaweth_start_xmit |
| 733 | ****************************************************************/ |
| 734 | static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net) |
| 735 | { |
| 736 | struct kaweth_device *kaweth = netdev_priv(net); |
| 737 | __le16 *private_header; |
| 738 | |
| 739 | int res; |
| 740 | |
| 741 | spin_lock(&kaweth->device_lock); |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | kaweth_async_set_rx_mode(kaweth); |
| 744 | netif_stop_queue(net); |
| 745 | |
| 746 | /* We now decide whether we can put our special header into the sk_buff */ |
| 747 | if (skb_cloned(skb) || skb_headroom(skb) < 2) { |
| 748 | /* no such luck - we make our own */ |
| 749 | struct sk_buff *copied_skb; |
| 750 | copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC); |
| 751 | dev_kfree_skb_irq(skb); |
| 752 | skb = copied_skb; |
| 753 | if (!copied_skb) { |
| 754 | kaweth->stats.tx_errors++; |
| 755 | netif_start_queue(net); |
| 756 | spin_unlock(&kaweth->device_lock); |
| 757 | return 0; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | private_header = (__le16 *)__skb_push(skb, 2); |
| 762 | *private_header = cpu_to_le16(skb->len-2); |
| 763 | kaweth->tx_skb = skb; |
| 764 | |
| 765 | usb_fill_bulk_urb(kaweth->tx_urb, |
| 766 | kaweth->dev, |
| 767 | usb_sndbulkpipe(kaweth->dev, 2), |
| 768 | private_header, |
| 769 | skb->len, |
| 770 | kaweth_usb_transmit_complete, |
| 771 | kaweth); |
| 772 | kaweth->end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC))) |
| 775 | { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 776 | warn("kaweth failed tx_urb %d", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | kaweth->stats.tx_errors++; |
| 778 | |
| 779 | netif_start_queue(net); |
| 780 | dev_kfree_skb_irq(skb); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | kaweth->stats.tx_packets++; |
| 785 | kaweth->stats.tx_bytes += skb->len; |
| 786 | net->trans_start = jiffies; |
| 787 | } |
| 788 | |
| 789 | spin_unlock(&kaweth->device_lock); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /**************************************************************** |
| 795 | * kaweth_set_rx_mode |
| 796 | ****************************************************************/ |
| 797 | static void kaweth_set_rx_mode(struct net_device *net) |
| 798 | { |
| 799 | struct kaweth_device *kaweth = netdev_priv(net); |
| 800 | |
| 801 | __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED | |
| 802 | KAWETH_PACKET_FILTER_BROADCAST | |
| 803 | KAWETH_PACKET_FILTER_MULTICAST; |
| 804 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 805 | dbg("Setting Rx mode to %d", packet_filter_bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | netif_stop_queue(net); |
| 808 | |
| 809 | if (net->flags & IFF_PROMISC) { |
| 810 | packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS; |
| 811 | } |
| 812 | else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) { |
| 813 | packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST; |
| 814 | } |
| 815 | |
| 816 | kaweth->packet_filter_bitmap = packet_filter_bitmap; |
| 817 | netif_wake_queue(net); |
| 818 | } |
| 819 | |
| 820 | /**************************************************************** |
| 821 | * kaweth_async_set_rx_mode |
| 822 | ****************************************************************/ |
| 823 | static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth) |
| 824 | { |
| 825 | __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap; |
| 826 | kaweth->packet_filter_bitmap = 0; |
| 827 | if (packet_filter_bitmap == 0) |
| 828 | return; |
| 829 | |
| 830 | { |
| 831 | int result; |
| 832 | result = kaweth_control(kaweth, |
| 833 | usb_sndctrlpipe(kaweth->dev, 0), |
| 834 | KAWETH_COMMAND_SET_PACKET_FILTER, |
| 835 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, |
| 836 | packet_filter_bitmap, |
| 837 | 0, |
| 838 | (void *)&kaweth->scratch, |
| 839 | 0, |
| 840 | KAWETH_CONTROL_TIMEOUT); |
| 841 | |
| 842 | if(result < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 843 | err("Failed to set Rx mode: %d", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | else { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 846 | dbg("Set Rx mode to %d", packet_filter_bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /**************************************************************** |
| 852 | * kaweth_netdev_stats |
| 853 | ****************************************************************/ |
| 854 | static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev) |
| 855 | { |
| 856 | struct kaweth_device *kaweth = netdev_priv(dev); |
| 857 | return &kaweth->stats; |
| 858 | } |
| 859 | |
| 860 | /**************************************************************** |
| 861 | * kaweth_tx_timeout |
| 862 | ****************************************************************/ |
| 863 | static void kaweth_tx_timeout(struct net_device *net) |
| 864 | { |
| 865 | struct kaweth_device *kaweth = netdev_priv(net); |
| 866 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 867 | warn("%s: Tx timed out. Resetting.", net->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | kaweth->stats.tx_errors++; |
| 869 | net->trans_start = jiffies; |
| 870 | |
| 871 | usb_unlink_urb(kaweth->tx_urb); |
| 872 | } |
| 873 | |
| 874 | /**************************************************************** |
| 875 | * kaweth_probe |
| 876 | ****************************************************************/ |
| 877 | static int kaweth_probe( |
| 878 | struct usb_interface *intf, |
| 879 | const struct usb_device_id *id /* from id_table */ |
| 880 | ) |
| 881 | { |
| 882 | struct usb_device *dev = interface_to_usbdev(intf); |
| 883 | struct kaweth_device *kaweth; |
| 884 | struct net_device *netdev; |
| 885 | const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 886 | int result = 0; |
| 887 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 888 | dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | dev->devnum, |
| 890 | le16_to_cpu(dev->descriptor.idVendor), |
| 891 | le16_to_cpu(dev->descriptor.idProduct), |
| 892 | le16_to_cpu(dev->descriptor.bcdDevice)); |
| 893 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 894 | dbg("Device at %p", dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 896 | dbg("Descriptor length: %x type: %x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | (int)dev->descriptor.bLength, |
| 898 | (int)dev->descriptor.bDescriptorType); |
| 899 | |
| 900 | netdev = alloc_etherdev(sizeof(*kaweth)); |
| 901 | if (!netdev) |
| 902 | return -ENOMEM; |
| 903 | |
| 904 | kaweth = netdev_priv(netdev); |
| 905 | kaweth->dev = dev; |
| 906 | kaweth->net = netdev; |
| 907 | |
| 908 | spin_lock_init(&kaweth->device_lock); |
| 909 | init_waitqueue_head(&kaweth->term_wait); |
| 910 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 911 | dbg("Resetting."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
| 913 | kaweth_reset(kaweth); |
| 914 | |
| 915 | /* |
| 916 | * If high byte of bcdDevice is nonzero, firmware is already |
| 917 | * downloaded. Don't try to do it again, or we'll hang the device. |
| 918 | */ |
| 919 | |
| 920 | if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 921 | info("Firmware present in device."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } else { |
| 923 | /* Download the firmware */ |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 924 | info("Downloading firmware..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL); |
| 926 | if ((result = kaweth_download_firmware(kaweth, |
| 927 | kaweth_new_code, |
| 928 | len_kaweth_new_code, |
| 929 | 100, |
| 930 | 2)) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 931 | err("Error downloading firmware (%d)", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | goto err_fw; |
| 933 | } |
| 934 | |
| 935 | if ((result = kaweth_download_firmware(kaweth, |
| 936 | kaweth_new_code_fix, |
| 937 | len_kaweth_new_code_fix, |
| 938 | 100, |
| 939 | 3)) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 940 | err("Error downloading firmware fix (%d)", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | goto err_fw; |
| 942 | } |
| 943 | |
| 944 | if ((result = kaweth_download_firmware(kaweth, |
| 945 | kaweth_trigger_code, |
| 946 | len_kaweth_trigger_code, |
| 947 | 126, |
| 948 | 2)) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 949 | err("Error downloading trigger code (%d)", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | goto err_fw; |
| 951 | |
| 952 | } |
| 953 | |
| 954 | if ((result = kaweth_download_firmware(kaweth, |
| 955 | kaweth_trigger_code_fix, |
| 956 | len_kaweth_trigger_code_fix, |
| 957 | 126, |
| 958 | 3)) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 959 | err("Error downloading trigger code fix (%d)", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | goto err_fw; |
| 961 | } |
| 962 | |
| 963 | |
| 964 | if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 965 | err("Error triggering firmware (%d)", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | goto err_fw; |
| 967 | } |
| 968 | |
| 969 | /* Device will now disappear for a moment... */ |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 970 | info("Firmware loaded. I'll be back..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | err_fw: |
| 972 | free_page((unsigned long)kaweth->firmware_buf); |
| 973 | free_netdev(netdev); |
| 974 | return -EIO; |
| 975 | } |
| 976 | |
| 977 | result = kaweth_read_configuration(kaweth); |
| 978 | |
| 979 | if(result < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 980 | err("Error reading configuration (%d), no net device created", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | goto err_free_netdev; |
| 982 | } |
| 983 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 984 | info("Statistics collection: %x", kaweth->configuration.statistics_mask); |
| 985 | info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1)); |
| 986 | info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size)); |
| 987 | info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | (int)kaweth->configuration.hw_addr[0], |
| 989 | (int)kaweth->configuration.hw_addr[1], |
| 990 | (int)kaweth->configuration.hw_addr[2], |
| 991 | (int)kaweth->configuration.hw_addr[3], |
| 992 | (int)kaweth->configuration.hw_addr[4], |
| 993 | (int)kaweth->configuration.hw_addr[5]); |
| 994 | |
| 995 | if(!memcmp(&kaweth->configuration.hw_addr, |
| 996 | &bcast_addr, |
| 997 | sizeof(bcast_addr))) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 998 | err("Firmware not functioning properly, no net device created"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | goto err_free_netdev; |
| 1000 | } |
| 1001 | |
| 1002 | if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1003 | dbg("Error setting URB size"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | goto err_free_netdev; |
| 1005 | } |
| 1006 | |
| 1007 | if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1008 | err("Error setting SOFS wait"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | goto err_free_netdev; |
| 1010 | } |
| 1011 | |
| 1012 | result = kaweth_set_receive_filter(kaweth, |
| 1013 | KAWETH_PACKET_FILTER_DIRECTED | |
| 1014 | KAWETH_PACKET_FILTER_BROADCAST | |
| 1015 | KAWETH_PACKET_FILTER_MULTICAST); |
| 1016 | |
| 1017 | if(result < 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1018 | err("Error setting receive filter"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | goto err_free_netdev; |
| 1020 | } |
| 1021 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1022 | dbg("Initializing net device."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1025 | if (!kaweth->tx_urb) |
| 1026 | goto err_free_netdev; |
| 1027 | kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1028 | if (!kaweth->rx_urb) |
| 1029 | goto err_only_tx; |
| 1030 | kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1031 | if (!kaweth->irq_urb) |
| 1032 | goto err_tx_and_rx; |
| 1033 | |
| 1034 | kaweth->intbuffer = usb_buffer_alloc( kaweth->dev, |
| 1035 | INTBUFFERSIZE, |
| 1036 | GFP_KERNEL, |
| 1037 | &kaweth->intbufferhandle); |
| 1038 | if (!kaweth->intbuffer) |
| 1039 | goto err_tx_and_rx_and_irq; |
| 1040 | kaweth->rx_buf = usb_buffer_alloc( kaweth->dev, |
| 1041 | KAWETH_BUF_SIZE, |
| 1042 | GFP_KERNEL, |
| 1043 | &kaweth->rxbufferhandle); |
| 1044 | if (!kaweth->rx_buf) |
| 1045 | goto err_all_but_rxbuf; |
| 1046 | |
| 1047 | memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr)); |
| 1048 | memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr, |
| 1049 | sizeof(kaweth->configuration.hw_addr)); |
| 1050 | |
| 1051 | netdev->open = kaweth_open; |
| 1052 | netdev->stop = kaweth_close; |
| 1053 | |
| 1054 | netdev->watchdog_timeo = KAWETH_TX_TIMEOUT; |
| 1055 | netdev->tx_timeout = kaweth_tx_timeout; |
| 1056 | |
| 1057 | netdev->hard_start_xmit = kaweth_start_xmit; |
| 1058 | netdev->set_multicast_list = kaweth_set_rx_mode; |
| 1059 | netdev->get_stats = kaweth_netdev_stats; |
| 1060 | netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size); |
| 1061 | SET_ETHTOOL_OPS(netdev, &ops); |
| 1062 | |
| 1063 | /* kaweth is zeroed as part of alloc_netdev */ |
| 1064 | |
| 1065 | INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth); |
| 1066 | |
| 1067 | SET_MODULE_OWNER(netdev); |
| 1068 | |
| 1069 | usb_set_intfdata(intf, kaweth); |
| 1070 | |
| 1071 | #if 0 |
| 1072 | // dma_supported() is deeply broken on almost all architectures |
| 1073 | if (dma_supported (&intf->dev, 0xffffffffffffffffULL)) |
| 1074 | kaweth->net->features |= NETIF_F_HIGHDMA; |
| 1075 | #endif |
| 1076 | |
| 1077 | SET_NETDEV_DEV(netdev, &intf->dev); |
| 1078 | if (register_netdev(netdev) != 0) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1079 | err("Error registering netdev."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | goto err_intfdata; |
| 1081 | } |
| 1082 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1083 | info("kaweth interface created at %s", kaweth->net->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1085 | dbg("Kaweth probe returning."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | return 0; |
| 1088 | |
| 1089 | err_intfdata: |
| 1090 | usb_set_intfdata(intf, NULL); |
| 1091 | usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle); |
| 1092 | err_all_but_rxbuf: |
| 1093 | usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle); |
| 1094 | err_tx_and_rx_and_irq: |
| 1095 | usb_free_urb(kaweth->irq_urb); |
| 1096 | err_tx_and_rx: |
| 1097 | usb_free_urb(kaweth->rx_urb); |
| 1098 | err_only_tx: |
| 1099 | usb_free_urb(kaweth->tx_urb); |
| 1100 | err_free_netdev: |
| 1101 | free_netdev(netdev); |
| 1102 | |
| 1103 | return -EIO; |
| 1104 | } |
| 1105 | |
| 1106 | /**************************************************************** |
| 1107 | * kaweth_disconnect |
| 1108 | ****************************************************************/ |
| 1109 | static void kaweth_disconnect(struct usb_interface *intf) |
| 1110 | { |
| 1111 | struct kaweth_device *kaweth = usb_get_intfdata(intf); |
| 1112 | struct net_device *netdev; |
| 1113 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1114 | info("Unregistering"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | usb_set_intfdata(intf, NULL); |
| 1117 | if (!kaweth) { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1118 | warn("unregistering non-existant device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return; |
| 1120 | } |
| 1121 | netdev = kaweth->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1123 | dbg("Unregistering net device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | unregister_netdev(netdev); |
| 1125 | |
| 1126 | usb_free_urb(kaweth->rx_urb); |
| 1127 | usb_free_urb(kaweth->tx_urb); |
| 1128 | usb_free_urb(kaweth->irq_urb); |
| 1129 | |
| 1130 | usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle); |
| 1131 | usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle); |
| 1132 | |
| 1133 | free_netdev(netdev); |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | // FIXME this completion stuff is a modified clone of |
| 1138 | // an OLD version of some stuff in usb.c ... |
| 1139 | struct usb_api_data { |
| 1140 | wait_queue_head_t wqh; |
| 1141 | int done; |
| 1142 | }; |
| 1143 | |
| 1144 | /*-------------------------------------------------------------------* |
| 1145 | * completion handler for compatibility wrappers (sync control/bulk) * |
| 1146 | *-------------------------------------------------------------------*/ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1147 | static void usb_api_blocking_completion(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { |
| 1149 | struct usb_api_data *awd = (struct usb_api_data *)urb->context; |
| 1150 | |
| 1151 | awd->done=1; |
| 1152 | wake_up(&awd->wqh); |
| 1153 | } |
| 1154 | |
| 1155 | /*-------------------------------------------------------------------* |
| 1156 | * COMPATIBILITY STUFF * |
| 1157 | *-------------------------------------------------------------------*/ |
| 1158 | |
| 1159 | // Starts urb and waits for completion or timeout |
| 1160 | static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length) |
| 1161 | { |
| 1162 | struct usb_api_data awd; |
| 1163 | int status; |
| 1164 | |
| 1165 | init_waitqueue_head(&awd.wqh); |
| 1166 | awd.done = 0; |
| 1167 | |
| 1168 | urb->context = &awd; |
| 1169 | status = usb_submit_urb(urb, GFP_NOIO); |
| 1170 | if (status) { |
| 1171 | // something went wrong |
| 1172 | usb_free_urb(urb); |
| 1173 | return status; |
| 1174 | } |
| 1175 | |
| 1176 | if (!wait_event_timeout(awd.wqh, awd.done, timeout)) { |
| 1177 | // timeout |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1178 | warn("usb_control/bulk_msg: timeout"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | usb_kill_urb(urb); // remove urb safely |
| 1180 | status = -ETIMEDOUT; |
| 1181 | } |
| 1182 | else { |
| 1183 | status = urb->status; |
| 1184 | } |
| 1185 | |
| 1186 | if (actual_length) { |
| 1187 | *actual_length = urb->actual_length; |
| 1188 | } |
| 1189 | |
| 1190 | usb_free_urb(urb); |
| 1191 | return status; |
| 1192 | } |
| 1193 | |
| 1194 | /*-------------------------------------------------------------------*/ |
| 1195 | // returns status (negative) or length (positive) |
| 1196 | static int kaweth_internal_control_msg(struct usb_device *usb_dev, |
| 1197 | unsigned int pipe, |
| 1198 | struct usb_ctrlrequest *cmd, void *data, |
| 1199 | int len, int timeout) |
| 1200 | { |
| 1201 | struct urb *urb; |
| 1202 | int retv; |
| 1203 | int length; |
| 1204 | |
| 1205 | urb = usb_alloc_urb(0, GFP_NOIO); |
| 1206 | if (!urb) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data, |
| 1210 | len, usb_api_blocking_completion, NULL); |
| 1211 | |
| 1212 | retv = usb_start_wait_urb(urb, timeout, &length); |
| 1213 | if (retv < 0) { |
| 1214 | return retv; |
| 1215 | } |
| 1216 | else { |
| 1217 | return length; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | /**************************************************************** |
| 1223 | * kaweth_init |
| 1224 | ****************************************************************/ |
| 1225 | static int __init kaweth_init(void) |
| 1226 | { |
Oliver Neukum | fbe2baf | 2006-09-28 23:36:04 +0200 | [diff] [blame^] | 1227 | dbg("Driver loading"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return usb_register(&kaweth_driver); |
| 1229 | } |
| 1230 | |
| 1231 | /**************************************************************** |
| 1232 | * kaweth_exit |
| 1233 | ****************************************************************/ |
| 1234 | static void __exit kaweth_exit(void) |
| 1235 | { |
| 1236 | usb_deregister(&kaweth_driver); |
| 1237 | } |
| 1238 | |
| 1239 | module_init(kaweth_init); |
| 1240 | module_exit(kaweth_exit); |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |