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