Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/staging/comedi/drivers/amplc_pci224.c

  1 /*
  2     comedi/drivers/amplc_pci224.c
  3     Driver for Amplicon PCI224 and PCI234 AO boards.
  4 
  5     Copyright (C) 2005 MEV Ltd. <http://www.mev.co.uk/>
  6 
  7     COMEDI - Linux Control and Measurement Device Interface
  8     Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
  9 
 10     This program is free software; you can redistribute it and/or modify
 11     it under the terms of the GNU General Public License as published by
 12     the Free Software Foundation; either version 2 of the License, or
 13     (at your option) any later version.
 14 
 15     This program is distributed in the hope that it will be useful,
 16     but WITHOUT ANY WARRANTY; without even the implied warranty of
 17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18     GNU General Public License for more details.
 19 */
 20 /*
 21 Driver: amplc_pci224
 22 Description: Amplicon PCI224, PCI234
 23 Author: Ian Abbott <abbotti@mev.co.uk>
 24 Devices: [Amplicon] PCI224 (amplc_pci224 or pci224),
 25   PCI234 (amplc_pci224 or pci234)
 26 Updated: Wed, 22 Oct 2008 12:25:08 +0100
 27 Status: works, but see caveats
 28 
 29 Supports:
 30 
 31   - ao_insn read/write
 32   - ao_do_cmd mode with the following sources:
 33 
 34     - start_src         TRIG_INT        TRIG_EXT
 35     - scan_begin_src    TRIG_TIMER      TRIG_EXT
 36     - convert_src       TRIG_NOW
 37     - scan_end_src      TRIG_COUNT
 38     - stop_src          TRIG_COUNT      TRIG_EXT        TRIG_NONE
 39 
 40     The channel list must contain at least one channel with no repeated
 41     channels.  The scan end count must equal the number of channels in
 42     the channel list.
 43 
 44     There is only one external trigger source so only one of start_src,
 45     scan_begin_src or stop_src may use TRIG_EXT.
 46 
 47 Configuration options - PCI224:
 48   [0] - PCI bus of device (optional).
 49   [1] - PCI slot of device (optional).
 50           If bus/slot is not specified, the first available PCI device
 51           will be used.
 52   [2] - Select available ranges according to jumper LK1.  All channels
 53         are set to the same range:
 54         0=Jumper position 1-2 (factory default), 4 software-selectable
 55           internal voltage references, giving 4 bipolar and 4 unipolar
 56           ranges:
 57             [-10V,+10V], [-5V,+5V], [-2.5V,+2.5V], [-1.25V,+1.25V],
 58             [0,+10V], [0,+5V], [0,+2.5V], [0,1.25V].
 59         1=Jumper position 2-3, 1 external voltage reference, giving
 60           1 bipolar and 1 unipolar range:
 61             [-Vext,+Vext], [0,+Vext].
 62 
 63 Configuration options - PCI234:
 64   [0] - PCI bus of device (optional).
 65   [1] - PCI slot of device (optional).
 66           If bus/slot is not specified, the first available PCI device
 67           will be used.
 68   [2] - Select internal or external voltage reference according to
 69         jumper LK1.  This affects all channels:
 70         0=Jumper position 1-2 (factory default), Vref=5V internal.
 71         1=Jumper position 2-3, Vref=Vext external.
 72   [3] - Select channel 0 range according to jumper LK2:
 73         0=Jumper position 2-3 (factory default), range [-2*Vref,+2*Vref]
 74           (10V bipolar when options[2]=0).
 75         1=Jumper position 1-2, range [-Vref,+Vref]
 76           (5V bipolar when options[2]=0).
 77   [4] - Select channel 1 range according to jumper LK3: cf. options[3].
 78   [5] - Select channel 2 range according to jumper LK4: cf. options[3].
 79   [6] - Select channel 3 range according to jumper LK5: cf. options[3].
 80 
 81 Passing a zero for an option is the same as leaving it unspecified.
 82 
 83 Caveats:
 84 
 85   1) All channels on the PCI224 share the same range.  Any change to the
 86      range as a result of insn_write or a streaming command will affect
 87      the output voltages of all channels, including those not specified
 88      by the instruction or command.
 89 
 90   2) For the analog output command,  the first scan may be triggered
 91      falsely at the start of acquisition.  This occurs when the DAC scan
 92      trigger source is switched from 'none' to 'timer' (scan_begin_src =
 93      TRIG_TIMER) or 'external' (scan_begin_src == TRIG_EXT) at the start
 94      of acquisition and the trigger source is at logic level 1 at the
 95      time of the switch.  This is very likely for TRIG_TIMER.  For
 96      TRIG_EXT, it depends on the state of the external line and whether
 97      the CR_INVERT flag has been set.  The remaining scans are triggered
 98      correctly.
 99 */
