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>
38 #include <linux/kernel.h>
39 #include <linux/string.h>
40 #include <linux/interrupt.h>
41 #include <linux/errno.h>
42 #include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
44 #include <linux/slab.h>
45 #include <linux/ioport.h>
46 #include <linux/netdevice.h>
47 #include <linux/if_arp.h>
48 #include <linux/skbuff.h>
49 #include <linux/delay.h> /* for udelay() */
50 #include <linux/etherdevice.h>
51 #include <linux/pnp.h>
53 #include <asm/bitops.h>
55 #include <asm/processor.h>
56 #include <asm/uaccess.h>
59 int sb1000_debug = SB1000_DEBUG;
64 static const int SB1000_IO_EXTENT = 8;
65 /* SB1000 Maximum Receive Unit */
66 static const int SB1000_MRU = 1500; /* octects */
69 struct sb1000_private {
70 struct sk_buff *rx_skb[NPIDS];
72 unsigned int rx_frames;
74 short rx_error_dpc_count;
75 unsigned char rx_session_id[NPIDS];
76 unsigned char rx_frame_id[NPIDS];
77 unsigned char rx_pkt_type[NPIDS];
78 struct net_device_stats stats;
81 /* prototypes for Linux interface */
82 extern int sb1000_probe(struct net_device *dev);
83 static int sb1000_open(struct net_device *dev);
84 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
85 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
86 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
87 static struct net_device_stats *sb1000_stats(struct net_device *dev);
88 static int sb1000_close(struct net_device *dev);
91 /* SB1000 hardware routines to be used during open/configuration phases */
92 static inline void nicedelay(unsigned long usecs);
93 static inline int card_wait_for_busy_clear(const int ioaddr[],
95 static inline int card_wait_for_ready(const int ioaddr[], const char* name,
97 static inline int card_send_command(const int ioaddr[], const char* name,
98 const unsigned char out[], unsigned char in[]);
100 /* SB1000 hardware routines to be used during frame rx interrupt */
101 static inline int sb1000_wait_for_ready(const int ioaddr[], const char* name);
102 static inline int sb1000_wait_for_ready_clear(const int ioaddr[],
104 static inline void sb1000_send_command(const int ioaddr[], const char* name,
105 const unsigned char out[]);
106 static inline void sb1000_read_status(const int ioaddr[], unsigned char in[]);
107 static inline void sb1000_issue_read_command(const int ioaddr[],
110 /* SB1000 commands for open/configuration */
111 static inline int sb1000_reset(const int ioaddr[], const char* name);
112 static inline int sb1000_check_CRC(const int ioaddr[], const char* name);
113 static inline int sb1000_start_get_set_command(const int ioaddr[],
115 static inline int sb1000_end_get_set_command(const int ioaddr[],
117 static inline int sb1000_activate(const int ioaddr[], const char* name);
118 static inline int sb1000_get_firmware_version(const int ioaddr[],
119 const char* name, unsigned char version[], int do_end);
120 static inline int sb1000_get_frequency(const int ioaddr[], const char* name,
122 static inline int sb1000_set_frequency(const int ioaddr[], const char* name,
124 static inline int sb1000_get_PIDs(const int ioaddr[], const char* name,
126 static inline int sb1000_set_PIDs(const int ioaddr[], const char* name,
129 /* SB1000 commands for frame rx interrupt */
130 static inline int sb1000_rx(struct net_device *dev);
131 static inline void sb1000_error_dpc(struct net_device *dev);
133 static const struct pnp_device_id sb1000_pnp_ids[] = {
137 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
140 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
142 struct net_device *dev;
143 unsigned short ioaddr[2], irq;
144 unsigned int serial_number;
147 if (pnp_device_attach(pdev) < 0)
149 if (pnp_activate_dev(pdev) < 0)
152 if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
154 if (!pnp_irq_valid(pdev, 0))
157 serial_number = pdev->card->serial;
159 ioaddr[0] = pnp_port_start(pdev, 0);
160 ioaddr[1] = pnp_port_start(pdev, 0);
162 irq = pnp_irq(pdev, 0);
164 if (!request_region(ioaddr[0], 16, "sb1000"))
166 if (!request_region(ioaddr[1], 16, "sb1000"))
167 goto out_release_region0;
169 dev = alloc_etherdev(sizeof(struct sb1000_private));
172 goto out_release_regions;
176 dev->base_addr = ioaddr[0];
177 /* mem_start holds the second I/O address */
178 dev->mem_start = ioaddr[1];
181 if (sb1000_debug > 0)
182 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
183 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
184 dev->mem_start, serial_number, dev->irq);
187 * The SB1000 is an rx-only cable modem device. The uplink is a modem
188 * and we do not want to arp on it.
190 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
192 SET_MODULE_OWNER(dev);
193 SET_NETDEV_DEV(dev, &pdev->dev);
195 if (sb1000_debug > 0)
196 printk(KERN_NOTICE "%s", version);
198 /* The SB1000-specific entries in the device structure. */
199 dev->open = sb1000_open;
200 dev->do_ioctl = sb1000_dev_ioctl;
201 dev->hard_start_xmit = sb1000_start_xmit;
202 dev->stop = sb1000_close;
203 dev->get_stats = sb1000_stats;
205 /* hardware address is 0:0:serial_number */
206 dev->dev_addr[2] = serial_number >> 24 & 0xff;
207 dev->dev_addr[3] = serial_number >> 16 & 0xff;
208 dev->dev_addr[4] = serial_number >> 8 & 0xff;
209 dev->dev_addr[5] = serial_number >> 0 & 0xff;
211 pnp_set_drvdata(pdev, dev);
213 error = register_netdev(dev);
215 goto out_free_netdev;
221 release_region(ioaddr[1], 16);
223 release_region(ioaddr[0], 16);
225 pnp_disable_dev(pdev);
227 pnp_device_detach(pdev);
232 sb1000_remove_one(struct pnp_dev *pdev)
234 struct net_device *dev = pnp_get_drvdata(pdev);
236 unregister_netdev(dev);
237 release_region(dev->base_addr, 16);
238 release_region(dev->mem_start, 16);
242 static struct pnp_driver sb1000_driver = {
244 .id_table = sb1000_pnp_ids,
245 .probe = sb1000_probe_one,
246 .remove = sb1000_remove_one,
251 * SB1000 hardware routines to be used during open/configuration phases
254 const int TimeOutJiffies = (875 * HZ) / 100;
256 static inline void nicedelay(unsigned long usecs)
258 current->state = TASK_INTERRUPTIBLE;
259 schedule_timeout(HZ);
263 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
265 card_wait_for_busy_clear(const int ioaddr[], const char* name)
268 unsigned long timeout;
270 a = inb(ioaddr[0] + 7);
271 timeout = jiffies + TimeOutJiffies;
272 while (a & 0x80 || a & 0x40) {
276 a = inb(ioaddr[0] + 7);
277 if (time_after_eq(jiffies, timeout)) {
278 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
287 /* Card Wait For Ready (cannot be used during an interrupt) */
289 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
292 unsigned long timeout;
294 a = inb(ioaddr[1] + 6);
295 timeout = jiffies + TimeOutJiffies;
296 while (a & 0x80 || !(a & 0x40)) {
300 a = inb(ioaddr[1] + 6);
301 if (time_after_eq(jiffies, timeout)) {
302 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
308 in[1] = inb(ioaddr[0] + 1);
309 in[2] = inb(ioaddr[0] + 2);
310 in[3] = inb(ioaddr[0] + 3);
311 in[4] = inb(ioaddr[0] + 4);
312 in[0] = inb(ioaddr[0] + 5);
313 in[6] = inb(ioaddr[0] + 6);
314 in[5] = inb(ioaddr[1] + 6);
318 /* Card Send Command (cannot be used during an interrupt) */
320 card_send_command(const int ioaddr[], const char* name,
321 const unsigned char out[], unsigned char in[])
325 if ((status = card_wait_for_busy_clear(ioaddr, name)))
327 outb(0xa0, ioaddr[0] + 6);
328 outb(out[2], ioaddr[0] + 1);
329 outb(out[3], ioaddr[0] + 2);
330 outb(out[4], ioaddr[0] + 3);
331 outb(out[5], ioaddr[0] + 4);
332 outb(out[1], ioaddr[0] + 5);
333 outb(0xa0, ioaddr[0] + 6);
334 outb(out[0], ioaddr[0] + 7);
335 if (out[0] != 0x20 && out[0] != 0x30) {
336 if ((status = card_wait_for_ready(ioaddr, name, in)))
339 if (sb1000_debug > 3)
340 printk(KERN_DEBUG "%s: card_send_command "
341 "out: %02x%02x%02x%02x%02x%02x "
342 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
343 out[0], out[1], out[2], out[3], out[4], out[5],
344 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
346 if (sb1000_debug > 3)
347 printk(KERN_DEBUG "%s: card_send_command "
348 "out: %02x%02x%02x%02x%02x%02x\n", name,
349 out[0], out[1], out[2], out[3], out[4], out[5]);
352 if (out[1] == 0x1b) {
353 x = (out[2] == 0x02);
355 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
363 * SB1000 hardware routines to be used during frame rx interrupt
365 const int Sb1000TimeOutJiffies = 7 * HZ;
367 /* Card Wait For Ready (to be used during frame rx) */
369 sb1000_wait_for_ready(const int ioaddr[], const char* name)
371 unsigned long timeout;
373 timeout = jiffies + Sb1000TimeOutJiffies;
374 while (inb(ioaddr[1] + 6) & 0x80) {
375 if (time_after_eq(jiffies, timeout)) {
376 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
381 timeout = jiffies + Sb1000TimeOutJiffies;
382 while (!(inb(ioaddr[1] + 6) & 0x40)) {
383 if (time_after_eq(jiffies, timeout)) {
384 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
393 /* Card Wait For Ready Clear (to be used during frame rx) */
395 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
397 unsigned long timeout;
399 timeout = jiffies + Sb1000TimeOutJiffies;
400 while (inb(ioaddr[1] + 6) & 0x80) {
401 if (time_after_eq(jiffies, timeout)) {
402 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
407 timeout = jiffies + Sb1000TimeOutJiffies;
408 while (inb(ioaddr[1] + 6) & 0x40) {
409 if (time_after_eq(jiffies, timeout)) {
410 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
418 /* Card Send Command (to be used during frame rx) */
420 sb1000_send_command(const int ioaddr[], const char* name,
421 const unsigned char out[])
423 outb(out[2], ioaddr[0] + 1);
424 outb(out[3], ioaddr[0] + 2);
425 outb(out[4], ioaddr[0] + 3);
426 outb(out[5], ioaddr[0] + 4);
427 outb(out[1], ioaddr[0] + 5);
428 outb(out[0], ioaddr[0] + 7);
429 if (sb1000_debug > 3)
430 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
431 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
435 /* Card Read Status (to be used during frame rx) */
437 sb1000_read_status(const int ioaddr[], unsigned char in[])
439 in[1] = inb(ioaddr[0] + 1);
440 in[2] = inb(ioaddr[0] + 2);
441 in[3] = inb(ioaddr[0] + 3);
442 in[4] = inb(ioaddr[0] + 4);
443 in[0] = inb(ioaddr[0] + 5);
447 /* Issue Read Command (to be used during frame rx) */
449 sb1000_issue_read_command(const int ioaddr[], const char* name)
451 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
453 sb1000_wait_for_ready_clear(ioaddr, name);
454 outb(0xa0, ioaddr[0] + 6);
455 sb1000_send_command(ioaddr, name, Command0);
461 * SB1000 commands for open/configuration
463 /* reset SB1000 card */
465 sb1000_reset(const int ioaddr[], const char* name)
469 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
471 port = ioaddr[1] + 6;
485 if ((status = card_send_command(ioaddr, name, Command0, st)))
492 /* check SB1000 firmware CRC */
494 sb1000_check_CRC(const int ioaddr[], const char* name)
498 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
501 if ((status = card_send_command(ioaddr, name, Command0, st)))
503 if (st[1] != st[3] || st[2] != st[4])
505 crc = st[1] << 8 | st[2];
510 sb1000_start_get_set_command(const int ioaddr[], const char* name)
513 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
515 return card_send_command(ioaddr, name, Command0, st);
519 sb1000_end_get_set_command(const int ioaddr[], const char* name)
523 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
524 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
526 if ((status = card_send_command(ioaddr, name, Command0, st)))
528 return card_send_command(ioaddr, name, Command1, st);
532 sb1000_activate(const int ioaddr[], const char* name)
536 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
537 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
540 if ((status = card_send_command(ioaddr, name, Command0, st)))
542 if ((status = card_send_command(ioaddr, name, Command1, st)))
545 if ((status = sb1000_start_get_set_command(ioaddr, name)))
550 return sb1000_start_get_set_command(ioaddr, name);
553 /* get SB1000 firmware version */
555 sb1000_get_firmware_version(const int ioaddr[], const char* name,
556 unsigned char version[], int do_end)
560 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
562 if ((status = sb1000_start_get_set_command(ioaddr, name)))
564 if ((status = card_send_command(ioaddr, name, Command0, st)))
571 return sb1000_end_get_set_command(ioaddr, name);
576 /* get SB1000 frequency */
578 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
582 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
585 if ((status = sb1000_start_get_set_command(ioaddr, name)))
587 if ((status = card_send_command(ioaddr, name, Command0, st)))
589 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
590 return sb1000_end_get_set_command(ioaddr, name);
593 /* set SB1000 frequency */
595 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
599 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
601 const int FrequencyLowerLimit = 57000;
602 const int FrequencyUpperLimit = 804000;
604 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
605 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
606 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
607 FrequencyUpperLimit);
611 if ((status = sb1000_start_get_set_command(ioaddr, name)))
613 Command0[5] = frequency & 0xff;
615 Command0[4] = frequency & 0xff;
617 Command0[3] = frequency & 0xff;
619 Command0[2] = frequency & 0xff;
620 return card_send_command(ioaddr, name, Command0, st);
623 /* get SB1000 PIDs */
625 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
629 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
630 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
631 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
632 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
635 if ((status = sb1000_start_get_set_command(ioaddr, name)))
638 if ((status = card_send_command(ioaddr, name, Command0, st)))
640 PID[0] = st[1] << 8 | st[2];
642 if ((status = card_send_command(ioaddr, name, Command1, st)))
644 PID[1] = st[1] << 8 | st[2];
646 if ((status = card_send_command(ioaddr, name, Command2, st)))
648 PID[2] = st[1] << 8 | st[2];
650 if ((status = card_send_command(ioaddr, name, Command3, st)))
652 PID[3] = st[1] << 8 | st[2];
654 return sb1000_end_get_set_command(ioaddr, name);
657 /* set SB1000 PIDs */
659 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
664 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
665 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
666 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
667 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
668 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
671 if ((status = sb1000_start_get_set_command(ioaddr, name)))
675 Command0[3] = p & 0xff;
677 Command0[2] = p & 0xff;
678 if ((status = card_send_command(ioaddr, name, Command0, st)))
682 Command1[3] = p & 0xff;
684 Command1[2] = p & 0xff;
685 if ((status = card_send_command(ioaddr, name, Command1, st)))
689 Command2[3] = p & 0xff;
691 Command2[2] = p & 0xff;
692 if ((status = card_send_command(ioaddr, name, Command2, st)))
696 Command3[3] = p & 0xff;
698 Command3[2] = p & 0xff;
699 if ((status = card_send_command(ioaddr, name, Command3, st)))
702 if ((status = card_send_command(ioaddr, name, Command4, st)))
704 return sb1000_end_get_set_command(ioaddr, name);
709 sb1000_print_status_buffer(const char* name, unsigned char st[],
710 unsigned char buffer[], int size)
714 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
715 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
716 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
717 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
718 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
719 buffer[46] << 8 | buffer[47],
720 buffer[42], buffer[43], buffer[44], buffer[45],
721 buffer[48] << 8 | buffer[49]);
723 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
724 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
725 for (j = 0; j < 8 && k < size; j++, k++)
726 printk(" %02x", buffer[k]);
734 * SB1000 commands for frame rx interrupt
736 /* receive a single frame and assemble datagram
737 * (this is the heart of the interrupt routine)
740 sb1000_rx(struct net_device *dev)
743 #define FRAMESIZE 184
744 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
747 unsigned int skbsize;
749 struct sb1000_private *lp = netdev_priv(dev);
750 struct net_device_stats *stats = &lp->stats;
752 /* SB1000 frame constants */
753 const int FrameSize = FRAMESIZE;
754 const int NewDatagramHeaderSkip = 8;
755 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
756 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
757 const int ContDatagramHeaderSkip = 7;
758 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
759 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
760 const int TrailerSize = 4;
762 ioaddr = dev->base_addr;
764 insw(ioaddr, (unsigned short*) st, 1);
766 printk("cm0: received: %02x %02x\n", st[0], st[1]);
767 #endif /* XXXDEBUG */
770 /* decide if it is a good or bad frame */
771 for (ns = 0; ns < NPIDS; ns++) {
772 session_id = lp->rx_session_id[ns];
773 frame_id = lp->rx_frame_id[ns];
774 if (st[0] == session_id) {
775 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
777 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
782 } else if (st[0] == (session_id | 0x40)) {
783 if ((st[1] & 0xf0) == 0x30) {
793 stats->rx_frame_errors++;
794 skb = lp->rx_skb[ns];
795 if (sb1000_debug > 1)
796 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
797 "expecting %02x %02x\n", dev->name, st[0], st[1],
798 skb ? session_id : session_id | 0x40, frame_id);
805 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
808 /* get data length */
809 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
811 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
812 #endif /* XXXDEBUG */
813 if (buffer[0] != NewDatagramHeaderSkip) {
814 if (sb1000_debug > 1)
815 printk(KERN_WARNING "%s: new datagram header skip error: "
816 "got %02x expecting %02x\n", dev->name, buffer[0],
817 NewDatagramHeaderSkip);
818 stats->rx_length_errors++;
819 insw(ioaddr, buffer, NewDatagramDataSize / 2);
822 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
823 buffer[NewDatagramHeaderSkip + 4]) - 17;
824 if (dlen > SB1000_MRU) {
825 if (sb1000_debug > 1)
826 printk(KERN_WARNING "%s: datagram length (%d) greater "
827 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
828 stats->rx_length_errors++;
829 insw(ioaddr, buffer, NewDatagramDataSize / 2);
832 lp->rx_dlen[ns] = dlen;
833 /* compute size to allocate for datagram */
834 skbsize = dlen + FrameSize;
835 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
836 if (sb1000_debug > 1)
837 printk(KERN_WARNING "%s: can't allocate %d bytes long "
838 "skbuff\n", dev->name, skbsize);
840 insw(ioaddr, buffer, NewDatagramDataSize / 2);
844 skb->mac.raw = skb->data;
845 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
846 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
847 NewDatagramDataSize / 2);
848 lp->rx_skb[ns] = skb;
850 /* continuation of previous datagram */
851 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
852 if (buffer[0] != ContDatagramHeaderSkip) {
853 if (sb1000_debug > 1)
854 printk(KERN_WARNING "%s: cont datagram header skip error: "
855 "got %02x expecting %02x\n", dev->name, buffer[0],
856 ContDatagramHeaderSkip);
857 stats->rx_length_errors++;
858 insw(ioaddr, buffer, ContDatagramDataSize / 2);
861 skb = lp->rx_skb[ns];
862 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
863 ContDatagramDataSize / 2);
864 dlen = lp->rx_dlen[ns];
866 if (skb->len < dlen + TrailerSize) {
867 lp->rx_session_id[ns] &= ~0x40;
871 /* datagram completed: send to upper level */
874 dev->last_rx = jiffies;
875 stats->rx_bytes+=dlen;
878 lp->rx_session_id[ns] |= 0x40;
882 insw(ioaddr, buffer, FrameSize / 2);
883 if (sb1000_debug > 1)
884 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
885 dev->name, st[0], st[1]);
886 stats->rx_frame_errors++;
888 if (sb1000_debug > 2)
889 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
893 if ((skb = lp->rx_skb[ns])) {
897 lp->rx_session_id[ns] |= 0x40;
903 sb1000_error_dpc(struct net_device *dev)
908 struct sb1000_private *lp = netdev_priv(dev);
909 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
910 const int ErrorDpcCounterInitialize = 200;
912 ioaddr[0] = dev->base_addr;
913 /* mem_start holds the second I/O address */
914 ioaddr[1] = dev->mem_start;
917 sb1000_wait_for_ready_clear(ioaddr, name);
918 sb1000_send_command(ioaddr, name, Command0);
919 sb1000_wait_for_ready(ioaddr, name);
920 sb1000_read_status(ioaddr, st);
922 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
928 * Linux interface functions
931 sb1000_open(struct net_device *dev)
934 int ioaddr[2], status;
935 struct sb1000_private *lp = netdev_priv(dev);
936 const unsigned short FirmwareVersion[] = {0x01, 0x01};
938 ioaddr[0] = dev->base_addr;
939 /* mem_start holds the second I/O address */
940 ioaddr[1] = dev->mem_start;
943 /* initialize sb1000 */
944 if ((status = sb1000_reset(ioaddr, name)))
947 if ((status = sb1000_check_CRC(ioaddr, name)))
950 /* initialize private data before board can catch interrupts */
951 lp->rx_skb[0] = NULL;
952 lp->rx_skb[1] = NULL;
953 lp->rx_skb[2] = NULL;
954 lp->rx_skb[3] = NULL;
960 lp->rx_error_count = 0;
961 lp->rx_error_dpc_count = 0;
962 lp->rx_session_id[0] = 0x50;
963 lp->rx_session_id[0] = 0x48;
964 lp->rx_session_id[0] = 0x44;
965 lp->rx_session_id[0] = 0x42;
966 lp->rx_frame_id[0] = 0;
967 lp->rx_frame_id[1] = 0;
968 lp->rx_frame_id[2] = 0;
969 lp->rx_frame_id[3] = 0;
970 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
974 if (sb1000_debug > 2)
975 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
977 /* Activate board and check firmware version */
979 if ((status = sb1000_activate(ioaddr, name)))
982 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
984 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
985 printk(KERN_WARNING "%s: found firmware version %x.%02x "
986 "(should be %x.%02x)\n", name, version[0], version[1],
987 FirmwareVersion[0], FirmwareVersion[1]);
990 netif_start_queue(dev);
991 return 0; /* Always succeed */
994 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
997 unsigned char version[2];
999 int ioaddr[2], status, frequency;
1000 unsigned int stats[5];
1001 struct sb1000_private *lp = netdev_priv(dev);
1003 if (!(dev && dev->flags & IFF_UP))
1006 ioaddr[0] = dev->base_addr;
1007 /* mem_start holds the second I/O address */
1008 ioaddr[1] = dev->mem_start;
1012 case SIOCGCMSTATS: /* get statistics */
1013 stats[0] = lp->stats.rx_bytes;
1014 stats[1] = lp->rx_frames;
1015 stats[2] = lp->stats.rx_packets;
1016 stats[3] = lp->stats.rx_errors;
1017 stats[4] = lp->stats.rx_dropped;
1018 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1023 case SIOCGCMFIRMWARE: /* get firmware version */
1024 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1026 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1030 case SIOCGCMFREQUENCY: /* get frequency */
1031 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1033 if(put_user(frequency, (int*) ifr->ifr_data))
1037 case SIOCSCMFREQUENCY: /* set frequency */
1038 if (!capable(CAP_NET_ADMIN))
1040 if(get_user(frequency, (int*) ifr->ifr_data))
1042 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1046 case SIOCGCMPIDS: /* get PIDs */
1047 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1049 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1053 case SIOCSCMPIDS: /* set PIDs */
1054 if (!capable(CAP_NET_ADMIN))
1056 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1058 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1060 /* set session_id, frame_id and pkt_type too */
1061 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1062 lp->rx_session_id[1] = 0x48;
1063 lp->rx_session_id[2] = 0x44;
1064 lp->rx_session_id[3] = 0x42;
1065 lp->rx_frame_id[0] = 0;
1066 lp->rx_frame_id[1] = 0;
1067 lp->rx_frame_id[2] = 0;
1068 lp->rx_frame_id[3] = 0;
1078 /* transmit function: do nothing since SB1000 can't send anything out */
1080 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1082 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1083 /* sb1000 can't xmit datagrams */
1088 /* SB1000 interrupt handler. */
1089 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1094 struct net_device *dev = (struct net_device *) dev_id;
1095 struct sb1000_private *lp = netdev_priv(dev);
1097 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1098 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1099 const int MaxRxErrorCount = 6;
1102 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1107 ioaddr[0] = dev->base_addr;
1108 /* mem_start holds the second I/O address */
1109 ioaddr[1] = dev->mem_start;
1112 /* is it a good interrupt? */
1113 st = inb(ioaddr[1] + 6);
1114 if (!(st & 0x08 && st & 0x20)) {
1118 if (sb1000_debug > 3)
1119 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1121 st = inb(ioaddr[0] + 7);
1123 lp->rx_error_count++;
1125 udelay(SB1000_DELAY);
1126 #endif /* SB1000_DELAY */
1127 sb1000_issue_read_command(ioaddr, name);
1129 sb1000_error_dpc(dev);
1130 sb1000_issue_read_command(ioaddr, name);
1132 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1133 sb1000_wait_for_ready_clear(ioaddr, name);
1134 sb1000_send_command(ioaddr, name, Command0);
1135 sb1000_wait_for_ready(ioaddr, name);
1136 sb1000_issue_read_command(ioaddr, name);
1138 if (lp->rx_error_count >= MaxRxErrorCount) {
1139 sb1000_wait_for_ready_clear(ioaddr, name);
1140 sb1000_send_command(ioaddr, name, Command1);
1141 sb1000_wait_for_ready(ioaddr, name);
1142 sb1000_issue_read_command(ioaddr, name);
1143 lp->rx_error_count = 0;
1149 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1151 struct sb1000_private *lp = netdev_priv(dev);
1155 static int sb1000_close(struct net_device *dev)
1159 struct sb1000_private *lp = netdev_priv(dev);
1161 if (sb1000_debug > 2)
1162 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1164 netif_stop_queue(dev);
1166 ioaddr[0] = dev->base_addr;
1167 /* mem_start holds the second I/O address */
1168 ioaddr[1] = dev->mem_start;
1170 free_irq(dev->irq, dev);
1171 /* If we don't do this, we can't re-insmod it later. */
1172 release_region(ioaddr[1], SB1000_IO_EXTENT);
1173 release_region(ioaddr[0], SB1000_IO_EXTENT);
1175 /* free rx_skb's if needed */
1176 for (i=0; i<4; i++) {
1177 if (lp->rx_skb[i]) {
1178 dev_kfree_skb(lp->rx_skb[i]);
1184 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1185 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1186 MODULE_LICENSE("GPL");
1191 return pnp_register_driver(&sb1000_driver);
1197 pnp_unregister_driver(&sb1000_driver);
1200 module_init(sb1000_init);
1201 module_exit(sb1000_exit);