sh: sh7619 / sh7206 IPR initialize update
[linux-2.6/mini2440.git] / drivers / net / sb1000.c
blobb9fa4fbb13987fb32beb2b0cfffc463dbfdc4e12
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];
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);
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 int card_wait_for_busy_clear(const int ioaddr[],
94 const char* name);
95 static inline int card_wait_for_ready(const int ioaddr[], const char* name,
96 unsigned char in[]);
97 static int card_send_command(const int ioaddr[], const char* name,
98 const unsigned char out[], unsigned char in[]);
100 /* SB1000 hardware routines to be used during frame rx interrupt */
101 static inline int sb1000_wait_for_ready(const int ioaddr[], const char* name);
102 static inline int sb1000_wait_for_ready_clear(const int ioaddr[],
103 const char* name);
104 static inline void sb1000_send_command(const int ioaddr[], const char* name,
105 const unsigned char out[]);
106 static inline void sb1000_read_status(const int ioaddr[], unsigned char in[]);
107 static inline void sb1000_issue_read_command(const int ioaddr[],
108 const char* name);
110 /* SB1000 commands for open/configuration */
111 static inline int sb1000_reset(const int ioaddr[], const char* name);
112 static inline int sb1000_check_CRC(const int ioaddr[], const char* name);
113 static inline int sb1000_start_get_set_command(const int ioaddr[],
114 const char* name);
115 static inline int sb1000_end_get_set_command(const int ioaddr[],
116 const char* name);
117 static inline int sb1000_activate(const int ioaddr[], const char* name);
118 static int sb1000_get_firmware_version(const int ioaddr[],
119 const char* name, unsigned char version[], int do_end);
120 static int sb1000_get_frequency(const int ioaddr[], const char* name,
121 int* frequency);
122 static int sb1000_set_frequency(const int ioaddr[], const char* name,
123 int frequency);
124 static int sb1000_get_PIDs(const int ioaddr[], const char* name,
125 short PID[]);
126 static int sb1000_set_PIDs(const int ioaddr[], const char* name,
127 const short PID[]);
129 /* SB1000 commands for frame rx interrupt */
130 static inline int sb1000_rx(struct net_device *dev);
131 static inline void sb1000_error_dpc(struct net_device *dev);
133 static const struct pnp_device_id sb1000_pnp_ids[] = {
134 { "GIC1000", 0 },
135 { "", 0 }
137 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
139 static int
140 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
142 struct net_device *dev;
143 unsigned short ioaddr[2], irq;
144 unsigned int serial_number;
145 int error = -ENODEV;
147 if (pnp_device_attach(pdev) < 0)
148 return -ENODEV;
149 if (pnp_activate_dev(pdev) < 0)
150 goto out_detach;
152 if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
153 goto out_disable;
154 if (!pnp_irq_valid(pdev, 0))
155 goto out_disable;
157 serial_number = pdev->card->serial;
159 ioaddr[0] = pnp_port_start(pdev, 0);
160 ioaddr[1] = pnp_port_start(pdev, 0);
162 irq = pnp_irq(pdev, 0);
164 if (!request_region(ioaddr[0], 16, "sb1000"))
165 goto out_disable;
166 if (!request_region(ioaddr[1], 16, "sb1000"))
167 goto out_release_region0;
169 dev = alloc_etherdev(sizeof(struct sb1000_private));
170 if (!dev) {
171 error = -ENOMEM;
172 goto out_release_regions;
176 dev->base_addr = ioaddr[0];
177 /* mem_start holds the second I/O address */
178 dev->mem_start = ioaddr[1];
179 dev->irq = irq;
181 if (sb1000_debug > 0)
182 printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
183 "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
184 dev->mem_start, serial_number, dev->irq);
187 * The SB1000 is an rx-only cable modem device. The uplink is a modem
188 * and we do not want to arp on it.
190 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
192 SET_MODULE_OWNER(dev);
193 SET_NETDEV_DEV(dev, &pdev->dev);
195 if (sb1000_debug > 0)
196 printk(KERN_NOTICE "%s", version);
198 /* The SB1000-specific entries in the device structure. */
199 dev->open = sb1000_open;
200 dev->do_ioctl = sb1000_dev_ioctl;
201 dev->hard_start_xmit = sb1000_start_xmit;
202 dev->stop = sb1000_close;
203 dev->get_stats = sb1000_stats;
205 /* hardware address is 0:0:serial_number */
206 dev->dev_addr[2] = serial_number >> 24 & 0xff;
207 dev->dev_addr[3] = serial_number >> 16 & 0xff;
208 dev->dev_addr[4] = serial_number >> 8 & 0xff;
209 dev->dev_addr[5] = serial_number >> 0 & 0xff;
211 pnp_set_drvdata(pdev, dev);
213 error = register_netdev(dev);
214 if (error)
215 goto out_free_netdev;
216 return 0;
218 out_free_netdev:
219 free_netdev(dev);
220 out_release_regions:
221 release_region(ioaddr[1], 16);
222 out_release_region0:
223 release_region(ioaddr[0], 16);
224 out_disable:
225 pnp_disable_dev(pdev);
226 out_detach:
227 pnp_device_detach(pdev);
228 return error;
231 static void
232 sb1000_remove_one(struct pnp_dev *pdev)
234 struct net_device *dev = pnp_get_drvdata(pdev);
236 unregister_netdev(dev);
237 release_region(dev->base_addr, 16);
238 release_region(dev->mem_start, 16);
239 free_netdev(dev);
242 static struct pnp_driver sb1000_driver = {
243 .name = "sb1000",
244 .id_table = sb1000_pnp_ids,
245 .probe = sb1000_probe_one,
246 .remove = sb1000_remove_one,
251 * SB1000 hardware routines to be used during open/configuration phases
254 static const int TimeOutJiffies = (875 * HZ) / 100;
256 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
257 static inline int
258 card_wait_for_busy_clear(const int ioaddr[], const char* name)
260 unsigned char a;
261 unsigned long timeout;
263 a = inb(ioaddr[0] + 7);
264 timeout = jiffies + TimeOutJiffies;
265 while (a & 0x80 || a & 0x40) {
266 /* a little sleep */
267 yield();
269 a = inb(ioaddr[0] + 7);
270 if (time_after_eq(jiffies, timeout)) {
271 printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
272 name);
273 return -ETIME;
277 return 0;
280 /* Card Wait For Ready (cannot be used during an interrupt) */
281 static inline int
282 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
284 unsigned char a;
285 unsigned long timeout;
287 a = inb(ioaddr[1] + 6);
288 timeout = jiffies + TimeOutJiffies;
289 while (a & 0x80 || !(a & 0x40)) {
290 /* a little sleep */
291 yield();
293 a = inb(ioaddr[1] + 6);
294 if (time_after_eq(jiffies, timeout)) {
295 printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
296 name);
297 return -ETIME;
301 in[1] = inb(ioaddr[0] + 1);
302 in[2] = inb(ioaddr[0] + 2);
303 in[3] = inb(ioaddr[0] + 3);
304 in[4] = inb(ioaddr[0] + 4);
305 in[0] = inb(ioaddr[0] + 5);
306 in[6] = inb(ioaddr[0] + 6);
307 in[5] = inb(ioaddr[1] + 6);
308 return 0;
311 /* Card Send Command (cannot be used during an interrupt) */
312 static int
313 card_send_command(const int ioaddr[], const char* name,
314 const unsigned char out[], unsigned char in[])
316 int status, x;
318 if ((status = card_wait_for_busy_clear(ioaddr, name)))
319 return status;
320 outb(0xa0, ioaddr[0] + 6);
321 outb(out[2], ioaddr[0] + 1);
322 outb(out[3], ioaddr[0] + 2);
323 outb(out[4], ioaddr[0] + 3);
324 outb(out[5], ioaddr[0] + 4);
325 outb(out[1], ioaddr[0] + 5);
326 outb(0xa0, ioaddr[0] + 6);
327 outb(out[0], ioaddr[0] + 7);
328 if (out[0] != 0x20 && out[0] != 0x30) {
329 if ((status = card_wait_for_ready(ioaddr, name, in)))
330 return status;
331 inb(ioaddr[0] + 7);
332 if (sb1000_debug > 3)
333 printk(KERN_DEBUG "%s: card_send_command "
334 "out: %02x%02x%02x%02x%02x%02x "
335 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
336 out[0], out[1], out[2], out[3], out[4], out[5],
337 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
338 } else {
339 if (sb1000_debug > 3)
340 printk(KERN_DEBUG "%s: card_send_command "
341 "out: %02x%02x%02x%02x%02x%02x\n", name,
342 out[0], out[1], out[2], out[3], out[4], out[5]);
345 if (out[1] == 0x1b) {
346 x = (out[2] == 0x02);
347 } else {
348 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
349 return -EIO;
351 return 0;
356 * SB1000 hardware routines to be used during frame rx interrupt
358 static const int Sb1000TimeOutJiffies = 7 * HZ;
360 /* Card Wait For Ready (to be used during frame rx) */
361 static inline int
362 sb1000_wait_for_ready(const int ioaddr[], const char* name)
364 unsigned long timeout;
366 timeout = jiffies + Sb1000TimeOutJiffies;
367 while (inb(ioaddr[1] + 6) & 0x80) {
368 if (time_after_eq(jiffies, timeout)) {
369 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
370 name);
371 return -ETIME;
374 timeout = jiffies + Sb1000TimeOutJiffies;
375 while (!(inb(ioaddr[1] + 6) & 0x40)) {
376 if (time_after_eq(jiffies, timeout)) {
377 printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
378 name);
379 return -ETIME;
382 inb(ioaddr[0] + 7);
383 return 0;
386 /* Card Wait For Ready Clear (to be used during frame rx) */
387 static inline int
388 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
390 unsigned long timeout;
392 timeout = jiffies + Sb1000TimeOutJiffies;
393 while (inb(ioaddr[1] + 6) & 0x80) {
394 if (time_after_eq(jiffies, timeout)) {
395 printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
396 name);
397 return -ETIME;
400 timeout = jiffies + Sb1000TimeOutJiffies;
401 while (inb(ioaddr[1] + 6) & 0x40) {
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 return 0;
411 /* Card Send Command (to be used during frame rx) */
412 static inline void
413 sb1000_send_command(const int ioaddr[], const char* name,
414 const unsigned char out[])
416 outb(out[2], ioaddr[0] + 1);
417 outb(out[3], ioaddr[0] + 2);
418 outb(out[4], ioaddr[0] + 3);
419 outb(out[5], ioaddr[0] + 4);
420 outb(out[1], ioaddr[0] + 5);
421 outb(out[0], ioaddr[0] + 7);
422 if (sb1000_debug > 3)
423 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
424 "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
425 return;
428 /* Card Read Status (to be used during frame rx) */
429 static inline void
430 sb1000_read_status(const int ioaddr[], unsigned char in[])
432 in[1] = inb(ioaddr[0] + 1);
433 in[2] = inb(ioaddr[0] + 2);
434 in[3] = inb(ioaddr[0] + 3);
435 in[4] = inb(ioaddr[0] + 4);
436 in[0] = inb(ioaddr[0] + 5);
437 return;
440 /* Issue Read Command (to be used during frame rx) */
441 static inline void
442 sb1000_issue_read_command(const int ioaddr[], const char* name)
444 const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
446 sb1000_wait_for_ready_clear(ioaddr, name);
447 outb(0xa0, ioaddr[0] + 6);
448 sb1000_send_command(ioaddr, name, Command0);
449 return;
454 * SB1000 commands for open/configuration
456 /* reset SB1000 card */
457 static inline int
458 sb1000_reset(const int ioaddr[], const char* name)
460 unsigned char st[7];
461 int port, status;
462 const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
464 port = ioaddr[1] + 6;
465 outb(0x4, port);
466 inb(port);
467 udelay(1000);
468 outb(0x0, port);
469 inb(port);
470 ssleep(1);
471 outb(0x4, port);
472 inb(port);
473 udelay(1000);
474 outb(0x0, port);
475 inb(port);
476 udelay(0);
478 if ((status = card_send_command(ioaddr, name, Command0, st)))
479 return status;
480 if (st[3] != 0xf0)
481 return -EIO;
482 return 0;
485 /* check SB1000 firmware CRC */
486 static inline int
487 sb1000_check_CRC(const int ioaddr[], const char* name)
489 unsigned char st[7];
490 int crc, status;
491 const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
493 /* check CRC */
494 if ((status = card_send_command(ioaddr, name, Command0, st)))
495 return status;
496 if (st[1] != st[3] || st[2] != st[4])
497 return -EIO;
498 crc = st[1] << 8 | st[2];
499 return 0;
502 static inline int
503 sb1000_start_get_set_command(const int ioaddr[], const char* name)
505 unsigned char st[7];
506 const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
508 return card_send_command(ioaddr, name, Command0, st);
511 static inline int
512 sb1000_end_get_set_command(const int ioaddr[], const char* name)
514 unsigned char st[7];
515 int status;
516 const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
517 const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
519 if ((status = card_send_command(ioaddr, name, Command0, st)))
520 return status;
521 return card_send_command(ioaddr, name, Command1, st);
524 static inline int
525 sb1000_activate(const int ioaddr[], const char* name)
527 unsigned char st[7];
528 int status;
529 const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
530 const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
532 ssleep(1);
533 if ((status = card_send_command(ioaddr, name, Command0, st)))
534 return status;
535 if ((status = card_send_command(ioaddr, name, Command1, st)))
536 return status;
537 if (st[3] != 0xf1) {
538 if ((status = sb1000_start_get_set_command(ioaddr, name)))
539 return status;
540 return -EIO;
542 udelay(1000);
543 return sb1000_start_get_set_command(ioaddr, name);
546 /* get SB1000 firmware version */
547 static int
548 sb1000_get_firmware_version(const int ioaddr[], const char* name,
549 unsigned char version[], int do_end)
551 unsigned char st[7];
552 int status;
553 const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
555 if ((status = sb1000_start_get_set_command(ioaddr, name)))
556 return status;
557 if ((status = card_send_command(ioaddr, name, Command0, st)))
558 return status;
559 if (st[0] != 0xa3)
560 return -EIO;
561 version[0] = st[1];
562 version[1] = st[2];
563 if (do_end)
564 return sb1000_end_get_set_command(ioaddr, name);
565 else
566 return 0;
569 /* get SB1000 frequency */
570 static int
571 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
573 unsigned char st[7];
574 int status;
575 const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
577 udelay(1000);
578 if ((status = sb1000_start_get_set_command(ioaddr, name)))
579 return status;
580 if ((status = card_send_command(ioaddr, name, Command0, st)))
581 return status;
582 *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
583 return sb1000_end_get_set_command(ioaddr, name);
586 /* set SB1000 frequency */
587 static int
588 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
590 unsigned char st[7];
591 int status;
592 unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
594 const int FrequencyLowerLimit = 57000;
595 const int FrequencyUpperLimit = 804000;
597 if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
598 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
599 "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
600 FrequencyUpperLimit);
601 return -EINVAL;
603 udelay(1000);
604 if ((status = sb1000_start_get_set_command(ioaddr, name)))
605 return status;
606 Command0[5] = frequency & 0xff;
607 frequency >>= 8;
608 Command0[4] = frequency & 0xff;
609 frequency >>= 8;
610 Command0[3] = frequency & 0xff;
611 frequency >>= 8;
612 Command0[2] = frequency & 0xff;
613 return card_send_command(ioaddr, name, Command0, st);
616 /* get SB1000 PIDs */
617 static int
618 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
620 unsigned char st[7];
621 int status;
622 const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
623 const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
624 const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
625 const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
627 udelay(1000);
628 if ((status = sb1000_start_get_set_command(ioaddr, name)))
629 return status;
631 if ((status = card_send_command(ioaddr, name, Command0, st)))
632 return status;
633 PID[0] = st[1] << 8 | st[2];
635 if ((status = card_send_command(ioaddr, name, Command1, st)))
636 return status;
637 PID[1] = st[1] << 8 | st[2];
639 if ((status = card_send_command(ioaddr, name, Command2, st)))
640 return status;
641 PID[2] = st[1] << 8 | st[2];
643 if ((status = card_send_command(ioaddr, name, Command3, st)))
644 return status;
645 PID[3] = st[1] << 8 | st[2];
647 return sb1000_end_get_set_command(ioaddr, name);
650 /* set SB1000 PIDs */
651 static int
652 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
654 unsigned char st[7];
655 short p;
656 int status;
657 unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
658 unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
659 unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
660 unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
661 const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
663 udelay(1000);
664 if ((status = sb1000_start_get_set_command(ioaddr, name)))
665 return status;
667 p = PID[0];
668 Command0[3] = p & 0xff;
669 p >>= 8;
670 Command0[2] = p & 0xff;
671 if ((status = card_send_command(ioaddr, name, Command0, st)))
672 return status;
674 p = PID[1];
675 Command1[3] = p & 0xff;
676 p >>= 8;
677 Command1[2] = p & 0xff;
678 if ((status = card_send_command(ioaddr, name, Command1, st)))
679 return status;
681 p = PID[2];
682 Command2[3] = p & 0xff;
683 p >>= 8;
684 Command2[2] = p & 0xff;
685 if ((status = card_send_command(ioaddr, name, Command2, st)))
686 return status;
688 p = PID[3];
689 Command3[3] = p & 0xff;
690 p >>= 8;
691 Command3[2] = p & 0xff;
692 if ((status = card_send_command(ioaddr, name, Command3, st)))
693 return status;
695 if ((status = card_send_command(ioaddr, name, Command4, st)))
696 return status;
697 return sb1000_end_get_set_command(ioaddr, name);
701 static inline void
702 sb1000_print_status_buffer(const char* name, unsigned char st[],
703 unsigned char buffer[], int size)
705 int i, j, k;
707 printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
708 if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
709 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
710 "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
711 buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
712 buffer[46] << 8 | buffer[47],
713 buffer[42], buffer[43], buffer[44], buffer[45],
714 buffer[48] << 8 | buffer[49]);
715 } else {
716 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
717 printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
718 for (j = 0; j < 8 && k < size; j++, k++)
719 printk(" %02x", buffer[k]);
720 printk("\n");
723 return;
727 * SB1000 commands for frame rx interrupt
729 /* receive a single frame and assemble datagram
730 * (this is the heart of the interrupt routine)
732 static inline int
733 sb1000_rx(struct net_device *dev)
736 #define FRAMESIZE 184
737 unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
738 short dlen;
739 int ioaddr, ns;
740 unsigned int skbsize;
741 struct sk_buff *skb;
742 struct sb1000_private *lp = netdev_priv(dev);
743 struct net_device_stats *stats = &lp->stats;
745 /* SB1000 frame constants */
746 const int FrameSize = FRAMESIZE;
747 const int NewDatagramHeaderSkip = 8;
748 const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
749 const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
750 const int ContDatagramHeaderSkip = 7;
751 const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
752 const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
753 const int TrailerSize = 4;
755 ioaddr = dev->base_addr;
757 insw(ioaddr, (unsigned short*) st, 1);
758 #ifdef XXXDEBUG
759 printk("cm0: received: %02x %02x\n", st[0], st[1]);
760 #endif /* XXXDEBUG */
761 lp->rx_frames++;
763 /* decide if it is a good or bad frame */
764 for (ns = 0; ns < NPIDS; ns++) {
765 session_id = lp->rx_session_id[ns];
766 frame_id = lp->rx_frame_id[ns];
767 if (st[0] == session_id) {
768 if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
769 goto good_frame;
770 } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
771 goto skipped_frame;
772 } else {
773 goto bad_frame;
775 } else if (st[0] == (session_id | 0x40)) {
776 if ((st[1] & 0xf0) == 0x30) {
777 goto skipped_frame;
778 } else {
779 goto bad_frame;
783 goto bad_frame;
785 skipped_frame:
786 stats->rx_frame_errors++;
787 skb = lp->rx_skb[ns];
788 if (sb1000_debug > 1)
789 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
790 "expecting %02x %02x\n", dev->name, st[0], st[1],
791 skb ? session_id : session_id | 0x40, frame_id);
792 if (skb) {
793 dev_kfree_skb(skb);
794 skb = NULL;
797 good_frame:
798 lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
799 /* new datagram */
800 if (st[0] & 0x40) {
801 /* get data length */
802 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
803 #ifdef XXXDEBUG
804 printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
805 #endif /* XXXDEBUG */
806 if (buffer[0] != NewDatagramHeaderSkip) {
807 if (sb1000_debug > 1)
808 printk(KERN_WARNING "%s: new datagram header skip error: "
809 "got %02x expecting %02x\n", dev->name, buffer[0],
810 NewDatagramHeaderSkip);
811 stats->rx_length_errors++;
812 insw(ioaddr, buffer, NewDatagramDataSize / 2);
813 goto bad_frame_next;
815 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
816 buffer[NewDatagramHeaderSkip + 4]) - 17;
817 if (dlen > SB1000_MRU) {
818 if (sb1000_debug > 1)
819 printk(KERN_WARNING "%s: datagram length (%d) greater "
820 "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
821 stats->rx_length_errors++;
822 insw(ioaddr, buffer, NewDatagramDataSize / 2);
823 goto bad_frame_next;
825 lp->rx_dlen[ns] = dlen;
826 /* compute size to allocate for datagram */
827 skbsize = dlen + FrameSize;
828 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
829 if (sb1000_debug > 1)
830 printk(KERN_WARNING "%s: can't allocate %d bytes long "
831 "skbuff\n", dev->name, skbsize);
832 stats->rx_dropped++;
833 insw(ioaddr, buffer, NewDatagramDataSize / 2);
834 goto dropped_frame;
836 skb->dev = dev;
837 skb->mac.raw = skb->data;
838 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
839 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
840 NewDatagramDataSize / 2);
841 lp->rx_skb[ns] = skb;
842 } else {
843 /* continuation of previous datagram */
844 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
845 if (buffer[0] != ContDatagramHeaderSkip) {
846 if (sb1000_debug > 1)
847 printk(KERN_WARNING "%s: cont datagram header skip error: "
848 "got %02x expecting %02x\n", dev->name, buffer[0],
849 ContDatagramHeaderSkip);
850 stats->rx_length_errors++;
851 insw(ioaddr, buffer, ContDatagramDataSize / 2);
852 goto bad_frame_next;
854 skb = lp->rx_skb[ns];
855 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
856 ContDatagramDataSize / 2);
857 dlen = lp->rx_dlen[ns];
859 if (skb->len < dlen + TrailerSize) {
860 lp->rx_session_id[ns] &= ~0x40;
861 return 0;
864 /* datagram completed: send to upper level */
865 skb_trim(skb, dlen);
866 netif_rx(skb);
867 dev->last_rx = jiffies;
868 stats->rx_bytes+=dlen;
869 stats->rx_packets++;
870 lp->rx_skb[ns] = NULL;
871 lp->rx_session_id[ns] |= 0x40;
872 return 0;
874 bad_frame:
875 insw(ioaddr, buffer, FrameSize / 2);
876 if (sb1000_debug > 1)
877 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
878 dev->name, st[0], st[1]);
879 stats->rx_frame_errors++;
880 bad_frame_next:
881 if (sb1000_debug > 2)
882 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
883 dropped_frame:
884 stats->rx_errors++;
885 if (ns < NPIDS) {
886 if ((skb = lp->rx_skb[ns])) {
887 dev_kfree_skb(skb);
888 lp->rx_skb[ns] = NULL;
890 lp->rx_session_id[ns] |= 0x40;
892 return -1;
895 static inline void
896 sb1000_error_dpc(struct net_device *dev)
898 char *name;
899 unsigned char st[5];
900 int ioaddr[2];
901 struct sb1000_private *lp = netdev_priv(dev);
902 const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
903 const int ErrorDpcCounterInitialize = 200;
905 ioaddr[0] = dev->base_addr;
906 /* mem_start holds the second I/O address */
907 ioaddr[1] = dev->mem_start;
908 name = dev->name;
910 sb1000_wait_for_ready_clear(ioaddr, name);
911 sb1000_send_command(ioaddr, name, Command0);
912 sb1000_wait_for_ready(ioaddr, name);
913 sb1000_read_status(ioaddr, st);
914 if (st[1] & 0x10)
915 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
916 return;
921 * Linux interface functions
923 static int
924 sb1000_open(struct net_device *dev)
926 char *name;
927 int ioaddr[2], status;
928 struct sb1000_private *lp = netdev_priv(dev);
929 const unsigned short FirmwareVersion[] = {0x01, 0x01};
931 ioaddr[0] = dev->base_addr;
932 /* mem_start holds the second I/O address */
933 ioaddr[1] = dev->mem_start;
934 name = dev->name;
936 /* initialize sb1000 */
937 if ((status = sb1000_reset(ioaddr, name)))
938 return status;
939 ssleep(1);
940 if ((status = sb1000_check_CRC(ioaddr, name)))
941 return status;
943 /* initialize private data before board can catch interrupts */
944 lp->rx_skb[0] = NULL;
945 lp->rx_skb[1] = NULL;
946 lp->rx_skb[2] = NULL;
947 lp->rx_skb[3] = NULL;
948 lp->rx_dlen[0] = 0;
949 lp->rx_dlen[1] = 0;
950 lp->rx_dlen[2] = 0;
951 lp->rx_dlen[3] = 0;
952 lp->rx_frames = 0;
953 lp->rx_error_count = 0;
954 lp->rx_error_dpc_count = 0;
955 lp->rx_session_id[0] = 0x50;
956 lp->rx_session_id[0] = 0x48;
957 lp->rx_session_id[0] = 0x44;
958 lp->rx_session_id[0] = 0x42;
959 lp->rx_frame_id[0] = 0;
960 lp->rx_frame_id[1] = 0;
961 lp->rx_frame_id[2] = 0;
962 lp->rx_frame_id[3] = 0;
963 if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
964 return -EAGAIN;
967 if (sb1000_debug > 2)
968 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
970 /* Activate board and check firmware version */
971 udelay(1000);
972 if ((status = sb1000_activate(ioaddr, name)))
973 return status;
974 udelay(0);
975 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
976 return status;
977 if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
978 printk(KERN_WARNING "%s: found firmware version %x.%02x "
979 "(should be %x.%02x)\n", name, version[0], version[1],
980 FirmwareVersion[0], FirmwareVersion[1]);
983 netif_start_queue(dev);
984 return 0; /* Always succeed */
987 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
989 char* name;
990 unsigned char version[2];
991 short PID[4];
992 int ioaddr[2], status, frequency;
993 unsigned int stats[5];
994 struct sb1000_private *lp = netdev_priv(dev);
996 if (!(dev && dev->flags & IFF_UP))
997 return -ENODEV;
999 ioaddr[0] = dev->base_addr;
1000 /* mem_start holds the second I/O address */
1001 ioaddr[1] = dev->mem_start;
1002 name = dev->name;
1004 switch (cmd) {
1005 case SIOCGCMSTATS: /* get statistics */
1006 stats[0] = lp->stats.rx_bytes;
1007 stats[1] = lp->rx_frames;
1008 stats[2] = lp->stats.rx_packets;
1009 stats[3] = lp->stats.rx_errors;
1010 stats[4] = lp->stats.rx_dropped;
1011 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1012 return -EFAULT;
1013 status = 0;
1014 break;
1016 case SIOCGCMFIRMWARE: /* get firmware version */
1017 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1018 return status;
1019 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1020 return -EFAULT;
1021 break;
1023 case SIOCGCMFREQUENCY: /* get frequency */
1024 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1025 return status;
1026 if(put_user(frequency, (int __user *) ifr->ifr_data))
1027 return -EFAULT;
1028 break;
1030 case SIOCSCMFREQUENCY: /* set frequency */
1031 if (!capable(CAP_NET_ADMIN))
1032 return -EPERM;
1033 if(get_user(frequency, (int __user *) ifr->ifr_data))
1034 return -EFAULT;
1035 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1036 return status;
1037 break;
1039 case SIOCGCMPIDS: /* get PIDs */
1040 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1041 return status;
1042 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1043 return -EFAULT;
1044 break;
1046 case SIOCSCMPIDS: /* set PIDs */
1047 if (!capable(CAP_NET_ADMIN))
1048 return -EPERM;
1049 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1050 return -EFAULT;
1051 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1052 return status;
1053 /* set session_id, frame_id and pkt_type too */
1054 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1055 lp->rx_session_id[1] = 0x48;
1056 lp->rx_session_id[2] = 0x44;
1057 lp->rx_session_id[3] = 0x42;
1058 lp->rx_frame_id[0] = 0;
1059 lp->rx_frame_id[1] = 0;
1060 lp->rx_frame_id[2] = 0;
1061 lp->rx_frame_id[3] = 0;
1062 break;
1064 default:
1065 status = -EINVAL;
1066 break;
1068 return status;
1071 /* transmit function: do nothing since SB1000 can't send anything out */
1072 static int
1073 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1075 printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1076 /* sb1000 can't xmit datagrams */
1077 dev_kfree_skb(skb);
1078 return 0;
1081 /* SB1000 interrupt handler. */
1082 static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1084 char *name;
1085 unsigned char st;
1086 int ioaddr[2];
1087 struct net_device *dev = dev_id;
1088 struct sb1000_private *lp = netdev_priv(dev);
1090 const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1091 const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1092 const int MaxRxErrorCount = 6;
1094 ioaddr[0] = dev->base_addr;
1095 /* mem_start holds the second I/O address */
1096 ioaddr[1] = dev->mem_start;
1097 name = dev->name;
1099 /* is it a good interrupt? */
1100 st = inb(ioaddr[1] + 6);
1101 if (!(st & 0x08 && st & 0x20)) {
1102 return IRQ_NONE;
1105 if (sb1000_debug > 3)
1106 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1108 st = inb(ioaddr[0] + 7);
1109 if (sb1000_rx(dev))
1110 lp->rx_error_count++;
1111 #ifdef SB1000_DELAY
1112 udelay(SB1000_DELAY);
1113 #endif /* SB1000_DELAY */
1114 sb1000_issue_read_command(ioaddr, name);
1115 if (st & 0x01) {
1116 sb1000_error_dpc(dev);
1117 sb1000_issue_read_command(ioaddr, name);
1119 if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1120 sb1000_wait_for_ready_clear(ioaddr, name);
1121 sb1000_send_command(ioaddr, name, Command0);
1122 sb1000_wait_for_ready(ioaddr, name);
1123 sb1000_issue_read_command(ioaddr, name);
1125 if (lp->rx_error_count >= MaxRxErrorCount) {
1126 sb1000_wait_for_ready_clear(ioaddr, name);
1127 sb1000_send_command(ioaddr, name, Command1);
1128 sb1000_wait_for_ready(ioaddr, name);
1129 sb1000_issue_read_command(ioaddr, name);
1130 lp->rx_error_count = 0;
1133 return IRQ_HANDLED;
1136 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1138 struct sb1000_private *lp = netdev_priv(dev);
1139 return &lp->stats;
1142 static int sb1000_close(struct net_device *dev)
1144 int i;
1145 int ioaddr[2];
1146 struct sb1000_private *lp = netdev_priv(dev);
1148 if (sb1000_debug > 2)
1149 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1151 netif_stop_queue(dev);
1153 ioaddr[0] = dev->base_addr;
1154 /* mem_start holds the second I/O address */
1155 ioaddr[1] = dev->mem_start;
1157 free_irq(dev->irq, dev);
1158 /* If we don't do this, we can't re-insmod it later. */
1159 release_region(ioaddr[1], SB1000_IO_EXTENT);
1160 release_region(ioaddr[0], SB1000_IO_EXTENT);
1162 /* free rx_skb's if needed */
1163 for (i=0; i<4; i++) {
1164 if (lp->rx_skb[i]) {
1165 dev_kfree_skb(lp->rx_skb[i]);
1168 return 0;
1171 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1172 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1173 MODULE_LICENSE("GPL");
1175 static int __init
1176 sb1000_init(void)
1178 return pnp_register_driver(&sb1000_driver);
1181 static void __exit
1182 sb1000_exit(void)
1184 pnp_unregister_driver(&sb1000_driver);
1187 module_init(sb1000_init);
1188 module_exit(sb1000_exit);