]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/comedi/drivers/das1800.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mv-sheeva.git] / drivers / staging / comedi / drivers / das1800.c
1 /*
2     comedi/drivers/das1800.c
3     Driver for Keitley das1700/das1800 series boards
4     Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
5
6     COMEDI - Linux Control and Measurement Device Interface
7     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ************************************************************************
24 */
25 /*
26 Driver: das1800
27 Description: Keithley Metrabyte DAS1800 (& compatibles)
28 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
29 Devices: [Keithley Metrabyte] DAS-1701ST (das-1701st),
30   DAS-1701ST-DA (das-1701st-da), DAS-1701/AO (das-1701ao),
31   DAS-1702ST (das-1702st), DAS-1702ST-DA (das-1702st-da),
32   DAS-1702HR (das-1702hr), DAS-1702HR-DA (das-1702hr-da),
33   DAS-1702/AO (das-1702ao), DAS-1801ST (das-1801st),
34   DAS-1801ST-DA (das-1801st-da), DAS-1801HC (das-1801hc),
35   DAS-1801AO (das-1801ao), DAS-1802ST (das-1802st),
36   DAS-1802ST-DA (das-1802st-da), DAS-1802HR (das-1802hr),
37   DAS-1802HR-DA (das-1802hr-da), DAS-1802HC (das-1802hc),
38   DAS-1802AO (das-1802ao)
39 Status: works
40
41 The waveform analog output on the 'ao' cards is not supported.
42 If you need it, send me (Frank Hess) an email.
43
44 Configuration options:
45   [0] - I/O port base address
46   [1] - IRQ (optional, required for timed or externally triggered conversions)
47   [2] - DMA0 (optional, requires irq)
48   [3] - DMA1 (optional, requires irq and dma0)
49 */
50 /*
51
52 This driver supports the following Keithley boards:
53
54 das-1701st
55 das-1701st-da
56 das-1701ao
57 das-1702st
58 das-1702st-da
59 das-1702hr
60 das-1702hr-da
61 das-1702ao
62 das-1801st
63 das-1801st-da
64 das-1801hc
65 das-1801ao
66 das-1802st
67 das-1802st-da
68 das-1802hr
69 das-1802hr-da
70 das-1802hc
71 das-1802ao
72
73 Options:
74         [0] - base io address
75         [1] - irq (optional, required for timed or externally triggered conversions)
76         [2] - dma0 (optional, requires irq)
77         [3] - dma1 (optional, requires irq and dma0)
78
79 irq can be omitted, although the cmd interface will not work without it.
80
81 analog input cmd triggers supported:
82         start_src:      TRIG_NOW | TRIG_EXT
83         scan_begin_src: TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT
84         scan_end_src:   TRIG_COUNT
85         convert_src:    TRIG_TIMER | TRIG_EXT (TRIG_EXT requires scan_begin_src == TRIG_FOLLOW)
86         stop_src:       TRIG_COUNT | TRIG_EXT | TRIG_NONE
87
88 scan_begin_src triggers TRIG_TIMER and TRIG_EXT use the card's
89 'burst mode' which limits the valid conversion time to 64 microseconds
90 (convert_arg <= 64000).  This limitation does not apply if scan_begin_src
91 is TRIG_FOLLOW.
92
93 NOTES:
94 Only the DAS-1801ST has been tested by me.
95 Unipolar and bipolar ranges cannot be mixed in the channel/gain list.
96
97 TODO:
98         Make it automatically allocate irq and dma channels if they are not specified
99         Add support for analog out on 'ao' cards
100         read insn for analog out
101 */
102
103 #include <linux/interrupt.h>
104 #include <linux/slab.h>
105 #include "../comedidev.h"
106
107 #include <linux/ioport.h>
108 #include <asm/dma.h>
109
110 #include "8253.h"
111 #include "comedi_fc.h"
112
113 /* misc. defines */
114 #define DAS1800_SIZE           16       /* uses 16 io addresses */
115 #define FIFO_SIZE              1024     /*  1024 sample fifo */
116 #define TIMER_BASE             200      /*  5 Mhz master clock */
117 #define UNIPOLAR               0x4      /*  bit that determines whether input range is uni/bipolar */
118 #define DMA_BUF_SIZE           0x1ff00  /*  size in bytes of dma buffers */
119
120 /* Registers for the das1800 */
121 #define DAS1800_FIFO            0x0
122 #define DAS1800_QRAM            0x0
123 #define DAS1800_DAC             0x0
124 #define DAS1800_SELECT          0x2
125 #define   ADC                     0x0
126 #define   QRAM                    0x1
127 #define   DAC(a)                  (0x2 + a)
128 #define DAS1800_DIGITAL         0x3
129 #define DAS1800_CONTROL_A       0x4
130 #define   FFEN                    0x1
131 #define   CGEN                    0x4
132 #define   CGSL                    0x8
133 #define   TGEN                    0x10
134 #define   TGSL                    0x20
135 #define   ATEN                    0x80
136 #define DAS1800_CONTROL_B       0x5
137 #define   DMA_CH5                 0x1
138 #define   DMA_CH6                 0x2
139 #define   DMA_CH7                 0x3
140 #define   DMA_CH5_CH6             0x5
141 #define   DMA_CH6_CH7             0x6
142 #define   DMA_CH7_CH5             0x7
143 #define   DMA_ENABLED             0x3   /* mask used to determine if dma is enabled */
144 #define   DMA_DUAL                0x4
145 #define   IRQ3                    0x8
146 #define   IRQ5                    0x10
147 #define   IRQ7                    0x18
148 #define   IRQ10                   0x28
149 #define   IRQ11                   0x30
150 #define   IRQ15                   0x38
151 #define   FIMD                    0x40
152 #define DAS1800_CONTROL_C       0X6
153 #define   IPCLK                   0x1
154 #define   XPCLK                   0x3
155 #define   BMDE                    0x4
156 #define   CMEN                    0x8
157 #define   UQEN                    0x10
158 #define   SD                      0x40
159 #define   UB                      0x80
160 #define DAS1800_STATUS          0x7
161 /* bits that prevent interrupt status bits (and CVEN) from being cleared on write */
162 #define   CLEAR_INTR_MASK         (CVEN_MASK | 0x1f)
163 #define   INT                     0x1
164 #define   DMATC                   0x2
165 #define   CT0TC                   0x8
166 #define   OVF                     0x10
167 #define   FHF                     0x20
168 #define   FNE                     0x40
169 #define   CVEN_MASK               0x40  /*  masks CVEN on write */
170 #define   CVEN                    0x80
171 #define DAS1800_BURST_LENGTH    0x8
172 #define DAS1800_BURST_RATE      0x9
173 #define DAS1800_QRAM_ADDRESS    0xa
174 #define DAS1800_COUNTER         0xc
175
176 #define IOBASE2                   0x400 /* offset of additional ioports used on 'ao' cards */
177
178 enum {
179         das1701st, das1701st_da, das1702st, das1702st_da, das1702hr,
180         das1702hr_da,
181         das1701ao, das1702ao, das1801st, das1801st_da, das1802st, das1802st_da,
182         das1802hr, das1802hr_da, das1801hc, das1802hc, das1801ao, das1802ao
183 };
184
185 static int das1800_attach(struct comedi_device *dev,
186                           struct comedi_devconfig *it);
187 static int das1800_detach(struct comedi_device *dev);
188 static int das1800_probe(struct comedi_device *dev);
189 static int das1800_cancel(struct comedi_device *dev,
190                           struct comedi_subdevice *s);
191 static irqreturn_t das1800_interrupt(int irq, void *d);
192 static int das1800_ai_poll(struct comedi_device *dev,
193                            struct comedi_subdevice *s);
194 static void das1800_ai_handler(struct comedi_device *dev);
195 static void das1800_handle_dma(struct comedi_device *dev,
196                                struct comedi_subdevice *s, unsigned int status);
197 static void das1800_flush_dma(struct comedi_device *dev,
198                               struct comedi_subdevice *s);
199 static void das1800_flush_dma_channel(struct comedi_device *dev,
200                                       struct comedi_subdevice *s,
201                                       unsigned int channel, uint16_t * buffer);
202 static void das1800_handle_fifo_half_full(struct comedi_device *dev,
203                                           struct comedi_subdevice *s);
204 static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
205                                           struct comedi_subdevice *s);
206 static int das1800_ai_do_cmdtest(struct comedi_device *dev,
207                                  struct comedi_subdevice *s,
208                                  struct comedi_cmd *cmd);
209 static int das1800_ai_do_cmd(struct comedi_device *dev,
210                              struct comedi_subdevice *s);
211 static int das1800_ai_rinsn(struct comedi_device *dev,
212                             struct comedi_subdevice *s,
213                             struct comedi_insn *insn, unsigned int *data);
214 static int das1800_ao_winsn(struct comedi_device *dev,
215                             struct comedi_subdevice *s,
216                             struct comedi_insn *insn, unsigned int *data);
217 static int das1800_di_rbits(struct comedi_device *dev,
218                             struct comedi_subdevice *s,
219                             struct comedi_insn *insn, unsigned int *data);
220 static int das1800_do_wbits(struct comedi_device *dev,
221                             struct comedi_subdevice *s,
222                             struct comedi_insn *insn, unsigned int *data);
223
224 static int das1800_set_frequency(struct comedi_device *dev);
225 static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode);
226 static unsigned int suggest_transfer_size(struct comedi_cmd *cmd);
227
228 /* analog input ranges */
229 static const struct comedi_lrange range_ai_das1801 = {
230         8,
231         {
232          RANGE(-5, 5),
233          RANGE(-1, 1),
234          RANGE(-0.1, 0.1),
235          RANGE(-0.02, 0.02),
236          RANGE(0, 5),
237          RANGE(0, 1),
238          RANGE(0, 0.1),
239          RANGE(0, 0.02),
240          }
241 };
242
243 static const struct comedi_lrange range_ai_das1802 = {
244         8,
245         {
246          RANGE(-10, 10),
247          RANGE(-5, 5),
248          RANGE(-2.5, 2.5),
249          RANGE(-1.25, 1.25),
250          RANGE(0, 10),
251          RANGE(0, 5),
252          RANGE(0, 2.5),
253          RANGE(0, 1.25),
254          }
255 };
256
257 struct das1800_board {
258         const char *name;
259         int ai_speed;           /* max conversion period in nanoseconds */
260         int resolution;         /* bits of ai resolution */
261         int qram_len;           /* length of card's channel / gain queue */
262         int common;             /* supports AREF_COMMON flag */
263         int do_n_chan;          /* number of digital output channels */
264         int ao_ability;         /* 0 == no analog out, 1 == basic analog out, 2 == waveform analog out */
265         int ao_n_chan;          /* number of analog out channels */
266         const struct comedi_lrange *range_ai;   /* available input ranges */
267 };
268
269 /* Warning: the maximum conversion speeds listed below are
270  * not always achievable depending on board setup (see
271  * user manual.)
272  */
273 static const struct das1800_board das1800_boards[] = {
274         {
275          .name = "das-1701st",
276          .ai_speed = 6250,
277          .resolution = 12,
278          .qram_len = 256,
279          .common = 1,
280          .do_n_chan = 4,
281          .ao_ability = 0,
282          .ao_n_chan = 0,
283          .range_ai = &range_ai_das1801,
284          },
285         {
286          .name = "das-1701st-da",
287          .ai_speed = 6250,
288          .resolution = 12,
289          .qram_len = 256,
290          .common = 1,
291          .do_n_chan = 4,
292          .ao_ability = 1,
293          .ao_n_chan = 4,
294          .range_ai = &range_ai_das1801,
295          },
296         {
297          .name = "das-1702st",
298          .ai_speed = 6250,
299          .resolution = 12,
300          .qram_len = 256,
301          .common = 1,
302          .do_n_chan = 4,
303          .ao_ability = 0,
304          .ao_n_chan = 0,
305          .range_ai = &range_ai_das1802,
306          },
307         {
308          .name = "das-1702st-da",
309          .ai_speed = 6250,
310          .resolution = 12,
311          .qram_len = 256,
312          .common = 1,
313          .do_n_chan = 4,
314          .ao_ability = 1,
315          .ao_n_chan = 4,
316          .range_ai = &range_ai_das1802,
317          },
318         {
319          .name = "das-1702hr",
320          .ai_speed = 20000,
321          .resolution = 16,
322          .qram_len = 256,
323          .common = 1,
324          .do_n_chan = 4,
325          .ao_ability = 0,
326          .ao_n_chan = 0,
327          .range_ai = &range_ai_das1802,
328          },
329         {
330          .name = "das-1702hr-da",
331          .ai_speed = 20000,
332          .resolution = 16,
333          .qram_len = 256,
334          .common = 1,
335          .do_n_chan = 4,
336          .ao_ability = 1,
337          .ao_n_chan = 2,
338          .range_ai = &range_ai_das1802,
339          },
340         {
341          .name = "das-1701ao",
342          .ai_speed = 6250,
343          .resolution = 12,
344          .qram_len = 256,
345          .common = 1,
346          .do_n_chan = 4,
347          .ao_ability = 2,
348          .ao_n_chan = 2,
349          .range_ai = &range_ai_das1801,
350          },
351         {
352          .name = "das-1702ao",
353          .ai_speed = 6250,
354          .resolution = 12,
355          .qram_len = 256,
356          .common = 1,
357          .do_n_chan = 4,
358          .ao_ability = 2,
359          .ao_n_chan = 2,
360          .range_ai = &range_ai_das1802,
361          },
362         {
363          .name = "das-1801st",
364          .ai_speed = 3000,
365          .resolution = 12,
366          .qram_len = 256,
367          .common = 1,
368          .do_n_chan = 4,
369          .ao_ability = 0,
370          .ao_n_chan = 0,
371          .range_ai = &range_ai_das1801,
372          },
373         {
374          .name = "das-1801st-da",
375          .ai_speed = 3000,
376          .resolution = 12,
377          .qram_len = 256,
378          .common = 1,
379          .do_n_chan = 4,
380          .ao_ability = 0,
381          .ao_n_chan = 4,
382          .range_ai = &range_ai_das1801,
383          },
384         {
385          .name = "das-1802st",
386          .ai_speed = 3000,
387          .resolution = 12,
388          .qram_len = 256,
389          .common = 1,
390          .do_n_chan = 4,
391          .ao_ability = 0,
392          .ao_n_chan = 0,
393          .range_ai = &range_ai_das1802,
394          },
395         {
396          .name = "das-1802st-da",
397          .ai_speed = 3000,
398          .resolution = 12,
399          .qram_len = 256,
400          .common = 1,
401          .do_n_chan = 4,
402          .ao_ability = 1,
403          .ao_n_chan = 4,
404          .range_ai = &range_ai_das1802,
405          },
406         {
407          .name = "das-1802hr",
408          .ai_speed = 10000,
409          .resolution = 16,
410          .qram_len = 256,
411          .common = 1,
412          .do_n_chan = 4,
413          .ao_ability = 0,
414          .ao_n_chan = 0,
415          .range_ai = &range_ai_das1802,
416          },
417         {
418          .name = "das-1802hr-da",
419          .ai_speed = 10000,
420          .resolution = 16,
421          .qram_len = 256,
422          .common = 1,
423          .do_n_chan = 4,
424          .ao_ability = 1,
425          .ao_n_chan = 2,
426          .range_ai = &range_ai_das1802,
427          },
428         {
429          .name = "das-1801hc",
430          .ai_speed = 3000,
431          .resolution = 12,
432          .qram_len = 64,
433          .common = 0,
434          .do_n_chan = 8,
435          .ao_ability = 1,
436          .ao_n_chan = 2,
437          .range_ai = &range_ai_das1801,
438          },
439         {
440          .name = "das-1802hc",
441          .ai_speed = 3000,
442          .resolution = 12,
443          .qram_len = 64,
444          .common = 0,
445          .do_n_chan = 8,
446          .ao_ability = 1,
447          .ao_n_chan = 2,
448          .range_ai = &range_ai_das1802,
449          },
450         {
451          .name = "das-1801ao",
452          .ai_speed = 3000,
453          .resolution = 12,
454          .qram_len = 256,
455          .common = 1,
456          .do_n_chan = 4,
457          .ao_ability = 2,
458          .ao_n_chan = 2,
459          .range_ai = &range_ai_das1801,
460          },
461         {
462          .name = "das-1802ao",
463          .ai_speed = 3000,
464          .resolution = 12,
465          .qram_len = 256,
466          .common = 1,
467          .do_n_chan = 4,
468          .ao_ability = 2,
469          .ao_n_chan = 2,
470          .range_ai = &range_ai_das1802,
471          },
472 };
473
474 /*
475  * Useful for shorthand access to the particular board structure
476  */
477 #define thisboard ((const struct das1800_board *)dev->board_ptr)
478
479 struct das1800_private {
480         volatile unsigned int count;    /* number of data points left to be taken */
481         unsigned int divisor1;  /* value to load into board's counter 1 for timed conversions */
482         unsigned int divisor2;  /* value to load into board's counter 2 for timed conversions */
483         int do_bits;            /* digital output bits */
484         int irq_dma_bits;       /* bits for control register b */
485         /* dma bits for control register b, stored so that dma can be
486          * turned on and off */
487         int dma_bits;
488         unsigned int dma0;      /* dma channels used */
489         unsigned int dma1;
490         volatile unsigned int dma_current;      /* dma channel currently in use */
491         uint16_t *ai_buf0;      /* pointers to dma buffers */
492         uint16_t *ai_buf1;
493         uint16_t *dma_current_buf;      /* pointer to dma buffer currently being used */
494         unsigned int dma_transfer_size; /* size of transfer currently used, in bytes */
495         unsigned long iobase2;  /* secondary io address used for analog out on 'ao' boards */
496         short ao_update_bits;   /* remembers the last write to the 'update' dac */
497 };
498
499 #define devpriv ((struct das1800_private *)dev->private)
500
501 /* analog out range for boards with basic analog out */
502 static const struct comedi_lrange range_ao_1 = {
503         1,
504         {
505          RANGE(-10, 10),
506          }
507 };
508
509 /* analog out range for 'ao' boards */
510 /*
511 static const struct comedi_lrange range_ao_2 = {
512         2,
513         {
514                 RANGE(-10, 10),
515                 RANGE(-5, 5),
516         }
517 };
518 */
519
520 static struct comedi_driver driver_das1800 = {
521         .driver_name = "das1800",
522         .module = THIS_MODULE,
523         .attach = das1800_attach,
524         .detach = das1800_detach,
525         .num_names = ARRAY_SIZE(das1800_boards),
526         .board_name = &das1800_boards[0].name,
527         .offset = sizeof(struct das1800_board),
528 };
529
530 /*
531  * A convenient macro that defines init_module() and cleanup_module(),
532  * as necessary.
533  */
534 COMEDI_INITCLEANUP(driver_das1800);
535
536 static int das1800_init_dma(struct comedi_device *dev, unsigned int dma0,
537                             unsigned int dma1)
538 {
539         unsigned long flags;
540
541         /*  need an irq to do dma */
542         if (dev->irq && dma0) {
543                 /* encode dma0 and dma1 into 2 digit hexadecimal for switch */
544                 switch ((dma0 & 0x7) | (dma1 << 4)) {
545                 case 0x5:       /*  dma0 == 5 */
546                         devpriv->dma_bits |= DMA_CH5;
547                         break;
548                 case 0x6:       /*  dma0 == 6 */
549                         devpriv->dma_bits |= DMA_CH6;
550                         break;
551                 case 0x7:       /*  dma0 == 7 */
552                         devpriv->dma_bits |= DMA_CH7;
553                         break;
554                 case 0x65:      /*  dma0 == 5, dma1 == 6 */
555                         devpriv->dma_bits |= DMA_CH5_CH6;
556                         break;
557                 case 0x76:      /*  dma0 == 6, dma1 == 7 */
558                         devpriv->dma_bits |= DMA_CH6_CH7;
559                         break;
560                 case 0x57:      /*  dma0 == 7, dma1 == 5 */
561                         devpriv->dma_bits |= DMA_CH7_CH5;
562                         break;
563                 default:
564                         printk(" only supports dma channels 5 through 7\n"
565                                " Dual dma only allows the following combinations:\n"
566                                " dma 5,6 / 6,7 / or 7,5\n");
567                         return -EINVAL;
568                         break;
569                 }
570                 if (request_dma(dma0, driver_das1800.driver_name)) {
571                         printk(" failed to allocate dma channel %i\n", dma0);
572                         return -EINVAL;
573                 }
574                 devpriv->dma0 = dma0;
575                 devpriv->dma_current = dma0;
576                 if (dma1) {
577                         if (request_dma(dma1, driver_das1800.driver_name)) {
578                                 printk(" failed to allocate dma channel %i\n",
579                                        dma1);
580                                 return -EINVAL;
581                         }
582                         devpriv->dma1 = dma1;
583                 }
584                 devpriv->ai_buf0 = kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
585                 if (devpriv->ai_buf0 == NULL)
586                         return -ENOMEM;
587                 devpriv->dma_current_buf = devpriv->ai_buf0;
588                 if (dma1) {
589                         devpriv->ai_buf1 =
590                             kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
591                         if (devpriv->ai_buf1 == NULL)
592                                 return -ENOMEM;
593                 }
594                 flags = claim_dma_lock();
595                 disable_dma(devpriv->dma0);
596                 set_dma_mode(devpriv->dma0, DMA_MODE_READ);
597                 if (dma1) {
598                         disable_dma(devpriv->dma1);
599                         set_dma_mode(devpriv->dma1, DMA_MODE_READ);
600                 }
601                 release_dma_lock(flags);
602         }
603         return 0;
604 }
605
606 static int das1800_attach(struct comedi_device *dev,
607                           struct comedi_devconfig *it)
608 {
609         struct comedi_subdevice *s;
610         unsigned long iobase = it->options[0];
611         unsigned int irq = it->options[1];
612         unsigned int dma0 = it->options[2];
613         unsigned int dma1 = it->options[3];
614         unsigned long iobase2;
615         int board;
616         int retval;
617
618         /* allocate and initialize dev->private */
619         if (alloc_private(dev, sizeof(struct das1800_private)) < 0)
620                 return -ENOMEM;
621
622         printk("comedi%d: %s: io 0x%lx", dev->minor, driver_das1800.driver_name,
623                iobase);
624         if (irq) {
625                 printk(", irq %u", irq);
626                 if (dma0) {
627                         printk(", dma %u", dma0);
628                         if (dma1)
629                                 printk(" and %u", dma1);
630                 }
631         }
632         printk("\n");
633
634         if (iobase == 0) {
635                 printk(" io base address required\n");
636                 return -EINVAL;
637         }
638
639         /* check if io addresses are available */
640         if (!request_region(iobase, DAS1800_SIZE, driver_das1800.driver_name)) {
641                 printk
642                     (" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
643                      iobase, iobase + DAS1800_SIZE - 1);
644                 return -EIO;
645         }
646         dev->iobase = iobase;
647
648         board = das1800_probe(dev);
649         if (board < 0) {
650                 printk(" unable to determine board type\n");
651                 return -ENODEV;
652         }
653
654         dev->board_ptr = das1800_boards + board;
655         dev->board_name = thisboard->name;
656
657         /*  if it is an 'ao' board with fancy analog out then we need extra io ports */
658         if (thisboard->ao_ability == 2) {
659                 iobase2 = iobase + IOBASE2;
660                 if (!request_region(iobase2, DAS1800_SIZE,
661                                     driver_das1800.driver_name)) {
662                         printk
663                             (" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
664                              iobase2, iobase2 + DAS1800_SIZE - 1);
665                         return -EIO;
666                 }
667                 devpriv->iobase2 = iobase2;
668         }
669
670         /* grab our IRQ */
671         if (irq) {
672                 if (request_irq(irq, das1800_interrupt, 0,
673                                 driver_das1800.driver_name, dev)) {
674                         printk(" unable to allocate irq %u\n", irq);
675                         return -EINVAL;
676                 }
677         }
678         dev->irq = irq;
679
680         /*  set bits that tell card which irq to use */
681         switch (irq) {
682         case 0:
683                 break;
684         case 3:
685                 devpriv->irq_dma_bits |= 0x8;
686                 break;
687         case 5:
688                 devpriv->irq_dma_bits |= 0x10;
689                 break;
690         case 7:
691                 devpriv->irq_dma_bits |= 0x18;
692                 break;
693         case 10:
694                 devpriv->irq_dma_bits |= 0x28;
695                 break;
696         case 11:
697                 devpriv->irq_dma_bits |= 0x30;
698                 break;
699         case 15:
700                 devpriv->irq_dma_bits |= 0x38;
701                 break;
702         default:
703                 printk(" irq out of range\n");
704                 return -EINVAL;
705                 break;
706         }
707
708         retval = das1800_init_dma(dev, dma0, dma1);
709         if (retval < 0)
710                 return retval;
711
712         if (devpriv->ai_buf0 == NULL) {
713                 devpriv->ai_buf0 =
714                     kmalloc(FIFO_SIZE * sizeof(uint16_t), GFP_KERNEL);
715                 if (devpriv->ai_buf0 == NULL)
716                         return -ENOMEM;
717         }
718
719         if (alloc_subdevices(dev, 4) < 0)
720                 return -ENOMEM;
721
722         /* analog input subdevice */
723         s = dev->subdevices + 0;
724         dev->read_subdev = s;
725         s->type = COMEDI_SUBD_AI;
726         s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_GROUND | SDF_CMD_READ;
727         if (thisboard->common)
728                 s->subdev_flags |= SDF_COMMON;
729         s->n_chan = thisboard->qram_len;
730         s->len_chanlist = thisboard->qram_len;
731         s->maxdata = (1 << thisboard->resolution) - 1;
732         s->range_table = thisboard->range_ai;
733         s->do_cmd = das1800_ai_do_cmd;
734         s->do_cmdtest = das1800_ai_do_cmdtest;
735         s->insn_read = das1800_ai_rinsn;
736         s->poll = das1800_ai_poll;
737         s->cancel = das1800_cancel;
738
739         /* analog out */
740         s = dev->subdevices + 1;
741         if (thisboard->ao_ability == 1) {
742                 s->type = COMEDI_SUBD_AO;
743                 s->subdev_flags = SDF_WRITABLE;
744                 s->n_chan = thisboard->ao_n_chan;
745                 s->maxdata = (1 << thisboard->resolution) - 1;
746                 s->range_table = &range_ao_1;
747                 s->insn_write = das1800_ao_winsn;
748         } else {
749                 s->type = COMEDI_SUBD_UNUSED;
750         }
751
752         /* di */
753         s = dev->subdevices + 2;
754         s->type = COMEDI_SUBD_DI;
755         s->subdev_flags = SDF_READABLE;
756         s->n_chan = 4;
757         s->maxdata = 1;
758         s->range_table = &range_digital;
759         s->insn_bits = das1800_di_rbits;
760
761         /* do */
762         s = dev->subdevices + 3;
763         s->type = COMEDI_SUBD_DO;
764         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
765         s->n_chan = thisboard->do_n_chan;
766         s->maxdata = 1;
767         s->range_table = &range_digital;
768         s->insn_bits = das1800_do_wbits;
769
770         das1800_cancel(dev, dev->read_subdev);
771
772         /*  initialize digital out channels */
773         outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL);
774
775         /*  initialize analog out channels */
776         if (thisboard->ao_ability == 1) {
777                 /*  select 'update' dac channel for baseAddress + 0x0 */
778                 outb(DAC(thisboard->ao_n_chan - 1),
779                      dev->iobase + DAS1800_SELECT);
780                 outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC);
781         }
782
783         return 0;
784 };
785
786 static int das1800_detach(struct comedi_device *dev)
787 {
788         /* only free stuff if it has been allocated by _attach */
789         if (dev->iobase)
790                 release_region(dev->iobase, DAS1800_SIZE);
791         if (dev->irq)
792                 free_irq(dev->irq, dev);
793         if (dev->private) {
794                 if (devpriv->iobase2)
795                         release_region(devpriv->iobase2, DAS1800_SIZE);
796                 if (devpriv->dma0)
797                         free_dma(devpriv->dma0);
798                 if (devpriv->dma1)
799                         free_dma(devpriv->dma1);
800                 if (devpriv->ai_buf0)
801                         kfree(devpriv->ai_buf0);
802                 if (devpriv->ai_buf1)
803                         kfree(devpriv->ai_buf1);
804         }
805
806         printk("comedi%d: %s: remove\n", dev->minor,
807                driver_das1800.driver_name);
808
809         return 0;
810 };
811
812 /* probes and checks das-1800 series board type
813  */
814 static int das1800_probe(struct comedi_device *dev)
815 {
816         int id;
817         int board;
818
819         id = (inb(dev->iobase + DAS1800_DIGITAL) >> 4) & 0xf;   /* get id bits */
820         board = ((struct das1800_board *)dev->board_ptr) - das1800_boards;
821
822         switch (id) {
823         case 0x3:
824                 if (board == das1801st_da || board == das1802st_da ||
825                     board == das1701st_da || board == das1702st_da) {
826                         printk(" Board model: %s\n",
827                                das1800_boards[board].name);
828                         return board;
829                 }
830                 printk
831                     (" Board model (probed, not recommended): das-1800st-da series\n");
832                 return das1801st;
833                 break;
834         case 0x4:
835                 if (board == das1802hr_da || board == das1702hr_da) {
836                         printk(" Board model: %s\n",
837                                das1800_boards[board].name);
838                         return board;
839                 }
840                 printk
841                     (" Board model (probed, not recommended): das-1802hr-da\n");
842                 return das1802hr;
843                 break;
844         case 0x5:
845                 if (board == das1801ao || board == das1802ao ||
846                     board == das1701ao || board == das1702ao) {
847                         printk(" Board model: %s\n",
848                                das1800_boards[board].name);
849                         return board;
850                 }
851                 printk
852                     (" Board model (probed, not recommended): das-1800ao series\n");
853                 return das1801ao;
854                 break;
855         case 0x6:
856                 if (board == das1802hr || board == das1702hr) {
857                         printk(" Board model: %s\n",
858                                das1800_boards[board].name);
859                         return board;
860                 }
861                 printk(" Board model (probed, not recommended): das-1802hr\n");
862                 return das1802hr;
863                 break;
864         case 0x7:
865                 if (board == das1801st || board == das1802st ||
866                     board == das1701st || board == das1702st) {
867                         printk(" Board model: %s\n",
868                                das1800_boards[board].name);
869                         return board;
870                 }
871                 printk
872                     (" Board model (probed, not recommended): das-1800st series\n");
873                 return das1801st;
874                 break;
875         case 0x8:
876                 if (board == das1801hc || board == das1802hc) {
877                         printk(" Board model: %s\n",
878                                das1800_boards[board].name);
879                         return board;
880                 }
881                 printk
882                     (" Board model (probed, not recommended): das-1800hc series\n");
883                 return das1801hc;
884                 break;
885         default:
886                 printk
887                     (" Board model: probe returned 0x%x (unknown, please report)\n",
888                      id);
889                 return board;
890                 break;
891         }
892         return -1;
893 }
894
895 static int das1800_ai_poll(struct comedi_device *dev,
896                            struct comedi_subdevice *s)
897 {
898         unsigned long flags;
899
900         /*  prevent race with interrupt handler */
901         spin_lock_irqsave(&dev->spinlock, flags);
902         das1800_ai_handler(dev);
903         spin_unlock_irqrestore(&dev->spinlock, flags);
904
905         return s->async->buf_write_count - s->async->buf_read_count;
906 }
907
908 static irqreturn_t das1800_interrupt(int irq, void *d)
909 {
910         struct comedi_device *dev = d;
911         unsigned int status;
912
913         if (dev->attached == 0) {
914                 comedi_error(dev, "premature interrupt");
915                 return IRQ_HANDLED;
916         }
917
918         /* Prevent race with das1800_ai_poll() on multi processor systems.
919          * Also protects indirect addressing in das1800_ai_handler */
920         spin_lock(&dev->spinlock);
921         status = inb(dev->iobase + DAS1800_STATUS);
922
923         /* if interrupt was not caused by das-1800 */
924         if (!(status & INT)) {
925                 spin_unlock(&dev->spinlock);
926                 return IRQ_NONE;
927         }
928         /* clear the interrupt status bit INT */
929         outb(CLEAR_INTR_MASK & ~INT, dev->iobase + DAS1800_STATUS);
930         /*  handle interrupt */
931         das1800_ai_handler(dev);
932
933         spin_unlock(&dev->spinlock);
934         return IRQ_HANDLED;
935 }
936
937 /* the guts of the interrupt handler, that is shared with das1800_ai_poll */
938 static void das1800_ai_handler(struct comedi_device *dev)
939 {
940         struct comedi_subdevice *s = dev->subdevices + 0;       /* analog input subdevice */
941         struct comedi_async *async = s->async;
942         struct comedi_cmd *cmd = &async->cmd;
943         unsigned int status = inb(dev->iobase + DAS1800_STATUS);
944
945         async->events = 0;
946         /*  select adc for base address + 0 */
947         outb(ADC, dev->iobase + DAS1800_SELECT);
948         /*  dma buffer full */
949         if (devpriv->irq_dma_bits & DMA_ENABLED) {
950                 /*  look for data from dma transfer even if dma terminal count hasn't happened yet */
951                 das1800_handle_dma(dev, s, status);
952         } else if (status & FHF) {      /*  if fifo half full */
953                 das1800_handle_fifo_half_full(dev, s);
954         } else if (status & FNE) {      /*  if fifo not empty */
955                 das1800_handle_fifo_not_empty(dev, s);
956         }
957
958         async->events |= COMEDI_CB_BLOCK;
959         /* if the card's fifo has overflowed */
960         if (status & OVF) {
961                 /*  clear OVF interrupt bit */
962                 outb(CLEAR_INTR_MASK & ~OVF, dev->iobase + DAS1800_STATUS);
963                 comedi_error(dev, "DAS1800 FIFO overflow");
964                 das1800_cancel(dev, s);
965                 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
966                 comedi_event(dev, s);
967                 return;
968         }
969         /*  stop taking data if appropriate */
970         /* stop_src TRIG_EXT */
971         if (status & CT0TC) {
972                 /*  clear CT0TC interrupt bit */
973                 outb(CLEAR_INTR_MASK & ~CT0TC, dev->iobase + DAS1800_STATUS);
974                 /*  make sure we get all remaining data from board before quitting */
975                 if (devpriv->irq_dma_bits & DMA_ENABLED)
976                         das1800_flush_dma(dev, s);
977                 else
978                         das1800_handle_fifo_not_empty(dev, s);
979                 das1800_cancel(dev, s); /* disable hardware conversions */
980                 async->events |= COMEDI_CB_EOA;
981         } else if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0) {        /*  stop_src TRIG_COUNT */
982                 das1800_cancel(dev, s); /* disable hardware conversions */
983                 async->events |= COMEDI_CB_EOA;
984         }
985
986         comedi_event(dev, s);
987
988         return;
989 }
990
991 static void das1800_handle_dma(struct comedi_device *dev,
992                                struct comedi_subdevice *s, unsigned int status)
993 {
994         unsigned long flags;
995         const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
996
997         flags = claim_dma_lock();
998         das1800_flush_dma_channel(dev, s, devpriv->dma_current,
999                                   devpriv->dma_current_buf);
1000         /*  re-enable  dma channel */
1001         set_dma_addr(devpriv->dma_current,
1002                      virt_to_bus(devpriv->dma_current_buf));
1003         set_dma_count(devpriv->dma_current, devpriv->dma_transfer_size);
1004         enable_dma(devpriv->dma_current);
1005         release_dma_lock(flags);
1006
1007         if (status & DMATC) {
1008                 /*  clear DMATC interrupt bit */
1009                 outb(CLEAR_INTR_MASK & ~DMATC, dev->iobase + DAS1800_STATUS);
1010                 /*  switch dma channels for next time, if appropriate */
1011                 if (dual_dma) {
1012                         /*  read data from the other channel next time */
1013                         if (devpriv->dma_current == devpriv->dma0) {
1014                                 devpriv->dma_current = devpriv->dma1;
1015                                 devpriv->dma_current_buf = devpriv->ai_buf1;
1016                         } else {
1017                                 devpriv->dma_current = devpriv->dma0;
1018                                 devpriv->dma_current_buf = devpriv->ai_buf0;
1019                         }
1020                 }
1021         }
1022
1023         return;
1024 }
1025
1026 static inline uint16_t munge_bipolar_sample(const struct comedi_device *dev,
1027                                             uint16_t sample)
1028 {
1029         sample += 1 << (thisboard->resolution - 1);
1030         return sample;
1031 }
1032
1033 static void munge_data(struct comedi_device *dev, uint16_t * array,
1034                        unsigned int num_elements)
1035 {
1036         unsigned int i;
1037         int unipolar;
1038
1039         /* see if card is using a unipolar or bipolar range so we can munge data correctly */
1040         unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
1041
1042         /* convert to unsigned type if we are in a bipolar mode */
1043         if (!unipolar) {
1044                 for (i = 0; i < num_elements; i++) {
1045                         array[i] = munge_bipolar_sample(dev, array[i]);
1046                 }
1047         }
1048 }
1049
1050 /* Utility function used by das1800_flush_dma() and das1800_handle_dma().
1051  * Assumes dma lock is held */
1052 static void das1800_flush_dma_channel(struct comedi_device *dev,
1053                                       struct comedi_subdevice *s,
1054                                       unsigned int channel, uint16_t * buffer)
1055 {
1056         unsigned int num_bytes, num_samples;
1057         struct comedi_cmd *cmd = &s->async->cmd;
1058
1059         disable_dma(channel);
1060
1061         /* clear flip-flop to make sure 2-byte registers
1062          * get set correctly */
1063         clear_dma_ff(channel);
1064
1065         /*  figure out how many points to read */
1066         num_bytes = devpriv->dma_transfer_size - get_dma_residue(channel);
1067         num_samples = num_bytes / sizeof(short);
1068
1069         /* if we only need some of the points */
1070         if (cmd->stop_src == TRIG_COUNT && devpriv->count < num_samples)
1071                 num_samples = devpriv->count;
1072
1073         munge_data(dev, buffer, num_samples);
1074         cfc_write_array_to_buffer(s, buffer, num_bytes);
1075         if (s->async->cmd.stop_src == TRIG_COUNT)
1076                 devpriv->count -= num_samples;
1077
1078         return;
1079 }
1080
1081 /* flushes remaining data from board when external trigger has stopped aquisition
1082  * and we are using dma transfers */
1083 static void das1800_flush_dma(struct comedi_device *dev,
1084                               struct comedi_subdevice *s)
1085 {
1086         unsigned long flags;
1087         const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
1088
1089         flags = claim_dma_lock();
1090         das1800_flush_dma_channel(dev, s, devpriv->dma_current,
1091                                   devpriv->dma_current_buf);
1092
1093         if (dual_dma) {
1094                 /*  switch to other channel and flush it */
1095                 if (devpriv->dma_current == devpriv->dma0) {
1096                         devpriv->dma_current = devpriv->dma1;
1097                         devpriv->dma_current_buf = devpriv->ai_buf1;
1098                 } else {
1099                         devpriv->dma_current = devpriv->dma0;
1100                         devpriv->dma_current_buf = devpriv->ai_buf0;
1101                 }
1102                 das1800_flush_dma_channel(dev, s, devpriv->dma_current,
1103                                           devpriv->dma_current_buf);
1104         }
1105
1106         release_dma_lock(flags);
1107
1108         /*  get any remaining samples in fifo */
1109         das1800_handle_fifo_not_empty(dev, s);
1110
1111         return;
1112 }
1113
1114 static void das1800_handle_fifo_half_full(struct comedi_device *dev,
1115                                           struct comedi_subdevice *s)
1116 {
1117         int numPoints = 0;      /* number of points to read */
1118         struct comedi_cmd *cmd = &s->async->cmd;
1119
1120         numPoints = FIFO_SIZE / 2;
1121         /* if we only need some of the points */
1122         if (cmd->stop_src == TRIG_COUNT && devpriv->count < numPoints)
1123                 numPoints = devpriv->count;
1124         insw(dev->iobase + DAS1800_FIFO, devpriv->ai_buf0, numPoints);
1125         munge_data(dev, devpriv->ai_buf0, numPoints);
1126         cfc_write_array_to_buffer(s, devpriv->ai_buf0,
1127                                   numPoints * sizeof(devpriv->ai_buf0[0]));
1128         if (cmd->stop_src == TRIG_COUNT)
1129                 devpriv->count -= numPoints;
1130         return;
1131 }
1132
1133 static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
1134                                           struct comedi_subdevice *s)
1135 {
1136         short dpnt;
1137         int unipolar;
1138         struct comedi_cmd *cmd = &s->async->cmd;
1139
1140         unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
1141
1142         while (inb(dev->iobase + DAS1800_STATUS) & FNE) {
1143                 if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0)
1144                         break;
1145                 dpnt = inw(dev->iobase + DAS1800_FIFO);
1146                 /* convert to unsigned type if we are in a bipolar mode */
1147                 if (!unipolar) ;
1148                 dpnt = munge_bipolar_sample(dev, dpnt);
1149                 cfc_write_to_buffer(s, dpnt);
1150                 if (cmd->stop_src == TRIG_COUNT)
1151                         devpriv->count--;
1152         }
1153
1154         return;
1155 }
1156
1157 static int das1800_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1158 {
1159         outb(0x0, dev->iobase + DAS1800_STATUS);        /* disable conversions */
1160         outb(0x0, dev->iobase + DAS1800_CONTROL_B);     /* disable interrupts and dma */
1161         outb(0x0, dev->iobase + DAS1800_CONTROL_A);     /* disable and clear fifo and stop triggering */
1162         if (devpriv->dma0)
1163                 disable_dma(devpriv->dma0);
1164         if (devpriv->dma1)
1165                 disable_dma(devpriv->dma1);
1166         return 0;
1167 }
1168
1169 /* test analog input cmd */
1170 static int das1800_ai_do_cmdtest(struct comedi_device *dev,
1171                                  struct comedi_subdevice *s,
1172                                  struct comedi_cmd *cmd)
1173 {
1174         int err = 0;
1175         int tmp;
1176         unsigned int tmp_arg;
1177         int i;
1178         int unipolar;
1179
1180         /* step 1: make sure trigger sources are trivially valid */
1181
1182         tmp = cmd->start_src;
1183         cmd->start_src &= TRIG_NOW | TRIG_EXT;
1184         if (!cmd->start_src || tmp != cmd->start_src)
1185                 err++;
1186
1187         tmp = cmd->scan_begin_src;
1188         cmd->scan_begin_src &= TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT;
1189         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1190                 err++;
1191
1192         tmp = cmd->convert_src;
1193         cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
1194         if (!cmd->convert_src || tmp != cmd->convert_src)
1195                 err++;
1196
1197         tmp = cmd->scan_end_src;
1198         cmd->scan_end_src &= TRIG_COUNT;
1199         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1200                 err++;
1201
1202         tmp = cmd->stop_src;
1203         cmd->stop_src &= TRIG_COUNT | TRIG_EXT | TRIG_NONE;
1204         if (!cmd->stop_src || tmp != cmd->stop_src)
1205                 err++;
1206
1207         if (err)
1208                 return 1;
1209
1210         /* step 2: make sure trigger sources are unique and mutually compatible */
1211
1212         /*  uniqueness check */
1213         if (cmd->start_src != TRIG_NOW && cmd->start_src != TRIG_EXT)
1214                 err++;
1215         if (cmd->scan_begin_src != TRIG_FOLLOW &&
1216             cmd->scan_begin_src != TRIG_TIMER &&
1217             cmd->scan_begin_src != TRIG_EXT)
1218                 err++;
1219         if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
1220                 err++;
1221         if (cmd->stop_src != TRIG_COUNT &&
1222             cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
1223                 err++;
1224         /* compatibility check */
1225         if (cmd->scan_begin_src != TRIG_FOLLOW &&
1226             cmd->convert_src != TRIG_TIMER)
1227                 err++;
1228
1229         if (err)
1230                 return 2;
1231
1232         /* step 3: make sure arguments are trivially compatible */
1233
1234         if (cmd->start_arg != 0) {
1235                 cmd->start_arg = 0;
1236                 err++;
1237         }
1238         if (cmd->convert_src == TRIG_TIMER) {
1239                 if (cmd->convert_arg < thisboard->ai_speed) {
1240                         cmd->convert_arg = thisboard->ai_speed;
1241                         err++;
1242                 }
1243         }
1244         if (!cmd->chanlist_len) {
1245                 cmd->chanlist_len = 1;
1246                 err++;
1247         }
1248         if (cmd->scan_end_arg != cmd->chanlist_len) {
1249                 cmd->scan_end_arg = cmd->chanlist_len;
1250                 err++;
1251         }
1252
1253         switch (cmd->stop_src) {
1254         case TRIG_COUNT:
1255                 if (!cmd->stop_arg) {
1256                         cmd->stop_arg = 1;
1257                         err++;
1258                 }
1259                 break;
1260         case TRIG_NONE:
1261                 if (cmd->stop_arg != 0) {
1262                         cmd->stop_arg = 0;
1263                         err++;
1264                 }
1265                 break;
1266         default:
1267                 break;
1268         }
1269
1270         if (err)
1271                 return 3;
1272
1273         /* step 4: fix up any arguments */
1274
1275         if (cmd->convert_src == TRIG_TIMER) {
1276                 /*  if we are not in burst mode */
1277                 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1278                         tmp_arg = cmd->convert_arg;
1279                         /* calculate counter values that give desired timing */
1280                         i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1281                                                        &(devpriv->divisor1),
1282                                                        &(devpriv->divisor2),
1283                                                        &(cmd->convert_arg),
1284                                                        cmd->
1285                                                        flags & TRIG_ROUND_MASK);
1286                         if (tmp_arg != cmd->convert_arg)
1287                                 err++;
1288                 }
1289                 /*  if we are in burst mode */
1290                 else {
1291                         /*  check that convert_arg is compatible */
1292                         tmp_arg = cmd->convert_arg;
1293                         cmd->convert_arg =
1294                             burst_convert_arg(cmd->convert_arg,
1295                                               cmd->flags & TRIG_ROUND_MASK);
1296                         if (tmp_arg != cmd->convert_arg)
1297                                 err++;
1298
1299                         if (cmd->scan_begin_src == TRIG_TIMER) {
1300                                 /*  if scans are timed faster than conversion rate allows */
1301                                 if (cmd->convert_arg * cmd->chanlist_len >
1302                                     cmd->scan_begin_arg) {
1303                                         cmd->scan_begin_arg =
1304                                             cmd->convert_arg *
1305                                             cmd->chanlist_len;
1306                                         err++;
1307                                 }
1308                                 tmp_arg = cmd->scan_begin_arg;
1309                                 /* calculate counter values that give desired timing */
1310                                 i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1311                                                                &(devpriv->
1312                                                                  divisor1),
1313                                                                &(devpriv->
1314                                                                  divisor2),
1315                                                                &(cmd->
1316                                                                  scan_begin_arg),
1317                                                                cmd->
1318                                                                flags &
1319                                                                TRIG_ROUND_MASK);
1320                                 if (tmp_arg != cmd->scan_begin_arg)
1321                                         err++;
1322                         }
1323                 }
1324         }
1325
1326         if (err)
1327                 return 4;
1328
1329         /*  make sure user is not trying to mix unipolar and bipolar ranges */
1330         if (cmd->chanlist) {
1331                 unipolar = CR_RANGE(cmd->chanlist[0]) & UNIPOLAR;
1332                 for (i = 1; i < cmd->chanlist_len; i++) {
1333                         if (unipolar != (CR_RANGE(cmd->chanlist[i]) & UNIPOLAR)) {
1334                                 comedi_error(dev,
1335                                              "unipolar and bipolar ranges cannot be mixed in the chanlist");
1336                                 err++;
1337                                 break;
1338                         }
1339                 }
1340         }
1341
1342         if (err)
1343                 return 5;
1344
1345         return 0;
1346 }
1347
1348 /* analog input cmd interface */
1349
1350 /* first, some utility functions used in the main ai_do_cmd() */
1351
1352 /* returns appropriate bits for control register a, depending on command */
1353 static int control_a_bits(struct comedi_cmd cmd)
1354 {
1355         int control_a;
1356
1357         control_a = FFEN;       /* enable fifo */
1358         if (cmd.stop_src == TRIG_EXT) {
1359                 control_a |= ATEN;
1360         }
1361         switch (cmd.start_src) {
1362         case TRIG_EXT:
1363                 control_a |= TGEN | CGSL;
1364                 break;
1365         case TRIG_NOW:
1366                 control_a |= CGEN;
1367                 break;
1368         default:
1369                 break;
1370         }
1371
1372         return control_a;
1373 }
1374
1375 /* returns appropriate bits for control register c, depending on command */
1376 static int control_c_bits(struct comedi_cmd cmd)
1377 {
1378         int control_c;
1379         int aref;
1380
1381         /* set clock source to internal or external, select analog reference,
1382          * select unipolar / bipolar
1383          */
1384         aref = CR_AREF(cmd.chanlist[0]);
1385         control_c = UQEN;       /* enable upper qram addresses */
1386         if (aref != AREF_DIFF)
1387                 control_c |= SD;
1388         if (aref == AREF_COMMON)
1389                 control_c |= CMEN;
1390         /* if a unipolar range was selected */
1391         if (CR_RANGE(cmd.chanlist[0]) & UNIPOLAR)
1392                 control_c |= UB;
1393         switch (cmd.scan_begin_src) {
1394         case TRIG_FOLLOW:       /*  not in burst mode */
1395                 switch (cmd.convert_src) {
1396                 case TRIG_TIMER:
1397                         /* trig on cascaded counters */
1398                         control_c |= IPCLK;
1399                         break;
1400                 case TRIG_EXT:
1401                         /* trig on falling edge of external trigger */
1402                         control_c |= XPCLK;
1403                         break;
1404                 default:
1405                         break;
1406                 }
1407                 break;
1408         case TRIG_TIMER:
1409                 /*  burst mode with internal pacer clock */
1410                 control_c |= BMDE | IPCLK;
1411                 break;
1412         case TRIG_EXT:
1413                 /*  burst mode with external trigger */
1414                 control_c |= BMDE | XPCLK;
1415                 break;
1416         default:
1417                 break;
1418         }
1419
1420         return control_c;
1421 }
1422
1423 /* sets up counters */
1424 static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd)
1425 {
1426         /*  setup cascaded counters for conversion/scan frequency */
1427         switch (cmd.scan_begin_src) {
1428         case TRIG_FOLLOW:       /*  not in burst mode */
1429                 if (cmd.convert_src == TRIG_TIMER) {
1430                         /* set conversion frequency */
1431                         i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1432                                                        &(devpriv->divisor1),
1433                                                        &(devpriv->divisor2),
1434                                                        &(cmd.convert_arg),
1435                                                        cmd.
1436                                                        flags & TRIG_ROUND_MASK);
1437                         if (das1800_set_frequency(dev) < 0) {
1438                                 return -1;
1439                         }
1440                 }
1441                 break;
1442         case TRIG_TIMER:        /*  in burst mode */
1443                 /* set scan frequency */
1444                 i8253_cascade_ns_to_timer_2div(TIMER_BASE, &(devpriv->divisor1),
1445                                                &(devpriv->divisor2),
1446                                                &(cmd.scan_begin_arg),
1447                                                cmd.flags & TRIG_ROUND_MASK);
1448                 if (das1800_set_frequency(dev) < 0) {
1449                         return -1;
1450                 }
1451                 break;
1452         default:
1453                 break;
1454         }
1455
1456         /*  setup counter 0 for 'about triggering' */
1457         if (cmd.stop_src == TRIG_EXT) {
1458                 /*  load counter 0 in mode 0 */
1459                 i8254_load(dev->iobase + DAS1800_COUNTER, 0, 0, 1, 0);
1460         }
1461
1462         return 0;
1463 }
1464
1465 /* sets up dma */
1466 static void setup_dma(struct comedi_device *dev, struct comedi_cmd cmd)
1467 {
1468         unsigned long lock_flags;
1469         const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
1470
1471         if ((devpriv->irq_dma_bits & DMA_ENABLED) == 0)
1472                 return;
1473
1474         /* determine a reasonable dma transfer size */
1475         devpriv->dma_transfer_size = suggest_transfer_size(&cmd);
1476         lock_flags = claim_dma_lock();
1477         disable_dma(devpriv->dma0);
1478         /* clear flip-flop to make sure 2-byte registers for
1479          * count and address get set correctly */
1480         clear_dma_ff(devpriv->dma0);
1481         set_dma_addr(devpriv->dma0, virt_to_bus(devpriv->ai_buf0));
1482         /*  set appropriate size of transfer */
1483         set_dma_count(devpriv->dma0, devpriv->dma_transfer_size);
1484         devpriv->dma_current = devpriv->dma0;
1485         devpriv->dma_current_buf = devpriv->ai_buf0;
1486         enable_dma(devpriv->dma0);
1487         /*  set up dual dma if appropriate */
1488         if (dual_dma) {
1489                 disable_dma(devpriv->dma1);
1490                 /* clear flip-flop to make sure 2-byte registers for
1491                  * count and address get set correctly */
1492                 clear_dma_ff(devpriv->dma1);
1493                 set_dma_addr(devpriv->dma1, virt_to_bus(devpriv->ai_buf1));
1494                 /*  set appropriate size of transfer */
1495                 set_dma_count(devpriv->dma1, devpriv->dma_transfer_size);
1496                 enable_dma(devpriv->dma1);
1497         }
1498         release_dma_lock(lock_flags);
1499
1500         return;
1501 }
1502
1503 /* programs channel/gain list into card */
1504 static void program_chanlist(struct comedi_device *dev, struct comedi_cmd cmd)
1505 {
1506         int i, n, chan_range;
1507         unsigned long irq_flags;
1508         const int range_mask = 0x3;     /* masks unipolar/bipolar bit off range */
1509         const int range_bitshift = 8;
1510
1511         n = cmd.chanlist_len;
1512         /*  spinlock protects indirect addressing */
1513         spin_lock_irqsave(&dev->spinlock, irq_flags);
1514         outb(QRAM, dev->iobase + DAS1800_SELECT);       /* select QRAM for baseAddress + 0x0 */
1515         outb(n - 1, dev->iobase + DAS1800_QRAM_ADDRESS);        /*set QRAM address start */
1516         /* make channel / gain list */
1517         for (i = 0; i < n; i++) {
1518                 chan_range =
1519                     CR_CHAN(cmd.
1520                             chanlist[i]) | ((CR_RANGE(cmd.chanlist[i]) &
1521                                              range_mask) << range_bitshift);
1522                 outw(chan_range, dev->iobase + DAS1800_QRAM);
1523         }
1524         outb(n - 1, dev->iobase + DAS1800_QRAM_ADDRESS);        /*finish write to QRAM */
1525         spin_unlock_irqrestore(&dev->spinlock, irq_flags);
1526
1527         return;
1528 }
1529
1530 /* analog input do_cmd */
1531 static int das1800_ai_do_cmd(struct comedi_device *dev,
1532                              struct comedi_subdevice *s)
1533 {
1534         int ret;
1535         int control_a, control_c;
1536         struct comedi_async *async = s->async;
1537         struct comedi_cmd cmd = async->cmd;
1538
1539         if (!dev->irq) {
1540                 comedi_error(dev,
1541                              "no irq assigned for das-1800, cannot do hardware conversions");
1542                 return -1;
1543         }
1544
1545         /* disable dma on TRIG_WAKE_EOS, or TRIG_RT
1546          * (because dma in handler is unsafe at hard real-time priority) */
1547         if (cmd.flags & (TRIG_WAKE_EOS | TRIG_RT)) {
1548                 devpriv->irq_dma_bits &= ~DMA_ENABLED;
1549         } else {
1550                 devpriv->irq_dma_bits |= devpriv->dma_bits;
1551         }
1552         /*  interrupt on end of conversion for TRIG_WAKE_EOS */
1553         if (cmd.flags & TRIG_WAKE_EOS) {
1554                 /*  interrupt fifo not empty */
1555                 devpriv->irq_dma_bits &= ~FIMD;
1556         } else {
1557                 /*  interrupt fifo half full */
1558                 devpriv->irq_dma_bits |= FIMD;
1559         }
1560         /*  determine how many conversions we need */
1561         if (cmd.stop_src == TRIG_COUNT) {
1562                 devpriv->count = cmd.stop_arg * cmd.chanlist_len;
1563         }
1564
1565         das1800_cancel(dev, s);
1566
1567         /*  determine proper bits for control registers */
1568         control_a = control_a_bits(cmd);
1569         control_c = control_c_bits(cmd);
1570
1571         /* setup card and start */
1572         program_chanlist(dev, cmd);
1573         ret = setup_counters(dev, cmd);
1574         if (ret < 0) {
1575                 comedi_error(dev, "Error setting up counters");
1576                 return ret;
1577         }
1578         setup_dma(dev, cmd);
1579         outb(control_c, dev->iobase + DAS1800_CONTROL_C);
1580         /*  set conversion rate and length for burst mode */
1581         if (control_c & BMDE) {
1582                 /*  program conversion period with number of microseconds minus 1 */
1583                 outb(cmd.convert_arg / 1000 - 1,
1584                      dev->iobase + DAS1800_BURST_RATE);
1585                 outb(cmd.chanlist_len - 1, dev->iobase + DAS1800_BURST_LENGTH);
1586         }
1587         outb(devpriv->irq_dma_bits, dev->iobase + DAS1800_CONTROL_B);   /*  enable irq/dma */
1588         outb(control_a, dev->iobase + DAS1800_CONTROL_A);       /* enable fifo and triggering */
1589         outb(CVEN, dev->iobase + DAS1800_STATUS);       /* enable conversions */
1590
1591         return 0;
1592 }
1593
1594 /* read analog input */
1595 static int das1800_ai_rinsn(struct comedi_device *dev,
1596                             struct comedi_subdevice *s,
1597                             struct comedi_insn *insn, unsigned int *data)
1598 {
1599         int i, n;
1600         int chan, range, aref, chan_range;
1601         int timeout = 1000;
1602         short dpnt;
1603         int conv_flags = 0;
1604         unsigned long irq_flags;
1605
1606         /* set up analog reference and unipolar / bipolar mode */
1607         aref = CR_AREF(insn->chanspec);
1608         conv_flags |= UQEN;
1609         if (aref != AREF_DIFF)
1610                 conv_flags |= SD;
1611         if (aref == AREF_COMMON)
1612                 conv_flags |= CMEN;
1613         /* if a unipolar range was selected */
1614         if (CR_RANGE(insn->chanspec) & UNIPOLAR)
1615                 conv_flags |= UB;
1616
1617         outb(conv_flags, dev->iobase + DAS1800_CONTROL_C);      /* software conversion enabled */
1618         outb(CVEN, dev->iobase + DAS1800_STATUS);       /* enable conversions */
1619         outb(0x0, dev->iobase + DAS1800_CONTROL_A);     /* reset fifo */
1620         outb(FFEN, dev->iobase + DAS1800_CONTROL_A);
1621
1622         chan = CR_CHAN(insn->chanspec);
1623         /* mask of unipolar/bipolar bit from range */
1624         range = CR_RANGE(insn->chanspec) & 0x3;
1625         chan_range = chan | (range << 8);
1626         spin_lock_irqsave(&dev->spinlock, irq_flags);
1627         outb(QRAM, dev->iobase + DAS1800_SELECT);       /* select QRAM for baseAddress + 0x0 */
1628         outb(0x0, dev->iobase + DAS1800_QRAM_ADDRESS);  /* set QRAM address start */
1629         outw(chan_range, dev->iobase + DAS1800_QRAM);
1630         outb(0x0, dev->iobase + DAS1800_QRAM_ADDRESS);  /*finish write to QRAM */
1631         outb(ADC, dev->iobase + DAS1800_SELECT);        /* select ADC for baseAddress + 0x0 */
1632
1633         for (n = 0; n < insn->n; n++) {
1634                 /* trigger conversion */
1635                 outb(0, dev->iobase + DAS1800_FIFO);
1636                 for (i = 0; i < timeout; i++) {
1637                         if (inb(dev->iobase + DAS1800_STATUS) & FNE)
1638                                 break;
1639                 }
1640                 if (i == timeout) {
1641                         comedi_error(dev, "timeout");
1642                         return -ETIME;
1643                 }
1644                 dpnt = inw(dev->iobase + DAS1800_FIFO);
1645                 /* shift data to offset binary for bipolar ranges */
1646                 if ((conv_flags & UB) == 0)
1647                         dpnt += 1 << (thisboard->resolution - 1);
1648                 data[n] = dpnt;
1649         }
1650         spin_unlock_irqrestore(&dev->spinlock, irq_flags);
1651
1652         return n;
1653 }
1654
1655 /* writes to an analog output channel */
1656 static int das1800_ao_winsn(struct comedi_device *dev,
1657                             struct comedi_subdevice *s,
1658                             struct comedi_insn *insn, unsigned int *data)
1659 {
1660         int chan = CR_CHAN(insn->chanspec);
1661 /* int range = CR_RANGE(insn->chanspec); */
1662         int update_chan = thisboard->ao_n_chan - 1;
1663         short output;
1664         unsigned long irq_flags;
1665
1666         /*   card expects two's complement data */
1667         output = data[0] - (1 << (thisboard->resolution - 1));
1668         /*  if the write is to the 'update' channel, we need to remember its value */
1669         if (chan == update_chan)
1670                 devpriv->ao_update_bits = output;
1671         /*  write to channel */
1672         spin_lock_irqsave(&dev->spinlock, irq_flags);
1673         outb(DAC(chan), dev->iobase + DAS1800_SELECT);  /* select dac channel for baseAddress + 0x0 */
1674         outw(output, dev->iobase + DAS1800_DAC);
1675         /*  now we need to write to 'update' channel to update all dac channels */
1676         if (chan != update_chan) {
1677                 outb(DAC(update_chan), dev->iobase + DAS1800_SELECT);   /* select 'update' channel for baseAddress + 0x0 */
1678                 outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC);
1679         }
1680         spin_unlock_irqrestore(&dev->spinlock, irq_flags);
1681
1682         return 1;
1683 }
1684
1685 /* reads from digital input channels */
1686 static int das1800_di_rbits(struct comedi_device *dev,
1687                             struct comedi_subdevice *s,
1688                             struct comedi_insn *insn, unsigned int *data)
1689 {
1690
1691         data[1] = inb(dev->iobase + DAS1800_DIGITAL) & 0xf;
1692         data[0] = 0;
1693
1694         return 2;
1695 }
1696
1697 /* writes to digital output channels */
1698 static int das1800_do_wbits(struct comedi_device *dev,
1699                             struct comedi_subdevice *s,
1700                             struct comedi_insn *insn, unsigned int *data)
1701 {
1702         unsigned int wbits;
1703
1704         /*  only set bits that have been masked */
1705         data[0] &= (1 << s->n_chan) - 1;
1706         wbits = devpriv->do_bits;
1707         wbits &= ~data[0];
1708         wbits |= data[0] & data[1];
1709         devpriv->do_bits = wbits;
1710
1711         outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL);
1712
1713         data[1] = devpriv->do_bits;
1714
1715         return 2;
1716 }
1717
1718 /* loads counters with divisor1, divisor2 from private structure */
1719 static int das1800_set_frequency(struct comedi_device *dev)
1720 {
1721         int err = 0;
1722
1723         /*  counter 1, mode 2 */
1724         if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 1, devpriv->divisor1,
1725                        2))
1726                 err++;
1727         /*  counter 2, mode 2 */
1728         if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 2, devpriv->divisor2,
1729                        2))
1730                 err++;
1731         if (err)
1732                 return -1;
1733
1734         return 0;
1735 }
1736
1737 /* converts requested conversion timing to timing compatible with
1738  * hardware, used only when card is in 'burst mode'
1739  */
1740 static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode)
1741 {
1742         unsigned int micro_sec;
1743
1744         /*  in burst mode, the maximum conversion time is 64 microseconds */
1745         if (convert_arg > 64000)
1746                 convert_arg = 64000;
1747
1748         /*  the conversion time must be an integral number of microseconds */
1749         switch (round_mode) {
1750         case TRIG_ROUND_NEAREST:
1751         default:
1752                 micro_sec = (convert_arg + 500) / 1000;
1753                 break;
1754         case TRIG_ROUND_DOWN:
1755                 micro_sec = convert_arg / 1000;
1756                 break;
1757         case TRIG_ROUND_UP:
1758                 micro_sec = (convert_arg - 1) / 1000 + 1;
1759                 break;
1760         }
1761
1762         /*  return number of nanoseconds */
1763         return micro_sec * 1000;
1764 }
1765
1766 /* utility function that suggests a dma transfer size based on the conversion period 'ns' */
1767 static unsigned int suggest_transfer_size(struct comedi_cmd *cmd)
1768 {
1769         unsigned int size = DMA_BUF_SIZE;
1770         static const int sample_size = 2;       /*  size in bytes of one sample from board */
1771         unsigned int fill_time = 300000000;     /*  target time in nanoseconds for filling dma buffer */
1772         unsigned int max_size;  /*  maximum size we will allow for a transfer */
1773
1774         /*  make dma buffer fill in 0.3 seconds for timed modes */
1775         switch (cmd->scan_begin_src) {
1776         case TRIG_FOLLOW:       /*  not in burst mode */
1777                 if (cmd->convert_src == TRIG_TIMER)
1778                         size = (fill_time / cmd->convert_arg) * sample_size;
1779                 break;
1780         case TRIG_TIMER:
1781                 size = (fill_time / (cmd->scan_begin_arg * cmd->chanlist_len)) *
1782                     sample_size;
1783                 break;
1784         default:
1785                 size = DMA_BUF_SIZE;
1786                 break;
1787         }
1788
1789         /*  set a minimum and maximum size allowed */
1790         max_size = DMA_BUF_SIZE;
1791         /*  if we are taking limited number of conversions, limit transfer size to that */
1792         if (cmd->stop_src == TRIG_COUNT &&
1793             cmd->stop_arg * cmd->chanlist_len * sample_size < max_size)
1794                 max_size = cmd->stop_arg * cmd->chanlist_len * sample_size;
1795
1796         if (size > max_size)
1797                 size = max_size;
1798         if (size < sample_size)
1799                 size = sample_size;
1800
1801         return size;
1802 }