bonding:record primary when modify it via sysfs
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / ethernet / dlink / de600.c
CommitLineData
1da177e4
LT
1static const char version[] = "de600.c: $Revision: 1.41-2.5 $, Bjorn Ekwall (bj0rn@blox.se)\n";
2/*
3 * de600.c
4 *
5 * Linux driver for the D-Link DE-600 Ethernet pocket adapter.
6 *
7 * Portions (C) Copyright 1993, 1994 by Bjorn Ekwall
8 * The Author may be reached as bj0rn@blox.se
9 *
10 * Based on adapter information gathered from DE600.ASM by D-Link Inc.,
11 * as included on disk C in the v.2.11 of PC/TCP from FTP Software.
12 * For DE600.asm:
13 * Portions (C) Copyright 1990 D-Link, Inc.
14 * Copyright, 1988-1992, Russell Nelson, Crynwr Software
15 *
16 * Adapted to the sample network driver core for linux,
17 * written by: Donald Becker <becker@super.org>
18 * (Now at <becker@scyld.com>)
19 *
20 **************************************************************/
21/*
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2, or (at your option)
25 * any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *
36 **************************************************************/
37
38/* Add more time here if your adapter won't work OK: */
39#define DE600_SLOW_DOWN udelay(delay_time)
40
1da177e4
LT
41#include <linux/module.h>
42#include <linux/kernel.h>
43#include <linux/types.h>
44#include <linux/fcntl.h>
45#include <linux/string.h>
46#include <linux/interrupt.h>
47#include <linux/ioport.h>
48#include <linux/in.h>
1da177e4
LT
49#include <linux/errno.h>
50#include <linux/init.h>
51#include <linux/delay.h>
52#include <linux/inet.h>
53#include <linux/netdevice.h>
54#include <linux/etherdevice.h>
55#include <linux/skbuff.h>
56
57#include <asm/io.h>
58
59#include "de600.h"
60
eb939922 61static bool check_lost = true;
1da177e4
LT
62module_param(check_lost, bool, 0);
63MODULE_PARM_DESC(check_lost, "If set then check for unplugged de600");
64
65static unsigned int delay_time = 10;
66module_param(delay_time, int, 0);
67MODULE_PARM_DESC(delay_time, "DE-600 deley on I/O in microseconds");
68
69
70/*
71 * D-Link driver variables:
72 */
73
74static volatile int rx_page;
75
76#define TX_PAGES 2
77static volatile int tx_fifo[TX_PAGES];
78static volatile int tx_fifo_in;
79static volatile int tx_fifo_out;
80static volatile int free_tx_pages = TX_PAGES;
81static int was_down;
82static DEFINE_SPINLOCK(de600_lock);
83
84static inline u8 de600_read_status(struct net_device *dev)
85{
86 u8 status;
87
88 outb_p(STATUS, DATA_PORT);
89 status = inb(STATUS_PORT);
90 outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
91
92 return status;
93}
94
95static inline u8 de600_read_byte(unsigned char type, struct net_device *dev)
96{
97 /* dev used by macros */
98 u8 lo;
99 outb_p((type), DATA_PORT);
100 lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
101 outb_p((type) | HI_NIBBLE, DATA_PORT);
102 return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
103}
104
105/*
106 * Open/initialize the board. This is called (in the current kernel)
107 * after booting when 'ifconfig <dev->name> $IP_ADDR' is run (in rc.inet1).
108 *
109 * This routine should set everything up anew at each open, even
110 * registers that "should" only need to be set once at boot, so that
111 * there is a non-reboot way to recover if something goes wrong.
112 */
113
114static int de600_open(struct net_device *dev)
115{
116 unsigned long flags;
117 int ret = request_irq(DE600_IRQ, de600_interrupt, 0, dev->name, dev);
118 if (ret) {
119 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name, DE600_IRQ);
120 return ret;
121 }
122 spin_lock_irqsave(&de600_lock, flags);
123 ret = adapter_init(dev);
124 spin_unlock_irqrestore(&de600_lock, flags);
125 return ret;
126}
127
128/*
129 * The inverse routine to de600_open().
130 */
131
132static int de600_close(struct net_device *dev)
133{
134 select_nic();
135 rx_page = 0;
136 de600_put_command(RESET);
137 de600_put_command(STOP_RESET);
138 de600_put_command(0);
139 select_prn();
140 free_irq(DE600_IRQ, dev);
141 return 0;
142}
143
1da177e4
LT
144static inline void trigger_interrupt(struct net_device *dev)
145{
146 de600_put_command(FLIP_IRQ);
147 select_prn();
148 DE600_SLOW_DOWN;
149 select_nic();
150 de600_put_command(0);
151}
152
153/*
154 * Copy a buffer to the adapter transmit page memory.
155 * Start sending.
156 */
6aa20a22 157
1da177e4
LT
158static int de600_start_xmit(struct sk_buff *skb, struct net_device *dev)
159{
160 unsigned long flags;
161 int transmit_from;
162 int len;
163 int tickssofar;
164 u8 *buffer = skb->data;
165 int i;
166
167 if (free_tx_pages <= 0) { /* Do timeouts, to avoid hangs. */
1ae5dc34
ED
168 tickssofar = jiffies - dev_trans_start(dev);
169 if (tickssofar < HZ/20)
5b548140 170 return NETDEV_TX_BUSY;
1da177e4
LT
171 /* else */
172 printk(KERN_WARNING "%s: transmit timed out (%d), %s?\n", dev->name, tickssofar, "network cable problem");
173 /* Restart the adapter. */
174 spin_lock_irqsave(&de600_lock, flags);
175 if (adapter_init(dev)) {
176 spin_unlock_irqrestore(&de600_lock, flags);
5b548140 177 return NETDEV_TX_BUSY;
1da177e4
LT
178 }
179 spin_unlock_irqrestore(&de600_lock, flags);
180 }
181
182 /* Start real output */
64303258 183 pr_debug("de600_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages);
1da177e4
LT
184
185 if ((len = skb->len) < RUNT)
186 len = RUNT;
187
188 spin_lock_irqsave(&de600_lock, flags);
189 select_nic();
190 tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
191 tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES; /* Next free tx page */
192
193 if(check_lost)
194 {
195 /* This costs about 40 instructions per packet... */
196 de600_setup_address(NODE_ADDRESS, RW_ADDR);
197 de600_read_byte(READ_DATA, dev);
198 if (was_down || (de600_read_byte(READ_DATA, dev) != 0xde)) {
199 if (adapter_init(dev)) {
200 spin_unlock_irqrestore(&de600_lock, flags);
5b548140 201 return NETDEV_TX_BUSY;
1da177e4
LT
202 }
203 }
204 }
205
206 de600_setup_address(transmit_from, RW_ADDR);
207 for (i = 0; i < skb->len ; ++i, ++buffer)
208 de600_put_byte(*buffer);
209 for (; i < len; ++i)
210 de600_put_byte(0);
211
212 if (free_tx_pages-- == TX_PAGES) { /* No transmission going on */
213 dev->trans_start = jiffies;
214 netif_start_queue(dev); /* allow more packets into adapter */
215 /* Send page and generate a faked interrupt */
216 de600_setup_address(transmit_from, TX_ADDR);
217 de600_put_command(TX_ENABLE);
218 }
219 else {
220 if (free_tx_pages)
221 netif_start_queue(dev);
222 else
223 netif_stop_queue(dev);
224 select_prn();
225 }
226 spin_unlock_irqrestore(&de600_lock, flags);
227 dev_kfree_skb(skb);
6ed10654 228 return NETDEV_TX_OK;
1da177e4
LT
229}
230
231/*
232 * The typical workload of the driver:
233 * Handle the network interface interrupts.
234 */
235
7d12e780 236static irqreturn_t de600_interrupt(int irq, void *dev_id)
1da177e4
LT
237{
238 struct net_device *dev = dev_id;
239 u8 irq_status;
240 int retrig = 0;
241 int boguscount = 0;
242
1da177e4 243 spin_lock(&de600_lock);
6aa20a22 244
1da177e4
LT
245 select_nic();
246 irq_status = de600_read_status(dev);
247
248 do {
64303258 249 pr_debug("de600_interrupt (%02X)\n", irq_status);
1da177e4
LT
250
251 if (irq_status & RX_GOOD)
252 de600_rx_intr(dev);
253 else if (!(irq_status & RX_BUSY))
254 de600_put_command(RX_ENABLE);
255
256 /* Any transmission in progress? */
257 if (free_tx_pages < TX_PAGES)
258 retrig = de600_tx_intr(dev, irq_status);
259 else
260 retrig = 0;
261
262 irq_status = de600_read_status(dev);
263 } while ( (irq_status & RX_GOOD) || ((++boguscount < 100) && retrig) );
264 /*
265 * Yeah, it _looks_ like busy waiting, smells like busy waiting
266 * and I know it's not PC, but please, it will only occur once
267 * in a while and then only for a loop or so (< 1ms for sure!)
268 */
269
270 /* Enable adapter interrupts */
271 select_prn();
272 if (retrig)
273 trigger_interrupt(dev);
274 spin_unlock(&de600_lock);
275 return IRQ_HANDLED;
276}
277
278static int de600_tx_intr(struct net_device *dev, int irq_status)
279{
280 /*
281 * Returns 1 if tx still not done
282 */
283
284 /* Check if current transmission is done yet */
285 if (irq_status & TX_BUSY)
286 return 1; /* tx not done, try again */
287
288 /* else */
289 /* If last transmission OK then bump fifo index */
290 if (!(irq_status & TX_FAILED16)) {
291 tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
292 ++free_tx_pages;
09f75cd7 293 dev->stats.tx_packets++;
1da177e4
LT
294 netif_wake_queue(dev);
295 }
296
297 /* More to send, or resend last packet? */
298 if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
299 dev->trans_start = jiffies;
300 de600_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
301 de600_put_command(TX_ENABLE);
302 return 1;
303 }
304 /* else */
305
306 return 0;
307}
308
309/*
310 * We have a good packet, get it out of the adapter.
311 */
312static void de600_rx_intr(struct net_device *dev)
313{
314 struct sk_buff *skb;
315 int i;
316 int read_from;
317 int size;
318 unsigned char *buffer;
319
320 /* Get size of received packet */
321 size = de600_read_byte(RX_LEN, dev); /* low byte */
322 size += (de600_read_byte(RX_LEN, dev) << 8); /* high byte */
323 size -= 4; /* Ignore trailing 4 CRC-bytes */
324
325 /* Tell adapter where to store next incoming packet, enable receiver */
326 read_from = rx_page_adr();
327 next_rx_page();
328 de600_put_command(RX_ENABLE);
329
330 if ((size < 32) || (size > 1535)) {
331 printk(KERN_WARNING "%s: Bogus packet size %d.\n", dev->name, size);
332 if (size > 10000)
333 adapter_init(dev);
334 return;
335 }
336
21a4e469 337 skb = netdev_alloc_skb(dev, size + 2);
1da177e4
LT
338 if (skb == NULL) {
339 printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, size);
340 return;
341 }
342 /* else */
343
1da177e4
LT
344 skb_reserve(skb,2); /* Align */
345
346 /* 'skb->data' points to the start of sk_buff data area. */
347 buffer = skb_put(skb,size);
348
349 /* copy the packet into the buffer */
350 de600_setup_address(read_from, RW_ADDR);
351 for (i = size; i > 0; --i, ++buffer)
352 *buffer = de600_read_byte(READ_DATA, dev);
353
354 skb->protocol=eth_type_trans(skb,dev);
355
356 netif_rx(skb);
357
358 /* update stats */
09f75cd7
JG
359 dev->stats.rx_packets++; /* count all receives */
360 dev->stats.rx_bytes += size; /* count all received bytes */
1da177e4
LT
361
362 /*
363 * If any worth-while packets have been received, netif_rx()
364 * will work on them when we get to the tasklets.
365 */
366}
367
b8aa76a2
SH
368static const struct net_device_ops de600_netdev_ops = {
369 .ndo_open = de600_open,
370 .ndo_stop = de600_close,
371 .ndo_start_xmit = de600_start_xmit,
372 .ndo_change_mtu = eth_change_mtu,
373 .ndo_set_mac_address = eth_mac_addr,
374 .ndo_validate_addr = eth_validate_addr,
375};
376
377
1da177e4
LT
378static struct net_device * __init de600_probe(void)
379{
380 int i;
381 struct net_device *dev;
382 int err;
383
09f75cd7 384 dev = alloc_etherdev(0);
1da177e4
LT
385 if (!dev)
386 return ERR_PTR(-ENOMEM);
387
1da177e4
LT
388
389 if (!request_region(DE600_IO, 3, "de600")) {
390 printk(KERN_WARNING "DE600: port 0x%x busy\n", DE600_IO);
391 err = -EBUSY;
392 goto out;
393 }
394
395 printk(KERN_INFO "%s: D-Link DE-600 pocket adapter", dev->name);
396 /* Alpha testers must have the version number to report bugs. */
64303258 397 pr_debug("%s", version);
1da177e4
LT
398
399 /* probe for adapter */
400 err = -ENODEV;
401 rx_page = 0;
402 select_nic();
403 (void)de600_read_status(dev);
404 de600_put_command(RESET);
405 de600_put_command(STOP_RESET);
406 if (de600_read_status(dev) & 0xf0) {
407 printk(": not at I/O %#3x.\n", DATA_PORT);
408 goto out1;
409 }
410
411 /*
412 * Maybe we found one,
413 * have to check if it is a D-Link DE-600 adapter...
414 */
415
416 /* Get the adapter ethernet address from the ROM */
417 de600_setup_address(NODE_ADDRESS, RW_ADDR);
418 for (i = 0; i < ETH_ALEN; i++) {
419 dev->dev_addr[i] = de600_read_byte(READ_DATA, dev);
420 dev->broadcast[i] = 0xff;
421 }
422
423 /* Check magic code */
424 if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
425 /* OK, install real address */
426 dev->dev_addr[0] = 0x00;
427 dev->dev_addr[1] = 0x80;
428 dev->dev_addr[2] = 0xc8;
429 dev->dev_addr[3] &= 0x0f;
430 dev->dev_addr[3] |= 0x70;
431 } else {
432 printk(" not identified in the printer port\n");
433 goto out1;
434 }
435
e174961c 436 printk(", Ethernet Address: %pM\n", dev->dev_addr);
1da177e4 437
b8aa76a2 438 dev->netdev_ops = &de600_netdev_ops;
1da177e4
LT
439
440 dev->flags&=~IFF_MULTICAST;
441
442 select_prn();
443
444 err = register_netdev(dev);
445 if (err)
446 goto out1;
447
448 return dev;
449
450out1:
451 release_region(DE600_IO, 3);
452out:
453 free_netdev(dev);
454 return ERR_PTR(err);
455}
456
457static int adapter_init(struct net_device *dev)
458{
459 int i;
460
461 select_nic();
462 rx_page = 0; /* used by RESET */
463 de600_put_command(RESET);
464 de600_put_command(STOP_RESET);
465
466 /* Check if it is still there... */
467 /* Get the some bytes of the adapter ethernet address from the ROM */
468 de600_setup_address(NODE_ADDRESS, RW_ADDR);
469 de600_read_byte(READ_DATA, dev);
470 if ((de600_read_byte(READ_DATA, dev) != 0xde) ||
471 (de600_read_byte(READ_DATA, dev) != 0x15)) {
472 /* was: if (de600_read_status(dev) & 0xf0) { */
473 printk("Something has happened to the DE-600! Please check it and do a new ifconfig!\n");
474 /* Goodbye, cruel world... */
475 dev->flags &= ~IFF_UP;
476 de600_close(dev);
477 was_down = 1;
478 netif_stop_queue(dev); /* Transmit busy... */
479 return 1; /* failed */
480 }
481
482 if (was_down) {
483 printk(KERN_INFO "%s: Thanks, I feel much better now!\n", dev->name);
484 was_down = 0;
485 }
486
487 tx_fifo_in = 0;
488 tx_fifo_out = 0;
489 free_tx_pages = TX_PAGES;
490
491
492 /* set the ether address. */
493 de600_setup_address(NODE_ADDRESS, RW_ADDR);
494 for (i = 0; i < ETH_ALEN; i++)
495 de600_put_byte(dev->dev_addr[i]);
496
497 /* where to start saving incoming packets */
498 rx_page = RX_BP | RX_BASE_PAGE;
499 de600_setup_address(MEM_4K, RW_ADDR);
500 /* Enable receiver */
501 de600_put_command(RX_ENABLE);
502 select_prn();
503
504 netif_start_queue(dev);
505
506 return 0; /* OK */
507}
508
509static struct net_device *de600_dev;
510
511static int __init de600_init(void)
512{
513 de600_dev = de600_probe();
514 if (IS_ERR(de600_dev))
515 return PTR_ERR(de600_dev);
516 return 0;
517}
518
519static void __exit de600_exit(void)
520{
521 unregister_netdev(de600_dev);
522 release_region(DE600_IO, 3);
523 free_netdev(de600_dev);
524}
525
526module_init(de600_init);
527module_exit(de600_exit);
528
529MODULE_LICENSE("GPL");