]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/das16.c
staging: comedi: das16: tidy up multi-line comments
[karo-tx-linux.git] / drivers / staging / comedi / drivers / das16.c
1 /*
2  * das16.c
3  * DAS16 driver
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7  * Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
8  * Copyright (C) 2001,2002 Frank Mori Hess <fmhess@users.sourceforge.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20
21 /*
22  * Driver: das16
23  * Description: DAS16 compatible boards
24  * Author: Sam Moore, Warren Jasper, ds, Chris Baugher, Frank Hess, Roman Fietze
25  * Devices: (Keithley Metrabyte) DAS-16 [das-16]
26  *          (Keithley Metrabyte) DAS-16G [das-16g]
27  *          (Keithley Metrabyte) DAS-16F [das-16f]
28  *          (Keithley Metrabyte) DAS-1201 [das-1201]
29  *          (Keithley Metrabyte) DAS-1202 [das-1202]
30  *          (Keithley Metrabyte) DAS-1401 [das-1401]
31  *          (Keithley Metrabyte) DAS-1402 [das-1402]
32  *          (Keithley Metrabyte) DAS-1601 [das-1601]
33  *          (Keithley Metrabyte) DAS-1602 [das-1602]
34  *          (ComputerBoards) PC104-DAS16/JR [pc104-das16jr]
35  *          (ComputerBoards) PC104-DAS16JR/16 [pc104-das16jr/16]
36  *          (ComputerBoards) CIO-DAS16 [cio-das16]
37  *          (ComputerBoards) CIO-DAS16F [cio-das16/f]
38  *          (ComputerBoards) CIO-DAS16/JR [cio-das16/jr]
39  *          (ComputerBoards) CIO-DAS16JR/16 [cio-das16jr/16]
40  *          (ComputerBoards) CIO-DAS1401/12 [cio-das1401/12]
41  *          (ComputerBoards) CIO-DAS1402/12 [cio-das1402/12]
42  *          (ComputerBoards) CIO-DAS1402/16 [cio-das1402/16]
43  *          (ComputerBoards) CIO-DAS1601/12 [cio-das1601/12]
44  *          (ComputerBoards) CIO-DAS1602/12 [cio-das1602/12]
45  *          (ComputerBoards) CIO-DAS1602/16 [cio-das1602/16]
46  *          (ComputerBoards) CIO-DAS16/330 [cio-das16/330]
47  * Status: works
48  * Updated: 2003-10-12
49  *
50  * A rewrite of the das16 and das1600 drivers.
51  *
52  * Options:
53  *      [0] - base io address
54  *      [1] - irq (does nothing, irq is not used anymore)
55  *      [2] - dma channel (optional, required for comedi_command support)
56  *      [3] - master clock speed in MHz (optional, 1 or 10, ignored if
57  *              board can probe clock, defaults to 1)
58  *      [4] - analog input range lowest voltage in microvolts (optional,
59  *              only useful if your board does not have software
60  *              programmable gain)
61  *      [5] - analog input range highest voltage in microvolts (optional,
62  *              only useful if board does not have software programmable
63  *              gain)
64  *      [6] - analog output range lowest voltage in microvolts (optional)
65  *      [7] - analog output range highest voltage in microvolts (optional)
66  *
67  * Passing a zero for an option is the same as leaving it unspecified.
68  */
69
70 /*
71  * Testing and debugging help provided by Daniel Koch.
72  *
73  * Keithley Manuals:
74  *      2309.PDF (das16)
75  *      4919.PDF (das1400, 1600)
76  *      4922.PDF (das-1400)
77  *      4923.PDF (das1200, 1400, 1600)
78  *
79  * Computer boards manuals also available from their website
80  * www.measurementcomputing.com
81  */
82
83 #include <linux/module.h>
84 #include <linux/delay.h>
85 #include <linux/pci.h>
86 #include <linux/interrupt.h>
87
88 #include <asm/dma.h>
89
90 #include "../comedidev.h"
91
92 #include "8253.h"
93 #include "8255.h"
94 #include "comedi_fc.h"
95
96 #define DAS16_DMA_SIZE 0xff00   /*  size in bytes of allocated dma buffer */
97
98 /*
99  * Register I/O map
100  */
101 #define DAS16_TRIG_REG                  0x00
102 #define DAS16_AI_LSB_REG                0x00
103 #define DAS16_AI_MSB_REG                0x01
104 #define DAS16_MUX_REG                   0x02
105 #define DAS16_DIO_REG                   0x03
106 #define DAS16_AO_LSB_REG(x)             ((x) ? 0x06 : 0x04)
107 #define DAS16_AO_MSB_REG(x)             ((x) ? 0x07 : 0x05)
108 #define DAS16_STATUS_REG                0x08
109 #define DAS16_STATUS_BUSY               (1 << 7)
110 #define DAS16_STATUS_UNIPOLAR           (1 << 6)
111 #define DAS16_STATUS_MUXBIT             (1 << 5)
112 #define DAS16_STATUS_INT                (1 << 4)
113 #define DAS16_CTRL_REG                  0x09
114 #define DAS16_CTRL_INTE                 (1 << 7)
115 #define DAS16_CTRL_IRQ(x)               (((x) & 0x7) << 4)
116 #define DAS16_CTRL_DMAE                 (1 << 2)
117 #define DAS16_CTRL_PACING_MASK          (3 << 0)
118 #define DAS16_CTRL_INT_PACER            (3 << 0)
119 #define DAS16_CTRL_EXT_PACER            (2 << 0)
120 #define DAS16_CTRL_SOFT_PACER           (0 << 0)
121 #define DAS16_PACER_REG                 0x0a
122 #define DAS16_PACER_BURST_LEN(x)        (((x) & 0xf) << 4)
123 #define DAS16_PACER_CTR0                (1 << 1)
124 #define DAS16_PACER_TRIG0               (1 << 0)
125 #define DAS16_GAIN_REG                  0x0b
126 #define DAS16_TIMER_BASE_REG            0x0c    /* to 0x0f */
127
128 #define DAS1600_CONV_REG                0x404
129 #define DAS1600_CONV_DISABLE            (1 << 6)
130 #define DAS1600_BURST_REG               0x405
131 #define DAS1600_BURST_VAL               (1 << 6)
132 #define DAS1600_ENABLE_REG              0x406
133 #define DAS1600_ENABLE_VAL              (1 << 6)
134 #define DAS1600_STATUS_REG              0x407
135 #define DAS1600_STATUS_BME              (1 << 6)
136 #define DAS1600_STATUS_ME               (1 << 5)
137 #define DAS1600_STATUS_CD               (1 << 4)
138 #define DAS1600_STATUS_WS               (1 << 1)
139 #define DAS1600_STATUS_CLK_10MHZ        (1 << 0)
140
141 static const struct comedi_lrange range_das1x01_bip = {
142         4, {
143                 BIP_RANGE(10),
144                 BIP_RANGE(1),
145                 BIP_RANGE(0.1),
146                 BIP_RANGE(0.01)
147         }
148 };
149
150 static const struct comedi_lrange range_das1x01_unip = {
151         4, {
152                 UNI_RANGE(10),
153                 UNI_RANGE(1),
154                 UNI_RANGE(0.1),
155                 UNI_RANGE(0.01)
156         }
157 };
158
159 static const struct comedi_lrange range_das1x02_bip = {
160         4, {
161                 BIP_RANGE(10),
162                 BIP_RANGE(5),
163                 BIP_RANGE(2.5),
164                 BIP_RANGE(1.25)
165         }
166 };
167
168 static const struct comedi_lrange range_das1x02_unip = {
169         4, {
170                 UNI_RANGE(10),
171                 UNI_RANGE(5),
172                 UNI_RANGE(2.5),
173                 UNI_RANGE(1.25)
174         }
175 };
176
177 static const struct comedi_lrange range_das16jr = {
178         9, {
179                 BIP_RANGE(10),
180                 BIP_RANGE(5),
181                 BIP_RANGE(2.5),
182                 BIP_RANGE(1.25),
183                 BIP_RANGE(0.625),
184                 UNI_RANGE(10),
185                 UNI_RANGE(5),
186                 UNI_RANGE(2.5),
187                 UNI_RANGE(1.25)
188         }
189 };
190
191 static const struct comedi_lrange range_das16jr_16 = {
192         8, {
193                 BIP_RANGE(10),
194                 BIP_RANGE(5),
195                 BIP_RANGE(2.5),
196                 BIP_RANGE(1.25),
197                 UNI_RANGE(10),
198                 UNI_RANGE(5),
199                 UNI_RANGE(2.5),
200                 UNI_RANGE(1.25)
201         }
202 };
203
204 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
205 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
206 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
207
208 enum {
209         das16_pg_none = 0,
210         das16_pg_16jr,
211         das16_pg_16jr_16,
212         das16_pg_1601,
213         das16_pg_1602,
214 };
215 static const int *const das16_gainlists[] = {
216         NULL,
217         das16jr_gainlist,
218         das16jr_16_gainlist,
219         das1600_gainlist,
220         das1600_gainlist,
221 };
222
223 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
224         &range_unknown,
225         &range_das16jr,
226         &range_das16jr_16,
227         &range_das1x01_unip,
228         &range_das1x02_unip,
229 };
230
231 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
232         &range_unknown,
233         &range_das16jr,
234         &range_das16jr_16,
235         &range_das1x01_bip,
236         &range_das1x02_bip,
237 };
238
239 struct das16_board {
240         const char *name;
241         unsigned int ai_maxdata;
242         unsigned int ai_speed;  /*  max conversion speed in nanosec */
243         unsigned int ai_pg;
244         unsigned int has_ao:1;
245         unsigned int has_8255:1;
246
247         unsigned int i8255_offset;
248
249         unsigned int size;
250         unsigned int id;
251 };
252
253 static const struct das16_board das16_boards[] = {
254         {
255                 .name           = "das-16",
256                 .ai_maxdata     = 0x0fff,
257                 .ai_speed       = 15000,
258                 .ai_pg          = das16_pg_none,
259                 .has_ao         = 1,
260                 .has_8255       = 1,
261                 .i8255_offset   = 0x10,
262                 .size           = 0x14,
263                 .id             = 0x00,
264         }, {
265                 .name           = "das-16g",
266                 .ai_maxdata     = 0x0fff,
267                 .ai_speed       = 15000,
268                 .ai_pg          = das16_pg_none,
269                 .has_ao         = 1,
270                 .has_8255       = 1,
271                 .i8255_offset   = 0x10,
272                 .size           = 0x14,
273                 .id             = 0x00,
274         }, {
275                 .name           = "das-16f",
276                 .ai_maxdata     = 0x0fff,
277                 .ai_speed       = 8500,
278                 .ai_pg          = das16_pg_none,
279                 .has_ao         = 1,
280                 .has_8255       = 1,
281                 .i8255_offset   = 0x10,
282                 .size           = 0x14,
283                 .id             = 0x00,
284         }, {
285                 .name           = "cio-das16",
286                 .ai_maxdata     = 0x0fff,
287                 .ai_speed       = 20000,
288                 .ai_pg          = das16_pg_none,
289                 .has_ao         = 1,
290                 .has_8255       = 1,
291                 .i8255_offset   = 0x10,
292                 .size           = 0x14,
293                 .id             = 0x80,
294         }, {
295                 .name           = "cio-das16/f",
296                 .ai_maxdata     = 0x0fff,
297                 .ai_speed       = 10000,
298                 .ai_pg          = das16_pg_none,
299                 .has_ao         = 1,
300                 .has_8255       = 1,
301                 .i8255_offset   = 0x10,
302                 .size           = 0x14,
303                 .id             = 0x80,
304         }, {
305                 .name           = "cio-das16/jr",
306                 .ai_maxdata     = 0x0fff,
307                 .ai_speed       = 7692,
308                 .ai_pg          = das16_pg_16jr,
309                 .size           = 0x10,
310                 .id             = 0x00,
311         }, {
312                 .name           = "pc104-das16jr",
313                 .ai_maxdata     = 0x0fff,
314                 .ai_speed       = 3300,
315                 .ai_pg          = das16_pg_16jr,
316                 .size           = 0x10,
317                 .id             = 0x00,
318         }, {
319                 .name           = "cio-das16jr/16",
320                 .ai_maxdata     = 0xffff,
321                 .ai_speed       = 10000,
322                 .ai_pg          = das16_pg_16jr_16,
323                 .size           = 0x10,
324                 .id             = 0x00,
325         }, {
326                 .name           = "pc104-das16jr/16",
327                 .ai_maxdata     = 0xffff,
328                 .ai_speed       = 10000,
329                 .ai_pg          = das16_pg_16jr_16,
330                 .size           = 0x10,
331                 .id             = 0x00,
332         }, {
333                 .name           = "das-1201",
334                 .ai_maxdata     = 0x0fff,
335                 .ai_speed       = 20000,
336                 .ai_pg          = das16_pg_none,
337                 .has_8255       = 1,
338                 .i8255_offset   = 0x400,
339                 .size           = 0x408,
340                 .id             = 0x20,
341         }, {
342                 .name           = "das-1202",
343                 .ai_maxdata     = 0x0fff,
344                 .ai_speed       = 10000,
345                 .ai_pg          = das16_pg_none,
346                 .has_8255       = 1,
347                 .i8255_offset   = 0x400,
348                 .size           = 0x408,
349                 .id             = 0x20,
350         }, {
351                 .name           = "das-1401",
352                 .ai_maxdata     = 0x0fff,
353                 .ai_speed       = 10000,
354                 .ai_pg          = das16_pg_1601,
355                 .size           = 0x408,
356                 .id             = 0xc0,
357         }, {
358                 .name           = "das-1402",
359                 .ai_maxdata     = 0x0fff,
360                 .ai_speed       = 10000,
361                 .ai_pg          = das16_pg_1602,
362                 .size           = 0x408,
363                 .id             = 0xc0,
364         }, {
365                 .name           = "das-1601",
366                 .ai_maxdata     = 0x0fff,
367                 .ai_speed       = 10000,
368                 .ai_pg          = das16_pg_1601,
369                 .has_ao         = 1,
370                 .has_8255       = 1,
371                 .i8255_offset   = 0x400,
372                 .size           = 0x408,
373                 .id             = 0xc0,
374         }, {
375                 .name           = "das-1602",
376                 .ai_maxdata     = 0x0fff,
377                 .ai_speed       = 10000,
378                 .ai_pg          = das16_pg_1602,
379                 .has_ao         = 1,
380                 .has_8255       = 1,
381                 .i8255_offset   = 0x400,
382                 .size           = 0x408,
383                 .id             = 0xc0,
384         }, {
385                 .name           = "cio-das1401/12",
386                 .ai_maxdata     = 0x0fff,
387                 .ai_speed       = 6250,
388                 .ai_pg          = das16_pg_1601,
389                 .size           = 0x408,
390                 .id             = 0xc0,
391         }, {
392                 .name           = "cio-das1402/12",
393                 .ai_maxdata     = 0x0fff,
394                 .ai_speed       = 6250,
395                 .ai_pg          = das16_pg_1602,
396                 .size           = 0x408,
397                 .id             = 0xc0,
398         }, {
399                 .name           = "cio-das1402/16",
400                 .ai_maxdata     = 0xffff,
401                 .ai_speed       = 10000,
402                 .ai_pg          = das16_pg_1602,
403                 .size           = 0x408,
404                 .id             = 0xc0,
405         }, {
406                 .name           = "cio-das1601/12",
407                 .ai_maxdata     = 0x0fff,
408                 .ai_speed       = 6250,
409                 .ai_pg          = das16_pg_1601,
410                 .has_ao         = 1,
411                 .has_8255       = 1,
412                 .i8255_offset   = 0x400,
413                 .size           = 0x408,
414                 .id             = 0xc0,
415         }, {
416                 .name           = "cio-das1602/12",
417                 .ai_maxdata     = 0x0fff,
418                 .ai_speed       = 10000,
419                 .ai_pg          = das16_pg_1602,
420                 .has_ao         = 1,
421                 .has_8255       = 1,
422                 .i8255_offset   = 0x400,
423                 .size           = 0x408,
424                 .id             = 0xc0,
425         }, {
426                 .name           = "cio-das1602/16",
427                 .ai_maxdata     = 0xffff,
428                 .ai_speed       = 10000,
429                 .ai_pg          = das16_pg_1602,
430                 .has_ao         = 1,
431                 .has_8255       = 1,
432                 .i8255_offset   = 0x400,
433                 .size           = 0x408,
434                 .id             = 0xc0,
435         }, {
436                 .name           = "cio-das16/330",
437                 .ai_maxdata     = 0x0fff,
438                 .ai_speed       = 3030,
439                 .ai_pg          = das16_pg_16jr,
440                 .size           = 0x14,
441                 .id             = 0xf0,
442         },
443 };
444
445 /* Period for timer interrupt in jiffies.  It's a function
446  * to deal with possibility of dynamic HZ patches  */
447 static inline int timer_period(void)
448 {
449         return HZ / 20;
450 }
451
452 struct das16_private_struct {
453         unsigned int            clockbase;
454         unsigned int            ctrl_reg;
455         unsigned long           adc_byte_count;
456         unsigned int            divisor1;
457         unsigned int            divisor2;
458         unsigned int            dma_chan;
459         uint16_t                *dma_buffer[2];
460         dma_addr_t              dma_buffer_addr[2];
461         unsigned int            current_buffer;
462         unsigned int            dma_transfer_size;
463         struct comedi_lrange    *user_ai_range_table;
464         struct comedi_lrange    *user_ao_range_table;
465         struct timer_list       timer;
466         short                   timer_running;
467         unsigned long           extra_iobase;
468         unsigned int            can_burst:1;
469 };
470
471 static void das16_ai_enable(struct comedi_device *dev,
472                             unsigned int mode, unsigned int src)
473 {
474         struct das16_private_struct *devpriv = dev->private;
475
476         devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
477                                DAS16_CTRL_DMAE |
478                                DAS16_CTRL_PACING_MASK);
479         devpriv->ctrl_reg |= mode;
480
481         if (src == TRIG_EXT)
482                 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
483         else
484                 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
485         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
486 }
487
488 static void das16_ai_disable(struct comedi_device *dev)
489 {
490         struct das16_private_struct *devpriv = dev->private;
491
492         /* disable interrupts, dma and pacer clocked conversions */
493         devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
494                                DAS16_CTRL_DMAE |
495                                DAS16_CTRL_PACING_MASK);
496         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
497 }
498
499 /* the pc104-das16jr (at least) has problems if the dma
500         transfer is interrupted in the middle of transferring
501         a 16 bit sample, so this function takes care to get
502         an even transfer count after disabling dma
503         channel.
504 */
505 static int disable_dma_on_even(struct comedi_device *dev)
506 {
507         struct das16_private_struct *devpriv = dev->private;
508         int residue;
509         int i;
510         static const int disable_limit = 100;
511         static const int enable_timeout = 100;
512
513         disable_dma(devpriv->dma_chan);
514         residue = get_dma_residue(devpriv->dma_chan);
515         for (i = 0; i < disable_limit && (residue % 2); ++i) {
516                 int j;
517                 enable_dma(devpriv->dma_chan);
518                 for (j = 0; j < enable_timeout; ++j) {
519                         int new_residue;
520                         udelay(2);
521                         new_residue = get_dma_residue(devpriv->dma_chan);
522                         if (new_residue != residue)
523                                 break;
524                 }
525                 disable_dma(devpriv->dma_chan);
526                 residue = get_dma_residue(devpriv->dma_chan);
527         }
528         if (i == disable_limit) {
529                 dev_err(dev->class_dev,
530                         "failed to get an even dma transfer, could be trouble\n");
531         }
532         return residue;
533 }
534
535 static void das16_interrupt(struct comedi_device *dev)
536 {
537         struct das16_private_struct *devpriv = dev->private;
538         struct comedi_subdevice *s = dev->read_subdev;
539         struct comedi_async *async = s->async;
540         struct comedi_cmd *cmd = &async->cmd;
541         unsigned long spin_flags;
542         unsigned long dma_flags;
543         int num_bytes, residue;
544         int buffer_index;
545
546         spin_lock_irqsave(&dev->spinlock, spin_flags);
547         if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
548                 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
549                 return;
550         }
551
552         dma_flags = claim_dma_lock();
553         clear_dma_ff(devpriv->dma_chan);
554         residue = disable_dma_on_even(dev);
555
556         /*  figure out how many points to read */
557         if (residue > devpriv->dma_transfer_size) {
558                 dev_err(dev->class_dev, "residue > transfer size!\n");
559                 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
560                 num_bytes = 0;
561         } else
562                 num_bytes = devpriv->dma_transfer_size - residue;
563
564         if (cmd->stop_src == TRIG_COUNT &&
565                                         num_bytes >= devpriv->adc_byte_count) {
566                 num_bytes = devpriv->adc_byte_count;
567                 async->events |= COMEDI_CB_EOA;
568         }
569
570         buffer_index = devpriv->current_buffer;
571         devpriv->current_buffer = (devpriv->current_buffer + 1) % 2;
572         devpriv->adc_byte_count -= num_bytes;
573
574         /*  re-enable  dma */
575         if ((async->events & COMEDI_CB_EOA) == 0) {
576                 set_dma_addr(devpriv->dma_chan,
577                              devpriv->dma_buffer_addr[devpriv->current_buffer]);
578                 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
579                 enable_dma(devpriv->dma_chan);
580         }
581         release_dma_lock(dma_flags);
582
583         spin_unlock_irqrestore(&dev->spinlock, spin_flags);
584
585         cfc_write_array_to_buffer(s,
586                                   devpriv->dma_buffer[buffer_index], num_bytes);
587
588         cfc_handle_events(dev, s);
589 }
590
591 static void das16_timer_interrupt(unsigned long arg)
592 {
593         struct comedi_device *dev = (struct comedi_device *)arg;
594         struct das16_private_struct *devpriv = dev->private;
595
596         das16_interrupt(dev);
597
598         if (devpriv->timer_running)
599                 mod_timer(&devpriv->timer, jiffies + timer_period());
600 }
601
602 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
603                           struct comedi_cmd *cmd)
604 {
605         const struct das16_board *board = comedi_board(dev);
606         struct das16_private_struct *devpriv = dev->private;
607         int err = 0, tmp;
608         int gain, start_chan, i;
609         int mask;
610
611         /* Step 1 : check if triggers are trivially valid */
612
613         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
614
615         mask = TRIG_FOLLOW;
616         if (devpriv->can_burst)
617                 mask |= TRIG_TIMER | TRIG_EXT;
618         err |= cfc_check_trigger_src(&cmd->scan_begin_src, mask);
619
620         tmp = cmd->convert_src;
621         mask = TRIG_TIMER | TRIG_EXT;
622         if (devpriv->can_burst)
623                 mask |= TRIG_NOW;
624         err |= cfc_check_trigger_src(&cmd->convert_src, mask);
625
626         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
627         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
628
629         if (err)
630                 return 1;
631
632         /* Step 2a : make sure trigger sources are unique */
633
634         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
635         err |= cfc_check_trigger_is_unique(cmd->convert_src);
636         err |= cfc_check_trigger_is_unique(cmd->stop_src);
637
638         /* Step 2b : and mutually compatible */
639
640         /*  make sure scan_begin_src and convert_src dont conflict */
641         if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
642                 err |= -EINVAL;
643         if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
644                 err |= -EINVAL;
645
646         if (err)
647                 return 2;
648
649         /* Step 3: check if arguments are trivially valid */
650
651         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
652
653         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
654                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
655
656         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
657
658         /* check against maximum frequency */
659         if (cmd->scan_begin_src == TRIG_TIMER)
660                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
661                                         board->ai_speed * cmd->chanlist_len);
662
663         if (cmd->convert_src == TRIG_TIMER)
664                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
665                                                  board->ai_speed);
666
667         if (cmd->stop_src == TRIG_NONE)
668                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
669
670         if (err)
671                 return 3;
672
673         /*  step 4: fix up arguments */
674         if (cmd->scan_begin_src == TRIG_TIMER) {
675                 unsigned int tmp = cmd->scan_begin_arg;
676                 /*  set divisors, correct timing arguments */
677                 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
678                                                &devpriv->divisor1,
679                                                &devpriv->divisor2,
680                                                &cmd->scan_begin_arg,
681                                                cmd->flags & TRIG_ROUND_MASK);
682                 err += (tmp != cmd->scan_begin_arg);
683         }
684         if (cmd->convert_src == TRIG_TIMER) {
685                 unsigned int tmp = cmd->convert_arg;
686                 /*  set divisors, correct timing arguments */
687                 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
688                                                &devpriv->divisor1,
689                                                &devpriv->divisor2,
690                                                &cmd->convert_arg,
691                                                cmd->flags & TRIG_ROUND_MASK);
692                 err += (tmp != cmd->convert_arg);
693         }
694         if (err)
695                 return 4;
696
697         /*  check channel/gain list against card's limitations */
698         if (cmd->chanlist) {
699                 gain = CR_RANGE(cmd->chanlist[0]);
700                 start_chan = CR_CHAN(cmd->chanlist[0]);
701                 for (i = 1; i < cmd->chanlist_len; i++) {
702                         if (CR_CHAN(cmd->chanlist[i]) !=
703                             (start_chan + i) % s->n_chan) {
704                                 dev_err(dev->class_dev,
705                                         "entries in chanlist must be consecutive channels, counting upwards\n");
706                                 err++;
707                         }
708                         if (CR_RANGE(cmd->chanlist[i]) != gain) {
709                                 dev_err(dev->class_dev,
710                                         "entries in chanlist must all have the same gain\n");
711                                 err++;
712                         }
713                 }
714         }
715         if (err)
716                 return 5;
717
718         return 0;
719 }
720
721 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
722                                     int rounding_flags)
723 {
724         struct das16_private_struct *devpriv = dev->private;
725         unsigned long timer_base = dev->iobase + DAS16_TIMER_BASE_REG;
726
727         i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
728                                        &devpriv->divisor1,
729                                        &devpriv->divisor2,
730                                        &ns,
731                                        rounding_flags & TRIG_ROUND_MASK);
732
733         /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
734         i8254_load(timer_base, 0, 1, devpriv->divisor1, 2);
735         i8254_load(timer_base, 0, 2, devpriv->divisor2, 2);
736
737         return ns;
738 }
739
740 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
741 {
742         const struct das16_board *board = comedi_board(dev);
743         struct das16_private_struct *devpriv = dev->private;
744         struct comedi_async *async = s->async;
745         struct comedi_cmd *cmd = &async->cmd;
746         unsigned int byte;
747         unsigned long flags;
748         int range;
749
750         if (cmd->flags & TRIG_RT) {
751                 dev_err(dev->class_dev,
752                          "isa dma transfers cannot be performed with TRIG_RT, aborting\n");
753                 return -1;
754         }
755
756         devpriv->adc_byte_count =
757             cmd->stop_arg * cmd->chanlist_len * sizeof(uint16_t);
758
759         if (devpriv->can_burst)
760                 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
761
762         /*  set scan limits */
763         byte = CR_CHAN(cmd->chanlist[0]);
764         byte |= CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]) << 4;
765         outb(byte, dev->iobase + DAS16_MUX_REG);
766
767         /* set gain (this is also burst rate register but according to
768          * computer boards manual, burst rate does nothing, even on
769          * keithley cards) */
770         if (board->ai_pg != das16_pg_none) {
771                 range = CR_RANGE(cmd->chanlist[0]);
772                 outb((das16_gainlists[board->ai_pg])[range],
773                      dev->iobase + DAS16_GAIN_REG);
774         }
775
776         /* set counter mode and counts */
777         cmd->convert_arg =
778             das16_set_pacer(dev, cmd->convert_arg,
779                             cmd->flags & TRIG_ROUND_MASK);
780
781         /* enable counters */
782         byte = 0;
783         if (devpriv->can_burst) {
784                 if (cmd->convert_src == TRIG_NOW) {
785                         outb(DAS1600_BURST_VAL,
786                              dev->iobase + DAS1600_BURST_REG);
787                         /*  set burst length */
788                         byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
789                 } else {
790                         outb(0, dev->iobase + DAS1600_BURST_REG);
791                 }
792         }
793         outb(byte, dev->iobase + DAS16_PACER_REG);
794
795         /*  set up dma transfer */
796         flags = claim_dma_lock();
797         disable_dma(devpriv->dma_chan);
798         /* clear flip-flop to make sure 2-byte registers for
799          * count and address get set correctly */
800         clear_dma_ff(devpriv->dma_chan);
801         devpriv->current_buffer = 0;
802         set_dma_addr(devpriv->dma_chan,
803                      devpriv->dma_buffer_addr[devpriv->current_buffer]);
804         devpriv->dma_transfer_size = DAS16_DMA_SIZE;
805         set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
806         enable_dma(devpriv->dma_chan);
807         release_dma_lock(flags);
808
809         /*  set up interrupt */
810         devpriv->timer_running = 1;
811         devpriv->timer.expires = jiffies + timer_period();
812         add_timer(&devpriv->timer);
813
814         das16_ai_enable(dev, DAS16_CTRL_DMAE, cmd->convert_src);
815
816         if (devpriv->can_burst)
817                 outb(0, dev->iobase + DAS1600_CONV_REG);
818
819         return 0;
820 }
821
822 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
823 {
824         struct das16_private_struct *devpriv = dev->private;
825         unsigned long flags;
826
827         spin_lock_irqsave(&dev->spinlock, flags);
828
829         das16_ai_disable(dev);
830         disable_dma(devpriv->dma_chan);
831
832         /*  disable SW timer */
833         if (devpriv->timer_running) {
834                 devpriv->timer_running = 0;
835                 del_timer(&devpriv->timer);
836         }
837
838         if (devpriv->can_burst)
839                 outb(0, dev->iobase + DAS1600_BURST_REG);
840
841         spin_unlock_irqrestore(&dev->spinlock, flags);
842
843         return 0;
844 }
845
846 static void das16_ai_munge(struct comedi_device *dev,
847                            struct comedi_subdevice *s, void *array,
848                            unsigned int num_bytes,
849                            unsigned int start_chan_index)
850 {
851         unsigned int i, num_samples = num_bytes / sizeof(short);
852         short *data = array;
853
854         for (i = 0; i < num_samples; i++) {
855                 data[i] = le16_to_cpu(data[i]);
856                 if (s->maxdata == 0x0fff)
857                         data[i] >>= 4;
858                 data[i] &= s->maxdata;
859         }
860 }
861
862 static int das16_ai_wait_for_conv(struct comedi_device *dev,
863                                   unsigned int timeout)
864 {
865         unsigned int status;
866         int i;
867
868         for (i = 0; i < timeout; i++) {
869                 status = inb(dev->iobase + DAS16_STATUS_REG);
870                 if (!(status & DAS16_STATUS_BUSY))
871                         return 0;
872         }
873         return -ETIME;
874 }
875
876 static int das16_ai_insn_read(struct comedi_device *dev,
877                               struct comedi_subdevice *s,
878                               struct comedi_insn *insn,
879                               unsigned int *data)
880 {
881         const struct das16_board *board = comedi_board(dev);
882         unsigned int chan = CR_CHAN(insn->chanspec);
883         unsigned int range = CR_RANGE(insn->chanspec);
884         unsigned int val;
885         int ret;
886         int i;
887
888         das16_ai_disable(dev);
889
890         /* set multiplexer */
891         outb(chan | (chan << 4), dev->iobase + DAS16_MUX_REG);
892
893         /* set gain */
894         if (board->ai_pg != das16_pg_none) {
895                 outb((das16_gainlists[board->ai_pg])[range],
896                      dev->iobase + DAS16_GAIN_REG);
897         }
898
899         for (i = 0; i < insn->n; i++) {
900                 /* trigger conversion */
901                 outb_p(0, dev->iobase + DAS16_TRIG_REG);
902
903                 ret = das16_ai_wait_for_conv(dev, 1000);
904                 if (ret)
905                         return ret;
906
907                 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
908                 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
909                 if (s->maxdata == 0x0fff)
910                         val >>= 4;
911                 val &= s->maxdata;
912
913                 data[i] = val;
914         }
915
916         return insn->n;
917 }
918
919 static int das16_ao_insn_write(struct comedi_device *dev,
920                                struct comedi_subdevice *s,
921                                struct comedi_insn *insn,
922                                unsigned int *data)
923 {
924         unsigned int chan = CR_CHAN(insn->chanspec);
925         unsigned int val;
926         int i;
927
928         for (i = 0; i < insn->n; i++) {
929                 val = data[i];
930                 val <<= 4;
931
932                 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
933                 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
934         }
935
936         return insn->n;
937 }
938
939 static int das16_di_insn_bits(struct comedi_device *dev,
940                               struct comedi_subdevice *s,
941                               struct comedi_insn *insn,
942                               unsigned int *data)
943 {
944         data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
945
946         return insn->n;
947 }
948
949 static int das16_do_insn_bits(struct comedi_device *dev,
950                               struct comedi_subdevice *s,
951                               struct comedi_insn *insn,
952                               unsigned int *data)
953 {
954         unsigned int mask = data[0];
955         unsigned int bits = data[1];
956
957         if (mask) {
958                 s->state &= ~mask;
959                 s->state |= (bits & mask);
960
961                 outb(s->state, dev->iobase + DAS16_DIO_REG);
962         }
963
964         data[1] = s->state;
965
966         return insn->n;
967 }
968
969 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
970 {
971         const struct das16_board *board = comedi_board(dev);
972         int diobits;
973
974         /* diobits indicates boards */
975         diobits = inb(dev->iobase + DAS16_DIO_REG) & 0xf0;
976         if (board->id != diobits) {
977                 dev_err(dev->class_dev,
978                         "requested board's id bits are incorrect (0x%x != 0x%x)\n",
979                         board->id, diobits);
980                 return -EINVAL;
981         }
982
983         return 0;
984 }
985
986 static void das16_reset(struct comedi_device *dev)
987 {
988         outb(0, dev->iobase + DAS16_STATUS_REG);
989         outb(0, dev->iobase + DAS16_CTRL_REG);
990         outb(0, dev->iobase + DAS16_PACER_REG);
991         outb(0, dev->iobase + DAS16_TIMER_BASE_REG + i8254_control_reg);
992 }
993
994 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
995 {
996         const struct das16_board *board = comedi_board(dev);
997         struct das16_private_struct *devpriv;
998         struct comedi_subdevice *s;
999         struct comedi_lrange *lrange;
1000         struct comedi_krange *krange;
1001         unsigned int dma_chan = it->options[2];
1002         unsigned int status;
1003         int ret;
1004
1005         /*  check that clock setting is valid */
1006         if (it->options[3]) {
1007                 if (it->options[3] != 0 &&
1008                     it->options[3] != 1 && it->options[3] != 10) {
1009                         dev_err(dev->class_dev,
1010                                 "Invalid option. Master clock must be set to 1 or 10 (MHz)\n");
1011                         return -EINVAL;
1012                 }
1013         }
1014
1015         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1016         if (!devpriv)
1017                 return -ENOMEM;
1018
1019         if (board->size < 0x400) {
1020                 ret = comedi_request_region(dev, it->options[0], board->size);
1021                 if (ret)
1022                         return ret;
1023         } else {
1024                 ret = comedi_request_region(dev, it->options[0], 0x10);
1025                 if (ret)
1026                         return ret;
1027                 /* Request an additional region for the 8255 */
1028                 ret = __comedi_request_region(dev, dev->iobase + 0x400,
1029                                               board->size & 0x3ff);
1030                 if (ret)
1031                         return ret;
1032                 devpriv->extra_iobase = dev->iobase + 0x400;
1033                 devpriv->can_burst = 1;
1034         }
1035
1036         /*  probe id bits to make sure they are consistent */
1037         if (das16_probe(dev, it))
1038                 return -EINVAL;
1039
1040         /*  get master clock speed */
1041         if (devpriv->can_burst) {
1042                 status = inb(dev->iobase + DAS1600_STATUS_REG);
1043
1044                 if (status & DAS1600_STATUS_CLK_10MHZ)
1045                         devpriv->clockbase = 100;
1046                 else
1047                         devpriv->clockbase = 1000;
1048         } else {
1049                 if (it->options[3])
1050                         devpriv->clockbase = 1000 / it->options[3];
1051                 else
1052                         devpriv->clockbase = 1000;      /*  1 MHz default */
1053         }
1054
1055         /* initialize dma */
1056         if (dma_chan == 1 || dma_chan == 3) {
1057                 unsigned long flags;
1058                 int i;
1059
1060                 if (request_dma(dma_chan, dev->board_name)) {
1061                         dev_err(dev->class_dev,
1062                                 "failed to request dma channel %i\n",
1063                                 dma_chan);
1064                         return -EINVAL;
1065                 }
1066                 devpriv->dma_chan = dma_chan;
1067
1068                 /* allocate dma buffers */
1069                 for (i = 0; i < 2; i++) {
1070                         void *p;
1071
1072                         p = pci_alloc_consistent(NULL, DAS16_DMA_SIZE,
1073                                                  &devpriv->dma_buffer_addr[i]);
1074                         if (!p)
1075                                 return -ENOMEM;
1076                         devpriv->dma_buffer[i] = p;
1077                 }
1078
1079                 flags = claim_dma_lock();
1080                 disable_dma(devpriv->dma_chan);
1081                 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1082                 release_dma_lock(flags);
1083
1084                 init_timer(&devpriv->timer);
1085                 devpriv->timer.function = das16_timer_interrupt;
1086                 devpriv->timer.data = (unsigned long)dev;
1087         }
1088
1089         /* get any user-defined input range */
1090         if (board->ai_pg == das16_pg_none &&
1091             (it->options[4] || it->options[5])) {
1092                 /* allocate single-range range table */
1093                 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1094                 if (!lrange)
1095                         return -ENOMEM;
1096
1097                 /* initialize ai range */
1098                 devpriv->user_ai_range_table = lrange;
1099                 lrange->length = 1;
1100                 krange = devpriv->user_ai_range_table->range;
1101                 krange->min = it->options[4];
1102                 krange->max = it->options[5];
1103                 krange->flags = UNIT_volt;
1104         }
1105
1106         /* get any user-defined output range */
1107         if (it->options[6] || it->options[7]) {
1108                 /* allocate single-range range table */
1109                 lrange = kzalloc(sizeof(*lrange) + sizeof(*krange), GFP_KERNEL);
1110                 if (!lrange)
1111                         return -ENOMEM;
1112
1113                 /* initialize ao range */
1114                 devpriv->user_ao_range_table = lrange;
1115                 lrange->length = 1;
1116                 krange = devpriv->user_ao_range_table->range;
1117                 krange->min = it->options[6];
1118                 krange->max = it->options[7];
1119                 krange->flags = UNIT_volt;
1120         }
1121
1122         ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1123         if (ret)
1124                 return ret;
1125
1126         status = inb(dev->iobase + DAS16_STATUS_REG);
1127
1128         /* Analog Input subdevice */
1129         s = &dev->subdevices[0];
1130         s->type         = COMEDI_SUBD_AI;
1131         s->subdev_flags = SDF_READABLE;
1132         if (status & DAS16_STATUS_MUXBIT) {
1133                 s->subdev_flags |= SDF_GROUND;
1134                 s->n_chan       = 16;
1135         } else {
1136                 s->subdev_flags |= SDF_DIFF;
1137                 s->n_chan       = 8;
1138         }
1139         s->len_chanlist = s->n_chan;
1140         s->maxdata      = board->ai_maxdata;
1141         if (devpriv->user_ai_range_table) { /*  user defined ai range */
1142                 s->range_table  = devpriv->user_ai_range_table;
1143         } else if (status & DAS16_STATUS_UNIPOLAR) {
1144                 s->range_table  = das16_ai_uni_lranges[board->ai_pg];
1145         } else {
1146                 s->range_table  = das16_ai_bip_lranges[board->ai_pg];
1147         }
1148         s->insn_read    = das16_ai_insn_read;
1149         if (devpriv->dma_chan) {
1150                 dev->read_subdev = s;
1151                 s->subdev_flags |= SDF_CMD_READ;
1152                 s->do_cmdtest   = das16_cmd_test;
1153                 s->do_cmd       = das16_cmd_exec;
1154                 s->cancel       = das16_cancel;
1155                 s->munge        = das16_ai_munge;
1156         }
1157
1158         /* Analog Output subdevice */
1159         s = &dev->subdevices[1];
1160         if (board->has_ao) {
1161                 s->type         = COMEDI_SUBD_AO;
1162                 s->subdev_flags = SDF_WRITABLE;
1163                 s->n_chan       = 2;
1164                 s->maxdata      = 0x0fff;
1165                 s->range_table  = devpriv->user_ao_range_table;
1166                 s->insn_write   = das16_ao_insn_write;
1167         } else {
1168                 s->type         = COMEDI_SUBD_UNUSED;
1169         }
1170
1171         /* Digital Input subdevice */
1172         s = &dev->subdevices[2];
1173         s->type         = COMEDI_SUBD_DI;
1174         s->subdev_flags = SDF_READABLE;
1175         s->n_chan       = 4;
1176         s->maxdata      = 1;
1177         s->range_table  = &range_digital;
1178         s->insn_bits    = das16_di_insn_bits;
1179
1180         /* Digital Output subdevice */
1181         s = &dev->subdevices[3];
1182         s->type         = COMEDI_SUBD_DO;
1183         s->subdev_flags = SDF_WRITABLE;
1184         s->n_chan       = 4;
1185         s->maxdata      = 1;
1186         s->range_table  = &range_digital;
1187         s->insn_bits    = das16_do_insn_bits;
1188
1189         /* initialize digital output lines */
1190         outb(s->state, dev->iobase + DAS16_DIO_REG);
1191
1192         /* 8255 Digital I/O subdevice */
1193         if (board->has_8255) {
1194                 s = &dev->subdevices[4];
1195                 ret = subdev_8255_init(dev, s, NULL,
1196                                        dev->iobase + board->i8255_offset);
1197                 if (ret)
1198                         return ret;
1199         }
1200
1201         das16_reset(dev);
1202         /* set the interrupt level */
1203         devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1204         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1205
1206         if (devpriv->can_burst) {
1207                 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1208                 outb(0, dev->iobase + DAS1600_CONV_REG);
1209                 outb(0, dev->iobase + DAS1600_BURST_REG);
1210         }
1211
1212         return 0;
1213 }
1214
1215 static void das16_detach(struct comedi_device *dev)
1216 {
1217         const struct das16_board *board = comedi_board(dev);
1218         struct das16_private_struct *devpriv = dev->private;
1219         int i;
1220
1221         if (devpriv) {
1222                 if (dev->iobase)
1223                         das16_reset(dev);
1224
1225                 for (i = 0; i < 2; i++) {
1226                         if (devpriv->dma_buffer[i])
1227                                 pci_free_consistent(NULL, DAS16_DMA_SIZE,
1228                                                     devpriv->dma_buffer[i],
1229                                                     devpriv->
1230                                                     dma_buffer_addr[i]);
1231                 }
1232                 if (devpriv->dma_chan)
1233                         free_dma(devpriv->dma_chan);
1234                 kfree(devpriv->user_ai_range_table);
1235                 kfree(devpriv->user_ao_range_table);
1236
1237                 if (devpriv->extra_iobase)
1238                         release_region(devpriv->extra_iobase,
1239                                        board->size & 0x3ff);
1240         }
1241
1242         comedi_legacy_detach(dev);
1243 }
1244
1245 static struct comedi_driver das16_driver = {
1246         .driver_name    = "das16",
1247         .module         = THIS_MODULE,
1248         .attach         = das16_attach,
1249         .detach         = das16_detach,
1250         .board_name     = &das16_boards[0].name,
1251         .num_names      = ARRAY_SIZE(das16_boards),
1252         .offset         = sizeof(das16_boards[0]),
1253 };
1254 module_comedi_driver(das16_driver);
1255
1256 MODULE_AUTHOR("Comedi http://www.comedi.org");
1257 MODULE_DESCRIPTION("Comedi driver for DAS16 compatible boards");
1258 MODULE_LICENSE("GPL");