Blame view

drivers/net/sb1000.c 31.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /* sb1000.c: A General Instruments SB1000 driver for linux. */
  /*
  	Written 1998 by Franco Venturi.
  
  	Copyright 1998 by Franco Venturi.
  	Copyright 1994,1995 by Donald Becker.
  	Copyright 1993 United States Government as represented by the
  	Director, National Security Agency.
  
  	This driver is for the General Instruments SB1000 (internal SURFboard)
  
  	The author may be reached as fventuri@mediaone.net
  
  	This program is free software; you can redistribute it
  	and/or  modify it under  the terms of  the GNU General
  	Public  License as  published  by  the  Free  Software
  	Foundation;  either  version 2 of the License, or  (at
  	your option) any later version.
  
  	Changes:
  
  	981115 Steven Hirsch <shirsch@adelphia.net>
  
  	Linus changed the timer interface.  Should work on all recent
  	development kernels.
  
  	980608 Steven Hirsch <shirsch@adelphia.net>
  
  	Small changes to make it work with 2.1.x kernels. Hopefully,
  	nothing major will change before official release of Linux 2.2.
6aa20a223   Jeff Garzik   drivers/net: Trim...
31

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
37
38
39
  	Merged with 2.2 - Alan Cox
  */
  
  static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)
  ";
  
  #include <linux/module.h>
  #include <linux/kernel.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
40
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
  #include <linux/string.h>
  #include <linux/interrupt.h>
  #include <linux/errno.h>
  #include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
  #include <linux/in.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
50
51
52
53
54
  #include <linux/ioport.h>
  #include <linux/netdevice.h>
  #include <linux/if_arp.h>
  #include <linux/skbuff.h>
  #include <linux/delay.h>	/* for udelay() */
  #include <linux/etherdevice.h>
  #include <linux/pnp.h>
  #include <linux/init.h>
  #include <linux/bitops.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
55
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
  
  #include <asm/io.h>
  #include <asm/processor.h>
  #include <asm/uaccess.h>
  
  #ifdef SB1000_DEBUG
  static int sb1000_debug = SB1000_DEBUG;
  #else
