More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / sb1000.c
blob4a151b4198c0b2cd3117f736f522a6fb15f057a8
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/version.h>
39 #include <linux/kernel.h>
40 #include <linux/string.h>
41 #include <linux/interrupt.h>
42 #include <linux/errno.h>
43 #include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
44 #include <linux/in.h>
45 #include <linux/slab.h>
46 #include <linux/ioport.h>
47 #include <linux/netdevice.h>
48 #include <linux/if_arp.h>
49 #include <linux/skbuff.h>
50 #include <linux/delay.h> /* for udelay() */
51 #include <linux/etherdevice.h>
52 #include <linux/pnp.h>
54 #include <asm/bitops.h>
55 #include <asm/io.h>
56 #include <asm/processor.h>
57 #include <asm/uaccess.h>
59 #ifdef SB1000_DEBUG
60 int sb1000_debug = SB1000_DEBUG;
61 #else
62 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 inline int sb1000_get_firmware_version(const int ioaddr[],
120 const char* name, unsigned char version[], int do_end);
121 static inline int sb1000_get_frequency(const int ioaddr[], const char* name,
122 int* frequency);
123 static inline int sb1000_set_frequency(const int ioaddr[], const char* name,
124 int frequency);
125 static inline int sb1000_get_PIDs(const int ioaddr[], const char* name,
126 short PID[]);
127 static inline 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_release_regions;
217 return 0;
219 out_release_regions:
220 release_region(ioaddr[1], 16);
221 out_release_region0:
222 release_region(ioaddr[0], 16);
223 out_disable:
224 pnp_disable_dev(pdev);
225 out_detach:
226 pnp_device_detach(pdev);
227 return error;
230 static void
231 sb1000_remove_one(struct pnp_dev *pdev)
233 struct net_device *dev = pnp_get_drvdata(pdev);
235 unregister_netdev(dev);
236 release_region(dev->base_addr, 16);
237 release_region(dev->mem_start, 16);
238 kfree(dev);
241 static struct pnp_driver sb1000_driver = {
242 .name = "sb1000",
243 .id_table = sb1000_pnp_ids,
244 .probe = sb1000_probe_one,
245 .remove = sb1000_remove_one,
250 * SB1000 hardware routines to be used during open/configuration phases
253 const int TimeOutJiffies = (875 * HZ) / 100;
255 static inline void nicedelay(unsigned long usecs)
257 current->state = TASK_INTERRUPTIBLE;
258 schedule_timeout(HZ);
259 return;
262 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
263 static inline int
264 card_wait_for_busy_clear(const int ioaddr[], const char* name)
266 unsigned char a;
267 unsigned long timeout;
269 a = inb(ioaddr[0] + 7);
270 timeout = jiffies + TimeOutJiffies;
271 while (a & 0x80 || a & 0x40) {
272 /* a little sleep */
273 yield();
275 a = inb(ioaddr[0] + 7);
276 if (time_after_eq(jiffies, timeout)) {
277 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
278 name);
279 return -ETIME;
283 return 0;
286 /* Card Wait For Ready (cannot be used during an interrupt) */
287 static inline int
288 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
290 unsigned char a;
291 unsigned long timeout;
293 a = inb(ioaddr[1] + 6);
294 timeout = jiffies + TimeOutJiffies;
295 while (a & 0x80 || !(a & 0x40)) {
296 /* a little sleep */
297 yield();
299 a = inb(ioaddr[1] + 6);
300 if (time_after_eq(jiffies, timeout)) {
301 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
302 name);
303 return -ETIME;
307 in[1] = inb(ioaddr[0] + 1);
308 in[2] = inb(ioaddr[0] + 2);
309 in[3] = inb(ioaddr[0] + 3);
310 in[4] = inb(ioaddr[0] + 4);
311 in[0] = inb(ioaddr[0] + 5);
312 in[6] = inb(ioaddr[0] + 6);
313 in[5] = inb(ioaddr[1] + 6);
314 return 0;
317 /* Card Send Command (cannot be used during an interrupt) */
318 static inline int
319 card_send_command(const int ioaddr[], const char* name,
320 const unsigned char out[], unsigned char in[])
322 int status, x;
324 if ((status = card_wait_for_busy_clear(ioaddr, name)))
325 return status;
326 outb(0xa0, ioaddr[0] + 6);
327 outb(out[2], ioaddr[0] + 1);
328 outb(out[3], ioaddr[0] + 2);
329 outb(out[4], ioaddr[0] + 3);
330 outb(out[5], ioaddr[0] + 4);
331 outb(out[1], ioaddr[0] + 5);
332 outb(0xa0, ioaddr[0] + 6);
333 outb(out[0], ioaddr[0] + 7);
334 if (out[0] != 0x20 && out[0] != 0x30) {
335 if ((status = card_wait_for_ready(ioaddr, name, in)))
336 return status;
337 inb(ioaddr[0] + 7);
338 if (sb1000_debug > 3)
339 printk(KERN_DEBUG "%s: card_send_command "
340 "out: %02x%02x%02x%02x%02x%02x "
341 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
342 out[0], out[1], out[2], out[3], out[4], out[5],
343 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
344 } else {
345 if (sb1000_debug > 3)
346 printk(KERN_DEBUG "%s: card_send_command "
347 "out: %02x%02x%02x%02x%02x%02x\n", name,
348 out[0], out[1], out[2], out[3], out[4], out[5]);
351 if (out[1] == 0x1b) {
352 x = (out[2] == 0x02);
353 } else {
354 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
355 return -EIO;
357 return 0;
362 * SB1000 hardware routines to be used during frame rx interrupt
364 const int Sb1000TimeOutJiffies = 7 * HZ;
366 /* Card Wait For Ready (to be used during frame rx) */
367 static inline int
368 sb1000_wait_for_ready(const int ioaddr[], const char* name)
370 unsigned long timeout;
372 timeout = jiffies + Sb1000TimeOutJiffies;
373 while (inb(ioaddr[1] + 6) & 0x80) {
374 if (time_after_eq(jiffies, timeout)) {
375 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
376 name);
377 return -ETIME;
380 timeout = jiffies + Sb1000TimeOutJiffies;
381 while (!(inb(ioaddr[1] + 6) & 0x40)) {
382 if (time_after_eq(jiffies, timeout)) {
383 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
384 name);
385 return -ETIME;
388 inb(ioaddr[0] + 7);
389 return 0;
392 /* Card Wait For Ready Clear (to be used during frame rx) */
393 static inline int
394 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
396 unsigned long timeout;
398 timeout = jiffies + Sb1000TimeOutJiffies;
399 while (inb(ioaddr[1] + 6) & 0x80) {
400 if (time_after_eq(jiffies, timeout)) {
401 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
402 name);
403 return -ETIME;
406 timeout = jiffies + Sb1000TimeOutJiffies;
407 while (inb(ioaddr[1] + 6) & 0x40) {
408 if (time_after_eq(jiffies, timeout)) {
409 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
410 name);
411 return -ETIME;
414 return 0;
417 /* Card Send Command (to be used during frame rx) */
418 static inline void
419 sb1000_send_command(const int ioaddr[], const char* name,
420 const unsigned char out[])
422 outb(out[2], ioaddr[0] + 1);
423 outb(out[3], ioaddr[0] + 2);
424 outb(out[4], ioaddr[0] + 3);
425 outb(out[5], ioaddr[0] + 4);
426 outb(out[1], ioaddr[0] + 5);
427 outb(out[0], ioaddr[0] + 7);
428 if (sb1000_debug > 3)
429 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
430 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
431 return;
434 /* Card Read Status (to be used during frame rx) */
435 static inline void
436 sb1000_read_status(const int ioaddr[], unsigned char in[])
438 in[1] = inb(ioaddr[0] + 1);
439 in[2] = inb(ioaddr[0] + 2);
440 in[3] = inb(ioaddr[0] + 3);
441 in[4] = inb(ioaddr[0] + 4);
442 in[0] = inb(ioaddr[0] + 5);
443 return;
446 /* Issue Read Command (to be used during frame rx) */
447 static inline void
448 sb1000_issue_read_command(const int ioaddr[], const char* name)
450 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
452 sb1000_wait_for_ready_clear(ioaddr, name);
453 outb(0xa0, ioaddr[0] + 6);
454 sb1000_send_command(ioaddr, name, Command0);
455 return;
460 * SB1000 commands for open/configuration
462 /* reset SB1000 card */
463 static inline int
464 sb1000_reset(const int ioaddr[], const char* name)
466 unsigned char st[7];
467 int port, status;
468 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
470 port = ioaddr[1] + 6;
471 outb(0x4, port);
472 inb(port);
473 udelay(1000);
474 outb(0x0, port);
475 inb(port);
476 nicedelay(60000);
477 outb(0x4, port);
478 inb(port);
479 udelay(1000);
480 outb(0x0, port);
481 inb(port);
482 udelay(0);
484 if ((status = card_send_command(ioaddr, name, Command0, st)))
485 return status;
486 if (st[3] != 0xf0)
487 return -EIO;
488 return 0;
491 /* check SB1000 firmware CRC */
492 static inline int
493 sb1000_check_CRC(const int ioaddr[], const char* name)
495 unsigned char st[7];
496 int crc, status;
497 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
499 /* check CRC */
500 if ((status = card_send_command(ioaddr, name, Command0, st)))
501 return status;
502 if (st[1] != st[3] || st[2] != st[4])
503 return -EIO;
504 crc = st[1] << 8 | st[2];
505 return 0;
508 static inline int
509 sb1000_start_get_set_command(const int ioaddr[], const char* name)
511 unsigned char st[7];
512 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
514 return card_send_command(ioaddr, name, Command0, st);
517 static inline int
518 sb1000_end_get_set_command(const int ioaddr[], const char* name)
520 unsigned char st[7];
521 int status;
522 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
523 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
525 if ((status = card_send_command(ioaddr, name, Command0, st)))
526 return status;
527 return card_send_command(ioaddr, name, Command1, st);
530 static inline int
531 sb1000_activate(const int ioaddr[], const char* name)
533 unsigned char st[7];
534 int status;
535 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
536 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
538 nicedelay(50000);
539 if ((status = card_send_command(ioaddr, name, Command0, st)))
540 return status;
541 if ((status = card_send_command(ioaddr, name, Command1, st)))
542 return status;
543 if (st[3] != 0xf1) {
544 if ((status = sb1000_start_get_set_command(ioaddr, name)))
545 return status;
546 return -EIO;
548 udelay(1000);
549 return sb1000_start_get_set_command(ioaddr, name);
552 /* get SB1000 firmware version */
553 static inline int
554 sb1000_get_firmware_version(const int ioaddr[], const char* name,
555 unsigned char version[], int do_end)
557 unsigned char st[7];
558 int status;
559 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
561 if ((status = sb1000_start_get_set_command(ioaddr, name)))
562 return status;
563 if ((status = card_send_command(ioaddr, name, Command0, st)))
564 return status;
565 if (st[0] != 0xa3)
566 return -EIO;
567 version[0] = st[1];
568 version[1] = st[2];
569 if (do_end)
570 return sb1000_end_get_set_command(ioaddr, name);
571 else
572 return 0;
575 /* get SB1000 frequency */
576 static inline int
577 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
579 unsigned char st[7];
580 int status;
581 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
583 udelay(1000);
584 if ((status = sb1000_start_get_set_command(ioaddr, name)))
585 return status;
586 if ((status = card_send_command(ioaddr, name, Command0, st)))
587 return status;
588 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
589 return sb1000_end_get_set_command(ioaddr, name);
592 /* set SB1000 frequency */
593 static inline int
594 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
596 unsigned char st[7];
597 int status;
598 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
600 const int FrequencyLowerLimit = 57000;
601 const int FrequencyUpperLimit = 804000;
603 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
604 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
605 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
606 FrequencyUpperLimit);
607 return -EINVAL;
609 udelay(1000);
610 if ((status = sb1000_start_get_set_command(ioaddr, name)))
611 return status;
612 Command0[5] = frequency & 0xff;
613 frequency >>= 8;
614 Command0[4] = frequency & 0xff;
615 frequency >>= 8;
616 Command0[3] = frequency & 0xff;
617 frequency >>= 8;
618 Command0[2] = frequency & 0xff;
619 return card_send_command(ioaddr, name, Command0, st);
622 /* get SB1000 PIDs */
623 static inline int
624 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
626 unsigned char st[7];
627 int status;
628 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
629 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
630 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
631 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
633 udelay(1000);
634 if ((status = sb1000_start_get_set_command(ioaddr, name)))
635 return status;
637 if ((status = card_send_command(ioaddr, name, Command0, st)))
638 return status;
639 PID[0] = st[1] << 8 | st[2];
641 if ((status = card_send_command(ioaddr, name, Command1, st)))
642 return status;
643 PID[1] = st[1] << 8 | st[2];
645 if ((status = card_send_command(ioaddr, name, Command2, st)))
646 return status;
647 PID[2] = st[1] << 8 | st[2];
649 if ((status = card_send_command(ioaddr, name, Command3, st)))
650 return status;
651 PID[3] = st[1] << 8 | st[2];
653 return sb1000_end_get_set_command(ioaddr, name);
656 /* set SB1000 PIDs */
657 static inline int
658 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
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};
667 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
669 udelay(1000);
670 if ((status = sb1000_start_get_set_command(ioaddr, name)))
671 return status;
673 p = PID[0];
674 Command0[3] = p & 0xff;
675 p >>= 8;
676 Command0[2] = p & 0xff;
677 if ((status = card_send_command(ioaddr, name, Command0, st)))
678 return status;
680 p = PID[1];
681 Command1[3] = p & 0xff;
682 p >>= 8;
683 Command1[2] = p & 0xff;
684 if ((status = card_send_command(ioaddr, name, Command1, st)))
685 return status;
687 p = PID[2];
688 Command2[3] = p & 0xff;
689 p >>= 8;
690 Command2[2] = p & 0xff;
691 if ((status = card_send_command(ioaddr, name, Command2, st)))
692 return status;
694 p = PID[3];
695 Command3[3] = p & 0xff;
696 p >>= 8;
697 Command3[2] = p & 0xff;
698 if ((status = card_send_command(ioaddr, name, Command3, st)))
699 return status;
701 if ((status = card_send_command(ioaddr, name, Command4, st)))
702 return status;
703 return sb1000_end_get_set_command(ioaddr, name);
707 static inline void
708 sb1000_print_status_buffer(const char* name, unsigned char st[],
709 unsigned char buffer[], int size)
711 int i, j, k;
713 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
714 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
715 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
716 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
717 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
718 buffer[46] << 8 | buffer[47],
719 buffer[42], buffer[43], buffer[44], buffer[45],
720 buffer[48] << 8 | buffer[49]);
721 } else {
722 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
723 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
724 for (j = 0; j < 8 && k < size; j++, k++)
725 printk(" %02x", buffer[k]);
726 printk("\n");
729 return;
733 * SB1000 commands for frame rx interrupt
735 /* receive a single frame and assemble datagram
736 * (this is the heart of the interrupt routine)
738 static inline int
739 sb1000_rx(struct net_device *dev)
742 #define FRAMESIZE 184
743 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
744 short dlen;
745 int ioaddr, ns;
746 unsigned int skbsize;
747 struct sk_buff *skb;
748 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
749 struct net_device_stats *stats = &lp->stats;
751 /* SB1000 frame constants */
752 const int FrameSize = FRAMESIZE;
753 const int NewDatagramHeaderSkip = 8;
754 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
755 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
756 const int ContDatagramHeaderSkip = 7;
757 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
758 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
759 const int TrailerSize = 4;
761 ioaddr = dev->base_addr;
763 insw(ioaddr, (unsigned short*) st, 1);
764 #ifdef XXXDEBUG
765 printk("cm0: received: %02x %02x\n", st[0], st[1]);
766 #endif /* XXXDEBUG */
767 lp->rx_frames++;
769 /* decide if it is a good or bad frame */
770 for (ns = 0; ns < NPIDS; ns++) {
771 session_id = lp->rx_session_id[ns];
772 frame_id = lp->rx_frame_id[ns];
773 if (st[0] == session_id) {
774 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
775 goto good_frame;
776 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
777 goto skipped_frame;
778 } else {
779 goto bad_frame;
781 } else if (st[0] == (session_id | 0x40)) {
782 if ((st[1] & 0xf0) == 0x30) {
783 goto skipped_frame;
784 } else {
785 goto bad_frame;
789 goto bad_frame;
791 skipped_frame:
792 stats->rx_frame_errors++;
793 skb = lp->rx_skb[ns];
794 if (sb1000_debug > 1)
795 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
796 "expecting %02x %02x\n", dev->name, st[0], st[1],
797 skb ? session_id : session_id | 0x40, frame_id);
798 if (skb) {
799 dev_kfree_skb(skb);
800 skb = 0;
803 good_frame:
804 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
805 /* new datagram */
806 if (st[0] & 0x40) {
807 /* get data length */
808 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
809 #ifdef XXXDEBUG
810 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
811 #endif /* XXXDEBUG */
812 if (buffer[0] != NewDatagramHeaderSkip) {
813 if (sb1000_debug > 1)
814 printk(KERN_WARNING "%s: new datagram header skip error: "
815 "got %02x expecting %02x\n", dev->name, buffer[0],
816 NewDatagramHeaderSkip);
817 stats->rx_length_errors++;
818 insw(ioaddr, buffer, NewDatagramDataSize / 2);
819 goto bad_frame_next;
821 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
822 buffer[NewDatagramHeaderSkip + 4]) - 17;
823 if (dlen > SB1000_MRU) {
824 if (sb1000_debug > 1)
825 printk(KERN_WARNING "%s: datagram length (%d) greater "
826 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
827 stats->rx_length_errors++;
828 insw(ioaddr, buffer, NewDatagramDataSize / 2);
829 goto bad_frame_next;
831 lp->rx_dlen[ns] = dlen;
832 /* compute size to allocate for datagram */
833 skbsize = dlen + FrameSize;
834 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
835 if (sb1000_debug > 1)
836 printk(KERN_WARNING "%s: can't allocate %d bytes long "
837 "skbuff\n", dev->name, skbsize);
838 stats->rx_dropped++;
839 insw(ioaddr, buffer, NewDatagramDataSize / 2);
840 goto dropped_frame;
842 skb->dev = dev;
843 skb->mac.raw = skb->data;
844 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
845 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
846 NewDatagramDataSize / 2);
847 lp->rx_skb[ns] = skb;
848 } else {
849 /* continuation of previous datagram */
850 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
851 if (buffer[0] != ContDatagramHeaderSkip) {
852 if (sb1000_debug > 1)
853 printk(KERN_WARNING "%s: cont datagram header skip error: "
854 "got %02x expecting %02x\n", dev->name, buffer[0],
855 ContDatagramHeaderSkip);
856 stats->rx_length_errors++;
857 insw(ioaddr, buffer, ContDatagramDataSize / 2);
858 goto bad_frame_next;
860 skb = lp->rx_skb[ns];
861 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
862 ContDatagramDataSize / 2);
863 dlen = lp->rx_dlen[ns];
865 if (skb->len < dlen + TrailerSize) {
866 lp->rx_session_id[ns] &= ~0x40;
867 return 0;
870 /* datagram completed: send to upper level */
871 skb_trim(skb, dlen);
872 netif_rx(skb);
873 dev->last_rx = jiffies;
874 stats->rx_bytes+=dlen;
875 stats->rx_packets++;
876 lp->rx_skb[ns] = 0;
877 lp->rx_session_id[ns] |= 0x40;
878 return 0;
880 bad_frame:
881 insw(ioaddr, buffer, FrameSize / 2);
882 if (sb1000_debug > 1)
883 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
884 dev->name, st[0], st[1]);
885 stats->rx_frame_errors++;
886 bad_frame_next:
887 if (sb1000_debug > 2)
888 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
889 dropped_frame:
890 stats->rx_errors++;
891 if (ns < NPIDS) {
892 if ((skb = lp->rx_skb[ns])) {
893 dev_kfree_skb(skb);
894 lp->rx_skb[ns] = 0;
896 lp->rx_session_id[ns] |= 0x40;
898 return -1;
901 static inline void
902 sb1000_error_dpc(struct net_device *dev)
904 char *name;
905 unsigned char st[5];
906 int ioaddr[2];
907 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
908 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
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 = (struct sb1000_private *)dev->priv;
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 nicedelay(200000);
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 = (struct sb1000_private *)dev->priv;
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] = lp->stats.rx_bytes;
1013 stats[1] = lp->rx_frames;
1014 stats[2] = lp->stats.rx_packets;
1015 stats[3] = lp->stats.rx_errors;
1016 stats[4] = lp->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*) 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*) 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, struct pt_regs *regs)
1090 char *name;
1091 unsigned char st;
1092 int ioaddr[2];
1093 struct net_device *dev = (struct net_device *) dev_id;
1094 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1096 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1097 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1098 const int MaxRxErrorCount = 6;
1100 if (dev == NULL) {
1101 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1102 irq);
1103 return IRQ_NONE;
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 struct net_device_stats *sb1000_stats(struct net_device *dev)
1150 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1151 return &lp->stats;
1154 static int sb1000_close(struct net_device *dev)
1156 int i;
1157 int ioaddr[2];
1158 struct sb1000_private *lp = (struct sb1000_private *)dev->priv;
1160 if (sb1000_debug > 2)
1161 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1163 netif_stop_queue(dev);
1165 ioaddr[0] = dev->base_addr;
1166 /* mem_start holds the second I/O address */
1167 ioaddr[1] = dev->mem_start;
1169 free_irq(dev->irq, dev);
1170 /* If we don't do this, we can't re-insmod it later. */
1171 release_region(ioaddr[1], SB1000_IO_EXTENT);
1172 release_region(ioaddr[0], SB1000_IO_EXTENT);
1174 /* free rx_skb's if needed */
1175 for (i=0; i<4; i++) {
1176 if (lp->rx_skb[i]) {
1177 dev_kfree_skb(lp->rx_skb[i]);
1180 return 0;
1183 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1184 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1185 MODULE_LICENSE("GPL");
1187 static int __init
1188 sb1000_init(void)
1190 return pnp_register_driver(&sb1000_driver);
1193 static void __exit
1194 sb1000_exit(void)
1196 pnp_unregister_driver(&sb1000_driver);
1199 module_init(sb1000_init);
1200 module_exit(sb1000_exit);