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)
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.
34 #include <linux/version.h>
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
40 #include <linux/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"
53 #include "me6000_reg.h"
54 #include "me6000_ao_reg.h"
55 #include "me6000_ao.h"
60 static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
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,
71 int *min, int *max, int *maxdata);
73 static int me6000_ao_query_timer(me_subdevice_t *subdevice,
76 long long *min_ticks, long long *max_ticks);
78 static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
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,
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,
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,
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,
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,
121 meIOStreamConfig_t *config_list,
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,
129 int time_out, int *count, int flags);
131 /** Start streaming. */
132 static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
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,
140 int *status, int *values, int flags);
142 /** Stop streaming. */
143 static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
145 int stop_mode, int flags);
147 /** Write datas to buffor. */
148 static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
151 int *values, int *count, int flags);
153 /** Interrupt handler. Copy from buffer to FIFO. */
154 static irqreturn_t me6000_ao_isr(int irq, void *dev_id);
156 /** Copy data from circular buffer to fifo (fast) in wraparound mode. */
157 inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
160 /** Copy data from circular buffer to fifo (fast).*/
161 inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
164 /** Copy data from circular buffer to fifo (slow).*/
165 inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
168 /** Copy data from user space to circular buffer. */
169 inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
172 /** Stop presentation. Preserve FIFOs. */
173 inline int ao_stop_immediately(me6000_ao_subdevice_t *instance);
175 /** Function for checking timeout in non-blocking mode. */
176 static void me6000_ao_work_control_task(struct work_struct *work);
181 static int me6000_ao_io_reset_subdevice(me_subdevice_t *subdevice,
182 struct file *filep, int flags)
184 me6000_ao_subdevice_t *instance;
185 int err = ME_ERRNO_SUCCESS;
189 instance = (me6000_ao_subdevice_t *) subdevice;
191 PDEBUG("executed. idx=%d\n", instance->ao_idx);
194 PERROR("Invalid flag specified.\n");
195 return ME_ERRNO_INVALID_FLAGS;
200 instance->status = ao_status_none;
201 instance->ao_control_task_flag = 0;
202 cancel_delayed_work(&instance->ao_control_task);
203 instance->timeout.delay = 0;
204 instance->timeout.start_time = jiffies;
206 //Stop state machine.
207 err = ao_stop_immediately(instance);
209 //Remove from synchronous start.
210 spin_lock(instance->preload_reg_lock);
211 tmp = inl(instance->preload_reg);
213 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
215 outl(tmp, instance->preload_reg);
216 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
217 instance->preload_reg - instance->reg_base, tmp);
218 *instance->preload_flags &=
219 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
222 //Reset triggering flag
223 *instance->triggering_flags &= ~(0x1 << instance->ao_idx);
224 spin_unlock(instance->preload_reg_lock);
226 if (instance->fifo) {
227 //Set single mode, dissable FIFO, dissable external trigger, block interrupt.
228 ctrl = ME6000_AO_MODE_SINGLE;
232 (ME6000_AO_CTRL_BIT_STOP |
233 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
235 outl(ctrl, instance->ctrl_reg);
236 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
238 instance->ctrl_reg - instance->reg_base, ctrl);
240 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
241 //Reset interrupt latch
242 inl(instance->irq_reset_reg);
245 instance->hardware_stop_delay = HZ / 10; //100ms
248 outl(0x8000, instance->single_reg);
249 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
250 instance->single_reg - instance->reg_base, 0x8000);
252 instance->circ_buf.head = 0;
253 instance->circ_buf.tail = 0;
254 instance->preloaded_count = 0;
255 instance->data_count = 0;
256 instance->single_value = 0x8000;
257 instance->single_value_in_fifo = 0x8000;
259 //Set status to signal that device is unconfigured.
260 instance->status = ao_status_none;
261 //Signal reset if user is on wait.
262 wake_up_interruptible_all(&instance->wait_queue);
269 static int me6000_ao_io_single_config(me_subdevice_t *subdevice,
275 int trig_type, int trig_edge, int flags)
277 me6000_ao_subdevice_t *instance;
278 int err = ME_ERRNO_SUCCESS;
281 unsigned long cpu_flags;
283 instance = (me6000_ao_subdevice_t *) subdevice;
285 PDEBUG("executed. ID=%d\n", instance->ao_idx);
287 // Checking parameters
290 ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
291 return ME_ERRNO_INVALID_FLAGS;
294 if (instance->fifo) { //Stream hardware (with or without fifo)
295 if ((trig_edge == ME_TRIG_TYPE_SW)
296 && (trig_edge != ME_TRIG_EDGE_NONE)) {
298 ("Invalid trigger edge. Software trigger has not edge.\n");
299 return ME_ERRNO_INVALID_TRIG_EDGE;
302 if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
304 case ME_TRIG_EDGE_ANY:
305 case ME_TRIG_EDGE_RISING:
306 case ME_TRIG_EDGE_FALLING:
310 PERROR("Invalid trigger edge.\n");
311 return ME_ERRNO_INVALID_TRIG_EDGE;
315 if ((trig_type != ME_TRIG_TYPE_SW)
316 && (trig_type != ME_TRIG_TYPE_EXT_DIGITAL)) {
318 ("Invalid trigger type. Trigger must be software or digital.\n");
319 return ME_ERRNO_INVALID_TRIG_TYPE;
322 if (trig_edge != ME_TRIG_EDGE_NONE) {
324 ("Invalid trigger edge. Single output trigger hasn't own edge.\n");
325 return ME_ERRNO_INVALID_TRIG_EDGE;
328 if (trig_type != ME_TRIG_TYPE_SW) {
330 ("Invalid trigger type. Trigger must be software.\n");
331 return ME_ERRNO_INVALID_TRIG_TYPE;
336 if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
337 && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
338 PERROR("Invalid trigger channel specified.\n");
339 return ME_ERRNO_INVALID_TRIG_CHAN;
342 if ((trig_type == ME_TRIG_TYPE_EXT_DIGITAL) && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS))
344 PERROR("Invalid trigger channel specified. Must be synchronous when digital is choose.\n");
345 return ME_ERRNO_INVALID_TRIG_CHAN;
348 if (ref != ME_REF_AO_GROUND) {
350 ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
351 return ME_ERRNO_INVALID_REF;
354 if (single_config != 0) {
356 ("Invalid single config specified. Only one range for anlog outputs is available.\n");
357 return ME_ERRNO_INVALID_SINGLE_CONFIG;
362 ("Invalid channel number specified. Analog output have only one channel.\n");
363 return ME_ERRNO_INVALID_CHANNEL;
368 //Subdevice running in stream mode!
369 if ((instance->status >= ao_status_stream_run_wait)
370 && (instance->status < ao_status_stream_end)) {
371 PERROR("Subdevice is busy.\n");
374 return ME_ERRNO_SUBDEVICE_BUSY;
376 /// @note For single all calls (config and write) are erasing previous state!
378 instance->status = ao_status_none;
380 // Correct single mirrors
381 instance->single_value_in_fifo = instance->single_value;
384 err = ao_stop_immediately(instance);
386 PERROR_CRITICAL("FSM IS BUSY!\n");
389 return ME_ERRNO_SUBDEVICE_BUSY;
392 if (instance->fifo) { // Set control register.
393 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
394 // Set stop bit. Stop streaming mode (If running.).
395 ctrl = inl(instance->ctrl_reg);
398 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
399 if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
400 PINFO("External digital trigger.\n");
402 if (trig_edge == ME_TRIG_EDGE_ANY) {
403 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
405 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
406 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
407 } else if (trig_edge == ME_TRIG_EDGE_FALLING) {
408 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
410 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
411 } else if (trig_edge == ME_TRIG_EDGE_RISING) {
412 instance->ctrl_trg = 0x0;
414 } else if (trig_type == ME_TRIG_TYPE_SW) {
415 PDEBUG("SOFTWARE TRIGGER\n");
416 instance->ctrl_trg = 0x0;
418 outl(ctrl, instance->ctrl_reg);
419 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
421 instance->ctrl_reg - instance->reg_base, ctrl);
422 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
424 PDEBUG("SOFTWARE TRIGGER\n");
427 // Set preload/synchronization register.
428 spin_lock(instance->preload_reg_lock);
430 if (trig_type == ME_TRIG_TYPE_SW) {
431 *instance->preload_flags &=
432 ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
433 } else //if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL)
435 *instance->preload_flags |=
436 ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
439 if (trig_chan == ME_TRIG_CHAN_DEFAULT) {
440 *instance->preload_flags &=
441 ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
442 } else //if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS)
444 *instance->preload_flags |=
445 ME6000_AO_SYNC_HOLD << instance->ao_idx;
448 //Reset hardware register
449 sync = inl(instance->preload_reg);
450 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
451 instance->preload_reg - instance->reg_base, sync);
452 sync &= ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
453 sync |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
455 //Output configured in default mode (safe one)
456 outl(sync, instance->preload_reg);
457 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
458 instance->preload_reg - instance->reg_base, sync);
459 spin_unlock(instance->preload_reg_lock);
461 instance->status = ao_status_single_configured;
468 static int me6000_ao_io_single_read(me_subdevice_t *subdevice,
471 int *value, int time_out, int flags)
473 me6000_ao_subdevice_t *instance;
474 int err = ME_ERRNO_SUCCESS;
477 unsigned long delay = 0;
479 instance = (me6000_ao_subdevice_t *) subdevice;
481 PDEBUG("executed. idx=%d\n", instance->ao_idx);
483 if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
484 PERROR("Invalid flag specified. %d\n", flags);
485 return ME_ERRNO_INVALID_FLAGS;
488 if ((instance->status >= ao_status_stream_configured)
489 && (instance->status <= ao_status_stream_end)) {
490 PERROR("Subdevice not configured to work in single mode!\n");
491 return ME_ERRNO_PREVIOUS_CONFIG;
495 PERROR("Invalid channel number specified.\n");
496 return ME_ERRNO_INVALID_CHANNEL;
500 PERROR("Invalid timeout specified.\n");
501 return ME_ERRNO_INVALID_TIMEOUT;
505 if ((!flags) && (instance->status == ao_status_single_run_wait)) { //Blocking mode. Wait for trigger.
507 delay = (time_out * HZ) / 1000;
514 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
515 wait_event_interruptible_timeout(instance->wait_queue,
517 ao_status_single_run_wait),
518 (delay) ? delay : LONG_MAX);
520 if (instance->status == ao_status_none) {
521 PDEBUG("Single canceled.\n");
522 err = ME_ERRNO_CANCELLED;
525 if (signal_pending(current)) {
526 PERROR("Wait on start of state machine interrupted.\n");
527 instance->status = ao_status_none;
528 ao_stop_immediately(instance);
529 err = ME_ERRNO_SIGNAL;
532 if ((delay) && ((jiffies - j) >= delay)) {
533 PDEBUG("Timeout reached.\n");
534 err = ME_ERRNO_TIMEOUT;
538 (!err) ? instance->single_value_in_fifo : instance->
540 } else { //Non-blocking mode
542 *value = instance->single_value;
550 static int me6000_ao_io_single_write(me_subdevice_t *subdevice,
553 int value, int time_out, int flags)
555 me6000_ao_subdevice_t *instance;
556 int err = ME_ERRNO_SUCCESS;
557 unsigned long cpu_flags;
559 unsigned long delay = 0;
566 /// Workaround for mix-mode - begin
569 /// Workaround for mix-mode - end
571 instance = (me6000_ao_subdevice_t *) subdevice;
573 PDEBUG("executed. idx=%d\n", instance->ao_idx);
576 ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
577 ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
578 PERROR("Invalid flag specified.\n");
579 return ME_ERRNO_INVALID_FLAGS;
582 if ((instance->status == ao_status_none)
583 || (instance->status > ao_status_single_end)) {
584 PERROR("Subdevice not configured to work in single mode!\n");
585 return ME_ERRNO_PREVIOUS_CONFIG;
589 PERROR("Invalid channel number specified.\n");
590 return ME_ERRNO_INVALID_CHANNEL;
593 if (value & ~ME6000_AO_MAX_DATA) {
594 PERROR("Invalid value provided.\n");
595 return ME_ERRNO_VALUE_OUT_OF_RANGE;
599 PERROR("Invalid timeout specified.\n");
600 return ME_ERRNO_INVALID_TIMEOUT;
605 /// @note For single all calls (config and write) are erasing previous state!
607 //Cancel control task
608 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
609 instance->ao_control_task_flag = 0;
610 cancel_delayed_work(&instance->ao_control_task);
612 // Correct single mirrors
613 instance->single_value_in_fifo = instance->single_value;
616 err = ao_stop_immediately(instance);
618 PERROR_CRITICAL("FSM IS BUSY!\n");
621 return ME_ERRNO_SUBDEVICE_BUSY;
625 delay = (time_out * HZ) / 1000;
631 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
633 instance->single_value_in_fifo = value;
635 if (instance->fifo) {
636 ctrl = inl(instance->ctrl_reg);
639 if (instance->fifo & ME6000_AO_HAS_FIFO) { /// Workaround for mix-mode - begin
641 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
642 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
644 instance->timer_reg - instance->reg_base,
645 (int)ME6000_AO_MIN_CHAN_TICKS);
646 instance->hardware_stop_delay = HZ / 10; //100ms
648 status = inl(instance->status_reg);
650 //Set the continous mode.
651 ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
652 ctrl |= ME6000_AO_MODE_CONTINUOUS;
655 if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO wasn't enabeled. Do it.
656 PINFO("Enableing FIFO.\n");
657 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
658 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
659 } else { //Check if FIFO is empty
660 if (status & ME6000_AO_STATUS_BIT_EF) { //FIFO not empty
661 PINFO("Reseting FIFO.\n");
663 ~(ME6000_AO_CTRL_BIT_ENABLE_FIFO |
664 ME6000_AO_CTRL_BIT_ENABLE_IRQ);
665 outl(ctrl, instance->ctrl_reg);
666 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
669 instance->reg_base, ctrl);
671 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
672 } else { //FIFO empty, only interrupt needs to be disabled!
673 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
677 outl(ctrl, instance->ctrl_reg);
678 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
680 instance->ctrl_reg - instance->reg_base, ctrl);
682 //Reset interrupt latch
683 inl(instance->irq_reset_reg);
685 //Write output - 1 value to FIFO
686 if (instance->ao_idx & 0x1) {
687 outl(value <<= 16, instance->fifo_reg);
688 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
690 instance->fifo_reg - instance->reg_base,
693 outl(value, instance->fifo_reg);
694 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
696 instance->fifo_reg - instance->reg_base,
699 /// Workaround for mix-mode - end
700 } else { //No FIFO - always in single mode
702 PDEBUG("Write value\n");
703 outl(value, instance->single_reg);
704 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
706 instance->single_reg - instance->reg_base, value);
709 mode = *instance->preload_flags >> instance->ao_idx;
710 mode &= (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG);
712 PINFO("Triggering mode: 0x%08x\n", mode);
714 spin_lock(instance->preload_reg_lock);
715 sync_mask = inl(instance->preload_reg);
716 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
717 instance->preload_reg - instance->reg_base, sync_mask);
719 case 0: //0x00000000: Individual software
720 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
722 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
723 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
724 if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) { //Now we can set correct mode.
726 ~((ME6000_AO_SYNC_EXT_TRIG |
727 ME6000_AO_SYNC_HOLD) << instance->
730 outl(sync_mask, instance->preload_reg);
732 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
734 instance->preload_reg - instance->reg_base,
737 } else { // No FIFO - Single mode: In this case resetting 'ME6000_AO_SYNC_HOLD' will trigger output.
738 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.
740 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
743 ME6000_AO_SYNC_HOLD << instance->ao_idx;
745 outl(sync_mask, instance->preload_reg);
747 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
749 instance->preload_reg - instance->reg_base,
753 instance->single_value = value;
756 case ME6000_AO_SYNC_EXT_TRIG: //0x00010000: Individual hardware
757 PDEBUG("DIGITAL TRIGGER\n");
758 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
760 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
761 if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) { //Now we can set correct mode.
763 ~((ME6000_AO_SYNC_EXT_TRIG |
764 ME6000_AO_SYNC_HOLD) << instance->
767 outl(sync_mask, instance->preload_reg);
769 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
771 instance->preload_reg - instance->reg_base,
774 } else { // No FIFO - Single mode
776 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
777 instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {
778 //Now we can set correct mode
780 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
783 ME6000_AO_SYNC_HOLD << instance->ao_idx;
785 outl(sync_mask, instance->preload_reg);
787 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
789 instance->preload_reg - instance->reg_base,
795 case ME6000_AO_SYNC_HOLD: //0x00000001: Synchronous software
796 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
799 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
800 instance->ao_idx)) !=
801 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
802 //Now we can set correct mode
804 ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
805 sync_mask |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
806 outl(sync_mask, instance->preload_reg);
807 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
809 instance->preload_reg - instance->reg_base,
812 //Set triggering flag
813 *instance->triggering_flags |= 0x1 << instance->ao_idx;
816 case (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG): //0x00010001: Synchronous hardware
817 PDEBUG("DIGITAL TRIGGER\n");
818 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
821 ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
822 instance->ao_idx)) !=
823 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
824 //Now we can set correct mode
826 (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
828 outl(sync_mask, instance->preload_reg);
829 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
831 instance->preload_reg - instance->reg_base,
834 //Set triggering flag
835 *instance->triggering_flags |= 0x1 << instance->ao_idx;
838 // spin_unlock(instance->preload_reg_lock); // Moved down.
840 if (instance->fifo) { //Activate ISM (remove 'stop' bits)
842 ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
843 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
844 ctrl |= instance->ctrl_trg;
846 ~(ME6000_AO_CTRL_BIT_STOP |
847 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
849 outl(ctrl, instance->ctrl_reg);
850 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
852 instance->ctrl_reg - instance->reg_base, ctrl);
854 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
856 /// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!
858 PINFO("<%s> start mode= 0x%08x %s\n", __func__, mode,
859 (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
861 if (instance->fifo & ME6000_AO_HAS_FIFO) { // FIFO - Continous mode
862 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Trigger outputs
863 //Add channel to start list
865 (ME6000_AO_SYNC_HOLD << instance->ao_idx),
866 instance->preload_reg);
867 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
869 instance->preload_reg - instance->reg_base,
870 sync_mask | (ME6000_AO_SYNC_HOLD <<
875 ("Fired all software synchronous outputs by software trigger.\n");
876 outl(0x8000, instance->single_reg);
877 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
879 instance->single_reg - instance->reg_base,
882 //Restore save settings
883 outl(sync_mask, instance->preload_reg);
884 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
886 instance->preload_reg - instance->reg_base,
889 } else if (!mode) { //Trigger outputs
890 /* //Remove channel from start list
891 outl(sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
892 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));
895 PINFO("Software trigger.\n");
896 outl(0x8000, instance->single_reg);
897 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
899 instance->single_reg - instance->reg_base,
902 /* //Restore save settings
903 outl(sync_mask, instance->preload_reg);
904 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask);
907 /// @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.
908 /// @note Because triggering is not working it can not be add to synchronous list. First 4 channels don't need this information, anyway.
909 *instance->triggering_flags &= 0xFFFFFFF0;
910 } else { // No FIFO - Single mode
911 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Fired all software synchronous outputs.
912 tmp = ~(*instance->preload_flags | 0xFFFF0000);
914 ("Fired all software synchronous outputs. mask:0x%08x\n",
916 tmp |= sync_mask & 0xFFFF0000;
917 // Add this channel to list
918 tmp &= ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
921 PINFO("Software trigger.\n");
922 outl(tmp, instance->preload_reg);
923 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
925 instance->preload_reg - instance->reg_base,
928 //Restore save settings
929 outl(sync_mask, instance->preload_reg);
930 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
932 instance->preload_reg - instance->reg_base,
935 //Set all as triggered.
936 *instance->triggering_flags = 0x0;
937 } else if (!mode) { // Add this channel to list
939 ~(ME6000_AO_SYNC_HOLD << instance->ao_idx),
940 instance->preload_reg);
941 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
943 instance->preload_reg - instance->reg_base,
944 sync_mask & ~(ME6000_AO_SYNC_HOLD <<
948 PINFO("Software trigger.\n");
950 //Restore save settings
951 outl(sync_mask, instance->preload_reg);
952 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
954 instance->preload_reg - instance->reg_base,
957 //Set all as triggered.
958 *instance->triggering_flags = 0x0;
962 spin_unlock(instance->preload_reg_lock);
964 instance->status = ao_status_single_run_wait;
966 instance->timeout.delay = delay;
967 instance->timeout.start_time = jiffies;
968 instance->ao_control_task_flag = 1;
969 queue_delayed_work(instance->me6000_workqueue,
970 &instance->ao_control_task, 1);
972 if (!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
975 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
976 wait_event_interruptible_timeout(instance->wait_queue,
978 ao_status_single_run_wait),
982 if (instance->status != ao_status_single_end) {
983 PDEBUG("Single canceled.\n");
984 err = ME_ERRNO_CANCELLED;
987 if (signal_pending(current)) {
988 PERROR("Wait on start of state machine interrupted.\n");
989 instance->ao_control_task_flag = 0;
990 cancel_delayed_work(&instance->ao_control_task);
991 ao_stop_immediately(instance);
992 instance->status = ao_status_none;
993 err = ME_ERRNO_SIGNAL;
996 if ((delay) && ((jiffies - j) >= delay)) {
997 if (instance->status == ao_status_single_end) {
998 PDEBUG("Timeout reached.\n");
999 } else if ((jiffies - j) > delay) {
1001 ("Timeout reached. Not handled by control task!\n");
1002 ao_stop_immediately(instance);
1005 ("Timeout reached. Signal come but status is strange: %d\n",
1007 ao_stop_immediately(instance);
1010 instance->ao_control_task_flag = 0;
1011 cancel_delayed_work(&instance->ao_control_task);
1012 instance->status = ao_status_single_end;
1013 err = ME_ERRNO_TIMEOUT;
1022 static int me6000_ao_io_stream_config(me_subdevice_t *subdevice,
1024 meIOStreamConfig_t *config_list,
1026 meIOStreamTrigger_t *trigger,
1027 int fifo_irq_threshold, int flags)
1029 me6000_ao_subdevice_t *instance;
1030 int err = ME_ERRNO_SUCCESS;
1032 unsigned long cpu_flags;
1033 uint64_t conv_ticks;
1034 unsigned int conv_start_ticks_low = trigger->iConvStartTicksLow;
1035 unsigned int conv_start_ticks_high = trigger->iConvStartTicksHigh;
1037 instance = (me6000_ao_subdevice_t *) subdevice;
1039 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1041 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1042 PERROR("Not a streaming ao.\n");
1043 return ME_ERRNO_NOT_SUPPORTED;
1047 (uint64_t) conv_start_ticks_low +
1048 ((uint64_t) conv_start_ticks_high << 32);
1051 ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY |
1052 ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1053 PERROR("Invalid flags.\n");
1054 return ME_ERRNO_INVALID_FLAGS;
1057 if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {
1058 if (!(flags & ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1060 ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
1061 return ME_ERRNO_INVALID_FLAGS;
1064 if ((trigger->iAcqStopTrigType != ME_TRIG_TYPE_NONE)
1065 || (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE)) {
1067 ("Hardware wraparound mode must be in infinite mode.\n");
1068 return ME_ERRNO_INVALID_FLAGS;
1073 PERROR("Only 1 entry in config list acceptable.\n");
1074 return ME_ERRNO_INVALID_CONFIG_LIST_COUNT;
1077 if (config_list[0].iChannel != 0) {
1078 PERROR("Invalid channel number specified.\n");
1079 return ME_ERRNO_INVALID_CHANNEL;
1082 if (config_list[0].iStreamConfig != 0) {
1083 PERROR("Only one range available.\n");
1084 return ME_ERRNO_INVALID_STREAM_CONFIG;
1087 if (config_list[0].iRef != ME_REF_AO_GROUND) {
1088 PERROR("Output is referenced to ground.\n");
1089 return ME_ERRNO_INVALID_REF;
1092 if ((trigger->iAcqStartTicksLow != 0)
1093 || (trigger->iAcqStartTicksHigh != 0)) {
1095 ("Invalid acquisition start trigger argument specified.\n");
1096 return ME_ERRNO_INVALID_ACQ_START_ARG;
1099 if (config_list[0].iFlags) {
1100 PERROR("Invalid config list flag.\n");
1101 return ME_ERRNO_INVALID_FLAGS;
1104 if ((trigger->iAcqStartTrigType != ME_TRIG_TYPE_SW)
1105 && (trigger->iAcqStartTrigType != ME_TRIG_TYPE_EXT_DIGITAL)) {
1106 PERROR("Invalid acquisition start trigger type specified.\n");
1107 return ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE;
1110 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {
1111 switch (trigger->iAcqStartTrigEdge) {
1112 case ME_TRIG_EDGE_RISING:
1113 case ME_TRIG_EDGE_FALLING:
1114 case ME_TRIG_EDGE_ANY:
1119 ("Invalid acquisition start trigger edge specified.\n");
1120 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1124 if ((trigger->iAcqStartTrigType == ME_TRIG_TYPE_SW)
1125 && (trigger->iAcqStartTrigEdge != ME_TRIG_TYPE_NONE)) {
1126 PERROR("Invalid acquisition start trigger edge specified.\n");
1127 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1130 if (trigger->iScanStartTrigType != ME_TRIG_TYPE_FOLLOW) {
1131 PERROR("Invalid scan start trigger type specified.\n");
1132 return ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE;
1135 if (trigger->iConvStartTrigType != ME_TRIG_TYPE_TIMER) {
1136 PERROR("Invalid conv start trigger type specified.\n");
1137 return ME_ERRNO_INVALID_CONV_START_TRIG_TYPE;
1140 if ((conv_ticks < ME6000_AO_MIN_CHAN_TICKS)
1141 || (conv_ticks > ME6000_AO_MAX_CHAN_TICKS)) {
1142 PERROR("Invalid conv start trigger argument specified.\n");
1143 return ME_ERRNO_INVALID_CONV_START_ARG;
1146 if (trigger->iAcqStartTicksLow || trigger->iAcqStartTicksHigh) {
1147 PERROR("Invalid acq start trigger argument specified.\n");
1148 return ME_ERRNO_INVALID_ACQ_START_ARG;
1151 if (trigger->iScanStartTicksLow || trigger->iScanStartTicksHigh) {
1152 PERROR("Invalid scan start trigger argument specified.\n");
1153 return ME_ERRNO_INVALID_SCAN_START_ARG;
1156 switch (trigger->iScanStopTrigType) {
1157 case ME_TRIG_TYPE_NONE:
1158 if (trigger->iScanStopCount != 0) {
1159 PERROR("Invalid scan stop count specified.\n");
1160 return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1164 case ME_TRIG_TYPE_COUNT:
1165 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1166 if (trigger->iScanStopCount <= 0) {
1167 PERROR("Invalid scan stop count specified.\n");
1168 return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1171 PERROR("The continous mode has not 'scan' contects.\n");
1172 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1177 PERROR("Invalid scan stop trigger type specified.\n");
1178 return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE;
1181 switch (trigger->iAcqStopTrigType) {
1182 case ME_TRIG_TYPE_NONE:
1183 if (trigger->iAcqStopCount != 0) {
1184 PERROR("Invalid acq stop count specified.\n");
1185 return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1189 case ME_TRIG_TYPE_COUNT:
1190 if (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE) {
1191 PERROR("Invalid acq stop trigger type specified.\n");
1192 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1195 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1196 if (trigger->iAcqStopCount <= 0) {
1198 ("The continous mode has not 'scan' contects.\n");
1199 return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1204 // PERROR("Invalid acq stop trigger type specified.\n");
1205 // return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1211 PERROR("Invalid acq stop trigger type specified.\n");
1212 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1215 switch (trigger->iAcqStartTrigChan) {
1216 case ME_TRIG_CHAN_DEFAULT:
1217 case ME_TRIG_CHAN_SYNCHRONOUS:
1221 PERROR("Invalid acq start trigger channel specified.\n");
1222 return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN;
1229 //Cancel control task
1230 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1231 instance->ao_control_task_flag = 0;
1232 cancel_delayed_work(&instance->ao_control_task);
1234 //Check if state machine is stopped.
1235 err = ao_stop_immediately(instance);
1237 PERROR_CRITICAL("FSM IS BUSY!\n");
1240 return ME_ERRNO_SUBDEVICE_BUSY;
1243 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1244 //Reset control register. Block all actions. Disable IRQ. Disable FIFO.
1245 ctrl = ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
1246 outl(ctrl, instance->ctrl_reg);
1247 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1248 instance->ctrl_reg - instance->reg_base, ctrl);
1250 //Reset interrupt latch
1251 inl(instance->irq_reset_reg);
1253 //This is paranoic, but to be sure.
1254 instance->preloaded_count = 0;
1255 instance->data_count = 0;
1256 instance->circ_buf.head = 0;
1257 instance->circ_buf.tail = 0;
1260 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) { //Wraparound
1261 if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) { //Hardware wraparound
1262 PINFO("Hardware wraparound.\n");
1263 ctrl |= ME6000_AO_MODE_WRAPAROUND;
1264 instance->mode = ME6000_AO_HW_WRAP_MODE;
1265 } else { //Software wraparound
1266 PINFO("Software wraparound.\n");
1267 ctrl |= ME6000_AO_MODE_CONTINUOUS;
1268 instance->mode = ME6000_AO_SW_WRAP_MODE;
1270 } else { //Continous
1271 PINFO("Continous.\n");
1272 ctrl |= ME6000_AO_MODE_CONTINUOUS;
1273 instance->mode = ME6000_AO_CONTINOUS;
1276 //Set the trigger edge.
1277 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) { //Set the trigger type and edge for external trigger.
1278 PINFO("External digital trigger.\n");
1279 instance->start_mode = ME6000_AO_EXT_TRIG;
1281 switch (trigger->iAcqStartTrigEdge) {
1282 case ME_TRIG_EDGE_RISING:
1283 PINFO("Set the trigger edge: rising.\n");
1284 instance->ctrl_trg = 0x0;
1287 case ME_TRIG_EDGE_FALLING:
1288 PINFO("Set the trigger edge: falling.\n");
1289 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1290 instance->ctrl_trg = ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1293 case ME_TRIG_EDGE_ANY:
1294 PINFO("Set the trigger edge: both edges.\n");
1295 // ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1296 instance->ctrl_trg =
1297 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
1298 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1302 PINFO("Internal software trigger.\n");
1303 instance->start_mode = 0;
1306 //Set the stop mode and value.
1307 if (trigger->iAcqStopTrigType == ME_TRIG_TYPE_COUNT) { //Amount of data
1308 instance->stop_mode = ME6000_AO_ACQ_STOP_MODE;
1309 instance->stop_count = trigger->iAcqStopCount;
1310 } else if (trigger->iScanStopTrigType == ME_TRIG_TYPE_COUNT) { //Amount of 'scans'
1311 instance->stop_mode = ME6000_AO_SCAN_STOP_MODE;
1312 instance->stop_count = trigger->iScanStopCount;
1314 instance->stop_mode = ME6000_AO_INF_STOP_MODE;
1315 instance->stop_count = 0;
1318 PINFO("Stop count: %d.\n", instance->stop_count);
1320 if (trigger->iAcqStartTrigChan == ME_TRIG_CHAN_SYNCHRONOUS) { //Synchronous start
1321 instance->start_mode |= ME6000_AO_SYNC_HOLD;
1322 if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) { //Externaly triggered
1323 PINFO("Synchronous start. Externaly trigger active.\n");
1324 instance->start_mode |= ME6000_AO_SYNC_EXT_TRIG;
1329 ("Synchronous start. Externaly trigger dissabled.\n");
1335 outl(conv_ticks - 2, instance->timer_reg);
1336 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%llx\n", instance->reg_base,
1337 instance->timer_reg - instance->reg_base, conv_ticks - 2);
1338 instance->hardware_stop_delay = (int)(conv_ticks * HZ) / ME6000_AO_BASE_FREQUENCY; //<== MUST be with cast!
1340 // Write the control word
1341 outl(ctrl, instance->ctrl_reg);
1342 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1343 instance->ctrl_reg - instance->reg_base, ctrl);
1346 instance->status = ao_status_stream_configured;
1347 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1354 static int me6000_ao_io_stream_new_values(me_subdevice_t *subdevice,
1356 int time_out, int *count, int flags)
1358 me6000_ao_subdevice_t *instance;
1359 int err = ME_ERRNO_SUCCESS;
1363 instance = (me6000_ao_subdevice_t *) subdevice;
1365 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1367 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1368 PERROR("Not a streaming ao.\n");
1369 return ME_ERRNO_NOT_SUPPORTED;
1373 PERROR("Invalid flag specified.\n");
1374 return ME_ERRNO_INVALID_FLAGS;
1377 if (!instance->circ_buf.buf) {
1378 PERROR("Circular buffer not exists.\n");
1379 return ME_ERRNO_INTERNAL;
1383 PERROR("Invalid time_out specified.\n");
1384 return ME_ERRNO_INVALID_TIMEOUT;
1389 if (me_circ_buf_space(&instance->circ_buf)) { //The buffer is NOT full.
1390 *count = me_circ_buf_space(&instance->circ_buf);
1391 } else { //The buffer is full.
1393 t = (time_out * HZ) / 1000;
1397 } else { //Max time.
1405 //Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
1406 wait_event_interruptible_timeout(instance->wait_queue,
1408 (&instance->circ_buf))
1409 || !(inl(instance->status_reg)
1411 ME6000_AO_STATUS_BIT_FSM)),
1414 if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {
1415 PERROR("AO subdevice is not running.\n");
1416 err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
1417 } else if (signal_pending(current)) {
1418 PERROR("Wait on values interrupted from signal.\n");
1419 instance->status = ao_status_none;
1420 ao_stop_immediately(instance);
1421 err = ME_ERRNO_SIGNAL;
1422 } else if ((jiffies - j) >= t) {
1423 PERROR("Wait on values timed out.\n");
1424 err = ME_ERRNO_TIMEOUT;
1425 } else { //Uff... all is good. Inform user about empty space.
1426 *count = me_circ_buf_space(&instance->circ_buf);
1435 static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
1437 int start_mode, int time_out, int flags)
1439 me6000_ao_subdevice_t *instance;
1440 int err = ME_ERRNO_SUCCESS;
1441 unsigned long cpu_flags = 0;
1446 int circ_buffer_count;
1449 unsigned long delay = 0;
1451 instance = (me6000_ao_subdevice_t *) subdevice;
1453 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1455 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1456 PERROR("Not a streaming ao.\n");
1457 return ME_ERRNO_NOT_SUPPORTED;
1460 if (flags & ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS) {
1461 PERROR("Invalid flags.\n");
1462 return ME_ERRNO_INVALID_FLAGS;
1466 PERROR("Invalid timeout specified.\n");
1467 return ME_ERRNO_INVALID_TIMEOUT;
1470 if ((start_mode != ME_START_MODE_BLOCKING)
1471 && (start_mode != ME_START_MODE_NONBLOCKING)) {
1472 PERROR("Invalid start mode specified.\n");
1473 return ME_ERRNO_INVALID_START_MODE;
1477 delay = (time_out * HZ) / 1000;
1482 switch (instance->status) { //Checking actual mode.
1483 case ao_status_stream_configured:
1484 case ao_status_stream_end:
1488 //The device is in wrong mode.
1489 case ao_status_none:
1490 case ao_status_single_configured:
1491 case ao_status_single_run_wait:
1492 case ao_status_single_run:
1493 case ao_status_single_end_wait:
1495 ("Subdevice must be preinitialize correctly for streaming.\n");
1496 return ME_ERRNO_PREVIOUS_CONFIG;
1498 case ao_status_stream_fifo_error:
1499 case ao_status_stream_buffer_error:
1500 case ao_status_stream_error:
1501 PDEBUG("Before restart broke stream 'STOP' must be caled.\n");
1502 return ME_STATUS_ERROR;
1504 case ao_status_stream_run_wait:
1505 case ao_status_stream_run:
1506 case ao_status_stream_end_wait:
1507 PDEBUG("Stream is already working.\n");
1508 return ME_ERRNO_SUBDEVICE_BUSY;
1511 instance->status = ao_status_stream_error;
1512 PERROR_CRITICAL("Status is in wrong state!\n");
1513 return ME_ERRNO_INTERNAL;
1519 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1520 instance->circ_buf.tail += instance->preloaded_count;
1521 instance->circ_buf.tail &= instance->circ_buf.mask;
1523 circ_buffer_count = me_circ_buf_values(&instance->circ_buf);
1525 if (!circ_buffer_count && !instance->preloaded_count) { //No values in buffer
1527 PERROR("No values in buffer!\n");
1528 return ME_ERRNO_LACK_OF_RESOURCES;
1531 //Cancel control task
1532 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1533 instance->ao_control_task_flag = 0;
1534 cancel_delayed_work(&instance->ao_control_task);
1537 err = ao_stop_immediately(instance);
1539 PERROR_CRITICAL("FSM IS BUSY!\n");
1542 return ME_ERRNO_SUBDEVICE_BUSY;
1544 //Set values for single_read()
1545 instance->single_value = ME6000_AO_MAX_DATA + 1;
1546 instance->single_value_in_fifo = ME6000_AO_MAX_DATA + 1;
1548 //Setting stop points
1549 if (instance->stop_mode == ME6000_AO_SCAN_STOP_MODE) {
1550 instance->stop_data_count =
1551 instance->stop_count * circ_buffer_count;
1553 instance->stop_data_count = instance->stop_count;
1556 if ((instance->stop_data_count != 0)
1557 && (instance->stop_data_count < circ_buffer_count)) {
1558 PERROR("More data in buffer than previously set limit!\n");
1561 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1562 ctrl = inl(instance->ctrl_reg);
1564 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
1565 PINFO("Enableing FIFO.\n");
1566 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
1567 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1569 instance->preloaded_count = 0;
1570 instance->data_count = 0;
1571 } else { //Block IRQ
1572 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1574 outl(ctrl, instance->ctrl_reg);
1575 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1576 instance->ctrl_reg - instance->reg_base, ctrl);
1578 //Reset interrupt latch
1579 inl(instance->irq_reset_reg);
1581 //Fill FIFO <= Generaly this should be done by user pre-load call but this is second place to do it.
1582 status = inl(instance->status_reg);
1583 if (!(status & ME6000_AO_STATUS_BIT_EF)) { //FIFO empty
1584 if (instance->stop_data_count != 0) {
1585 count = ME6000_AO_FIFO_COUNT;
1588 (ME6000_AO_FIFO_COUNT <
1590 stop_data_count) ? ME6000_AO_FIFO_COUNT :
1591 instance->stop_data_count;
1596 ao_write_data(instance, count, instance->preloaded_count);
1598 if (count < 0) { //This should never happend!
1599 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1600 spin_unlock_irqrestore(&instance->subdevice_lock,
1603 return ME_ERRNO_INTERNAL;
1606 //Set pre-load features.
1607 spin_lock(instance->preload_reg_lock);
1608 synch = inl(instance->preload_reg);
1610 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
1613 (instance->start_mode & ~ME6000_AO_EXT_TRIG) << instance->ao_idx;
1614 outl(synch, instance->preload_reg);
1615 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1616 instance->preload_reg - instance->reg_base, synch);
1617 spin_unlock(instance->preload_reg_lock);
1619 //Default count is '0'
1620 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1621 instance->preloaded_count = 0;
1622 instance->circ_buf.tail += count;
1623 instance->circ_buf.tail &= instance->circ_buf.mask;
1624 } else { //Wraparound
1625 instance->preloaded_count += count;
1626 instance->data_count += count;
1628 //Special case: Infinite wraparound with less than FIFO datas always should runs in hardware mode.
1629 if ((instance->stop_mode == ME6000_AO_INF_STOP_MODE)
1630 && (circ_buffer_count <= ME6000_AO_FIFO_COUNT)) { //Change to hardware wraparound
1632 ("Changeing mode from software wraparound to hardware wraparound.\n");
1635 ao_write_data(instance, circ_buffer_count,
1636 instance->preloaded_count);
1637 ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
1638 ctrl |= ME6000_AO_MODE_WRAPAROUND;
1641 if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) { //Reset position indicator.
1642 instance->preloaded_count = 0;
1643 } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) { //This should never happend!
1645 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1646 spin_unlock_irqrestore(&instance->subdevice_lock,
1649 return ME_ERRNO_INTERNAL;
1653 //Set status to 'wait for start'
1654 instance->status = ao_status_stream_run_wait;
1656 status = inl(instance->status_reg);
1657 //Start state machine and interrupts
1658 PINFO("<%s:%d> Start state machine.\n", __func__, __LINE__);
1659 ctrl &= ~(ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
1660 if (instance->start_mode == ME6000_AO_EXT_TRIG) {
1661 PDEBUG("DIGITAL TRIGGER\n");
1662 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
1664 if (!(status & ME6000_AO_STATUS_BIT_HF)) { //More than half!
1665 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
1666 PINFO("<%s:%d> Start interrupts.\n", __func__,
1668 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1671 outl(ctrl, instance->ctrl_reg);
1672 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1673 instance->ctrl_reg - instance->reg_base, ctrl);
1674 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1677 PINFO("<%s> start mode= 0x%x %s\n", __func__, instance->start_mode,
1678 (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
1680 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { //Trigger outputs
1681 spin_lock(instance->preload_reg_lock);
1682 synch = inl(instance->preload_reg);
1683 //Add channel to start list
1684 outl(synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx),
1685 instance->preload_reg);
1686 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1688 instance->preload_reg - instance->reg_base,
1689 synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx));
1693 ("Fired all software synchronous outputs by software trigger.\n");
1694 outl(0x8000, instance->single_reg);
1695 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1697 instance->single_reg - instance->reg_base, 0x8000);
1699 //Restore save settings
1700 outl(synch, instance->preload_reg);
1701 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1703 instance->preload_reg - instance->reg_base, synch);
1704 spin_unlock(instance->preload_reg_lock);
1705 } else if (!instance->start_mode) { //Trigger outputs
1707 spin_lock(instance->preload_reg_lock);
1708 synch = inl(instance->preload_reg);
1709 //Remove channel from start list
1710 outl(synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
1711 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));
1714 PINFO("Software trigger.\n");
1715 outl(0x8000, instance->single_reg);
1716 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1718 instance->single_reg - instance->reg_base, 0x8000);
1721 //Restore save settings
1722 outl(synch, instance->preload_reg);
1723 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch);
1724 spin_unlock(instance->preload_reg_lock);
1727 // Set control task's timeout
1728 instance->timeout.delay = delay;
1729 instance->timeout.start_time = jiffies;
1731 if (status & ME6000_AO_STATUS_BIT_HF) { //Less than half but not empty!
1732 PINFO("Less than half.\n");
1733 if (instance->stop_data_count == 0) {
1734 count = ME6000_AO_FIFO_COUNT / 2;
1737 ((ME6000_AO_FIFO_COUNT / 2) <
1738 instance->stop_data_count) ? ME6000_AO_FIFO_COUNT /
1739 2 : instance->stop_data_count;
1744 ao_write_data(instance, count, instance->preloaded_count);
1746 if (count < 0) { //This should never happend!
1747 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1749 return ME_ERRNO_INTERNAL;
1752 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
1753 instance->circ_buf.tail += count;
1754 instance->circ_buf.tail &= instance->circ_buf.mask;
1755 } else { //Wraparound
1756 instance->data_count += count;
1757 instance->preloaded_count += count;
1759 if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) { //Reset position indicator.
1760 instance->preloaded_count = 0;
1761 } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) { //This should never happend!
1763 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1765 return ME_ERRNO_INTERNAL;
1769 status = inl(instance->status_reg);
1770 if (!(status & ME6000_AO_STATUS_BIT_HF)) { //More than half!
1771 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1772 PINFO("<%s:%d> Start interrupts.\n", __func__,
1774 ctrl = inl(instance->ctrl_reg);
1775 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1776 outl(ctrl, instance->ctrl_reg);
1777 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1779 instance->ctrl_reg - instance->reg_base,
1781 spin_unlock_irqrestore(&instance->subdevice_lock,
1785 //Special case: Limited wraparound with less than HALF FIFO datas need work around to generate first interrupt.
1786 if ((instance->stop_mode != ME6000_AO_INF_STOP_MODE)
1787 && (instance->mode == ME6000_AO_SW_WRAP_MODE)
1788 && (circ_buffer_count <= (ME6000_AO_FIFO_COUNT / 2))) { //Put more data to FIFO
1789 PINFO("Limited wraparound with less than HALF FIFO datas.\n");
1790 if (instance->preloaded_count) { //This should never happend!
1792 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1794 return ME_ERRNO_INTERNAL;
1797 while (instance->stop_data_count > instance->data_count) { //Maximum data not set jet.
1799 if (circ_buffer_count != ao_write_data(instance, circ_buffer_count, 0)) { //This should never happend!
1801 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1803 return ME_ERRNO_INTERNAL;
1805 instance->data_count += circ_buffer_count;
1807 if (!((status = inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF)) { //FIFO is more than half. Enable IRQ and end copy.
1808 //Reset interrupt latch
1809 inl(instance->irq_reset_reg);
1811 spin_lock_irqsave(&instance->subdevice_lock,
1813 PINFO("<%s:%d> Start interrupts.\n",
1814 __func__, __LINE__);
1815 ctrl = inl(instance->ctrl_reg);
1816 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1817 outl(ctrl, instance->ctrl_reg);
1818 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1820 instance->ctrl_reg -
1821 instance->reg_base, ctrl);
1822 spin_unlock_irqrestore(&instance->
1829 // Schedule control task
1830 instance->ao_control_task_flag = 1;
1831 queue_delayed_work(instance->me6000_workqueue,
1832 &instance->ao_control_task, 1);
1834 if (start_mode == ME_START_MODE_BLOCKING) { //Wait for start.
1836 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1837 wait_event_interruptible_timeout(instance->wait_queue,
1838 (instance->status !=
1839 ao_status_stream_run_wait),
1843 if ((instance->status != ao_status_stream_run)
1844 && (instance->status != ao_status_stream_end)) {
1845 PDEBUG("Starting stream canceled. %d\n",
1847 err = ME_ERRNO_CANCELLED;
1850 if (signal_pending(current)) {
1851 PERROR("Wait on start of state machine interrupted.\n");
1852 instance->status = ao_status_none;
1853 ao_stop_immediately(instance);
1854 err = ME_ERRNO_SIGNAL;
1857 if ((delay) && ((jiffies - ref) >= delay)) {
1858 if (instance->status != ao_status_stream_run) {
1859 if (instance->status == ao_status_stream_end) {
1860 PDEBUG("Timeout reached.\n");
1861 } else if ((jiffies - ref) > delay) {
1863 ("Timeout reached. Not handled by control task!\n");
1864 ao_stop_immediately(instance);
1867 ("Timeout reached. Signal come but status is strange: %d\n",
1869 ao_stop_immediately(instance);
1872 instance->ao_control_task_flag = 0;
1873 cancel_delayed_work(&instance->ao_control_task);
1874 instance->status = ao_status_stream_end;
1875 err = ME_ERRNO_TIMEOUT;
1884 static int me6000_ao_io_stream_status(me_subdevice_t *subdevice,
1887 int *status, int *values, int flags)
1889 me6000_ao_subdevice_t *instance;
1890 int err = ME_ERRNO_SUCCESS;
1892 instance = (me6000_ao_subdevice_t *) subdevice;
1894 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1896 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1897 PERROR("Not a streaming ao.\n");
1898 return ME_ERRNO_NOT_SUPPORTED;
1902 PERROR("Invalid flag specified.\n");
1903 return ME_ERRNO_INVALID_FLAGS;
1906 if ((wait != ME_WAIT_NONE) && (wait != ME_WAIT_IDLE)) {
1907 PERROR("Invalid wait argument specified.\n");
1908 *status = ME_STATUS_INVALID;
1909 return ME_ERRNO_INVALID_WAIT;
1914 switch (instance->status) {
1915 case ao_status_single_configured:
1916 case ao_status_single_end:
1917 case ao_status_stream_configured:
1918 case ao_status_stream_end:
1919 case ao_status_stream_fifo_error:
1920 case ao_status_stream_buffer_error:
1921 case ao_status_stream_error:
1922 *status = ME_STATUS_IDLE;
1925 case ao_status_single_run_wait:
1926 case ao_status_single_run:
1927 case ao_status_single_end_wait:
1928 case ao_status_stream_run_wait:
1929 case ao_status_stream_run:
1930 case ao_status_stream_end_wait:
1931 *status = ME_STATUS_BUSY;
1934 case ao_status_none:
1937 (inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM) ?
1938 ME_STATUS_BUSY : ME_STATUS_IDLE;
1942 if ((wait == ME_WAIT_IDLE) && (*status == ME_STATUS_BUSY)) {
1943 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1944 wait_event_interruptible_timeout(instance->wait_queue,
1945 ((instance->status !=
1946 ao_status_single_run_wait)
1947 && (instance->status !=
1948 ao_status_single_run)
1949 && (instance->status !=
1950 ao_status_single_end_wait)
1951 && (instance->status !=
1952 ao_status_stream_run_wait)
1953 && (instance->status !=
1954 ao_status_stream_run)
1955 && (instance->status !=
1956 ao_status_stream_end_wait)),
1959 if (instance->status != ao_status_stream_end) {
1960 PDEBUG("Wait for IDLE canceled. %d\n",
1962 err = ME_ERRNO_CANCELLED;
1965 if (signal_pending(current)) {
1966 PERROR("Wait for IDLE interrupted.\n");
1967 instance->status = ao_status_none;
1968 ao_stop_immediately(instance);
1969 err = ME_ERRNO_SIGNAL;
1972 *status = ME_STATUS_IDLE;
1975 *values = me_circ_buf_space(&instance->circ_buf);
1982 static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
1984 int stop_mode, int flags)
1985 { /// @note Stop work and empty buffer and FIFO
1986 int err = ME_ERRNO_SUCCESS;
1987 me6000_ao_subdevice_t *instance;
1988 unsigned long cpu_flags;
1989 volatile uint32_t ctrl;
1991 instance = (me6000_ao_subdevice_t *) subdevice;
1993 PDEBUG("executed. idx=%d\n", instance->ao_idx);
1995 if (flags & ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS) {
1996 PERROR("Invalid flag specified.\n");
1997 return ME_ERRNO_INVALID_FLAGS;
2000 if ((stop_mode != ME_STOP_MODE_IMMEDIATE)
2001 && (stop_mode != ME_STOP_MODE_LAST_VALUE)) {
2002 PERROR("Invalid stop mode specified.\n");
2003 return ME_ERRNO_INVALID_STOP_MODE;
2006 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2007 PERROR("Not a streaming ao.\n");
2008 return ME_ERRNO_NOT_SUPPORTED;
2011 if (instance->status < ao_status_stream_configured) {
2012 //There is nothing to stop!
2013 PERROR("Subdevice not in streaming mode. %d\n",
2015 return ME_ERRNO_PREVIOUS_CONFIG;
2020 //Mark as stopping. => Software stop.
2021 instance->status = ao_status_stream_end_wait;
2023 if (stop_mode == ME_STOP_MODE_IMMEDIATE) { //Stopped now!
2024 err = ao_stop_immediately(instance);
2025 } else if (stop_mode == ME_STOP_MODE_LAST_VALUE) {
2026 ctrl = inl(instance->ctrl_reg) & ME6000_AO_CTRL_MODE_MASK;
2027 if (ctrl == ME6000_AO_MODE_WRAPAROUND) { //Hardware wraparound => Hardware stop.
2028 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2029 ctrl = inl(instance->ctrl_reg);
2030 ctrl |= ME6000_AO_CTRL_BIT_STOP;
2031 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2032 outl(ctrl, instance->ctrl_reg);
2033 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2035 instance->ctrl_reg - instance->reg_base,
2037 spin_unlock_irqrestore(&instance->subdevice_lock,
2040 //Reset interrupt latch
2041 inl(instance->irq_reset_reg);
2043 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
2044 wait_event_interruptible_timeout(instance->wait_queue,
2045 (instance->status !=
2046 ao_status_stream_end_wait),
2049 if (instance->status != ao_status_stream_end) {
2050 PDEBUG("Stopping stream canceled.\n");
2051 err = ME_ERRNO_CANCELLED;
2054 if (signal_pending(current)) {
2055 PERROR("Stopping stream interrupted.\n");
2056 instance->status = ao_status_none;
2057 ao_stop_immediately(instance);
2058 err = ME_ERRNO_SIGNAL;
2062 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2063 ctrl = inl(instance->ctrl_reg);
2064 ctrl |= ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2065 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2066 if (!flags) { //Reset FIFO
2067 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2069 outl(ctrl, instance->ctrl_reg);
2070 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
2071 instance->ctrl_reg - instance->reg_base, ctrl);
2072 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2074 //Reset interrupt latch
2075 inl(instance->irq_reset_reg);
2077 if (!flags) { //Reset software buffer
2078 instance->circ_buf.head = 0;
2079 instance->circ_buf.tail = 0;
2080 instance->preloaded_count = 0;
2081 instance->data_count = 0;
2089 static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
2092 int *values, int *count, int flags)
2094 int err = ME_ERRNO_SUCCESS;
2095 me6000_ao_subdevice_t *instance;
2096 unsigned long cpu_flags = 0;
2099 int copied_from_user = 0;
2100 int left_to_copy_from_user = *count;
2104 instance = (me6000_ao_subdevice_t *) subdevice;
2106 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2108 //Checking arguments
2109 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2110 PERROR("Not a streaming ao.\n");
2111 return ME_ERRNO_NOT_SUPPORTED;
2115 PERROR("Invalid flag specified.\n");
2116 return ME_ERRNO_INVALID_FLAGS;
2120 PERROR("Invalid count of values specified.\n");
2121 return ME_ERRNO_INVALID_VALUE_COUNT;
2124 if (values == NULL) {
2125 PERROR("Invalid address of values specified.\n");
2126 return ME_ERRNO_INVALID_POINTER;
2129 if ((instance->status == ao_status_none) || (instance->status == ao_status_single_configured)) { //The device is in single mode.
2131 ("Subdevice must be preinitialize correctly for streaming.\n");
2132 return ME_ERRNO_PREVIOUS_CONFIG;
2135 switch (write_mode) {
2136 case ME_WRITE_MODE_PRELOAD:
2138 //Device must be stopped.
2139 if ((instance->status != ao_status_stream_configured)
2140 && (instance->status != ao_status_stream_end)) {
2142 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2143 return ME_ERRNO_PREVIOUS_CONFIG;
2146 case ME_WRITE_MODE_NONBLOCKING:
2147 case ME_WRITE_MODE_BLOCKING:
2148 /// @note In blocking mode: When device is not runing and there is not enought space call will blocked up!
2149 /// @note Some other thread must empty buffer by strating engine.
2153 PERROR("Invalid write mode specified.\n");
2154 return ME_ERRNO_INVALID_WRITE_MODE;
2157 if (instance->mode & ME6000_AO_WRAP_MODE) { //Wraparound mode. Device must be stopped.
2158 if ((instance->status != ao_status_stream_configured)
2159 && (instance->status != ao_status_stream_end)) {
2161 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2162 return ME_ERRNO_INVALID_WRITE_MODE;
2166 if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2167 && (write_mode != ME_WRITE_MODE_PRELOAD)) {
2169 PERROR("Only 'pre-load' write is acceptable in hardware wraparound mode.\n");
2170 return ME_ERRNO_PREVIOUS_CONFIG;
2172 //This is transparent for user.
2173 PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
2174 write_mode = ME_WRITE_MODE_PRELOAD;
2179 if (write_mode == ME_WRITE_MODE_PRELOAD) { //Init enviroment - preload
2180 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2181 reg_copy = inl(instance->ctrl_reg);
2183 if (!(reg_copy & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO not active. Enable it.
2184 reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2185 outl(reg_copy, instance->ctrl_reg);
2186 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2188 instance->ctrl_reg - instance->reg_base,
2190 instance->preloaded_count = 0;
2192 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2196 //Copy to buffer. This step is common for all modes.
2198 ao_get_data_from_user(instance, left_to_copy_from_user,
2200 left_to_copy_from_user));
2201 left_to_copy_from_user -= copied_from_user;
2203 reg_copy = inl(instance->status_reg);
2204 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.
2205 PERROR("Broken pipe in write.\n");
2206 err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
2210 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!
2212 // Block interrupts.
2213 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2214 reg_copy = inl(instance->ctrl_reg);
2215 reg_copy &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2216 outl(reg_copy, instance->ctrl_reg);
2217 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2219 instance->ctrl_reg - instance->reg_base,
2221 spin_unlock_irqrestore(&instance->subdevice_lock,
2226 ao_write_data(instance, ME6000_AO_FIFO_COUNT / 2,
2228 if (copied_values > 0) {
2229 instance->circ_buf.tail += copied_values;
2230 instance->circ_buf.tail &=
2231 instance->circ_buf.mask;
2234 //Reset interrupt latch
2235 inl(instance->irq_reset_reg);
2237 // Activate interrupts.
2238 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2239 reg_copy = inl(instance->ctrl_reg);
2240 reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2241 outl(reg_copy, instance->ctrl_reg);
2242 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2244 instance->ctrl_reg - instance->reg_base,
2246 spin_unlock_irqrestore(&instance->subdevice_lock,
2249 if (copied_values == 0) { //This was checked and never should happend!
2250 PERROR_CRITICAL("COPY FINISH WITH 0!\n");
2253 if (copied_values < 0) { //This was checked and never should happend!
2254 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
2255 instance->status = ao_status_stream_fifo_error;
2256 err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2261 if (!left_to_copy_from_user) { //All datas were copied.
2263 } else { //Not all datas were copied.
2264 if (instance->mode & ME6000_AO_WRAP_MODE) { //Error too much datas! Wraparound is limited in size!
2266 ("Too much data for wraparound mode! Exceeded size of %ld.\n",
2267 ME6000_AO_CIRC_BUF_COUNT - 1);
2268 err = ME_ERRNO_RING_BUFFER_OVERFLOW;
2272 if (write_mode != ME_WRITE_MODE_BLOCKING) { //Non blocking calls
2276 wait_event_interruptible(instance->wait_queue,
2277 me_circ_buf_space(&instance->
2280 if (signal_pending(current)) {
2281 PERROR("Writing interrupted by signal.\n");
2282 instance->status = ao_status_none;
2283 ao_stop_immediately(instance);
2284 err = ME_ERRNO_SIGNAL;
2288 if (instance->status == ao_status_none) { //Reset
2289 PERROR("Writing interrupted by reset.\n");
2290 err = ME_ERRNO_CANCELLED;
2296 if (write_mode == ME_WRITE_MODE_PRELOAD) { //Copy data to FIFO - preload
2298 ao_write_data_pooling(instance, ME6000_AO_FIFO_COUNT,
2299 instance->preloaded_count);
2300 instance->preloaded_count += copied_values;
2301 instance->data_count += copied_values;
2303 if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2304 && (me_circ_buf_values(&instance->circ_buf) >
2305 ME6000_AO_FIFO_COUNT)) {
2307 ("Too much data for hardware wraparound mode! Exceeded size of %d.\n",
2308 ME6000_AO_FIFO_COUNT);
2309 err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2313 *count = *count - left_to_copy_from_user;
2319 static irqreturn_t me6000_ao_isr(int irq, void *dev_id)
2321 me6000_ao_subdevice_t *instance = dev_id;
2322 uint32_t irq_status;
2327 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2329 if (irq != instance->irq) {
2330 PERROR("Incorrect interrupt num: %d.\n", irq);
2334 irq_status = inl(instance->irq_status_reg);
2335 if (!(irq_status & (ME6000_IRQ_STATUS_BIT_AO_HF << instance->ao_idx))) {
2336 PINFO("%ld Shared interrupt. %s(): ID=%d: status_reg=0x%04X\n",
2337 jiffies, __func__, instance->ao_idx, irq_status);
2341 if (!instance->circ_buf.buf) {
2342 instance->status = ao_status_stream_error;
2343 PERROR_CRITICAL("CIRCULAR BUFFER NOT EXISTS!\n");
2344 //Block interrupts. Stop machine.
2345 ctrl = inl(instance->ctrl_reg);
2346 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2348 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
2349 outl(ctrl, instance->ctrl_reg);
2350 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2352 instance->ctrl_reg - instance->reg_base, ctrl);
2355 wake_up_interruptible_all(&instance->wait_queue);
2359 status = inl(instance->status_reg);
2360 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { //Too late. Not working! END? BROKEN PIPE?
2361 /// @note Error checking was moved to separate task.
2362 PDEBUG("Interrupt come but ISM is not working!\n");
2363 //Block interrupts. Stop machine.
2364 ctrl = inl(instance->ctrl_reg);
2365 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2367 ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2368 outl(ctrl, instance->ctrl_reg);
2369 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2371 instance->ctrl_reg - instance->reg_base, ctrl);
2373 //Reset interrupt latch
2374 inl(instance->irq_reset_reg);
2376 /// @note User notification was also moved to separate task.
2379 //General procedure. Process more datas.
2381 #ifdef MEDEBUG_DEBUG
2382 if (!me_circ_buf_values(&instance->circ_buf)) { //Buffer is empty!
2383 PDEBUG("Circular buffer empty!\n");
2388 if (status & ME6000_AO_STATUS_BIT_HF) { //OK less than half
2391 ctrl = inl(instance->ctrl_reg);
2392 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2393 outl(ctrl, instance->ctrl_reg);
2394 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2396 instance->ctrl_reg - instance->reg_base, ctrl);
2399 //Calculate how many should be copied.
2401 (instance->stop_data_count) ? instance->
2403 instance->data_count : ME6000_AO_FIFO_COUNT / 2;
2404 if (ME6000_AO_FIFO_COUNT / 2 < count) {
2405 count = ME6000_AO_FIFO_COUNT / 2;
2408 if (instance->mode == ME6000_AO_CONTINOUS) { //Continous
2409 count = ao_write_data(instance, count, 0);
2411 instance->circ_buf.tail += count;
2412 instance->circ_buf.tail &=
2413 instance->circ_buf.mask;
2414 instance->data_count += count;
2416 if ((instance->status == ao_status_stream_end_wait) && !me_circ_buf_values(&instance->circ_buf)) { //Stoping. Whole buffer was copied.
2420 } else if ((instance->mode == ME6000_AO_SW_WRAP_MODE) && ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS)) { //Wraparound (software)
2421 if (instance->status == ao_status_stream_end_wait) { //We stoping => Copy to the end of the buffer.
2423 ao_write_data(instance, count, 0);
2424 } else { //Copy in wraparound mode.
2426 ao_write_data_wraparound(instance,
2433 instance->data_count += count;
2434 instance->preloaded_count += count;
2435 instance->preloaded_count %=
2436 me_circ_buf_values(&instance->
2439 if ((instance->status == ao_status_stream_end_wait) && !instance->preloaded_count) { //Stoping. Whole buffer was copied.
2445 if ((count <= 0) || (instance->stop_data_count && (instance->stop_data_count <= instance->data_count))) { //End of work.
2448 } //Repeat if still is under half fifo
2450 inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF);
2452 //Unblock interrupts
2453 ctrl = inl(instance->ctrl_reg);
2454 if (count >= 0) { //Copy was successful.
2455 if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. No more interrupts.
2456 PDEBUG("Finishing work. Interrupt disabled.\n");
2457 instance->status = ao_status_stream_end_wait;
2458 } else if (count > 0) { //Normal work. Enable interrupt.
2459 PDEBUG("Normal work. Enable interrupt.\n");
2460 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2461 } else { //Normal work but there are no more data in buffer. Interrupt blocked. stream_write() will unblock it.
2463 ("No data in software buffer. Interrupt blocked.\n");
2465 } else { //Error during copy.
2466 instance->status = ao_status_stream_fifo_error;
2469 outl(ctrl, instance->ctrl_reg);
2470 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2472 instance->ctrl_reg - instance->reg_base, ctrl);
2473 } else { //?? more than half
2475 ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
2478 PINFO("ISR: Buffer count: %d.(T:%d H:%d)\n",
2479 me_circ_buf_values(&instance->circ_buf), instance->circ_buf.tail,
2480 instance->circ_buf.head);
2481 PINFO("ISR: Stop count: %d.\n", instance->stop_count);
2482 PINFO("ISR: Stop data count: %d.\n", instance->stop_data_count);
2483 PINFO("ISR: Data count: %d.\n", instance->data_count);
2485 //Reset interrupt latch
2486 inl(instance->irq_reset_reg);
2489 wake_up_interruptible_all(&instance->wait_queue);
2494 static void me6000_ao_destructor(struct me_subdevice *subdevice)
2496 me6000_ao_subdevice_t *instance;
2498 instance = (me6000_ao_subdevice_t *) subdevice;
2500 PDEBUG("executed. idx=%d\n", instance->ao_idx);
2502 instance->ao_control_task_flag = 0;
2504 // Reset subdevice to asure clean exit.
2505 me6000_ao_io_reset_subdevice(subdevice, NULL,
2506 ME_IO_RESET_SUBDEVICE_NO_FLAGS);
2508 // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
2509 if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
2510 set_current_state(TASK_INTERRUPTIBLE);
2511 schedule_timeout(2);
2514 if (instance->fifo & ME6000_AO_HAS_FIFO) {
2515 if (instance->irq) {
2516 free_irq(instance->irq, instance);
2520 if (instance->circ_buf.buf) {
2521 PDEBUG("free circ_buf = %p size=%d",
2522 instance->circ_buf.buf,
2523 PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2524 free_pages((unsigned long)instance->circ_buf.buf,
2525 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2527 instance->circ_buf.buf = NULL;
2530 me_subdevice_deinit(&instance->base);
2534 me6000_ao_subdevice_t *me6000_ao_constructor(uint32_t reg_base,
2535 spinlock_t *preload_reg_lock,
2536 uint32_t *preload_flags,
2537 uint32_t *triggering_flags,
2542 struct workqueue_struct *me6000_wq)
2544 me6000_ao_subdevice_t *subdevice;
2547 PDEBUG("executed ID=%d.\n", ao_idx);
2549 /* Allocate memory for subdevice instance */
2550 subdevice = kmalloc(sizeof(me6000_ao_subdevice_t), GFP_KERNEL);
2553 PERROR("Cannot get memory for subdevice instance.\n");
2557 memset(subdevice, 0, sizeof(me6000_ao_subdevice_t));
2559 /* Initialize subdevice base class */
2560 err = me_subdevice_init(&subdevice->base);
2563 PERROR("Cannot initialize subdevice base class instance.\n");
2567 // Initialize spin locks.
2568 spin_lock_init(&subdevice->subdevice_lock);
2570 subdevice->preload_reg_lock = preload_reg_lock;
2571 subdevice->preload_flags = preload_flags;
2572 subdevice->triggering_flags = triggering_flags;
2574 /* Store analog output index */
2575 subdevice->ao_idx = ao_idx;
2577 /* Store if analog output has fifo */
2578 subdevice->fifo = fifo;
2580 if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2581 /* Allocate and initialize circular buffer */
2582 subdevice->circ_buf.mask = ME6000_AO_CIRC_BUF_COUNT - 1;
2583 subdevice->circ_buf.buf =
2584 (void *)__get_free_pages(GFP_KERNEL,
2585 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2586 PDEBUG("circ_buf = %p size=%ld\n", subdevice->circ_buf.buf,
2587 ME6000_AO_CIRC_BUF_SIZE);
2589 if (!subdevice->circ_buf.buf) {
2591 ("Cannot initialize subdevice base class instance.\n");
2596 memset(subdevice->circ_buf.buf, 0, ME6000_AO_CIRC_BUF_SIZE);
2598 subdevice->circ_buf.mask = 0;
2599 subdevice->circ_buf.buf = NULL;
2601 subdevice->circ_buf.head = 0;
2602 subdevice->circ_buf.tail = 0;
2604 subdevice->status = ao_status_none;
2605 subdevice->ao_control_task_flag = 0;
2606 subdevice->timeout.delay = 0;
2607 subdevice->timeout.start_time = jiffies;
2609 /* Initialize wait queue */
2610 init_waitqueue_head(&subdevice->wait_queue);
2612 /* Initialize single value to 0V */
2613 subdevice->single_value = 0x8000;
2614 subdevice->single_value_in_fifo = 0x8000;
2616 /* Initialize range boarders */
2618 subdevice->min = ME6000_AO_MIN_RANGE_HIGH;
2619 subdevice->max = ME6000_AO_MAX_RANGE_HIGH;
2621 subdevice->min = ME6000_AO_MIN_RANGE;
2622 subdevice->max = ME6000_AO_MAX_RANGE;
2625 /* Register interrupt service routine */
2627 if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2628 subdevice->irq = irq;
2629 if (request_irq(subdevice->irq, me6000_ao_isr,
2630 #ifdef IRQF_DISABLED
2631 IRQF_DISABLED | IRQF_SHARED,
2633 SA_INTERRUPT | SA_SHIRQ,
2635 ME6000_NAME, subdevice)) {
2636 PERROR("Cannot get interrupt line.\n");
2637 PDEBUG("free circ_buf = %p size=%d",
2638 subdevice->circ_buf.buf,
2639 PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2640 free_pages((unsigned long)subdevice->circ_buf.buf,
2641 ME6000_AO_CIRC_BUF_SIZE_ORDER);
2642 subdevice->circ_buf.buf = NULL;
2646 PINFO("Registered irq=%d.\n", subdevice->irq);
2651 /* Initialize registers */
2652 // Only streamed subdevices support interrupts. For the rest this register has no meaning.
2653 subdevice->irq_status_reg = reg_base + ME6000_AO_IRQ_STATUS_REG;
2654 subdevice->preload_reg = reg_base + ME6000_AO_PRELOAD_REG;
2657 subdevice->ctrl_reg = reg_base + ME6000_AO_00_CTRL_REG;
2658 subdevice->status_reg = reg_base + ME6000_AO_00_STATUS_REG;
2659 subdevice->fifo_reg = reg_base + ME6000_AO_00_FIFO_REG;
2660 subdevice->timer_reg = reg_base + ME6000_AO_00_TIMER_REG;
2661 subdevice->irq_reset_reg =
2662 reg_base + ME6000_AO_00_IRQ_RESET_REG;
2663 subdevice->single_reg = reg_base + ME6000_AO_00_SINGLE_REG;
2664 } else if (ao_idx == 1) {
2665 subdevice->ctrl_reg = reg_base + ME6000_AO_01_CTRL_REG;
2666 subdevice->status_reg = reg_base + ME6000_AO_01_STATUS_REG;
2667 subdevice->fifo_reg = reg_base + ME6000_AO_01_FIFO_REG;
2668 subdevice->timer_reg = reg_base + ME6000_AO_01_TIMER_REG;
2669 subdevice->irq_reset_reg =
2670 reg_base + ME6000_AO_01_IRQ_RESET_REG;
2671 subdevice->single_reg = reg_base + ME6000_AO_01_SINGLE_REG;
2672 } else if (ao_idx == 2) {
2673 subdevice->ctrl_reg = reg_base + ME6000_AO_02_CTRL_REG;
2674 subdevice->status_reg = reg_base + ME6000_AO_02_STATUS_REG;
2675 subdevice->fifo_reg = reg_base + ME6000_AO_02_FIFO_REG;
2676 subdevice->timer_reg = reg_base + ME6000_AO_02_TIMER_REG;
2677 subdevice->irq_reset_reg =
2678 reg_base + ME6000_AO_02_IRQ_RESET_REG;
2679 subdevice->single_reg = reg_base + ME6000_AO_02_SINGLE_REG;
2680 } else if (ao_idx == 3) {
2681 subdevice->ctrl_reg = reg_base + ME6000_AO_03_CTRL_REG;
2682 subdevice->status_reg = reg_base + ME6000_AO_03_STATUS_REG;
2683 subdevice->fifo_reg = reg_base + ME6000_AO_03_FIFO_REG;
2684 subdevice->timer_reg = reg_base + ME6000_AO_03_TIMER_REG;
2685 subdevice->irq_reset_reg =
2686 reg_base + ME6000_AO_03_IRQ_RESET_REG;
2687 subdevice->single_reg = reg_base + ME6000_AO_03_SINGLE_REG;
2689 subdevice->ctrl_reg = reg_base + ME6000_AO_DUMY;
2690 subdevice->fifo_reg = reg_base + ME6000_AO_DUMY;
2691 subdevice->timer_reg = reg_base + ME6000_AO_DUMY;
2692 subdevice->irq_reset_reg = reg_base + ME6000_AO_DUMY;
2693 subdevice->single_reg = reg_base + ME6000_AO_DUMY;
2695 subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
2697 subdevice->single_reg =
2698 reg_base + ME6000_AO_04_SINGLE_REG;
2699 } else if (ao_idx == 5) {
2700 subdevice->single_reg =
2701 reg_base + ME6000_AO_05_SINGLE_REG;
2702 } else if (ao_idx == 6) {
2703 subdevice->single_reg =
2704 reg_base + ME6000_AO_06_SINGLE_REG;
2705 } else if (ao_idx == 7) {
2706 subdevice->single_reg =
2707 reg_base + ME6000_AO_07_SINGLE_REG;
2708 } else if (ao_idx == 8) {
2709 subdevice->single_reg =
2710 reg_base + ME6000_AO_08_SINGLE_REG;
2711 } else if (ao_idx == 9) {
2712 subdevice->single_reg =
2713 reg_base + ME6000_AO_09_SINGLE_REG;
2714 } else if (ao_idx == 10) {
2715 subdevice->single_reg =
2716 reg_base + ME6000_AO_10_SINGLE_REG;
2717 } else if (ao_idx == 11) {
2718 subdevice->single_reg =
2719 reg_base + ME6000_AO_11_SINGLE_REG;
2720 } else if (ao_idx == 12) {
2721 subdevice->single_reg =
2722 reg_base + ME6000_AO_12_SINGLE_REG;
2723 } else if (ao_idx == 13) {
2724 subdevice->single_reg =
2725 reg_base + ME6000_AO_13_SINGLE_REG;
2726 } else if (ao_idx == 14) {
2727 subdevice->single_reg =
2728 reg_base + ME6000_AO_14_SINGLE_REG;
2729 } else if (ao_idx == 15) {
2730 subdevice->single_reg =
2731 reg_base + ME6000_AO_15_SINGLE_REG;
2733 PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx);
2734 me_subdevice_deinit((me_subdevice_t *) subdevice);
2735 if (subdevice->fifo) {
2736 free_pages((unsigned long)subdevice->circ_buf.
2737 buf, ME6000_AO_CIRC_BUF_SIZE_ORDER);
2739 subdevice->circ_buf.buf = NULL;
2744 #ifdef MEDEBUG_DEBUG_REG
2745 subdevice->reg_base = reg_base;
2748 /* Override base class methods. */
2749 subdevice->base.me_subdevice_destructor = me6000_ao_destructor;
2750 subdevice->base.me_subdevice_io_reset_subdevice =
2751 me6000_ao_io_reset_subdevice;
2752 subdevice->base.me_subdevice_io_single_config =
2753 me6000_ao_io_single_config;
2754 subdevice->base.me_subdevice_io_single_read = me6000_ao_io_single_read;
2755 subdevice->base.me_subdevice_io_single_write =
2756 me6000_ao_io_single_write;
2757 subdevice->base.me_subdevice_io_stream_config =
2758 me6000_ao_io_stream_config;
2759 subdevice->base.me_subdevice_io_stream_new_values =
2760 me6000_ao_io_stream_new_values;
2761 subdevice->base.me_subdevice_io_stream_write =
2762 me6000_ao_io_stream_write;
2763 subdevice->base.me_subdevice_io_stream_start =
2764 me6000_ao_io_stream_start;
2765 subdevice->base.me_subdevice_io_stream_status =
2766 me6000_ao_io_stream_status;
2767 subdevice->base.me_subdevice_io_stream_stop = me6000_ao_io_stream_stop;
2768 subdevice->base.me_subdevice_query_number_channels =
2769 me6000_ao_query_number_channels;
2770 subdevice->base.me_subdevice_query_subdevice_type =
2771 me6000_ao_query_subdevice_type;
2772 subdevice->base.me_subdevice_query_subdevice_caps =
2773 me6000_ao_query_subdevice_caps;
2774 subdevice->base.me_subdevice_query_subdevice_caps_args =
2775 me6000_ao_query_subdevice_caps_args;
2776 subdevice->base.me_subdevice_query_range_by_min_max =
2777 me6000_ao_query_range_by_min_max;
2778 subdevice->base.me_subdevice_query_number_ranges =
2779 me6000_ao_query_number_ranges;
2780 subdevice->base.me_subdevice_query_range_info =
2781 me6000_ao_query_range_info;
2782 subdevice->base.me_subdevice_query_timer = me6000_ao_query_timer;
2784 //prepare work queue and work function
2785 subdevice->me6000_workqueue = me6000_wq;
2787 /* workqueue API changed in kernel 2.6.20 */
2788 INIT_DELAYED_WORK(&subdevice->ao_control_task,
2789 me6000_ao_work_control_task);
2791 if (subdevice->fifo) { //Set speed
2792 outl(ME6000_AO_MIN_CHAN_TICKS - 1, subdevice->timer_reg);
2793 subdevice->hardware_stop_delay = HZ / 10; //100ms
2799 /** @brief Stop presentation. Preserve FIFOs.
2801 * @param instance The subdevice instance (pointer).
2803 inline int ao_stop_immediately(me6000_ao_subdevice_t *instance)
2805 unsigned long cpu_flags;
2809 uint32_t single_mask;
2811 if (instance->ao_idx < ME6000_AO_SINGLE_STATUS_OFFSET)
2812 single_mask = 0x0000;
2814 single_mask = 0x0001 << (instance->ao_idx -
2815 ME6000_AO_SINGLE_STATUS_OFFSET);
2818 (instance->hardware_stop_delay >
2819 (HZ / 10)) ? instance->hardware_stop_delay : HZ / 10;
2820 for (i = 0; i <= timeout; i++) {
2821 if (instance->fifo) {
2822 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2823 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2824 ctrl = inl(instance->ctrl_reg);
2826 ME6000_AO_CTRL_BIT_STOP |
2827 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2829 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
2830 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
2831 outl(ctrl, instance->ctrl_reg);
2832 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2834 instance->ctrl_reg - instance->reg_base,
2836 spin_unlock_irqrestore(&instance->subdevice_lock,
2839 if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) { // Exit.
2843 if (!(inl(instance->status_reg) & single_mask)) { // Exit.
2848 PINFO("<%s> Wait for stop: %d\n", __func__, i);
2851 set_current_state(TASK_INTERRUPTIBLE);
2852 schedule_timeout(1);
2856 PERROR_CRITICAL("FSM IS BUSY!\n");
2857 return ME_ERRNO_INTERNAL;
2859 return ME_ERRNO_SUCCESS;
2862 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2863 * @note This is time critical function. Checking is done at begining and end only.
2864 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2866 * @param instance The subdevice instance (pointer).
2867 * @param count Maximum number of copied data.
2868 * @param start_pos Position of the firs value in buffer.
2870 * @return On success: Number of copied data.
2871 * @return On error/success: 0. No datas were copied => no data in buffer.
2872 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2874 inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
2876 { /// @note This is time critical function!
2880 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2881 int local_count = count;
2884 if (count <= 0) { //Wrong count!
2888 while (i < local_count) {
2889 //Get value from buffer
2890 value = *(instance->circ_buf.buf + pos);
2892 if (instance->ao_idx & 0x1) {
2896 outl(value, instance->fifo_reg);
2897 //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);
2900 pos &= instance->circ_buf.mask;
2901 if (pos == instance->circ_buf.head) {
2902 pos = instance->circ_buf.tail;
2907 status = inl(instance->status_reg);
2908 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full before all datas were copied!
2909 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2911 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2912 } else { //Add last value
2913 value = *(instance->circ_buf.buf + pos);
2914 if (instance->ao_idx & 0x1) {
2918 outl(value, instance->fifo_reg);
2919 //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);
2922 PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
2927 /** @brief Copy data from software buffer to fifo (fast).
2928 * @note This is time critical function. Checking is done at begining and end only.
2929 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2931 * @param instance The subdevice instance (pointer).
2932 * @param count Maximum number of copied data.
2933 * @param start_pos Position of the firs value in buffer.
2935 * @return On success: Number of copied data.
2936 * @return On error/success: 0. No datas were copied => no data in buffer.
2937 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2939 inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
2941 { /// @note This is time critical function!
2945 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2946 int local_count = count;
2950 if (count <= 0) { //Wrong count!
2954 max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
2955 if (max_count <= 0) { //No data to copy!
2959 if (max_count < count) {
2960 local_count = max_count;
2963 while (i < local_count) {
2964 //Get value from buffer
2965 value = *(instance->circ_buf.buf + pos);
2967 if (instance->ao_idx & 0x1) {
2971 outl(value, instance->fifo_reg);
2972 //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);
2975 pos &= instance->circ_buf.mask;
2979 status = inl(instance->status_reg);
2980 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full before all datas were copied!
2981 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2983 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2984 } else { //Add last value
2985 value = *(instance->circ_buf.buf + pos);
2986 if (instance->ao_idx & 0x1) {
2990 outl(value, instance->fifo_reg);
2991 //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);
2994 PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
2998 /** @brief Copy data from software buffer to fifo (slow).
2999 * @note This is slow function that copy all data from buffer to FIFO with full control.
3001 * @param instance The subdevice instance (pointer).
3002 * @param count Maximum number of copied data.
3003 * @param start_pos Position of the firs value in buffer.
3005 * @return On success: Number of copied values.
3006 * @return On error/success: 0. FIFO was full at begining.
3007 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
3009 inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
3011 { /// @note This is slow function!
3015 (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
3016 int local_count = count;
3020 if (count <= 0) { //Wrong count!
3021 PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
3025 max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
3026 if (max_count <= 0) { //No data to copy!
3027 PERROR("idx=%d SLOW LOADED: No data to copy!\n",
3032 if (max_count < count) {
3033 local_count = max_count;
3036 for (i = 0; i < local_count; i++) {
3037 status = inl(instance->status_reg);
3038 if (!(status & ME6000_AO_STATUS_BIT_FF)) { //FIFO is full!
3041 //Get value from buffer
3042 value = *(instance->circ_buf.buf + pos);
3044 if (instance->ao_idx & 0x1) {
3048 outl(value, instance->fifo_reg);
3049 //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);
3052 pos &= instance->circ_buf.mask;
3055 PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
3059 /** @brief Copy data from user space to circular buffer.
3060 * @param instance The subdevice instance (pointer).
3061 * @param count Number of datas in user space.
3062 * @param user_values Buffer's pointer.
3064 * @return On success: Number of copied values.
3065 * @return On error: -ME_ERRNO_INTERNAL.
3067 inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
3075 empty_space = me_circ_buf_space(&instance->circ_buf);
3076 //We have only this space free.
3077 copied = (count < empty_space) ? count : empty_space;
3078 for (i = 0; i < copied; i++) { //Copy from user to buffer
3079 if ((err = get_user(value, (int *)(user_values + i)))) {
3081 ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
3082 instance->ao_idx, user_values + i, err);
3083 return -ME_ERRNO_INTERNAL;
3085 /// @note The analog output in me6000 series has size of 16 bits.
3086 *(instance->circ_buf.buf + instance->circ_buf.head) =
3088 instance->circ_buf.head++;
3089 instance->circ_buf.head &= instance->circ_buf.mask;
3092 PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
3096 static void me6000_ao_work_control_task(struct work_struct *work)
3098 me6000_ao_subdevice_t *instance;
3099 unsigned long cpu_flags = 0;
3105 uint32_t single_mask;
3108 container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
3109 PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
3112 status = inl(instance->status_reg);
3113 PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
3114 instance->status_reg - instance->reg_base, status);
3116 /// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
3117 // single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
3118 single_mask = *instance->triggering_flags & (0x1 << instance->ao_idx);
3120 switch (instance->status) { // Checking actual mode.
3122 // Not configured for work.
3123 case ao_status_none:
3126 //This are stable modes. No need to do anything. (?)
3127 case ao_status_single_configured:
3128 case ao_status_stream_configured:
3129 case ao_status_stream_fifo_error:
3130 case ao_status_stream_buffer_error:
3131 case ao_status_stream_error:
3132 PERROR("Shouldn't be running!.\n");
3136 case ao_status_single_run_wait:
3137 case ao_status_single_run:
3138 case ao_status_single_end_wait:
3139 if (instance->fifo) { // Extra registers.
3140 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working.
3141 if (((instance->fifo & ME6000_AO_HAS_FIFO)
3142 && (!(status & ME6000_AO_STATUS_BIT_EF)))
3143 || (!(instance->fifo & ME6000_AO_HAS_FIFO))) { // Single is in end state.
3145 ("Single call has been complited.\n");
3147 // Set correct value for single_read();
3148 instance->single_value =
3149 instance->single_value_in_fifo;
3151 // Set status as 'ao_status_single_end'
3152 instance->status = ao_status_single_end;
3154 spin_lock(instance->preload_reg_lock);
3155 if ((single_mask) && (*instance->preload_flags & (ME6000_AO_SYNC_HOLD << instance->ao_idx))) { // This is one of synchronous start channels. Set all as triggered.
3156 *instance->triggering_flags =
3159 //Set this channel as triggered (none active).
3160 *instance->triggering_flags &=
3161 ~(0x1 << instance->ao_idx);
3163 spin_unlock(instance->preload_reg_lock);
3167 // Wait for stop ISM.
3174 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3175 PDEBUG("Timeout reached.\n");
3176 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3177 spin_lock_irqsave(&instance->subdevice_lock,
3179 ctrl = inl(instance->ctrl_reg);
3181 ME6000_AO_CTRL_BIT_STOP |
3182 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3184 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3185 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3187 ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
3188 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
3190 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
3192 outl(ctrl, instance->ctrl_reg);
3193 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3195 instance->ctrl_reg -
3196 instance->reg_base, ctrl);
3197 spin_unlock_irqrestore(&instance->
3201 //Reset interrupt latch
3202 inl(instance->irq_reset_reg);
3204 spin_lock(instance->preload_reg_lock);
3205 //Remove from synchronous start. Block triggering from this output.
3206 synch = inl(instance->preload_reg);
3208 ~((ME6000_AO_SYNC_HOLD |
3209 ME6000_AO_SYNC_EXT_TRIG) << instance->
3211 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO - set to single safe mode
3213 ME6000_AO_SYNC_HOLD << instance->
3216 outl(synch, instance->preload_reg);
3218 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3220 instance->preload_reg - instance->reg_base,
3222 //Set this channel as triggered (none active).
3223 *instance->triggering_flags &=
3224 ~(0x1 << instance->ao_idx);
3225 spin_unlock(instance->preload_reg_lock);
3227 // Set correct value for single_read();
3228 instance->single_value_in_fifo =
3229 instance->single_value;
3231 instance->status = ao_status_single_end;
3236 } else { // No extra registers.
3238 if (!(status & single_mask))
3239 {// State machine is not working.
3240 PDEBUG("Single call has been complited.\n");
3242 // Set correct value for single_read();
3243 instance->single_value = instance->single_value_in_fifo;
3245 // Set status as 'ao_status_single_end'
3246 instance->status = ao_status_single_end;
3250 // Wait for stop ISM.
3256 if (!single_mask) { // Was triggered.
3257 PDEBUG("Single call has been complited.\n");
3259 // Set correct value for single_read();
3260 instance->single_value =
3261 instance->single_value_in_fifo;
3263 // Set status as 'ao_status_single_end'
3264 instance->status = ao_status_single_end;
3272 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3273 PDEBUG("Timeout reached.\n");
3275 spin_lock(instance->preload_reg_lock);
3276 //Remove from synchronous start. Block triggering from this output.
3277 synch = inl(instance->preload_reg);
3279 ~(ME6000_AO_SYNC_EXT_TRIG << instance->
3282 ME6000_AO_SYNC_HOLD << instance->ao_idx;
3284 outl(synch, instance->preload_reg);
3286 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3288 instance->preload_reg - instance->reg_base,
3290 //Set this channel as triggered (none active).
3291 *instance->triggering_flags &=
3292 ~(0x1 << instance->ao_idx);
3293 spin_unlock(instance->preload_reg_lock);
3295 // Restore old settings.
3296 PDEBUG("Write old value back to register.\n");
3297 outl(instance->single_value,
3298 instance->single_reg);
3300 ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3302 instance->single_reg - instance->reg_base,
3303 instance->single_value);
3305 // Set correct value for single_read();
3306 instance->single_value_in_fifo =
3307 instance->single_value;
3309 instance->status = ao_status_single_end;
3320 case ao_status_stream_end:
3321 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3323 ("Streaming on single device! This feature is not implemented in this version!\n");
3324 instance->status = ao_status_stream_error;
3329 case ao_status_single_end:
3330 if (instance->fifo) { // Extra registers.
3331 if (status & ME6000_AO_STATUS_BIT_FSM) { // State machine is working but the status is set to end. Force stop.
3337 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3338 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3339 ctrl = inl(instance->ctrl_reg);
3341 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
3342 ME6000_AO_CTRL_BIT_STOP;
3344 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3345 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3346 outl(ctrl, instance->ctrl_reg);
3347 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3349 instance->ctrl_reg - instance->reg_base,
3351 spin_unlock_irqrestore(&instance->subdevice_lock,
3354 //Reset interrupt latch
3355 inl(instance->irq_reset_reg);
3356 } else { // No extra registers.
3358 if (status & single_mask)
3359 {// State machine is working but the status is set to end. Force stop.
3369 case ao_status_stream_run_wait:
3370 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3372 ("Streaming on single device! This feature is not implemented in this version!\n");
3373 instance->status = ao_status_stream_error;
3379 if (status & ME6000_AO_STATUS_BIT_FSM) { // State machine is working. Waiting for start finish.
3380 instance->status = ao_status_stream_run;
3382 // Signal end of this step
3384 } else { // State machine is not working.
3385 if (!(status & ME6000_AO_STATUS_BIT_EF)) { // FIFO is empty. Procedure has started and finish already!
3386 instance->status = ao_status_stream_end;
3397 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
3398 PDEBUG("Timeout reached.\n");
3399 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3400 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3401 ctrl = inl(instance->ctrl_reg);
3403 ME6000_AO_CTRL_BIT_STOP |
3404 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3406 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3407 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3408 outl(ctrl, instance->ctrl_reg);
3409 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3411 instance->ctrl_reg - instance->reg_base,
3413 spin_unlock_irqrestore(&instance->subdevice_lock,
3416 //Reset interrupt latch
3417 inl(instance->irq_reset_reg);
3419 spin_lock(instance->preload_reg_lock);
3420 //Remove from synchronous start. Block triggering from this output.
3421 synch = inl(instance->preload_reg);
3423 ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
3425 outl(synch, instance->preload_reg);
3426 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3428 instance->preload_reg - instance->reg_base,
3430 spin_unlock(instance->preload_reg_lock);
3432 instance->status = ao_status_stream_end;
3441 case ao_status_stream_run:
3442 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3444 ("Streaming on single device! This feature is not implemented in this version!\n");
3445 instance->status = ao_status_stream_error;
3451 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working. This is an error.
3453 if (!(status & ME6000_AO_STATUS_BIT_EF)) { // FIFO is empty.
3454 if (me_circ_buf_values(&instance->circ_buf)) { // Software buffer is not empty.
3455 if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. Requed data shown.
3457 ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3459 ao_status_stream_end;
3462 ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3464 ao_status_stream_buffer_error;
3466 } else { // Software buffer is empty.
3468 ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3469 instance->status = ao_status_stream_end;
3471 } else { // There are still datas in FIFO.
3472 if (me_circ_buf_values(&instance->circ_buf)) { // Software buffer is not empty.
3474 ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3475 } else { // Software buffer is empty.
3477 ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3479 instance->status = ao_status_stream_fifo_error;
3483 // Signal the failure.
3491 case ao_status_stream_end_wait:
3492 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) { // No FIFO
3494 ("Streaming on single device! This feature is not implemented in this version!\n");
3495 instance->status = ao_status_stream_error;
3501 if (!(status & ME6000_AO_STATUS_BIT_FSM)) { // State machine is not working. Waiting for stop finish.
3502 instance->status = ao_status_stream_end;
3505 // State machine is working.
3510 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3512 instance->status = ao_status_stream_error;
3519 if (signaling) { //Signal it.
3520 wake_up_interruptible_all(&instance->wait_queue);
3523 if (instance->ao_control_task_flag && reschedule) { // Reschedule task
3524 queue_delayed_work(instance->me6000_workqueue,
3525 &instance->ao_control_task, 1);
3527 PINFO("<%s> Ending control task.\n", __func__);
3532 static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
3535 int *max, int *maxdata, int *range)
3537 me6000_ao_subdevice_t *instance;
3539 instance = (me6000_ao_subdevice_t *) subdevice;
3541 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3543 if ((*max - *min) < 0) {
3544 PERROR("Invalid minimum and maximum values specified.\n");
3545 return ME_ERRNO_INVALID_MIN_MAX;
3548 if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3549 if ((*max <= (instance->max + 1000)) && (*min >= instance->min)) {
3550 *min = instance->min;
3551 *max = instance->max;
3552 *maxdata = ME6000_AO_MAX_DATA;
3555 PERROR("No matching range available.\n");
3556 return ME_ERRNO_NO_RANGE;
3559 PERROR("Invalid physical unit specified.\n");
3560 return ME_ERRNO_INVALID_UNIT;
3563 return ME_ERRNO_SUCCESS;
3566 static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
3567 int unit, int *count)
3569 me6000_ao_subdevice_t *instance;
3571 instance = (me6000_ao_subdevice_t *) subdevice;
3573 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3575 if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3581 return ME_ERRNO_SUCCESS;
3584 static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
3587 int *min, int *max, int *maxdata)
3589 me6000_ao_subdevice_t *instance;
3591 instance = (me6000_ao_subdevice_t *) subdevice;
3593 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3596 *unit = ME_UNIT_VOLT;
3597 *min = instance->min;
3598 *max = instance->max;
3599 *maxdata = ME6000_AO_MAX_DATA;
3601 PERROR("Invalid range number specified.\n");
3602 return ME_ERRNO_INVALID_RANGE;
3605 return ME_ERRNO_SUCCESS;
3608 static int me6000_ao_query_timer(me_subdevice_t *subdevice,
3610 int *base_frequency,
3611 long long *min_ticks, long long *max_ticks)
3613 me6000_ao_subdevice_t *instance;
3615 instance = (me6000_ao_subdevice_t *) subdevice;
3617 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3619 if (instance->fifo) { //Streaming device.
3620 *base_frequency = ME6000_AO_BASE_FREQUENCY;
3621 if (timer == ME_TIMER_ACQ_START) {
3622 *min_ticks = ME6000_AO_MIN_ACQ_TICKS;
3623 *max_ticks = ME6000_AO_MAX_ACQ_TICKS;
3624 } else if (timer == ME_TIMER_CONV_START) {
3625 *min_ticks = ME6000_AO_MIN_CHAN_TICKS;
3626 *max_ticks = ME6000_AO_MAX_CHAN_TICKS;
3628 } else { //Not streaming device!
3629 *base_frequency = 0;
3634 return ME_ERRNO_SUCCESS;
3637 static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
3640 me6000_ao_subdevice_t *instance;
3641 instance = (me6000_ao_subdevice_t *) subdevice;
3643 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3646 return ME_ERRNO_SUCCESS;
3649 static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
3650 int *type, int *subtype)
3652 me6000_ao_subdevice_t *instance;
3654 instance = (me6000_ao_subdevice_t *) subdevice;
3656 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3661 fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
3664 return ME_ERRNO_SUCCESS;
3667 static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
3669 me6000_ao_subdevice_t *instance;
3670 instance = (me6000_ao_subdevice_t *) subdevice;
3672 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3675 ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
3678 return ME_ERRNO_SUCCESS;
3681 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
3682 int cap, int *args, int count)
3684 me6000_ao_subdevice_t *instance;
3685 int err = ME_ERRNO_SUCCESS;
3687 instance = (me6000_ao_subdevice_t *) subdevice;
3689 PDEBUG("executed. idx=%d\n", instance->ao_idx);
3692 PERROR("Invalid capability argument count.\n");
3693 return ME_ERRNO_INVALID_CAP_ARG_COUNT;
3697 case ME_CAP_AI_FIFO_SIZE:
3698 args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
3701 case ME_CAP_AI_BUFFER_SIZE:
3703 (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
3707 PERROR("Invalid capability.\n");
3708 err = ME_ERRNO_INVALID_CAP;