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