f71e13096   Arjan van de Ven   Massive net drive...
64
  static const int sb1000_debug = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #endif
  
  static const int SB1000_IO_EXTENT = 8;
  /* SB1000 Maximum Receive Unit */
  static const int SB1000_MRU = 1500; /* octects */
  
  #define NPIDS 4
  struct sb1000_private {
  	struct sk_buff *rx_skb[NPIDS];
  	short rx_dlen[NPIDS];
  	unsigned int rx_frames;
  	short rx_error_count;
  	short rx_error_dpc_count;
  	unsigned char rx_session_id[NPIDS];
  	unsigned char rx_frame_id[NPIDS];
  	unsigned char rx_pkt_type[NPIDS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
86
  };
  
  /* prototypes for Linux interface */
  extern int sb1000_probe(struct net_device *dev);
  static int sb1000_open(struct net_device *dev);
  static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
61357325f   Stephen Hemminger   netdev: convert b...
87
88
  static netdev_tx_t sb1000_start_xmit(struct sk_buff *skb,
  				     struct net_device *dev);
7d12e780e   David Howells   IRQ: Maintain reg...
89
  static irqreturn_t sb1000_interrupt(int irq, void *dev_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
  static int sb1000_close(struct net_device *dev);
  
  
  /* SB1000 hardware routines to be used during open/configuration phases */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
94
  static int card_wait_for_busy_clear(const int ioaddr[],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
  	const char* name);
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
96
  static int card_wait_for_ready(const int ioaddr[], const char* name,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  	unsigned char in[]);
858119e15   Arjan van de Ven   [PATCH] Unlinline...
98
  static int card_send_command(const int ioaddr[], const char* name,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
  	const unsigned char out[], unsigned char in[]);
  
  /* SB1000 hardware routines to be used during frame rx interrupt */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
102
103
  static int sb1000_wait_for_ready(const int ioaddr[], const char* name);
  static int sb1000_wait_for_ready_clear(const int ioaddr[],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	const char* name);
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
105
  static void sb1000_send_command(const int ioaddr[], const char* name,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  	const unsigned char out[]);
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
107
108
  static void sb1000_read_status(const int ioaddr[], unsigned char in[]);
  static void sb1000_issue_read_command(const int ioaddr[],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
  	const char* name);
  
  /* SB1000 commands for open/configuration */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
112
113
  static int sb1000_reset(const int ioaddr[], const char* name);
  static int sb1000_check_CRC(const int ioaddr[], const char* name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
  static inline int sb1000_start_get_set_command(const int ioaddr[],
  	const char* name);
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
116
  static int sb1000_end_get_set_command(const int ioaddr[],
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  	const char* name);
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
118
  static int sb1000_activate(const int ioaddr[], const char* name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
126
127
128
129
130
  static int sb1000_get_firmware_version(const int ioaddr[],
  	const char* name, unsigned char version[], int do_end);
  static int sb1000_get_frequency(const int ioaddr[], const char* name,
  	int* frequency);
  static int sb1000_set_frequency(const int ioaddr[], const char* name,
  	int frequency);
  static int sb1000_get_PIDs(const int ioaddr[], const char* name,
  	short PID[]);
  static int sb1000_set_PIDs(const int ioaddr[], const char* name,
  	const short PID[]);
  
  /* SB1000 commands for frame rx interrupt */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
131
132
  static int sb1000_rx(struct net_device *dev);
  static void sb1000_error_dpc(struct net_device *dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
135
136
137
138
  
  static const struct pnp_device_id sb1000_pnp_ids[] = {
  	{ "GIC1000", 0 },
  	{ "", 0 }
  };
  MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
d49d19c96   Stephen Hemminger   sb1000: update to...
139
140
141
142
143
144
145
146
147
  static const struct net_device_ops sb1000_netdev_ops = {
  	.ndo_open		= sb1000_open,
  	.ndo_start_xmit		= sb1000_start_xmit,
  	.ndo_do_ioctl		= sb1000_dev_ioctl,
  	.ndo_stop		= sb1000_close,
  	.ndo_change_mtu		= eth_change_mtu,
  	.ndo_set_mac_address 	= eth_mac_addr,
  	.ndo_validate_addr	= eth_validate_addr,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
  static int
  sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
  {
  	struct net_device *dev;
  	unsigned short ioaddr[2], irq;
  	unsigned int serial_number;
  	int error = -ENODEV;
6aa20a223   Jeff Garzik   drivers/net: Trim...
155

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
  	if (pnp_device_attach(pdev) < 0)
  		return -ENODEV;
  	if (pnp_activate_dev(pdev) < 0)
  		goto out_detach;
  
  	if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
  		goto out_disable;
  	if (!pnp_irq_valid(pdev, 0))
  		goto out_disable;
6aa20a223   Jeff Garzik   drivers/net: Trim...
165

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  	serial_number = pdev->card->serial;
6aa20a223   Jeff Garzik   drivers/net: Trim...
167

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
  	ioaddr[0] = pnp_port_start(pdev, 0);
  	ioaddr[1] = pnp_port_start(pdev, 0);
6aa20a223   Jeff Garzik   drivers/net: Trim...
170

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
175
176
177
178
179
180
181
182
  	irq = pnp_irq(pdev, 0);
  
  	if (!request_region(ioaddr[0], 16, "sb1000"))
  		goto out_disable;
  	if (!request_region(ioaddr[1], 16, "sb1000"))
  		goto out_release_region0;
  
  	dev = alloc_etherdev(sizeof(struct sb1000_private));
  	if (!dev) {
  		error = -ENOMEM;
  		goto out_release_regions;
  	}
6aa20a223   Jeff Garzik   drivers/net: Trim...
183

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  	dev->base_addr = ioaddr[0];
  	/* mem_start holds the second I/O address */
  	dev->mem_start = ioaddr[1];
  	dev->irq = irq;
  
  	if (sb1000_debug > 0)
  		printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
  			"S/N %#8.8x, IRQ %d.
  ", dev->name, dev->base_addr,
  			dev->mem_start, serial_number, dev->irq);
  
  	/*
  	 * The SB1000 is an rx-only cable modem device.  The uplink is a modem
  	 * and we do not want to arp on it.
  	 */
  	dev->flags = IFF_POINTOPOINT|IFF_NOARP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
  	SET_NETDEV_DEV(dev, &pdev->dev);
  
  	if (sb1000_debug > 0)
  		printk(KERN_NOTICE "%s", version);
d49d19c96   Stephen Hemminger   sb1000: update to...
204
  	dev->netdev_ops	= &sb1000_netdev_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  
  	/* hardware address is 0:0:serial_number */
  	dev->dev_addr[2]	= serial_number >> 24 & 0xff;
  	dev->dev_addr[3]	= serial_number >> 16 & 0xff;
  	dev->dev_addr[4]	= serial_number >>  8 & 0xff;
  	dev->dev_addr[5]	= serial_number >>  0 & 0xff;
  
  	pnp_set_drvdata(pdev, dev);
  
  	error = register_netdev(dev);
  	if (error)
  		goto out_free_netdev;
  	return 0;
  
   out_free_netdev:
  	free_netdev(dev);
   out_release_regions:
  	release_region(ioaddr[1], 16);
   out_release_region0:
  	release_region(ioaddr[0], 16);
   out_disable:
  	pnp_disable_dev(pdev);
   out_detach:
  	pnp_device_detach(pdev);
  	return error;
  }
  
  static void
  sb1000_remove_one(struct pnp_dev *pdev)
  {
  	struct net_device *dev = pnp_get_drvdata(pdev);
  
  	unregister_netdev(dev);
  	release_region(dev->base_addr, 16);
  	release_region(dev->mem_start, 16);
  	free_netdev(dev);
  }
  
  static struct pnp_driver sb1000_driver = {
  	.name		= "sb1000",
  	.id_table	= sb1000_pnp_ids,
  	.probe		= sb1000_probe_one,
  	.remove		= sb1000_remove_one,
  };
6aa20a223   Jeff Garzik   drivers/net: Trim...
249

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
253
254
  /*
   * SB1000 hardware routines to be used during open/configuration phases
   */
  
  static const int TimeOutJiffies = (875 * HZ) / 100;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  /* Card Wait For Busy Clear (cannot be used during an interrupt) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
256
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  card_wait_for_busy_clear(const int ioaddr[], const char* name)
  {
  	unsigned char a;
  	unsigned long timeout;
  
  	a = inb(ioaddr[0] + 7);
  	timeout = jiffies + TimeOutJiffies;
  	while (a & 0x80 || a & 0x40) {
  		/* a little sleep */
  		yield();
  
  		a = inb(ioaddr[0] + 7);
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  
  	return 0;
  }
  
  /* Card Wait For Ready (cannot be used during an interrupt) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
281
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
  {
  	unsigned char a;
  	unsigned long timeout;
  
  	a = inb(ioaddr[1] + 6);
  	timeout = jiffies + TimeOutJiffies;
  	while (a & 0x80 || !(a & 0x40)) {
  		/* a little sleep */
  		yield();
  
  		a = inb(ioaddr[1] + 6);
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: card_wait_for_ready timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  
  	in[1] = inb(ioaddr[0] + 1);
  	in[2] = inb(ioaddr[0] + 2);
  	in[3] = inb(ioaddr[0] + 3);
  	in[4] = inb(ioaddr[0] + 4);
  	in[0] = inb(ioaddr[0] + 5);
  	in[6] = inb(ioaddr[0] + 6);
  	in[5] = inb(ioaddr[1] + 6);
  	return 0;
  }
  
  /* Card Send Command (cannot be used during an interrupt) */
858119e15   Arjan van de Ven   [PATCH] Unlinline...
313
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  card_send_command(const int ioaddr[], const char* name,
  	const unsigned char out[], unsigned char in[])
  {
  	int status, x;
  
  	if ((status = card_wait_for_busy_clear(ioaddr, name)))
  		return status;
  	outb(0xa0, ioaddr[0] + 6);
  	outb(out[2], ioaddr[0] + 1);
  	outb(out[3], ioaddr[0] + 2);
  	outb(out[4], ioaddr[0] + 3);
  	outb(out[5], ioaddr[0] + 4);
  	outb(out[1], ioaddr[0] + 5);
  	outb(0xa0, ioaddr[0] + 6);
  	outb(out[0], ioaddr[0] + 7);
  	if (out[0] != 0x20 && out[0] != 0x30) {
  		if ((status = card_wait_for_ready(ioaddr, name, in)))
  			return status;
  		inb(ioaddr[0] + 7);
  		if (sb1000_debug > 3)
  			printk(KERN_DEBUG "%s: card_send_command "
  				"out: %02x%02x%02x%02x%02x%02x  "
  				"in: %02x%02x%02x%02x%02x%02x%02x
  ", name,
  				out[0], out[1], out[2], out[3], out[4], out[5],
  				in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
  	} else {
  		if (sb1000_debug > 3)
  			printk(KERN_DEBUG "%s: card_send_command "
  				"out: %02x%02x%02x%02x%02x%02x
  ", name,
  				out[0], out[1], out[2], out[3], out[4], out[5]);
  	}
  
  	if (out[1] == 0x1b) {
  		x = (out[2] == 0x02);
  	} else {
  		if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
  			return -EIO;
  	}
  	return 0;
  }
6aa20a223   Jeff Garzik   drivers/net: Trim...
356

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
361
362
  /*
   * SB1000 hardware routines to be used during frame rx interrupt
   */
  static const int Sb1000TimeOutJiffies = 7 * HZ;
  
  /* Card Wait For Ready (to be used during frame rx) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
363
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  sb1000_wait_for_ready(const int ioaddr[], const char* name)
  {
  	unsigned long timeout;
  
  	timeout = jiffies + Sb1000TimeOutJiffies;
  	while (inb(ioaddr[1] + 6) & 0x80) {
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  	timeout = jiffies + Sb1000TimeOutJiffies;
  	while (!(inb(ioaddr[1] + 6) & 0x40)) {
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  	inb(ioaddr[0] + 7);
  	return 0;
  }
  
  /* Card Wait For Ready Clear (to be used during frame rx) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
391
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
  {
  	unsigned long timeout;
  
  	timeout = jiffies + Sb1000TimeOutJiffies;
  	while (inb(ioaddr[1] + 6) & 0x80) {
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  	timeout = jiffies + Sb1000TimeOutJiffies;
  	while (inb(ioaddr[1] + 6) & 0x40) {
  		if (time_after_eq(jiffies, timeout)) {
  			printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout
  ",
  				name);
  			return -ETIME;
  		}
  	}
  	return 0;
  }
  
  /* Card Send Command (to be used during frame rx) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
418
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
420
421
422
423
424
425
426
427
428
429
430
431
  sb1000_send_command(const int ioaddr[], const char* name,
  	const unsigned char out[])
  {
  	outb(out[2], ioaddr[0] + 1);
  	outb(out[3], ioaddr[0] + 2);
  	outb(out[4], ioaddr[0] + 3);
  	outb(out[5], ioaddr[0] + 4);
  	outb(out[1], ioaddr[0] + 5);
  	outb(out[0], ioaddr[0] + 7);
  	if (sb1000_debug > 3)
  		printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
  			"%02x%02x
  ", name, out[0], out[1], out[2], out[3], out[4], out[5]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
  }
  
  /* Card Read Status (to be used during frame rx) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
435
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
438
439
440
441
442
  sb1000_read_status(const int ioaddr[], unsigned char in[])
  {
  	in[1] = inb(ioaddr[0] + 1);
  	in[2] = inb(ioaddr[0] + 2);
  	in[3] = inb(ioaddr[0] + 3);
  	in[4] = inb(ioaddr[0] + 4);
  	in[0] = inb(ioaddr[0] + 5);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
  }
  
  /* Issue Read Command (to be used during frame rx) */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
446
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
  sb1000_issue_read_command(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
449
  	static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
  
  	sb1000_wait_for_ready_clear(ioaddr, name);
  	outb(0xa0, ioaddr[0] + 6);
  	sb1000_send_command(ioaddr, name, Command0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  }
6aa20a223   Jeff Garzik   drivers/net: Trim...
455

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
458
459
  /*
   * SB1000 commands for open/configuration
   */
  /* reset SB1000 card */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
460
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
462
  sb1000_reset(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
463
  	static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
465
  	unsigned char st[7];
  	int port, status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
470
471
472
  
  	port = ioaddr[1] + 6;
  	outb(0x4, port);
  	inb(port);
  	udelay(1000);
  	outb(0x0, port);
  	inb(port);
a26c074c1   Nishanth Aravamudan   [PATCH] net/sb100...
473
  	ssleep(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  	outb(0x4, port);
  	inb(port);
  	udelay(1000);
  	outb(0x0, port);
  	inb(port);
  	udelay(0);
  
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	if (st[3] != 0xf0)
  		return -EIO;
  	return 0;
  }
  
  /* check SB1000 firmware CRC */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
489
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
  sb1000_check_CRC(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
492
  	static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
494
  	unsigned char st[7];
  	int crc, status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
499
500
501
502
503
504
505
506
507
  
  	/* check CRC */
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	if (st[1] != st[3] || st[2] != st[4])
  		return -EIO;
  	crc = st[1] << 8 | st[2];
  	return 0;
  }
  
  static inline int
  sb1000_start_get_set_command(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
508
  	static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
  	unsigned char st[7];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
  
  	return card_send_command(ioaddr, name, Command0, st);
  }
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
513
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
515
  sb1000_end_get_set_command(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
516
517
  	static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
  	static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
  	unsigned char st[7];
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
521
522
523
524
  
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	return card_send_command(ioaddr, name, Command1, st);
  }
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
525
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
527
  sb1000_activate(const int ioaddr[], const char* name)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
528
529
  	static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
  	static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
531
  	unsigned char st[7];
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532

a26c074c1   Nishanth Aravamudan   [PATCH] net/sb100...
533
  	ssleep(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	if ((status = card_send_command(ioaddr, name, Command1, st)))
  		return status;
  	if (st[3] != 0xf1) {
      	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  			return status;
  		return -EIO;
  	}
  	udelay(1000);
      return sb1000_start_get_set_command(ioaddr, name);
  }
  
  /* get SB1000 firmware version */
  static int
  sb1000_get_firmware_version(const int ioaddr[], const char* name,
  	unsigned char version[], int do_end)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
552
  	static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
554
  	unsigned char st[7];
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  
  	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  		return status;
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	if (st[0] != 0xa3)
  		return -EIO;
  	version[0] = st[1];
  	version[1] = st[2];
  	if (do_end)
  		return sb1000_end_get_set_command(ioaddr, name);
  	else
  		return 0;
  }
  
  /* get SB1000 frequency */
  static int
  sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
574
  	static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
575
576
  	unsigned char st[7];
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  
  	udelay(1000);
  	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  		return status;
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	*frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
  	return sb1000_end_get_set_command(ioaddr, name);
  }
  
  /* set SB1000 frequency */
  static int
  sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
  {
  	unsigned char st[7];
  	int status;
  	unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
  
  	const int FrequencyLowerLimit = 57000;
  	const int FrequencyUpperLimit = 804000;
  
  	if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
  		printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
  			"[%d,%d] kHz
  ", name, frequency, FrequencyLowerLimit,
  			FrequencyUpperLimit);
  		return -EINVAL;
  	}
  	udelay(1000);
  	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  		return status;
  	Command0[5] = frequency & 0xff;
  	frequency >>= 8;
  	Command0[4] = frequency & 0xff;
  	frequency >>= 8;
  	Command0[3] = frequency & 0xff;
  	frequency >>= 8;
  	Command0[2] = frequency & 0xff;
  	return card_send_command(ioaddr, name, Command0, st);
  }
  
  /* get SB1000 PIDs */
  static int
  sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
622
623
624
625
  	static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
  	static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
  	static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
  	static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
  	unsigned char st[7];
  	int status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
  
  	udelay(1000);
  	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  		return status;
  
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  	PID[0] = st[1] << 8 | st[2];
  
  	if ((status = card_send_command(ioaddr, name, Command1, st)))
  		return status;
  	PID[1] = st[1] << 8 | st[2];
  
  	if ((status = card_send_command(ioaddr, name, Command2, st)))
  		return status;
  	PID[2] = st[1] << 8 | st[2];
  
  	if ((status = card_send_command(ioaddr, name, Command3, st)))
  		return status;
  	PID[3] = st[1] << 8 | st[2];
  
  	return sb1000_end_get_set_command(ioaddr, name);
  }
  
  /* set SB1000 PIDs */
  static int
  sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
656
  	static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
660
661
662
663
  	unsigned char st[7];
  	short p;
  	int status;
  	unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
  	unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
  	unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
  	unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  
  	udelay(1000);
  	if ((status = sb1000_start_get_set_command(ioaddr, name)))
  		return status;
  
  	p = PID[0];
  	Command0[3] = p & 0xff;
  	p >>= 8;
  	Command0[2] = p & 0xff;
  	if ((status = card_send_command(ioaddr, name, Command0, st)))
  		return status;
  
  	p = PID[1];
  	Command1[3] = p & 0xff;
  	p >>= 8;
  	Command1[2] = p & 0xff;
  	if ((status = card_send_command(ioaddr, name, Command1, st)))
  		return status;
  
  	p = PID[2];
  	Command2[3] = p & 0xff;
  	p >>= 8;
  	Command2[2] = p & 0xff;
  	if ((status = card_send_command(ioaddr, name, Command2, st)))
  		return status;
  
  	p = PID[3];
  	Command3[3] = p & 0xff;
  	p >>= 8;
  	Command3[2] = p & 0xff;
  	if ((status = card_send_command(ioaddr, name, Command3, st)))
  		return status;
  
  	if ((status = card_send_command(ioaddr, name, Command4, st)))
  		return status;
  	return sb1000_end_get_set_command(ioaddr, name);
  }
