import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / drivers / isdn / hisax / hscx_irq.c
1 /* $Id: hscx_irq.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
2  *
3  * low level b-channel stuff for Siemens HSCX
4  *
5  * Author       Karsten Keil
6  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
7  * 
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  * This is an include file for fast inline IRQ stuff
12  *
13  */
14
15
16 static inline void
17 waitforCEC(struct IsdnCardState *cs, int hscx)
18 {
19         int to = 50;
20
21         while ((READHSCX(cs, hscx, HSCX_STAR) & 0x04) && to) {
22                 udelay(1);
23                 to--;
24         }
25         if (!to)
26                 printk(KERN_WARNING "HiSax: waitforCEC timeout\n");
27 }
28
29
30 static inline void
31 waitforXFW(struct IsdnCardState *cs, int hscx)
32 {
33         int to = 50;
34
35         while ((!(READHSCX(cs, hscx, HSCX_STAR) & 0x44) == 0x40) && to) {
36                 udelay(1);
37                 to--;
38         }
39         if (!to)
40                 printk(KERN_WARNING "HiSax: waitforXFW timeout\n");
41 }
42
43 static inline void
44 WriteHSCXCMDR(struct IsdnCardState *cs, int hscx, u_char data)
45 {
46         long flags;
47
48         save_flags(flags);
49         cli();
50         waitforCEC(cs, hscx);
51         WRITEHSCX(cs, hscx, HSCX_CMDR, data);
52         restore_flags(flags);
53 }
54
55
56
57 static void
58 hscx_empty_fifo(struct BCState *bcs, int count)
59 {
60         u_char *ptr;
61         struct IsdnCardState *cs = bcs->cs;
62         long flags;
63
64         if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
65                 debugl1(cs, "hscx_empty_fifo");
66
67         if (bcs->hw.hscx.rcvidx + count > HSCX_BUFMAX) {
68                 if (cs->debug & L1_DEB_WARN)
69                         debugl1(cs, "hscx_empty_fifo: incoming packet too large");
70                 WriteHSCXCMDR(cs, bcs->hw.hscx.hscx, 0x80);
71                 bcs->hw.hscx.rcvidx = 0;
72                 return;
73         }
74         ptr = bcs->hw.hscx.rcvbuf + bcs->hw.hscx.rcvidx;
75         bcs->hw.hscx.rcvidx += count;
76         save_flags(flags);
77         cli();
78         READHSCXFIFO(cs, bcs->hw.hscx.hscx, ptr, count);
79         WriteHSCXCMDR(cs, bcs->hw.hscx.hscx, 0x80);
80         restore_flags(flags);
81         if (cs->debug & L1_DEB_HSCX_FIFO) {
82                 char *t = bcs->blog;
83
84                 t += sprintf(t, "hscx_empty_fifo %c cnt %d",
85                              bcs->hw.hscx.hscx ? 'B' : 'A', count);
86                 QuickHex(t, ptr, count);
87                 debugl1(cs, bcs->blog);
88         }
89 }
90
91 static void
92 hscx_fill_fifo(struct BCState *bcs)
93 {
94         struct IsdnCardState *cs = bcs->cs;
95         int more, count;
96         int fifo_size = test_bit(HW_IPAC, &cs->HW_Flags)? 64: 32;
97         u_char *ptr;
98         long flags;
99
100
101         if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
102                 debugl1(cs, "hscx_fill_fifo");
103
104         if (!bcs->tx_skb)
105                 return;
106         if (bcs->tx_skb->len <= 0)
107                 return;
108
109         more = (bcs->mode == L1_MODE_TRANS) ? 1 : 0;
110         if (bcs->tx_skb->len > fifo_size) {
111                 more = !0;
112                 count = fifo_size;
113         } else
114                 count = bcs->tx_skb->len;
115
116         waitforXFW(cs, bcs->hw.hscx.hscx);
117         save_flags(flags);
118         cli();
119         ptr = bcs->tx_skb->data;
120         skb_pull(bcs->tx_skb, count);
121         bcs->tx_cnt -= count;
122         bcs->hw.hscx.count += count;
123         WRITEHSCXFIFO(cs, bcs->hw.hscx.hscx, ptr, count);
124         WriteHSCXCMDR(cs, bcs->hw.hscx.hscx, more ? 0x8 : 0xa);
125         restore_flags(flags);
126         if (cs->debug & L1_DEB_HSCX_FIFO) {
127                 char *t = bcs->blog;
128
129                 t += sprintf(t, "hscx_fill_fifo %c cnt %d",
130                              bcs->hw.hscx.hscx ? 'B' : 'A', count);
131                 QuickHex(t, ptr, count);
132                 debugl1(cs, bcs->blog);
133         }
134 }
135
136 static inline void
137 hscx_interrupt(struct IsdnCardState *cs, u_char val, u_char hscx)
138 {
139         u_char r;
140         struct BCState *bcs = cs->bcs + hscx;
141         struct sk_buff *skb;
142         int fifo_size = test_bit(HW_IPAC, &cs->HW_Flags)? 64: 32;
143         int count;
144
145         if (!test_bit(BC_FLG_INIT, &bcs->Flag))
146                 return;
147
148         if (val & 0x80) {       /* RME */
149                 r = READHSCX(cs, hscx, HSCX_RSTA);
150                 if ((r & 0xf0) != 0xa0) {
151                         if (!(r & 0x80)) {
152                                 if (cs->debug & L1_DEB_WARN)
153                                         debugl1(cs, "HSCX invalid frame");
154 #ifdef ERROR_STATISTIC
155                                 bcs->err_inv++;
156 #endif
157                         }
158                         if ((r & 0x40) && bcs->mode) {
159                                 if (cs->debug & L1_DEB_WARN)
160                                         debugl1(cs, "HSCX RDO mode=%d",
161                                                 bcs->mode);
162 #ifdef ERROR_STATISTIC
163                                 bcs->err_rdo++;
164 #endif
165                         }
166                         if (!(r & 0x20)) {
167                                 if (cs->debug & L1_DEB_WARN)
168                                         debugl1(cs, "HSCX CRC error");
169 #ifdef ERROR_STATISTIC
170                                 bcs->err_crc++;
171 #endif
172                         }
173                         WriteHSCXCMDR(cs, hscx, 0x80);
174                 } else {
175                         count = READHSCX(cs, hscx, HSCX_RBCL) & (
176                                 test_bit(HW_IPAC, &cs->HW_Flags)? 0x3f: 0x1f);
177                         if (count == 0)
178                                 count = fifo_size;
179                         hscx_empty_fifo(bcs, count);
180                         if ((count = bcs->hw.hscx.rcvidx - 1) > 0) {
181                                 if (cs->debug & L1_DEB_HSCX_FIFO)
182                                         debugl1(cs, "HX Frame %d", count);
183                                 if (!(skb = dev_alloc_skb(count)))
184                                         printk(KERN_WARNING "HSCX: receive out of memory\n");
185                                 else {
186                                         memcpy(skb_put(skb, count), bcs->hw.hscx.rcvbuf, count);
187                                         skb_queue_tail(&bcs->rqueue, skb);
188                                 }
189                         }
190                 }
191                 bcs->hw.hscx.rcvidx = 0;
192                 hscx_sched_event(bcs, B_RCVBUFREADY);
193         }
194         if (val & 0x40) {       /* RPF */
195                 hscx_empty_fifo(bcs, fifo_size);
196                 if (bcs->mode == L1_MODE_TRANS) {
197                         /* receive audio data */
198                         if (!(skb = dev_alloc_skb(fifo_size)))
199                                 printk(KERN_WARNING "HiSax: receive out of memory\n");
200                         else {
201                                 memcpy(skb_put(skb, fifo_size), bcs->hw.hscx.rcvbuf, fifo_size);
202                                 skb_queue_tail(&bcs->rqueue, skb);
203                         }
204                         bcs->hw.hscx.rcvidx = 0;
205                         hscx_sched_event(bcs, B_RCVBUFREADY);
206                 }
207         }
208         if (val & 0x10) {       /* XPR */
209                 if (bcs->tx_skb) {
210                         if (bcs->tx_skb->len) {
211                                 hscx_fill_fifo(bcs);
212                                 return;
213                         } else {
214                                 if (bcs->st->lli.l1writewakeup &&
215                                         (PACKET_NOACK != bcs->tx_skb->pkt_type))
216                                         bcs->st->lli.l1writewakeup(bcs->st, bcs->hw.hscx.count);
217                                 dev_kfree_skb_irq(bcs->tx_skb);
218                                 bcs->hw.hscx.count = 0; 
219                                 bcs->tx_skb = NULL;
220                         }
221                 }
222                 if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
223                         bcs->hw.hscx.count = 0;
224                         test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
225                         hscx_fill_fifo(bcs);
226                 } else {
227                         test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
228                         hscx_sched_event(bcs, B_XMTBUFREADY);
229                 }
230         }
231 }
232
233 static inline void
234 hscx_int_main(struct IsdnCardState *cs, u_char val)
235 {
236
237         u_char exval;
238         struct BCState *bcs;
239
240         if (val & 0x01) {
241                 bcs = cs->bcs + 1;
242                 exval = READHSCX(cs, 1, HSCX_EXIR);
243                 if (exval & 0x40) {
244                         if (bcs->mode == 1)
245                                 hscx_fill_fifo(bcs);
246                         else {
247 #ifdef ERROR_STATISTIC
248                                 bcs->err_tx++;
249 #endif
250                                 /* Here we lost an TX interrupt, so
251                                    * restart transmitting the whole frame.
252                                  */
253                                 if (bcs->tx_skb) {
254                                         skb_push(bcs->tx_skb, bcs->hw.hscx.count);
255                                         bcs->tx_cnt += bcs->hw.hscx.count;
256                                         bcs->hw.hscx.count = 0;
257                                 }
258                                 WriteHSCXCMDR(cs, bcs->hw.hscx.hscx, 0x01);
259                                 if (cs->debug & L1_DEB_WARN)
260                                         debugl1(cs, "HSCX B EXIR %x Lost TX", exval);
261                         }
262                 } else if (cs->debug & L1_DEB_HSCX)
263                         debugl1(cs, "HSCX B EXIR %x", exval);
264         }
265         if (val & 0xf8) {
266                 if (cs->debug & L1_DEB_HSCX)
267                         debugl1(cs, "HSCX B interrupt %x", val);
268                 hscx_interrupt(cs, val, 1);
269         }
270         if (val & 0x02) {
271                 bcs = cs->bcs;
272                 exval = READHSCX(cs, 0, HSCX_EXIR);
273                 if (exval & 0x40) {
274                         if (bcs->mode == L1_MODE_TRANS)
275                                 hscx_fill_fifo(bcs);
276                         else {
277                                 /* Here we lost an TX interrupt, so
278                                    * restart transmitting the whole frame.
279                                  */
280 #ifdef ERROR_STATISTIC
281                                 bcs->err_tx++;
282 #endif
283                                 if (bcs->tx_skb) {
284                                         skb_push(bcs->tx_skb, bcs->hw.hscx.count);
285                                         bcs->tx_cnt += bcs->hw.hscx.count;
286                                         bcs->hw.hscx.count = 0;
287                                 }
288                                 WriteHSCXCMDR(cs, bcs->hw.hscx.hscx, 0x01);
289                                 if (cs->debug & L1_DEB_WARN)
290                                         debugl1(cs, "HSCX A EXIR %x Lost TX", exval);
291                         }
292                 } else if (cs->debug & L1_DEB_HSCX)
293                         debugl1(cs, "HSCX A EXIR %x", exval);
294         }
295         if (val & 0x04) {
296                 exval = READHSCX(cs, 0, HSCX_ISTA);
297                 if (cs->debug & L1_DEB_HSCX)
298                         debugl1(cs, "HSCX A interrupt %x", exval);
299                 hscx_interrupt(cs, exval, 0);
300         }
301 }