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