]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/telephony/ixj.c
[PATCH] ixj* - compile warning cleanup
[linux-3.10.git] / drivers / telephony / ixj.c
1 /****************************************************************************
2  *    ixj.c
3  *
4  * Device Driver for Quicknet Technologies, Inc.'s Telephony cards
5  * including the Internet PhoneJACK, Internet PhoneJACK Lite,
6  * Internet PhoneJACK PCI, Internet LineJACK, Internet PhoneCARD and
7  * SmartCABLE
8  *
9  *    (c) Copyright 1999-2001  Quicknet Technologies, Inc.
10  *
11  *    This program is free software; you can redistribute it and/or
12  *    modify it under the terms of the GNU General Public License
13  *    as published by the Free Software Foundation; either version
14  *    2 of the License, or (at your option) any later version.
15  *
16  * Author:          Ed Okerson, <eokerson@quicknet.net>
17  *
18  * Contributors:    Greg Herlein, <gherlein@quicknet.net>
19  *                  David W. Erhart, <derhart@quicknet.net>
20  *                  John Sellers, <jsellers@quicknet.net>
21  *                  Mike Preston, <mpreston@quicknet.net>
22  *    
23  * Fixes:           David Huggins-Daines, <dhd@cepstral.com>
24  *                  Fabio Ferrari, <fabio.ferrari@digitro.com.br>
25  *                  Artis Kugevics, <artis@mt.lv>
26  *                  Daniele Bellucci, <bellucda@tiscali.it>
27  *
28  * More information about the hardware related to this driver can be found  
29  * at our website:    http://www.quicknet.net
30  *
31  * IN NO EVENT SHALL QUICKNET TECHNOLOGIES, INC. BE LIABLE TO ANY PARTY FOR
32  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
33  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF QUICKNET
34  * TECHNOLOGIES, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *    
36  * QUICKNET TECHNOLOGIES, INC. SPECIFICALLY DISCLAIMS ANY WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
38  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
39  * ON AN "AS IS" BASIS, AND QUICKNET TECHNOLOGIES, INC. HAS NO OBLIGATION
40  * TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41  *
42  ***************************************************************************/
43
44 /*
45  * $Log: ixj.c,v $
46  *
47  * Revision 4.8  2003/07/09 19:39:00  Daniele Bellucci
48  * Audit some copy_*_user and minor cleanup.
49  *
50  * Revision 4.7  2001/08/13 06:19:33  craigs
51  * Added additional changes from Alan Cox and John Anderson for
52  * 2.2 to 2.4 cleanup and bounds checking
53  *
54  * Revision 4.6  2001/08/13 01:05:05  craigs
55  * Really fixed PHONE_QUERY_CODEC problem this time
56  *
57  * Revision 4.5  2001/08/13 00:11:03  craigs
58  * Fixed problem in handling of PHONE_QUERY_CODEC, thanks to Shane Anderson
59  *
60  * Revision 4.4  2001/08/07 07:58:12  craigs
61  * Changed back to three digit version numbers
62  * Added tagbuild target to allow automatic and easy tagging of versions
63  *
64  * Revision 4.3  2001/08/07 07:24:47  craigs
65  * Added ixj-ver.h to allow easy configuration management of driver
66  * Added display of version number in /prox/ixj
67  *
68  * Revision 4.2  2001/08/06 07:07:19  craigs
69  * Reverted IXJCTL_DSP_TYPE and IXJCTL_DSP_VERSION files to original
70  * behaviour of returning int rather than short *
71  *
72  * Revision 4.1  2001/08/05 00:17:37  craigs
73  * More changes for correct PCMCIA installation
74  * Start of changes for backward Linux compatibility
75  *
76  * Revision 4.0  2001/08/04 12:33:12  craigs
77  * New version using GNU autoconf
78  *
79  * Revision 3.105  2001/07/20 23:14:32  eokerson
80  * More work on CallerID generation when using ring cadences.
81  *
82  * Revision 3.104  2001/07/06 01:33:55  eokerson
83  * Some bugfixes from Robert Vojta <vojta@ipex.cz> and a few mods to the Makefile.
84  *
85  * Revision 3.103  2001/07/05 19:20:16  eokerson
86  * Updated HOWTO
87  * Changed mic gain to 30dB on Internet LineJACK mic/speaker port.
88  *
89  * Revision 3.102  2001/07/03 23:51:21  eokerson
90  * Un-mute mic on Internet LineJACK when in speakerphone mode.
91  *
92  * Revision 3.101  2001/07/02 19:26:56  eokerson
93  * Removed initialiazation of ixjdebug and ixj_convert_loaded so they will go in the .bss instead of the .data
94  *
95  * Revision 3.100  2001/07/02 19:18:27  eokerson
96  * Changed driver to make dynamic allocation possible.  We now pass IXJ * between functions instead of array indexes.
97  * Fixed the way the POTS and PSTN ports interact during a PSTN call to allow local answering.
98  * Fixed speaker mode on Internet LineJACK.
99  *
100  * Revision 3.99  2001/05/09 14:11:16  eokerson
101  * Fixed kmalloc error in ixj_build_filter_cadence.  Thanks David Chan <cat@waulogy.stanford.edu>.
102  *
103  * Revision 3.98  2001/05/08 19:55:33  eokerson
104  * Fixed POTS hookstate detection while it is connected to PSTN port.
105  *
106  * Revision 3.97  2001/05/08 00:01:04  eokerson
107  * Fixed kernel oops when sending caller ID data.
108  *
109  * Revision 3.96  2001/05/04 23:09:30  eokerson
110  * Now uses one kernel timer for each card, instead of one for the entire driver.
111  *
112  * Revision 3.95  2001/04/25 22:06:47  eokerson
113  * Fixed squawking at beginning of some G.723.1 calls.
114  *
115  * Revision 3.94  2001/04/03 23:42:00  eokerson
116  * Added linear volume ioctls
117  * Added raw filter load ioctl
118  *
119  * Revision 3.93  2001/02/27 01:00:06  eokerson
120  * Fixed blocking in CallerID.
121  * Reduced size of ixj structure for smaller driver footprint.
122  *
123  * Revision 3.92  2001/02/20 22:02:59  eokerson
124  * Fixed isapnp and pcmcia module compatibility for 2.4.x kernels.
125  * Improved PSTN ring detection.
126  * Fixed wink generation on POTS ports.
127  *
128  * Revision 3.91  2001/02/13 00:55:44  eokerson
129  * Turn AEC back on after changing frame sizes.
130  *
131  * Revision 3.90  2001/02/12 16:42:00  eokerson
132  * Added ALAW codec, thanks to Fabio Ferrari for the table based converters to make ALAW from ULAW.
133  *
134  * Revision 3.89  2001/02/12 15:41:16  eokerson
135  * Fix from Artis Kugevics - Tone gains were not being set correctly.
136  *
137  * Revision 3.88  2001/02/05 23:25:42  eokerson
138  * Fixed lockup bugs with deregister.
139  *
140  * Revision 3.87  2001/01/29 21:00:39  eokerson
141  * Fix from Fabio Ferrari <fabio.ferrari@digitro.com.br> to properly handle EAGAIN and EINTR during non-blocking write.
142  * Updated copyright date.
143  *
144  * Revision 3.86  2001/01/23 23:53:46  eokerson
145  * Fixes to G.729 compatibility.
146  *
147  * Revision 3.85  2001/01/23 21:30:36  eokerson
148  * Added verbage about cards supported.
149  * Removed commands that put the card in low power mode at some times that it should not be in low power mode.
150  *
151  * Revision 3.84  2001/01/22 23:32:10  eokerson
152  * Some bugfixes from David Huggins-Daines, <dhd@cepstral.com> and other cleanups.
153  *
154  * Revision 3.83  2001/01/19 14:51:41  eokerson
155  * Fixed ixj_WriteDSPCommand to decrement usage counter when command fails.
156  *
157  * Revision 3.82  2001/01/19 00:34:49  eokerson
158  * Added verbosity to write overlap errors.
159  *
160  * Revision 3.81  2001/01/18 23:56:54  eokerson
161  * Fixed PSTN line test functions.
162  *
163  * Revision 3.80  2001/01/18 22:29:27  eokerson
164  * Updated AEC/AGC values for different cards.
165  *
166  * Revision 3.79  2001/01/17 02:58:54  eokerson
167  * Fixed AEC reset after Caller ID.
168  * Fixed Codec lockup after Caller ID on Call Waiting when not using 30ms frames.
169  *
170  * Revision 3.78  2001/01/16 19:43:09  eokerson
171  * Added support for Linux 2.4.x kernels.
172  *
173  * Revision 3.77  2001/01/09 04:00:52  eokerson
174  * Linetest will now test the line, even if it has previously succeded.
175  *
176  * Revision 3.76  2001/01/08 19:27:00  eokerson
177  * Fixed problem with standard cable on Internet PhoneCARD.
178  *
179  * Revision 3.75  2000/12/22 16:52:14  eokerson
180  * Modified to allow hookstate detection on the POTS port when the PSTN port is selected.
181  *
182  * Revision 3.74  2000/12/08 22:41:50  eokerson
183  * Added capability for G729B.
184  *
185  * Revision 3.73  2000/12/07 23:35:16  eokerson
186  * Added capability to have different ring pattern before CallerID data.
187  * Added hookstate checks in CallerID routines to stop FSK.
188  *
189  * Revision 3.72  2000/12/06 19:31:31  eokerson
190  * Modified signal behavior to only send one signal per event.
191  *
192  * Revision 3.71  2000/12/06 03:23:08  eokerson
193  * Fixed CallerID on Call Waiting.
194  *
195  * Revision 3.70  2000/12/04 21:29:37  eokerson
196  * Added checking to Smart Cable gain functions.
197  *
198  * Revision 3.69  2000/12/04 21:05:20  eokerson
199  * Changed ixjdebug levels.
200  * Added ioctls to change gains in Internet Phone CARD Smart Cable.
201  *
202  * Revision 3.68  2000/12/04 00:17:21  craigs
203  * Changed mixer voice gain to +6dB rather than 0dB
204  *
205  * Revision 3.67  2000/11/30 21:25:51  eokerson
206  * Fixed write signal errors.
207  *
208  * Revision 3.66  2000/11/29 22:42:44  eokerson
209  * Fixed PSTN ring detect problems.
210  *
211  * Revision 3.65  2000/11/29 07:31:55  craigs
212  * Added new 425Hz filter co-efficients
213  * Added card-specific DTMF prescaler initialisation
214  *
215  * Revision 3.64  2000/11/28 14:03:32  craigs
216  * Changed certain mixer initialisations to be 0dB rather than 12dB
217  * Added additional information to /proc/ixj
218  *
219  * Revision 3.63  2000/11/28 11:38:41  craigs
220  * Added display of AEC modes in AUTO and AGC mode
221  *
222  * Revision 3.62  2000/11/28 04:05:44  eokerson
223  * Improved PSTN ring detection routine.
224  *
225  * Revision 3.61  2000/11/27 21:53:12  eokerson
226  * Fixed flash detection.
227  *
228  * Revision 3.60  2000/11/27 15:57:29  eokerson
229  * More work on G.729 load routines.
230  *
231  * Revision 3.59  2000/11/25 21:55:12  eokerson
232  * Fixed errors in G.729 load routine.
233  *
234  * Revision 3.58  2000/11/25 04:08:29  eokerson
235  * Added board locks around G.729 and TS85 load routines.
236  *
237  * Revision 3.57  2000/11/24 05:35:17  craigs
238  * Added ability to retrieve mixer values on LineJACK
239  * Added complete initialisation of all mixer values at startup
240  * Fixed spelling mistake
241  *
242  * Revision 3.56  2000/11/23 02:52:11  robertj
243  * Added cvs change log keyword.
244  * Fixed bug in capabilities list when using G.729 module.
245  *
246  */
247
248 #include "ixj-ver.h"
249
250 #define PERFMON_STATS
251 #define IXJDEBUG 0
252 #define MAXRINGS 5
253
254 #include <linux/module.h>
255
256 #include <linux/init.h>
257 #include <linux/sched.h>
258 #include <linux/kernel.h>       /* printk() */
259 #include <linux/fs.h>           /* everything... */
260 #include <linux/errno.h>        /* error codes */
261 #include <linux/slab.h>
262 #include <linux/mm.h>
263 #include <linux/ioport.h>
264 #include <linux/interrupt.h>
265 #include <linux/proc_fs.h>
266 #include <linux/poll.h>
267 #include <linux/timer.h>
268 #include <linux/delay.h>
269 #include <linux/pci.h>
270
271 #include <asm/io.h>
272 #include <asm/uaccess.h>
273
274 #include <linux/isapnp.h>
275
276 #include "ixj.h"
277
278 #define TYPE(inode) (iminor(inode) >> 4)
279 #define NUM(inode) (iminor(inode) & 0xf)
280
281 static int ixjdebug;
282 static int hertz = HZ;
283 static int samplerate = 100;
284
285 module_param(ixjdebug, int, 0);
286
287 /************************************************************************
288 *
289 * ixjdebug meanings are now bit mapped instead of level based
290 * Values can be or'ed together to turn on multiple messages
291 *
292 * bit  0 (0x0001) = any failure
293 * bit  1 (0x0002) = general messages
294 * bit  2 (0x0004) = POTS ringing related
295 * bit  3 (0x0008) = PSTN events
296 * bit  4 (0x0010) = PSTN Cadence state details
297 * bit  5 (0x0020) = Tone detection triggers
298 * bit  6 (0x0040) = Tone detection cadence details
299 * bit  7 (0x0080) = ioctl tracking
300 * bit  8 (0x0100) = signal tracking
301 * bit  9 (0x0200) = CallerID generation details
302 *
303 ************************************************************************/
304
305 #ifdef IXJ_DYN_ALLOC
306
307 static IXJ *ixj[IXJMAX];
308 #define get_ixj(b)      ixj[(b)]
309
310 /*
311  *      Allocate a free IXJ device
312  */
313  
314 static IXJ *ixj_alloc()
315 {
316         for(cnt=0; cnt<IXJMAX; cnt++)
317         {
318                 if(ixj[cnt] == NULL || !ixj[cnt]->DSPbase)
319                 {
320                         j = kmalloc(sizeof(IXJ), GFP_KERNEL);
321                         if (j == NULL)
322                                 return NULL;
323                         ixj[cnt] = j;
324                         return j;
325                 }
326         }
327         return NULL;
328 }
329
330 static void ixj_fsk_free(IXJ *j)
331 {
332         if(j->fskdata != NULL) {
333                 kfree(j->fskdata);
334                 j->fskdata = NULL;
335         }
336 }
337
338 static void ixj_fsk_alloc(IXJ *j)
339 {
340         if(!j->fskdata) {
341                 j->fskdata = kmalloc(8000, GFP_KERNEL);
342                 if (!j->fskdata) {
343                         if(ixjdebug & 0x0200) {
344                                 printk("IXJ phone%d - allocate failed\n", j->board);
345                         }
346                         return;
347                 } else {
348                         j->fsksize = 8000;
349                         if(ixjdebug & 0x0200) {
350                                 printk("IXJ phone%d - allocate succeded\n", j->board);
351                         }
352                 }
353         }
354 }
355
356 #else
357
358 static IXJ ixj[IXJMAX];
359 #define get_ixj(b)      (&ixj[(b)])
360
361 /*
362  *      Allocate a free IXJ device
363  */
364  
365 static IXJ *ixj_alloc(void)
366 {
367         int cnt;
368         for(cnt=0; cnt<IXJMAX; cnt++) {
369                 if(!ixj[cnt].DSPbase)
370                         return &ixj[cnt];
371         }
372         return NULL;
373 }
374
375 static inline void ixj_fsk_free(IXJ *j) {;}
376
377 static inline void ixj_fsk_alloc(IXJ *j)
378 {
379         j->fsksize = 8000;
380 }
381
382 #endif
383
384 #ifdef PERFMON_STATS
385 #define ixj_perfmon(x)  ((x)++)
386 #else
387 #define ixj_perfmon(x)  do { } while(0)
388 #endif
389
390 static int ixj_convert_loaded;
391
392 static int ixj_WriteDSPCommand(unsigned short, IXJ *j);
393
394 /************************************************************************
395 *
396 * These are function definitions to allow external modules to register
397 * enhanced functionality call backs.
398 *
399 ************************************************************************/
400
401 static int Stub(IXJ * J, unsigned long arg)
402 {
403         return 0;
404 }
405
406 static IXJ_REGFUNC ixj_PreRead = &Stub;
407 static IXJ_REGFUNC ixj_PostRead = &Stub;
408 static IXJ_REGFUNC ixj_PreWrite = &Stub;
409 static IXJ_REGFUNC ixj_PostWrite = &Stub;
410
411 static void ixj_read_frame(IXJ *j);
412 static void ixj_write_frame(IXJ *j);
413 static void ixj_init_timer(IXJ *j);
414 static void ixj_add_timer(IXJ * j);
415 static void ixj_timeout(unsigned long ptr);
416 static int read_filters(IXJ *j);
417 static int LineMonitor(IXJ *j);
418 static int ixj_fasync(int fd, struct file *, int mode);
419 static int ixj_set_port(IXJ *j, int arg);
420 static int ixj_set_pots(IXJ *j, int arg);
421 static int ixj_hookstate(IXJ *j);
422 static int ixj_record_start(IXJ *j);
423 static void ixj_record_stop(IXJ *j);
424 static void set_rec_volume(IXJ *j, int volume);
425 static int get_rec_volume(IXJ *j);
426 static int set_rec_codec(IXJ *j, int rate);
427 static void ixj_vad(IXJ *j, int arg);
428 static int ixj_play_start(IXJ *j);
429 static void ixj_play_stop(IXJ *j);
430 static int ixj_set_tone_on(unsigned short arg, IXJ *j);
431 static int ixj_set_tone_off(unsigned short, IXJ *j);
432 static int ixj_play_tone(IXJ *j, char tone);
433 static void ixj_aec_start(IXJ *j, int level);
434 static int idle(IXJ *j);
435 static void ixj_ring_on(IXJ *j);
436 static void ixj_ring_off(IXJ *j);
437 static void aec_stop(IXJ *j);
438 static void ixj_ringback(IXJ *j);
439 static void ixj_busytone(IXJ *j);
440 static void ixj_dialtone(IXJ *j);
441 static void ixj_cpt_stop(IXJ *j);
442 static char daa_int_read(IXJ *j);
443 static char daa_CR_read(IXJ *j, int cr);
444 static int daa_set_mode(IXJ *j, int mode);
445 static int ixj_linetest(IXJ *j);
446 static int ixj_daa_write(IXJ *j);
447 static int ixj_daa_cid_read(IXJ *j);
448 static void DAA_Coeff_US(IXJ *j);
449 static void DAA_Coeff_UK(IXJ *j);
450 static void DAA_Coeff_France(IXJ *j);
451 static void DAA_Coeff_Germany(IXJ *j);
452 static void DAA_Coeff_Australia(IXJ *j);
453 static void DAA_Coeff_Japan(IXJ *j);
454 static int ixj_init_filter(IXJ *j, IXJ_FILTER * jf);
455 static int ixj_init_filter_raw(IXJ *j, IXJ_FILTER_RAW * jfr);
456 static int ixj_init_tone(IXJ *j, IXJ_TONE * ti);
457 static int ixj_build_cadence(IXJ *j, IXJ_CADENCE __user * cp);
458 static int ixj_build_filter_cadence(IXJ *j, IXJ_FILTER_CADENCE __user * cp);
459 /* Serial Control Interface funtions */
460 static int SCI_Control(IXJ *j, int control);
461 static int SCI_Prepare(IXJ *j);
462 static int SCI_WaitHighSCI(IXJ *j);
463 static int SCI_WaitLowSCI(IXJ *j);
464 static DWORD PCIEE_GetSerialNumber(WORD wAddress);
465 static int ixj_PCcontrol_wait(IXJ *j);
466 static void ixj_pre_cid(IXJ *j);
467 static void ixj_write_cid(IXJ *j);
468 static void ixj_write_cid_bit(IXJ *j, int bit);
469 static int set_base_frame(IXJ *j, int size);
470 static int set_play_codec(IXJ *j, int rate);
471 static void set_rec_depth(IXJ *j, int depth);
472 static int ixj_mixer(long val, IXJ *j);
473
474 /************************************************************************
475 CT8020/CT8021 Host Programmers Model
476 Host address    Function                                        Access
477 DSPbase +
478 0-1             Aux Software Status Register (reserved)         Read Only
479 2-3             Software Status Register                        Read Only
480 4-5             Aux Software Control Register (reserved)        Read Write
481 6-7             Software Control Register                       Read Write
482 8-9             Hardware Status Register                        Read Only
483 A-B             Hardware Control Register                       Read Write
484 C-D Host Transmit (Write) Data Buffer Access Port (buffer input)Write Only
485 E-F Host Recieve (Read) Data Buffer Access Port (buffer input)  Read Only
486 ************************************************************************/
487
488 static inline void ixj_read_HSR(IXJ *j)
489 {
490         j->hsr.bytes.low = inb_p(j->DSPbase + 8);
491         j->hsr.bytes.high = inb_p(j->DSPbase + 9);
492 }
493
494 static inline int IsControlReady(IXJ *j)
495 {
496         ixj_read_HSR(j);
497         return j->hsr.bits.controlrdy ? 1 : 0;
498 }
499
500 static inline int IsPCControlReady(IXJ *j)
501 {
502         j->pccr1.byte = inb_p(j->XILINXbase + 3);
503         return j->pccr1.bits.crr ? 1 : 0;
504 }
505
506 static inline int IsStatusReady(IXJ *j)
507 {
508         ixj_read_HSR(j);
509         return j->hsr.bits.statusrdy ? 1 : 0;
510 }
511
512 static inline int IsRxReady(IXJ *j)
513 {
514         ixj_read_HSR(j);
515         ixj_perfmon(j->rxreadycheck);
516         return j->hsr.bits.rxrdy ? 1 : 0;
517 }
518
519 static inline int IsTxReady(IXJ *j)
520 {
521         ixj_read_HSR(j);
522         ixj_perfmon(j->txreadycheck);
523         return j->hsr.bits.txrdy ? 1 : 0;
524 }
525
526 static inline void set_play_volume(IXJ *j, int volume)
527 {
528         if (ixjdebug & 0x0002)
529                 printk(KERN_INFO "IXJ: /dev/phone%d Setting Play Volume to 0x%4.4x\n", j->board, volume);
530         ixj_WriteDSPCommand(0xCF02, j);
531         ixj_WriteDSPCommand(volume, j);
532 }
533
534 static int set_play_volume_linear(IXJ *j, int volume)
535 {
536         int newvolume, dspplaymax;
537
538         if (ixjdebug & 0x0002)
539                 printk(KERN_INFO "IXJ: /dev/phone %d Setting Linear Play Volume to 0x%4.4x\n", j->board, volume);
540         if(volume > 100 || volume < 0) {
541                 return -1;
542         }
543
544         /* This should normalize the perceived volumes between the different cards caused by differences in the hardware */
545         switch (j->cardtype) {
546         case QTI_PHONEJACK:
547                 dspplaymax = 0x380;
548                 break;
549         case QTI_LINEJACK:
550                 if(j->port == PORT_PSTN) {
551                         dspplaymax = 0x48;
552                 } else {
553                         dspplaymax = 0x100;
554                 }
555                 break;
556         case QTI_PHONEJACK_LITE:
557                 dspplaymax = 0x380;
558                 break;
559         case QTI_PHONEJACK_PCI:
560                 dspplaymax = 0x6C;
561                 break;
562         case QTI_PHONECARD:
563                 dspplaymax = 0x50;
564                 break;
565         default:
566                 return -1;
567         }
568         newvolume = (dspplaymax * volume) / 100;
569         set_play_volume(j, newvolume);
570         return 0;
571 }
572
573 static inline void set_play_depth(IXJ *j, int depth)
574 {
575         if (depth > 60)
576                 depth = 60;
577         if (depth < 0)
578                 depth = 0;
579         ixj_WriteDSPCommand(0x5280 + depth, j);
580 }
581
582 static inline int get_play_volume(IXJ *j)
583 {
584         ixj_WriteDSPCommand(0xCF00, j);
585         return j->ssr.high << 8 | j->ssr.low;
586 }
587
588 static int get_play_volume_linear(IXJ *j)
589 {
590         int volume, newvolume, dspplaymax;
591
592         /* This should normalize the perceived volumes between the different cards caused by differences in the hardware */
593         switch (j->cardtype) {
594         case QTI_PHONEJACK:
595                 dspplaymax = 0x380;
596                 break;
597         case QTI_LINEJACK:
598                 if(j->port == PORT_PSTN) {
599                         dspplaymax = 0x48;
600                 } else {
601                         dspplaymax = 0x100;
602                 }
603                 break;
604         case QTI_PHONEJACK_LITE:
605                 dspplaymax = 0x380;
606                 break;
607         case QTI_PHONEJACK_PCI:
608                 dspplaymax = 0x6C;
609                 break;
610         case QTI_PHONECARD:
611                 dspplaymax = 100;
612                 break;
613         default:
614                 return -1;
615         }
616         volume = get_play_volume(j);
617         newvolume = (volume * 100) / dspplaymax;
618         if(newvolume > 100)
619                 newvolume = 100;
620         return newvolume;
621 }
622
623 static inline BYTE SLIC_GetState(IXJ *j)
624 {
625         if (j->cardtype == QTI_PHONECARD) {
626                 j->pccr1.byte = 0;
627                 j->psccr.bits.dev = 3;
628                 j->psccr.bits.rw = 1;
629                 outw_p(j->psccr.byte << 8, j->XILINXbase + 0x00);
630                 ixj_PCcontrol_wait(j);
631                 j->pslic.byte = inw_p(j->XILINXbase + 0x00) & 0xFF;
632                 ixj_PCcontrol_wait(j);
633                 if (j->pslic.bits.powerdown)
634                         return PLD_SLIC_STATE_OC;
635                 else if (!j->pslic.bits.ring0 && !j->pslic.bits.ring1)
636                         return PLD_SLIC_STATE_ACTIVE;
637                 else
638                         return PLD_SLIC_STATE_RINGING;
639         } else {
640                 j->pld_slicr.byte = inb_p(j->XILINXbase + 0x01);
641         }
642         return j->pld_slicr.bits.state;
643 }
644
645 static BOOL SLIC_SetState(BYTE byState, IXJ *j)
646 {
647         BOOL fRetVal = FALSE;
648
649         if (j->cardtype == QTI_PHONECARD) {
650                 if (j->flags.pcmciasct) {
651                         switch (byState) {
652                         case PLD_SLIC_STATE_TIPOPEN:
653                         case PLD_SLIC_STATE_OC:
654                                 j->pslic.bits.powerdown = 1;
655                                 j->pslic.bits.ring0 = j->pslic.bits.ring1 = 0;
656                                 fRetVal = TRUE;
657                                 break;
658                         case PLD_SLIC_STATE_RINGING:
659                                 if (j->readers || j->writers) {
660                                         j->pslic.bits.powerdown = 0;
661                                         j->pslic.bits.ring0 = 1;
662                                         j->pslic.bits.ring1 = 0;
663                                         fRetVal = TRUE;
664                                 }
665                                 break;
666                         case PLD_SLIC_STATE_OHT:        /* On-hook transmit */
667
668                         case PLD_SLIC_STATE_STANDBY:
669                         case PLD_SLIC_STATE_ACTIVE:
670                                 if (j->readers || j->writers) {
671                                         j->pslic.bits.powerdown = 0;
672                                 } else {
673                                         j->pslic.bits.powerdown = 1;
674                                 }
675                                 j->pslic.bits.ring0 = j->pslic.bits.ring1 = 0;
676                                 fRetVal = TRUE;
677                                 break;
678                         case PLD_SLIC_STATE_APR:        /* Active polarity reversal */
679
680                         case PLD_SLIC_STATE_OHTPR:      /* OHT polarity reversal */
681
682                         default:
683                                 fRetVal = FALSE;
684                                 break;
685                         }
686                         j->psccr.bits.dev = 3;
687                         j->psccr.bits.rw = 0;
688                         outw_p(j->psccr.byte << 8 | j->pslic.byte, j->XILINXbase + 0x00);
689                         ixj_PCcontrol_wait(j);
690                 }
691         } else {
692                 /* Set the C1, C2, C3 & B2EN signals. */
693                 switch (byState) {
694                 case PLD_SLIC_STATE_OC:
695                         j->pld_slicw.bits.c1 = 0;
696                         j->pld_slicw.bits.c2 = 0;
697                         j->pld_slicw.bits.c3 = 0;
698                         j->pld_slicw.bits.b2en = 0;
699                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
700                         fRetVal = TRUE;
701                         break;
702                 case PLD_SLIC_STATE_RINGING:
703                         j->pld_slicw.bits.c1 = 1;
704                         j->pld_slicw.bits.c2 = 0;
705                         j->pld_slicw.bits.c3 = 0;
706                         j->pld_slicw.bits.b2en = 1;
707                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
708                         fRetVal = TRUE;
709                         break;
710                 case PLD_SLIC_STATE_ACTIVE:
711                         j->pld_slicw.bits.c1 = 0;
712                         j->pld_slicw.bits.c2 = 1;
713                         j->pld_slicw.bits.c3 = 0;
714                         j->pld_slicw.bits.b2en = 0;
715                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
716                         fRetVal = TRUE;
717                         break;
718                 case PLD_SLIC_STATE_OHT:        /* On-hook transmit */
719
720                         j->pld_slicw.bits.c1 = 1;
721                         j->pld_slicw.bits.c2 = 1;
722                         j->pld_slicw.bits.c3 = 0;
723                         j->pld_slicw.bits.b2en = 0;
724                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
725                         fRetVal = TRUE;
726                         break;
727                 case PLD_SLIC_STATE_TIPOPEN:
728                         j->pld_slicw.bits.c1 = 0;
729                         j->pld_slicw.bits.c2 = 0;
730                         j->pld_slicw.bits.c3 = 1;
731                         j->pld_slicw.bits.b2en = 0;
732                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
733                         fRetVal = TRUE;
734                         break;
735                 case PLD_SLIC_STATE_STANDBY:
736                         j->pld_slicw.bits.c1 = 1;
737                         j->pld_slicw.bits.c2 = 0;
738                         j->pld_slicw.bits.c3 = 1;
739                         j->pld_slicw.bits.b2en = 1;
740                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
741                         fRetVal = TRUE;
742                         break;
743                 case PLD_SLIC_STATE_APR:        /* Active polarity reversal */
744
745                         j->pld_slicw.bits.c1 = 0;
746                         j->pld_slicw.bits.c2 = 1;
747                         j->pld_slicw.bits.c3 = 1;
748                         j->pld_slicw.bits.b2en = 0;
749                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
750                         fRetVal = TRUE;
751                         break;
752                 case PLD_SLIC_STATE_OHTPR:      /* OHT polarity reversal */
753
754                         j->pld_slicw.bits.c1 = 1;
755                         j->pld_slicw.bits.c2 = 1;
756                         j->pld_slicw.bits.c3 = 1;
757                         j->pld_slicw.bits.b2en = 0;
758                         outb_p(j->pld_slicw.byte, j->XILINXbase + 0x01);
759                         fRetVal = TRUE;
760                         break;
761                 default:
762                         fRetVal = FALSE;
763                         break;
764                 }
765         }
766
767         return fRetVal;
768 }
769
770 static int ixj_wink(IXJ *j)
771 {
772         BYTE slicnow;
773
774         slicnow = SLIC_GetState(j);
775
776         j->pots_winkstart = jiffies;
777         SLIC_SetState(PLD_SLIC_STATE_OC, j);
778
779         while (time_before(jiffies, j->pots_winkstart + j->winktime)) {
780                 set_current_state(TASK_INTERRUPTIBLE);
781                 schedule_timeout(1);
782         }
783
784         SLIC_SetState(slicnow, j);
785         return 0;
786 }
787
788 static void ixj_init_timer(IXJ *j)
789 {
790         init_timer(&j->timer);
791         j->timer.function = ixj_timeout;
792         j->timer.data = (unsigned long)j;
793 }
794
795 static void ixj_add_timer(IXJ *j)
796 {
797         j->timer.expires = jiffies + (hertz / samplerate);
798         add_timer(&j->timer);
799 }
800
801 static void ixj_tone_timeout(IXJ *j)
802 {
803         IXJ_TONE ti;
804
805         j->tone_state++;
806         if (j->tone_state == 3) {
807                 j->tone_state = 0;
808                 if (j->cadence_t) {
809                         j->tone_cadence_state++;
810                         if (j->tone_cadence_state >= j->cadence_t->elements_used) {
811                                 switch (j->cadence_t->termination) {
812                                 case PLAY_ONCE:
813                                         ixj_cpt_stop(j);
814                                         break;
815                                 case REPEAT_LAST_ELEMENT:
816                                         j->tone_cadence_state--;
817                                         ixj_play_tone(j, j->cadence_t->ce[j->tone_cadence_state].index);
818                                         break;
819                                 case REPEAT_ALL:
820                                         j->tone_cadence_state = 0;
821                                         if (j->cadence_t->ce[j->tone_cadence_state].freq0) {
822                                                 ti.tone_index = j->cadence_t->ce[j->tone_cadence_state].index;
823                                                 ti.freq0 = j->cadence_t->ce[j->tone_cadence_state].freq0;
824                                                 ti.gain0 = j->cadence_t->ce[j->tone_cadence_state].gain0;
825                                                 ti.freq1 = j->cadence_t->ce[j->tone_cadence_state].freq1;
826                                                 ti.gain1 = j->cadence_t->ce[j->tone_cadence_state].gain1;
827                                                 ixj_init_tone(j, &ti);
828                                         }
829                                         ixj_set_tone_on(j->cadence_t->ce[0].tone_on_time, j);
830                                         ixj_set_tone_off(j->cadence_t->ce[0].tone_off_time, j);
831                                         ixj_play_tone(j, j->cadence_t->ce[0].index);
832                                         break;
833                                 }
834                         } else {
835                                 if (j->cadence_t->ce[j->tone_cadence_state].gain0) {
836                                         ti.tone_index = j->cadence_t->ce[j->tone_cadence_state].index;
837                                         ti.freq0 = j->cadence_t->ce[j->tone_cadence_state].freq0;
838                                         ti.gain0 = j->cadence_t->ce[j->tone_cadence_state].gain0;
839                                         ti.freq1 = j->cadence_t->ce[j->tone_cadence_state].freq1;
840                                         ti.gain1 = j->cadence_t->ce[j->tone_cadence_state].gain1;
841                                         ixj_init_tone(j, &ti);
842                                 }
843                                 ixj_set_tone_on(j->cadence_t->ce[j->tone_cadence_state].tone_on_time, j);
844                                 ixj_set_tone_off(j->cadence_t->ce[j->tone_cadence_state].tone_off_time, j);
845                                 ixj_play_tone(j, j->cadence_t->ce[j->tone_cadence_state].index);
846                         }
847                 }
848         }
849 }
850
851 static inline void ixj_kill_fasync(IXJ *j, IXJ_SIGEVENT event, int dir)
852 {
853         if(j->ixj_signals[event]) {
854                 if(ixjdebug & 0x0100)
855                         printk("Sending signal for event %d\n", event);
856                         /* Send apps notice of change */
857                 /* see config.h for macro definition */
858                 kill_fasync(&(j->async_queue), j->ixj_signals[event], dir);
859         }
860 }
861
862 static void ixj_pstn_state(IXJ *j)
863 {
864         int var;
865         union XOPXR0 XR0, daaint;
866
867         var = 10;
868
869         XR0.reg = j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.reg;
870         daaint.reg = 0;
871         XR0.bitreg.RMR = j->m_DAAShadowRegs.SOP_REGS.SOP.cr1.bitreg.RMR;
872
873         j->pld_scrr.byte = inb_p(j->XILINXbase);
874         if (j->pld_scrr.bits.daaflag) {
875                 daa_int_read(j);
876                 if(j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.bitreg.RING) {
877                         if(time_after(jiffies, j->pstn_sleeptil) && !(j->flags.pots_pstn && j->hookstate)) {
878                                 daaint.bitreg.RING = 1;
879                                 if(ixjdebug & 0x0008) {
880                                         printk(KERN_INFO "IXJ DAA Ring Interrupt /dev/phone%d at %ld\n", j->board, jiffies);
881                                 }
882                         } else {
883                                 daa_set_mode(j, SOP_PU_RESET);
884                         }
885                 }
886                 if(j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.bitreg.Caller_ID) {
887                         daaint.bitreg.Caller_ID = 1;
888                         j->pstn_cid_intr = 1;
889                         j->pstn_cid_received = jiffies;
890                         if(ixjdebug & 0x0008) {
891                                 printk(KERN_INFO "IXJ DAA Caller_ID Interrupt /dev/phone%d at %ld\n", j->board, jiffies);
892                         }
893                 }
894                 if(j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.bitreg.Cadence) {
895                         daaint.bitreg.Cadence = 1;
896                         if(ixjdebug & 0x0008) {
897                                 printk(KERN_INFO "IXJ DAA Cadence Interrupt /dev/phone%d at %ld\n", j->board, jiffies);
898                         }
899                 }
900                 if(j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.bitreg.VDD_OK != XR0.bitreg.VDD_OK) {
901                         daaint.bitreg.VDD_OK = 1;
902                         daaint.bitreg.SI_0 = j->m_DAAShadowRegs.XOP_REGS.XOP.xr0.bitreg.VDD_OK;
903                 }
904         }
905         daa_CR_read(j, 1);
906         if(j->m_DAAShadowRegs.SOP_REGS.SOP.cr1.bitreg.RMR != XR0.bitreg.RMR && time_after(jiffies, j->pstn_sleeptil) && !(j->flags.pots_pstn && j->hookstate)) {
907                 daaint.bitreg.RMR = 1;
908                 daaint.bitreg.SI_1 = j->m_DAAShadowRegs.SOP_REGS.SOP.cr1.bitreg.RMR;
909                 if(ixjdebug & 0x0008) {
910                         printk(KERN_INFO "IXJ DAA RMR /dev/phone%d was %s for %ld\n", j->board, XR0.bitreg.RMR?"on":"off", jiffies - j->pstn_last_rmr);
911                 }
912                 j->pstn_prev_rmr = j->pstn_last_rmr;
913                 j->pstn_last_rmr = jiffies;
914         }
915         switch(j->daa_mode) {
916                 case SOP_PU_SLEEP:
917                         if (daaint.bitreg.RING) {
918                                 if (!j->flags.pstn_ringing) {
919                                         if (j->daa_mode != SOP_PU_RINGING) {
920                                                 j->pstn_ring_int = jiffies;
921                                                 daa_set_mode(j, SOP_PU_RINGING);
922                                         }
923                                 }
924                         }
925                         break;
926                 case SOP_PU_RINGING:
927                         if (daaint.bitreg.RMR) {
928                                 if (ixjdebug & 0x0008) {
929                                         printk(KERN_INFO "IXJ Ring Cadence a state = %d /dev/phone%d at %ld\n", j->cadence_f[4].state, j->board, jiffies);
930                                 }
931                                 if (daaint.bitreg.SI_1) {                /* Rising edge of RMR */
932                                         j->flags.pstn_rmr = 1;
933                                         j->pstn_ring_start = jiffies;
934                                         j->pstn_ring_stop = 0;
935                                         j->ex.bits.pstn_ring = 0;
936                                         if (j->cadence_f[4].state == 0) {
937                                                 j->cadence_f[4].state = 1;
938                                                 j->cadence_f[4].on1min = jiffies + (long)((j->cadence_f[4].on1 * hertz * (100 - var)) / 10000);
939                                                 j->cadence_f[4].on1dot = jiffies + (long)((j->cadence_f[4].on1 * hertz * (100)) / 10000);
940                                                 j->cadence_f[4].on1max = jiffies + (long)((j->cadence_f[4].on1 * hertz * (100 + var)) / 10000);
941                                         } else if (j->cadence_f[4].state == 2) {
942                                                 if((time_after(jiffies, j->cadence_f[4].off1min) &&
943                                                     time_before(jiffies, j->cadence_f[4].off1max))) {
944                                                         if (j->cadence_f[4].on2) {
945                                                                 j->cadence_f[4].state = 3;
946                                                                 j->cadence_f[4].on2min = jiffies + (long)((j->cadence_f[4].on2 * (hertz * (100 - var)) / 10000));
947                                                                 j->cadence_f[4].on2dot = jiffies + (long)((j->cadence_f[4].on2 * (hertz * (100)) / 10000));
948                                                                 j->cadence_f[4].on2max = jiffies + (long)((j->cadence_f[4].on2 * (hertz * (100 + var)) / 10000));
949                                                         } else {
950                                                                 j->cadence_f[4].state = 7;
951                                                         }
952                                                 } else {
953                                                         if (ixjdebug & 0x0008) {
954                                                                 printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
955                                                                                 j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
956                                                                                 j->cadence_f[4].off1);
957                                                         }
958                                                         j->cadence_f[4].state = 0;
959                                                 }
960                                         } else if (j->cadence_f[4].state == 4) {
961                                                 if((time_after(jiffies, j->cadence_f[4].off2min) &&
962                                                     time_before(jiffies, j->cadence_f[4].off2max))) {
963                                                         if (j->cadence_f[4].on3) {
964                                                                 j->cadence_f[4].state = 5;
965                                                                 j->cadence_f[4].on3min = jiffies + (long)((j->cadence_f[4].on3 * (hertz * (100 - var)) / 10000));
966                                                                 j->cadence_f[4].on3dot = jiffies + (long)((j->cadence_f[4].on3 * (hertz * (100)) / 10000));
967                                                                 j->cadence_f[4].on3max = jiffies + (long)((j->cadence_f[4].on3 * (hertz * (100 + var)) / 10000));
968                                                         } else {
969                                                                 j->cadence_f[4].state = 7;
970                                                         }
971                                                 } else {
972                                                         if (ixjdebug & 0x0008) {
973                                                                 printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
974                                                                                 j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
975                                                                                 j->cadence_f[4].off2);
976                                                         }
977                                                         j->cadence_f[4].state = 0;
978                                                 }
979                                         } else if (j->cadence_f[4].state == 6) {
980                                                 if((time_after(jiffies, j->cadence_f[4].off3min) &&
981                                                     time_before(jiffies, j->cadence_f[4].off3max))) {
982                                                         j->cadence_f[4].state = 7;
983                                                 } else {
984                                                         if (ixjdebug & 0x0008) {
985                                                                 printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
986                                                                                 j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
987                                                                                 j->cadence_f[4].off3);
988                                                         }
989                                                         j->cadence_f[4].state = 0;
990                                                 }
991                                         } else {
992                                                 j->cadence_f[4].state = 0;
993                                         }
994                                 } else {                                /* Falling edge of RMR */
995                                         j->pstn_ring_start = 0;
996                                         j->pstn_ring_stop = jiffies;
997                                         if (j->cadence_f[4].state == 1) {
998                                                 if(!j->cadence_f[4].on1) {
999                                                         j->cadence_f[4].state = 7;
1000                                                 } else if((time_after(jiffies, j->cadence_f[4].on1min) &&
1001                                                   time_before(jiffies, j->cadence_f[4].on1max))) {
1002                                                         if (j->cadence_f[4].off1) {
1003                                                                 j->cadence_f[4].state = 2;
1004                                                                 j->cadence_f[4].off1min = jiffies + (long)((j->cadence_f[4].off1 * (hertz * (100 - var)) / 10000));
1005                                                                 j->cadence_f[4].off1dot = jiffies + (long)((j->cadence_f[4].off1 * (hertz * (100)) / 10000));
1006                                                                 j->cadence_f[4].off1max = jiffies + (long)((j->cadence_f[4].off1 * (hertz * (100 + var)) / 10000));
1007                                                         } else {
1008                                                                 j->cadence_f[4].state = 7;
1009                                                         }
1010                                                 } else {
1011                                                         if (ixjdebug & 0x0008) {
1012                                                                 printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
1013                                                                                 j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
1014                                                                                 j->cadence_f[4].on1);
1015                                                         }
1016                                                         j->cadence_f[4].state = 0;
1017                                                 }
1018                                         } else if (j->cadence_f[4].state == 3) {
1019                                                 if((time_after(jiffies, j->cadence_f[4].on2min) &&
1020                                                     time_before(jiffies, j->cadence_f[4].on2max))) {
1021                                                         if (j->cadence_f[4].off2) {
1022                                                                 j->cadence_f[4].state = 4;
1023                                                                 j->cadence_f[4].off2min = jiffies + (long)((j->cadence_f[4].off2 * (hertz * (100 - var)) / 10000));
1024                                                                 j->cadence_f[4].off2dot = jiffies + (long)((j->cadence_f[4].off2 * (hertz * (100)) / 10000));
1025                                                                 j->cadence_f[4].off2max = jiffies + (long)((j->cadence_f[4].off2 * (hertz * (100 + var)) / 10000));
1026                                                         } else {
1027                                                                 j->cadence_f[4].state = 7;
1028                                                         }
1029                                                 } else {
1030                                                         if (ixjdebug & 0x0008) {
1031                                                                 printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
1032                                                                                 j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
1033                                                                                 j->cadence_f[4].on2);
1034                                                         }
1035                                                         j->cadence_f[4].state = 0;
1036                                                 }
1037                                         } else if (j->cadence_f[4].state == 5) {
1038                                                 if((time_after(jiffies, j->cadence_f[4].on3min) &&
1039                                                     time_before(jiffies, j->cadence_f[4].on3max))) {
1040                                                         if (j->cadence_f[4].off3) {
1041                                                                 j->cadence_f[4].state = 6;
1042                                                                 j->cadence_f[4].off3min = jiffies + (long)((j->cadence_f[4].off3 * (hertz * (100 - var)) / 10000));
1043                                                                 j->cadence_f[4].off3dot = jiffies + (long)((j->cadence_f[4].off3 * (hertz * (100)) / 10000));
1044                                                                 j->cadence_f[4].off3max = jiffies + (long)((j->cadence_f[4].off3 * (hertz * (100 + var)) / 10000));
1045                                                         } else {
1046                                                                 j->cadence_f[4].state = 7;
1047                                                         }
1048                                                 } else {
1049                                                         j->cadence_f[4].state = 0;
1050                                                 }
1051                                         } else {
1052                                                 if (ixjdebug & 0x0008) {
1053                                                         printk(KERN_INFO "IXJ Ring Cadence fail state = %d /dev/phone%d at %ld should be %d\n",
1054                                                                         j->cadence_f[4].state, j->board, jiffies - j->pstn_prev_rmr,
1055                                                                         j->cadence_f[4].on3);
1056                                                 }
1057                                                 j->cadence_f[4].state = 0;
1058                                         }
1059                                 }
1060                                 if (ixjdebug & 0x0010) {
1061                                         printk(KERN_INFO "IXJ Ring Cadence b state = %d /dev/phone%d at %ld\n", j->cadence_f[4].state, j->board, jiffies);
1062                                 }
1063                                 if (ixjdebug & 0x0010) {
1064                                         switch(j->cadence_f[4].state) {
1065                                                 case 1:
1066                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1067                                                 j->cadence_f[4].on1, j->cadence_f[4].on1min, j->cadence_f[4].on1dot, j->cadence_f[4].on1max);
1068                                                         break;
1069                                                 case 2:
1070                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1071                                                 j->cadence_f[4].off1, j->cadence_f[4].off1min, j->cadence_f[4].off1dot, j->cadence_f[4].off1max);
1072                                                         break;
1073                                                 case 3:
1074                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1075                                                 j->cadence_f[4].on2, j->cadence_f[4].on2min, j->cadence_f[4].on2dot, j->cadence_f[4].on2max);
1076                                                         break;
1077                                                 case 4:
1078                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1079                                                 j->cadence_f[4].off2, j->cadence_f[4].off2min, j->cadence_f[4].off2dot, j->cadence_f[4].off2max);
1080                                                         break;
1081                                                 case 5:
1082                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1083                                                 j->cadence_f[4].on3, j->cadence_f[4].on3min, j->cadence_f[4].on3dot, j->cadence_f[4].on3max);
1084                                                         break;
1085                                                 case 6: 
1086                                                         printk(KERN_INFO "IXJ /dev/phone%d Next Ring Cadence state at %u min %ld - %ld - max %ld\n", j->board,
1087                                                 j->cadence_f[4].off3, j->cadence_f[4].off3min, j->cadence_f[4].off3dot, j->cadence_f[4].off3max);
1088                                                         break;
1089                                         }
1090                                 }
1091                         }
1092                         if (j->cadence_f[4].state == 7) {
1093                                 j->cadence_f[4].state = 0;
1094                                 j->pstn_ring_stop = jiffies;
1095                                 j->ex.bits.pstn_ring = 1;
1096                                 ixj_kill_fasync(j, SIG_PSTN_RING, POLL_IN);
1097                                 if(ixjdebug & 0x0008) {
1098                                         printk(KERN_INFO "IXJ Ring int set /dev/phone%d at %ld\n", j->board, jiffies);
1099                                 }
1100                         }
1101                         if((j->pstn_ring_int != 0 && time_after(jiffies, j->pstn_ring_int + (hertz * 5)) && !j->flags.pstn_rmr) ||
1102                            (j->pstn_ring_stop != 0 && time_after(jiffies, j->pstn_ring_stop + (hertz * 5)))) {
1103                                 if(ixjdebug & 0x0008) {
1104                                         printk("IXJ DAA no ring in 5 seconds /dev/phone%d at %ld\n", j->board, jiffies);
1105                                         printk("IXJ DAA pstn ring int /dev/phone%d at %ld\n", j->board, j->pstn_ring_int);
1106                                         printk("IXJ DAA pstn ring stop /dev/phone%d at %ld\n", j->board, j->pstn_ring_stop);
1107                                 }
1108                                 j->pstn_ring_stop = j->pstn_ring_int = 0;
1109                                 daa_set_mode(j, SOP_PU_SLEEP);
1110                         } 
1111                         outb_p(j->pld_scrw.byte, j->XILINXbase);
1112                         if (j->pstn_cid_intr && time_after(jiffies, j->pstn_cid_received + hertz)) {
1113                                 ixj_daa_cid_read(j);
1114                                 j->ex.bits.caller_id = 1;
1115                                 ixj_kill_fasync(j, SIG_CALLER_ID, POLL_IN);
1116                                 j->pstn_cid_intr = 0;
1117                         }
1118                         if (daaint.bitreg.Cadence) {
1119                                 if(ixjdebug & 0x0008) {
1120                                         printk("IXJ DAA Cadence interrupt going to sleep /dev/phone%d\n", j->board);
1121                                 }
1122                                 daa_set_mode(j, SOP_PU_SLEEP);
1123                                 j->ex.bits.pstn_ring = 0;
1124                         }
1125                         break;
1126                 case SOP_PU_CONVERSATION:
1127                         if (daaint.bitreg.VDD_OK) {
1128                                 if(!daaint.bitreg.SI_0) {
1129                                         if (!j->pstn_winkstart) {
1130                                                 if(ixjdebug & 0x0008) {
1131                                                         printk("IXJ DAA possible wink /dev/phone%d %ld\n", j->board, jiffies);
1132                                                 }
1133                                                 j->pstn_winkstart = jiffies;
1134                                         } 
1135                                 } else {
1136                                         if (j->pstn_winkstart) {
1137                                                 if(ixjdebug & 0x0008) {
1138                                                         printk("IXJ DAA possible wink end /dev/phone%d %ld\n", j->board, jiffies);
1139                                                 }
1140                                                 j->pstn_winkstart = 0;
1141                                         }
1142                                 }
1143                         }
1144                         if (j->pstn_winkstart && time_after(jiffies, j->pstn_winkstart + ((hertz * j->winktime) / 1000))) {
1145                                 if(ixjdebug & 0x0008) {
1146                                         printk("IXJ DAA wink detected going to sleep /dev/phone%d %ld\n", j->board, jiffies);
1147                                 }
1148                                 daa_set_mode(j, SOP_PU_SLEEP);
1149                                 j->pstn_winkstart = 0;
1150                                 j->ex.bits.pstn_wink = 1;
1151                                 ixj_kill_fasync(j, SIG_PSTN_WINK, POLL_IN);
1152                         }
1153                         break;
1154         }
1155 }
1156
1157 static void ixj_timeout(unsigned long ptr)
1158 {
1159         int board;
1160         unsigned long jifon;
1161         IXJ *j = (IXJ *)ptr;
1162         board = j->board;
1163
1164         if (j->DSPbase && atomic_read(&j->DSPWrite) == 0 && test_and_set_bit(board, (void *)&j->busyflags) == 0) {
1165                 ixj_perfmon(j->timerchecks);
1166                 j->hookstate = ixj_hookstate(j);
1167                 if (j->tone_state) {
1168                         if (!(j->hookstate)) {
1169                                 ixj_cpt_stop(j);
1170                                 if (j->m_hook) {
1171                                         j->m_hook = 0;
1172                                         j->ex.bits.hookstate = 1;
1173                                         ixj_kill_fasync(j, SIG_HOOKSTATE, POLL_IN);
1174                                 }
1175                                 clear_bit(board, &j->busyflags);
1176                                 ixj_add_timer(j);
1177                                 return;
1178                         }
1179                         if (j->tone_state == 1)
1180                                 jifon = ((hertz * j->tone_on_time) * 25 / 100000);
1181                         else
1182                                 jifon = ((hertz * j->tone_on_time) * 25 / 100000) + ((hertz * j->tone_off_time) * 25 / 100000);
1183                         if (time_before(jiffies, j->tone_start_jif + jifon)) {
1184                                 if (j->tone_state == 1) {
1185                                         ixj_play_tone(j, j->tone_index);
1186                                         if (j->dsp.low == 0x20) {
1187                                                 clear_bit(board, &j->busyflags);
1188                                                 ixj_add_timer(j);
1189                                                 return;
1190                                         }
1191                                 } else {
1192                                         ixj_play_tone(j, 0);
1193                                         if (j->dsp.low == 0x20) {
1194                                                 clear_bit(board, &j->busyflags);
1195                                                 ixj_add_timer(j);
1196                                                 return;
1197                                         }
1198                                 }
1199                         } else {
1200                                 ixj_tone_timeout(j);
1201                                 if (j->flags.dialtone) {
1202                                         ixj_dialtone(j);
1203                                 }
1204                                 if (j->flags.busytone) {
1205                                         ixj_busytone(j);
1206                                         if (j->dsp.low == 0x20) {
1207                                                 clear_bit(board, &j->busyflags);
1208                                                 ixj_add_timer(j);
1209                                                 return;
1210                                         }
1211                                 }
1212                                 if (j->flags.ringback) {
1213                                         ixj_ringback(j);
1214                                         if (j->dsp.low == 0x20) {
1215                                                 clear_bit(board, &j->busyflags);
1216                                                 ixj_add_timer(j);
1217                                                 return;
1218                                         }
1219                                 }
1220                                 if (!j->tone_state) {
1221                                         ixj_cpt_stop(j);
1222                                 }
1223                         }
1224                 }
1225                 if (!(j->tone_state && j->dsp.low == 0x20)) {
1226                         if (IsRxReady(j)) {
1227                                 ixj_read_frame(j);
1228                         }
1229                         if (IsTxReady(j)) {
1230                                 ixj_write_frame(j);
1231                         }
1232                 }
1233                 if (j->flags.cringing) {
1234                         if (j->hookstate & 1) {
1235                                 j->flags.cringing = 0;
1236                                 ixj_ring_off(j);
1237                         } else if(j->cadence_f[5].enable && ((!j->cadence_f[5].en_filter) || (j->cadence_f[5].en_filter && j->flags.firstring))) {
1238                                 switch(j->cadence_f[5].state) {
1239                                         case 0:
1240                                                 j->cadence_f[5].on1dot = jiffies + (long)((j->cadence_f[5].on1 * (hertz * 100) / 10000));
1241                                                 if (time_before(jiffies, j->cadence_f[5].on1dot)) {
1242                                                         if(ixjdebug & 0x0004) {
1243                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1244                                                         }
1245                                                         ixj_ring_on(j);
1246                                                 }
1247                                                 j->cadence_f[5].state = 1;
1248                                                 break;
1249                                         case 1:
1250                                                 if (time_after(jiffies, j->cadence_f[5].on1dot)) {
1251                                                         j->cadence_f[5].off1dot = jiffies + (long)((j->cadence_f[5].off1 * (hertz * 100) / 10000));
1252                                                         if(ixjdebug & 0x0004) {
1253                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1254                                                         }
1255                                                         ixj_ring_off(j);
1256                                                         j->cadence_f[5].state = 2;
1257                                                 }
1258                                                 break;
1259                                         case 2:
1260                                                 if (time_after(jiffies, j->cadence_f[5].off1dot)) {
1261                                                         if(ixjdebug & 0x0004) {
1262                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1263                                                         }
1264                                                         ixj_ring_on(j);
1265                                                         if (j->cadence_f[5].on2) {
1266                                                                 j->cadence_f[5].on2dot = jiffies + (long)((j->cadence_f[5].on2 * (hertz * 100) / 10000));
1267                                                                 j->cadence_f[5].state = 3;
1268                                                         } else {
1269                                                                 j->cadence_f[5].state = 7;
1270                                                         }
1271                                                 }
1272                                                 break;
1273                                         case 3:
1274                                                 if (time_after(jiffies, j->cadence_f[5].on2dot)) {
1275                                                         if(ixjdebug & 0x0004) {
1276                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1277                                                         }
1278                                                         ixj_ring_off(j);
1279                                                         if (j->cadence_f[5].off2) {
1280                                                                 j->cadence_f[5].off2dot = jiffies + (long)((j->cadence_f[5].off2 * (hertz * 100) / 10000));
1281                                                                 j->cadence_f[5].state = 4;
1282                                                         } else {
1283                                                                 j->cadence_f[5].state = 7;
1284                                                         }
1285                                                 }
1286                                                 break;
1287                                         case 4:
1288                                                 if (time_after(jiffies, j->cadence_f[5].off2dot)) {
1289                                                         if(ixjdebug & 0x0004) {
1290                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1291                                                         }
1292                                                         ixj_ring_on(j);
1293                                                         if (j->cadence_f[5].on3) {
1294                                                                 j->cadence_f[5].on3dot = jiffies + (long)((j->cadence_f[5].on3 * (hertz * 100) / 10000));
1295                                                                 j->cadence_f[5].state = 5;
1296                                                         } else {
1297                                                                 j->cadence_f[5].state = 7;
1298                                                         }
1299                                                 }
1300                                                 break;
1301                                         case 5:
1302                                                 if (time_after(jiffies, j->cadence_f[5].on3dot)) {
1303                                                         if(ixjdebug & 0x0004) {
1304                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1305                                                         }
1306                                                         ixj_ring_off(j);
1307                                                         if (j->cadence_f[5].off3) {
1308                                                                 j->cadence_f[5].off3dot = jiffies + (long)((j->cadence_f[5].off3 * (hertz * 100) / 10000));
1309                                                                 j->cadence_f[5].state = 6;
1310                                                         } else {
1311                                                                 j->cadence_f[5].state = 7;
1312                                                         }
1313                                                 }
1314                                                 break;
1315                                         case 6:
1316                                                 if (time_after(jiffies, j->cadence_f[5].off3dot)) {
1317                                                         if(ixjdebug & 0x0004) {
1318                                                                 printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1319                                                         }
1320                                                         j->cadence_f[5].state = 7;
1321                                                 }
1322                                                 break;
1323                                         case 7:
1324                                                 if(ixjdebug & 0x0004) {
1325                                                         printk("Ringing cadence state = %d - %ld\n", j->cadence_f[5].state, jiffies);
1326                                                 }
1327                                                 j->flags.cidring = 1;
1328                                                 j->cadence_f[5].state = 0;
1329                                                 break;
1330                                 }
1331                                 if (j->flags.cidring && !j->flags.cidsent) {
1332                                         j->flags.cidsent = 1;
1333                                         if(j->fskdcnt) {
1334                                                 SLIC_SetState(PLD_SLIC_STATE_OHT, j);
1335                                                 ixj_pre_cid(j);
1336                                         }
1337                                         j->flags.cidring = 0;
1338                                 }
1339                                 clear_bit(board, &j->busyflags);
1340                                 ixj_add_timer(j);
1341                                 return;
1342                         } else {
1343                                 if (time_after(jiffies, j->ring_cadence_jif + (hertz / 2))) {
1344                                         if (j->flags.cidring && !j->flags.cidsent) {
1345                                                 j->flags.cidsent = 1;
1346                                                 if(j->fskdcnt) {
1347                                                         SLIC_SetState(PLD_SLIC_STATE_OHT, j);
1348                                                         ixj_pre_cid(j);
1349                                                 }
1350                                                 j->flags.cidring = 0;
1351                                         }
1352                                         j->ring_cadence_t--;
1353                                         if (j->ring_cadence_t == -1)
1354                                                 j->ring_cadence_t = 15;
1355                                         j->ring_cadence_jif = jiffies;
1356
1357                                         if (j->ring_cadence & 1 << j->ring_cadence_t) {
1358                                                 if(j->flags.cidsent && j->cadence_f[5].en_filter)
1359                                                         j->flags.firstring = 1;
1360                                                 else
1361                                                         ixj_ring_on(j);
1362                                         } else {
1363                                                 ixj_ring_off(j);
1364                                                 if(!j->flags.cidsent)
1365                                                         j->flags.cidring = 1;
1366                                         }
1367                                 }
1368                                 clear_bit(board, &j->busyflags);
1369                                 ixj_add_timer(j);
1370                                 return;
1371                         }
1372                 }
1373                 if (!j->flags.ringing) {
1374                         if (j->hookstate) { /* & 1) { */
1375                                 if (j->dsp.low != 0x20 &&
1376                                     SLIC_GetState(j) != PLD_SLIC_STATE_ACTIVE) {
1377                                         SLIC_SetState(PLD_SLIC_STATE_ACTIVE, j);
1378                                 }
1379                                 LineMonitor(j);
1380                                 read_filters(j);
1381                                 ixj_WriteDSPCommand(0x511B, j);
1382                                 j->proc_load = j->ssr.high << 8 | j->ssr.low;
1383                                 if (!j->m_hook && (j->hookstate & 1)) {
1384                                         j->m_hook = j->ex.bits.hookstate = 1;
1385                                         ixj_kill_fasync(j, SIG_HOOKSTATE, POLL_IN);
1386                                 }
1387                         } else {
1388                                 if (j->ex.bits.dtmf_ready) {
1389                                         j->dtmf_wp = j->dtmf_rp = j->ex.bits.dtmf_ready = 0;
1390                                 }
1391                                 if (j->m_hook) {
1392                                         j->m_hook = 0;
1393                                         j->ex.bits.hookstate = 1;
1394                                         ixj_kill_fasync(j, SIG_HOOKSTATE, POLL_IN);
1395                                 }
1396                         }
1397                 }
1398                 if (j->cardtype == QTI_LINEJACK && !j->flags.pstncheck && j->flags.pstn_present) {
1399                         ixj_pstn_state(j);
1400                 }
1401                 if (j->ex.bytes) {
1402                         wake_up_interruptible(&j->poll_q);      /* Wake any blocked selects */
1403                 }
1404                 clear_bit(board, &j->busyflags);
1405         }
1406         ixj_add_timer(j);
1407 }
1408
1409 static int ixj_status_wait(IXJ *j)
1410 {
1411         unsigned long jif;
1412
1413         jif = jiffies + ((60 * hertz) / 100);
1414         while (!IsStatusReady(j)) {
1415                 ixj_perfmon(j->statuswait);
1416                 if (time_after(jiffies, jif)) {
1417                         ixj_perfmon(j->statuswaitfail);
1418                         return -1;
1419                 }
1420         }
1421         return 0;
1422 }
1423
1424 static int ixj_PCcontrol_wait(IXJ *j)
1425 {
1426         unsigned long jif;
1427
1428         jif = jiffies + ((60 * hertz) / 100);
1429         while (!IsPCControlReady(j)) {
1430                 ixj_perfmon(j->pcontrolwait);
1431                 if (time_after(jiffies, jif)) {
1432                         ixj_perfmon(j->pcontrolwaitfail);
1433                         return -1;
1434                 }
1435         }
1436         return 0;
1437 }
1438
1439 static int ixj_WriteDSPCommand(unsigned short cmd, IXJ *j)
1440 {
1441         BYTES bytes;
1442         unsigned long jif;
1443
1444         atomic_inc(&j->DSPWrite);
1445         if(atomic_read(&j->DSPWrite) > 1) {
1446                 printk("IXJ %d DSP write overlap attempting command 0x%4.4x\n", j->board, cmd);
1447                 return -1;
1448         }
1449         bytes.high = (cmd & 0xFF00) >> 8;
1450         bytes.low = cmd & 0x00FF;
1451         jif = jiffies + ((60 * hertz) / 100);
1452         while (!IsControlReady(j)) {
1453                 ixj_perfmon(j->iscontrolready);
1454                 if (time_after(jiffies, jif)) {
1455                         ixj_perfmon(j->iscontrolreadyfail);
1456                         atomic_dec(&j->DSPWrite);
1457                         if(atomic_read(&j->DSPWrite) > 0) {
1458                                 printk("IXJ %d DSP overlaped command 0x%4.4x during control ready failure.\n", j->board, cmd);
1459                                 while(atomic_read(&j->DSPWrite) > 0) {
1460                                         atomic_dec(&j->DSPWrite);
1461                                 }
1462                         }
1463                         return -1;
1464                 }
1465         }
1466         outb(bytes.low, j->DSPbase + 6);
1467         outb(bytes.high, j->DSPbase + 7);
1468
1469         if (ixj_status_wait(j)) {
1470                 j->ssr.low = 0xFF;
1471                 j->ssr.high = 0xFF;
1472                 atomic_dec(&j->DSPWrite);
1473                 if(atomic_read(&j->DSPWrite) > 0) {
1474                         printk("IXJ %d DSP overlaped command 0x%4.4x during status wait failure.\n", j->board, cmd);
1475                         while(atomic_read(&j->DSPWrite) > 0) {
1476                                 atomic_dec(&j->DSPWrite);
1477                         }
1478                 }
1479                 return -1;
1480         }
1481 /* Read Software Status Register */
1482         j->ssr.low = inb_p(j->DSPbase + 2);
1483         j->ssr.high = inb_p(j->DSPbase + 3);
1484         atomic_dec(&j->DSPWrite);
1485         if(atomic_read(&j->DSPWrite) > 0) {
1486                 printk("IXJ %d DSP overlaped command 0x%4.4x\n", j->board, cmd);
1487                 while(atomic_read(&j->DSPWrite) > 0) {
1488                         atomic_dec(&j->DSPWrite);
1489                 }
1490         }
1491         return 0;
1492 }
1493
1494 /***************************************************************************
1495 *
1496 *  General Purpose IO Register read routine
1497 *
1498 ***************************************************************************/
1499 static inline int ixj_gpio_read(IXJ *j)
1500 {
1501         if (ixj_WriteDSPCommand(0x5143, j))
1502                 return -1;
1503
1504         j->gpio.bytes.low = j->ssr.low;
1505         j->gpio.bytes.high = j->ssr.high;
1506
1507         return 0;
1508 }
1509
1510 static inline void LED_SetState(int state, IXJ *j)
1511 {
1512         if (j->cardtype == QTI_LINEJACK) {
1513                 j->pld_scrw.bits.led1 = state & 0x1 ? 1 : 0;
1514                 j->pld_scrw.bits.led2 = state & 0x2 ? 1 : 0;
1515                 j->pld_scrw.bits.led3 = state & 0x4 ? 1 : 0;
1516                 j->pld_scrw.bits.led4 = state & 0x8 ? 1 : 0;
1517
1518                 outb(j->pld_scrw.byte, j->XILINXbase);
1519         }
1520 }
1521
1522 /*********************************************************************
1523 *  GPIO Pins are configured as follows on the Quicknet Internet
1524 *  PhoneJACK Telephony Cards
1525
1526 * POTS Select        GPIO_6=0 GPIO_7=0
1527 * Mic/Speaker Select GPIO_6=0 GPIO_7=1
1528 * Handset Select     GPIO_6=1 GPIO_7=0
1529 *
1530 * SLIC Active        GPIO_1=0 GPIO_2=1 GPIO_5=0
1531 * SLIC Ringing       GPIO_1=1 GPIO_2=1 GPIO_5=0
1532 * SLIC Open Circuit  GPIO_1=0 GPIO_2=0 GPIO_5=0
1533 *
1534 * Hook Switch changes reported on GPIO_3
1535 *********************************************************************/
1536 static int ixj_set_port(IXJ *j, int arg)
1537 {
1538         if (j->cardtype == QTI_PHONEJACK_LITE) {
1539                 if (arg != PORT_POTS)
1540                         return 10;
1541                 else
1542                         return 0;
1543         }
1544         switch (arg) {
1545         case PORT_POTS:
1546                 j->port = PORT_POTS;
1547                 switch (j->cardtype) {
1548                 case QTI_PHONECARD:
1549                         if (j->flags.pcmciasct == 1)
1550                                 SLIC_SetState(PLD_SLIC_STATE_ACTIVE, j);
1551                         else
1552                                 return 11;
1553                         break;
1554                 case QTI_PHONEJACK_PCI:
1555                         j->pld_slicw.pcib.mic = 0;
1556                         j->pld_slicw.pcib.spk = 0;
1557                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1558                         break;
1559                 case QTI_LINEJACK:
1560                         ixj_set_pots(j, 0);                     /* Disconnect POTS/PSTN relay */
1561                         if (ixj_WriteDSPCommand(0xC528, j))             /* Write CODEC config to
1562                                                                            Software Control Register */
1563                                 return 2;
1564                         j->pld_scrw.bits.daafsyncen = 0;        /* Turn off DAA Frame Sync */
1565
1566                         outb(j->pld_scrw.byte, j->XILINXbase);
1567                         j->pld_clock.byte = 0;
1568                         outb(j->pld_clock.byte, j->XILINXbase + 0x04);
1569                         j->pld_slicw.bits.rly1 = 1;
1570                         j->pld_slicw.bits.spken = 0;
1571                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1572                         ixj_mixer(0x1200, j);   /* Turn Off MIC switch on mixer left */
1573                         ixj_mixer(0x1401, j);   /* Turn On Mono1 switch on mixer left */
1574                         ixj_mixer(0x1300, j);       /* Turn Off MIC switch on mixer right */
1575                         ixj_mixer(0x1501, j);       /* Turn On Mono1 switch on mixer right */
1576                         ixj_mixer(0x0E80, j);   /*Mic mute */
1577                         ixj_mixer(0x0F00, j);   /* Set mono out (SLIC) to 0dB */
1578                         ixj_mixer(0x0080, j);   /* Mute Master Left volume */
1579                         ixj_mixer(0x0180, j);   /* Mute Master Right volume */
1580                         SLIC_SetState(PLD_SLIC_STATE_STANDBY, j);
1581 /*                      SLIC_SetState(PLD_SLIC_STATE_ACTIVE, j); */
1582                         break;
1583                 case QTI_PHONEJACK:
1584                         j->gpio.bytes.high = 0x0B;
1585                         j->gpio.bits.gpio6 = 0;
1586                         j->gpio.bits.gpio7 = 0;
1587                         ixj_WriteDSPCommand(j->gpio.word, j);
1588                         break;
1589                 }
1590                 break;
1591         case PORT_PSTN:
1592                 if (j->cardtype == QTI_LINEJACK) {
1593                         ixj_WriteDSPCommand(0xC534, j); /* Write CODEC config to Software Control Register */
1594
1595                         j->pld_slicw.bits.rly3 = 0;
1596                         j->pld_slicw.bits.rly1 = 1;
1597                         j->pld_slicw.bits.spken = 0;
1598                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1599                         j->port = PORT_PSTN;
1600                 } else {
1601                         return 4;
1602                 }
1603                 break;
1604         case PORT_SPEAKER:
1605                 j->port = PORT_SPEAKER;
1606                 switch (j->cardtype) {
1607                 case QTI_PHONECARD:
1608                         if (j->flags.pcmciasct) {
1609                                 SLIC_SetState(PLD_SLIC_STATE_OC, j);
1610                         }
1611                         break;
1612                 case QTI_PHONEJACK_PCI:
1613                         j->pld_slicw.pcib.mic = 1;
1614                         j->pld_slicw.pcib.spk = 1;
1615                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1616                         break;
1617                 case QTI_LINEJACK:
1618                         ixj_set_pots(j, 0);                     /* Disconnect POTS/PSTN relay */
1619                         if (ixj_WriteDSPCommand(0xC528, j))             /* Write CODEC config to
1620                                                                            Software Control Register */
1621                                 return 2;
1622                         j->pld_scrw.bits.daafsyncen = 0;        /* Turn off DAA Frame Sync */
1623
1624                         outb(j->pld_scrw.byte, j->XILINXbase);
1625                         j->pld_clock.byte = 0;
1626                         outb(j->pld_clock.byte, j->XILINXbase + 0x04);
1627                         j->pld_slicw.bits.rly1 = 1;
1628                         j->pld_slicw.bits.spken = 1;
1629                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1630                         ixj_mixer(0x1201, j);   /* Turn On MIC switch on mixer left */
1631                         ixj_mixer(0x1400, j);   /* Turn Off Mono1 switch on mixer left */
1632                         ixj_mixer(0x1301, j);       /* Turn On MIC switch on mixer right */
1633                         ixj_mixer(0x1500, j);       /* Turn Off Mono1 switch on mixer right */
1634                         ixj_mixer(0x0E06, j);   /*Mic un-mute 0dB */
1635                         ixj_mixer(0x0F80, j);   /* Mute mono out (SLIC) */
1636                         ixj_mixer(0x0000, j);   /* Set Master Left volume to 0dB */
1637                         ixj_mixer(0x0100, j);   /* Set Master Right volume to 0dB */
1638                         break;
1639                 case QTI_PHONEJACK:
1640                         j->gpio.bytes.high = 0x0B;
1641                         j->gpio.bits.gpio6 = 0;
1642                         j->gpio.bits.gpio7 = 1;
1643                         ixj_WriteDSPCommand(j->gpio.word, j);
1644                         break;
1645                 }
1646                 break;
1647         case PORT_HANDSET:
1648                 if (j->cardtype != QTI_PHONEJACK) {
1649                         return 5;
1650                 } else {
1651                         j->gpio.bytes.high = 0x0B;
1652                         j->gpio.bits.gpio6 = 1;
1653                         j->gpio.bits.gpio7 = 0;
1654                         ixj_WriteDSPCommand(j->gpio.word, j);
1655                         j->port = PORT_HANDSET;
1656                 }
1657                 break;
1658         default:
1659                 return 6;
1660                 break;
1661         }
1662         return 0;
1663 }
1664
1665 static int ixj_set_pots(IXJ *j, int arg)
1666 {
1667         if (j->cardtype == QTI_LINEJACK) {
1668                 if (arg) {
1669                         if (j->port == PORT_PSTN) {
1670                                 j->pld_slicw.bits.rly1 = 0;
1671                                 outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1672                                 j->flags.pots_pstn = 1;
1673                                 return 1;
1674                         } else {
1675                                 j->flags.pots_pstn = 0;
1676                                 return 0;
1677                         }
1678                 } else {
1679                         j->pld_slicw.bits.rly1 = 1;
1680                         outb(j->pld_slicw.byte, j->XILINXbase + 0x01);
1681                         j->flags.pots_pstn = 0;
1682                         return 1;
1683                 }
1684         } else {
1685                 return 0;
1686         }
1687 }
1688
1689 static void ixj_ring_on(IXJ *j)
1690 {
1691         if (j->dsp.low == 0x20) /* Internet PhoneJACK */
1692          {
1693                 if (ixjdebug & 0x0004)
1694                         printk(KERN_INFO "IXJ Ring On /dev/phone%d\n",  j->board);
1695
1696                 j->gpio.bytes.high = 0x0B;
1697                 j->gpio.bytes.low = 0x00;
1698                 j->gpio.bits.gpio1 = 1;
1699                 j->gpio.bits.gpio2 = 1;
1700                 j->gpio.bits.gpio5 = 0;
1701                 ixj_WriteDSPCommand(j->gpio.word, j);   /* send the ring signal */
1702         } else                  /* Internet LineJACK, Internet PhoneJACK Lite or Internet PhoneJACK PCI */
1703         {
1704                 if (ixjdebug & 0x0004)
1705                         printk(KERN_INFO "IXJ Ring On /dev/phone%d\n", j->board);
1706
1707                 SLIC_SetState(PLD_SLIC_STATE_RINGING, j);
1708         }
1709 }
1710
1711 static int ixj_siadc(IXJ *j, int val)
1712 {
1713         if(j->cardtype == QTI_PHONECARD){
1714                 if(j->flags.pcmciascp){
1715                         if(val == -1)
1716                                 return j->siadc.bits.rxg;
1717
1718                         if(val < 0 || val > 0x1F)
1719                                 return -1;
1720
1721                         j->siadc.bits.hom = 0;                          /* Handset Out Mute */
1722                         j->siadc.bits.lom = 0;                          /* Line Out Mute */
1723                         j->siadc.bits.rxg = val;                        /*(0xC000 - 0x41C8) / 0x4EF;    RX PGA Gain */
1724                         j->psccr.bits.addr = 6;                         /* R/W Smart Cable Register Address */
1725                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1726                         j->psccr.bits.dev = 0;
1727                         outb(j->siadc.byte, j->XILINXbase + 0x00);
1728                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1729                         ixj_PCcontrol_wait(j);
1730                         return j->siadc.bits.rxg;
1731                 }
1732         }
1733         return -1;
1734 }
1735
1736 static int ixj_sidac(IXJ *j, int val)
1737 {
1738         if(j->cardtype == QTI_PHONECARD){
1739                 if(j->flags.pcmciascp){
1740                         if(val == -1)
1741                                 return j->sidac.bits.txg;
1742
1743                         if(val < 0 || val > 0x1F)
1744                                 return -1;
1745
1746                         j->sidac.bits.srm = 1;                          /* Speaker Right Mute */
1747                         j->sidac.bits.slm = 1;                          /* Speaker Left Mute */
1748                         j->sidac.bits.txg = val;                        /* (0xC000 - 0x45E4) / 0x5D3;    TX PGA Gain */
1749                         j->psccr.bits.addr = 7;                         /* R/W Smart Cable Register Address */
1750                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1751                         j->psccr.bits.dev = 0;
1752                         outb(j->sidac.byte, j->XILINXbase + 0x00);
1753                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1754                         ixj_PCcontrol_wait(j);
1755                         return j->sidac.bits.txg;
1756                 }
1757         }
1758         return -1;
1759 }
1760
1761 static int ixj_pcmcia_cable_check(IXJ *j)
1762 {
1763         j->pccr1.byte = inb_p(j->XILINXbase + 0x03);
1764         if (!j->flags.pcmciastate) {
1765                 j->pccr2.byte = inb_p(j->XILINXbase + 0x02);
1766                 if (j->pccr1.bits.drf || j->pccr2.bits.rstc) {
1767                         j->flags.pcmciastate = 4;
1768                         return 0;
1769                 }
1770                 if (j->pccr1.bits.ed) {
1771                         j->pccr1.bits.ed = 0;
1772                         j->psccr.bits.dev = 3;
1773                         j->psccr.bits.rw = 1;
1774                         outw_p(j->psccr.byte << 8, j->XILINXbase + 0x00);
1775                         ixj_PCcontrol_wait(j);
1776                         j->pslic.byte = inw_p(j->XILINXbase + 0x00) & 0xFF;
1777                         j->pslic.bits.led2 = j->pslic.bits.det ? 1 : 0;
1778                         j->psccr.bits.dev = 3;
1779                         j->psccr.bits.rw = 0;
1780                         outw_p(j->psccr.byte << 8 | j->pslic.byte, j->XILINXbase + 0x00);
1781                         ixj_PCcontrol_wait(j);
1782                         return j->pslic.bits.led2 ? 1 : 0;
1783                 } else if (j->flags.pcmciasct) {
1784                         return j->r_hook;
1785                 } else {
1786                         return 1;
1787                 }
1788         } else if (j->flags.pcmciastate == 4) {
1789                 if (!j->pccr1.bits.drf) {
1790                         j->flags.pcmciastate = 3;
1791                 }
1792                 return 0;
1793         } else if (j->flags.pcmciastate == 3) {
1794                 j->pccr2.bits.pwr = 0;
1795                 j->pccr2.bits.rstc = 1;
1796                 outb(j->pccr2.byte, j->XILINXbase + 0x02);
1797                 j->checkwait = jiffies + (hertz * 2);
1798                 j->flags.incheck = 1;
1799                 j->flags.pcmciastate = 2;
1800                 return 0;
1801         } else if (j->flags.pcmciastate == 2) {
1802                 if (j->flags.incheck) {
1803                         if (time_before(jiffies, j->checkwait)) {
1804                                 return 0;
1805                         } else {
1806                                 j->flags.incheck = 0;
1807                         }
1808                 }
1809                 j->pccr2.bits.pwr = 0;
1810                 j->pccr2.bits.rstc = 0;
1811                 outb_p(j->pccr2.byte, j->XILINXbase + 0x02);
1812                 j->flags.pcmciastate = 1;
1813                 return 0;
1814         } else if (j->flags.pcmciastate == 1) {
1815                 j->flags.pcmciastate = 0;
1816                 if (!j->pccr1.bits.drf) {
1817                         j->psccr.bits.dev = 3;
1818                         j->psccr.bits.rw = 1;
1819                         outb_p(j->psccr.byte, j->XILINXbase + 0x01);
1820                         ixj_PCcontrol_wait(j);
1821                         j->flags.pcmciascp = 1;         /* Set Cable Present Flag */
1822
1823                         j->flags.pcmciasct = (inw_p(j->XILINXbase + 0x00) >> 8) & 0x03;         /* Get Cable Type */
1824
1825                         if (j->flags.pcmciasct == 3) {
1826                                 j->flags.pcmciastate = 4;
1827                                 return 0;
1828                         } else if (j->flags.pcmciasct == 0) {
1829                                 j->pccr2.bits.pwr = 1;
1830                                 j->pccr2.bits.rstc = 0;
1831                                 outb_p(j->pccr2.byte, j->XILINXbase + 0x02);
1832                                 j->port = PORT_SPEAKER;
1833                         } else {
1834                                 j->port = PORT_POTS;
1835                         }
1836                         j->sic1.bits.cpd = 0;                           /* Chip Power Down */
1837                         j->sic1.bits.mpd = 0;                           /* MIC Bias Power Down */
1838                         j->sic1.bits.hpd = 0;                           /* Handset Bias Power Down */
1839                         j->sic1.bits.lpd = 0;                           /* Line Bias Power Down */
1840                         j->sic1.bits.spd = 1;                           /* Speaker Drive Power Down */
1841                         j->psccr.bits.addr = 1;                         /* R/W Smart Cable Register Address */
1842                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1843                         j->psccr.bits.dev = 0;
1844                         outb(j->sic1.byte, j->XILINXbase + 0x00);
1845                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1846                         ixj_PCcontrol_wait(j);
1847
1848                         j->sic2.bits.al = 0;                            /* Analog Loopback DAC analog -> ADC analog */
1849                         j->sic2.bits.dl2 = 0;                           /* Digital Loopback DAC -> ADC one bit */
1850                         j->sic2.bits.dl1 = 0;                           /* Digital Loopback ADC -> DAC one bit */
1851                         j->sic2.bits.pll = 0;                           /* 1 = div 10, 0 = div 5 */
1852                         j->sic2.bits.hpd = 0;                           /* HPF disable */
1853                         j->psccr.bits.addr = 2;                         /* R/W Smart Cable Register Address */
1854                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1855                         j->psccr.bits.dev = 0;
1856                         outb(j->sic2.byte, j->XILINXbase + 0x00);
1857                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1858                         ixj_PCcontrol_wait(j);
1859
1860                         j->psccr.bits.addr = 3;                         /* R/W Smart Cable Register Address */
1861                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1862                         j->psccr.bits.dev = 0;
1863                         outb(0x00, j->XILINXbase + 0x00);               /* PLL Divide N1 */
1864                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1865                         ixj_PCcontrol_wait(j);
1866
1867                         j->psccr.bits.addr = 4;                         /* R/W Smart Cable Register Address */
1868                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1869                         j->psccr.bits.dev = 0;
1870                         outb(0x09, j->XILINXbase + 0x00);               /* PLL Multiply M1 */
1871                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1872                         ixj_PCcontrol_wait(j);
1873
1874                         j->sirxg.bits.lig = 1;                          /* Line In Gain */
1875                         j->sirxg.bits.lim = 1;                          /* Line In Mute */
1876                         j->sirxg.bits.mcg = 0;                          /* MIC In Gain was 3 */
1877                         j->sirxg.bits.mcm = 0;                          /* MIC In Mute */
1878                         j->sirxg.bits.him = 0;                          /* Handset In Mute */
1879                         j->sirxg.bits.iir = 1;                          /* IIR */
1880                         j->psccr.bits.addr = 5;                         /* R/W Smart Cable Register Address */
1881                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1882                         j->psccr.bits.dev = 0;
1883                         outb(j->sirxg.byte, j->XILINXbase + 0x00);
1884                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1885                         ixj_PCcontrol_wait(j);
1886
1887                         ixj_siadc(j, 0x17);
1888                         ixj_sidac(j, 0x1D);
1889
1890                         j->siaatt.bits.sot = 0;
1891                         j->psccr.bits.addr = 9;                         /* R/W Smart Cable Register Address */
1892                         j->psccr.bits.rw = 0;                           /* Read / Write flag */
1893                         j->psccr.bits.dev = 0;
1894                         outb(j->siaatt.byte, j->XILINXbase + 0x00);
1895                         outb(j->psccr.byte, j->XILINXbase + 0x01);
1896                         ixj_PCcontrol_wait(j);
1897
1898                         if (j->flags.pcmciasct == 1 && !j->readers && !j->writers) {
1899                                 j->psccr.byte = j->pslic.byte = 0;
1900                                 j->pslic.bits.powerdown = 1;
1901                                 j->psccr.bits.dev = 3;
1902                                 j->psccr.bits.rw = 0;
1903                                 outw_p(j->psccr.byte << 8 | j->pslic.byte, j->XILINXbase + 0x00);
1904                                 ixj_PCcontrol_wait(j);
1905                         }
1906                 }
1907                 return 0;
1908         } else {
1909                 j->flags.pcmciascp = 0;
1910                 return 0;
1911         }
1912         return 0;
1913 }
1914
1915 static int ixj_hookstate(IXJ *j)
1916 {
1917         unsigned long det;
1918         int fOffHook = 0;
1919
1920         switch (j->cardtype) {
1921         case QTI_PHONEJACK:
1922                 ixj_gpio_read(j);
1923                 fOffHook = j->gpio.bits.gpio3read ? 1 : 0;
1924                 break;
1925         case QTI_LINEJACK:
1926         case QTI_PHONEJACK_LITE:
1927         case QTI_PHONEJACK_PCI:
1928                 SLIC_GetState(j);
1929                 if(j->cardtype == QTI_LINEJACK && j->flags.pots_pstn == 1 && (j->readers || j->writers)) {
1930                         fOffHook = j->pld_slicr.bits.potspstn ? 1 : 0;
1931                         if(fOffHook != j->p_hook) {
1932                                 if(!j->checkwait) {
1933                                         j->checkwait = jiffies;
1934                                 } 
1935                                 if(time_before(jiffies, j->checkwait + 2)) {
1936                                         fOffHook ^= 1;
1937                                 } else {
1938                                         j->checkwait = 0;
1939                                 }
1940                                 j->p_hook = fOffHook;
1941                                 printk("IXJ : /dev/phone%d pots-pstn hookstate check %d at %ld\n", j->board, fOffHook, jiffies);
1942                         }
1943                 } else {
1944                         if (j->pld_slicr.bits.state == PLD_SLIC_STATE_ACTIVE ||
1945                             j->pld_slicr.bits.state == PLD_SLIC_STATE_STANDBY) {
1946                                 if (j->flags.ringing || j->flags.cringing) {
1947                                         if (!in_interrupt()) {
1948                                                 det = jiffies + (hertz / 50);
1949                                                 while (time_before(jiffies, det)) {
1950                                                         set_current_state(TASK_INTERRUPTIBLE);
1951                                                         schedule_timeout(1);
1952                                                 }
1953                                         }
1954                                         SLIC_GetState(j);
1955                                         if (j->pld_slicr.bits.state == PLD_SLIC_STATE_RINGING) {
1956                                                 ixj_ring_on(j);
1957                                         }
1958                                 }
1959                                 if (j->cardtype == QTI_PHONEJACK_PCI) {
1960                                         j->pld_scrr.byte = inb_p(j->XILINXbase);
1961                                         fOffHook = j->pld_scrr.pcib.det ? 1 : 0;
1962                                 } else
1963                                         fOffHook = j->pld_slicr.bits.det ? 1 : 0;
1964                         }
1965                 }
1966                 break;
1967         case QTI_PHONECARD:
1968                 fOffHook = ixj_pcmcia_cable_check(j);
1969                 break;
1970         }
1971         if (j->r_hook != fOffHook) {
1972                 j->r_hook = fOffHook;
1973                 if (j->port == PORT_SPEAKER || j->port == PORT_HANDSET) { // || (j->port == PORT_PSTN && j->flags.pots_pstn == 0)) {
1974                         j->ex.bits.hookstate = 1;
1975                         ixj_kill_fasync(j, SIG_HOOKSTATE, POLL_IN);
1976                 } else if (!fOffHook) {
1977                         j->flash_end = jiffies + ((60 * hertz) / 100);
1978                 }
1979         }
1980         if (fOffHook) {
1981                 if(time_before(jiffies, j->flash_end)) {
1982                         j->ex.bits.flash = 1;
1983                         j->flash_end = 0;
1984                         ixj_kill_fasync(j, SIG_FLASH, POLL_IN);
1985                 }
1986         } else {
1987                 if(time_before(jiffies, j->flash_end)) {
1988                         fOffHook = 1;
1989                 }
1990         }
1991
1992         if (j->port == PORT_PSTN && j->daa_mode == SOP_PU_CONVERSATION)
1993                 fOffHook |= 2;
1994
1995         if (j->port == PORT_SPEAKER) {
1996                 if(j->cardtype == QTI_PHONECARD) {
1997                         if(j->flags.pcmciascp && j->flags.pcmciasct) {
1998                                 fOffHook |= 2;
1999                         }
2000                 } else {
2001                         fOffHook |= 2;
2002                 }
2003         }
2004
2005         if (j->port == PORT_HANDSET)
2006                 fOffHook |= 2;
2007
2008         return fOffHook;
2009 }
2010
2011 static void ixj_ring_off(IXJ *j)
2012 {
2013         if (j->dsp.low == 0x20) /* Internet PhoneJACK */
2014          {
2015                 if (ixjdebug & 0x0004)
2016                         printk(KERN_INFO "IXJ Ring Off\n");
2017                 j->gpio.bytes.high = 0x0B;
2018                 j->gpio.bytes.low = 0x00;
2019                 j->gpio.bits.gpio1 = 0;
2020                 j->gpio.bits.gpio2 = 1;
2021                 j->gpio.bits.gpio5 = 0;
2022                 ixj_WriteDSPCommand(j->gpio.word, j);
2023         } else                  /* Internet LineJACK */
2024         {
2025                 if (ixjdebug & 0x0004)
2026                         printk(KERN_INFO "IXJ Ring Off\n");
2027
2028                 if(!j->flags.cidplay)
2029                         SLIC_SetState(PLD_SLIC_STATE_STANDBY, j);
2030
2031                 SLIC_GetState(j);
2032         }
2033 }
2034
2035 static void ixj_ring_start(IXJ *j)
2036 {
2037         j->flags.cringing = 1;
2038         if (ixjdebug & 0x0004)
2039                 printk(KERN_INFO "IXJ Cadence Ringing Start /dev/phone%d\n", j->board);
2040         if (ixj_hookstate(j) & 1) {
2041                 if (j->port == PORT_POTS)
2042                         ixj_ring_off(j);
2043                 j->flags.cringing = 0;
2044                 if (ixjdebug & 0x0004)
2045                         printk(KERN_INFO "IXJ Cadence Ringing Stopped /dev/phone%d off hook\n", j->board);
2046         } else if(j->cadence_f[5].enable && (!j->cadence_f[5].en_filter)) {
2047                 j->ring_cadence_jif = jiffies;
2048                 j->flags.cidsent = j->flags.cidring = 0;
2049                 j->cadence_f[5].state = 0;
2050                 if(j->cadence_f[5].on1)
2051                         ixj_ring_on(j);
2052         } else {
2053                 j->ring_cadence_jif = jiffies;
2054                 j->ring_cadence_t = 15;
2055                 if (j->ring_cadence & 1 << j->ring_cadence_t) {
2056                         ixj_ring_on(j);
2057                 } else {
2058                         ixj_ring_off(j);
2059                 }
2060                 j->flags.cidsent = j->flags.cidring = j->flags.firstring = 0;
2061         }
2062 }
2063
2064 static int ixj_ring(IXJ *j)
2065 {
2066         char cntr;
2067         unsigned long jif, det;
2068
2069         j->flags.ringing = 1;
2070         if (ixj_hookstate(j) & 1) {
2071                 ixj_ring_off(j);
2072                 j->flags.ringing = 0;
2073                 return 1;
2074         }
2075         det = 0;
2076         for (cntr = 0; cntr < j->maxrings; cntr++) {
2077                 jif = jiffies + (1 * hertz);
2078                 ixj_ring_on(j);
2079                 while (time_before(jiffies, jif)) {
2080                         if (ixj_hookstate(j) & 1) {
2081                                 ixj_ring_off(j);
2082                                 j->flags.ringing = 0;
2083                                 return 1;
2084                         }
2085                         set_current_state(TASK_INTERRUPTIBLE);
2086                         schedule_timeout(1);
2087                         if (signal_pending(current))
2088                                 break;
2089                 }
2090                 jif = jiffies + (3 * hertz);
2091                 ixj_ring_off(j);
2092                 while (time_before(jiffies, jif)) {
2093                         if (ixj_hookstate(j) & 1) {
2094                                 det = jiffies + (hertz / 100);
2095                                 while (time_before(jiffies, det)) {
2096                                         set_current_state(TASK_INTERRUPTIBLE);
2097                                         schedule_timeout(1);
2098                                         if (signal_pending(current))
2099                                                 break;
2100                                 }
2101                                 if (ixj_hookstate(j) & 1) {
2102                                         j->flags.ringing = 0;
2103                                         return 1;
2104                                 }
2105                         }
2106                         set_current_state(TASK_INTERRUPTIBLE);
2107                         schedule_timeout(1);
2108                         if (signal_pending(current))
2109                                 break;
2110                 }
2111         }
2112         ixj_ring_off(j);
2113         j->flags.ringing = 0;
2114         return 0;
2115 }
2116
2117 static int ixj_open(struct phone_device *p, struct file *file_p)
2118 {
2119         IXJ *j = get_ixj(p->board);
2120         file_p->private_data = j;
2121
2122         if (!j->DSPbase)
2123                 return -ENODEV;
2124
2125         if (file_p->f_mode & FMODE_READ) {
2126                 if(!j->readers) {
2127                         j->readers++;
2128                 } else {
2129                         return -EBUSY;
2130                 }
2131         }
2132
2133         if (file_p->f_mode & FMODE_WRITE) {
2134                 if(!j->writers) {
2135                         j->writers++;
2136                 } else {
2137                         if (file_p->f_mode & FMODE_READ){
2138                                 j->readers--;
2139                         }
2140                         return -EBUSY;
2141                 }
2142         }
2143
2144         if (j->cardtype == QTI_PHONECARD) {
2145                 j->pslic.bits.powerdown = 0;
2146                 j->psccr.bits.dev = 3;
2147                 j->psccr.bits.rw = 0;
2148                 outw_p(j->psccr.byte << 8 | j->pslic.byte, j->XILINXbase + 0x00);
2149                 ixj_PCcontrol_wait(j);
2150         }
2151
2152         j->flags.cidplay = 0;
2153         j->flags.cidcw_ack = 0;
2154
2155         if (ixjdebug & 0x0002)
2156                 printk(KERN_INFO "Opening board %d\n", p->board);
2157
2158         j->framesread = j->frameswritten = 0;
2159         return 0;
2160 }
2161
2162 static int ixj_release(struct inode *inode, struct file *file_p)
2163 {
2164         IXJ_TONE ti;
2165         int cnt;
2166         IXJ *j = file_p->private_data;
2167         int board = j->p.board;
2168
2169         /*
2170          *    Set up locks to ensure that only one process is talking to the DSP at a time.
2171          *    This is necessary to keep the DSP from locking up.
2172          */
2173         while(test_and_set_bit(board, (void *)&j->busyflags) != 0) {
2174                 set_current_state(TASK_INTERRUPTIBLE);
2175                 schedule_timeout(1);
2176         }
2177         if (ixjdebug & 0x0002)
2178                 printk(KERN_INFO "Closing board %d\n", NUM(inode));
2179
2180         if (j->cardtype == QTI_PHONECARD)
2181                 ixj_set_port(j, PORT_SPEAKER);
2182         else
2183                 ixj_set_port(j, PORT_POTS);
2184
2185         aec_stop(j);
2186         ixj_play_stop(j);
2187         ixj_record_stop(j);
2188         set_play_volume(j, 0x100);
2189         set_rec_volume(j, 0x100);
2190         ixj_ring_off(j);
2191
2192         /* Restore the tone table to default settings. */
2193         ti.tone_index = 10;
2194         ti.gain0 = 1;
2195         ti.freq0 = hz941;
2196         ti.gain1 = 0;
2197         ti.freq1 = hz1209;
2198         ixj_init_tone(j, &ti);
2199         ti.tone_index = 11;
2200         ti.gain0 = 1;
2201         ti.freq0 = hz941;
2202         ti.gain1 = 0;
2203         ti.freq1 = hz1336;
2204         ixj_init_tone(j, &ti);
2205         ti.tone_index = 12;
2206         ti.gain0 = 1;
2207         ti.freq0 = hz941;
2208         ti.gain1 = 0;
2209         ti.freq1 = hz1477;
2210         ixj_init_tone(j, &ti);
2211         ti.tone_index = 13;
2212         ti.gain0 = 1;
2213         ti.freq0 = hz800;
2214         ti.gain1 = 0;
2215         ti.freq1 = 0;
2216         ixj_init_tone(j, &ti);
2217         ti.tone_index = 14;
2218         ti.gain0 = 1;
2219         ti.freq0 = hz1000;
2220         ti.gain1 = 0;
2221         ti.freq1 = 0;
2222         ixj_init_tone(j, &ti);
2223         ti.tone_index = 15;
2224         ti.gain0 = 1;
2225         ti.freq0 = hz1250;
2226         ti.gain1 = 0;
2227         ti.freq1 = 0;
2228         ixj_init_tone(j, &ti);
2229         ti.tone_index = 16;
2230         ti.gain0 = 1;
2231         ti.freq0 = hz950;
2232         ti.gain1 = 0;
2233         ti.freq1 = 0;
2234         ixj_init_tone(j, &ti);
2235         ti.tone_index = 17;
2236         ti.gain0 = 1;
2237         ti.freq0 = hz1100;
2238         ti.gain1 = 0;
2239         ti.freq1 = 0;
2240         ixj_init_tone(j, &ti);
2241         ti.tone_index = 18;
2242         ti.gain0 = 1;
2243         ti.freq0 = hz1400;
2244         ti.gain1 = 0;
2245         ti.freq1 = 0;
2246         ixj_init_tone(j, &ti);
2247         ti.tone_index = 19;
2248         ti.gain0 = 1;
2249         ti.freq0 = hz1500;
2250         ti.gain1 = 0;
2251         ti.freq1 = 0;
2252         ixj_init_tone(j, &ti);
2253         ti.tone_index = 20;
2254         ti.gain0 = 1;
2255         ti.freq0 = hz1600;
2256         ti.gain1 = 0;
2257         ti.freq1 = 0;
2258         ixj_init_tone(j, &ti);
2259         ti.tone_index = 21;
2260         ti.gain0 = 1;
2261         ti.freq0 = hz1800;
2262         ti.gain1 = 0;
2263         ti.freq1 = 0;
2264         ixj_init_tone(j, &ti);
2265         ti.tone_index = 22;
2266         ti.gain0 = 1;
2267         ti.freq0 = hz2100;
2268         ti.gain1 = 0;
2269         ti.freq1 = 0;
2270         ixj_init_tone(j, &ti);
2271         ti.tone_index = 23;
2272         ti.gain0 = 1;
2273         ti.freq0 = hz1300;
2274         ti.gain1 = 0;
2275         ti.freq1 = 0;
2276         ixj_init_tone(j, &ti);
2277         ti.tone_index = 24;
2278         ti.gain0 = 1;
2279         ti.freq0 = hz2450;
2280         ti.gain1 = 0;
2281         ti.freq1 = 0;
2282         ixj_init_tone(j, &ti);
2283         ti.tone_index = 25;
2284         ti.gain0 = 1;
2285         ti.freq0 = hz350;
2286         ti.gain1 = 0;
2287         ti.freq1 = hz440;
2288         ixj_init_tone(j, &ti);
2289         ti.tone_index = 26;
2290         ti.gain0 = 1;
2291         ti.freq0 = hz440;
2292         ti.gain1 = 0;
2293         ti.freq1 = hz480;
2294         ixj_init_tone(j, &ti);
2295         ti.tone_index = 27;
2296         ti.gain0 = 1;
2297         ti.freq0 = hz480;
2298         ti.gain1 = 0;
2299         ti.freq1 = hz620;
2300         ixj_init_tone(j, &ti);
2301
2302         set_rec_depth(j, 2);    /* Set Record Channel Limit to 2 frames */
2303
2304         set_play_depth(j, 2);   /* Set Playback Channel Limit to 2 frames */
2305
2306         j->ex.bits.dtmf_ready = 0;
2307         j->dtmf_state = 0;
2308         j->dtmf_wp = j->dtmf_rp = 0;
2309         j->rec_mode = j->play_mode = -1;
2310         j->flags.ringing = 0;
2311         j->maxrings = MAXRINGS;
2312         j->ring_cadence = USA_RING_CADENCE;
2313         if(j->cadence_f[5].enable) {
2314                 j->cadence_f[5].enable = j->cadence_f[5].en_filter = j->cadence_f[5].state = 0;
2315         }
2316         j->drybuffer = 0;
2317         j->winktime = 320;
2318         j->flags.dtmf_oob = 0;
2319         for (cnt = 0; cnt < 4; cnt++)
2320                 j->cadence_f[cnt].enable = 0;
2321
2322         idle(j);
2323
2324         if(j->cardtype == QTI_PHONECARD) {
2325                 SLIC_SetState(PLD_SLIC_STATE_OC, j);
2326         }
2327
2328         if (file_p->f_mode & FMODE_READ)
2329                 j->readers--;
2330         if (file_p->f_mode & FMODE_WRITE)
2331                 j->writers--;
2332
2333         if (j->read_buffer && !j->readers) {
2334                 kfree(j->read_buffer);
2335                 j->read_buffer = NULL;
2336                 j->read_buffer_size = 0;
2337         }
2338         if (j->write_buffer && !j->writers) {
2339                 kfree(j->write_buffer);
2340                 j->write_buffer = NULL;
2341                 j->write_buffer_size = 0;
2342         }
2343         j->rec_codec = j->play_codec = 0;
2344         j->rec_frame_size = j->play_frame_size = 0;
2345         j->flags.cidsent = j->flags.cidring = 0;
2346         ixj_fasync(-1, file_p, 0);      /* remove from list of async notification */
2347
2348         if(j->cardtype == QTI_LINEJACK && !j->readers && !j->writers) {
2349                 ixj_set_port(j, PORT_PSTN);
2350                 daa_set_mode(j, SOP_PU_SLEEP);
2351                 ixj_set_pots(j, 1);
2352         }
2353         ixj_WriteDSPCommand(0x0FE3, j); /* Put the DSP in 1/5 power mode. */
2354
2355         /* Set up the default signals for events */
2356         for (cnt = 0; cnt < 35; cnt++)
2357                 j->ixj_signals[cnt] = SIGIO;
2358
2359         /* Set the excetion signal enable flags */
2360         j->ex_sig.bits.dtmf_ready = j->ex_sig.bits.hookstate = j->ex_sig.bits.flash = j->ex_sig.bits.pstn_ring = 
2361         j->ex_sig.bits.caller_id = j->ex_sig.bits.pstn_wink = j->ex_sig.bits.f0 = j->ex_sig.bits.f1 = j->ex_sig.bits.f2 = 
2362         j->ex_sig.bits.f3 = j->ex_sig.bits.fc0 = j->ex_sig.bits.fc1 = j->ex_sig.bits.fc2 = j->ex_sig.bits.fc3 = 1;
2363
2364         file_p->private_data = NULL;
2365         clear_bit(board, &j->busyflags);
2366         return 0;
2367 }
2368
2369 static int read_filters(IXJ *j)
2370 {
2371         unsigned short fc, cnt, trg;
2372         int var;
2373
2374         trg = 0;
2375         if (ixj_WriteDSPCommand(0x5144, j)) {
2376                 if(ixjdebug & 0x0001) {
2377                         printk(KERN_INFO "Read Frame Counter failed!\n");
2378                 }
2379                 return -1;
2380         }
2381         fc = j->ssr.high << 8 | j->ssr.low;
2382         if (fc == j->frame_count)
2383                 return 1;
2384
2385         j->frame_count = fc;
2386
2387         if (j->dtmf_proc)
2388                 return 1;
2389
2390         var = 10;
2391
2392         for (cnt = 0; cnt < 4; cnt++) {
2393                 if (ixj_WriteDSPCommand(0x5154 + cnt, j)) {
2394                         if(ixjdebug & 0x0001) {
2395                                 printk(KERN_INFO "Select Filter %d failed!\n", cnt);
2396                         }
2397                         return -1;
2398                 }
2399                 if (ixj_WriteDSPCommand(0x515C, j)) {
2400                         if(ixjdebug & 0x0001) {
2401                                 printk(KERN_INFO "Read Filter History %d failed!\n", cnt);
2402                         }
2403                         return -1;
2404                 }
2405                 j->filter_hist[cnt] = j->ssr.high << 8 | j->ssr.low;
2406
2407                 if (j->cadence_f[cnt].enable) {
2408                         if (j->filter_hist[cnt] & 3 && !(j->filter_hist[cnt] & 12)) {
2409                                 if (j->cadence_f[cnt].state == 0) {
2410                                         j->cadence_f[cnt].state = 1;
2411                                         j->cadence_f[cnt].on1min = jiffies + (long)((j->cadence_f[cnt].on1 * (hertz * (100 - var)) / 10000));
2412                                         j->cadence_f[cnt].on1dot = jiffies + (long)((j->cadence_f[cnt].on1 * (hertz * (100)) / 10000));
2413                                         j->cadence_f[cnt].on1max = jiffies + (long)((j->cadence_f[cnt].on1 * (hertz * (100 + var)) / 10000));
2414                                 } else if (j->cadence_f[cnt].state == 2 &&
2415                                            (time_after(jiffies, j->cadence_f[cnt].off1min) &&
2416                                             time_before(jiffies, j->cadence_f[cnt].off1max))) {
2417                                         if (j->cadence_f[cnt].on2) {
2418                                                 j->cadence_f[cnt].state = 3;
2419                                                 j->cadence_f[cnt].on2min = jiffies + (long)((j->cadence_f[cnt].on2 * (hertz * (100 - var)) / 10000));
2420                                                 j->cadence_f[cnt].on2dot = jiffies + (long)((j->cadence_f[cnt].on2 * (hertz * (100)) / 10000));
2421                                                 j->cadence_f[cnt].on2max = jiffies + (long)((j->cadence_f[cnt].on2 * (hertz * (100 + var)) / 10000));
2422                                         } else {
2423                                                 j->cadence_f[cnt].state = 7;
2424                                         }
2425                                 } else if (j->cadence_f[cnt].state == 4 &&
2426                                            (time_after(jiffies, j->cadence_f[cnt].off2min) &&
2427                                             time_before(jiffies, j->cadence_f[cnt].off2max))) {
2428                                         if (j->cadence_f[cnt].on3) {
2429                                                 j->cadence_f[cnt].state = 5;
2430                                                 j->cadence_f[cnt].on3min = jiffies + (long)((j->cadence_f[cnt].on3 * (hertz * (100 - var)) / 10000));
2431                                                 j->cadence_f[cnt].on3dot = jiffies + (long)((j->cadence_f[cnt].on3 * (hertz * (100)) / 10000));
2432                                                 j->cadence_f[cnt].on3max = jiffies + (long)((j->cadence_f[cnt].on3 * (hertz * (100 + var)) / 10000));
2433                                         } else {
2434                                                 j->cadence_f[cnt].state = 7;
2435                                         }
2436                                 } else {
2437                                         j->cadence_f[cnt].state = 0;
2438                                 }
2439                         } else if (j->filter_hist[cnt] & 12 && !(j->filter_hist[cnt] & 3)) {
2440                                 if (j->cadence_f[cnt].state == 1) {
2441                                         if(!j->cadence_f[cnt].on1) {
2442                                                 j->cadence_f[cnt].state = 7;
2443                                         } else if((time_after(jiffies, j->cadence_f[cnt].on1min) &&
2444                                           time_before(jiffies, j->cadence_f[cnt].on1max))) {
2445                                                 if(j->cadence_f[cnt].off1) {
2446                                                         j->cadence_f[cnt].state = 2;
2447                                                         j->cadence_f[cnt].off1min = jiffies + (long)((j->cadence_f[cnt].off1 * (hertz * (100 - var)) / 10000));
2448                                                         j->cadence_f[cnt].off1dot = jiffies + (long)((j->cadence_f[cnt].off1 * (hertz * (100)) / 10000));
2449                                                         j->cadence_f[cnt].off1max = jiffies + (long)((j->cadence_f[cnt].off1 * (hertz * (100 + var)) / 10000));
2450                                                 } else {
2451                                                         j->cadence_f[cnt].state = 7;
2452                                                 }
2453                                         } else {
2454                                                 j->cadence_f[cnt].state = 0;
2455                                         }
2456                                 } else if (j->cadence_f[cnt].state == 3) {
2457                                         if((time_after(jiffies, j->cadence_f[cnt].on2min) &&
2458                                             time_before(jiffies, j->cadence_f[cnt].on2max))) {
2459                                                 if(j->cadence_f[cnt].off2) {
2460                                                         j->cadence_f[cnt].state = 4;
2461                                                         j->cadence_f[cnt].off2min = jiffies + (long)((j->cadence_f[cnt].off2 * (hertz * (100 - var)) / 10000));
2462                                                         j->cadence_f[cnt].off2dot = jiffies + (long)((j->cadence_f[cnt].off2 * (hertz * (100)) / 10000));
2463                                                         j->cadence_f[cnt].off2max = jiffies + (long)((j->cadence_f[cnt].off2 * (hertz * (100 + var)) / 10000));
2464                                                 } else {
2465                                                         j->cadence_f[cnt].state = 7;
2466                                                 }
2467                                         } else {
2468                                                 j->cadence_f[cnt].state = 0;
2469                                         }
2470                                 } else if (j->cadence_f[cnt].state == 5) {
2471                                         if ((time_after(jiffies, j->cadence_f[cnt].on3min) &&
2472                                             time_before(jiffies, j->cadence_f[cnt].on3max))) {
2473                                                 if(j->cadence_f[cnt].off3) {
2474                                                         j->cadence_f[cnt].state = 6;
2475                                                         j->cadence_f[cnt].off3min = jiffies + (long)((j->cadence_f[cnt].off3 * (hertz * (100 - var)) / 10000));
2476                                                         j->cadence_f[cnt].off3dot = jiffies + (long)((j->cadence_f[cnt].off3 * (hertz * (100)) / 10000));
2477                                                         j->cadence_f[cnt].off3max = jiffies + (long)((j->cadence_f[cnt].off3 * (hertz * (100 + var)) / 10000));
2478                                                 } else {
2479                                                         j->cadence_f[cnt].state = 7;
2480                                                 }
2481                                         } else {
2482                                                 j->cadence_f[cnt].state = 0;
2483                                         }
2484                                 } else {
2485                                         j->cadence_f[cnt].state = 0;
2486                                 }
2487                         } else {
2488                                 switch(j->cadence_f[cnt].state) {
2489                                         case 1:
2490                                                 if(time_after(jiffies, j->cadence_f[cnt].on1dot) &&
2491                                                    !j->cadence_f[cnt].off1 &&
2492                                                    !j->cadence_f[cnt].on2 && !j->cadence_f[cnt].off2 &&
2493                                                    !j->cadence_f[cnt].on3 && !j->cadence_f[cnt].off3) {
2494                                                         j->cadence_f[cnt].state = 7;
2495                                                 }
2496                                                 break;
2497                                         case 3:
2498                                                 if(time_after(jiffies, j->cadence_f[cnt].on2dot) &&
2499                                                    !j->cadence_f[cnt].off2 &&
2500                                                    !j->cadence_f[cnt].on3 && !j->cadence_f[cnt].off3) {
2501                                                         j->cadence_f[cnt].state = 7;
2502                                                 }
2503                                                 break;
2504                                         case 5:
2505                                                 if(time_after(jiffies, j->cadence_f[cnt].on3dot) &&
2506                                                    !j->cadence_f[cnt].off3) {
2507                                                         j->cadence_f[cnt].state = 7;
2508                                                 }
2509                                                 break;
2510                                 }
2511                         }
2512
2513                         if (ixjdebug & 0x0040) {
2514                                 printk(KERN_INFO "IXJ Tone Cadence state = %d /dev/phone%d at %ld\n", j->cadence_f[cnt].state, j->board, jiffies);
2515                                 switch(j->cadence_f[cnt].state) {
2516                                         case 0:
2517                                                 printk(KERN_INFO "IXJ /dev/phone%d No Tone detected\n", j->board);
2518                                                 break;
2519                                         case 1:
2520                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %u %ld - %ld - %ld\n", j->board,
2521                                         j->cadence_f[cnt].on1, j->cadence_f[cnt].on1min, j->cadence_f[cnt].on1dot, j->cadence_f[cnt].on1max);
2522                                                 break;
2523                                         case 2:
2524                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %ld - %ld\n", j->board, j->cadence_f[cnt].off1min, 
2525                                                                                                                         j->cadence_f[cnt].off1max);
2526                                                 break;
2527                                         case 3:
2528                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %ld - %ld\n", j->board, j->cadence_f[cnt].on2min,
2529                                                                                                                         j->cadence_f[cnt].on2max);
2530                                                 break;
2531                                         case 4:
2532                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %ld - %ld\n", j->board, j->cadence_f[cnt].off2min,
2533                                                                                                                         j->cadence_f[cnt].off2max);
2534                                                 break;
2535                                         case 5:
2536                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %ld - %ld\n", j->board, j->cadence_f[cnt].on3min,
2537                                                                                                                         j->cadence_f[cnt].on3max);
2538                                                 break;
2539                                         case 6: 
2540                                                 printk(KERN_INFO "IXJ /dev/phone%d Next Tone Cadence state at %ld - %ld\n", j->board, j->cadence_f[cnt].off3min,
2541                                                                                                                         j->cadence_f[cnt].off3max);
2542                                                 break;
2543                                 }
2544                         } 
2545                 }
2546                 if (j->cadence_f[cnt].state == 7) {
2547                         j->cadence_f[cnt].state = 0;
2548                         if (j->cadence_f[cnt].enable == 1)
2549                                 j->cadence_f[cnt].enable = 0;
2550                         switch (cnt) {
2551                         case 0:
2552                                 if(ixjdebug & 0x0020) {
2553                                         printk(KERN_INFO "Filter Cadence 0 triggered %ld\n", jiffies);
2554                                 }
2555                                 j->ex.bits.fc0 = 1;
2556                                 ixj_kill_fasync(j, SIG_FC0, POLL_IN);
2557                                 break;
2558                         case 1:
2559                                 if(ixjdebug & 0x0020) {
2560                                         printk(KERN_INFO "Filter Cadence 1 triggered %ld\n", jiffies);
2561                                 }
2562                                 j->ex.bits.fc1 = 1;
2563                                 ixj_kill_fasync(j, SIG_FC1, POLL_IN);
2564                                 break;
2565                         case 2:
2566                                 if(ixjdebug & 0x0020) {
2567                                         printk(KERN_INFO "Filter Cadence 2 triggered %ld\n", jiffies);
2568                                 }
2569                                 j->ex.bits.fc2 = 1;
2570                                 ixj_kill_fasync(j, SIG_FC2, POLL_IN);
2571                                 break;
2572                         case 3:
2573                                 if(ixjdebug & 0x0020) {
2574                                         printk(KERN_INFO "Filter Cadence 3 triggered %ld\n", jiffies);
2575                                 }
2576                                 j->ex.bits.fc3 = 1;
2577                                 ixj_kill_fasync(j, SIG_FC3, POLL_IN);
2578                                 break;
2579                         }
2580                 }
2581                 if (j->filter_en[cnt] && ((j->filter_hist[cnt] & 3 && !(j->filter_hist[cnt] & 12)) ||
2582                                           (j->filter_hist[cnt] & 12 && !(j->filter_hist[cnt] & 3)))) {
2583                         if((j->filter_hist[cnt] & 3 && !(j->filter_hist[cnt] & 12))) {
2584                                 trg = 1;
2585                         } else if((j->filter_hist[cnt] & 12 && !(j->filter_hist[cnt] & 3))) {
2586                                 trg = 0;
2587                         }
2588                         switch (cnt) {
2589                         case 0:
2590                                 if(ixjdebug & 0x0020) {
2591                                         printk(KERN_INFO "Filter 0 triggered %d at %ld\n", trg, jiffies);
2592                                 }
2593                                 j->ex.bits.f0 = 1;
2594                                 ixj_kill_fasync(j, SIG_F0, POLL_IN);
2595                                 break;
2596                         case 1:
2597                                 if(ixjdebug & 0x0020) {
2598                                         printk(KERN_INFO "Filter 1 triggered %d at %ld\n", trg, jiffies);
2599                                 }
2600                                 j->ex.bits.f1 = 1;
2601                                 ixj_kill_fasync(j, SIG_F1, POLL_IN);
2602                                 break;
2603                         case 2:
2604                                 if(ixjdebug & 0x0020) {
2605                                         printk(KERN_INFO "Filter 2 triggered %d at %ld\n", trg, jiffies);
2606                                 }
2607                                 j->ex.bits.f2 = 1;
2608                                 ixj_kill_fasync(j, SIG_F2, POLL_IN);
2609                                 break;
2610                         case 3:
2611                                 if(ixjdebug & 0x0020) {
2612                                         printk(KERN_INFO "Filter 3 triggered %d at %ld\n", trg, jiffies);
2613                                 }
2614                                 j->ex.bits.f3 = 1;
2615                                 ixj_kill_fasync(j, SIG_F3, POLL_IN);
2616                                 break;
2617                         }
2618                 }
2619         }
2620         return 0;
2621 }
2622
2623 static int LineMonitor(IXJ *j)
2624 {
2625         if (j->dtmf_proc) {
2626                 return -1;
2627         }
2628         j->dtmf_proc = 1;
2629
2630         if (ixj_WriteDSPCommand(0x7000, j))             /* Line Monitor */
2631                 return -1;
2632
2633         j->dtmf.bytes.high = j->ssr.high;
2634         j->dtmf.bytes.low = j->ssr.low;
2635         if (!j->dtmf_state && j->dtmf.bits.dtmf_valid) {
2636                 j->dtmf_state = 1;
2637                 j->dtmf_current = j->dtmf.bits.digit;
2638         }
2639         if (j->dtmf_state && !j->dtmf.bits.dtmf_valid)  /* && j->dtmf_wp != j->dtmf_rp) */
2640          {
2641                 if(!j->cidcw_wait) {
2642                         j->dtmfbuffer[j->dtmf_wp] = j->dtmf_current;
2643                         j->dtmf_wp++;
2644                         if (j->dtmf_wp == 79)
2645                                 j->dtmf_wp = 0;
2646                         j->ex.bits.dtmf_ready = 1;
2647                         if(j->ex_sig.bits.dtmf_ready) {
2648                                 ixj_kill_fasync(j, SIG_DTMF_READY, POLL_IN);
2649                         }
2650                 }
2651                 else if(j->dtmf_current == 0x00 || j->dtmf_current == 0x0D) {
2652                         if(ixjdebug & 0x0020) {
2653                                 printk("IXJ phone%d saw CIDCW Ack DTMF %d from display at %ld\n", j->board, j->dtmf_current, jiffies);
2654                         }
2655                         j->flags.cidcw_ack = 1;
2656                 }
2657                 j->dtmf_state = 0;
2658         }
2659         j->dtmf_proc = 0;
2660
2661         return 0;
2662 }
2663
2664 /************************************************************************
2665 *
2666 * Functions to allow alaw <-> ulaw conversions.
2667 *
2668 ************************************************************************/
2669
2670 static void ulaw2alaw(unsigned char *buff, unsigned long len)
2671 {
2672         static unsigned char table_ulaw2alaw[] =
2673         {
2674                 0x2A, 0x2B, 0x28, 0x29, 0x2E, 0x2F, 0x2C, 0x2D, 
2675                 0x22, 0x23, 0x20, 0x21, 0x26, 0x27, 0x24, 0x25, 
2676                 0x3A, 0x3B, 0x38, 0x39, 0x3E, 0x3F, 0x3C, 0x3D, 
2677                 0x32, 0x33, 0x30, 0x31, 0x36, 0x37, 0x34, 0x35, 
2678                 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D, 0x02, 
2679                 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05, 0x1A, 
2680                 0x1B, 0x18, 0x19, 0x1E, 0x1F, 0x1C, 0x1D, 0x12, 
2681                 0x13, 0x10, 0x11, 0x16, 0x17, 0x14, 0x15, 0x6B, 
2682                 0x68, 0x69, 0x6E, 0x6F, 0x6C, 0x6D, 0x62, 0x63, 
2683                 0x60, 0x61, 0x66, 0x67, 0x64, 0x65, 0x7B, 0x79, 
2684                 0x7E, 0x7F, 0x7C, 0x7D, 0x72, 0x73, 0x70, 0x71, 
2685                 0x76, 0x77, 0x74, 0x75, 0x4B, 0x49, 0x4F, 0x4D, 
2686                 0x42, 0x43, 0x40, 0x41, 0x46, 0x47, 0x44, 0x45, 
2687                 0x5A, 0x5B, 0x58, 0x59, 0x5E, 0x5F, 0x5C, 0x5D, 
2688                 0x52, 0x52, 0x53, 0x53, 0x50, 0x50, 0x51, 0x51, 
2689                 0x56, 0x56, 0x57, 0x57, 0x54, 0x54, 0x55, 0xD5, 
2690                 0xAA, 0xAB, 0xA8, 0xA9, 0xAE, 0xAF, 0xAC, 0xAD, 
2691                 0xA2, 0xA3, 0xA0, 0xA1, 0xA6, 0xA7, 0xA4, 0xA5, 
2692                 0xBA, 0xBB, 0xB8, 0xB9, 0xBE, 0xBF, 0xBC, 0xBD, 
2693                 0xB2, 0xB3, 0xB0, 0xB1, 0xB6, 0xB7, 0xB4, 0xB5, 
2694                 0x8B, 0x88, 0x89, 0x8E, 0x8F, 0x8C, 0x8D, 0x82, 
2695                 0x83, 0x80, 0x81, 0x86, 0x87, 0x84, 0x85, 0x9A, 
2696                 0x9B, 0x98, 0x99, 0x9E, 0x9F, 0x9C, 0x9D, 0x92, 
2697                 0x93, 0x90, 0x91, 0x96, 0x97, 0x94, 0x95, 0xEB, 
2698                 0xE8, 0xE9, 0xEE, 0xEF, 0xEC, 0xED, 0xE2, 0xE3, 
2699                 0xE0, 0xE1, 0xE6, 0xE7, 0xE4, 0xE5, 0xFB, 0xF9, 
2700                 0xFE, 0xFF, 0xFC, 0xFD, 0xF2, 0xF3, 0xF0, 0xF1, 
2701                 0xF6, 0xF7, 0xF4, 0xF5, 0xCB, 0xC9, 0xCF, 0xCD, 
2702                 0xC2, 0xC3, 0xC0, 0xC1, 0xC6, 0xC7, 0xC4, 0xC5, 
2703                 0xDA, 0xDB, 0xD8, 0xD9, 0xDE, 0xDF, 0xDC, 0xDD, 
2704                 0xD2, 0xD2, 0xD3, 0xD3, 0xD0, 0xD0, 0xD1, 0xD1, 
2705                 0xD6, 0xD6, 0xD7, 0xD7, 0xD4, 0xD4, 0xD5, 0xD5