]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/ni_mio_common.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.git] / drivers / staging / comedi / drivers / ni_mio_common.c
1 /*
2     comedi/drivers/ni_mio_common.c
3     Hardware driver for DAQ-STC based boards
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7     Copyright (C) 2002-2006 Frank Mori Hess <fmhess@users.sourceforge.net>
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
20 /*
21         This file is meant to be included by another file, e.g.,
22         ni_atmio.c or ni_pcimio.c.
23
24         Interrupt support originally added by Truxton Fulton
25         <trux@truxton.com>
26
27         References (from ftp://ftp.natinst.com/support/manuals):
28
29            340747b.pdf  AT-MIO E series Register Level Programmer Manual
30            341079b.pdf  PCI E Series RLPM
31            340934b.pdf  DAQ-STC reference manual
32         67xx and 611x registers (from ftp://ftp.ni.com/support/daq/mhddk/documentation/)
33         release_ni611x.pdf
34         release_ni67xx.pdf
35         Other possibly relevant info:
36
37            320517c.pdf  User manual (obsolete)
38            320517f.pdf  User manual (new)
39            320889a.pdf  delete
40            320906c.pdf  maximum signal ratings
41            321066a.pdf  about 16x
42            321791a.pdf  discontinuation of at-mio-16e-10 rev. c
43            321808a.pdf  about at-mio-16e-10 rev P
44            321837a.pdf  discontinuation of at-mio-16de-10 rev d
45            321838a.pdf  about at-mio-16de-10 rev N
46
47         ISSUES:
48
49          - the interrupt routine needs to be cleaned up
50
51         2006-02-07: S-Series PCI-6143: Support has been added but is not
52                 fully tested as yet. Terry Barnaby, BEAM Ltd.
53 */
54
55 /* #define DEBUG_INTERRUPT */
56 /* #define DEBUG_STATUS_A */
57 /* #define DEBUG_STATUS_B */
58
59 #include <linux/interrupt.h>
60 #include <linux/sched.h>
61 #include <linux/delay.h>
62 #include "8255.h"
63 #include "mite.h"
64 #include "comedi_fc.h"
65
66 #ifndef MDPRINTK
67 #define MDPRINTK(format, args...)
68 #endif
69
70 /* A timeout count */
71 #define NI_TIMEOUT 1000
72 static const unsigned old_RTSI_clock_channel = 7;
73
74 /* Note: this table must match the ai_gain_* definitions */
75 static const short ni_gainlkup[][16] = {
76         [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
77                         0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
78         [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
79         [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
80                         0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
81         [ai_gain_4] = {0, 1, 4, 7},
82         [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
83                           0x003, 0x004, 0x005, 0x006},
84         [ai_gain_622x] = {0, 1, 4, 5},
85         [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
86         [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
87 };
88
89 static const struct comedi_lrange range_ni_E_ai = { 16, {
90                                                          RANGE(-10, 10),
91                                                          RANGE(-5, 5),
92                                                          RANGE(-2.5, 2.5),
93                                                          RANGE(-1, 1),
94                                                          RANGE(-0.5, 0.5),
95                                                          RANGE(-0.25, 0.25),
96                                                          RANGE(-0.1, 0.1),
97                                                          RANGE(-0.05, 0.05),
98                                                          RANGE(0, 20),
99                                                          RANGE(0, 10),
100                                                          RANGE(0, 5),
101                                                          RANGE(0, 2),
102                                                          RANGE(0, 1),
103                                                          RANGE(0, 0.5),
104                                                          RANGE(0, 0.2),
105                                                          RANGE(0, 0.1),
106                                                          }
107 };
108
109 static const struct comedi_lrange range_ni_E_ai_limited = { 8, {
110                                                                 RANGE(-10, 10),
111                                                                 RANGE(-5, 5),
112                                                                 RANGE(-1, 1),
113                                                                 RANGE(-0.1,
114                                                                       0.1),
115                                                                 RANGE(0, 10),
116                                                                 RANGE(0, 5),
117                                                                 RANGE(0, 1),
118                                                                 RANGE(0, 0.1),
119                                                                 }
120 };
121
122 static const struct comedi_lrange range_ni_E_ai_limited14 = { 14, {
123                                                                    RANGE(-10,
124                                                                          10),
125                                                                    RANGE(-5, 5),
126                                                                    RANGE(-2, 2),
127                                                                    RANGE(-1, 1),
128                                                                    RANGE(-0.5,
129                                                                          0.5),
130                                                                    RANGE(-0.2,
131                                                                          0.2),
132                                                                    RANGE(-0.1,
133                                                                          0.1),
134                                                                    RANGE(0, 10),
135                                                                    RANGE(0, 5),
136                                                                    RANGE(0, 2),
137                                                                    RANGE(0, 1),
138                                                                    RANGE(0,
139                                                                          0.5),
140                                                                    RANGE(0,
141                                                                          0.2),
142                                                                    RANGE(0,
143                                                                          0.1),
144                                                                    }
145 };
146
147 static const struct comedi_lrange range_ni_E_ai_bipolar4 = { 4, {
148                                                                  RANGE(-10, 10),
149                                                                  RANGE(-5, 5),
150                                                                  RANGE(-0.5,
151                                                                        0.5),
152                                                                  RANGE(-0.05,
153                                                                        0.05),
154                                                                  }
155 };
156
157 static const struct comedi_lrange range_ni_E_ai_611x = { 8, {
158                                                              RANGE(-50, 50),
159                                                              RANGE(-20, 20),
160                                                              RANGE(-10, 10),
161                                                              RANGE(-5, 5),
162                                                              RANGE(-2, 2),
163                                                              RANGE(-1, 1),
164                                                              RANGE(-0.5, 0.5),
165                                                              RANGE(-0.2, 0.2),
166                                                              }
167 };
168
169 static const struct comedi_lrange range_ni_M_ai_622x = { 4, {
170                                                              RANGE(-10, 10),
171                                                              RANGE(-5, 5),
172                                                              RANGE(-1, 1),
173                                                              RANGE(-0.2, 0.2),
174                                                              }
175 };
176
177 static const struct comedi_lrange range_ni_M_ai_628x = { 7, {
178                                                              RANGE(-10, 10),
179                                                              RANGE(-5, 5),
180                                                              RANGE(-2, 2),
181                                                              RANGE(-1, 1),
182                                                              RANGE(-0.5, 0.5),
183                                                              RANGE(-0.2, 0.2),
184                                                              RANGE(-0.1, 0.1),
185                                                              }
186 };
187
188 static const struct comedi_lrange range_ni_E_ao_ext = { 4, {
189                                                             RANGE(-10, 10),
190                                                             RANGE(0, 10),
191                                                             RANGE_ext(-1, 1),
192                                                             RANGE_ext(0, 1),
193                                                             }
194 };
195
196 static const struct comedi_lrange *const ni_range_lkup[] = {
197         [ai_gain_16] = &range_ni_E_ai,
198         [ai_gain_8] = &range_ni_E_ai_limited,
199         [ai_gain_14] = &range_ni_E_ai_limited14,
200         [ai_gain_4] = &range_ni_E_ai_bipolar4,
201         [ai_gain_611x] = &range_ni_E_ai_611x,
202         [ai_gain_622x] = &range_ni_M_ai_622x,
203         [ai_gain_628x] = &range_ni_M_ai_628x,
204         [ai_gain_6143] = &range_bipolar5
205 };
206
207 static int ni_dio_insn_config(struct comedi_device *dev,
208                               struct comedi_subdevice *s,
209                               struct comedi_insn *insn, unsigned int *data);
210 static int ni_dio_insn_bits(struct comedi_device *dev,
211                             struct comedi_subdevice *s,
212                             struct comedi_insn *insn, unsigned int *data);
213 static int ni_cdio_cmdtest(struct comedi_device *dev,
214                            struct comedi_subdevice *s, struct comedi_cmd *cmd);
215 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
216 static int ni_cdio_cancel(struct comedi_device *dev,
217                           struct comedi_subdevice *s);
218 static void handle_cdio_interrupt(struct comedi_device *dev);
219 static int ni_cdo_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
220                           unsigned int trignum);
221
222 static int ni_serial_insn_config(struct comedi_device *dev,
223                                  struct comedi_subdevice *s,
224                                  struct comedi_insn *insn, unsigned int *data);
225 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
226                                    struct comedi_subdevice *s,
227                                    unsigned char data_out,
228                                    unsigned char *data_in);
229 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
230                                    struct comedi_subdevice *s,
231                                    unsigned char data_out,
232                                    unsigned char *data_in);
233
234 static int ni_calib_insn_read(struct comedi_device *dev,
235                               struct comedi_subdevice *s,
236                               struct comedi_insn *insn, unsigned int *data);
237 static int ni_calib_insn_write(struct comedi_device *dev,
238                                struct comedi_subdevice *s,
239                                struct comedi_insn *insn, unsigned int *data);
240
241 static int ni_eeprom_insn_read(struct comedi_device *dev,
242                                struct comedi_subdevice *s,
243                                struct comedi_insn *insn, unsigned int *data);
244 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
245                                         struct comedi_subdevice *s,
246                                         struct comedi_insn *insn,
247                                         unsigned int *data);
248
249 static int ni_pfi_insn_bits(struct comedi_device *dev,
250                             struct comedi_subdevice *s,
251                             struct comedi_insn *insn, unsigned int *data);
252 static int ni_pfi_insn_config(struct comedi_device *dev,
253                               struct comedi_subdevice *s,
254                               struct comedi_insn *insn, unsigned int *data);
255 static unsigned ni_old_get_pfi_routing(struct comedi_device *dev,
256                                        unsigned chan);
257
258 static void ni_rtsi_init(struct comedi_device *dev);
259 static int ni_rtsi_insn_bits(struct comedi_device *dev,
260                              struct comedi_subdevice *s,
261                              struct comedi_insn *insn, unsigned int *data);
262 static int ni_rtsi_insn_config(struct comedi_device *dev,
263                                struct comedi_subdevice *s,
264                                struct comedi_insn *insn, unsigned int *data);
265
266 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s);
267 static int ni_read_eeprom(struct comedi_device *dev, int addr);
268
269 #ifdef DEBUG_STATUS_A
270 static void ni_mio_print_status_a(int status);
271 #else
272 #define ni_mio_print_status_a(a)
273 #endif
274 #ifdef DEBUG_STATUS_B
275 static void ni_mio_print_status_b(int status);
276 #else
277 #define ni_mio_print_status_b(a)
278 #endif
279
280 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s);
281 #ifndef PCIDMA
282 static void ni_handle_fifo_half_full(struct comedi_device *dev);
283 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
284                                  struct comedi_subdevice *s);
285 #endif
286 static void ni_handle_fifo_dregs(struct comedi_device *dev);
287 static int ni_ai_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
288                          unsigned int trignum);
289 static void ni_load_channelgain_list(struct comedi_device *dev,
290                                      unsigned int n_chan, unsigned int *list);
291 static void shutdown_ai_command(struct comedi_device *dev);
292
293 static int ni_ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
294                          unsigned int trignum);
295
296 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s);
297
298 static int ni_8255_callback(int dir, int port, int data, unsigned long arg);
299
300 static int ni_gpct_insn_write(struct comedi_device *dev,
301                               struct comedi_subdevice *s,
302                               struct comedi_insn *insn, unsigned int *data);
303 static int ni_gpct_insn_read(struct comedi_device *dev,
304                              struct comedi_subdevice *s,
305                              struct comedi_insn *insn, unsigned int *data);
306 static int ni_gpct_insn_config(struct comedi_device *dev,
307                                struct comedi_subdevice *s,
308                                struct comedi_insn *insn, unsigned int *data);
309 #ifdef PCIDMA
310 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
311 static int ni_gpct_cmdtest(struct comedi_device *dev,
312                            struct comedi_subdevice *s, struct comedi_cmd *cmd);
313 #endif
314 static int ni_gpct_cancel(struct comedi_device *dev,
315                           struct comedi_subdevice *s);
316 static void handle_gpct_interrupt(struct comedi_device *dev,
317                                   unsigned short counter_index);
318
319 static int init_cs5529(struct comedi_device *dev);
320 static int cs5529_do_conversion(struct comedi_device *dev,
321                                 unsigned short *data);
322 static int cs5529_ai_insn_read(struct comedi_device *dev,
323                                struct comedi_subdevice *s,
324                                struct comedi_insn *insn, unsigned int *data);
325 #ifdef NI_CS5529_DEBUG
326 static unsigned int cs5529_config_read(struct comedi_device *dev,
327                                        unsigned int reg_select_bits);
328 #endif
329 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
330                                 unsigned int reg_select_bits);
331
332 static int ni_m_series_pwm_config(struct comedi_device *dev,
333                                   struct comedi_subdevice *s,
334                                   struct comedi_insn *insn, unsigned int *data);
335 static int ni_6143_pwm_config(struct comedi_device *dev,
336                               struct comedi_subdevice *s,
337                               struct comedi_insn *insn, unsigned int *data);
338
339 static int ni_set_master_clock(struct comedi_device *dev, unsigned source,
340                                unsigned period_ns);
341 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status);
342 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status);
343
344 enum aimodes {
345         AIMODE_NONE = 0,
346         AIMODE_HALF_FULL = 1,
347         AIMODE_SCAN = 2,
348         AIMODE_SAMPLE = 3,
349 };
350
351 enum ni_common_subdevices {
352         NI_AI_SUBDEV,
353         NI_AO_SUBDEV,
354         NI_DIO_SUBDEV,
355         NI_8255_DIO_SUBDEV,
356         NI_UNUSED_SUBDEV,
357         NI_CALIBRATION_SUBDEV,
358         NI_EEPROM_SUBDEV,
359         NI_PFI_DIO_SUBDEV,
360         NI_CS5529_CALIBRATION_SUBDEV,
361         NI_SERIAL_SUBDEV,
362         NI_RTSI_SUBDEV,
363         NI_GPCT0_SUBDEV,
364         NI_GPCT1_SUBDEV,
365         NI_FREQ_OUT_SUBDEV,
366         NI_NUM_SUBDEVICES
367 };
368 static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index)
369 {
370         switch (counter_index) {
371         case 0:
372                 return NI_GPCT0_SUBDEV;
373                 break;
374         case 1:
375                 return NI_GPCT1_SUBDEV;
376                 break;
377         default:
378                 break;
379         }
380         BUG();
381         return NI_GPCT0_SUBDEV;
382 }
383
384 enum timebase_nanoseconds {
385         TIMEBASE_1_NS = 50,
386         TIMEBASE_2_NS = 10000
387 };
388
389 #define SERIAL_DISABLED         0
390 #define SERIAL_600NS            600
391 #define SERIAL_1_2US            1200
392 #define SERIAL_10US                     10000
393
394 static const int num_adc_stages_611x = 3;
395
396 static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
397                                unsigned ai_mite_status);
398 static void handle_b_interrupt(struct comedi_device *dev, unsigned short status,
399                                unsigned ao_mite_status);
400 static void get_last_sample_611x(struct comedi_device *dev);
401 static void get_last_sample_6143(struct comedi_device *dev);
402
403 static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
404                                    unsigned bit_mask, unsigned bit_values)
405 {
406         struct ni_private *devpriv = dev->private;
407         unsigned long flags;
408
409         spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
410         switch (reg) {
411         case Interrupt_A_Enable_Register:
412                 devpriv->int_a_enable_reg &= ~bit_mask;
413                 devpriv->int_a_enable_reg |= bit_values & bit_mask;
414                 devpriv->stc_writew(dev, devpriv->int_a_enable_reg,
415                                     Interrupt_A_Enable_Register);
416                 break;
417         case Interrupt_B_Enable_Register:
418                 devpriv->int_b_enable_reg &= ~bit_mask;
419                 devpriv->int_b_enable_reg |= bit_values & bit_mask;
420                 devpriv->stc_writew(dev, devpriv->int_b_enable_reg,
421                                     Interrupt_B_Enable_Register);
422                 break;
423         case IO_Bidirection_Pin_Register:
424                 devpriv->io_bidirection_pin_reg &= ~bit_mask;
425                 devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
426                 devpriv->stc_writew(dev, devpriv->io_bidirection_pin_reg,
427                                     IO_Bidirection_Pin_Register);
428                 break;
429         case AI_AO_Select:
430                 devpriv->ai_ao_select_reg &= ~bit_mask;
431                 devpriv->ai_ao_select_reg |= bit_values & bit_mask;
432                 ni_writeb(devpriv->ai_ao_select_reg, AI_AO_Select);
433                 break;
434         case G0_G1_Select:
435                 devpriv->g0_g1_select_reg &= ~bit_mask;
436                 devpriv->g0_g1_select_reg |= bit_values & bit_mask;
437                 ni_writeb(devpriv->g0_g1_select_reg, G0_G1_Select);
438                 break;
439         default:
440                 printk("Warning %s() called with invalid register\n", __func__);
441                 printk("reg is %d\n", reg);
442                 break;
443         }
444         mmiowb();
445         spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
446 }
447
448 #ifdef PCIDMA
449 static int ni_ai_drain_dma(struct comedi_device *dev);
450
451 /* DMA channel setup */
452
453 /* negative channel means no channel */
454 static inline void ni_set_ai_dma_channel(struct comedi_device *dev, int channel)
455 {
456         unsigned bitfield;
457
458         if (channel >= 0) {
459                 bitfield =
460                     (ni_stc_dma_channel_select_bitfield(channel) <<
461                      AI_DMA_Select_Shift) & AI_DMA_Select_Mask;
462         } else {
463                 bitfield = 0;
464         }
465         ni_set_bitfield(dev, AI_AO_Select, AI_DMA_Select_Mask, bitfield);
466 }
467
468 /* negative channel means no channel */
469 static inline void ni_set_ao_dma_channel(struct comedi_device *dev, int channel)
470 {
471         unsigned bitfield;
472
473         if (channel >= 0) {
474                 bitfield =
475                     (ni_stc_dma_channel_select_bitfield(channel) <<
476                      AO_DMA_Select_Shift) & AO_DMA_Select_Mask;
477         } else {
478                 bitfield = 0;
479         }
480         ni_set_bitfield(dev, AI_AO_Select, AO_DMA_Select_Mask, bitfield);
481 }
482
483 /* negative mite_channel means no channel */
484 static inline void ni_set_gpct_dma_channel(struct comedi_device *dev,
485                                            unsigned gpct_index,
486                                            int mite_channel)
487 {
488         unsigned bitfield;
489
490         if (mite_channel >= 0) {
491                 bitfield = GPCT_DMA_Select_Bits(gpct_index, mite_channel);
492         } else {
493                 bitfield = 0;
494         }
495         ni_set_bitfield(dev, G0_G1_Select, GPCT_DMA_Select_Mask(gpct_index),
496                         bitfield);
497 }
498
499 /* negative mite_channel means no channel */
500 static inline void ni_set_cdo_dma_channel(struct comedi_device *dev,
501                                           int mite_channel)
502 {
503         struct ni_private *devpriv = dev->private;
504         unsigned long flags;
505
506         spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
507         devpriv->cdio_dma_select_reg &= ~CDO_DMA_Select_Mask;
508         if (mite_channel >= 0) {
509                 /*XXX just guessing ni_stc_dma_channel_select_bitfield() returns the right bits,
510                    under the assumption the cdio dma selection works just like ai/ao/gpct.
511                    Definitely works for dma channels 0 and 1. */
512                 devpriv->cdio_dma_select_reg |=
513                     (ni_stc_dma_channel_select_bitfield(mite_channel) <<
514                      CDO_DMA_Select_Shift) & CDO_DMA_Select_Mask;
515         }
516         ni_writeb(devpriv->cdio_dma_select_reg, M_Offset_CDIO_DMA_Select);
517         mmiowb();
518         spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
519 }
520
521 static int ni_request_ai_mite_channel(struct comedi_device *dev)
522 {
523         struct ni_private *devpriv = dev->private;
524         unsigned long flags;
525
526         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
527         BUG_ON(devpriv->ai_mite_chan);
528         devpriv->ai_mite_chan =
529             mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
530         if (devpriv->ai_mite_chan == NULL) {
531                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
532                 comedi_error(dev,
533                              "failed to reserve mite dma channel for analog input.");
534                 return -EBUSY;
535         }
536         devpriv->ai_mite_chan->dir = COMEDI_INPUT;
537         ni_set_ai_dma_channel(dev, devpriv->ai_mite_chan->channel);
538         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
539         return 0;
540 }
541
542 static int ni_request_ao_mite_channel(struct comedi_device *dev)
543 {
544         struct ni_private *devpriv = dev->private;
545         unsigned long flags;
546
547         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
548         BUG_ON(devpriv->ao_mite_chan);
549         devpriv->ao_mite_chan =
550             mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
551         if (devpriv->ao_mite_chan == NULL) {
552                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
553                 comedi_error(dev,
554                              "failed to reserve mite dma channel for analog outut.");
555                 return -EBUSY;
556         }
557         devpriv->ao_mite_chan->dir = COMEDI_OUTPUT;
558         ni_set_ao_dma_channel(dev, devpriv->ao_mite_chan->channel);
559         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
560         return 0;
561 }
562
563 static int ni_request_gpct_mite_channel(struct comedi_device *dev,
564                                         unsigned gpct_index,
565                                         enum comedi_io_direction direction)
566 {
567         struct ni_private *devpriv = dev->private;
568         unsigned long flags;
569         struct mite_channel *mite_chan;
570
571         BUG_ON(gpct_index >= NUM_GPCT);
572         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
573         BUG_ON(devpriv->counter_dev->counters[gpct_index].mite_chan);
574         mite_chan =
575             mite_request_channel(devpriv->mite,
576                                  devpriv->gpct_mite_ring[gpct_index]);
577         if (mite_chan == NULL) {
578                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
579                 comedi_error(dev,
580                              "failed to reserve mite dma channel for counter.");
581                 return -EBUSY;
582         }
583         mite_chan->dir = direction;
584         ni_tio_set_mite_channel(&devpriv->counter_dev->counters[gpct_index],
585                                 mite_chan);
586         ni_set_gpct_dma_channel(dev, gpct_index, mite_chan->channel);
587         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
588         return 0;
589 }
590
591 #endif /*  PCIDMA */
592
593 static int ni_request_cdo_mite_channel(struct comedi_device *dev)
594 {
595 #ifdef PCIDMA
596         struct ni_private *devpriv = dev->private;
597         unsigned long flags;
598
599         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
600         BUG_ON(devpriv->cdo_mite_chan);
601         devpriv->cdo_mite_chan =
602             mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
603         if (devpriv->cdo_mite_chan == NULL) {
604                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
605                 comedi_error(dev,
606                              "failed to reserve mite dma channel for correlated digital outut.");
607                 return -EBUSY;
608         }
609         devpriv->cdo_mite_chan->dir = COMEDI_OUTPUT;
610         ni_set_cdo_dma_channel(dev, devpriv->cdo_mite_chan->channel);
611         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
612 #endif /*  PCIDMA */
613         return 0;
614 }
615
616 static void ni_release_ai_mite_channel(struct comedi_device *dev)
617 {
618 #ifdef PCIDMA
619         struct ni_private *devpriv = dev->private;
620         unsigned long flags;
621
622         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
623         if (devpriv->ai_mite_chan) {
624                 ni_set_ai_dma_channel(dev, -1);
625                 mite_release_channel(devpriv->ai_mite_chan);
626                 devpriv->ai_mite_chan = NULL;
627         }
628         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
629 #endif /*  PCIDMA */
630 }
631
632 static void ni_release_ao_mite_channel(struct comedi_device *dev)
633 {
634 #ifdef PCIDMA
635         struct ni_private *devpriv = dev->private;
636         unsigned long flags;
637
638         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
639         if (devpriv->ao_mite_chan) {
640                 ni_set_ao_dma_channel(dev, -1);
641                 mite_release_channel(devpriv->ao_mite_chan);
642                 devpriv->ao_mite_chan = NULL;
643         }
644         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
645 #endif /*  PCIDMA */
646 }
647
648 #ifdef PCIDMA
649 static void ni_release_gpct_mite_channel(struct comedi_device *dev,
650                                          unsigned gpct_index)
651 {
652         struct ni_private *devpriv = dev->private;
653         unsigned long flags;
654
655         BUG_ON(gpct_index >= NUM_GPCT);
656         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
657         if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
658                 struct mite_channel *mite_chan =
659                     devpriv->counter_dev->counters[gpct_index].mite_chan;
660
661                 ni_set_gpct_dma_channel(dev, gpct_index, -1);
662                 ni_tio_set_mite_channel(&devpriv->
663                                         counter_dev->counters[gpct_index],
664                                         NULL);
665                 mite_release_channel(mite_chan);
666         }
667         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
668 }
669 #endif /*  PCIDMA */
670
671 static void ni_release_cdo_mite_channel(struct comedi_device *dev)
672 {
673 #ifdef PCIDMA
674         struct ni_private *devpriv = dev->private;
675         unsigned long flags;
676
677         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
678         if (devpriv->cdo_mite_chan) {
679                 ni_set_cdo_dma_channel(dev, -1);
680                 mite_release_channel(devpriv->cdo_mite_chan);
681                 devpriv->cdo_mite_chan = NULL;
682         }
683         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
684 #endif /*  PCIDMA */
685 }
686
687 /* e-series boards use the second irq signals to generate dma requests for their counters */
688 #ifdef PCIDMA
689 static void ni_e_series_enable_second_irq(struct comedi_device *dev,
690                                           unsigned gpct_index, short enable)
691 {
692         const struct ni_board_struct *board = comedi_board(dev);
693         struct ni_private *devpriv = dev->private;
694
695         if (board->reg_type & ni_reg_m_series_mask)
696                 return;
697         switch (gpct_index) {
698         case 0:
699                 if (enable) {
700                         devpriv->stc_writew(dev, G0_Gate_Second_Irq_Enable,
701                                             Second_IRQ_A_Enable_Register);
702                 } else {
703                         devpriv->stc_writew(dev, 0,
704                                             Second_IRQ_A_Enable_Register);
705                 }
706                 break;
707         case 1:
708                 if (enable) {
709                         devpriv->stc_writew(dev, G1_Gate_Second_Irq_Enable,
710                                             Second_IRQ_B_Enable_Register);
711                 } else {
712                         devpriv->stc_writew(dev, 0,
713                                             Second_IRQ_B_Enable_Register);
714                 }
715                 break;
716         default:
717                 BUG();
718                 break;
719         }
720 }
721 #endif /*  PCIDMA */
722
723 static void ni_clear_ai_fifo(struct comedi_device *dev)
724 {
725         const struct ni_board_struct *board = comedi_board(dev);
726         struct ni_private *devpriv = dev->private;
727
728         if (board->reg_type == ni_reg_6143) {
729                 /*  Flush the 6143 data FIFO */
730                 ni_writel(0x10, AIFIFO_Control_6143);   /*  Flush fifo */
731                 ni_writel(0x00, AIFIFO_Control_6143);   /*  Flush fifo */
732                 while (ni_readl(AIFIFO_Status_6143) & 0x10) ;   /*  Wait for complete */
733         } else {
734                 devpriv->stc_writew(dev, 1, ADC_FIFO_Clear);
735                 if (board->reg_type == ni_reg_625x) {
736                         ni_writeb(0, M_Offset_Static_AI_Control(0));
737                         ni_writeb(1, M_Offset_Static_AI_Control(0));
738 #if 0
739                         /* the NI example code does 3 convert pulses for 625x boards,
740                            but that appears to be wrong in practice. */
741                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
742                                             AI_Command_1_Register);
743                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
744                                             AI_Command_1_Register);
745                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
746                                             AI_Command_1_Register);
747 #endif
748                 }
749         }
750 }
751
752 static void win_out2(struct comedi_device *dev, uint32_t data, int reg)
753 {
754         struct ni_private *devpriv = dev->private;
755
756         devpriv->stc_writew(dev, data >> 16, reg);
757         devpriv->stc_writew(dev, data & 0xffff, reg + 1);
758 }
759
760 static uint32_t win_in2(struct comedi_device *dev, int reg)
761 {
762         struct ni_private *devpriv = dev->private;
763         uint32_t bits;
764
765         bits = devpriv->stc_readw(dev, reg) << 16;
766         bits |= devpriv->stc_readw(dev, reg + 1);
767         return bits;
768 }
769
770 #define ao_win_out(data, addr) ni_ao_win_outw(dev, data, addr)
771 static inline void ni_ao_win_outw(struct comedi_device *dev, uint16_t data,
772                                   int addr)
773 {
774         struct ni_private *devpriv = dev->private;
775         unsigned long flags;
776
777         spin_lock_irqsave(&devpriv->window_lock, flags);
778         ni_writew(addr, AO_Window_Address_611x);
779         ni_writew(data, AO_Window_Data_611x);
780         spin_unlock_irqrestore(&devpriv->window_lock, flags);
781 }
782
783 static inline void ni_ao_win_outl(struct comedi_device *dev, uint32_t data,
784                                   int addr)
785 {
786         struct ni_private *devpriv = dev->private;
787         unsigned long flags;
788
789         spin_lock_irqsave(&devpriv->window_lock, flags);
790         ni_writew(addr, AO_Window_Address_611x);
791         ni_writel(data, AO_Window_Data_611x);
792         spin_unlock_irqrestore(&devpriv->window_lock, flags);
793 }
794
795 static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
796 {
797         struct ni_private *devpriv = dev->private;
798         unsigned long flags;
799         unsigned short data;
800
801         spin_lock_irqsave(&devpriv->window_lock, flags);
802         ni_writew(addr, AO_Window_Address_611x);
803         data = ni_readw(AO_Window_Data_611x);
804         spin_unlock_irqrestore(&devpriv->window_lock, flags);
805         return data;
806 }
807
808 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
809 * share registers (such as Interrupt_A_Register) without interfering with
810 * each other.
811 *
812 * NOTE: the switch/case statements are optimized out for a constant argument
813 * so this is actually quite fast---  If you must wrap another function around this
814 * make it inline to avoid a large speed penalty.
815 *
816 * value should only be 1 or 0.
817 */
818 static inline void ni_set_bits(struct comedi_device *dev, int reg,
819                                unsigned bits, unsigned value)
820 {
821         unsigned bit_values;
822
823         if (value)
824                 bit_values = bits;
825         else
826                 bit_values = 0;
827         ni_set_bitfield(dev, reg, bits, bit_values);
828 }
829
830 static irqreturn_t ni_E_interrupt(int irq, void *d)
831 {
832         struct comedi_device *dev = d;
833         struct ni_private *devpriv = dev->private;
834         unsigned short a_status;
835         unsigned short b_status;
836         unsigned int ai_mite_status = 0;
837         unsigned int ao_mite_status = 0;
838         unsigned long flags;
839 #ifdef PCIDMA
840         struct mite_struct *mite = devpriv->mite;
841 #endif
842
843         if (!dev->attached)
844                 return IRQ_NONE;
845         smp_mb();               /*  make sure dev->attached is checked before handler does anything else. */
846
847         /*  lock to avoid race with comedi_poll */
848         spin_lock_irqsave(&dev->spinlock, flags);
849         a_status = devpriv->stc_readw(dev, AI_Status_1_Register);
850         b_status = devpriv->stc_readw(dev, AO_Status_1_Register);
851 #ifdef PCIDMA
852         if (mite) {
853                 unsigned long flags_too;
854
855                 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
856                 if (devpriv->ai_mite_chan) {
857                         ai_mite_status = mite_get_status(devpriv->ai_mite_chan);
858                         if (ai_mite_status & CHSR_LINKC)
859                                 writel(CHOR_CLRLC,
860                                        devpriv->mite->mite_io_addr +
861                                        MITE_CHOR(devpriv->
862                                                  ai_mite_chan->channel));
863                 }
864                 if (devpriv->ao_mite_chan) {
865                         ao_mite_status = mite_get_status(devpriv->ao_mite_chan);
866                         if (ao_mite_status & CHSR_LINKC)
867                                 writel(CHOR_CLRLC,
868                                        mite->mite_io_addr +
869                                        MITE_CHOR(devpriv->
870                                                  ao_mite_chan->channel));
871                 }
872                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
873         }
874 #endif
875         ack_a_interrupt(dev, a_status);
876         ack_b_interrupt(dev, b_status);
877         if ((a_status & Interrupt_A_St) || (ai_mite_status & CHSR_INT))
878                 handle_a_interrupt(dev, a_status, ai_mite_status);
879         if ((b_status & Interrupt_B_St) || (ao_mite_status & CHSR_INT))
880                 handle_b_interrupt(dev, b_status, ao_mite_status);
881         handle_gpct_interrupt(dev, 0);
882         handle_gpct_interrupt(dev, 1);
883         handle_cdio_interrupt(dev);
884
885         spin_unlock_irqrestore(&dev->spinlock, flags);
886         return IRQ_HANDLED;
887 }
888
889 #ifdef PCIDMA
890 static void ni_sync_ai_dma(struct comedi_device *dev)
891 {
892         struct ni_private *devpriv = dev->private;
893         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
894         unsigned long flags;
895
896         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
897         if (devpriv->ai_mite_chan)
898                 mite_sync_input_dma(devpriv->ai_mite_chan, s->async);
899         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
900 }
901
902 static void mite_handle_b_linkc(struct mite_struct *mite,
903                                 struct comedi_device *dev)
904 {
905         struct ni_private *devpriv = dev->private;
906         struct comedi_subdevice *s = &dev->subdevices[NI_AO_SUBDEV];
907         unsigned long flags;
908
909         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
910         if (devpriv->ao_mite_chan) {
911                 mite_sync_output_dma(devpriv->ao_mite_chan, s->async);
912         }
913         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
914 }
915
916 static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
917 {
918         struct ni_private *devpriv = dev->private;
919         static const int timeout = 10000;
920         int i;
921         for (i = 0; i < timeout; i++) {
922                 unsigned short b_status;
923
924                 b_status = devpriv->stc_readw(dev, AO_Status_1_Register);
925                 if (b_status & AO_FIFO_Half_Full_St)
926                         break;
927                 /* if we poll too often, the pci bus activity seems
928                    to slow the dma transfer down */
929                 udelay(10);
930         }
931         if (i == timeout) {
932                 comedi_error(dev, "timed out waiting for dma load");
933                 return -EPIPE;
934         }
935         return 0;
936 }
937
938 #endif /* PCIDMA */
939 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
940 {
941         struct ni_private *devpriv = dev->private;
942
943         if (devpriv->aimode == AIMODE_SCAN) {
944 #ifdef PCIDMA
945                 static const int timeout = 10;
946                 int i;
947
948                 for (i = 0; i < timeout; i++) {
949                         ni_sync_ai_dma(dev);
950                         if ((s->async->events & COMEDI_CB_EOS))
951                                 break;
952                         udelay(1);
953                 }
954 #else
955                 ni_handle_fifo_dregs(dev);
956                 s->async->events |= COMEDI_CB_EOS;
957 #endif
958         }
959         /* handle special case of single scan using AI_End_On_End_Of_Scan */
960         if ((devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)) {
961                 shutdown_ai_command(dev);
962         }
963 }
964
965 static void shutdown_ai_command(struct comedi_device *dev)
966 {
967         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
968
969 #ifdef PCIDMA
970         ni_ai_drain_dma(dev);
971 #endif
972         ni_handle_fifo_dregs(dev);
973         get_last_sample_611x(dev);
974         get_last_sample_6143(dev);
975
976         s->async->events |= COMEDI_CB_EOA;
977 }
978
979 static void ni_event(struct comedi_device *dev, struct comedi_subdevice *s)
980 {
981         if (s->
982             async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW |
983                              COMEDI_CB_EOA)) {
984                 switch (s->index) {
985                 case NI_AI_SUBDEV:
986                         ni_ai_reset(dev, s);
987                         break;
988                 case NI_AO_SUBDEV:
989                         ni_ao_reset(dev, s);
990                         break;
991                 case NI_GPCT0_SUBDEV:
992                 case NI_GPCT1_SUBDEV:
993                         ni_gpct_cancel(dev, s);
994                         break;
995                 case NI_DIO_SUBDEV:
996                         ni_cdio_cancel(dev, s);
997                         break;
998                 default:
999                         break;
1000                 }
1001         }
1002         comedi_event(dev, s);
1003 }
1004
1005 static void handle_gpct_interrupt(struct comedi_device *dev,
1006                                   unsigned short counter_index)
1007 {
1008 #ifdef PCIDMA
1009         struct ni_private *devpriv = dev->private;
1010         struct comedi_subdevice *s;
1011
1012         s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1013
1014         ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1015                                 s);
1016         if (s->async->events)
1017                 ni_event(dev, s);
1018 #endif
1019 }
1020
1021 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1022 {
1023         struct ni_private *devpriv = dev->private;
1024         unsigned short ack = 0;
1025
1026         if (a_status & AI_SC_TC_St) {
1027                 ack |= AI_SC_TC_Interrupt_Ack;
1028         }
1029         if (a_status & AI_START1_St) {
1030                 ack |= AI_START1_Interrupt_Ack;
1031         }
1032         if (a_status & AI_START_St) {
1033                 ack |= AI_START_Interrupt_Ack;
1034         }
1035         if (a_status & AI_STOP_St) {
1036                 /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
1037                 ack |= AI_STOP_Interrupt_Ack /*| AI_START_Interrupt_Ack */ ;
1038         }
1039         if (ack)
1040                 devpriv->stc_writew(dev, ack, Interrupt_A_Ack_Register);
1041 }
1042
1043 static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
1044                                unsigned ai_mite_status)
1045 {
1046         struct ni_private *devpriv = dev->private;
1047         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1048
1049         /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1050         if (s->type == COMEDI_SUBD_UNUSED)
1051                 return;
1052
1053 #ifdef DEBUG_INTERRUPT
1054         printk
1055             ("ni_mio_common: interrupt: a_status=%04x ai_mite_status=%08x\n",
1056              status, ai_mite_status);
1057         ni_mio_print_status_a(status);
1058 #endif
1059 #ifdef PCIDMA
1060         if (ai_mite_status & CHSR_LINKC) {
1061                 ni_sync_ai_dma(dev);
1062         }
1063
1064         if (ai_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1065                                CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1066                                CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1067                 printk
1068                     ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
1069                      ai_mite_status);
1070                 /* mite_print_chsr(ai_mite_status); */
1071                 s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1072                 /* disable_irq(dev->irq); */
1073         }
1074 #endif
1075
1076         /* test for all uncommon interrupt events at the same time */
1077         if (status & (AI_Overrun_St | AI_Overflow_St | AI_SC_TC_Error_St |
1078                       AI_SC_TC_St | AI_START1_St)) {
1079                 if (status == 0xffff) {
1080                         printk
1081                             ("ni_mio_common: a_status=0xffff.  Card removed?\n");
1082                         /* we probably aren't even running a command now,
1083                          * so it's a good idea to be careful. */
1084                         if (comedi_is_subdevice_running(s)) {
1085                                 s->async->events |=
1086                                     COMEDI_CB_ERROR | COMEDI_CB_EOA;
1087                                 ni_event(dev, s);
1088                         }
1089                         return;
1090                 }
1091                 if (status & (AI_Overrun_St | AI_Overflow_St |
1092                               AI_SC_TC_Error_St)) {
1093                         printk("ni_mio_common: ai error a_status=%04x\n",
1094                                status);
1095                         ni_mio_print_status_a(status);
1096
1097                         shutdown_ai_command(dev);
1098
1099                         s->async->events |= COMEDI_CB_ERROR;
1100                         if (status & (AI_Overrun_St | AI_Overflow_St))
1101                                 s->async->events |= COMEDI_CB_OVERFLOW;
1102
1103                         ni_event(dev, s);
1104
1105                         return;
1106                 }
1107                 if (status & AI_SC_TC_St) {
1108 #ifdef DEBUG_INTERRUPT
1109                         printk("ni_mio_common: SC_TC interrupt\n");
1110 #endif
1111                         if (!devpriv->ai_continuous) {
1112                                 shutdown_ai_command(dev);
1113                         }
1114                 }
1115         }
1116 #ifndef PCIDMA
1117         if (status & AI_FIFO_Half_Full_St) {
1118                 int i;
1119                 static const int timeout = 10;
1120                 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1121                  *fail to get the fifo less than half full, so loop to be sure.*/
1122                 for (i = 0; i < timeout; ++i) {
1123                         ni_handle_fifo_half_full(dev);
1124                         if ((devpriv->stc_readw(dev,
1125                                                 AI_Status_1_Register) &
1126                              AI_FIFO_Half_Full_St) == 0)
1127                                 break;
1128                 }
1129         }
1130 #endif /*  !PCIDMA */
1131
1132         if ((status & AI_STOP_St)) {
1133                 ni_handle_eos(dev, s);
1134         }
1135
1136         ni_event(dev, s);
1137
1138 #ifdef DEBUG_INTERRUPT
1139         status = devpriv->stc_readw(dev, AI_Status_1_Register);
1140         if (status & Interrupt_A_St) {
1141                 printk
1142                     ("handle_a_interrupt: didn't clear interrupt? status=0x%x\n",
1143                      status);
1144         }
1145 #endif
1146 }
1147
1148 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1149 {
1150         struct ni_private *devpriv = dev->private;
1151         unsigned short ack = 0;
1152
1153         if (b_status & AO_BC_TC_St) {
1154                 ack |= AO_BC_TC_Interrupt_Ack;
1155         }
1156         if (b_status & AO_Overrun_St) {
1157                 ack |= AO_Error_Interrupt_Ack;
1158         }
1159         if (b_status & AO_START_St) {
1160                 ack |= AO_START_Interrupt_Ack;
1161         }
1162         if (b_status & AO_START1_St) {
1163                 ack |= AO_START1_Interrupt_Ack;
1164         }
1165         if (b_status & AO_UC_TC_St) {
1166                 ack |= AO_UC_TC_Interrupt_Ack;
1167         }
1168         if (b_status & AO_UI2_TC_St) {
1169                 ack |= AO_UI2_TC_Interrupt_Ack;
1170         }
1171         if (b_status & AO_UPDATE_St) {
1172                 ack |= AO_UPDATE_Interrupt_Ack;
1173         }
1174         if (ack)
1175                 devpriv->stc_writew(dev, ack, Interrupt_B_Ack_Register);
1176 }
1177
1178 static void handle_b_interrupt(struct comedi_device *dev,
1179                                unsigned short b_status, unsigned ao_mite_status)
1180 {
1181         struct ni_private *devpriv = dev->private;
1182         struct comedi_subdevice *s = &dev->subdevices[NI_AO_SUBDEV];
1183         /* unsigned short ack=0; */
1184
1185 #ifdef DEBUG_INTERRUPT
1186         printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
1187                b_status, ao_mite_status);
1188         ni_mio_print_status_b(b_status);
1189 #endif
1190
1191 #ifdef PCIDMA
1192         /* Currently, mite.c requires us to handle LINKC */
1193         if (ao_mite_status & CHSR_LINKC) {
1194                 mite_handle_b_linkc(devpriv->mite, dev);
1195         }
1196
1197         if (ao_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1198                                CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1199                                CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1200                 printk
1201                     ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
1202                      ao_mite_status);
1203                 /* mite_print_chsr(ao_mite_status); */
1204                 s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
1205         }
1206 #endif
1207
1208         if (b_status == 0xffff)
1209                 return;
1210         if (b_status & AO_Overrun_St) {
1211                 printk
1212                     ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
1213                      b_status, devpriv->stc_readw(dev, AO_Status_2_Register));
1214                 s->async->events |= COMEDI_CB_OVERFLOW;
1215         }
1216
1217         if (b_status & AO_BC_TC_St) {
1218                 MDPRINTK
1219                     ("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",
1220                      b_status, devpriv->stc_readw(dev, AO_Status_2_Register));
1221                 s->async->events |= COMEDI_CB_EOA;
1222         }
1223 #ifndef PCIDMA
1224         if (b_status & AO_FIFO_Request_St) {
1225                 int ret;
1226
1227                 ret = ni_ao_fifo_half_empty(dev, s);
1228                 if (!ret) {
1229                         printk("ni_mio_common: AO buffer underrun\n");
1230                         ni_set_bits(dev, Interrupt_B_Enable_Register,
1231                                     AO_FIFO_Interrupt_Enable |
1232                                     AO_Error_Interrupt_Enable, 0);
1233                         s->async->events |= COMEDI_CB_OVERFLOW;
1234                 }
1235         }
1236 #endif
1237
1238         ni_event(dev, s);
1239 }
1240
1241 #ifdef DEBUG_STATUS_A
1242 static const char *const status_a_strings[] = {
1243         "passthru0", "fifo", "G0_gate", "G0_TC",
1244         "stop", "start", "sc_tc", "start1",
1245         "start2", "sc_tc_error", "overflow", "overrun",
1246         "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_a"
1247 };
1248
1249 static void ni_mio_print_status_a(int status)
1250 {
1251         int i;
1252
1253         printk("A status:");
1254         for (i = 15; i >= 0; i--) {
1255                 if (status & (1 << i)) {
1256                         printk(" %s", status_a_strings[i]);
1257                 }
1258         }
1259         printk("\n");
1260 }
1261 #endif
1262
1263 #ifdef DEBUG_STATUS_B
1264 static const char *const status_b_strings[] = {
1265         "passthru1", "fifo", "G1_gate", "G1_TC",
1266         "UI2_TC", "UPDATE", "UC_TC", "BC_TC",
1267         "start1", "overrun", "start", "bc_tc_error",
1268         "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_b"
1269 };
1270
1271 static void ni_mio_print_status_b(int status)
1272 {
1273         int i;
1274
1275         printk("B status:");
1276         for (i = 15; i >= 0; i--) {
1277                 if (status & (1 << i)) {
1278                         printk(" %s", status_b_strings[i]);
1279                 }
1280         }
1281         printk("\n");
1282 }
1283 #endif
1284
1285 #ifndef PCIDMA
1286
1287 static void ni_ao_fifo_load(struct comedi_device *dev,
1288                             struct comedi_subdevice *s, int n)
1289 {
1290         const struct ni_board_struct *board = comedi_board(dev);
1291         struct comedi_async *async = s->async;
1292         struct comedi_cmd *cmd = &async->cmd;
1293         int chan;
1294         int i;
1295         unsigned short d;
1296         u32 packed_data;
1297         int range;
1298         int err = 1;
1299
1300         chan = async->cur_chan;
1301         for (i = 0; i < n; i++) {
1302                 err &= comedi_buf_get(async, &d);
1303                 if (err == 0)
1304                         break;
1305
1306                 range = CR_RANGE(cmd->chanlist[chan]);
1307
1308                 if (board->reg_type & ni_reg_6xxx_mask) {
1309                         packed_data = d & 0xffff;
1310                         /* 6711 only has 16 bit wide ao fifo */
1311                         if (board->reg_type != ni_reg_6711) {
1312                                 err &= comedi_buf_get(async, &d);
1313                                 if (err == 0)
1314                                         break;
1315                                 chan++;
1316                                 i++;
1317                                 packed_data |= (d << 16) & 0xffff0000;
1318                         }
1319                         ni_writel(packed_data, DAC_FIFO_Data_611x);
1320                 } else {
1321                         ni_writew(d, DAC_FIFO_Data);
1322                 }
1323                 chan++;
1324                 chan %= cmd->chanlist_len;
1325         }
1326         async->cur_chan = chan;
1327         if (err == 0) {
1328                 async->events |= COMEDI_CB_OVERFLOW;
1329         }
1330 }
1331
1332 /*
1333  *  There's a small problem if the FIFO gets really low and we
1334  *  don't have the data to fill it.  Basically, if after we fill
1335  *  the FIFO with all the data available, the FIFO is _still_
1336  *  less than half full, we never clear the interrupt.  If the
1337  *  IRQ is in edge mode, we never get another interrupt, because
1338  *  this one wasn't cleared.  If in level mode, we get flooded
1339  *  with interrupts that we can't fulfill, because nothing ever
1340  *  gets put into the buffer.
1341  *
1342  *  This kind of situation is recoverable, but it is easier to
1343  *  just pretend we had a FIFO underrun, since there is a good
1344  *  chance it will happen anyway.  This is _not_ the case for
1345  *  RT code, as RT code might purposely be running close to the
1346  *  metal.  Needs to be fixed eventually.
1347  */
1348 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
1349                                  struct comedi_subdevice *s)
1350 {
1351         const struct ni_board_struct *board = comedi_board(dev);
1352         int n;
1353
1354         n = comedi_buf_read_n_available(s->async);
1355         if (n == 0) {
1356                 s->async->events |= COMEDI_CB_OVERFLOW;
1357                 return 0;
1358         }
1359
1360         n /= sizeof(short);
1361         if (n > board->ao_fifo_depth / 2)
1362                 n = board->ao_fifo_depth / 2;
1363
1364         ni_ao_fifo_load(dev, s, n);
1365
1366         s->async->events |= COMEDI_CB_BLOCK;
1367
1368         return 1;
1369 }
1370
1371 static int ni_ao_prep_fifo(struct comedi_device *dev,
1372                            struct comedi_subdevice *s)
1373 {
1374         const struct ni_board_struct *board = comedi_board(dev);
1375         struct ni_private *devpriv = dev->private;
1376         int n;
1377
1378         /* reset fifo */
1379         devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
1380         if (board->reg_type & ni_reg_6xxx_mask)
1381                 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
1382
1383         /* load some data */
1384         n = comedi_buf_read_n_available(s->async);
1385         if (n == 0)
1386                 return 0;
1387
1388         n /= sizeof(short);
1389         if (n > board->ao_fifo_depth)
1390                 n = board->ao_fifo_depth;
1391
1392         ni_ao_fifo_load(dev, s, n);
1393
1394         return n;
1395 }
1396
1397 static void ni_ai_fifo_read(struct comedi_device *dev,
1398                             struct comedi_subdevice *s, int n)
1399 {
1400         const struct ni_board_struct *board = comedi_board(dev);
1401         struct ni_private *devpriv = dev->private;
1402         struct comedi_async *async = s->async;
1403         int i;
1404
1405         if (board->reg_type == ni_reg_611x) {
1406                 unsigned short data[2];
1407                 u32 dl;
1408
1409                 for (i = 0; i < n / 2; i++) {
1410                         dl = ni_readl(ADC_FIFO_Data_611x);
1411                         /* This may get the hi/lo data in the wrong order */
1412                         data[0] = (dl >> 16) & 0xffff;
1413                         data[1] = dl & 0xffff;
1414                         cfc_write_array_to_buffer(s, data, sizeof(data));
1415                 }
1416                 /* Check if there's a single sample stuck in the FIFO */
1417                 if (n % 2) {
1418                         dl = ni_readl(ADC_FIFO_Data_611x);
1419                         data[0] = dl & 0xffff;
1420                         cfc_write_to_buffer(s, data[0]);
1421                 }
1422         } else if (board->reg_type == ni_reg_6143) {
1423                 unsigned short data[2];
1424                 u32 dl;
1425
1426                 /*  This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1427                 for (i = 0; i < n / 2; i++) {
1428                         dl = ni_readl(AIFIFO_Data_6143);
1429
1430                         data[0] = (dl >> 16) & 0xffff;
1431                         data[1] = dl & 0xffff;
1432                         cfc_write_array_to_buffer(s, data, sizeof(data));
1433                 }
1434                 if (n % 2) {
1435                         /* Assume there is a single sample stuck in the FIFO */
1436                         ni_writel(0x01, AIFIFO_Control_6143);   /*  Get stranded sample into FIFO */
1437                         dl = ni_readl(AIFIFO_Data_6143);
1438                         data[0] = (dl >> 16) & 0xffff;
1439                         cfc_write_to_buffer(s, data[0]);
1440                 }
1441         } else {
1442                 if (n > sizeof(devpriv->ai_fifo_buffer) /
1443                     sizeof(devpriv->ai_fifo_buffer[0])) {
1444                         comedi_error(dev, "bug! ai_fifo_buffer too small");
1445                         async->events |= COMEDI_CB_ERROR;
1446                         return;
1447                 }
1448                 for (i = 0; i < n; i++) {
1449                         devpriv->ai_fifo_buffer[i] =
1450                             ni_readw(ADC_FIFO_Data_Register);
1451                 }
1452                 cfc_write_array_to_buffer(s, devpriv->ai_fifo_buffer,
1453                                           n *
1454                                           sizeof(devpriv->ai_fifo_buffer[0]));
1455         }
1456 }
1457
1458 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1459 {
1460         const struct ni_board_struct *board = comedi_board(dev);
1461         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1462         int n;
1463
1464         n = board->ai_fifo_depth / 2;
1465
1466         ni_ai_fifo_read(dev, s, n);
1467 }
1468 #endif
1469
1470 #ifdef PCIDMA
1471 static int ni_ai_drain_dma(struct comedi_device *dev)
1472 {
1473         struct ni_private *devpriv = dev->private;
1474         int i;
1475         static const int timeout = 10000;
1476         unsigned long flags;
1477         int retval = 0;
1478
1479         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1480         if (devpriv->ai_mite_chan) {
1481                 for (i = 0; i < timeout; i++) {
1482                         if ((devpriv->stc_readw(dev,
1483                                                 AI_Status_1_Register) &
1484                              AI_FIFO_Empty_St)
1485                             && mite_bytes_in_transit(devpriv->ai_mite_chan) ==
1486                             0)
1487                                 break;
1488                         udelay(5);
1489                 }
1490                 if (i == timeout) {
1491                         printk("ni_mio_common: wait for dma drain timed out\n");
1492                         printk
1493                             ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1494                              mite_bytes_in_transit(devpriv->ai_mite_chan),
1495                              devpriv->stc_readw(dev, AI_Status_1_Register));
1496                         retval = -1;
1497                 }
1498         }
1499         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1500
1501         ni_sync_ai_dma(dev);
1502
1503         return retval;
1504 }
1505 #endif
1506 /*
1507    Empties the AI fifo
1508 */
1509 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1510 {
1511         const struct ni_board_struct *board = comedi_board(dev);
1512         struct ni_private *devpriv = dev->private;
1513         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1514         unsigned short data[2];
1515         u32 dl;
1516         unsigned short fifo_empty;
1517         int i;
1518
1519         if (board->reg_type == ni_reg_611x) {
1520                 while ((devpriv->stc_readw(dev,
1521                                            AI_Status_1_Register) &
1522                         AI_FIFO_Empty_St) == 0) {
1523                         dl = ni_readl(ADC_FIFO_Data_611x);
1524
1525                         /* This may get the hi/lo data in the wrong order */
1526                         data[0] = (dl >> 16);
1527                         data[1] = (dl & 0xffff);
1528                         cfc_write_array_to_buffer(s, data, sizeof(data));
1529                 }
1530         } else if (board->reg_type == ni_reg_6143) {
1531                 i = 0;
1532                 while (ni_readl(AIFIFO_Status_6143) & 0x04) {
1533                         dl = ni_readl(AIFIFO_Data_6143);
1534
1535                         /* This may get the hi/lo data in the wrong order */
1536                         data[0] = (dl >> 16);
1537                         data[1] = (dl & 0xffff);
1538                         cfc_write_array_to_buffer(s, data, sizeof(data));
1539                         i += 2;
1540                 }
1541                 /*  Check if stranded sample is present */
1542                 if (ni_readl(AIFIFO_Status_6143) & 0x01) {
1543                         ni_writel(0x01, AIFIFO_Control_6143);   /*  Get stranded sample into FIFO */
1544                         dl = ni_readl(AIFIFO_Data_6143);
1545                         data[0] = (dl >> 16) & 0xffff;
1546                         cfc_write_to_buffer(s, data[0]);
1547                 }
1548
1549         } else {
1550                 fifo_empty =
1551                     devpriv->stc_readw(dev,
1552                                        AI_Status_1_Register) & AI_FIFO_Empty_St;
1553                 while (fifo_empty == 0) {
1554                         for (i = 0;
1555                              i <
1556                              sizeof(devpriv->ai_fifo_buffer) /
1557                              sizeof(devpriv->ai_fifo_buffer[0]); i++) {
1558                                 fifo_empty =
1559                                     devpriv->stc_readw(dev,
1560                                                        AI_Status_1_Register) &
1561                                     AI_FIFO_Empty_St;
1562                                 if (fifo_empty)
1563                                         break;
1564                                 devpriv->ai_fifo_buffer[i] =
1565                                     ni_readw(ADC_FIFO_Data_Register);
1566                         }
1567                         cfc_write_array_to_buffer(s, devpriv->ai_fifo_buffer,
1568                                                   i *
1569                                                   sizeof(devpriv->
1570                                                          ai_fifo_buffer[0]));
1571                 }
1572         }
1573 }
1574
1575 static void get_last_sample_611x(struct comedi_device *dev)
1576 {
1577         const struct ni_board_struct *board = comedi_board(dev);
1578         struct ni_private *devpriv __maybe_unused = dev->private;
1579         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1580         unsigned short data;
1581         u32 dl;
1582
1583         if (board->reg_type != ni_reg_611x)
1584                 return;
1585
1586         /* Check if there's a single sample stuck in the FIFO */
1587         if (ni_readb(XXX_Status) & 0x80) {
1588                 dl = ni_readl(ADC_FIFO_Data_611x);
1589                 data = (dl & 0xffff);
1590                 cfc_write_to_buffer(s, data);
1591         }
1592 }
1593
1594 static void get_last_sample_6143(struct comedi_device *dev)
1595 {
1596         const struct ni_board_struct *board = comedi_board(dev);
1597         struct ni_private *devpriv __maybe_unused = dev->private;
1598         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1599         unsigned short data;
1600         u32 dl;
1601
1602         if (board->reg_type != ni_reg_6143)
1603                 return;
1604
1605         /* Check if there's a single sample stuck in the FIFO */
1606         if (ni_readl(AIFIFO_Status_6143) & 0x01) {
1607                 ni_writel(0x01, AIFIFO_Control_6143);   /*  Get stranded sample into FIFO */
1608                 dl = ni_readl(AIFIFO_Data_6143);
1609
1610                 /* This may get the hi/lo data in the wrong order */
1611                 data = (dl >> 16) & 0xffff;
1612                 cfc_write_to_buffer(s, data);
1613         }
1614 }
1615
1616 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1617                         void *data, unsigned int num_bytes,
1618                         unsigned int chan_index)
1619 {
1620         struct ni_private *devpriv = dev->private;
1621         struct comedi_async *async = s->async;
1622         unsigned int i;
1623         unsigned int length = num_bytes / bytes_per_sample(s);
1624         unsigned short *array = data;
1625         unsigned int *larray = data;
1626
1627         for (i = 0; i < length; i++) {
1628 #ifdef PCIDMA
1629                 if (s->subdev_flags & SDF_LSAMPL)
1630                         larray[i] = le32_to_cpu(larray[i]);
1631                 else
1632                         array[i] = le16_to_cpu(array[i]);
1633 #endif
1634                 if (s->subdev_flags & SDF_LSAMPL)
1635                         larray[i] += devpriv->ai_offset[chan_index];
1636                 else
1637                         array[i] += devpriv->ai_offset[chan_index];
1638                 chan_index++;
1639                 chan_index %= async->cmd.chanlist_len;
1640         }
1641 }
1642
1643 #ifdef PCIDMA
1644
1645 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1646 {
1647         const struct ni_board_struct *board = comedi_board(dev);
1648         struct ni_private *devpriv = dev->private;
1649         struct comedi_subdevice *s = &dev->subdevices[NI_AI_SUBDEV];
1650         int retval;
1651         unsigned long flags;
1652
1653         retval = ni_request_ai_mite_channel(dev);
1654         if (retval)
1655                 return retval;
1656 /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
1657
1658         /* write alloc the entire buffer */
1659         comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1660
1661         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1662         if (devpriv->ai_mite_chan == NULL) {
1663                 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1664                 return -EIO;
1665         }
1666
1667         switch (board->reg_type) {
1668         case ni_reg_611x:
1669         case ni_reg_6143:
1670                 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1671                 break;
1672         case ni_reg_628x:
1673                 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1674                 break;
1675         default:
1676                 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1677                 break;
1678         }
1679         /*start the MITE */
1680         mite_dma_arm(devpriv->ai_mite_chan);
1681         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1682
1683         return 0;
1684 }
1685
1686 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1687 {
1688         const struct ni_board_struct *board = comedi_board(dev);
1689         struct ni_private *devpriv = dev->private;
1690         struct comedi_subdevice *s = &dev->subdevices[NI_AO_SUBDEV];
1691         int retval;
1692         unsigned long flags;
1693
1694         retval = ni_request_ao_mite_channel(dev);
1695         if (retval)
1696                 return retval;
1697
1698         /* read alloc the entire buffer */
1699         comedi_buf_read_alloc(s->async, s->async->prealloc_bufsz);
1700
1701         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1702         if (devpriv->ao_mite_chan) {
1703                 if (board->reg_type & (ni_reg_611x | ni_reg_6713)) {
1704                         mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1705                 } else {
1706                         /* doing 32 instead of 16 bit wide transfers from memory
1707                            makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1708                         mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1709                 }
1710                 mite_dma_arm(devpriv->ao_mite_chan);
1711         } else
1712                 retval = -EIO;
1713         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1714
1715         return retval;
1716 }
1717
1718 #endif /*  PCIDMA */
1719
1720 /*
1721    used for both cancel ioctl and board initialization
1722
1723    this is pretty harsh for a cancel, but it works...
1724  */
1725
1726 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1727 {
1728         const struct ni_board_struct *board = comedi_board(dev);
1729         struct ni_private *devpriv = dev->private;
1730
1731         ni_release_ai_mite_channel(dev);
1732         /* ai configuration */
1733         devpriv->stc_writew(dev, AI_Configuration_Start | AI_Reset,
1734                             Joint_Reset_Register);
1735
1736         ni_set_bits(dev, Interrupt_A_Enable_Register,
1737                     AI_SC_TC_Interrupt_Enable | AI_START1_Interrupt_Enable |
1738                     AI_START2_Interrupt_Enable | AI_START_Interrupt_Enable |
1739                     AI_STOP_Interrupt_Enable | AI_Error_Interrupt_Enable |
1740                     AI_FIFO_Interrupt_Enable, 0);
1741
1742         ni_clear_ai_fifo(dev);
1743
1744         if (board->reg_type != ni_reg_6143)
1745                 ni_writeb(0, Misc_Command);
1746
1747         devpriv->stc_writew(dev, AI_Disarm, AI_Command_1_Register);     /* reset pulses */
1748         devpriv->stc_writew(dev,
1749                             AI_Start_Stop | AI_Mode_1_Reserved
1750                             /*| AI_Trigger_Once */ ,
1751                             AI_Mode_1_Register);
1752         devpriv->stc_writew(dev, 0x0000, AI_Mode_2_Register);
1753         /* generate FIFO interrupts on non-empty */
1754         devpriv->stc_writew(dev, (0 << 6) | 0x0000, AI_Mode_3_Register);
1755         if (board->reg_type == ni_reg_611x) {
1756                 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1757                                     AI_SOC_Polarity |
1758                                     AI_LOCALMUX_CLK_Pulse_Width,
1759                                     AI_Personal_Register);
1760                 devpriv->stc_writew(dev,
1761                                     AI_SCAN_IN_PROG_Output_Select(3) |
1762                                     AI_EXTMUX_CLK_Output_Select(0) |
1763                                     AI_LOCALMUX_CLK_Output_Select(2) |
1764                                     AI_SC_TC_Output_Select(3) |
1765                                     AI_CONVERT_Output_Select
1766                                     (AI_CONVERT_Output_Enable_High),
1767                                     AI_Output_Control_Register);
1768         } else if (board->reg_type == ni_reg_6143) {
1769                 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1770                                     AI_SOC_Polarity |
1771                                     AI_LOCALMUX_CLK_Pulse_Width,
1772                                     AI_Personal_Register);
1773                 devpriv->stc_writew(dev,
1774                                     AI_SCAN_IN_PROG_Output_Select(3) |
1775                                     AI_EXTMUX_CLK_Output_Select(0) |
1776                                     AI_LOCALMUX_CLK_Output_Select(2) |
1777                                     AI_SC_TC_Output_Select(3) |
1778                                     AI_CONVERT_Output_Select
1779                                     (AI_CONVERT_Output_Enable_Low),
1780                                     AI_Output_Control_Register);
1781         } else {
1782                 unsigned ai_output_control_bits;
1783                 devpriv->stc_writew(dev, AI_SHIFTIN_Pulse_Width |
1784                                     AI_SOC_Polarity |
1785                                     AI_CONVERT_Pulse_Width |
1786                                     AI_LOCALMUX_CLK_Pulse_Width,
1787                                     AI_Personal_Register);
1788                 ai_output_control_bits =
1789                     AI_SCAN_IN_PROG_Output_Select(3) |
1790                     AI_EXTMUX_CLK_Output_Select(0) |
1791                     AI_LOCALMUX_CLK_Output_Select(2) |
1792                     AI_SC_TC_Output_Select(3);
1793                 if (board->reg_type == ni_reg_622x)
1794                         ai_output_control_bits |=
1795                             AI_CONVERT_Output_Select
1796                             (AI_CONVERT_Output_Enable_High);
1797                 else
1798                         ai_output_control_bits |=
1799                             AI_CONVERT_Output_Select
1800                             (AI_CONVERT_Output_Enable_Low);
1801                 devpriv->stc_writew(dev, ai_output_control_bits,
1802                                     AI_Output_Control_Register);
1803         }
1804         /* the following registers should not be changed, because there
1805          * are no backup registers in devpriv.  If you want to change
1806          * any of these, add a backup register and other appropriate code:
1807          *      AI_Mode_1_Register
1808          *      AI_Mode_3_Register
1809          *      AI_Personal_Register
1810          *      AI_Output_Control_Register
1811          */
1812         devpriv->stc_writew(dev, AI_SC_TC_Error_Confirm | AI_START_Interrupt_Ack | AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack | AI_SC_TC_Interrupt_Ack | AI_Error_Interrupt_Ack | AI_STOP_Interrupt_Ack, Interrupt_A_Ack_Register);      /* clear interrupts */
1813
1814         devpriv->stc_writew(dev, AI_Configuration_End, Joint_Reset_Register);
1815
1816         return 0;
1817 }
1818
1819 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1820 {
1821         unsigned long flags;
1822         int count;
1823
1824         /*  lock to avoid race with interrupt handler */
1825         spin_lock_irqsave(&dev->spinlock, flags);
1826 #ifndef PCIDMA
1827         ni_handle_fifo_dregs(dev);
1828 #else
1829         ni_sync_ai_dma(dev);
1830 #endif
1831         count = s->async->buf_write_count - s->async->buf_read_count;
1832         spin_unlock_irqrestore(&dev->spinlock, flags);
1833
1834         return count;
1835 }
1836
1837 static int ni_ai_insn_read(struct comedi_device *dev,
1838                            struct comedi_subdevice *s, struct comedi_insn *insn,
1839                            unsigned int *data)
1840 {
1841         const struct ni_board_struct *board = comedi_board(dev);
1842         struct ni_private *devpriv = dev->private;
1843         int i, n;
1844         const unsigned int mask = (1 << board->adbits) - 1;
1845         unsigned signbits;
1846         unsigned short d;
1847         unsigned long dl;
1848
1849         ni_load_channelgain_list(dev, 1, &insn->chanspec);
1850
1851         ni_clear_ai_fifo(dev);
1852
1853         signbits = devpriv->ai_offset[0];
1854         if (board->reg_type == ni_reg_611x) {
1855                 for (n = 0; n < num_adc_stages_611x; n++) {
1856                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
1857                                             AI_Command_1_Register);
1858                         udelay(1);
1859                 }
1860                 for (n = 0; n < insn->n; n++) {
1861                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
1862                                             AI_Command_1_Register);
1863                         /* The 611x has screwy 32-bit FIFOs. */
1864                         d = 0;
1865                         for (i = 0; i < NI_TIMEOUT; i++) {
1866                                 if (ni_readb(XXX_Status) & 0x80) {
1867                                         d = (ni_readl(ADC_FIFO_Data_611x) >> 16)
1868                                             & 0xffff;
1869                                         break;
1870                                 }
1871                                 if (!(devpriv->stc_readw(dev,
1872                                                          AI_Status_1_Register) &
1873                                       AI_FIFO_Empty_St)) {
1874                                         d = ni_readl(ADC_FIFO_Data_611x) &
1875                                             0xffff;
1876                                         break;
1877                                 }
1878                         }
1879                         if (i == NI_TIMEOUT) {
1880                                 printk
1881                                     ("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1882                                 return -ETIME;
1883                         }
1884                         d += signbits;
1885                         data[n] = d;
1886                 }
1887         } else if (board->reg_type == ni_reg_6143) {
1888                 for (n = 0; n < insn->n; n++) {
1889                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
1890                                             AI_Command_1_Register);
1891
1892                         /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1893                         dl = 0;
1894                         for (i = 0; i < NI_TIMEOUT; i++) {
1895                                 if (ni_readl(AIFIFO_Status_6143) & 0x01) {
1896                                         ni_writel(0x01, AIFIFO_Control_6143);   /*  Get stranded sample into FIFO */
1897                                         dl = ni_readl(AIFIFO_Data_6143);
1898                                         break;
1899                                 }
1900                         }
1901                         if (i == NI_TIMEOUT) {
1902                                 printk
1903                                     ("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1904                                 return -ETIME;
1905                         }
1906                         data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1907                 }
1908         } else {
1909                 for (n = 0; n < insn->n; n++) {
1910                         devpriv->stc_writew(dev, AI_CONVERT_Pulse,
1911                                             AI_Command_1_Register);
1912                         for (i = 0; i < NI_TIMEOUT; i++) {
1913                                 if (!(devpriv->stc_readw(dev,
1914                                                          AI_Status_1_Register) &
1915                                       AI_FIFO_Empty_St))
1916                                         break;
1917                         }
1918                         if (i == NI_TIMEOUT) {
1919                                 printk
1920                                     ("ni_mio_common: timeout in ni_ai_insn_read\n");
1921                                 return -ETIME;
1922                         }
1923                         if (board->reg_type & ni_reg_m_series_mask) {
1924                                 data[n] =
1925                                     ni_readl(M_Offset_AI_FIFO_Data) & mask;
1926                         } else {
1927                                 d = ni_readw(ADC_FIFO_Data_Register);
1928                                 d += signbits;  /* subtle: needs to be short addition */
1929                                 data[n] = d;
1930                         }
1931                 }
1932         }
1933         return insn->n;
1934 }
1935
1936 static void ni_prime_channelgain_list(struct comedi_device *dev)
1937 {
1938         struct ni_private *devpriv = dev->private;
1939         int i;
1940
1941         devpriv->stc_writew(dev, AI_CONVERT_Pulse, AI_Command_1_Register);
1942         for (i = 0; i < NI_TIMEOUT; ++i) {
1943                 if (!(devpriv->stc_readw(dev,
1944                                          AI_Status_1_Register) &
1945                       AI_FIFO_Empty_St)) {
1946                         devpriv->stc_writew(dev, 1, ADC_FIFO_Clear);
1947                         return;
1948                 }
1949                 udelay(1);
1950         }
1951         printk("ni_mio_common: timeout loading channel/gain list\n");
1952 }
1953
1954 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1955                                               unsigned int n_chan,
1956                                               unsigned int *list)
1957 {
1958         const struct ni_board_struct *board = comedi_board(dev);
1959         struct ni_private *devpriv = dev->private;
1960         unsigned int chan, range, aref;
1961         unsigned int i;
1962         unsigned offset;
1963         unsigned int dither;
1964         unsigned range_code;
1965
1966         devpriv->stc_writew(dev, 1, Configuration_Memory_Clear);
1967
1968 /* offset = 1 << (board->adbits - 1); */
1969         if ((list[0] & CR_ALT_SOURCE)) {
1970                 unsigned bypass_bits;
1971                 chan = CR_CHAN(list[0]);
1972                 range = CR_RANGE(list[0]);
1973                 range_code = ni_gainlkup[board->gainlkup][range];
1974                 dither = ((list[0] & CR_ALT_FILTER) != 0);
1975                 bypass_bits = MSeries_AI_Bypass_Config_FIFO_Bit;
1976                 bypass_bits |= chan;
1977                 bypass_bits |=
1978                     (devpriv->ai_calib_source) &
1979                     (MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
1980                      MSeries_AI_Bypass_Cal_Sel_Neg_Mask |
1981                      MSeries_AI_Bypass_Mode_Mux_Mask |
1982                      MSeries_AO_Bypass_AO_Cal_Sel_Mask);
1983                 bypass_bits |= MSeries_AI_Bypass_Gain_Bits(range_code);
1984                 if (dither)
1985                         bypass_bits |= MSeries_AI_Bypass_Dither_Bit;
1986                 /*  don't use 2's complement encoding */
1987                 bypass_bits |= MSeries_AI_Bypass_Polarity_Bit;
1988                 ni_writel(bypass_bits, M_Offset_AI_Config_FIFO_Bypass);
1989         } else {
1990                 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass);
1991         }
1992         offset = 0;
1993         for (i = 0; i < n_chan; i++) {
1994                 unsigned config_bits = 0;
1995                 chan = CR_CHAN(list[i]);
1996                 aref = CR_AREF(list[i]);
1997                 range = CR_RANGE(list[i]);
1998                 dither = ((list[i] & CR_ALT_FILTER) != 0);
1999
2000                 range_code = ni_gainlkup[board->gainlkup][range];
2001                 devpriv->ai_offset[i] = offset;
2002                 switch (aref) {
2003                 case AREF_DIFF:
2004                         config_bits |=
2005                             MSeries_AI_Config_Channel_Type_Differential_Bits;
2006                         break;
2007                 case AREF_COMMON:
2008                         config_bits |=
2009                             MSeries_AI_Config_Channel_Type_Common_Ref_Bits;
2010                         break;
2011                 case AREF_GROUND:
2012                         config_bits |=
2013                             MSeries_AI_Config_Channel_Type_Ground_Ref_Bits;
2014                         break;
2015                 case AREF_OTHER:
2016                         break;
2017                 }
2018                 config_bits |= MSeries_AI_Config_Channel_Bits(chan);
2019                 config_bits |=
2020                     MSeries_AI_Config_Bank_Bits(board->reg_type, chan);
2021                 config_bits |= MSeries_AI_Config_Gain_Bits(range_code);
2022                 if (i == n_chan - 1)
2023                         config_bits |= MSeries_AI_Config_Last_Channel_Bit;
2024                 if (dither)
2025                         config_bits |= MSeries_AI_Config_Dither_Bit;
2026                 /*  don't use 2's complement encoding */
2027                 config_bits |= MSeries_AI_Config_Polarity_Bit;
2028                 ni_writew(config_bits, M_Offset_AI_Config_FIFO_Data);
2029         }
2030         ni_prime_channelgain_list(dev);
2031 }
2032
2033 /*
2034  * Notes on the 6110 and 6111:
2035  * These boards a slightly different than the rest of the series, since
2036  * they have multiple A/D converters.
2037  * From the driver side, the configuration memory is a
2038  * little different.
2039  * Configuration Memory Low:
2040  *   bits 15-9: same
2041  *   bit 8: unipolar/bipolar (should be 0 for bipolar)
2042  *   bits 0-3: gain.  This is 4 bits instead of 3 for the other boards
2043  *       1001 gain=0.1 (+/- 50)
2044  *       1010 0.2
2045  *       1011 0.1
2046  *       0001 1
2047  *       0010 2
2048  *       0011 5
2049  *       0100 10
2050  *       0101 20
2051  *       0110 50
2052  * Configuration Memory High:
2053  *   bits 12-14: Channel Type
2054  *       001 for differential
2055  *       000 for calibration
2056  *   bit 11: coupling  (this is not currently handled)
2057  *       1 AC coupling
2058  *       0 DC coupling
2059  *   bits 0-2: channel
2060  *       valid channels are 0-3
2061  */
2062 static void ni_load_channelgain_list(struct comedi_device *dev,
2063                                      unsigned int n_chan, unsigned int *list)
2064 {
2065         const struct ni_board_struct *board = comedi_board(dev);
2066         struct ni_private *devpriv = dev->private;
2067         unsigned int chan, range, aref;
2068         unsigned int i;
2069         unsigned int hi, lo;
2070         unsigned offset;
2071         unsigned int dither;
2072
2073         if (board->reg_type & ni_reg_m_series_mask) {
2074                 ni_m_series_load_channelgain_list(dev, n_chan, list);
2075                 return;
2076         }
2077         if (n_chan == 1 && (board->reg_type != ni_reg_611x)
2078             && (board->reg_type != ni_reg_6143)) {
2079                 if (devpriv->changain_state
2080                     && devpriv->changain_spec == list[0]) {
2081                         /*  ready to go. */
2082                         return;
2083                 }
2084                 devpriv->changain_state = 1;
2085                 devpriv->changain_spec = list[0];
2086         } else {
2087                 devpriv->changain_state = 0;
2088         }
2089
2090         devpriv->stc_writew(dev, 1, Configuration_Memory_Clear);
2091
2092         /*  Set up Calibration mode if required */
2093         if (board->reg_type == ni_reg_6143) {
2094                 if ((list[0] & CR_ALT_SOURCE)
2095                     && !devpriv->ai_calib_source_enabled) {
2096                         /*  Strobe Relay enable bit */
2097                         ni_writew(devpriv->ai_calib_source |
2098                                   Calibration_Channel_6143_RelayOn,
2099                                   Calibration_Channel_6143);
2100                         ni_writew(devpriv->ai_calib_source,
2101                                   Calibration_Channel_6143);
2102                         devpriv->ai_calib_source_enabled = 1;
2103                         msleep_interruptible(100);      /*  Allow relays to change */
2104                 } else if (!(list[0] & CR_ALT_SOURCE)
2105                            && devpriv->ai_calib_source_enabled) {
2106                         /*  Strobe Relay disable bit */
2107                         ni_writew(devpriv->ai_calib_source |
2108                                   Calibration_Channel_6143_RelayOff,
2109                                   Calibration_Channel_6143);
2110                         ni_writew(devpriv->ai_calib_source,
2111                                   Calibration_Channel_6143);
2112                         devpriv->ai_calib_source_enabled = 0;
2113                         msleep_interruptible(100);      /*  Allow relays to change */
2114                 }
2115         }
2116
2117         offset = 1 << (board->adbits - 1);
2118         for (i = 0; i < n_chan; i++) {
2119                 if ((board->reg_type != ni_reg_6143)
2120                     && (list[i] & CR_ALT_SOURCE)) {
2121                         chan = devpriv->ai_calib_source;
2122                 } else {
2123                         chan = CR_CHAN(list[i]);
2124                 }
2125                 aref = CR_AREF(list[i]);
2126                 range = CR_RANGE(list[i]);
2127                 dither = ((list[i] & CR_ALT_FILTER) != 0);
2128
2129                 /* fix the external/internal range differences */
2130                 range = ni_gainlkup[board->gainlkup][range];
2131                 if (board->reg_type == ni_reg_611x)
2132                         devpriv->ai_offset[i] = offset;
2133                 else
2134                         devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
2135
2136                 hi = 0;
2137                 if ((list[i] & CR_ALT_SOURCE)) {
2138                         if (board->reg_type == ni_reg_611x)
2139                                 ni_writew(CR_CHAN(list[i]) & 0x0003,
2140                                           Calibration_Channel_Select_611x);
2141                 } else {
2142                         if (board->reg_type == ni_reg_611x)
2143                                 aref = AREF_DIFF;
2144                         else if (board->reg_type == ni_reg_6143)
2145                                 aref = AREF_OTHER;
2146                         switch (aref) {
2147                         case AREF_DIFF:
2148                                 hi |= AI_DIFFERENTIAL;
2149                                 break;
2150                         case AREF_COMMON:
2151                                 hi |= AI_COMMON;
2152                                 break;
2153                         case AREF_GROUND:
2154                                 hi |= AI_GROUND;
2155                                 break;
2156                         case AREF_OTHER:
2157                                 break;
2158                         }
2159                 }
2160                 hi |= AI_CONFIG_CHANNEL(chan);
2161
2162                 ni_writew(hi, Configuration_Memory_High);
2163
2164                 if (board->reg_type != ni_reg_6143) {
2165                         lo = range;
2166                         if (i == n_chan - 1)
2167                                 lo |= AI_LAST_CHANNEL;
2168                         if (dither)
2169                                 lo |= AI_DITHER;
2170
2171                         ni_writew(lo, Configuration_Memory_Low);
2172                 }
2173         }
2174
2175         /* prime the channel/gain list */
2176         if ((board->reg_type != ni_reg_611x)
2177             && (board->reg_type != ni_reg_6143)) {
2178                 ni_prime_channelgain_list(dev);
2179         }
2180 }
2181
2182 static int ni_ns_to_timer(const struct comedi_device *dev, unsigned nanosec,
2183                           int round_mode)
2184 {
2185         struct ni_private *devpriv = dev->private;
2186         int divider;
2187
2188         switch (round_mode) {
2189         case TRIG_ROUND_NEAREST:
2190         default:
2191                 divider = (nanosec + devpriv->clock_ns / 2) / devpriv->clock_ns;
2192                 break;
2193         case TRIG_ROUND_DOWN:
2194                 divider = (nanosec) / devpriv->clock_ns;
2195                 break;
2196         case TRIG_ROUND_UP:
2197                 divider = (nanosec + devpriv->clock_ns - 1) / devpriv->clock_ns;
2198                 break;
2199         }
2200         return divider - 1;
2201 }
2202
2203 static unsigned ni_timer_to_ns(const struct comedi_device *dev, int timer)
2204 {
2205         struct ni_private *devpriv = dev->private;
2206
2207         return devpriv->clock_ns * (timer + 1);
2208 }
2209
2210 static unsigned ni_min_ai_scan_period_ns(struct comedi_device *dev,
2211                                          unsigned num_channels)
2212 {
2213         const struct ni_board_struct *board = comedi_board(dev);
2214
2215         switch (board->reg_type) {
2216         case ni_reg_611x:
2217         case ni_reg_6143:
2218                 /*  simultaneously-sampled inputs */
2219                 return board->ai_speed;
2220                 break;
2221         default:
2222                 /*  multiplexed inputs */
2223                 break;
2224         }
2225         return board->ai_speed * num_channels;
2226 }
2227
2228 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2229                          struct comedi_cmd *cmd)
2230 {
2231         const struct ni_board_struct *board = comedi_board(dev);
2232         struct ni_private *devpriv = dev->private;
2233         int err = 0;
2234         int tmp;
2235         unsigned int sources;
2236
2237         /* Step 1 : check if triggers are trivially valid */
2238
2239         if ((cmd->flags & CMDF_WRITE))
2240                 cmd->flags &= ~CMDF_WRITE;
2241
2242         err |= cfc_check_trigger_src(&cmd->start_src,
2243                                         TRIG_NOW | TRIG_INT | TRIG_EXT);
2244         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
2245                                         TRIG_TIMER | TRIG_EXT);
2246
2247         sources = TRIG_TIMER | TRIG_EXT;
2248         if (board->reg_type == ni_reg_611x ||
2249             board->reg_type == ni_reg_6143)
2250                 sources |= TRIG_NOW;
2251         err |= cfc_check_trigger_src(&cmd->convert_src, sources);
2252
2253         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2254         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2255
2256         if (err)
2257                 return 1;
2258
2259         /* Step 2a : make sure trigger sources are unique */
2260
2261         err |= cfc_check_trigger_is_unique(cmd->start_src);
2262         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
2263         err |= cfc_check_trigger_is_unique(cmd->convert_src);
2264         err |= cfc_check_trigger_is_unique(cmd->stop_src);
2265
2266         /* Step 2b : and mutually compatible */
2267
2268         if (err)
2269                 return 2;
2270
2271         /* Step 3: check if arguments are trivially valid */
2272
2273         if (cmd->start_src == TRIG_EXT) {
2274                 /* external trigger */
2275                 unsigned int tmp = CR_CHAN(cmd->start_arg);
2276
2277                 if (tmp > 16)
2278                         tmp = 16;
2279                 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
2280                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, tmp);
2281         } else {
2282                 /* true for both TRIG_NOW and TRIG_INT */
2283                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
2284         }
2285
2286         if (cmd->scan_begin_src == TRIG_TIMER) {
2287                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
2288                         ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2289                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
2290                                                  devpriv->clock_ns * 0xffffff);
2291         } else if (cmd->scan_begin_src == TRIG_EXT) {
2292                 /* external trigger */
2293                 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
2294
2295                 if (tmp > 16)
2296                         tmp = 16;
2297                 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
2298                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
2299         } else {                /* TRIG_OTHER */
2300                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2301         }
2302
2303         if (cmd->convert_src == TRIG_TIMER) {
2304                 if ((board->reg_type == ni_reg_611x)
2305                     || (board->reg_type == ni_reg_6143)) {
2306                         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
2307                 } else {
2308                         err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
2309                                                          board->ai_speed);
2310                         err |= cfc_check_trigger_arg_max(&cmd->convert_arg,
2311                                                 devpriv->clock_ns * 0xffff);
2312                 }
2313         } else if (cmd->convert_src == TRIG_EXT) {
2314                 /* external trigger */
2315                 unsigned int tmp = CR_CHAN(cmd->convert_arg);
2316
2317                 if (tmp > 16)
2318                         tmp = 16;
2319                 tmp |= (cmd->convert_arg & (CR_ALT_FILTER | CR_INVERT));
2320                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, tmp);
2321         } else if (cmd->convert_src == TRIG_NOW) {
2322                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
2323         }
2324
2325         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
2326
2327         if (cmd->stop_src == TRIG_COUNT) {
2328                 unsigned int max_count = 0x01000000;
2329
2330                 if (board->reg_type == ni_reg_611x)
2331                         max_count -= num_adc_stages_611x;
2332                 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, max_count);
2333                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
2334         } else {
2335                 /* TRIG_NONE */
2336                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
2337         }
2338
2339         if (err)
2340                 return 3;
2341
2342         /* step 4: fix up any arguments */
2343
2344         if (cmd->scan_begin_src == TRIG_TIMER) {
2345                 tmp = cmd->scan_begin_arg;
2346                 cmd->scan_begin_arg =
2347                     ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2348                                                        cmd->scan_begin_arg,
2349                                                        cmd->
2350                                                        flags &
2351                                                        TRIG_ROUND_MASK));
2352                 if (tmp != cmd->scan_begin_arg)
2353                         err++;
2354         }
2355         if (cmd->convert_src == TRIG_TIMER) {
2356                 if ((board->reg_type != ni_reg_611x)
2357                     && (board->reg_type != ni_reg_6143)) {
2358                         tmp = cmd->convert_arg;
2359                         cmd->convert_arg =
2360                             ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2361                                                                cmd->convert_arg,
2362                                                                cmd->
2363                                                                flags &
2364                                                                TRIG_ROUND_MASK));
2365                         if (tmp != cmd->convert_arg)
2366                                 err++;
2367                         if (cmd->scan_begin_src == TRIG_TIMER &&
2368                             cmd->scan_begin_arg <
2369                             cmd->convert_arg * cmd->scan_end_arg) {
2370                                 cmd->scan_begin_arg =
2371                                     cmd->convert_arg * cmd->scan_end_arg;
2372                                 err++;
2373                         }
2374                 }
2375         }
2376
2377         if (err)
2378                 return 4;
2379
2380         return 0;
2381 }
2382
2383 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2384 {
2385         const struct ni_board_struct *board = comedi_board(dev);
2386         struct ni_private *devpriv = dev->private;
2387         const struct comedi_cmd *cmd = &s->async->cmd;
2388         int timer;
2389         int mode1 = 0;          /* mode1 is needed for both stop and convert */
2390         int mode2 = 0;
2391         int start_stop_select = 0;
2392         unsigned int stop_count;
2393         int interrupt_a_enable = 0;
2394
2395         MDPRINTK("ni_ai_cmd\n");
2396         if (dev->irq == 0) {
2397                 comedi_error(dev, "cannot run command without an irq");
2398                 return -EIO;
2399         }
2400         ni_clear_ai_fifo(dev);
2401
2402         ni_load_channelgain_list(dev, cmd->chanlist_len, cmd->chanlist);
2403
2404         /* start configuration */
2405         devpriv->stc_writew(dev, AI_Configuration_Start, Joint_Reset_Register);
2406
2407         /* disable analog triggering for now, since it
2408          * interferes with the use of pfi0 */
2409         devpriv->an_trig_etc_reg &= ~Analog_Trigger_Enable;
2410         devpriv->stc_writew(dev, devpriv->an_trig_etc_reg,
2411                             Analog_Trigger_Etc_Register);
2412
2413         switch (cmd->start_src) {
2414         case TRIG_INT:
2415         case TRIG_NOW:
2416                 devpriv->stc_writew(dev, AI_START2_Select(0) |
2417                                     AI_START1_Sync | AI_START1_Edge |
2418                                     AI_START1_Select(0),
2419                                     AI_Trigger_Select_Register);
2420                 break;
2421         case TRIG_EXT:
2422                 {
2423                         int chan = CR_CHAN(cmd->start_arg);
2424                         unsigned int bits = AI_START2_Select(0) |
2425                             AI_START1_Sync | AI_START1_Select(chan + 1);
2426
2427                         if (cmd->start_arg & CR_INVERT)
2428                                 bits |= AI_START1_Polarity;
2429                         if (cmd->start_arg & CR_EDGE)
2430                                 bits |= AI_START1_Edge;
2431                         devpriv->stc_writew(dev, bits,
2432                                             AI_Trigger_Select_Register);
2433                         break;
2434                 }
2435         }
2436
2437         mode2 &= ~AI_Pre_Trigger;
2438         mode2 &= ~AI_SC_Initial_Load_Source;
2439         mode2 &= ~AI_SC_Reload_Mode;
2440         devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2441
2442         if (cmd->chanlist_len == 1 || (board->reg_type == ni_reg_611x)
2443             || (board->reg_type == ni_reg_6143)) {
2444                 start_stop_select |= AI_STOP_Polarity;
2445                 start_stop_select |= AI_STOP_Select(31);        /*  logic low */
2446                 start_stop_select |= AI_STOP_Sync;
2447         } else {
2448                 start_stop_select |= AI_STOP_Select(19);        /*  ai configuration memory */
2449         }
2450         devpriv->stc_writew(dev, start_stop_select,
2451                             AI_START_STOP_Select_Register);
2452
2453         devpriv->ai_cmd2 = 0;
2454         switch (cmd->stop_src) {
2455         case TRIG_COUNT:
2456                 stop_count = cmd->stop_arg - 1;
2457
2458                 if (board->reg_type == ni_reg_611x) {
2459                         /*  have to take 3 stage adc pipeline into account */
2460                         stop_count += num_adc_stages_611x;
2461                 }
2462                 /* stage number of scans */
2463                 devpriv->stc_writel(dev, stop_count, AI_SC_Load_A_Registers);
2464
2465                 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Trigger_Once;
2466                 devpriv->stc_writew(dev, mode1, AI_Mode_1_Register);
2467                 /* load SC (Scan Count) */
2468                 devpriv->stc_writew(dev, AI_SC_Load, AI_Command_1_Register);
2469
2470                 devpriv->ai_continuous = 0;
2471                 if (stop_count == 0) {
2472                         devpriv->ai_cmd2 |= AI_End_On_End_Of_Scan;
2473                         interrupt_a_enable |= AI_STOP_Interrupt_Enable;
2474                         /*  this is required to get the last sample for chanlist_len > 1, not sure why */
2475                         if (cmd->chanlist_len > 1)
2476                                 start_stop_select |=
2477                                     AI_STOP_Polarity | AI_STOP_Edge;
2478                 }
2479                 break;
2480         case TRIG_NONE:
2481                 /* stage number of scans */
2482                 devpriv->stc_writel(dev, 0, AI_SC_Load_A_Registers);
2483
2484                 mode1 |= AI_Start_Stop | AI_Mode_1_Reserved | AI_Continuous;
2485                 devpriv->stc_writew(dev, mode1, AI_Mode_1_Register);
2486
2487                 /* load SC (Scan Count) */
2488                 devpriv->stc_writew(dev, AI_SC_Load, AI_Command_1_Register);
2489
2490                 devpriv->ai_continuous = 1;
2491
2492                 break;
2493         }
2494
2495         switch (cmd->scan_begin_src) {
2496         case TRIG_TIMER:
2497                 /*
2498                    stop bits for non 611x boards
2499                    AI_SI_Special_Trigger_Delay=0
2500                    AI_Pre_Trigger=0
2501                    AI_START_STOP_Select_Register:
2502                    AI_START_Polarity=0 (?)      rising edge
2503                    AI_START_Edge=1              edge triggered
2504                    AI_START_Sync=1 (?)
2505                    AI_START_Select=0            SI_TC
2506                    AI_STOP_Polarity=0           rising edge
2507                    AI_STOP_Edge=0               level
2508                    AI_STOP_Sync=1
2509                    AI_STOP_Select=19            external pin (configuration mem)
2510                  */
2511                 start_stop_select |= AI_START_Edge | AI_START_Sync;
2512                 devpriv->stc_writew(dev, start_stop_select,
2513                                     AI_START_STOP_Select_Register);
2514
2515                 mode2 |= AI_SI_Reload_Mode(0);
2516                 /* AI_SI_Initial_Load_Source=A */
2517                 mode2 &= ~AI_SI_Initial_Load_Source;
2518                 /* mode2 |= AI_SC_Reload_Mode; */
2519                 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2520
2521                 /* load SI */
2522                 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2523                                        TRIG_ROUND_NEAREST);
2524                 devpriv->stc_writel(dev, timer, AI_SI_Load_A_Registers);
2525                 devpriv->stc_writew(dev, AI_SI_Load, AI_Command_1_Register);
2526                 break;
2527         case TRIG_EXT:
2528                 if (cmd->scan_begin_arg & CR_EDGE)
2529                         start_stop_select |= AI_START_Edge;
2530                 /* AI_START_Polarity==1 is falling edge */
2531                 if (cmd->scan_begin_arg & CR_INVERT)
2532                         start_stop_select |= AI_START_Polarity;
2533                 if (cmd->scan_begin_src != cmd->convert_src ||
2534                     (cmd->scan_begin_arg & ~CR_EDGE) !=
2535                     (cmd->convert_arg & ~CR_EDGE))
2536                         start_stop_select |= AI_START_Sync;
2537                 start_stop_select |=
2538                     AI_START_Select(1 + CR_CHAN(cmd->scan_begin_arg));
2539                 devpriv->stc_writew(dev, start_stop_select,
2540                                     AI_START_STOP_Select_Register);
2541                 break;
2542         }
2543
2544         switch (cmd->convert_src) {
2545         case TRIG_TIMER:
2546         case TRIG_NOW:
2547                 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2548                         timer = 1;
2549                 else
2550                         timer = ni_ns_to_timer(dev, cmd->convert_arg,
2551                                                TRIG_ROUND_NEAREST);
2552                 devpriv->stc_writew(dev, 1, AI_SI2_Load_A_Register);    /* 0,0 does not work. */
2553                 devpriv->stc_writew(dev, timer, AI_SI2_Load_B_Register);
2554
2555                 /* AI_SI2_Reload_Mode = alternate */
2556                 /* AI_SI2_Initial_Load_Source = A */
2557                 mode2 &= ~AI_SI2_Initial_Load_Source;
2558                 mode2 |= AI_SI2_Reload_Mode;
2559                 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2560
2561                 /* AI_SI2_Load */
2562                 devpriv->stc_writew(dev, AI_SI2_Load, AI_Command_1_Register);
2563
2564                 mode2 |= AI_SI2_Reload_Mode;    /*  alternate */
2565                 mode2 |= AI_SI2_Initial_Load_Source;    /*  B */
2566
2567                 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2568                 break;
2569         case TRIG_EXT:
2570                 mode1 |= AI_CONVERT_Source_Select(1 + cmd->convert_arg);
2571                 if ((cmd->convert_arg & CR_INVERT) == 0)
2572                         mode1 |= AI_CONVERT_Source_Polarity;
2573                 devpriv->stc_writew(dev, mode1, AI_Mode_1_Register);
2574
2575                 mode2 |= AI_Start_Stop_Gate_Enable | AI_SC_Gate_Enable;
2576                 devpriv->stc_writew(dev, mode2, AI_Mode_2_Register);
2577
2578                 break;
2579         }
2580
2581         if (dev->irq) {
2582
2583                 /* interrupt on FIFO, errors, SC_TC */
2584                 interrupt_a_enable |= AI_Error_Interrupt_Enable |
2585                     AI_SC_TC_Interrupt_Enable;
2586
2587 #ifndef PCIDMA
2588                 interrupt_a_enable |= AI_FIFO_Interrupt_Enable;
2589 #endif
2590
2591                 if (cmd->flags & TRIG_WAKE_EOS
2592                     || (devpriv->ai_cmd2 & AI_End_On_End_Of_Scan)) {
2593                         /* wake on end-of-scan */
2594                         devpriv->aimode = AIMODE_SCAN;
2595                 } else {
2596                         devpriv->aimode = AIMODE_HALF_FULL;
2597                 }
2598
2599                 switch (devpriv->aimode) {
2600                 case AIMODE_HALF_FULL:
2601                         /*generate FIFO interrupts and DMA requests on half-full */
2602 #ifdef PCIDMA
2603                         devpriv->stc_writew(dev, AI_FIFO_Mode_HF_to_E,
2604                                             AI_Mode_3_Register);
2605 #else
2606                         devpriv->stc_writew(dev, AI_FIFO_Mode_HF,
2607                                             AI_Mode_3_Register);
2608 #endif
2609                         break;
2610                 case AIMODE_SAMPLE:
2611                         /*generate FIFO interrupts on non-empty */
2612                         devpriv->stc_writew(dev, AI_FIFO_Mode_NE,
2613                                             AI_Mode_3_Register);
2614                         break;
2615                 case AIMODE_SCAN:
2616 #ifdef PCIDMA
2617                         devpriv->stc_writew(dev, AI_FIFO_Mode_NE,
2618                                             AI_Mode_3_Register);
2619 #else
2620                         devpriv->stc_writew(dev, AI_FIFO_Mode_HF,
2621                                             AI_Mode_3_Register);
2622 #endif
2623                         interrupt_a_enable |= AI_STOP_Interrupt_Enable;
2624                         break;
2625                 default:
2626                         break;
2627                 }
2628
2629                 devpriv->stc_writew(dev, AI_Error_Interrupt_Ack | AI_STOP_Interrupt_Ack | AI_START_Interrupt_Ack | AI_START2_Interrupt_Ack | AI_START1_Interrupt_Ack | AI_SC_TC_Interrupt_Ack | AI_SC_TC_Error_Confirm, Interrupt_A_Ack_Register);      /* clear interrupts */
2630
2631                 ni_set_bits(dev, Interrupt_A_Enable_Register,
2632                             interrupt_a_enable, 1);
2633
2634                 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",
2635                          devpriv->int_a_enable_reg);
2636         } else {
2637                 /* interrupt on nothing */
2638                 ni_set_bits(dev, Interrupt_A_Enable_Register, ~0, 0);
2639
2640                 /* XXX start polling if necessary */
2641                 MDPRINTK("interrupting on nothing\n");
2642         }
2643
2644         /* end configuration */
2645         devpriv->stc_writew(dev, AI_Configuration_End, Joint_Reset_Register);
2646
2647         switch (cmd->scan_begin_src) {
2648         case TRIG_TIMER:
2649                 devpriv->stc_writew(dev,
2650                                     AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm |
2651                                     AI_SC_Arm, AI_Command_1_Register);
2652                 break;
2653         case TRIG_EXT:
2654                 /* XXX AI_SI_Arm? */
2655                 devpriv->stc_writew(dev,
2656                                     AI_SI2_Arm | AI_SI_Arm | AI_DIV_Arm |
2657                                     AI_SC_Arm, AI_Command_1_Register);
2658                 break;
2659         }
2660
2661 #ifdef PCIDMA
2662         {
2663                 int retval = ni_ai_setup_MITE_dma(dev);
2664                 if (retval)
2665                         return retval;
2666         }
2667         /* mite_dump_regs(devpriv->mite); */
2668 #endif
2669
2670         switch (cmd->start_src) {
2671         case TRIG_NOW:
2672                 /* AI_START1_Pulse */
2673                 devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2,
2674                                     AI_Command_2_Register);
2675                 s->async->inttrig = NULL;
2676                 break;
2677         case TRIG_EXT:
2678                 s->async->inttrig = NULL;
2679                 break;
2680         case TRIG_INT:
2681                 s->async->inttrig = &ni_ai_inttrig;
2682                 break;
2683         }
2684
2685         MDPRINTK("exit ni_ai_cmd\n");
2686
2687         return 0;
2688 }
2689
2690 static int ni_ai_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
2691                          unsigned int trignum)
2692 {
2693         struct ni_private *devpriv = dev->private;
2694
2695         if (trignum != 0)
2696                 return -EINVAL;
2697
2698         devpriv->stc_writew(dev, AI_START1_Pulse | devpriv->ai_cmd2,
2699                             AI_Command_2_Register);
2700         s->async->inttrig = NULL;
2701
2702         return 1;
2703 }
2704
2705 static int ni_ai_config_analog_trig(struct comedi_device *dev,
2706                                     struct comedi_subdevice *s,
2707                                     struct comedi_insn *insn,
2708                                     unsigned int *data);
2709
2710 static int ni_ai_insn_config(struct comedi_device *dev,
2711                              struct comedi_subdevice *s,
2712                              struct comedi_insn *insn, unsigned int *data)
2713 {
2714         const struct ni_board_struct *board = comedi_board(dev);
2715         struct ni_private *devpriv = dev->private;
2716
2717         if (insn->n < 1)
2718                 return -EINVAL;
2719
2720         switch (data[0]) {
2721         case INSN_CONFIG_ANALOG_TRIG:
2722                 return ni_ai_config_analog_trig(dev, s, insn, data);
2723         case INSN_CONFIG_ALT_SOURCE:
2724                 if (board->reg_type & ni_reg_m_series_mask) {
2725                         if (data[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask |
2726                                         MSeries_AI_Bypass_Cal_Sel_Neg_Mask |
2727                                         MSeries_AI_Bypass_Mode_Mux_Mask |
2728                                         MSeries_AO_Bypass_AO_Cal_Sel_Mask)) {
2729                                 return -EINVAL;
2730                         }
2731                         devpriv->ai_calib_source = data[1];
2732                 } else if (board->reg_type == ni_reg_6143) {
2733                         unsigned int calib_source;
2734
2735                         calib_source = data[1] & 0xf;
2736
2737                         if (calib_source > 0xF)
2738                                 return -EINVAL;
2739
2740                         devpriv->ai_calib_source = calib_source;
2741                         ni_writew(calib_source, Calibration_Channel_6143);
2742                 } else {
2743                         unsigned int calib_source;
2744                         unsigned int calib_source_adjust;
2745
2746                         calib_source = data[1] & 0xf;
2747                         calib_source_adjust = (data[1] >> 4) & 0xff;
2748
2749                         if (calib_source >= 8)
2750                                 return -EINVAL;
2751                         devpriv->ai_calib_source = calib_source;
2752                         if (board->reg_type == ni_reg_611x) {
2753                                 ni_writeb(calib_source_adjust,
2754                                           Cal_Gain_Select_611x);
2755                         }
2756                 }
2757                 return 2;
2758         default:
2759                 break;
2760         }
2761
2762         return -EINVAL;
2763 }
2764
2765 static int ni_ai_config_analog_trig(struct comedi_device *dev,
2766                                     struct comedi_subdevice *s,
2767                                     struct comedi_insn *insn,
2768                                     unsigned int *data)
2769 {
2770         const struct ni_board_struct *board = comedi_board(dev);
2771         struct ni_private *devpriv = dev->private;
2772         unsigned int a, b, modebits;
2773         int err = 0;
2774
2775         /* data[1] is flags
2776          * data[2] is analog line
2777          * data[3] is set level
2778          * data[4] is reset level */
2779         if (!board->has_analog_trig)
2780                 return -EINVAL;
2781         if ((data[1] & 0xffff0000) != COMEDI_EV_SCAN_BEGIN) {
2782                 data[1] &= (COMEDI_EV_SCAN_BEGIN | 0xffff);
2783                 err++;
2784         }
2785         if (data[2] >= board->n_adchan) {
2786                 data[2] = board->n_adchan - 1;
2787                 err++;
2788         }
2789         if (data[3] > 255) {    /* a */
2790                 data[3] = 255;
2791                 err++;
2792         }
2793         if (data[4] > 255) {    /* b */
2794                 data[4] = 255;
2795                 err++;
2796         }
2797         /*
2798          * 00 ignore
2799          * 01 set
2800          * 10 reset
2801          *
2802          * modes:
2803          *   1 level:                    +b-   +a-
2804          *     high mode                00 00 01 10
2805          *     low mode                 00 00 10 01
2806          *   2 level: (a<b)
2807          *     hysteresis low mode      10 00 00 01
2808          *     hysteresis high mode     01 00 00 10
2809          *     middle mode              10 01 01 10
2810          */
2811
2812         a = data[3];
2813         b = data[4];
2814         modebits = data[1] & 0xff;
2815         if (modebits & 0xf0) {
2816                 /* two level mode */
2817                 if (b < a) {
2818                         /* swap order */
2819                         a = data[4];
2820                         b = data[3];
2821                         modebits =
2822                             ((data[1] & 0xf) << 4) | ((data[1] & 0xf0) >> 4);
2823                 }
2824                 devpriv->atrig_low = a;
2825                 devpriv->atrig_high = b;
2826                 switch (modebits) {
2827                 case 0x81:      /* low hysteresis mode */
2828                         devpriv->atrig_mode = 6;
2829                         break;
2830                 case 0x42:      /* high hysteresis mode */
2831                         devpriv->atrig_mode = 3;
2832                         break;
2833                 case 0x96:      /* middle window mode */
2834                         devpriv->atrig_mode = 2;
2835                         break;
2836                 default:
2837                         data[1] &= ~0xff;
2838                         err++;
2839                 }
2840         } else {
2841                 /* one level mode */
2842                 if (b != 0) {
2843                         data[4] = 0;
2844                         err++;
2845                 }
2846                 switch (modebits) {
2847                 case 0x06:      /* high window mode */
2848                         devpriv->atrig_high = a;
2849                         devpriv->atrig_mode = 0;
2850                         break;
2851                 case 0x09:      /* low window mode */
2852                         devpriv->atrig_low = a;
2853                         devpriv->atrig_mode = 1;
2854                         break;
2855                 default:
2856                         data[1] &= ~0xff;
2857                         err++;
2858                 }
2859         }
2860         if (err)
2861                 return -EAGAIN;
2862         return 5;
2863 }
2864
2865 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2866 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2867                         void *data, unsigned int num_bytes,
2868                         unsigned int chan_index)
2869 {
2870         const struct ni_board_struct *board = comedi_board(dev);
2871         struct comedi_async *async = s->async;
2872         unsigned int range;
2873         unsigned int i;
2874         unsigned int offset;
2875         unsigned int length = num_bytes / sizeof(short);
2876         unsigned short *array = data;
2877
2878         offset = 1 << (board->aobits - 1);
2879         for (i = 0; i < length; i++) {
2880                 range = CR_RANGE(async->cmd.chanlist[chan_index]);
2881                 if (board->ao_unipolar == 0 || (range & 1) == 0)
2882                         array[i] -= offset;
2883 #ifdef PCIDMA
2884                 array[i] = cpu_to_le16(array[i]);
2885 #endif
2886                 chan_index++;
2887                 chan_index %= async->cmd.chanlist_len;
2888         }
2889 }
2890
2891 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2892                                           struct comedi_subdevice *s,
2893                                           unsigned int chanspec[],
2894                                           unsigned int n_chans, int timed)
2895 {
2896         const struct ni_board_struct *board = comedi_board(dev);
2897         struct ni_private *devpriv = dev->private;
2898         unsigned int range;
2899         unsigned int chan;
2900         unsigned int conf;
2901         int i;
2902         int invert = 0;
2903
2904         if (timed) {
2905                 for (i = 0; i < board->n_aochan; ++i) {
2906                         devpriv->ao_conf[i] &= ~MSeries_AO_Update_Timed_Bit;
2907                         ni_writeb(devpriv->ao_conf[i],
2908                                   M_Offset_AO_Config_Bank(i));
2909                         ni_writeb(0xf, M_Offset_AO_Waveform_Order(i));
2910                 }
2911         }
2912         for (i = 0; i < n_chans; i++) {
2913                 const struct comedi_krange *krange;
2914                 chan = CR_CHAN(chanspec[i]);
2915                 range = CR_RANGE(chanspec[i]);
2916                 krange = s->range_table->range + range;
2917                 invert = 0;
2918                 conf = 0;
2919                 switch (krange->max - krange->min) {
2920                 case 20000000:
2921                         conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2922                         ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2923                         break;
2924                 case 10000000:
2925                         conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2926                         ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan));
2927                         break;
2928                 case 4000000:
2929                         conf |= MSeries_AO_DAC_Reference_10V_Internal_Bits;
2930                         ni_writeb(MSeries_Attenuate_x5_Bit,
2931                                   M_Offset_AO_Reference_Attenuation(chan));
2932                         break;
2933                 case 2000000:
2934                         conf |= MSeries_AO_DAC_Reference_5V_Internal_Bits;
2935                         ni_writeb(MSeries_Attenuate_x5_Bit,
2936                                   M_Offset_AO_Reference_Attenuation(chan));
2937                         break;
2938                 default:
2939                         printk("%s: bug! unhandled ao reference voltage\n",
2940                                __func__);
2941                         break;
2942                 }
2943                 switch (krange->max + krange->min) {
2944                 case 0:
2945                         conf |= MSeries_AO_DAC_Offset_0V_Bits;
2946                         break;
2947                 case 10000000:
2948                         conf |= MSeries_AO_DAC_Offset_5V_Bits;
2949                         break;
2950                 default:
2951                         printk("%s: bug! unhandled ao offset voltage\n",
2952                                __func__);
2953                         break;
2954                 }
2955                 if (timed)
2956                         conf |= MSeries_AO_Update_Timed_Bit;
2957                 ni_writeb(conf, M_Offset_AO_Config_Bank(chan));
2958                 devpriv->ao_conf[chan] = conf;
2959                 ni_writeb(i, M_Offset_AO_Waveform_Order(chan));
2960         }
2961         return invert;
2962 }
2963
2964 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2965                                      struct comedi_subdevice *s,
2966                                      unsigned int chanspec[],
2967                                      unsigned int n_chans)
2968 {
2969         const struct ni_board_struct *board = comedi_board(dev);
2970         struct ni_private *devpriv = dev->private;
2971         unsigned int range;
2972         unsigned int chan;
2973         unsigned int conf;
2974         int i;
2975         int invert = 0;
2976
2977         for (i = 0; i < n_chans; i++) {
2978                 chan = CR_CHAN(chanspec[i]);
2979                 range = CR_RANGE(chanspec[i]);
2980                 conf = AO_Channel(chan);
2981
2982                 if (board->ao_unipolar) {
2983                         if ((range & 1) == 0) {
2984                                 conf |= AO_Bipolar;
2985                                 invert = (1 << (board->aobits - 1));
2986                         } else {
2987                                 invert = 0;
2988                         }
2989                         if (range & 2)
2990                                 conf |= AO_Ext_Ref;
2991                 } else {
2992                         conf |= AO_Bipolar;
2993                         invert = (1 << (board->aobits - 1));
2994                 }
2995
2996                 /* not all boards can deglitch, but this shouldn't hurt */
2997                 if (chanspec[i] & CR_DEGLITCH)
2998                         conf |= AO_Deglitch;
2999
3000                 /* analog reference */
3001                 /* AREF_OTHER connects AO ground to AI ground, i think */
3002                 conf |= (CR_AREF(chanspec[i]) ==
3003                          AREF_OTHER) ? AO_Ground_Ref : 0;
3004
3005                 ni_writew(conf, AO_Configuration);
3006                 devpriv->ao_conf[chan] = conf;
3007         }
3008         return invert;
3009 }
3010
3011 static int ni_ao_config_chanlist(struct comedi_device *dev,
3012                                  struct comedi_subdevice *s,
3013                                  unsigned int chanspec[], unsigned int n_chans,
3014                                  int timed)
3015 {
3016         const struct ni_board_struct *board = comedi_board(dev);
3017
3018         if (board->reg_type & ni_reg_m_series_mask)
3019                 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
3020                                                       timed);
3021         else
3022                 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
3023 }
3024
3025 static int ni_ao_insn_read(struct comedi_device *dev,
3026                            struct comedi_subdevice *s, struct comedi_insn *insn,
3027                            unsigned int *data)
3028 {
3029         struct ni_private *devpriv = dev->private;
3030
3031         data[0] = devpriv->ao[CR_CHAN(insn->chanspec)];
3032
3033         return 1;
3034 }
3035
3036 static int ni_ao_insn_write(struct comedi_device *dev,
3037                             struct comedi_subdevice *s,
3038                             struct comedi_insn *insn, unsigned int *data)
3039 {
3040         const struct ni_board_struct *board = comedi_board(dev);
3041         struct ni_private *devpriv = dev->private;
3042         unsigned int chan = CR_CHAN(insn->chanspec);
3043         unsigned int invert;
3044
3045         invert = ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
3046
3047         devpriv->ao[chan] = data[0];
3048
3049         if (board->reg_type & ni_reg_m_series_mask) {
3050                 ni_writew(data[0], M_Offset_DAC_Direct_Data(chan));
3051         } else
3052                 ni_writew(data[0] ^ invert,
3053                           (chan) ? DAC1_Direct_Data : DAC0_Direct_Data);
3054
3055         return 1;
3056 }
3057
3058 static int ni_ao_insn_write_671x(struct comedi_device *dev,
3059                                  struct comedi_subdevice *s,
3060                                  struct comedi_insn *insn, unsigned int *data)
3061 {
3062         const struct ni_board_struct *board = comedi_board(dev);
3063         struct ni_private *devpriv = dev->private;
3064         unsigned int chan = CR_CHAN(insn->chanspec);
3065         unsigned int invert;
3066
3067         ao_win_out(1 << chan, AO_Immediate_671x);
3068         invert = 1 << (board->aobits - 1);
3069
3070         ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
3071
3072         devpriv->ao[chan] = data[0];
3073         ao_win_out(data[0] ^ invert, DACx_Direct_Data_671x(chan));
3074
3075         return 1;
3076 }
3077
3078 static int ni_ao_insn_config(struct comedi_device *dev,
3079                              struct comedi_subdevice *s,
3080                              struct comedi_insn *insn, unsigned int *data)
3081 {
3082         const struct ni_board_struct *board = comedi_board(dev);
3083         struct ni_private *devpriv = dev->private;
3084
3085         switch (data[0]) {
3086         case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
3087                 switch (data[1]) {
3088                 case COMEDI_OUTPUT:
3089                         data[2] = 1 + board->ao_fifo_depth * sizeof(short);
3090                         if (devpriv->mite)
3091                                 data[2] += devpriv->mite->fifo_size;
3092                         break;
3093                 case COMEDI_INPUT:
3094                         data[2] = 0;
3095                         break;
3096                 default:
3097                         return -EINVAL;
3098                         break;
3099                 }
3100                 return 0;
3101         default:
3102                 break;
3103         }
3104
3105         return -EINVAL;
3106 }
3107
3108 static int ni_ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
3109                          unsigned int trignum)
3110 {
3111         const struct ni_board_struct *board __maybe_unused = comedi_board(dev);
3112         struct ni_private *devpriv = dev->private;
3113         int ret;
3114         int interrupt_b_bits;
3115         int i;
3116         static const int timeout = 1000;
3117
3118         if (trignum != 0)
3119                 return -EINVAL;
3120
3121         /* Null trig at beginning prevent ao start trigger from executing more than
3122            once per command (and doing things like trying to allocate the ao dma channel
3123            multiple times) */
3124         s->async->inttrig = NULL;
3125
3126         ni_set_bits(dev, Interrupt_B_Enable_Register,
3127                     AO_FIFO_Interrupt_Enable | AO_Error_Interrupt_Enable, 0);
3128         interrupt_b_bits = AO_Error_Interrupt_Enable;
3129 #ifdef PCIDMA
3130         devpriv->stc_writew(dev, 1, DAC_FIFO_Clear);
3131         if (board->reg_type & ni_reg_6xxx_mask)
3132                 ni_ao_win_outl(dev, 0x6, AO_FIFO_Offset_Load_611x);
3133         ret = ni_ao_setup_MITE_dma(dev);
3134         if (ret)
3135                 return ret;
3136         ret = ni_ao_wait_for_dma_load(dev);
3137         if (ret < 0)
3138                 return ret;
3139 #else
3140         ret = ni_ao_prep_fifo(dev, s);
3141         if (ret == 0)
3142                 return -EPIPE;
3143
3144         interrupt_b_bits |= AO_FIFO_Interrupt_Enable;
3145 #endif
3146
3147         devpriv->stc_writew(dev, devpriv->ao_mode3 | AO_Not_An_UPDATE,
3148                             AO_Mode_3_Register);
3149         devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
3150         /* wait for DACs to be loaded */
3151         for (i = 0; i < timeout; i++) {
3152                 udelay(1);
3153                 if ((devpriv->stc_readw(dev,
3154                                         Joint_Status_2_Register) &
3155                      AO_TMRDACWRs_In_Progress_St) == 0)
3156                         break;
3157         }
3158         if (i == timeout) {
3159                 comedi_error(dev,
3160                              "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
3161                 return -EIO;
3162         }
3163         /*  stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears */
3164         devpriv->stc_writew(dev, AO_Error_Interrupt_Ack,
3165                             Interrupt_B_Ack_Register);
3166
3167         ni_set_bits(dev, Interrupt_B_Enable_Register, interrupt_b_bits, 1);
3168
3169         devpriv->stc_writew(dev,
3170                             devpriv->ao_cmd1 | AO_UI_Arm | AO_UC_Arm | AO_BC_Arm
3171                             | AO_DAC1_Update_Mode | AO_DAC0_Update_Mode,
3172                             AO_Command_1_Register);
3173
3174         devpriv->stc_writew(dev, devpriv->ao_cmd2 | AO_START1_Pulse,
3175                             AO_Command_2_Register);
3176
3177         return 0;
3178 }
3179
3180 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3181 {
3182         const struct ni_board_struct *board = comedi_board(dev);
3183         struct ni_private *devpriv = dev->private;
3184         const struct comedi_cmd *cmd = &s->async->cmd;
3185         int bits;
3186         int i;
3187         unsigned trigvar;
3188
3189         if (dev->irq == 0) {
3190                 comedi_error(dev, "cannot run command without an irq");
3191                 return -EIO;
3192         }
3193
3194         devpriv->stc_writew(dev, AO_Configuration_Start, Joint_Reset_Register);
3195
3196         devpriv->stc_writew(dev, AO_Disarm, AO_Command_1_Register);
3197
3198         if (board->reg_type & ni_reg_6xxx_mask) {
3199                 ao_win_out(CLEAR_WG, AO_Misc_611x);
3200
3201                 bits = 0;
3202                 for (i = 0; i < cmd->chanlist_len; i++) {
3203                         int chan;
3204
3205                         chan = CR_CHAN(cmd->chanlist[i]);
3206                         bits |= 1 << chan;
3207                         ao_win_out(chan, AO_Waveform_Generation_611x);
3208                 }
3209                 ao_win_out(bits, AO_Timed_611x);
3210         }
3211
3212         ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3213
3214         if (cmd->stop_src == TRIG_NONE) {
3215                 devpriv->ao_mode1 |= AO_Continuous;
3216                 devpriv->ao_mode1 &= ~AO_Trigger_Once;
3217         } else {
3218                 devpriv->ao_mode1 &= ~AO_Continuous;
3219                 devpriv->ao_mode1 |= AO_Trigger_Once;
3220         }
3221         devpriv->stc_writew(dev, devpriv->ao_mode1, AO_Mode_1_Register);
3222         switch (cmd->start_src) {
3223         case TRIG_INT:
3224         case TRIG_NOW:
3225                 devpriv->ao_trigger_select &=
3226                     ~(AO_START1_Polarity | AO_START1_Select(-1));
3227                 devpriv->ao_trigger_select |= AO_START1_Edge | AO_START1_Sync;
3228                 devpriv->stc_writew(dev, devpriv->ao_trigger_select,
3229                                     AO_Trigger_Select_Register);
3230                 break;
3231         case TRIG_EXT:
3232                 devpriv->ao_trigger_select =
3233                     AO_START1_Select(CR_CHAN(cmd->start_arg) + 1);
3234                 if (cmd->start_arg & CR_INVERT)
3235                         devpriv->ao_trigger_select |= AO_START1_Polarity;       /*  0=active high, 1=active low. see daq-stc 3-24 (p186) */
3236                 if (cmd->start_arg & CR_EDGE)
3237                         devpriv->ao_trigger_select |= AO_START1_Edge;   /*  0=edge detection disabled, 1=enabled */
3238                 devpriv->stc_writew(dev, devpriv->ao_trigger_select,
3239                                     AO_Trigger_Select_Register);
3240                 break;
3241         default:
3242                 BUG();
3243                 break;
3244         }
3245         devpriv->ao_mode3 &= ~AO_Trigger_Length;
3246         devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
3247
3248         devpriv->stc_writew(dev, devpriv->ao_mode1, AO_Mode_1_Register);
3249         devpriv->ao_mode2 &= ~AO_BC_Initial_Load_Source;
3250         devpriv->stc_writew(dev, devpriv->ao_mode2, AO_Mode_2_Register);
3251         if (cmd->stop_src == TRIG_NONE) {
3252                 devpriv->stc_writel(dev, 0xffffff, AO_BC_Load_A_Register);
3253         } else {
3254                 devpriv->stc_writel(dev, 0, AO_BC_Load_A_Register);
3255         }
3256         devpriv->stc_writew(dev, AO_BC_Load, AO_Command_1_Register);
3257         devpriv->ao_mode2 &= ~AO_UC_Initial_Load_Source;
3258         devpriv->stc_writew(dev, devpriv->ao_mode2, AO_Mode_2_Register);
3259         switch (cmd->stop_src) {
3260         case TRIG_COUNT:
3261                 if (board->reg_type & ni_reg_m_series_mask) {
3262                         /*  this is how the NI example code does it for m-series boards, verified correct with 6259 */
3263                         devpriv->stc_writel(dev, cmd->stop_arg - 1,
3264                                             AO_UC_Load_A_Register);
3265                         devpriv->stc_writew(dev, AO_UC_Load,
3266                                             AO_Command_1_Register);
3267                 } else {
3268                         devpriv->stc_writel(dev, cmd->stop_arg,
3269                                             AO_UC_Load_A_Register);
3270                         devpriv->stc_writew(dev, AO_UC_Load,
3271                                             AO_Command_1_Register);
3272                         devpriv->stc_writel(dev, cmd->stop_arg - 1,
3273                                             AO_UC_Load_A_Register);
3274                 }
3275                 break;
3276         case TRIG_NONE:
3277                 devpriv->stc_writel(dev, 0xffffff, AO_UC_Load_A_Register);
3278                 devpriv->stc_writew(dev, AO_UC_Load, AO_Command_1_Register);
3279                 devpriv->stc_writel(dev, 0xffffff, AO_UC_Load_A_Register);
3280                 break;
3281         default:
3282                 devpriv->stc_writel(dev, 0, AO_UC_Load_A_Register);
3283                 devpriv->stc_writew(dev, AO_UC_Load, AO_Command_1_Register);
3284                 devpriv->stc_writel(dev, cmd->stop_arg, AO_UC_Load_A_Register);
3285         }
3286
3287         devpriv->ao_mode1 &=
3288             ~(AO_UI_Source_Select(0x1f) | AO_UI_Source_Polarity |
3289               AO_UPDATE_Source_Select(0x1f) | AO_UPDATE_Source_Polarity);
3290         switch (cmd->scan_begin_src) {
3291         case TRIG_TIMER:
3292                 devpriv->ao_cmd2 &= ~AO_BC_Gate_Enable;
3293                 trigvar =
3294                     ni_ns_to_timer(dev, cmd->scan_begin_arg,
3295                                    TRIG_ROUND_NEAREST);
3296                 devpriv->stc_writel(dev, 1, AO_UI_Load_A_Register);
3297                 devpriv->stc_writew(dev, AO_UI_Load, AO_Command_1_Register);
3298                 devpriv->stc_writel(dev, trigvar, AO_UI_Load_A_Register);
3299                 break;
3300         case TRIG_EXT:
3301                 devpriv->ao_mode1 |=
3302                     AO_UPDATE_Source_Select(cmd->scan_begin_arg);
3303                 if (cmd->scan_begin_arg & CR_INVERT)
3304                         devpriv->ao_mode1 |= AO_UPDATE_Source_Polarity;
3305                 devpriv->ao_cmd2 |= AO_BC_Gate_Enable;
3306                 break;
3307         default:
3308                 BUG();
3309                 break;
3310         }
3311         devpriv->stc_writew(dev, devpriv->ao_cmd2, AO_Command_2_Register);
3312         devpriv->stc_writew(dev, devpriv->ao_mode1, AO_Mode_1_Register);
3313         devpriv->ao_mode2 &=
3314             ~(AO_UI_Reload_Mode(3) | AO_UI_Initial_Load_Source);
3315         devpriv->stc_writew(dev, devpriv->ao_mode2, AO_Mode_2_Register);
3316
3317         if (cmd->scan_end_arg > 1) {
3318                 devpriv->ao_mode1 |= AO_Multiple_Channels;
3319                 devpriv->stc_writew(dev,
3320                                     AO_Number_Of_Channels(cmd->scan_end_arg -
3321                                                           1) |
3322                                     AO_UPDATE_Output_Select
3323                                     (AO_Update_Output_High_Z),
3324                                     AO_Output_Control_Register);
3325         } else {
3326                 unsigned bits;
3327                 devpriv->ao_mode1 &= ~AO_Multiple_Channels;
3328                 bits = AO_UPDATE_Output_Select(AO_Update_Output_High_Z);
3329                 if (board->reg_type &
3330                     (ni_reg_m_series_mask | ni_reg_6xxx_mask)) {
3331                         bits |= AO_Number_Of_Channels(0);
3332                 } else {
3333                         bits |=
3334                             AO_Number_Of_Channels(CR_CHAN(cmd->chanlist[0]));
3335                 }
3336                 devpriv->stc_writew(dev, bits, AO_Output_Control_Register);
3337         }
3338         devpriv->stc_writew(dev, devpriv->ao_mode1, AO_Mode_1_Register);
3339
3340         devpriv->stc_writew(dev, AO_DAC0_Update_Mode | AO_DAC1_Update_Mode,
3341                             AO_Command_1_Register);
3342
3343         devpriv->ao_mode3 |= AO_Stop_On_Overrun_Error;
3344         devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
3345
3346         devpriv->ao_mode2 &= ~AO_FIFO_Mode_Mask;
3347 #ifdef PCIDMA
3348         devpriv->ao_mode2 |= AO_FIFO_Mode_HF_to_F;
3349 #else
3350         devpriv->ao_mode2 |= AO_FIFO_Mode_HF;
3351 #endif
3352         devpriv->ao_mode2 &= ~AO_FIFO_Retransmit_Enable;
3353         devpriv->stc_writew(dev, devpriv->ao_mode2, AO_Mode_2_Register);
3354
3355         bits = AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
3356             AO_TMRDACWR_Pulse_Width;
3357         if (board->ao_fifo_depth)
3358                 bits |= AO_FIFO_Enable;
3359         else
3360                 bits |= AO_DMA_PIO_Control;
3361 #if 0
3362         /* F Hess: windows driver does not set AO_Number_Of_DAC_Packages bit for 6281,
3363            verified with bus analyzer. */
3364         if (board->reg_type & ni_reg_m_series_mask)
3365                 bits |= AO_Number_Of_DAC_Packages;
3366 #endif
3367         devpriv->stc_writew(dev, bits, AO_Personal_Register);
3368         /*  enable sending of ao dma requests */
3369         devpriv->stc_writew(dev, AO_AOFREQ_Enable, AO_Start_Select_Register);
3370
3371         devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
3372
3373         if (cmd->stop_src == TRIG_COUNT) {
3374                 devpriv->stc_writew(dev, AO_BC_TC_Interrupt_Ack,
3375                                     Interrupt_B_Ack_Register);
3376                 ni_set_bits(dev, Interrupt_B_Enable_Register,
3377                             AO_BC_TC_Interrupt_Enable, 1);
3378         }
3379
3380         s->async->inttrig = &ni_ao_inttrig;
3381
3382         return 0;
3383 }
3384
3385 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3386                          struct comedi_cmd *cmd)
3387 {
3388         const struct ni_board_struct *board = comedi_board(dev);
3389         struct ni_private *devpriv = dev->private;
3390         int err = 0;
3391         int tmp;
3392
3393         /* Step 1 : check if triggers are trivially valid */
3394
3395         if ((cmd->flags & CMDF_WRITE) == 0)
3396                 cmd->flags |= CMDF_WRITE;
3397
3398         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3399         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
3400                                         TRIG_TIMER | TRIG_EXT);
3401         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3402         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3403         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3404
3405         if (err)
3406                 return 1;
3407
3408         /* Step 2a : make sure trigger sources are unique */
3409
3410         err |= cfc_check_trigger_is_unique(cmd->start_src);
3411         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
3412         err |= cfc_check_trigger_is_unique(cmd->stop_src);
3413
3414         /* Step 2b : and mutually compatible */
3415
3416         if (err)
3417                 return 2;
3418
3419         /* Step 3: check if arguments are trivially valid */
3420
3421         if (cmd->start_src == TRIG_EXT) {
3422                 /* external trigger */
3423                 unsigned int tmp = CR_CHAN(cmd->start_arg);
3424
3425                 if (tmp > 18)
3426                         tmp = 18;
3427                 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
3428                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, tmp);
3429         } else {
3430                 /* true for both TRIG_NOW and TRIG_INT */
3431                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
3432         }
3433
3434         if (cmd->scan_begin_src == TRIG_TIMER) {
3435                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
3436                                                  board->ao_speed);
3437                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
3438                                                  devpriv->clock_ns * 0xffffff);
3439         }
3440
3441         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
3442         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
3443
3444         if (cmd->stop_src == TRIG_COUNT)
3445                 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3446         else    /* TRIG_NONE */
3447                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
3448
3449         if (err)
3450                 return 3;
3451
3452         /* step 4: fix up any arguments */
3453         if (cmd->scan_begin_src == TRIG_TIMER) {
3454                 tmp = cmd->scan_begin_arg;
3455                 cmd->scan_begin_arg =
3456                     ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3457                                                        cmd->scan_begin_arg,
3458                                                        cmd->
3459                                                        flags &
3460                                                        TRIG_ROUND_MASK));
3461                 if (tmp != cmd->scan_begin_arg)
3462                         err++;
3463         }
3464         if (err)
3465                 return 4;
3466
3467         /* step 5: fix up chanlist */
3468
3469         if (err)
3470                 return 5;
3471
3472         return 0;
3473 }
3474
3475 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3476 {
3477         const struct ni_board_struct *board = comedi_board(dev);
3478         struct ni_private *devpriv = dev->private;
3479
3480         /* devpriv->ao0p=0x0000; */
3481         /* ni_writew(devpriv->ao0p,AO_Configuration); */
3482
3483         /* devpriv->ao1p=AO_Channel(1); */
3484         /* ni_writew(devpriv->ao1p,AO_Configuration); */
3485
3486         ni_release_ao_mite_channel(dev);
3487
3488         devpriv->stc_writew(dev, AO_Configuration_Start, Joint_Reset_Register);
3489         devpriv->stc_writew(dev, AO_Disarm, AO_Command_1_Register);
3490         ni_set_bits(dev, Interrupt_B_Enable_Register, ~0, 0);
3491         devpriv->stc_writew(dev, AO_BC_Source_Select, AO_Personal_Register);
3492         devpriv->stc_writew(dev, 0x3f98, Interrupt_B_Ack_Register);
3493         devpriv->stc_writew(dev, AO_BC_Source_Select | AO_UPDATE_Pulse_Width |
3494                             AO_TMRDACWR_Pulse_Width, AO_Personal_Register);
3495         devpriv->stc_writew(dev, 0, AO_Output_Control_Register);
3496         devpriv->stc_writew(dev, 0, AO_Start_Select_Register);
3497         devpriv->ao_cmd1 = 0;
3498         devpriv->stc_writew(dev, devpriv->ao_cmd1, AO_Command_1_Register);
3499         devpriv->ao_cmd2 = 0;
3500         devpriv->stc_writew(dev, devpriv->ao_cmd2, AO_Command_2_Register);
3501         devpriv->ao_mode1 = 0;
3502         devpriv->stc_writew(dev, devpriv->ao_mode1, AO_Mode_1_Register);
3503         devpriv->ao_mode2 = 0;
3504         devpriv->stc_writew(dev, devpriv->ao_mode2, AO_Mode_2_Register);
3505         if (board->reg_type & ni_reg_m_series_mask)
3506                 devpriv->ao_mode3 = AO_Last_Gate_Disable;
3507         else
3508                 devpriv->ao_mode3 = 0;
3509         devpriv->stc_writew(dev, devpriv->ao_mode3, AO_Mode_3_Register);
3510         devpriv->ao_trigger_select = 0;
3511         devpriv->stc_writew(dev, devpriv->ao_trigger_select,
3512                             AO_Trigger_Select_Register);
3513         if (board->reg_type & ni_reg_6xxx_mask) {
3514                 unsigned immediate_bits = 0;
3515                 unsigned i;
3516                 for (i = 0; i < s->n_chan; ++i) {
3517                         immediate_bits |= 1 << i;
3518                 }
3519                 ao_win_out(immediate_bits, AO_Immediate_671x);
3520                 ao_win_out(CLEAR_WG, AO_Misc_611x);
3521         }
3522         devpriv->stc_writew(dev, AO_Configuration_End, Joint_Reset_Register);
3523
3524         return 0;
3525 }
3526
3527 /* digital io */
3528
3529 static int ni_dio_insn_config(struct comedi_device *dev,
3530                               struct comedi_subdevice *s,
3531                               struct comedi_insn *insn,
3532                               unsigned int *data)
3533 {
3534         struct ni_private *devpriv = dev->private;
3535         int ret;
3536
3537         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3538         if (ret)
3539                 return ret;
3540
3541         devpriv->dio_control &= ~DIO_Pins_Dir_Mask;
3542         devpriv->dio_control |= DIO_Pins_Dir(s->io_bits);
3543         devpriv->stc_writew(dev, devpriv->dio_control, DIO_Control_Register);
3544
3545         return insn->n;
3546 }
3547
3548 static int ni_dio_insn_bits(struct comedi_device *dev,
3549                             struct comedi_subdevice *s,
3550                             struct comedi_insn *insn,
3551                             unsigned int *data)
3552 {
3553         struct ni_private *devpriv = dev->private;
3554
3555         /* Make sure we're not using the serial part of the dio */
3556         if ((data[0] & (DIO_SDIN | DIO_SDOUT)) && devpriv->serial_interval_ns)
3557                 return -EBUSY;
3558
3559         if (comedi_dio_update_state(s, data)) {
3560                 devpriv->dio_output &= ~DIO_Parallel_Data_Mask;
3561                 devpriv->dio_output |= DIO_Parallel_Data_Out(s->state);
3562                 devpriv->stc_writew(dev, devpriv->dio_output,
3563                                     DIO_Output_Register);
3564         }
3565
3566         data[1] = devpriv->stc_readw(dev, DIO_Parallel_Input_Register);
3567
3568         return insn->n;
3569 }
3570
3571 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3572                                        struct comedi_subdevice *s,
3573                                        struct comedi_insn *insn,
3574                                        unsigned int *data)
3575 {
3576         struct ni_private *devpriv __maybe_unused = dev->private;
3577         int ret;
3578
3579         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3580         if (ret)
3581                 return ret;
3582
3583         ni_writel(s->io_bits, M_Offset_DIO_Direction);
3584
3585         return insn->n;
3586 }
3587
3588 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3589                                      struct comedi_subdevice *s,
3590                                      struct comedi_insn *insn,
3591                                      unsigned int *data)
3592 {
3593         struct ni_private *devpriv __maybe_unused = dev->private;
3594
3595         if (comedi_dio_update_state(s, data))
3596                 ni_writel(s->state, M_Offset_Static_Digital_Output);
3597
3598         data[1] = ni_readl(M_Offset_Static_Digital_Input);
3599
3600         return insn->n;
3601 }
3602
3603 static int ni_cdio_cmdtest(struct comedi_device *dev,
3604                            struct comedi_subdevice *s, struct comedi_cmd *cmd)
3605 {
3606         int err = 0;
3607         int tmp;
3608         unsigned i;
3609
3610         /* Step 1 : check if triggers are trivially valid */
3611
3612         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_INT);
3613         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3614         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3615         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3616         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3617
3618         if (err)
3619                 return 1;
3620
3621         /* Step 2a : make sure trigger sources are unique */
3622         /* Step 2b : and mutually compatible */
3623
3624         if (err)
3625                 return 2;
3626
3627         /* Step 3: check if arguments are trivially valid */
3628
3629         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
3630
3631         tmp = cmd->scan_begin_arg;
3632         tmp &= CR_PACK_FLAGS(CDO_Sample_Source_Select_Mask, 0, 0, CR_INVERT);
3633         if (tmp != cmd->scan_begin_arg)
3634                 err |= -EINVAL;
3635
3636         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
3637         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
3638         err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
3639
3640         if (err)
3641                 return 3;
3642
3643         /* step 4: fix up any arguments */
3644
3645         if (err)
3646                 return 4;
3647
3648         /* step 5: check chanlist */
3649
3650         for (i = 0; i < cmd->chanlist_len; ++i) {
3651                 if (cmd->chanlist[i] != i)
3652                         err = 1;
3653         }
3654
3655         if (err)
3656                 return 5;
3657
3658         return 0;
3659 }
3660
3661 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3662 {
3663         struct ni_private *devpriv __maybe_unused = dev->private;
3664         const struct comedi_cmd *cmd = &s->async->cmd;
3665         unsigned cdo_mode_bits = CDO_FIFO_Mode_Bit | CDO_Halt_On_Error_Bit;
3666         int retval;
3667
3668         ni_writel(CDO_Reset_Bit, M_Offset_CDIO_Command);
3669         switch (cmd->scan_begin_src) {
3670         case TRIG_EXT:
3671                 cdo_mode_bits |=
3672                     CR_CHAN(cmd->scan_begin_arg) &
3673                     CDO_Sample_Source_Select_Mask;
3674                 break;
3675         default:
3676                 BUG();
3677                 break;
3678         }
3679         if (cmd->scan_begin_arg & CR_INVERT)
3680                 cdo_mode_bits |= CDO_Polarity_Bit;
3681         ni_writel(cdo_mode_bits, M_Offset_CDO_Mode);
3682         if (s->io_bits) {
3683                 ni_writel(s->state, M_Offset_CDO_FIFO_Data);
3684                 ni_writel(CDO_SW_Update_Bit, M_Offset_CDIO_Command);
3685                 ni_writel(s->io_bits, M_Offset_CDO_Mask_Enable);
3686         } else {
3687                 comedi_error(dev,
3688                              "attempted to run digital output command with no lines configured as outputs");
3689                 return -EIO;
3690         }
3691         retval = ni_request_cdo_mite_channel(dev);
3692         if (retval < 0) {
3693                 return retval;
3694         }
3695         s->async->inttrig = &ni_cdo_inttrig;
3696         return 0;
3697 }
3698
3699 static int ni_cdo_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
3700                           unsigned int trignum)
3701 {
3702 #ifdef PCIDMA
3703         struct ni_private *devpriv = dev->private;
3704         unsigned long flags;
3705 #endif
3706         int retval = 0;
3707         unsigned i;
3708         const unsigned timeout = 1000;
3709
3710         s->async->inttrig = NULL;
3711
3712         /* read alloc the entire buffer */
3713         comedi_buf_read_alloc(s->async, s->async->prealloc_bufsz);
3714
3715 #ifdef PCIDMA
3716         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3717         if (devpriv->cdo_mite_chan) {
3718                 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3719                 mite_dma_arm(devpriv->cdo_mite_chan);
3720         } else {
3721                 comedi_error(dev, "BUG: no cdo mite channel?");
3722                 retval = -EIO;
3723         }
3724         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3725         if (retval < 0)
3726                 return retval;
3727 #endif
3728 /*
3729 * XXX not sure what interrupt C group does
3730 * ni_writeb(Interrupt_Group_C_Enable_Bit,
3731 * M_Offset_Interrupt_C_Enable); wait for dma to fill output fifo
3732 */
3733         for (i = 0; i < timeout; ++i) {
3734                 if (ni_readl(M_Offset_CDIO_Status) & CDO_FIFO_Full_Bit)
3735                         break;
3736                 udelay(10);
3737         }
3738         if (i == timeout) {
3739                 comedi_error(dev, "dma failed to fill cdo fifo!");
3740                 ni_cdio_cancel(dev, s);
3741                 return -EIO;
3742         }
3743         ni_writel(CDO_Arm_Bit | CDO_Error_Interrupt_Enable_Set_Bit |
3744                   CDO_Empty_FIFO_Interrupt_Enable_Set_Bit,
3745                   M_Offset_CDIO_Command);
3746         return retval;
3747 }
3748
3749 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3750 {
3751         struct ni_private *devpriv __maybe_unused = dev->private;
3752
3753         ni_writel(CDO_Disarm_Bit | CDO_Error_Interrupt_Enable_Clear_Bit |
3754                   CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit |
3755                   CDO_FIFO_Request_Interrupt_Enable_Clear_Bit,
3756                   M_Offset_CDIO_Command);
3757 /*
3758 * XXX not sure what interrupt C group does ni_writeb(0,
3759 * M_Offset_Interrupt_C_Enable);
3760 */
3761         ni_writel(0, M_Offset_CDO_Mask_Enable);
3762         ni_release_cdo_mite_channel(dev);
3763         return 0;
3764 }
3765
3766 static void handle_cdio_interrupt(struct comedi_device *dev)
3767 {
3768         const struct ni_board_struct *board = comedi_board(dev);
3769         struct ni_private *devpriv __maybe_unused = dev->private;
3770         unsigned cdio_status;
3771         struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3772 #ifdef PCIDMA
3773         unsigned long flags;
3774 #endif
3775
3776         if ((board->reg_type & ni_reg_m_series_mask) == 0) {
3777                 return;
3778         }
3779 #ifdef PCIDMA
3780         spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3781         if (devpriv->cdo_mite_chan) {
3782                 unsigned cdo_mite_status =
3783                     mite_get_status(devpriv->cdo_mite_chan);
3784                 if (cdo_mite_status & CHSR_LINKC) {
3785                         writel(CHOR_CLRLC,
3786                                devpriv->mite->mite_io_addr +
3787                                MITE_CHOR(devpriv->cdo_mite_chan->channel));
3788                 }
3789                 mite_sync_output_dma(devpriv->cdo_mite_chan, s->async);
3790         }
3791         spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3792 #endif
3793
3794         cdio_status = ni_readl(M_Offset_CDIO_Status);
3795         if (cdio_status & (CDO_Overrun_Bit | CDO_Underflow_Bit)) {
3796 /* printk("cdio error: statux=0x%x\n", cdio_status); */
3797                 ni_writel(CDO_Error_Interrupt_Confirm_Bit, M_Offset_CDIO_Command);      /*  XXX just guessing this is needed and does something useful */
3798                 s->async->events |= COMEDI_CB_OVERFLOW;
3799         }
3800         if (cdio_status & CDO_FIFO_Empty_Bit) {
3801 /* printk("cdio fifo empty\n"); */
3802                 ni_writel(CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit,
3803                           M_Offset_CDIO_Command);
3804 /* s->async->events |= COMEDI_CB_EOA; */
3805         }
3806         ni_event(dev, s);
3807 }
3808
3809 static int ni_serial_insn_config(struct comedi_device *dev,
3810                                  struct comedi_subdevice *s,
3811                                  struct comedi_insn *insn, unsigned int *data)
3812 {
3813         struct ni_private *devpriv = dev->private;
3814         int err = insn->n;
3815         unsigned char byte_out, byte_in = 0;
3816
3817         if (insn->n != 2)
3818                 return -EINVAL;
3819
3820         switch (data[0]) {
3821         case INSN_CONFIG_SERIAL_CLOCK:
3822
3823 #ifdef DEBUG_DIO
3824                 printk("SPI serial clock Config cd\n", data[1]);
3825 #endif
3826                 devpriv->serial_hw_mode = 1;
3827                 devpriv->dio_control |= DIO_HW_Serial_Enable;
3828
3829                 if (data[1] == SERIAL_DISABLED) {
3830                         devpriv->serial_hw_mode = 0;
3831                         devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
3832                                                   DIO_Software_Serial_Control);
3833                         data[1] = SERIAL_DISABLED;
3834                         devpriv->serial_interval_ns = data[1];
3835                 } else if (data[1] <= SERIAL_600NS) {
3836                         /* Warning: this clock speed is too fast to reliably
3837                            control SCXI. */
3838                         devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
3839                         devpriv->clock_and_fout |= Slow_Internal_Timebase;
3840                         devpriv->clock_and_fout &= ~DIO_Serial_Out_Divide_By_2;
3841                         data[1] = SERIAL_600NS;
3842                         devpriv->serial_interval_ns = data[1];
3843                 } else if (data[1] <= SERIAL_1_2US) {
3844                         devpriv->dio_control &= ~DIO_HW_Serial_Timebase;
3845                         devpriv->clock_and_fout |= Slow_Internal_Timebase |
3846                             DIO_Serial_Out_Divide_By_2;
3847                         data[1] = SERIAL_1_2US;
3848                         devpriv->serial_interval_ns = data[1];
3849                 } else if (data[1] <= SERIAL_10US) {
3850                         devpriv->dio_control |= DIO_HW_Serial_Timebase;
3851                         devpriv->clock_and_fout |= Slow_Internal_Timebase |
3852                             DIO_Serial_Out_Divide_By_2;
3853                         /* Note: DIO_Serial_Out_Divide_By_2 only affects
3854                            600ns/1.2us. If you turn divide_by_2 off with the
3855                            slow clock, you will still get 10us, except then
3856                            all your delays are wrong. */
3857                         data[1] = SERIAL_10US;
3858                         devpriv->serial_interval_ns = data[1];
3859                 } else {
3860                         devpriv->dio_control &= ~(DIO_HW_Serial_Enable |
3861                                                   DIO_Software_Serial_Control);
3862                         devpriv->serial_hw_mode = 0;
3863                         data[1] = (data[1] / 1000) * 1000;
3864                         devpriv->serial_interval_ns = data[1];
3865                 }
3866
3867                 devpriv->stc_writew(dev, devpriv->dio_control,
3868                                     DIO_Control_Register);
3869                 devpriv->stc_writew(dev, devpriv->clock_and_fout,
3870                                     Clock_and_FOUT_Register);
3871                 return 1;
3872
3873                 break;
3874
3875         case INSN_CONFIG_BIDIRECTIONAL_DATA:
3876
3877                 if (devpriv->serial_interval_ns == 0) {
3878                         return -EINVAL;
3879                 }
3880
3881                 byte_out = data[1] & 0xFF;
3882
3883                 if (devpriv->serial_hw_mode) {
3884                         err = ni_serial_hw_readwrite8(dev, s, byte_out,
3885                                                       &byte_in);
3886                 } else if (devpriv->serial_interval_ns > 0) {
3887                         err = ni_serial_sw_readwrite8(dev, s, byte_out,
3888                                                       &byte_in);
3889                 } else {
3890                         printk("ni_serial_insn_config: serial disabled!\n");
3891                         return -EINVAL;
3892                 }
3893                 if (err < 0)
3894                         return err;
3895                 data[1] = byte_in & 0xFF;
3896                 return insn->n;
3897
3898                 break;
3899         default:
3900                 return -EINVAL;
3901         }
3902
3903 }
3904
3905 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3906                                    struct comedi_subdevice *s,
3907                                    unsigned char data_out,
3908                                    unsigned char *data_in)
3909 {
3910         struct ni_private *devpriv = dev->private;
3911         unsigned int status1;
3912         int err = 0, count = 20;
3913
3914 #ifdef DEBUG_DIO
3915         printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out);
3916 #endif
3917
3918         devpriv->dio_output &= ~DIO_Serial_Data_Mask;
3919         devpriv->dio_output |= DIO_Serial_Data_Out(data_out);
3920         devpriv->stc_writew(dev, devpriv->dio_output, DIO_Output_Register);
3921
3922         status1 = devpriv->stc_readw(dev, Joint_Status_1_Register);
3923         if (status1 & DIO_Serial_IO_In_Progress_St) {
3924                 err = -EBUSY;
3925                 goto Error;
3926         }
3927
3928         devpriv->dio_control |= DIO_HW_Serial_Start;
3929         devpriv->stc_writew(dev, devpriv->dio_control, DIO_Control_Register);
3930         devpriv->dio_control &= ~DIO_HW_Serial_Start;
3931
3932         /* Wait until STC says we're done, but don't loop infinitely. */
3933         while ((status1 =
3934                 devpriv->stc_readw(dev,
3935                                    Joint_Status_1_Register)) &
3936                DIO_Serial_IO_In_Progress_St) {
3937                 /* Delay one bit per loop */
3938                 udelay((devpriv->serial_interval_ns + 999) / 1000);
3939                 if (--count < 0) {
3940                         printk
3941                             ("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
3942                         err = -ETIME;
3943                         goto Error;
3944                 }
3945         }
3946
3947         /* Delay for last bit. This delay is absolutely necessary, because
3948            DIO_Serial_IO_In_Progress_St goes high one bit too early. */
3949         udelay((devpriv->serial_interval_ns + 999) / 1000);
3950
3951         if (data_in != NULL) {
3952                 *data_in = devpriv->stc_readw(dev, DIO_Serial_Input_Register);
3953 #ifdef DEBUG_DIO
3954                 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in);
3955 #endif
3956         }
3957
3958 Error:
3959         devpriv->stc_writew(dev, devpriv->dio_control, DIO_Control_Register);
3960
3961         return err;
3962 }
3963
3964 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3965                                    struct comedi_subdevice *s,
3966                                    unsigned char data_out,
3967                                    unsigned char *data_in)
3968 {
3969         struct ni_private *devpriv = dev->private;
3970         unsigned char mask, input = 0;
3971
3972 #ifdef DEBUG_DIO
3973         printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out);
3974 #endif
3975
3976         /* Wait for one bit before transfer */
3977         udelay((devpriv->serial_interval_ns + 999) / 1000);
3978
3979         for (mask = 0x80; mask; mask >>= 1) {
3980                 /* Output current bit; note that we cannot touch s->state
3981                    because it is a per-subdevice field, and serial is
3982                    a separate subdevice from DIO. */
3983                 devpriv->dio_output &= ~DIO_SDOUT;
3984                 if (data_out & mask) {
3985                         devpriv->dio_output |= DIO_SDOUT;
3986                 }
3987                 devpriv->stc_writew(dev, devpriv->dio_output,
3988                                     DIO_Output_Register);
3989
3990                 /* Assert SDCLK (active low, inverted), wait for half of
3991                    the delay, deassert SDCLK, and wait for the other half. */
3992                 devpriv->dio_control |= DIO_Software_Serial_Control;
3993                 devpriv->stc_writew(dev, devpriv->dio_control,
3994                                     DIO_Control_Register);
3995
3996                 udelay((devpriv->serial_interval_ns + 999) / 2000);
3997
3998                 devpriv->dio_control &= ~DIO_Software_Serial_Control;
3999                 devpriv->stc_writew(dev, devpriv->dio_control,
4000                                     DIO_Control_Register);
4001
4002                 udelay((devpriv->serial_interval_ns + 999) / 2000);
4003
4004                 /* Input current bit */
4005                 if (devpriv->stc_readw(dev,
4006                                        DIO_Parallel_Input_Register) & DIO_SDIN)
4007                 {
4008 /*                      printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
4009                         input |= mask;
4010                 }
4011         }
4012 #ifdef DEBUG_DIO
4013         printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input);
4014 #endif
4015         if (data_in)
4016                 *data_in = input;
4017
4018         return 0;
4019 }
4020
4021 static void mio_common_detach(struct comedi_device *dev)
4022 {
4023         struct ni_private *devpriv = dev->private;
4024
4025         if (devpriv) {
4026                 if (devpriv->counter_dev) {
4027                         ni_gpct_device_destroy(devpriv->counter_dev);
4028                 }
4029         }
4030 }
4031
4032 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
4033 {
4034         int i;
4035
4036         for (i = 0; i < s->n_chan; i++) {
4037                 ni_ao_win_outw(dev, AO_Channel(i) | 0x0,
4038                                AO_Configuration_2_67xx);
4039         }
4040         ao_win_out(0x0, AO_Later_Single_Point_Updates);
4041 }
4042
4043 static unsigned ni_gpct_to_stc_register(enum ni_gpct_register reg)
4044 {
4045         unsigned stc_register;
4046         switch (reg) {
4047         case NITIO_G0_Autoincrement_Reg:
4048                 stc_register = G_Autoincrement_Register(0);
4049                 break;
4050         case NITIO_G1_Autoincrement_Reg:
4051                 stc_register = G_Autoincrement_Register(1);
4052                 break;
4053         case NITIO_G0_Command_Reg:
4054                 stc_register = G_Command_Register(0);
4055                 break;
4056         case NITIO_G1_Command_Reg:
4057                 stc_register = G_Command_Register(1);
4058                 break;
4059         case NITIO_G0_HW_Save_Reg:
4060                 stc_register = G_HW_Save_Register(0);
4061                 break;
4062         case NITIO_G1_HW_Save_Reg:
4063                 stc_register = G_HW_Save_Register(1);
4064                 break;
4065         case NITIO_G0_SW_Save_Reg:
4066                 stc_register = G_Save_Register(0);
4067                 break;
4068         case NITIO_G1_SW_Save_Reg:
4069                 stc_register = G_Save_Register(1);
4070                 break;
4071         case NITIO_G0_Mode_Reg:
4072                 stc_register = G_Mode_Register(0);
4073                 break;
4074         case NITIO_G1_Mode_Reg:
4075                 stc_register = G_Mode_Register(1);
4076                 break;
4077         case NITIO_G0_LoadA_Reg:
4078                 stc_register = G_Load_A_Register(0);
4079                 break;
4080         case NITIO_G1_LoadA_Reg:
4081                 stc_register = G_Load_A_Register(1);
4082                 break;
4083         case NITIO_G0_LoadB_Reg:
4084                 stc_register = G_Load_B_Register(0);
4085                 break;
4086         case NITIO_G1_LoadB_Reg:
4087                 stc_register = G_Load_B_Register(1);
4088                 break;
4089         case NITIO_G0_Input_Select_Reg:
4090                 stc_register = G_Input_Select_Register(0);
4091                 break;
4092         case NITIO_G1_Input_Select_Reg:
4093                 stc_register = G_Input_Select_Register(1);
4094                 break;
4095         case NITIO_G01_Status_Reg:
4096                 stc_register = G_Status_Register;
4097                 break;
4098         case NITIO_G01_Joint_Reset_Reg:
4099                 stc_register = Joint_Reset_Register;
4100                 break;
4101         case NITIO_G01_Joint_Status1_Reg:
4102                 stc_register = Joint_Status_1_Register;
4103                 break;
4104         case NITIO_G01_Joint_Status2_Reg:
4105                 stc_register = Joint_Status_2_Register;
4106                 break;
4107         case NITIO_G0_Interrupt_Acknowledge_Reg:
4108                 stc_register = Interrupt_A_Ack_Register;
4109                 break;
4110         case NITIO_G1_Interrupt_Acknowledge_Reg:
4111                 stc_register = Interrupt_B_Ack_Register;
4112                 break;
4113         case NITIO_G0_Status_Reg:
4114                 stc_register = AI_Status_1_Register;
4115                 break;
4116         case NITIO_G1_Status_Reg:
4117                 stc_register = AO_Status_1_Register;
4118                 break;
4119         case NITIO_G0_Interrupt_Enable_Reg:
4120                 stc_register = Interrupt_A_Enable_Register;
4121                 break;
4122         case NITIO_G1_Interrupt_Enable_Reg:
4123                 stc_register = Interrupt_B_Enable_Register;
4124                 break;
4125         default:
4126                 printk("%s: unhandled register 0x%x in switch.\n",
4127                        __func__, reg);
4128                 BUG();
4129                 return 0;
4130                 break;
4131         }
4132         return stc_register;
4133 }
4134
4135 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned bits,
4136                                    enum ni_gpct_register reg)
4137 {
4138         struct comedi_device *dev = counter->counter_dev->dev;
4139         struct ni_private *devpriv = dev->private;
4140         unsigned stc_register;
4141         /* bits in the join reset register which are relevant to counters */
4142         static const unsigned gpct_joint_reset_mask = G0_Reset | G1_Reset;
4143         static const unsigned gpct_interrupt_a_enable_mask =
4144             G0_Gate_Interrupt_Enable | G0_TC_Interrupt_Enable;
4145         static const unsigned gpct_interrupt_b_enable_mask =
4146             G1_Gate_Interrupt_Enable | G1_TC_Interrupt_Enable;
4147
4148         switch (reg) {
4149                 /* m-series-only registers */
4150         case NITIO_G0_Counting_Mode_Reg:
4151                 ni_writew(bits, M_Offset_G0_Counting_Mode);
4152                 break;
4153         case NITIO_G1_Counting_Mode_Reg:
4154                 ni_writew(bits, M_Offset_G1_Counting_Mode);
4155                 break;
4156         case NITIO_G0_Second_Gate_Reg:
4157                 ni_writew(bits, M_Offset_G0_Second_Gate);
4158                 break;
4159         case NITIO_G1_Second_Gate_Reg:
4160                 ni_writew(bits, M_Offset_G1_Second_Gate);
4161                 break;
4162         case NITIO_G0_DMA_Config_Reg:
4163                 ni_writew(bits, M_Offset_G0_DMA_Config);
4164                 break;
4165         case NITIO_G1_DMA_Config_Reg:
4166                 ni_writew(bits, M_Offset_G1_DMA_Config);
4167                 break;
4168         case NITIO_G0_ABZ_Reg:
4169                 ni_writew(bits, M_Offset_G0_MSeries_ABZ);
4170                 break;
4171         case NITIO_G1_ABZ_Reg:
4172                 ni_writew(bits, M_Offset_G1_MSeries_ABZ);
4173                 break;
4174
4175                 /* 32 bit registers */
4176         case NITIO_G0_LoadA_Reg:
4177         case NITIO_G1_LoadA_Reg:
4178         case NITIO_G0_LoadB_Reg:
4179         case NITIO_G1_LoadB_Reg:
4180                 stc_register = ni_gpct_to_stc_register(reg);
4181                 devpriv->stc_writel(dev, bits, stc_register);
4182                 break;
4183
4184                 /* 16 bit registers */
4185         case NITIO_G0_Interrupt_Enable_Reg:
4186                 BUG_ON(bits & ~gpct_interrupt_a_enable_mask);
4187                 ni_set_bitfield(dev, Interrupt_A_Enable_Register,
4188                                 gpct_interrupt_a_enable_mask, bits);
4189                 break;
4190         case NITIO_G1_Interrupt_Enable_Reg:
4191                 BUG_ON(bits & ~gpct_interrupt_b_enable_mask);
4192                 ni_set_bitfield(dev, Interrupt_B_Enable_Register,
4193                                 gpct_interrupt_b_enable_mask, bits);
4194                 break;
4195         case NITIO_G01_Joint_Reset_Reg:
4196                 BUG_ON(bits & ~gpct_joint_reset_mask);
4197                 /* fall-through */
4198         default:
4199                 stc_register = ni_gpct_to_stc_register(reg);
4200                 devpriv->stc_writew(dev, bits, stc_register);
4201         }
4202 }
4203
4204 static unsigned ni_gpct_read_register(struct ni_gpct *counter,
4205                                       enum ni_gpct_register reg)
4206 {
4207         struct comedi_device *dev = counter->counter_dev->dev;
4208         struct ni_private *devpriv = dev->private;
4209         unsigned stc_register;
4210
4211         switch (reg) {
4212                 /* m-series only registers */
4213         case NITIO_G0_DMA_Status_Reg:
4214                 return ni_readw(M_Offset_G0_DMA_Status);
4215                 break;
4216         case NITIO_G1_DMA_Status_Reg:
4217                 return ni_readw(M_Offset_G1_DMA_Status);
4218                 break;
4219
4220                 /* 32 bit registers */
4221         case NITIO_G0_HW_Save_Reg:
4222         case NITIO_G1_HW_Save_Reg:
4223         case NITIO_G0_SW_Save_Reg:
4224         case NITIO_G1_SW_Save_Reg:
4225                 stc_register = ni_gpct_to_stc_register(reg);
4226                 return devpriv->stc_readl(dev, stc_register);
4227                 break;
4228
4229                 /* 16 bit registers */
4230         default:
4231                 stc_register = ni_gpct_to_stc_register(reg);
4232                 return devpriv->stc_readw(dev, stc_register);
4233                 break;
4234         }
4235         return 0;
4236 }
4237
4238 static int ni_freq_out_insn_read(struct comedi_device *dev,
4239                                  struct comedi_subdevice *s,
4240                                  struct comedi_insn *insn, unsigned int *data)
4241 {
4242         struct ni_private *devpriv = dev->private;
4243
4244         data[0] = devpriv->clock_and_fout & FOUT_Divider_mask;
4245         return 1;
4246 }
4247
4248 static int ni_freq_out_insn_write(struct comedi_device *dev,
4249                                   struct comedi_subdevice *s,
4250                                   struct comedi_insn *insn, unsigned int *data)
4251 {
4252         struct ni_private *devpriv = dev->private;
4253
4254         devpriv->clock_and_fout &= ~FOUT_Enable;
4255         devpriv->stc_writew(dev, devpriv->clock_and_fout,
4256                             Clock_and_FOUT_Register);
4257         devpriv->clock_and_fout &= ~FOUT_Divider_mask;
4258         devpriv->clock_and_fout |= FOUT_Divider(data[0]);
4259         devpriv->clock_and_fout |= FOUT_Enable;
4260         devpriv->stc_writew(dev, devpriv->clock_and_fout,
4261                             Clock_and_FOUT_Register);
4262         return insn->n;
4263 }
4264
4265 static int ni_set_freq_out_clock(struct comedi_device *dev,
4266                                  unsigned int clock_source)
4267 {
4268         struct ni_private *devpriv = dev->private;
4269
4270         switch (clock_source) {
4271         case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4272                 devpriv->clock_and_fout &= ~FOUT_Timebase_Select;
4273                 break;
4274         case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4275                 devpriv->clock_and_fout |= FOUT_Timebase_Select;
4276                 break;
4277         default:
4278                 return -EINVAL;
4279         }
4280         devpriv->stc_writew(dev, devpriv->clock_and_fout,
4281                             Clock_and_FOUT_Register);
4282         return 3;
4283 }
4284
4285 static void ni_get_freq_out_clock(struct comedi_device *dev,
4286                                   unsigned int *clock_source,
4287                                   unsigned int *clock_period_ns)
4288 {
4289         struct ni_private *devpriv = dev->private;
4290
4291         if (devpriv->clock_and_fout & FOUT_Timebase_Select) {
4292                 *clock_source = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4293                 *clock_period_ns = TIMEBASE_2_NS;
4294         } else {
4295                 *clock_source = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4296                 *clock_period_ns = TIMEBASE_1_NS * 2;
4297         }
4298 }
4299
4300 static int ni_freq_out_insn_config(struct comedi_device *dev,
4301                                    struct comedi_subdevice *s,
4302                                    struct comedi_insn *insn, unsigned int *data)
4303 {
4304         switch (data[0]) {
4305         case INSN_CONFIG_SET_CLOCK_SRC:
4306                 return ni_set_freq_out_clock(dev, data[1]);
4307                 break;
4308         case INSN_CONFIG_GET_CLOCK_SRC:
4309                 ni_get_freq_out_clock(dev, &data[1], &data[2]);
4310                 return 3;
4311         default:
4312                 break;
4313         }
4314         return -EINVAL;
4315 }
4316
4317 static int ni_alloc_private(struct comedi_device *dev)
4318 {
4319         struct ni_private *devpriv;
4320
4321         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
4322         if (!devpriv)
4323                 return -ENOMEM;
4324
4325         spin_lock_init(&devpriv->window_lock);
4326         spin_lock_init(&devpriv->soft_reg_copy_lock);
4327         spin_lock_init(&devpriv->mite_channel_lock);
4328
4329         return 0;
4330 };
4331
4332 static int ni_E_init(struct comedi_device *dev)
4333 {
4334         const struct ni_board_struct *board = comedi_board(dev);
4335         struct ni_private *devpriv = dev->private;
4336         struct comedi_subdevice *s;
4337         unsigned j;
4338         enum ni_gpct_variant counter_variant;
4339         int ret;
4340
4341         if (board->n_aochan > MAX_N_AO_CHAN) {
4342                 printk("bug! n_aochan > MAX_N_AO_CHAN\n");
4343                 return -EINVAL;
4344         }
4345
4346         ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
4347         if (ret)
4348                 return ret;
4349
4350         /* analog input subdevice */
4351
4352         s = &dev->subdevices[NI_AI_SUBDEV];
4353         dev->read_subdev = s;
4354         if (board->n_adchan) {
4355                 s->type = COMEDI_SUBD_AI;
4356                 s->subdev_flags =
4357                     SDF_READABLE | SDF_DIFF | SDF_DITHER | SDF_CMD_READ;
4358                 if (board->reg_type != ni_reg_611x)
4359                         s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
4360                 if (board->adbits > 16)
4361                         s->subdev_flags |= SDF_LSAMPL;
4362                 if (board->reg_type & ni_reg_m_series_mask)
4363                         s->subdev_flags |= SDF_SOFT_CALIBRATED;
4364                 s->n_chan = board->n_adchan;
4365                 s->len_chanlist = 512;
4366                 s->maxdata = (1 << board->adbits) - 1;
4367                 s->range_table = ni_range_lkup[board->gainlkup];
4368                 s->insn_read = &ni_ai_insn_read;
4369                 s->insn_config = &ni_ai_insn_config;
4370                 s->do_cmdtest = &ni_ai_cmdtest;
4371                 s->do_cmd = &ni_ai_cmd;
4372                 s->cancel = &ni_ai_reset;
4373                 s->poll = &ni_ai_poll;
4374                 s->munge = &ni_ai_munge;
4375 #ifdef PCIDMA
4376                 s->async_dma_dir = DMA_FROM_DEVICE;
4377 #endif
4378         } else {
4379                 s->type = COMEDI_SUBD_UNUSED;
4380         }
4381
4382         /* analog output subdevice */
4383
4384         s = &dev->subdevices[NI_AO_SUBDEV];
4385         if (board->n_aochan) {
4386                 s->type = COMEDI_SUBD_AO;
4387                 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
4388                 if (board->reg_type & ni_reg_m_series_mask)
4389                         s->subdev_flags |= SDF_SOFT_CALIBRATED;
4390                 s->n_chan = board->n_aochan;
4391                 s->maxdata = (1 << board->aobits) - 1;
4392                 s->range_table = board->ao_range_table;
4393                 s->insn_read = &ni_ao_insn_read;
4394                 if (board->reg_type & ni_reg_6xxx_mask) {
4395                         s->insn_write = &ni_ao_insn_write_671x;
4396                 } else {
4397                         s->insn_write = &ni_ao_insn_write;
4398                 }
4399                 s->insn_config = &ni_ao_insn_config;
4400 #ifdef PCIDMA
4401                 if (board->n_aochan) {
4402                         s->async_dma_dir = DMA_TO_DEVICE;
4403 #else
4404                 if (board->ao_fifo_depth) {
4405 #endif
4406                         dev->write_subdev = s;
4407                         s->subdev_flags |= SDF_CMD_WRITE;
4408                         s->do_cmd = &ni_ao_cmd;
4409                         s->do_cmdtest = &ni_ao_cmdtest;
4410                         s->len_chanlist = board->n_aochan;
4411                         if ((board->reg_type & ni_reg_m_series_mask) == 0)
4412                                 s->munge = ni_ao_munge;
4413                 }
4414                 s->cancel = &ni_ao_reset;
4415         } else {
4416                 s->type = COMEDI_SUBD_UNUSED;
4417         }
4418         if ((board->reg_type & ni_reg_67xx_mask))
4419                 init_ao_67xx(dev, s);
4420
4421         /* digital i/o subdevice */
4422
4423         s = &dev->subdevices[NI_DIO_SUBDEV];
4424         s->type = COMEDI_SUBD_DIO;
4425         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
4426         s->maxdata = 1;
4427         s->io_bits = 0;         /* all bits input */
4428         s->range_table = &range_digital;
4429         s->n_chan = board->num_p0_dio_channels;
4430         if (board->reg_type & ni_reg_m_series_mask) {
4431                 s->subdev_flags |=
4432                     SDF_LSAMPL | SDF_CMD_WRITE /* | SDF_CMD_READ */ ;
4433                 s->insn_bits = &ni_m_series_dio_insn_bits;
4434                 s->insn_config = &ni_m_series_dio_insn_config;
4435                 s->do_cmd = &ni_cdio_cmd;
4436                 s->do_cmdtest = &ni_cdio_cmdtest;
4437                 s->cancel = &ni_cdio_cancel;
4438                 s->async_dma_dir = DMA_BIDIRECTIONAL;
4439                 s->len_chanlist = s->n_chan;
4440
4441                 ni_writel(CDO_Reset_Bit | CDI_Reset_Bit, M_Offset_CDIO_Command);
4442                 ni_writel(s->io_bits, M_Offset_DIO_Direction);
4443         } else {
4444                 s->insn_bits = &ni_dio_insn_bits;
4445                 s->insn_config = &ni_dio_insn_config;
4446                 devpriv->dio_control = DIO_Pins_Dir(s->io_bits);
4447                 ni_writew(devpriv->dio_control, DIO_Control_Register);
4448         }
4449
4450         /* 8255 device */
4451         s = &dev->subdevices[NI_8255_DIO_SUBDEV];
4452         if (board->has_8255) {
4453                 subdev_8255_init(dev, s, ni_8255_callback, (unsigned long)dev);
4454         } else {
4455                 s->type = COMEDI_SUBD_UNUSED;
4456         }
4457
4458         /* formerly general purpose counter/timer device, but no longer used */
4459         s = &dev->subdevices[NI_UNUSED_SUBDEV];
4460         s->type = COMEDI_SUBD_UNUSED;
4461
4462         /* calibration subdevice -- ai and ao */
4463         s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
4464         s->type = COMEDI_SUBD_CALIB;
4465         if (board->reg_type & ni_reg_m_series_mask) {
4466                 /*  internal PWM analog output used for AI nonlinearity calibration */
4467                 s->subdev_flags = SDF_INTERNAL;
4468                 s->insn_config = &ni_m_series_pwm_config;
4469                 s->n_chan = 1;
4470                 s->maxdata = 0;
4471                 ni_writel(0x0, M_Offset_Cal_PWM);
4472         } else if (board->reg_type == ni_reg_6143) {
4473                 /*  internal PWM analog output used for AI nonlinearity calibration */
4474                 s->subdev_flags = SDF_INTERNAL;
4475                 s->insn_config = &ni_6143_pwm_config;
4476                 s->n_chan = 1;
4477                 s->maxdata = 0;
4478         } else {
4479                 s->subdev_flags = SDF_WRITABLE | SDF_INTERNAL;
4480                 s->insn_read = &ni_calib_insn_read;
4481                 s->insn_write = &ni_calib_insn_write;
4482                 caldac_setup(dev, s);
4483         }
4484
4485         /* EEPROM */
4486         s = &dev->subdevices[NI_EEPROM_SUBDEV];
4487         s->type = COMEDI_SUBD_MEMORY;
4488         s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
4489         s->maxdata = 0xff;
4490         if (board->reg_type & ni_reg_m_series_mask) {
4491                 s->n_chan = M_SERIES_EEPROM_SIZE;
4492                 s->insn_read = &ni_m_series_eeprom_insn_read;
4493         } else {
4494                 s->n_chan = 512;
4495                 s->insn_read = &ni_eeprom_insn_read;
4496         }
4497
4498         /* PFI */
4499         s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
4500         s->type = COMEDI_SUBD_DIO;
4501         s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
4502         if (board->reg_type & ni_reg_m_series_mask) {
4503                 unsigned i;
4504                 s->n_chan = 16;
4505                 ni_writew(s->state, M_Offset_PFI_DO);
4506                 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
4507                         ni_writew(devpriv->pfi_output_select_reg[i],
4508                                   M_Offset_PFI_Output_Select(i + 1));
4509                 }
4510         } else {
4511                 s->n_chan = 10;
4512         }
4513         s->maxdata = 1;
4514         if (board->reg_type & ni_reg_m_series_mask) {
4515                 s->insn_bits = &ni_pfi_insn_bits;
4516         }
4517         s->insn_config = &ni_pfi_insn_config;
4518         ni_set_bits(dev, IO_Bidirection_Pin_Register, ~0, 0);
4519
4520         /* cs5529 calibration adc */
4521         s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
4522         if (board->reg_type & ni_reg_67xx_mask) {
4523                 s->type = COMEDI_SUBD_AI;
4524                 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
4525                 /*  one channel for each analog output channel */
4526                 s->n_chan = board->n_aochan;
4527                 s->maxdata = (1 << 16) - 1;
4528                 s->range_table = &range_unknown;        /* XXX */
4529                 s->insn_read = cs5529_ai_insn_read;
4530                 s->insn_config = NULL;
4531                 init_cs5529(dev);
4532         } else {
4533                 s->type = COMEDI_SUBD_UNUSED;
4534         }
4535
4536         /* Serial */
4537         s = &dev->subdevices[NI_SERIAL_SUBDEV];
4538         s->type = COMEDI_SUBD_SERIAL;
4539         s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
4540         s->n_chan = 1;
4541         s->maxdata = 0xff;
4542         s->insn_config = ni_serial_insn_config;
4543         devpriv->serial_interval_ns = 0;
4544         devpriv->serial_hw_mode = 0;
4545
4546         /* RTSI */
4547         s = &dev->subdevices[NI_RTSI_SUBDEV];
4548         s->type = COMEDI_SUBD_DIO;
4549         s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
4550         s->n_chan = 8;
4551         s->maxdata = 1;
4552         s->insn_bits = ni_rtsi_insn_bits;
4553         s->insn_config = ni_rtsi_insn_config;
4554         ni_rtsi_init(dev);
4555
4556         if (board->reg_type & ni_reg_m_series_mask) {
4557                 counter_variant = ni_gpct_variant_m_series;
4558         } else {
4559                 counter_variant = ni_gpct_variant_e_series;
4560         }
4561         devpriv->counter_dev = ni_gpct_device_construct(dev,
4562                                                         &ni_gpct_write_register,
4563                                                         &ni_gpct_read_register,
4564                                                         counter_variant,
4565                                                         NUM_GPCT);
4566         /* General purpose counters */
4567         for (j = 0; j < NUM_GPCT; ++j) {
4568                 s = &dev->subdevices[NI_GPCT_SUBDEV(j)];
4569                 s->type = COMEDI_SUBD_COUNTER;
4570                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
4571                 s->n_chan = 3;
4572                 if (board->reg_type & ni_reg_m_series_mask)
4573                         s->maxdata = 0xffffffff;
4574                 else
4575                         s->maxdata = 0xffffff;
4576                 s->insn_read = &ni_gpct_insn_read;
4577                 s->insn_write = &ni_gpct_insn_write;
4578                 s->insn_config = &ni_gpct_insn_config;
4579 #ifdef PCIDMA
4580                 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
4581                 s->do_cmd = &ni_gpct_cmd;
4582                 s->len_chanlist = 1;
4583                 s->do_cmdtest = &ni_gpct_cmdtest;
4584                 s->cancel = &ni_gpct_cancel;
4585                 s->async_dma_dir = DMA_BIDIRECTIONAL;
4586 #endif
4587                 s->private = &devpriv->counter_dev->counters[j];
4588
4589                 devpriv->counter_dev->counters[j].chip_index = 0;
4590                 devpriv->counter_dev->counters[j].counter_index = j;
4591                 ni_tio_init_counter(&devpriv->counter_dev->counters[j]);
4592         }
4593
4594         /* Frequency output */
4595         s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
4596         s->type = COMEDI_SUBD_COUNTER;
4597         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
4598         s->n_chan = 1;
4599         s->maxdata = 0xf;
4600         s->insn_read = &ni_freq_out_insn_read;
4601         s->insn_write = &ni_freq_out_insn_write;
4602         s->insn_config = &ni_freq_out_insn_config;
4603
4604         /* ai configuration */
4605         s = &dev->subdevices[NI_AI_SUBDEV];
4606         ni_ai_reset(dev, s);
4607         if ((board->reg_type & ni_reg_6xxx_mask) == 0) {
4608                 /*  BEAM is this needed for PCI-6143 ?? */
4609                 devpriv->clock_and_fout =
4610                     Slow_Internal_Time_Divide_By_2 |
4611                     Slow_Internal_Timebase |
4612                     Clock_To_Board_Divide_By_2 |
4613                     Clock_To_Board |
4614                     AI_Output_Divide_By_2 | AO_Output_Divide_By_2;
4615         } else {
4616                 devpriv->clock_and_fout =
4617                     Slow_Internal_Time_Divide_By_2 |
4618                     Slow_Internal_Timebase |
4619                     Clock_To_Board_Divide_By_2 | Clock_To_Board;
4620         }
4621         devpriv->stc_writew(dev, devpriv->clock_and_fout,
4622                             Clock_and_FOUT_Register);
4623
4624         /* analog output configuration */
4625         s = &dev->subdevices[NI_AO_SUBDEV];
4626         ni_ao_reset(dev, s);
4627
4628         if (dev->irq) {
4629                 devpriv->stc_writew(dev,
4630                                     (IRQ_POLARITY ? Interrupt_Output_Polarity :
4631                                      0) | (Interrupt_Output_On_3_Pins & 0) |
4632                                     Interrupt_A_Enable | Interrupt_B_Enable |
4633                                     Interrupt_A_Output_Select(interrupt_pin
4634                                                               (dev->irq)) |
4635                                     Interrupt_B_Output_Select(interrupt_pin
4636                                                               (dev->irq)),
4637                                     Interrupt_Control_Register);
4638         }
4639
4640         /* DMA setup */
4641         ni_writeb(devpriv->ai_ao_select_reg, AI_AO_Select);
4642         ni_writeb(devpriv->g0_g1_select_reg, G0_G1_Select);
4643
4644         if (board->reg_type & ni_reg_6xxx_mask) {
4645                 ni_writeb(0, Magic_611x);
4646         } else if (board->reg_type & ni_reg_m_series_mask) {
4647                 int channel;
4648                 for (channel = 0; channel < board->n_aochan; ++channel) {
4649                         ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel));
4650                         ni_writeb(0x0,
4651                                   M_Offset_AO_Reference_Attenuation(channel));
4652                 }
4653                 ni_writeb(0x0, M_Offset_AO_Calibration);
4654         }
4655
4656         printk("\n");
4657         return 0;
4658 }
4659
4660 static int ni_8255_callback(int dir, int port, int data, unsigned long arg)
4661 {
4662         struct comedi_device *dev = (struct comedi_device *)arg;
4663         struct ni_private *devpriv __maybe_unused = dev->private;
4664
4665         if (dir) {
4666                 ni_writeb(data, Port_A + 2 * port);
4667                 return 0;
4668         } else {
4669                 return ni_readb(Port_A + 2 * port);
4670         }
4671 }
4672
4673 /*
4674         presents the EEPROM as a subdevice
4675 */
4676
4677 static int ni_eeprom_insn_read(struct comedi_device *dev,
4678                                struct comedi_subdevice *s,
4679                                struct comedi_insn *insn, unsigned int *data)
4680 {
4681         data[0] = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4682
4683         return 1;
4684 }
4685
4686 /*
4687         reads bytes out of eeprom
4688 */
4689
4690 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4691 {
4692         struct ni_private *devpriv __maybe_unused = dev->private;
4693         int bit;
4694         int bitstring;
4695
4696         bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4697         ni_writeb(0x04, Serial_Command);
4698         for (bit = 0x8000; bit; bit >>= 1) {
4699                 ni_writeb(0x04 | ((bit & bitstring) ? 0x02 : 0),
4700                           Serial_Command);
4701                 ni_writeb(0x05 | ((bit & bitstring) ? 0x02 : 0),
4702                           Serial_Command);
4703         }
4704         bitstring = 0;
4705         for (bit = 0x80; bit; bit >>= 1) {
4706                 ni_writeb(0x04, Serial_Command);
4707                 ni_writeb(0x05, Serial_Command);
4708                 bitstring |= ((ni_readb(XXX_Status) & PROMOUT) ? bit : 0);
4709         }
4710         ni_writeb(0x00, Serial_Command);
4711
4712         return bitstring;
4713 }
4714
4715 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4716                                         struct comedi_subdevice *s,
4717                                         struct comedi_insn *insn,
4718                                         unsigned int *data)
4719 {
4720         struct ni_private *devpriv = dev->private;
4721
4722         data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4723
4724         return 1;
4725 }
4726
4727 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4728 {
4729         struct ni_private *devpriv = dev->private;
4730
4731         data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4732         data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4733         return 3;
4734 }
4735
4736 static int ni_m_series_pwm_config(struct comedi_device *dev,
4737                                   struct comedi_subdevice *s,
4738                                   struct comedi_insn *insn, unsigned int *data)
4739 {
4740         struct ni_private *devpriv = dev->private;
4741         unsigned up_count, down_count;
4742
4743         switch (data[0]) {
4744         case INSN_CONFIG_PWM_OUTPUT:
4745                 switch (data[1]) {
4746                 case TRIG_ROUND_NEAREST:
4747                         up_count =
4748                             (data[2] +
4749                              devpriv->clock_ns / 2) / devpriv->clock_ns;
4750                         break;
4751                 case TRIG_ROUND_DOWN:
4752                         up_count = data[2] / devpriv->clock_ns;
4753                         break;
4754                 case TRIG_ROUND_UP:
4755                         up_count =
4756                             (data[2] + devpriv->clock_ns -
4757                              1) / devpriv->clock_ns;
4758                         break;
4759                 default:
4760                         return -EINVAL;
4761                         break;
4762                 }
4763                 switch (data[3]) {
4764                 case TRIG_ROUND_NEAREST:
4765                         down_count =
4766                             (data[4] +
4767                              devpriv->clock_ns / 2) / devpriv->clock_ns;
4768                         break;
4769                 case TRIG_ROUND_DOWN:
4770                         down_count = data[4] / devpriv->clock_ns;
4771                         break;
4772                 case TRIG_ROUND_UP:
4773                         down_count =
4774                             (data[4] + devpriv->clock_ns -
4775                              1) / devpriv->clock_ns;
4776                         break;
4777                 default:
4778                         return -EINVAL;
4779                         break;
4780                 }
4781                 if (up_count * devpriv->clock_ns != data[2] ||
4782                     down_count * devpriv->clock_ns != data[4]) {
4783                         data[2] = up_count * devpriv->clock_ns;
4784                         data[4] = down_count * devpriv->clock_ns;
4785                         return -EAGAIN;
4786                 }
4787                 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count) |
4788                           MSeries_Cal_PWM_Low_Time_Bits(down_count),
4789                           M_Offset_Cal_PWM);
4790                 devpriv->pwm_up_count = up_count;
4791                 devpriv->pwm_down_count = down_count;
4792                 return 5;
4793                 break;
4794         case INSN_CONFIG_GET_PWM_OUTPUT:
4795                 return ni_get_pwm_config(dev, data);
4796                 break;
4797         default:
4798                 return -EINVAL;
4799                 break;
4800         }
4801         return 0;
4802 }
4803
4804 static int ni_6143_pwm_config(struct comedi_device *dev,
4805                               struct comedi_subdevice *s,
4806                               struct comedi_insn *insn, unsigned int *data)
4807 {
4808         struct ni_private *devpriv = dev->private;
4809         unsigned up_count, down_count;
4810
4811         switch (data[0]) {
4812         case INSN_CONFIG_PWM_OUTPUT:
4813                 switch (data[1]) {
4814                 case TRIG_ROUND_NEAREST:
4815                         up_count =
4816                             (data[2] +
4817                              devpriv->clock_ns / 2) / devpriv->clock_ns;
4818                         break;
4819                 case TRIG_ROUND_DOWN:
4820                         up_count = data[2] / devpriv->clock_ns;
4821                         break;
4822                 case TRIG_ROUND_UP:
4823                         up_count =
4824                             (data[2] + devpriv->clock_ns -
4825                              1) / devpriv->clock_ns;
4826                         break;
4827                 default:
4828                         return -EINVAL;
4829                         break;
4830                 }
4831                 switch (data[3]) {
4832                 case TRIG_ROUND_NEAREST:
4833                         down_count =
4834                             (data[4] +
4835                              devpriv->clock_ns / 2) / devpriv->clock_ns;
4836                         break;
4837                 case TRIG_ROUND_DOWN:
4838                         down_count = data[4] / devpriv->clock_ns;
4839                         break;
4840                 case TRIG_ROUND_UP:
4841                         down_count =
4842                             (data[4] + devpriv->clock_ns -
4843                              1) / devpriv->clock_ns;
4844                         break;
4845                 default:
4846                         return -EINVAL;
4847                         break;
4848                 }
4849                 if (up_count * devpriv->clock_ns != data[2] ||
4850                     down_count * devpriv->clock_ns != data[4]) {
4851                         data[2] = up_count * devpriv->clock_ns;
4852                         data[4] = down_count * devpriv->clock_ns;
4853                         return -EAGAIN;
4854                 }
4855                 ni_writel(up_count, Calibration_HighTime_6143);
4856                 devpriv->pwm_up_count = up_count;
4857                 ni_writel(down_count, Calibration_LowTime_6143);
4858                 devpriv->pwm_down_count = down_count;
4859                 return 5;
4860                 break;
4861         case INSN_CONFIG_GET_PWM_OUTPUT:
4862                 return ni_get_pwm_config(dev, data);
4863         default:
4864                 return -EINVAL;
4865                 break;
4866         }
4867         return 0;
4868 }
4869
4870 static void ni_write_caldac(struct comedi_device *dev, int addr, int val);
4871 /*
4872         calibration subdevice
4873 */
4874 static int ni_calib_insn_write(struct comedi_device *dev,
4875                                struct comedi_subdevice *s,
4876                                struct comedi_insn *insn, unsigned int *data)
4877 {
4878         ni_write_caldac(dev, CR_CHAN(insn->chanspec), data[0]);
4879
4880         return 1;
4881 }
4882
4883 static int ni_calib_insn_read(struct comedi_device *dev,
4884                               struct comedi_subdevice *s,
4885                               struct comedi_insn *insn, unsigned int *data)
4886 {
4887         struct ni_private *devpriv = dev->private;
4888
4889         data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4890
4891         return 1;
4892 }
4893
4894 static int pack_mb88341(int addr, int val, int *bitstring);
4895 static int pack_dac8800(int addr, int val, int *bitstring);
4896 static int pack_dac8043(int addr, int val, int *bitstring);
4897 static int pack_ad8522(int addr, int val, int *bitstring);
4898 static int pack_ad8804(int addr, int val, int *bitstring);
4899 static int pack_ad8842(int addr, int val, int *bitstring);
4900
4901 struct caldac_struct {
4902         int n_chans;
4903         int n_bits;
4904         int (*packbits) (int, int, int *);
4905 };
4906
4907 static struct caldac_struct caldacs[] = {
4908         [mb88341] = {12, 8, pack_mb88341},
4909         [dac8800] = {8, 8, pack_dac8800},
4910         [dac8043] = {1, 12, pack_dac8043},
4911         [ad8522] = {2, 12, pack_ad8522},
4912         [ad8804] = {12, 8, pack_ad8804},
4913         [ad8842] = {8, 8, pack_ad8842},
4914         [ad8804_debug] = {16, 8, pack_ad8804},
4915 };
4916
4917 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4918 {
4919         const struct ni_board_struct *board = comedi_board(dev);
4920         struct ni_private *devpriv = dev->private;
4921         int i, j;
4922         int n_dacs;
4923         int n_chans = 0;
4924         int n_bits;
4925         int diffbits = 0;
4926         int type;
4927         int chan;
4928
4929         type = board->caldac[0];
4930         if (type == caldac_none)
4931                 return;
4932         n_bits = caldacs[type].n_bits;
4933         for (i = 0; i < 3; i++) {
4934                 type = board->caldac[i];
4935                 if (type == caldac_none)
4936                         break;
4937                 if (caldacs[type].n_bits != n_bits)
4938                         diffbits = 1;
4939                 n_chans += caldacs[type].n_chans;
4940         }
4941         n_dacs = i;
4942         s->n_chan = n_chans;
4943
4944         if (diffbits) {
4945                 unsigned int *maxdata_list;
4946
4947                 if (n_chans > MAX_N_CALDACS) {
4948                         printk("BUG! MAX_N_CALDACS too small\n");
4949                 }
4950                 s->maxdata_list = maxdata_list = devpriv->caldac_maxdata_list;
4951                 chan = 0;
4952                 for (i = 0; i < n_dacs; i++) {
4953                         type = board->caldac[i];
4954                         for (j = 0; j < caldacs[type].n_chans; j++) {
4955                                 maxdata_list[chan] =
4956                                     (1 << caldacs[type].n_bits) - 1;
4957                                 chan++;
4958                         }
4959                 }
4960
4961                 for (chan = 0; chan < s->n_chan; chan++)
4962                         ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4963         } else {
4964                 type = board->caldac[0];
4965                 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4966
4967                 for (chan = 0; chan < s->n_chan; chan++)
4968                         ni_write_caldac(dev, i, s->maxdata / 2);
4969         }
4970 }
4971
4972 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4973 {
4974         const struct ni_board_struct *board = comedi_board(dev);
4975         struct ni_private *devpriv = dev->private;
4976         unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4977         int i;
4978         int type;
4979
4980         /* printk("ni_write_caldac: chan=%d val=%d\n",addr,val); */
4981         if (devpriv->caldacs[addr] == val)
4982                 return;
4983         devpriv->caldacs[addr] = val;
4984
4985         for (i = 0; i < 3; i++) {
4986                 type = board->caldac[i];
4987                 if (type == caldac_none)
4988                         break;
4989                 if (addr < caldacs[type].n_chans) {
4990                         bits = caldacs[type].packbits(addr, val, &bitstring);
4991                         loadbit = SerDacLd(i);
4992                         /* printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring); */
4993                         break;
4994                 }
4995                 addr -= caldacs[type].n_chans;
4996         }
4997
4998         for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4999                 ni_writeb(((bit & bitstring) ? 0x02 : 0), Serial_Command);
5000                 udelay(1);
5001                 ni_writeb(1 | ((bit & bitstring) ? 0x02 : 0), Serial_Command);
5002                 udelay(1);
5003         }
5004         ni_writeb(loadbit, Serial_Command);
5005         udelay(1);
5006         ni_writeb(0, Serial_Command);
5007 }
5008
5009 static int pack_mb88341(int addr, int val, int *bitstring)
5010 {
5011         /*
5012            Fujitsu MB 88341
5013            Note that address bits are reversed.  Thanks to
5014            Ingo Keen for noticing this.
5015
5016            Note also that the 88341 expects address values from
5017            1-12, whereas we use channel numbers 0-11.  The NI
5018            docs use 1-12, also, so be careful here.
5019          */
5020         addr++;
5021         *bitstring = ((addr & 0x1) << 11) |
5022             ((addr & 0x2) << 9) |
5023             ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
5024         return 12;
5025 }
5026
5027 static int pack_dac8800(int addr, int val, int *bitstring)
5028 {
5029         *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
5030         return 11;
5031 }
5032
5033 static int pack_dac8043(int addr, int val, int *bitstring)
5034 {
5035         *bitstring = val & 0xfff;
5036         return 12;
5037 }
5038
5039 static int pack_ad8522(int addr, int val, int *bitstring)
5040 {
5041         *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
5042         return 16;
5043 }
5044
5045 static int pack_ad8804(int addr, int val, int *bitstring)
5046 {
5047         *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
5048         return 12;
5049 }
5050
5051 static int pack_ad8842(int addr, int val, int *bitstring)
5052 {
5053         *bitstring = ((addr + 1) << 8) | (val & 0xff);
5054         return 12;
5055 }
5056
5057 #if 0
5058 /*
5059  *      Read the GPCTs current value.
5060  */
5061 static int GPCT_G_Watch(struct comedi_device *dev, int chan)
5062 {
5063         unsigned int hi1, hi2, lo;
5064
5065         devpriv->gpct_command[chan] &= ~G_Save_Trace;
5066         devpriv->stc_writew(dev, devpriv->gpct_command[chan],
5067                             G_Command_Register(chan));
5068
5069         devpriv->gpct_command[chan] |= G_Save_Trace;
5070         devpriv->stc_writew(dev, devpriv->gpct_command[chan],
5071                             G_Command_Register(chan));
5072
5073         /* This procedure is used because the two registers cannot
5074          * be read atomically. */
5075         do {
5076                 hi1 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
5077                 lo = devpriv->stc_readw(dev, G_Save_Register_Low(chan));
5078                 hi2 = devpriv->stc_readw(dev, G_Save_Register_High(chan));
5079         } while (hi1 != hi2);
5080
5081         return (hi1 << 16) | lo;
5082 }
5083
5084 static void GPCT_Reset(struct comedi_device *dev, int chan)
5085 {
5086         int temp_ack_reg = 0;
5087
5088         /* printk("GPCT_Reset..."); */
5089         devpriv->gpct_cur_operation[chan] = GPCT_RESET;
5090
5091         switch (chan) {
5092         case 0:
5093                 devpriv->stc_writew(dev, G0_Reset, Joint_Reset_Register);
5094                 ni_set_bits(dev, Interrupt_A_Enable_Register,
5095                             G0_TC_Interrupt_Enable, 0);
5096                 ni_set_bits(dev, Interrupt_A_Enable_Register,
5097                             G0_Gate_Interrupt_Enable, 0);
5098                 temp_ack_reg |= G0_Gate_Error_Confirm;
5099                 temp_ack_reg |= G0_TC_Error_Confirm;
5100                 temp_ack_reg |= G0_TC_Interrupt_Ack;
5101                 temp_ack_reg |= G0_Gate_Interrupt_Ack;
5102                 devpriv->stc_writew(dev, temp_ack_reg,
5103                                     Interrupt_A_Ack_Register);
5104
5105                 /* problem...this interferes with the other ctr... */
5106                 devpriv->an_trig_etc_reg |= GPFO_0_Output_Enable;
5107                 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg,
5108                                     Analog_Trigger_Etc_Register);
5109                 break;
5110         case 1:
5111                 devpriv->stc_writew(dev, G1_Reset, Joint_Reset_Register);
5112                 ni_set_bits(dev, Interrupt_B_Enable_Register,
5113                             G1_TC_Interrupt_Enable, 0);
5114                 ni_set_bits(dev, Interrupt_B_Enable_Register,
5115                             G0_Gate_Interrupt_Enable, 0);
5116                 temp_ack_reg |= G1_Gate_Error_Confirm;
5117                 temp_ack_reg |= G1_TC_Error_Confirm;
5118                 temp_ack_reg |= G1_TC_Interrupt_Ack;
5119                 temp_ack_reg |= G1_Gate_Interrupt_Ack;
5120                 devpriv->stc_writew(dev, temp_ack_reg,
5121                                     Interrupt_B_Ack_Register);
5122
5123                 devpriv->an_trig_etc_reg |= GPFO_1_Output_Enable;
5124                 devpriv->stc_writew(dev, devpriv->an_trig_etc_reg,
5125                                     Analog_Trigger_Etc_Register);
5126                 break;
5127         }
5128
5129         devpriv->gpct_mode[chan] = 0;
5130         devpriv->gpct_input_select[chan] = 0;
5131         devpriv->gpct_command[chan] = 0;
5132
5133         devpriv->gpct_command[chan] |= G_Synchronized_Gate;
5134
5135         devpriv->stc_writew(dev, devpriv->gpct_mode[chan],
5136                             G_Mode_Register(chan));
5137         devpriv->stc_writew(dev, devpriv->gpct_input_select[chan],
5138                             G_Input_Select_Register(chan));
5139         devpriv->stc_writew(dev, 0, G_Autoincrement_Register(chan));
5140
5141         /* printk("exit GPCT_Reset\n"); */
5142 }
5143
5144 #endif
5145
5146 static int ni_gpct_insn_config(struct comedi_device *dev,
5147                                struct comedi_subdevice *s,
5148                                struct comedi_insn *insn, unsigned int *data)
5149 {
5150         struct ni_gpct *counter = s->private;
5151         return ni_tio_insn_config(counter, insn, data);
5152 }
5153
5154 static int ni_gpct_insn_read(struct comedi_device *dev,
5155                              struct comedi_subdevice *s,
5156                              struct comedi_insn *insn, unsigned int *data)
5157 {
5158         struct ni_gpct *counter = s->private;
5159         return ni_tio_rinsn(counter, insn, data);
5160 }
5161
5162 static int ni_gpct_insn_write(struct comedi_device *dev,
5163                               struct comedi_subdevice *s,
5164                               struct comedi_insn *insn, unsigned int *data)
5165 {
5166         struct ni_gpct *counter = s->private;
5167         return ni_tio_winsn(counter, insn, data);
5168 }
5169
5170 #ifdef PCIDMA
5171 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5172 {
5173         int retval;
5174         struct ni_gpct *counter = s->private;
5175 /* const struct comedi_cmd *cmd = &s->async->cmd; */
5176
5177         retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5178                                               COMEDI_INPUT);
5179         if (retval) {
5180                 comedi_error(dev,
5181                              "no dma channel available for use by counter");
5182                 return retval;
5183         }
5184         ni_tio_acknowledge_and_confirm(counter, NULL, NULL, NULL, NULL);
5185         ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5186         retval = ni_tio_cmd(counter, s->async);
5187         return retval;
5188 }
5189 #endif
5190
5191 #ifdef PCIDMA
5192 static int ni_gpct_cmdtest(struct comedi_device *dev,
5193                            struct comedi_subdevice *s, struct comedi_cmd *cmd)
5194 {
5195         struct ni_gpct *counter = s->private;
5196
5197         return ni_tio_cmdtest(counter, cmd);
5198         return -ENOTSUPP;
5199 }
5200 #endif
5201
5202 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5203 {
5204 #ifdef PCIDMA
5205         struct ni_gpct *counter = s->private;
5206         int retval;
5207
5208         retval = ni_tio_cancel(counter);
5209         ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5210         ni_release_gpct_mite_channel(dev, counter->counter_index);
5211         return retval;
5212 #else
5213         return 0;
5214 #endif
5215 }
5216
5217 /*
5218  *
5219  *  Programmable Function Inputs
5220  *
5221  */
5222
5223 static int ni_m_series_set_pfi_routing(struct comedi_device *dev, unsigned chan,
5224                                        unsigned source)
5225 {
5226         struct ni_private *devpriv = dev->private;
5227         unsigned pfi_reg_index;
5228         unsigned array_offset;
5229
5230         if ((source & 0x1f) != source)
5231                 return -EINVAL;
5232         pfi_reg_index = 1 + chan / 3;
5233         array_offset = pfi_reg_index - 1;
5234         devpriv->pfi_output_select_reg[array_offset] &=
5235             ~MSeries_PFI_Output_Select_Mask(chan);
5236         devpriv->pfi_output_select_reg[array_offset] |=
5237             MSeries_PFI_Output_Select_Bits(chan, source);
5238         ni_writew(devpriv->pfi_output_select_reg[array_offset],
5239                   M_Offset_PFI_Output_Select(pfi_reg_index));
5240         return 2;
5241 }
5242
5243 static int ni_old_set_pfi_routing(struct comedi_device *dev, unsigned chan,
5244                                   unsigned source)
5245 {
5246         /*  pre-m-series boards have fixed signals on pfi pins */
5247         if (source != ni_old_get_pfi_routing(dev, chan))
5248                 return -EINVAL;
5249         return 2;
5250 }
5251
5252 static int ni_set_pfi_routing(struct comedi_device *dev, unsigned chan,
5253                               unsigned source)
5254 {
5255         const struct ni_board_struct *board = comedi_board(dev);
5256
5257         if (board->reg_type & ni_reg_m_series_mask)
5258                 return ni_m_series_set_pfi_routing(dev, chan, source);
5259         else
5260                 return ni_old_set_pfi_routing(dev, chan, source);
5261 }
5262
5263 static unsigned ni_m_series_get_pfi_routing(struct comedi_device *dev,
5264                                             unsigned chan)
5265 {
5266         struct ni_private *devpriv = dev->private;
5267         const unsigned array_offset = chan / 3;
5268
5269         return MSeries_PFI_Output_Select_Source(chan,
5270                                                 devpriv->
5271                                                 pfi_output_select_reg
5272                                                 [array_offset]);
5273 }
5274
5275 static unsigned ni_old_get_pfi_routing(struct comedi_device *dev, unsigned chan)
5276 {
5277         /*  pre-m-series boards have fixed signals on pfi pins */
5278         switch (chan) {
5279         case 0:
5280                 return NI_PFI_OUTPUT_AI_START1;
5281                 break;
5282         case 1:
5283                 return NI_PFI_OUTPUT_AI_START2;
5284                 break;
5285         case 2:
5286                 return NI_PFI_OUTPUT_AI_CONVERT;
5287                 break;
5288         case 3:
5289                 return NI_PFI_OUTPUT_G_SRC1;
5290                 break;
5291         case 4:
5292                 return NI_PFI_OUTPUT_G_GATE1;
5293                 break;
5294         case 5:
5295                 return NI_PFI_OUTPUT_AO_UPDATE_N;
5296                 break;
5297         case 6:
5298                 return NI_PFI_OUTPUT_AO_START1;
5299                 break;
5300         case 7:
5301                 return NI_PFI_OUTPUT_AI_START_PULSE;
5302                 break;
5303         case 8:
5304                 return NI_PFI_OUTPUT_G_SRC0;
5305                 break;
5306         case 9:
5307                 return NI_PFI_OUTPUT_G_GATE0;
5308                 break;
5309         default:
5310                 printk("%s: bug, unhandled case in switch.\n", __func__);
5311                 break;
5312         }
5313         return 0;
5314 }
5315
5316 static unsigned ni_get_pfi_routing(struct comedi_device *dev, unsigned chan)
5317 {
5318         const struct ni_board_struct *board = comedi_board(dev);
5319
5320         if (board->reg_type & ni_reg_m_series_mask)
5321                 return ni_m_series_get_pfi_routing(dev, chan);
5322         else
5323                 return ni_old_get_pfi_routing(dev, chan);
5324 }
5325
5326 static int ni_config_filter(struct comedi_device *dev, unsigned pfi_channel,
5327                             enum ni_pfi_filter_select filter)
5328 {
5329         const struct ni_board_struct *board = comedi_board(dev);
5330         struct ni_private *devpriv __maybe_unused = dev->private;
5331         unsigned bits;
5332
5333         if ((board->reg_type & ni_reg_m_series_mask) == 0) {
5334                 return -ENOTSUPP;
5335         }
5336         bits = ni_readl(M_Offset_PFI_Filter);
5337         bits &= ~MSeries_PFI_Filter_Select_Mask(pfi_channel);
5338         bits |= MSeries_PFI_Filter_Select_Bits(pfi_channel, filter);
5339         ni_writel(bits, M_Offset_PFI_Filter);
5340         return 0;
5341 }
5342
5343 static int ni_pfi_insn_bits(struct comedi_device *dev,
5344                             struct comedi_subdevice *s,
5345                             struct comedi_insn *insn,
5346                             unsigned int *data)
5347 {
5348         const struct ni_board_struct *board = comedi_board(dev);
5349         struct ni_private *devpriv __maybe_unused = dev->private;
5350
5351         if (!(board->reg_type & ni_reg_m_series_mask))
5352                 return -ENOTSUPP;
5353
5354         if (comedi_dio_update_state(s, data))
5355                 ni_writew(s->state, M_Offset_PFI_DO);
5356
5357         data[1] = ni_readw(M_Offset_PFI_DI);
5358
5359         return insn->n;
5360 }
5361
5362 static int ni_pfi_insn_config(struct comedi_device *dev,
5363                               struct comedi_subdevice *s,
5364                               struct comedi_insn *insn, unsigned int *data)
5365 {
5366         struct ni_private *devpriv = dev->private;
5367         unsigned int chan;
5368
5369         if (insn->n < 1)
5370                 return -EINVAL;
5371
5372         chan = CR_CHAN(insn->chanspec);
5373
5374         switch (data[0]) {
5375         case COMEDI_OUTPUT:
5376                 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1 << chan, 1);
5377                 break;
5378         case COMEDI_INPUT:
5379                 ni_set_bits(dev, IO_Bidirection_Pin_Register, 1 << chan, 0);
5380                 break;
5381         case INSN_CONFIG_DIO_QUERY:
5382                 data[1] =
5383                     (devpriv->io_bidirection_pin_reg & (1 << chan)) ?
5384                     COMEDI_OUTPUT : COMEDI_INPUT;
5385                 return 0;
5386                 break;
5387         case INSN_CONFIG_SET_ROUTING:
5388                 return ni_set_pfi_routing(dev, chan, data[1]);
5389                 break;
5390         case INSN_CONFIG_GET_ROUTING:
5391                 data[1] = ni_get_pfi_routing(dev, chan);
5392                 break;
5393         case INSN_CONFIG_FILTER:
5394                 return ni_config_filter(dev, chan, data[1]);
5395                 break;
5396         default:
5397                 return -EINVAL;
5398         }
5399         return 0;
5400 }
5401
5402 /*
5403  *
5404  *  NI RTSI Bus Functions
5405  *
5406  */
5407 static void ni_rtsi_init(struct comedi_device *dev)
5408 {
5409         const struct ni_board_struct *board = comedi_board(dev);
5410         struct ni_private *devpriv = dev->private;
5411
5412         /*  Initialises the RTSI bus signal switch to a default state */
5413
5414         /*  Set clock mode to internal */
5415         devpriv->clock_and_fout2 = MSeries_RTSI_10MHz_Bit;
5416         if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0) {
5417                 printk("ni_set_master_clock failed, bug?");
5418         }
5419         /*  default internal lines routing to RTSI bus lines */
5420         devpriv->rtsi_trig_a_output_reg =
5421             RTSI_Trig_Output_Bits(0,
5422                                   NI_RTSI_OUTPUT_ADR_START1) |
5423             RTSI_Trig_Output_Bits(1,
5424                                   NI_RTSI_OUTPUT_ADR_START2) |
5425             RTSI_Trig_Output_Bits(2,
5426                                   NI_RTSI_OUTPUT_SCLKG) |
5427             RTSI_Trig_Output_Bits(3, NI_RTSI_OUTPUT_DACUPDN);
5428         devpriv->stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5429                             RTSI_Trig_A_Output_Register);
5430         devpriv->rtsi_trig_b_output_reg =
5431             RTSI_Trig_Output_Bits(4,
5432                                   NI_RTSI_OUTPUT_DA_START1) |
5433             RTSI_Trig_Output_Bits(5,
5434                                   NI_RTSI_OUTPUT_G_SRC0) |
5435             RTSI_Trig_Output_Bits(6, NI_RTSI_OUTPUT_G_GATE0);
5436         if (board->reg_type & ni_reg_m_series_mask)
5437                 devpriv->rtsi_trig_b_output_reg |=
5438                     RTSI_Trig_Output_Bits(7, NI_RTSI_OUTPUT_RTSI_OSC);
5439         devpriv->stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5440                             RTSI_Trig_B_Output_Register);
5441
5442 /*
5443 * Sets the source and direction of the 4 on board lines
5444 * devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
5445 */
5446 }
5447
5448 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5449                              struct comedi_subdevice *s,
5450                              struct comedi_insn *insn, unsigned int *data)
5451 {
5452         data[1] = 0;
5453
5454         return insn->n;
5455 }
5456
5457 /* Find best multiplier/divider to try and get the PLL running at 80 MHz
5458  * given an arbitrary frequency input clock */
5459 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns,
5460                                          unsigned *freq_divider,
5461                                          unsigned *freq_multiplier,
5462                                          unsigned *actual_period_ns)
5463 {
5464         unsigned div;
5465         unsigned best_div = 1;
5466         static const unsigned max_div = 0x10;
5467         unsigned mult;
5468         unsigned best_mult = 1;
5469         static const unsigned max_mult = 0x100;
5470         static const unsigned pico_per_nano = 1000;
5471
5472         const unsigned reference_picosec = reference_period_ns * pico_per_nano;
5473         /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
5474          * 20 MHz for most timing clocks */
5475         static const unsigned target_picosec = 12500;
5476         static const unsigned fudge_factor_80_to_20Mhz = 4;
5477         int best_period_picosec = 0;
5478         for (div = 1; div <= max_div; ++div) {
5479                 for (mult = 1; mult <= max_mult; ++mult) {
5480                         unsigned new_period_ps =
5481                             (reference_picosec * div) / mult;
5482                         if (abs(new_period_ps - target_picosec) <
5483                             abs(best_period_picosec - target_picosec)) {
5484                                 best_period_picosec = new_period_ps;
5485                                 best_div = div;
5486                                 best_mult = mult;
5487                         }
5488                 }
5489         }
5490         if (best_period_picosec == 0) {
5491                 printk("%s: bug, failed to find pll parameters\n", __func__);
5492                 return -EIO;
5493         }
5494         *freq_divider = best_div;
5495         *freq_multiplier = best_mult;
5496         *actual_period_ns =
5497             (best_period_picosec * fudge_factor_80_to_20Mhz +
5498              (pico_per_nano / 2)) / pico_per_nano;
5499         return 0;
5500 }
5501
5502 static inline unsigned num_configurable_rtsi_channels(struct comedi_device *dev)
5503 {
5504         const struct ni_board_struct *board = comedi_board(dev);
5505
5506         if (board->reg_type & ni_reg_m_series_mask)
5507                 return 8;
5508         else
5509                 return 7;
5510 }
5511
5512 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
5513                                            unsigned source, unsigned period_ns)
5514 {
5515         struct ni_private *devpriv = dev->private;
5516         static const unsigned min_period_ns = 50;
5517         static const unsigned max_period_ns = 1000;
5518         static const unsigned timeout = 1000;
5519         unsigned pll_control_bits;
5520         unsigned freq_divider;
5521         unsigned freq_multiplier;
5522         unsigned i;
5523         int retval;
5524
5525         if (source == NI_MIO_PLL_PXI10_CLOCK)
5526                 period_ns = 100;
5527         /*  these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
5528         if (period_ns < min_period_ns || period_ns > max_period_ns) {
5529                 printk
5530                     ("%s: you must specify an input clock frequency between %i and %i nanosec "
5531                      "for the phased-lock loop.\n", __func__,
5532                      min_period_ns, max_period_ns);
5533                 return -EINVAL;
5534         }
5535         devpriv->rtsi_trig_direction_reg &= ~Use_RTSI_Clock_Bit;
5536         devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5537                             RTSI_Trig_Direction_Register);
5538         pll_control_bits =
5539             MSeries_PLL_Enable_Bit | MSeries_PLL_VCO_Mode_75_150MHz_Bits;
5540         devpriv->clock_and_fout2 |=
5541             MSeries_Timebase1_Select_Bit | MSeries_Timebase3_Select_Bit;
5542         devpriv->clock_and_fout2 &= ~MSeries_PLL_In_Source_Select_Mask;
5543         switch (source) {
5544         case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
5545                 devpriv->clock_and_fout2 |=
5546                     MSeries_PLL_In_Source_Select_Star_Trigger_Bits;
5547                 retval = ni_mseries_get_pll_parameters(period_ns, &freq_divider,
5548                                                        &freq_multiplier,
5549                                                        &devpriv->clock_ns);
5550                 if (retval < 0)
5551                         return retval;
5552                 break;
5553         case NI_MIO_PLL_PXI10_CLOCK:
5554                 /* pxi clock is 10MHz */
5555                 devpriv->clock_and_fout2 |=
5556                     MSeries_PLL_In_Source_Select_PXI_Clock10;
5557                 retval = ni_mseries_get_pll_parameters(period_ns, &freq_divider,
5558                                                        &freq_multiplier,
5559                                                        &devpriv->clock_ns);
5560                 if (retval < 0)
5561                         return retval;
5562                 break;
5563         default:
5564                 {
5565                         unsigned rtsi_channel;
5566                         static const unsigned max_rtsi_channel = 7;
5567                         for (rtsi_channel = 0; rtsi_channel <= max_rtsi_channel;
5568                              ++rtsi_channel) {
5569                                 if (source ==
5570                                     NI_MIO_PLL_RTSI_CLOCK(rtsi_channel)) {
5571                                         devpriv->clock_and_fout2 |=
5572                                             MSeries_PLL_In_Source_Select_RTSI_Bits
5573                                             (rtsi_channel);
5574                                         break;
5575                                 }
5576                         }
5577                         if (rtsi_channel > max_rtsi_channel)
5578                                 return -EINVAL;
5579                         retval = ni_mseries_get_pll_parameters(period_ns,
5580                                                                &freq_divider,
5581                                                                &freq_multiplier,
5582                                                                &devpriv->
5583                                                                clock_ns);
5584                         if (retval < 0)
5585                                 return retval;
5586                 }
5587                 break;
5588         }
5589         ni_writew(devpriv->clock_and_fout2, M_Offset_Clock_and_Fout2);
5590         pll_control_bits |=
5591             MSeries_PLL_Divisor_Bits(freq_divider) |
5592             MSeries_PLL_Multiplier_Bits(freq_multiplier);
5593
5594         /* printk("using divider=%i, multiplier=%i for PLL. pll_control_bits = 0x%x\n",
5595          * freq_divider, freq_multiplier, pll_control_bits); */
5596         /* printk("clock_ns=%d\n", devpriv->clock_ns); */
5597         ni_writew(pll_control_bits, M_Offset_PLL_Control);
5598         devpriv->clock_source = source;
5599         /* it seems to typically take a few hundred microseconds for PLL to lock */
5600         for (i = 0; i < timeout; ++i) {
5601                 if (ni_readw(M_Offset_PLL_Status) & MSeries_PLL_Locked_Bit) {
5602                         break;
5603                 }
5604                 udelay(1);
5605         }
5606         if (i == timeout) {
5607                 printk
5608                     ("%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns.\n",
5609                      __func__, source, period_ns);
5610                 return -ETIMEDOUT;
5611         }
5612         return 3;
5613 }
5614
5615 static int ni_set_master_clock(struct comedi_device *dev, unsigned source,
5616                                unsigned period_ns)
5617 {
5618         const struct ni_board_struct *board = comedi_board(dev);
5619         struct ni_private *devpriv = dev->private;
5620
5621         if (source == NI_MIO_INTERNAL_CLOCK) {
5622                 devpriv->rtsi_trig_direction_reg &= ~Use_RTSI_Clock_Bit;
5623                 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5624                                     RTSI_Trig_Direction_Register);
5625                 devpriv->clock_ns = TIMEBASE_1_NS;
5626                 if (board->reg_type & ni_reg_m_series_mask) {
5627                         devpriv->clock_and_fout2 &=
5628                             ~(MSeries_Timebase1_Select_Bit |
5629                               MSeries_Timebase3_Select_Bit);
5630                         ni_writew(devpriv->clock_and_fout2,
5631                                   M_Offset_Clock_and_Fout2);
5632                         ni_writew(0, M_Offset_PLL_Control);
5633                 }
5634                 devpriv->clock_source = source;
5635         } else {
5636                 if (board->reg_type & ni_reg_m_series_mask) {
5637                         return ni_mseries_set_pll_master_clock(dev, source,
5638                                                                period_ns);
5639                 } else {
5640                         if (source == NI_MIO_RTSI_CLOCK) {
5641                                 devpriv->rtsi_trig_direction_reg |=
5642                                     Use_RTSI_Clock_Bit;
5643                                 devpriv->stc_writew(dev,
5644                                                     devpriv->
5645                                                     rtsi_trig_direction_reg,
5646                                                     RTSI_Trig_Direction_Register);
5647                                 if (period_ns == 0) {
5648                                         printk
5649                                             ("%s: we don't handle an unspecified clock period correctly yet, returning error.\n",
5650                                              __func__);
5651                                         return -EINVAL;
5652                                 } else {
5653                                         devpriv->clock_ns = period_ns;
5654                                 }
5655                                 devpriv->clock_source = source;
5656                         } else
5657                                 return -EINVAL;
5658                 }
5659         }
5660         return 3;
5661 }
5662
5663 static int ni_valid_rtsi_output_source(struct comedi_device *dev, unsigned chan,
5664                                        unsigned source)
5665 {
5666         const struct ni_board_struct *board = comedi_board(dev);
5667
5668         if (chan >= num_configurable_rtsi_channels(dev)) {
5669                 if (chan == old_RTSI_clock_channel) {
5670                         if (source == NI_RTSI_OUTPUT_RTSI_OSC)
5671                                 return 1;
5672                         else {
5673                                 printk
5674                                     ("%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards.\n",
5675                                      __func__, chan, old_RTSI_clock_channel);
5676                                 return 0;
5677                         }
5678                 }
5679                 return 0;
5680         }
5681         switch (source) {
5682         case NI_RTSI_OUTPUT_ADR_START1:
5683         case NI_RTSI_OUTPUT_ADR_START2:
5684         case NI_RTSI_OUTPUT_SCLKG:
5685         case NI_RTSI_OUTPUT_DACUPDN:
5686         case NI_RTSI_OUTPUT_DA_START1:
5687         case NI_RTSI_OUTPUT_G_SRC0:
5688         case NI_RTSI_OUTPUT_G_GATE0:
5689         case NI_RTSI_OUTPUT_RGOUT0:
5690         case NI_RTSI_OUTPUT_RTSI_BRD_0:
5691                 return 1;
5692                 break;
5693         case NI_RTSI_OUTPUT_RTSI_OSC:
5694                 if (board->reg_type & ni_reg_m_series_mask)
5695                         return 1;
5696                 else
5697                         return 0;
5698                 break;
5699         default:
5700                 return 0;
5701                 break;
5702         }
5703 }
5704
5705 static int ni_set_rtsi_routing(struct comedi_device *dev, unsigned chan,
5706                                unsigned source)
5707 {
5708         struct ni_private *devpriv = dev->private;
5709
5710         if (ni_valid_rtsi_output_source(dev, chan, source) == 0)
5711                 return -EINVAL;
5712         if (chan < 4) {
5713                 devpriv->rtsi_trig_a_output_reg &= ~RTSI_Trig_Output_Mask(chan);
5714                 devpriv->rtsi_trig_a_output_reg |=
5715                     RTSI_Trig_Output_Bits(chan, source);
5716                 devpriv->stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5717                                     RTSI_Trig_A_Output_Register);
5718         } else if (chan < 8) {
5719                 devpriv->rtsi_trig_b_output_reg &= ~RTSI_Trig_Output_Mask(chan);
5720                 devpriv->rtsi_trig_b_output_reg |=
5721                     RTSI_Trig_Output_Bits(chan, source);
5722                 devpriv->stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5723                                     RTSI_Trig_B_Output_Register);
5724         }
5725         return 2;
5726 }
5727
5728 static unsigned ni_get_rtsi_routing(struct comedi_device *dev, unsigned chan)
5729 {
5730         struct ni_private *devpriv = dev->private;
5731
5732         if (chan < 4) {
5733                 return RTSI_Trig_Output_Source(chan,
5734                                                devpriv->rtsi_trig_a_output_reg);
5735         } else if (chan < num_configurable_rtsi_channels(dev)) {
5736                 return RTSI_Trig_Output_Source(chan,
5737                                                devpriv->rtsi_trig_b_output_reg);
5738         } else {
5739                 if (chan == old_RTSI_clock_channel)
5740                         return NI_RTSI_OUTPUT_RTSI_OSC;
5741                 printk("%s: bug! should never get here?\n", __func__);
5742                 return 0;
5743         }
5744 }
5745
5746 static int ni_rtsi_insn_config(struct comedi_device *dev,
5747                                struct comedi_subdevice *s,
5748                                struct comedi_insn *insn, unsigned int *data)
5749 {
5750         const struct ni_board_struct *board = comedi_board(dev);
5751         struct ni_private *devpriv = dev->private;
5752         unsigned int chan = CR_CHAN(insn->chanspec);
5753
5754         switch (data[0]) {
5755         case INSN_CONFIG_DIO_OUTPUT:
5756                 if (chan < num_configurable_rtsi_channels(dev)) {
5757                         devpriv->rtsi_trig_direction_reg |=
5758                             RTSI_Output_Bit(chan,
5759                                 (board->reg_type & ni_reg_m_series_mask) != 0);
5760                 } else if (chan == old_RTSI_clock_channel) {
5761                         devpriv->rtsi_trig_direction_reg |=
5762                             Drive_RTSI_Clock_Bit;
5763                 }
5764                 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5765                                     RTSI_Trig_Direction_Register);
5766                 break;
5767         case INSN_CONFIG_DIO_INPUT:
5768                 if (chan < num_configurable_rtsi_channels(dev)) {
5769                         devpriv->rtsi_trig_direction_reg &=
5770                             ~RTSI_Output_Bit(chan,
5771                                 (board->reg_type & ni_reg_m_series_mask) != 0);
5772                 } else if (chan == old_RTSI_clock_channel) {
5773                         devpriv->rtsi_trig_direction_reg &=
5774                             ~Drive_RTSI_Clock_Bit;
5775                 }
5776                 devpriv->stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5777                                     RTSI_Trig_Direction_Register);
5778                 break;
5779         case INSN_CONFIG_DIO_QUERY:
5780                 if (chan < num_configurable_rtsi_channels(dev)) {
5781                         data[1] =
5782                             (devpriv->rtsi_trig_direction_reg &
5783                              RTSI_Output_Bit(chan,
5784                                 (board->reg_type & ni_reg_m_series_mask) != 0))
5785                                 ? INSN_CONFIG_DIO_OUTPUT
5786                                 : INSN_CONFIG_DIO_INPUT;
5787                 } else if (chan == old_RTSI_clock_channel) {
5788                         data[1] =
5789                             (devpriv->rtsi_trig_direction_reg &
5790                              Drive_RTSI_Clock_Bit)
5791                             ? INSN_CONFIG_DIO_OUTPUT : INSN_CONFIG_DIO_INPUT;
5792                 }
5793                 return 2;
5794                 break;
5795         case INSN_CONFIG_SET_CLOCK_SRC:
5796                 return ni_set_master_clock(dev, data[1], data[2]);
5797                 break;
5798         case INSN_CONFIG_GET_CLOCK_SRC:
5799                 data[1] = devpriv->clock_source;
5800                 data[2] = devpriv->clock_ns;
5801                 return 3;
5802                 break;
5803         case INSN_CONFIG_SET_ROUTING:
5804                 return ni_set_rtsi_routing(dev, chan, data[1]);
5805                 break;
5806         case INSN_CONFIG_GET_ROUTING:
5807                 data[1] = ni_get_rtsi_routing(dev, chan);
5808                 return 2;
5809                 break;
5810         default:
5811                 return -EINVAL;
5812                 break;
5813         }
5814         return 1;
5815 }
5816
5817 static int cs5529_wait_for_idle(struct comedi_device *dev)
5818 {
5819         unsigned short status;
5820         const int timeout = HZ;
5821         int i;
5822
5823         for (i = 0; i < timeout; i++) {
5824                 status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
5825                 if ((status & CSS_ADC_BUSY) == 0) {
5826                         break;
5827                 }
5828                 set_current_state(TASK_INTERRUPTIBLE);
5829                 if (schedule_timeout(1)) {
5830                         return -EIO;
5831                 }
5832         }
5833 /* printk("looped %i times waiting for idle\n", i); */
5834         if (i == timeout) {
5835                 printk("%s: %s: timeout\n", __FILE__, __func__);
5836                 return -ETIME;
5837         }
5838         return 0;
5839 }
5840
5841 static void cs5529_command(struct comedi_device *dev, unsigned short value)
5842 {
5843         static const int timeout = 100;
5844         int i;
5845
5846         ni_ao_win_outw(dev, value, CAL_ADC_Command_67xx);
5847         /* give time for command to start being serially clocked into cs5529.
5848          * this insures that the CSS_ADC_BUSY bit will get properly
5849          * set before we exit this function.
5850          */
5851         for (i = 0; i < timeout; i++) {
5852                 if ((ni_ao_win_inw(dev, CAL_ADC_Status_67xx) & CSS_ADC_BUSY))
5853                         break;
5854                 udelay(1);
5855         }
5856 /* printk("looped %i times writing command to cs5529\n", i); */
5857         if (i == timeout) {
5858                 comedi_error(dev, "possible problem - never saw adc go busy?");
5859         }
5860 }
5861
5862 /* write to cs5529 register */
5863 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
5864                                 unsigned int reg_select_bits)
5865 {
5866         ni_ao_win_outw(dev, ((value >> 16) & 0xff),
5867                        CAL_ADC_Config_Data_High_Word_67xx);
5868         ni_ao_win_outw(dev, (value & 0xffff),
5869                        CAL_ADC_Config_Data_Low_Word_67xx);
5870         reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
5871         cs5529_command(dev, CSCMD_COMMAND | reg_select_bits);
5872         if (cs5529_wait_for_idle(dev))
5873                 comedi_error(dev, "time or signal in cs5529_config_write()");
5874 }
5875
5876 #ifdef NI_CS5529_DEBUG
5877 /* read from cs5529 register */
5878 static unsigned int cs5529_config_read(struct comedi_device *dev,
5879                                        unsigned int reg_select_bits)
5880 {
5881         unsigned int value;
5882
5883         reg_select_bits &= CSCMD_REGISTER_SELECT_MASK;
5884         cs5529_command(dev, CSCMD_COMMAND | CSCMD_READ | reg_select_bits);
5885         if (cs5529_wait_for_idle(dev))
5886                 comedi_error(dev, "timeout or signal in cs5529_config_read()");
5887         value = (ni_ao_win_inw(dev,
5888                                CAL_ADC_Config_Data_High_Word_67xx) << 16) &
5889             0xff0000;
5890         value |= ni_ao_win_inw(dev, CAL_ADC_Config_Data_Low_Word_67xx) & 0xffff;
5891         return value;
5892 }
5893 #endif
5894
5895 static int cs5529_do_conversion(struct comedi_device *dev, unsigned short *data)
5896 {
5897         int retval;
5898         unsigned short status;
5899
5900         cs5529_command(dev, CSCMD_COMMAND | CSCMD_SINGLE_CONVERSION);
5901         retval = cs5529_wait_for_idle(dev);
5902         if (retval) {
5903                 comedi_error(dev,
5904                              "timeout or signal in cs5529_do_conversion()");
5905                 return -ETIME;
5906         }
5907         status = ni_ao_win_inw(dev, CAL_ADC_Status_67xx);
5908         if (status & CSS_OSC_DETECT) {
5909                 printk
5910                     ("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
5911                 return -EIO;
5912         }
5913         if (status & CSS_OVERRANGE) {
5914                 printk
5915                     ("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
5916         }
5917         if (data) {
5918                 *data = ni_ao_win_inw(dev, CAL_ADC_Data_67xx);
5919                 /* cs5529 returns 16 bit signed data in bipolar mode */
5920                 *data ^= (1 << 15);
5921         }
5922         return 0;
5923 }
5924
5925 static int cs5529_ai_insn_read(struct comedi_device *dev,
5926                                struct comedi_subdevice *s,
5927                                struct comedi_insn *insn, unsigned int *data)
5928 {
5929         int n, retval;
5930         unsigned short sample;
5931         unsigned int channel_select;
5932         const unsigned int INTERNAL_REF = 0x1000;
5933
5934         /* Set calibration adc source.  Docs lie, reference select bits 8 to 11
5935          * do nothing. bit 12 seems to chooses internal reference voltage, bit
5936          * 13 causes the adc input to go overrange (maybe reads external reference?) */
5937         if (insn->chanspec & CR_ALT_SOURCE)
5938                 channel_select = INTERNAL_REF;
5939         else
5940                 channel_select = CR_CHAN(insn->chanspec);
5941         ni_ao_win_outw(dev, channel_select, AO_Calibration_Channel_Select_67xx);
5942
5943         for (n = 0; n < insn->n; n++) {
5944                 retval = cs5529_do_conversion(dev, &sample);
5945                 if (retval < 0)
5946                         return retval;
5947                 data[n] = sample;
5948         }
5949         return insn->n;
5950 }
5951
5952 static int init_cs5529(struct comedi_device *dev)
5953 {
5954         unsigned int config_bits =
5955             CSCFG_PORT_MODE | CSCFG_WORD_RATE_2180_CYCLES;
5956
5957 #if 1
5958         /* do self-calibration */
5959         cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET_GAIN,
5960                             CSCMD_CONFIG_REGISTER);
5961         /* need to force a conversion for calibration to run */
5962         cs5529_do_conversion(dev, NULL);
5963 #else
5964         /* force gain calibration to 1 */
5965         cs5529_config_write(dev, 0x400000, CSCMD_GAIN_REGISTER);
5966         cs5529_config_write(dev, config_bits | CSCFG_SELF_CAL_OFFSET,
5967                             CSCMD_CONFIG_REGISTER);
5968         if (cs5529_wait_for_idle(dev))
5969                 comedi_error(dev, "timeout or signal in init_cs5529()\n");
5970 #endif
5971 #ifdef NI_CS5529_DEBUG
5972         printk("config: 0x%x\n", cs5529_config_read(dev,
5973                                                     CSCMD_CONFIG_REGISTER));
5974         printk("gain: 0x%x\n", cs5529_config_read(dev, CSCMD_GAIN_REGISTER));
5975         printk("offset: 0x%x\n", cs5529_config_read(dev,
5976                                                     CSCMD_OFFSET_REGISTER));
5977 #endif
5978         return 0;
5979 }