Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 2001 Vojtech Pavlik
0004  *
0005  *  CATC EL1210A NetMate USB Ethernet driver
0006  *
0007  *  Sponsored by SuSE
0008  *
0009  *  Based on the work of
0010  *      Donald Becker
0011  *
0012  *  Old chipset support added by Simon Evans <spse@secret.org.uk> 2002
0013  *    - adds support for Belkin F5U011
0014  */
0015 
0016 /*
0017  *
0018  * Should you need to contact me, the author, you can do so either by
0019  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
0020  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
0021  */
0022 
0023 #include <linux/module.h>
0024 #include <linux/kernel.h>
0025 #include <linux/string.h>
0026 #include <linux/netdevice.h>
0027 #include <linux/etherdevice.h>
0028 #include <linux/skbuff.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/ethtool.h>
0031 #include <linux/crc32.h>
0032 #include <linux/bitops.h>
0033 #include <linux/gfp.h>
0034 #include <linux/uaccess.h>
0035 
0036 #undef DEBUG
0037 
0038 #include <linux/usb.h>
0039 
0040 /*
0041  * Version information.
0042  */
0043 
0044 #define DRIVER_VERSION "v2.8"
0045 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>"
0046 #define DRIVER_DESC "CATC EL1210A NetMate USB Ethernet driver"
0047 #define SHORT_DRIVER_DESC "EL1210A NetMate USB Ethernet"
0048 
0049 MODULE_AUTHOR(DRIVER_AUTHOR);
0050 MODULE_DESCRIPTION(DRIVER_DESC);
0051 MODULE_LICENSE("GPL");
0052 
0053 static const char driver_name[] = "catc";
0054 
0055 /*
0056  * Some defines.
0057  */
0058 
0059 #define STATS_UPDATE        (HZ)    /* Time between stats updates */
0060 #define TX_TIMEOUT      (5*HZ)  /* Max time the queue can be stopped */
0061 #define PKT_SZ          1536    /* Max Ethernet packet size */
0062 #define RX_MAX_BURST        15  /* Max packets per rx buffer (> 0, < 16) */
0063 #define TX_MAX_BURST        15  /* Max full sized packets per tx buffer (> 0) */
0064 #define CTRL_QUEUE      16  /* Max control requests in flight (power of two) */
0065 #define RX_PKT_SZ       1600    /* Max size of receive packet for F5U011 */
0066 
0067 /*
0068  * Control requests.
0069  */
0070 
0071 enum control_requests {
0072     ReadMem =   0xf1,
0073     GetMac =    0xf2,
0074     Reset =     0xf4,
0075     SetMac =    0xf5,
0076     SetRxMode =     0xf5,  /* F5U011 only */
0077     WriteROM =  0xf8,
0078     SetReg =    0xfa,
0079     GetReg =    0xfb,
0080     WriteMem =  0xfc,
0081     ReadROM =   0xfd,
0082 };
0083 
0084 /*
0085  * Registers.
0086  */
0087 
0088 enum register_offsets {
0089     TxBufCount =    0x20,
0090     RxBufCount =    0x21,
0091     OpModes =   0x22,
0092     TxQed =     0x23,
0093     RxQed =     0x24,
0094     MaxBurst =  0x25,
0095     RxUnit =    0x60,
0096     EthStatus = 0x61,
0097     StationAddr0 =  0x67,
0098     EthStats =  0x69,
0099     LEDCtrl =   0x81,
0100 };
0101 
0102 enum eth_stats {
0103     TxSingleColl =  0x00,
0104         TxMultiColl =   0x02,
0105         TxExcessColl =  0x04,
0106         RxFramErr = 0x06,
0107 };
0108 
0109 enum op_mode_bits {
0110     Op3MemWaits =   0x03,
0111     OpLenInclude =  0x08,
0112     OpRxMerge = 0x10,
0113     OpTxMerge = 0x20,
0114     OpWin95bugfix = 0x40,
0115     OpLoopback =    0x80,
0116 };
0117 
0118 enum rx_filter_bits {
0119     RxEnable =  0x01,
0120     RxPolarity =    0x02,
0121     RxForceOK = 0x04,
0122     RxMultiCast =   0x08,
0123     RxPromisc = 0x10,
0124     AltRxPromisc =  0x20, /* F5U011 uses different bit */
0125 };
0126 
0127 enum led_values {
0128     LEDFast =   0x01,
0129     LEDSlow =   0x02,
0130     LEDFlash =  0x03,
0131     LEDPulse =  0x04,
0132     LEDLink =   0x08,
0133 };
0134 
0135 enum link_status {
0136     LinkNoChange = 0,
0137     LinkGood     = 1,
0138     LinkBad      = 2
0139 };
0140 
0141 /*
0142  * The catc struct.
0143  */
0144 
0145 #define CTRL_RUNNING    0
0146 #define RX_RUNNING  1
0147 #define TX_RUNNING  2
0148 
0149 struct catc {
0150     struct net_device *netdev;
0151     struct usb_device *usbdev;
0152 
0153     unsigned long flags;
0154 
0155     unsigned int tx_ptr, tx_idx;
0156     unsigned int ctrl_head, ctrl_tail;
0157     spinlock_t tx_lock, ctrl_lock;
0158 
0159     u8 tx_buf[2][TX_MAX_BURST * (PKT_SZ + 2)];
0160     u8 rx_buf[RX_MAX_BURST * (PKT_SZ + 2)];
0161     u8 irq_buf[2];
0162     u8 ctrl_buf[64];
0163     struct usb_ctrlrequest ctrl_dr;
0164 
0165     struct timer_list timer;
0166     u8 stats_buf[8];
0167     u16 stats_vals[4];
0168     unsigned long last_stats;
0169 
0170     u8 multicast[64];
0171 
0172     struct ctrl_queue {
0173         u8 dir;
0174         u8 request;
0175         u16 value;
0176         u16 index;
0177         void *buf;
0178         int len;
0179         void (*callback)(struct catc *catc, struct ctrl_queue *q);
0180     } ctrl_queue[CTRL_QUEUE];
0181 
0182     struct urb *tx_urb, *rx_urb, *irq_urb, *ctrl_urb;
0183 
0184     u8 is_f5u011;   /* Set if device is an F5U011 */
0185     u8 rxmode[2];   /* Used for F5U011 */
0186     atomic_t recq_sz; /* Used for F5U011 - counter of waiting rx packets */
0187 };
0188 
0189 /*
0190  * Useful macros.
0191  */
0192 
0193 #define catc_get_mac(catc, mac)             catc_ctrl_msg(catc, USB_DIR_IN,  GetMac, 0, 0, mac,  6)
0194 #define catc_reset(catc)                catc_ctrl_msg(catc, USB_DIR_OUT, Reset, 0, 0, NULL, 0)
0195 #define catc_set_reg(catc, reg, val)            catc_ctrl_msg(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0)
0196 #define catc_get_reg(catc, reg, buf)            catc_ctrl_msg(catc, USB_DIR_IN,  GetReg, 0, reg, buf, 1)
0197 #define catc_write_mem(catc, addr, buf, size)       catc_ctrl_msg(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size)
0198 #define catc_read_mem(catc, addr, buf, size)        catc_ctrl_msg(catc, USB_DIR_IN,  ReadMem, 0, addr, buf, size)
0199 
0200 #define f5u011_rxmode(catc, rxmode)         catc_ctrl_msg(catc, USB_DIR_OUT, SetRxMode, 0, 1, rxmode, 2)
0201 #define f5u011_rxmode_async(catc, rxmode)       catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 1, &rxmode, 2, NULL)
0202 #define f5u011_mchash_async(catc, hash)         catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 2, &hash, 8, NULL)
0203 
0204 #define catc_set_reg_async(catc, reg, val)      catc_ctrl_async(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0, NULL)
0205 #define catc_get_reg_async(catc, reg, cb)       catc_ctrl_async(catc, USB_DIR_IN, GetReg, 0, reg, NULL, 1, cb)
0206 #define catc_write_mem_async(catc, addr, buf, size) catc_ctrl_async(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size, NULL)
0207 
0208 /*
0209  * Receive routines.
0210  */
0211 
0212 static void catc_rx_done(struct urb *urb)
0213 {
0214     struct catc *catc = urb->context;
0215     u8 *pkt_start = urb->transfer_buffer;
0216     struct sk_buff *skb;
0217     int pkt_len, pkt_offset = 0;
0218     int status = urb->status;
0219 
0220     if (!catc->is_f5u011) {
0221         clear_bit(RX_RUNNING, &catc->flags);
0222         pkt_offset = 2;
0223     }
0224 
0225     if (status) {
0226         dev_dbg(&urb->dev->dev, "rx_done, status %d, length %d\n",
0227             status, urb->actual_length);
0228         return;
0229     }
0230 
0231     do {
0232         if(!catc->is_f5u011) {
0233             pkt_len = le16_to_cpup((__le16*)pkt_start);
0234             if (pkt_len > urb->actual_length) {
0235                 catc->netdev->stats.rx_length_errors++;
0236                 catc->netdev->stats.rx_errors++;
0237                 break;
0238             }
0239         } else {
0240             pkt_len = urb->actual_length;
0241         }
0242 
0243         if (!(skb = dev_alloc_skb(pkt_len)))
0244             return;
0245 
0246         skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
0247         skb_put(skb, pkt_len);
0248 
0249         skb->protocol = eth_type_trans(skb, catc->netdev);
0250         netif_rx(skb);
0251 
0252         catc->netdev->stats.rx_packets++;
0253         catc->netdev->stats.rx_bytes += pkt_len;
0254 
0255         /* F5U011 only does one packet per RX */
0256         if (catc->is_f5u011)
0257             break;
0258         pkt_start += (((pkt_len + 1) >> 6) + 1) << 6;
0259 
0260     } while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length);
0261 
0262     if (catc->is_f5u011) {
0263         if (atomic_read(&catc->recq_sz)) {
0264             int state;
0265             atomic_dec(&catc->recq_sz);
0266             netdev_dbg(catc->netdev, "getting extra packet\n");
0267             urb->dev = catc->usbdev;
0268             if ((state = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
0269                 netdev_dbg(catc->netdev,
0270                        "submit(rx_urb) status %d\n", state);
0271             }
0272         } else {
0273             clear_bit(RX_RUNNING, &catc->flags);
0274         }
0275     }
0276 }
0277 
0278 static void catc_irq_done(struct urb *urb)
0279 {
0280     struct catc *catc = urb->context;
0281     u8 *data = urb->transfer_buffer;
0282     int status = urb->status;
0283     unsigned int hasdata, linksts = LinkNoChange;
0284     int res;
0285 
0286     if (!catc->is_f5u011) {
0287         hasdata = data[1] & 0x80;
0288         if (data[1] & 0x40)
0289             linksts = LinkGood;
0290         else if (data[1] & 0x20)
0291             linksts = LinkBad;
0292     } else {
0293         hasdata = (unsigned int)(be16_to_cpup((__be16*)data) & 0x0fff);
0294         if (data[0] == 0x90)
0295             linksts = LinkGood;
0296         else if (data[0] == 0xA0)
0297             linksts = LinkBad;
0298     }
0299 
0300     switch (status) {
0301     case 0:         /* success */
0302         break;
0303     case -ECONNRESET:   /* unlink */
0304     case -ENOENT:
0305     case -ESHUTDOWN:
0306         return;
0307     /* -EPIPE:  should clear the halt */
0308     default:        /* error */
0309         dev_dbg(&urb->dev->dev,
0310             "irq_done, status %d, data %02x %02x.\n",
0311             status, data[0], data[1]);
0312         goto resubmit;
0313     }
0314 
0315     if (linksts == LinkGood) {
0316         netif_carrier_on(catc->netdev);
0317         netdev_dbg(catc->netdev, "link ok\n");
0318     }
0319 
0320     if (linksts == LinkBad) {
0321         netif_carrier_off(catc->netdev);
0322         netdev_dbg(catc->netdev, "link bad\n");
0323     }
0324 
0325     if (hasdata) {
0326         if (test_and_set_bit(RX_RUNNING, &catc->flags)) {
0327             if (catc->is_f5u011)
0328                 atomic_inc(&catc->recq_sz);
0329         } else {
0330             catc->rx_urb->dev = catc->usbdev;
0331             if ((res = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
0332                 dev_err(&catc->usbdev->dev,
0333                     "submit(rx_urb) status %d\n", res);
0334             }
0335         }
0336     }
0337 resubmit:
0338     res = usb_submit_urb (urb, GFP_ATOMIC);
0339     if (res)
0340         dev_err(&catc->usbdev->dev,
0341             "can't resubmit intr, %s-%s, status %d\n",
0342             catc->usbdev->bus->bus_name,
0343             catc->usbdev->devpath, res);
0344 }
0345 
0346 /*
0347  * Transmit routines.
0348  */
0349 
0350 static int catc_tx_run(struct catc *catc)
0351 {
0352     int status;
0353 
0354     if (catc->is_f5u011)
0355         catc->tx_ptr = (catc->tx_ptr + 63) & ~63;
0356 
0357     catc->tx_urb->transfer_buffer_length = catc->tx_ptr;
0358     catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx];
0359     catc->tx_urb->dev = catc->usbdev;
0360 
0361     if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0)
0362         dev_err(&catc->usbdev->dev, "submit(tx_urb), status %d\n",
0363             status);
0364 
0365     catc->tx_idx = !catc->tx_idx;
0366     catc->tx_ptr = 0;
0367 
0368     netif_trans_update(catc->netdev);
0369     return status;
0370 }
0371 
0372 static void catc_tx_done(struct urb *urb)
0373 {
0374     struct catc *catc = urb->context;
0375     unsigned long flags;
0376     int r, status = urb->status;
0377 
0378     if (status == -ECONNRESET) {
0379         dev_dbg(&urb->dev->dev, "Tx Reset.\n");
0380         urb->status = 0;
0381         netif_trans_update(catc->netdev);
0382         catc->netdev->stats.tx_errors++;
0383         clear_bit(TX_RUNNING, &catc->flags);
0384         netif_wake_queue(catc->netdev);
0385         return;
0386     }
0387 
0388     if (status) {
0389         dev_dbg(&urb->dev->dev, "tx_done, status %d, length %d\n",
0390             status, urb->actual_length);
0391         return;
0392     }
0393 
0394     spin_lock_irqsave(&catc->tx_lock, flags);
0395 
0396     if (catc->tx_ptr) {
0397         r = catc_tx_run(catc);
0398         if (unlikely(r < 0))
0399             clear_bit(TX_RUNNING, &catc->flags);
0400     } else {
0401         clear_bit(TX_RUNNING, &catc->flags);
0402     }
0403 
0404     netif_wake_queue(catc->netdev);
0405 
0406     spin_unlock_irqrestore(&catc->tx_lock, flags);
0407 }
0408 
0409 static netdev_tx_t catc_start_xmit(struct sk_buff *skb,
0410                      struct net_device *netdev)
0411 {
0412     struct catc *catc = netdev_priv(netdev);
0413     unsigned long flags;
0414     int r = 0;
0415     char *tx_buf;
0416 
0417     spin_lock_irqsave(&catc->tx_lock, flags);
0418 
0419     catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
0420     tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
0421     if (catc->is_f5u011)
0422         *(__be16 *)tx_buf = cpu_to_be16(skb->len);
0423     else
0424         *(__le16 *)tx_buf = cpu_to_le16(skb->len);
0425     skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
0426     catc->tx_ptr += skb->len + 2;
0427 
0428     if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
0429         r = catc_tx_run(catc);
0430         if (r < 0)
0431             clear_bit(TX_RUNNING, &catc->flags);
0432     }
0433 
0434     if ((catc->is_f5u011 && catc->tx_ptr) ||
0435         (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
0436         netif_stop_queue(netdev);
0437 
0438     spin_unlock_irqrestore(&catc->tx_lock, flags);
0439 
0440     if (r >= 0) {
0441         catc->netdev->stats.tx_bytes += skb->len;
0442         catc->netdev->stats.tx_packets++;
0443     }
0444 
0445     dev_kfree_skb(skb);
0446 
0447     return NETDEV_TX_OK;
0448 }
0449 
0450 static void catc_tx_timeout(struct net_device *netdev, unsigned int txqueue)
0451 {
0452     struct catc *catc = netdev_priv(netdev);
0453 
0454     dev_warn(&netdev->dev, "Transmit timed out.\n");
0455     usb_unlink_urb(catc->tx_urb);
0456 }
0457 
0458 /*
0459  * Control messages.
0460  */
0461 
0462 static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
0463 {
0464         int retval = usb_control_msg(catc->usbdev,
0465         dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
0466          request, 0x40 | dir, value, index, buf, len, 1000);
0467         return retval < 0 ? retval : 0;
0468 }
0469 
0470 static void catc_ctrl_run(struct catc *catc)
0471 {
0472     struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
0473     struct usb_device *usbdev = catc->usbdev;
0474     struct urb *urb = catc->ctrl_urb;
0475     struct usb_ctrlrequest *dr = &catc->ctrl_dr;
0476     int status;
0477 
0478     dr->bRequest = q->request;
0479     dr->bRequestType = 0x40 | q->dir;
0480     dr->wValue = cpu_to_le16(q->value);
0481     dr->wIndex = cpu_to_le16(q->index);
0482     dr->wLength = cpu_to_le16(q->len);
0483 
0484         urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
0485     urb->transfer_buffer_length = q->len;
0486     urb->transfer_buffer = catc->ctrl_buf;
0487     urb->setup_packet = (void *) dr;
0488     urb->dev = usbdev;
0489 
0490     if (!q->dir && q->buf && q->len)
0491         memcpy(catc->ctrl_buf, q->buf, q->len);
0492 
0493     if ((status = usb_submit_urb(catc->ctrl_urb, GFP_ATOMIC)))
0494         dev_err(&catc->usbdev->dev, "submit(ctrl_urb) status %d\n",
0495             status);
0496 }
0497 
0498 static void catc_ctrl_done(struct urb *urb)
0499 {
0500     struct catc *catc = urb->context;
0501     struct ctrl_queue *q;
0502     unsigned long flags;
0503     int status = urb->status;
0504 
0505     if (status)
0506         dev_dbg(&urb->dev->dev, "ctrl_done, status %d, len %d.\n",
0507             status, urb->actual_length);
0508 
0509     spin_lock_irqsave(&catc->ctrl_lock, flags);
0510 
0511     q = catc->ctrl_queue + catc->ctrl_tail;
0512 
0513     if (q->dir) {
0514         if (q->buf && q->len)
0515             memcpy(q->buf, catc->ctrl_buf, q->len);
0516         else
0517             q->buf = catc->ctrl_buf;
0518     }
0519 
0520     if (q->callback)
0521         q->callback(catc, q);
0522 
0523     catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
0524 
0525     if (catc->ctrl_head != catc->ctrl_tail)
0526         catc_ctrl_run(catc);
0527     else
0528         clear_bit(CTRL_RUNNING, &catc->flags);
0529 
0530     spin_unlock_irqrestore(&catc->ctrl_lock, flags);
0531 }
0532 
0533 static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
0534     u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
0535 {
0536     struct ctrl_queue *q;
0537     int retval = 0;
0538     unsigned long flags;
0539 
0540     spin_lock_irqsave(&catc->ctrl_lock, flags);
0541 
0542     q = catc->ctrl_queue + catc->ctrl_head;
0543 
0544     q->dir = dir;
0545     q->request = request;
0546     q->value = value;
0547     q->index = index;
0548     q->buf = buf;
0549     q->len = len;
0550     q->callback = callback;
0551 
0552     catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
0553 
0554     if (catc->ctrl_head == catc->ctrl_tail) {
0555         dev_err(&catc->usbdev->dev, "ctrl queue full\n");
0556         catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
0557         retval = -1;
0558     }
0559 
0560     if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
0561         catc_ctrl_run(catc);
0562 
0563     spin_unlock_irqrestore(&catc->ctrl_lock, flags);
0564 
0565     return retval;
0566 }
0567 
0568 /*
0569  * Statistics.
0570  */
0571 
0572 static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
0573 {
0574     int index = q->index - EthStats;
0575     u16 data, last;
0576 
0577     catc->stats_buf[index] = *((char *)q->buf);
0578 
0579     if (index & 1)
0580         return;
0581 
0582     data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
0583     last = catc->stats_vals[index >> 1];
0584 
0585     switch (index) {
0586         case TxSingleColl:
0587         case TxMultiColl:
0588             catc->netdev->stats.collisions += data - last;
0589             break;
0590         case TxExcessColl:
0591             catc->netdev->stats.tx_aborted_errors += data - last;
0592             catc->netdev->stats.tx_errors += data - last;
0593             break;
0594         case RxFramErr:
0595             catc->netdev->stats.rx_frame_errors += data - last;
0596             catc->netdev->stats.rx_errors += data - last;
0597             break;
0598     }
0599 
0600     catc->stats_vals[index >> 1] = data;
0601 }
0602 
0603 static void catc_stats_timer(struct timer_list *t)
0604 {
0605     struct catc *catc = from_timer(catc, t, timer);
0606     int i;
0607 
0608     for (i = 0; i < 8; i++)
0609         catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
0610 
0611     mod_timer(&catc->timer, jiffies + STATS_UPDATE);
0612 }
0613 
0614 /*
0615  * Receive modes. Broadcast, Multicast, Promisc.
0616  */
0617 
0618 static void catc_multicast(const unsigned char *addr, u8 *multicast)
0619 {
0620     u32 crc;
0621 
0622     crc = ether_crc_le(6, addr);
0623     multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
0624 }
0625 
0626 static void catc_set_multicast_list(struct net_device *netdev)
0627 {
0628     struct catc *catc = netdev_priv(netdev);
0629     struct netdev_hw_addr *ha;
0630     u8 broadcast[ETH_ALEN];
0631     u8 rx = RxEnable | RxPolarity | RxMultiCast;
0632 
0633     eth_broadcast_addr(broadcast);
0634     memset(catc->multicast, 0, 64);
0635 
0636     catc_multicast(broadcast, catc->multicast);
0637     catc_multicast(netdev->dev_addr, catc->multicast);
0638 
0639     if (netdev->flags & IFF_PROMISC) {
0640         memset(catc->multicast, 0xff, 64);
0641         rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc;
0642     }
0643 
0644     if (netdev->flags & IFF_ALLMULTI) {
0645         memset(catc->multicast, 0xff, 64);
0646     } else {
0647         netdev_for_each_mc_addr(ha, netdev) {
0648             u32 crc = ether_crc_le(6, ha->addr);
0649             if (!catc->is_f5u011) {
0650                 catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
0651             } else {
0652                 catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7);
0653             }
0654         }
0655     }
0656     if (!catc->is_f5u011) {
0657         catc_set_reg_async(catc, RxUnit, rx);
0658         catc_write_mem_async(catc, 0xfa80, catc->multicast, 64);
0659     } else {
0660         f5u011_mchash_async(catc, catc->multicast);
0661         if (catc->rxmode[0] != rx) {
0662             catc->rxmode[0] = rx;
0663             netdev_dbg(catc->netdev,
0664                    "Setting RX mode to %2.2X %2.2X\n",
0665                    catc->rxmode[0], catc->rxmode[1]);
0666             f5u011_rxmode_async(catc, catc->rxmode);
0667         }
0668     }
0669 }
0670 
0671 static void catc_get_drvinfo(struct net_device *dev,
0672                  struct ethtool_drvinfo *info)
0673 {
0674     struct catc *catc = netdev_priv(dev);
0675     strlcpy(info->driver, driver_name, sizeof(info->driver));
0676     strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
0677     usb_make_path(catc->usbdev, info->bus_info, sizeof(info->bus_info));
0678 }
0679 
0680 static int catc_get_link_ksettings(struct net_device *dev,
0681                    struct ethtool_link_ksettings *cmd)
0682 {
0683     struct catc *catc = netdev_priv(dev);
0684     if (!catc->is_f5u011)
0685         return -EOPNOTSUPP;
0686 
0687     ethtool_link_ksettings_zero_link_mode(cmd, supported);
0688     ethtool_link_ksettings_add_link_mode(cmd, supported, 10baseT_Half);
0689     ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
0690 
0691     ethtool_link_ksettings_zero_link_mode(cmd, advertising);
0692     ethtool_link_ksettings_add_link_mode(cmd, advertising, 10baseT_Half);
0693     ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
0694 
0695     cmd->base.speed = SPEED_10;
0696     cmd->base.duplex = DUPLEX_HALF;
0697     cmd->base.port = PORT_TP;
0698     cmd->base.phy_address = 0;
0699     cmd->base.autoneg = AUTONEG_DISABLE;
0700 
0701     return 0;
0702 }
0703 
0704 static const struct ethtool_ops ops = {
0705     .get_drvinfo = catc_get_drvinfo,
0706     .get_link = ethtool_op_get_link,
0707     .get_link_ksettings = catc_get_link_ksettings,
0708 };
0709 
0710 /*
0711  * Open, close.
0712  */
0713 
0714 static int catc_open(struct net_device *netdev)
0715 {
0716     struct catc *catc = netdev_priv(netdev);
0717     int status;
0718 
0719     catc->irq_urb->dev = catc->usbdev;
0720     if ((status = usb_submit_urb(catc->irq_urb, GFP_KERNEL)) < 0) {
0721         dev_err(&catc->usbdev->dev, "submit(irq_urb) status %d\n",
0722             status);
0723         return -1;
0724     }
0725 
0726     netif_start_queue(netdev);
0727 
0728     if (!catc->is_f5u011)
0729         mod_timer(&catc->timer, jiffies + STATS_UPDATE);
0730 
0731     return 0;
0732 }
0733 
0734 static int catc_stop(struct net_device *netdev)
0735 {
0736     struct catc *catc = netdev_priv(netdev);
0737 
0738     netif_stop_queue(netdev);
0739 
0740     if (!catc->is_f5u011)
0741         del_timer_sync(&catc->timer);
0742 
0743     usb_kill_urb(catc->rx_urb);
0744     usb_kill_urb(catc->tx_urb);
0745     usb_kill_urb(catc->irq_urb);
0746     usb_kill_urb(catc->ctrl_urb);
0747 
0748     return 0;
0749 }
0750 
0751 static const struct net_device_ops catc_netdev_ops = {
0752     .ndo_open       = catc_open,
0753     .ndo_stop       = catc_stop,
0754     .ndo_start_xmit     = catc_start_xmit,
0755 
0756     .ndo_tx_timeout     = catc_tx_timeout,
0757     .ndo_set_rx_mode    = catc_set_multicast_list,
0758     .ndo_set_mac_address    = eth_mac_addr,
0759     .ndo_validate_addr  = eth_validate_addr,
0760 };
0761 
0762 /*
0763  * USB probe, disconnect.
0764  */
0765 
0766 static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
0767 {
0768     struct device *dev = &intf->dev;
0769     struct usb_device *usbdev = interface_to_usbdev(intf);
0770     struct net_device *netdev;
0771     struct catc *catc;
0772     u8 broadcast[ETH_ALEN];
0773     u8 *macbuf;
0774     int pktsz, ret = -ENOMEM;
0775 
0776     macbuf = kmalloc(ETH_ALEN, GFP_KERNEL);
0777     if (!macbuf)
0778         goto error;
0779 
0780     if (usb_set_interface(usbdev,
0781             intf->altsetting->desc.bInterfaceNumber, 1)) {
0782         dev_err(dev, "Can't set altsetting 1.\n");
0783         ret = -EIO;
0784         goto fail_mem;
0785     }
0786 
0787     netdev = alloc_etherdev(sizeof(struct catc));
0788     if (!netdev)
0789         goto fail_mem;
0790 
0791     catc = netdev_priv(netdev);
0792 
0793     netdev->netdev_ops = &catc_netdev_ops;
0794     netdev->watchdog_timeo = TX_TIMEOUT;
0795     netdev->ethtool_ops = &ops;
0796 
0797     catc->usbdev = usbdev;
0798     catc->netdev = netdev;
0799 
0800     spin_lock_init(&catc->tx_lock);
0801     spin_lock_init(&catc->ctrl_lock);
0802 
0803     timer_setup(&catc->timer, catc_stats_timer, 0);
0804 
0805     catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
0806     catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
0807     catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
0808     catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
0809     if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
0810         (!catc->rx_urb) || (!catc->irq_urb)) {
0811         dev_err(&intf->dev, "No free urbs available.\n");
0812         ret = -ENOMEM;
0813         goto fail_free;
0814     }
0815 
0816     /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
0817     if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
0818         le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
0819         le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
0820         dev_dbg(dev, "Testing for f5u011\n");
0821         catc->is_f5u011 = 1;
0822         atomic_set(&catc->recq_sz, 0);
0823         pktsz = RX_PKT_SZ;
0824     } else {
0825         pktsz = RX_MAX_BURST * (PKT_SZ + 2);
0826     }
0827 
0828     usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
0829         NULL, NULL, 0, catc_ctrl_done, catc);
0830 
0831     usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
0832         NULL, 0, catc_tx_done, catc);
0833 
0834     usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
0835         catc->rx_buf, pktsz, catc_rx_done, catc);
0836 
0837     usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
0838                 catc->irq_buf, 2, catc_irq_done, catc, 1);
0839 
0840     if (!catc->is_f5u011) {
0841         u32 *buf;
0842         int i;
0843 
0844         dev_dbg(dev, "Checking memory size\n");
0845 
0846         buf = kmalloc(4, GFP_KERNEL);
0847         if (!buf) {
0848             ret = -ENOMEM;
0849             goto fail_free;
0850         }
0851 
0852         *buf = 0x12345678;
0853         catc_write_mem(catc, 0x7a80, buf, 4);
0854         *buf = 0x87654321;
0855         catc_write_mem(catc, 0xfa80, buf, 4);
0856         catc_read_mem(catc, 0x7a80, buf, 4);
0857 
0858         switch (*buf) {
0859         case 0x12345678:
0860             catc_set_reg(catc, TxBufCount, 8);
0861             catc_set_reg(catc, RxBufCount, 32);
0862             dev_dbg(dev, "64k Memory\n");
0863             break;
0864         default:
0865             dev_warn(&intf->dev,
0866                  "Couldn't detect memory size, assuming 32k\n");
0867             fallthrough;
0868         case 0x87654321:
0869             catc_set_reg(catc, TxBufCount, 4);
0870             catc_set_reg(catc, RxBufCount, 16);
0871             dev_dbg(dev, "32k Memory\n");
0872             break;
0873         }
0874 
0875         kfree(buf);
0876 
0877         dev_dbg(dev, "Getting MAC from SEEROM.\n");
0878 
0879         catc_get_mac(catc, macbuf);
0880         eth_hw_addr_set(netdev, macbuf);
0881 
0882         dev_dbg(dev, "Setting MAC into registers.\n");
0883 
0884         for (i = 0; i < 6; i++)
0885             catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
0886 
0887         dev_dbg(dev, "Filling the multicast list.\n");
0888 
0889         eth_broadcast_addr(broadcast);
0890         catc_multicast(broadcast, catc->multicast);
0891         catc_multicast(netdev->dev_addr, catc->multicast);
0892         catc_write_mem(catc, 0xfa80, catc->multicast, 64);
0893 
0894         dev_dbg(dev, "Clearing error counters.\n");
0895 
0896         for (i = 0; i < 8; i++)
0897             catc_set_reg(catc, EthStats + i, 0);
0898         catc->last_stats = jiffies;
0899 
0900         dev_dbg(dev, "Enabling.\n");
0901 
0902         catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
0903         catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
0904         catc_set_reg(catc, LEDCtrl, LEDLink);
0905         catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
0906     } else {
0907         dev_dbg(dev, "Performing reset\n");
0908         catc_reset(catc);
0909         catc_get_mac(catc, macbuf);
0910         eth_hw_addr_set(netdev, macbuf);
0911 
0912         dev_dbg(dev, "Setting RX Mode\n");
0913         catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
0914         catc->rxmode[1] = 0;
0915         f5u011_rxmode(catc, catc->rxmode);
0916     }
0917     dev_dbg(dev, "Init done.\n");
0918     printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, %pM.\n",
0919            netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
0920            usbdev->bus->bus_name, usbdev->devpath, netdev->dev_addr);
0921     usb_set_intfdata(intf, catc);
0922 
0923     SET_NETDEV_DEV(netdev, &intf->dev);
0924     ret = register_netdev(netdev);
0925     if (ret)
0926         goto fail_clear_intfdata;
0927 
0928     kfree(macbuf);
0929     return 0;
0930 
0931 fail_clear_intfdata:
0932     usb_set_intfdata(intf, NULL);
0933 fail_free:
0934     usb_free_urb(catc->ctrl_urb);
0935     usb_free_urb(catc->tx_urb);
0936     usb_free_urb(catc->rx_urb);
0937     usb_free_urb(catc->irq_urb);
0938     free_netdev(netdev);
0939 fail_mem:
0940     kfree(macbuf);
0941 error:
0942     return ret;
0943 }
0944 
0945 static void catc_disconnect(struct usb_interface *intf)
0946 {
0947     struct catc *catc = usb_get_intfdata(intf);
0948 
0949     usb_set_intfdata(intf, NULL);
0950     if (catc) {
0951         unregister_netdev(catc->netdev);
0952         usb_free_urb(catc->ctrl_urb);
0953         usb_free_urb(catc->tx_urb);
0954         usb_free_urb(catc->rx_urb);
0955         usb_free_urb(catc->irq_urb);
0956         free_netdev(catc->netdev);
0957     }
0958 }
0959 
0960 /*
0961  * Module functions and tables.
0962  */
0963 
0964 static const struct usb_device_id catc_id_table[] = {
0965     { USB_DEVICE(0x0423, 0xa) },    /* CATC Netmate, Belkin F5U011 */
0966     { USB_DEVICE(0x0423, 0xc) },    /* CATC Netmate II, Belkin F5U111 */
0967     { USB_DEVICE(0x08d1, 0x1) },    /* smartBridges smartNIC */
0968     { }
0969 };
0970 
0971 MODULE_DEVICE_TABLE(usb, catc_id_table);
0972 
0973 static struct usb_driver catc_driver = {
0974     .name =     driver_name,
0975     .probe =    catc_probe,
0976     .disconnect =   catc_disconnect,
0977     .id_table = catc_id_table,
0978     .disable_hub_initiated_lpm = 1,
0979 };
0980 
0981 module_usb_driver(catc_driver);