usbnet: convert catc to internal net_device_stats
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / usb / catc.c
CommitLineData
1da177e4
LT
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/slab.h>
40#include <linux/netdevice.h>
41#include <linux/etherdevice.h>
42#include <linux/skbuff.h>
43#include <linux/spinlock.h>
44#include <linux/ethtool.h>
45#include <linux/crc32.h>
46#include <linux/bitops.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
1da177e4
LT
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
7d12e780 225static void catc_rx_done(struct urb *urb)
1da177e4
LT
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;
c94cb314 231 int status = urb->status;
1da177e4
LT
232
233 if (!catc->is_f5u011) {
234 clear_bit(RX_RUNNING, &catc->flags);
235 pkt_offset = 2;
236 }
237
c94cb314
ON
238 if (status) {
239 dbg("rx_done, status %d, length %d", status, urb->actual_length);
1da177e4
LT
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) {
edc4ae08
SH
247 catc->netdev->stats.rx_length_errors++;
248 catc->netdev->stats.rx_errors++;
1da177e4
LT
249 break;
250 }
251 } else {
252 pkt_len = urb->actual_length;
253 }
254
255 if (!(skb = dev_alloc_skb(pkt_len)))
256 return;
257
8c7b7faa 258 skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
1da177e4
LT
259 skb_put(skb, pkt_len);
260
261 skb->protocol = eth_type_trans(skb, catc->netdev);
262 netif_rx(skb);
263
edc4ae08
SH
264 catc->netdev->stats.rx_packets++;
265 catc->netdev->stats.rx_bytes += pkt_len;
1da177e4
LT
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
1da177e4
LT
274 if (catc->is_f5u011) {
275 if (atomic_read(&catc->recq_sz)) {
c94cb314 276 int state;
1da177e4
LT
277 atomic_dec(&catc->recq_sz);
278 dbg("getting extra packet");
279 urb->dev = catc->usbdev;
c94cb314
ON
280 if ((state = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
281 dbg("submit(rx_urb) status %d", state);
1da177e4
LT
282 }
283 } else {
284 clear_bit(RX_RUNNING, &catc->flags);
285 }
286 }
287}
288
7d12e780 289static void catc_irq_done(struct urb *urb)
1da177e4
LT
290{
291 struct catc *catc = urb->context;
292 u8 *data = urb->transfer_buffer;
c94cb314 293 int status = urb->status;
1da177e4 294 unsigned int hasdata = 0, linksts = LinkNoChange;
c94cb314 295 int res;
1da177e4
LT
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
c94cb314 311 switch (status) {
1da177e4
LT
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 */
c94cb314 320 dbg("irq_done, status %d, data %02x %02x.", status, data[0], data[1]);
1da177e4
LT
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;
c94cb314
ON
340 if ((res = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
341 err("submit(rx_urb) status %d", res);
1da177e4
LT
342 }
343 }
344 }
345resubmit:
c94cb314
ON
346 res = usb_submit_urb (urb, GFP_ATOMIC);
347 if (res)
1da177e4
LT
348 err ("can't resubmit intr, %s-%s, status %d",
349 catc->usbdev->bus->bus_name,
c94cb314 350 catc->usbdev->devpath, res);
1da177e4
LT
351}
352
353/*
354 * Transmit routines.
355 */
356
5a9f4e33 357static int catc_tx_run(struct catc *catc)
1da177e4
LT
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;
5a9f4e33 375 return status;
1da177e4
LT
376}
377
7d12e780 378static void catc_tx_done(struct urb *urb)
1da177e4
LT
379{
380 struct catc *catc = urb->context;
381 unsigned long flags;
c94cb314 382 int r, status = urb->status;
1da177e4 383
c94cb314 384 if (status == -ECONNRESET) {
1da177e4 385 dbg("Tx Reset.");
1da177e4
LT
386 urb->status = 0;
387 catc->netdev->trans_start = jiffies;
edc4ae08 388 catc->netdev->stats.tx_errors++;
1da177e4
LT
389 clear_bit(TX_RUNNING, &catc->flags);
390 netif_wake_queue(catc->netdev);
391 return;
392 }
393
c94cb314
ON
394 if (status) {
395 dbg("tx_done, status %d, length %d", status, urb->actual_length);
1da177e4
LT
396 return;
397 }
398
399 spin_lock_irqsave(&catc->tx_lock, flags);
400
5a9f4e33
ON
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 {
1da177e4 406 clear_bit(TX_RUNNING, &catc->flags);
5a9f4e33 407 }
1da177e4
LT
408
409 netif_wake_queue(catc->netdev);
410
411 spin_unlock_irqrestore(&catc->tx_lock, flags);
412}
413
edc4ae08 414static int catc_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1da177e4
LT
415{
416 struct catc *catc = netdev_priv(netdev);
417 unsigned long flags;
5a9f4e33 418 int r = 0;
1da177e4
LT
419 char *tx_buf;
420
421 spin_lock_irqsave(&catc->tx_lock, flags);
422
423 catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
424 tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
4ec7ffa2
AV
425 if (catc->is_f5u011)
426 *(__be16 *)tx_buf = cpu_to_be16(skb->len);
427 else
428 *(__le16 *)tx_buf = cpu_to_le16(skb->len);
d626f62b 429 skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
1da177e4
LT
430 catc->tx_ptr += skb->len + 2;
431
5a9f4e33
ON
432 if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
433 r = catc_tx_run(catc);
434 if (r < 0)
435 clear_bit(TX_RUNNING, &catc->flags);
436 }
1da177e4
LT
437
438 if ((catc->is_f5u011 && catc->tx_ptr)
439 || (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
440 netif_stop_queue(netdev);
441
442 spin_unlock_irqrestore(&catc->tx_lock, flags);
443
5a9f4e33 444 if (r >= 0) {
edc4ae08
SH
445 catc->netdev->stats.tx_bytes += skb->len;
446 catc->netdev->stats.tx_packets++;
5a9f4e33 447 }
1da177e4
LT
448
449 dev_kfree_skb(skb);
450
451 return 0;
452}
453
454static void catc_tx_timeout(struct net_device *netdev)
455{
456 struct catc *catc = netdev_priv(netdev);
457
4dc89948 458 dev_warn(&netdev->dev, "Transmit timed out.\n");
1da177e4
LT
459 usb_unlink_urb(catc->tx_urb);
460}
461
462/*
463 * Control messages.
464 */
465
466static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
467{
468 int retval = usb_control_msg(catc->usbdev,
469 dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
470 request, 0x40 | dir, value, index, buf, len, 1000);
471 return retval < 0 ? retval : 0;
472}
473
474static void catc_ctrl_run(struct catc *catc)
475{
476 struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
477 struct usb_device *usbdev = catc->usbdev;
478 struct urb *urb = catc->ctrl_urb;
479 struct usb_ctrlrequest *dr = &catc->ctrl_dr;
480 int status;
481
482 dr->bRequest = q->request;
483 dr->bRequestType = 0x40 | q->dir;
484 dr->wValue = cpu_to_le16(q->value);
485 dr->wIndex = cpu_to_le16(q->index);
486 dr->wLength = cpu_to_le16(q->len);
487
488 urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
489 urb->transfer_buffer_length = q->len;
490 urb->transfer_buffer = catc->ctrl_buf;
491 urb->setup_packet = (void *) dr;
492 urb->dev = usbdev;
493
494 if (!q->dir && q->buf && q->len)
495 memcpy(catc->ctrl_buf, q->buf, q->len);
496
497 if ((status = usb_submit_urb(catc->ctrl_urb, GFP_KERNEL)))
498 err("submit(ctrl_urb) status %d", status);
499}
500
7d12e780 501static void catc_ctrl_done(struct urb *urb)
1da177e4
LT
502{
503 struct catc *catc = urb->context;
504 struct ctrl_queue *q;
505 unsigned long flags;
c94cb314 506 int status = urb->status;
1da177e4 507
c94cb314
ON
508 if (status)
509 dbg("ctrl_done, status %d, len %d.", status, urb->actual_length);
1da177e4
LT
510
511 spin_lock_irqsave(&catc->ctrl_lock, flags);
512
513 q = catc->ctrl_queue + catc->ctrl_tail;
514
515 if (q->dir) {
516 if (q->buf && q->len)
517 memcpy(q->buf, catc->ctrl_buf, q->len);
518 else
519 q->buf = catc->ctrl_buf;
520 }
521
522 if (q->callback)
523 q->callback(catc, q);
524
525 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
526
527 if (catc->ctrl_head != catc->ctrl_tail)
528 catc_ctrl_run(catc);
529 else
530 clear_bit(CTRL_RUNNING, &catc->flags);
531
532 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
533}
534
535static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
536 u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
537{
538 struct ctrl_queue *q;
539 int retval = 0;
540 unsigned long flags;
541
542 spin_lock_irqsave(&catc->ctrl_lock, flags);
543
544 q = catc->ctrl_queue + catc->ctrl_head;
545
546 q->dir = dir;
547 q->request = request;
548 q->value = value;
549 q->index = index;
550 q->buf = buf;
551 q->len = len;
552 q->callback = callback;
553
554 catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
555
556 if (catc->ctrl_head == catc->ctrl_tail) {
557 err("ctrl queue full");
558 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
559 retval = -1;
560 }
561
562 if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
563 catc_ctrl_run(catc);
564
565 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
566
567 return retval;
568}
569
570/*
571 * Statistics.
572 */
573
574static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
575{
576 int index = q->index - EthStats;
577 u16 data, last;
578
579 catc->stats_buf[index] = *((char *)q->buf);
580
581 if (index & 1)
582 return;
583
584 data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
585 last = catc->stats_vals[index >> 1];
586
587 switch (index) {
588 case TxSingleColl:
589 case TxMultiColl:
edc4ae08 590 catc->netdev->stats.collisions += data - last;
1da177e4
LT
591 break;
592 case TxExcessColl:
edc4ae08
SH
593 catc->netdev->stats.tx_aborted_errors += data - last;
594 catc->netdev->stats.tx_errors += data - last;
1da177e4
LT
595 break;
596 case RxFramErr:
edc4ae08
SH
597 catc->netdev->stats.rx_frame_errors += data - last;
598 catc->netdev->stats.rx_errors += data - last;
1da177e4
LT
599 break;
600 }
601
602 catc->stats_vals[index >> 1] = data;
603}
604
605static void catc_stats_timer(unsigned long data)
606{
607 struct catc *catc = (void *) data;
608 int i;
609
610 for (i = 0; i < 8; i++)
611 catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
612
613 mod_timer(&catc->timer, jiffies + STATS_UPDATE);
614}
615
1da177e4
LT
616/*
617 * Receive modes. Broadcast, Multicast, Promisc.
618 */
619
620static void catc_multicast(unsigned char *addr, u8 *multicast)
621{
622 u32 crc;
623
624 crc = ether_crc_le(6, addr);
625 multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
626}
627
628static void catc_set_multicast_list(struct net_device *netdev)
629{
630 struct catc *catc = netdev_priv(netdev);
631 struct dev_mc_list *mc;
632 u8 broadcast[6];
633 u8 rx = RxEnable | RxPolarity | RxMultiCast;
634 int i;
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 for (i = 0, mc = netdev->mc_list; mc && i < netdev->mc_count; i++, mc = mc->next) {
651 u32 crc = ether_crc_le(6, mc->dmi_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 cmd->speed = 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 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
746/*
747 * USB probe, disconnect.
748 */
749
750static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
751{
752 struct usb_device *usbdev = interface_to_usbdev(intf);
753 struct net_device *netdev;
754 struct catc *catc;
755 u8 broadcast[6];
756 int i, pktsz;
757
758 if (usb_set_interface(usbdev,
759 intf->altsetting->desc.bInterfaceNumber, 1)) {
760 err("Can't set altsetting 1.");
761 return -EIO;
762 }
763
764 netdev = alloc_etherdev(sizeof(struct catc));
765 if (!netdev)
766 return -ENOMEM;
767
768 catc = netdev_priv(netdev);
769
770 netdev->open = catc_open;
771 netdev->hard_start_xmit = catc_hard_start_xmit;
772 netdev->stop = catc_stop;
1da177e4
LT
773 netdev->tx_timeout = catc_tx_timeout;
774 netdev->watchdog_timeo = TX_TIMEOUT;
775 netdev->set_multicast_list = catc_set_multicast_list;
776 SET_ETHTOOL_OPS(netdev, &ops);
777
778 catc->usbdev = usbdev;
779 catc->netdev = netdev;
780
781 spin_lock_init(&catc->tx_lock);
782 spin_lock_init(&catc->ctrl_lock);
783
784 init_timer(&catc->timer);
785 catc->timer.data = (long) catc;
786 catc->timer.function = catc_stats_timer;
787
788 catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
789 catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
790 catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
791 catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
792 if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
793 (!catc->rx_urb) || (!catc->irq_urb)) {
794 err("No free urbs available.");
c69694b7
MK
795 usb_free_urb(catc->ctrl_urb);
796 usb_free_urb(catc->tx_urb);
797 usb_free_urb(catc->rx_urb);
798 usb_free_urb(catc->irq_urb);
1da177e4
LT
799 free_netdev(netdev);
800 return -ENOMEM;
801 }
802
803 /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
804 if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
805 le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
806 le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
807 dbg("Testing for f5u011");
808 catc->is_f5u011 = 1;
809 atomic_set(&catc->recq_sz, 0);
810 pktsz = RX_PKT_SZ;
811 } else {
812 pktsz = RX_MAX_BURST * (PKT_SZ + 2);
813 }
814
815 usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
816 NULL, NULL, 0, catc_ctrl_done, catc);
817
818 usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
819 NULL, 0, catc_tx_done, catc);
820
821 usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
822 catc->rx_buf, pktsz, catc_rx_done, catc);
823
824 usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
825 catc->irq_buf, 2, catc_irq_done, catc, 1);
826
827 if (!catc->is_f5u011) {
828 dbg("Checking memory size\n");
829
830 i = 0x12345678;
831 catc_write_mem(catc, 0x7a80, &i, 4);
832 i = 0x87654321;
833 catc_write_mem(catc, 0xfa80, &i, 4);
834 catc_read_mem(catc, 0x7a80, &i, 4);
835
836 switch (i) {
837 case 0x12345678:
838 catc_set_reg(catc, TxBufCount, 8);
839 catc_set_reg(catc, RxBufCount, 32);
840 dbg("64k Memory\n");
841 break;
842 default:
4dc89948
GKH
843 dev_warn(&intf->dev,
844 "Couldn't detect memory size, assuming 32k\n");
1da177e4
LT
845 case 0x87654321:
846 catc_set_reg(catc, TxBufCount, 4);
847 catc_set_reg(catc, RxBufCount, 16);
848 dbg("32k Memory\n");
849 break;
850 }
851
852 dbg("Getting MAC from SEEROM.");
853
854 catc_get_mac(catc, netdev->dev_addr);
855
856 dbg("Setting MAC into registers.");
857
858 for (i = 0; i < 6; i++)
859 catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
860
861 dbg("Filling the multicast list.");
862
863 memset(broadcast, 0xff, 6);
864 catc_multicast(broadcast, catc->multicast);
865 catc_multicast(netdev->dev_addr, catc->multicast);
866 catc_write_mem(catc, 0xfa80, catc->multicast, 64);
867
868 dbg("Clearing error counters.");
869
870 for (i = 0; i < 8; i++)
871 catc_set_reg(catc, EthStats + i, 0);
872 catc->last_stats = jiffies;
873
874 dbg("Enabling.");
875
876 catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
877 catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
878 catc_set_reg(catc, LEDCtrl, LEDLink);
879 catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
880 } else {
881 dbg("Performing reset\n");
882 catc_reset(catc);
883 catc_get_mac(catc, netdev->dev_addr);
884
885 dbg("Setting RX Mode");
886 catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
887 catc->rxmode[1] = 0;
888 f5u011_rxmode(catc, catc->rxmode);
889 }
890 dbg("Init done.");
891 printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, ",
892 netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
893 usbdev->bus->bus_name, usbdev->devpath);
894 for (i = 0; i < 5; i++) printk("%2.2x:", netdev->dev_addr[i]);
895 printk("%2.2x.\n", netdev->dev_addr[i]);
896 usb_set_intfdata(intf, catc);
897
898 SET_NETDEV_DEV(netdev, &intf->dev);
899 if (register_netdev(netdev) != 0) {
900 usb_set_intfdata(intf, NULL);
901 usb_free_urb(catc->ctrl_urb);
902 usb_free_urb(catc->tx_urb);
903 usb_free_urb(catc->rx_urb);
904 usb_free_urb(catc->irq_urb);
905 free_netdev(netdev);
906 return -EIO;
907 }
908 return 0;
909}
910
911static void catc_disconnect(struct usb_interface *intf)
912{
913 struct catc *catc = usb_get_intfdata(intf);
914
915 usb_set_intfdata(intf, NULL);
916 if (catc) {
917 unregister_netdev(catc->netdev);
918 usb_free_urb(catc->ctrl_urb);
919 usb_free_urb(catc->tx_urb);
920 usb_free_urb(catc->rx_urb);
921 usb_free_urb(catc->irq_urb);
922 free_netdev(catc->netdev);
923 }
924}
925
926/*
927 * Module functions and tables.
928 */
929
930static struct usb_device_id catc_id_table [] = {
931 { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */
932 { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */
933 { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */
934 { }
935};
936
937MODULE_DEVICE_TABLE(usb, catc_id_table);
938
939static struct usb_driver catc_driver = {
1da177e4
LT
940 .name = driver_name,
941 .probe = catc_probe,
942 .disconnect = catc_disconnect,
943 .id_table = catc_id_table,
944};
945
946static int __init catc_init(void)
947{
948 int result = usb_register(&catc_driver);
949 if (result == 0)
880c9c66
GKH
950 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
951 DRIVER_DESC "\n");
1da177e4
LT
952 return result;
953}
954
955static void __exit catc_exit(void)
956{
957 usb_deregister(&catc_driver);
958}
959
960module_init(catc_init);
961module_exit(catc_exit);