r8169: WakeOnLan fix for the 8168
[linux-2.6/mini2440.git] / drivers / net / sb1000.c
blobfc0e38bddeebbc7a5fea9b772ff2f3949a602aa4
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 const struct net_device_ops sb1000_netdev_ops = {
138 .ndo_open = sb1000_open,
139 .ndo_start_xmit = sb1000_start_xmit,
140 .ndo_do_ioctl = sb1000_dev_ioctl,
141 .ndo_stop = sb1000_close,
142 .ndo_change_mtu = eth_change_mtu,
143 .ndo_set_mac_address = eth_mac_addr,
144 .ndo_validate_addr = eth_validate_addr,
147 static int
148 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
150 struct net_device *dev;
151 unsigned short ioaddr[2], irq;
152 unsigned int serial_number;
153 int error = -ENODEV;
155 if (pnp_device_attach(pdev) < 0)
156 return -ENODEV;
157 if (pnp_activate_dev(pdev) < 0)
158 goto out_detach;
160 if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
161 goto out_disable;
162 if (!pnp_irq_valid(pdev, 0))
163 goto out_disable;
165 serial_number = pdev->card->serial;
167 ioaddr[0] = pnp_port_start(pdev, 0);
168 ioaddr[1] = pnp_port_start(pdev, 0);
170 irq = pnp_irq(pdev, 0);
172 if (!request_region(ioaddr[0], 16, "sb1000"))
173 goto out_disable;
174 if (!request_region(ioaddr[1], 16, "sb1000"))
175 goto out_release_region0;
177 dev = alloc_etherdev(sizeof(struct sb1000_private));
178 if (!dev) {
179 error = -ENOMEM;
180 goto out_release_regions;
184 dev->base_addr = ioaddr[0];
185 /* mem_start holds the second I/O address */
186 dev->mem_start = ioaddr[1];
187 dev->irq = irq;
189 if (sb1000_debug > 0)
190 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
191 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
192 dev->mem_start, serial_number, dev->irq);
195 * The SB1000 is an rx-only cable modem device. The uplink is a modem
196 * and we do not want to arp on it.
198 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
200 SET_NETDEV_DEV(dev, &pdev->dev);
202 if (sb1000_debug > 0)
203 printk(KERN_NOTICE "%s", version);
205 dev->netdev_ops = &sb1000_netdev_ops;
207 /* hardware address is 0:0:serial_number */
208 dev->dev_addr[2] = serial_number >> 24 & 0xff;
209 dev->dev_addr[3] = serial_number >> 16 & 0xff;
210 dev->dev_addr[4] = serial_number >> 8 & 0xff;
211 dev->dev_addr[5] = serial_number >> 0 & 0xff;
213 pnp_set_drvdata(pdev, dev);
215 error = register_netdev(dev);
216 if (error)
217 goto out_free_netdev;
218 return 0;
220 out_free_netdev:
221 free_netdev(dev);
222 out_release_regions:
223 release_region(ioaddr[1], 16);
224 out_release_region0:
225 release_region(ioaddr[0], 16);
226 out_disable:
227 pnp_disable_dev(pdev);
228 out_detach:
229 pnp_device_detach(pdev);
230 return error;
233 static void
234 sb1000_remove_one(struct pnp_dev *pdev)
236 struct net_device *dev = pnp_get_drvdata(pdev);
238 unregister_netdev(dev);
239 release_region(dev->base_addr, 16);
240 release_region(dev->mem_start, 16);
241 free_netdev(dev);
244 static struct pnp_driver sb1000_driver = {
245 .name = "sb1000",
246 .id_table = sb1000_pnp_ids,
247 .probe = sb1000_probe_one,
248 .remove = sb1000_remove_one,
253 * SB1000 hardware routines to be used during open/configuration phases
256 static const int TimeOutJiffies = (875 * HZ) / 100;
258 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
259 static int
260 card_wait_for_busy_clear(const int ioaddr[], const char* name)
262 unsigned char a;
263 unsigned long timeout;
265 a = inb(ioaddr[0] + 7);
266 timeout = jiffies + TimeOutJiffies;
267 while (a & 0x80 || a & 0x40) {
268 /* a little sleep */
269 yield();
271 a = inb(ioaddr[0] + 7);
272 if (time_after_eq(jiffies, timeout)) {
273 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
274 name);
275 return -ETIME;
279 return 0;
282 /* Card Wait For Ready (cannot be used during an interrupt) */
283 static int
284 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
286 unsigned char a;
287 unsigned long timeout;
289 a = inb(ioaddr[1] + 6);
290 timeout = jiffies + TimeOutJiffies;
291 while (a & 0x80 || !(a & 0x40)) {
292 /* a little sleep */
293 yield();
295 a = inb(ioaddr[1] + 6);
296 if (time_after_eq(jiffies, timeout)) {
297 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
298 name);
299 return -ETIME;
303 in[1] = inb(ioaddr[0] + 1);
304 in[2] = inb(ioaddr[0] + 2);
305 in[3] = inb(ioaddr[0] + 3);
306 in[4] = inb(ioaddr[0] + 4);
307 in[0] = inb(ioaddr[0] + 5);
308 in[6] = inb(ioaddr[0] + 6);
309 in[5] = inb(ioaddr[1] + 6);
310 return 0;
313 /* Card Send Command (cannot be used during an interrupt) */
314 static int
315 card_send_command(const int ioaddr[], const char* name,
316 const unsigned char out[], unsigned char in[])
318 int status, x;
320 if ((status = card_wait_for_busy_clear(ioaddr, name)))
321 return status;
322 outb(0xa0, ioaddr[0] + 6);
323 outb(out[2], ioaddr[0] + 1);
324 outb(out[3], ioaddr[0] + 2);
325 outb(out[4], ioaddr[0] + 3);
326 outb(out[5], ioaddr[0] + 4);
327 outb(out[1], ioaddr[0] + 5);
328 outb(0xa0, ioaddr[0] + 6);
329 outb(out[0], ioaddr[0] + 7);
330 if (out[0] != 0x20 && out[0] != 0x30) {
331 if ((status = card_wait_for_ready(ioaddr, name, in)))
332 return status;
333 inb(ioaddr[0] + 7);
334 if (sb1000_debug > 3)
335 printk(KERN_DEBUG "%s: card_send_command "
336 "out: %02x%02x%02x%02x%02x%02x "
337 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
338 out[0], out[1], out[2], out[3], out[4], out[5],
339 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
340 } else {
341 if (sb1000_debug > 3)
342 printk(KERN_DEBUG "%s: card_send_command "
343 "out: %02x%02x%02x%02x%02x%02x\n", name,
344 out[0], out[1], out[2], out[3], out[4], out[5]);
347 if (out[1] == 0x1b) {
348 x = (out[2] == 0x02);
349 } else {
350 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
351 return -EIO;
353 return 0;
358 * SB1000 hardware routines to be used during frame rx interrupt
360 static const int Sb1000TimeOutJiffies = 7 * HZ;
362 /* Card Wait For Ready (to be used during frame rx) */
363 static int
364 sb1000_wait_for_ready(const int ioaddr[], const char* name)
366 unsigned long timeout;
368 timeout = jiffies + Sb1000TimeOutJiffies;
369 while (inb(ioaddr[1] + 6) & 0x80) {
370 if (time_after_eq(jiffies, timeout)) {
371 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
372 name);
373 return -ETIME;
376 timeout = jiffies + Sb1000TimeOutJiffies;
377 while (!(inb(ioaddr[1] + 6) & 0x40)) {
378 if (time_after_eq(jiffies, timeout)) {
379 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
380 name);
381 return -ETIME;
384 inb(ioaddr[0] + 7);
385 return 0;
388 /* Card Wait For Ready Clear (to be used during frame rx) */
389 static int
390 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
392 unsigned long timeout;
394 timeout = jiffies + Sb1000TimeOutJiffies;
395 while (inb(ioaddr[1] + 6) & 0x80) {
396 if (time_after_eq(jiffies, timeout)) {
397 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
398 name);
399 return -ETIME;
402 timeout = jiffies + Sb1000TimeOutJiffies;
403 while (inb(ioaddr[1] + 6) & 0x40) {
404 if (time_after_eq(jiffies, timeout)) {
405 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
406 name);
407 return -ETIME;
410 return 0;
413 /* Card Send Command (to be used during frame rx) */
414 static void
415 sb1000_send_command(const int ioaddr[], const char* name,
416 const unsigned char out[])
418 outb(out[2], ioaddr[0] + 1);
419 outb(out[3], ioaddr[0] + 2);
420 outb(out[4], ioaddr[0] + 3);
421 outb(out[5], ioaddr[0] + 4);
422 outb(out[1], ioaddr[0] + 5);
423 outb(out[0], ioaddr[0] + 7);
424 if (sb1000_debug > 3)
425 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
426 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
427 return;
430 /* Card Read Status (to be used during frame rx) */
431 static void
432 sb1000_read_status(const int ioaddr[], unsigned char in[])
434 in[1] = inb(ioaddr[0] + 1);
435 in[2] = inb(ioaddr[0] + 2);
436 in[3] = inb(ioaddr[0] + 3);
437 in[4] = inb(ioaddr[0] + 4);
438 in[0] = inb(ioaddr[0] + 5);
439 return;
442 /* Issue Read Command (to be used during frame rx) */
443 static void
444 sb1000_issue_read_command(const int ioaddr[], const char* name)
446 static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
448 sb1000_wait_for_ready_clear(ioaddr, name);
449 outb(0xa0, ioaddr[0] + 6);
450 sb1000_send_command(ioaddr, name, Command0);
451 return;
456 * SB1000 commands for open/configuration
458 /* reset SB1000 card */
459 static int
460 sb1000_reset(const int ioaddr[], const char* name)
462 static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
464 unsigned char st[7];
465 int port, status;
467 port = ioaddr[1] + 6;
468 outb(0x4, port);
469 inb(port);
470 udelay(1000);
471 outb(0x0, port);
472 inb(port);
473 ssleep(1);
474 outb(0x4, port);
475 inb(port);
476 udelay(1000);
477 outb(0x0, port);
478 inb(port);
479 udelay(0);
481 if ((status = card_send_command(ioaddr, name, Command0, st)))
482 return status;
483 if (st[3] != 0xf0)
484 return -EIO;
485 return 0;
488 /* check SB1000 firmware CRC */
489 static int
490 sb1000_check_CRC(const int ioaddr[], const char* name)
492 static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
494 unsigned char st[7];
495 int crc, status;
497 /* check CRC */
498 if ((status = card_send_command(ioaddr, name, Command0, st)))
499 return status;
500 if (st[1] != st[3] || st[2] != st[4])
501 return -EIO;
502 crc = st[1] << 8 | st[2];
503 return 0;
506 static inline int
507 sb1000_start_get_set_command(const int ioaddr[], const char* name)
509 static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
511 unsigned char st[7];
513 return card_send_command(ioaddr, name, Command0, st);
516 static int
517 sb1000_end_get_set_command(const int ioaddr[], const char* name)
519 static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
520 static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
522 unsigned char st[7];
523 int status;
525 if ((status = card_send_command(ioaddr, name, Command0, st)))
526 return status;
527 return card_send_command(ioaddr, name, Command1, st);
530 static int
531 sb1000_activate(const int ioaddr[], const char* name)
533 static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
534 static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
536 unsigned char st[7];
537 int status;
539 ssleep(1);
540 if ((status = card_send_command(ioaddr, name, Command0, st)))
541 return status;
542 if ((status = card_send_command(ioaddr, name, Command1, st)))
543 return status;
544 if (st[3] != 0xf1) {
545 if ((status = sb1000_start_get_set_command(ioaddr, name)))
546 return status;
547 return -EIO;
549 udelay(1000);
550 return sb1000_start_get_set_command(ioaddr, name);
553 /* get SB1000 firmware version */
554 static int
555 sb1000_get_firmware_version(const int ioaddr[], const char* name,
556 unsigned char version[], int do_end)
558 static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
560 unsigned char st[7];
561 int status;
563 if ((status = sb1000_start_get_set_command(ioaddr, name)))
564 return status;
565 if ((status = card_send_command(ioaddr, name, Command0, st)))
566 return status;
567 if (st[0] != 0xa3)
568 return -EIO;
569 version[0] = st[1];
570 version[1] = st[2];
571 if (do_end)
572 return sb1000_end_get_set_command(ioaddr, name);
573 else
574 return 0;
577 /* get SB1000 frequency */
578 static int
579 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
581 static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
583 unsigned char st[7];
584 int status;
586 udelay(1000);
587 if ((status = sb1000_start_get_set_command(ioaddr, name)))
588 return status;
589 if ((status = card_send_command(ioaddr, name, Command0, st)))
590 return status;
591 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
592 return sb1000_end_get_set_command(ioaddr, name);
595 /* set SB1000 frequency */
596 static int
597 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
599 unsigned char st[7];
600 int status;
601 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
603 const int FrequencyLowerLimit = 57000;
604 const int FrequencyUpperLimit = 804000;
606 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
607 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
608 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
609 FrequencyUpperLimit);
610 return -EINVAL;
612 udelay(1000);
613 if ((status = sb1000_start_get_set_command(ioaddr, name)))
614 return status;
615 Command0[5] = frequency & 0xff;
616 frequency >>= 8;
617 Command0[4] = frequency & 0xff;
618 frequency >>= 8;
619 Command0[3] = frequency & 0xff;
620 frequency >>= 8;
621 Command0[2] = frequency & 0xff;
622 return card_send_command(ioaddr, name, Command0, st);
625 /* get SB1000 PIDs */
626 static int
627 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
629 static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
630 static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
631 static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
632 static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
634 unsigned char st[7];
635 int status;
637 udelay(1000);
638 if ((status = sb1000_start_get_set_command(ioaddr, name)))
639 return status;
641 if ((status = card_send_command(ioaddr, name, Command0, st)))
642 return status;
643 PID[0] = st[1] << 8 | st[2];
645 if ((status = card_send_command(ioaddr, name, Command1, st)))
646 return status;
647 PID[1] = st[1] << 8 | st[2];
649 if ((status = card_send_command(ioaddr, name, Command2, st)))
650 return status;
651 PID[2] = st[1] << 8 | st[2];
653 if ((status = card_send_command(ioaddr, name, Command3, st)))
654 return status;
655 PID[3] = st[1] << 8 | st[2];
657 return sb1000_end_get_set_command(ioaddr, name);
660 /* set SB1000 PIDs */
661 static int
662 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
664 static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
666 unsigned char st[7];
667 short p;
668 int status;
669 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
670 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
671 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
672 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
674 udelay(1000);
675 if ((status = sb1000_start_get_set_command(ioaddr, name)))
676 return status;
678 p = PID[0];
679 Command0[3] = p & 0xff;
680 p >>= 8;
681 Command0[2] = p & 0xff;
682 if ((status = card_send_command(ioaddr, name, Command0, st)))
683 return status;
685 p = PID[1];
686 Command1[3] = p & 0xff;
687 p >>= 8;
688 Command1[2] = p & 0xff;
689 if ((status = card_send_command(ioaddr, name, Command1, st)))
690 return status;
692 p = PID[2];
693 Command2[3] = p & 0xff;
694 p >>= 8;
695 Command2[2] = p & 0xff;
696 if ((status = card_send_command(ioaddr, name, Command2, st)))
697 return status;
699 p = PID[3];
700 Command3[3] = p & 0xff;
701 p >>= 8;
702 Command3[2] = p & 0xff;
703 if ((status = card_send_command(ioaddr, name, Command3, st)))
704 return status;
706 if ((status = card_send_command(ioaddr, name, Command4, st)))
707 return status;
708 return sb1000_end_get_set_command(ioaddr, name);
712 static void
713 sb1000_print_status_buffer(const char* name, unsigned char st[],
714 unsigned char buffer[], int size)
716 int i, j, k;
718 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
719 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
720 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
721 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
722 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
723 buffer[46] << 8 | buffer[47],
724 buffer[42], buffer[43], buffer[44], buffer[45],
725 buffer[48] << 8 | buffer[49]);
726 } else {
727 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
728 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
729 for (j = 0; j < 8 && k < size; j++, k++)
730 printk(" %02x", buffer[k]);
731 printk("\n");
734 return;
738 * SB1000 commands for frame rx interrupt
740 /* receive a single frame and assemble datagram
741 * (this is the heart of the interrupt routine)
743 static int
744 sb1000_rx(struct net_device *dev)
747 #define FRAMESIZE 184
748 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
749 short dlen;
750 int ioaddr, ns;
751 unsigned int skbsize;
752 struct sk_buff *skb;
753 struct sb1000_private *lp = netdev_priv(dev);
754 struct net_device_stats *stats = &dev->stats;
756 /* SB1000 frame constants */
757 const int FrameSize = FRAMESIZE;
758 const int NewDatagramHeaderSkip = 8;
759 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
760 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
761 const int ContDatagramHeaderSkip = 7;
762 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
763 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
764 const int TrailerSize = 4;
766 ioaddr = dev->base_addr;
768 insw(ioaddr, (unsigned short*) st, 1);
769 #ifdef XXXDEBUG
770 printk("cm0: received: %02x %02x\n", st[0], st[1]);
771 #endif /* XXXDEBUG */
772 lp->rx_frames++;
774 /* decide if it is a good or bad frame */
775 for (ns = 0; ns < NPIDS; ns++) {
776 session_id = lp->rx_session_id[ns];
777 frame_id = lp->rx_frame_id[ns];
778 if (st[0] == session_id) {
779 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
780 goto good_frame;
781 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
782 goto skipped_frame;
783 } else {
784 goto bad_frame;
786 } else if (st[0] == (session_id | 0x40)) {
787 if ((st[1] & 0xf0) == 0x30) {
788 goto skipped_frame;
789 } else {
790 goto bad_frame;
794 goto bad_frame;
796 skipped_frame:
797 stats->rx_frame_errors++;
798 skb = lp->rx_skb[ns];
799 if (sb1000_debug > 1)
800 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
801 "expecting %02x %02x\n", dev->name, st[0], st[1],
802 skb ? session_id : session_id | 0x40, frame_id);
803 if (skb) {
804 dev_kfree_skb(skb);
805 skb = NULL;
808 good_frame:
809 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
810 /* new datagram */
811 if (st[0] & 0x40) {
812 /* get data length */
813 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
814 #ifdef XXXDEBUG
815 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
816 #endif /* XXXDEBUG */
817 if (buffer[0] != NewDatagramHeaderSkip) {
818 if (sb1000_debug > 1)
819 printk(KERN_WARNING "%s: new datagram header skip error: "
820 "got %02x expecting %02x\n", dev->name, buffer[0],
821 NewDatagramHeaderSkip);
822 stats->rx_length_errors++;
823 insw(ioaddr, buffer, NewDatagramDataSize / 2);
824 goto bad_frame_next;
826 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
827 buffer[NewDatagramHeaderSkip + 4]) - 17;
828 if (dlen > SB1000_MRU) {
829 if (sb1000_debug > 1)
830 printk(KERN_WARNING "%s: datagram length (%d) greater "
831 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
832 stats->rx_length_errors++;
833 insw(ioaddr, buffer, NewDatagramDataSize / 2);
834 goto bad_frame_next;
836 lp->rx_dlen[ns] = dlen;
837 /* compute size to allocate for datagram */
838 skbsize = dlen + FrameSize;
839 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
840 if (sb1000_debug > 1)
841 printk(KERN_WARNING "%s: can't allocate %d bytes long "
842 "skbuff\n", dev->name, skbsize);
843 stats->rx_dropped++;
844 insw(ioaddr, buffer, NewDatagramDataSize / 2);
845 goto dropped_frame;
847 skb->dev = dev;
848 skb_reset_mac_header(skb);
849 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
850 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
851 NewDatagramDataSize / 2);
852 lp->rx_skb[ns] = skb;
853 } else {
854 /* continuation of previous datagram */
855 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
856 if (buffer[0] != ContDatagramHeaderSkip) {
857 if (sb1000_debug > 1)
858 printk(KERN_WARNING "%s: cont datagram header skip error: "
859 "got %02x expecting %02x\n", dev->name, buffer[0],
860 ContDatagramHeaderSkip);
861 stats->rx_length_errors++;
862 insw(ioaddr, buffer, ContDatagramDataSize / 2);
863 goto bad_frame_next;
865 skb = lp->rx_skb[ns];
866 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
867 ContDatagramDataSize / 2);
868 dlen = lp->rx_dlen[ns];
870 if (skb->len < dlen + TrailerSize) {
871 lp->rx_session_id[ns] &= ~0x40;
872 return 0;
875 /* datagram completed: send to upper level */
876 skb_trim(skb, dlen);
877 netif_rx(skb);
878 stats->rx_bytes+=dlen;
879 stats->rx_packets++;
880 lp->rx_skb[ns] = NULL;
881 lp->rx_session_id[ns] |= 0x40;
882 return 0;
884 bad_frame:
885 insw(ioaddr, buffer, FrameSize / 2);
886 if (sb1000_debug > 1)
887 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
888 dev->name, st[0], st[1]);
889 stats->rx_frame_errors++;
890 bad_frame_next:
891 if (sb1000_debug > 2)
892 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
893 dropped_frame:
894 stats->rx_errors++;
895 if (ns < NPIDS) {
896 if ((skb = lp->rx_skb[ns])) {
897 dev_kfree_skb(skb);
898 lp->rx_skb[ns] = NULL;
900 lp->rx_session_id[ns] |= 0x40;
902 return -1;
905 static void
906 sb1000_error_dpc(struct net_device *dev)
908 static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
910 char *name;
911 unsigned char st[5];
912 int ioaddr[2];
913 struct sb1000_private *lp = netdev_priv(dev);
914 const int ErrorDpcCounterInitialize = 200;
916 ioaddr[0] = dev->base_addr;
917 /* mem_start holds the second I/O address */
918 ioaddr[1] = dev->mem_start;
919 name = dev->name;
921 sb1000_wait_for_ready_clear(ioaddr, name);
922 sb1000_send_command(ioaddr, name, Command0);
923 sb1000_wait_for_ready(ioaddr, name);
924 sb1000_read_status(ioaddr, st);
925 if (st[1] & 0x10)
926 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
927 return;
932 * Linux interface functions
934 static int
935 sb1000_open(struct net_device *dev)
937 char *name;
938 int ioaddr[2], status;
939 struct sb1000_private *lp = netdev_priv(dev);
940 const unsigned short FirmwareVersion[] = {0x01, 0x01};
942 ioaddr[0] = dev->base_addr;
943 /* mem_start holds the second I/O address */
944 ioaddr[1] = dev->mem_start;
945 name = dev->name;
947 /* initialize sb1000 */
948 if ((status = sb1000_reset(ioaddr, name)))
949 return status;
950 ssleep(1);
951 if ((status = sb1000_check_CRC(ioaddr, name)))
952 return status;
954 /* initialize private data before board can catch interrupts */
955 lp->rx_skb[0] = NULL;
956 lp->rx_skb[1] = NULL;
957 lp->rx_skb[2] = NULL;
958 lp->rx_skb[3] = NULL;
959 lp->rx_dlen[0] = 0;
960 lp->rx_dlen[1] = 0;
961 lp->rx_dlen[2] = 0;
962 lp->rx_dlen[3] = 0;
963 lp->rx_frames = 0;
964 lp->rx_error_count = 0;
965 lp->rx_error_dpc_count = 0;
966 lp->rx_session_id[0] = 0x50;
967 lp->rx_session_id[0] = 0x48;
968 lp->rx_session_id[0] = 0x44;
969 lp->rx_session_id[0] = 0x42;
970 lp->rx_frame_id[0] = 0;
971 lp->rx_frame_id[1] = 0;
972 lp->rx_frame_id[2] = 0;
973 lp->rx_frame_id[3] = 0;
974 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
975 return -EAGAIN;
978 if (sb1000_debug > 2)
979 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
981 /* Activate board and check firmware version */
982 udelay(1000);
983 if ((status = sb1000_activate(ioaddr, name)))
984 return status;
985 udelay(0);
986 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
987 return status;
988 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
989 printk(KERN_WARNING "%s: found firmware version %x.%02x "
990 "(should be %x.%02x)\n", name, version[0], version[1],
991 FirmwareVersion[0], FirmwareVersion[1]);
994 netif_start_queue(dev);
995 return 0; /* Always succeed */
998 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1000 char* name;
1001 unsigned char version[2];
1002 short PID[4];
1003 int ioaddr[2], status, frequency;
1004 unsigned int stats[5];
1005 struct sb1000_private *lp = netdev_priv(dev);
1007 if (!(dev && dev->flags & IFF_UP))
1008 return -ENODEV;
1010 ioaddr[0] = dev->base_addr;
1011 /* mem_start holds the second I/O address */
1012 ioaddr[1] = dev->mem_start;
1013 name = dev->name;
1015 switch (cmd) {
1016 case SIOCGCMSTATS: /* get statistics */
1017 stats[0] = dev->stats.rx_bytes;
1018 stats[1] = lp->rx_frames;
1019 stats[2] = dev->stats.rx_packets;
1020 stats[3] = dev->stats.rx_errors;
1021 stats[4] = dev->stats.rx_dropped;
1022 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1023 return -EFAULT;
1024 status = 0;
1025 break;
1027 case SIOCGCMFIRMWARE: /* get firmware version */
1028 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1029 return status;
1030 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1031 return -EFAULT;
1032 break;
1034 case SIOCGCMFREQUENCY: /* get frequency */
1035 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1036 return status;
1037 if(put_user(frequency, (int __user *) ifr->ifr_data))
1038 return -EFAULT;
1039 break;
1041 case SIOCSCMFREQUENCY: /* set frequency */
1042 if (!capable(CAP_NET_ADMIN))
1043 return -EPERM;
1044 if(get_user(frequency, (int __user *) ifr->ifr_data))
1045 return -EFAULT;
1046 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1047 return status;
1048 break;
1050 case SIOCGCMPIDS: /* get PIDs */
1051 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1052 return status;
1053 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1054 return -EFAULT;
1055 break;
1057 case SIOCSCMPIDS: /* set PIDs */
1058 if (!capable(CAP_NET_ADMIN))
1059 return -EPERM;
1060 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1061 return -EFAULT;
1062 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1063 return status;
1064 /* set session_id, frame_id and pkt_type too */
1065 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1066 lp->rx_session_id[1] = 0x48;
1067 lp->rx_session_id[2] = 0x44;
1068 lp->rx_session_id[3] = 0x42;
1069 lp->rx_frame_id[0] = 0;
1070 lp->rx_frame_id[1] = 0;
1071 lp->rx_frame_id[2] = 0;
1072 lp->rx_frame_id[3] = 0;
1073 break;
1075 default:
1076 status = -EINVAL;
1077 break;
1079 return status;
1082 /* transmit function: do nothing since SB1000 can't send anything out */
1083 static int
1084 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1086 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1087 /* sb1000 can't xmit datagrams */
1088 dev_kfree_skb(skb);
1089 return 0;
1092 /* SB1000 interrupt handler. */
1093 static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1095 static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1096 static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1098 char *name;
1099 unsigned char st;
1100 int ioaddr[2];
1101 struct net_device *dev = dev_id;
1102 struct sb1000_private *lp = netdev_priv(dev);
1104 const int MaxRxErrorCount = 6;
1106 ioaddr[0] = dev->base_addr;
1107 /* mem_start holds the second I/O address */
1108 ioaddr[1] = dev->mem_start;
1109 name = dev->name;
1111 /* is it a good interrupt? */
1112 st = inb(ioaddr[1] + 6);
1113 if (!(st & 0x08 && st & 0x20)) {
1114 return IRQ_NONE;
1117 if (sb1000_debug > 3)
1118 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1120 st = inb(ioaddr[0] + 7);
1121 if (sb1000_rx(dev))
1122 lp->rx_error_count++;
1123 #ifdef SB1000_DELAY
1124 udelay(SB1000_DELAY);
1125 #endif /* SB1000_DELAY */
1126 sb1000_issue_read_command(ioaddr, name);
1127 if (st & 0x01) {
1128 sb1000_error_dpc(dev);
1129 sb1000_issue_read_command(ioaddr, name);
1131 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1132 sb1000_wait_for_ready_clear(ioaddr, name);
1133 sb1000_send_command(ioaddr, name, Command0);
1134 sb1000_wait_for_ready(ioaddr, name);
1135 sb1000_issue_read_command(ioaddr, name);
1137 if (lp->rx_error_count >= MaxRxErrorCount) {
1138 sb1000_wait_for_ready_clear(ioaddr, name);
1139 sb1000_send_command(ioaddr, name, Command1);
1140 sb1000_wait_for_ready(ioaddr, name);
1141 sb1000_issue_read_command(ioaddr, name);
1142 lp->rx_error_count = 0;
1145 return IRQ_HANDLED;
1148 static int sb1000_close(struct net_device *dev)
1150 int i;
1151 int ioaddr[2];
1152 struct sb1000_private *lp = netdev_priv(dev);
1154 if (sb1000_debug > 2)
1155 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1157 netif_stop_queue(dev);
1159 ioaddr[0] = dev->base_addr;
1160 /* mem_start holds the second I/O address */
1161 ioaddr[1] = dev->mem_start;
1163 free_irq(dev->irq, dev);
1164 /* If we don't do this, we can't re-insmod it later. */
1165 release_region(ioaddr[1], SB1000_IO_EXTENT);
1166 release_region(ioaddr[0], SB1000_IO_EXTENT);
1168 /* free rx_skb's if needed */
1169 for (i=0; i<4; i++) {
1170 if (lp->rx_skb[i]) {
1171 dev_kfree_skb(lp->rx_skb[i]);
1174 return 0;
1177 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1178 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1179 MODULE_LICENSE("GPL");
1181 static int __init
1182 sb1000_init(void)
1184 return pnp_register_driver(&sb1000_driver);
1187 static void __exit
1188 sb1000_exit(void)
1190 pnp_unregister_driver(&sb1000_driver);
1193 module_init(sb1000_init);
1194 module_exit(sb1000_exit);