1/* $Id: interrupt.c,v 1.1.1.1 2007/08/03 18:52:36 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#include "includes.h"
19#include "hardware.h"
20#include "message.h"
21#include "card.h"
22#include <linux/interrupt.h>
23
24static int get_card_from_irq(int irq)
25{
26	int i;
27
28	for(i = 0 ; i < cinst ; i++) {
29		if(sc_adapter[i]->interrupt == irq)
30			return i;
31	}
32	return -1;
33}
34
35/*
36 *
37 */
38irqreturn_t interrupt_handler(int interrupt, void *cardptr)
39{
40
41	RspMessage rcvmsg;
42	int channel;
43	int card;
44
45	card = get_card_from_irq(interrupt);
46
47	if(!IS_VALID_CARD(card)) {
48		pr_debug("Invalid param: %d is not a valid card id\n", card);
49		return IRQ_NONE;
50	}
51
52	pr_debug("%s: Entered Interrupt handler\n",
53			sc_adapter[card]->devicename);
54
55 	/*
56	 * Pull all of the waiting messages off the response queue
57	 */
58	while (!receivemessage(card, &rcvmsg)) {
59		/*
60		 * Push the message to the adapter structure for
61		 * send_and_receive to snoop
62		 */
63		if(sc_adapter[card]->want_async_messages)
64			memcpy(&(sc_adapter[card]->async_msg),
65					&rcvmsg, sizeof(RspMessage));
66
67		channel = (unsigned int) rcvmsg.phy_link_no;
68
69		/*
70		 * Trap Invalid request messages
71		 */
72		if(IS_CM_MESSAGE(rcvmsg, 0, 0, Invalid)) {
73			pr_debug("%s: Invalid request Message, rsp_status = %d\n",
74				sc_adapter[card]->devicename,
75				rcvmsg.rsp_status);
76			break;
77		}
78
79		/*
80		 * Check for a linkRead message
81		 */
82		if (IS_CE_MESSAGE(rcvmsg, Lnk, 1, Read))
83		{
84			pr_debug("%s: Received packet 0x%x bytes long at 0x%lx\n",
85						sc_adapter[card]->devicename,
86						rcvmsg.msg_data.response.msg_len,
87						rcvmsg.msg_data.response.buff_offset);
88			rcvpkt(card, &rcvmsg);
89			continue;
90
91		}
92
93		/*
94		 * Handle a write acknoledgement
95		 */
96		if(IS_CE_MESSAGE(rcvmsg, Lnk, 1, Write)) {
97			pr_debug("%s: Packet Send ACK on channel %d\n",
98				sc_adapter[card]->devicename,
99				rcvmsg.phy_link_no);
100			sc_adapter[card]->channel[rcvmsg.phy_link_no-1].free_sendbufs++;
101			continue;
102		}
103
104		/*
105		 * Handle a connection message
106		 */
107		if (IS_CE_MESSAGE(rcvmsg, Phy, 1, Connect))
108		{
109			unsigned int callid;
110			setup_parm setup;
111			pr_debug("%s: Connect message: line %d: status %d: cause 0x%x\n",
112						sc_adapter[card]->devicename,
113						rcvmsg.phy_link_no,
114						rcvmsg.rsp_status,
115						rcvmsg.msg_data.byte_array[2]);
116
117			memcpy(&callid,rcvmsg.msg_data.byte_array,sizeof(int));
118			if(callid>=0x8000 && callid<=0xFFFF)
119			{
120				pr_debug("%s: Got Dial-Out Rsp\n",
121					sc_adapter[card]->devicename);
122				indicate_status(card, ISDN_STAT_DCONN,
123						(unsigned long)rcvmsg.phy_link_no-1,NULL);
124
125			}
126			else if(callid>=0x0000 && callid<=0x7FFF)
127			{
128				pr_debug("%s: Got Incoming Call\n",
129						sc_adapter[card]->devicename);
130				strcpy(setup.phone,&(rcvmsg.msg_data.byte_array[4]));
131				strcpy(setup.eazmsn,
132					sc_adapter[card]->channel[rcvmsg.phy_link_no-1].dn);
133				setup.si1 = 7;
134				setup.si2 = 0;
135				setup.plan = 0;
136				setup.screen = 0;
137
138				indicate_status(card, ISDN_STAT_ICALL,(unsigned long)rcvmsg.phy_link_no-1,(char *)&setup);
139				indicate_status(card, ISDN_STAT_DCONN,(unsigned long)rcvmsg.phy_link_no-1,NULL);
140			}
141			continue;
142		}
143
144		/*
145		 * Handle a disconnection message
146		 */
147		if (IS_CE_MESSAGE(rcvmsg, Phy, 1, Disconnect))
148		{
149			pr_debug("%s: disconnect message: line %d: status %d: cause 0x%x\n",
150						sc_adapter[card]->devicename,
151						rcvmsg.phy_link_no,
152						rcvmsg.rsp_status,
153					 	rcvmsg.msg_data.byte_array[2]);
154
155			indicate_status(card, ISDN_STAT_BHUP,(unsigned long)rcvmsg.phy_link_no-1,NULL);
156			indicate_status(card, ISDN_STAT_DHUP,(unsigned long)rcvmsg.phy_link_no-1,NULL);
157			continue;
158
159		}
160
161		/*
162		 * Handle a startProc engine up message
163		 */
164		if (IS_CM_MESSAGE(rcvmsg, 5, 0, MiscEngineUp)) {
165			pr_debug("%s: Received EngineUp message\n",
166				sc_adapter[card]->devicename);
167			sc_adapter[card]->EngineUp = 1;
168			sendmessage(card, CEPID,ceReqTypeCall,ceReqClass0,ceReqCallGetMyNumber,1,0,NULL);
169			sendmessage(card, CEPID,ceReqTypeCall,ceReqClass0,ceReqCallGetMyNumber,2,0,NULL);
170			init_timer(&sc_adapter[card]->stat_timer);
171			sc_adapter[card]->stat_timer.function = check_phystat;
172			sc_adapter[card]->stat_timer.data = card;
173			sc_adapter[card]->stat_timer.expires = jiffies + CHECKSTAT_TIME;
174			add_timer(&sc_adapter[card]->stat_timer);
175			continue;
176		}
177
178		/*
179		 * Start proc response
180		 */
181		if (IS_CM_MESSAGE(rcvmsg, 2, 0, StartProc)) {
182			pr_debug("%s: StartProc Response Status %d\n",
183				sc_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(sc_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			sc_adapter[card]->nphystat = (b2stat >> 8) | b1stat; /* endian?? */
209			pr_debug("%s: PhyStat is 0x%2x\n",
210				sc_adapter[card]->devicename,
211				sc_adapter[card]->nphystat);
212			continue;
213		}
214
215
216		/*
217		 * Handle a GetFramFormat
218		 */
219		if(IS_CE_MESSAGE(rcvmsg, Call, 0, GetFrameFormat)) {
220			if(rcvmsg.msg_data.byte_array[0] != HDLC_PROTO) {
221				unsigned int proto = HDLC_PROTO;
222				/*
223				 * Set board format to HDLC if it wasn't already
224				 */
225				pr_debug("%s: current frame format: 0x%x, will change to HDLC\n",
226						sc_adapter[card]->devicename,
227					rcvmsg.msg_data.byte_array[0]);
228				sendmessage(card, CEPID, ceReqTypeCall,
229						ceReqClass0,
230						ceReqCallSetFrameFormat,
231						(unsigned char) channel +1,
232						1,&proto);
233				}
234			continue;
235		}
236
237		/*
238		 * Hmm...
239		 */
240		pr_debug("%s: Received unhandled message (%d,%d,%d) link %d\n",
241			sc_adapter[card]->devicename,
242			rcvmsg.type, rcvmsg.class, rcvmsg.code,
243			rcvmsg.phy_link_no);
244
245	}	/* while */
246
247	pr_debug("%s: Exiting Interrupt Handler\n",
248			sc_adapter[card]->devicename);
249	return IRQ_HANDLED;
250}
251