linux/drivers/staging/meilhaus/me6000_ao.c
<<
>>
Prefs
   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 <asm/io.h>
  40#include <asm/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
  60static 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
  65static int me6000_ao_query_number_ranges(me_subdevice_t * subdevice,
  66                                         int unit, int *count);
  67
  68static int me6000_ao_query_range_info(me_subdevice_t * subdevice,
  69                                      int range,
  70                                      int *unit,
  71                                      int *min, int *max, int *maxdata);
  72
  73static 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
  78static int me6000_ao_query_number_channels(me_subdevice_t * subdevice,
  79                                           int *number);
  80
  81static int me6000_ao_query_subdevice_type(me_subdevice_t * subdevice,
  82                                          int *type, int *subtype);
  83
  84static int me6000_ao_query_subdevice_caps(me_subdevice_t * subdevice,
  85                                          int *caps);
  86
  87static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
  88                                               int cap, int *args, int count);
  89
  90/** Remove subdevice. */
  91static 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'. */
  94static int me6000_ao_io_reset_subdevice(me_subdevice_t * subdevice,
  95                                        struct file *filep, int flags);
  96
  97/** Set output as single */
  98static 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. */
 107static 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. */
 113static 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. */
 119static 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. */
 127static 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. */
 132static 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. */
 137static 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. */
 143static 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. */
 148static 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#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
 155static irqreturn_t me6000_ao_isr(int irq, void *dev_id);
 156#else
 157static irqreturn_t me6000_ao_isr(int irq, void *dev_id, struct pt_regs *regs);
 158#endif
 159
 160/** Copy data from circular buffer to fifo (fast) in wraparound mode. */
 161int inline ao_write_data_wraparound(me6000_ao_subdevice_t * instance, int count,
 162                                    int start_pos);
 163
 164/** Copy data from circular buffer to fifo (fast).*/
 165int inline ao_write_data(me6000_ao_subdevice_t * instance, int count,
 166                         int start_pos);
 167
 168/** Copy data from circular buffer to fifo (slow).*/
 169int inline ao_write_data_pooling(me6000_ao_subdevice_t * instance, int count,
 170                                 int start_pos);
 171
 172/** Copy data from user space to circular buffer. */
 173int inline ao_get_data_from_user(me6000_ao_subdevice_t * instance, int count,
 174                                 int *user_values);
 175
 176/** Stop presentation. Preserve FIFOs. */
 177int inline ao_stop_immediately(me6000_ao_subdevice_t * instance);
 178
 179/** Function for checking timeout in non-blocking mode. */
 180#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
 181static void me6000_ao_work_control_task(void *subdevice);
 182#else
 183static void me6000_ao_work_control_task(struct work_struct *work);
 184#endif
 185
 186/* Functions
 187 */
 188
 189static int me6000_ao_io_reset_subdevice(me_subdevice_t * subdevice,
 190                                        struct file *filep, int flags)
 191{
 192        me6000_ao_subdevice_t *instance;
 193        int err = ME_ERRNO_SUCCESS;
 194        uint32_t tmp;
 195        uint32_t ctrl;
 196
 197        instance = (me6000_ao_subdevice_t *) subdevice;
 198
 199        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 200
 201        if (flags) {
 202                PERROR("Invalid flag specified.\n");
 203                return ME_ERRNO_INVALID_FLAGS;
 204        }
 205
 206        ME_SUBDEVICE_ENTER;
 207
 208        instance->status = ao_status_none;
 209        instance->ao_control_task_flag = 0;
 210        cancel_delayed_work(&instance->ao_control_task);
 211        instance->timeout.delay = 0;
 212        instance->timeout.start_time = jiffies;
 213
 214        //Stop state machine.
 215        err = ao_stop_immediately(instance);
 216
 217        //Remove from synchronous start.
 218        spin_lock(instance->preload_reg_lock);
 219        tmp = inl(instance->preload_reg);
 220        tmp &=
 221            ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
 222              ao_idx);
 223        outl(tmp, instance->preload_reg);
 224        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 225                   instance->preload_reg - instance->reg_base, tmp);
 226        *instance->preload_flags &=
 227            ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
 228              ao_idx);
 229
 230        //Reset triggering flag
 231        *instance->triggering_flags &= ~(0x1 << instance->ao_idx);
 232        spin_unlock(instance->preload_reg_lock);
 233
 234        if (instance->fifo) {
 235                //Set single mode, dissable FIFO, dissable external trigger, block interrupt.
 236                ctrl = ME6000_AO_MODE_SINGLE;
 237
 238                //Block ISM.
 239                ctrl |=
 240                    (ME6000_AO_CTRL_BIT_STOP |
 241                     ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
 242
 243                outl(ctrl, instance->ctrl_reg);
 244                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
 245                           instance->reg_base,
 246                           instance->ctrl_reg - instance->reg_base, ctrl);
 247                //Set speed
 248                outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
 249                //Reset interrupt latch
 250                inl(instance->irq_reset_reg);
 251        }
 252
 253        instance->hardware_stop_delay = HZ / 10;        //100ms
 254
 255        //Set output to 0V
 256        outl(0x8000, instance->single_reg);
 257        PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 258                   instance->single_reg - instance->reg_base, 0x8000);
 259
 260        instance->circ_buf.head = 0;
 261        instance->circ_buf.tail = 0;
 262        instance->preloaded_count = 0;
 263        instance->data_count = 0;
 264        instance->single_value = 0x8000;
 265        instance->single_value_in_fifo = 0x8000;
 266
 267        //Set status to signal that device is unconfigured.
 268        instance->status = ao_status_none;
 269        //Signal reset if user is on wait.
 270        wake_up_interruptible_all(&instance->wait_queue);
 271
 272        ME_SUBDEVICE_EXIT;
 273
 274        return err;
 275}
 276
 277static int me6000_ao_io_single_config(me_subdevice_t * subdevice,
 278                                      struct file *filep,
 279                                      int channel,
 280                                      int single_config,
 281                                      int ref,
 282                                      int trig_chan,
 283                                      int trig_type, int trig_edge, int flags)
 284{
 285        me6000_ao_subdevice_t *instance;
 286        int err = ME_ERRNO_SUCCESS;
 287        uint32_t ctrl;
 288        uint32_t sync;
 289        unsigned long cpu_flags;
 290
 291        instance = (me6000_ao_subdevice_t *) subdevice;
 292
 293        PDEBUG("executed. ID=%d\n", instance->ao_idx);
 294
 295        // Checking parameters
 296        if (flags) {
 297                PERROR
 298                    ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
 299                return ME_ERRNO_INVALID_FLAGS;
 300        }
 301
 302        if (instance->fifo) {   //Stream hardware (with or without fifo)
 303                if ((trig_edge == ME_TRIG_TYPE_SW)
 304                    && (trig_edge != ME_TRIG_EDGE_NONE)) {
 305                        PERROR
 306                            ("Invalid trigger edge. Software trigger has not edge.\n");
 307                        return ME_ERRNO_INVALID_TRIG_EDGE;
 308                }
 309
 310                if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
 311                        switch (trig_edge) {
 312                        case ME_TRIG_EDGE_ANY:
 313                        case ME_TRIG_EDGE_RISING:
 314                        case ME_TRIG_EDGE_FALLING:
 315                                break;
 316
 317                        default:
 318                                PERROR("Invalid trigger edge.\n");
 319                                return ME_ERRNO_INVALID_TRIG_EDGE;
 320                        }
 321                }
 322
 323                if ((trig_type != ME_TRIG_TYPE_SW)
 324                    && (trig_type != ME_TRIG_TYPE_EXT_DIGITAL)) {
 325                        PERROR
 326                            ("Invalid trigger type. Trigger must be software or digital.\n");
 327                        return ME_ERRNO_INVALID_TRIG_TYPE;
 328                }
 329        } else {                //Single
 330                if (trig_edge != ME_TRIG_EDGE_NONE) {
 331                        PERROR
 332                            ("Invalid trigger edge. Single output trigger hasn't own edge.\n");
 333                        return ME_ERRNO_INVALID_TRIG_EDGE;
 334                }
 335
 336                if (trig_type != ME_TRIG_TYPE_SW) {
 337                        PERROR
 338                            ("Invalid trigger type. Trigger must be software.\n");
 339                        return ME_ERRNO_INVALID_TRIG_TYPE;
 340                }
 341
 342        }
 343
 344        if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
 345            && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
 346                PERROR("Invalid trigger channel specified.\n");
 347                return ME_ERRNO_INVALID_TRIG_CHAN;
 348        }
 349/*
 350        if ((trig_type == ME_TRIG_TYPE_EXT_DIGITAL) && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS))
 351        {
 352                PERROR("Invalid trigger channel specified. Must be synchronous when digital is choose.\n");
 353                return ME_ERRNO_INVALID_TRIG_CHAN;
 354        }
 355*/
 356        if (ref != ME_REF_AO_GROUND) {
 357                PERROR
 358                    ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
 359                return ME_ERRNO_INVALID_REF;
 360        }
 361
 362        if (single_config != 0) {
 363                PERROR
 364                    ("Invalid single config specified. Only one range for anlog outputs is available.\n");
 365                return ME_ERRNO_INVALID_SINGLE_CONFIG;
 366        }
 367
 368        if (channel != 0) {
 369                PERROR
 370                    ("Invalid channel number specified. Analog output have only one channel.\n");
 371                return ME_ERRNO_INVALID_CHANNEL;
 372        }
 373
 374        ME_SUBDEVICE_ENTER;
 375
 376        //Subdevice running in stream mode!
 377        if ((instance->status >= ao_status_stream_run_wait)
 378            && (instance->status < ao_status_stream_end)) {
 379                PERROR("Subdevice is busy.\n");
 380                ME_SUBDEVICE_EXIT;
 381
 382                return ME_ERRNO_SUBDEVICE_BUSY;
 383        }
 384/// @note For single all calls (config and write) are erasing previous state!
 385
 386        instance->status = ao_status_none;
 387
 388        // Correct single mirrors
 389        instance->single_value_in_fifo = instance->single_value;
 390
 391        //Stop device
 392        err = ao_stop_immediately(instance);
 393        if (err) {
 394                PERROR_CRITICAL("FSM IS BUSY!\n");
 395                ME_SUBDEVICE_EXIT;
 396
 397                return ME_ERRNO_SUBDEVICE_BUSY;
 398        }
 399
 400        if (instance->fifo) {   // Set control register.
 401                spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
 402                // Set stop bit. Stop streaming mode (If running.).
 403                ctrl = inl(instance->ctrl_reg);
 404                //Reset all bits.
 405                ctrl =
 406                    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
 407                if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL) {
 408                        PINFO("External digital trigger.\n");
 409
 410                        if (trig_edge == ME_TRIG_EDGE_ANY) {
 411//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
 412                                instance->ctrl_trg =
 413                                    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
 414                                    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
 415                        } else if (trig_edge == ME_TRIG_EDGE_FALLING) {
 416//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
 417                                instance->ctrl_trg =
 418                                    ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
 419                        } else if (trig_edge == ME_TRIG_EDGE_RISING) {
 420                                instance->ctrl_trg = 0x0;
 421                        }
 422                } else if (trig_type == ME_TRIG_TYPE_SW) {
 423                        PDEBUG("SOFTWARE TRIGGER\n");
 424                        instance->ctrl_trg = 0x0;
 425                }
 426                outl(ctrl, instance->ctrl_reg);
 427                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
 428                           instance->reg_base,
 429                           instance->ctrl_reg - instance->reg_base, ctrl);
 430                spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
 431        } else {
 432                PDEBUG("SOFTWARE TRIGGER\n");
 433        }
 434
 435        // Set preload/synchronization register.
 436        spin_lock(instance->preload_reg_lock);
 437
 438        if (trig_type == ME_TRIG_TYPE_SW) {
 439                *instance->preload_flags &=
 440                    ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
 441        } else                  //if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL)
 442        {
 443                *instance->preload_flags |=
 444                    ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
 445        }
 446
 447        if (trig_chan == ME_TRIG_CHAN_DEFAULT) {
 448                *instance->preload_flags &=
 449                    ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
 450        } else                  //if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS)
 451        {
 452                *instance->preload_flags |=
 453                    ME6000_AO_SYNC_HOLD << instance->ao_idx;
 454        }
 455
 456        //Reset hardware register
 457        sync = inl(instance->preload_reg);
 458        PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 459                   instance->preload_reg - instance->reg_base, sync);
 460        sync &= ~(ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx);
 461        sync |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
 462
 463        //Output configured in default mode (safe one)
 464        outl(sync, instance->preload_reg);
 465        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 466                   instance->preload_reg - instance->reg_base, sync);
 467        spin_unlock(instance->preload_reg_lock);
 468
 469        instance->status = ao_status_single_configured;
 470
 471        ME_SUBDEVICE_EXIT;
 472
 473        return err;
 474}
 475
 476static int me6000_ao_io_single_read(me_subdevice_t * subdevice,
 477                                    struct file *filep,
 478                                    int channel,
 479                                    int *value, int time_out, int flags)
 480{
 481        me6000_ao_subdevice_t *instance;
 482        int err = ME_ERRNO_SUCCESS;
 483
 484        unsigned long j;
 485        unsigned long delay = 0;
 486
 487        instance = (me6000_ao_subdevice_t *) subdevice;
 488
 489        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 490
 491        if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
 492                PERROR("Invalid flag specified. %d\n", flags);
 493                return ME_ERRNO_INVALID_FLAGS;
 494        }
 495
 496        if ((instance->status >= ao_status_stream_configured)
 497            && (instance->status <= ao_status_stream_end)) {
 498                PERROR("Subdevice not configured to work in single mode!\n");
 499                return ME_ERRNO_PREVIOUS_CONFIG;
 500        }
 501
 502        if (channel != 0) {
 503                PERROR("Invalid channel number specified.\n");
 504                return ME_ERRNO_INVALID_CHANNEL;
 505        }
 506
 507        if (time_out < 0) {
 508                PERROR("Invalid timeout specified.\n");
 509                return ME_ERRNO_INVALID_TIMEOUT;
 510        }
 511
 512        ME_SUBDEVICE_ENTER;
 513        if ((!flags) && (instance->status == ao_status_single_run_wait)) {      //Blocking mode. Wait for trigger.
 514                if (time_out) {
 515                        delay = (time_out * HZ) / 1000;
 516                        if (delay == 0)
 517                                delay = 1;
 518                }
 519
 520                j = jiffies;
 521
 522                //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
 523                wait_event_interruptible_timeout(instance->wait_queue,
 524                                                 (instance->status !=
 525                                                  ao_status_single_run_wait),
 526                                                 (delay) ? delay : LONG_MAX);
 527
 528                if (instance->status == ao_status_none) {
 529                        PDEBUG("Single canceled.\n");
 530                        err = ME_ERRNO_CANCELLED;
 531                }
 532
 533                if (signal_pending(current)) {
 534                        PERROR("Wait on start of state machine interrupted.\n");
 535                        instance->status = ao_status_none;
 536                        ao_stop_immediately(instance);
 537                        err = ME_ERRNO_SIGNAL;
 538                }
 539
 540                if ((delay) && ((jiffies - j) >= delay)) {
 541                        PDEBUG("Timeout reached.\n");
 542                        err = ME_ERRNO_TIMEOUT;
 543                }
 544
 545                *value =
 546                    (!err) ? instance->single_value_in_fifo : instance->
 547                    single_value;
 548        } else {                //Non-blocking mode
 549                //Read value
 550                *value = instance->single_value;
 551        }
 552
 553        ME_SUBDEVICE_EXIT;
 554
 555        return err;
 556}
 557
 558static int me6000_ao_io_single_write(me_subdevice_t * subdevice,
 559                                     struct file *filep,
 560                                     int channel,
 561                                     int value, int time_out, int flags)
 562{
 563        me6000_ao_subdevice_t *instance;
 564        int err = ME_ERRNO_SUCCESS;
 565        unsigned long cpu_flags;
 566        unsigned long j;
 567        unsigned long delay = 0;
 568
 569        uint32_t sync_mask;
 570        uint32_t mode;
 571
 572        uint32_t tmp;
 573
 574/// Workaround for mix-mode - begin
 575        uint32_t ctrl = 0x0;
 576        uint32_t status;
 577/// Workaround for mix-mode - end
 578
 579        instance = (me6000_ao_subdevice_t *) subdevice;
 580
 581        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 582
 583        if (flags &
 584            ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
 585              ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
 586                PERROR("Invalid flag specified.\n");
 587                return ME_ERRNO_INVALID_FLAGS;
 588        }
 589
 590        if ((instance->status == ao_status_none)
 591            || (instance->status > ao_status_single_end)) {
 592                PERROR("Subdevice not configured to work in single mode!\n");
 593                return ME_ERRNO_PREVIOUS_CONFIG;
 594        }
 595
 596        if (channel != 0) {
 597                PERROR("Invalid channel number specified.\n");
 598                return ME_ERRNO_INVALID_CHANNEL;
 599        }
 600
 601        if (value & ~ME6000_AO_MAX_DATA) {
 602                PERROR("Invalid value provided.\n");
 603                return ME_ERRNO_VALUE_OUT_OF_RANGE;
 604        }
 605
 606        if (time_out < 0) {
 607                PERROR("Invalid timeout specified.\n");
 608                return ME_ERRNO_INVALID_TIMEOUT;
 609        }
 610
 611        ME_SUBDEVICE_ENTER;
 612
 613/// @note For single all calls (config and write) are erasing previous state!
 614
 615        //Cancel control task
 616        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
 617        instance->ao_control_task_flag = 0;
 618        cancel_delayed_work(&instance->ao_control_task);
 619
 620        // Correct single mirrors
 621        instance->single_value_in_fifo = instance->single_value;
 622
 623        //Stop device
 624        err = ao_stop_immediately(instance);
 625        if (err) {
 626                PERROR_CRITICAL("FSM IS BUSY!\n");
 627                ME_SUBDEVICE_EXIT;
 628
 629                return ME_ERRNO_SUBDEVICE_BUSY;
 630        }
 631
 632        if (time_out) {
 633                delay = (time_out * HZ) / 1000;
 634
 635                if (delay == 0)
 636                        delay = 1;
 637        }
 638
 639        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
 640
 641        instance->single_value_in_fifo = value;
 642
 643        if (instance->fifo) {
 644                ctrl = inl(instance->ctrl_reg);
 645        }
 646
 647        if (instance->fifo & ME6000_AO_HAS_FIFO) {      /// Workaround for mix-mode - begin
 648                //Set speed
 649                outl(ME6000_AO_MIN_CHAN_TICKS - 1, instance->timer_reg);
 650                PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
 651                           instance->reg_base,
 652                           instance->timer_reg - instance->reg_base,
 653                           (int)ME6000_AO_MIN_CHAN_TICKS);
 654                instance->hardware_stop_delay = HZ / 10;        //100ms
 655
 656                status = inl(instance->status_reg);
 657
 658                //Set the continous mode.
 659                ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
 660                ctrl |= ME6000_AO_MODE_CONTINUOUS;
 661
 662                //Prepare FIFO
 663                if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO wasn't enabeled. Do it.
 664                        PINFO("Enableing FIFO.\n");
 665                        ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
 666                        ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
 667                } else {        //Check if FIFO is empty
 668                        if (status & ME6000_AO_STATUS_BIT_EF) { //FIFO not empty
 669                                PINFO("Reseting FIFO.\n");
 670                                ctrl &=
 671                                    ~(ME6000_AO_CTRL_BIT_ENABLE_FIFO |
 672                                      ME6000_AO_CTRL_BIT_ENABLE_IRQ);
 673                                outl(ctrl, instance->ctrl_reg);
 674                                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
 675                                           instance->reg_base,
 676                                           instance->ctrl_reg -
 677                                           instance->reg_base, ctrl);
 678
 679                                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
 680                        } else {        //FIFO empty, only interrupt needs to be disabled!
 681                                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
 682                        }
 683                }
 684
 685                outl(ctrl, instance->ctrl_reg);
 686                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
 687                           instance->reg_base,
 688                           instance->ctrl_reg - instance->reg_base, ctrl);
 689
 690                //Reset interrupt latch
 691                inl(instance->irq_reset_reg);
 692
 693                //Write output - 1 value to FIFO
 694                if (instance->ao_idx & 0x1) {
 695                        outl(value <<= 16, instance->fifo_reg);
 696                        PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
 697                                   instance->reg_base,
 698                                   instance->fifo_reg - instance->reg_base,
 699                                   value <<= 16);
 700                } else {
 701                        outl(value, instance->fifo_reg);
 702                        PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
 703                                   instance->reg_base,
 704                                   instance->fifo_reg - instance->reg_base,
 705                                   value);
 706                }
 707                /// Workaround for mix-mode - end
 708        } else {                //No FIFO - always in single mode
 709                //Write value
 710                PDEBUG("Write value\n");
 711                outl(value, instance->single_reg);
 712                PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
 713                           instance->reg_base,
 714                           instance->single_reg - instance->reg_base, value);
 715        }
 716
 717        mode = *instance->preload_flags >> instance->ao_idx;
 718        mode &= (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG);
 719
 720        PINFO("Triggering mode: 0x%08x\n", mode);
 721
 722        spin_lock(instance->preload_reg_lock);
 723        sync_mask = inl(instance->preload_reg);
 724        PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 725                   instance->preload_reg - instance->reg_base, sync_mask);
 726        switch (mode) {
 727        case 0:         //0x00000000: Individual software
 728                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
 729
 730                if (instance->fifo & ME6000_AO_HAS_FIFO) {      // FIFO - Continous mode
 731                        ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
 732                        if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) {       //Now we can set correct mode.
 733                                sync_mask &=
 734                                    ~((ME6000_AO_SYNC_EXT_TRIG |
 735                                       ME6000_AO_SYNC_HOLD) << instance->
 736                                      ao_idx);
 737
 738                                outl(sync_mask, instance->preload_reg);
 739                                PDEBUG_REG
 740                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 741                                     instance->reg_base,
 742                                     instance->preload_reg - instance->reg_base,
 743                                     sync_mask);
 744                        }
 745                } else {        // No FIFO - Single mode: In this case resetting 'ME6000_AO_SYNC_HOLD' will trigger output.
 746                        if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {       //Now we can set correct mode. This is exception. It is set to synchronous and triggered later.
 747                                sync_mask &=
 748                                    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
 749                                      ao_idx);
 750                                sync_mask |=
 751                                    ME6000_AO_SYNC_HOLD << instance->ao_idx;
 752
 753                                outl(sync_mask, instance->preload_reg);
 754                                PDEBUG_REG
 755                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 756                                     instance->reg_base,
 757                                     instance->preload_reg - instance->reg_base,
 758                                     sync_mask);
 759                        }
 760                }
 761                instance->single_value = value;
 762                break;
 763
 764        case ME6000_AO_SYNC_EXT_TRIG:   //0x00010000: Individual hardware
 765                PDEBUG("DIGITAL TRIGGER\n");
 766                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
 767
 768                if (instance->fifo & ME6000_AO_HAS_FIFO) {      // FIFO - Continous mode
 769                        if ((sync_mask & ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != 0x0) {       //Now we can set correct mode.
 770                                sync_mask &=
 771                                    ~((ME6000_AO_SYNC_EXT_TRIG |
 772                                       ME6000_AO_SYNC_HOLD) << instance->
 773                                      ao_idx);
 774
 775                                outl(sync_mask, instance->preload_reg);
 776                                PDEBUG_REG
 777                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 778                                     instance->reg_base,
 779                                     instance->preload_reg - instance->reg_base,
 780                                     sync_mask);
 781                        }
 782                } else {        // No FIFO - Single mode
 783                        if ((sync_mask &
 784                             ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
 785                              instance->ao_idx)) != ME6000_AO_SYNC_HOLD) {
 786                                //Now we can set correct mode
 787                                sync_mask &=
 788                                    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
 789                                      ao_idx);
 790                                sync_mask |=
 791                                    ME6000_AO_SYNC_HOLD << instance->ao_idx;
 792
 793                                outl(sync_mask, instance->preload_reg);
 794                                PDEBUG_REG
 795                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 796                                     instance->reg_base,
 797                                     instance->preload_reg - instance->reg_base,
 798                                     sync_mask);
 799                        }
 800                }
 801                break;
 802
 803        case ME6000_AO_SYNC_HOLD:       //0x00000001: Synchronous software
 804                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
 805
 806                if ((sync_mask &
 807                     ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
 808                      instance->ao_idx)) !=
 809                    (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
 810                        //Now we can set correct mode
 811                        sync_mask |=
 812                            ME6000_AO_SYNC_EXT_TRIG << instance->ao_idx;
 813                        sync_mask |= ME6000_AO_SYNC_HOLD << instance->ao_idx;
 814                        outl(sync_mask, instance->preload_reg);
 815                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 816                                   instance->reg_base,
 817                                   instance->preload_reg - instance->reg_base,
 818                                   sync_mask);
 819                }
 820                //Set triggering flag
 821                *instance->triggering_flags |= 0x1 << instance->ao_idx;
 822                break;
 823
 824        case (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG):   //0x00010001: Synchronous hardware
 825                PDEBUG("DIGITAL TRIGGER\n");
 826                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
 827
 828                if ((sync_mask &
 829                     ((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
 830                      instance->ao_idx)) !=
 831                    (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG)) {
 832                        //Now we can set correct mode
 833                        sync_mask |=
 834                            (ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
 835                            instance->ao_idx;
 836                        outl(sync_mask, instance->preload_reg);
 837                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 838                                   instance->reg_base,
 839                                   instance->preload_reg - instance->reg_base,
 840                                   sync_mask);
 841                }
 842                //Set triggering flag
 843                *instance->triggering_flags |= 0x1 << instance->ao_idx;
 844                break;
 845        }
 846//              spin_unlock(instance->preload_reg_lock);        // Moved down.
 847
 848        if (instance->fifo) {   //Activate ISM (remove 'stop' bits)
 849                ctrl &=
 850                    ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
 851                      ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
 852                ctrl |= instance->ctrl_trg;
 853                ctrl &=
 854                    ~(ME6000_AO_CTRL_BIT_STOP |
 855                      ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
 856
 857                outl(ctrl, instance->ctrl_reg);
 858                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
 859                           instance->reg_base,
 860                           instance->ctrl_reg - instance->reg_base, ctrl);
 861        }
 862        spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
 863
 864/// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!
 865
 866        PINFO("<%s> start mode= 0x%08x %s\n", __func__, mode,
 867              (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
 868              "");
 869        if (instance->fifo & ME6000_AO_HAS_FIFO) {      // FIFO - Continous mode
 870                if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {       //Trigger outputs
 871                        //Add channel to start list
 872                        outl(sync_mask |
 873                             (ME6000_AO_SYNC_HOLD << instance->ao_idx),
 874                             instance->preload_reg);
 875                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 876                                   instance->reg_base,
 877                                   instance->preload_reg - instance->reg_base,
 878                                   sync_mask | (ME6000_AO_SYNC_HOLD <<
 879                                                instance->ao_idx));
 880
 881                        //Fire
 882                        PINFO
 883                            ("Fired all software synchronous outputs by software trigger.\n");
 884                        outl(0x8000, instance->single_reg);
 885                        PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
 886                                   instance->reg_base,
 887                                   instance->single_reg - instance->reg_base,
 888                                   0x8000);
 889
 890                        //Restore save settings
 891                        outl(sync_mask, instance->preload_reg);
 892                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 893                                   instance->reg_base,
 894                                   instance->preload_reg - instance->reg_base,
 895                                   sync_mask);
 896
 897                } else if (!mode) {     //Trigger outputs
 898/*                      //Remove channel from start list
 899                        outl(sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
 900                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
 901*/
 902                        //Fire
 903                        PINFO("Software trigger.\n");
 904                        outl(0x8000, instance->single_reg);
 905                        PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
 906                                   instance->reg_base,
 907                                   instance->single_reg - instance->reg_base,
 908                                   0x8000);
 909
 910/*                      //Restore save settings
 911                        outl(sync_mask, instance->preload_reg);
 912                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask);
 913*/
 914                }
 915/// @note This is mix-mode case. For now I do not have possibility to trigger first 4 channels (continous mode) and other (single) ones at once.
 916/// @note Because triggering is not working it can not be add to synchronous list. First 4 channels don't need this information, anyway.
 917                *instance->triggering_flags &= 0xFFFFFFF0;
 918        } else {                // No FIFO - Single mode
 919                if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {       //Fired all software synchronous outputs.
 920                        tmp = ~(*instance->preload_flags | 0xFFFF0000);
 921                        PINFO
 922                            ("Fired all software synchronous outputs. mask:0x%08x\n",
 923                             tmp);
 924                        tmp |= sync_mask & 0xFFFF0000;
 925                        // Add this channel to list
 926                        tmp &= ~(ME6000_AO_SYNC_HOLD << instance->ao_idx);
 927
 928                        //Fire
 929                        PINFO("Software trigger.\n");
 930                        outl(tmp, instance->preload_reg);
 931                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 932                                   instance->reg_base,
 933                                   instance->preload_reg - instance->reg_base,
 934                                   tmp);
 935
 936                        //Restore save settings
 937                        outl(sync_mask, instance->preload_reg);
 938                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 939                                   instance->reg_base,
 940                                   instance->preload_reg - instance->reg_base,
 941                                   sync_mask);
 942
 943                        //Set all as triggered.
 944                        *instance->triggering_flags = 0x0;
 945                } else if (!mode) {     // Add this channel to list
 946                        outl(sync_mask &
 947                             ~(ME6000_AO_SYNC_HOLD << instance->ao_idx),
 948                             instance->preload_reg);
 949                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 950                                   instance->reg_base,
 951                                   instance->preload_reg - instance->reg_base,
 952                                   sync_mask & ~(ME6000_AO_SYNC_HOLD <<
 953                                                 instance->ao_idx));
 954
 955                        //Fire
 956                        PINFO("Software trigger.\n");
 957
 958                        //Restore save settings
 959                        outl(sync_mask, instance->preload_reg);
 960                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
 961                                   instance->reg_base,
 962                                   instance->preload_reg - instance->reg_base,
 963                                   sync_mask);
 964
 965                        //Set all as triggered.
 966                        *instance->triggering_flags = 0x0;
 967                }
 968
 969        }
 970        spin_unlock(instance->preload_reg_lock);
 971
 972        instance->status = ao_status_single_run_wait;
 973
 974        instance->timeout.delay = delay;
 975        instance->timeout.start_time = jiffies;
 976        instance->ao_control_task_flag = 1;
 977        queue_delayed_work(instance->me6000_workqueue,
 978                           &instance->ao_control_task, 1);
 979
 980        if (!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
 981                j = jiffies;
 982
 983                //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
 984                wait_event_interruptible_timeout(instance->wait_queue,
 985                                                 (instance->status !=
 986                                                  ao_status_single_run_wait),
 987                                                 (delay) ? delay +
 988                                                 1 : LONG_MAX);
 989
 990                if (instance->status != ao_status_single_end) {
 991                        PDEBUG("Single canceled.\n");
 992                        err = ME_ERRNO_CANCELLED;
 993                }
 994
 995                if (signal_pending(current)) {
 996                        PERROR("Wait on start of state machine interrupted.\n");
 997                        instance->ao_control_task_flag = 0;
 998                        cancel_delayed_work(&instance->ao_control_task);
 999                        ao_stop_immediately(instance);
1000                        instance->status = ao_status_none;
1001                        err = ME_ERRNO_SIGNAL;
1002                }
1003
1004                if ((delay) && ((jiffies - j) >= delay)) {
1005                        if (instance->status == ao_status_single_end) {
1006                                PDEBUG("Timeout reached.\n");
1007                        } else if ((jiffies - j) > delay) {
1008                                PERROR
1009                                    ("Timeout reached. Not handled by control task!\n");
1010                                ao_stop_immediately(instance);
1011                        } else {
1012                                PERROR
1013                                    ("Timeout reached. Signal come but status is strange: %d\n",
1014                                     instance->status);
1015                                ao_stop_immediately(instance);
1016                        }
1017
1018                        instance->ao_control_task_flag = 0;
1019                        cancel_delayed_work(&instance->ao_control_task);
1020                        instance->status = ao_status_single_end;
1021                        err = ME_ERRNO_TIMEOUT;
1022                }
1023        }
1024
1025        ME_SUBDEVICE_EXIT;
1026
1027        return err;
1028}
1029
1030static int me6000_ao_io_stream_config(me_subdevice_t * subdevice,
1031                                      struct file *filep,
1032                                      meIOStreamConfig_t * config_list,
1033                                      int count,
1034                                      meIOStreamTrigger_t * trigger,
1035                                      int fifo_irq_threshold, int flags)
1036{
1037        me6000_ao_subdevice_t *instance;
1038        int err = ME_ERRNO_SUCCESS;
1039        uint32_t ctrl;
1040        unsigned long cpu_flags;
1041        uint64_t conv_ticks;
1042        unsigned int conv_start_ticks_low = trigger->iConvStartTicksLow;
1043        unsigned int conv_start_ticks_high = trigger->iConvStartTicksHigh;
1044
1045        instance = (me6000_ao_subdevice_t *) subdevice;
1046
1047        PDEBUG("executed. idx=%d\n", instance->ao_idx);
1048
1049        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1050                PERROR("Not a streaming ao.\n");
1051                return ME_ERRNO_NOT_SUPPORTED;
1052        }
1053
1054        conv_ticks =
1055            (uint64_t) conv_start_ticks_low +
1056            ((uint64_t) conv_start_ticks_high << 32);
1057
1058        if (flags &
1059            ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY |
1060              ME_IO_STREAM_CONFIG_WRAPAROUND)) {
1061                PERROR("Invalid flags.\n");
1062                return ME_ERRNO_INVALID_FLAGS;
1063        }
1064
1065        if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {
1066                if (!flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1067                        PERROR
1068                            ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
1069                        return ME_ERRNO_INVALID_FLAGS;
1070                }
1071
1072                if ((trigger->iAcqStopTrigType != ME_TRIG_TYPE_NONE)
1073                    || (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE)) {
1074                        PERROR
1075                            ("Hardware wraparound mode must be in infinite mode.\n");
1076                        return ME_ERRNO_INVALID_FLAGS;
1077                }
1078        }
1079
1080        if (count != 1) {
1081                PERROR("Only 1 entry in config list acceptable.\n");
1082                return ME_ERRNO_INVALID_CONFIG_LIST_COUNT;
1083        }
1084
1085        if (config_list[0].iChannel != 0) {
1086                PERROR("Invalid channel number specified.\n");
1087                return ME_ERRNO_INVALID_CHANNEL;
1088        }
1089
1090        if (config_list[0].iStreamConfig != 0) {
1091                PERROR("Only one range available.\n");
1092                return ME_ERRNO_INVALID_STREAM_CONFIG;
1093        }
1094
1095        if (config_list[0].iRef != ME_REF_AO_GROUND) {
1096                PERROR("Output is referenced to ground.\n");
1097                return ME_ERRNO_INVALID_REF;
1098        }
1099
1100        if ((trigger->iAcqStartTicksLow != 0)
1101            || (trigger->iAcqStartTicksHigh != 0)) {
1102                PERROR
1103                    ("Invalid acquisition start trigger argument specified.\n");
1104                return ME_ERRNO_INVALID_ACQ_START_ARG;
1105        }
1106
1107        if (config_list[0].iFlags) {
1108                PERROR("Invalid config list flag.\n");
1109                return ME_ERRNO_INVALID_FLAGS;
1110        }
1111
1112        if ((trigger->iAcqStartTrigType != ME_TRIG_TYPE_SW)
1113            && (trigger->iAcqStartTrigType != ME_TRIG_TYPE_EXT_DIGITAL)) {
1114                PERROR("Invalid acquisition start trigger type specified.\n");
1115                return ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE;
1116        }
1117
1118        if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {
1119                switch (trigger->iAcqStartTrigEdge) {
1120                case ME_TRIG_EDGE_RISING:
1121                case ME_TRIG_EDGE_FALLING:
1122                case ME_TRIG_EDGE_ANY:
1123                        break;
1124
1125                default:
1126                        PERROR
1127                            ("Invalid acquisition start trigger edge specified.\n");
1128                        return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1129                }
1130        }
1131
1132        if ((trigger->iAcqStartTrigType == ME_TRIG_TYPE_SW)
1133            && (trigger->iAcqStartTrigEdge != ME_TRIG_TYPE_NONE)) {
1134                PERROR("Invalid acquisition start trigger edge specified.\n");
1135                return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE;
1136        }
1137
1138        if (trigger->iScanStartTrigType != ME_TRIG_TYPE_FOLLOW) {
1139                PERROR("Invalid scan start trigger type specified.\n");
1140                return ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE;
1141        }
1142
1143        if (trigger->iConvStartTrigType != ME_TRIG_TYPE_TIMER) {
1144                PERROR("Invalid conv start trigger type specified.\n");
1145                return ME_ERRNO_INVALID_CONV_START_TRIG_TYPE;
1146        }
1147
1148        if ((conv_ticks < ME6000_AO_MIN_CHAN_TICKS)
1149            || (conv_ticks > ME6000_AO_MAX_CHAN_TICKS)) {
1150                PERROR("Invalid conv start trigger argument specified.\n");
1151                return ME_ERRNO_INVALID_CONV_START_ARG;
1152        }
1153
1154        if (trigger->iAcqStartTicksLow || trigger->iAcqStartTicksHigh) {
1155                PERROR("Invalid acq start trigger argument specified.\n");
1156                return ME_ERRNO_INVALID_ACQ_START_ARG;
1157        }
1158
1159        if (trigger->iScanStartTicksLow || trigger->iScanStartTicksHigh) {
1160                PERROR("Invalid scan start trigger argument specified.\n");
1161                return ME_ERRNO_INVALID_SCAN_START_ARG;
1162        }
1163
1164        switch (trigger->iScanStopTrigType) {
1165        case ME_TRIG_TYPE_NONE:
1166                if (trigger->iScanStopCount != 0) {
1167                        PERROR("Invalid scan stop count specified.\n");
1168                        return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1169                }
1170                break;
1171
1172        case ME_TRIG_TYPE_COUNT:
1173                if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1174                        if (trigger->iScanStopCount <= 0) {
1175                                PERROR("Invalid scan stop count specified.\n");
1176                                return ME_ERRNO_INVALID_SCAN_STOP_ARG;
1177                        }
1178                } else {
1179                        PERROR("The continous mode has not 'scan' contects.\n");
1180                        return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1181                }
1182                break;
1183
1184        default:
1185                PERROR("Invalid scan stop trigger type specified.\n");
1186                return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE;
1187        }
1188
1189        switch (trigger->iAcqStopTrigType) {
1190        case ME_TRIG_TYPE_NONE:
1191                if (trigger->iAcqStopCount != 0) {
1192                        PERROR("Invalid acq stop count specified.\n");
1193                        return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1194                }
1195                break;
1196
1197        case ME_TRIG_TYPE_COUNT:
1198                if (trigger->iScanStopTrigType != ME_TRIG_TYPE_NONE) {
1199                        PERROR("Invalid acq stop trigger type specified.\n");
1200                        return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1201                }
1202
1203                if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {
1204                        if (trigger->iAcqStopCount <= 0) {
1205                                PERROR
1206                                    ("The continous mode has not 'scan' contects.\n");
1207                                return ME_ERRNO_INVALID_ACQ_STOP_ARG;
1208                        }
1209                }
1210//                      else
1211//                      {
1212//                              PERROR("Invalid acq stop trigger type specified.\n");
1213//                              return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1214//                      }
1215
1216                break;
1217
1218        default:
1219                PERROR("Invalid acq stop trigger type specified.\n");
1220                return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE;
1221        }
1222
1223        switch (trigger->iAcqStartTrigChan) {
1224        case ME_TRIG_CHAN_DEFAULT:
1225        case ME_TRIG_CHAN_SYNCHRONOUS:
1226                break;
1227
1228        default:
1229                PERROR("Invalid acq start trigger channel specified.\n");
1230                return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN;
1231        }
1232
1233        ME_SUBDEVICE_ENTER;
1234
1235        //Stop device
1236
1237        //Cancel control task
1238        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1239        instance->ao_control_task_flag = 0;
1240        cancel_delayed_work(&instance->ao_control_task);
1241
1242        //Check if state machine is stopped.
1243        err = ao_stop_immediately(instance);
1244        if (err) {
1245                PERROR_CRITICAL("FSM IS BUSY!\n");
1246                ME_SUBDEVICE_EXIT;
1247
1248                return ME_ERRNO_SUBDEVICE_BUSY;
1249        }
1250
1251        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1252        //Reset control register. Block all actions. Disable IRQ. Disable FIFO.
1253        ctrl = ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
1254        outl(ctrl, instance->ctrl_reg);
1255        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1256                   instance->ctrl_reg - instance->reg_base, ctrl);
1257
1258        //Reset interrupt latch
1259        inl(instance->irq_reset_reg);
1260
1261        //This is paranoic, but to be sure.
1262        instance->preloaded_count = 0;
1263        instance->data_count = 0;
1264        instance->circ_buf.head = 0;
1265        instance->circ_buf.tail = 0;
1266
1267        /* Set mode. */
1268        if (flags & ME_IO_STREAM_CONFIG_WRAPAROUND) {   //Wraparound
1269                if (flags & ME_IO_STREAM_CONFIG_HARDWARE_ONLY) {        //Hardware wraparound
1270                        PINFO("Hardware wraparound.\n");
1271                        ctrl |= ME6000_AO_MODE_WRAPAROUND;
1272                        instance->mode = ME6000_AO_HW_WRAP_MODE;
1273                } else {        //Software wraparound
1274                        PINFO("Software wraparound.\n");
1275                        ctrl |= ME6000_AO_MODE_CONTINUOUS;
1276                        instance->mode = ME6000_AO_SW_WRAP_MODE;
1277                }
1278        } else {                //Continous
1279                PINFO("Continous.\n");
1280                ctrl |= ME6000_AO_MODE_CONTINUOUS;
1281                instance->mode = ME6000_AO_CONTINOUS;
1282        }
1283
1284        //Set the trigger edge.
1285        if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {   //Set the trigger type and edge for external trigger.
1286                PINFO("External digital trigger.\n");
1287                instance->start_mode = ME6000_AO_EXT_TRIG;
1288
1289                switch (trigger->iAcqStartTrigEdge) {
1290                case ME_TRIG_EDGE_RISING:
1291                        PINFO("Set the trigger edge: rising.\n");
1292                        instance->ctrl_trg = 0x0;
1293                        break;
1294
1295                case ME_TRIG_EDGE_FALLING:
1296                        PINFO("Set the trigger edge: falling.\n");
1297//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1298                        instance->ctrl_trg = ME6000_AO_CTRL_BIT_EX_TRIG_EDGE;
1299                        break;
1300
1301                case ME_TRIG_EDGE_ANY:
1302                        PINFO("Set the trigger edge: both edges.\n");
1303//                                      ctrl |= ME6000_AO_CTRL_BIT_EX_TRIG_EDGE | ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1304                        instance->ctrl_trg =
1305                            ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
1306                            ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1307                        break;
1308                }
1309        } else {
1310                PINFO("Internal software trigger.\n");
1311                instance->start_mode = 0;
1312        }
1313
1314        //Set the stop mode and value.
1315        if (trigger->iAcqStopTrigType == ME_TRIG_TYPE_COUNT) {  //Amount of data
1316                instance->stop_mode = ME6000_AO_ACQ_STOP_MODE;
1317                instance->stop_count = trigger->iAcqStopCount;
1318        } else if (trigger->iScanStopTrigType == ME_TRIG_TYPE_COUNT) {  //Amount of 'scans'
1319                instance->stop_mode = ME6000_AO_SCAN_STOP_MODE;
1320                instance->stop_count = trigger->iScanStopCount;
1321        } else {                //Infinite
1322                instance->stop_mode = ME6000_AO_INF_STOP_MODE;
1323                instance->stop_count = 0;
1324        }
1325
1326        PINFO("Stop count: %d.\n", instance->stop_count);
1327
1328        if (trigger->iAcqStartTrigChan == ME_TRIG_CHAN_SYNCHRONOUS) {   //Synchronous start
1329                instance->start_mode |= ME6000_AO_SYNC_HOLD;
1330                if (trigger->iAcqStartTrigType == ME_TRIG_TYPE_EXT_DIGITAL) {   //Externaly triggered
1331                        PINFO("Synchronous start. Externaly trigger active.\n");
1332                        instance->start_mode |= ME6000_AO_SYNC_EXT_TRIG;
1333                }
1334#ifdef MEDEBUG_INFO
1335                else {
1336                        PINFO
1337                            ("Synchronous start. Externaly trigger dissabled.\n");
1338                }
1339#endif
1340
1341        }
1342        //Set speed
1343        outl(conv_ticks - 2, instance->timer_reg);
1344        PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%llx\n", instance->reg_base,
1345                   instance->timer_reg - instance->reg_base, conv_ticks - 2);
1346        instance->hardware_stop_delay = (int)(conv_ticks * HZ) / ME6000_AO_BASE_FREQUENCY;      //<== MUST be with cast!
1347
1348        // Write the control word
1349        outl(ctrl, instance->ctrl_reg);
1350        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1351                   instance->ctrl_reg - instance->reg_base, ctrl);
1352
1353        //Set status.
1354        instance->status = ao_status_stream_configured;
1355        spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1356
1357        ME_SUBDEVICE_EXIT;
1358
1359        return err;
1360}
1361
1362static int me6000_ao_io_stream_new_values(me_subdevice_t * subdevice,
1363                                          struct file *filep,
1364                                          int time_out, int *count, int flags)
1365{
1366        me6000_ao_subdevice_t *instance;
1367        int err = ME_ERRNO_SUCCESS;
1368        long t = 0;
1369        long j;
1370
1371        instance = (me6000_ao_subdevice_t *) subdevice;
1372
1373        PDEBUG("executed. idx=%d\n", instance->ao_idx);
1374
1375        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1376                PERROR("Not a streaming ao.\n");
1377                return ME_ERRNO_NOT_SUPPORTED;
1378        }
1379
1380        if (flags) {
1381                PERROR("Invalid flag specified.\n");
1382                return ME_ERRNO_INVALID_FLAGS;
1383        }
1384
1385        if (!instance->circ_buf.buf) {
1386                PERROR("Circular buffer not exists.\n");
1387                return ME_ERRNO_INTERNAL;
1388        }
1389
1390        if (time_out < 0) {
1391                PERROR("Invalid time_out specified.\n");
1392                return ME_ERRNO_INVALID_TIMEOUT;
1393        }
1394
1395        ME_SUBDEVICE_ENTER;
1396
1397        if (me_circ_buf_space(&instance->circ_buf)) {   //The buffer is NOT full.
1398                *count = me_circ_buf_space(&instance->circ_buf);
1399        } else {                //The buffer is full.
1400                if (time_out) {
1401                        t = (time_out * HZ) / 1000;
1402
1403                        if (t == 0)
1404                                t = 1;
1405                } else {        //Max time.
1406                        t = LONG_MAX;
1407                }
1408
1409                *count = 0;
1410
1411                j = jiffies;
1412
1413                //Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
1414                wait_event_interruptible_timeout(instance->wait_queue,
1415                                                 ((me_circ_buf_space
1416                                                   (&instance->circ_buf))
1417                                                  || !(inl(instance->status_reg)
1418                                                       &
1419                                                       ME6000_AO_STATUS_BIT_FSM)),
1420                                                 t);
1421
1422                if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {
1423                        PERROR("AO subdevice is not running.\n");
1424                        err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
1425                } else if (signal_pending(current)) {
1426                        PERROR("Wait on values interrupted from signal.\n");
1427                        instance->status = ao_status_none;
1428                        ao_stop_immediately(instance);
1429                        err = ME_ERRNO_SIGNAL;
1430                } else if ((jiffies - j) >= t) {
1431                        PERROR("Wait on values timed out.\n");
1432                        err = ME_ERRNO_TIMEOUT;
1433                } else {        //Uff... all is good. Inform user about empty space.
1434                        *count = me_circ_buf_space(&instance->circ_buf);
1435                }
1436        }
1437
1438        ME_SUBDEVICE_EXIT;
1439
1440        return err;
1441}
1442
1443static int me6000_ao_io_stream_start(me_subdevice_t * subdevice,
1444                                     struct file *filep,
1445                                     int start_mode, int time_out, int flags)
1446{
1447        me6000_ao_subdevice_t *instance;
1448        int err = ME_ERRNO_SUCCESS;
1449        unsigned long cpu_flags = 0;
1450        uint32_t status;
1451        uint32_t ctrl;
1452        uint32_t synch;
1453        int count = 0;
1454        int circ_buffer_count;
1455
1456        unsigned long ref;
1457        unsigned long delay = 0;
1458
1459        instance = (me6000_ao_subdevice_t *) subdevice;
1460
1461        PDEBUG("executed. idx=%d\n", instance->ao_idx);
1462
1463        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1464                PERROR("Not a streaming ao.\n");
1465                return ME_ERRNO_NOT_SUPPORTED;
1466        }
1467
1468        if (flags & ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS) {
1469                PERROR("Invalid flags.\n");
1470                return ME_ERRNO_INVALID_FLAGS;
1471        }
1472
1473        if (time_out < 0) {
1474                PERROR("Invalid timeout specified.\n");
1475                return ME_ERRNO_INVALID_TIMEOUT;
1476        }
1477
1478        if ((start_mode != ME_START_MODE_BLOCKING)
1479            && (start_mode != ME_START_MODE_NONBLOCKING)) {
1480                PERROR("Invalid start mode specified.\n");
1481                return ME_ERRNO_INVALID_START_MODE;
1482        }
1483
1484        if (time_out) {
1485                delay = (time_out * HZ) / 1000;
1486                if (delay == 0)
1487                        delay = 1;
1488        }
1489
1490        switch (instance->status) {     //Checking actual mode.
1491        case ao_status_stream_configured:
1492        case ao_status_stream_end:
1493                //Correct modes!
1494                break;
1495
1496                //The device is in wrong mode.
1497        case ao_status_none:
1498        case ao_status_single_configured:
1499        case ao_status_single_run_wait:
1500        case ao_status_single_run:
1501        case ao_status_single_end_wait:
1502                PERROR
1503                    ("Subdevice must be preinitialize correctly for streaming.\n");
1504                return ME_ERRNO_PREVIOUS_CONFIG;
1505
1506        case ao_status_stream_fifo_error:
1507        case ao_status_stream_buffer_error:
1508        case ao_status_stream_error:
1509                PDEBUG("Before restart broke stream 'STOP' must be caled.\n");
1510                return ME_STATUS_ERROR;
1511
1512        case ao_status_stream_run_wait:
1513        case ao_status_stream_run:
1514        case ao_status_stream_end_wait:
1515                PDEBUG("Stream is already working.\n");
1516                return ME_ERRNO_SUBDEVICE_BUSY;
1517
1518        default:
1519                instance->status = ao_status_stream_error;
1520                PERROR_CRITICAL("Status is in wrong state!\n");
1521                return ME_ERRNO_INTERNAL;
1522
1523        }
1524
1525        ME_SUBDEVICE_ENTER;
1526
1527        if (instance->mode == ME6000_AO_CONTINOUS) {    //Continous
1528                instance->circ_buf.tail += instance->preloaded_count;
1529                instance->circ_buf.tail &= instance->circ_buf.mask;
1530        }
1531        circ_buffer_count = me_circ_buf_values(&instance->circ_buf);
1532
1533        if (!circ_buffer_count && !instance->preloaded_count) { //No values in buffer
1534                ME_SUBDEVICE_EXIT;
1535                PERROR("No values in buffer!\n");
1536                return ME_ERRNO_LACK_OF_RESOURCES;
1537        }
1538
1539        //Cancel control task
1540        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
1541        instance->ao_control_task_flag = 0;
1542        cancel_delayed_work(&instance->ao_control_task);
1543
1544        //Stop device
1545        err = ao_stop_immediately(instance);
1546        if (err) {
1547                PERROR_CRITICAL("FSM IS BUSY!\n");
1548                ME_SUBDEVICE_EXIT;
1549
1550                return ME_ERRNO_SUBDEVICE_BUSY;
1551        }
1552        //Set values for single_read()
1553        instance->single_value = ME6000_AO_MAX_DATA + 1;
1554        instance->single_value_in_fifo = ME6000_AO_MAX_DATA + 1;
1555
1556        //Setting stop points
1557        if (instance->stop_mode == ME6000_AO_SCAN_STOP_MODE) {
1558                instance->stop_data_count =
1559                    instance->stop_count * circ_buffer_count;
1560        } else {
1561                instance->stop_data_count = instance->stop_count;
1562        }
1563
1564        if ((instance->stop_data_count != 0)
1565            && (instance->stop_data_count < circ_buffer_count)) {
1566                PERROR("More data in buffer than previously set limit!\n");
1567        }
1568
1569        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1570        ctrl = inl(instance->ctrl_reg);
1571        //Check FIFO
1572        if (!(ctrl & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) { //FIFO wasn't enabeled. Do it. <= This should be done by user call with ME_WRITE_MODE_PRELOAD
1573                PINFO("Enableing FIFO.\n");
1574                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
1575                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1576
1577                instance->preloaded_count = 0;
1578                instance->data_count = 0;
1579        } else {                //Block IRQ
1580                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1581        }
1582        outl(ctrl, instance->ctrl_reg);
1583        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1584                   instance->ctrl_reg - instance->reg_base, ctrl);
1585
1586        //Reset interrupt latch
1587        inl(instance->irq_reset_reg);
1588
1589        //Fill FIFO <= Generaly this should be done by user pre-load call but this is second place to do it.
1590        status = inl(instance->status_reg);
1591        if (!(status & ME6000_AO_STATUS_BIT_EF)) {      //FIFO empty
1592                if (instance->stop_data_count != 0) {
1593                        count = ME6000_AO_FIFO_COUNT;
1594                } else {
1595                        count =
1596                            (ME6000_AO_FIFO_COUNT <
1597                             instance->
1598                             stop_data_count) ? ME6000_AO_FIFO_COUNT :
1599                            instance->stop_data_count;
1600                }
1601
1602                //Copy data
1603                count =
1604                    ao_write_data(instance, count, instance->preloaded_count);
1605
1606                if (count < 0) {        //This should never happend!
1607                        PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1608                        spin_unlock_irqrestore(&instance->subdevice_lock,
1609                                               cpu_flags);
1610                        ME_SUBDEVICE_EXIT;
1611                        return ME_ERRNO_INTERNAL;
1612                }
1613        }
1614        //Set pre-load features.
1615        spin_lock(instance->preload_reg_lock);
1616        synch = inl(instance->preload_reg);
1617        synch &=
1618            ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) << instance->
1619              ao_idx);
1620        synch |=
1621            (instance->start_mode & ~ME6000_AO_EXT_TRIG) << instance->ao_idx;
1622        outl(synch, instance->preload_reg);
1623        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1624                   instance->preload_reg - instance->reg_base, synch);
1625        spin_unlock(instance->preload_reg_lock);
1626
1627        //Default count is '0'
1628        if (instance->mode == ME6000_AO_CONTINOUS) {    //Continous
1629                instance->preloaded_count = 0;
1630                instance->circ_buf.tail += count;
1631                instance->circ_buf.tail &= instance->circ_buf.mask;
1632        } else {                //Wraparound
1633                instance->preloaded_count += count;
1634                instance->data_count += count;
1635
1636                //Special case: Infinite wraparound with less than FIFO datas always should runs in hardware mode.
1637                if ((instance->stop_mode == ME6000_AO_INF_STOP_MODE)
1638                    && (circ_buffer_count <= ME6000_AO_FIFO_COUNT)) {   //Change to hardware wraparound
1639                        PDEBUG
1640                            ("Changeing mode from software wraparound to hardware wraparound.\n");
1641                        //Copy all data
1642                        count =
1643                            ao_write_data(instance, circ_buffer_count,
1644                                          instance->preloaded_count);
1645                        ctrl &= ~ME6000_AO_CTRL_MODE_MASK;
1646                        ctrl |= ME6000_AO_MODE_WRAPAROUND;
1647                }
1648
1649                if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) {     //Reset position indicator.
1650                        instance->preloaded_count = 0;
1651                } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) {       //This should never happend!
1652                        PERROR_CRITICAL
1653                            ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1654                        spin_unlock_irqrestore(&instance->subdevice_lock,
1655                                               cpu_flags);
1656                        ME_SUBDEVICE_EXIT;
1657                        return ME_ERRNO_INTERNAL;
1658                }
1659        }
1660
1661        //Set status to 'wait for start'
1662        instance->status = ao_status_stream_run_wait;
1663
1664        status = inl(instance->status_reg);
1665        //Start state machine and interrupts
1666        PINFO("<%s:%d> Start state machine.\n", __func__, __LINE__);
1667        ctrl &= ~(ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP);
1668        if (instance->start_mode == ME6000_AO_EXT_TRIG) {
1669                PDEBUG("DIGITAL TRIGGER\n");
1670                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG;
1671        }
1672        if (!(status & ME6000_AO_STATUS_BIT_HF)) {      //More than half!
1673                if ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS) {   //Enable IRQ only when hardware_continous is set and FIFO is more than half
1674                        PINFO("<%s:%d> Start interrupts.\n", __func__,
1675                              __LINE__);
1676                        ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1677                }
1678        }
1679        outl(ctrl, instance->ctrl_reg);
1680        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
1681                   instance->ctrl_reg - instance->reg_base, ctrl);
1682        spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
1683
1684        //Trigger output
1685        PINFO("<%s> start mode= 0x%x %s\n", __func__, instance->start_mode,
1686              (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) ? "SYNCHRONOUS" :
1687              "");
1688        if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {       //Trigger outputs
1689                spin_lock(instance->preload_reg_lock);
1690                synch = inl(instance->preload_reg);
1691                //Add channel to start list
1692                outl(synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx),
1693                     instance->preload_reg);
1694                PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1695                           instance->reg_base,
1696                           instance->preload_reg - instance->reg_base,
1697                           synch | (ME6000_AO_SYNC_HOLD << instance->ao_idx));
1698
1699                //Fire
1700                PINFO
1701                    ("Fired all software synchronous outputs by software trigger.\n");
1702                outl(0x8000, instance->single_reg);
1703                PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1704                           instance->reg_base,
1705                           instance->single_reg - instance->reg_base, 0x8000);
1706
1707                //Restore save settings
1708                outl(synch, instance->preload_reg);
1709                PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1710                           instance->reg_base,
1711                           instance->preload_reg - instance->reg_base, synch);
1712                spin_unlock(instance->preload_reg_lock);
1713        } else if (!instance->start_mode) {     //Trigger outputs
1714/*
1715                spin_lock(instance->preload_reg_lock);
1716                        synch = inl(instance->preload_reg);
1717                        //Remove channel from start list
1718                        outl(synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
1719                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch & ~(ME6000_AO_SYNC_HOLD << instance->ao_idx));
1720*/
1721                //Fire
1722                PINFO("Software trigger.\n");
1723                outl(0x8000, instance->single_reg);
1724                PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1725                           instance->reg_base,
1726                           instance->single_reg - instance->reg_base, 0x8000);
1727
1728/*
1729                        //Restore save settings
1730                        outl(synch, instance->preload_reg);
1731                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch);
1732                spin_unlock(instance->preload_reg_lock);
1733*/
1734        }
1735        // Set control task's timeout
1736        instance->timeout.delay = delay;
1737        instance->timeout.start_time = jiffies;
1738
1739        if (status & ME6000_AO_STATUS_BIT_HF) { //Less than half but not empty!
1740                PINFO("Less than half.\n");
1741                if (instance->stop_data_count == 0) {
1742                        count = ME6000_AO_FIFO_COUNT / 2;
1743                } else {
1744                        count =
1745                            ((ME6000_AO_FIFO_COUNT / 2) <
1746                             instance->stop_data_count) ? ME6000_AO_FIFO_COUNT /
1747                            2 : instance->stop_data_count;
1748                }
1749
1750                //Copy data
1751                count =
1752                    ao_write_data(instance, count, instance->preloaded_count);
1753
1754                if (count < 0) {        //This should never happend!
1755                        PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1756                        ME_SUBDEVICE_EXIT;
1757                        return ME_ERRNO_INTERNAL;
1758                }
1759
1760                if (instance->mode == ME6000_AO_CONTINOUS) {    //Continous
1761                        instance->circ_buf.tail += count;
1762                        instance->circ_buf.tail &= instance->circ_buf.mask;
1763                } else {        //Wraparound
1764                        instance->data_count += count;
1765                        instance->preloaded_count += count;
1766
1767                        if (instance->preloaded_count == me_circ_buf_values(&instance->circ_buf)) {     //Reset position indicator.
1768                                instance->preloaded_count = 0;
1769                        } else if (instance->preloaded_count > me_circ_buf_values(&instance->circ_buf)) {       //This should never happend!
1770                                PERROR_CRITICAL
1771                                    ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1772                                ME_SUBDEVICE_EXIT;
1773                                return ME_ERRNO_INTERNAL;
1774                        }
1775                }
1776
1777                status = inl(instance->status_reg);
1778                if (!(status & ME6000_AO_STATUS_BIT_HF)) {      //More than half!
1779                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
1780                        PINFO("<%s:%d> Start interrupts.\n", __func__,
1781                              __LINE__);
1782                        ctrl = inl(instance->ctrl_reg);
1783                        ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1784                        outl(ctrl, instance->ctrl_reg);
1785                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1786                                   instance->reg_base,
1787                                   instance->ctrl_reg - instance->reg_base,
1788                                   ctrl);
1789                        spin_unlock_irqrestore(&instance->subdevice_lock,
1790                                               cpu_flags);
1791                }
1792        }
1793        //Special case: Limited wraparound with less than HALF FIFO datas need work around to generate first interrupt.
1794        if ((instance->stop_mode != ME6000_AO_INF_STOP_MODE)
1795            && (instance->mode == ME6000_AO_SW_WRAP_MODE)
1796            && (circ_buffer_count <= (ME6000_AO_FIFO_COUNT / 2))) {     //Put more data to FIFO
1797                PINFO("Limited wraparound with less than HALF FIFO datas.\n");
1798                if (instance->preloaded_count) {        //This should never happend!
1799                        PERROR_CRITICAL
1800                            ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1801                        ME_SUBDEVICE_EXIT;
1802                        return ME_ERRNO_INTERNAL;
1803                }
1804
1805                while (instance->stop_data_count > instance->data_count) {      //Maximum data not set jet.
1806                        //Copy to buffer
1807                        if (circ_buffer_count != ao_write_data(instance, circ_buffer_count, 0)) {       //This should never happend!
1808                                PERROR_CRITICAL
1809                                    ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1810                                ME_SUBDEVICE_EXIT;
1811                                return ME_ERRNO_INTERNAL;
1812                        }
1813                        instance->data_count += circ_buffer_count;
1814
1815                        if (!((status = inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF)) {        //FIFO is more than half. Enable IRQ and end copy.
1816                                //Reset interrupt latch
1817                                inl(instance->irq_reset_reg);
1818
1819                                spin_lock_irqsave(&instance->subdevice_lock,
1820                                                  cpu_flags);
1821                                PINFO("<%s:%d> Start interrupts.\n",
1822                                      __func__, __LINE__);
1823                                ctrl = inl(instance->ctrl_reg);
1824                                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
1825                                outl(ctrl, instance->ctrl_reg);
1826                                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1827                                           instance->reg_base,
1828                                           instance->ctrl_reg -
1829                                           instance->reg_base, ctrl);
1830                                spin_unlock_irqrestore(&instance->
1831                                                       subdevice_lock,
1832                                                       cpu_flags);
1833                                break;
1834                        }
1835                }
1836        }
1837        // Schedule control task
1838        instance->ao_control_task_flag = 1;
1839        queue_delayed_work(instance->me6000_workqueue,
1840                           &instance->ao_control_task, 1);
1841
1842        if (start_mode == ME_START_MODE_BLOCKING) {     //Wait for start.
1843                ref = jiffies;
1844                //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1845                wait_event_interruptible_timeout(instance->wait_queue,
1846                                                 (instance->status !=
1847                                                  ao_status_stream_run_wait),
1848                                                 (delay) ? delay +
1849                                                 1 : LONG_MAX);
1850
1851                if ((instance->status != ao_status_stream_run)
1852                    && (instance->status != ao_status_stream_end)) {
1853                        PDEBUG("Starting stream canceled. %d\n",
1854                               instance->status);
1855                        err = ME_ERRNO_CANCELLED;
1856                }
1857
1858                if (signal_pending(current)) {
1859                        PERROR("Wait on start of state machine interrupted.\n");
1860                        instance->status = ao_status_none;
1861                        ao_stop_immediately(instance);
1862                        err = ME_ERRNO_SIGNAL;
1863                }
1864
1865                if ((delay) && ((jiffies - ref) >= delay)) {
1866                        if (instance->status != ao_status_stream_run) {
1867                                if (instance->status == ao_status_stream_end) {
1868                                        PDEBUG("Timeout reached.\n");
1869                                } else if ((jiffies - ref) > delay) {
1870                                        PERROR
1871                                            ("Timeout reached. Not handled by control task!\n");
1872                                        ao_stop_immediately(instance);
1873                                } else {
1874                                        PERROR
1875                                            ("Timeout reached. Signal come but status is strange: %d\n",
1876                                             instance->status);
1877                                        ao_stop_immediately(instance);
1878                                }
1879
1880                                instance->ao_control_task_flag = 0;
1881                                cancel_delayed_work(&instance->ao_control_task);
1882                                instance->status = ao_status_stream_end;
1883                                err = ME_ERRNO_TIMEOUT;
1884                        }
1885                }
1886        }
1887
1888        ME_SUBDEVICE_EXIT;
1889        return err;
1890}
1891
1892static int me6000_ao_io_stream_status(me_subdevice_t * subdevice,
1893                                      struct file *filep,
1894                                      int wait,
1895                                      int *status, int *values, int flags)
1896{
1897        me6000_ao_subdevice_t *instance;
1898        int err = ME_ERRNO_SUCCESS;
1899
1900        instance = (me6000_ao_subdevice_t *) subdevice;
1901
1902        PDEBUG("executed. idx=%d\n", instance->ao_idx);
1903
1904        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
1905                PERROR("Not a streaming ao.\n");
1906                return ME_ERRNO_NOT_SUPPORTED;
1907        }
1908
1909        if (flags) {
1910                PERROR("Invalid flag specified.\n");
1911                return ME_ERRNO_INVALID_FLAGS;
1912        }
1913
1914        if ((wait != ME_WAIT_NONE) && (wait != ME_WAIT_IDLE)) {
1915                PERROR("Invalid wait argument specified.\n");
1916                *status = ME_STATUS_INVALID;
1917                return ME_ERRNO_INVALID_WAIT;
1918        }
1919
1920        ME_SUBDEVICE_ENTER;
1921
1922        switch (instance->status) {
1923        case ao_status_single_configured:
1924        case ao_status_single_end:
1925        case ao_status_stream_configured:
1926        case ao_status_stream_end:
1927        case ao_status_stream_fifo_error:
1928        case ao_status_stream_buffer_error:
1929        case ao_status_stream_error:
1930                *status = ME_STATUS_IDLE;
1931                break;
1932
1933        case ao_status_single_run_wait:
1934        case ao_status_single_run:
1935        case ao_status_single_end_wait:
1936        case ao_status_stream_run_wait:
1937        case ao_status_stream_run:
1938        case ao_status_stream_end_wait:
1939                *status = ME_STATUS_BUSY;
1940                break;
1941
1942        case ao_status_none:
1943        default:
1944                *status =
1945                    (inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM) ?
1946                    ME_STATUS_BUSY : ME_STATUS_IDLE;
1947                break;
1948        }
1949
1950        if ((wait == ME_WAIT_IDLE) && (*status == ME_STATUS_BUSY)) {
1951                //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1952                wait_event_interruptible_timeout(instance->wait_queue,
1953                                                 ((instance->status !=
1954                                                   ao_status_single_run_wait)
1955                                                  && (instance->status !=
1956                                                      ao_status_single_run)
1957                                                  && (instance->status !=
1958                                                      ao_status_single_end_wait)
1959                                                  && (instance->status !=
1960                                                      ao_status_stream_run_wait)
1961                                                  && (instance->status !=
1962                                                      ao_status_stream_run)
1963                                                  && (instance->status !=
1964                                                      ao_status_stream_end_wait)),
1965                                                 LONG_MAX);
1966
1967                if (instance->status != ao_status_stream_end) {
1968                        PDEBUG("Wait for IDLE canceled. %d\n",
1969                               instance->status);
1970                        err = ME_ERRNO_CANCELLED;
1971                }
1972
1973                if (signal_pending(current)) {
1974                        PERROR("Wait for IDLE interrupted.\n");
1975                        instance->status = ao_status_none;
1976                        ao_stop_immediately(instance);
1977                        err = ME_ERRNO_SIGNAL;
1978                }
1979
1980                *status = ME_STATUS_IDLE;
1981        }
1982
1983        *values = me_circ_buf_space(&instance->circ_buf);
1984
1985        ME_SUBDEVICE_EXIT;
1986
1987        return err;
1988}
1989
1990static int me6000_ao_io_stream_stop(me_subdevice_t * subdevice,
1991                                    struct file *filep,
1992                                    int stop_mode, int flags)
1993{                               /// @note Stop work and empty buffer and FIFO
1994        int err = ME_ERRNO_SUCCESS;
1995        me6000_ao_subdevice_t *instance;
1996        unsigned long cpu_flags;
1997        volatile uint32_t ctrl;
1998
1999        instance = (me6000_ao_subdevice_t *) subdevice;
2000
2001        PDEBUG("executed. idx=%d\n", instance->ao_idx);
2002
2003        if (flags & ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS) {
2004                PERROR("Invalid flag specified.\n");
2005                return ME_ERRNO_INVALID_FLAGS;
2006        }
2007
2008        if ((stop_mode != ME_STOP_MODE_IMMEDIATE)
2009            && (stop_mode != ME_STOP_MODE_LAST_VALUE)) {
2010                PERROR("Invalid stop mode specified.\n");
2011                return ME_ERRNO_INVALID_STOP_MODE;
2012        }
2013
2014        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2015                PERROR("Not a streaming ao.\n");
2016                return ME_ERRNO_NOT_SUPPORTED;
2017        }
2018
2019        if (instance->status < ao_status_stream_configured) {
2020                //There is nothing to stop!
2021                PERROR("Subdevice not in streaming mode. %d\n",
2022                       instance->status);
2023                return ME_ERRNO_PREVIOUS_CONFIG;
2024        }
2025
2026        ME_SUBDEVICE_ENTER;
2027
2028        //Mark as stopping. => Software stop.
2029        instance->status = ao_status_stream_end_wait;
2030
2031        if (stop_mode == ME_STOP_MODE_IMMEDIATE) {      //Stopped now!
2032                err = ao_stop_immediately(instance);
2033        } else if (stop_mode == ME_STOP_MODE_LAST_VALUE) {
2034                ctrl = inl(instance->ctrl_reg) & ME6000_AO_CTRL_MODE_MASK;
2035                if (ctrl == ME6000_AO_MODE_WRAPAROUND) {        //Hardware wraparound => Hardware stop.
2036                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2037                        ctrl = inl(instance->ctrl_reg);
2038                        ctrl |= ME6000_AO_CTRL_BIT_STOP;
2039                        ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2040                        outl(ctrl, instance->ctrl_reg);
2041                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2042                                   instance->reg_base,
2043                                   instance->ctrl_reg - instance->reg_base,
2044                                   ctrl);
2045                        spin_unlock_irqrestore(&instance->subdevice_lock,
2046                                               cpu_flags);
2047
2048                        //Reset interrupt latch
2049                        inl(instance->irq_reset_reg);
2050                }
2051                //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
2052                wait_event_interruptible_timeout(instance->wait_queue,
2053                                                 (instance->status !=
2054                                                  ao_status_stream_end_wait),
2055                                                 LONG_MAX);
2056
2057                if (instance->status != ao_status_stream_end) {
2058                        PDEBUG("Stopping stream canceled.\n");
2059                        err = ME_ERRNO_CANCELLED;
2060                }
2061
2062                if (signal_pending(current)) {
2063                        PERROR("Stopping stream interrupted.\n");
2064                        instance->status = ao_status_none;
2065                        ao_stop_immediately(instance);
2066                        err = ME_ERRNO_SIGNAL;
2067                }
2068        }
2069
2070        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2071        ctrl = inl(instance->ctrl_reg);
2072        ctrl |= ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2073        ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2074        if (!flags) {           //Reset FIFO
2075                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2076        }
2077        outl(ctrl, instance->ctrl_reg);
2078        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
2079                   instance->ctrl_reg - instance->reg_base, ctrl);
2080        spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2081
2082        //Reset interrupt latch
2083        inl(instance->irq_reset_reg);
2084
2085        if (!flags) {           //Reset software buffer
2086                instance->circ_buf.head = 0;
2087                instance->circ_buf.tail = 0;
2088                instance->preloaded_count = 0;
2089                instance->data_count = 0;
2090        }
2091
2092        ME_SUBDEVICE_EXIT;
2093
2094        return err;
2095}
2096
2097static int me6000_ao_io_stream_write(me_subdevice_t * subdevice,
2098                                     struct file *filep,
2099                                     int write_mode,
2100                                     int *values, int *count, int flags)
2101{
2102        int err = ME_ERRNO_SUCCESS;
2103        me6000_ao_subdevice_t *instance;
2104        unsigned long cpu_flags = 0;
2105        uint32_t reg_copy;
2106
2107        int copied_from_user = 0;
2108        int left_to_copy_from_user = *count;
2109
2110        int copied_values;
2111
2112        instance = (me6000_ao_subdevice_t *) subdevice;
2113
2114        PDEBUG("executed. idx=%d\n", instance->ao_idx);
2115
2116        //Checking arguments
2117        if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {
2118                PERROR("Not a streaming ao.\n");
2119                return ME_ERRNO_NOT_SUPPORTED;
2120        }
2121
2122        if (flags) {
2123                PERROR("Invalid flag specified.\n");
2124                return ME_ERRNO_INVALID_FLAGS;
2125        }
2126
2127        if (*count <= 0) {
2128                PERROR("Invalid count of values specified.\n");
2129                return ME_ERRNO_INVALID_VALUE_COUNT;
2130        }
2131
2132        if (values == NULL) {
2133                PERROR("Invalid address of values specified.\n");
2134                return ME_ERRNO_INVALID_POINTER;
2135        }
2136
2137        if ((instance->status == ao_status_none) || (instance->status == ao_status_single_configured)) {        //The device is in single mode.
2138                PERROR
2139                    ("Subdevice must be preinitialize correctly for streaming.\n");
2140                return ME_ERRNO_PREVIOUS_CONFIG;
2141        }
2142
2143        switch (write_mode) {
2144        case ME_WRITE_MODE_PRELOAD:
2145
2146                //Device must be stopped.
2147                if ((instance->status != ao_status_stream_configured)
2148                    && (instance->status != ao_status_stream_end)) {
2149                        PERROR
2150                            ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2151                        return ME_ERRNO_PREVIOUS_CONFIG;
2152                }
2153                break;
2154        case ME_WRITE_MODE_NONBLOCKING:
2155        case ME_WRITE_MODE_BLOCKING:
2156                /// @note In blocking mode: When device is not runing and there is not enought space call will blocked up!
2157                /// @note Some other thread must empty buffer by strating engine.
2158                break;
2159
2160        default:
2161                PERROR("Invalid write mode specified.\n");
2162                return ME_ERRNO_INVALID_WRITE_MODE;
2163        }
2164
2165        if (instance->mode & ME6000_AO_WRAP_MODE) {     //Wraparound mode. Device must be stopped.
2166                if ((instance->status != ao_status_stream_configured)
2167                    && (instance->status != ao_status_stream_end)) {
2168                        PERROR
2169                            ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2170                        return ME_ERRNO_INVALID_WRITE_MODE;
2171                }
2172        }
2173
2174        if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2175            && (write_mode != ME_WRITE_MODE_PRELOAD)) {
2176/*
2177                PERROR("Only 'pre-load' write is acceptable in hardware wraparound mode.\n");
2178                return ME_ERRNO_PREVIOUS_CONFIG;
2179*/
2180                //This is transparent for user.
2181                PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
2182                write_mode = ME_WRITE_MODE_PRELOAD;
2183        }
2184
2185        ME_SUBDEVICE_ENTER;
2186
2187        if (write_mode == ME_WRITE_MODE_PRELOAD) {      //Init enviroment - preload
2188                spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2189                reg_copy = inl(instance->ctrl_reg);
2190                //Check FIFO
2191                if (!(reg_copy & ME6000_AO_CTRL_BIT_ENABLE_FIFO)) {     //FIFO not active. Enable it.
2192                        reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_FIFO;
2193                        outl(reg_copy, instance->ctrl_reg);
2194                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2195                                   instance->reg_base,
2196                                   instance->ctrl_reg - instance->reg_base,
2197                                   reg_copy);
2198                        instance->preloaded_count = 0;
2199                }
2200                spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
2201        }
2202
2203        while (1) {
2204                //Copy to buffer. This step is common for all modes.
2205                copied_from_user =
2206                    ao_get_data_from_user(instance, left_to_copy_from_user,
2207                                          values + (*count -
2208                                                    left_to_copy_from_user));
2209                left_to_copy_from_user -= copied_from_user;
2210
2211                reg_copy = inl(instance->status_reg);
2212                if ((instance->status == ao_status_stream_run) && !(reg_copy & ME6000_AO_STATUS_BIT_FSM)) {     //BROKEN PIPE! The state machine is stoped but logical status show that should be working.
2213                        PERROR("Broken pipe in write.\n");
2214                        err = ME_ERRNO_SUBDEVICE_NOT_RUNNING;
2215                        break;
2216                }
2217
2218                if ((instance->status == ao_status_stream_run) && (instance->mode == ME6000_AO_CONTINOUS) && (reg_copy & ME6000_AO_STATUS_BIT_HF)) {    //Continous mode runing and data are below half!
2219
2220                        // Block interrupts.
2221                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2222                        reg_copy = inl(instance->ctrl_reg);
2223                        reg_copy &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2224                        outl(reg_copy, instance->ctrl_reg);
2225                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2226                                   instance->reg_base,
2227                                   instance->ctrl_reg - instance->reg_base,
2228                                   reg_copy);
2229                        spin_unlock_irqrestore(&instance->subdevice_lock,
2230                                               cpu_flags);
2231
2232                        //Fast copy
2233                        copied_values =
2234                            ao_write_data(instance, ME6000_AO_FIFO_COUNT / 2,
2235                                          0);
2236                        if (copied_values > 0) {
2237                                instance->circ_buf.tail += copied_values;
2238                                instance->circ_buf.tail &=
2239                                    instance->circ_buf.mask;
2240                                continue;
2241                        }
2242                        //Reset interrupt latch
2243                        inl(instance->irq_reset_reg);
2244
2245                        // Activate interrupts.
2246                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2247                        reg_copy = inl(instance->ctrl_reg);
2248                        reg_copy |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2249                        outl(reg_copy, instance->ctrl_reg);
2250                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2251                                   instance->reg_base,
2252                                   instance->ctrl_reg - instance->reg_base,
2253                                   reg_copy);
2254                        spin_unlock_irqrestore(&instance->subdevice_lock,
2255                                               cpu_flags);
2256
2257                        if (copied_values == 0) {       //This was checked and never should happend!
2258                                PERROR_CRITICAL("COPY FINISH WITH 0!\n");
2259                        }
2260
2261                        if (copied_values < 0) {        //This was checked and never should happend!
2262                                PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
2263                                instance->status = ao_status_stream_fifo_error;
2264                                err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2265                                break;
2266                        }
2267                }
2268
2269                if (!left_to_copy_from_user) {  //All datas were copied.
2270                        break;
2271                } else {        //Not all datas were copied.
2272                        if (instance->mode & ME6000_AO_WRAP_MODE) {     //Error too much datas! Wraparound is limited in size!
2273                                PERROR
2274                                    ("Too much data for wraparound mode!  Exceeded size of %ld.\n",
2275                                     ME6000_AO_CIRC_BUF_COUNT - 1);
2276                                err = ME_ERRNO_RING_BUFFER_OVERFLOW;
2277                                break;
2278                        }
2279
2280                        if (write_mode != ME_WRITE_MODE_BLOCKING) {     //Non blocking calls
2281                                break;
2282                        }
2283
2284                        wait_event_interruptible(instance->wait_queue,
2285                                                 me_circ_buf_space(&instance->
2286                                                                   circ_buf));
2287
2288                        if (signal_pending(current)) {
2289                                PERROR("Writing interrupted by signal.\n");
2290                                instance->status = ao_status_none;
2291                                ao_stop_immediately(instance);
2292                                err = ME_ERRNO_SIGNAL;
2293                                break;
2294                        }
2295
2296                        if (instance->status == ao_status_none) {       //Reset
2297                                PERROR("Writing interrupted by reset.\n");
2298                                err = ME_ERRNO_CANCELLED;
2299                                break;
2300                        }
2301                }
2302        }
2303
2304        if (write_mode == ME_WRITE_MODE_PRELOAD) {      //Copy data to FIFO - preload
2305                copied_values =
2306                    ao_write_data_pooling(instance, ME6000_AO_FIFO_COUNT,
2307                                          instance->preloaded_count);
2308                instance->preloaded_count += copied_values;
2309                instance->data_count += copied_values;
2310
2311                if ((instance->mode == ME6000_AO_HW_WRAP_MODE)
2312                    && (me_circ_buf_values(&instance->circ_buf) >
2313                        ME6000_AO_FIFO_COUNT)) {
2314                        PERROR
2315                            ("Too much data for hardware wraparound mode! Exceeded size of %d.\n",
2316                             ME6000_AO_FIFO_COUNT);
2317                        err = ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2318                }
2319        }
2320
2321        *count = *count - left_to_copy_from_user;
2322        ME_SUBDEVICE_EXIT;
2323
2324        return err;
2325}
2326
2327#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
2328static irqreturn_t me6000_ao_isr(int irq, void *dev_id)
2329#else
2330static irqreturn_t me6000_ao_isr(int irq, void *dev_id, struct pt_regs *regs)
2331#endif
2332{
2333        me6000_ao_subdevice_t *instance = dev_id;
2334        uint32_t irq_status;
2335        uint32_t ctrl;
2336        uint32_t status;
2337        int count = 0;
2338
2339        PDEBUG("executed. idx=%d\n", instance->ao_idx);
2340
2341        if (irq != instance->irq) {
2342                PERROR("Incorrect interrupt num: %d.\n", irq);
2343                return IRQ_NONE;
2344        }
2345
2346        irq_status = inl(instance->irq_status_reg);
2347        if (!(irq_status & (ME6000_IRQ_STATUS_BIT_AO_HF << instance->ao_idx))) {
2348                PINFO("%ld Shared interrupt. %s(): ID=%d: status_reg=0x%04X\n",
2349                      jiffies, __func__, instance->ao_idx, irq_status);
2350                return IRQ_NONE;
2351        }
2352
2353        if (!instance->circ_buf.buf) {
2354                instance->status = ao_status_stream_error;
2355                PERROR_CRITICAL("CIRCULAR BUFFER NOT EXISTS!\n");
2356                //Block interrupts. Stop machine.
2357                ctrl = inl(instance->ctrl_reg);
2358                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2359                ctrl |=
2360                    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP | ME6000_AO_CTRL_BIT_STOP;
2361                outl(ctrl, instance->ctrl_reg);
2362                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2363                           instance->reg_base,
2364                           instance->ctrl_reg - instance->reg_base, ctrl);
2365
2366                //Inform user
2367                wake_up_interruptible_all(&instance->wait_queue);
2368                return IRQ_HANDLED;
2369        }
2370
2371        status = inl(instance->status_reg);
2372        if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     //Too late. Not working! END? BROKEN PIPE?
2373                /// @note Error checking was moved to separate task.
2374                PDEBUG("Interrupt come but ISM is not working!\n");
2375                //Block interrupts. Stop machine.
2376                ctrl = inl(instance->ctrl_reg);
2377                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2378                ctrl |=
2379                    ME6000_AO_CTRL_BIT_STOP | ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2380                outl(ctrl, instance->ctrl_reg);
2381                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2382                           instance->reg_base,
2383                           instance->ctrl_reg - instance->reg_base, ctrl);
2384
2385                //Reset interrupt latch
2386                inl(instance->irq_reset_reg);
2387
2388                /// @note User notification was also moved to separate task.
2389                return IRQ_HANDLED;
2390        }
2391        //General procedure. Process more datas.
2392
2393#ifdef MEDEBUG_DEBUG
2394        if (!me_circ_buf_values(&instance->circ_buf)) { //Buffer is empty!
2395                PDEBUG("Circular buffer empty!\n");
2396        }
2397#endif
2398
2399        //Check FIFO
2400        if (status & ME6000_AO_STATUS_BIT_HF) { //OK less than half
2401
2402                //Block interrupts
2403                ctrl = inl(instance->ctrl_reg);
2404                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2405                outl(ctrl, instance->ctrl_reg);
2406                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2407                           instance->reg_base,
2408                           instance->ctrl_reg - instance->reg_base, ctrl);
2409
2410                do {
2411                        //Calculate how many should be copied.
2412                        count =
2413                            (instance->stop_data_count) ? instance->
2414                            stop_data_count -
2415                            instance->data_count : ME6000_AO_FIFO_COUNT / 2;
2416                        if (ME6000_AO_FIFO_COUNT / 2 < count) {
2417                                count = ME6000_AO_FIFO_COUNT / 2;
2418                        }
2419                        //Copy data
2420                        if (instance->mode == ME6000_AO_CONTINOUS) {    //Continous
2421                                count = ao_write_data(instance, count, 0);
2422                                if (count > 0) {
2423                                        instance->circ_buf.tail += count;
2424                                        instance->circ_buf.tail &=
2425                                            instance->circ_buf.mask;
2426                                        instance->data_count += count;
2427
2428                                        if ((instance->status == ao_status_stream_end_wait) && !me_circ_buf_values(&instance->circ_buf)) {      //Stoping. Whole buffer was copied.
2429                                                break;
2430                                        }
2431                                }
2432                        } else if ((instance->mode == ME6000_AO_SW_WRAP_MODE) && ((ctrl & ME6000_AO_CTRL_MODE_MASK) == ME6000_AO_MODE_CONTINUOUS)) {    //Wraparound (software)
2433                                if (instance->status == ao_status_stream_end_wait) {    //We stoping => Copy to the end of the buffer.
2434                                        count =
2435                                            ao_write_data(instance, count, 0);
2436                                } else {        //Copy in wraparound mode.
2437                                        count =
2438                                            ao_write_data_wraparound(instance,
2439                                                                     count,
2440                                                                     instance->
2441                                                                     preloaded_count);
2442                                }
2443
2444                                if (count > 0) {
2445                                        instance->data_count += count;
2446                                        instance->preloaded_count += count;
2447                                        instance->preloaded_count %=
2448                                            me_circ_buf_values(&instance->
2449                                                               circ_buf);
2450
2451                                        if ((instance->status == ao_status_stream_end_wait) && !instance->preloaded_count) {    //Stoping. Whole buffer was copied.
2452                                                break;
2453                                        }
2454                                }
2455                        }
2456
2457                        if ((count <= 0) || (instance->stop_data_count && (instance->stop_data_count <= instance->data_count))) {       //End of work.
2458                                break;
2459                        }
2460                }               //Repeat if still is under half fifo
2461                while ((status =
2462                        inl(instance->status_reg)) & ME6000_AO_STATUS_BIT_HF);
2463
2464                //Unblock interrupts
2465                ctrl = inl(instance->ctrl_reg);
2466                if (count >= 0) {       //Copy was successful.
2467                        if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. No more interrupts.
2468                                PDEBUG("Finishing work. Interrupt disabled.\n");
2469                                instance->status = ao_status_stream_end_wait;
2470                        } else if (count > 0) { //Normal work. Enable interrupt.
2471                                PDEBUG("Normal work. Enable interrupt.\n");
2472                                ctrl |= ME6000_AO_CTRL_BIT_ENABLE_IRQ;
2473                        } else {        //Normal work but there are no more data in buffer. Interrupt blocked. stream_write() will unblock it.
2474                                PDEBUG
2475                                    ("No data in software buffer. Interrupt blocked.\n");
2476                        }
2477                } else {        //Error during copy.
2478                        instance->status = ao_status_stream_fifo_error;
2479                }
2480
2481                outl(ctrl, instance->ctrl_reg);
2482                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2483                           instance->reg_base,
2484                           instance->ctrl_reg - instance->reg_base, ctrl);
2485        } else {                //?? more than half
2486                PDEBUG
2487                    ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
2488        }
2489
2490        PINFO("ISR: Buffer count: %d.(T:%d H:%d)\n",
2491              me_circ_buf_values(&instance->circ_buf), instance->circ_buf.tail,
2492              instance->circ_buf.head);
2493        PINFO("ISR: Stop count: %d.\n", instance->stop_count);
2494        PINFO("ISR: Stop data count: %d.\n", instance->stop_data_count);
2495        PINFO("ISR: Data count: %d.\n", instance->data_count);
2496
2497        //Reset interrupt latch
2498        inl(instance->irq_reset_reg);
2499
2500        //Inform user
2501        wake_up_interruptible_all(&instance->wait_queue);
2502
2503        return IRQ_HANDLED;
2504}
2505
2506static void me6000_ao_destructor(struct me_subdevice *subdevice)
2507{
2508        me6000_ao_subdevice_t *instance;
2509
2510        instance = (me6000_ao_subdevice_t *) subdevice;
2511
2512        PDEBUG("executed. idx=%d\n", instance->ao_idx);
2513
2514        instance->ao_control_task_flag = 0;
2515
2516        // Reset subdevice to asure clean exit.
2517        me6000_ao_io_reset_subdevice(subdevice, NULL,
2518                                     ME_IO_RESET_SUBDEVICE_NO_FLAGS);
2519
2520        // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
2521        if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
2522                set_current_state(TASK_INTERRUPTIBLE);
2523                schedule_timeout(2);
2524        }
2525
2526        if (instance->fifo & ME6000_AO_HAS_FIFO) {
2527                if (instance->irq) {
2528                        free_irq(instance->irq, instance);
2529                        instance->irq = 0;
2530                }
2531
2532                if (instance->circ_buf.buf) {
2533                        PDEBUG("free circ_buf = %p size=%d",
2534                               instance->circ_buf.buf,
2535                               PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2536                        free_pages((unsigned long)instance->circ_buf.buf,
2537                                   ME6000_AO_CIRC_BUF_SIZE_ORDER);
2538                }
2539                instance->circ_buf.buf = NULL;
2540        }
2541
2542        me_subdevice_deinit(&instance->base);
2543        kfree(instance);
2544}
2545
2546me6000_ao_subdevice_t *me6000_ao_constructor(uint32_t reg_base,
2547                                             spinlock_t * preload_reg_lock,
2548                                             uint32_t * preload_flags,
2549                                             uint32_t * triggering_flags,
2550                                             int ao_idx,
2551                                             int fifo,
2552                                             int irq,
2553                                             int high_range,
2554                                             struct workqueue_struct *me6000_wq)
2555{
2556        me6000_ao_subdevice_t *subdevice;
2557        int err;
2558
2559        PDEBUG("executed ID=%d.\n", ao_idx);
2560
2561        /* Allocate memory for subdevice instance */
2562        subdevice = kmalloc(sizeof(me6000_ao_subdevice_t), GFP_KERNEL);
2563
2564        if (!subdevice) {
2565                PERROR("Cannot get memory for subdevice instance.\n");
2566                return NULL;
2567        }
2568
2569        memset(subdevice, 0, sizeof(me6000_ao_subdevice_t));
2570
2571        /* Initialize subdevice base class */
2572        err = me_subdevice_init(&subdevice->base);
2573
2574        if (err) {
2575                PERROR("Cannot initialize subdevice base class instance.\n");
2576                kfree(subdevice);
2577                return NULL;
2578        }
2579        // Initialize spin locks.
2580        spin_lock_init(&subdevice->subdevice_lock);
2581
2582        subdevice->preload_reg_lock = preload_reg_lock;
2583        subdevice->preload_flags = preload_flags;
2584        subdevice->triggering_flags = triggering_flags;
2585
2586        /* Store analog output index */
2587        subdevice->ao_idx = ao_idx;
2588
2589        /* Store if analog output has fifo */
2590        subdevice->fifo = fifo;
2591
2592        if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2593                /* Allocate and initialize circular buffer */
2594                subdevice->circ_buf.mask = ME6000_AO_CIRC_BUF_COUNT - 1;
2595                subdevice->circ_buf.buf =
2596                    (void *)__get_free_pages(GFP_KERNEL,
2597                                             ME6000_AO_CIRC_BUF_SIZE_ORDER);
2598                PDEBUG("circ_buf = %p size=%ld\n", subdevice->circ_buf.buf,
2599                       ME6000_AO_CIRC_BUF_SIZE);
2600
2601                if (!subdevice->circ_buf.buf) {
2602                        PERROR
2603                            ("Cannot initialize subdevice base class instance.\n");
2604                        kfree(subdevice);
2605                        return NULL;
2606                }
2607
2608                memset(subdevice->circ_buf.buf, 0, ME6000_AO_CIRC_BUF_SIZE);
2609        } else {
2610                subdevice->circ_buf.mask = 0;
2611                subdevice->circ_buf.buf = NULL;
2612        }
2613        subdevice->circ_buf.head = 0;
2614        subdevice->circ_buf.tail = 0;
2615
2616        subdevice->status = ao_status_none;
2617        subdevice->ao_control_task_flag = 0;
2618        subdevice->timeout.delay = 0;
2619        subdevice->timeout.start_time = jiffies;
2620
2621        /* Initialize wait queue */
2622        init_waitqueue_head(&subdevice->wait_queue);
2623
2624        /* Initialize single value to 0V */
2625        subdevice->single_value = 0x8000;
2626        subdevice->single_value_in_fifo = 0x8000;
2627
2628        /* Initialize range boarders */
2629        if (high_range) {
2630                subdevice->min = ME6000_AO_MIN_RANGE_HIGH;
2631                subdevice->max = ME6000_AO_MAX_RANGE_HIGH;
2632        } else {
2633                subdevice->min = ME6000_AO_MIN_RANGE;
2634                subdevice->max = ME6000_AO_MAX_RANGE;
2635        }
2636
2637        /* Register interrupt service routine */
2638
2639        if (subdevice->fifo & ME6000_AO_HAS_FIFO) {
2640                subdevice->irq = irq;
2641                if (request_irq(subdevice->irq, me6000_ao_isr,
2642#ifdef IRQF_DISABLED
2643                                IRQF_DISABLED | IRQF_SHARED,
2644#else
2645                                SA_INTERRUPT | SA_SHIRQ,
2646#endif
2647                                ME6000_NAME, subdevice)) {
2648                        PERROR("Cannot get interrupt line.\n");
2649                        PDEBUG("free circ_buf = %p size=%d",
2650                               subdevice->circ_buf.buf,
2651                               PAGE_SHIFT << ME6000_AO_CIRC_BUF_SIZE_ORDER);
2652                        free_pages((unsigned long)subdevice->circ_buf.buf,
2653                                   ME6000_AO_CIRC_BUF_SIZE_ORDER);
2654                        subdevice->circ_buf.buf = NULL;
2655                        kfree(subdevice);
2656                        return NULL;
2657                }
2658                PINFO("Registered irq=%d.\n", subdevice->irq);
2659        } else {
2660                subdevice->irq = 0;
2661        }
2662
2663        /* Initialize registers */
2664        // Only streamed subdevices support interrupts. For the rest this register has no meaning.
2665        subdevice->irq_status_reg = reg_base + ME6000_AO_IRQ_STATUS_REG;
2666        subdevice->preload_reg = reg_base + ME6000_AO_PRELOAD_REG;
2667
2668        if (ao_idx == 0) {
2669                subdevice->ctrl_reg = reg_base + ME6000_AO_00_CTRL_REG;
2670                subdevice->status_reg = reg_base + ME6000_AO_00_STATUS_REG;
2671                subdevice->fifo_reg = reg_base + ME6000_AO_00_FIFO_REG;
2672                subdevice->timer_reg = reg_base + ME6000_AO_00_TIMER_REG;
2673                subdevice->irq_reset_reg =
2674                    reg_base + ME6000_AO_00_IRQ_RESET_REG;
2675                subdevice->single_reg = reg_base + ME6000_AO_00_SINGLE_REG;
2676        } else if (ao_idx == 1) {
2677                subdevice->ctrl_reg = reg_base + ME6000_AO_01_CTRL_REG;
2678                subdevice->status_reg = reg_base + ME6000_AO_01_STATUS_REG;
2679                subdevice->fifo_reg = reg_base + ME6000_AO_01_FIFO_REG;
2680                subdevice->timer_reg = reg_base + ME6000_AO_01_TIMER_REG;
2681                subdevice->irq_reset_reg =
2682                    reg_base + ME6000_AO_01_IRQ_RESET_REG;
2683                subdevice->single_reg = reg_base + ME6000_AO_01_SINGLE_REG;
2684        } else if (ao_idx == 2) {
2685                subdevice->ctrl_reg = reg_base + ME6000_AO_02_CTRL_REG;
2686                subdevice->status_reg = reg_base + ME6000_AO_02_STATUS_REG;
2687                subdevice->fifo_reg = reg_base + ME6000_AO_02_FIFO_REG;
2688                subdevice->timer_reg = reg_base + ME6000_AO_02_TIMER_REG;
2689                subdevice->irq_reset_reg =
2690                    reg_base + ME6000_AO_02_IRQ_RESET_REG;
2691                subdevice->single_reg = reg_base + ME6000_AO_02_SINGLE_REG;
2692        } else if (ao_idx == 3) {
2693                subdevice->ctrl_reg = reg_base + ME6000_AO_03_CTRL_REG;
2694                subdevice->status_reg = reg_base + ME6000_AO_03_STATUS_REG;
2695                subdevice->fifo_reg = reg_base + ME6000_AO_03_FIFO_REG;
2696                subdevice->timer_reg = reg_base + ME6000_AO_03_TIMER_REG;
2697                subdevice->irq_reset_reg =
2698                    reg_base + ME6000_AO_03_IRQ_RESET_REG;
2699                subdevice->single_reg = reg_base + ME6000_AO_03_SINGLE_REG;
2700        } else {
2701                subdevice->ctrl_reg = reg_base + ME6000_AO_DUMY;
2702                subdevice->fifo_reg = reg_base + ME6000_AO_DUMY;
2703                subdevice->timer_reg = reg_base + ME6000_AO_DUMY;
2704                subdevice->irq_reset_reg = reg_base + ME6000_AO_DUMY;
2705                subdevice->single_reg = reg_base + ME6000_AO_DUMY;
2706
2707                subdevice->status_reg = reg_base + ME6000_AO_SINGLE_STATUS_REG;
2708                if (ao_idx == 4) {
2709                        subdevice->single_reg =
2710                            reg_base + ME6000_AO_04_SINGLE_REG;
2711                } else if (ao_idx == 5) {
2712                        subdevice->single_reg =
2713                            reg_base + ME6000_AO_05_SINGLE_REG;
2714                } else if (ao_idx == 6) {
2715                        subdevice->single_reg =
2716                            reg_base + ME6000_AO_06_SINGLE_REG;
2717                } else if (ao_idx == 7) {
2718                        subdevice->single_reg =
2719                            reg_base + ME6000_AO_07_SINGLE_REG;
2720                } else if (ao_idx == 8) {
2721                        subdevice->single_reg =
2722                            reg_base + ME6000_AO_08_SINGLE_REG;
2723                } else if (ao_idx == 9) {
2724                        subdevice->single_reg =
2725                            reg_base + ME6000_AO_09_SINGLE_REG;
2726                } else if (ao_idx == 10) {
2727                        subdevice->single_reg =
2728                            reg_base + ME6000_AO_10_SINGLE_REG;
2729                } else if (ao_idx == 11) {
2730                        subdevice->single_reg =
2731                            reg_base + ME6000_AO_11_SINGLE_REG;
2732                } else if (ao_idx == 12) {
2733                        subdevice->single_reg =
2734                            reg_base + ME6000_AO_12_SINGLE_REG;
2735                } else if (ao_idx == 13) {
2736                        subdevice->single_reg =
2737                            reg_base + ME6000_AO_13_SINGLE_REG;
2738                } else if (ao_idx == 14) {
2739                        subdevice->single_reg =
2740                            reg_base + ME6000_AO_14_SINGLE_REG;
2741                } else if (ao_idx == 15) {
2742                        subdevice->single_reg =
2743                            reg_base + ME6000_AO_15_SINGLE_REG;
2744                } else {
2745                        PERROR_CRITICAL("WRONG SUBDEVICE ID=%d!", ao_idx);
2746                        me_subdevice_deinit((me_subdevice_t *) subdevice);
2747                        if (subdevice->fifo) {
2748                                free_pages((unsigned long)subdevice->circ_buf.
2749                                           buf, ME6000_AO_CIRC_BUF_SIZE_ORDER);
2750                        }
2751                        subdevice->circ_buf.buf = NULL;
2752                        kfree(subdevice);
2753                        return NULL;
2754                }
2755        }
2756#ifdef MEDEBUG_DEBUG_REG
2757        subdevice->reg_base = reg_base;
2758#endif
2759
2760        /* Override base class methods. */
2761        subdevice->base.me_subdevice_destructor = me6000_ao_destructor;
2762        subdevice->base.me_subdevice_io_reset_subdevice =
2763            me6000_ao_io_reset_subdevice;
2764        subdevice->base.me_subdevice_io_single_config =
2765            me6000_ao_io_single_config;
2766        subdevice->base.me_subdevice_io_single_read = me6000_ao_io_single_read;
2767        subdevice->base.me_subdevice_io_single_write =
2768            me6000_ao_io_single_write;
2769        subdevice->base.me_subdevice_io_stream_config =
2770            me6000_ao_io_stream_config;
2771        subdevice->base.me_subdevice_io_stream_new_values =
2772            me6000_ao_io_stream_new_values;
2773        subdevice->base.me_subdevice_io_stream_write =
2774            me6000_ao_io_stream_write;
2775        subdevice->base.me_subdevice_io_stream_start =
2776            me6000_ao_io_stream_start;
2777        subdevice->base.me_subdevice_io_stream_status =
2778            me6000_ao_io_stream_status;
2779        subdevice->base.me_subdevice_io_stream_stop = me6000_ao_io_stream_stop;
2780        subdevice->base.me_subdevice_query_number_channels =
2781            me6000_ao_query_number_channels;
2782        subdevice->base.me_subdevice_query_subdevice_type =
2783            me6000_ao_query_subdevice_type;
2784        subdevice->base.me_subdevice_query_subdevice_caps =
2785            me6000_ao_query_subdevice_caps;
2786        subdevice->base.me_subdevice_query_subdevice_caps_args =
2787            me6000_ao_query_subdevice_caps_args;
2788        subdevice->base.me_subdevice_query_range_by_min_max =
2789            me6000_ao_query_range_by_min_max;
2790        subdevice->base.me_subdevice_query_number_ranges =
2791            me6000_ao_query_number_ranges;
2792        subdevice->base.me_subdevice_query_range_info =
2793            me6000_ao_query_range_info;
2794        subdevice->base.me_subdevice_query_timer = me6000_ao_query_timer;
2795
2796        //prepare work queue and work function
2797        subdevice->me6000_workqueue = me6000_wq;
2798
2799/* workqueue API changed in kernel 2.6.20 */
2800#if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
2801        INIT_WORK(&subdevice->ao_control_task, me6000_ao_work_control_task,
2802                  (void *)subdevice);
2803#else
2804        INIT_DELAYED_WORK(&subdevice->ao_control_task,
2805                          me6000_ao_work_control_task);
2806#endif
2807
2808        if (subdevice->fifo) {  //Set speed
2809                outl(ME6000_AO_MIN_CHAN_TICKS - 1, subdevice->timer_reg);
2810                subdevice->hardware_stop_delay = HZ / 10;       //100ms
2811        }
2812
2813        return subdevice;
2814}
2815
2816/** @brief Stop presentation. Preserve FIFOs.
2817*
2818* @param instance The subdevice instance (pointer).
2819*/
2820int inline ao_stop_immediately(me6000_ao_subdevice_t * instance)
2821{
2822        unsigned long cpu_flags;
2823        uint32_t ctrl;
2824        int timeout;
2825        int i;
2826        uint32_t single_mask;
2827
2828        single_mask =
2829            (instance->ao_idx - ME6000_AO_SINGLE_STATUS_OFFSET <
2830             0) ? 0x0000 : (0x0001 << (instance->ao_idx -
2831                                       ME6000_AO_SINGLE_STATUS_OFFSET));
2832
2833        timeout =
2834            (instance->hardware_stop_delay >
2835             (HZ / 10)) ? instance->hardware_stop_delay : HZ / 10;
2836        for (i = 0; i <= timeout; i++) {
2837                if (instance->fifo) {
2838                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
2839                        // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2840                        ctrl = inl(instance->ctrl_reg);
2841                        ctrl |=
2842                            ME6000_AO_CTRL_BIT_STOP |
2843                            ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
2844                        ctrl &=
2845                            ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
2846                              ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
2847                        outl(ctrl, instance->ctrl_reg);
2848                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2849                                   instance->reg_base,
2850                                   instance->ctrl_reg - instance->reg_base,
2851                                   ctrl);
2852                        spin_unlock_irqrestore(&instance->subdevice_lock,
2853                                               cpu_flags);
2854
2855                        if (!(inl(instance->status_reg) & ME6000_AO_STATUS_BIT_FSM)) {  // Exit.
2856                                break;
2857                        }
2858                } else {
2859                        if (!(inl(instance->status_reg) & single_mask)) {       // Exit.
2860                                break;
2861                        }
2862                }
2863
2864                PINFO("<%s> Wait for stop: %d\n", __func__, i);
2865
2866                //Still working!
2867                set_current_state(TASK_INTERRUPTIBLE);
2868                schedule_timeout(1);
2869        }
2870
2871        if (i > timeout) {
2872                PERROR_CRITICAL("FSM IS BUSY!\n");
2873                return ME_ERRNO_INTERNAL;
2874        }
2875        return ME_ERRNO_SUCCESS;
2876}
2877
2878/** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2879* @note This is time critical function. Checking is done at begining and end only.
2880* @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2881*
2882* @param instance The subdevice instance (pointer).
2883* @param count Maximum number of copied data.
2884* @param start_pos Position of the firs value in buffer.
2885*
2886* @return On success: Number of copied data.
2887* @return On error/success: 0.  No datas were copied => no data in buffer.
2888* @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2889*/
2890int inline ao_write_data_wraparound(me6000_ao_subdevice_t * instance, int count,
2891                                    int start_pos)
2892{                               /// @note This is time critical function!
2893        uint32_t status;
2894        uint32_t value;
2895        int pos =
2896            (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2897        int local_count = count;
2898        int i = 1;
2899
2900        if (count <= 0) {       //Wrong count!
2901                return 0;
2902        }
2903
2904        while (i < local_count) {
2905                //Get value from buffer
2906                value = *(instance->circ_buf.buf + pos);
2907                //Prepare it
2908                if (instance->ao_idx & 0x1) {
2909                        value <<= 16;
2910                }
2911                //Put value to FIFO
2912                outl(value, instance->fifo_reg);
2913                //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);
2914
2915                pos++;
2916                pos &= instance->circ_buf.mask;
2917                if (pos == instance->circ_buf.head) {
2918                        pos = instance->circ_buf.tail;
2919                }
2920                i++;
2921        }
2922
2923        status = inl(instance->status_reg);
2924        if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full before all datas were copied!
2925                PERROR("idx=%d FIFO is full before all datas were copied!\n",
2926                       instance->ao_idx);
2927                return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
2928        } else {                //Add last value
2929                value = *(instance->circ_buf.buf + pos);
2930                if (instance->ao_idx & 0x1) {
2931                        value <<= 16;
2932                }
2933                //Put value to FIFO
2934                outl(value, instance->fifo_reg);
2935                //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);
2936        }
2937
2938        PINFO("idx=%d WRAPAROUND LOADED %d values\n", instance->ao_idx,
2939              local_count);
2940        return local_count;
2941}
2942
2943/** @brief Copy data from software buffer to fifo (fast).
2944* @note This is time critical function. Checking is done at begining and end only.
2945* @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2946*
2947* @param instance The subdevice instance (pointer).
2948* @param count Maximum number of copied data.
2949* @param start_pos Position of the firs value in buffer.
2950*
2951* @return On success: Number of copied data.
2952* @return On error/success: 0.  No datas were copied => no data in buffer.
2953* @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2954*/
2955int inline ao_write_data(me6000_ao_subdevice_t * instance, int count,
2956                         int start_pos)
2957{                               /// @note This is time critical function!
2958        uint32_t status;
2959        uint32_t value;
2960        int pos =
2961            (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
2962        int local_count = count;
2963        int max_count;
2964        int i = 1;
2965
2966        if (count <= 0) {       //Wrong count!
2967                return 0;
2968        }
2969
2970        max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
2971        if (max_count <= 0) {   //No data to copy!
2972                return 0;
2973        }
2974
2975        if (max_count < count) {
2976                local_count = max_count;
2977        }
2978
2979        while (i < local_count) {
2980                //Get value from buffer
2981                value = *(instance->circ_buf.buf + pos);
2982                //Prepare it
2983                if (instance->ao_idx & 0x1) {
2984                        value <<= 16;
2985                }
2986                //Put value to FIFO
2987                outl(value, instance->fifo_reg);
2988                //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);
2989
2990                pos++;
2991                pos &= instance->circ_buf.mask;
2992                i++;
2993        }
2994
2995        status = inl(instance->status_reg);
2996        if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full before all datas were copied!
2997                PERROR("idx=%d FIFO is full before all datas were copied!\n",
2998                       instance->ao_idx);
2999                return -ME_ERRNO_FIFO_BUFFER_OVERFLOW;
3000        } else {                //Add last value
3001                value = *(instance->circ_buf.buf + pos);
3002                if (instance->ao_idx & 0x1) {
3003                        value <<= 16;
3004                }
3005                //Put value to FIFO
3006                outl(value, instance->fifo_reg);
3007                //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);
3008        }
3009
3010        PINFO("idx=%d FAST LOADED %d values\n", instance->ao_idx, local_count);
3011        return local_count;
3012}
3013
3014/** @brief Copy data from software buffer to fifo (slow).
3015* @note This is slow function that copy all data from buffer to FIFO with full control.
3016*
3017* @param instance The subdevice instance (pointer).
3018* @param count Maximum number of copied data.
3019* @param start_pos Position of the firs value in buffer.
3020*
3021* @return On success: Number of copied values.
3022* @return On error/success: 0.  FIFO was full at begining.
3023* @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
3024*/
3025int inline ao_write_data_pooling(me6000_ao_subdevice_t * instance, int count,
3026                                 int start_pos)
3027{                               /// @note This is slow function!
3028        uint32_t status;
3029        uint32_t value;
3030        int pos =
3031            (instance->circ_buf.tail + start_pos) & instance->circ_buf.mask;
3032        int local_count = count;
3033        int i;
3034        int max_count;
3035
3036        if (count <= 0) {       //Wrong count!
3037                PERROR("idx=%d SLOW LOADED: Wrong count!\n", instance->ao_idx);
3038                return 0;
3039        }
3040
3041        max_count = me_circ_buf_values(&instance->circ_buf) - start_pos;
3042        if (max_count <= 0) {   //No data to copy!
3043                PERROR("idx=%d SLOW LOADED: No data to copy!\n",
3044                       instance->ao_idx);
3045                return 0;
3046        }
3047
3048        if (max_count < count) {
3049                local_count = max_count;
3050        }
3051
3052        for (i = 0; i < local_count; i++) {
3053                status = inl(instance->status_reg);
3054                if (!(status & ME6000_AO_STATUS_BIT_FF)) {      //FIFO is full!
3055                        return i;
3056                }
3057                //Get value from buffer
3058                value = *(instance->circ_buf.buf + pos);
3059                //Prepare it
3060                if (instance->ao_idx & 0x1) {
3061                        value <<= 16;
3062                }
3063                //Put value to FIFO
3064                outl(value, instance->fifo_reg);
3065                //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);
3066
3067                pos++;
3068                pos &= instance->circ_buf.mask;
3069        }
3070
3071        PINFO("idx=%d SLOW LOADED %d values\n", instance->ao_idx, local_count);
3072        return local_count;
3073}
3074
3075/** @brief Copy data from user space to circular buffer.
3076* @param instance The subdevice instance (pointer).
3077* @param count Number of datas in user space.
3078* @param user_values Buffer's pointer.
3079*
3080* @return On success: Number of copied values.
3081* @return On error: -ME_ERRNO_INTERNAL.
3082*/
3083int inline ao_get_data_from_user(me6000_ao_subdevice_t * instance, int count,
3084                                 int *user_values)
3085{
3086        int i, err;
3087        int empty_space;
3088        int copied;
3089        int value;
3090
3091        empty_space = me_circ_buf_space(&instance->circ_buf);
3092        //We have only this space free.
3093        copied = (count < empty_space) ? count : empty_space;
3094        for (i = 0; i < copied; i++) {  //Copy from user to buffer
3095                if ((err = get_user(value, (int *)(user_values + i)))) {
3096                        PERROR
3097                            ("idx=%d BUFFER LOADED: get_user(0x%p) return an error: %d\n",
3098                             instance->ao_idx, user_values + i, err);
3099                        return -ME_ERRNO_INTERNAL;
3100                }
3101                /// @note The analog output in me6000 series has size of 16 bits.
3102                *(instance->circ_buf.buf + instance->circ_buf.head) =
3103                    (uint16_t) value;
3104                instance->circ_buf.head++;
3105                instance->circ_buf.head &= instance->circ_buf.mask;
3106        }
3107
3108        PINFO("idx=%d BUFFER LOADED %d values\n", instance->ao_idx, copied);
3109        return copied;
3110}
3111
3112static void me6000_ao_work_control_task(
3113#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
3114                                               void *subdevice
3115#else
3116                                               struct work_struct *work
3117#endif
3118    )
3119{
3120        me6000_ao_subdevice_t *instance;
3121        unsigned long cpu_flags = 0;
3122        uint32_t status;
3123        uint32_t ctrl;
3124        uint32_t synch;
3125        int reschedule = 0;
3126        int signaling = 0;
3127        uint32_t single_mask;
3128
3129#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
3130        instance = (me6000_ao_subdevice_t *) subdevice;
3131#else
3132        instance =
3133            container_of((void *)work, me6000_ao_subdevice_t, ao_control_task);
3134#endif
3135        PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
3136              instance->ao_idx);
3137
3138        status = inl(instance->status_reg);
3139        PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
3140                   instance->status_reg - instance->reg_base, status);
3141
3142/// @note AO_STATUS_BIT_FSM doesn't work as should be for pure single channels (idx>=4)
3143//      single_mask = (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET < 0) ? 0x0000 : (0x0001 << (instance->ao_idx-ME6000_AO_SINGLE_STATUS_OFFSET));
3144        single_mask = *instance->triggering_flags & (0x1 << instance->ao_idx);
3145
3146        switch (instance->status) {     // Checking actual mode.
3147
3148                // Not configured for work.
3149        case ao_status_none:
3150                break;
3151
3152                //This are stable modes. No need to do anything. (?)
3153        case ao_status_single_configured:
3154        case ao_status_stream_configured:
3155        case ao_status_stream_fifo_error:
3156        case ao_status_stream_buffer_error:
3157        case ao_status_stream_error:
3158                PERROR("Shouldn't be running!.\n");
3159                break;
3160
3161                // Single modes
3162        case ao_status_single_run_wait:
3163        case ao_status_single_run:
3164        case ao_status_single_end_wait:
3165                if (instance->fifo) {   // Extra registers.
3166                        if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working.
3167                                if (((instance->fifo & ME6000_AO_HAS_FIFO)
3168                                     && (!(status & ME6000_AO_STATUS_BIT_EF)))
3169                                    || (!(instance->fifo & ME6000_AO_HAS_FIFO))) {      // Single is in end state.
3170                                        PDEBUG
3171                                            ("Single call has been complited.\n");
3172
3173                                        // Set correct value for single_read();
3174                                        instance->single_value =
3175                                            instance->single_value_in_fifo;
3176
3177                                        // Set status as 'ao_status_single_end'
3178                                        instance->status = ao_status_single_end;
3179
3180                                        spin_lock(instance->preload_reg_lock);
3181                                        if ((single_mask) && (*instance->preload_flags & (ME6000_AO_SYNC_HOLD << instance->ao_idx))) {  // This is one of synchronous start channels. Set all as triggered.
3182                                                *instance->triggering_flags =
3183                                                    0x00000000;
3184                                        } else {
3185                                                //Set this channel as triggered (none active).
3186                                                *instance->triggering_flags &=
3187                                                    ~(0x1 << instance->ao_idx);
3188                                        }
3189                                        spin_unlock(instance->preload_reg_lock);
3190
3191                                        // Signal the end.
3192                                        signaling = 1;
3193                                        // Wait for stop ISM.
3194                                        reschedule = 1;
3195
3196                                        break;
3197                                }
3198                        }
3199                        // Check timeout.
3200                        if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3201                                PDEBUG("Timeout reached.\n");
3202                                // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3203                                spin_lock_irqsave(&instance->subdevice_lock,
3204                                                  cpu_flags);
3205                                ctrl = inl(instance->ctrl_reg);
3206                                ctrl |=
3207                                    ME6000_AO_CTRL_BIT_STOP |
3208                                    ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3209                                ctrl &=
3210                                    ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3211                                      ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3212                                ctrl &=
3213                                    ~(ME6000_AO_CTRL_BIT_EX_TRIG_EDGE |
3214                                      ME6000_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH);
3215                                //Disabling FIFO
3216                                ctrl &= ~ME6000_AO_CTRL_BIT_ENABLE_FIFO;
3217
3218                                outl(ctrl, instance->ctrl_reg);
3219                                PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3220                                           instance->reg_base,
3221                                           instance->ctrl_reg -
3222                                           instance->reg_base, ctrl);
3223                                spin_unlock_irqrestore(&instance->
3224                                                       subdevice_lock,
3225                                                       cpu_flags);
3226
3227                                //Reset interrupt latch
3228                                inl(instance->irq_reset_reg);
3229
3230                                spin_lock(instance->preload_reg_lock);
3231                                //Remove from synchronous start. Block triggering from this output.
3232                                synch = inl(instance->preload_reg);
3233                                synch &=
3234                                    ~((ME6000_AO_SYNC_HOLD |
3235                                       ME6000_AO_SYNC_EXT_TRIG) << instance->
3236                                      ao_idx);
3237                                if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO - set to single safe mode
3238                                        synch |=
3239                                            ME6000_AO_SYNC_HOLD << instance->
3240                                            ao_idx;
3241                                }
3242                                outl(synch, instance->preload_reg);
3243                                PDEBUG_REG
3244                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3245                                     instance->reg_base,
3246                                     instance->preload_reg - instance->reg_base,
3247                                     synch);
3248                                //Set this channel as triggered (none active).
3249                                *instance->triggering_flags &=
3250                                    ~(0x1 << instance->ao_idx);
3251                                spin_unlock(instance->preload_reg_lock);
3252
3253                                // Set correct value for single_read();
3254                                instance->single_value_in_fifo =
3255                                    instance->single_value;
3256
3257                                instance->status = ao_status_single_end;
3258
3259                                // Signal the end.
3260                                signaling = 1;
3261                        }
3262                } else {        // No extra registers.
3263/*
3264                                if (!(status & single_mask))
3265                                {// State machine is not working.
3266                                        PDEBUG("Single call has been complited.\n");
3267
3268                                        // Set correct value for single_read();
3269                                        instance->single_value = instance->single_value_in_fifo;
3270
3271                                        // Set status as 'ao_status_single_end'
3272                                        instance->status = ao_status_single_end;
3273
3274                                        // Signal the end.
3275                                        signaling = 1;
3276                                        // Wait for stop ISM.
3277                                        reschedule = 1;
3278
3279                                        break;
3280                                }
3281*/
3282                        if (!single_mask) {     // Was triggered.
3283                                PDEBUG("Single call has been complited.\n");
3284
3285                                // Set correct value for single_read();
3286                                instance->single_value =
3287                                    instance->single_value_in_fifo;
3288
3289                                // Set status as 'ao_status_single_end'
3290                                instance->status = ao_status_single_end;
3291
3292                                // Signal the end.
3293                                signaling = 1;
3294
3295                                break;
3296                        }
3297                        // Check timeout.
3298                        if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3299                                PDEBUG("Timeout reached.\n");
3300
3301                                spin_lock(instance->preload_reg_lock);
3302                                //Remove from synchronous start. Block triggering from this output.
3303                                synch = inl(instance->preload_reg);
3304                                synch &=
3305                                    ~(ME6000_AO_SYNC_EXT_TRIG << instance->
3306                                      ao_idx);
3307                                synch |=
3308                                    ME6000_AO_SYNC_HOLD << instance->ao_idx;
3309
3310                                outl(synch, instance->preload_reg);
3311                                PDEBUG_REG
3312                                    ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3313                                     instance->reg_base,
3314                                     instance->preload_reg - instance->reg_base,
3315                                     synch);
3316                                //Set this channel as triggered (none active).
3317                                *instance->triggering_flags &=
3318                                    ~(0x1 << instance->ao_idx);
3319                                spin_unlock(instance->preload_reg_lock);
3320
3321                                // Restore old settings.
3322                                PDEBUG("Write old value back to register.\n");
3323                                outl(instance->single_value,
3324                                     instance->single_reg);
3325                                PDEBUG_REG
3326                                    ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3327                                     instance->reg_base,
3328                                     instance->single_reg - instance->reg_base,
3329                                     instance->single_value);
3330
3331                                // Set correct value for single_read();
3332                                instance->single_value_in_fifo =
3333                                    instance->single_value;
3334
3335                                instance->status = ao_status_single_end;
3336
3337                                // Signal the end.
3338                                signaling = 1;
3339                        }
3340                }
3341
3342                // Wait for stop.
3343                reschedule = 1;
3344                break;
3345
3346        case ao_status_stream_end:
3347                if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3348                        PERROR_CRITICAL
3349                            ("Streaming on single device! This feature is not implemented in this version!\n");
3350                        instance->status = ao_status_stream_error;
3351                        // Signal the end.
3352                        signaling = 1;
3353                        break;
3354                }
3355        case ao_status_single_end:
3356                if (instance->fifo) {   // Extra registers.
3357                        if (status & ME6000_AO_STATUS_BIT_FSM) {        // State machine is working but the status is set to end. Force stop.
3358
3359                                // Wait for stop.
3360                                reschedule = 1;
3361                        }
3362
3363                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3364                        // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3365                        ctrl = inl(instance->ctrl_reg);
3366                        ctrl |=
3367                            ME6000_AO_CTRL_BIT_IMMEDIATE_STOP |
3368                            ME6000_AO_CTRL_BIT_STOP;
3369                        ctrl &=
3370                            ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3371                              ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3372                        outl(ctrl, instance->ctrl_reg);
3373                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3374                                   instance->reg_base,
3375                                   instance->ctrl_reg - instance->reg_base,
3376                                   ctrl);
3377                        spin_unlock_irqrestore(&instance->subdevice_lock,
3378                                               cpu_flags);
3379
3380                        //Reset interrupt latch
3381                        inl(instance->irq_reset_reg);
3382                } else {        // No extra registers.
3383/*
3384                                if (status & single_mask)
3385                                {// State machine is working but the status is set to end. Force stop.
3386
3387                                        // Wait for stop.
3388                                        reschedule = 1;
3389                                }
3390*/
3391                }
3392                break;
3393
3394                // Stream modes
3395        case ao_status_stream_run_wait:
3396                if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3397                        PERROR_CRITICAL
3398                            ("Streaming on single device! This feature is not implemented in this version!\n");
3399                        instance->status = ao_status_stream_error;
3400                        // Signal the end.
3401                        signaling = 1;
3402                        break;
3403                }
3404
3405                if (status & ME6000_AO_STATUS_BIT_FSM) {        // State machine is working. Waiting for start finish.
3406                        instance->status = ao_status_stream_run;
3407
3408                        // Signal end of this step
3409                        signaling = 1;
3410                } else {        // State machine is not working.
3411                        if (!(status & ME6000_AO_STATUS_BIT_EF)) {      // FIFO is empty. Procedure has started and finish already!
3412                                instance->status = ao_status_stream_end;
3413
3414                                // Signal the end.
3415                                signaling = 1;
3416                                // Wait for stop.
3417                                reschedule = 1;
3418                                break;
3419                        }
3420                }
3421
3422                // Check timeout.
3423                if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {       // Timeout
3424                        PDEBUG("Timeout reached.\n");
3425                        // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3426                        spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
3427                        ctrl = inl(instance->ctrl_reg);
3428                        ctrl |=
3429                            ME6000_AO_CTRL_BIT_STOP |
3430                            ME6000_AO_CTRL_BIT_IMMEDIATE_STOP;
3431                        ctrl &=
3432                            ~(ME6000_AO_CTRL_BIT_ENABLE_IRQ |
3433                              ME6000_AO_CTRL_BIT_ENABLE_EX_TRIG);
3434                        outl(ctrl, instance->ctrl_reg);
3435                        PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3436                                   instance->reg_base,
3437                                   instance->ctrl_reg - instance->reg_base,
3438                                   ctrl);
3439                        spin_unlock_irqrestore(&instance->subdevice_lock,
3440                                               cpu_flags);
3441
3442                        //Reset interrupt latch
3443                        inl(instance->irq_reset_reg);
3444
3445                        spin_lock(instance->preload_reg_lock);
3446                        //Remove from synchronous start. Block triggering from this output.
3447                        synch = inl(instance->preload_reg);
3448                        synch &=
3449                            ~((ME6000_AO_SYNC_HOLD | ME6000_AO_SYNC_EXT_TRIG) <<
3450                              instance->ao_idx);
3451                        outl(synch, instance->preload_reg);
3452                        PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3453                                   instance->reg_base,
3454                                   instance->preload_reg - instance->reg_base,
3455                                   synch);
3456                        spin_unlock(instance->preload_reg_lock);
3457
3458                        instance->status = ao_status_stream_end;
3459
3460                        // Signal the end.
3461                        signaling = 1;
3462                }
3463                // Wait for stop.
3464                reschedule = 1;
3465                break;
3466
3467        case ao_status_stream_run:
3468                if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3469                        PERROR_CRITICAL
3470                            ("Streaming on single device! This feature is not implemented in this version!\n");
3471                        instance->status = ao_status_stream_error;
3472                        // Signal the end.
3473                        signaling = 1;
3474                        break;
3475                }
3476
3477                if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. This is an error.
3478                        // BROKEN PIPE!
3479                        if (!(status & ME6000_AO_STATUS_BIT_EF)) {      // FIFO is empty.
3480                                if (me_circ_buf_values(&instance->circ_buf)) {  // Software buffer is not empty.
3481                                        if (instance->stop_data_count && (instance->stop_data_count <= instance->data_count)) { //Finishing work. Requed data shown.
3482                                                PDEBUG
3483                                                    ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3484                                                instance->status =
3485                                                    ao_status_stream_end;
3486                                        } else {
3487                                                PERROR
3488                                                    ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3489                                                instance->status =
3490                                                    ao_status_stream_buffer_error;
3491                                        }
3492                                } else {        // Software buffer is empty.
3493                                        PDEBUG
3494                                            ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3495                                        instance->status = ao_status_stream_end;
3496                                }
3497                        } else {        // There are still datas in FIFO.
3498                                if (me_circ_buf_values(&instance->circ_buf)) {  // Software buffer is not empty.
3499                                        PERROR
3500                                            ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3501                                } else {        // Software buffer is empty.
3502                                        PERROR
3503                                            ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3504                                }
3505                                instance->status = ao_status_stream_fifo_error;
3506
3507                        }
3508
3509                        // Signal the failure.
3510                        signaling = 1;
3511                        break;
3512                }
3513                // Wait for stop.
3514                reschedule = 1;
3515                break;
3516
3517        case ao_status_stream_end_wait:
3518                if (!(instance->fifo & ME6000_AO_HAS_FIFO)) {   // No FIFO
3519                        PERROR_CRITICAL
3520                            ("Streaming on single device! This feature is not implemented in this version!\n");
3521                        instance->status = ao_status_stream_error;
3522                        // Signal the end.
3523                        signaling = 1;
3524                        break;
3525                }
3526
3527                if (!(status & ME6000_AO_STATUS_BIT_FSM)) {     // State machine is not working. Waiting for stop finish.
3528                        instance->status = ao_status_stream_end;
3529                        signaling = 1;
3530                }
3531                // State machine is working.
3532                reschedule = 1;
3533                break;
3534
3535        default:
3536                PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3537                                instance->status);
3538                instance->status = ao_status_stream_error;
3539                // Signal the end.
3540                signaling = 1;
3541                break;
3542
3543        }
3544
3545        if (signaling) {        //Signal it.
3546                wake_up_interruptible_all(&instance->wait_queue);
3547        }
3548
3549        if (instance->ao_control_task_flag && reschedule) {     // Reschedule task
3550                queue_delayed_work(instance->me6000_workqueue,
3551                                   &instance->ao_control_task, 1);
3552        } else {
3553                PINFO("<%s> Ending control task.\n", __func__);
3554        }
3555
3556}
3557
3558static int me6000_ao_query_range_by_min_max(me_subdevice_t * subdevice,
3559                                            int unit,
3560                                            int *min,
3561                                            int *max, int *maxdata, int *range)
3562{
3563        me6000_ao_subdevice_t *instance;
3564
3565        instance = (me6000_ao_subdevice_t *) subdevice;
3566
3567        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3568
3569        if ((*max - *min) < 0) {
3570                PERROR("Invalid minimum and maximum values specified.\n");
3571                return ME_ERRNO_INVALID_MIN_MAX;
3572        }
3573
3574        if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3575                if ((*max <= (instance->max + 1000)) && (*min >= instance->min)) {
3576                        *min = instance->min;
3577                        *max = instance->max;
3578                        *maxdata = ME6000_AO_MAX_DATA;
3579                        *range = 0;
3580                } else {
3581                        PERROR("No matching range available.\n");
3582                        return ME_ERRNO_NO_RANGE;
3583                }
3584        } else {
3585                PERROR("Invalid physical unit specified.\n");
3586                return ME_ERRNO_INVALID_UNIT;
3587        }
3588
3589        return ME_ERRNO_SUCCESS;
3590}
3591
3592static int me6000_ao_query_number_ranges(me_subdevice_t * subdevice,
3593                                         int unit, int *count)
3594{
3595        me6000_ao_subdevice_t *instance;
3596
3597        instance = (me6000_ao_subdevice_t *) subdevice;
3598
3599        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3600
3601        if ((unit == ME_UNIT_VOLT) || (unit == ME_UNIT_ANY)) {
3602                *count = 1;
3603        } else {
3604                *count = 0;
3605        }
3606
3607        return ME_ERRNO_SUCCESS;
3608}
3609
3610static int me6000_ao_query_range_info(me_subdevice_t * subdevice,
3611                                      int range,
3612                                      int *unit,
3613                                      int *min, int *max, int *maxdata)
3614{
3615        me6000_ao_subdevice_t *instance;
3616
3617        instance = (me6000_ao_subdevice_t *) subdevice;
3618
3619        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3620
3621        if (range == 0) {
3622                *unit = ME_UNIT_VOLT;
3623                *min = instance->min;
3624                *max = instance->max;
3625                *maxdata = ME6000_AO_MAX_DATA;
3626        } else {
3627                PERROR("Invalid range number specified.\n");
3628                return ME_ERRNO_INVALID_RANGE;
3629        }
3630
3631        return ME_ERRNO_SUCCESS;
3632}
3633
3634static int me6000_ao_query_timer(me_subdevice_t * subdevice,
3635                                 int timer,
3636                                 int *base_frequency,
3637                                 long long *min_ticks, long long *max_ticks)
3638{
3639        me6000_ao_subdevice_t *instance;
3640
3641        instance = (me6000_ao_subdevice_t *) subdevice;
3642
3643        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3644
3645        if (instance->fifo) {   //Streaming device.
3646                *base_frequency = ME6000_AO_BASE_FREQUENCY;
3647                if (timer == ME_TIMER_ACQ_START) {
3648                        *min_ticks = ME6000_AO_MIN_ACQ_TICKS;
3649                        *max_ticks = ME6000_AO_MAX_ACQ_TICKS;
3650                } else if (timer == ME_TIMER_CONV_START) {
3651                        *min_ticks = ME6000_AO_MIN_CHAN_TICKS;
3652                        *max_ticks = ME6000_AO_MAX_CHAN_TICKS;
3653                }
3654        } else {                //Not streaming device!
3655                *base_frequency = 0;
3656                *min_ticks = 0;
3657                *max_ticks = 0;
3658        }
3659
3660        return ME_ERRNO_SUCCESS;
3661}
3662
3663static int me6000_ao_query_number_channels(me_subdevice_t * subdevice,
3664                                           int *number)
3665{
3666        me6000_ao_subdevice_t *instance;
3667        instance = (me6000_ao_subdevice_t *) subdevice;
3668
3669        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3670
3671        *number = 1;
3672        return ME_ERRNO_SUCCESS;
3673}
3674
3675static int me6000_ao_query_subdevice_type(me_subdevice_t * subdevice,
3676                                          int *type, int *subtype)
3677{
3678        me6000_ao_subdevice_t *instance;
3679
3680        instance = (me6000_ao_subdevice_t *) subdevice;
3681
3682        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3683
3684        *type = ME_TYPE_AO;
3685        *subtype =
3686            (instance->
3687             fifo & ME6000_AO_HAS_FIFO) ? ME_SUBTYPE_STREAMING :
3688            ME_SUBTYPE_SINGLE;
3689
3690        return ME_ERRNO_SUCCESS;
3691}
3692
3693static int me6000_ao_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
3694{
3695        me6000_ao_subdevice_t *instance;
3696        instance = (me6000_ao_subdevice_t *) subdevice;
3697
3698        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3699
3700        *caps =
3701            ME_CAPS_AO_TRIG_SYNCHRONOUS | ((instance->fifo) ? ME_CAPS_AO_FIFO :
3702                                           ME_CAPS_NONE);
3703
3704        return ME_ERRNO_SUCCESS;
3705}
3706
3707static int me6000_ao_query_subdevice_caps_args(struct me_subdevice *subdevice,
3708                                               int cap, int *args, int count)
3709{
3710        me6000_ao_subdevice_t *instance;
3711        int err = ME_ERRNO_SUCCESS;
3712
3713        instance = (me6000_ao_subdevice_t *) subdevice;
3714
3715        PDEBUG("executed. idx=%d\n", instance->ao_idx);
3716
3717        if (count != 1) {
3718                PERROR("Invalid capability argument count.\n");
3719                return ME_ERRNO_INVALID_CAP_ARG_COUNT;
3720        }
3721
3722        switch (cap) {
3723        case ME_CAP_AI_FIFO_SIZE:
3724                args[0] = (instance->fifo) ? ME6000_AO_FIFO_COUNT : 0;
3725                break;
3726
3727        case ME_CAP_AI_BUFFER_SIZE:
3728                args[0] =
3729                    (instance->circ_buf.buf) ? ME6000_AO_CIRC_BUF_COUNT : 0;
3730                break;
3731
3732        default:
3733                PERROR("Invalid capability.\n");
3734                err = ME_ERRNO_INVALID_CAP;
3735                args[0] = 0;
3736        }
3737
3738        return err;
3739}
3740
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.