1 /* $Id: message.c,v 1.5.8.2 2001/09/23 22:24:59 kai Exp $
3 * functions for sending and receiving control messages
5 * Copyright (C) 1996 SpellCaster Telecommunications Inc.
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
10 * For more information, please contact gpl-info@spellcast.com or write:
12 * SpellCaster Telecommunications Inc.
13 * 5621 Finch Avenue East, Unit #3
14 * Scarborough, Ontario Canada
17 * +1 (416) 297-6433 Facsimile
25 extern board *sc_adapter[];
26 extern unsigned int cinst;
29 * Obligatory function prototypes
31 extern int indicate_status(int,ulong,char*);
32 extern int scm_command(isdn_ctrl *);
33 extern void *memcpy_fromshmem(int, void *, const void *, size_t);
37 * receive a message from the board
39 int receivemessage(int card, RspMessage *rspmsg)
44 if (!IS_VALID_CARD(card)) {
45 pr_debug("Invalid param: %d is not a valid card id\n", card);
49 pr_debug("%s: Entered receivemessage\n",
50 sc_adapter[card]->devicename);
53 * See if there are messages waiting
55 if (inb(sc_adapter[card]->ioport[FIFO_STATUS]) & RF_HAS_DATA) {
57 * Map in the DPM to the base page and copy the message
59 spin_lock_irqsave(&sc_adapter[card]->lock, flags);
60 outb((sc_adapter[card]->shmem_magic >> 14) | 0x80,
61 sc_adapter[card]->ioport[sc_adapter[card]->shmem_pgport]);
62 dpm = (DualPortMemory *) sc_adapter[card]->rambase;
63 memcpy_fromio(rspmsg, &(dpm->rsp_queue[dpm->rsp_tail]),
65 dpm->rsp_tail = (dpm->rsp_tail+1) % MAX_MESSAGES;
66 inb(sc_adapter[card]->ioport[FIFO_READ]);
67 spin_unlock_irqrestore(&sc_adapter[card]->lock, flags);
69 * Tell the board that the message is received
71 pr_debug("%s: Received Message seq:%d pid:%d time:%d cmd:%d "
72 "cnt:%d (type,class,code):(%d,%d,%d) "
73 "link:%d stat:0x%x\n",
74 sc_adapter[card]->devicename,
78 rspmsg->cmd_sequence_no,
92 * send a message to the board
94 int sendmessage(int card,
100 unsigned int data_len,
107 if (!IS_VALID_CARD(card)) {
108 pr_debug("Invalid param: %d is not a valid card id\n", card);
113 * Make sure we only send CEPID messages when the engine is up
114 * and CMPID messages when it is down
116 if(sc_adapter[card]->EngineUp && procid == CMPID) {
117 pr_debug("%s: Attempt to send CM message with engine up\n",
118 sc_adapter[card]->devicename);
122 if(!sc_adapter[card]->EngineUp && procid == CEPID) {
123 pr_debug("%s: Attempt to send CE message with engine down\n",
124 sc_adapter[card]->devicename);
128 memset(&sndmsg, 0, MSG_LEN);
129 sndmsg.msg_byte_cnt = 4;
131 sndmsg.class = class;
133 sndmsg.phy_link_no = link;
136 if (data_len > MSG_DATA_LEN)
137 data_len = MSG_DATA_LEN;
138 memcpy(&(sndmsg.msg_data), data, data_len);
139 sndmsg.msg_byte_cnt = data_len + 8;
142 sndmsg.process_id = procid;
143 sndmsg.sequence_no = sc_adapter[card]->seq_no++ % 256;
146 * wait for an empty slot in the queue
148 while (!(inb(sc_adapter[card]->ioport[FIFO_STATUS]) & WF_NOT_FULL))
152 * Disable interrupts and map in shared memory
154 spin_lock_irqsave(&sc_adapter[card]->lock, flags);
155 outb((sc_adapter[card]->shmem_magic >> 14) | 0x80,
156 sc_adapter[card]->ioport[sc_adapter[card]->shmem_pgport]);
157 dpm = (DualPortMemory *) sc_adapter[card]->rambase; /* Fix me */
158 memcpy_toio(&(dpm->req_queue[dpm->req_head]),&sndmsg,MSG_LEN);
159 dpm->req_head = (dpm->req_head+1) % MAX_MESSAGES;
160 outb(sndmsg.sequence_no, sc_adapter[card]->ioport[FIFO_WRITE]);
161 spin_unlock_irqrestore(&sc_adapter[card]->lock, flags);
163 pr_debug("%s: Sent Message seq:%d pid:%d time:%d "
164 "cnt:%d (type,class,code):(%d,%d,%d) "
166 sc_adapter[card]->devicename,
179 int send_and_receive(int card,
185 unsigned char data_len,
187 RspMessage *mesgdata,
193 if (!IS_VALID_CARD(card)) {
194 pr_debug("Invalid param: %d is not a valid card id\n", card);
198 sc_adapter[card]->want_async_messages = 1;
199 retval = sendmessage(card, procid, type, class, code, link,
200 data_len, (unsigned int *) data);
203 pr_debug("%s: SendMessage failed in SAR\n",
204 sc_adapter[card]->devicename);
205 sc_adapter[card]->want_async_messages = 0;
210 /* wait for the response */
211 while (tries < timeout) {
212 set_current_state(TASK_INTERRUPTIBLE);
215 pr_debug("SAR waiting..\n");
218 * See if we got our message back
220 if ((sc_adapter[card]->async_msg.type == type) &&
221 (sc_adapter[card]->async_msg.class == class) &&
222 (sc_adapter[card]->async_msg.code == code) &&
223 (sc_adapter[card]->async_msg.phy_link_no == link)) {
228 pr_debug("%s: Got ASYNC message\n",
229 sc_adapter[card]->devicename);
230 memcpy(mesgdata, &(sc_adapter[card]->async_msg),
232 sc_adapter[card]->want_async_messages = 0;
239 pr_debug("%s: SAR message timeout\n", sc_adapter[card]->devicename);
240 sc_adapter[card]->want_async_messages = 0;