Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / meilhaus / me6000_ao.c
1 /**
2  * @file me6000_ao.c
3  *
4  * @brief ME-6000 analog output subdevice instance.
5  * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
13  * This file is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 /* Includes
33  */
34 #include <linux/version.h>
35 #include <linux/module.h>
36
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/io.h>
40 #include <linux/uaccess.h>
41 #include <linux/types.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44
45 #include <linux/workqueue.h>
46
47 #include "medefines.h"
48 #include "meinternal.h"
49 #include "meerror.h"
50
51 #include "medebug.h"
52 #include "meids.h"
53 #include "me6000_reg.h"
54 #include "me6000_ao_reg.h"
55 #include "me6000_ao.h"
56
57 /* Defines
58  */
59
60 static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
61                                             int unit,
62                                             int *min,
63                                             int *max, int *maxdata, int *range);
64
65 static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
66                                          int unit, int *count);
67
68 static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
69                                       int range,
70                                       int *unit,
71                                       int *min, int *max, int *maxdata);
72
73 static int me6000_ao_query_timer(me_subdevice_t *subdevice,
74                                  int timer,
75                                  int *base_frequency,
76                                  long long *min_ticks, long long *max_ticks);
77
78 static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
79                                            int *number);
80
81 static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
82                                           int *type, int *subtype);
83
84 static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice,
85                                           int *caps);
86
87 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
88                                                int cap, int *args, int count);
89
90 /** Remove subdevice. */
91 static void me6000_ao_destructor(struct me_subdevice *subdevice);
92
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);
96
97 /** Set output as single */
98 static int me6000_ao_io_single_config(me_subdevice_t *subdevice,
99                                       struct file *filep,
100                                       int channel,
101                                       int single_config,
102                                       int ref,
103                                       int trig_chan,
104                                       int trig_type, int trig_edge, int flags);
105
106 /** Pass to user actual value of output. */
107 static int me6000_ao_io_single_read(me_subdevice_t *subdevice,
108                                     struct file *filep,
109                                     int channel,
110                                     int *value, int time_out, int flags);
111
112 /** Write to output requed value. */
113 static int me6000_ao_io_single_write(me_subdevice_t *subdevice,
114                                      struct file *filep,
115                                      int channel,
116                                      int value, int time_out, int flags);
117
118 /** Set output as streamed device. */
119 static int me6000_ao_io_stream_config(me_subdevice_t *subdevice,
120                                       struct file *filep,
121                                       meIOStreamConfig_t *config_list,
122                                       int count,
123                                       meIOStreamTrigger_t *trigger,
124                                       int fifo_irq_threshold, int flags);
125
126 /** Wait for / Check empty space in buffer. */
127 static int me6000_ao_io_stream_new_values(me_subdevice_t *subdevice,
128                                           struct file *filep,
129                                           int time_out, int *count, int flags);
130
131 /** Start streaming. */
132 static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
133                                      struct file *filep,
134                                      int start_mode, int time_out, int flags);
135
136 /** Check actual state. / Wait for end. */
137 static int me6000_ao_io_stream_status(me_subdevice_t *subdevice,
138                                       struct file *filep,
139                                       int wait,
140                                       int *status, int *values, int flags);
141
142 /** Stop streaming. */
143 static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
144                                     struct file *filep,
145                                     int stop_mode, int flags);
146
147 /** Write datas to buffor. */
148 static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
149                                      struct file *filep,
150                                      int write_mode,
151                                      int *values, int *count, int flags);
152
153 /** Interrupt handler. Copy from buffer to FIFO. */
154 static irqreturn_t me6000_ao_isr(int irq, void *dev_id);
155
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,
158                                     int start_pos);
159
160 /** Copy data from circular buffer to fifo (fast).*/
161 inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
162                          int start_pos);
163
164 /** Copy data from circular buffer to fifo (slow).*/
165 inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
166                                  int start_pos);
167
168 /** Copy data from user space to circular buffer. */
169 inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
170                                  int *user_values);
171
172 /** Stop presentation. Preserve FIFOs. */
173 inline int ao_stop_immediately(me6000_ao_subdevice_t *instance);
174
175 /** Function for checking timeout in non-blocking mode. */
176 static void me6000_ao_work_control_task(struct work_struct *work);
177
178 /* Functions
179  */
180
181 static int me6000_ao_io_reset_subdevice(me_subdevice_t *subdevice,
182                                         struct file *filep, int flags)
183 {
184         me6000_ao_subdevice_t *instance;
185         int err = ME_ERRNO_SUCCESS;
186         uint32_t tmp;
187         uint32_t ctrl;
188
189         instance = (me6000_ao_subdevice_t *) subdevice;
190
191         PDEBUG("executed. idx=%d\n", instance->ao_idx);
192
193         if (flags) {
194                 PERROR("Invalid flag specified.\n");
195                 return ME_ERRNO_INVALID_FLAGS;
196         }
197
198         ME_SUBDEVICE_ENTER;
199
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;
205
206         //Stop state machine.
207         err = ao_stop_immediately(instance);
208
209         //Remove from synchronous start.
210         spin_lock(instance->preload_reg_lock);
211         tmp = inl(instance->preload_reg);
212         tmp &=
213             ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
214               ao_idx);
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->
220               ao_idx);
221
222         //Reset triggering flag
223         *instance->triggering_flags &= ~(0x1 << instance->ao_idx);
224         spin_unlock(instance->preload_reg_lock);
225
226         if (instance->fifo) {
227                 //Set single mode, dissable FIFO, dissable external trigger, block interrupt.
228                 ctrl = ME6000_AO_MODE_SINGLE;
229
230                 //Block ISM.
231                 ctrl |=
232                     (ME6000_AO_CTRL_BIT_STOP |
233                      ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
234
235                 outl(ctrl, instance->ctrl_reg);
236                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
237                            instance->reg_base,
238                            instance->ctrl_reg - instance->reg_base, ctrl);
239                 //Set speed
240                 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
241                 //Reset interrupt latch
242                 inl(instance->irq_reset_reg);
243         }
244
245         instance->hardware_stop_delay = HZ / 10;        //100ms
246
247         //Set output to 0V
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);
251
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;
258
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);
263
264         ME_SUBDEVICE_EXIT;
265
266         return err;
267 }
268
269 static int me6000_ao_io_single_config(me_subdevice_t *subdevice,
270                                       struct file *filep,
271                                       int channel,
272                                       int single_config,
273                                       int ref,
274                                       int trig_chan,
275                                       int trig_type, int trig_edge, int flags)
276 {
277         me6000_ao_subdevice_t *instance;
278         int err = ME_ERRNO_SUCCESS;
279         uint32_t ctrl;
280         uint32_t sync;
281         unsigned long cpu_flags;
282
283         instance = (me6000_ao_subdevice_t *) subdevice;
284
285         PDEBUG("executed. ID=%d\n", instance->ao_idx);
286
287         // Checking parameters
288         if (flags) {
289                 PERROR
290                     ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
291                 return ME_ERRNO_INVALID_FLAGS;
292         }
293
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)) {
297                         PERROR
298                             ("Invalid trigger edge. Software trigger has not edge.\n");
299                         return ME_ERRNO_INVALID_TRIG_EDGE;
300                 }
301
302                 if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
303                         switch (trig_edge) {
304                         case ME_TRIG_EDGE_ANY:
305                         case ME_TRIG_EDGE_RISING:
306                         case ME_TRIG_EDGE_FALLING:
307                                 break;
308
309                         default:
310                                 PERROR("Invalid trigger edge.\n");
311                                 return ME_ERRNO_INVALID_TRIG_EDGE;
312                         }
313                 }
314
315                 if ((trig_type != ME_TRIG_TYPE_SW)
316                     && (trig_type != ME_TRIG_TYPE_EXT_DIGITAL)) {
317                         PERROR
318                             ("Invalid trigger type. Trigger must be software or digital.\n");
319                         return ME_ERRNO_INVALID_TRIG_TYPE;
320                 }
321         } else {                //Single
322                 if (trig_edge != ME_TRIG_EDGE_NONE) {
323                         PERROR
324                             ("Invalid trigger edge. Single output trigger hasn't own edge.\n");
325                         return ME_ERRNO_INVALID_TRIG_EDGE;
326                 }
327
328                 if (trig_type != ME_TRIG_TYPE_SW) {
329                         PERROR
330                             ("Invalid trigger type. Trigger must be software.\n");
331                         return ME_ERRNO_INVALID_TRIG_TYPE;
332                 }
333
334         }
335
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;
340         }
341 /*
342         if ((trig_type == ME_TRIG_TYPE_EXT_DIGITAL) && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS))
343         {
344                 PERROR("Invalid trigger channel specified. Must be synchronous when digital is choose.\n");
345                 return ME_ERRNO_INVALID_TRIG_CHAN;
346         }
347 */
348         if (ref != ME_REF_AO_GROUND) {
349                 PERROR
350                     ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
351                 return ME_ERRNO_INVALID_REF;
352         }
353
354         if (single_config != 0) {
355                 PERROR
356                     ("Invalid single config specified. Only one range for anlog outputs is available.\n");
357                 return ME_ERRNO_INVALID_SINGLE_CONFIG;
358         }
359
360         if (channel != 0) {
361                 PERROR
362                     ("Invalid channel number specified. Analog output have only one channel.\n");
363                 return ME_ERRNO_INVALID_CHANNEL;
364         }
365
366         ME_SUBDEVICE_ENTER;
367
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");
372                 ME_SUBDEVICE_EXIT;
373
374                 return ME_ERRNO_SUBDEVICE_BUSY;
375         }
376 /// @note For single all calls (config and write) are erasing previous state!
377
378         instance->status = ao_status_none;
379
380         // Correct single mirrors
381         instance->single_value_in_fifo = instance->single_value;
382
383         //Stop device
384         err = ao_stop_immediately(instance);
385         if (err) {
386                 PERROR_CRITICAL("FSM IS BUSY!\n");
387                 ME_SUBDEVICE_EXIT;
388
389                 return ME_ERRNO_SUBDEVICE_BUSY;
390         }
391
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);
396                 //Reset all bits.
397                 ctrl =
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");
401
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;
404                                 instance->ctrl_trg =
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;
409                                 instance->ctrl_trg =
410                                     ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
411                         } else if (trig_edge == ME_TRIG_EDGE_RISING) {
412                                 instance->ctrl_trg = 0x0;
413                         }
414                 } else if (trig_type == ME_TRIG_TYPE_SW) {
415                         PDEBUG("SOFTWARE TRIGGER\n");
416                         instance->ctrl_trg = 0x0;
417                 }
418                 outl(ctrl, instance->ctrl_reg);
419                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
420                            instance->reg_base,
421                            instance->ctrl_reg - instance->reg_base, ctrl);
422                 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
423         } else {
424                 PDEBUG("SOFTWARE TRIGGER\n");
425         }
426
427         // Set preload/synchronization register.
428         spin_lock(instance->preload_reg_lock);
429
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)
434         {
435                 *instance->preload_flags |=
436                     ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
437         }
438
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)
443         {
444                 *instance->preload_flags |=
445                     ME6000_AO_SYNC_HOLD << instance->ao_idx;
446         }
447
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;
454
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);
460
461         instance->status = ao_status_single_configured;
462
463         ME_SUBDEVICE_EXIT;
464
465         return err;
466 }
467
468 static int me6000_ao_io_single_read(me_subdevice_t *subdevice,
469                                     struct file *filep,
470                                     int channel,
471                                     int *value, int time_out, int flags)
472 {
473         me6000_ao_subdevice_t *instance;
474         int err = ME_ERRNO_SUCCESS;
475
476         unsigned long j;
477         unsigned long delay = 0;
478
479         instance = (me6000_ao_subdevice_t *) subdevice;
480
481         PDEBUG("executed. idx=%d\n", instance->ao_idx);
482
483         if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
484                 PERROR("Invalid flag specified. %d\n", flags);
485                 return ME_ERRNO_INVALID_FLAGS;
486         }
487
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;
492         }
493
494         if (channel != 0) {
495                 PERROR("Invalid channel number specified.\n");
496                 return ME_ERRNO_INVALID_CHANNEL;
497         }
498
499         if (time_out < 0) {
500                 PERROR("Invalid timeout specified.\n");
501                 return ME_ERRNO_INVALID_TIMEOUT;
502         }
503
504         ME_SUBDEVICE_ENTER;
505         if ((!flags) && (instance->status == ao_status_single_run_wait)) {      //Blocking mode. Wait for trigger.
506                 if (time_out) {
507                         delay = (time_out * HZ) / 1000;
508                         if (delay == 0)
509                                 delay = 1;
510                 }
511
512                 j = jiffies;
513
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,
516                                                  (instance->status !=
517                                                   ao_status_single_run_wait),
518                                                  (delay) ? delay : LONG_MAX);
519
520                 if (instance->status == ao_status_none) {
521                         PDEBUG("Single canceled.\n");
522                         err = ME_ERRNO_CANCELLED;
523                 }
524
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;
530                 }
531
532                 if ((delay) && ((jiffies - j) >= delay)) {
533                         PDEBUG("Timeout reached.\n");
534                         err = ME_ERRNO_TIMEOUT;
535                 }
536
537                 *value =
538                     (!err) ? instance->single_value_in_fifo : instance->
539                     single_value;
540         } else {                //Non-blocking mode
541                 //Read value
542                 *value = instance->single_value;
543         }
544
545         ME_SUBDEVICE_EXIT;
546
547         return err;
548 }
549
550 static int me6000_ao_io_single_write(me_subdevice_t *subdevice,
551                                      struct file *filep,
552                                      int channel,
553                                      int value, int time_out, int flags)
554 {
555         me6000_ao_subdevice_t *instance;
556         int err = ME_ERRNO_SUCCESS;
557         unsigned long cpu_flags;
558         unsigned long j;
559         unsigned long delay = 0;
560
561         uint32_t sync_mask;
562         uint32_t mode;
563
564         uint32_t tmp;
565
566 /// Workaround for mix-mode - begin
567         uint32_t ctrl = 0x0;
568         uint32_t status;
569 /// Workaround for mix-mode - end
570
571         instance = (me6000_ao_subdevice_t *) subdevice;
572
573         PDEBUG("executed. idx=%d\n", instance->ao_idx);
574
575         if (flags &
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;
580         }
581
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;
586         }
587
588         if (channel != 0) {
589                 PERROR("Invalid channel number specified.\n");
590                 return ME_ERRNO_INVALID_CHANNEL;
591         }
592
593         if (value & ~ME6000_AO_MAX_DATA) {
594                 PERROR("Invalid value provided.\n");
595                 return ME_ERRNO_VALUE_OUT_OF_RANGE;
596         }
597
598         if (time_out < 0) {
599                 PERROR("Invalid timeout specified.\n");
600                 return ME_ERRNO_INVALID_TIMEOUT;
601         }
602
603         ME_SUBDEVICE_ENTER;
604
605 /// @note For single all calls (config and write) are erasing previous state!
606
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);
611
612         // Correct single mirrors
613         instance->single_value_in_fifo = instance->single_value;
614
615         //Stop device
616         err = ao_stop_immediately(instance);
617         if (err) {
618                 PERROR_CRITICAL("FSM IS BUSY!\n");
619                 ME_SUBDEVICE_EXIT;
620
621                 return ME_ERRNO_SUBDEVICE_BUSY;
622         }
623
624         if (time_out) {
625                 delay = (time_out * HZ) / 1000;
626
627                 if (delay == 0)
628                         delay = 1;
629         }
630
631         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
632
633         instance->single_value_in_fifo = value;
634
635         if (instance->fifo) {
636                 ctrl = inl(instance->ctrl_reg);
637         }
638
639         if (instance->fifo & ME6000_AO_HAS_FIFO) {      /// Workaround for mix-mode - begin
640                 //Set speed
641                 outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
642                 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
643                            instance->reg_base,
644                            instance->timer_reg - instance->reg_base,
645                            (int)ME6000_AO_MIN_CHAN_TICKS);
646                 instance->hardware_stop_delay = HZ / 10;        //100ms
647
648                 status = inl(instance->status_reg);
649
650                 //Set the continous mode.
651                 ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
652                 ctrl |= ME6000_AO_MODE_CONTINUOUS;
653
654                 //Prepare FIFO
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");
662                                 ctrl &=
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",
667                                            instance->reg_base,
668                                            instance->ctrl_reg -
669                                            instance->reg_base, ctrl);
670
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;
674                         }
675                 }
676
677                 outl(ctrl, instance->ctrl_reg);
678                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
679                            instance->reg_base,
680                            instance->ctrl_reg - instance->reg_base, ctrl);
681
682                 //Reset interrupt latch
683                 inl(instance->irq_reset_reg);
684
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",
689                                    instance->reg_base,
690                                    instance->fifo_reg - instance->reg_base,
691                                    value <<= 16);
692                 } else {
693                         outl(value, instance->fifo_reg);
694                         PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
695                                    instance->reg_base,
696                                    instance->fifo_reg - instance->reg_base,
697                                    value);
698                 }
699                 /// Workaround for mix-mode - end
700         } else {                //No FIFO - always in single mode
701                 //Write value
702                 PDEBUG("Write value\n");
703                 outl(value, instance->single_reg);
704                 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
705                            instance->reg_base,
706                            instance->single_reg - instance->reg_base, value);
707         }
708
709         mode = *instance->preload_flags >> instance->ao_idx;
710         mode &= (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG);
711
712         PINFO("Triggering mode: 0x%08x\n", mode);
713
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);
718         switch (mode) {
719         case 0:         //0x00000000: Individual software
720                 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
721
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.
725                                 sync_mask &=
726                                     ~((ME6000_AO_SYNC_EXT_TRIG |
727                                        ME6000_AO_SYNC_HOLD) << instance->
728                                       ao_idx);
729
730                                 outl(sync_mask, instance->preload_reg);
731                                 PDEBUG_REG
732                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
733                                      instance->reg_base,
734                                      instance->preload_reg - instance->reg_base,
735                                      sync_mask);
736                         }
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.
739                                 sync_mask &=
740                                     ~(ME6000_AO_SYNC_EXT_TRIG << instance->
741                                       ao_idx);
742                                 sync_mask |=
743                                     ME6000_AO_SYNC_HOLD << instance->ao_idx;
744
745                                 outl(sync_mask, instance->preload_reg);
746                                 PDEBUG_REG
747                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
748                                      instance->reg_base,
749                                      instance->preload_reg - instance->reg_base,
750                                      sync_mask);
751                         }
752                 }
753                 instance->single_value = value;
754                 break;
755
756         case ME6000_AO_SYNC_EXT_TRIG:   //0x00010000: Individual hardware
757                 PDEBUG("DIGITAL TRIGGER\n");
758                 ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
759
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.
762                                 sync_mask &=
763                                     ~((ME6000_AO_SYNC_EXT_TRIG |
764                                        ME6000_AO_SYNC_HOLD) << instance->
765                                       ao_idx);
766
767                                 outl(sync_mask, instance->preload_reg);
768                                 PDEBUG_REG
769                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
770                                      instance->reg_base,
771                                      instance->preload_reg - instance->reg_base,
772                                      sync_mask);
773                         }
774                 } else {        // No FIFO - Single mode
775                         if ((sync_mask &
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
779                                 sync_mask &=
780                                     ~(ME6000_AO_SYNC_EXT_TRIG << instance->
781                                       ao_idx);
782                                 sync_mask |=
783                                     ME6000_AO_SYNC_HOLD << instance->ao_idx;
784
785                                 outl(sync_mask, instance->preload_reg);
786                                 PDEBUG_REG
787                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
788                                      instance->reg_base,
789                                      instance->preload_reg - instance->reg_base,
790                                      sync_mask);
791                         }
792                 }
793                 break;
794
795         case ME6000_AO_SYNC_HOLD:       //0x00000001: Synchronous software
796                 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
797
798                 if ((sync_mask &
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
803                         sync_mask |=
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",
808                                    instance->reg_base,
809                                    instance->preload_reg - instance->reg_base,
810                                    sync_mask);
811                 }
812                 //Set triggering flag
813                 *instance->triggering_flags |= 0x1 << instance->ao_idx;
814                 break;
815
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;
819
820                 if ((sync_mask &
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
825                         sync_mask |=
826                             (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
827                             instance->ao_idx;
828                         outl(sync_mask, instance->preload_reg);
829                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
830                                    instance->reg_base,
831                                    instance->preload_reg - instance->reg_base,
832                                    sync_mask);
833                 }
834                 //Set triggering flag
835                 *instance->triggering_flags |= 0x1 << instance->ao_idx;
836                 break;
837         }
838 //              spin_unlock(instance->preload_reg_lock);        // Moved down.
839
840         if (instance->fifo) {   //Activate ISM (remove 'stop' bits)
841                 ctrl &=
842                     ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
843                       ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
844                 ctrl |= instance->ctrl_trg;
845                 ctrl &=
846                     ~(ME6000_AO_CTRL_BIT_STOP |
847                       ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
848
849                 outl(ctrl, instance->ctrl_reg);
850                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
851                            instance->reg_base,
852                            instance->ctrl_reg - instance->reg_base, ctrl);
853         }
854         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
855
856 /// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!
857
858         PINFO("<%s> start mode= 0x%08x %s\n", __func__, mode,
859               (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
860               "");
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
864                         outl(sync_mask |
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",
868                                    instance->reg_base,
869                                    instance->preload_reg - instance->reg_base,
870                                    sync_mask | (ME6000_AO_SYNC_HOLD <<
871                                                 instance->ao_idx));
872
873                         //Fire
874                         PINFO
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",
878                                    instance->reg_base,
879                                    instance->single_reg - instance->reg_base,
880                                    0x8000);
881
882                         //Restore save settings
883                         outl(sync_mask, instance->preload_reg);
884                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
885                                    instance->reg_base,
886                                    instance->preload_reg - instance->reg_base,
887                                    sync_mask);
888
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));
893 */
894                         //Fire
895                         PINFO("Software trigger.\n");
896                         outl(0x8000, instance->single_reg);
897                         PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
898                                    instance->reg_base,
899                                    instance->single_reg - instance->reg_base,
900                                    0x8000);
901
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);
905 */
906                 }
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);
913                         PINFO
914                             ("Fired all software synchronous outputs. mask:0x%08x\n",
915                              tmp);
916                         tmp |= sync_mask & 0xFFFF0000;
917                         // Add this channel to list
918                         tmp &= ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
919
920                         //Fire
921                         PINFO("Software trigger.\n");
922                         outl(tmp, instance->preload_reg);
923                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
924                                    instance->reg_base,
925                                    instance->preload_reg - instance->reg_base,
926                                    tmp);
927
928                         //Restore save settings
929                         outl(sync_mask, instance->preload_reg);
930                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
931                                    instance->reg_base,
932                                    instance->preload_reg - instance->reg_base,
933                                    sync_mask);
934
935                         //Set all as triggered.
936                         *instance->triggering_flags = 0x0;
937                 } else if (!mode) {     // Add this channel to list
938                         outl(sync_mask &
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",
942                                    instance->reg_base,
943                                    instance->preload_reg - instance->reg_base,
944                                    sync_mask & ~(ME6000_AO_SYNC_HOLD <<
945                                                  instance->ao_idx));
946
947                         //Fire
948                         PINFO("Software trigger.\n");
949
950                         //Restore save settings
951                         outl(sync_mask, instance->preload_reg);
952                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
953                                    instance->reg_base,
954                                    instance->preload_reg - instance->reg_base,
955                                    sync_mask);
956
957                         //Set all as triggered.
958                         *instance->triggering_flags = 0x0;
959                 }
960
961         }
962         spin_unlock(instance->preload_reg_lock);
963
964         instance->status = ao_status_single_run_wait;
965
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);
971
972         if (!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
973                 j = jiffies;
974
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,
977                                                  (instance->status !=
978                                                   ao_status_single_run_wait),
979                                                  (delay) ? delay +
980                                                  1 : LONG_MAX);
981
982                 if (instance->status != ao_status_single_end) {
983                         PDEBUG("Single canceled.\n");
984                         err = ME_ERRNO_CANCELLED;
985                 }
986
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;
994                 }
995
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) {
1000                                 PERROR
1001                                     ("Timeout reached. Not handled by control task!\n");
1002                                 ao_stop_immediately(instance);
1003                         } else {
1004                                 PERROR
1005                                     ("Timeout reached. Signal come but status is strange: %d\n",
1006                                      instance->status);
1007                                 ao_stop_immediately(instance);
1008                         }
1009
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;
1014                 }
1015         }
1016
1017         ME_SUBDEVICE_EXIT;
1018
1019         return err;
1020 }
1021
1022 static int me6000_ao_io_stream_config(me_subdevice_t *subdevice,
1023                                       struct file *filep,
1024                                       meIOStreamConfig_t *config_list,
1025                                       int count,
1026                                       meIOStreamTrigger_t *trigger,
1027                                       int fifo_irq_threshold, int flags)
1028 {
1029         me6000_ao_subdevice_t *instance;
1030         int err = ME_ERRNO_SUCCESS;
1031         uint32_t ctrl;
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;
1036
1037         instance = (me6000_ao_subdevice_t *) subdevice;
1038
1039         PDEBUG("executed. idx=%d\n", instance->ao_idx);
1040
1041         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1042                 PERROR("Not a streaming ao.\n");
1043                 return ME_ERRNO_NOT_SUPPORTED;
1044         }
1045
1046         conv_ticks =
1047             (uint64_t) conv_start_ticks_low +
1048             ((uint64_t) conv_start_ticks_high << 32);
1049
1050         if (flags &
1051             ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY |
1052               ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1053                 PERROR("Invalid flags.\n");
1054                 return ME_ERRNO_INVALID_FLAGS;
1055         }
1056
1057         if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {
1058                 if (!(flags & ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1059                         PERROR
1060                             ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
1061                         return ME_ERRNO_INVALID_FLAGS;
1062                 }
1063
1064                 if ((trigger->iAcqStopTrigType != ME_TRIG_TYPE_NONE)
1065                     || (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE)) {
1066                         PERROR
1067                             ("Hardware wraparound mode must be in infinite mode.\n");
1068                         return ME_ERRNO_INVALID_FLAGS;
1069                 }
1070         }
1071
1072         if (count != 1) {
1073                 PERROR("Only 1 entry in config list acceptable.\n");
1074                 return ME_ERRNO_INVALID_CONFIG_LIST_COUNT;
1075         }
1076
1077         if (config_list[0].iChannel != 0) {
1078                 PERROR("Invalid channel number specified.\n");
1079                 return ME_ERRNO_INVALID_CHANNEL;
1080         }
1081
1082         if (config_list[0].iStreamConfig != 0) {
1083                 PERROR("Only one range available.\n");
1084                 return ME_ERRNO_INVALID_STREAM_CONFIG;
1085         }
1086
1087         if (config_list[0].iRef != ME_REF_AO_GROUND) {
1088                 PERROR("Output is referenced to ground.\n");
1089                 return ME_ERRNO_INVALID_REF;
1090         }
1091
1092         if ((trigger->iAcqStartTicksLow != 0)
1093             || (trigger->iAcqStartTicksHigh != 0)) {
1094                 PERROR
1095                     ("Invalid acquisition start trigger argument specified.\n");
1096                 return ME_ERRNO_INVALID_ACQ_START_ARG;
1097         }
1098
1099         if (config_list[0].iFlags) {
1100                 PERROR("Invalid config list flag.\n");
1101                 return ME_ERRNO_INVALID_FLAGS;
1102         }
1103
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;
1108         }
1109
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:
1115                         break;
1116
1117                 default:
1118                         PERROR
1119                             ("Invalid acquisition start trigger edge specified.\n");
1120                         return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1121                 }
1122         }
1123
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;
1128         }
1129
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;
1133         }
1134
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;
1138         }
1139
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;
1144         }
1145
1146         if (trigger->iAcqStartTicksLow || trigger->iAcqStartTicksHigh) {
1147                 PERROR("Invalid acq start trigger argument specified.\n");
1148                 return ME_ERRNO_INVALID_ACQ_START_ARG;
1149         }
1150
1151         if (trigger->iScanStartTicksLow || trigger->iScanStartTicksHigh) {
1152                 PERROR("Invalid scan start trigger argument specified.\n");
1153                 return ME_ERRNO_INVALID_SCAN_START_ARG;
1154         }
1155
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;
1161                 }
1162                 break;
1163
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;
1169                         }
1170                 } else {
1171                         PERROR("The continous mode has not 'scan' contects.\n");
1172                         return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1173                 }
1174                 break;
1175
1176         default:
1177                 PERROR("Invalid scan stop trigger type specified.\n");
1178                 return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE;
1179         }
1180
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;
1186                 }
1187                 break;
1188
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;
1193                 }
1194
1195                 if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1196                         if (trigger->iAcqStopCount <= 0) {
1197                                 PERROR
1198                                     ("The continous mode has not 'scan' contects.\n");
1199                                 return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1200                         }
1201                 }
1202 //                      else
1203 //                      {
1204 //                              PERROR("Invalid acq stop trigger type specified.\n");
1205 //                              return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1206 //                      }
1207
1208                 break;
1209
1210         default:
1211                 PERROR("Invalid acq stop trigger type specified.\n");
1212                 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1213         }
1214
1215         switch (trigger->iAcqStartTrigChan) {
1216         case ME_TRIG_CHAN_DEFAULT:
1217         case ME_TRIG_CHAN_SYNCHRONOUS:
1218                 break;
1219
1220         default:
1221                 PERROR("Invalid acq start trigger channel specified.\n");
1222                 return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN;
1223         }
1224
1225         ME_SUBDEVICE_ENTER;
1226
1227         //Stop device
1228
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);
1233
1234         //Check if state machine is stopped.
1235         err = ao_stop_immediately(instance);
1236         if (err) {
1237                 PERROR_CRITICAL("FSM IS BUSY!\n");
1238                 ME_SUBDEVICE_EXIT;
1239
1240                 return ME_ERRNO_SUBDEVICE_BUSY;
1241         }
1242
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);
1249
1250         //Reset interrupt latch
1251         inl(instance->irq_reset_reg);
1252
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;
1258
1259         /* Set mode. */
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;
1269                 }
1270         } else {                //Continous
1271                 PINFO("Continous.\n");
1272                 ctrl |= ME6000_AO_MODE_CONTINUOUS;
1273                 instance->mode = ME6000_AO_CONTINOUS;
1274         }
1275
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;
1280
1281                 switch (trigger->iAcqStartTrigEdge) {
1282                 case ME_TRIG_EDGE_RISING:
1283                         PINFO("Set the trigger edge: rising.\n");
1284                         instance->ctrl_trg = 0x0;
1285                         break;
1286
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;
1291                         break;
1292
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;
1299                         break;
1300                 }
1301         } else {
1302                 PINFO("Internal software trigger.\n");
1303                 instance->start_mode = 0;
1304         }
1305
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;
1313         } else {                //Infinite
1314                 instance->stop_mode = ME6000_AO_INF_STOP_MODE;
1315                 instance->stop_count = 0;
1316         }
1317
1318         PINFO("Stop count: %d.\n", instance->stop_count);
1319
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;
1325                 }
1326 #ifdef MEDEBUG_INFO
1327                 else {
1328                         PINFO
1329                             ("Synchronous start. Externaly trigger dissabled.\n");
1330                 }
1331 #endif
1332
1333         }
1334         //Set speed
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!
1339
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);
1344
1345         //Set status.
1346         instance->status = ao_status_stream_configured;
1347         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1348
1349         ME_SUBDEVICE_EXIT;
1350
1351         return err;
1352 }
1353
1354 static int me6000_ao_io_stream_new_values(me_subdevice_t *subdevice,
1355                                           struct file *filep,
1356                                           int time_out, int *count, int flags)
1357 {
1358         me6000_ao_subdevice_t *instance;
1359         int err = ME_ERRNO_SUCCESS;
1360         long t = 0;
1361         long j;
1362
1363         instance = (me6000_ao_subdevice_t *) subdevice;
1364
1365         PDEBUG("executed. idx=%d\n", instance->ao_idx);
1366
1367         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1368                 PERROR("Not a streaming ao.\n");
1369                 return ME_ERRNO_NOT_SUPPORTED;
1370         }
1371
1372         if (flags) {
1373                 PERROR("Invalid flag specified.\n");
1374                 return ME_ERRNO_INVALID_FLAGS;
1375         }
1376
1377         if (!instance->circ_buf.buf) {
1378                 PERROR("Circular buffer not exists.\n");
1379                 return ME_ERRNO_INTERNAL;
1380         }
1381
1382         if (time_out < 0) {
1383                 PERROR("Invalid time_out specified.\n");
1384                 return ME_ERRNO_INVALID_TIMEOUT;
1385         }
1386
1387         ME_SUBDEVICE_ENTER;
1388
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.
1392                 if (time_out) {
1393                         t = (time_out * HZ) / 1000;
1394
1395                         if (t == 0)
1396                                 t = 1;
1397                 } else {        //Max time.
1398                         t = LONG_MAX;
1399                 }
1400
1401                 *count = 0;
1402
1403                 j = jiffies;
1404
1405                 //Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
1406                 wait_event_interruptible_timeout(instance->wait_queue,
1407                                                  ((me_circ_buf_space
1408                                                    (&instance->circ_buf))
1409                                                   || !(inl(instance->status_reg)
1410                                                        &
1411                                                        ME6000_AO_STATUS_BIT_FSM)),
1412                                                  t);
1413
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);
1427                 }
1428         }
1429
1430         ME_SUBDEVICE_EXIT;
1431
1432         return err;
1433 }
1434
1435 static int me6000_ao_io_stream_start(me_subdevice_t *subdevice,
1436                                      struct file *filep,
1437                                      int start_mode, int time_out, int flags)
1438 {
1439         me6000_ao_subdevice_t *instance;
1440         int err = ME_ERRNO_SUCCESS;
1441         unsigned long cpu_flags = 0;
1442         uint32_t status;
1443         uint32_t ctrl;
1444         uint32_t synch;
1445         int count = 0;
1446         int circ_buffer_count;
1447
1448         unsigned long ref;
1449         unsigned long delay = 0;
1450
1451         instance = (me6000_ao_subdevice_t *) subdevice;
1452
1453         PDEBUG("executed. idx=%d\n", instance->ao_idx);
1454
1455         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1456                 PERROR("Not a streaming ao.\n");
1457                 return ME_ERRNO_NOT_SUPPORTED;
1458         }
1459
1460         if (flags & ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS) {
1461                 PERROR("Invalid flags.\n");
1462                 return ME_ERRNO_INVALID_FLAGS;
1463         }
1464
1465         if (time_out < 0) {
1466                 PERROR("Invalid timeout specified.\n");
1467                 return ME_ERRNO_INVALID_TIMEOUT;
1468         }
1469
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;
1474         }
1475
1476         if (time_out) {
1477                 delay = (time_out * HZ) / 1000;
1478                 if (delay == 0)
1479                         delay = 1;
1480         }
1481
1482         switch (instance->status) {     //Checking actual mode.
1483         case ao_status_stream_configured:
1484         case ao_status_stream_end:
1485                 //Correct modes!
1486                 break;
1487
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:
1494                 PERROR
1495                     ("Subdevice must be preinitialize correctly for streaming.\n");
1496                 return ME_ERRNO_PREVIOUS_CONFIG;
1497
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;
1503
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;
1509
1510         default:
1511                 instance->status = ao_status_stream_error;
1512                 PERROR_CRITICAL("Status is in wrong state!\n");
1513                 return ME_ERRNO_INTERNAL;
1514
1515         }
1516
1517         ME_SUBDEVICE_ENTER;
1518
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;
1522         }
1523         circ_buffer_count = me_circ_buf_values(&instance->circ_buf);
1524
1525         if (!circ_buffer_count && !instance->preloaded_count) { //No values in buffer
1526                 ME_SUBDEVICE_EXIT;
1527                 PERROR("No values in buffer!\n");
1528                 return ME_ERRNO_LACK_OF_RESOURCES;
1529         }
1530
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);
1535
1536         //Stop device
1537         err = ao_stop_immediately(instance);
1538         if (err) {
1539                 PERROR_CRITICAL("FSM IS BUSY!\n");
1540                 ME_SUBDEVICE_EXIT;
1541
1542                 return ME_ERRNO_SUBDEVICE_BUSY;
1543         }
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;
1547
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;
1552         } else {
1553                 instance->stop_data_count = instance->stop_count;
1554         }
1555
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");
1559         }
1560
1561         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1562         ctrl = inl(instance->ctrl_reg);
1563         //Check FIFO
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;
1568
1569                 instance->preloaded_count = 0;
1570                 instance->data_count = 0;
1571         } else {                //Block IRQ
1572                 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1573         }
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);
1577
1578         //Reset interrupt latch
1579         inl(instance->irq_reset_reg);
1580
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;
1586                 } else {
1587                         count =
1588                             (ME6000_AO_FIFO_COUNT <
1589                              instance->
1590                              stop_data_count) ? ME6000_AO_FIFO_COUNT :
1591                             instance->stop_data_count;
1592                 }
1593
1594                 //Copy data
1595                 count =
1596                     ao_write_data(instance, count, instance->preloaded_count);
1597
1598                 if (count < 0) {        //This should never happend!
1599                         PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1600                         spin_unlock_irqrestore(&instance->subdevice_lock,
1601                                                cpu_flags);
1602                         ME_SUBDEVICE_EXIT;
1603                         return ME_ERRNO_INTERNAL;
1604                 }
1605         }
1606         //Set pre-load features.
1607         spin_lock(instance->preload_reg_lock);
1608         synch = inl(instance->preload_reg);
1609         synch &=
1610             ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
1611               ao_idx);
1612         synch |=
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);
1618
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;
1627
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
1631                         PDEBUG
1632                             ("Changeing mode from software wraparound to hardware wraparound.\n");
1633                         //Copy all data
1634                         count =
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;
1639                 }
1640
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!
1644                         PERROR_CRITICAL
1645                             ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1646                         spin_unlock_irqrestore(&instance->subdevice_lock,
1647                                                cpu_flags);
1648                         ME_SUBDEVICE_EXIT;
1649                         return ME_ERRNO_INTERNAL;
1650                 }
1651         }
1652
1653         //Set status to 'wait for start'
1654         instance->status = ao_status_stream_run_wait;
1655
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;
1663         }
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__,
1667                               __LINE__);
1668                         ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1669                 }
1670         }
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);
1675
1676         //Trigger output
1677         PINFO("<%s> start mode= 0x%x %s\n", __func__, instance->start_mode,
1678               (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
1679               "");
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",
1687                            instance->reg_base,
1688                            instance->preload_reg - instance->reg_base,
1689                            synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx));
1690
1691                 //Fire
1692                 PINFO
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",
1696                            instance->reg_base,
1697                            instance->single_reg - instance->reg_base, 0x8000);
1698
1699                 //Restore save settings
1700                 outl(synch, instance->preload_reg);
1701                 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1702                            instance->reg_base,
1703                            instance->preload_reg - instance->reg_base, synch);
1704                 spin_unlock(instance->preload_reg_lock);
1705         } else if (!instance->start_mode) {     //Trigger outputs
1706 /*
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));
1712 */
1713                 //Fire
1714                 PINFO("Software trigger.\n");
1715                 outl(0x8000, instance->single_reg);
1716                 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1717                            instance->reg_base,
1718                            instance->single_reg - instance->reg_base, 0x8000);
1719
1720 /*
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);
1725 */
1726         }
1727         // Set control task's timeout
1728         instance->timeout.delay = delay;
1729         instance->timeout.start_time = jiffies;
1730
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;
1735                 } else {
1736                         count =
1737                             ((ME6000_AO_FIFO_COUNT / 2) <
1738                              instance->stop_data_count) ? ME6000_AO_FIFO_COUNT /
1739                             2 : instance->stop_data_count;
1740                 }
1741
1742                 //Copy data
1743                 count =
1744                     ao_write_data(instance, count, instance->preloaded_count);
1745
1746                 if (count < 0) {        //This should never happend!
1747                         PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1748                         ME_SUBDEVICE_EXIT;
1749                         return ME_ERRNO_INTERNAL;
1750                 }
1751
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;
1758
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!
1762                                 PERROR_CRITICAL
1763                                     ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1764                                 ME_SUBDEVICE_EXIT;
1765                                 return ME_ERRNO_INTERNAL;
1766                         }
1767                 }
1768
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__,
1773                               __LINE__);
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",
1778                                    instance->reg_base,
1779                                    instance->ctrl_reg - instance->reg_base,
1780                                    ctrl);
1781                         spin_unlock_irqrestore(&instance->subdevice_lock,
1782                                                cpu_flags);
1783                 }
1784         }
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!
1791                         PERROR_CRITICAL
1792                             ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1793                         ME_SUBDEVICE_EXIT;
1794                         return ME_ERRNO_INTERNAL;
1795                 }
1796
1797                 while (instance->stop_data_count > instance->data_count) {      //Maximum data not set jet.
1798                         //Copy to buffer
1799                         if (circ_buffer_count != ao_write_data(instance, circ_buffer_count, 0)) {       //This should never happend!
1800                                 PERROR_CRITICAL
1801                                     ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1802                                 ME_SUBDEVICE_EXIT;
1803                                 return ME_ERRNO_INTERNAL;
1804                         }
1805                         instance->data_count += circ_buffer_count;
1806
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);
1810
1811                                 spin_lock_irqsave(&instance->subdevice_lock,
1812                                                   cpu_flags);
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",
1819                                            instance->reg_base,
1820                                            instance->ctrl_reg -
1821                                            instance->reg_base, ctrl);
1822                                 spin_unlock_irqrestore(&instance->
1823                                                        subdevice_lock,
1824                                                        cpu_flags);
1825                                 break;
1826                         }
1827                 }
1828         }
1829         // Schedule control task
1830         instance->ao_control_task_flag = 1;
1831         queue_delayed_work(instance->me6000_workqueue,
1832                            &instance->ao_control_task, 1);
1833
1834         if (start_mode == ME_START_MODE_BLOCKING) {     //Wait for start.
1835                 ref = jiffies;
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),
1840                                                  (delay) ? delay +
1841                                                  1 : LONG_MAX);
1842
1843                 if ((instance->status != ao_status_stream_run)
1844                     && (instance->status != ao_status_stream_end)) {
1845                         PDEBUG("Starting stream canceled. %d\n",
1846                                instance->status);
1847                         err = ME_ERRNO_CANCELLED;
1848                 }
1849
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;
1855                 }
1856
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) {
1862                                         PERROR
1863                                             ("Timeout reached. Not handled by control task!\n");
1864                                         ao_stop_immediately(instance);
1865                                 } else {
1866                                         PERROR
1867                                             ("Timeout reached. Signal come but status is strange: %d\n",
1868                                              instance->status);
1869                                         ao_stop_immediately(instance);
1870                                 }
1871
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;
1876                         }
1877                 }
1878         }
1879
1880         ME_SUBDEVICE_EXIT;
1881         return err;
1882 }
1883
1884 static int me6000_ao_io_stream_status(me_subdevice_t *subdevice,
1885                                       struct file *filep,
1886                                       int wait,
1887                                       int *status, int *values, int flags)
1888 {
1889         me6000_ao_subdevice_t *instance;
1890         int err = ME_ERRNO_SUCCESS;
1891
1892         instance = (me6000_ao_subdevice_t *) subdevice;
1893
1894         PDEBUG("executed. idx=%d\n", instance->ao_idx);
1895
1896         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1897                 PERROR("Not a streaming ao.\n");
1898                 return ME_ERRNO_NOT_SUPPORTED;
1899         }
1900
1901         if (flags) {
1902                 PERROR("Invalid flag specified.\n");
1903                 return ME_ERRNO_INVALID_FLAGS;
1904         }
1905
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;
1910         }
1911
1912         ME_SUBDEVICE_ENTER;
1913
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;
1923                 break;
1924
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;
1932                 break;
1933
1934         case ao_status_none:
1935         default:
1936                 *status =
1937                     (inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM) ?
1938                     ME_STATUS_BUSY : ME_STATUS_IDLE;
1939                 break;
1940         }
1941
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)),
1957                                                  LONG_MAX);
1958
1959                 if (instance->status != ao_status_stream_end) {
1960                         PDEBUG("Wait for IDLE canceled. %d\n",
1961                                instance->status);
1962                         err = ME_ERRNO_CANCELLED;
1963                 }
1964
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;
1970                 }
1971
1972                 *status = ME_STATUS_IDLE;
1973         }
1974
1975         *values = me_circ_buf_space(&instance->circ_buf);
1976
1977         ME_SUBDEVICE_EXIT;
1978
1979         return err;
1980 }
1981
1982 static int me6000_ao_io_stream_stop(me_subdevice_t *subdevice,
1983                                     struct file *filep,
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;
1990
1991         instance = (me6000_ao_subdevice_t *) subdevice;
1992
1993         PDEBUG("executed. idx=%d\n", instance->ao_idx);
1994
1995         if (flags & ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS) {
1996                 PERROR("Invalid flag specified.\n");
1997                 return ME_ERRNO_INVALID_FLAGS;
1998         }
1999
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;
2004         }
2005
2006         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2007                 PERROR("Not a streaming ao.\n");
2008                 return ME_ERRNO_NOT_SUPPORTED;
2009         }
2010
2011         if (instance->status < ao_status_stream_configured) {
2012                 //There is nothing to stop!
2013                 PERROR("Subdevice not in streaming mode. %d\n",
2014                        instance->status);
2015                 return ME_ERRNO_PREVIOUS_CONFIG;
2016         }
2017
2018         ME_SUBDEVICE_ENTER;
2019
2020         //Mark as stopping. => Software stop.
2021         instance->status = ao_status_stream_end_wait;
2022
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",
2034                                    instance->reg_base,
2035                                    instance->ctrl_reg - instance->reg_base,
2036                                    ctrl);
2037                         spin_unlock_irqrestore(&instance->subdevice_lock,
2038                                                cpu_flags);
2039
2040                         //Reset interrupt latch
2041                         inl(instance->irq_reset_reg);
2042                 }
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),
2047                                                  LONG_MAX);
2048
2049                 if (instance->status != ao_status_stream_end) {
2050                         PDEBUG("Stopping stream canceled.\n");
2051                         err = ME_ERRNO_CANCELLED;
2052                 }
2053
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;
2059                 }
2060         }
2061
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;
2068         }
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);
2073
2074         //Reset interrupt latch
2075         inl(instance->irq_reset_reg);
2076
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;
2082         }
2083
2084         ME_SUBDEVICE_EXIT;
2085
2086         return err;
2087 }
2088
2089 static int me6000_ao_io_stream_write(me_subdevice_t *subdevice,
2090                                      struct file *filep,
2091                                      int write_mode,
2092                                      int *values, int *count, int flags)
2093 {
2094         int err = ME_ERRNO_SUCCESS;
2095         me6000_ao_subdevice_t *instance;
2096         unsigned long cpu_flags = 0;
2097         uint32_t reg_copy;
2098
2099         int copied_from_user = 0;
2100         int left_to_copy_from_user = *count;
2101
2102         int copied_values;
2103
2104         instance = (me6000_ao_subdevice_t *) subdevice;
2105
2106         PDEBUG("executed. idx=%d\n", instance->ao_idx);
2107
2108         //Checking arguments
2109         if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2110                 PERROR("Not a streaming ao.\n");
2111                 return ME_ERRNO_NOT_SUPPORTED;
2112         }
2113
2114         if (flags) {
2115                 PERROR("Invalid flag specified.\n");
2116                 return ME_ERRNO_INVALID_FLAGS;
2117         }
2118
2119         if (*count <= 0) {
2120                 PERROR("Invalid count of values specified.\n");
2121                 return ME_ERRNO_INVALID_VALUE_COUNT;
2122         }
2123
2124         if (values == NULL) {
2125                 PERROR("Invalid address of values specified.\n");
2126                 return ME_ERRNO_INVALID_POINTER;
2127         }
2128
2129         if ((instance->status == ao_status_none) || (instance->status == ao_status_single_configured)) {        //The device is in single mode.
2130                 PERROR
2131                     ("Subdevice must be preinitialize correctly for streaming.\n");
2132                 return ME_ERRNO_PREVIOUS_CONFIG;
2133         }
2134
2135         switch (write_mode) {
2136         case ME_WRITE_MODE_PRELOAD:
2137
2138                 //Device must be stopped.
2139                 if ((instance->status != ao_status_stream_configured)
2140                     && (instance->status != ao_status_stream_end)) {
2141                         PERROR
2142                             ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2143                         return ME_ERRNO_PREVIOUS_CONFIG;
2144                 }
2145                 break;
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.
2150                 break;
2151
2152         default:
2153                 PERROR("Invalid write mode specified.\n");
2154                 return ME_ERRNO_INVALID_WRITE_MODE;
2155         }
2156
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)) {
2160                         PERROR
2161                             ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2162                         return ME_ERRNO_INVALID_WRITE_MODE;
2163                 }
2164         }
2165
2166         if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2167             && (write_mode != ME_WRITE_MODE_PRELOAD)) {
2168 /*
2169                 PERROR("Only 'pre-load' write is acceptable in hardware wraparound mode.\n");
2170                 return ME_ERRNO_PREVIOUS_CONFIG;
2171 */
2172                 //This is transparent for user.
2173                 PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
2174                 write_mode = ME_WRITE_MODE_PRELOAD;
2175         }
2176
2177         ME_SUBDEVICE_ENTER;
2178
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);
2182                 //Check FIFO
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",
2187                                    instance->reg_base,
2188                                    instance->ctrl_reg - instance->reg_base,
2189                                    reg_copy);
2190                         instance->preloaded_count = 0;
2191                 }
2192                 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2193         }
2194
2195         while (1) {
2196                 //Copy to buffer. This step is common for all modes.
2197                 copied_from_user =
2198                     ao_get_data_from_user(instance, left_to_copy_from_user,
2199                                           values + (*count -
2200                                                     left_to_copy_from_user));
2201                 left_to_copy_from_user -= copied_from_user;
2202
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;
2207                         break;
2208                 }
2209
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!
2211
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",
2218                                    instance->reg_base,
2219                                    instance->ctrl_reg - instance->reg_base,
2220                                    reg_copy);
2221                         spin_unlock_irqrestore(&instance->subdevice_lock,
2222                                                cpu_flags);
2223
2224                         //Fast copy
2225                         copied_values =
2226                             ao_write_data(instance, ME6000_AO_FIFO_COUNT / 2,
2227                                           0);
2228                         if (copied_values > 0) {
2229                                 instance->circ_buf.tail += copied_values;
2230                                 instance->circ_buf.tail &=
2231                                     instance->circ_buf.mask;
2232                                 continue;
2233                         }
2234                         //Reset interrupt latch
2235                         inl(instance->irq_reset_reg);
2236
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",
2243                                    instance->reg_base,
2244                                    instance->ctrl_reg - instance->reg_base,
2245                                    reg_copy);
2246                         spin_unlock_irqrestore(&instance->subdevice_lock,
2247                                                cpu_flags);
2248
2249                         if (copied_values == 0) {       //This was checked and never should happend!
2250                                 PERROR_CRITICAL("COPY FINISH WITH 0!\n");
2251                         }
2252
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;
2257                                 break;
2258                         }
2259                 }
2260
2261                 if (!left_to_copy_from_user) {  //All datas were copied.
2262                         break;
2263                 } else {        //Not all datas were copied.
2264                         if (instance->mode & ME6000_AO_WRAP_MODE) {     //Error too much datas! Wraparound is limited in size!
2265                                 PERROR
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;
2269                                 break;
2270                         }
2271
2272                         if (write_mode != ME_WRITE_MODE_BLOCKING) {     //Non blocking calls
2273                                 break;
2274                         }
2275
2276                         wait_event_interruptible(instance->wait_queue,
2277                                                  me_circ_buf_space(&instance->
2278                                                                    circ_buf));
2279
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;
2285                                 break;
2286                         }
2287
2288                         if (instance->status == ao_status_none) {       //Reset
2289                                 PERROR("Writing interrupted by reset.\n");
2290                                 err = ME_ERRNO_CANCELLED;
2291                                 break;
2292                         }
2293                 }
2294         }
2295
2296         if (write_mode == ME_WRITE_MODE_PRELOAD) {      //Copy data to FIFO - preload
2297                 copied_values =
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;
2302
2303                 if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2304                     && (me_circ_buf_values(&instance->circ_buf) >
2305                         ME6000_AO_FIFO_COUNT)) {
2306                         PERROR
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;
2310                 }
2311         }
2312
2313         *count = *count - left_to_copy_from_user;
2314         ME_SUBDEVICE_EXIT;
2315
2316         return err;
2317 }
2318
2319 static irqreturn_t me6000_ao_isr(int irq, void *dev_id)
2320 {
2321         me6000_ao_subdevice_t *instance = dev_id;
2322         uint32_t irq_status;
2323         uint32_t ctrl;
2324         uint32_t status;
2325         int count = 0;
2326
2327         PDEBUG("executed. idx=%d\n", instance->ao_idx);
2328
2329         if (irq != instance->irq) {
2330                 PERROR("Incorrect interrupt num: %d.\n", irq);
2331                 return IRQ_NONE;
2332         }
2333
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);
2338                 return IRQ_NONE;
2339         }
2340
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;
2347                 ctrl |=
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",
2351                            instance->reg_base,
2352                            instance->ctrl_reg - instance->reg_base, ctrl);
2353
2354                 //Inform user
2355                 wake_up_interruptible_all(&instance->wait_queue);
2356                 return IRQ_HANDLED;
2357         }
2358
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;
2366                 ctrl |=
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",
2370                            instance->reg_base,
2371                            instance->ctrl_reg - instance->reg_base, ctrl);
2372
2373                 //Reset interrupt latch
2374                 inl(instance->irq_reset_reg);
2375
2376                 /// @note User notification was also moved to separate task.
2377                 return IRQ_HANDLED;
2378         }
2379         //General procedure. Process more datas.
2380
2381 #ifdef MEDEBUG_DEBUG
2382         if (!me_circ_buf_values(&instance->circ_buf)) { //Buffer is empty!
2383                 PDEBUG("Circular buffer empty!\n");
2384         }
2385 #endif
2386
2387         //Check FIFO
2388         if (status & ME6000_AO_STATUS_BIT_HF) { //OK less than half
2389
2390                 //Block interrupts
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",
2395                            instance->reg_base,
2396                            instance->ctrl_reg - instance->reg_base, ctrl);
2397
2398                 do {
2399                         //Calculate how many should be copied.
2400                         count =
2401                             (instance->stop_data_count) ? instance->
2402                             stop_data_count -
2403                             instance->data_count : ME6000_AO_FIFO_COUNT / 2;
2404                         if (ME6000_AO_FIFO_COUNT / 2 < count) {
2405                                 count = ME6000_AO_FIFO_COUNT / 2;
2406                         }
2407                         //Copy data
2408                         if (instance->mode == ME6000_AO_CONTINOUS) {    //Continous
2409                                 count = ao_write_data(instance, count, 0);
2410                                 if (count > 0) {
2411                                         instance->circ_buf.tail += count;
2412                                         instance->circ_buf.tail &=
2413                                             instance->circ_buf.mask;
2414                                         instance->data_count += count;
2415
2416                                         if ((instance->status == ao_status_stream_end_wait) && !me_circ_buf_values(&instance->circ_buf)) {      //Stoping. Whole buffer was copied.
2417                                                 break;
2418                                         }
2419                                 }
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.
2422                                         count =
2423                                             ao_write_data(instance, count, 0);
2424                                 } else {        //Copy in wraparound mode.
2425                                         count =
2426                                             ao_write_data_wraparound(instance,
2427                                                                      count,
2428                                                                      instance->
2429                                                                      preloaded_count);
2430                                 }
2431
2432                                 if (count > 0) {
2433                                         instance->data_count += count;
2434                                         instance->preloaded_count += count;
2435                                         instance->preloaded_count %=
2436                                             me_circ_buf_values(&instance->
2437                                                                circ_buf);
2438
2439                                         if ((instance->status == ao_status_stream_end_wait) && !instance->preloaded_count) {    //Stoping. Whole buffer was copied.
2440                                                 break;
2441                                         }
2442                                 }
2443                         }
2444
2445                         if ((count <= 0) || (instance->stop_data_count && (instance->stop_data_count <= instance->data_count))) {       //End of work.
2446                                 break;
2447                         }
2448                 }               //Repeat if still is under half fifo
2449                 while ((status =
2450                         inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF);
2451
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.
2462                                 PDEBUG
2463                                     ("No data in software buffer. Interrupt blocked.\n");
2464                         }
2465                 } else {        //Error during copy.
2466                         instance->status = ao_status_stream_fifo_error;
2467                 }
2468
2469                 outl(ctrl, instance->ctrl_reg);
2470                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2471                            instance->reg_base,
2472                            instance->ctrl_reg - instance->reg_base, ctrl);
2473         } else {                //?? more than half
2474                 PDEBUG
2475                     ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
2476         }
2477
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);
2484
2485         //Reset interrupt latch
2486         inl(instance->irq_reset_reg);
2487
2488         //Inform user
2489         wake_up_interruptible_all(&instance->wait_queue);
2490
2491         return IRQ_HANDLED;
2492 }
2493
2494 static void me6000_ao_destructor(struct me_subdevice *subdevice)
2495 {
2496         me6000_ao_subdevice_t *instance;
2497
2498         instance = (me6000_ao_subdevice_t *) subdevice;
2499
2500         PDEBUG("executed. idx=%d\n", instance->ao_idx);
2501
2502         instance->ao_control_task_flag = 0;
2503
2504         // Reset subdevice to asure clean exit.
2505         me6000_ao_io_reset_subdevice(subdevice, NULL,
2506                                      ME_IO_RESET_SUBDEVICE_NO_FLAGS);
2507
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);
2512         }
2513
2514         if (instance->fifo & ME6000_AO_HAS_FIFO) {
2515                 if (instance->irq) {
2516                         free_irq(instance->irq, instance);
2517                         instance->irq = 0;
2518                 }
2519
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);
2526                 }
2527                 instance->circ_buf.buf = NULL;
2528         }
2529
2530         me_subdevice_deinit(&instance->base);
2531         kfree(instance);
2532 }
2533
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,
2538                                              int ao_idx,
2539                                              int fifo,
2540                                              int irq,
2541                                              int high_range,
2542                                              struct workqueue_struct *me6000_wq)
2543 {
2544         me6000_ao_subdevice_t *subdevice;
2545         int err;
2546
2547         PDEBUG("executed ID=%d.\n", ao_idx);
2548
2549         /* Allocate memory for subdevice instance */
2550         subdevice = kmalloc(sizeof(me6000_ao_subdevice_t), GFP_KERNEL);
2551
2552         if (!subdevice) {
2553                 PERROR("Cannot get memory for subdevice instance.\n");
2554                 return NULL;
2555         }
2556
2557         memset(subdevice, 0, sizeof(me6000_ao_subdevice_t));
2558
2559         /* Initialize subdevice base class */
2560         err = me_subdevice_init(&subdevice->base);
2561
2562         if (err) {
2563                 PERROR("Cannot initialize subdevice base class instance.\n");
2564                 kfree(subdevice);
2565                 return NULL;
2566         }
2567         // Initialize spin locks.
2568         spin_lock_init(&subdevice->subdevice_lock);
2569
2570         subdevice->preload_reg_lock = preload_reg_lock;
2571         subdevice->preload_flags = preload_flags;
2572         subdevice->triggering_flags = triggering_flags;
2573
2574         /* Store analog output index */
2575         subdevice->ao_idx = ao_idx;
2576
2577         /* Store if analog output has fifo */
2578         subdevice->fifo = fifo;
2579
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);
2588
2589                 if (!subdevice->circ_buf.buf) {
2590                         PERROR
2591                             ("Cannot initialize subdevice base class instance.\n");
2592                         kfree(subdevice);
2593                         return NULL;
2594                 }
2595
2596                 memset(subdevice->circ_buf.buf, 0, ME6000_AO_CIRC_BUF_SIZE);
2597         } else {
2598                 subdevice->circ_buf.mask = 0;
2599                 subdevice->circ_buf.buf = NULL;
2600         }
2601         subdevice->circ_buf.head = 0;
2602         subdevice->circ_buf.tail = 0;
2603
2604         subdevice->status = ao_status_none;
2605         subdevice->ao_control_task_flag = 0;
2606         subdevice->timeout.delay = 0;
2607         subdevice->timeout.start_time = jiffies;
2608
2609         /* Initialize wait queue */
2610         init_waitqueue_head(&subdevice->wait_queue);
2611
2612         /* Initialize single value to 0V */
2613         subdevice->single_value = 0x8000;
2614         subdevice->single_value_in_fifo = 0x8000;
2615
2616         /* Initialize range boarders */
2617         if (high_range) {
2618                 subdevice->min = ME6000_AO_MIN_RANGE_HIGH;
2619                 subdevice->max = ME6000_AO_MAX_RANGE_HIGH;
2620         } else {
2621                 subdevice->min = ME6000_AO_MIN_RANGE;
2622                 subdevice->max = ME6000_AO_MAX_RANGE;
2623         }
2624
2625         /* Register interrupt service routine */
2626
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;
2639                         kfree(subdevice);
2640                         return NULL;
2641                 }
2642                 PINFO("Registered irq=%d.\n", subdevice->irq);
2643         } else {
2644                 subdevice->irq = 0;
2645         }
2646
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;
2651
2652         if (ao_idx == 0) {
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;
2684         } else {
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;
2690
2691                 subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
2692                 if (ao_idx == 4) {
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;
2728                 } else {
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);
2734                         }
2735                         subdevice->circ_buf.buf = NULL;
2736                         kfree(subdevice);
2737                         return NULL;
2738                 }
2739         }
2740 #ifdef MEDEBUG_DEBUG_REG
2741         subdevice->reg_base = reg_base;
2742 #endif
2743
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;
2779
2780         //prepare work queue and work function
2781         subdevice->me6000_workqueue = me6000_wq;
2782
2783 /* workqueue API changed in kernel 2.6.20 */
2784         INIT_DELAYED_WORK(&subdevice->ao_control_task,
2785                           me6000_ao_work_control_task);
2786
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
2790         }
2791
2792         return subdevice;
2793 }
2794
2795 /** @brief Stop presentation. Preserve FIFOs.
2796 *
2797 * @param instance The subdevice instance (pointer).
2798 */
2799 inline int ao_stop_immediately(me6000_ao_subdevice_t *instance)
2800 {
2801         unsigned long cpu_flags;
2802         uint32_t ctrl;
2803         int timeout;
2804         int i;
2805         uint32_t single_mask;
2806
2807         if (instance->ao_idx < ME6000_AO_SINGLE_STATUS_OFFSET)
2808                 single_mask = 0x0000;
2809         else
2810                 single_mask = 0x0001 << (instance->ao_idx -
2811                                 ME6000_AO_SINGLE_STATUS_OFFSET);
2812
2813         timeout =
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);
2821                         ctrl |=
2822                             ME6000_AO_CTRL_BIT_STOP |
2823                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2824                         ctrl &=
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",
2829                                    instance->reg_base,
2830                                    instance->ctrl_reg - instance->reg_base,
2831                                    ctrl);
2832                         spin_unlock_irqrestore(&instance->subdevice_lock,
2833                                                cpu_flags);
2834
2835                         if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {  // Exit.
2836                                 break;
2837                         }
2838                 } else {
2839                         if (!(inl(instance->status_reg) & single_mask)) {       // Exit.
2840                                 break;
2841                         }
2842                 }
2843
2844                 PINFO("<%s> Wait for stop: %d\n", __func__, i);
2845
2846                 //Still working!
2847                 set_current_state(TASK_INTERRUPTIBLE);
2848                 schedule_timeout(1);
2849         }
2850
2851         if (i > timeout) {
2852                 PERROR_CRITICAL("FSM IS BUSY!\n");
2853                 return ME_ERRNO_INTERNAL;
2854         }
2855         return ME_ERRNO_SUCCESS;
2856 }
2857
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.
2861 *
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.
2865 *
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.
2869 */
2870 inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
2871                                     int start_pos)
2872 {                               /// @note This is time critical function!
2873         uint32_t status;
2874         uint32_t value;
2875         int pos =
2876             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2877         int local_count = count;
2878         int i = 1;
2879
2880         if (count <= 0) {       //Wrong count!
2881                 return 0;
2882         }
2883
2884         while (i < local_count) {
2885                 //Get value from buffer
2886                 value = *(instance->circ_buf.buf + pos);
2887                 //Prepare it
2888                 if (instance->ao_idx & 0x1) {
2889                         value <<= 16;
2890                 }
2891                 //Put value to FIFO
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);
2894
2895                 pos++;
2896                 pos &= instance->circ_buf.mask;
2897                 if (pos == instance->circ_buf.head) {
2898                         pos = instance->circ_buf.tail;
2899                 }
2900                 i++;
2901         }
2902
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",
2906                        instance->ao_idx);
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) {
2911                         value <<= 16;
2912                 }
2913                 //Put value to FIFO
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);
2916         }
2917
2918         PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
2919               local_count);
2920         return local_count;
2921 }
2922
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.
2926 *
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.
2930 *
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.
2934 */
2935 inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
2936                          int start_pos)
2937 {                               /// @note This is time critical function!
2938         uint32_t status;
2939         uint32_t value;
2940         int pos =
2941             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2942         int local_count = count;
2943         int max_count;
2944         int i = 1;
2945
2946         if (count <= 0) {       //Wrong count!
2947                 return 0;
2948         }
2949
2950         max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
2951         if (max_count <= 0) {   //No data to copy!
2952                 return 0;
2953         }
2954
2955         if (max_count < count) {
2956                 local_count = max_count;
2957         }
2958
2959         while (i < local_count) {
2960                 //Get value from buffer
2961                 value = *(instance->circ_buf.buf + pos);
2962                 //Prepare it
2963                 if (instance->ao_idx & 0x1) {
2964                         value <<= 16;
2965                 }
2966                 //Put value to FIFO
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);
2969
2970                 pos++;
2971                 pos &= instance->circ_buf.mask;
2972                 i++;
2973         }
2974
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",
2978                        instance->ao_idx);
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) {
2983                         value <<= 16;
2984                 }
2985                 //Put value to FIFO
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);
2988         }
2989
2990         PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
2991         return local_count;
2992 }
2993
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.
2996 *
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.
3000 *
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.
3004 */
3005 inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
3006                                  int start_pos)
3007 {                               /// @note This is slow function!
3008         uint32_t status;
3009         uint32_t value;
3010         int pos =
3011             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
3012         int local_count = count;
3013         int i;
3014         int max_count;
3015
3016         if (count <= 0) {       //Wrong count!
3017                 PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
3018                 return 0;
3019         }
3020
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",
3024                        instance->ao_idx);
3025                 return 0;
3026         }
3027
3028         if (max_count < count) {
3029                 local_count = max_count;
3030         }
3031
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!
3035                         return i;
3036                 }
3037                 //Get value from buffer
3038                 value = *(instance->circ_buf.buf + pos);
3039                 //Prepare it
3040                 if (instance->ao_idx & 0x1) {
3041                         value <<= 16;
3042                 }
3043                 //Put value to FIFO
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);
3046
3047                 pos++;
3048                 pos &= instance->circ_buf.mask;
3049         }
3050
3051         PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
3052         return local_count;
3053 }
3054
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.
3059 *
3060 * @return On success: Number of copied values.
3061 * @return On error: -ME_ERRNO_INTERNAL.
3062 */
3063 inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
3064                                  int *user_values)
3065 {
3066         int i, err;
3067         int empty_space;
3068         int copied;
3069         int value;
3070
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)))) {
3076                         PERROR
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;
3080                 }
3081                 /// @note The analog output in me6000 series has size of 16 bits.
3082                 *(instance->circ_buf.buf + instance->circ_buf.head) =
3083                     (uint16_t) value;
3084                 instance->circ_buf.head++;
3085                 instance->circ_buf.head &= instance->circ_buf.mask;
3086         }
3087
3088         PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
3089         return copied;
3090 }
3091
3092 static void me6000_ao_work_control_task(struct work_struct *work)
3093 {
3094         me6000_ao_subdevice_t *instance;
3095         unsigned long cpu_flags = 0;
3096         uint32_t status;
3097         uint32_t ctrl;
3098         uint32_t synch;
3099         int reschedule = 0;
3100         int signaling = 0;
3101         uint32_t single_mask;
3102
3103         instance =
3104             container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
3105         PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
3106               instance->ao_idx);
3107
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);
3111
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);
3115
3116         switch (instance->status) {     // Checking actual mode.
3117
3118                 // Not configured for work.
3119         case ao_status_none:
3120                 break;
3121
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");
3129                 break;
3130
3131                 // Single modes
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.
3140                                         PDEBUG
3141                                             ("Single call has been complited.\n");
3142
3143                                         // Set correct value for single_read();
3144                                         instance->single_value =
3145                                             instance->single_value_in_fifo;
3146
3147                                         // Set status as 'ao_status_single_end'
3148                                         instance->status = ao_status_single_end;
3149
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 =
3153                                                     0x00000000;
3154                                         } else {
3155                                                 //Set this channel as triggered (none active).
3156                                                 *instance->triggering_flags &=
3157                                                     ~(0x1 << instance->ao_idx);
3158                                         }
3159                                         spin_unlock(instance->preload_reg_lock);
3160
3161                                         // Signal the end.
3162                                         signaling = 1;
3163                                         // Wait for stop ISM.
3164                                         reschedule = 1;
3165
3166                                         break;
3167                                 }
3168                         }
3169                         // Check timeout.
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,
3174                                                   cpu_flags);
3175                                 ctrl = inl(instance->ctrl_reg);
3176                                 ctrl |=
3177                                     ME6000_AO_CTRL_BIT_STOP |
3178                                     ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3179                                 ctrl &=
3180                                     ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3181                                       ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3182                                 ctrl &=
3183                                     ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
3184                                       ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
3185                                 //Disabling FIFO
3186                                 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
3187
3188                                 outl(ctrl, instance->ctrl_reg);
3189                                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3190                                            instance->reg_base,
3191                                            instance->ctrl_reg -
3192                                            instance->reg_base, ctrl);
3193                                 spin_unlock_irqrestore(&instance->
3194                                                        subdevice_lock,
3195                                                        cpu_flags);
3196
3197                                 //Reset interrupt latch
3198                                 inl(instance->irq_reset_reg);
3199
3200                                 spin_lock(instance->preload_reg_lock);
3201                                 //Remove from synchronous start. Block triggering from this output.
3202                                 synch = inl(instance->preload_reg);
3203                                 synch &=
3204                                     ~((ME6000_AO_SYNC_HOLD |
3205                                        ME6000_AO_SYNC_EXT_TRIG) << instance->
3206                                       ao_idx);
3207                                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO - set to single safe mode
3208                                         synch |=
3209                                             ME6000_AO_SYNC_HOLD << instance->
3210                                             ao_idx;
3211                                 }
3212                                 outl(synch, instance->preload_reg);
3213                                 PDEBUG_REG
3214                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3215                                      instance->reg_base,
3216                                      instance->preload_reg - instance->reg_base,
3217                                      synch);
3218                                 //Set this channel as triggered (none active).
3219                                 *instance->triggering_flags &=
3220                                     ~(0x1 << instance->ao_idx);
3221                                 spin_unlock(instance->preload_reg_lock);
3222
3223                                 // Set correct value for single_read();
3224                                 instance->single_value_in_fifo =
3225                                     instance->single_value;
3226
3227                                 instance->status = ao_status_single_end;
3228
3229                                 // Signal the end.
3230                                 signaling = 1;
3231                         }
3232                 } else {        // No extra registers.
3233 /*
3234                                 if (!(status & single_mask))
3235                                 {// State machine is not working.
3236                                         PDEBUG("Single call has been complited.\n");
3237
3238                                         // Set correct value for single_read();
3239                                         instance->single_value = instance->single_value_in_fifo;
3240
3241                                         // Set status as 'ao_status_single_end'
3242                                         instance->status = ao_status_single_end;
3243
3244                                         // Signal the end.
3245                                         signaling = 1;
3246                                         // Wait for stop ISM.
3247                                         reschedule = 1;
3248
3249                                         break;
3250                                 }
3251 */
3252                         if (!single_mask) {     // Was triggered.
3253                                 PDEBUG("Single call has been complited.\n");
3254
3255                                 // Set correct value for single_read();
3256                                 instance->single_value =
3257                                     instance->single_value_in_fifo;
3258
3259                                 // Set status as 'ao_status_single_end'
3260                                 instance->status = ao_status_single_end;
3261
3262                                 // Signal the end.
3263                                 signaling = 1;
3264
3265                                 break;
3266                         }
3267                         // Check timeout.
3268                         if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3269                                 PDEBUG("Timeout reached.\n");
3270
3271                                 spin_lock(instance->preload_reg_lock);
3272                                 //Remove from synchronous start. Block triggering from this output.
3273                                 synch = inl(instance->preload_reg);
3274                                 synch &=
3275                                     ~(ME6000_AO_SYNC_EXT_TRIG << instance->
3276                                       ao_idx);
3277                                 synch |=
3278                                     ME6000_AO_SYNC_HOLD << instance->ao_idx;
3279
3280                                 outl(synch, instance->preload_reg);
3281                                 PDEBUG_REG
3282                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3283                                      instance->reg_base,
3284                                      instance->preload_reg - instance->reg_base,
3285                                      synch);
3286                                 //Set this channel as triggered (none active).
3287                                 *instance->triggering_flags &=
3288                                     ~(0x1 << instance->ao_idx);
3289                                 spin_unlock(instance->preload_reg_lock);
3290
3291                                 // Restore old settings.
3292                                 PDEBUG("Write old value back to register.\n");
3293                                 outl(instance->single_value,
3294                                      instance->single_reg);
3295                                 PDEBUG_REG
3296                                     ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3297                                      instance->reg_base,
3298                                      instance->single_reg - instance->reg_base,
3299                                      instance->single_value);
3300
3301                                 // Set correct value for single_read();
3302                                 instance->single_value_in_fifo =
3303                                     instance->single_value;
3304
3305                                 instance->status = ao_status_single_end;
3306
3307                                 // Signal the end.
3308                                 signaling = 1;
3309                         }
3310                 }
3311
3312                 // Wait for stop.
3313                 reschedule = 1;
3314                 break;
3315
3316         case ao_status_stream_end:
3317                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3318                         PERROR_CRITICAL
3319                             ("Streaming on single device! This feature is not implemented in this version!\n");
3320                         instance->status = ao_status_stream_error;
3321                         // Signal the end.
3322                         signaling = 1;
3323                         break;
3324                 }
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.
3328
3329                                 // Wait for stop.
3330                                 reschedule = 1;
3331                         }
3332
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);
3336                         ctrl |=
3337                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
3338                             ME6000_AO_CTRL_BIT_STOP;
3339                         ctrl &=
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",
3344                                    instance->reg_base,
3345                                    instance->ctrl_reg - instance->reg_base,
3346                                    ctrl);
3347                         spin_unlock_irqrestore(&instance->subdevice_lock,
3348                                                cpu_flags);
3349
3350                         //Reset interrupt latch
3351                         inl(instance->irq_reset_reg);
3352                 } else {        // No extra registers.
3353 /*
3354                                 if (status & single_mask)
3355                                 {// State machine is working but the status is set to end. Force stop.
3356
3357                                         // Wait for stop.
3358                                         reschedule = 1;
3359                                 }
3360 */
3361                 }
3362                 break;
3363
3364                 // Stream modes
3365         case ao_status_stream_run_wait:
3366                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3367                         PERROR_CRITICAL
3368                             ("Streaming on single device! This feature is not implemented in this version!\n");
3369                         instance->status = ao_status_stream_error;
3370                         // Signal the end.
3371                         signaling = 1;
3372                         break;
3373                 }
3374
3375                 if (status & ME6000_AO_STATUS_BIT_FSM) {        // State machine is working. Waiting for start finish.
3376                         instance->status = ao_status_stream_run;
3377
3378                         // Signal end of this step
3379                         signaling = 1;
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;
3383
3384                                 // Signal the end.
3385                                 signaling = 1;
3386                                 // Wait for stop.
3387                                 reschedule = 1;
3388                                 break;
3389                         }
3390                 }
3391
3392                 // Check timeout.
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);
3398                         ctrl |=
3399                             ME6000_AO_CTRL_BIT_STOP |
3400                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3401                         ctrl &=
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",
3406                                    instance->reg_base,
3407                                    instance->ctrl_reg - instance->reg_base,
3408                                    ctrl);
3409                         spin_unlock_irqrestore(&instance->subdevice_lock,
3410                                                cpu_flags);
3411
3412                         //Reset interrupt latch
3413                         inl(instance->irq_reset_reg);
3414
3415                         spin_lock(instance->preload_reg_lock);
3416                         //Remove from synchronous start. Block triggering from this output.
3417                         synch = inl(instance->preload_reg);
3418                         synch &=
3419                             ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
3420                               instance->ao_idx);
3421                         outl(synch, instance->preload_reg);
3422                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3423                                    instance->reg_base,
3424                                    instance->preload_reg - instance->reg_base,
3425                                    synch);
3426                         spin_unlock(instance->preload_reg_lock);
3427
3428                         instance->status = ao_status_stream_end;
3429
3430                         // Signal the end.
3431                         signaling = 1;
3432                 }
3433                 // Wait for stop.
3434                 reschedule = 1;
3435                 break;
3436
3437         case ao_status_stream_run:
3438                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3439                         PERROR_CRITICAL
3440                             ("Streaming on single device! This feature is not implemented in this version!\n");
3441                         instance->status = ao_status_stream_error;
3442                         // Signal the end.
3443                         signaling = 1;
3444                         break;
3445                 }
3446
3447                 if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. This is an error.
3448                         // BROKEN PIPE!
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.
3452                                                 PDEBUG
3453                                                     ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3454                                                 instance->status =
3455                                                     ao_status_stream_end;
3456                                         } else {
3457                                                 PERROR
3458                                                     ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3459                                                 instance->status =
3460                                                     ao_status_stream_buffer_error;
3461                                         }
3462                                 } else {        // Software buffer is empty.
3463                                         PDEBUG
3464                                             ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3465                                         instance->status = ao_status_stream_end;
3466                                 }
3467                         } else {        // There are still datas in FIFO.
3468                                 if (me_circ_buf_values(&instance->circ_buf)) {  // Software buffer is not empty.
3469                                         PERROR
3470                                             ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3471                                 } else {        // Software buffer is empty.
3472                                         PERROR
3473                                             ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3474                                 }
3475                                 instance->status = ao_status_stream_fifo_error;
3476
3477                         }
3478
3479                         // Signal the failure.
3480                         signaling = 1;
3481                         break;
3482                 }
3483                 // Wait for stop.
3484                 reschedule = 1;
3485                 break;
3486
3487         case ao_status_stream_end_wait:
3488                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3489                         PERROR_CRITICAL
3490                             ("Streaming on single device! This feature is not implemented in this version!\n");
3491                         instance->status = ao_status_stream_error;
3492                         // Signal the end.
3493                         signaling = 1;
3494                         break;
3495                 }
3496
3497                 if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. Waiting for stop finish.
3498                         instance->status = ao_status_stream_end;
3499                         signaling = 1;
3500                 }
3501                 // State machine is working.
3502                 reschedule = 1;
3503                 break;
3504
3505         default:
3506                 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3507                                 instance->status);
3508                 instance->status = ao_status_stream_error;
3509                 // Signal the end.
3510                 signaling = 1;
3511                 break;
3512
3513         }
3514
3515         if (signaling) {        //Signal it.
3516                 wake_up_interruptible_all(&instance->wait_queue);
3517         }
3518
3519         if (instance->ao_control_task_flag && reschedule) {     // Reschedule task
3520                 queue_delayed_work(instance->me6000_workqueue,
3521                                    &instance->ao_control_task, 1);
3522         } else {
3523                 PINFO("<%s> Ending control task.\n", __func__);
3524         }
3525
3526 }
3527
3528 static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
3529                                             int unit,
3530                                             int *min,
3531                                             int *max, int *maxdata, int *range)
3532 {
3533         me6000_ao_subdevice_t *instance;
3534
3535         instance = (me6000_ao_subdevice_t *) subdevice;
3536
3537         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3538
3539         if ((*max - *min) < 0) {
3540                 PERROR("Invalid minimum and maximum values specified.\n");
3541                 return ME_ERRNO_INVALID_MIN_MAX;
3542         }
3543
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;
3549                         *range = 0;
3550                 } else {
3551                         PERROR("No matching range available.\n");
3552                         return ME_ERRNO_NO_RANGE;
3553                 }
3554         } else {
3555                 PERROR("Invalid physical unit specified.\n");
3556                 return ME_ERRNO_INVALID_UNIT;
3557         }
3558
3559         return ME_ERRNO_SUCCESS;
3560 }
3561
3562 static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
3563                                          int unit, int *count)
3564 {
3565         me6000_ao_subdevice_t *instance;
3566
3567         instance = (me6000_ao_subdevice_t *) subdevice;
3568
3569         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3570
3571         if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3572                 *count = 1;
3573         } else {
3574                 *count = 0;
3575         }
3576
3577         return ME_ERRNO_SUCCESS;
3578 }
3579
3580 static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
3581                                       int range,
3582                                       int *unit,
3583                                       int *min, int *max, int *maxdata)
3584 {
3585         me6000_ao_subdevice_t *instance;
3586
3587         instance = (me6000_ao_subdevice_t *) subdevice;
3588
3589         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3590
3591         if (range == 0) {
3592                 *unit = ME_UNIT_VOLT;
3593                 *min = instance->min;
3594                 *max = instance->max;
3595                 *maxdata = ME6000_AO_MAX_DATA;
3596         } else {
3597                 PERROR("Invalid range number specified.\n");
3598                 return ME_ERRNO_INVALID_RANGE;
3599         }
3600
3601         return ME_ERRNO_SUCCESS;
3602 }
3603
3604 static int me6000_ao_query_timer(me_subdevice_t *subdevice,
3605                                  int timer,
3606                                  int *base_frequency,
3607                                  long long *min_ticks, long long *max_ticks)
3608 {
3609         me6000_ao_subdevice_t *instance;
3610
3611         instance = (me6000_ao_subdevice_t *) subdevice;
3612
3613         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3614
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;
3623                 }
3624         } else {                //Not streaming device!
3625                 *base_frequency = 0;
3626                 *min_ticks = 0;
3627                 *max_ticks = 0;
3628         }
3629
3630         return ME_ERRNO_SUCCESS;
3631 }
3632
3633 static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
3634                                            int *number)
3635 {
3636         me6000_ao_subdevice_t *instance;
3637         instance = (me6000_ao_subdevice_t *) subdevice;
3638
3639         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3640
3641         *number = 1;
3642         return ME_ERRNO_SUCCESS;
3643 }
3644
3645 static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
3646                                           int *type, int *subtype)
3647 {
3648         me6000_ao_subdevice_t *instance;
3649
3650         instance = (me6000_ao_subdevice_t *) subdevice;
3651
3652         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3653
3654         *type = ME_TYPE_AO;
3655         *subtype =
3656             (instance->
3657              fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
3658             ME_SUBTYPE_SINGLE;
3659
3660         return ME_ERRNO_SUCCESS;
3661 }
3662
3663 static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
3664 {
3665         me6000_ao_subdevice_t *instance;
3666         instance = (me6000_ao_subdevice_t *) subdevice;
3667
3668         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3669
3670         *caps =
3671             ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
3672                                            ME_CAPS_NONE);
3673
3674         return ME_ERRNO_SUCCESS;
3675 }
3676
3677 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
3678                                                int cap, int *args, int count)
3679 {
3680         me6000_ao_subdevice_t *instance;
3681         int err = ME_ERRNO_SUCCESS;
3682
3683         instance = (me6000_ao_subdevice_t *) subdevice;
3684
3685         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3686
3687         if (count != 1) {
3688                 PERROR("Invalid capability argument count.\n");
3689                 return ME_ERRNO_INVALID_CAP_ARG_COUNT;
3690         }
3691
3692         switch (cap) {
3693         case ME_CAP_AI_FIFO_SIZE:
3694                 args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
3695                 break;
3696
3697         case ME_CAP_AI_BUFFER_SIZE:
3698                 args[0] =
3699                     (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
3700                 break;
3701
3702         default:
3703                 PERROR("Invalid capability.\n");
3704                 err = ME_ERRNO_INVALID_CAP;
3705                 args[0] = 0;
3706         }
3707
3708         return err;
3709 }