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