]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/das16.c
staging: comedi: das16: cleanup comedi_error() messages
[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; /*  master clock speed in ns */
447         unsigned int ctrl_reg;
448         unsigned long adc_byte_count;   /*  number of bytes remaining */
449         /*  divisor dividing master clock to get conversion frequency */
450         unsigned int divisor1;
451         /*  divisor dividing master clock to get conversion frequency */
452         unsigned int divisor2;
453         unsigned int dma_chan;  /*  dma channel */
454         uint16_t *dma_buffer[2];
455         dma_addr_t dma_buffer_addr[2];
456         unsigned int current_buffer;
457         unsigned int dma_transfer_size; /*  target number of bytes to transfer per dma shot */
458         /**
459          * user-defined analog input and output ranges
460          * defined from config options
461          */
462         struct comedi_lrange *user_ai_range_table;
463         struct comedi_lrange *user_ao_range_table;
464
465         struct timer_list timer;        /*  for timed interrupt */
466         short timer_running;
467
468         unsigned long extra_iobase;
469         unsigned int can_burst:1;
470 };
471
472 static void das16_ai_enable(struct comedi_device *dev,
473                             unsigned int mode, unsigned int src)
474 {
475         struct das16_private_struct *devpriv = dev->private;
476
477         devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
478                                DAS16_CTRL_DMAE |
479                                DAS16_CTRL_PACING_MASK);
480         devpriv->ctrl_reg |= mode;
481
482         if (src == TRIG_EXT)
483                 devpriv->ctrl_reg |= DAS16_CTRL_EXT_PACER;
484         else
485                 devpriv->ctrl_reg |= DAS16_CTRL_INT_PACER;
486         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
487 }
488
489 static void das16_ai_disable(struct comedi_device *dev)
490 {
491         struct das16_private_struct *devpriv = dev->private;
492
493         /* disable interrupts, dma and pacer clocked conversions */
494         devpriv->ctrl_reg &= ~(DAS16_CTRL_INTE |
495                                DAS16_CTRL_DMAE |
496                                DAS16_CTRL_PACING_MASK);
497         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
498 }
499
500 /* the pc104-das16jr (at least) has problems if the dma
501         transfer is interrupted in the middle of transferring
502         a 16 bit sample, so this function takes care to get
503         an even transfer count after disabling dma
504         channel.
505 */
506 static int disable_dma_on_even(struct comedi_device *dev)
507 {
508         struct das16_private_struct *devpriv = dev->private;
509         int residue;
510         int i;
511         static const int disable_limit = 100;
512         static const int enable_timeout = 100;
513
514         disable_dma(devpriv->dma_chan);
515         residue = get_dma_residue(devpriv->dma_chan);
516         for (i = 0; i < disable_limit && (residue % 2); ++i) {
517                 int j;
518                 enable_dma(devpriv->dma_chan);
519                 for (j = 0; j < enable_timeout; ++j) {
520                         int new_residue;
521                         udelay(2);
522                         new_residue = get_dma_residue(devpriv->dma_chan);
523                         if (new_residue != residue)
524                                 break;
525                 }
526                 disable_dma(devpriv->dma_chan);
527                 residue = get_dma_residue(devpriv->dma_chan);
528         }
529         if (i == disable_limit) {
530                 dev_err(dev->class_dev,
531                         "failed to get an even dma transfer, could be trouble\n");
532         }
533         return residue;
534 }
535
536 static void das16_interrupt(struct comedi_device *dev)
537 {
538         struct das16_private_struct *devpriv = dev->private;
539         struct comedi_subdevice *s = dev->read_subdev;
540         struct comedi_async *async = s->async;
541         struct comedi_cmd *cmd = &async->cmd;
542         unsigned long spin_flags;
543         unsigned long dma_flags;
544         int num_bytes, residue;
545         int buffer_index;
546
547         spin_lock_irqsave(&dev->spinlock, spin_flags);
548         if (!(devpriv->ctrl_reg & DAS16_CTRL_DMAE)) {
549                 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
550                 return;
551         }
552
553         dma_flags = claim_dma_lock();
554         clear_dma_ff(devpriv->dma_chan);
555         residue = disable_dma_on_even(dev);
556
557         /*  figure out how many points to read */
558         if (residue > devpriv->dma_transfer_size) {
559                 dev_err(dev->class_dev, "residue > transfer size!\n");
560                 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
561                 num_bytes = 0;
562         } else
563                 num_bytes = devpriv->dma_transfer_size - residue;
564
565         if (cmd->stop_src == TRIG_COUNT &&
566                                         num_bytes >= devpriv->adc_byte_count) {
567                 num_bytes = devpriv->adc_byte_count;
568                 async->events |= COMEDI_CB_EOA;
569         }
570
571         buffer_index = devpriv->current_buffer;
572         devpriv->current_buffer = (devpriv->current_buffer + 1) % 2;
573         devpriv->adc_byte_count -= num_bytes;
574
575         /*  re-enable  dma */
576         if ((async->events & COMEDI_CB_EOA) == 0) {
577                 set_dma_addr(devpriv->dma_chan,
578                              devpriv->dma_buffer_addr[devpriv->current_buffer]);
579                 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
580                 enable_dma(devpriv->dma_chan);
581         }
582         release_dma_lock(dma_flags);
583
584         spin_unlock_irqrestore(&dev->spinlock, spin_flags);
585
586         cfc_write_array_to_buffer(s,
587                                   devpriv->dma_buffer[buffer_index], num_bytes);
588
589         cfc_handle_events(dev, s);
590 }
591
592 static void das16_timer_interrupt(unsigned long arg)
593 {
594         struct comedi_device *dev = (struct comedi_device *)arg;
595         struct das16_private_struct *devpriv = dev->private;
596
597         das16_interrupt(dev);
598
599         if (devpriv->timer_running)
600                 mod_timer(&devpriv->timer, jiffies + timer_period());
601 }
602
603 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
604                           struct comedi_cmd *cmd)
605 {
606         const struct das16_board *board = comedi_board(dev);
607         struct das16_private_struct *devpriv = dev->private;
608         int err = 0, tmp;
609         int gain, start_chan, i;
610         int mask;
611
612         /* Step 1 : check if triggers are trivially valid */
613
614         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
615
616         mask = TRIG_FOLLOW;
617         if (devpriv->can_burst)
618                 mask |= TRIG_TIMER | TRIG_EXT;
619         err |= cfc_check_trigger_src(&cmd->scan_begin_src, mask);
620
621         tmp = cmd->convert_src;
622         mask = TRIG_TIMER | TRIG_EXT;
623         if (devpriv->can_burst)
624                 mask |= TRIG_NOW;
625         err |= cfc_check_trigger_src(&cmd->convert_src, mask);
626
627         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
628         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
629
630         if (err)
631                 return 1;
632
633         /* Step 2a : make sure trigger sources are unique */
634
635         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
636         err |= cfc_check_trigger_is_unique(cmd->convert_src);
637         err |= cfc_check_trigger_is_unique(cmd->stop_src);
638
639         /* Step 2b : and mutually compatible */
640
641         /*  make sure scan_begin_src and convert_src dont conflict */
642         if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
643                 err |= -EINVAL;
644         if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
645                 err |= -EINVAL;
646
647         if (err)
648                 return 2;
649
650         /* Step 3: check if arguments are trivially valid */
651
652         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
653
654         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
655                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
656
657         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
658
659         /* check against maximum frequency */
660         if (cmd->scan_begin_src == TRIG_TIMER)
661                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
662                                         board->ai_speed * cmd->chanlist_len);
663
664         if (cmd->convert_src == TRIG_TIMER)
665                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
666                                                  board->ai_speed);
667
668         if (cmd->stop_src == TRIG_NONE)
669                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
670
671         if (err)
672                 return 3;
673
674         /*  step 4: fix up arguments */
675         if (cmd->scan_begin_src == TRIG_TIMER) {
676                 unsigned int tmp = cmd->scan_begin_arg;
677                 /*  set divisors, correct timing arguments */
678                 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
679                                                &(devpriv->divisor1),
680                                                &(devpriv->divisor2),
681                                                &(cmd->scan_begin_arg),
682                                                cmd->flags & TRIG_ROUND_MASK);
683                 err += (tmp != cmd->scan_begin_arg);
684         }
685         if (cmd->convert_src == TRIG_TIMER) {
686                 unsigned int tmp = cmd->convert_arg;
687                 /*  set divisors, correct timing arguments */
688                 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
689                                                &(devpriv->divisor1),
690                                                &(devpriv->divisor2),
691                                                &(cmd->convert_arg),
692                                                cmd->flags & TRIG_ROUND_MASK);
693                 err += (tmp != cmd->convert_arg);
694         }
695         if (err)
696                 return 4;
697
698         /*  check channel/gain list against card's limitations */
699         if (cmd->chanlist) {
700                 gain = CR_RANGE(cmd->chanlist[0]);
701                 start_chan = CR_CHAN(cmd->chanlist[0]);
702                 for (i = 1; i < cmd->chanlist_len; i++) {
703                         if (CR_CHAN(cmd->chanlist[i]) !=
704                             (start_chan + i) % s->n_chan) {
705                                 dev_err(dev->class_dev,
706                                         "entries in chanlist must be consecutive channels, counting upwards\n");
707                                 err++;
708                         }
709                         if (CR_RANGE(cmd->chanlist[i]) != gain) {
710                                 dev_err(dev->class_dev,
711                                         "entries in chanlist must all have the same gain\n");
712                                 err++;
713                         }
714                 }
715         }
716         if (err)
717                 return 5;
718
719         return 0;
720 }
721
722 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
723                                     int rounding_flags)
724 {
725         struct das16_private_struct *devpriv = dev->private;
726         unsigned long timer_base = dev->iobase + DAS16_TIMER_BASE_REG;
727
728         i8253_cascade_ns_to_timer_2div(devpriv->clockbase, &(devpriv->divisor1),
729                                        &(devpriv->divisor2), &ns,
730                                        rounding_flags & TRIG_ROUND_MASK);
731
732         /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
733         i8254_load(timer_base, 0, 1, devpriv->divisor1, 2);
734         i8254_load(timer_base, 0, 2, devpriv->divisor2, 2);
735
736         return ns;
737 }
738
739 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
740 {
741         const struct das16_board *board = comedi_board(dev);
742         struct das16_private_struct *devpriv = dev->private;
743         struct comedi_async *async = s->async;
744         struct comedi_cmd *cmd = &async->cmd;
745         unsigned int byte;
746         unsigned long flags;
747         int range;
748
749         if (cmd->flags & TRIG_RT) {
750                 dev_err(dev->class_dev,
751                          "isa dma transfers cannot be performed with TRIG_RT, aborting\n");
752                 return -1;
753         }
754
755         devpriv->adc_byte_count =
756             cmd->stop_arg * cmd->chanlist_len * sizeof(uint16_t);
757
758         if (devpriv->can_burst)
759                 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV_REG);
760
761         /*  set scan limits */
762         byte = CR_CHAN(cmd->chanlist[0]);
763         byte |= CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]) << 4;
764         outb(byte, dev->iobase + DAS16_MUX_REG);
765
766         /* set gain (this is also burst rate register but according to
767          * computer boards manual, burst rate does nothing, even on
768          * keithley cards) */
769         if (board->ai_pg != das16_pg_none) {
770                 range = CR_RANGE(cmd->chanlist[0]);
771                 outb((das16_gainlists[board->ai_pg])[range],
772                      dev->iobase + DAS16_GAIN_REG);
773         }
774
775         /* set counter mode and counts */
776         cmd->convert_arg =
777             das16_set_pacer(dev, cmd->convert_arg,
778                             cmd->flags & TRIG_ROUND_MASK);
779
780         /* enable counters */
781         byte = 0;
782         if (devpriv->can_burst) {
783                 if (cmd->convert_src == TRIG_NOW) {
784                         outb(DAS1600_BURST_VAL,
785                              dev->iobase + DAS1600_BURST_REG);
786                         /*  set burst length */
787                         byte |= DAS16_PACER_BURST_LEN(cmd->chanlist_len - 1);
788                 } else {
789                         outb(0, dev->iobase + DAS1600_BURST_REG);
790                 }
791         }
792         outb(byte, dev->iobase + DAS16_PACER_REG);
793
794         /*  set up dma transfer */
795         flags = claim_dma_lock();
796         disable_dma(devpriv->dma_chan);
797         /* clear flip-flop to make sure 2-byte registers for
798          * count and address get set correctly */
799         clear_dma_ff(devpriv->dma_chan);
800         devpriv->current_buffer = 0;
801         set_dma_addr(devpriv->dma_chan,
802                      devpriv->dma_buffer_addr[devpriv->current_buffer]);
803         devpriv->dma_transfer_size = DAS16_DMA_SIZE;
804         set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
805         enable_dma(devpriv->dma_chan);
806         release_dma_lock(flags);
807
808         /*  set up interrupt */
809         devpriv->timer_running = 1;
810         devpriv->timer.expires = jiffies + timer_period();
811         add_timer(&devpriv->timer);
812
813         das16_ai_enable(dev, DAS16_CTRL_DMAE, cmd->convert_src);
814
815         if (devpriv->can_burst)
816                 outb(0, dev->iobase + DAS1600_CONV_REG);
817
818         return 0;
819 }
820
821 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
822 {
823         struct das16_private_struct *devpriv = dev->private;
824         unsigned long flags;
825
826         spin_lock_irqsave(&dev->spinlock, flags);
827
828         das16_ai_disable(dev);
829         disable_dma(devpriv->dma_chan);
830
831         /*  disable SW timer */
832         if (devpriv->timer_running) {
833                 devpriv->timer_running = 0;
834                 del_timer(&devpriv->timer);
835         }
836
837         if (devpriv->can_burst)
838                 outb(0, dev->iobase + DAS1600_BURST_REG);
839
840         spin_unlock_irqrestore(&dev->spinlock, flags);
841
842         return 0;
843 }
844
845 static void das16_ai_munge(struct comedi_device *dev,
846                            struct comedi_subdevice *s, void *array,
847                            unsigned int num_bytes,
848                            unsigned int start_chan_index)
849 {
850         unsigned int i, num_samples = num_bytes / sizeof(short);
851         short *data = array;
852
853         for (i = 0; i < num_samples; i++) {
854                 data[i] = le16_to_cpu(data[i]);
855                 if (s->maxdata == 0x0fff)
856                         data[i] >>= 4;
857                 data[i] &= s->maxdata;
858         }
859 }
860
861 static int das16_ai_wait_for_conv(struct comedi_device *dev,
862                                   unsigned int timeout)
863 {
864         unsigned int status;
865         int i;
866
867         for (i = 0; i < timeout; i++) {
868                 status = inb(dev->iobase + DAS16_STATUS_REG);
869                 if (!(status & DAS16_STATUS_BUSY))
870                         return 0;
871         }
872         return -ETIME;
873 }
874
875 static int das16_ai_insn_read(struct comedi_device *dev,
876                               struct comedi_subdevice *s,
877                               struct comedi_insn *insn,
878                               unsigned int *data)
879 {
880         const struct das16_board *board = comedi_board(dev);
881         unsigned int chan = CR_CHAN(insn->chanspec);
882         unsigned int range = CR_RANGE(insn->chanspec);
883         unsigned int val;
884         int ret;
885         int i;
886
887         das16_ai_disable(dev);
888
889         /* set multiplexer */
890         outb(chan | (chan << 4), dev->iobase + DAS16_MUX_REG);
891
892         /* set gain */
893         if (board->ai_pg != das16_pg_none) {
894                 outb((das16_gainlists[board->ai_pg])[range],
895                      dev->iobase + DAS16_GAIN_REG);
896         }
897
898         for (i = 0; i < insn->n; i++) {
899                 /* trigger conversion */
900                 outb_p(0, dev->iobase + DAS16_TRIG_REG);
901
902                 ret = das16_ai_wait_for_conv(dev, 1000);
903                 if (ret)
904                         return ret;
905
906                 val = inb(dev->iobase + DAS16_AI_MSB_REG) << 8;
907                 val |= inb(dev->iobase + DAS16_AI_LSB_REG);
908                 if (s->maxdata == 0x0fff)
909                         val >>= 4;
910                 val &= s->maxdata;
911
912                 data[i] = val;
913         }
914
915         return insn->n;
916 }
917
918 static int das16_ao_insn_write(struct comedi_device *dev,
919                                struct comedi_subdevice *s,
920                                struct comedi_insn *insn,
921                                unsigned int *data)
922 {
923         unsigned int chan = CR_CHAN(insn->chanspec);
924         unsigned int val;
925         int i;
926
927         for (i = 0; i < insn->n; i++) {
928                 val = data[i];
929                 val <<= 4;
930
931                 outb(val & 0xff, dev->iobase + DAS16_AO_LSB_REG(chan));
932                 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB_REG(chan));
933         }
934
935         return insn->n;
936 }
937
938 static int das16_di_insn_bits(struct comedi_device *dev,
939                               struct comedi_subdevice *s,
940                               struct comedi_insn *insn,
941                               unsigned int *data)
942 {
943         data[1] = inb(dev->iobase + DAS16_DIO_REG) & 0xf;
944
945         return insn->n;
946 }
947
948 static int das16_do_insn_bits(struct comedi_device *dev,
949                               struct comedi_subdevice *s,
950                               struct comedi_insn *insn,
951                               unsigned int *data)
952 {
953         unsigned int mask = data[0];
954         unsigned int bits = data[1];
955
956         if (mask) {
957                 s->state &= ~mask;
958                 s->state |= (bits & mask);
959
960                 outb(s->state, dev->iobase + DAS16_DIO_REG);
961         }
962
963         data[1] = s->state;
964
965         return insn->n;
966 }
967
968 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
969 {
970         const struct das16_board *board = comedi_board(dev);
971         int diobits;
972
973         /* diobits indicates boards */
974         diobits = inb(dev->iobase + DAS16_DIO_REG) & 0xf0;
975         if (board->id != diobits) {
976                 dev_err(dev->class_dev,
977                         "requested board's id bits are incorrect (0x%x != 0x%x)\n",
978                         board->id, diobits);
979                 return -EINVAL;
980         }
981
982         return 0;
983 }
984
985 static void das16_reset(struct comedi_device *dev)
986 {
987         outb(0, dev->iobase + DAS16_STATUS_REG);
988         outb(0, dev->iobase + DAS16_CTRL_REG);
989         outb(0, dev->iobase + DAS16_PACER_REG);
990         outb(0, dev->iobase + DAS16_TIMER_BASE_REG + i8254_control_reg);
991 }
992
993 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
994 {
995         const struct das16_board *board = comedi_board(dev);
996         struct das16_private_struct *devpriv;
997         struct comedi_subdevice *s;
998         struct comedi_krange *user_ai_range;
999         struct comedi_krange *user_ao_range;
1000         unsigned int dma_chan = it->options[2];
1001         unsigned int status;
1002         int ret;
1003
1004         /*  check that clock setting is valid */
1005         if (it->options[3]) {
1006                 if (it->options[3] != 0 &&
1007                     it->options[3] != 1 && it->options[3] != 10) {
1008                         dev_err(dev->class_dev,
1009                                 "Invalid option. Master clock must be set to 1 or 10 (MHz)\n");
1010                         return -EINVAL;
1011                 }
1012         }
1013
1014         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1015         if (!devpriv)
1016                 return -ENOMEM;
1017
1018         if (board->size < 0x400) {
1019                 ret = comedi_request_region(dev, it->options[0], board->size);
1020                 if (ret)
1021                         return ret;
1022         } else {
1023                 ret = comedi_request_region(dev, it->options[0], 0x10);
1024                 if (ret)
1025                         return ret;
1026                 /* Request an additional region for the 8255 */
1027                 ret = __comedi_request_region(dev, dev->iobase + 0x400,
1028                                               board->size & 0x3ff);
1029                 if (ret)
1030                         return ret;
1031                 devpriv->extra_iobase = dev->iobase + 0x400;
1032                 devpriv->can_burst = 1;
1033         }
1034
1035         /*  probe id bits to make sure they are consistent */
1036         if (das16_probe(dev, it))
1037                 return -EINVAL;
1038
1039         /*  get master clock speed */
1040         if (devpriv->can_burst) {
1041                 status = inb(dev->iobase + DAS1600_STATUS_REG);
1042
1043                 if (status & DAS1600_STATUS_CLK_10MHZ)
1044                         devpriv->clockbase = 100;
1045                 else
1046                         devpriv->clockbase = 1000;
1047         } else {
1048                 if (it->options[3])
1049                         devpriv->clockbase = 1000 / it->options[3];
1050                 else
1051                         devpriv->clockbase = 1000;      /*  1 MHz default */
1052         }
1053
1054         /* initialize dma */
1055         if (dma_chan == 1 || dma_chan == 3) {
1056                 unsigned long flags;
1057                 int i;
1058
1059                 if (request_dma(dma_chan, dev->board_name)) {
1060                         dev_err(dev->class_dev,
1061                                 "failed to request dma channel %i\n",
1062                                 dma_chan);
1063                         return -EINVAL;
1064                 }
1065                 devpriv->dma_chan = dma_chan;
1066
1067                 /* allocate dma buffers */
1068                 for (i = 0; i < 2; i++) {
1069                         void *p;
1070
1071                         p = pci_alloc_consistent(NULL, DAS16_DMA_SIZE,
1072                                                  &devpriv->dma_buffer_addr[i]);
1073                         if (!p)
1074                                 return -ENOMEM;
1075                         devpriv->dma_buffer[i] = p;
1076                 }
1077
1078                 flags = claim_dma_lock();
1079                 disable_dma(devpriv->dma_chan);
1080                 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1081                 release_dma_lock(flags);
1082
1083                 init_timer(&(devpriv->timer));
1084                 devpriv->timer.function = das16_timer_interrupt;
1085                 devpriv->timer.data = (unsigned long)dev;
1086         }
1087
1088         /*  get any user-defined input range */
1089         if (board->ai_pg == das16_pg_none &&
1090             (it->options[4] || it->options[5])) {
1091                 /*  allocate single-range range table */
1092                 devpriv->user_ai_range_table =
1093                     kmalloc(sizeof(struct comedi_lrange) +
1094                             sizeof(struct comedi_krange), GFP_KERNEL);
1095                 /*  initialize ai range */
1096                 devpriv->user_ai_range_table->length = 1;
1097                 user_ai_range = devpriv->user_ai_range_table->range;
1098                 user_ai_range->min = it->options[4];
1099                 user_ai_range->max = it->options[5];
1100                 user_ai_range->flags = UNIT_volt;
1101         }
1102         /*  get any user-defined output range */
1103         if (it->options[6] || it->options[7]) {
1104                 /*  allocate single-range range table */
1105                 devpriv->user_ao_range_table =
1106                     kmalloc(sizeof(struct comedi_lrange) +
1107                             sizeof(struct comedi_krange), GFP_KERNEL);
1108                 /*  initialize ao range */
1109                 devpriv->user_ao_range_table->length = 1;
1110                 user_ao_range = devpriv->user_ao_range_table->range;
1111                 user_ao_range->min = it->options[6];
1112                 user_ao_range->max = it->options[7];
1113                 user_ao_range->flags = UNIT_volt;
1114         }
1115
1116         ret = comedi_alloc_subdevices(dev, 4 + board->has_8255);
1117         if (ret)
1118                 return ret;
1119
1120         status = inb(dev->iobase + DAS16_STATUS_REG);
1121
1122         /* Analog Input subdevice */
1123         s = &dev->subdevices[0];
1124         s->type         = COMEDI_SUBD_AI;
1125         s->subdev_flags = SDF_READABLE;
1126         if (status & DAS16_STATUS_MUXBIT) {
1127                 s->subdev_flags |= SDF_GROUND;
1128                 s->n_chan       = 16;
1129         } else {
1130                 s->subdev_flags |= SDF_DIFF;
1131                 s->n_chan       = 8;
1132         }
1133         s->len_chanlist = s->n_chan;
1134         s->maxdata      = board->ai_maxdata;
1135         if (devpriv->user_ai_range_table) { /*  user defined ai range */
1136                 s->range_table  = devpriv->user_ai_range_table;
1137         } else if (status & DAS16_STATUS_UNIPOLAR) {
1138                 s->range_table  = das16_ai_uni_lranges[board->ai_pg];
1139         } else {
1140                 s->range_table  = das16_ai_bip_lranges[board->ai_pg];
1141         }
1142         s->insn_read    = das16_ai_insn_read;
1143         if (devpriv->dma_chan) {
1144                 dev->read_subdev = s;
1145                 s->subdev_flags |= SDF_CMD_READ;
1146                 s->do_cmdtest   = das16_cmd_test;
1147                 s->do_cmd       = das16_cmd_exec;
1148                 s->cancel       = das16_cancel;
1149                 s->munge        = das16_ai_munge;
1150         }
1151
1152         /* Analog Output subdevice */
1153         s = &dev->subdevices[1];
1154         if (board->has_ao) {
1155                 s->type         = COMEDI_SUBD_AO;
1156                 s->subdev_flags = SDF_WRITABLE;
1157                 s->n_chan       = 2;
1158                 s->maxdata      = 0x0fff;
1159                 if (devpriv->user_ao_range_table)
1160                         s->range_table  = devpriv->user_ao_range_table;
1161                 else
1162                         s->range_table  = &range_unknown;
1163
1164                 s->insn_write   = das16_ao_insn_write;
1165         } else {
1166                 s->type         = COMEDI_SUBD_UNUSED;
1167         }
1168
1169         /* Digital Input subdevice */
1170         s = &dev->subdevices[2];
1171         s->type         = COMEDI_SUBD_DI;
1172         s->subdev_flags = SDF_READABLE;
1173         s->n_chan       = 4;
1174         s->maxdata      = 1;
1175         s->range_table  = &range_digital;
1176         s->insn_bits    = das16_di_insn_bits;
1177
1178         /* Digital Output subdevice */
1179         s = &dev->subdevices[3];
1180         s->type         = COMEDI_SUBD_DO;
1181         s->subdev_flags = SDF_WRITABLE;
1182         s->n_chan       = 4;
1183         s->maxdata      = 1;
1184         s->range_table  = &range_digital;
1185         s->insn_bits    = das16_do_insn_bits;
1186
1187         /* initialize digital output lines */
1188         outb(s->state, dev->iobase + DAS16_DIO_REG);
1189
1190         /* 8255 Digital I/O subdevice */
1191         if (board->has_8255) {
1192                 s = &dev->subdevices[4];
1193                 ret = subdev_8255_init(dev, s, NULL,
1194                                        dev->iobase + board->i8255_offset);
1195                 if (ret)
1196                         return ret;
1197         }
1198
1199         das16_reset(dev);
1200         /* set the interrupt level */
1201         devpriv->ctrl_reg = DAS16_CTRL_IRQ(dev->irq);
1202         outb(devpriv->ctrl_reg, dev->iobase + DAS16_CTRL_REG);
1203
1204         if (devpriv->can_burst) {
1205                 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE_REG);
1206                 outb(0, dev->iobase + DAS1600_CONV_REG);
1207                 outb(0, dev->iobase + DAS1600_BURST_REG);
1208         }
1209
1210         return 0;
1211 }
1212
1213 static void das16_detach(struct comedi_device *dev)
1214 {
1215         const struct das16_board *board = comedi_board(dev);
1216         struct das16_private_struct *devpriv = dev->private;
1217
1218         das16_reset(dev);
1219         if (devpriv) {
1220                 int i;
1221                 for (i = 0; i < 2; i++) {
1222                         if (devpriv->dma_buffer[i])
1223                                 pci_free_consistent(NULL, DAS16_DMA_SIZE,
1224                                                     devpriv->dma_buffer[i],
1225                                                     devpriv->
1226                                                     dma_buffer_addr[i]);
1227                 }
1228                 if (devpriv->dma_chan)
1229                         free_dma(devpriv->dma_chan);
1230                 kfree(devpriv->user_ai_range_table);
1231                 kfree(devpriv->user_ao_range_table);
1232         }
1233         if (devpriv->extra_iobase)
1234                 release_region(devpriv->extra_iobase, board->size & 0x3ff);
1235         comedi_legacy_detach(dev);
1236 }
1237
1238 static struct comedi_driver das16_driver = {
1239         .driver_name    = "das16",
1240         .module         = THIS_MODULE,
1241         .attach         = das16_attach,
1242         .detach         = das16_detach,
1243         .board_name     = &das16_boards[0].name,
1244         .num_names      = ARRAY_SIZE(das16_boards),
1245         .offset         = sizeof(das16_boards[0]),
1246 };
1247 module_comedi_driver(das16_driver);
1248
1249 MODULE_AUTHOR("Comedi http://www.comedi.org");
1250 MODULE_DESCRIPTION("Comedi low-level driver");
1251 MODULE_LICENSE("GPL");