2 * comedi/drivers/amplc_pc236_common.c
3 * Common support code for "amplc_pc236" and "amplc_pci236".
5 * Copyright (C) 2002-2014 MEV Ltd. <http://www.mev.co.uk/>
7 * COMEDI - Linux Control and Measurement Device Interface
8 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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.
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.
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
24 #include "../comedidev.h"
26 #include "amplc_pc236.h"
29 static void pc236_intr_update(struct comedi_device *dev, bool enable)
31 const struct pc236_board *board = dev->board_ptr;
32 struct pc236_private *devpriv = dev->private;
35 spin_lock_irqsave(&dev->spinlock, flags);
36 devpriv->enable_irq = enable;
37 if (board->intr_update_cb)
38 board->intr_update_cb(dev, enable);
39 spin_unlock_irqrestore(&dev->spinlock, flags);
43 * This function is called when an interrupt occurs to check whether
44 * the interrupt has been marked as enabled and was generated by the
45 * board. If so, the function prepares the hardware for the next
47 * Returns false if the interrupt should be ignored.
49 static bool pc236_intr_check(struct comedi_device *dev)
51 const struct pc236_board *board = dev->board_ptr;
52 struct pc236_private *devpriv = dev->private;
56 spin_lock_irqsave(&dev->spinlock, flags);
57 if (devpriv->enable_irq) {
58 if (board->intr_chk_clr_cb)
59 retval = board->intr_chk_clr_cb(dev);
63 spin_unlock_irqrestore(&dev->spinlock, flags);
68 static int pc236_intr_insn(struct comedi_device *dev,
69 struct comedi_subdevice *s, struct comedi_insn *insn,
76 static int pc236_intr_cmdtest(struct comedi_device *dev,
77 struct comedi_subdevice *s,
78 struct comedi_cmd *cmd)
82 /* Step 1 : check if triggers are trivially valid */
84 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
85 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
86 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
87 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
88 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
93 /* Step 2a : make sure trigger sources are unique */
94 /* Step 2b : and mutually compatible */
96 /* Step 3: check it arguments are trivially valid */
98 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
99 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
100 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
101 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
103 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
108 /* Step 4: fix up any arguments */
110 /* Step 5: check channel list if it exists */
115 static int pc236_intr_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
117 pc236_intr_update(dev, true);
122 static int pc236_intr_cancel(struct comedi_device *dev,
123 struct comedi_subdevice *s)
125 pc236_intr_update(dev, false);
130 static irqreturn_t pc236_interrupt(int irq, void *d)
132 struct comedi_device *dev = d;
133 struct comedi_subdevice *s = dev->read_subdev;
136 handled = pc236_intr_check(dev);
137 if (dev->attached && handled) {
138 comedi_buf_write_samples(s, &s->state, 1);
139 comedi_handle_events(dev, s);
141 return IRQ_RETVAL(handled);
144 int amplc_pc236_common_attach(struct comedi_device *dev, unsigned long iobase,
145 unsigned int irq, unsigned long req_irq_flags)
147 struct comedi_subdevice *s;
150 dev->iobase = iobase;
152 ret = comedi_alloc_subdevices(dev, 2);
156 s = &dev->subdevices[0];
157 /* digital i/o subdevice (8255) */
158 ret = subdev_8255_init(dev, s, NULL, 0x00);
162 s = &dev->subdevices[1];
163 dev->read_subdev = s;
164 s->type = COMEDI_SUBD_UNUSED;
165 pc236_intr_update(dev, false);
167 if (request_irq(irq, pc236_interrupt, req_irq_flags,
168 dev->board_name, dev) >= 0) {
170 s->type = COMEDI_SUBD_DI;
171 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
174 s->range_table = &range_digital;
175 s->insn_bits = pc236_intr_insn;
177 s->do_cmdtest = pc236_intr_cmdtest;
178 s->do_cmd = pc236_intr_cmd;
179 s->cancel = pc236_intr_cancel;
185 EXPORT_SYMBOL_GPL(amplc_pc236_common_attach);
187 static int __init amplc_pc236_common_init(void)
191 module_init(amplc_pc236_common_init);
193 static void __exit amplc_pc236_common_exit(void)
196 module_exit(amplc_pc236_common_exit);
198 MODULE_AUTHOR("Comedi http://www.comedi.org");
199 MODULE_DESCRIPTION("Comedi helper for amplc_pc236 and amplc_pci236");
200 MODULE_LICENSE("GPL");