linux/drivers/staging/meilhaus/me1600_ao.c
<<
>>
Prefs
   1/**
   2 * @file me1600_ao.c
   3 *
   4 * @brief ME-1600 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
  35#include <linux/module.h>
  36
  37#include <linux/slab.h>
  38#include <linux/spinlock.h>
  39#include <asm/io.h>
  40#include <linux/types.h>
  41#include <linux/sched.h>
  42
  43#include <linux/workqueue.h>
  44
  45#include "medefines.h"
  46#include "meinternal.h"
  47#include "meerror.h"
  48#include "medebug.h"
  49
  50#include "me1600_ao_reg.h"
  51#include "me1600_ao.h"
  52
  53/* Defines
  54 */
  55
  56static void me1600_ao_destructor(struct me_subdevice *subdevice);
  57
  58#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  59static void me1600_ao_work_control_task(void *subdevice);
  60#else
  61static void me1600_ao_work_control_task(struct work_struct *work);
  62#endif
  63
  64static int me1600_ao_io_reset_subdevice(me_subdevice_t * subdevice,
  65                                        struct file *filep, int flags);
  66static int me1600_ao_io_single_config(me_subdevice_t * subdevice,
  67                                      struct file *filep, int channel,
  68                                      int single_config, int ref, int trig_chan,
  69                                      int trig_type, int trig_edge, int flags);
  70static int me1600_ao_io_single_read(me_subdevice_t * subdevice,
  71                                    struct file *filep, int channel, int *value,
  72                                    int time_out, int flags);
  73static int me1600_ao_io_single_write(me_subdevice_t * subdevice,
  74                                     struct file *filep, int channel, int value,
  75                                     int time_out, int flags);
  76static int me1600_ao_query_number_channels(me_subdevice_t * subdevice,
  77                                           int *number);
  78static int me1600_ao_query_subdevice_type(me_subdevice_t * subdevice, int *type,
  79                                          int *subtype);
  80static int me1600_ao_query_subdevice_caps(me_subdevice_t * subdevice,
  81                                          int *caps);
  82static int me1600_ao_query_range_by_min_max(me_subdevice_t * subdevice,
  83                                            int unit, int *min, int *max,
  84                                            int *maxdata, int *range);
  85static int me1600_ao_query_number_ranges(me_subdevice_t * subdevice, int unit,
  86                                         int *count);
  87static int me1600_ao_query_range_info(me_subdevice_t * subdevice, int range,
  88                                      int *unit, int *min, int *max,
  89                                      int *maxdata);
  90
  91/* Functions
  92 */
  93
  94me1600_ao_subdevice_t *me1600_ao_constructor(uint32_t reg_base,
  95                                             unsigned int ao_idx,
  96                                             int curr,
  97                                             spinlock_t * config_regs_lock,
  98                                             spinlock_t * ao_shadows_lock,
  99                                             me1600_ao_shadow_t *
 100                                             ao_regs_shadows,
 101                                             struct workqueue_struct *me1600_wq)
 102{
 103        me1600_ao_subdevice_t *subdevice;
 104        int err;
 105
 106        PDEBUG("executed. idx=%d\n", ao_idx);
 107
 108        // Allocate memory for subdevice instance.
 109        subdevice = kmalloc(sizeof(me1600_ao_subdevice_t), GFP_KERNEL);
 110
 111        if (!subdevice) {
 112                PERROR
 113                    ("Cannot get memory for analog output subdevice instance.\n");
 114                return NULL;
 115        }
 116
 117        memset(subdevice, 0, sizeof(me1600_ao_subdevice_t));
 118
 119        // Initialize subdevice base class.
 120        err = me_subdevice_init(&subdevice->base);
 121
 122        if (err) {
 123                PERROR("Cannot initialize subdevice base class instance.\n");
 124                kfree(subdevice);
 125                return NULL;
 126        }
 127        // Initialize spin locks.
 128        spin_lock_init(&subdevice->subdevice_lock);
 129        subdevice->config_regs_lock = config_regs_lock;
 130        subdevice->ao_shadows_lock = ao_shadows_lock;
 131
 132        // Save the subdevice index.
 133        subdevice->ao_idx = ao_idx;
 134
 135        // Initialize range lists.
 136        subdevice->u_ranges_count = 2;
 137
 138        subdevice->u_ranges[0].min = 0; //0V
 139        subdevice->u_ranges[0].max = 9997558;   //10V
 140
 141        subdevice->u_ranges[1].min = -10E6;     //-10V
 142        subdevice->u_ranges[1].max = 9995117;   //10V
 143
 144        if (curr) {             // This is version with current outputs.
 145                subdevice->i_ranges_count = 2;
 146
 147                subdevice->i_ranges[0].min = 0; //0mA
 148                subdevice->i_ranges[0].max = 19995117;  //20mA
 149
 150                subdevice->i_ranges[1].min = 4E3;       //4mA
 151                subdevice->i_ranges[1].max = 19995118;  //20mA
 152        } else {                // This is version without current outputs.
 153                subdevice->i_ranges_count = 0;
 154
 155                subdevice->i_ranges[0].min = 0; //0mA
 156                subdevice->i_ranges[0].max = 0; //0mA
 157
 158                subdevice->i_ranges[1].min = 0; //0mA
 159                subdevice->i_ranges[1].max = 0; //0mA
 160        }
 161
 162        // Initialize registers.
 163        subdevice->uni_bi_reg = reg_base + ME1600_UNI_BI_REG;
 164        subdevice->i_range_reg = reg_base + ME1600_020_420_REG;
 165        subdevice->sim_output_reg = reg_base + ME1600_SIM_OUTPUT_REG;
 166        subdevice->current_on_reg = reg_base + ME1600_CURRENT_ON_REG;
 167#ifdef MEDEBUG_DEBUG_REG
 168        subdevice->reg_base = reg_base;
 169#endif
 170
 171        // Initialize shadow structure.
 172        subdevice->ao_regs_shadows = ao_regs_shadows;
 173
 174        // Override base class methods.
 175        subdevice->base.me_subdevice_destructor = me1600_ao_destructor;
 176        subdevice->base.me_subdevice_io_reset_subdevice =
 177            me1600_ao_io_reset_subdevice;
 178        subdevice->base.me_subdevice_io_single_config =
 179            me1600_ao_io_single_config;
 180        subdevice->base.me_subdevice_io_single_read = me1600_ao_io_single_read;
 181        subdevice->base.me_subdevice_io_single_write =
 182            me1600_ao_io_single_write;
 183        subdevice->base.me_subdevice_query_number_channels =
 184            me1600_ao_query_number_channels;
 185        subdevice->base.me_subdevice_query_subdevice_type =
 186            me1600_ao_query_subdevice_type;
 187        subdevice->base.me_subdevice_query_subdevice_caps =
 188            me1600_ao_query_subdevice_caps;
 189        subdevice->base.me_subdevice_query_range_by_min_max =
 190            me1600_ao_query_range_by_min_max;
 191        subdevice->base.me_subdevice_query_number_ranges =
 192            me1600_ao_query_number_ranges;
 193        subdevice->base.me_subdevice_query_range_info =
 194            me1600_ao_query_range_info;
 195
 196        // Initialize wait queue.
 197        init_waitqueue_head(&subdevice->wait_queue);
 198
 199        // Prepare work queue.
 200        subdevice->me1600_workqueue = me1600_wq;
 201
 202/* workqueue API changed in kernel 2.6.20 */
 203#if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) )
 204        INIT_WORK(&subdevice->ao_control_task, me1600_ao_work_control_task,
 205                  (void *)subdevice);
 206#else
 207        INIT_DELAYED_WORK(&subdevice->ao_control_task,
 208                          me1600_ao_work_control_task);
 209#endif
 210        return subdevice;
 211}
 212
 213static void me1600_ao_destructor(struct me_subdevice *subdevice)
 214{
 215        me1600_ao_subdevice_t *instance;
 216
 217        instance = (me1600_ao_subdevice_t *) subdevice;
 218
 219        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 220
 221        instance->ao_control_task_flag = 0;
 222
 223        // Reset subdevice to asure clean exit.
 224        me1600_ao_io_reset_subdevice(subdevice, NULL,
 225                                     ME_IO_RESET_SUBDEVICE_NO_FLAGS);
 226
 227        // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
 228        if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
 229                set_current_state(TASK_INTERRUPTIBLE);
 230                schedule_timeout(2);
 231        }
 232}
 233
 234static int me1600_ao_io_reset_subdevice(me_subdevice_t * subdevice,
 235                                        struct file *filep, int flags)
 236{
 237        me1600_ao_subdevice_t *instance;
 238        uint16_t tmp;
 239
 240        instance = (me1600_ao_subdevice_t *) subdevice;
 241
 242        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 243
 244        if (flags) {
 245                PERROR("Invalid flag specified.\n");
 246                return ME_ERRNO_INVALID_FLAGS;
 247        }
 248
 249        ME_SUBDEVICE_ENTER;
 250
 251        //Cancel control task
 252        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
 253        instance->ao_control_task_flag = 0;
 254        cancel_delayed_work(&instance->ao_control_task);
 255        (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
 256
 257        // Reset all settings.
 258        spin_lock(&instance->subdevice_lock);
 259        spin_lock(instance->ao_shadows_lock);
 260        (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
 261        (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
 262        (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Not waiting for triggering.
 263        (instance->ao_regs_shadows)->synchronous &= ~(0x1 << instance->ao_idx); //Individual triggering.
 264
 265        // Set output to default (safe) state.
 266        spin_lock(instance->config_regs_lock);
 267        tmp = inw(instance->uni_bi_reg);        // unipolar
 268        tmp |= (0x1 << instance->ao_idx);
 269        outw(tmp, instance->uni_bi_reg);
 270        PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 271                   instance->uni_bi_reg - instance->reg_base, tmp);
 272
 273        tmp = inw(instance->current_on_reg);    // Volts only!
 274        tmp &= ~(0x1 << instance->ao_idx);
 275        tmp &= 0x00FF;
 276        outw(tmp, instance->current_on_reg);
 277        PDEBUG_REG("current_on_reg outl(0x%lX+0x%lX)=0x%x\n",
 278                   instance->reg_base,
 279                   instance->current_on_reg - instance->reg_base, tmp);
 280
 281        tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
 282        tmp &= ~(0x1 << instance->ao_idx);
 283        outw(tmp, instance->i_range_reg);
 284        PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 285                   instance->i_range_reg - instance->reg_base, tmp);
 286
 287        outw(0, (instance->ao_regs_shadows)->registry[instance->ao_idx]);
 288        PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
 289                   (instance->ao_regs_shadows)->registry[instance->ao_idx] -
 290                   instance->reg_base, 0);
 291
 292        // Trigger output.
 293        outw(0x0000, instance->sim_output_reg);
 294        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 295                   instance->reg_base,
 296                   instance->sim_output_reg - instance->reg_base, 0x0000);
 297        outw(0xFFFF, instance->sim_output_reg);
 298        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 299                   instance->reg_base,
 300                   instance->sim_output_reg - instance->reg_base, 0xFFFF);
 301        spin_unlock(instance->config_regs_lock);
 302        spin_unlock(instance->ao_shadows_lock);
 303
 304        // Set status to 'none'
 305        instance->status = ao_status_none;
 306        spin_unlock(&instance->subdevice_lock);
 307
 308        //Signal reset if user is on wait.
 309        wake_up_interruptible_all(&instance->wait_queue);
 310
 311        ME_SUBDEVICE_EXIT;
 312
 313        return ME_ERRNO_SUCCESS;
 314}
 315
 316static int me1600_ao_io_single_config(me_subdevice_t * subdevice,
 317                                      struct file *filep,
 318                                      int channel,
 319                                      int single_config,
 320                                      int ref,
 321                                      int trig_chan,
 322                                      int trig_type, int trig_edge, int flags)
 323{
 324        me1600_ao_subdevice_t *instance;
 325        uint16_t tmp;
 326
 327        instance = (me1600_ao_subdevice_t *) subdevice;
 328
 329        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 330
 331        // Checking parameters.
 332        if (flags) {
 333                PERROR
 334                    ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
 335                return ME_ERRNO_INVALID_FLAGS;
 336        }
 337
 338        if (trig_edge != ME_TRIG_EDGE_NONE) {
 339                PERROR
 340                    ("Invalid trigger edge. Software trigger has not edge. Must be ME_TRIG_EDGE_NONE\n");
 341                return ME_ERRNO_INVALID_TRIG_EDGE;
 342        }
 343
 344        if (trig_type != ME_TRIG_TYPE_SW) {
 345                PERROR("Invalid trigger edge. Must be ME_TRIG_TYPE_SW.\n");
 346                return ME_ERRNO_INVALID_TRIG_TYPE;
 347        }
 348
 349        if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
 350            && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
 351                PERROR("Invalid trigger channel specified.\n");
 352                return ME_ERRNO_INVALID_TRIG_CHAN;
 353        }
 354
 355        if (ref != ME_REF_AO_GROUND) {
 356                PERROR
 357                    ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
 358                return ME_ERRNO_INVALID_REF;
 359        }
 360
 361        if (((single_config + 1) >
 362             (instance->u_ranges_count + instance->i_ranges_count))
 363            || (single_config < 0)) {
 364                PERROR("Invalid range specified.\n");
 365                return ME_ERRNO_INVALID_SINGLE_CONFIG;
 366        }
 367
 368        if (channel) {
 369                PERROR("Invalid channel specified.\n");
 370                return ME_ERRNO_INVALID_CHANNEL;
 371        }
 372        // Checking parameters - done. All is fine. Do config.
 373
 374        ME_SUBDEVICE_ENTER;
 375
 376        //Cancel control task
 377        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
 378        instance->ao_control_task_flag = 0;
 379        cancel_delayed_work(&instance->ao_control_task);
 380
 381        spin_lock(&instance->subdevice_lock);
 382        spin_lock(instance->ao_shadows_lock);
 383        (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
 384        (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
 385        (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
 386
 387        spin_lock(instance->config_regs_lock);
 388        switch (single_config) {
 389        case 0:         // 0V 10V
 390                tmp = inw(instance->current_on_reg);    // Volts
 391                tmp &= ~(0x1 << instance->ao_idx);
 392                outw(tmp, instance->current_on_reg);
 393                PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
 394                           instance->reg_base,
 395                           instance->current_on_reg - instance->reg_base, tmp);
 396
 397                // 0V
 398                outw(0,
 399                     (instance->ao_regs_shadows)->registry[instance->ao_idx]);
 400                PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 401                           instance->reg_base,
 402                           (instance->ao_regs_shadows)->registry[instance->
 403                                                                 ao_idx] -
 404                           instance->reg_base, 0);
 405
 406                tmp = inw(instance->uni_bi_reg);        // unipolar
 407                tmp |= (0x1 << instance->ao_idx);
 408                outw(tmp, instance->uni_bi_reg);
 409                PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
 410                           instance->reg_base,
 411                           instance->uni_bi_reg - instance->reg_base, tmp);
 412
 413                tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
 414                tmp &= ~(0x1 << instance->ao_idx);
 415                outw(tmp, instance->i_range_reg);
 416                PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
 417                           instance->reg_base,
 418                           instance->i_range_reg - instance->reg_base, tmp);
 419                break;
 420
 421        case 1:         // -10V 10V
 422                tmp = inw(instance->current_on_reg);    // Volts
 423                tmp &= ~(0x1 << instance->ao_idx);
 424                outw(tmp, instance->current_on_reg);
 425                PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
 426                           instance->reg_base,
 427                           instance->current_on_reg - instance->reg_base, tmp);
 428
 429                // 0V
 430                outw(0x0800,
 431                     (instance->ao_regs_shadows)->registry[instance->ao_idx]);
 432                PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 433                           instance->reg_base,
 434                           (instance->ao_regs_shadows)->registry[instance->
 435                                                                 ao_idx] -
 436                           instance->reg_base, 0x0800);
 437
 438                tmp = inw(instance->uni_bi_reg);        // bipolar
 439                tmp &= ~(0x1 << instance->ao_idx);
 440                outw(tmp, instance->uni_bi_reg);
 441                PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
 442                           instance->reg_base,
 443                           instance->uni_bi_reg - instance->reg_base, tmp);
 444
 445                tmp = inw(instance->i_range_reg);       // 0..20mA <= If exists.
 446                tmp &= ~(0x1 << instance->ao_idx);
 447                outw(tmp, instance->i_range_reg);
 448                PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
 449                           instance->reg_base,
 450                           instance->i_range_reg - instance->reg_base, tmp);
 451                break;
 452
 453        case 2:         // 0mA 20mA
 454                tmp = inw(instance->current_on_reg);    // mAmpers
 455                tmp |= (0x1 << instance->ao_idx);
 456                outw(tmp, instance->current_on_reg);
 457                PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
 458                           instance->reg_base,
 459                           instance->current_on_reg - instance->reg_base, tmp);
 460
 461                tmp = inw(instance->i_range_reg);       // 0..20mA
 462                tmp &= ~(0x1 << instance->ao_idx);
 463                outw(tmp, instance->i_range_reg);
 464                PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
 465                           instance->reg_base,
 466                           instance->i_range_reg - instance->reg_base, tmp);
 467
 468                // 0mA
 469                outw(0,
 470                     (instance->ao_regs_shadows)->registry[instance->ao_idx]);
 471                PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 472                           instance->reg_base,
 473                           (instance->ao_regs_shadows)->registry[instance->
 474                                                                 ao_idx] -
 475                           instance->reg_base, 0);
 476
 477                tmp = inw(instance->uni_bi_reg);        // unipolar
 478                tmp |= (0x1 << instance->ao_idx);
 479                outw(tmp, instance->uni_bi_reg);
 480                PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
 481                           instance->reg_base,
 482                           instance->uni_bi_reg - instance->reg_base, tmp);
 483                break;
 484
 485        case 3:         // 4mA 20mA
 486                tmp = inw(instance->current_on_reg);    // mAmpers
 487                tmp |= (0x1 << instance->ao_idx);
 488                outw(tmp, instance->current_on_reg);
 489                PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
 490                           instance->reg_base,
 491                           instance->current_on_reg - instance->reg_base, tmp);
 492
 493                tmp = inw(instance->i_range_reg);       // 4..20mA
 494                tmp |= (0x1 << instance->ao_idx);
 495                outw(tmp, instance->i_range_reg);
 496                PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
 497                           instance->reg_base,
 498                           instance->i_range_reg - instance->reg_base, tmp);
 499
 500                // 4mA
 501                outw(0,
 502                     (instance->ao_regs_shadows)->registry[instance->ao_idx]);
 503                PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 504                           instance->reg_base,
 505                           (instance->ao_regs_shadows)->registry[instance->
 506                                                                 ao_idx] -
 507                           instance->reg_base, 0);
 508
 509                tmp = inw(instance->uni_bi_reg);        // unipolar
 510                tmp |= (0x1 << instance->ao_idx);
 511                outw(tmp, instance->uni_bi_reg);
 512                PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
 513                           instance->reg_base,
 514                           instance->uni_bi_reg - instance->reg_base, tmp);
 515                break;
 516        }
 517
 518        // Trigger output.
 519        outw(0x0000, instance->sim_output_reg);
 520        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 521                   instance->reg_base,
 522                   instance->sim_output_reg - instance->reg_base, 0x0000);
 523        outw(0xFFFF, instance->sim_output_reg);
 524        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 525                   instance->reg_base,
 526                   instance->sim_output_reg - instance->reg_base, 0xFFFF);
 527
 528        if (trig_chan == ME_TRIG_CHAN_DEFAULT) {        // Individual triggering.
 529                (instance->ao_regs_shadows)->synchronous &=
 530                    ~(0x1 << instance->ao_idx);
 531                PDEBUG("Individual triggering.\n");
 532        } else if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS) {     // Synchronous triggering.
 533                (instance->ao_regs_shadows)->synchronous |=
 534                    (0x1 << instance->ao_idx);
 535                PDEBUG("Synchronous triggering.\n");
 536        }
 537        spin_unlock(instance->config_regs_lock);
 538        spin_unlock(instance->ao_shadows_lock);
 539
 540        instance->status = ao_status_single_configured;
 541        spin_unlock(&instance->subdevice_lock);
 542
 543        ME_SUBDEVICE_EXIT;
 544
 545        return ME_ERRNO_SUCCESS;
 546}
 547
 548static int me1600_ao_io_single_read(me_subdevice_t * subdevice,
 549                                    struct file *filep,
 550                                    int channel,
 551                                    int *value, int time_out, int flags)
 552{
 553        me1600_ao_subdevice_t *instance;
 554        unsigned long delay = 0;
 555        unsigned long j = 0;
 556        int err = ME_ERRNO_SUCCESS;
 557
 558        instance = (me1600_ao_subdevice_t *) subdevice;
 559
 560        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 561
 562        if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
 563                PERROR("Invalid flag specified. %d\n", flags);
 564                return ME_ERRNO_INVALID_FLAGS;
 565        }
 566
 567        if (time_out < 0) {
 568                PERROR("Invalid timeout specified.\n");
 569                return ME_ERRNO_INVALID_TIMEOUT;
 570        }
 571
 572        if (channel) {
 573                PERROR("Invalid channel specified.\n");
 574                return ME_ERRNO_INVALID_CHANNEL;
 575        }
 576
 577        if ((!flags) && ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {    //Blocking mode. Wait for software trigger.
 578                if (time_out) {
 579                        delay = (time_out * HZ) / 1000;
 580                        if (delay == 0)
 581                                delay = 1;
 582                }
 583
 584                j = jiffies;
 585
 586                //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
 587                wait_event_interruptible_timeout(instance->wait_queue,
 588                                                 (!((instance->
 589                                                     ao_regs_shadows)->
 590                                                    trigger & instance->
 591                                                    ao_idx)),
 592                                                 (delay) ? delay : LONG_MAX);
 593
 594                if (instance == ao_status_none) {       // Reset was called.
 595                        PDEBUG("Single canceled.\n");
 596                        err = ME_ERRNO_CANCELLED;
 597                }
 598
 599                if (signal_pending(current)) {
 600                        PERROR("Wait on start of state machine interrupted.\n");
 601                        err = ME_ERRNO_SIGNAL;
 602                }
 603
 604                if ((delay) && ((jiffies - j) >= delay)) {
 605                        PDEBUG("Timeout reached.\n");
 606                        err = ME_ERRNO_TIMEOUT;
 607                }
 608        }
 609
 610        *value = (instance->ao_regs_shadows)->mirror[instance->ao_idx];
 611
 612        return err;
 613}
 614
 615static int me1600_ao_io_single_write(me_subdevice_t * subdevice,
 616                                     struct file *filep,
 617                                     int channel,
 618                                     int value, int time_out, int flags)
 619{
 620        me1600_ao_subdevice_t *instance;
 621        int err = ME_ERRNO_SUCCESS;
 622        unsigned long delay = 0;
 623        int i;
 624        unsigned long j = 0;
 625
 626        instance = (me1600_ao_subdevice_t *) subdevice;
 627
 628        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 629
 630        if (flags &
 631            ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
 632              ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
 633                PERROR("Invalid flag specified.\n");
 634                return ME_ERRNO_INVALID_FLAGS;
 635        }
 636
 637        if (time_out < 0) {
 638                PERROR("Invalid timeout specified.\n");
 639                return ME_ERRNO_INVALID_TIMEOUT;
 640        }
 641
 642        if (value & ~ME1600_AO_MAX_DATA) {
 643                PERROR("Invalid value provided.\n");
 644                return ME_ERRNO_VALUE_OUT_OF_RANGE;
 645        }
 646
 647        if (channel) {
 648                PERROR("Invalid channel specified.\n");
 649                return ME_ERRNO_INVALID_CHANNEL;
 650        }
 651
 652        ME_SUBDEVICE_ENTER;
 653
 654        //Cancel control task
 655        PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
 656        instance->ao_control_task_flag = 0;
 657        cancel_delayed_work(&instance->ao_control_task);
 658        (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx);     //Cancell waiting for trigger.
 659
 660        if (time_out) {
 661                delay = (time_out * HZ) / 1000;
 662
 663                if (delay == 0)
 664                        delay = 1;
 665        }
 666        //Write value.
 667        spin_lock(instance->ao_shadows_lock);
 668        (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
 669            (uint16_t) value;
 670
 671        if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) {       // Trigger all outputs from synchronous list.
 672                for (i = 0; i < (instance->ao_regs_shadows)->count; i++) {
 673                        if (((instance->ao_regs_shadows)->synchronous & (0x1 << i)) || (i == instance->ao_idx)) {       // Set all from synchronous list to correct state.
 674                                PDEBUG
 675                                    ("Synchronous triggering: output %d. idx=%d\n",
 676                                     i, instance->ao_idx);
 677                                (instance->ao_regs_shadows)->mirror[i] =
 678                                    (instance->ao_regs_shadows)->shadow[i];
 679
 680                                outw((instance->ao_regs_shadows)->shadow[i],
 681                                     (instance->ao_regs_shadows)->registry[i]);
 682                                PDEBUG_REG
 683                                    ("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 684                                     instance->reg_base,
 685                                     (instance->ao_regs_shadows)->registry[i] -
 686                                     instance->reg_base,
 687                                     (instance->ao_regs_shadows)->shadow[i]);
 688
 689                                (instance->ao_regs_shadows)->trigger &=
 690                                    ~(0x1 << i);
 691                        }
 692                }
 693
 694                // Trigger output.
 695                outw(0x0000, instance->sim_output_reg);
 696                PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 697                           instance->reg_base,
 698                           instance->sim_output_reg - instance->reg_base, 0);
 699                outw(0xFFFF, instance->sim_output_reg);
 700                PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 701                           instance->reg_base,
 702                           instance->sim_output_reg - instance->reg_base,
 703                           0xFFFF);
 704                instance->status = ao_status_single_end;
 705        } else {                // Individual mode.
 706                if ((instance->ao_regs_shadows)->synchronous & (0x1 << instance->ao_idx)) {     // Put on synchronous start list. Set output as waiting for trigger.
 707                        PDEBUG("Add to synchronous list. idx=%d\n",
 708                               instance->ao_idx);
 709                        (instance->ao_regs_shadows)->trigger |=
 710                            (0x1 << instance->ao_idx);
 711                        instance->status = ao_status_single_run;
 712                        PDEBUG("Synchronous list: 0x%x.\n",
 713                               (instance->ao_regs_shadows)->synchronous);
 714                } else {        // Fired this one.
 715                        PDEBUG("Triggering. idx=%d\n", instance->ao_idx);
 716                        (instance->ao_regs_shadows)->mirror[instance->ao_idx] =
 717                            (instance->ao_regs_shadows)->shadow[instance->
 718                                                                ao_idx];
 719
 720                        outw((instance->ao_regs_shadows)->
 721                             shadow[instance->ao_idx],
 722                             (instance->ao_regs_shadows)->registry[instance->
 723                                                                   ao_idx]);
 724                        PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
 725                                   instance->reg_base,
 726                                   (instance->ao_regs_shadows)->
 727                                   registry[instance->ao_idx] -
 728                                   instance->reg_base,
 729                                   (instance->ao_regs_shadows)->
 730                                   shadow[instance->ao_idx]);
 731
 732                        // Set output as triggered.
 733                        (instance->ao_regs_shadows)->trigger &=
 734                            ~(0x1 << instance->ao_idx);
 735
 736                        // Trigger output.
 737                        outw(0x0000, instance->sim_output_reg);
 738                        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 739                                   instance->reg_base,
 740                                   instance->sim_output_reg -
 741                                   instance->reg_base, 0);
 742                        outw(0xFFFF, instance->sim_output_reg);
 743                        PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
 744                                   instance->reg_base,
 745                                   instance->sim_output_reg -
 746                                   instance->reg_base, 0xFFFF);
 747                        instance->status = ao_status_single_end;
 748                }
 749        }
 750        spin_unlock(instance->ao_shadows_lock);
 751
 752        //Init control task
 753        instance->timeout.delay = delay;
 754        instance->timeout.start_time = jiffies;
 755        instance->ao_control_task_flag = 1;
 756        queue_delayed_work(instance->me1600_workqueue,
 757                           &instance->ao_control_task, 1);
 758
 759        if ((!flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING) && ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {      //Blocking mode. Wait for software trigger.
 760                if (time_out) {
 761                        delay = (time_out * HZ) / 1000;
 762                        if (delay == 0)
 763                                delay = 1;
 764                }
 765
 766                j = jiffies;
 767
 768                //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
 769                wait_event_interruptible_timeout(instance->wait_queue,
 770                                                 (!((instance->
 771                                                     ao_regs_shadows)->
 772                                                    trigger & instance->
 773                                                    ao_idx)),
 774                                                 (delay) ? delay : LONG_MAX);
 775
 776                if (instance == ao_status_none) {
 777                        PDEBUG("Single canceled.\n");
 778                        err = ME_ERRNO_CANCELLED;
 779                }
 780                if (signal_pending(current)) {
 781                        PERROR("Wait on start of state machine interrupted.\n");
 782                        err = ME_ERRNO_SIGNAL;
 783                }
 784
 785                if ((delay) && ((jiffies - j) >= delay)) {
 786                        PDEBUG("Timeout reached.\n");
 787                        err = ME_ERRNO_TIMEOUT;
 788                }
 789        }
 790
 791        ME_SUBDEVICE_EXIT;
 792
 793        return err;
 794}
 795
 796static int me1600_ao_query_number_channels(me_subdevice_t * subdevice,
 797                                           int *number)
 798{
 799        me1600_ao_subdevice_t *instance;
 800        instance = (me1600_ao_subdevice_t *) subdevice;
 801
 802        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 803
 804        *number = 1;            //Every subdevice has only 1 channel.
 805        return ME_ERRNO_SUCCESS;
 806}
 807
 808static int me1600_ao_query_subdevice_type(me_subdevice_t * subdevice, int *type,
 809                                          int *subtype)
 810{
 811        me1600_ao_subdevice_t *instance;
 812        instance = (me1600_ao_subdevice_t *) subdevice;
 813
 814        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 815
 816        *type = ME_TYPE_AO;
 817        *subtype = ME_SUBTYPE_SINGLE;
 818        return ME_ERRNO_SUCCESS;
 819}
 820
 821static int me1600_ao_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
 822{
 823        PDEBUG("executed.\n");
 824        *caps = ME_CAPS_AO_TRIG_SYNCHRONOUS;
 825        return ME_ERRNO_SUCCESS;
 826}
 827
 828static int me1600_ao_query_range_by_min_max(me_subdevice_t * subdevice,
 829                                            int unit,
 830                                            int *min,
 831                                            int *max, int *maxdata, int *range)
 832{
 833        me1600_ao_subdevice_t *instance;
 834        int i;
 835        int r = -1;
 836        int diff = 21E6;
 837
 838        instance = (me1600_ao_subdevice_t *) subdevice;
 839
 840        PDEBUG("executed. idx=%d\n", instance->ao_idx);
 841
 842        if ((*max - *min) < 0) {
 843                PERROR("Invalid minimum and maximum values specified.\n");
 844                return ME_ERRNO_INVALID_MIN_MAX;
 845        }
 846        // Maximum ranges are slightly less then 10V or 20mA. For convenient we accepted this value as valid one.
 847        if (unit == ME_UNIT_VOLT) {
 848                for (i = 0; i < instance->u_ranges_count; i++) {
 849                        if ((instance->u_ranges[i].min <= *min)
 850                            && ((instance->u_ranges[i].max + 5000) >= *max)) {
 851                                if ((instance->u_ranges[i].max -
 852                                     instance->u_ranges[i].min) - (*max -
 853                                                                   *min) <
 854                                    diff) {
 855                                        r = i;
 856                                        diff =
 857                                            (instance->u_ranges[i].max -
 858                                             instance->u_ranges[i].min) -
 859                                            (*max - *min);
 860                                }
 861                        }
 862                }
 863
 864                if (r < 0) {
 865                        PERROR("No matching range found.\n");
 866                        return ME_ERRNO_NO_RANGE;
 867                } else {
 868                        *min = instance->u_ranges[r].min;
 869                        *max = instance->u_ranges[r].max;
 870                        *range = r;
 871                }
 872        } else if (unit == ME_UNIT_AMPERE) {
 873                for (i = 0; i < instance->i_ranges_count; i++) {
 874                        if ((instance->i_ranges[i].min <= *min)
 875                            && (instance->i_ranges[i].max + 5000 >= *max)) {
 876                                if ((instance->i_ranges[i].max -
 877                                     instance->i_ranges[i].min) - (*max -
 878                                                                   *min) <
 879                                    diff) {
 880                                        r = i;
 881                                        diff =
 882                                            (instance->i_ranges[i].max -
 883                                             instance->i_ranges[i].min) -
 884                                            (*max - *min);
 885                                }
 886                        }
 887                }
 888
 889                if (r < 0) {
 890                        PERROR("No matching range found.\n");
 891                        return ME_ERRNO_NO_RANGE;
 892                } else {
 893                        *min = instance->i_ranges[r].min;
 894                        *max = instance->i_ranges[r].max;
 895                        *range = r + instance->u_ranges_count;
 896                }
 897        } else {
 898                PERROR("Invalid physical unit specified.\n");
 899                return ME_ERRNO_INVALID_UNIT;
 900        }
 901        *maxdata = ME1600_AO_MAX_DATA;
 902
 903        return ME_ERRNO_SUCCESS;
 904}
 905
 906static int me1600_ao_query_number_ranges(me_subdevice_t * subdevice,
 907                                         int unit, int *count)
 908{
 909        me1600_ao_subdevice_t *instance;
 910
 911        PDEBUG("executed.\n");
 912
 913        instance = (me1600_ao_subdevice_t *) subdevice;
 914        switch (unit) {
 915        case ME_UNIT_VOLT:
 916                *count = instance->u_ranges_count;
 917                break;
 918        case ME_UNIT_AMPERE:
 919                *count = instance->i_ranges_count;
 920                break;
 921        case ME_UNIT_ANY:
 922                *count = instance->u_ranges_count + instance->i_ranges_count;
 923                break;
 924        default:
 925                *count = 0;
 926        }
 927
 928        return ME_ERRNO_SUCCESS;
 929}
 930
 931static int me1600_ao_query_range_info(me_subdevice_t * subdevice,
 932                                      int range,
 933                                      int *unit,
 934                                      int *min, int *max, int *maxdata)
 935{
 936        me1600_ao_subdevice_t *instance;
 937
 938        PDEBUG("executed.\n");
 939
 940        instance = (me1600_ao_subdevice_t *) subdevice;
 941
 942        if (((range + 1) >
 943             (instance->u_ranges_count + instance->i_ranges_count))
 944            || (range < 0)) {
 945                PERROR("Invalid range number specified.\n");
 946                return ME_ERRNO_INVALID_RANGE;
 947        }
 948
 949        if (range < instance->u_ranges_count) {
 950                *unit = ME_UNIT_VOLT;
 951                *min = instance->u_ranges[range].min;
 952                *max = instance->u_ranges[range].max;
 953        } else if (range < instance->u_ranges_count + instance->i_ranges_count) {
 954                *unit = ME_UNIT_AMPERE;
 955                *min = instance->i_ranges[range - instance->u_ranges_count].min;
 956                *max = instance->i_ranges[range - instance->u_ranges_count].max;
 957        }
 958        *maxdata = ME1600_AO_MAX_DATA;
 959
 960        return ME_ERRNO_SUCCESS;
 961}
 962
 963#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
 964static void me1600_ao_work_control_task(void *subdevice)
 965#else
 966static void me1600_ao_work_control_task(struct work_struct *work)
 967#endif
 968{
 969        me1600_ao_subdevice_t *instance;
 970        int reschedule = 1;
 971        int signaling = 0;
 972
 973#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
 974        instance = (me1600_ao_subdevice_t *) subdevice;
 975#else
 976        instance =
 977            container_of((void *)work, me1600_ao_subdevice_t, ao_control_task);
 978#endif
 979
 980        PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
 981              instance->ao_idx);
 982
 983        if (!((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {       // Output was triggerd.
 984                // Signal the end.
 985                signaling = 1;
 986                reschedule = 0;
 987                if (instance->status == ao_status_single_run) {
 988                        instance->status = ao_status_single_end;
 989                }
 990
 991        } else if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) {        // Timeout
 992                PDEBUG("Timeout reached.\n");
 993                spin_lock(instance->ao_shadows_lock);
 994                // Restore old settings.
 995                PDEBUG("Write old value back to register.\n");
 996                (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
 997                    (instance->ao_regs_shadows)->mirror[instance->ao_idx];
 998
 999                outw((instance->ao_regs_shadows)->mirror[instance->ao_idx],
1000                     (instance->ao_regs_shadows)->registry[instance->ao_idx]);
1001                PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
1002                           instance->reg_base,
1003                           (instance->ao_regs_shadows)->registry[instance->
1004                                                                 ao_idx] -
1005                           instance->reg_base,
1006                           (instance->ao_regs_shadows)->mirror[instance->
1007                                                               ao_idx]);
1008
1009                //Remove from synchronous strt list.
1010                (instance->ao_regs_shadows)->trigger &=
1011                    ~(0x1 << instance->ao_idx);
1012                if (instance->status == ao_status_none) {
1013                        instance->status = ao_status_single_end;
1014                }
1015                spin_unlock(instance->ao_shadows_lock);
1016
1017                // Signal the end.
1018                signaling = 1;
1019                reschedule = 0;
1020        }
1021
1022        if (signaling) {        //Signal it.
1023                wake_up_interruptible_all(&instance->wait_queue);
1024        }
1025
1026        if (instance->ao_control_task_flag && reschedule) {     // Reschedule task
1027                queue_delayed_work(instance->me1600_workqueue,
1028                                   &instance->ao_control_task, 1);
1029        } else {
1030                PINFO("<%s> Ending control task.\n", __func__);
1031        }
1032
1033}
1034
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.