tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / staging / comedi / drivers / addi-data / APCI1710_Tor.c
1 /**
2 @verbatim
3
4 Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
5
6         ADDI-DATA GmbH
7         Dieselstrasse 3
8         D-77833 Ottersweier
9         Tel: +19(0)7223/9493-0
10         Fax: +49(0)7223/9493-92
11         http://www.addi-data.com
12         info@addi-data.com
13
14 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 You should also find the complete GPL in the COPYING file accompanying this source code.
21
22 @endverbatim
23 */
24 /*
25
26   +-----------------------------------------------------------------------+
27   | (C) ADDI-DATA GmbH          Dieselstraße 3       D-77833 Ottersweier  |
28   +-----------------------------------------------------------------------+
29   | Tel : +49 (0) 7223/9493-0     | email    : info@addi-data.com         |
30   | Fax : +49 (0) 7223/9493-92    | Internet : http://www.addi-data.com   |
31   +-----------------------------------------------------------------------+
32   | Project     : API APCI1710    | Compiler : gcc                        |
33   | Module name : TOR.C           | Version  : 2.96                       |
34   +-------------------------------+---------------------------------------+
35   | Project manager: Eric Stolz   | Date     :  02/12/2002                |
36   +-----------------------------------------------------------------------+
37   | Description :   APCI-1710 tor counter module                          |
38   |                                                                       |
39   |                                                                       |
40   +-----------------------------------------------------------------------+
41   |                             UPDATES                                   |
42   +-----------------------------------------------------------------------+
43   |   Date   |   Author  |          Description of updates                |
44   +----------+-----------+------------------------------------------------+
45   | 27/01/99 | S. Weber  | 40 MHz implementation                          |
46   +-----------------------------------------------------------------------+
47   | 28/04/00 | S. Weber  | Simple,double and quadruple mode implementation|
48   |          |           | Extern clock implementation                    |
49   +-----------------------------------------------------------------------+
50   | 08/05/00 | Guinot C  | - 0400/0228 All Function in RING 0             |
51   |          |           |   available                                    |
52   +-----------------------------------------------------------------------+
53 */
54
55 #define APCI1710_30MHZ                  30
56 #define APCI1710_33MHZ                  33
57 #define APCI1710_40MHZ                  40
58
59 #define APCI1710_GATE_INPUT             10
60
61 #define APCI1710_TOR_SIMPLE_MODE        2
62 #define APCI1710_TOR_DOUBLE_MODE        3
63 #define APCI1710_TOR_QUADRUPLE_MODE     4
64
65 #define APCI1710_SINGLE                 0
66 #define APCI1710_CONTINUOUS             1
67
68 #define APCI1710_TOR_GETPROGRESSSTATUS  0
69 #define APCI1710_TOR_GETCOUNTERVALUE    1
70 #define APCI1710_TOR_READINTERRUPT      2
71
72 /*
73 +----------------------------------------------------------------------------+
74 | Function Name     : _INT_     i_APCI1710_InitTorCounter                    |
75 |                                       (unsigned char_     b_BoardHandle,            |
76 |                                        unsigned char_     b_ModulNbr,               |
77 |                                        unsigned char_     b_TorCounter,             |
78 |                                        unsigned char_     b_PCIInputClock,          |
79 |                                        unsigned char_     b_TimingUnit,             |
80 |                                        ULONG_   ul_TimingInterval,         |
81 |                                        PULONG_ pul_RealTimingInterval)     |
82 +----------------------------------------------------------------------------+
83 | Task              : Configure the selected tor counter (b_TorCounter)      |
84 |                     from selected module (b_ModulNbr).                     |
85 |                     The ul_TimingInterval and ul_TimingUnit determine the  |
86 |                     timing base for the measurement.                       |
87 |                     The pul_RealTimingInterval return the real timing      |
88 |                     value. You must calling this function be for you call  |
89 |                     any other function witch access of the tor counter.    |
90 |                                                                            |
91 +----------------------------------------------------------------------------+
92 | Input Parameters  :    |
93 |
94                 CR_AREF unsigned char_   b_ModulNbr       : Module number to configure  |
95 |                                                (0 to 3)                    |
96 |           data[0] unsigned char_   b_TorCounter     : Tor counter selection       |
97 |                                                (0 or 1).                   |
98 |           data[1] unsigned char_   b_PCIInputClock  : Selection from PCI bus clock|
99 |                                                - APCI1710_30MHZ :          |
100 |                                                  The PC have a PCI bus     |
101 |                                                  clock from 30 MHz         |
102 |                                                - APCI1710_33MHZ :          |
103 |                                                  The PC have a PCI bus     |
104 |                                                  clock from 33 MHz         |
105 |                                                - APCI1710_40MHZ            |
106 |                                                  The APCI-1710 have a      |
107 |                                                  integrated 40Mhz          |
108 |                                                  quartz.                   |
109 |                                                - APCI1710_GATE_INPUT       |
110 |                                                  Used the gate input for   |
111 |                                                  the base clock. If you    |
112 |                                                  have selected this option,|
113 |                                                  than it is not possibl to |
114 |                                                  used the gate input for   |
115 |                                                  enabled the acquisition   |
116 |           data[2] unsigned char_   b_TimingUnit    : Base timing unit (0 to 4)    |
117 |                                                 0 : ns                     |
118 |                                                 1 : µs                     |
119 |                                                 2 : ms                     |
120 |                                                 3 : s                      |
121 |                                                 4 : mn                     |
122 |           data[3]          ULONG_ ul_TimingInterval : Base timing value.          |
123 +----------------------------------------------------------------------------+
124 | Output Parameters : PULONG_  pul_RealTimingInterval : Real  base timing    |
125 |                     data[0]                                  value.               |
126 +----------------------------------------------------------------------------+
127 | Return Value      :  0: No error                                           |
128 |                     -1: The handle parameter of the board is wrong         |
129 |                     -2: Module selection wrong                             |
130 |                     -3: The module is not a tor counter module             |
131 |                     -4: Tor counter selection is wrong                     |
132 |                     -5: The selected PCI input clock is wrong              |
133 |                     -6: Timing unit selection is wrong                     |
134 |                     -7: Base timing selection is wrong                     |
135 |                     -8: You can not used the 40MHz clock selection wich    |
136 |                         this board                                         |
137 |                     -9: You can not used the 40MHz clock selection wich    |
138 |                         this TOR version                                   |
139 +----------------------------------------------------------------------------+
140 */
141
142 static int i_APCI1710_InsnConfigInitTorCounter(struct comedi_device *dev,
143                                                struct comedi_subdevice *s,
144                                                struct comedi_insn *insn,
145                                                unsigned int *data)
146 {
147         struct addi_private *devpriv = dev->private;
148         int i_ReturnValue = 0;
149         unsigned int ul_TimerValue = 0;
150         unsigned int dw_Command;
151         double d_RealTimingInterval = 0;
152         unsigned char b_ModulNbr;
153         unsigned char b_TorCounter;
154         unsigned char b_PCIInputClock;
155         unsigned char b_TimingUnit;
156         unsigned int ul_TimingInterval;
157         unsigned int ul_RealTimingInterval = 0;
158
159         i_ReturnValue = insn->n;
160         b_ModulNbr = (unsigned char) CR_AREF(insn->chanspec);
161
162         b_TorCounter = (unsigned char) data[0];
163         b_PCIInputClock = (unsigned char) data[1];
164         b_TimingUnit = (unsigned char) data[2];
165         ul_TimingInterval = (unsigned int) data[3];
166         printk("INPUT clock %d\n", b_PCIInputClock);
167
168                 /**************************/
169         /* Test the module number */
170                 /**************************/
171
172         if (b_ModulNbr < 4) {
173                 /***********************/
174                 /* Test if tor counter */
175                 /***********************/
176
177                 if ((devpriv->s_BoardInfos.
178                                 dw_MolduleConfiguration[b_ModulNbr] &
179                                 0xFFFF0000UL) == APCI1710_TOR_COUNTER) {
180               /**********************************/
181                         /* Test the tor counter selection */
182               /**********************************/
183
184                         if (b_TorCounter <= 1) {
185                  /**************************/
186                                 /* Test the PCI bus clock */
187                  /**************************/
188
189                                 if ((b_PCIInputClock == APCI1710_30MHZ) ||
190                                         (b_PCIInputClock == APCI1710_33MHZ) ||
191                                         (b_PCIInputClock == APCI1710_40MHZ) ||
192                                         (b_PCIInputClock ==
193                                                 APCI1710_GATE_INPUT)) {
194                     /************************/
195                                         /* Test the timing unit */
196                     /************************/
197
198                                         if ((b_TimingUnit <= 4)
199                                                 || (b_PCIInputClock ==
200                                                         APCI1710_GATE_INPUT)) {
201                        /**********************************/
202                                                 /* Test the base timing selection */
203                        /**********************************/
204
205                                                 if (((b_PCIInputClock == APCI1710_30MHZ) && (b_TimingUnit == 0) && (ul_TimingInterval >= 133) && (ul_TimingInterval <= 0xFFFFFFFFUL)) || ((b_PCIInputClock == APCI1710_30MHZ) && (b_TimingUnit == 1) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 571230650UL)) || ((b_PCIInputClock == APCI1710_30MHZ) && (b_TimingUnit == 2) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 571230UL)) || ((b_PCIInputClock == APCI1710_30MHZ) && (b_TimingUnit == 3) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 571UL)) || ((b_PCIInputClock == APCI1710_30MHZ) && (b_TimingUnit == 4) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 9UL)) || ((b_PCIInputClock == APCI1710_33MHZ) && (b_TimingUnit == 0) && (ul_TimingInterval >= 121) && (ul_TimingInterval <= 0xFFFFFFFFUL)) || ((b_PCIInputClock == APCI1710_33MHZ) && (b_TimingUnit == 1) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 519691043UL)) || ((b_PCIInputClock == APCI1710_33MHZ) && (b_TimingUnit == 2) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 519691UL)) || ((b_PCIInputClock == APCI1710_33MHZ) && (b_TimingUnit == 3) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 520UL)) || ((b_PCIInputClock == APCI1710_33MHZ) && (b_TimingUnit == 4) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 8UL)) || ((b_PCIInputClock == APCI1710_40MHZ) && (b_TimingUnit == 0) && (ul_TimingInterval >= 100) && (ul_TimingInterval <= 0xFFFFFFFFUL)) || ((b_PCIInputClock == APCI1710_40MHZ) && (b_TimingUnit == 1) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 429496729UL)) || ((b_PCIInputClock == APCI1710_40MHZ) && (b_TimingUnit == 2) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 429496UL)) || ((b_PCIInputClock == APCI1710_40MHZ) && (b_TimingUnit == 3) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 429UL)) || ((b_PCIInputClock == APCI1710_40MHZ) && (b_TimingUnit == 4) && (ul_TimingInterval >= 1) && (ul_TimingInterval <= 7UL)) || ((b_PCIInputClock == APCI1710_GATE_INPUT) && (ul_TimingInterval >= 2))) {
206                                 /**************************/
207                                                         /* Test the board version */
208                                 /**************************/
209
210                                                         if (((b_PCIInputClock == APCI1710_40MHZ) && (devpriv->s_BoardInfos.b_BoardVersion > 0)) || (b_PCIInputClock != APCI1710_40MHZ)) {
211                              /************************/
212                                                                 /* Test the TOR version */
213                              /************************/
214
215                                                                 if (((b_PCIInputClock == APCI1710_40MHZ) && ((devpriv->s_BoardInfos.dw_MolduleConfiguration[b_ModulNbr] & 0xFFFF) >= 0x3131)) || ((b_PCIInputClock == APCI1710_GATE_INPUT) && ((devpriv->s_BoardInfos.dw_MolduleConfiguration[b_ModulNbr] & 0xFFFF) >= 0x3132)) || (b_PCIInputClock == APCI1710_30MHZ) || (b_PCIInputClock == APCI1710_33MHZ)) {
216                                 /*********************************/
217                                                                         /* Test if not extern clock used */
218                                 /*********************************/
219
220                                                                         if (b_PCIInputClock != APCI1710_GATE_INPUT) {
221                                                                                 fpu_begin
222                                                                                         ();
223                                    /****************************************/
224                                                                                 /* Calculate the timer 0 division fator */
225                                    /****************************************/
226
227                                                                                 switch (b_TimingUnit) {
228                                       /******/
229                                                                                         /* ns */
230                                       /******/
231
232                                                                                 case 0:
233
234                                               /******************/
235                                                                                         /* Timer 0 factor */
236                                               /******************/
237
238                                                                                         ul_TimerValue
239                                                                                                 =
240                                                                                                 (unsigned int)
241                                                                                                 (ul_TimingInterval
242                                                                                                 *
243                                                                                                 (0.00025 * b_PCIInputClock));
244
245                                               /*******************/
246                                                                                         /* Round the value */
247                                               /*******************/
248
249                                                                                         if ((double)((double)ul_TimingInterval * (0.00025 * (double)b_PCIInputClock)) >= ((double)((double)ul_TimerValue + 0.5))) {
250                                                                                                 ul_TimerValue
251                                                                                                         =
252                                                                                                         ul_TimerValue
253                                                                                                         +
254                                                                                                         1;
255                                                                                         }
256
257                                               /*****************************/
258                                                                                         /* Calculate the real timing */
259                                               /*****************************/
260
261                                                                                         ul_RealTimingInterval
262                                                                                                 =
263                                                                                                 (unsigned int)
264                                                                                                 (ul_TimerValue
265                                                                                                 /
266                                                                                                 (0.00025 * (double)b_PCIInputClock));
267                                                                                         d_RealTimingInterval
268                                                                                                 =
269                                                                                                 (double)
270                                                                                                 ul_TimerValue
271                                                                                                 /
272                                                                                                 (0.00025
273                                                                                                 *
274                                                                                                 (double)
275                                                                                                 b_PCIInputClock);
276
277                                                                                         if ((double)((double)ul_TimerValue / (0.00025 * (double)b_PCIInputClock)) >= (double)((double)ul_RealTimingInterval + 0.5)) {
278                                                                                                 ul_RealTimingInterval
279                                                                                                         =
280                                                                                                         ul_RealTimingInterval
281                                                                                                         +
282                                                                                                         1;
283                                                                                         }
284
285                                                                                         ul_TimingInterval
286                                                                                                 =
287                                                                                                 ul_TimingInterval
288                                                                                                 -
289                                                                                                 1;
290                                                                                         ul_TimerValue
291                                                                                                 =
292                                                                                                 ul_TimerValue
293                                                                                                 -
294                                                                                                 2;
295
296                                                                                         if (b_PCIInputClock != APCI1710_40MHZ) {
297                                                                                                 ul_TimerValue
298                                                                                                         =
299                                                                                                         (unsigned int)
300                                                                                                         (
301                                                                                                         (double)
302                                                                                                         (ul_TimerValue)
303                                                                                                         *
304                                                                                                         1.007752288);
305                                                                                         }
306
307                                                                                         break;
308
309                                       /******/
310                                                                                         /* æs */
311                                       /******/
312
313                                                                                 case 1:
314
315                                               /******************/
316                                                                                         /* Timer 0 factor */
317                                               /******************/
318
319                                                                                         ul_TimerValue
320                                                                                                 =
321                                                                                                 (unsigned int)
322                                                                                                 (ul_TimingInterval
323                                                                                                 *
324                                                                                                 (0.25 * b_PCIInputClock));
325
326                                               /*******************/
327                                                                                         /* Round the value */
328                                               /*******************/
329
330                                                                                         if ((double)((double)ul_TimingInterval * (0.25 * (double)b_PCIInputClock)) >= ((double)((double)ul_TimerValue + 0.5))) {
331                                                                                                 ul_TimerValue
332                                                                                                         =
333                                                                                                         ul_TimerValue
334                                                                                                         +
335                                                                                                         1;
336                                                                                         }
337
338                                               /*****************************/
339                                                                                         /* Calculate the real timing */
340                                               /*****************************/
341
342                                                                                         ul_RealTimingInterval
343                                                                                                 =
344                                                                                                 (unsigned int)
345                                                                                                 (ul_TimerValue
346                                                                                                 /
347                                                                                                 (0.25 * (double)b_PCIInputClock));
348                                                                                         d_RealTimingInterval
349                                                                                                 =
350                                                                                                 (double)
351                                                                                                 ul_TimerValue
352                                                                                                 /
353                                                                                                 (
354                                                                                                 (double)
355                                                                                                 0.25
356                                                                                                 *
357                                                                                                 (double)
358                                                                                                 b_PCIInputClock);
359
360                                                                                         if ((double)((double)ul_TimerValue / (0.25 * (double)b_PCIInputClock)) >= (double)((double)ul_RealTimingInterval + 0.5)) {
361                                                                                                 ul_RealTimingInterval
362                                                                                                         =
363                                                                                                         ul_RealTimingInterval
364                                                                                                         +
365                                                                                                         1;
366                                                                                         }
367
368                                                                                         ul_TimingInterval
369                                                                                                 =
370                                                                                                 ul_TimingInterval
371                                                                                                 -
372                                                                                                 1;
373                                                                                         ul_TimerValue
374                                                                                                 =
375                                                                                                 ul_TimerValue
376                                                                                                 -
377                                                                                                 2;
378
379                                                                                         if (b_PCIInputClock != APCI1710_40MHZ) {
380                                                                                                 ul_TimerValue
381                                                                                                         =
382                                                                                                         (unsigned int)
383                                                                                                         (
384                                                                                                         (double)
385                                                                                                         (ul_TimerValue)
386                                                                                                         *
387                                                                                                         1.007752288);
388                                                                                         }
389
390                                                                                         break;
391
392                                       /******/
393                                                                                         /* ms */
394                                       /******/
395
396                                                                                 case 2:
397
398                                               /******************/
399                                                                                         /* Timer 0 factor */
400                                               /******************/
401
402                                                                                         ul_TimerValue
403                                                                                                 =
404                                                                                                 ul_TimingInterval
405                                                                                                 *
406                                                                                                 (250.0
407                                                                                                 *
408                                                                                                 b_PCIInputClock);
409
410                                               /*******************/
411                                                                                         /* Round the value */
412                                               /*******************/
413
414                                                                                         if ((double)((double)ul_TimingInterval * (250.0 * (double)b_PCIInputClock)) >= ((double)((double)ul_TimerValue + 0.5))) {
415                                                                                                 ul_TimerValue
416                                                                                                         =
417                                                                                                         ul_TimerValue
418                                                                                                         +
419                                                                                                         1;
420                                                                                         }
421
422                                               /*****************************/
423                                                                                         /* Calculate the real timing */
424                                               /*****************************/
425
426                                                                                         ul_RealTimingInterval
427                                                                                                 =
428                                                                                                 (unsigned int)
429                                                                                                 (ul_TimerValue
430                                                                                                 /
431                                                                                                 (250.0 * (double)b_PCIInputClock));
432                                                                                         d_RealTimingInterval
433                                                                                                 =
434                                                                                                 (double)
435                                                                                                 ul_TimerValue
436                                                                                                 /
437                                                                                                 (250.0
438                                                                                                 *
439                                                                                                 (double)
440                                                                                                 b_PCIInputClock);
441
442                                                                                         if ((double)((double)ul_TimerValue / (250.0 * (double)b_PCIInputClock)) >= (double)((double)ul_RealTimingInterval + 0.5)) {
443                                                                                                 ul_RealTimingInterval
444                                                                                                         =
445                                                                                                         ul_RealTimingInterval
446                                                                                                         +
447                                                                                                         1;
448                                                                                         }
449
450                                                                                         ul_TimingInterval
451                                                                                                 =
452                                                                                                 ul_TimingInterval
453                                                                                                 -
454                                                                                                 1;
455                                                                                         ul_TimerValue
456                                                                                                 =
457                                                                                                 ul_TimerValue
458                                                                                                 -
459                                                                                                 2;
460
461                                                                                         if (b_PCIInputClock != APCI1710_40MHZ) {
462                                                                                                 ul_TimerValue
463                                                                                                         =
464                                                                                                         (unsigned int)
465                                                                                                         (
466                                                                                                         (double)
467                                                                                                         (ul_TimerValue)
468                                                                                                         *
469                                                                                                         1.007752288);
470                                                                                         }
471
472                                                                                         break;
473
474                                       /*****/
475                                                                                         /* s */
476                                       /*****/
477
478                                                                                 case 3:
479
480                                               /******************/
481                                                                                         /* Timer 0 factor */
482                                               /******************/
483
484                                                                                         ul_TimerValue
485                                                                                                 =
486                                                                                                 (unsigned int)
487                                                                                                 (ul_TimingInterval
488                                                                                                 *
489                                                                                                 (250000.0
490                                                                                                         *
491                                                                                                         b_PCIInputClock));
492
493                                               /*******************/
494                                                                                         /* Round the value */
495                                               /*******************/
496
497                                                                                         if ((double)((double)ul_TimingInterval * (250000.0 * (double)b_PCIInputClock)) >= ((double)((double)ul_TimerValue + 0.5))) {
498                                                                                                 ul_TimerValue
499                                                                                                         =
500                                                                                                         ul_TimerValue
501                                                                                                         +
502                                                                                                         1;
503                                                                                         }
504
505                                               /*****************************/
506                                                                                         /* Calculate the real timing */
507                                               /*****************************/
508
509                                                                                         ul_RealTimingInterval
510                                                                                                 =
511                                                                                                 (unsigned int)
512                                                                                                 (ul_TimerValue
513                                                                                                 /
514                                                                                                 (250000.0
515                                                                                                         *
516                                                                                                         (double)
517                                                                                                         b_PCIInputClock));
518                                                                                         d_RealTimingInterval
519                                                                                                 =
520                                                                                                 (double)
521                                                                                                 ul_TimerValue
522                                                                                                 /
523                                                                                                 (250000.0
524                                                                                                 *
525                                                                                                 (double)
526                                                                                                 b_PCIInputClock);
527
528                                                                                         if ((double)((double)ul_TimerValue / (250000.0 * (double)b_PCIInputClock)) >= (double)((double)ul_RealTimingInterval + 0.5)) {
529                                                                                                 ul_RealTimingInterval
530                                                                                                         =
531                                                                                                         ul_RealTimingInterval
532                                                                                                         +
533                                                                                                         1;
534                                                                                         }
535
536                                                                                         ul_TimingInterval
537                                                                                                 =
538                                                                                                 ul_TimingInterval
539                                                                                                 -
540                                                                                                 1;
541                                                                                         ul_TimerValue
542                                                                                                 =
543                                                                                                 ul_TimerValue
544                                                                                                 -
545                                                                                                 2;
546
547                                                                                         if (b_PCIInputClock != APCI1710_40MHZ) {
548                                                                                                 ul_TimerValue
549                                                                                                         =
550                                                                                                         (unsigned int)
551                                                                                                         (
552                                                                                                         (double)
553                                                                                                         (ul_TimerValue)
554                                                                                                         *
555                                                                                                         1.007752288);
556                                                                                         }
557
558                                                                                         break;
559
560                                       /******/
561                                                                                         /* mn */
562                                       /******/
563
564                                                                                 case 4:
565
566                                               /******************/
567                                                                                         /* Timer 0 factor */
568                                               /******************/
569
570                                                                                         ul_TimerValue
571                                                                                                 =
572                                                                                                 (unsigned int)
573                                                                                                 (
574                                                                                                 (ul_TimingInterval
575                                                                                                         *
576                                                                                                         60)
577                                                                                                 *
578                                                                                                 (250000.0
579                                                                                                         *
580                                                                                                         b_PCIInputClock));
581
582                                               /*******************/
583                                                                                         /* Round the value */
584                                               /*******************/
585
586                                                                                         if ((double)((double)(ul_TimingInterval * 60.0) * (250000.0 * (double)b_PCIInputClock)) >= ((double)((double)ul_TimerValue + 0.5))) {
587                                                                                                 ul_TimerValue
588                                                                                                         =
589                                                                                                         ul_TimerValue
590                                                                                                         +
591                                                                                                         1;
592                                                                                         }
593
594                                               /*****************************/
595                                                                                         /* Calculate the real timing */
596                                               /*****************************/
597
598                                                                                         ul_RealTimingInterval
599                                                                                                 =
600                                                                                                 (unsigned int)
601                                                                                                 (ul_TimerValue
602                                                                                                 /
603                                                                                                 (250000.0
604                                                                                                         *
605                                                                                                         (double)
606                                                                                                         b_PCIInputClock))
607                                                                                                 /
608                                                                                                 60;
609                                                                                         d_RealTimingInterval
610                                                                                                 =
611                                                                                                 (
612                                                                                                 (double)
613                                                                                                 ul_TimerValue
614                                                                                                 /
615                                                                                                 (250000.0
616                                                                                                         *
617                                                                                                         (double)
618                                                                                                         b_PCIInputClock))
619                                                                                                 /
620                                                                                                 60.0;
621
622                                                                                         if ((double)(((double)ul_TimerValue / (250000.0 * (double)b_PCIInputClock)) / 60.0) >= (double)((double)ul_RealTimingInterval + 0.5)) {
623                                                                                                 ul_RealTimingInterval
624                                                                                                         =
625                                                                                                         ul_RealTimingInterval
626                                                                                                         +
627                                                                                                         1;
628                                                                                         }
629
630                                                                                         ul_TimingInterval
631                                                                                                 =
632                                                                                                 ul_TimingInterval
633                                                                                                 -
634                                                                                                 1;
635                                                                                         ul_TimerValue
636                                                                                                 =
637                                                                                                 ul_TimerValue
638                                                                                                 -
639                                                                                                 2;
640
641                                                                                         if (b_PCIInputClock != APCI1710_40MHZ) {
642                                                                                                 ul_TimerValue
643                                                                                                         =
644                                                                                                         (unsigned int)
645                                                                                                         (
646                                                                                                         (double)
647                                                                                                         (ul_TimerValue)
648                                                                                                         *
649                                                                                                         1.007752288);
650                                                                                         }
651
652                                                                                         break;
653                                                                                 }
654
655                                                                                 fpu_end();
656                                                                         }       /*  if (b_PCIInputClock != APCI1710_GATE_INPUT) */
657                                                                         else {
658                                    /*************************************************************/
659                                                                                 /* 2 Clock used for the overflow and the reload from counter */
660                                    /*************************************************************/
661
662                                                                                 ul_TimerValue
663                                                                                         =
664                                                                                         ul_TimingInterval
665                                                                                         -
666                                                                                         2;
667                                                                         }       /*  if (b_PCIInputClock != APCI1710_GATE_INPUT) */
668
669                                 /****************************/
670                                                                         /* Save the PCI input clock */
671                                 /****************************/
672                                                                         devpriv->
673                                                                                 s_ModuleInfo
674                                                                                 [b_ModulNbr].
675                                                                                 s_TorCounterModuleInfo.
676                                                                                 b_PCIInputClock
677                                                                                 =
678                                                                                 b_PCIInputClock;
679
680                                 /************************/
681                                                                         /* Save the timing unit */
682                                 /************************/
683
684                                                                         devpriv->
685                                                                                 s_ModuleInfo
686                                                                                 [b_ModulNbr].
687                                                                                 s_TorCounterModuleInfo.
688                                                                                 s_TorCounterInfo
689                                                                                 [b_TorCounter].
690                                                                                 b_TimingUnit
691                                                                                 =
692                                                                                 b_TimingUnit;
693
694                                 /************************/
695                                                                         /* Save the base timing */
696                                 /************************/
697                                                                         devpriv->
698                                                                                 s_ModuleInfo
699                                                                                 [b_ModulNbr].
700                                                                                 s_TorCounterModuleInfo.
701                                                                                 s_TorCounterInfo
702                                                                                 [b_TorCounter].
703                                                                                 d_TimingInterval
704                                                                                 =
705                                                                                 d_RealTimingInterval;
706
707                                                                         devpriv->
708                                                                                 s_ModuleInfo
709                                                                                 [b_ModulNbr].
710                                                                                 s_TorCounterModuleInfo.
711                                                                                 s_TorCounterInfo
712                                                                                 [b_TorCounter].
713                                                                                 ul_RealTimingInterval
714                                                                                 =
715                                                                                 ul_RealTimingInterval;
716
717                                 /*******************/
718                                                                         /* Get the command */
719                                 /*******************/
720
721                                                                         dw_Command
722                                                                                 =
723                                                                                 inl
724                                                                                 (devpriv->
725                                                                                 s_BoardInfos.
726                                                                                 ui_Address
727                                                                                 +
728                                                                                 4
729                                                                                 +
730                                                                                 (16 * b_TorCounter) + (64 * b_ModulNbr));
731
732                                                                         dw_Command
733                                                                                 =
734                                                                                 (dw_Command
735                                                                                 >>
736                                                                                 4)
737                                                                                 &
738                                                                                 0xF;
739
740                                 /******************/
741                                                                         /* Test if 40 MHz */
742                                 /******************/
743
744                                                                         if (b_PCIInputClock == APCI1710_40MHZ) {
745                                    /****************************/
746                                                                                 /* Set the 40 MHz selection */
747                                    /****************************/
748
749                                                                                 dw_Command
750                                                                                         =
751                                                                                         dw_Command
752                                                                                         |
753                                                                                         0x10;
754                                                                         }
755
756                                 /*****************************/
757                                                                         /* Test if extern clock used */
758                                 /*****************************/
759
760                                                                         if (b_PCIInputClock == APCI1710_GATE_INPUT) {
761                                    /****************************/
762                                                                                 /* Set the 40 MHz selection */
763                                    /****************************/
764
765                                                                                 dw_Command
766                                                                                         =
767                                                                                         dw_Command
768                                                                                         |
769                                                                                         0x20;
770                                                                         }
771
772                                 /*************************/
773                                                                         /* Write the new command */
774                                 /*************************/
775
776                                                                         outl(dw_Command, devpriv->s_BoardInfos.ui_Address + 4 + (16 * b_TorCounter) + (64 * b_ModulNbr));
777
778                                 /*******************/
779                                                                         /* Disable the tor */
780                                 /*******************/
781
782                                                                         outl(0, devpriv->s_BoardInfos.ui_Address + 8 + (16 * b_TorCounter) + (64 * b_ModulNbr));
783                                 /*************************/
784                                                                         /* Set the timer 1 value */
785                                 /*************************/
786
787                                                                         outl(ul_TimerValue, devpriv->s_BoardInfos.ui_Address + 0 + (16 * b_TorCounter) + (64 * b_ModulNbr));
788
789                                 /*********************/
790                                                                         /* Tor counter init. */
791                                 /*********************/
792
793                                                                         devpriv->
794                                                                                 s_ModuleInfo
795                                                                                 [b_ModulNbr].
796                                                                                 s_TorCounterModuleInfo.
797                                                                                 s_TorCounterInfo
798                                                                                 [b_TorCounter].
799                                                                                 b_TorCounterInit
800                                                                                 =
801                                                                                 1;
802                                                                 } else {
803                                 /***********************************************/
804                                                                         /* TOR version error for 40MHz clock selection */
805                                 /***********************************************/
806
807                                                                         DPRINTK("TOR version error for 40MHz clock selection\n");
808                                                                         i_ReturnValue
809                                                                                 =
810                                                                                 -9;
811                                                                 }
812                                                         } else {
813                              /**************************************************************/
814                                                                 /* You can not used the 40MHz clock selection wich this board */
815                              /**************************************************************/
816
817                                                                 DPRINTK("You can not used the 40MHz clock selection wich this board\n");
818                                                                 i_ReturnValue =
819                                                                         -8;
820                                                         }
821                                                 } else {
822                           /**********************************/
823                                                         /* Base timing selection is wrong */
824                           /**********************************/
825
826                                                         DPRINTK("Base timing selection is wrong\n");
827                                                         i_ReturnValue = -7;
828                                                 }
829                                         }       /*  if ((b_TimingUnit >= 0) && (b_TimingUnit <= 4)) */
830                                         else {
831                        /**********************************/
832                                                 /* Timing unit selection is wrong */
833                        /**********************************/
834
835                                                 DPRINTK("Timing unit selection is wrong\n");
836                                                 i_ReturnValue = -6;
837                                         }       /*  if ((b_TimingUnit >= 0) && (b_TimingUnit <= 4)) */
838                                 }       /*  if ((b_PCIInputClock == APCI1710_30MHZ) || (b_PCIInputClock == APCI1710_33MHZ)) */
839                                 else {
840                     /*****************************************/
841                                         /* The selected PCI input clock is wrong */
842                     /*****************************************/
843
844                                         DPRINTK("The selected PCI input clock is wrong\n");
845                                         i_ReturnValue = -5;
846                                 }       /*  if ((b_PCIInputClock == APCI1710_30MHZ) || (b_PCIInputClock == APCI1710_33MHZ)) */
847                         }       /*  if (b_TorCounterMode >= 0 && b_TorCounterMode <= 7) */
848                         else {
849                  /**********************************/
850                                 /* Tor Counter selection is wrong */
851                  /**********************************/
852
853                                 DPRINTK("Tor Counter selection is wrong\n");
854                                 i_ReturnValue = -4;
855                         }       /*  if (b_TorCounterMode >= 0 && b_TorCounterMode <= 7) */
856                 } else {
857               /******************************************/
858                         /* The module is not a tor counter module */
859               /******************************************/
860
861                         DPRINTK("The module is not a tor counter module\n");
862                         i_ReturnValue = -3;
863                 }
864         } else {
865            /***********************/
866                 /* Module number error */
867            /***********************/
868
869                 DPRINTK("Module number error\n");
870                 i_ReturnValue = -2;
871         }
872         data[0] = (unsigned int) ul_RealTimingInterval;
873         return i_ReturnValue;
874 }
875
876 /*
877 +----------------------------------------------------------------------------+
878 | Function Name     : _INT_ i_APCI1710_EnableTorCounter                      |
879 |                                               (unsigned char_ b_BoardHandle,        |
880 |                                                unsigned char_ b_ModulNbr,           |
881 |                                                unsigned char_ b_TorCounter,         |
882 |                                                unsigned char_ b_InputMode,          |
883 |                                                unsigned char_ b_ExternGate,         |
884 |                                                unsigned char_ b_CycleMode,          |
885 |                                                unsigned char_ b_InterruptEnable)    |
886 +----------------------------------------------------------------------------+
887 | Task              : Enable the tor counter (b_TorCounter) from selected    |
888 |                     module (b_ModulNbr). You must calling the              |
889 |                     "i_APCI1710_InitTorCounter" function be for you call   |
890 |                     this function.                                         |
891 |                     If you enable the tor counter interrupt, the           |
892 |                     tor counter generate a interrupt after the timing cycle|
893 |                     See function "i_APCI1710_SetBoardIntRoutineX" and the  |
894 |                     Interrupt mask description chapter from this manual.   |
895 |                     The b_CycleMode parameter determine if you will        |
896 |                     measured a single or more cycle.                       |
897 +----------------------------------------------------------------------------+
898 | Input Parameters  : unsigned char_ b_BoardHandle  : Handle of board APCI-1710       |
899 |                     unsigned char_ b_ModulNbr     : Selected module number (0 to 3) |
900 |                     unsigned char_ b_TorCounter   : Tor counter selection (0 or 1). |
901 |                     unsigned char_ b_InputMode    : Input signal level selection    |
902 |                                               0 : Tor count each low level |
903 |                                               1 : Tor count each high level|
904 |                     unsigned char_ b_ExternGate   : Extern gate action selection    |
905 |                                               0 : Extern gate signal not   |
906 |                                                   used                     |
907 |                                               1 : Extern gate signal used. |
908 |                                                   If you selected the      |
909 |                                                   single mode, each high   |
910 |                                                   level signal start the   |
911 |                                                   counter.                 |
912 |                                                   If you selected the      |
913 |                                                   continuous mode, the     |
914 |                                                   first high level signal  |
915 |                                                   start the tor counter    |
916 |                                                                            |
917 |                                             APCI1710_TOR_QUADRUPLE _MODE : |
918 |                                             In the quadruple mode, the edge|
919 |                                             analysis circuit generates a   |
920 |                                             counting pulse from each edge  |
921 |                                             of 2 signals which are phase   |
922 |                                             shifted in relation to each    |
923 |                                             other.                         |
924 |                                             The gate input is used for the |
925 |                                             signal B                       |
926 |                                                                            |
927 |                                             APCI1710_TOR_DOUBLE_MODE:      |
928 |                                             Functions in the same way as   |
929 |                                             the quadruple mode, except that|
930 |                                             only two of the four edges are |
931 |                                             analysed per period.           |
932 |                                             The gate input is used for the |
933 |                                             signal B                       |
934 |                                                                            |
935 |                                             APCI1710_TOR_SIMPLE_MODE:      |
936 |                                             Functions in the same way as   |
937 |                                             the quadruple mode, except that|
938 |                                             only one of the four edges is  |
939 |                                             analysed per period.           |
940 |                                             The gate input is used for the |
941 |                                             signal B                       |
942 |                                                                            |
943 |                     unsigned char_ b_CycleMode    : Selected the tor counter        |
944 |                                            acquisition mode                |
945 |                     unsigned char_ b_InterruptEnable : Enable or disable the        |
946 |                                               tor counter interrupt.       |
947 |                                               APCI1710_ENABLE:             |
948 |                                               Enable the tor counter       |
949 |                                               interrupt                    |
950 |                                               APCI1710_DISABLE:            |
951 |                                               Disable the tor counter      |
952 |                                               interrupt                    |
953 +----------------------------------------------------------------------------+
954 | Output Parameters : -                                                      |
955 +----------------------------------------------------------------------------+
956 | Return Value      :  0: No error                                           |
957 |                     -1: The handle parameter of the board is wrong         |
958 |                     -2: Module selection wrong                             |
959 |                     -3: The module is not a tor counter module             |
960 |                     -4: Tor counter selection is wrong                     |
961 |                     -5: Tor counter not initialised see function           |
962 |                         "i_APCI1710_InitTorCounter"                        |
963 |                     -6: Tor input signal selection is wrong                |
964 |                     -7: Extern gate signal mode is wrong                   |
965 |                     -8: Tor counter acquisition mode cycle is wrong        |
966 |                     -9: Interrupt parameter is wrong                       |
967 |                     -10:Interrupt function not initialised.                |
968 |                         See function "i_APCI1710_SetBoardIntRoutineX"      |
969 +----------------------------------------------------------------------------+
970 */
971 /*
972 +----------------------------------------------------------------------------+
973 | Function Name     : _INT_ i_APCI1710_DisableTorCounter                     |
974 |                                               (unsigned char_  b_BoardHandle,       |
975 |                                                unsigned char_  b_ModulNbr,          |
976 |                                                unsigned char_  b_TorCounter)        |
977 +----------------------------------------------------------------------------+
978 | Task              : Disable the tor counter (b_TorCounter) from selected   |
979 |                     module (b_ModulNbr). If you disable the tor counter    |
980 |                     after a start cycle occur and you restart the tor      |
981 |                     counter witch the " i_APCI1710_EnableTorCounter"       |
982 |                     function, the status register is cleared               |
983 +----------------------------------------------------------------------------+
984 | Input Parameters  : unsigned char_ b_BoardHandle  : Handle of board APCI-1710       |
985 |                     unsigned char_ b_ModulNbr     : Selected module number (0 to 3) |
986 |                     unsigned char_ b_TorCounter   : Tor counter selection (0 or 1). |
987 +----------------------------------------------------------------------------+
988 | Output Parameters : -                                                      |
989 +----------------------------------------------------------------------------+
990 | Return Value      :  0: No error                                           |
991 |                     -1: The handle parameter of the board is wrong         |
992 |                     -2: Module selection wrong                             |
993 |                     -3: The module is not a tor counter module             |
994 |                     -4: Tor counter selection is wrong                     |
995 |                     -5: Tor counter not initialised see function           |
996 |                         "i_APCI1710_InitTorCounter"                        |
997 |                     -6: Tor counter not enabled see function               |
998 |                         "i_APCI1710_EnableTorCounter"                      |
999 +----------------------------------------------------------------------------+
1000 */
1001
1002 static int i_APCI1710_InsnWriteEnableDisableTorCounter(struct comedi_device *dev,
1003                                                        struct comedi_subdevice *s,
1004                                                        struct comedi_insn *insn,
1005                                                        unsigned int *data)
1006 {
1007         struct addi_private *devpriv = dev->private;
1008         int i_ReturnValue = 0;
1009         unsigned int dw_Status;
1010         unsigned int dw_DummyRead;
1011         unsigned int dw_ConfigReg;
1012         unsigned char b_ModulNbr, b_Action;
1013         unsigned char b_TorCounter;
1014         unsigned char b_InputMode;
1015         unsigned char b_ExternGate;
1016         unsigned char b_CycleMode;
1017         unsigned char b_InterruptEnable;
1018
1019         b_ModulNbr = (unsigned char) CR_AREF(insn->chanspec);
1020         b_Action = (unsigned char) data[0];     /*  enable or disable */
1021         b_TorCounter = (unsigned char) data[1];
1022         b_InputMode = (unsigned char) data[2];
1023         b_ExternGate = (unsigned char) data[3];
1024         b_CycleMode = (unsigned char) data[4];
1025         b_InterruptEnable = (unsigned char) data[5];
1026         i_ReturnValue = insn->n;
1027         devpriv->tsk_Current = current; /*  Save the current process task structure */
1028         /**************************/
1029         /* Test the module number */
1030         /**************************/
1031
1032         if (b_ModulNbr < 4) {
1033            /***********************/
1034                 /* Test if tor counter */
1035            /***********************/
1036
1037                 if ((devpriv->s_BoardInfos.
1038                                 dw_MolduleConfiguration[b_ModulNbr] &
1039                                 0xFFFF0000UL) == APCI1710_TOR_COUNTER) {
1040               /**********************************/
1041                         /* Test the tor counter selection */
1042               /**********************************/
1043
1044                         if (b_TorCounter <= 1) {
1045                                 switch (b_Action)       /*  Enable or Disable */
1046                                 {
1047                                 case APCI1710_ENABLE:
1048                  /***********************************/
1049                                         /* Test if tor counter initialised */
1050                  /***********************************/
1051
1052                                         dw_Status =
1053                                                 inl(devpriv->s_BoardInfos.
1054                                                 ui_Address + 8 +
1055                                                 (16 * b_TorCounter) +
1056                                                 (64 * b_ModulNbr));
1057
1058                                         if (dw_Status & 0x10) {
1059                     /******************************/
1060                                                 /* Test the input signal mode */
1061                     /******************************/
1062
1063                                                 if (b_InputMode == 0 ||
1064                                                         b_InputMode == 1 ||
1065                                                         b_InputMode ==
1066                                                         APCI1710_TOR_SIMPLE_MODE
1067                                                         || b_InputMode ==
1068                                                         APCI1710_TOR_DOUBLE_MODE
1069                                                         || b_InputMode ==
1070                                                         APCI1710_TOR_QUADRUPLE_MODE)
1071                                                 {
1072                        /************************************/
1073                                                         /* Test the extern gate signal mode */
1074                        /************************************/
1075
1076                                                         if (b_ExternGate == 0
1077                                                                 || b_ExternGate
1078                                                                 == 1
1079                                                                 || b_InputMode >
1080                                                                 1) {
1081                           /*********************************/
1082                                                                 /* Test the cycle mode parameter */
1083                           /*********************************/
1084
1085                                                                 if ((b_CycleMode == APCI1710_SINGLE) || (b_CycleMode == APCI1710_CONTINUOUS)) {
1086                              /***************************/
1087                                                                         /* Test the interrupt flag */
1088                              /***************************/
1089
1090                                                                         if ((b_InterruptEnable == APCI1710_ENABLE) || (b_InterruptEnable == APCI1710_DISABLE)) {
1091
1092                                    /***************************/
1093                                                                                 /* Save the interrupt mode */
1094                                    /***************************/
1095
1096                                                                                 devpriv->
1097                                                                                         s_ModuleInfo
1098                                                                                         [b_ModulNbr].
1099                                                                                         s_TorCounterModuleInfo.
1100                                                                                         s_TorCounterInfo
1101                                                                                         [b_TorCounter].
1102                                                                                         b_InterruptEnable
1103                                                                                         =
1104                                                                                         b_InterruptEnable;
1105
1106                                    /*******************/
1107                                                                                 /* Get the command */
1108                                    /*******************/
1109
1110                                                                                 dw_ConfigReg
1111                                                                                         =
1112                                                                                         inl
1113                                                                                         (devpriv->
1114                                                                                         s_BoardInfos.
1115                                                                                         ui_Address
1116                                                                                         +
1117                                                                                         4
1118                                                                                         +
1119                                                                                         (16 * b_TorCounter) + (64 * b_ModulNbr));
1120
1121                                                                                 dw_ConfigReg
1122                                                                                         =
1123                                                                                         (dw_ConfigReg
1124                                                                                         >>
1125                                                                                         4)
1126                                                                                         &
1127                                                                                         0x30;
1128
1129                                    /********************************/
1130                                                                                 /* Test if not direct mode used */
1131                                    /********************************/
1132
1133                                                                                 if (b_InputMode > 1) {
1134                                       /*******************************/
1135                                                                                         /* Extern gate can not be used */
1136                                       /*******************************/
1137
1138                                                                                         b_ExternGate
1139                                                                                                 =
1140                                                                                                 0;
1141
1142                                       /*******************************************/
1143                                                                                         /* Enable the extern gate for the Signal B */
1144                                       /*******************************************/
1145
1146                                                                                         dw_ConfigReg
1147                                                                                                 =
1148                                                                                                 dw_ConfigReg
1149                                                                                                 |
1150                                                                                                 0x40;
1151
1152                                       /***********************/
1153                                                                                         /* Test if simple mode */
1154                                       /***********************/
1155
1156                                                                                         if (b_InputMode == APCI1710_TOR_SIMPLE_MODE) {
1157                                          /**************************/
1158                                                                                                 /* Enable the sinple mode */
1159                                          /**************************/
1160
1161                                                                                                 dw_ConfigReg
1162                                                                                                         =
1163                                                                                                         dw_ConfigReg
1164                                                                                                         |
1165                                                                                                         0x780;
1166
1167                                                                                         }       /*  if (b_InputMode == APCI1710_TOR_SIMPLE_MODE) */
1168
1169                                       /***********************/
1170                                                                                         /* Test if double mode */
1171                                       /***********************/
1172
1173                                                                                         if (b_InputMode == APCI1710_TOR_DOUBLE_MODE) {
1174                                          /**************************/
1175                                                                                                 /* Enable the double mode */
1176                                          /**************************/
1177
1178                                                                                                 dw_ConfigReg
1179                                                                                                         =
1180                                                                                                         dw_ConfigReg
1181                                                                                                         |
1182                                                                                                         0x180;
1183
1184                                                                                         }       /*  if (b_InputMode == APCI1710_TOR_DOUBLE_MODE) */
1185
1186                                                                                         b_InputMode
1187                                                                                                 =
1188                                                                                                 0;
1189                                                                                 }       /*  if (b_InputMode > 1) */
1190
1191                                    /*******************/
1192                                                                                 /* Set the command */
1193                                    /*******************/
1194
1195                                                                                 dw_ConfigReg
1196                                                                                         =
1197                                                                                         dw_ConfigReg
1198                                                                                         |
1199                                                                                         b_CycleMode
1200                                                                                         |
1201                                                                                         (b_InterruptEnable
1202                                                                                         *
1203                                                                                         2)
1204                                                                                         |
1205                                                                                         (b_InputMode
1206                                                                                         *
1207                                                                                         4)
1208                                                                                         |
1209                                                                                         (b_ExternGate
1210                                                                                         *
1211                                                                                         8);
1212
1213                                    /*****************************/
1214                                                                                 /* Clear the status register */
1215                                    /*****************************/
1216
1217                                                                                 dw_DummyRead
1218                                                                                         =
1219                                                                                         inl
1220                                                                                         (devpriv->
1221                                                                                         s_BoardInfos.
1222                                                                                         ui_Address
1223                                                                                         +
1224                                                                                         0
1225                                                                                         +
1226                                                                                         (16 * b_TorCounter) + (64 * b_ModulNbr));
1227
1228                                    /***************************************/
1229                                                                                 /* Clear the interrupt status register */
1230                                    /***************************************/
1231
1232                                                                                 dw_DummyRead
1233                                                                                         =
1234                                                                                         inl
1235                                                                                         (devpriv->
1236                                                                                         s_BoardInfos.
1237                                                                                         ui_Address
1238                                                                                         +
1239                                                                                         12
1240                                                                                         +
1241                                                                                         (16 * b_TorCounter) + (64 * b_ModulNbr));
1242
1243                                    /********************/
1244                                                                                 /* Set the commando */
1245                                    /********************/
1246
1247                                                                                 outl(dw_ConfigReg, devpriv->s_BoardInfos.ui_Address + 4 + (16 * b_TorCounter) + (64 * b_ModulNbr));
1248
1249                                    /****************/
1250                                                                                 /* Set the gate */
1251                                    /****************/
1252
1253                                                                                 outl(1, devpriv->s_BoardInfos.ui_Address + 8 + (16 * b_TorCounter) + (64 * b_ModulNbr));
1254
1255                                                                         }       /*  if ((b_InterruptEnable == APCI1710_ENABLE) || (b_InterruptEnable == APCI1710_DISABLE)) */
1256                                                                         else {
1257                                 /********************************/
1258                                                                                 /* Interrupt parameter is wrong */
1259                                 /********************************/
1260
1261                                                                                 DPRINTK("Interrupt parameter is wrong\n");
1262                                                                                 i_ReturnValue
1263                                                                                         =
1264                                                                                         -9;
1265                                                                         }       /*  if ((b_InterruptEnable == APCI1710_ENABLE) || (b_InterruptEnable == APCI1710_DISABLE)) */
1266                                                                 }       /*  if ((b_CycleMode == APCI1710_SINGLE) || (b_CycleMode == APCI1710_CONTINUOUS)) */
1267                                                                 else {
1268                              /***********************************************/
1269                                                                         /* Tor counter acquisition mode cycle is wrong */
1270                              /***********************************************/
1271
1272                                                                         DPRINTK("Tor counter acquisition mode cycle is wrong\n");
1273                                                                         i_ReturnValue
1274                                                                                 =
1275                                                                                 -8;
1276                                                                 }       /*  if ((b_CycleMode == APCI1710_SINGLE) || (b_CycleMode == APCI1710_CONTINUOUS)) */
1277                                                         }       /*  if (b_ExternGate >= 0 && b_ExternGate <= 1) */
1278                                                         else {
1279                           /***********************************/
1280                                                                 /* Extern gate input mode is wrong */
1281                           /***********************************/
1282
1283                                                                 DPRINTK("Extern gate input mode is wrong\n");
1284                                                                 i_ReturnValue =
1285                                                                         -7;
1286                                                         }       /*  if (b_ExternGate >= 0 && b_ExternGate <= 1) */
1287                                                 }       /*  if (b_InputMode >= 0 && b_InputMode <= 1) */
1288                                                 else {
1289                        /***************************************/
1290                                                         /* Tor input signal selection is wrong */
1291                        /***************************************/
1292
1293                                                         DPRINTK("Tor input signal selection is wrong\n");
1294                                                         i_ReturnValue = -6;
1295                                                 }
1296                                         } else {
1297                     /*******************************/
1298                                                 /* Tor counter not initialised */
1299                     /*******************************/
1300
1301                                                 DPRINTK("Tor counter not initialised\n");
1302                                                 i_ReturnValue = -5;
1303                                         }
1304                                         break;
1305
1306                                 case APCI1710_DISABLE:
1307                          /***********************************/
1308                                         /* Test if tor counter initialised */
1309                  /***********************************/
1310
1311                                         dw_Status = inl(devpriv->s_BoardInfos.
1312                                                 ui_Address + 8 +
1313                                                 (16 * b_TorCounter) +
1314                                                 (64 * b_ModulNbr));
1315
1316                  /*******************************/
1317                                         /* Test if counter initialised */
1318                  /*******************************/
1319
1320                                         if (dw_Status & 0x10) {
1321                     /***************************/
1322                                                 /* Test if counter enabled */
1323                     /***************************/
1324
1325                                                 if (dw_Status & 0x1) {
1326                        /****************************/
1327                                                         /* Clear the interrupt mode */
1328                        /****************************/
1329                                                         devpriv->
1330                                                                 s_ModuleInfo
1331                                                                 [b_ModulNbr].
1332                                                                 s_TorCounterModuleInfo.
1333                                                                 s_TorCounterInfo
1334                                                                 [b_TorCounter].
1335                                                                 b_InterruptEnable
1336                                                                 =
1337                                                                 APCI1710_DISABLE;
1338
1339                        /******************/
1340                                                         /* Clear the gate */
1341                        /******************/
1342
1343                                                         outl(0, devpriv->
1344                                                                 s_BoardInfos.
1345                                                                 ui_Address + 8 +
1346                                                                 (16 * b_TorCounter) + (64 * b_ModulNbr));
1347                                                 }       /*  if (dw_Status & 0x1) */
1348                                                 else {
1349                        /***************************/
1350                                                         /* Tor counter not enabled */
1351                        /***************************/
1352
1353                                                         DPRINTK("Tor counter not enabled \n");
1354                                                         i_ReturnValue = -6;
1355                                                 }       /*  if (dw_Status & 0x1) */
1356                                         }       /*  if (dw_Status & 0x10) */
1357                                         else {
1358                     /*******************************/
1359                                                 /* Tor counter not initialised */
1360                     /*******************************/
1361
1362                                                 DPRINTK("Tor counter not initialised\n");
1363                                                 i_ReturnValue = -5;
1364                                         }       /*  // if (dw_Status & 0x10) */
1365
1366                                 }       /*  switch */
1367                         }       /*  if (b_TorCounter <= 1) */
1368                         else {
1369                  /**********************************/
1370                                 /* Tor counter selection is wrong */
1371                  /**********************************/
1372
1373                                 DPRINTK("Tor counter selection is wrong\n");
1374                                 i_ReturnValue = -4;
1375                         }       /*  if (b_TorCounter <= 1) */
1376                 } else {
1377               /******************************************/
1378                         /* The module is not a tor counter module */
1379               /******************************************/
1380
1381                         DPRINTK("The module is not a tor counter module \n");
1382                         i_ReturnValue = -3;
1383                 }
1384         } else {
1385            /***********************/
1386                 /* Module number error */
1387            /***********************/
1388
1389                 DPRINTK("Module number error \n");
1390                 i_ReturnValue = -2;
1391         }
1392
1393         return i_ReturnValue;
1394 }
1395
1396 /*
1397 +----------------------------------------------------------------------------+
1398 | Function Name     : _INT_ i_APCI1710_GetTorCounterInitialisation           |
1399 |                                               (unsigned char_     b_BoardHandle,    |
1400 |                                                unsigned char_     b_ModulNbr,       |
1401 |                                                unsigned char_     b_TorCounter,     |
1402 |                                                unsigned char *_   pb_TimingUnit,     |
1403 |                                                PULONG_ pul_TimingInterval, |
1404 |                                                unsigned char *_   pb_InputMode,      |
1405 |                                                unsigned char *_   pb_ExternGate,     |
1406 |                                                unsigned char *_   pb_CycleMode,      |
1407 |                                                unsigned char *_   pb_Enable,         |
1408 |                                                unsigned char *_   pb_InterruptEnable)|
1409 +----------------------------------------------------------------------------+
1410 | Task              : Enable the tor counter (b_TorCounter) from selected    |
1411 |                     module (b_ModulNbr). You must calling the              |
1412 |                     "i_APCI1710_InitTorCounter" function be for you call   |
1413 |                     this function.                                         |
1414 |                     If you enable the tor counter interrupt, the           |
1415 |                     tor counter generate a interrupt after the timing cycle|
1416 |                     See function "i_APCI1710_SetBoardIntRoutineX" and the  |
1417 |                     Interrupt mask description chapter from this manual.   |
1418 |                     The b_CycleMode parameter determine if you will        |
1419 |                     measured a single or more cycle.                       |
1420 +----------------------------------------------------------------------------+
1421 | Input Parameters  : unsigned char_ b_BoardHandle  : Handle of board APCI-1710       |
1422 |                     unsigned char_ b_ModulNbr     : Selected module number (0 to 3) |
1423 |                     unsigned char_ b_TorCounter   : Tor counter selection (0 or 1)
1424
1425         b_ModulNbr                      =       CR_AREF(insn->chanspec);
1426         b_TorCounter            =       CR_CHAN(insn->chanspec);
1427 . |
1428 +----------------------------------------------------------------------------+
1429 | Output Parameters : unsigned char *_  pb_TimingUnit    : Base timing unit (0 to 4)   |
1430 |                                                 0 : ns                     |
1431 |                                                 1 : µs                     |
1432 |                                                 2 : ms                     |
1433 |                                                 3 : s                      |
1434 |                                                 4 : mn                     |
1435 |                     PULONG_ pul_TimingInterval : Base timing value.        |
1436 |                     unsigned char *_ pb_InputMode        : Input signal level        |
1437 |                                                  selection                 |
1438 |                                               0 : Tor count each low level |
1439 |                                               1 : Tor count each high level|
1440 |                     unsigned char *_ pb_ExternGate    : Extern gate action         |
1441 |                                                 selection                  |
1442 |                                                 0 : Extern gate signal not |
1443 |                                                     used                   |
1444 |                                                 1 : Extern gate signal used|
1445 |                     unsigned char *_ pb_CycleMode       : Tor counter acquisition    |
1446 |                                                 mode                       |
1447 |                     unsigned char *_ pb_Enable                : Indicate if the tor counter|
1448 |                                                 is enabled or no           |
1449 |                                                 0 : Tor counter disabled   |
1450 |                                                 1 : Tor counter enabled    |
1451 |                     unsigned char *_ pb_InterruptEnable : Enable or disable the      |
1452 |                                                 tor counter interrupt.     |
1453 |                                                 APCI1710_ENABLE:           |
1454 |                                                 Enable the tor counter     |
1455 |                                                 interrupt                  |
1456 |                                                 APCI1710_DISABLE:          |
1457 |                                                 Disable the tor counter    |
1458 |                                                 interrupt
1459         pb_TimingUnit           =       (unsigned char *) &data[0];
1460         pul_TimingInterval      =  (unsigned int *) &data[1];
1461         pb_InputMode            =       (unsigned char *) &data[2];
1462         pb_ExternGate           =       (unsigned char *) &data[3];
1463         pb_CycleMode            =       (unsigned char *) &data[4];
1464         pb_Enable                       =       (unsigned char *) &data[5];
1465         pb_InterruptEnable      =       (unsigned char *) &data[6];
1466                  |
1467 +----------------------------------------------------------------------------+
1468 | Return Value      :  0: No error                                           |
1469 |                     -1: The handle parameter of the board is wrong         |
1470 |                     -2: Module selection wrong                             |
1471 |                     -3: The module is not a tor counter module             |
1472 |                     -4: Tor counter selection is wrong                     |
1473 |                     -5: Tor counter not initialised see function           |
1474 |                         "i_APCI1710_InitTorCounter"                        |
1475 +----------------------------------------------------------------------------+
1476 */
1477
1478 static int i_APCI1710_InsnReadGetTorCounterInitialisation(struct comedi_device *dev,
1479                                                           struct comedi_subdevice *s,
1480                                                           struct comedi_insn *insn,
1481                                                           unsigned int *data)
1482 {
1483         struct addi_private *devpriv = dev->private;
1484         int i_ReturnValue = 0;
1485         unsigned int dw_Status;
1486         unsigned char b_ModulNbr;
1487         unsigned char b_TorCounter;
1488         unsigned char *pb_TimingUnit;
1489         unsigned int *pul_TimingInterval;
1490         unsigned char *pb_InputMode;
1491         unsigned char *pb_ExternGate;
1492         unsigned char *pb_CycleMode;
1493         unsigned char *pb_Enable;
1494         unsigned char *pb_InterruptEnable;
1495
1496         i_ReturnValue = insn->n;
1497         b_ModulNbr = CR_AREF(insn->chanspec);
1498         b_TorCounter = CR_CHAN(insn->chanspec);
1499
1500         pb_TimingUnit = (unsigned char *) &data[0];
1501         pul_TimingInterval = (unsigned int *) &data[1];
1502         pb_InputMode = (unsigned char *) &data[2];
1503         pb_ExternGate = (unsigned char *) &data[3];
1504         pb_CycleMode = (unsigned char *) &data[4];
1505         pb_Enable = (unsigned char *) &data[5];
1506         pb_InterruptEnable = (unsigned char *) &data[6];
1507
1508         /**************************/
1509         /* Test the module number */
1510         /**************************/
1511
1512         if (b_ModulNbr < 4) {
1513            /***********************/
1514                 /* Test if tor counter */
1515            /***********************/
1516
1517                 if ((devpriv->s_BoardInfos.
1518                                 dw_MolduleConfiguration[b_ModulNbr] &
1519                                 0xFFFF0000UL) == APCI1710_TOR_COUNTER) {
1520               /**********************************/
1521                         /* Test the tor counter selection */
1522               /**********************************/
1523
1524                         if (b_TorCounter <= 1) {
1525
1526                  /***********************************/
1527                                 /* Test if tor counter initialised */
1528                  /***********************************/
1529
1530                                 dw_Status = inl(devpriv->s_BoardInfos.
1531                                         ui_Address + 8 + (16 * b_TorCounter) +
1532                                         (64 * b_ModulNbr));
1533
1534                                 if (dw_Status & 0x10) {
1535                                         *pb_Enable = dw_Status & 1;
1536
1537                     /********************/
1538                                         /* Get the commando */
1539                     /********************/
1540
1541                                         dw_Status = inl(devpriv->s_BoardInfos.
1542                                                 ui_Address + 4 +
1543                                                 (16 * b_TorCounter) +
1544                                                 (64 * b_ModulNbr));
1545
1546                                         *pb_CycleMode =
1547                                                 (unsigned char) ((dw_Status >> 4) & 1);
1548                                         *pb_InterruptEnable =
1549                                                 (unsigned char) ((dw_Status >> 5) & 1);
1550
1551                     /******************************************************/
1552                                         /* Test if extern gate used for clock or for signal B */
1553                     /******************************************************/
1554
1555                                         if (dw_Status & 0x600) {
1556                        /*****************************************/
1557                                                 /* Test if extern gate used for signal B */
1558                        /*****************************************/
1559
1560                                                 if (dw_Status & 0x400) {
1561                           /***********************/
1562                                                         /* Test if simple mode */
1563                           /***********************/
1564
1565                                                         if ((dw_Status & 0x7800)
1566                                                                 == 0x7800) {
1567                                                                 *pb_InputMode =
1568                                                                         APCI1710_TOR_SIMPLE_MODE;
1569                                                         }
1570
1571                           /***********************/
1572                                                         /* Test if double mode */
1573                           /***********************/
1574
1575                                                         if ((dw_Status & 0x7800)
1576                                                                 == 0x1800) {
1577                                                                 *pb_InputMode =
1578                                                                         APCI1710_TOR_DOUBLE_MODE;
1579                                                         }
1580
1581                           /**************************/
1582                                                         /* Test if quadruple mode */
1583                           /**************************/
1584
1585                                                         if ((dw_Status & 0x7800)
1586                                                                 == 0x0000) {
1587                                                                 *pb_InputMode =
1588                                                                         APCI1710_TOR_QUADRUPLE_MODE;
1589                                                         }
1590                                                 }       /*  if (dw_Status & 0x400) */
1591                                                 else {
1592                                                         *pb_InputMode = 1;
1593                                                 }       /*  // if (dw_Status & 0x400) */
1594
1595                        /************************/
1596                                                 /* Extern gate not used */
1597                        /************************/
1598
1599                                                 *pb_ExternGate = 0;
1600                                         }       /*  if (dw_Status & 0x600) */
1601                                         else {
1602                                                 *pb_InputMode =
1603                                                         (unsigned char) ((dw_Status >> 6)
1604                                                         & 1);
1605                                                 *pb_ExternGate =
1606                                                         (unsigned char) ((dw_Status >> 7)
1607                                                         & 1);
1608                                         }       /*  if (dw_Status & 0x600) */
1609
1610                                         *pb_TimingUnit =
1611                                                 devpriv->
1612                                                 s_ModuleInfo[b_ModulNbr].
1613                                                 s_TorCounterModuleInfo.
1614                                                 s_TorCounterInfo[b_TorCounter].
1615                                                 b_TimingUnit;
1616
1617                                         *pul_TimingInterval =
1618                                                 devpriv->
1619                                                 s_ModuleInfo[b_ModulNbr].
1620                                                 s_TorCounterModuleInfo.
1621                                                 s_TorCounterInfo[b_TorCounter].
1622                                                 ul_RealTimingInterval;
1623                                 } else {
1624                     /*******************************/
1625                                         /* Tor counter not initialised */
1626                     /*******************************/
1627
1628                                         DPRINTK("Tor counter not initialised\n");
1629                                         i_ReturnValue = -5;
1630                                 }
1631
1632                         }       /*  if (b_TorCounter <= 1) */
1633                         else {
1634                  /**********************************/
1635                                 /* Tor counter selection is wrong */
1636                  /**********************************/
1637
1638                                 DPRINTK("Tor counter selection is wrong \n");
1639                                 i_ReturnValue = -4;
1640                         }       /*  if (b_TorCounter <= 1) */
1641                 } else {
1642               /******************************************/
1643                         /* The module is not a tor counter module */
1644               /******************************************/
1645
1646                         DPRINTK("The module is not a tor counter module\n");
1647                         i_ReturnValue = -3;
1648                 }
1649         } else {
1650            /***********************/
1651                 /* Module number error */
1652            /***********************/
1653
1654                 DPRINTK("Module number error\n");
1655                 i_ReturnValue = -2;
1656         }
1657
1658         return i_ReturnValue;
1659 }
1660
1661 /*
1662 +----------------------------------------------------------------------------+
1663 | Function Name     : _INT_ i_APCI1710_ReadTorCounterValue                   |
1664 |                               (unsigned char_     b_BoardHandle,                    |
1665 |                                unsigned char_     b_ModulNbr,                       |
1666 |                                unsigned char_     b_TorCounter,                     |
1667 |                                unsigned int_    ui_TimeOut,                        |
1668 |                                unsigned char *_   pb_TorCounterStatus,               |
1669 |                                PULONG_ pul_TorCounterValue)                |
1670 +----------------------------------------------------------------------------+
1671 | Task          case APCI1710_TOR_GETPROGRESSSTATUS: Return the tor counter
1672 (b_TorCounter) status (pb_TorCounterStatus) from selected tor counter        |
1673 |                     module (b_ModulNbr).
1674
1675                                  case APCI1710_TOR_GETCOUNTERVALUE :
1676   Return the tor counter (b_TorCounter) status           |
1677 |                     (pb_TorCounterStatus) and the timing value             |
1678 |                     (pul_TorCounterValue) after a conting cycle stop       |
1679 |                     from selected tor counter module (b_ModulNbr).         |
1680 +----------------------------------------------------------------------------+
1681 | Input Parameters  : unsigned char_ b_BoardHandle  : Handle of board APCI-1710       |
1682 |                     unsigned char_ b_ModulNbr     : Selected module number (0 to 3) |
1683 |                     unsigned char_ b_TorCounter   : Tor counter selection (0 or 1).
1684         b_ModulNbr    = CR_AREF(insn->chanspec);
1685         b_ReadType    = (unsigned char) data[0];
1686         b_TorCounter  = (unsigned char) data[1];
1687         ui_TimeOut        = (unsigned int) data[2]; |
1688 +----------------------------------------------------------------------------+
1689 | Output Parameters : unsigned char *_  pb_TorCounterStatus : Return the tor counter   |
1690 |                                                    status.                 |
1691 |                                               0 : Conting cycle not started|
1692 |                                                   Software gate not set.   |
1693 |                                               1 : Conting cycle started.   |
1694 |                                                   Software gate set.       |
1695 |                                               2 : Conting cycle stopped.   |
1696 |                                                   The conting cycle is     |
1697 |                                                   terminate.               |
1698 |                                               3 : A overflow occur. You    |
1699 |                                                   must change the base     |
1700 |                                                   timing witch the         |
1701 |                                                   function                 |
1702 |                                                 "i_APCI1710_InitTorCounter"|
1703 |                                               4 : Timeeout occur           |
1704 |                     unsigned int *  pul_TorCounterValue  : Tor counter value.
1705         pb_TorCounterStatus=(unsigned char *) &data[0];
1706         pul_TorCounterValue=(unsigned int *) &data[1];    |
1707 +----------------------------------------------------------------------------+
1708 | Return Value      :  0: No error                                           |
1709 |                     -1: The handle parameter of the board is wrong         |
1710 |                     -2: Module selection wrong                             |
1711 |                     -3: The module is not a tor counter module             |
1712 |                     -4: Tor counter selection is wrong                     |
1713 |                     -5: Tor counter not initialised see function           |
1714 |                         "i_APCI1710_InitTorCounter"                        |
1715 |                     -6: Tor counter not enabled see function               |
1716 |                         "i_APCI1710_EnableTorCounter"                      |
1717 |                     -7: Timeout parameter is wrong (0 to 65535)            |
1718 +----------------------------------------------------------------------------+
1719 */
1720
1721 static int i_APCI1710_InsnBitsGetTorCounterProgressStatusAndValue(struct comedi_device *dev,
1722                                                                   struct comedi_subdevice *s,
1723                                                                   struct comedi_insn *insn,
1724                                                                   unsigned int *data)
1725 {
1726         struct addi_private *devpriv = dev->private;
1727         int i_ReturnValue = 0;
1728         unsigned int dw_Status;
1729         unsigned int dw_TimeOut = 0;
1730         unsigned char b_ModulNbr;
1731         unsigned char b_TorCounter;
1732         unsigned char b_ReadType;
1733         unsigned int ui_TimeOut;
1734         unsigned char *pb_TorCounterStatus;
1735         unsigned int *pul_TorCounterValue;
1736
1737         i_ReturnValue = insn->n;
1738         b_ModulNbr = CR_AREF(insn->chanspec);
1739         b_ReadType = (unsigned char) data[0];
1740         b_TorCounter = (unsigned char) data[1];
1741         ui_TimeOut = (unsigned int) data[2];
1742         pb_TorCounterStatus = (unsigned char *) &data[0];
1743         pul_TorCounterValue = (unsigned int *) &data[1];
1744
1745         /**************************/
1746         /* Test the module number */
1747         /**************************/
1748
1749         if (b_ReadType == APCI1710_TOR_READINTERRUPT) {
1750
1751                 data[0] = devpriv->s_InterruptParameters.
1752                         s_FIFOInterruptParameters[devpriv->
1753                         s_InterruptParameters.ui_Read].b_OldModuleMask;
1754                 data[1] = devpriv->s_InterruptParameters.
1755                         s_FIFOInterruptParameters[devpriv->
1756                         s_InterruptParameters.ui_Read].ul_OldInterruptMask;
1757                 data[2] = devpriv->s_InterruptParameters.
1758                         s_FIFOInterruptParameters[devpriv->
1759                         s_InterruptParameters.ui_Read].ul_OldCounterLatchValue;
1760
1761                            /**************************/
1762                 /* Increment the read FIFO */
1763                            /***************************/
1764
1765                 devpriv->
1766                         s_InterruptParameters.
1767                         ui_Read = (devpriv->
1768                         s_InterruptParameters.
1769                         ui_Read + 1) % APCI1710_SAVE_INTERRUPT;
1770
1771                 return insn->n;
1772         }
1773
1774         if (b_ModulNbr < 4) {
1775            /***********************/
1776                 /* Test if tor counter */
1777            /***********************/
1778
1779                 if ((devpriv->s_BoardInfos.
1780                                 dw_MolduleConfiguration[b_ModulNbr] &
1781                                 0xFFFF0000UL) == APCI1710_TOR_COUNTER) {
1782               /**********************************/
1783                         /* Test the tor counter selection */
1784               /**********************************/
1785
1786                         if (b_TorCounter <= 1) {
1787                  /***********************************/
1788                                 /* Test if tor counter initialised */
1789                  /***********************************/
1790
1791                                 dw_Status = inl(devpriv->s_BoardInfos.
1792                                         ui_Address + 8 + (16 * b_TorCounter) +
1793                                         (64 * b_ModulNbr));
1794
1795                  /*******************************/
1796                                 /* Test if counter initialised */
1797                  /*******************************/
1798
1799                                 if (dw_Status & 0x10) {
1800                     /***************************/
1801                                         /* Test if counter enabled */
1802                     /***************************/
1803
1804                                         if (dw_Status & 0x1) {
1805
1806                                                 switch (b_ReadType) {
1807
1808                                                 case APCI1710_TOR_GETPROGRESSSTATUS:
1809                        /*******************/
1810                                                         /* Read the status */
1811                        /*******************/
1812
1813                                                         dw_Status =
1814                                                                 inl(devpriv->
1815                                                                 s_BoardInfos.
1816                                                                 ui_Address + 4 +
1817                                                                 (16 * b_TorCounter) + (64 * b_ModulNbr));
1818
1819                                                         dw_Status =
1820                                                                 dw_Status & 0xF;
1821
1822                        /*****************/
1823                                                         /* Test if start */
1824                        /*****************/
1825
1826                                                         if (dw_Status & 1) {
1827                                                                 if (dw_Status &
1828                                                                         2) {
1829                                                                         if (dw_Status & 4) {
1830                                 /************************/
1831                                                                                 /* Tor counter overflow */
1832                                 /************************/
1833
1834                                                                                 *pb_TorCounterStatus
1835                                                                                         =
1836                                                                                         3;
1837                                                                         } else {
1838                                 /***********************/
1839                                                                                 /* Tor counter started */
1840                                 /***********************/
1841
1842                                                                                 *pb_TorCounterStatus
1843                                                                                         =
1844                                                                                         2;
1845                                                                         }
1846                                                                 } else {
1847                              /***********************/
1848                                                                         /* Tor counter started */
1849                              /***********************/
1850
1851                                                                         *pb_TorCounterStatus
1852                                                                                 =
1853                                                                                 1;
1854                                                                 }
1855                                                         } else {
1856                           /***************************/
1857                                                                 /* Tor counter not started */
1858                           /***************************/
1859
1860                                                                 *pb_TorCounterStatus
1861                                                                         = 0;
1862                                                         }
1863                                                         break;
1864
1865                                                 case APCI1710_TOR_GETCOUNTERVALUE:
1866
1867                        /*****************************/
1868                                                         /* Test the timout parameter */
1869                        /*****************************/
1870
1871                                                         if ((ui_TimeOut >= 0)
1872                                                                 && (ui_TimeOut
1873                                                                         <=
1874                                                                         65535UL))
1875                                                         {
1876                                                                 for (;;) {
1877                              /*******************/
1878                                                                         /* Read the status */
1879                              /*******************/
1880
1881                                                                         dw_Status
1882                                                                                 =
1883                                                                                 inl
1884                                                                                 (devpriv->
1885                                                                                 s_BoardInfos.
1886                                                                                 ui_Address
1887                                                                                 +
1888                                                                                 4
1889                                                                                 +
1890                                                                                 (16 * b_TorCounter) + (64 * b_ModulNbr));
1891                              /********************/
1892                                                                         /* Test if overflow */
1893                              /********************/
1894
1895                                                                         if ((dw_Status & 4) == 4) {
1896                                 /******************/
1897                                                                                 /* Overflow occur */
1898                                 /******************/
1899
1900                                                                                 *pb_TorCounterStatus
1901                                                                                         =
1902                                                                                         3;
1903
1904                                 /******************/
1905                                                                                 /* Read the value */
1906                                 /******************/
1907
1908                                                                                 *pul_TorCounterValue
1909                                                                                         =
1910                                                                                         inl
1911                                                                                         (devpriv->
1912                                                                                         s_BoardInfos.
1913                                                                                         ui_Address
1914                                                                                         +
1915                                                                                         0
1916                                                                                         +
1917                                                                                         (16 * b_TorCounter) + (64 * b_ModulNbr));
1918                                                                                 break;
1919                                                                         }       /*  if ((dw_Status & 4) == 4) */
1920                                                                         else {
1921                                 /*******************************/
1922                                                                                 /* Test if measurement stopped */
1923                                 /*******************************/
1924
1925                                                                                 if ((dw_Status & 2) == 2) {
1926                                    /***********************/
1927                                                                                         /* A stop signal occur */
1928                                    /***********************/
1929
1930                                                                                         *pb_TorCounterStatus
1931                                                                                                 =
1932                                                                                                 2;
1933
1934                                    /******************/
1935                                                                                         /* Read the value */
1936                                    /******************/
1937
1938                                                                                         *pul_TorCounterValue
1939                                                                                                 =
1940                                                                                                 inl
1941                                                                                                 (devpriv->
1942                                                                                                 s_BoardInfos.
1943                                                                                                 ui_Address
1944                                                                                                 +
1945                                                                                                 0
1946                                                                                                 +
1947                                                                                                 (16 * b_TorCounter) + (64 * b_ModulNbr));
1948
1949                                                                                         break;
1950                                                                                 }       /*  if ((dw_Status & 2) == 2) */
1951                                                                                 else {
1952                                    /*******************************/
1953                                                                                         /* Test if measurement started */
1954                                    /*******************************/
1955
1956                                                                                         if ((dw_Status & 1) == 1) {
1957                                       /************************/
1958                                                                                                 /* A start signal occur */
1959                                       /************************/
1960
1961                                                                                                 *pb_TorCounterStatus
1962                                                                                                         =
1963                                                                                                         1;
1964                                                                                         }       /*  if ((dw_Status & 1) == 1) */
1965                                                                                         else {
1966                                       /***************************/
1967                                                                                                 /* Measurement not started */
1968                                       /***************************/
1969
1970                                                                                                 *pb_TorCounterStatus
1971                                                                                                         =
1972                                                                                                         0;
1973                                                                                         }       /*  if ((dw_Status & 1) == 1) */
1974                                                                                 }       /*  if ((dw_Status & 2) == 2) */
1975                                                                         }       /*  if ((dw_Status & 8) == 8) */
1976
1977                                                                         if (dw_TimeOut == ui_TimeOut) {
1978                                 /*****************/
1979                                                                                 /* Timeout occur */
1980                                 /*****************/
1981
1982                                                                                 break;
1983                                                                         } else {
1984                                 /*************************/
1985                                                                                 /* Increment the timeout */
1986                                 /*************************/
1987
1988                                                                                 dw_TimeOut
1989                                                                                         =
1990                                                                                         dw_TimeOut
1991                                                                                         +
1992                                                                                         1;
1993
1994                                                                                 mdelay(1000);
1995                                                                         }
1996                                                                 }       /*  for (;;) */
1997
1998                           /*************************/
1999                                                                 /* Test if timeout occur */
2000                           /*************************/
2001
2002                                                                 if ((*pb_TorCounterStatus != 3) && (dw_TimeOut == ui_TimeOut) && (ui_TimeOut != 0)) {
2003                              /*****************/
2004                                                                         /* Timeout occur */
2005                              /*****************/
2006
2007                                                                         *pb_TorCounterStatus
2008                                                                                 =
2009                                                                                 4;
2010                                                                 }
2011                                                         } else {
2012                           /******************************/
2013                                                                 /* Timeout parameter is wrong */
2014                           /******************************/
2015
2016                                                                 DPRINTK("Timeout parameter is wrong\n");
2017                                                                 i_ReturnValue =
2018                                                                         -7;
2019                                                         }
2020                                                         break;
2021
2022                                                 default:
2023                                                         printk("Inputs wrong\n");
2024                                                 }       /*  switch end */
2025                                         }       /*  if (dw_Status & 0x1) */
2026                                         else {
2027                        /***************************/
2028                                                 /* Tor counter not enabled */
2029                        /***************************/
2030
2031                                                 DPRINTK("Tor counter not enabled\n");
2032                                                 i_ReturnValue = -6;
2033                                         }       /*  if (dw_Status & 0x1) */
2034                                 } else {
2035                     /*******************************/
2036                                         /* Tor counter not initialised */
2037                     /*******************************/
2038
2039                                         DPRINTK("Tor counter not initialised\n");
2040                                         i_ReturnValue = -5;
2041                                 }
2042                         }       /*  if (b_TorCounter <= 1) */
2043                         else {
2044                  /**********************************/
2045                                 /* Tor counter selection is wrong */
2046                  /**********************************/
2047
2048                                 DPRINTK("Tor counter selection is wrong\n");
2049                                 i_ReturnValue = -4;
2050                         }       /*  if (b_TorCounter <= 1) */
2051                 } else {
2052               /******************************************/
2053                         /* The module is not a tor counter module */
2054               /******************************************/
2055
2056                         DPRINTK("The module is not a tor counter module\n");
2057                         i_ReturnValue = -3;
2058                 }
2059         } else {
2060            /***********************/
2061                 /* Module number error */
2062            /***********************/
2063
2064                 DPRINTK("Module number error\n");
2065                 i_ReturnValue = -2;
2066         }
2067
2068         return i_ReturnValue;
2069 }