100 
101 #include <linux/module.h>
102 #include <linux/pci.h>
103 #include <linux/interrupt.h>
104 #include <linux/slab.h>
105 
106 #include "../comedidev.h"
107 
108 #include "comedi_fc.h"
109 #include "8253.h"
110 
111 #define DRIVER_NAME     "amplc_pci224"
112 
113 /*
114  * PCI IDs.
115  */
116 #define PCI_DEVICE_ID_AMPLICON_PCI224 0x0007
117 #define PCI_DEVICE_ID_AMPLICON_PCI234 0x0008
118 #define PCI_DEVICE_ID_INVALID 0xffff
119 
120 /*
121  * PCI224/234 i/o space 1 (PCIBAR2) registers.
122  */
123 #define PCI224_IO1_SIZE 0x20    /* Size of i/o space 1 (8-bit registers) */
124 #define PCI224_Z2_CT0   0x14    /* 82C54 counter/timer 0 */
125 #define PCI224_Z2_CT1   0x15    /* 82C54 counter/timer 1 */
126 #define PCI224_Z2_CT2   0x16    /* 82C54 counter/timer 2 */
127 #define PCI224_Z2_CTC   0x17    /* 82C54 counter/timer control word */
128 #define PCI224_ZCLK_SCE 0x1A    /* Group Z Clock Configuration Register */
129 #define PCI224_ZGAT_SCE 0x1D    /* Group Z Gate Configuration Register */
130 #define PCI224_INT_SCE  0x1E    /* ISR Interrupt source mask register */
131                                 /* /Interrupt status */
132 
133 /*
134  * PCI224/234 i/o space 2 (PCIBAR3) 16-bit registers.
135  */
136 #define PCI224_IO2_SIZE 0x10    /* Size of i/o space 2 (16-bit registers). */
137 #define PCI224_DACDATA  0x00    /* (w-o) DAC FIFO data. */
138 #define PCI224_SOFTTRIG 0x00    /* (r-o) DAC software scan trigger. */
139 #define PCI224_DACCON   0x02    /* (r/w) DAC status/configuration. */
140 #define PCI224_FIFOSIZ  0x04    /* (w-o) FIFO size for wraparound mode. */
141 #define PCI224_DACCEN   0x06    /* (w-o) DAC channel enable register. */
142 
143 /*
144  * DACCON values.
145  */
146 /* (r/w) Scan trigger. */
147 #define PCI224_DACCON_TRIG_MASK         (7 << 0)
148 #define PCI224_DACCON_TRIG_NONE         (0 << 0)        /* none */
149 #define PCI224_DACCON_TRIG_SW           (1 << 0)        /* software trig */
150 #define PCI224_DACCON_TRIG_EXTP         (2 << 0)        /* ext +ve edge */
151 #define PCI224_DACCON_TRIG_EXTN         (3 << 0)        /* ext -ve edge */
152 #define PCI224_DACCON_TRIG_Z2CT0        (4 << 0)        /* Z2 CT0 out */
153 #define PCI224_DACCON_TRIG_Z2CT1        (5 << 0)        /* Z2 CT1 out */
154 #define PCI224_DACCON_TRIG_Z2CT2        (6 << 0)        /* Z2 CT2 out */
155 /* (r/w) Polarity (PCI224 only, PCI234 always bipolar!). */
156 #define PCI224_DACCON_POLAR_MASK        (1 << 3)
157 #define PCI224_DACCON_POLAR_UNI         (0 << 3)        /* range [0,Vref] */
158 #define PCI224_DACCON_POLAR_BI          (1 << 3)        /* range [-Vref,Vref] */
159 /* (r/w) Internal Vref (PCI224 only, when LK1 in position 1-2). */
160 #define PCI224_DACCON_VREF_MASK         (3 << 4)
161 #define PCI224_DACCON_VREF_1_25         (0 << 4)        /* Vref = 1.25V */
162 #define PCI224_DACCON_VREF_2_5          (1 << 4)        /* Vref = 2.5V */
163 #define PCI224_DACCON_VREF_5            (2 << 4)        /* Vref = 5V */
164 #define PCI224_DACCON_VREF_10           (3 << 4)        /* Vref = 10V */
165 /* (r/w) Wraparound mode enable (to play back stored waveform). */
166 #define PCI224_DACCON_FIFOWRAP          (1 << 7)
167 /* (r/w) FIFO enable.  It MUST be set! */
168 #define PCI224_DACCON_FIFOENAB          (1 << 8)
169 /* (r/w) FIFO interrupt trigger level (most values are not very useful). */
170 #define PCI224_DACCON_FIFOINTR_MASK     (7 << 9)
171 #define PCI224_DACCON_FIFOINTR_EMPTY    (0 << 9)        /* when empty */
172 #define PCI224_DACCON_FIFOINTR_NEMPTY   (1 << 9)        /* when not empty */
173 #define PCI224_DACCON_FIFOINTR_NHALF    (2 << 9)        /* when not half full */
174 #define PCI224_DACCON_FIFOINTR_HALF     (3 << 9)        /* when half full */
175 #define PCI224_DACCON_FIFOINTR_NFULL    (4 << 9)        /* when not full */
176 #define PCI224_DACCON_FIFOINTR_FULL     (5 << 9)        /* when full */
177 /* (r-o) FIFO fill level. */
178 #define PCI224_DACCON_FIFOFL_MASK       (7 << 12)
179 #define PCI224_DACCON_FIFOFL_EMPTY      (1 << 12)       /* 0 */
180 #define PCI224_DACCON_FIFOFL_ONETOHALF  (0 << 12)       /* [1,2048] */
181 #define PCI224_DACCON_FIFOFL_HALFTOFULL (4 << 12)       /* [2049,4095] */
182 #define PCI224_DACCON_FIFOFL_FULL       (6 << 12)       /* 4096 */
183 /* (r-o) DAC busy flag. */
184 #define PCI224_DACCON_BUSY              (1 << 15)
185 /* (w-o) FIFO reset. */
186 #define PCI224_DACCON_FIFORESET         (1 << 12)
187 /* (w-o) Global reset (not sure what it does). */
188 #define PCI224_DACCON_GLOBALRESET       (1 << 13)
189 
190 /*
191  * DAC FIFO size.
192  */
193 #define PCI224_FIFO_SIZE        4096
194 
195 /*
196  * DAC FIFO guaranteed minimum room available, depending on reported fill level.
197  * The maximum room available depends on the reported fill level and how much
198  * has been written!
199  */
200 #define PCI224_FIFO_ROOM_EMPTY          PCI224_FIFO_SIZE
201 #define PCI224_FIFO_ROOM_ONETOHALF      (PCI224_FIFO_SIZE / 2)
202 #define PCI224_FIFO_ROOM_HALFTOFULL     1
203 #define PCI224_FIFO_ROOM_FULL           0
204 
205 /*
206  * Counter/timer clock input configuration sources.
207  */
208 #define CLK_CLK         0       /* reserved (channel-specific clock) */
209 #define CLK_10MHZ       1       /* internal 10 MHz clock */
210 #define CLK_1MHZ        2       /* internal 1 MHz clock */
211 #define CLK_100KHZ      3       /* internal 100 kHz clock */
212 #define CLK_10KHZ       4       /* internal 10 kHz clock */
213 #define CLK_1KHZ        5       /* internal 1 kHz clock */
214 #define CLK_OUTNM1      6       /* output of channel-1 modulo total */
215 #define CLK_EXT         7       /* external clock */
216 /* Macro to construct clock input configuration register value. */
217 #define CLK_CONFIG(chan, src)   ((((chan) & 3) << 3) | ((src) & 7))
218 
219 /*
220  * Counter/timer gate input configuration sources.
221  */
222 #define GAT_VCC         0       /* VCC (i.e. enabled) */
223 #define GAT_GND         1       /* GND (i.e. disabled) */
224 #define GAT_EXT         2       /* reserved (external gate input) */
225 #define GAT_NOUTNM2     3       /* inverted output of channel-2 modulo total */
226 /* Macro to construct gate input configuration register value. */
227 #define GAT_CONFIG(chan, src)   ((((chan) & 3) << 3) | ((src) & 7))
228 
229 /*
230  * Summary of CLK_OUTNM1 and GAT_NOUTNM2 connections for PCI224 and PCI234:
231  *
232  *              Channel's       Channel's
233  *              clock input     gate input
234  * Channel      CLK_OUTNM1      GAT_NOUTNM2
235  * -------      ----------      -----------
236  * Z2-CT0       Z2-CT2-OUT      /Z2-CT1-OUT
237  * Z2-CT1       Z2-CT0-OUT      /Z2-CT2-OUT
238  * Z2-CT2       Z2-CT1-OUT      /Z2-CT0-OUT
239  */
240 
241 /*
242  * Interrupt enable/status bits
243  */
244 #define PCI224_INTR_EXT         0x01    /* rising edge on external input */
245 #define PCI224_INTR_DAC         0x04    /* DAC (FIFO) interrupt */
246 #define PCI224_INTR_Z2CT1       0x20    /* rising edge on Z2-CT1 output */
247 
248 #define PCI224_INTR_EDGE_BITS   (PCI224_INTR_EXT | PCI224_INTR_Z2CT1)
249 #define PCI224_INTR_LEVEL_BITS  PCI224_INTR_DACFIFO
250 
251 /*
252  * Handy macros.
253  */
254 
255 /* Combine old and new bits. */
256 #define COMBINE(old, new, mask) (((old) & ~(mask)) | ((new) & (mask)))
257 
258 /* Current CPU.  XXX should this be hard_smp_processor_id()? */
259 #define THISCPU         smp_processor_id()
260 
261 /* State bits for use with atomic bit operations. */
262 #define AO_CMD_STARTED  0
263 
264 /*
265  * Range tables.
266  */
267 
268 /* The software selectable internal ranges for PCI224 (option[2] == 0). */
269 static const struct comedi_lrange range_pci224_internal = {
270         8, {
271                 BIP_RANGE(10),
272                 BIP_RANGE(5),
273                 BIP_RANGE(2.5),
274                 BIP_RANGE(1.25),
275                 UNI_RANGE(10),
276                 UNI_RANGE(5),
277                 UNI_RANGE(2.5),
278                 UNI_RANGE(1.25)
279         }
280 };
281 
282 static const unsigned short hwrange_pci224_internal[8] = {
283         PCI224_DACCON_POLAR_BI | PCI224_DACCON_VREF_10,
284         PCI224_DACCON_POLAR_BI | PCI224_DACCON_VREF_5,
285         PCI224_DACCON_POLAR_BI | PCI224_DACCON_VREF_2_5,
286         PCI224_DACCON_POLAR_BI | PCI224_DACCON_VREF_1_25,
287         PCI224_DACCON_POLAR_UNI | PCI224_DACCON_VREF_10,
288         PCI224_DACCON_POLAR_UNI | PCI224_DACCON_VREF_5,
289         PCI224_DACCON_POLAR_UNI | PCI224_DACCON_VREF_2_5,
290         PCI224_DACCON_POLAR_UNI | PCI224_DACCON_VREF_1_25,
291 };
292 
293 /* The software selectable external ranges for PCI224 (option[2] == 1). */
294 static const struct comedi_lrange range_pci224_external = {
295         2, {
296                 RANGE_ext(-1, 1),       /* bipolar [-Vref,+Vref] */
297                 RANGE_ext(0, 1)         /* unipolar [0,+Vref] */
298         }
299 };
300 
301 static const unsigned short hwrange_pci224_external[2] = {
302         PCI224_DACCON_POLAR_BI,
303         PCI224_DACCON_POLAR_UNI,
304 };
305 
306 /* The hardware selectable Vref*2 external range for PCI234
307  * (option[2] == 1, option[3+n] == 0). */
308 static const struct comedi_lrange range_pci234_ext2 = {
309         1, {
310                 RANGE_ext(-2, 2)
311         }
312 };
313 
314 /* The hardware selectable Vref external range for PCI234
315  * (option[2] == 1, option[3+n] == 1). */
316 static const struct comedi_lrange range_pci234_ext = {
317         1, {
318                 RANGE_ext(-1, 1)
319         }
320 };
321 
322 /* This serves for all the PCI234 ranges. */
323 static const unsigned short hwrange_pci234[1] = {
324         PCI224_DACCON_POLAR_BI, /* bipolar - hardware ignores it! */
325 };
326 
327 /*
328  * Board descriptions.
329  */
330 
331 enum pci224_model { any_model, pci224_model, pci234_model };
332 
333 struct pci224_board {
334         const char *name;
335         unsigned short devid;
336         enum pci224_model model;
337         unsigned int ao_chans;
338         unsigned int ao_bits;
339 };
340 
341 static const struct pci224_board pci224_boards[] = {
342         {
343          .name = "pci224",
344          .devid = PCI_DEVICE_ID_AMPLICON_PCI224,
345          .model = pci224_model,
346          .ao_chans = 16,
347          .ao_bits = 12,
348          },
349         {
350          .name = "pci234",
351          .devid = PCI_DEVICE_ID_AMPLICON_PCI234,
352          .model = pci234_model,
353          .ao_chans = 4,
354          .ao_bits = 16,
355          },
356         {
357          .name = DRIVER_NAME,
358          .devid = PCI_DEVICE_ID_INVALID,
359          .model = any_model,    /* wildcard */
360          },
361 };
362 
363 /* this structure is for data unique to this hardware driver.  If
364    several hardware drivers keep similar information in this structure,
365    feel free to suggest moving the variable to the struct comedi_device struct.  */
366 struct pci224_private {
367         const unsigned short *hwrange;
368         unsigned long iobase1;
369         unsigned long state;
370         spinlock_t ao_spinlock;
371         unsigned int *ao_readback;
372         unsigned short *ao_scan_vals;
373         unsigned char *ao_scan_order;
374         int intr_cpuid;
375         short intr_running;
376         unsigned short daccon;
377         unsigned int cached_div1;
378         unsigned int cached_div2;
379         unsigned int ao_stop_count;
380         short ao_stop_continuous;
381         unsigned short ao_enab; /* max 16 channels so 'short' will do */
382         unsigned char intsce;
383 };
384 
385 /*
386  * Called from the 'insn_write' function to perform a single write.
387  */
388 static void
389 pci224_ao_set_data(struct comedi_device *dev, int chan, int range,
390                    unsigned int data)
391 {
392         const struct pci224_board *thisboard = comedi_board(dev);
393         struct pci224_private *devpriv = dev->private;
394         unsigned short mangled;
395 
396         /* Store unmangled data for readback. */
397         devpriv->ao_readback[chan] = data;
398         /* Enable the channel. */
399         outw(1 << chan, dev->iobase + PCI224_DACCEN);
400         /* Set range and reset FIFO. */
401         devpriv->daccon = COMBINE(devpriv->daccon, devpriv->hwrange[range],
402                                   (PCI224_DACCON_POLAR_MASK |
403                                    PCI224_DACCON_VREF_MASK));
404         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
405              dev->iobase + PCI224_DACCON);
406         /*
407          * Mangle the data.  The hardware expects:
408          * - bipolar: 16-bit 2's complement
409          * - unipolar: 16-bit unsigned
410          */
411         mangled = (unsigned short)data << (16 - thisboard->ao_bits);
412         if ((devpriv->daccon & PCI224_DACCON_POLAR_MASK) ==
413             PCI224_DACCON_POLAR_BI) {
414                 mangled ^= 0x8000;
415         }
416         /* Write mangled data to the FIFO. */
417         outw(mangled, dev->iobase + PCI224_DACDATA);
418         /* Trigger the conversion. */
419         inw(dev->iobase + PCI224_SOFTTRIG);
420 }
421 
422 /*
423  * 'insn_write' function for AO subdevice.
424  */
425 static int
426 pci224_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
427                      struct comedi_insn *insn, unsigned int *data)
428 {
429         int i;
430         int chan, range;
431 
432         /* Unpack channel and range. */
433         chan = CR_CHAN(insn->chanspec);
434         range = CR_RANGE(insn->chanspec);
435 
436         /* Writing a list of values to an AO channel is probably not
437          * very useful, but that's how the interface is defined. */
438         for (i = 0; i < insn->n; i++)
439                 pci224_ao_set_data(dev, chan, range, data[i]);
440 
441         return i;
442 }
443 
444 /*
445  * 'insn_read' function for AO subdevice.
446  *
447  * N.B. The value read will not be valid if the DAC channel has
448  * never been written successfully since the device was attached
449  * or since the channel has been used by an AO streaming write
450  * command.
451  */
452 static int
453 pci224_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
454                     struct comedi_insn *insn, unsigned int *data)
455 {
456         struct pci224_private *devpriv = dev->private;
457         int i;
458         int chan;
459 
460         chan = CR_CHAN(insn->chanspec);
461 
462         for (i = 0; i < insn->n; i++)
463                 data[i] = devpriv->ao_readback[chan];
464 
465 
466         return i;
467 }
468 
469 /*
470  * Just a wrapper for the inline function 'i8253_cascade_ns_to_timer'.
471  */
472 static void
473 pci224_cascade_ns_to_timer(int osc_base, unsigned int *d1, unsigned int *d2,
474                            unsigned int *nanosec, int round_mode)
475 {
476         i8253_cascade_ns_to_timer(osc_base, d1, d2, nanosec, round_mode);
477 }
478 
479 /*
480  * Kills a command running on the AO subdevice.
481  */
482 static void pci224_ao_stop(struct comedi_device *dev,
483                            struct comedi_subdevice *s)
484 {
485         struct pci224_private *devpriv = dev->private;
486         unsigned long flags;
487 
488         if (!test_and_clear_bit(AO_CMD_STARTED, &devpriv->state))
489                 return;
490 
491 
492         spin_lock_irqsave(&devpriv->ao_spinlock, flags);
493         /* Kill the interrupts. */
494         devpriv->intsce = 0;
495         outb(0, devpriv->iobase1 + PCI224_INT_SCE);
496         /*
497          * Interrupt routine may or may not be running.  We may or may not
498          * have been called from the interrupt routine (directly or
499          * indirectly via a comedi_events() callback routine).  It's highly
500          * unlikely that we've been called from some other interrupt routine
501          * but who knows what strange things coders get up to!
502          *
503          * If the interrupt routine is currently running, wait for it to
504          * finish, unless we appear to have been called via the interrupt
505          * routine.
506          */
507         while (devpriv->intr_running && devpriv->intr_cpuid != THISCPU) {
508                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
509                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
510         }
511         spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
512         /* Reconfigure DAC for insn_write usage. */
513         outw(0, dev->iobase + PCI224_DACCEN);   /* Disable channels. */
514         devpriv->daccon = COMBINE(devpriv->daccon,
515                                   PCI224_DACCON_TRIG_SW |
516                                   PCI224_DACCON_FIFOINTR_EMPTY,
517                                   PCI224_DACCON_TRIG_MASK |
518                                   PCI224_DACCON_FIFOINTR_MASK);
519         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
520              dev->iobase + PCI224_DACCON);
521 }
522 
523 /*
524  * Handles start of acquisition for the AO subdevice.
525  */
526 static void pci224_ao_start(struct comedi_device *dev,
527                             struct comedi_subdevice *s)
528 {
529         struct pci224_private *devpriv = dev->private;
530         struct comedi_cmd *cmd = &s->async->cmd;
531         unsigned long flags;
532 
533         set_bit(AO_CMD_STARTED, &devpriv->state);
534         if (!devpriv->ao_stop_continuous && devpriv->ao_stop_count == 0) {
535                 /* An empty acquisition! */
536                 s->async->events |= COMEDI_CB_EOA;
537                 cfc_handle_events(dev, s);
538         } else {
539                 /* Enable interrupts. */
540                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
541                 if (cmd->stop_src == TRIG_EXT)
542                         devpriv->intsce = PCI224_INTR_EXT | PCI224_INTR_DAC;
543                 else
544                         devpriv->intsce = PCI224_INTR_DAC;
545 
546                 outb(devpriv->intsce, devpriv->iobase1 + PCI224_INT_SCE);
547                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
548         }
549 }
550 
551 /*
552  * Handles interrupts from the DAC FIFO.
553  */
554 static void pci224_ao_handle_fifo(struct comedi_device *dev,
555                                   struct comedi_subdevice *s)
556 {
557         struct pci224_private *devpriv = dev->private;
558         struct comedi_cmd *cmd = &s->async->cmd;
559         unsigned int num_scans;
560         unsigned int room;
561         unsigned short dacstat;
562         unsigned int i, n;
563         unsigned int bytes_per_scan;
564 
565         if (cmd->chanlist_len) {
566                 bytes_per_scan = cmd->chanlist_len * sizeof(short);
567         } else {
568                 /* Shouldn't get here! */
569                 bytes_per_scan = sizeof(short);
570         }
571         /* Determine number of scans available in buffer. */
572         num_scans = comedi_buf_read_n_available(s->async) / bytes_per_scan;
573         if (!devpriv->ao_stop_continuous) {
574                 /* Fixed number of scans. */
575                 if (num_scans > devpriv->ao_stop_count)
576                         num_scans = devpriv->ao_stop_count;
577 
578         }
579 
580         /* Determine how much room is in the FIFO (in samples). */
581         dacstat = inw(dev->iobase + PCI224_DACCON);
582         switch (dacstat & PCI224_DACCON_FIFOFL_MASK) {
583         case PCI224_DACCON_FIFOFL_EMPTY:
584                 room = PCI224_FIFO_ROOM_EMPTY;
585                 if (!devpriv->ao_stop_continuous && devpriv->ao_stop_count == 0) {
586                         /* FIFO empty at end of counted acquisition. */
587                         s->async->events |= COMEDI_CB_EOA;
588                         cfc_handle_events(dev, s);
589                         return;
590                 }
591                 break;
592         case PCI224_DACCON_FIFOFL_ONETOHALF:
593                 room = PCI224_FIFO_ROOM_ONETOHALF;
594                 break;
595         case PCI224_DACCON_FIFOFL_HALFTOFULL:
596                 room = PCI224_FIFO_ROOM_HALFTOFULL;
597                 break;
598         default:
599                 room = PCI224_FIFO_ROOM_FULL;
600                 break;
601         }
602         if (room >= PCI224_FIFO_ROOM_ONETOHALF) {
603                 /* FIFO is less than half-full. */
604                 if (num_scans == 0) {
605                         /* Nothing left to put in the FIFO. */
606                         dev_err(dev->class_dev, "AO buffer underrun\n");
607                         s->async->events |= COMEDI_CB_OVERFLOW;
608                 }
609         }
610         /* Determine how many new scans can be put in the FIFO. */
611         if (cmd->chanlist_len)
612                 room /= cmd->chanlist_len;
613 
614         /* Determine how many scans to process. */
615         if (num_scans > room)
616                 num_scans = room;
617 
618         /* Process scans. */
619         for (n = 0; n < num_scans; n++) {
620                 cfc_read_array_from_buffer(s, &devpriv->ao_scan_vals[0],
621                                            bytes_per_scan);
622                 for (i = 0; i < cmd->chanlist_len; i++) {
623                         outw(devpriv->ao_scan_vals[devpriv->ao_scan_order[i]],
624                              dev->iobase + PCI224_DACDATA);
625                 }
626         }
627         if (!devpriv->ao_stop_continuous) {
628                 devpriv->ao_stop_count -= num_scans;
629                 if (devpriv->ao_stop_count == 0) {
630                         /*
631                          * Change FIFO interrupt trigger level to wait
632                          * until FIFO is empty.
633                          */
634                         devpriv->daccon = COMBINE(devpriv->daccon,
635                                                   PCI224_DACCON_FIFOINTR_EMPTY,
636                                                   PCI224_DACCON_FIFOINTR_MASK);
637                         outw(devpriv->daccon, dev->iobase + PCI224_DACCON);
638                 }
639         }
640         if ((devpriv->daccon & PCI224_DACCON_TRIG_MASK) ==
641             PCI224_DACCON_TRIG_NONE) {
642                 unsigned short trig;
643 
644                 /*
645                  * This is the initial DAC FIFO interrupt at the
646                  * start of the acquisition.  The DAC's scan trigger
647                  * has been set to 'none' up until now.
648                  *
649                  * Now that data has been written to the FIFO, the
650                  * DAC's scan trigger source can be set to the
651                  * correct value.
652                  *
653                  * BUG: The first scan will be triggered immediately
654                  * if the scan trigger source is at logic level 1.
655                  */
656                 if (cmd->scan_begin_src == TRIG_TIMER) {
657                         trig = PCI224_DACCON_TRIG_Z2CT0;
658                 } else {
659                         /* cmd->scan_begin_src == TRIG_EXT */
660                         if (cmd->scan_begin_arg & CR_INVERT)
661                                 trig = PCI224_DACCON_TRIG_EXTN;
662                         else
663                                 trig = PCI224_DACCON_TRIG_EXTP;
664 
665                 }
666                 devpriv->daccon = COMBINE(devpriv->daccon, trig,
667                                           PCI224_DACCON_TRIG_MASK);
668                 outw(devpriv->daccon, dev->iobase + PCI224_DACCON);
669         }
670 
671         cfc_handle_events(dev, s);
672 }
673 
674 /*
675  * Internal trigger function to start acquisition on AO subdevice.
676  */
677 static int
678 pci224_ao_inttrig_start(struct comedi_device *dev, struct comedi_subdevice *s,
679                         unsigned int trignum)
680 {
681         if (trignum != 0)
682                 return -EINVAL;
683 
684         s->async->inttrig = NULL;
685         pci224_ao_start(dev, s);
686 
687         return 1;
688 }
689 
690 #define MAX_SCAN_PERIOD         0xFFFFFFFFU
691 #define MIN_SCAN_PERIOD         2500
692 #define CONVERT_PERIOD          625
693 
694 /*
695  * 'do_cmdtest' function for AO subdevice.
696  */
697 static int
698 pci224_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
699                   struct comedi_cmd *cmd)
700 {
701         struct pci224_private *devpriv = dev->private;
702         int err = 0;
703         unsigned int tmp;
704 
705         /* Step 1 : check if triggers are trivially valid */
706 
707         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
708         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
709                                         TRIG_EXT | TRIG_TIMER);
710         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
711         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
712         err |= cfc_check_trigger_src(&cmd->stop_src,
713                                         TRIG_COUNT | TRIG_EXT | TRIG_NONE);
714 
715         if (err)
716                 return 1;
717 
718         /* Step 2a : make sure trigger sources are unique */
719 
720         err |= cfc_check_trigger_is_unique(cmd->start_src);
721         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
722         err |= cfc_check_trigger_is_unique(cmd->stop_src);
723 
724         /* Step 2b : and mutually compatible */
725 
726         /*
727          * There's only one external trigger signal (which makes these
728          * tests easier).  Only one thing can use it.
729          */
730         tmp = 0;
731         if (cmd->start_src & TRIG_EXT)
732                 tmp++;
733         if (cmd->scan_begin_src & TRIG_EXT)
734                 tmp++;
735         if (cmd->stop_src & TRIG_EXT)
736                 tmp++;
737         if (tmp > 1)
738                 err |= -EINVAL;
739 
740         if (err)
741                 return 2;
742 
743         /* Step 3: check if arguments are trivially valid */
744 
745         switch (cmd->start_src) {
746         case TRIG_INT:
747                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
748                 break;
749         case TRIG_EXT:
750                 /* Force to external trigger 0. */
751                 if ((cmd->start_arg & ~CR_FLAGS_MASK) != 0) {
752                         cmd->start_arg = COMBINE(cmd->start_arg, 0,
753                                                  ~CR_FLAGS_MASK);
754                         err |= -EINVAL;
755                 }
756                 /* The only flag allowed is CR_EDGE, which is ignored. */
757                 if ((cmd->start_arg & CR_FLAGS_MASK & ~CR_EDGE) != 0) {
758                         cmd->start_arg = COMBINE(cmd->start_arg, 0,
759                                                  CR_FLAGS_MASK & ~CR_EDGE);
760                         err |= -EINVAL;
761                 }
762                 break;
763         }
764 
765         switch (cmd->scan_begin_src) {
766         case TRIG_TIMER:
767                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
768                                                  MAX_SCAN_PERIOD);
769 
770                 tmp = cmd->chanlist_len * CONVERT_PERIOD;
771                 if (tmp < MIN_SCAN_PERIOD)
772                         tmp = MIN_SCAN_PERIOD;
773                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg, tmp);
774                 break;
775         case TRIG_EXT:
776                 /* Force to external trigger 0. */
777                 if ((cmd->scan_begin_arg & ~CR_FLAGS_MASK) != 0) {
778                         cmd->scan_begin_arg = COMBINE(cmd->scan_begin_arg, 0,
779                                                       ~CR_FLAGS_MASK);
780                         err |= -EINVAL;
781                 }
782                 /* Only allow flags CR_EDGE and CR_INVERT.  Ignore CR_EDGE. */
783                 if ((cmd->scan_begin_arg & CR_FLAGS_MASK &
784                      ~(CR_EDGE | CR_INVERT)) != 0) {
785                         cmd->scan_begin_arg = COMBINE(cmd->scan_begin_arg, 0,
786                                                       CR_FLAGS_MASK &
787                                                       ~(CR_EDGE | CR_INVERT));
788                         err |= -EINVAL;
789                 }
790                 break;
791         }
792 
793         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
794         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
795 
796         switch (cmd->stop_src) {
797         case TRIG_COUNT:
798                 /* Any count allowed. */
799                 break;
800         case TRIG_EXT:
801                 /* Force to external trigger 0. */
802                 if ((cmd->stop_arg & ~CR_FLAGS_MASK) != 0) {
803                         cmd->stop_arg = COMBINE(cmd->stop_arg, 0,
804                                                 ~CR_FLAGS_MASK);
805                         err |= -EINVAL;
806                 }
807                 /* The only flag allowed is CR_EDGE, which is ignored. */
808                 if ((cmd->stop_arg & CR_FLAGS_MASK & ~CR_EDGE) != 0) {
809                         cmd->stop_arg = COMBINE(cmd->stop_arg, 0,
810                                                 CR_FLAGS_MASK & ~CR_EDGE);
811                 }
812                 break;
813         case TRIG_NONE:
814                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
815                 break;
816         }
817 
818         if (err)
819                 return 3;
820 
821         /* Step 4: fix up any arguments. */
822 
823         if (cmd->scan_begin_src == TRIG_TIMER) {
824                 unsigned int div1, div2, round;
825                 int round_mode = cmd->flags & TRIG_ROUND_MASK;
826 
827                 tmp = cmd->scan_begin_arg;
828                 /* Check whether to use a single timer. */
829                 switch (round_mode) {
830                 case TRIG_ROUND_NEAREST:
831                 default:
832                         round = I8254_OSC_BASE_10MHZ / 2;
833                         break;
834                 case TRIG_ROUND_DOWN:
835                         round = 0;
836                         break;
837                 case TRIG_ROUND_UP:
838                         round = I8254_OSC_BASE_10MHZ - 1;
839                         break;
840                 }
841                 /* Be careful to avoid overflow! */
842                 div2 = cmd->scan_begin_arg / I8254_OSC_BASE_10MHZ;
843                 div2 += (round + cmd->scan_begin_arg % I8254_OSC_BASE_10MHZ) /
844                         I8254_OSC_BASE_10MHZ;
845                 if (div2 <= 0x10000) {
846                         /* A single timer will suffice. */
847                         if (div2 < 2)
848                                 div2 = 2;
849                         cmd->scan_begin_arg = div2 * I8254_OSC_BASE_10MHZ;
850                         if (cmd->scan_begin_arg < div2 ||
851                             cmd->scan_begin_arg < I8254_OSC_BASE_10MHZ) {
852                                 /* Overflow! */
853                                 cmd->scan_begin_arg = MAX_SCAN_PERIOD;
854                         }
855                 } else {
856                         /* Use two timers. */
857                         div1 = devpriv->cached_div1;
858                         div2 = devpriv->cached_div2;
859                         pci224_cascade_ns_to_timer(I8254_OSC_BASE_10MHZ,
860                                                    &div1, &div2,
861                                                    &cmd->scan_begin_arg,
862                                                    round_mode);
863                         devpriv->cached_div1 = div1;
864                         devpriv->cached_div2 = div2;
865                 }
866                 if (tmp != cmd->scan_begin_arg)
867                         err++;
868 
869         }
870 
871         if (err)
872                 return 4;
873 
874         /* Step 5: check channel list. */
875 
876         if (cmd->chanlist && (cmd->chanlist_len > 0)) {
877                 unsigned int range;
878                 enum { range_err = 1, dupchan_err = 2, };
879                 unsigned errors;
880                 unsigned int n;
881                 unsigned int ch;
882 
883                 /*
884                  * Check all channels have the same range index.  Don't care
885                  * about analogue reference, as we can't configure it.
886                  *
887                  * Check the list has no duplicate channels.
888                  */
889                 range = CR_RANGE(cmd->chanlist[0]);
890                 errors = 0;
891                 tmp = 0;
892                 for (n = 0; n < cmd->chanlist_len; n++) {
893                         ch = CR_CHAN(cmd->chanlist[n]);
894                         if (tmp & (1U << ch))
895                                 errors |= dupchan_err;
896 
897                         tmp |= (1U << ch);
898                         if (CR_RANGE(cmd->chanlist[n]) != range)
899                                 errors |= range_err;
900 
901                 }
902                 if (errors) {
903                         if (errors & dupchan_err) {
904                                 dev_dbg(dev->class_dev,
905                                         "%s: entries in chanlist must contain no duplicate channels\n",
906                                         __func__);
907                         }
908                         if (errors & range_err) {
909                                 dev_dbg(dev->class_dev,
910                                         "%s: entries in chanlist must all have the same range index\n",
911                                         __func__);
912                         }
913                         err++;
914                 }
915         }
916 
917         if (err)
918                 return 5;
919 
920         return 0;
921 }
922 
923 /*
924  * 'do_cmd' function for AO subdevice.
925  */
926 static int pci224_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
927 {
928         struct pci224_private *devpriv = dev->private;
929         struct comedi_cmd *cmd = &s->async->cmd;
930         int range;
931         unsigned int i, j;
932         unsigned int ch;
933         unsigned int rank;
934         unsigned long flags;
935 
936         /* Cannot handle null/empty chanlist. */
937         if (cmd->chanlist == NULL || cmd->chanlist_len == 0)
938                 return -EINVAL;
939 
940 
941         /* Determine which channels are enabled and their load order.  */
942         devpriv->ao_enab = 0;
943 
944         for (i = 0; i < cmd->chanlist_len; i++) {
945                 ch = CR_CHAN(cmd->chanlist[i]);
946                 devpriv->ao_enab |= 1U << ch;
947                 rank = 0;
948                 for (j = 0; j < cmd->chanlist_len; j++) {
949                         if (CR_CHAN(cmd->chanlist[j]) < ch)
950                                 rank++;
951 
952                 }
953                 devpriv->ao_scan_order[rank] = i;
954         }
955 
956         /* Set enabled channels. */
957         outw(devpriv->ao_enab, dev->iobase + PCI224_DACCEN);
958 
959         /* Determine range and polarity.  All channels the same.  */
960         range = CR_RANGE(cmd->chanlist[0]);
961 
962         /*
963          * Set DAC range and polarity.
964          * Set DAC scan trigger source to 'none'.
965          * Set DAC FIFO interrupt trigger level to 'not half full'.
966          * Reset DAC FIFO.
967          *
968          * N.B. DAC FIFO interrupts are currently disabled.
969          */
970         devpriv->daccon = COMBINE(devpriv->daccon,
971                                   (devpriv->
972                                    hwrange[range] | PCI224_DACCON_TRIG_NONE |
973                                    PCI224_DACCON_FIFOINTR_NHALF),
974                                   (PCI224_DACCON_POLAR_MASK |
975                                    PCI224_DACCON_VREF_MASK |
976                                    PCI224_DACCON_TRIG_MASK |
977                                    PCI224_DACCON_FIFOINTR_MASK));
978         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
979              dev->iobase + PCI224_DACCON);
980 
981         if (cmd->scan_begin_src == TRIG_TIMER) {
982                 unsigned int div1, div2, round;
983                 unsigned int ns = cmd->scan_begin_arg;
984                 int round_mode = cmd->flags & TRIG_ROUND_MASK;
985 
986                 /* Check whether to use a single timer. */
987                 switch (round_mode) {
988                 case TRIG_ROUND_NEAREST:
989                 default:
990                         round = I8254_OSC_BASE_10MHZ / 2;
991                         break;
992                 case TRIG_ROUND_DOWN:
993                         round = 0;
994                         break;
995                 case TRIG_ROUND_UP:
996                         round = I8254_OSC_BASE_10MHZ - 1;
997                         break;
998                 }
999                 /* Be careful to avoid overflow! */
1000                 div2 = cmd->scan_begin_arg / I8254_OSC_BASE_10MHZ;
1001                 div2 += (round + cmd->scan_begin_arg % I8254_OSC_BASE_10MHZ) /
1002                         I8254_OSC_BASE_10MHZ;
1003                 if (div2 <= 0x10000) {
1004                         /* A single timer will suffice. */
1005                         if (div2 < 2)
1006                                 div2 = 2;
1007                         div2 &= 0xffff;
1008                         div1 = 1;       /* Flag that single timer to be used. */
1009                 } else {
1010                         /* Use two timers. */
1011                         div1 = devpriv->cached_div1;
1012                         div2 = devpriv->cached_div2;
1013                         pci224_cascade_ns_to_timer(I8254_OSC_BASE_10MHZ,
1014                                                    &div1, &div2,
1015                                                    &ns, round_mode);
1016                 }
1017 
1018                 /*
1019                  * The output of timer Z2-0 will be used as the scan trigger
1020                  * source.
1021                  */
1022                 /* Make sure Z2-0 is gated on.  */
1023                 outb(GAT_CONFIG(0, GAT_VCC),
1024                      devpriv->iobase1 + PCI224_ZGAT_SCE);
1025                 if (div1 == 1) {
1026                         /* Not cascading.  Z2-0 needs 10 MHz clock. */
1027                         outb(CLK_CONFIG(0, CLK_10MHZ),
1028                              devpriv->iobase1 + PCI224_ZCLK_SCE);
1029                 } else {
1030                         /* Cascading with Z2-2. */
1031                         /* Make sure Z2-2 is gated on.  */
1032                         outb(GAT_CONFIG(2, GAT_VCC),
1033                              devpriv->iobase1 + PCI224_ZGAT_SCE);
1034                         /* Z2-2 needs 10 MHz clock. */
1035                         outb(CLK_CONFIG(2, CLK_10MHZ),
1036                              devpriv->iobase1 + PCI224_ZCLK_SCE);
1037                         /* Load Z2-2 mode (2) and counter (div1). */
1038                         i8254_load(devpriv->iobase1 + PCI224_Z2_CT0, 0,
1039                                    2, div1, 2);
1040                         /* Z2-0 is clocked from Z2-2's output. */
1041                         outb(CLK_CONFIG(0, CLK_OUTNM1),
1042                              devpriv->iobase1 + PCI224_ZCLK_SCE);
1043                 }
1044                 /* Load Z2-0 mode (2) and counter (div2). */
1045                 i8254_load(devpriv->iobase1 + PCI224_Z2_CT0, 0, 0, div2, 2);
1046         }
1047 
1048         /*
1049          * Sort out end of acquisition.
1050          */
1051         switch (cmd->stop_src) {
1052         case TRIG_COUNT:
1053                 /* Fixed number of scans.  */
1054                 devpriv->ao_stop_continuous = 0;
1055                 devpriv->ao_stop_count = cmd->stop_arg;
1056                 break;
1057         default:
1058                 /* Continuous scans. */
1059                 devpriv->ao_stop_continuous = 1;
1060                 devpriv->ao_stop_count = 0;
1061                 break;
1062         }
1063 
1064         /*
1065          * Sort out start of acquisition.
1066          */
1067         switch (cmd->start_src) {
1068         case TRIG_INT:
1069                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1070                 s->async->inttrig = &pci224_ao_inttrig_start;
1071                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1072                 break;
1073         case TRIG_EXT:
1074                 /* Enable external interrupt trigger to start acquisition. */
1075                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1076                 devpriv->intsce |= PCI224_INTR_EXT;
1077                 outb(devpriv->intsce, devpriv->iobase1 + PCI224_INT_SCE);
1078                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1079                 break;
1080         }
1081 
1082         return 0;
1083 }
1084 
1085 /*
1086  * 'cancel' function for AO subdevice.
1087  */
1088 static int pci224_ao_cancel(struct comedi_device *dev,
1089                             struct comedi_subdevice *s)
1090 {
1091         pci224_ao_stop(dev, s);
1092         return 0;
1093 }
1094 
1095 /*
1096  * 'munge' data for AO command.
1097  */
1098 static void
1099 pci224_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1100                 void *data, unsigned int num_bytes, unsigned int chan_index)
1101 {
1102         const struct pci224_board *thisboard = comedi_board(dev);
1103         struct pci224_private *devpriv = dev->private;
1104         struct comedi_async *async = s->async;
1105         unsigned short *array = data;
1106         unsigned int length = num_bytes / sizeof(*array);
1107         unsigned int offset;
1108         unsigned int shift;
1109         unsigned int i;
1110 
1111         /* The hardware expects 16-bit numbers. */
1112         shift = 16 - thisboard->ao_bits;
1113         /* Channels will be all bipolar or all unipolar. */
1114         if ((devpriv->hwrange[CR_RANGE(async->cmd.chanlist[0])] &
1115              PCI224_DACCON_POLAR_MASK) == PCI224_DACCON_POLAR_UNI) {
1116                 /* Unipolar */
1117                 offset = 0;
1118         } else {
1119                 /* Bipolar */
1120                 offset = 32768;
1121         }
1122         /* Munge the data. */
1123         for (i = 0; i < length; i++)
1124                 array[i] = (array[i] << shift) - offset;
1125 
1126 }
1127 
1128 /*
1129  * Interrupt handler.
1130  */
1131 static irqreturn_t pci224_interrupt(int irq, void *d)
1132 {
1133         struct comedi_device *dev = d;
1134         struct pci224_private *devpriv = dev->private;
1135         struct comedi_subdevice *s = dev->write_subdev;
1136         struct comedi_cmd *cmd;
1137         unsigned char intstat, valid_intstat;
1138         unsigned char curenab;
1139         int retval = 0;
1140         unsigned long flags;
1141 
1142         intstat = inb(devpriv->iobase1 + PCI224_INT_SCE) & 0x3F;
1143         if (intstat) {
1144                 retval = 1;
1145                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1146                 valid_intstat = devpriv->intsce & intstat;
1147                 /* Temporarily disable interrupt sources. */
1148                 curenab = devpriv->intsce & ~intstat;
1149                 outb(curenab, devpriv->iobase1 + PCI224_INT_SCE);
1150                 devpriv->intr_running = 1;
1151                 devpriv->intr_cpuid = THISCPU;
1152                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1153                 if (valid_intstat != 0) {
1154                         cmd = &s->async->cmd;
1155                         if (valid_intstat & PCI224_INTR_EXT) {
1156                                 devpriv->intsce &= ~PCI224_INTR_EXT;
1157                                 if (cmd->start_src == TRIG_EXT)
1158                                         pci224_ao_start(dev, s);
1159                                 else if (cmd->stop_src == TRIG_EXT)
1160                                         pci224_ao_stop(dev, s);
1161 
1162                         }
1163                         if (valid_intstat & PCI224_INTR_DAC)
1164                                 pci224_ao_handle_fifo(dev, s);
1165 
1166                 }
1167                 /* Reenable interrupt sources. */
1168                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1169                 if (curenab != devpriv->intsce) {
1170                         outb(devpriv->intsce,
1171                              devpriv->iobase1 + PCI224_INT_SCE);
1172                 }
1173                 devpriv->intr_running = 0;
1174                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1175         }
1176         return IRQ_RETVAL(retval);
1177 }
1178 
1179 /*
1180  * This function looks for a board matching the supplied PCI device.
1181  */
1182 static const struct pci224_board
1183 *pci224_find_pci_board(struct pci_dev *pci_dev)
1184 {
1185         int i;
1186 
1187         for (i = 0; i < ARRAY_SIZE(pci224_boards); i++)
1188                 if (pci_dev->device == pci224_boards[i].devid)
1189                         return &pci224_boards[i];
1190         return NULL;
1191 }
1192 
1193 /*
1194  * This function looks for a PCI device matching the requested board name,
1195  * bus and slot.
1196  */
1197 static struct pci_dev *pci224_find_pci_dev(struct comedi_device *dev,
1198                                            struct comedi_devconfig *it)
1199 {
1200         const struct pci224_board *thisboard = comedi_board(dev);
1201         struct pci_dev *pci_dev = NULL;
1202         int bus = it->options[0];
1203         int slot = it->options[1];
1204 
1205         for_each_pci_dev(pci_dev) {
1206                 if (bus || slot) {
1207                         if (bus != pci_dev->bus->number ||
1208                             slot != PCI_SLOT(pci_dev->devfn))
1209                                 continue;
1210                 }
1211                 if (pci_dev->vendor != PCI_VENDOR_ID_AMPLICON)
1212                         continue;
1213 
1214                 if (thisboard->model == any_model) {
1215                         /* Match any supported model. */
1216                         const struct pci224_board *board_ptr;
1217 
1218                         board_ptr = pci224_find_pci_board(pci_dev);
1219                         if (board_ptr == NULL)
1220                                 continue;
1221                         /* Change board_ptr to matched board. */
1222                         dev->board_ptr = board_ptr;
1223                 } else {
1224                         /* Match specific model name. */
1225                         if (thisboard->devid != pci_dev->device)
1226                                 continue;
1227                 }
1228                 return pci_dev;
1229         }
1230         dev_err(dev->class_dev,
1231                 "No supported board found! (req. bus %d, slot %d)\n",
1232                 bus, slot);
1233         return NULL;
1234 }
1235 
1236 /*
1237  * Common part of attach and auto_attach.
1238  */
1239 static int pci224_attach_common(struct comedi_device *dev,
1240                                 struct pci_dev *pci_dev, int *options)
1241 {
1242         const struct pci224_board *thisboard = comedi_board(dev);
1243         struct pci224_private *devpriv = dev->private;
1244         struct comedi_subdevice *s;
1245         unsigned int irq;
1246         unsigned n;
1247         int ret;
1248 
1249         comedi_set_hw_dev(dev, &pci_dev->dev);
1250 
1251         ret = comedi_pci_enable(dev);
1252         if (ret)
1253                 return ret;
1254 
1255         spin_lock_init(&devpriv->ao_spinlock);
1256 
1257         devpriv->iobase1 = pci_resource_start(pci_dev, 2);
1258         dev->iobase = pci_resource_start(pci_dev, 3);
1259         irq = pci_dev->irq;
1260 
1261         /* Allocate readback buffer for AO channels. */
1262         devpriv->ao_readback = kmalloc(sizeof(devpriv->ao_readback[0]) *
1263                                        thisboard->ao_chans, GFP_KERNEL);
1264         if (!devpriv->ao_readback)
1265                 return -ENOMEM;
1266 
1267 
1268         /* Allocate buffer to hold values for AO channel scan. */
1269         devpriv->ao_scan_vals = kmalloc(sizeof(devpriv->ao_scan_vals[0]) *
1270                                         thisboard->ao_chans, GFP_KERNEL);
1271         if (!devpriv->ao_scan_vals)
1272                 return -ENOMEM;
1273 
1274 
1275         /* Allocate buffer to hold AO channel scan order. */
1276         devpriv->ao_scan_order = kmalloc(sizeof(devpriv->ao_scan_order[0]) *
1277                                          thisboard->ao_chans, GFP_KERNEL);
1278         if (!devpriv->ao_scan_order)
1279                 return -ENOMEM;
1280 
1281 
1282         /* Disable interrupt sources. */
1283         devpriv->intsce = 0;
1284         outb(0, devpriv->iobase1 + PCI224_INT_SCE);
1285 
1286         /* Initialize the DAC hardware. */
1287         outw(PCI224_DACCON_GLOBALRESET, dev->iobase + PCI224_DACCON);
1288         outw(0, dev->iobase + PCI224_DACCEN);
1289         outw(0, dev->iobase + PCI224_FIFOSIZ);
1290         devpriv->daccon = (PCI224_DACCON_TRIG_SW | PCI224_DACCON_POLAR_BI |
1291                            PCI224_DACCON_FIFOENAB |
1292                            PCI224_DACCON_FIFOINTR_EMPTY);
1293         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
1294              dev->iobase + PCI224_DACCON);
1295 
1296         ret = comedi_alloc_subdevices(dev, 1);
1297         if (ret)
1298                 return ret;
1299 
1300         s = &dev->subdevices[0];
1301         /* Analog output subdevice. */
1302         s->type = COMEDI_SUBD_AO;
1303         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1304         s->n_chan = thisboard->ao_chans;
1305         s->maxdata = (1 << thisboard->ao_bits) - 1;
1306         s->insn_write = &pci224_ao_insn_write;
1307         s->insn_read = &pci224_ao_insn_read;
1308         s->len_chanlist = s->n_chan;
1309 
1310         dev->write_subdev = s;
1311         s->do_cmd = &pci224_ao_cmd;
1312         s->do_cmdtest = &pci224_ao_cmdtest;
1313         s->cancel = &pci224_ao_cancel;
1314         s->munge = &pci224_ao_munge;
1315 
1316         /* Sort out channel range options. */
1317         if (thisboard->model == pci234_model) {
1318                 /* PCI234 range options. */
1319                 const struct comedi_lrange **range_table_list;
1320 
1321                 s->range_table_list = range_table_list =
1322                     kmalloc(sizeof(struct comedi_lrange *) * s->n_chan,
1323                             GFP_KERNEL);
1324                 if (!s->range_table_list)
1325                         return -ENOMEM;
1326 
1327                 if (options) {
1328                         for (n = 2; n < 3 + s->n_chan; n++) {
1329                                 if (options[n] < 0 || options[n] > 1) {
1330                                         dev_warn(dev->class_dev, DRIVER_NAME
1331                                                  ": warning! bad options[%u]=%d\n",
1332                                                  n, options[n]);
1333                                 }
1334                         }
1335                 }
1336                 for (n = 0; n < s->n_chan; n++) {
1337                         if (n < COMEDI_NDEVCONFOPTS - 3 && options &&
1338                             options[3 + n] == 1) {
1339                                 if (options[2] == 1)
1340                                         range_table_list[n] = &range_pci234_ext;
1341                                 else
1342                                         range_table_list[n] = &range_bipolar5;
1343 
1344                         } else {
1345                                 if (options && options[2] == 1) {
1346                                         range_table_list[n] =
1347                                             &range_pci234_ext2;
1348                                 } else {
1349                                         range_table_list[n] = &range_bipolar10;
1350                                 }
1351                         }
1352                 }
1353                 devpriv->hwrange = hwrange_pci234;
1354         } else {
1355                 /* PCI224 range options. */
1356                 if (options && options[2] == 1) {
1357                         s->range_table = &range_pci224_external;
1358                         devpriv->hwrange = hwrange_pci224_external;
1359                 } else {
1360                         if (options && options[2] != 0) {
1361                                 dev_warn(dev->class_dev, DRIVER_NAME
1362                                          ": warning! bad options[2]=%d\n",
1363                                          options[2]);
1364                         }
1365                         s->range_table = &range_pci224_internal;
1366                         devpriv->hwrange = hwrange_pci224_internal;
1367                 }
1368         }
1369 
1370         dev->board_name = thisboard->name;
1371 
1372         if (irq) {
1373                 ret = request_irq(irq, pci224_interrupt, IRQF_SHARED,
1374                                   DRIVER_NAME, dev);
1375                 if (ret < 0) {
1376                         dev_err(dev->class_dev,
1377                                 "error! unable to allocate irq %u\n", irq);
1378                         return ret;
1379                 } else {
1380                         dev->irq = irq;
1381                 }
1382         }
1383 
1384         return 0;
1385 }
1386 
1387 static int pci224_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1388 {
1389         struct pci224_private *devpriv;
1390         struct pci_dev *pci_dev;
1391 
1392         dev_info(dev->class_dev, DRIVER_NAME ": attach\n");
1393 
1394         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1395         if (!devpriv)
1396                 return -ENOMEM;
1397 
1398         pci_dev = pci224_find_pci_dev(dev, it);
1399         if (!pci_dev)
1400                 return -EIO;
1401 
1402         return pci224_attach_common(dev, pci_dev, it->options);
1403 }
1404 
1405 static int
1406 pci224_auto_attach(struct comedi_device *dev, unsigned long context_unused)
1407 {
1408         struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
1409         struct pci224_private *devpriv;
1410 
1411         dev_info(dev->class_dev, DRIVER_NAME ": attach pci %s\n",
1412                  pci_name(pci_dev));
1413 
1414         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1415         if (!devpriv)
1416                 return -ENOMEM;
1417 
1418         dev->board_ptr = pci224_find_pci_board(pci_dev);
1419         if (dev->board_ptr == NULL) {
1420                 dev_err(dev->class_dev,
1421                         DRIVER_NAME ": BUG! cannot determine board type!\n");
1422                 return -EINVAL;
1423         }
1424         /*
1425          * Need to 'get' the PCI device to match the 'put' in pci224_detach().
1426          * TODO: Remove the pci_dev_get() and matching pci_dev_put() once
1427          * support for manual attachment of PCI devices via pci224_attach()
1428          * has been removed.
1429          */
1430         pci_dev_get(pci_dev);
1431         return pci224_attach_common(dev, pci_dev, NULL);
1432 }
1433 
1434 static void pci224_detach(struct comedi_device *dev)
1435 {
1436         struct pci224_private *devpriv = dev->private;
1437         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1438 
1439         if (dev->irq)
1440                 free_irq(dev->irq, dev);
1441         if (dev->subdevices) {
1442                 struct comedi_subdevice *s;
1443 
1444                 s = &dev->subdevices[0];
1445                 /* AO subdevice */
1446                 kfree(s->range_table_list);
1447         }
1448         if (devpriv) {
1449                 kfree(devpriv->ao_readback);
1450                 kfree(devpriv->ao_scan_vals);
1451                 kfree(devpriv->ao_scan_order);
1452         }
1453         comedi_pci_disable(dev);
1454         if (pcidev)
1455                 pci_dev_put(pcidev);
1456 }
1457 
1458 static struct comedi_driver amplc_pci224_driver = {
1459         .driver_name    = "amplc_pci224",
1460         .module         = THIS_MODULE,
1461         .attach         = pci224_attach,
1462         .detach         = pci224_detach,
1463         .auto_attach    = pci224_auto_attach,
1464         .board_name     = &pci224_boards[0].name,
1465         .offset         = sizeof(struct pci224_board),
1466         .num_names      = ARRAY_SIZE(pci224_boards),
1467 };
1468 
1469 static int amplc_pci224_pci_probe(struct pci_dev *dev,
1470                                   const struct pci_device_id *id)
1471 {
1472         return comedi_pci_auto_config(dev, &amplc_pci224_driver,
1473                                       id->driver_data);
1474 }
1475 
1476 static const struct pci_device_id amplc_pci224_pci_table[] = {
1477         { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI224) },
1478         { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI234) },
1479         { 0 }
1480 };
1481 MODULE_DEVICE_TABLE(pci, amplc_pci224_pci_table);
1482 
1483 static struct pci_driver amplc_pci224_pci_driver = {
1484         .name           = "amplc_pci224",
1485         .id_table       = amplc_pci224_pci_table,
1486         .probe          = amplc_pci224_pci_probe,
1487         .remove         = comedi_pci_auto_unconfig,
1488 };
1489 module_comedi_pci_driver(amplc_pci224_driver, amplc_pci224_pci_driver);
1490 
1491 MODULE_AUTHOR("Comedi http://www.comedi.org");
1492 MODULE_DESCRIPTION("Comedi low-level driver");
1493 MODULE_LICENSE("GPL");
1494 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us