1 /*
2  * SDLA		An implementation of a driver for the Sangoma S502/S508 series
3  *		multi-protocol PC interface card.  Initial offering is with
4  *		the DLCI driver, providing Frame Relay support for linux.
5  *
6  *		Global definitions for the Frame relay interface.
7  *
8  * Version:	@(#)sdla.c   0.30	12 Sep 1996
9  *
10  * Credits:	Sangoma Technologies, for the use of 2 cards for an extended
11  *			period of time.
12  *		David Mandelstam <dm@sangoma.com> for getting me started on
13  *			this project, and incentive to complete it.
14  *		Gene Kozen <74604.152@compuserve.com> for providing me with
15  *			important information about the cards.
16  *
17  * Author:	Mike McLagan <mike.mclagan@linux.org>
18  *
19  * Changes:
20  *		0.15	Mike McLagan	Improved error handling, packet dropping
21  *		0.20	Mike McLagan	New transmit/receive flags for config
22  *					If in FR mode, don't accept packets from
23  *					non DLCI devices.
24  *		0.25	Mike McLagan	Fixed problem with rejecting packets
25  *					from non DLCI devices.
26  *		0.30	Mike McLagan	Fixed kernel panic when used with modified
27  *					ifconfig
28  *
29  *		This program is free software; you can redistribute it and/or
30  *		modify it under the terms of the GNU General Public License
31  *		as published by the Free Software Foundation; either version
32  *		2 of the License, or (at your option) any later version.
33  */
34 
35 #include <linux/config.h> /* for CONFIG_DLCI_MAX */
36 #include <linux/module.h>
37 
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/types.h>
41 #include <linux/fcntl.h>
42 #include <linux/interrupt.h>
43 #include <linux/ptrace.h>
44 #include <linux/ioport.h>
45 #include <linux/in.h>
46 #include <linux/slab.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <asm/io.h>
55 #include <asm/dma.h>
56 #include <asm/uaccess.h>
57 
58 #include <linux/netdevice.h>
59 #include <linux/skbuff.h>
60 #include <linux/if_arp.h>
61 #include <linux/if_frad.h>
62 
63 #include <linux/sdla.h>
64 
65 static const char* version = "SDLA driver v0.30, 12 Sep 1996, mike.mclagan@linux.org";
66 
67 static const char* devname = "sdla";
68 
69 static unsigned int valid_port[] __initdata = { 0x250, 0x270, 0x280, 0x300, 0x350, 0x360, 0x380, 0x390};
70 
71 static unsigned int valid_mem[]  __initdata = {
72 				    0xA0000, 0xA2000, 0xA4000, 0xA6000, 0xA8000, 0xAA000, 0xAC000, 0xAE000,
73                                     0xB0000, 0xB2000, 0xB4000, 0xB6000, 0xB8000, 0xBA000, 0xBC000, 0xBE000,
74                                     0xC0000, 0xC2000, 0xC4000, 0xC6000, 0xC8000, 0xCA000, 0xCC000, 0xCE000,
75                                     0xD0000, 0xD2000, 0xD4000, 0xD6000, 0xD8000, 0xDA000, 0xDC000, 0xDE000,
76                                     0xE0000, 0xE2000, 0xE4000, 0xE6000, 0xE8000, 0xEA000, 0xEC000, 0xEE000};
77 
78 /*********************************************************
79  *
80  * these are the core routines that access the card itself
81  *
82  *********************************************************/
83 
84 #define SDLA_WINDOW(dev,addr) outb((((addr) >> 13) & 0x1F), (dev)->base_addr + SDLA_REG_Z80_WINDOW)
85 
sdla_read(struct net_device * dev,int addr,void * buf,short len)86 static void sdla_read(struct net_device *dev, int addr, void *buf, short len)
87 {
88 	unsigned long flags;
89 	char          *temp, *base;
90 	int           offset, bytes;
91 
92 	temp = buf;
93 	while(len)
94 	{
95 		offset = addr & SDLA_ADDR_MASK;
96 		bytes = offset + len > SDLA_WINDOW_SIZE ? SDLA_WINDOW_SIZE - offset : len;
97 		base = (void *) (dev->mem_start + offset);
98 
99 		save_flags(flags);
100 		cli();
101 		SDLA_WINDOW(dev, addr);
102 		memcpy(temp, base, bytes);
103 		restore_flags(flags);
104 
105 		addr += bytes;
106 		temp += bytes;
107 		len  -= bytes;
108 	}
109 }
110 
sdla_write(struct net_device * dev,int addr,void * buf,short len)111 static void sdla_write(struct net_device *dev, int addr, void *buf, short len)
112 {
113 	unsigned long flags;
114 	char          *temp, *base;
115 	int           offset, bytes;
116 
117 	temp = buf;
118 	while(len)
119 	{
120 		offset = addr & SDLA_ADDR_MASK;
121 		bytes = offset + len > SDLA_WINDOW_SIZE ? SDLA_WINDOW_SIZE - offset : len;
122 		base = (void *) (dev->mem_start + offset);
123 		save_flags(flags);
124 		cli();
125 		SDLA_WINDOW(dev, addr);
126 		memcpy(base, temp, bytes);
127 		restore_flags(flags);
128 		addr += bytes;
129 		temp += bytes;
130 		len  -= bytes;
131 	}
132 }
133 
sdla_clear(struct net_device * dev)134 static void sdla_clear(struct net_device *dev)
135 {
136 	unsigned long flags;
137 	char          *base;
138 	int           len, addr, bytes;
139 
140 	len = 65536;
141 	addr = 0;
142 	bytes = SDLA_WINDOW_SIZE;
143 	base = (void *) dev->mem_start;
144 
145 	save_flags(flags);
146 	cli();
147 	while(len)
148 	{
149 		SDLA_WINDOW(dev, addr);
150 		memset(base, 0, bytes);
151 
152 		addr += bytes;
153 		len  -= bytes;
154 	}
155 	restore_flags(flags);
156 }
157 
sdla_byte(struct net_device * dev,int addr)158 static char sdla_byte(struct net_device *dev, int addr)
159 {
160 	unsigned long flags;
161 	char          byte, *temp;
162 
163 	temp = (void *) (dev->mem_start + (addr & SDLA_ADDR_MASK));
164 
165 	save_flags(flags);
166 	cli();
167 	SDLA_WINDOW(dev, addr);
168 	byte = *temp;
169 	restore_flags(flags);
170 
171 	return(byte);
172 }
173 
sdla_stop(struct net_device * dev)174 void sdla_stop(struct net_device *dev)
175 {
176 	struct frad_local *flp;
177 
178 	flp = dev->priv;
179 	switch(flp->type)
180 	{
181 		case SDLA_S502A:
182 			outb(SDLA_S502A_HALT, dev->base_addr + SDLA_REG_CONTROL);
183 			flp->state = SDLA_HALT;
184 			break;
185 		case SDLA_S502E:
186 			outb(SDLA_HALT, dev->base_addr + SDLA_REG_Z80_CONTROL);
187 			outb(SDLA_S502E_ENABLE, dev->base_addr + SDLA_REG_CONTROL);
188 			flp->state = SDLA_S502E_ENABLE;
189 			break;
190 		case SDLA_S507:
191 			flp->state &= ~SDLA_CPUEN;
192 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
193 			break;
194 		case SDLA_S508:
195 			flp->state &= ~SDLA_CPUEN;
196 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
197 			break;
198 	}
199 }
200 
sdla_start(struct net_device * dev)201 void sdla_start(struct net_device *dev)
202 {
203 	struct frad_local *flp;
204 
205 	flp = dev->priv;
206 	switch(flp->type)
207 	{
208 		case SDLA_S502A:
209 			outb(SDLA_S502A_NMI, dev->base_addr + SDLA_REG_CONTROL);
210 			outb(SDLA_S502A_START, dev->base_addr + SDLA_REG_CONTROL);
211 			flp->state = SDLA_S502A_START;
212 			break;
213 		case SDLA_S502E:
214 			outb(SDLA_S502E_CPUEN, dev->base_addr + SDLA_REG_Z80_CONTROL);
215 			outb(0x00, dev->base_addr + SDLA_REG_CONTROL);
216 			flp->state = 0;
217 			break;
218 		case SDLA_S507:
219 			flp->state |= SDLA_CPUEN;
220 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
221 			break;
222 		case SDLA_S508:
223 			flp->state |= SDLA_CPUEN;
224 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
225 			break;
226 	}
227 }
228 
229 /****************************************************
230  *
231  * this is used for the S502A/E cards to determine
232  * the speed of the onboard CPU.  Calibration is
233  * necessary for the Frame Relay code uploaded
234  * later.  Incorrect results cause timing problems
235  * with link checks & status messages
236  *
237  ***************************************************/
238 
sdla_z80_poll(struct net_device * dev,int z80_addr,int jiffs,char resp1,char resp2)239 int sdla_z80_poll(struct net_device *dev, int z80_addr, int jiffs, char resp1, char resp2)
240 {
241 	unsigned long start, done, now;
242 	char          resp, *temp;
243 
244 	start = now = jiffies;
245 	done = jiffies + jiffs;
246 
247 	temp = (void *)dev->mem_start;
248 	temp += z80_addr & SDLA_ADDR_MASK;
249 
250 	resp = ~resp1;
251 	while (time_before(jiffies, done) && (resp != resp1) && (!resp2 || (resp != resp2)))
252 	{
253 		if (jiffies != now)
254 		{
255 			SDLA_WINDOW(dev, z80_addr);
256 			now = jiffies;
257 			resp = *temp;
258 		}
259 	}
260 	return(time_before(jiffies, done) ? jiffies - start : -1);
261 }
262 
263 /* constants for Z80 CPU speed */
264 #define Z80_READY 		'1'	/* Z80 is ready to begin */
265 #define LOADER_READY 		'2'	/* driver is ready to begin */
266 #define Z80_SCC_OK 		'3'	/* SCC is on board */
267 #define Z80_SCC_BAD	 	'4'	/* SCC was not found */
268 
sdla_cpuspeed(struct net_device * dev,struct ifreq * ifr)269 static int sdla_cpuspeed(struct net_device *dev, struct ifreq *ifr)
270 {
271 	int  jiffs;
272 	char data;
273 
274 	sdla_start(dev);
275 	if (sdla_z80_poll(dev, 0, 3*HZ, Z80_READY, 0) < 0)
276 		return(-EIO);
277 
278 	data = LOADER_READY;
279 	sdla_write(dev, 0, &data, 1);
280 
281 	if ((jiffs = sdla_z80_poll(dev, 0, 8*HZ, Z80_SCC_OK, Z80_SCC_BAD)) < 0)
282 		return(-EIO);
283 
284 	sdla_stop(dev);
285 	sdla_read(dev, 0, &data, 1);
286 
287 	if (data == Z80_SCC_BAD)
288 	{
289 		printk("%s: SCC bad\n", dev->name);
290 		return(-EIO);
291 	}
292 
293 	if (data != Z80_SCC_OK)
294 		return(-EINVAL);
295 
296 	if (jiffs < 165)
297 		ifr->ifr_mtu = SDLA_CPU_16M;
298 	else if (jiffs < 220)
299 		ifr->ifr_mtu = SDLA_CPU_10M;
300 	else if (jiffs < 258)
301 		ifr->ifr_mtu = SDLA_CPU_8M;
302 	else if (jiffs < 357)
303 		ifr->ifr_mtu = SDLA_CPU_7M;
304 	else if (jiffs < 467)
305 		ifr->ifr_mtu = SDLA_CPU_5M;
306 	else
307 		ifr->ifr_mtu = SDLA_CPU_3M;
308 
309 	return(0);
310 }
311 
312 /************************************************
313  *
314  *  Direct interaction with the Frame Relay code
315  *  starts here.
316  *
317  ************************************************/
318 
319 struct _dlci_stat
320 {
321 	short dlci		__attribute__((packed));
322 	char  flags;
323 };
324 
325 struct _frad_stat
326 {
327 	char    flags;
328 	struct _dlci_stat dlcis[SDLA_MAX_DLCI];
329 };
330 
sdla_errors(struct net_device * dev,int cmd,int dlci,int ret,int len,void * data)331 static void sdla_errors(struct net_device *dev, int cmd, int dlci, int ret, int len, void *data)
332 {
333 	struct _dlci_stat *pstatus;
334 	short             *pdlci;
335 	int               i;
336 	char              *state, line[30];
337 
338 	switch (ret)
339 	{
340 		case SDLA_RET_MODEM:
341 			state = data;
342 			if (*state & SDLA_MODEM_DCD_LOW)
343 				printk(KERN_INFO "%s: Modem DCD unexpectedly low!\n", dev->name);
344 			if (*state & SDLA_MODEM_CTS_LOW)
345 				printk(KERN_INFO "%s: Modem CTS unexpectedly low!\n", dev->name);
346 			/* I should probably do something about this! */
347 			break;
348 
349 		case SDLA_RET_CHANNEL_OFF:
350 			printk(KERN_INFO "%s: Channel became inoperative!\n", dev->name);
351 			/* same here */
352 			break;
353 
354 		case SDLA_RET_CHANNEL_ON:
355 			printk(KERN_INFO "%s: Channel became operative!\n", dev->name);
356 			/* same here */
357 			break;
358 
359 		case SDLA_RET_DLCI_STATUS:
360 			printk(KERN_INFO "%s: Status change reported by Access Node.\n", dev->name);
361 			len /= sizeof(struct _dlci_stat);
362 			for(pstatus = data, i=0;i < len;i++,pstatus++)
363 			{
364 				if (pstatus->flags & SDLA_DLCI_NEW)
365 					state = "new";
366 				else if (pstatus->flags & SDLA_DLCI_DELETED)
367 					state = "deleted";
368 				else if (pstatus->flags & SDLA_DLCI_ACTIVE)
369 					state = "active";
370 				else
371 				{
372 					sprintf(line, "unknown status: %02X", pstatus->flags);
373 					state = line;
374 				}
375 				printk(KERN_INFO "%s: DLCI %i: %s.\n", dev->name, pstatus->dlci, state);
376 				/* same here */
377 			}
378 			break;
379 
380 		case SDLA_RET_DLCI_UNKNOWN:
381 			printk(KERN_INFO "%s: Received unknown DLCIs:", dev->name);
382 			len /= sizeof(short);
383 			for(pdlci = data,i=0;i < len;i++,pdlci++)
384 				printk(" %i", *pdlci);
385 			printk("\n");
386 			break;
387 
388 		case SDLA_RET_TIMEOUT:
389 			printk(KERN_ERR "%s: Command timed out!\n", dev->name);
390 			break;
391 
392 		case SDLA_RET_BUF_OVERSIZE:
393 			printk(KERN_INFO "%s: Bc/CIR overflow, acceptable size is %i\n", dev->name, len);
394 			break;
395 
396 		case SDLA_RET_BUF_TOO_BIG:
397 			printk(KERN_INFO "%s: Buffer size over specified max of %i\n", dev->name, len);
398 			break;
399 
400 		case SDLA_RET_CHANNEL_INACTIVE:
401 		case SDLA_RET_DLCI_INACTIVE:
402 		case SDLA_RET_CIR_OVERFLOW:
403 		case SDLA_RET_NO_BUFS:
404 			if (cmd == SDLA_INFORMATION_WRITE)
405 				break;
406 
407 		default:
408 			printk(KERN_DEBUG "%s: Cmd 0x%2.2X generated return code 0x%2.2X\n", dev->name, cmd, ret);
409 			/* Further processing could be done here */
410 			break;
411 	}
412 }
413 
sdla_cmd(struct net_device * dev,int cmd,short dlci,short flags,void * inbuf,short inlen,void * outbuf,short * outlen)414 static int sdla_cmd(struct net_device *dev, int cmd, short dlci, short flags,
415                         void *inbuf, short inlen, void *outbuf, short *outlen)
416 {
417 	static struct _frad_stat status;
418 	struct frad_local        *flp;
419 	struct sdla_cmd          *cmd_buf;
420 	unsigned long            pflags;
421 	int                      jiffs, ret, waiting, len;
422 	long                     window;
423 
424 	flp = dev->priv;
425 	window = flp->type == SDLA_S508 ? SDLA_508_CMD_BUF : SDLA_502_CMD_BUF;
426 	cmd_buf = (struct sdla_cmd *)(dev->mem_start + (window & SDLA_ADDR_MASK));
427 	ret = 0;
428 	len = 0;
429 	jiffs = jiffies + HZ;  /* 1 second is plenty */
430 	save_flags(pflags);
431 	cli();
432 	SDLA_WINDOW(dev, window);
433 	cmd_buf->cmd = cmd;
434 	cmd_buf->dlci = dlci;
435 	cmd_buf->flags = flags;
436 
437 	if (inbuf)
438 		memcpy(cmd_buf->data, inbuf, inlen);
439 
440 	cmd_buf->length = inlen;
441 
442 	cmd_buf->opp_flag = 1;
443 	restore_flags(pflags);
444 
445 	waiting = 1;
446 	len = 0;
447 	while (waiting && time_before_eq(jiffies, jiffs))
448 	{
449 		if (waiting++ % 3)
450 		{
451 			save_flags(pflags);
452 			cli();
453 			SDLA_WINDOW(dev, window);
454 			waiting = ((volatile int)(cmd_buf->opp_flag));
455 			restore_flags(pflags);
456 		}
457 	}
458 
459 	if (!waiting)
460 	{
461 		save_flags(pflags);
462 		cli();
463 		SDLA_WINDOW(dev, window);
464 		ret = cmd_buf->retval;
465 		len = cmd_buf->length;
466 		if (outbuf && outlen)
467 		{
468 			*outlen = *outlen >= len ? len : *outlen;
469 
470 			if (*outlen)
471 				memcpy(outbuf, cmd_buf->data, *outlen);
472 		}
473 
474 		/* This is a local copy that's used for error handling */
475 		if (ret)
476 			memcpy(&status, cmd_buf->data, len > sizeof(status) ? sizeof(status) : len);
477 
478 		restore_flags(pflags);
479 	}
480 	else
481 		ret = SDLA_RET_TIMEOUT;
482 
483 	if (ret != SDLA_RET_OK)
484 	   	sdla_errors(dev, cmd, dlci, ret, len, &status);
485 
486 	return(ret);
487 }
488 
489 /***********************************************
490  *
491  * these functions are called by the DLCI driver
492  *
493  ***********************************************/
494 
495 static int sdla_reconfig(struct net_device *dev);
496 
sdla_activate(struct net_device * slave,struct net_device * master)497 int sdla_activate(struct net_device *slave, struct net_device *master)
498 {
499 	struct frad_local *flp;
500 	int i;
501 
502 	flp = slave->priv;
503 
504 	for(i=0;i<CONFIG_DLCI_MAX;i++)
505 		if (flp->master[i] == master)
506 			break;
507 
508 	if (i == CONFIG_DLCI_MAX)
509 		return(-ENODEV);
510 
511 	flp->dlci[i] = abs(flp->dlci[i]);
512 
513 	if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
514 		sdla_cmd(slave, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
515 
516 	return(0);
517 }
518 
sdla_deactivate(struct net_device * slave,struct net_device * master)519 int sdla_deactivate(struct net_device *slave, struct net_device *master)
520 {
521 	struct frad_local *flp;
522 	int               i;
523 
524 	flp = slave->priv;
525 
526 	for(i=0;i<CONFIG_DLCI_MAX;i++)
527 		if (flp->master[i] == master)
528 			break;
529 
530 	if (i == CONFIG_DLCI_MAX)
531 		return(-ENODEV);
532 
533 	flp->dlci[i] = -abs(flp->dlci[i]);
534 
535 	if (netif_running(slave) && (flp->config.station == FRAD_STATION_NODE))
536 		sdla_cmd(slave, SDLA_DEACTIVATE_DLCI, 0, 0, &flp->dlci[i], sizeof(short), NULL, NULL);
537 
538 	return(0);
539 }
540 
sdla_assoc(struct net_device * slave,struct net_device * master)541 int sdla_assoc(struct net_device *slave, struct net_device *master)
542 {
543 	struct frad_local *flp;
544 	int               i;
545 
546 	if (master->type != ARPHRD_DLCI)
547 		return(-EINVAL);
548 
549 	flp = slave->priv;
550 
551 	for(i=0;i<CONFIG_DLCI_MAX;i++)
552 	{
553 		if (!flp->master[i])
554 			break;
555 		if (abs(flp->dlci[i]) == *(short *)(master->dev_addr))
556 			return(-EADDRINUSE);
557 	}
558 
559 	if (i == CONFIG_DLCI_MAX)
560 		return(-EMLINK);  /* #### Alan: Comments on this ?? */
561 
562 	MOD_INC_USE_COUNT;
563 
564 	flp->master[i] = master;
565 	flp->dlci[i] = -*(short *)(master->dev_addr);
566 	master->mtu = slave->mtu;
567 
568 	if (netif_running(slave)) {
569 		if (flp->config.station == FRAD_STATION_CPE)
570 			sdla_reconfig(slave);
571 		else
572 			sdla_cmd(slave, SDLA_ADD_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
573 	}
574 
575 	return(0);
576 }
577 
sdla_deassoc(struct net_device * slave,struct net_device * master)578 int sdla_deassoc(struct net_device *slave, struct net_device *master)
579 {
580 	struct frad_local *flp;
581 	int               i;
582 
583 	flp = slave->priv;
584 
585 	for(i=0;i<CONFIG_DLCI_MAX;i++)
586 		if (flp->master[i] == master)
587 			break;
588 
589 	if (i == CONFIG_DLCI_MAX)
590 		return(-ENODEV);
591 
592 	flp->master[i] = NULL;
593 	flp->dlci[i] = 0;
594 
595 	MOD_DEC_USE_COUNT;
596 
597 	if (netif_running(slave)) {
598 		if (flp->config.station == FRAD_STATION_CPE)
599 			sdla_reconfig(slave);
600 		else
601 			sdla_cmd(slave, SDLA_DELETE_DLCI, 0, 0, master->dev_addr, sizeof(short), NULL, NULL);
602 	}
603 
604 	return(0);
605 }
606 
sdla_dlci_conf(struct net_device * slave,struct net_device * master,int get)607 int sdla_dlci_conf(struct net_device *slave, struct net_device *master, int get)
608 {
609 	struct frad_local *flp;
610 	struct dlci_local *dlp;
611 	int               i;
612 	short             len, ret;
613 
614 	flp = slave->priv;
615 
616 	for(i=0;i<CONFIG_DLCI_MAX;i++)
617 		if (flp->master[i] == master)
618 			break;
619 
620 	if (i == CONFIG_DLCI_MAX)
621 		return(-ENODEV);
622 
623 	dlp = master->priv;
624 
625 	ret = SDLA_RET_OK;
626 	len = sizeof(struct dlci_conf);
627 	if (netif_running(slave)) {
628 		if (get)
629 			ret = sdla_cmd(slave, SDLA_READ_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,
630 			            NULL, 0, &dlp->config, &len);
631 		else
632 			ret = sdla_cmd(slave, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0,
633 			            &dlp->config, sizeof(struct dlci_conf) - 4 * sizeof(short), NULL, NULL);
634 	}
635 
636 	return(ret == SDLA_RET_OK ? 0 : -EIO);
637 }
638 
639 /**************************
640  *
641  * now for the Linux driver
642  *
643  **************************/
644 
645 /* NOTE: the DLCI driver deals with freeing the SKB!! */
sdla_transmit(struct sk_buff * skb,struct net_device * dev)646 static int sdla_transmit(struct sk_buff *skb, struct net_device *dev)
647 {
648 	struct frad_local *flp;
649 	int               ret, addr, accept, i;
650 	short             size;
651 	unsigned long     flags;
652 	struct buf_entry  *pbuf;
653 
654 	flp = dev->priv;
655 	ret = 0;
656 	accept = 1;
657 
658 	netif_stop_queue(dev);
659 
660 	/*
661 	 * stupid GateD insists on setting up the multicast router thru us
662 	 * and we're ill equipped to handle a non Frame Relay packet at this
663 	 * time!
664 	 */
665 
666 	accept = 1;
667 	switch (dev->type)
668 	{
669 		case ARPHRD_FRAD:
670 			if (skb->dev->type != ARPHRD_DLCI)
671 			{
672 				printk(KERN_WARNING "%s: Non DLCI device, type %i, tried to send on FRAD module.\n", dev->name, skb->dev->type);
673 				accept = 0;
674 			}
675 			break;
676 		default:
677 			printk(KERN_WARNING "%s: unknown firmware type 0x%4.4X\n", dev->name, dev->type);
678 			accept = 0;
679 			break;
680 	}
681 	if (accept)
682 	{
683 		/* this is frame specific, but till there's a PPP module, it's the default */
684 		switch (flp->type)
685 		{
686 			case SDLA_S502A:
687 			case SDLA_S502E:
688 				ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, skb->data, skb->len, NULL, NULL);
689 				break;
690 				case SDLA_S508:
691 				size = sizeof(addr);
692 				ret = sdla_cmd(dev, SDLA_INFORMATION_WRITE, *(short *)(skb->dev->dev_addr), 0, NULL, skb->len, &addr, &size);
693 				if (ret == SDLA_RET_OK)
694 				{
695 					save_flags(flags);
696 					cli();
697 					SDLA_WINDOW(dev, addr);
698 					pbuf = (void *)(((unsigned long) dev->mem_start) + (addr & SDLA_ADDR_MASK));
699 						sdla_write(dev, pbuf->buf_addr, skb->data, skb->len);
700 						SDLA_WINDOW(dev, addr);
701 					pbuf->opp_flag = 1;
702 					restore_flags(flags);
703 				}
704 				break;
705 		}
706 		switch (ret)
707 		{
708 			case SDLA_RET_OK:
709 				flp->stats.tx_packets++;
710 				ret = DLCI_RET_OK;
711 				break;
712 
713 			case SDLA_RET_CIR_OVERFLOW:
714 			case SDLA_RET_BUF_OVERSIZE:
715 			case SDLA_RET_NO_BUFS:
716 				flp->stats.tx_dropped++;
717 				ret = DLCI_RET_DROP;
718 				break;
719 
720 			default:
721 				flp->stats.tx_errors++;
722 				ret = DLCI_RET_ERR;
723 				break;
724 		}
725 	}
726 	netif_wake_queue(dev);
727 	for(i=0;i<CONFIG_DLCI_MAX;i++)
728 	{
729 		if(flp->master[i]!=NULL)
730 			netif_wake_queue(flp->master[i]);
731 	}
732 	return(ret);
733 }
734 
sdla_receive(struct net_device * dev)735 static void sdla_receive(struct net_device *dev)
736 {
737 	struct net_device	  *master;
738 	struct frad_local *flp;
739 	struct dlci_local *dlp;
740 	struct sk_buff	 *skb;
741 
742 	struct sdla_cmd	*cmd;
743 	struct buf_info	*pbufi;
744 	struct buf_entry  *pbuf;
745 
746 	unsigned long	  flags;
747 	int               i=0, received, success, addr, buf_base, buf_top;
748 	short             dlci, len, len2, split;
749 
750 	flp = dev->priv;
751 	success = 1;
752 	received = addr = buf_top = buf_base = 0;
753 	len = dlci = 0;
754 	skb = NULL;
755 	master = NULL;
756 	cmd = NULL;
757 	pbufi = NULL;
758 	pbuf = NULL;
759 
760 	save_flags(flags);
761 	cli();
762 
763 	switch (flp->type)
764 	{
765 		case SDLA_S502A:
766 		case SDLA_S502E:
767 			cmd = (void *) (dev->mem_start + (SDLA_502_RCV_BUF & SDLA_ADDR_MASK));
768 			SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
769 			success = cmd->opp_flag;
770 			if (!success)
771 				break;
772 
773 			dlci = cmd->dlci;
774 			len = cmd->length;
775 			break;
776 
777 		case SDLA_S508:
778 			pbufi = (void *) (dev->mem_start + (SDLA_508_RXBUF_INFO & SDLA_ADDR_MASK));
779 			SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
780 			pbuf = (void *) (dev->mem_start + ((pbufi->rse_base + flp->buffer * sizeof(struct buf_entry)) & SDLA_ADDR_MASK));
781 			success = pbuf->opp_flag;
782 			if (!success)
783 				break;
784 
785 			buf_top = pbufi->buf_top;
786 			buf_base = pbufi->buf_base;
787 			dlci = pbuf->dlci;
788 			len = pbuf->length;
789 			addr = pbuf->buf_addr;
790 			break;
791 	}
792 
793 	/* common code, find the DLCI and get the SKB */
794 	if (success)
795 	{
796 		for (i=0;i<CONFIG_DLCI_MAX;i++)
797 			if (flp->dlci[i] == dlci)
798 				break;
799 
800 		if (i == CONFIG_DLCI_MAX)
801 		{
802 			printk(KERN_NOTICE "%s: Received packet from invalid DLCI %i, ignoring.", dev->name, dlci);
803 			flp->stats.rx_errors++;
804 			success = 0;
805 		}
806 	}
807 
808 	if (success)
809 	{
810 		master = flp->master[i];
811 		skb = dev_alloc_skb(len + sizeof(struct frhdr));
812 		if (skb == NULL)
813 		{
814 			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
815 			flp->stats.rx_dropped++;
816 			success = 0;
817 		}
818 		else
819 			skb_reserve(skb, sizeof(struct frhdr));
820 	}
821 
822 	/* pick up the data */
823 	switch (flp->type)
824 	{
825 		case SDLA_S502A:
826 		case SDLA_S502E:
827 			if (success)
828 				sdla_read(dev, SDLA_502_RCV_BUF + SDLA_502_DATA_OFS, skb_put(skb,len), len);
829 
830 			SDLA_WINDOW(dev, SDLA_502_RCV_BUF);
831 			cmd->opp_flag = 0;
832 			break;
833 
834 		case SDLA_S508:
835 			if (success)
836 			{
837 				/* is this buffer split off the end of the internal ring buffer */
838 				split = addr + len > buf_top + 1 ? len - (buf_top - addr + 1) : 0;
839 				len2 = len - split;
840 
841 				sdla_read(dev, addr, skb_put(skb, len2), len2);
842 				if (split)
843 					sdla_read(dev, buf_base, skb_put(skb, split), split);
844 			}
845 
846 			/* increment the buffer we're looking at */
847 			SDLA_WINDOW(dev, SDLA_508_RXBUF_INFO);
848 			flp->buffer = (flp->buffer + 1) % pbufi->rse_num;
849 			pbuf->opp_flag = 0;
850 			break;
851 	}
852 
853 	if (success)
854 	{
855 		flp->stats.rx_packets++;
856 		dlp = master->priv;
857 		(*dlp->receive)(skb, master);
858 	}
859 
860 	restore_flags(flags);
861 }
862 
sdla_isr(int irq,void * dev_id,struct pt_regs * regs)863 static void sdla_isr(int irq, void *dev_id, struct pt_regs * regs)
864 {
865 	struct net_device     *dev;
866 	struct frad_local *flp;
867 	char              byte;
868 
869 	dev = dev_id;
870 
871 	if (dev == NULL)
872 	{
873 		printk(KERN_WARNING "sdla_isr(): irq %d for unknown device.\n", irq);
874 		return;
875 	}
876 
877 	flp = dev->priv;
878 
879 	if (!flp->initialized)
880 	{
881 		printk(KERN_WARNING "%s: irq %d for uninitialized device.\n", dev->name, irq);
882 		return;
883 	}
884 
885 	byte = sdla_byte(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE);
886 	switch (byte)
887 	{
888 		case SDLA_INTR_RX:
889 			sdla_receive(dev);
890 			break;
891 
892 		/* the command will get an error return, which is processed above */
893 		case SDLA_INTR_MODEM:
894 		case SDLA_INTR_STATUS:
895 			sdla_cmd(dev, SDLA_READ_DLC_STATUS, 0, 0, NULL, 0, NULL, NULL);
896 			break;
897 
898 		case SDLA_INTR_TX:
899 		case SDLA_INTR_COMPLETE:
900 		case SDLA_INTR_TIMER:
901 			printk(KERN_WARNING "%s: invalid irq flag 0x%02X.\n", dev->name, byte);
902 			break;
903 	}
904 
905 	/* the S502E requires a manual acknowledgement of the interrupt */
906 	if (flp->type == SDLA_S502E)
907 	{
908 		flp->state &= ~SDLA_S502E_INTACK;
909 		outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
910 		flp->state |= SDLA_S502E_INTACK;
911 		outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
912 	}
913 
914 	/* this clears the byte, informing the Z80 we're done */
915 	byte = 0;
916 	sdla_write(dev, flp->type == SDLA_S508 ? SDLA_508_IRQ_INTERFACE : SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
917 }
918 
sdla_poll(unsigned long device)919 static void sdla_poll(unsigned long device)
920 {
921 	struct net_device	  *dev;
922 	struct frad_local *flp;
923 
924 	dev = (struct net_device *) device;
925 	flp = dev->priv;
926 
927 	if (sdla_byte(dev, SDLA_502_RCV_BUF))
928 		sdla_receive(dev);
929 
930 	flp->timer.expires = 1;
931 	add_timer(&flp->timer);
932 }
933 
sdla_close(struct net_device * dev)934 static int sdla_close(struct net_device *dev)
935 {
936 	struct frad_local *flp;
937 	struct intr_info  intr;
938 	int               len, i;
939 	short             dlcis[CONFIG_DLCI_MAX];
940 
941 	flp = dev->priv;
942 
943 	len = 0;
944 	for(i=0;i<CONFIG_DLCI_MAX;i++)
945 		if (flp->dlci[i])
946 			dlcis[len++] = abs(flp->dlci[i]);
947 	len *= 2;
948 
949 	if (flp->config.station == FRAD_STATION_NODE)
950 	{
951 		for(i=0;i<CONFIG_DLCI_MAX;i++)
952 			if (flp->dlci[i] > 0)
953 				sdla_cmd(dev, SDLA_DEACTIVATE_DLCI, 0, 0, dlcis, len, NULL, NULL);
954 		sdla_cmd(dev, SDLA_DELETE_DLCI, 0, 0, &flp->dlci[i], sizeof(flp->dlci[i]), NULL, NULL);
955 	}
956 
957 	memset(&intr, 0, sizeof(intr));
958 	/* let's start up the reception */
959 	switch(flp->type)
960 	{
961 		case SDLA_S502A:
962 			del_timer(&flp->timer);
963 			break;
964 
965 		case SDLA_S502E:
966 			sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
967 			flp->state &= ~SDLA_S502E_INTACK;
968 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
969 			break;
970 
971 		case SDLA_S507:
972 			break;
973 
974 		case SDLA_S508:
975 			sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
976 			flp->state &= ~SDLA_S508_INTEN;
977 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
978 			break;
979 	}
980 
981 	sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
982 
983 	netif_stop_queue(dev);
984 
985 	MOD_DEC_USE_COUNT;
986 
987 	return(0);
988 }
989 
990 struct conf_data {
991 	struct frad_conf config;
992 	short            dlci[CONFIG_DLCI_MAX];
993 };
994 
sdla_open(struct net_device * dev)995 static int sdla_open(struct net_device *dev)
996 {
997 	struct frad_local *flp;
998 	struct dlci_local *dlp;
999 	struct conf_data  data;
1000 	struct intr_info  intr;
1001 	int               len, i;
1002 	char              byte;
1003 
1004 	flp = dev->priv;
1005 
1006 	if (!flp->initialized)
1007 		return(-EPERM);
1008 
1009 	if (!flp->configured)
1010 		return(-EPERM);
1011 
1012 	/* time to send in the configuration */
1013 	len = 0;
1014 	for(i=0;i<CONFIG_DLCI_MAX;i++)
1015 		if (flp->dlci[i])
1016 			data.dlci[len++] = abs(flp->dlci[i]);
1017 	len *= 2;
1018 
1019 	memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1020 	len += sizeof(struct frad_conf);
1021 
1022 	sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1023 	sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1024 
1025 	if (flp->type == SDLA_S508)
1026 		flp->buffer = 0;
1027 
1028 	sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1029 
1030 	/* let's start up the reception */
1031 	memset(&intr, 0, sizeof(intr));
1032 	switch(flp->type)
1033 	{
1034 		case SDLA_S502A:
1035 			flp->timer.expires = 1;
1036 			add_timer(&flp->timer);
1037 			break;
1038 
1039 		case SDLA_S502E:
1040 			flp->state |= SDLA_S502E_ENABLE;
1041 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1042 			flp->state |= SDLA_S502E_INTACK;
1043 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1044 			byte = 0;
1045 			sdla_write(dev, SDLA_502_IRQ_INTERFACE, &byte, sizeof(byte));
1046 			intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1047 			sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(char) + sizeof(short), NULL, NULL);
1048 			break;
1049 
1050 		case SDLA_S507:
1051 			break;
1052 
1053 		case SDLA_S508:
1054 			flp->state |= SDLA_S508_INTEN;
1055 			outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1056 			byte = 0;
1057 			sdla_write(dev, SDLA_508_IRQ_INTERFACE, &byte, sizeof(byte));
1058 			intr.flags = SDLA_INTR_RX | SDLA_INTR_STATUS | SDLA_INTR_MODEM;
1059 			intr.irq = dev->irq;
1060 			sdla_cmd(dev, SDLA_SET_IRQ_TRIGGER, 0, 0, &intr, sizeof(struct intr_info), NULL, NULL);
1061 			break;
1062 	}
1063 
1064 	if (flp->config.station == FRAD_STATION_CPE)
1065 	{
1066 		byte = SDLA_ICS_STATUS_ENQ;
1067 		sdla_cmd(dev, SDLA_ISSUE_IN_CHANNEL_SIGNAL, 0, 0, &byte, sizeof(byte), NULL, NULL);
1068 	}
1069 	else
1070 	{
1071 		sdla_cmd(dev, SDLA_ADD_DLCI, 0, 0, data.dlci, len - sizeof(struct frad_conf), NULL, NULL);
1072 		for(i=0;i<CONFIG_DLCI_MAX;i++)
1073 			if (flp->dlci[i] > 0)
1074 				sdla_cmd(dev, SDLA_ACTIVATE_DLCI, 0, 0, &flp->dlci[i], 2*sizeof(flp->dlci[i]), NULL, NULL);
1075 	}
1076 
1077 	/* configure any specific DLCI settings */
1078 	for(i=0;i<CONFIG_DLCI_MAX;i++)
1079 		if (flp->dlci[i])
1080 		{
1081 			dlp = flp->master[i]->priv;
1082 			if (dlp->configured)
1083 				sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, abs(flp->dlci[i]), 0, &dlp->config, sizeof(struct dlci_conf), NULL, NULL);
1084 		}
1085 
1086 	netif_start_queue(dev);
1087 
1088 	MOD_INC_USE_COUNT;
1089 
1090 	return(0);
1091 }
1092 
sdla_config(struct net_device * dev,struct frad_conf * conf,int get)1093 static int sdla_config(struct net_device *dev, struct frad_conf *conf, int get)
1094 {
1095 	struct frad_local *flp;
1096 	struct conf_data  data;
1097 	int               i;
1098 	short             size;
1099 
1100 	if (dev->type == 0xFFFF)
1101 		return(-EUNATCH);
1102 
1103 	flp = dev->priv;
1104 
1105 	if (!get)
1106 	{
1107 		if (netif_running(dev))
1108 			return(-EBUSY);
1109 
1110 		if(copy_from_user(&data.config, conf, sizeof(struct frad_conf)))
1111 			return -EFAULT;
1112 
1113 		if (data.config.station & ~FRAD_STATION_NODE)
1114 			return(-EINVAL);
1115 
1116 		if (data.config.flags & ~FRAD_VALID_FLAGS)
1117 			return(-EINVAL);
1118 
1119 		if ((data.config.kbaud < 0) ||
1120 			 ((data.config.kbaud > 128) && (flp->type != SDLA_S508)))
1121 			return(-EINVAL);
1122 
1123 		if (data.config.clocking & ~(FRAD_CLOCK_INT | SDLA_S508_PORT_RS232))
1124 			return(-EINVAL);
1125 
1126 		if ((data.config.mtu < 0) || (data.config.mtu > SDLA_MAX_MTU))
1127 			return(-EINVAL);
1128 
1129 		if ((data.config.T391 < 5) || (data.config.T391 > 30))
1130 			return(-EINVAL);
1131 
1132 		if ((data.config.T392 < 5) || (data.config.T392 > 30))
1133 			return(-EINVAL);
1134 
1135 		if ((data.config.N391 < 1) || (data.config.N391 > 255))
1136 			return(-EINVAL);
1137 
1138 		if ((data.config.N392 < 1) || (data.config.N392 > 10))
1139 			return(-EINVAL);
1140 
1141 		if ((data.config.N393 < 1) || (data.config.N393 > 10))
1142 			return(-EINVAL);
1143 
1144 		memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1145 		flp->config.flags |= SDLA_DIRECT_RECV;
1146 
1147 		if (flp->type == SDLA_S508)
1148 			flp->config.flags |= SDLA_TX70_RX30;
1149 
1150 		if (dev->mtu != flp->config.mtu)
1151 		{
1152 			/* this is required to change the MTU */
1153 			dev->mtu = flp->config.mtu;
1154 			for(i=0;i<CONFIG_DLCI_MAX;i++)
1155 				if (flp->master[i])
1156 					flp->master[i]->mtu = flp->config.mtu;
1157 		}
1158 
1159 		flp->config.mtu += sizeof(struct frhdr);
1160 
1161 		/* off to the races! */
1162 		if (!flp->configured)
1163 			sdla_start(dev);
1164 
1165 		flp->configured = 1;
1166 	}
1167 	else
1168 	{
1169 		/* no sense reading if the CPU isn't started */
1170 		if (netif_running(dev))
1171 		{
1172 			size = sizeof(data);
1173 			if (sdla_cmd(dev, SDLA_READ_DLCI_CONFIGURATION, 0, 0, NULL, 0, &data, &size) != SDLA_RET_OK)
1174 				return(-EIO);
1175 		}
1176 		else
1177 			if (flp->configured)
1178 				memcpy(&data.config, &flp->config, sizeof(struct frad_conf));
1179 			else
1180 				memset(&data.config, 0, sizeof(struct frad_conf));
1181 
1182 		memcpy(&flp->config, &data.config, sizeof(struct frad_conf));
1183 		data.config.flags &= FRAD_VALID_FLAGS;
1184 		data.config.mtu -= data.config.mtu > sizeof(struct frhdr) ? sizeof(struct frhdr) : data.config.mtu;
1185 		return copy_to_user(conf, &data.config, sizeof(struct frad_conf))?-EFAULT:0;
1186 	}
1187 
1188 	return(0);
1189 }
1190 
sdla_xfer(struct net_device * dev,struct sdla_mem * info,int read)1191 static int sdla_xfer(struct net_device *dev, struct sdla_mem *info, int read)
1192 {
1193 	struct sdla_mem mem;
1194 	char	*temp;
1195 
1196 	if(copy_from_user(&mem, info, sizeof(mem)))
1197 		return -EFAULT;
1198 
1199 	if (read)
1200 	{
1201 		temp = kmalloc(mem.len, GFP_KERNEL);
1202 		if (!temp)
1203 			return(-ENOMEM);
1204 		memset(temp, 0, mem.len);
1205 		sdla_read(dev, mem.addr, temp, mem.len);
1206 		if(copy_to_user(mem.data, temp, mem.len))
1207 		{
1208 			kfree(temp);
1209 			return -EFAULT;
1210 		}
1211 		kfree(temp);
1212 	}
1213 	else
1214 	{
1215 		temp = kmalloc(mem.len, GFP_KERNEL);
1216 		if (!temp)
1217 			return(-ENOMEM);
1218 		if(copy_from_user(temp, mem.data, mem.len))
1219 		{
1220 			kfree(temp);
1221 			return -EFAULT;
1222 		}
1223 		sdla_write(dev, mem.addr, temp, mem.len);
1224 		kfree(temp);
1225 	}
1226 	return(0);
1227 }
1228 
sdla_reconfig(struct net_device * dev)1229 static int sdla_reconfig(struct net_device *dev)
1230 {
1231 	struct frad_local *flp;
1232 	struct conf_data  data;
1233 	int               i, len;
1234 
1235 	flp = dev->priv;
1236 
1237 	len = 0;
1238 	for(i=0;i<CONFIG_DLCI_MAX;i++)
1239 		if (flp->dlci[i])
1240 			data.dlci[len++] = flp->dlci[i];
1241 	len *= 2;
1242 
1243 	memcpy(&data, &flp->config, sizeof(struct frad_conf));
1244 	len += sizeof(struct frad_conf);
1245 
1246 	sdla_cmd(dev, SDLA_DISABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1247 	sdla_cmd(dev, SDLA_SET_DLCI_CONFIGURATION, 0, 0, &data, len, NULL, NULL);
1248 	sdla_cmd(dev, SDLA_ENABLE_COMMUNICATIONS, 0, 0, NULL, 0, NULL, NULL);
1249 
1250 	return(0);
1251 }
1252 
sdla_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1253 static int sdla_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1254 {
1255 	struct frad_local *flp;
1256 
1257 	if(!capable(CAP_NET_ADMIN))
1258 		return -EPERM;
1259 
1260 	flp = dev->priv;
1261 
1262 	if (!flp->initialized)
1263 		return(-EINVAL);
1264 
1265 	switch (cmd)
1266 	{
1267 		case FRAD_GET_CONF:
1268 		case FRAD_SET_CONF:
1269 			return(sdla_config(dev, (struct frad_conf *)ifr->ifr_data, cmd == FRAD_GET_CONF));
1270 
1271 		case SDLA_IDENTIFY:
1272 			ifr->ifr_flags = flp->type;
1273 			break;
1274 
1275 		case SDLA_CPUSPEED:
1276 			return(sdla_cpuspeed(dev, ifr));
1277 
1278 /* ==========================================================
1279 NOTE:  This is rather a useless action right now, as the
1280        current driver does not support protocols other than
1281        FR.  However, Sangoma has modules for a number of
1282        other protocols in the works.
1283 ============================================================*/
1284 		case SDLA_PROTOCOL:
1285 			if (flp->configured)
1286 				return(-EALREADY);
1287 
1288 			switch (ifr->ifr_flags)
1289 			{
1290 				case ARPHRD_FRAD:
1291 					dev->type = ifr->ifr_flags;
1292 					break;
1293 				default:
1294 					return(-ENOPROTOOPT);
1295 			}
1296 			break;
1297 
1298 		case SDLA_CLEARMEM:
1299 			sdla_clear(dev);
1300 			break;
1301 
1302 		case SDLA_WRITEMEM:
1303 		case SDLA_READMEM:
1304 			if(!capable(CAP_SYS_RAWIO))
1305 				return -EPERM;
1306 			return(sdla_xfer(dev, (struct sdla_mem *)ifr->ifr_data, cmd == SDLA_READMEM));
1307 
1308 		case SDLA_START:
1309 			sdla_start(dev);
1310 			break;
1311 
1312 		case SDLA_STOP:
1313 			sdla_stop(dev);
1314 			break;
1315 
1316 		default:
1317 			return(-EOPNOTSUPP);
1318 	}
1319 	return(0);
1320 }
1321 
sdla_change_mtu(struct net_device * dev,int new_mtu)1322 int sdla_change_mtu(struct net_device *dev, int new_mtu)
1323 {
1324 	struct frad_local *flp;
1325 
1326 	flp = dev->priv;
1327 
1328 	if (netif_running(dev))
1329 		return(-EBUSY);
1330 
1331 	/* for now, you can't change the MTU! */
1332 	return(-EOPNOTSUPP);
1333 }
1334 
sdla_set_config(struct net_device * dev,struct ifmap * map)1335 int sdla_set_config(struct net_device *dev, struct ifmap *map)
1336 {
1337 	struct frad_local *flp;
1338 	int               i;
1339 	char              byte;
1340 
1341 	flp = dev->priv;
1342 
1343 	if (flp->initialized)
1344 		return(-EINVAL);
1345 
1346 	for(i=0;i < sizeof(valid_port) / sizeof (int) ; i++)
1347 		if (valid_port[i] == map->base_addr)
1348 			break;
1349 
1350 	if (i == sizeof(valid_port) / sizeof(int))
1351 		return(-EINVAL);
1352 
1353 	dev->base_addr = map->base_addr;
1354 	if (!request_region(dev->base_addr, SDLA_IO_EXTENTS, dev->name)){
1355 		printk(KERN_WARNING "SDLA: io-port 0x%04lx in use \n", dev->base_addr);
1356 		return(-EINVAL);
1357 	}
1358 	/* test for card types, S502A, S502E, S507, S508                 */
1359 	/* these tests shut down the card completely, so clear the state */
1360 	flp->type = SDLA_UNKNOWN;
1361 	flp->state = 0;
1362 
1363 	for(i=1;i<SDLA_IO_EXTENTS;i++)
1364 		if (inb(dev->base_addr + i) != 0xFF)
1365 			break;
1366 
1367 	if (i == SDLA_IO_EXTENTS)
1368 	{
1369 		outb(SDLA_HALT, dev->base_addr + SDLA_REG_Z80_CONTROL);
1370 		if ((inb(dev->base_addr + SDLA_S502_STS) & 0x0F) == 0x08)
1371 		{
1372 			outb(SDLA_S502E_INTACK, dev->base_addr + SDLA_REG_CONTROL);
1373 			if ((inb(dev->base_addr + SDLA_S502_STS) & 0x0F) == 0x0C)
1374 			{
1375 				outb(SDLA_HALT, dev->base_addr + SDLA_REG_CONTROL);
1376 				flp->type = SDLA_S502E;
1377 			}
1378 		}
1379 	}
1380 
1381 	if (flp->type == SDLA_UNKNOWN)
1382 	{
1383 		for(byte=inb(dev->base_addr),i=0;i<SDLA_IO_EXTENTS;i++)
1384 			if (inb(dev->base_addr + i) != byte)
1385 				break;
1386 
1387 		if (i == SDLA_IO_EXTENTS)
1388 		{
1389 			outb(SDLA_HALT, dev->base_addr + SDLA_REG_CONTROL);
1390 			if ((inb(dev->base_addr + SDLA_S502_STS) & 0x7E) == 0x30)
1391 			{
1392 				outb(SDLA_S507_ENABLE, dev->base_addr + SDLA_REG_CONTROL);
1393 				if ((inb(dev->base_addr + SDLA_S502_STS) & 0x7E) == 0x32)
1394 				{
1395 					outb(SDLA_HALT, dev->base_addr + SDLA_REG_CONTROL);
1396 					flp->type = SDLA_S507;
1397 				}
1398 			}
1399 		}
1400 	}
1401 
1402 	if (flp->type == SDLA_UNKNOWN)
1403 	{
1404 		outb(SDLA_HALT, dev->base_addr + SDLA_REG_CONTROL);
1405 		if ((inb(dev->base_addr + SDLA_S508_STS) & 0x3F) == 0x00)
1406 		{
1407 			outb(SDLA_S508_INTEN, dev->base_addr + SDLA_REG_CONTROL);
1408 			if ((inb(dev->base_addr + SDLA_S508_STS) & 0x3F) == 0x10)
1409 			{
1410 				outb(SDLA_HALT, dev->base_addr + SDLA_REG_CONTROL);
1411 				flp->type = SDLA_S508;
1412 			}
1413 		}
1414 	}
1415 
1416 	if (flp->type == SDLA_UNKNOWN)
1417 	{
1418 		outb(SDLA_S502A_HALT, dev->base_addr + SDLA_REG_CONTROL);
1419 		if (inb(dev->base_addr + SDLA_S502_STS) == 0x40)
1420 		{
1421 			outb(SDLA_S502A_START, dev->base_addr + SDLA_REG_CONTROL);
1422 			if (inb(dev->base_addr + SDLA_S502_STS) == 0x40)
1423 			{
1424 				outb(SDLA_S502A_INTEN, dev->base_addr + SDLA_REG_CONTROL);
1425 				if (inb(dev->base_addr + SDLA_S502_STS) == 0x44)
1426 				{
1427 					outb(SDLA_S502A_START, dev->base_addr + SDLA_REG_CONTROL);
1428 					flp->type = SDLA_S502A;
1429 				}
1430 			}
1431 		}
1432 	}
1433 
1434 	if (flp->type == SDLA_UNKNOWN)
1435 	{
1436 		printk(KERN_NOTICE "%s: Unknown card type\n", dev->name);
1437 		return(-ENODEV);
1438 	}
1439 
1440 	switch(dev->base_addr)
1441 	{
1442 		case 0x270:
1443 		case 0x280:
1444 		case 0x380:
1445 		case 0x390:
1446 			if ((flp->type != SDLA_S508) && (flp->type != SDLA_S507))
1447 				return(-EINVAL);
1448 	}
1449 
1450 	switch (map->irq)
1451 	{
1452 		case 2:
1453 			if (flp->type != SDLA_S502E)
1454 				return(-EINVAL);
1455 			break;
1456 
1457 		case 10:
1458 		case 11:
1459 		case 12:
1460 		case 15:
1461 		case 4:
1462 			if ((flp->type != SDLA_S508) && (flp->type != SDLA_S507))
1463 				return(-EINVAL);
1464 
1465 		case 3:
1466 		case 5:
1467 		case 7:
1468 			if (flp->type == SDLA_S502A)
1469 				return(-EINVAL);
1470 			break;
1471 
1472 		default:
1473 			return(-EINVAL);
1474 	}
1475 	dev->irq = map->irq;
1476 
1477 	if (request_irq(dev->irq, &sdla_isr, 0, dev->name, dev))
1478 		return(-EAGAIN);
1479 
1480 	if (flp->type == SDLA_S507)
1481 	{
1482 		switch(dev->irq)
1483 		{
1484 			case 3:
1485 				flp->state = SDLA_S507_IRQ3;
1486 				break;
1487 			case 4:
1488 				flp->state = SDLA_S507_IRQ4;
1489 				break;
1490 			case 5:
1491 				flp->state = SDLA_S507_IRQ5;
1492 				break;
1493 			case 7:
1494 				flp->state = SDLA_S507_IRQ7;
1495 				break;
1496 			case 10:
1497 				flp->state = SDLA_S507_IRQ10;
1498 				break;
1499 			case 11:
1500 				flp->state = SDLA_S507_IRQ11;
1501 				break;
1502 			case 12:
1503 				flp->state = SDLA_S507_IRQ12;
1504 				break;
1505 			case 15:
1506 				flp->state = SDLA_S507_IRQ15;
1507 				break;
1508 		}
1509 	}
1510 
1511 	for(i=0;i < sizeof(valid_mem) / sizeof (int) ; i++)
1512 		if (valid_mem[i] == map->mem_start)
1513 			break;
1514 
1515 	if (i == sizeof(valid_mem) / sizeof(int))
1516 	/*
1517 	 *	FIXME:
1518 	 *	BUG BUG BUG: MUST RELEASE THE IRQ WE ALLOCATED IN
1519 	 *	ALL THESE CASES
1520 	 *
1521 	 */
1522 		return(-EINVAL);
1523 
1524 	if ((flp->type == SDLA_S502A) && (((map->mem_start & 0xF000) >> 12) == 0x0E))
1525 		return(-EINVAL);
1526 
1527 	if ((flp->type != SDLA_S507) && ((map->mem_start >> 16) == 0x0B))
1528 		return(-EINVAL);
1529 
1530 	if ((flp->type == SDLA_S507) && ((map->mem_start >> 16) == 0x0D))
1531 		return(-EINVAL);
1532 
1533 	dev->mem_start = map->mem_start;
1534 	dev->mem_end = dev->mem_start + 0x2000;
1535 
1536 	byte = flp->type != SDLA_S508 ? SDLA_8K_WINDOW : 0;
1537 	byte |= (map->mem_start & 0xF000) >> (12 + (flp->type == SDLA_S508 ? 1 : 0));
1538 	switch(flp->type)
1539 	{
1540 		case SDLA_S502A:
1541 		case SDLA_S502E:
1542 			switch (map->mem_start >> 16)
1543 			{
1544 				case 0x0A:
1545 					byte |= SDLA_S502_SEG_A;
1546 					break;
1547 				case 0x0C:
1548 					byte |= SDLA_S502_SEG_C;
1549 					break;
1550 				case 0x0D:
1551 					byte |= SDLA_S502_SEG_D;
1552 					break;
1553 				case 0x0E:
1554 					byte |= SDLA_S502_SEG_E;
1555 					break;
1556 			}
1557 			break;
1558 		case SDLA_S507:
1559 			switch (map->mem_start >> 16)
1560 			{
1561 				case 0x0A:
1562 					byte |= SDLA_S507_SEG_A;
1563 					break;
1564 				case 0x0B:
1565 					byte |= SDLA_S507_SEG_B;
1566 					break;
1567 				case 0x0C:
1568 					byte |= SDLA_S507_SEG_C;
1569 					break;
1570 				case 0x0E:
1571 					byte |= SDLA_S507_SEG_E;
1572 					break;
1573 			}
1574 			break;
1575 		case SDLA_S508:
1576 			switch (map->mem_start >> 16)
1577 			{
1578 				case 0x0A:
1579 					byte |= SDLA_S508_SEG_A;
1580 					break;
1581 				case 0x0C:
1582 					byte |= SDLA_S508_SEG_C;
1583 					break;
1584 				case 0x0D:
1585 					byte |= SDLA_S508_SEG_D;
1586 					break;
1587 				case 0x0E:
1588 					byte |= SDLA_S508_SEG_E;
1589 					break;
1590 			}
1591 			break;
1592 	}
1593 
1594 	/* set the memory bits, and enable access */
1595 	outb(byte, dev->base_addr + SDLA_REG_PC_WINDOW);
1596 
1597 	switch(flp->type)
1598 	{
1599 		case SDLA_S502E:
1600 			flp->state = SDLA_S502E_ENABLE;
1601 			break;
1602 		case SDLA_S507:
1603 			flp->state |= SDLA_MEMEN;
1604 			break;
1605 		case SDLA_S508:
1606 			flp->state = SDLA_MEMEN;
1607 			break;
1608 	}
1609 	outb(flp->state, dev->base_addr + SDLA_REG_CONTROL);
1610 
1611 	flp->initialized = 1;
1612 	return(0);
1613 }
1614 
sdla_stats(struct net_device * dev)1615 static struct net_device_stats *sdla_stats(struct net_device *dev)
1616 {
1617 	struct frad_local *flp;
1618 	flp = dev->priv;
1619 
1620 	return(&flp->stats);
1621 }
1622 
sdla_init(struct net_device * dev)1623 int __init sdla_init(struct net_device *dev)
1624 {
1625 	struct frad_local *flp;
1626 
1627 	/* allocate the private data structure */
1628 	flp = kmalloc(sizeof(struct frad_local), GFP_KERNEL);
1629 	if (!flp)
1630 		return(-ENOMEM);
1631 
1632 	memset(flp, 0, sizeof(struct frad_local));
1633 	dev->priv = flp;
1634 
1635 	dev->flags		= 0;
1636 	dev->open		= sdla_open;
1637 	dev->stop		= sdla_close;
1638 	dev->do_ioctl		= sdla_ioctl;
1639 	dev->set_config		= sdla_set_config;
1640 	dev->get_stats		= sdla_stats;
1641 	dev->hard_start_xmit	= sdla_transmit;
1642 	dev->change_mtu		= sdla_change_mtu;
1643 
1644 	dev->type		= 0xFFFF;
1645 	dev->hard_header_len = 0;
1646 	dev->addr_len		= 0;
1647 	dev->mtu		= SDLA_MAX_MTU;
1648 
1649 	flp->activate		= sdla_activate;
1650 	flp->deactivate		= sdla_deactivate;
1651 	flp->assoc		= sdla_assoc;
1652 	flp->deassoc		= sdla_deassoc;
1653 	flp->dlci_conf		= sdla_dlci_conf;
1654 
1655 	init_timer(&flp->timer);
1656 	flp->timer.expires	= 1;
1657 	flp->timer.data		= (unsigned long) dev;
1658 	flp->timer.function	= sdla_poll;
1659 
1660 	return(0);
1661 }
1662 
sdla_c_setup(void)1663 int __init sdla_c_setup(void)
1664 {
1665 	printk("%s.\n", version);
1666 	register_frad(devname);
1667 	return 0;
1668 }
1669 
1670 #ifdef MODULE
1671 static struct net_device sdla0 = {"sdla0", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, sdla_init};
1672 
1673 MODULE_LICENSE("GPL");
1674 
init_module(void)1675 int init_module(void)
1676 {
1677 	int result;
1678 
1679 	sdla_c_setup();
1680 	if ((result = register_netdev(&sdla0)) != 0)
1681 		return result;
1682 	return 0;
1683 }
1684 
cleanup_module(void)1685 void cleanup_module(void)
1686 {
1687 	unregister_netdev(&sdla0);
1688 	if (sdla0.priv)
1689 		kfree(sdla0.priv);
1690 	if (sdla0.irq)
1691 		free_irq(sdla0.irq, &sdla0);
1692 }
1693 #endif /* MODULE */
1694