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