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