Generic Counter Interface¶
Introduction¶
Counter devices are prevalent within a diverse spectrum of industries. The ubiquitous presence of these devices necessitates a common interface and standard of interaction and exposure. This driver API attempts to resolve the issue of duplicate code found among existing counter device drivers by introducing a generic counter interface for consumption. The Generic Counter interface enables drivers to support and expose a common set of components and functionality present in counter devices.
Theory¶
Counter devices can vary greatly in design, but regardless of whether some devices are quadrature encoder counters or tally counters, all counter devices consist of a core set of components. This core set of components, shared by all counter devices, is what forms the essence of the Generic Counter interface.
There are three core components to a counter:
Count: Count data for a set of Signals.
Signal: Input data that is evaluated by the counter to determine the count data.
Synapse: The association of a Signal with a respective Count.
COUNT¶
A Count represents the count data for a set of Signals. The Generic Counter interface provides the following available count data types:
COUNT_POSITION: Unsigned integer value representing position.
A Count has a count function mode which represents the update behavior for the count data. The Generic Counter interface provides the following available count function modes:
Increase: Accumulated count is incremented.
Decrease: Accumulated count is decremented.
Pulse-Direction: Rising edges on signal A updates the respective count. The input level of signal B determines direction.
Quadrature: A pair of quadrature encoding signals are evaluated to determine position and direction. The following Quadrature modes are available:
x1 A: If direction is forward, rising edges on quadrature pair signal A updates the respective count; if the direction is backward, falling edges on quadrature pair signal A updates the respective count. Quadrature encoding determines the direction.
x1 B: If direction is forward, rising edges on quadrature pair signal B updates the respective count; if the direction is backward, falling edges on quadrature pair signal B updates the respective count. Quadrature encoding determines the direction.
x2 A: Any state transition on quadrature pair signal A updates the respective count. Quadrature encoding determines the direction.
x2 B: Any state transition on quadrature pair signal B updates the respective count. Quadrature encoding determines the direction.
x4: Any state transition on either quadrature pair signals updates the respective count. Quadrature encoding determines the direction.
A Count has a set of one or more associated Signals.
SIGNAL¶
A Signal represents a counter input data; this is the input data that is evaluated by the counter to determine the count data; e.g. a quadrature signal output line of a rotary encoder. Not all counter devices provide user access to the Signal data.
The Generic Counter interface provides the following available signal data types for when the Signal data is available for user access:
SIGNAL_LEVEL: Signal line state level. The following states are possible:
SIGNAL_LEVEL_LOW: Signal line is in a low state.
SIGNAL_LEVEL_HIGH: Signal line is in a high state.
A Signal may be associated with one or more Counts.
SYNAPSE¶
A Synapse represents the association of a Signal with a respective Count. Signal data affects respective Count data, and the Synapse represents this relationship.
The Synapse action mode specifies the Signal data condition which triggers the respective Count's count function evaluation to update the count data. The Generic Counter interface provides the following available action modes:
None: Signal does not trigger the count function. In Pulse-Direction count function mode, this Signal is evaluated as Direction.
Rising Edge: Low state transitions to high state.
Falling Edge: High state transitions to low state.
Both Edges: Any state transition.
A counter is defined as a set of input signals associated with count data that are generated by the evaluation of the state of the associated input signals as defined by the respective count functions. Within the context of the Generic Counter interface, a counter consists of Counts each associated with a set of Signals, whose respective Synapse instances represent the count function update conditions for the associated Counts.
Paradigm¶
The most basic counter device may be expressed as a single Count associated with a single Signal via a single Synapse. Take for example a counter device which simply accumulates a count of rising edges on a source input line:
Count Synapse Signal
----- ------- ------
+---------------------+
| Data: Count | Rising Edge ________
| Function: Increase | <------------- / Source \
| | ____________
+---------------------+
In this example, the Signal is a source input line with a pulsing voltage, while the Count is a persistent count value which is repeatedly incremented. The Signal is associated with the respective Count via a Synapse. The increase function is triggered by the Signal data condition specified by the Synapse -- in this case a rising edge condition on the voltage input line. In summary, the counter device existence and behavior is aptly represented by respective Count, Signal, and Synapse components: a rising edge condition triggers an increase function on an accumulating count datum.
A counter device is not limited to a single Signal; in fact, in theory many Signals may be associated with even a single Count. For example, a quadrature encoder counter device can keep track of position based on the states of two input lines:
Count Synapse Signal
----- ------- ------
+-------------------------+
| Data: Position | Both Edges ___
| Function: Quadrature x4 | <------------ / A \
| | _______
| |
| | Both Edges ___
| | <------------ / B \
| | _______
+-------------------------+
In this example, two Signals (quadrature encoder lines A and B) are associated with a single Count: a rising or falling edge on either A or B triggers the "Quadrature x4" function which determines the direction of movement and updates the respective position data. The "Quadrature x4" function is likely implemented in the hardware of the quadrature encoder counter device; the Count, Signals, and Synapses simply represent this hardware behavior and functionality.
Signals associated with the same Count can have differing Synapse action mode conditions. For example, a quadrature encoder counter device operating in a non-quadrature Pulse-Direction mode could have one input line dedicated for movement and a second input line dedicated for direction:
Count Synapse Signal
----- ------- ------
+---------------------------+
| Data: Position | Rising Edge ___
| Function: Pulse-Direction | <------------- / A \ (Movement)
| | _______
| |
| | None ___
| | <------------- / B \ (Direction)
| | _______
+---------------------------+
Only Signal A triggers the "Pulse-Direction" update function, but the instantaneous state of Signal B is still required in order to know the direction so that the position data may be properly updated. Ultimately, both Signals are associated with the same Count via two respective Synapses, but only one Synapse has an active action mode condition which triggers the respective count function while the other is left with a "None" condition action mode to indicate its respective Signal's availability for state evaluation despite its non-triggering mode.
Keep in mind that the Signal, Synapse, and Count are abstract representations which do not need to be closely married to their respective physical sources. This allows the user of a counter to divorce themselves from the nuances of physical components (such as whether an input line is differential or single-ended) and instead focus on the core idea of what the data and process represent (e.g. position as interpreted from quadrature encoding data).
Userspace Interface¶
Several sysfs attributes are generated by the Generic Counter interface, and reside under the /sys/bus/counter/devices/counterX directory, where counterX refers to the respective counter device. Please see Documentation/ABI/testing/sysfs-bus-counter for detailed information on each Generic Counter interface sysfs attribute.
Through these sysfs attributes, programs and scripts may interact with the Generic Counter paradigm Counts, Signals, and Synapses of respective counter devices.
Driver API¶
Driver authors may utilize the Generic Counter interface in their code by including the include/linux/counter.h header file. This header file provides several core data structures, function prototypes, and macros for defining a counter device.
-
struct
counter_signal_ext
¶ Counter Signal extensions
Definition
struct counter_signal_ext {
const char *name;
ssize_t (*read)(struct counter_device *counter, struct counter_signal *signal, void *priv, char *buf);
ssize_t (*write)(struct counter_device *counter,struct counter_signal *signal, void *priv, const char *buf, size_t len);
void *priv;
};
Members
name
attribute name
read
read callback for this attribute; may be NULL
write
write callback for this attribute; may be NULL
priv
data private to the driver
-
struct
counter_signal
¶ Counter Signal node
Definition
struct counter_signal {
int id;
const char *name;
const struct counter_signal_ext *ext;
size_t num_ext;
void *priv;
};
Members
id
unique ID used to identify signal
name
device-specific Signal name; ideally, this should match the name as it appears in the datasheet documentation
ext
optional array of Counter Signal extensions
num_ext
number of Counter Signal extensions specified in ext
priv
optional private data supplied by driver
-
struct
counter_signal_enum_ext
¶ Signal enum extension attribute
Definition
struct counter_signal_enum_ext {
const char * const *items;
size_t num_items;
int (*get)(struct counter_device *counter, struct counter_signal *signal, size_t *item);
int (*set)(struct counter_device *counter, struct counter_signal *signal, size_t item);
};
Members
items
Array of strings
num_items
Number of items specified in items
get
Get callback function; may be NULL
set
Set callback function; may be NULL
Description
The counter_signal_enum_ext structure can be used to implement enum style Signal extension attributes. Enum style attributes are those which have a set of strings that map to unsigned integer values. The Generic Counter Signal enum extension helper code takes care of mapping between value and string, as well as generating a "_available" file which contains a list of all available items. The get callback is used to query the currently active item; the index of the item within the respective items array is returned via the 'item' parameter. The set callback is called when the attribute is updated; the 'item' parameter contains the index of the newly activated item within the respective items array.
-
COUNTER_SIGNAL_ENUM
(_name, _e)¶ Initialize Signal enum extension
Parameters
_name
Attribute name
_e
Pointer to a counter_signal_enum_ext structure
Description
This should usually be used together with COUNTER_SIGNAL_ENUM_AVAILABLE()
-
COUNTER_SIGNAL_ENUM_AVAILABLE
(_name, _e)¶ Initialize Signal enum available extension
Parameters
_name
Attribute name ("_available" will be appended to the name)
_e
Pointer to a counter_signal_enum_ext structure
Description
Creates a read only attribute that lists all the available enum items in a
newline separated list. This should usually be used together with
COUNTER_SIGNAL_ENUM()
-
struct
counter_synapse
¶ Counter Synapse node
Definition
struct counter_synapse {
size_t action;
const enum counter_synapse_action *actions_list;
size_t num_actions;
struct counter_signal *signal;
};
Members
action
index of current action mode
actions_list
array of available action modes
num_actions
number of action modes specified in actions_list
signal
pointer to associated signal
-
struct
counter_count_ext
¶ Counter Count extension
Definition
struct counter_count_ext {
const char *name;
ssize_t (*read)(struct counter_device *counter, struct counter_count *count, void *priv, char *buf);
ssize_t (*write)(struct counter_device *counter,struct counter_count *count, void *priv, const char *buf, size_t len);
void *priv;
};
Members
name
attribute name
read
read callback for this attribute; may be NULL
write
write callback for this attribute; may be NULL
priv
data private to the driver
-
struct
counter_count
¶ Counter Count node
Definition
struct counter_count {
int id;
const char *name;
size_t function;
const enum counter_count_function *functions_list;
size_t num_functions;
struct counter_synapse *synapses;
size_t num_synapses;
const struct counter_count_ext *ext;
size_t num_ext;
void *priv;
};
Members
id
unique ID used to identify Count
name
device-specific Count name; ideally, this should match the name as it appears in the datasheet documentation
function
index of current function mode
functions_list
array available function modes
num_functions
number of function modes specified in functions_list
synapses
array of synapses for initialization
num_synapses
number of synapses specified in synapses
ext
optional array of Counter Count extensions
num_ext
number of Counter Count extensions specified in ext
priv
optional private data supplied by driver
-
struct
counter_count_enum_ext
¶ Count enum extension attribute
Definition
struct counter_count_enum_ext {
const char * const *items;
size_t num_items;
int (*get)(struct counter_device *counter, struct counter_count *count, size_t *item);
int (*set)(struct counter_device *counter, struct counter_count *count, size_t item);
};
Members
items
Array of strings
num_items
Number of items specified in items
get
Get callback function; may be NULL
set
Set callback function; may be NULL
Description
The counter_count_enum_ext structure can be used to implement enum style Count extension attributes. Enum style attributes are those which have a set of strings that map to unsigned integer values. The Generic Counter Count enum extension helper code takes care of mapping between value and string, as well as generating a "_available" file which contains a list of all available items. The get callback is used to query the currently active item; the index of the item within the respective items array is returned via the 'item' parameter. The set callback is called when the attribute is updated; the 'item' parameter contains the index of the newly activated item within the respective items array.
-
COUNTER_COUNT_ENUM
(_name, _e)¶ Initialize Count enum extension
Parameters
_name
Attribute name
_e
Pointer to a counter_count_enum_ext structure
Description
This should usually be used together with COUNTER_COUNT_ENUM_AVAILABLE()
-
COUNTER_COUNT_ENUM_AVAILABLE
(_name, _e)¶ Initialize Count enum available extension
Parameters
_name
Attribute name ("_available" will be appended to the name)
_e
Pointer to a counter_count_enum_ext structure
Description
Creates a read only attribute that lists all the available enum items in a
newline separated list. This should usually be used together with
COUNTER_COUNT_ENUM()
-
struct
counter_device_attr_group
¶ internal container for attribute group
Definition
struct counter_device_attr_group {
struct attribute_group attr_group;
struct list_head attr_list;
size_t num_attr;
};
Members
attr_group
Counter sysfs attributes group
attr_list
list to keep track of created Counter sysfs attributes
num_attr
number of Counter sysfs attributes
-
struct
counter_device_state
¶ internal state container for a Counter device
Definition
struct counter_device_state {
int id;
struct device dev;
struct counter_device_attr_group *groups_list;
size_t num_groups;
const struct attribute_group **groups;
};
Members
id
unique ID used to identify the Counter
dev
internal device structure
groups_list
attribute groups list (for Signals, Counts, and ext)
num_groups
number of attribute groups containers
groups
Counter sysfs attribute groups (to populate dev.groups)
-
struct
counter_signal_read_value
¶ Opaque Signal read value
Definition
struct counter_signal_read_value {
char *buf;
size_t len;
};
Members
buf
string representation of Signal read value
len
length of string in buf
-
struct
counter_count_read_value
¶ Opaque Count read value
Definition
struct counter_count_read_value {
char *buf;
size_t len;
};
Members
buf
string representation of Count read value
len
length of string in buf
-
struct
counter_count_write_value
¶ Opaque Count write value
Definition
struct counter_count_write_value {
const char *buf;
};
Members
buf
string representation of Count write value
-
struct
counter_ops
¶ Callbacks from driver
Definition
struct counter_ops {
int (*signal_read)(struct counter_device *counter,struct counter_signal *signal, struct counter_signal_read_value *val);
int (*count_read)(struct counter_device *counter,struct counter_count *count, struct counter_count_read_value *val);
int (*count_write)(struct counter_device *counter,struct counter_count *count, struct counter_count_write_value *val);
int (*function_get)(struct counter_device *counter, struct counter_count *count, size_t *function);
int (*function_set)(struct counter_device *counter, struct counter_count *count, size_t function);
int (*action_get)(struct counter_device *counter,struct counter_count *count, struct counter_synapse *synapse, size_t *action);
int (*action_set)(struct counter_device *counter,struct counter_count *count, struct counter_synapse *synapse, size_t action);
};
Members
signal_read
optional read callback for Signal attribute. The read value of the respective Signal should be passed back via the val parameter. val points to an opaque type which should be set only by calling the counter_signal_read_value_set function from within the signal_read callback.
count_read
optional read callback for Count attribute. The read value of the respective Count should be passed back via the val parameter. val points to an opaque type which should be set only by calling the counter_count_read_value_set function from within the count_read callback.
count_write
optional write callback for Count attribute. The write value for the respective Count is passed in via the val parameter. val points to an opaque type which should be accessed only by calling the counter_count_write_value_get function.
function_get
function to get the current count function mode. Returns 0 on success and negative error code on error. The index of the respective Count's returned function mode should be passed back via the function parameter.
function_set
function to set the count function mode. function is the index of the requested function mode from the respective Count's functions_list array.
action_get
function to get the current action mode. Returns 0 on success and negative error code on error. The index of the respective Signal's returned action mode should be passed back via the action parameter.
action_set
function to set the action mode. action is the index of the requested action mode from the respective Synapse's actions_list array.
-
struct
counter_device_ext
¶ Counter device extension
Definition
struct counter_device_ext {
const char *name;
ssize_t (*read)(struct counter_device *counter, void *priv, char *buf);
ssize_t (*write)(struct counter_device *counter, void *priv, const char *buf, size_t len);
void *priv;
};
Members
name
attribute name
read
read callback for this attribute; may be NULL
write
write callback for this attribute; may be NULL
priv
data private to the driver
-
struct
counter_device_enum_ext
¶ Counter enum extension attribute
Definition
struct counter_device_enum_ext {
const char * const *items;
size_t num_items;
int (*get)(struct counter_device *counter, size_t *item);
int (*set)(struct counter_device *counter, size_t item);
};
Members
items
Array of strings
num_items
Number of items specified in items
get
Get callback function; may be NULL
set
Set callback function; may be NULL
Description
The counter_device_enum_ext structure can be used to implement enum style Counter extension attributes. Enum style attributes are those which have a set of strings that map to unsigned integer values. The Generic Counter enum extension helper code takes care of mapping between value and string, as well as generating a "_available" file which contains a list of all available items. The get callback is used to query the currently active item; the index of the item within the respective items array is returned via the 'item' parameter. The set callback is called when the attribute is updated; the 'item' parameter contains the index of the newly activated item within the respective items array.
-
COUNTER_DEVICE_ENUM
(_name, _e)¶ Initialize Counter enum extension
Parameters
_name
Attribute name
_e
Pointer to a counter_device_enum_ext structure
Description
This should usually be used together with COUNTER_DEVICE_ENUM_AVAILABLE()
-
COUNTER_DEVICE_ENUM_AVAILABLE
(_name, _e)¶ Initialize Counter enum available extension
Parameters
_name
Attribute name ("_available" will be appended to the name)
_e
Pointer to a counter_device_enum_ext structure
Description
Creates a read only attribute that lists all the available enum items in a
newline separated list. This should usually be used together with
COUNTER_DEVICE_ENUM()
-
struct
counter_device
¶ Counter data structure
Definition
struct counter_device {
const char *name;
struct device *parent;
struct counter_device_state *device_state;
const struct counter_ops *ops;
struct counter_signal *signals;
size_t num_signals;
struct counter_count *counts;
size_t num_counts;
const struct counter_device_ext *ext;
size_t num_ext;
void *priv;
};
Members
name
name of the device as it appears in the datasheet
parent
optional parent device providing the counters
device_state
internal device state container
ops
callbacks from driver
signals
array of Signals
num_signals
number of Signals specified in signals
counts
array of Counts
num_counts
number of Counts specified in counts
ext
optional array of Counter device extensions
num_ext
number of Counter device extensions specified in ext
priv
optional private data supplied by driver
-
void
counter_signal_read_value_set
(struct counter_signal_read_value *const val, const enum counter_signal_value_type type, void *const data)¶ set counter_signal_read_value data
Parameters
struct counter_signal_read_value *const val
counter_signal_read_value structure to set
const enum counter_signal_value_type type
property Signal data represents
void *const data
Signal data
Description
This function sets an opaque counter_signal_read_value structure with the provided Signal data.
-
void
counter_count_read_value_set
(struct counter_count_read_value *const val, const enum counter_count_value_type type, void *const data)¶ set counter_count_read_value data
Parameters
struct counter_count_read_value *const val
counter_count_read_value structure to set
const enum counter_count_value_type type
property Count data represents
void *const data
Count data
Description
This function sets an opaque counter_count_read_value structure with the provided Count data.
-
int
counter_count_write_value_get
(void *const data, const enum counter_count_value_type type, const struct counter_count_write_value *const val)¶ get counter_count_write_value data
Parameters
void *const data
Count data
const enum counter_count_value_type type
property Count data represents
const struct counter_count_write_value *const val
counter_count_write_value structure containing data
Description
This function extracts Count data from the provided opaque counter_count_write_value structure and stores it at the address provided by data.
Return
0 on success, negative error number on failure.
-
int
counter_register
(struct counter_device *const counter)¶ register Counter to the system
Parameters
struct counter_device *const counter
pointer to Counter to register
Description
This function registers a Counter to the system. A sysfs "counter" directory will be created and populated with sysfs attributes correlating with the Counter Signals, Synapses, and Counts respectively.
-
void
counter_unregister
(struct counter_device *const counter)¶ unregister Counter from the system
Parameters
struct counter_device *const counter
pointer to Counter to unregister
Description
The Counter is unregistered from the system; all allocated memory is freed.
-
int
devm_counter_register
(struct device * dev, struct counter_device *const counter)¶ Resource-managed counter_register
Parameters
struct device * dev
device to allocate counter_device for
struct counter_device *const counter
pointer to Counter to register
Description
Managed counter_register. The Counter registered with this function is automatically unregistered on driver detach. This function calls counter_register internally. Refer to that function for more information.
If an Counter registered with this function needs to be unregistered separately, devm_counter_unregister must be used.
Return
0 on success, negative error number on failure.
-
void
devm_counter_unregister
(struct device * dev, struct counter_device *const counter)¶ Resource-managed counter_unregister
Parameters
struct device * dev
device this counter_device belongs to
struct counter_device *const counter
pointer to Counter associated with the device
Description
Unregister Counter registered with devm_counter_register.
Implementation¶
To support a counter device, a driver must first allocate the available Counter Signals via counter_signal structures. These Signals should be stored as an array and set to the signals array member of an allocated counter_device structure before the Counter is registered to the system.
Counter Counts may be allocated via counter_count structures, and respective Counter Signal associations (Synapses) made via counter_synapse structures. Associated counter_synapse structures are stored as an array and set to the the synapses array member of the respective counter_count structure. These counter_count structures are set to the counts array member of an allocated counter_device structure before the Counter is registered to the system.
Driver callbacks should be provided to the counter_device structure via a constant counter_ops structure in order to communicate with the device: to read and write various Signals and Counts, and to set and get the "action mode" and "function mode" for various Synapses and Counts respectively.
A defined counter_device structure may be registered to the system by passing it to the counter_register function, and unregistered by passing it to the counter_unregister function. Similarly, the devm_counter_register and devm_counter_unregister functions may be used if device memory-managed registration is desired.
Extension sysfs attributes can be created for auxiliary functionality and data by passing in defined counter_device_ext, counter_count_ext, and counter_signal_ext structures. In these cases, the counter_device_ext structure is used for global configuration of the respective Counter device, while the counter_count_ext and counter_signal_ext structures allow for auxiliary exposure and configuration of a specific Count or Signal respectively.
Architecture¶
When the Generic Counter interface counter module is loaded, the counter_init function is called which registers a bus_type named "counter" to the system. Subsequently, when the module is unloaded, the counter_exit function is called which unregisters the bus_type named "counter" from the system.
Counter devices are registered to the system via the counter_register function, and later removed via the counter_unregister function. The counter_register function establishes a unique ID for the Counter device and creates a respective sysfs directory, where X is the mentioned unique ID:
/sys/bus/counter/devices/counterX
Sysfs attributes are created within the counterX directory to expose functionality, configurations, and data relating to the Counts, Signals, and Synapses of the Counter device, as well as options and information for the Counter device itself.
Each Signal has a directory created to house its relevant sysfs attributes, where Y is the unique ID of the respective Signal:
/sys/bus/counter/devices/counterX/signalY
Similarly, each Count has a directory created to house its relevant sysfs attributes, where Y is the unique ID of the respective Count:
/sys/bus/counter/devices/counterX/countY
For a more detailed breakdown of the available Generic Counter interface sysfs attributes, please refer to the Documentation/ABI/testing/sysfs-bus-counter file.
The Signals and Counts associated with the Counter device are registered to the system as well by the counter_register function. The signal_read/signal_write driver callbacks are associated with their respective Signal attributes, while the count_read/count_write and function_get/function_set driver callbacks are associated with their respective Count attributes; similarly, the same is true for the action_get/action_set driver callbacks and their respective Synapse attributes. If a driver callback is left undefined, then the respective read/write permission is left disabled for the relevant attributes.
Similarly, extension sysfs attributes are created for the defined counter_device_ext, counter_count_ext, and counter_signal_ext structures that are passed in.