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