Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[sfrench/cifs-2.6.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 #ifdef IRQF_DISABLED
2631                                 IRQF_DISABLED | IRQF_SHARED,
2632 #else
2633                                 SA_INTERRUPT | SA_SHIRQ,
2634 #endif
2635                                 ME6000_NAME, subdevice)) {
2636                         PERROR("Cannot get interrupt line.\n");
2637                         PDEBUG("free circ_buf = %p size=%d",
2638                                subdevice->circ_buf.buf,
2639                                PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2640                         free_pages((unsigned long)subdevice->circ_buf.buf,
2641                                    ME6000_AO_CIRC_BUF_SIZE_ORDER);
2642                         subdevice->circ_buf.buf = NULL;
2643                         kfree(subdevice);
2644                         return NULL;
2645                 }
2646                 PINFO("Registered irq=%d.\n", subdevice->irq);
2647         } else {
2648                 subdevice->irq = 0;
2649         }
2650
2651         /* Initialize registers */
2652         // Only streamed subdevices support interrupts. For the rest this register has no meaning.
2653         subdevice->irq_status_reg = reg_base + ME6000_AO_IRQ_STATUS_REG;
2654         subdevice->preload_reg = reg_base + ME6000_AO_PRELOAD_REG;
2655
2656         if (ao_idx == 0) {
2657                 subdevice->ctrl_reg = reg_base + ME6000_AO_00_CTRL_REG;
2658                 subdevice->status_reg = reg_base + ME6000_AO_00_STATUS_REG;
2659                 subdevice->fifo_reg = reg_base + ME6000_AO_00_FIFO_REG;
2660                 subdevice->timer_reg = reg_base + ME6000_AO_00_TIMER_REG;
2661                 subdevice->irq_reset_reg =
2662                     reg_base + ME6000_AO_00_IRQ_RESET_REG;
2663                 subdevice->single_reg = reg_base + ME6000_AO_00_SINGLE_REG;
2664         } else if (ao_idx == 1) {
2665                 subdevice->ctrl_reg = reg_base + ME6000_AO_01_CTRL_REG;
2666                 subdevice->status_reg = reg_base + ME6000_AO_01_STATUS_REG;
2667                 subdevice->fifo_reg = reg_base + ME6000_AO_01_FIFO_REG;
2668                 subdevice->timer_reg = reg_base + ME6000_AO_01_TIMER_REG;
2669                 subdevice->irq_reset_reg =
2670                     reg_base + ME6000_AO_01_IRQ_RESET_REG;
2671                 subdevice->single_reg = reg_base + ME6000_AO_01_SINGLE_REG;
2672         } else if (ao_idx == 2) {
2673                 subdevice->ctrl_reg = reg_base + ME6000_AO_02_CTRL_REG;
2674                 subdevice->status_reg = reg_base + ME6000_AO_02_STATUS_REG;
2675                 subdevice->fifo_reg = reg_base + ME6000_AO_02_FIFO_REG;
2676                 subdevice->timer_reg = reg_base + ME6000_AO_02_TIMER_REG;
2677                 subdevice->irq_reset_reg =
2678                     reg_base + ME6000_AO_02_IRQ_RESET_REG;
2679                 subdevice->single_reg = reg_base + ME6000_AO_02_SINGLE_REG;
2680         } else if (ao_idx == 3) {
2681                 subdevice->ctrl_reg = reg_base + ME6000_AO_03_CTRL_REG;
2682                 subdevice->status_reg = reg_base + ME6000_AO_03_STATUS_REG;
2683                 subdevice->fifo_reg = reg_base + ME6000_AO_03_FIFO_REG;
2684                 subdevice->timer_reg = reg_base + ME6000_AO_03_TIMER_REG;
2685                 subdevice->irq_reset_reg =
2686                     reg_base + ME6000_AO_03_IRQ_RESET_REG;
2687                 subdevice->single_reg = reg_base + ME6000_AO_03_SINGLE_REG;
2688         } else {
2689                 subdevice->ctrl_reg = reg_base + ME6000_AO_DUMY;
2690                 subdevice->fifo_reg = reg_base + ME6000_AO_DUMY;
2691                 subdevice->timer_reg = reg_base + ME6000_AO_DUMY;
2692                 subdevice->irq_reset_reg = reg_base + ME6000_AO_DUMY;
2693                 subdevice->single_reg = reg_base + ME6000_AO_DUMY;
2694
2695                 subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
2696                 if (ao_idx == 4) {
2697                         subdevice->single_reg =
2698                             reg_base + ME6000_AO_04_SINGLE_REG;
2699                 } else if (ao_idx == 5) {
2700                         subdevice->single_reg =
2701                             reg_base + ME6000_AO_05_SINGLE_REG;
2702                 } else if (ao_idx == 6) {
2703                         subdevice->single_reg =
2704                             reg_base + ME6000_AO_06_SINGLE_REG;
2705                 } else if (ao_idx == 7) {
2706                         subdevice->single_reg =
2707                             reg_base + ME6000_AO_07_SINGLE_REG;
2708                 } else if (ao_idx == 8) {
2709                         subdevice->single_reg =
2710                             reg_base + ME6000_AO_08_SINGLE_REG;
2711                 } else if (ao_idx == 9) {
2712                         subdevice->single_reg =
2713                             reg_base + ME6000_AO_09_SINGLE_REG;
2714                 } else if (ao_idx == 10) {
2715                         subdevice->single_reg =
2716                             reg_base + ME6000_AO_10_SINGLE_REG;
2717                 } else if (ao_idx == 11) {
2718                         subdevice->single_reg =
2719                             reg_base + ME6000_AO_11_SINGLE_REG;
2720                 } else if (ao_idx == 12) {
2721                         subdevice->single_reg =
2722                             reg_base + ME6000_AO_12_SINGLE_REG;
2723                 } else if (ao_idx == 13) {
2724                         subdevice->single_reg =
2725                             reg_base + ME6000_AO_13_SINGLE_REG;
2726                 } else if (ao_idx == 14) {
2727                         subdevice->single_reg =
2728                             reg_base + ME6000_AO_14_SINGLE_REG;
2729                 } else if (ao_idx == 15) {
2730                         subdevice->single_reg =
2731                             reg_base + ME6000_AO_15_SINGLE_REG;
2732                 } else {
2733                         PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx);
2734                         me_subdevice_deinit((me_subdevice_t *) subdevice);
2735                         if (subdevice->fifo) {
2736                                 free_pages((unsigned long)subdevice->circ_buf.
2737                                            buf, ME6000_AO_CIRC_BUF_SIZE_ORDER);
2738                         }
2739                         subdevice->circ_buf.buf = NULL;
2740                         kfree(subdevice);
2741                         return NULL;
2742                 }
2743         }
2744 #ifdef MEDEBUG_DEBUG_REG
2745         subdevice->reg_base = reg_base;
2746 #endif
2747
2748         /* Override base class methods. */
2749         subdevice->base.me_subdevice_destructor = me6000_ao_destructor;
2750         subdevice->base.me_subdevice_io_reset_subdevice =
2751             me6000_ao_io_reset_subdevice;
2752         subdevice->base.me_subdevice_io_single_config =
2753             me6000_ao_io_single_config;
2754         subdevice->base.me_subdevice_io_single_read = me6000_ao_io_single_read;
2755         subdevice->base.me_subdevice_io_single_write =
2756             me6000_ao_io_single_write;
2757         subdevice->base.me_subdevice_io_stream_config =
2758             me6000_ao_io_stream_config;
2759         subdevice->base.me_subdevice_io_stream_new_values =
2760             me6000_ao_io_stream_new_values;
2761         subdevice->base.me_subdevice_io_stream_write =
2762             me6000_ao_io_stream_write;
2763         subdevice->base.me_subdevice_io_stream_start =
2764             me6000_ao_io_stream_start;
2765         subdevice->base.me_subdevice_io_stream_status =
2766             me6000_ao_io_stream_status;
2767         subdevice->base.me_subdevice_io_stream_stop = me6000_ao_io_stream_stop;
2768         subdevice->base.me_subdevice_query_number_channels =
2769             me6000_ao_query_number_channels;
2770         subdevice->base.me_subdevice_query_subdevice_type =
2771             me6000_ao_query_subdevice_type;
2772         subdevice->base.me_subdevice_query_subdevice_caps =
2773             me6000_ao_query_subdevice_caps;
2774         subdevice->base.me_subdevice_query_subdevice_caps_args =
2775             me6000_ao_query_subdevice_caps_args;
2776         subdevice->base.me_subdevice_query_range_by_min_max =
2777             me6000_ao_query_range_by_min_max;
2778         subdevice->base.me_subdevice_query_number_ranges =
2779             me6000_ao_query_number_ranges;
2780         subdevice->base.me_subdevice_query_range_info =
2781             me6000_ao_query_range_info;
2782         subdevice->base.me_subdevice_query_timer = me6000_ao_query_timer;
2783
2784         //prepare work queue and work function
2785         subdevice->me6000_workqueue = me6000_wq;
2786
2787 /* workqueue API changed in kernel 2.6.20 */
2788         INIT_DELAYED_WORK(&subdevice->ao_control_task,
2789                           me6000_ao_work_control_task);
2790
2791         if (subdevice->fifo) {  //Set speed
2792                 outl(ME6000_AO_MIN_CHAN_TICKS - 1, subdevice->timer_reg);
2793                 subdevice->hardware_stop_delay = HZ / 10;       //100ms
2794         }
2795
2796         return subdevice;
2797 }
2798
2799 /** @brief Stop presentation. Preserve FIFOs.
2800 *
2801 * @param instance The subdevice instance (pointer).
2802 */
2803 inline int ao_stop_immediately(me6000_ao_subdevice_t *instance)
2804 {
2805         unsigned long cpu_flags;
2806         uint32_t ctrl;
2807         int timeout;
2808         int i;
2809         uint32_t single_mask;
2810
2811         if (instance->ao_idx < ME6000_AO_SINGLE_STATUS_OFFSET)
2812                 single_mask = 0x0000;
2813         else
2814                 single_mask = 0x0001 << (instance->ao_idx -
2815                                 ME6000_AO_SINGLE_STATUS_OFFSET);
2816
2817         timeout =
2818             (instance->hardware_stop_delay >
2819              (HZ / 10)) ? instance->hardware_stop_delay : HZ / 10;
2820         for (i = 0; i <= timeout; i++) {
2821                 if (instance->fifo) {
2822                         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2823                         // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2824                         ctrl = inl(instance->ctrl_reg);
2825                         ctrl |=
2826                             ME6000_AO_CTRL_BIT_STOP |
2827                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2828                         ctrl &=
2829                             ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
2830                               ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
2831                         outl(ctrl, instance->ctrl_reg);
2832                         PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2833                                    instance->reg_base,
2834                                    instance->ctrl_reg - instance->reg_base,
2835                                    ctrl);
2836                         spin_unlock_irqrestore(&instance->subdevice_lock,
2837                                                cpu_flags);
2838
2839                         if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {  // Exit.
2840                                 break;
2841                         }
2842                 } else {
2843                         if (!(inl(instance->status_reg) & single_mask)) {       // Exit.
2844                                 break;
2845                         }
2846                 }
2847
2848                 PINFO("<%s> Wait for stop: %d\n", __func__, i);
2849
2850                 //Still working!
2851                 set_current_state(TASK_INTERRUPTIBLE);
2852                 schedule_timeout(1);
2853         }
2854
2855         if (i > timeout) {
2856                 PERROR_CRITICAL("FSM IS BUSY!\n");
2857                 return ME_ERRNO_INTERNAL;
2858         }
2859         return ME_ERRNO_SUCCESS;
2860 }
2861
2862 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2863 * @note This is time critical function. Checking is done at begining and end only.
2864 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2865 *
2866 * @param instance The subdevice instance (pointer).
2867 * @param count Maximum number of copied data.
2868 * @param start_pos Position of the firs value in buffer.
2869 *
2870 * @return On success: Number of copied data.
2871 * @return On error/success: 0.  No datas were copied => no data in buffer.
2872 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2873 */
2874 inline int ao_write_data_wraparound(me6000_ao_subdevice_t *instance, int count,
2875                                     int start_pos)
2876 {                               /// @note This is time critical function!
2877         uint32_t status;
2878         uint32_t value;
2879         int pos =
2880             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2881         int local_count = count;
2882         int i = 1;
2883
2884         if (count <= 0) {       //Wrong count!
2885                 return 0;
2886         }
2887
2888         while (i < local_count) {
2889                 //Get value from buffer
2890                 value = *(instance->circ_buf.buf + pos);
2891                 //Prepare it
2892                 if (instance->ao_idx & 0x1) {
2893                         value <<= 16;
2894                 }
2895                 //Put value to FIFO
2896                 outl(value, instance->fifo_reg);
2897                 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2898
2899                 pos++;
2900                 pos &= instance->circ_buf.mask;
2901                 if (pos == instance->circ_buf.head) {
2902                         pos = instance->circ_buf.tail;
2903                 }
2904                 i++;
2905         }
2906
2907         status = inl(instance->status_reg);
2908         if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full before all datas were copied!
2909                 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2910                        instance->ao_idx);
2911                 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2912         } else {                //Add last value
2913                 value = *(instance->circ_buf.buf + pos);
2914                 if (instance->ao_idx & 0x1) {
2915                         value <<= 16;
2916                 }
2917                 //Put value to FIFO
2918                 outl(value, instance->fifo_reg);
2919                 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2920         }
2921
2922         PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
2923               local_count);
2924         return local_count;
2925 }
2926
2927 /** @brief Copy data from software buffer to fifo (fast).
2928 * @note This is time critical function. Checking is done at begining and end only.
2929 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2930 *
2931 * @param instance The subdevice instance (pointer).
2932 * @param count Maximum number of copied data.
2933 * @param start_pos Position of the firs value in buffer.
2934 *
2935 * @return On success: Number of copied data.
2936 * @return On error/success: 0.  No datas were copied => no data in buffer.
2937 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2938 */
2939 inline int ao_write_data(me6000_ao_subdevice_t *instance, int count,
2940                          int start_pos)
2941 {                               /// @note This is time critical function!
2942         uint32_t status;
2943         uint32_t value;
2944         int pos =
2945             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2946         int local_count = count;
2947         int max_count;
2948         int i = 1;
2949
2950         if (count <= 0) {       //Wrong count!
2951                 return 0;
2952         }
2953
2954         max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
2955         if (max_count <= 0) {   //No data to copy!
2956                 return 0;
2957         }
2958
2959         if (max_count < count) {
2960                 local_count = max_count;
2961         }
2962
2963         while (i < local_count) {
2964                 //Get value from buffer
2965                 value = *(instance->circ_buf.buf + pos);
2966                 //Prepare it
2967                 if (instance->ao_idx & 0x1) {
2968                         value <<= 16;
2969                 }
2970                 //Put value to FIFO
2971                 outl(value, instance->fifo_reg);
2972                 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2973
2974                 pos++;
2975                 pos &= instance->circ_buf.mask;
2976                 i++;
2977         }
2978
2979         status = inl(instance->status_reg);
2980         if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full before all datas were copied!
2981                 PERROR("idx=%d FIFO is full before all datas were copied!\n",
2982                        instance->ao_idx);
2983                 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2984         } else {                //Add last value
2985                 value = *(instance->circ_buf.buf + pos);
2986                 if (instance->ao_idx & 0x1) {
2987                         value <<= 16;
2988                 }
2989                 //Put value to FIFO
2990                 outl(value, instance->fifo_reg);
2991                 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2992         }
2993
2994         PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
2995         return local_count;
2996 }
2997
2998 /** @brief Copy data from software buffer to fifo (slow).
2999 * @note This is slow function that copy all data from buffer to FIFO with full control.
3000 *
3001 * @param instance The subdevice instance (pointer).
3002 * @param count Maximum number of copied data.
3003 * @param start_pos Position of the firs value in buffer.
3004 *
3005 * @return On success: Number of copied values.
3006 * @return On error/success: 0.  FIFO was full at begining.
3007 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
3008 */
3009 inline int ao_write_data_pooling(me6000_ao_subdevice_t *instance, int count,
3010                                  int start_pos)
3011 {                               /// @note This is slow function!
3012         uint32_t status;
3013         uint32_t value;
3014         int pos =
3015             (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
3016         int local_count = count;
3017         int i;
3018         int max_count;
3019
3020         if (count <= 0) {       //Wrong count!
3021                 PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
3022                 return 0;
3023         }
3024
3025         max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
3026         if (max_count <= 0) {   //No data to copy!
3027                 PERROR("idx=%d SLOW LOADED: No data to copy!\n",
3028                        instance->ao_idx);
3029                 return 0;
3030         }
3031
3032         if (max_count < count) {
3033                 local_count = max_count;
3034         }
3035
3036         for (i = 0; i < local_count; i++) {
3037                 status = inl(instance->status_reg);
3038                 if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full!
3039                         return i;
3040                 }
3041                 //Get value from buffer
3042                 value = *(instance->circ_buf.buf + pos);
3043                 //Prepare it
3044                 if (instance->ao_idx & 0x1) {
3045                         value <<= 16;
3046                 }
3047                 //Put value to FIFO
3048                 outl(value, instance->fifo_reg);
3049                 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
3050
3051                 pos++;
3052                 pos &= instance->circ_buf.mask;
3053         }
3054
3055         PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
3056         return local_count;
3057 }
3058
3059 /** @brief Copy data from user space to circular buffer.
3060 * @param instance The subdevice instance (pointer).
3061 * @param count Number of datas in user space.
3062 * @param user_values Buffer's pointer.
3063 *
3064 * @return On success: Number of copied values.
3065 * @return On error: -ME_ERRNO_INTERNAL.
3066 */
3067 inline int ao_get_data_from_user(me6000_ao_subdevice_t *instance, int count,
3068                                  int *user_values)
3069 {
3070         int i, err;
3071         int empty_space;
3072         int copied;
3073         int value;
3074
3075         empty_space = me_circ_buf_space(&instance->circ_buf);
3076         //We have only this space free.
3077         copied = (count < empty_space) ? count : empty_space;
3078         for (i = 0; i < copied; i++) {  //Copy from user to buffer
3079                 if ((err = get_user(value, (int *)(user_values + i)))) {
3080                         PERROR
3081                             ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
3082                              instance->ao_idx, user_values + i, err);
3083                         return -ME_ERRNO_INTERNAL;
3084                 }
3085                 /// @note The analog output in me6000 series has size of 16 bits.
3086                 *(instance->circ_buf.buf + instance->circ_buf.head) =
3087                     (uint16_t) value;
3088                 instance->circ_buf.head++;
3089                 instance->circ_buf.head &= instance->circ_buf.mask;
3090         }
3091
3092         PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
3093         return copied;
3094 }
3095
3096 static void me6000_ao_work_control_task(struct work_struct *work)
3097 {
3098         me6000_ao_subdevice_t *instance;
3099         unsigned long cpu_flags = 0;
3100         uint32_t status;
3101         uint32_t ctrl;
3102         uint32_t synch;
3103         int reschedule = 0;
3104         int signaling = 0;
3105         uint32_t single_mask;
3106
3107         instance =
3108             container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
3109         PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
3110               instance->ao_idx);
3111
3112         status = inl(instance->status_reg);
3113         PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
3114                    instance->status_reg - instance->reg_base, status);
3115
3116 /// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
3117 //      single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
3118         single_mask = *instance->triggering_flags & (0x1 << instance->ao_idx);
3119
3120         switch (instance->status) {     // Checking actual mode.
3121
3122                 // Not configured for work.
3123         case ao_status_none:
3124                 break;
3125
3126                 //This are stable modes. No need to do anything. (?)
3127         case ao_status_single_configured:
3128         case ao_status_stream_configured:
3129         case ao_status_stream_fifo_error:
3130         case ao_status_stream_buffer_error:
3131         case ao_status_stream_error:
3132                 PERROR("Shouldn't be running!.\n");
3133                 break;
3134
3135                 // Single modes
3136         case ao_status_single_run_wait:
3137         case ao_status_single_run:
3138         case ao_status_single_end_wait:
3139                 if (instance->fifo) {   // Extra registers.
3140                         if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working.
3141                                 if (((instance->fifo & ME6000_AO_HAS_FIFO)
3142                                      && (!(status & ME6000_AO_STATUS_BIT_EF)))
3143                                     || (!(instance->fifo & ME6000_AO_HAS_FIFO))) {      // Single is in end state.
3144                                         PDEBUG
3145                                             ("Single call has been complited.\n");
3146
3147                                         // Set correct value for single_read();
3148                                         instance->single_value =
3149                                             instance->single_value_in_fifo;
3150
3151                                         // Set status as 'ao_status_single_end'
3152                                         instance->status = ao_status_single_end;
3153
3154                                         spin_lock(instance->preload_reg_lock);
3155                                         if ((single_mask) && (*instance->preload_flags & (ME6000_AO_SYNC_HOLD << instance->ao_idx))) {  // This is one of synchronous start channels. Set all as triggered.
3156                                                 *instance->triggering_flags =
3157                                                     0x00000000;
3158                                         } else {
3159                                                 //Set this channel as triggered (none active).
3160                                                 *instance->triggering_flags &=
3161                                                     ~(0x1 << instance->ao_idx);
3162                                         }
3163                                         spin_unlock(instance->preload_reg_lock);
3164
3165                                         // Signal the end.
3166                                         signaling = 1;
3167                                         // Wait for stop ISM.
3168                                         reschedule = 1;
3169
3170                                         break;
3171                                 }
3172                         }
3173                         // Check timeout.
3174                         if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3175                                 PDEBUG("Timeout reached.\n");
3176                                 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3177                                 spin_lock_irqsave(&instance->subdevice_lock,
3178                                                   cpu_flags);
3179                                 ctrl = inl(instance->ctrl_reg);
3180                                 ctrl |=
3181                                     ME6000_AO_CTRL_BIT_STOP |
3182                                     ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3183                                 ctrl &=
3184                                     ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3185                                       ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3186                                 ctrl &=
3187                                     ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
3188                                       ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
3189                                 //Disabling FIFO
3190                                 ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
3191
3192                                 outl(ctrl, instance->ctrl_reg);
3193                                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3194                                            instance->reg_base,
3195                                            instance->ctrl_reg -
3196                                            instance->reg_base, ctrl);
3197                                 spin_unlock_irqrestore(&instance->
3198                                                        subdevice_lock,
3199                                                        cpu_flags);
3200
3201                                 //Reset interrupt latch
3202                                 inl(instance->irq_reset_reg);
3203
3204                                 spin_lock(instance->preload_reg_lock);
3205                                 //Remove from synchronous start. Block triggering from this output.
3206                                 synch = inl(instance->preload_reg);
3207                                 synch &=
3208                                     ~((ME6000_AO_SYNC_HOLD |
3209                                        ME6000_AO_SYNC_EXT_TRIG) << instance->
3210                                       ao_idx);
3211                                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO - set to single safe mode
3212                                         synch |=
3213                                             ME6000_AO_SYNC_HOLD << instance->
3214                                             ao_idx;
3215                                 }
3216                                 outl(synch, instance->preload_reg);
3217                                 PDEBUG_REG
3218                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3219                                      instance->reg_base,
3220                                      instance->preload_reg - instance->reg_base,
3221                                      synch);
3222                                 //Set this channel as triggered (none active).
3223                                 *instance->triggering_flags &=
3224                                     ~(0x1 << instance->ao_idx);
3225                                 spin_unlock(instance->preload_reg_lock);
3226
3227                                 // Set correct value for single_read();
3228                                 instance->single_value_in_fifo =
3229                                     instance->single_value;
3230
3231                                 instance->status = ao_status_single_end;
3232
3233                                 // Signal the end.
3234                                 signaling = 1;
3235                         }
3236                 } else {        // No extra registers.
3237 /*
3238                                 if (!(status & single_mask))
3239                                 {// State machine is not working.
3240                                         PDEBUG("Single call has been complited.\n");
3241
3242                                         // Set correct value for single_read();
3243                                         instance->single_value = instance->single_value_in_fifo;
3244
3245                                         // Set status as 'ao_status_single_end'
3246                                         instance->status = ao_status_single_end;
3247
3248                                         // Signal the end.
3249                                         signaling = 1;
3250                                         // Wait for stop ISM.
3251                                         reschedule = 1;
3252
3253                                         break;
3254                                 }
3255 */
3256                         if (!single_mask) {     // Was triggered.
3257                                 PDEBUG("Single call has been complited.\n");
3258
3259                                 // Set correct value for single_read();
3260                                 instance->single_value =
3261                                     instance->single_value_in_fifo;
3262
3263                                 // Set status as 'ao_status_single_end'
3264                                 instance->status = ao_status_single_end;
3265
3266                                 // Signal the end.
3267                                 signaling = 1;
3268
3269                                 break;
3270                         }
3271                         // Check timeout.
3272                         if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3273                                 PDEBUG("Timeout reached.\n");
3274
3275                                 spin_lock(instance->preload_reg_lock);
3276                                 //Remove from synchronous start. Block triggering from this output.
3277                                 synch = inl(instance->preload_reg);
3278                                 synch &=
3279                                     ~(ME6000_AO_SYNC_EXT_TRIG << instance->
3280                                       ao_idx);
3281                                 synch |=
3282                                     ME6000_AO_SYNC_HOLD << instance->ao_idx;
3283
3284                                 outl(synch, instance->preload_reg);
3285                                 PDEBUG_REG
3286                                     ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3287                                      instance->reg_base,
3288                                      instance->preload_reg - instance->reg_base,
3289                                      synch);
3290                                 //Set this channel as triggered (none active).
3291                                 *instance->triggering_flags &=
3292                                     ~(0x1 << instance->ao_idx);
3293                                 spin_unlock(instance->preload_reg_lock);
3294
3295                                 // Restore old settings.
3296                                 PDEBUG("Write old value back to register.\n");
3297                                 outl(instance->single_value,
3298                                      instance->single_reg);
3299                                 PDEBUG_REG
3300                                     ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3301                                      instance->reg_base,
3302                                      instance->single_reg - instance->reg_base,
3303                                      instance->single_value);
3304
3305                                 // Set correct value for single_read();
3306                                 instance->single_value_in_fifo =
3307                                     instance->single_value;
3308
3309                                 instance->status = ao_status_single_end;
3310
3311                                 // Signal the end.
3312                                 signaling = 1;
3313                         }
3314                 }
3315
3316                 // Wait for stop.
3317                 reschedule = 1;
3318                 break;
3319
3320         case ao_status_stream_end:
3321                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3322                         PERROR_CRITICAL
3323                             ("Streaming on single device! This feature is not implemented in this version!\n");
3324                         instance->status = ao_status_stream_error;
3325                         // Signal the end.
3326                         signaling = 1;
3327                         break;
3328                 }
3329         case ao_status_single_end:
3330                 if (instance->fifo) {   // Extra registers.
3331                         if (status & ME6000_AO_STATUS_BIT_FSM) {        // State machine is working but the status is set to end. Force stop.
3332
3333                                 // Wait for stop.
3334                                 reschedule = 1;
3335                         }
3336
3337                         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3338                         // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3339                         ctrl = inl(instance->ctrl_reg);
3340                         ctrl |=
3341                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
3342                             ME6000_AO_CTRL_BIT_STOP;
3343                         ctrl &=
3344                             ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3345                               ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3346                         outl(ctrl, instance->ctrl_reg);
3347                         PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3348                                    instance->reg_base,
3349                                    instance->ctrl_reg - instance->reg_base,
3350                                    ctrl);
3351                         spin_unlock_irqrestore(&instance->subdevice_lock,
3352                                                cpu_flags);
3353
3354                         //Reset interrupt latch
3355                         inl(instance->irq_reset_reg);
3356                 } else {        // No extra registers.
3357 /*
3358                                 if (status & single_mask)
3359                                 {// State machine is working but the status is set to end. Force stop.
3360
3361                                         // Wait for stop.
3362                                         reschedule = 1;
3363                                 }
3364 */
3365                 }
3366                 break;
3367
3368                 // Stream modes
3369         case ao_status_stream_run_wait:
3370                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3371                         PERROR_CRITICAL
3372                             ("Streaming on single device! This feature is not implemented in this version!\n");
3373                         instance->status = ao_status_stream_error;
3374                         // Signal the end.
3375                         signaling = 1;
3376                         break;
3377                 }
3378
3379                 if (status & ME6000_AO_STATUS_BIT_FSM) {        // State machine is working. Waiting for start finish.
3380                         instance->status = ao_status_stream_run;
3381
3382                         // Signal end of this step
3383                         signaling = 1;
3384                 } else {        // State machine is not working.
3385                         if (!(status & ME6000_AO_STATUS_BIT_EF)) {      // FIFO is empty. Procedure has started and finish already!
3386                                 instance->status = ao_status_stream_end;
3387
3388                                 // Signal the end.
3389                                 signaling = 1;
3390                                 // Wait for stop.
3391                                 reschedule = 1;
3392                                 break;
3393                         }
3394                 }
3395
3396                 // Check timeout.
3397                 if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3398                         PDEBUG("Timeout reached.\n");
3399                         // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3400                         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3401                         ctrl = inl(instance->ctrl_reg);
3402                         ctrl |=
3403                             ME6000_AO_CTRL_BIT_STOP |
3404                             ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3405                         ctrl &=
3406                             ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3407                               ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3408                         outl(ctrl, instance->ctrl_reg);
3409                         PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3410                                    instance->reg_base,
3411                                    instance->ctrl_reg - instance->reg_base,
3412                                    ctrl);
3413                         spin_unlock_irqrestore(&instance->subdevice_lock,
3414                                                cpu_flags);
3415
3416                         //Reset interrupt latch
3417                         inl(instance->irq_reset_reg);
3418
3419                         spin_lock(instance->preload_reg_lock);
3420                         //Remove from synchronous start. Block triggering from this output.
3421                         synch = inl(instance->preload_reg);
3422                         synch &=
3423                             ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
3424                               instance->ao_idx);
3425                         outl(synch, instance->preload_reg);
3426                         PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3427                                    instance->reg_base,
3428                                    instance->preload_reg - instance->reg_base,
3429                                    synch);
3430                         spin_unlock(instance->preload_reg_lock);
3431
3432                         instance->status = ao_status_stream_end;
3433
3434                         // Signal the end.
3435                         signaling = 1;
3436                 }
3437                 // Wait for stop.
3438                 reschedule = 1;
3439                 break;
3440
3441         case ao_status_stream_run:
3442                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3443                         PERROR_CRITICAL
3444                             ("Streaming on single device! This feature is not implemented in this version!\n");
3445                         instance->status = ao_status_stream_error;
3446                         // Signal the end.
3447                         signaling = 1;
3448                         break;
3449                 }
3450
3451                 if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. This is an error.
3452                         // BROKEN PIPE!
3453                         if (!(status & ME6000_AO_STATUS_BIT_EF)) {      // FIFO is empty.
3454                                 if (me_circ_buf_values(&instance->circ_buf)) {  // Software buffer is not empty.
3455                                         if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. Requed data shown.
3456                                                 PDEBUG
3457                                                     ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3458                                                 instance->status =
3459                                                     ao_status_stream_end;
3460                                         } else {
3461                                                 PERROR
3462                                                     ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3463                                                 instance->status =
3464                                                     ao_status_stream_buffer_error;
3465                                         }
3466                                 } else {        // Software buffer is empty.
3467                                         PDEBUG
3468                                             ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3469                                         instance->status = ao_status_stream_end;
3470                                 }
3471                         } else {        // There are still datas in FIFO.
3472                                 if (me_circ_buf_values(&instance->circ_buf)) {  // Software buffer is not empty.
3473                                         PERROR
3474                                             ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3475                                 } else {        // Software buffer is empty.
3476                                         PERROR
3477                                             ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3478                                 }
3479                                 instance->status = ao_status_stream_fifo_error;
3480
3481                         }
3482
3483                         // Signal the failure.
3484                         signaling = 1;
3485                         break;
3486                 }
3487                 // Wait for stop.
3488                 reschedule = 1;
3489                 break;
3490
3491         case ao_status_stream_end_wait:
3492                 if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3493                         PERROR_CRITICAL
3494                             ("Streaming on single device! This feature is not implemented in this version!\n");
3495                         instance->status = ao_status_stream_error;
3496                         // Signal the end.
3497                         signaling = 1;
3498                         break;
3499                 }
3500
3501                 if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. Waiting for stop finish.
3502                         instance->status = ao_status_stream_end;
3503                         signaling = 1;
3504                 }
3505                 // State machine is working.
3506                 reschedule = 1;
3507                 break;
3508
3509         default:
3510                 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3511                                 instance->status);
3512                 instance->status = ao_status_stream_error;
3513                 // Signal the end.
3514                 signaling = 1;
3515                 break;
3516
3517         }
3518
3519         if (signaling) {        //Signal it.
3520                 wake_up_interruptible_all(&instance->wait_queue);
3521         }
3522
3523         if (instance->ao_control_task_flag && reschedule) {     // Reschedule task
3524                 queue_delayed_work(instance->me6000_workqueue,
3525                                    &instance->ao_control_task, 1);
3526         } else {
3527                 PINFO("<%s> Ending control task.\n", __func__);
3528         }
3529
3530 }
3531
3532 static int me6000_ao_query_range_by_min_max(me_subdevice_t *subdevice,
3533                                             int unit,
3534                                             int *min,
3535                                             int *max, int *maxdata, int *range)
3536 {
3537         me6000_ao_subdevice_t *instance;
3538
3539         instance = (me6000_ao_subdevice_t *) subdevice;
3540
3541         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3542
3543         if ((*max - *min) < 0) {
3544                 PERROR("Invalid minimum and maximum values specified.\n");
3545                 return ME_ERRNO_INVALID_MIN_MAX;
3546         }
3547
3548         if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3549                 if ((*max <= (instance->max + 1000)) && (*min >= instance->min)) {
3550                         *min = instance->min;
3551                         *max = instance->max;
3552                         *maxdata = ME6000_AO_MAX_DATA;
3553                         *range = 0;
3554                 } else {
3555                         PERROR("No matching range available.\n");
3556                         return ME_ERRNO_NO_RANGE;
3557                 }
3558         } else {
3559                 PERROR("Invalid physical unit specified.\n");
3560                 return ME_ERRNO_INVALID_UNIT;
3561         }
3562
3563         return ME_ERRNO_SUCCESS;
3564 }
3565
3566 static int me6000_ao_query_number_ranges(me_subdevice_t *subdevice,
3567                                          int unit, int *count)
3568 {
3569         me6000_ao_subdevice_t *instance;
3570
3571         instance = (me6000_ao_subdevice_t *) subdevice;
3572
3573         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3574
3575         if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3576                 *count = 1;
3577         } else {
3578                 *count = 0;
3579         }
3580
3581         return ME_ERRNO_SUCCESS;
3582 }
3583
3584 static int me6000_ao_query_range_info(me_subdevice_t *subdevice,
3585                                       int range,
3586                                       int *unit,
3587                                       int *min, int *max, int *maxdata)
3588 {
3589         me6000_ao_subdevice_t *instance;
3590
3591         instance = (me6000_ao_subdevice_t *) subdevice;
3592
3593         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3594
3595         if (range == 0) {
3596                 *unit = ME_UNIT_VOLT;
3597                 *min = instance->min;
3598                 *max = instance->max;
3599                 *maxdata = ME6000_AO_MAX_DATA;
3600         } else {
3601                 PERROR("Invalid range number specified.\n");
3602                 return ME_ERRNO_INVALID_RANGE;
3603         }
3604
3605         return ME_ERRNO_SUCCESS;
3606 }
3607
3608 static int me6000_ao_query_timer(me_subdevice_t *subdevice,
3609                                  int timer,
3610                                  int *base_frequency,
3611                                  long long *min_ticks, long long *max_ticks)
3612 {
3613         me6000_ao_subdevice_t *instance;
3614
3615         instance = (me6000_ao_subdevice_t *) subdevice;
3616
3617         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3618
3619         if (instance->fifo) {   //Streaming device.
3620                 *base_frequency = ME6000_AO_BASE_FREQUENCY;
3621                 if (timer == ME_TIMER_ACQ_START) {
3622                         *min_ticks = ME6000_AO_MIN_ACQ_TICKS;
3623                         *max_ticks = ME6000_AO_MAX_ACQ_TICKS;
3624                 } else if (timer == ME_TIMER_CONV_START) {
3625                         *min_ticks = ME6000_AO_MIN_CHAN_TICKS;
3626                         *max_ticks = ME6000_AO_MAX_CHAN_TICKS;
3627                 }
3628         } else {                //Not streaming device!
3629                 *base_frequency = 0;
3630                 *min_ticks = 0;
3631                 *max_ticks = 0;
3632         }
3633
3634         return ME_ERRNO_SUCCESS;
3635 }
3636
3637 static int me6000_ao_query_number_channels(me_subdevice_t *subdevice,
3638                                            int *number)
3639 {
3640         me6000_ao_subdevice_t *instance;
3641         instance = (me6000_ao_subdevice_t *) subdevice;
3642
3643         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3644
3645         *number = 1;
3646         return ME_ERRNO_SUCCESS;
3647 }
3648
3649 static int me6000_ao_query_subdevice_type(me_subdevice_t *subdevice,
3650                                           int *type, int *subtype)
3651 {
3652         me6000_ao_subdevice_t *instance;
3653
3654         instance = (me6000_ao_subdevice_t *) subdevice;
3655
3656         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3657
3658         *type = ME_TYPE_AO;
3659         *subtype =
3660             (instance->
3661              fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
3662             ME_SUBTYPE_SINGLE;
3663
3664         return ME_ERRNO_SUCCESS;
3665 }
3666
3667 static int me6000_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
3668 {
3669         me6000_ao_subdevice_t *instance;
3670         instance = (me6000_ao_subdevice_t *) subdevice;
3671
3672         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3673
3674         *caps =
3675             ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
3676                                            ME_CAPS_NONE);
3677
3678         return ME_ERRNO_SUCCESS;
3679 }
3680
3681 static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
3682                                                int cap, int *args, int count)
3683 {
3684         me6000_ao_subdevice_t *instance;
3685         int err = ME_ERRNO_SUCCESS;
3686
3687         instance = (me6000_ao_subdevice_t *) subdevice;
3688
3689         PDEBUG("executed. idx=%d\n", instance->ao_idx);
3690
3691         if (count != 1) {
3692                 PERROR("Invalid capability argument count.\n");
3693                 return ME_ERRNO_INVALID_CAP_ARG_COUNT;
3694         }
3695
3696         switch (cap) {
3697         case ME_CAP_AI_FIFO_SIZE:
3698                 args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
3699                 break;
3700
3701         case ME_CAP_AI_BUFFER_SIZE:
3702                 args[0] =
3703                     (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
3704                 break;
3705
3706         default:
3707                 PERROR("Invalid capability.\n");
3708                 err = ME_ERRNO_INVALID_CAP;
3709                 args[0] = 0;
3710         }
3711
3712         return err;
3713 }