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)
11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
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.
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.
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.
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
40 #include <linux/types.h>
41 #include <linux/sched.h>
43 #include <linux/workqueue.h>
45 #include "medefines.h"
46 #include "meinternal.h"
50 #include "me1600_ao_reg.h"
51 #include "me1600_ao.h"
56 static void me1600_ao_destructor(struct me_subdevice *subdevice);
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
59 static void me1600_ao_work_control_task(void *subdevice);
61 static void me1600_ao_work_control_task(struct work_struct *work);
64 static int me1600_ao_io_reset_subdevice(me_subdevice_t * subdevice,
65 struct file *filep, int flags);
66 static int me1600_ao_io_single_config(me_subdevice_t * subdevice,
67 struct file *filep, int channel,
68 int single_config, int ref, int trig_chan,
69 int trig_type, int trig_edge, int flags);
70 static int me1600_ao_io_single_read(me_subdevice_t * subdevice,
71 struct file *filep, int channel, int *value,
72 int time_out, int flags);
73 static int me1600_ao_io_single_write(me_subdevice_t * subdevice,
74 struct file *filep, int channel, int value,
75 int time_out, int flags);
76 static int me1600_ao_query_number_channels(me_subdevice_t * subdevice,
78 static int me1600_ao_query_subdevice_type(me_subdevice_t * subdevice, int *type,
80 static int me1600_ao_query_subdevice_caps(me_subdevice_t * subdevice,
82 static int me1600_ao_query_range_by_min_max(me_subdevice_t * subdevice,
83 int unit, int *min, int *max,
84 int *maxdata, int *range);
85 static int me1600_ao_query_number_ranges(me_subdevice_t * subdevice, int unit,
87 static int me1600_ao_query_range_info(me_subdevice_t * subdevice, int range,
88 int *unit, int *min, int *max,
94 me1600_ao_subdevice_t *me1600_ao_constructor(uint32_t reg_base,
97 spinlock_t * config_regs_lock,
98 spinlock_t * ao_shadows_lock,
101 struct workqueue_struct *me1600_wq)
103 me1600_ao_subdevice_t *subdevice;
106 PDEBUG("executed. idx=%d\n", ao_idx);
108 // Allocate memory for subdevice instance.
109 subdevice = kmalloc(sizeof(me1600_ao_subdevice_t), GFP_KERNEL);
113 ("Cannot get memory for analog output subdevice instance.\n");
117 memset(subdevice, 0, sizeof(me1600_ao_subdevice_t));
119 // Initialize subdevice base class.
120 err = me_subdevice_init(&subdevice->base);
123 PERROR("Cannot initialize subdevice base class instance.\n");
127 // Initialize spin locks.
128 spin_lock_init(&subdevice->subdevice_lock);
129 subdevice->config_regs_lock = config_regs_lock;
130 subdevice->ao_shadows_lock = ao_shadows_lock;
132 // Save the subdevice index.
133 subdevice->ao_idx = ao_idx;
135 // Initialize range lists.
136 subdevice->u_ranges_count = 2;
138 subdevice->u_ranges[0].min = 0; //0V
139 subdevice->u_ranges[0].max = 9997558; //10V
141 subdevice->u_ranges[1].min = -10E6; //-10V
142 subdevice->u_ranges[1].max = 9995117; //10V
144 if (curr) { // This is version with current outputs.
145 subdevice->i_ranges_count = 2;
147 subdevice->i_ranges[0].min = 0; //0mA
148 subdevice->i_ranges[0].max = 19995117; //20mA
150 subdevice->i_ranges[1].min = 4E3; //4mA
151 subdevice->i_ranges[1].max = 19995118; //20mA
152 } else { // This is version without current outputs.
153 subdevice->i_ranges_count = 0;
155 subdevice->i_ranges[0].min = 0; //0mA
156 subdevice->i_ranges[0].max = 0; //0mA
158 subdevice->i_ranges[1].min = 0; //0mA
159 subdevice->i_ranges[1].max = 0; //0mA
162 // Initialize registers.
163 subdevice->uni_bi_reg = reg_base + ME1600_UNI_BI_REG;
164 subdevice->i_range_reg = reg_base + ME1600_020_420_REG;
165 subdevice->sim_output_reg = reg_base + ME1600_SIM_OUTPUT_REG;
166 subdevice->current_on_reg = reg_base + ME1600_CURRENT_ON_REG;
167 #ifdef MEDEBUG_DEBUG_REG
168 subdevice->reg_base = reg_base;
171 // Initialize shadow structure.
172 subdevice->ao_regs_shadows = ao_regs_shadows;
174 // Override base class methods.
175 subdevice->base.me_subdevice_destructor = me1600_ao_destructor;
176 subdevice->base.me_subdevice_io_reset_subdevice =
177 me1600_ao_io_reset_subdevice;
178 subdevice->base.me_subdevice_io_single_config =
179 me1600_ao_io_single_config;
180 subdevice->base.me_subdevice_io_single_read = me1600_ao_io_single_read;
181 subdevice->base.me_subdevice_io_single_write =
182 me1600_ao_io_single_write;
183 subdevice->base.me_subdevice_query_number_channels =
184 me1600_ao_query_number_channels;
185 subdevice->base.me_subdevice_query_subdevice_type =
186 me1600_ao_query_subdevice_type;
187 subdevice->base.me_subdevice_query_subdevice_caps =
188 me1600_ao_query_subdevice_caps;
189 subdevice->base.me_subdevice_query_range_by_min_max =
190 me1600_ao_query_range_by_min_max;
191 subdevice->base.me_subdevice_query_number_ranges =
192 me1600_ao_query_number_ranges;
193 subdevice->base.me_subdevice_query_range_info =
194 me1600_ao_query_range_info;
196 // Initialize wait queue.
197 init_waitqueue_head(&subdevice->wait_queue);
199 // Prepare work queue.
200 subdevice->me1600_workqueue = me1600_wq;
202 /* workqueue API changed in kernel 2.6.20 */
203 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
204 INIT_WORK(&subdevice->ao_control_task, me1600_ao_work_control_task,
207 INIT_DELAYED_WORK(&subdevice->ao_control_task,
208 me1600_ao_work_control_task);
213 static void me1600_ao_destructor(struct me_subdevice *subdevice)
215 me1600_ao_subdevice_t *instance;
217 instance = (me1600_ao_subdevice_t *) subdevice;
219 PDEBUG("executed. idx=%d\n", instance->ao_idx);
221 instance->ao_control_task_flag = 0;
223 // Reset subdevice to asure clean exit.
224 me1600_ao_io_reset_subdevice(subdevice, NULL,
225 ME_IO_RESET_SUBDEVICE_NO_FLAGS);
227 // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
228 if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
229 set_current_state(TASK_INTERRUPTIBLE);
234 static int me1600_ao_io_reset_subdevice(me_subdevice_t * subdevice,
235 struct file *filep, int flags)
237 me1600_ao_subdevice_t *instance;
240 instance = (me1600_ao_subdevice_t *) subdevice;
242 PDEBUG("executed. idx=%d\n", instance->ao_idx);
245 PERROR("Invalid flag specified.\n");
246 return ME_ERRNO_INVALID_FLAGS;
251 //Cancel control task
252 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
253 instance->ao_control_task_flag = 0;
254 cancel_delayed_work(&instance->ao_control_task);
255 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
257 // Reset all settings.
258 spin_lock(&instance->subdevice_lock);
259 spin_lock(instance->ao_shadows_lock);
260 (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
261 (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
262 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Not waiting for triggering.
263 (instance->ao_regs_shadows)->synchronous &= ~(0x1 << instance->ao_idx); //Individual triggering.
265 // Set output to default (safe) state.
266 spin_lock(instance->config_regs_lock);
267 tmp = inw(instance->uni_bi_reg); // unipolar
268 tmp |= (0x1 << instance->ao_idx);
269 outw(tmp, instance->uni_bi_reg);
270 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
271 instance->uni_bi_reg - instance->reg_base, tmp);
273 tmp = inw(instance->current_on_reg); // Volts only!
274 tmp &= ~(0x1 << instance->ao_idx);
276 outw(tmp, instance->current_on_reg);
277 PDEBUG_REG("current_on_reg outl(0x%lX+0x%lX)=0x%x\n",
279 instance->current_on_reg - instance->reg_base, tmp);
281 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
282 tmp &= ~(0x1 << instance->ao_idx);
283 outw(tmp, instance->i_range_reg);
284 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
285 instance->i_range_reg - instance->reg_base, tmp);
287 outw(0, (instance->ao_regs_shadows)->registry[instance->ao_idx]);
288 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
289 (instance->ao_regs_shadows)->registry[instance->ao_idx] -
290 instance->reg_base, 0);
293 outw(0x0000, instance->sim_output_reg);
294 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
296 instance->sim_output_reg - instance->reg_base, 0x0000);
297 outw(0xFFFF, instance->sim_output_reg);
298 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
300 instance->sim_output_reg - instance->reg_base, 0xFFFF);
301 spin_unlock(instance->config_regs_lock);
302 spin_unlock(instance->ao_shadows_lock);
304 // Set status to 'none'
305 instance->status = ao_status_none;
306 spin_unlock(&instance->subdevice_lock);
308 //Signal reset if user is on wait.
309 wake_up_interruptible_all(&instance->wait_queue);
313 return ME_ERRNO_SUCCESS;
316 static int me1600_ao_io_single_config(me_subdevice_t * subdevice,
322 int trig_type, int trig_edge, int flags)
324 me1600_ao_subdevice_t *instance;
327 instance = (me1600_ao_subdevice_t *) subdevice;
329 PDEBUG("executed. idx=%d\n", instance->ao_idx);
331 // Checking parameters.
334 ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
335 return ME_ERRNO_INVALID_FLAGS;
338 if (trig_edge != ME_TRIG_EDGE_NONE) {
340 ("Invalid trigger edge. Software trigger has not edge. Must be ME_TRIG_EDGE_NONE\n");
341 return ME_ERRNO_INVALID_TRIG_EDGE;
344 if (trig_type != ME_TRIG_TYPE_SW) {
345 PERROR("Invalid trigger edge. Must be ME_TRIG_TYPE_SW.\n");
346 return ME_ERRNO_INVALID_TRIG_TYPE;
349 if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
350 && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
351 PERROR("Invalid trigger channel specified.\n");
352 return ME_ERRNO_INVALID_TRIG_CHAN;
355 if (ref != ME_REF_AO_GROUND) {
357 ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
358 return ME_ERRNO_INVALID_REF;
361 if (((single_config + 1) >
362 (instance->u_ranges_count + instance->i_ranges_count))
363 || (single_config < 0)) {
364 PERROR("Invalid range specified.\n");
365 return ME_ERRNO_INVALID_SINGLE_CONFIG;
369 PERROR("Invalid channel specified.\n");
370 return ME_ERRNO_INVALID_CHANNEL;
372 // Checking parameters - done. All is fine. Do config.
376 //Cancel control task
377 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
378 instance->ao_control_task_flag = 0;
379 cancel_delayed_work(&instance->ao_control_task);
381 spin_lock(&instance->subdevice_lock);
382 spin_lock(instance->ao_shadows_lock);
383 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
384 (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
385 (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
387 spin_lock(instance->config_regs_lock);
388 switch (single_config) {
390 tmp = inw(instance->current_on_reg); // Volts
391 tmp &= ~(0x1 << instance->ao_idx);
392 outw(tmp, instance->current_on_reg);
393 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
395 instance->current_on_reg - instance->reg_base, tmp);
399 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
400 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
402 (instance->ao_regs_shadows)->registry[instance->
404 instance->reg_base, 0);
406 tmp = inw(instance->uni_bi_reg); // unipolar
407 tmp |= (0x1 << instance->ao_idx);
408 outw(tmp, instance->uni_bi_reg);
409 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
411 instance->uni_bi_reg - instance->reg_base, tmp);
413 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
414 tmp &= ~(0x1 << instance->ao_idx);
415 outw(tmp, instance->i_range_reg);
416 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
418 instance->i_range_reg - instance->reg_base, tmp);
422 tmp = inw(instance->current_on_reg); // Volts
423 tmp &= ~(0x1 << instance->ao_idx);
424 outw(tmp, instance->current_on_reg);
425 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
427 instance->current_on_reg - instance->reg_base, tmp);
431 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
432 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
434 (instance->ao_regs_shadows)->registry[instance->
436 instance->reg_base, 0x0800);
438 tmp = inw(instance->uni_bi_reg); // bipolar
439 tmp &= ~(0x1 << instance->ao_idx);
440 outw(tmp, instance->uni_bi_reg);
441 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
443 instance->uni_bi_reg - instance->reg_base, tmp);
445 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
446 tmp &= ~(0x1 << instance->ao_idx);
447 outw(tmp, instance->i_range_reg);
448 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
450 instance->i_range_reg - instance->reg_base, tmp);
454 tmp = inw(instance->current_on_reg); // mAmpers
455 tmp |= (0x1 << instance->ao_idx);
456 outw(tmp, instance->current_on_reg);
457 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
459 instance->current_on_reg - instance->reg_base, tmp);
461 tmp = inw(instance->i_range_reg); // 0..20mA
462 tmp &= ~(0x1 << instance->ao_idx);
463 outw(tmp, instance->i_range_reg);
464 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
466 instance->i_range_reg - instance->reg_base, tmp);
470 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
471 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
473 (instance->ao_regs_shadows)->registry[instance->
475 instance->reg_base, 0);
477 tmp = inw(instance->uni_bi_reg); // unipolar
478 tmp |= (0x1 << instance->ao_idx);
479 outw(tmp, instance->uni_bi_reg);
480 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
482 instance->uni_bi_reg - instance->reg_base, tmp);
486 tmp = inw(instance->current_on_reg); // mAmpers
487 tmp |= (0x1 << instance->ao_idx);
488 outw(tmp, instance->current_on_reg);
489 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
491 instance->current_on_reg - instance->reg_base, tmp);
493 tmp = inw(instance->i_range_reg); // 4..20mA
494 tmp |= (0x1 << instance->ao_idx);
495 outw(tmp, instance->i_range_reg);
496 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
498 instance->i_range_reg - instance->reg_base, tmp);
502 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
503 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
505 (instance->ao_regs_shadows)->registry[instance->
507 instance->reg_base, 0);
509 tmp = inw(instance->uni_bi_reg); // unipolar
510 tmp |= (0x1 << instance->ao_idx);
511 outw(tmp, instance->uni_bi_reg);
512 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
514 instance->uni_bi_reg - instance->reg_base, tmp);
519 outw(0x0000, instance->sim_output_reg);
520 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
522 instance->sim_output_reg - instance->reg_base, 0x0000);
523 outw(0xFFFF, instance->sim_output_reg);
524 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
526 instance->sim_output_reg - instance->reg_base, 0xFFFF);
528 if (trig_chan == ME_TRIG_CHAN_DEFAULT) { // Individual triggering.
529 (instance->ao_regs_shadows)->synchronous &=
530 ~(0x1 << instance->ao_idx);
531 PDEBUG("Individual triggering.\n");
532 } else if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS) { // Synchronous triggering.
533 (instance->ao_regs_shadows)->synchronous |=
534 (0x1 << instance->ao_idx);
535 PDEBUG("Synchronous triggering.\n");
537 spin_unlock(instance->config_regs_lock);
538 spin_unlock(instance->ao_shadows_lock);
540 instance->status = ao_status_single_configured;
541 spin_unlock(&instance->subdevice_lock);
545 return ME_ERRNO_SUCCESS;
548 static int me1600_ao_io_single_read(me_subdevice_t * subdevice,
551 int *value, int time_out, int flags)
553 me1600_ao_subdevice_t *instance;
554 unsigned long delay = 0;
556 int err = ME_ERRNO_SUCCESS;
558 instance = (me1600_ao_subdevice_t *) subdevice;
560 PDEBUG("executed. idx=%d\n", instance->ao_idx);
562 if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
563 PERROR("Invalid flag specified. %d\n", flags);
564 return ME_ERRNO_INVALID_FLAGS;
568 PERROR("Invalid timeout specified.\n");
569 return ME_ERRNO_INVALID_TIMEOUT;
573 PERROR("Invalid channel specified.\n");
574 return ME_ERRNO_INVALID_CHANNEL;
577 if ((!flags) && ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) { //Blocking mode. Wait for software trigger.
579 delay = (time_out * HZ) / 1000;
586 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
587 wait_event_interruptible_timeout(instance->wait_queue,
592 (delay) ? delay : LONG_MAX);
594 if (instance == ao_status_none) { // Reset was called.
595 PDEBUG("Single canceled.\n");
596 err = ME_ERRNO_CANCELLED;
599 if (signal_pending(current)) {
600 PERROR("Wait on start of state machine interrupted.\n");
601 err = ME_ERRNO_SIGNAL;
604 if ((delay) && ((jiffies - j) >= delay)) {
605 PDEBUG("Timeout reached.\n");
606 err = ME_ERRNO_TIMEOUT;
610 *value = (instance->ao_regs_shadows)->mirror[instance->ao_idx];
615 static int me1600_ao_io_single_write(me_subdevice_t * subdevice,
618 int value, int time_out, int flags)
620 me1600_ao_subdevice_t *instance;
621 int err = ME_ERRNO_SUCCESS;
622 unsigned long delay = 0;
626 instance = (me1600_ao_subdevice_t *) subdevice;
628 PDEBUG("executed. idx=%d\n", instance->ao_idx);
631 ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
632 ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
633 PERROR("Invalid flag specified.\n");
634 return ME_ERRNO_INVALID_FLAGS;
638 PERROR("Invalid timeout specified.\n");
639 return ME_ERRNO_INVALID_TIMEOUT;
642 if (value & ~ME1600_AO_MAX_DATA) {
643 PERROR("Invalid value provided.\n");
644 return ME_ERRNO_VALUE_OUT_OF_RANGE;
648 PERROR("Invalid channel specified.\n");
649 return ME_ERRNO_INVALID_CHANNEL;
654 //Cancel control task
655 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
656 instance->ao_control_task_flag = 0;
657 cancel_delayed_work(&instance->ao_control_task);
658 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
661 delay = (time_out * HZ) / 1000;
667 spin_lock(instance->ao_shadows_lock);
668 (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
671 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { // Trigger all outputs from synchronous list.
672 for (i = 0; i < (instance->ao_regs_shadows)->count; i++) {
673 if (((instance->ao_regs_shadows)->synchronous & (0x1 << i)) || (i == instance->ao_idx)) { // Set all from synchronous list to correct state.
675 ("Synchronous triggering: output %d. idx=%d\n",
676 i, instance->ao_idx);
677 (instance->ao_regs_shadows)->mirror[i] =
678 (instance->ao_regs_shadows)->shadow[i];
680 outw((instance->ao_regs_shadows)->shadow[i],
681 (instance->ao_regs_shadows)->registry[i]);
683 ("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
685 (instance->ao_regs_shadows)->registry[i] -
687 (instance->ao_regs_shadows)->shadow[i]);
689 (instance->ao_regs_shadows)->trigger &=
695 outw(0x0000, instance->sim_output_reg);
696 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
698 instance->sim_output_reg - instance->reg_base, 0);
699 outw(0xFFFF, instance->sim_output_reg);
700 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
702 instance->sim_output_reg - instance->reg_base,
704 instance->status = ao_status_single_end;
705 } else { // Individual mode.
706 if ((instance->ao_regs_shadows)->synchronous & (0x1 << instance->ao_idx)) { // Put on synchronous start list. Set output as waiting for trigger.
707 PDEBUG("Add to synchronous list. idx=%d\n",
709 (instance->ao_regs_shadows)->trigger |=
710 (0x1 << instance->ao_idx);
711 instance->status = ao_status_single_run;
712 PDEBUG("Synchronous list: 0x%x.\n",
713 (instance->ao_regs_shadows)->synchronous);
714 } else { // Fired this one.
715 PDEBUG("Triggering. idx=%d\n", instance->ao_idx);
716 (instance->ao_regs_shadows)->mirror[instance->ao_idx] =
717 (instance->ao_regs_shadows)->shadow[instance->
720 outw((instance->ao_regs_shadows)->
721 shadow[instance->ao_idx],
722 (instance->ao_regs_shadows)->registry[instance->
724 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
726 (instance->ao_regs_shadows)->
727 registry[instance->ao_idx] -
729 (instance->ao_regs_shadows)->
730 shadow[instance->ao_idx]);
732 // Set output as triggered.
733 (instance->ao_regs_shadows)->trigger &=
734 ~(0x1 << instance->ao_idx);
737 outw(0x0000, instance->sim_output_reg);
738 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
740 instance->sim_output_reg -
741 instance->reg_base, 0);
742 outw(0xFFFF, instance->sim_output_reg);
743 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
745 instance->sim_output_reg -
746 instance->reg_base, 0xFFFF);
747 instance->status = ao_status_single_end;
750 spin_unlock(instance->ao_shadows_lock);
753 instance->timeout.delay = delay;
754 instance->timeout.start_time = jiffies;
755 instance->ao_control_task_flag = 1;
756 queue_delayed_work(instance->me1600_workqueue,
757 &instance->ao_control_task, 1);
759 if ((!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) &&
760 ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {
761 /* Blocking mode. Wait for software trigger. */
763 delay = (time_out * HZ) / 1000;
770 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
771 wait_event_interruptible_timeout(instance->wait_queue,
776 (delay) ? delay : LONG_MAX);
778 if (instance == ao_status_none) {
779 PDEBUG("Single canceled.\n");
780 err = ME_ERRNO_CANCELLED;
782 if (signal_pending(current)) {
783 PERROR("Wait on start of state machine interrupted.\n");
784 err = ME_ERRNO_SIGNAL;
787 if ((delay) && ((jiffies - j) >= delay)) {
788 PDEBUG("Timeout reached.\n");
789 err = ME_ERRNO_TIMEOUT;
798 static int me1600_ao_query_number_channels(me_subdevice_t * subdevice,
801 me1600_ao_subdevice_t *instance;
802 instance = (me1600_ao_subdevice_t *) subdevice;
804 PDEBUG("executed. idx=%d\n", instance->ao_idx);
806 *number = 1; //Every subdevice has only 1 channel.
807 return ME_ERRNO_SUCCESS;
810 static int me1600_ao_query_subdevice_type(me_subdevice_t * subdevice, int *type,
813 me1600_ao_subdevice_t *instance;
814 instance = (me1600_ao_subdevice_t *) subdevice;
816 PDEBUG("executed. idx=%d\n", instance->ao_idx);
819 *subtype = ME_SUBTYPE_SINGLE;
820 return ME_ERRNO_SUCCESS;
823 static int me1600_ao_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
825 PDEBUG("executed.\n");
826 *caps = ME_CAPS_AO_TRIG_SYNCHRONOUS;
827 return ME_ERRNO_SUCCESS;
830 static int me1600_ao_query_range_by_min_max(me_subdevice_t * subdevice,
833 int *max, int *maxdata, int *range)
835 me1600_ao_subdevice_t *instance;
840 instance = (me1600_ao_subdevice_t *) subdevice;
842 PDEBUG("executed. idx=%d\n", instance->ao_idx);
844 if ((*max - *min) < 0) {
845 PERROR("Invalid minimum and maximum values specified.\n");
846 return ME_ERRNO_INVALID_MIN_MAX;
848 // Maximum ranges are slightly less then 10V or 20mA. For convenient we accepted this value as valid one.
849 if (unit == ME_UNIT_VOLT) {
850 for (i = 0; i < instance->u_ranges_count; i++) {
851 if ((instance->u_ranges[i].min <= *min)
852 && ((instance->u_ranges[i].max + 5000) >= *max)) {
853 if ((instance->u_ranges[i].max -
854 instance->u_ranges[i].min) - (*max -
859 (instance->u_ranges[i].max -
860 instance->u_ranges[i].min) -
867 PERROR("No matching range found.\n");
868 return ME_ERRNO_NO_RANGE;
870 *min = instance->u_ranges[r].min;
871 *max = instance->u_ranges[r].max;
874 } else if (unit == ME_UNIT_AMPERE) {
875 for (i = 0; i < instance->i_ranges_count; i++) {
876 if ((instance->i_ranges[i].min <= *min)
877 && (instance->i_ranges[i].max + 5000 >= *max)) {
878 if ((instance->i_ranges[i].max -
879 instance->i_ranges[i].min) - (*max -
884 (instance->i_ranges[i].max -
885 instance->i_ranges[i].min) -
892 PERROR("No matching range found.\n");
893 return ME_ERRNO_NO_RANGE;
895 *min = instance->i_ranges[r].min;
896 *max = instance->i_ranges[r].max;
897 *range = r + instance->u_ranges_count;
900 PERROR("Invalid physical unit specified.\n");
901 return ME_ERRNO_INVALID_UNIT;
903 *maxdata = ME1600_AO_MAX_DATA;
905 return ME_ERRNO_SUCCESS;
908 static int me1600_ao_query_number_ranges(me_subdevice_t * subdevice,
909 int unit, int *count)
911 me1600_ao_subdevice_t *instance;
913 PDEBUG("executed.\n");
915 instance = (me1600_ao_subdevice_t *) subdevice;
918 *count = instance->u_ranges_count;
921 *count = instance->i_ranges_count;
924 *count = instance->u_ranges_count + instance->i_ranges_count;
930 return ME_ERRNO_SUCCESS;
933 static int me1600_ao_query_range_info(me_subdevice_t * subdevice,
936 int *min, int *max, int *maxdata)
938 me1600_ao_subdevice_t *instance;
940 PDEBUG("executed.\n");
942 instance = (me1600_ao_subdevice_t *) subdevice;
945 (instance->u_ranges_count + instance->i_ranges_count))
947 PERROR("Invalid range number specified.\n");
948 return ME_ERRNO_INVALID_RANGE;
951 if (range < instance->u_ranges_count) {
952 *unit = ME_UNIT_VOLT;
953 *min = instance->u_ranges[range].min;
954 *max = instance->u_ranges[range].max;
955 } else if (range < instance->u_ranges_count + instance->i_ranges_count) {
956 *unit = ME_UNIT_AMPERE;
957 *min = instance->i_ranges[range - instance->u_ranges_count].min;
958 *max = instance->i_ranges[range - instance->u_ranges_count].max;
960 *maxdata = ME1600_AO_MAX_DATA;
962 return ME_ERRNO_SUCCESS;
965 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
966 static void me1600_ao_work_control_task(void *subdevice)
968 static void me1600_ao_work_control_task(struct work_struct *work)
971 me1600_ao_subdevice_t *instance;
975 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
976 instance = (me1600_ao_subdevice_t *) subdevice;
979 container_of((void *)work, me1600_ao_subdevice_t, ao_control_task);
982 PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
985 if (!((instance->ao_regs_shadows)->trigger & instance->ao_idx)) { // Output was triggerd.
989 if (instance->status == ao_status_single_run) {
990 instance->status = ao_status_single_end;
993 } else if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
994 PDEBUG("Timeout reached.\n");
995 spin_lock(instance->ao_shadows_lock);
996 // Restore old settings.
997 PDEBUG("Write old value back to register.\n");
998 (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
999 (instance->ao_regs_shadows)->mirror[instance->ao_idx];
1001 outw((instance->ao_regs_shadows)->mirror[instance->ao_idx],
1002 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
1003 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
1005 (instance->ao_regs_shadows)->registry[instance->
1008 (instance->ao_regs_shadows)->mirror[instance->
1011 //Remove from synchronous strt list.
1012 (instance->ao_regs_shadows)->trigger &=
1013 ~(0x1 << instance->ao_idx);
1014 if (instance->status == ao_status_none) {
1015 instance->status = ao_status_single_end;
1017 spin_unlock(instance->ao_shadows_lock);
1024 if (signaling) { //Signal it.
1025 wake_up_interruptible_all(&instance->wait_queue);
1028 if (instance->ao_control_task_flag && reschedule) { // Reschedule task
1029 queue_delayed_work(instance->me1600_workqueue,
1030 &instance->ao_control_task, 1);
1032 PINFO("<%s> Ending control task.\n", __func__);