[CPUFREQ] ondemand,conservative governor store the idle ticks for all cpus
[linux-2.6/mini2440.git] / drivers / net / sb1000.c
blobe15369c8d1651dbd9d20ffa1724f29f47ce9e2e6
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 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];
79 struct net_device_stats stats;
82 /* prototypes for Linux interface */
83 extern int sb1000_probe(struct net_device *dev);
84 static int sb1000_open(struct net_device *dev);
85 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
86 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
87 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
88 static struct net_device_stats *sb1000_stats(struct net_device *dev);
89 static int sb1000_close(struct net_device *dev);
92 /* SB1000 hardware routines to be used during open/configuration phases */
93 static inline void nicedelay(unsigned long usecs);
94 static inline int card_wait_for_busy_clear(const int ioaddr[],
95 const char* name);
96 static inline int card_wait_for_ready(const int ioaddr[], const char* name,
97 unsigned char in[]);
98 static inline int card_send_command(const int ioaddr[], const char* name,
99 const unsigned char out[], unsigned char in[]);
101 /* SB1000 hardware routines to be used during frame rx interrupt */
102 static inline int sb1000_wait_for_ready(const int ioaddr[], const char* name);
103 static inline int sb1000_wait_for_ready_clear(const int ioaddr[],
104 const char* name);
105 static inline void sb1000_send_command(const int ioaddr[], const char* name,
106 const unsigned char out[]);
107 static inline void sb1000_read_status(const int ioaddr[], unsigned char in[]);
108 static inline void sb1000_issue_read_command(const int ioaddr[],
109 const char* name);
111 /* SB1000 commands for open/configuration */
112 static inline int sb1000_reset(const int ioaddr[], const char* name);
113 static inline int sb1000_check_CRC(const int ioaddr[], const char* name);
114 static inline int sb1000_start_get_set_command(const int ioaddr[],
115 const char* name);
116 static inline int sb1000_end_get_set_command(const int ioaddr[],
117 const char* name);
118 static inline int sb1000_activate(const int ioaddr[], const char* name);
119 static int sb1000_get_firmware_version(const int ioaddr[],
120 const char* name, unsigned char version[], int do_end);
121 static int sb1000_get_frequency(const int ioaddr[], const char* name,
122 int* frequency);
123 static int sb1000_set_frequency(const int ioaddr[], const char* name,
124 int frequency);
125 static int sb1000_get_PIDs(const int ioaddr[], const char* name,
126 short PID[]);
127 static int sb1000_set_PIDs(const int ioaddr[], const char* name,
128 const short PID[]);
130 /* SB1000 commands for frame rx interrupt */
131 static inline int sb1000_rx(struct net_device *dev);
132 static inline void sb1000_error_dpc(struct net_device *dev);
134 static const struct pnp_device_id sb1000_pnp_ids[] = {
135 { "GIC1000", 0 },
136 { "", 0 }
138 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
140 static int
141 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
143 struct net_device *dev;
144 unsigned short ioaddr[2], irq;
145 unsigned int serial_number;
146 int error = -ENODEV;
148 if (pnp_device_attach(pdev) < 0)
149 return -ENODEV;
150 if (pnp_activate_dev(pdev) < 0)
151 goto out_detach;
153 if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
154 goto out_disable;
155 if (!pnp_irq_valid(pdev, 0))
156 goto out_disable;
158 serial_number = pdev->card->serial;
160 ioaddr[0] = pnp_port_start(pdev, 0);
161 ioaddr[1] = pnp_port_start(pdev, 0);
163 irq = pnp_irq(pdev, 0);
165 if (!request_region(ioaddr[0], 16, "sb1000"))
166 goto out_disable;
167 if (!request_region(ioaddr[1], 16, "sb1000"))
168 goto out_release_region0;
170 dev = alloc_etherdev(sizeof(struct sb1000_private));
171 if (!dev) {
172 error = -ENOMEM;
173 goto out_release_regions;
177 dev->base_addr = ioaddr[0];
178 /* mem_start holds the second I/O address */
179 dev->mem_start = ioaddr[1];
180 dev->irq = irq;
182 if (sb1000_debug > 0)
183 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
184 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
185 dev->mem_start, serial_number, dev->irq);
188 * The SB1000 is an rx-only cable modem device. The uplink is a modem
189 * and we do not want to arp on it.
191 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
193 SET_MODULE_OWNER(dev);
194 SET_NETDEV_DEV(dev, &pdev->dev);
196 if (sb1000_debug > 0)
197 printk(KERN_NOTICE "%s", version);
199 /* The SB1000-specific entries in the device structure. */
200 dev->open = sb1000_open;
201 dev->do_ioctl = sb1000_dev_ioctl;
202 dev->hard_start_xmit = sb1000_start_xmit;
203 dev->stop = sb1000_close;
204 dev->get_stats = sb1000_stats;
206 /* hardware address is 0:0:serial_number */
207 dev->dev_addr[2] = serial_number >> 24 & 0xff;
208 dev->dev_addr[3] = serial_number >> 16 & 0xff;
209 dev->dev_addr[4] = serial_number >> 8 & 0xff;
210 dev->dev_addr[5] = serial_number >> 0 & 0xff;
212 pnp_set_drvdata(pdev, dev);
214 error = register_netdev(dev);
215 if (error)
216 goto out_free_netdev;
217 return 0;
219 out_free_netdev:
220 free_netdev(dev);
221 out_release_regions:
222 release_region(ioaddr[1], 16);
223 out_release_region0:
224 release_region(ioaddr[0], 16);
225 out_disable:
226 pnp_disable_dev(pdev);
227 out_detach:
228 pnp_device_detach(pdev);
229 return error;
232 static void
233 sb1000_remove_one(struct pnp_dev *pdev)
235 struct net_device *dev = pnp_get_drvdata(pdev);
237 unregister_netdev(dev);
238 release_region(dev->base_addr, 16);
239 release_region(dev->mem_start, 16);
240 free_netdev(dev);
243 static struct pnp_driver sb1000_driver = {
244 .name = "sb1000",
245 .id_table = sb1000_pnp_ids,
246 .probe = sb1000_probe_one,
247 .remove = sb1000_remove_one,
252 * SB1000 hardware routines to be used during open/configuration phases
255 static const int TimeOutJiffies = (875 * HZ) / 100;
257 static inline void nicedelay(unsigned long usecs)
259 current->state = TASK_INTERRUPTIBLE;
260 schedule_timeout(HZ);
261 return;
264 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
265 static inline int
266 card_wait_for_busy_clear(const int ioaddr[], const char* name)
268 unsigned char a;
269 unsigned long timeout;
271 a = inb(ioaddr[0] + 7);
272 timeout = jiffies + TimeOutJiffies;
273 while (a & 0x80 || a & 0x40) {
274 /* a little sleep */
275 yield();
277 a = inb(ioaddr[0] + 7);
278 if (time_after_eq(jiffies, timeout)) {
279 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
280 name);
281 return -ETIME;
285 return 0;
288 /* Card Wait For Ready (cannot be used during an interrupt) */
289 static inline int
290 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
292 unsigned char a;
293 unsigned long timeout;
295 a = inb(ioaddr[1] + 6);
296 timeout = jiffies + TimeOutJiffies;
297 while (a & 0x80 || !(a & 0x40)) {
298 /* a little sleep */
299 yield();
301 a = inb(ioaddr[1] + 6);
302 if (time_after_eq(jiffies, timeout)) {
303 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
304 name);
305 return -ETIME;
309 in[1] = inb(ioaddr[0] + 1);
310 in[2] = inb(ioaddr[0] + 2);
311 in[3] = inb(ioaddr[0] + 3);
312 in[4] = inb(ioaddr[0] + 4);
313 in[0] = inb(ioaddr[0] + 5);
314 in[6] = inb(ioaddr[0] + 6);
315 in[5] = inb(ioaddr[1] + 6);
316 return 0;
319 /* Card Send Command (cannot be used during an interrupt) */
320 static inline int
321 card_send_command(const int ioaddr[], const char* name,
322 const unsigned char out[], unsigned char in[])
324 int status, x;
326 if ((status = card_wait_for_busy_clear(ioaddr, name)))
327 return status;
328 outb(0xa0, ioaddr[0] + 6);
329 outb(out[2], ioaddr[0] + 1);
330 outb(out[3], ioaddr[0] + 2);
331 outb(out[4], ioaddr[0] + 3);
332 outb(out[5], ioaddr[0] + 4);
333 outb(out[1], ioaddr[0] + 5);
334 outb(0xa0, ioaddr[0] + 6);
335 outb(out[0], ioaddr[0] + 7);
336 if (out[0] != 0x20 && out[0] != 0x30) {
337 if ((status = card_wait_for_ready(ioaddr, name, in)))
338 return status;
339 inb(ioaddr[0] + 7);
340 if (sb1000_debug > 3)
341 printk(KERN_DEBUG "%s: card_send_command "
342 "out: %02x%02x%02x%02x%02x%02x "
343 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
344 out[0], out[1], out[2], out[3], out[4], out[5],
345 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
346 } else {
347 if (sb1000_debug > 3)
348 printk(KERN_DEBUG "%s: card_send_command "
349 "out: %02x%02x%02x%02x%02x%02x\n", name,
350 out[0], out[1], out[2], out[3], out[4], out[5]);
353 if (out[1] == 0x1b) {
354 x = (out[2] == 0x02);
355 } else {
356 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
357 return -EIO;
359 return 0;
364 * SB1000 hardware routines to be used during frame rx interrupt
366 static const int Sb1000TimeOutJiffies = 7 * HZ;
368 /* Card Wait For Ready (to be used during frame rx) */
369 static inline int
370 sb1000_wait_for_ready(const int ioaddr[], const char* name)
372 unsigned long timeout;
374 timeout = jiffies + Sb1000TimeOutJiffies;
375 while (inb(ioaddr[1] + 6) & 0x80) {
376 if (time_after_eq(jiffies, timeout)) {
377 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
378 name);
379 return -ETIME;
382 timeout = jiffies + Sb1000TimeOutJiffies;
383 while (!(inb(ioaddr[1] + 6) & 0x40)) {
384 if (time_after_eq(jiffies, timeout)) {
385 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
386 name);
387 return -ETIME;
390 inb(ioaddr[0] + 7);
391 return 0;
394 /* Card Wait For Ready Clear (to be used during frame rx) */
395 static inline int
396 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
398 unsigned long timeout;
400 timeout = jiffies + Sb1000TimeOutJiffies;
401 while (inb(ioaddr[1] + 6) & 0x80) {
402 if (time_after_eq(jiffies, timeout)) {
403 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
404 name);
405 return -ETIME;
408 timeout = jiffies + Sb1000TimeOutJiffies;
409 while (inb(ioaddr[1] + 6) & 0x40) {
410 if (time_after_eq(jiffies, timeout)) {
411 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
412 name);
413 return -ETIME;
416 return 0;
419 /* Card Send Command (to be used during frame rx) */
420 static inline void
421 sb1000_send_command(const int ioaddr[], const char* name,
422 const unsigned char out[])
424 outb(out[2], ioaddr[0] + 1);
425 outb(out[3], ioaddr[0] + 2);
426 outb(out[4], ioaddr[0] + 3);
427 outb(out[5], ioaddr[0] + 4);
428 outb(out[1], ioaddr[0] + 5);
429 outb(out[0], ioaddr[0] + 7);
430 if (sb1000_debug > 3)
431 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
432 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
433 return;
436 /* Card Read Status (to be used during frame rx) */
437 static inline void
438 sb1000_read_status(const int ioaddr[], unsigned char in[])
440 in[1] = inb(ioaddr[0] + 1);
441 in[2] = inb(ioaddr[0] + 2);
442 in[3] = inb(ioaddr[0] + 3);
443 in[4] = inb(ioaddr[0] + 4);
444 in[0] = inb(ioaddr[0] + 5);
445 return;
448 /* Issue Read Command (to be used during frame rx) */
449 static inline void
450 sb1000_issue_read_command(const int ioaddr[], const char* name)
452 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
454 sb1000_wait_for_ready_clear(ioaddr, name);
455 outb(0xa0, ioaddr[0] + 6);
456 sb1000_send_command(ioaddr, name, Command0);
457 return;
462 * SB1000 commands for open/configuration
464 /* reset SB1000 card */
465 static inline int
466 sb1000_reset(const int ioaddr[], const char* name)
468 unsigned char st[7];
469 int port, status;
470 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
472 port = ioaddr[1] + 6;
473 outb(0x4, port);
474 inb(port);
475 udelay(1000);
476 outb(0x0, port);
477 inb(port);
478 nicedelay(60000);
479 outb(0x4, port);
480 inb(port);
481 udelay(1000);
482 outb(0x0, port);
483 inb(port);
484 udelay(0);
486 if ((status = card_send_command(ioaddr, name, Command0, st)))
487 return status;
488 if (st[3] != 0xf0)
489 return -EIO;
490 return 0;
493 /* check SB1000 firmware CRC */
494 static inline int
495 sb1000_check_CRC(const int ioaddr[], const char* name)
497 unsigned char st[7];
498 int crc, status;
499 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
501 /* check CRC */
502 if ((status = card_send_command(ioaddr, name, Command0, st)))
503 return status;
504 if (st[1] != st[3] || st[2] != st[4])
505 return -EIO;
506 crc = st[1] << 8 | st[2];
507 return 0;
510 static inline int
511 sb1000_start_get_set_command(const int ioaddr[], const char* name)
513 unsigned char st[7];
514 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
516 return card_send_command(ioaddr, name, Command0, st);
519 static inline int
520 sb1000_end_get_set_command(const int ioaddr[], const char* name)
522 unsigned char st[7];
523 int status;
524 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
525 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
527 if ((status = card_send_command(ioaddr, name, Command0, st)))
528 return status;
529 return card_send_command(ioaddr, name, Command1, st);
532 static inline int
533 sb1000_activate(const int ioaddr[], const char* name)
535 unsigned char st[7];
536 int status;
537 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
538 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
540 nicedelay(50000);
541 if ((status = card_send_command(ioaddr, name, Command0, st)))
542 return status;
543 if ((status = card_send_command(ioaddr, name, Command1, st)))
544 return status;
545 if (st[3] != 0xf1) {
546 if ((status = sb1000_start_get_set_command(ioaddr, name)))
547 return status;
548 return -EIO;
550 udelay(1000);
551 return sb1000_start_get_set_command(ioaddr, name);
554 /* get SB1000 firmware version */
555 static int
556 sb1000_get_firmware_version(const int ioaddr[], const char* name,
557 unsigned char version[], int do_end)
559 unsigned char st[7];
560 int status;
561 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
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 unsigned char st[7];
582 int status;
583 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
585 udelay(1000);
586 if ((status = sb1000_start_get_set_command(ioaddr, name)))
587 return status;
588 if ((status = card_send_command(ioaddr, name, Command0, st)))
589 return status;
590 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
591 return sb1000_end_get_set_command(ioaddr, name);
594 /* set SB1000 frequency */
595 static int
596 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
598 unsigned char st[7];
599 int status;
600 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
602 const int FrequencyLowerLimit = 57000;
603 const int FrequencyUpperLimit = 804000;
605 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
606 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
607 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
608 FrequencyUpperLimit);
609 return -EINVAL;
611 udelay(1000);
612 if ((status = sb1000_start_get_set_command(ioaddr, name)))
613 return status;
614 Command0[5] = frequency & 0xff;
615 frequency >>= 8;
616 Command0[4] = frequency & 0xff;
617 frequency >>= 8;
618 Command0[3] = frequency & 0xff;
619 frequency >>= 8;
620 Command0[2] = frequency & 0xff;
621 return card_send_command(ioaddr, name, Command0, st);
624 /* get SB1000 PIDs */
625 static int
626 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
628 unsigned char st[7];
629 int status;
630 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
631 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
632 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
633 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
635 udelay(1000);
636 if ((status = sb1000_start_get_set_command(ioaddr, name)))
637 return status;
639 if ((status = card_send_command(ioaddr, name, Command0, st)))
640 return status;
641 PID[0] = st[1] << 8 | st[2];
643 if ((status = card_send_command(ioaddr, name, Command1, st)))
644 return status;
645 PID[1] = st[1] << 8 | st[2];
647 if ((status = card_send_command(ioaddr, name, Command2, st)))
648 return status;
649 PID[2] = st[1] << 8 | st[2];
651 if ((status = card_send_command(ioaddr, name, Command3, st)))
652 return status;
653 PID[3] = st[1] << 8 | st[2];
655 return sb1000_end_get_set_command(ioaddr, name);
658 /* set SB1000 PIDs */
659 static int
660 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
662 unsigned char st[7];
663 short p;
664 int status;
665 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
666 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
667 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
668 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
669 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
671 udelay(1000);
672 if ((status = sb1000_start_get_set_command(ioaddr, name)))
673 return status;
675 p = PID[0];
676 Command0[3] = p & 0xff;
677 p >>= 8;
678 Command0[2] = p & 0xff;
679 if ((status = card_send_command(ioaddr, name, Command0, st)))
680 return status;
682 p = PID[1];
683 Command1[3] = p & 0xff;
684 p >>= 8;
685 Command1[2] = p & 0xff;
686 if ((status = card_send_command(ioaddr, name, Command1, st)))
687 return status;
689 p = PID[2];
690 Command2[3] = p & 0xff;
691 p >>= 8;
692 Command2[2] = p & 0xff;
693 if ((status = card_send_command(ioaddr, name, Command2, st)))
694 return status;
696 p = PID[3];
697 Command3[3] = p & 0xff;
698 p >>= 8;
699 Command3[2] = p & 0xff;
700 if ((status = card_send_command(ioaddr, name, Command3, st)))
701 return status;
703 if ((status = card_send_command(ioaddr, name, Command4, st)))
704 return status;
705 return sb1000_end_get_set_command(ioaddr, name);
709 static inline void
710 sb1000_print_status_buffer(const char* name, unsigned char st[],
711 unsigned char buffer[], int size)
713 int i, j, k;
715 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
716 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
717 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
718 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
719 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
720 buffer[46] << 8 | buffer[47],
721 buffer[42], buffer[43], buffer[44], buffer[45],
722 buffer[48] << 8 | buffer[49]);
723 } else {
724 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
725 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
726 for (j = 0; j < 8 && k < size; j++, k++)
727 printk(" %02x", buffer[k]);
728 printk("\n");
731 return;
735 * SB1000 commands for frame rx interrupt
737 /* receive a single frame and assemble datagram
738 * (this is the heart of the interrupt routine)
740 static inline int
741 sb1000_rx(struct net_device *dev)
744 #define FRAMESIZE 184
745 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
746 short dlen;
747 int ioaddr, ns;
748 unsigned int skbsize;
749 struct sk_buff *skb;
750 struct sb1000_private *lp = netdev_priv(dev);
751 struct net_device_stats *stats = &lp->stats;
753 /* SB1000 frame constants */
754 const int FrameSize = FRAMESIZE;
755 const int NewDatagramHeaderSkip = 8;
756 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
757 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
758 const int ContDatagramHeaderSkip = 7;
759 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
760 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
761 const int TrailerSize = 4;
763 ioaddr = dev->base_addr;
765 insw(ioaddr, (unsigned short*) st, 1);
766 #ifdef XXXDEBUG
767 printk("cm0: received: %02x %02x\n", st[0], st[1]);
768 #endif /* XXXDEBUG */
769 lp->rx_frames++;
771 /* decide if it is a good or bad frame */
772 for (ns = 0; ns < NPIDS; ns++) {
773 session_id = lp->rx_session_id[ns];
774 frame_id = lp->rx_frame_id[ns];
775 if (st[0] == session_id) {
776 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
777 goto good_frame;
778 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
779 goto skipped_frame;
780 } else {
781 goto bad_frame;
783 } else if (st[0] == (session_id | 0x40)) {
784 if ((st[1] & 0xf0) == 0x30) {
785 goto skipped_frame;
786 } else {
787 goto bad_frame;
791 goto bad_frame;
793 skipped_frame:
794 stats->rx_frame_errors++;
795 skb = lp->rx_skb[ns];
796 if (sb1000_debug > 1)
797 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
798 "expecting %02x %02x\n", dev->name, st[0], st[1],
799 skb ? session_id : session_id | 0x40, frame_id);
800 if (skb) {
801 dev_kfree_skb(skb);
802 skb = NULL;
805 good_frame:
806 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
807 /* new datagram */
808 if (st[0] & 0x40) {
809 /* get data length */
810 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
811 #ifdef XXXDEBUG
812 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
813 #endif /* XXXDEBUG */
814 if (buffer[0] != NewDatagramHeaderSkip) {
815 if (sb1000_debug > 1)
816 printk(KERN_WARNING "%s: new datagram header skip error: "
817 "got %02x expecting %02x\n", dev->name, buffer[0],
818 NewDatagramHeaderSkip);
819 stats->rx_length_errors++;
820 insw(ioaddr, buffer, NewDatagramDataSize / 2);
821 goto bad_frame_next;
823 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
824 buffer[NewDatagramHeaderSkip + 4]) - 17;
825 if (dlen > SB1000_MRU) {
826 if (sb1000_debug > 1)
827 printk(KERN_WARNING "%s: datagram length (%d) greater "
828 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
829 stats->rx_length_errors++;
830 insw(ioaddr, buffer, NewDatagramDataSize / 2);
831 goto bad_frame_next;
833 lp->rx_dlen[ns] = dlen;
834 /* compute size to allocate for datagram */
835 skbsize = dlen + FrameSize;
836 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
837 if (sb1000_debug > 1)
838 printk(KERN_WARNING "%s: can't allocate %d bytes long "
839 "skbuff\n", dev->name, skbsize);
840 stats->rx_dropped++;
841 insw(ioaddr, buffer, NewDatagramDataSize / 2);
842 goto dropped_frame;
844 skb->dev = dev;
845 skb->mac.raw = skb->data;
846 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
847 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
848 NewDatagramDataSize / 2);
849 lp->rx_skb[ns] = skb;
850 } else {
851 /* continuation of previous datagram */
852 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
853 if (buffer[0] != ContDatagramHeaderSkip) {
854 if (sb1000_debug > 1)
855 printk(KERN_WARNING "%s: cont datagram header skip error: "
856 "got %02x expecting %02x\n", dev->name, buffer[0],
857 ContDatagramHeaderSkip);
858 stats->rx_length_errors++;
859 insw(ioaddr, buffer, ContDatagramDataSize / 2);
860 goto bad_frame_next;
862 skb = lp->rx_skb[ns];
863 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
864 ContDatagramDataSize / 2);
865 dlen = lp->rx_dlen[ns];
867 if (skb->len < dlen + TrailerSize) {
868 lp->rx_session_id[ns] &= ~0x40;
869 return 0;
872 /* datagram completed: send to upper level */
873 skb_trim(skb, dlen);
874 netif_rx(skb);
875 dev->last_rx = jiffies;
876 stats->rx_bytes+=dlen;
877 stats->rx_packets++;
878 lp->rx_skb[ns] = NULL;
879 lp->rx_session_id[ns] |= 0x40;
880 return 0;
882 bad_frame:
883 insw(ioaddr, buffer, FrameSize / 2);
884 if (sb1000_debug > 1)
885 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
886 dev->name, st[0], st[1]);
887 stats->rx_frame_errors++;
888 bad_frame_next:
889 if (sb1000_debug > 2)
890 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
891 dropped_frame:
892 stats->rx_errors++;
893 if (ns < NPIDS) {
894 if ((skb = lp->rx_skb[ns])) {
895 dev_kfree_skb(skb);
896 lp->rx_skb[ns] = NULL;
898 lp->rx_session_id[ns] |= 0x40;
900 return -1;
903 static inline void
904 sb1000_error_dpc(struct net_device *dev)
906 char *name;
907 unsigned char st[5];
908 int ioaddr[2];
909 struct sb1000_private *lp = netdev_priv(dev);
910 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
911 const int ErrorDpcCounterInitialize = 200;
913 ioaddr[0] = dev->base_addr;
914 /* mem_start holds the second I/O address */
915 ioaddr[1] = dev->mem_start;
916 name = dev->name;
918 sb1000_wait_for_ready_clear(ioaddr, name);
919 sb1000_send_command(ioaddr, name, Command0);
920 sb1000_wait_for_ready(ioaddr, name);
921 sb1000_read_status(ioaddr, st);
922 if (st[1] & 0x10)
923 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
924 return;
929 * Linux interface functions
931 static int
932 sb1000_open(struct net_device *dev)
934 char *name;
935 int ioaddr[2], status;
936 struct sb1000_private *lp = netdev_priv(dev);
937 const unsigned short FirmwareVersion[] = {0x01, 0x01};
939 ioaddr[0] = dev->base_addr;
940 /* mem_start holds the second I/O address */
941 ioaddr[1] = dev->mem_start;
942 name = dev->name;
944 /* initialize sb1000 */
945 if ((status = sb1000_reset(ioaddr, name)))
946 return status;
947 nicedelay(200000);
948 if ((status = sb1000_check_CRC(ioaddr, name)))
949 return status;
951 /* initialize private data before board can catch interrupts */
952 lp->rx_skb[0] = NULL;
953 lp->rx_skb[1] = NULL;
954 lp->rx_skb[2] = NULL;
955 lp->rx_skb[3] = NULL;
956 lp->rx_dlen[0] = 0;
957 lp->rx_dlen[1] = 0;
958 lp->rx_dlen[2] = 0;
959 lp->rx_dlen[3] = 0;
960 lp->rx_frames = 0;
961 lp->rx_error_count = 0;
962 lp->rx_error_dpc_count = 0;
963 lp->rx_session_id[0] = 0x50;
964 lp->rx_session_id[0] = 0x48;
965 lp->rx_session_id[0] = 0x44;
966 lp->rx_session_id[0] = 0x42;
967 lp->rx_frame_id[0] = 0;
968 lp->rx_frame_id[1] = 0;
969 lp->rx_frame_id[2] = 0;
970 lp->rx_frame_id[3] = 0;
971 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
972 return -EAGAIN;
975 if (sb1000_debug > 2)
976 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
978 /* Activate board and check firmware version */
979 udelay(1000);
980 if ((status = sb1000_activate(ioaddr, name)))
981 return status;
982 udelay(0);
983 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
984 return status;
985 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
986 printk(KERN_WARNING "%s: found firmware version %x.%02x "
987 "(should be %x.%02x)\n", name, version[0], version[1],
988 FirmwareVersion[0], FirmwareVersion[1]);
991 netif_start_queue(dev);
992 return 0; /* Always succeed */
995 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
997 char* name;
998 unsigned char version[2];
999 short PID[4];
1000 int ioaddr[2], status, frequency;
1001 unsigned int stats[5];
1002 struct sb1000_private *lp = netdev_priv(dev);
1004 if (!(dev && dev->flags & IFF_UP))
1005 return -ENODEV;
1007 ioaddr[0] = dev->base_addr;
1008 /* mem_start holds the second I/O address */
1009 ioaddr[1] = dev->mem_start;
1010 name = dev->name;
1012 switch (cmd) {
1013 case SIOCGCMSTATS: /* get statistics */
1014 stats[0] = lp->stats.rx_bytes;
1015 stats[1] = lp->rx_frames;
1016 stats[2] = lp->stats.rx_packets;
1017 stats[3] = lp->stats.rx_errors;
1018 stats[4] = lp->stats.rx_dropped;
1019 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1020 return -EFAULT;
1021 status = 0;
1022 break;
1024 case SIOCGCMFIRMWARE: /* get firmware version */
1025 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1026 return status;
1027 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1028 return -EFAULT;
1029 break;
1031 case SIOCGCMFREQUENCY: /* get frequency */
1032 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1033 return status;
1034 if(put_user(frequency, (int __user *) ifr->ifr_data))
1035 return -EFAULT;
1036 break;
1038 case SIOCSCMFREQUENCY: /* set frequency */
1039 if (!capable(CAP_NET_ADMIN))
1040 return -EPERM;
1041 if(get_user(frequency, (int __user *) ifr->ifr_data))
1042 return -EFAULT;
1043 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1044 return status;
1045 break;
1047 case SIOCGCMPIDS: /* get PIDs */
1048 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1049 return status;
1050 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1051 return -EFAULT;
1052 break;
1054 case SIOCSCMPIDS: /* set PIDs */
1055 if (!capable(CAP_NET_ADMIN))
1056 return -EPERM;
1057 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1058 return -EFAULT;
1059 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1060 return status;
1061 /* set session_id, frame_id and pkt_type too */
1062 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1063 lp->rx_session_id[1] = 0x48;
1064 lp->rx_session_id[2] = 0x44;
1065 lp->rx_session_id[3] = 0x42;
1066 lp->rx_frame_id[0] = 0;
1067 lp->rx_frame_id[1] = 0;
1068 lp->rx_frame_id[2] = 0;
1069 lp->rx_frame_id[3] = 0;
1070 break;
1072 default:
1073 status = -EINVAL;
1074 break;
1076 return status;
1079 /* transmit function: do nothing since SB1000 can't send anything out */
1080 static int
1081 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1083 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1084 /* sb1000 can't xmit datagrams */
1085 dev_kfree_skb(skb);
1086 return 0;
1089 /* SB1000 interrupt handler. */
1090 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1092 char *name;
1093 unsigned char st;
1094 int ioaddr[2];
1095 struct net_device *dev = (struct net_device *) dev_id;
1096 struct sb1000_private *lp = netdev_priv(dev);
1098 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1099 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1100 const int MaxRxErrorCount = 6;
1102 if (dev == NULL) {
1103 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1104 irq);
1105 return IRQ_NONE;
1108 ioaddr[0] = dev->base_addr;
1109 /* mem_start holds the second I/O address */
1110 ioaddr[1] = dev->mem_start;
1111 name = dev->name;
1113 /* is it a good interrupt? */
1114 st = inb(ioaddr[1] + 6);
1115 if (!(st & 0x08 && st & 0x20)) {
1116 return IRQ_NONE;
1119 if (sb1000_debug > 3)
1120 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1122 st = inb(ioaddr[0] + 7);
1123 if (sb1000_rx(dev))
1124 lp->rx_error_count++;
1125 #ifdef SB1000_DELAY
1126 udelay(SB1000_DELAY);
1127 #endif /* SB1000_DELAY */
1128 sb1000_issue_read_command(ioaddr, name);
1129 if (st & 0x01) {
1130 sb1000_error_dpc(dev);
1131 sb1000_issue_read_command(ioaddr, name);
1133 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1134 sb1000_wait_for_ready_clear(ioaddr, name);
1135 sb1000_send_command(ioaddr, name, Command0);
1136 sb1000_wait_for_ready(ioaddr, name);
1137 sb1000_issue_read_command(ioaddr, name);
1139 if (lp->rx_error_count >= MaxRxErrorCount) {
1140 sb1000_wait_for_ready_clear(ioaddr, name);
1141 sb1000_send_command(ioaddr, name, Command1);
1142 sb1000_wait_for_ready(ioaddr, name);
1143 sb1000_issue_read_command(ioaddr, name);
1144 lp->rx_error_count = 0;
1147 return IRQ_HANDLED;
1150 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1152 struct sb1000_private *lp = netdev_priv(dev);
1153 return &lp->stats;
1156 static int sb1000_close(struct net_device *dev)
1158 int i;
1159 int ioaddr[2];
1160 struct sb1000_private *lp = netdev_priv(dev);
1162 if (sb1000_debug > 2)
1163 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1165 netif_stop_queue(dev);
1167 ioaddr[0] = dev->base_addr;
1168 /* mem_start holds the second I/O address */
1169 ioaddr[1] = dev->mem_start;
1171 free_irq(dev->irq, dev);
1172 /* If we don't do this, we can't re-insmod it later. */
1173 release_region(ioaddr[1], SB1000_IO_EXTENT);
1174 release_region(ioaddr[0], SB1000_IO_EXTENT);
1176 /* free rx_skb's if needed */
1177 for (i=0; i<4; i++) {
1178 if (lp->rx_skb[i]) {
1179 dev_kfree_skb(lp->rx_skb[i]);
1182 return 0;
1185 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1186 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1187 MODULE_LICENSE("GPL");
1189 static int __init
1190 sb1000_init(void)
1192 return pnp_register_driver(&sb1000_driver);
1195 static void __exit
1196 sb1000_exit(void)
1198 pnp_unregister_driver(&sb1000_driver);
1201 module_init(sb1000_init);
1202 module_exit(sb1000_exit);