Merge branch 'upstream-linus' of git://oss.oracle.com/home/sourcebo/git/ocfs2
[sfrench/cifs-2.6.git] / drivers / isdn / hysdn / hysdn_sched.c
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
14 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/signal.h>
17 #include <linux/kernel.h>
18 #include <linux/ioport.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <asm/io.h>
22
23 #include "hysdn_defs.h"
24
25 /*****************************************************************************/
26 /* hysdn_sched_rx is called from the cards handler to announce new data is   */
27 /* available from the card. The routine has to handle the data and return    */
28 /* with a nonzero code if the data could be worked (or even thrown away), if */
29 /* no room to buffer the data is available a zero return tells the card      */
30 /* to keep the data until later.                                             */
31 /*****************************************************************************/
32 int
33 hysdn_sched_rx(hysdn_card *card, unsigned char *buf, unsigned short len,
34                         unsigned short chan)
35 {
36
37         switch (chan) {
38                 case CHAN_NDIS_DATA:
39                         if (hynet_enable & (1 << card->myid)) {
40                           /* give packet to network handler */
41                                 hysdn_rx_netpkt(card, buf, len);
42                         }
43                         break;
44
45                 case CHAN_ERRLOG:
46                         hysdn_card_errlog(card, (tErrLogEntry *) buf, len);
47                         if (card->err_log_state == ERRLOG_STATE_ON)
48                                 card->err_log_state = ERRLOG_STATE_START;       /* start new fetch */
49                         break;
50 #ifdef CONFIG_HYSDN_CAPI
51                 case CHAN_CAPI:
52 /* give packet to CAPI handler */
53                         if (hycapi_enable & (1 << card->myid)) {
54                                 hycapi_rx_capipkt(card, buf, len);
55                         }
56                         break;
57 #endif /* CONFIG_HYSDN_CAPI */
58                 default:
59                         printk(KERN_INFO "irq message channel %d len %d unhandled \n", chan, len);
60                         break;
61
62         }                       /* switch rx channel */
63
64         return (1);             /* always handled */
65 }                               /* hysdn_sched_rx */
66
67 /*****************************************************************************/
68 /* hysdn_sched_tx is called from the cards handler to announce that there is */
69 /* room in the tx-buffer to the card and data may be sent if needed.         */
70 /* If the routine wants to send data it must fill buf, len and chan with the */
71 /* appropriate data and return a nonzero value. With a zero return no new    */
72 /* data to send is assumed. maxlen specifies the buffer size available for   */
73 /* sending.                                                                  */
74 /*****************************************************************************/
75 int
76 hysdn_sched_tx(hysdn_card *card, unsigned char *buf,
77                 unsigned short volatile *len, unsigned short volatile *chan,
78                 unsigned short maxlen)
79 {
80         struct sk_buff *skb;
81
82         if (card->net_tx_busy) {
83                 card->net_tx_busy = 0;  /* reset flag */
84                 hysdn_tx_netack(card);  /* acknowledge packet send */
85         }                       /* a network packet has completely been transferred */
86         /* first of all async requests are handled */
87         if (card->async_busy) {
88                 if (card->async_len <= maxlen) {
89                         memcpy(buf, card->async_data, card->async_len);
90                         *len = card->async_len;
91                         *chan = card->async_channel;
92                         card->async_busy = 0;   /* reset request */
93                         return (1);
94                 }
95                 card->async_busy = 0;   /* in case of length error */
96         }                       /* async request */
97         if ((card->err_log_state == ERRLOG_STATE_START) &&
98             (maxlen >= ERRLOG_CMD_REQ_SIZE)) {
99                 strcpy(buf, ERRLOG_CMD_REQ);    /* copy the command */
100                 *len = ERRLOG_CMD_REQ_SIZE;     /* buffer length */
101                 *chan = CHAN_ERRLOG;    /* and channel */
102                 card->err_log_state = ERRLOG_STATE_ON;  /* new state is on */
103                 return (1);     /* tell that data should be send */
104         }                       /* error log start and able to send */
105         if ((card->err_log_state == ERRLOG_STATE_STOP) &&
106             (maxlen >= ERRLOG_CMD_STOP_SIZE)) {
107                 strcpy(buf, ERRLOG_CMD_STOP);   /* copy the command */
108                 *len = ERRLOG_CMD_STOP_SIZE;    /* buffer length */
109                 *chan = CHAN_ERRLOG;    /* and channel */
110                 card->err_log_state = ERRLOG_STATE_OFF;         /* new state is off */
111                 return (1);     /* tell that data should be send */
112         }                       /* error log start and able to send */
113         /* now handle network interface packets */
114         if ((hynet_enable & (1 << card->myid)) && 
115             (skb = hysdn_tx_netget(card)) != NULL) 
116         {
117                 if (skb->len <= maxlen) {
118                         memcpy(buf, skb->data, skb->len);       /* copy the packet to the buffer */
119                         *len = skb->len;
120                         *chan = CHAN_NDIS_DATA;
121                         card->net_tx_busy = 1;  /* we are busy sending network data */
122                         return (1);     /* go and send the data */
123                 } else
124                         hysdn_tx_netack(card);  /* aknowledge packet -> throw away */
125         }                       /* send a network packet if available */
126 #ifdef CONFIG_HYSDN_CAPI
127         if( ((hycapi_enable & (1 << card->myid))) && 
128             ((skb = hycapi_tx_capiget(card)) != NULL) )
129         {
130                 if (skb->len <= maxlen) {
131                         memcpy(buf, skb->data, skb->len);
132                         *len = skb->len;
133                         *chan = CHAN_CAPI;
134                         hycapi_tx_capiack(card);
135                         return (1);     /* go and send the data */
136                 }
137         }
138 #endif /* CONFIG_HYSDN_CAPI */
139         return (0);             /* nothing to send */
140 }                               /* hysdn_sched_tx */
141
142
143 /*****************************************************************************/
144 /* send one config line to the card and return 0 if successful, otherwise a */
145 /* negative error code.                                                      */
146 /* The function works with timeouts perhaps not giving the greatest speed    */
147 /* sending the line, but this should be meaningless beacuse only some lines  */
148 /* are to be sent and this happens very seldom.                              */
149 /*****************************************************************************/
150 int
151 hysdn_tx_cfgline(hysdn_card *card, unsigned char *line, unsigned short chan)
152 {
153         int cnt = 50;           /* timeout intervalls */
154         unsigned long flags;
155
156         if (card->debug_flags & LOG_SCHED_ASYN)
157                 hysdn_addlog(card, "async tx-cfg chan=%d len=%d", chan, strlen(line) + 1);
158
159         save_flags(flags);
160         cli();
161         while (card->async_busy) {
162                 sti();
163
164                 if (card->debug_flags & LOG_SCHED_ASYN)
165                         hysdn_addlog(card, "async tx-cfg delayed");
166
167                 msleep_interruptible(20);               /* Timeout 20ms */
168                 if (!--cnt) {
169                         restore_flags(flags);
170                         return (-ERR_ASYNC_TIME);       /* timed out */
171                 }
172                 cli();
173         }                       /* wait for buffer to become free */
174
175         strcpy(card->async_data, line);
176         card->async_len = strlen(line) + 1;
177         card->async_channel = chan;
178         card->async_busy = 1;   /* request transfer */
179
180         /* now queue the task */
181         schedule_work(&card->irq_queue);
182         sti();
183
184         if (card->debug_flags & LOG_SCHED_ASYN)
185                 hysdn_addlog(card, "async tx-cfg data queued");
186
187         cnt++;                  /* short delay */
188         cli();
189
190         while (card->async_busy) {
191                 sti();
192
193                 if (card->debug_flags & LOG_SCHED_ASYN)
194                         hysdn_addlog(card, "async tx-cfg waiting for tx-ready");
195
196                 msleep_interruptible(20);               /* Timeout 20ms */
197                 if (!--cnt) {
198                         restore_flags(flags);
199                         return (-ERR_ASYNC_TIME);       /* timed out */
200                 }
201                 cli();
202         }                       /* wait for buffer to become free again */
203
204         restore_flags(flags);
205
206         if (card->debug_flags & LOG_SCHED_ASYN)
207                 hysdn_addlog(card, "async tx-cfg data send");
208
209         return (0);             /* line send correctly */
210 }                               /* hysdn_tx_cfgline */