command.c 8.3 KB

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