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