* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / wan / sdla_x25.c
blob270c5a59ff19af3cba6f2c89bfd4bd6e1f7d2f46
1 /*****************************************************************************
2 * sdla_x25.c WANPIPE(tm) Multiprotocol WAN Link Driver. X.25 module.
4 * Author: Gene Kozin <genek@compuserve.com>
6 * Copyright: (c) 1995-1997 Sangoma Technologies Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 * ============================================================================
13 * Mar 15, 1998 Alan Cox o 2.1.x porting
14 * Nov 27, 1997 Jaspreet Singh o Added protection against enabling of irqs
15 * when they are disabled.
16 * Nov 17, 1997 Farhan Thawar o Added IPX support
17 * o Changed if_send() to now buffer packets when
18 * the board is busy
19 * o Removed queueing of packets via the polling
20 * routing
21 * o Changed if_send() critical flags to properly
22 * handle race conditions
23 * Nov 06, 1997 Farhan Thawar o Added support for SVC timeouts
24 * o Changed PVC encapsulation to ETH_P_IP
25 * Jul 21, 1997 Jaspreet Singh o Fixed freeing up of buffers using kfree()
26 * when packets are received.
27 * Mar 11, 1997 Farhan Thawar Version 3.1.1
28 * o added support for V35
29 * o changed if_send() to return 0 if
30 * wandev.critical() is true
31 * o free socket buffer in if_send() if
32 * returning 0
33 * o added support for single '@' address to
34 * accept all incoming calls
35 * o fixed bug in set_chan_state() to disconnect
36 * Jan 15, 1997 Gene Kozin Version 3.1.0
37 * o implemented exec() entry point
38 * Jan 07, 1997 Gene Kozin Initial version.
39 *****************************************************************************/
42 #include <linux/kernel.h> /* printk(), and other useful stuff */
43 #include <linux/stddef.h> /* offsetof(), etc. */
44 #include <linux/errno.h> /* return codes */
45 #include <linux/string.h> /* inline memset(), etc. */
46 #include <linux/malloc.h> /* kmalloc(), kfree() */
47 #include <linux/wanrouter.h> /* WAN router definitions */
48 #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
49 #include <asm/byteorder.h> /* htons(), etc. */
50 #include <asm/uaccess.h>
52 #define _GNUC_
53 #include <linux/sdla_x25.h> /* X.25 firmware API definitions */
55 /****** Defines & Macros ****************************************************/
57 #define CMD_OK 0 /* normal firmware return code */
58 #define CMD_TIMEOUT 0xFF /* firmware command timed out */
59 #define MAX_CMD_RETRY 10 /* max number of firmware retries */
61 #define X25_CHAN_MTU 4096 /* unfragmented logical channel MTU */
62 #define X25_HRDHDR_SZ 7 /* max encapsulation header size */
63 #define X25_CONCT_TMOUT (90*HZ) /* link connection timeout */
64 #define X25_RECON_TMOUT (10*HZ) /* link connection timeout */
65 #define CONNECT_TIMEOUT (90*HZ) /* link connection timeout */
66 #define HOLD_DOWN_TIME (30*HZ) /* link hold down time */
68 /* For IPXWAN */
69 #define CVHexToAscii(b) (((unsigned char)(b) > (unsigned char)9) ? ((unsigned char)'A' + ((unsigned char)(b) - (unsigned char)10)) : ((unsigned char)'0' + (unsigned char)(b)))
71 /****** Data Structures *****************************************************/
73 /* This is an extention of the 'struct net_device' we create for each network
74 * interface to keep the rest of X.25 channel-specific data.
76 typedef struct x25_channel
78 char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */
79 char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */
80 unsigned lcn; /* logical channel number */
81 unsigned tx_pkt_size;
82 unsigned short protocol; /* ethertype, 0 - multiplexed */
83 char svc; /* 0 - permanent, 1 - switched */
84 char state; /* channel state */
85 char drop_sequence; /* mark sequence for dropping */
86 unsigned long state_tick; /* time of the last state change */
87 unsigned idle_timeout; /* sec, before disconnecting */
88 unsigned long i_timeout_sofar; /* # of sec's we've been idle */
89 unsigned hold_timeout; /* sec, before re-connecting */
90 unsigned long tick_counter; /* counter for transmit time out */
91 char devtint; /* Weather we should dev_tint() */
92 struct sk_buff* rx_skb; /* receive socket buffer */
93 struct sk_buff* tx_skb; /* transmit socket buffer */
94 sdla_t* card; /* -> owner */
95 int ch_idx;
96 struct net_device_stats ifstats; /* interface statistics */
97 } x25_channel_t;
99 typedef struct x25_call_info
101 char dest[17]; /* ASCIIZ destination address */
102 char src[17]; /* ASCIIZ source address */
103 char nuser; /* number of user data bytes */
104 unsigned char user[127]; /* user data */
105 char nfacil; /* number of facilities */
106 struct
108 unsigned char code;
109 unsigned char parm;
110 } facil[64]; /* facilities */
111 } x25_call_info_t;
113 /****** Function Prototypes *************************************************/
115 /* WAN link driver entry points. These are called by the WAN router module. */
116 static int update (wan_device_t* wandev);
117 static int new_if (wan_device_t* wandev, struct net_device* dev,
118 wanif_conf_t* conf);
119 static int del_if (wan_device_t* wandev, struct net_device* dev);
121 /* WANPIPE-specific entry points */
122 static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data);
124 /* Network device interface */
125 static int if_init (struct net_device* dev);
126 static int if_open (struct net_device* dev);
127 static int if_close (struct net_device* dev);
128 static int if_header (struct sk_buff* skb, struct net_device* dev,
129 unsigned short type, void* daddr, void* saddr, unsigned len);
130 static int if_rebuild_hdr (struct sk_buff* skb);
131 static int if_send (struct sk_buff* skb, struct net_device* dev);
132 static struct net_device_stats * if_stats (struct net_device* dev);
134 /* Interrupt handlers */
135 static void wpx_isr (sdla_t* card);
136 static void rx_intr (sdla_t* card);
137 static void tx_intr (sdla_t* card);
138 static void status_intr (sdla_t* card);
139 static void event_intr (sdla_t* card);
140 static void spur_intr (sdla_t* card);
142 /* Background polling routines */
143 static void wpx_poll (sdla_t* card);
144 static void poll_disconnected (sdla_t* card);
145 static void poll_connecting (sdla_t* card);
146 static void poll_active (sdla_t* card);
148 /* X.25 firmware interface functions */
149 static int x25_get_version (sdla_t* card, char* str);
150 static int x25_configure (sdla_t* card, TX25Config* conf);
151 static int x25_get_err_stats (sdla_t* card);
152 static int x25_get_stats (sdla_t* card);
153 static int x25_set_intr_mode (sdla_t* card, int mode);
154 static int x25_close_hdlc (sdla_t* card);
155 static int x25_open_hdlc (sdla_t* card);
156 static int x25_setup_hdlc (sdla_t* card);
157 static int x25_set_dtr (sdla_t* card, int dtr);
158 static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan);
159 static int x25_place_call (sdla_t* card, x25_channel_t* chan);
160 static int x25_accept_call (sdla_t* card, int lcn, int qdm);
161 static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn);
162 static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf);
163 static int x25_fetch_events (sdla_t* card);
164 static int x25_error (sdla_t* card, int err, int cmd, int lcn);
166 /* X.25 asynchronous event handlers */
167 static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
168 static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
169 static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
170 static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
171 static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb);
173 /* Miscellaneous functions */
174 static int connect (sdla_t* card);
175 static int disconnect (sdla_t* card);
176 static struct net_device* get_dev_by_lcn(wan_device_t* wandev, unsigned lcn);
177 static int chan_connect (struct net_device* dev);
178 static int chan_disc (struct net_device* dev);
179 static void set_chan_state (struct net_device* dev, int state);
180 static int chan_send (struct net_device* dev, struct sk_buff* skb);
181 static unsigned char bps_to_speed_code (unsigned long bps);
182 static unsigned int dec_to_uint (unsigned char* str, int len);
183 static unsigned int hex_to_uint (unsigned char* str, int len);
184 static void parse_call_info (unsigned char* str, x25_call_info_t* info);
186 /* IPX functions */
187 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming);
188 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto);
190 extern void disable_irq(unsigned int);
191 extern void enable_irq(unsigned int);
193 /****** Global Data **********************************************************
194 * Note: All data must be explicitly initialized!!!
197 /****** Public Functions ****************************************************/
199 /*============================================================================
200 * X.25 Protocol Initialization routine.
202 * This routine is called by the main WANPIPE module during setup. At this
203 * point adapter is completely initialized and X.25 firmware is running.
204 * o read firmware version (to make sure it's alive)
205 * o configure adapter
206 * o initialize protocol-specific fields of the adapter data space.
208 * Return: 0 o.k.
209 * < 0 failure.
211 int wpx_init (sdla_t* card, wandev_conf_t* conf)
213 union
215 char str[80];
216 TX25Config cfg;
217 } u;
219 /* Verify configuration ID */
220 if (conf->config_id != WANCONFIG_X25)
222 printk(KERN_INFO "%s: invalid configuration ID %u!\n",
223 card->devname, conf->config_id)
225 return -EINVAL;
228 /* Initialize protocol-specific fields */
229 card->mbox = (void*)(card->hw.dpmbase + X25_MBOX_OFFS);
230 card->rxmb = (void*)(card->hw.dpmbase + X25_RXMBOX_OFFS);
231 card->flags = (void*)(card->hw.dpmbase + X25_STATUS_OFFS);
233 /* Read firmware version. Note that when adapter initializes, it
234 * clears the mailbox, so it may appear that the first command was
235 * executed successfully when in fact it was merely erased. To work
236 * around this, we execute the first command twice.
238 if (x25_get_version(card, NULL) || x25_get_version(card, u.str))
239 return -EIO
241 printk(KERN_INFO "%s: running X.25 firmware v%s\n",
242 card->devname, u.str)
245 /* Configure adapter. Here we set resonable defaults, then parse
246 * device configuration structure and set configuration options.
247 * Most configuration options are verified and corrected (if
248 * necessary) since we can't rely on the adapter to do so and don't
249 * want it to fail either.
251 memset(&u.cfg, 0, sizeof(u.cfg));
252 u.cfg.t1 = 3;
253 u.cfg.n2 = 10;
254 u.cfg.autoHdlc = 1; /* automatic HDLC connection */
255 u.cfg.hdlcWindow = 7;
256 u.cfg.pktWindow = 2;
257 u.cfg.station = 1; /* DTE */
258 u.cfg.options = 0x00B0; /* disable D-bit pragmatics */
259 u.cfg.ccittCompat = 1988;
260 u.cfg.t10t20 = 30;
261 u.cfg.t11t21 = 30;
262 u.cfg.t12t22 = 30;
263 u.cfg.t13t23 = 30;
264 u.cfg.t16t26 = 30;
265 u.cfg.t28 = 30;
266 u.cfg.r10r20 = 5;
267 u.cfg.r12r22 = 5;
268 u.cfg.r13r23 = 5;
269 u.cfg.responseOpt = 1; /* RR's after every packet */
271 if (conf->clocking != WANOPT_EXTERNAL)
272 u.cfg.baudRate = bps_to_speed_code(conf->bps)
274 if (conf->station != WANOPT_DTE)
276 u.cfg.station = 0; /* DCE mode */
278 if (conf->interface != WANOPT_RS232 ) {
279 u.cfg.hdlcOptions |= 0x80; /* V35 mode */
281 /* adjust MTU */
282 if (!conf->mtu || (conf->mtu >= 1024))
283 card->wandev.mtu = 1024
285 else if (conf->mtu >= 512)
286 card->wandev.mtu = 512
288 else if (conf->mtu >= 256)
289 card->wandev.mtu = 256
291 else if (conf->mtu >= 128)
292 card->wandev.mtu = 128
294 else card->wandev.mtu = 64;
295 u.cfg.defPktSize = u.cfg.pktMTU = card->wandev.mtu;
297 if (conf->u.x25.hi_pvc)
299 card->u.x.hi_pvc = min(conf->u.x25.hi_pvc, 4095);
300 card->u.x.lo_pvc = min(conf->u.x25.lo_pvc, card->u.x.hi_pvc);
302 if (conf->u.x25.hi_svc)
304 card->u.x.hi_svc = min(conf->u.x25.hi_svc, 4095);
305 card->u.x.lo_svc = min(conf->u.x25.lo_svc, card->u.x.hi_svc);
307 u.cfg.loPVC = card->u.x.lo_pvc;
308 u.cfg.hiPVC = card->u.x.hi_pvc;
309 u.cfg.loTwoWaySVC = card->u.x.lo_svc;
310 u.cfg.hiTwoWaySVC = card->u.x.hi_svc;
312 if (conf->u.x25.hdlc_window)
313 u.cfg.hdlcWindow = min(conf->u.x25.hdlc_window, 7)
315 if (conf->u.x25.pkt_window)
316 u.cfg.pktWindow = min(conf->u.x25.pkt_window, 7)
318 if (conf->u.x25.t1)
319 u.cfg.t1 = min(conf->u.x25.t1, 30)
321 u.cfg.t2 = min(conf->u.x25.t2, 29);
322 u.cfg.t4 = min(conf->u.x25.t4, 240);
323 if (conf->u.x25.n2)
324 u.cfg.n2 = min(conf->u.x25.n2, 30)
326 if (conf->u.x25.ccitt_compat)
327 u.cfg.ccittCompat = conf->u.x25.ccitt_compat
330 /* initialize adapter */
331 if ((x25_configure(card, &u.cfg) != CMD_OK) ||
332 (x25_close_hdlc(card) != CMD_OK) || /* close HDLC link */
333 (x25_set_dtr(card, 0) != CMD_OK)) /* drop DTR */
334 return -EIO
337 /* Initialize protocol-specific fields of adapter data space */
338 card->wandev.bps = conf->bps;
339 card->wandev.interface = conf->interface;
340 card->wandev.clocking = conf->clocking;
341 card->wandev.station = conf->station;
342 card->isr = &wpx_isr;
343 card->poll = &wpx_poll;
344 card->exec = &wpx_exec;
345 card->wandev.update = &update;
346 card->wandev.new_if = &new_if;
347 card->wandev.del_if = &del_if;
348 card->wandev.state = WAN_DISCONNECTED;
349 card->wandev.enable_tx_int = 0;
350 card->irq_dis_if_send_count = 0;
351 card->irq_dis_poll_count = 0;
352 card->wandev.enable_IPX = conf->enable_IPX;
354 if (conf->network_number)
355 card->wandev.network_number = conf->network_number;
356 else
357 card->wandev.network_number = 0xDEADBEEF;
358 return 0;
361 /******* WAN Device Driver Entry Points *************************************/
363 /*============================================================================
364 * Update device status & statistics.
366 static int update (wan_device_t* wandev)
368 sdla_t* card;
370 /* sanity checks */
371 if ((wandev == NULL) || (wandev->private == NULL))
372 return -EFAULT;
373 if (wandev->state == WAN_UNCONFIGURED)
374 return -ENODEV;
375 if (test_and_set_bit(0, (void*)&wandev->critical))
376 return -EAGAIN;
377 card = wandev->private;
379 x25_get_err_stats(card);
380 x25_get_stats(card);
381 wandev->critical = 0;
382 return 0;
385 /*============================================================================
386 * Create new logical channel.
387 * This routine is called by the router when ROUTER_IFNEW IOCTL is being
388 * handled.
389 * o parse media- and hardware-specific configuration
390 * o make sure that a new channel can be created
391 * o allocate resources, if necessary
392 * o prepare network device structure for registaration.
394 * Return: 0 o.k.
395 * < 0 failure (channel will not be created)
397 static int new_if (wan_device_t* wandev, struct net_device* dev, wanif_conf_t* conf)
399 sdla_t* card = wandev->private;
400 x25_channel_t* chan;
401 int err = 0;
403 if ((conf->name[0] == '\0') || (strlen(conf->name) > WAN_IFNAME_SZ))
405 printk(KERN_INFO "%s: invalid interface name!\n",
406 card->devname)
408 return -EINVAL;
411 /* allocate and initialize private data */
412 chan = kmalloc(sizeof(x25_channel_t), GFP_KERNEL);
413 if (chan == NULL)
414 return -ENOMEM
416 memset(chan, 0, sizeof(x25_channel_t));
417 strcpy(chan->name, conf->name);
418 chan->card = card;
419 chan->protocol = ETH_P_IP;
420 chan->tx_skb = chan->rx_skb = NULL;
422 /* verify media address */
423 if (conf->addr[0] == '@') /* SVC */
425 chan->svc = 1;
426 strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ);
428 /* Set channel timeouts (default if not specified) */
429 chan->idle_timeout = (conf->idle_timeout) ? conf->idle_timeout : 90;
430 chan->hold_timeout = (conf->hold_timeout) ? conf->hold_timeout : 10;
432 else if (is_digit(conf->addr[0])) /* PVC */
434 int lcn = dec_to_uint(conf->addr, 0);
436 if ((lcn >= card->u.x.lo_pvc) && (lcn <= card->u.x.hi_pvc))
438 chan->lcn = lcn;
440 else
442 printk(KERN_ERR
443 "%s: PVC %u is out of range on interface %s!\n",
444 wandev->name, lcn, chan->name)
446 err = -EINVAL;
449 else
451 printk(KERN_ERR
452 "%s: invalid media address on interface %s!\n",
453 wandev->name, chan->name)
455 err = -EINVAL;
457 if (err)
459 kfree(chan);
460 return err;
463 /* prepare network device data space for registration */
464 dev->name = chan->name;
465 dev->init = &if_init;
466 dev->priv = chan;
467 return 0;
470 /*============================================================================
471 * Delete logical channel.
473 static int del_if (wan_device_t* wandev, struct net_device* dev)
475 if (dev->priv)
477 kfree(dev->priv);
478 dev->priv = NULL;
480 return 0;
483 /****** WANPIPE-specific entry points ***************************************/
485 /*============================================================================
486 * Execute adapter interface command.
489 static int wpx_exec (struct sdla* card, void* u_cmd, void* u_data)
491 TX25Mbox* mbox = card->mbox;
492 int retry = MAX_CMD_RETRY;
493 int err, len;
494 TX25Cmd cmd;
496 if(copy_from_user((void*)&cmd, u_cmd, sizeof(cmd)))
497 return -EFAULT;
499 /* execute command */
503 memcpy(&mbox->cmd, &cmd, sizeof(cmd));
504 if (cmd.length)
506 if(copy_from_user((void*)&mbox->data, u_data, cmd.length))
507 return-EFAULT;
509 if (sdla_exec(mbox))
510 err = mbox->cmd.result
512 else return -EIO;
514 while (err && retry-- && x25_error(card, err, cmd.command, cmd.lcn));
516 /* return result */
517 if(copy_to_user(u_cmd, (void*)&mbox->cmd, sizeof(TX25Cmd)))
518 return -EFAULT;
519 len = mbox->cmd.length;
520 if (len && u_data && copy_to_user(u_data, (void*)&mbox->data, len))
521 return -EFAULT;
522 return 0;
525 /****** Network Device Interface ********************************************/
527 /*============================================================================
528 * Initialize Linux network interface.
530 * This routine is called only once for each interface, during Linux network
531 * interface registration. Returning anything but zero will fail interface
532 * registration.
534 static int if_init (struct net_device* dev)
536 x25_channel_t* chan = dev->priv;
537 sdla_t* card = chan->card;
538 wan_device_t* wandev = &card->wandev;
540 /* Initialize device driver entry points */
541 dev->open = &if_open;
542 dev->stop = &if_close;
543 dev->hard_header = &if_header;
544 dev->rebuild_header = &if_rebuild_hdr;
545 dev->hard_start_xmit = &if_send;
546 dev->get_stats = &if_stats;
548 /* Initialize media-specific parameters */
549 dev->type = 30; /* ARP h/w type */
550 dev->mtu = X25_CHAN_MTU;
551 dev->hard_header_len = X25_HRDHDR_SZ; /* media header length */
552 dev->addr_len = 2; /* hardware address length */
553 if (!chan->svc)
554 *(unsigned short*)dev->dev_addr = htons(chan->lcn);
556 /* Initialize hardware parameters (just for reference) */
557 dev->irq = wandev->irq;
558 dev->dma = wandev->dma;
559 dev->base_addr = wandev->ioport;
560 dev->mem_start = (unsigned long)wandev->maddr;
561 dev->mem_end = dev->mem_end + wandev->msize - 1;
563 /* Set transmit buffer queue length */
564 dev->tx_queue_len = 10;
566 /* Initialize socket buffers */
568 dev_init_buffers(dev);
569 set_chan_state(dev, WAN_DISCONNECTED);
570 return 0;
573 /*============================================================================
574 * Open network interface.
575 * o prevent module from unloading by incrementing use count
576 * o if link is disconnected then initiate connection
578 * Return 0 if O.k. or errno.
580 static int if_open (struct net_device* dev)
582 x25_channel_t* chan = dev->priv;
583 sdla_t* card = chan->card;
585 if (dev->start)
586 return -EBUSY; /* only one open is allowed */
588 if (test_and_set_bit(0, (void*)&card->wandev.critical))
589 return -EAGAIN;
591 dev->interrupt = 0;
592 dev->tbusy = 0;
593 dev->start = 1;
594 wanpipe_open(card);
596 /* If this is the first open, initiate physical connection */
597 if (card->open_cnt == 1)
598 connect(card);
599 card->wandev.critical = 0;
600 return 0;
603 /*============================================================================
604 * Close network interface.
605 * o reset flags.
606 * o if there's no more open channels then disconnect physical link.
608 static int if_close (struct net_device* dev)
610 x25_channel_t* chan = dev->priv;
611 sdla_t* card = chan->card;
613 if (test_and_set_bit(0, (void*)&card->wandev.critical))
614 return -EAGAIN;
616 dev->start = 0;
617 if ((chan->state == WAN_CONNECTED) || (chan->state == WAN_CONNECTING))
618 chan_disc(dev);
620 wanpipe_close(card);
622 /* If this is the last close, disconnect physical link */
623 if (!card->open_cnt)
624 disconnect(card);
626 card->wandev.critical = 0;
627 return 0;
630 /*============================================================================
631 * Build media header.
632 * o encapsulate packet according to encapsulation type.
634 * The trick here is to put packet type (Ethertype) into 'protocol' field of
635 * the socket buffer, so that we don't forget it. If encapsulation fails,
636 * set skb->protocol to 0 and discard packet later.
638 * Return: media header length.
640 static int if_header (struct sk_buff* skb, struct net_device* dev,
641 unsigned short type, void* daddr, void* saddr, unsigned len)
643 x25_channel_t* chan = dev->priv;
644 int hdr_len = dev->hard_header_len;
646 skb->protocol = type;
647 if (!chan->protocol)
649 hdr_len = wanrouter_encapsulate(skb, dev);
650 if (hdr_len < 0)
652 hdr_len = 0;
653 skb->protocol = 0;
656 return hdr_len;
659 /*============================================================================
660 * Re-build media header.
662 * Return: 1 physical address resolved.
663 * 0 physical address not resolved
666 static int if_rebuild_hdr (struct sk_buff* skb)
668 struct net_device *dev=skb->dev;
669 x25_channel_t* chan = dev->priv;
670 sdla_t* card = chan->card;
672 printk(KERN_INFO "%s: rebuild_header() called for interface %s!\n",
673 card->devname, dev->name);
674 return 1;
677 /*============================================================================
678 * Send a packet on a network interface.
679 * o set tbusy flag (marks start of the transmission).
680 * o check link state. If link is not up, then drop the packet.
681 * o check channel status. If it's down then initiate a call.
682 * o pass a packet to corresponding WAN device.
683 * o free socket buffer
685 * Return: 0 complete (socket buffer must be freed)
686 * non-0 packet may be re-transmitted (tbusy must be set)
688 * Notes:
689 * 1. This routine is called either by the protocol stack or by the "net
690 * bottom half" (with interrupts enabled).
691 * 2. Setting tbusy flag will inhibit further transmit requests from the
692 * protocol stack and can be used for flow control with protocol layer.
695 static int if_send (struct sk_buff* skb, struct net_device* dev)
697 x25_channel_t* chan = dev->priv;
698 sdla_t* card = chan->card;
699 struct net_device *dev2;
700 TX25Status* status = card->flags;
701 unsigned long host_cpu_flags;
703 if (dev->tbusy)
705 ++chan->ifstats.rx_dropped;
706 if ((jiffies - chan->tick_counter) < (5*HZ))
708 return dev->tbusy;
710 printk(KERN_INFO "%s: Transmit time out %s!\n",
711 card->devname, dev->name)
713 for( dev2 = card->wandev.dev; dev2; dev2 = dev2->slave)
715 dev2->tbusy = 0;
718 chan->tick_counter = jiffies;
720 disable_irq(card->hw.irq);
721 ++card->irq_dis_if_send_count;
723 if (test_and_set_bit(0, (void*)&card->wandev.critical))
725 printk(KERN_INFO "Hit critical in if_send()!\n");
726 if (card->wandev.critical == CRITICAL_IN_ISR)
728 card->wandev.enable_tx_int = 1;
729 dev->tbusy = 1;
731 save_flags(host_cpu_flags);
732 cli();
733 if ((!(--card->irq_dis_if_send_count)) &&
734 (!card->irq_dis_poll_count))
735 enable_irq(card->hw.irq);
736 restore_flags(host_cpu_flags);
738 return dev->tbusy;
740 dev_kfree_skb(skb);
742 save_flags(host_cpu_flags);
743 cli();
744 if ((!(--card->irq_dis_if_send_count)) &&
745 (!card->irq_dis_poll_count))
746 enable_irq(card->hw.irq);
747 restore_flags(host_cpu_flags);
749 return dev->tbusy;
752 /* Below is only until we have per-channel IPX going.... */
753 if(!(chan->svc))
754 chan->protocol = skb->protocol;
756 if (card->wandev.state != WAN_CONNECTED)
757 ++chan->ifstats.tx_dropped;
759 /* Below is only until we have per-channel IPX going.... */
760 else if ( (chan->svc) && (chan->protocol && (chan->protocol != skb->protocol)))
762 printk(KERN_INFO
763 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
764 card->devname, skb->protocol, dev->name);
765 ++chan->ifstats.tx_errors;
767 else switch (chan->state)
769 case WAN_DISCONNECTED:
770 /* Try to establish connection. If succeded, then start
771 * transmission, else drop a packet.
773 if (chan_connect(dev) != 0)
775 ++chan->ifstats.tx_dropped;
776 ++card->wandev.stats.tx_dropped;
777 break;
779 /* fall through */
781 case WAN_CONNECTED:
782 if( skb->protocol == ETH_P_IPX )
784 if(card->wandev.enable_IPX)
786 switch_net_numbers( skb->data,
787 card->wandev.network_number, 0);
789 else
791 ++card->wandev.stats.tx_dropped;
792 ++chan->ifstats.tx_dropped;
793 goto tx_done;
796 dev->trans_start = jiffies;
797 if(chan_send(dev, skb))
799 dev->tbusy = 1;
800 status->imask |= 0x2;
802 break;
804 default:
805 ++chan->ifstats.tx_dropped;
806 ++card->wandev.stats.tx_dropped;
808 tx_done:
809 if (!dev->tbusy)
810 dev_kfree_skb(skb);
812 card->wandev.critical = 0;
813 save_flags(host_cpu_flags);
814 cli();
815 if ((!(--card->irq_dis_if_send_count)) && (!card->irq_dis_poll_count))
816 enable_irq(card->hw.irq);
817 restore_flags(host_cpu_flags);
818 return dev->tbusy;
821 /*============================================================================
822 * Get Ethernet-style interface statistics.
823 * Return a pointer to struct net_device_stats
826 static struct net_device_stats* if_stats (struct net_device* dev)
828 x25_channel_t* chan = dev->priv;
829 if(chan==NULL)
830 return NULL;
831 return &chan->ifstats;
834 /****** Interrupt Handlers **************************************************/
836 /*============================================================================
837 * X.25 Interrupt Service Routine.
840 static void wpx_isr (sdla_t* card)
842 TX25Status* status = card->flags;
843 struct net_device *dev;
844 unsigned long host_cpu_flags;
846 card->in_isr = 1;
847 card->buff_int_mode_unbusy = 0;
849 if (test_and_set_bit(0, (void*)&card->wandev.critical))
852 printk(KERN_INFO "wpx_isr: %s, wandev.critical set to 0x%02X, int type = 0x%02X\n", card->devname, card->wandev.critical, status->iflags);
853 card->in_isr = 0;
854 return;
857 /* For all interrupts set the critical flag to CRITICAL_RX_INTR.
858 * If the if_send routine is called with this flag set it will set
859 * the enable transmit flag to 1. (for a delayed interrupt)
861 card->wandev.critical = CRITICAL_IN_ISR;
863 switch (status->iflags)
865 case 0x01: /* receive interrupt */
866 rx_intr(card);
867 break;
869 case 0x02: /* transmit interrupt */
870 tx_intr(card);
871 card->buff_int_mode_unbusy = 1;
872 status->imask &= ~0x2;
873 break;
875 case 0x04: /* modem status interrupt */
876 status_intr(card);
877 break;
879 case 0x10: /* network event interrupt */
880 event_intr(card);
881 break;
883 default: /* unwanted interrupt */
884 spur_intr(card);
886 card->wandev.critical = CRITICAL_INTR_HANDLED;
887 if( card->wandev.enable_tx_int)
889 card->wandev.enable_tx_int = 0;
890 status->imask |= 0x2;
892 save_flags(host_cpu_flags);
893 cli();
894 card->in_isr = 0;
895 status->iflags = 0; /* clear interrupt condition */
896 card->wandev.critical = 0;
897 restore_flags(host_cpu_flags);
899 if(card->buff_int_mode_unbusy)
901 for(dev = card->wandev.dev; dev; dev = dev->slave)
903 if(((x25_channel_t*)dev->priv)->devtint)
905 mark_bh(NET_BH);
906 return;
912 /*============================================================================
913 * Receive interrupt handler.
914 * This routine handles fragmented IP packets using M-bit according to the
915 * RFC1356.
916 * o map ligical channel number to network interface.
917 * o allocate socket buffer or append received packet to the existing one.
918 * o if M-bit is reset (i.e. it's the last packet in a sequence) then
919 * decapsulate packet and pass socket buffer to the protocol stack.
921 * Notes:
922 * 1. When allocating a socket buffer, if M-bit is set then more data is
923 * comming and we have to allocate buffer for the maximum IP packet size
924 * expected on this channel.
925 * 2. If something goes wrong and X.25 packet has to be dropped (e.g. no
926 * socket buffers available) the whole packet sequence must be discarded.
929 static void rx_intr (sdla_t* card)
931 TX25Mbox* rxmb = card->rxmb;
932 unsigned lcn = rxmb->cmd.lcn; /* logical channel number */
933 unsigned len = rxmb->cmd.length; /* packet length */
934 unsigned qdm = rxmb->cmd.qdm; /* Q,D and M bits */
935 wan_device_t* wandev = &card->wandev;
936 struct net_device* dev = get_dev_by_lcn(wandev, lcn);
937 x25_channel_t* chan;
938 struct sk_buff* skb;
939 void* bufptr;
941 if (dev == NULL)
943 /* Invalid channel, discard packet */
944 printk(KERN_INFO "%s: receiving on orphaned LCN %d!\n",
945 card->devname, lcn);
946 return;
949 chan = dev->priv;
950 chan->i_timeout_sofar = jiffies;
951 if (chan->drop_sequence)
953 if (!(qdm & 0x01)) chan->drop_sequence = 0;
954 return;
957 skb = chan->rx_skb;
958 if (skb == NULL)
960 /* Allocate new socket buffer */
961 int bufsize = (qdm & 0x01) ? dev->mtu : len;
963 skb = dev_alloc_skb(bufsize + dev->hard_header_len);
964 if (skb == NULL)
966 printk(KERN_INFO "%s: no socket buffers available!\n",
967 card->devname);
968 chan->drop_sequence = 1; /* set flag */
969 ++chan->ifstats.rx_dropped;
970 return;
972 skb->dev = dev;
973 skb->protocol = htons(chan->protocol);
974 chan->rx_skb = skb;
977 if (skb_tailroom(skb) < len)
979 /* No room for the packet. Call off the whole thing! */
980 dev_kfree_skb(skb);
981 chan->rx_skb = NULL;
982 if (qdm & 0x01) chan->drop_sequence = 1;
984 printk(KERN_INFO "%s: unexpectedly long packet sequence "
985 "on interface %s!\n", card->devname, dev->name);
986 ++chan->ifstats.rx_length_errors;
987 return;
990 /* Append packet to the socket buffer */
991 bufptr = skb_put(skb, len);
992 memcpy(bufptr, rxmb->data, len);
994 if (qdm & 0x01)
995 return; /* more data is comming */
997 dev->last_rx = jiffies; /* timestamp */
998 chan->rx_skb = NULL; /* dequeue packet */
1000 /* Decapsulate packet, if necessary */
1001 if (!skb->protocol && !wanrouter_type_trans(skb, dev))
1003 /* can't decapsulate packet */
1004 dev_kfree_skb(skb);
1005 ++chan->ifstats.rx_errors;
1007 else
1009 if( handle_IPXWAN(skb->data, card->devname, card->wandev.enable_IPX, card->wandev.network_number, skb->protocol))
1011 if( card->wandev.enable_IPX )
1013 if(chan_send(dev, skb))
1015 chan->tx_skb = skb;
1017 else
1019 dev_kfree_skb(skb);
1022 else
1024 /* FIXME: increment IPX packet dropped statistic */
1027 else
1029 netif_rx(skb);
1030 ++chan->ifstats.rx_packets;
1031 chan->ifstats.rx_bytes += skb->len;
1036 /*============================================================================
1037 * Transmit interrupt handler.
1038 * o Release socket buffer
1039 * o Clear 'tbusy' flag
1042 static void tx_intr (sdla_t* card)
1044 struct net_device *dev;
1046 /* unbusy all devices and then dev_tint(); */
1047 for(dev = card->wandev.dev; dev; dev = dev->slave)
1049 ((x25_channel_t*)dev->priv)->devtint = dev->tbusy;
1050 dev->tbusy = 0;
1055 /*============================================================================
1056 * Modem status interrupt handler.
1058 static void status_intr (sdla_t* card)
1062 /*============================================================================
1063 * Network event interrupt handler.
1065 static void event_intr (sdla_t* card)
1069 /*============================================================================
1070 * Spurious interrupt handler.
1071 * o print a warning
1072 * o
1073 * If number of spurious interrupts exceeded some limit, then ???
1075 static void spur_intr (sdla_t* card)
1077 printk(KERN_INFO "%s: spurious interrupt!\n", card->devname);
1080 /****** Background Polling Routines ****************************************/
1082 /*============================================================================
1083 * Main polling routine.
1084 * This routine is repeatedly called by the WANPIPE 'thread' to allow for
1085 * time-dependent housekeeping work.
1087 * Notes:
1088 * 1. This routine may be called on interrupt context with all interrupts
1089 * enabled. Beware!
1092 static void wpx_poll (sdla_t* card)
1094 unsigned long host_cpu_flags;
1096 disable_irq(card->hw.irq);
1097 ++card->irq_dis_poll_count;
1099 if (test_and_set_bit(0, (void*)&card->wandev.critical))
1101 printk(KERN_INFO "%s: critical in polling!\n",card->devname);
1102 save_flags(host_cpu_flags);
1103 cli();
1104 if ((!card->irq_dis_if_send_count) &&
1105 (!(--card->irq_dis_poll_count)))
1106 enable_irq(card->hw.irq);
1107 restore_flags(host_cpu_flags);
1108 return;
1111 switch(card->wandev.state)
1113 case WAN_CONNECTED:
1114 poll_active(card);
1115 break;
1117 case WAN_CONNECTING:
1118 poll_connecting(card);
1119 break;
1121 case WAN_DISCONNECTED:
1122 poll_disconnected(card);
1124 card->wandev.critical = 0;
1125 save_flags(host_cpu_flags);
1126 cli();
1127 if ((!card->irq_dis_if_send_count) && (!(--card->irq_dis_poll_count)))
1128 enable_irq(card->hw.irq);
1129 restore_flags(host_cpu_flags);
1132 /*============================================================================
1133 * Handle physical link establishment phase.
1134 * o if connection timed out, disconnect the link.
1136 static void poll_connecting (sdla_t* card)
1138 TX25Status* status = card->flags;
1140 if (status->gflags & X25_HDLC_ABM)
1142 wanpipe_set_state(card, WAN_CONNECTED);
1143 x25_set_intr_mode(card, 0x83); /* enable Rx interrupts */
1144 status->imask &= ~0x2; /* mask Tx interupts */
1146 else if ((jiffies - card->state_tick) > CONNECT_TIMEOUT)
1147 disconnect(card);
1150 /*============================================================================
1151 * Handle physical link disconnected phase.
1152 * o if hold-down timeout has expired and there are open interfaces, connect
1153 * link.
1155 static void poll_disconnected (sdla_t* card)
1157 if (card->open_cnt && ((jiffies - card->state_tick) > HOLD_DOWN_TIME))
1158 connect(card);
1161 /*============================================================================
1162 * Handle active link phase.
1163 * o fetch X.25 asynchronous events.
1164 * o kick off transmission on all interfaces.
1166 static void poll_active (sdla_t* card)
1168 struct net_device* dev;
1170 /* Fetch X.25 asynchronous events */
1171 x25_fetch_events(card);
1173 for (dev = card->wandev.dev; dev; dev = dev->slave)
1175 x25_channel_t* chan = dev->priv;
1176 struct sk_buff* skb = chan->tx_skb;
1178 /* If there is a packet queued for transmission then kick
1179 * the channel's send routine. When transmission is complete
1180 * or if error has occurred, release socket buffer and reset
1181 * 'tbusy' flag.
1183 if (skb && (chan_send(dev, skb) == 0))
1185 chan->tx_skb = NULL;
1186 dev->tbusy = 0;
1187 dev_kfree_skb(skb);
1190 /* If SVC has been idle long enough, close virtual circuit */
1192 if(( chan->svc )&&( chan->state == WAN_CONNECTED ))
1194 if( (jiffies - chan->i_timeout_sofar) / HZ > chan->idle_timeout )
1196 /* Close svc */
1197 printk(KERN_INFO "%s: Closing down Idle link %s on LCN %d\n",card->devname,chan->name,chan->lcn);
1198 chan->i_timeout_sofar = jiffies;
1199 chan_disc(dev);
1205 /****** SDLA Firmware-Specific Functions *************************************
1206 * Almost all X.25 commands can unexpetedly fail due to so called 'X.25
1207 * asynchronous events' such as restart, interrupt, incoming call request,
1208 * call clear request, etc. They can't be ignored and have to be dealt with
1209 * immediately. To tackle with this problem we execute each interface command
1210 * in a loop until good return code is received or maximum number of retries
1211 * is reached. Each interface command returns non-zero return code, an
1212 * asynchronous event/error handler x25_error() is called.
1215 /*============================================================================
1216 * Read X.25 firmware version.
1217 * Put code version as ASCII string in str.
1219 static int x25_get_version (sdla_t* card, char* str)
1221 TX25Mbox* mbox = card->mbox;
1222 int retry = MAX_CMD_RETRY;
1223 int err;
1227 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1228 mbox->cmd.command = X25_READ_CODE_VERSION;
1229 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1230 } while (err && retry-- &&
1231 x25_error(card, err, X25_READ_CODE_VERSION, 0));
1233 if (!err && str)
1235 int len = mbox->cmd.length;
1236 memcpy(str, mbox->data, len);
1237 str[len] = '\0';
1239 return err;
1242 /*============================================================================
1243 * Configure adapter.
1246 static int x25_configure (sdla_t* card, TX25Config* conf)
1248 TX25Mbox* mbox = card->mbox;
1249 int retry = MAX_CMD_RETRY;
1250 int err;
1254 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1255 memcpy(mbox->data, (void*)conf, sizeof(TX25Config));
1256 mbox->cmd.length = sizeof(TX25Config);
1257 mbox->cmd.command = X25_SET_CONFIGURATION;
1258 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1259 } while (err && retry-- && x25_error(card, err, X25_SET_CONFIGURATION, 0));
1260 return err;
1263 /*============================================================================
1264 * Get communications error statistics.
1266 static int x25_get_err_stats (sdla_t* card)
1268 TX25Mbox* mbox = card->mbox;
1269 int retry = MAX_CMD_RETRY;
1270 int err;
1274 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1275 mbox->cmd.command = X25_HDLC_READ_COMM_ERR;
1276 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1277 } while (err && retry-- && x25_error(card, err, X25_HDLC_READ_COMM_ERR, 0));
1279 if (!err)
1281 THdlcCommErr* stats = (void*)mbox->data;
1283 card->wandev.stats.rx_over_errors = stats->rxOverrun;
1284 card->wandev.stats.rx_crc_errors = stats->rxBadCrc;
1285 card->wandev.stats.rx_missed_errors = stats->rxAborted;
1286 card->wandev.stats.tx_aborted_errors = stats->txAborted;
1288 return err;
1291 /*============================================================================
1292 * Get protocol statistics.
1294 static int x25_get_stats (sdla_t* card)
1296 TX25Mbox* mbox = card->mbox;
1297 int retry = MAX_CMD_RETRY;
1298 int err;
1302 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1303 mbox->cmd.command = X25_READ_STATISTICS;
1304 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1305 } while (err && retry-- && x25_error(card, err, X25_READ_STATISTICS, 0));
1307 if (!err)
1309 TX25Stats* stats = (void*)mbox->data;
1311 card->wandev.stats.rx_packets = stats->rxData;
1312 card->wandev.stats.tx_packets = stats->rxData;
1314 return err;
1317 /*============================================================================
1318 * Close HDLC link.
1320 static int x25_close_hdlc (sdla_t* card)
1322 TX25Mbox* mbox = card->mbox;
1323 int retry = MAX_CMD_RETRY;
1324 int err;
1328 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1329 mbox->cmd.command = X25_HDLC_LINK_CLOSE;
1330 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1331 } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_CLOSE, 0));
1333 return err;
1336 /*============================================================================
1337 * Open HDLC link.
1339 static int x25_open_hdlc (sdla_t* card)
1341 TX25Mbox* mbox = card->mbox;
1342 int retry = MAX_CMD_RETRY;
1343 int err;
1347 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1348 mbox->cmd.command = X25_HDLC_LINK_OPEN;
1349 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1350 } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_OPEN, 0));
1352 return err;
1355 /*============================================================================
1356 * Setup HDLC link.
1358 static int x25_setup_hdlc (sdla_t* card)
1360 TX25Mbox* mbox = card->mbox;
1361 int retry = MAX_CMD_RETRY;
1362 int err;
1366 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1367 mbox->cmd.command = X25_HDLC_LINK_SETUP;
1368 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1369 } while (err && retry-- && x25_error(card, err, X25_HDLC_LINK_SETUP, 0));
1371 return err;
1374 /*============================================================================
1375 * Set (raise/drop) DTR.
1377 static int x25_set_dtr (sdla_t* card, int dtr)
1379 TX25Mbox* mbox = card->mbox;
1380 int retry = MAX_CMD_RETRY;
1381 int err;
1385 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1386 mbox->data[0] = 0;
1387 mbox->data[2] = 0;
1388 mbox->data[1] = dtr ? 0x02 : 0x01;
1389 mbox->cmd.length = 3;
1390 mbox->cmd.command = X25_SET_GLOBAL_VARS;
1391 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1392 } while (err && retry-- && x25_error(card, err, X25_SET_GLOBAL_VARS, 0));
1394 return err;
1397 /*============================================================================
1398 * Set interrupt mode.
1400 static int x25_set_intr_mode (sdla_t* card, int mode)
1402 TX25Mbox* mbox = card->mbox;
1403 int retry = MAX_CMD_RETRY;
1404 int err;
1408 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1409 mbox->data[0] = mode;
1410 if (card->hw.fwid == SFID_X25_508)
1412 mbox->data[1] = card->hw.irq;
1413 mbox->cmd.length = 2;
1415 else mbox->cmd.length = 1;
1416 mbox->cmd.command = X25_SET_INTERRUPT_MODE;
1417 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1418 } while (err && retry-- && x25_error(card, err, X25_SET_INTERRUPT_MODE, 0)) ;
1419 return err;
1422 /*============================================================================
1423 * Read X.25 channel configuration.
1425 static int x25_get_chan_conf (sdla_t* card, x25_channel_t* chan)
1427 TX25Mbox* mbox = card->mbox;
1428 int retry = MAX_CMD_RETRY;
1429 int lcn = chan->lcn;
1430 int err;
1434 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1435 mbox->cmd.lcn = lcn;
1436 mbox->cmd.command = X25_READ_CHANNEL_CONFIG;
1437 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1438 } while (err && retry-- && x25_error(card, err, X25_READ_CHANNEL_CONFIG, lcn));
1440 if (!err)
1442 TX25Status* status = card->flags;
1444 /* calculate an offset into the array of status bytes */
1445 if (card->u.x.hi_svc <= 255)
1446 chan->ch_idx = lcn - 1;
1447 else
1449 int offset;
1451 switch (mbox->data[0] && 0x1F)
1453 case 0x01:
1454 offset = status->pvc_map; break;
1455 case 0x03:
1456 offset = status->icc_map; break;
1457 case 0x07:
1458 offset = status->twc_map; break;
1459 case 0x0B:
1460 offset = status->ogc_map; break;
1461 default:
1462 offset = 0;
1464 chan->ch_idx = lcn - 1 - offset;
1467 /* get actual transmit packet size on this channel */
1468 switch(mbox->data[1] & 0x38)
1470 case 0x00:
1471 chan->tx_pkt_size = 16;
1472 break;
1473 case 0x08:
1474 chan->tx_pkt_size = 32;
1475 break;
1476 case 0x10:
1477 chan->tx_pkt_size = 64;
1478 break;
1479 case 0x18:
1480 chan->tx_pkt_size = 128;
1481 break;
1482 case 0x20:
1483 chan->tx_pkt_size = 256;
1484 break;
1485 case 0x28:
1486 chan->tx_pkt_size = 512;
1487 break;
1488 case 0x30:
1489 chan->tx_pkt_size = 1024;
1490 break;
1492 printk(KERN_INFO "%s: X.25 packet size on LCN %d is %d.\n",
1493 card->devname, lcn, chan->tx_pkt_size);
1495 return err;
1498 /*============================================================================
1499 * Place X.25 call.
1502 static int x25_place_call (sdla_t* card, x25_channel_t* chan)
1504 TX25Mbox* mbox = card->mbox;
1505 int retry = MAX_CMD_RETRY;
1506 int err;
1507 char str[64];
1509 sprintf(str, "-d%s -uCC", chan->addr);
1512 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1513 strcpy(mbox->data, str);
1514 mbox->cmd.length = strlen(str);
1515 mbox->cmd.command = X25_PLACE_CALL;
1516 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1517 } while (err && retry-- && x25_error(card, err, X25_PLACE_CALL, 0));
1519 if (!err)
1521 chan->lcn = mbox->cmd.lcn;
1522 chan->protocol = ETH_P_IP;
1524 return err;
1527 /*============================================================================
1528 * Accept X.25 call.
1531 static int x25_accept_call (sdla_t* card, int lcn, int qdm)
1533 TX25Mbox* mbox = card->mbox;
1534 int retry = MAX_CMD_RETRY;
1535 int err;
1539 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1540 mbox->cmd.lcn = lcn;
1541 mbox->cmd.qdm = qdm;
1542 mbox->cmd.command = X25_ACCEPT_CALL;
1543 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1544 } while (err && retry-- && x25_error(card, err, X25_ACCEPT_CALL, lcn));
1546 return err;
1549 /*============================================================================
1550 * Clear X.25 call.
1552 static int x25_clear_call (sdla_t* card, int lcn, int cause, int diagn)
1554 TX25Mbox* mbox = card->mbox;
1555 int retry = MAX_CMD_RETRY;
1556 int err;
1560 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1561 mbox->cmd.lcn = lcn;
1562 mbox->cmd.cause = cause;
1563 mbox->cmd.diagn = diagn;
1564 mbox->cmd.command = X25_CLEAR_CALL;
1565 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1566 } while (err && retry-- && x25_error(card, err, X25_CLEAR_CALL, lcn));
1568 return err;
1571 /*============================================================================
1572 * Send X.25 data packet.
1574 static int x25_send (sdla_t* card, int lcn, int qdm, int len, void* buf)
1576 TX25Mbox* mbox = card->mbox;
1577 int retry = MAX_CMD_RETRY;
1578 int err;
1582 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1583 memcpy(mbox->data, buf, len);
1584 mbox->cmd.length = len;
1585 mbox->cmd.lcn = lcn;
1586 mbox->cmd.qdm = qdm;
1587 mbox->cmd.command = X25_WRITE;
1588 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1589 } while (err && retry-- && x25_error(card, err, X25_WRITE, lcn));
1590 return err;
1593 /*============================================================================
1594 * Fetch X.25 asynchronous events.
1596 static int x25_fetch_events (sdla_t* card)
1598 TX25Status* status = card->flags;
1599 TX25Mbox* mbox = card->mbox;
1600 int err = 0;
1602 if (status->gflags & 0x20)
1604 memset(&mbox->cmd, 0, sizeof(TX25Cmd));
1605 mbox->cmd.command = X25_IS_DATA_AVAILABLE;
1606 err = sdla_exec(mbox) ? mbox->cmd.result : CMD_TIMEOUT;
1607 if (err)
1608 x25_error(card, err, X25_IS_DATA_AVAILABLE, 0);
1610 return err;
1613 /*============================================================================
1614 * X.25 asynchronous event/error handler.
1615 * This routine is called each time interface command returns non-zero
1616 * return code to handle X.25 asynchronous events and common errors.
1617 * Return non-zero to repeat command or zero to cancel it.
1619 * Notes:
1620 * 1. This function may be called recursively, as handling some of the
1621 * asynchronous events (e.g. call request) requires execution of the
1622 * interface command(s) that, in turn, may also return asynchronous
1623 * events. To avoid re-entrancy problems we copy mailbox to dynamically
1624 * allocated memory before processing events.
1626 static int x25_error (sdla_t* card, int err, int cmd, int lcn)
1628 int retry = 1;
1629 unsigned dlen = ((TX25Mbox*)card->mbox)->cmd.length;
1630 TX25Mbox* mb;
1632 mb = kmalloc(sizeof(TX25Mbox) + dlen, GFP_ATOMIC);
1633 if (mb == NULL)
1635 printk(KERN_ERR "%s: x25_error() out of memory!\n",
1636 card->devname);
1637 return 0;
1639 memcpy(mb, card->mbox, sizeof(TX25Mbox) + dlen);
1640 switch (err)
1642 case 0x40: /* X.25 asynchronous packet was received */
1643 mb->data[dlen] = '\0';
1644 switch (mb->cmd.pktType & 0x7F)
1646 case 0x30: /* incoming call */
1647 retry = incoming_call(card, cmd, lcn, mb);
1648 break;
1650 case 0x31: /* connected */
1651 retry = call_accepted(card, cmd, lcn, mb);
1652 break;
1654 case 0x02: /* call clear request */
1655 retry = call_cleared(card, cmd, lcn, mb);
1656 break;
1658 case 0x04: /* reset request */
1659 printk(KERN_INFO "%s: X.25 reset request on LCN %d! "
1660 "Cause:0x%02X Diagn:0x%02X\n",
1661 card->devname, mb->cmd.lcn, mb->cmd.cause,
1662 mb->cmd.diagn);
1663 break;
1665 case 0x08: /* restart request */
1666 retry = restart_event(card, cmd, lcn, mb);
1667 break;
1669 default:
1670 printk(KERN_INFO "%s: X.25 event 0x%02X on LCN %d! "
1671 "Cause:0x%02X Diagn:0x%02X\n",
1672 card->devname, mb->cmd.pktType,
1673 mb->cmd.lcn, mb->cmd.cause, mb->cmd.diagn);
1675 break;
1677 case 0x41: /* X.25 protocol violation indication */
1678 printk(KERN_INFO
1679 "%s: X.25 protocol violation on LCN %d! "
1680 "Packet:0x%02X Cause:0x%02X Diagn:0x%02X\n",
1681 card->devname, mb->cmd.lcn,
1682 mb->cmd.pktType & 0x7F, mb->cmd.cause, mb->cmd.diagn);
1683 break;
1685 case 0x42: /* X.25 timeout */
1686 retry = timeout_event(card, cmd, lcn, mb);
1687 break;
1689 case 0x43: /* X.25 retry limit exceeded */
1690 printk(KERN_INFO
1691 "%s: exceeded X.25 retry limit on LCN %d! "
1692 "Packet:0x%02X Diagn:0x%02X\n", card->devname,
1693 mb->cmd.lcn, mb->cmd.pktType, mb->cmd.diagn);
1694 break;
1696 case 0x08: /* modem failure */
1697 printk(KERN_INFO "%s: modem failure!\n", card->devname);
1698 break;
1700 case 0x09: /* N2 retry limit */
1701 printk(KERN_INFO "%s: exceeded HDLC retry limit!\n",
1702 card->devname);
1703 break;
1705 case 0x06: /* unnumbered frame was received while in ABM */
1706 printk(KERN_INFO "%s: received Unnumbered frame 0x%02X!\n",
1707 card->devname, mb->data[0]);
1708 break;
1710 case CMD_TIMEOUT:
1711 printk(KERN_ERR "%s: command 0x%02X timed out!\n",
1712 card->devname, cmd);
1713 retry = 0; /* abort command */
1714 break;
1716 default:
1717 printk(KERN_INFO "%s: command 0x%02X returned 0x%02X!\n",
1718 card->devname, cmd, err);
1719 retry = 0; /* abort command */
1721 kfree(mb);
1722 return retry;
1725 /****** X.25 Asynchronous Event Handlers *************************************
1726 * These functions are called by the x25_error() and should return 0, if
1727 * the command resulting in the asynchronous event must be aborted.
1730 /*============================================================================
1731 * Handle X.25 incoming call request.
1732 * RFC 1356 establishes the following rules:
1733 * 1. The first octet in the Call User Data (CUD) field of the call
1734 * request packet contains NLPID identifying protocol encapsulation.
1735 * 2. Calls MUST NOT be accepted unless router supports requested
1736 * protocol encapsulation.
1737 * 3. A diagnostic code 249 defined by ISO/IEC 8208 may be used when
1738 * clearing a call because protocol encapsulation is not supported.
1739 * 4. If an incoming call is received while a call request is pending
1740 * (i.e. call collision has occurred), the incoming call shall be
1741 * rejected and call request shall be retried.
1744 static int incoming_call (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
1746 wan_device_t* wandev = &card->wandev;
1747 int new_lcn = mb->cmd.lcn;
1748 struct net_device* dev = get_dev_by_lcn(wandev, new_lcn);
1749 x25_channel_t* chan = NULL;
1750 int accept = 0; /* set to '1' if o.k. to accept call */
1751 x25_call_info_t* info;
1753 /* Make sure there is no call collision */
1754 if (dev != NULL)
1756 printk(KERN_INFO
1757 "%s: X.25 incoming call collision on LCN %d!\n",
1758 card->devname, new_lcn);
1759 x25_clear_call(card, new_lcn, 0, 0);
1760 return 1;
1763 /* Make sure D bit is not set in call request */
1764 if (mb->cmd.qdm & 0x02)
1766 printk(KERN_INFO
1767 "%s: X.25 incoming call on LCN %d with D-bit set!\n",
1768 card->devname, new_lcn);
1769 x25_clear_call(card, new_lcn, 0, 0);
1770 return 1;
1773 /* Parse call request data */
1774 info = kmalloc(sizeof(x25_call_info_t), GFP_ATOMIC);
1775 if (info == NULL)
1777 printk(KERN_ERR
1778 "%s: not enough memory to parse X.25 incoming call "
1779 "on LCN %d!\n", card->devname, new_lcn);
1780 x25_clear_call(card, new_lcn, 0, 0);
1781 return 1;
1783 parse_call_info(mb->data, info);
1784 printk(KERN_INFO "%s: X.25 incoming call on LCN %d! Call data: %s\n",
1785 card->devname, new_lcn, mb->data);
1787 /* Find available channel */
1788 for (dev = wandev->dev; dev; dev = dev->slave)
1790 chan = dev->priv;
1792 if (!chan->svc || (chan->state != WAN_DISCONNECTED))
1793 continue;
1794 if (strcmp(info->src, chan->addr) == 0)
1795 break;
1796 /* If just an '@' is specified, accept all incoming calls */
1797 if (strcmp(chan->addr, "") == 0)
1798 break;
1801 if (dev == NULL)
1803 printk(KERN_INFO "%s: no channels available!\n",
1804 card->devname);
1805 x25_clear_call(card, new_lcn, 0, 0);
1808 /* Check requested protocol encapsulation */
1809 else if (info->nuser == 0)
1811 printk(KERN_INFO
1812 "%s: no user data in incoming call on LCN %d!\n",
1813 card->devname, new_lcn);
1814 x25_clear_call(card, new_lcn, 0, 0);
1816 else switch (info->user[0])
1818 case 0: /* multiplexed */
1819 chan->protocol = 0;
1820 accept = 1;
1821 break;
1823 case NLPID_IP: /* IP datagrams */
1824 chan->protocol = ETH_P_IP;
1825 accept = 1;
1826 break;
1828 case NLPID_SNAP: /* IPX datagrams */
1829 chan->protocol = ETH_P_IPX;
1830 accept = 1;
1831 break;
1832 default:
1833 printk(KERN_INFO
1834 "%s: unsupported NLPID 0x%02X in incoming call "
1835 "on LCN %d!\n", card->devname, info->user[0], new_lcn);
1836 x25_clear_call(card, new_lcn, 0, 249);
1839 if (accept && (x25_accept_call(card, new_lcn, 0) == CMD_OK))
1841 chan->lcn = new_lcn;
1842 if (x25_get_chan_conf(card, chan) == CMD_OK)
1843 set_chan_state(dev, WAN_CONNECTED);
1844 else
1845 x25_clear_call(card, new_lcn, 0, 0);
1847 kfree(info);
1848 return 1;
1851 /*============================================================================
1852 * Handle accepted call.
1855 static int call_accepted (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
1857 unsigned new_lcn = mb->cmd.lcn;
1858 struct net_device* dev = get_dev_by_lcn(&card->wandev, new_lcn);
1859 x25_channel_t* chan;
1861 printk(KERN_INFO "%s: X.25 call accepted on LCN %d!\n",
1862 card->devname, new_lcn);
1863 if (dev == NULL)
1865 printk(KERN_INFO
1866 "%s: clearing orphaned connection on LCN %d!\n",
1867 card->devname, new_lcn);
1868 x25_clear_call(card, new_lcn, 0, 0);
1869 return 1;
1872 /* Get channel configuration and notify router */
1873 chan = dev->priv;
1874 if (x25_get_chan_conf(card, chan) != CMD_OK)
1876 x25_clear_call(card, new_lcn, 0, 0);
1877 return 1;
1879 set_chan_state(dev, WAN_CONNECTED);
1880 return 1;
1883 /*============================================================================
1884 * Handle cleared call.
1887 static int call_cleared (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
1889 unsigned new_lcn = mb->cmd.lcn;
1890 struct net_device* dev = get_dev_by_lcn(&card->wandev, new_lcn);
1892 printk(KERN_INFO "%s: X.25 clear request on LCN %d! Cause:0x%02X "
1893 "Diagn:0x%02X\n",
1894 card->devname, new_lcn, mb->cmd.cause, mb->cmd.diagn);
1895 if (dev == NULL)
1896 return 1;
1897 set_chan_state(dev, WAN_DISCONNECTED);
1898 return ((cmd == X25_WRITE) && (lcn == new_lcn)) ? 0 : 1;
1901 /*============================================================================
1902 * Handle X.25 restart event.
1905 static int restart_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
1907 wan_device_t* wandev = &card->wandev;
1908 struct net_device* dev;
1910 printk(KERN_INFO
1911 "%s: X.25 restart request! Cause:0x%02X Diagn:0x%02X\n",
1912 card->devname, mb->cmd.cause, mb->cmd.diagn);
1914 /* down all logical channels */
1915 for (dev = wandev->dev; dev; dev = dev->slave)
1916 set_chan_state(dev, WAN_DISCONNECTED);
1917 return (cmd == X25_WRITE) ? 0 : 1;
1920 /*============================================================================
1921 * Handle timeout event.
1923 static int timeout_event (sdla_t* card, int cmd, int lcn, TX25Mbox* mb)
1925 unsigned new_lcn = mb->cmd.lcn;
1927 if (mb->cmd.pktType == 0x05) /* call request time out */
1929 struct net_device* dev = get_dev_by_lcn(&card->wandev, new_lcn);
1931 printk(KERN_INFO "%s: X.25 call timed timeout on LCN %d!\n",
1932 card->devname, new_lcn);
1933 if (dev)
1934 set_chan_state(dev, WAN_DISCONNECTED);
1936 else printk(KERN_INFO "%s: X.25 packet 0x%02X timeout on LCN %d!\n",
1937 card->devname, mb->cmd.pktType, new_lcn);
1938 return 1;
1941 /******* Miscellaneous ******************************************************/
1943 /*============================================================================
1944 * Establish physical connection.
1945 * o open HDLC and raise DTR
1947 * Return: 0 connection established
1948 * 1 connection is in progress
1949 * <0 error
1951 static int connect (sdla_t* card)
1953 if (x25_open_hdlc(card) || x25_setup_hdlc(card))
1954 return -EIO;
1955 wanpipe_set_state(card, WAN_CONNECTING);
1956 return 1;
1959 /*============================================================================
1960 * Tear down physical connection.
1961 * o close HDLC link
1962 * o drop DTR
1964 * Return: 0
1965 * <0 error
1967 static int disconnect (sdla_t* card)
1969 wanpipe_set_state(card, WAN_DISCONNECTED);
1970 x25_set_intr_mode(card, 0); /* disable interrupt generation */
1971 x25_close_hdlc(card); /* close HDLC link */
1972 x25_set_dtr(card, 0); /* drop DTR */
1973 return 0;
1976 /*============================================================================
1977 * Find network device by its channel number.
1979 static struct net_device* get_dev_by_lcn (wan_device_t* wandev, unsigned lcn)
1981 struct net_device* dev;
1983 for (dev = wandev->dev; dev; dev = dev->slave)
1984 if (((x25_channel_t*)dev->priv)->lcn == lcn)
1985 break;
1986 return dev;
1989 /*============================================================================
1990 * Initiate connection on the logical channel.
1991 * o for PVC we just get channel configuration
1992 * o for SVCs place an X.25 call
1994 * Return: 0 connected
1995 * >0 connection in progress
1996 * <0 failure
1998 static int chan_connect (struct net_device* dev)
2000 x25_channel_t* chan = dev->priv;
2001 sdla_t* card = chan->card;
2003 if (chan->svc)
2005 if (!chan->addr[0])
2006 return -EINVAL; /* no destination address */
2007 printk(KERN_INFO "%s: placing X.25 call to %s ...\n",
2008 card->devname, chan->addr);
2009 if (x25_place_call(card, chan) != CMD_OK)
2010 return -EIO;
2011 set_chan_state(dev, WAN_CONNECTING);
2012 return 1;
2014 else
2016 if (x25_get_chan_conf(card, chan) != CMD_OK)
2017 return -EIO;
2018 set_chan_state(dev, WAN_CONNECTED);
2020 return 0;
2023 /*============================================================================
2024 * Disconnect logical channel.
2025 * o if SVC then clear X.25 call
2027 static int chan_disc (struct net_device* dev)
2029 x25_channel_t* chan = dev->priv;
2031 if (chan->svc)
2032 x25_clear_call(chan->card, chan->lcn, 0, 0);
2033 set_chan_state(dev, WAN_DISCONNECTED);
2034 return 0;
2037 /*============================================================================
2038 * Set logical channel state.
2040 static void set_chan_state (struct net_device* dev, int state)
2042 x25_channel_t* chan = dev->priv;
2043 sdla_t* card = chan->card;
2044 unsigned long flags;
2046 save_flags(flags);
2047 cli();
2048 if (chan->state != state)
2050 switch (state)
2052 case WAN_CONNECTED:
2053 printk (KERN_INFO "%s: interface %s connected!\n",
2054 card->devname, dev->name);
2055 *(unsigned short*)dev->dev_addr = htons(chan->lcn);
2056 chan->i_timeout_sofar = jiffies;
2057 break;
2059 case WAN_CONNECTING:
2060 printk (KERN_INFO "%s: interface %s connecting...\n",
2061 card->devname, dev->name);
2062 break;
2064 case WAN_DISCONNECTED:
2065 printk (KERN_INFO "%s: interface %s disconnected!\n",
2066 card->devname, dev->name);
2067 if (chan->svc)
2069 *(unsigned short*)dev->dev_addr = 0;
2070 chan->lcn = 0;
2072 break;
2074 chan->state = state;
2076 chan->state_tick = jiffies;
2077 restore_flags(flags);
2080 /*============================================================================
2081 * Send packet on a logical channel.
2082 * When this function is called, tx_skb field of the channel data space
2083 * points to the transmit socket buffer. When transmission is complete,
2084 * release socket buffer and reset 'tbusy' flag.
2086 * Return: 0 - transmission complete
2087 * 1 - busy
2089 * Notes:
2090 * 1. If packet length is greater than MTU for this channel, we'll fragment
2091 * the packet into 'complete sequence' using M-bit.
2092 * 2. When transmission is complete, an event notification should be issued
2093 * to the router.
2095 static int chan_send (struct net_device* dev, struct sk_buff* skb)
2097 x25_channel_t* chan = dev->priv;
2098 sdla_t* card = chan->card;
2099 TX25Status* status = card->flags;
2100 unsigned len, qdm;
2102 /* Check to see if channel is ready */
2103 if (!(status->cflags[chan->ch_idx] & 0x40))
2104 return 1;
2106 if (skb->len > chan->tx_pkt_size)
2108 len = chan->tx_pkt_size;
2109 qdm = 0x01; /* set M-bit (more data) */
2111 else /* final packet */
2113 len = skb->len;
2114 qdm = 0;
2116 switch(x25_send(card, chan->lcn, qdm, len, skb->data))
2118 case 0x00: /* success */
2119 chan->i_timeout_sofar = jiffies;
2120 if (qdm)
2122 skb_pull(skb, len);
2123 return 1;
2125 ++chan->ifstats.tx_packets;
2126 chan->ifstats.tx_bytes += skb->len;
2127 break;
2129 case 0x33: /* Tx busy */
2130 return 1;
2132 default: /* failure */
2133 ++chan->ifstats.tx_errors;
2134 /* return 1; */
2136 return 0;
2139 /*============================================================================
2140 * Parse X.25 call request data and fill x25_call_info_t structure.
2143 static void parse_call_info (unsigned char* str, x25_call_info_t* info)
2145 memset(info, 0, sizeof(x25_call_info_t));
2146 for (; *str; ++str)
2148 int i;
2149 unsigned ch;
2151 if (*str == '-') switch (str[1])
2153 case 'd': /* destination address */
2154 for (i = 0; i < 16; ++i)
2156 ch = str[2+i];
2157 if (!is_digit(ch))
2158 break;
2159 info->dest[i] = ch;
2161 break;
2163 case 's': /* source address */
2164 for (i = 0; i < 16; ++i)
2166 ch = str[2+i];
2167 if (!is_digit(ch))
2168 break;
2169 info->src[i] = ch;
2171 break;
2173 case 'u': /* user data */
2174 for (i = 0; i < 127; ++i)
2176 ch = str[2+2*i];
2177 if (!is_hex_digit(ch))
2178 break;
2179 info->user[i] = hex_to_uint(&str[2+2*i], 2);
2181 info->nuser = i;
2182 break;
2184 case 'f': /* facilities */
2185 for (i = 0; i < 64; ++i)
2187 ch = str[2+4*i];
2188 if (!is_hex_digit(ch))
2189 break;
2190 info->facil[i].code =
2191 hex_to_uint(&str[2+4*i], 2);
2192 ch = str[4+4*i];
2193 if (!is_hex_digit(ch))
2194 break;
2195 info->facil[i].parm =
2196 hex_to_uint(&str[4+4*i], 2);
2198 info->nfacil = i;
2199 break;
2204 /*============================================================================
2205 * Convert line speed in bps to a number used by S502 code.
2207 static unsigned char bps_to_speed_code (unsigned long bps)
2209 unsigned char number;
2211 if (bps <= 1200) number = 0x01 ;
2212 else if (bps <= 2400) number = 0x02;
2213 else if (bps <= 4800) number = 0x03;
2214 else if (bps <= 9600) number = 0x04;
2215 else if (bps <= 19200) number = 0x05;
2216 else if (bps <= 38400) number = 0x06;
2217 else if (bps <= 45000) number = 0x07;
2218 else if (bps <= 56000) number = 0x08;
2219 else if (bps <= 64000) number = 0x09;
2220 else if (bps <= 74000) number = 0x0A;
2221 else if (bps <= 112000) number = 0x0B;
2222 else if (bps <= 128000) number = 0x0C;
2223 else number = 0x0D;
2225 return number;
2228 /*============================================================================
2229 * Convert decimal string to unsigned integer.
2230 * If len != 0 then only 'len' characters of the string are converted.
2232 static unsigned int dec_to_uint (unsigned char* str, int len)
2234 unsigned val;
2236 if (!len) len = strlen(str);
2237 for (val = 0; len && is_digit(*str); ++str, --len)
2238 val = (val * 10) + (*str - (unsigned)'0');
2239 return val;
2242 /*============================================================================
2243 * Convert hex string to unsigned integer.
2244 * If len != 0 then only 'len' characters of the string are conferted.
2246 static unsigned int hex_to_uint (unsigned char* str, int len)
2248 unsigned val, ch;
2250 if (!len) len = strlen(str);
2251 for (val = 0; len; ++str, --len)
2253 ch = *str;
2254 if (is_digit(ch))
2255 val = (val << 4) + (ch - (unsigned)'0');
2256 else if (is_hex_digit(ch))
2257 val = (val << 4) + ((ch & 0xDF) - (unsigned)'A' + 10);
2258 else
2259 break;
2261 return val;
2265 static int handle_IPXWAN(unsigned char *sendpacket, char *devname, unsigned char enable_IPX, unsigned long network_number, unsigned short proto)
2267 int i;
2269 if( proto == htons(ETH_P_IPX) ) {
2270 /* It's an IPX packet */
2271 if(!enable_IPX) {
2272 /* Return 1 so we don't pass it up the stack. */
2273 return 1;
2275 } else {
2276 /* It's not IPX so pass it up the stack. */
2277 return 0;
2280 if( sendpacket[16] == 0x90 &&
2281 sendpacket[17] == 0x04)
2283 /* It's IPXWAN */
2285 if( sendpacket[2] == 0x02 &&
2286 sendpacket[34] == 0x00)
2288 /* It's a timer request packet */
2289 printk(KERN_INFO "%s: Received IPXWAN Timer Request packet\n",devname);
2291 /* Go through the routing options and answer no to every
2292 * option except Unnumbered RIP/SAP */
2293 for(i = 41; sendpacket[i] == 0x00; i += 5)
2295 /* 0x02 is the option for Unnumbered RIP/SAP */
2296 if( sendpacket[i + 4] != 0x02)
2297 sendpacket[i + 1] = 0;
2300 /* Skip over the extended Node ID option */
2301 if( sendpacket[i] == 0x04 )
2302 i += 8;
2304 /* We also want to turn off all header compression opt. */
2305 for(; sendpacket[i] == 0x80 ;)
2307 sendpacket[i + 1] = 0;
2308 i += (sendpacket[i + 2] << 8) + (sendpacket[i + 3]) + 4;
2311 /* Set the packet type to timer response */
2312 sendpacket[34] = 0x01;
2314 printk(KERN_INFO "%s: Sending IPXWAN Timer Response\n",devname);
2316 else if( sendpacket[34] == 0x02 )
2318 /* This is an information request packet */
2319 printk(KERN_INFO "%s: Received IPXWAN Information Request packet\n",devname);
2321 /* Set the packet type to information response */
2322 sendpacket[34] = 0x03;
2324 /* Set the router name */
2325 sendpacket[51] = 'X';
2326 sendpacket[52] = 'T';
2327 sendpacket[53] = 'P';
2328 sendpacket[54] = 'I';
2329 sendpacket[55] = 'P';
2330 sendpacket[56] = 'E';
2331 sendpacket[57] = '-';
2332 sendpacket[58] = CVHexToAscii(network_number >> 28);
2333 sendpacket[59] = CVHexToAscii((network_number & 0x0F000000)>> 24);
2334 sendpacket[60] = CVHexToAscii((network_number & 0x00F00000)>> 20);
2335 sendpacket[61] = CVHexToAscii((network_number & 0x000F0000)>> 16);
2336 sendpacket[62] = CVHexToAscii((network_number & 0x0000F000)>> 12);
2337 sendpacket[63] = CVHexToAscii((network_number & 0x00000F00)>> 8);
2338 sendpacket[64] = CVHexToAscii((network_number & 0x000000F0)>> 4);
2339 sendpacket[65] = CVHexToAscii(network_number & 0x0000000F);
2340 for(i = 66; i < 99; i+= 1)
2341 sendpacket[i] = 0;
2343 printk(KERN_INFO "%s: Sending IPXWAN Information Response packet\n",devname);
2345 else
2347 printk(KERN_INFO "%s: Unknown IPXWAN packet!\n",devname);
2348 return 0;
2351 /* Set the WNodeID to our network address */
2352 sendpacket[35] = (unsigned char)(network_number >> 24);
2353 sendpacket[36] = (unsigned char)((network_number & 0x00FF0000) >> 16);
2354 sendpacket[37] = (unsigned char)((network_number & 0x0000FF00) >> 8);
2355 sendpacket[38] = (unsigned char)(network_number & 0x000000FF);
2357 return 1;
2358 } else {
2359 /* If we get here its an IPX-data packet, so it'll get passed up the stack.
2360 switch the network numbers */
2361 switch_net_numbers(sendpacket, network_number, 1);
2362 return 0;
2367 If incoming is 0 (outgoing)- if the net numbers is ours make it 0
2368 if incoming is 1 - if the net number is 0 make it ours
2372 static void switch_net_numbers(unsigned char *sendpacket, unsigned long network_number, unsigned char incoming)
2374 unsigned long pnetwork_number;
2376 pnetwork_number = (unsigned long)((sendpacket[6] << 24) +
2377 (sendpacket[7] << 16) + (sendpacket[8] << 8) +
2378 sendpacket[9]);
2380 if (!incoming)
2382 /* If the destination network number is ours, make it 0 */
2383 if( pnetwork_number == network_number)
2385 sendpacket[6] = sendpacket[7] = sendpacket[8] =
2386 sendpacket[9] = 0x00;
2389 else
2391 /* If the incoming network is 0, make it ours */
2392 if( pnetwork_number == 0)
2394 sendpacket[6] = (unsigned char)(network_number >> 24);
2395 sendpacket[7] = (unsigned char)((network_number &
2396 0x00FF0000) >> 16);
2397 sendpacket[8] = (unsigned char)((network_number &
2398 0x0000FF00) >> 8);
2399 sendpacket[9] = (unsigned char)(network_number &
2400 0x000000FF);
2405 pnetwork_number = (unsigned long)((sendpacket[18] << 24) +
2406 (sendpacket[19] << 16) + (sendpacket[20] << 8) +
2407 sendpacket[21]);
2409 if( !incoming )
2411 /* If the source network is ours, make it 0 */
2412 if( pnetwork_number == network_number)
2414 sendpacket[18] = sendpacket[19] = sendpacket[20] =
2415 sendpacket[21] = 0x00;
2418 else
2420 /* If the source network is 0, make it ours */
2421 if( pnetwork_number == 0 )
2423 sendpacket[18] = (unsigned char)(network_number >> 24);
2424 sendpacket[19] = (unsigned char)((network_number &
2425 0x00FF0000) >> 16);
2426 sendpacket[20] = (unsigned char)((network_number &
2427 0x0000FF00) >> 8);
2428 sendpacket[21] = (unsigned char)(network_number &
2429 0x000000FF);
2432 } /* switch_net_numbers */
2435 /****** End *****************************************************************/