Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * comedidev.h
0004  * header file for kernel-only structures, variables, and constants
0005  *
0006  * COMEDI - Linux Control and Measurement Device Interface
0007  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
0008  */
0009 
0010 #ifndef _COMEDIDEV_H
0011 #define _COMEDIDEV_H
0012 
0013 #include <linux/dma-mapping.h>
0014 #include <linux/mutex.h>
0015 #include <linux/spinlock_types.h>
0016 #include <linux/rwsem.h>
0017 #include <linux/kref.h>
0018 #include <linux/comedi.h>
0019 
0020 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
0021 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
0022     COMEDI_MINORVERSION, COMEDI_MICROVERSION)
0023 #define COMEDI_RELEASE VERSION
0024 
0025 #define COMEDI_NUM_BOARD_MINORS 0x30
0026 
0027 /**
0028  * struct comedi_subdevice - Working data for a COMEDI subdevice
0029  * @device: COMEDI device to which this subdevice belongs.  (Initialized by
0030  *  comedi_alloc_subdevices().)
0031  * @index: Index of this subdevice within device's array of subdevices.
0032  *  (Initialized by comedi_alloc_subdevices().)
0033  * @type: Type of subdevice from &enum comedi_subdevice_type.  (Initialized by
0034  *  the low-level driver.)
0035  * @n_chan: Number of channels the subdevice supports.  (Initialized by the
0036  *  low-level driver.)
0037  * @subdev_flags: Various "SDF" flags indicating aspects of the subdevice to
0038  *  the COMEDI core and user application.  (Initialized by the low-level
0039  *  driver.)
0040  * @len_chanlist: Maximum length of a channel list if the subdevice supports
0041  *  asynchronous acquisition commands.  (Optionally initialized by the
0042  *  low-level driver, or changed from 0 to 1 during post-configuration.)
0043  * @private: Private data pointer which is either set by the low-level driver
0044  *  itself, or by a call to comedi_alloc_spriv() which allocates storage.
0045  *  In the latter case, the storage is automatically freed after the
0046  *  low-level driver's "detach" handler is called for the device.
0047  *  (Initialized by the low-level driver.)
0048  * @async: Pointer to &struct comedi_async id the subdevice supports
0049  *  asynchronous acquisition commands.  (Allocated and initialized during
0050  *  post-configuration if needed.)
0051  * @lock: Pointer to a file object that performed a %COMEDI_LOCK ioctl on the
0052  *  subdevice.  (Initially NULL.)
0053  * @busy: Pointer to a file object that is performing an asynchronous
0054  *  acquisition command on the subdevice.  (Initially NULL.)
0055  * @runflags: Internal flags for use by COMEDI core, mostly indicating whether
0056  *  an asynchronous acquisition command is running.
0057  * @spin_lock: Generic spin-lock for use by the COMEDI core and the low-level
0058  *  driver.  (Initialized by comedi_alloc_subdevices().)
0059  * @io_bits: Bit-mask indicating the channel directions for a DIO subdevice
0060  *  with no more than 32 channels.  A '1' at a bit position indicates the
0061  *  corresponding channel is configured as an output.  (Initialized by the
0062  *  low-level driver for a DIO subdevice.  Forced to all-outputs during
0063  *  post-configuration for a digital output subdevice.)
0064  * @maxdata: If non-zero, this is the maximum raw data value of each channel.
0065  *  If zero, the maximum data value is channel-specific.  (Initialized by
0066  *  the low-level driver.)
0067  * @maxdata_list: If the maximum data value is channel-specific, this points
0068  *  to an array of maximum data values indexed by channel index.
0069  *  (Initialized by the low-level driver.)
0070  * @range_table: If non-NULL, this points to a COMEDI range table for the
0071  *  subdevice.  If NULL, the range table is channel-specific.  (Initialized
0072  *  by the low-level driver, will be set to an "invalid" range table during
0073  *  post-configuration if @range_table and @range_table_list are both
0074  *  NULL.)
0075  * @range_table_list: If the COMEDI range table is channel-specific, this
0076  *  points to an array of pointers to COMEDI range tables indexed by
0077  *  channel number.  (Initialized by the low-level driver.)
0078  * @chanlist: Not used.
0079  * @insn_read: Optional pointer to a handler for the %INSN_READ instruction.
0080  *  (Initialized by the low-level driver, or set to a default handler
0081  *  during post-configuration.)
0082  * @insn_write: Optional pointer to a handler for the %INSN_WRITE instruction.
0083  *  (Initialized by the low-level driver, or set to a default handler
0084  *  during post-configuration.)
0085  * @insn_bits: Optional pointer to a handler for the %INSN_BITS instruction
0086  *  for a digital input, digital output or digital input/output subdevice.
0087  *  (Initialized by the low-level driver, or set to a default handler
0088  *  during post-configuration.)
0089  * @insn_config: Optional pointer to a handler for the %INSN_CONFIG
0090  *  instruction.  (Initialized by the low-level driver, or set to a default
0091  *  handler during post-configuration.)
0092  * @do_cmd: If the subdevice supports asynchronous acquisition commands, this
0093  *  points to a handler to set it up in hardware.  (Initialized by the
0094  *  low-level driver.)
0095  * @do_cmdtest: If the subdevice supports asynchronous acquisition commands,
0096  *  this points to a handler used to check and possibly tweak a prospective
0097  *  acquisition command without setting it up in hardware.  (Initialized by
0098  *  the low-level driver.)
0099  * @poll: If the subdevice supports asynchronous acquisition commands, this
0100  *  is an optional pointer to a handler for the %COMEDI_POLL ioctl which
0101  *  instructs the low-level driver to synchronize buffers.  (Initialized by
0102  *  the low-level driver if needed.)
0103  * @cancel: If the subdevice supports asynchronous acquisition commands, this
0104  *  points to a handler used to terminate a running command.  (Initialized
0105  *  by the low-level driver.)
0106  * @buf_change: If the subdevice supports asynchronous acquisition commands,
0107  *  this is an optional pointer to a handler that is called when the data
0108  *  buffer for handling asynchronous commands is allocated or reallocated.
0109  *  (Initialized by the low-level driver if needed.)
0110  * @munge: If the subdevice supports asynchronous acquisition commands and
0111  *  uses DMA to transfer data from the hardware to the acquisition buffer,
0112  *  this points to a function used to "munge" the data values from the
0113  *  hardware into the format expected by COMEDI.  (Initialized by the
0114  *  low-level driver if needed.)
0115  * @async_dma_dir: If the subdevice supports asynchronous acquisition commands
0116  *  and uses DMA to transfer data from the hardware to the acquisition
0117  *  buffer, this sets the DMA direction for the buffer. (initialized to
0118  *  %DMA_NONE by comedi_alloc_subdevices() and changed by the low-level
0119  *  driver if necessary.)
0120  * @state: Handy bit-mask indicating the output states for a DIO or digital
0121  *  output subdevice with no more than 32 channels. (Initialized by the
0122  *  low-level driver.)
0123  * @class_dev: If the subdevice supports asynchronous acquisition commands,
0124  *  this points to a sysfs comediX_subdY device where X is the minor device
0125  *  number of the COMEDI device and Y is the subdevice number.  The minor
0126  *  device number for the sysfs device is allocated dynamically in the
0127  *  range 48 to 255.  This is used to allow the COMEDI device to be opened
0128  *  with a different default read or write subdevice.  (Allocated during
0129  *  post-configuration if needed.)
0130  * @minor: If @class_dev is set, this is its dynamically allocated minor
0131  *  device number.  (Set during post-configuration if necessary.)
0132  * @readback: Optional pointer to memory allocated by
0133  *  comedi_alloc_subdev_readback() used to hold the values written to
0134  *  analog output channels so they can be read back.  The storage is
0135  *  automatically freed after the low-level driver's "detach" handler is
0136  *  called for the device.  (Initialized by the low-level driver.)
0137  *
0138  * This is the main control structure for a COMEDI subdevice.  If the subdevice
0139  * supports asynchronous acquisition commands, additional information is stored
0140  * in the &struct comedi_async pointed to by @async.
0141  *
0142  * Most of the subdevice is initialized by the low-level driver's "attach" or
0143  * "auto_attach" handlers but parts of it are initialized by
0144  * comedi_alloc_subdevices(), and other parts are initialized during
0145  * post-configuration on return from that handler.
0146  *
0147  * A low-level driver that sets @insn_bits for a digital input, digital output,
0148  * or DIO subdevice may leave @insn_read and @insn_write uninitialized, in
0149  * which case they will be set to a default handler during post-configuration
0150  * that uses @insn_bits to emulate the %INSN_READ and %INSN_WRITE instructions.
0151  */
0152 struct comedi_subdevice {
0153     struct comedi_device *device;
0154     int index;
0155     int type;
0156     int n_chan;
0157     int subdev_flags;
0158     int len_chanlist;   /* maximum length of channel/gain list */
0159 
0160     void *private;
0161 
0162     struct comedi_async *async;
0163 
0164     void *lock;
0165     void *busy;
0166     unsigned int runflags;
0167     spinlock_t spin_lock;   /* generic spin-lock for COMEDI and drivers */
0168 
0169     unsigned int io_bits;
0170 
0171     unsigned int maxdata;   /* if maxdata==0, use list */
0172     const unsigned int *maxdata_list;   /* list is channel specific */
0173 
0174     const struct comedi_lrange *range_table;
0175     const struct comedi_lrange *const *range_table_list;
0176 
0177     unsigned int *chanlist; /* driver-owned chanlist (not used) */
0178 
0179     int (*insn_read)(struct comedi_device *dev, struct comedi_subdevice *s,
0180              struct comedi_insn *insn, unsigned int *data);
0181     int (*insn_write)(struct comedi_device *dev, struct comedi_subdevice *s,
0182               struct comedi_insn *insn, unsigned int *data);
0183     int (*insn_bits)(struct comedi_device *dev, struct comedi_subdevice *s,
0184              struct comedi_insn *insn, unsigned int *data);
0185     int (*insn_config)(struct comedi_device *dev,
0186                struct comedi_subdevice *s,
0187                struct comedi_insn *insn,
0188                unsigned int *data);
0189 
0190     int (*do_cmd)(struct comedi_device *dev, struct comedi_subdevice *s);
0191     int (*do_cmdtest)(struct comedi_device *dev,
0192               struct comedi_subdevice *s,
0193               struct comedi_cmd *cmd);
0194     int (*poll)(struct comedi_device *dev, struct comedi_subdevice *s);
0195     int (*cancel)(struct comedi_device *dev, struct comedi_subdevice *s);
0196 
0197     /* called when the buffer changes */
0198     int (*buf_change)(struct comedi_device *dev,
0199               struct comedi_subdevice *s);
0200 
0201     void (*munge)(struct comedi_device *dev, struct comedi_subdevice *s,
0202               void *data, unsigned int num_bytes,
0203               unsigned int start_chan_index);
0204     enum dma_data_direction async_dma_dir;
0205 
0206     unsigned int state;
0207 
0208     struct device *class_dev;
0209     int minor;
0210 
0211     unsigned int *readback;
0212 };
0213 
0214 /**
0215  * struct comedi_buf_page - Describe a page of a COMEDI buffer
0216  * @virt_addr: Kernel address of page.
0217  * @dma_addr: DMA address of page if in DMA coherent memory.
0218  */
0219 struct comedi_buf_page {
0220     void *virt_addr;
0221     dma_addr_t dma_addr;
0222 };
0223 
0224 /**
0225  * struct comedi_buf_map - Describe pages in a COMEDI buffer
0226  * @dma_hw_dev: Low-level hardware &struct device pointer copied from the
0227  *  COMEDI device's hw_dev member.
0228  * @page_list: Pointer to array of &struct comedi_buf_page, one for each
0229  *  page in the buffer.
0230  * @n_pages: Number of pages in the buffer.
0231  * @dma_dir: DMA direction used to allocate pages of DMA coherent memory,
0232  *  or %DMA_NONE if pages allocated from regular memory.
0233  * @refcount: &struct kref reference counter used to free the buffer.
0234  *
0235  * A COMEDI data buffer is allocated as individual pages, either in
0236  * conventional memory or DMA coherent memory, depending on the attached,
0237  * low-level hardware device.  (The buffer pages also get mapped into the
0238  * kernel's contiguous virtual address space pointed to by the 'prealloc_buf'
0239  * member of &struct comedi_async.)
0240  *
0241  * The buffer is normally freed when the COMEDI device is detached from the
0242  * low-level driver (which may happen due to device removal), but if it happens
0243  * to be mmapped at the time, the pages cannot be freed until the buffer has
0244  * been munmapped.  That is what the reference counter is for.  (The virtual
0245  * address space pointed by 'prealloc_buf' is freed when the COMEDI device is
0246  * detached.)
0247  */
0248 struct comedi_buf_map {
0249     struct device *dma_hw_dev;
0250     struct comedi_buf_page *page_list;
0251     unsigned int n_pages;
0252     enum dma_data_direction dma_dir;
0253     struct kref refcount;
0254 };
0255 
0256 /**
0257  * struct comedi_async - Control data for asynchronous COMEDI commands
0258  * @prealloc_buf: Kernel virtual address of allocated acquisition buffer.
0259  * @prealloc_bufsz: Buffer size (in bytes).
0260  * @buf_map: Map of buffer pages.
0261  * @max_bufsize: Maximum allowed buffer size (in bytes).
0262  * @buf_write_count: "Write completed" count (in bytes, modulo 2**32).
0263  * @buf_write_alloc_count: "Allocated for writing" count (in bytes,
0264  *  modulo 2**32).
0265  * @buf_read_count: "Read completed" count (in bytes, modulo 2**32).
0266  * @buf_read_alloc_count: "Allocated for reading" count (in bytes,
0267  *  modulo 2**32).
0268  * @buf_write_ptr: Buffer position for writer.
0269  * @buf_read_ptr: Buffer position for reader.
0270  * @cur_chan: Current position in chanlist for scan (for those drivers that
0271  *  use it).
0272  * @scans_done: The number of scans completed.
0273  * @scan_progress: Amount received or sent for current scan (in bytes).
0274  * @munge_chan: Current position in chanlist for "munging".
0275  * @munge_count: "Munge" count (in bytes, modulo 2**32).
0276  * @munge_ptr: Buffer position for "munging".
0277  * @events: Bit-vector of events that have occurred.
0278  * @cmd: Details of comedi command in progress.
0279  * @wait_head: Task wait queue for file reader or writer.
0280  * @cb_mask: Bit-vector of events that should wake waiting tasks.
0281  * @inttrig: Software trigger function for command, or NULL.
0282  *
0283  * Note about the ..._count and ..._ptr members:
0284  *
0285  * Think of the _Count values being integers of unlimited size, indexing
0286  * into a buffer of infinite length (though only an advancing portion
0287  * of the buffer of fixed length prealloc_bufsz is accessible at any
0288  * time).  Then:
0289  *
0290  *   Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <=
0291  *   Buf_Write_Count <= Buf_Write_Alloc_Count <=
0292  *   (Buf_Read_Count + prealloc_bufsz)
0293  *
0294  * (Those aren't the actual members, apart from prealloc_bufsz.) When the
0295  * buffer is reset, those _Count values start at 0 and only increase in value,
0296  * maintaining the above inequalities until the next time the buffer is
0297  * reset.  The buffer is divided into the following regions by the inequalities:
0298  *
0299  *   [0, Buf_Read_Count):
0300  *     old region no longer accessible
0301  *
0302  *   [Buf_Read_Count, Buf_Read_Alloc_Count):
0303  *     filled and munged region allocated for reading but not yet read
0304  *
0305  *   [Buf_Read_Alloc_Count, Munge_Count):
0306  *     filled and munged region not yet allocated for reading
0307  *
0308  *   [Munge_Count, Buf_Write_Count):
0309  *     filled region not yet munged
0310  *
0311  *   [Buf_Write_Count, Buf_Write_Alloc_Count):
0312  *     unfilled region allocated for writing but not yet written
0313  *
0314  *   [Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz):
0315  *     unfilled region not yet allocated for writing
0316  *
0317  *   [Buf_Read_Count + prealloc_bufsz, infinity):
0318  *     unfilled region not yet accessible
0319  *
0320  * Data needs to be written into the buffer before it can be read out,
0321  * and may need to be converted (or "munged") between the two
0322  * operations.  Extra unfilled buffer space may need to allocated for
0323  * writing (advancing Buf_Write_Alloc_Count) before new data is written.
0324  * After writing new data, the newly filled space needs to be released
0325  * (advancing Buf_Write_Count).  This also results in the new data being
0326  * "munged" (advancing Munge_Count).  Before data is read out of the
0327  * buffer, extra space may need to be allocated for reading (advancing
0328  * Buf_Read_Alloc_Count).  After the data has been read out, the space
0329  * needs to be released (advancing Buf_Read_Count).
0330  *
0331  * The actual members, buf_read_count, buf_read_alloc_count,
0332  * munge_count, buf_write_count, and buf_write_alloc_count take the
0333  * value of the corresponding capitalized _Count values modulo 2^32
0334  * (UINT_MAX+1).  Subtracting a "higher" _count value from a "lower"
0335  * _count value gives the same answer as subtracting a "higher" _Count
0336  * value from a lower _Count value because prealloc_bufsz < UINT_MAX+1.
0337  * The modulo operation is done implicitly.
0338  *
0339  * The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value
0340  * of the corresponding capitalized _Count values modulo prealloc_bufsz.
0341  * These correspond to byte indices in the physical buffer.  The modulo
0342  * operation is done by subtracting prealloc_bufsz when the value
0343  * exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is
0344  * less than or equal to UINT_MAX).
0345  */
0346 struct comedi_async {
0347     void *prealloc_buf;
0348     unsigned int prealloc_bufsz;
0349     struct comedi_buf_map *buf_map;
0350     unsigned int max_bufsize;
0351     unsigned int buf_write_count;
0352     unsigned int buf_write_alloc_count;
0353     unsigned int buf_read_count;
0354     unsigned int buf_read_alloc_count;
0355     unsigned int buf_write_ptr;
0356     unsigned int buf_read_ptr;
0357     unsigned int cur_chan;
0358     unsigned int scans_done;
0359     unsigned int scan_progress;
0360     unsigned int munge_chan;
0361     unsigned int munge_count;
0362     unsigned int munge_ptr;
0363     unsigned int events;
0364     struct comedi_cmd cmd;
0365     wait_queue_head_t wait_head;
0366     unsigned int cb_mask;
0367     int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s,
0368                unsigned int x);
0369 };
0370 
0371 /**
0372  * enum comedi_cb - &struct comedi_async callback "events"
0373  * @COMEDI_CB_EOS:      end-of-scan
0374  * @COMEDI_CB_EOA:      end-of-acquisition/output
0375  * @COMEDI_CB_BLOCK:        data has arrived, wakes up read() / write()
0376  * @COMEDI_CB_EOBUF:        DEPRECATED: end of buffer
0377  * @COMEDI_CB_ERROR:        card error during acquisition
0378  * @COMEDI_CB_OVERFLOW:     buffer overflow/underflow
0379  * @COMEDI_CB_ERROR_MASK:   events that indicate an error has occurred
0380  * @COMEDI_CB_CANCEL_MASK:  events that will cancel an async command
0381  */
0382 enum comedi_cb {
0383     COMEDI_CB_EOS       = BIT(0),
0384     COMEDI_CB_EOA       = BIT(1),
0385     COMEDI_CB_BLOCK     = BIT(2),
0386     COMEDI_CB_EOBUF     = BIT(3),
0387     COMEDI_CB_ERROR     = BIT(4),
0388     COMEDI_CB_OVERFLOW  = BIT(5),
0389     /* masks */
0390     COMEDI_CB_ERROR_MASK    = (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW),
0391     COMEDI_CB_CANCEL_MASK   = (COMEDI_CB_EOA | COMEDI_CB_ERROR_MASK)
0392 };
0393 
0394 /**
0395  * struct comedi_driver - COMEDI driver registration
0396  * @driver_name: Name of driver.
0397  * @module: Owning module.
0398  * @attach: The optional "attach" handler for manually configured COMEDI
0399  *  devices.
0400  * @detach: The "detach" handler for deconfiguring COMEDI devices.
0401  * @auto_attach: The optional "auto_attach" handler for automatically
0402  *  configured COMEDI devices.
0403  * @num_names: Optional number of "board names" supported.
0404  * @board_name: Optional pointer to a pointer to a board name.  The pointer
0405  *  to a board name is embedded in an element of a driver-defined array
0406  *  of static, read-only board type information.
0407  * @offset: Optional size of each element of the driver-defined array of
0408  *  static, read-only board type information, i.e. the offset between each
0409  *  pointer to a board name.
0410  *
0411  * This is used with comedi_driver_register() and comedi_driver_unregister() to
0412  * register and unregister a low-level COMEDI driver with the COMEDI core.
0413  *
0414  * If @num_names is non-zero, @board_name should be non-NULL, and @offset
0415  * should be at least sizeof(*board_name).  These are used by the handler for
0416  * the %COMEDI_DEVCONFIG ioctl to match a hardware device and its driver by
0417  * board name.  If @num_names is zero, the %COMEDI_DEVCONFIG ioctl matches a
0418  * hardware device and its driver by driver name.  This is only useful if the
0419  * @attach handler is set.  If @num_names is non-zero, the driver's @attach
0420  * handler will be called with the COMEDI device structure's board_ptr member
0421  * pointing to the matched pointer to a board name within the driver's private
0422  * array of static, read-only board type information.
0423  *
0424  * The @detach handler has two roles.  If a COMEDI device was successfully
0425  * configured by the @attach or @auto_attach handler, it is called when the
0426  * device is being deconfigured (by the %COMEDI_DEVCONFIG ioctl, or due to
0427  * unloading of the driver, or due to device removal).  It is also called when
0428  * the @attach or @auto_attach handler returns an error.  Therefore, the
0429  * @attach or @auto_attach handlers can defer clean-up on error until the
0430  * @detach handler is called.  If the @attach or @auto_attach handlers free
0431  * any resources themselves, they must prevent the @detach handler from
0432  * freeing the same resources.  The @detach handler must not assume that all
0433  * resources requested by the @attach or @auto_attach handler were
0434  * successfully allocated.
0435  */
0436 struct comedi_driver {
0437     /* private: */
0438     struct comedi_driver *next; /* Next in list of COMEDI drivers. */
0439     /* public: */
0440     const char *driver_name;
0441     struct module *module;
0442     int (*attach)(struct comedi_device *dev, struct comedi_devconfig *it);
0443     void (*detach)(struct comedi_device *dev);
0444     int (*auto_attach)(struct comedi_device *dev, unsigned long context);
0445     unsigned int num_names;
0446     const char *const *board_name;
0447     int offset;
0448 };
0449 
0450 /**
0451  * struct comedi_device - Working data for a COMEDI device
0452  * @use_count: Number of open file objects.
0453  * @driver: Low-level COMEDI driver attached to this COMEDI device.
0454  * @pacer: Optional pointer to a dynamically allocated acquisition pacer
0455  *  control.  It is freed automatically after the COMEDI device is
0456  *  detached from the low-level driver.
0457  * @private: Optional pointer to private data allocated by the low-level
0458  *  driver.  It is freed automatically after the COMEDI device is
0459  *  detached from the low-level driver.
0460  * @class_dev: Sysfs comediX device.
0461  * @minor: Minor device number of COMEDI char device (0-47).
0462  * @detach_count: Counter incremented every time the COMEDI device is detached.
0463  *  Used for checking a previous attachment is still valid.
0464  * @hw_dev: Optional pointer to the low-level hardware &struct device.  It is
0465  *  required for automatically configured COMEDI devices and optional for
0466  *  COMEDI devices configured by the %COMEDI_DEVCONFIG ioctl, although
0467  *  the bus-specific COMEDI functions only work if it is set correctly.
0468  *  It is also passed to dma_alloc_coherent() for COMEDI subdevices that
0469  *  have their 'async_dma_dir' member set to something other than
0470  *  %DMA_NONE.
0471  * @board_name: Pointer to a COMEDI board name or a COMEDI driver name.  When
0472  *  the low-level driver's "attach" handler is called by the handler for
0473  *  the %COMEDI_DEVCONFIG ioctl, it either points to a matched board name
0474  *  string if the 'num_names' member of the &struct comedi_driver is
0475  *  non-zero, otherwise it points to the low-level driver name string.
0476  *  When the low-lever driver's "auto_attach" handler is called for an
0477  *  automatically configured COMEDI device, it points to the low-level
0478  *  driver name string.  The low-level driver is free to change it in its
0479  *  "attach" or "auto_attach" handler if it wishes.
0480  * @board_ptr: Optional pointer to private, read-only board type information in
0481  *  the low-level driver.  If the 'num_names' member of the &struct
0482  *  comedi_driver is non-zero, the handler for the %COMEDI_DEVCONFIG ioctl
0483  *  will point it to a pointer to a matched board name string within the
0484  *  driver's private array of static, read-only board type information when
0485  *  calling the driver's "attach" handler.  The low-level driver is free to
0486  *  change it.
0487  * @attached: Flag indicating that the COMEDI device is attached to a low-level
0488  *  driver.
0489  * @ioenabled: Flag used to indicate that a PCI device has been enabled and
0490  *  its regions requested.
0491  * @spinlock: Generic spin-lock for use by the low-level driver.
0492  * @mutex: Generic mutex for use by the COMEDI core module.
0493  * @attach_lock: &struct rw_semaphore used to guard against the COMEDI device
0494  *  being detached while an operation is in progress.  The down_write()
0495  *  operation is only allowed while @mutex is held and is used when
0496  *  changing @attached and @detach_count and calling the low-level driver's
0497  *  "detach" handler.  The down_read() operation is generally used without
0498  *  holding @mutex.
0499  * @refcount: &struct kref reference counter for freeing COMEDI device.
0500  * @n_subdevices: Number of COMEDI subdevices allocated by the low-level
0501  *  driver for this device.
0502  * @subdevices: Dynamically allocated array of COMEDI subdevices.
0503  * @mmio: Optional pointer to a remapped MMIO region set by the low-level
0504  *  driver.
0505  * @iobase: Optional base of an I/O port region requested by the low-level
0506  *  driver.
0507  * @iolen: Length of I/O port region requested at @iobase.
0508  * @irq: Optional IRQ number requested by the low-level driver.
0509  * @read_subdev: Optional pointer to a default COMEDI subdevice operated on by
0510  *  the read() file operation.  Set by the low-level driver.
0511  * @write_subdev: Optional pointer to a default COMEDI subdevice operated on by
0512  *  the write() file operation.  Set by the low-level driver.
0513  * @async_queue: Storage for fasync_helper().
0514  * @open: Optional pointer to a function set by the low-level driver to be
0515  *  called when @use_count changes from 0 to 1.
0516  * @close: Optional pointer to a function set by the low-level driver to be
0517  *  called when @use_count changed from 1 to 0.
0518  * @insn_device_config: Optional pointer to a handler for all sub-instructions
0519  *  except %INSN_DEVICE_CONFIG_GET_ROUTES of the %INSN_DEVICE_CONFIG
0520  *  instruction.  If this is not initialized by the low-level driver, a
0521  *  default handler will be set during post-configuration.
0522  * @get_valid_routes: Optional pointer to a handler for the
0523  *  %INSN_DEVICE_CONFIG_GET_ROUTES sub-instruction of the
0524  *  %INSN_DEVICE_CONFIG instruction set.  If this is not initialized by the
0525  *  low-level driver, a default handler that copies zero routes back to the
0526  *  user will be used.
0527  *
0528  * This is the main control data structure for a COMEDI device (as far as the
0529  * COMEDI core is concerned).  There are two groups of COMEDI devices -
0530  * "legacy" devices that are configured by the handler for the
0531  * %COMEDI_DEVCONFIG ioctl, and automatically configured devices resulting
0532  * from a call to comedi_auto_config() as a result of a bus driver probe in
0533  * a low-level COMEDI driver.  The "legacy" COMEDI devices are allocated
0534  * during module initialization if the "comedi_num_legacy_minors" module
0535  * parameter is non-zero and use minor device numbers from 0 to
0536  * comedi_num_legacy_minors minus one.  The automatically configured COMEDI
0537  * devices are allocated on demand and use minor device numbers from
0538  * comedi_num_legacy_minors to 47.
0539  */
0540 struct comedi_device {
0541     int use_count;
0542     struct comedi_driver *driver;
0543     struct comedi_8254 *pacer;
0544     void *private;
0545 
0546     struct device *class_dev;
0547     int minor;
0548     unsigned int detach_count;
0549     struct device *hw_dev;
0550 
0551     const char *board_name;
0552     const void *board_ptr;
0553     unsigned int attached:1;
0554     unsigned int ioenabled:1;
0555     spinlock_t spinlock;    /* generic spin-lock for low-level driver */
0556     struct mutex mutex; /* generic mutex for COMEDI core */
0557     struct rw_semaphore attach_lock;
0558     struct kref refcount;
0559 
0560     int n_subdevices;
0561     struct comedi_subdevice *subdevices;
0562 
0563     /* dumb */
0564     void __iomem *mmio;
0565     unsigned long iobase;
0566     unsigned long iolen;
0567     unsigned int irq;
0568 
0569     struct comedi_subdevice *read_subdev;
0570     struct comedi_subdevice *write_subdev;
0571 
0572     struct fasync_struct *async_queue;
0573 
0574     int (*open)(struct comedi_device *dev);
0575     void (*close)(struct comedi_device *dev);
0576     int (*insn_device_config)(struct comedi_device *dev,
0577                   struct comedi_insn *insn, unsigned int *data);
0578     unsigned int (*get_valid_routes)(struct comedi_device *dev,
0579                      unsigned int n_pairs,
0580                      unsigned int *pair_data);
0581 };
0582 
0583 /*
0584  * function prototypes
0585  */
0586 
0587 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
0588 
0589 struct comedi_device *comedi_dev_get_from_minor(unsigned int minor);
0590 int comedi_dev_put(struct comedi_device *dev);
0591 
0592 bool comedi_is_subdevice_running(struct comedi_subdevice *s);
0593 
0594 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size);
0595 void comedi_set_spriv_auto_free(struct comedi_subdevice *s);
0596 
0597 int comedi_check_chanlist(struct comedi_subdevice *s,
0598               int n,
0599               unsigned int *chanlist);
0600 
0601 /* range stuff */
0602 
0603 #define RANGE(a, b)     {(a) * 1e6, (b) * 1e6, 0}
0604 #define RANGE_ext(a, b)     {(a) * 1e6, (b) * 1e6, RF_EXTERNAL}
0605 #define RANGE_mA(a, b)      {(a) * 1e6, (b) * 1e6, UNIT_mA}
0606 #define RANGE_unitless(a, b)    {(a) * 1e6, (b) * 1e6, 0}
0607 #define BIP_RANGE(a)        {-(a) * 1e6, (a) * 1e6, 0}
0608 #define UNI_RANGE(a)        {0, (a) * 1e6, 0}
0609 
0610 extern const struct comedi_lrange range_bipolar10;
0611 extern const struct comedi_lrange range_bipolar5;
0612 extern const struct comedi_lrange range_bipolar2_5;
0613 extern const struct comedi_lrange range_unipolar10;
0614 extern const struct comedi_lrange range_unipolar5;
0615 extern const struct comedi_lrange range_unipolar2_5;
0616 extern const struct comedi_lrange range_0_20mA;
0617 extern const struct comedi_lrange range_4_20mA;
0618 extern const struct comedi_lrange range_0_32mA;
0619 extern const struct comedi_lrange range_unknown;
0620 
0621 #define range_digital       range_unipolar5
0622 
0623 /**
0624  * struct comedi_lrange - Describes a COMEDI range table
0625  * @length: Number of entries in the range table.
0626  * @range: Array of &struct comedi_krange, one for each range.
0627  *
0628  * Each element of @range[] describes the minimum and maximum physical range
0629  * and the type of units.  Typically, the type of unit is %UNIT_volt
0630  * (i.e. volts) and the minimum and maximum are in millionths of a volt.
0631  * There may also be a flag that indicates the minimum and maximum are merely
0632  * scale factors for an unknown, external reference.
0633  */
0634 struct comedi_lrange {
0635     int length;
0636     struct comedi_krange range[];
0637 };
0638 
0639 /**
0640  * comedi_range_is_bipolar() - Test if subdevice range is bipolar
0641  * @s: COMEDI subdevice.
0642  * @range: Index of range within a range table.
0643  *
0644  * Tests whether a range is bipolar by checking whether its minimum value
0645  * is negative.
0646  *
0647  * Assumes @range is valid.  Does not work for subdevices using a
0648  * channel-specific range table list.
0649  *
0650  * Return:
0651  *  %true if the range is bipolar.
0652  *  %false if the range is unipolar.
0653  */
0654 static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s,
0655                        unsigned int range)
0656 {
0657     return s->range_table->range[range].min < 0;
0658 }
0659 
0660 /**
0661  * comedi_range_is_unipolar() - Test if subdevice range is unipolar
0662  * @s: COMEDI subdevice.
0663  * @range: Index of range within a range table.
0664  *
0665  * Tests whether a range is unipolar by checking whether its minimum value
0666  * is at least 0.
0667  *
0668  * Assumes @range is valid.  Does not work for subdevices using a
0669  * channel-specific range table list.
0670  *
0671  * Return:
0672  *  %true if the range is unipolar.
0673  *  %false if the range is bipolar.
0674  */
0675 static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
0676                         unsigned int range)
0677 {
0678     return s->range_table->range[range].min >= 0;
0679 }
0680 
0681 /**
0682  * comedi_range_is_external() - Test if subdevice range is external
0683  * @s: COMEDI subdevice.
0684  * @range: Index of range within a range table.
0685  *
0686  * Tests whether a range is externally reference by checking whether its
0687  * %RF_EXTERNAL flag is set.
0688  *
0689  * Assumes @range is valid.  Does not work for subdevices using a
0690  * channel-specific range table list.
0691  *
0692  * Return:
0693  *  %true if the range is external.
0694  *  %false if the range is internal.
0695  */
0696 static inline bool comedi_range_is_external(struct comedi_subdevice *s,
0697                         unsigned int range)
0698 {
0699     return !!(s->range_table->range[range].flags & RF_EXTERNAL);
0700 }
0701 
0702 /**
0703  * comedi_chan_range_is_bipolar() - Test if channel-specific range is bipolar
0704  * @s: COMEDI subdevice.
0705  * @chan: The channel number.
0706  * @range: Index of range within a range table.
0707  *
0708  * Tests whether a range is bipolar by checking whether its minimum value
0709  * is negative.
0710  *
0711  * Assumes @chan and @range are valid.  Only works for subdevices with a
0712  * channel-specific range table list.
0713  *
0714  * Return:
0715  *  %true if the range is bipolar.
0716  *  %false if the range is unipolar.
0717  */
0718 static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s,
0719                         unsigned int chan,
0720                         unsigned int range)
0721 {
0722     return s->range_table_list[chan]->range[range].min < 0;
0723 }
0724 
0725 /**
0726  * comedi_chan_range_is_unipolar() - Test if channel-specific range is unipolar
0727  * @s: COMEDI subdevice.
0728  * @chan: The channel number.
0729  * @range: Index of range within a range table.
0730  *
0731  * Tests whether a range is unipolar by checking whether its minimum value
0732  * is at least 0.
0733  *
0734  * Assumes @chan and @range are valid.  Only works for subdevices with a
0735  * channel-specific range table list.
0736  *
0737  * Return:
0738  *  %true if the range is unipolar.
0739  *  %false if the range is bipolar.
0740  */
0741 static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s,
0742                          unsigned int chan,
0743                          unsigned int range)
0744 {
0745     return s->range_table_list[chan]->range[range].min >= 0;
0746 }
0747 
0748 /**
0749  * comedi_chan_range_is_external() - Test if channel-specific range is external
0750  * @s: COMEDI subdevice.
0751  * @chan: The channel number.
0752  * @range: Index of range within a range table.
0753  *
0754  * Tests whether a range is externally reference by checking whether its
0755  * %RF_EXTERNAL flag is set.
0756  *
0757  * Assumes @chan and @range are valid.  Only works for subdevices with a
0758  * channel-specific range table list.
0759  *
0760  * Return:
0761  *  %true if the range is bipolar.
0762  *  %false if the range is unipolar.
0763  */
0764 static inline bool comedi_chan_range_is_external(struct comedi_subdevice *s,
0765                          unsigned int chan,
0766                          unsigned int range)
0767 {
0768     return !!(s->range_table_list[chan]->range[range].flags & RF_EXTERNAL);
0769 }
0770 
0771 /**
0772  * comedi_offset_munge() - Convert between offset binary and 2's complement
0773  * @s: COMEDI subdevice.
0774  * @val: Value to be converted.
0775  *
0776  * Toggles the highest bit of a sample value to toggle between offset binary
0777  * and 2's complement.  Assumes that @s->maxdata is a power of 2 minus 1.
0778  *
0779  * Return: The converted value.
0780  */
0781 static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
0782                            unsigned int val)
0783 {
0784     return val ^ s->maxdata ^ (s->maxdata >> 1);
0785 }
0786 
0787 /**
0788  * comedi_bytes_per_sample() - Determine subdevice sample size
0789  * @s: COMEDI subdevice.
0790  *
0791  * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on
0792  * whether the %SDF_LSAMPL subdevice flag is set or not.
0793  *
0794  * Return: The subdevice sample size.
0795  */
0796 static inline unsigned int comedi_bytes_per_sample(struct comedi_subdevice *s)
0797 {
0798     return s->subdev_flags & SDF_LSAMPL ? sizeof(int) : sizeof(short);
0799 }
0800 
0801 /**
0802  * comedi_sample_shift() - Determine log2 of subdevice sample size
0803  * @s: COMEDI subdevice.
0804  *
0805  * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on
0806  * whether the %SDF_LSAMPL subdevice flag is set or not.  The log2 of the
0807  * sample size will be 2 or 1 and can be used as the right operand of a
0808  * bit-shift operator to multiply or divide something by the sample size.
0809  *
0810  * Return: log2 of the subdevice sample size.
0811  */
0812 static inline unsigned int comedi_sample_shift(struct comedi_subdevice *s)
0813 {
0814     return s->subdev_flags & SDF_LSAMPL ? 2 : 1;
0815 }
0816 
0817 /**
0818  * comedi_bytes_to_samples() - Convert a number of bytes to a number of samples
0819  * @s: COMEDI subdevice.
0820  * @nbytes: Number of bytes
0821  *
0822  * Return: The number of bytes divided by the subdevice sample size.
0823  */
0824 static inline unsigned int comedi_bytes_to_samples(struct comedi_subdevice *s,
0825                            unsigned int nbytes)
0826 {
0827     return nbytes >> comedi_sample_shift(s);
0828 }
0829 
0830 /**
0831  * comedi_samples_to_bytes() - Convert a number of samples to a number of bytes
0832  * @s: COMEDI subdevice.
0833  * @nsamples: Number of samples.
0834  *
0835  * Return: The number of samples multiplied by the subdevice sample size.
0836  * (Does not check for arithmetic overflow.)
0837  */
0838 static inline unsigned int comedi_samples_to_bytes(struct comedi_subdevice *s,
0839                            unsigned int nsamples)
0840 {
0841     return nsamples << comedi_sample_shift(s);
0842 }
0843 
0844 /**
0845  * comedi_check_trigger_src() - Trivially validate a comedi_cmd trigger source
0846  * @src: Pointer to the trigger source to validate.
0847  * @flags: Bitmask of valid %TRIG_* for the trigger.
0848  *
0849  * This is used in "step 1" of the do_cmdtest functions of comedi drivers
0850  * to validate the comedi_cmd triggers. The mask of the @src against the
0851  * @flags allows the userspace comedilib to pass all the comedi_cmd
0852  * triggers as %TRIG_ANY and get back a bitmask of the valid trigger sources.
0853  *
0854  * Return:
0855  *  0 if trigger sources in *@src are all supported.
0856  *  -EINVAL if any trigger source in *@src is unsupported.
0857  */
0858 static inline int comedi_check_trigger_src(unsigned int *src,
0859                        unsigned int flags)
0860 {
0861     unsigned int orig_src = *src;
0862 
0863     *src = orig_src & flags;
0864     if (*src == TRIG_INVALID || *src != orig_src)
0865         return -EINVAL;
0866     return 0;
0867 }
0868 
0869 /**
0870  * comedi_check_trigger_is_unique() - Make sure a trigger source is unique
0871  * @src: The trigger source to check.
0872  *
0873  * Return:
0874  *  0 if no more than one trigger source is set.
0875  *  -EINVAL if more than one trigger source is set.
0876  */
0877 static inline int comedi_check_trigger_is_unique(unsigned int src)
0878 {
0879     /* this test is true if more than one _src bit is set */
0880     if ((src & (src - 1)) != 0)
0881         return -EINVAL;
0882     return 0;
0883 }
0884 
0885 /**
0886  * comedi_check_trigger_arg_is() - Trivially validate a trigger argument
0887  * @arg: Pointer to the trigger arg to validate.
0888  * @val: The value the argument should be.
0889  *
0890  * Forces *@arg to be @val.
0891  *
0892  * Return:
0893  *  0 if *@arg was already @val.
0894  *  -EINVAL if *@arg differed from @val.
0895  */
0896 static inline int comedi_check_trigger_arg_is(unsigned int *arg,
0897                           unsigned int val)
0898 {
0899     if (*arg != val) {
0900         *arg = val;
0901         return -EINVAL;
0902     }
0903     return 0;
0904 }
0905 
0906 /**
0907  * comedi_check_trigger_arg_min() - Trivially validate a trigger argument min
0908  * @arg: Pointer to the trigger arg to validate.
0909  * @val: The minimum value the argument should be.
0910  *
0911  * Forces *@arg to be at least @val, setting it to @val if necessary.
0912  *
0913  * Return:
0914  *  0 if *@arg was already at least @val.
0915  *  -EINVAL if *@arg was less than @val.
0916  */
0917 static inline int comedi_check_trigger_arg_min(unsigned int *arg,
0918                            unsigned int val)
0919 {
0920     if (*arg < val) {
0921         *arg = val;
0922         return -EINVAL;
0923     }
0924     return 0;
0925 }
0926 
0927 /**
0928  * comedi_check_trigger_arg_max() - Trivially validate a trigger argument max
0929  * @arg: Pointer to the trigger arg to validate.
0930  * @val: The maximum value the argument should be.
0931  *
0932  * Forces *@arg to be no more than @val, setting it to @val if necessary.
0933  *
0934  * Return:
0935  *  0 if*@arg was already no more than @val.
0936  *  -EINVAL if *@arg was greater than @val.
0937  */
0938 static inline int comedi_check_trigger_arg_max(unsigned int *arg,
0939                            unsigned int val)
0940 {
0941     if (*arg > val) {
0942         *arg = val;
0943         return -EINVAL;
0944     }
0945     return 0;
0946 }
0947 
0948 /*
0949  * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
0950  * Also useful for retrieving a previously configured hardware device of
0951  * known bus type.  Set automatically for auto-configured devices.
0952  * Automatically set to NULL when detaching hardware device.
0953  */
0954 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
0955 
0956 /**
0957  * comedi_buf_n_bytes_ready - Determine amount of unread data in buffer
0958  * @s: COMEDI subdevice.
0959  *
0960  * Determines the number of bytes of unread data in the asynchronous
0961  * acquisition data buffer for a subdevice.  The data in question might not
0962  * have been fully "munged" yet.
0963  *
0964  * Returns: The amount of unread data in bytes.
0965  */
0966 static inline unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s)
0967 {
0968     return s->async->buf_write_count - s->async->buf_read_count;
0969 }
0970 
0971 unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n);
0972 unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n);
0973 
0974 unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s);
0975 unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s, unsigned int n);
0976 unsigned int comedi_buf_read_free(struct comedi_subdevice *s, unsigned int n);
0977 
0978 unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
0979                       const void *data, unsigned int nsamples);
0980 unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
0981                      void *data, unsigned int nsamples);
0982 
0983 /* drivers.c - general comedi driver functions */
0984 
0985 #define COMEDI_TIMEOUT_MS   1000
0986 
0987 int comedi_timeout(struct comedi_device *dev, struct comedi_subdevice *s,
0988            struct comedi_insn *insn,
0989            int (*cb)(struct comedi_device *dev,
0990                  struct comedi_subdevice *s,
0991                  struct comedi_insn *insn, unsigned long context),
0992            unsigned long context);
0993 
0994 unsigned int comedi_handle_events(struct comedi_device *dev,
0995                   struct comedi_subdevice *s);
0996 
0997 int comedi_dio_insn_config(struct comedi_device *dev,
0998                struct comedi_subdevice *s,
0999                struct comedi_insn *insn, unsigned int *data,
1000                unsigned int mask);
1001 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
1002                      unsigned int *data);
1003 unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
1004                        struct comedi_cmd *cmd);
1005 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s);
1006 unsigned int comedi_nscans_left(struct comedi_subdevice *s,
1007                 unsigned int nscans);
1008 unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
1009                   unsigned int nsamples);
1010 void comedi_inc_scan_progress(struct comedi_subdevice *s,
1011                   unsigned int num_bytes);
1012 
1013 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size);
1014 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices);
1015 int comedi_alloc_subdev_readback(struct comedi_subdevice *s);
1016 
1017 int comedi_readback_insn_read(struct comedi_device *dev,
1018                   struct comedi_subdevice *s,
1019                   struct comedi_insn *insn, unsigned int *data);
1020 
1021 int comedi_load_firmware(struct comedi_device *dev, struct device *hw_dev,
1022              const char *name,
1023              int (*cb)(struct comedi_device *dev,
1024                    const u8 *data, size_t size,
1025                    unsigned long context),
1026              unsigned long context);
1027 
1028 int __comedi_request_region(struct comedi_device *dev,
1029                 unsigned long start, unsigned long len);
1030 int comedi_request_region(struct comedi_device *dev,
1031               unsigned long start, unsigned long len);
1032 void comedi_legacy_detach(struct comedi_device *dev);
1033 
1034 int comedi_auto_config(struct device *hardware_device,
1035                struct comedi_driver *driver, unsigned long context);
1036 void comedi_auto_unconfig(struct device *hardware_device);
1037 
1038 int comedi_driver_register(struct comedi_driver *driver);
1039 void comedi_driver_unregister(struct comedi_driver *driver);
1040 
1041 /**
1042  * module_comedi_driver() - Helper macro for registering a comedi driver
1043  * @__comedi_driver: comedi_driver struct
1044  *
1045  * Helper macro for comedi drivers which do not do anything special in module
1046  * init/exit. This eliminates a lot of boilerplate. Each module may only use
1047  * this macro once, and calling it replaces module_init() and module_exit().
1048  */
1049 #define module_comedi_driver(__comedi_driver) \
1050     module_driver(__comedi_driver, comedi_driver_register, \
1051             comedi_driver_unregister)
1052 
1053 #endif /* _COMEDIDEV_H */