Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / net / sb1000.c
blob5986cec17f19eff6b3aa06ba9f4d500ef953b8bd
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>
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 */
43 #include <linux/in.h>
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>
52 #include <linux/init.h>
53 #include <linux/bitops.h>
55 #include <asm/io.h>
56 #include <asm/processor.h>
57 #include <asm/uaccess.h>
59 #ifdef SB1000_DEBUG
60 static int sb1000_debug = SB1000_DEBUG;
61 #else
62 static const int sb1000_debug = 1;
63 #endif
65 static const int SB1000_IO_EXTENT = 8;
66 /* SB1000 Maximum Receive Unit */
67 static const int SB1000_MRU = 1500; /* octects */
69 #define NPIDS 4
70 struct sb1000_private {
71 struct sk_buff *rx_skb[NPIDS];
72 short rx_dlen[NPIDS];
73 unsigned int rx_frames;
74 short rx_error_count;
75 short rx_error_dpc_count;
76 unsigned char rx_session_id[NPIDS];
77 unsigned char rx_frame_id[NPIDS];
78 unsigned char rx_pkt_type[NPIDS];
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);
87 static int sb1000_close(struct net_device *dev);
90 /* SB1000 hardware routines to be used during open/configuration phases */
91 static int card_wait_for_busy_clear(const int ioaddr[],
92 const char* name);
93 static int card_wait_for_ready(const int ioaddr[], const char* name,
94 unsigned char in[]);
95 static int card_send_command(const int ioaddr[], const char* name,
96 const unsigned char out[], unsigned char in[]);
98 /* SB1000 hardware routines to be used during frame rx interrupt */
99 static int sb1000_wait_for_ready(const int ioaddr[], const char* name);
100 static int sb1000_wait_for_ready_clear(const int ioaddr[],
101 const char* name);
102 static void sb1000_send_command(const int ioaddr[], const char* name,
103 const unsigned char out[]);
104 static void sb1000_read_status(const int ioaddr[], unsigned char in[]);
105 static void sb1000_issue_read_command(const int ioaddr[],
106 const char* name);
108 /* SB1000 commands for open/configuration */
109 static int sb1000_reset(const int ioaddr[], const char* name);
110 static int sb1000_check_CRC(const int ioaddr[], const char* name);
111 static inline int sb1000_start_get_set_command(const int ioaddr[],
112 const char* name);
113 static int sb1000_end_get_set_command(const int ioaddr[],
114 const char* name);
115 static int sb1000_activate(const int ioaddr[], const char* name);
116 static int sb1000_get_firmware_version(const int ioaddr[],
117 const char* name, unsigned char version[], int do_end);
118 static int sb1000_get_frequency(const int ioaddr[], const char* name,
119 int* frequency);
120 static int sb1000_set_frequency(const int ioaddr[], const char* name,
121 int frequency);
122 static int sb1000_get_PIDs(const int ioaddr[], const char* name,
123 short PID[]);
124 static int sb1000_set_PIDs(const int ioaddr[], const char* name,
125 const short PID[]);
127 /* SB1000 commands for frame rx interrupt */
128 static int sb1000_rx(struct net_device *dev);
129 static void sb1000_error_dpc(struct net_device *dev);
131 static const struct pnp_device_id sb1000_pnp_ids[] = {
132 { "GIC1000", 0 },
133 { "", 0 }
135 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
137 static int
138 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
140 struct net_device *dev;
141 unsigned short ioaddr[2], irq;
142 unsigned int serial_number;
143 int error = -ENODEV;
145 if (pnp_device_attach(pdev) < 0)
146 return -ENODEV;
147 if (pnp_activate_dev(pdev) < 0)
148 goto out_detach;
150 if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
151 goto out_disable;
152 if (!pnp_irq_valid(pdev, 0))
153 goto out_disable;
155 serial_number = pdev->card->serial;
157 ioaddr[0] = pnp_port_start(pdev, 0);
158 ioaddr[1] = pnp_port_start(pdev, 0);
160 irq = pnp_irq(pdev, 0);
162 if (!request_region(ioaddr[0], 16, "sb1000"))
163 goto out_disable;
164 if (!request_region(ioaddr[1], 16, "sb1000"))
165 goto out_release_region0;
167 dev = alloc_etherdev(sizeof(struct sb1000_private));
168 if (!dev) {
169 error = -ENOMEM;
170 goto out_release_regions;
174 dev->base_addr = ioaddr[0];
175 /* mem_start holds the second I/O address */
176 dev->mem_start = ioaddr[1];
177 dev->irq = irq;
179 if (sb1000_debug > 0)
180 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
181 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
182 dev->mem_start, serial_number, dev->irq);
185 * The SB1000 is an rx-only cable modem device. The uplink is a modem
186 * and we do not want to arp on it.
188 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
190 SET_NETDEV_DEV(dev, &pdev->dev);
192 if (sb1000_debug > 0)
193 printk(KERN_NOTICE "%s", version);
195 /* The SB1000-specific entries in the device structure. */
196 dev->open = sb1000_open;
197 dev->do_ioctl = sb1000_dev_ioctl;
198 dev->hard_start_xmit = sb1000_start_xmit;
199 dev->stop = sb1000_close;
201 /* hardware address is 0:0:serial_number */
202 dev->dev_addr[2] = serial_number >> 24 & 0xff;
203 dev->dev_addr[3] = serial_number >> 16 & 0xff;
204 dev->dev_addr[4] = serial_number >> 8 & 0xff;
205 dev->dev_addr[5] = serial_number >> 0 & 0xff;
207 pnp_set_drvdata(pdev, dev);
209 error = register_netdev(dev);
210 if (error)
211 goto out_free_netdev;
212 return 0;
214 out_free_netdev:
215 free_netdev(dev);
216 out_release_regions:
217 release_region(ioaddr[1], 16);
218 out_release_region0:
219 release_region(ioaddr[0], 16);
220 out_disable:
221 pnp_disable_dev(pdev);
222 out_detach:
223 pnp_device_detach(pdev);
224 return error;
227 static void
228 sb1000_remove_one(struct pnp_dev *pdev)
230 struct net_device *dev = pnp_get_drvdata(pdev);
232 unregister_netdev(dev);
233 release_region(dev->base_addr, 16);
234 release_region(dev->mem_start, 16);
235 free_netdev(dev);
238 static struct pnp_driver sb1000_driver = {
239 .name = "sb1000",
240 .id_table = sb1000_pnp_ids,
241 .probe = sb1000_probe_one,
242 .remove = sb1000_remove_one,
247 * SB1000 hardware routines to be used during open/configuration phases
250 static const int TimeOutJiffies = (875 * HZ) / 100;
252 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
253 static int
254 card_wait_for_busy_clear(const int ioaddr[], const char* name)
256 unsigned char a;
257 unsigned long timeout;
259 a = inb(ioaddr[0] + 7);
260 timeout = jiffies + TimeOutJiffies;
261 while (a & 0x80 || a & 0x40) {
262 /* a little sleep */
263 yield();
265 a = inb(ioaddr[0] + 7);
266 if (time_after_eq(jiffies, timeout)) {
267 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
268 name);
269 return -ETIME;
273 return 0;
276 /* Card Wait For Ready (cannot be used during an interrupt) */
277 static int
278 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
280 unsigned char a;
281 unsigned long timeout;
283 a = inb(ioaddr[1] + 6);
284 timeout = jiffies + TimeOutJiffies;
285 while (a & 0x80 || !(a & 0x40)) {
286 /* a little sleep */
287 yield();
289 a = inb(ioaddr[1] + 6);
290 if (time_after_eq(jiffies, timeout)) {
291 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
292 name);
293 return -ETIME;
297 in[1] = inb(ioaddr[0] + 1);
298 in[2] = inb(ioaddr[0] + 2);
299 in[3] = inb(ioaddr[0] + 3);
300 in[4] = inb(ioaddr[0] + 4);
301 in[0] = inb(ioaddr[0] + 5);
302 in[6] = inb(ioaddr[0] + 6);
303 in[5] = inb(ioaddr[1] + 6);
304 return 0;
307 /* Card Send Command (cannot be used during an interrupt) */
308 static int
309 card_send_command(const int ioaddr[], const char* name,
310 const unsigned char out[], unsigned char in[])
312 int status, x;
314 if ((status = card_wait_for_busy_clear(ioaddr, name)))
315 return status;
316 outb(0xa0, ioaddr[0] + 6);
317 outb(out[2], ioaddr[0] + 1);
318 outb(out[3], ioaddr[0] + 2);
319 outb(out[4], ioaddr[0] + 3);
320 outb(out[5], ioaddr[0] + 4);
321 outb(out[1], ioaddr[0] + 5);
322 outb(0xa0, ioaddr[0] + 6);
323 outb(out[0], ioaddr[0] + 7);
324 if (out[0] != 0x20 && out[0] != 0x30) {
325 if ((status = card_wait_for_ready(ioaddr, name, in)))
326 return status;
327 inb(ioaddr[0] + 7);
328 if (sb1000_debug > 3)
329 printk(KERN_DEBUG "%s: card_send_command "
330 "out: %02x%02x%02x%02x%02x%02x "
331 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
332 out[0], out[1], out[2], out[3], out[4], out[5],
333 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
334 } else {
335 if (sb1000_debug > 3)
336 printk(KERN_DEBUG "%s: card_send_command "
337 "out: %02x%02x%02x%02x%02x%02x\n", name,
338 out[0], out[1], out[2], out[3], out[4], out[5]);
341 if (out[1] == 0x1b) {
342 x = (out[2] == 0x02);
343 } else {
344 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
345 return -EIO;
347 return 0;
352 * SB1000 hardware routines to be used during frame rx interrupt
354 static const int Sb1000TimeOutJiffies = 7 * HZ;
356 /* Card Wait For Ready (to be used during frame rx) */
357 static int
358 sb1000_wait_for_ready(const int ioaddr[], const char* name)
360 unsigned long timeout;
362 timeout = jiffies + Sb1000TimeOutJiffies;
363 while (inb(ioaddr[1] + 6) & 0x80) {
364 if (time_after_eq(jiffies, timeout)) {
365 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
366 name);
367 return -ETIME;
370 timeout = jiffies + Sb1000TimeOutJiffies;
371 while (!(inb(ioaddr[1] + 6) & 0x40)) {
372 if (time_after_eq(jiffies, timeout)) {
373 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
374 name);
375 return -ETIME;
378 inb(ioaddr[0] + 7);
379 return 0;
382 /* Card Wait For Ready Clear (to be used during frame rx) */
383 static int
384 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
386 unsigned long timeout;
388 timeout = jiffies + Sb1000TimeOutJiffies;
389 while (inb(ioaddr[1] + 6) & 0x80) {
390 if (time_after_eq(jiffies, timeout)) {
391 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
392 name);
393 return -ETIME;
396 timeout = jiffies + Sb1000TimeOutJiffies;
397 while (inb(ioaddr[1] + 6) & 0x40) {
398 if (time_after_eq(jiffies, timeout)) {
399 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
400 name);
401 return -ETIME;
404 return 0;
407 /* Card Send Command (to be used during frame rx) */
408 static void
409 sb1000_send_command(const int ioaddr[], const char* name,
410 const unsigned char out[])
412 outb(out[2], ioaddr[0] + 1);
413 outb(out[3], ioaddr[0] + 2);
414 outb(out[4], ioaddr[0] + 3);
415 outb(out[5], ioaddr[0] + 4);
416 outb(out[1], ioaddr[0] + 5);
417 outb(out[0], ioaddr[0] + 7);
418 if (sb1000_debug > 3)
419 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
420 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
421 return;
424 /* Card Read Status (to be used during frame rx) */
425 static void
426 sb1000_read_status(const int ioaddr[], unsigned char in[])
428 in[1] = inb(ioaddr[0] + 1);
429 in[2] = inb(ioaddr[0] + 2);
430 in[3] = inb(ioaddr[0] + 3);
431 in[4] = inb(ioaddr[0] + 4);
432 in[0] = inb(ioaddr[0] + 5);
433 return;
436 /* Issue Read Command (to be used during frame rx) */
437 static void
438 sb1000_issue_read_command(const int ioaddr[], const char* name)
440 static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
442 sb1000_wait_for_ready_clear(ioaddr, name);
443 outb(0xa0, ioaddr[0] + 6);
444 sb1000_send_command(ioaddr, name, Command0);
445 return;
450 * SB1000 commands for open/configuration
452 /* reset SB1000 card */
453 static int
454 sb1000_reset(const int ioaddr[], const char* name)
456 static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
458 unsigned char st[7];
459 int port, status;
461 port = ioaddr[1] + 6;
462 outb(0x4, port);
463 inb(port);
464 udelay(1000);
465 outb(0x0, port);
466 inb(port);
467 ssleep(1);
468 outb(0x4, port);
469 inb(port);
470 udelay(1000);
471 outb(0x0, port);
472 inb(port);
473 udelay(0);
475 if ((status = card_send_command(ioaddr, name, Command0, st)))
476 return status;
477 if (st[3] != 0xf0)
478 return -EIO;
479 return 0;
482 /* check SB1000 firmware CRC */
483 static int
484 sb1000_check_CRC(const int ioaddr[], const char* name)
486 static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
488 unsigned char st[7];
489 int crc, status;
491 /* check CRC */
492 if ((status = card_send_command(ioaddr, name, Command0, st)))
493 return status;
494 if (st[1] != st[3] || st[2] != st[4])
495 return -EIO;
496 crc = st[1] << 8 | st[2];
497 return 0;
500 static inline int
501 sb1000_start_get_set_command(const int ioaddr[], const char* name)
503 static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
505 unsigned char st[7];
507 return card_send_command(ioaddr, name, Command0, st);
510 static int
511 sb1000_end_get_set_command(const int ioaddr[], const char* name)
513 static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
514 static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
516 unsigned char st[7];
517 int status;
519 if ((status = card_send_command(ioaddr, name, Command0, st)))
520 return status;
521 return card_send_command(ioaddr, name, Command1, st);
524 static int
525 sb1000_activate(const int ioaddr[], const char* name)
527 static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
528 static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
530 unsigned char st[7];
531 int status;
533 ssleep(1);
534 if ((status = card_send_command(ioaddr, name, Command0, st)))
535 return status;
536 if ((status = card_send_command(ioaddr, name, Command1, st)))
537 return status;
538 if (st[3] != 0xf1) {
539 if ((status = sb1000_start_get_set_command(ioaddr, name)))
540 return status;
541 return -EIO;
543 udelay(1000);
544 return sb1000_start_get_set_command(ioaddr, name);
547 /* get SB1000 firmware version */
548 static int
549 sb1000_get_firmware_version(const int ioaddr[], const char* name,
550 unsigned char version[], int do_end)
552 static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
554 unsigned char st[7];
555 int status;
557 if ((status = sb1000_start_get_set_command(ioaddr, name)))
558 return status;
559 if ((status = card_send_command(ioaddr, name, Command0, st)))
560 return status;
561 if (st[0] != 0xa3)
562 return -EIO;
563 version[0] = st[1];
564 version[1] = st[2];
565 if (do_end)
566 return sb1000_end_get_set_command(ioaddr, name);
567 else
568 return 0;
571 /* get SB1000 frequency */
572 static int
573 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
575 static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
577 unsigned char st[7];
578 int status;
580 udelay(1000);
581 if ((status = sb1000_start_get_set_command(ioaddr, name)))
582 return status;
583 if ((status = card_send_command(ioaddr, name, Command0, st)))
584 return status;
585 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
586 return sb1000_end_get_set_command(ioaddr, name);
589 /* set SB1000 frequency */
590 static int
591 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
593 unsigned char st[7];
594 int status;
595 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
597 const int FrequencyLowerLimit = 57000;
598 const int FrequencyUpperLimit = 804000;
600 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
601 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
602 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
603 FrequencyUpperLimit);
604 return -EINVAL;
606 udelay(1000);
607 if ((status = sb1000_start_get_set_command(ioaddr, name)))
608 return status;
609 Command0[5] = frequency & 0xff;
610 frequency >>= 8;
611 Command0[4] = frequency & 0xff;
612 frequency >>= 8;
613 Command0[3] = frequency & 0xff;
614 frequency >>= 8;
615 Command0[2] = frequency & 0xff;
616 return card_send_command(ioaddr, name, Command0, st);
619 /* get SB1000 PIDs */
620 static int
621 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
623 static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
624 static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
625 static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
626 static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
628 unsigned char st[7];
629 int status;
631 udelay(1000);
632 if ((status = sb1000_start_get_set_command(ioaddr, name)))
633 return status;
635 if ((status = card_send_command(ioaddr, name, Command0, st)))
636 return status;
637 PID[0] = st[1] << 8 | st[2];
639 if ((status = card_send_command(ioaddr, name, Command1, st)))
640 return status;
641 PID[1] = st[1] << 8 | st[2];
643 if ((status = card_send_command(ioaddr, name, Command2, st)))
644 return status;
645 PID[2] = st[1] << 8 | st[2];
647 if ((status = card_send_command(ioaddr, name, Command3, st)))
648 return status;
649 PID[3] = st[1] << 8 | st[2];
651 return sb1000_end_get_set_command(ioaddr, name);
654 /* set SB1000 PIDs */
655 static int
656 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
658 static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
660 unsigned char st[7];
661 short p;
662 int status;
663 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
664 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
665 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
666 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
668 udelay(1000);
669 if ((status = sb1000_start_get_set_command(ioaddr, name)))
670 return status;
672 p = PID[0];
673 Command0[3] = p & 0xff;
674 p >>= 8;
675 Command0[2] = p & 0xff;
676 if ((status = card_send_command(ioaddr, name, Command0, st)))
677 return status;
679 p = PID[1];
680 Command1[3] = p & 0xff;
681 p >>= 8;
682 Command1[2] = p & 0xff;
683 if ((status = card_send_command(ioaddr, name, Command1, st)))
684 return status;
686 p = PID[2];
687 Command2[3] = p & 0xff;
688 p >>= 8;
689 Command2[2] = p & 0xff;
690 if ((status = card_send_command(ioaddr, name, Command2, st)))
691 return status;
693 p = PID[3];
694 Command3[3] = p & 0xff;
695 p >>= 8;
696 Command3[2] = p & 0xff;
697 if ((status = card_send_command(ioaddr, name, Command3, st)))
698 return status;
700 if ((status = card_send_command(ioaddr, name, Command4, st)))
701 return status;
702 return sb1000_end_get_set_command(ioaddr, name);
706 static void
707 sb1000_print_status_buffer(const char* name, unsigned char st[],
708 unsigned char buffer[], int size)
710 int i, j, k;
712 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
713 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
714 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
715 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
716 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
717 buffer[46] << 8 | buffer[47],
718 buffer[42], buffer[43], buffer[44], buffer[45],
719 buffer[48] << 8 | buffer[49]);
720 } else {
721 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
722 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
723 for (j = 0; j < 8 && k < size; j++, k++)
724 printk(" %02x", buffer[k]);
725 printk("\n");
728 return;
732 * SB1000 commands for frame rx interrupt
734 /* receive a single frame and assemble datagram
735 * (this is the heart of the interrupt routine)
737 static int
738 sb1000_rx(struct net_device *dev)
741 #define FRAMESIZE 184
742 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
743 short dlen;
744 int ioaddr, ns;
745 unsigned int skbsize;
746 struct sk_buff *skb;
747 struct sb1000_private *lp = netdev_priv(dev);
748 struct net_device_stats *stats = &dev->stats;
750 /* SB1000 frame constants */
751 const int FrameSize = FRAMESIZE;
752 const int NewDatagramHeaderSkip = 8;
753 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
754 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
755 const int ContDatagramHeaderSkip = 7;
756 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
757 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
758 const int TrailerSize = 4;
760 ioaddr = dev->base_addr;
762 insw(ioaddr, (unsigned short*) st, 1);
763 #ifdef XXXDEBUG
764 printk("cm0: received: %02x %02x\n", st[0], st[1]);
765 #endif /* XXXDEBUG */
766 lp->rx_frames++;
768 /* decide if it is a good or bad frame */
769 for (ns = 0; ns < NPIDS; ns++) {
770 session_id = lp->rx_session_id[ns];
771 frame_id = lp->rx_frame_id[ns];
772 if (st[0] == session_id) {
773 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
774 goto good_frame;
775 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
776 goto skipped_frame;
777 } else {
778 goto bad_frame;
780 } else if (st[0] == (session_id | 0x40)) {
781 if ((st[1] & 0xf0) == 0x30) {
782 goto skipped_frame;
783 } else {
784 goto bad_frame;
788 goto bad_frame;
790 skipped_frame:
791 stats->rx_frame_errors++;
792 skb = lp->rx_skb[ns];
793 if (sb1000_debug > 1)
794 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
795 "expecting %02x %02x\n", dev->name, st[0], st[1],
796 skb ? session_id : session_id | 0x40, frame_id);
797 if (skb) {
798 dev_kfree_skb(skb);
799 skb = NULL;
802 good_frame:
803 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
804 /* new datagram */
805 if (st[0] & 0x40) {
806 /* get data length */
807 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
808 #ifdef XXXDEBUG
809 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
810 #endif /* XXXDEBUG */
811 if (buffer[0] != NewDatagramHeaderSkip) {
812 if (sb1000_debug > 1)
813 printk(KERN_WARNING "%s: new datagram header skip error: "
814 "got %02x expecting %02x\n", dev->name, buffer[0],
815 NewDatagramHeaderSkip);
816 stats->rx_length_errors++;
817 insw(ioaddr, buffer, NewDatagramDataSize / 2);
818 goto bad_frame_next;
820 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
821 buffer[NewDatagramHeaderSkip + 4]) - 17;
822 if (dlen > SB1000_MRU) {
823 if (sb1000_debug > 1)
824 printk(KERN_WARNING "%s: datagram length (%d) greater "
825 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
826 stats->rx_length_errors++;
827 insw(ioaddr, buffer, NewDatagramDataSize / 2);
828 goto bad_frame_next;
830 lp->rx_dlen[ns] = dlen;
831 /* compute size to allocate for datagram */
832 skbsize = dlen + FrameSize;
833 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
834 if (sb1000_debug > 1)
835 printk(KERN_WARNING "%s: can't allocate %d bytes long "
836 "skbuff\n", dev->name, skbsize);
837 stats->rx_dropped++;
838 insw(ioaddr, buffer, NewDatagramDataSize / 2);
839 goto dropped_frame;
841 skb->dev = dev;
842 skb_reset_mac_header(skb);
843 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
844 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
845 NewDatagramDataSize / 2);
846 lp->rx_skb[ns] = skb;
847 } else {
848 /* continuation of previous datagram */
849 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
850 if (buffer[0] != ContDatagramHeaderSkip) {
851 if (sb1000_debug > 1)
852 printk(KERN_WARNING "%s: cont datagram header skip error: "
853 "got %02x expecting %02x\n", dev->name, buffer[0],
854 ContDatagramHeaderSkip);
855 stats->rx_length_errors++;
856 insw(ioaddr, buffer, ContDatagramDataSize / 2);
857 goto bad_frame_next;
859 skb = lp->rx_skb[ns];
860 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
861 ContDatagramDataSize / 2);
862 dlen = lp->rx_dlen[ns];
864 if (skb->len < dlen + TrailerSize) {
865 lp->rx_session_id[ns] &= ~0x40;
866 return 0;
869 /* datagram completed: send to upper level */
870 skb_trim(skb, dlen);
871 netif_rx(skb);
872 dev->last_rx = jiffies;
873 stats->rx_bytes+=dlen;
874 stats->rx_packets++;
875 lp->rx_skb[ns] = NULL;
876 lp->rx_session_id[ns] |= 0x40;
877 return 0;
879 bad_frame:
880 insw(ioaddr, buffer, FrameSize / 2);
881 if (sb1000_debug > 1)
882 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
883 dev->name, st[0], st[1]);
884 stats->rx_frame_errors++;
885 bad_frame_next:
886 if (sb1000_debug > 2)
887 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
888 dropped_frame:
889 stats->rx_errors++;
890 if (ns < NPIDS) {
891 if ((skb = lp->rx_skb[ns])) {
892 dev_kfree_skb(skb);
893 lp->rx_skb[ns] = NULL;
895 lp->rx_session_id[ns] |= 0x40;
897 return -1;
900 static void
901 sb1000_error_dpc(struct net_device *dev)
903 static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
905 char *name;
906 unsigned char st[5];
907 int ioaddr[2];
908 struct sb1000_private *lp = netdev_priv(dev);
909 const int ErrorDpcCounterInitialize = 200;
911 ioaddr[0] = dev->base_addr;
912 /* mem_start holds the second I/O address */
913 ioaddr[1] = dev->mem_start;
914 name = dev->name;
916 sb1000_wait_for_ready_clear(ioaddr, name);
917 sb1000_send_command(ioaddr, name, Command0);
918 sb1000_wait_for_ready(ioaddr, name);
919 sb1000_read_status(ioaddr, st);
920 if (st[1] & 0x10)
921 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
922 return;
927 * Linux interface functions
929 static int
930 sb1000_open(struct net_device *dev)
932 char *name;
933 int ioaddr[2], status;
934 struct sb1000_private *lp = netdev_priv(dev);
935 const unsigned short FirmwareVersion[] = {0x01, 0x01};
937 ioaddr[0] = dev->base_addr;
938 /* mem_start holds the second I/O address */
939 ioaddr[1] = dev->mem_start;
940 name = dev->name;
942 /* initialize sb1000 */
943 if ((status = sb1000_reset(ioaddr, name)))
944 return status;
945 ssleep(1);
946 if ((status = sb1000_check_CRC(ioaddr, name)))
947 return status;
949 /* initialize private data before board can catch interrupts */
950 lp->rx_skb[0] = NULL;
951 lp->rx_skb[1] = NULL;
952 lp->rx_skb[2] = NULL;
953 lp->rx_skb[3] = NULL;
954 lp->rx_dlen[0] = 0;
955 lp->rx_dlen[1] = 0;
956 lp->rx_dlen[2] = 0;
957 lp->rx_dlen[3] = 0;
958 lp->rx_frames = 0;
959 lp->rx_error_count = 0;
960 lp->rx_error_dpc_count = 0;
961 lp->rx_session_id[0] = 0x50;
962 lp->rx_session_id[0] = 0x48;
963 lp->rx_session_id[0] = 0x44;
964 lp->rx_session_id[0] = 0x42;
965 lp->rx_frame_id[0] = 0;
966 lp->rx_frame_id[1] = 0;
967 lp->rx_frame_id[2] = 0;
968 lp->rx_frame_id[3] = 0;
969 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
970 return -EAGAIN;
973 if (sb1000_debug > 2)
974 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
976 /* Activate board and check firmware version */
977 udelay(1000);
978 if ((status = sb1000_activate(ioaddr, name)))
979 return status;
980 udelay(0);
981 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
982 return status;
983 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
984 printk(KERN_WARNING "%s: found firmware version %x.%02x "
985 "(should be %x.%02x)\n", name, version[0], version[1],
986 FirmwareVersion[0], FirmwareVersion[1]);
989 netif_start_queue(dev);
990 return 0; /* Always succeed */
993 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
995 char* name;
996 unsigned char version[2];
997 short PID[4];
998 int ioaddr[2], status, frequency;
999 unsigned int stats[5];
1000 struct sb1000_private *lp = netdev_priv(dev);
1002 if (!(dev && dev->flags & IFF_UP))
1003 return -ENODEV;
1005 ioaddr[0] = dev->base_addr;
1006 /* mem_start holds the second I/O address */
1007 ioaddr[1] = dev->mem_start;
1008 name = dev->name;
1010 switch (cmd) {
1011 case SIOCGCMSTATS: /* get statistics */
1012 stats[0] = dev->stats.rx_bytes;
1013 stats[1] = lp->rx_frames;
1014 stats[2] = dev->stats.rx_packets;
1015 stats[3] = dev->stats.rx_errors;
1016 stats[4] = dev->stats.rx_dropped;
1017 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1018 return -EFAULT;
1019 status = 0;
1020 break;
1022 case SIOCGCMFIRMWARE: /* get firmware version */
1023 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1024 return status;
1025 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1026 return -EFAULT;
1027 break;
1029 case SIOCGCMFREQUENCY: /* get frequency */
1030 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1031 return status;
1032 if(put_user(frequency, (int __user *) ifr->ifr_data))
1033 return -EFAULT;
1034 break;
1036 case SIOCSCMFREQUENCY: /* set frequency */
1037 if (!capable(CAP_NET_ADMIN))
1038 return -EPERM;
1039 if(get_user(frequency, (int __user *) ifr->ifr_data))
1040 return -EFAULT;
1041 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1042 return status;
1043 break;
1045 case SIOCGCMPIDS: /* get PIDs */
1046 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1047 return status;
1048 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1049 return -EFAULT;
1050 break;
1052 case SIOCSCMPIDS: /* set PIDs */
1053 if (!capable(CAP_NET_ADMIN))
1054 return -EPERM;
1055 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1056 return -EFAULT;
1057 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1058 return status;
1059 /* set session_id, frame_id and pkt_type too */
1060 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1061 lp->rx_session_id[1] = 0x48;
1062 lp->rx_session_id[2] = 0x44;
1063 lp->rx_session_id[3] = 0x42;
1064 lp->rx_frame_id[0] = 0;
1065 lp->rx_frame_id[1] = 0;
1066 lp->rx_frame_id[2] = 0;
1067 lp->rx_frame_id[3] = 0;
1068 break;
1070 default:
1071 status = -EINVAL;
1072 break;
1074 return status;
1077 /* transmit function: do nothing since SB1000 can't send anything out */
1078 static int
1079 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1081 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1082 /* sb1000 can't xmit datagrams */
1083 dev_kfree_skb(skb);
1084 return 0;
1087 /* SB1000 interrupt handler. */
1088 static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1090 static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1091 static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1093 char *name;
1094 unsigned char st;
1095 int ioaddr[2];
1096 struct net_device *dev = dev_id;
1097 struct sb1000_private *lp = netdev_priv(dev);
1099 const int MaxRxErrorCount = 6;
1101 ioaddr[0] = dev->base_addr;
1102 /* mem_start holds the second I/O address */
1103 ioaddr[1] = dev->mem_start;
1104 name = dev->name;
1106 /* is it a good interrupt? */
1107 st = inb(ioaddr[1] + 6);
1108 if (!(st & 0x08 && st & 0x20)) {
1109 return IRQ_NONE;
1112 if (sb1000_debug > 3)
1113 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1115 st = inb(ioaddr[0] + 7);
1116 if (sb1000_rx(dev))
1117 lp->rx_error_count++;
1118 #ifdef SB1000_DELAY
1119 udelay(SB1000_DELAY);
1120 #endif /* SB1000_DELAY */
1121 sb1000_issue_read_command(ioaddr, name);
1122 if (st & 0x01) {
1123 sb1000_error_dpc(dev);
1124 sb1000_issue_read_command(ioaddr, name);
1126 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1127 sb1000_wait_for_ready_clear(ioaddr, name);
1128 sb1000_send_command(ioaddr, name, Command0);
1129 sb1000_wait_for_ready(ioaddr, name);
1130 sb1000_issue_read_command(ioaddr, name);
1132 if (lp->rx_error_count >= MaxRxErrorCount) {
1133 sb1000_wait_for_ready_clear(ioaddr, name);
1134 sb1000_send_command(ioaddr, name, Command1);
1135 sb1000_wait_for_ready(ioaddr, name);
1136 sb1000_issue_read_command(ioaddr, name);
1137 lp->rx_error_count = 0;
1140 return IRQ_HANDLED;
1143 static int sb1000_close(struct net_device *dev)
1145 int i;
1146 int ioaddr[2];
1147 struct sb1000_private *lp = netdev_priv(dev);
1149 if (sb1000_debug > 2)
1150 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1152 netif_stop_queue(dev);
1154 ioaddr[0] = dev->base_addr;
1155 /* mem_start holds the second I/O address */
1156 ioaddr[1] = dev->mem_start;
1158 free_irq(dev->irq, dev);
1159 /* If we don't do this, we can't re-insmod it later. */
1160 release_region(ioaddr[1], SB1000_IO_EXTENT);
1161 release_region(ioaddr[0], SB1000_IO_EXTENT);
1163 /* free rx_skb's if needed */
1164 for (i=0; i<4; i++) {
1165 if (lp->rx_skb[i]) {
1166 dev_kfree_skb(lp->rx_skb[i]);
1169 return 0;
1172 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1173 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1174 MODULE_LICENSE("GPL");
1176 static int __init
1177 sb1000_init(void)
1179 return pnp_register_driver(&sb1000_driver);
1182 static void __exit
1183 sb1000_exit(void)
1185 pnp_unregister_driver(&sb1000_driver);
1188 module_init(sb1000_init);
1189 module_exit(sb1000_exit);