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