blob: 4a4e66152ce77e32669d3d7079d2d9826fb5331b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: command.c,v 1.4.10.1 2001/09/23 22:24:59 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#include <linux/module.h>
19#include "includes.h" /* This must be first */
20#include "hardware.h"
21#include "message.h"
22#include "card.h"
23#include "scioc.h"
24
Adrian Bunke3ca5e72005-06-25 14:58:34 -070025static int dial(int card, unsigned long channel, setup_parm setup);
26static int hangup(int card, unsigned long channel);
27static int answer(int card, unsigned long channel);
28static int clreaz(int card, unsigned long channel);
29static int seteaz(int card, unsigned long channel, char *);
30static int setl2(int card, unsigned long arg);
31static int setl3(int card, unsigned long arg);
32static int acceptb(int card, unsigned long channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#ifdef DEBUG
35/*
36 * Translate command codes to strings
37 */
38static char *commands[] = { "ISDN_CMD_IOCTL",
39 "ISDN_CMD_DIAL",
40 "ISDN_CMD_ACCEPTB",
41 "ISDN_CMD_ACCEPTB",
42 "ISDN_CMD_HANGUP",
43 "ISDN_CMD_CLREAZ",
44 "ISDN_CMD_SETEAZ",
45 NULL,
46 NULL,
47 NULL,
48 "ISDN_CMD_SETL2",
49 NULL,
50 "ISDN_CMD_SETL3",
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL, };
56
57/*
58 * Translates ISDN4Linux protocol codes to strings for debug messages
59 */
60static char *l3protos[] = { "ISDN_PROTO_L3_TRANS" };
61static char *l2protos[] = { "ISDN_PROTO_L2_X75I",
62 "ISDN_PROTO_L2_X75UI",
63 "ISDN_PROTO_L2_X75BUI",
64 "ISDN_PROTO_L2_HDLC",
65 "ISDN_PROTO_L2_TRANS" };
66#endif
67
68int get_card_from_id(int driver)
69{
70 int i;
71
Joe Perches475be4d2012-02-19 19:52:38 -080072 for (i = 0; i < cinst; i++) {
73 if (sc_adapter[i]->driverId == driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return i;
75 }
76 return -ENODEV;
77}
78
Joe Perches475be4d2012-02-19 19:52:38 -080079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * command
81 */
82
83int command(isdn_ctrl *cmd)
84{
85 int card;
86
87 card = get_card_from_id(cmd->driver);
Joe Perches475be4d2012-02-19 19:52:38 -080088 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 pr_debug("Invalid param: %d is not a valid card id\n", card);
90 return -ENODEV;
91 }
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /*
94 * Dispatch the command
95 */
Joe Perches475be4d2012-02-19 19:52:38 -080096 switch (cmd->command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 case ISDN_CMD_IOCTL:
98 {
Joe Perches475be4d2012-02-19 19:52:38 -080099 unsigned long cmdptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 scs_ioctl ioc;
101
102 memcpy(&cmdptr, cmd->parm.num, sizeof(unsigned long));
103 if (copy_from_user(&ioc, (scs_ioctl __user *)cmdptr,
104 sizeof(scs_ioctl))) {
Andrew Morton20f43532006-10-03 01:16:14 -0700105 pr_debug("%s: Failed to verify user space 0x%lx\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800106 sc_adapter[card]->devicename, cmdptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EFAULT;
108 }
109 return sc_ioctl(card, &ioc);
110 }
111 case ISDN_CMD_DIAL:
112 return dial(card, cmd->arg, cmd->parm.setup);
113 case ISDN_CMD_HANGUP:
114 return hangup(card, cmd->arg);
115 case ISDN_CMD_ACCEPTD:
116 return answer(card, cmd->arg);
117 case ISDN_CMD_ACCEPTB:
118 return acceptb(card, cmd->arg);
119 case ISDN_CMD_CLREAZ:
120 return clreaz(card, cmd->arg);
121 case ISDN_CMD_SETEAZ:
122 return seteaz(card, cmd->arg, cmd->parm.num);
123 case ISDN_CMD_SETL2:
124 return setl2(card, cmd->arg);
125 case ISDN_CMD_SETL3:
126 return setl3(card, cmd->arg);
127 default:
128 return -EINVAL;
129 }
130 return 0;
131}
132
133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * start the onboard firmware
135 */
Joe Perches475be4d2012-02-19 19:52:38 -0800136int startproc(int card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int status;
139
Joe Perches475be4d2012-02-19 19:52:38 -0800140 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 pr_debug("Invalid param: %d is not a valid card id\n", card);
142 return -ENODEV;
143 }
144
145 /*
Joe Perches475be4d2012-02-19 19:52:38 -0800146 * send start msg
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Joe Perches475be4d2012-02-19 19:52:38 -0800148 status = sendmessage(card, CMPID, cmReqType2,
149 cmReqClass0,
150 cmReqStartProc,
151 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 pr_debug("%s: Sent startProc\n", sc_adapter[card]->devicename);
Joe Perches475be4d2012-02-19 19:52:38 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return status;
155}
156
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
Joe Perches475be4d2012-02-19 19:52:38 -0800159 * Dials the number passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700161static int dial(int card, unsigned long channel, setup_parm setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 int status;
164 char Phone[48];
Joe Perches475be4d2012-02-19 19:52:38 -0800165
166 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pr_debug("Invalid param: %d is not a valid card id\n", card);
168 return -ENODEV;
169 }
170
Joe Perches475be4d2012-02-19 19:52:38 -0800171 /*extract ISDN number to dial from eaz/msn string*/
172 strcpy(Phone, setup.phone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*send the connection message*/
Joe Perches475be4d2012-02-19 19:52:38 -0800175 status = sendmessage(card, CEPID, ceReqTypePhy,
176 ceReqClass1,
177 ceReqPhyConnect,
178 (unsigned char)channel + 1,
179 strlen(Phone),
180 (unsigned int *)Phone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Andrew Morton20f43532006-10-03 01:16:14 -0700182 pr_debug("%s: Dialing %s on channel %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800183 sc_adapter[card]->devicename, Phone, channel + 1);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return status;
186}
187
188/*
Joe Perches475be4d2012-02-19 19:52:38 -0800189 * Answer an incoming call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700191static int answer(int card, unsigned long channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Joe Perches475be4d2012-02-19 19:52:38 -0800193 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 pr_debug("Invalid param: %d is not a valid card id\n", card);
195 return -ENODEV;
196 }
197
Joe Perches475be4d2012-02-19 19:52:38 -0800198 if (setup_buffers(card, channel + 1)) {
199 hangup(card, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -ENOBUFS;
201 }
202
Joe Perches475be4d2012-02-19 19:52:38 -0800203 indicate_status(card, ISDN_STAT_BCONN, channel, NULL);
Andrew Morton20f43532006-10-03 01:16:14 -0700204 pr_debug("%s: Answered incoming call on channel %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800205 sc_adapter[card]->devicename, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207}
208
209/*
210 * Hangup up the call on specified channel
211 */
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700212static int hangup(int card, unsigned long channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int status;
215
Joe Perches475be4d2012-02-19 19:52:38 -0800216 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 pr_debug("Invalid param: %d is not a valid card id\n", card);
218 return -ENODEV;
219 }
220
221 status = sendmessage(card, CEPID, ceReqTypePhy,
Joe Perches475be4d2012-02-19 19:52:38 -0800222 ceReqClass1,
223 ceReqPhyDisconnect,
224 (unsigned char)channel + 1,
225 0,
226 NULL);
Andrew Morton20f43532006-10-03 01:16:14 -0700227 pr_debug("%s: Sent HANGUP message to channel %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800228 sc_adapter[card]->devicename, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return status;
230}
231
232/*
233 * Set the layer 2 protocol (X.25, HDLC, Raw)
234 */
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700235static int setl2(int card, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Joe Perches475be4d2012-02-19 19:52:38 -0800237 int status = 0;
238 int protocol, channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Joe Perches475be4d2012-02-19 19:52:38 -0800240 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 pr_debug("Invalid param: %d is not a valid card id\n", card);
242 return -ENODEV;
243 }
244 protocol = arg >> 8;
245 channel = arg & 0xff;
246 sc_adapter[card]->channel[channel].l2_proto = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
249 * check that the adapter is also set to the correct protocol
250 */
251 pr_debug("%s: Sending GetFrameFormat for channel %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800252 sc_adapter[card]->devicename, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 status = sendmessage(card, CEPID, ceReqTypeCall,
Joe Perches475be4d2012-02-19 19:52:38 -0800254 ceReqClass0,
255 ceReqCallGetFrameFormat,
256 (unsigned char)channel + 1,
257 1,
258 (unsigned int *)protocol);
259 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return status;
261 return 0;
262}
263
264/*
265 * Set the layer 3 protocol
266 */
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700267static int setl3(int card, unsigned long channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 int protocol = channel >> 8;
270
Joe Perches475be4d2012-02-19 19:52:38 -0800271 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pr_debug("Invalid param: %d is not a valid card id\n", card);
273 return -ENODEV;
274 }
275
276 sc_adapter[card]->channel[channel].l3_proto = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278}
279
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700280static int acceptb(int card, unsigned long channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Joe Perches475be4d2012-02-19 19:52:38 -0800282 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pr_debug("Invalid param: %d is not a valid card id\n", card);
284 return -ENODEV;
285 }
286
Joe Perches475be4d2012-02-19 19:52:38 -0800287 if (setup_buffers(card, channel + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 {
Joe Perches475be4d2012-02-19 19:52:38 -0800289 hangup(card, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ENOBUFS;
291 }
292
Andrew Morton20f43532006-10-03 01:16:14 -0700293 pr_debug("%s: B-Channel connection accepted on channel %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800294 sc_adapter[card]->devicename, channel + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 indicate_status(card, ISDN_STAT_BCONN, channel, NULL);
296 return 0;
297}
298
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700299static int clreaz(int card, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Joe Perches475be4d2012-02-19 19:52:38 -0800301 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 pr_debug("Invalid param: %d is not a valid card id\n", card);
303 return -ENODEV;
304 }
305
306 strcpy(sc_adapter[card]->channel[arg].eazlist, "");
307 sc_adapter[card]->channel[arg].eazclear = 1;
Andrew Morton20f43532006-10-03 01:16:14 -0700308 pr_debug("%s: EAZ List cleared for channel %lu\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800309 sc_adapter[card]->devicename, arg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
Adrian Bunke3ca5e72005-06-25 14:58:34 -0700313static int seteaz(int card, unsigned long arg, char *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Joe Perches475be4d2012-02-19 19:52:38 -0800315 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 pr_debug("Invalid param: %d is not a valid card id\n", card);
317 return -ENODEV;
318 }
319
320 strcpy(sc_adapter[card]->channel[arg].eazlist, num);
321 sc_adapter[card]->channel[arg].eazclear = 0;
Andrew Morton20f43532006-10-03 01:16:14 -0700322 pr_debug("%s: EAZ list for channel %lu set to: %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800323 sc_adapter[card]->devicename, arg + 1,
324 sc_adapter[card]->channel[arg].eazlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
328int reset(int card)
329{
330 unsigned long flags;
331
Joe Perches475be4d2012-02-19 19:52:38 -0800332 if (!IS_VALID_CARD(card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 pr_debug("Invalid param: %d is not a valid card id\n", card);
334 return -ENODEV;
335 }
336
337 indicate_status(card, ISDN_STAT_STOP, 0, NULL);
338
Joe Perches475be4d2012-02-19 19:52:38 -0800339 if (sc_adapter[card]->EngineUp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 del_timer(&sc_adapter[card]->stat_timer);
341 }
342
343 sc_adapter[card]->EngineUp = 0;
344
345 spin_lock_irqsave(&sc_adapter[card]->lock, flags);
346 init_timer(&sc_adapter[card]->reset_timer);
Alexey Dobriyane3aded32007-07-19 01:49:10 -0700347 sc_adapter[card]->reset_timer.function = sc_check_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sc_adapter[card]->reset_timer.data = card;
349 sc_adapter[card]->reset_timer.expires = jiffies + CHECKRESET_TIME;
350 add_timer(&sc_adapter[card]->reset_timer);
351 spin_unlock_irqrestore(&sc_adapter[card]->lock, flags);
352
Joe Perches475be4d2012-02-19 19:52:38 -0800353 outb(0x1, sc_adapter[card]->ioport[SFT_RESET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 pr_debug("%s: Adapter Reset\n", sc_adapter[card]->devicename);
356 return 0;
357}
358
Joe Perches475be4d2012-02-19 19:52:38 -0800359void flushreadfifo(int card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Joe Perches475be4d2012-02-19 19:52:38 -0800361 while (inb(sc_adapter[card]->ioport[FIFO_STATUS]) & RF_HAS_DATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 inb(sc_adapter[card]->ioport[FIFO_READ]);
363}