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