USB: remove dbg() usage in USB networking drivers
[linux-2.6/libata-dev.git] / drivers / net / usb / catc.c
blob18d9579123ea82a2b649e0f2bdcdaa6a9f2ca80c
1 /*
2 * Copyright (c) 2001 Vojtech Pavlik
4 * CATC EL1210A NetMate USB Ethernet driver
6 * Sponsored by SuSE
8 * Based on the work of
9 * Donald Becker
11 * Old chipset support added by Simon Evans <spse@secret.org.uk> 2002
12 * - adds support for Belkin F5U011
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.
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.
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
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
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>
49 #undef DEBUG
51 #include <linux/usb.h>
54 * Version information.
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"
62 MODULE_AUTHOR(DRIVER_AUTHOR);
63 MODULE_DESCRIPTION(DRIVER_DESC);
64 MODULE_LICENSE("GPL");
66 static const char driver_name[] = "catc";
69 * Some defines.
70 */
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 */
81 * Control requests.
84 enum 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,
98 * Registers.
101 enum 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,
115 enum eth_stats {
116 TxSingleColl = 0x00,
117 TxMultiColl = 0x02,
118 TxExcessColl = 0x04,
119 RxFramErr = 0x06,
122 enum op_mode_bits {
123 Op3MemWaits = 0x03,
124 OpLenInclude = 0x08,
125 OpRxMerge = 0x10,
126 OpTxMerge = 0x20,
127 OpWin95bugfix = 0x40,
128 OpLoopback = 0x80,
131 enum 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 */
140 enum led_values {
141 LEDFast = 0x01,
142 LEDSlow = 0x02,
143 LEDFlash = 0x03,
144 LEDPulse = 0x04,
145 LEDLink = 0x08,
148 enum link_status {
149 LinkNoChange = 0,
150 LinkGood = 1,
151 LinkBad = 2
155 * The catc struct.
158 #define CTRL_RUNNING 0
159 #define RX_RUNNING 1
160 #define TX_RUNNING 2
162 struct catc {
163 struct net_device *netdev;
164 struct usb_device *usbdev;
166 unsigned long flags;
168 unsigned int tx_ptr, tx_idx;
169 unsigned int ctrl_head, ctrl_tail;
170 spinlock_t tx_lock, ctrl_lock;
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;
178 struct timer_list timer;
179 u8 stats_buf[8];
180 u16 stats_vals[4];
181 unsigned long last_stats;
183 u8 multicast[64];
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];
195 struct urb *tx_urb, *rx_urb, *irq_urb, *ctrl_urb;
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 */
203 * Useful macros.
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)
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)
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)
222 * Receive routines.
225 static void catc_rx_done(struct urb *urb)
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;
233 if (!catc->is_f5u011) {
234 clear_bit(RX_RUNNING, &catc->flags);
235 pkt_offset = 2;
238 if (status) {
239 dev_dbg(&urb->dev->dev, "rx_done, status %d, length %d\n",
240 status, urb->actual_length);
241 return;
244 do {
245 if(!catc->is_f5u011) {
246 pkt_len = le16_to_cpup((__le16*)pkt_start);
247 if (pkt_len > urb->actual_length) {
248 catc->netdev->stats.rx_length_errors++;
249 catc->netdev->stats.rx_errors++;
250 break;
252 } else {
253 pkt_len = urb->actual_length;
256 if (!(skb = dev_alloc_skb(pkt_len)))
257 return;
259 skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
260 skb_put(skb, pkt_len);
262 skb->protocol = eth_type_trans(skb, catc->netdev);
263 netif_rx(skb);
265 catc->netdev->stats.rx_packets++;
266 catc->netdev->stats.rx_bytes += pkt_len;
268 /* F5U011 only does one packet per RX */
269 if (catc->is_f5u011)
270 break;
271 pkt_start += (((pkt_len + 1) >> 6) + 1) << 6;
273 } while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length);
275 if (catc->is_f5u011) {
276 if (atomic_read(&catc->recq_sz)) {
277 int state;
278 atomic_dec(&catc->recq_sz);
279 netdev_dbg(catc->netdev, "getting extra packet\n");
280 urb->dev = catc->usbdev;
281 if ((state = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
282 netdev_dbg(catc->netdev,
283 "submit(rx_urb) status %d\n", state);
285 } else {
286 clear_bit(RX_RUNNING, &catc->flags);
291 static void catc_irq_done(struct urb *urb)
293 struct catc *catc = urb->context;
294 u8 *data = urb->transfer_buffer;
295 int status = urb->status;
296 unsigned int hasdata = 0, linksts = LinkNoChange;
297 int res;
299 if (!catc->is_f5u011) {
300 hasdata = data[1] & 0x80;
301 if (data[1] & 0x40)
302 linksts = LinkGood;
303 else if (data[1] & 0x20)
304 linksts = LinkBad;
305 } else {
306 hasdata = (unsigned int)(be16_to_cpup((__be16*)data) & 0x0fff);
307 if (data[0] == 0x90)
308 linksts = LinkGood;
309 else if (data[0] == 0xA0)
310 linksts = LinkBad;
313 switch (status) {
314 case 0: /* success */
315 break;
316 case -ECONNRESET: /* unlink */
317 case -ENOENT:
318 case -ESHUTDOWN:
319 return;
320 /* -EPIPE: should clear the halt */
321 default: /* error */
322 dev_dbg(&urb->dev->dev,
323 "irq_done, status %d, data %02x %02x.\n",
324 status, data[0], data[1]);
325 goto resubmit;
328 if (linksts == LinkGood) {
329 netif_carrier_on(catc->netdev);
330 netdev_dbg(catc->netdev, "link ok\n");
333 if (linksts == LinkBad) {
334 netif_carrier_off(catc->netdev);
335 netdev_dbg(catc->netdev, "link bad\n");
338 if (hasdata) {
339 if (test_and_set_bit(RX_RUNNING, &catc->flags)) {
340 if (catc->is_f5u011)
341 atomic_inc(&catc->recq_sz);
342 } else {
343 catc->rx_urb->dev = catc->usbdev;
344 if ((res = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
345 dev_err(&catc->usbdev->dev,
346 "submit(rx_urb) status %d\n", res);
350 resubmit:
351 res = usb_submit_urb (urb, GFP_ATOMIC);
352 if (res)
353 dev_err(&catc->usbdev->dev,
354 "can't resubmit intr, %s-%s, status %d\n",
355 catc->usbdev->bus->bus_name,
356 catc->usbdev->devpath, res);
360 * Transmit routines.
363 static int catc_tx_run(struct catc *catc)
365 int status;
367 if (catc->is_f5u011)
368 catc->tx_ptr = (catc->tx_ptr + 63) & ~63;
370 catc->tx_urb->transfer_buffer_length = catc->tx_ptr;
371 catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx];
372 catc->tx_urb->dev = catc->usbdev;
374 if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0)
375 dev_err(&catc->usbdev->dev, "submit(tx_urb), status %d\n",
376 status);
378 catc->tx_idx = !catc->tx_idx;
379 catc->tx_ptr = 0;
381 catc->netdev->trans_start = jiffies;
382 return status;
385 static void catc_tx_done(struct urb *urb)
387 struct catc *catc = urb->context;
388 unsigned long flags;
389 int r, status = urb->status;
391 if (status == -ECONNRESET) {
392 dev_dbg(&urb->dev->dev, "Tx Reset.\n");
393 urb->status = 0;
394 catc->netdev->trans_start = jiffies;
395 catc->netdev->stats.tx_errors++;
396 clear_bit(TX_RUNNING, &catc->flags);
397 netif_wake_queue(catc->netdev);
398 return;
401 if (status) {
402 dev_dbg(&urb->dev->dev, "tx_done, status %d, length %d\n",
403 status, urb->actual_length);
404 return;
407 spin_lock_irqsave(&catc->tx_lock, flags);
409 if (catc->tx_ptr) {
410 r = catc_tx_run(catc);
411 if (unlikely(r < 0))
412 clear_bit(TX_RUNNING, &catc->flags);
413 } else {
414 clear_bit(TX_RUNNING, &catc->flags);
417 netif_wake_queue(catc->netdev);
419 spin_unlock_irqrestore(&catc->tx_lock, flags);
422 static netdev_tx_t catc_start_xmit(struct sk_buff *skb,
423 struct net_device *netdev)
425 struct catc *catc = netdev_priv(netdev);
426 unsigned long flags;
427 int r = 0;
428 char *tx_buf;
430 spin_lock_irqsave(&catc->tx_lock, flags);
432 catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
433 tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
434 if (catc->is_f5u011)
435 *(__be16 *)tx_buf = cpu_to_be16(skb->len);
436 else
437 *(__le16 *)tx_buf = cpu_to_le16(skb->len);
438 skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
439 catc->tx_ptr += skb->len + 2;
441 if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
442 r = catc_tx_run(catc);
443 if (r < 0)
444 clear_bit(TX_RUNNING, &catc->flags);
447 if ((catc->is_f5u011 && catc->tx_ptr) ||
448 (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
449 netif_stop_queue(netdev);
451 spin_unlock_irqrestore(&catc->tx_lock, flags);
453 if (r >= 0) {
454 catc->netdev->stats.tx_bytes += skb->len;
455 catc->netdev->stats.tx_packets++;
458 dev_kfree_skb(skb);
460 return NETDEV_TX_OK;
463 static void catc_tx_timeout(struct net_device *netdev)
465 struct catc *catc = netdev_priv(netdev);
467 dev_warn(&netdev->dev, "Transmit timed out.\n");
468 usb_unlink_urb(catc->tx_urb);
472 * Control messages.
475 static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
477 int retval = usb_control_msg(catc->usbdev,
478 dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
479 request, 0x40 | dir, value, index, buf, len, 1000);
480 return retval < 0 ? retval : 0;
483 static void catc_ctrl_run(struct catc *catc)
485 struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
486 struct usb_device *usbdev = catc->usbdev;
487 struct urb *urb = catc->ctrl_urb;
488 struct usb_ctrlrequest *dr = &catc->ctrl_dr;
489 int status;
491 dr->bRequest = q->request;
492 dr->bRequestType = 0x40 | q->dir;
493 dr->wValue = cpu_to_le16(q->value);
494 dr->wIndex = cpu_to_le16(q->index);
495 dr->wLength = cpu_to_le16(q->len);
497 urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
498 urb->transfer_buffer_length = q->len;
499 urb->transfer_buffer = catc->ctrl_buf;
500 urb->setup_packet = (void *) dr;
501 urb->dev = usbdev;
503 if (!q->dir && q->buf && q->len)
504 memcpy(catc->ctrl_buf, q->buf, q->len);
506 if ((status = usb_submit_urb(catc->ctrl_urb, GFP_ATOMIC)))
507 dev_err(&catc->usbdev->dev, "submit(ctrl_urb) status %d\n",
508 status);
511 static void catc_ctrl_done(struct urb *urb)
513 struct catc *catc = urb->context;
514 struct ctrl_queue *q;
515 unsigned long flags;
516 int status = urb->status;
518 if (status)
519 dev_dbg(&urb->dev->dev, "ctrl_done, status %d, len %d.\n",
520 status, urb->actual_length);
522 spin_lock_irqsave(&catc->ctrl_lock, flags);
524 q = catc->ctrl_queue + catc->ctrl_tail;
526 if (q->dir) {
527 if (q->buf && q->len)
528 memcpy(q->buf, catc->ctrl_buf, q->len);
529 else
530 q->buf = catc->ctrl_buf;
533 if (q->callback)
534 q->callback(catc, q);
536 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
538 if (catc->ctrl_head != catc->ctrl_tail)
539 catc_ctrl_run(catc);
540 else
541 clear_bit(CTRL_RUNNING, &catc->flags);
543 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
546 static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
547 u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
549 struct ctrl_queue *q;
550 int retval = 0;
551 unsigned long flags;
553 spin_lock_irqsave(&catc->ctrl_lock, flags);
555 q = catc->ctrl_queue + catc->ctrl_head;
557 q->dir = dir;
558 q->request = request;
559 q->value = value;
560 q->index = index;
561 q->buf = buf;
562 q->len = len;
563 q->callback = callback;
565 catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
567 if (catc->ctrl_head == catc->ctrl_tail) {
568 dev_err(&catc->usbdev->dev, "ctrl queue full\n");
569 catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
570 retval = -1;
573 if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
574 catc_ctrl_run(catc);
576 spin_unlock_irqrestore(&catc->ctrl_lock, flags);
578 return retval;
582 * Statistics.
585 static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
587 int index = q->index - EthStats;
588 u16 data, last;
590 catc->stats_buf[index] = *((char *)q->buf);
592 if (index & 1)
593 return;
595 data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
596 last = catc->stats_vals[index >> 1];
598 switch (index) {
599 case TxSingleColl:
600 case TxMultiColl:
601 catc->netdev->stats.collisions += data - last;
602 break;
603 case TxExcessColl:
604 catc->netdev->stats.tx_aborted_errors += data - last;
605 catc->netdev->stats.tx_errors += data - last;
606 break;
607 case RxFramErr:
608 catc->netdev->stats.rx_frame_errors += data - last;
609 catc->netdev->stats.rx_errors += data - last;
610 break;
613 catc->stats_vals[index >> 1] = data;
616 static void catc_stats_timer(unsigned long data)
618 struct catc *catc = (void *) data;
619 int i;
621 for (i = 0; i < 8; i++)
622 catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
624 mod_timer(&catc->timer, jiffies + STATS_UPDATE);
628 * Receive modes. Broadcast, Multicast, Promisc.
631 static void catc_multicast(unsigned char *addr, u8 *multicast)
633 u32 crc;
635 crc = ether_crc_le(6, addr);
636 multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
639 static void catc_set_multicast_list(struct net_device *netdev)
641 struct catc *catc = netdev_priv(netdev);
642 struct netdev_hw_addr *ha;
643 u8 broadcast[6];
644 u8 rx = RxEnable | RxPolarity | RxMultiCast;
646 memset(broadcast, 0xff, 6);
647 memset(catc->multicast, 0, 64);
649 catc_multicast(broadcast, catc->multicast);
650 catc_multicast(netdev->dev_addr, catc->multicast);
652 if (netdev->flags & IFF_PROMISC) {
653 memset(catc->multicast, 0xff, 64);
654 rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc;
657 if (netdev->flags & IFF_ALLMULTI) {
658 memset(catc->multicast, 0xff, 64);
659 } else {
660 netdev_for_each_mc_addr(ha, netdev) {
661 u32 crc = ether_crc_le(6, ha->addr);
662 if (!catc->is_f5u011) {
663 catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
664 } else {
665 catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7);
669 if (!catc->is_f5u011) {
670 catc_set_reg_async(catc, RxUnit, rx);
671 catc_write_mem_async(catc, 0xfa80, catc->multicast, 64);
672 } else {
673 f5u011_mchash_async(catc, catc->multicast);
674 if (catc->rxmode[0] != rx) {
675 catc->rxmode[0] = rx;
676 netdev_dbg(catc->netdev,
677 "Setting RX mode to %2.2X %2.2X\n",
678 catc->rxmode[0], catc->rxmode[1]);
679 f5u011_rxmode_async(catc, catc->rxmode);
684 static void catc_get_drvinfo(struct net_device *dev,
685 struct ethtool_drvinfo *info)
687 struct catc *catc = netdev_priv(dev);
688 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
689 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
690 usb_make_path (catc->usbdev, info->bus_info, sizeof info->bus_info);
693 static int catc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
695 struct catc *catc = netdev_priv(dev);
696 if (!catc->is_f5u011)
697 return -EOPNOTSUPP;
699 cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_TP;
700 cmd->advertising = ADVERTISED_10baseT_Half | ADVERTISED_TP;
701 ethtool_cmd_speed_set(cmd, SPEED_10);
702 cmd->duplex = DUPLEX_HALF;
703 cmd->port = PORT_TP;
704 cmd->phy_address = 0;
705 cmd->transceiver = XCVR_INTERNAL;
706 cmd->autoneg = AUTONEG_DISABLE;
707 cmd->maxtxpkt = 1;
708 cmd->maxrxpkt = 1;
709 return 0;
712 static const struct ethtool_ops ops = {
713 .get_drvinfo = catc_get_drvinfo,
714 .get_settings = catc_get_settings,
715 .get_link = ethtool_op_get_link
719 * Open, close.
722 static int catc_open(struct net_device *netdev)
724 struct catc *catc = netdev_priv(netdev);
725 int status;
727 catc->irq_urb->dev = catc->usbdev;
728 if ((status = usb_submit_urb(catc->irq_urb, GFP_KERNEL)) < 0) {
729 dev_err(&catc->usbdev->dev, "submit(irq_urb) status %d\n",
730 status);
731 return -1;
734 netif_start_queue(netdev);
736 if (!catc->is_f5u011)
737 mod_timer(&catc->timer, jiffies + STATS_UPDATE);
739 return 0;
742 static int catc_stop(struct net_device *netdev)
744 struct catc *catc = netdev_priv(netdev);
746 netif_stop_queue(netdev);
748 if (!catc->is_f5u011)
749 del_timer_sync(&catc->timer);
751 usb_kill_urb(catc->rx_urb);
752 usb_kill_urb(catc->tx_urb);
753 usb_kill_urb(catc->irq_urb);
754 usb_kill_urb(catc->ctrl_urb);
756 return 0;
759 static const struct net_device_ops catc_netdev_ops = {
760 .ndo_open = catc_open,
761 .ndo_stop = catc_stop,
762 .ndo_start_xmit = catc_start_xmit,
764 .ndo_tx_timeout = catc_tx_timeout,
765 .ndo_set_rx_mode = catc_set_multicast_list,
766 .ndo_change_mtu = eth_change_mtu,
767 .ndo_set_mac_address = eth_mac_addr,
768 .ndo_validate_addr = eth_validate_addr,
772 * USB probe, disconnect.
775 static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
777 struct device *dev = &intf->dev;
778 struct usb_device *usbdev = interface_to_usbdev(intf);
779 struct net_device *netdev;
780 struct catc *catc;
781 u8 broadcast[6];
782 int i, pktsz;
784 if (usb_set_interface(usbdev,
785 intf->altsetting->desc.bInterfaceNumber, 1)) {
786 dev_err(dev, "Can't set altsetting 1.\n");
787 return -EIO;
790 netdev = alloc_etherdev(sizeof(struct catc));
791 if (!netdev)
792 return -ENOMEM;
794 catc = netdev_priv(netdev);
796 netdev->netdev_ops = &catc_netdev_ops;
797 netdev->watchdog_timeo = TX_TIMEOUT;
798 SET_ETHTOOL_OPS(netdev, &ops);
800 catc->usbdev = usbdev;
801 catc->netdev = netdev;
803 spin_lock_init(&catc->tx_lock);
804 spin_lock_init(&catc->ctrl_lock);
806 init_timer(&catc->timer);
807 catc->timer.data = (long) catc;
808 catc->timer.function = catc_stats_timer;
810 catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
811 catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
812 catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
813 catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
814 if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
815 (!catc->rx_urb) || (!catc->irq_urb)) {
816 dev_err(&intf->dev, "No free urbs available.\n");
817 usb_free_urb(catc->ctrl_urb);
818 usb_free_urb(catc->tx_urb);
819 usb_free_urb(catc->rx_urb);
820 usb_free_urb(catc->irq_urb);
821 free_netdev(netdev);
822 return -ENOMEM;
825 /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
826 if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
827 le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
828 le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
829 dev_dbg(dev, "Testing for f5u011\n");
830 catc->is_f5u011 = 1;
831 atomic_set(&catc->recq_sz, 0);
832 pktsz = RX_PKT_SZ;
833 } else {
834 pktsz = RX_MAX_BURST * (PKT_SZ + 2);
837 usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
838 NULL, NULL, 0, catc_ctrl_done, catc);
840 usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
841 NULL, 0, catc_tx_done, catc);
843 usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
844 catc->rx_buf, pktsz, catc_rx_done, catc);
846 usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
847 catc->irq_buf, 2, catc_irq_done, catc, 1);
849 if (!catc->is_f5u011) {
850 dev_dbg(dev, "Checking memory size\n");
852 i = 0x12345678;
853 catc_write_mem(catc, 0x7a80, &i, 4);
854 i = 0x87654321;
855 catc_write_mem(catc, 0xfa80, &i, 4);
856 catc_read_mem(catc, 0x7a80, &i, 4);
858 switch (i) {
859 case 0x12345678:
860 catc_set_reg(catc, TxBufCount, 8);
861 catc_set_reg(catc, RxBufCount, 32);
862 dev_dbg(dev, "64k Memory\n");
863 break;
864 default:
865 dev_warn(&intf->dev,
866 "Couldn't detect memory size, assuming 32k\n");
867 case 0x87654321:
868 catc_set_reg(catc, TxBufCount, 4);
869 catc_set_reg(catc, RxBufCount, 16);
870 dev_dbg(dev, "32k Memory\n");
871 break;
874 dev_dbg(dev, "Getting MAC from SEEROM.\n");
876 catc_get_mac(catc, netdev->dev_addr);
878 dev_dbg(dev, "Setting MAC into registers.\n");
880 for (i = 0; i < 6; i++)
881 catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
883 dev_dbg(dev, "Filling the multicast list.\n");
885 memset(broadcast, 0xff, 6);
886 catc_multicast(broadcast, catc->multicast);
887 catc_multicast(netdev->dev_addr, catc->multicast);
888 catc_write_mem(catc, 0xfa80, catc->multicast, 64);
890 dev_dbg(dev, "Clearing error counters.\n");
892 for (i = 0; i < 8; i++)
893 catc_set_reg(catc, EthStats + i, 0);
894 catc->last_stats = jiffies;
896 dev_dbg(dev, "Enabling.\n");
898 catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
899 catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
900 catc_set_reg(catc, LEDCtrl, LEDLink);
901 catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
902 } else {
903 dev_dbg(dev, "Performing reset\n");
904 catc_reset(catc);
905 catc_get_mac(catc, netdev->dev_addr);
907 dev_dbg(dev, "Setting RX Mode\n");
908 catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
909 catc->rxmode[1] = 0;
910 f5u011_rxmode(catc, catc->rxmode);
912 dev_dbg(dev, "Init done.\n");
913 printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, %pM.\n",
914 netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
915 usbdev->bus->bus_name, usbdev->devpath, netdev->dev_addr);
916 usb_set_intfdata(intf, catc);
918 SET_NETDEV_DEV(netdev, &intf->dev);
919 if (register_netdev(netdev) != 0) {
920 usb_set_intfdata(intf, NULL);
921 usb_free_urb(catc->ctrl_urb);
922 usb_free_urb(catc->tx_urb);
923 usb_free_urb(catc->rx_urb);
924 usb_free_urb(catc->irq_urb);
925 free_netdev(netdev);
926 return -EIO;
928 return 0;
931 static void catc_disconnect(struct usb_interface *intf)
933 struct catc *catc = usb_get_intfdata(intf);
935 usb_set_intfdata(intf, NULL);
936 if (catc) {
937 unregister_netdev(catc->netdev);
938 usb_free_urb(catc->ctrl_urb);
939 usb_free_urb(catc->tx_urb);
940 usb_free_urb(catc->rx_urb);
941 usb_free_urb(catc->irq_urb);
942 free_netdev(catc->netdev);
947 * Module functions and tables.
950 static struct usb_device_id catc_id_table [] = {
951 { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */
952 { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */
953 { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */
957 MODULE_DEVICE_TABLE(usb, catc_id_table);
959 static struct usb_driver catc_driver = {
960 .name = driver_name,
961 .probe = catc_probe,
962 .disconnect = catc_disconnect,
963 .id_table = catc_id_table,
964 .disable_hub_initiated_lpm = 1,
967 module_usb_driver(catc_driver);