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