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