blob: 26c5bebd9ecab4c39c23044bbc45d0adc84a5897 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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
David Howells7d12e782006-10-05 14:55:46 +0100225static void catc_rx_done(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
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;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800231 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!catc->is_f5u011) {
234 clear_bit(RX_RUNNING, &catc->flags);
235 pkt_offset = 2;
236 }
237
Oliver Neukumc94cb312008-12-18 23:00:59 -0800238 if (status) {
239 dbg("rx_done, status %d, length %d", status, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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) {
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000247 catc->netdev->stats.rx_length_errors++;
248 catc->netdev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 }
251 } else {
252 pkt_len = urb->actual_length;
253 }
254
255 if (!(skb = dev_alloc_skb(pkt_len)))
256 return;
257
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700258 skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 skb_put(skb, pkt_len);
260
261 skb->protocol = eth_type_trans(skb, catc->netdev);
262 netif_rx(skb);
263
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000264 catc->netdev->stats.rx_packets++;
265 catc->netdev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (catc->is_f5u011) {
275 if (atomic_read(&catc->recq_sz)) {
Oliver Neukumc94cb312008-12-18 23:00:59 -0800276 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 atomic_dec(&catc->recq_sz);
278 dbg("getting extra packet");
279 urb->dev = catc->usbdev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800280 if ((state = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
281 dbg("submit(rx_urb) status %d", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 } else {
284 clear_bit(RX_RUNNING, &catc->flags);
285 }
286 }
287}
288
David Howells7d12e782006-10-05 14:55:46 +0100289static void catc_irq_done(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct catc *catc = urb->context;
292 u8 *data = urb->transfer_buffer;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800293 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned int hasdata = 0, linksts = LinkNoChange;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800295 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
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
Oliver Neukumc94cb312008-12-18 23:00:59 -0800311 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 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 */
Oliver Neukumc94cb312008-12-18 23:00:59 -0800320 dbg("irq_done, status %d, data %02x %02x.", status, data[0], data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 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;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800340 if ((res = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700341 dev_err(&catc->usbdev->dev,
342 "submit(rx_urb) status %d\n", res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 }
345 }
346resubmit:
Oliver Neukumc94cb312008-12-18 23:00:59 -0800347 res = usb_submit_urb (urb, GFP_ATOMIC);
348 if (res)
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700349 dev_err(&catc->usbdev->dev,
350 "can't resubmit intr, %s-%s, status %d\n",
351 catc->usbdev->bus->bus_name,
352 catc->usbdev->devpath, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
356 * Transmit routines.
357 */
358
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200359static int catc_tx_run(struct catc *catc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 int status;
362
363 if (catc->is_f5u011)
364 catc->tx_ptr = (catc->tx_ptr + 63) & ~63;
365
366 catc->tx_urb->transfer_buffer_length = catc->tx_ptr;
367 catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx];
368 catc->tx_urb->dev = catc->usbdev;
369
370 if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0)
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700371 dev_err(&catc->usbdev->dev, "submit(tx_urb), status %d\n",
372 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 catc->tx_idx = !catc->tx_idx;
375 catc->tx_ptr = 0;
376
377 catc->netdev->trans_start = jiffies;
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200378 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
David Howells7d12e782006-10-05 14:55:46 +0100381static void catc_tx_done(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct catc *catc = urb->context;
384 unsigned long flags;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800385 int r, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Oliver Neukumc94cb312008-12-18 23:00:59 -0800387 if (status == -ECONNRESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 dbg("Tx Reset.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 urb->status = 0;
390 catc->netdev->trans_start = jiffies;
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000391 catc->netdev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 clear_bit(TX_RUNNING, &catc->flags);
393 netif_wake_queue(catc->netdev);
394 return;
395 }
396
Oliver Neukumc94cb312008-12-18 23:00:59 -0800397 if (status) {
398 dbg("tx_done, status %d, length %d", status, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return;
400 }
401
402 spin_lock_irqsave(&catc->tx_lock, flags);
403
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200404 if (catc->tx_ptr) {
405 r = catc_tx_run(catc);
406 if (unlikely(r < 0))
407 clear_bit(TX_RUNNING, &catc->flags);
408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 clear_bit(TX_RUNNING, &catc->flags);
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 netif_wake_queue(catc->netdev);
413
414 spin_unlock_irqrestore(&catc->tx_lock, flags);
415}
416
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000417static netdev_tx_t catc_start_xmit(struct sk_buff *skb,
418 struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct catc *catc = netdev_priv(netdev);
421 unsigned long flags;
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200422 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 char *tx_buf;
424
425 spin_lock_irqsave(&catc->tx_lock, flags);
426
427 catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
428 tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
Al Viro4ec7ffa2008-05-21 06:32:11 +0100429 if (catc->is_f5u011)
430 *(__be16 *)tx_buf = cpu_to_be16(skb->len);
431 else
432 *(__le16 *)tx_buf = cpu_to_le16(skb->len);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300433 skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 catc->tx_ptr += skb->len + 2;
435
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200436 if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
437 r = catc_tx_run(catc);
438 if (r < 0)
439 clear_bit(TX_RUNNING, &catc->flags);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Joe Perches8e95a202009-12-03 07:58:21 +0000442 if ((catc->is_f5u011 && catc->tx_ptr) ||
443 (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 netif_stop_queue(netdev);
445
446 spin_unlock_irqrestore(&catc->tx_lock, flags);
447
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200448 if (r >= 0) {
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000449 catc->netdev->stats.tx_bytes += skb->len;
450 catc->netdev->stats.tx_packets++;
Oliver Neukum5a9f4e32007-03-30 13:11:00 +0200451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 dev_kfree_skb(skb);
454
Patrick McHardy6ed10652009-06-23 06:03:08 +0000455 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static void catc_tx_timeout(struct net_device *netdev)
459{
460 struct catc *catc = netdev_priv(netdev);
461
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700462 dev_warn(&netdev->dev, "Transmit timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 usb_unlink_urb(catc->tx_urb);
464}
465
466/*
467 * Control messages.
468 */
469
470static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
471{
472 int retval = usb_control_msg(catc->usbdev,
473 dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
474 request, 0x40 | dir, value, index, buf, len, 1000);
475 return retval < 0 ? retval : 0;
476}
477
478static void catc_ctrl_run(struct catc *catc)
479{
480 struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
481 struct usb_device *usbdev = catc->usbdev;
482 struct urb *urb = catc->ctrl_urb;
483 struct usb_ctrlrequest *dr = &catc->ctrl_dr;
484 int status;
485
486 dr->bRequest = q->request;
487 dr->bRequestType = 0x40 | q->dir;
488 dr->wValue = cpu_to_le16(q->value);
489 dr->wIndex = cpu_to_le16(q->index);
490 dr->wLength = cpu_to_le16(q->len);
491
492 urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
493 urb->transfer_buffer_length = q->len;
494 urb->transfer_buffer = catc->ctrl_buf;
495 urb->setup_packet = (void *) dr;
496 urb->dev = usbdev;
497
498 if (!q->dir && q->buf && q->len)
499 memcpy(catc->ctrl_buf, q->buf, q->len);
500
Alexey Khoroshilov930a6ea2011-05-30 07:06:24 +0000501 if ((status = usb_submit_urb(catc->ctrl_urb, GFP_ATOMIC)))
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700502 dev_err(&catc->usbdev->dev, "submit(ctrl_urb) status %d\n",
503 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
David Howells7d12e782006-10-05 14:55:46 +0100506static void catc_ctrl_done(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct catc *catc = urb->context;
509 struct ctrl_queue *q;
510 unsigned long flags;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800511 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Oliver Neukumc94cb312008-12-18 23:00:59 -0800513 if (status)
514 dbg("ctrl_done, status %d, len %d.", status, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 spin_lock_irqsave(&catc->ctrl_lock, flags);
517
518 q = catc->ctrl_queue + catc->ctrl_tail;
519
520 if (q->dir) {
521 if (q->buf && q->len)
522 memcpy(q->buf, catc->ctrl_buf, q->len);
523 else
524 q->buf = catc->ctrl_buf;
525 }
526
527 if (q->callback)
528 q->callback(catc, q);
529
530 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
531
532 if (catc->ctrl_head != catc->ctrl_tail)
533 catc_ctrl_run(catc);
534 else
535 clear_bit(CTRL_RUNNING, &catc->flags);
536
537 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
538}
539
540static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
541 u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
542{
543 struct ctrl_queue *q;
544 int retval = 0;
545 unsigned long flags;
546
547 spin_lock_irqsave(&catc->ctrl_lock, flags);
548
549 q = catc->ctrl_queue + catc->ctrl_head;
550
551 q->dir = dir;
552 q->request = request;
553 q->value = value;
554 q->index = index;
555 q->buf = buf;
556 q->len = len;
557 q->callback = callback;
558
559 catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
560
561 if (catc->ctrl_head == catc->ctrl_tail) {
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700562 dev_err(&catc->usbdev->dev, "ctrl queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
564 retval = -1;
565 }
566
567 if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
568 catc_ctrl_run(catc);
569
570 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
571
572 return retval;
573}
574
575/*
576 * Statistics.
577 */
578
579static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
580{
581 int index = q->index - EthStats;
582 u16 data, last;
583
584 catc->stats_buf[index] = *((char *)q->buf);
585
586 if (index & 1)
587 return;
588
589 data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
590 last = catc->stats_vals[index >> 1];
591
592 switch (index) {
593 case TxSingleColl:
594 case TxMultiColl:
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000595 catc->netdev->stats.collisions += data - last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 case TxExcessColl:
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000598 catc->netdev->stats.tx_aborted_errors += data - last;
599 catc->netdev->stats.tx_errors += data - last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 case RxFramErr:
Stephen Hemmingeredc4ae02009-03-20 19:35:48 +0000602 catc->netdev->stats.rx_frame_errors += data - last;
603 catc->netdev->stats.rx_errors += data - last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 }
606
607 catc->stats_vals[index >> 1] = data;
608}
609
610static void catc_stats_timer(unsigned long data)
611{
612 struct catc *catc = (void *) data;
613 int i;
614
615 for (i = 0; i < 8; i++)
616 catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
617
618 mod_timer(&catc->timer, jiffies + STATS_UPDATE);
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * Receive modes. Broadcast, Multicast, Promisc.
623 */
624
625static void catc_multicast(unsigned char *addr, u8 *multicast)
626{
627 u32 crc;
628
629 crc = ether_crc_le(6, addr);
630 multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
631}
632
633static void catc_set_multicast_list(struct net_device *netdev)
634{
635 struct catc *catc = netdev_priv(netdev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000636 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 u8 broadcast[6];
638 u8 rx = RxEnable | RxPolarity | RxMultiCast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 memset(broadcast, 0xff, 6);
641 memset(catc->multicast, 0, 64);
642
643 catc_multicast(broadcast, catc->multicast);
644 catc_multicast(netdev->dev_addr, catc->multicast);
645
646 if (netdev->flags & IFF_PROMISC) {
647 memset(catc->multicast, 0xff, 64);
648 rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc;
649 }
650
651 if (netdev->flags & IFF_ALLMULTI) {
652 memset(catc->multicast, 0xff, 64);
653 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000654 netdev_for_each_mc_addr(ha, netdev) {
655 u32 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!catc->is_f5u011) {
657 catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
658 } else {
659 catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7);
660 }
661 }
662 }
663 if (!catc->is_f5u011) {
664 catc_set_reg_async(catc, RxUnit, rx);
665 catc_write_mem_async(catc, 0xfa80, catc->multicast, 64);
666 } else {
667 f5u011_mchash_async(catc, catc->multicast);
668 if (catc->rxmode[0] != rx) {
669 catc->rxmode[0] = rx;
670 dbg("Setting RX mode to %2.2X %2.2X", catc->rxmode[0], catc->rxmode[1]);
671 f5u011_rxmode_async(catc, catc->rxmode);
672 }
673 }
674}
675
676static void catc_get_drvinfo(struct net_device *dev,
677 struct ethtool_drvinfo *info)
678{
679 struct catc *catc = netdev_priv(dev);
680 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
681 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
682 usb_make_path (catc->usbdev, info->bus_info, sizeof info->bus_info);
683}
684
685static int catc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
686{
687 struct catc *catc = netdev_priv(dev);
688 if (!catc->is_f5u011)
689 return -EOPNOTSUPP;
690
691 cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_TP;
692 cmd->advertising = ADVERTISED_10baseT_Half | ADVERTISED_TP;
David Decotigny70739492011-04-27 18:32:40 +0000693 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 cmd->duplex = DUPLEX_HALF;
695 cmd->port = PORT_TP;
696 cmd->phy_address = 0;
697 cmd->transceiver = XCVR_INTERNAL;
698 cmd->autoneg = AUTONEG_DISABLE;
699 cmd->maxtxpkt = 1;
700 cmd->maxrxpkt = 1;
701 return 0;
702}
703
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700704static const struct ethtool_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 .get_drvinfo = catc_get_drvinfo,
706 .get_settings = catc_get_settings,
707 .get_link = ethtool_op_get_link
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) {
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700721 dev_err(&catc->usbdev->dev, "submit(irq_urb) status %d\n",
722 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 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
Stephen Hemminger19b8f8f2009-03-20 19:35:49 +0000751static 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,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000757 .ndo_set_rx_mode = catc_set_multicast_list,
Stephen Hemminger19b8f8f2009-03-20 19:35:49 +0000758 .ndo_change_mtu = eth_change_mtu,
759 .ndo_set_mac_address = eth_mac_addr,
760 .ndo_validate_addr = eth_validate_addr,
761};
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
764 * USB probe, disconnect.
765 */
766
767static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
768{
769 struct usb_device *usbdev = interface_to_usbdev(intf);
770 struct net_device *netdev;
771 struct catc *catc;
772 u8 broadcast[6];
773 int i, pktsz;
774
775 if (usb_set_interface(usbdev,
776 intf->altsetting->desc.bInterfaceNumber, 1)) {
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700777 dev_err(&intf->dev, "Can't set altsetting 1.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EIO;
779 }
780
781 netdev = alloc_etherdev(sizeof(struct catc));
782 if (!netdev)
783 return -ENOMEM;
784
785 catc = netdev_priv(netdev);
786
Stephen Hemminger19b8f8f2009-03-20 19:35:49 +0000787 netdev->netdev_ops = &catc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 netdev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 SET_ETHTOOL_OPS(netdev, &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 init_timer(&catc->timer);
798 catc->timer.data = (long) catc;
799 catc->timer.function = catc_stats_timer;
800
801 catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
802 catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
803 catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
804 catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
805 if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
806 (!catc->rx_urb) || (!catc->irq_urb)) {
Greg Kroah-Hartman50c627c2012-04-25 12:37:47 -0700807 dev_err(&intf->dev, "No free urbs available.\n");
Mariusz Kozlowskic69694b2006-11-08 15:36:22 +0100808 usb_free_urb(catc->ctrl_urb);
809 usb_free_urb(catc->tx_urb);
810 usb_free_urb(catc->rx_urb);
811 usb_free_urb(catc->irq_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 free_netdev(netdev);
813 return -ENOMEM;
814 }
815
816 /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
817 if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
818 le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
819 le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
820 dbg("Testing for f5u011");
821 catc->is_f5u011 = 1;
822 atomic_set(&catc->recq_sz, 0);
823 pktsz = RX_PKT_SZ;
824 } else {
825 pktsz = RX_MAX_BURST * (PKT_SZ + 2);
826 }
827
828 usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
829 NULL, NULL, 0, catc_ctrl_done, catc);
830
831 usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
832 NULL, 0, catc_tx_done, catc);
833
834 usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
835 catc->rx_buf, pktsz, catc_rx_done, catc);
836
837 usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
838 catc->irq_buf, 2, catc_irq_done, catc, 1);
839
840 if (!catc->is_f5u011) {
841 dbg("Checking memory size\n");
842
843 i = 0x12345678;
844 catc_write_mem(catc, 0x7a80, &i, 4);
845 i = 0x87654321;
846 catc_write_mem(catc, 0xfa80, &i, 4);
847 catc_read_mem(catc, 0x7a80, &i, 4);
848
849 switch (i) {
850 case 0x12345678:
851 catc_set_reg(catc, TxBufCount, 8);
852 catc_set_reg(catc, RxBufCount, 32);
853 dbg("64k Memory\n");
854 break;
855 default:
Greg Kroah-Hartman4dc89942008-08-14 09:37:34 -0700856 dev_warn(&intf->dev,
857 "Couldn't detect memory size, assuming 32k\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case 0x87654321:
859 catc_set_reg(catc, TxBufCount, 4);
860 catc_set_reg(catc, RxBufCount, 16);
861 dbg("32k Memory\n");
862 break;
863 }
864
865 dbg("Getting MAC from SEEROM.");
866
867 catc_get_mac(catc, netdev->dev_addr);
868
869 dbg("Setting MAC into registers.");
870
871 for (i = 0; i < 6; i++)
872 catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
873
874 dbg("Filling the multicast list.");
875
876 memset(broadcast, 0xff, 6);
877 catc_multicast(broadcast, catc->multicast);
878 catc_multicast(netdev->dev_addr, catc->multicast);
879 catc_write_mem(catc, 0xfa80, catc->multicast, 64);
880
881 dbg("Clearing error counters.");
882
883 for (i = 0; i < 8; i++)
884 catc_set_reg(catc, EthStats + i, 0);
885 catc->last_stats = jiffies;
886
887 dbg("Enabling.");
888
889 catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
890 catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
891 catc_set_reg(catc, LEDCtrl, LEDLink);
892 catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
893 } else {
894 dbg("Performing reset\n");
895 catc_reset(catc);
896 catc_get_mac(catc, netdev->dev_addr);
897
898 dbg("Setting RX Mode");
899 catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
900 catc->rxmode[1] = 0;
901 f5u011_rxmode(catc, catc->rxmode);
902 }
903 dbg("Init done.");
H Hartley Sweeten3b8dff32009-12-29 20:06:45 -0800904 printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, %pM.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
H Hartley Sweeten3b8dff32009-12-29 20:06:45 -0800906 usbdev->bus->bus_name, usbdev->devpath, netdev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 usb_set_intfdata(intf, catc);
908
909 SET_NETDEV_DEV(netdev, &intf->dev);
910 if (register_netdev(netdev) != 0) {
911 usb_set_intfdata(intf, NULL);
912 usb_free_urb(catc->ctrl_urb);
913 usb_free_urb(catc->tx_urb);
914 usb_free_urb(catc->rx_urb);
915 usb_free_urb(catc->irq_urb);
916 free_netdev(netdev);
917 return -EIO;
918 }
919 return 0;
920}
921
922static void catc_disconnect(struct usb_interface *intf)
923{
924 struct catc *catc = usb_get_intfdata(intf);
925
926 usb_set_intfdata(intf, NULL);
927 if (catc) {
928 unregister_netdev(catc->netdev);
929 usb_free_urb(catc->ctrl_urb);
930 usb_free_urb(catc->tx_urb);
931 usb_free_urb(catc->rx_urb);
932 usb_free_urb(catc->irq_urb);
933 free_netdev(catc->netdev);
934 }
935}
936
937/*
938 * Module functions and tables.
939 */
940
941static struct usb_device_id catc_id_table [] = {
942 { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */
943 { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */
944 { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */
945 { }
946};
947
948MODULE_DEVICE_TABLE(usb, catc_id_table);
949
950static struct usb_driver catc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 .name = driver_name,
952 .probe = catc_probe,
953 .disconnect = catc_disconnect,
954 .id_table = catc_id_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700955 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956};
957
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800958module_usb_driver(catc_driver);