Revert last change. Bug noticed by Linus.
[linux-2.6/linux-mips.git] / drivers / isdn / isdn_x25iface.c
blob9a50748015c5ce13eabbc6eac8896703be96578f
1 /* $Id: isdn_x25iface.c,v 1.7 1999/08/22 20:26:13 calle Exp $
2 * stuff needed to support the Linux X.25 PLP code on top of devices that
3 * can provide a lab_b service using the concap_proto mechanism.
4 * This module supports a network interface wich provides lapb_sematics
5 * -- as defined in ../../Documentation/networking/x25-iface.txt -- to
6 * the upper layer and assumes that the lower layer provides a reliable
7 * data link service by means of the concap_device_ops callbacks.
9 * Only protocol specific stuff goes here. Device specific stuff
10 * goes to another -- device related -- concap_proto support source file.
12 * $Log: isdn_x25iface.c,v $
13 * Revision 1.7 1999/08/22 20:26:13 calle
14 * backported changes from kernel 2.3.14:
15 * - several #include "config.h" gone, others come.
16 * - "struct device" changed to "struct net_device" in 2.3.14, added a
17 * define in isdn_compat.h for older kernel versions.
19 * Revision 1.6 1999/01/27 22:53:19 he
20 * minor updates (spellings, jiffies wrap around in isdn_tty)
22 * Revision 1.5 1998/10/30 17:55:39 he
23 * dialmode for x25iface and multulink ppp
25 * Revision 1.4 1998/06/17 19:51:00 he
26 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
27 * brute force fix to avoid Ugh's in isdn_tty_write()
28 * cleaned up some dead code
30 * Revision 1.3 1998/02/20 17:25:20 fritz
31 * Changes for recent kernels.
33 * Revision 1.2 1998/01/31 22:49:22 keil
34 * correct comments
36 * Revision 1.1 1998/01/31 22:27:58 keil
37 * New files from Henner Eisen for X.25 support
41 /* #include <linux/isdn.h> */
42 #include <linux/netdevice.h>
43 #include <linux/concap.h>
44 #include <linux/wanrouter.h>
45 #include "isdn_x25iface.h"
47 /* for debugging messages not to cause an oops when device pointer is NULL*/
48 #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
51 typedef struct isdn_x25iface_proto_data {
52 int magic;
53 enum wan_states state;
54 /* Private stuff, not to be accessed via proto_data. We provide the
55 other storage for the concap_proto instance here as well,
56 enabling us to allocate both with just one kmalloc(): */
57 struct concap_proto priv;
58 } ix25_pdata_t;
62 /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
63 void isdn_x25iface_proto_del( struct concap_proto * );
64 int isdn_x25iface_proto_close( struct concap_proto * );
65 int isdn_x25iface_proto_restart( struct concap_proto *,
66 struct net_device *,
67 struct concap_device_ops *);
68 int isdn_x25iface_xmit( struct concap_proto *, struct sk_buff * );
69 int isdn_x25iface_receive( struct concap_proto *, struct sk_buff * );
70 int isdn_x25iface_connect_ind( struct concap_proto * );
71 int isdn_x25iface_disconn_ind( struct concap_proto * );
74 static struct concap_proto_ops ix25_pops = {
75 &isdn_x25iface_proto_new,
76 &isdn_x25iface_proto_del,
77 &isdn_x25iface_proto_restart,
78 &isdn_x25iface_proto_close,
79 &isdn_x25iface_xmit,
80 &isdn_x25iface_receive,
81 &isdn_x25iface_connect_ind,
82 &isdn_x25iface_disconn_ind
85 /* error message helper function */
86 static void illegal_state_warn( unsigned state, unsigned char firstbyte)
88 printk( KERN_WARNING "isdn_x25iface: firstbyte %x illegal in"
89 "current state %d\n",firstbyte, state );
92 /* check protocol data field for consistency */
93 static int pdata_is_bad( ix25_pdata_t * pda ){
95 if( pda && pda -> magic == ISDN_X25IFACE_MAGIC ) return 0;
96 printk( KERN_WARNING
97 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
98 return 1;
101 /* create a new x25 interface protocol instance
103 struct concap_proto * isdn_x25iface_proto_new()
105 ix25_pdata_t * tmp = kmalloc(sizeof(ix25_pdata_t),GFP_KERNEL);
106 IX25DEBUG("isdn_x25iface_proto_new\n");
107 if( tmp ){
108 tmp -> magic = ISDN_X25IFACE_MAGIC;
109 tmp -> state = WAN_UNCONFIGURED;
110 /* private data space used to hold the concap_proto data.
111 Only to be accessed via the returned pointer */
112 tmp -> priv.dops = NULL;
113 tmp -> priv.net_dev = NULL;
114 tmp -> priv.pops = &ix25_pops;
115 tmp -> priv.flags = 0;
116 tmp -> priv.proto_data = tmp;
117 return( &(tmp -> priv) );
119 return NULL;
122 /* close the x25iface encapsulation protocol
124 int isdn_x25iface_proto_close(struct concap_proto *cprot){
126 ix25_pdata_t *tmp;
127 int ret = 0;
128 ulong flags;
130 if( ! cprot ){
131 printk( KERN_ERR "isdn_x25iface_proto_close: "
132 "invalid concap_proto pointer\n" );
133 return -1;
135 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot -> net_dev) );
136 save_flags(flags);
137 cli(); /* avoid races with incoming events calling pops methods while
138 cprot members are inconsistent */
139 cprot -> dops = NULL;
140 cprot -> net_dev = NULL;
141 tmp = cprot -> proto_data;
142 if( pdata_is_bad( tmp ) ){
143 ret = -1;
144 } else {
145 tmp -> state = WAN_UNCONFIGURED;
147 restore_flags(flags);
149 return ret;
152 /* Delete the x25iface encapsulation protocol instance
154 void isdn_x25iface_proto_del(struct concap_proto *cprot){
156 ix25_pdata_t * tmp;
158 IX25DEBUG( "isdn_x25iface_proto_del \n" );
159 if( ! cprot ){
160 printk( KERN_ERR "isdn_x25iface_proto_del: "
161 "concap_proto pointer is NULL\n" );
162 return;
164 tmp = cprot -> proto_data;
165 if( tmp == NULL ){
166 printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
167 "proto_data pointer (maybe already deleted?)\n");
168 return;
170 /* close if the protocol is still open */
171 if( cprot -> dops ) isdn_x25iface_proto_close(cprot);
172 /* freeing the storage should be sufficient now. But some additional
173 settings might help to catch wild pointer bugs */
174 tmp -> magic = 0;
175 cprot -> proto_data = NULL;
177 kfree( tmp );
178 return;
181 /* (re-)initialize the data structures for x25iface encapsulation
183 int isdn_x25iface_proto_restart(struct concap_proto *cprot,
184 struct net_device *ndev,
185 struct concap_device_ops *dops)
187 ix25_pdata_t * pda = cprot -> proto_data ;
188 ulong flags;
190 IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev) );
192 if ( pdata_is_bad( pda ) ) return -1;
194 if( !( dops && dops -> data_req && dops -> connect_req
195 && dops -> disconn_req ) ){
196 printk( KERN_WARNING "isdn_x25iface_restart: required dops"
197 " missing\n" );
198 isdn_x25iface_proto_close(cprot);
199 return -1;
201 save_flags(flags);
202 cli(); /* avoid races with incoming events calling pops methods while
203 cprot members are inconsistent */
204 cprot -> net_dev = ndev;
205 cprot -> pops = &ix25_pops;
206 cprot -> dops = dops;
207 pda -> state = WAN_DISCONNECTED;
208 restore_flags(flags);
209 return 0;
212 /* deliver a dl_data frame received from i4l HL driver to the network layer
214 int isdn_x25iface_receive(struct concap_proto *cprot, struct sk_buff *skb)
216 IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot->net_dev) );
217 if ( ( (ix25_pdata_t*) (cprot->proto_data) )
218 -> state == WAN_CONNECTED ){
219 skb -> dev = cprot -> net_dev;
220 skb -> protocol = htons(ETH_P_X25);
221 skb -> pkt_type = PACKET_HOST;
222 if( skb_push(skb, 1)){
223 skb -> data[0]=0x00;
224 skb -> mac.raw = skb -> data;
225 netif_rx(skb);
226 return 0;
229 printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
230 dev_kfree_skb(skb);
231 return -1;
234 /* a connection set up is indicated by lower layer
236 int isdn_x25iface_connect_ind(struct concap_proto *cprot)
238 struct sk_buff * skb = dev_alloc_skb(1);
239 enum wan_states *state_p
240 = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
241 IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
242 , MY_DEVNAME(cprot->net_dev) );
243 if( *state_p == WAN_UNCONFIGURED ){
244 printk(KERN_WARNING
245 "isdn_x25iface_connect_ind while unconfigured %s\n"
246 , MY_DEVNAME(cprot->net_dev) );
247 return -1;
249 *state_p = WAN_CONNECTED;
250 if( skb ){
251 *( skb_put(skb, 1) ) = 0x01;
252 skb -> mac.raw = skb -> data;
253 skb -> dev = cprot -> net_dev;
254 skb -> protocol = htons(ETH_P_X25);
255 skb -> pkt_type = PACKET_HOST;
256 netif_rx(skb);
257 return 0;
258 } else {
259 printk(KERN_WARNING "isdn_x25iface_connect_ind: "
260 " out of memory -- disconnecting\n");
261 cprot -> dops -> disconn_req(cprot);
262 return -1;
266 /* a disconnect is indicated by lower layer
268 int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
270 struct sk_buff *skb;
271 enum wan_states *state_p
272 = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
273 IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot -> net_dev) );
274 if( *state_p == WAN_UNCONFIGURED ){
275 printk(KERN_WARNING
276 "isdn_x25iface_disconn_ind while unconfigured\n");
277 return -1;
279 if(! cprot -> net_dev) return -1;
280 *state_p = WAN_DISCONNECTED;
281 skb = dev_alloc_skb(1);
282 if( skb ){
283 *( skb_put(skb, 1) ) = 0x02;
284 skb -> mac.raw = skb -> data;
285 skb -> dev = cprot -> net_dev;
286 skb -> protocol = htons(ETH_P_X25);
287 skb -> pkt_type = PACKET_HOST;
288 netif_rx(skb);
289 return 0;
290 } else {
291 printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
292 " out of memory\n");
293 return -1;
297 /* process a frame handed over to us from linux network layer. First byte
298 semantics as defined in ../../Documentation/networking/x25-iface.txt
300 int isdn_x25iface_xmit(struct concap_proto *cprot, struct sk_buff *skb)
302 unsigned char firstbyte = skb->data[0];
303 unsigned *state =
304 &( ( (ix25_pdata_t*) (cprot -> proto_data) ) -> state );
305 int ret = 0;
306 IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot -> net_dev), firstbyte, *state );
307 switch ( firstbyte ){
308 case 0x00: /* dl_data request */
309 if( *state == WAN_CONNECTED ){
310 skb_pull(skb, 1);
311 cprot -> net_dev -> trans_start = jiffies;
312 ret = ( cprot -> dops -> data_req(cprot, skb) );
313 /* prepare for future retransmissions */
314 if( ret ) skb_push(skb,1);
315 return ret;
317 illegal_state_warn( *state, firstbyte );
318 break;
319 case 0x01: /* dl_connect request */
320 if( *state == WAN_DISCONNECTED ){
321 *state = WAN_CONNECTING;
322 ret = cprot -> dops -> connect_req(cprot);
323 if(ret){
324 /* reset state and notify upper layer about
325 * immidiatly failed attempts */
326 isdn_x25iface_disconn_ind(cprot);
328 } else {
329 illegal_state_warn( *state, firstbyte );
331 break;
332 case 0x02: /* dl_disconnect request */
333 switch ( *state ){
334 case WAN_DISCONNECTED:
335 /* Should not happen. However, give upper layer a
336 chance to recover from inconstistency but don't
337 trust the lower layer sending the disconn_confirm
338 when already disconnected */
339 printk(KERN_WARNING "isdn_x25iface_xmit: disconnect "
340 " requested while disconnected\n" );
341 isdn_x25iface_disconn_ind(cprot);
342 break; /* prevent infinite loops */
343 case WAN_CONNECTING:
344 case WAN_CONNECTED:
345 *state = WAN_DISCONNECTED;
346 cprot -> dops -> disconn_req(cprot);
347 break;
348 default:
349 illegal_state_warn( *state, firstbyte );
351 break;
352 case 0x03: /* changing lapb parameters requested */
353 printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
354 " options not yet supported\n");
355 break;
356 default:
357 printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
358 " first byte %x ignored:\n", firstbyte);
360 dev_kfree_skb(skb);
361 return 0;