Staging: meilhaus: unsigned won't get negative after subtraction
[linux-2.6/mini2440.git] / drivers / staging / meilhaus / me6000_ao.c
blobdcb3526d591cfaf23f74cd21d2e7214d3eb5de3e
1 /**
2 * @file me6000_ao.c
4 * @brief ME-6000 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 */
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.
28 #ifndef __KERNEL__
29 # define __KERNEL__
30 #endif
32 /* Includes
34 #include <linux/version.h>
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <asm/io.h>
40 #include <asm/uaccess.h>
41 #include <linux/types.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
45 #include <linux/workqueue.h>
47 #include "medefines.h"
48 #include "meinternal.h"
49 #include "meerror.h"
51 #include "medebug.h"
52 #include "meids.h"
53 #include "me6000_reg.h"
54 #include "me6000_ao_reg.h"
55 #include "me6000_ao.h"
57 /* Defines
60 static int me6000_ao_query_range_by_min_max(me_subdevice_t * subdevice,
61 int unit,
62 int *min,
63 int *max, int *maxdata, int *range);
65 static int me6000_ao_query_number_ranges(me_subdevice_t * subdevice,
66 int unit, int *count);
68 static int me6000_ao_query_range_info(me_subdevice_t * subdevice,
69 int range,
70 int *unit,
71 int *min, int *max, int *maxdata);
73 static int me6000_ao_query_timer(me_subdevice_t * subdevice,
74 int timer,
75 int *base_frequency,
76 long long *min_ticks, long long *max_ticks);
78 static int me6000_ao_query_number_channels(me_subdevice_t * subdevice,
79 int *number);
81 static int me6000_ao_query_subdevice_type(me_subdevice_t * subdevice,
82 int *type, int *subtype);
84 static int me6000_ao_query_subdevice_caps(me_subdevice_t * subdevice,
85 int *caps);
87 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
88 int cap, int *args, int count);
90 /** Remove subdevice. */
91 static void me6000_ao_destructor(struct me_subdevice *subdevice);
93 /** Reset subdevice. Stop all actions. Reset registry. Disable FIFO. Set output to 0V and status to 'none'. */
94 static int me6000_ao_io_reset_subdevice(me_subdevice_t * subdevice,
95 struct file *filep, int flags);
97 /** Set output as single */
98 static int me6000_ao_io_single_config(me_subdevice_t * subdevice,
99 struct file *filep,
100 int channel,
101 int single_config,
102 int ref,
103 int trig_chan,
104 int trig_type, int trig_edge, int flags);
106 /** Pass to user actual value of output. */
107 static int me6000_ao_io_single_read(me_subdevice_t * subdevice,
108 struct file *filep,
109 int channel,
110 int *value, int time_out, int flags);
112 /** Write to output requed value. */
113 static int me6000_ao_io_single_write(me_subdevice_t * subdevice,
114 struct file *filep,
115 int channel,
116 int value, int time_out, int flags);
118 /** Set output as streamed device. */
119 static int me6000_ao_io_stream_config(me_subdevice_t * subdevice,
120 struct file *filep,
121 meIOStreamConfig_t * config_list,
122 int count,
123 meIOStreamTrigger_t * trigger,
124 int fifo_irq_threshold, int flags);
126 /** Wait for / Check empty space in buffer. */
127 static int me6000_ao_io_stream_new_values(me_subdevice_t * subdevice,
128 struct file *filep,
129 int time_out, int *count, int flags);
131 /** Start streaming. */
132 static int me6000_ao_io_stream_start(me_subdevice_t * subdevice,
133 struct file *filep,
134 int start_mode, int time_out, int flags);
136 /** Check actual state. / Wait for end. */
137 static int me6000_ao_io_stream_status(me_subdevice_t * subdevice,
138 struct file *filep,
139 int wait,
140 int *status, int *values, int flags);
142 /** Stop streaming. */
143 static int me6000_ao_io_stream_stop(me_subdevice_t * subdevice,
144 struct file *filep,
145 int stop_mode, int flags);
147 /** Write datas to buffor. */
148 static int me6000_ao_io_stream_write(me_subdevice_t * subdevice,
149 struct file *filep,
150 int write_mode,
151 int *values, int *count, int flags);
153 /** Interrupt handler. Copy from buffer to FIFO. */
154 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
155 static irqreturn_t me6000_ao_isr(int irq, void *dev_id);
156 #else
157 static irqreturn_t me6000_ao_isr(int irq, void *dev_id, struct pt_regs *regs);
158 #endif
160 /** Copy data from circular buffer to fifo (fast) in wraparound mode. */
161 int inline ao_write_data_wraparound(me6000_ao_subdevice_t * instance, int count,
162 int start_pos);
164 /** Copy data from circular buffer to fifo (fast).*/
165 int inline ao_write_data(me6000_ao_subdevice_t * instance, int count,
166 int start_pos);
168 /** Copy data from circular buffer to fifo (slow).*/
169 int inline ao_write_data_pooling(me6000_ao_subdevice_t * instance, int count,
170 int start_pos);
172 /** Copy data from user space to circular buffer. */
173 int inline ao_get_data_from_user(me6000_ao_subdevice_t * instance, int count,
174 int *user_values);
176 /** Stop presentation. Preserve FIFOs. */
177 int inline ao_stop_immediately(me6000_ao_subdevice_t * instance);
179 /** Function for checking timeout in non-blocking mode. */
180 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
181 static void me6000_ao_work_control_task(void *subdevice);
182 #else
183 static void me6000_ao_work_control_task(struct work_struct *work);
184 #endif
186 /* Functions
189 static int me6000_ao_io_reset_subdevice(me_subdevice_t * subdevice,
190 struct file *filep, int flags)
192 me6000_ao_subdevice_t *instance;
193 int err = ME_ERRNO_SUCCESS;
194 uint32_t tmp;
195 uint32_t ctrl;
197 instance = (me6000_ao_subdevice_t *) subdevice;
199 PDEBUG("executed. idx=%d\n", instance->ao_idx);
201 if (flags) {
202 PERROR("Invalid flag specified.\n");
203 return ME_ERRNO_INVALID_FLAGS;
206 ME_SUBDEVICE_ENTER;
208 instance->status = ao_status_none;
209 instance->ao_control_task_flag = 0;
210 cancel_delayed_work(&instance->ao_control_task);
211 instance->timeout.delay = 0;
212 instance->timeout.start_time = jiffies;
214 //Stop state machine.
215 err = ao_stop_immediately(instance);
217 //Remove from synchronous start.
218 spin_lock(instance->preload_reg_lock);
219 tmp = inl(instance->preload_reg);
220 tmp &=
221 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
222 ao_idx);
223 outl(tmp, instance->preload_reg);
224 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
225 instance->preload_reg - instance->reg_base, tmp);
226 *instance->preload_flags &=
227 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
228 ao_idx);
230 //Reset triggering flag
231 *instance->triggering_flags &= ~(0x1 << instance->ao_idx);
232 spin_unlock(instance->preload_reg_lock);
234 if (instance->fifo) {
235 //Set single mode, dissable FIFO, dissable external trigger, block interrupt.
236 ctrl = ME6000_AO_MODE_SINGLE;
238 //Block ISM.
239 ctrl |=
240 (ME6000_AO_CTRL_BIT_STOP |
241 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
243 outl(ctrl, instance->ctrl_reg);
244 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
245 instance->reg_base,
246 instance->ctrl_reg - instance->reg_base, ctrl);
247 //Set speed
248 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
249 //Reset interrupt latch
250 inl(instance->irq_reset_reg);
253 instance->hardware_stop_delay = HZ / 10; //100ms
255 //Set output to 0V
256 outl(0x8000, instance->single_reg);
257 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
258 instance->single_reg - instance->reg_base, 0x8000);
260 instance->circ_buf.head = 0;
261 instance->circ_buf.tail = 0;
262 instance->preloaded_count = 0;
263 instance->data_count = 0;
264 instance->single_value = 0x8000;
265 instance->single_value_in_fifo = 0x8000;
267 //Set status to signal that device is unconfigured.
268 instance->status = ao_status_none;
269 //Signal reset if user is on wait.
270 wake_up_interruptible_all(&instance->wait_queue);
272 ME_SUBDEVICE_EXIT;
274 return err;
277 static int me6000_ao_io_single_config(me_subdevice_t * subdevice,
278 struct file *filep,
279 int channel,
280 int single_config,
281 int ref,
282 int trig_chan,
283 int trig_type, int trig_edge, int flags)
285 me6000_ao_subdevice_t *instance;
286 int err = ME_ERRNO_SUCCESS;
287 uint32_t ctrl;
288 uint32_t sync;
289 unsigned long cpu_flags;
291 instance = (me6000_ao_subdevice_t *) subdevice;
293 PDEBUG("executed. ID=%d\n", instance->ao_idx);
295 // Checking parameters
296 if (flags) {
297 PERROR
298 ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
299 return ME_ERRNO_INVALID_FLAGS;
302 if (instance->fifo) { //Stream hardware (with or without fifo)
303 if ((trig_edge == ME_TRIG_TYPE_SW)
304 && (trig_edge != ME_TRIG_EDGE_NONE)) {
305 PERROR
306 ("Invalid trigger edge. Software trigger has not edge.\n");
307 return ME_ERRNO_INVALID_TRIG_EDGE;
310 if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
311 switch (trig_edge) {
312 case ME_TRIG_EDGE_ANY:
313 case ME_TRIG_EDGE_RISING:
314 case ME_TRIG_EDGE_FALLING:
315 break;
317 default:
318 PERROR("Invalid trigger edge.\n");
319 return ME_ERRNO_INVALID_TRIG_EDGE;
323 if ((trig_type != ME_TRIG_TYPE_SW)
324 && (trig_type != ME_TRIG_TYPE_EXT_DIGITAL)) {
325 PERROR
326 ("Invalid trigger type. Trigger must be software or digital.\n");
327 return ME_ERRNO_INVALID_TRIG_TYPE;
329 } else { //Single
330 if (trig_edge != ME_TRIG_EDGE_NONE) {
331 PERROR
332 ("Invalid trigger edge. Single output trigger hasn't own edge.\n");
333 return ME_ERRNO_INVALID_TRIG_EDGE;
336 if (trig_type != ME_TRIG_TYPE_SW) {
337 PERROR
338 ("Invalid trigger type. Trigger must be software.\n");
339 return ME_ERRNO_INVALID_TRIG_TYPE;
344 if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
345 && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
346 PERROR("Invalid trigger channel specified.\n");
347 return ME_ERRNO_INVALID_TRIG_CHAN;
350 if ((trig_type == ME_TRIG_TYPE_EXT_DIGITAL) && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS))
352 PERROR("Invalid trigger channel specified. Must be synchronous when digital is choose.\n");
353 return ME_ERRNO_INVALID_TRIG_CHAN;
356 if (ref != ME_REF_AO_GROUND) {
357 PERROR
358 ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
359 return ME_ERRNO_INVALID_REF;
362 if (single_config != 0) {
363 PERROR
364 ("Invalid single config specified. Only one range for anlog outputs is available.\n");
365 return ME_ERRNO_INVALID_SINGLE_CONFIG;
368 if (channel != 0) {
369 PERROR
370 ("Invalid channel number specified. Analog output have only one channel.\n");
371 return ME_ERRNO_INVALID_CHANNEL;
374 ME_SUBDEVICE_ENTER;
376 //Subdevice running in stream mode!
377 if ((instance->status >= ao_status_stream_run_wait)
378 && (instance->status < ao_status_stream_end)) {
379 PERROR("Subdevice is busy.\n");
380 ME_SUBDEVICE_EXIT;
382 return ME_ERRNO_SUBDEVICE_BUSY;
384 /// @note For single all calls (config and write) are erasing previous state!
386 instance->status = ao_status_none;
388 // Correct single mirrors
389 instance->single_value_in_fifo = instance->single_value;
391 //Stop device
392 err = ao_stop_immediately(instance);
393 if (err) {
394 PERROR_CRITICAL("FSM IS BUSY!\n");
395 ME_SUBDEVICE_EXIT;
397 return ME_ERRNO_SUBDEVICE_BUSY;
400 if (instance->fifo) { // Set control register.
401 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
402 // Set stop bit. Stop streaming mode (If running.).
403 ctrl = inl(instance->ctrl_reg);
404 //Reset all bits.
405 ctrl =
406 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
407 if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
408 PINFO("External digital trigger.\n");
410 if (trig_edge == ME_TRIG_EDGE_ANY) {
411 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
412 instance->ctrl_trg =
413 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
414 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
415 } else if (trig_edge == ME_TRIG_EDGE_FALLING) {
416 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
417 instance->ctrl_trg =
418 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
419 } else if (trig_edge == ME_TRIG_EDGE_RISING) {
420 instance->ctrl_trg = 0x0;
422 } else if (trig_type == ME_TRIG_TYPE_SW) {
423 PDEBUG("SOFTWARE TRIGGER\n");
424 instance->ctrl_trg = 0x0;
426 outl(ctrl, instance->ctrl_reg);
427 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
428 instance->reg_base,
429 instance->ctrl_reg - instance->reg_base, ctrl);
430 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
431 } else {
432 PDEBUG("SOFTWARE TRIGGER\n");
435 // Set preload/synchronization register.
436 spin_lock(instance->preload_reg_lock);
438 if (trig_type == ME_TRIG_TYPE_SW) {
439 *instance->preload_flags &=
440 ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
441 } else //if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL)
443 *instance->preload_flags |=
444 ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
447 if (trig_chan == ME_TRIG_CHAN_DEFAULT) {
448 *instance->preload_flags &=
449 ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
450 } else //if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS)
452 *instance->preload_flags |=
453 ME6000_AO_SYNC_HOLD << instance->ao_idx;
456 //Reset hardware register
457 sync = inl(instance->preload_reg);
458 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
459 instance->preload_reg - instance->reg_base, sync);
460 sync &= ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
461 sync |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
463 //Output configured in default mode (safe one)
464 outl(sync, instance->preload_reg);
465 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
466 instance->preload_reg - instance->reg_base, sync);
467 spin_unlock(instance->preload_reg_lock);
469 instance->status = ao_status_single_configured;
471 ME_SUBDEVICE_EXIT;
473 return err;
476 static int me6000_ao_io_single_read(me_subdevice_t * subdevice,
477 struct file *filep,
478 int channel,
479 int *value, int time_out, int flags)
481 me6000_ao_subdevice_t *instance;
482 int err = ME_ERRNO_SUCCESS;
484 unsigned long j;
485 unsigned long delay = 0;
487 instance = (me6000_ao_subdevice_t *) subdevice;
489 PDEBUG("executed. idx=%d\n", instance->ao_idx);
491 if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
492 PERROR("Invalid flag specified. %d\n", flags);
493 return ME_ERRNO_INVALID_FLAGS;
496 if ((instance->status >= ao_status_stream_configured)
497 && (instance->status <= ao_status_stream_end)) {
498 PERROR("Subdevice not configured to work in single mode!\n");
499 return ME_ERRNO_PREVIOUS_CONFIG;
502 if (channel != 0) {
503 PERROR("Invalid channel number specified.\n");
504 return ME_ERRNO_INVALID_CHANNEL;
507 if (time_out < 0) {
508 PERROR("Invalid timeout specified.\n");
509 return ME_ERRNO_INVALID_TIMEOUT;
512 ME_SUBDEVICE_ENTER;
513 if ((!flags) && (instance->status == ao_status_single_run_wait)) { //Blocking mode. Wait for trigger.
514 if (time_out) {
515 delay = (time_out * HZ) / 1000;
516 if (delay == 0)
517 delay = 1;
520 j = jiffies;
522 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
523 wait_event_interruptible_timeout(instance->wait_queue,
524 (instance->status !=
525 ao_status_single_run_wait),
526 (delay) ? delay : LONG_MAX);
528 if (instance->status == ao_status_none) {
529 PDEBUG("Single canceled.\n");
530 err = ME_ERRNO_CANCELLED;
533 if (signal_pending(current)) {
534 PERROR("Wait on start of state machine interrupted.\n");
535 instance->status = ao_status_none;
536 ao_stop_immediately(instance);
537 err = ME_ERRNO_SIGNAL;
540 if ((delay) && ((jiffies - j) >= delay)) {
541 PDEBUG("Timeout reached.\n");
542 err = ME_ERRNO_TIMEOUT;
545 *value =
546 (!err) ? instance->single_value_in_fifo : instance->
547 single_value;
548 } else { //Non-blocking mode
549 //Read value
550 *value = instance->single_value;
553 ME_SUBDEVICE_EXIT;
555 return err;
558 static int me6000_ao_io_single_write(me_subdevice_t * subdevice,
559 struct file *filep,
560 int channel,
561 int value, int time_out, int flags)
563 me6000_ao_subdevice_t *instance;
564 int err = ME_ERRNO_SUCCESS;
565 unsigned long cpu_flags;
566 unsigned long j;
567 unsigned long delay = 0;
569 uint32_t sync_mask;
570 uint32_t mode;
572 uint32_t tmp;
574 /// Workaround for mix-mode - begin
575 uint32_t ctrl = 0x0;
576 uint32_t status;
577 /// Workaround for mix-mode - end
579 instance = (me6000_ao_subdevice_t *) subdevice;
581 PDEBUG("executed. idx=%d\n", instance->ao_idx);
583 if (flags &
584 ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
585 ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
586 PERROR("Invalid flag specified.\n");
587 return ME_ERRNO_INVALID_FLAGS;
590 if ((instance->status == ao_status_none)
591 || (instance->status > ao_status_single_end)) {
592 PERROR("Subdevice not configured to work in single mode!\n");
593 return ME_ERRNO_PREVIOUS_CONFIG;
596 if (channel != 0) {
597 PERROR("Invalid channel number specified.\n");
598 return ME_ERRNO_INVALID_CHANNEL;
601 if (value & ~ME6000_AO_MAX_DATA) {
602 PERROR("Invalid value provided.\n");
603 return ME_ERRNO_VALUE_OUT_OF_RANGE;
606 if (time_out < 0) {
607 PERROR("Invalid timeout specified.\n");
608 return ME_ERRNO_INVALID_TIMEOUT;
611 ME_SUBDEVICE_ENTER;
613 /// @note For single all calls (config and write) are erasing previous state!
615 //Cancel control task
616 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
617 instance->ao_control_task_flag = 0;
618 cancel_delayed_work(&instance->ao_control_task);
620 // Correct single mirrors
621 instance->single_value_in_fifo = instance->single_value;
623 //Stop device
624 err = ao_stop_immediately(instance);
625 if (err) {
626 PERROR_CRITICAL("FSM IS BUSY!\n");
627 ME_SUBDEVICE_EXIT;
629 return ME_ERRNO_SUBDEVICE_BUSY;
632 if (time_out) {
633 delay = (time_out * HZ) / 1000;
635 if (delay == 0)
636 delay = 1;
639 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
641 instance->single_value_in_fifo = value;
643 if (instance->fifo) {
644 ctrl = inl(instance->ctrl_reg);
647 if (instance->fifo & ME6000_AO_HAS_FIFO) { /// Workaround for mix-mode - begin
648 //Set speed
649 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
650 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
651 instance->reg_base,
652 instance->timer_reg - instance->reg_base,
653 (int)ME6000_AO_MIN_CHAN_TICKS);
654 instance->hardware_stop_delay = HZ / 10; //100ms
656 status = inl(instance->status_reg);
658 //Set the continous mode.
659 ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
660 ctrl |= ME6000_AO_MODE_CONTINUOUS;
662 //Prepare FIFO
663 if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO wasn't enabeled. Do it.
664 PINFO("Enableing FIFO.\n");
665 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
666 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
667 } else { //Check if FIFO is empty
668 if (status & ME6000_AO_STATUS_BIT_EF) { //FIFO not empty
669 PINFO("Reseting FIFO.\n");
670 ctrl &=
671 ~(ME6000_AO_CTRL_BIT_ENABLE_FIFO |
672 ME6000_AO_CTRL_BIT_ENABLE_IRQ);
673 outl(ctrl, instance->ctrl_reg);
674 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
675 instance->reg_base,
676 instance->ctrl_reg -
677 instance->reg_base, ctrl);
679 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
680 } else { //FIFO empty, only interrupt needs to be disabled!
681 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
685 outl(ctrl, instance->ctrl_reg);
686 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
687 instance->reg_base,
688 instance->ctrl_reg - instance->reg_base, ctrl);
690 //Reset interrupt latch
691 inl(instance->irq_reset_reg);
693 //Write output - 1 value to FIFO
694 if (instance->ao_idx & 0x1) {
695 outl(value <<= 16, instance->fifo_reg);
696 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
697 instance->reg_base,
698 instance->fifo_reg - instance->reg_base,
699 value <<= 16);
700 } else {
701 outl(value, instance->fifo_reg);
702 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
703 instance->reg_base,
704 instance->fifo_reg - instance->reg_base,
705 value);
707 /// Workaround for mix-mode - end
708 } else { //No FIFO - always in single mode
709 //Write value
710 PDEBUG("Write value\n");
711 outl(value, instance->single_reg);
712 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
713 instance->reg_base,
714 instance->single_reg - instance->reg_base, value);
717 mode = *instance->preload_flags >> instance->ao_idx;
718 mode &= (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG);
720 PINFO("Triggering mode: 0x%08x\n", mode);
722 spin_lock(instance->preload_reg_lock);
723 sync_mask = inl(instance->preload_reg);
724 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
725 instance->preload_reg - instance->reg_base, sync_mask);
726 switch (mode) {
727 case 0: //0x00000000: Individual software
728 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
730 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
731 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
732 if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) { //Now we can set correct mode.
733 sync_mask &=
734 ~((ME6000_AO_SYNC_EXT_TRIG |
735 ME6000_AO_SYNC_HOLD) << instance->
736 ao_idx);
738 outl(sync_mask, instance->preload_reg);
739 PDEBUG_REG
740 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
741 instance->reg_base,
742 instance->preload_reg - instance->reg_base,
743 sync_mask);
745 } else { // No FIFO - Single mode: In this case resetting 'ME6000_AO_SYNC_HOLD' will trigger output.
746 if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != ME6000_AO_SYNC_HOLD) { //Now we can set correct mode. This is exception. It is set to synchronous and triggered later.
747 sync_mask &=
748 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
749 ao_idx);
750 sync_mask |=
751 ME6000_AO_SYNC_HOLD << instance->ao_idx;
753 outl(sync_mask, instance->preload_reg);
754 PDEBUG_REG
755 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
756 instance->reg_base,
757 instance->preload_reg - instance->reg_base,
758 sync_mask);
761 instance->single_value = value;
762 break;
764 case ME6000_AO_SYNC_EXT_TRIG: //0x00010000: Individual hardware
765 PDEBUG("DIGITAL TRIGGER\n");
766 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
768 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
769 if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) { //Now we can set correct mode.
770 sync_mask &=
771 ~((ME6000_AO_SYNC_EXT_TRIG |
772 ME6000_AO_SYNC_HOLD) << instance->
773 ao_idx);
775 outl(sync_mask, instance->preload_reg);
776 PDEBUG_REG
777 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
778 instance->reg_base,
779 instance->preload_reg - instance->reg_base,
780 sync_mask);
782 } else { // No FIFO - Single mode
783 if ((sync_mask &
784 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
785 instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {
786 //Now we can set correct mode
787 sync_mask &=
788 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
789 ao_idx);
790 sync_mask |=
791 ME6000_AO_SYNC_HOLD << instance->ao_idx;
793 outl(sync_mask, instance->preload_reg);
794 PDEBUG_REG
795 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
796 instance->reg_base,
797 instance->preload_reg - instance->reg_base,
798 sync_mask);
801 break;
803 case ME6000_AO_SYNC_HOLD: //0x00000001: Synchronous software
804 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
806 if ((sync_mask &
807 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
808 instance->ao_idx)) !=
809 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
810 //Now we can set correct mode
811 sync_mask |=
812 ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
813 sync_mask |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
814 outl(sync_mask, instance->preload_reg);
815 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
816 instance->reg_base,
817 instance->preload_reg - instance->reg_base,
818 sync_mask);
820 //Set triggering flag
821 *instance->triggering_flags |= 0x1 << instance->ao_idx;
822 break;
824 case (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG): //0x00010001: Synchronous hardware
825 PDEBUG("DIGITAL TRIGGER\n");
826 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
828 if ((sync_mask &
829 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
830 instance->ao_idx)) !=
831 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
832 //Now we can set correct mode
833 sync_mask |=
834 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
835 instance->ao_idx;
836 outl(sync_mask, instance->preload_reg);
837 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
838 instance->reg_base,
839 instance->preload_reg - instance->reg_base,
840 sync_mask);
842 //Set triggering flag
843 *instance->triggering_flags |= 0x1 << instance->ao_idx;
844 break;
846 // spin_unlock(instance->preload_reg_lock); // Moved down.
848 if (instance->fifo) { //Activate ISM (remove 'stop' bits)
849 ctrl &=
850 ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
851 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
852 ctrl |= instance->ctrl_trg;
853 ctrl &=
854 ~(ME6000_AO_CTRL_BIT_STOP |
855 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
857 outl(ctrl, instance->ctrl_reg);
858 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
859 instance->reg_base,
860 instance->ctrl_reg - instance->reg_base, ctrl);
862 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
864 /// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!
866 PINFO("<%s> start mode= 0x%08x %s\n", __func__, mode,
867 (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
868 "");
869 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
870 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Trigger outputs
871 //Add channel to start list
872 outl(sync_mask |
873 (ME6000_AO_SYNC_HOLD << instance->ao_idx),
874 instance->preload_reg);
875 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
876 instance->reg_base,
877 instance->preload_reg - instance->reg_base,
878 sync_mask | (ME6000_AO_SYNC_HOLD <<
879 instance->ao_idx));
881 //Fire
882 PINFO
883 ("Fired all software synchronous outputs by software trigger.\n");
884 outl(0x8000, instance->single_reg);
885 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
886 instance->reg_base,
887 instance->single_reg - instance->reg_base,
888 0x8000);
890 //Restore save settings
891 outl(sync_mask, instance->preload_reg);
892 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
893 instance->reg_base,
894 instance->preload_reg - instance->reg_base,
895 sync_mask);
897 } else if (!mode) { //Trigger outputs
898 /* //Remove channel from start list
899 outl(sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
900 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
902 //Fire
903 PINFO("Software trigger.\n");
904 outl(0x8000, instance->single_reg);
905 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
906 instance->reg_base,
907 instance->single_reg - instance->reg_base,
908 0x8000);
910 /* //Restore save settings
911 outl(sync_mask, instance->preload_reg);
912 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask);
915 /// @note This is mix-mode case. For now I do not have possibility to trigger first 4 channels (continous mode) and other (single) ones at once.
916 /// @note Because triggering is not working it can not be add to synchronous list. First 4 channels don't need this information, anyway.
917 *instance->triggering_flags &= 0xFFFFFFF0;
918 } else { // No FIFO - Single mode
919 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Fired all software synchronous outputs.
920 tmp = ~(*instance->preload_flags | 0xFFFF0000);
921 PINFO
922 ("Fired all software synchronous outputs. mask:0x%08x\n",
923 tmp);
924 tmp |= sync_mask & 0xFFFF0000;
925 // Add this channel to list
926 tmp &= ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
928 //Fire
929 PINFO("Software trigger.\n");
930 outl(tmp, instance->preload_reg);
931 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
932 instance->reg_base,
933 instance->preload_reg - instance->reg_base,
934 tmp);
936 //Restore save settings
937 outl(sync_mask, instance->preload_reg);
938 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
939 instance->reg_base,
940 instance->preload_reg - instance->reg_base,
941 sync_mask);
943 //Set all as triggered.
944 *instance->triggering_flags = 0x0;
945 } else if (!mode) { // Add this channel to list
946 outl(sync_mask &
947 ~(ME6000_AO_SYNC_HOLD << instance->ao_idx),
948 instance->preload_reg);
949 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
950 instance->reg_base,
951 instance->preload_reg - instance->reg_base,
952 sync_mask & ~(ME6000_AO_SYNC_HOLD <<
953 instance->ao_idx));
955 //Fire
956 PINFO("Software trigger.\n");
958 //Restore save settings
959 outl(sync_mask, instance->preload_reg);
960 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
961 instance->reg_base,
962 instance->preload_reg - instance->reg_base,
963 sync_mask);
965 //Set all as triggered.
966 *instance->triggering_flags = 0x0;
970 spin_unlock(instance->preload_reg_lock);
972 instance->status = ao_status_single_run_wait;
974 instance->timeout.delay = delay;
975 instance->timeout.start_time = jiffies;
976 instance->ao_control_task_flag = 1;
977 queue_delayed_work(instance->me6000_workqueue,
978 &instance->ao_control_task, 1);
980 if (!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
981 j = jiffies;
983 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
984 wait_event_interruptible_timeout(instance->wait_queue,
985 (instance->status !=
986 ao_status_single_run_wait),
987 (delay) ? delay +
988 1 : LONG_MAX);
990 if (instance->status != ao_status_single_end) {
991 PDEBUG("Single canceled.\n");
992 err = ME_ERRNO_CANCELLED;
995 if (signal_pending(current)) {
996 PERROR("Wait on start of state machine interrupted.\n");
997 instance->ao_control_task_flag = 0;
998 cancel_delayed_work(&instance->ao_control_task);
999 ao_stop_immediately(instance);
1000 instance->status = ao_status_none;
1001 err = ME_ERRNO_SIGNAL;
1004 if ((delay) && ((jiffies - j) >= delay)) {
1005 if (instance->status == ao_status_single_end) {
1006 PDEBUG("Timeout reached.\n");
1007 } else if ((jiffies - j) > delay) {
1008 PERROR
1009 ("Timeout reached. Not handled by control task!\n");
1010 ao_stop_immediately(instance);
1011 } else {
1012 PERROR
1013 ("Timeout reached. Signal come but status is strange: %d\n",
1014 instance->status);
1015 ao_stop_immediately(instance);
1018 instance->ao_control_task_flag = 0;
1019 cancel_delayed_work(&instance->ao_control_task);
1020 instance->status = ao_status_single_end;
1021 err = ME_ERRNO_TIMEOUT;
1025 ME_SUBDEVICE_EXIT;
1027 return err;
1030 static int me6000_ao_io_stream_config(me_subdevice_t * subdevice,
1031 struct file *filep,
1032 meIOStreamConfig_t * config_list,
1033 int count,
1034 meIOStreamTrigger_t * trigger,
1035 int fifo_irq_threshold, int flags)
1037 me6000_ao_subdevice_t *instance;
1038 int err = ME_ERRNO_SUCCESS;
1039 uint32_t ctrl;
1040 unsigned long cpu_flags;
1041 uint64_t conv_ticks;
1042 unsigned int conv_start_ticks_low = trigger->iConvStartTicksLow;
1043 unsigned int conv_start_ticks_high = trigger->iConvStartTicksHigh;
1045 instance = (me6000_ao_subdevice_t *) subdevice;
1047 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1049 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1050 PERROR("Not a streaming ao.\n");
1051 return ME_ERRNO_NOT_SUPPORTED;
1054 conv_ticks =
1055 (uint64_t) conv_start_ticks_low +
1056 ((uint64_t) conv_start_ticks_high << 32);
1058 if (flags &
1059 ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY |
1060 ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1061 PERROR("Invalid flags.\n");
1062 return ME_ERRNO_INVALID_FLAGS;
1065 if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {
1066 if (!(flags & ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1067 PERROR
1068 ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
1069 return ME_ERRNO_INVALID_FLAGS;
1072 if ((trigger->iAcqStopTrigType != ME_TRIG_TYPE_NONE)
1073 || (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE)) {
1074 PERROR
1075 ("Hardware wraparound mode must be in infinite mode.\n");
1076 return ME_ERRNO_INVALID_FLAGS;
1080 if (count != 1) {
1081 PERROR("Only 1 entry in config list acceptable.\n");
1082 return ME_ERRNO_INVALID_CONFIG_LIST_COUNT;
1085 if (config_list[0].iChannel != 0) {
1086 PERROR("Invalid channel number specified.\n");
1087 return ME_ERRNO_INVALID_CHANNEL;
1090 if (config_list[0].iStreamConfig != 0) {
1091 PERROR("Only one range available.\n");
1092 return ME_ERRNO_INVALID_STREAM_CONFIG;
1095 if (config_list[0].iRef != ME_REF_AO_GROUND) {
1096 PERROR("Output is referenced to ground.\n");
1097 return ME_ERRNO_INVALID_REF;
1100 if ((trigger->iAcqStartTicksLow != 0)
1101 || (trigger->iAcqStartTicksHigh != 0)) {
1102 PERROR
1103 ("Invalid acquisition start trigger argument specified.\n");
1104 return ME_ERRNO_INVALID_ACQ_START_ARG;
1107 if (config_list[0].iFlags) {
1108 PERROR("Invalid config list flag.\n");
1109 return ME_ERRNO_INVALID_FLAGS;
1112 if ((trigger->iAcqStartTrigType != ME_TRIG_TYPE_SW)
1113 && (trigger->iAcqStartTrigType != ME_TRIG_TYPE_EXT_DIGITAL)) {
1114 PERROR("Invalid acquisition start trigger type specified.\n");
1115 return ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE;
1118 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {
1119 switch (trigger->iAcqStartTrigEdge) {
1120 case ME_TRIG_EDGE_RISING:
1121 case ME_TRIG_EDGE_FALLING:
1122 case ME_TRIG_EDGE_ANY:
1123 break;
1125 default:
1126 PERROR
1127 ("Invalid acquisition start trigger edge specified.\n");
1128 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1132 if ((trigger->iAcqStartTrigType == ME_TRIG_TYPE_SW)
1133 && (trigger->iAcqStartTrigEdge != ME_TRIG_TYPE_NONE)) {
1134 PERROR("Invalid acquisition start trigger edge specified.\n");
1135 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1138 if (trigger->iScanStartTrigType != ME_TRIG_TYPE_FOLLOW) {
1139 PERROR("Invalid scan start trigger type specified.\n");
1140 return ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE;
1143 if (trigger->iConvStartTrigType != ME_TRIG_TYPE_TIMER) {
1144 PERROR("Invalid conv start trigger type specified.\n");
1145 return ME_ERRNO_INVALID_CONV_START_TRIG_TYPE;
1148 if ((conv_ticks < ME6000_AO_MIN_CHAN_TICKS)
1149 || (conv_ticks > ME6000_AO_MAX_CHAN_TICKS)) {
1150 PERROR("Invalid conv start trigger argument specified.\n");
1151 return ME_ERRNO_INVALID_CONV_START_ARG;
1154 if (trigger->iAcqStartTicksLow || trigger->iAcqStartTicksHigh) {
1155 PERROR("Invalid acq start trigger argument specified.\n");
1156 return ME_ERRNO_INVALID_ACQ_START_ARG;
1159 if (trigger->iScanStartTicksLow || trigger->iScanStartTicksHigh) {
1160 PERROR("Invalid scan start trigger argument specified.\n");
1161 return ME_ERRNO_INVALID_SCAN_START_ARG;
1164 switch (trigger->iScanStopTrigType) {
1165 case ME_TRIG_TYPE_NONE:
1166 if (trigger->iScanStopCount != 0) {
1167 PERROR("Invalid scan stop count specified.\n");
1168 return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1170 break;
1172 case ME_TRIG_TYPE_COUNT:
1173 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1174 if (trigger->iScanStopCount <= 0) {
1175 PERROR("Invalid scan stop count specified.\n");
1176 return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1178 } else {
1179 PERROR("The continous mode has not 'scan' contects.\n");
1180 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1182 break;
1184 default:
1185 PERROR("Invalid scan stop trigger type specified.\n");
1186 return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE;
1189 switch (trigger->iAcqStopTrigType) {
1190 case ME_TRIG_TYPE_NONE:
1191 if (trigger->iAcqStopCount != 0) {
1192 PERROR("Invalid acq stop count specified.\n");
1193 return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1195 break;
1197 case ME_TRIG_TYPE_COUNT:
1198 if (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE) {
1199 PERROR("Invalid acq stop trigger type specified.\n");
1200 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1203 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1204 if (trigger->iAcqStopCount <= 0) {
1205 PERROR
1206 ("The continous mode has not 'scan' contects.\n");
1207 return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1210 // else
1211 // {
1212 // PERROR("Invalid acq stop trigger type specified.\n");
1213 // return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1214 // }
1216 break;
1218 default:
1219 PERROR("Invalid acq stop trigger type specified.\n");
1220 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1223 switch (trigger->iAcqStartTrigChan) {
1224 case ME_TRIG_CHAN_DEFAULT:
1225 case ME_TRIG_CHAN_SYNCHRONOUS:
1226 break;
1228 default:
1229 PERROR("Invalid acq start trigger channel specified.\n");
1230 return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN;
1233 ME_SUBDEVICE_ENTER;
1235 //Stop device
1237 //Cancel control task
1238 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1239 instance->ao_control_task_flag = 0;
1240 cancel_delayed_work(&instance->ao_control_task);
1242 //Check if state machine is stopped.
1243 err = ao_stop_immediately(instance);
1244 if (err) {
1245 PERROR_CRITICAL("FSM IS BUSY!\n");
1246 ME_SUBDEVICE_EXIT;
1248 return ME_ERRNO_SUBDEVICE_BUSY;
1251 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1252 //Reset control register. Block all actions. Disable IRQ. Disable FIFO.
1253 ctrl = ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
1254 outl(ctrl, instance->ctrl_reg);
1255 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1256 instance->ctrl_reg - instance->reg_base, ctrl);
1258 //Reset interrupt latch
1259 inl(instance->irq_reset_reg);
1261 //This is paranoic, but to be sure.
1262 instance->preloaded_count = 0;
1263 instance->data_count = 0;
1264 instance->circ_buf.head = 0;
1265 instance->circ_buf.tail = 0;
1267 /* Set mode. */
1268 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) { //Wraparound
1269 if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) { //Hardware wraparound
1270 PINFO("Hardware wraparound.\n");
1271 ctrl |= ME6000_AO_MODE_WRAPAROUND;
1272 instance->mode = ME6000_AO_HW_WRAP_MODE;
1273 } else { //Software wraparound
1274 PINFO("Software wraparound.\n");
1275 ctrl |= ME6000_AO_MODE_CONTINUOUS;
1276 instance->mode = ME6000_AO_SW_WRAP_MODE;
1278 } else { //Continous
1279 PINFO("Continous.\n");
1280 ctrl |= ME6000_AO_MODE_CONTINUOUS;
1281 instance->mode = ME6000_AO_CONTINOUS;
1284 //Set the trigger edge.
1285 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) { //Set the trigger type and edge for external trigger.
1286 PINFO("External digital trigger.\n");
1287 instance->start_mode = ME6000_AO_EXT_TRIG;
1289 switch (trigger->iAcqStartTrigEdge) {
1290 case ME_TRIG_EDGE_RISING:
1291 PINFO("Set the trigger edge: rising.\n");
1292 instance->ctrl_trg = 0x0;
1293 break;
1295 case ME_TRIG_EDGE_FALLING:
1296 PINFO("Set the trigger edge: falling.\n");
1297 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1298 instance->ctrl_trg = ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1299 break;
1301 case ME_TRIG_EDGE_ANY:
1302 PINFO("Set the trigger edge: both edges.\n");
1303 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1304 instance->ctrl_trg =
1305 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
1306 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1307 break;
1309 } else {
1310 PINFO("Internal software trigger.\n");
1311 instance->start_mode = 0;
1314 //Set the stop mode and value.
1315 if (trigger->iAcqStopTrigType == ME_TRIG_TYPE_COUNT) { //Amount of data
1316 instance->stop_mode = ME6000_AO_ACQ_STOP_MODE;
1317 instance->stop_count = trigger->iAcqStopCount;
1318 } else if (trigger->iScanStopTrigType == ME_TRIG_TYPE_COUNT) { //Amount of 'scans'
1319 instance->stop_mode = ME6000_AO_SCAN_STOP_MODE;
1320 instance->stop_count = trigger->iScanStopCount;
1321 } else { //Infinite
1322 instance->stop_mode = ME6000_AO_INF_STOP_MODE;
1323 instance->stop_count = 0;
1326 PINFO("Stop count: %d.\n", instance->stop_count);
1328 if (trigger->iAcqStartTrigChan == ME_TRIG_CHAN_SYNCHRONOUS) { //Synchronous start
1329 instance->start_mode |= ME6000_AO_SYNC_HOLD;
1330 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) { //Externaly triggered
1331 PINFO("Synchronous start. Externaly trigger active.\n");
1332 instance->start_mode |= ME6000_AO_SYNC_EXT_TRIG;
1334 #ifdef MEDEBUG_INFO
1335 else {
1336 PINFO
1337 ("Synchronous start. Externaly trigger dissabled.\n");
1339 #endif
1342 //Set speed
1343 outl(conv_ticks - 2, instance->timer_reg);
1344 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%llx\n", instance->reg_base,
1345 instance->timer_reg - instance->reg_base, conv_ticks - 2);
1346 instance->hardware_stop_delay = (int)(conv_ticks * HZ) / ME6000_AO_BASE_FREQUENCY; //<== MUST be with cast!
1348 // Write the control word
1349 outl(ctrl, instance->ctrl_reg);
1350 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1351 instance->ctrl_reg - instance->reg_base, ctrl);
1353 //Set status.
1354 instance->status = ao_status_stream_configured;
1355 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1357 ME_SUBDEVICE_EXIT;
1359 return err;
1362 static int me6000_ao_io_stream_new_values(me_subdevice_t * subdevice,
1363 struct file *filep,
1364 int time_out, int *count, int flags)
1366 me6000_ao_subdevice_t *instance;
1367 int err = ME_ERRNO_SUCCESS;
1368 long t = 0;
1369 long j;
1371 instance = (me6000_ao_subdevice_t *) subdevice;
1373 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1375 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1376 PERROR("Not a streaming ao.\n");
1377 return ME_ERRNO_NOT_SUPPORTED;
1380 if (flags) {
1381 PERROR("Invalid flag specified.\n");
1382 return ME_ERRNO_INVALID_FLAGS;
1385 if (!instance->circ_buf.buf) {
1386 PERROR("Circular buffer not exists.\n");
1387 return ME_ERRNO_INTERNAL;
1390 if (time_out < 0) {
1391 PERROR("Invalid time_out specified.\n");
1392 return ME_ERRNO_INVALID_TIMEOUT;
1395 ME_SUBDEVICE_ENTER;
1397 if (me_circ_buf_space(&instance->circ_buf)) { //The buffer is NOT full.
1398 *count = me_circ_buf_space(&instance->circ_buf);
1399 } else { //The buffer is full.
1400 if (time_out) {
1401 t = (time_out * HZ) / 1000;
1403 if (t == 0)
1404 t = 1;
1405 } else { //Max time.
1406 t = LONG_MAX;
1409 *count = 0;
1411 j = jiffies;
1413 //Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
1414 wait_event_interruptible_timeout(instance->wait_queue,
1415 ((me_circ_buf_space
1416 (&instance->circ_buf))
1417 || !(inl(instance->status_reg)
1419 ME6000_AO_STATUS_BIT_FSM)),
1422 if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {
1423 PERROR("AO subdevice is not running.\n");
1424 err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
1425 } else if (signal_pending(current)) {
1426 PERROR("Wait on values interrupted from signal.\n");
1427 instance->status = ao_status_none;
1428 ao_stop_immediately(instance);
1429 err = ME_ERRNO_SIGNAL;
1430 } else if ((jiffies - j) >= t) {
1431 PERROR("Wait on values timed out.\n");
1432 err = ME_ERRNO_TIMEOUT;
1433 } else { //Uff... all is good. Inform user about empty space.
1434 *count = me_circ_buf_space(&instance->circ_buf);
1438 ME_SUBDEVICE_EXIT;
1440 return err;
1443 static int me6000_ao_io_stream_start(me_subdevice_t * subdevice,
1444 struct file *filep,
1445 int start_mode, int time_out, int flags)
1447 me6000_ao_subdevice_t *instance;
1448 int err = ME_ERRNO_SUCCESS;
1449 unsigned long cpu_flags = 0;
1450 uint32_t status;
1451 uint32_t ctrl;
1452 uint32_t synch;
1453 int count = 0;
1454 int circ_buffer_count;
1456 unsigned long ref;
1457 unsigned long delay = 0;
1459 instance = (me6000_ao_subdevice_t *) subdevice;
1461 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1463 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1464 PERROR("Not a streaming ao.\n");
1465 return ME_ERRNO_NOT_SUPPORTED;
1468 if (flags & ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS) {
1469 PERROR("Invalid flags.\n");
1470 return ME_ERRNO_INVALID_FLAGS;
1473 if (time_out < 0) {
1474 PERROR("Invalid timeout specified.\n");
1475 return ME_ERRNO_INVALID_TIMEOUT;
1478 if ((start_mode != ME_START_MODE_BLOCKING)
1479 && (start_mode != ME_START_MODE_NONBLOCKING)) {
1480 PERROR("Invalid start mode specified.\n");
1481 return ME_ERRNO_INVALID_START_MODE;
1484 if (time_out) {
1485 delay = (time_out * HZ) / 1000;
1486 if (delay == 0)
1487 delay = 1;
1490 switch (instance->status) { //Checking actual mode.
1491 case ao_status_stream_configured:
1492 case ao_status_stream_end:
1493 //Correct modes!
1494 break;
1496 //The device is in wrong mode.
1497 case ao_status_none:
1498 case ao_status_single_configured:
1499 case ao_status_single_run_wait:
1500 case ao_status_single_run:
1501 case ao_status_single_end_wait:
1502 PERROR
1503 ("Subdevice must be preinitialize correctly for streaming.\n");
1504 return ME_ERRNO_PREVIOUS_CONFIG;
1506 case ao_status_stream_fifo_error:
1507 case ao_status_stream_buffer_error:
1508 case ao_status_stream_error:
1509 PDEBUG("Before restart broke stream 'STOP' must be caled.\n");
1510 return ME_STATUS_ERROR;
1512 case ao_status_stream_run_wait:
1513 case ao_status_stream_run:
1514 case ao_status_stream_end_wait:
1515 PDEBUG("Stream is already working.\n");
1516 return ME_ERRNO_SUBDEVICE_BUSY;
1518 default:
1519 instance->status = ao_status_stream_error;
1520 PERROR_CRITICAL("Status is in wrong state!\n");
1521 return ME_ERRNO_INTERNAL;
1525 ME_SUBDEVICE_ENTER;
1527 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1528 instance->circ_buf.tail += instance->preloaded_count;
1529 instance->circ_buf.tail &= instance->circ_buf.mask;
1531 circ_buffer_count = me_circ_buf_values(&instance->circ_buf);
1533 if (!circ_buffer_count && !instance->preloaded_count) { //No values in buffer
1534 ME_SUBDEVICE_EXIT;
1535 PERROR("No values in buffer!\n");
1536 return ME_ERRNO_LACK_OF_RESOURCES;
1539 //Cancel control task
1540 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1541 instance->ao_control_task_flag = 0;
1542 cancel_delayed_work(&instance->ao_control_task);
1544 //Stop device
1545 err = ao_stop_immediately(instance);
1546 if (err) {
1547 PERROR_CRITICAL("FSM IS BUSY!\n");
1548 ME_SUBDEVICE_EXIT;
1550 return ME_ERRNO_SUBDEVICE_BUSY;
1552 //Set values for single_read()
1553 instance->single_value = ME6000_AO_MAX_DATA + 1;
1554 instance->single_value_in_fifo = ME6000_AO_MAX_DATA + 1;
1556 //Setting stop points
1557 if (instance->stop_mode == ME6000_AO_SCAN_STOP_MODE) {
1558 instance->stop_data_count =
1559 instance->stop_count * circ_buffer_count;
1560 } else {
1561 instance->stop_data_count = instance->stop_count;
1564 if ((instance->stop_data_count != 0)
1565 && (instance->stop_data_count < circ_buffer_count)) {
1566 PERROR("More data in buffer than previously set limit!\n");
1569 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1570 ctrl = inl(instance->ctrl_reg);
1571 //Check FIFO
1572 if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO wasn't enabeled. Do it. <= This should be done by user call with ME_WRITE_MODE_PRELOAD
1573 PINFO("Enableing FIFO.\n");
1574 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
1575 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1577 instance->preloaded_count = 0;
1578 instance->data_count = 0;
1579 } else { //Block IRQ
1580 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1582 outl(ctrl, instance->ctrl_reg);
1583 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1584 instance->ctrl_reg - instance->reg_base, ctrl);
1586 //Reset interrupt latch
1587 inl(instance->irq_reset_reg);
1589 //Fill FIFO <= Generaly this should be done by user pre-load call but this is second place to do it.
1590 status = inl(instance->status_reg);
1591 if (!(status & ME6000_AO_STATUS_BIT_EF)) { //FIFO empty
1592 if (instance->stop_data_count != 0) {
1593 count = ME6000_AO_FIFO_COUNT;
1594 } else {
1595 count =
1596 (ME6000_AO_FIFO_COUNT <
1597 instance->
1598 stop_data_count) ? ME6000_AO_FIFO_COUNT :
1599 instance->stop_data_count;
1602 //Copy data
1603 count =
1604 ao_write_data(instance, count, instance->preloaded_count);
1606 if (count < 0) { //This should never happend!
1607 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1608 spin_unlock_irqrestore(&instance->subdevice_lock,
1609 cpu_flags);
1610 ME_SUBDEVICE_EXIT;
1611 return ME_ERRNO_INTERNAL;
1614 //Set pre-load features.
1615 spin_lock(instance->preload_reg_lock);
1616 synch = inl(instance->preload_reg);
1617 synch &=
1618 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
1619 ao_idx);
1620 synch |=
1621 (instance->start_mode & ~ME6000_AO_EXT_TRIG) << instance->ao_idx;
1622 outl(synch, instance->preload_reg);
1623 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1624 instance->preload_reg - instance->reg_base, synch);
1625 spin_unlock(instance->preload_reg_lock);
1627 //Default count is '0'
1628 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1629 instance->preloaded_count = 0;
1630 instance->circ_buf.tail += count;
1631 instance->circ_buf.tail &= instance->circ_buf.mask;
1632 } else { //Wraparound
1633 instance->preloaded_count += count;
1634 instance->data_count += count;
1636 //Special case: Infinite wraparound with less than FIFO datas always should runs in hardware mode.
1637 if ((instance->stop_mode == ME6000_AO_INF_STOP_MODE)
1638 && (circ_buffer_count <= ME6000_AO_FIFO_COUNT)) { //Change to hardware wraparound
1639 PDEBUG
1640 ("Changeing mode from software wraparound to hardware wraparound.\n");
1641 //Copy all data
1642 count =
1643 ao_write_data(instance, circ_buffer_count,
1644 instance->preloaded_count);
1645 ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
1646 ctrl |= ME6000_AO_MODE_WRAPAROUND;
1649 if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) { //Reset position indicator.
1650 instance->preloaded_count = 0;
1651 } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) { //This should never happend!
1652 PERROR_CRITICAL
1653 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1654 spin_unlock_irqrestore(&instance->subdevice_lock,
1655 cpu_flags);
1656 ME_SUBDEVICE_EXIT;
1657 return ME_ERRNO_INTERNAL;
1661 //Set status to 'wait for start'
1662 instance->status = ao_status_stream_run_wait;
1664 status = inl(instance->status_reg);
1665 //Start state machine and interrupts
1666 PINFO("<%s:%d> Start state machine.\n", __func__, __LINE__);
1667 ctrl &= ~(ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
1668 if (instance->start_mode == ME6000_AO_EXT_TRIG) {
1669 PDEBUG("DIGITAL TRIGGER\n");
1670 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
1672 if (!(status & ME6000_AO_STATUS_BIT_HF)) { //More than half!
1673 if ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS) { //Enable IRQ only when hardware_continous is set and FIFO is more than half
1674 PINFO("<%s:%d> Start interrupts.\n", __func__,
1675 __LINE__);
1676 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1679 outl(ctrl, instance->ctrl_reg);
1680 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1681 instance->ctrl_reg - instance->reg_base, ctrl);
1682 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1684 //Trigger output
1685 PINFO("<%s> start mode= 0x%x %s\n", __func__, instance->start_mode,
1686 (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
1687 "");
1688 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Trigger outputs
1689 spin_lock(instance->preload_reg_lock);
1690 synch = inl(instance->preload_reg);
1691 //Add channel to start list
1692 outl(synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx),
1693 instance->preload_reg);
1694 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1695 instance->reg_base,
1696 instance->preload_reg - instance->reg_base,
1697 synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx));
1699 //Fire
1700 PINFO
1701 ("Fired all software synchronous outputs by software trigger.\n");
1702 outl(0x8000, instance->single_reg);
1703 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1704 instance->reg_base,
1705 instance->single_reg - instance->reg_base, 0x8000);
1707 //Restore save settings
1708 outl(synch, instance->preload_reg);
1709 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1710 instance->reg_base,
1711 instance->preload_reg - instance->reg_base, synch);
1712 spin_unlock(instance->preload_reg_lock);
1713 } else if (!instance->start_mode) { //Trigger outputs
1715 spin_lock(instance->preload_reg_lock);
1716 synch = inl(instance->preload_reg);
1717 //Remove channel from start list
1718 outl(synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
1719 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
1721 //Fire
1722 PINFO("Software trigger.\n");
1723 outl(0x8000, instance->single_reg);
1724 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1725 instance->reg_base,
1726 instance->single_reg - instance->reg_base, 0x8000);
1729 //Restore save settings
1730 outl(synch, instance->preload_reg);
1731 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch);
1732 spin_unlock(instance->preload_reg_lock);
1735 // Set control task's timeout
1736 instance->timeout.delay = delay;
1737 instance->timeout.start_time = jiffies;
1739 if (status & ME6000_AO_STATUS_BIT_HF) { //Less than half but not empty!
1740 PINFO("Less than half.\n");
1741 if (instance->stop_data_count == 0) {
1742 count = ME6000_AO_FIFO_COUNT / 2;
1743 } else {
1744 count =
1745 ((ME6000_AO_FIFO_COUNT / 2) <
1746 instance->stop_data_count) ? ME6000_AO_FIFO_COUNT /
1747 2 : instance->stop_data_count;
1750 //Copy data
1751 count =
1752 ao_write_data(instance, count, instance->preloaded_count);
1754 if (count < 0) { //This should never happend!
1755 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1756 ME_SUBDEVICE_EXIT;
1757 return ME_ERRNO_INTERNAL;
1760 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1761 instance->circ_buf.tail += count;
1762 instance->circ_buf.tail &= instance->circ_buf.mask;
1763 } else { //Wraparound
1764 instance->data_count += count;
1765 instance->preloaded_count += count;
1767 if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) { //Reset position indicator.
1768 instance->preloaded_count = 0;
1769 } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) { //This should never happend!
1770 PERROR_CRITICAL
1771 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1772 ME_SUBDEVICE_EXIT;
1773 return ME_ERRNO_INTERNAL;
1777 status = inl(instance->status_reg);
1778 if (!(status & ME6000_AO_STATUS_BIT_HF)) { //More than half!
1779 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1780 PINFO("<%s:%d> Start interrupts.\n", __func__,
1781 __LINE__);
1782 ctrl = inl(instance->ctrl_reg);
1783 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1784 outl(ctrl, instance->ctrl_reg);
1785 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1786 instance->reg_base,
1787 instance->ctrl_reg - instance->reg_base,
1788 ctrl);
1789 spin_unlock_irqrestore(&instance->subdevice_lock,
1790 cpu_flags);
1793 //Special case: Limited wraparound with less than HALF FIFO datas need work around to generate first interrupt.
1794 if ((instance->stop_mode != ME6000_AO_INF_STOP_MODE)
1795 && (instance->mode == ME6000_AO_SW_WRAP_MODE)
1796 && (circ_buffer_count <= (ME6000_AO_FIFO_COUNT / 2))) { //Put more data to FIFO
1797 PINFO("Limited wraparound with less than HALF FIFO datas.\n");
1798 if (instance->preloaded_count) { //This should never happend!
1799 PERROR_CRITICAL
1800 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1801 ME_SUBDEVICE_EXIT;
1802 return ME_ERRNO_INTERNAL;
1805 while (instance->stop_data_count > instance->data_count) { //Maximum data not set jet.
1806 //Copy to buffer
1807 if (circ_buffer_count != ao_write_data(instance, circ_buffer_count, 0)) { //This should never happend!
1808 PERROR_CRITICAL
1809 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1810 ME_SUBDEVICE_EXIT;
1811 return ME_ERRNO_INTERNAL;
1813 instance->data_count += circ_buffer_count;
1815 if (!((status = inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF)) { //FIFO is more than half. Enable IRQ and end copy.
1816 //Reset interrupt latch
1817 inl(instance->irq_reset_reg);
1819 spin_lock_irqsave(&instance->subdevice_lock,
1820 cpu_flags);
1821 PINFO("<%s:%d> Start interrupts.\n",
1822 __func__, __LINE__);
1823 ctrl = inl(instance->ctrl_reg);
1824 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1825 outl(ctrl, instance->ctrl_reg);
1826 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1827 instance->reg_base,
1828 instance->ctrl_reg -
1829 instance->reg_base, ctrl);
1830 spin_unlock_irqrestore(&instance->
1831 subdevice_lock,
1832 cpu_flags);
1833 break;
1837 // Schedule control task
1838 instance->ao_control_task_flag = 1;
1839 queue_delayed_work(instance->me6000_workqueue,
1840 &instance->ao_control_task, 1);
1842 if (start_mode == ME_START_MODE_BLOCKING) { //Wait for start.
1843 ref = jiffies;
1844 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1845 wait_event_interruptible_timeout(instance->wait_queue,
1846 (instance->status !=
1847 ao_status_stream_run_wait),
1848 (delay) ? delay +
1849 1 : LONG_MAX);
1851 if ((instance->status != ao_status_stream_run)
1852 && (instance->status != ao_status_stream_end)) {
1853 PDEBUG("Starting stream canceled. %d\n",
1854 instance->status);
1855 err = ME_ERRNO_CANCELLED;
1858 if (signal_pending(current)) {
1859 PERROR("Wait on start of state machine interrupted.\n");
1860 instance->status = ao_status_none;
1861 ao_stop_immediately(instance);
1862 err = ME_ERRNO_SIGNAL;
1865 if ((delay) && ((jiffies - ref) >= delay)) {
1866 if (instance->status != ao_status_stream_run) {
1867 if (instance->status == ao_status_stream_end) {
1868 PDEBUG("Timeout reached.\n");
1869 } else if ((jiffies - ref) > delay) {
1870 PERROR
1871 ("Timeout reached. Not handled by control task!\n");
1872 ao_stop_immediately(instance);
1873 } else {
1874 PERROR
1875 ("Timeout reached. Signal come but status is strange: %d\n",
1876 instance->status);
1877 ao_stop_immediately(instance);
1880 instance->ao_control_task_flag = 0;
1881 cancel_delayed_work(&instance->ao_control_task);
1882 instance->status = ao_status_stream_end;
1883 err = ME_ERRNO_TIMEOUT;
1888 ME_SUBDEVICE_EXIT;
1889 return err;
1892 static int me6000_ao_io_stream_status(me_subdevice_t * subdevice,
1893 struct file *filep,
1894 int wait,
1895 int *status, int *values, int flags)
1897 me6000_ao_subdevice_t *instance;
1898 int err = ME_ERRNO_SUCCESS;
1900 instance = (me6000_ao_subdevice_t *) subdevice;
1902 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1904 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1905 PERROR("Not a streaming ao.\n");
1906 return ME_ERRNO_NOT_SUPPORTED;
1909 if (flags) {
1910 PERROR("Invalid flag specified.\n");
1911 return ME_ERRNO_INVALID_FLAGS;
1914 if ((wait != ME_WAIT_NONE) && (wait != ME_WAIT_IDLE)) {
1915 PERROR("Invalid wait argument specified.\n");
1916 *status = ME_STATUS_INVALID;
1917 return ME_ERRNO_INVALID_WAIT;
1920 ME_SUBDEVICE_ENTER;
1922 switch (instance->status) {
1923 case ao_status_single_configured:
1924 case ao_status_single_end:
1925 case ao_status_stream_configured:
1926 case ao_status_stream_end:
1927 case ao_status_stream_fifo_error:
1928 case ao_status_stream_buffer_error:
1929 case ao_status_stream_error:
1930 *status = ME_STATUS_IDLE;
1931 break;
1933 case ao_status_single_run_wait:
1934 case ao_status_single_run:
1935 case ao_status_single_end_wait:
1936 case ao_status_stream_run_wait:
1937 case ao_status_stream_run:
1938 case ao_status_stream_end_wait:
1939 *status = ME_STATUS_BUSY;
1940 break;
1942 case ao_status_none:
1943 default:
1944 *status =
1945 (inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM) ?
1946 ME_STATUS_BUSY : ME_STATUS_IDLE;
1947 break;
1950 if ((wait == ME_WAIT_IDLE) && (*status == ME_STATUS_BUSY)) {
1951 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1952 wait_event_interruptible_timeout(instance->wait_queue,
1953 ((instance->status !=
1954 ao_status_single_run_wait)
1955 && (instance->status !=
1956 ao_status_single_run)
1957 && (instance->status !=
1958 ao_status_single_end_wait)
1959 && (instance->status !=
1960 ao_status_stream_run_wait)
1961 && (instance->status !=
1962 ao_status_stream_run)
1963 && (instance->status !=
1964 ao_status_stream_end_wait)),
1965 LONG_MAX);
1967 if (instance->status != ao_status_stream_end) {
1968 PDEBUG("Wait for IDLE canceled. %d\n",
1969 instance->status);
1970 err = ME_ERRNO_CANCELLED;
1973 if (signal_pending(current)) {
1974 PERROR("Wait for IDLE interrupted.\n");
1975 instance->status = ao_status_none;
1976 ao_stop_immediately(instance);
1977 err = ME_ERRNO_SIGNAL;
1980 *status = ME_STATUS_IDLE;
1983 *values = me_circ_buf_space(&instance->circ_buf);
1985 ME_SUBDEVICE_EXIT;
1987 return err;
1990 static int me6000_ao_io_stream_stop(me_subdevice_t * subdevice,
1991 struct file *filep,
1992 int stop_mode, int flags)
1993 { /// @note Stop work and empty buffer and FIFO
1994 int err = ME_ERRNO_SUCCESS;
1995 me6000_ao_subdevice_t *instance;
1996 unsigned long cpu_flags;
1997 volatile uint32_t ctrl;
1999 instance = (me6000_ao_subdevice_t *) subdevice;
2001 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2003 if (flags & ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS) {
2004 PERROR("Invalid flag specified.\n");
2005 return ME_ERRNO_INVALID_FLAGS;
2008 if ((stop_mode != ME_STOP_MODE_IMMEDIATE)
2009 && (stop_mode != ME_STOP_MODE_LAST_VALUE)) {
2010 PERROR("Invalid stop mode specified.\n");
2011 return ME_ERRNO_INVALID_STOP_MODE;
2014 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2015 PERROR("Not a streaming ao.\n");
2016 return ME_ERRNO_NOT_SUPPORTED;
2019 if (instance->status < ao_status_stream_configured) {
2020 //There is nothing to stop!
2021 PERROR("Subdevice not in streaming mode. %d\n",
2022 instance->status);
2023 return ME_ERRNO_PREVIOUS_CONFIG;
2026 ME_SUBDEVICE_ENTER;
2028 //Mark as stopping. => Software stop.
2029 instance->status = ao_status_stream_end_wait;
2031 if (stop_mode == ME_STOP_MODE_IMMEDIATE) { //Stopped now!
2032 err = ao_stop_immediately(instance);
2033 } else if (stop_mode == ME_STOP_MODE_LAST_VALUE) {
2034 ctrl = inl(instance->ctrl_reg) & ME6000_AO_CTRL_MODE_MASK;
2035 if (ctrl == ME6000_AO_MODE_WRAPAROUND) { //Hardware wraparound => Hardware stop.
2036 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2037 ctrl = inl(instance->ctrl_reg);
2038 ctrl |= ME6000_AO_CTRL_BIT_STOP;
2039 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2040 outl(ctrl, instance->ctrl_reg);
2041 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2042 instance->reg_base,
2043 instance->ctrl_reg - instance->reg_base,
2044 ctrl);
2045 spin_unlock_irqrestore(&instance->subdevice_lock,
2046 cpu_flags);
2048 //Reset interrupt latch
2049 inl(instance->irq_reset_reg);
2051 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
2052 wait_event_interruptible_timeout(instance->wait_queue,
2053 (instance->status !=
2054 ao_status_stream_end_wait),
2055 LONG_MAX);
2057 if (instance->status != ao_status_stream_end) {
2058 PDEBUG("Stopping stream canceled.\n");
2059 err = ME_ERRNO_CANCELLED;
2062 if (signal_pending(current)) {
2063 PERROR("Stopping stream interrupted.\n");
2064 instance->status = ao_status_none;
2065 ao_stop_immediately(instance);
2066 err = ME_ERRNO_SIGNAL;
2070 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2071 ctrl = inl(instance->ctrl_reg);
2072 ctrl |= ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2073 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2074 if (!flags) { //Reset FIFO
2075 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2077 outl(ctrl, instance->ctrl_reg);
2078 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
2079 instance->ctrl_reg - instance->reg_base, ctrl);
2080 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2082 //Reset interrupt latch
2083 inl(instance->irq_reset_reg);
2085 if (!flags) { //Reset software buffer
2086 instance->circ_buf.head = 0;
2087 instance->circ_buf.tail = 0;
2088 instance->preloaded_count = 0;
2089 instance->data_count = 0;
2092 ME_SUBDEVICE_EXIT;
2094 return err;
2097 static int me6000_ao_io_stream_write(me_subdevice_t * subdevice,
2098 struct file *filep,
2099 int write_mode,
2100 int *values, int *count, int flags)
2102 int err = ME_ERRNO_SUCCESS;
2103 me6000_ao_subdevice_t *instance;
2104 unsigned long cpu_flags = 0;
2105 uint32_t reg_copy;
2107 int copied_from_user = 0;
2108 int left_to_copy_from_user = *count;
2110 int copied_values;
2112 instance = (me6000_ao_subdevice_t *) subdevice;
2114 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2116 //Checking arguments
2117 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2118 PERROR("Not a streaming ao.\n");
2119 return ME_ERRNO_NOT_SUPPORTED;
2122 if (flags) {
2123 PERROR("Invalid flag specified.\n");
2124 return ME_ERRNO_INVALID_FLAGS;
2127 if (*count <= 0) {
2128 PERROR("Invalid count of values specified.\n");
2129 return ME_ERRNO_INVALID_VALUE_COUNT;
2132 if (values == NULL) {
2133 PERROR("Invalid address of values specified.\n");
2134 return ME_ERRNO_INVALID_POINTER;
2137 if ((instance->status == ao_status_none) || (instance->status == ao_status_single_configured)) { //The device is in single mode.
2138 PERROR
2139 ("Subdevice must be preinitialize correctly for streaming.\n");
2140 return ME_ERRNO_PREVIOUS_CONFIG;
2143 switch (write_mode) {
2144 case ME_WRITE_MODE_PRELOAD:
2146 //Device must be stopped.
2147 if ((instance->status != ao_status_stream_configured)
2148 && (instance->status != ao_status_stream_end)) {
2149 PERROR
2150 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2151 return ME_ERRNO_PREVIOUS_CONFIG;
2153 break;
2154 case ME_WRITE_MODE_NONBLOCKING:
2155 case ME_WRITE_MODE_BLOCKING:
2156 /// @note In blocking mode: When device is not runing and there is not enought space call will blocked up!
2157 /// @note Some other thread must empty buffer by strating engine.
2158 break;
2160 default:
2161 PERROR("Invalid write mode specified.\n");
2162 return ME_ERRNO_INVALID_WRITE_MODE;
2165 if (instance->mode & ME6000_AO_WRAP_MODE) { //Wraparound mode. Device must be stopped.
2166 if ((instance->status != ao_status_stream_configured)
2167 && (instance->status != ao_status_stream_end)) {
2168 PERROR
2169 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2170 return ME_ERRNO_INVALID_WRITE_MODE;
2174 if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2175 && (write_mode != ME_WRITE_MODE_PRELOAD)) {
2177 PERROR("Only 'pre-load' write is acceptable in hardware wraparound mode.\n");
2178 return ME_ERRNO_PREVIOUS_CONFIG;
2180 //This is transparent for user.
2181 PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
2182 write_mode = ME_WRITE_MODE_PRELOAD;
2185 ME_SUBDEVICE_ENTER;
2187 if (write_mode == ME_WRITE_MODE_PRELOAD) { //Init enviroment - preload
2188 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2189 reg_copy = inl(instance->ctrl_reg);
2190 //Check FIFO
2191 if (!(reg_copy & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO not active. Enable it.
2192 reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2193 outl(reg_copy, instance->ctrl_reg);
2194 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2195 instance->reg_base,
2196 instance->ctrl_reg - instance->reg_base,
2197 reg_copy);
2198 instance->preloaded_count = 0;
2200 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2203 while (1) {
2204 //Copy to buffer. This step is common for all modes.
2205 copied_from_user =
2206 ao_get_data_from_user(instance, left_to_copy_from_user,
2207 values + (*count -
2208 left_to_copy_from_user));
2209 left_to_copy_from_user -= copied_from_user;
2211 reg_copy = inl(instance->status_reg);
2212 if ((instance->status == ao_status_stream_run) && !(reg_copy & ME6000_AO_STATUS_BIT_FSM)) { //BROKEN PIPE! The state machine is stoped but logical status show that should be working.
2213 PERROR("Broken pipe in write.\n");
2214 err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
2215 break;
2218 if ((instance->status == ao_status_stream_run) && (instance->mode == ME6000_AO_CONTINOUS) && (reg_copy & ME6000_AO_STATUS_BIT_HF)) { //Continous mode runing and data are below half!
2220 // Block interrupts.
2221 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2222 reg_copy = inl(instance->ctrl_reg);
2223 reg_copy &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2224 outl(reg_copy, instance->ctrl_reg);
2225 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2226 instance->reg_base,
2227 instance->ctrl_reg - instance->reg_base,
2228 reg_copy);
2229 spin_unlock_irqrestore(&instance->subdevice_lock,
2230 cpu_flags);
2232 //Fast copy
2233 copied_values =
2234 ao_write_data(instance, ME6000_AO_FIFO_COUNT / 2,
2236 if (copied_values > 0) {
2237 instance->circ_buf.tail += copied_values;
2238 instance->circ_buf.tail &=
2239 instance->circ_buf.mask;
2240 continue;
2242 //Reset interrupt latch
2243 inl(instance->irq_reset_reg);
2245 // Activate interrupts.
2246 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2247 reg_copy = inl(instance->ctrl_reg);
2248 reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2249 outl(reg_copy, instance->ctrl_reg);
2250 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2251 instance->reg_base,
2252 instance->ctrl_reg - instance->reg_base,
2253 reg_copy);
2254 spin_unlock_irqrestore(&instance->subdevice_lock,
2255 cpu_flags);
2257 if (copied_values == 0) { //This was checked and never should happend!
2258 PERROR_CRITICAL("COPY FINISH WITH 0!\n");
2261 if (copied_values < 0) { //This was checked and never should happend!
2262 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
2263 instance->status = ao_status_stream_fifo_error;
2264 err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2265 break;
2269 if (!left_to_copy_from_user) { //All datas were copied.
2270 break;
2271 } else { //Not all datas were copied.
2272 if (instance->mode & ME6000_AO_WRAP_MODE) { //Error too much datas! Wraparound is limited in size!
2273 PERROR
2274 ("Too much data for wraparound mode! Exceeded size of %ld.\n",
2275 ME6000_AO_CIRC_BUF_COUNT - 1);
2276 err = ME_ERRNO_RING_BUFFER_OVERFLOW;
2277 break;
2280 if (write_mode != ME_WRITE_MODE_BLOCKING) { //Non blocking calls
2281 break;
2284 wait_event_interruptible(instance->wait_queue,
2285 me_circ_buf_space(&instance->
2286 circ_buf));
2288 if (signal_pending(current)) {
2289 PERROR("Writing interrupted by signal.\n");
2290 instance->status = ao_status_none;
2291 ao_stop_immediately(instance);
2292 err = ME_ERRNO_SIGNAL;
2293 break;
2296 if (instance->status == ao_status_none) { //Reset
2297 PERROR("Writing interrupted by reset.\n");
2298 err = ME_ERRNO_CANCELLED;
2299 break;
2304 if (write_mode == ME_WRITE_MODE_PRELOAD) { //Copy data to FIFO - preload
2305 copied_values =
2306 ao_write_data_pooling(instance, ME6000_AO_FIFO_COUNT,
2307 instance->preloaded_count);
2308 instance->preloaded_count += copied_values;
2309 instance->data_count += copied_values;
2311 if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2312 && (me_circ_buf_values(&instance->circ_buf) >
2313 ME6000_AO_FIFO_COUNT)) {
2314 PERROR
2315 ("Too much data for hardware wraparound mode! Exceeded size of %d.\n",
2316 ME6000_AO_FIFO_COUNT);
2317 err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2321 *count = *count - left_to_copy_from_user;
2322 ME_SUBDEVICE_EXIT;
2324 return err;
2327 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
2328 static irqreturn_t me6000_ao_isr(int irq, void *dev_id)
2329 #else
2330 static irqreturn_t me6000_ao_isr(int irq, void *dev_id, struct pt_regs *regs)
2331 #endif
2333 me6000_ao_subdevice_t *instance = dev_id;
2334 uint32_t irq_status;
2335 uint32_t ctrl;
2336 uint32_t status;
2337 int count = 0;
2339 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2341 if (irq != instance->irq) {
2342 PERROR("Incorrect interrupt num: %d.\n", irq);
2343 return IRQ_NONE;
2346 irq_status = inl(instance->irq_status_reg);
2347 if (!(irq_status & (ME6000_IRQ_STATUS_BIT_AO_HF << instance->ao_idx))) {
2348 PINFO("%ld Shared interrupt. %s(): ID=%d: status_reg=0x%04X\n",
2349 jiffies, __func__, instance->ao_idx, irq_status);
2350 return IRQ_NONE;
2353 if (!instance->circ_buf.buf) {
2354 instance->status = ao_status_stream_error;
2355 PERROR_CRITICAL("CIRCULAR BUFFER NOT EXISTS!\n");
2356 //Block interrupts. Stop machine.
2357 ctrl = inl(instance->ctrl_reg);
2358 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2359 ctrl |=
2360 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
2361 outl(ctrl, instance->ctrl_reg);
2362 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2363 instance->reg_base,
2364 instance->ctrl_reg - instance->reg_base, ctrl);
2366 //Inform user
2367 wake_up_interruptible_all(&instance->wait_queue);
2368 return IRQ_HANDLED;
2371 status = inl(instance->status_reg);
2372 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { //Too late. Not working! END? BROKEN PIPE?
2373 /// @note Error checking was moved to separate task.
2374 PDEBUG("Interrupt come but ISM is not working!\n");
2375 //Block interrupts. Stop machine.
2376 ctrl = inl(instance->ctrl_reg);
2377 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2378 ctrl |=
2379 ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2380 outl(ctrl, instance->ctrl_reg);
2381 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2382 instance->reg_base,
2383 instance->ctrl_reg - instance->reg_base, ctrl);
2385 //Reset interrupt latch
2386 inl(instance->irq_reset_reg);
2388 /// @note User notification was also moved to separate task.
2389 return IRQ_HANDLED;
2391 //General procedure. Process more datas.
2393 #ifdef MEDEBUG_DEBUG
2394 if (!me_circ_buf_values(&instance->circ_buf)) { //Buffer is empty!
2395 PDEBUG("Circular buffer empty!\n");
2397 #endif
2399 //Check FIFO
2400 if (status & ME6000_AO_STATUS_BIT_HF) { //OK less than half
2402 //Block interrupts
2403 ctrl = inl(instance->ctrl_reg);
2404 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2405 outl(ctrl, instance->ctrl_reg);
2406 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2407 instance->reg_base,
2408 instance->ctrl_reg - instance->reg_base, ctrl);
2410 do {
2411 //Calculate how many should be copied.
2412 count =
2413 (instance->stop_data_count) ? instance->
2414 stop_data_count -
2415 instance->data_count : ME6000_AO_FIFO_COUNT / 2;
2416 if (ME6000_AO_FIFO_COUNT / 2 < count) {
2417 count = ME6000_AO_FIFO_COUNT / 2;
2419 //Copy data
2420 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
2421 count = ao_write_data(instance, count, 0);
2422 if (count > 0) {
2423 instance->circ_buf.tail += count;
2424 instance->circ_buf.tail &=
2425 instance->circ_buf.mask;
2426 instance->data_count += count;
2428 if ((instance->status == ao_status_stream_end_wait) && !me_circ_buf_values(&instance->circ_buf)) { //Stoping. Whole buffer was copied.
2429 break;
2432 } else if ((instance->mode == ME6000_AO_SW_WRAP_MODE) && ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS)) { //Wraparound (software)
2433 if (instance->status == ao_status_stream_end_wait) { //We stoping => Copy to the end of the buffer.
2434 count =
2435 ao_write_data(instance, count, 0);
2436 } else { //Copy in wraparound mode.
2437 count =
2438 ao_write_data_wraparound(instance,
2439 count,
2440 instance->
2441 preloaded_count);
2444 if (count > 0) {
2445 instance->data_count += count;
2446 instance->preloaded_count += count;
2447 instance->preloaded_count %=
2448 me_circ_buf_values(&instance->
2449 circ_buf);
2451 if ((instance->status == ao_status_stream_end_wait) && !instance->preloaded_count) { //Stoping. Whole buffer was copied.
2452 break;
2457 if ((count <= 0) || (instance->stop_data_count && (instance->stop_data_count <= instance->data_count))) { //End of work.
2458 break;
2460 } //Repeat if still is under half fifo
2461 while ((status =
2462 inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF);
2464 //Unblock interrupts
2465 ctrl = inl(instance->ctrl_reg);
2466 if (count >= 0) { //Copy was successful.
2467 if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. No more interrupts.
2468 PDEBUG("Finishing work. Interrupt disabled.\n");
2469 instance->status = ao_status_stream_end_wait;
2470 } else if (count > 0) { //Normal work. Enable interrupt.
2471 PDEBUG("Normal work. Enable interrupt.\n");
2472 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2473 } else { //Normal work but there are no more data in buffer. Interrupt blocked. stream_write() will unblock it.
2474 PDEBUG
2475 ("No data in software buffer. Interrupt blocked.\n");
2477 } else { //Error during copy.
2478 instance->status = ao_status_stream_fifo_error;
2481 outl(ctrl, instance->ctrl_reg);
2482 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2483 instance->reg_base,
2484 instance->ctrl_reg - instance->reg_base, ctrl);
2485 } else { //?? more than half
2486 PDEBUG
2487 ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
2490 PINFO("ISR: Buffer count: %d.(T:%d H:%d)\n",
2491 me_circ_buf_values(&instance->circ_buf), instance->circ_buf.tail,
2492 instance->circ_buf.head);
2493 PINFO("ISR: Stop count: %d.\n", instance->stop_count);
2494 PINFO("ISR: Stop data count: %d.\n", instance->stop_data_count);
2495 PINFO("ISR: Data count: %d.\n", instance->data_count);
2497 //Reset interrupt latch
2498 inl(instance->irq_reset_reg);
2500 //Inform user
2501 wake_up_interruptible_all(&instance->wait_queue);
2503 return IRQ_HANDLED;
2506 static void me6000_ao_destructor(struct me_subdevice *subdevice)
2508 me6000_ao_subdevice_t *instance;
2510 instance = (me6000_ao_subdevice_t *) subdevice;
2512 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2514 instance->ao_control_task_flag = 0;
2516 // Reset subdevice to asure clean exit.
2517 me6000_ao_io_reset_subdevice(subdevice, NULL,
2518 ME_IO_RESET_SUBDEVICE_NO_FLAGS);
2520 // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
2521 if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
2522 set_current_state(TASK_INTERRUPTIBLE);
2523 schedule_timeout(2);
2526 if (instance->fifo & ME6000_AO_HAS_FIFO) {
2527 if (instance->irq) {
2528 free_irq(instance->irq, instance);
2529 instance->irq = 0;
2532 if (instance->circ_buf.buf) {
2533 PDEBUG("free circ_buf = %p size=%d",
2534 instance->circ_buf.buf,
2535 PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2536 free_pages((unsigned long)instance->circ_buf.buf,
2537 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2539 instance->circ_buf.buf = NULL;
2542 me_subdevice_deinit(&instance->base);
2543 kfree(instance);
2546 me6000_ao_subdevice_t *me6000_ao_constructor(uint32_t reg_base,
2547 spinlock_t * preload_reg_lock,
2548 uint32_t * preload_flags,
2549 uint32_t * triggering_flags,
2550 int ao_idx,
2551 int fifo,
2552 int irq,
2553 int high_range,
2554 struct workqueue_struct *me6000_wq)
2556 me6000_ao_subdevice_t *subdevice;
2557 int err;
2559 PDEBUG("executed ID=%d.\n", ao_idx);
2561 /* Allocate memory for subdevice instance */
2562 subdevice = kmalloc(sizeof(me6000_ao_subdevice_t), GFP_KERNEL);
2564 if (!subdevice) {
2565 PERROR("Cannot get memory for subdevice instance.\n");
2566 return NULL;
2569 memset(subdevice, 0, sizeof(me6000_ao_subdevice_t));
2571 /* Initialize subdevice base class */
2572 err = me_subdevice_init(&subdevice->base);
2574 if (err) {
2575 PERROR("Cannot initialize subdevice base class instance.\n");
2576 kfree(subdevice);
2577 return NULL;
2579 // Initialize spin locks.
2580 spin_lock_init(&subdevice->subdevice_lock);
2582 subdevice->preload_reg_lock = preload_reg_lock;
2583 subdevice->preload_flags = preload_flags;
2584 subdevice->triggering_flags = triggering_flags;
2586 /* Store analog output index */
2587 subdevice->ao_idx = ao_idx;
2589 /* Store if analog output has fifo */
2590 subdevice->fifo = fifo;
2592 if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2593 /* Allocate and initialize circular buffer */
2594 subdevice->circ_buf.mask = ME6000_AO_CIRC_BUF_COUNT - 1;
2595 subdevice->circ_buf.buf =
2596 (void *)__get_free_pages(GFP_KERNEL,
2597 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2598 PDEBUG("circ_buf = %p size=%ld\n", subdevice->circ_buf.buf,
2599 ME6000_AO_CIRC_BUF_SIZE);
2601 if (!subdevice->circ_buf.buf) {
2602 PERROR
2603 ("Cannot initialize subdevice base class instance.\n");
2604 kfree(subdevice);
2605 return NULL;
2608 memset(subdevice->circ_buf.buf, 0, ME6000_AO_CIRC_BUF_SIZE);
2609 } else {
2610 subdevice->circ_buf.mask = 0;
2611 subdevice->circ_buf.buf = NULL;
2613 subdevice->circ_buf.head = 0;
2614 subdevice->circ_buf.tail = 0;
2616 subdevice->status = ao_status_none;
2617 subdevice->ao_control_task_flag = 0;
2618 subdevice->timeout.delay = 0;
2619 subdevice->timeout.start_time = jiffies;
2621 /* Initialize wait queue */
2622 init_waitqueue_head(&subdevice->wait_queue);
2624 /* Initialize single value to 0V */
2625 subdevice->single_value = 0x8000;
2626 subdevice->single_value_in_fifo = 0x8000;
2628 /* Initialize range boarders */
2629 if (high_range) {
2630 subdevice->min = ME6000_AO_MIN_RANGE_HIGH;
2631 subdevice->max = ME6000_AO_MAX_RANGE_HIGH;
2632 } else {
2633 subdevice->min = ME6000_AO_MIN_RANGE;
2634 subdevice->max = ME6000_AO_MAX_RANGE;
2637 /* Register interrupt service routine */
2639 if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2640 subdevice->irq = irq;
2641 if (request_irq(subdevice->irq, me6000_ao_isr,
2642 #ifdef IRQF_DISABLED
2643 IRQF_DISABLED | IRQF_SHARED,
2644 #else
2645 SA_INTERRUPT | SA_SHIRQ,
2646 #endif
2647 ME6000_NAME, subdevice)) {
2648 PERROR("Cannot get interrupt line.\n");
2649 PDEBUG("free circ_buf = %p size=%d",
2650 subdevice->circ_buf.buf,
2651 PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2652 free_pages((unsigned long)subdevice->circ_buf.buf,
2653 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2654 subdevice->circ_buf.buf = NULL;
2655 kfree(subdevice);
2656 return NULL;
2658 PINFO("Registered irq=%d.\n", subdevice->irq);
2659 } else {
2660 subdevice->irq = 0;
2663 /* Initialize registers */
2664 // Only streamed subdevices support interrupts. For the rest this register has no meaning.
2665 subdevice->irq_status_reg = reg_base + ME6000_AO_IRQ_STATUS_REG;
2666 subdevice->preload_reg = reg_base + ME6000_AO_PRELOAD_REG;
2668 if (ao_idx == 0) {
2669 subdevice->ctrl_reg = reg_base + ME6000_AO_00_CTRL_REG;
2670 subdevice->status_reg = reg_base + ME6000_AO_00_STATUS_REG;
2671 subdevice->fifo_reg = reg_base + ME6000_AO_00_FIFO_REG;
2672 subdevice->timer_reg = reg_base + ME6000_AO_00_TIMER_REG;
2673 subdevice->irq_reset_reg =
2674 reg_base + ME6000_AO_00_IRQ_RESET_REG;
2675 subdevice->single_reg = reg_base + ME6000_AO_00_SINGLE_REG;
2676 } else if (ao_idx == 1) {
2677 subdevice->ctrl_reg = reg_base + ME6000_AO_01_CTRL_REG;
2678 subdevice->status_reg = reg_base + ME6000_AO_01_STATUS_REG;
2679 subdevice->fifo_reg = reg_base + ME6000_AO_01_FIFO_REG;
2680 subdevice->timer_reg = reg_base + ME6000_AO_01_TIMER_REG;
2681 subdevice->irq_reset_reg =
2682 reg_base + ME6000_AO_01_IRQ_RESET_REG;
2683 subdevice->single_reg = reg_base + ME6000_AO_01_SINGLE_REG;
2684 } else if (ao_idx == 2) {
2685 subdevice->ctrl_reg = reg_base + ME6000_AO_02_CTRL_REG;
2686 subdevice->status_reg = reg_base + ME6000_AO_02_STATUS_REG;
2687 subdevice->fifo_reg = reg_base + ME6000_AO_02_FIFO_REG;
2688 subdevice->timer_reg = reg_base + ME6000_AO_02_TIMER_REG;
2689 subdevice->irq_reset_reg =
2690 reg_base + ME6000_AO_02_IRQ_RESET_REG;
2691 subdevice->single_reg = reg_base + ME6000_AO_02_SINGLE_REG;
2692 } else if (ao_idx == 3) {
2693 subdevice->ctrl_reg = reg_base + ME6000_AO_03_CTRL_REG;
2694 subdevice->status_reg = reg_base + ME6000_AO_03_STATUS_REG;
2695 subdevice->fifo_reg = reg_base + ME6000_AO_03_FIFO_REG;
2696 subdevice->timer_reg = reg_base + ME6000_AO_03_TIMER_REG;
2697 subdevice->irq_reset_reg =
2698 reg_base + ME6000_AO_03_IRQ_RESET_REG;
2699 subdevice->single_reg = reg_base + ME6000_AO_03_SINGLE_REG;
2700 } else {
2701 subdevice->ctrl_reg = reg_base + ME6000_AO_DUMY;
2702 subdevice->fifo_reg = reg_base + ME6000_AO_DUMY;
2703 subdevice->timer_reg = reg_base + ME6000_AO_DUMY;
2704 subdevice->irq_reset_reg = reg_base + ME6000_AO_DUMY;
2705 subdevice->single_reg = reg_base + ME6000_AO_DUMY;
2707 subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
2708 if (ao_idx == 4) {
2709 subdevice->single_reg =
2710 reg_base + ME6000_AO_04_SINGLE_REG;
2711 } else if (ao_idx == 5) {
2712 subdevice->single_reg =
2713 reg_base + ME6000_AO_05_SINGLE_REG;
2714 } else if (ao_idx == 6) {
2715 subdevice->single_reg =
2716 reg_base + ME6000_AO_06_SINGLE_REG;
2717 } else if (ao_idx == 7) {
2718 subdevice->single_reg =
2719 reg_base + ME6000_AO_07_SINGLE_REG;
2720 } else if (ao_idx == 8) {
2721 subdevice->single_reg =
2722 reg_base + ME6000_AO_08_SINGLE_REG;
2723 } else if (ao_idx == 9) {
2724 subdevice->single_reg =
2725 reg_base + ME6000_AO_09_SINGLE_REG;
2726 } else if (ao_idx == 10) {
2727 subdevice->single_reg =
2728 reg_base + ME6000_AO_10_SINGLE_REG;
2729 } else if (ao_idx == 11) {
2730 subdevice->single_reg =
2731 reg_base + ME6000_AO_11_SINGLE_REG;
2732 } else if (ao_idx == 12) {
2733 subdevice->single_reg =
2734 reg_base + ME6000_AO_12_SINGLE_REG;
2735 } else if (ao_idx == 13) {
2736 subdevice->single_reg =
2737 reg_base + ME6000_AO_13_SINGLE_REG;
2738 } else if (ao_idx == 14) {
2739 subdevice->single_reg =
2740 reg_base + ME6000_AO_14_SINGLE_REG;
2741 } else if (ao_idx == 15) {
2742 subdevice->single_reg =
2743 reg_base + ME6000_AO_15_SINGLE_REG;
2744 } else {
2745 PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx);
2746 me_subdevice_deinit((me_subdevice_t *) subdevice);
2747 if (subdevice->fifo) {
2748 free_pages((unsigned long)subdevice->circ_buf.
2749 buf, ME6000_AO_CIRC_BUF_SIZE_ORDER);
2751 subdevice->circ_buf.buf = NULL;
2752 kfree(subdevice);
2753 return NULL;
2756 #ifdef MEDEBUG_DEBUG_REG
2757 subdevice->reg_base = reg_base;
2758 #endif
2760 /* Override base class methods. */
2761 subdevice->base.me_subdevice_destructor = me6000_ao_destructor;
2762 subdevice->base.me_subdevice_io_reset_subdevice =
2763 me6000_ao_io_reset_subdevice;
2764 subdevice->base.me_subdevice_io_single_config =
2765 me6000_ao_io_single_config;
2766 subdevice->base.me_subdevice_io_single_read = me6000_ao_io_single_read;
2767 subdevice->base.me_subdevice_io_single_write =
2768 me6000_ao_io_single_write;
2769 subdevice->base.me_subdevice_io_stream_config =
2770 me6000_ao_io_stream_config;
2771 subdevice->base.me_subdevice_io_stream_new_values =
2772 me6000_ao_io_stream_new_values;
2773 subdevice->base.me_subdevice_io_stream_write =
2774 me6000_ao_io_stream_write;
2775 subdevice->base.me_subdevice_io_stream_start =
2776 me6000_ao_io_stream_start;
2777 subdevice->base.me_subdevice_io_stream_status =
2778 me6000_ao_io_stream_status;
2779 subdevice->base.me_subdevice_io_stream_stop = me6000_ao_io_stream_stop;
2780 subdevice->base.me_subdevice_query_number_channels =
2781 me6000_ao_query_number_channels;
2782 subdevice->base.me_subdevice_query_subdevice_type =
2783 me6000_ao_query_subdevice_type;
2784 subdevice->base.me_subdevice_query_subdevice_caps =
2785 me6000_ao_query_subdevice_caps;
2786 subdevice->base.me_subdevice_query_subdevice_caps_args =
2787 me6000_ao_query_subdevice_caps_args;
2788 subdevice->base.me_subdevice_query_range_by_min_max =
2789 me6000_ao_query_range_by_min_max;
2790 subdevice->base.me_subdevice_query_number_ranges =
2791 me6000_ao_query_number_ranges;
2792 subdevice->base.me_subdevice_query_range_info =
2793 me6000_ao_query_range_info;
2794 subdevice->base.me_subdevice_query_timer = me6000_ao_query_timer;
2796 //prepare work queue and work function
2797 subdevice->me6000_workqueue = me6000_wq;
2799 /* workqueue API changed in kernel 2.6.20 */
2800 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2801 INIT_WORK(&subdevice->ao_control_task, me6000_ao_work_control_task,
2802 (void *)subdevice);
2803 #else
2804 INIT_DELAYED_WORK(&subdevice->ao_control_task,
2805 me6000_ao_work_control_task);
2806 #endif
2808 if (subdevice->fifo) { //Set speed
2809 outl(ME6000_AO_MIN_CHAN_TICKS - 1, subdevice->timer_reg);
2810 subdevice->hardware_stop_delay = HZ / 10; //100ms
2813 return subdevice;
2816 /** @brief Stop presentation. Preserve FIFOs.
2818 * @param instance The subdevice instance (pointer).
2820 int inline ao_stop_immediately(me6000_ao_subdevice_t * instance)
2822 unsigned long cpu_flags;
2823 uint32_t ctrl;
2824 int timeout;
2825 int i;
2826 uint32_t single_mask;
2828 if (instance->ao_idx < ME6000_AO_SINGLE_STATUS_OFFSET)
2829 single_mask = 0x0000;
2830 else
2831 single_mask = 0x0001 << (instance->ao_idx -
2832 ME6000_AO_SINGLE_STATUS_OFFSET);
2834 timeout =
2835 (instance->hardware_stop_delay >
2836 (HZ / 10)) ? instance->hardware_stop_delay : HZ / 10;
2837 for (i = 0; i <= timeout; i++) {
2838 if (instance->fifo) {
2839 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2840 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2841 ctrl = inl(instance->ctrl_reg);
2842 ctrl |=
2843 ME6000_AO_CTRL_BIT_STOP |
2844 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2845 ctrl &=
2846 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
2847 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
2848 outl(ctrl, instance->ctrl_reg);
2849 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2850 instance->reg_base,
2851 instance->ctrl_reg - instance->reg_base,
2852 ctrl);
2853 spin_unlock_irqrestore(&instance->subdevice_lock,
2854 cpu_flags);
2856 if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) { // Exit.
2857 break;
2859 } else {
2860 if (!(inl(instance->status_reg) & single_mask)) { // Exit.
2861 break;
2865 PINFO("<%s> Wait for stop: %d\n", __func__, i);
2867 //Still working!
2868 set_current_state(TASK_INTERRUPTIBLE);
2869 schedule_timeout(1);
2872 if (i > timeout) {
2873 PERROR_CRITICAL("FSM IS BUSY!\n");
2874 return ME_ERRNO_INTERNAL;
2876 return ME_ERRNO_SUCCESS;
2879 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2880 * @note This is time critical function. Checking is done at begining and end only.
2881 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2883 * @param instance The subdevice instance (pointer).
2884 * @param count Maximum number of copied data.
2885 * @param start_pos Position of the firs value in buffer.
2887 * @return On success: Number of copied data.
2888 * @return On error/success: 0. No datas were copied => no data in buffer.
2889 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2891 int inline ao_write_data_wraparound(me6000_ao_subdevice_t * instance, int count,
2892 int start_pos)
2893 { /// @note This is time critical function!
2894 uint32_t status;
2895 uint32_t value;
2896 int pos =
2897 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2898 int local_count = count;
2899 int i = 1;
2901 if (count <= 0) { //Wrong count!
2902 return 0;
2905 while (i < local_count) {
2906 //Get value from buffer
2907 value = *(instance->circ_buf.buf + pos);
2908 //Prepare it
2909 if (instance->ao_idx & 0x1) {
2910 value <<= 16;
2912 //Put value to FIFO
2913 outl(value, instance->fifo_reg);
2914 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2916 pos++;
2917 pos &= instance->circ_buf.mask;
2918 if (pos == instance->circ_buf.head) {
2919 pos = instance->circ_buf.tail;
2921 i++;
2924 status = inl(instance->status_reg);
2925 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full before all datas were copied!
2926 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2927 instance->ao_idx);
2928 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2929 } else { //Add last value
2930 value = *(instance->circ_buf.buf + pos);
2931 if (instance->ao_idx & 0x1) {
2932 value <<= 16;
2934 //Put value to FIFO
2935 outl(value, instance->fifo_reg);
2936 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2939 PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
2940 local_count);
2941 return local_count;
2944 /** @brief Copy data from software buffer to fifo (fast).
2945 * @note This is time critical function. Checking is done at begining and end only.
2946 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2948 * @param instance The subdevice instance (pointer).
2949 * @param count Maximum number of copied data.
2950 * @param start_pos Position of the firs value in buffer.
2952 * @return On success: Number of copied data.
2953 * @return On error/success: 0. No datas were copied => no data in buffer.
2954 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2956 int inline ao_write_data(me6000_ao_subdevice_t * instance, int count,
2957 int start_pos)
2958 { /// @note This is time critical function!
2959 uint32_t status;
2960 uint32_t value;
2961 int pos =
2962 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2963 int local_count = count;
2964 int max_count;
2965 int i = 1;
2967 if (count <= 0) { //Wrong count!
2968 return 0;
2971 max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
2972 if (max_count <= 0) { //No data to copy!
2973 return 0;
2976 if (max_count < count) {
2977 local_count = max_count;
2980 while (i < local_count) {
2981 //Get value from buffer
2982 value = *(instance->circ_buf.buf + pos);
2983 //Prepare it
2984 if (instance->ao_idx & 0x1) {
2985 value <<= 16;
2987 //Put value to FIFO
2988 outl(value, instance->fifo_reg);
2989 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2991 pos++;
2992 pos &= instance->circ_buf.mask;
2993 i++;
2996 status = inl(instance->status_reg);
2997 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full before all datas were copied!
2998 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2999 instance->ao_idx);
3000 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
3001 } else { //Add last value
3002 value = *(instance->circ_buf.buf + pos);
3003 if (instance->ao_idx & 0x1) {
3004 value <<= 16;
3006 //Put value to FIFO
3007 outl(value, instance->fifo_reg);
3008 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
3011 PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
3012 return local_count;
3015 /** @brief Copy data from software buffer to fifo (slow).
3016 * @note This is slow function that copy all data from buffer to FIFO with full control.
3018 * @param instance The subdevice instance (pointer).
3019 * @param count Maximum number of copied data.
3020 * @param start_pos Position of the firs value in buffer.
3022 * @return On success: Number of copied values.
3023 * @return On error/success: 0. FIFO was full at begining.
3024 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
3026 int inline ao_write_data_pooling(me6000_ao_subdevice_t * instance, int count,
3027 int start_pos)
3028 { /// @note This is slow function!
3029 uint32_t status;
3030 uint32_t value;
3031 int pos =
3032 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
3033 int local_count = count;
3034 int i;
3035 int max_count;
3037 if (count <= 0) { //Wrong count!
3038 PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
3039 return 0;
3042 max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
3043 if (max_count <= 0) { //No data to copy!
3044 PERROR("idx=%d SLOW LOADED: No data to copy!\n",
3045 instance->ao_idx);
3046 return 0;
3049 if (max_count < count) {
3050 local_count = max_count;
3053 for (i = 0; i < local_count; i++) {
3054 status = inl(instance->status_reg);
3055 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full!
3056 return i;
3058 //Get value from buffer
3059 value = *(instance->circ_buf.buf + pos);
3060 //Prepare it
3061 if (instance->ao_idx & 0x1) {
3062 value <<= 16;
3064 //Put value to FIFO
3065 outl(value, instance->fifo_reg);
3066 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
3068 pos++;
3069 pos &= instance->circ_buf.mask;
3072 PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
3073 return local_count;
3076 /** @brief Copy data from user space to circular buffer.
3077 * @param instance The subdevice instance (pointer).
3078 * @param count Number of datas in user space.
3079 * @param user_values Buffer's pointer.
3081 * @return On success: Number of copied values.
3082 * @return On error: -ME_ERRNO_INTERNAL.
3084 int inline ao_get_data_from_user(me6000_ao_subdevice_t * instance, int count,
3085 int *user_values)
3087 int i, err;
3088 int empty_space;
3089 int copied;
3090 int value;
3092 empty_space = me_circ_buf_space(&instance->circ_buf);
3093 //We have only this space free.
3094 copied = (count < empty_space) ? count : empty_space;
3095 for (i = 0; i < copied; i++) { //Copy from user to buffer
3096 if ((err = get_user(value, (int *)(user_values + i)))) {
3097 PERROR
3098 ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
3099 instance->ao_idx, user_values + i, err);
3100 return -ME_ERRNO_INTERNAL;
3102 /// @note The analog output in me6000 series has size of 16 bits.
3103 *(instance->circ_buf.buf + instance->circ_buf.head) =
3104 (uint16_t) value;
3105 instance->circ_buf.head++;
3106 instance->circ_buf.head &= instance->circ_buf.mask;
3109 PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
3110 return copied;
3113 static void me6000_ao_work_control_task(
3114 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
3115 void *subdevice
3116 #else
3117 struct work_struct *work
3118 #endif
3121 me6000_ao_subdevice_t *instance;
3122 unsigned long cpu_flags = 0;
3123 uint32_t status;
3124 uint32_t ctrl;
3125 uint32_t synch;
3126 int reschedule = 0;
3127 int signaling = 0;
3128 uint32_t single_mask;
3130 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
3131 instance = (me6000_ao_subdevice_t *) subdevice;
3132 #else
3133 instance =
3134 container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
3135 #endif
3136 PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
3137 instance->ao_idx);
3139 status = inl(instance->status_reg);
3140 PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
3141 instance->status_reg - instance->reg_base, status);
3143 /// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
3144 // single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
3145 single_mask = *instance->triggering_flags & (0x1 << instance->ao_idx);
3147 switch (instance->status) { // Checking actual mode.
3149 // Not configured for work.
3150 case ao_status_none:
3151 break;
3153 //This are stable modes. No need to do anything. (?)
3154 case ao_status_single_configured:
3155 case ao_status_stream_configured:
3156 case ao_status_stream_fifo_error:
3157 case ao_status_stream_buffer_error:
3158 case ao_status_stream_error:
3159 PERROR("Shouldn't be running!.\n");
3160 break;
3162 // Single modes
3163 case ao_status_single_run_wait:
3164 case ao_status_single_run:
3165 case ao_status_single_end_wait:
3166 if (instance->fifo) { // Extra registers.
3167 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working.
3168 if (((instance->fifo & ME6000_AO_HAS_FIFO)
3169 && (!(status & ME6000_AO_STATUS_BIT_EF)))
3170 || (!(instance->fifo & ME6000_AO_HAS_FIFO))) { // Single is in end state.
3171 PDEBUG
3172 ("Single call has been complited.\n");
3174 // Set correct value for single_read();
3175 instance->single_value =
3176 instance->single_value_in_fifo;
3178 // Set status as 'ao_status_single_end'
3179 instance->status = ao_status_single_end;
3181 spin_lock(instance->preload_reg_lock);
3182 if ((single_mask) && (*instance->preload_flags & (ME6000_AO_SYNC_HOLD << instance->ao_idx))) { // This is one of synchronous start channels. Set all as triggered.
3183 *instance->triggering_flags =
3184 0x00000000;
3185 } else {
3186 //Set this channel as triggered (none active).
3187 *instance->triggering_flags &=
3188 ~(0x1 << instance->ao_idx);
3190 spin_unlock(instance->preload_reg_lock);
3192 // Signal the end.
3193 signaling = 1;
3194 // Wait for stop ISM.
3195 reschedule = 1;
3197 break;
3200 // Check timeout.
3201 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3202 PDEBUG("Timeout reached.\n");
3203 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3204 spin_lock_irqsave(&instance->subdevice_lock,
3205 cpu_flags);
3206 ctrl = inl(instance->ctrl_reg);
3207 ctrl |=
3208 ME6000_AO_CTRL_BIT_STOP |
3209 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3210 ctrl &=
3211 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3212 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3213 ctrl &=
3214 ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
3215 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
3216 //Disabling FIFO
3217 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
3219 outl(ctrl, instance->ctrl_reg);
3220 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3221 instance->reg_base,
3222 instance->ctrl_reg -
3223 instance->reg_base, ctrl);
3224 spin_unlock_irqrestore(&instance->
3225 subdevice_lock,
3226 cpu_flags);
3228 //Reset interrupt latch
3229 inl(instance->irq_reset_reg);
3231 spin_lock(instance->preload_reg_lock);
3232 //Remove from synchronous start. Block triggering from this output.
3233 synch = inl(instance->preload_reg);
3234 synch &=
3235 ~((ME6000_AO_SYNC_HOLD |
3236 ME6000_AO_SYNC_EXT_TRIG) << instance->
3237 ao_idx);
3238 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO - set to single safe mode
3239 synch |=
3240 ME6000_AO_SYNC_HOLD << instance->
3241 ao_idx;
3243 outl(synch, instance->preload_reg);
3244 PDEBUG_REG
3245 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3246 instance->reg_base,
3247 instance->preload_reg - instance->reg_base,
3248 synch);
3249 //Set this channel as triggered (none active).
3250 *instance->triggering_flags &=
3251 ~(0x1 << instance->ao_idx);
3252 spin_unlock(instance->preload_reg_lock);
3254 // Set correct value for single_read();
3255 instance->single_value_in_fifo =
3256 instance->single_value;
3258 instance->status = ao_status_single_end;
3260 // Signal the end.
3261 signaling = 1;
3263 } else { // No extra registers.
3265 if (!(status & single_mask))
3266 {// State machine is not working.
3267 PDEBUG("Single call has been complited.\n");
3269 // Set correct value for single_read();
3270 instance->single_value = instance->single_value_in_fifo;
3272 // Set status as 'ao_status_single_end'
3273 instance->status = ao_status_single_end;
3275 // Signal the end.
3276 signaling = 1;
3277 // Wait for stop ISM.
3278 reschedule = 1;
3280 break;
3283 if (!single_mask) { // Was triggered.
3284 PDEBUG("Single call has been complited.\n");
3286 // Set correct value for single_read();
3287 instance->single_value =
3288 instance->single_value_in_fifo;
3290 // Set status as 'ao_status_single_end'
3291 instance->status = ao_status_single_end;
3293 // Signal the end.
3294 signaling = 1;
3296 break;
3298 // Check timeout.
3299 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3300 PDEBUG("Timeout reached.\n");
3302 spin_lock(instance->preload_reg_lock);
3303 //Remove from synchronous start. Block triggering from this output.
3304 synch = inl(instance->preload_reg);
3305 synch &=
3306 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
3307 ao_idx);
3308 synch |=
3309 ME6000_AO_SYNC_HOLD << instance->ao_idx;
3311 outl(synch, instance->preload_reg);
3312 PDEBUG_REG
3313 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3314 instance->reg_base,
3315 instance->preload_reg - instance->reg_base,
3316 synch);
3317 //Set this channel as triggered (none active).
3318 *instance->triggering_flags &=
3319 ~(0x1 << instance->ao_idx);
3320 spin_unlock(instance->preload_reg_lock);
3322 // Restore old settings.
3323 PDEBUG("Write old value back to register.\n");
3324 outl(instance->single_value,
3325 instance->single_reg);
3326 PDEBUG_REG
3327 ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3328 instance->reg_base,
3329 instance->single_reg - instance->reg_base,
3330 instance->single_value);
3332 // Set correct value for single_read();
3333 instance->single_value_in_fifo =
3334 instance->single_value;
3336 instance->status = ao_status_single_end;
3338 // Signal the end.
3339 signaling = 1;
3343 // Wait for stop.
3344 reschedule = 1;
3345 break;
3347 case ao_status_stream_end:
3348 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3349 PERROR_CRITICAL
3350 ("Streaming on single device! This feature is not implemented in this version!\n");
3351 instance->status = ao_status_stream_error;
3352 // Signal the end.
3353 signaling = 1;
3354 break;
3356 case ao_status_single_end:
3357 if (instance->fifo) { // Extra registers.
3358 if (status & ME6000_AO_STATUS_BIT_FSM) { // State machine is working but the status is set to end. Force stop.
3360 // Wait for stop.
3361 reschedule = 1;
3364 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3365 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3366 ctrl = inl(instance->ctrl_reg);
3367 ctrl |=
3368 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
3369 ME6000_AO_CTRL_BIT_STOP;
3370 ctrl &=
3371 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3372 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3373 outl(ctrl, instance->ctrl_reg);
3374 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3375 instance->reg_base,
3376 instance->ctrl_reg - instance->reg_base,
3377 ctrl);
3378 spin_unlock_irqrestore(&instance->subdevice_lock,
3379 cpu_flags);
3381 //Reset interrupt latch
3382 inl(instance->irq_reset_reg);
3383 } else { // No extra registers.
3385 if (status & single_mask)
3386 {// State machine is working but the status is set to end. Force stop.
3388 // Wait for stop.
3389 reschedule = 1;
3393 break;
3395 // Stream modes
3396 case ao_status_stream_run_wait:
3397 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3398 PERROR_CRITICAL
3399 ("Streaming on single device! This feature is not implemented in this version!\n");
3400 instance->status = ao_status_stream_error;
3401 // Signal the end.
3402 signaling = 1;
3403 break;
3406 if (status & ME6000_AO_STATUS_BIT_FSM) { // State machine is working. Waiting for start finish.
3407 instance->status = ao_status_stream_run;
3409 // Signal end of this step
3410 signaling = 1;
3411 } else { // State machine is not working.
3412 if (!(status & ME6000_AO_STATUS_BIT_EF)) { // FIFO is empty. Procedure has started and finish already!
3413 instance->status = ao_status_stream_end;
3415 // Signal the end.
3416 signaling = 1;
3417 // Wait for stop.
3418 reschedule = 1;
3419 break;
3423 // Check timeout.
3424 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3425 PDEBUG("Timeout reached.\n");
3426 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3427 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3428 ctrl = inl(instance->ctrl_reg);
3429 ctrl |=
3430 ME6000_AO_CTRL_BIT_STOP |
3431 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3432 ctrl &=
3433 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3434 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3435 outl(ctrl, instance->ctrl_reg);
3436 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3437 instance->reg_base,
3438 instance->ctrl_reg - instance->reg_base,
3439 ctrl);
3440 spin_unlock_irqrestore(&instance->subdevice_lock,
3441 cpu_flags);
3443 //Reset interrupt latch
3444 inl(instance->irq_reset_reg);
3446 spin_lock(instance->preload_reg_lock);
3447 //Remove from synchronous start. Block triggering from this output.
3448 synch = inl(instance->preload_reg);
3449 synch &=
3450 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
3451 instance->ao_idx);
3452 outl(synch, instance->preload_reg);
3453 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3454 instance->reg_base,
3455 instance->preload_reg - instance->reg_base,
3456 synch);
3457 spin_unlock(instance->preload_reg_lock);
3459 instance->status = ao_status_stream_end;
3461 // Signal the end.
3462 signaling = 1;
3464 // Wait for stop.
3465 reschedule = 1;
3466 break;
3468 case ao_status_stream_run:
3469 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3470 PERROR_CRITICAL
3471 ("Streaming on single device! This feature is not implemented in this version!\n");
3472 instance->status = ao_status_stream_error;
3473 // Signal the end.
3474 signaling = 1;
3475 break;
3478 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working. This is an error.
3479 // BROKEN PIPE!
3480 if (!(status & ME6000_AO_STATUS_BIT_EF)) { // FIFO is empty.
3481 if (me_circ_buf_values(&instance->circ_buf)) { // Software buffer is not empty.
3482 if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. Requed data shown.
3483 PDEBUG
3484 ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3485 instance->status =
3486 ao_status_stream_end;
3487 } else {
3488 PERROR
3489 ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3490 instance->status =
3491 ao_status_stream_buffer_error;
3493 } else { // Software buffer is empty.
3494 PDEBUG
3495 ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3496 instance->status = ao_status_stream_end;
3498 } else { // There are still datas in FIFO.
3499 if (me_circ_buf_values(&instance->circ_buf)) { // Software buffer is not empty.
3500 PERROR
3501 ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3502 } else { // Software buffer is empty.
3503 PERROR
3504 ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3506 instance->status = ao_status_stream_fifo_error;
3510 // Signal the failure.
3511 signaling = 1;
3512 break;
3514 // Wait for stop.
3515 reschedule = 1;
3516 break;
3518 case ao_status_stream_end_wait:
3519 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3520 PERROR_CRITICAL
3521 ("Streaming on single device! This feature is not implemented in this version!\n");
3522 instance->status = ao_status_stream_error;
3523 // Signal the end.
3524 signaling = 1;
3525 break;
3528 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working. Waiting for stop finish.
3529 instance->status = ao_status_stream_end;
3530 signaling = 1;
3532 // State machine is working.
3533 reschedule = 1;
3534 break;
3536 default:
3537 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3538 instance->status);
3539 instance->status = ao_status_stream_error;
3540 // Signal the end.
3541 signaling = 1;
3542 break;
3546 if (signaling) { //Signal it.
3547 wake_up_interruptible_all(&instance->wait_queue);
3550 if (instance->ao_control_task_flag && reschedule) { // Reschedule task
3551 queue_delayed_work(instance->me6000_workqueue,
3552 &instance->ao_control_task, 1);
3553 } else {
3554 PINFO("<%s> Ending control task.\n", __func__);
3559 static int me6000_ao_query_range_by_min_max(me_subdevice_t * subdevice,
3560 int unit,
3561 int *min,
3562 int *max, int *maxdata, int *range)
3564 me6000_ao_subdevice_t *instance;
3566 instance = (me6000_ao_subdevice_t *) subdevice;
3568 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3570 if ((*max - *min) < 0) {
3571 PERROR("Invalid minimum and maximum values specified.\n");
3572 return ME_ERRNO_INVALID_MIN_MAX;
3575 if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3576 if ((*max <= (instance->max + 1000)) && (*min >= instance->min)) {
3577 *min = instance->min;
3578 *max = instance->max;
3579 *maxdata = ME6000_AO_MAX_DATA;
3580 *range = 0;
3581 } else {
3582 PERROR("No matching range available.\n");
3583 return ME_ERRNO_NO_RANGE;
3585 } else {
3586 PERROR("Invalid physical unit specified.\n");
3587 return ME_ERRNO_INVALID_UNIT;
3590 return ME_ERRNO_SUCCESS;
3593 static int me6000_ao_query_number_ranges(me_subdevice_t * subdevice,
3594 int unit, int *count)
3596 me6000_ao_subdevice_t *instance;
3598 instance = (me6000_ao_subdevice_t *) subdevice;
3600 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3602 if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3603 *count = 1;
3604 } else {
3605 *count = 0;
3608 return ME_ERRNO_SUCCESS;
3611 static int me6000_ao_query_range_info(me_subdevice_t * subdevice,
3612 int range,
3613 int *unit,
3614 int *min, int *max, int *maxdata)
3616 me6000_ao_subdevice_t *instance;
3618 instance = (me6000_ao_subdevice_t *) subdevice;
3620 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3622 if (range == 0) {
3623 *unit = ME_UNIT_VOLT;
3624 *min = instance->min;
3625 *max = instance->max;
3626 *maxdata = ME6000_AO_MAX_DATA;
3627 } else {
3628 PERROR("Invalid range number specified.\n");
3629 return ME_ERRNO_INVALID_RANGE;
3632 return ME_ERRNO_SUCCESS;
3635 static int me6000_ao_query_timer(me_subdevice_t * subdevice,
3636 int timer,
3637 int *base_frequency,
3638 long long *min_ticks, long long *max_ticks)
3640 me6000_ao_subdevice_t *instance;
3642 instance = (me6000_ao_subdevice_t *) subdevice;
3644 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3646 if (instance->fifo) { //Streaming device.
3647 *base_frequency = ME6000_AO_BASE_FREQUENCY;
3648 if (timer == ME_TIMER_ACQ_START) {
3649 *min_ticks = ME6000_AO_MIN_ACQ_TICKS;
3650 *max_ticks = ME6000_AO_MAX_ACQ_TICKS;
3651 } else if (timer == ME_TIMER_CONV_START) {
3652 *min_ticks = ME6000_AO_MIN_CHAN_TICKS;
3653 *max_ticks = ME6000_AO_MAX_CHAN_TICKS;
3655 } else { //Not streaming device!
3656 *base_frequency = 0;
3657 *min_ticks = 0;
3658 *max_ticks = 0;
3661 return ME_ERRNO_SUCCESS;
3664 static int me6000_ao_query_number_channels(me_subdevice_t * subdevice,
3665 int *number)
3667 me6000_ao_subdevice_t *instance;
3668 instance = (me6000_ao_subdevice_t *) subdevice;
3670 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3672 *number = 1;
3673 return ME_ERRNO_SUCCESS;
3676 static int me6000_ao_query_subdevice_type(me_subdevice_t * subdevice,
3677 int *type, int *subtype)
3679 me6000_ao_subdevice_t *instance;
3681 instance = (me6000_ao_subdevice_t *) subdevice;
3683 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3685 *type = ME_TYPE_AO;
3686 *subtype =
3687 (instance->
3688 fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
3689 ME_SUBTYPE_SINGLE;
3691 return ME_ERRNO_SUCCESS;
3694 static int me6000_ao_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
3696 me6000_ao_subdevice_t *instance;
3697 instance = (me6000_ao_subdevice_t *) subdevice;
3699 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3701 *caps =
3702 ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
3703 ME_CAPS_NONE);
3705 return ME_ERRNO_SUCCESS;
3708 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
3709 int cap, int *args, int count)
3711 me6000_ao_subdevice_t *instance;
3712 int err = ME_ERRNO_SUCCESS;
3714 instance = (me6000_ao_subdevice_t *) subdevice;
3716 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3718 if (count != 1) {
3719 PERROR("Invalid capability argument count.\n");
3720 return ME_ERRNO_INVALID_CAP_ARG_COUNT;
3723 switch (cap) {
3724 case ME_CAP_AI_FIFO_SIZE:
3725 args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
3726 break;
3728 case ME_CAP_AI_BUFFER_SIZE:
3729 args[0] =
3730 (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
3731 break;
3733 default:
3734 PERROR("Invalid capability.\n");
3735 err = ME_ERRNO_INVALID_CAP;
3736 args[0] = 0;
3739 return err;