1 /* sb1000.c: A General Instruments SB1000 driver for linux. */
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.
22 981115 Steven Hirsch <shirsch@adelphia.net>
24 Linus changed the timer interface. Should work on all recent
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>
47 #include <linux/slab.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>
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>
66 int sb1000_debug = SB1000_DEBUG;
71 static const int SB1000_IO_EXTENT = 8;
72 /* SB1000 Maximum Receive Unit */
73 static const int SB1000_MRU = 1500; /* octects */
76 struct sb1000_private {
77 struct sk_buff *rx_skb[NPIDS];
79 unsigned int rx_frames;
81 short rx_error_dpc_count;
82 unsigned char rx_session_id[NPIDS];
83 unsigned char rx_frame_id[NPIDS];
84 unsigned char rx_pkt_type[NPIDS];
85 struct net_device_stats stats;
88 /* prototypes for Linux interface */
89 extern int sb1000_probe(struct net_device *dev);
90 static int sb1000_open(struct net_device *dev);
91 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
92 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
93 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
94 static struct net_device_stats *sb1000_stats(struct net_device *dev);
95 static int sb1000_close(struct net_device *dev);
98 /* SB1000 hardware routines to be used during open/configuration phases */
99 static inline void nicedelay(unsigned long usecs);
100 static inline int card_wait_for_busy_clear(const int ioaddr[],
102 static inline int card_wait_for_ready(const int ioaddr[], const char* name,
104 static inline int card_send_command(const int ioaddr[], const char* name,
105 const unsigned char out[], unsigned char in[]);
107 /* SB1000 hardware routines to be used during frame rx interrupt */
108 static inline int sb1000_wait_for_ready(const int ioaddr[], const char* name);
109 static inline int sb1000_wait_for_ready_clear(const int ioaddr[],
111 static inline void sb1000_send_command(const int ioaddr[], const char* name,
112 const unsigned char out[]);
113 static inline void sb1000_read_status(const int ioaddr[], unsigned char in[]);
114 static inline void sb1000_issue_read_command(const int ioaddr[],
117 /* SB1000 commands for open/configuration */
118 static inline int sb1000_reset(const int ioaddr[], const char* name);
119 static inline int sb1000_check_CRC(const int ioaddr[], const char* name);
120 static inline int sb1000_start_get_set_command(const int ioaddr[],
122 static inline int sb1000_end_get_set_command(const int ioaddr[],
124 static inline int sb1000_activate(const int ioaddr[], const char* name);
125 static inline int sb1000_get_firmware_version(const int ioaddr[],
126 const char* name, unsigned char version[], int do_end);
127 static inline int sb1000_get_frequency(const int ioaddr[], const char* name,
129 static inline int sb1000_set_frequency(const int ioaddr[], const char* name,
131 static inline int sb1000_get_PIDs(const int ioaddr[], const char* name,
133 static inline int sb1000_set_PIDs(const int ioaddr[], const char* name,
136 /* SB1000 commands for frame rx interrupt */
137 static inline int sb1000_rx(struct net_device *dev);
138 static inline void sb1000_error_dpc(struct net_device *dev);
140 static struct isapnp_device_id id_table[] = {
141 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
142 ISAPNP_VENDOR('G','I','C'), ISAPNP_FUNCTION(0x1000), 0 },
146 MODULE_DEVICE_TABLE(isapnp, id_table);
148 /* probe for SB1000 using Plug-n-Play mechanism */
150 sb1000_probe(struct net_device *dev)
153 unsigned short ioaddr[2], irq;
154 struct pci_dev *idev=NULL;
155 unsigned int serial_number;
163 idev=isapnp_find_dev(NULL, ISAPNP_VENDOR('G','I','C'),
164 ISAPNP_FUNCTION(0x1000), idev);
178 idev->activate(idev);
184 if(!idev->resource[0].start || check_region(idev->resource[0].start, 16))
186 if(!idev->resource[1].start || check_region(idev->resource[1].start, 16))
189 serial_number = idev->bus->serial;
191 ioaddr[0]=idev->resource[0].start;
192 ioaddr[1]=idev->resource[1].start;
194 irq = idev->irq_resource[0].start;
196 /* check I/O base and IRQ */
197 if (dev->base_addr != 0 && dev->base_addr != ioaddr[0])
199 if (dev->rmem_end != 0 && dev->rmem_end != ioaddr[1])
201 if (dev->irq != 0 && dev->irq != irq)
207 if (!request_region(ioaddr[0], 16, dev->name))
209 if (!request_region(ioaddr[1], 16, dev->name)) {
210 release_region(ioaddr[0], 16);
214 dev->base_addr = ioaddr[0];
215 /* rmem_end holds the second I/O address - fv */
216 dev->rmem_end = ioaddr[1];
219 if (sb1000_debug > 0)
220 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
221 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
222 dev->rmem_end, serial_number, dev->irq);
224 dev = init_etherdev(dev, 0);
227 SET_MODULE_OWNER(dev);
229 /* Make up a SB1000-specific-data structure. */
230 dev->priv = kmalloc(sizeof(struct sb1000_private), GFP_KERNEL);
231 if (dev->priv == NULL)
233 memset(dev->priv, 0, sizeof(struct sb1000_private));
235 if (sb1000_debug > 0)
236 printk(KERN_NOTICE "%s", version);
238 /* The SB1000-specific entries in the device structure. */
239 dev->open = sb1000_open;
240 dev->do_ioctl = sb1000_dev_ioctl;
241 dev->hard_start_xmit = sb1000_start_xmit;
242 dev->stop = sb1000_close;
243 dev->get_stats = sb1000_stats;
245 /* Fill in the generic fields of the device structure. */
246 dev->change_mtu = NULL;
247 dev->hard_header = NULL;
248 dev->rebuild_header = NULL;
249 dev->set_mac_address = NULL;
250 dev->header_cache_update= NULL;
252 dev->type = ARPHRD_ETHER;
253 dev->hard_header_len = 0;
255 dev->addr_len = ETH_ALEN;
256 /* hardware address is 0:0:serial_number */
257 dev->dev_addr[0] = 0;
258 dev->dev_addr[1] = 0;
259 dev->dev_addr[2] = serial_number >> 24 & 0xff;
260 dev->dev_addr[3] = serial_number >> 16 & 0xff;
261 dev->dev_addr[4] = serial_number >> 8 & 0xff;
262 dev->dev_addr[5] = serial_number >> 0 & 0xff;
263 dev->tx_queue_len = 0;
265 /* New-style flags. */
266 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
276 * SB1000 hardware routines to be used during open/configuration phases
279 const int TimeOutJiffies = (875 * HZ) / 100;
281 static inline void nicedelay(unsigned long usecs)
283 current->state = TASK_INTERRUPTIBLE;
284 schedule_timeout(HZ);
288 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
290 card_wait_for_busy_clear(const int ioaddr[], const char* name)
293 unsigned long timeout;
295 a = inb(ioaddr[0] + 7);
296 timeout = jiffies + TimeOutJiffies;
297 while (a & 0x80 || a & 0x40) {
299 current->state = TASK_INTERRUPTIBLE;
301 a = inb(ioaddr[0] + 7);
302 if (time_after_eq(jiffies, timeout)) {
303 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
312 /* Card Wait For Ready (cannot be used during an interrupt) */
314 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
317 unsigned long timeout;
319 a = inb(ioaddr[1] + 6);
320 timeout = jiffies + TimeOutJiffies;
321 while (a & 0x80 || !(a & 0x40)) {
323 current->state = TASK_INTERRUPTIBLE;
325 a = inb(ioaddr[1] + 6);
326 if (time_after_eq(jiffies, timeout)) {
327 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
333 in[1] = inb(ioaddr[0] + 1);
334 in[2] = inb(ioaddr[0] + 2);
335 in[3] = inb(ioaddr[0] + 3);
336 in[4] = inb(ioaddr[0] + 4);
337 in[0] = inb(ioaddr[0] + 5);
338 in[6] = inb(ioaddr[0] + 6);
339 in[5] = inb(ioaddr[1] + 6);
343 /* Card Send Command (cannot be used during an interrupt) */
345 card_send_command(const int ioaddr[], const char* name,
346 const unsigned char out[], unsigned char in[])
350 if ((status = card_wait_for_busy_clear(ioaddr, name)))
352 outb(0xa0, ioaddr[0] + 6);
353 outb(out[2], ioaddr[0] + 1);
354 outb(out[3], ioaddr[0] + 2);
355 outb(out[4], ioaddr[0] + 3);
356 outb(out[5], ioaddr[0] + 4);
357 outb(out[1], ioaddr[0] + 5);
358 outb(0xa0, ioaddr[0] + 6);
359 outb(out[0], ioaddr[0] + 7);
360 if (out[0] != 0x20 && out[0] != 0x30) {
361 if ((status = card_wait_for_ready(ioaddr, name, in)))
364 if (sb1000_debug > 3)
365 printk(KERN_DEBUG "%s: card_send_command "
366 "out: %02x%02x%02x%02x%02x%02x "
367 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
368 out[0], out[1], out[2], out[3], out[4], out[5],
369 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
371 if (sb1000_debug > 3)
372 printk(KERN_DEBUG "%s: card_send_command "
373 "out: %02x%02x%02x%02x%02x%02x\n", name,
374 out[0], out[1], out[2], out[3], out[4], out[5]);
377 if (out[1] == 0x1b) {
378 x = (out[2] == 0x02);
380 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
388 * SB1000 hardware routines to be used during frame rx interrupt
390 const int Sb1000TimeOutJiffies = 7 * HZ;
392 /* Card Wait For Ready (to be used during frame rx) */
394 sb1000_wait_for_ready(const int ioaddr[], const char* name)
396 unsigned long timeout;
398 timeout = jiffies + Sb1000TimeOutJiffies;
399 while (inb(ioaddr[1] + 6) & 0x80) {
400 if (time_after_eq(jiffies, timeout)) {
401 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
406 timeout = jiffies + Sb1000TimeOutJiffies;
407 while (!(inb(ioaddr[1] + 6) & 0x40)) {
408 if (time_after_eq(jiffies, timeout)) {
409 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
418 /* Card Wait For Ready Clear (to be used during frame rx) */
420 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
422 unsigned long timeout;
424 timeout = jiffies + Sb1000TimeOutJiffies;
425 while (inb(ioaddr[1] + 6) & 0x80) {
426 if (time_after_eq(jiffies, timeout)) {
427 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
432 timeout = jiffies + Sb1000TimeOutJiffies;
433 while (inb(ioaddr[1] + 6) & 0x40) {
434 if (time_after_eq(jiffies, timeout)) {
435 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
443 /* Card Send Command (to be used during frame rx) */
445 sb1000_send_command(const int ioaddr[], const char* name,
446 const unsigned char out[])
448 outb(out[2], ioaddr[0] + 1);
449 outb(out[3], ioaddr[0] + 2);
450 outb(out[4], ioaddr[0] + 3);
451 outb(out[5], ioaddr[0] + 4);
452 outb(out[1], ioaddr[0] + 5);
453 outb(out[0], ioaddr[0] + 7);
454 if (sb1000_debug > 3)
455 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
456 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
460 /* Card Read Status (to be used during frame rx) */
462 sb1000_read_status(const int ioaddr[], unsigned char in[])
464 in[1] = inb(ioaddr[0] + 1);
465 in[2] = inb(ioaddr[0] + 2);
466 in[3] = inb(ioaddr[0] + 3);
467 in[4] = inb(ioaddr[0] + 4);
468 in[0] = inb(ioaddr[0] + 5);
472 /* Issue Read Command (to be used during frame rx) */
474 sb1000_issue_read_command(const int ioaddr[], const char* name)
476 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
478 sb1000_wait_for_ready_clear(ioaddr, name);
479 outb(0xa0, ioaddr[0] + 6);
480 sb1000_send_command(ioaddr, name, Command0);
486 * SB1000 commands for open/configuration
488 /* reset SB1000 card */
490 sb1000_reset(const int ioaddr[], const char* name)
494 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
496 port = ioaddr[1] + 6;
510 if ((status = card_send_command(ioaddr, name, Command0, st)))
517 /* check SB1000 firmware CRC */
519 sb1000_check_CRC(const int ioaddr[], const char* name)
523 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
526 if ((status = card_send_command(ioaddr, name, Command0, st)))
528 if (st[1] != st[3] || st[2] != st[4])
530 crc = st[1] << 8 | st[2];
535 sb1000_start_get_set_command(const int ioaddr[], const char* name)
538 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
540 return card_send_command(ioaddr, name, Command0, st);
544 sb1000_end_get_set_command(const int ioaddr[], const char* name)
548 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
549 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
551 if ((status = card_send_command(ioaddr, name, Command0, st)))
553 return card_send_command(ioaddr, name, Command1, st);
557 sb1000_activate(const int ioaddr[], const char* name)
561 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
562 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
565 if ((status = card_send_command(ioaddr, name, Command0, st)))
567 if ((status = card_send_command(ioaddr, name, Command1, st)))
570 if ((status = sb1000_start_get_set_command(ioaddr, name)))
575 return sb1000_start_get_set_command(ioaddr, name);
578 /* get SB1000 firmware version */
580 sb1000_get_firmware_version(const int ioaddr[], const char* name,
581 unsigned char version[], int do_end)
585 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
587 if ((status = sb1000_start_get_set_command(ioaddr, name)))
589 if ((status = card_send_command(ioaddr, name, Command0, st)))
596 return sb1000_end_get_set_command(ioaddr, name);
601 /* get SB1000 frequency */
603 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
607 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
610 if ((status = sb1000_start_get_set_command(ioaddr, name)))
612 if ((status = card_send_command(ioaddr, name, Command0, st)))
614 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
615 return sb1000_end_get_set_command(ioaddr, name);
618 /* set SB1000 frequency */
620 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
624 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
626 const int FrequencyLowerLimit = 57000;
627 const int FrequencyUpperLimit = 804000;
629 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
630 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
631 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
632 FrequencyUpperLimit);
636 if ((status = sb1000_start_get_set_command(ioaddr, name)))
638 Command0[5] = frequency & 0xff;
640 Command0[4] = frequency & 0xff;
642 Command0[3] = frequency & 0xff;
644 Command0[2] = frequency & 0xff;
645 return card_send_command(ioaddr, name, Command0, st);
648 /* get SB1000 PIDs */
650 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
654 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
655 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
656 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
657 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
660 if ((status = sb1000_start_get_set_command(ioaddr, name)))
663 if ((status = card_send_command(ioaddr, name, Command0, st)))
665 PID[0] = st[1] << 8 | st[2];
667 if ((status = card_send_command(ioaddr, name, Command1, st)))
669 PID[1] = st[1] << 8 | st[2];
671 if ((status = card_send_command(ioaddr, name, Command2, st)))
673 PID[2] = st[1] << 8 | st[2];
675 if ((status = card_send_command(ioaddr, name, Command3, st)))
677 PID[3] = st[1] << 8 | st[2];
679 return sb1000_end_get_set_command(ioaddr, name);
682 /* set SB1000 PIDs */
684 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
689 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
690 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
691 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
692 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
693 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
696 if ((status = sb1000_start_get_set_command(ioaddr, name)))
700 Command0[3] = p & 0xff;
702 Command0[2] = p & 0xff;
703 if ((status = card_send_command(ioaddr, name, Command0, st)))
707 Command1[3] = p & 0xff;
709 Command1[2] = p & 0xff;
710 if ((status = card_send_command(ioaddr, name, Command1, st)))
714 Command2[3] = p & 0xff;
716 Command2[2] = p & 0xff;
717 if ((status = card_send_command(ioaddr, name, Command2, st)))
721 Command3[3] = p & 0xff;
723 Command3[2] = p & 0xff;
724 if ((status = card_send_command(ioaddr, name, Command3, st)))
727 if ((status = card_send_command(ioaddr, name, Command4, st)))
729 return sb1000_end_get_set_command(ioaddr, name);
734 sb1000_print_status_buffer(const char* name, unsigned char st[],
735 unsigned char buffer[], int size)
739 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
740 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
741 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
742 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
743 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
744 buffer[46] << 8 | buffer[47],
745 buffer[42], buffer[43], buffer[44], buffer[45],
746 buffer[48] << 8 | buffer[49]);
748 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
749 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
750 for (j = 0; j < 8 && k < size; j++, k++)
751 printk(" %02x", buffer[k]);
759 * SB1000 commands for frame rx interrupt
761 /* receive a single frame and assemble datagram
762 * (this is the heart of the interrupt routine)
765 sb1000_rx(struct net_device *dev)
768 #define FRAMESIZE 184
769 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
772 unsigned int skbsize;
774 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
775 struct net_device_stats *stats = &lp->stats;
777 /* SB1000 frame constants */
778 const int FrameSize = FRAMESIZE;
779 const int NewDatagramHeaderSkip = 8;
780 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
781 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
782 const int ContDatagramHeaderSkip = 7;
783 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
784 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
785 const int TrailerSize = 4;
787 ioaddr = dev->base_addr;
789 insw(ioaddr, (unsigned short*) st, 1);
791 printk("cm0: received: %02x %02x\n", st[0], st[1]);
792 #endif /* XXXDEBUG */
795 /* decide if it is a good or bad frame */
796 for (ns = 0; ns < NPIDS; ns++) {
797 session_id = lp->rx_session_id[ns];
798 frame_id = lp->rx_frame_id[ns];
799 if (st[0] == session_id) {
800 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
802 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
807 } else if (st[0] == (session_id | 0x40)) {
808 if ((st[1] & 0xf0) == 0x30) {
818 stats->rx_frame_errors++;
819 skb = lp->rx_skb[ns];
820 if (sb1000_debug > 1)
821 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
822 "expecting %02x %02x\n", dev->name, st[0], st[1],
823 skb ? session_id : session_id | 0x40, frame_id);
830 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
833 /* get data length */
834 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
836 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
837 #endif /* XXXDEBUG */
838 if (buffer[0] != NewDatagramHeaderSkip) {
839 if (sb1000_debug > 1)
840 printk(KERN_WARNING "%s: new datagram header skip error: "
841 "got %02x expecting %02x\n", dev->name, buffer[0],
842 NewDatagramHeaderSkip);
843 stats->rx_length_errors++;
844 insw(ioaddr, buffer, NewDatagramDataSize / 2);
847 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
848 buffer[NewDatagramHeaderSkip + 4]) - 17;
849 if (dlen > SB1000_MRU) {
850 if (sb1000_debug > 1)
851 printk(KERN_WARNING "%s: datagram length (%d) greater "
852 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
853 stats->rx_length_errors++;
854 insw(ioaddr, buffer, NewDatagramDataSize / 2);
857 lp->rx_dlen[ns] = dlen;
858 /* compute size to allocate for datagram */
859 skbsize = dlen + FrameSize;
860 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
861 if (sb1000_debug > 1)
862 printk(KERN_WARNING "%s: can't allocate %d bytes long "
863 "skbuff\n", dev->name, skbsize);
865 insw(ioaddr, buffer, NewDatagramDataSize / 2);
869 skb->mac.raw = skb->data;
870 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
871 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
872 NewDatagramDataSize / 2);
873 lp->rx_skb[ns] = skb;
875 /* continuation of previous datagram */
876 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
877 if (buffer[0] != ContDatagramHeaderSkip) {
878 if (sb1000_debug > 1)
879 printk(KERN_WARNING "%s: cont datagram header skip error: "
880 "got %02x expecting %02x\n", dev->name, buffer[0],
881 ContDatagramHeaderSkip);
882 stats->rx_length_errors++;
883 insw(ioaddr, buffer, ContDatagramDataSize / 2);
886 skb = lp->rx_skb[ns];
887 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
888 ContDatagramDataSize / 2);
889 dlen = lp->rx_dlen[ns];
891 if (skb->len < dlen + TrailerSize) {
892 lp->rx_session_id[ns] &= ~0x40;
896 /* datagram completed: send to upper level */
899 dev->last_rx = jiffies;
900 stats->rx_bytes+=dlen;
903 lp->rx_session_id[ns] |= 0x40;
907 insw(ioaddr, buffer, FrameSize / 2);
908 if (sb1000_debug > 1)
909 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
910 dev->name, st[0], st[1]);
911 stats->rx_frame_errors++;
913 if (sb1000_debug > 2)
914 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
918 if ((skb = lp->rx_skb[ns])) {
922 lp->rx_session_id[ns] |= 0x40;
928 sb1000_error_dpc(struct net_device *dev)
933 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
934 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
935 const int ErrorDpcCounterInitialize = 200;
937 ioaddr[0] = dev->base_addr;
938 /* rmem_end holds the second I/O address - fv */
939 ioaddr[1] = dev->rmem_end;
942 sb1000_wait_for_ready_clear(ioaddr, name);
943 sb1000_send_command(ioaddr, name, Command0);
944 sb1000_wait_for_ready(ioaddr, name);
945 sb1000_read_status(ioaddr, st);
947 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
953 * Linux interface functions
956 sb1000_open(struct net_device *dev)
959 int ioaddr[2], status;
960 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
961 const unsigned short FirmwareVersion[] = {0x01, 0x01};
963 ioaddr[0] = dev->base_addr;
964 /* rmem_end holds the second I/O address - fv */
965 ioaddr[1] = dev->rmem_end;
968 /* initialize sb1000 */
969 if ((status = sb1000_reset(ioaddr, name)))
972 if ((status = sb1000_check_CRC(ioaddr, name)))
975 /* initialize private data before board can catch interrupts */
976 lp->rx_skb[0] = NULL;
977 lp->rx_skb[1] = NULL;
978 lp->rx_skb[2] = NULL;
979 lp->rx_skb[3] = NULL;
985 lp->rx_error_count = 0;
986 lp->rx_error_dpc_count = 0;
987 lp->rx_session_id[0] = 0x50;
988 lp->rx_session_id[0] = 0x48;
989 lp->rx_session_id[0] = 0x44;
990 lp->rx_session_id[0] = 0x42;
991 lp->rx_frame_id[0] = 0;
992 lp->rx_frame_id[1] = 0;
993 lp->rx_frame_id[2] = 0;
994 lp->rx_frame_id[3] = 0;
995 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
999 if (sb1000_debug > 2)
1000 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
1002 /* Activate board and check firmware version */
1004 if ((status = sb1000_activate(ioaddr, name)))
1007 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
1009 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
1010 printk(KERN_WARNING "%s: found firmware version %x.%02x "
1011 "(should be %x.%02x)\n", name, version[0], version[1],
1012 FirmwareVersion[0], FirmwareVersion[1]);
1015 netif_start_queue(dev);
1016 return 0; /* Always succeed */
1019 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1022 unsigned char version[2];
1024 int ioaddr[2], status, frequency;
1025 unsigned int stats[5];
1026 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1028 if (!(dev && dev->flags & IFF_UP))
1031 ioaddr[0] = dev->base_addr;
1032 /* rmem_end holds the second I/O address - fv */
1033 ioaddr[1] = dev->rmem_end;
1037 case SIOCGCMSTATS: /* get statistics */
1038 stats[0] = lp->stats.rx_bytes;
1039 stats[1] = lp->rx_frames;
1040 stats[2] = lp->stats.rx_packets;
1041 stats[3] = lp->stats.rx_errors;
1042 stats[4] = lp->stats.rx_dropped;
1043 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1048 case SIOCGCMFIRMWARE: /* get firmware version */
1049 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1051 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1055 case SIOCGCMFREQUENCY: /* get frequency */
1056 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1058 if(put_user(frequency, (int*) ifr->ifr_data))
1062 case SIOCSCMFREQUENCY: /* set frequency */
1063 if (!capable(CAP_NET_ADMIN))
1065 if(get_user(frequency, (int*) ifr->ifr_data))
1067 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1071 case SIOCGCMPIDS: /* get PIDs */
1072 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1074 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1078 case SIOCSCMPIDS: /* set PIDs */
1079 if (!capable(CAP_NET_ADMIN))
1081 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1083 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1085 /* set session_id, frame_id and pkt_type too */
1086 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1087 lp->rx_session_id[1] = 0x48;
1088 lp->rx_session_id[2] = 0x44;
1089 lp->rx_session_id[3] = 0x42;
1090 lp->rx_frame_id[0] = 0;
1091 lp->rx_frame_id[1] = 0;
1092 lp->rx_frame_id[2] = 0;
1093 lp->rx_frame_id[3] = 0;
1103 /* transmit function: do nothing since SB1000 can't send anything out */
1105 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1107 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1108 /* sb1000 can't xmit datagrams */
1113 /* SB1000 interrupt handler. */
1114 static void sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1119 struct net_device *dev = (struct net_device *) dev_id;
1120 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1122 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1123 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1124 const int MaxRxErrorCount = 6;
1127 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1132 ioaddr[0] = dev->base_addr;
1133 /* rmem_end holds the second I/O address - fv */
1134 ioaddr[1] = dev->rmem_end;
1137 /* is it a good interrupt? */
1138 st = inb(ioaddr[1] + 6);
1139 if (!(st & 0x08 && st & 0x20)) {
1143 if (sb1000_debug > 3)
1144 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1146 st = inb(ioaddr[0] + 7);
1148 lp->rx_error_count++;
1150 udelay(SB1000_DELAY);
1151 #endif /* SB1000_DELAY */
1152 sb1000_issue_read_command(ioaddr, name);
1154 sb1000_error_dpc(dev);
1155 sb1000_issue_read_command(ioaddr, name);
1157 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1158 sb1000_wait_for_ready_clear(ioaddr, name);
1159 sb1000_send_command(ioaddr, name, Command0);
1160 sb1000_wait_for_ready(ioaddr, name);
1161 sb1000_issue_read_command(ioaddr, name);
1163 if (lp->rx_error_count >= MaxRxErrorCount) {
1164 sb1000_wait_for_ready_clear(ioaddr, name);
1165 sb1000_send_command(ioaddr, name, Command1);
1166 sb1000_wait_for_ready(ioaddr, name);
1167 sb1000_issue_read_command(ioaddr, name);
1168 lp->rx_error_count = 0;
1174 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1176 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1180 static int sb1000_close(struct net_device *dev)
1184 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1186 if (sb1000_debug > 2)
1187 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1189 netif_stop_queue(dev);
1191 ioaddr[0] = dev->base_addr;
1192 /* rmem_end holds the second I/O address - fv */
1193 ioaddr[1] = dev->rmem_end;
1195 free_irq(dev->irq, dev);
1196 /* If we don't do this, we can't re-insmod it later. */
1197 release_region(ioaddr[1], SB1000_IO_EXTENT);
1198 release_region(ioaddr[0], SB1000_IO_EXTENT);
1200 /* free rx_skb's if needed */
1201 for (i=0; i<4; i++) {
1202 if (lp->rx_skb[i]) {
1203 dev_kfree_skb(lp->rx_skb[i]);
1210 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1211 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1212 MODULE_LICENSE("GPL");
1214 MODULE_PARM(io, "1-2i");
1215 MODULE_PARM(irq, "i");
1216 MODULE_PARM_DESC(io, "SB1000 I/O base addresses");
1217 MODULE_PARM_DESC(irq, "SB1000 IRQ number");
1219 static struct net_device dev_sb1000;
1227 for (i = 0; i < 100; i++) {
1228 sprintf(dev_sb1000.name, "cm%d", i);
1229 if (dev_get(dev_sb1000.name) == 0) break;
1232 printk(KERN_ERR "sb1000: can't register any device cm<n>\n");
1235 dev_sb1000.init = sb1000_probe;
1236 dev_sb1000.base_addr = io[0];
1237 /* rmem_end holds the second I/O address - fv */
1238 dev_sb1000.rmem_end = io[1];
1239 dev_sb1000.irq = irq;
1240 if (register_netdev(&dev_sb1000) != 0) {
1241 printk(KERN_ERR "sb1000: failed to register device (io: %03x,%03x "
1242 "irq: %d)\n", io[0], io[1], irq);
1248 void cleanup_module(void)
1250 unregister_netdev(&dev_sb1000);
1251 release_region(dev_sb1000.base_addr, 16);
1252 release_region(dev_sb1000.rmem_end, 16);
1253 kfree(dev_sb1000.priv);
1254 dev_sb1000.priv = NULL;
1260 * compile-command: "gcc -D__KERNEL__ -DMODULE -Wall -Wstrict-prototypes -O -m486 -c sb1000.c"
1261 * version-control: t