command.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. /*
  95. * Dispatch the command
  96. */
  97. switch(cmd->command) {
  98. case ISDN_CMD_IOCTL:
  99. {
  100. unsigned long cmdptr;
  101. scs_ioctl ioc;
  102. memcpy(&cmdptr, cmd->parm.num, sizeof(unsigned long));
  103. if (copy_from_user(&ioc, (scs_ioctl __user *)cmdptr,
  104. sizeof(scs_ioctl))) {
  105. pr_debug("%s: Failed to verify user space 0x%lx\n",
  106. sc_adapter[card]->devicename, cmdptr);
  107. 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. * start the onboard firmware
  134. */
  135. int startproc(int card)
  136. {
  137. int status;
  138. if(!IS_VALID_CARD(card)) {
  139. pr_debug("Invalid param: %d is not a valid card id\n", card);
  140. return -ENODEV;
  141. }
  142. /*
  143. * send start msg
  144. */
  145. status = sendmessage(card, CMPID,cmReqType2,
  146. cmReqClass0,
  147. cmReqStartProc,
  148. 0,0,NULL);
  149. pr_debug("%s: Sent startProc\n", sc_adapter[card]->devicename);
  150. return status;
  151. }
  152. /*
  153. * Dials the number passed in
  154. */
  155. static int dial(int card, unsigned long channel, setup_parm setup)
  156. {
  157. int status;
  158. char Phone[48];
  159. if(!IS_VALID_CARD(card)) {
  160. pr_debug("Invalid param: %d is not a valid card id\n", card);
  161. return -ENODEV;
  162. }
  163. /*extract ISDN number to dial from eaz/msn string*/
  164. strcpy(Phone,setup.phone);
  165. /*send the connection message*/
  166. status = sendmessage(card, CEPID,ceReqTypePhy,
  167. ceReqClass1,
  168. ceReqPhyConnect,
  169. (unsigned char) channel+1,
  170. strlen(Phone),
  171. (unsigned int *) Phone);
  172. pr_debug("%s: Dialing %s on channel %lu\n",
  173. sc_adapter[card]->devicename, Phone, channel+1);
  174. return status;
  175. }
  176. /*
  177. * Answer an incoming call
  178. */
  179. static int answer(int card, unsigned long channel)
  180. {
  181. if(!IS_VALID_CARD(card)) {
  182. pr_debug("Invalid param: %d is not a valid card id\n", card);
  183. return -ENODEV;
  184. }
  185. if(setup_buffers(card, channel+1, BUFFER_SIZE)) {
  186. hangup(card, channel+1);
  187. return -ENOBUFS;
  188. }
  189. indicate_status(card, ISDN_STAT_BCONN,channel,NULL);
  190. pr_debug("%s: Answered incoming call on channel %lu\n",
  191. sc_adapter[card]->devicename, channel+1);
  192. return 0;
  193. }
  194. /*
  195. * Hangup up the call on specified channel
  196. */
  197. static int hangup(int card, unsigned long channel)
  198. {
  199. int status;
  200. if(!IS_VALID_CARD(card)) {
  201. pr_debug("Invalid param: %d is not a valid card id\n", card);
  202. return -ENODEV;
  203. }
  204. status = sendmessage(card, CEPID, ceReqTypePhy,
  205. ceReqClass1,
  206. ceReqPhyDisconnect,
  207. (unsigned char) channel+1,
  208. 0,
  209. NULL);
  210. pr_debug("%s: Sent HANGUP message to channel %lu\n",
  211. sc_adapter[card]->devicename, channel+1);
  212. return status;
  213. }
  214. /*
  215. * Set the layer 2 protocol (X.25, HDLC, Raw)
  216. */
  217. static int setl2(int card, unsigned long arg)
  218. {
  219. int status =0;
  220. int protocol,channel;
  221. if(!IS_VALID_CARD(card)) {
  222. pr_debug("Invalid param: %d is not a valid card id\n", card);
  223. return -ENODEV;
  224. }
  225. protocol = arg >> 8;
  226. channel = arg & 0xff;
  227. sc_adapter[card]->channel[channel].l2_proto = protocol;
  228. /*
  229. * check that the adapter is also set to the correct protocol
  230. */
  231. pr_debug("%s: Sending GetFrameFormat for channel %d\n",
  232. sc_adapter[card]->devicename, channel+1);
  233. status = sendmessage(card, CEPID, ceReqTypeCall,
  234. ceReqClass0,
  235. ceReqCallGetFrameFormat,
  236. (unsigned char)channel+1,
  237. 1,
  238. (unsigned int *) protocol);
  239. if(status)
  240. return status;
  241. return 0;
  242. }
  243. /*
  244. * Set the layer 3 protocol
  245. */
  246. static int setl3(int card, unsigned long channel)
  247. {
  248. int protocol = channel >> 8;
  249. if(!IS_VALID_CARD(card)) {
  250. pr_debug("Invalid param: %d is not a valid card id\n", card);
  251. return -ENODEV;
  252. }
  253. sc_adapter[card]->channel[channel].l3_proto = protocol;
  254. return 0;
  255. }
  256. static int acceptb(int card, unsigned long channel)
  257. {
  258. if(!IS_VALID_CARD(card)) {
  259. pr_debug("Invalid param: %d is not a valid card id\n", card);
  260. return -ENODEV;
  261. }
  262. if(setup_buffers(card, channel+1, BUFFER_SIZE))
  263. {
  264. hangup(card, channel+1);
  265. return -ENOBUFS;
  266. }
  267. pr_debug("%s: B-Channel connection accepted on channel %lu\n",
  268. sc_adapter[card]->devicename, channel+1);
  269. indicate_status(card, ISDN_STAT_BCONN, channel, NULL);
  270. return 0;
  271. }
  272. static int clreaz(int card, unsigned long arg)
  273. {
  274. if(!IS_VALID_CARD(card)) {
  275. pr_debug("Invalid param: %d is not a valid card id\n", card);
  276. return -ENODEV;
  277. }
  278. strcpy(sc_adapter[card]->channel[arg].eazlist, "");
  279. sc_adapter[card]->channel[arg].eazclear = 1;
  280. pr_debug("%s: EAZ List cleared for channel %lu\n",
  281. sc_adapter[card]->devicename, arg+1);
  282. return 0;
  283. }
  284. static int seteaz(int card, unsigned long arg, char *num)
  285. {
  286. if(!IS_VALID_CARD(card)) {
  287. pr_debug("Invalid param: %d is not a valid card id\n", card);
  288. return -ENODEV;
  289. }
  290. strcpy(sc_adapter[card]->channel[arg].eazlist, num);
  291. sc_adapter[card]->channel[arg].eazclear = 0;
  292. pr_debug("%s: EAZ list for channel %lu set to: %s\n",
  293. sc_adapter[card]->devicename, arg+1,
  294. sc_adapter[card]->channel[arg].eazlist);
  295. return 0;
  296. }
  297. int reset(int card)
  298. {
  299. unsigned long flags;
  300. if(!IS_VALID_CARD(card)) {
  301. pr_debug("Invalid param: %d is not a valid card id\n", card);
  302. return -ENODEV;
  303. }
  304. indicate_status(card, ISDN_STAT_STOP, 0, NULL);
  305. if(sc_adapter[card]->EngineUp) {
  306. del_timer(&sc_adapter[card]->stat_timer);
  307. }
  308. sc_adapter[card]->EngineUp = 0;
  309. spin_lock_irqsave(&sc_adapter[card]->lock, flags);
  310. init_timer(&sc_adapter[card]->reset_timer);
  311. sc_adapter[card]->reset_timer.function = check_reset;
  312. sc_adapter[card]->reset_timer.data = card;
  313. sc_adapter[card]->reset_timer.expires = jiffies + CHECKRESET_TIME;
  314. add_timer(&sc_adapter[card]->reset_timer);
  315. spin_unlock_irqrestore(&sc_adapter[card]->lock, flags);
  316. outb(0x1,sc_adapter[card]->ioport[SFT_RESET]);
  317. pr_debug("%s: Adapter Reset\n", sc_adapter[card]->devicename);
  318. return 0;
  319. }
  320. void flushreadfifo (int card)
  321. {
  322. while(inb(sc_adapter[card]->ioport[FIFO_STATUS]) & RF_HAS_DATA)
  323. inb(sc_adapter[card]->ioport[FIFO_READ]);
  324. }