Merge branch 'upstream' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / net / de600.c
1 static 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
41 /*
42 * If you still have trouble reading/writing to the adapter,
43 * modify the following "#define": (see <asm/io.h> for more info)
44 #define REALLY_SLOW_IO
45 */
46
47 /* use 0 for production, 1 for verification, >2 for debug */
48 #ifdef DE600_DEBUG
49 #define PRINTK(x) if (de600_debug >= 2) printk x
50 #else
51 #define DE600_DEBUG 0
52 #define PRINTK(x) /**/
53 #endif
54
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/types.h>
58 #include <linux/fcntl.h>
59 #include <linux/string.h>
60 #include <linux/interrupt.h>
61 #include <linux/ioport.h>
62 #include <linux/in.h>
63 #include <asm/system.h>
64 #include <linux/errno.h>
65 #include <linux/init.h>
66 #include <linux/delay.h>
67 #include <linux/inet.h>
68 #include <linux/netdevice.h>
69 #include <linux/etherdevice.h>
70 #include <linux/skbuff.h>
71
72 #include <asm/io.h>
73
74 #include "de600.h"
75
76 static unsigned int de600_debug = DE600_DEBUG;
77 module_param(de600_debug, int, 0);
78 MODULE_PARM_DESC(de600_debug, "DE-600 debug level (0-2)");
79
80 static unsigned int check_lost = 1;
81 module_param(check_lost, bool, 0);
82 MODULE_PARM_DESC(check_lost, "If set then check for unplugged de600");
83
84 static unsigned int delay_time = 10;
85 module_param(delay_time, int, 0);
86 MODULE_PARM_DESC(delay_time, "DE-600 deley on I/O in microseconds");
87
88
89 /*
90 * D-Link driver variables:
91 */
92
93 static volatile int rx_page;
94
95 #define TX_PAGES 2
96 static volatile int tx_fifo[TX_PAGES];
97 static volatile int tx_fifo_in;
98 static volatile int tx_fifo_out;
99 static volatile int free_tx_pages = TX_PAGES;
100 static int was_down;
101 static DEFINE_SPINLOCK(de600_lock);
102
103 static inline u8 de600_read_status(struct net_device *dev)
104 {
105 u8 status;
106
107 outb_p(STATUS, DATA_PORT);
108 status = inb(STATUS_PORT);
109 outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
110
111 return status;
112 }
113
114 static inline u8 de600_read_byte(unsigned char type, struct net_device *dev)
115 {
116 /* dev used by macros */
117 u8 lo;
118 outb_p((type), DATA_PORT);
119 lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
120 outb_p((type) | HI_NIBBLE, DATA_PORT);
121 return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
122 }
123
124 /*
125 * Open/initialize the board. This is called (in the current kernel)
126 * after booting when 'ifconfig <dev->name> $IP_ADDR' is run (in rc.inet1).
127 *
128 * This routine should set everything up anew at each open, even
129 * registers that "should" only need to be set once at boot, so that
130 * there is a non-reboot way to recover if something goes wrong.
131 */
132
133 static int de600_open(struct net_device *dev)
134 {
135 unsigned long flags;
136 int ret = request_irq(DE600_IRQ, de600_interrupt, 0, dev->name, dev);
137 if (ret) {
138 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name, DE600_IRQ);
139 return ret;
140 }
141 spin_lock_irqsave(&de600_lock, flags);
142 ret = adapter_init(dev);
143 spin_unlock_irqrestore(&de600_lock, flags);
144 return ret;
145 }
146
147 /*
148 * The inverse routine to de600_open().
149 */
150
151 static int de600_close(struct net_device *dev)
152 {
153 select_nic();
154 rx_page = 0;
155 de600_put_command(RESET);
156 de600_put_command(STOP_RESET);
157 de600_put_command(0);
158 select_prn();
159 free_irq(DE600_IRQ, dev);
160 return 0;
161 }
162
163 static struct net_device_stats *get_stats(struct net_device *dev)
164 {
165 return (struct net_device_stats *)(dev->priv);
166 }
167
168 static inline void trigger_interrupt(struct net_device *dev)
169 {
170 de600_put_command(FLIP_IRQ);
171 select_prn();
172 DE600_SLOW_DOWN;
173 select_nic();
174 de600_put_command(0);
175 }
176
177 /*
178 * Copy a buffer to the adapter transmit page memory.
179 * Start sending.
180 */
181
182 static int de600_start_xmit(struct sk_buff *skb, struct net_device *dev)
183 {
184 unsigned long flags;
185 int transmit_from;
186 int len;
187 int tickssofar;
188 u8 *buffer = skb->data;
189 int i;
190
191 if (free_tx_pages <= 0) { /* Do timeouts, to avoid hangs. */
192 tickssofar = jiffies - dev->trans_start;
193 if (tickssofar < 5)
194 return 1;
195 /* else */
196 printk(KERN_WARNING "%s: transmit timed out (%d), %s?\n", dev->name, tickssofar, "network cable problem");
197 /* Restart the adapter. */
198 spin_lock_irqsave(&de600_lock, flags);
199 if (adapter_init(dev)) {
200 spin_unlock_irqrestore(&de600_lock, flags);
201 return 1;
202 }
203 spin_unlock_irqrestore(&de600_lock, flags);
204 }
205
206 /* Start real output */
207 PRINTK(("de600_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages));
208
209 if ((len = skb->len) < RUNT)
210 len = RUNT;
211
212 spin_lock_irqsave(&de600_lock, flags);
213 select_nic();
214 tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
215 tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES; /* Next free tx page */
216
217 if(check_lost)
218 {
219 /* This costs about 40 instructions per packet... */
220 de600_setup_address(NODE_ADDRESS, RW_ADDR);
221 de600_read_byte(READ_DATA, dev);
222 if (was_down || (de600_read_byte(READ_DATA, dev) != 0xde)) {
223 if (adapter_init(dev)) {
224 spin_unlock_irqrestore(&de600_lock, flags);
225 return 1;
226 }
227 }
228 }
229
230 de600_setup_address(transmit_from, RW_ADDR);
231 for (i = 0; i < skb->len ; ++i, ++buffer)
232 de600_put_byte(*buffer);
233 for (; i < len; ++i)
234 de600_put_byte(0);
235
236 if (free_tx_pages-- == TX_PAGES) { /* No transmission going on */
237 dev->trans_start = jiffies;
238 netif_start_queue(dev); /* allow more packets into adapter */
239 /* Send page and generate a faked interrupt */
240 de600_setup_address(transmit_from, TX_ADDR);
241 de600_put_command(TX_ENABLE);
242 }
243 else {
244 if (free_tx_pages)
245 netif_start_queue(dev);
246 else
247 netif_stop_queue(dev);
248 select_prn();
249 }
250 spin_unlock_irqrestore(&de600_lock, flags);
251 dev_kfree_skb(skb);
252 return 0;
253 }
254
255 /*
256 * The typical workload of the driver:
257 * Handle the network interface interrupts.
258 */
259
260 static irqreturn_t de600_interrupt(int irq, void *dev_id)
261 {
262 struct net_device *dev = dev_id;
263 u8 irq_status;
264 int retrig = 0;
265 int boguscount = 0;
266
267 spin_lock(&de600_lock);
268
269 select_nic();
270 irq_status = de600_read_status(dev);
271
272 do {
273 PRINTK(("de600_interrupt (%02X)\n", irq_status));
274
275 if (irq_status & RX_GOOD)
276 de600_rx_intr(dev);
277 else if (!(irq_status & RX_BUSY))
278 de600_put_command(RX_ENABLE);
279
280 /* Any transmission in progress? */
281 if (free_tx_pages < TX_PAGES)
282 retrig = de600_tx_intr(dev, irq_status);
283 else
284 retrig = 0;
285
286 irq_status = de600_read_status(dev);
287 } while ( (irq_status & RX_GOOD) || ((++boguscount < 100) && retrig) );
288 /*
289 * Yeah, it _looks_ like busy waiting, smells like busy waiting
290 * and I know it's not PC, but please, it will only occur once
291 * in a while and then only for a loop or so (< 1ms for sure!)
292 */
293
294 /* Enable adapter interrupts */
295 select_prn();
296 if (retrig)
297 trigger_interrupt(dev);
298 spin_unlock(&de600_lock);
299 return IRQ_HANDLED;
300 }
301
302 static int de600_tx_intr(struct net_device *dev, int irq_status)
303 {
304 /*
305 * Returns 1 if tx still not done
306 */
307
308 /* Check if current transmission is done yet */
309 if (irq_status & TX_BUSY)
310 return 1; /* tx not done, try again */
311
312 /* else */
313 /* If last transmission OK then bump fifo index */
314 if (!(irq_status & TX_FAILED16)) {
315 tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
316 ++free_tx_pages;
317 ((struct net_device_stats *)(dev->priv))->tx_packets++;
318 netif_wake_queue(dev);
319 }
320
321 /* More to send, or resend last packet? */
322 if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
323 dev->trans_start = jiffies;
324 de600_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
325 de600_put_command(TX_ENABLE);
326 return 1;
327 }
328 /* else */
329
330 return 0;
331 }
332
333 /*
334 * We have a good packet, get it out of the adapter.
335 */
336 static void de600_rx_intr(struct net_device *dev)
337 {
338 struct sk_buff *skb;
339 int i;
340 int read_from;
341 int size;
342 unsigned char *buffer;
343
344 /* Get size of received packet */
345 size = de600_read_byte(RX_LEN, dev); /* low byte */
346 size += (de600_read_byte(RX_LEN, dev) << 8); /* high byte */
347 size -= 4; /* Ignore trailing 4 CRC-bytes */
348
349 /* Tell adapter where to store next incoming packet, enable receiver */
350 read_from = rx_page_adr();
351 next_rx_page();
352 de600_put_command(RX_ENABLE);
353
354 if ((size < 32) || (size > 1535)) {
355 printk(KERN_WARNING "%s: Bogus packet size %d.\n", dev->name, size);
356 if (size > 10000)
357 adapter_init(dev);
358 return;
359 }
360
361 skb = dev_alloc_skb(size+2);
362 if (skb == NULL) {
363 printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, size);
364 return;
365 }
366 /* else */
367
368 skb->dev = dev;
369 skb_reserve(skb,2); /* Align */
370
371 /* 'skb->data' points to the start of sk_buff data area. */
372 buffer = skb_put(skb,size);
373
374 /* copy the packet into the buffer */
375 de600_setup_address(read_from, RW_ADDR);
376 for (i = size; i > 0; --i, ++buffer)
377 *buffer = de600_read_byte(READ_DATA, dev);
378
379 skb->protocol=eth_type_trans(skb,dev);
380
381 netif_rx(skb);
382
383 /* update stats */
384 dev->last_rx = jiffies;
385 ((struct net_device_stats *)(dev->priv))->rx_packets++; /* count all receives */
386 ((struct net_device_stats *)(dev->priv))->rx_bytes += size; /* count all received bytes */
387
388 /*
389 * If any worth-while packets have been received, netif_rx()
390 * will work on them when we get to the tasklets.
391 */
392 }
393
394 static struct net_device * __init de600_probe(void)
395 {
396 int i;
397 struct net_device *dev;
398 int err;
399
400 dev = alloc_etherdev(sizeof(struct net_device_stats));
401 if (!dev)
402 return ERR_PTR(-ENOMEM);
403
404 SET_MODULE_OWNER(dev);
405
406 if (!request_region(DE600_IO, 3, "de600")) {
407 printk(KERN_WARNING "DE600: port 0x%x busy\n", DE600_IO);
408 err = -EBUSY;
409 goto out;
410 }
411
412 printk(KERN_INFO "%s: D-Link DE-600 pocket adapter", dev->name);
413 /* Alpha testers must have the version number to report bugs. */
414 if (de600_debug > 1)
415 printk(version);
416
417 /* probe for adapter */
418 err = -ENODEV;
419 rx_page = 0;
420 select_nic();
421 (void)de600_read_status(dev);
422 de600_put_command(RESET);
423 de600_put_command(STOP_RESET);
424 if (de600_read_status(dev) & 0xf0) {
425 printk(": not at I/O %#3x.\n", DATA_PORT);
426 goto out1;
427 }
428
429 /*
430 * Maybe we found one,
431 * have to check if it is a D-Link DE-600 adapter...
432 */
433
434 /* Get the adapter ethernet address from the ROM */
435 de600_setup_address(NODE_ADDRESS, RW_ADDR);
436 for (i = 0; i < ETH_ALEN; i++) {
437 dev->dev_addr[i] = de600_read_byte(READ_DATA, dev);
438 dev->broadcast[i] = 0xff;
439 }
440
441 /* Check magic code */
442 if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
443 /* OK, install real address */
444 dev->dev_addr[0] = 0x00;
445 dev->dev_addr[1] = 0x80;
446 dev->dev_addr[2] = 0xc8;
447 dev->dev_addr[3] &= 0x0f;
448 dev->dev_addr[3] |= 0x70;
449 } else {
450 printk(" not identified in the printer port\n");
451 goto out1;
452 }
453
454 printk(", Ethernet Address: %02X", dev->dev_addr[0]);
455 for (i = 1; i < ETH_ALEN; i++)
456 printk(":%02X",dev->dev_addr[i]);
457 printk("\n");
458
459 dev->get_stats = get_stats;
460
461 dev->open = de600_open;
462 dev->stop = de600_close;
463 dev->hard_start_xmit = &de600_start_xmit;
464
465 dev->flags&=~IFF_MULTICAST;
466
467 select_prn();
468
469 err = register_netdev(dev);
470 if (err)
471 goto out1;
472
473 return dev;
474
475 out1:
476 release_region(DE600_IO, 3);
477 out:
478 free_netdev(dev);
479 return ERR_PTR(err);
480 }
481
482 static int adapter_init(struct net_device *dev)
483 {
484 int i;
485
486 select_nic();
487 rx_page = 0; /* used by RESET */
488 de600_put_command(RESET);
489 de600_put_command(STOP_RESET);
490
491 /* Check if it is still there... */
492 /* Get the some bytes of the adapter ethernet address from the ROM */
493 de600_setup_address(NODE_ADDRESS, RW_ADDR);
494 de600_read_byte(READ_DATA, dev);
495 if ((de600_read_byte(READ_DATA, dev) != 0xde) ||
496 (de600_read_byte(READ_DATA, dev) != 0x15)) {
497 /* was: if (de600_read_status(dev) & 0xf0) { */
498 printk("Something has happened to the DE-600! Please check it and do a new ifconfig!\n");
499 /* Goodbye, cruel world... */
500 dev->flags &= ~IFF_UP;
501 de600_close(dev);
502 was_down = 1;
503 netif_stop_queue(dev); /* Transmit busy... */
504 return 1; /* failed */
505 }
506
507 if (was_down) {
508 printk(KERN_INFO "%s: Thanks, I feel much better now!\n", dev->name);
509 was_down = 0;
510 }
511
512 tx_fifo_in = 0;
513 tx_fifo_out = 0;
514 free_tx_pages = TX_PAGES;
515
516
517 /* set the ether address. */
518 de600_setup_address(NODE_ADDRESS, RW_ADDR);
519 for (i = 0; i < ETH_ALEN; i++)
520 de600_put_byte(dev->dev_addr[i]);
521
522 /* where to start saving incoming packets */
523 rx_page = RX_BP | RX_BASE_PAGE;
524 de600_setup_address(MEM_4K, RW_ADDR);
525 /* Enable receiver */
526 de600_put_command(RX_ENABLE);
527 select_prn();
528
529 netif_start_queue(dev);
530
531 return 0; /* OK */
532 }
533
534 static struct net_device *de600_dev;
535
536 static int __init de600_init(void)
537 {
538 de600_dev = de600_probe();
539 if (IS_ERR(de600_dev))
540 return PTR_ERR(de600_dev);
541 return 0;
542 }
543
544 static void __exit de600_exit(void)
545 {
546 unregister_netdev(de600_dev);
547 release_region(DE600_IO, 3);
548 free_netdev(de600_dev);
549 }
550
551 module_init(de600_init);
552 module_exit(de600_exit);
553
554 MODULE_LICENSE("GPL");
This page took 0.044658 seconds and 5 git commands to generate.