command.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* $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. #include <linux/module.h>
  18. #include "includes.h" /* This must be first */
  19. #include "hardware.h"
  20. #include "message.h"
  21. #include "card.h"
  22. #include "scioc.h"
  23. static int dial(int card, unsigned long channel, setup_parm setup);
  24. static int hangup(int card, unsigned long channel);
  25. static int answer(int card, unsigned long channel);
  26. static int clreaz(int card, unsigned long channel);
  27. static int seteaz(int card, unsigned long channel, char *);
  28. static int setl2(int card, unsigned long arg);
  29. static int setl3(int card, unsigned long arg);
  30. static int acceptb(int card, unsigned long channel);
  31. extern int cinst;
  32. extern board *sc_adapter[];
  33. extern int sc_ioctl(int, scs_ioctl *);
  34. extern int setup_buffers(int, int, unsigned int);
  35. extern int indicate_status(int, int,ulong,char*);
  36. extern void check_reset(unsigned long);
  37. extern int send_and_receive(int, unsigned int, unsigned char, unsigned char,
  38. unsigned char, unsigned char, unsigned char, unsigned char *,
  39. RspMessage *, int);
  40. extern int sendmessage(int, unsigned int, unsigned int, unsigned int,
  41. unsigned int, unsigned int, unsigned int, unsigned int *);
  42. extern inline void pullphone(char *, char *);
  43. #ifdef DEBUG
  44. /*
  45. * Translate command codes to strings
  46. */
  47. static char *commands[] = { "ISDN_CMD_IOCTL",
  48. "ISDN_CMD_DIAL",
  49. "ISDN_CMD_ACCEPTB",
  50. "ISDN_CMD_ACCEPTB",
  51. "ISDN_CMD_HANGUP",
  52. "ISDN_CMD_CLREAZ",
  53. "ISDN_CMD_SETEAZ",
  54. NULL,
  55. NULL,
  56. NULL,
  57. "ISDN_CMD_SETL2",
  58. NULL,
  59. "ISDN_CMD_SETL3",
  60. NULL,
  61. NULL,
  62. NULL,
  63. NULL,
  64. NULL, };
  65. /*
  66. * Translates ISDN4Linux protocol codes to strings for debug messages
  67. */
  68. static char *l3protos[] = { "ISDN_PROTO_L3_TRANS" };
  69. static char *l2protos[] = { "ISDN_PROTO_L2_X75I",
  70. "ISDN_PROTO_L2_X75UI",
  71. "ISDN_PROTO_L2_X75BUI",
  72. "ISDN_PROTO_L2_HDLC",
  73. "ISDN_PROTO_L2_TRANS" };
  74. #endif
  75. int get_card_from_id(int driver)
  76. {
  77. int i;
  78. for(i = 0 ; i < cinst ; i++) {
  79. if(sc_adapter[i]->driverId == driver)
  80. return i;
  81. }
  82. return -ENODEV;
  83. }
  84. /*
  85. * command
  86. */
  87. int command(isdn_ctrl *cmd)
  88. {
  89. int card;
  90. card = get_card_from_id(cmd->driver);
  91. if(!IS_VALID_CARD(card)) {
  92. pr_debug("Invalid param: %d is not a valid card id\n", card);
  93. return -ENODEV;
  94. }
  95. pr_debug("%s: Received %s command from Link Layer\n",
  96. sc_adapter[card]->devicename, commands[cmd->command]);
  97. /*
  98. * Dispatch the command
  99. */
  100. switch(cmd->command) {
  101. case ISDN_CMD_IOCTL:
  102. {
  103. unsigned long cmdptr;
  104. scs_ioctl ioc;
  105. memcpy(&cmdptr, cmd->parm.num, sizeof(unsigned long));
  106. if (copy_from_user(&ioc, (scs_ioctl __user *)cmdptr,
  107. sizeof(scs_ioctl))) {
  108. pr_debug("%s: Failed to verify user space 0x%x\n",
  109. sc_adapter[card]->devicename, cmdptr);
  110. return -EFAULT;
  111. }
  112. return sc_ioctl(card, &ioc);
  113. }
  114. case ISDN_CMD_DIAL:
  115. return dial(card, cmd->arg, cmd->parm.setup);
  116. case ISDN_CMD_HANGUP:
  117. return hangup(card, cmd->arg);
  118. case ISDN_CMD_ACCEPTD:
  119. return answer(card, cmd->arg);
  120. case ISDN_CMD_ACCEPTB:
  121. return acceptb(card, cmd->arg);
  122. case ISDN_CMD_CLREAZ:
  123. return clreaz(card, cmd->arg);
  124. case ISDN_CMD_SETEAZ:
  125. return seteaz(card, cmd->arg, cmd->parm.num);
  126. case ISDN_CMD_SETL2:
  127. return setl2(card, cmd->arg);
  128. case ISDN_CMD_SETL3:
  129. return setl3(card, cmd->arg);
  130. default:
  131. return -EINVAL;
  132. }
  133. return 0;
  134. }
  135. /*
  136. * start the onboard firmware
  137. */
  138. int startproc(int card)
  139. {
  140. int status;
  141. if(!IS_VALID_CARD(card)) {
  142. pr_debug("Invalid param: %d is not a valid card id\n", card);
  143. return -ENODEV;
  144. }
  145. /*
  146. * send start msg
  147. */
  148. status = sendmessage(card, CMPID,cmReqType2,
  149. cmReqClass0,
  150. cmReqStartProc,
  151. 0,0,NULL);
  152. pr_debug("%s: Sent startProc\n", sc_adapter[card]->devicename);
  153. return status;
  154. }
  155. /*
  156. * Dials the number passed in
  157. */
  158. static int dial(int card, unsigned long channel, setup_parm setup)
  159. {
  160. int status;
  161. char Phone[48];
  162. if(!IS_VALID_CARD(card)) {
  163. pr_debug("Invalid param: %d is not a valid card id\n", card);
  164. return -ENODEV;
  165. }
  166. /*extract ISDN number to dial from eaz/msn string*/
  167. strcpy(Phone,setup.phone);
  168. /*send the connection message*/
  169. status = sendmessage(card, CEPID,ceReqTypePhy,
  170. ceReqClass1,
  171. ceReqPhyConnect,
  172. (unsigned char) channel+1,
  173. strlen(Phone),
  174. (unsigned int *) Phone);
  175. pr_debug("%s: Dialing %s on channel %d\n",
  176. sc_adapter[card]->devicename, Phone, channel+1);
  177. return status;
  178. }
  179. /*
  180. * Answer an incoming call
  181. */
  182. static int answer(int card, unsigned long channel)
  183. {
  184. if(!IS_VALID_CARD(card)) {
  185. pr_debug("Invalid param: %d is not a valid card id\n", card);
  186. return -ENODEV;
  187. }
  188. if(setup_buffers(card, channel+1, BUFFER_SIZE)) {
  189. hangup(card, channel+1);
  190. return -ENOBUFS;
  191. }
  192. indicate_status(card, ISDN_STAT_BCONN,channel,NULL);
  193. pr_debug("%s: Answered incoming call on channel %s\n",
  194. sc_adapter[card]->devicename, channel+1);
  195. return 0;
  196. }
  197. /*
  198. * Hangup up the call on specified channel
  199. */
  200. static int hangup(int card, unsigned long channel)
  201. {
  202. int status;
  203. if(!IS_VALID_CARD(card)) {
  204. pr_debug("Invalid param: %d is not a valid card id\n", card);
  205. return -ENODEV;
  206. }
  207. status = sendmessage(card, CEPID, ceReqTypePhy,
  208. ceReqClass1,
  209. ceReqPhyDisconnect,
  210. (unsigned char) channel+1,
  211. 0,
  212. NULL);
  213. pr_debug("%s: Sent HANGUP message to channel %d\n",
  214. sc_adapter[card]->devicename, channel+1);
  215. return status;
  216. }
  217. /*
  218. * Set the layer 2 protocol (X.25, HDLC, Raw)
  219. */
  220. static int setl2(int card, unsigned long arg)
  221. {
  222. int status =0;
  223. int protocol,channel;
  224. if(!IS_VALID_CARD(card)) {
  225. pr_debug("Invalid param: %d is not a valid card id\n", card);
  226. return -ENODEV;
  227. }
  228. protocol = arg >> 8;
  229. channel = arg & 0xff;
  230. sc_adapter[card]->channel[channel].l2_proto = protocol;
  231. pr_debug("%s: Level 2 protocol for channel %d set to %s from %d\n",
  232. sc_adapter[card]->devicename, channel+1,
  233. l2protos[sc_adapter[card]->channel[channel].l2_proto],protocol);
  234. /*
  235. * check that the adapter is also set to the correct protocol
  236. */
  237. pr_debug("%s: Sending GetFrameFormat for channel %d\n",
  238. sc_adapter[card]->devicename, channel+1);
  239. status = sendmessage(card, CEPID, ceReqTypeCall,
  240. ceReqClass0,
  241. ceReqCallGetFrameFormat,
  242. (unsigned char)channel+1,
  243. 1,
  244. (unsigned int *) protocol);
  245. if(status)
  246. return status;
  247. return 0;
  248. }
  249. /*
  250. * Set the layer 3 protocol
  251. */
  252. static int setl3(int card, unsigned long channel)
  253. {
  254. int protocol = channel >> 8;
  255. if(!IS_VALID_CARD(card)) {
  256. pr_debug("Invalid param: %d is not a valid card id\n", card);
  257. return -ENODEV;
  258. }
  259. sc_adapter[card]->channel[channel].l3_proto = protocol;
  260. pr_debug("%s: Level 3 protocol for channel %d set to %s\n",
  261. sc_adapter[card]->devicename, channel+1, l3protos[protocol]);
  262. return 0;
  263. }
  264. static int acceptb(int card, unsigned long channel)
  265. {
  266. if(!IS_VALID_CARD(card)) {
  267. pr_debug("Invalid param: %d is not a valid card id\n", card);
  268. return -ENODEV;
  269. }
  270. if(setup_buffers(card, channel+1, BUFFER_SIZE))
  271. {
  272. hangup(card, channel+1);
  273. return -ENOBUFS;
  274. }
  275. pr_debug("%s: B-Channel connection accepted on channel %d\n",
  276. sc_adapter[card]->devicename, channel+1);
  277. indicate_status(card, ISDN_STAT_BCONN, channel, NULL);
  278. return 0;
  279. }
  280. static int clreaz(int card, unsigned long arg)
  281. {
  282. if(!IS_VALID_CARD(card)) {
  283. pr_debug("Invalid param: %d is not a valid card id\n", card);
  284. return -ENODEV;
  285. }
  286. strcpy(sc_adapter[card]->channel[arg].eazlist, "");
  287. sc_adapter[card]->channel[arg].eazclear = 1;
  288. pr_debug("%s: EAZ List cleared for channel %d\n",
  289. sc_adapter[card]->devicename, arg+1);
  290. return 0;
  291. }
  292. static int seteaz(int card, unsigned long arg, char *num)
  293. {
  294. if(!IS_VALID_CARD(card)) {
  295. pr_debug("Invalid param: %d is not a valid card id\n", card);
  296. return -ENODEV;
  297. }
  298. strcpy(sc_adapter[card]->channel[arg].eazlist, num);
  299. sc_adapter[card]->channel[arg].eazclear = 0;
  300. pr_debug("%s: EAZ list for channel %d set to: %s\n",
  301. sc_adapter[card]->devicename, arg+1,
  302. sc_adapter[card]->channel[arg].eazlist);
  303. return 0;
  304. }
  305. int reset(int card)
  306. {
  307. unsigned long flags;
  308. if(!IS_VALID_CARD(card)) {
  309. pr_debug("Invalid param: %d is not a valid card id\n", card);
  310. return -ENODEV;
  311. }
  312. indicate_status(card, ISDN_STAT_STOP, 0, NULL);
  313. if(sc_adapter[card]->EngineUp) {
  314. del_timer(&sc_adapter[card]->stat_timer);
  315. }
  316. sc_adapter[card]->EngineUp = 0;
  317. spin_lock_irqsave(&sc_adapter[card]->lock, flags);
  318. init_timer(&sc_adapter[card]->reset_timer);
  319. sc_adapter[card]->reset_timer.function = check_reset;
  320. sc_adapter[card]->reset_timer.data = card;
  321. sc_adapter[card]->reset_timer.expires = jiffies + CHECKRESET_TIME;
  322. add_timer(&sc_adapter[card]->reset_timer);
  323. spin_unlock_irqrestore(&sc_adapter[card]->lock, flags);
  324. outb(0x1,sc_adapter[card]->ioport[SFT_RESET]);
  325. pr_debug("%s: Adapter Reset\n", sc_adapter[card]->devicename);
  326. return 0;
  327. }
  328. void flushreadfifo (int card)
  329. {
  330. while(inb(sc_adapter[card]->ioport[FIFO_STATUS]) & RF_HAS_DATA)
  331. inb(sc_adapter[card]->ioport[FIFO_READ]);
  332. }