import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / drivers / isdn / sc / interrupt.c
1 /* $Id: interrupt.c,v 1.1.4.1 2001/11/20 14:19:37 kai Exp $
2  *
3  * Copyright (C) 1996  SpellCaster Telecommunications Inc.
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
8  * For more information, please contact gpl-info@spellcast.com or write:
9  *
10  *     SpellCaster Telecommunications Inc.
11  *     5621 Finch Avenue East, Unit #3
12  *     Scarborough, Ontario  Canada
13  *     M1B 2T9
14  *     +1 (416) 297-8565
15  *     +1 (416) 297-6433 Facsimile
16  */
17
18 #define __NO_VERSION__
19 #include "includes.h"
20 #include "hardware.h"
21 #include "message.h"
22 #include "card.h"
23
24 extern int indicate_status(int, int, ulong, char *);
25 extern void check_phystat(unsigned long);
26 extern int receivemessage(int, RspMessage *);
27 extern int sendmessage(int, unsigned int, unsigned int, unsigned int,
28         unsigned int, unsigned int, unsigned int, unsigned int *);
29 extern void rcvpkt(int, RspMessage *);
30
31 extern int cinst;
32 extern board *adapter[];
33
34 int get_card_from_irq(int irq)
35 {
36         int i;
37
38         for(i = 0 ; i < cinst ; i++) {
39                 if(adapter[i]->interrupt == irq)
40                         return i;
41         }
42         return -1;
43 }
44
45 /*
46  * 
47  */
48 void interrupt_handler(int interrupt, void * cardptr, struct pt_regs *regs ) {
49
50         RspMessage rcvmsg;
51         int channel;
52         int card;
53
54         card = get_card_from_irq(interrupt);
55
56         if(!IS_VALID_CARD(card)) {
57                 pr_debug("Invalid param: %d is not a valid card id\n", card);
58                 return;
59         }
60
61         pr_debug("%s: Entered Interrupt handler\n", adapter[card]->devicename);
62         
63         /*
64          * Pull all of the waiting messages off the response queue
65          */
66         while (!receivemessage(card, &rcvmsg)) {
67                 /*
68                  * Push the message to the adapter structure for
69                  * send_and_receive to snoop
70                  */
71                 if(adapter[card]->want_async_messages)
72                         memcpy(&(adapter[card]->async_msg), &rcvmsg, sizeof(RspMessage));
73
74                 channel = (unsigned int) rcvmsg.phy_link_no;
75                 
76                 /*
77                  * Trap Invalid request messages
78                  */
79                 if(IS_CM_MESSAGE(rcvmsg, 0, 0, Invalid)) {
80                         pr_debug("%s: Invalid request Message, rsp_status = %d\n", 
81                                 adapter[card]->devicename, rcvmsg.rsp_status);
82                         break;  
83                 }
84                 
85                 /*
86                  * Check for a linkRead message
87                  */
88                 if (IS_CE_MESSAGE(rcvmsg, Lnk, 1, Read))
89                 {
90                         pr_debug("%s: Received packet 0x%x bytes long at 0x%x\n",
91                                                 adapter[card]->devicename,
92                                                 rcvmsg.msg_data.response.msg_len,
93                                                 rcvmsg.msg_data.response.buff_offset);
94                         rcvpkt(card, &rcvmsg);
95                         continue;
96
97                 }
98
99                 /*
100                  * Handle a write acknoledgement
101                  */
102                 if(IS_CE_MESSAGE(rcvmsg, Lnk, 1, Write)) {
103                         pr_debug("%s: Packet Send ACK on channel %d\n", adapter[card]->devicename,
104                                 rcvmsg.phy_link_no);
105                         adapter[card]->channel[rcvmsg.phy_link_no-1].free_sendbufs++;
106                         continue;
107                 }
108
109                 /*
110                  * Handle a connection message
111                  */
112                 if (IS_CE_MESSAGE(rcvmsg, Phy, 1, Connect)) 
113                 {
114                         unsigned int callid;
115                         setup_parm setup;       
116                         pr_debug("%s: Connect message: line %d: status %d: cause 0x%x\n",
117                                                 adapter[card]->devicename,
118                                                 rcvmsg.phy_link_no,
119                                                 rcvmsg.rsp_status,
120                                                 rcvmsg.msg_data.byte_array[2]);
121                         
122                         memcpy(&callid,rcvmsg.msg_data.byte_array,sizeof(int));
123                         if(callid>=0x8000 && callid<=0xFFFF)
124                         {               
125                                 pr_debug("%s: Got Dial-Out Rsp\n", adapter[card]->devicename);  
126                                 indicate_status(card, ISDN_STAT_DCONN,
127                                                 (unsigned long)rcvmsg.phy_link_no-1,NULL);
128                                 
129                         }
130                         else if(callid>=0x0000 && callid<=0x7FFF)
131                         {
132                                 pr_debug("%s: Got Incoming Call\n", adapter[card]->devicename); 
133                                 strcpy(setup.phone,&(rcvmsg.msg_data.byte_array[4]));
134                                 strcpy(setup.eazmsn,adapter[card]->channel[rcvmsg.phy_link_no-1].dn);
135                                 setup.si1 = 7;
136                                 setup.si2 = 0;
137                                 setup.plan = 0;
138                                 setup.screen = 0;
139
140                                 indicate_status(card, ISDN_STAT_ICALL,(unsigned long)rcvmsg.phy_link_no-1,(char *)&setup);
141                                 indicate_status(card, ISDN_STAT_DCONN,(unsigned long)rcvmsg.phy_link_no-1,NULL);
142                         }
143                         continue;
144                 }
145
146                 /*
147                  * Handle a disconnection message
148                  */
149                 if (IS_CE_MESSAGE(rcvmsg, Phy, 1, Disconnect)) 
150                 {
151                         pr_debug("%s: disconnect message: line %d: status %d: cause 0x%x\n",
152                                                 adapter[card]->devicename,
153                                                 rcvmsg.phy_link_no,
154                                                 rcvmsg.rsp_status,
155                                                 rcvmsg.msg_data.byte_array[2]);
156
157                         indicate_status(card, ISDN_STAT_BHUP,(unsigned long)rcvmsg.phy_link_no-1,NULL);
158                         indicate_status(card, ISDN_STAT_DHUP,(unsigned long)rcvmsg.phy_link_no-1,NULL);
159                         continue;
160
161                 }
162
163                 /*
164                  * Handle a startProc engine up message
165                  */
166                 if (IS_CM_MESSAGE(rcvmsg, 5, 0, MiscEngineUp)) {
167                         pr_debug("%s: Received EngineUp message\n", adapter[card]->devicename);
168                         adapter[card]->EngineUp = 1;
169                         sendmessage(card, CEPID,ceReqTypeCall,ceReqClass0,ceReqCallGetMyNumber,1,0,NULL);
170                         sendmessage(card, CEPID,ceReqTypeCall,ceReqClass0,ceReqCallGetMyNumber,2,0,NULL);
171                         init_timer(&adapter[card]->stat_timer);
172                         adapter[card]->stat_timer.function = check_phystat;
173                         adapter[card]->stat_timer.data = card;
174                         adapter[card]->stat_timer.expires = jiffies + CHECKSTAT_TIME;
175                         add_timer(&adapter[card]->stat_timer);
176                         continue;
177                 }
178
179                 /*
180                  * Start proc response
181                  */
182                 if (IS_CM_MESSAGE(rcvmsg, 2, 0, StartProc)) {
183                         pr_debug("%s: StartProc Response Status %d\n", adapter[card]->devicename,
184                                 rcvmsg.rsp_status);
185                         continue;
186                 }
187
188                 /*
189                  * Handle a GetMyNumber Rsp
190                  */
191                 if (IS_CE_MESSAGE(rcvmsg,Call,0,GetMyNumber)){
192                         strcpy(adapter[card]->channel[rcvmsg.phy_link_no-1].dn,rcvmsg.msg_data.byte_array);
193                         continue;
194                 }
195                         
196                 /*
197                  * PhyStatus response
198                  */
199                 if(IS_CE_MESSAGE(rcvmsg, Phy, 2, Status)) {
200                         unsigned int b1stat, b2stat;
201
202                         /*
203                          * Covert the message data to the adapter->phystat code
204                          */
205                         b1stat = (unsigned int) rcvmsg.msg_data.byte_array[0];
206                         b2stat = (unsigned int) rcvmsg.msg_data.byte_array[1];
207
208                         adapter[card]->nphystat = (b2stat >> 8) | b1stat; /* endian?? */
209                         pr_debug("%s: PhyStat is 0x%2x\n", adapter[card]->devicename,
210                                 adapter[card]->nphystat);
211                         continue;
212                 }
213
214
215                 /* 
216                  * Handle a GetFramFormat
217                  */
218                 if(IS_CE_MESSAGE(rcvmsg, Call, 0, GetFrameFormat)) {
219                         if(rcvmsg.msg_data.byte_array[0] != HDLC_PROTO) {
220                                 unsigned int proto = HDLC_PROTO;
221                                 /*
222                                  * Set board format to HDLC if it wasn't already
223                                  */
224                                 pr_debug("%s: current frame format: 0x%x, will change to HDLC\n",
225                                                 adapter[card]->devicename,
226                                         rcvmsg.msg_data.byte_array[0]);
227                                 sendmessage(card, CEPID, ceReqTypeCall,
228                                                 ceReqClass0,
229                                                 ceReqCallSetFrameFormat,
230                                                 (unsigned char) channel +1,
231                                                 1,&proto);
232                                 }
233                         continue;
234                 }
235
236                 /*
237                  * Hmm...
238                  */
239                 pr_debug("%s: Received unhandled message (%d,%d,%d) link %d\n",
240                         adapter[card]->devicename, rcvmsg.type, rcvmsg.class, rcvmsg.code, 
241                         rcvmsg.phy_link_no);
242
243         }       /* while */
244
245         pr_debug("%s: Exiting Interrupt Handler\n", adapter[card]->devicename);
246 }