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