Merge branch 'for-2.6.31' of git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6] / drivers / staging / meilhaus / me1600_ao.c
1 /**
2  * @file me1600_ao.c
3  *
4  * @brief ME-1600 analog output subdevice instance.
5  * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
13  * This file is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 /* Includes
33  */
34
35 #include <linux/module.h>
36
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/io.h>
40 #include <linux/types.h>
41 #include <linux/sched.h>
42
43 #include <linux/workqueue.h>
44
45 #include "medefines.h"
46 #include "meinternal.h"
47 #include "meerror.h"
48 #include "medebug.h"
49
50 #include "me1600_ao_reg.h"
51 #include "me1600_ao.h"
52
53 /* Defines
54  */
55
56 static void me1600_ao_destructor(struct me_subdevice *subdevice);
57
58 static void me1600_ao_work_control_task(struct work_struct *work);
59
60 static int me1600_ao_io_reset_subdevice(me_subdevice_t *subdevice,
61                                         struct file *filep, int flags);
62 static int me1600_ao_io_single_config(me_subdevice_t *subdevice,
63                                       struct file *filep, int channel,
64                                       int single_config, int ref, int trig_chan,
65                                       int trig_type, int trig_edge, int flags);
66 static int me1600_ao_io_single_read(me_subdevice_t *subdevice,
67                                     struct file *filep, int channel, int *value,
68                                     int time_out, int flags);
69 static int me1600_ao_io_single_write(me_subdevice_t *subdevice,
70                                      struct file *filep, int channel, int value,
71                                      int time_out, int flags);
72 static int me1600_ao_query_number_channels(me_subdevice_t *subdevice,
73                                            int *number);
74 static int me1600_ao_query_subdevice_type(me_subdevice_t *subdevice, int *type,
75                                           int *subtype);
76 static int me1600_ao_query_subdevice_caps(me_subdevice_t *subdevice,
77                                           int *caps);
78 static int me1600_ao_query_range_by_min_max(me_subdevice_t *subdevice,
79                                             int unit, int *min, int *max,
80                                             int *maxdata, int *range);
81 static int me1600_ao_query_number_ranges(me_subdevice_t *subdevice, int unit,
82                                          int *count);
83 static int me1600_ao_query_range_info(me_subdevice_t *subdevice, int range,
84                                       int *unit, int *min, int *max,
85                                       int *maxdata);
86
87 /* Functions
88  */
89
90 me1600_ao_subdevice_t *me1600_ao_constructor(uint32_t reg_base,
91                                              unsigned int ao_idx,
92                                              int curr,
93                                              spinlock_t *config_regs_lock,
94                                              spinlock_t *ao_shadows_lock,
95                                              me1600_ao_shadow_t *ao_regs_shadows,
96                                              struct workqueue_struct *me1600_wq)
97 {
98         me1600_ao_subdevice_t *subdevice;
99         int err;
100
101         PDEBUG("executed. idx=%d\n", ao_idx);
102
103         // Allocate memory for subdevice instance.
104         subdevice = kmalloc(sizeof(me1600_ao_subdevice_t), GFP_KERNEL);
105
106         if (!subdevice) {
107                 PERROR
108                     ("Cannot get memory for analog output subdevice instance.\n");
109                 return NULL;
110         }
111
112         memset(subdevice, 0, sizeof(me1600_ao_subdevice_t));
113
114         // Initialize subdevice base class.
115         err = me_subdevice_init(&subdevice->base);
116
117         if (err) {
118                 PERROR("Cannot initialize subdevice base class instance.\n");
119                 kfree(subdevice);
120                 return NULL;
121         }
122         // Initialize spin locks.
123         spin_lock_init(&subdevice->subdevice_lock);
124         subdevice->config_regs_lock = config_regs_lock;
125         subdevice->ao_shadows_lock = ao_shadows_lock;
126
127         // Save the subdevice index.
128         subdevice->ao_idx = ao_idx;
129
130         // Initialize range lists.
131         subdevice->u_ranges_count = 2;
132
133         subdevice->u_ranges[0].min = 0; //0V
134         subdevice->u_ranges[0].max = 9997558;   //10V
135
136         subdevice->u_ranges[1].min = -10E6;     //-10V
137         subdevice->u_ranges[1].max = 9995117;   //10V
138
139         if (curr) {             // This is version with current outputs.
140                 subdevice->i_ranges_count = 2;
141
142                 subdevice->i_ranges[0].min = 0; //0mA
143                 subdevice->i_ranges[0].max = 19995117;  //20mA
144
145                 subdevice->i_ranges[1].min = 4E3;       //4mA
146                 subdevice->i_ranges[1].max = 19995118;  //20mA
147         } else {                // This is version without current outputs.
148                 subdevice->i_ranges_count = 0;
149
150                 subdevice->i_ranges[0].min = 0; //0mA
151                 subdevice->i_ranges[0].max = 0; //0mA
152
153                 subdevice->i_ranges[1].min = 0; //0mA
154                 subdevice->i_ranges[1].max = 0; //0mA
155         }
156
157         // Initialize registers.
158         subdevice->uni_bi_reg = reg_base + ME1600_UNI_BI_REG;
159         subdevice->i_range_reg = reg_base + ME1600_020_420_REG;
160         subdevice->sim_output_reg = reg_base + ME1600_SIM_OUTPUT_REG;
161         subdevice->current_on_reg = reg_base + ME1600_CURRENT_ON_REG;
162 #ifdef MEDEBUG_DEBUG_REG
163         subdevice->reg_base = reg_base;
164 #endif
165
166         // Initialize shadow structure.
167         subdevice->ao_regs_shadows = ao_regs_shadows;
168
169         // Override base class methods.
170         subdevice->base.me_subdevice_destructor = me1600_ao_destructor;
171         subdevice->base.me_subdevice_io_reset_subdevice =
172             me1600_ao_io_reset_subdevice;
173         subdevice->base.me_subdevice_io_single_config =
174             me1600_ao_io_single_config;
175         subdevice->base.me_subdevice_io_single_read = me1600_ao_io_single_read;
176         subdevice->base.me_subdevice_io_single_write =
177             me1600_ao_io_single_write;
178         subdevice->base.me_subdevice_query_number_channels =
179             me1600_ao_query_number_channels;
180         subdevice->base.me_subdevice_query_subdevice_type =
181             me1600_ao_query_subdevice_type;
182         subdevice->base.me_subdevice_query_subdevice_caps =
183             me1600_ao_query_subdevice_caps;
184         subdevice->base.me_subdevice_query_range_by_min_max =
185             me1600_ao_query_range_by_min_max;
186         subdevice->base.me_subdevice_query_number_ranges =
187             me1600_ao_query_number_ranges;
188         subdevice->base.me_subdevice_query_range_info =
189             me1600_ao_query_range_info;
190
191         // Initialize wait queue.
192         init_waitqueue_head(&subdevice->wait_queue);
193
194         // Prepare work queue.
195         subdevice->me1600_workqueue = me1600_wq;
196
197 /* workqueue API changed in kernel 2.6.20 */
198         INIT_DELAYED_WORK(&subdevice->ao_control_task,
199                           me1600_ao_work_control_task);
200         return subdevice;
201 }
202
203 static void me1600_ao_destructor(struct me_subdevice *subdevice)
204 {
205         me1600_ao_subdevice_t *instance;
206
207         instance = (me1600_ao_subdevice_t *) subdevice;
208
209         PDEBUG("executed. idx=%d\n", instance->ao_idx);
210
211         instance->ao_control_task_flag = 0;
212
213         // Reset subdevice to asure clean exit.
214         me1600_ao_io_reset_subdevice(subdevice, NULL,
215                                      ME_IO_RESET_SUBDEVICE_NO_FLAGS);
216
217         // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
218         if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
219                 set_current_state(TASK_INTERRUPTIBLE);
220                 schedule_timeout(2);
221         }
222 }
223
224 static int me1600_ao_io_reset_subdevice(me_subdevice_t *subdevice,
225                                         struct file *filep, int flags)
226 {
227         me1600_ao_subdevice_t *instance;
228         uint16_t tmp;
229
230         instance = (me1600_ao_subdevice_t *) subdevice;
231
232         PDEBUG("executed. idx=%d\n", instance->ao_idx);
233
234         if (flags) {
235                 PERROR("Invalid flag specified.\n");
236                 return ME_ERRNO_INVALID_FLAGS;
237         }
238
239         ME_SUBDEVICE_ENTER;
240
241         //Cancel control task
242         PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
243         instance->ao_control_task_flag = 0;
244         cancel_delayed_work(&instance->ao_control_task);
245         (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
246
247         // Reset all settings.
248         spin_lock(&instance->subdevice_lock);
249         spin_lock(instance->ao_shadows_lock);
250         (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
251         (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
252         (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Not waiting for triggering.
253         (instance->ao_regs_shadows)->synchronous &= ~(0x1 << instance->ao_idx); //Individual triggering.
254
255         // Set output to default (safe) state.
256         spin_lock(instance->config_regs_lock);
257         tmp = inw(instance->uni_bi_reg);        // unipolar
258         tmp |= (0x1 << instance->ao_idx);
259         outw(tmp, instance->uni_bi_reg);
260         PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
261                    instance->uni_bi_reg - instance->reg_base, tmp);
262
263         tmp = inw(instance->current_on_reg);    // Volts only!
264         tmp &= ~(0x1 << instance->ao_idx);
265         tmp &= 0x00FF;
266         outw(tmp, instance->current_on_reg);
267         PDEBUG_REG("current_on_reg outl(0x%lX+0x%lX)=0x%x\n",
268                    instance->reg_base,
269                    instance->current_on_reg - instance->reg_base, tmp);
270
271         tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
272         tmp &= ~(0x1 << instance->ao_idx);
273         outw(tmp, instance->i_range_reg);
274         PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
275                    instance->i_range_reg - instance->reg_base, tmp);
276
277         outw(0, (instance->ao_regs_shadows)->registry[instance->ao_idx]);
278         PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
279                    (instance->ao_regs_shadows)->registry[instance->ao_idx] -
280                    instance->reg_base, 0);
281
282         // Trigger output.
283         outw(0x0000, instance->sim_output_reg);
284         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
285                    instance->reg_base,
286                    instance->sim_output_reg - instance->reg_base, 0x0000);
287         outw(0xFFFF, instance->sim_output_reg);
288         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
289                    instance->reg_base,
290                    instance->sim_output_reg - instance->reg_base, 0xFFFF);
291         spin_unlock(instance->config_regs_lock);
292         spin_unlock(instance->ao_shadows_lock);
293
294         // Set status to 'none'
295         instance->status = ao_status_none;
296         spin_unlock(&instance->subdevice_lock);
297
298         //Signal reset if user is on wait.
299         wake_up_interruptible_all(&instance->wait_queue);
300
301         ME_SUBDEVICE_EXIT;
302
303         return ME_ERRNO_SUCCESS;
304 }
305
306 static int me1600_ao_io_single_config(me_subdevice_t *subdevice,
307                                       struct file *filep,
308                                       int channel,
309                                       int single_config,
310                                       int ref,
311                                       int trig_chan,
312                                       int trig_type, int trig_edge, int flags)
313 {
314         me1600_ao_subdevice_t *instance;
315         uint16_t tmp;
316
317         instance = (me1600_ao_subdevice_t *) subdevice;
318
319         PDEBUG("executed. idx=%d\n", instance->ao_idx);
320
321         // Checking parameters.
322         if (flags) {
323                 PERROR
324                     ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
325                 return ME_ERRNO_INVALID_FLAGS;
326         }
327
328         if (trig_edge != ME_TRIG_EDGE_NONE) {
329                 PERROR
330                     ("Invalid trigger edge. Software trigger has not edge. Must be ME_TRIG_EDGE_NONE\n");
331                 return ME_ERRNO_INVALID_TRIG_EDGE;
332         }
333
334         if (trig_type != ME_TRIG_TYPE_SW) {
335                 PERROR("Invalid trigger edge. Must be ME_TRIG_TYPE_SW.\n");
336                 return ME_ERRNO_INVALID_TRIG_TYPE;
337         }
338
339         if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
340             && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
341                 PERROR("Invalid trigger channel specified.\n");
342                 return ME_ERRNO_INVALID_TRIG_CHAN;
343         }
344
345         if (ref != ME_REF_AO_GROUND) {
346                 PERROR
347                     ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
348                 return ME_ERRNO_INVALID_REF;
349         }
350
351         if (((single_config + 1) >
352              (instance->u_ranges_count + instance->i_ranges_count))
353             || (single_config < 0)) {
354                 PERROR("Invalid range specified.\n");
355                 return ME_ERRNO_INVALID_SINGLE_CONFIG;
356         }
357
358         if (channel) {
359                 PERROR("Invalid channel specified.\n");
360                 return ME_ERRNO_INVALID_CHANNEL;
361         }
362         // Checking parameters - done. All is fine. Do config.
363
364         ME_SUBDEVICE_ENTER;
365
366         //Cancel control task
367         PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
368         instance->ao_control_task_flag = 0;
369         cancel_delayed_work(&instance->ao_control_task);
370
371         spin_lock(&instance->subdevice_lock);
372         spin_lock(instance->ao_shadows_lock);
373         (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
374         (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
375         (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
376
377         spin_lock(instance->config_regs_lock);
378         switch (single_config) {
379         case 0:         // 0V 10V
380                 tmp = inw(instance->current_on_reg);    // Volts
381                 tmp &= ~(0x1 << instance->ao_idx);
382                 outw(tmp, instance->current_on_reg);
383                 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
384                            instance->reg_base,
385                            instance->current_on_reg - instance->reg_base, tmp);
386
387                 // 0V
388                 outw(0,
389                      (instance->ao_regs_shadows)->registry[instance->ao_idx]);
390                 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
391                            instance->reg_base,
392                            (instance->ao_regs_shadows)->registry[instance->
393                                                                  ao_idx] -
394                            instance->reg_base, 0);
395
396                 tmp = inw(instance->uni_bi_reg);        // unipolar
397                 tmp |= (0x1 << instance->ao_idx);
398                 outw(tmp, instance->uni_bi_reg);
399                 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
400                            instance->reg_base,
401                            instance->uni_bi_reg - instance->reg_base, tmp);
402
403                 tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
404                 tmp &= ~(0x1 << instance->ao_idx);
405                 outw(tmp, instance->i_range_reg);
406                 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
407                            instance->reg_base,
408                            instance->i_range_reg - instance->reg_base, tmp);
409                 break;
410
411         case 1:         // -10V 10V
412                 tmp = inw(instance->current_on_reg);    // Volts
413                 tmp &= ~(0x1 << instance->ao_idx);
414                 outw(tmp, instance->current_on_reg);
415                 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
416                            instance->reg_base,
417                            instance->current_on_reg - instance->reg_base, tmp);
418
419                 // 0V
420                 outw(0x0800,
421                      (instance->ao_regs_shadows)->registry[instance->ao_idx]);
422                 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
423                            instance->reg_base,
424                            (instance->ao_regs_shadows)->registry[instance->
425                                                                  ao_idx] -
426                            instance->reg_base, 0x0800);
427
428                 tmp = inw(instance->uni_bi_reg);        // bipolar
429                 tmp &= ~(0x1 << instance->ao_idx);
430                 outw(tmp, instance->uni_bi_reg);
431                 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
432                            instance->reg_base,
433                            instance->uni_bi_reg - instance->reg_base, tmp);
434
435                 tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
436                 tmp &= ~(0x1 << instance->ao_idx);
437                 outw(tmp, instance->i_range_reg);
438                 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
439                            instance->reg_base,
440                            instance->i_range_reg - instance->reg_base, tmp);
441                 break;
442
443         case 2:         // 0mA 20mA
444                 tmp = inw(instance->current_on_reg);    // mAmpers
445                 tmp |= (0x1 << instance->ao_idx);
446                 outw(tmp, instance->current_on_reg);
447                 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
448                            instance->reg_base,
449                            instance->current_on_reg - instance->reg_base, tmp);
450
451                 tmp = inw(instance->i_range_reg);       // 0..20mA
452                 tmp &= ~(0x1 << instance->ao_idx);
453                 outw(tmp, instance->i_range_reg);
454                 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
455                            instance->reg_base,
456                            instance->i_range_reg - instance->reg_base, tmp);
457
458                 // 0mA
459                 outw(0,
460                      (instance->ao_regs_shadows)->registry[instance->ao_idx]);
461                 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
462                            instance->reg_base,
463                            (instance->ao_regs_shadows)->registry[instance->
464                                                                  ao_idx] -
465                            instance->reg_base, 0);
466
467                 tmp = inw(instance->uni_bi_reg);        // unipolar
468                 tmp |= (0x1 << instance->ao_idx);
469                 outw(tmp, instance->uni_bi_reg);
470                 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
471                            instance->reg_base,
472                            instance->uni_bi_reg - instance->reg_base, tmp);
473                 break;
474
475         case 3:         // 4mA 20mA
476                 tmp = inw(instance->current_on_reg);    // mAmpers
477                 tmp |= (0x1 << instance->ao_idx);
478                 outw(tmp, instance->current_on_reg);
479                 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
480                            instance->reg_base,
481                            instance->current_on_reg - instance->reg_base, tmp);
482
483                 tmp = inw(instance->i_range_reg);       // 4..20mA
484                 tmp |= (0x1 << instance->ao_idx);
485                 outw(tmp, instance->i_range_reg);
486                 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
487                            instance->reg_base,
488                            instance->i_range_reg - instance->reg_base, tmp);
489
490                 // 4mA
491                 outw(0,
492                      (instance->ao_regs_shadows)->registry[instance->ao_idx]);
493                 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
494                            instance->reg_base,
495                            (instance->ao_regs_shadows)->registry[instance->
496                                                                  ao_idx] -
497                            instance->reg_base, 0);
498
499                 tmp = inw(instance->uni_bi_reg);        // unipolar
500                 tmp |= (0x1 << instance->ao_idx);
501                 outw(tmp, instance->uni_bi_reg);
502                 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
503                            instance->reg_base,
504                            instance->uni_bi_reg - instance->reg_base, tmp);
505                 break;
506         }
507
508         // Trigger output.
509         outw(0x0000, instance->sim_output_reg);
510         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
511                    instance->reg_base,
512                    instance->sim_output_reg - instance->reg_base, 0x0000);
513         outw(0xFFFF, instance->sim_output_reg);
514         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
515                    instance->reg_base,
516                    instance->sim_output_reg - instance->reg_base, 0xFFFF);
517
518         if (trig_chan == ME_TRIG_CHAN_DEFAULT) {        // Individual triggering.
519                 (instance->ao_regs_shadows)->synchronous &=
520                     ~(0x1 << instance->ao_idx);
521                 PDEBUG("Individual triggering.\n");
522         } else if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS) {     // Synchronous triggering.
523                 (instance->ao_regs_shadows)->synchronous |=
524                     (0x1 << instance->ao_idx);
525                 PDEBUG("Synchronous triggering.\n");
526         }
527         spin_unlock(instance->config_regs_lock);
528         spin_unlock(instance->ao_shadows_lock);
529
530         instance->status = ao_status_single_configured;
531         spin_unlock(&instance->subdevice_lock);
532
533         ME_SUBDEVICE_EXIT;
534
535         return ME_ERRNO_SUCCESS;
536 }
537
538 static int me1600_ao_io_single_read(me_subdevice_t *subdevice,
539                                     struct file *filep,
540                                     int channel,
541                                     int *value, int time_out, int flags)
542 {
543         me1600_ao_subdevice_t *instance;
544         unsigned long delay = 0;
545         unsigned long j = 0;
546         int err = ME_ERRNO_SUCCESS;
547
548         instance = (me1600_ao_subdevice_t *) subdevice;
549
550         PDEBUG("executed. idx=%d\n", instance->ao_idx);
551
552         if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
553                 PERROR("Invalid flag specified. %d\n", flags);
554                 return ME_ERRNO_INVALID_FLAGS;
555         }
556
557         if (time_out < 0) {
558                 PERROR("Invalid timeout specified.\n");
559                 return ME_ERRNO_INVALID_TIMEOUT;
560         }
561
562         if (channel) {
563                 PERROR("Invalid channel specified.\n");
564                 return ME_ERRNO_INVALID_CHANNEL;
565         }
566
567         if ((!flags) && ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {    //Blocking mode. Wait for software trigger.
568                 if (time_out) {
569                         delay = (time_out * HZ) / 1000;
570                         if (delay == 0)
571                                 delay = 1;
572                 }
573
574                 j = jiffies;
575
576                 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
577                 wait_event_interruptible_timeout(instance->wait_queue,
578                                                  (!((instance->
579                                                      ao_regs_shadows)->
580                                                     trigger & instance->
581                                                     ao_idx)),
582                                                  (delay) ? delay : LONG_MAX);
583
584                 if (instance == ao_status_none) {       // Reset was called.
585                         PDEBUG("Single canceled.\n");
586                         err = ME_ERRNO_CANCELLED;
587                 }
588
589                 if (signal_pending(current)) {
590                         PERROR("Wait on start of state machine interrupted.\n");
591                         err = ME_ERRNO_SIGNAL;
592                 }
593
594                 if ((delay) && ((jiffies - j) >= delay)) {
595                         PDEBUG("Timeout reached.\n");
596                         err = ME_ERRNO_TIMEOUT;
597                 }
598         }
599
600         *value = (instance->ao_regs_shadows)->mirror[instance->ao_idx];
601
602         return err;
603 }
604
605 static int me1600_ao_io_single_write(me_subdevice_t *subdevice,
606                                      struct file *filep,
607                                      int channel,
608                                      int value, int time_out, int flags)
609 {
610         me1600_ao_subdevice_t *instance;
611         int err = ME_ERRNO_SUCCESS;
612         unsigned long delay = 0;
613         int i;
614         unsigned long j = 0;
615
616         instance = (me1600_ao_subdevice_t *) subdevice;
617
618         PDEBUG("executed. idx=%d\n", instance->ao_idx);
619
620         if (flags &
621             ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
622               ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
623                 PERROR("Invalid flag specified.\n");
624                 return ME_ERRNO_INVALID_FLAGS;
625         }
626
627         if (time_out < 0) {
628                 PERROR("Invalid timeout specified.\n");
629                 return ME_ERRNO_INVALID_TIMEOUT;
630         }
631
632         if (value & ~ME1600_AO_MAX_DATA) {
633                 PERROR("Invalid value provided.\n");
634                 return ME_ERRNO_VALUE_OUT_OF_RANGE;
635         }
636
637         if (channel) {
638                 PERROR("Invalid channel specified.\n");
639                 return ME_ERRNO_INVALID_CHANNEL;
640         }
641
642         ME_SUBDEVICE_ENTER;
643
644         //Cancel control task
645         PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
646         instance->ao_control_task_flag = 0;
647         cancel_delayed_work(&instance->ao_control_task);
648         (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
649
650         if (time_out) {
651                 delay = (time_out * HZ) / 1000;
652
653                 if (delay == 0)
654                         delay = 1;
655         }
656         //Write value.
657         spin_lock(instance->ao_shadows_lock);
658         (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
659             (uint16_t) value;
660
661         if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {       // Trigger all outputs from synchronous list.
662                 for (i = 0; i < (instance->ao_regs_shadows)->count; i++) {
663                         if (((instance->ao_regs_shadows)->synchronous & (0x1 << i)) || (i == instance->ao_idx)) {       // Set all from synchronous list to correct state.
664                                 PDEBUG
665                                     ("Synchronous triggering: output %d. idx=%d\n",
666                                      i, instance->ao_idx);
667                                 (instance->ao_regs_shadows)->mirror[i] =
668                                     (instance->ao_regs_shadows)->shadow[i];
669
670                                 outw((instance->ao_regs_shadows)->shadow[i],
671                                      (instance->ao_regs_shadows)->registry[i]);
672                                 PDEBUG_REG
673                                     ("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
674                                      instance->reg_base,
675                                      (instance->ao_regs_shadows)->registry[i] -
676                                      instance->reg_base,
677                                      (instance->ao_regs_shadows)->shadow[i]);
678
679                                 (instance->ao_regs_shadows)->trigger &=
680                                     ~(0x1 << i);
681                         }
682                 }
683
684                 // Trigger output.
685                 outw(0x0000, instance->sim_output_reg);
686                 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
687                            instance->reg_base,
688                            instance->sim_output_reg - instance->reg_base, 0);
689                 outw(0xFFFF, instance->sim_output_reg);
690                 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
691                            instance->reg_base,
692                            instance->sim_output_reg - instance->reg_base,
693                            0xFFFF);
694                 instance->status = ao_status_single_end;
695         } else {                // Individual mode.
696                 if ((instance->ao_regs_shadows)->synchronous & (0x1 << instance->ao_idx)) {     // Put on synchronous start list. Set output as waiting for trigger.
697                         PDEBUG("Add to synchronous list. idx=%d\n",
698                                instance->ao_idx);
699                         (instance->ao_regs_shadows)->trigger |=
700                             (0x1 << instance->ao_idx);
701                         instance->status = ao_status_single_run;
702                         PDEBUG("Synchronous list: 0x%x.\n",
703                                (instance->ao_regs_shadows)->synchronous);
704                 } else {        // Fired this one.
705                         PDEBUG("Triggering. idx=%d\n", instance->ao_idx);
706                         (instance->ao_regs_shadows)->mirror[instance->ao_idx] =
707                             (instance->ao_regs_shadows)->shadow[instance->
708                                                                 ao_idx];
709
710                         outw((instance->ao_regs_shadows)->
711                              shadow[instance->ao_idx],
712                              (instance->ao_regs_shadows)->registry[instance->
713                                                                    ao_idx]);
714                         PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
715                                    instance->reg_base,
716                                    (instance->ao_regs_shadows)->
717                                    registry[instance->ao_idx] -
718                                    instance->reg_base,
719                                    (instance->ao_regs_shadows)->
720                                    shadow[instance->ao_idx]);
721
722                         // Set output as triggered.
723                         (instance->ao_regs_shadows)->trigger &=
724                             ~(0x1 << instance->ao_idx);
725
726                         // Trigger output.
727                         outw(0x0000, instance->sim_output_reg);
728                         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
729                                    instance->reg_base,
730                                    instance->sim_output_reg -
731                                    instance->reg_base, 0);
732                         outw(0xFFFF, instance->sim_output_reg);
733                         PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
734                                    instance->reg_base,
735                                    instance->sim_output_reg -
736                                    instance->reg_base, 0xFFFF);
737                         instance->status = ao_status_single_end;
738                 }
739         }
740         spin_unlock(instance->ao_shadows_lock);
741
742         //Init control task
743         instance->timeout.delay = delay;
744         instance->timeout.start_time = jiffies;
745         instance->ao_control_task_flag = 1;
746         queue_delayed_work(instance->me1600_workqueue,
747                            &instance->ao_control_task, 1);
748
749         if ((!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) &&
750             ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {
751                 /* Blocking mode. Wait for software trigger. */
752                 if (time_out) {
753                         delay = (time_out * HZ) / 1000;
754                         if (delay == 0)
755                                 delay = 1;
756                 }
757
758                 j = jiffies;
759
760                 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
761                 wait_event_interruptible_timeout(instance->wait_queue,
762                                                  (!((instance->
763                                                      ao_regs_shadows)->
764                                                     trigger & instance->
765                                                     ao_idx)),
766                                                  (delay) ? delay : LONG_MAX);
767
768                 if (instance == ao_status_none) {
769                         PDEBUG("Single canceled.\n");
770                         err = ME_ERRNO_CANCELLED;
771                 }
772                 if (signal_pending(current)) {
773                         PERROR("Wait on start of state machine interrupted.\n");
774                         err = ME_ERRNO_SIGNAL;
775                 }
776
777                 if ((delay) && ((jiffies - j) >= delay)) {
778                         PDEBUG("Timeout reached.\n");
779                         err = ME_ERRNO_TIMEOUT;
780                 }
781         }
782
783         ME_SUBDEVICE_EXIT;
784
785         return err;
786 }
787
788 static int me1600_ao_query_number_channels(me_subdevice_t *subdevice,
789                                            int *number)
790 {
791         me1600_ao_subdevice_t *instance;
792         instance = (me1600_ao_subdevice_t *) subdevice;
793
794         PDEBUG("executed. idx=%d\n", instance->ao_idx);
795
796         *number = 1;            //Every subdevice has only 1 channel.
797         return ME_ERRNO_SUCCESS;
798 }
799
800 static int me1600_ao_query_subdevice_type(me_subdevice_t *subdevice, int *type,
801                                           int *subtype)
802 {
803         me1600_ao_subdevice_t *instance;
804         instance = (me1600_ao_subdevice_t *) subdevice;
805
806         PDEBUG("executed. idx=%d\n", instance->ao_idx);
807
808         *type = ME_TYPE_AO;
809         *subtype = ME_SUBTYPE_SINGLE;
810         return ME_ERRNO_SUCCESS;
811 }
812
813 static int me1600_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
814 {
815         PDEBUG("executed.\n");
816         *caps = ME_CAPS_AO_TRIG_SYNCHRONOUS;
817         return ME_ERRNO_SUCCESS;
818 }
819
820 static int me1600_ao_query_range_by_min_max(me_subdevice_t *subdevice,
821                                             int unit,
822                                             int *min,
823                                             int *max, int *maxdata, int *range)
824 {
825         me1600_ao_subdevice_t *instance;
826         int i;
827         int r = -1;
828         int diff = 21E6;
829
830         instance = (me1600_ao_subdevice_t *) subdevice;
831
832         PDEBUG("executed. idx=%d\n", instance->ao_idx);
833
834         if ((*max - *min) < 0) {
835                 PERROR("Invalid minimum and maximum values specified.\n");
836                 return ME_ERRNO_INVALID_MIN_MAX;
837         }
838         // Maximum ranges are slightly less then 10V or 20mA. For convenient we accepted this value as valid one.
839         if (unit == ME_UNIT_VOLT) {
840                 for (i = 0; i < instance->u_ranges_count; i++) {
841                         if ((instance->u_ranges[i].min <= *min)
842                             && ((instance->u_ranges[i].max + 5000) >= *max)) {
843                                 if ((instance->u_ranges[i].max -
844                                      instance->u_ranges[i].min) - (*max -
845                                                                    *min) <
846                                     diff) {
847                                         r = i;
848                                         diff =
849                                             (instance->u_ranges[i].max -
850                                              instance->u_ranges[i].min) -
851                                             (*max - *min);
852                                 }
853                         }
854                 }
855
856                 if (r < 0) {
857                         PERROR("No matching range found.\n");
858                         return ME_ERRNO_NO_RANGE;
859                 } else {
860                         *min = instance->u_ranges[r].min;
861                         *max = instance->u_ranges[r].max;
862                         *range = r;
863                 }
864         } else if (unit == ME_UNIT_AMPERE) {
865                 for (i = 0; i < instance->i_ranges_count; i++) {
866                         if ((instance->i_ranges[i].min <= *min)
867                             && (instance->i_ranges[i].max + 5000 >= *max)) {
868                                 if ((instance->i_ranges[i].max -
869                                      instance->i_ranges[i].min) - (*max -
870                                                                    *min) <
871                                     diff) {
872                                         r = i;
873                                         diff =
874                                             (instance->i_ranges[i].max -
875                                              instance->i_ranges[i].min) -
876                                             (*max - *min);
877                                 }
878                         }
879                 }
880
881                 if (r < 0) {
882                         PERROR("No matching range found.\n");
883                         return ME_ERRNO_NO_RANGE;
884                 } else {
885                         *min = instance->i_ranges[r].min;
886                         *max = instance->i_ranges[r].max;
887                         *range = r + instance->u_ranges_count;
888                 }
889         } else {
890                 PERROR("Invalid physical unit specified.\n");
891                 return ME_ERRNO_INVALID_UNIT;
892         }
893         *maxdata = ME1600_AO_MAX_DATA;
894
895         return ME_ERRNO_SUCCESS;
896 }
897
898 static int me1600_ao_query_number_ranges(me_subdevice_t *subdevice,
899                                          int unit, int *count)
900 {
901         me1600_ao_subdevice_t *instance;
902
903         PDEBUG("executed.\n");
904
905         instance = (me1600_ao_subdevice_t *) subdevice;
906         switch (unit) {
907         case ME_UNIT_VOLT:
908                 *count = instance->u_ranges_count;
909                 break;
910         case ME_UNIT_AMPERE:
911                 *count = instance->i_ranges_count;
912                 break;
913         case ME_UNIT_ANY:
914                 *count = instance->u_ranges_count + instance->i_ranges_count;
915                 break;
916         default:
917                 *count = 0;
918         }
919
920         return ME_ERRNO_SUCCESS;
921 }
922
923 static int me1600_ao_query_range_info(me_subdevice_t *subdevice,
924                                       int range,
925                                       int *unit,
926                                       int *min, int *max, int *maxdata)
927 {
928         me1600_ao_subdevice_t *instance;
929
930         PDEBUG("executed.\n");
931
932         instance = (me1600_ao_subdevice_t *) subdevice;
933
934         if (((range + 1) >
935              (instance->u_ranges_count + instance->i_ranges_count))
936             || (range < 0)) {
937                 PERROR("Invalid range number specified.\n");
938                 return ME_ERRNO_INVALID_RANGE;
939         }
940
941         if (range < instance->u_ranges_count) {
942                 *unit = ME_UNIT_VOLT;
943                 *min = instance->u_ranges[range].min;
944                 *max = instance->u_ranges[range].max;
945         } else if (range < instance->u_ranges_count + instance->i_ranges_count) {
946                 *unit = ME_UNIT_AMPERE;
947                 *min = instance->i_ranges[range - instance->u_ranges_count].min;
948                 *max = instance->i_ranges[range - instance->u_ranges_count].max;
949         }
950         *maxdata = ME1600_AO_MAX_DATA;
951
952         return ME_ERRNO_SUCCESS;
953 }
954
955 static void me1600_ao_work_control_task(struct work_struct *work)
956 {
957         me1600_ao_subdevice_t *instance;
958         int reschedule = 1;
959         int signaling = 0;
960
961         instance =
962             container_of((void *)work, me1600_ao_subdevice_t, ao_control_task);
963
964         PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
965               instance->ao_idx);
966
967         if (!((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {       // Output was triggerd.
968                 // Signal the end.
969                 signaling = 1;
970                 reschedule = 0;
971                 if (instance->status == ao_status_single_run) {
972                         instance->status = ao_status_single_end;
973                 }
974
975         } else if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {        // Timeout
976                 PDEBUG("Timeout reached.\n");
977                 spin_lock(instance->ao_shadows_lock);
978                 // Restore old settings.
979                 PDEBUG("Write old value back to register.\n");
980                 (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
981                     (instance->ao_regs_shadows)->mirror[instance->ao_idx];
982
983                 outw((instance->ao_regs_shadows)->mirror[instance->ao_idx],
984                      (instance->ao_regs_shadows)->registry[instance->ao_idx]);
985                 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
986                            instance->reg_base,
987                            (instance->ao_regs_shadows)->registry[instance->
988                                                                  ao_idx] -
989                            instance->reg_base,
990                            (instance->ao_regs_shadows)->mirror[instance->
991                                                                ao_idx]);
992
993                 //Remove from synchronous strt list.
994                 (instance->ao_regs_shadows)->trigger &=
995                     ~(0x1 << instance->ao_idx);
996                 if (instance->status == ao_status_none) {
997                         instance->status = ao_status_single_end;
998                 }
999                 spin_unlock(instance->ao_shadows_lock);
1000
1001                 // Signal the end.
1002                 signaling = 1;
1003                 reschedule = 0;
1004         }
1005
1006         if (signaling) {        //Signal it.
1007                 wake_up_interruptible_all(&instance->wait_queue);
1008         }
1009
1010         if (instance->ao_control_task_flag && reschedule) {     // Reschedule task
1011                 queue_delayed_work(instance->me1600_workqueue,
1012                                    &instance->ao_control_task, 1);
1013         } else {
1014                 PINFO("<%s> Ending control task.\n", __func__);
1015         }
1016
1017 }