The LDT fixes in particular fix some potentially random strange behaviour.
[davej-history.git] / drivers / net / sb1000.c
blob95a7c864e51f15df4eb6db4a728ac80a331ece2f
1 /* sb1000.c: A General Instruments SB1000 driver for linux. */
2 /*
3 Written 1998 by Franco Venturi.
5 Copyright 1998 by Franco Venturi.
6 Copyright 1994,1995 by Donald Becker.
7 Copyright 1993 United States Government as represented by the
8 Director, National Security Agency.
10 This driver is for the General Instruments SB1000 (internal SURFboard)
12 The author may be reached as fventuri@mediaone.net
14 This program is free software; you can redistribute it
15 and/or modify it under the terms of the GNU General
16 Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at
18 your option) any later version.
20 Changes:
22 981115 Steven Hirsch <shirsch@adelphia.net>
24 Linus changed the timer interface. Should work on all recent
25 development kernels.
27 980608 Steven Hirsch <shirsch@adelphia.net>
29 Small changes to make it work with 2.1.x kernels. Hopefully,
30 nothing major will change before official release of Linux 2.2.
32 Merged with 2.2 - Alan Cox
35 static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)\n";
37 #include <linux/module.h>
39 #include <linux/version.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/interrupt.h>
44 #include <linux/ptrace.h>
45 #include <linux/errno.h>
46 #include <linux/in.h>
47 #include <linux/malloc.h>
48 #include <linux/ioport.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_arp.h>
51 #include <linux/skbuff.h>
52 #include <linux/delay.h> /* for udelay() */
53 #include <asm/processor.h>
55 #include <asm/bitops.h>
56 #include <asm/io.h>
57 #include <asm/uaccess.h>
58 #include <linux/etherdevice.h>
59 #include <linux/isapnp.h>
61 /* for SIOGCM/SIOSCM stuff */
63 #include <linux/if_cablemodem.h>
65 #ifdef SB1000_DEBUG
66 int sb1000_debug = SB1000_DEBUG;
67 #else
68 int sb1000_debug = 1;
69 #endif
71 static const int SB1000_IO_EXTENT = 8;
72 /* SB1000 Maximum Receive Unit */
73 static const int SB1000_MRU = 1500; /* octects */
75 #define NPIDS 4
76 struct sb1000_private {
77 struct sk_buff *rx_skb[NPIDS];
78 short rx_dlen[NPIDS];
79 unsigned int rx_bytes;
80 unsigned int rx_frames;
81 short rx_error_count;
82 short rx_error_dpc_count;
83 unsigned char rx_session_id[NPIDS];
84 unsigned char rx_frame_id[NPIDS];
85 unsigned char rx_pkt_type[NPIDS];
86 struct net_device_stats stats;
89 /* prototypes for Linux interface */
90 extern int sb1000_probe(struct net_device *dev);
91 static int sb1000_open(struct net_device *dev);
92 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
93 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
94 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
95 static struct net_device_stats *sb1000_stats(struct net_device *dev);
96 static int sb1000_close(struct net_device *dev);
99 /* SB1000 hardware routines to be used during open/configuration phases */
100 static inline void nicedelay(unsigned long usecs);
101 static inline int card_wait_for_busy_clear(const int ioaddr[],
102 const char* name);
103 static inline int card_wait_for_ready(const int ioaddr[], const char* name,
104 unsigned char in[]);
105 static inline int card_send_command(const int ioaddr[], const char* name,
106 const unsigned char out[], unsigned char in[]);
108 /* SB1000 hardware routines to be used during frame rx interrupt */
109 static inline int sb1000_wait_for_ready(const int ioaddr[], const char* name);
110 static inline int sb1000_wait_for_ready_clear(const int ioaddr[],
111 const char* name);
112 static inline void sb1000_send_command(const int ioaddr[], const char* name,
113 const unsigned char out[]);
114 static inline void sb1000_read_status(const int ioaddr[], unsigned char in[]);
115 static inline void sb1000_issue_read_command(const int ioaddr[],
116 const char* name);
118 /* SB1000 commands for open/configuration */
119 static inline int sb1000_reset(const int ioaddr[], const char* name);
120 static inline int sb1000_check_CRC(const int ioaddr[], const char* name);
121 static inline int sb1000_start_get_set_command(const int ioaddr[],
122 const char* name);
123 static inline int sb1000_end_get_set_command(const int ioaddr[],
124 const char* name);
125 static inline int sb1000_activate(const int ioaddr[], const char* name);
126 static inline int sb1000_get_firmware_version(const int ioaddr[],
127 const char* name, unsigned char version[], int do_end);
128 static inline int sb1000_get_frequency(const int ioaddr[], const char* name,
129 int* frequency);
130 static inline int sb1000_set_frequency(const int ioaddr[], const char* name,
131 int frequency);
132 static inline int sb1000_get_PIDs(const int ioaddr[], const char* name,
133 short PID[]);
134 static inline int sb1000_set_PIDs(const int ioaddr[], const char* name,
135 const short PID[]);
137 /* SB1000 commands for frame rx interrupt */
138 static inline int sb1000_rx(struct net_device *dev);
139 static inline void sb1000_error_dpc(struct net_device *dev);
141 /* probe for SB1000 using Plug-n-Play mechanism */
143 sb1000_probe(struct net_device *dev)
146 unsigned short ioaddr[2], irq;
147 struct pci_dev *idev=NULL;
148 unsigned int serial_number;
150 while(1)
153 * Find the card
156 idev=isapnp_find_dev(NULL, ISAPNP_VENDOR('G','I','C'),
157 ISAPNP_FUNCTION(0x1000), idev);
160 * No card
163 if(idev==NULL)
164 return -ENODEV;
167 * Bring it online
170 idev->prepare(idev);
171 idev->activate(idev);
174 * Ports free ?
177 if(!idev->resource[0].start || check_region(idev->resource[0].start, 16))
178 continue;
179 if(!idev->resource[1].start || check_region(idev->resource[1].start, 16))
180 continue;
182 serial_number = idev->bus->serial;
184 ioaddr[0]=idev->resource[0].start;
185 ioaddr[1]=idev->resource[1].start;
187 irq = idev->irq;
189 /* check I/O base and IRQ */
190 if (dev->base_addr != 0 && dev->base_addr != ioaddr[0])
191 continue;
192 if (dev->rmem_end != 0 && dev->rmem_end != ioaddr[1])
193 continue;
194 if (dev->irq != 0 && dev->irq != irq)
195 continue;
198 * Ok set it up.
202 dev->base_addr = ioaddr[0];
203 /* rmem_end holds the second I/O address - fv */
204 dev->rmem_end = ioaddr[1];
205 dev->irq = irq;
207 if (sb1000_debug > 0)
208 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
209 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
210 dev->rmem_end, serial_number, dev->irq);
212 dev = init_etherdev(dev, 0);
213 if (!dev)
214 return -ENOMEM;
215 SET_MODULE_OWNER(dev);
217 /* Make up a SB1000-specific-data structure. */
218 dev->priv = kmalloc(sizeof(struct sb1000_private), GFP_KERNEL);
219 if (dev->priv == NULL)
220 return -ENOMEM;
221 memset(dev->priv, 0, sizeof(struct sb1000_private));
223 if (sb1000_debug > 0)
224 printk(KERN_NOTICE "%s", version);
226 /* The SB1000-specific entries in the device structure. */
227 dev->open = sb1000_open;
228 dev->do_ioctl = sb1000_dev_ioctl;
229 dev->hard_start_xmit = sb1000_start_xmit;
230 dev->stop = sb1000_close;
231 dev->get_stats = sb1000_stats;
233 /* Fill in the generic fields of the device structure. */
234 dev->change_mtu = NULL;
235 dev->hard_header = NULL;
236 dev->rebuild_header = NULL;
237 dev->set_mac_address = NULL;
238 dev->header_cache_update= NULL;
240 dev->type = ARPHRD_ETHER;
241 dev->hard_header_len = 0;
242 dev->mtu = 1500;
243 dev->addr_len = ETH_ALEN;
244 /* hardware address is 0:0:serial_number */
245 dev->dev_addr[0] = 0;
246 dev->dev_addr[1] = 0;
247 dev->dev_addr[2] = serial_number >> 24 & 0xff;
248 dev->dev_addr[3] = serial_number >> 16 & 0xff;
249 dev->dev_addr[4] = serial_number >> 8 & 0xff;
250 dev->dev_addr[5] = serial_number >> 0 & 0xff;
251 dev->tx_queue_len = 0;
253 /* New-style flags. */
254 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
256 /* Lock resources */
258 request_region(ioaddr[0], 16, dev->name);
259 request_region(ioaddr[1], 16, dev->name);
261 return 0;
267 * SB1000 hardware routines to be used during open/configuration phases
270 const int TimeOutJiffies = (int)(8.75 * HZ);
272 static inline void nicedelay(unsigned long usecs)
274 current->state = TASK_INTERRUPTIBLE;
275 schedule_timeout(HZ);
276 return;
279 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
280 static inline int
281 card_wait_for_busy_clear(const int ioaddr[], const char* name)
283 unsigned char a;
284 unsigned long timeout;
286 a = inb(ioaddr[0] + 7);
287 timeout = jiffies + TimeOutJiffies;
288 while (a & 0x80 || a & 0x40) {
289 /* a little sleep */
290 current->state = TASK_INTERRUPTIBLE;
291 schedule_timeout(0);
292 a = inb(ioaddr[0] + 7);
293 if (jiffies >= timeout) {
294 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
295 name);
296 return -ETIME;
300 return 0;
303 /* Card Wait For Ready (cannot be used during an interrupt) */
304 static inline int
305 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
307 unsigned char a;
308 unsigned long timeout;
310 a = inb(ioaddr[1] + 6);
311 timeout = jiffies + TimeOutJiffies;
312 while (a & 0x80 || !(a & 0x40)) {
313 /* a little sleep */
314 current->state = TASK_INTERRUPTIBLE;
315 schedule_timeout(0);
316 a = inb(ioaddr[1] + 6);
317 if (jiffies >= timeout) {
318 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
319 name);
320 return -ETIME;
324 in[1] = inb(ioaddr[0] + 1);
325 in[2] = inb(ioaddr[0] + 2);
326 in[3] = inb(ioaddr[0] + 3);
327 in[4] = inb(ioaddr[0] + 4);
328 in[0] = inb(ioaddr[0] + 5);
329 in[6] = inb(ioaddr[0] + 6);
330 in[5] = inb(ioaddr[1] + 6);
331 return 0;
334 /* Card Send Command (cannot be used during an interrupt) */
335 static inline int
336 card_send_command(const int ioaddr[], const char* name,
337 const unsigned char out[], unsigned char in[])
339 int status, x;
341 if ((status = card_wait_for_busy_clear(ioaddr, name)))
342 return status;
343 outb(0xa0, ioaddr[0] + 6);
344 outb(out[2], ioaddr[0] + 1);
345 outb(out[3], ioaddr[0] + 2);
346 outb(out[4], ioaddr[0] + 3);
347 outb(out[5], ioaddr[0] + 4);
348 outb(out[1], ioaddr[0] + 5);
349 outb(0xa0, ioaddr[0] + 6);
350 outb(out[0], ioaddr[0] + 7);
351 if (out[0] != 0x20 && out[0] != 0x30) {
352 if ((status = card_wait_for_ready(ioaddr, name, in)))
353 return status;
354 inb(ioaddr[0] + 7);
355 if (sb1000_debug > 3)
356 printk(KERN_DEBUG "%s: card_send_command "
357 "out: %02x%02x%02x%02x%02x%02x "
358 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
359 out[0], out[1], out[2], out[3], out[4], out[5],
360 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
361 } else {
362 if (sb1000_debug > 3)
363 printk(KERN_DEBUG "%s: card_send_command "
364 "out: %02x%02x%02x%02x%02x%02x\n", name,
365 out[0], out[1], out[2], out[3], out[4], out[5]);
368 if (out[1] == 0x1b) {
369 x = (out[2] == 0x02);
370 } else {
371 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
372 return -EIO;
374 return 0;
379 * SB1000 hardware routines to be used during frame rx interrupt
381 const int Sb1000TimeOutJiffies = 7 * HZ;
383 /* Card Wait For Ready (to be used during frame rx) */
384 static inline int
385 sb1000_wait_for_ready(const int ioaddr[], const char* name)
387 unsigned long timeout;
389 timeout = jiffies + Sb1000TimeOutJiffies;
390 while (inb(ioaddr[1] + 6) & 0x80) {
391 if (jiffies >= timeout) {
392 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
393 name);
394 return -ETIME;
397 timeout = jiffies + Sb1000TimeOutJiffies;
398 while (!(inb(ioaddr[1] + 6) & 0x40)) {
399 if (jiffies >= timeout) {
400 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
401 name);
402 return -ETIME;
405 inb(ioaddr[0] + 7);
406 return 0;
409 /* Card Wait For Ready Clear (to be used during frame rx) */
410 static inline int
411 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
413 unsigned long timeout;
415 timeout = jiffies + Sb1000TimeOutJiffies;
416 while (inb(ioaddr[1] + 6) & 0x80) {
417 if (jiffies >= timeout) {
418 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
419 name);
420 return -ETIME;
423 timeout = jiffies + Sb1000TimeOutJiffies;
424 while (inb(ioaddr[1] + 6) & 0x40) {
425 if (jiffies >= timeout) {
426 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
427 name);
428 return -ETIME;
431 return 0;
434 /* Card Send Command (to be used during frame rx) */
435 static inline void
436 sb1000_send_command(const int ioaddr[], const char* name,
437 const unsigned char out[])
439 outb(out[2], ioaddr[0] + 1);
440 outb(out[3], ioaddr[0] + 2);
441 outb(out[4], ioaddr[0] + 3);
442 outb(out[5], ioaddr[0] + 4);
443 outb(out[1], ioaddr[0] + 5);
444 outb(out[0], ioaddr[0] + 7);
445 if (sb1000_debug > 3)
446 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
447 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
448 return;
451 /* Card Read Status (to be used during frame rx) */
452 static inline void
453 sb1000_read_status(const int ioaddr[], unsigned char in[])
455 in[1] = inb(ioaddr[0] + 1);
456 in[2] = inb(ioaddr[0] + 2);
457 in[3] = inb(ioaddr[0] + 3);
458 in[4] = inb(ioaddr[0] + 4);
459 in[0] = inb(ioaddr[0] + 5);
460 return;
463 /* Issue Read Command (to be used during frame rx) */
464 static inline void
465 sb1000_issue_read_command(const int ioaddr[], const char* name)
467 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
469 sb1000_wait_for_ready_clear(ioaddr, name);
470 outb(0xa0, ioaddr[0] + 6);
471 sb1000_send_command(ioaddr, name, Command0);
472 return;
477 * SB1000 commands for open/configuration
479 /* reset SB1000 card */
480 static inline int
481 sb1000_reset(const int ioaddr[], const char* name)
483 unsigned char st[7];
484 int port, status;
485 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
487 port = ioaddr[1] + 6;
488 outb(0x4, port);
489 inb(port);
490 udelay(1000);
491 outb(0x0, port);
492 inb(port);
493 nicedelay(60000);
494 outb(0x4, port);
495 inb(port);
496 udelay(1000);
497 outb(0x0, port);
498 inb(port);
499 udelay(0);
501 if ((status = card_send_command(ioaddr, name, Command0, st)))
502 return status;
503 if (st[3] != 0xf0)
504 return -EIO;
505 return 0;
508 /* check SB1000 firmware CRC */
509 static inline int
510 sb1000_check_CRC(const int ioaddr[], const char* name)
512 unsigned char st[7];
513 int crc, status;
514 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
516 /* check CRC */
517 if ((status = card_send_command(ioaddr, name, Command0, st)))
518 return status;
519 if (st[1] != st[3] || st[2] != st[4])
520 return -EIO;
521 crc = st[1] << 8 | st[2];
522 return 0;
525 static inline int
526 sb1000_start_get_set_command(const int ioaddr[], const char* name)
528 unsigned char st[7];
529 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
531 return card_send_command(ioaddr, name, Command0, st);
534 static inline int
535 sb1000_end_get_set_command(const int ioaddr[], const char* name)
537 unsigned char st[7];
538 int status;
539 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
540 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
542 if ((status = card_send_command(ioaddr, name, Command0, st)))
543 return status;
544 return card_send_command(ioaddr, name, Command1, st);
547 static inline int
548 sb1000_activate(const int ioaddr[], const char* name)
550 unsigned char st[7];
551 int status;
552 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
553 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
555 nicedelay(50000);
556 if ((status = card_send_command(ioaddr, name, Command0, st)))
557 return status;
558 if ((status = card_send_command(ioaddr, name, Command1, st)))
559 return status;
560 if (st[3] != 0xf1) {
561 if ((status = sb1000_start_get_set_command(ioaddr, name)))
562 return status;
563 return -EIO;
565 udelay(1000);
566 return sb1000_start_get_set_command(ioaddr, name);
569 /* get SB1000 firmware version */
570 static inline int
571 sb1000_get_firmware_version(const int ioaddr[], const char* name,
572 unsigned char version[], int do_end)
574 unsigned char st[7];
575 int status;
576 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
578 if ((status = sb1000_start_get_set_command(ioaddr, name)))
579 return status;
580 if ((status = card_send_command(ioaddr, name, Command0, st)))
581 return status;
582 if (st[0] != 0xa3)
583 return -EIO;
584 version[0] = st[1];
585 version[1] = st[2];
586 if (do_end)
587 return sb1000_end_get_set_command(ioaddr, name);
588 else
589 return 0;
592 /* get SB1000 frequency */
593 static inline int
594 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
596 unsigned char st[7];
597 int status;
598 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
600 udelay(1000);
601 if ((status = sb1000_start_get_set_command(ioaddr, name)))
602 return status;
603 if ((status = card_send_command(ioaddr, name, Command0, st)))
604 return status;
605 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
606 return sb1000_end_get_set_command(ioaddr, name);
609 /* set SB1000 frequency */
610 static inline int
611 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
613 unsigned char st[7];
614 int status;
615 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
617 const int FrequencyLowerLimit = 57000;
618 const int FrequencyUpperLimit = 804000;
620 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
621 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
622 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
623 FrequencyUpperLimit);
624 return -EINVAL;
626 udelay(1000);
627 if ((status = sb1000_start_get_set_command(ioaddr, name)))
628 return status;
629 Command0[5] = frequency & 0xff;
630 frequency >>= 8;
631 Command0[4] = frequency & 0xff;
632 frequency >>= 8;
633 Command0[3] = frequency & 0xff;
634 frequency >>= 8;
635 Command0[2] = frequency & 0xff;
636 return card_send_command(ioaddr, name, Command0, st);
639 /* get SB1000 PIDs */
640 static inline int
641 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
643 unsigned char st[7];
644 int status;
645 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
646 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
647 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
648 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
650 udelay(1000);
651 if ((status = sb1000_start_get_set_command(ioaddr, name)))
652 return status;
654 if ((status = card_send_command(ioaddr, name, Command0, st)))
655 return status;
656 PID[0] = st[1] << 8 | st[2];
658 if ((status = card_send_command(ioaddr, name, Command1, st)))
659 return status;
660 PID[1] = st[1] << 8 | st[2];
662 if ((status = card_send_command(ioaddr, name, Command2, st)))
663 return status;
664 PID[2] = st[1] << 8 | st[2];
666 if ((status = card_send_command(ioaddr, name, Command3, st)))
667 return status;
668 PID[3] = st[1] << 8 | st[2];
670 return sb1000_end_get_set_command(ioaddr, name);
673 /* set SB1000 PIDs */
674 static inline int
675 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
677 unsigned char st[7];
678 short p;
679 int status;
680 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
681 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
682 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
683 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
684 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
686 udelay(1000);
687 if ((status = sb1000_start_get_set_command(ioaddr, name)))
688 return status;
690 p = PID[0];
691 Command0[3] = p & 0xff;
692 p >>= 8;
693 Command0[2] = p & 0xff;
694 if ((status = card_send_command(ioaddr, name, Command0, st)))
695 return status;
697 p = PID[1];
698 Command1[3] = p & 0xff;
699 p >>= 8;
700 Command1[2] = p & 0xff;
701 if ((status = card_send_command(ioaddr, name, Command1, st)))
702 return status;
704 p = PID[2];
705 Command2[3] = p & 0xff;
706 p >>= 8;
707 Command2[2] = p & 0xff;
708 if ((status = card_send_command(ioaddr, name, Command2, st)))
709 return status;
711 p = PID[3];
712 Command3[3] = p & 0xff;
713 p >>= 8;
714 Command3[2] = p & 0xff;
715 if ((status = card_send_command(ioaddr, name, Command3, st)))
716 return status;
718 if ((status = card_send_command(ioaddr, name, Command4, st)))
719 return status;
720 return sb1000_end_get_set_command(ioaddr, name);
724 static inline void
725 sb1000_print_status_buffer(const char* name, unsigned char st[],
726 unsigned char buffer[], int size)
728 int i, j, k;
730 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
731 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
732 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
733 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
734 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
735 buffer[46] << 8 | buffer[47],
736 buffer[42], buffer[43], buffer[44], buffer[45],
737 buffer[48] << 8 | buffer[49]);
738 } else {
739 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
740 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
741 for (j = 0; j < 8 && k < size; j++, k++)
742 printk(" %02x", buffer[k]);
743 printk("\n");
746 return;
750 * SB1000 commands for frame rx interrupt
752 /* receive a single frame and assemble datagram
753 * (this is the heart of the interrupt routine)
755 static inline int
756 sb1000_rx(struct net_device *dev)
759 #define FRAMESIZE 184
760 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
761 short dlen;
762 int ioaddr, ns;
763 unsigned int skbsize;
764 struct sk_buff *skb;
765 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
766 struct net_device_stats *stats = &lp->stats;
768 /* SB1000 frame constants */
769 const int FrameSize = FRAMESIZE;
770 const int NewDatagramHeaderSkip = 8;
771 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
772 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
773 const int ContDatagramHeaderSkip = 7;
774 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
775 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
776 const int TrailerSize = 4;
778 ioaddr = dev->base_addr;
780 insw(ioaddr, (unsigned short*) st, 1);
781 #ifdef XXXDEBUG
782 printk("cm0: received: %02x %02x\n", st[0], st[1]);
783 #endif /* XXXDEBUG */
784 lp->rx_frames++;
786 /* decide if it is a good or bad frame */
787 for (ns = 0; ns < NPIDS; ns++) {
788 session_id = lp->rx_session_id[ns];
789 frame_id = lp->rx_frame_id[ns];
790 if (st[0] == session_id) {
791 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
792 goto good_frame;
793 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
794 goto skipped_frame;
795 } else {
796 goto bad_frame;
798 } else if (st[0] == (session_id | 0x40)) {
799 if ((st[1] & 0xf0) == 0x30) {
800 goto skipped_frame;
801 } else {
802 goto bad_frame;
806 goto bad_frame;
808 skipped_frame:
809 stats->rx_frame_errors++;
810 skb = lp->rx_skb[ns];
811 if (sb1000_debug > 1)
812 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
813 "expecting %02x %02x\n", dev->name, st[0], st[1],
814 skb ? session_id : session_id | 0x40, frame_id);
815 if (skb) {
816 dev_kfree_skb(skb);
817 skb = 0;
820 good_frame:
821 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
822 /* new datagram */
823 if (st[0] & 0x40) {
824 /* get data length */
825 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
826 #ifdef XXXDEBUG
827 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
828 #endif /* XXXDEBUG */
829 if (buffer[0] != NewDatagramHeaderSkip) {
830 if (sb1000_debug > 1)
831 printk(KERN_WARNING "%s: new datagram header skip error: "
832 "got %02x expecting %02x\n", dev->name, buffer[0],
833 NewDatagramHeaderSkip);
834 stats->rx_length_errors++;
835 insw(ioaddr, buffer, NewDatagramDataSize / 2);
836 goto bad_frame_next;
838 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
839 buffer[NewDatagramHeaderSkip + 4]) - 17;
840 if (dlen > SB1000_MRU) {
841 if (sb1000_debug > 1)
842 printk(KERN_WARNING "%s: datagram length (%d) greater "
843 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
844 stats->rx_length_errors++;
845 insw(ioaddr, buffer, NewDatagramDataSize / 2);
846 goto bad_frame_next;
848 lp->rx_dlen[ns] = dlen;
849 /* compute size to allocate for datagram */
850 skbsize = dlen + FrameSize;
851 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
852 if (sb1000_debug > 1)
853 printk(KERN_WARNING "%s: can't allocate %d bytes long "
854 "skbuff\n", dev->name, skbsize);
855 stats->rx_dropped++;
856 insw(ioaddr, buffer, NewDatagramDataSize / 2);
857 goto dropped_frame;
859 skb->dev = dev;
860 skb->mac.raw = skb->data;
861 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
862 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
863 NewDatagramDataSize / 2);
864 lp->rx_skb[ns] = skb;
865 } else {
866 /* continuation of previous datagram */
867 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
868 if (buffer[0] != ContDatagramHeaderSkip) {
869 if (sb1000_debug > 1)
870 printk(KERN_WARNING "%s: cont datagram header skip error: "
871 "got %02x expecting %02x\n", dev->name, buffer[0],
872 ContDatagramHeaderSkip);
873 stats->rx_length_errors++;
874 insw(ioaddr, buffer, ContDatagramDataSize / 2);
875 goto bad_frame_next;
877 skb = lp->rx_skb[ns];
878 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
879 ContDatagramDataSize / 2);
880 dlen = lp->rx_dlen[ns];
882 if (skb->len < dlen + TrailerSize) {
883 lp->rx_session_id[ns] &= ~0x40;
884 return 0;
887 /* datagram completed: send to upper level */
888 skb_trim(skb, dlen);
889 netif_rx(skb);
890 stats->rx_bytes+=dlen;
891 stats->rx_packets++;
892 lp->rx_bytes += dlen;
893 lp->rx_skb[ns] = 0;
894 lp->rx_session_id[ns] |= 0x40;
895 return 0;
897 bad_frame:
898 insw(ioaddr, buffer, FrameSize / 2);
899 if (sb1000_debug > 1)
900 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
901 dev->name, st[0], st[1]);
902 stats->rx_frame_errors++;
903 bad_frame_next:
904 if (sb1000_debug > 2)
905 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
906 dropped_frame:
907 stats->rx_errors++;
908 if (ns < NPIDS) {
909 if ((skb = lp->rx_skb[ns])) {
910 dev_kfree_skb(skb);
911 lp->rx_skb[ns] = 0;
913 lp->rx_session_id[ns] |= 0x40;
915 return -1;
918 static inline void
919 sb1000_error_dpc(struct net_device *dev)
921 char *name;
922 unsigned char st[5];
923 int ioaddr[2];
924 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
925 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
926 const int ErrorDpcCounterInitialize = 200;
928 ioaddr[0] = dev->base_addr;
929 /* rmem_end holds the second I/O address - fv */
930 ioaddr[1] = dev->rmem_end;
931 name = dev->name;
933 sb1000_wait_for_ready_clear(ioaddr, name);
934 sb1000_send_command(ioaddr, name, Command0);
935 sb1000_wait_for_ready(ioaddr, name);
936 sb1000_read_status(ioaddr, st);
937 if (st[1] & 0x10)
938 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
939 return;
944 * Linux interface functions
946 static int
947 sb1000_open(struct net_device *dev)
949 char *name;
950 int ioaddr[2], status;
951 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
952 const unsigned short FirmwareVersion[] = {0x01, 0x01};
954 ioaddr[0] = dev->base_addr;
955 /* rmem_end holds the second I/O address - fv */
956 ioaddr[1] = dev->rmem_end;
957 name = dev->name;
958 request_region(ioaddr[0], SB1000_IO_EXTENT, "sb1000");
959 request_region(ioaddr[1], SB1000_IO_EXTENT, "sb1000");
961 /* initialize sb1000 */
962 if ((status = sb1000_reset(ioaddr, name)))
963 return status;
964 nicedelay(200000);
965 if ((status = sb1000_check_CRC(ioaddr, name)))
966 return status;
968 /* initialize private data before board can catch interrupts */
969 lp->rx_skb[0] = NULL;
970 lp->rx_skb[1] = NULL;
971 lp->rx_skb[2] = NULL;
972 lp->rx_skb[3] = NULL;
973 lp->rx_dlen[0] = 0;
974 lp->rx_dlen[1] = 0;
975 lp->rx_dlen[2] = 0;
976 lp->rx_dlen[3] = 0;
977 lp->rx_bytes = 0;
978 lp->rx_frames = 0;
979 lp->rx_error_count = 0;
980 lp->rx_error_dpc_count = 0;
981 lp->rx_session_id[0] = 0x50;
982 lp->rx_session_id[0] = 0x48;
983 lp->rx_session_id[0] = 0x44;
984 lp->rx_session_id[0] = 0x42;
985 lp->rx_frame_id[0] = 0;
986 lp->rx_frame_id[1] = 0;
987 lp->rx_frame_id[2] = 0;
988 lp->rx_frame_id[3] = 0;
989 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
990 return -EAGAIN;
993 if (sb1000_debug > 2)
994 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
996 /* Activate board and check firmware version */
997 udelay(1000);
998 if ((status = sb1000_activate(ioaddr, name)))
999 return status;
1000 udelay(0);
1001 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
1002 return status;
1003 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
1004 printk(KERN_WARNING "%s: found firmware version %x.%02x "
1005 "(should be %x.%02x)\n", name, version[0], version[1],
1006 FirmwareVersion[0], FirmwareVersion[1]);
1009 netif_start_queue(dev);
1010 return 0; /* Always succeed */
1013 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1015 char* name;
1016 unsigned char version[2];
1017 short PID[4];
1018 int ioaddr[2], status, frequency;
1019 unsigned int stats[5];
1020 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1022 if (!(dev && dev->flags & IFF_UP))
1023 return -ENODEV;
1025 ioaddr[0] = dev->base_addr;
1026 /* rmem_end holds the second I/O address - fv */
1027 ioaddr[1] = dev->rmem_end;
1028 name = dev->name;
1030 switch (cmd) {
1031 case SIOCGCMSTATS: /* get statistics */
1032 stats[0] = lp->rx_bytes;
1033 stats[1] = lp->rx_frames;
1034 stats[2] = lp->stats.rx_packets;
1035 stats[3] = lp->stats.rx_errors;
1036 stats[4] = lp->stats.rx_dropped;
1037 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1038 return -EFAULT;
1039 status = 0;
1040 break;
1042 case SIOCGCMFIRMWARE: /* get firmware version */
1043 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1044 return status;
1045 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1046 return -EFAULT;
1047 break;
1049 case SIOCGCMFREQUENCY: /* get frequency */
1050 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1051 return status;
1052 if(put_user(frequency, (int*) ifr->ifr_data))
1053 return -EFAULT;
1054 break;
1056 case SIOCSCMFREQUENCY: /* set frequency */
1057 if (!capable(CAP_NET_ADMIN))
1058 return -EPERM;
1059 if(get_user(frequency, (int*) ifr->ifr_data))
1060 return -EFAULT;
1061 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1062 return status;
1063 break;
1065 case SIOCGCMPIDS: /* get PIDs */
1066 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1067 return status;
1068 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1069 return -EFAULT;
1070 break;
1072 case SIOCSCMPIDS: /* set PIDs */
1073 if (!capable(CAP_NET_ADMIN))
1074 return -EPERM;
1075 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1076 return -EFAULT;
1077 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1078 return status;
1079 /* set session_id, frame_id and pkt_type too */
1080 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1081 lp->rx_session_id[1] = 0x48;
1082 lp->rx_session_id[2] = 0x44;
1083 lp->rx_session_id[3] = 0x42;
1084 lp->rx_frame_id[0] = 0;
1085 lp->rx_frame_id[1] = 0;
1086 lp->rx_frame_id[2] = 0;
1087 lp->rx_frame_id[3] = 0;
1088 break;
1090 default:
1091 status = -EINVAL;
1092 break;
1094 return status;
1097 /* transmit function: do nothing since SB1000 can't send anything out */
1098 static int
1099 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1101 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1102 /* sb1000 can't xmit datagrams */
1103 dev_kfree_skb(skb);
1104 return 0;
1107 /* SB1000 interrupt handler. */
1108 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1110 char *name;
1111 unsigned char st;
1112 int ioaddr[2];
1113 struct net_device *dev = (struct net_device *) dev_id;
1114 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1116 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1117 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1118 const int MaxRxErrorCount = 6;
1120 if (dev == NULL) {
1121 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1122 irq);
1123 return;
1126 ioaddr[0] = dev->base_addr;
1127 /* rmem_end holds the second I/O address - fv */
1128 ioaddr[1] = dev->rmem_end;
1129 name = dev->name;
1131 /* is it a good interrupt? */
1132 st = inb(ioaddr[1] + 6);
1133 if (!(st & 0x08 && st & 0x20)) {
1134 return;
1137 if (sb1000_debug > 3)
1138 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1140 st = inb(ioaddr[0] + 7);
1141 if (sb1000_rx(dev))
1142 lp->rx_error_count++;
1143 #ifdef SB1000_DELAY
1144 udelay(SB1000_DELAY);
1145 #endif /* SB1000_DELAY */
1146 sb1000_issue_read_command(ioaddr, name);
1147 if (st & 0x01) {
1148 sb1000_error_dpc(dev);
1149 sb1000_issue_read_command(ioaddr, name);
1151 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1152 sb1000_wait_for_ready_clear(ioaddr, name);
1153 sb1000_send_command(ioaddr, name, Command0);
1154 sb1000_wait_for_ready(ioaddr, name);
1155 sb1000_issue_read_command(ioaddr, name);
1157 if (lp->rx_error_count >= MaxRxErrorCount) {
1158 sb1000_wait_for_ready_clear(ioaddr, name);
1159 sb1000_send_command(ioaddr, name, Command1);
1160 sb1000_wait_for_ready(ioaddr, name);
1161 sb1000_issue_read_command(ioaddr, name);
1162 lp->rx_error_count = 0;
1165 return;
1168 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1170 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1171 return &lp->stats;
1174 static int sb1000_close(struct net_device *dev)
1176 int i;
1177 int ioaddr[2];
1178 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1180 if (sb1000_debug > 2)
1181 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1183 netif_stop_queue(dev);
1185 ioaddr[0] = dev->base_addr;
1186 /* rmem_end holds the second I/O address - fv */
1187 ioaddr[1] = dev->rmem_end;
1189 free_irq(dev->irq, dev);
1190 /* If we don't do this, we can't re-insmod it later. */
1191 release_region(ioaddr[1], SB1000_IO_EXTENT);
1192 release_region(ioaddr[0], SB1000_IO_EXTENT);
1194 /* free rx_skb's if needed */
1195 for (i=0; i<4; i++) {
1196 if (lp->rx_skb[i]) {
1197 dev_kfree_skb(lp->rx_skb[i]);
1200 return 0;
1203 #ifdef MODULE
1204 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1205 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1206 MODULE_PARM(io, "1-2i");
1207 MODULE_PARM(irq, "i");
1209 static struct net_device dev_sb1000;
1210 static int io[2];
1211 static int irq;
1214 init_module(void)
1216 int i;
1217 for (i = 0; i < 100; i++) {
1218 sprintf(dev_sb1000.name, "cm%d", i);
1219 if (dev_get(dev_sb1000.name) == 0) break;
1221 if (i == 100) {
1222 printk(KERN_ERR "sb1000: can't register any device cm<n>\n");
1223 return -ENFILE;
1225 dev_sb1000.init = sb1000_probe;
1226 dev_sb1000.base_addr = io[0];
1227 /* rmem_end holds the second I/O address - fv */
1228 dev_sb1000.rmem_end = io[1];
1229 dev_sb1000.irq = irq;
1230 if (register_netdev(&dev_sb1000) != 0) {
1231 printk(KERN_ERR "sb1000: failed to register device (io: %03x,%03x "
1232 "irq: %d)\n", io[0], io[1], irq);
1233 return -EIO;
1235 return 0;
1238 void cleanup_module(void)
1240 unregister_netdev(&dev_sb1000);
1241 release_region(dev_sb1000.base_addr, 16);
1242 release_region(dev_sb1000.rmem_end, 16);
1243 kfree(dev_sb1000.priv);
1244 dev_sb1000.priv = NULL;
1246 #endif /* MODULE */
1249 * Local variables:
1250 * compile-command: "gcc -D__KERNEL__ -DMODULE -Wall -Wstrict-prototypes -O -m486 -c sb1000.c"
1251 * version-control: t
1252 * tab-width: 4
1253 * c-basic-offset: 4
1254 * End: