more changes on original files
[linux-2.4.git] / drivers / isdn / pcbit / callbacks.c
1 /*
2  * Callbacks for the FSM
3  *
4  * Copyright (C) 1996 Universidade de Lisboa
5  * 
6  * Written by Pedro Roque Marques (pedro_m@yahoo.com)
7  *
8  * This software may be used and distributed according to the terms of 
9  * the GNU General Public License, incorporated herein by reference.
10  */
11
12 /*
13  * Fix: 19981230 - Carlos Morgado <chbm@techie.com>
14  * Port of Nelson Escravana's <nelson.escravana@usa.net> fix to CalledPN 
15  * NULL pointer dereference in cb_in_1 (originally fixed in 2.0)
16  */
17
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
21
22 #include <linux/types.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
25 #include <linux/tqueue.h>
26 #include <linux/skbuff.h>
27
28 #include <asm/io.h>
29
30 #include <linux/isdnif.h>
31
32 #include "pcbit.h"
33 #include "layer2.h"
34 #include "edss1.h"
35 #include "callbacks.h"
36 #include "capi.h"
37
38 ushort last_ref_num = 1;
39
40 /*
41  *  send_conn_req
42  *
43  */
44
45 void cb_out_1(struct pcbit_dev * dev, struct pcbit_chan* chan, 
46               struct callb_data *cbdata) 
47 {
48         struct sk_buff *skb;
49         int len;
50         ushort refnum;
51
52
53 #ifdef DEBUG
54         printk(KERN_DEBUG "Called Party Number: %s\n", 
55                cbdata->data.setup.CalledPN);
56 #endif
57         /*
58          * hdr - kmalloc in capi_conn_req
59          *     - kfree   when msg has been sent
60          */
61
62         if ((len = capi_conn_req(cbdata->data.setup.CalledPN, &skb, 
63                                  chan->proto)) < 0)
64         {
65                 printk("capi_conn_req failed\n");
66                 return;
67         }
68
69
70         refnum = last_ref_num++ & 0x7fffU;
71
72         chan->callref = 0;
73         chan->layer2link = 0;
74         chan->snum = 0;
75         chan->s_refnum = refnum;
76
77         pcbit_l2_write(dev, MSG_CONN_REQ, refnum, skb, len);
78 }
79
80 /*
81  *  rcv CONNECT
82  *  will go into ACTIVE state
83  *  send CONN_ACTIVE_RESP
84  *  send Select protocol request 
85  */
86
87 void cb_out_2(struct pcbit_dev * dev, struct pcbit_chan* chan, 
88               struct callb_data *data) 
89 {
90         isdn_ctrl ictl;
91         struct sk_buff *skb;
92         int len;
93         ushort refnum;
94
95         if ((len=capi_conn_active_resp(chan, &skb)) < 0)
96         {
97                 printk("capi_conn_active_req failed\n");
98                 return;
99         }
100
101         refnum = last_ref_num++ & 0x7fffU;
102         chan->s_refnum = refnum;
103
104         pcbit_l2_write(dev, MSG_CONN_ACTV_RESP, refnum, skb, len);
105
106
107         ictl.command = ISDN_STAT_DCONN;
108         ictl.driver=dev->id;
109         ictl.arg=chan->id;
110         dev->dev_if->statcallb(&ictl);
111
112         /* ACTIVE D-channel */
113
114         /* Select protocol  */
115
116         if ((len=capi_select_proto_req(chan, &skb, 1 /*outgoing*/)) < 0) { 
117                 printk("capi_select_proto_req failed\n");
118                 return;
119         }
120
121         refnum = last_ref_num++ & 0x7fffU;
122         chan->s_refnum = refnum;
123
124         pcbit_l2_write(dev, MSG_SELP_REQ, refnum, skb, len);
125 }
126
127
128 /*
129  * Disconnect received (actually RELEASE COMPLETE) 
130  * This means we were not able to establish connection with remote
131  * Inform the big boss above
132  */
133 void cb_out_3(struct pcbit_dev * dev, struct pcbit_chan* chan,
134               struct callb_data *data) 
135 {
136         isdn_ctrl ictl;
137
138         ictl.command = ISDN_STAT_DHUP;
139         ictl.driver=dev->id;
140         ictl.arg=chan->id;
141         dev->dev_if->statcallb(&ictl);
142 }
143
144
145 /*
146  * Incoming call received
147  * inform user
148  */
149
150 void cb_in_1(struct pcbit_dev * dev, struct pcbit_chan* chan,
151              struct callb_data *cbdata) 
152 {
153         isdn_ctrl ictl;
154         unsigned short refnum;
155         struct sk_buff *skb;
156         int len;
157
158
159         ictl.command = ISDN_STAT_ICALL;
160         ictl.driver=dev->id;
161         ictl.arg=chan->id;
162         
163         /*
164          *  ictl.num >= strlen() + strlen() + 5
165          */
166
167         if (cbdata->data.setup.CallingPN == NULL) {
168                 printk(KERN_DEBUG "NULL CallingPN to phone; using 0\n");
169                 strcpy(ictl.parm.setup.phone, "0");
170         }
171         else {
172                 strcpy(ictl.parm.setup.phone, cbdata->data.setup.CallingPN);
173         }
174         if (cbdata->data.setup.CalledPN == NULL) {
175                 printk(KERN_DEBUG "NULL CalledPN to eazmsn; using 0\n");
176                 strcpy(ictl.parm.setup.eazmsn, "0");
177         }
178         else {
179                 strcpy(ictl.parm.setup.eazmsn, cbdata->data.setup.CalledPN);
180         }
181         ictl.parm.setup.si1 = 7;
182         ictl.parm.setup.si2 = 0;
183         ictl.parm.setup.plan = 0;
184         ictl.parm.setup.screen = 0;
185
186 #ifdef DEBUG
187         printk(KERN_DEBUG "statstr: %s\n", ictl.num);
188 #endif
189
190         dev->dev_if->statcallb(&ictl);
191
192         
193         if ((len=capi_conn_resp(chan, &skb)) < 0) {
194                 printk(KERN_DEBUG "capi_conn_resp failed\n");
195                 return;
196         }
197
198         refnum = last_ref_num++ & 0x7fffU;
199         chan->s_refnum = refnum;
200
201         pcbit_l2_write(dev, MSG_CONN_RESP, refnum, skb, len);
202 }
203
204 /*
205  * user has replied
206  * open the channel
207  * send CONNECT message CONNECT_ACTIVE_REQ in CAPI
208  */
209
210 void cb_in_2(struct pcbit_dev * dev, struct pcbit_chan* chan,
211              struct callb_data *data)
212 {
213         unsigned short refnum;
214         struct sk_buff *skb;
215         int len;
216         
217         if ((len = capi_conn_active_req(chan, &skb)) < 0) {        
218                 printk(KERN_DEBUG "capi_conn_active_req failed\n");
219                 return;
220         }
221
222
223         refnum = last_ref_num++ & 0x7fffU;
224         chan->s_refnum = refnum;
225
226         printk(KERN_DEBUG "sending MSG_CONN_ACTV_REQ\n");
227         pcbit_l2_write(dev, MSG_CONN_ACTV_REQ, refnum, skb, len);
228 }
229
230 /*
231  * CONN_ACK arrived
232  * start b-proto selection
233  *
234  */
235
236 void cb_in_3(struct pcbit_dev * dev, struct pcbit_chan* chan, 
237              struct callb_data *data)
238 {
239         unsigned short refnum;
240         struct sk_buff *skb;
241         int len;
242         
243         if ((len = capi_select_proto_req(chan, &skb, 0 /*incoming*/)) < 0)
244         {
245                 printk("capi_select_proto_req failed\n");
246                 return;
247         }
248
249         refnum = last_ref_num++ & 0x7fffU;
250         chan->s_refnum = refnum;
251
252         pcbit_l2_write(dev, MSG_SELP_REQ, refnum, skb, len);
253
254 }
255
256
257 /*
258  * Received disconnect ind on active state
259  * send disconnect resp
260  * send msg to user
261  */
262 void cb_disc_1(struct pcbit_dev * dev, struct pcbit_chan* chan, 
263                struct callb_data *data)
264 {
265         struct sk_buff *skb;
266         int len;
267         ushort refnum;
268         isdn_ctrl ictl;
269   
270         if ((len = capi_disc_resp(chan, &skb)) < 0) {
271                 printk("capi_disc_resp failed\n");
272                 return;
273         }
274
275         refnum = last_ref_num++ & 0x7fffU;
276         chan->s_refnum = refnum;
277
278         pcbit_l2_write(dev, MSG_DISC_RESP, refnum, skb, len);    
279
280         ictl.command = ISDN_STAT_BHUP;
281         ictl.driver=dev->id;
282         ictl.arg=chan->id;
283         dev->dev_if->statcallb(&ictl);
284 }
285
286         
287 /*
288  *  User HANGUP on active/call proceeding state
289  *  send disc.req
290  */
291 void cb_disc_2(struct pcbit_dev * dev, struct pcbit_chan* chan, 
292                struct callb_data *data)
293 {
294         struct sk_buff *skb;
295         int len;
296         ushort refnum;
297
298         if ((len = capi_disc_req(chan->callref, &skb, CAUSE_NORMAL)) < 0)
299         {
300                 printk("capi_disc_req failed\n");
301                 return;
302         }
303
304         refnum = last_ref_num++ & 0x7fffU;
305         chan->s_refnum = refnum;
306
307         pcbit_l2_write(dev, MSG_DISC_REQ, refnum, skb, len);  
308 }
309
310 /*
311  *  Disc confirm received send BHUP
312  *  Problem: when the HL driver sends the disc req itself
313  *           LL receives BHUP
314  */
315 void cb_disc_3(struct pcbit_dev * dev, struct pcbit_chan* chan, 
316                struct callb_data *data)
317 {
318         isdn_ctrl ictl;
319
320         ictl.command = ISDN_STAT_BHUP;
321         ictl.driver=dev->id;
322         ictl.arg=chan->id;
323         dev->dev_if->statcallb(&ictl);
324 }
325
326 void cb_notdone(struct pcbit_dev * dev, struct pcbit_chan* chan, 
327                 struct callb_data *data)
328 {
329 }
330
331 /*
332  * send activate b-chan protocol
333  */
334 void cb_selp_1(struct pcbit_dev * dev, struct pcbit_chan* chan, 
335                struct callb_data *data) 
336 {
337         struct sk_buff *skb;
338         int len;
339         ushort refnum;
340
341         if ((len = capi_activate_transp_req(chan, &skb)) < 0)
342         {
343                 printk("capi_conn_activate_transp_req failed\n");
344                 return;
345         }
346
347         refnum = last_ref_num++ & 0x7fffU;
348         chan->s_refnum = refnum;
349
350         pcbit_l2_write(dev, MSG_ACT_TRANSP_REQ, refnum, skb, len);
351 }
352
353 /*
354  *  Inform User that the B-channel is available
355  */
356 void cb_open(struct pcbit_dev * dev, struct pcbit_chan* chan, 
357              struct callb_data *data) 
358 {
359         isdn_ctrl ictl;
360
361         ictl.command = ISDN_STAT_BCONN;
362         ictl.driver=dev->id;
363         ictl.arg=chan->id;
364         dev->dev_if->statcallb(&ictl);
365 }
366
367
368