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