6aa20a223   Jeff Garzik   drivers/net: Trim...
701

a8d06342b   Denys Vlasenko   sb1000.c: stop in...
702
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  sb1000_print_status_buffer(const char* name, unsigned char st[],
  	unsigned char buffer[], int size)
  {
  	int i, j, k;
  
  	printk(KERN_DEBUG "%s: status: %02x %02x
  ", name, st[0], st[1]);
  	if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
  		printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
  			"to %d.%d.%d.%d:%d
  ", name, buffer[28] << 8 | buffer[29],
  			buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
              buffer[46] << 8 | buffer[47],
  			buffer[42], buffer[43], buffer[44], buffer[45],
              buffer[48] << 8 | buffer[49]);
  	} else {
  		for (i = 0, k = 0; i < (size + 7) / 8; i++) {
  			printk(KERN_DEBUG "%s: %s", name, i ? "       " : "buffer:");
  			for (j = 0; j < 8 && k < size; j++, k++)
  				printk(" %02x", buffer[k]);
  			printk("
  ");
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
728
729
730
731
732
733
734
  }
  
  /*
   * SB1000 commands for frame rx interrupt
   */
  /* receive a single frame and assemble datagram
   * (this is the heart of the interrupt routine)
   */
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
735
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
740
741
742
743
744
745
  sb1000_rx(struct net_device *dev)
  {
  
  #define FRAMESIZE 184
  	unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
  	short dlen;
  	int ioaddr, ns;
  	unsigned int skbsize;
  	struct sk_buff *skb;
  	struct sb1000_private *lp = netdev_priv(dev);
09f75cd7b   Jeff Garzik   [NET] drivers/net...
746
  	struct net_device_stats *stats = &dev->stats;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  
  	/* SB1000 frame constants */
  	const int FrameSize = FRAMESIZE;
  	const int NewDatagramHeaderSkip = 8;
  	const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
  	const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
  	const int ContDatagramHeaderSkip = 7;
  	const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
  	const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
  	const int TrailerSize = 4;
  
  	ioaddr = dev->base_addr;
  
  	insw(ioaddr, (unsigned short*) st, 1);
  #ifdef XXXDEBUG
  printk("cm0: received: %02x %02x
  ", st[0], st[1]);
  #endif /* XXXDEBUG */
  	lp->rx_frames++;
  
  	/* decide if it is a good or bad frame */
  	for (ns = 0; ns < NPIDS; ns++) {
  		session_id = lp->rx_session_id[ns];
  		frame_id = lp->rx_frame_id[ns];
  		if (st[0] == session_id) {
  			if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
  				goto good_frame;
  			} else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
  				goto skipped_frame;
  			} else {
  				goto bad_frame;
  			}
  		} else if (st[0] == (session_id | 0x40)) {
  			if ((st[1] & 0xf0) == 0x30) {
  				goto skipped_frame;
  			} else {
  				goto bad_frame;
  			}
  		}
  	}
  	goto bad_frame;
  
  skipped_frame:
  	stats->rx_frame_errors++;
  	skb = lp->rx_skb[ns];
  	if (sb1000_debug > 1)
  		printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
  			"expecting %02x %02x
  ", dev->name, st[0], st[1],
  			skb ? session_id : session_id | 0x40, frame_id);
  	if (skb) {
  		dev_kfree_skb(skb);
  		skb = NULL;
  	}
  
  good_frame:
  	lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
  	/* new datagram */
  	if (st[0] & 0x40) {
  		/* get data length */
  		insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
  #ifdef XXXDEBUG
  printk("cm0: IP identification: %02x%02x  fragment offset: %02x%02x
  ", buffer[30], buffer[31], buffer[32], buffer[33]);
  #endif /* XXXDEBUG */
  		if (buffer[0] != NewDatagramHeaderSkip) {
  			if (sb1000_debug > 1)
  				printk(KERN_WARNING "%s: new datagram header skip error: "
  					"got %02x expecting %02x
  ", dev->name, buffer[0],
  					NewDatagramHeaderSkip);
  			stats->rx_length_errors++;
  			insw(ioaddr, buffer, NewDatagramDataSize / 2);
  			goto bad_frame_next;
  		}
  		dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
  			buffer[NewDatagramHeaderSkip + 4]) - 17;
  		if (dlen > SB1000_MRU) {
  			if (sb1000_debug > 1)
  				printk(KERN_WARNING "%s: datagram length (%d) greater "
  					"than MRU (%d)
  ", dev->name, dlen, SB1000_MRU);
  			stats->rx_length_errors++;
  			insw(ioaddr, buffer, NewDatagramDataSize / 2);
  			goto bad_frame_next;
  		}
  		lp->rx_dlen[ns] = dlen;
  		/* compute size to allocate for datagram */
  		skbsize = dlen + FrameSize;
  		if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
  			if (sb1000_debug > 1)
  				printk(KERN_WARNING "%s: can't allocate %d bytes long "
  					"skbuff
  ", dev->name, skbsize);
  			stats->rx_dropped++;
  			insw(ioaddr, buffer, NewDatagramDataSize / 2);
  			goto dropped_frame;
  		}
  		skb->dev = dev;
459a98ed8   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
846
  		skb_reset_mac_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  		skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
  		insw(ioaddr, skb_put(skb, NewDatagramDataSize),
  			NewDatagramDataSize / 2);
  		lp->rx_skb[ns] = skb;
  	} else {
  		/* continuation of previous datagram */
  		insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
  		if (buffer[0] != ContDatagramHeaderSkip) {
  			if (sb1000_debug > 1)
  				printk(KERN_WARNING "%s: cont datagram header skip error: "
  					"got %02x expecting %02x
  ", dev->name, buffer[0],
  					ContDatagramHeaderSkip);
  			stats->rx_length_errors++;
  			insw(ioaddr, buffer, ContDatagramDataSize / 2);
  			goto bad_frame_next;
  		}
  		skb = lp->rx_skb[ns];
  		insw(ioaddr, skb_put(skb, ContDatagramDataSize),
  			ContDatagramDataSize / 2);
  		dlen = lp->rx_dlen[ns];
  	}
  	if (skb->len < dlen + TrailerSize) {
  		lp->rx_session_id[ns] &= ~0x40;
  		return 0;
  	}
  
  	/* datagram completed: send to upper level */
  	skb_trim(skb, dlen);
  	netif_rx(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  	stats->rx_bytes+=dlen;
  	stats->rx_packets++;
  	lp->rx_skb[ns] = NULL;
  	lp->rx_session_id[ns] |= 0x40;
  	return 0;
  
  bad_frame:
  	insw(ioaddr, buffer, FrameSize / 2);
  	if (sb1000_debug > 1)
  		printk(KERN_WARNING "%s: frame error: got %02x %02x
  ",
  			dev->name, st[0], st[1]);
  	stats->rx_frame_errors++;
  bad_frame_next:
  	if (sb1000_debug > 2)
  		sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
  dropped_frame:
  	stats->rx_errors++;
  	if (ns < NPIDS) {
  		if ((skb = lp->rx_skb[ns])) {
  			dev_kfree_skb(skb);
  			lp->rx_skb[ns] = NULL;
  		}
  		lp->rx_session_id[ns] |= 0x40;
  	}
  	return -1;
  }
a8d06342b   Denys Vlasenko   sb1000.c: stop in...
904
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
906
  sb1000_error_dpc(struct net_device *dev)
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
907
  	static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
909
910
911
  	char *name;
  	unsigned char st[5];
  	int ioaddr[2];
  	struct sb1000_private *lp = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
913
914
915
916
917
918
919
920
921
922
923
924
  	const int ErrorDpcCounterInitialize = 200;
  
  	ioaddr[0] = dev->base_addr;
  	/* mem_start holds the second I/O address */
  	ioaddr[1] = dev->mem_start;
  	name = dev->name;
  
  	sb1000_wait_for_ready_clear(ioaddr, name);
  	sb1000_send_command(ioaddr, name, Command0);
  	sb1000_wait_for_ready(ioaddr, name);
  	sb1000_read_status(ioaddr, st);
  	if (st[1] & 0x10)
  		lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
925
  }
6aa20a223   Jeff Garzik   drivers/net: Trim...
926

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
  /*
   * Linux interface functions
   */
  static int
  sb1000_open(struct net_device *dev)
  {
  	char *name;
  	int ioaddr[2], status;
  	struct sb1000_private *lp = netdev_priv(dev);
  	const unsigned short FirmwareVersion[] = {0x01, 0x01};
  
  	ioaddr[0] = dev->base_addr;
  	/* mem_start holds the second I/O address */
  	ioaddr[1] = dev->mem_start;
  	name = dev->name;
  
  	/* initialize sb1000 */
  	if ((status = sb1000_reset(ioaddr, name)))
  		return status;
a26c074c1   Nishanth Aravamudan   [PATCH] net/sb100...
946
  	ssleep(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
  	if ((status = sb1000_check_CRC(ioaddr, name)))
  		return status;
  
  	/* initialize private data before board can catch interrupts */
  	lp->rx_skb[0] = NULL;
  	lp->rx_skb[1] = NULL;
  	lp->rx_skb[2] = NULL;
  	lp->rx_skb[3] = NULL;
  	lp->rx_dlen[0] = 0;
  	lp->rx_dlen[1] = 0;
  	lp->rx_dlen[2] = 0;
  	lp->rx_dlen[3] = 0;
  	lp->rx_frames = 0;
  	lp->rx_error_count = 0;
  	lp->rx_error_dpc_count = 0;
  	lp->rx_session_id[0] = 0x50;
d58c0e956   Julia Lawall   drivers/net/sb100...
963
964
965
  	lp->rx_session_id[1] = 0x48;
  	lp->rx_session_id[2] = 0x44;
  	lp->rx_session_id[3] = 0x42;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
966
967
968
969
  	lp->rx_frame_id[0] = 0;
  	lp->rx_frame_id[1] = 0;
  	lp->rx_frame_id[2] = 0;
  	lp->rx_frame_id[3] = 0;
a0607fd3a   Joe Perches   drivers/net: requ...
970
  	if (request_irq(dev->irq, sb1000_interrupt, 0, "sb1000", dev)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  		return -EAGAIN;
  	}
  
  	if (sb1000_debug > 2)
  		printk(KERN_DEBUG "%s: Opening, IRQ %d
  ", name, dev->irq);
  
  	/* Activate board and check firmware version */
  	udelay(1000);
  	if ((status = sb1000_activate(ioaddr, name)))
  		return status;
  	udelay(0);
  	if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
  		return status;
  	if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
  		printk(KERN_WARNING "%s: found firmware version %x.%02x "
  			"(should be %x.%02x)
  ", name, version[0], version[1],
  			FirmwareVersion[0], FirmwareVersion[1]);
  
  
  	netif_start_queue(dev);
  	return 0;					/* Always succeed */
  }
  
  static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  {
  	char* name;
  	unsigned char version[2];
  	short PID[4];
  	int ioaddr[2], status, frequency;
  	unsigned int stats[5];
  	struct sb1000_private *lp = netdev_priv(dev);
  
  	if (!(dev && dev->flags & IFF_UP))
  		return -ENODEV;
  
  	ioaddr[0] = dev->base_addr;
  	/* mem_start holds the second I/O address */
  	ioaddr[1] = dev->mem_start;
  	name = dev->name;
  
  	switch (cmd) {
  	case SIOCGCMSTATS:		/* get statistics */
09f75cd7b   Jeff Garzik   [NET] drivers/net...
1015
  		stats[0] = dev->stats.rx_bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1016
  		stats[1] = lp->rx_frames;
09f75cd7b   Jeff Garzik   [NET] drivers/net...
1017
1018
1019
  		stats[2] = dev->stats.rx_packets;
  		stats[3] = dev->stats.rx_errors;
  		stats[4] = dev->stats.rx_dropped;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  		if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
  			return -EFAULT;
  		status = 0;
  		break;
  
  	case SIOCGCMFIRMWARE:		/* get firmware version */
  		if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
  			return status;
  		if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
  			return -EFAULT;
  		break;
  
  	case SIOCGCMFREQUENCY:		/* get frequency */
  		if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
  			return status;
  		if(put_user(frequency, (int __user *) ifr->ifr_data))
  			return -EFAULT;
  		break;
  
  	case SIOCSCMFREQUENCY:		/* set frequency */
  		if (!capable(CAP_NET_ADMIN))
  			return -EPERM;
  		if(get_user(frequency, (int __user *) ifr->ifr_data))
  			return -EFAULT;
  		if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
  			return status;
  		break;
  
  	case SIOCGCMPIDS:			/* get PIDs */
  		if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
  			return status;
  		if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
  			return -EFAULT;
  		break;
  
  	case SIOCSCMPIDS:			/* set PIDs */
  		if (!capable(CAP_NET_ADMIN))
  			return -EPERM;
  		if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
  			return -EFAULT;
  		if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
  			return status;
  		/* set session_id, frame_id and pkt_type too */
  		lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
  		lp->rx_session_id[1] = 0x48;
  		lp->rx_session_id[2] = 0x44;
  		lp->rx_session_id[3] = 0x42;
  		lp->rx_frame_id[0] = 0;
  		lp->rx_frame_id[1] = 0;
  		lp->rx_frame_id[2] = 0;
  		lp->rx_frame_id[3] = 0;
  		break;
  
  	default:
  		status = -EINVAL;
  		break;
  	}
  	return status;
  }
  
  /* transmit function: do nothing since SB1000 can't send anything out */
61357325f   Stephen Hemminger   netdev: convert b...
1081
  static netdev_tx_t
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082
1083
1084
1085
1086
1087
  sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  {
  	printk(KERN_WARNING "%s: trying to transmit!!!
  ", dev->name);
  	/* sb1000 can't xmit datagrams */
  	dev_kfree_skb(skb);
6ed106549   Patrick McHardy   net: use NETDEV_T...
1088
  	return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1089
1090
1091
  }
  
  /* SB1000 interrupt handler. */
7d12e780e   David Howells   IRQ: Maintain reg...
1092
  static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1093
  {
7dd73bbcc   Denys Vlasenko   sb1000.c: make co...
1094
1095
  	static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
  	static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1096
1097
1098
  	char *name;
  	unsigned char st;
  	int ioaddr[2];
c31f28e77   Jeff Garzik   drivers/net: elim...
1099
  	struct net_device *dev = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
  	struct sb1000_private *lp = netdev_priv(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1101
  	const int MaxRxErrorCount = 6;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  	ioaddr[0] = dev->base_addr;
  	/* mem_start holds the second I/O address */
  	ioaddr[1] = dev->mem_start;
  	name = dev->name;
  
  	/* is it a good interrupt? */
  	st = inb(ioaddr[1] + 6);
  	if (!(st & 0x08 && st & 0x20)) {
  		return IRQ_NONE;
  	}
  
  	if (sb1000_debug > 3)
  		printk(KERN_DEBUG "%s: entering interrupt
  ", dev->name);
  
  	st = inb(ioaddr[0] + 7);
  	if (sb1000_rx(dev))
  		lp->rx_error_count++;
  #ifdef SB1000_DELAY
  	udelay(SB1000_DELAY);
  #endif /* SB1000_DELAY */
  	sb1000_issue_read_command(ioaddr, name);
  	if (st & 0x01) {
  		sb1000_error_dpc(dev);
  		sb1000_issue_read_command(ioaddr, name);
  	}
  	if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
  		sb1000_wait_for_ready_clear(ioaddr, name);
  		sb1000_send_command(ioaddr, name, Command0);
  		sb1000_wait_for_ready(ioaddr, name);
  		sb1000_issue_read_command(ioaddr, name);
  	}
  	if (lp->rx_error_count >= MaxRxErrorCount) {
  		sb1000_wait_for_ready_clear(ioaddr, name);
  		sb1000_send_command(ioaddr, name, Command1);
  		sb1000_wait_for_ready(ioaddr, name);
  		sb1000_issue_read_command(ioaddr, name);
  		lp->rx_error_count = 0;
  	}
  
  	return IRQ_HANDLED;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
  static int sb1000_close(struct net_device *dev)
  {
  	int i;
  	int ioaddr[2];
  	struct sb1000_private *lp = netdev_priv(dev);
  
  	if (sb1000_debug > 2)
  		printk(KERN_DEBUG "%s: Shutting down sb1000.
  ", dev->name);
  
  	netif_stop_queue(dev);
6aa20a223   Jeff Garzik   drivers/net: Trim...
1155

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
  	ioaddr[0] = dev->base_addr;
  	/* mem_start holds the second I/O address */
  	ioaddr[1] = dev->mem_start;
  
  	free_irq(dev->irq, dev);
  	/* If we don't do this, we can't re-insmod it later. */
  	release_region(ioaddr[1], SB1000_IO_EXTENT);
  	release_region(ioaddr[0], SB1000_IO_EXTENT);
  
  	/* free rx_skb's if needed */
  	for (i=0; i<4; i++) {
  		if (lp->rx_skb[i]) {
  			dev_kfree_skb(lp->rx_skb[i]);
  		}
  	}
  	return 0;
  }
  
  MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
  MODULE_DESCRIPTION("General Instruments SB1000 driver");
  MODULE_LICENSE("GPL");
  
  static int __init
  sb1000_init(void)
  {
  	return pnp_register_driver(&sb1000_driver);
  }
  
  static void __exit
  sb1000_exit(void)
  {
  	pnp_unregister_driver(&sb1000_driver);
  }
  
  module_init(sb1000_init);
  module_exit(sb1000_exit);