hysdn_sched.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id: hysdn_sched.c,v 1.5.6.4 2001/11/06 21:58:19 kai Exp $
  2. *
  3. * Linux driver for HYSDN cards
  4. * scheduler routines for handling exchange card <-> pc.
  5. *
  6. * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
  7. * Copyright 1999 by Werner Cornelius (werner@titro.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. */
  13. #include <linux/signal.h>
  14. #include <linux/kernel.h>
  15. #include <linux/ioport.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <asm/io.h>
  19. #include "hysdn_defs.h"
  20. /*****************************************************************************/
  21. /* hysdn_sched_rx is called from the cards handler to announce new data is */
  22. /* available from the card. The routine has to handle the data and return */
  23. /* with a nonzero code if the data could be worked (or even thrown away), if */
  24. /* no room to buffer the data is available a zero return tells the card */
  25. /* to keep the data until later. */
  26. /*****************************************************************************/
  27. int
  28. hysdn_sched_rx(hysdn_card *card, unsigned char *buf, unsigned short len,
  29. unsigned short chan)
  30. {
  31. switch (chan) {
  32. case CHAN_NDIS_DATA:
  33. if (hynet_enable & (1 << card->myid)) {
  34. /* give packet to network handler */
  35. hysdn_rx_netpkt(card, buf, len);
  36. }
  37. break;
  38. case CHAN_ERRLOG:
  39. hysdn_card_errlog(card, (tErrLogEntry *) buf, len);
  40. if (card->err_log_state == ERRLOG_STATE_ON)
  41. card->err_log_state = ERRLOG_STATE_START; /* start new fetch */
  42. break;
  43. #ifdef CONFIG_HYSDN_CAPI
  44. case CHAN_CAPI:
  45. /* give packet to CAPI handler */
  46. if (hycapi_enable & (1 << card->myid)) {
  47. hycapi_rx_capipkt(card, buf, len);
  48. }
  49. break;
  50. #endif /* CONFIG_HYSDN_CAPI */
  51. default:
  52. printk(KERN_INFO "irq message channel %d len %d unhandled \n", chan, len);
  53. break;
  54. } /* switch rx channel */
  55. return (1); /* always handled */
  56. } /* hysdn_sched_rx */
  57. /*****************************************************************************/
  58. /* hysdn_sched_tx is called from the cards handler to announce that there is */
  59. /* room in the tx-buffer to the card and data may be sent if needed. */
  60. /* If the routine wants to send data it must fill buf, len and chan with the */
  61. /* appropriate data and return a nonzero value. With a zero return no new */
  62. /* data to send is assumed. maxlen specifies the buffer size available for */
  63. /* sending. */
  64. /*****************************************************************************/
  65. int
  66. hysdn_sched_tx(hysdn_card *card, unsigned char *buf,
  67. unsigned short volatile *len, unsigned short volatile *chan,
  68. unsigned short maxlen)
  69. {
  70. struct sk_buff *skb;
  71. if (card->net_tx_busy) {
  72. card->net_tx_busy = 0; /* reset flag */
  73. hysdn_tx_netack(card); /* acknowledge packet send */
  74. } /* a network packet has completely been transferred */
  75. /* first of all async requests are handled */
  76. if (card->async_busy) {
  77. if (card->async_len <= maxlen) {
  78. memcpy(buf, card->async_data, card->async_len);
  79. *len = card->async_len;
  80. *chan = card->async_channel;
  81. card->async_busy = 0; /* reset request */
  82. return (1);
  83. }
  84. card->async_busy = 0; /* in case of length error */
  85. } /* async request */
  86. if ((card->err_log_state == ERRLOG_STATE_START) &&
  87. (maxlen >= ERRLOG_CMD_REQ_SIZE)) {
  88. strcpy(buf, ERRLOG_CMD_REQ); /* copy the command */
  89. *len = ERRLOG_CMD_REQ_SIZE; /* buffer length */
  90. *chan = CHAN_ERRLOG; /* and channel */
  91. card->err_log_state = ERRLOG_STATE_ON; /* new state is on */
  92. return (1); /* tell that data should be send */
  93. } /* error log start and able to send */
  94. if ((card->err_log_state == ERRLOG_STATE_STOP) &&
  95. (maxlen >= ERRLOG_CMD_STOP_SIZE)) {
  96. strcpy(buf, ERRLOG_CMD_STOP); /* copy the command */
  97. *len = ERRLOG_CMD_STOP_SIZE; /* buffer length */
  98. *chan = CHAN_ERRLOG; /* and channel */
  99. card->err_log_state = ERRLOG_STATE_OFF; /* new state is off */
  100. return (1); /* tell that data should be send */
  101. } /* error log start and able to send */
  102. /* now handle network interface packets */
  103. if ((hynet_enable & (1 << card->myid)) &&
  104. (skb = hysdn_tx_netget(card)) != NULL)
  105. {
  106. if (skb->len <= maxlen) {
  107. /* copy the packet to the buffer */
  108. skb_copy_from_linear_data(skb, buf, skb->len);
  109. *len = skb->len;
  110. *chan = CHAN_NDIS_DATA;
  111. card->net_tx_busy = 1; /* we are busy sending network data */
  112. return (1); /* go and send the data */
  113. } else
  114. hysdn_tx_netack(card); /* aknowledge packet -> throw away */
  115. } /* send a network packet if available */
  116. #ifdef CONFIG_HYSDN_CAPI
  117. if (((hycapi_enable & (1 << card->myid))) &&
  118. ((skb = hycapi_tx_capiget(card)) != NULL))
  119. {
  120. if (skb->len <= maxlen) {
  121. skb_copy_from_linear_data(skb, buf, skb->len);
  122. *len = skb->len;
  123. *chan = CHAN_CAPI;
  124. hycapi_tx_capiack(card);
  125. return (1); /* go and send the data */
  126. }
  127. }
  128. #endif /* CONFIG_HYSDN_CAPI */
  129. return (0); /* nothing to send */
  130. } /* hysdn_sched_tx */
  131. /*****************************************************************************/
  132. /* send one config line to the card and return 0 if successful, otherwise a */
  133. /* negative error code. */
  134. /* The function works with timeouts perhaps not giving the greatest speed */
  135. /* sending the line, but this should be meaningless because only some lines */
  136. /* are to be sent and this happens very seldom. */
  137. /*****************************************************************************/
  138. int
  139. hysdn_tx_cfgline(hysdn_card *card, unsigned char *line, unsigned short chan)
  140. {
  141. int cnt = 50; /* timeout intervalls */
  142. unsigned long flags;
  143. if (card->debug_flags & LOG_SCHED_ASYN)
  144. hysdn_addlog(card, "async tx-cfg chan=%d len=%d", chan, strlen(line) + 1);
  145. while (card->async_busy) {
  146. if (card->debug_flags & LOG_SCHED_ASYN)
  147. hysdn_addlog(card, "async tx-cfg delayed");
  148. msleep_interruptible(20); /* Timeout 20ms */
  149. if (!--cnt)
  150. return (-ERR_ASYNC_TIME); /* timed out */
  151. } /* wait for buffer to become free */
  152. spin_lock_irqsave(&card->hysdn_lock, flags);
  153. strcpy(card->async_data, line);
  154. card->async_len = strlen(line) + 1;
  155. card->async_channel = chan;
  156. card->async_busy = 1; /* request transfer */
  157. /* now queue the task */
  158. schedule_work(&card->irq_queue);
  159. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  160. if (card->debug_flags & LOG_SCHED_ASYN)
  161. hysdn_addlog(card, "async tx-cfg data queued");
  162. cnt++; /* short delay */
  163. while (card->async_busy) {
  164. if (card->debug_flags & LOG_SCHED_ASYN)
  165. hysdn_addlog(card, "async tx-cfg waiting for tx-ready");
  166. msleep_interruptible(20); /* Timeout 20ms */
  167. if (!--cnt)
  168. return (-ERR_ASYNC_TIME); /* timed out */
  169. } /* wait for buffer to become free again */
  170. if (card->debug_flags & LOG_SCHED_ASYN)
  171. hysdn_addlog(card, "async tx-cfg data send");
  172. return (0); /* line send correctly */
  173. } /* hysdn_tx_cfgline */