* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / sb1000.c
blobb0d8a9ad4d778ea37c679e798aceed2f0e28aa0f
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>
60 /* for SIOGCM/SIOSCM stuff */
61 #include <linux/if_cablemodem.h>
63 #ifdef SB1000_DEBUG
64 int sb1000_debug = SB1000_DEBUG;
65 #else
66 int sb1000_debug = 1;
67 #endif
69 static const int SB1000_IO_EXTENT = 8;
70 /* SB1000 Maximum Receive Unit */
71 static const int SB1000_MRU = 1500; /* octects */
73 #define NPIDS 4
74 struct sb1000_private {
75 struct sk_buff *rx_skb[NPIDS];
76 short rx_dlen[NPIDS];
77 unsigned int rx_bytes;
78 unsigned int rx_frames;
79 short rx_error_count;
80 short rx_error_dpc_count;
81 unsigned char rx_session_id[NPIDS];
82 unsigned char rx_frame_id[NPIDS];
83 unsigned char rx_pkt_type[NPIDS];
84 struct net_device_stats stats;
87 /* prototypes for Linux interface */
88 extern int sb1000_probe(struct net_device *dev);
89 static int sb1000_open(struct net_device *dev);
90 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
91 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
92 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
93 static struct enet_statistics *sb1000_stats(struct net_device *dev);
94 static int sb1000_close(struct net_device *dev);
96 /* Plug-n-Play routine */
97 static inline unsigned char read_resource_data(void);
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);
142 /* Plug-n-Play constants */
143 static const int READ_DATA_PORT = 0x203; /* This port number may change!!! */
144 static const int ADDRESS_PORT = 0x279;
145 static const int WRITE_DATA_PORT = 0xa79;
147 /* Plug-n-Play read resource mechanism */
148 static inline unsigned char
149 read_resource_data(void) {
150 /* poll */
151 outb(0x05, ADDRESS_PORT); /* Select PnP status register. */
152 while (!(inb(READ_DATA_PORT) & 0x1)) ;
153 /* read resource data */
154 outb(0x04, ADDRESS_PORT); /* Select PnP resource data register. */
155 return inb(READ_DATA_PORT);
158 /* probe for SB1000 using Plug-n-Play mechanism */
160 sb1000_probe(struct net_device *dev)
163 unsigned short ioaddr[2], irq;
164 short i, csn;
165 unsigned int serial_number;
167 const unsigned char initiation_key[] = { 0x00, 0x00, 0x6a, 0xb5, 0xda,
168 0xed, 0xf6, 0xfb, 0x7d, 0xbe, 0xdf, 0x6f, 0x37, 0x1b, 0x0d,
169 0x86, 0xc3, 0x61, 0xb0, 0x58, 0x2c, 0x16, 0x8b, 0x45, 0xa2,
170 0xd1, 0xe8, 0x74, 0x3a, 0x9d, 0xce, 0xe7, 0x73, 0x39 };
171 const unsigned char sb1000_vendor_ID[] = {
172 0x1d, 0x23, 0x10, 0x00 }; /* "GIC1000" */
174 /* Reset the ISA PnP mechanism */
175 outb(0x02, ADDRESS_PORT); /* Select PnP config control register. */
176 outb(0x02, WRITE_DATA_PORT); /* Return to WaitForKey state. */
178 /* send initiation key */
179 for (i = 0; i < sizeof(initiation_key) / sizeof(initiation_key[0]); i++) {
180 outb(initiation_key[i], ADDRESS_PORT);
183 /* set card CSN into configuration mode */
184 for (csn = 1; csn <= 255; csn++) {
185 outb(0x03, ADDRESS_PORT); /* Select PnP wake[CSN] register. */
186 outb(csn, WRITE_DATA_PORT); /* Wake[CSN] */
187 /* check card ID */
188 for (i = 0; i < 4; i++) {
189 if (read_resource_data() != sb1000_vendor_ID[i]) break;
191 if (i == 4) break;
194 /* SB1000 not found */
195 if (csn > 255) {
196 /* return to WaitForKey state */
197 outb(0x02, ADDRESS_PORT); /* Select PnP config control register. */
198 outb(0x02, WRITE_DATA_PORT);/* Return to WaitForKey state. */
199 return -ENODEV;
202 /* found: get serial number and skip checksum */
203 serial_number = 0;
204 for (i = 0; i < 4; i++) {
205 serial_number |= read_resource_data() << (8 * i);
207 read_resource_data();
209 /* get I/O port base address */
210 outb(0x60, ADDRESS_PORT); /* Select PnP I/O port base address 0. */
211 ioaddr[0] = inb(READ_DATA_PORT) << 8;
212 outb(0x61, ADDRESS_PORT);
213 ioaddr[0] |= inb(READ_DATA_PORT);
214 outb(0x62, ADDRESS_PORT); /* Select PnP I/O port base address 1. */
215 ioaddr[1] = inb(READ_DATA_PORT) << 8;
216 outb(0x63, ADDRESS_PORT);
217 ioaddr[1] |= inb(READ_DATA_PORT);
219 /* get IRQ */
220 outb(0x70, ADDRESS_PORT); /* Select PnP IRQ level select 0. */
221 irq = inb(READ_DATA_PORT);
223 /* return to WaitForKey state */
224 outb(0x02, ADDRESS_PORT); /* Select PnP config control register. */
225 outb(0x02, WRITE_DATA_PORT); /* Return to WaitForKey state. */
227 /* check I/O base and IRQ */
228 if (dev->base_addr != 0 && dev->base_addr != ioaddr[0]) {
229 return -ENODEV;
231 if (dev->rmem_end != 0 && dev->rmem_end != ioaddr[1]) {
232 return -ENODEV;
234 if (dev->irq != 0 && dev->irq != irq) {
235 return -ENODEV;
238 dev->base_addr = ioaddr[0];
239 /* rmem_end holds the second I/O address - fv */
240 dev->rmem_end = ioaddr[1];
241 dev->irq = irq;
243 if (sb1000_debug > 0)
244 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), csn %d, "
245 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
246 dev->rmem_end, csn, serial_number, dev->irq);
248 dev = init_etherdev(dev, 0);
250 /* Make up a SB1000-specific-data structure. */
251 dev->priv = kmalloc(sizeof(struct sb1000_private), GFP_KERNEL);
252 if (dev->priv == NULL)
253 return -ENOMEM;
254 memset(dev->priv, 0, sizeof(struct sb1000_private));
256 if (sb1000_debug > 0)
257 printk(KERN_NOTICE "%s", version);
259 /* The SB1000-specific entries in the device structure. */
260 dev->open = sb1000_open;
261 dev->do_ioctl = sb1000_dev_ioctl;
262 dev->hard_start_xmit = sb1000_start_xmit;
263 dev->stop = sb1000_close;
264 dev->get_stats = sb1000_stats;
266 /* Fill in the generic fields of the device structure. */
267 dev->change_mtu = NULL;
268 dev->hard_header = NULL;
269 dev->rebuild_header = NULL;
270 dev->set_mac_address = NULL;
271 dev->header_cache_update= NULL;
273 dev->type = ARPHRD_ETHER;
274 dev->hard_header_len = 0;
275 dev->mtu = 1500;
276 dev->addr_len = ETH_ALEN;
277 /* hardware address is 0:0:serial_number */
278 dev->dev_addr[0] = 0;
279 dev->dev_addr[1] = 0;
280 dev->dev_addr[2] = serial_number >> 24 & 0xff;
281 dev->dev_addr[3] = serial_number >> 16 & 0xff;
282 dev->dev_addr[4] = serial_number >> 8 & 0xff;
283 dev->dev_addr[5] = serial_number >> 0 & 0xff;
284 dev->tx_queue_len = 0;
286 /* New-style flags. */
287 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
288 return 0;
293 * SB1000 hardware routines to be used during open/configuration phases
295 const int TimeOutJiffies = (int)(8.75 * HZ);
297 static inline void nicedelay(unsigned long usecs)
299 current->state = TASK_INTERRUPTIBLE;
300 schedule_timeout(HZ);
301 return;
304 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
305 static inline int
306 card_wait_for_busy_clear(const int ioaddr[], const char* name)
308 unsigned char a;
309 unsigned long timeout;
311 a = inb(ioaddr[0] + 7);
312 timeout = jiffies + TimeOutJiffies;
313 while (a & 0x80 || a & 0x40) {
314 /* a little sleep */
315 current->state = TASK_INTERRUPTIBLE;
316 schedule_timeout(0);
317 a = inb(ioaddr[0] + 7);
318 if (jiffies >= timeout) {
319 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
320 name);
321 return -ETIME;
325 return 0;
328 /* Card Wait For Ready (cannot be used during an interrupt) */
329 static inline int
330 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
332 unsigned char a;
333 unsigned long timeout;
335 a = inb(ioaddr[1] + 6);
336 timeout = jiffies + TimeOutJiffies;
337 while (a & 0x80 || !(a & 0x40)) {
338 /* a little sleep */
339 current->state = TASK_INTERRUPTIBLE;
340 schedule_timeout(0);
341 a = inb(ioaddr[1] + 6);
342 if (jiffies >= timeout) {
343 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
344 name);
345 return -ETIME;
349 in[1] = inb(ioaddr[0] + 1);
350 in[2] = inb(ioaddr[0] + 2);
351 in[3] = inb(ioaddr[0] + 3);
352 in[4] = inb(ioaddr[0] + 4);
353 in[0] = inb(ioaddr[0] + 5);
354 in[6] = inb(ioaddr[0] + 6);
355 in[5] = inb(ioaddr[1] + 6);
356 return 0;
359 /* Card Send Command (cannot be used during an interrupt) */
360 static inline int
361 card_send_command(const int ioaddr[], const char* name,
362 const unsigned char out[], unsigned char in[])
364 int status, x;
366 if ((status = card_wait_for_busy_clear(ioaddr, name)))
367 return status;
368 outb(0xa0, ioaddr[0] + 6);
369 outb(out[2], ioaddr[0] + 1);
370 outb(out[3], ioaddr[0] + 2);
371 outb(out[4], ioaddr[0] + 3);
372 outb(out[5], ioaddr[0] + 4);
373 outb(out[1], ioaddr[0] + 5);
374 outb(0xa0, ioaddr[0] + 6);
375 outb(out[0], ioaddr[0] + 7);
376 if (out[0] != 0x20 && out[0] != 0x30) {
377 if ((status = card_wait_for_ready(ioaddr, name, in)))
378 return status;
379 inb(ioaddr[0] + 7);
380 if (sb1000_debug > 3)
381 printk(KERN_DEBUG "%s: card_send_command "
382 "out: %02x%02x%02x%02x%02x%02x "
383 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
384 out[0], out[1], out[2], out[3], out[4], out[5],
385 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
386 } else {
387 if (sb1000_debug > 3)
388 printk(KERN_DEBUG "%s: card_send_command "
389 "out: %02x%02x%02x%02x%02x%02x\n", name,
390 out[0], out[1], out[2], out[3], out[4], out[5]);
393 if (out[1] == 0x1b) {
394 x = (out[2] == 0x02);
395 } else {
396 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
397 return -EIO;
399 return 0;
404 * SB1000 hardware routines to be used during frame rx interrupt
406 const int Sb1000TimeOutJiffies = 7 * HZ;
408 /* Card Wait For Ready (to be used during frame rx) */
409 static inline int
410 sb1000_wait_for_ready(const int ioaddr[], const char* name)
412 unsigned long timeout;
414 timeout = jiffies + Sb1000TimeOutJiffies;
415 while (inb(ioaddr[1] + 6) & 0x80) {
416 if (jiffies >= timeout) {
417 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
418 name);
419 return -ETIME;
422 timeout = jiffies + Sb1000TimeOutJiffies;
423 while (!(inb(ioaddr[1] + 6) & 0x40)) {
424 if (jiffies >= timeout) {
425 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
426 name);
427 return -ETIME;
430 inb(ioaddr[0] + 7);
431 return 0;
434 /* Card Wait For Ready Clear (to be used during frame rx) */
435 static inline int
436 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
438 unsigned long timeout;
440 timeout = jiffies + Sb1000TimeOutJiffies;
441 while (inb(ioaddr[1] + 6) & 0x80) {
442 if (jiffies >= timeout) {
443 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
444 name);
445 return -ETIME;
448 timeout = jiffies + Sb1000TimeOutJiffies;
449 while (inb(ioaddr[1] + 6) & 0x40) {
450 if (jiffies >= timeout) {
451 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
452 name);
453 return -ETIME;
456 return 0;
459 /* Card Send Command (to be used during frame rx) */
460 static inline void
461 sb1000_send_command(const int ioaddr[], const char* name,
462 const unsigned char out[])
464 outb(out[2], ioaddr[0] + 1);
465 outb(out[3], ioaddr[0] + 2);
466 outb(out[4], ioaddr[0] + 3);
467 outb(out[5], ioaddr[0] + 4);
468 outb(out[1], ioaddr[0] + 5);
469 outb(out[0], ioaddr[0] + 7);
470 if (sb1000_debug > 3)
471 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
472 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
473 return;
476 /* Card Read Status (to be used during frame rx) */
477 static inline void
478 sb1000_read_status(const int ioaddr[], unsigned char in[])
480 in[1] = inb(ioaddr[0] + 1);
481 in[2] = inb(ioaddr[0] + 2);
482 in[3] = inb(ioaddr[0] + 3);
483 in[4] = inb(ioaddr[0] + 4);
484 in[0] = inb(ioaddr[0] + 5);
485 return;
488 /* Issue Read Command (to be used during frame rx) */
489 static inline void
490 sb1000_issue_read_command(const int ioaddr[], const char* name)
492 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
494 sb1000_wait_for_ready_clear(ioaddr, name);
495 outb(0xa0, ioaddr[0] + 6);
496 sb1000_send_command(ioaddr, name, Command0);
497 return;
502 * SB1000 commands for open/configuration
504 /* reset SB1000 card */
505 static inline int
506 sb1000_reset(const int ioaddr[], const char* name)
508 unsigned char st[7];
509 int port, status;
510 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
512 port = ioaddr[1] + 6;
513 outb(0x4, port);
514 inb(port);
515 udelay(1000);
516 outb(0x0, port);
517 inb(port);
518 nicedelay(60000);
519 outb(0x4, port);
520 inb(port);
521 udelay(1000);
522 outb(0x0, port);
523 inb(port);
524 udelay(0);
526 if ((status = card_send_command(ioaddr, name, Command0, st)))
527 return status;
528 if (st[3] != 0xf0)
529 return -EIO;
530 return 0;
533 /* check SB1000 firmware CRC */
534 static inline int
535 sb1000_check_CRC(const int ioaddr[], const char* name)
537 unsigned char st[7];
538 int crc, status;
539 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
541 /* check CRC */
542 if ((status = card_send_command(ioaddr, name, Command0, st)))
543 return status;
544 if (st[1] != st[3] || st[2] != st[4])
545 return -EIO;
546 crc = st[1] << 8 | st[2];
547 return 0;
550 static inline int
551 sb1000_start_get_set_command(const int ioaddr[], const char* name)
553 unsigned char st[7];
554 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
556 return card_send_command(ioaddr, name, Command0, st);
559 static inline int
560 sb1000_end_get_set_command(const int ioaddr[], const char* name)
562 unsigned char st[7];
563 int status;
564 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
565 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
567 if ((status = card_send_command(ioaddr, name, Command0, st)))
568 return status;
569 return card_send_command(ioaddr, name, Command1, st);
572 static inline int
573 sb1000_activate(const int ioaddr[], const char* name)
575 unsigned char st[7];
576 int status;
577 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
578 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
580 nicedelay(50000);
581 if ((status = card_send_command(ioaddr, name, Command0, st)))
582 return status;
583 if ((status = card_send_command(ioaddr, name, Command1, st)))
584 return status;
585 if (st[3] != 0xf1) {
586 if ((status = sb1000_start_get_set_command(ioaddr, name)))
587 return status;
588 return -EIO;
590 udelay(1000);
591 return sb1000_start_get_set_command(ioaddr, name);
594 /* get SB1000 firmware version */
595 static inline int
596 sb1000_get_firmware_version(const int ioaddr[], const char* name,
597 unsigned char version[], int do_end)
599 unsigned char st[7];
600 int status;
601 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
603 if ((status = sb1000_start_get_set_command(ioaddr, name)))
604 return status;
605 if ((status = card_send_command(ioaddr, name, Command0, st)))
606 return status;
607 if (st[0] != 0xa3)
608 return -EIO;
609 version[0] = st[1];
610 version[1] = st[2];
611 if (do_end)
612 return sb1000_end_get_set_command(ioaddr, name);
613 else
614 return 0;
617 /* get SB1000 frequency */
618 static inline int
619 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
621 unsigned char st[7];
622 int status;
623 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
625 udelay(1000);
626 if ((status = sb1000_start_get_set_command(ioaddr, name)))
627 return status;
628 if ((status = card_send_command(ioaddr, name, Command0, st)))
629 return status;
630 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
631 return sb1000_end_get_set_command(ioaddr, name);
634 /* set SB1000 frequency */
635 static inline int
636 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
638 unsigned char st[7];
639 int status;
640 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
642 const int FrequencyLowerLimit = 57000;
643 const int FrequencyUpperLimit = 804000;
645 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
646 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
647 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
648 FrequencyUpperLimit);
649 return -EINVAL;
651 udelay(1000);
652 if ((status = sb1000_start_get_set_command(ioaddr, name)))
653 return status;
654 Command0[5] = frequency & 0xff;
655 frequency >>= 8;
656 Command0[4] = frequency & 0xff;
657 frequency >>= 8;
658 Command0[3] = frequency & 0xff;
659 frequency >>= 8;
660 Command0[2] = frequency & 0xff;
661 return card_send_command(ioaddr, name, Command0, st);
664 /* get SB1000 PIDs */
665 static inline int
666 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
668 unsigned char st[7];
669 int status;
670 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
671 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
672 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
673 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
675 udelay(1000);
676 if ((status = sb1000_start_get_set_command(ioaddr, name)))
677 return status;
679 if ((status = card_send_command(ioaddr, name, Command0, st)))
680 return status;
681 PID[0] = st[1] << 8 | st[2];
683 if ((status = card_send_command(ioaddr, name, Command1, st)))
684 return status;
685 PID[1] = st[1] << 8 | st[2];
687 if ((status = card_send_command(ioaddr, name, Command2, st)))
688 return status;
689 PID[2] = st[1] << 8 | st[2];
691 if ((status = card_send_command(ioaddr, name, Command3, st)))
692 return status;
693 PID[3] = st[1] << 8 | st[2];
695 return sb1000_end_get_set_command(ioaddr, name);
698 /* set SB1000 PIDs */
699 static inline int
700 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
702 unsigned char st[7];
703 short p;
704 int status;
705 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
706 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
707 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
708 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
709 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
711 udelay(1000);
712 if ((status = sb1000_start_get_set_command(ioaddr, name)))
713 return status;
715 p = PID[0];
716 Command0[3] = p & 0xff;
717 p >>= 8;
718 Command0[2] = p & 0xff;
719 if ((status = card_send_command(ioaddr, name, Command0, st)))
720 return status;
722 p = PID[1];
723 Command1[3] = p & 0xff;
724 p >>= 8;
725 Command1[2] = p & 0xff;
726 if ((status = card_send_command(ioaddr, name, Command1, st)))
727 return status;
729 p = PID[2];
730 Command2[3] = p & 0xff;
731 p >>= 8;
732 Command2[2] = p & 0xff;
733 if ((status = card_send_command(ioaddr, name, Command2, st)))
734 return status;
736 p = PID[3];
737 Command3[3] = p & 0xff;
738 p >>= 8;
739 Command3[2] = p & 0xff;
740 if ((status = card_send_command(ioaddr, name, Command3, st)))
741 return status;
743 if ((status = card_send_command(ioaddr, name, Command4, st)))
744 return status;
745 return sb1000_end_get_set_command(ioaddr, name);
749 static inline void
750 sb1000_print_status_buffer(const char* name, unsigned char st[],
751 unsigned char buffer[], int size)
753 int i, j, k;
755 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
756 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
757 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
758 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
759 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
760 buffer[46] << 8 | buffer[47],
761 buffer[42], buffer[43], buffer[44], buffer[45],
762 buffer[48] << 8 | buffer[49]);
763 } else {
764 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
765 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
766 for (j = 0; j < 8 && k < size; j++, k++)
767 printk(" %02x", buffer[k]);
768 printk("\n");
771 return;
775 * SB1000 commands for frame rx interrupt
777 /* receive a single frame and assemble datagram
778 * (this is the heart of the interrupt routine)
780 static inline int
781 sb1000_rx(struct net_device *dev)
784 #define FRAMESIZE 184
785 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
786 short dlen;
787 int ioaddr, ns;
788 unsigned int skbsize;
789 struct sk_buff *skb;
790 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
791 struct enet_statistics *stats = &lp->stats;
793 /* SB1000 frame constants */
794 const int FrameSize = FRAMESIZE;
795 const int NewDatagramHeaderSkip = 8;
796 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
797 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
798 const int ContDatagramHeaderSkip = 7;
799 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
800 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
801 const int TrailerSize = 4;
803 ioaddr = dev->base_addr;
805 insw(ioaddr, (unsigned short*) st, 1);
806 #ifdef XXXDEBUG
807 printk("cm0: received: %02x %02x\n", st[0], st[1]);
808 #endif /* XXXDEBUG */
809 lp->rx_frames++;
811 /* decide if it is a good or bad frame */
812 for (ns = 0; ns < NPIDS; ns++) {
813 session_id = lp->rx_session_id[ns];
814 frame_id = lp->rx_frame_id[ns];
815 if (st[0] == session_id) {
816 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
817 goto good_frame;
818 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
819 goto skipped_frame;
820 } else {
821 goto bad_frame;
823 } else if (st[0] == (session_id | 0x40)) {
824 if ((st[1] & 0xf0) == 0x30) {
825 goto skipped_frame;
826 } else {
827 goto bad_frame;
831 goto bad_frame;
833 skipped_frame:
834 stats->rx_frame_errors++;
835 skb = lp->rx_skb[ns];
836 if (sb1000_debug > 1)
837 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
838 "expecting %02x %02x\n", dev->name, st[0], st[1],
839 skb ? session_id : session_id | 0x40, frame_id);
840 if (skb) {
841 dev_kfree_skb(skb);
842 skb = 0;
845 good_frame:
846 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
847 /* new datagram */
848 if (st[0] & 0x40) {
849 /* get data length */
850 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
851 #ifdef XXXDEBUG
852 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
853 #endif /* XXXDEBUG */
854 if (buffer[0] != NewDatagramHeaderSkip) {
855 if (sb1000_debug > 1)
856 printk(KERN_WARNING "%s: new datagram header skip error: "
857 "got %02x expecting %02x\n", dev->name, buffer[0],
858 NewDatagramHeaderSkip);
859 stats->rx_length_errors++;
860 insw(ioaddr, buffer, NewDatagramDataSize / 2);
861 goto bad_frame_next;
863 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
864 buffer[NewDatagramHeaderSkip + 4]) - 17;
865 if (dlen > SB1000_MRU) {
866 if (sb1000_debug > 1)
867 printk(KERN_WARNING "%s: datagram length (%d) greater "
868 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
869 stats->rx_length_errors++;
870 insw(ioaddr, buffer, NewDatagramDataSize / 2);
871 goto bad_frame_next;
873 lp->rx_dlen[ns] = dlen;
874 /* compute size to allocate for datagram */
875 skbsize = dlen + FrameSize;
876 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
877 if (sb1000_debug > 1)
878 printk(KERN_WARNING "%s: can't allocate %d bytes long "
879 "skbuff\n", dev->name, skbsize);
880 stats->rx_dropped++;
881 insw(ioaddr, buffer, NewDatagramDataSize / 2);
882 goto dropped_frame;
884 skb->dev = dev;
885 skb->mac.raw = skb->data;
886 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
887 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
888 NewDatagramDataSize / 2);
889 lp->rx_skb[ns] = skb;
890 } else {
891 /* continuation of previous datagram */
892 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
893 if (buffer[0] != ContDatagramHeaderSkip) {
894 if (sb1000_debug > 1)
895 printk(KERN_WARNING "%s: cont datagram header skip error: "
896 "got %02x expecting %02x\n", dev->name, buffer[0],
897 ContDatagramHeaderSkip);
898 stats->rx_length_errors++;
899 insw(ioaddr, buffer, ContDatagramDataSize / 2);
900 goto bad_frame_next;
902 skb = lp->rx_skb[ns];
903 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
904 ContDatagramDataSize / 2);
905 dlen = lp->rx_dlen[ns];
907 if (skb->len < dlen + TrailerSize) {
908 lp->rx_session_id[ns] &= ~0x40;
909 return 0;
912 /* datagram completed: send to upper level */
913 skb_trim(skb, dlen);
914 netif_rx(skb);
915 stats->rx_bytes+=dlen;
916 stats->rx_packets++;
917 lp->rx_bytes += dlen;
918 lp->rx_skb[ns] = 0;
919 lp->rx_session_id[ns] |= 0x40;
920 return 0;
922 bad_frame:
923 insw(ioaddr, buffer, FrameSize / 2);
924 if (sb1000_debug > 1)
925 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
926 dev->name, st[0], st[1]);
927 stats->rx_frame_errors++;
928 bad_frame_next:
929 if (sb1000_debug > 2)
930 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
931 dropped_frame:
932 stats->rx_errors++;
933 if (ns < NPIDS) {
934 if ((skb = lp->rx_skb[ns])) {
935 dev_kfree_skb(skb);
936 lp->rx_skb[ns] = 0;
938 lp->rx_session_id[ns] |= 0x40;
940 return -1;
943 static inline void
944 sb1000_error_dpc(struct net_device *dev)
946 char *name;
947 unsigned char st[5];
948 int ioaddr[2];
949 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
950 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
951 const int ErrorDpcCounterInitialize = 200;
953 ioaddr[0] = dev->base_addr;
954 /* rmem_end holds the second I/O address - fv */
955 ioaddr[1] = dev->rmem_end;
956 name = dev->name;
958 sb1000_wait_for_ready_clear(ioaddr, name);
959 sb1000_send_command(ioaddr, name, Command0);
960 sb1000_wait_for_ready(ioaddr, name);
961 sb1000_read_status(ioaddr, st);
962 if (st[1] & 0x10)
963 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
964 return;
969 * Linux interface functions
971 static int
972 sb1000_open(struct net_device *dev)
974 char *name;
975 int ioaddr[2], status;
976 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
977 const unsigned short FirmwareVersion[] = {0x01, 0x01};
979 ioaddr[0] = dev->base_addr;
980 /* rmem_end holds the second I/O address - fv */
981 ioaddr[1] = dev->rmem_end;
982 name = dev->name;
983 request_region(ioaddr[0], SB1000_IO_EXTENT, "sb1000");
984 request_region(ioaddr[1], SB1000_IO_EXTENT, "sb1000");
986 /* initialize sb1000 */
987 if ((status = sb1000_reset(ioaddr, name)))
988 return status;
989 nicedelay(200000);
990 if ((status = sb1000_check_CRC(ioaddr, name)))
991 return status;
993 /* initialize private data before board can catch interrupts */
994 lp->rx_skb[0] = NULL;
995 lp->rx_skb[1] = NULL;
996 lp->rx_skb[2] = NULL;
997 lp->rx_skb[3] = NULL;
998 lp->rx_dlen[0] = 0;
999 lp->rx_dlen[1] = 0;
1000 lp->rx_dlen[2] = 0;
1001 lp->rx_dlen[3] = 0;
1002 lp->rx_bytes = 0;
1003 lp->rx_frames = 0;
1004 lp->rx_error_count = 0;
1005 lp->rx_error_dpc_count = 0;
1006 lp->rx_session_id[0] = 0x50;
1007 lp->rx_session_id[0] = 0x48;
1008 lp->rx_session_id[0] = 0x44;
1009 lp->rx_session_id[0] = 0x42;
1010 lp->rx_frame_id[0] = 0;
1011 lp->rx_frame_id[1] = 0;
1012 lp->rx_frame_id[2] = 0;
1013 lp->rx_frame_id[3] = 0;
1014 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
1015 return -EAGAIN;
1018 if (sb1000_debug > 2)
1019 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
1021 /* Activate board and check firmware version */
1022 udelay(1000);
1023 if ((status = sb1000_activate(ioaddr, name)))
1024 return status;
1025 udelay(0);
1026 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
1027 return status;
1028 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
1029 printk(KERN_WARNING "%s: found firmware version %x.%02x "
1030 "(should be %x.%02x)\n", name, version[0], version[1],
1031 FirmwareVersion[0], FirmwareVersion[1]);
1033 dev->interrupt = 0;
1034 dev->tbusy = 0;
1035 dev->start = 1;
1037 MOD_INC_USE_COUNT;
1038 return 0; /* Always succeed */
1041 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1043 char* name;
1044 unsigned char version[2];
1045 short PID[4];
1046 int ioaddr[2], status, frequency;
1047 unsigned int stats[5];
1048 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1050 if (!(dev && dev->flags & IFF_UP))
1051 return -ENODEV;
1053 ioaddr[0] = dev->base_addr;
1054 /* rmem_end holds the second I/O address - fv */
1055 ioaddr[1] = dev->rmem_end;
1056 name = dev->name;
1058 switch (cmd) {
1059 case SIOCGCMSTATS: /* get statistics */
1060 stats[0] = lp->rx_bytes;
1061 stats[1] = lp->rx_frames;
1062 stats[2] = lp->stats.rx_packets;
1063 stats[3] = lp->stats.rx_errors;
1064 stats[4] = lp->stats.rx_dropped;
1065 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1066 return -EFAULT;
1067 status = 0;
1068 break;
1070 case SIOCGCMFIRMWARE: /* get firmware version */
1071 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1072 return status;
1073 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1074 return -EFAULT;
1075 break;
1077 case SIOCGCMFREQUENCY: /* get frequency */
1078 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1079 return status;
1080 if(put_user(frequency, (int*) ifr->ifr_data))
1081 return -EFAULT;
1082 break;
1084 case SIOCSCMFREQUENCY: /* set frequency */
1085 if (!suser())
1086 return -EPERM;
1087 if(get_user(frequency, (int*) ifr->ifr_data))
1088 return -EFAULT;
1089 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1090 return status;
1091 break;
1093 case SIOCGCMPIDS: /* get PIDs */
1094 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1095 return status;
1096 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1097 return -EFAULT;
1098 break;
1100 case SIOCSCMPIDS: /* set PIDs */
1101 if (!suser())
1102 return -EPERM;
1103 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1104 return -EFAULT;
1105 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1106 return status;
1107 /* set session_id, frame_id and pkt_type too */
1108 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1109 lp->rx_session_id[1] = 0x48;
1110 lp->rx_session_id[2] = 0x44;
1111 lp->rx_session_id[3] = 0x42;
1112 lp->rx_frame_id[0] = 0;
1113 lp->rx_frame_id[1] = 0;
1114 lp->rx_frame_id[2] = 0;
1115 lp->rx_frame_id[3] = 0;
1116 break;
1118 default:
1119 status = -EINVAL;
1120 break;
1122 return status;
1125 /* transmit function: do nothing since SB1000 can't send anything out */
1126 static int
1127 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1129 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1130 /* sb1000 can't xmit datagrams */
1131 dev_kfree_skb(skb);
1132 return 0;
1135 /* SB1000 interrupt handler. */
1136 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1138 char *name;
1139 unsigned char st;
1140 int ioaddr[2];
1141 struct net_device *dev = (struct net_device *) dev_id;
1142 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1144 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1145 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1146 const int MaxRxErrorCount = 6;
1148 if (dev == NULL) {
1149 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1150 irq);
1151 return;
1153 if (dev->interrupt)
1154 printk(KERN_ERR "%s: Re-entering the interrupt handler.\n",
1155 dev->name);
1156 dev->interrupt = 1;
1158 ioaddr[0] = dev->base_addr;
1159 /* rmem_end holds the second I/O address - fv */
1160 ioaddr[1] = dev->rmem_end;
1161 name = dev->name;
1163 /* is it a good interrupt? */
1164 st = inb(ioaddr[1] + 6);
1165 if (!(st & 0x08 && st & 0x20)) {
1166 dev->interrupt = 0;
1167 return;
1170 if (sb1000_debug > 3)
1171 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1173 st = inb(ioaddr[0] + 7);
1174 if (sb1000_rx(dev))
1175 lp->rx_error_count++;
1176 #ifdef SB1000_DELAY
1177 udelay(SB1000_DELAY);
1178 #endif /* SB1000_DELAY */
1179 sb1000_issue_read_command(ioaddr, name);
1180 if (st & 0x01) {
1181 sb1000_error_dpc(dev);
1182 sb1000_issue_read_command(ioaddr, name);
1184 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1185 sb1000_wait_for_ready_clear(ioaddr, name);
1186 sb1000_send_command(ioaddr, name, Command0);
1187 sb1000_wait_for_ready(ioaddr, name);
1188 sb1000_issue_read_command(ioaddr, name);
1190 if (lp->rx_error_count >= MaxRxErrorCount) {
1191 sb1000_wait_for_ready_clear(ioaddr, name);
1192 sb1000_send_command(ioaddr, name, Command1);
1193 sb1000_wait_for_ready(ioaddr, name);
1194 sb1000_issue_read_command(ioaddr, name);
1195 lp->rx_error_count = 0;
1198 dev->interrupt = 0;
1199 return;
1202 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1204 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1205 return &lp->stats;
1208 static int sb1000_close(struct net_device *dev)
1210 int i;
1211 int ioaddr[2];
1212 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1214 if (sb1000_debug > 2)
1215 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1217 dev->tbusy = 1;
1218 dev->start = 0;
1220 ioaddr[0] = dev->base_addr;
1221 /* rmem_end holds the second I/O address - fv */
1222 ioaddr[1] = dev->rmem_end;
1224 free_irq(dev->irq, dev);
1225 /* If we don't do this, we can't re-insmod it later. */
1226 release_region(ioaddr[1], SB1000_IO_EXTENT);
1227 release_region(ioaddr[0], SB1000_IO_EXTENT);
1229 /* free rx_skb's if needed */
1230 for (i=0; i<4; i++) {
1231 if (lp->rx_skb[i]) {
1232 dev_kfree_skb(lp->rx_skb[i]);
1235 MOD_DEC_USE_COUNT;
1236 return 0;
1239 #ifdef MODULE
1240 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1241 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1242 MODULE_PARM(io, "1-2i");
1243 MODULE_PARM(irq, "i");
1245 static char devname[8] = {0, };
1246 static struct net_device dev_sb1000 = {
1247 devname,
1248 0, 0, 0, 0,
1249 0, 0,
1250 0, 0, 0, NULL, sb1000_probe };
1252 static int io[2] = {0, 0};
1253 static int irq = 0;
1256 init_module(void)
1258 int i;
1259 for (i = 0; i < 100; i++) {
1260 sprintf(devname, "cm%d", i);
1261 if (dev_get(devname) == 0) break;
1263 if (i == 100) {
1264 printk(KERN_ERR "sb1000: can't register any device cm<n>\n");
1265 return -ENFILE;
1267 dev_sb1000.base_addr = io[0];
1268 /* rmem_end holds the second I/O address - fv */
1269 dev_sb1000.rmem_end = io[1];
1270 dev_sb1000.irq = irq;
1271 if (register_netdev(&dev_sb1000) != 0) {
1272 printk(KERN_ERR "sb1000: failed to register device (io: %03x,%03x "
1273 "irq: %d)\n", io[0], io[1], irq);
1274 return -EIO;
1276 return 0;
1279 void cleanup_module(void)
1281 unregister_netdev(&dev_sb1000);
1282 kfree_s(dev_sb1000.priv, sizeof(struct sb1000_private));
1283 dev_sb1000.priv = NULL;
1285 #endif /* MODULE */
1288 * Local variables:
1289 * compile-command: "gcc -D__KERNEL__ -DMODULE -Wall -Wstrict-prototypes -O -m486 -c sb1000.c"
1290 * version-control: t
1291 * tab-width: 4
1292 * c-basic-offset: 4
1293 * End: