linux/drivers/staging/iio/ring_generic.h
<<
>>
Prefs
   1/* The industrial I/O core - generic ring buffer interfaces.
   2 *
   3 * Copyright (c) 2008 Jonathan Cameron
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 as published by
   7 * the Free Software Foundation.
   8 */
   9
  10#ifndef _IIO_RING_GENERIC_H_
  11#define _IIO_RING_GENERIC_H_
  12#include "iio.h"
  13
  14struct iio_handler;
  15struct iio_ring_buffer;
  16struct iio_dev;
  17
  18/**
  19 * iio_push_ring_event() - ring buffer specific push to event chrdev
  20 * @ring_buf:           ring buffer that is the event source
  21 * @event_code:         event indentification code
  22 * @timestamp:          time of event
  23 **/
  24int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
  25                        int event_code,
  26                        s64 timestamp);
  27/**
  28 * iio_push_or_escallate_ring_event() - escallate or add as appropriate
  29 *
  30 * Typical usecase is to escallate a 50% ring full to 75% full if noone has yet
  31 * read the first event. Clearly the 50% full is no longer of interest in
  32 * typical use case.
  33 **/
  34int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
  35                                     int event_code,
  36                                     s64 timestamp);
  37
  38/**
  39 * struct iio_ring_access_funcs - access functions for ring buffers.
  40 * @create:             perform allocation
  41 * @init:               get ring buffer ready for use
  42 * @_exit:              reverse steps in init
  43 * @_free:              deallocate ring buffer
  44 * @mark_in_use:        reference counting, typically to prevent module removal
  45 * @unmark_in_use:      reduce reference count when no longer using ring buffer
  46 * @store_to:           actually store stuff to the ring buffer
  47 * @read_last:          get the last element stored
  48 * @rip_lots:           try to get a specified number of elements (must exist)
  49 * @mark_param_change:  notify ring that some relevant parameter has changed
  50 *                      Often this means the underlying storage may need to
  51 *                      change.
  52 * @request_update:     if a parameter change has been marked, update underlying
  53 *                      storage.
  54 * @get_bpd:            get current bytes per datum
  55 * @set_bpd:            set number of bytes per datum
  56 * @get_length:         get number of datums in ring
  57 * @set_length:         set number of datums in ring
  58 * @is_enabled:         query if ring is currently being used
  59 * @enable:             enable the ring
  60 *
  61 * The purpose of this structure is to make the ring buffer element
  62 * modular as event for a given driver, different usecases may require
  63 * different ring designs (space efficiency vs speed for example.
  64 *
  65 * It is worth noting that a given ring implementation may only support a small
  66 * proportion of these functions.  The core code 'should' cope fine with any of
  67 * them not existing.
  68 **/
  69struct iio_ring_access_funcs {
  70        void (*mark_in_use)(struct iio_ring_buffer *ring);
  71        void (*unmark_in_use)(struct iio_ring_buffer *ring);
  72
  73        int (*store_to)(struct iio_ring_buffer *ring, u8 *data, s64 timestamp);
  74        int (*read_last)(struct iio_ring_buffer *ring, u8 *data);
  75        int (*rip_lots)(struct iio_ring_buffer *ring,
  76                        size_t count,
  77                        u8 **data,
  78                        int *dead_offset);
  79
  80        int (*mark_param_change)(struct iio_ring_buffer *ring);
  81        int (*request_update)(struct iio_ring_buffer *ring);
  82
  83        int (*get_bpd)(struct iio_ring_buffer *ring);
  84        int (*set_bpd)(struct iio_ring_buffer *ring, size_t bpd);
  85        int (*get_length)(struct iio_ring_buffer *ring);
  86        int (*set_length)(struct iio_ring_buffer *ring, int length);
  87
  88        int (*is_enabled)(struct iio_ring_buffer *ring);
  89        int (*enable)(struct iio_ring_buffer *ring);
  90};
  91
  92/**
  93 * struct iio_ring_buffer - general ring buffer structure
  94 * @length:             [DEVICE]number of datums in ring
  95 * @bpd:                [DEVICE]size of individual datum including timestamp
  96 * @loopcount:          [INTERN]number of times the ring has looped
  97 * @access_minor_name:  [INTERN]store of name of the access chrdev minor number
  98 *                      sysfs attribute
  99 * @access_handler:     [INTERN]chrdev access handling
 100 * @event_minor_name:   [INTERN]store of name of the event chrdev minor number
 101 *                      sysfs attribute
 102 * @ev_int:             [INTERN]chrdev interface for the event chrdev
 103 * @shared_ev_pointer:  [INTERN]the shared event pointer to allow escalation of
 104 *                      events
 105 * @ring_access:        [DRIVER]ring access functions associated with the
 106 *                      implementation.
 107 * @ring_prenable:      [DRIVER] function to run prior to marking ring enabled
 108 * @ring_postenable:    [DRIVER] function to run after marking ring enabled
 109 * @ring_predisable:    [DRIVER] function to run prior to marking ring disabled
 110 * @ring_postdisable:   [DRIVER] function to run after marking ring disabled
 111  **/
 112struct iio_ring_buffer {
 113        struct device dev;
 114        struct device access_dev;
 115        struct iio_dev *indio_dev;
 116        struct module *owner;
 117        int                             id;
 118        int                             access_id;
 119        int                             length;
 120        int                             bpd;
 121        int                             loopcount;
 122        struct iio_handler              access_handler;
 123        struct iio_event_interface      ev_int;
 124        struct iio_shared_ev_pointer    shared_ev_pointer;
 125        struct iio_ring_access_funcs    access;
 126        int                             (*preenable)(struct iio_dev *);
 127        int                             (*postenable)(struct iio_dev *);
 128        int                             (*predisable)(struct iio_dev *);
 129        int                             (*postdisable)(struct iio_dev *);
 130
 131};
 132void iio_ring_buffer_init(struct iio_ring_buffer *ring,
 133                          struct iio_dev *dev_info);
 134
 135/**
 136 * __iio_init_ring_buffer() - initialize common elements of ring buffers.
 137 **/
 138static inline void __iio_init_ring_buffer(struct iio_ring_buffer *ring,
 139                                 int bytes_per_datum, int length)
 140{
 141        ring->bpd = bytes_per_datum;
 142        ring->length = length;
 143        ring->loopcount = 0;
 144        ring->shared_ev_pointer.ev_p = 0;
 145        ring->shared_ev_pointer.lock =
 146                __SPIN_LOCK_UNLOCKED(ring->shared_ev_pointer->loc);
 147}
 148
 149/**
 150 * struct iio_scan_el - an individual element of a scan
 151 * @dev_attr:           control attribute (if directly controllable)
 152 * @number:             unique identifier of element (used for bit mask)
 153 * @bit_count:          number of bits in scan element
 154 * @label:              useful data for the scan el (often reg address)
 155 * @set_state:          for some devices datardy signals are generated
 156 *                      for any enabled lines.  This allows unwanted lines
 157 *                      to be disabled and hence not get in the way.
 158 **/
 159struct iio_scan_el {
 160        struct device_attribute         dev_attr;
 161        unsigned int                    number;
 162        int                             bit_count;
 163        unsigned int                    label;
 164
 165        int (*set_state)(struct iio_scan_el *scanel,
 166                         struct iio_dev *dev_info,
 167                         bool state);
 168};
 169
 170#define to_iio_scan_el(_dev_attr)                               \
 171        container_of(_dev_attr, struct iio_scan_el, dev_attr);
 172
 173/**
 174 * iio_scan_el_store() - sysfs scan element selection interface.
 175 *
 176 * A generic function used to enable various scan elements.  In some
 177 * devices explicit read commands for each channel mean this is merely
 178 * a software switch.  In others this must actively disable the channel.
 179 * Complexities occur when this interacts with data ready type triggers
 180 * which may not reset unless every channel that is enabled is explicitly
 181 * read.
 182 **/
 183ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
 184                          const char *buf, size_t len);
 185/**
 186 * iio_scal_el_show() - sysfs interface to query whether a scan element is
 187 *                      is enabled or not.
 188 **/
 189ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
 190                         char *buf);
 191/**
 192 * IIO_SCAN_EL: - declare and initialize a scan element without control func
 193 * @_name:      identifying name. Resulting struct is iio_scan_el_##_name,
 194 *              sysfs element, scan_en_##_name.
 195 * @_number:    unique id number for the scan element.
 196 * @_bits:      number of bits in the scan element result (used in mixed bit
 197 *              length devices).
 198 * @_label:     indentification variable used by drivers.  Often a reg address.
 199 **/
 200#define IIO_SCAN_EL(_name, _number, _bits, _label)                      \
 201        struct iio_scan_el iio_scan_el_##_name = {                      \
 202                .dev_attr = __ATTR(scan_en_##_name,                     \
 203                                   S_IRUGO | S_IWUSR,                   \
 204                                   iio_scan_el_show,                    \
 205                                   iio_scan_el_store),                  \
 206                .mask = (1 << _number),                                 \
 207                .bit_count = _bits,                                     \
 208                .label = _label,                                        \
 209        }
 210
 211ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
 212                             const char *buf, size_t len);
 213
 214ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
 215                            char *buf);
 216/**
 217 * IIO_SCAN_EL_C: - declare and initialize a scan element with a control func
 218 *
 219 * @_controlfunc: function used to notify hardware of whether state changes
 220 **/
 221#define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)      \
 222        struct iio_scan_el iio_scan_el_##_name = {                      \
 223                .dev_attr = __ATTR(scan_en_##_name,                     \
 224                                   S_IRUGO | S_IWUSR,                   \
 225                                   iio_scan_el_show,                    \
 226                                   iio_scan_el_store),                  \
 227                .number =  _number,                                     \
 228                .bit_count = _bits,                                     \
 229                .label = _label,                                        \
 230                .set_state = _controlfunc,                              \
 231        }
 232/**
 233 * IIO_SCAN_EL_TIMESTAMP: - declare a special scan element for timestamps
 234 *
 235 * Odd one out. Handled slightly differently from other scan elements.
 236 **/
 237#define IIO_SCAN_EL_TIMESTAMP                                   \
 238        struct iio_scan_el iio_scan_el_timestamp = {            \
 239                .dev_attr = __ATTR(scan_en_timestamp,           \
 240                                   S_IRUGO | S_IWUSR,           \
 241                                   iio_scan_el_ts_show,         \
 242                                   iio_scan_el_ts_store),       \
 243        }
 244
 245static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
 246{
 247        put_device(&ring->dev);
 248};
 249
 250#define to_iio_ring_buffer(d)                   \
 251        container_of(d, struct iio_ring_buffer, dev)
 252#define access_dev_to_iio_ring_buffer(d)                        \
 253        container_of(d, struct iio_ring_buffer, access_dev)
 254int iio_ring_buffer_register(struct iio_ring_buffer *ring);
 255void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
 256
 257ssize_t iio_read_ring_length(struct device *dev,
 258                             struct device_attribute *attr,
 259                             char *buf);
 260ssize_t iio_write_ring_length(struct device *dev,
 261                              struct device_attribute *attr,
 262                              const char *buf,
 263                              size_t len);
 264ssize_t iio_read_ring_bps(struct device *dev,
 265                          struct device_attribute *attr,
 266                          char *buf);
 267ssize_t iio_store_ring_enable(struct device *dev,
 268                              struct device_attribute *attr,
 269                              const char *buf,
 270                              size_t len);
 271ssize_t iio_show_ring_enable(struct device *dev,
 272                             struct device_attribute *attr,
 273                             char *buf);
 274#define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR,     \
 275                                         iio_read_ring_length,          \
 276                                         iio_write_ring_length)
 277#define IIO_RING_BPS_ATTR DEVICE_ATTR(bps, S_IRUGO | S_IWUSR,   \
 278                                      iio_read_ring_bps, NULL)
 279#define IIO_RING_ENABLE_ATTR DEVICE_ATTR(ring_enable, S_IRUGO | S_IWUSR, \
 280                                         iio_show_ring_enable,          \
 281                                         iio_store_ring_enable)
 282
 283#endif /* _IIO_RING_GENERIC_H_ */
 284