blob: 60dc92562c6ddfd1bbe0c3f8b32048b3dcb572ec [file] [log] [blame]
Karsten Keil1700fe12008-07-26 18:55:28 +02001/*
2 *
3 * hfcpci.c low level driver for CCD's hfc-pci based cards
4 *
5 * Author Werner Cornelius (werner@isdn4linux.de)
6 * based on existing driver for CCD hfc ISA cards
7 * type approval valid for HFC-S PCI A based card
8 *
9 * Copyright 1999 by Werner Cornelius (werner@isdn-development.de)
10 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
Andreas Eversberg87c5fa12008-09-28 13:01:01 +020026 * Module options:
27 *
28 * debug:
29 * NOTE: only one poll value must be given for all cards
30 * See hfc_pci.h for debug flags.
31 *
32 * poll:
33 * NOTE: only one poll value must be given for all cards
34 * Give the number of samples for each fifo process.
35 * By default 128 is used. Decrease to reduce delay, increase to
36 * reduce cpu load. If unsure, don't mess with it!
37 * A value of 128 will use controller's interrupt. Other values will
38 * use kernel timer, because the controller will not allow lower values
39 * than 128.
40 * Also note that the value depends on the kernel timer frequency.
41 * If kernel uses a frequency of 1000 Hz, steps of 8 samples are possible.
42 * If the kernel uses 100 Hz, steps of 80 samples are possible.
43 * If the kernel uses 300 Hz, steps of about 26 samples are possible.
44 *
Karsten Keil1700fe12008-07-26 18:55:28 +020045 */
46
47#include <linux/module.h>
48#include <linux/pci.h>
49#include <linux/delay.h>
50#include <linux/mISDNhw.h>
51
52#include "hfc_pci.h"
53
54static const char *hfcpci_revision = "2.0";
55
Karsten Keil1700fe12008-07-26 18:55:28 +020056static int HFC_cnt;
57static uint debug;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +020058static uint poll, tics;
Hannes Eder6c2959a2009-02-12 09:28:40 +000059static struct timer_list hfc_tl;
Hannes Ederaa611f82009-02-14 13:10:33 +000060static unsigned long hfc_jiffies;
Karsten Keil1700fe12008-07-26 18:55:28 +020061
62MODULE_AUTHOR("Karsten Keil");
63MODULE_LICENSE("GPL");
Karsten Keil9785a8f82009-01-11 17:58:13 +010064module_param(debug, uint, S_IRUGO | S_IWUSR);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +020065module_param(poll, uint, S_IRUGO | S_IWUSR);
Karsten Keil1700fe12008-07-26 18:55:28 +020066
Karsten Keil1700fe12008-07-26 18:55:28 +020067enum {
68 HFC_CCD_2BD0,
69 HFC_CCD_B000,
70 HFC_CCD_B006,
71 HFC_CCD_B007,
72 HFC_CCD_B008,
73 HFC_CCD_B009,
74 HFC_CCD_B00A,
75 HFC_CCD_B00B,
76 HFC_CCD_B00C,
77 HFC_CCD_B100,
78 HFC_CCD_B700,
79 HFC_CCD_B701,
80 HFC_ASUS_0675,
81 HFC_BERKOM_A1T,
82 HFC_BERKOM_TCONCEPT,
83 HFC_ANIGMA_MC145575,
84 HFC_ZOLTRIX_2BD0,
85 HFC_DIGI_DF_M_IOM2_E,
86 HFC_DIGI_DF_M_E,
87 HFC_DIGI_DF_M_IOM2_A,
88 HFC_DIGI_DF_M_A,
89 HFC_ABOCOM_2BD1,
90 HFC_SITECOM_DC105V2,
91};
92
93struct hfcPCI_hw {
94 unsigned char cirm;
95 unsigned char ctmt;
96 unsigned char clkdel;
97 unsigned char states;
98 unsigned char conn;
99 unsigned char mst_m;
100 unsigned char int_m1;
101 unsigned char int_m2;
102 unsigned char sctrl;
103 unsigned char sctrl_r;
104 unsigned char sctrl_e;
105 unsigned char trm;
106 unsigned char fifo_en;
107 unsigned char bswapped;
108 unsigned char protocol;
109 int nt_timer;
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700110 unsigned char __iomem *pci_io; /* start of PCI IO memory */
Karsten Keil1700fe12008-07-26 18:55:28 +0200111 dma_addr_t dmahandle;
112 void *fifos; /* FIFO memory */
113 int last_bfifo_cnt[2];
114 /* marker saving last b-fifo frame count */
115 struct timer_list timer;
116};
117
118#define HFC_CFG_MASTER 1
119#define HFC_CFG_SLAVE 2
120#define HFC_CFG_PCM 3
121#define HFC_CFG_2HFC 4
122#define HFC_CFG_SLAVEHFC 5
123#define HFC_CFG_NEG_F0 6
124#define HFC_CFG_SW_DD_DU 7
125
126#define FLG_HFC_TIMER_T1 16
127#define FLG_HFC_TIMER_T3 17
128
129#define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */
130#define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */
131#define CLKDEL_TE 0x0e /* CLKDEL in TE mode */
132#define CLKDEL_NT 0x6c /* CLKDEL in NT mode */
133
134
135struct hfc_pci {
Karsten Keil1700fe12008-07-26 18:55:28 +0200136 u_char subtype;
137 u_char chanlimit;
138 u_char initdone;
139 u_long cfg;
140 u_int irq;
141 u_int irqcnt;
142 struct pci_dev *pdev;
143 struct hfcPCI_hw hw;
144 spinlock_t lock; /* card lock */
145 struct dchannel dch;
146 struct bchannel bch[2];
147};
148
149/* Interface functions */
150static void
151enable_hwirq(struct hfc_pci *hc)
152{
153 hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;
154 Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
155}
156
157static void
158disable_hwirq(struct hfc_pci *hc)
159{
160 hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);
161 Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
162}
163
164/*
165 * free hardware resources used by driver
166 */
167static void
168release_io_hfcpci(struct hfc_pci *hc)
169{
170 /* disable memory mapped ports + busmaster */
171 pci_write_config_word(hc->pdev, PCI_COMMAND, 0);
172 del_timer(&hc->hw.timer);
173 pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700174 iounmap(hc->hw.pci_io);
Karsten Keil1700fe12008-07-26 18:55:28 +0200175}
176
177/*
178 * set mode (NT or TE)
179 */
180static void
181hfcpci_setmode(struct hfc_pci *hc)
182{
183 if (hc->hw.protocol == ISDN_P_NT_S0) {
184 hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */
185 hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */
186 hc->hw.states = 1; /* G1 */
187 } else {
188 hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */
189 hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */
190 hc->hw.states = 2; /* F2 */
191 }
192 Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);
193 Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);
194 udelay(10);
195 Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */
196 Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
197}
198
199/*
200 * function called to reset the HFC PCI chip. A complete software reset of chip
201 * and fifos is done.
202 */
203static void
204reset_hfcpci(struct hfc_pci *hc)
205{
206 u_char val;
207 int cnt = 0;
208
209 printk(KERN_DEBUG "reset_hfcpci: entered\n");
210 val = Read_hfc(hc, HFCPCI_CHIP_ID);
211 printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);
212 /* enable memory mapped ports, disable busmaster */
213 pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
214 disable_hwirq(hc);
215 /* enable memory ports + busmaster */
216 pci_write_config_word(hc->pdev, PCI_COMMAND,
217 PCI_ENA_MEMIO + PCI_ENA_MASTER);
218 val = Read_hfc(hc, HFCPCI_STATUS);
219 printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);
220 hc->hw.cirm = HFCPCI_RESET; /* Reset On */
221 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
222 set_current_state(TASK_UNINTERRUPTIBLE);
223 mdelay(10); /* Timeout 10ms */
224 hc->hw.cirm = 0; /* Reset Off */
225 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
226 val = Read_hfc(hc, HFCPCI_STATUS);
227 printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);
228 while (cnt < 50000) { /* max 50000 us */
229 udelay(5);
230 cnt += 5;
231 val = Read_hfc(hc, HFCPCI_STATUS);
232 if (!(val & 2))
233 break;
234 }
235 printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);
236
237 hc->hw.fifo_en = 0x30; /* only D fifos enabled */
238
239 hc->hw.bswapped = 0; /* no exchange */
240 hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;
241 hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */
242 hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */
243 hc->hw.sctrl_r = 0;
244 hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */
245 hc->hw.mst_m = 0;
246 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
247 hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */
248 if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))
249 hc->hw.mst_m |= HFCPCI_F0_NEGATIV;
250 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
251 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
252 Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
253 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
254
255 hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |
256 HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;
257 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
258
259 /* Clear already pending ints */
260 if (Read_hfc(hc, HFCPCI_INT_S1));
261
262 /* set NT/TE mode */
263 hfcpci_setmode(hc);
264
265 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
266 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
267
268 /*
269 * Init GCI/IOM2 in master mode
270 * Slots 0 and 1 are set for B-chan 1 and 2
271 * D- and monitor/CI channel are not enabled
272 * STIO1 is used as output for data, B1+B2 from ST->IOM+HFC
273 * STIO2 is used as data input, B1+B2 from IOM->ST
274 * ST B-channel send disabled -> continous 1s
275 * The IOM slots are always enabled
276 */
277 if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
278 /* set data flow directions: connect B1,B2: HFC to/from PCM */
279 hc->hw.conn = 0x09;
280 } else {
281 hc->hw.conn = 0x36; /* set data flow directions */
282 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
283 Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);
284 Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);
285 Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);
286 Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);
287 } else {
288 Write_hfc(hc, HFCPCI_B1_SSL, 0x80);
289 Write_hfc(hc, HFCPCI_B2_SSL, 0x81);
290 Write_hfc(hc, HFCPCI_B1_RSL, 0x80);
291 Write_hfc(hc, HFCPCI_B2_RSL, 0x81);
292 }
293 }
294 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
295 val = Read_hfc(hc, HFCPCI_INT_S2);
296}
297
298/*
299 * Timer function called when kernel timer expires
300 */
301static void
302hfcpci_Timer(struct hfc_pci *hc)
303{
304 hc->hw.timer.expires = jiffies + 75;
305 /* WD RESET */
306/*
307 * WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);
308 * add_timer(&hc->hw.timer);
309 */
310}
311
312
313/*
314 * select a b-channel entry matching and active
315 */
316static struct bchannel *
317Sel_BCS(struct hfc_pci *hc, int channel)
318{
319 if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&
320 (hc->bch[0].nr & channel))
321 return &hc->bch[0];
322 else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&
323 (hc->bch[1].nr & channel))
324 return &hc->bch[1];
325 else
326 return NULL;
327}
328
329/*
330 * clear the desired B-channel rx fifo
331 */
332static void
333hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)
334{
335 u_char fifo_state;
336 struct bzfifo *bzr;
337
338 if (fifo) {
339 bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
340 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;
341 } else {
342 bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
343 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;
344 }
345 if (fifo_state)
346 hc->hw.fifo_en ^= fifo_state;
347 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
348 hc->hw.last_bfifo_cnt[fifo] = 0;
349 bzr->f1 = MAX_B_FRAMES;
350 bzr->f2 = bzr->f1; /* init F pointers to remain constant */
351 bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
352 bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(
353 le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));
354 if (fifo_state)
355 hc->hw.fifo_en |= fifo_state;
356 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
357}
358
359/*
360 * clear the desired B-channel tx fifo
361 */
362static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)
363{
364 u_char fifo_state;
365 struct bzfifo *bzt;
366
367 if (fifo) {
368 bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
369 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;
370 } else {
371 bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
372 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;
373 }
374 if (fifo_state)
375 hc->hw.fifo_en ^= fifo_state;
376 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
377 if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
378 printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "
379 "z1(%x) z2(%x) state(%x)\n",
380 fifo, bzt->f1, bzt->f2,
381 le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
382 le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),
383 fifo_state);
384 bzt->f2 = MAX_B_FRAMES;
385 bzt->f1 = bzt->f2; /* init F pointers to remain constant */
386 bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700387 bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 2);
Karsten Keil1700fe12008-07-26 18:55:28 +0200388 if (fifo_state)
389 hc->hw.fifo_en |= fifo_state;
390 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
391 if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
392 printk(KERN_DEBUG
393 "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",
394 fifo, bzt->f1, bzt->f2,
395 le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
396 le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));
397}
398
399/*
400 * read a complete B-frame out of the buffer
401 */
402static void
403hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,
404 u_char *bdata, int count)
405{
406 u_char *ptr, *ptr1, new_f2;
407 int total, maxlen, new_z2;
408 struct zt *zp;
409
410 if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
411 printk(KERN_DEBUG "hfcpci_empty_fifo\n");
412 zp = &bz->za[bz->f2]; /* point to Z-Regs */
413 new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */
414 if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
415 new_z2 -= B_FIFO_SIZE; /* buffer wrap */
416 new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;
417 if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||
418 (*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {
419 if (bch->debug & DEBUG_HW)
420 printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "
421 "invalid length %d or crc\n", count);
422#ifdef ERROR_STATISTIC
423 bch->err_inv++;
424#endif
425 bz->za[new_f2].z2 = cpu_to_le16(new_z2);
426 bz->f2 = new_f2; /* next buffer */
427 } else {
428 bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);
429 if (!bch->rx_skb) {
430 printk(KERN_WARNING "HFCPCI: receive out of memory\n");
431 return;
432 }
433 total = count;
434 count -= 3;
435 ptr = skb_put(bch->rx_skb, count);
436
437 if (le16_to_cpu(zp->z2) + count <= B_FIFO_SIZE + B_SUB_VAL)
438 maxlen = count; /* complete transfer */
439 else
440 maxlen = B_FIFO_SIZE + B_SUB_VAL -
441 le16_to_cpu(zp->z2); /* maximum */
442
443 ptr1 = bdata + (le16_to_cpu(zp->z2) - B_SUB_VAL);
444 /* start of data */
445 memcpy(ptr, ptr1, maxlen); /* copy data */
446 count -= maxlen;
447
448 if (count) { /* rest remaining */
449 ptr += maxlen;
450 ptr1 = bdata; /* start of buffer */
451 memcpy(ptr, ptr1, count); /* rest */
452 }
453 bz->za[new_f2].z2 = cpu_to_le16(new_z2);
454 bz->f2 = new_f2; /* next buffer */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000455 recv_Bchannel(bch, MISDN_ID_ANY);
Karsten Keil1700fe12008-07-26 18:55:28 +0200456 }
457}
458
459/*
460 * D-channel receive procedure
461 */
462static int
463receive_dmsg(struct hfc_pci *hc)
464{
465 struct dchannel *dch = &hc->dch;
466 int maxlen;
467 int rcnt, total;
468 int count = 5;
469 u_char *ptr, *ptr1;
470 struct dfifo *df;
471 struct zt *zp;
472
473 df = &((union fifo_area *)(hc->hw.fifos))->d_chan.d_rx;
474 while (((df->f1 & D_FREG_MASK) != (df->f2 & D_FREG_MASK)) && count--) {
475 zp = &df->za[df->f2 & D_FREG_MASK];
476 rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
477 if (rcnt < 0)
478 rcnt += D_FIFO_SIZE;
479 rcnt++;
480 if (dch->debug & DEBUG_HW_DCHANNEL)
481 printk(KERN_DEBUG
482 "hfcpci recd f1(%d) f2(%d) z1(%x) z2(%x) cnt(%d)\n",
483 df->f1, df->f2,
484 le16_to_cpu(zp->z1),
485 le16_to_cpu(zp->z2),
486 rcnt);
487
488 if ((rcnt > MAX_DFRAME_LEN + 3) || (rcnt < 4) ||
489 (df->data[le16_to_cpu(zp->z1)])) {
490 if (dch->debug & DEBUG_HW)
491 printk(KERN_DEBUG
492 "empty_fifo hfcpci paket inv. len "
493 "%d or crc %d\n",
494 rcnt,
495 df->data[le16_to_cpu(zp->z1)]);
496#ifdef ERROR_STATISTIC
497 cs->err_rx++;
498#endif
499 df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
500 (MAX_D_FRAMES + 1); /* next buffer */
501 df->za[df->f2 & D_FREG_MASK].z2 =
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700502 cpu_to_le16((le16_to_cpu(zp->z2) + rcnt) & (D_FIFO_SIZE - 1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200503 } else {
504 dch->rx_skb = mI_alloc_skb(rcnt - 3, GFP_ATOMIC);
505 if (!dch->rx_skb) {
506 printk(KERN_WARNING
507 "HFC-PCI: D receive out of memory\n");
508 break;
509 }
510 total = rcnt;
511 rcnt -= 3;
512 ptr = skb_put(dch->rx_skb, rcnt);
513
514 if (le16_to_cpu(zp->z2) + rcnt <= D_FIFO_SIZE)
515 maxlen = rcnt; /* complete transfer */
516 else
517 maxlen = D_FIFO_SIZE - le16_to_cpu(zp->z2);
518 /* maximum */
519
520 ptr1 = df->data + le16_to_cpu(zp->z2);
521 /* start of data */
522 memcpy(ptr, ptr1, maxlen); /* copy data */
523 rcnt -= maxlen;
524
525 if (rcnt) { /* rest remaining */
526 ptr += maxlen;
527 ptr1 = df->data; /* start of buffer */
528 memcpy(ptr, ptr1, rcnt); /* rest */
529 }
530 df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
531 (MAX_D_FRAMES + 1); /* next buffer */
532 df->za[df->f2 & D_FREG_MASK].z2 = cpu_to_le16((
533 le16_to_cpu(zp->z2) + total) & (D_FIFO_SIZE - 1));
534 recv_Dchannel(dch);
535 }
536 }
537 return 1;
538}
539
540/*
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200541 * check for transparent receive data and read max one 'poll' size if avail
Karsten Keil1700fe12008-07-26 18:55:28 +0200542 */
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200543static void
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000544hfcpci_empty_fifo_trans(struct bchannel *bch, struct bzfifo *rxbz,
545 struct bzfifo *txbz, u_char *bdata)
Karsten Keil1700fe12008-07-26 18:55:28 +0200546{
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000547 __le16 *z1r, *z2r, *z1t, *z2t;
548 int new_z2, fcnt_rx, fcnt_tx, maxlen;
549 u_char *ptr, *ptr1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200550
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000551 z1r = &rxbz->za[MAX_B_FRAMES].z1; /* pointer to z reg */
Karsten Keil1700fe12008-07-26 18:55:28 +0200552 z2r = z1r + 1;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000553 z1t = &txbz->za[MAX_B_FRAMES].z1;
554 z2t = z1t + 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200555
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000556 fcnt_rx = le16_to_cpu(*z1r) - le16_to_cpu(*z2r);
557 if (!fcnt_rx)
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200558 return; /* no data avail */
Karsten Keil1700fe12008-07-26 18:55:28 +0200559
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000560 if (fcnt_rx <= 0)
561 fcnt_rx += B_FIFO_SIZE; /* bytes actually buffered */
562 new_z2 = le16_to_cpu(*z2r) + fcnt_rx; /* new position in fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200563 if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
564 new_z2 -= B_FIFO_SIZE; /* buffer wrap */
565
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000566 if (fcnt_rx > MAX_DATA_SIZE) { /* flush, if oversized */
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200567 *z2r = cpu_to_le16(new_z2); /* new position */
568 return;
569 }
570
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000571 fcnt_tx = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
572 if (fcnt_tx <= 0)
573 fcnt_tx += B_FIFO_SIZE;
574 /* fcnt_tx contains available bytes in tx-fifo */
575 fcnt_tx = B_FIFO_SIZE - fcnt_tx;
576 /* remaining bytes to send (bytes in tx-fifo) */
577
578 bch->rx_skb = mI_alloc_skb(fcnt_rx, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +0200579 if (bch->rx_skb) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000580 ptr = skb_put(bch->rx_skb, fcnt_rx);
581 if (le16_to_cpu(*z2r) + fcnt_rx <= B_FIFO_SIZE + B_SUB_VAL)
582 maxlen = fcnt_rx; /* complete transfer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200583 else
584 maxlen = B_FIFO_SIZE + B_SUB_VAL - le16_to_cpu(*z2r);
585 /* maximum */
586
587 ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);
588 /* start of data */
589 memcpy(ptr, ptr1, maxlen); /* copy data */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000590 fcnt_rx -= maxlen;
Karsten Keil1700fe12008-07-26 18:55:28 +0200591
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000592 if (fcnt_rx) { /* rest remaining */
Karsten Keil1700fe12008-07-26 18:55:28 +0200593 ptr += maxlen;
594 ptr1 = bdata; /* start of buffer */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000595 memcpy(ptr, ptr1, fcnt_rx); /* rest */
Karsten Keil1700fe12008-07-26 18:55:28 +0200596 }
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000597 recv_Bchannel(bch, fcnt_tx); /* bch, id */
Karsten Keil1700fe12008-07-26 18:55:28 +0200598 } else
599 printk(KERN_WARNING "HFCPCI: receive out of memory\n");
600
601 *z2r = cpu_to_le16(new_z2); /* new position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200602}
603
604/*
605 * B-channel main receive routine
606 */
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700607static void
Karsten Keil1700fe12008-07-26 18:55:28 +0200608main_rec_hfcpci(struct bchannel *bch)
609{
610 struct hfc_pci *hc = bch->hw;
611 int rcnt, real_fifo;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200612 int receive = 0, count = 5;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000613 struct bzfifo *txbz, *rxbz;
Karsten Keil1700fe12008-07-26 18:55:28 +0200614 u_char *bdata;
615 struct zt *zp;
616
Karsten Keil1700fe12008-07-26 18:55:28 +0200617 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000618 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
619 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200620 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;
621 real_fifo = 1;
622 } else {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000623 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
624 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200625 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;
626 real_fifo = 0;
627 }
628Begin:
629 count--;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000630 if (rxbz->f1 != rxbz->f2) {
Karsten Keil1700fe12008-07-26 18:55:28 +0200631 if (bch->debug & DEBUG_HW_BCHANNEL)
632 printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000633 bch->nr, rxbz->f1, rxbz->f2);
634 zp = &rxbz->za[rxbz->f2];
Karsten Keil1700fe12008-07-26 18:55:28 +0200635
636 rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
637 if (rcnt < 0)
638 rcnt += B_FIFO_SIZE;
639 rcnt++;
640 if (bch->debug & DEBUG_HW_BCHANNEL)
641 printk(KERN_DEBUG
642 "hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",
643 bch->nr, le16_to_cpu(zp->z1),
644 le16_to_cpu(zp->z2), rcnt);
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000645 hfcpci_empty_bfifo(bch, rxbz, bdata, rcnt);
646 rcnt = rxbz->f1 - rxbz->f2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200647 if (rcnt < 0)
648 rcnt += MAX_B_FRAMES + 1;
649 if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {
650 rcnt = 0;
651 hfcpci_clear_fifo_rx(hc, real_fifo);
652 }
653 hc->hw.last_bfifo_cnt[real_fifo] = rcnt;
654 if (rcnt > 1)
655 receive = 1;
656 else
657 receive = 0;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200658 } else if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000659 hfcpci_empty_fifo_trans(bch, rxbz, txbz, bdata);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200660 return;
661 } else
Karsten Keil1700fe12008-07-26 18:55:28 +0200662 receive = 0;
663 if (count && receive)
664 goto Begin;
665
666}
667
668/*
669 * D-channel send routine
670 */
671static void
672hfcpci_fill_dfifo(struct hfc_pci *hc)
673{
674 struct dchannel *dch = &hc->dch;
675 int fcnt;
676 int count, new_z1, maxlen;
677 struct dfifo *df;
678 u_char *src, *dst, new_f1;
679
680 if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))
681 printk(KERN_DEBUG "%s\n", __func__);
682
683 if (!dch->tx_skb)
684 return;
685 count = dch->tx_skb->len - dch->tx_idx;
686 if (count <= 0)
687 return;
688 df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;
689
690 if (dch->debug & DEBUG_HW_DFIFO)
691 printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,
692 df->f1, df->f2,
693 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));
694 fcnt = df->f1 - df->f2; /* frame count actually buffered */
695 if (fcnt < 0)
696 fcnt += (MAX_D_FRAMES + 1); /* if wrap around */
697 if (fcnt > (MAX_D_FRAMES - 1)) {
698 if (dch->debug & DEBUG_HW_DCHANNEL)
699 printk(KERN_DEBUG
700 "hfcpci_fill_Dfifo more as 14 frames\n");
701#ifdef ERROR_STATISTIC
702 cs->err_tx++;
703#endif
704 return;
705 }
706 /* now determine free bytes in FIFO buffer */
707 maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -
708 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;
709 if (maxlen <= 0)
710 maxlen += D_FIFO_SIZE; /* count now contains available bytes */
711
712 if (dch->debug & DEBUG_HW_DCHANNEL)
713 printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",
714 count, maxlen);
715 if (count > maxlen) {
716 if (dch->debug & DEBUG_HW_DCHANNEL)
717 printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");
718 return;
719 }
720 new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &
721 (D_FIFO_SIZE - 1);
722 new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);
723 src = dch->tx_skb->data + dch->tx_idx; /* source pointer */
724 dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
725 maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
726 /* end fifo */
727 if (maxlen > count)
728 maxlen = count; /* limit size */
729 memcpy(dst, src, maxlen); /* first copy */
730
731 count -= maxlen; /* remaining bytes */
732 if (count) {
733 dst = df->data; /* start of buffer */
734 src += maxlen; /* new position */
735 memcpy(dst, src, count);
736 }
737 df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
738 /* for next buffer */
739 df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
740 /* new pos actual buffer */
741 df->f1 = new_f1; /* next frame */
742 dch->tx_idx = dch->tx_skb->len;
743}
744
745/*
746 * B-channel send routine
747 */
748static void
749hfcpci_fill_fifo(struct bchannel *bch)
750{
751 struct hfc_pci *hc = bch->hw;
752 int maxlen, fcnt;
753 int count, new_z1;
754 struct bzfifo *bz;
755 u_char *bdata;
756 u_char new_f1, *src, *dst;
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700757 __le16 *z1t, *z2t;
Karsten Keil1700fe12008-07-26 18:55:28 +0200758
759 if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
760 printk(KERN_DEBUG "%s\n", __func__);
761 if ((!bch->tx_skb) || bch->tx_skb->len <= 0)
762 return;
763 count = bch->tx_skb->len - bch->tx_idx;
764 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
765 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
766 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;
767 } else {
768 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
769 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;
770 }
771
772 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
773 z1t = &bz->za[MAX_B_FRAMES].z1;
774 z2t = z1t + 1;
775 if (bch->debug & DEBUG_HW_BCHANNEL)
776 printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "
777 "cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,
778 le16_to_cpu(*z1t), le16_to_cpu(*z2t));
779 fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
780 if (fcnt <= 0)
781 fcnt += B_FIFO_SIZE;
782 /* fcnt contains available bytes in fifo */
783 fcnt = B_FIFO_SIZE - fcnt;
784 /* remaining bytes to send (bytes in fifo) */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200785
786 /* "fill fifo if empty" feature */
787 if (test_bit(FLG_FILLEMPTY, &bch->Flags) && !fcnt) {
788 /* printk(KERN_DEBUG "%s: buffer empty, so we have "
789 "underrun\n", __func__); */
790 /* fill buffer, to prevent future underrun */
791 count = HFCPCI_FILLEMPTY;
792 new_z1 = le16_to_cpu(*z1t) + count;
793 /* new buffer Position */
794 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
795 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
796 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
797 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
798 /* end of fifo */
799 if (bch->debug & DEBUG_HW_BFIFO)
800 printk(KERN_DEBUG "hfcpci_FFt fillempty "
801 "fcnt(%d) maxl(%d) nz1(%x) dst(%p)\n",
802 fcnt, maxlen, new_z1, dst);
803 fcnt += count;
804 if (maxlen > count)
805 maxlen = count; /* limit size */
806 memset(dst, 0x2a, maxlen); /* first copy */
807 count -= maxlen; /* remaining bytes */
808 if (count) {
809 dst = bdata; /* start of buffer */
810 memset(dst, 0x2a, count);
811 }
812 *z1t = cpu_to_le16(new_z1); /* now send data */
813 }
814
Karsten Keil1700fe12008-07-26 18:55:28 +0200815next_t_frame:
816 count = bch->tx_skb->len - bch->tx_idx;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200817 /* maximum fill shall be poll*2 */
818 if (count > (poll << 1) - fcnt)
819 count = (poll << 1) - fcnt;
Karsten Keil1700fe12008-07-26 18:55:28 +0200820 if (count <= 0)
821 return;
822 /* data is suitable for fifo */
823 new_z1 = le16_to_cpu(*z1t) + count;
824 /* new buffer Position */
825 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
826 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
827 src = bch->tx_skb->data + bch->tx_idx;
828 /* source pointer */
829 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
830 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
831 /* end of fifo */
832 if (bch->debug & DEBUG_HW_BFIFO)
833 printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "
834 "maxl(%d) nz1(%x) dst(%p)\n",
835 fcnt, maxlen, new_z1, dst);
836 fcnt += count;
837 bch->tx_idx += count;
838 if (maxlen > count)
839 maxlen = count; /* limit size */
840 memcpy(dst, src, maxlen); /* first copy */
841 count -= maxlen; /* remaining bytes */
842 if (count) {
843 dst = bdata; /* start of buffer */
844 src += maxlen; /* new position */
845 memcpy(dst, src, count);
846 }
847 *z1t = cpu_to_le16(new_z1); /* now send data */
848 if (bch->tx_idx < bch->tx_skb->len)
849 return;
850 /* send confirm, on trans, free on hdlc. */
851 if (test_bit(FLG_TRANSPARENT, &bch->Flags))
852 confirm_Bsend(bch);
853 dev_kfree_skb(bch->tx_skb);
854 if (get_next_bframe(bch))
855 goto next_t_frame;
856 return;
857 }
858 if (bch->debug & DEBUG_HW_BCHANNEL)
859 printk(KERN_DEBUG
860 "%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",
861 __func__, bch->nr, bz->f1, bz->f2,
862 bz->za[bz->f1].z1);
863 fcnt = bz->f1 - bz->f2; /* frame count actually buffered */
864 if (fcnt < 0)
865 fcnt += (MAX_B_FRAMES + 1); /* if wrap around */
866 if (fcnt > (MAX_B_FRAMES - 1)) {
867 if (bch->debug & DEBUG_HW_BCHANNEL)
868 printk(KERN_DEBUG
869 "hfcpci_fill_Bfifo more as 14 frames\n");
870 return;
871 }
872 /* now determine free bytes in FIFO buffer */
873 maxlen = le16_to_cpu(bz->za[bz->f2].z2) -
874 le16_to_cpu(bz->za[bz->f1].z1) - 1;
875 if (maxlen <= 0)
876 maxlen += B_FIFO_SIZE; /* count now contains available bytes */
877
878 if (bch->debug & DEBUG_HW_BCHANNEL)
879 printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",
880 bch->nr, count, maxlen);
881
882 if (maxlen < count) {
883 if (bch->debug & DEBUG_HW_BCHANNEL)
884 printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");
885 return;
886 }
887 new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;
888 /* new buffer Position */
889 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
890 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
891
892 new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);
893 src = bch->tx_skb->data + bch->tx_idx; /* source pointer */
894 dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);
895 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);
896 /* end fifo */
897 if (maxlen > count)
898 maxlen = count; /* limit size */
899 memcpy(dst, src, maxlen); /* first copy */
900
901 count -= maxlen; /* remaining bytes */
902 if (count) {
903 dst = bdata; /* start of buffer */
904 src += maxlen; /* new position */
905 memcpy(dst, src, count);
906 }
907 bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */
908 bz->f1 = new_f1; /* next frame */
909 dev_kfree_skb(bch->tx_skb);
910 get_next_bframe(bch);
911}
912
913
914
915/*
916 * handle L1 state changes TE
917 */
918
919static void
920ph_state_te(struct dchannel *dch)
921{
922 if (dch->debug)
923 printk(KERN_DEBUG "%s: TE newstate %x\n",
924 __func__, dch->state);
925 switch (dch->state) {
926 case 0:
927 l1_event(dch->l1, HW_RESET_IND);
928 break;
929 case 3:
930 l1_event(dch->l1, HW_DEACT_IND);
931 break;
932 case 5:
933 case 8:
934 l1_event(dch->l1, ANYSIGNAL);
935 break;
936 case 6:
937 l1_event(dch->l1, INFO2);
938 break;
939 case 7:
940 l1_event(dch->l1, INFO4_P8);
941 break;
942 }
943}
944
945/*
946 * handle L1 state changes NT
947 */
948
949static void
950handle_nt_timer3(struct dchannel *dch) {
951 struct hfc_pci *hc = dch->hw;
952
953 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
954 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
955 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
956 hc->hw.nt_timer = 0;
957 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
958 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
959 hc->hw.mst_m |= HFCPCI_MASTER;
960 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
961 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
962 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
963}
964
965static void
966ph_state_nt(struct dchannel *dch)
967{
968 struct hfc_pci *hc = dch->hw;
969
970 if (dch->debug)
971 printk(KERN_DEBUG "%s: NT newstate %x\n",
972 __func__, dch->state);
973 switch (dch->state) {
974 case 2:
975 if (hc->hw.nt_timer < 0) {
976 hc->hw.nt_timer = 0;
977 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
978 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
979 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
980 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
981 /* Clear already pending ints */
982 if (Read_hfc(hc, HFCPCI_INT_S1));
983 Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);
984 udelay(10);
985 Write_hfc(hc, HFCPCI_STATES, 4);
986 dch->state = 4;
987 } else if (hc->hw.nt_timer == 0) {
988 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
989 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
990 hc->hw.nt_timer = NT_T1_COUNT;
991 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
992 hc->hw.ctmt |= HFCPCI_TIM3_125;
993 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
994 HFCPCI_CLTIMER);
995 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
996 test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);
997 /* allow G2 -> G3 transition */
998 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
999 } else {
1000 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
1001 }
1002 break;
1003 case 1:
1004 hc->hw.nt_timer = 0;
1005 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1006 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1007 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1008 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1009 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1010 hc->hw.mst_m &= ~HFCPCI_MASTER;
1011 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1012 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1013 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
1014 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
1015 break;
1016 case 4:
1017 hc->hw.nt_timer = 0;
1018 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1019 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1020 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1021 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1022 break;
1023 case 3:
1024 if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {
1025 if (!test_and_clear_bit(FLG_L2_ACTIVATED,
1026 &dch->Flags)) {
1027 handle_nt_timer3(dch);
1028 break;
1029 }
1030 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1031 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
1032 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1033 hc->hw.nt_timer = NT_T3_COUNT;
1034 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
1035 hc->hw.ctmt |= HFCPCI_TIM3_125;
1036 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
1037 HFCPCI_CLTIMER);
1038 }
1039 break;
1040 }
1041}
1042
1043static void
1044ph_state(struct dchannel *dch)
1045{
1046 struct hfc_pci *hc = dch->hw;
1047
1048 if (hc->hw.protocol == ISDN_P_NT_S0) {
1049 if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&
1050 hc->hw.nt_timer < 0)
1051 handle_nt_timer3(dch);
1052 else
1053 ph_state_nt(dch);
1054 } else
1055 ph_state_te(dch);
1056}
1057
1058/*
1059 * Layer 1 callback function
1060 */
1061static int
1062hfc_l1callback(struct dchannel *dch, u_int cmd)
1063{
1064 struct hfc_pci *hc = dch->hw;
1065
1066 switch (cmd) {
1067 case INFO3_P8:
1068 case INFO3_P10:
1069 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1070 hc->hw.mst_m |= HFCPCI_MASTER;
1071 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1072 break;
1073 case HW_RESET_REQ:
1074 Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);
1075 /* HFC ST 3 */
1076 udelay(6);
1077 Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */
1078 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1079 hc->hw.mst_m |= HFCPCI_MASTER;
1080 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1081 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
1082 HFCPCI_DO_ACTION);
1083 l1_event(dch->l1, HW_POWERUP_IND);
1084 break;
1085 case HW_DEACT_REQ:
1086 hc->hw.mst_m &= ~HFCPCI_MASTER;
1087 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1088 skb_queue_purge(&dch->squeue);
1089 if (dch->tx_skb) {
1090 dev_kfree_skb(dch->tx_skb);
1091 dch->tx_skb = NULL;
1092 }
1093 dch->tx_idx = 0;
1094 if (dch->rx_skb) {
1095 dev_kfree_skb(dch->rx_skb);
1096 dch->rx_skb = NULL;
1097 }
1098 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1099 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1100 del_timer(&dch->timer);
1101 break;
1102 case HW_POWERUP_REQ:
1103 Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);
1104 break;
1105 case PH_ACTIVATE_IND:
1106 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
1107 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
1108 GFP_ATOMIC);
1109 break;
1110 case PH_DEACTIVATE_IND:
1111 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1112 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
1113 GFP_ATOMIC);
1114 break;
1115 default:
1116 if (dch->debug & DEBUG_HW)
1117 printk(KERN_DEBUG "%s: unknown command %x\n",
1118 __func__, cmd);
1119 return -1;
1120 }
1121 return 0;
1122}
1123
1124/*
1125 * Interrupt handler
1126 */
1127static inline void
1128tx_birq(struct bchannel *bch)
1129{
1130 if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
1131 hfcpci_fill_fifo(bch);
1132 else {
1133 if (bch->tx_skb)
1134 dev_kfree_skb(bch->tx_skb);
1135 if (get_next_bframe(bch))
1136 hfcpci_fill_fifo(bch);
1137 }
1138}
1139
1140static inline void
1141tx_dirq(struct dchannel *dch)
1142{
1143 if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)
1144 hfcpci_fill_dfifo(dch->hw);
1145 else {
1146 if (dch->tx_skb)
1147 dev_kfree_skb(dch->tx_skb);
1148 if (get_next_dframe(dch))
1149 hfcpci_fill_dfifo(dch->hw);
1150 }
1151}
1152
1153static irqreturn_t
1154hfcpci_int(int intno, void *dev_id)
1155{
1156 struct hfc_pci *hc = dev_id;
1157 u_char exval;
1158 struct bchannel *bch;
1159 u_char val, stat;
1160
1161 spin_lock(&hc->lock);
1162 if (!(hc->hw.int_m2 & 0x08)) {
1163 spin_unlock(&hc->lock);
1164 return IRQ_NONE; /* not initialised */
1165 }
1166 stat = Read_hfc(hc, HFCPCI_STATUS);
1167 if (HFCPCI_ANYINT & stat) {
1168 val = Read_hfc(hc, HFCPCI_INT_S1);
1169 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1170 printk(KERN_DEBUG
1171 "HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);
1172 } else {
1173 /* shared */
1174 spin_unlock(&hc->lock);
1175 return IRQ_NONE;
1176 }
1177 hc->irqcnt++;
1178
1179 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1180 printk(KERN_DEBUG "HFC-PCI irq %x\n", val);
1181 val &= hc->hw.int_m1;
1182 if (val & 0x40) { /* state machine irq */
1183 exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;
1184 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1185 printk(KERN_DEBUG "ph_state chg %d->%d\n",
1186 hc->dch.state, exval);
1187 hc->dch.state = exval;
1188 schedule_event(&hc->dch, FLG_PHCHANGE);
1189 val &= ~0x40;
1190 }
1191 if (val & 0x80) { /* timer irq */
1192 if (hc->hw.protocol == ISDN_P_NT_S0) {
1193 if ((--hc->hw.nt_timer) < 0)
1194 schedule_event(&hc->dch, FLG_PHCHANGE);
1195 }
1196 val &= ~0x80;
1197 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);
1198 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001199 if (val & 0x08) { /* B1 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001200 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1201 if (bch)
1202 main_rec_hfcpci(bch);
1203 else if (hc->dch.debug)
1204 printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");
1205 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001206 if (val & 0x10) { /* B2 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001207 bch = Sel_BCS(hc, 2);
1208 if (bch)
1209 main_rec_hfcpci(bch);
1210 else if (hc->dch.debug)
1211 printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");
1212 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001213 if (val & 0x01) { /* B1 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001214 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1215 if (bch)
1216 tx_birq(bch);
1217 else if (hc->dch.debug)
1218 printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");
1219 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001220 if (val & 0x02) { /* B2 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001221 bch = Sel_BCS(hc, 2);
1222 if (bch)
1223 tx_birq(bch);
1224 else if (hc->dch.debug)
1225 printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");
1226 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001227 if (val & 0x20) /* D rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001228 receive_dmsg(hc);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001229 if (val & 0x04) { /* D tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001230 if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))
1231 del_timer(&hc->dch.timer);
1232 tx_dirq(&hc->dch);
1233 }
1234 spin_unlock(&hc->lock);
1235 return IRQ_HANDLED;
1236}
1237
1238/*
1239 * timer callback for D-chan busy resolution. Currently no function
1240 */
1241static void
1242hfcpci_dbusy_timer(struct hfc_pci *hc)
1243{
1244}
1245
1246/*
1247 * activate/deactivate hardware for selected channels and mode
1248 */
1249static int
1250mode_hfcpci(struct bchannel *bch, int bc, int protocol)
1251{
1252 struct hfc_pci *hc = bch->hw;
1253 int fifo2;
1254 u_char rx_slot = 0, tx_slot = 0, pcm_mode;
1255
1256 if (bch->debug & DEBUG_HW_BCHANNEL)
1257 printk(KERN_DEBUG
1258 "HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",
1259 bch->state, protocol, bch->nr, bc);
1260
1261 fifo2 = bc;
1262 pcm_mode = (bc>>24) & 0xff;
1263 if (pcm_mode) { /* PCM SLOT USE */
1264 if (!test_bit(HFC_CFG_PCM, &hc->cfg))
1265 printk(KERN_WARNING
1266 "%s: pcm channel id without HFC_CFG_PCM\n",
1267 __func__);
1268 rx_slot = (bc>>8) & 0xff;
1269 tx_slot = (bc>>16) & 0xff;
1270 bc = bc & 0xff;
1271 } else if (test_bit(HFC_CFG_PCM, &hc->cfg) &&
1272 (protocol > ISDN_P_NONE))
1273 printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",
1274 __func__);
1275 if (hc->chanlimit > 1) {
1276 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1277 hc->hw.sctrl_e &= ~0x80;
1278 } else {
1279 if (bc & 2) {
1280 if (protocol != ISDN_P_NONE) {
1281 hc->hw.bswapped = 1; /* B1 and B2 exchanged */
1282 hc->hw.sctrl_e |= 0x80;
1283 } else {
1284 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1285 hc->hw.sctrl_e &= ~0x80;
1286 }
1287 fifo2 = 1;
1288 } else {
1289 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1290 hc->hw.sctrl_e &= ~0x80;
1291 }
1292 }
1293 switch (protocol) {
1294 case (-1): /* used for init */
1295 bch->state = -1;
1296 bch->nr = bc;
1297 case (ISDN_P_NONE):
1298 if (bch->state == ISDN_P_NONE)
1299 return 0;
1300 if (bc & 2) {
1301 hc->hw.sctrl &= ~SCTRL_B2_ENA;
1302 hc->hw.sctrl_r &= ~SCTRL_B2_ENA;
1303 } else {
1304 hc->hw.sctrl &= ~SCTRL_B1_ENA;
1305 hc->hw.sctrl_r &= ~SCTRL_B1_ENA;
1306 }
1307 if (fifo2 & 2) {
1308 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;
1309 hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +
1310 HFCPCI_INTS_B2REC);
1311 } else {
1312 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;
1313 hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +
1314 HFCPCI_INTS_B1REC);
1315 }
1316#ifdef REVERSE_BITORDER
1317 if (bch->nr & 2)
1318 hc->hw.cirm &= 0x7f;
1319 else
1320 hc->hw.cirm &= 0xbf;
1321#endif
1322 bch->state = ISDN_P_NONE;
1323 bch->nr = bc;
1324 test_and_clear_bit(FLG_HDLC, &bch->Flags);
1325 test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
1326 break;
1327 case (ISDN_P_B_RAW):
1328 bch->state = protocol;
1329 bch->nr = bc;
1330 hfcpci_clear_fifo_rx(hc, (fifo2 & 2)?1:0);
1331 hfcpci_clear_fifo_tx(hc, (fifo2 & 2)?1:0);
1332 if (bc & 2) {
1333 hc->hw.sctrl |= SCTRL_B2_ENA;
1334 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1335#ifdef REVERSE_BITORDER
1336 hc->hw.cirm |= 0x80;
1337#endif
1338 } else {
1339 hc->hw.sctrl |= SCTRL_B1_ENA;
1340 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1341#ifdef REVERSE_BITORDER
1342 hc->hw.cirm |= 0x40;
1343#endif
1344 }
1345 if (fifo2 & 2) {
1346 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001347 if (!tics)
1348 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
1349 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001350 hc->hw.ctmt |= 2;
1351 hc->hw.conn &= ~0x18;
1352 } else {
1353 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001354 if (!tics)
1355 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
1356 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001357 hc->hw.ctmt |= 1;
1358 hc->hw.conn &= ~0x03;
1359 }
1360 test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
1361 break;
1362 case (ISDN_P_B_HDLC):
1363 bch->state = protocol;
1364 bch->nr = bc;
1365 hfcpci_clear_fifo_rx(hc, (fifo2 & 2)?1:0);
1366 hfcpci_clear_fifo_tx(hc, (fifo2 & 2)?1:0);
1367 if (bc & 2) {
1368 hc->hw.sctrl |= SCTRL_B2_ENA;
1369 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1370 } else {
1371 hc->hw.sctrl |= SCTRL_B1_ENA;
1372 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1373 }
1374 if (fifo2 & 2) {
1375 hc->hw.last_bfifo_cnt[1] = 0;
1376 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
1377 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
1378 HFCPCI_INTS_B2REC);
1379 hc->hw.ctmt &= ~2;
1380 hc->hw.conn &= ~0x18;
1381 } else {
1382 hc->hw.last_bfifo_cnt[0] = 0;
1383 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
1384 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
1385 HFCPCI_INTS_B1REC);
1386 hc->hw.ctmt &= ~1;
1387 hc->hw.conn &= ~0x03;
1388 }
1389 test_and_set_bit(FLG_HDLC, &bch->Flags);
1390 break;
1391 default:
1392 printk(KERN_DEBUG "prot not known %x\n", protocol);
1393 return -ENOPROTOOPT;
1394 }
1395 if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
1396 if ((protocol == ISDN_P_NONE) ||
1397 (protocol == -1)) { /* init case */
1398 rx_slot = 0;
1399 tx_slot = 0;
1400 } else {
1401 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
1402 rx_slot |= 0xC0;
1403 tx_slot |= 0xC0;
1404 } else {
1405 rx_slot |= 0x80;
1406 tx_slot |= 0x80;
1407 }
1408 }
1409 if (bc & 2) {
1410 hc->hw.conn &= 0xc7;
1411 hc->hw.conn |= 0x08;
1412 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",
1413 __func__, tx_slot);
1414 printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",
1415 __func__, rx_slot);
1416 Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);
1417 Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);
1418 } else {
1419 hc->hw.conn &= 0xf8;
1420 hc->hw.conn |= 0x01;
1421 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",
1422 __func__, tx_slot);
1423 printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",
1424 __func__, rx_slot);
1425 Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);
1426 Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);
1427 }
1428 }
1429 Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
1430 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1431 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1432 Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
1433 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1434 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1435 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1436#ifdef REVERSE_BITORDER
1437 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1438#endif
1439 return 0;
1440}
1441
1442static int
1443set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)
1444{
1445 struct hfc_pci *hc = bch->hw;
1446
1447 if (bch->debug & DEBUG_HW_BCHANNEL)
1448 printk(KERN_DEBUG
1449 "HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",
1450 bch->state, protocol, bch->nr, chan);
1451 if (bch->nr != chan) {
1452 printk(KERN_DEBUG
1453 "HFCPCI rxtest wrong channel parameter %x/%x\n",
1454 bch->nr, chan);
1455 return -EINVAL;
1456 }
1457 switch (protocol) {
1458 case (ISDN_P_B_RAW):
1459 bch->state = protocol;
1460 hfcpci_clear_fifo_rx(hc, (chan & 2)?1:0);
1461 if (chan & 2) {
1462 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1463 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001464 if (!tics)
1465 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001466 hc->hw.ctmt |= 2;
1467 hc->hw.conn &= ~0x18;
1468#ifdef REVERSE_BITORDER
1469 hc->hw.cirm |= 0x80;
1470#endif
1471 } else {
1472 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1473 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001474 if (!tics)
1475 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001476 hc->hw.ctmt |= 1;
1477 hc->hw.conn &= ~0x03;
1478#ifdef REVERSE_BITORDER
1479 hc->hw.cirm |= 0x40;
1480#endif
1481 }
1482 break;
1483 case (ISDN_P_B_HDLC):
1484 bch->state = protocol;
1485 hfcpci_clear_fifo_rx(hc, (chan & 2)?1:0);
1486 if (chan & 2) {
1487 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1488 hc->hw.last_bfifo_cnt[1] = 0;
1489 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
1490 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
1491 hc->hw.ctmt &= ~2;
1492 hc->hw.conn &= ~0x18;
1493 } else {
1494 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1495 hc->hw.last_bfifo_cnt[0] = 0;
1496 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
1497 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
1498 hc->hw.ctmt &= ~1;
1499 hc->hw.conn &= ~0x03;
1500 }
1501 break;
1502 default:
1503 printk(KERN_DEBUG "prot not known %x\n", protocol);
1504 return -ENOPROTOOPT;
1505 }
1506 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1507 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1508 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1509 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1510 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1511#ifdef REVERSE_BITORDER
1512 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1513#endif
1514 return 0;
1515}
1516
1517static void
1518deactivate_bchannel(struct bchannel *bch)
1519{
1520 struct hfc_pci *hc = bch->hw;
1521 u_long flags;
1522
1523 spin_lock_irqsave(&hc->lock, flags);
1524 if (test_and_clear_bit(FLG_TX_NEXT, &bch->Flags)) {
1525 dev_kfree_skb(bch->next_skb);
1526 bch->next_skb = NULL;
1527 }
1528 if (bch->tx_skb) {
1529 dev_kfree_skb(bch->tx_skb);
1530 bch->tx_skb = NULL;
1531 }
1532 bch->tx_idx = 0;
1533 if (bch->rx_skb) {
1534 dev_kfree_skb(bch->rx_skb);
1535 bch->rx_skb = NULL;
1536 }
1537 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
1538 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1539 test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
1540 spin_unlock_irqrestore(&hc->lock, flags);
1541}
1542
1543/*
1544 * Layer 1 B-channel hardware access
1545 */
1546static int
1547channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1548{
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001549 int ret = 0;
Karsten Keil1700fe12008-07-26 18:55:28 +02001550
1551 switch (cq->op) {
1552 case MISDN_CTRL_GETOP:
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001553 cq->op = MISDN_CTRL_FILL_EMPTY;
1554 break;
1555 case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
1556 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
1557 if (debug & DEBUG_HW_OPEN)
1558 printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
1559 "off=%d)\n", __func__, bch->nr, !!cq->p1);
Karsten Keil1700fe12008-07-26 18:55:28 +02001560 break;
1561 default:
1562 printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
1563 ret = -EINVAL;
1564 break;
1565 }
1566 return ret;
1567}
1568static int
1569hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1570{
1571 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1572 struct hfc_pci *hc = bch->hw;
1573 int ret = -EINVAL;
1574 u_long flags;
1575
1576 if (bch->debug & DEBUG_HW)
1577 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1578 switch (cmd) {
1579 case HW_TESTRX_RAW:
1580 spin_lock_irqsave(&hc->lock, flags);
1581 ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);
1582 spin_unlock_irqrestore(&hc->lock, flags);
1583 break;
1584 case HW_TESTRX_HDLC:
1585 spin_lock_irqsave(&hc->lock, flags);
1586 ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);
1587 spin_unlock_irqrestore(&hc->lock, flags);
1588 break;
1589 case HW_TESTRX_OFF:
1590 spin_lock_irqsave(&hc->lock, flags);
1591 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
1592 spin_unlock_irqrestore(&hc->lock, flags);
1593 ret = 0;
1594 break;
1595 case CLOSE_CHANNEL:
1596 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1597 if (test_bit(FLG_ACTIVE, &bch->Flags))
1598 deactivate_bchannel(bch);
1599 ch->protocol = ISDN_P_NONE;
1600 ch->peer = NULL;
1601 module_put(THIS_MODULE);
1602 ret = 0;
1603 break;
1604 case CONTROL_CHANNEL:
1605 ret = channel_bctrl(bch, arg);
1606 break;
1607 default:
1608 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1609 __func__, cmd);
1610 }
1611 return ret;
1612}
1613
1614/*
1615 * Layer2 -> Layer 1 Dchannel data
1616 */
1617static int
1618hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
1619{
1620 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1621 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1622 struct hfc_pci *hc = dch->hw;
1623 int ret = -EINVAL;
1624 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1625 unsigned int id;
1626 u_long flags;
1627
1628 switch (hh->prim) {
1629 case PH_DATA_REQ:
1630 spin_lock_irqsave(&hc->lock, flags);
1631 ret = dchannel_senddata(dch, skb);
1632 if (ret > 0) { /* direct TX */
1633 id = hh->id; /* skb can be freed */
1634 hfcpci_fill_dfifo(dch->hw);
1635 ret = 0;
1636 spin_unlock_irqrestore(&hc->lock, flags);
1637 queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
1638 } else
1639 spin_unlock_irqrestore(&hc->lock, flags);
1640 return ret;
1641 case PH_ACTIVATE_REQ:
1642 spin_lock_irqsave(&hc->lock, flags);
1643 if (hc->hw.protocol == ISDN_P_NT_S0) {
1644 ret = 0;
1645 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1646 hc->hw.mst_m |= HFCPCI_MASTER;
1647 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1648 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1649 spin_unlock_irqrestore(&hc->lock, flags);
1650 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
1651 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
1652 break;
1653 }
1654 test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);
1655 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
1656 HFCPCI_DO_ACTION | 1);
1657 } else
1658 ret = l1_event(dch->l1, hh->prim);
1659 spin_unlock_irqrestore(&hc->lock, flags);
1660 break;
1661 case PH_DEACTIVATE_REQ:
1662 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1663 spin_lock_irqsave(&hc->lock, flags);
1664 if (hc->hw.protocol == ISDN_P_NT_S0) {
1665 /* prepare deactivation */
1666 Write_hfc(hc, HFCPCI_STATES, 0x40);
1667 skb_queue_purge(&dch->squeue);
1668 if (dch->tx_skb) {
1669 dev_kfree_skb(dch->tx_skb);
1670 dch->tx_skb = NULL;
1671 }
1672 dch->tx_idx = 0;
1673 if (dch->rx_skb) {
1674 dev_kfree_skb(dch->rx_skb);
1675 dch->rx_skb = NULL;
1676 }
1677 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1678 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1679 del_timer(&dch->timer);
1680#ifdef FIXME
1681 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
1682 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
1683#endif
1684 hc->hw.mst_m &= ~HFCPCI_MASTER;
1685 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1686 ret = 0;
1687 } else {
1688 ret = l1_event(dch->l1, hh->prim);
1689 }
1690 spin_unlock_irqrestore(&hc->lock, flags);
1691 break;
1692 }
1693 if (!ret)
1694 dev_kfree_skb(skb);
1695 return ret;
1696}
1697
1698/*
1699 * Layer2 -> Layer 1 Bchannel data
1700 */
1701static int
1702hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
1703{
1704 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1705 struct hfc_pci *hc = bch->hw;
1706 int ret = -EINVAL;
1707 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1708 unsigned int id;
1709 u_long flags;
1710
1711 switch (hh->prim) {
1712 case PH_DATA_REQ:
1713 spin_lock_irqsave(&hc->lock, flags);
1714 ret = bchannel_senddata(bch, skb);
1715 if (ret > 0) { /* direct TX */
1716 id = hh->id; /* skb can be freed */
1717 hfcpci_fill_fifo(bch);
1718 ret = 0;
1719 spin_unlock_irqrestore(&hc->lock, flags);
1720 if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
1721 queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
1722 } else
1723 spin_unlock_irqrestore(&hc->lock, flags);
1724 return ret;
1725 case PH_ACTIVATE_REQ:
1726 spin_lock_irqsave(&hc->lock, flags);
1727 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
1728 ret = mode_hfcpci(bch, bch->nr, ch->protocol);
1729 else
1730 ret = 0;
1731 spin_unlock_irqrestore(&hc->lock, flags);
1732 if (!ret)
1733 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
1734 NULL, GFP_KERNEL);
1735 break;
1736 case PH_DEACTIVATE_REQ:
1737 deactivate_bchannel(bch);
1738 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
1739 NULL, GFP_KERNEL);
1740 ret = 0;
1741 break;
1742 }
1743 if (!ret)
1744 dev_kfree_skb(skb);
1745 return ret;
1746}
1747
1748/*
1749 * called for card init message
1750 */
1751
Harvey Harrison1532dcb2008-09-22 19:16:51 -07001752static void
Karsten Keil1700fe12008-07-26 18:55:28 +02001753inithfcpci(struct hfc_pci *hc)
1754{
1755 printk(KERN_DEBUG "inithfcpci: entered\n");
1756 hc->dch.timer.function = (void *) hfcpci_dbusy_timer;
1757 hc->dch.timer.data = (long) &hc->dch;
1758 init_timer(&hc->dch.timer);
1759 hc->chanlimit = 2;
1760 mode_hfcpci(&hc->bch[0], 1, -1);
1761 mode_hfcpci(&hc->bch[1], 2, -1);
1762}
1763
1764
1765static int
1766init_card(struct hfc_pci *hc)
1767{
1768 int cnt = 3;
1769 u_long flags;
1770
1771 printk(KERN_DEBUG "init_card: entered\n");
1772
1773
1774 spin_lock_irqsave(&hc->lock, flags);
1775 disable_hwirq(hc);
1776 spin_unlock_irqrestore(&hc->lock, flags);
1777 if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {
1778 printk(KERN_WARNING
1779 "mISDN: couldn't get interrupt %d\n", hc->irq);
1780 return -EIO;
1781 }
1782 spin_lock_irqsave(&hc->lock, flags);
1783 reset_hfcpci(hc);
1784 while (cnt) {
1785 inithfcpci(hc);
1786 /*
1787 * Finally enable IRQ output
1788 * this is only allowed, if an IRQ routine is allready
1789 * established for this HFC, so don't do that earlier
1790 */
1791 enable_hwirq(hc);
1792 spin_unlock_irqrestore(&hc->lock, flags);
1793 /* Timeout 80ms */
1794 current->state = TASK_UNINTERRUPTIBLE;
1795 schedule_timeout((80*HZ)/1000);
1796 printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
1797 hc->irq, hc->irqcnt);
1798 /* now switch timer interrupt off */
1799 spin_lock_irqsave(&hc->lock, flags);
1800 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1801 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1802 /* reinit mode reg */
1803 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1804 if (!hc->irqcnt) {
1805 printk(KERN_WARNING
1806 "HFC PCI: IRQ(%d) getting no interrupts "
1807 "during init %d\n", hc->irq, 4 - cnt);
1808 if (cnt == 1) {
1809 spin_unlock_irqrestore(&hc->lock, flags);
1810 return -EIO;
1811 } else {
1812 reset_hfcpci(hc);
1813 cnt--;
1814 }
1815 } else {
1816 spin_unlock_irqrestore(&hc->lock, flags);
1817 hc->initdone = 1;
1818 return 0;
1819 }
1820 }
1821 disable_hwirq(hc);
1822 spin_unlock_irqrestore(&hc->lock, flags);
1823 free_irq(hc->irq, hc);
1824 return -EIO;
1825}
1826
1827static int
1828channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)
1829{
1830 int ret = 0;
1831 u_char slot;
1832
1833 switch (cq->op) {
1834 case MISDN_CTRL_GETOP:
1835 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
1836 MISDN_CTRL_DISCONNECT;
1837 break;
1838 case MISDN_CTRL_LOOP:
1839 /* channel 0 disabled loop */
1840 if (cq->channel < 0 || cq->channel > 2) {
1841 ret = -EINVAL;
1842 break;
1843 }
1844 if (cq->channel & 1) {
1845 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1846 slot = 0xC0;
1847 else
1848 slot = 0x80;
1849 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
1850 __func__, slot);
1851 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1852 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1853 hc->hw.conn = (hc->hw.conn & ~7) | 6;
1854 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1855 }
1856 if (cq->channel & 2) {
1857 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1858 slot = 0xC1;
1859 else
1860 slot = 0x81;
1861 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
1862 __func__, slot);
1863 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1864 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1865 hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;
1866 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1867 }
1868 if (cq->channel & 3)
1869 hc->hw.trm |= 0x80; /* enable IOM-loop */
1870 else {
1871 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1872 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1873 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1874 }
1875 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1876 break;
1877 case MISDN_CTRL_CONNECT:
1878 if (cq->channel == cq->p1) {
1879 ret = -EINVAL;
1880 break;
1881 }
1882 if (cq->channel < 1 || cq->channel > 2 ||
1883 cq->p1 < 1 || cq->p1 > 2) {
1884 ret = -EINVAL;
1885 break;
1886 }
1887 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1888 slot = 0xC0;
1889 else
1890 slot = 0x80;
1891 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
1892 __func__, slot);
1893 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1894 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1895 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1896 slot = 0xC1;
1897 else
1898 slot = 0x81;
1899 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
1900 __func__, slot);
1901 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1902 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1903 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;
1904 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1905 hc->hw.trm |= 0x80;
1906 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1907 break;
1908 case MISDN_CTRL_DISCONNECT:
1909 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1910 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1911 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1912 break;
1913 default:
1914 printk(KERN_WARNING "%s: unknown Op %x\n",
1915 __func__, cq->op);
1916 ret = -EINVAL;
1917 break;
1918 }
1919 return ret;
1920}
1921
1922static int
1923open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,
1924 struct channel_req *rq)
1925{
1926 int err = 0;
1927
1928 if (debug & DEBUG_HW_OPEN)
1929 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
1930 hc->dch.dev.id, __builtin_return_address(0));
1931 if (rq->protocol == ISDN_P_NONE)
1932 return -EINVAL;
Martin Bachem55a6af92008-09-04 12:42:39 +02001933 if (rq->adr.channel == 1) {
1934 /* TODO: E-Channel */
1935 return -EINVAL;
1936 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001937 if (!hc->initdone) {
1938 if (rq->protocol == ISDN_P_TE_S0) {
1939 err = create_l1(&hc->dch, hfc_l1callback);
1940 if (err)
1941 return err;
1942 }
1943 hc->hw.protocol = rq->protocol;
1944 ch->protocol = rq->protocol;
1945 err = init_card(hc);
1946 if (err)
1947 return err;
1948 } else {
1949 if (rq->protocol != ch->protocol) {
1950 if (hc->hw.protocol == ISDN_P_TE_S0)
1951 l1_event(hc->dch.l1, CLOSE_CHANNEL);
Andreas Eversbergc3b3cde2008-11-09 10:23:19 +01001952 if (rq->protocol == ISDN_P_TE_S0) {
1953 err = create_l1(&hc->dch, hfc_l1callback);
1954 if (err)
1955 return err;
1956 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001957 hc->hw.protocol = rq->protocol;
1958 ch->protocol = rq->protocol;
1959 hfcpci_setmode(hc);
1960 }
1961 }
1962
1963 if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||
1964 ((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {
1965 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
1966 0, NULL, GFP_KERNEL);
1967 }
1968 rq->ch = ch;
1969 if (!try_module_get(THIS_MODULE))
1970 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1971 return 0;
1972}
1973
1974static int
1975open_bchannel(struct hfc_pci *hc, struct channel_req *rq)
1976{
1977 struct bchannel *bch;
1978
1979 if (rq->adr.channel > 2)
1980 return -EINVAL;
1981 if (rq->protocol == ISDN_P_NONE)
1982 return -EINVAL;
1983 bch = &hc->bch[rq->adr.channel - 1];
1984 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1985 return -EBUSY; /* b-channel can be only open once */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001986 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
Karsten Keil1700fe12008-07-26 18:55:28 +02001987 bch->ch.protocol = rq->protocol;
1988 rq->ch = &bch->ch; /* TODO: E-channel */
1989 if (!try_module_get(THIS_MODULE))
1990 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1991 return 0;
1992}
1993
1994/*
1995 * device control function
1996 */
1997static int
1998hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1999{
2000 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
2001 struct dchannel *dch = container_of(dev, struct dchannel, dev);
2002 struct hfc_pci *hc = dch->hw;
2003 struct channel_req *rq;
2004 int err = 0;
2005
2006 if (dch->debug & DEBUG_HW)
2007 printk(KERN_DEBUG "%s: cmd:%x %p\n",
2008 __func__, cmd, arg);
2009 switch (cmd) {
2010 case OPEN_CHANNEL:
2011 rq = arg;
Martin Bachema9b61832008-09-03 18:08:30 +02002012 if ((rq->protocol == ISDN_P_TE_S0) ||
2013 (rq->protocol == ISDN_P_NT_S0))
Karsten Keil1700fe12008-07-26 18:55:28 +02002014 err = open_dchannel(hc, ch, rq);
2015 else
2016 err = open_bchannel(hc, rq);
2017 break;
2018 case CLOSE_CHANNEL:
2019 if (debug & DEBUG_HW_OPEN)
2020 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
2021 __func__, hc->dch.dev.id,
2022 __builtin_return_address(0));
2023 module_put(THIS_MODULE);
2024 break;
2025 case CONTROL_CHANNEL:
2026 err = channel_ctrl(hc, arg);
2027 break;
2028 default:
2029 if (dch->debug & DEBUG_HW)
2030 printk(KERN_DEBUG "%s: unknown command %x\n",
2031 __func__, cmd);
2032 return -EINVAL;
2033 }
2034 return err;
2035}
2036
2037static int
2038setup_hw(struct hfc_pci *hc)
2039{
2040 void *buffer;
2041
2042 printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);
2043 hc->hw.cirm = 0;
2044 hc->dch.state = 0;
2045 pci_set_master(hc->pdev);
2046 if (!hc->irq) {
2047 printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");
2048 return 1;
2049 }
Harvey Harrison1532dcb2008-09-22 19:16:51 -07002050 hc->hw.pci_io = (char __iomem *)(unsigned long)hc->pdev->resource[1].start;
Karsten Keil1700fe12008-07-26 18:55:28 +02002051
2052 if (!hc->hw.pci_io) {
2053 printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");
2054 return 1;
2055 }
2056 /* Allocate memory for FIFOS */
2057 /* the memory needs to be on a 32k boundary within the first 4G */
2058 pci_set_dma_mask(hc->pdev, 0xFFFF8000);
2059 buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);
2060 /* We silently assume the address is okay if nonzero */
2061 if (!buffer) {
2062 printk(KERN_WARNING
2063 "HFC-PCI: Error allocating memory for FIFO!\n");
2064 return 1;
2065 }
2066 hc->hw.fifos = buffer;
2067 pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);
2068 hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);
2069 printk(KERN_INFO
2070 "HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",
2071 (u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,
Karsten Keil7878ac82008-07-28 12:21:25 +02002072 (u_long) hc->hw.dmahandle, hc->irq, HZ);
Karsten Keil1700fe12008-07-26 18:55:28 +02002073 /* enable memory mapped ports, disable busmaster */
2074 pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
2075 hc->hw.int_m2 = 0;
2076 disable_hwirq(hc);
2077 hc->hw.int_m1 = 0;
2078 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
2079 /* At this point the needed PCI config is done */
2080 /* fifos are still not enabled */
2081 hc->hw.timer.function = (void *) hfcpci_Timer;
2082 hc->hw.timer.data = (long) hc;
2083 init_timer(&hc->hw.timer);
2084 /* default PCM master */
2085 test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);
2086 return 0;
2087}
2088
2089static void
2090release_card(struct hfc_pci *hc) {
2091 u_long flags;
2092
2093 spin_lock_irqsave(&hc->lock, flags);
2094 hc->hw.int_m2 = 0; /* interrupt output off ! */
2095 disable_hwirq(hc);
2096 mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);
2097 mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);
2098 if (hc->dch.timer.function != NULL) {
2099 del_timer(&hc->dch.timer);
2100 hc->dch.timer.function = NULL;
2101 }
2102 spin_unlock_irqrestore(&hc->lock, flags);
2103 if (hc->hw.protocol == ISDN_P_TE_S0)
2104 l1_event(hc->dch.l1, CLOSE_CHANNEL);
2105 if (hc->initdone)
2106 free_irq(hc->irq, hc);
2107 release_io_hfcpci(hc); /* must release after free_irq! */
2108 mISDN_unregister_device(&hc->dch.dev);
2109 mISDN_freebchannel(&hc->bch[1]);
2110 mISDN_freebchannel(&hc->bch[0]);
2111 mISDN_freedchannel(&hc->dch);
Karsten Keil1700fe12008-07-26 18:55:28 +02002112 pci_set_drvdata(hc->pdev, NULL);
2113 kfree(hc);
2114}
2115
2116static int
2117setup_card(struct hfc_pci *card)
2118{
2119 int err = -EINVAL;
2120 u_int i;
Karsten Keil1700fe12008-07-26 18:55:28 +02002121 char name[MISDN_MAX_IDLEN];
2122
Karsten Keil1700fe12008-07-26 18:55:28 +02002123 card->dch.debug = debug;
2124 spin_lock_init(&card->lock);
2125 mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);
2126 card->dch.hw = card;
2127 card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
2128 card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
2129 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
2130 card->dch.dev.D.send = hfcpci_l2l1D;
2131 card->dch.dev.D.ctrl = hfc_dctrl;
2132 card->dch.dev.nrbchan = 2;
2133 for (i = 0; i < 2; i++) {
2134 card->bch[i].nr = i + 1;
Karsten Keilff4cc1d2008-07-30 18:26:58 +02002135 set_channelmap(i + 1, card->dch.dev.channelmap);
Karsten Keil1700fe12008-07-26 18:55:28 +02002136 card->bch[i].debug = debug;
2137 mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);
2138 card->bch[i].hw = card;
2139 card->bch[i].ch.send = hfcpci_l2l1B;
2140 card->bch[i].ch.ctrl = hfc_bctrl;
2141 card->bch[i].ch.nr = i + 1;
2142 list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);
2143 }
2144 err = setup_hw(card);
2145 if (err)
2146 goto error;
2147 snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002148 err = mISDN_register_device(&card->dch.dev, &card->pdev->dev, name);
Karsten Keil1700fe12008-07-26 18:55:28 +02002149 if (err)
2150 goto error;
2151 HFC_cnt++;
Karsten Keil1700fe12008-07-26 18:55:28 +02002152 printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);
2153 return 0;
2154error:
2155 mISDN_freebchannel(&card->bch[1]);
2156 mISDN_freebchannel(&card->bch[0]);
2157 mISDN_freedchannel(&card->dch);
2158 kfree(card);
2159 return err;
2160}
2161
2162/* private data in the PCI devices list */
2163struct _hfc_map {
2164 u_int subtype;
2165 u_int flag;
2166 char *name;
2167};
2168
2169static const struct _hfc_map hfc_map[] =
2170{
2171 {HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},
2172 {HFC_CCD_B000, 0, "Billion B000"},
2173 {HFC_CCD_B006, 0, "Billion B006"},
2174 {HFC_CCD_B007, 0, "Billion B007"},
2175 {HFC_CCD_B008, 0, "Billion B008"},
2176 {HFC_CCD_B009, 0, "Billion B009"},
2177 {HFC_CCD_B00A, 0, "Billion B00A"},
2178 {HFC_CCD_B00B, 0, "Billion B00B"},
2179 {HFC_CCD_B00C, 0, "Billion B00C"},
2180 {HFC_CCD_B100, 0, "Seyeon B100"},
2181 {HFC_CCD_B700, 0, "Primux II S0 B700"},
2182 {HFC_CCD_B701, 0, "Primux II S0 NT B701"},
2183 {HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},
2184 {HFC_ASUS_0675, 0, "Asuscom/Askey 675"},
2185 {HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},
2186 {HFC_BERKOM_A1T, 0, "German telekom A1T"},
2187 {HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},
2188 {HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},
2189 {HFC_DIGI_DF_M_IOM2_E, 0,
2190 "Digi International DataFire Micro V IOM2 (Europe)"},
2191 {HFC_DIGI_DF_M_E, 0,
2192 "Digi International DataFire Micro V (Europe)"},
2193 {HFC_DIGI_DF_M_IOM2_A, 0,
2194 "Digi International DataFire Micro V IOM2 (North America)"},
2195 {HFC_DIGI_DF_M_A, 0,
2196 "Digi International DataFire Micro V (North America)"},
2197 {HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},
2198 {},
2199};
2200
2201static struct pci_device_id hfc_ids[] =
2202{
2203 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_2BD0,
2204 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[0]},
2205 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B000,
2206 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[1]},
2207 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B006,
2208 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[2]},
2209 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B007,
2210 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[3]},
2211 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B008,
2212 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[4]},
2213 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B009,
2214 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[5]},
2215 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00A,
2216 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[6]},
2217 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00B,
2218 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[7]},
2219 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00C,
2220 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[8]},
2221 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B100,
2222 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[9]},
2223 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B700,
2224 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[10]},
2225 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B701,
2226 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[11]},
2227 {PCI_VENDOR_ID_ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1,
2228 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[12]},
2229 {PCI_VENDOR_ID_ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675,
2230 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[13]},
2231 {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT,
2232 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[14]},
2233 {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_A1T,
2234 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[15]},
2235 {PCI_VENDOR_ID_ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575,
2236 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[16]},
2237 {PCI_VENDOR_ID_ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0,
2238 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[17]},
2239 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E,
2240 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[18]},
2241 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_E,
2242 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[19]},
2243 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A,
2244 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[20]},
2245 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_A,
2246 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[21]},
2247 {PCI_VENDOR_ID_SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2,
2248 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[22]},
2249 {},
2250};
2251
2252static int __devinit
2253hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2254{
2255 int err = -ENOMEM;
2256 struct hfc_pci *card;
2257 struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;
2258
2259 card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);
2260 if (!card) {
2261 printk(KERN_ERR "No kmem for HFC card\n");
2262 return err;
2263 }
2264 card->pdev = pdev;
2265 card->subtype = m->subtype;
2266 err = pci_enable_device(pdev);
2267 if (err) {
2268 kfree(card);
2269 return err;
2270 }
2271
2272 printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",
2273 m->name, pci_name(pdev));
2274
2275 card->irq = pdev->irq;
2276 pci_set_drvdata(pdev, card);
2277 err = setup_card(card);
2278 if (err)
2279 pci_set_drvdata(pdev, NULL);
2280 return err;
2281}
2282
2283static void __devexit
2284hfc_remove_pci(struct pci_dev *pdev)
2285{
2286 struct hfc_pci *card = pci_get_drvdata(pdev);
Karsten Keil1700fe12008-07-26 18:55:28 +02002287
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002288 if (card)
Karsten Keil1700fe12008-07-26 18:55:28 +02002289 release_card(card);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002290 else
Karsten Keil1700fe12008-07-26 18:55:28 +02002291 if (debug)
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002292 printk(KERN_WARNING "%s: drvdata already removed\n",
Karsten Keil1700fe12008-07-26 18:55:28 +02002293 __func__);
2294}
2295
2296
2297static struct pci_driver hfc_driver = {
2298 .name = "hfcpci",
2299 .probe = hfc_probe,
2300 .remove = __devexit_p(hfc_remove_pci),
2301 .id_table = hfc_ids,
2302};
2303
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002304static int
2305_hfcpci_softirq(struct device *dev, void *arg)
2306{
2307 struct hfc_pci *hc = dev_get_drvdata(dev);
2308 struct bchannel *bch;
2309 if (hc == NULL)
2310 return 0;
2311
2312 if (hc->hw.int_m2 & HFCPCI_IRQ_ENABLE) {
2313 spin_lock(&hc->lock);
2314 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
2315 if (bch && bch->state == ISDN_P_B_RAW) { /* B1 rx&tx */
2316 main_rec_hfcpci(bch);
2317 tx_birq(bch);
2318 }
2319 bch = Sel_BCS(hc, hc->hw.bswapped ? 1 : 2);
2320 if (bch && bch->state == ISDN_P_B_RAW) { /* B2 rx&tx */
2321 main_rec_hfcpci(bch);
2322 tx_birq(bch);
2323 }
2324 spin_unlock(&hc->lock);
2325 }
2326 return 0;
2327}
2328
2329static void
2330hfcpci_softirq(void *arg)
2331{
2332 (void) driver_for_each_device(&hfc_driver.driver, NULL, arg,
2333 _hfcpci_softirq);
2334
2335 /* if next event would be in the past ... */
2336 if ((s32)(hfc_jiffies + tics - jiffies) <= 0)
2337 hfc_jiffies = jiffies + 1;
2338 else
2339 hfc_jiffies += tics;
2340 hfc_tl.expires = hfc_jiffies;
2341 add_timer(&hfc_tl);
2342}
2343
Karsten Keil1700fe12008-07-26 18:55:28 +02002344static int __init
2345HFC_init(void)
2346{
2347 int err;
2348
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002349 if (!poll)
2350 poll = HFCPCI_BTRANS_THRESHOLD;
2351
2352 if (poll != HFCPCI_BTRANS_THRESHOLD) {
Andreas Eversberg400fd972008-10-11 08:13:29 +02002353 tics = (poll * HZ) / 8000;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002354 if (tics < 1)
2355 tics = 1;
Andreas Eversberg400fd972008-10-11 08:13:29 +02002356 poll = (tics * 8000) / HZ;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002357 if (poll > 256 || poll < 8) {
2358 printk(KERN_ERR "%s: Wrong poll value %d not in range "
2359 "of 8..256.\n", __func__, poll);
2360 err = -EINVAL;
2361 return err;
2362 }
2363 }
2364 if (poll != HFCPCI_BTRANS_THRESHOLD) {
2365 printk(KERN_INFO "%s: Using alternative poll value of %d\n",
2366 __func__, poll);
2367 hfc_tl.function = (void *)hfcpci_softirq;
2368 hfc_tl.data = 0;
2369 init_timer(&hfc_tl);
2370 hfc_tl.expires = jiffies + tics;
2371 hfc_jiffies = hfc_tl.expires;
2372 add_timer(&hfc_tl);
2373 } else
2374 tics = 0; /* indicate the use of controller's timer */
2375
Karsten Keil1700fe12008-07-26 18:55:28 +02002376 err = pci_register_driver(&hfc_driver);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002377 if (err) {
2378 if (timer_pending(&hfc_tl))
2379 del_timer(&hfc_tl);
2380 }
2381
Karsten Keil1700fe12008-07-26 18:55:28 +02002382 return err;
2383}
2384
2385static void __exit
2386HFC_cleanup(void)
2387{
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002388 if (timer_pending(&hfc_tl))
2389 del_timer(&hfc_tl);
2390
Karsten Keil1700fe12008-07-26 18:55:28 +02002391 pci_unregister_driver(&hfc_driver);
2392}
2393
2394module_init(HFC_init);
2395module_exit(HFC_cleanup);
Matthias Urlichse314f892008-10-16 13:58:54 +02002396
2397MODULE_DEVICE_TABLE(pci, hfc_ids);