blob: 1bd8bc7eb5c7cff3889670ac07ef818410e68f66 [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 */
Karsten Keil034005a2012-05-15 23:51:06 +0000456 recv_Bchannel(bch, MISDN_ID_ANY, false);
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 fcnt_tx = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
569 if (fcnt_tx <= 0)
570 fcnt_tx += B_FIFO_SIZE;
Joe Perches475be4d2012-02-19 19:52:38 -0800571 /* fcnt_tx contains available bytes in tx-fifo */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000572 fcnt_tx = B_FIFO_SIZE - fcnt_tx;
Joe Perches475be4d2012-02-19 19:52:38 -0800573 /* remaining bytes to send (bytes in tx-fifo) */
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000574
Karsten Keil7206e652012-05-15 23:51:05 +0000575 maxlen = bchannel_get_rxbuf(bch, fcnt_rx);
576 if (maxlen < 0) {
577 pr_warning("B%d: No bufferspace for %d bytes\n",
578 bch->nr, fcnt_rx);
579 } else {
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);
Joe Perches475be4d2012-02-19 19:52:38 -0800585 /* maximum */
Karsten Keil1700fe12008-07-26 18:55:28 +0200586
587 ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);
Joe Perches475be4d2012-02-19 19:52:38 -0800588 /* start of data */
Karsten Keil1700fe12008-07-26 18:55:28 +0200589 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 }
Karsten Keil034005a2012-05-15 23:51:06 +0000597 recv_Bchannel(bch, fcnt_tx, false); /* bch, id, !force */
Karsten Keil7206e652012-05-15 23:51:05 +0000598 }
Karsten Keil1700fe12008-07-26 18:55:28 +0200599 *z2r = cpu_to_le16(new_z2); /* new position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200600}
601
602/*
603 * B-channel main receive routine
604 */
Harvey Harrison1532dcb2008-09-22 19:16:51 -0700605static void
Karsten Keil1700fe12008-07-26 18:55:28 +0200606main_rec_hfcpci(struct bchannel *bch)
607{
608 struct hfc_pci *hc = bch->hw;
609 int rcnt, real_fifo;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200610 int receive = 0, count = 5;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000611 struct bzfifo *txbz, *rxbz;
Karsten Keil1700fe12008-07-26 18:55:28 +0200612 u_char *bdata;
613 struct zt *zp;
614
Karsten Keil1700fe12008-07-26 18:55:28 +0200615 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000616 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
617 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200618 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;
619 real_fifo = 1;
620 } else {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000621 rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
622 txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200623 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;
624 real_fifo = 0;
625 }
626Begin:
627 count--;
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000628 if (rxbz->f1 != rxbz->f2) {
Karsten Keil1700fe12008-07-26 18:55:28 +0200629 if (bch->debug & DEBUG_HW_BCHANNEL)
630 printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800631 bch->nr, rxbz->f1, rxbz->f2);
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000632 zp = &rxbz->za[rxbz->f2];
Karsten Keil1700fe12008-07-26 18:55:28 +0200633
634 rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
635 if (rcnt < 0)
636 rcnt += B_FIFO_SIZE;
637 rcnt++;
638 if (bch->debug & DEBUG_HW_BCHANNEL)
639 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800640 "hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",
641 bch->nr, le16_to_cpu(zp->z1),
642 le16_to_cpu(zp->z2), rcnt);
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000643 hfcpci_empty_bfifo(bch, rxbz, bdata, rcnt);
644 rcnt = rxbz->f1 - rxbz->f2;
Karsten Keil1700fe12008-07-26 18:55:28 +0200645 if (rcnt < 0)
646 rcnt += MAX_B_FRAMES + 1;
647 if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {
648 rcnt = 0;
649 hfcpci_clear_fifo_rx(hc, real_fifo);
650 }
651 hc->hw.last_bfifo_cnt[real_fifo] = rcnt;
652 if (rcnt > 1)
653 receive = 1;
654 else
655 receive = 0;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200656 } else if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
Andreas Eversberg7cfa1532009-05-22 11:04:46 +0000657 hfcpci_empty_fifo_trans(bch, rxbz, txbz, bdata);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +0200658 return;
659 } else
Karsten Keil1700fe12008-07-26 18:55:28 +0200660 receive = 0;
661 if (count && receive)
662 goto Begin;
663
664}
665
666/*
667 * D-channel send routine
668 */
669static void
670hfcpci_fill_dfifo(struct hfc_pci *hc)
671{
672 struct dchannel *dch = &hc->dch;
673 int fcnt;
674 int count, new_z1, maxlen;
675 struct dfifo *df;
676 u_char *src, *dst, new_f1;
677
678 if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))
679 printk(KERN_DEBUG "%s\n", __func__);
680
681 if (!dch->tx_skb)
682 return;
683 count = dch->tx_skb->len - dch->tx_idx;
684 if (count <= 0)
685 return;
686 df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;
687
688 if (dch->debug & DEBUG_HW_DFIFO)
689 printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800690 df->f1, df->f2,
691 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));
Karsten Keil1700fe12008-07-26 18:55:28 +0200692 fcnt = df->f1 - df->f2; /* frame count actually buffered */
693 if (fcnt < 0)
694 fcnt += (MAX_D_FRAMES + 1); /* if wrap around */
695 if (fcnt > (MAX_D_FRAMES - 1)) {
696 if (dch->debug & DEBUG_HW_DCHANNEL)
697 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800698 "hfcpci_fill_Dfifo more as 14 frames\n");
Karsten Keil1700fe12008-07-26 18:55:28 +0200699#ifdef ERROR_STATISTIC
700 cs->err_tx++;
701#endif
702 return;
703 }
704 /* now determine free bytes in FIFO buffer */
705 maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -
Joe Perches475be4d2012-02-19 19:52:38 -0800706 le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200707 if (maxlen <= 0)
708 maxlen += D_FIFO_SIZE; /* count now contains available bytes */
709
710 if (dch->debug & DEBUG_HW_DCHANNEL)
711 printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800712 count, maxlen);
Karsten Keil1700fe12008-07-26 18:55:28 +0200713 if (count > maxlen) {
714 if (dch->debug & DEBUG_HW_DCHANNEL)
715 printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");
716 return;
717 }
718 new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &
Joe Perches475be4d2012-02-19 19:52:38 -0800719 (D_FIFO_SIZE - 1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200720 new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);
721 src = dch->tx_skb->data + dch->tx_idx; /* source pointer */
722 dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
723 maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800724 /* end fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200725 if (maxlen > count)
726 maxlen = count; /* limit size */
727 memcpy(dst, src, maxlen); /* first copy */
728
729 count -= maxlen; /* remaining bytes */
730 if (count) {
731 dst = df->data; /* start of buffer */
732 src += maxlen; /* new position */
733 memcpy(dst, src, count);
734 }
735 df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800736 /* for next buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200737 df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800738 /* new pos actual buffer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200739 df->f1 = new_f1; /* next frame */
740 dch->tx_idx = dch->tx_skb->len;
741}
742
743/*
744 * B-channel send routine
745 */
746static void
747hfcpci_fill_fifo(struct bchannel *bch)
748{
Joe Perches475be4d2012-02-19 19:52:38 -0800749 struct hfc_pci *hc = bch->hw;
Karsten Keil1700fe12008-07-26 18:55:28 +0200750 int maxlen, fcnt;
751 int count, new_z1;
752 struct bzfifo *bz;
753 u_char *bdata;
754 u_char new_f1, *src, *dst;
Harvey Harrisonf11d32d2008-09-22 19:16:20 -0700755 __le16 *z1t, *z2t;
Karsten Keil1700fe12008-07-26 18:55:28 +0200756
757 if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
758 printk(KERN_DEBUG "%s\n", __func__);
Karsten Keil6d1ee482012-05-15 23:51:07 +0000759 if ((!bch->tx_skb) || bch->tx_skb->len == 0) {
760 if (!test_bit(FLG_FILLEMPTY, &bch->Flags) &&
761 !test_bit(FLG_TRANSPARENT, &bch->Flags))
762 return;
763 count = HFCPCI_FILLEMPTY;
764 } else {
765 count = bch->tx_skb->len - bch->tx_idx;
766 }
Karsten Keil1700fe12008-07-26 18:55:28 +0200767 if ((bch->nr & 2) && (!hc->hw.bswapped)) {
768 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
769 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;
770 } else {
771 bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
772 bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;
773 }
774
775 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
776 z1t = &bz->za[MAX_B_FRAMES].z1;
777 z2t = z1t + 1;
778 if (bch->debug & DEBUG_HW_BCHANNEL)
779 printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "
Joe Perches475be4d2012-02-19 19:52:38 -0800780 "cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,
781 le16_to_cpu(*z1t), le16_to_cpu(*z2t));
Karsten Keil1700fe12008-07-26 18:55:28 +0200782 fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
783 if (fcnt <= 0)
784 fcnt += B_FIFO_SIZE;
Karsten Keil6d1ee482012-05-15 23:51:07 +0000785 if (test_bit(FLG_FILLEMPTY, &bch->Flags)) {
786 /* fcnt contains available bytes in fifo */
787 if (count > fcnt)
788 count = fcnt;
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200789 new_z1 = le16_to_cpu(*z1t) + count;
Joe Perches475be4d2012-02-19 19:52:38 -0800790 /* new buffer Position */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200791 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
792 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
793 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
794 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
Joe Perches475be4d2012-02-19 19:52:38 -0800795 /* end of fifo */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200796 if (bch->debug & DEBUG_HW_BFIFO)
797 printk(KERN_DEBUG "hfcpci_FFt fillempty "
Joe Perches475be4d2012-02-19 19:52:38 -0800798 "fcnt(%d) maxl(%d) nz1(%x) dst(%p)\n",
799 fcnt, maxlen, new_z1, dst);
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200800 if (maxlen > count)
Joe Perches475be4d2012-02-19 19:52:38 -0800801 maxlen = count; /* limit size */
Karsten Keil6d1ee482012-05-15 23:51:07 +0000802 memset(dst, bch->fill[0], maxlen); /* first copy */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200803 count -= maxlen; /* remaining bytes */
804 if (count) {
805 dst = bdata; /* start of buffer */
Karsten Keil6d1ee482012-05-15 23:51:07 +0000806 memset(dst, bch->fill[0], count);
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200807 }
808 *z1t = cpu_to_le16(new_z1); /* now send data */
Karsten Keil6d1ee482012-05-15 23:51:07 +0000809 return;
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200810 }
Karsten Keil6d1ee482012-05-15 23:51:07 +0000811 /* fcnt contains available bytes in fifo */
812 fcnt = B_FIFO_SIZE - fcnt;
813 /* remaining bytes to send (bytes in fifo) */
Andreas Eversberg8dd2f362008-08-02 22:51:52 +0200814
Joe Perches475be4d2012-02-19 19:52:38 -0800815 next_t_frame:
Karsten Keil1700fe12008-07-26 18:55:28 +0200816 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;
Joe Perches475be4d2012-02-19 19:52:38 -0800824 /* new buffer Position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200825 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;
Joe Perches475be4d2012-02-19 19:52:38 -0800828 /* source pointer */
Karsten Keil1700fe12008-07-26 18:55:28 +0200829 dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
830 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
Joe Perches475be4d2012-02-19 19:52:38 -0800831 /* end of fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200832 if (bch->debug & DEBUG_HW_BFIFO)
833 printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "
Joe Perches475be4d2012-02-19 19:52:38 -0800834 "maxl(%d) nz1(%x) dst(%p)\n",
835 fcnt, maxlen, new_z1, dst);
Karsten Keil1700fe12008-07-26 18:55:28 +0200836 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;
Karsten Keil1700fe12008-07-26 18:55:28 +0200850 dev_kfree_skb(bch->tx_skb);
851 if (get_next_bframe(bch))
852 goto next_t_frame;
853 return;
854 }
855 if (bch->debug & DEBUG_HW_BCHANNEL)
856 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800857 "%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",
858 __func__, bch->nr, bz->f1, bz->f2,
859 bz->za[bz->f1].z1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200860 fcnt = bz->f1 - bz->f2; /* frame count actually buffered */
861 if (fcnt < 0)
862 fcnt += (MAX_B_FRAMES + 1); /* if wrap around */
863 if (fcnt > (MAX_B_FRAMES - 1)) {
864 if (bch->debug & DEBUG_HW_BCHANNEL)
865 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800866 "hfcpci_fill_Bfifo more as 14 frames\n");
Karsten Keil1700fe12008-07-26 18:55:28 +0200867 return;
868 }
869 /* now determine free bytes in FIFO buffer */
870 maxlen = le16_to_cpu(bz->za[bz->f2].z2) -
Joe Perches475be4d2012-02-19 19:52:38 -0800871 le16_to_cpu(bz->za[bz->f1].z1) - 1;
Karsten Keil1700fe12008-07-26 18:55:28 +0200872 if (maxlen <= 0)
873 maxlen += B_FIFO_SIZE; /* count now contains available bytes */
874
875 if (bch->debug & DEBUG_HW_BCHANNEL)
876 printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800877 bch->nr, count, maxlen);
Karsten Keil1700fe12008-07-26 18:55:28 +0200878
879 if (maxlen < count) {
880 if (bch->debug & DEBUG_HW_BCHANNEL)
881 printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");
882 return;
883 }
884 new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;
Joe Perches475be4d2012-02-19 19:52:38 -0800885 /* new buffer Position */
Karsten Keil1700fe12008-07-26 18:55:28 +0200886 if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
887 new_z1 -= B_FIFO_SIZE; /* buffer wrap */
888
889 new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);
890 src = bch->tx_skb->data + bch->tx_idx; /* source pointer */
891 dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);
892 maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);
Joe Perches475be4d2012-02-19 19:52:38 -0800893 /* end fifo */
Karsten Keil1700fe12008-07-26 18:55:28 +0200894 if (maxlen > count)
895 maxlen = count; /* limit size */
896 memcpy(dst, src, maxlen); /* first copy */
897
898 count -= maxlen; /* remaining bytes */
899 if (count) {
900 dst = bdata; /* start of buffer */
901 src += maxlen; /* new position */
902 memcpy(dst, src, count);
903 }
904 bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */
905 bz->f1 = new_f1; /* next frame */
906 dev_kfree_skb(bch->tx_skb);
907 get_next_bframe(bch);
908}
909
910
911
912/*
913 * handle L1 state changes TE
914 */
915
916static void
917ph_state_te(struct dchannel *dch)
918{
919 if (dch->debug)
920 printk(KERN_DEBUG "%s: TE newstate %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800921 __func__, dch->state);
Karsten Keil1700fe12008-07-26 18:55:28 +0200922 switch (dch->state) {
923 case 0:
924 l1_event(dch->l1, HW_RESET_IND);
925 break;
926 case 3:
927 l1_event(dch->l1, HW_DEACT_IND);
928 break;
929 case 5:
930 case 8:
931 l1_event(dch->l1, ANYSIGNAL);
932 break;
933 case 6:
934 l1_event(dch->l1, INFO2);
935 break;
936 case 7:
937 l1_event(dch->l1, INFO4_P8);
938 break;
939 }
940}
941
942/*
943 * handle L1 state changes NT
944 */
945
946static void
947handle_nt_timer3(struct dchannel *dch) {
948 struct hfc_pci *hc = dch->hw;
949
950 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
951 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
952 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
953 hc->hw.nt_timer = 0;
954 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
955 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
956 hc->hw.mst_m |= HFCPCI_MASTER;
957 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
958 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -0800959 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +0200960}
961
962static void
963ph_state_nt(struct dchannel *dch)
964{
965 struct hfc_pci *hc = dch->hw;
966
967 if (dch->debug)
968 printk(KERN_DEBUG "%s: NT newstate %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800969 __func__, dch->state);
Karsten Keil1700fe12008-07-26 18:55:28 +0200970 switch (dch->state) {
971 case 2:
972 if (hc->hw.nt_timer < 0) {
973 hc->hw.nt_timer = 0;
974 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
975 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
976 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
977 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
978 /* Clear already pending ints */
David S. Millera719e0a2011-04-17 16:34:50 -0700979 (void) Read_hfc(hc, HFCPCI_INT_S1);
Karsten Keil1700fe12008-07-26 18:55:28 +0200980 Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);
981 udelay(10);
982 Write_hfc(hc, HFCPCI_STATES, 4);
983 dch->state = 4;
984 } else if (hc->hw.nt_timer == 0) {
985 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
986 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
987 hc->hw.nt_timer = NT_T1_COUNT;
988 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
989 hc->hw.ctmt |= HFCPCI_TIM3_125;
990 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
Joe Perches475be4d2012-02-19 19:52:38 -0800991 HFCPCI_CLTIMER);
Karsten Keil1700fe12008-07-26 18:55:28 +0200992 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
993 test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);
994 /* allow G2 -> G3 transition */
995 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
996 } else {
997 Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
998 }
999 break;
1000 case 1:
1001 hc->hw.nt_timer = 0;
1002 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1003 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1004 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1005 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1006 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1007 hc->hw.mst_m &= ~HFCPCI_MASTER;
1008 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1009 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1010 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -08001011 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001012 break;
1013 case 4:
1014 hc->hw.nt_timer = 0;
1015 test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
1016 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1017 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1018 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1019 break;
1020 case 3:
1021 if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {
1022 if (!test_and_clear_bit(FLG_L2_ACTIVATED,
Joe Perches475be4d2012-02-19 19:52:38 -08001023 &dch->Flags)) {
Karsten Keil1700fe12008-07-26 18:55:28 +02001024 handle_nt_timer3(dch);
1025 break;
1026 }
1027 test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
1028 hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
1029 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1030 hc->hw.nt_timer = NT_T3_COUNT;
1031 hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
1032 hc->hw.ctmt |= HFCPCI_TIM3_125;
1033 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
Joe Perches475be4d2012-02-19 19:52:38 -08001034 HFCPCI_CLTIMER);
Karsten Keil1700fe12008-07-26 18:55:28 +02001035 }
1036 break;
1037 }
1038}
1039
1040static void
1041ph_state(struct dchannel *dch)
1042{
1043 struct hfc_pci *hc = dch->hw;
1044
1045 if (hc->hw.protocol == ISDN_P_NT_S0) {
1046 if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&
1047 hc->hw.nt_timer < 0)
1048 handle_nt_timer3(dch);
1049 else
1050 ph_state_nt(dch);
1051 } else
1052 ph_state_te(dch);
1053}
1054
1055/*
1056 * Layer 1 callback function
1057 */
1058static int
1059hfc_l1callback(struct dchannel *dch, u_int cmd)
1060{
1061 struct hfc_pci *hc = dch->hw;
1062
1063 switch (cmd) {
1064 case INFO3_P8:
1065 case INFO3_P10:
1066 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1067 hc->hw.mst_m |= HFCPCI_MASTER;
1068 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1069 break;
1070 case HW_RESET_REQ:
1071 Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);
1072 /* HFC ST 3 */
1073 udelay(6);
1074 Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */
1075 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1076 hc->hw.mst_m |= HFCPCI_MASTER;
1077 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1078 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -08001079 HFCPCI_DO_ACTION);
Karsten Keil1700fe12008-07-26 18:55:28 +02001080 l1_event(dch->l1, HW_POWERUP_IND);
1081 break;
1082 case HW_DEACT_REQ:
1083 hc->hw.mst_m &= ~HFCPCI_MASTER;
1084 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1085 skb_queue_purge(&dch->squeue);
1086 if (dch->tx_skb) {
1087 dev_kfree_skb(dch->tx_skb);
1088 dch->tx_skb = NULL;
1089 }
1090 dch->tx_idx = 0;
1091 if (dch->rx_skb) {
1092 dev_kfree_skb(dch->rx_skb);
1093 dch->rx_skb = NULL;
1094 }
1095 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1096 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1097 del_timer(&dch->timer);
1098 break;
1099 case HW_POWERUP_REQ:
1100 Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);
1101 break;
1102 case PH_ACTIVATE_IND:
1103 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
1104 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
Joe Perches475be4d2012-02-19 19:52:38 -08001105 GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001106 break;
1107 case PH_DEACTIVATE_IND:
1108 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1109 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
Joe Perches475be4d2012-02-19 19:52:38 -08001110 GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001111 break;
1112 default:
1113 if (dch->debug & DEBUG_HW)
1114 printk(KERN_DEBUG "%s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001115 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02001116 return -1;
1117 }
1118 return 0;
1119}
1120
1121/*
1122 * Interrupt handler
1123 */
1124static inline void
1125tx_birq(struct bchannel *bch)
1126{
1127 if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
1128 hfcpci_fill_fifo(bch);
1129 else {
1130 if (bch->tx_skb)
1131 dev_kfree_skb(bch->tx_skb);
1132 if (get_next_bframe(bch))
1133 hfcpci_fill_fifo(bch);
1134 }
1135}
1136
1137static inline void
1138tx_dirq(struct dchannel *dch)
1139{
1140 if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)
1141 hfcpci_fill_dfifo(dch->hw);
1142 else {
1143 if (dch->tx_skb)
1144 dev_kfree_skb(dch->tx_skb);
1145 if (get_next_dframe(dch))
1146 hfcpci_fill_dfifo(dch->hw);
1147 }
1148}
1149
1150static irqreturn_t
1151hfcpci_int(int intno, void *dev_id)
1152{
1153 struct hfc_pci *hc = dev_id;
1154 u_char exval;
1155 struct bchannel *bch;
1156 u_char val, stat;
1157
1158 spin_lock(&hc->lock);
1159 if (!(hc->hw.int_m2 & 0x08)) {
1160 spin_unlock(&hc->lock);
1161 return IRQ_NONE; /* not initialised */
1162 }
1163 stat = Read_hfc(hc, HFCPCI_STATUS);
1164 if (HFCPCI_ANYINT & stat) {
1165 val = Read_hfc(hc, HFCPCI_INT_S1);
1166 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1167 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001168 "HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);
Karsten Keil1700fe12008-07-26 18:55:28 +02001169 } else {
1170 /* shared */
1171 spin_unlock(&hc->lock);
1172 return IRQ_NONE;
1173 }
1174 hc->irqcnt++;
1175
1176 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1177 printk(KERN_DEBUG "HFC-PCI irq %x\n", val);
1178 val &= hc->hw.int_m1;
1179 if (val & 0x40) { /* state machine irq */
1180 exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;
1181 if (hc->dch.debug & DEBUG_HW_DCHANNEL)
1182 printk(KERN_DEBUG "ph_state chg %d->%d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001183 hc->dch.state, exval);
Karsten Keil1700fe12008-07-26 18:55:28 +02001184 hc->dch.state = exval;
1185 schedule_event(&hc->dch, FLG_PHCHANGE);
1186 val &= ~0x40;
1187 }
1188 if (val & 0x80) { /* timer irq */
1189 if (hc->hw.protocol == ISDN_P_NT_S0) {
1190 if ((--hc->hw.nt_timer) < 0)
1191 schedule_event(&hc->dch, FLG_PHCHANGE);
1192 }
1193 val &= ~0x80;
1194 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);
1195 }
Joe Perches475be4d2012-02-19 19:52:38 -08001196 if (val & 0x08) { /* B1 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001197 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1198 if (bch)
1199 main_rec_hfcpci(bch);
1200 else if (hc->dch.debug)
1201 printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");
1202 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001203 if (val & 0x10) { /* B2 rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001204 bch = Sel_BCS(hc, 2);
1205 if (bch)
1206 main_rec_hfcpci(bch);
1207 else if (hc->dch.debug)
1208 printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");
1209 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001210 if (val & 0x01) { /* B1 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001211 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
1212 if (bch)
1213 tx_birq(bch);
1214 else if (hc->dch.debug)
1215 printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");
1216 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001217 if (val & 0x02) { /* B2 tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001218 bch = Sel_BCS(hc, 2);
1219 if (bch)
1220 tx_birq(bch);
1221 else if (hc->dch.debug)
1222 printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");
1223 }
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001224 if (val & 0x20) /* D rx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001225 receive_dmsg(hc);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001226 if (val & 0x04) { /* D tx */
Karsten Keil1700fe12008-07-26 18:55:28 +02001227 if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))
1228 del_timer(&hc->dch.timer);
1229 tx_dirq(&hc->dch);
1230 }
1231 spin_unlock(&hc->lock);
1232 return IRQ_HANDLED;
1233}
1234
1235/*
1236 * timer callback for D-chan busy resolution. Currently no function
1237 */
1238static void
1239hfcpci_dbusy_timer(struct hfc_pci *hc)
1240{
1241}
1242
1243/*
1244 * activate/deactivate hardware for selected channels and mode
1245 */
1246static int
1247mode_hfcpci(struct bchannel *bch, int bc, int protocol)
1248{
1249 struct hfc_pci *hc = bch->hw;
1250 int fifo2;
1251 u_char rx_slot = 0, tx_slot = 0, pcm_mode;
1252
1253 if (bch->debug & DEBUG_HW_BCHANNEL)
1254 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001255 "HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",
1256 bch->state, protocol, bch->nr, bc);
Karsten Keil1700fe12008-07-26 18:55:28 +02001257
1258 fifo2 = bc;
Joe Perches475be4d2012-02-19 19:52:38 -08001259 pcm_mode = (bc >> 24) & 0xff;
Karsten Keil1700fe12008-07-26 18:55:28 +02001260 if (pcm_mode) { /* PCM SLOT USE */
1261 if (!test_bit(HFC_CFG_PCM, &hc->cfg))
1262 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001263 "%s: pcm channel id without HFC_CFG_PCM\n",
1264 __func__);
1265 rx_slot = (bc >> 8) & 0xff;
1266 tx_slot = (bc >> 16) & 0xff;
Karsten Keil1700fe12008-07-26 18:55:28 +02001267 bc = bc & 0xff;
Karsten Keileac74af2009-05-22 11:04:56 +00001268 } else if (test_bit(HFC_CFG_PCM, &hc->cfg) && (protocol > ISDN_P_NONE))
Karsten Keil1700fe12008-07-26 18:55:28 +02001269 printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001270 __func__);
Karsten Keil1700fe12008-07-26 18:55:28 +02001271 if (hc->chanlimit > 1) {
1272 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1273 hc->hw.sctrl_e &= ~0x80;
1274 } else {
1275 if (bc & 2) {
1276 if (protocol != ISDN_P_NONE) {
1277 hc->hw.bswapped = 1; /* B1 and B2 exchanged */
1278 hc->hw.sctrl_e |= 0x80;
1279 } else {
1280 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1281 hc->hw.sctrl_e &= ~0x80;
1282 }
1283 fifo2 = 1;
1284 } else {
1285 hc->hw.bswapped = 0; /* B1 and B2 normal mode */
1286 hc->hw.sctrl_e &= ~0x80;
1287 }
1288 }
1289 switch (protocol) {
1290 case (-1): /* used for init */
1291 bch->state = -1;
1292 bch->nr = bc;
1293 case (ISDN_P_NONE):
1294 if (bch->state == ISDN_P_NONE)
1295 return 0;
1296 if (bc & 2) {
1297 hc->hw.sctrl &= ~SCTRL_B2_ENA;
1298 hc->hw.sctrl_r &= ~SCTRL_B2_ENA;
1299 } else {
1300 hc->hw.sctrl &= ~SCTRL_B1_ENA;
1301 hc->hw.sctrl_r &= ~SCTRL_B1_ENA;
1302 }
1303 if (fifo2 & 2) {
1304 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;
1305 hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001306 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001307 } else {
1308 hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;
1309 hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001310 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001311 }
1312#ifdef REVERSE_BITORDER
1313 if (bch->nr & 2)
1314 hc->hw.cirm &= 0x7f;
1315 else
1316 hc->hw.cirm &= 0xbf;
1317#endif
1318 bch->state = ISDN_P_NONE;
1319 bch->nr = bc;
1320 test_and_clear_bit(FLG_HDLC, &bch->Flags);
1321 test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
1322 break;
1323 case (ISDN_P_B_RAW):
1324 bch->state = protocol;
1325 bch->nr = bc;
Karsten Keileac74af2009-05-22 11:04:56 +00001326 hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
1327 hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001328 if (bc & 2) {
1329 hc->hw.sctrl |= SCTRL_B2_ENA;
1330 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1331#ifdef REVERSE_BITORDER
1332 hc->hw.cirm |= 0x80;
1333#endif
1334 } else {
1335 hc->hw.sctrl |= SCTRL_B1_ENA;
1336 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1337#ifdef REVERSE_BITORDER
1338 hc->hw.cirm |= 0x40;
1339#endif
1340 }
1341 if (fifo2 & 2) {
1342 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001343 if (!tics)
1344 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001345 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001346 hc->hw.ctmt |= 2;
1347 hc->hw.conn &= ~0x18;
1348 } else {
1349 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001350 if (!tics)
1351 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001352 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001353 hc->hw.ctmt |= 1;
1354 hc->hw.conn &= ~0x03;
1355 }
1356 test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
1357 break;
1358 case (ISDN_P_B_HDLC):
1359 bch->state = protocol;
1360 bch->nr = bc;
Karsten Keileac74af2009-05-22 11:04:56 +00001361 hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
1362 hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001363 if (bc & 2) {
1364 hc->hw.sctrl |= SCTRL_B2_ENA;
1365 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1366 } else {
1367 hc->hw.sctrl |= SCTRL_B1_ENA;
1368 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1369 }
1370 if (fifo2 & 2) {
1371 hc->hw.last_bfifo_cnt[1] = 0;
1372 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
1373 hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001374 HFCPCI_INTS_B2REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001375 hc->hw.ctmt &= ~2;
1376 hc->hw.conn &= ~0x18;
1377 } else {
1378 hc->hw.last_bfifo_cnt[0] = 0;
1379 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
1380 hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
Joe Perches475be4d2012-02-19 19:52:38 -08001381 HFCPCI_INTS_B1REC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001382 hc->hw.ctmt &= ~1;
1383 hc->hw.conn &= ~0x03;
1384 }
1385 test_and_set_bit(FLG_HDLC, &bch->Flags);
1386 break;
1387 default:
1388 printk(KERN_DEBUG "prot not known %x\n", protocol);
1389 return -ENOPROTOOPT;
1390 }
1391 if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
1392 if ((protocol == ISDN_P_NONE) ||
Joe Perches475be4d2012-02-19 19:52:38 -08001393 (protocol == -1)) { /* init case */
Karsten Keil1700fe12008-07-26 18:55:28 +02001394 rx_slot = 0;
1395 tx_slot = 0;
1396 } else {
1397 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
1398 rx_slot |= 0xC0;
1399 tx_slot |= 0xC0;
1400 } else {
1401 rx_slot |= 0x80;
1402 tx_slot |= 0x80;
1403 }
1404 }
1405 if (bc & 2) {
1406 hc->hw.conn &= 0xc7;
1407 hc->hw.conn |= 0x08;
1408 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001409 __func__, tx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001410 printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001411 __func__, rx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001412 Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);
1413 Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);
1414 } else {
1415 hc->hw.conn &= 0xf8;
1416 hc->hw.conn |= 0x01;
1417 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001418 __func__, tx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001419 printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001420 __func__, rx_slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001421 Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);
1422 Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);
1423 }
1424 }
1425 Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
1426 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1427 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1428 Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
1429 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1430 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1431 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1432#ifdef REVERSE_BITORDER
1433 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1434#endif
1435 return 0;
1436}
1437
1438static int
1439set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)
1440{
1441 struct hfc_pci *hc = bch->hw;
1442
1443 if (bch->debug & DEBUG_HW_BCHANNEL)
1444 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001445 "HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",
1446 bch->state, protocol, bch->nr, chan);
Karsten Keil1700fe12008-07-26 18:55:28 +02001447 if (bch->nr != chan) {
1448 printk(KERN_DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -08001449 "HFCPCI rxtest wrong channel parameter %x/%x\n",
1450 bch->nr, chan);
Karsten Keil1700fe12008-07-26 18:55:28 +02001451 return -EINVAL;
1452 }
1453 switch (protocol) {
1454 case (ISDN_P_B_RAW):
1455 bch->state = protocol;
Karsten Keileac74af2009-05-22 11:04:56 +00001456 hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001457 if (chan & 2) {
1458 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1459 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001460 if (!tics)
1461 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001462 hc->hw.ctmt |= 2;
1463 hc->hw.conn &= ~0x18;
1464#ifdef REVERSE_BITORDER
1465 hc->hw.cirm |= 0x80;
1466#endif
1467 } else {
1468 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1469 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02001470 if (!tics)
1471 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
Karsten Keil1700fe12008-07-26 18:55:28 +02001472 hc->hw.ctmt |= 1;
1473 hc->hw.conn &= ~0x03;
1474#ifdef REVERSE_BITORDER
1475 hc->hw.cirm |= 0x40;
1476#endif
1477 }
1478 break;
1479 case (ISDN_P_B_HDLC):
1480 bch->state = protocol;
Karsten Keileac74af2009-05-22 11:04:56 +00001481 hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
Karsten Keil1700fe12008-07-26 18:55:28 +02001482 if (chan & 2) {
1483 hc->hw.sctrl_r |= SCTRL_B2_ENA;
1484 hc->hw.last_bfifo_cnt[1] = 0;
1485 hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
1486 hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
1487 hc->hw.ctmt &= ~2;
1488 hc->hw.conn &= ~0x18;
1489 } else {
1490 hc->hw.sctrl_r |= SCTRL_B1_ENA;
1491 hc->hw.last_bfifo_cnt[0] = 0;
1492 hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
1493 hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
1494 hc->hw.ctmt &= ~1;
1495 hc->hw.conn &= ~0x03;
1496 }
1497 break;
1498 default:
1499 printk(KERN_DEBUG "prot not known %x\n", protocol);
1500 return -ENOPROTOOPT;
1501 }
1502 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1503 Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
1504 Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
1505 Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
1506 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1507#ifdef REVERSE_BITORDER
1508 Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
1509#endif
1510 return 0;
1511}
1512
1513static void
1514deactivate_bchannel(struct bchannel *bch)
1515{
1516 struct hfc_pci *hc = bch->hw;
1517 u_long flags;
1518
1519 spin_lock_irqsave(&hc->lock, flags);
Karsten Keilfb286f02009-07-09 10:02:29 +02001520 mISDN_clear_bchannel(bch);
Karsten Keil1700fe12008-07-26 18:55:28 +02001521 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
Karsten Keil1700fe12008-07-26 18:55:28 +02001522 spin_unlock_irqrestore(&hc->lock, flags);
1523}
1524
1525/*
1526 * Layer 1 B-channel hardware access
1527 */
1528static int
1529channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1530{
Karsten Keil6d1ee482012-05-15 23:51:07 +00001531 return mISDN_ctrl_bchannel(bch, cq);
Karsten Keil1700fe12008-07-26 18:55:28 +02001532}
1533static int
1534hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1535{
1536 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1537 struct hfc_pci *hc = bch->hw;
1538 int ret = -EINVAL;
1539 u_long flags;
1540
1541 if (bch->debug & DEBUG_HW)
1542 printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
1543 switch (cmd) {
1544 case HW_TESTRX_RAW:
1545 spin_lock_irqsave(&hc->lock, flags);
1546 ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);
1547 spin_unlock_irqrestore(&hc->lock, flags);
1548 break;
1549 case HW_TESTRX_HDLC:
1550 spin_lock_irqsave(&hc->lock, flags);
1551 ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);
1552 spin_unlock_irqrestore(&hc->lock, flags);
1553 break;
1554 case HW_TESTRX_OFF:
1555 spin_lock_irqsave(&hc->lock, flags);
1556 mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
1557 spin_unlock_irqrestore(&hc->lock, flags);
1558 ret = 0;
1559 break;
1560 case CLOSE_CHANNEL:
1561 test_and_clear_bit(FLG_OPEN, &bch->Flags);
Karsten Keil13681122012-05-15 23:51:01 +00001562 deactivate_bchannel(bch);
Karsten Keil1700fe12008-07-26 18:55:28 +02001563 ch->protocol = ISDN_P_NONE;
1564 ch->peer = NULL;
1565 module_put(THIS_MODULE);
1566 ret = 0;
1567 break;
1568 case CONTROL_CHANNEL:
1569 ret = channel_bctrl(bch, arg);
1570 break;
1571 default:
1572 printk(KERN_WARNING "%s: unknown prim(%x)\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001573 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02001574 }
1575 return ret;
1576}
1577
1578/*
1579 * Layer2 -> Layer 1 Dchannel data
1580 */
1581static int
1582hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
1583{
1584 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1585 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1586 struct hfc_pci *hc = dch->hw;
1587 int ret = -EINVAL;
1588 struct mISDNhead *hh = mISDN_HEAD_P(skb);
1589 unsigned int id;
1590 u_long flags;
1591
1592 switch (hh->prim) {
1593 case PH_DATA_REQ:
1594 spin_lock_irqsave(&hc->lock, flags);
1595 ret = dchannel_senddata(dch, skb);
1596 if (ret > 0) { /* direct TX */
1597 id = hh->id; /* skb can be freed */
1598 hfcpci_fill_dfifo(dch->hw);
1599 ret = 0;
1600 spin_unlock_irqrestore(&hc->lock, flags);
1601 queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
1602 } else
1603 spin_unlock_irqrestore(&hc->lock, flags);
1604 return ret;
1605 case PH_ACTIVATE_REQ:
1606 spin_lock_irqsave(&hc->lock, flags);
1607 if (hc->hw.protocol == ISDN_P_NT_S0) {
1608 ret = 0;
1609 if (test_bit(HFC_CFG_MASTER, &hc->cfg))
1610 hc->hw.mst_m |= HFCPCI_MASTER;
1611 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1612 if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1613 spin_unlock_irqrestore(&hc->lock, flags);
1614 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
Joe Perches475be4d2012-02-19 19:52:38 -08001615 MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
Karsten Keil1700fe12008-07-26 18:55:28 +02001616 break;
1617 }
1618 test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);
1619 Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
Joe Perches475be4d2012-02-19 19:52:38 -08001620 HFCPCI_DO_ACTION | 1);
Karsten Keil1700fe12008-07-26 18:55:28 +02001621 } else
1622 ret = l1_event(dch->l1, hh->prim);
1623 spin_unlock_irqrestore(&hc->lock, flags);
1624 break;
1625 case PH_DEACTIVATE_REQ:
1626 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1627 spin_lock_irqsave(&hc->lock, flags);
1628 if (hc->hw.protocol == ISDN_P_NT_S0) {
1629 /* prepare deactivation */
1630 Write_hfc(hc, HFCPCI_STATES, 0x40);
1631 skb_queue_purge(&dch->squeue);
1632 if (dch->tx_skb) {
1633 dev_kfree_skb(dch->tx_skb);
1634 dch->tx_skb = NULL;
1635 }
1636 dch->tx_idx = 0;
1637 if (dch->rx_skb) {
1638 dev_kfree_skb(dch->rx_skb);
1639 dch->rx_skb = NULL;
1640 }
1641 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1642 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1643 del_timer(&dch->timer);
1644#ifdef FIXME
1645 if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
1646 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
1647#endif
1648 hc->hw.mst_m &= ~HFCPCI_MASTER;
1649 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1650 ret = 0;
1651 } else {
1652 ret = l1_event(dch->l1, hh->prim);
1653 }
1654 spin_unlock_irqrestore(&hc->lock, flags);
1655 break;
1656 }
1657 if (!ret)
1658 dev_kfree_skb(skb);
1659 return ret;
1660}
1661
1662/*
1663 * Layer2 -> Layer 1 Bchannel data
1664 */
1665static int
1666hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
1667{
1668 struct bchannel *bch = container_of(ch, struct bchannel, ch);
1669 struct hfc_pci *hc = bch->hw;
1670 int ret = -EINVAL;
1671 struct mISDNhead *hh = mISDN_HEAD_P(skb);
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001672 unsigned long flags;
Karsten Keil1700fe12008-07-26 18:55:28 +02001673
1674 switch (hh->prim) {
1675 case PH_DATA_REQ:
1676 spin_lock_irqsave(&hc->lock, flags);
1677 ret = bchannel_senddata(bch, skb);
1678 if (ret > 0) { /* direct TX */
Karsten Keil1700fe12008-07-26 18:55:28 +02001679 hfcpci_fill_fifo(bch);
1680 ret = 0;
Karsten Keil8bfddfb2012-05-15 23:51:02 +00001681 }
1682 spin_unlock_irqrestore(&hc->lock, flags);
Karsten Keil1700fe12008-07-26 18:55:28 +02001683 return ret;
1684 case PH_ACTIVATE_REQ:
1685 spin_lock_irqsave(&hc->lock, flags);
1686 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
1687 ret = mode_hfcpci(bch, bch->nr, ch->protocol);
1688 else
1689 ret = 0;
1690 spin_unlock_irqrestore(&hc->lock, flags);
1691 if (!ret)
1692 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08001693 NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001694 break;
1695 case PH_DEACTIVATE_REQ:
1696 deactivate_bchannel(bch);
1697 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08001698 NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001699 ret = 0;
1700 break;
1701 }
1702 if (!ret)
1703 dev_kfree_skb(skb);
1704 return ret;
1705}
1706
1707/*
1708 * called for card init message
1709 */
1710
Harvey Harrison1532dcb2008-09-22 19:16:51 -07001711static void
Karsten Keil1700fe12008-07-26 18:55:28 +02001712inithfcpci(struct hfc_pci *hc)
1713{
1714 printk(KERN_DEBUG "inithfcpci: entered\n");
1715 hc->dch.timer.function = (void *) hfcpci_dbusy_timer;
1716 hc->dch.timer.data = (long) &hc->dch;
1717 init_timer(&hc->dch.timer);
1718 hc->chanlimit = 2;
1719 mode_hfcpci(&hc->bch[0], 1, -1);
1720 mode_hfcpci(&hc->bch[1], 2, -1);
1721}
1722
1723
1724static int
1725init_card(struct hfc_pci *hc)
1726{
1727 int cnt = 3;
1728 u_long flags;
1729
1730 printk(KERN_DEBUG "init_card: entered\n");
1731
1732
1733 spin_lock_irqsave(&hc->lock, flags);
1734 disable_hwirq(hc);
1735 spin_unlock_irqrestore(&hc->lock, flags);
1736 if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {
1737 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001738 "mISDN: couldn't get interrupt %d\n", hc->irq);
Karsten Keil1700fe12008-07-26 18:55:28 +02001739 return -EIO;
1740 }
1741 spin_lock_irqsave(&hc->lock, flags);
1742 reset_hfcpci(hc);
1743 while (cnt) {
1744 inithfcpci(hc);
1745 /*
1746 * Finally enable IRQ output
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001747 * this is only allowed, if an IRQ routine is already
Karsten Keil1700fe12008-07-26 18:55:28 +02001748 * established for this HFC, so don't do that earlier
1749 */
1750 enable_hwirq(hc);
1751 spin_unlock_irqrestore(&hc->lock, flags);
1752 /* Timeout 80ms */
1753 current->state = TASK_UNINTERRUPTIBLE;
Joe Perches475be4d2012-02-19 19:52:38 -08001754 schedule_timeout((80 * HZ) / 1000);
Karsten Keil1700fe12008-07-26 18:55:28 +02001755 printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001756 hc->irq, hc->irqcnt);
Karsten Keil1700fe12008-07-26 18:55:28 +02001757 /* now switch timer interrupt off */
1758 spin_lock_irqsave(&hc->lock, flags);
1759 hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
1760 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
1761 /* reinit mode reg */
1762 Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
1763 if (!hc->irqcnt) {
1764 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08001765 "HFC PCI: IRQ(%d) getting no interrupts "
1766 "during init %d\n", hc->irq, 4 - cnt);
Andreas Mohrcdae28e2009-06-02 18:15:12 +02001767 if (cnt == 1)
1768 break;
1769 else {
Karsten Keil1700fe12008-07-26 18:55:28 +02001770 reset_hfcpci(hc);
1771 cnt--;
1772 }
1773 } else {
1774 spin_unlock_irqrestore(&hc->lock, flags);
1775 hc->initdone = 1;
1776 return 0;
1777 }
1778 }
1779 disable_hwirq(hc);
1780 spin_unlock_irqrestore(&hc->lock, flags);
1781 free_irq(hc->irq, hc);
1782 return -EIO;
1783}
1784
1785static int
1786channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)
1787{
1788 int ret = 0;
1789 u_char slot;
1790
1791 switch (cq->op) {
1792 case MISDN_CTRL_GETOP:
1793 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
Karsten Keilc626c122012-05-04 04:15:33 +00001794 MISDN_CTRL_DISCONNECT | MISDN_CTRL_L1_TIMER3;
Karsten Keil1700fe12008-07-26 18:55:28 +02001795 break;
1796 case MISDN_CTRL_LOOP:
1797 /* channel 0 disabled loop */
1798 if (cq->channel < 0 || cq->channel > 2) {
1799 ret = -EINVAL;
1800 break;
1801 }
1802 if (cq->channel & 1) {
1803 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1804 slot = 0xC0;
1805 else
1806 slot = 0x80;
1807 printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001808 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001809 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1810 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1811 hc->hw.conn = (hc->hw.conn & ~7) | 6;
1812 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1813 }
1814 if (cq->channel & 2) {
1815 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1816 slot = 0xC1;
1817 else
1818 slot = 0x81;
1819 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001820 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001821 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1822 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1823 hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;
1824 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1825 }
1826 if (cq->channel & 3)
1827 hc->hw.trm |= 0x80; /* enable IOM-loop */
1828 else {
1829 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1830 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1831 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1832 }
1833 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1834 break;
1835 case MISDN_CTRL_CONNECT:
1836 if (cq->channel == cq->p1) {
1837 ret = -EINVAL;
1838 break;
1839 }
1840 if (cq->channel < 1 || cq->channel > 2 ||
1841 cq->p1 < 1 || cq->p1 > 2) {
1842 ret = -EINVAL;
1843 break;
1844 }
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",
Joe Perches475be4d2012-02-19 19:52:38 -08001850 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001851 Write_hfc(hc, HFCPCI_B1_SSL, slot);
1852 Write_hfc(hc, HFCPCI_B2_RSL, slot);
1853 if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
1854 slot = 0xC1;
1855 else
1856 slot = 0x81;
1857 printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001858 __func__, slot);
Karsten Keil1700fe12008-07-26 18:55:28 +02001859 Write_hfc(hc, HFCPCI_B2_SSL, slot);
1860 Write_hfc(hc, HFCPCI_B1_RSL, slot);
1861 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;
1862 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1863 hc->hw.trm |= 0x80;
1864 Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
1865 break;
1866 case MISDN_CTRL_DISCONNECT:
1867 hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
1868 Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
1869 hc->hw.trm &= 0x7f; /* disable IOM-loop */
1870 break;
Karsten Keilc626c122012-05-04 04:15:33 +00001871 case MISDN_CTRL_L1_TIMER3:
1872 ret = l1_event(hc->dch.l1, HW_TIMER3_VALUE | (cq->p1 & 0xff));
1873 break;
Karsten Keil1700fe12008-07-26 18:55:28 +02001874 default:
1875 printk(KERN_WARNING "%s: unknown Op %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001876 __func__, cq->op);
Karsten Keil1700fe12008-07-26 18:55:28 +02001877 ret = -EINVAL;
1878 break;
1879 }
1880 return ret;
1881}
1882
1883static int
1884open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,
Joe Perches475be4d2012-02-19 19:52:38 -08001885 struct channel_req *rq)
Karsten Keil1700fe12008-07-26 18:55:28 +02001886{
1887 int err = 0;
1888
1889 if (debug & DEBUG_HW_OPEN)
1890 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -08001891 hc->dch.dev.id, __builtin_return_address(0));
Karsten Keil1700fe12008-07-26 18:55:28 +02001892 if (rq->protocol == ISDN_P_NONE)
1893 return -EINVAL;
Martin Bachem55a6af92008-09-04 12:42:39 +02001894 if (rq->adr.channel == 1) {
1895 /* TODO: E-Channel */
1896 return -EINVAL;
1897 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001898 if (!hc->initdone) {
1899 if (rq->protocol == ISDN_P_TE_S0) {
1900 err = create_l1(&hc->dch, hfc_l1callback);
1901 if (err)
1902 return err;
1903 }
1904 hc->hw.protocol = rq->protocol;
1905 ch->protocol = rq->protocol;
1906 err = init_card(hc);
1907 if (err)
1908 return err;
1909 } else {
1910 if (rq->protocol != ch->protocol) {
1911 if (hc->hw.protocol == ISDN_P_TE_S0)
1912 l1_event(hc->dch.l1, CLOSE_CHANNEL);
Andreas Eversbergc3b3cde2008-11-09 10:23:19 +01001913 if (rq->protocol == ISDN_P_TE_S0) {
1914 err = create_l1(&hc->dch, hfc_l1callback);
1915 if (err)
1916 return err;
1917 }
Karsten Keil1700fe12008-07-26 18:55:28 +02001918 hc->hw.protocol = rq->protocol;
1919 ch->protocol = rq->protocol;
1920 hfcpci_setmode(hc);
1921 }
1922 }
1923
1924 if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||
1925 ((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {
1926 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
Joe Perches475be4d2012-02-19 19:52:38 -08001927 0, NULL, GFP_KERNEL);
Karsten Keil1700fe12008-07-26 18:55:28 +02001928 }
1929 rq->ch = ch;
1930 if (!try_module_get(THIS_MODULE))
1931 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1932 return 0;
1933}
1934
1935static int
1936open_bchannel(struct hfc_pci *hc, struct channel_req *rq)
1937{
1938 struct bchannel *bch;
1939
Dan Carpenter819a1002012-03-26 21:20:48 +00001940 if (rq->adr.channel == 0 || rq->adr.channel > 2)
Karsten Keil1700fe12008-07-26 18:55:28 +02001941 return -EINVAL;
1942 if (rq->protocol == ISDN_P_NONE)
1943 return -EINVAL;
1944 bch = &hc->bch[rq->adr.channel - 1];
1945 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1946 return -EBUSY; /* b-channel can be only open once */
1947 bch->ch.protocol = rq->protocol;
1948 rq->ch = &bch->ch; /* TODO: E-channel */
1949 if (!try_module_get(THIS_MODULE))
1950 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1951 return 0;
1952}
1953
1954/*
1955 * device control function
1956 */
1957static int
1958hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1959{
1960 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1961 struct dchannel *dch = container_of(dev, struct dchannel, dev);
1962 struct hfc_pci *hc = dch->hw;
1963 struct channel_req *rq;
1964 int err = 0;
1965
1966 if (dch->debug & DEBUG_HW)
1967 printk(KERN_DEBUG "%s: cmd:%x %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001968 __func__, cmd, arg);
Karsten Keil1700fe12008-07-26 18:55:28 +02001969 switch (cmd) {
1970 case OPEN_CHANNEL:
1971 rq = arg;
Martin Bachema9b61832008-09-03 18:08:30 +02001972 if ((rq->protocol == ISDN_P_TE_S0) ||
1973 (rq->protocol == ISDN_P_NT_S0))
Karsten Keil1700fe12008-07-26 18:55:28 +02001974 err = open_dchannel(hc, ch, rq);
1975 else
1976 err = open_bchannel(hc, rq);
1977 break;
1978 case CLOSE_CHANNEL:
1979 if (debug & DEBUG_HW_OPEN)
1980 printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001981 __func__, hc->dch.dev.id,
1982 __builtin_return_address(0));
Karsten Keil1700fe12008-07-26 18:55:28 +02001983 module_put(THIS_MODULE);
1984 break;
1985 case CONTROL_CHANNEL:
1986 err = channel_ctrl(hc, arg);
1987 break;
1988 default:
1989 if (dch->debug & DEBUG_HW)
1990 printk(KERN_DEBUG "%s: unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001991 __func__, cmd);
Karsten Keil1700fe12008-07-26 18:55:28 +02001992 return -EINVAL;
1993 }
1994 return err;
1995}
1996
1997static int
1998setup_hw(struct hfc_pci *hc)
1999{
2000 void *buffer;
2001
2002 printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);
2003 hc->hw.cirm = 0;
2004 hc->dch.state = 0;
2005 pci_set_master(hc->pdev);
2006 if (!hc->irq) {
2007 printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");
2008 return 1;
2009 }
Karsten Keileac74af2009-05-22 11:04:56 +00002010 hc->hw.pci_io =
2011 (char __iomem *)(unsigned long)hc->pdev->resource[1].start;
Karsten Keil1700fe12008-07-26 18:55:28 +02002012
2013 if (!hc->hw.pci_io) {
2014 printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");
2015 return 1;
2016 }
2017 /* Allocate memory for FIFOS */
2018 /* the memory needs to be on a 32k boundary within the first 4G */
2019 pci_set_dma_mask(hc->pdev, 0xFFFF8000);
2020 buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);
2021 /* We silently assume the address is okay if nonzero */
2022 if (!buffer) {
2023 printk(KERN_WARNING
Joe Perches475be4d2012-02-19 19:52:38 -08002024 "HFC-PCI: Error allocating memory for FIFO!\n");
Karsten Keil1700fe12008-07-26 18:55:28 +02002025 return 1;
2026 }
2027 hc->hw.fifos = buffer;
2028 pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);
2029 hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);
2030 printk(KERN_INFO
Joe Perches475be4d2012-02-19 19:52:38 -08002031 "HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",
2032 (u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,
2033 (u_long) hc->hw.dmahandle, hc->irq, HZ);
Karsten Keil1700fe12008-07-26 18:55:28 +02002034 /* enable memory mapped ports, disable busmaster */
2035 pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
2036 hc->hw.int_m2 = 0;
2037 disable_hwirq(hc);
2038 hc->hw.int_m1 = 0;
2039 Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
2040 /* At this point the needed PCI config is done */
2041 /* fifos are still not enabled */
2042 hc->hw.timer.function = (void *) hfcpci_Timer;
2043 hc->hw.timer.data = (long) hc;
2044 init_timer(&hc->hw.timer);
2045 /* default PCM master */
2046 test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);
2047 return 0;
2048}
2049
2050static void
2051release_card(struct hfc_pci *hc) {
2052 u_long flags;
2053
2054 spin_lock_irqsave(&hc->lock, flags);
2055 hc->hw.int_m2 = 0; /* interrupt output off ! */
2056 disable_hwirq(hc);
2057 mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);
2058 mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);
2059 if (hc->dch.timer.function != NULL) {
2060 del_timer(&hc->dch.timer);
2061 hc->dch.timer.function = NULL;
2062 }
2063 spin_unlock_irqrestore(&hc->lock, flags);
2064 if (hc->hw.protocol == ISDN_P_TE_S0)
2065 l1_event(hc->dch.l1, CLOSE_CHANNEL);
2066 if (hc->initdone)
2067 free_irq(hc->irq, hc);
2068 release_io_hfcpci(hc); /* must release after free_irq! */
2069 mISDN_unregister_device(&hc->dch.dev);
2070 mISDN_freebchannel(&hc->bch[1]);
2071 mISDN_freebchannel(&hc->bch[0]);
2072 mISDN_freedchannel(&hc->dch);
Karsten Keil1700fe12008-07-26 18:55:28 +02002073 pci_set_drvdata(hc->pdev, NULL);
2074 kfree(hc);
2075}
2076
2077static int
2078setup_card(struct hfc_pci *card)
2079{
2080 int err = -EINVAL;
2081 u_int i;
Karsten Keil1700fe12008-07-26 18:55:28 +02002082 char name[MISDN_MAX_IDLEN];
2083
Karsten Keil1700fe12008-07-26 18:55:28 +02002084 card->dch.debug = debug;
2085 spin_lock_init(&card->lock);
2086 mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);
2087 card->dch.hw = card;
2088 card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
2089 card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08002090 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil1700fe12008-07-26 18:55:28 +02002091 card->dch.dev.D.send = hfcpci_l2l1D;
2092 card->dch.dev.D.ctrl = hfc_dctrl;
2093 card->dch.dev.nrbchan = 2;
2094 for (i = 0; i < 2; i++) {
2095 card->bch[i].nr = i + 1;
Karsten Keilff4cc1d2008-07-30 18:26:58 +02002096 set_channelmap(i + 1, card->dch.dev.channelmap);
Karsten Keil1700fe12008-07-26 18:55:28 +02002097 card->bch[i].debug = debug;
Karsten Keil034005a2012-05-15 23:51:06 +00002098 mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM, poll >> 1);
Karsten Keil1700fe12008-07-26 18:55:28 +02002099 card->bch[i].hw = card;
2100 card->bch[i].ch.send = hfcpci_l2l1B;
2101 card->bch[i].ch.ctrl = hfc_bctrl;
2102 card->bch[i].ch.nr = i + 1;
2103 list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);
2104 }
2105 err = setup_hw(card);
2106 if (err)
2107 goto error;
2108 snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002109 err = mISDN_register_device(&card->dch.dev, &card->pdev->dev, name);
Karsten Keil1700fe12008-07-26 18:55:28 +02002110 if (err)
2111 goto error;
2112 HFC_cnt++;
Karsten Keil1700fe12008-07-26 18:55:28 +02002113 printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);
2114 return 0;
2115error:
2116 mISDN_freebchannel(&card->bch[1]);
2117 mISDN_freebchannel(&card->bch[0]);
2118 mISDN_freedchannel(&card->dch);
2119 kfree(card);
2120 return err;
2121}
2122
2123/* private data in the PCI devices list */
2124struct _hfc_map {
2125 u_int subtype;
2126 u_int flag;
2127 char *name;
2128};
2129
2130static const struct _hfc_map hfc_map[] =
2131{
2132 {HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},
2133 {HFC_CCD_B000, 0, "Billion B000"},
2134 {HFC_CCD_B006, 0, "Billion B006"},
2135 {HFC_CCD_B007, 0, "Billion B007"},
2136 {HFC_CCD_B008, 0, "Billion B008"},
2137 {HFC_CCD_B009, 0, "Billion B009"},
2138 {HFC_CCD_B00A, 0, "Billion B00A"},
2139 {HFC_CCD_B00B, 0, "Billion B00B"},
2140 {HFC_CCD_B00C, 0, "Billion B00C"},
2141 {HFC_CCD_B100, 0, "Seyeon B100"},
2142 {HFC_CCD_B700, 0, "Primux II S0 B700"},
2143 {HFC_CCD_B701, 0, "Primux II S0 NT B701"},
2144 {HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},
2145 {HFC_ASUS_0675, 0, "Asuscom/Askey 675"},
2146 {HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},
2147 {HFC_BERKOM_A1T, 0, "German telekom A1T"},
2148 {HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},
2149 {HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},
2150 {HFC_DIGI_DF_M_IOM2_E, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002151 "Digi International DataFire Micro V IOM2 (Europe)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002152 {HFC_DIGI_DF_M_E, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002153 "Digi International DataFire Micro V (Europe)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002154 {HFC_DIGI_DF_M_IOM2_A, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002155 "Digi International DataFire Micro V IOM2 (North America)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002156 {HFC_DIGI_DF_M_A, 0,
Joe Perches475be4d2012-02-19 19:52:38 -08002157 "Digi International DataFire Micro V (North America)"},
Karsten Keil1700fe12008-07-26 18:55:28 +02002158 {HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},
2159 {},
2160};
2161
2162static struct pci_device_id hfc_ids[] =
2163{
Peter Hueweb8176a32010-07-15 09:02:36 +00002164 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_2BD0),
Joe Perches475be4d2012-02-19 19:52:38 -08002165 (unsigned long) &hfc_map[0] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002166 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B000),
Joe Perches475be4d2012-02-19 19:52:38 -08002167 (unsigned long) &hfc_map[1] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002168 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B006),
Joe Perches475be4d2012-02-19 19:52:38 -08002169 (unsigned long) &hfc_map[2] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002170 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B007),
Joe Perches475be4d2012-02-19 19:52:38 -08002171 (unsigned long) &hfc_map[3] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002172 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B008),
Joe Perches475be4d2012-02-19 19:52:38 -08002173 (unsigned long) &hfc_map[4] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002174 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B009),
Joe Perches475be4d2012-02-19 19:52:38 -08002175 (unsigned long) &hfc_map[5] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002176 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00A),
Joe Perches475be4d2012-02-19 19:52:38 -08002177 (unsigned long) &hfc_map[6] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002178 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00B),
Joe Perches475be4d2012-02-19 19:52:38 -08002179 (unsigned long) &hfc_map[7] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002180 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00C),
Joe Perches475be4d2012-02-19 19:52:38 -08002181 (unsigned long) &hfc_map[8] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002182 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B100),
Joe Perches475be4d2012-02-19 19:52:38 -08002183 (unsigned long) &hfc_map[9] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002184 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B700),
Joe Perches475be4d2012-02-19 19:52:38 -08002185 (unsigned long) &hfc_map[10] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002186 { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B701),
Joe Perches475be4d2012-02-19 19:52:38 -08002187 (unsigned long) &hfc_map[11] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002188 { PCI_VDEVICE(ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1),
Joe Perches475be4d2012-02-19 19:52:38 -08002189 (unsigned long) &hfc_map[12] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002190 { PCI_VDEVICE(ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675),
Joe Perches475be4d2012-02-19 19:52:38 -08002191 (unsigned long) &hfc_map[13] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002192 { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT),
Joe Perches475be4d2012-02-19 19:52:38 -08002193 (unsigned long) &hfc_map[14] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002194 { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_A1T),
Joe Perches475be4d2012-02-19 19:52:38 -08002195 (unsigned long) &hfc_map[15] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002196 { PCI_VDEVICE(ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575),
Joe Perches475be4d2012-02-19 19:52:38 -08002197 (unsigned long) &hfc_map[16] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002198 { PCI_VDEVICE(ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0),
Joe Perches475be4d2012-02-19 19:52:38 -08002199 (unsigned long) &hfc_map[17] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002200 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E),
Joe Perches475be4d2012-02-19 19:52:38 -08002201 (unsigned long) &hfc_map[18] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002202 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_E),
Joe Perches475be4d2012-02-19 19:52:38 -08002203 (unsigned long) &hfc_map[19] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002204 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A),
Joe Perches475be4d2012-02-19 19:52:38 -08002205 (unsigned long) &hfc_map[20] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002206 { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_A),
Joe Perches475be4d2012-02-19 19:52:38 -08002207 (unsigned long) &hfc_map[21] },
Peter Hueweb8176a32010-07-15 09:02:36 +00002208 { PCI_VDEVICE(SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2),
Joe Perches475be4d2012-02-19 19:52:38 -08002209 (unsigned long) &hfc_map[22] },
Karsten Keil1700fe12008-07-26 18:55:28 +02002210 {},
2211};
2212
2213static int __devinit
2214hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2215{
2216 int err = -ENOMEM;
2217 struct hfc_pci *card;
2218 struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;
2219
2220 card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);
2221 if (!card) {
2222 printk(KERN_ERR "No kmem for HFC card\n");
2223 return err;
2224 }
2225 card->pdev = pdev;
2226 card->subtype = m->subtype;
2227 err = pci_enable_device(pdev);
2228 if (err) {
2229 kfree(card);
2230 return err;
2231 }
2232
2233 printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",
2234 m->name, pci_name(pdev));
2235
2236 card->irq = pdev->irq;
2237 pci_set_drvdata(pdev, card);
2238 err = setup_card(card);
2239 if (err)
2240 pci_set_drvdata(pdev, NULL);
2241 return err;
2242}
2243
2244static void __devexit
2245hfc_remove_pci(struct pci_dev *pdev)
2246{
2247 struct hfc_pci *card = pci_get_drvdata(pdev);
Karsten Keil1700fe12008-07-26 18:55:28 +02002248
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002249 if (card)
Karsten Keil1700fe12008-07-26 18:55:28 +02002250 release_card(card);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002251 else
Karsten Keil1700fe12008-07-26 18:55:28 +02002252 if (debug)
Karsten Keileac74af2009-05-22 11:04:56 +00002253 printk(KERN_DEBUG "%s: drvdata already removed\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002254 __func__);
Karsten Keil1700fe12008-07-26 18:55:28 +02002255}
2256
2257
2258static struct pci_driver hfc_driver = {
2259 .name = "hfcpci",
2260 .probe = hfc_probe,
2261 .remove = __devexit_p(hfc_remove_pci),
2262 .id_table = hfc_ids,
2263};
2264
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002265static int
2266_hfcpci_softirq(struct device *dev, void *arg)
2267{
2268 struct hfc_pci *hc = dev_get_drvdata(dev);
2269 struct bchannel *bch;
2270 if (hc == NULL)
2271 return 0;
2272
2273 if (hc->hw.int_m2 & HFCPCI_IRQ_ENABLE) {
2274 spin_lock(&hc->lock);
2275 bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
2276 if (bch && bch->state == ISDN_P_B_RAW) { /* B1 rx&tx */
2277 main_rec_hfcpci(bch);
2278 tx_birq(bch);
2279 }
2280 bch = Sel_BCS(hc, hc->hw.bswapped ? 1 : 2);
2281 if (bch && bch->state == ISDN_P_B_RAW) { /* B2 rx&tx */
2282 main_rec_hfcpci(bch);
2283 tx_birq(bch);
2284 }
2285 spin_unlock(&hc->lock);
2286 }
2287 return 0;
2288}
2289
2290static void
2291hfcpci_softirq(void *arg)
2292{
2293 (void) driver_for_each_device(&hfc_driver.driver, NULL, arg,
Joe Perches475be4d2012-02-19 19:52:38 -08002294 _hfcpci_softirq);
Matthias Urlichsb36b6542008-08-16 00:09:24 +02002295
2296 /* if next event would be in the past ... */
2297 if ((s32)(hfc_jiffies + tics - jiffies) <= 0)
2298 hfc_jiffies = jiffies + 1;
2299 else
2300 hfc_jiffies += tics;
2301 hfc_tl.expires = hfc_jiffies;
2302 add_timer(&hfc_tl);
2303}
2304
Karsten Keil1700fe12008-07-26 18:55:28 +02002305static int __init
2306HFC_init(void)
2307{
2308 int err;
2309
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002310 if (!poll)
2311 poll = HFCPCI_BTRANS_THRESHOLD;
2312
2313 if (poll != HFCPCI_BTRANS_THRESHOLD) {
Andreas Eversberg400fd972008-10-11 08:13:29 +02002314 tics = (poll * HZ) / 8000;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002315 if (tics < 1)
2316 tics = 1;
Andreas Eversberg400fd972008-10-11 08:13:29 +02002317 poll = (tics * 8000) / HZ;
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002318 if (poll > 256 || poll < 8) {
2319 printk(KERN_ERR "%s: Wrong poll value %d not in range "
Joe Perches475be4d2012-02-19 19:52:38 -08002320 "of 8..256.\n", __func__, poll);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002321 err = -EINVAL;
2322 return err;
2323 }
2324 }
2325 if (poll != HFCPCI_BTRANS_THRESHOLD) {
2326 printk(KERN_INFO "%s: Using alternative poll value of %d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08002327 __func__, poll);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002328 hfc_tl.function = (void *)hfcpci_softirq;
2329 hfc_tl.data = 0;
2330 init_timer(&hfc_tl);
2331 hfc_tl.expires = jiffies + tics;
2332 hfc_jiffies = hfc_tl.expires;
2333 add_timer(&hfc_tl);
2334 } else
2335 tics = 0; /* indicate the use of controller's timer */
2336
Karsten Keil1700fe12008-07-26 18:55:28 +02002337 err = pci_register_driver(&hfc_driver);
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002338 if (err) {
2339 if (timer_pending(&hfc_tl))
2340 del_timer(&hfc_tl);
2341 }
2342
Karsten Keil1700fe12008-07-26 18:55:28 +02002343 return err;
2344}
2345
2346static void __exit
2347HFC_cleanup(void)
2348{
Andreas Eversberg87c5fa12008-09-28 13:01:01 +02002349 if (timer_pending(&hfc_tl))
2350 del_timer(&hfc_tl);
2351
Karsten Keil1700fe12008-07-26 18:55:28 +02002352 pci_unregister_driver(&hfc_driver);
2353}
2354
2355module_init(HFC_init);
2356module_exit(HFC_cleanup);
Matthias Urlichse314f892008-10-16 13:58:54 +02002357
2358MODULE_DEVICE_TABLE(pci, hfc_ids);