mISDN: Add PCI ID for Junghanns 8S card
[safe/jmp/linux-2.6] / drivers / isdn / hardware / mISDN / hfcmulti.c
1 /*
2  * hfcmulti.c  low level driver for hfc-4s/hfc-8s/hfc-e1 based cards
3  *
4  * Author       Andreas Eversberg (jolly@eversberg.eu)
5  * ported to mqueue mechanism:
6  *              Peter Sprenger (sprengermoving-bytes.de)
7  *
8  * inspired by existing hfc-pci driver:
9  * Copyright 1999  by Werner Cornelius (werner@isdn-development.de)
10  * Copyright 2008  by Karsten Keil (kkeil@suse.de)
11  * Copyright 2008  by Andreas Eversberg (jolly@eversberg.eu)
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  *
28  * Thanks to Cologne Chip AG for this great controller!
29  */
30
31 /*
32  * module parameters:
33  * type:
34  *      By default (0), the card is automatically detected.
35  *      Or use the following combinations:
36  *      Bit 0-7   = 0x00001 = HFC-E1 (1 port)
37  * or   Bit 0-7   = 0x00004 = HFC-4S (4 ports)
38  * or   Bit 0-7   = 0x00008 = HFC-8S (8 ports)
39  *      Bit 8     = 0x00100 = uLaw (instead of aLaw)
40  *      Bit 9     = 0x00200 = Disable DTMF detect on all B-channels via hardware
41  *      Bit 10    = spare
42  *      Bit 11    = 0x00800 = Force PCM bus into slave mode. (otherwhise auto)
43  * or   Bit 12    = 0x01000 = Force PCM bus into master mode. (otherwhise auto)
44  *      Bit 13    = spare
45  *      Bit 14    = 0x04000 = Use external ram (128K)
46  *      Bit 15    = 0x08000 = Use external ram (512K)
47  *      Bit 16    = 0x10000 = Use 64 timeslots instead of 32
48  * or   Bit 17    = 0x20000 = Use 128 timeslots instead of anything else
49  *      Bit 18    = spare
50  *      Bit 19    = 0x80000 = Send the Watchdog a Signal (Dual E1 with Watchdog)
51  * (all other bits are reserved and shall be 0)
52  *      example: 0x20204 one HFC-4S with dtmf detection and 128 timeslots on PCM
53  *               bus (PCM master)
54  *
55  * port: (optional or required for all ports on all installed cards)
56  *      HFC-4S/HFC-8S only bits:
57  *      Bit 0     = 0x001 = Use master clock for this S/T interface
58  *                          (ony once per chip).
59  *      Bit 1     = 0x002 = transmitter line setup (non capacitive mode)
60  *                          Don't use this unless you know what you are doing!
61  *      Bit 2     = 0x004 = Disable E-channel. (No E-channel processing)
62  *      example: 0x0001,0x0000,0x0000,0x0000 one HFC-4S with master clock
63  *               received from port 1
64  *
65  *      HFC-E1 only bits:
66  *      Bit 0     = 0x0001 = interface: 0=copper, 1=optical
67  *      Bit 1     = 0x0002 = reserved (later for 32 B-channels transparent mode)
68  *      Bit 2     = 0x0004 = Report LOS
69  *      Bit 3     = 0x0008 = Report AIS
70  *      Bit 4     = 0x0010 = Report SLIP
71  *      Bit 5     = 0x0020 = Report RDI
72  *      Bit 8     = 0x0100 = Turn off CRC-4 Multiframe Mode, use double frame
73  *                           mode instead.
74  *      Bit 9     = 0x0200 = Force get clock from interface, even in NT mode.
75  * or   Bit 10    = 0x0400 = Force put clock to interface, even in TE mode.
76  *      Bit 11    = 0x0800 = Use direct RX clock for PCM sync rather than PLL.
77  *                           (E1 only)
78  *      Bit 12-13 = 0xX000 = elastic jitter buffer (1-3), Set both bits to 0
79  *                           for default.
80  * (all other bits are reserved and shall be 0)
81  *
82  * debug:
83  *      NOTE: only one debug value must be given for all cards
84  *      enable debugging (see hfc_multi.h for debug options)
85  *
86  * poll:
87  *      NOTE: only one poll value must be given for all cards
88  *      Give the number of samples for each fifo process.
89  *      By default 128 is used. Decrease to reduce delay, increase to
90  *      reduce cpu load. If unsure, don't mess with it!
91  *      Valid is 8, 16, 32, 64, 128, 256.
92  *
93  * pcm:
94  *      NOTE: only one pcm value must be given for every card.
95  *      The PCM bus id tells the mISDNdsp module about the connected PCM bus.
96  *      By default (0), the PCM bus id is 100 for the card that is PCM master.
97  *      If multiple cards are PCM master (because they are not interconnected),
98  *      each card with PCM master will have increasing PCM id.
99  *      All PCM busses with the same ID are expected to be connected and have
100  *      common time slots slots.
101  *      Only one chip of the PCM bus must be master, the others slave.
102  *      -1 means no support of PCM bus not even.
103  *      Omit this value, if all cards are interconnected or none is connected.
104  *      If unsure, don't give this parameter.
105  *
106  * dslot:
107  *      NOTE: only one dslot value must be given for every card.
108  *      Also this value must be given for non-E1 cards. If omitted, the E1
109  *      card has D-channel on time slot 16, which is default.
110  *      If 1..15 or 17..31, an alternate time slot is used for D-channel.
111  *      In this case, the application must be able to handle this.
112  *      If -1 is given, the D-channel is disabled and all 31 slots can be used
113  *      for B-channel. (only for specific applications)
114  *      If you don't know how to use it, you don't need it!
115  *
116  * iomode:
117  *      NOTE: only one mode value must be given for every card.
118  *      -> See hfc_multi.h for HFC_IO_MODE_* values
119  *      By default, the IO mode is pci memory IO (MEMIO).
120  *      Some cards requre specific IO mode, so it cannot be changed.
121  *      It may be usefull to set IO mode to register io (REGIO) to solve
122  *      PCI bridge problems.
123  *      If unsure, don't give this parameter.
124  *
125  * clockdelay_nt:
126  *      NOTE: only one clockdelay_nt value must be given once for all cards.
127  *      Give the value of the clock control register (A_ST_CLK_DLY)
128  *      of the S/T interfaces in NT mode.
129  *      This register is needed for the TBR3 certification, so don't change it.
130  *
131  * clockdelay_te:
132  *      NOTE: only one clockdelay_te value must be given once
133  *      Give the value of the clock control register (A_ST_CLK_DLY)
134  *      of the S/T interfaces in TE mode.
135  *      This register is needed for the TBR3 certification, so don't change it.
136  *
137  * clock:
138  *      NOTE: only one clock value must be given once
139  *      Selects interface with clock source for mISDN and applications.
140  *      Set to card number starting with 1. Set to -1 to disable.
141  *      By default, the first card is used as clock source.
142  *
143  * hwid:
144  *      NOTE: only one hwid value must be given once
145  *      Enable special embedded devices with XHFC controllers.
146  */
147
148 /*
149  * debug register access (never use this, it will flood your system log)
150  * #define HFC_REGISTER_DEBUG
151  */
152
153 #define HFC_MULTI_VERSION       "2.03"
154
155 #include <linux/module.h>
156 #include <linux/pci.h>
157 #include <linux/delay.h>
158 #include <linux/mISDNhw.h>
159 #include <linux/mISDNdsp.h>
160
161 /*
162 #define IRQCOUNT_DEBUG
163 #define IRQ_DEBUG
164 */
165
166 #include "hfc_multi.h"
167 #ifdef ECHOPREP
168 #include "gaintab.h"
169 #endif
170
171 #define MAX_CARDS       8
172 #define MAX_PORTS       (8 * MAX_CARDS)
173
174 static LIST_HEAD(HFClist);
175 static spinlock_t HFClock; /* global hfc list lock */
176
177 static void ph_state_change(struct dchannel *);
178
179 static struct hfc_multi *syncmaster;
180 static int plxsd_master; /* if we have a master card (yet) */
181 static spinlock_t plx_lock; /* may not acquire other lock inside */
182
183 #define TYP_E1          1
184 #define TYP_4S          4
185 #define TYP_8S          8
186
187 static int poll_timer = 6;      /* default = 128 samples = 16ms */
188 /* number of POLL_TIMER interrupts for G2 timeout (ca 1s) */
189 static int nt_t1_count[] = { 3840, 1920, 960, 480, 240, 120, 60, 30  };
190 #define CLKDEL_TE       0x0f    /* CLKDEL in TE mode */
191 #define CLKDEL_NT       0x6c    /* CLKDEL in NT mode
192                                    (0x60 MUST be included!) */
193
194 #define DIP_4S  0x1             /* DIP Switches for Beronet 1S/2S/4S cards */
195 #define DIP_8S  0x2             /* DIP Switches for Beronet 8S+ cards */
196 #define DIP_E1  0x3             /* DIP Switches for Beronet E1 cards */
197
198 /*
199  * module stuff
200  */
201
202 static uint     type[MAX_CARDS];
203 static int      pcm[MAX_CARDS];
204 static int      dslot[MAX_CARDS];
205 static uint     iomode[MAX_CARDS];
206 static uint     port[MAX_PORTS];
207 static uint     debug;
208 static uint     poll;
209 static int      clock;
210 static uint     timer;
211 static uint     clockdelay_te = CLKDEL_TE;
212 static uint     clockdelay_nt = CLKDEL_NT;
213 #define HWID_NONE       0
214 #define HWID_MINIP4     1
215 #define HWID_MINIP8     2
216 #define HWID_MINIP16    3
217 static uint     hwid = HWID_NONE;
218
219 static int      HFC_cnt, Port_cnt, PCM_cnt = 99;
220
221 MODULE_AUTHOR("Andreas Eversberg");
222 MODULE_LICENSE("GPL");
223 MODULE_VERSION(HFC_MULTI_VERSION);
224 module_param(debug, uint, S_IRUGO | S_IWUSR);
225 module_param(poll, uint, S_IRUGO | S_IWUSR);
226 module_param(clock, int, S_IRUGO | S_IWUSR);
227 module_param(timer, uint, S_IRUGO | S_IWUSR);
228 module_param(clockdelay_te, uint, S_IRUGO | S_IWUSR);
229 module_param(clockdelay_nt, uint, S_IRUGO | S_IWUSR);
230 module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
231 module_param_array(pcm, int, NULL, S_IRUGO | S_IWUSR);
232 module_param_array(dslot, int, NULL, S_IRUGO | S_IWUSR);
233 module_param_array(iomode, uint, NULL, S_IRUGO | S_IWUSR);
234 module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
235 module_param(hwid, uint, S_IRUGO | S_IWUSR); /* The hardware ID */
236
237 #ifdef HFC_REGISTER_DEBUG
238 #define HFC_outb(hc, reg, val) \
239         (hc->HFC_outb(hc, reg, val, __func__, __LINE__))
240 #define HFC_outb_nodebug(hc, reg, val) \
241         (hc->HFC_outb_nodebug(hc, reg, val, __func__, __LINE__))
242 #define HFC_inb(hc, reg) \
243         (hc->HFC_inb(hc, reg, __func__, __LINE__))
244 #define HFC_inb_nodebug(hc, reg) \
245         (hc->HFC_inb_nodebug(hc, reg, __func__, __LINE__))
246 #define HFC_inw(hc, reg) \
247         (hc->HFC_inw(hc, reg, __func__, __LINE__))
248 #define HFC_inw_nodebug(hc, reg) \
249         (hc->HFC_inw_nodebug(hc, reg, __func__, __LINE__))
250 #define HFC_wait(hc) \
251         (hc->HFC_wait(hc, __func__, __LINE__))
252 #define HFC_wait_nodebug(hc) \
253         (hc->HFC_wait_nodebug(hc, __func__, __LINE__))
254 #else
255 #define HFC_outb(hc, reg, val)          (hc->HFC_outb(hc, reg, val))
256 #define HFC_outb_nodebug(hc, reg, val)  (hc->HFC_outb_nodebug(hc, reg, val))
257 #define HFC_inb(hc, reg)                (hc->HFC_inb(hc, reg))
258 #define HFC_inb_nodebug(hc, reg)        (hc->HFC_inb_nodebug(hc, reg))
259 #define HFC_inw(hc, reg)                (hc->HFC_inw(hc, reg))
260 #define HFC_inw_nodebug(hc, reg)        (hc->HFC_inw_nodebug(hc, reg))
261 #define HFC_wait(hc)                    (hc->HFC_wait(hc))
262 #define HFC_wait_nodebug(hc)            (hc->HFC_wait_nodebug(hc))
263 #endif
264
265 #ifdef CONFIG_MISDN_HFCMULTI_8xx
266 #include "hfc_multi_8xx.h"
267 #endif
268
269 /* HFC_IO_MODE_PCIMEM */
270 static void
271 #ifdef HFC_REGISTER_DEBUG
272 HFC_outb_pcimem(struct hfc_multi *hc, u_char reg, u_char val,
273                 const char *function, int line)
274 #else
275 HFC_outb_pcimem(struct hfc_multi *hc, u_char reg, u_char val)
276 #endif
277 {
278         writeb(val, (hc->pci_membase)+reg);
279 }
280 static u_char
281 #ifdef HFC_REGISTER_DEBUG
282 HFC_inb_pcimem(struct hfc_multi *hc, u_char reg, const char *function, int line)
283 #else
284 HFC_inb_pcimem(struct hfc_multi *hc, u_char reg)
285 #endif
286 {
287         return readb((hc->pci_membase)+reg);
288 }
289 static u_short
290 #ifdef HFC_REGISTER_DEBUG
291 HFC_inw_pcimem(struct hfc_multi *hc, u_char reg, const char *function, int line)
292 #else
293 HFC_inw_pcimem(struct hfc_multi *hc, u_char reg)
294 #endif
295 {
296         return readw((hc->pci_membase)+reg);
297 }
298 static void
299 #ifdef HFC_REGISTER_DEBUG
300 HFC_wait_pcimem(struct hfc_multi *hc, const char *function, int line)
301 #else
302 HFC_wait_pcimem(struct hfc_multi *hc)
303 #endif
304 {
305         while (readb((hc->pci_membase)+R_STATUS) & V_BUSY);
306 }
307
308 /* HFC_IO_MODE_REGIO */
309 static void
310 #ifdef HFC_REGISTER_DEBUG
311 HFC_outb_regio(struct hfc_multi *hc, u_char reg, u_char val,
312         const char *function, int line)
313 #else
314 HFC_outb_regio(struct hfc_multi *hc, u_char reg, u_char val)
315 #endif
316 {
317         outb(reg, (hc->pci_iobase)+4);
318         outb(val, hc->pci_iobase);
319 }
320 static u_char
321 #ifdef HFC_REGISTER_DEBUG
322 HFC_inb_regio(struct hfc_multi *hc, u_char reg, const char *function, int line)
323 #else
324 HFC_inb_regio(struct hfc_multi *hc, u_char reg)
325 #endif
326 {
327         outb(reg, (hc->pci_iobase)+4);
328         return inb(hc->pci_iobase);
329 }
330 static u_short
331 #ifdef HFC_REGISTER_DEBUG
332 HFC_inw_regio(struct hfc_multi *hc, u_char reg, const char *function, int line)
333 #else
334 HFC_inw_regio(struct hfc_multi *hc, u_char reg)
335 #endif
336 {
337         outb(reg, (hc->pci_iobase)+4);
338         return inw(hc->pci_iobase);
339 }
340 static void
341 #ifdef HFC_REGISTER_DEBUG
342 HFC_wait_regio(struct hfc_multi *hc, const char *function, int line)
343 #else
344 HFC_wait_regio(struct hfc_multi *hc)
345 #endif
346 {
347         outb(R_STATUS, (hc->pci_iobase)+4);
348         while (inb(hc->pci_iobase) & V_BUSY);
349 }
350
351 #ifdef HFC_REGISTER_DEBUG
352 static void
353 HFC_outb_debug(struct hfc_multi *hc, u_char reg, u_char val,
354                 const char *function, int line)
355 {
356         char regname[256] = "", bits[9] = "xxxxxxxx";
357         int i;
358
359         i = -1;
360         while (hfc_register_names[++i].name) {
361                 if (hfc_register_names[i].reg == reg)
362                         strcat(regname, hfc_register_names[i].name);
363         }
364         if (regname[0] == '\0')
365                 strcpy(regname, "register");
366
367         bits[7] = '0'+(!!(val&1));
368         bits[6] = '0'+(!!(val&2));
369         bits[5] = '0'+(!!(val&4));
370         bits[4] = '0'+(!!(val&8));
371         bits[3] = '0'+(!!(val&16));
372         bits[2] = '0'+(!!(val&32));
373         bits[1] = '0'+(!!(val&64));
374         bits[0] = '0'+(!!(val&128));
375         printk(KERN_DEBUG
376             "HFC_outb(chip %d, %02x=%s, 0x%02x=%s); in %s() line %d\n",
377             hc->id, reg, regname, val, bits, function, line);
378         HFC_outb_nodebug(hc, reg, val);
379 }
380 static u_char
381 HFC_inb_debug(struct hfc_multi *hc, u_char reg, const char *function, int line)
382 {
383         char regname[256] = "", bits[9] = "xxxxxxxx";
384         u_char val = HFC_inb_nodebug(hc, reg);
385         int i;
386
387         i = 0;
388         while (hfc_register_names[i++].name)
389                 ;
390         while (hfc_register_names[++i].name) {
391                 if (hfc_register_names[i].reg == reg)
392                         strcat(regname, hfc_register_names[i].name);
393         }
394         if (regname[0] == '\0')
395                 strcpy(regname, "register");
396
397         bits[7] = '0'+(!!(val&1));
398         bits[6] = '0'+(!!(val&2));
399         bits[5] = '0'+(!!(val&4));
400         bits[4] = '0'+(!!(val&8));
401         bits[3] = '0'+(!!(val&16));
402         bits[2] = '0'+(!!(val&32));
403         bits[1] = '0'+(!!(val&64));
404         bits[0] = '0'+(!!(val&128));
405         printk(KERN_DEBUG
406             "HFC_inb(chip %d, %02x=%s) = 0x%02x=%s; in %s() line %d\n",
407             hc->id, reg, regname, val, bits, function, line);
408         return val;
409 }
410 static u_short
411 HFC_inw_debug(struct hfc_multi *hc, u_char reg, const char *function, int line)
412 {
413         char regname[256] = "";
414         u_short val = HFC_inw_nodebug(hc, reg);
415         int i;
416
417         i = 0;
418         while (hfc_register_names[i++].name)
419                 ;
420         while (hfc_register_names[++i].name) {
421                 if (hfc_register_names[i].reg == reg)
422                         strcat(regname, hfc_register_names[i].name);
423         }
424         if (regname[0] == '\0')
425                 strcpy(regname, "register");
426
427         printk(KERN_DEBUG
428             "HFC_inw(chip %d, %02x=%s) = 0x%04x; in %s() line %d\n",
429             hc->id, reg, regname, val, function, line);
430         return val;
431 }
432 static void
433 HFC_wait_debug(struct hfc_multi *hc, const char *function, int line)
434 {
435         printk(KERN_DEBUG "HFC_wait(chip %d); in %s() line %d\n",
436             hc->id, function, line);
437         HFC_wait_nodebug(hc);
438 }
439 #endif
440
441 /* write fifo data (REGIO) */
442 static void
443 write_fifo_regio(struct hfc_multi *hc, u_char *data, int len)
444 {
445         outb(A_FIFO_DATA0, (hc->pci_iobase)+4);
446         while (len>>2) {
447                 outl(cpu_to_le32(*(u32 *)data), hc->pci_iobase);
448                 data += 4;
449                 len -= 4;
450         }
451         while (len>>1) {
452                 outw(cpu_to_le16(*(u16 *)data), hc->pci_iobase);
453                 data += 2;
454                 len -= 2;
455         }
456         while (len) {
457                 outb(*data, hc->pci_iobase);
458                 data++;
459                 len--;
460         }
461 }
462 /* write fifo data (PCIMEM) */
463 static void
464 write_fifo_pcimem(struct hfc_multi *hc, u_char *data, int len)
465 {
466         while (len>>2) {
467                 writel(cpu_to_le32(*(u32 *)data),
468                         hc->pci_membase + A_FIFO_DATA0);
469                 data += 4;
470                 len -= 4;
471         }
472         while (len>>1) {
473                 writew(cpu_to_le16(*(u16 *)data),
474                         hc->pci_membase + A_FIFO_DATA0);
475                 data += 2;
476                 len -= 2;
477         }
478         while (len) {
479                 writeb(*data, hc->pci_membase + A_FIFO_DATA0);
480                 data++;
481                 len--;
482         }
483 }
484 /* read fifo data (REGIO) */
485 static void
486 read_fifo_regio(struct hfc_multi *hc, u_char *data, int len)
487 {
488         outb(A_FIFO_DATA0, (hc->pci_iobase)+4);
489         while (len>>2) {
490                 *(u32 *)data = le32_to_cpu(inl(hc->pci_iobase));
491                 data += 4;
492                 len -= 4;
493         }
494         while (len>>1) {
495                 *(u16 *)data = le16_to_cpu(inw(hc->pci_iobase));
496                 data += 2;
497                 len -= 2;
498         }
499         while (len) {
500                 *data = inb(hc->pci_iobase);
501                 data++;
502                 len--;
503         }
504 }
505
506 /* read fifo data (PCIMEM) */
507 static void
508 read_fifo_pcimem(struct hfc_multi *hc, u_char *data, int len)
509 {
510         while (len>>2) {
511                 *(u32 *)data =
512                         le32_to_cpu(readl(hc->pci_membase + A_FIFO_DATA0));
513                 data += 4;
514                 len -= 4;
515         }
516         while (len>>1) {
517                 *(u16 *)data =
518                         le16_to_cpu(readw(hc->pci_membase + A_FIFO_DATA0));
519                 data += 2;
520                 len -= 2;
521         }
522         while (len) {
523                 *data = readb(hc->pci_membase + A_FIFO_DATA0);
524                 data++;
525                 len--;
526         }
527 }
528
529
530 static void
531 enable_hwirq(struct hfc_multi *hc)
532 {
533         hc->hw.r_irq_ctrl |= V_GLOB_IRQ_EN;
534         HFC_outb(hc, R_IRQ_CTRL, hc->hw.r_irq_ctrl);
535 }
536
537 static void
538 disable_hwirq(struct hfc_multi *hc)
539 {
540         hc->hw.r_irq_ctrl &= ~((u_char)V_GLOB_IRQ_EN);
541         HFC_outb(hc, R_IRQ_CTRL, hc->hw.r_irq_ctrl);
542 }
543
544 #define NUM_EC 2
545 #define MAX_TDM_CHAN 32
546
547
548 inline void
549 enablepcibridge(struct hfc_multi *c)
550 {
551         HFC_outb(c, R_BRG_PCM_CFG, (0x0 << 6) | 0x3); /* was _io before */
552 }
553
554 inline void
555 disablepcibridge(struct hfc_multi *c)
556 {
557         HFC_outb(c, R_BRG_PCM_CFG, (0x0 << 6) | 0x2); /* was _io before */
558 }
559
560 inline unsigned char
561 readpcibridge(struct hfc_multi *hc, unsigned char address)
562 {
563         unsigned short cipv;
564         unsigned char data;
565
566         if (!hc->pci_iobase)
567                 return 0;
568
569         /* slow down a PCI read access by 1 PCI clock cycle */
570         HFC_outb(hc, R_CTRL, 0x4); /*was _io before*/
571
572         if (address == 0)
573                 cipv = 0x4000;
574         else
575                 cipv = 0x5800;
576
577         /* select local bridge port address by writing to CIP port */
578         /* data = HFC_inb(c, cipv); * was _io before */
579         outw(cipv, hc->pci_iobase + 4);
580         data = inb(hc->pci_iobase);
581
582         /* restore R_CTRL for normal PCI read cycle speed */
583         HFC_outb(hc, R_CTRL, 0x0); /* was _io before */
584
585         return data;
586 }
587
588 inline void
589 writepcibridge(struct hfc_multi *hc, unsigned char address, unsigned char data)
590 {
591         unsigned short cipv;
592         unsigned int datav;
593
594         if (!hc->pci_iobase)
595                 return;
596
597         if (address == 0)
598                 cipv = 0x4000;
599         else
600                 cipv = 0x5800;
601
602         /* select local bridge port address by writing to CIP port */
603         outw(cipv, hc->pci_iobase + 4);
604         /* define a 32 bit dword with 4 identical bytes for write sequence */
605         datav = data | ((__u32) data << 8) | ((__u32) data << 16) |
606             ((__u32) data << 24);
607
608         /*
609          * write this 32 bit dword to the bridge data port
610          * this will initiate a write sequence of up to 4 writes to the same
611          * address on the local bus interface the number of write accesses
612          * is undefined but >=1 and depends on the next PCI transaction
613          * during write sequence on the local bus
614          */
615         outl(datav, hc->pci_iobase);
616 }
617
618 inline void
619 cpld_set_reg(struct hfc_multi *hc, unsigned char reg)
620 {
621         /* Do data pin read low byte */
622         HFC_outb(hc, R_GPIO_OUT1, reg);
623 }
624
625 inline void
626 cpld_write_reg(struct hfc_multi *hc, unsigned char reg, unsigned char val)
627 {
628         cpld_set_reg(hc, reg);
629
630         enablepcibridge(hc);
631         writepcibridge(hc, 1, val);
632         disablepcibridge(hc);
633
634         return;
635 }
636
637 inline unsigned char
638 cpld_read_reg(struct hfc_multi *hc, unsigned char reg)
639 {
640         unsigned char bytein;
641
642         cpld_set_reg(hc, reg);
643
644         /* Do data pin read low byte */
645         HFC_outb(hc, R_GPIO_OUT1, reg);
646
647         enablepcibridge(hc);
648         bytein = readpcibridge(hc, 1);
649         disablepcibridge(hc);
650
651         return bytein;
652 }
653
654 inline void
655 vpm_write_address(struct hfc_multi *hc, unsigned short addr)
656 {
657         cpld_write_reg(hc, 0, 0xff & addr);
658         cpld_write_reg(hc, 1, 0x01 & (addr >> 8));
659 }
660
661 inline unsigned short
662 vpm_read_address(struct hfc_multi *c)
663 {
664         unsigned short addr;
665         unsigned short highbit;
666
667         addr = cpld_read_reg(c, 0);
668         highbit = cpld_read_reg(c, 1);
669
670         addr = addr | (highbit << 8);
671
672         return addr & 0x1ff;
673 }
674
675 inline unsigned char
676 vpm_in(struct hfc_multi *c, int which, unsigned short addr)
677 {
678         unsigned char res;
679
680         vpm_write_address(c, addr);
681
682         if (!which)
683                 cpld_set_reg(c, 2);
684         else
685                 cpld_set_reg(c, 3);
686
687         enablepcibridge(c);
688         res = readpcibridge(c, 1);
689         disablepcibridge(c);
690
691         cpld_set_reg(c, 0);
692
693         return res;
694 }
695
696 inline void
697 vpm_out(struct hfc_multi *c, int which, unsigned short addr,
698     unsigned char data)
699 {
700         vpm_write_address(c, addr);
701
702         enablepcibridge(c);
703
704         if (!which)
705                 cpld_set_reg(c, 2);
706         else
707                 cpld_set_reg(c, 3);
708
709         writepcibridge(c, 1, data);
710
711         cpld_set_reg(c, 0);
712
713         disablepcibridge(c);
714
715         {
716         unsigned char regin;
717         regin = vpm_in(c, which, addr);
718         if (regin != data)
719                 printk(KERN_DEBUG "Wrote 0x%x to register 0x%x but got back "
720                         "0x%x\n", data, addr, regin);
721         }
722
723 }
724
725
726 static void
727 vpm_init(struct hfc_multi *wc)
728 {
729         unsigned char reg;
730         unsigned int mask;
731         unsigned int i, x, y;
732         unsigned int ver;
733
734         for (x = 0; x < NUM_EC; x++) {
735                 /* Setup GPIO's */
736                 if (!x) {
737                         ver = vpm_in(wc, x, 0x1a0);
738                         printk(KERN_DEBUG "VPM: Chip %d: ver %02x\n", x, ver);
739                 }
740
741                 for (y = 0; y < 4; y++) {
742                         vpm_out(wc, x, 0x1a8 + y, 0x00); /* GPIO out */
743                         vpm_out(wc, x, 0x1ac + y, 0x00); /* GPIO dir */
744                         vpm_out(wc, x, 0x1b0 + y, 0x00); /* GPIO sel */
745                 }
746
747                 /* Setup TDM path - sets fsync and tdm_clk as inputs */
748                 reg = vpm_in(wc, x, 0x1a3); /* misc_con */
749                 vpm_out(wc, x, 0x1a3, reg & ~2);
750
751                 /* Setup Echo length (256 taps) */
752                 vpm_out(wc, x, 0x022, 1);
753                 vpm_out(wc, x, 0x023, 0xff);
754
755                 /* Setup timeslots */
756                 vpm_out(wc, x, 0x02f, 0x00);
757                 mask = 0x02020202 << (x * 4);
758
759                 /* Setup the tdm channel masks for all chips */
760                 for (i = 0; i < 4; i++)
761                         vpm_out(wc, x, 0x33 - i, (mask >> (i << 3)) & 0xff);
762
763                 /* Setup convergence rate */
764                 printk(KERN_DEBUG "VPM: A-law mode\n");
765                 reg = 0x00 | 0x10 | 0x01;
766                 vpm_out(wc, x, 0x20, reg);
767                 printk(KERN_DEBUG "VPM reg 0x20 is %x\n", reg);
768                 /*vpm_out(wc, x, 0x20, (0x00 | 0x08 | 0x20 | 0x10)); */
769
770                 vpm_out(wc, x, 0x24, 0x02);
771                 reg = vpm_in(wc, x, 0x24);
772                 printk(KERN_DEBUG "NLP Thresh is set to %d (0x%x)\n", reg, reg);
773
774                 /* Initialize echo cans */
775                 for (i = 0; i < MAX_TDM_CHAN; i++) {
776                         if (mask & (0x00000001 << i))
777                                 vpm_out(wc, x, i, 0x00);
778                 }
779
780                 /*
781                  * ARM arch at least disallows a udelay of
782                  * more than 2ms... it gives a fake "__bad_udelay"
783                  * reference at link-time.
784                  * long delays in kernel code are pretty sucky anyway
785                  * for now work around it using 5 x 2ms instead of 1 x 10ms
786                  */
787
788                 udelay(2000);
789                 udelay(2000);
790                 udelay(2000);
791                 udelay(2000);
792                 udelay(2000);
793
794                 /* Put in bypass mode */
795                 for (i = 0; i < MAX_TDM_CHAN; i++) {
796                         if (mask & (0x00000001 << i))
797                                 vpm_out(wc, x, i, 0x01);
798                 }
799
800                 /* Enable bypass */
801                 for (i = 0; i < MAX_TDM_CHAN; i++) {
802                         if (mask & (0x00000001 << i))
803                                 vpm_out(wc, x, 0x78 + i, 0x01);
804                 }
805
806         }
807 }
808
809 #ifdef UNUSED
810 static void
811 vpm_check(struct hfc_multi *hctmp)
812 {
813         unsigned char gpi2;
814
815         gpi2 = HFC_inb(hctmp, R_GPI_IN2);
816
817         if ((gpi2 & 0x3) != 0x3)
818                 printk(KERN_DEBUG "Got interrupt 0x%x from VPM!\n", gpi2);
819 }
820 #endif /* UNUSED */
821
822
823 /*
824  * Interface to enable/disable the HW Echocan
825  *
826  * these functions are called within a spin_lock_irqsave on
827  * the channel instance lock, so we are not disturbed by irqs
828  *
829  * we can later easily change the interface to make  other
830  * things configurable, for now we configure the taps
831  *
832  */
833
834 static void
835 vpm_echocan_on(struct hfc_multi *hc, int ch, int taps)
836 {
837         unsigned int timeslot;
838         unsigned int unit;
839         struct bchannel *bch = hc->chan[ch].bch;
840 #ifdef TXADJ
841         int txadj = -4;
842         struct sk_buff *skb;
843 #endif
844         if (hc->chan[ch].protocol != ISDN_P_B_RAW)
845                 return;
846
847         if (!bch)
848                 return;
849
850 #ifdef TXADJ
851         skb = _alloc_mISDN_skb(PH_CONTROL_IND, HFC_VOL_CHANGE_TX,
852                 sizeof(int), &txadj, GFP_ATOMIC);
853         if (skb)
854                 recv_Bchannel_skb(bch, skb);
855 #endif
856
857         timeslot = ((ch/4)*8) + ((ch%4)*4) + 1;
858         unit = ch % 4;
859
860         printk(KERN_NOTICE "vpm_echocan_on called taps [%d] on timeslot %d\n",
861             taps, timeslot);
862
863         vpm_out(hc, unit, timeslot, 0x7e);
864 }
865
866 static void
867 vpm_echocan_off(struct hfc_multi *hc, int ch)
868 {
869         unsigned int timeslot;
870         unsigned int unit;
871         struct bchannel *bch = hc->chan[ch].bch;
872 #ifdef TXADJ
873         int txadj = 0;
874         struct sk_buff *skb;
875 #endif
876
877         if (hc->chan[ch].protocol != ISDN_P_B_RAW)
878                 return;
879
880         if (!bch)
881                 return;
882
883 #ifdef TXADJ
884         skb = _alloc_mISDN_skb(PH_CONTROL_IND, HFC_VOL_CHANGE_TX,
885                 sizeof(int), &txadj, GFP_ATOMIC);
886         if (skb)
887                 recv_Bchannel_skb(bch, skb);
888 #endif
889
890         timeslot = ((ch/4)*8) + ((ch%4)*4) + 1;
891         unit = ch % 4;
892
893         printk(KERN_NOTICE "vpm_echocan_off called on timeslot %d\n",
894             timeslot);
895         /* FILLME */
896         vpm_out(hc, unit, timeslot, 0x01);
897 }
898
899
900 /*
901  * Speech Design resync feature
902  * NOTE: This is called sometimes outside interrupt handler.
903  * We must lock irqsave, so no other interrupt (other card) will occurr!
904  * Also multiple interrupts may nest, so must lock each access (lists, card)!
905  */
906 static inline void
907 hfcmulti_resync(struct hfc_multi *locked, struct hfc_multi *newmaster, int rm)
908 {
909         struct hfc_multi *hc, *next, *pcmmaster = NULL;
910         void __iomem *plx_acc_32;
911         u_int pv;
912         u_long flags;
913
914         spin_lock_irqsave(&HFClock, flags);
915         spin_lock(&plx_lock); /* must be locked inside other locks */
916
917         if (debug & DEBUG_HFCMULTI_PLXSD)
918                 printk(KERN_DEBUG "%s: RESYNC(syncmaster=0x%p)\n",
919                         __func__, syncmaster);
920
921         /* select new master */
922         if (newmaster) {
923                 if (debug & DEBUG_HFCMULTI_PLXSD)
924                         printk(KERN_DEBUG "using provided controller\n");
925         } else {
926                 list_for_each_entry_safe(hc, next, &HFClist, list) {
927                         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
928                                 if (hc->syncronized) {
929                                         newmaster = hc;
930                                         break;
931                                 }
932                         }
933                 }
934         }
935
936         /* Disable sync of all cards */
937         list_for_each_entry_safe(hc, next, &HFClist, list) {
938                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
939                         plx_acc_32 = hc->plx_membase + PLX_GPIOC;
940                         pv = readl(plx_acc_32);
941                         pv &= ~PLX_SYNC_O_EN;
942                         writel(pv, plx_acc_32);
943                         if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip)) {
944                                 pcmmaster = hc;
945                                 if (hc->ctype == HFC_TYPE_E1) {
946                                         if (debug & DEBUG_HFCMULTI_PLXSD)
947                                                 printk(KERN_DEBUG
948                                                         "Schedule SYNC_I\n");
949                                         hc->e1_resync |= 1; /* get SYNC_I */
950                                 }
951                         }
952                 }
953         }
954
955         if (newmaster) {
956                 hc = newmaster;
957                 if (debug & DEBUG_HFCMULTI_PLXSD)
958                         printk(KERN_DEBUG "id=%d (0x%p) = syncronized with "
959                                 "interface.\n", hc->id, hc);
960                 /* Enable new sync master */
961                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
962                 pv = readl(plx_acc_32);
963                 pv |= PLX_SYNC_O_EN;
964                 writel(pv, plx_acc_32);
965                 /* switch to jatt PLL, if not disabled by RX_SYNC */
966                 if (hc->ctype == HFC_TYPE_E1
967                                 && !test_bit(HFC_CHIP_RX_SYNC, &hc->chip)) {
968                         if (debug & DEBUG_HFCMULTI_PLXSD)
969                                 printk(KERN_DEBUG "Schedule jatt PLL\n");
970                         hc->e1_resync |= 2; /* switch to jatt */
971                 }
972         } else {
973                 if (pcmmaster) {
974                         hc = pcmmaster;
975                         if (debug & DEBUG_HFCMULTI_PLXSD)
976                                 printk(KERN_DEBUG
977                                         "id=%d (0x%p) = PCM master syncronized "
978                                         "with QUARTZ\n", hc->id, hc);
979                         if (hc->ctype == HFC_TYPE_E1) {
980                                 /* Use the crystal clock for the PCM
981                                    master card */
982                                 if (debug & DEBUG_HFCMULTI_PLXSD)
983                                         printk(KERN_DEBUG
984                                             "Schedule QUARTZ for HFC-E1\n");
985                                 hc->e1_resync |= 4; /* switch quartz */
986                         } else {
987                                 if (debug & DEBUG_HFCMULTI_PLXSD)
988                                         printk(KERN_DEBUG
989                                             "QUARTZ is automatically "
990                                             "enabled by HFC-%dS\n", hc->ctype);
991                         }
992                         plx_acc_32 = hc->plx_membase + PLX_GPIOC;
993                         pv = readl(plx_acc_32);
994                         pv |= PLX_SYNC_O_EN;
995                         writel(pv, plx_acc_32);
996                 } else
997                         if (!rm)
998                                 printk(KERN_ERR "%s no pcm master, this MUST "
999                                         "not happen!\n", __func__);
1000         }
1001         syncmaster = newmaster;
1002
1003         spin_unlock(&plx_lock);
1004         spin_unlock_irqrestore(&HFClock, flags);
1005 }
1006
1007 /* This must be called AND hc must be locked irqsave!!! */
1008 inline void
1009 plxsd_checksync(struct hfc_multi *hc, int rm)
1010 {
1011         if (hc->syncronized) {
1012                 if (syncmaster == NULL) {
1013                         if (debug & DEBUG_HFCMULTI_PLXSD)
1014                                 printk(KERN_WARNING "%s: GOT sync on card %d"
1015                                         " (id=%d)\n", __func__, hc->id + 1,
1016                                         hc->id);
1017                         hfcmulti_resync(hc, hc, rm);
1018                 }
1019         } else {
1020                 if (syncmaster == hc) {
1021                         if (debug & DEBUG_HFCMULTI_PLXSD)
1022                                 printk(KERN_WARNING "%s: LOST sync on card %d"
1023                                         " (id=%d)\n", __func__, hc->id + 1,
1024                                         hc->id);
1025                         hfcmulti_resync(hc, NULL, rm);
1026                 }
1027         }
1028 }
1029
1030
1031 /*
1032  * free hardware resources used by driver
1033  */
1034 static void
1035 release_io_hfcmulti(struct hfc_multi *hc)
1036 {
1037         void __iomem *plx_acc_32;
1038         u_int   pv;
1039         u_long  plx_flags;
1040
1041         if (debug & DEBUG_HFCMULTI_INIT)
1042                 printk(KERN_DEBUG "%s: entered\n", __func__);
1043
1044         /* soft reset also masks all interrupts */
1045         hc->hw.r_cirm |= V_SRES;
1046         HFC_outb(hc, R_CIRM, hc->hw.r_cirm);
1047         udelay(1000);
1048         hc->hw.r_cirm &= ~V_SRES;
1049         HFC_outb(hc, R_CIRM, hc->hw.r_cirm);
1050         udelay(1000); /* instead of 'wait' that may cause locking */
1051
1052         /* release Speech Design card, if PLX was initialized */
1053         if (test_bit(HFC_CHIP_PLXSD, &hc->chip) && hc->plx_membase) {
1054                 if (debug & DEBUG_HFCMULTI_PLXSD)
1055                         printk(KERN_DEBUG "%s: release PLXSD card %d\n",
1056                             __func__, hc->id + 1);
1057                 spin_lock_irqsave(&plx_lock, plx_flags);
1058                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
1059                 writel(PLX_GPIOC_INIT, plx_acc_32);
1060                 pv = readl(plx_acc_32);
1061                 /* Termination off */
1062                 pv &= ~PLX_TERM_ON;
1063                 /* Disconnect the PCM */
1064                 pv |= PLX_SLAVE_EN_N;
1065                 pv &= ~PLX_MASTER_EN;
1066                 pv &= ~PLX_SYNC_O_EN;
1067                 /* Put the DSP in Reset */
1068                 pv &= ~PLX_DSP_RES_N;
1069                 writel(pv, plx_acc_32);
1070                 if (debug & DEBUG_HFCMULTI_INIT)
1071                         printk(KERN_WARNING "%s: PCM off: PLX_GPIO=%x\n",
1072                                 __func__, pv);
1073                 spin_unlock_irqrestore(&plx_lock, plx_flags);
1074         }
1075
1076         /* disable memory mapped ports / io ports */
1077         test_and_clear_bit(HFC_CHIP_PLXSD, &hc->chip); /* prevent resync */
1078         if (hc->pci_dev)
1079                 pci_write_config_word(hc->pci_dev, PCI_COMMAND, 0);
1080         if (hc->pci_membase)
1081                 iounmap(hc->pci_membase);
1082         if (hc->plx_membase)
1083                 iounmap(hc->plx_membase);
1084         if (hc->pci_iobase)
1085                 release_region(hc->pci_iobase, 8);
1086         if (hc->xhfc_membase)
1087                 iounmap((void *)hc->xhfc_membase);
1088
1089         if (hc->pci_dev) {
1090                 pci_disable_device(hc->pci_dev);
1091                 pci_set_drvdata(hc->pci_dev, NULL);
1092         }
1093         if (debug & DEBUG_HFCMULTI_INIT)
1094                 printk(KERN_DEBUG "%s: done\n", __func__);
1095 }
1096
1097 /*
1098  * function called to reset the HFC chip. A complete software reset of chip
1099  * and fifos is done. All configuration of the chip is done.
1100  */
1101
1102 static int
1103 init_chip(struct hfc_multi *hc)
1104 {
1105         u_long                  flags, val, val2 = 0, rev;
1106         int                     i, err = 0;
1107         u_char                  r_conf_en, rval;
1108         void __iomem            *plx_acc_32;
1109         u_int                   pv;
1110         u_long                  plx_flags, hfc_flags;
1111         int                     plx_count;
1112         struct hfc_multi        *pos, *next, *plx_last_hc;
1113
1114         spin_lock_irqsave(&hc->lock, flags);
1115         /* reset all registers */
1116         memset(&hc->hw, 0, sizeof(struct hfcm_hw));
1117
1118         /* revision check */
1119         if (debug & DEBUG_HFCMULTI_INIT)
1120                 printk(KERN_DEBUG "%s: entered\n", __func__);
1121         val = HFC_inb(hc, R_CHIP_ID);
1122         if ((val>>4) != 0x8 && (val>>4) != 0xc && (val>>4) != 0xe
1123                         && (val>>1) != 0x31) {
1124                 printk(KERN_INFO "HFC_multi: unknown CHIP_ID:%x\n", (u_int)val);
1125                 err = -EIO;
1126                 goto out;
1127         }
1128         rev = HFC_inb(hc, R_CHIP_RV);
1129         printk(KERN_INFO
1130             "HFC_multi: detected HFC with chip ID=0x%lx revision=%ld%s\n",
1131             val, rev, (rev == 0 && (hc->ctype != HFC_TYPE_XHFC)) ?
1132                 " (old FIFO handling)" : "");
1133         if (hc->ctype != HFC_TYPE_XHFC && rev == 0) {
1134                 test_and_set_bit(HFC_CHIP_REVISION0, &hc->chip);
1135                 printk(KERN_WARNING
1136                     "HFC_multi: NOTE: Your chip is revision 0, "
1137                     "ask Cologne Chip for update. Newer chips "
1138                     "have a better FIFO handling. Old chips "
1139                     "still work but may have slightly lower "
1140                     "HDLC transmit performance.\n");
1141         }
1142         if (rev > 1) {
1143                 printk(KERN_WARNING "HFC_multi: WARNING: This driver doesn't "
1144                     "consider chip revision = %ld. The chip / "
1145                     "bridge may not work.\n", rev);
1146         }
1147
1148         /* set s-ram size */
1149         hc->Flen = 0x10;
1150         hc->Zmin = 0x80;
1151         hc->Zlen = 384;
1152         hc->DTMFbase = 0x1000;
1153         if (test_bit(HFC_CHIP_EXRAM_128, &hc->chip)) {
1154                 if (debug & DEBUG_HFCMULTI_INIT)
1155                         printk(KERN_DEBUG "%s: changing to 128K extenal RAM\n",
1156                             __func__);
1157                 hc->hw.r_ctrl |= V_EXT_RAM;
1158                 hc->hw.r_ram_sz = 1;
1159                 hc->Flen = 0x20;
1160                 hc->Zmin = 0xc0;
1161                 hc->Zlen = 1856;
1162                 hc->DTMFbase = 0x2000;
1163         }
1164         if (test_bit(HFC_CHIP_EXRAM_512, &hc->chip)) {
1165                 if (debug & DEBUG_HFCMULTI_INIT)
1166                         printk(KERN_DEBUG "%s: changing to 512K extenal RAM\n",
1167                             __func__);
1168                 hc->hw.r_ctrl |= V_EXT_RAM;
1169                 hc->hw.r_ram_sz = 2;
1170                 hc->Flen = 0x20;
1171                 hc->Zmin = 0xc0;
1172                 hc->Zlen = 8000;
1173                 hc->DTMFbase = 0x2000;
1174         }
1175         if (hc->ctype == HFC_TYPE_XHFC) {
1176                 hc->Flen = 0x8;
1177                 hc->Zmin = 0x0;
1178                 hc->Zlen = 64;
1179                 hc->DTMFbase = 0x0;
1180         }
1181         hc->max_trans = poll << 1;
1182         if (hc->max_trans > hc->Zlen)
1183                 hc->max_trans = hc->Zlen;
1184
1185         /* Speech Design PLX bridge */
1186         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
1187                 if (debug & DEBUG_HFCMULTI_PLXSD)
1188                         printk(KERN_DEBUG "%s: initializing PLXSD card %d\n",
1189                             __func__, hc->id + 1);
1190                 spin_lock_irqsave(&plx_lock, plx_flags);
1191                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
1192                 writel(PLX_GPIOC_INIT, plx_acc_32);
1193                 pv = readl(plx_acc_32);
1194                 /* The first and the last cards are terminating the PCM bus */
1195                 pv |= PLX_TERM_ON; /* hc is currently the last */
1196                 /* Disconnect the PCM */
1197                 pv |= PLX_SLAVE_EN_N;
1198                 pv &= ~PLX_MASTER_EN;
1199                 pv &= ~PLX_SYNC_O_EN;
1200                 /* Put the DSP in Reset */
1201                 pv &= ~PLX_DSP_RES_N;
1202                 writel(pv, plx_acc_32);
1203                 spin_unlock_irqrestore(&plx_lock, plx_flags);
1204                 if (debug & DEBUG_HFCMULTI_INIT)
1205                         printk(KERN_WARNING "%s: slave/term: PLX_GPIO=%x\n",
1206                                 __func__, pv);
1207                 /*
1208                  * If we are the 3rd PLXSD card or higher, we must turn
1209                  * termination of last PLXSD card off.
1210                  */
1211                 spin_lock_irqsave(&HFClock, hfc_flags);
1212                 plx_count = 0;
1213                 plx_last_hc = NULL;
1214                 list_for_each_entry_safe(pos, next, &HFClist, list) {
1215                         if (test_bit(HFC_CHIP_PLXSD, &pos->chip)) {
1216                                 plx_count++;
1217                                 if (pos != hc)
1218                                         plx_last_hc = pos;
1219                         }
1220                 }
1221                 if (plx_count >= 3) {
1222                         if (debug & DEBUG_HFCMULTI_PLXSD)
1223                                 printk(KERN_DEBUG "%s: card %d is between, so "
1224                                         "we disable termination\n",
1225                                     __func__, plx_last_hc->id + 1);
1226                         spin_lock_irqsave(&plx_lock, plx_flags);
1227                         plx_acc_32 = plx_last_hc->plx_membase + PLX_GPIOC;
1228                         pv = readl(plx_acc_32);
1229                         pv &= ~PLX_TERM_ON;
1230                         writel(pv, plx_acc_32);
1231                         spin_unlock_irqrestore(&plx_lock, plx_flags);
1232                         if (debug & DEBUG_HFCMULTI_INIT)
1233                             printk(KERN_WARNING "%s: term off: PLX_GPIO=%x\n",
1234                                         __func__, pv);
1235                 }
1236                 spin_unlock_irqrestore(&HFClock, hfc_flags);
1237                 hc->hw.r_pcm_md0 = V_F0_LEN; /* shift clock for DSP */
1238         }
1239
1240         if (test_bit(HFC_CHIP_EMBSD, &hc->chip))
1241                 hc->hw.r_pcm_md0 = V_F0_LEN; /* shift clock for DSP */
1242
1243         /* we only want the real Z2 read-pointer for revision > 0 */
1244         if (!test_bit(HFC_CHIP_REVISION0, &hc->chip))
1245                 hc->hw.r_ram_sz |= V_FZ_MD;
1246
1247         /* select pcm mode */
1248         if (test_bit(HFC_CHIP_PCM_SLAVE, &hc->chip)) {
1249                 if (debug & DEBUG_HFCMULTI_INIT)
1250                         printk(KERN_DEBUG "%s: setting PCM into slave mode\n",
1251                             __func__);
1252         } else
1253         if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip) && !plxsd_master) {
1254                 if (debug & DEBUG_HFCMULTI_INIT)
1255                         printk(KERN_DEBUG "%s: setting PCM into master mode\n",
1256                             __func__);
1257                 hc->hw.r_pcm_md0 |= V_PCM_MD;
1258         } else {
1259                 if (debug & DEBUG_HFCMULTI_INIT)
1260                         printk(KERN_DEBUG "%s: performing PCM auto detect\n",
1261                             __func__);
1262         }
1263
1264         /* soft reset */
1265         HFC_outb(hc, R_CTRL, hc->hw.r_ctrl);
1266         if (hc->ctype == HFC_TYPE_XHFC)
1267                 HFC_outb(hc, 0x0C /* R_FIFO_THRES */,
1268                                 0x11 /* 16 Bytes TX/RX */);
1269         else
1270                 HFC_outb(hc, R_RAM_SZ, hc->hw.r_ram_sz);
1271         HFC_outb(hc, R_FIFO_MD, 0);
1272         if (hc->ctype == HFC_TYPE_XHFC)
1273                 hc->hw.r_cirm = V_SRES | V_HFCRES | V_PCMRES | V_STRES;
1274         else
1275                 hc->hw.r_cirm = V_SRES | V_HFCRES | V_PCMRES | V_STRES
1276                         | V_RLD_EPR;
1277         HFC_outb(hc, R_CIRM, hc->hw.r_cirm);
1278         udelay(100);
1279         hc->hw.r_cirm = 0;
1280         HFC_outb(hc, R_CIRM, hc->hw.r_cirm);
1281         udelay(100);
1282         if (hc->ctype != HFC_TYPE_XHFC)
1283                 HFC_outb(hc, R_RAM_SZ, hc->hw.r_ram_sz);
1284
1285         /* Speech Design PLX bridge pcm and sync mode */
1286         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
1287                 spin_lock_irqsave(&plx_lock, plx_flags);
1288                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
1289                 pv = readl(plx_acc_32);
1290                 /* Connect PCM */
1291                 if (hc->hw.r_pcm_md0 & V_PCM_MD) {
1292                         pv |= PLX_MASTER_EN | PLX_SLAVE_EN_N;
1293                         pv |= PLX_SYNC_O_EN;
1294                         if (debug & DEBUG_HFCMULTI_INIT)
1295                                 printk(KERN_WARNING "%s: master: PLX_GPIO=%x\n",
1296                                         __func__, pv);
1297                 } else {
1298                         pv &= ~(PLX_MASTER_EN | PLX_SLAVE_EN_N);
1299                         pv &= ~PLX_SYNC_O_EN;
1300                         if (debug & DEBUG_HFCMULTI_INIT)
1301                                 printk(KERN_WARNING "%s: slave: PLX_GPIO=%x\n",
1302                                         __func__, pv);
1303                 }
1304                 writel(pv, plx_acc_32);
1305                 spin_unlock_irqrestore(&plx_lock, plx_flags);
1306         }
1307
1308         /* PCM setup */
1309         HFC_outb(hc, R_PCM_MD0, hc->hw.r_pcm_md0 | 0x90);
1310         if (hc->slots == 32)
1311                 HFC_outb(hc, R_PCM_MD1, 0x00);
1312         if (hc->slots == 64)
1313                 HFC_outb(hc, R_PCM_MD1, 0x10);
1314         if (hc->slots == 128)
1315                 HFC_outb(hc, R_PCM_MD1, 0x20);
1316         HFC_outb(hc, R_PCM_MD0, hc->hw.r_pcm_md0 | 0xa0);
1317         if (test_bit(HFC_CHIP_PLXSD, &hc->chip))
1318                 HFC_outb(hc, R_PCM_MD2, V_SYNC_SRC); /* sync via SYNC_I / O */
1319         else if (test_bit(HFC_CHIP_EMBSD, &hc->chip))
1320                 HFC_outb(hc, R_PCM_MD2, 0x10); /* V_C2O_EN */
1321         else
1322                 HFC_outb(hc, R_PCM_MD2, 0x00); /* sync from interface */
1323         HFC_outb(hc, R_PCM_MD0, hc->hw.r_pcm_md0 | 0x00);
1324         for (i = 0; i < 256; i++) {
1325                 HFC_outb_nodebug(hc, R_SLOT, i);
1326                 HFC_outb_nodebug(hc, A_SL_CFG, 0);
1327                 if (hc->ctype != HFC_TYPE_XHFC)
1328                         HFC_outb_nodebug(hc, A_CONF, 0);
1329                 hc->slot_owner[i] = -1;
1330         }
1331
1332         /* set clock speed */
1333         if (test_bit(HFC_CHIP_CLOCK2, &hc->chip)) {
1334                 if (debug & DEBUG_HFCMULTI_INIT)
1335                         printk(KERN_DEBUG
1336                             "%s: setting double clock\n", __func__);
1337                 HFC_outb(hc, R_BRG_PCM_CFG, V_PCM_CLK);
1338         }
1339
1340         if (test_bit(HFC_CHIP_EMBSD, &hc->chip))
1341                 HFC_outb(hc, 0x02 /* R_CLK_CFG */, 0x40 /* V_CLKO_OFF */);
1342
1343         /* B410P GPIO */
1344         if (test_bit(HFC_CHIP_B410P, &hc->chip)) {
1345                 printk(KERN_NOTICE "Setting GPIOs\n");
1346                 HFC_outb(hc, R_GPIO_SEL, 0x30);
1347                 HFC_outb(hc, R_GPIO_EN1, 0x3);
1348                 udelay(1000);
1349                 printk(KERN_NOTICE "calling vpm_init\n");
1350                 vpm_init(hc);
1351         }
1352
1353         /* check if R_F0_CNT counts (8 kHz frame count) */
1354         val = HFC_inb(hc, R_F0_CNTL);
1355         val += HFC_inb(hc, R_F0_CNTH) << 8;
1356         if (debug & DEBUG_HFCMULTI_INIT)
1357                 printk(KERN_DEBUG
1358                     "HFC_multi F0_CNT %ld after reset\n", val);
1359         spin_unlock_irqrestore(&hc->lock, flags);
1360         set_current_state(TASK_UNINTERRUPTIBLE);
1361         schedule_timeout((HZ/100)?:1); /* Timeout minimum 10ms */
1362         spin_lock_irqsave(&hc->lock, flags);
1363         val2 = HFC_inb(hc, R_F0_CNTL);
1364         val2 += HFC_inb(hc, R_F0_CNTH) << 8;
1365         if (debug & DEBUG_HFCMULTI_INIT)
1366                 printk(KERN_DEBUG
1367                         "HFC_multi F0_CNT %ld after 10 ms (1st try)\n",
1368                     val2);
1369         if (val2 >= val+8) { /* 1 ms */
1370                 /* it counts, so we keep the pcm mode */
1371                 if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip))
1372                         printk(KERN_INFO "controller is PCM bus MASTER\n");
1373                 else
1374                 if (test_bit(HFC_CHIP_PCM_SLAVE, &hc->chip))
1375                         printk(KERN_INFO "controller is PCM bus SLAVE\n");
1376                 else {
1377                         test_and_set_bit(HFC_CHIP_PCM_SLAVE, &hc->chip);
1378                         printk(KERN_INFO "controller is PCM bus SLAVE "
1379                                 "(auto detected)\n");
1380                 }
1381         } else {
1382                 /* does not count */
1383                 if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip)) {
1384 controller_fail:
1385                         printk(KERN_ERR "HFC_multi ERROR, getting no 125us "
1386                             "pulse. Seems that controller fails.\n");
1387                         err = -EIO;
1388                         goto out;
1389                 }
1390                 if (test_bit(HFC_CHIP_PCM_SLAVE, &hc->chip)) {
1391                         printk(KERN_INFO "controller is PCM bus SLAVE "
1392                                 "(ignoring missing PCM clock)\n");
1393                 } else {
1394                         /* only one pcm master */
1395                         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)
1396                                 && plxsd_master) {
1397                                 printk(KERN_ERR "HFC_multi ERROR, no clock "
1398                                     "on another Speech Design card found. "
1399                                     "Please be sure to connect PCM cable.\n");
1400                                 err = -EIO;
1401                                 goto out;
1402                         }
1403                         /* retry with master clock */
1404                         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
1405                                 spin_lock_irqsave(&plx_lock, plx_flags);
1406                                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
1407                                 pv = readl(plx_acc_32);
1408                                 pv |= PLX_MASTER_EN | PLX_SLAVE_EN_N;
1409                                 pv |= PLX_SYNC_O_EN;
1410                                 writel(pv, plx_acc_32);
1411                                 spin_unlock_irqrestore(&plx_lock, plx_flags);
1412                                 if (debug & DEBUG_HFCMULTI_INIT)
1413                                     printk(KERN_WARNING "%s: master: PLX_GPIO"
1414                                         "=%x\n", __func__, pv);
1415                         }
1416                         hc->hw.r_pcm_md0 |= V_PCM_MD;
1417                         HFC_outb(hc, R_PCM_MD0, hc->hw.r_pcm_md0 | 0x00);
1418                         spin_unlock_irqrestore(&hc->lock, flags);
1419                         set_current_state(TASK_UNINTERRUPTIBLE);
1420                         schedule_timeout((HZ/100)?:1); /* Timeout min. 10ms */
1421                         spin_lock_irqsave(&hc->lock, flags);
1422                         val2 = HFC_inb(hc, R_F0_CNTL);
1423                         val2 += HFC_inb(hc, R_F0_CNTH) << 8;
1424                         if (debug & DEBUG_HFCMULTI_INIT)
1425                                 printk(KERN_DEBUG "HFC_multi F0_CNT %ld after "
1426                                         "10 ms (2nd try)\n", val2);
1427                         if (val2 >= val+8) { /* 1 ms */
1428                                 test_and_set_bit(HFC_CHIP_PCM_MASTER,
1429                                         &hc->chip);
1430                                 printk(KERN_INFO "controller is PCM bus MASTER "
1431                                         "(auto detected)\n");
1432                         } else
1433                                 goto controller_fail;
1434                 }
1435         }
1436
1437         /* Release the DSP Reset */
1438         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
1439                 if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip))
1440                         plxsd_master = 1;
1441                 spin_lock_irqsave(&plx_lock, plx_flags);
1442                 plx_acc_32 = hc->plx_membase + PLX_GPIOC;
1443                 pv = readl(plx_acc_32);
1444                 pv |=  PLX_DSP_RES_N;
1445                 writel(pv, plx_acc_32);
1446                 spin_unlock_irqrestore(&plx_lock, plx_flags);
1447                 if (debug & DEBUG_HFCMULTI_INIT)
1448                         printk(KERN_WARNING "%s: reset off: PLX_GPIO=%x\n",
1449                                 __func__, pv);
1450         }
1451
1452         /* pcm id */
1453         if (hc->pcm)
1454                 printk(KERN_INFO "controller has given PCM BUS ID %d\n",
1455                         hc->pcm);
1456         else {
1457                 if (test_bit(HFC_CHIP_PCM_MASTER, &hc->chip)
1458                  || test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
1459                         PCM_cnt++; /* SD has proprietary bridging */
1460                 }
1461                 hc->pcm = PCM_cnt;
1462                 printk(KERN_INFO "controller has PCM BUS ID %d "
1463                         "(auto selected)\n", hc->pcm);
1464         }
1465
1466         /* set up timer */
1467         HFC_outb(hc, R_TI_WD, poll_timer);
1468         hc->hw.r_irqmsk_misc |= V_TI_IRQMSK;
1469
1470         /* set E1 state machine IRQ */
1471         if (hc->ctype == HFC_TYPE_E1)
1472                 hc->hw.r_irqmsk_misc |= V_STA_IRQMSK;
1473
1474         /* set DTMF detection */
1475         if (test_bit(HFC_CHIP_DTMF, &hc->chip)) {
1476                 if (debug & DEBUG_HFCMULTI_INIT)
1477                         printk(KERN_DEBUG "%s: enabling DTMF detection "
1478                             "for all B-channel\n", __func__);
1479                 hc->hw.r_dtmf = V_DTMF_EN | V_DTMF_STOP;
1480                 if (test_bit(HFC_CHIP_ULAW, &hc->chip))
1481                         hc->hw.r_dtmf |= V_ULAW_SEL;
1482                 HFC_outb(hc, R_DTMF_N, 102 - 1);
1483                 hc->hw.r_irqmsk_misc |= V_DTMF_IRQMSK;
1484         }
1485
1486         /* conference engine */
1487         if (test_bit(HFC_CHIP_ULAW, &hc->chip))
1488                 r_conf_en = V_CONF_EN | V_ULAW;
1489         else
1490                 r_conf_en = V_CONF_EN;
1491         if (hc->ctype != HFC_TYPE_XHFC)
1492                 HFC_outb(hc, R_CONF_EN, r_conf_en);
1493
1494         /* setting leds */
1495         switch (hc->leds) {
1496         case 1: /* HFC-E1 OEM */
1497                 if (test_bit(HFC_CHIP_WATCHDOG, &hc->chip))
1498                         HFC_outb(hc, R_GPIO_SEL, 0x32);
1499                 else
1500                         HFC_outb(hc, R_GPIO_SEL, 0x30);
1501
1502                 HFC_outb(hc, R_GPIO_EN1, 0x0f);
1503                 HFC_outb(hc, R_GPIO_OUT1, 0x00);
1504
1505                 HFC_outb(hc, R_GPIO_EN0, V_GPIO_EN2 | V_GPIO_EN3);
1506                 break;
1507
1508         case 2: /* HFC-4S OEM */
1509         case 3:
1510                 HFC_outb(hc, R_GPIO_SEL, 0xf0);
1511                 HFC_outb(hc, R_GPIO_EN1, 0xff);
1512                 HFC_outb(hc, R_GPIO_OUT1, 0x00);
1513                 break;
1514         }
1515
1516         if (test_bit(HFC_CHIP_EMBSD, &hc->chip)) {
1517                 hc->hw.r_st_sync = 0x10; /* V_AUTO_SYNCI */
1518                 HFC_outb(hc, R_ST_SYNC, hc->hw.r_st_sync);
1519         }
1520
1521         /* set master clock */
1522         if (hc->masterclk >= 0) {
1523                 if (debug & DEBUG_HFCMULTI_INIT)
1524                         printk(KERN_DEBUG "%s: setting ST master clock "
1525                             "to port %d (0..%d)\n",
1526                             __func__, hc->masterclk, hc->ports-1);
1527                 hc->hw.r_st_sync |= (hc->masterclk | V_AUTO_SYNC);
1528                 HFC_outb(hc, R_ST_SYNC, hc->hw.r_st_sync);
1529         }
1530
1531
1532
1533         /* setting misc irq */
1534         HFC_outb(hc, R_IRQMSK_MISC, hc->hw.r_irqmsk_misc);
1535         if (debug & DEBUG_HFCMULTI_INIT)
1536                 printk(KERN_DEBUG "r_irqmsk_misc.2: 0x%x\n",
1537                     hc->hw.r_irqmsk_misc);
1538
1539         /* RAM access test */
1540         HFC_outb(hc, R_RAM_ADDR0, 0);
1541         HFC_outb(hc, R_RAM_ADDR1, 0);
1542         HFC_outb(hc, R_RAM_ADDR2, 0);
1543         for (i = 0; i < 256; i++) {
1544                 HFC_outb_nodebug(hc, R_RAM_ADDR0, i);
1545                 HFC_outb_nodebug(hc, R_RAM_DATA, ((i*3)&0xff));
1546         }
1547         for (i = 0; i < 256; i++) {
1548                 HFC_outb_nodebug(hc, R_RAM_ADDR0, i);
1549                 HFC_inb_nodebug(hc, R_RAM_DATA);
1550                 rval = HFC_inb_nodebug(hc, R_INT_DATA);
1551                 if (rval != ((i * 3) & 0xff)) {
1552                         printk(KERN_DEBUG
1553                             "addr:%x val:%x should:%x\n", i, rval,
1554                             (i * 3) & 0xff);
1555                         err++;
1556                 }
1557         }
1558         if (err) {
1559                 printk(KERN_DEBUG "aborting - %d RAM access errors\n", err);
1560                 err = -EIO;
1561                 goto out;
1562         }
1563
1564         if (debug & DEBUG_HFCMULTI_INIT)
1565                 printk(KERN_DEBUG "%s: done\n", __func__);
1566 out:
1567         spin_unlock_irqrestore(&hc->lock, flags);
1568         return err;
1569 }
1570
1571
1572 /*
1573  * control the watchdog
1574  */
1575 static void
1576 hfcmulti_watchdog(struct hfc_multi *hc)
1577 {
1578         hc->wdcount++;
1579
1580         if (hc->wdcount > 10) {
1581                 hc->wdcount = 0;
1582                 hc->wdbyte = hc->wdbyte == V_GPIO_OUT2 ?
1583                     V_GPIO_OUT3 : V_GPIO_OUT2;
1584
1585         /* printk("Sending Watchdog Kill %x\n",hc->wdbyte); */
1586                 HFC_outb(hc, R_GPIO_EN0, V_GPIO_EN2 | V_GPIO_EN3);
1587                 HFC_outb(hc, R_GPIO_OUT0, hc->wdbyte);
1588         }
1589 }
1590
1591
1592
1593 /*
1594  * output leds
1595  */
1596 static void
1597 hfcmulti_leds(struct hfc_multi *hc)
1598 {
1599         unsigned long lled;
1600         unsigned long leddw;
1601         int i, state, active, leds;
1602         struct dchannel *dch;
1603         int led[4];
1604
1605         hc->ledcount += poll;
1606         if (hc->ledcount > 4096) {
1607                 hc->ledcount -= 4096;
1608                 hc->ledstate = 0xAFFEAFFE;
1609         }
1610
1611         switch (hc->leds) {
1612         case 1: /* HFC-E1 OEM */
1613                 /* 2 red blinking: NT mode deactivate
1614                  * 2 red steady:   TE mode deactivate
1615                  * left green:     L1 active
1616                  * left red:       frame sync, but no L1
1617                  * right green:    L2 active
1618                  */
1619                 if (hc->chan[hc->dslot].sync != 2) { /* no frame sync */
1620                         if (hc->chan[hc->dslot].dch->dev.D.protocol
1621                                 != ISDN_P_NT_E1) {
1622                                 led[0] = 1;
1623                                 led[1] = 1;
1624                         } else if (hc->ledcount>>11) {
1625                                 led[0] = 1;
1626                                 led[1] = 1;
1627                         } else {
1628                                 led[0] = 0;
1629                                 led[1] = 0;
1630                         }
1631                         led[2] = 0;
1632                         led[3] = 0;
1633                 } else { /* with frame sync */
1634                         /* TODO make it work */
1635                         led[0] = 0;
1636                         led[1] = 0;
1637                         led[2] = 0;
1638                         led[3] = 1;
1639                 }
1640                 leds = (led[0] | (led[1]<<2) | (led[2]<<1) | (led[3]<<3))^0xF;
1641                         /* leds are inverted */
1642                 if (leds != (int)hc->ledstate) {
1643                         HFC_outb_nodebug(hc, R_GPIO_OUT1, leds);
1644                         hc->ledstate = leds;
1645                 }
1646                 break;
1647
1648         case 2: /* HFC-4S OEM */
1649                 /* red blinking = PH_DEACTIVATE NT Mode
1650                  * red steady   = PH_DEACTIVATE TE Mode
1651                  * green steady = PH_ACTIVATE
1652                  */
1653                 for (i = 0; i < 4; i++) {
1654                         state = 0;
1655                         active = -1;
1656                         dch = hc->chan[(i << 2) | 2].dch;
1657                         if (dch) {
1658                                 state = dch->state;
1659                                 if (dch->dev.D.protocol == ISDN_P_NT_S0)
1660                                         active = 3;
1661                                 else
1662                                         active = 7;
1663                         }
1664                         if (state) {
1665                                 if (state == active) {
1666                                         led[i] = 1; /* led green */
1667                                 } else
1668                                         if (dch->dev.D.protocol == ISDN_P_TE_S0)
1669                                                 /* TE mode: led red */
1670                                                 led[i] = 2;
1671                                         else
1672                                                 if (hc->ledcount>>11)
1673                                                         /* led red */
1674                                                         led[i] = 2;
1675                                                 else
1676                                                         /* led off */
1677                                                         led[i] = 0;
1678                         } else
1679                                 led[i] = 0; /* led off */
1680                 }
1681                 if (test_bit(HFC_CHIP_B410P, &hc->chip)) {
1682                         leds = 0;
1683                         for (i = 0; i < 4; i++) {
1684                                 if (led[i] == 1) {
1685                                         /*green*/
1686                                         leds |= (0x2 << (i * 2));
1687                                 } else if (led[i] == 2) {
1688                                         /*red*/
1689                                         leds |= (0x1 << (i * 2));
1690                                 }
1691                         }
1692                         if (leds != (int)hc->ledstate) {
1693                                 vpm_out(hc, 0, 0x1a8 + 3, leds);
1694                                 hc->ledstate = leds;
1695                         }
1696                 } else {
1697                         leds = ((led[3] > 0) << 0) | ((led[1] > 0) << 1) |
1698                             ((led[0] > 0) << 2) | ((led[2] > 0) << 3) |
1699                             ((led[3] & 1) << 4) | ((led[1] & 1) << 5) |
1700                             ((led[0] & 1) << 6) | ((led[2] & 1) << 7);
1701                         if (leds != (int)hc->ledstate) {
1702                                 HFC_outb_nodebug(hc, R_GPIO_EN1, leds & 0x0F);
1703                                 HFC_outb_nodebug(hc, R_GPIO_OUT1, leds >> 4);
1704                                 hc->ledstate = leds;
1705                         }
1706                 }
1707                 break;
1708
1709         case 3: /* HFC 1S/2S Beronet */
1710                 /* red blinking = PH_DEACTIVATE NT Mode
1711                  * red steady   = PH_DEACTIVATE TE Mode
1712                  * green steady = PH_ACTIVATE
1713                  */
1714                 for (i = 0; i < 2; i++) {
1715                         state = 0;
1716                         active = -1;
1717                         dch = hc->chan[(i << 2) | 2].dch;
1718                         if (dch) {
1719                                 state = dch->state;
1720                                 if (dch->dev.D.protocol == ISDN_P_NT_S0)
1721                                         active = 3;
1722                                 else
1723                                         active = 7;
1724                         }
1725                         if (state) {
1726                                 if (state == active) {
1727                                         led[i] = 1; /* led green */
1728                                 } else
1729                                         if (dch->dev.D.protocol == ISDN_P_TE_S0)
1730                                                 /* TE mode: led red */
1731                                                 led[i] = 2;
1732                                         else
1733                                                 if (hc->ledcount >> 11)
1734                                                         /* led red */
1735                                                         led[i] = 2;
1736                                                 else
1737                                                         /* led off */
1738                                                         led[i] = 0;
1739                         } else
1740                                 led[i] = 0; /* led off */
1741                 }
1742
1743
1744                 leds = (led[0] > 0) | ((led[1] > 0)<<1) | ((led[0]&1)<<2)
1745                         | ((led[1]&1)<<3);
1746                 if (leds != (int)hc->ledstate) {
1747                         HFC_outb_nodebug(hc, R_GPIO_EN1,
1748                             ((led[0] > 0) << 2) | ((led[1] > 0) << 3));
1749                         HFC_outb_nodebug(hc, R_GPIO_OUT1,
1750                             ((led[0] & 1) << 2) | ((led[1] & 1) << 3));
1751                         hc->ledstate = leds;
1752                 }
1753                 break;
1754         case 8: /* HFC 8S+ Beronet */
1755                 lled = 0;
1756
1757                 for (i = 0; i < 8; i++) {
1758                         state = 0;
1759                         active = -1;
1760                         dch = hc->chan[(i << 2) | 2].dch;
1761                         if (dch) {
1762                                 state = dch->state;
1763                                 if (dch->dev.D.protocol == ISDN_P_NT_S0)
1764                                         active = 3;
1765                                 else
1766                                         active = 7;
1767                         }
1768                         if (state) {
1769                                 if (state == active) {
1770                                         lled |= 0 << i;
1771                                 } else
1772                                         if (hc->ledcount >> 11)
1773                                                 lled |= 0 << i;
1774                                         else
1775                                                 lled |= 1 << i;
1776                         } else
1777                                 lled |= 1 << i;
1778                 }
1779                 leddw = lled << 24 | lled << 16 | lled << 8 | lled;
1780                 if (leddw != hc->ledstate) {
1781                         /* HFC_outb(hc, R_BRG_PCM_CFG, 1);
1782                         HFC_outb(c, R_BRG_PCM_CFG, (0x0 << 6) | 0x3); */
1783                         /* was _io before */
1784                         HFC_outb_nodebug(hc, R_BRG_PCM_CFG, 1 | V_PCM_CLK);
1785                         outw(0x4000, hc->pci_iobase + 4);
1786                         outl(leddw, hc->pci_iobase);
1787                         HFC_outb_nodebug(hc, R_BRG_PCM_CFG, V_PCM_CLK);
1788                         hc->ledstate = leddw;
1789                 }
1790                 break;
1791         }
1792 }
1793 /*
1794  * read dtmf coefficients
1795  */
1796
1797 static void
1798 hfcmulti_dtmf(struct hfc_multi *hc)
1799 {
1800         s32             *coeff;
1801         u_int           mantissa;
1802         int             co, ch;
1803         struct bchannel *bch = NULL;
1804         u8              exponent;
1805         int             dtmf = 0;
1806         int             addr;
1807         u16             w_float;
1808         struct sk_buff  *skb;
1809         struct mISDNhead *hh;
1810
1811         if (debug & DEBUG_HFCMULTI_DTMF)
1812                 printk(KERN_DEBUG "%s: dtmf detection irq\n", __func__);
1813         for (ch = 0; ch <= 31; ch++) {
1814                 /* only process enabled B-channels */
1815                 bch = hc->chan[ch].bch;
1816                 if (!bch)
1817                         continue;
1818                 if (!hc->created[hc->chan[ch].port])
1819                         continue;
1820                 if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
1821                         continue;
1822                 if (debug & DEBUG_HFCMULTI_DTMF)
1823                         printk(KERN_DEBUG "%s: dtmf channel %d:",
1824                                 __func__, ch);
1825                 coeff = &(hc->chan[ch].coeff[hc->chan[ch].coeff_count * 16]);
1826                 dtmf = 1;
1827                 for (co = 0; co < 8; co++) {
1828                         /* read W(n-1) coefficient */
1829                         addr = hc->DTMFbase + ((co<<7) | (ch<<2));
1830                         HFC_outb_nodebug(hc, R_RAM_ADDR0, addr);
1831                         HFC_outb_nodebug(hc, R_RAM_ADDR1, addr>>8);
1832                         HFC_outb_nodebug(hc, R_RAM_ADDR2, (addr>>16)
1833                                 | V_ADDR_INC);
1834                         w_float = HFC_inb_nodebug(hc, R_RAM_DATA);
1835                         w_float |= (HFC_inb_nodebug(hc, R_RAM_DATA) << 8);
1836                         if (debug & DEBUG_HFCMULTI_DTMF)
1837                                 printk(" %04x", w_float);
1838
1839                         /* decode float (see chip doc) */
1840                         mantissa = w_float & 0x0fff;
1841                         if (w_float & 0x8000)
1842                                 mantissa |= 0xfffff000;
1843                         exponent = (w_float>>12) & 0x7;
1844                         if (exponent) {
1845                                 mantissa ^= 0x1000;
1846                                 mantissa <<= (exponent-1);
1847                         }
1848
1849                         /* store coefficient */
1850                         coeff[co<<1] = mantissa;
1851
1852                         /* read W(n) coefficient */
1853                         w_float = HFC_inb_nodebug(hc, R_RAM_DATA);
1854                         w_float |= (HFC_inb_nodebug(hc, R_RAM_DATA) << 8);
1855                         if (debug & DEBUG_HFCMULTI_DTMF)
1856                                 printk(" %04x", w_float);
1857
1858                         /* decode float (see chip doc) */
1859                         mantissa = w_float & 0x0fff;
1860                         if (w_float & 0x8000)
1861                                 mantissa |= 0xfffff000;
1862                         exponent = (w_float>>12) & 0x7;
1863                         if (exponent) {
1864                                 mantissa ^= 0x1000;
1865                                 mantissa <<= (exponent-1);
1866                         }
1867
1868                         /* store coefficient */
1869                         coeff[(co<<1)|1] = mantissa;
1870                 }
1871                 if (debug & DEBUG_HFCMULTI_DTMF)
1872                         printk(" DTMF ready %08x %08x %08x %08x "
1873                             "%08x %08x %08x %08x\n",
1874                             coeff[0], coeff[1], coeff[2], coeff[3],
1875                             coeff[4], coeff[5], coeff[6], coeff[7]);
1876                 hc->chan[ch].coeff_count++;
1877                 if (hc->chan[ch].coeff_count == 8) {
1878                         hc->chan[ch].coeff_count = 0;
1879                         skb = mI_alloc_skb(512, GFP_ATOMIC);
1880                         if (!skb) {
1881                                 printk(KERN_WARNING "%s: No memory for skb\n",
1882                                     __func__);
1883                                 continue;
1884                         }
1885                         hh = mISDN_HEAD_P(skb);
1886                         hh->prim = PH_CONTROL_IND;
1887                         hh->id = DTMF_HFC_COEF;
1888                         memcpy(skb_put(skb, 512), hc->chan[ch].coeff, 512);
1889                         recv_Bchannel_skb(bch, skb);
1890                 }
1891         }
1892
1893         /* restart DTMF processing */
1894         hc->dtmf = dtmf;
1895         if (dtmf)
1896                 HFC_outb_nodebug(hc, R_DTMF, hc->hw.r_dtmf | V_RST_DTMF);
1897 }
1898
1899
1900 /*
1901  * fill fifo as much as possible
1902  */
1903
1904 static void
1905 hfcmulti_tx(struct hfc_multi *hc, int ch)
1906 {
1907         int i, ii, temp, len = 0;
1908         int Zspace, z1, z2; /* must be int for calculation */
1909         int Fspace, f1, f2;
1910         u_char *d;
1911         int *txpending, slot_tx;
1912         struct  bchannel *bch;
1913         struct  dchannel *dch;
1914         struct  sk_buff **sp = NULL;
1915         int *idxp;
1916
1917         bch = hc->chan[ch].bch;
1918         dch = hc->chan[ch].dch;
1919         if ((!dch) && (!bch))
1920                 return;
1921
1922         txpending = &hc->chan[ch].txpending;
1923         slot_tx = hc->chan[ch].slot_tx;
1924         if (dch) {
1925                 if (!test_bit(FLG_ACTIVE, &dch->Flags))
1926                         return;
1927                 sp = &dch->tx_skb;
1928                 idxp = &dch->tx_idx;
1929         } else {
1930                 if (!test_bit(FLG_ACTIVE, &bch->Flags))
1931                         return;
1932                 sp = &bch->tx_skb;
1933                 idxp = &bch->tx_idx;
1934         }
1935         if (*sp)
1936                 len = (*sp)->len;
1937
1938         if ((!len) && *txpending != 1)
1939                 return; /* no data */
1940
1941         if (test_bit(HFC_CHIP_B410P, &hc->chip) &&
1942             (hc->chan[ch].protocol == ISDN_P_B_RAW) &&
1943             (hc->chan[ch].slot_rx < 0) &&
1944             (hc->chan[ch].slot_tx < 0))
1945                 HFC_outb_nodebug(hc, R_FIFO, 0x20 | (ch << 1));
1946         else
1947                 HFC_outb_nodebug(hc, R_FIFO, ch << 1);
1948         HFC_wait_nodebug(hc);
1949
1950         if (*txpending == 2) {
1951                 /* reset fifo */
1952                 HFC_outb_nodebug(hc, R_INC_RES_FIFO, V_RES_F);
1953                 HFC_wait_nodebug(hc);
1954                 HFC_outb(hc, A_SUBCH_CFG, 0);
1955                 *txpending = 1;
1956         }
1957 next_frame:
1958         if (dch || test_bit(FLG_HDLC, &bch->Flags)) {
1959                 f1 = HFC_inb_nodebug(hc, A_F1);
1960                 f2 = HFC_inb_nodebug(hc, A_F2);
1961                 while (f2 != (temp = HFC_inb_nodebug(hc, A_F2))) {
1962                         if (debug & DEBUG_HFCMULTI_FIFO)
1963                                 printk(KERN_DEBUG
1964                                     "%s(card %d): reread f2 because %d!=%d\n",
1965                                     __func__, hc->id + 1, temp, f2);
1966                         f2 = temp; /* repeat until F2 is equal */
1967                 }
1968                 Fspace = f2 - f1 - 1;
1969                 if (Fspace < 0)
1970                         Fspace += hc->Flen;
1971                 /*
1972                  * Old FIFO handling doesn't give us the current Z2 read
1973                  * pointer, so we cannot send the next frame before the fifo
1974                  * is empty. It makes no difference except for a slightly
1975                  * lower performance.
1976                  */
1977                 if (test_bit(HFC_CHIP_REVISION0, &hc->chip)) {
1978                         if (f1 != f2)
1979                                 Fspace = 0;
1980                         else
1981                                 Fspace = 1;
1982                 }
1983                 /* one frame only for ST D-channels, to allow resending */
1984                 if (hc->ctype != HFC_TYPE_E1 && dch) {
1985                         if (f1 != f2)
1986                                 Fspace = 0;
1987                 }
1988                 /* F-counter full condition */
1989                 if (Fspace == 0)
1990                         return;
1991         }
1992         z1 = HFC_inw_nodebug(hc, A_Z1) - hc->Zmin;
1993         z2 = HFC_inw_nodebug(hc, A_Z2) - hc->Zmin;
1994         while (z2 != (temp = (HFC_inw_nodebug(hc, A_Z2) - hc->Zmin))) {
1995                 if (debug & DEBUG_HFCMULTI_FIFO)
1996                         printk(KERN_DEBUG "%s(card %d): reread z2 because "
1997                                 "%d!=%d\n", __func__, hc->id + 1, temp, z2);
1998                 z2 = temp; /* repeat unti Z2 is equal */
1999         }
2000         hc->chan[ch].Zfill = z1 - z2;
2001         if (hc->chan[ch].Zfill < 0)
2002                 hc->chan[ch].Zfill += hc->Zlen;
2003         Zspace = z2 - z1;
2004         if (Zspace <= 0)
2005                 Zspace += hc->Zlen;
2006         Zspace -= 4; /* keep not too full, so pointers will not overrun */
2007         /* fill transparent data only to maxinum transparent load (minus 4) */
2008         if (bch && test_bit(FLG_TRANSPARENT, &bch->Flags))
2009                 Zspace = Zspace - hc->Zlen + hc->max_trans;
2010         if (Zspace <= 0) /* no space of 4 bytes */
2011                 return;
2012
2013         /* if no data */
2014         if (!len) {
2015                 if (z1 == z2) { /* empty */
2016                         /* if done with FIFO audio data during PCM connection */
2017                         if (bch && (!test_bit(FLG_HDLC, &bch->Flags)) &&
2018                             *txpending && slot_tx >= 0) {
2019                                 if (debug & DEBUG_HFCMULTI_MODE)
2020                                         printk(KERN_DEBUG
2021                                             "%s: reconnecting PCM due to no "
2022                                             "more FIFO data: channel %d "
2023                                             "slot_tx %d\n",
2024                                             __func__, ch, slot_tx);
2025                                 /* connect slot */
2026                                 if (hc->ctype == HFC_TYPE_XHFC)
2027                                         HFC_outb(hc, A_CON_HDLC, 0xc0
2028                                             | 0x07 << 2 | V_HDLC_TRP | V_IFF);
2029                                                 /* Enable FIFO, no interrupt */
2030                                 else
2031                                         HFC_outb(hc, A_CON_HDLC, 0xc0 | 0x00 |
2032                                             V_HDLC_TRP | V_IFF);
2033                                 HFC_outb_nodebug(hc, R_FIFO, ch<<1 | 1);
2034                                 HFC_wait_nodebug(hc);
2035                                 if (hc->ctype == HFC_TYPE_XHFC)
2036                                         HFC_outb(hc, A_CON_HDLC, 0xc0
2037                                             | 0x07 << 2 | V_HDLC_TRP | V_IFF);
2038                                                 /* Enable FIFO, no interrupt */
2039                                 else
2040                                         HFC_outb(hc, A_CON_HDLC, 0xc0 | 0x00 |
2041                                             V_HDLC_TRP | V_IFF);
2042                                 HFC_outb_nodebug(hc, R_FIFO, ch<<1);
2043                                 HFC_wait_nodebug(hc);
2044                         }
2045                         *txpending = 0;
2046                 }
2047                 return; /* no data */
2048         }
2049
2050         /* "fill fifo if empty" feature */
2051         if (bch && test_bit(FLG_FILLEMPTY, &bch->Flags)
2052                 && !test_bit(FLG_HDLC, &bch->Flags) && z2 == z1) {
2053                 if (debug & DEBUG_HFCMULTI_FILL)
2054                         printk(KERN_DEBUG "%s: buffer empty, so we have "
2055                                 "underrun\n", __func__);
2056                 /* fill buffer, to prevent future underrun */
2057                 hc->write_fifo(hc, hc->silence_data, poll >> 1);
2058                 Zspace -= (poll >> 1);
2059         }
2060
2061         /* if audio data and connected slot */
2062         if (bch && (!test_bit(FLG_HDLC, &bch->Flags)) && (!*txpending)
2063                 && slot_tx >= 0) {
2064                 if (debug & DEBUG_HFCMULTI_MODE)
2065                         printk(KERN_DEBUG "%s: disconnecting PCM due to "
2066                             "FIFO data: channel %d slot_tx %d\n",
2067                             __func__, ch, slot_tx);
2068                 /* disconnect slot */
2069                 if (hc->ctype == HFC_TYPE_XHFC)
2070                         HFC_outb(hc, A_CON_HDLC, 0x80
2071                             | 0x07 << 2 | V_HDLC_TRP | V_IFF);
2072                                 /* Enable FIFO, no interrupt */
2073                 else
2074                         HFC_outb(hc, A_CON_HDLC, 0x80 | 0x00 |
2075                             V_HDLC_TRP | V_IFF);
2076                 HFC_outb_nodebug(hc, R_FIFO, ch<<1 | 1);
2077                 HFC_wait_nodebug(hc);
2078                 if (hc->ctype == HFC_TYPE_XHFC)
2079                         HFC_outb(hc, A_CON_HDLC, 0x80
2080                             | 0x07 << 2 | V_HDLC_TRP | V_IFF);
2081                                 /* Enable FIFO, no interrupt */
2082                 else
2083                         HFC_outb(hc, A_CON_HDLC, 0x80 | 0x00 |
2084                             V_HDLC_TRP | V_IFF);
2085                 HFC_outb_nodebug(hc, R_FIFO, ch<<1);
2086                 HFC_wait_nodebug(hc);
2087         }
2088         *txpending = 1;
2089
2090         /* show activity */
2091         hc->activity[hc->chan[ch].port] = 1;
2092
2093         /* fill fifo to what we have left */
2094         ii = len;
2095         if (dch || test_bit(FLG_HDLC, &bch->Flags))
2096                 temp = 1;
2097         else
2098                 temp = 0;
2099         i = *idxp;
2100         d = (*sp)->data + i;
2101         if (ii - i > Zspace)
2102                 ii = Zspace + i;
2103         if (debug & DEBUG_HFCMULTI_FIFO)
2104                 printk(KERN_DEBUG "%s(card %d): fifo(%d) has %d bytes space "
2105                     "left (z1=%04x, z2=%04x) sending %d of %d bytes %s\n",
2106                         __func__, hc->id + 1, ch, Zspace, z1, z2, ii-i, len-i,
2107                         temp ? "HDLC":"TRANS");
2108
2109         /* Have to prep the audio data */
2110         hc->write_fifo(hc, d, ii - i);
2111         hc->chan[ch].Zfill += ii - i;
2112         *idxp = ii;
2113
2114         /* if not all data has been written */
2115         if (ii != len) {
2116                 /* NOTE: fifo is started by the calling function */
2117                 return;
2118         }
2119
2120         /* if all data has been written, terminate frame */
2121         if (dch || test_bit(FLG_HDLC, &bch->Flags)) {
2122                 /* increment f-counter */
2123                 HFC_outb_nodebug(hc, R_INC_RES_FIFO, V_INC_F);
2124                 HFC_wait_nodebug(hc);
2125         }
2126
2127         /* send confirm, since get_net_bframe will not do it with trans */
2128         if (bch && test_bit(FLG_TRANSPARENT, &bch->Flags))
2129                 confirm_Bsend(bch);
2130
2131         /* check for next frame */
2132         dev_kfree_skb(*sp);
2133         if (bch && get_next_bframe(bch)) { /* hdlc is confirmed here */
2134                 len = (*sp)->len;
2135                 goto next_frame;
2136         }
2137         if (dch && get_next_dframe(dch)) {
2138                 len = (*sp)->len;
2139                 goto next_frame;
2140         }
2141
2142         /*
2143          * now we have no more data, so in case of transparent,
2144          * we set the last byte in fifo to 'silence' in case we will get
2145          * no more data at all. this prevents sending an undefined value.
2146          */
2147         if (bch && test_bit(FLG_TRANSPARENT, &bch->Flags))
2148                 HFC_outb_nodebug(hc, A_FIFO_DATA0_NOINC, hc->silence);
2149 }
2150
2151
2152 /* NOTE: only called if E1 card is in active state */
2153 static void
2154 hfcmulti_rx(struct hfc_multi *hc, int ch)
2155 {
2156         int temp;
2157         int Zsize, z1, z2 = 0; /* = 0, to make GCC happy */
2158         int f1 = 0, f2 = 0; /* = 0, to make GCC happy */
2159         int again = 0;
2160         struct  bchannel *bch;
2161         struct  dchannel *dch;
2162         struct sk_buff  *skb, **sp = NULL;
2163         int     maxlen;
2164
2165         bch = hc->chan[ch].bch;
2166         dch = hc->chan[ch].dch;
2167         if ((!dch) && (!bch))
2168                 return;
2169         if (dch) {
2170                 if (!test_bit(FLG_ACTIVE, &dch->Flags))
2171                         return;
2172                 sp = &dch->rx_skb;
2173                 maxlen = dch->maxlen;
2174         } else {
2175                 if (!test_bit(FLG_ACTIVE, &bch->Flags))
2176                         return;
2177                 sp = &bch->rx_skb;
2178                 maxlen = bch->maxlen;
2179         }
2180 next_frame:
2181         /* on first AND before getting next valid frame, R_FIFO must be written
2182            to. */
2183         if (test_bit(HFC_CHIP_B410P, &hc->chip) &&
2184             (hc->chan[ch].protocol == ISDN_P_B_RAW) &&
2185             (hc->chan[ch].slot_rx < 0) &&
2186             (hc->chan[ch].slot_tx < 0))
2187                 HFC_outb_nodebug(hc, R_FIFO, 0x20 | (ch<<1) | 1);
2188         else
2189                 HFC_outb_nodebug(hc, R_FIFO, (ch<<1)|1);
2190         HFC_wait_nodebug(hc);
2191
2192         /* ignore if rx is off BUT change fifo (above) to start pending TX */
2193         if (hc->chan[ch].rx_off)
2194                 return;
2195
2196         if (dch || test_bit(FLG_HDLC, &bch->Flags)) {
2197                 f1 = HFC_inb_nodebug(hc, A_F1);
2198                 while (f1 != (temp = HFC_inb_nodebug(hc, A_F1))) {
2199                         if (debug & DEBUG_HFCMULTI_FIFO)
2200                                 printk(KERN_DEBUG
2201                                     "%s(card %d): reread f1 because %d!=%d\n",
2202                                     __func__, hc->id + 1, temp, f1);
2203                         f1 = temp; /* repeat until F1 is equal */
2204                 }
2205                 f2 = HFC_inb_nodebug(hc, A_F2);
2206         }
2207         z1 = HFC_inw_nodebug(hc, A_Z1) - hc->Zmin;
2208         while (z1 != (temp = (HFC_inw_nodebug(hc, A_Z1) - hc->Zmin))) {
2209                 if (debug & DEBUG_HFCMULTI_FIFO)
2210                         printk(KERN_DEBUG "%s(card %d): reread z2 because "
2211                                 "%d!=%d\n", __func__, hc->id + 1, temp, z2);
2212                 z1 = temp; /* repeat until Z1 is equal */
2213         }
2214         z2 = HFC_inw_nodebug(hc, A_Z2) - hc->Zmin;
2215         Zsize = z1 - z2;
2216         if ((dch || test_bit(FLG_HDLC, &bch->Flags)) && f1 != f2)
2217                 /* complete hdlc frame */
2218                 Zsize++;
2219         if (Zsize < 0)
2220                 Zsize += hc->Zlen;
2221         /* if buffer is empty */
2222         if (Zsize <= 0)
2223                 return;
2224
2225         if (*sp == NULL) {
2226                 *sp = mI_alloc_skb(maxlen + 3, GFP_ATOMIC);
2227                 if (*sp == NULL) {
2228                         printk(KERN_DEBUG "%s: No mem for rx_skb\n",
2229                             __func__);
2230                         return;
2231                 }
2232         }
2233         /* show activity */
2234         hc->activity[hc->chan[ch].port] = 1;
2235
2236         /* empty fifo with what we have */
2237         if (dch || test_bit(FLG_HDLC, &bch->Flags)) {
2238                 if (debug & DEBUG_HFCMULTI_FIFO)
2239                         printk(KERN_DEBUG "%s(card %d): fifo(%d) reading %d "
2240                             "bytes (z1=%04x, z2=%04x) HDLC %s (f1=%d, f2=%d) "
2241                             "got=%d (again %d)\n", __func__, hc->id + 1, ch,
2242                             Zsize, z1, z2, (f1 == f2) ? "fragment" : "COMPLETE",
2243                             f1, f2, Zsize + (*sp)->len, again);
2244                 /* HDLC */
2245                 if ((Zsize + (*sp)->len) > (maxlen + 3)) {
2246                         if (debug & DEBUG_HFCMULTI_FIFO)
2247                                 printk(KERN_DEBUG
2248                                     "%s(card %d): hdlc-frame too large.\n",
2249                                     __func__, hc->id + 1);
2250                         skb_trim(*sp, 0);
2251                         HFC_outb_nodebug(hc, R_INC_RES_FIFO, V_RES_F);
2252                         HFC_wait_nodebug(hc);
2253                         return;
2254                 }
2255
2256                 hc->read_fifo(hc, skb_put(*sp, Zsize), Zsize);
2257
2258                 if (f1 != f2) {
2259                         /* increment Z2,F2-counter */
2260                         HFC_outb_nodebug(hc, R_INC_RES_FIFO, V_INC_F);
2261                         HFC_wait_nodebug(hc);
2262                         /* check size */
2263                         if ((*sp)->len < 4) {
2264                                 if (debug & DEBUG_HFCMULTI_FIFO)
2265                                         printk(KERN_DEBUG
2266                                             "%s(card %d): Frame below minimum "
2267                                             "size\n", __func__, hc->id + 1);
2268                                 skb_trim(*sp, 0);
2269                                 goto next_frame;
2270                         }
2271                         /* there is at least one complete frame, check crc */
2272                         if ((*sp)->data[(*sp)->len - 1]) {
2273                                 if (debug & DEBUG_HFCMULTI_CRC)
2274                                         printk(KERN_DEBUG
2275                                             "%s: CRC-error\n", __func__);
2276                                 skb_trim(*sp, 0);
2277                                 goto next_frame;
2278                         }
2279                         skb_trim(*sp, (*sp)->len - 3);
2280                         if ((*sp)->len < MISDN_COPY_SIZE) {
2281                                 skb = *sp;
2282                                 *sp = mI_alloc_skb(skb->len, GFP_ATOMIC);
2283                                 if (*sp) {
2284                                         memcpy(skb_put(*sp, skb->len),
2285                                             skb->data, skb->len);
2286                                         skb_trim(skb, 0);
2287                                 } else {
2288                                         printk(KERN_DEBUG "%s: No mem\n",
2289                                             __func__);
2290                                         *sp = skb;
2291                                         skb = NULL;
2292                                 }
2293                         } else {
2294                                 skb = NULL;
2295                         }
2296                         if (debug & DEBUG_HFCMULTI_FIFO) {
2297                                 printk(KERN_DEBUG "%s(card %d):",
2298                                         __func__, hc->id + 1);
2299                                 temp = 0;
2300                                 while (temp < (*sp)->len)
2301                                         printk(" %02x", (*sp)->data[temp++]);
2302                                 printk("\n");
2303                         }
2304                         if (dch)
2305                                 recv_Dchannel(dch);
2306                         else
2307                                 recv_Bchannel(bch, MISDN_ID_ANY);
2308                         *sp = skb;
2309                         again++;
2310                         goto next_frame;
2311                 }
2312                 /* there is an incomplete frame */
2313         } else {
2314                 /* transparent */
2315                 if (Zsize > skb_tailroom(*sp))
2316                         Zsize = skb_tailroom(*sp);
2317                 hc->read_fifo(hc, skb_put(*sp, Zsize), Zsize);
2318                 if (((*sp)->len) < MISDN_COPY_SIZE) {
2319                         skb = *sp;
2320                         *sp = mI_alloc_skb(skb->len, GFP_ATOMIC);
2321                         if (*sp) {
2322                                 memcpy(skb_put(*sp, skb->len),
2323                                     skb->data, skb->len);
2324                                 skb_trim(skb, 0);
2325                         } else {
2326                                 printk(KERN_DEBUG "%s: No mem\n", __func__);
2327                                 *sp = skb;
2328                                 skb = NULL;
2329                         }
2330                 } else {
2331                         skb = NULL;
2332                 }
2333                 if (debug & DEBUG_HFCMULTI_FIFO)
2334                         printk(KERN_DEBUG
2335                             "%s(card %d): fifo(%d) reading %d bytes "
2336                             "(z1=%04x, z2=%04x) TRANS\n",
2337                                 __func__, hc->id + 1, ch, Zsize, z1, z2);
2338                 /* only bch is transparent */
2339                 recv_Bchannel(bch, hc->chan[ch].Zfill);
2340                 *sp = skb;
2341         }
2342 }
2343
2344
2345 /*
2346  * Interrupt handler
2347  */
2348 static void
2349 signal_state_up(struct dchannel *dch, int info, char *msg)
2350 {
2351         struct sk_buff  *skb;
2352         int             id, data = info;
2353
2354         if (debug & DEBUG_HFCMULTI_STATE)
2355                 printk(KERN_DEBUG "%s: %s\n", __func__, msg);
2356
2357         id = TEI_SAPI | (GROUP_TEI << 8); /* manager address */
2358
2359         skb = _alloc_mISDN_skb(MPH_INFORMATION_IND, id, sizeof(data), &data,
2360                 GFP_ATOMIC);
2361         if (!skb)
2362                 return;
2363         recv_Dchannel_skb(dch, skb);
2364 }
2365
2366 static inline void
2367 handle_timer_irq(struct hfc_multi *hc)
2368 {
2369         int             ch, temp;
2370         struct dchannel *dch;
2371         u_long          flags;
2372
2373         /* process queued resync jobs */
2374         if (hc->e1_resync) {
2375                 /* lock, so e1_resync gets not changed */
2376                 spin_lock_irqsave(&HFClock, flags);
2377                 if (hc->e1_resync & 1) {
2378                         if (debug & DEBUG_HFCMULTI_PLXSD)
2379                                 printk(KERN_DEBUG "Enable SYNC_I\n");
2380                         HFC_outb(hc, R_SYNC_CTRL, V_EXT_CLK_SYNC);
2381                         /* disable JATT, if RX_SYNC is set */
2382                         if (test_bit(HFC_CHIP_RX_SYNC, &hc->chip))
2383                                 HFC_outb(hc, R_SYNC_OUT, V_SYNC_E1_RX);
2384                 }
2385                 if (hc->e1_resync & 2) {
2386                         if (debug & DEBUG_HFCMULTI_PLXSD)
2387                                 printk(KERN_DEBUG "Enable jatt PLL\n");
2388                         HFC_outb(hc, R_SYNC_CTRL, V_SYNC_OFFS);
2389                 }
2390                 if (hc->e1_resync & 4) {
2391                         if (debug & DEBUG_HFCMULTI_PLXSD)
2392                                 printk(KERN_DEBUG
2393                                     "Enable QUARTZ for HFC-E1\n");
2394                         /* set jatt to quartz */
2395                         HFC_outb(hc, R_SYNC_CTRL, V_EXT_CLK_SYNC
2396                                 | V_JATT_OFF);
2397                         /* switch to JATT, in case it is not already */
2398                         HFC_outb(hc, R_SYNC_OUT, 0);
2399                 }
2400                 hc->e1_resync = 0;
2401                 spin_unlock_irqrestore(&HFClock, flags);
2402         }
2403
2404         if (hc->ctype != HFC_TYPE_E1 || hc->e1_state == 1)
2405                 for (ch = 0; ch <= 31; ch++) {
2406                         if (hc->created[hc->chan[ch].port]) {
2407                                 hfcmulti_tx(hc, ch);
2408                                 /* fifo is started when switching to rx-fifo */
2409                                 hfcmulti_rx(hc, ch);
2410                                 if (hc->chan[ch].dch &&
2411                                     hc->chan[ch].nt_timer > -1) {
2412                                         dch = hc->chan[ch].dch;
2413                                         if (!(--hc->chan[ch].nt_timer)) {
2414                                                 schedule_event(dch,
2415                                                     FLG_PHCHANGE);
2416                                                 if (debug &
2417                                                     DEBUG_HFCMULTI_STATE)
2418                                                         printk(KERN_DEBUG
2419                                                             "%s: nt_timer at "
2420                                                             "state %x\n",
2421                                                             __func__,
2422                                                             dch->state);
2423                                         }
2424                                 }
2425                         }
2426                 }
2427         if (hc->ctype == HFC_TYPE_E1 && hc->created[0]) {
2428                 dch = hc->chan[hc->dslot].dch;
2429                 if (test_bit(HFC_CFG_REPORT_LOS, &hc->chan[hc->dslot].cfg)) {
2430                         /* LOS */
2431                         temp = HFC_inb_nodebug(hc, R_SYNC_STA) & V_SIG_LOS;
2432                         if (!temp && hc->chan[hc->dslot].los)
2433                                 signal_state_up(dch, L1_SIGNAL_LOS_ON,
2434                                     "LOS detected");
2435                         if (temp && !hc->chan[hc->dslot].los)
2436                                 signal_state_up(dch, L1_SIGNAL_LOS_OFF,
2437                                     "LOS gone");
2438                         hc->chan[hc->dslot].los = temp;
2439                 }
2440                 if (test_bit(HFC_CFG_REPORT_AIS, &hc->chan[hc->dslot].cfg)) {
2441                         /* AIS */
2442                         temp = HFC_inb_nodebug(hc, R_SYNC_STA) & V_AIS;
2443                         if (!temp && hc->chan[hc->dslot].ais)
2444                                 signal_state_up(dch, L1_SIGNAL_AIS_ON,
2445                                     "AIS detected");
2446                         if (temp && !hc->chan[hc->dslot].ais)
2447                                 signal_state_up(dch, L1_SIGNAL_AIS_OFF,
2448                                     "AIS gone");
2449                         hc->chan[hc->dslot].ais = temp;
2450                 }
2451                 if (test_bit(HFC_CFG_REPORT_SLIP, &hc->chan[hc->dslot].cfg)) {
2452                         /* SLIP */
2453                         temp = HFC_inb_nodebug(hc, R_SLIP) & V_FOSLIP_RX;
2454                         if (!temp && hc->chan[hc->dslot].slip_rx)
2455                                 signal_state_up(dch, L1_SIGNAL_SLIP_RX,
2456                                     " bit SLIP detected RX");
2457                         hc->chan[hc->dslot].slip_rx = temp;
2458                         temp = HFC_inb_nodebug(hc, R_SLIP) & V_FOSLIP_TX;
2459                         if (!temp && hc->chan[hc->dslot].slip_tx)
2460                                 signal_state_up(dch, L1_SIGNAL_SLIP_TX,
2461                                     " bit SLIP detected TX");
2462                         hc->chan[hc->dslot].slip_tx = temp;
2463                 }
2464                 if (test_bit(HFC_CFG_REPORT_RDI, &hc->chan[hc->dslot].cfg)) {
2465                         /* RDI */
2466                         temp = HFC_inb_nodebug(hc, R_RX_SL0_0) & V_A;
2467                         if (!temp && hc->chan[hc->dslot].rdi)
2468                                 signal_state_up(dch, L1_SIGNAL_RDI_ON,
2469                                     "RDI detected");
2470                         if (temp && !hc->chan[hc->dslot].rdi)
2471                                 signal_state_up(dch, L1_SIGNAL_RDI_OFF,
2472                                     "RDI gone");
2473                         hc->chan[hc->dslot].rdi = temp;
2474                 }
2475                 temp = HFC_inb_nodebug(hc, R_JATT_DIR);
2476                 switch (hc->chan[hc->dslot].sync) {
2477                 case 0:
2478                         if ((temp & 0x60) == 0x60) {
2479                                 if (debug & DEBUG_HFCMULTI_SYNC)
2480                                         printk(KERN_DEBUG
2481                                             "%s: (id=%d) E1 now "
2482                                             "in clock sync\n",
2483                                             __func__, hc->id);
2484                                 HFC_outb(hc, R_RX_OFF,
2485                                     hc->chan[hc->dslot].jitter | V_RX_INIT);
2486                                 HFC_outb(hc, R_TX_OFF,
2487                                     hc->chan[hc->dslot].jitter | V_RX_INIT);
2488                                 hc->chan[hc->dslot].sync = 1;
2489                                 goto check_framesync;
2490                         }
2491                         break;
2492                 case 1:
2493                         if ((temp & 0x60) != 0x60) {
2494                                 if (debug & DEBUG_HFCMULTI_SYNC)
2495                                         printk(KERN_DEBUG
2496                                             "%s: (id=%d) E1 "
2497                                             "lost clock sync\n",
2498                                             __func__, hc->id);
2499                                 hc->chan[hc->dslot].sync = 0;
2500                                 break;
2501                         }
2502 check_framesync:
2503                         temp = HFC_inb_nodebug(hc, R_SYNC_STA);
2504                         if (temp == 0x27) {
2505                                 if (debug & DEBUG_HFCMULTI_SYNC)
2506                                         printk(KERN_DEBUG
2507                                             "%s: (id=%d) E1 "
2508                                             "now in frame sync\n",
2509                                             __func__, hc->id);
2510                                 hc->chan[hc->dslot].sync = 2;
2511                         }
2512                         break;
2513                 case 2:
2514                         if ((temp & 0x60) != 0x60) {
2515                                 if (debug & DEBUG_HFCMULTI_SYNC)
2516                                         printk(KERN_DEBUG
2517                                             "%s: (id=%d) E1 lost "
2518                                             "clock & frame sync\n",
2519                                             __func__, hc->id);
2520                                 hc->chan[hc->dslot].sync = 0;
2521                                 break;
2522                         }
2523                         temp = HFC_inb_nodebug(hc, R_SYNC_STA);
2524                         if (temp != 0x27) {
2525                                 if (debug & DEBUG_HFCMULTI_SYNC)
2526                                         printk(KERN_DEBUG
2527                                             "%s: (id=%d) E1 "
2528                                             "lost frame sync\n",
2529                                             __func__, hc->id);
2530                                 hc->chan[hc->dslot].sync = 1;
2531                         }
2532                         break;
2533                 }
2534         }
2535
2536         if (test_bit(HFC_CHIP_WATCHDOG, &hc->chip))
2537                 hfcmulti_watchdog(hc);
2538
2539         if (hc->leds)
2540                 hfcmulti_leds(hc);
2541 }
2542
2543 static void
2544 ph_state_irq(struct hfc_multi *hc, u_char r_irq_statech)
2545 {
2546         struct dchannel *dch;
2547         int             ch;
2548         int             active;
2549         u_char          st_status, temp;
2550
2551         /* state machine */
2552         for (ch = 0; ch <= 31; ch++) {
2553                 if (hc->chan[ch].dch) {
2554                         dch = hc->chan[ch].dch;
2555                         if (r_irq_statech & 1) {
2556                                 HFC_outb_nodebug(hc, R_ST_SEL,
2557                                         hc->chan[ch].port);
2558                                 /* undocumented: delay after R_ST_SEL */
2559                                 udelay(1);
2560                                 /* undocumented: status changes during read */
2561                                 st_status = HFC_inb_nodebug(hc, A_ST_RD_STATE);
2562                                 while (st_status != (temp =
2563                                         HFC_inb_nodebug(hc, A_ST_RD_STATE))) {
2564                                         if (debug & DEBUG_HFCMULTI_STATE)
2565                                                 printk(KERN_DEBUG "%s: reread "
2566                                                     "STATE because %d!=%d\n",
2567                                                     __func__, temp,
2568                                                     st_status);
2569                                         st_status = temp; /* repeat */
2570                                 }
2571
2572                                 /* Speech Design TE-sync indication */
2573                                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip) &&
2574                                         dch->dev.D.protocol == ISDN_P_TE_S0) {
2575                                         if (st_status & V_FR_SYNC_ST)
2576                                                 hc->syncronized |=
2577                                                     (1 << hc->chan[ch].port);
2578                                         else
2579                                                 hc->syncronized &=
2580                                                    ~(1 << hc->chan[ch].port);
2581                                 }
2582                                 dch->state = st_status & 0x0f;
2583                                 if (dch->dev.D.protocol == ISDN_P_NT_S0)
2584                                         active = 3;
2585                                 else
2586                                         active = 7;
2587                                 if (dch->state == active) {
2588                                         HFC_outb_nodebug(hc, R_FIFO,
2589                                                 (ch << 1) | 1);
2590                                         HFC_wait_nodebug(hc);
2591                                         HFC_outb_nodebug(hc,
2592                                                 R_INC_RES_FIFO, V_RES_F);
2593                                         HFC_wait_nodebug(hc);
2594                                         dch->tx_idx = 0;
2595                                 }
2596                                 schedule_event(dch, FLG_PHCHANGE);
2597                                 if (debug & DEBUG_HFCMULTI_STATE)
2598                                         printk(KERN_DEBUG
2599                                             "%s: S/T newstate %x port %d\n",
2600                                             __func__, dch->state,
2601                                             hc->chan[ch].port);
2602                         }
2603                         r_irq_statech >>= 1;
2604                 }
2605         }
2606         if (test_bit(HFC_CHIP_PLXSD, &hc->chip))
2607                 plxsd_checksync(hc, 0);
2608 }
2609
2610 static void
2611 fifo_irq(struct hfc_multi *hc, int block)
2612 {
2613         int     ch, j;
2614         struct dchannel *dch;
2615         struct bchannel *bch;
2616         u_char r_irq_fifo_bl;
2617
2618         r_irq_fifo_bl = HFC_inb_nodebug(hc, R_IRQ_FIFO_BL0 + block);
2619         j = 0;
2620         while (j < 8) {
2621                 ch = (block << 2) + (j >> 1);
2622                 dch = hc->chan[ch].dch;
2623                 bch = hc->chan[ch].bch;
2624                 if (((!dch) && (!bch)) || (!hc->created[hc->chan[ch].port])) {
2625                         j += 2;
2626                         continue;
2627                 }
2628                 if (dch && (r_irq_fifo_bl & (1 << j)) &&
2629                     test_bit(FLG_ACTIVE, &dch->Flags)) {
2630                         hfcmulti_tx(hc, ch);
2631                         /* start fifo */
2632                         HFC_outb_nodebug(hc, R_FIFO, 0);
2633                         HFC_wait_nodebug(hc);
2634                 }
2635                 if (bch && (r_irq_fifo_bl & (1 << j)) &&
2636                     test_bit(FLG_ACTIVE, &bch->Flags)) {
2637                         hfcmulti_tx(hc, ch);
2638                         /* start fifo */
2639                         HFC_outb_nodebug(hc, R_FIFO, 0);
2640                         HFC_wait_nodebug(hc);
2641                 }
2642                 j++;
2643                 if (dch && (r_irq_fifo_bl & (1 << j)) &&
2644                     test_bit(FLG_ACTIVE, &dch->Flags)) {
2645                         hfcmulti_rx(hc, ch);
2646                 }
2647                 if (bch && (r_irq_fifo_bl & (1 << j)) &&
2648                     test_bit(FLG_ACTIVE, &bch->Flags)) {
2649                         hfcmulti_rx(hc, ch);
2650                 }
2651                 j++;
2652         }
2653 }
2654
2655 #ifdef IRQ_DEBUG
2656 int irqsem;
2657 #endif
2658 static irqreturn_t
2659 hfcmulti_interrupt(int intno, void *dev_id)
2660 {
2661 #ifdef IRQCOUNT_DEBUG
2662         static int iq1 = 0, iq2 = 0, iq3 = 0, iq4 = 0,
2663             iq5 = 0, iq6 = 0, iqcnt = 0;
2664 #endif
2665         struct hfc_multi        *hc = dev_id;
2666         struct dchannel         *dch;
2667         u_char                  r_irq_statech, status, r_irq_misc, r_irq_oview;
2668         int                     i;
2669         void __iomem            *plx_acc;
2670         u_short                 wval;
2671         u_char                  e1_syncsta, temp;
2672         u_long                  flags;
2673
2674         if (!hc) {
2675                 printk(KERN_ERR "HFC-multi: Spurious interrupt!\n");
2676                 return IRQ_NONE;
2677         }
2678
2679         spin_lock(&hc->lock);
2680
2681 #ifdef IRQ_DEBUG
2682         if (irqsem)
2683                 printk(KERN_ERR "irq for card %d during irq from "
2684                 "card %d, this is no bug.\n", hc->id + 1, irqsem);
2685         irqsem = hc->id + 1;
2686 #endif
2687 #ifdef CONFIG_MISDN_HFCMULTI_8xx
2688         if (hc->immap->im_cpm.cp_pbdat & hc->pb_irqmsk)
2689                 goto irq_notforus;
2690 #endif
2691         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
2692                 spin_lock_irqsave(&plx_lock, flags);
2693                 plx_acc = hc->plx_membase + PLX_INTCSR;
2694                 wval = readw(plx_acc);
2695                 spin_unlock_irqrestore(&plx_lock, flags);
2696                 if (!(wval & PLX_INTCSR_LINTI1_STATUS))
2697                         goto irq_notforus;
2698         }
2699
2700         status = HFC_inb_nodebug(hc, R_STATUS);
2701         r_irq_statech = HFC_inb_nodebug(hc, R_IRQ_STATECH);
2702 #ifdef IRQCOUNT_DEBUG
2703         if (r_irq_statech)
2704                 iq1++;
2705         if (status & V_DTMF_STA)
2706                 iq2++;
2707         if (status & V_LOST_STA)
2708                 iq3++;
2709         if (status & V_EXT_IRQSTA)
2710                 iq4++;
2711         if (status & V_MISC_IRQSTA)
2712                 iq5++;
2713         if (status & V_FR_IRQSTA)
2714                 iq6++;
2715         if (iqcnt++ > 5000) {
2716                 printk(KERN_ERR "iq1:%x iq2:%x iq3:%x iq4:%x iq5:%x iq6:%x\n",
2717                     iq1, iq2, iq3, iq4, iq5, iq6);
2718                 iqcnt = 0;
2719         }
2720 #endif
2721
2722         if (!r_irq_statech &&
2723             !(status & (V_DTMF_STA | V_LOST_STA | V_EXT_IRQSTA |
2724             V_MISC_IRQSTA | V_FR_IRQSTA))) {
2725                 /* irq is not for us */
2726                 goto irq_notforus;
2727         }
2728         hc->irqcnt++;
2729         if (r_irq_statech) {
2730                 if (hc->ctype != HFC_TYPE_E1)
2731                         ph_state_irq(hc, r_irq_statech);
2732         }
2733         if (status & V_EXT_IRQSTA)
2734                 ; /* external IRQ */
2735         if (status & V_LOST_STA) {
2736                 /* LOST IRQ */
2737                 HFC_outb(hc, R_INC_RES_FIFO, V_RES_LOST); /* clear irq! */
2738         }
2739         if (status & V_MISC_IRQSTA) {
2740                 /* misc IRQ */
2741                 r_irq_misc = HFC_inb_nodebug(hc, R_IRQ_MISC);
2742                 r_irq_misc &= hc->hw.r_irqmsk_misc; /* ignore disabled irqs */
2743                 if (r_irq_misc & V_STA_IRQ) {
2744                         if (hc->ctype == HFC_TYPE_E1) {
2745                                 /* state machine */
2746                                 dch = hc->chan[hc->dslot].dch;
2747                                 e1_syncsta = HFC_inb_nodebug(hc, R_SYNC_STA);
2748                                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)
2749                                  && hc->e1_getclock) {
2750                                         if (e1_syncsta & V_FR_SYNC_E1)
2751                                                 hc->syncronized = 1;
2752                                         else
2753                                                 hc->syncronized = 0;
2754                                 }
2755                                 /* undocumented: status changes during read */
2756                                 dch->state = HFC_inb_nodebug(hc, R_E1_RD_STA);
2757                                 while (dch->state != (temp =
2758                                         HFC_inb_nodebug(hc, R_E1_RD_STA))) {
2759                                         if (debug & DEBUG_HFCMULTI_STATE)
2760                                                 printk(KERN_DEBUG "%s: reread "
2761                                                     "STATE because %d!=%d\n",
2762                                                     __func__, temp,
2763                                                     dch->state);
2764                                         dch->state = temp; /* repeat */
2765                                 }
2766                                 dch->state = HFC_inb_nodebug(hc, R_E1_RD_STA)
2767                                         & 0x7;
2768                                 schedule_event(dch, FLG_PHCHANGE);
2769                                 if (debug & DEBUG_HFCMULTI_STATE)
2770                                         printk(KERN_DEBUG
2771                                             "%s: E1 (id=%d) newstate %x\n",
2772                                             __func__, hc->id, dch->state);
2773                                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip))
2774                                         plxsd_checksync(hc, 0);
2775                         }
2776                 }
2777                 if (r_irq_misc & V_TI_IRQ) {
2778                         if (hc->iclock_on)
2779                                 mISDN_clock_update(hc->iclock, poll, NULL);
2780                         handle_timer_irq(hc);
2781                 }
2782
2783                 if (r_irq_misc & V_DTMF_IRQ) {
2784                         hfcmulti_dtmf(hc);
2785                 }
2786                 if (r_irq_misc & V_IRQ_PROC) {
2787                         static int irq_proc_cnt;
2788                         if (!irq_proc_cnt++)
2789                                 printk(KERN_WARNING "%s: got V_IRQ_PROC -"
2790                                     " this should not happen\n", __func__);
2791                 }
2792
2793         }
2794         if (status & V_FR_IRQSTA) {
2795                 /* FIFO IRQ */
2796                 r_irq_oview = HFC_inb_nodebug(hc, R_IRQ_OVIEW);
2797                 for (i = 0; i < 8; i++) {
2798                         if (r_irq_oview & (1 << i))
2799                                 fifo_irq(hc, i);
2800                 }
2801         }
2802
2803 #ifdef IRQ_DEBUG
2804         irqsem = 0;
2805 #endif
2806         spin_unlock(&hc->lock);
2807         return IRQ_HANDLED;
2808
2809 irq_notforus:
2810 #ifdef IRQ_DEBUG
2811         irqsem = 0;
2812 #endif
2813         spin_unlock(&hc->lock);
2814         return IRQ_NONE;
2815 }
2816
2817
2818 /*
2819  * timer callback for D-chan busy resolution. Currently no function
2820  */
2821
2822 static void
2823 hfcmulti_dbusy_timer(struct hfc_multi *hc)
2824 {
2825 }
2826
2827
2828 /*
2829  * activate/deactivate hardware for selected channels and mode
2830  *
2831  * configure B-channel with the given protocol
2832  * ch eqals to the HFC-channel (0-31)
2833  * ch is the number of channel (0-4,4-7,8-11,12-15,16-19,20-23,24-27,28-31
2834  * for S/T, 1-31 for E1)
2835  * the hdlc interrupts will be set/unset
2836  */
2837 static int
2838 mode_hfcmulti(struct hfc_multi *hc, int ch, int protocol, int slot_tx,
2839     int bank_tx, int slot_rx, int bank_rx)
2840 {
2841         int flow_tx = 0, flow_rx = 0, routing = 0;
2842         int oslot_tx, oslot_rx;
2843         int conf;
2844
2845         if (ch < 0 || ch > 31)
2846                 return EINVAL;
2847         oslot_tx = hc->chan[ch].slot_tx;
2848         oslot_rx = hc->chan[ch].slot_rx;
2849         conf = hc->chan[ch].conf;
2850
2851         if (debug & DEBUG_HFCMULTI_MODE)
2852                 printk(KERN_DEBUG
2853                     "%s: card %d channel %d protocol %x slot old=%d new=%d "
2854                     "bank new=%d (TX) slot old=%d new=%d bank new=%d (RX)\n",
2855                     __func__, hc->id, ch, protocol, oslot_tx, slot_tx,
2856                     bank_tx, oslot_rx, slot_rx, bank_rx);
2857
2858         if (oslot_tx >= 0 && slot_tx != oslot_tx) {
2859                 /* remove from slot */
2860                 if (debug & DEBUG_HFCMULTI_MODE)
2861                         printk(KERN_DEBUG "%s: remove from slot %d (TX)\n",
2862                             __func__, oslot_tx);
2863                 if (hc->slot_owner[oslot_tx<<1] == ch) {
2864                         HFC_outb(hc, R_SLOT, oslot_tx << 1);
2865                         HFC_outb(hc, A_SL_CFG, 0);
2866                         if (hc->ctype != HFC_TYPE_XHFC)
2867                                 HFC_outb(hc, A_CONF, 0);
2868                         hc->slot_owner[oslot_tx<<1] = -1;
2869                 } else {
2870                         if (debug & DEBUG_HFCMULTI_MODE)
2871                                 printk(KERN_DEBUG
2872                                     "%s: we are not owner of this tx slot "
2873                                     "anymore, channel %d is.\n",
2874                                     __func__, hc->slot_owner[oslot_tx<<1]);
2875                 }
2876         }
2877
2878         if (oslot_rx >= 0 && slot_rx != oslot_rx) {
2879                 /* remove from slot */
2880                 if (debug & DEBUG_HFCMULTI_MODE)
2881                         printk(KERN_DEBUG
2882                             "%s: remove from slot %d (RX)\n",
2883                             __func__, oslot_rx);
2884                 if (hc->slot_owner[(oslot_rx << 1) | 1] == ch) {
2885                         HFC_outb(hc, R_SLOT, (oslot_rx << 1) | V_SL_DIR);
2886                         HFC_outb(hc, A_SL_CFG, 0);
2887                         hc->slot_owner[(oslot_rx << 1) | 1] = -1;
2888                 } else {
2889                         if (debug & DEBUG_HFCMULTI_MODE)
2890                                 printk(KERN_DEBUG
2891                                     "%s: we are not owner of this rx slot "
2892                                     "anymore, channel %d is.\n",
2893                                     __func__,
2894                                     hc->slot_owner[(oslot_rx << 1) | 1]);
2895                 }
2896         }
2897
2898         if (slot_tx < 0) {
2899                 flow_tx = 0x80; /* FIFO->ST */
2900                 /* disable pcm slot */
2901                 hc->chan[ch].slot_tx = -1;
2902                 hc->chan[ch].bank_tx = 0;
2903         } else {
2904                 /* set pcm slot */
2905                 if (hc->chan[ch].txpending)
2906                         flow_tx = 0x80; /* FIFO->ST */
2907                 else
2908                         flow_tx = 0xc0; /* PCM->ST */
2909                 /* put on slot */
2910                 routing = bank_tx ? 0xc0 : 0x80;
2911                 if (conf >= 0 || bank_tx > 1)
2912                         routing = 0x40; /* loop */
2913                 if (debug & DEBUG_HFCMULTI_MODE)
2914                         printk(KERN_DEBUG "%s: put channel %d to slot %d bank"
2915                             " %d flow %02x routing %02x conf %d (TX)\n",
2916                             __func__, ch, slot_tx, bank_tx,
2917                             flow_tx, routing, conf);
2918                 HFC_outb(hc, R_SLOT, slot_tx << 1);
2919                 HFC_outb(hc, A_SL_CFG, (ch<<1) | routing);
2920                 if (hc->ctype != HFC_TYPE_XHFC)
2921                         HFC_outb(hc, A_CONF,
2922                                 (conf < 0) ? 0 : (conf | V_CONF_SL));
2923                 hc->slot_owner[slot_tx << 1] = ch;
2924                 hc->chan[ch].slot_tx = slot_tx;
2925                 hc->chan[ch].bank_tx = bank_tx;
2926         }
2927         if (slot_rx < 0) {
2928                 /* disable pcm slot */
2929                 flow_rx = 0x80; /* ST->FIFO */
2930                 hc->chan[ch].slot_rx = -1;
2931                 hc->chan[ch].bank_rx = 0;
2932         } else {
2933                 /* set pcm slot */
2934                 if (hc->chan[ch].txpending)
2935                         flow_rx = 0x80; /* ST->FIFO */
2936                 else
2937                         flow_rx = 0xc0; /* ST->(FIFO,PCM) */
2938                 /* put on slot */
2939                 routing = bank_rx?0x80:0xc0; /* reversed */
2940                 if (conf >= 0 || bank_rx > 1)
2941                         routing = 0x40; /* loop */
2942                 if (debug & DEBUG_HFCMULTI_MODE)
2943                         printk(KERN_DEBUG "%s: put channel %d to slot %d bank"
2944                             " %d flow %02x routing %02x conf %d (RX)\n",
2945                             __func__, ch, slot_rx, bank_rx,
2946                             flow_rx, routing, conf);
2947                 HFC_outb(hc, R_SLOT, (slot_rx<<1) | V_SL_DIR);
2948                 HFC_outb(hc, A_SL_CFG, (ch<<1) | V_CH_DIR | routing);
2949                 hc->slot_owner[(slot_rx<<1)|1] = ch;
2950                 hc->chan[ch].slot_rx = slot_rx;
2951                 hc->chan[ch].bank_rx = bank_rx;
2952         }
2953
2954         switch (protocol) {
2955         case (ISDN_P_NONE):
2956                 /* disable TX fifo */
2957                 HFC_outb(hc, R_FIFO, ch << 1);
2958                 HFC_wait(hc);
2959                 HFC_outb(hc, A_CON_HDLC, flow_tx | 0x00 | V_IFF);
2960                 HFC_outb(hc, A_SUBCH_CFG, 0);
2961                 HFC_outb(hc, A_IRQ_MSK, 0);
2962                 HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
2963                 HFC_wait(hc);
2964                 /* disable RX fifo */
2965                 HFC_outb(hc, R_FIFO, (ch<<1)|1);
2966                 HFC_wait(hc);
2967                 HFC_outb(hc, A_CON_HDLC, flow_rx | 0x00);
2968                 HFC_outb(hc, A_SUBCH_CFG, 0);
2969                 HFC_outb(hc, A_IRQ_MSK, 0);
2970                 HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
2971                 HFC_wait(hc);
2972                 if (hc->chan[ch].bch && hc->ctype != HFC_TYPE_E1) {
2973                         hc->hw.a_st_ctrl0[hc->chan[ch].port] &=
2974                             ((ch & 0x3) == 0)? ~V_B1_EN: ~V_B2_EN;
2975                         HFC_outb(hc, R_ST_SEL, hc->chan[ch].port);
2976                         /* undocumented: delay after R_ST_SEL */
2977                         udelay(1);
2978                         HFC_outb(hc, A_ST_CTRL0,
2979                             hc->hw.a_st_ctrl0[hc->chan[ch].port]);
2980                 }
2981                 if (hc->chan[ch].bch) {
2982                         test_and_clear_bit(FLG_HDLC, &hc->chan[ch].bch->Flags);
2983                         test_and_clear_bit(FLG_TRANSPARENT,
2984                             &hc->chan[ch].bch->Flags);
2985                 }
2986                 break;
2987         case (ISDN_P_B_RAW): /* B-channel */
2988
2989                 if (test_bit(HFC_CHIP_B410P, &hc->chip) &&
2990                     (hc->chan[ch].slot_rx < 0) &&
2991                     (hc->chan[ch].slot_tx < 0)) {
2992
2993                         printk(KERN_DEBUG
2994                             "Setting B-channel %d to echo cancelable "
2995                             "state on PCM slot %d\n", ch,
2996                             ((ch / 4) * 8) + ((ch % 4) * 4) + 1);
2997                         printk(KERN_DEBUG
2998                             "Enabling pass through for channel\n");
2999                         vpm_out(hc, ch, ((ch / 4) * 8) +
3000                             ((ch % 4) * 4) + 1, 0x01);
3001                         /* rx path */
3002                         /* S/T -> PCM */
3003                         HFC_outb(hc, R_FIFO, (ch << 1));
3004                         HFC_wait(hc);
3005                         HFC_outb(hc, A_CON_HDLC, 0xc0 | V_HDLC_TRP | V_IFF);
3006                         HFC_outb(hc, R_SLOT, (((ch / 4) * 8) +
3007                             ((ch % 4) * 4) + 1) << 1);
3008                         HFC_outb(hc, A_SL_CFG, 0x80 | (ch << 1));
3009
3010                         /* PCM -> FIFO */
3011                         HFC_outb(hc, R_FIFO, 0x20 | (ch << 1) | 1);
3012                         HFC_wait(hc);
3013                         HFC_outb(hc, A_CON_HDLC, 0x20 | V_HDLC_TRP | V_IFF);
3014                         HFC_outb(hc, A_SUBCH_CFG, 0);
3015                         HFC_outb(hc, A_IRQ_MSK, 0);
3016                         HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3017                         HFC_wait(hc);
3018                         HFC_outb(hc, R_SLOT, ((((ch / 4) * 8) +
3019                             ((ch % 4) * 4) + 1) << 1) | 1);
3020                         HFC_outb(hc, A_SL_CFG, 0x80 | 0x20 | (ch << 1) | 1);
3021
3022                         /* tx path */
3023                         /* PCM -> S/T */
3024                         HFC_outb(hc, R_FIFO, (ch << 1) | 1);
3025                         HFC_wait(hc);
3026                         HFC_outb(hc, A_CON_HDLC, 0xc0 | V_HDLC_TRP | V_IFF);
3027                         HFC_outb(hc, R_SLOT, ((((ch / 4) * 8) +
3028                             ((ch % 4) * 4)) << 1) | 1);
3029                         HFC_outb(hc, A_SL_CFG, 0x80 | 0x40 | (ch << 1) | 1);
3030
3031                         /* FIFO -> PCM */
3032                         HFC_outb(hc, R_FIFO, 0x20 | (ch << 1));
3033                         HFC_wait(hc);
3034                         HFC_outb(hc, A_CON_HDLC, 0x20 | V_HDLC_TRP | V_IFF);
3035                         HFC_outb(hc, A_SUBCH_CFG, 0);
3036                         HFC_outb(hc, A_IRQ_MSK, 0);
3037                         HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3038                         HFC_wait(hc);
3039                         /* tx silence */
3040                         HFC_outb_nodebug(hc, A_FIFO_DATA0_NOINC, hc->silence);
3041                         HFC_outb(hc, R_SLOT, (((ch / 4) * 8) +
3042                             ((ch % 4) * 4)) << 1);
3043                         HFC_outb(hc, A_SL_CFG, 0x80 | 0x20 | (ch << 1));
3044                 } else {
3045                         /* enable TX fifo */
3046                         HFC_outb(hc, R_FIFO, ch << 1);
3047                         HFC_wait(hc);
3048                         if (hc->ctype == HFC_TYPE_XHFC)
3049                                 HFC_outb(hc, A_CON_HDLC, flow_tx | 0x07 << 2 |
3050                                         V_HDLC_TRP | V_IFF);
3051                                         /* Enable FIFO, no interrupt */
3052                         else
3053                                 HFC_outb(hc, A_CON_HDLC, flow_tx | 0x00 |
3054                                         V_HDLC_TRP | V_IFF);
3055                         HFC_outb(hc, A_SUBCH_CFG, 0);
3056                         HFC_outb(hc, A_IRQ_MSK, 0);
3057                         HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3058                         HFC_wait(hc);
3059                         /* tx silence */
3060                         HFC_outb_nodebug(hc, A_FIFO_DATA0_NOINC, hc->silence);
3061                         /* enable RX fifo */
3062                         HFC_outb(hc, R_FIFO, (ch<<1)|1);
3063                         HFC_wait(hc);
3064                         if (hc->ctype == HFC_TYPE_XHFC)
3065                                 HFC_outb(hc, A_CON_HDLC, flow_rx | 0x07 << 2 |
3066                                         V_HDLC_TRP);
3067                                         /* Enable FIFO, no interrupt*/
3068                         else
3069                                 HFC_outb(hc, A_CON_HDLC, flow_rx | 0x00 |
3070                                                 V_HDLC_TRP);
3071                         HFC_outb(hc, A_SUBCH_CFG, 0);
3072                         HFC_outb(hc, A_IRQ_MSK, 0);
3073                         HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3074                         HFC_wait(hc);
3075                 }
3076                 if (hc->ctype != HFC_TYPE_E1) {
3077                         hc->hw.a_st_ctrl0[hc->chan[ch].port] |=
3078                             ((ch & 0x3) == 0) ? V_B1_EN : V_B2_EN;
3079                         HFC_outb(hc, R_ST_SEL, hc->chan[ch].port);
3080                         /* undocumented: delay after R_ST_SEL */
3081                         udelay(1);
3082                         HFC_outb(hc, A_ST_CTRL0,
3083                             hc->hw.a_st_ctrl0[hc->chan[ch].port]);
3084                 }
3085                 if (hc->chan[ch].bch)
3086                         test_and_set_bit(FLG_TRANSPARENT,
3087                             &hc->chan[ch].bch->Flags);
3088                 break;
3089         case (ISDN_P_B_HDLC): /* B-channel */
3090         case (ISDN_P_TE_S0): /* D-channel */
3091         case (ISDN_P_NT_S0):
3092         case (ISDN_P_TE_E1):
3093         case (ISDN_P_NT_E1):
3094                 /* enable TX fifo */
3095                 HFC_outb(hc, R_FIFO, ch<<1);
3096                 HFC_wait(hc);
3097                 if (hc->ctype == HFC_TYPE_E1 || hc->chan[ch].bch) {
3098                         /* E1 or B-channel */
3099                         HFC_outb(hc, A_CON_HDLC, flow_tx | 0x04);
3100                         HFC_outb(hc, A_SUBCH_CFG, 0);
3101                 } else {
3102                         /* D-Channel without HDLC fill flags */
3103                         HFC_outb(hc, A_CON_HDLC, flow_tx | 0x04 | V_IFF);
3104                         HFC_outb(hc, A_SUBCH_CFG, 2);
3105                 }
3106                 HFC_outb(hc, A_IRQ_MSK, V_IRQ);
3107                 HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3108                 HFC_wait(hc);
3109                 /* enable RX fifo */
3110                 HFC_outb(hc, R_FIFO, (ch<<1)|1);
3111                 HFC_wait(hc);
3112                 HFC_outb(hc, A_CON_HDLC, flow_rx | 0x04);
3113                 if (hc->ctype == HFC_TYPE_E1 || hc->chan[ch].bch)
3114                         HFC_outb(hc, A_SUBCH_CFG, 0); /* full 8 bits */
3115                 else
3116                         HFC_outb(hc, A_SUBCH_CFG, 2); /* 2 bits dchannel */
3117                 HFC_outb(hc, A_IRQ_MSK, V_IRQ);
3118                 HFC_outb(hc, R_INC_RES_FIFO, V_RES_F);
3119                 HFC_wait(hc);
3120                 if (hc->chan[ch].bch) {
3121                         test_and_set_bit(FLG_HDLC, &hc->chan[ch].bch->Flags);
3122                         if (hc->ctype != HFC_TYPE_E1) {
3123                                 hc->hw.a_st_ctrl0[hc->chan[ch].port] |=
3124                                   ((ch&0x3) == 0) ? V_B1_EN : V_B2_EN;
3125                                 HFC_outb(hc, R_ST_SEL, hc->chan[ch].port);
3126                                 /* undocumented: delay after R_ST_SEL */
3127                                 udelay(1);
3128                                 HFC_outb(hc, A_ST_CTRL0,
3129                                   hc->hw.a_st_ctrl0[hc->chan[ch].port]);
3130                         }
3131                 }
3132                 break;
3133         default:
3134                 printk(KERN_DEBUG "%s: protocol not known %x\n",
3135                     __func__, protocol);
3136                 hc->chan[ch].protocol = ISDN_P_NONE;
3137                 return -ENOPROTOOPT;
3138         }
3139         hc->chan[ch].protocol = protocol;
3140         return 0;
3141 }
3142
3143
3144 /*
3145  * connect/disconnect PCM
3146  */
3147
3148 static void
3149 hfcmulti_pcm(struct hfc_multi *hc, int ch, int slot_tx, int bank_tx,
3150     int slot_rx, int bank_rx)
3151 {
3152         if (slot_rx < 0 || slot_rx < 0 || bank_tx < 0 || bank_rx < 0) {
3153                 /* disable PCM */
3154                 mode_hfcmulti(hc, ch, hc->chan[ch].protocol, -1, 0, -1, 0);
3155                 return;
3156         }
3157
3158         /* enable pcm */
3159         mode_hfcmulti(hc, ch, hc->chan[ch].protocol, slot_tx, bank_tx,
3160                 slot_rx, bank_rx);
3161 }
3162
3163 /*
3164  * set/disable conference
3165  */
3166
3167 static void
3168 hfcmulti_conf(struct hfc_multi *hc, int ch, int num)
3169 {
3170         if (num >= 0 && num <= 7)
3171                 hc->chan[ch].conf = num;
3172         else
3173                 hc->chan[ch].conf = -1;
3174         mode_hfcmulti(hc, ch, hc->chan[ch].protocol, hc->chan[ch].slot_tx,
3175             hc->chan[ch].bank_tx, hc->chan[ch].slot_rx,
3176             hc->chan[ch].bank_rx);
3177 }
3178
3179
3180 /*
3181  * set/disable sample loop
3182  */
3183
3184 /* NOTE: this function is experimental and therefore disabled */
3185
3186 /*
3187  * Layer 1 callback function
3188  */
3189 static int
3190 hfcm_l1callback(struct dchannel *dch, u_int cmd)
3191 {
3192         struct hfc_multi        *hc = dch->hw;
3193         u_long  flags;
3194
3195         switch (cmd) {
3196         case INFO3_P8:
3197         case INFO3_P10:
3198                 break;
3199         case HW_RESET_REQ:
3200                 /* start activation */
3201                 spin_lock_irqsave(&hc->lock, flags);
3202                 if (hc->ctype == HFC_TYPE_E1) {
3203                         if (debug & DEBUG_HFCMULTI_MSG)
3204                                 printk(KERN_DEBUG
3205                                     "%s: HW_RESET_REQ no BRI\n",
3206                                     __func__);
3207                 } else {
3208                         HFC_outb(hc, R_ST_SEL, hc->chan[dch->slot].port);
3209                         /* undocumented: delay after R_ST_SEL */
3210                         udelay(1);
3211                         HFC_outb(hc, A_ST_WR_STATE, V_ST_LD_STA | 3); /* F3 */
3212                         udelay(6); /* wait at least 5,21us */
3213                         HFC_outb(hc, A_ST_WR_STATE, 3);
3214                         HFC_outb(hc, A_ST_WR_STATE, 3 | (V_ST_ACT*3));
3215                                 /* activate */
3216                 }
3217                 spin_unlock_irqrestore(&hc->lock, flags);
3218                 l1_event(dch->l1, HW_POWERUP_IND);
3219                 break;
3220         case HW_DEACT_REQ:
3221                 /* start deactivation */
3222                 spin_lock_irqsave(&hc->lock, flags);
3223                 if (hc->ctype == HFC_TYPE_E1) {
3224                         if (debug & DEBUG_HFCMULTI_MSG)
3225                                 printk(KERN_DEBUG
3226                                     "%s: HW_DEACT_REQ no BRI\n",
3227                                     __func__);
3228                 } else {
3229                         HFC_outb(hc, R_ST_SEL, hc->chan[dch->slot].port);
3230                         /* undocumented: delay after R_ST_SEL */
3231                         udelay(1);
3232                         HFC_outb(hc, A_ST_WR_STATE, V_ST_ACT*2);
3233                                 /* deactivate */
3234                         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
3235                                 hc->syncronized &=
3236                                    ~(1 << hc->chan[dch->slot].port);
3237                                 plxsd_checksync(hc, 0);
3238                         }
3239                 }
3240                 skb_queue_purge(&dch->squeue);
3241                 if (dch->tx_skb) {
3242                         dev_kfree_skb(dch->tx_skb);
3243                         dch->tx_skb = NULL;
3244                 }
3245                 dch->tx_idx = 0;
3246                 if (dch->rx_skb) {
3247                         dev_kfree_skb(dch->rx_skb);
3248                         dch->rx_skb = NULL;
3249                 }
3250                 test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
3251                 if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
3252                         del_timer(&dch->timer);
3253                 spin_unlock_irqrestore(&hc->lock, flags);
3254                 break;
3255         case HW_POWERUP_REQ:
3256                 spin_lock_irqsave(&hc->lock, flags);
3257                 if (hc->ctype == HFC_TYPE_E1) {
3258                         if (debug & DEBUG_HFCMULTI_MSG)
3259                                 printk(KERN_DEBUG
3260                                     "%s: HW_POWERUP_REQ no BRI\n",
3261                                     __func__);
3262                 } else {
3263                         HFC_outb(hc, R_ST_SEL, hc->chan[dch->slot].port);
3264                         /* undocumented: delay after R_ST_SEL */
3265                         udelay(1);
3266                         HFC_outb(hc, A_ST_WR_STATE, 3 | 0x10); /* activate */
3267                         udelay(6); /* wait at least 5,21us */
3268                         HFC_outb(hc, A_ST_WR_STATE, 3); /* activate */
3269                 }
3270                 spin_unlock_irqrestore(&hc->lock, flags);
3271                 break;
3272         case PH_ACTIVATE_IND:
3273                 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
3274                 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
3275                         GFP_ATOMIC);
3276                 break;
3277         case PH_DEACTIVATE_IND:
3278                 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
3279                 _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
3280                         GFP_ATOMIC);
3281                 break;
3282         default:
3283                 if (dch->debug & DEBUG_HW)
3284                         printk(KERN_DEBUG "%s: unknown command %x\n",
3285                             __func__, cmd);
3286                 return -1;
3287         }
3288         return 0;
3289 }
3290
3291 /*
3292  * Layer2 -> Layer 1 Transfer
3293  */
3294
3295 static int
3296 handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
3297 {
3298         struct mISDNdevice      *dev = container_of(ch, struct mISDNdevice, D);
3299         struct dchannel         *dch = container_of(dev, struct dchannel, dev);
3300         struct hfc_multi        *hc = dch->hw;
3301         struct mISDNhead        *hh = mISDN_HEAD_P(skb);
3302         int                     ret = -EINVAL;
3303         unsigned int            id;
3304         u_long                  flags;
3305
3306         switch (hh->prim) {
3307         case PH_DATA_REQ:
3308                 if (skb->len < 1)
3309                         break;
3310                 spin_lock_irqsave(&hc->lock, flags);
3311                 ret = dchannel_senddata(dch, skb);
3312                 if (ret > 0) { /* direct TX */
3313                         id = hh->id; /* skb can be freed */
3314                         hfcmulti_tx(hc, dch->slot);
3315                         ret = 0;
3316                         /* start fifo */
3317                         HFC_outb(hc, R_FIFO, 0);
3318                         HFC_wait(hc);
3319                         spin_unlock_irqrestore(&hc->lock, flags);
3320                         queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
3321                 } else
3322                         spin_unlock_irqrestore(&hc->lock, flags);
3323                 return ret;
3324         case PH_ACTIVATE_REQ:
3325                 if (dch->dev.D.protocol != ISDN_P_TE_S0) {
3326                         spin_lock_irqsave(&hc->lock, flags);
3327                         ret = 0;
3328                         if (debug & DEBUG_HFCMULTI_MSG)
3329                                 printk(KERN_DEBUG
3330                                     "%s: PH_ACTIVATE port %d (0..%d)\n",
3331                                     __func__, hc->chan[dch->slot].port,
3332                                     hc->ports-1);
3333                         /* start activation */
3334                         if (hc->ctype == HFC_TYPE_E1) {
3335                                 ph_state_change(dch);
3336                                 if (debug & DEBUG_HFCMULTI_STATE)
3337                                         printk(KERN_DEBUG
3338                                             "%s: E1 report state %x \n",
3339                                             __func__, dch->state);
3340                         } else {
3341                                 HFC_outb(hc, R_ST_SEL,
3342                                     hc->chan[dch->slot].port);
3343                                 /* undocumented: delay after R_ST_SEL */
3344                                 udelay(1);
3345                                 HFC_outb(hc, A_ST_WR_STATE, V_ST_LD_STA | 1);
3346                                     /* G1 */
3347                                 udelay(6); /* wait at least 5,21us */
3348                                 HFC_outb(hc, A_ST_WR_STATE, 1);
3349                                 HFC_outb(hc, A_ST_WR_STATE, 1 |
3350                                     (V_ST_ACT*3)); /* activate */
3351                                 dch->state = 1;
3352                         }
3353                         spin_unlock_irqrestore(&hc->lock, flags);
3354                 } else
3355                         ret = l1_event(dch->l1, hh->prim);
3356                 break;
3357         case PH_DEACTIVATE_REQ:
3358                 test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
3359                 if (dch->dev.D.protocol != ISDN_P_TE_S0) {
3360                         spin_lock_irqsave(&hc->lock, flags);
3361                         if (debug & DEBUG_HFCMULTI_MSG)
3362                                 printk(KERN_DEBUG
3363                                     "%s: PH_DEACTIVATE port %d (0..%d)\n",
3364                                     __func__, hc->chan[dch->slot].port,
3365                                     hc->ports-1);
3366                         /* start deactivation */
3367                         if (hc->ctype == HFC_TYPE_E1) {
3368                                 if (debug & DEBUG_HFCMULTI_MSG)
3369                                         printk(KERN_DEBUG
3370                                             "%s: PH_DEACTIVATE no BRI\n",
3371                                             __func__);
3372                         } else {
3373                                 HFC_outb(hc, R_ST_SEL,
3374                                     hc->chan[dch->slot].port);
3375                                 /* undocumented: delay after R_ST_SEL */
3376                                 udelay(1);
3377                                 HFC_outb(hc, A_ST_WR_STATE, V_ST_ACT * 2);
3378                                     /* deactivate */
3379                                 dch->state = 1;
3380                         }
3381                         skb_queue_purge(&dch->squeue);
3382                         if (dch->tx_skb) {
3383                                 dev_kfree_skb(dch->tx_skb);
3384                                 dch->tx_skb = NULL;
3385                         }
3386                         dch->tx_idx = 0;
3387                         if (dch->rx_skb) {
3388                                 dev_kfree_skb(dch->rx_skb);
3389                                 dch->rx_skb = NULL;
3390                         }
3391                         test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
3392                         if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
3393                                 del_timer(&dch->timer);
3394 #ifdef FIXME
3395                         if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
3396                                 dchannel_sched_event(&hc->dch, D_CLEARBUSY);
3397 #endif
3398                         ret = 0;
3399                         spin_unlock_irqrestore(&hc->lock, flags);
3400                 } else
3401                         ret = l1_event(dch->l1, hh->prim);
3402                 break;
3403         }
3404         if (!ret)
3405                 dev_kfree_skb(skb);
3406         return ret;
3407 }
3408
3409 static void
3410 deactivate_bchannel(struct bchannel *bch)
3411 {
3412         struct hfc_multi        *hc = bch->hw;
3413         u_long                  flags;
3414
3415         spin_lock_irqsave(&hc->lock, flags);
3416         if (test_and_clear_bit(FLG_TX_NEXT, &bch->Flags)) {
3417                 dev_kfree_skb(bch->next_skb);
3418                 bch->next_skb = NULL;
3419         }
3420         if (bch->tx_skb) {
3421                 dev_kfree_skb(bch->tx_skb);
3422                 bch->tx_skb = NULL;
3423         }
3424         bch->tx_idx = 0;
3425         if (bch->rx_skb) {
3426                 dev_kfree_skb(bch->rx_skb);
3427                 bch->rx_skb = NULL;
3428         }
3429         hc->chan[bch->slot].coeff_count = 0;
3430         test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
3431         test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
3432         hc->chan[bch->slot].rx_off = 0;
3433         hc->chan[bch->slot].conf = -1;
3434         mode_hfcmulti(hc, bch->slot, ISDN_P_NONE, -1, 0, -1, 0);
3435         spin_unlock_irqrestore(&hc->lock, flags);
3436 }
3437
3438 static int
3439 handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
3440 {
3441         struct bchannel         *bch = container_of(ch, struct bchannel, ch);
3442         struct hfc_multi        *hc = bch->hw;
3443         int                     ret = -EINVAL;
3444         struct mISDNhead        *hh = mISDN_HEAD_P(skb);
3445         unsigned int            id;
3446         u_long                  flags;
3447
3448         switch (hh->prim) {
3449         case PH_DATA_REQ:
3450                 if (!skb->len)
3451                         break;
3452                 spin_lock_irqsave(&hc->lock, flags);
3453                 ret = bchannel_senddata(bch, skb);
3454                 if (ret > 0) { /* direct TX */
3455                         id = hh->id; /* skb can be freed */
3456                         hfcmulti_tx(hc, bch->slot);
3457                         ret = 0;
3458                         /* start fifo */
3459                         HFC_outb_nodebug(hc, R_FIFO, 0);
3460                         HFC_wait_nodebug(hc);
3461                         if (!test_bit(FLG_TRANSPARENT, &bch->Flags)) {
3462                                 spin_unlock_irqrestore(&hc->lock, flags);
3463                                 queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
3464                         } else
3465                                 spin_unlock_irqrestore(&hc->lock, flags);
3466                 } else
3467                         spin_unlock_irqrestore(&hc->lock, flags);
3468                 return ret;
3469         case PH_ACTIVATE_REQ:
3470                 if (debug & DEBUG_HFCMULTI_MSG)
3471                         printk(KERN_DEBUG "%s: PH_ACTIVATE ch %d (0..32)\n",
3472                                 __func__, bch->slot);
3473                 spin_lock_irqsave(&hc->lock, flags);
3474                 /* activate B-channel if not already activated */
3475                 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
3476                         hc->chan[bch->slot].txpending = 0;
3477                         ret = mode_hfcmulti(hc, bch->slot,
3478                                 ch->protocol,
3479                                 hc->chan[bch->slot].slot_tx,
3480                                 hc->chan[bch->slot].bank_tx,
3481                                 hc->chan[bch->slot].slot_rx,
3482                                 hc->chan[bch->slot].bank_rx);
3483                         if (!ret) {
3484                                 if (ch->protocol == ISDN_P_B_RAW && !hc->dtmf
3485                                         && test_bit(HFC_CHIP_DTMF, &hc->chip)) {
3486                                         /* start decoder */
3487                                         hc->dtmf = 1;
3488                                         if (debug & DEBUG_HFCMULTI_DTMF)
3489                                                 printk(KERN_DEBUG
3490                                                     "%s: start dtmf decoder\n",
3491                                                         __func__);
3492                                         HFC_outb(hc, R_DTMF, hc->hw.r_dtmf |
3493                                             V_RST_DTMF);
3494                                 }
3495                         }
3496                 } else
3497                         ret = 0;
3498                 spin_unlock_irqrestore(&hc->lock, flags);
3499                 if (!ret)
3500                         _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0, NULL,
3501                                 GFP_KERNEL);
3502                 break;
3503         case PH_CONTROL_REQ:
3504                 spin_lock_irqsave(&hc->lock, flags);
3505                 switch (hh->id) {
3506                 case HFC_SPL_LOOP_ON: /* set sample loop */
3507                         if (debug & DEBUG_HFCMULTI_MSG)
3508                         printk(KERN_DEBUG
3509                             "%s: HFC_SPL_LOOP_ON (len = %d)\n",
3510                             __func__, skb->len);
3511                         ret = 0;
3512                         break;
3513                 case HFC_SPL_LOOP_OFF: /* set silence */
3514                         if (debug & DEBUG_HFCMULTI_MSG)
3515                                 printk(KERN_DEBUG "%s: HFC_SPL_LOOP_OFF\n",
3516                                     __func__);
3517                         ret = 0;
3518                         break;
3519                 default:
3520                         printk(KERN_ERR
3521                              "%s: unknown PH_CONTROL_REQ info %x\n",
3522                              __func__, hh->id);
3523                         ret = -EINVAL;
3524                 }
3525                 spin_unlock_irqrestore(&hc->lock, flags);
3526                 break;
3527         case PH_DEACTIVATE_REQ:
3528                 deactivate_bchannel(bch); /* locked there */
3529                 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0, NULL,
3530                         GFP_KERNEL);
3531                 ret = 0;
3532                 break;
3533         }
3534         if (!ret)
3535                 dev_kfree_skb(skb);
3536         return ret;
3537 }
3538
3539 /*
3540  * bchannel control function
3541  */
3542 static int
3543 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
3544 {
3545         int                     ret = 0;
3546         struct dsp_features     *features =
3547                 (struct dsp_features *)(*((u_long *)&cq->p1));
3548         struct hfc_multi        *hc = bch->hw;
3549         int                     slot_tx;
3550         int                     bank_tx;
3551         int                     slot_rx;
3552         int                     bank_rx;
3553         int                     num;
3554
3555         switch (cq->op) {
3556         case MISDN_CTRL_GETOP:
3557                 cq->op = MISDN_CTRL_HFC_OP | MISDN_CTRL_HW_FEATURES_OP
3558                         | MISDN_CTRL_RX_OFF | MISDN_CTRL_FILL_EMPTY;
3559                 break;
3560         case MISDN_CTRL_RX_OFF: /* turn off / on rx stream */
3561                 hc->chan[bch->slot].rx_off = !!cq->p1;
3562                 if (!hc->chan[bch->slot].rx_off) {
3563                         /* reset fifo on rx on */
3564                         HFC_outb_nodebug(hc, R_FIFO, (bch->slot << 1) | 1);
3565                         HFC_wait_nodebug(hc);
3566                         HFC_outb_nodebug(hc, R_INC_RES_FIFO, V_RES_F);
3567                         HFC_wait_nodebug(hc);
3568                 }
3569                 if (debug & DEBUG_HFCMULTI_MSG)
3570                         printk(KERN_DEBUG "%s: RX_OFF request (nr=%d off=%d)\n",
3571                             __func__, bch->nr, hc->chan[bch->slot].rx_off);
3572                 break;
3573         case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
3574                 test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
3575                 if (debug & DEBUG_HFCMULTI_MSG)
3576                         printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
3577                                 "off=%d)\n", __func__, bch->nr, !!cq->p1);
3578                 break;
3579         case MISDN_CTRL_HW_FEATURES: /* fill features structure */
3580                 if (debug & DEBUG_HFCMULTI_MSG)
3581                         printk(KERN_DEBUG "%s: HW_FEATURE request\n",
3582                             __func__);
3583                 /* create confirm */
3584                 features->hfc_id = hc->id;
3585                 if (test_bit(HFC_CHIP_DTMF, &hc->chip))
3586                         features->hfc_dtmf = 1;
3587                 if (test_bit(HFC_CHIP_CONF, &hc->chip))
3588                         features->hfc_conf = 1;
3589                 features->hfc_loops = 0;
3590                 if (test_bit(HFC_CHIP_B410P, &hc->chip)) {
3591                         features->hfc_echocanhw = 1;
3592                 } else {
3593                         features->pcm_id = hc->pcm;
3594                         features->pcm_slots = hc->slots;
3595                         features->pcm_banks = 2;
3596                 }
3597                 break;
3598         case MISDN_CTRL_HFC_PCM_CONN: /* connect to pcm timeslot (0..N) */
3599                 slot_tx = cq->p1 & 0xff;
3600                 bank_tx = cq->p1 >> 8;
3601                 slot_rx = cq->p2 & 0xff;
3602                 bank_rx = cq->p2 >> 8;
3603                 if (debug & DEBUG_HFCMULTI_MSG)
3604                         printk(KERN_DEBUG
3605                             "%s: HFC_PCM_CONN slot %d bank %d (TX) "
3606                             "slot %d bank %d (RX)\n",
3607                             __func__, slot_tx, bank_tx,
3608                             slot_rx, bank_rx);
3609                 if (slot_tx < hc->slots && bank_tx <= 2 &&
3610                     slot_rx < hc->slots && bank_rx <= 2)
3611                         hfcmulti_pcm(hc, bch->slot,
3612                             slot_tx, bank_tx, slot_rx, bank_rx);
3613                 else {
3614                         printk(KERN_WARNING
3615                             "%s: HFC_PCM_CONN slot %d bank %d (TX) "
3616                             "slot %d bank %d (RX) out of range\n",
3617                             __func__, slot_tx, bank_tx,
3618                             slot_rx, bank_rx);
3619                         ret = -EINVAL;
3620                 }
3621                 break;
3622         case MISDN_CTRL_HFC_PCM_DISC: /* release interface from pcm timeslot */
3623                 if (debug & DEBUG_HFCMULTI_MSG)
3624                         printk(KERN_DEBUG "%s: HFC_PCM_DISC\n",
3625                             __func__);
3626                 hfcmulti_pcm(hc, bch->slot, -1, 0, -1, 0);
3627                 break;
3628         case MISDN_CTRL_HFC_CONF_JOIN: /* join conference (0..7) */
3629                 num = cq->p1 & 0xff;
3630                 if (debug & DEBUG_HFCMULTI_MSG)
3631                         printk(KERN_DEBUG "%s: HFC_CONF_JOIN conf %d\n",
3632                             __func__, num);
3633                 if (num <= 7)
3634                         hfcmulti_conf(hc, bch->slot, num);
3635                 else {
3636                         printk(KERN_WARNING
3637                             "%s: HW_CONF_JOIN conf %d out of range\n",
3638                             __func__, num);
3639                         ret = -EINVAL;
3640                 }
3641                 break;
3642         case MISDN_CTRL_HFC_CONF_SPLIT: /* split conference */
3643                 if (debug & DEBUG_HFCMULTI_MSG)
3644                         printk(KERN_DEBUG "%s: HFC_CONF_SPLIT\n", __func__);
3645                 hfcmulti_conf(hc, bch->slot, -1);
3646                 break;
3647         case MISDN_CTRL_HFC_ECHOCAN_ON:
3648                 if (debug & DEBUG_HFCMULTI_MSG)
3649                         printk(KERN_DEBUG "%s: HFC_ECHOCAN_ON\n", __func__);
3650                 if (test_bit(HFC_CHIP_B410P, &hc->chip))
3651                         vpm_echocan_on(hc, bch->slot, cq->p1);
3652                 else
3653                         ret = -EINVAL;
3654                 break;
3655
3656         case MISDN_CTRL_HFC_ECHOCAN_OFF:
3657                 if (debug & DEBUG_HFCMULTI_MSG)
3658                         printk(KERN_DEBUG "%s: HFC_ECHOCAN_OFF\n",
3659                                 __func__);
3660                 if (test_bit(HFC_CHIP_B410P, &hc->chip))
3661                         vpm_echocan_off(hc, bch->slot);
3662                 else
3663                         ret = -EINVAL;
3664                 break;
3665         default:
3666                 printk(KERN_WARNING "%s: unknown Op %x\n",
3667                     __func__, cq->op);
3668                 ret = -EINVAL;
3669                 break;
3670         }
3671         return ret;
3672 }
3673
3674 static int
3675 hfcm_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
3676 {
3677         struct bchannel         *bch = container_of(ch, struct bchannel, ch);
3678         struct hfc_multi        *hc = bch->hw;
3679         int                     err = -EINVAL;
3680         u_long  flags;
3681
3682         if (bch->debug & DEBUG_HW)
3683                 printk(KERN_DEBUG "%s: cmd:%x %p\n",
3684                     __func__, cmd, arg);
3685         switch (cmd) {
3686         case CLOSE_CHANNEL:
3687                 test_and_clear_bit(FLG_OPEN, &bch->Flags);
3688                 if (test_bit(FLG_ACTIVE, &bch->Flags))
3689                         deactivate_bchannel(bch); /* locked there */
3690                 ch->protocol = ISDN_P_NONE;
3691                 ch->peer = NULL;
3692                 module_put(THIS_MODULE);
3693                 err = 0;
3694                 break;
3695         case CONTROL_CHANNEL:
3696                 spin_lock_irqsave(&hc->lock, flags);
3697                 err = channel_bctrl(bch, arg);
3698                 spin_unlock_irqrestore(&hc->lock, flags);
3699                 break;
3700         default:
3701                 printk(KERN_WARNING "%s: unknown prim(%x)\n",
3702                         __func__, cmd);
3703         }
3704         return err;
3705 }
3706
3707 /*
3708  * handle D-channel events
3709  *
3710  * handle state change event
3711  */
3712 static void
3713 ph_state_change(struct dchannel *dch)
3714 {
3715         struct hfc_multi *hc;
3716         int ch, i;
3717
3718         if (!dch) {
3719                 printk(KERN_WARNING "%s: ERROR given dch is NULL\n",
3720                     __func__);
3721                 return;
3722         }
3723         hc = dch->hw;
3724         ch = dch->slot;
3725
3726         if (hc->ctype == HFC_TYPE_E1) {
3727                 if (dch->dev.D.protocol == ISDN_P_TE_E1) {
3728                         if (debug & DEBUG_HFCMULTI_STATE)
3729                                 printk(KERN_DEBUG
3730                                     "%s: E1 TE (id=%d) newstate %x\n",
3731                                     __func__, hc->id, dch->state);
3732                 } else {
3733                         if (debug & DEBUG_HFCMULTI_STATE)
3734                                 printk(KERN_DEBUG
3735                                     "%s: E1 NT (id=%d) newstate %x\n",
3736                                     __func__, hc->id, dch->state);
3737                 }
3738                 switch (dch->state) {
3739                 case (1):
3740                         if (hc->e1_state != 1) {
3741                             for (i = 1; i <= 31; i++) {
3742                                 /* reset fifos on e1 activation */
3743                                 HFC_outb_nodebug(hc, R_FIFO, (i << 1) | 1);
3744                                 HFC_wait_nodebug(hc);
3745                                 HFC_outb_nodebug(hc,
3746                                         R_INC_RES_FIFO, V_RES_F);
3747                                 HFC_wait_nodebug(hc);
3748                             }
3749                         }
3750                         test_and_set_bit(FLG_ACTIVE, &dch->Flags);
3751                         _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
3752                             MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
3753                         break;
3754
3755                 default:
3756                         if (hc->e1_state != 1)
3757                                 return;
3758                         test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
3759                         _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
3760                             MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
3761                 }
3762                 hc->e1_state = dch->state;
3763         } else {
3764                 if (dch->dev.D.protocol == ISDN_P_TE_S0) {
3765                         if (debug & DEBUG_HFCMULTI_STATE)
3766                                 printk(KERN_DEBUG
3767                                     "%s: S/T TE newstate %x\n",
3768                                     __func__, dch->state);
3769                         switch (dch->state) {
3770                         case (0):
3771                                 l1_event(dch->l1, HW_RESET_IND);
3772                                 break;
3773                         case (3):
3774                                 l1_event(dch->l1, HW_DEACT_IND);
3775                                 break;
3776                         case (5):
3777                         case (8):
3778                                 l1_event(dch->l1, ANYSIGNAL);
3779                                 break;
3780                         case (6):
3781                                 l1_event(dch->l1, INFO2);
3782                                 break;
3783                         case (7):
3784                                 l1_event(dch->l1, INFO4_P8);
3785                                 break;
3786                         }
3787                 } else {
3788                         if (debug & DEBUG_HFCMULTI_STATE)
3789                                 printk(KERN_DEBUG "%s: S/T NT newstate %x\n",
3790                                     __func__, dch->state);
3791                         switch (dch->state) {
3792                         case (2):
3793                                 if (hc->chan[ch].nt_timer == 0) {
3794                                         hc->chan[ch].nt_timer = -1;
3795                                         HFC_outb(hc, R_ST_SEL,
3796                                             hc->chan[ch].port);
3797                                         /* undocumented: delay after R_ST_SEL */
3798                                         udelay(1);
3799                                         HFC_outb(hc, A_ST_WR_STATE, 4 |
3800                                             V_ST_LD_STA); /* G4 */
3801                                         udelay(6); /* wait at least 5,21us */
3802                                         HFC_outb(hc, A_ST_WR_STATE, 4);
3803                                         dch->state = 4;
3804                                 } else {
3805                                         /* one extra count for the next event */
3806                                         hc->chan[ch].nt_timer =
3807                                             nt_t1_count[poll_timer] + 1;
3808                                         HFC_outb(hc, R_ST_SEL,
3809                                             hc->chan[ch].port);
3810                                         /* undocumented: delay after R_ST_SEL */
3811                                         udelay(1);
3812                                         /* allow G2 -> G3 transition */
3813                                         HFC_outb(hc, A_ST_WR_STATE, 2 |
3814                                             V_SET_G2_G3);
3815                                 }
3816                                 break;
3817                         case (1):
3818                                 hc->chan[ch].nt_timer = -1;
3819                                 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
3820                                 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
3821                                     MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
3822                                 break;
3823                         case (4):
3824                                 hc->chan[ch].nt_timer = -1;
3825                                 break;
3826                         case (3):
3827                                 hc->chan[ch].nt_timer = -1;
3828                                 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
3829                                 _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
3830                                     MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
3831                                 break;
3832                         }
3833                 }
3834         }
3835 }
3836
3837 /*
3838  * called for card mode init message
3839  */
3840
3841 static void
3842 hfcmulti_initmode(struct dchannel *dch)
3843 {
3844         struct hfc_multi *hc = dch->hw;
3845         u_char          a_st_wr_state, r_e1_wr_sta;
3846         int             i, pt;
3847
3848         if (debug & DEBUG_HFCMULTI_INIT)
3849                 printk(KERN_DEBUG "%s: entered\n", __func__);
3850
3851         if (hc->ctype == HFC_TYPE_E1) {
3852                 hc->chan[hc->dslot].slot_tx = -1;
3853                 hc->chan[hc->dslot].slot_rx = -1;
3854                 hc->chan[hc->dslot].conf = -1;
3855                 if (hc->dslot) {
3856                         mode_hfcmulti(hc, hc->dslot, dch->dev.D.protocol,
3857                                 -1, 0, -1, 0);
3858                         dch->timer.function = (void *) hfcmulti_dbusy_timer;
3859                         dch->timer.data = (long) dch;
3860                         init_timer(&dch->timer);
3861                 }
3862                 for (i = 1; i <= 31; i++) {
3863                         if (i == hc->dslot)
3864                                 continue;
3865                         hc->chan[i].slot_tx = -1;
3866                         hc->chan[i].slot_rx = -1;
3867                         hc->chan[i].conf = -1;
3868                         mode_hfcmulti(hc, i, ISDN_P_NONE, -1, 0, -1, 0);
3869                 }
3870                 /* E1 */
3871                 if (test_bit(HFC_CFG_REPORT_LOS, &hc->chan[hc->dslot].cfg)) {
3872                         HFC_outb(hc, R_LOS0, 255); /* 2 ms */
3873                         HFC_outb(hc, R_LOS1, 255); /* 512 ms */
3874                 }
3875                 if (test_bit(HFC_CFG_OPTICAL, &hc->chan[hc->dslot].cfg)) {
3876                         HFC_outb(hc, R_RX0, 0);
3877                         hc->hw.r_tx0 = 0 | V_OUT_EN;
3878                 } else {
3879                         HFC_outb(hc, R_RX0, 1);
3880                         hc->hw.r_tx0 = 1 | V_OUT_EN;
3881                 }
3882                 hc->hw.r_tx1 = V_ATX | V_NTRI;
3883                 HFC_outb(hc, R_TX0, hc->hw.r_tx0);
3884                 HFC_outb(hc, R_TX1, hc->hw.r_tx1);
3885                 HFC_outb(hc, R_TX_FR0, 0x00);
3886                 HFC_outb(hc, R_TX_FR1, 0xf8);
3887
3888                 if (test_bit(HFC_CFG_CRC4, &hc->chan[hc->dslot].cfg))
3889                         HFC_outb(hc, R_TX_FR2, V_TX_MF | V_TX_E | V_NEG_E);
3890
3891                 HFC_outb(hc, R_RX_FR0, V_AUTO_RESYNC | V_AUTO_RECO | 0);
3892
3893                 if (test_bit(HFC_CFG_CRC4, &hc->chan[hc->dslot].cfg))
3894                         HFC_outb(hc, R_RX_FR1, V_RX_MF | V_RX_MF_SYNC);
3895
3896                 if (dch->dev.D.protocol == ISDN_P_NT_E1) {
3897                         if (debug & DEBUG_HFCMULTI_INIT)
3898                                 printk(KERN_DEBUG "%s: E1 port is NT-mode\n",
3899                                     __func__);
3900                         r_e1_wr_sta = 0; /* G0 */
3901                         hc->e1_getclock = 0;
3902                 } else {
3903                         if (debug & DEBUG_HFCMULTI_INIT)
3904                                 printk(KERN_DEBUG "%s: E1 port is TE-mode\n",
3905                                     __func__);
3906                         r_e1_wr_sta = 0; /* F0 */
3907                         hc->e1_getclock = 1;
3908                 }
3909                 if (test_bit(HFC_CHIP_RX_SYNC, &hc->chip))
3910                         HFC_outb(hc, R_SYNC_OUT, V_SYNC_E1_RX);
3911                 else
3912                         HFC_outb(hc, R_SYNC_OUT, 0);
3913                 if (test_bit(HFC_CHIP_E1CLOCK_GET, &hc->chip))
3914                         hc->e1_getclock = 1;
3915                 if (test_bit(HFC_CHIP_E1CLOCK_PUT, &hc->chip))
3916                         hc->e1_getclock = 0;
3917                 if (test_bit(HFC_CHIP_PCM_SLAVE, &hc->chip)) {
3918                         /* SLAVE (clock master) */
3919                         if (debug & DEBUG_HFCMULTI_INIT)
3920                                 printk(KERN_DEBUG
3921                                     "%s: E1 port is clock master "
3922                                     "(clock from PCM)\n", __func__);
3923                         HFC_outb(hc, R_SYNC_CTRL, V_EXT_CLK_SYNC | V_PCM_SYNC);
3924                 } else {
3925                         if (hc->e1_getclock) {
3926                                 /* MASTER (clock slave) */
3927                                 if (debug & DEBUG_HFCMULTI_INIT)
3928                                         printk(KERN_DEBUG
3929                                             "%s: E1 port is clock slave "
3930                                             "(clock to PCM)\n", __func__);
3931                                 HFC_outb(hc, R_SYNC_CTRL, V_SYNC_OFFS);
3932                         } else {
3933                                 /* MASTER (clock master) */
3934                                 if (debug & DEBUG_HFCMULTI_INIT)
3935                                         printk(KERN_DEBUG "%s: E1 port is "
3936                                             "clock master "
3937                                             "(clock from QUARTZ)\n",
3938                                             __func__);
3939                                 HFC_outb(hc, R_SYNC_CTRL, V_EXT_CLK_SYNC |
3940                                     V_PCM_SYNC | V_JATT_OFF);
3941                                 HFC_outb(hc, R_SYNC_OUT, 0);
3942                         }
3943                 }
3944                 HFC_outb(hc, R_JATT_ATT, 0x9c); /* undoc register */
3945                 HFC_outb(hc, R_PWM_MD, V_PWM0_MD);
3946                 HFC_outb(hc, R_PWM0, 0x50);
3947                 HFC_outb(hc, R_PWM1, 0xff);
3948                 /* state machine setup */
3949                 HFC_outb(hc, R_E1_WR_STA, r_e1_wr_sta | V_E1_LD_STA);
3950                 udelay(6); /* wait at least 5,21us */
3951                 HFC_outb(hc, R_E1_WR_STA, r_e1_wr_sta);
3952                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
3953                         hc->syncronized = 0;
3954                         plxsd_checksync(hc, 0);
3955                 }
3956         } else {
3957                 i = dch->slot;
3958                 hc->chan[i].slot_tx = -1;
3959                 hc->chan[i].slot_rx = -1;
3960                 hc->chan[i].conf = -1;
3961                 mode_hfcmulti(hc, i, dch->dev.D.protocol, -1, 0, -1, 0);
3962                 dch->timer.function = (void *)hfcmulti_dbusy_timer;
3963                 dch->timer.data = (long) dch;
3964                 init_timer(&dch->timer);
3965                 hc->chan[i - 2].slot_tx = -1;
3966                 hc->chan[i - 2].slot_rx = -1;
3967                 hc->chan[i - 2].conf = -1;
3968                 mode_hfcmulti(hc, i - 2, ISDN_P_NONE, -1, 0, -1, 0);
3969                 hc->chan[i - 1].slot_tx = -1;
3970                 hc->chan[i - 1].slot_rx = -1;
3971                 hc->chan[i - 1].conf = -1;
3972                 mode_hfcmulti(hc, i - 1, ISDN_P_NONE, -1, 0, -1, 0);
3973                 /* ST */
3974                 pt = hc->chan[i].port;
3975                 /* select interface */
3976                 HFC_outb(hc, R_ST_SEL, pt);
3977                 /* undocumented: delay after R_ST_SEL */
3978                 udelay(1);
3979                 if (dch->dev.D.protocol == ISDN_P_NT_S0) {
3980                         if (debug & DEBUG_HFCMULTI_INIT)
3981                                 printk(KERN_DEBUG
3982                                     "%s: ST port %d is NT-mode\n",
3983                                     __func__, pt);
3984                         /* clock delay */
3985                         HFC_outb(hc, A_ST_CLK_DLY, clockdelay_nt);
3986                         a_st_wr_state = 1; /* G1 */
3987                         hc->hw.a_st_ctrl0[pt] = V_ST_MD;
3988                 } else {
3989                         if (debug & DEBUG_HFCMULTI_INIT)
3990                                 printk(KERN_DEBUG
3991                                     "%s: ST port %d is TE-mode\n",
3992                                     __func__, pt);
3993                         /* clock delay */
3994                         HFC_outb(hc, A_ST_CLK_DLY, clockdelay_te);
3995                         a_st_wr_state = 2; /* F2 */
3996                         hc->hw.a_st_ctrl0[pt] = 0;
3997                 }
3998                 if (!test_bit(HFC_CFG_NONCAP_TX, &hc->chan[i].cfg))
3999                         hc->hw.a_st_ctrl0[pt] |= V_TX_LI;
4000                 if (hc->ctype == HFC_TYPE_XHFC) {
4001                         hc->hw.a_st_ctrl0[pt] |= 0x40 /* V_ST_PU_CTRL */;
4002                         HFC_outb(hc, 0x35 /* A_ST_CTRL3 */,
4003                                 0x7c << 1 /* V_ST_PULSE */);
4004                 }
4005                 /* line setup */
4006                 HFC_outb(hc, A_ST_CTRL0,  hc->hw.a_st_ctrl0[pt]);
4007                 /* disable E-channel */
4008                 if ((dch->dev.D.protocol == ISDN_P_NT_S0) ||
4009                     test_bit(HFC_CFG_DIS_ECHANNEL, &hc->chan[i].cfg))
4010                         HFC_outb(hc, A_ST_CTRL1, V_E_IGNO);
4011                 else
4012                         HFC_outb(hc, A_ST_CTRL1, 0);
4013                 /* enable B-channel receive */
4014                 HFC_outb(hc, A_ST_CTRL2,  V_B1_RX_EN | V_B2_RX_EN);
4015                 /* state machine setup */
4016                 HFC_outb(hc, A_ST_WR_STATE, a_st_wr_state | V_ST_LD_STA);
4017                 udelay(6); /* wait at least 5,21us */
4018                 HFC_outb(hc, A_ST_WR_STATE, a_st_wr_state);
4019                 hc->hw.r_sci_msk |= 1 << pt;
4020                 /* state machine interrupts */
4021                 HFC_outb(hc, R_SCI_MSK, hc->hw.r_sci_msk);
4022                 /* unset sync on port */
4023                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4024                         hc->syncronized &=
4025                            ~(1 << hc->chan[dch->slot].port);
4026                         plxsd_checksync(hc, 0);
4027                 }
4028         }
4029         if (debug & DEBUG_HFCMULTI_INIT)
4030                 printk("%s: done\n", __func__);
4031 }
4032
4033
4034 static int
4035 open_dchannel(struct hfc_multi *hc, struct dchannel *dch,
4036     struct channel_req *rq)
4037 {
4038         int     err = 0;
4039         u_long  flags;
4040
4041         if (debug & DEBUG_HW_OPEN)
4042                 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
4043                     dch->dev.id, __builtin_return_address(0));
4044         if (rq->protocol == ISDN_P_NONE)
4045                 return -EINVAL;
4046         if ((dch->dev.D.protocol != ISDN_P_NONE) &&
4047             (dch->dev.D.protocol != rq->protocol)) {
4048             if (debug & DEBUG_HFCMULTI_MODE)
4049                 printk(KERN_WARNING "%s: change protocol %x to %x\n",
4050                     __func__, dch->dev.D.protocol, rq->protocol);
4051         }
4052         if ((dch->dev.D.protocol == ISDN_P_TE_S0)
4053          && (rq->protocol != ISDN_P_TE_S0))
4054                 l1_event(dch->l1, CLOSE_CHANNEL);
4055         if (dch->dev.D.protocol != rq->protocol) {
4056                 if (rq->protocol == ISDN_P_TE_S0) {
4057                         err = create_l1(dch, hfcm_l1callback);
4058                         if (err)
4059                                 return err;
4060                 }
4061                 dch->dev.D.protocol = rq->protocol;
4062                 spin_lock_irqsave(&hc->lock, flags);
4063                 hfcmulti_initmode(dch);
4064                 spin_unlock_irqrestore(&hc->lock, flags);
4065         }
4066
4067         if (((rq->protocol == ISDN_P_NT_S0) && (dch->state == 3)) ||
4068             ((rq->protocol == ISDN_P_TE_S0) && (dch->state == 7)) ||
4069             ((rq->protocol == ISDN_P_NT_E1) && (dch->state == 1)) ||
4070             ((rq->protocol == ISDN_P_TE_E1) && (dch->state == 1))) {
4071                 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
4072                     0, NULL, GFP_KERNEL);
4073         }
4074         rq->ch = &dch->dev.D;
4075         if (!try_module_get(THIS_MODULE))
4076                 printk(KERN_WARNING "%s:cannot get module\n", __func__);
4077         return 0;
4078 }
4079
4080 static int
4081 open_bchannel(struct hfc_multi *hc, struct dchannel *dch,
4082     struct channel_req *rq)
4083 {
4084         struct bchannel *bch;
4085         int             ch;
4086
4087         if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
4088                 return -EINVAL;
4089         if (rq->protocol == ISDN_P_NONE)
4090                 return -EINVAL;
4091         if (hc->ctype == HFC_TYPE_E1)
4092                 ch = rq->adr.channel;
4093         else
4094                 ch = (rq->adr.channel - 1) + (dch->slot - 2);
4095         bch = hc->chan[ch].bch;
4096         if (!bch) {
4097                 printk(KERN_ERR "%s:internal error ch %d has no bch\n",
4098                     __func__, ch);
4099                 return -EINVAL;
4100         }
4101         if (test_and_set_bit(FLG_OPEN, &bch->Flags))
4102                 return -EBUSY; /* b-channel can be only open once */
4103         test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
4104         bch->ch.protocol = rq->protocol;
4105         hc->chan[ch].rx_off = 0;
4106         rq->ch = &bch->ch;
4107         if (!try_module_get(THIS_MODULE))
4108                 printk(KERN_WARNING "%s:cannot get module\n", __func__);
4109         return 0;
4110 }
4111
4112 /*
4113  * device control function
4114  */
4115 static int
4116 channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
4117 {
4118         struct hfc_multi        *hc = dch->hw;
4119         int     ret = 0;
4120         int     wd_mode, wd_cnt;
4121
4122         switch (cq->op) {
4123         case MISDN_CTRL_GETOP:
4124                 cq->op = MISDN_CTRL_HFC_OP;
4125                 break;
4126         case MISDN_CTRL_HFC_WD_INIT: /* init the watchdog */
4127                 wd_cnt = cq->p1 & 0xf;
4128                 wd_mode = !!(cq->p1 >> 4);
4129                 if (debug & DEBUG_HFCMULTI_MSG)
4130                         printk(KERN_DEBUG
4131                             "%s: MISDN_CTRL_HFC_WD_INIT mode %s counter 0x%x\n",
4132                             __func__, wd_mode ? "AUTO" : "MANUAL", wd_cnt);
4133                 /* set the watchdog timer */
4134                 HFC_outb(hc, R_TI_WD, poll_timer | (wd_cnt << 4));
4135                 hc->hw.r_bert_wd_md = (wd_mode ? V_AUTO_WD_RES : 0);
4136                 if (hc->ctype == HFC_TYPE_XHFC)
4137                         hc->hw.r_bert_wd_md |= 0x40 /* V_WD_EN */;
4138                 /* init the watchdog register and reset the counter */
4139                 HFC_outb(hc, R_BERT_WD_MD, hc->hw.r_bert_wd_md | V_WD_RES);
4140                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4141                         /* enable the watchdog output for Speech-Design */
4142                         HFC_outb(hc, R_GPIO_SEL, V_GPIO_SEL7);
4143                         HFC_outb(hc, R_GPIO_EN1, V_GPIO_EN15);
4144                         HFC_outb(hc, R_GPIO_OUT1, 0);
4145                         HFC_outb(hc, R_GPIO_OUT1, V_GPIO_OUT15);
4146                 }
4147                 break;
4148         case MISDN_CTRL_HFC_WD_RESET: /* reset the watchdog counter */
4149                 if (debug & DEBUG_HFCMULTI_MSG)
4150                         printk(KERN_DEBUG "%s: MISDN_CTRL_HFC_WD_RESET\n",
4151                             __func__);
4152                 HFC_outb(hc, R_BERT_WD_MD, hc->hw.r_bert_wd_md | V_WD_RES);
4153                 break;
4154         default:
4155                 printk(KERN_WARNING "%s: unknown Op %x\n",
4156                     __func__, cq->op);
4157                 ret = -EINVAL;
4158                 break;
4159         }
4160         return ret;
4161 }
4162
4163 static int
4164 hfcm_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
4165 {
4166         struct mISDNdevice      *dev = container_of(ch, struct mISDNdevice, D);
4167         struct dchannel         *dch = container_of(dev, struct dchannel, dev);
4168         struct hfc_multi        *hc = dch->hw;
4169         struct channel_req      *rq;
4170         int                     err = 0;
4171         u_long                  flags;
4172
4173         if (dch->debug & DEBUG_HW)
4174                 printk(KERN_DEBUG "%s: cmd:%x %p\n",
4175                     __func__, cmd, arg);
4176         switch (cmd) {
4177         case OPEN_CHANNEL:
4178                 rq = arg;
4179                 switch (rq->protocol) {
4180                 case ISDN_P_TE_S0:
4181                 case ISDN_P_NT_S0:
4182                         if (hc->ctype == HFC_TYPE_E1) {
4183                                 err = -EINVAL;
4184                                 break;
4185                         }
4186                         err = open_dchannel(hc, dch, rq); /* locked there */
4187                         break;
4188                 case ISDN_P_TE_E1:
4189                 case ISDN_P_NT_E1:
4190                         if (hc->ctype != HFC_TYPE_E1) {
4191                                 err = -EINVAL;
4192                                 break;
4193                         }
4194                         err = open_dchannel(hc, dch, rq); /* locked there */
4195                         break;
4196                 default:
4197                         spin_lock_irqsave(&hc->lock, flags);
4198                         err = open_bchannel(hc, dch, rq);
4199                         spin_unlock_irqrestore(&hc->lock, flags);
4200                 }
4201                 break;
4202         case CLOSE_CHANNEL:
4203                 if (debug & DEBUG_HW_OPEN)
4204                         printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
4205                             __func__, dch->dev.id,
4206                             __builtin_return_address(0));
4207                 module_put(THIS_MODULE);
4208                 break;
4209         case CONTROL_CHANNEL:
4210                 spin_lock_irqsave(&hc->lock, flags);
4211                 err = channel_dctrl(dch, arg);
4212                 spin_unlock_irqrestore(&hc->lock, flags);
4213                 break;
4214         default:
4215                 if (dch->debug & DEBUG_HW)
4216                         printk(KERN_DEBUG "%s: unknown command %x\n",
4217                             __func__, cmd);
4218                 err = -EINVAL;
4219         }
4220         return err;
4221 }
4222
4223 static int
4224 clockctl(void *priv, int enable)
4225 {
4226         struct hfc_multi *hc = priv;
4227
4228         hc->iclock_on = enable;
4229         return 0;
4230 }
4231
4232 /*
4233  * initialize the card
4234  */
4235
4236 /*
4237  * start timer irq, wait some time and check if we have interrupts.
4238  * if not, reset chip and try again.
4239  */
4240 static int
4241 init_card(struct hfc_multi *hc)
4242 {
4243         int     err = -EIO;
4244         u_long  flags;
4245         void    __iomem *plx_acc;
4246         u_long  plx_flags;
4247
4248         if (debug & DEBUG_HFCMULTI_INIT)
4249                 printk(KERN_DEBUG "%s: entered\n", __func__);
4250
4251         spin_lock_irqsave(&hc->lock, flags);
4252         /* set interrupts but leave global interrupt disabled */
4253         hc->hw.r_irq_ctrl = V_FIFO_IRQ;
4254         disable_hwirq(hc);
4255         spin_unlock_irqrestore(&hc->lock, flags);
4256
4257         if (request_irq(hc->irq, hfcmulti_interrupt, IRQF_SHARED,
4258             "HFC-multi", hc)) {
4259                 printk(KERN_WARNING "mISDN: Could not get interrupt %d.\n",
4260                     hc->irq);
4261                 hc->irq = 0;
4262                 return -EIO;
4263         }
4264
4265         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4266                 spin_lock_irqsave(&plx_lock, plx_flags);
4267                 plx_acc = hc->plx_membase + PLX_INTCSR;
4268                 writew((PLX_INTCSR_PCIINT_ENABLE | PLX_INTCSR_LINTI1_ENABLE),
4269                         plx_acc); /* enable PCI & LINT1 irq */
4270                 spin_unlock_irqrestore(&plx_lock, plx_flags);
4271         }
4272
4273         if (debug & DEBUG_HFCMULTI_INIT)
4274                 printk(KERN_DEBUG "%s: IRQ %d count %d\n",
4275                     __func__, hc->irq, hc->irqcnt);
4276         err = init_chip(hc);
4277         if (err)
4278                 goto error;
4279         /*
4280          * Finally enable IRQ output
4281          * this is only allowed, if an IRQ routine is allready
4282          * established for this HFC, so don't do that earlier
4283          */
4284         spin_lock_irqsave(&hc->lock, flags);
4285         enable_hwirq(hc);
4286         spin_unlock_irqrestore(&hc->lock, flags);
4287         /* printk(KERN_DEBUG "no master irq set!!!\n"); */
4288         set_current_state(TASK_UNINTERRUPTIBLE);
4289         schedule_timeout((100*HZ)/1000); /* Timeout 100ms */
4290         /* turn IRQ off until chip is completely initialized */
4291         spin_lock_irqsave(&hc->lock, flags);
4292         disable_hwirq(hc);
4293         spin_unlock_irqrestore(&hc->lock, flags);
4294         if (debug & DEBUG_HFCMULTI_INIT)
4295                 printk(KERN_DEBUG "%s: IRQ %d count %d\n",
4296                     __func__, hc->irq, hc->irqcnt);
4297         if (hc->irqcnt) {
4298                 if (debug & DEBUG_HFCMULTI_INIT)
4299                         printk(KERN_DEBUG "%s: done\n", __func__);
4300
4301                 return 0;
4302         }
4303         if (test_bit(HFC_CHIP_PCM_SLAVE, &hc->chip)) {
4304                 printk(KERN_INFO "ignoring missing interrupts\n");
4305                 return 0;
4306         }
4307
4308         printk(KERN_ERR "HFC PCI: IRQ(%d) getting no interrupts during init.\n",
4309                 hc->irq);
4310
4311         err = -EIO;
4312
4313 error:
4314         if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4315                 spin_lock_irqsave(&plx_lock, plx_flags);
4316                 plx_acc = hc->plx_membase + PLX_INTCSR;
4317                 writew(0x00, plx_acc); /*disable IRQs*/
4318                 spin_unlock_irqrestore(&plx_lock, plx_flags);
4319         }
4320
4321         if (debug & DEBUG_HFCMULTI_INIT)
4322                 printk(KERN_WARNING "%s: free irq %d\n", __func__, hc->irq);
4323         if (hc->irq) {
4324                 free_irq(hc->irq, hc);
4325                 hc->irq = 0;
4326         }
4327
4328         if (debug & DEBUG_HFCMULTI_INIT)
4329                 printk(KERN_DEBUG "%s: done (err=%d)\n", __func__, err);
4330         return err;
4331 }
4332
4333 /*
4334  * find pci device and set it up
4335  */
4336
4337 static int
4338 setup_pci(struct hfc_multi *hc, struct pci_dev *pdev,
4339                 const struct pci_device_id *ent)
4340 {
4341         struct hm_map   *m = (struct hm_map *)ent->driver_data;
4342
4343         printk(KERN_INFO
4344             "HFC-multi: card manufacturer: '%s' card name: '%s' clock: %s\n",
4345             m->vendor_name, m->card_name, m->clock2 ? "double" : "normal");
4346
4347         hc->pci_dev = pdev;
4348         if (m->clock2)
4349                 test_and_set_bit(HFC_CHIP_CLOCK2, &hc->chip);
4350
4351         if (ent->device == 0xB410) {
4352                 test_and_set_bit(HFC_CHIP_B410P, &hc->chip);
4353                 test_and_set_bit(HFC_CHIP_PCM_MASTER, &hc->chip);
4354                 test_and_clear_bit(HFC_CHIP_PCM_SLAVE, &hc->chip);
4355                 hc->slots = 32;
4356         }
4357
4358         if (hc->pci_dev->irq <= 0) {
4359                 printk(KERN_WARNING "HFC-multi: No IRQ for PCI card found.\n");
4360                 return -EIO;
4361         }
4362         if (pci_enable_device(hc->pci_dev)) {
4363                 printk(KERN_WARNING "HFC-multi: Error enabling PCI card.\n");
4364                 return -EIO;
4365         }
4366         hc->leds = m->leds;
4367         hc->ledstate = 0xAFFEAFFE;
4368         hc->opticalsupport = m->opticalsupport;
4369
4370         hc->pci_iobase = 0;
4371         hc->pci_membase = NULL;
4372         hc->plx_membase = NULL;
4373
4374         /* set memory access methods */
4375         if (m->io_mode) /* use mode from card config */
4376                 hc->io_mode = m->io_mode;
4377         switch (hc->io_mode) {
4378         case HFC_IO_MODE_PLXSD:
4379                 test_and_set_bit(HFC_CHIP_PLXSD, &hc->chip);
4380                 hc->slots = 128; /* required */
4381                 hc->HFC_outb = HFC_outb_pcimem;
4382                 hc->HFC_inb = HFC_inb_pcimem;
4383                 hc->HFC_inw = HFC_inw_pcimem;
4384                 hc->HFC_wait = HFC_wait_pcimem;
4385                 hc->read_fifo = read_fifo_pcimem;
4386                 hc->write_fifo = write_fifo_pcimem;
4387                 hc->plx_origmembase =  hc->pci_dev->resource[0].start;
4388                 /* MEMBASE 1 is PLX PCI Bridge */
4389
4390                 if (!hc->plx_origmembase) {
4391                         printk(KERN_WARNING
4392                           "HFC-multi: No IO-Memory for PCI PLX bridge found\n");
4393                         pci_disable_device(hc->pci_dev);
4394                         return -EIO;
4395                 }
4396
4397                 hc->plx_membase = ioremap(hc->plx_origmembase, 0x80);
4398                 if (!hc->plx_membase) {
4399                         printk(KERN_WARNING
4400                             "HFC-multi: failed to remap plx address space. "
4401                             "(internal error)\n");
4402                         pci_disable_device(hc->pci_dev);
4403                         return -EIO;
4404                 }
4405                 printk(KERN_INFO
4406                     "HFC-multi: plx_membase:%#lx plx_origmembase:%#lx\n",
4407                     (u_long)hc->plx_membase, hc->plx_origmembase);
4408
4409                 hc->pci_origmembase =  hc->pci_dev->resource[2].start;
4410                     /* MEMBASE 1 is PLX PCI Bridge */
4411                 if (!hc->pci_origmembase) {
4412                         printk(KERN_WARNING
4413                             "HFC-multi: No IO-Memory for PCI card found\n");
4414                         pci_disable_device(hc->pci_dev);
4415                         return -EIO;
4416                 }
4417
4418                 hc->pci_membase = ioremap(hc->pci_origmembase, 0x400);
4419                 if (!hc->pci_membase) {
4420                         printk(KERN_WARNING "HFC-multi: failed to remap io "
4421                             "address space. (internal error)\n");
4422                         pci_disable_device(hc->pci_dev);
4423                         return -EIO;
4424                 }
4425
4426                 printk(KERN_INFO
4427                     "card %d: defined at MEMBASE %#lx (%#lx) IRQ %d HZ %d "
4428                     "leds-type %d\n",
4429                     hc->id, (u_long)hc->pci_membase, hc->pci_origmembase,
4430                     hc->pci_dev->irq, HZ, hc->leds);
4431                 pci_write_config_word(hc->pci_dev, PCI_COMMAND, PCI_ENA_MEMIO);
4432                 break;
4433         case HFC_IO_MODE_PCIMEM:
4434                 hc->HFC_outb = HFC_outb_pcimem;
4435                 hc->HFC_inb = HFC_inb_pcimem;
4436                 hc->HFC_inw = HFC_inw_pcimem;
4437                 hc->HFC_wait = HFC_wait_pcimem;
4438                 hc->read_fifo = read_fifo_pcimem;
4439                 hc->write_fifo = write_fifo_pcimem;
4440                 hc->pci_origmembase = hc->pci_dev->resource[1].start;
4441                 if (!hc->pci_origmembase) {
4442                         printk(KERN_WARNING
4443                             "HFC-multi: No IO-Memory for PCI card found\n");
4444                         pci_disable_device(hc->pci_dev);
4445                         return -EIO;
4446                 }
4447
4448                 hc->pci_membase = ioremap(hc->pci_origmembase, 256);
4449                 if (!hc->pci_membase) {
4450                         printk(KERN_WARNING
4451                             "HFC-multi: failed to remap io address space. "
4452                             "(internal error)\n");
4453                         pci_disable_device(hc->pci_dev);
4454                         return -EIO;
4455                 }
4456                 printk(KERN_INFO "card %d: defined at MEMBASE %#lx (%#lx) IRQ "
4457                     "%d HZ %d leds-type %d\n", hc->id, (u_long)hc->pci_membase,
4458                     hc->pci_origmembase, hc->pci_dev->irq, HZ, hc->leds);
4459                 pci_write_config_word(hc->pci_dev, PCI_COMMAND, PCI_ENA_MEMIO);
4460                 break;
4461         case HFC_IO_MODE_REGIO:
4462                 hc->HFC_outb = HFC_outb_regio;
4463                 hc->HFC_inb = HFC_inb_regio;
4464                 hc->HFC_inw = HFC_inw_regio;
4465                 hc->HFC_wait = HFC_wait_regio;
4466                 hc->read_fifo = read_fifo_regio;
4467                 hc->write_fifo = write_fifo_regio;
4468                 hc->pci_iobase = (u_int) hc->pci_dev->resource[0].start;
4469                 if (!hc->pci_iobase) {
4470                         printk(KERN_WARNING
4471                                 "HFC-multi: No IO for PCI card found\n");
4472                         pci_disable_device(hc->pci_dev);
4473                         return -EIO;
4474                 }
4475
4476                 if (!request_region(hc->pci_iobase, 8, "hfcmulti")) {
4477                         printk(KERN_WARNING "HFC-multi: failed to request "
4478                             "address space at 0x%08lx (internal error)\n",
4479                             hc->pci_iobase);
4480                         pci_disable_device(hc->pci_dev);
4481                         return -EIO;
4482                 }
4483
4484                 printk(KERN_INFO
4485                     "%s %s: defined at IOBASE %#x IRQ %d HZ %d leds-type %d\n",
4486                     m->vendor_name, m->card_name, (u_int) hc->pci_iobase,
4487                     hc->pci_dev->irq, HZ, hc->leds);
4488                 pci_write_config_word(hc->pci_dev, PCI_COMMAND, PCI_ENA_REGIO);
4489                 break;
4490         default:
4491                 printk(KERN_WARNING "HFC-multi: Invalid IO mode.\n");
4492                 pci_disable_device(hc->pci_dev);
4493                 return -EIO;
4494         }
4495
4496         pci_set_drvdata(hc->pci_dev, hc);
4497
4498         /* At this point the needed PCI config is done */
4499         /* fifos are still not enabled */
4500         return 0;
4501 }
4502
4503
4504 /*
4505  * remove port
4506  */
4507
4508 static void
4509 release_port(struct hfc_multi *hc, struct dchannel *dch)
4510 {
4511         int     pt, ci, i = 0;
4512         u_long  flags;
4513         struct bchannel *pb;
4514
4515         ci = dch->slot;
4516         pt = hc->chan[ci].port;
4517
4518         if (debug & DEBUG_HFCMULTI_INIT)
4519                 printk(KERN_DEBUG "%s: entered for port %d\n",
4520                         __func__, pt + 1);
4521
4522         if (pt >= hc->ports) {
4523                 printk(KERN_WARNING "%s: ERROR port out of range (%d).\n",
4524                      __func__, pt + 1);
4525                 return;
4526         }
4527
4528         if (debug & DEBUG_HFCMULTI_INIT)
4529                 printk(KERN_DEBUG "%s: releasing port=%d\n",
4530                     __func__, pt + 1);
4531
4532         if (dch->dev.D.protocol == ISDN_P_TE_S0)
4533                 l1_event(dch->l1, CLOSE_CHANNEL);
4534
4535         hc->chan[ci].dch = NULL;
4536
4537         if (hc->created[pt]) {
4538                 hc->created[pt] = 0;
4539                 mISDN_unregister_device(&dch->dev);
4540         }
4541
4542         spin_lock_irqsave(&hc->lock, flags);
4543
4544         if (dch->timer.function) {
4545                 del_timer(&dch->timer);
4546                 dch->timer.function = NULL;
4547         }
4548
4549         if (hc->ctype == HFC_TYPE_E1) { /* E1 */
4550                 /* remove sync */
4551                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4552                         hc->syncronized = 0;
4553                         plxsd_checksync(hc, 1);
4554                 }
4555                 /* free channels */
4556                 for (i = 0; i <= 31; i++) {
4557                         if (hc->chan[i].bch) {
4558                                 if (debug & DEBUG_HFCMULTI_INIT)
4559                                         printk(KERN_DEBUG
4560                                             "%s: free port %d channel %d\n",
4561                                             __func__, hc->chan[i].port+1, i);
4562                                 pb = hc->chan[i].bch;
4563                                 hc->chan[i].bch = NULL;
4564                                 spin_unlock_irqrestore(&hc->lock, flags);
4565                                 mISDN_freebchannel(pb);
4566                                 kfree(pb);
4567                                 kfree(hc->chan[i].coeff);
4568                                 spin_lock_irqsave(&hc->lock, flags);
4569                         }
4570                 }
4571         } else {
4572                 /* remove sync */
4573                 if (test_bit(HFC_CHIP_PLXSD, &hc->chip)) {
4574                         hc->syncronized &=
4575                            ~(1 << hc->chan[ci].port);
4576                         plxsd_checksync(hc, 1);
4577                 }
4578                 /* free channels */
4579                 if (hc->chan[ci - 2].bch) {
4580                         if (debug & DEBUG_HFCMULTI_INIT)
4581                                 printk(KERN_DEBUG
4582                                     "%s: free port %d channel %d\n",
4583                                     __func__, hc->chan[ci - 2].port+1,
4584                                     ci - 2);
4585                         pb = hc->chan[ci - 2].bch;
4586                         hc->chan[ci - 2].bch = NULL;
4587                         spin_unlock_irqrestore(&hc->lock, flags);
4588                         mISDN_freebchannel(pb);
4589                         kfree(pb);
4590                         kfree(hc->chan[ci - 2].coeff);
4591                         spin_lock_irqsave(&hc->lock, flags);
4592                 }
4593                 if (hc->chan[ci - 1].bch) {
4594                         if (debug & DEBUG_HFCMULTI_INIT)
4595                                 printk(KERN_DEBUG
4596                                     "%s: free port %d channel %d\n",
4597                                     __func__, hc->chan[ci - 1].port+1,
4598                                     ci - 1);
4599                         pb = hc->chan[ci - 1].bch;
4600                         hc->chan[ci - 1].bch = NULL;
4601                         spin_unlock_irqrestore(&hc->lock, flags);
4602                         mISDN_freebchannel(pb);
4603                         kfree(pb);
4604                         kfree(hc->chan[ci - 1].coeff);
4605                         spin_lock_irqsave(&hc->lock, flags);
4606                 }
4607         }
4608
4609         spin_unlock_irqrestore(&hc->lock, flags);
4610
4611         if (debug & DEBUG_HFCMULTI_INIT)
4612                 printk(KERN_DEBUG "%s: free port %d channel D\n", __func__, pt);
4613         mISDN_freedchannel(dch);
4614         kfree(dch);
4615
4616         if (debug & DEBUG_HFCMULTI_INIT)
4617                 printk(KERN_DEBUG "%s: done!\n", __func__);
4618 }
4619
4620 static void
4621 release_card(struct hfc_multi *hc)
4622 {
4623         u_long  flags;
4624         int     ch;
4625
4626         if (debug & DEBUG_HFCMULTI_INIT)
4627                 printk(KERN_WARNING "%s: release card (%d) entered\n",
4628                     __func__, hc->id);
4629
4630         /* unregister clock source */
4631         if (hc->iclock)
4632                 mISDN_unregister_clock(hc->iclock);
4633
4634         /* disable irq */
4635         spin_lock_irqsave(&hc->lock, flags);
4636         disable_hwirq(hc);
4637         spin_unlock_irqrestore(&hc->lock, flags);
4638         udelay(1000);
4639
4640         /* dimm leds */
4641         if (hc->leds)
4642                 hfcmulti_leds(hc);
4643
4644         /* disable D-channels & B-channels */
4645         if (debug & DEBUG_HFCMULTI_INIT)
4646                 printk(KERN_DEBUG "%s: disable all channels (d and b)\n",
4647                     __func__);
4648         for (ch = 0; ch <= 31; ch++) {
4649                 if (hc->chan[ch].dch)
4650                         release_port(hc, hc->chan[ch].dch);
4651         }
4652
4653         /* release hardware & irq */
4654         if (hc->irq) {
4655                 if (debug & DEBUG_HFCMULTI_INIT)
4656                         printk(KERN_WARNING "%s: free irq %d\n",
4657                             __func__, hc->irq);
4658                 free_irq(hc->irq, hc);
4659                 hc->irq = 0;
4660
4661         }
4662         release_io_hfcmulti(hc);
4663
4664         if (debug & DEBUG_HFCMULTI_INIT)
4665                 printk(KERN_WARNING "%s: remove instance from list\n",
4666                      __func__);
4667         list_del(&hc->list);
4668
4669         if (debug & DEBUG_HFCMULTI_INIT)
4670                 printk(KERN_WARNING "%s: delete instance\n", __func__);
4671         if (hc == syncmaster)
4672                 syncmaster = NULL;
4673         kfree(hc);
4674         if (debug & DEBUG_HFCMULTI_INIT)
4675                 printk(KERN_WARNING "%s: card successfully removed\n",
4676                     __func__);
4677 }
4678
4679 static int
4680 init_e1_port(struct hfc_multi *hc, struct hm_map *m)
4681 {
4682         struct dchannel *dch;
4683         struct bchannel *bch;
4684         int             ch, ret = 0;
4685         char            name[MISDN_MAX_IDLEN];
4686
4687         dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
4688         if (!dch)
4689                 return -ENOMEM;
4690         dch->debug = debug;
4691         mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, ph_state_change);
4692         dch->hw = hc;
4693         dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
4694         dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
4695             (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
4696         dch->dev.D.send = handle_dmsg;
4697         dch->dev.D.ctrl = hfcm_dctrl;
4698         dch->dev.nrbchan = (hc->dslot)?30:31;
4699         dch->slot = hc->dslot;
4700         hc->chan[hc->dslot].dch = dch;
4701         hc->chan[hc->dslot].port = 0;
4702         hc->chan[hc->dslot].nt_timer = -1;
4703         for (ch = 1; ch <= 31; ch++) {
4704                 if (ch == hc->dslot) /* skip dchannel */
4705                         continue;
4706                 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
4707                 if (!bch) {
4708                         printk(KERN_ERR "%s: no memory for bchannel\n",
4709                             __func__);
4710                         ret = -ENOMEM;
4711                         goto free_chan;
4712                 }
4713                 hc->chan[ch].coeff = kzalloc(512, GFP_KERNEL);
4714                 if (!hc->chan[ch].coeff) {
4715                         printk(KERN_ERR "%s: no memory for coeffs\n",
4716                             __func__);
4717                         ret = -ENOMEM;
4718                         kfree(bch);
4719                         goto free_chan;
4720                 }
4721                 bch->nr = ch;
4722                 bch->slot = ch;
4723                 bch->debug = debug;
4724                 mISDN_initbchannel(bch, MAX_DATA_MEM);
4725                 bch->hw = hc;
4726                 bch->ch.send = handle_bmsg;
4727                 bch->ch.ctrl = hfcm_bctrl;
4728                 bch->ch.nr = ch;
4729                 list_add(&bch->ch.list, &dch->dev.bchannels);
4730                 hc->chan[ch].bch = bch;
4731                 hc->chan[ch].port = 0;
4732                 set_channelmap(bch->nr, dch->dev.channelmap);
4733         }
4734         /* set optical line type */
4735         if (port[Port_cnt] & 0x001) {
4736                 if (!m->opticalsupport)  {
4737                         printk(KERN_INFO
4738                             "This board has no optical "
4739                             "support\n");
4740                 } else {
4741                         if (debug & DEBUG_HFCMULTI_INIT)
4742                                 printk(KERN_DEBUG
4743                                     "%s: PORT set optical "
4744                                     "interfacs: card(%d) "
4745                                     "port(%d)\n",
4746                                     __func__,
4747                                     HFC_cnt + 1, 1);
4748                         test_and_set_bit(HFC_CFG_OPTICAL,
4749                             &hc->chan[hc->dslot].cfg);
4750                 }
4751         }
4752         /* set LOS report */
4753         if (port[Port_cnt] & 0x004) {
4754                 if (debug & DEBUG_HFCMULTI_INIT)
4755                         printk(KERN_DEBUG "%s: PORT set "
4756                             "LOS report: card(%d) port(%d)\n",
4757                             __func__, HFC_cnt + 1, 1);
4758                 test_and_set_bit(HFC_CFG_REPORT_LOS,
4759                     &hc->chan[hc->dslot].cfg);
4760         }
4761         /* set AIS report */
4762         if (port[Port_cnt] & 0x008) {
4763                 if (debug & DEBUG_HFCMULTI_INIT)
4764                         printk(KERN_DEBUG "%s: PORT set "
4765                             "AIS report: card(%d) port(%d)\n",
4766                             __func__, HFC_cnt + 1, 1);
4767                 test_and_set_bit(HFC_CFG_REPORT_AIS,
4768                     &hc->chan[hc->dslot].cfg);
4769         }
4770         /* set SLIP report */
4771         if (port[Port_cnt] & 0x010) {
4772                 if (debug & DEBUG_HFCMULTI_INIT)
4773                         printk(KERN_DEBUG
4774                             "%s: PORT set SLIP report: "
4775                             "card(%d) port(%d)\n",
4776                             __func__, HFC_cnt + 1, 1);
4777                 test_and_set_bit(HFC_CFG_REPORT_SLIP,
4778                     &hc->chan[hc->dslot].cfg);
4779         }
4780         /* set RDI report */
4781         if (port[Port_cnt] & 0x020) {
4782                 if (debug & DEBUG_HFCMULTI_INIT)
4783                         printk(KERN_DEBUG
4784                             "%s: PORT set RDI report: "
4785                             "card(%d) port(%d)\n",
4786                             __func__, HFC_cnt + 1, 1);
4787                 test_and_set_bit(HFC_CFG_REPORT_RDI,
4788                     &hc->chan[hc->dslot].cfg);
4789         }
4790         /* set CRC-4 Mode */
4791         if (!(port[Port_cnt] & 0x100)) {
4792                 if (debug & DEBUG_HFCMULTI_INIT)
4793                         printk(KERN_DEBUG "%s: PORT turn on CRC4 report:"
4794                                 " card(%d) port(%d)\n",
4795                                 __func__, HFC_cnt + 1, 1);
4796                 test_and_set_bit(HFC_CFG_CRC4,
4797                     &hc->chan[hc->dslot].cfg);
4798         } else {
4799                 if (debug & DEBUG_HFCMULTI_INIT)
4800                         printk(KERN_DEBUG "%s: PORT turn off CRC4"
4801                                 " report: card(%d) port(%d)\n",
4802                                 __func__, HFC_cnt + 1, 1);
4803         }
4804         /* set forced clock */
4805         if (port[Port_cnt] & 0x0200) {
4806                 if (debug & DEBUG_HFCMULTI_INIT)
4807                         printk(KERN_DEBUG "%s: PORT force getting clock from "
4808                                 "E1: card(%d) port(%d)\n",
4809                                 __func__, HFC_cnt + 1, 1);
4810                 test_and_set_bit(HFC_CHIP_E1CLOCK_GET, &hc->chip);
4811         } else
4812         if (port[Port_cnt] & 0x0400) {
4813                 if (debug & DEBUG_HFCMULTI_INIT)
4814                         printk(KERN_DEBUG "%s: PORT force putting clock to "
4815                                 "E1: card(%d) port(%d)\n",
4816                                 __func__, HFC_cnt + 1, 1);
4817                 test_and_set_bit(HFC_CHIP_E1CLOCK_PUT, &hc->chip);
4818         }
4819         /* set JATT PLL */
4820         if (port[Port_cnt] & 0x0800) {
4821                 if (debug & DEBUG_HFCMULTI_INIT)
4822                         printk(KERN_DEBUG "%s: PORT disable JATT PLL on "
4823                                 "E1: card(%d) port(%d)\n",
4824                                 __func__, HFC_cnt + 1, 1);
4825                 test_and_set_bit(HFC_CHIP_RX_SYNC, &hc->chip);
4826         }
4827         /* set elastic jitter buffer */
4828         if (port[Port_cnt] & 0x3000) {
4829                 hc->chan[hc->dslot].jitter = (port[Port_cnt]>>12) & 0x3;
4830                 if (debug & DEBUG_HFCMULTI_INIT)
4831                         printk(KERN_DEBUG
4832                             "%s: PORT set elastic "
4833                             "buffer to %d: card(%d) port(%d)\n",
4834                             __func__, hc->chan[hc->dslot].jitter,
4835                             HFC_cnt + 1, 1);
4836         } else
4837                 hc->chan[hc->dslot].jitter = 2; /* default */
4838         snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-e1.%d", HFC_cnt + 1);
4839         ret = mISDN_register_device(&dch->dev, &hc->pci_dev->dev, name);
4840         if (ret)
4841                 goto free_chan;
4842         hc->created[0] = 1;
4843         return ret;
4844 free_chan:
4845         release_port(hc, dch);
4846         return ret;
4847 }
4848
4849 static int
4850 init_multi_port(struct hfc_multi *hc, int pt)
4851 {
4852         struct dchannel *dch;
4853         struct bchannel *bch;
4854         int             ch, i, ret = 0;
4855         char            name[MISDN_MAX_IDLEN];
4856
4857         dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
4858         if (!dch)
4859                 return -ENOMEM;
4860         dch->debug = debug;
4861         mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, ph_state_change);
4862         dch->hw = hc;
4863         dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
4864         dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
4865             (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
4866         dch->dev.D.send = handle_dmsg;
4867         dch->dev.D.ctrl = hfcm_dctrl;
4868         dch->dev.nrbchan = 2;
4869         i = pt << 2;
4870         dch->slot = i + 2;
4871         hc->chan[i + 2].dch = dch;
4872         hc->chan[i + 2].port = pt;
4873         hc->chan[i + 2].nt_timer = -1;
4874         for (ch = 0; ch < dch->dev.nrbchan; ch++) {
4875                 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
4876                 if (!bch) {
4877                         printk(KERN_ERR "%s: no memory for bchannel\n",
4878                             __func__);
4879                         ret = -ENOMEM;
4880                         goto free_chan;
4881                 }
4882                 hc->chan[i + ch].coeff = kzalloc(512, GFP_KERNEL);
4883                 if (!hc->chan[i + ch].coeff) {
4884                         printk(KERN_ERR "%s: no memory for coeffs\n",
4885                             __func__);
4886                         ret = -ENOMEM;
4887                         kfree(bch);
4888                         goto free_chan;
4889                 }
4890                 bch->nr = ch + 1;
4891                 bch->slot = i + ch;
4892                 bch->debug = debug;
4893                 mISDN_initbchannel(bch, MAX_DATA_MEM);
4894                 bch->hw = hc;
4895                 bch->ch.send = handle_bmsg;
4896                 bch->ch.ctrl = hfcm_bctrl;
4897                 bch->ch.nr = ch + 1;
4898                 list_add(&bch->ch.list, &dch->dev.bchannels);
4899                 hc->chan[i + ch].bch = bch;
4900                 hc->chan[i + ch].port = pt;
4901                 set_channelmap(bch->nr, dch->dev.channelmap);
4902         }
4903         /* set master clock */
4904         if (port[Port_cnt] & 0x001) {
4905                 if (debug & DEBUG_HFCMULTI_INIT)
4906                         printk(KERN_DEBUG
4907                             "%s: PROTOCOL set master clock: "
4908                             "card(%d) port(%d)\n",
4909                             __func__, HFC_cnt + 1, pt + 1);
4910                 if (dch->dev.D.protocol != ISDN_P_TE_S0) {
4911                         printk(KERN_ERR "Error: Master clock "
4912                             "for port(%d) of card(%d) is only"
4913                             " possible with TE-mode\n",
4914                             pt + 1, HFC_cnt + 1);
4915                         ret = -EINVAL;
4916                         goto free_chan;
4917                 }
4918                 if (hc->masterclk >= 0) {
4919                         printk(KERN_ERR "Error: Master clock "
4920                             "for port(%d) of card(%d) already "
4921                             "defined for port(%d)\n",
4922                             pt + 1, HFC_cnt + 1, hc->masterclk+1);
4923                         ret = -EINVAL;
4924                         goto free_chan;
4925                 }
4926                 hc->masterclk = pt;
4927         }
4928         /* set transmitter line to non capacitive */
4929         if (port[Port_cnt] & 0x002) {
4930                 if (debug & DEBUG_HFCMULTI_INIT)
4931                         printk(KERN_DEBUG
4932                             "%s: PROTOCOL set non capacitive "
4933                             "transmitter: card(%d) port(%d)\n",
4934                             __func__, HFC_cnt + 1, pt + 1);
4935                 test_and_set_bit(HFC_CFG_NONCAP_TX,
4936                     &hc->chan[i + 2].cfg);
4937         }
4938         /* disable E-channel */
4939         if (port[Port_cnt] & 0x004) {
4940         if (debug & DEBUG_HFCMULTI_INIT)
4941                         printk(KERN_DEBUG
4942                             "%s: PROTOCOL disable E-channel: "
4943                             "card(%d) port(%d)\n",
4944                             __func__, HFC_cnt + 1, pt + 1);
4945                 test_and_set_bit(HFC_CFG_DIS_ECHANNEL,
4946                     &hc->chan[i + 2].cfg);
4947         }
4948         if (hc->ctype == HFC_TYPE_XHFC) {
4949                 snprintf(name, MISDN_MAX_IDLEN - 1, "xhfc.%d-%d",
4950                         HFC_cnt + 1, pt + 1);
4951                 ret = mISDN_register_device(&dch->dev, NULL, name);
4952         } else {
4953                 snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-%ds.%d-%d",
4954                         hc->ctype, HFC_cnt + 1, pt + 1);
4955                 ret = mISDN_register_device(&dch->dev, &hc->pci_dev->dev, name);
4956         }
4957         if (ret)
4958                 goto free_chan;
4959         hc->created[pt] = 1;
4960         return ret;
4961 free_chan:
4962         release_port(hc, dch);
4963         return ret;
4964 }
4965
4966 static int
4967 hfcmulti_init(struct hm_map *m, struct pci_dev *pdev,
4968     const struct pci_device_id *ent)
4969 {
4970         int             ret_err = 0;
4971         int             pt;
4972         struct hfc_multi        *hc;
4973         u_long          flags;
4974         u_char          dips = 0, pmj = 0; /* dip settings, port mode Jumpers */
4975         int             i;
4976
4977         if (HFC_cnt >= MAX_CARDS) {
4978                 printk(KERN_ERR "too many cards (max=%d).\n",
4979                         MAX_CARDS);
4980                 return -EINVAL;
4981         }
4982         if ((type[HFC_cnt] & 0xff) && (type[HFC_cnt] & 0xff) != m->type) {
4983                 printk(KERN_WARNING "HFC-MULTI: Card '%s:%s' type %d found but "
4984                     "type[%d] %d was supplied as module parameter\n",
4985                     m->vendor_name, m->card_name, m->type, HFC_cnt,
4986                     type[HFC_cnt] & 0xff);
4987                 printk(KERN_WARNING "HFC-MULTI: Load module without parameters "
4988                         "first, to see cards and their types.");
4989                 return -EINVAL;
4990         }
4991         if (debug & DEBUG_HFCMULTI_INIT)
4992                 printk(KERN_DEBUG "%s: Registering %s:%s chip type %d (0x%x)\n",
4993                     __func__, m->vendor_name, m->card_name, m->type,
4994                     type[HFC_cnt]);
4995
4996         /* allocate card+fifo structure */
4997         hc = kzalloc(sizeof(struct hfc_multi), GFP_KERNEL);
4998         if (!hc) {
4999                 printk(KERN_ERR "No kmem for HFC-Multi card\n");
5000                 return -ENOMEM;
5001         }
5002         spin_lock_init(&hc->lock);
5003         hc->mtyp = m;
5004         hc->ctype =  m->type;
5005         hc->ports = m->ports;
5006         hc->id = HFC_cnt;
5007         hc->pcm = pcm[HFC_cnt];
5008         hc->io_mode = iomode[HFC_cnt];
5009         if (dslot[HFC_cnt] < 0 && hc->ctype == HFC_TYPE_E1) {
5010                 hc->dslot = 0;
5011                 printk(KERN_INFO "HFC-E1 card has disabled D-channel, but "
5012                         "31 B-channels\n");
5013         }
5014         if (dslot[HFC_cnt] > 0 && dslot[HFC_cnt] < 32
5015             && hc->ctype == HFC_TYPE_E1) {
5016                 hc->dslot = dslot[HFC_cnt];
5017                 printk(KERN_INFO "HFC-E1 card has alternating D-channel on "
5018                         "time slot %d\n", dslot[HFC_cnt]);
5019         } else
5020                 hc->dslot = 16;
5021
5022         /* set chip specific features */
5023         hc->masterclk = -1;
5024         if (type[HFC_cnt] & 0x100) {
5025                 test_and_set_bit(HFC_CHIP_ULAW, &hc->chip);
5026                 hc->silence = 0xff; /* ulaw silence */
5027         } else
5028                 hc->silence = 0x2a; /* alaw silence */
5029         if ((poll >> 1) > sizeof(hc->silence_data)) {
5030                 printk(KERN_ERR "HFCMULTI error: silence_data too small, "
5031                         "please fix\n");
5032                 return -EINVAL;
5033         }
5034         for (i = 0; i < (poll >> 1); i++)
5035                 hc->silence_data[i] = hc->silence;
5036
5037         if (hc->ctype != HFC_TYPE_XHFC) {
5038                 if (!(type[HFC_cnt] & 0x200))
5039                         test_and_set_bit(HFC_CHIP_DTMF, &hc->chip);
5040                 test_and_set_bit(HFC_CHIP_CONF, &hc->chip);
5041         }
5042
5043         if (type[HFC_cnt] & 0x800)
5044                 test_and_set_bit(HFC_CHIP_PCM_SLAVE, &hc->chip);
5045         if (type[HFC_cnt] & 0x1000) {
5046                 test_and_set_bit(HFC_CHIP_PCM_MASTER, &hc->chip);
5047                 test_and_clear_bit(HFC_CHIP_PCM_SLAVE, &hc->chip);
5048         }
5049         if (type[HFC_cnt] & 0x4000)
5050                 test_and_set_bit(HFC_CHIP_EXRAM_128, &hc->chip);
5051         if (type[HFC_cnt] & 0x8000)
5052                 test_and_set_bit(HFC_CHIP_EXRAM_512, &hc->chip);
5053         hc->slots = 32;
5054         if (type[HFC_cnt] & 0x10000)
5055                 hc->slots = 64;
5056         if (type[HFC_cnt] & 0x20000)
5057                 hc->slots = 128;
5058         if (type[HFC_cnt] & 0x80000) {
5059                 test_and_set_bit(HFC_CHIP_WATCHDOG, &hc->chip);
5060                 hc->wdcount = 0;
5061                 hc->wdbyte = V_GPIO_OUT2;
5062                 printk(KERN_NOTICE "Watchdog enabled\n");
5063         }
5064
5065         if (pdev && ent)
5066                 /* setup pci, hc->slots may change due to PLXSD */
5067                 ret_err = setup_pci(hc, pdev, ent);
5068         else
5069 #ifdef CONFIG_MISDN_HFCMULTI_8xx
5070                 ret_err = setup_embedded(hc, m);
5071 #else
5072         {
5073                 printk(KERN_WARNING "Embedded IO Mode not selected\n");
5074                 ret_err = -EIO;
5075         }
5076 #endif
5077         if (ret_err) {
5078                 if (hc == syncmaster)
5079                         syncmaster = NULL;
5080                 kfree(hc);
5081                 return ret_err;
5082         }
5083
5084         hc->HFC_outb_nodebug = hc->HFC_outb;
5085         hc->HFC_inb_nodebug = hc->HFC_inb;
5086         hc->HFC_inw_nodebug = hc->HFC_inw;
5087         hc->HFC_wait_nodebug = hc->HFC_wait;
5088 #ifdef HFC_REGISTER_DEBUG
5089         hc->HFC_outb = HFC_outb_debug;
5090         hc->HFC_inb = HFC_inb_debug;
5091         hc->HFC_inw = HFC_inw_debug;
5092         hc->HFC_wait = HFC_wait_debug;
5093 #endif
5094         /* create channels */
5095         for (pt = 0; pt < hc->ports; pt++) {
5096                 if (Port_cnt >= MAX_PORTS) {
5097                         printk(KERN_ERR "too many ports (max=%d).\n",
5098                                 MAX_PORTS);
5099                         ret_err = -EINVAL;
5100                         goto free_card;
5101                 }
5102                 if (hc->ctype == HFC_TYPE_E1)
5103                         ret_err = init_e1_port(hc, m);
5104                 else
5105                         ret_err = init_multi_port(hc, pt);
5106                 if (debug & DEBUG_HFCMULTI_INIT)
5107                         printk(KERN_DEBUG
5108                             "%s: Registering D-channel, card(%d) port(%d)"
5109                             "result %d\n",
5110                             __func__, HFC_cnt + 1, pt, ret_err);
5111
5112                 if (ret_err) {
5113                         while (pt) { /* release already registered ports */
5114                                 pt--;
5115                                 release_port(hc, hc->chan[(pt << 2) + 2].dch);
5116                         }
5117                         goto free_card;
5118                 }
5119                 Port_cnt++;
5120         }
5121
5122         /* disp switches */
5123         switch (m->dip_type) {
5124         case DIP_4S:
5125                 /*
5126                  * Get DIP setting for beroNet 1S/2S/4S cards
5127                  * DIP Setting: (collect GPIO 13/14/15 (R_GPIO_IN1) +
5128                  * GPI 19/23 (R_GPI_IN2))
5129                  */
5130                 dips = ((~HFC_inb(hc, R_GPIO_IN1) & 0xE0) >> 5) |
5131                         ((~HFC_inb(hc, R_GPI_IN2) & 0x80) >> 3) |
5132                         (~HFC_inb(hc, R_GPI_IN2) & 0x08);
5133
5134                 /* Port mode (TE/NT) jumpers */
5135                 pmj = ((HFC_inb(hc, R_GPI_IN3) >> 4)  & 0xf);
5136
5137                 if (test_bit(HFC_CHIP_B410P, &hc->chip))
5138                         pmj = ~pmj & 0xf;
5139
5140                 printk(KERN_INFO "%s: %s DIPs(0x%x) jumpers(0x%x)\n",
5141                         m->vendor_name, m->card_name, dips, pmj);
5142                 break;
5143         case DIP_8S:
5144                 /*
5145                  * Get DIP Setting for beroNet 8S0+ cards
5146                  * Enable PCI auxbridge function
5147                  */
5148                 HFC_outb(hc, R_BRG_PCM_CFG, 1 | V_PCM_CLK);
5149                 /* prepare access to auxport */
5150                 outw(0x4000, hc->pci_iobase + 4);
5151                 /*
5152                  * some dummy reads are required to
5153                  * read valid DIP switch data
5154                  */
5155                 dips = inb(hc->pci_iobase);
5156                 dips = inb(hc->pci_iobase);
5157                 dips = inb(hc->pci_iobase);
5158                 dips = ~inb(hc->pci_iobase) & 0x3F;
5159                 outw(0x0, hc->pci_iobase + 4);
5160                 /* disable PCI auxbridge function */
5161                 HFC_outb(hc, R_BRG_PCM_CFG, V_PCM_CLK);
5162                 printk(KERN_INFO "%s: %s DIPs(0x%x)\n",
5163                     m->vendor_name, m->card_name, dips);
5164                 break;
5165         case DIP_E1:
5166                 /*
5167                  * get DIP Setting for beroNet E1 cards
5168                  * DIP Setting: collect GPI 4/5/6/7 (R_GPI_IN0)
5169                  */
5170                 dips = (~HFC_inb(hc, R_GPI_IN0) & 0xF0)>>4;
5171                 printk(KERN_INFO "%s: %s DIPs(0x%x)\n",
5172                     m->vendor_name, m->card_name, dips);
5173                 break;
5174         }
5175
5176         /* add to list */
5177         spin_lock_irqsave(&HFClock, flags);
5178         list_add_tail(&hc->list, &HFClist);
5179         spin_unlock_irqrestore(&HFClock, flags);
5180
5181         /* use as clock source */
5182         if (clock == HFC_cnt + 1)
5183                 hc->iclock = mISDN_register_clock("HFCMulti", 0, clockctl, hc);
5184
5185         /* initialize hardware */
5186         hc->irq = (m->irq) ? : hc->pci_dev->irq;
5187         ret_err = init_card(hc);
5188         if (ret_err) {
5189                 printk(KERN_ERR "init card returns %d\n", ret_err);
5190                 release_card(hc);
5191                 return ret_err;
5192         }
5193
5194         /* start IRQ and return */
5195         spin_lock_irqsave(&hc->lock, flags);
5196         enable_hwirq(hc);
5197         spin_unlock_irqrestore(&hc->lock, flags);
5198         return 0;
5199
5200 free_card:
5201         release_io_hfcmulti(hc);
5202         if (hc == syncmaster)
5203                 syncmaster = NULL;
5204         kfree(hc);
5205         return ret_err;
5206 }
5207
5208 static void __devexit hfc_remove_pci(struct pci_dev *pdev)
5209 {
5210         struct hfc_multi        *card = pci_get_drvdata(pdev);
5211         u_long                  flags;
5212
5213         if (debug)
5214                 printk(KERN_INFO "removing hfc_multi card vendor:%x "
5215                     "device:%x subvendor:%x subdevice:%x\n",
5216                     pdev->vendor, pdev->device,
5217                     pdev->subsystem_vendor, pdev->subsystem_device);
5218
5219         if (card) {
5220                 spin_lock_irqsave(&HFClock, flags);
5221                 release_card(card);
5222                 spin_unlock_irqrestore(&HFClock, flags);
5223         }  else {
5224                 if (debug)
5225                         printk(KERN_WARNING "%s: drvdata allready removed\n",
5226                             __func__);
5227         }
5228 }
5229
5230 #define VENDOR_CCD      "Cologne Chip AG"
5231 #define VENDOR_BN       "beroNet GmbH"
5232 #define VENDOR_DIG      "Digium Inc."
5233 #define VENDOR_JH       "Junghanns.NET GmbH"
5234 #define VENDOR_PRIM     "PrimuX"
5235
5236 static const struct hm_map hfcm_map[] = {
5237 /*0*/   {VENDOR_BN, "HFC-1S Card (mini PCI)", 4, 1, 1, 3, 0, DIP_4S, 0, 0},
5238 /*1*/   {VENDOR_BN, "HFC-2S Card", 4, 2, 1, 3, 0, DIP_4S, 0, 0},
5239 /*2*/   {VENDOR_BN, "HFC-2S Card (mini PCI)", 4, 2, 1, 3, 0, DIP_4S, 0, 0},
5240 /*3*/   {VENDOR_BN, "HFC-4S Card", 4, 4, 1, 2, 0, DIP_4S, 0, 0},
5241 /*4*/   {VENDOR_BN, "HFC-4S Card (mini PCI)", 4, 4, 1, 2, 0, 0, 0, 0},
5242 /*5*/   {VENDOR_CCD, "HFC-4S Eval (old)", 4, 4, 0, 0, 0, 0, 0, 0},
5243 /*6*/   {VENDOR_CCD, "HFC-4S IOB4ST", 4, 4, 1, 2, 0, DIP_4S, 0, 0},
5244 /*7*/   {VENDOR_CCD, "HFC-4S", 4, 4, 1, 2, 0, 0, 0, 0},
5245 /*8*/   {VENDOR_DIG, "HFC-4S Card", 4, 4, 0, 2, 0, 0, HFC_IO_MODE_REGIO, 0},
5246 /*9*/   {VENDOR_CCD, "HFC-4S Swyx 4xS0 SX2 QuadBri", 4, 4, 1, 2, 0, 0, 0, 0},
5247 /*10*/  {VENDOR_JH, "HFC-4S (junghanns 2.0)", 4, 4, 1, 2, 0, 0, 0, 0},
5248 /*11*/  {VENDOR_PRIM, "HFC-2S Primux Card", 4, 2, 0, 0, 0, 0, 0, 0},
5249
5250 /*12*/  {VENDOR_BN, "HFC-8S Card", 8, 8, 1, 0, 0, 0, 0, 0},
5251 /*13*/  {VENDOR_BN, "HFC-8S Card (+)", 8, 8, 1, 8, 0, DIP_8S,
5252                 HFC_IO_MODE_REGIO, 0},
5253 /*14*/  {VENDOR_CCD, "HFC-8S Eval (old)", 8, 8, 0, 0, 0, 0, 0, 0},
5254 /*15*/  {VENDOR_CCD, "HFC-8S IOB4ST Recording", 8, 8, 1, 0, 0, 0, 0, 0},
5255
5256 /*16*/  {VENDOR_CCD, "HFC-8S IOB8ST", 8, 8, 1, 0, 0, 0, 0, 0},
5257 /*17*/  {VENDOR_CCD, "HFC-8S", 8, 8, 1, 0, 0, 0, 0, 0},
5258 /*18*/  {VENDOR_CCD, "HFC-8S", 8, 8, 1, 0, 0, 0, 0, 0},
5259
5260 /*19*/  {VENDOR_BN, "HFC-E1 Card", 1, 1, 0, 1, 0, DIP_E1, 0, 0},
5261 /*20*/  {VENDOR_BN, "HFC-E1 Card (mini PCI)", 1, 1, 0, 1, 0, 0, 0, 0},
5262 /*21*/  {VENDOR_BN, "HFC-E1+ Card (Dual)", 1, 1, 0, 1, 0, DIP_E1, 0, 0},
5263 /*22*/  {VENDOR_BN, "HFC-E1 Card (Dual)", 1, 1, 0, 1, 0, DIP_E1, 0, 0},
5264
5265 /*23*/  {VENDOR_CCD, "HFC-E1 Eval (old)", 1, 1, 0, 0, 0, 0, 0, 0},
5266 /*24*/  {VENDOR_CCD, "HFC-E1 IOB1E1", 1, 1, 0, 1, 0, 0, 0, 0},
5267 /*25*/  {VENDOR_CCD, "HFC-E1", 1, 1, 0, 1, 0, 0, 0, 0},
5268
5269 /*26*/  {VENDOR_CCD, "HFC-4S Speech Design", 4, 4, 0, 0, 0, 0,
5270                 HFC_IO_MODE_PLXSD, 0},
5271 /*27*/  {VENDOR_CCD, "HFC-E1 Speech Design", 1, 1, 0, 0, 0, 0,
5272                 HFC_IO_MODE_PLXSD, 0},
5273 /*28*/  {VENDOR_CCD, "HFC-4S OpenVox", 4, 4, 1, 0, 0, 0, 0, 0},
5274 /*29*/  {VENDOR_CCD, "HFC-2S OpenVox", 4, 2, 1, 0, 0, 0, 0, 0},
5275 /*30*/  {VENDOR_CCD, "HFC-8S OpenVox", 8, 8, 1, 0, 0, 0, 0, 0},
5276 /*31*/  {VENDOR_CCD, "XHFC-4S Speech Design", 5, 4, 0, 0, 0, 0,
5277                 HFC_IO_MODE_EMBSD, XHFC_IRQ},
5278 /*32*/  {VENDOR_JH, "HFC-8S (junghanns)", 8, 8, 1, 0, 0, 0, 0, 0},
5279 };
5280
5281 #undef H
5282 #define H(x)    ((unsigned long)&hfcm_map[x])
5283 static struct pci_device_id hfmultipci_ids[] __devinitdata = {
5284
5285         /* Cards with HFC-4S Chip */
5286         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5287                 PCI_SUBDEVICE_ID_CCD_BN1SM, 0, 0, H(0)}, /* BN1S mini PCI */
5288         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5289                 PCI_SUBDEVICE_ID_CCD_BN2S, 0, 0, H(1)}, /* BN2S */
5290         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5291                 PCI_SUBDEVICE_ID_CCD_BN2SM, 0, 0, H(2)}, /* BN2S mini PCI */
5292         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5293                 PCI_SUBDEVICE_ID_CCD_BN4S, 0, 0, H(3)}, /* BN4S */
5294         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5295                 PCI_SUBDEVICE_ID_CCD_BN4SM, 0, 0, H(4)}, /* BN4S mini PCI */
5296         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5297                 PCI_DEVICE_ID_CCD_HFC4S, 0, 0, H(5)}, /* Old Eval */
5298         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5299                 PCI_SUBDEVICE_ID_CCD_IOB4ST, 0, 0, H(6)}, /* IOB4ST */
5300         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5301                 PCI_SUBDEVICE_ID_CCD_HFC4S, 0, 0, H(7)}, /* 4S */
5302         { PCI_VENDOR_ID_DIGIUM, PCI_DEVICE_ID_DIGIUM_HFC4S,
5303                 PCI_VENDOR_ID_DIGIUM, PCI_DEVICE_ID_DIGIUM_HFC4S, 0, 0, H(8)},
5304         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5305                 PCI_SUBDEVICE_ID_CCD_SWYX4S, 0, 0, H(9)}, /* 4S Swyx */
5306         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5307                 PCI_SUBDEVICE_ID_CCD_JH4S20, 0, 0, H(10)},
5308         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5309                 PCI_SUBDEVICE_ID_CCD_PMX2S, 0, 0, H(11)}, /* Primux */
5310         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5311                 PCI_SUBDEVICE_ID_CCD_OV4S, 0, 0, H(28)}, /* OpenVox 4 */
5312         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_VENDOR_ID_CCD,
5313                 PCI_SUBDEVICE_ID_CCD_OV2S, 0, 0, H(29)}, /* OpenVox 2 */
5314
5315         /* Cards with HFC-8S Chip */
5316         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5317         PCI_SUBDEVICE_ID_CCD_BN8S, 0, 0, H(12)}, /* BN8S */
5318         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5319         PCI_SUBDEVICE_ID_CCD_BN8SP, 0, 0, H(13)}, /* BN8S+ */
5320         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5321         PCI_DEVICE_ID_CCD_HFC8S, 0, 0, H(14)}, /* old Eval */
5322         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5323         PCI_SUBDEVICE_ID_CCD_IOB8STR, 0, 0, H(15)}, /* IOB8ST Recording */
5324         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5325                 PCI_SUBDEVICE_ID_CCD_IOB8ST, 0, 0, H(16)}, /* IOB8ST  */
5326         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5327                 PCI_SUBDEVICE_ID_CCD_IOB8ST_1, 0, 0, H(17)}, /* IOB8ST  */
5328         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5329                 PCI_SUBDEVICE_ID_CCD_HFC8S, 0, 0, H(18)}, /* 8S */
5330         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5331                 PCI_SUBDEVICE_ID_CCD_OV8S, 0, 0, H(30)}, /* OpenVox 8 */
5332         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_VENDOR_ID_CCD,
5333                 PCI_SUBDEVICE_ID_CCD_JH8S, 0, 0, H(32)}, /* Junganns 8S  */
5334
5335
5336         /* Cards with HFC-E1 Chip */
5337         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5338                 PCI_SUBDEVICE_ID_CCD_BNE1, 0, 0, H(19)}, /* BNE1 */
5339         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5340                 PCI_SUBDEVICE_ID_CCD_BNE1M, 0, 0, H(20)}, /* BNE1 mini PCI */
5341         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5342                 PCI_SUBDEVICE_ID_CCD_BNE1DP, 0, 0, H(21)}, /* BNE1 + (Dual) */
5343         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5344                 PCI_SUBDEVICE_ID_CCD_BNE1D, 0, 0, H(22)}, /* BNE1 (Dual) */
5345
5346         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5347                 PCI_DEVICE_ID_CCD_HFCE1, 0, 0, H(23)}, /* Old Eval */
5348         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5349                 PCI_SUBDEVICE_ID_CCD_IOB1E1, 0, 0, H(24)}, /* IOB1E1 */
5350         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_VENDOR_ID_CCD,
5351                 PCI_SUBDEVICE_ID_CCD_HFCE1, 0, 0, H(25)}, /* E1 */
5352
5353         { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_CCD,
5354                 PCI_SUBDEVICE_ID_CCD_SPD4S, 0, 0, H(26)}, /* PLX PCI Bridge */
5355         { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_CCD,
5356                 PCI_SUBDEVICE_ID_CCD_SPDE1, 0, 0, H(27)}, /* PLX PCI Bridge */
5357         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC4S, PCI_ANY_ID, PCI_ANY_ID,
5358                 0, 0, 0},
5359         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFC8S, PCI_ANY_ID, PCI_ANY_ID,
5360                 0, 0, 0},
5361         { PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_HFCE1, PCI_ANY_ID, PCI_ANY_ID,
5362                 0, 0, 0},
5363         {0, }
5364 };
5365 #undef H
5366
5367 MODULE_DEVICE_TABLE(pci, hfmultipci_ids);
5368
5369 static int
5370 hfcmulti_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5371 {
5372         struct hm_map   *m = (struct hm_map *)ent->driver_data;
5373         int             ret;
5374
5375         if (m == NULL && ent->vendor == PCI_VENDOR_ID_CCD && (
5376             ent->device == PCI_DEVICE_ID_CCD_HFC4S ||
5377             ent->device == PCI_DEVICE_ID_CCD_HFC8S ||
5378             ent->device == PCI_DEVICE_ID_CCD_HFCE1)) {
5379                 printk(KERN_ERR
5380                     "Unknown HFC multiport controller (vendor:%x device:%x "
5381                     "subvendor:%x subdevice:%x)\n", ent->vendor, ent->device,
5382                     ent->subvendor, ent->subdevice);
5383                 printk(KERN_ERR
5384                     "Please contact the driver maintainer for support.\n");
5385                 return -ENODEV;
5386         }
5387         ret = hfcmulti_init(m, pdev, ent);
5388         if (ret)
5389                 return ret;
5390         HFC_cnt++;
5391         printk(KERN_INFO "%d devices registered\n", HFC_cnt);
5392         return 0;
5393 }
5394
5395 static struct pci_driver hfcmultipci_driver = {
5396         .name           = "hfc_multi",
5397         .probe          = hfcmulti_probe,
5398         .remove         = __devexit_p(hfc_remove_pci),
5399         .id_table       = hfmultipci_ids,
5400 };
5401
5402 static void __exit
5403 HFCmulti_cleanup(void)
5404 {
5405         struct hfc_multi *card, *next;
5406
5407         /* get rid of all devices of this driver */
5408         list_for_each_entry_safe(card, next, &HFClist, list)
5409                 release_card(card);
5410         pci_unregister_driver(&hfcmultipci_driver);
5411 }
5412
5413 static int __init
5414 HFCmulti_init(void)
5415 {
5416         int err;
5417         int i, xhfc = 0;
5418         struct hm_map m;
5419
5420         printk(KERN_INFO "mISDN: HFC-multi driver %s\n", HFC_MULTI_VERSION);
5421
5422 #ifdef IRQ_DEBUG
5423         printk(KERN_DEBUG "%s: IRQ_DEBUG IS ENABLED!\n", __func__);
5424 #endif
5425
5426         spin_lock_init(&HFClock);
5427         spin_lock_init(&plx_lock);
5428
5429         if (debug & DEBUG_HFCMULTI_INIT)
5430                 printk(KERN_DEBUG "%s: init entered\n", __func__);
5431
5432         switch (poll) {
5433         case 0:
5434                 poll_timer = 6;
5435                 poll = 128;
5436                 break;
5437         case 8:
5438                 poll_timer = 2;
5439                 break;
5440         case 16:
5441                 poll_timer = 3;
5442                 break;
5443         case 32:
5444                 poll_timer = 4;
5445                 break;
5446         case 64:
5447                 poll_timer = 5;
5448                 break;
5449         case 128:
5450                 poll_timer = 6;
5451                 break;
5452         case 256:
5453                 poll_timer = 7;
5454                 break;
5455         default:
5456                 printk(KERN_ERR
5457                     "%s: Wrong poll value (%d).\n", __func__, poll);
5458                 err = -EINVAL;
5459                 return err;
5460
5461         }
5462
5463         if (!clock)
5464                 clock = 1;
5465
5466         /* Register the embedded devices.
5467          * This should be done before the PCI cards registration */
5468         switch (hwid) {
5469         case HWID_MINIP4:
5470                 xhfc = 1;
5471                 m = hfcm_map[31];
5472                 break;
5473         case HWID_MINIP8:
5474                 xhfc = 2;
5475                 m = hfcm_map[31];
5476                 break;
5477         case HWID_MINIP16:
5478                 xhfc = 4;
5479                 m = hfcm_map[31];
5480                 break;
5481         default:
5482                 xhfc = 0;
5483         }
5484
5485         for (i = 0; i < xhfc; ++i) {
5486                 err = hfcmulti_init(&m, NULL, NULL);
5487                 if (err) {
5488                         printk(KERN_ERR "error registering embedded driver: "
5489                                 "%x\n", err);
5490                         return -err;
5491                 }
5492                 HFC_cnt++;
5493                 printk(KERN_INFO "%d devices registered\n", HFC_cnt);
5494         }
5495
5496         /* Register the PCI cards */
5497         err = pci_register_driver(&hfcmultipci_driver);
5498         if (err < 0) {
5499                 printk(KERN_ERR "error registering pci driver: %x\n", err);
5500                 return err;
5501         }
5502
5503         return 0;
5504 }
5505
5506
5507 module_init(HFCmulti_init);
5508 module_exit(HFCmulti_cleanup);