ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / sb1000.c
1 /* sb1000.c: A General Instruments SB1000 driver for linux. */
2 /*
3         Written 1998 by Franco Venturi.
4
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.
9
10         This driver is for the General Instruments SB1000 (internal SURFboard)
11
12         The author may be reached as fventuri@mediaone.net
13
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.
19
20         Changes:
21
22         981115 Steven Hirsch <shirsch@adelphia.net>
23
24         Linus changed the timer interface.  Should work on all recent
25         development kernels.
26
27         980608 Steven Hirsch <shirsch@adelphia.net>
28
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.
31         
32         Merged with 2.2 - Alan Cox
33 */
34
35 static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)\n";
36
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
53 #include <asm/bitops.h>
54 #include <asm/io.h>
55 #include <asm/processor.h>
56 #include <asm/uaccess.h>
57
58 #ifdef SB1000_DEBUG
59 int sb1000_debug = SB1000_DEBUG;
60 #else
61 int sb1000_debug = 1;
62 #endif
63
64 static const int SB1000_IO_EXTENT = 8;
65 /* SB1000 Maximum Receive Unit */
66 static const int SB1000_MRU = 1500; /* octects */
67
68 #define NPIDS 4
69 struct sb1000_private {
70         struct sk_buff *rx_skb[NPIDS];
71         short rx_dlen[NPIDS];
72         unsigned int rx_frames;
73         short rx_error_count;
74         short rx_error_dpc_count;
75         unsigned char rx_session_id[NPIDS];
76         unsigned char rx_frame_id[NPIDS];
77         unsigned char rx_pkt_type[NPIDS];
78         struct net_device_stats stats;
79 };
80
81 /* prototypes for Linux interface */
82 extern int sb1000_probe(struct net_device *dev);
83 static int sb1000_open(struct net_device *dev);
84 static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
85 static int sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev);
86 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs);
87 static struct net_device_stats *sb1000_stats(struct net_device *dev);
88 static int sb1000_close(struct net_device *dev);
89
90
91 /* SB1000 hardware routines to be used during open/configuration phases */
92 static inline void nicedelay(unsigned long usecs);
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 inline int card_send_command(const int ioaddr[], const char* name,
98         const unsigned char out[], unsigned char in[]);
99
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);
109
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 inline int sb1000_get_firmware_version(const int ioaddr[],
119         const char* name, unsigned char version[], int do_end);
120 static inline int sb1000_get_frequency(const int ioaddr[], const char* name,
121         int* frequency);
122 static inline int sb1000_set_frequency(const int ioaddr[], const char* name,
123         int frequency);
124 static inline int sb1000_get_PIDs(const int ioaddr[], const char* name,
125         short PID[]);
126 static inline int sb1000_set_PIDs(const int ioaddr[], const char* name,
127         const short PID[]);
128
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);
132
133 static const struct pnp_device_id sb1000_pnp_ids[] = {
134         { "GIC1000", 0 },
135         { "", 0 }
136 };
137 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
138
139 static int
140 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
141 {
142         struct net_device *dev;
143         unsigned short ioaddr[2], irq;
144         unsigned int serial_number;
145         int error = -ENODEV;
146         
147         if (pnp_device_attach(pdev) < 0)
148                 return -ENODEV;
149         if (pnp_activate_dev(pdev) < 0)
150                 goto out_detach;
151
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;
156                 
157         serial_number = pdev->card->serial;
158                 
159         ioaddr[0] = pnp_port_start(pdev, 0);
160         ioaddr[1] = pnp_port_start(pdev, 0);
161                 
162         irq = pnp_irq(pdev, 0);
163
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;
168
169         dev = alloc_etherdev(sizeof(struct sb1000_private));
170         if (!dev) {
171                 error = -ENOMEM;
172                 goto out_release_regions;
173         }
174
175                  
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;
180
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);
185
186         /*
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.
189          */
190         dev->flags = IFF_POINTOPOINT|IFF_NOARP;
191
192         SET_MODULE_OWNER(dev);
193         SET_NETDEV_DEV(dev, &pdev->dev);
194
195         if (sb1000_debug > 0)
196                 printk(KERN_NOTICE "%s", version);
197
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;
204
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;
210
211         pnp_set_drvdata(pdev, dev);
212
213         error = register_netdev(dev);
214         if (error)
215                 goto out_free_netdev;
216         return 0;
217
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;
229 }
230
231 static void
232 sb1000_remove_one(struct pnp_dev *pdev)
233 {
234         struct net_device *dev = pnp_get_drvdata(pdev);
235
236         unregister_netdev(dev);
237         release_region(dev->base_addr, 16);
238         release_region(dev->mem_start, 16);
239         free_netdev(dev);
240 }
241
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,
247 };
248
249 \f
250 /*
251  * SB1000 hardware routines to be used during open/configuration phases
252  */
253
254 const int TimeOutJiffies = (875 * HZ) / 100;
255
256 static inline void nicedelay(unsigned long usecs)
257 {
258         current->state = TASK_INTERRUPTIBLE;
259         schedule_timeout(HZ);
260         return;
261 }
262
263 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
264 static inline int
265 card_wait_for_busy_clear(const int ioaddr[], const char* name)
266 {
267         unsigned char a;
268         unsigned long timeout;
269
270         a = inb(ioaddr[0] + 7);
271         timeout = jiffies + TimeOutJiffies;
272         while (a & 0x80 || a & 0x40) {
273                 /* a little sleep */
274                 yield();
275
276                 a = inb(ioaddr[0] + 7);
277                 if (time_after_eq(jiffies, timeout)) {
278                         printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
279                                 name);
280                         return -ETIME;
281                 }
282         }
283
284         return 0;
285 }
286
287 /* Card Wait For Ready (cannot be used during an interrupt) */
288 static inline int
289 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
290 {
291         unsigned char a;
292         unsigned long timeout;
293
294         a = inb(ioaddr[1] + 6);
295         timeout = jiffies + TimeOutJiffies;
296         while (a & 0x80 || !(a & 0x40)) {
297                 /* a little sleep */
298                 yield();
299
300                 a = inb(ioaddr[1] + 6);
301                 if (time_after_eq(jiffies, timeout)) {
302                         printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
303                                 name);
304                         return -ETIME;
305                 }
306         }
307
308         in[1] = inb(ioaddr[0] + 1);
309         in[2] = inb(ioaddr[0] + 2);
310         in[3] = inb(ioaddr[0] + 3);
311         in[4] = inb(ioaddr[0] + 4);
312         in[0] = inb(ioaddr[0] + 5);
313         in[6] = inb(ioaddr[0] + 6);
314         in[5] = inb(ioaddr[1] + 6);
315         return 0;
316 }
317
318 /* Card Send Command (cannot be used during an interrupt) */
319 static inline int
320 card_send_command(const int ioaddr[], const char* name,
321         const unsigned char out[], unsigned char in[])
322 {
323         int status, x;
324
325         if ((status = card_wait_for_busy_clear(ioaddr, name)))
326                 return status;
327         outb(0xa0, ioaddr[0] + 6);
328         outb(out[2], ioaddr[0] + 1);
329         outb(out[3], ioaddr[0] + 2);
330         outb(out[4], ioaddr[0] + 3);
331         outb(out[5], ioaddr[0] + 4);
332         outb(out[1], ioaddr[0] + 5);
333         outb(0xa0, ioaddr[0] + 6);
334         outb(out[0], ioaddr[0] + 7);
335         if (out[0] != 0x20 && out[0] != 0x30) {
336                 if ((status = card_wait_for_ready(ioaddr, name, in)))
337                         return status;
338                 inb(ioaddr[0] + 7);
339                 if (sb1000_debug > 3)
340                         printk(KERN_DEBUG "%s: card_send_command "
341                                 "out: %02x%02x%02x%02x%02x%02x  "
342                                 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
343                                 out[0], out[1], out[2], out[3], out[4], out[5],
344                                 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
345         } else {
346                 if (sb1000_debug > 3)
347                         printk(KERN_DEBUG "%s: card_send_command "
348                                 "out: %02x%02x%02x%02x%02x%02x\n", name,
349                                 out[0], out[1], out[2], out[3], out[4], out[5]);
350         }
351
352         if (out[1] == 0x1b) {
353                 x = (out[2] == 0x02);
354         } else {
355                 if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
356                         return -EIO;
357         }
358         return 0;
359 }
360
361 \f
362 /*
363  * SB1000 hardware routines to be used during frame rx interrupt
364  */
365 const int Sb1000TimeOutJiffies = 7 * HZ;
366
367 /* Card Wait For Ready (to be used during frame rx) */
368 static inline int
369 sb1000_wait_for_ready(const int ioaddr[], const char* name)
370 {
371         unsigned long timeout;
372
373         timeout = jiffies + Sb1000TimeOutJiffies;
374         while (inb(ioaddr[1] + 6) & 0x80) {
375                 if (time_after_eq(jiffies, timeout)) {
376                         printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
377                                 name);
378                         return -ETIME;
379                 }
380         }
381         timeout = jiffies + Sb1000TimeOutJiffies;
382         while (!(inb(ioaddr[1] + 6) & 0x40)) {
383                 if (time_after_eq(jiffies, timeout)) {
384                         printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
385                                 name);
386                         return -ETIME;
387                 }
388         }
389         inb(ioaddr[0] + 7);
390         return 0;
391 }
392
393 /* Card Wait For Ready Clear (to be used during frame rx) */
394 static inline int
395 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
396 {
397         unsigned long timeout;
398
399         timeout = jiffies + Sb1000TimeOutJiffies;
400         while (inb(ioaddr[1] + 6) & 0x80) {
401                 if (time_after_eq(jiffies, timeout)) {
402                         printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
403                                 name);
404                         return -ETIME;
405                 }
406         }
407         timeout = jiffies + Sb1000TimeOutJiffies;
408         while (inb(ioaddr[1] + 6) & 0x40) {
409                 if (time_after_eq(jiffies, timeout)) {
410                         printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
411                                 name);
412                         return -ETIME;
413                 }
414         }
415         return 0;
416 }
417
418 /* Card Send Command (to be used during frame rx) */
419 static inline void
420 sb1000_send_command(const int ioaddr[], const char* name,
421         const unsigned char out[])
422 {
423         outb(out[2], ioaddr[0] + 1);
424         outb(out[3], ioaddr[0] + 2);
425         outb(out[4], ioaddr[0] + 3);
426         outb(out[5], ioaddr[0] + 4);
427         outb(out[1], ioaddr[0] + 5);
428         outb(out[0], ioaddr[0] + 7);
429         if (sb1000_debug > 3)
430                 printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
431                         "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
432         return;
433 }
434
435 /* Card Read Status (to be used during frame rx) */
436 static inline void
437 sb1000_read_status(const int ioaddr[], unsigned char in[])
438 {
439         in[1] = inb(ioaddr[0] + 1);
440         in[2] = inb(ioaddr[0] + 2);
441         in[3] = inb(ioaddr[0] + 3);
442         in[4] = inb(ioaddr[0] + 4);
443         in[0] = inb(ioaddr[0] + 5);
444         return;
445 }
446
447 /* Issue Read Command (to be used during frame rx) */
448 static inline void
449 sb1000_issue_read_command(const int ioaddr[], const char* name)
450 {
451         const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
452
453         sb1000_wait_for_ready_clear(ioaddr, name);
454         outb(0xa0, ioaddr[0] + 6);
455         sb1000_send_command(ioaddr, name, Command0);
456         return;
457 }
458
459 \f
460 /*
461  * SB1000 commands for open/configuration
462  */
463 /* reset SB1000 card */
464 static inline int
465 sb1000_reset(const int ioaddr[], const char* name)
466 {
467         unsigned char st[7];
468         int port, status;
469         const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
470
471         port = ioaddr[1] + 6;
472         outb(0x4, port);
473         inb(port);
474         udelay(1000);
475         outb(0x0, port);
476         inb(port);
477         nicedelay(60000);
478         outb(0x4, port);
479         inb(port);
480         udelay(1000);
481         outb(0x0, port);
482         inb(port);
483         udelay(0);
484
485         if ((status = card_send_command(ioaddr, name, Command0, st)))
486                 return status;
487         if (st[3] != 0xf0)
488                 return -EIO;
489         return 0;
490 }
491
492 /* check SB1000 firmware CRC */
493 static inline int
494 sb1000_check_CRC(const int ioaddr[], const char* name)
495 {
496         unsigned char st[7];
497         int crc, status;
498         const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
499
500         /* check CRC */
501         if ((status = card_send_command(ioaddr, name, Command0, st)))
502                 return status;
503         if (st[1] != st[3] || st[2] != st[4])
504                 return -EIO;
505         crc = st[1] << 8 | st[2];
506         return 0;
507 }
508
509 static inline int
510 sb1000_start_get_set_command(const int ioaddr[], const char* name)
511 {
512         unsigned char st[7];
513         const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
514
515         return card_send_command(ioaddr, name, Command0, st);
516 }
517
518 static inline int
519 sb1000_end_get_set_command(const int ioaddr[], const char* name)
520 {
521         unsigned char st[7];
522         int status;
523         const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
524         const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
525
526         if ((status = card_send_command(ioaddr, name, Command0, st)))
527                 return status;
528         return card_send_command(ioaddr, name, Command1, st);
529 }
530
531 static inline int
532 sb1000_activate(const int ioaddr[], const char* name)
533 {
534         unsigned char st[7];
535         int status;
536         const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
537         const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
538
539         nicedelay(50000);
540         if ((status = card_send_command(ioaddr, name, Command0, st)))
541                 return status;
542         if ((status = card_send_command(ioaddr, name, Command1, st)))
543                 return status;
544         if (st[3] != 0xf1) {
545         if ((status = sb1000_start_get_set_command(ioaddr, name)))
546                         return status;
547                 return -EIO;
548         }
549         udelay(1000);
550     return sb1000_start_get_set_command(ioaddr, name);
551 }
552
553 /* get SB1000 firmware version */
554 static inline int
555 sb1000_get_firmware_version(const int ioaddr[], const char* name,
556         unsigned char version[], int do_end)
557 {
558         unsigned char st[7];
559         int status;
560         const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
561
562         if ((status = sb1000_start_get_set_command(ioaddr, name)))
563                 return status;
564         if ((status = card_send_command(ioaddr, name, Command0, st)))
565                 return status;
566         if (st[0] != 0xa3)
567                 return -EIO;
568         version[0] = st[1];
569         version[1] = st[2];
570         if (do_end)
571                 return sb1000_end_get_set_command(ioaddr, name);
572         else
573                 return 0;
574 }
575
576 /* get SB1000 frequency */
577 static inline int
578 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
579 {
580         unsigned char st[7];
581         int status;
582         const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
583
584         udelay(1000);
585         if ((status = sb1000_start_get_set_command(ioaddr, name)))
586                 return status;
587         if ((status = card_send_command(ioaddr, name, Command0, st)))
588                 return status;
589         *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
590         return sb1000_end_get_set_command(ioaddr, name);
591 }
592
593 /* set SB1000 frequency */
594 static inline int
595 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
596 {
597         unsigned char st[7];
598         int status;
599         unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
600
601         const int FrequencyLowerLimit = 57000;
602         const int FrequencyUpperLimit = 804000;
603
604         if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
605                 printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
606                         "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
607                         FrequencyUpperLimit);
608                 return -EINVAL;
609         }
610         udelay(1000);
611         if ((status = sb1000_start_get_set_command(ioaddr, name)))
612                 return status;
613         Command0[5] = frequency & 0xff;
614         frequency >>= 8;
615         Command0[4] = frequency & 0xff;
616         frequency >>= 8;
617         Command0[3] = frequency & 0xff;
618         frequency >>= 8;
619         Command0[2] = frequency & 0xff;
620         return card_send_command(ioaddr, name, Command0, st);
621 }
622
623 /* get SB1000 PIDs */
624 static inline int
625 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
626 {
627         unsigned char st[7];
628         int status;
629         const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
630         const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
631         const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
632         const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
633
634         udelay(1000);
635         if ((status = sb1000_start_get_set_command(ioaddr, name)))
636                 return status;
637
638         if ((status = card_send_command(ioaddr, name, Command0, st)))
639                 return status;
640         PID[0] = st[1] << 8 | st[2];
641
642         if ((status = card_send_command(ioaddr, name, Command1, st)))
643                 return status;
644         PID[1] = st[1] << 8 | st[2];
645
646         if ((status = card_send_command(ioaddr, name, Command2, st)))
647                 return status;
648         PID[2] = st[1] << 8 | st[2];
649
650         if ((status = card_send_command(ioaddr, name, Command3, st)))
651                 return status;
652         PID[3] = st[1] << 8 | st[2];
653
654         return sb1000_end_get_set_command(ioaddr, name);
655 }
656
657 /* set SB1000 PIDs */
658 static inline int
659 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
660 {
661         unsigned char st[7];
662         short p;
663         int status;
664         unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
665         unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
666         unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
667         unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
668         const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
669
670         udelay(1000);
671         if ((status = sb1000_start_get_set_command(ioaddr, name)))
672                 return status;
673
674         p = PID[0];
675         Command0[3] = p & 0xff;
676         p >>= 8;
677         Command0[2] = p & 0xff;
678         if ((status = card_send_command(ioaddr, name, Command0, st)))
679                 return status;
680
681         p = PID[1];
682         Command1[3] = p & 0xff;
683         p >>= 8;
684         Command1[2] = p & 0xff;
685         if ((status = card_send_command(ioaddr, name, Command1, st)))
686                 return status;
687
688         p = PID[2];
689         Command2[3] = p & 0xff;
690         p >>= 8;
691         Command2[2] = p & 0xff;
692         if ((status = card_send_command(ioaddr, name, Command2, st)))
693                 return status;
694
695         p = PID[3];
696         Command3[3] = p & 0xff;
697         p >>= 8;
698         Command3[2] = p & 0xff;
699         if ((status = card_send_command(ioaddr, name, Command3, st)))
700                 return status;
701
702         if ((status = card_send_command(ioaddr, name, Command4, st)))
703                 return status;
704         return sb1000_end_get_set_command(ioaddr, name);
705 }
706
707 \f
708 static inline void
709 sb1000_print_status_buffer(const char* name, unsigned char st[],
710         unsigned char buffer[], int size)
711 {
712         int i, j, k;
713
714         printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
715         if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
716                 printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
717                         "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
718                         buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
719             buffer[46] << 8 | buffer[47],
720                         buffer[42], buffer[43], buffer[44], buffer[45],
721             buffer[48] << 8 | buffer[49]);
722         } else {
723                 for (i = 0, k = 0; i < (size + 7) / 8; i++) {
724                         printk(KERN_DEBUG "%s: %s", name, i ? "       " : "buffer:");
725                         for (j = 0; j < 8 && k < size; j++, k++)
726                                 printk(" %02x", buffer[k]);
727                         printk("\n");
728                 }
729         }
730         return;
731 }
732
733 /*
734  * SB1000 commands for frame rx interrupt
735  */
736 /* receive a single frame and assemble datagram
737  * (this is the heart of the interrupt routine)
738  */
739 static inline int
740 sb1000_rx(struct net_device *dev)
741 {
742
743 #define FRAMESIZE 184
744         unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
745         short dlen;
746         int ioaddr, ns;
747         unsigned int skbsize;
748         struct sk_buff *skb;
749         struct sb1000_private *lp = netdev_priv(dev);
750         struct net_device_stats *stats = &lp->stats;
751
752         /* SB1000 frame constants */
753         const int FrameSize = FRAMESIZE;
754         const int NewDatagramHeaderSkip = 8;
755         const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
756         const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
757         const int ContDatagramHeaderSkip = 7;
758         const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
759         const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
760         const int TrailerSize = 4;
761
762         ioaddr = dev->base_addr;
763
764         insw(ioaddr, (unsigned short*) st, 1);
765 #ifdef XXXDEBUG
766 printk("cm0: received: %02x %02x\n", st[0], st[1]);
767 #endif /* XXXDEBUG */
768         lp->rx_frames++;
769
770         /* decide if it is a good or bad frame */
771         for (ns = 0; ns < NPIDS; ns++) {
772                 session_id = lp->rx_session_id[ns];
773                 frame_id = lp->rx_frame_id[ns];
774                 if (st[0] == session_id) {
775                         if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
776                                 goto good_frame;
777                         } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
778                                 goto skipped_frame;
779                         } else {
780                                 goto bad_frame;
781                         }
782                 } else if (st[0] == (session_id | 0x40)) {
783                         if ((st[1] & 0xf0) == 0x30) {
784                                 goto skipped_frame;
785                         } else {
786                                 goto bad_frame;
787                         }
788                 }
789         }
790         goto bad_frame;
791
792 skipped_frame:
793         stats->rx_frame_errors++;
794         skb = lp->rx_skb[ns];
795         if (sb1000_debug > 1)
796                 printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
797                         "expecting %02x %02x\n", dev->name, st[0], st[1],
798                         skb ? session_id : session_id | 0x40, frame_id);
799         if (skb) {
800                 dev_kfree_skb(skb);
801                 skb = 0;
802         }
803
804 good_frame:
805         lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
806         /* new datagram */
807         if (st[0] & 0x40) {
808                 /* get data length */
809                 insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
810 #ifdef XXXDEBUG
811 printk("cm0: IP identification: %02x%02x  fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
812 #endif /* XXXDEBUG */
813                 if (buffer[0] != NewDatagramHeaderSkip) {
814                         if (sb1000_debug > 1)
815                                 printk(KERN_WARNING "%s: new datagram header skip error: "
816                                         "got %02x expecting %02x\n", dev->name, buffer[0],
817                                         NewDatagramHeaderSkip);
818                         stats->rx_length_errors++;
819                         insw(ioaddr, buffer, NewDatagramDataSize / 2);
820                         goto bad_frame_next;
821                 }
822                 dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
823                         buffer[NewDatagramHeaderSkip + 4]) - 17;
824                 if (dlen > SB1000_MRU) {
825                         if (sb1000_debug > 1)
826                                 printk(KERN_WARNING "%s: datagram length (%d) greater "
827                                         "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
828                         stats->rx_length_errors++;
829                         insw(ioaddr, buffer, NewDatagramDataSize / 2);
830                         goto bad_frame_next;
831                 }
832                 lp->rx_dlen[ns] = dlen;
833                 /* compute size to allocate for datagram */
834                 skbsize = dlen + FrameSize;
835                 if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
836                         if (sb1000_debug > 1)
837                                 printk(KERN_WARNING "%s: can't allocate %d bytes long "
838                                         "skbuff\n", dev->name, skbsize);
839                         stats->rx_dropped++;
840                         insw(ioaddr, buffer, NewDatagramDataSize / 2);
841                         goto dropped_frame;
842                 }
843                 skb->dev = dev;
844                 skb->mac.raw = skb->data;
845                 skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
846                 insw(ioaddr, skb_put(skb, NewDatagramDataSize),
847                         NewDatagramDataSize / 2);
848                 lp->rx_skb[ns] = skb;
849         } else {
850                 /* continuation of previous datagram */
851                 insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
852                 if (buffer[0] != ContDatagramHeaderSkip) {
853                         if (sb1000_debug > 1)
854                                 printk(KERN_WARNING "%s: cont datagram header skip error: "
855                                         "got %02x expecting %02x\n", dev->name, buffer[0],
856                                         ContDatagramHeaderSkip);
857                         stats->rx_length_errors++;
858                         insw(ioaddr, buffer, ContDatagramDataSize / 2);
859                         goto bad_frame_next;
860                 }
861                 skb = lp->rx_skb[ns];
862                 insw(ioaddr, skb_put(skb, ContDatagramDataSize),
863                         ContDatagramDataSize / 2);
864                 dlen = lp->rx_dlen[ns];
865         }
866         if (skb->len < dlen + TrailerSize) {
867                 lp->rx_session_id[ns] &= ~0x40;
868                 return 0;
869         }
870
871         /* datagram completed: send to upper level */
872         skb_trim(skb, dlen);
873         netif_rx(skb);
874         dev->last_rx = jiffies;
875         stats->rx_bytes+=dlen;
876         stats->rx_packets++;
877         lp->rx_skb[ns] = 0;
878         lp->rx_session_id[ns] |= 0x40;
879         return 0;
880
881 bad_frame:
882         insw(ioaddr, buffer, FrameSize / 2);
883         if (sb1000_debug > 1)
884                 printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
885                         dev->name, st[0], st[1]);
886         stats->rx_frame_errors++;
887 bad_frame_next:
888         if (sb1000_debug > 2)
889                 sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
890 dropped_frame:
891         stats->rx_errors++;
892         if (ns < NPIDS) {
893                 if ((skb = lp->rx_skb[ns])) {
894                         dev_kfree_skb(skb);
895                         lp->rx_skb[ns] = 0;
896                 }
897                 lp->rx_session_id[ns] |= 0x40;
898         }
899         return -1;
900 }
901
902 static inline void
903 sb1000_error_dpc(struct net_device *dev)
904 {
905         char *name;
906         unsigned char st[5];
907         int ioaddr[2];
908         struct sb1000_private *lp = netdev_priv(dev);
909         const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
910         const int ErrorDpcCounterInitialize = 200;
911
912         ioaddr[0] = dev->base_addr;
913         /* mem_start holds the second I/O address */
914         ioaddr[1] = dev->mem_start;
915         name = dev->name;
916
917         sb1000_wait_for_ready_clear(ioaddr, name);
918         sb1000_send_command(ioaddr, name, Command0);
919         sb1000_wait_for_ready(ioaddr, name);
920         sb1000_read_status(ioaddr, st);
921         if (st[1] & 0x10)
922                 lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
923         return;
924 }
925
926 \f
927 /*
928  * Linux interface functions
929  */
930 static int
931 sb1000_open(struct net_device *dev)
932 {
933         char *name;
934         int ioaddr[2], status;
935         struct sb1000_private *lp = netdev_priv(dev);
936         const unsigned short FirmwareVersion[] = {0x01, 0x01};
937
938         ioaddr[0] = dev->base_addr;
939         /* mem_start holds the second I/O address */
940         ioaddr[1] = dev->mem_start;
941         name = dev->name;
942
943         /* initialize sb1000 */
944         if ((status = sb1000_reset(ioaddr, name)))
945                 return status;
946         nicedelay(200000);
947         if ((status = sb1000_check_CRC(ioaddr, name)))
948                 return status;
949
950         /* initialize private data before board can catch interrupts */
951         lp->rx_skb[0] = NULL;
952         lp->rx_skb[1] = NULL;
953         lp->rx_skb[2] = NULL;
954         lp->rx_skb[3] = NULL;
955         lp->rx_dlen[0] = 0;
956         lp->rx_dlen[1] = 0;
957         lp->rx_dlen[2] = 0;
958         lp->rx_dlen[3] = 0;
959         lp->rx_frames = 0;
960         lp->rx_error_count = 0;
961         lp->rx_error_dpc_count = 0;
962         lp->rx_session_id[0] = 0x50;
963         lp->rx_session_id[0] = 0x48;
964         lp->rx_session_id[0] = 0x44;
965         lp->rx_session_id[0] = 0x42;
966         lp->rx_frame_id[0] = 0;
967         lp->rx_frame_id[1] = 0;
968         lp->rx_frame_id[2] = 0;
969         lp->rx_frame_id[3] = 0;
970         if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
971                 return -EAGAIN;
972         }
973
974         if (sb1000_debug > 2)
975                 printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
976
977         /* Activate board and check firmware version */
978         udelay(1000);
979         if ((status = sb1000_activate(ioaddr, name)))
980                 return status;
981         udelay(0);
982         if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
983                 return status;
984         if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
985                 printk(KERN_WARNING "%s: found firmware version %x.%02x "
986                         "(should be %x.%02x)\n", name, version[0], version[1],
987                         FirmwareVersion[0], FirmwareVersion[1]);
988
989
990         netif_start_queue(dev);
991         return 0;                                       /* Always succeed */
992 }
993
994 static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
995 {
996         char* name;
997         unsigned char version[2];
998         short PID[4];
999         int ioaddr[2], status, frequency;
1000         unsigned int stats[5];
1001         struct sb1000_private *lp = netdev_priv(dev);
1002
1003         if (!(dev && dev->flags & IFF_UP))
1004                 return -ENODEV;
1005
1006         ioaddr[0] = dev->base_addr;
1007         /* mem_start holds the second I/O address */
1008         ioaddr[1] = dev->mem_start;
1009         name = dev->name;
1010
1011         switch (cmd) {
1012         case SIOCGCMSTATS:              /* get statistics */
1013                 stats[0] = lp->stats.rx_bytes;
1014                 stats[1] = lp->rx_frames;
1015                 stats[2] = lp->stats.rx_packets;
1016                 stats[3] = lp->stats.rx_errors;
1017                 stats[4] = lp->stats.rx_dropped;
1018                 if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1019                         return -EFAULT;
1020                 status = 0;
1021                 break;
1022
1023         case SIOCGCMFIRMWARE:           /* get firmware version */
1024                 if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1025                         return status;
1026                 if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1027                         return -EFAULT;
1028                 break;
1029
1030         case SIOCGCMFREQUENCY:          /* get frequency */
1031                 if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1032                         return status;
1033                 if(put_user(frequency, (int*) ifr->ifr_data))
1034                         return -EFAULT;
1035                 break;
1036
1037         case SIOCSCMFREQUENCY:          /* set frequency */
1038                 if (!capable(CAP_NET_ADMIN))
1039                         return -EPERM;
1040                 if(get_user(frequency, (int*) ifr->ifr_data))
1041                         return -EFAULT;
1042                 if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1043                         return status;
1044                 break;
1045
1046         case SIOCGCMPIDS:                       /* get PIDs */
1047                 if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1048                         return status;
1049                 if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1050                         return -EFAULT;
1051                 break;
1052
1053         case SIOCSCMPIDS:                       /* set PIDs */
1054                 if (!capable(CAP_NET_ADMIN))
1055                         return -EPERM;
1056                 if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1057                         return -EFAULT;
1058                 if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1059                         return status;
1060                 /* set session_id, frame_id and pkt_type too */
1061                 lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1062                 lp->rx_session_id[1] = 0x48;
1063                 lp->rx_session_id[2] = 0x44;
1064                 lp->rx_session_id[3] = 0x42;
1065                 lp->rx_frame_id[0] = 0;
1066                 lp->rx_frame_id[1] = 0;
1067                 lp->rx_frame_id[2] = 0;
1068                 lp->rx_frame_id[3] = 0;
1069                 break;
1070
1071         default:
1072                 status = -EINVAL;
1073                 break;
1074         }
1075         return status;
1076 }
1077
1078 /* transmit function: do nothing since SB1000 can't send anything out */
1079 static int
1080 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1081 {
1082         printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1083         /* sb1000 can't xmit datagrams */
1084         dev_kfree_skb(skb);
1085         return 0;
1086 }
1087
1088 /* SB1000 interrupt handler. */
1089 static irqreturn_t sb1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1090 {
1091         char *name;
1092         unsigned char st;
1093         int ioaddr[2];
1094         struct net_device *dev = (struct net_device *) dev_id;
1095         struct sb1000_private *lp = netdev_priv(dev);
1096
1097         const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1098         const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1099         const int MaxRxErrorCount = 6;
1100
1101         if (dev == NULL) {
1102                 printk(KERN_ERR "sb1000_interrupt(): irq %d for unknown device.\n",
1103                         irq);
1104                 return IRQ_NONE;
1105         }
1106
1107         ioaddr[0] = dev->base_addr;
1108         /* mem_start holds the second I/O address */
1109         ioaddr[1] = dev->mem_start;
1110         name = dev->name;
1111
1112         /* is it a good interrupt? */
1113         st = inb(ioaddr[1] + 6);
1114         if (!(st & 0x08 && st & 0x20)) {
1115                 return IRQ_NONE;
1116         }
1117
1118         if (sb1000_debug > 3)
1119                 printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1120
1121         st = inb(ioaddr[0] + 7);
1122         if (sb1000_rx(dev))
1123                 lp->rx_error_count++;
1124 #ifdef SB1000_DELAY
1125         udelay(SB1000_DELAY);
1126 #endif /* SB1000_DELAY */
1127         sb1000_issue_read_command(ioaddr, name);
1128         if (st & 0x01) {
1129                 sb1000_error_dpc(dev);
1130                 sb1000_issue_read_command(ioaddr, name);
1131         }
1132         if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1133                 sb1000_wait_for_ready_clear(ioaddr, name);
1134                 sb1000_send_command(ioaddr, name, Command0);
1135                 sb1000_wait_for_ready(ioaddr, name);
1136                 sb1000_issue_read_command(ioaddr, name);
1137         }
1138         if (lp->rx_error_count >= MaxRxErrorCount) {
1139                 sb1000_wait_for_ready_clear(ioaddr, name);
1140                 sb1000_send_command(ioaddr, name, Command1);
1141                 sb1000_wait_for_ready(ioaddr, name);
1142                 sb1000_issue_read_command(ioaddr, name);
1143                 lp->rx_error_count = 0;
1144         }
1145
1146         return IRQ_HANDLED;
1147 }
1148
1149 static struct net_device_stats *sb1000_stats(struct net_device *dev)
1150 {
1151         struct sb1000_private *lp = netdev_priv(dev);
1152         return &lp->stats;
1153 }
1154
1155 static int sb1000_close(struct net_device *dev)
1156 {
1157         int i;
1158         int ioaddr[2];
1159         struct sb1000_private *lp = netdev_priv(dev);
1160
1161         if (sb1000_debug > 2)
1162                 printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1163
1164         netif_stop_queue(dev);
1165         
1166         ioaddr[0] = dev->base_addr;
1167         /* mem_start holds the second I/O address */
1168         ioaddr[1] = dev->mem_start;
1169
1170         free_irq(dev->irq, dev);
1171         /* If we don't do this, we can't re-insmod it later. */
1172         release_region(ioaddr[1], SB1000_IO_EXTENT);
1173         release_region(ioaddr[0], SB1000_IO_EXTENT);
1174
1175         /* free rx_skb's if needed */
1176         for (i=0; i<4; i++) {
1177                 if (lp->rx_skb[i]) {
1178                         dev_kfree_skb(lp->rx_skb[i]);
1179                 }
1180         }
1181         return 0;
1182 }
1183
1184 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1185 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1186 MODULE_LICENSE("GPL");
1187
1188 static int __init
1189 sb1000_init(void)
1190 {
1191         return pnp_register_driver(&sb1000_driver);
1192 }
1193
1194 static void __exit
1195 sb1000_exit(void)
1196 {
1197         pnp_unregister_driver(&sb1000_driver);
1198 }
1199
1200 module_init(sb1000_init);
1201 module_exit(sb1000_exit);