blob: 123e8e5e57be16e414dd53f149d7087e4355e3b2 [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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000047#include <linux/interrupt.h>
Karsten Keil1700fe12008-07-26 18:55:28 +020048#include <linux/module.h>
49#include <linux/pci.h>
50#include <linux/delay.h>
51#include <linux/mISDNhw.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Karsten Keil1700fe12008-07-26 18:55:28 +020053
54#include "hfc_pci.h"
55
56static const char *hfcpci_revision = "2.0";
57
Karsten Keil1700fe12008-07-26 18:55:28 +020058static int HFC_cnt;
59static uint debug;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +020060static uint poll, tics;
Hannes Eder6c2959a2009-02-12 09:28:40 +000061static struct timer_list hfc_tl;
Hannes Ederaa611f82009-02-14 13:10:33 +000062static unsigned long hfc_jiffies;
Karsten Keil1700fe12008-07-26 18:55:28 +020063
64MODULE_AUTHOR("Karsten Keil");
65MODULE_LICENSE("GPL");
Karsten Keil9785a8f82009-01-11 17:58:13 +010066module_param(debug, uint, S_IRUGO | S_IWUSR);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +020067module_param(poll, uint, S_IRUGO | S_IWUSR);
Karsten Keil1700fe12008-07-26 18:55:28 +020068
Karsten Keil1700fe12008-07-26 18:55:28 +020069enum {
70 HFC_CCD_2BD0,
71 HFC_CCD_B000,
72 HFC_CCD_B006,
73 HFC_CCD_B007,
74 HFC_CCD_B008,
75 HFC_CCD_B009,
76 HFC_CCD_B00A,
77 HFC_CCD_B00B,
78 HFC_CCD_B00C,
79 HFC_CCD_B100,
80 HFC_CCD_B700,
81 HFC_CCD_B701,
82 HFC_ASUS_0675,
83 HFC_BERKOM_A1T,
84 HFC_BERKOM_TCONCEPT,
85 HFC_ANIGMA_MC145575,
86 HFC_ZOLTRIX_2BD0,
87 HFC_DIGI_DF_M_IOM2_E,
88 HFC_DIGI_DF_M_E,
89 HFC_DIGI_DF_M_IOM2_A,
90 HFC_DIGI_DF_M_A,
91 HFC_ABOCOM_2BD1,
92 HFC_SITECOM_DC105V2,
93};
94
95struct hfcPCI_hw {
96 unsigned char cirm;
97 unsigned char ctmt;
98 unsigned char clkdel;
99 unsigned char states;
100 unsigned char conn;
101 unsigned char mst_m;
102 unsigned char int_m1;
103 unsigned char int_m2;
104 unsigned char sctrl;
105 unsigned char sctrl_r;
106 unsigned char sctrl_e;
107 unsigned char trm;
108 unsigned char fifo_en;
109 unsigned char bswapped;
110 unsigned char protocol;
111 int nt_timer;
Joe Perches475be4d2012-02-19 19:52:38 -0800112 unsigned char __iomem *pci_io; /* start of PCI IO memory */
Karsten Keil1700fe12008-07-26 18:55:28 +0200113 dma_addr_t dmahandle;
114 void *fifos; /* FIFO memory */
115 int last_bfifo_cnt[2];
Joe Perches475be4d2012-02-19 19:52:38 -0800116 /* marker saving last b-fifo frame count */
Karsten Keil1700fe12008-07-26 18:55:28 +0200117 struct timer_list timer;
118};
119
120#define HFC_CFG_MASTER 1
121#define HFC_CFG_SLAVE 2
122#define HFC_CFG_PCM 3
123#define HFC_CFG_2HFC 4
124#define HFC_CFG_SLAVEHFC 5
125#define HFC_CFG_NEG_F0 6
126#define HFC_CFG_SW_DD_DU 7
127
128#define FLG_HFC_TIMER_T1 16
129#define FLG_HFC_TIMER_T3 17
130
131#define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */
132#define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */
133#define CLKDEL_TE 0x0e /* CLKDEL in TE mode */
134#define CLKDEL_NT 0x6c /* CLKDEL in NT mode */
135
136
137struct hfc_pci {
Karsten Keil1700fe12008-07-26 18:55:28 +0200138 u_char subtype;
139 u_char chanlimit;
140 u_char initdone;
141 u_long cfg;
142 u_int irq;
143 u_int irqcnt;
144 struct pci_dev *pdev;
145 struct hfcPCI_hw hw;
146 spinlock_t lock; /* card lock */
147 struct dchannel dch;
148 struct bchannel bch[2];
149};
150
151/* Interface functions */
152static void
153enable_hwirq(struct hfc_pci *hc)
154{
155 hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;
156 Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
157}
158
159static void
160disable_hwirq(struct hfc_pci *hc)
161{
162 hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);
163 Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
164}
165
166/*
167 * free hardware resources used by driver
168 */
169static void
170release_io_hfcpci(struct hfc_pci *hc)
171{
172 /* disable memory mapped ports + busmaster */
173 pci_write_config_word(hc->pdev, PCI_COMMAND, 0);
174 del_timer(&hc->hw.timer);
175 pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700176 iounmap(hc->hw.pci_io);
Karsten Keil1700fe12008-07-26 18:55:28 +0200177}
178
179/*
180 * set mode (NT or TE)
181 */
182static void
183hfcpci_setmode(struct hfc_pci *hc)
184{
185 if (hc->hw.protocol == ISDN_P_NT_S0) {
186 hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */
187 hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */
188 hc->hw.states = 1; /* G1 */
189 } else {
190 hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */
191 hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */
192 hc->hw.states = 2; /* F2 */
193 }
194 Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);
195 Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);
196 udelay(10);
197 Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */
198 Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
199}
200
201/*
202 * function called to reset the HFC PCI chip. A complete software reset of chip
203 * and fifos is done.
204 */
205static void
206reset_hfcpci(struct hfc_pci *hc)
207{
208 u_char val;
209 int cnt = 0;
210
211 printk(KERN_DEBUG "reset_hfcpci: entered\n");
212 val = Read_hfc(hc, HFCPCI_CHIP_ID);
213 printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);
214 /* enable memory mapped ports, disable busmaster */
215 pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
216 disable_hwirq(hc);
217 /* enable memory ports + busmaster */
218 pci_write_config_word(hc->pdev, PCI_COMMAND,
Joe Perches475be4d2012-02-19 19:52:38 -0800219 PCI_ENA_MEMIO + PCI_ENA_MASTER);
Karsten Keil1700fe12008-07-26 18:55:28 +0200220 val = Read_hfc(hc, HFCPCI_STATUS);
221 printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);
222 hc->hw.cirm = HFCPCI_RESET; /* Reset On */
223 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
224 set_current_state(TASK_UNINTERRUPTIBLE);
225 mdelay(10); /* Timeout 10ms */
226 hc->hw.cirm = 0; /* Reset Off */
227 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
228 val = Read_hfc(hc, HFCPCI_STATUS);
229 printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);
230 while (cnt < 50000) { /* max 50000 us */
231 udelay(5);
232 cnt += 5;
233 val = Read_hfc(hc, HFCPCI_STATUS);
234 if (!(val & 2))
235 break;
236 }
237 printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);
238
239 hc->hw.fifo_en = 0x30; /* only D fifos enabled */
240
241 hc->hw.bswapped = 0; /* no exchange */
242 hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;
243 hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */
244 hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */
245 hc->hw.sctrl_r = 0;
246 hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */
247 hc->hw.mst_m = 0;
248 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
249 hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */
250 if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))
251 hc->hw.mst_m |= HFCPCI_F0_NEGATIV;
252 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
253 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
254 Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
255 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
256
257 hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |
Joe Perches475be4d2012-02-19 19:52:38 -0800258 HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;
Karsten Keil1700fe12008-07-26 18:55:28 +0200259 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
260
261 /* Clear already pending ints */
Karsten Keileac74af2009-05-22 11:04:56 +0000262 val = Read_hfc(hc, HFCPCI_INT_S1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200263
264 /* set NT/TE mode */
265 hfcpci_setmode(hc);
266
267 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
268 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
269
270 /*
271 * Init GCI/IOM2 in master mode
272 * Slots 0 and 1 are set for B-chan 1 and 2
273 * D- and monitor/CI channel are not enabled
274 * STIO1 is used as output for data, B1+B2 from ST->IOM+HFC
275 * STIO2 is used as data input, B1+B2 from IOM->ST
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276 * ST B-channel send disabled -> continuous 1s
Karsten Keil1700fe12008-07-26 18:55:28 +0200277 * The IOM slots are always enabled
278 */
279 if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
280 /* set data flow directions: connect B1,B2: HFC to/from PCM */
281 hc->hw.conn = 0x09;
282 } else {
283 hc->hw.conn = 0x36; /* set data flow directions */
284 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
285 Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);
286 Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);
287 Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);
288 Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);
289 } else {
290 Write_hfc(hc, HFCPCI_B1_SSL, 0x80);
291 Write_hfc(hc, HFCPCI_B2_SSL, 0x81);
292 Write_hfc(hc, HFCPCI_B1_RSL, 0x80);
293 Write_hfc(hc, HFCPCI_B2_RSL, 0x81);
294 }
295 }
296 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
297 val = Read_hfc(hc, HFCPCI_INT_S2);
298}
299
300/*
301 * Timer function called when kernel timer expires
302 */
303static void
304hfcpci_Timer(struct hfc_pci *hc)
305{
306 hc->hw.timer.expires = jiffies + 75;
307 /* WD RESET */
308/*
309 * WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);
310 * add_timer(&hc->hw.timer);
311 */
312}
313
314
315/*
316 * select a b-channel entry matching and active
317 */
318static struct bchannel *
319Sel_BCS(struct hfc_pci *hc, int channel)
320{
321 if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800322 (hc->bch[0].nr & channel))
Karsten Keil1700fe12008-07-26 18:55:28 +0200323 return &hc->bch[0];
324 else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800325 (hc->bch[1].nr & channel))
Karsten Keil1700fe12008-07-26 18:55:28 +0200326 return &hc->bch[1];
327 else
328 return NULL;
329}
330
331/*
332 * clear the desired B-channel rx fifo
333 */
334static void
335hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)
336{
337 u_char fifo_state;
338 struct bzfifo *bzr;
339
340 if (fifo) {
341 bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
342 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;
343 } else {
344 bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
345 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;
346 }
347 if (fifo_state)
348 hc->hw.fifo_en ^= fifo_state;
349 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
350 hc->hw.last_bfifo_cnt[fifo] = 0;
351 bzr->f1 = MAX_B_FRAMES;
352 bzr->f2 = bzr->f1; /* init F pointers to remain constant */
353 bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
354 bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(
Joe Perches475be4d2012-02-19 19:52:38 -0800355 le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200356 if (fifo_state)
357 hc->hw.fifo_en |= fifo_state;
358 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
359}
360
361/*
362 * clear the desired B-channel tx fifo
363 */
364static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)
365{
366 u_char fifo_state;
367 struct bzfifo *bzt;
368
369 if (fifo) {
370 bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
371 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;
372 } else {
373 bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
374 fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;
375 }
376 if (fifo_state)
377 hc->hw.fifo_en ^= fifo_state;
378 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
379 if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
380 printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "
Joe Perches475be4d2012-02-19 19:52:38 -0800381 "z1(%x) z2(%x) state(%x)\n",
382 fifo, bzt->f1, bzt->f2,
383 le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
384 le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),
385 fifo_state);
Karsten Keil1700fe12008-07-26 18:55:28 +0200386 bzt->f2 = MAX_B_FRAMES;
387 bzt->f1 = bzt->f2; /* init F pointers to remain constant */
388 bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700389 bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 2);
Karsten Keil1700fe12008-07-26 18:55:28 +0200390 if (fifo_state)
391 hc->hw.fifo_en |= fifo_state;
392 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
393 if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
394 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800395 "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",
396 fifo, bzt->f1, bzt->f2,
397 le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
398 le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));
Karsten Keil1700fe12008-07-26 18:55:28 +0200399}
400
401/*
402 * read a complete B-frame out of the buffer
403 */
404static void
405hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,
Joe Perches475be4d2012-02-19 19:52:38 -0800406 u_char *bdata, int count)
Karsten Keil1700fe12008-07-26 18:55:28 +0200407{
408 u_char *ptr, *ptr1, new_f2;
David S. Millera719e0a2011-04-17 16:34:50 -0700409 int maxlen, new_z2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200410 struct zt *zp;
411
412 if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
413 printk(KERN_DEBUG "hfcpci_empty_fifo\n");
414 zp = &bz->za[bz->f2]; /* point to Z-Regs */
415 new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */
416 if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
417 new_z2 -= B_FIFO_SIZE; /* buffer wrap */
418 new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;
419 if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||
420 (*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {
421 if (bch->debug & DEBUG_HW)
422 printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "
Joe Perches475be4d2012-02-19 19:52:38 -0800423 "invalid length %d or crc\n", count);
Karsten Keil1700fe12008-07-26 18:55:28 +0200424#ifdef ERROR_STATISTIC
425 bch->err_inv++;
426#endif
427 bz->za[new_f2].z2 = cpu_to_le16(new_z2);
428 bz->f2 = new_f2; /* next buffer */
429 } else {
430 bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);
431 if (!bch->rx_skb) {
432 printk(KERN_WARNING "HFCPCI: receive out of memory\n");
433 return;
434 }
Karsten Keil1700fe12008-07-26 18:55:28 +0200435 count -= 3;
436 ptr = skb_put(bch->rx_skb, count);
437
438 if (le16_to_cpu(zp->z2) + count <= B_FIFO_SIZE + B_SUB_VAL)
439 maxlen = count; /* complete transfer */
440 else
441 maxlen = B_FIFO_SIZE + B_SUB_VAL -
Joe Perches475be4d2012-02-19 19:52:38 -0800442 le16_to_cpu(zp->z2); /* maximum */
Karsten Keil1700fe12008-07-26 18:55:28 +0200443
444 ptr1 = bdata + (le16_to_cpu(zp->z2) - B_SUB_VAL);
Joe Perches475be4d2012-02-19 19:52:38 -0800445 /* start of data */
Karsten Keil1700fe12008-07-26 18:55:28 +0200446 memcpy(ptr, ptr1, maxlen); /* copy data */
447 count -= maxlen;
448
449 if (count) { /* rest remaining */
450 ptr += maxlen;
451 ptr1 = bdata; /* start of buffer */
452 memcpy(ptr, ptr1, count); /* rest */
453 }
454 bz->za[new_f2].z2 = cpu_to_le16(new_z2);
455 bz->f2 = new_f2; /* next buffer */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000456 recv_Bchannel(bch, MISDN_ID_ANY);
Karsten Keil1700fe12008-07-26 18:55:28 +0200457 }
458}
459
460/*
461 * D-channel receive procedure
462 */
463static int
464receive_dmsg(struct hfc_pci *hc)
465{
466 struct dchannel *dch = &hc->dch;
467 int maxlen;
468 int rcnt, total;
469 int count = 5;
470 u_char *ptr, *ptr1;
471 struct dfifo *df;
472 struct zt *zp;
473
474 df = &((union fifo_area *)(hc->hw.fifos))->d_chan.d_rx;
475 while (((df->f1 & D_FREG_MASK) != (df->f2 & D_FREG_MASK)) && count--) {
476 zp = &df->za[df->f2 & D_FREG_MASK];
477 rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
478 if (rcnt < 0)
479 rcnt += D_FIFO_SIZE;
480 rcnt++;
481 if (dch->debug & DEBUG_HW_DCHANNEL)
482 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800483 "hfcpci recd f1(%d) f2(%d) z1(%x) z2(%x) cnt(%d)\n",
484 df->f1, df->f2,
485 le16_to_cpu(zp->z1),
486 le16_to_cpu(zp->z2),
487 rcnt);
Karsten Keil1700fe12008-07-26 18:55:28 +0200488
489 if ((rcnt > MAX_DFRAME_LEN + 3) || (rcnt < 4) ||
490 (df->data[le16_to_cpu(zp->z1)])) {
491 if (dch->debug & DEBUG_HW)
492 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800493 "empty_fifo hfcpci paket inv. len "
494 "%d or crc %d\n",
495 rcnt,
496 df->data[le16_to_cpu(zp->z1)]);
Karsten Keil1700fe12008-07-26 18:55:28 +0200497#ifdef ERROR_STATISTIC
498 cs->err_rx++;
499#endif
500 df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
Joe Perches475be4d2012-02-19 19:52:38 -0800501 (MAX_D_FRAMES + 1); /* next buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200502 df->za[df->f2 & D_FREG_MASK].z2 =
Joe Perches475be4d2012-02-19 19:52:38 -0800503 cpu_to_le16((le16_to_cpu(zp->z2) + rcnt) &
504 (D_FIFO_SIZE - 1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200505 } else {
506 dch->rx_skb = mI_alloc_skb(rcnt - 3, GFP_ATOMIC);
507 if (!dch->rx_skb) {
508 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -0800509 "HFC-PCI: D receive out of memory\n");
Karsten Keil1700fe12008-07-26 18:55:28 +0200510 break;
511 }
512 total = rcnt;
513 rcnt -= 3;
514 ptr = skb_put(dch->rx_skb, rcnt);
515
516 if (le16_to_cpu(zp->z2) + rcnt <= D_FIFO_SIZE)
517 maxlen = rcnt; /* complete transfer */
518 else
519 maxlen = D_FIFO_SIZE - le16_to_cpu(zp->z2);
Joe Perches475be4d2012-02-19 19:52:38 -0800520 /* maximum */
Karsten Keil1700fe12008-07-26 18:55:28 +0200521
522 ptr1 = df->data + le16_to_cpu(zp->z2);
Joe Perches475be4d2012-02-19 19:52:38 -0800523 /* start of data */
Karsten Keil1700fe12008-07-26 18:55:28 +0200524 memcpy(ptr, ptr1, maxlen); /* copy data */
525 rcnt -= maxlen;
526
527 if (rcnt) { /* rest remaining */
528 ptr += maxlen;
529 ptr1 = df->data; /* start of buffer */
530 memcpy(ptr, ptr1, rcnt); /* rest */
531 }
532 df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
Joe Perches475be4d2012-02-19 19:52:38 -0800533 (MAX_D_FRAMES + 1); /* next buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200534 df->za[df->f2 & D_FREG_MASK].z2 = cpu_to_le16((
Joe Perches475be4d2012-02-19 19:52:38 -0800535 le16_to_cpu(zp->z2) + total) & (D_FIFO_SIZE - 1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200536 recv_Dchannel(dch);
537 }
538 }
539 return 1;
540}
541
542/*
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200543 * check for transparent receive data and read max one 'poll' size if avail
Karsten Keil1700fe12008-07-26 18:55:28 +0200544 */
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200545static void
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000546hfcpci_empty_fifo_trans(struct bchannel *bch, struct bzfifo *rxbz,
Joe Perches475be4d2012-02-19 19:52:38 -0800547 struct bzfifo *txbz, u_char *bdata)
Karsten Keil1700fe12008-07-26 18:55:28 +0200548{
Joe Perches475be4d2012-02-19 19:52:38 -0800549 __le16 *z1r, *z2r, *z1t, *z2t;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000550 int new_z2, fcnt_rx, fcnt_tx, maxlen;
551 u_char *ptr, *ptr1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200552
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000553 z1r = &rxbz->za[MAX_B_FRAMES].z1; /* pointer to z reg */
Karsten Keil1700fe12008-07-26 18:55:28 +0200554 z2r = z1r + 1;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000555 z1t = &txbz->za[MAX_B_FRAMES].z1;
556 z2t = z1t + 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200557
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000558 fcnt_rx = le16_to_cpu(*z1r) - le16_to_cpu(*z2r);
559 if (!fcnt_rx)
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200560 return; /* no data avail */
Karsten Keil1700fe12008-07-26 18:55:28 +0200561
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000562 if (fcnt_rx <= 0)
563 fcnt_rx += B_FIFO_SIZE; /* bytes actually buffered */
564 new_z2 = le16_to_cpu(*z2r) + fcnt_rx; /* new position in fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200565 if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
566 new_z2 -= B_FIFO_SIZE; /* buffer wrap */
567
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000568 if (fcnt_rx > MAX_DATA_SIZE) { /* flush, if oversized */
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200569 *z2r = cpu_to_le16(new_z2); /* new position */
570 return;
571 }
572
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000573 fcnt_tx = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
574 if (fcnt_tx <= 0)
575 fcnt_tx += B_FIFO_SIZE;
Joe Perches475be4d2012-02-19 19:52:38 -0800576 /* fcnt_tx contains available bytes in tx-fifo */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000577 fcnt_tx = B_FIFO_SIZE - fcnt_tx;
Joe Perches475be4d2012-02-19 19:52:38 -0800578 /* remaining bytes to send (bytes in tx-fifo) */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000579
580 bch->rx_skb = mI_alloc_skb(fcnt_rx, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +0200581 if (bch->rx_skb) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000582 ptr = skb_put(bch->rx_skb, fcnt_rx);
583 if (le16_to_cpu(*z2r) + fcnt_rx <= B_FIFO_SIZE + B_SUB_VAL)
584 maxlen = fcnt_rx; /* complete transfer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200585 else
586 maxlen = B_FIFO_SIZE + B_SUB_VAL - le16_to_cpu(*z2r);
Joe Perches475be4d2012-02-19 19:52:38 -0800587 /* maximum */
Karsten Keil1700fe12008-07-26 18:55:28 +0200588
589 ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);
Joe Perches475be4d2012-02-19 19:52:38 -0800590 /* start of data */
Karsten Keil1700fe12008-07-26 18:55:28 +0200591 memcpy(ptr, ptr1, maxlen); /* copy data */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000592 fcnt_rx -= maxlen;
Karsten Keil1700fe12008-07-26 18:55:28 +0200593
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000594 if (fcnt_rx) { /* rest remaining */
Karsten Keil1700fe12008-07-26 18:55:28 +0200595 ptr += maxlen;
596 ptr1 = bdata; /* start of buffer */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000597 memcpy(ptr, ptr1, fcnt_rx); /* rest */
Karsten Keil1700fe12008-07-26 18:55:28 +0200598 }
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000599 recv_Bchannel(bch, fcnt_tx); /* bch, id */
Karsten Keil1700fe12008-07-26 18:55:28 +0200600 } else
601 printk(KERN_WARNING "HFCPCI: receive out of memory\n");
602
603 *z2r = cpu_to_le16(new_z2); /* new position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200604}
605
606/*
607 * B-channel main receive routine
608 */
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700609static void
Karsten Keil1700fe12008-07-26 18:55:28 +0200610main_rec_hfcpci(struct bchannel *bch)
611{
612 struct hfc_pci *hc = bch->hw;
613 int rcnt, real_fifo;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200614 int receive = 0, count = 5;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000615 struct bzfifo *txbz, *rxbz;
Karsten Keil1700fe12008-07-26 18:55:28 +0200616 u_char *bdata;
617 struct zt *zp;
618
Karsten Keil1700fe12008-07-26 18:55:28 +0200619 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000620 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
621 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200622 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;
623 real_fifo = 1;
624 } else {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000625 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
626 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200627 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;
628 real_fifo = 0;
629 }
630Begin:
631 count--;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000632 if (rxbz->f1 != rxbz->f2) {
Karsten Keil1700fe12008-07-26 18:55:28 +0200633 if (bch->debug & DEBUG_HW_BCHANNEL)
634 printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800635 bch->nr, rxbz->f1, rxbz->f2);
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000636 zp = &rxbz->za[rxbz->f2];
Karsten Keil1700fe12008-07-26 18:55:28 +0200637
638 rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
639 if (rcnt < 0)
640 rcnt += B_FIFO_SIZE;
641 rcnt++;
642 if (bch->debug & DEBUG_HW_BCHANNEL)
643 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800644 "hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",
645 bch->nr, le16_to_cpu(zp->z1),
646 le16_to_cpu(zp->z2), rcnt);
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000647 hfcpci_empty_bfifo(bch, rxbz, bdata, rcnt);
648 rcnt = rxbz->f1 - rxbz->f2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200649 if (rcnt < 0)
650 rcnt += MAX_B_FRAMES + 1;
651 if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {
652 rcnt = 0;
653 hfcpci_clear_fifo_rx(hc, real_fifo);
654 }
655 hc->hw.last_bfifo_cnt[real_fifo] = rcnt;
656 if (rcnt > 1)
657 receive = 1;
658 else
659 receive = 0;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200660 } else if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000661 hfcpci_empty_fifo_trans(bch, rxbz, txbz, bdata);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200662 return;
663 } else
Karsten Keil1700fe12008-07-26 18:55:28 +0200664 receive = 0;
665 if (count && receive)
666 goto Begin;
667
668}
669
670/*
671 * D-channel send routine
672 */
673static void
674hfcpci_fill_dfifo(struct hfc_pci *hc)
675{
676 struct dchannel *dch = &hc->dch;
677 int fcnt;
678 int count, new_z1, maxlen;
679 struct dfifo *df;
680 u_char *src, *dst, new_f1;
681
682 if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))
683 printk(KERN_DEBUG "%s\n", __func__);
684
685 if (!dch->tx_skb)
686 return;
687 count = dch->tx_skb->len - dch->tx_idx;
688 if (count <= 0)
689 return;
690 df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;
691
692 if (dch->debug & DEBUG_HW_DFIFO)
693 printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800694 df->f1, df->f2,
695 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200696 fcnt = df->f1 - df->f2; /* frame count actually buffered */
697 if (fcnt < 0)
698 fcnt += (MAX_D_FRAMES + 1); /* if wrap around */
699 if (fcnt > (MAX_D_FRAMES - 1)) {
700 if (dch->debug & DEBUG_HW_DCHANNEL)
701 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800702 "hfcpci_fill_Dfifo more as 14 frames\n");
Karsten Keil1700fe12008-07-26 18:55:28 +0200703#ifdef ERROR_STATISTIC
704 cs->err_tx++;
705#endif
706 return;
707 }
708 /* now determine free bytes in FIFO buffer */
709 maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -
Joe Perches475be4d2012-02-19 19:52:38 -0800710 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200711 if (maxlen <= 0)
712 maxlen += D_FIFO_SIZE; /* count now contains available bytes */
713
714 if (dch->debug & DEBUG_HW_DCHANNEL)
715 printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800716 count, maxlen);
Karsten Keil1700fe12008-07-26 18:55:28 +0200717 if (count > maxlen) {
718 if (dch->debug & DEBUG_HW_DCHANNEL)
719 printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");
720 return;
721 }
722 new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &
Joe Perches475be4d2012-02-19 19:52:38 -0800723 (D_FIFO_SIZE - 1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200724 new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);
725 src = dch->tx_skb->data + dch->tx_idx; /* source pointer */
726 dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
727 maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800728 /* end fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200729 if (maxlen > count)
730 maxlen = count; /* limit size */
731 memcpy(dst, src, maxlen); /* first copy */
732
733 count -= maxlen; /* remaining bytes */
734 if (count) {
735 dst = df->data; /* start of buffer */
736 src += maxlen; /* new position */
737 memcpy(dst, src, count);
738 }
739 df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800740 /* for next buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200741 df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800742 /* new pos actual buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200743 df->f1 = new_f1; /* next frame */
744 dch->tx_idx = dch->tx_skb->len;
745}
746
747/*
748 * B-channel send routine
749 */
750static void
751hfcpci_fill_fifo(struct bchannel *bch)
752{
Joe Perches475be4d2012-02-19 19:52:38 -0800753 struct hfc_pci *hc = bch->hw;
Karsten Keil1700fe12008-07-26 18:55:28 +0200754 int maxlen, fcnt;
755 int count, new_z1;
756 struct bzfifo *bz;
757 u_char *bdata;
758 u_char new_f1, *src, *dst;
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700759 __le16 *z1t, *z2t;
Karsten Keil1700fe12008-07-26 18:55:28 +0200760
761 if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
762 printk(KERN_DEBUG "%s\n", __func__);
763 if ((!bch->tx_skb) || bch->tx_skb->len <= 0)
764 return;
765 count = bch->tx_skb->len - bch->tx_idx;
766 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
767 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
768 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;
769 } else {
770 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
771 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;
772 }
773
774 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
775 z1t = &bz->za[MAX_B_FRAMES].z1;
776 z2t = z1t + 1;
777 if (bch->debug & DEBUG_HW_BCHANNEL)
778 printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "
Joe Perches475be4d2012-02-19 19:52:38 -0800779 "cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,
780 le16_to_cpu(*z1t), le16_to_cpu(*z2t));
Karsten Keil1700fe12008-07-26 18:55:28 +0200781 fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
782 if (fcnt <= 0)
783 fcnt += B_FIFO_SIZE;
Joe Perches475be4d2012-02-19 19:52:38 -0800784 /* fcnt contains available bytes in fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200785 fcnt = B_FIFO_SIZE - fcnt;
Joe Perches475be4d2012-02-19 19:52:38 -0800786 /* remaining bytes to send (bytes in fifo) */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200787
788 /* "fill fifo if empty" feature */
789 if (test_bit(FLG_FILLEMPTY, &bch->Flags) && !fcnt) {
790 /* printk(KERN_DEBUG "%s: buffer empty, so we have "
Joe Perches475be4d2012-02-19 19:52:38 -0800791 "underrun\n", __func__); */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200792 /* fill buffer, to prevent future underrun */
793 count = HFCPCI_FILLEMPTY;
794 new_z1 = le16_to_cpu(*z1t) + count;
Joe Perches475be4d2012-02-19 19:52:38 -0800795 /* new buffer Position */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200796 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
797 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
798 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
799 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
Joe Perches475be4d2012-02-19 19:52:38 -0800800 /* end of fifo */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200801 if (bch->debug & DEBUG_HW_BFIFO)
802 printk(KERN_DEBUG "hfcpci_FFt fillempty "
Joe Perches475be4d2012-02-19 19:52:38 -0800803 "fcnt(%d) maxl(%d) nz1(%x) dst(%p)\n",
804 fcnt, maxlen, new_z1, dst);
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200805 fcnt += count;
806 if (maxlen > count)
Joe Perches475be4d2012-02-19 19:52:38 -0800807 maxlen = count; /* limit size */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200808 memset(dst, 0x2a, maxlen); /* first copy */
809 count -= maxlen; /* remaining bytes */
810 if (count) {
811 dst = bdata; /* start of buffer */
812 memset(dst, 0x2a, count);
813 }
814 *z1t = cpu_to_le16(new_z1); /* now send data */
815 }
816
Joe Perches475be4d2012-02-19 19:52:38 -0800817 next_t_frame:
Karsten Keil1700fe12008-07-26 18:55:28 +0200818 count = bch->tx_skb->len - bch->tx_idx;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200819 /* maximum fill shall be poll*2 */
820 if (count > (poll << 1) - fcnt)
821 count = (poll << 1) - fcnt;
Karsten Keil1700fe12008-07-26 18:55:28 +0200822 if (count <= 0)
823 return;
824 /* data is suitable for fifo */
825 new_z1 = le16_to_cpu(*z1t) + count;
Joe Perches475be4d2012-02-19 19:52:38 -0800826 /* new buffer Position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200827 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
828 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
829 src = bch->tx_skb->data + bch->tx_idx;
Joe Perches475be4d2012-02-19 19:52:38 -0800830 /* source pointer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200831 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
832 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
Joe Perches475be4d2012-02-19 19:52:38 -0800833 /* end of fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200834 if (bch->debug & DEBUG_HW_BFIFO)
835 printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "
Joe Perches475be4d2012-02-19 19:52:38 -0800836 "maxl(%d) nz1(%x) dst(%p)\n",
837 fcnt, maxlen, new_z1, dst);
Karsten Keil1700fe12008-07-26 18:55:28 +0200838 fcnt += count;
839 bch->tx_idx += count;
840 if (maxlen > count)
841 maxlen = count; /* limit size */
842 memcpy(dst, src, maxlen); /* first copy */
843 count -= maxlen; /* remaining bytes */
844 if (count) {
845 dst = bdata; /* start of buffer */
846 src += maxlen; /* new position */
847 memcpy(dst, src, count);
848 }
849 *z1t = cpu_to_le16(new_z1); /* now send data */
850 if (bch->tx_idx < bch->tx_skb->len)
851 return;
Karsten Keil1700fe12008-07-26 18:55:28 +0200852 dev_kfree_skb(bch->tx_skb);
853 if (get_next_bframe(bch))
854 goto next_t_frame;
855 return;
856 }
857 if (bch->debug & DEBUG_HW_BCHANNEL)
858 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800859 "%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",
860 __func__, bch->nr, bz->f1, bz->f2,
861 bz->za[bz->f1].z1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200862 fcnt = bz->f1 - bz->f2; /* frame count actually buffered */
863 if (fcnt < 0)
864 fcnt += (MAX_B_FRAMES + 1); /* if wrap around */
865 if (fcnt > (MAX_B_FRAMES - 1)) {
866 if (bch->debug & DEBUG_HW_BCHANNEL)
867 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800868 "hfcpci_fill_Bfifo more as 14 frames\n");
Karsten Keil1700fe12008-07-26 18:55:28 +0200869 return;
870 }
871 /* now determine free bytes in FIFO buffer */
872 maxlen = le16_to_cpu(bz->za[bz->f2].z2) -
Joe Perches475be4d2012-02-19 19:52:38 -0800873 le16_to_cpu(bz->za[bz->f1].z1) - 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200874 if (maxlen <= 0)
875 maxlen += B_FIFO_SIZE; /* count now contains available bytes */
876
877 if (bch->debug & DEBUG_HW_BCHANNEL)
878 printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800879 bch->nr, count, maxlen);
Karsten Keil1700fe12008-07-26 18:55:28 +0200880
881 if (maxlen < count) {
882 if (bch->debug & DEBUG_HW_BCHANNEL)
883 printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");
884 return;
885 }
886 new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;
Joe Perches475be4d2012-02-19 19:52:38 -0800887 /* new buffer Position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200888 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
889 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
890
891 new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);
892 src = bch->tx_skb->data + bch->tx_idx; /* source pointer */
893 dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);
894 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800895 /* end fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200896 if (maxlen > count)
897 maxlen = count; /* limit size */
898 memcpy(dst, src, maxlen); /* first copy */
899
900 count -= maxlen; /* remaining bytes */
901 if (count) {
902 dst = bdata; /* start of buffer */
903 src += maxlen; /* new position */
904 memcpy(dst, src, count);
905 }
906 bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */
907 bz->f1 = new_f1; /* next frame */
908 dev_kfree_skb(bch->tx_skb);
909 get_next_bframe(bch);
910}
911
912
913
914/*
915 * handle L1 state changes TE
916 */
917
918static void
919ph_state_te(struct dchannel *dch)
920{
921 if (dch->debug)
922 printk(KERN_DEBUG "%s: TE newstate %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800923 __func__, dch->state);
Karsten Keil1700fe12008-07-26 18:55:28 +0200924 switch (dch->state) {
925 case 0:
926 l1_event(dch->l1, HW_RESET_IND);
927 break;
928 case 3:
929 l1_event(dch->l1, HW_DEACT_IND);
930 break;
931 case 5:
932 case 8:
933 l1_event(dch->l1, ANYSIGNAL);
934 break;
935 case 6:
936 l1_event(dch->l1, INFO2);
937 break;
938 case 7:
939 l1_event(dch->l1, INFO4_P8);
940 break;
941 }
942}
943
944/*
945 * handle L1 state changes NT
946 */
947
948static void
949handle_nt_timer3(struct dchannel *dch) {
950 struct hfc_pci *hc = dch->hw;
951
952 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
953 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
954 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
955 hc->hw.nt_timer = 0;
956 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
957 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
958 hc->hw.mst_m |= HFCPCI_MASTER;
959 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
960 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -0800961 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +0200962}
963
964static void
965ph_state_nt(struct dchannel *dch)
966{
967 struct hfc_pci *hc = dch->hw;
968
969 if (dch->debug)
970 printk(KERN_DEBUG "%s: NT newstate %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800971 __func__, dch->state);
Karsten Keil1700fe12008-07-26 18:55:28 +0200972 switch (dch->state) {
973 case 2:
974 if (hc->hw.nt_timer < 0) {
975 hc->hw.nt_timer = 0;
976 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
977 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
978 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
979 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
980 /* Clear already pending ints */
David S. Millera719e0a2011-04-17 16:34:50 -0700981 (void) Read_hfc(hc, HFCPCI_INT_S1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200982 Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);
983 udelay(10);
984 Write_hfc(hc, HFCPCI_STATES, 4);
985 dch->state = 4;
986 } else if (hc->hw.nt_timer == 0) {
987 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
988 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
989 hc->hw.nt_timer = NT_T1_COUNT;
990 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
991 hc->hw.ctmt |= HFCPCI_TIM3_125;
992 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
Joe Perches475be4d2012-02-19 19:52:38 -0800993 HFCPCI_CLTIMER);
Karsten Keil1700fe12008-07-26 18:55:28 +0200994 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
995 test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);
996 /* allow G2 -> G3 transition */
997 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
998 } else {
999 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
1000 }
1001 break;
1002 case 1:
1003 hc->hw.nt_timer = 0;
1004 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1005 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1006 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1007 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1008 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1009 hc->hw.mst_m &= ~HFCPCI_MASTER;
1010 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1011 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1012 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -08001013 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001014 break;
1015 case 4:
1016 hc->hw.nt_timer = 0;
1017 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1018 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1019 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1020 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1021 break;
1022 case 3:
1023 if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {
1024 if (!test_and_clear_bit(FLG_L2_ACTIVATED,
Joe Perches475be4d2012-02-19 19:52:38 -08001025 &dch->Flags)) {
Karsten Keil1700fe12008-07-26 18:55:28 +02001026 handle_nt_timer3(dch);
1027 break;
1028 }
1029 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1030 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
1031 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1032 hc->hw.nt_timer = NT_T3_COUNT;
1033 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
1034 hc->hw.ctmt |= HFCPCI_TIM3_125;
1035 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
Joe Perches475be4d2012-02-19 19:52:38 -08001036 HFCPCI_CLTIMER);
Karsten Keil1700fe12008-07-26 18:55:28 +02001037 }
1038 break;
1039 }
1040}
1041
1042static void
1043ph_state(struct dchannel *dch)
1044{
1045 struct hfc_pci *hc = dch->hw;
1046
1047 if (hc->hw.protocol == ISDN_P_NT_S0) {
1048 if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&
1049 hc->hw.nt_timer < 0)
1050 handle_nt_timer3(dch);
1051 else
1052 ph_state_nt(dch);
1053 } else
1054 ph_state_te(dch);
1055}
1056
1057/*
1058 * Layer 1 callback function
1059 */
1060static int
1061hfc_l1callback(struct dchannel *dch, u_int cmd)
1062{
1063 struct hfc_pci *hc = dch->hw;
1064
1065 switch (cmd) {
1066 case INFO3_P8:
1067 case INFO3_P10:
1068 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1069 hc->hw.mst_m |= HFCPCI_MASTER;
1070 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1071 break;
1072 case HW_RESET_REQ:
1073 Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);
1074 /* HFC ST 3 */
1075 udelay(6);
1076 Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */
1077 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1078 hc->hw.mst_m |= HFCPCI_MASTER;
1079 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1080 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -08001081 HFCPCI_DO_ACTION);
Karsten Keil1700fe12008-07-26 18:55:28 +02001082 l1_event(dch->l1, HW_POWERUP_IND);
1083 break;
1084 case HW_DEACT_REQ:
1085 hc->hw.mst_m &= ~HFCPCI_MASTER;
1086 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1087 skb_queue_purge(&dch->squeue);
1088 if (dch->tx_skb) {
1089 dev_kfree_skb(dch->tx_skb);
1090 dch->tx_skb = NULL;
1091 }
1092 dch->tx_idx = 0;
1093 if (dch->rx_skb) {
1094 dev_kfree_skb(dch->rx_skb);
1095 dch->rx_skb = NULL;
1096 }
1097 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1098 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1099 del_timer(&dch->timer);
1100 break;
1101 case HW_POWERUP_REQ:
1102 Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);
1103 break;
1104 case PH_ACTIVATE_IND:
1105 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
1106 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
Joe Perches475be4d2012-02-19 19:52:38 -08001107 GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001108 break;
1109 case PH_DEACTIVATE_IND:
1110 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1111 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
Joe Perches475be4d2012-02-19 19:52:38 -08001112 GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001113 break;
1114 default:
1115 if (dch->debug & DEBUG_HW)
1116 printk(KERN_DEBUG "%s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001117 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02001118 return -1;
1119 }
1120 return 0;
1121}
1122
1123/*
1124 * Interrupt handler
1125 */
1126static inline void
1127tx_birq(struct bchannel *bch)
1128{
1129 if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
1130 hfcpci_fill_fifo(bch);
1131 else {
1132 if (bch->tx_skb)
1133 dev_kfree_skb(bch->tx_skb);
1134 if (get_next_bframe(bch))
1135 hfcpci_fill_fifo(bch);
1136 }
1137}
1138
1139static inline void
1140tx_dirq(struct dchannel *dch)
1141{
1142 if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)
1143 hfcpci_fill_dfifo(dch->hw);
1144 else {
1145 if (dch->tx_skb)
1146 dev_kfree_skb(dch->tx_skb);
1147 if (get_next_dframe(dch))
1148 hfcpci_fill_dfifo(dch->hw);
1149 }
1150}
1151
1152static irqreturn_t
1153hfcpci_int(int intno, void *dev_id)
1154{
1155 struct hfc_pci *hc = dev_id;
1156 u_char exval;
1157 struct bchannel *bch;
1158 u_char val, stat;
1159
1160 spin_lock(&hc->lock);
1161 if (!(hc->hw.int_m2 & 0x08)) {
1162 spin_unlock(&hc->lock);
1163 return IRQ_NONE; /* not initialised */
1164 }
1165 stat = Read_hfc(hc, HFCPCI_STATUS);
1166 if (HFCPCI_ANYINT & stat) {
1167 val = Read_hfc(hc, HFCPCI_INT_S1);
1168 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1169 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001170 "HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);
Karsten Keil1700fe12008-07-26 18:55:28 +02001171 } else {
1172 /* shared */
1173 spin_unlock(&hc->lock);
1174 return IRQ_NONE;
1175 }
1176 hc->irqcnt++;
1177
1178 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1179 printk(KERN_DEBUG "HFC-PCI irq %x\n", val);
1180 val &= hc->hw.int_m1;
1181 if (val & 0x40) { /* state machine irq */
1182 exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;
1183 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1184 printk(KERN_DEBUG "ph_state chg %d->%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001185 hc->dch.state, exval);
Karsten Keil1700fe12008-07-26 18:55:28 +02001186 hc->dch.state = exval;
1187 schedule_event(&hc->dch, FLG_PHCHANGE);
1188 val &= ~0x40;
1189 }
1190 if (val & 0x80) { /* timer irq */
1191 if (hc->hw.protocol == ISDN_P_NT_S0) {
1192 if ((--hc->hw.nt_timer) < 0)
1193 schedule_event(&hc->dch, FLG_PHCHANGE);
1194 }
1195 val &= ~0x80;
1196 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);
1197 }
Joe Perches475be4d2012-02-19 19:52:38 -08001198 if (val & 0x08) { /* B1 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001199 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1200 if (bch)
1201 main_rec_hfcpci(bch);
1202 else if (hc->dch.debug)
1203 printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");
1204 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001205 if (val & 0x10) { /* B2 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001206 bch = Sel_BCS(hc, 2);
1207 if (bch)
1208 main_rec_hfcpci(bch);
1209 else if (hc->dch.debug)
1210 printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");
1211 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001212 if (val & 0x01) { /* B1 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001213 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1214 if (bch)
1215 tx_birq(bch);
1216 else if (hc->dch.debug)
1217 printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");
1218 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001219 if (val & 0x02) { /* B2 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001220 bch = Sel_BCS(hc, 2);
1221 if (bch)
1222 tx_birq(bch);
1223 else if (hc->dch.debug)
1224 printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");
1225 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001226 if (val & 0x20) /* D rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001227 receive_dmsg(hc);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001228 if (val & 0x04) { /* D tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001229 if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))
1230 del_timer(&hc->dch.timer);
1231 tx_dirq(&hc->dch);
1232 }
1233 spin_unlock(&hc->lock);
1234 return IRQ_HANDLED;
1235}
1236
1237/*
1238 * timer callback for D-chan busy resolution. Currently no function
1239 */
1240static void
1241hfcpci_dbusy_timer(struct hfc_pci *hc)
1242{
1243}
1244
1245/*
1246 * activate/deactivate hardware for selected channels and mode
1247 */
1248static int
1249mode_hfcpci(struct bchannel *bch, int bc, int protocol)
1250{
1251 struct hfc_pci *hc = bch->hw;
1252 int fifo2;
1253 u_char rx_slot = 0, tx_slot = 0, pcm_mode;
1254
1255 if (bch->debug & DEBUG_HW_BCHANNEL)
1256 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001257 "HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",
1258 bch->state, protocol, bch->nr, bc);
Karsten Keil1700fe12008-07-26 18:55:28 +02001259
1260 fifo2 = bc;
Joe Perches475be4d2012-02-19 19:52:38 -08001261 pcm_mode = (bc >> 24) & 0xff;
Karsten Keil1700fe12008-07-26 18:55:28 +02001262 if (pcm_mode) { /* PCM SLOT USE */
1263 if (!test_bit(HFC_CFG_PCM, &hc->cfg))
1264 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001265 "%s: pcm channel id without HFC_CFG_PCM\n",
1266 __func__);
1267 rx_slot = (bc >> 8) & 0xff;
1268 tx_slot = (bc >> 16) & 0xff;
Karsten Keil1700fe12008-07-26 18:55:28 +02001269 bc = bc & 0xff;
Karsten Keileac74af2009-05-22 11:04:56 +00001270 } else if (test_bit(HFC_CFG_PCM, &hc->cfg) && (protocol > ISDN_P_NONE))
Karsten Keil1700fe12008-07-26 18:55:28 +02001271 printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001272 __func__);
Karsten Keil1700fe12008-07-26 18:55:28 +02001273 if (hc->chanlimit > 1) {
1274 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1275 hc->hw.sctrl_e &= ~0x80;
1276 } else {
1277 if (bc & 2) {
1278 if (protocol != ISDN_P_NONE) {
1279 hc->hw.bswapped = 1; /* B1 and B2 exchanged */
1280 hc->hw.sctrl_e |= 0x80;
1281 } else {
1282 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1283 hc->hw.sctrl_e &= ~0x80;
1284 }
1285 fifo2 = 1;
1286 } else {
1287 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1288 hc->hw.sctrl_e &= ~0x80;
1289 }
1290 }
1291 switch (protocol) {
1292 case (-1): /* used for init */
1293 bch->state = -1;
1294 bch->nr = bc;
1295 case (ISDN_P_NONE):
1296 if (bch->state == ISDN_P_NONE)
1297 return 0;
1298 if (bc & 2) {
1299 hc->hw.sctrl &= ~SCTRL_B2_ENA;
1300 hc->hw.sctrl_r &= ~SCTRL_B2_ENA;
1301 } else {
1302 hc->hw.sctrl &= ~SCTRL_B1_ENA;
1303 hc->hw.sctrl_r &= ~SCTRL_B1_ENA;
1304 }
1305 if (fifo2 & 2) {
1306 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;
1307 hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001308 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001309 } else {
1310 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;
1311 hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001312 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001313 }
1314#ifdef REVERSE_BITORDER
1315 if (bch->nr & 2)
1316 hc->hw.cirm &= 0x7f;
1317 else
1318 hc->hw.cirm &= 0xbf;
1319#endif
1320 bch->state = ISDN_P_NONE;
1321 bch->nr = bc;
1322 test_and_clear_bit(FLG_HDLC, &bch->Flags);
1323 test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
1324 break;
1325 case (ISDN_P_B_RAW):
1326 bch->state = protocol;
1327 bch->nr = bc;
Karsten Keileac74af2009-05-22 11:04:56 +00001328 hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
1329 hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001330 if (bc & 2) {
1331 hc->hw.sctrl |= SCTRL_B2_ENA;
1332 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1333#ifdef REVERSE_BITORDER
1334 hc->hw.cirm |= 0x80;
1335#endif
1336 } else {
1337 hc->hw.sctrl |= SCTRL_B1_ENA;
1338 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1339#ifdef REVERSE_BITORDER
1340 hc->hw.cirm |= 0x40;
1341#endif
1342 }
1343 if (fifo2 & 2) {
1344 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001345 if (!tics)
1346 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001347 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001348 hc->hw.ctmt |= 2;
1349 hc->hw.conn &= ~0x18;
1350 } else {
1351 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001352 if (!tics)
1353 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001354 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001355 hc->hw.ctmt |= 1;
1356 hc->hw.conn &= ~0x03;
1357 }
1358 test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
1359 break;
1360 case (ISDN_P_B_HDLC):
1361 bch->state = protocol;
1362 bch->nr = bc;
Karsten Keileac74af2009-05-22 11:04:56 +00001363 hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
1364 hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001365 if (bc & 2) {
1366 hc->hw.sctrl |= SCTRL_B2_ENA;
1367 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1368 } else {
1369 hc->hw.sctrl |= SCTRL_B1_ENA;
1370 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1371 }
1372 if (fifo2 & 2) {
1373 hc->hw.last_bfifo_cnt[1] = 0;
1374 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
1375 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001376 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001377 hc->hw.ctmt &= ~2;
1378 hc->hw.conn &= ~0x18;
1379 } else {
1380 hc->hw.last_bfifo_cnt[0] = 0;
1381 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
1382 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001383 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001384 hc->hw.ctmt &= ~1;
1385 hc->hw.conn &= ~0x03;
1386 }
1387 test_and_set_bit(FLG_HDLC, &bch->Flags);
1388 break;
1389 default:
1390 printk(KERN_DEBUG "prot not known %x\n", protocol);
1391 return -ENOPROTOOPT;
1392 }
1393 if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
1394 if ((protocol == ISDN_P_NONE) ||
Joe Perches475be4d2012-02-19 19:52:38 -08001395 (protocol == -1)) { /* init case */
Karsten Keil1700fe12008-07-26 18:55:28 +02001396 rx_slot = 0;
1397 tx_slot = 0;
1398 } else {
1399 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
1400 rx_slot |= 0xC0;
1401 tx_slot |= 0xC0;
1402 } else {
1403 rx_slot |= 0x80;
1404 tx_slot |= 0x80;
1405 }
1406 }
1407 if (bc & 2) {
1408 hc->hw.conn &= 0xc7;
1409 hc->hw.conn |= 0x08;
1410 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001411 __func__, tx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001412 printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001413 __func__, rx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001414 Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);
1415 Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);
1416 } else {
1417 hc->hw.conn &= 0xf8;
1418 hc->hw.conn |= 0x01;
1419 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001420 __func__, tx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001421 printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001422 __func__, rx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001423 Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);
1424 Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);
1425 }
1426 }
1427 Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
1428 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1429 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1430 Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
1431 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1432 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1433 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1434#ifdef REVERSE_BITORDER
1435 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1436#endif
1437 return 0;
1438}
1439
1440static int
1441set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)
1442{
1443 struct hfc_pci *hc = bch->hw;
1444
1445 if (bch->debug & DEBUG_HW_BCHANNEL)
1446 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001447 "HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",
1448 bch->state, protocol, bch->nr, chan);
Karsten Keil1700fe12008-07-26 18:55:28 +02001449 if (bch->nr != chan) {
1450 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001451 "HFCPCI rxtest wrong channel parameter %x/%x\n",
1452 bch->nr, chan);
Karsten Keil1700fe12008-07-26 18:55:28 +02001453 return -EINVAL;
1454 }
1455 switch (protocol) {
1456 case (ISDN_P_B_RAW):
1457 bch->state = protocol;
Karsten Keileac74af2009-05-22 11:04:56 +00001458 hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001459 if (chan & 2) {
1460 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1461 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001462 if (!tics)
1463 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001464 hc->hw.ctmt |= 2;
1465 hc->hw.conn &= ~0x18;
1466#ifdef REVERSE_BITORDER
1467 hc->hw.cirm |= 0x80;
1468#endif
1469 } else {
1470 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1471 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001472 if (!tics)
1473 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001474 hc->hw.ctmt |= 1;
1475 hc->hw.conn &= ~0x03;
1476#ifdef REVERSE_BITORDER
1477 hc->hw.cirm |= 0x40;
1478#endif
1479 }
1480 break;
1481 case (ISDN_P_B_HDLC):
1482 bch->state = protocol;
Karsten Keileac74af2009-05-22 11:04:56 +00001483 hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001484 if (chan & 2) {
1485 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1486 hc->hw.last_bfifo_cnt[1] = 0;
1487 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
1488 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
1489 hc->hw.ctmt &= ~2;
1490 hc->hw.conn &= ~0x18;
1491 } else {
1492 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1493 hc->hw.last_bfifo_cnt[0] = 0;
1494 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
1495 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
1496 hc->hw.ctmt &= ~1;
1497 hc->hw.conn &= ~0x03;
1498 }
1499 break;
1500 default:
1501 printk(KERN_DEBUG "prot not known %x\n", protocol);
1502 return -ENOPROTOOPT;
1503 }
1504 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1505 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1506 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1507 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1508 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1509#ifdef REVERSE_BITORDER
1510 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1511#endif
1512 return 0;
1513}
1514
1515static void
1516deactivate_bchannel(struct bchannel *bch)
1517{
1518 struct hfc_pci *hc = bch->hw;
1519 u_long flags;
1520
1521 spin_lock_irqsave(&hc->lock, flags);
Karsten Keilfb286f02009-07-09 10:02:29 +02001522 mISDN_clear_bchannel(bch);
Karsten Keil1700fe12008-07-26 18:55:28 +02001523 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
Karsten Keil1700fe12008-07-26 18:55:28 +02001524 spin_unlock_irqrestore(&hc->lock, flags);
1525}
1526
1527/*
1528 * Layer 1 B-channel hardware access
1529 */
1530static int
1531channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1532{
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001533 int ret = 0;
Karsten Keil1700fe12008-07-26 18:55:28 +02001534
1535 switch (cq->op) {
1536 case MISDN_CTRL_GETOP:
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001537 cq->op = MISDN_CTRL_FILL_EMPTY;
1538 break;
1539 case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
1540 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
1541 if (debug & DEBUG_HW_OPEN)
1542 printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
Joe Perches475be4d2012-02-19 19:52:38 -08001543 "off=%d)\n", __func__, bch->nr, !!cq->p1);
Karsten Keil1700fe12008-07-26 18:55:28 +02001544 break;
1545 default:
1546 printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
1547 ret = -EINVAL;
1548 break;
1549 }
1550 return ret;
1551}
1552static int
1553hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1554{
1555 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1556 struct hfc_pci *hc = bch->hw;
1557 int ret = -EINVAL;
1558 u_long flags;
1559
1560 if (bch->debug & DEBUG_HW)
1561 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1562 switch (cmd) {
1563 case HW_TESTRX_RAW:
1564 spin_lock_irqsave(&hc->lock, flags);
1565 ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);
1566 spin_unlock_irqrestore(&hc->lock, flags);
1567 break;
1568 case HW_TESTRX_HDLC:
1569 spin_lock_irqsave(&hc->lock, flags);
1570 ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);
1571 spin_unlock_irqrestore(&hc->lock, flags);
1572 break;
1573 case HW_TESTRX_OFF:
1574 spin_lock_irqsave(&hc->lock, flags);
1575 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
1576 spin_unlock_irqrestore(&hc->lock, flags);
1577 ret = 0;
1578 break;
1579 case CLOSE_CHANNEL:
1580 test_and_clear_bit(FLG_OPEN, &bch->Flags);
Karsten Keil13681122012-05-15 23:51:01 +00001581 deactivate_bchannel(bch);
Karsten Keil1700fe12008-07-26 18:55:28 +02001582 ch->protocol = ISDN_P_NONE;
1583 ch->peer = NULL;
1584 module_put(THIS_MODULE);
1585 ret = 0;
1586 break;
1587 case CONTROL_CHANNEL:
1588 ret = channel_bctrl(bch, arg);
1589 break;
1590 default:
1591 printk(KERN_WARNING "%s: unknown prim(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001592 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02001593 }
1594 return ret;
1595}
1596
1597/*
1598 * Layer2 -> Layer 1 Dchannel data
1599 */
1600static int
1601hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
1602{
1603 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1604 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1605 struct hfc_pci *hc = dch->hw;
1606 int ret = -EINVAL;
1607 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1608 unsigned int id;
1609 u_long flags;
1610
1611 switch (hh->prim) {
1612 case PH_DATA_REQ:
1613 spin_lock_irqsave(&hc->lock, flags);
1614 ret = dchannel_senddata(dch, skb);
1615 if (ret > 0) { /* direct TX */
1616 id = hh->id; /* skb can be freed */
1617 hfcpci_fill_dfifo(dch->hw);
1618 ret = 0;
1619 spin_unlock_irqrestore(&hc->lock, flags);
1620 queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
1621 } else
1622 spin_unlock_irqrestore(&hc->lock, flags);
1623 return ret;
1624 case PH_ACTIVATE_REQ:
1625 spin_lock_irqsave(&hc->lock, flags);
1626 if (hc->hw.protocol == ISDN_P_NT_S0) {
1627 ret = 0;
1628 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1629 hc->hw.mst_m |= HFCPCI_MASTER;
1630 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1631 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1632 spin_unlock_irqrestore(&hc->lock, flags);
1633 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -08001634 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001635 break;
1636 }
1637 test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);
1638 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -08001639 HFCPCI_DO_ACTION | 1);
Karsten Keil1700fe12008-07-26 18:55:28 +02001640 } else
1641 ret = l1_event(dch->l1, hh->prim);
1642 spin_unlock_irqrestore(&hc->lock, flags);
1643 break;
1644 case PH_DEACTIVATE_REQ:
1645 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1646 spin_lock_irqsave(&hc->lock, flags);
1647 if (hc->hw.protocol == ISDN_P_NT_S0) {
1648 /* prepare deactivation */
1649 Write_hfc(hc, HFCPCI_STATES, 0x40);
1650 skb_queue_purge(&dch->squeue);
1651 if (dch->tx_skb) {
1652 dev_kfree_skb(dch->tx_skb);
1653 dch->tx_skb = NULL;
1654 }
1655 dch->tx_idx = 0;
1656 if (dch->rx_skb) {
1657 dev_kfree_skb(dch->rx_skb);
1658 dch->rx_skb = NULL;
1659 }
1660 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1661 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1662 del_timer(&dch->timer);
1663#ifdef FIXME
1664 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
1665 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
1666#endif
1667 hc->hw.mst_m &= ~HFCPCI_MASTER;
1668 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1669 ret = 0;
1670 } else {
1671 ret = l1_event(dch->l1, hh->prim);
1672 }
1673 spin_unlock_irqrestore(&hc->lock, flags);
1674 break;
1675 }
1676 if (!ret)
1677 dev_kfree_skb(skb);
1678 return ret;
1679}
1680
1681/*
1682 * Layer2 -> Layer 1 Bchannel data
1683 */
1684static int
1685hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
1686{
1687 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1688 struct hfc_pci *hc = bch->hw;
1689 int ret = -EINVAL;
1690 struct mISDNhead *hh = mISDN_HEAD_P(skb);
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001691 unsigned long flags;
Karsten Keil1700fe12008-07-26 18:55:28 +02001692
1693 switch (hh->prim) {
1694 case PH_DATA_REQ:
1695 spin_lock_irqsave(&hc->lock, flags);
1696 ret = bchannel_senddata(bch, skb);
1697 if (ret > 0) { /* direct TX */
Karsten Keil1700fe12008-07-26 18:55:28 +02001698 hfcpci_fill_fifo(bch);
1699 ret = 0;
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001700 }
1701 spin_unlock_irqrestore(&hc->lock, flags);
Karsten Keil1700fe12008-07-26 18:55:28 +02001702 return ret;
1703 case PH_ACTIVATE_REQ:
1704 spin_lock_irqsave(&hc->lock, flags);
1705 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
1706 ret = mode_hfcpci(bch, bch->nr, ch->protocol);
1707 else
1708 ret = 0;
1709 spin_unlock_irqrestore(&hc->lock, flags);
1710 if (!ret)
1711 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08001712 NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001713 break;
1714 case PH_DEACTIVATE_REQ:
1715 deactivate_bchannel(bch);
1716 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08001717 NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001718 ret = 0;
1719 break;
1720 }
1721 if (!ret)
1722 dev_kfree_skb(skb);
1723 return ret;
1724}
1725
1726/*
1727 * called for card init message
1728 */
1729
Harvey Harrison1532dcb2008-09-22 19:16:51 -07001730static void
Karsten Keil1700fe12008-07-26 18:55:28 +02001731inithfcpci(struct hfc_pci *hc)
1732{
1733 printk(KERN_DEBUG "inithfcpci: entered\n");
1734 hc->dch.timer.function = (void *) hfcpci_dbusy_timer;
1735 hc->dch.timer.data = (long) &hc->dch;
1736 init_timer(&hc->dch.timer);
1737 hc->chanlimit = 2;
1738 mode_hfcpci(&hc->bch[0], 1, -1);
1739 mode_hfcpci(&hc->bch[1], 2, -1);
1740}
1741
1742
1743static int
1744init_card(struct hfc_pci *hc)
1745{
1746 int cnt = 3;
1747 u_long flags;
1748
1749 printk(KERN_DEBUG "init_card: entered\n");
1750
1751
1752 spin_lock_irqsave(&hc->lock, flags);
1753 disable_hwirq(hc);
1754 spin_unlock_irqrestore(&hc->lock, flags);
1755 if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {
1756 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001757 "mISDN: couldn't get interrupt %d\n", hc->irq);
Karsten Keil1700fe12008-07-26 18:55:28 +02001758 return -EIO;
1759 }
1760 spin_lock_irqsave(&hc->lock, flags);
1761 reset_hfcpci(hc);
1762 while (cnt) {
1763 inithfcpci(hc);
1764 /*
1765 * Finally enable IRQ output
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001766 * this is only allowed, if an IRQ routine is already
Karsten Keil1700fe12008-07-26 18:55:28 +02001767 * established for this HFC, so don't do that earlier
1768 */
1769 enable_hwirq(hc);
1770 spin_unlock_irqrestore(&hc->lock, flags);
1771 /* Timeout 80ms */
1772 current->state = TASK_UNINTERRUPTIBLE;
Joe Perches475be4d2012-02-19 19:52:38 -08001773 schedule_timeout((80 * HZ) / 1000);
Karsten Keil1700fe12008-07-26 18:55:28 +02001774 printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001775 hc->irq, hc->irqcnt);
Karsten Keil1700fe12008-07-26 18:55:28 +02001776 /* now switch timer interrupt off */
1777 spin_lock_irqsave(&hc->lock, flags);
1778 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1779 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1780 /* reinit mode reg */
1781 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1782 if (!hc->irqcnt) {
1783 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001784 "HFC PCI: IRQ(%d) getting no interrupts "
1785 "during init %d\n", hc->irq, 4 - cnt);
Andreas Mohrcdae28e2009-06-02 18:15:12 +02001786 if (cnt == 1)
1787 break;
1788 else {
Karsten Keil1700fe12008-07-26 18:55:28 +02001789 reset_hfcpci(hc);
1790 cnt--;
1791 }
1792 } else {
1793 spin_unlock_irqrestore(&hc->lock, flags);
1794 hc->initdone = 1;
1795 return 0;
1796 }
1797 }
1798 disable_hwirq(hc);
1799 spin_unlock_irqrestore(&hc->lock, flags);
1800 free_irq(hc->irq, hc);
1801 return -EIO;
1802}
1803
1804static int
1805channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)
1806{
1807 int ret = 0;
1808 u_char slot;
1809
1810 switch (cq->op) {
1811 case MISDN_CTRL_GETOP:
1812 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
Karsten Keilc626c122012-05-04 04:15:33 +00001813 MISDN_CTRL_DISCONNECT | MISDN_CTRL_L1_TIMER3;
Karsten Keil1700fe12008-07-26 18:55:28 +02001814 break;
1815 case MISDN_CTRL_LOOP:
1816 /* channel 0 disabled loop */
1817 if (cq->channel < 0 || cq->channel > 2) {
1818 ret = -EINVAL;
1819 break;
1820 }
1821 if (cq->channel & 1) {
1822 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1823 slot = 0xC0;
1824 else
1825 slot = 0x80;
1826 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001827 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001828 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1829 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1830 hc->hw.conn = (hc->hw.conn & ~7) | 6;
1831 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1832 }
1833 if (cq->channel & 2) {
1834 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1835 slot = 0xC1;
1836 else
1837 slot = 0x81;
1838 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001839 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001840 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1841 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1842 hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;
1843 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1844 }
1845 if (cq->channel & 3)
1846 hc->hw.trm |= 0x80; /* enable IOM-loop */
1847 else {
1848 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1849 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1850 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1851 }
1852 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1853 break;
1854 case MISDN_CTRL_CONNECT:
1855 if (cq->channel == cq->p1) {
1856 ret = -EINVAL;
1857 break;
1858 }
1859 if (cq->channel < 1 || cq->channel > 2 ||
1860 cq->p1 < 1 || cq->p1 > 2) {
1861 ret = -EINVAL;
1862 break;
1863 }
1864 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1865 slot = 0xC0;
1866 else
1867 slot = 0x80;
1868 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001869 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001870 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1871 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1872 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1873 slot = 0xC1;
1874 else
1875 slot = 0x81;
1876 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001877 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001878 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1879 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1880 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;
1881 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1882 hc->hw.trm |= 0x80;
1883 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1884 break;
1885 case MISDN_CTRL_DISCONNECT:
1886 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1887 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1888 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1889 break;
Karsten Keilc626c122012-05-04 04:15:33 +00001890 case MISDN_CTRL_L1_TIMER3:
1891 ret = l1_event(hc->dch.l1, HW_TIMER3_VALUE | (cq->p1 & 0xff));
1892 break;
Karsten Keil1700fe12008-07-26 18:55:28 +02001893 default:
1894 printk(KERN_WARNING "%s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001895 __func__, cq->op);
Karsten Keil1700fe12008-07-26 18:55:28 +02001896 ret = -EINVAL;
1897 break;
1898 }
1899 return ret;
1900}
1901
1902static int
1903open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,
Joe Perches475be4d2012-02-19 19:52:38 -08001904 struct channel_req *rq)
Karsten Keil1700fe12008-07-26 18:55:28 +02001905{
1906 int err = 0;
1907
1908 if (debug & DEBUG_HW_OPEN)
1909 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001910 hc->dch.dev.id, __builtin_return_address(0));
Karsten Keil1700fe12008-07-26 18:55:28 +02001911 if (rq->protocol == ISDN_P_NONE)
1912 return -EINVAL;
Martin Bachem55a6af92008-09-04 12:42:39 +02001913 if (rq->adr.channel == 1) {
1914 /* TODO: E-Channel */
1915 return -EINVAL;
1916 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001917 if (!hc->initdone) {
1918 if (rq->protocol == ISDN_P_TE_S0) {
1919 err = create_l1(&hc->dch, hfc_l1callback);
1920 if (err)
1921 return err;
1922 }
1923 hc->hw.protocol = rq->protocol;
1924 ch->protocol = rq->protocol;
1925 err = init_card(hc);
1926 if (err)
1927 return err;
1928 } else {
1929 if (rq->protocol != ch->protocol) {
1930 if (hc->hw.protocol == ISDN_P_TE_S0)
1931 l1_event(hc->dch.l1, CLOSE_CHANNEL);
Andreas Eversbergc3b3cde2008-11-09 10:23:19 +01001932 if (rq->protocol == ISDN_P_TE_S0) {
1933 err = create_l1(&hc->dch, hfc_l1callback);
1934 if (err)
1935 return err;
1936 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001937 hc->hw.protocol = rq->protocol;
1938 ch->protocol = rq->protocol;
1939 hfcpci_setmode(hc);
1940 }
1941 }
1942
1943 if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||
1944 ((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {
1945 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
Joe Perches475be4d2012-02-19 19:52:38 -08001946 0, NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001947 }
1948 rq->ch = ch;
1949 if (!try_module_get(THIS_MODULE))
1950 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1951 return 0;
1952}
1953
1954static int
1955open_bchannel(struct hfc_pci *hc, struct channel_req *rq)
1956{
1957 struct bchannel *bch;
1958
Dan Carpenter819a1002012-03-26 21:20:48 +00001959 if (rq->adr.channel == 0 || rq->adr.channel > 2)
Karsten Keil1700fe12008-07-26 18:55:28 +02001960 return -EINVAL;
1961 if (rq->protocol == ISDN_P_NONE)
1962 return -EINVAL;
1963 bch = &hc->bch[rq->adr.channel - 1];
1964 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1965 return -EBUSY; /* b-channel can be only open once */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +02001966 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
Karsten Keil1700fe12008-07-26 18:55:28 +02001967 bch->ch.protocol = rq->protocol;
1968 rq->ch = &bch->ch; /* TODO: E-channel */
1969 if (!try_module_get(THIS_MODULE))
1970 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1971 return 0;
1972}
1973
1974/*
1975 * device control function
1976 */
1977static int
1978hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1979{
1980 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1981 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1982 struct hfc_pci *hc = dch->hw;
1983 struct channel_req *rq;
1984 int err = 0;
1985
1986 if (dch->debug & DEBUG_HW)
1987 printk(KERN_DEBUG "%s: cmd:%x %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001988 __func__, cmd, arg);
Karsten Keil1700fe12008-07-26 18:55:28 +02001989 switch (cmd) {
1990 case OPEN_CHANNEL:
1991 rq = arg;
Martin Bachema9b61832008-09-03 18:08:30 +02001992 if ((rq->protocol == ISDN_P_TE_S0) ||
1993 (rq->protocol == ISDN_P_NT_S0))
Karsten Keil1700fe12008-07-26 18:55:28 +02001994 err = open_dchannel(hc, ch, rq);
1995 else
1996 err = open_bchannel(hc, rq);
1997 break;
1998 case CLOSE_CHANNEL:
1999 if (debug & DEBUG_HW_OPEN)
2000 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002001 __func__, hc->dch.dev.id,
2002 __builtin_return_address(0));
Karsten Keil1700fe12008-07-26 18:55:28 +02002003 module_put(THIS_MODULE);
2004 break;
2005 case CONTROL_CHANNEL:
2006 err = channel_ctrl(hc, arg);
2007 break;
2008 default:
2009 if (dch->debug & DEBUG_HW)
2010 printk(KERN_DEBUG "%s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002011 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02002012 return -EINVAL;
2013 }
2014 return err;
2015}
2016
2017static int
2018setup_hw(struct hfc_pci *hc)
2019{
2020 void *buffer;
2021
2022 printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);
2023 hc->hw.cirm = 0;
2024 hc->dch.state = 0;
2025 pci_set_master(hc->pdev);
2026 if (!hc->irq) {
2027 printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");
2028 return 1;
2029 }
Karsten Keileac74af2009-05-22 11:04:56 +00002030 hc->hw.pci_io =
2031 (char __iomem *)(unsigned long)hc->pdev->resource[1].start;
Karsten Keil1700fe12008-07-26 18:55:28 +02002032
2033 if (!hc->hw.pci_io) {
2034 printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");
2035 return 1;
2036 }
2037 /* Allocate memory for FIFOS */
2038 /* the memory needs to be on a 32k boundary within the first 4G */
2039 pci_set_dma_mask(hc->pdev, 0xFFFF8000);
2040 buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);
2041 /* We silently assume the address is okay if nonzero */
2042 if (!buffer) {
2043 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08002044 "HFC-PCI: Error allocating memory for FIFO!\n");
Karsten Keil1700fe12008-07-26 18:55:28 +02002045 return 1;
2046 }
2047 hc->hw.fifos = buffer;
2048 pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);
2049 hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);
2050 printk(KERN_INFO
Joe Perches475be4d2012-02-19 19:52:38 -08002051 "HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",
2052 (u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,
2053 (u_long) hc->hw.dmahandle, hc->irq, HZ);
Karsten Keil1700fe12008-07-26 18:55:28 +02002054 /* enable memory mapped ports, disable busmaster */
2055 pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
2056 hc->hw.int_m2 = 0;
2057 disable_hwirq(hc);
2058 hc->hw.int_m1 = 0;
2059 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
2060 /* At this point the needed PCI config is done */
2061 /* fifos are still not enabled */
2062 hc->hw.timer.function = (void *) hfcpci_Timer;
2063 hc->hw.timer.data = (long) hc;
2064 init_timer(&hc->hw.timer);
2065 /* default PCM master */
2066 test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);
2067 return 0;
2068}
2069
2070static void
2071release_card(struct hfc_pci *hc) {
2072 u_long flags;
2073
2074 spin_lock_irqsave(&hc->lock, flags);
2075 hc->hw.int_m2 = 0; /* interrupt output off ! */
2076 disable_hwirq(hc);
2077 mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);
2078 mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);
2079 if (hc->dch.timer.function != NULL) {
2080 del_timer(&hc->dch.timer);
2081 hc->dch.timer.function = NULL;
2082 }
2083 spin_unlock_irqrestore(&hc->lock, flags);
2084 if (hc->hw.protocol == ISDN_P_TE_S0)
2085 l1_event(hc->dch.l1, CLOSE_CHANNEL);
2086 if (hc->initdone)
2087 free_irq(hc->irq, hc);
2088 release_io_hfcpci(hc); /* must release after free_irq! */
2089 mISDN_unregister_device(&hc->dch.dev);
2090 mISDN_freebchannel(&hc->bch[1]);
2091 mISDN_freebchannel(&hc->bch[0]);
2092 mISDN_freedchannel(&hc->dch);
Karsten Keil1700fe12008-07-26 18:55:28 +02002093 pci_set_drvdata(hc->pdev, NULL);
2094 kfree(hc);
2095}
2096
2097static int
2098setup_card(struct hfc_pci *card)
2099{
2100 int err = -EINVAL;
2101 u_int i;
Karsten Keil1700fe12008-07-26 18:55:28 +02002102 char name[MISDN_MAX_IDLEN];
2103
Karsten Keil1700fe12008-07-26 18:55:28 +02002104 card->dch.debug = debug;
2105 spin_lock_init(&card->lock);
2106 mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);
2107 card->dch.hw = card;
2108 card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
2109 card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08002110 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil1700fe12008-07-26 18:55:28 +02002111 card->dch.dev.D.send = hfcpci_l2l1D;
2112 card->dch.dev.D.ctrl = hfc_dctrl;
2113 card->dch.dev.nrbchan = 2;
2114 for (i = 0; i < 2; i++) {
2115 card->bch[i].nr = i + 1;
Karsten Keilff4cc1d2008-07-30 18:26:58 +02002116 set_channelmap(i + 1, card->dch.dev.channelmap);
Karsten Keil1700fe12008-07-26 18:55:28 +02002117 card->bch[i].debug = debug;
2118 mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);
2119 card->bch[i].hw = card;
2120 card->bch[i].ch.send = hfcpci_l2l1B;
2121 card->bch[i].ch.ctrl = hfc_bctrl;
2122 card->bch[i].ch.nr = i + 1;
2123 list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);
2124 }
2125 err = setup_hw(card);
2126 if (err)
2127 goto error;
2128 snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002129 err = mISDN_register_device(&card->dch.dev, &card->pdev->dev, name);
Karsten Keil1700fe12008-07-26 18:55:28 +02002130 if (err)
2131 goto error;
2132 HFC_cnt++;
Karsten Keil1700fe12008-07-26 18:55:28 +02002133 printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);
2134 return 0;
2135error:
2136 mISDN_freebchannel(&card->bch[1]);
2137 mISDN_freebchannel(&card->bch[0]);
2138 mISDN_freedchannel(&card->dch);
2139 kfree(card);
2140 return err;
2141}
2142
2143/* private data in the PCI devices list */
2144struct _hfc_map {
2145 u_int subtype;
2146 u_int flag;
2147 char *name;
2148};
2149
2150static const struct _hfc_map hfc_map[] =
2151{
2152 {HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},
2153 {HFC_CCD_B000, 0, "Billion B000"},
2154 {HFC_CCD_B006, 0, "Billion B006"},
2155 {HFC_CCD_B007, 0, "Billion B007"},
2156 {HFC_CCD_B008, 0, "Billion B008"},
2157 {HFC_CCD_B009, 0, "Billion B009"},
2158 {HFC_CCD_B00A, 0, "Billion B00A"},
2159 {HFC_CCD_B00B, 0, "Billion B00B"},
2160 {HFC_CCD_B00C, 0, "Billion B00C"},
2161 {HFC_CCD_B100, 0, "Seyeon B100"},
2162 {HFC_CCD_B700, 0, "Primux II S0 B700"},
2163 {HFC_CCD_B701, 0, "Primux II S0 NT B701"},
2164 {HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},
2165 {HFC_ASUS_0675, 0, "Asuscom/Askey 675"},
2166 {HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},
2167 {HFC_BERKOM_A1T, 0, "German telekom A1T"},
2168 {HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},
2169 {HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},
2170 {HFC_DIGI_DF_M_IOM2_E, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002171 "Digi International DataFire Micro V IOM2 (Europe)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002172 {HFC_DIGI_DF_M_E, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002173 "Digi International DataFire Micro V (Europe)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002174 {HFC_DIGI_DF_M_IOM2_A, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002175 "Digi International DataFire Micro V IOM2 (North America)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002176 {HFC_DIGI_DF_M_A, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002177 "Digi International DataFire Micro V (North America)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002178 {HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},
2179 {},
2180};
2181
2182static struct pci_device_id hfc_ids[] =
2183{
Peter Hueweb8176a32010-07-15 09:02:36 +00002184 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_2BD0),
Joe Perches475be4d2012-02-19 19:52:38 -08002185 (unsigned long) &hfc_map[0] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002186 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B000),
Joe Perches475be4d2012-02-19 19:52:38 -08002187 (unsigned long) &hfc_map[1] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002188 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B006),
Joe Perches475be4d2012-02-19 19:52:38 -08002189 (unsigned long) &hfc_map[2] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002190 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B007),
Joe Perches475be4d2012-02-19 19:52:38 -08002191 (unsigned long) &hfc_map[3] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002192 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B008),
Joe Perches475be4d2012-02-19 19:52:38 -08002193 (unsigned long) &hfc_map[4] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002194 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B009),
Joe Perches475be4d2012-02-19 19:52:38 -08002195 (unsigned long) &hfc_map[5] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002196 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00A),
Joe Perches475be4d2012-02-19 19:52:38 -08002197 (unsigned long) &hfc_map[6] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002198 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00B),
Joe Perches475be4d2012-02-19 19:52:38 -08002199 (unsigned long) &hfc_map[7] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002200 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00C),
Joe Perches475be4d2012-02-19 19:52:38 -08002201 (unsigned long) &hfc_map[8] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002202 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B100),
Joe Perches475be4d2012-02-19 19:52:38 -08002203 (unsigned long) &hfc_map[9] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002204 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B700),
Joe Perches475be4d2012-02-19 19:52:38 -08002205 (unsigned long) &hfc_map[10] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002206 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B701),
Joe Perches475be4d2012-02-19 19:52:38 -08002207 (unsigned long) &hfc_map[11] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002208 { PCI_VDEVICE(ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1),
Joe Perches475be4d2012-02-19 19:52:38 -08002209 (unsigned long) &hfc_map[12] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002210 { PCI_VDEVICE(ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675),
Joe Perches475be4d2012-02-19 19:52:38 -08002211 (unsigned long) &hfc_map[13] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002212 { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT),
Joe Perches475be4d2012-02-19 19:52:38 -08002213 (unsigned long) &hfc_map[14] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002214 { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_A1T),
Joe Perches475be4d2012-02-19 19:52:38 -08002215 (unsigned long) &hfc_map[15] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002216 { PCI_VDEVICE(ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575),
Joe Perches475be4d2012-02-19 19:52:38 -08002217 (unsigned long) &hfc_map[16] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002218 { PCI_VDEVICE(ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0),
Joe Perches475be4d2012-02-19 19:52:38 -08002219 (unsigned long) &hfc_map[17] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002220 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E),
Joe Perches475be4d2012-02-19 19:52:38 -08002221 (unsigned long) &hfc_map[18] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002222 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_E),
Joe Perches475be4d2012-02-19 19:52:38 -08002223 (unsigned long) &hfc_map[19] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002224 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A),
Joe Perches475be4d2012-02-19 19:52:38 -08002225 (unsigned long) &hfc_map[20] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002226 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_A),
Joe Perches475be4d2012-02-19 19:52:38 -08002227 (unsigned long) &hfc_map[21] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002228 { PCI_VDEVICE(SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2),
Joe Perches475be4d2012-02-19 19:52:38 -08002229 (unsigned long) &hfc_map[22] },
Karsten Keil1700fe12008-07-26 18:55:28 +02002230 {},
2231};
2232
2233static int __devinit
2234hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2235{
2236 int err = -ENOMEM;
2237 struct hfc_pci *card;
2238 struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;
2239
2240 card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);
2241 if (!card) {
2242 printk(KERN_ERR "No kmem for HFC card\n");
2243 return err;
2244 }
2245 card->pdev = pdev;
2246 card->subtype = m->subtype;
2247 err = pci_enable_device(pdev);
2248 if (err) {
2249 kfree(card);
2250 return err;
2251 }
2252
2253 printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",
2254 m->name, pci_name(pdev));
2255
2256 card->irq = pdev->irq;
2257 pci_set_drvdata(pdev, card);
2258 err = setup_card(card);
2259 if (err)
2260 pci_set_drvdata(pdev, NULL);
2261 return err;
2262}
2263
2264static void __devexit
2265hfc_remove_pci(struct pci_dev *pdev)
2266{
2267 struct hfc_pci *card = pci_get_drvdata(pdev);
Karsten Keil1700fe12008-07-26 18:55:28 +02002268
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002269 if (card)
Karsten Keil1700fe12008-07-26 18:55:28 +02002270 release_card(card);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002271 else
Karsten Keil1700fe12008-07-26 18:55:28 +02002272 if (debug)
Karsten Keileac74af2009-05-22 11:04:56 +00002273 printk(KERN_DEBUG "%s: drvdata already removed\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002274 __func__);
Karsten Keil1700fe12008-07-26 18:55:28 +02002275}
2276
2277
2278static struct pci_driver hfc_driver = {
2279 .name = "hfcpci",
2280 .probe = hfc_probe,
2281 .remove = __devexit_p(hfc_remove_pci),
2282 .id_table = hfc_ids,
2283};
2284
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002285static int
2286_hfcpci_softirq(struct device *dev, void *arg)
2287{
2288 struct hfc_pci *hc = dev_get_drvdata(dev);
2289 struct bchannel *bch;
2290 if (hc == NULL)
2291 return 0;
2292
2293 if (hc->hw.int_m2 & HFCPCI_IRQ_ENABLE) {
2294 spin_lock(&hc->lock);
2295 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
2296 if (bch && bch->state == ISDN_P_B_RAW) { /* B1 rx&tx */
2297 main_rec_hfcpci(bch);
2298 tx_birq(bch);
2299 }
2300 bch = Sel_BCS(hc, hc->hw.bswapped ? 1 : 2);
2301 if (bch && bch->state == ISDN_P_B_RAW) { /* B2 rx&tx */
2302 main_rec_hfcpci(bch);
2303 tx_birq(bch);
2304 }
2305 spin_unlock(&hc->lock);
2306 }
2307 return 0;
2308}
2309
2310static void
2311hfcpci_softirq(void *arg)
2312{
2313 (void) driver_for_each_device(&hfc_driver.driver, NULL, arg,
Joe Perches475be4d2012-02-19 19:52:38 -08002314 _hfcpci_softirq);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002315
2316 /* if next event would be in the past ... */
2317 if ((s32)(hfc_jiffies + tics - jiffies) <= 0)
2318 hfc_jiffies = jiffies + 1;
2319 else
2320 hfc_jiffies += tics;
2321 hfc_tl.expires = hfc_jiffies;
2322 add_timer(&hfc_tl);
2323}
2324
Karsten Keil1700fe12008-07-26 18:55:28 +02002325static int __init
2326HFC_init(void)
2327{
2328 int err;
2329
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002330 if (!poll)
2331 poll = HFCPCI_BTRANS_THRESHOLD;
2332
2333 if (poll != HFCPCI_BTRANS_THRESHOLD) {
Andreas Eversberg400fd972008-10-11 08:13:29 +02002334 tics = (poll * HZ) / 8000;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002335 if (tics < 1)
2336 tics = 1;
Andreas Eversberg400fd972008-10-11 08:13:29 +02002337 poll = (tics * 8000) / HZ;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002338 if (poll > 256 || poll < 8) {
2339 printk(KERN_ERR "%s: Wrong poll value %d not in range "
Joe Perches475be4d2012-02-19 19:52:38 -08002340 "of 8..256.\n", __func__, poll);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002341 err = -EINVAL;
2342 return err;
2343 }
2344 }
2345 if (poll != HFCPCI_BTRANS_THRESHOLD) {
2346 printk(KERN_INFO "%s: Using alternative poll value of %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002347 __func__, poll);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002348 hfc_tl.function = (void *)hfcpci_softirq;
2349 hfc_tl.data = 0;
2350 init_timer(&hfc_tl);
2351 hfc_tl.expires = jiffies + tics;
2352 hfc_jiffies = hfc_tl.expires;
2353 add_timer(&hfc_tl);
2354 } else
2355 tics = 0; /* indicate the use of controller's timer */
2356
Karsten Keil1700fe12008-07-26 18:55:28 +02002357 err = pci_register_driver(&hfc_driver);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002358 if (err) {
2359 if (timer_pending(&hfc_tl))
2360 del_timer(&hfc_tl);
2361 }
2362
Karsten Keil1700fe12008-07-26 18:55:28 +02002363 return err;
2364}
2365
2366static void __exit
2367HFC_cleanup(void)
2368{
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002369 if (timer_pending(&hfc_tl))
2370 del_timer(&hfc_tl);
2371
Karsten Keil1700fe12008-07-26 18:55:28 +02002372 pci_unregister_driver(&hfc_driver);
2373}
2374
2375module_init(HFC_init);
2376module_exit(HFC_cleanup);
Matthias Urlichse314f892008-10-16 13:58:54 +02002377
2378MODULE_DEVICE_TABLE(pci, hfc_ids);