Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / comedi / drivers / amplc_pc236_common.c
1 /*
2  * comedi/drivers/amplc_pc236_common.c
3  * Common support code for "amplc_pc236" and "amplc_pci236".
4  *
5  * Copyright (C) 2002-2014 MEV Ltd. <http://www.mev.co.uk/>
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  * Copyright (C) 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 #include <linux/module.h>
22 #include <linux/interrupt.h>
23
24 #include "../comedidev.h"
25
26 #include "amplc_pc236.h"
27 #include "8255.h"
28
29 static void pc236_intr_update(struct comedi_device *dev, bool enable)
30 {
31         const struct pc236_board *thisboard = dev->board_ptr;
32         struct pc236_private *devpriv = dev->private;
33         unsigned long flags;
34
35         spin_lock_irqsave(&dev->spinlock, flags);
36         devpriv->enable_irq = enable;
37         if (thisboard->intr_update_cb)
38                 thisboard->intr_update_cb(dev, enable);
39         spin_unlock_irqrestore(&dev->spinlock, flags);
40 }
41
42 /*
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
46  * interrupt.
47  * Returns false if the interrupt should be ignored.
48  */
49 static bool pc236_intr_check(struct comedi_device *dev)
50 {
51         const struct pc236_board *thisboard = dev->board_ptr;
52         struct pc236_private *devpriv = dev->private;
53         bool retval = false;
54         unsigned long flags;
55
56         spin_lock_irqsave(&dev->spinlock, flags);
57         if (devpriv->enable_irq) {
58                 if (thisboard->intr_chk_clr_cb)
59                         retval = thisboard->intr_chk_clr_cb(dev);
60                 else
61                         retval = true;
62         }
63         spin_unlock_irqrestore(&dev->spinlock, flags);
64
65         return retval;
66 }
67
68 static int pc236_intr_insn(struct comedi_device *dev,
69                            struct comedi_subdevice *s, struct comedi_insn *insn,
70                            unsigned int *data)
71 {
72         data[1] = 0;
73         return insn->n;
74 }
75
76 static int pc236_intr_cmdtest(struct comedi_device *dev,
77                               struct comedi_subdevice *s,
78                               struct comedi_cmd *cmd)
79 {
80         int err = 0;
81
82         /* Step 1 : check if triggers are trivially valid */
83
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);
89
90         if (err)
91                 return 1;
92
93         /* Step 2a : make sure trigger sources are unique */
94         /* Step 2b : and mutually compatible */
95
96         /* Step 3: check it arguments are trivially valid */
97
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,
102                                            cmd->chanlist_len);
103         err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
104
105         if (err)
106                 return 3;
107
108         /* Step 4: fix up any arguments */
109
110         /* Step 5: check channel list if it exists */
111
112         return 0;
113 }
114
115 static int pc236_intr_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
116 {
117         pc236_intr_update(dev, true);
118
119         return 0;
120 }
121
122 static int pc236_intr_cancel(struct comedi_device *dev,
123                              struct comedi_subdevice *s)
124 {
125         pc236_intr_update(dev, false);
126
127         return 0;
128 }
129
130 static irqreturn_t pc236_interrupt(int irq, void *d)
131 {
132         struct comedi_device *dev = d;
133         struct comedi_subdevice *s = dev->read_subdev;
134         bool handled;
135
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);
140         }
141         return IRQ_RETVAL(handled);
142 }
143
144 int amplc_pc236_common_attach(struct comedi_device *dev, unsigned long iobase,
145                               unsigned int irq, unsigned long req_irq_flags)
146 {
147         struct comedi_subdevice *s;
148         int ret;
149
150         dev->iobase = iobase;
151
152         ret = comedi_alloc_subdevices(dev, 2);
153         if (ret)
154                 return ret;
155
156         s = &dev->subdevices[0];
157         /* digital i/o subdevice (8255) */
158         ret = subdev_8255_init(dev, s, NULL, 0x00);
159         if (ret)
160                 return ret;
161
162         s = &dev->subdevices[1];
163         dev->read_subdev = s;
164         s->type = COMEDI_SUBD_UNUSED;
165         pc236_intr_update(dev, false);
166         if (irq) {
167                 if (request_irq(irq, pc236_interrupt, req_irq_flags,
168                                 dev->board_name, dev) >= 0) {
169                         dev->irq = irq;
170                         s->type = COMEDI_SUBD_DI;
171                         s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
172                         s->n_chan = 1;
173                         s->maxdata = 1;
174                         s->range_table = &range_digital;
175                         s->insn_bits = pc236_intr_insn;
176                         s->len_chanlist = 1;
177                         s->do_cmdtest = pc236_intr_cmdtest;
178                         s->do_cmd = pc236_intr_cmd;
179                         s->cancel = pc236_intr_cancel;
180                 }
181         }
182
183         return 0;
184 }
185 EXPORT_SYMBOL_GPL(amplc_pc236_common_attach);
186
187 static int __init amplc_pc236_common_init(void)
188 {
189         return 0;
190 }
191 module_init(amplc_pc236_common_init);
192
193 static void __exit amplc_pc236_common_exit(void)
194 {
195 }
196 module_exit(amplc_pc236_common_exit);
197
198 MODULE_AUTHOR("Comedi http://www.comedi.org");
199 MODULE_DESCRIPTION("Comedi helper for amplc_pc236 and amplc_pci236");
200 MODULE_LICENSE("GPL");