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 IRQF_DISABLED
| IRQF_SHARED
,
2631 ME6000_NAME
, subdevice
)) {
2632 PERROR("Cannot get interrupt line.\n");
2633 PDEBUG("free circ_buf = %p size=%d",
2634 subdevice
->circ_buf
.buf
,
2635 PAGE_SHIFT
<< ME6000_AO_CIRC_BUF_SIZE_ORDER
);
2636 free_pages((unsigned long)subdevice
->circ_buf
.buf
,
2637 ME6000_AO_CIRC_BUF_SIZE_ORDER
);
2638 subdevice
->circ_buf
.buf
= NULL
;
2642 PINFO("Registered irq=%d.\n", subdevice
->irq
);
2647 /* Initialize registers */
2648 // Only streamed subdevices support interrupts. For the rest this register has no meaning.
2649 subdevice
->irq_status_reg
= reg_base
+ ME6000_AO_IRQ_STATUS_REG
;
2650 subdevice
->preload_reg
= reg_base
+ ME6000_AO_PRELOAD_REG
;
2653 subdevice
->ctrl_reg
= reg_base
+ ME6000_AO_00_CTRL_REG
;
2654 subdevice
->status_reg
= reg_base
+ ME6000_AO_00_STATUS_REG
;
2655 subdevice
->fifo_reg
= reg_base
+ ME6000_AO_00_FIFO_REG
;
2656 subdevice
->timer_reg
= reg_base
+ ME6000_AO_00_TIMER_REG
;
2657 subdevice
->irq_reset_reg
=
2658 reg_base
+ ME6000_AO_00_IRQ_RESET_REG
;
2659 subdevice
->single_reg
= reg_base
+ ME6000_AO_00_SINGLE_REG
;
2660 } else if (ao_idx
== 1) {
2661 subdevice
->ctrl_reg
= reg_base
+ ME6000_AO_01_CTRL_REG
;
2662 subdevice
->status_reg
= reg_base
+ ME6000_AO_01_STATUS_REG
;
2663 subdevice
->fifo_reg
= reg_base
+ ME6000_AO_01_FIFO_REG
;
2664 subdevice
->timer_reg
= reg_base
+ ME6000_AO_01_TIMER_REG
;
2665 subdevice
->irq_reset_reg
=
2666 reg_base
+ ME6000_AO_01_IRQ_RESET_REG
;
2667 subdevice
->single_reg
= reg_base
+ ME6000_AO_01_SINGLE_REG
;
2668 } else if (ao_idx
== 2) {
2669 subdevice
->ctrl_reg
= reg_base
+ ME6000_AO_02_CTRL_REG
;
2670 subdevice
->status_reg
= reg_base
+ ME6000_AO_02_STATUS_REG
;
2671 subdevice
->fifo_reg
= reg_base
+ ME6000_AO_02_FIFO_REG
;
2672 subdevice
->timer_reg
= reg_base
+ ME6000_AO_02_TIMER_REG
;
2673 subdevice
->irq_reset_reg
=
2674 reg_base
+ ME6000_AO_02_IRQ_RESET_REG
;
2675 subdevice
->single_reg
= reg_base
+ ME6000_AO_02_SINGLE_REG
;
2676 } else if (ao_idx
== 3) {
2677 subdevice
->ctrl_reg
= reg_base
+ ME6000_AO_03_CTRL_REG
;
2678 subdevice
->status_reg
= reg_base
+ ME6000_AO_03_STATUS_REG
;
2679 subdevice
->fifo_reg
= reg_base
+ ME6000_AO_03_FIFO_REG
;
2680 subdevice
->timer_reg
= reg_base
+ ME6000_AO_03_TIMER_REG
;
2681 subdevice
->irq_reset_reg
=
2682 reg_base
+ ME6000_AO_03_IRQ_RESET_REG
;
2683 subdevice
->single_reg
= reg_base
+ ME6000_AO_03_SINGLE_REG
;
2685 subdevice
->ctrl_reg
= reg_base
+ ME6000_AO_DUMY
;
2686 subdevice
->fifo_reg
= reg_base
+ ME6000_AO_DUMY
;
2687 subdevice
->timer_reg
= reg_base
+ ME6000_AO_DUMY
;
2688 subdevice
->irq_reset_reg
= reg_base
+ ME6000_AO_DUMY
;
2689 subdevice
->single_reg
= reg_base
+ ME6000_AO_DUMY
;
2691 subdevice
->status_reg
= reg_base
+ ME6000_AO_SINGLE_STATUS_REG
;
2693 subdevice
->single_reg
=
2694 reg_base
+ ME6000_AO_04_SINGLE_REG
;
2695 } else if (ao_idx
== 5) {
2696 subdevice
->single_reg
=
2697 reg_base
+ ME6000_AO_05_SINGLE_REG
;
2698 } else if (ao_idx
== 6) {
2699 subdevice
->single_reg
=
2700 reg_base
+ ME6000_AO_06_SINGLE_REG
;
2701 } else if (ao_idx
== 7) {
2702 subdevice
->single_reg
=
2703 reg_base
+ ME6000_AO_07_SINGLE_REG
;
2704 } else if (ao_idx
== 8) {
2705 subdevice
->single_reg
=
2706 reg_base
+ ME6000_AO_08_SINGLE_REG
;
2707 } else if (ao_idx
== 9) {
2708 subdevice
->single_reg
=
2709 reg_base
+ ME6000_AO_09_SINGLE_REG
;
2710 } else if (ao_idx
== 10) {
2711 subdevice
->single_reg
=
2712 reg_base
+ ME6000_AO_10_SINGLE_REG
;
2713 } else if (ao_idx
== 11) {
2714 subdevice
->single_reg
=
2715 reg_base
+ ME6000_AO_11_SINGLE_REG
;
2716 } else if (ao_idx
== 12) {
2717 subdevice
->single_reg
=
2718 reg_base
+ ME6000_AO_12_SINGLE_REG
;
2719 } else if (ao_idx
== 13) {
2720 subdevice
->single_reg
=
2721 reg_base
+ ME6000_AO_13_SINGLE_REG
;
2722 } else if (ao_idx
== 14) {
2723 subdevice
->single_reg
=
2724 reg_base
+ ME6000_AO_14_SINGLE_REG
;
2725 } else if (ao_idx
== 15) {
2726 subdevice
->single_reg
=
2727 reg_base
+ ME6000_AO_15_SINGLE_REG
;
2729 PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx
);
2730 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
2731 if (subdevice
->fifo
) {
2732 free_pages((unsigned long)subdevice
->circ_buf
.
2733 buf
, ME6000_AO_CIRC_BUF_SIZE_ORDER
);
2735 subdevice
->circ_buf
.buf
= NULL
;
2740 #ifdef MEDEBUG_DEBUG_REG
2741 subdevice
->reg_base
= reg_base
;
2744 /* Override base class methods. */
2745 subdevice
->base
.me_subdevice_destructor
= me6000_ao_destructor
;
2746 subdevice
->base
.me_subdevice_io_reset_subdevice
=
2747 me6000_ao_io_reset_subdevice
;
2748 subdevice
->base
.me_subdevice_io_single_config
=
2749 me6000_ao_io_single_config
;
2750 subdevice
->base
.me_subdevice_io_single_read
= me6000_ao_io_single_read
;
2751 subdevice
->base
.me_subdevice_io_single_write
=
2752 me6000_ao_io_single_write
;
2753 subdevice
->base
.me_subdevice_io_stream_config
=
2754 me6000_ao_io_stream_config
;
2755 subdevice
->base
.me_subdevice_io_stream_new_values
=
2756 me6000_ao_io_stream_new_values
;
2757 subdevice
->base
.me_subdevice_io_stream_write
=
2758 me6000_ao_io_stream_write
;
2759 subdevice
->base
.me_subdevice_io_stream_start
=
2760 me6000_ao_io_stream_start
;
2761 subdevice
->base
.me_subdevice_io_stream_status
=
2762 me6000_ao_io_stream_status
;
2763 subdevice
->base
.me_subdevice_io_stream_stop
= me6000_ao_io_stream_stop
;
2764 subdevice
->base
.me_subdevice_query_number_channels
=
2765 me6000_ao_query_number_channels
;
2766 subdevice
->base
.me_subdevice_query_subdevice_type
=
2767 me6000_ao_query_subdevice_type
;
2768 subdevice
->base
.me_subdevice_query_subdevice_caps
=
2769 me6000_ao_query_subdevice_caps
;
2770 subdevice
->base
.me_subdevice_query_subdevice_caps_args
=
2771 me6000_ao_query_subdevice_caps_args
;
2772 subdevice
->base
.me_subdevice_query_range_by_min_max
=
2773 me6000_ao_query_range_by_min_max
;
2774 subdevice
->base
.me_subdevice_query_number_ranges
=
2775 me6000_ao_query_number_ranges
;
2776 subdevice
->base
.me_subdevice_query_range_info
=
2777 me6000_ao_query_range_info
;
2778 subdevice
->base
.me_subdevice_query_timer
= me6000_ao_query_timer
;
2780 //prepare work queue and work function
2781 subdevice
->me6000_workqueue
= me6000_wq
;
2783 /* workqueue API changed in kernel 2.6.20 */
2784 INIT_DELAYED_WORK(&subdevice
->ao_control_task
,
2785 me6000_ao_work_control_task
);
2787 if (subdevice
->fifo
) { //Set speed
2788 outl(ME6000_AO_MIN_CHAN_TICKS
- 1, subdevice
->timer_reg
);
2789 subdevice
->hardware_stop_delay
= HZ
/ 10; //100ms
2795 /** @brief Stop presentation. Preserve FIFOs.
2797 * @param instance The subdevice instance (pointer).
2799 inline int ao_stop_immediately(me6000_ao_subdevice_t
*instance
)
2801 unsigned long cpu_flags
;
2805 uint32_t single_mask
;
2807 if (instance
->ao_idx
< ME6000_AO_SINGLE_STATUS_OFFSET
)
2808 single_mask
= 0x0000;
2810 single_mask
= 0x0001 << (instance
->ao_idx
-
2811 ME6000_AO_SINGLE_STATUS_OFFSET
);
2814 (instance
->hardware_stop_delay
>
2815 (HZ
/ 10)) ? instance
->hardware_stop_delay
: HZ
/ 10;
2816 for (i
= 0; i
<= timeout
; i
++) {
2817 if (instance
->fifo
) {
2818 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2819 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2820 ctrl
= inl(instance
->ctrl_reg
);
2822 ME6000_AO_CTRL_BIT_STOP
|
2823 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP
;
2825 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ
|
2826 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG
);
2827 outl(ctrl
, instance
->ctrl_reg
);
2828 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2830 instance
->ctrl_reg
- instance
->reg_base
,
2832 spin_unlock_irqrestore(&instance
->subdevice_lock
,
2835 if (!(inl(instance
->status_reg
) & ME6000_AO_STATUS_BIT_FSM
)) { // Exit.
2839 if (!(inl(instance
->status_reg
) & single_mask
)) { // Exit.
2844 PINFO("<%s> Wait for stop: %d\n", __func__
, i
);
2847 set_current_state(TASK_INTERRUPTIBLE
);
2848 schedule_timeout(1);
2852 PERROR_CRITICAL("FSM IS BUSY!\n");
2853 return ME_ERRNO_INTERNAL
;
2855 return ME_ERRNO_SUCCESS
;
2858 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2859 * @note This is time critical function. Checking is done at begining and end only.
2860 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2862 * @param instance The subdevice instance (pointer).
2863 * @param count Maximum number of copied data.
2864 * @param start_pos Position of the firs value in buffer.
2866 * @return On success: Number of copied data.
2867 * @return On error/success: 0. No datas were copied => no data in buffer.
2868 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2870 inline int ao_write_data_wraparound(me6000_ao_subdevice_t
*instance
, int count
,
2872 { /// @note This is time critical function!
2876 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2877 int local_count
= count
;
2880 if (count
<= 0) { //Wrong count!
2884 while (i
< local_count
) {
2885 //Get value from buffer
2886 value
= *(instance
->circ_buf
.buf
+ pos
);
2888 if (instance
->ao_idx
& 0x1) {
2892 outl(value
, instance
->fifo_reg
);
2893 //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);
2896 pos
&= instance
->circ_buf
.mask
;
2897 if (pos
== instance
->circ_buf
.head
) {
2898 pos
= instance
->circ_buf
.tail
;
2903 status
= inl(instance
->status_reg
);
2904 if (!(status
& ME6000_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2905 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2907 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2908 } else { //Add last value
2909 value
= *(instance
->circ_buf
.buf
+ pos
);
2910 if (instance
->ao_idx
& 0x1) {
2914 outl(value
, instance
->fifo_reg
);
2915 //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);
2918 PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance
->ao_idx
,
2923 /** @brief Copy data from software buffer to fifo (fast).
2924 * @note This is time critical function. Checking is done at begining and end only.
2925 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2927 * @param instance The subdevice instance (pointer).
2928 * @param count Maximum number of copied data.
2929 * @param start_pos Position of the firs value in buffer.
2931 * @return On success: Number of copied data.
2932 * @return On error/success: 0. No datas were copied => no data in buffer.
2933 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2935 inline int ao_write_data(me6000_ao_subdevice_t
*instance
, int count
,
2937 { /// @note This is time critical function!
2941 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2942 int local_count
= count
;
2946 if (count
<= 0) { //Wrong count!
2950 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
2951 if (max_count
<= 0) { //No data to copy!
2955 if (max_count
< count
) {
2956 local_count
= max_count
;
2959 while (i
< local_count
) {
2960 //Get value from buffer
2961 value
= *(instance
->circ_buf
.buf
+ pos
);
2963 if (instance
->ao_idx
& 0x1) {
2967 outl(value
, instance
->fifo_reg
);
2968 //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);
2971 pos
&= instance
->circ_buf
.mask
;
2975 status
= inl(instance
->status_reg
);
2976 if (!(status
& ME6000_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2977 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2979 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2980 } else { //Add last value
2981 value
= *(instance
->circ_buf
.buf
+ pos
);
2982 if (instance
->ao_idx
& 0x1) {
2986 outl(value
, instance
->fifo_reg
);
2987 //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);
2990 PINFO("idx=%d FAST LOADED %d values\n", instance
->ao_idx
, local_count
);
2994 /** @brief Copy data from software buffer to fifo (slow).
2995 * @note This is slow function that copy all data from buffer to FIFO with full control.
2997 * @param instance The subdevice instance (pointer).
2998 * @param count Maximum number of copied data.
2999 * @param start_pos Position of the firs value in buffer.
3001 * @return On success: Number of copied values.
3002 * @return On error/success: 0. FIFO was full at begining.
3003 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
3005 inline int ao_write_data_pooling(me6000_ao_subdevice_t
*instance
, int count
,
3007 { /// @note This is slow function!
3011 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
3012 int local_count
= count
;
3016 if (count
<= 0) { //Wrong count!
3017 PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance
->ao_idx
);
3021 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
3022 if (max_count
<= 0) { //No data to copy!
3023 PERROR("idx=%d SLOW LOADED: No data to copy!\n",
3028 if (max_count
< count
) {
3029 local_count
= max_count
;
3032 for (i
= 0; i
< local_count
; i
++) {
3033 status
= inl(instance
->status_reg
);
3034 if (!(status
& ME6000_AO_STATUS_BIT_FF
)) { //FIFO is full!
3037 //Get value from buffer
3038 value
= *(instance
->circ_buf
.buf
+ pos
);
3040 if (instance
->ao_idx
& 0x1) {
3044 outl(value
, instance
->fifo_reg
);
3045 //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);
3048 pos
&= instance
->circ_buf
.mask
;
3051 PINFO("idx=%d SLOW LOADED %d values\n", instance
->ao_idx
, local_count
);
3055 /** @brief Copy data from user space to circular buffer.
3056 * @param instance The subdevice instance (pointer).
3057 * @param count Number of datas in user space.
3058 * @param user_values Buffer's pointer.
3060 * @return On success: Number of copied values.
3061 * @return On error: -ME_ERRNO_INTERNAL.
3063 inline int ao_get_data_from_user(me6000_ao_subdevice_t
*instance
, int count
,
3071 empty_space
= me_circ_buf_space(&instance
->circ_buf
);
3072 //We have only this space free.
3073 copied
= (count
< empty_space
) ? count
: empty_space
;
3074 for (i
= 0; i
< copied
; i
++) { //Copy from user to buffer
3075 if ((err
= get_user(value
, (int *)(user_values
+ i
)))) {
3077 ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
3078 instance
->ao_idx
, user_values
+ i
, err
);
3079 return -ME_ERRNO_INTERNAL
;
3081 /// @note The analog output in me6000 series has size of 16 bits.
3082 *(instance
->circ_buf
.buf
+ instance
->circ_buf
.head
) =
3084 instance
->circ_buf
.head
++;
3085 instance
->circ_buf
.head
&= instance
->circ_buf
.mask
;
3088 PINFO("idx=%d BUFFER LOADED %d values\n", instance
->ao_idx
, copied
);
3092 static void me6000_ao_work_control_task(struct work_struct
*work
)
3094 me6000_ao_subdevice_t
*instance
;
3095 unsigned long cpu_flags
= 0;
3101 uint32_t single_mask
;
3104 container_of((void *)work
, me6000_ao_subdevice_t
, ao_control_task
);
3105 PINFO("<%s: %ld> executed. idx=%d\n", __func__
, jiffies
,
3108 status
= inl(instance
->status_reg
);
3109 PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
3110 instance
->status_reg
- instance
->reg_base
, status
);
3112 /// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
3113 // single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
3114 single_mask
= *instance
->triggering_flags
& (0x1 << instance
->ao_idx
);
3116 switch (instance
->status
) { // Checking actual mode.
3118 // Not configured for work.
3119 case ao_status_none
:
3122 //This are stable modes. No need to do anything. (?)
3123 case ao_status_single_configured
:
3124 case ao_status_stream_configured
:
3125 case ao_status_stream_fifo_error
:
3126 case ao_status_stream_buffer_error
:
3127 case ao_status_stream_error
:
3128 PERROR("Shouldn't be running!.\n");
3132 case ao_status_single_run_wait
:
3133 case ao_status_single_run
:
3134 case ao_status_single_end_wait
:
3135 if (instance
->fifo
) { // Extra registers.
3136 if (!(status
& ME6000_AO_STATUS_BIT_FSM
)) { // State machine is not working.
3137 if (((instance
->fifo
& ME6000_AO_HAS_FIFO
)
3138 && (!(status
& ME6000_AO_STATUS_BIT_EF
)))
3139 || (!(instance
->fifo
& ME6000_AO_HAS_FIFO
))) { // Single is in end state.
3141 ("Single call has been complited.\n");
3143 // Set correct value for single_read();
3144 instance
->single_value
=
3145 instance
->single_value_in_fifo
;
3147 // Set status as 'ao_status_single_end'
3148 instance
->status
= ao_status_single_end
;
3150 spin_lock(instance
->preload_reg_lock
);
3151 if ((single_mask
) && (*instance
->preload_flags
& (ME6000_AO_SYNC_HOLD
<< instance
->ao_idx
))) { // This is one of synchronous start channels. Set all as triggered.
3152 *instance
->triggering_flags
=
3155 //Set this channel as triggered (none active).
3156 *instance
->triggering_flags
&=
3157 ~(0x1 << instance
->ao_idx
);
3159 spin_unlock(instance
->preload_reg_lock
);
3163 // Wait for stop ISM.
3170 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3171 PDEBUG("Timeout reached.\n");
3172 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3173 spin_lock_irqsave(&instance
->subdevice_lock
,
3175 ctrl
= inl(instance
->ctrl_reg
);
3177 ME6000_AO_CTRL_BIT_STOP
|
3178 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP
;
3180 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ
|
3181 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3183 ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE
|
3184 ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
3186 ctrl
&= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO
;
3188 outl(ctrl
, instance
->ctrl_reg
);
3189 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3191 instance
->ctrl_reg
-
3192 instance
->reg_base
, ctrl
);
3193 spin_unlock_irqrestore(&instance
->
3197 //Reset interrupt latch
3198 inl(instance
->irq_reset_reg
);
3200 spin_lock(instance
->preload_reg_lock
);
3201 //Remove from synchronous start. Block triggering from this output.
3202 synch
= inl(instance
->preload_reg
);
3204 ~((ME6000_AO_SYNC_HOLD
|
3205 ME6000_AO_SYNC_EXT_TRIG
) << instance
->
3207 if (!(instance
->fifo
& ME6000_AO_HAS_FIFO
)) { // No FIFO - set to single safe mode
3209 ME6000_AO_SYNC_HOLD
<< instance
->
3212 outl(synch
, instance
->preload_reg
);
3214 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3216 instance
->preload_reg
- instance
->reg_base
,
3218 //Set this channel as triggered (none active).
3219 *instance
->triggering_flags
&=
3220 ~(0x1 << instance
->ao_idx
);
3221 spin_unlock(instance
->preload_reg_lock
);
3223 // Set correct value for single_read();
3224 instance
->single_value_in_fifo
=
3225 instance
->single_value
;
3227 instance
->status
= ao_status_single_end
;
3232 } else { // No extra registers.
3234 if (!(status & single_mask))
3235 {// State machine is not working.
3236 PDEBUG("Single call has been complited.\n");
3238 // Set correct value for single_read();
3239 instance->single_value = instance->single_value_in_fifo;
3241 // Set status as 'ao_status_single_end'
3242 instance->status = ao_status_single_end;
3246 // Wait for stop ISM.
3252 if (!single_mask
) { // Was triggered.
3253 PDEBUG("Single call has been complited.\n");
3255 // Set correct value for single_read();
3256 instance
->single_value
=
3257 instance
->single_value_in_fifo
;
3259 // Set status as 'ao_status_single_end'
3260 instance
->status
= ao_status_single_end
;
3268 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3269 PDEBUG("Timeout reached.\n");
3271 spin_lock(instance
->preload_reg_lock
);
3272 //Remove from synchronous start. Block triggering from this output.
3273 synch
= inl(instance
->preload_reg
);
3275 ~(ME6000_AO_SYNC_EXT_TRIG
<< instance
->
3278 ME6000_AO_SYNC_HOLD
<< instance
->ao_idx
;
3280 outl(synch
, instance
->preload_reg
);
3282 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3284 instance
->preload_reg
- instance
->reg_base
,
3286 //Set this channel as triggered (none active).
3287 *instance
->triggering_flags
&=
3288 ~(0x1 << instance
->ao_idx
);
3289 spin_unlock(instance
->preload_reg_lock
);
3291 // Restore old settings.
3292 PDEBUG("Write old value back to register.\n");
3293 outl(instance
->single_value
,
3294 instance
->single_reg
);
3296 ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3298 instance
->single_reg
- instance
->reg_base
,
3299 instance
->single_value
);
3301 // Set correct value for single_read();
3302 instance
->single_value_in_fifo
=
3303 instance
->single_value
;
3305 instance
->status
= ao_status_single_end
;
3316 case ao_status_stream_end
:
3317 if (!(instance
->fifo
& ME6000_AO_HAS_FIFO
)) { // No FIFO
3319 ("Streaming on single device! This feature is not implemented in this version!\n");
3320 instance
->status
= ao_status_stream_error
;
3325 case ao_status_single_end
:
3326 if (instance
->fifo
) { // Extra registers.
3327 if (status
& ME6000_AO_STATUS_BIT_FSM
) { // State machine is working but the status is set to end. Force stop.
3333 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3334 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3335 ctrl
= inl(instance
->ctrl_reg
);
3337 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP
|
3338 ME6000_AO_CTRL_BIT_STOP
;
3340 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ
|
3341 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3342 outl(ctrl
, instance
->ctrl_reg
);
3343 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3345 instance
->ctrl_reg
- instance
->reg_base
,
3347 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3350 //Reset interrupt latch
3351 inl(instance
->irq_reset_reg
);
3352 } else { // No extra registers.
3354 if (status & single_mask)
3355 {// State machine is working but the status is set to end. Force stop.
3365 case ao_status_stream_run_wait
:
3366 if (!(instance
->fifo
& ME6000_AO_HAS_FIFO
)) { // No FIFO
3368 ("Streaming on single device! This feature is not implemented in this version!\n");
3369 instance
->status
= ao_status_stream_error
;
3375 if (status
& ME6000_AO_STATUS_BIT_FSM
) { // State machine is working. Waiting for start finish.
3376 instance
->status
= ao_status_stream_run
;
3378 // Signal end of this step
3380 } else { // State machine is not working.
3381 if (!(status
& ME6000_AO_STATUS_BIT_EF
)) { // FIFO is empty. Procedure has started and finish already!
3382 instance
->status
= ao_status_stream_end
;
3393 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3394 PDEBUG("Timeout reached.\n");
3395 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3396 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3397 ctrl
= inl(instance
->ctrl_reg
);
3399 ME6000_AO_CTRL_BIT_STOP
|
3400 ME6000_AO_CTRL_BIT_IMMEDIATE_STOP
;
3402 ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ
|
3403 ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3404 outl(ctrl
, instance
->ctrl_reg
);
3405 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3407 instance
->ctrl_reg
- instance
->reg_base
,
3409 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3412 //Reset interrupt latch
3413 inl(instance
->irq_reset_reg
);
3415 spin_lock(instance
->preload_reg_lock
);
3416 //Remove from synchronous start. Block triggering from this output.
3417 synch
= inl(instance
->preload_reg
);
3419 ~((ME6000_AO_SYNC_HOLD
| ME6000_AO_SYNC_EXT_TRIG
) <<
3421 outl(synch
, instance
->preload_reg
);
3422 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3424 instance
->preload_reg
- instance
->reg_base
,
3426 spin_unlock(instance
->preload_reg_lock
);
3428 instance
->status
= ao_status_stream_end
;
3437 case ao_status_stream_run
:
3438 if (!(instance
->fifo
& ME6000_AO_HAS_FIFO
)) { // No FIFO
3440 ("Streaming on single device! This feature is not implemented in this version!\n");
3441 instance
->status
= ao_status_stream_error
;
3447 if (!(status
& ME6000_AO_STATUS_BIT_FSM
)) { // State machine is not working. This is an error.
3449 if (!(status
& ME6000_AO_STATUS_BIT_EF
)) { // FIFO is empty.
3450 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3451 if (instance
->stop_data_count
&& (instance
->stop_data_count
<= instance
->data_count
)) { //Finishing work. Requed data shown.
3453 ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3455 ao_status_stream_end
;
3458 ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3460 ao_status_stream_buffer_error
;
3462 } else { // Software buffer is empty.
3464 ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3465 instance
->status
= ao_status_stream_end
;
3467 } else { // There are still datas in FIFO.
3468 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3470 ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3471 } else { // Software buffer is empty.
3473 ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3475 instance
->status
= ao_status_stream_fifo_error
;
3479 // Signal the failure.
3487 case ao_status_stream_end_wait
:
3488 if (!(instance
->fifo
& ME6000_AO_HAS_FIFO
)) { // No FIFO
3490 ("Streaming on single device! This feature is not implemented in this version!\n");
3491 instance
->status
= ao_status_stream_error
;
3497 if (!(status
& ME6000_AO_STATUS_BIT_FSM
)) { // State machine is not working. Waiting for stop finish.
3498 instance
->status
= ao_status_stream_end
;
3501 // State machine is working.
3506 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3508 instance
->status
= ao_status_stream_error
;
3515 if (signaling
) { //Signal it.
3516 wake_up_interruptible_all(&instance
->wait_queue
);
3519 if (instance
->ao_control_task_flag
&& reschedule
) { // Reschedule task
3520 queue_delayed_work(instance
->me6000_workqueue
,
3521 &instance
->ao_control_task
, 1);
3523 PINFO("<%s> Ending control task.\n", __func__
);
3528 static int me6000_ao_query_range_by_min_max(me_subdevice_t
*subdevice
,
3531 int *max
, int *maxdata
, int *range
)
3533 me6000_ao_subdevice_t
*instance
;
3535 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3537 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3539 if ((*max
- *min
) < 0) {
3540 PERROR("Invalid minimum and maximum values specified.\n");
3541 return ME_ERRNO_INVALID_MIN_MAX
;
3544 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
3545 if ((*max
<= (instance
->max
+ 1000)) && (*min
>= instance
->min
)) {
3546 *min
= instance
->min
;
3547 *max
= instance
->max
;
3548 *maxdata
= ME6000_AO_MAX_DATA
;
3551 PERROR("No matching range available.\n");
3552 return ME_ERRNO_NO_RANGE
;
3555 PERROR("Invalid physical unit specified.\n");
3556 return ME_ERRNO_INVALID_UNIT
;
3559 return ME_ERRNO_SUCCESS
;
3562 static int me6000_ao_query_number_ranges(me_subdevice_t
*subdevice
,
3563 int unit
, int *count
)
3565 me6000_ao_subdevice_t
*instance
;
3567 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3569 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3571 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
3577 return ME_ERRNO_SUCCESS
;
3580 static int me6000_ao_query_range_info(me_subdevice_t
*subdevice
,
3583 int *min
, int *max
, int *maxdata
)
3585 me6000_ao_subdevice_t
*instance
;
3587 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3589 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3592 *unit
= ME_UNIT_VOLT
;
3593 *min
= instance
->min
;
3594 *max
= instance
->max
;
3595 *maxdata
= ME6000_AO_MAX_DATA
;
3597 PERROR("Invalid range number specified.\n");
3598 return ME_ERRNO_INVALID_RANGE
;
3601 return ME_ERRNO_SUCCESS
;
3604 static int me6000_ao_query_timer(me_subdevice_t
*subdevice
,
3606 int *base_frequency
,
3607 long long *min_ticks
, long long *max_ticks
)
3609 me6000_ao_subdevice_t
*instance
;
3611 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3613 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3615 if (instance
->fifo
) { //Streaming device.
3616 *base_frequency
= ME6000_AO_BASE_FREQUENCY
;
3617 if (timer
== ME_TIMER_ACQ_START
) {
3618 *min_ticks
= ME6000_AO_MIN_ACQ_TICKS
;
3619 *max_ticks
= ME6000_AO_MAX_ACQ_TICKS
;
3620 } else if (timer
== ME_TIMER_CONV_START
) {
3621 *min_ticks
= ME6000_AO_MIN_CHAN_TICKS
;
3622 *max_ticks
= ME6000_AO_MAX_CHAN_TICKS
;
3624 } else { //Not streaming device!
3625 *base_frequency
= 0;
3630 return ME_ERRNO_SUCCESS
;
3633 static int me6000_ao_query_number_channels(me_subdevice_t
*subdevice
,
3636 me6000_ao_subdevice_t
*instance
;
3637 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3639 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3642 return ME_ERRNO_SUCCESS
;
3645 static int me6000_ao_query_subdevice_type(me_subdevice_t
*subdevice
,
3646 int *type
, int *subtype
)
3648 me6000_ao_subdevice_t
*instance
;
3650 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3652 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3657 fifo
& ME6000_AO_HAS_FIFO
) ? ME_SUBTYPE_STREAMING
:
3660 return ME_ERRNO_SUCCESS
;
3663 static int me6000_ao_query_subdevice_caps(me_subdevice_t
*subdevice
, int *caps
)
3665 me6000_ao_subdevice_t
*instance
;
3666 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3668 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3671 ME_CAPS_AO_TRIG_SYNCHRONOUS
| ((instance
->fifo
) ? ME_CAPS_AO_FIFO
:
3674 return ME_ERRNO_SUCCESS
;
3677 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice
*subdevice
,
3678 int cap
, int *args
, int count
)
3680 me6000_ao_subdevice_t
*instance
;
3681 int err
= ME_ERRNO_SUCCESS
;
3683 instance
= (me6000_ao_subdevice_t
*) subdevice
;
3685 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
3688 PERROR("Invalid capability argument count.\n");
3689 return ME_ERRNO_INVALID_CAP_ARG_COUNT
;
3693 case ME_CAP_AI_FIFO_SIZE
:
3694 args
[0] = (instance
->fifo
) ? ME6000_AO_FIFO_COUNT
: 0;
3697 case ME_CAP_AI_BUFFER_SIZE
:
3699 (instance
->circ_buf
.buf
) ? ME6000_AO_CIRC_BUF_COUNT
: 0;
3703 PERROR("Invalid capability.\n");
3704 err
= ME_ERRNO_INVALID_CAP
;