Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001 Vojtech Pavlik |
| 3 | * |
| 4 | * CATC EL1210A NetMate USB Ethernet driver |
| 5 | * |
| 6 | * Sponsored by SuSE |
| 7 | * |
| 8 | * Based on the work of |
| 9 | * Donald Becker |
| 10 | * |
| 11 | * Old chipset support added by Simon Evans <spse@secret.org.uk> 2002 |
| 12 | * - adds support for Belkin F5U011 |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
| 30 | * Should you need to contact me, the author, you can do so either by |
| 31 | * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail: |
| 32 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 33 | */ |
| 34 | |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/string.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/netdevice.h> |
| 41 | #include <linux/etherdevice.h> |
| 42 | #include <linux/skbuff.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/ethtool.h> |
| 45 | #include <linux/crc32.h> |
| 46 | #include <linux/bitops.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | |
| 49 | #undef DEBUG |
| 50 | |
| 51 | #include <linux/usb.h> |
| 52 | |
| 53 | /* |
| 54 | * Version information. |
| 55 | */ |
| 56 | |
| 57 | #define DRIVER_VERSION "v2.8" |
| 58 | #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>" |
| 59 | #define DRIVER_DESC "CATC EL1210A NetMate USB Ethernet driver" |
| 60 | #define SHORT_DRIVER_DESC "EL1210A NetMate USB Ethernet" |
| 61 | |
| 62 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 63 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 64 | MODULE_LICENSE("GPL"); |
| 65 | |
| 66 | static const char driver_name[] = "catc"; |
| 67 | |
| 68 | /* |
| 69 | * Some defines. |
| 70 | */ |
| 71 | |
| 72 | #define STATS_UPDATE (HZ) /* Time between stats updates */ |
| 73 | #define TX_TIMEOUT (5*HZ) /* Max time the queue can be stopped */ |
| 74 | #define PKT_SZ 1536 /* Max Ethernet packet size */ |
| 75 | #define RX_MAX_BURST 15 /* Max packets per rx buffer (> 0, < 16) */ |
| 76 | #define TX_MAX_BURST 15 /* Max full sized packets per tx buffer (> 0) */ |
| 77 | #define CTRL_QUEUE 16 /* Max control requests in flight (power of two) */ |
| 78 | #define RX_PKT_SZ 1600 /* Max size of receive packet for F5U011 */ |
| 79 | |
| 80 | /* |
| 81 | * Control requests. |
| 82 | */ |
| 83 | |
| 84 | enum control_requests { |
| 85 | ReadMem = 0xf1, |
| 86 | GetMac = 0xf2, |
| 87 | Reset = 0xf4, |
| 88 | SetMac = 0xf5, |
| 89 | SetRxMode = 0xf5, /* F5U011 only */ |
| 90 | WriteROM = 0xf8, |
| 91 | SetReg = 0xfa, |
| 92 | GetReg = 0xfb, |
| 93 | WriteMem = 0xfc, |
| 94 | ReadROM = 0xfd, |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Registers. |
| 99 | */ |
| 100 | |
| 101 | enum register_offsets { |
| 102 | TxBufCount = 0x20, |
| 103 | RxBufCount = 0x21, |
| 104 | OpModes = 0x22, |
| 105 | TxQed = 0x23, |
| 106 | RxQed = 0x24, |
| 107 | MaxBurst = 0x25, |
| 108 | RxUnit = 0x60, |
| 109 | EthStatus = 0x61, |
| 110 | StationAddr0 = 0x67, |
| 111 | EthStats = 0x69, |
| 112 | LEDCtrl = 0x81, |
| 113 | }; |
| 114 | |
| 115 | enum eth_stats { |
| 116 | TxSingleColl = 0x00, |
| 117 | TxMultiColl = 0x02, |
| 118 | TxExcessColl = 0x04, |
| 119 | RxFramErr = 0x06, |
| 120 | }; |
| 121 | |
| 122 | enum op_mode_bits { |
| 123 | Op3MemWaits = 0x03, |
| 124 | OpLenInclude = 0x08, |
| 125 | OpRxMerge = 0x10, |
| 126 | OpTxMerge = 0x20, |
| 127 | OpWin95bugfix = 0x40, |
| 128 | OpLoopback = 0x80, |
| 129 | }; |
| 130 | |
| 131 | enum rx_filter_bits { |
| 132 | RxEnable = 0x01, |
| 133 | RxPolarity = 0x02, |
| 134 | RxForceOK = 0x04, |
| 135 | RxMultiCast = 0x08, |
| 136 | RxPromisc = 0x10, |
| 137 | AltRxPromisc = 0x20, /* F5U011 uses different bit */ |
| 138 | }; |
| 139 | |
| 140 | enum led_values { |
| 141 | LEDFast = 0x01, |
| 142 | LEDSlow = 0x02, |
| 143 | LEDFlash = 0x03, |
| 144 | LEDPulse = 0x04, |
| 145 | LEDLink = 0x08, |
| 146 | }; |
| 147 | |
| 148 | enum link_status { |
| 149 | LinkNoChange = 0, |
| 150 | LinkGood = 1, |
| 151 | LinkBad = 2 |
| 152 | }; |
| 153 | |
| 154 | /* |
| 155 | * The catc struct. |
| 156 | */ |
| 157 | |
| 158 | #define CTRL_RUNNING 0 |
| 159 | #define RX_RUNNING 1 |
| 160 | #define TX_RUNNING 2 |
| 161 | |
| 162 | struct catc { |
| 163 | struct net_device *netdev; |
| 164 | struct usb_device *usbdev; |
| 165 | |
| 166 | struct net_device_stats stats; |
| 167 | unsigned long flags; |
| 168 | |
| 169 | unsigned int tx_ptr, tx_idx; |
| 170 | unsigned int ctrl_head, ctrl_tail; |
| 171 | spinlock_t tx_lock, ctrl_lock; |
| 172 | |
| 173 | u8 tx_buf[2][TX_MAX_BURST * (PKT_SZ + 2)]; |
| 174 | u8 rx_buf[RX_MAX_BURST * (PKT_SZ + 2)]; |
| 175 | u8 irq_buf[2]; |
| 176 | u8 ctrl_buf[64]; |
| 177 | struct usb_ctrlrequest ctrl_dr; |
| 178 | |
| 179 | struct timer_list timer; |
| 180 | u8 stats_buf[8]; |
| 181 | u16 stats_vals[4]; |
| 182 | unsigned long last_stats; |
| 183 | |
| 184 | u8 multicast[64]; |
| 185 | |
| 186 | struct ctrl_queue { |
| 187 | u8 dir; |
| 188 | u8 request; |
| 189 | u16 value; |
| 190 | u16 index; |
| 191 | void *buf; |
| 192 | int len; |
| 193 | void (*callback)(struct catc *catc, struct ctrl_queue *q); |
| 194 | } ctrl_queue[CTRL_QUEUE]; |
| 195 | |
| 196 | struct urb *tx_urb, *rx_urb, *irq_urb, *ctrl_urb; |
| 197 | |
| 198 | u8 is_f5u011; /* Set if device is an F5U011 */ |
| 199 | u8 rxmode[2]; /* Used for F5U011 */ |
| 200 | atomic_t recq_sz; /* Used for F5U011 - counter of waiting rx packets */ |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | * Useful macros. |
| 205 | */ |
| 206 | |
| 207 | #define catc_get_mac(catc, mac) catc_ctrl_msg(catc, USB_DIR_IN, GetMac, 0, 0, mac, 6) |
| 208 | #define catc_reset(catc) catc_ctrl_msg(catc, USB_DIR_OUT, Reset, 0, 0, NULL, 0) |
| 209 | #define catc_set_reg(catc, reg, val) catc_ctrl_msg(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0) |
| 210 | #define catc_get_reg(catc, reg, buf) catc_ctrl_msg(catc, USB_DIR_IN, GetReg, 0, reg, buf, 1) |
| 211 | #define catc_write_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size) |
| 212 | #define catc_read_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_IN, ReadMem, 0, addr, buf, size) |
| 213 | |
| 214 | #define f5u011_rxmode(catc, rxmode) catc_ctrl_msg(catc, USB_DIR_OUT, SetRxMode, 0, 1, rxmode, 2) |
| 215 | #define f5u011_rxmode_async(catc, rxmode) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 1, &rxmode, 2, NULL) |
| 216 | #define f5u011_mchash_async(catc, hash) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 2, &hash, 8, NULL) |
| 217 | |
| 218 | #define catc_set_reg_async(catc, reg, val) catc_ctrl_async(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0, NULL) |
| 219 | #define catc_get_reg_async(catc, reg, cb) catc_ctrl_async(catc, USB_DIR_IN, GetReg, 0, reg, NULL, 1, cb) |
| 220 | #define catc_write_mem_async(catc, addr, buf, size) catc_ctrl_async(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size, NULL) |
| 221 | |
| 222 | /* |
| 223 | * Receive routines. |
| 224 | */ |
| 225 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 226 | static void catc_rx_done(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | struct catc *catc = urb->context; |
| 229 | u8 *pkt_start = urb->transfer_buffer; |
| 230 | struct sk_buff *skb; |
| 231 | int pkt_len, pkt_offset = 0; |
| 232 | |
| 233 | if (!catc->is_f5u011) { |
| 234 | clear_bit(RX_RUNNING, &catc->flags); |
| 235 | pkt_offset = 2; |
| 236 | } |
| 237 | |
| 238 | if (urb->status) { |
| 239 | dbg("rx_done, status %d, length %d", urb->status, urb->actual_length); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | do { |
| 244 | if(!catc->is_f5u011) { |
| 245 | pkt_len = le16_to_cpup((__le16*)pkt_start); |
| 246 | if (pkt_len > urb->actual_length) { |
| 247 | catc->stats.rx_length_errors++; |
| 248 | catc->stats.rx_errors++; |
| 249 | break; |
| 250 | } |
| 251 | } else { |
| 252 | pkt_len = urb->actual_length; |
| 253 | } |
| 254 | |
| 255 | if (!(skb = dev_alloc_skb(pkt_len))) |
| 256 | return; |
| 257 | |
| 258 | skb->dev = catc->netdev; |
| 259 | eth_copy_and_sum(skb, pkt_start + pkt_offset, pkt_len, 0); |
| 260 | skb_put(skb, pkt_len); |
| 261 | |
| 262 | skb->protocol = eth_type_trans(skb, catc->netdev); |
| 263 | netif_rx(skb); |
| 264 | |
| 265 | catc->stats.rx_packets++; |
| 266 | catc->stats.rx_bytes += pkt_len; |
| 267 | |
| 268 | /* F5U011 only does one packet per RX */ |
| 269 | if (catc->is_f5u011) |
| 270 | break; |
| 271 | pkt_start += (((pkt_len + 1) >> 6) + 1) << 6; |
| 272 | |
| 273 | } while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length); |
| 274 | |
| 275 | catc->netdev->last_rx = jiffies; |
| 276 | |
| 277 | if (catc->is_f5u011) { |
| 278 | if (atomic_read(&catc->recq_sz)) { |
| 279 | int status; |
| 280 | atomic_dec(&catc->recq_sz); |
| 281 | dbg("getting extra packet"); |
| 282 | urb->dev = catc->usbdev; |
| 283 | if ((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 284 | dbg("submit(rx_urb) status %d", status); |
| 285 | } |
| 286 | } else { |
| 287 | clear_bit(RX_RUNNING, &catc->flags); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 292 | static void catc_irq_done(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | struct catc *catc = urb->context; |
| 295 | u8 *data = urb->transfer_buffer; |
| 296 | int status; |
| 297 | unsigned int hasdata = 0, linksts = LinkNoChange; |
| 298 | |
| 299 | if (!catc->is_f5u011) { |
| 300 | hasdata = data[1] & 0x80; |
| 301 | if (data[1] & 0x40) |
| 302 | linksts = LinkGood; |
| 303 | else if (data[1] & 0x20) |
| 304 | linksts = LinkBad; |
| 305 | } else { |
| 306 | hasdata = (unsigned int)(be16_to_cpup((__be16*)data) & 0x0fff); |
| 307 | if (data[0] == 0x90) |
| 308 | linksts = LinkGood; |
| 309 | else if (data[0] == 0xA0) |
| 310 | linksts = LinkBad; |
| 311 | } |
| 312 | |
| 313 | switch (urb->status) { |
| 314 | case 0: /* success */ |
| 315 | break; |
| 316 | case -ECONNRESET: /* unlink */ |
| 317 | case -ENOENT: |
| 318 | case -ESHUTDOWN: |
| 319 | return; |
| 320 | /* -EPIPE: should clear the halt */ |
| 321 | default: /* error */ |
| 322 | dbg("irq_done, status %d, data %02x %02x.", urb->status, data[0], data[1]); |
| 323 | goto resubmit; |
| 324 | } |
| 325 | |
| 326 | if (linksts == LinkGood) { |
| 327 | netif_carrier_on(catc->netdev); |
| 328 | dbg("link ok"); |
| 329 | } |
| 330 | |
| 331 | if (linksts == LinkBad) { |
| 332 | netif_carrier_off(catc->netdev); |
| 333 | dbg("link bad"); |
| 334 | } |
| 335 | |
| 336 | if (hasdata) { |
| 337 | if (test_and_set_bit(RX_RUNNING, &catc->flags)) { |
| 338 | if (catc->is_f5u011) |
| 339 | atomic_inc(&catc->recq_sz); |
| 340 | } else { |
| 341 | catc->rx_urb->dev = catc->usbdev; |
| 342 | if ((status = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) { |
| 343 | err("submit(rx_urb) status %d", status); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | resubmit: |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame^] | 348 | status = usb_submit_urb (urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (status) |
| 350 | err ("can't resubmit intr, %s-%s, status %d", |
| 351 | catc->usbdev->bus->bus_name, |
| 352 | catc->usbdev->devpath, status); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Transmit routines. |
| 357 | */ |
| 358 | |
| 359 | static void catc_tx_run(struct catc *catc) |
| 360 | { |
| 361 | int status; |
| 362 | |
| 363 | if (catc->is_f5u011) |
| 364 | catc->tx_ptr = (catc->tx_ptr + 63) & ~63; |
| 365 | |
| 366 | catc->tx_urb->transfer_buffer_length = catc->tx_ptr; |
| 367 | catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx]; |
| 368 | catc->tx_urb->dev = catc->usbdev; |
| 369 | |
| 370 | if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0) |
| 371 | err("submit(tx_urb), status %d", status); |
| 372 | |
| 373 | catc->tx_idx = !catc->tx_idx; |
| 374 | catc->tx_ptr = 0; |
| 375 | |
| 376 | catc->netdev->trans_start = jiffies; |
| 377 | } |
| 378 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 379 | static void catc_tx_done(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | struct catc *catc = urb->context; |
| 382 | unsigned long flags; |
| 383 | |
| 384 | if (urb->status == -ECONNRESET) { |
| 385 | dbg("Tx Reset."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | urb->status = 0; |
| 387 | catc->netdev->trans_start = jiffies; |
| 388 | catc->stats.tx_errors++; |
| 389 | clear_bit(TX_RUNNING, &catc->flags); |
| 390 | netif_wake_queue(catc->netdev); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | if (urb->status) { |
| 395 | dbg("tx_done, status %d, length %d", urb->status, urb->actual_length); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | spin_lock_irqsave(&catc->tx_lock, flags); |
| 400 | |
| 401 | if (catc->tx_ptr) |
| 402 | catc_tx_run(catc); |
| 403 | else |
| 404 | clear_bit(TX_RUNNING, &catc->flags); |
| 405 | |
| 406 | netif_wake_queue(catc->netdev); |
| 407 | |
| 408 | spin_unlock_irqrestore(&catc->tx_lock, flags); |
| 409 | } |
| 410 | |
| 411 | static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 412 | { |
| 413 | struct catc *catc = netdev_priv(netdev); |
| 414 | unsigned long flags; |
| 415 | char *tx_buf; |
| 416 | |
| 417 | spin_lock_irqsave(&catc->tx_lock, flags); |
| 418 | |
| 419 | catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6; |
| 420 | tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr; |
| 421 | *((u16*)tx_buf) = (catc->is_f5u011) ? cpu_to_be16((u16)skb->len) : cpu_to_le16((u16)skb->len); |
| 422 | memcpy(tx_buf + 2, skb->data, skb->len); |
| 423 | catc->tx_ptr += skb->len + 2; |
| 424 | |
| 425 | if (!test_and_set_bit(TX_RUNNING, &catc->flags)) |
| 426 | catc_tx_run(catc); |
| 427 | |
| 428 | if ((catc->is_f5u011 && catc->tx_ptr) |
| 429 | || (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2)))) |
| 430 | netif_stop_queue(netdev); |
| 431 | |
| 432 | spin_unlock_irqrestore(&catc->tx_lock, flags); |
| 433 | |
| 434 | catc->stats.tx_bytes += skb->len; |
| 435 | catc->stats.tx_packets++; |
| 436 | |
| 437 | dev_kfree_skb(skb); |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static void catc_tx_timeout(struct net_device *netdev) |
| 443 | { |
| 444 | struct catc *catc = netdev_priv(netdev); |
| 445 | |
| 446 | warn("Transmit timed out."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | usb_unlink_urb(catc->tx_urb); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Control messages. |
| 452 | */ |
| 453 | |
| 454 | static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len) |
| 455 | { |
| 456 | int retval = usb_control_msg(catc->usbdev, |
| 457 | dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0), |
| 458 | request, 0x40 | dir, value, index, buf, len, 1000); |
| 459 | return retval < 0 ? retval : 0; |
| 460 | } |
| 461 | |
| 462 | static void catc_ctrl_run(struct catc *catc) |
| 463 | { |
| 464 | struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail; |
| 465 | struct usb_device *usbdev = catc->usbdev; |
| 466 | struct urb *urb = catc->ctrl_urb; |
| 467 | struct usb_ctrlrequest *dr = &catc->ctrl_dr; |
| 468 | int status; |
| 469 | |
| 470 | dr->bRequest = q->request; |
| 471 | dr->bRequestType = 0x40 | q->dir; |
| 472 | dr->wValue = cpu_to_le16(q->value); |
| 473 | dr->wIndex = cpu_to_le16(q->index); |
| 474 | dr->wLength = cpu_to_le16(q->len); |
| 475 | |
| 476 | urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0); |
| 477 | urb->transfer_buffer_length = q->len; |
| 478 | urb->transfer_buffer = catc->ctrl_buf; |
| 479 | urb->setup_packet = (void *) dr; |
| 480 | urb->dev = usbdev; |
| 481 | |
| 482 | if (!q->dir && q->buf && q->len) |
| 483 | memcpy(catc->ctrl_buf, q->buf, q->len); |
| 484 | |
| 485 | if ((status = usb_submit_urb(catc->ctrl_urb, GFP_KERNEL))) |
| 486 | err("submit(ctrl_urb) status %d", status); |
| 487 | } |
| 488 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 489 | static void catc_ctrl_done(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
| 491 | struct catc *catc = urb->context; |
| 492 | struct ctrl_queue *q; |
| 493 | unsigned long flags; |
| 494 | |
| 495 | if (urb->status) |
| 496 | dbg("ctrl_done, status %d, len %d.", urb->status, urb->actual_length); |
| 497 | |
| 498 | spin_lock_irqsave(&catc->ctrl_lock, flags); |
| 499 | |
| 500 | q = catc->ctrl_queue + catc->ctrl_tail; |
| 501 | |
| 502 | if (q->dir) { |
| 503 | if (q->buf && q->len) |
| 504 | memcpy(q->buf, catc->ctrl_buf, q->len); |
| 505 | else |
| 506 | q->buf = catc->ctrl_buf; |
| 507 | } |
| 508 | |
| 509 | if (q->callback) |
| 510 | q->callback(catc, q); |
| 511 | |
| 512 | catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1); |
| 513 | |
| 514 | if (catc->ctrl_head != catc->ctrl_tail) |
| 515 | catc_ctrl_run(catc); |
| 516 | else |
| 517 | clear_bit(CTRL_RUNNING, &catc->flags); |
| 518 | |
| 519 | spin_unlock_irqrestore(&catc->ctrl_lock, flags); |
| 520 | } |
| 521 | |
| 522 | static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value, |
| 523 | u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q)) |
| 524 | { |
| 525 | struct ctrl_queue *q; |
| 526 | int retval = 0; |
| 527 | unsigned long flags; |
| 528 | |
| 529 | spin_lock_irqsave(&catc->ctrl_lock, flags); |
| 530 | |
| 531 | q = catc->ctrl_queue + catc->ctrl_head; |
| 532 | |
| 533 | q->dir = dir; |
| 534 | q->request = request; |
| 535 | q->value = value; |
| 536 | q->index = index; |
| 537 | q->buf = buf; |
| 538 | q->len = len; |
| 539 | q->callback = callback; |
| 540 | |
| 541 | catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1); |
| 542 | |
| 543 | if (catc->ctrl_head == catc->ctrl_tail) { |
| 544 | err("ctrl queue full"); |
| 545 | catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1); |
| 546 | retval = -1; |
| 547 | } |
| 548 | |
| 549 | if (!test_and_set_bit(CTRL_RUNNING, &catc->flags)) |
| 550 | catc_ctrl_run(catc); |
| 551 | |
| 552 | spin_unlock_irqrestore(&catc->ctrl_lock, flags); |
| 553 | |
| 554 | return retval; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Statistics. |
| 559 | */ |
| 560 | |
| 561 | static void catc_stats_done(struct catc *catc, struct ctrl_queue *q) |
| 562 | { |
| 563 | int index = q->index - EthStats; |
| 564 | u16 data, last; |
| 565 | |
| 566 | catc->stats_buf[index] = *((char *)q->buf); |
| 567 | |
| 568 | if (index & 1) |
| 569 | return; |
| 570 | |
| 571 | data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1]; |
| 572 | last = catc->stats_vals[index >> 1]; |
| 573 | |
| 574 | switch (index) { |
| 575 | case TxSingleColl: |
| 576 | case TxMultiColl: |
| 577 | catc->stats.collisions += data - last; |
| 578 | break; |
| 579 | case TxExcessColl: |
| 580 | catc->stats.tx_aborted_errors += data - last; |
| 581 | catc->stats.tx_errors += data - last; |
| 582 | break; |
| 583 | case RxFramErr: |
| 584 | catc->stats.rx_frame_errors += data - last; |
| 585 | catc->stats.rx_errors += data - last; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | catc->stats_vals[index >> 1] = data; |
| 590 | } |
| 591 | |
| 592 | static void catc_stats_timer(unsigned long data) |
| 593 | { |
| 594 | struct catc *catc = (void *) data; |
| 595 | int i; |
| 596 | |
| 597 | for (i = 0; i < 8; i++) |
| 598 | catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done); |
| 599 | |
| 600 | mod_timer(&catc->timer, jiffies + STATS_UPDATE); |
| 601 | } |
| 602 | |
| 603 | static struct net_device_stats *catc_get_stats(struct net_device *netdev) |
| 604 | { |
| 605 | struct catc *catc = netdev_priv(netdev); |
| 606 | return &catc->stats; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Receive modes. Broadcast, Multicast, Promisc. |
| 611 | */ |
| 612 | |
| 613 | static void catc_multicast(unsigned char *addr, u8 *multicast) |
| 614 | { |
| 615 | u32 crc; |
| 616 | |
| 617 | crc = ether_crc_le(6, addr); |
| 618 | multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7); |
| 619 | } |
| 620 | |
| 621 | static void catc_set_multicast_list(struct net_device *netdev) |
| 622 | { |
| 623 | struct catc *catc = netdev_priv(netdev); |
| 624 | struct dev_mc_list *mc; |
| 625 | u8 broadcast[6]; |
| 626 | u8 rx = RxEnable | RxPolarity | RxMultiCast; |
| 627 | int i; |
| 628 | |
| 629 | memset(broadcast, 0xff, 6); |
| 630 | memset(catc->multicast, 0, 64); |
| 631 | |
| 632 | catc_multicast(broadcast, catc->multicast); |
| 633 | catc_multicast(netdev->dev_addr, catc->multicast); |
| 634 | |
| 635 | if (netdev->flags & IFF_PROMISC) { |
| 636 | memset(catc->multicast, 0xff, 64); |
| 637 | rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc; |
| 638 | } |
| 639 | |
| 640 | if (netdev->flags & IFF_ALLMULTI) { |
| 641 | memset(catc->multicast, 0xff, 64); |
| 642 | } else { |
| 643 | for (i = 0, mc = netdev->mc_list; mc && i < netdev->mc_count; i++, mc = mc->next) { |
| 644 | u32 crc = ether_crc_le(6, mc->dmi_addr); |
| 645 | if (!catc->is_f5u011) { |
| 646 | catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7); |
| 647 | } else { |
| 648 | catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | if (!catc->is_f5u011) { |
| 653 | catc_set_reg_async(catc, RxUnit, rx); |
| 654 | catc_write_mem_async(catc, 0xfa80, catc->multicast, 64); |
| 655 | } else { |
| 656 | f5u011_mchash_async(catc, catc->multicast); |
| 657 | if (catc->rxmode[0] != rx) { |
| 658 | catc->rxmode[0] = rx; |
| 659 | dbg("Setting RX mode to %2.2X %2.2X", catc->rxmode[0], catc->rxmode[1]); |
| 660 | f5u011_rxmode_async(catc, catc->rxmode); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | static void catc_get_drvinfo(struct net_device *dev, |
| 666 | struct ethtool_drvinfo *info) |
| 667 | { |
| 668 | struct catc *catc = netdev_priv(dev); |
| 669 | strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN); |
| 670 | strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); |
| 671 | usb_make_path (catc->usbdev, info->bus_info, sizeof info->bus_info); |
| 672 | } |
| 673 | |
| 674 | static int catc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 675 | { |
| 676 | struct catc *catc = netdev_priv(dev); |
| 677 | if (!catc->is_f5u011) |
| 678 | return -EOPNOTSUPP; |
| 679 | |
| 680 | cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_TP; |
| 681 | cmd->advertising = ADVERTISED_10baseT_Half | ADVERTISED_TP; |
| 682 | cmd->speed = SPEED_10; |
| 683 | cmd->duplex = DUPLEX_HALF; |
| 684 | cmd->port = PORT_TP; |
| 685 | cmd->phy_address = 0; |
| 686 | cmd->transceiver = XCVR_INTERNAL; |
| 687 | cmd->autoneg = AUTONEG_DISABLE; |
| 688 | cmd->maxtxpkt = 1; |
| 689 | cmd->maxrxpkt = 1; |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static struct ethtool_ops ops = { |
| 694 | .get_drvinfo = catc_get_drvinfo, |
| 695 | .get_settings = catc_get_settings, |
| 696 | .get_link = ethtool_op_get_link |
| 697 | }; |
| 698 | |
| 699 | /* |
| 700 | * Open, close. |
| 701 | */ |
| 702 | |
| 703 | static int catc_open(struct net_device *netdev) |
| 704 | { |
| 705 | struct catc *catc = netdev_priv(netdev); |
| 706 | int status; |
| 707 | |
| 708 | catc->irq_urb->dev = catc->usbdev; |
| 709 | if ((status = usb_submit_urb(catc->irq_urb, GFP_KERNEL)) < 0) { |
| 710 | err("submit(irq_urb) status %d", status); |
| 711 | return -1; |
| 712 | } |
| 713 | |
| 714 | netif_start_queue(netdev); |
| 715 | |
| 716 | if (!catc->is_f5u011) |
| 717 | mod_timer(&catc->timer, jiffies + STATS_UPDATE); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int catc_stop(struct net_device *netdev) |
| 723 | { |
| 724 | struct catc *catc = netdev_priv(netdev); |
| 725 | |
| 726 | netif_stop_queue(netdev); |
| 727 | |
| 728 | if (!catc->is_f5u011) |
| 729 | del_timer_sync(&catc->timer); |
| 730 | |
| 731 | usb_kill_urb(catc->rx_urb); |
| 732 | usb_kill_urb(catc->tx_urb); |
| 733 | usb_kill_urb(catc->irq_urb); |
| 734 | usb_kill_urb(catc->ctrl_urb); |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * USB probe, disconnect. |
| 741 | */ |
| 742 | |
| 743 | static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 744 | { |
| 745 | struct usb_device *usbdev = interface_to_usbdev(intf); |
| 746 | struct net_device *netdev; |
| 747 | struct catc *catc; |
| 748 | u8 broadcast[6]; |
| 749 | int i, pktsz; |
| 750 | |
| 751 | if (usb_set_interface(usbdev, |
| 752 | intf->altsetting->desc.bInterfaceNumber, 1)) { |
| 753 | err("Can't set altsetting 1."); |
| 754 | return -EIO; |
| 755 | } |
| 756 | |
| 757 | netdev = alloc_etherdev(sizeof(struct catc)); |
| 758 | if (!netdev) |
| 759 | return -ENOMEM; |
| 760 | |
| 761 | catc = netdev_priv(netdev); |
| 762 | |
| 763 | netdev->open = catc_open; |
| 764 | netdev->hard_start_xmit = catc_hard_start_xmit; |
| 765 | netdev->stop = catc_stop; |
| 766 | netdev->get_stats = catc_get_stats; |
| 767 | netdev->tx_timeout = catc_tx_timeout; |
| 768 | netdev->watchdog_timeo = TX_TIMEOUT; |
| 769 | netdev->set_multicast_list = catc_set_multicast_list; |
| 770 | SET_ETHTOOL_OPS(netdev, &ops); |
| 771 | |
| 772 | catc->usbdev = usbdev; |
| 773 | catc->netdev = netdev; |
| 774 | |
| 775 | spin_lock_init(&catc->tx_lock); |
| 776 | spin_lock_init(&catc->ctrl_lock); |
| 777 | |
| 778 | init_timer(&catc->timer); |
| 779 | catc->timer.data = (long) catc; |
| 780 | catc->timer.function = catc_stats_timer; |
| 781 | |
| 782 | catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 783 | catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 784 | catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 785 | catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 786 | if ((!catc->ctrl_urb) || (!catc->tx_urb) || |
| 787 | (!catc->rx_urb) || (!catc->irq_urb)) { |
| 788 | err("No free urbs available."); |
Mariusz Kozlowski | c69694b | 2006-11-08 15:36:22 +0100 | [diff] [blame] | 789 | usb_free_urb(catc->ctrl_urb); |
| 790 | usb_free_urb(catc->tx_urb); |
| 791 | usb_free_urb(catc->rx_urb); |
| 792 | usb_free_urb(catc->irq_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | free_netdev(netdev); |
| 794 | return -ENOMEM; |
| 795 | } |
| 796 | |
| 797 | /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */ |
| 798 | if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 && |
| 799 | le16_to_cpu(usbdev->descriptor.idProduct) == 0xa && |
| 800 | le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) { |
| 801 | dbg("Testing for f5u011"); |
| 802 | catc->is_f5u011 = 1; |
| 803 | atomic_set(&catc->recq_sz, 0); |
| 804 | pktsz = RX_PKT_SZ; |
| 805 | } else { |
| 806 | pktsz = RX_MAX_BURST * (PKT_SZ + 2); |
| 807 | } |
| 808 | |
| 809 | usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0), |
| 810 | NULL, NULL, 0, catc_ctrl_done, catc); |
| 811 | |
| 812 | usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1), |
| 813 | NULL, 0, catc_tx_done, catc); |
| 814 | |
| 815 | usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1), |
| 816 | catc->rx_buf, pktsz, catc_rx_done, catc); |
| 817 | |
| 818 | usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2), |
| 819 | catc->irq_buf, 2, catc_irq_done, catc, 1); |
| 820 | |
| 821 | if (!catc->is_f5u011) { |
| 822 | dbg("Checking memory size\n"); |
| 823 | |
| 824 | i = 0x12345678; |
| 825 | catc_write_mem(catc, 0x7a80, &i, 4); |
| 826 | i = 0x87654321; |
| 827 | catc_write_mem(catc, 0xfa80, &i, 4); |
| 828 | catc_read_mem(catc, 0x7a80, &i, 4); |
| 829 | |
| 830 | switch (i) { |
| 831 | case 0x12345678: |
| 832 | catc_set_reg(catc, TxBufCount, 8); |
| 833 | catc_set_reg(catc, RxBufCount, 32); |
| 834 | dbg("64k Memory\n"); |
| 835 | break; |
| 836 | default: |
| 837 | warn("Couldn't detect memory size, assuming 32k"); |
| 838 | case 0x87654321: |
| 839 | catc_set_reg(catc, TxBufCount, 4); |
| 840 | catc_set_reg(catc, RxBufCount, 16); |
| 841 | dbg("32k Memory\n"); |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | dbg("Getting MAC from SEEROM."); |
| 846 | |
| 847 | catc_get_mac(catc, netdev->dev_addr); |
| 848 | |
| 849 | dbg("Setting MAC into registers."); |
| 850 | |
| 851 | for (i = 0; i < 6; i++) |
| 852 | catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]); |
| 853 | |
| 854 | dbg("Filling the multicast list."); |
| 855 | |
| 856 | memset(broadcast, 0xff, 6); |
| 857 | catc_multicast(broadcast, catc->multicast); |
| 858 | catc_multicast(netdev->dev_addr, catc->multicast); |
| 859 | catc_write_mem(catc, 0xfa80, catc->multicast, 64); |
| 860 | |
| 861 | dbg("Clearing error counters."); |
| 862 | |
| 863 | for (i = 0; i < 8; i++) |
| 864 | catc_set_reg(catc, EthStats + i, 0); |
| 865 | catc->last_stats = jiffies; |
| 866 | |
| 867 | dbg("Enabling."); |
| 868 | |
| 869 | catc_set_reg(catc, MaxBurst, RX_MAX_BURST); |
| 870 | catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits); |
| 871 | catc_set_reg(catc, LEDCtrl, LEDLink); |
| 872 | catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast); |
| 873 | } else { |
| 874 | dbg("Performing reset\n"); |
| 875 | catc_reset(catc); |
| 876 | catc_get_mac(catc, netdev->dev_addr); |
| 877 | |
| 878 | dbg("Setting RX Mode"); |
| 879 | catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast; |
| 880 | catc->rxmode[1] = 0; |
| 881 | f5u011_rxmode(catc, catc->rxmode); |
| 882 | } |
| 883 | dbg("Init done."); |
| 884 | printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, ", |
| 885 | netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate", |
| 886 | usbdev->bus->bus_name, usbdev->devpath); |
| 887 | for (i = 0; i < 5; i++) printk("%2.2x:", netdev->dev_addr[i]); |
| 888 | printk("%2.2x.\n", netdev->dev_addr[i]); |
| 889 | usb_set_intfdata(intf, catc); |
| 890 | |
| 891 | SET_NETDEV_DEV(netdev, &intf->dev); |
| 892 | if (register_netdev(netdev) != 0) { |
| 893 | usb_set_intfdata(intf, NULL); |
| 894 | usb_free_urb(catc->ctrl_urb); |
| 895 | usb_free_urb(catc->tx_urb); |
| 896 | usb_free_urb(catc->rx_urb); |
| 897 | usb_free_urb(catc->irq_urb); |
| 898 | free_netdev(netdev); |
| 899 | return -EIO; |
| 900 | } |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static void catc_disconnect(struct usb_interface *intf) |
| 905 | { |
| 906 | struct catc *catc = usb_get_intfdata(intf); |
| 907 | |
| 908 | usb_set_intfdata(intf, NULL); |
| 909 | if (catc) { |
| 910 | unregister_netdev(catc->netdev); |
| 911 | usb_free_urb(catc->ctrl_urb); |
| 912 | usb_free_urb(catc->tx_urb); |
| 913 | usb_free_urb(catc->rx_urb); |
| 914 | usb_free_urb(catc->irq_urb); |
| 915 | free_netdev(catc->netdev); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Module functions and tables. |
| 921 | */ |
| 922 | |
| 923 | static struct usb_device_id catc_id_table [] = { |
| 924 | { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */ |
| 925 | { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */ |
| 926 | { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */ |
| 927 | { } |
| 928 | }; |
| 929 | |
| 930 | MODULE_DEVICE_TABLE(usb, catc_id_table); |
| 931 | |
| 932 | static struct usb_driver catc_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | .name = driver_name, |
| 934 | .probe = catc_probe, |
| 935 | .disconnect = catc_disconnect, |
| 936 | .id_table = catc_id_table, |
| 937 | }; |
| 938 | |
| 939 | static int __init catc_init(void) |
| 940 | { |
| 941 | int result = usb_register(&catc_driver); |
| 942 | if (result == 0) |
| 943 | info(DRIVER_VERSION " " DRIVER_DESC); |
| 944 | return result; |
| 945 | } |
| 946 | |
| 947 | static void __exit catc_exit(void) |
| 948 | { |
| 949 | usb_deregister(&catc_driver); |
| 950 | } |
| 951 | |
| 952 | module_init(catc_init); |
| 953 | module_exit(catc_exit); |