Version:  2.0.40 2.2.26 2.4.37 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 3.16

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         unsigned short ao_enab; /* max 16 channels so 'short' will do */
381         unsigned char intsce;
382 };
383 
384 /*
385  * Called from the 'insn_write' function to perform a single write.
386  */
387 static void
388 pci224_ao_set_data(struct comedi_device *dev, int chan, int range,
389                    unsigned int data)
390 {
391         const struct pci224_board *thisboard = comedi_board(dev);
392         struct pci224_private *devpriv = dev->private;
393         unsigned short mangled;
394 
395         /* Store unmangled data for readback. */
396         devpriv->ao_readback[chan] = data;
397         /* Enable the channel. */
398         outw(1 << chan, dev->iobase + PCI224_DACCEN);
399         /* Set range and reset FIFO. */
400         devpriv->daccon = COMBINE(devpriv->daccon, devpriv->hwrange[range],
401                                   (PCI224_DACCON_POLAR_MASK |
402                                    PCI224_DACCON_VREF_MASK));
403         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
404              dev->iobase + PCI224_DACCON);
405         /*
406          * Mangle the data.  The hardware expects:
407          * - bipolar: 16-bit 2's complement
408          * - unipolar: 16-bit unsigned
409          */
410         mangled = (unsigned short)data << (16 - thisboard->ao_bits);
411         if ((devpriv->daccon & PCI224_DACCON_POLAR_MASK) ==
412             PCI224_DACCON_POLAR_BI) {
413                 mangled ^= 0x8000;
414         }
415         /* Write mangled data to the FIFO. */
416         outw(mangled, dev->iobase + PCI224_DACDATA);
417         /* Trigger the conversion. */
418         inw(dev->iobase + PCI224_SOFTTRIG);
419 }
420 
421 /*
422  * 'insn_write' function for AO subdevice.
423  */
424 static int
425 pci224_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
426                      struct comedi_insn *insn, unsigned int *data)
427 {
428         int i;
429         int chan, range;
430 
431         /* Unpack channel and range. */
432         chan = CR_CHAN(insn->chanspec);
433         range = CR_RANGE(insn->chanspec);
434 
435         /* Writing a list of values to an AO channel is probably not
436          * very useful, but that's how the interface is defined. */
437         for (i = 0; i < insn->n; i++)
438                 pci224_ao_set_data(dev, chan, range, data[i]);
439 
440         return i;
441 }
442 
443 /*
444  * 'insn_read' function for AO subdevice.
445  *
446  * N.B. The value read will not be valid if the DAC channel has
447  * never been written successfully since the device was attached
448  * or since the channel has been used by an AO streaming write
449  * command.
450  */
451 static int
452 pci224_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
453                     struct comedi_insn *insn, unsigned int *data)
454 {
455         struct pci224_private *devpriv = dev->private;
456         int i;
457         int chan;
458 
459         chan = CR_CHAN(insn->chanspec);
460 
461         for (i = 0; i < insn->n; i++)
462                 data[i] = devpriv->ao_readback[chan];
463 
464 
465         return i;
466 }
467 
468 /*
469  * Kills a command running on the AO subdevice.
470  */
471 static void pci224_ao_stop(struct comedi_device *dev,
472                            struct comedi_subdevice *s)
473 {
474         struct pci224_private *devpriv = dev->private;
475         unsigned long flags;
476 
477         if (!test_and_clear_bit(AO_CMD_STARTED, &devpriv->state))
478                 return;
479 
480 
481         spin_lock_irqsave(&devpriv->ao_spinlock, flags);
482         /* Kill the interrupts. */
483         devpriv->intsce = 0;
484         outb(0, devpriv->iobase1 + PCI224_INT_SCE);
485         /*
486          * Interrupt routine may or may not be running.  We may or may not
487          * have been called from the interrupt routine (directly or
488          * indirectly via a comedi_events() callback routine).  It's highly
489          * unlikely that we've been called from some other interrupt routine
490          * but who knows what strange things coders get up to!
491          *
492          * If the interrupt routine is currently running, wait for it to
493          * finish, unless we appear to have been called via the interrupt
494          * routine.
495          */
496         while (devpriv->intr_running && devpriv->intr_cpuid != THISCPU) {
497                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
498                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
499         }
500         spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
501         /* Reconfigure DAC for insn_write usage. */
502         outw(0, dev->iobase + PCI224_DACCEN);   /* Disable channels. */
503         devpriv->daccon = COMBINE(devpriv->daccon,
504                                   PCI224_DACCON_TRIG_SW |
505                                   PCI224_DACCON_FIFOINTR_EMPTY,
506                                   PCI224_DACCON_TRIG_MASK |
507                                   PCI224_DACCON_FIFOINTR_MASK);
508         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
509              dev->iobase + PCI224_DACCON);
510 }
511 
512 /*
513  * Handles start of acquisition for the AO subdevice.
514  */
515 static void pci224_ao_start(struct comedi_device *dev,
516                             struct comedi_subdevice *s)
517 {
518         struct pci224_private *devpriv = dev->private;
519         struct comedi_cmd *cmd = &s->async->cmd;
520         unsigned long flags;
521 
522         set_bit(AO_CMD_STARTED, &devpriv->state);
523         if (cmd->stop_src == TRIG_COUNT && devpriv->ao_stop_count == 0) {
524                 /* An empty acquisition! */
525                 s->async->events |= COMEDI_CB_EOA;
526                 cfc_handle_events(dev, s);
527         } else {
528                 /* Enable interrupts. */
529                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
530                 if (cmd->stop_src == TRIG_EXT)
531                         devpriv->intsce = PCI224_INTR_EXT | PCI224_INTR_DAC;
532                 else
533                         devpriv->intsce = PCI224_INTR_DAC;
534 
535                 outb(devpriv->intsce, devpriv->iobase1 + PCI224_INT_SCE);
536                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
537         }
538 }
539 
540 /*
541  * Handles interrupts from the DAC FIFO.
542  */
543 static void pci224_ao_handle_fifo(struct comedi_device *dev,
544                                   struct comedi_subdevice *s)
545 {
546         struct pci224_private *devpriv = dev->private;
547         struct comedi_cmd *cmd = &s->async->cmd;
548         unsigned int bytes_per_scan = cfc_bytes_per_scan(s);
549         unsigned int num_scans;
550         unsigned int room;
551         unsigned short dacstat;
552         unsigned int i, n;
553 
554         /* Determine number of scans available in buffer. */
555         num_scans = comedi_buf_read_n_available(s) / bytes_per_scan;
556         if (cmd->stop_src == TRIG_COUNT) {
557                 /* Fixed number of scans. */
558                 if (num_scans > devpriv->ao_stop_count)
559                         num_scans = devpriv->ao_stop_count;
560 
561         }
562 
563         /* Determine how much room is in the FIFO (in samples). */
564         dacstat = inw(dev->iobase + PCI224_DACCON);
565         switch (dacstat & PCI224_DACCON_FIFOFL_MASK) {
566         case PCI224_DACCON_FIFOFL_EMPTY:
567                 room = PCI224_FIFO_ROOM_EMPTY;
568                 if (cmd->stop_src == TRIG_COUNT && devpriv->ao_stop_count == 0) {
569                         /* FIFO empty at end of counted acquisition. */
570                         s->async->events |= COMEDI_CB_EOA;
571                         cfc_handle_events(dev, s);
572                         return;
573                 }
574                 break;
575         case PCI224_DACCON_FIFOFL_ONETOHALF:
576                 room = PCI224_FIFO_ROOM_ONETOHALF;
577                 break;
578         case PCI224_DACCON_FIFOFL_HALFTOFULL:
579                 room = PCI224_FIFO_ROOM_HALFTOFULL;
580                 break;
581         default:
582                 room = PCI224_FIFO_ROOM_FULL;
583                 break;
584         }
585         if (room >= PCI224_FIFO_ROOM_ONETOHALF) {
586                 /* FIFO is less than half-full. */
587                 if (num_scans == 0) {
588                         /* Nothing left to put in the FIFO. */
589                         dev_err(dev->class_dev, "AO buffer underrun\n");
590                         s->async->events |= COMEDI_CB_OVERFLOW;
591                 }
592         }
593         /* Determine how many new scans can be put in the FIFO. */
594         room /= cmd->chanlist_len;
595 
596         /* Determine how many scans to process. */
597         if (num_scans > room)
598                 num_scans = room;
599 
600         /* Process scans. */
601         for (n = 0; n < num_scans; n++) {
602                 cfc_read_array_from_buffer(s, &devpriv->ao_scan_vals[0],
603                                            bytes_per_scan);
604                 for (i = 0; i < cmd->chanlist_len; i++) {
605                         outw(devpriv->ao_scan_vals[devpriv->ao_scan_order[i]],
606                              dev->iobase + PCI224_DACDATA);
607                 }
608         }
609         if (cmd->stop_src == TRIG_COUNT) {
610                 devpriv->ao_stop_count -= num_scans;
611                 if (devpriv->ao_stop_count == 0) {
612                         /*
613                          * Change FIFO interrupt trigger level to wait
614                          * until FIFO is empty.
615                          */
616                         devpriv->daccon = COMBINE(devpriv->daccon,
617                                                   PCI224_DACCON_FIFOINTR_EMPTY,
618                                                   PCI224_DACCON_FIFOINTR_MASK);
619                         outw(devpriv->daccon, dev->iobase + PCI224_DACCON);
620                 }
621         }
622         if ((devpriv->daccon & PCI224_DACCON_TRIG_MASK) ==
623             PCI224_DACCON_TRIG_NONE) {
624                 unsigned short trig;
625 
626                 /*
627                  * This is the initial DAC FIFO interrupt at the
628                  * start of the acquisition.  The DAC's scan trigger
629                  * has been set to 'none' up until now.
630                  *
631                  * Now that data has been written to the FIFO, the
632                  * DAC's scan trigger source can be set to the
633                  * correct value.
634                  *
635                  * BUG: The first scan will be triggered immediately
636                  * if the scan trigger source is at logic level 1.
637                  */
638                 if (cmd->scan_begin_src == TRIG_TIMER) {
639                         trig = PCI224_DACCON_TRIG_Z2CT0;
640                 } else {
641                         /* cmd->scan_begin_src == TRIG_EXT */
642                         if (cmd->scan_begin_arg & CR_INVERT)
643                                 trig = PCI224_DACCON_TRIG_EXTN;
644                         else
645                                 trig = PCI224_DACCON_TRIG_EXTP;
646 
647                 }
648                 devpriv->daccon = COMBINE(devpriv->daccon, trig,
649                                           PCI224_DACCON_TRIG_MASK);
650                 outw(devpriv->daccon, dev->iobase + PCI224_DACCON);
651         }
652 
653         cfc_handle_events(dev, s);
654 }
655 
656 static int pci224_ao_inttrig_start(struct comedi_device *dev,
657                                    struct comedi_subdevice *s,
658                                    unsigned int trig_num)
659 {
660         struct comedi_cmd *cmd = &s->async->cmd;
661 
662         if (trig_num != cmd->start_arg)
663                 return -EINVAL;
664 
665         s->async->inttrig = NULL;
666         pci224_ao_start(dev, s);
667 
668         return 1;
669 }
670 
671 static int pci224_ao_check_chanlist(struct comedi_device *dev,
672                                     struct comedi_subdevice *s,
673                                     struct comedi_cmd *cmd)
674 {
675         unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
676         unsigned int chan_mask = 0;
677         int i;
678 
679         for (i = 0; i < cmd->chanlist_len; i++) {
680                 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
681                 unsigned int range = CR_RANGE(cmd->chanlist[i]);
682 
683                 if (chan_mask & (1 << chan)) {
684                         dev_dbg(dev->class_dev,
685                                 "%s: entries in chanlist must contain no duplicate channels\n",
686                                 __func__);
687                         return -EINVAL;
688                 }
689                 chan_mask |= (1 << chan);
690 
691                 if (range != range0) {
692                         dev_dbg(dev->class_dev,
693                                 "%s: entries in chanlist must all have the same range index\n",
694                                 __func__);
695                         return -EINVAL;
696                 }
697         }
698 
699         return 0;
700 }
701 
702 #define MAX_SCAN_PERIOD         0xFFFFFFFFU
703 #define MIN_SCAN_PERIOD         2500
704 #define CONVERT_PERIOD          625
705 
706 /*
707  * 'do_cmdtest' function for AO subdevice.
708  */
709 static int
710 pci224_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
711                   struct comedi_cmd *cmd)
712 {
713         struct pci224_private *devpriv = dev->private;
714         int err = 0;
715         unsigned int arg;
716 
717         /* Step 1 : check if triggers are trivially valid */
718 
719         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
720         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
721                                         TRIG_EXT | TRIG_TIMER);
722         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
723         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
724         err |= cfc_check_trigger_src(&cmd->stop_src,
725                                         TRIG_COUNT | TRIG_EXT | TRIG_NONE);
726 
727         if (err)
728                 return 1;
729 
730         /* Step 2a : make sure trigger sources are unique */
731 
732         err |= cfc_check_trigger_is_unique(cmd->start_src);
733         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
734         err |= cfc_check_trigger_is_unique(cmd->stop_src);
735 
736         /* Step 2b : and mutually compatible */
737 
738         /*
739          * There's only one external trigger signal (which makes these
740          * tests easier).  Only one thing can use it.
741          */
742         arg = 0;
743         if (cmd->start_src & TRIG_EXT)
744                 arg++;
745         if (cmd->scan_begin_src & TRIG_EXT)
746                 arg++;
747         if (cmd->stop_src & TRIG_EXT)
748                 arg++;
749         if (arg > 1)
750                 err |= -EINVAL;
751 
752         if (err)
753                 return 2;
754 
755         /* Step 3: check if arguments are trivially valid */
756 
757         switch (cmd->start_src) {
758         case TRIG_INT:
759                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
760                 break;
761         case TRIG_EXT:
762                 /* Force to external trigger 0. */
763                 if ((cmd->start_arg & ~CR_FLAGS_MASK) != 0) {
764                         cmd->start_arg = COMBINE(cmd->start_arg, 0,
765                                                  ~CR_FLAGS_MASK);
766                         err |= -EINVAL;
767                 }
768                 /* The only flag allowed is CR_EDGE, which is ignored. */
769                 if ((cmd->start_arg & CR_FLAGS_MASK & ~CR_EDGE) != 0) {
770                         cmd->start_arg = COMBINE(cmd->start_arg, 0,
771                                                  CR_FLAGS_MASK & ~CR_EDGE);
772                         err |= -EINVAL;
773                 }
774                 break;
775         }
776 
777         switch (cmd->scan_begin_src) {
778         case TRIG_TIMER:
779                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
780                                                  MAX_SCAN_PERIOD);
781 
782                 arg = cmd->chanlist_len * CONVERT_PERIOD;
783                 if (arg < MIN_SCAN_PERIOD)
784                         arg = MIN_SCAN_PERIOD;
785                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg, arg);
786                 break;
787         case TRIG_EXT:
788                 /* Force to external trigger 0. */
789                 if ((cmd->scan_begin_arg & ~CR_FLAGS_MASK) != 0) {
790                         cmd->scan_begin_arg = COMBINE(cmd->scan_begin_arg, 0,
791                                                       ~CR_FLAGS_MASK);
792                         err |= -EINVAL;
793                 }
794                 /* Only allow flags CR_EDGE and CR_INVERT.  Ignore CR_EDGE. */
795                 if ((cmd->scan_begin_arg & CR_FLAGS_MASK &
796                      ~(CR_EDGE | CR_INVERT)) != 0) {
797                         cmd->scan_begin_arg = COMBINE(cmd->scan_begin_arg, 0,
798                                                       CR_FLAGS_MASK &
799                                                       ~(CR_EDGE | CR_INVERT));
800                         err |= -EINVAL;
801                 }
802                 break;
803         }
804 
805         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
806         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
807 
808         switch (cmd->stop_src) {
809         case TRIG_COUNT:
810                 /* Any count allowed. */
811                 break;
812         case TRIG_EXT:
813                 /* Force to external trigger 0. */
814                 if ((cmd->stop_arg & ~CR_FLAGS_MASK) != 0) {
815                         cmd->stop_arg = COMBINE(cmd->stop_arg, 0,
816                                                 ~CR_FLAGS_MASK);
817                         err |= -EINVAL;
818                 }
819                 /* The only flag allowed is CR_EDGE, which is ignored. */
820                 if ((cmd->stop_arg & CR_FLAGS_MASK & ~CR_EDGE) != 0) {
821                         cmd->stop_arg = COMBINE(cmd->stop_arg, 0,
822                                                 CR_FLAGS_MASK & ~CR_EDGE);
823                 }
824                 break;
825         case TRIG_NONE:
826                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
827                 break;
828         }
829 
830         if (err)
831                 return 3;
832 
833         /* Step 4: fix up any arguments. */
834 
835         if (cmd->scan_begin_src == TRIG_TIMER) {
836                 arg = cmd->scan_begin_arg;
837                 /* Use two timers. */
838                 i8253_cascade_ns_to_timer(I8254_OSC_BASE_10MHZ,
839                                           &devpriv->cached_div1,
840                                           &devpriv->cached_div2,
841                                           &arg, cmd->flags);
842                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
843         }
844 
845         if (err)
846                 return 4;
847 
848         /* Step 5: check channel list if it exists */
849         if (cmd->chanlist && cmd->chanlist_len > 0)
850                 err |= pci224_ao_check_chanlist(dev, s, cmd);
851 
852         if (err)
853                 return 5;
854 
855         return 0;
856 }
857 
858 static void pci224_ao_start_pacer(struct comedi_device *dev,
859                                   struct comedi_subdevice *s)
860 {
861         struct pci224_private *devpriv = dev->private;
862         unsigned long timer_base = devpriv->iobase1 + PCI224_Z2_CT0;
863 
864         /*
865          * The output of timer Z2-0 will be used as the scan trigger
866          * source.
867          */
868         /* Make sure Z2-0 is gated on.  */
869         outb(GAT_CONFIG(0, GAT_VCC), devpriv->iobase1 + PCI224_ZGAT_SCE);
870         /* Cascading with Z2-2. */
871         /* Make sure Z2-2 is gated on.  */
872         outb(GAT_CONFIG(2, GAT_VCC), devpriv->iobase1 + PCI224_ZGAT_SCE);
873         /* Z2-2 needs 10 MHz clock. */
874         outb(CLK_CONFIG(2, CLK_10MHZ), devpriv->iobase1 + PCI224_ZCLK_SCE);
875         /* Load Z2-2 mode (2) and counter (div1). */
876         i8254_set_mode(timer_base, 0, 2, I8254_MODE2 | I8254_BINARY);
877         i8254_write(timer_base, 0, 2, devpriv->cached_div1);
878         /* Z2-0 is clocked from Z2-2's output. */
879         outb(CLK_CONFIG(0, CLK_OUTNM1), devpriv->iobase1 + PCI224_ZCLK_SCE);
880         /* Load Z2-0 mode (2) and counter (div2). */
881         i8254_set_mode(timer_base, 0, 0, I8254_MODE2 | I8254_BINARY);
882         i8254_write(timer_base, 0, 0, devpriv->cached_div2);
883 }
884 
885 static int pci224_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
886 {
887         struct pci224_private *devpriv = dev->private;
888         struct comedi_cmd *cmd = &s->async->cmd;
889         int range;
890         unsigned int i, j;
891         unsigned int ch;
892         unsigned int rank;
893         unsigned long flags;
894 
895         /* Cannot handle null/empty chanlist. */
896         if (cmd->chanlist == NULL || cmd->chanlist_len == 0)
897                 return -EINVAL;
898 
899 
900         /* Determine which channels are enabled and their load order.  */
901         devpriv->ao_enab = 0;
902 
903         for (i = 0; i < cmd->chanlist_len; i++) {
904                 ch = CR_CHAN(cmd->chanlist[i]);
905                 devpriv->ao_enab |= 1U << ch;
906                 rank = 0;
907                 for (j = 0; j < cmd->chanlist_len; j++) {
908                         if (CR_CHAN(cmd->chanlist[j]) < ch)
909                                 rank++;
910 
911                 }
912                 devpriv->ao_scan_order[rank] = i;
913         }
914 
915         /* Set enabled channels. */
916         outw(devpriv->ao_enab, dev->iobase + PCI224_DACCEN);
917 
918         /* Determine range and polarity.  All channels the same.  */
919         range = CR_RANGE(cmd->chanlist[0]);
920 
921         /*
922          * Set DAC range and polarity.
923          * Set DAC scan trigger source to 'none'.
924          * Set DAC FIFO interrupt trigger level to 'not half full'.
925          * Reset DAC FIFO.
926          *
927          * N.B. DAC FIFO interrupts are currently disabled.
928          */
929         devpriv->daccon = COMBINE(devpriv->daccon,
930                                   (devpriv->
931                                    hwrange[range] | PCI224_DACCON_TRIG_NONE |
932                                    PCI224_DACCON_FIFOINTR_NHALF),
933                                   (PCI224_DACCON_POLAR_MASK |
934                                    PCI224_DACCON_VREF_MASK |
935                                    PCI224_DACCON_TRIG_MASK |
936                                    PCI224_DACCON_FIFOINTR_MASK));
937         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
938              dev->iobase + PCI224_DACCON);
939 
940         if (cmd->scan_begin_src == TRIG_TIMER)
941                 pci224_ao_start_pacer(dev, s);
942 
943         /*
944          * Sort out end of acquisition.
945          */
946         if (cmd->stop_src == TRIG_COUNT)
947                 devpriv->ao_stop_count = cmd->stop_arg;
948         else    /* TRIG_EXT | TRIG_NONE */
949                 devpriv->ao_stop_count = 0;
950 
951         spin_lock_irqsave(&devpriv->ao_spinlock, flags);
952         if (cmd->start_src == TRIG_INT) {
953                 s->async->inttrig = pci224_ao_inttrig_start;
954         } else {        /* TRIG_EXT */
955                 /* Enable external interrupt trigger to start acquisition. */
956                 devpriv->intsce |= PCI224_INTR_EXT;
957                 outb(devpriv->intsce, devpriv->iobase1 + PCI224_INT_SCE);
958         }
959         spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
960 
961         return 0;
962 }
963 
964 /*
965  * 'cancel' function for AO subdevice.
966  */
967 static int pci224_ao_cancel(struct comedi_device *dev,
968                             struct comedi_subdevice *s)
969 {
970         pci224_ao_stop(dev, s);
971         return 0;
972 }
973 
974 /*
975  * 'munge' data for AO command.
976  */
977 static void
978 pci224_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
979                 void *data, unsigned int num_bytes, unsigned int chan_index)
980 {
981         const struct pci224_board *thisboard = comedi_board(dev);
982         struct pci224_private *devpriv = dev->private;
983         struct comedi_cmd *cmd = &s->async->cmd;
984         unsigned short *array = data;
985         unsigned int length = num_bytes / sizeof(*array);
986         unsigned int offset;
987         unsigned int shift;
988         unsigned int i;
989 
990         /* The hardware expects 16-bit numbers. */
991         shift = 16 - thisboard->ao_bits;
992         /* Channels will be all bipolar or all unipolar. */
993         if ((devpriv->hwrange[CR_RANGE(cmd->chanlist[0])] &
994              PCI224_DACCON_POLAR_MASK) == PCI224_DACCON_POLAR_UNI) {
995                 /* Unipolar */
996                 offset = 0;
997         } else {
998                 /* Bipolar */
999                 offset = 32768;
1000         }
1001         /* Munge the data. */
1002         for (i = 0; i < length; i++)
1003                 array[i] = (array[i] << shift) - offset;
1004 
1005 }
1006 
1007 /*
1008  * Interrupt handler.
1009  */
1010 static irqreturn_t pci224_interrupt(int irq, void *d)
1011 {
1012         struct comedi_device *dev = d;
1013         struct pci224_private *devpriv = dev->private;
1014         struct comedi_subdevice *s = dev->write_subdev;
1015         struct comedi_cmd *cmd;
1016         unsigned char intstat, valid_intstat;
1017         unsigned char curenab;
1018         int retval = 0;
1019         unsigned long flags;
1020 
1021         intstat = inb(devpriv->iobase1 + PCI224_INT_SCE) & 0x3F;
1022         if (intstat) {
1023                 retval = 1;
1024                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1025                 valid_intstat = devpriv->intsce & intstat;
1026                 /* Temporarily disable interrupt sources. */
1027                 curenab = devpriv->intsce & ~intstat;
1028                 outb(curenab, devpriv->iobase1 + PCI224_INT_SCE);
1029                 devpriv->intr_running = 1;
1030                 devpriv->intr_cpuid = THISCPU;
1031                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1032                 if (valid_intstat != 0) {
1033                         cmd = &s->async->cmd;
1034                         if (valid_intstat & PCI224_INTR_EXT) {
1035                                 devpriv->intsce &= ~PCI224_INTR_EXT;
1036                                 if (cmd->start_src == TRIG_EXT)
1037                                         pci224_ao_start(dev, s);
1038                                 else if (cmd->stop_src == TRIG_EXT)
1039                                         pci224_ao_stop(dev, s);
1040 
1041                         }
1042                         if (valid_intstat & PCI224_INTR_DAC)
1043                                 pci224_ao_handle_fifo(dev, s);
1044 
1045                 }
1046                 /* Reenable interrupt sources. */
1047                 spin_lock_irqsave(&devpriv->ao_spinlock, flags);
1048                 if (curenab != devpriv->intsce) {
1049                         outb(devpriv->intsce,
1050                              devpriv->iobase1 + PCI224_INT_SCE);
1051                 }
1052                 devpriv->intr_running = 0;
1053                 spin_unlock_irqrestore(&devpriv->ao_spinlock, flags);
1054         }
1055         return IRQ_RETVAL(retval);
1056 }
1057 
1058 /*
1059  * This function looks for a board matching the supplied PCI device.
1060  */
1061 static const struct pci224_board
1062 *pci224_find_pci_board(struct pci_dev *pci_dev)
1063 {
1064         int i;
1065 
1066         for (i = 0; i < ARRAY_SIZE(pci224_boards); i++)
1067                 if (pci_dev->device == pci224_boards[i].devid)
1068                         return &pci224_boards[i];
1069         return NULL;
1070 }
1071 
1072 /*
1073  * This function looks for a PCI device matching the requested board name,
1074  * bus and slot.
1075  */
1076 static struct pci_dev *pci224_find_pci_dev(struct comedi_device *dev,
1077                                            struct comedi_devconfig *it)
1078 {
1079         const struct pci224_board *thisboard = comedi_board(dev);
1080         struct pci_dev *pci_dev = NULL;
1081         int bus = it->options[0];
1082         int slot = it->options[1];
1083 
1084         for_each_pci_dev(pci_dev) {
1085                 if (bus || slot) {
1086                         if (bus != pci_dev->bus->number ||
1087                             slot != PCI_SLOT(pci_dev->devfn))
1088                                 continue;
1089                 }
1090                 if (pci_dev->vendor != PCI_VENDOR_ID_AMPLICON)
1091                         continue;
1092 
1093                 if (thisboard->model == any_model) {
1094                         /* Match any supported model. */
1095                         const struct pci224_board *board_ptr;
1096 
1097                         board_ptr = pci224_find_pci_board(pci_dev);
1098                         if (board_ptr == NULL)
1099                                 continue;
1100                         /* Change board_ptr to matched board. */
1101                         dev->board_ptr = board_ptr;
1102                 } else {
1103                         /* Match specific model name. */
1104                         if (thisboard->devid != pci_dev->device)
1105                                 continue;
1106                 }
1107                 return pci_dev;
1108         }
1109         dev_err(dev->class_dev,
1110                 "No supported board found! (req. bus %d, slot %d)\n",
1111                 bus, slot);
1112         return NULL;
1113 }
1114 
1115 /*
1116  * Common part of attach and auto_attach.
1117  */
1118 static int pci224_attach_common(struct comedi_device *dev,
1119                                 struct pci_dev *pci_dev, int *options)
1120 {
1121         const struct pci224_board *thisboard = comedi_board(dev);
1122         struct pci224_private *devpriv = dev->private;
1123         struct comedi_subdevice *s;
1124         unsigned int irq;
1125         unsigned n;
1126         int ret;
1127 
1128         comedi_set_hw_dev(dev, &pci_dev->dev);
1129 
1130         ret = comedi_pci_enable(dev);
1131         if (ret)
1132                 return ret;
1133 
1134         spin_lock_init(&devpriv->ao_spinlock);
1135 
1136         devpriv->iobase1 = pci_resource_start(pci_dev, 2);
1137         dev->iobase = pci_resource_start(pci_dev, 3);
1138         irq = pci_dev->irq;
1139 
1140         /* Allocate readback buffer for AO channels. */
1141         devpriv->ao_readback = kmalloc(sizeof(devpriv->ao_readback[0]) *
1142                                        thisboard->ao_chans, GFP_KERNEL);
1143         if (!devpriv->ao_readback)
1144                 return -ENOMEM;
1145 
1146 
1147         /* Allocate buffer to hold values for AO channel scan. */
1148         devpriv->ao_scan_vals = kmalloc(sizeof(devpriv->ao_scan_vals[0]) *
1149                                         thisboard->ao_chans, GFP_KERNEL);
1150         if (!devpriv->ao_scan_vals)
1151                 return -ENOMEM;
1152 
1153 
1154         /* Allocate buffer to hold AO channel scan order. */
1155         devpriv->ao_scan_order = kmalloc(sizeof(devpriv->ao_scan_order[0]) *
1156                                          thisboard->ao_chans, GFP_KERNEL);
1157         if (!devpriv->ao_scan_order)
1158                 return -ENOMEM;
1159 
1160 
1161         /* Disable interrupt sources. */
1162         devpriv->intsce = 0;
1163         outb(0, devpriv->iobase1 + PCI224_INT_SCE);
1164 
1165         /* Initialize the DAC hardware. */
1166         outw(PCI224_DACCON_GLOBALRESET, dev->iobase + PCI224_DACCON);
1167         outw(0, dev->iobase + PCI224_DACCEN);
1168         outw(0, dev->iobase + PCI224_FIFOSIZ);
1169         devpriv->daccon = (PCI224_DACCON_TRIG_SW | PCI224_DACCON_POLAR_BI |
1170                            PCI224_DACCON_FIFOENAB |
1171                            PCI224_DACCON_FIFOINTR_EMPTY);
1172         outw(devpriv->daccon | PCI224_DACCON_FIFORESET,
1173              dev->iobase + PCI224_DACCON);
1174 
1175         ret = comedi_alloc_subdevices(dev, 1);
1176         if (ret)
1177                 return ret;
1178 
1179         s = &dev->subdevices[0];
1180         /* Analog output subdevice. */
1181         s->type = COMEDI_SUBD_AO;
1182         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1183         s->n_chan = thisboard->ao_chans;
1184         s->maxdata = (1 << thisboard->ao_bits) - 1;
1185         s->insn_write = &pci224_ao_insn_write;
1186         s->insn_read = &pci224_ao_insn_read;
1187         s->len_chanlist = s->n_chan;
1188 
1189         dev->write_subdev = s;
1190         s->do_cmd = &pci224_ao_cmd;
1191         s->do_cmdtest = &pci224_ao_cmdtest;
1192         s->cancel = &pci224_ao_cancel;
1193         s->munge = &pci224_ao_munge;
1194 
1195         /* Sort out channel range options. */
1196         if (thisboard->model == pci234_model) {
1197                 /* PCI234 range options. */
1198                 const struct comedi_lrange **range_table_list;
1199 
1200                 s->range_table_list = range_table_list =
1201                     kmalloc(sizeof(struct comedi_lrange *) * s->n_chan,
1202                             GFP_KERNEL);
1203                 if (!s->range_table_list)
1204                         return -ENOMEM;
1205 
1206                 if (options) {
1207                         for (n = 2; n < 3 + s->n_chan; n++) {
1208                                 if (options[n] < 0 || options[n] > 1) {
1209                                         dev_warn(dev->class_dev, DRIVER_NAME
1210                                                  ": warning! bad options[%u]=%d\n",
1211                                                  n, options[n]);
1212                                 }
1213                         }
1214                 }
1215                 for (n = 0; n < s->n_chan; n++) {
1216                         if (n < COMEDI_NDEVCONFOPTS - 3 && options &&
1217                             options[3 + n] == 1) {
1218                                 if (options[2] == 1)
1219                                         range_table_list[n] = &range_pci234_ext;
1220                                 else
1221                                         range_table_list[n] = &range_bipolar5;
1222 
1223                         } else {
1224                                 if (options && options[2] == 1) {
1225                                         range_table_list[n] =
1226                                             &range_pci234_ext2;
1227                                 } else {
1228                                         range_table_list[n] = &range_bipolar10;
1229                                 }
1230                         }
1231                 }
1232                 devpriv->hwrange = hwrange_pci234;
1233         } else {
1234                 /* PCI224 range options. */
1235                 if (options && options[2] == 1) {
1236                         s->range_table = &range_pci224_external;
1237                         devpriv->hwrange = hwrange_pci224_external;
1238                 } else {
1239                         if (options && options[2] != 0) {
1240                                 dev_warn(dev->class_dev, DRIVER_NAME
1241                                          ": warning! bad options[2]=%d\n",
1242                                          options[2]);
1243                         }
1244                         s->range_table = &range_pci224_internal;
1245                         devpriv->hwrange = hwrange_pci224_internal;
1246                 }
1247         }
1248 
1249         dev->board_name = thisboard->name;
1250 
1251         if (irq) {
1252                 ret = request_irq(irq, pci224_interrupt, IRQF_SHARED,
1253                                   DRIVER_NAME, dev);
1254                 if (ret < 0) {
1255                         dev_err(dev->class_dev,
1256                                 "error! unable to allocate irq %u\n", irq);
1257                         return ret;
1258                 } else {
1259                         dev->irq = irq;
1260                 }
1261         }
1262 
1263         return 0;
1264 }
1265 
1266 static int pci224_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1267 {
1268         struct pci224_private *devpriv;
1269         struct pci_dev *pci_dev;
1270 
1271         dev_info(dev->class_dev, DRIVER_NAME ": attach\n");
1272 
1273         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1274         if (!devpriv)
1275                 return -ENOMEM;
1276 
1277         pci_dev = pci224_find_pci_dev(dev, it);
1278         if (!pci_dev)
1279                 return -EIO;
1280 
1281         return pci224_attach_common(dev, pci_dev, it->options);
1282 }
1283 
1284 static int
1285 pci224_auto_attach(struct comedi_device *dev, unsigned long context_unused)
1286 {
1287         struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
1288         struct pci224_private *devpriv;
1289 
1290         dev_info(dev->class_dev, DRIVER_NAME ": attach pci %s\n",
1291                  pci_name(pci_dev));
1292 
1293         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1294         if (!devpriv)
1295                 return -ENOMEM;
1296 
1297         dev->board_ptr = pci224_find_pci_board(pci_dev);
1298         if (dev->board_ptr == NULL) {
1299                 dev_err(dev->class_dev,
1300                         DRIVER_NAME ": BUG! cannot determine board type!\n");
1301                 return -EINVAL;
1302         }
1303         /*
1304          * Need to 'get' the PCI device to match the 'put' in pci224_detach().
1305          * TODO: Remove the pci_dev_get() and matching pci_dev_put() once
1306          * support for manual attachment of PCI devices via pci224_attach()
1307          * has been removed.
1308          */
1309         pci_dev_get(pci_dev);
1310         return pci224_attach_common(dev, pci_dev, NULL);
1311 }
1312 
1313 static void pci224_detach(struct comedi_device *dev)
1314 {
1315         struct pci224_private *devpriv = dev->private;
1316         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1317 
1318         if (dev->irq)
1319                 free_irq(dev->irq, dev);
1320         if (dev->subdevices) {
1321                 struct comedi_subdevice *s;
1322 
1323                 s = &dev->subdevices[0];
1324                 /* AO subdevice */
1325                 kfree(s->range_table_list);
1326         }
1327         if (devpriv) {
1328                 kfree(devpriv->ao_readback);
1329                 kfree(devpriv->ao_scan_vals);
1330                 kfree(devpriv->ao_scan_order);
1331         }
1332         comedi_pci_disable(dev);
1333         if (pcidev)
1334                 pci_dev_put(pcidev);
1335 }
1336 
1337 static struct comedi_driver amplc_pci224_driver = {
1338         .driver_name    = "amplc_pci224",
1339         .module         = THIS_MODULE,
1340         .attach         = pci224_attach,
1341         .detach         = pci224_detach,
1342         .auto_attach    = pci224_auto_attach,
1343         .board_name     = &pci224_boards[0].name,
1344         .offset         = sizeof(struct pci224_board),
1345         .num_names      = ARRAY_SIZE(pci224_boards),
1346 };
1347 
1348 static int amplc_pci224_pci_probe(struct pci_dev *dev,
1349                                   const struct pci_device_id *id)
1350 {
1351         return comedi_pci_auto_config(dev, &amplc_pci224_driver,
1352                                       id->driver_data);
1353 }
1354 
1355 static const struct pci_device_id amplc_pci224_pci_table[] = {
1356         { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI224) },
1357         { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI234) },
1358         { 0 }
1359 };
1360 MODULE_DEVICE_TABLE(pci, amplc_pci224_pci_table);
1361 
1362 static struct pci_driver amplc_pci224_pci_driver = {
1363         .name           = "amplc_pci224",
1364         .id_table       = amplc_pci224_pci_table,
1365         .probe          = amplc_pci224_pci_probe,
1366         .remove         = comedi_pci_auto_unconfig,
1367 };
1368 module_comedi_pci_driver(amplc_pci224_driver, amplc_pci224_pci_driver);
1369 
1370 MODULE_AUTHOR("Comedi http://www.comedi.org");
1371 MODULE_DESCRIPTION("Comedi low-level driver");
1372 MODULE_LICENSE("GPL");
1373 

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