command.c 9.2 KB

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