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