gpio: cdev: add uAPI v2
Kent Gibson
reviewerv9 cover gpio: cdev: add uAPI v2
This patchset defines and implements a new version of the GPIO CDEV uAPI to address existing 32/64-bit alignment issues, add support for debounce, event sequence numbers, and allow for requested lines with different configurations.
It provides some future proofing by adding optional configuration fields and padding reserved for future use.
The series can be partitioned into three blocks; the first two patches are minor fixes that impact later patches, the next eleven contain the v2 uAPI definition and implementation, and the final seven port the GPIO tools to the v2 uAPI and extend them to use new uAPI features.
The more complicated patches include their own commentary where appropriate.
Cheers,
Kent.
Changes for v9:
- references to function names should include braces (patch 02)
- add time scale suffixes to timestamp (_ns) and debounce period (_us) variables (patch 04 and later)
- address multiple review comments (patch 04)
- drop mention of future API removal (patch 06)
- use static_assert() rather than BUILD_BUG_ON() (patch 07)
- change event buffer overflow behaviour to discard old events rather than recent events (patch 09)
- add spaces around '*' in '*16' (patch 09)
- reword comments regarding field access and locking (patch 09 and 12)
Changes for v8:
- fix BUILD_BUG_ON conditions and relocate them before the return in gpiolib_cdev_register() (patch 07)
Changes for v7:
- use _BITULL for ULL flag definitions (patch 04)
- add check on kmalloc_array return value in linereq_get_values() (patch 07) and linereq_set_values_unlocked() (patch 11)
- restore v1 functions used by gpio-mockup selftests (patch 17)
Changes for v6:
- flags variable in linereq_create() should be u64 not unsigned long (patch 07)
- remove restrictions on configuration changes - any change from one valid state to another valid state is allowed. (patches 09, 10, 12)
Changes for v5:
All changes for v5 fix issues with the gpiolib-cdev.c implementation, in patches 07-12.
The uAPI is unchanged from v4, as is the port of the tools.
- use IS_ALIGNED in BUILD_BUG_ON checks (patch 07)
- relocate BUILD_BUG_ON checks to gpiolib_cdev_register (patch 07)
- s/requies/requires/ (patch 07)
- use unsigned int for variables that are never negative
- change lineinfo_get() parameter from cmd to bool watch (patch 08)
- flagsv2 in gpio_v2_line_info_to_v1() should be u64, not int (patch 08)
- change "_locked" suffixed function names to "_unlocked" (patch 10 and
11)
- be less eager breaking long lines
- move commentary into checkin comment where appropriate - particularly patch 12
- restructure the request/line split - rename struct line to struct linereq, and struct edge_detector to struct line, and relocate the desc field from linereq to line. The linereq name was selected over line_request as function names such as linereq_set_values() are more clearly associated with requests than line_request_set_values(), particularly as there is also a struct line. And linereq is as informative as linerequest, so I went with the shortened form.
Changes for v4:
- bitmap width clarification in gpiod.h (patch 04)
- fix info offset initialisation bug (patch 08 and inserting patch 01)
- replace strncpy with strscpy to remove compiler warnings (patch 08 and inserting patch 02)
- fix mask handling in line_get_values (patch 07)
Changes for v3:
- disabling the character device from the build requires EXPERT
- uAPI revisions (see patch 02)
- replace padding_not_zeroed with calls to memchr_inv
- don't use bitops on 64-bit flags as that doesn't work on BE-32
- accept first attribute matching a line in gpio_v2_line_config.attrs rather than the last
- rework lsgpio port to uAPI v2 as flags reverted to v1 like layout (since patch v2)
- swapped patches 17 and 18 to apply debounce to multiple monitored lines
Changes for v2:
- split out cleanup patches into a separate series.
- split implementation patch into a patch for each ioctl or major feature.
- split tool port patch into a patch per tool.
- rework uAPI to allow requested lines with different configurations.
Kent Gibson (20):
gpiolib: cdev: gpio_desc_to_lineinfo() should set info offset
gpiolib: cdev: replace strncpy() with strscpy()
gpio: uapi: define GPIO_MAX_NAME_SIZE for array sizes
gpio: uapi: define uAPI v2
gpiolib: make cdev a build option
gpiolib: add build option for CDEV v1 ABI
gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and
GPIO_V2_LINE_GET_VALUES_IOCTL
gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and
GPIO_V2_GET_LINEINFO_WATCH_IOCTL
gpiolib: cdev: support edge detection for uAPI v2
gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
gpiolib: cdev: support setting debounce
gpio: uapi: document uAPI v1 as deprecated
tools: gpio: port lsgpio to v2 uAPI
tools: gpio: port gpio-watch to v2 uAPI
tools: gpio: rename nlines to num_lines
tools: gpio: port gpio-hammer to v2 uAPI
tools: gpio: port gpio-event-mon to v2 uAPI
tools: gpio: add multi-line monitoring to gpio-event-mon
tools: gpio: add debounce support to gpio-event-mon
drivers/gpio/Kconfig | 29 +- drivers/gpio/Makefile | 2 +- drivers/gpio/gpiolib-cdev.c | 1531 +++++++++++++++++++++++++++++++---- drivers/gpio/gpiolib-cdev.h | 15 + drivers/gpio/gpiolib.c | 5 + drivers/gpio/gpiolib.h | 6 + include/uapi/linux/gpio.h | 334 +++++++- tools/gpio/gpio-event-mon.c | 146 ++-- tools/gpio/gpio-hammer.c | 56 +- tools/gpio/gpio-utils.c | 176 +++- tools/gpio/gpio-utils.h | 48 +- tools/gpio/gpio-watch.c | 16 +- tools/gpio/lsgpio.c | 60 +- 13 files changed, 2098 insertions(+), 326 deletions(-)
base-commit: 36eccdb58fb55d2bea6a0e62932e4d7e5192d409
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 1/20 gpiolib: cdev: gpio_desc_to_lineinfo() should set info offset
Set the value of the line info offset in gpio_desc_to_lineinfo(), rather than relying on it being passed in the info. This makes the function behave as you would expect from the name - it generates the line info corresponding to a given GPIO desc.
---
There are some instances where this results in the offset being set when it is already set in the info, but this is clearer especially considering that, as part of the replacement of strncpy with strscpy and to ensure kernel stack cannot be leaked to userspace, the info is initially zeroed in a subsequent patch.
drivers/gpio/gpiolib-cdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib-cdev.c +2 -1
@@ -752,6 +752,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,bool ok_for_pinctrl;unsigned long flags; + info->line_offset =gpio_chip_hwgpio (desc); +/* * This function takes a mutex so we must check this before taking * the spinlock.
@@ -933,7 +935,6 @@ static int lineinfo_changed_notify(struct notifier_block *nb,return NOTIFY_DONE ;memset (&chg,0 ,sizeof (chg)); - chg.info.line_offset =gpio_chip_hwgpio (desc); chg.event_type = action; chg.timestamp =ktime_get_ns ();gpio_desc_to_lineinfo (desc, &chg.info);
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 2/20 gpiolib: cdev: replace strncpy() with strscpy()
In reply to Kent GibsonReplace usage of strncpy() with strscpy() to remove -Wstringop-truncation warnings.
The structures being populated are zeroed, to prevent stack leakage as they are returned to userspace, so strscpy() performs the equivalent function without the warnings.
---
The memset in gpio_desc_to_lineinfo(), in conjunction with the strscpy, is necessary as strncpy zero pads the remainder of the destination. It also guarantees that the info cannot leak kernel stack to userspace. This is useful here, but is even more important for the v2 info, that this function is changed to generate in a subsequent patch, as that struct contains padding and attribute arrays that also need to be initialised.
drivers/gpio/gpiolib-cdev.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
drivers/gpio/gpiolib-cdev.c +7 -16
@@ -752,6 +752,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,bool ok_for_pinctrl;unsigned long flags; +memset (info,0 ,sizeof (*info)); info->line_offset =gpio_chip_hwgpio (desc);/*
@@ -766,19 +767,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,spin_lock_irqsave (&gpio_lock, flags); -if (desc->name) { -strncpy (info->name, desc->name,sizeof (info->name)); - info->name[sizeof (info->name) -1 ] ='\0' ; - }else { - info->name[0 ] ='\0' ; - } +if (desc->name) +strscpy (info->name, desc->name,sizeof (info->name)); -if (desc->label) { -strncpy (info->consumer, desc->label,sizeof (info->consumer)); - info->consumer[sizeof (info->consumer) -1 ] ='\0' ; - }else { - info->consumer[0 ] ='\0' ; - } +if (desc->label) +strscpy (info->consumer, desc->label,sizeof (info->consumer));/* * Userspace only need to know that the kernel is using this GPIO so
@@ -842,12 +835,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)memset (&chipinfo,0 ,sizeof (chipinfo)); -strncpy (chipinfo.name,dev_name (&gdev->dev), +strscpy (chipinfo.name,dev_name (&gdev->dev),sizeof (chipinfo.name)); - chipinfo.name[sizeof (chipinfo.name)-1 ] ='\0' ; -strncpy (chipinfo.label, gdev->label, +strscpy (chipinfo.label, gdev->label,sizeof (chipinfo.label)); - chipinfo.label[sizeof (chipinfo.label)-1 ] ='\0' ; chipinfo.lines = gdev->ngpio;if (copy_to_user (ip, &chipinfo,sizeof (chipinfo)))return -EFAULT ;
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 3/20 gpio: uapi: define GPIO_MAX_NAME_SIZE for array sizes
In reply to Kent GibsonReplace constant array sizes with a macro constant to clarify the source of array sizes, provide a place to document any constraints on the size, and to simplify array sizing in userspace if constructing structs from their composite fields.
--- include/uapi/linux/gpio.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
include/uapi/linux/gpio.h +11 -6
@@ -14,6 +14,11 @@#include <linux/ioctl.h> #include <linux/types.h> +/* +* The maximum size of name and label arrays. +*/ +#define GPIO_MAX_NAME_SIZE 32 +/** * struct gpiochip_info - Information about a certain GPIO chip * @name: the Linux kernel name of this GPIO chip
@@ -22,8 +27,8 @@* @lines: number of GPIO lines on this chip */ struct gpiochip_info { -char name[32 ]; -char label[32 ]; +char name[GPIO_MAX_NAME_SIZE ]; +char label[GPIO_MAX_NAME_SIZE ];__u32 lines; };
@@ -52,8 +57,8 @@ struct gpiochip_info {struct gpioline_info {__u32 line_offset;__u32 flags; -char name[32 ]; -char consumer[32 ]; +char name[GPIO_MAX_NAME_SIZE ]; +char consumer[GPIO_MAX_NAME_SIZE ]; };/* Maximum number of requested handles */
@@ -123,7 +128,7 @@ struct gpiohandle_request {__u32 lineoffsets[GPIOHANDLES_MAX ];__u32 flags;__u8 default_values[GPIOHANDLES_MAX ]; -char consumer_label[32 ]; +char consumer_label[GPIO_MAX_NAME_SIZE ];__u32 lines;int fd; };
@@ -182,7 +187,7 @@ struct gpioevent_request {__u32 lineoffset;__u32 handleflags;__u32 eventflags; -char consumer_label[32 ]; +char consumer_label[GPIO_MAX_NAME_SIZE ];int fd; };
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 4/20 gpio: uapi: define uAPI v2
In reply to Kent GibsonAdd a new version of the uAPI to address existing 32/64-bit alignment issues, add support for debounce and event sequence numbers, allow requested lines with different configurations, and provide some future proofing by adding padding reserved for future use.
The alignment issue relates to the gpioevent_data, which packs to different sizes on 32-bit and 64-bit platforms. That creates problems for 32-bit apps running on 64-bit kernels. uAPI v2 addresses that particular issue, and the problem more generally, by adding pad fields that explicitly pad structs out to 64-bit boundaries, so they will pack to the same size now, and even if some of the reserved padding is used for __u64 fields in the future.
The new structs have been analysed with pahole to ensure that they are sized as expected and contain no implicit padding.
The lack of future proofing in v1 makes it impossible to, for example, add the debounce feature that is included in v2.
The future proofing is addressed by providing configurable attributes in line config and reserved padding in all structs for future features. Specifically, the line request, config, info, info_changed and event structs receive updated versions and new ioctls.
As the majority of the structs and ioctls were being replaced, it is opportune to rework some of the other aspects of the uAPI:
v1 has three different flags fields, each with their own separate bit definitions. In v2 that is collapsed to one - gpio_v2_line_flag.
The handle and event requests are merged into a single request, the line request, as the two requests were mostly the same other than the edge detection provided by event requests. As a byproduct, the v2 uAPI allows for multiple lines producing edge events on the same line handle. This is a new capability as v1 only supports a single line in an event request.
As a consequence, there are now only two types of file handle to be concerned with, the chip and the line, and it is clearer which ioctls apply to which type of handle.
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or line_offset, and consumer rather than consumer_label.
Additionally, v1 GPIOHANDLES_MAX becomes GPIO_V2_LINES_MAX in v2 for clarity, and the gpiohandle_data __u8 array becomes a bitmap in gpio_v2_line_values.
The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
---
Changes for v9:
- use time scale suffixes on timestamp (_ns) and debounce period (_us) variables
- document all enum values
- comment wording tweaks
- change some field orders to improve readability
Changes for v7:
- use _BITULL for flag constants
Changes for v4:
- clarify bitmap width in GPIO_V2_LINES_MAX description
Changes for v3:
- relocated commentary into commit description
- hard limit max requested lines to 64 so bitmaps always fit in a single u64.
- prefix all v2 symbols with GPIO_V2
- 64-bit flag values to ULL
- use __aligned_u64 to ensure 64-bit fields are 64-bit aligned
- support masked get values, as per set values.
Changes for v2:
- lower case V1 and V2, except in capitalized names
- hyphenate 32/64-bit
- rename bitmap field to bits
- drop PAD_SIZE consts in favour of hard coded numbers
- sort includes
- change config flags to __u64
- increase padding of gpioline_event
- relocate GPIOLINE_CHANGED enum into v2 section (is common with v1)
- rework config to collapse direction, drive, bias and edge enums back into flags and add optional attributes that can be associated with a subset of the requested lines.
Changes for v1 (since the RFC):
- document the constraints on array sizes to maintain 32/64 alignment
- add sequence numbers to gpioline_event
- use bitmap for values instead of array of __u8
- gpioline_info_v2 contains gpioline_config instead of its composite fields
- provide constants for all array sizes, especially padding
- renamed "GPIOLINE_FLAG_V2_KERNEL" to "GPIOLINE_FLAG_V2_USED"
- renamed "default_values" to "values"
- made gpioline_direction zero based
- document clock used in gpioline_event timestamp
- add event_buffer_size to gpioline_request
- rename debounce to debounce_period
- rename lines to num_lines
include/uapi/linux/gpio.h | 291 +++++++++++++++++++++++++++++++++++++- 1 file changed, 284 insertions(+), 7 deletions(-)
include/uapi/linux/gpio.h +284 -7
@@ -11,11 +11,14 @@#ifndef _UAPI_GPIO_H_#define _UAPI_GPIO_H_ +#include <linux/const.h> #include <linux/ioctl.h> #include <linux/types.h> /* * The maximum size of name and label arrays. +* +* Must be a multiple of 8 to ensure 32/64-bit alignment of structs. */ #define GPIO_MAX_NAME_SIZE 32
@@ -32,6 +35,265 @@ struct gpiochip_info {__u32 lines; }; +/* +* Maximum number of requested lines. +* +* Must be no greater than 64, as bitmaps are restricted here to 64-bits +* for simplicity, and a multiple of 2 to ensure 32/64-bit alignment of +* structs. +*/ +#define GPIO_V2_LINES_MAX 64 + +/* +* The maximum number of configuration attributes associated with a line +* request. +*/ +#define GPIO_V2_LINE_NUM_ATTRS_MAX 10 + +/** +* enum gpio_v2_line_flag - &struct gpio_v2_line_attribute.flags values +* @GPIO_V2_LINE_FLAG_USED: line is not available for request +* @GPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical low +* @GPIO_V2_LINE_FLAG_INPUT: line is an input +* @GPIO_V2_LINE_FLAG_OUTPUT: line is an output +* @GPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active) +* edges +* @GPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to +* inactive) edges +* @GPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain output +* @GPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source output +* @GPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabled +* @GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabled +* @GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled +*/ +enum gpio_v2_line_flag { +GPIO_V2_LINE_FLAG_USED =_BITULL (0 ), +GPIO_V2_LINE_FLAG_ACTIVE_LOW =_BITULL (1 ), +GPIO_V2_LINE_FLAG_INPUT =_BITULL (2 ), +GPIO_V2_LINE_FLAG_OUTPUT =_BITULL (3 ), +GPIO_V2_LINE_FLAG_EDGE_RISING =_BITULL (4 ), +GPIO_V2_LINE_FLAG_EDGE_FALLING =_BITULL (5 ), +GPIO_V2_LINE_FLAG_OPEN_DRAIN =_BITULL (6 ), +GPIO_V2_LINE_FLAG_OPEN_SOURCE =_BITULL (7 ), +GPIO_V2_LINE_FLAG_BIAS_PULL_UP =_BITULL (8 ), +GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN =_BITULL (9 ), +GPIO_V2_LINE_FLAG_BIAS_DISABLED =_BITULL (10 ), +}; + +/** +* struct gpio_v2_line_values - Values of GPIO lines +* @bits: a bitmap containing the value of the lines, set to 1 for active +* and 0 for inactive. +* @mask: a bitmap identifying the lines to get or set, with each bit +* number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +*/ +struct gpio_v2_line_values { +__aligned_u64 bits; +__aligned_u64 mask; +}; + +/** +* enum gpio_v2_line_attr_id - &struct gpio_v2_line_attribute.id values +* identifying which field of the attribute union is in use. +* @GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in use +* @GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in use +* @GPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us is in use +*/ +enum gpio_v2_line_attr_id { +GPIO_V2_LINE_ATTR_ID_FLAGS =1 , +GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES =2 , +GPIO_V2_LINE_ATTR_ID_DEBOUNCE =3 , +}; + +/** +* struct gpio_v2_line_attribute - a configurable attribute of a line +* @id: attribute identifier with value from &enum gpio_v2_line_attr_id +* @padding: reserved for future use and must be zero filled +* @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO +* line, with values from enum gpio_v2_line_flag, such as +* GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed +* together. This overrides the default flags contained in the &struct +* gpio_v2_line_config for the associated line. +* @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap +* containing the values to which the lines will be set, with each bit +* number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +* @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired +* debounce period, in microseconds +*/ +struct gpio_v2_line_attribute { +__u32 id; +__u32 padding; +union { +__aligned_u64 flags; +__aligned_u64 values; +__u32 debounce_period_us; + }; +}; + +/** +* struct gpio_v2_line_config_attribute - a configuration attribute +* associated with one or more of the requested lines. +* @attr: the configurable attribute +* @mask: a bitmap identifying the lines to which the attribute applies, +* with each bit number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +*/ +struct gpio_v2_line_config_attribute { +struct gpio_v2_line_attribute attr; +__aligned_u64 mask; +}; + +/** +* struct gpio_v2_line_config - Configuration for GPIO lines +* @flags: flags for the GPIO lines, with values from enum +* gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, +* GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together. This is the default for +* all requested lines but may be overridden for particular lines using +* attrs. +* @num_attrs: the number of attributes in attrs +* @padding: reserved for future use and must be zero filled +* @attrs: the configuration attributes associated with the requested +* lines. Any attribute should only be associated with a particular line +* once. If an attribute is associated with a line multiple times then the +* first occurrence (i.e. lowest index) has precedence. +*/ +struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ]; +struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX ]; +}; + +/** +* struct gpio_v2_line_request - Information about a request for GPIO lines +* @offsets: an array of desired lines, specified by offset index for the +* associated GPIO chip +* @consumer: a desired consumer label for the selected GPIO lines such as +* "my-bitbanged-relay" +* @config: requested configuration for the lines. +* @num_lines: number of lines requested in this request, i.e. the number +* of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to +* request a single line +* @event_buffer_size: a suggested minimum number of line events that the +* kernel should buffer. This is only relevant if edge detection is +* enabled in the configuration. Note that this is only a suggested value +* and the kernel may allocate a larger buffer or cap the size of the +* buffer. If this field is zero then the buffer size defaults to a minimum +* of num_lines*16. +* @padding: reserved for future use and must be zero filled +* @fd: if successful this field will contain a valid anonymous file handle +* after a GPIO_GET_LINE_IOCTL operation, zero or negative value means +* error +*/ +struct gpio_v2_line_request { +__u32 offsets[GPIO_V2_LINES_MAX ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +struct gpio_v2_line_config config; +__u32 num_lines; +__u32 event_buffer_size; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ]; +__s32 fd; +}; + +/** +* struct gpio_v2_line_info - Information about a certain GPIO line +* @name: the name of this GPIO line, such as the output pin of the line on +* the chip, a rail or a pin header name on a board, as specified by the +* GPIO chip, may be empty +* @consumer: a functional name for the consumer of this GPIO line as set +* by whatever is using it, will be empty if there is no current user but +* may also be empty if the consumer doesn't set this up +* @flags: flags for the GPIO line, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, +* GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together +* @offset: the local offset on this GPIO chip, fill this in when +* requesting the line information from the kernel +* @num_attrs: the number of attributes in attrs +* @attrs: the configuration attributes associated with the line +* @padding: reserved for future use +*/ +struct gpio_v2_line_info { +char name[GPIO_MAX_NAME_SIZE ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +__u32 offset; +__u32 num_attrs; +__aligned_u64 flags; +struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX ]; +/* Space reserved for future use. */ +__u32 padding[4 ]; +}; + +/** +* enum gpio_v2_line_changed_type - &struct gpio_v2_line_changed.event_type +* values +* @GPIO_V2_LINE_CHANGED_REQUESTED: line has been requested +* @GPIO_V2_LINE_CHANGED_RELEASED: line has been released +* @GPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured +*/ +enum gpio_v2_line_changed_type { +GPIO_V2_LINE_CHANGED_REQUESTED =1 , +GPIO_V2_LINE_CHANGED_RELEASED =2 , +GPIO_V2_LINE_CHANGED_CONFIG =3 , +}; + +/** +* struct gpio_v2_line_info_changed - Information about a change in status +* of a GPIO line +* @info: updated line information +* @timestamp_ns: estimate of time of status change occurrence, in nanoseconds +* @event_type: the type of change with a value from enum +* gpio_v2_line_changed_type +* @padding: reserved for future use +*/ +struct gpio_v2_line_info_changed { +struct gpio_v2_line_info info; +__aligned_u64 timestamp_ns; +__u32 event_type; +/* Pad struct to 64-bit boundary and reserve space for future use. */ +__u32 padding[5 ]; +}; + +/** +* enum gpio_v2_line_event_id - &struct gpio_v2_line_event.id values +* @GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edge +* @GPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge +*/ +enum gpio_v2_line_event_id { +GPIO_V2_LINE_EVENT_RISING_EDGE =1 , +GPIO_V2_LINE_EVENT_FALLING_EDGE =2 , +}; + +/** +* struct gpio_v2_line_event - The actual event being pushed to userspace +* @timestamp_ns: best estimate of time of event occurrence, in nanoseconds. +* The timestamp_ns is read from CLOCK_MONOTONIC and is intended to allow the +* accurate measurement of the time between events. It does not provide +* the wall-clock time. +* @id: event identifier with value from enum gpio_v2_line_event_id +* @offset: the offset of the line that triggered the event +* @seqno: the sequence number for this event in the sequence of events for +* all the lines in this line request +* @line_seqno: the sequence number for this event in the sequence of +* events on this particular line +* @padding: reserved for future use +*/ +struct gpio_v2_line_event { +__aligned_u64 timestamp_ns; +__u32 id; +__u32 offset; +__u32 seqno; +__u32 line_seqno; +/* Space reserved for future use. */ +__u32 padding[6 ]; +}; + +/* +* ABI v1 +*/ +/* Informational flags */ #define GPIOLINE_FLAG_KERNEL (1UL << 0)/* Line used by the kernel */ #define GPIOLINE_FLAG_IS_OUT (1UL << 1)
@@ -149,8 +411,6 @@ struct gpiohandle_config {__u32 padding[4 ];/* padding for future use */ }; -#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0a, struct gpiohandle_config) -/** * struct gpiohandle_data - Information of values on a GPIO handle * @values: when getting the state of lines this contains the current
@@ -161,9 +421,6 @@ struct gpiohandle_data {__u8 values[GPIOHANDLES_MAX ]; }; -#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) -#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) -/* Eventrequest flags */ #define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0)#define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1)
@@ -207,11 +464,31 @@ struct gpioevent_data { __u32 id; }; +/* +* v1 and v2 ioctl()s +*/ #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info) +#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0C, __u32) + +/* +* v2 ioctl()s +*/ +#define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request) +#define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config) +#define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values) +#define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values) + +/* +* v1 ioctl()s +*/ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info) -#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0b, struct gpioline_info) -#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0c, __u32)#define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request)#define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request) +#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) +#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) +#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0A, struct gpiohandle_config) +#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0B, struct gpioline_info)#endif /* _UAPI_GPIO_H_ */
Signature · 1 line
2.28.0
Re: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 15 lines · Kent Gibson
include/uapi/linux/gpio.h +15
-0+/** +* struct gpio_v2_line_attribute - a configurable attribute of a line +* @id: attribute identifier with value from &enum gpio_v2_line_attr_id +* @padding: reserved for future use and must be zero filled +* @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO +* line, with values from enum gpio_v2_line_flag, such as +* GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed +* together. This overrides the default flags contained in the &struct +* gpio_v2_line_config for the associated line. +* @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap +* containing the values to which the lines will be set, with each bit +* number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +* @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired +* debounce period, in microseconds
include/uapi/linux/gpio.h struct gpiochip_info { · line 133
Quoted 10 lines
include/uapi/linux/gpio.h +10
-0+*/ +struct gpio_v2_line_attribute { +__u32 id; +__u32 padding; +union { +__aligned_u64 flags; +__aligned_u64 values; +__u32 debounce_period_us; + }; +};
Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all __aligned_u64 even when 32 bits are sufficient?
Quoted 11 lines
include/uapi/linux/gpio.h +10
-0+struct gpio_v2_line_request { +__u32 offsets[GPIO_V2_LINES_MAX ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +struct gpio_v2_line_config config; +__u32 num_lines; +__u32 event_buffer_size; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ]; +__s32 fd; +};
include/uapi/linux/gpio.h struct gpiochip_info { · line 227
Quoted 10 lines
include/uapi/linux/gpio.h +10
-0+struct gpio_v2_line_info { +char name[GPIO_MAX_NAME_SIZE ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +__u32 offset; +__u32 num_attrs; +__aligned_u64 flags; +struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX ]; +/* Space reserved for future use. */ +__u32 padding[4 ]; +};
These are both several hundred bytes long, requiring a lot of data to be copied to the stack and take up space there. I see this is not actually much different for the v1 API, but I wonder if there has been any analysis of whether this has a noticeable effect on application runtime.
Arnd
Re: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 23 lines · Arnd Bergmann
Quoted 21 lines · Kent Gibson
include/uapi/linux/gpio.h +21
-0+/** +* struct gpio_v2_line_attribute - a configurable attribute of a line +* @id: attribute identifier with value from &enum gpio_v2_line_attr_id +* @padding: reserved for future use and must be zero filled +* @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO +* line, with values from enum gpio_v2_line_flag, such as +* GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed +* together. This overrides the default flags contained in the &struct +* gpio_v2_line_config for the associated line. +* @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap +* containing the values to which the lines will be set, with each bit +* number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +* @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired +* debounce period, in microseconds +*/ +struct gpio_v2_line_attribute { +__u32 id; +__u32 padding; +union { +__aligned_u64 flags;
Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 133
Quoted 4 lines
include/uapi/linux/gpio.h +4
-0+__aligned_u64 values; +__u32 debounce_period_us; + }; +};Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all
__aligned_u64even when 32 bits are sufficient?
Ah yes, adding support for GPIO ioctl()'s to strace has been on my TODO list for 3 years now. :(
Quoted 11 lines
Quoted 10 lines
include/uapi/linux/gpio.h +10
-0+struct gpio_v2_line_request { +__u32 offsets[GPIO_V2_LINES_MAX ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +struct gpio_v2_line_config config; +__u32 num_lines; +__u32 event_buffer_size; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ]; +__s32 fd; +};
Quoted 17 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 227
Quoted 10 lines
include/uapi/linux/gpio.h +10
-0+struct gpio_v2_line_info { +char name[GPIO_MAX_NAME_SIZE ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +__u32 offset; +__u32 num_attrs; +__aligned_u64 flags; +struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX ]; +/* Space reserved for future use. */ +__u32 padding[4 ]; +};These are both several hundred bytes long, requiring a lot of data to be copied to the stack and take up space there. I see this is not actually much different for the v1 API, but I wonder if there has been any analysis of whether this has a noticeable effect on application runtime.
The main difference between this and V1 is that we can now pass arguments for flags as defined in struct gpio_v2_line_attribute. I haven't measured the impact but first: this is not a hot path (retrieving line info is not done a lot like reading line events or setting/getting values), and second: this structure is 280 bytes long which is still less than a page so we should not face more context switches than with a smaller structure, right?
Bartosz
Kent Gibson
reviewerRe: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 34 lines · Bartosz Golaszewski
Quoted 23 lines · Arnd Bergmann
Quoted 21 lines · Kent Gibson
include/uapi/linux/gpio.h +21
-0+/** +* struct gpio_v2_line_attribute - a configurable attribute of a line +* @id: attribute identifier with value from &enum gpio_v2_line_attr_id +* @padding: reserved for future use and must be zero filled +* @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO +* line, with values from enum gpio_v2_line_flag, such as +* GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed +* together. This overrides the default flags contained in the &struct +* gpio_v2_line_config for the associated line. +* @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap +* containing the values to which the lines will be set, with each bit +* number corresponding to the index into &struct +* gpio_v2_line_request.offsets. +* @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired +* debounce period, in microseconds +*/ +struct gpio_v2_line_attribute { +__u32 id; +__u32 padding; +union { +__aligned_u64 flags;Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 133
Quoted 4 lines
include/uapi/linux/gpio.h +4
-0+__aligned_u64 values; +__u32 debounce_period_us; + }; +};Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all
__aligned_u64even when 32 bits are sufficient?
Quoted 3 lines
Ah yes, adding support for GPIO ioctl()'s to strace has been on my TODO list for 3 years now. :(
Great - you beat me to it - I was going to ask if we could fix strace ;-). If it can be taught the id/union semantics I'd rather do that, so we could then also format the 64-bit values appropriately. e.g. flags and values are both bitmaps but the bits are interpreted very differently.
Cheers,
Kent.
Kent Gibson
reviewerstrace decoding for GPIO uAPI
In reply to Bartosz GolaszewskiQuoted 5 lines · Bartosz Golaszewski
Quoted 4 lines · Arnd Bergmann
Quoted 1 line · Kent Gibson
+/**
Quoted 8 lines
Quoted 4 lines
Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all
__aligned_u64even when 32 bits are sufficient?Ah yes, adding support for GPIO ioctl()'s to strace has been on my TODO list for 3 years now. :(
I'm looking at doing this as it would be useful to have.
Just starting with the v1 uAPI, as v2 isn't merged yet.
I think I've got it basically done, but I thought I'd run it past you before sending a patch to the strace list.
Successful calls currently look like this:
ioctl(3, GPIO_GET_CHIPINFO_IOCTL, {name="gpiochip1", label="gpio-mockup-B", lines=8}) = 0
ioctl(3, GPIO_GET_LINEHANDLE_IOCTL, {lines=8, lineoffsets=[3, 2, 1, 0, 4, 5, 6, 7], flags=GPIOHANDLE_REQUEST_INPUT, default_values=[0, 0, 0, 0, 0, 0, 0, 0], consumer_label="", fd=7}) = 0
ioctl(0, GPIO_GET_LINEEVENT_IOCTL, {lineoffset=2, handleflags=GPIOHANDLE_REQUEST_INPUT, eventflags=GPIOEVENT_REQUEST_BOTH_EDGES, consumer_label="high to low", fd=3}) = 0
ioctl(3, GPIO_GET_LINEINFO_IOCTL, {line_offset=1, flags=GPIOLINE_FLAG_KERNEL|GPIOLINE_FLAG_IS_OUT, name="gpio-mockup-A-1", consumer="output atv-lo to as-is atv-hi"}) = 0
ioctl(7, GPIOHANDLE_GET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0
ioctl(7, GPIOHANDLE_SET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0
ioctl(7, GPIOHANDLE_SET_CONFIG_IOCTL, {flags=GPIOHANDLE_REQUEST_ACTIVE_LOW, default_values=[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3, flags=0, name="gpio-mockup-A-3", consumer=""}) = 0
ioctl(3, GPIO_GET_LINEINFO_UNWATCH_IOCTL, {offset=3}) = 0
For unsuccessful calls it only prints the input fields, and drops the output fields. e.g.
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3}) = -1 EPERM (Operation not permitted)
Does that work for you?
Cheers,
Kent.
Re: strace decoding for GPIO uAPI
Quoted 41 lines · Kent Gibson
Quoted 5 lines · Bartosz Golaszewski
Quoted 4 lines · Arnd Bergmann
Quoted 1 line · Kent Gibson
+/**
Quoted 8 lines
Quoted 4 lines
Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all
__aligned_u64even when 32 bits are sufficient?Ah yes, adding support for GPIO ioctl()'s to strace has been on my TODO list for 3 years now. :(
I'm looking at doing this as it would be useful to have.
Just starting with the v1 uAPI, as v2 isn't merged yet.
I think I've got it basically done, but I thought I'd run it past you before sending a patch to the strace list.
Successful calls currently look like this:
ioctl(3, GPIO_GET_CHIPINFO_IOCTL, {name="gpiochip1", label="gpio-mockup-B", lines=8})= 0ioctl(3,
GPIO_GET_LINEHANDLE_IOCTL, {lines=8, lineoffsets=[3, 2, 1, 0, 4, 5, 6, 7], flags=GPIOHANDLE_REQUEST_INPUT, default_values=[0, 0, 0, 0, 0, 0, 0, 0], consumer_label="", fd=7}) = 0ioctl(0,
GPIO_GET_LINEEVENT_IOCTL, {lineoffset=2, handleflags=GPIOHANDLE_REQUEST_INPUT, eventflags=GPIOEVENT_REQUEST_BOTH_EDGES, consumer_label="high to low", fd=3}) = 0ioctl(3,
GPIO_GET_LINEINFO_IOCTL, {line_offset=1, flags=GPIOLINE_FLAG_KERNEL|GPIOLINE_FLAG_IS_OUT, name="gpio-mockup-A-1", consumer="output atv-lo to as-is atv-hi"}) = 0ioctl(7,
GPIOHANDLE_GET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0ioctl(7,
GPIOHANDLE_SET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0ioctl(7,
GPIOHANDLE_SET_CONFIG_IOCTL, {flags=GPIOHANDLE_REQUEST_ACTIVE_LOW, default_values=[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3, flags=0, name="gpio-mockup-A-3", consumer=""})= 0
Quoted 9 lines
ioctl(3, GPIO_GET_LINEINFO_UNWATCH_IOCTL, {offset=3})= 0For unsuccessful calls it only prints the input fields, and drops the output fields. e.g.
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3})= -1 EPERM (Operation not permitted)Does that work for you?
Kent,
This is perfect! Thanks for doing this! For the error cases: I'm not sure how strace handles this for other ioctls() - maybe we could print something like 'flags=N/A'? Unless what you did is the standard way, then leave it.
Bartosz
Kent Gibson
reviewerRe: strace decoding for GPIO uAPI
Quoted 52 lines · Bartosz Golaszewski
Quoted 41 lines · Kent Gibson
Quoted 5 lines · Bartosz Golaszewski
Quoted 4 lines · Arnd Bergmann
Quoted 1 line · Kent Gibson
+/**
Quoted 8 lines
Quoted 4 lines
Having different-sized members in the union makes it hard for something like strace to print the contents. How about just making them all
__aligned_u64even when 32 bits are sufficient?Ah yes, adding support for GPIO ioctl()'s to strace has been on my TODO list for 3 years now. :(
I'm looking at doing this as it would be useful to have.
Just starting with the v1 uAPI, as v2 isn't merged yet.
I think I've got it basically done, but I thought I'd run it past you before sending a patch to the strace list.
Successful calls currently look like this:
ioctl(3, GPIO_GET_CHIPINFO_IOCTL, {name="gpiochip1", label="gpio-mockup-B", lines=8})= 0ioctl(3,
GPIO_GET_LINEHANDLE_IOCTL, {lines=8, lineoffsets=[3, 2, 1, 0, 4, 5, 6, 7], flags=GPIOHANDLE_REQUEST_INPUT, default_values=[0, 0, 0, 0, 0, 0, 0, 0], consumer_label="", fd=7}) = 0ioctl(0,
GPIO_GET_LINEEVENT_IOCTL, {lineoffset=2, handleflags=GPIOHANDLE_REQUEST_INPUT, eventflags=GPIOEVENT_REQUEST_BOTH_EDGES, consumer_label="high to low", fd=3}) = 0ioctl(3,
GPIO_GET_LINEINFO_IOCTL, {line_offset=1, flags=GPIOLINE_FLAG_KERNEL|GPIOLINE_FLAG_IS_OUT, name="gpio-mockup-A-1", consumer="output atv-lo to as-is atv-hi"}) = 0ioctl(7,
GPIOHANDLE_GET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0ioctl(7,
GPIOHANDLE_SET_LINE_VALUES_IOCTL, {values=[1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0ioctl(7,
GPIOHANDLE_SET_CONFIG_IOCTL, {flags=GPIOHANDLE_REQUEST_ACTIVE_LOW, default_values=[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]}) = 0
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3, flags=0, name="gpio-mockup-A-3", consumer=""})= 0Quoted 9 lines
ioctl(3, GPIO_GET_LINEINFO_UNWATCH_IOCTL, {offset=3})= 0For unsuccessful calls it only prints the input fields, and drops the output fields. e.g.
ioctl(3, GPIO_GET_LINEINFO_WATCH_IOCTL, {line_offset=3})= -1 EPERM (Operation not permitted)Does that work for you?
Quoted 7 lines
Kent,
This is perfect! Thanks for doing this! For the error cases: I'm not sure how strace handles this for other
ioctls()- maybe we could print something like 'flags=N/A'? Unless what you did is the standard way, then leave it.
I think that is pretty standard, but I will double check.
Just debugging the uAPI v2 decoding now, though I wont submit that until the uAPI is in an -rc. It may be useful for debugging in the meantime, so I'll push a copy to my github fork[1] shortly - the v1 decoding is there already.
I've noticed a few things I want to tidy up in the v1 decoding before I submit that though. Should have it ready to submit in the next few days.
Cheers,
Kent.
Re: strace decoding for GPIO uAPI
In reply to Bartosz GolaszewskiQuoted 2 lines · Bartosz Golaszewski
Quoted 1 line · Kent Gibson
On Tue, Sep 22, 2020 at 11:50:51AM +0200, Bartosz Golaszewski wrote:
...
Quoted 8 lines
Quoted 3 lines
I'm looking at doing this as it would be useful to have.
Does that work for you?
This is perfect! Thanks for doing this! For the error cases: I'm not sure how strace handles this for other
ioctls()- maybe we could print something like 'flags=N/A'? Unless what you did is the standard way, then leave it.
+1 here. Thanks for doing this, it's helpful!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v9 04/20] gpio: uapi: define uAPI v2
In reply to Kent GibsonQuoted 51 lines · Kent Gibson
Add a new version of the uAPI to address existing 32/64-bit alignment issues, add support for debounce and event sequence numbers, allow requested lines with different configurations, and provide some future proofing by adding padding reserved for future use.
The alignment issue relates to the
gpioevent_data, which packs to different sizes on 32-bit and 64-bit platforms. That creates problems for 32-bit apps running on 64-bit kernels. uAPI v2 addresses that particular issue, and the problem more generally, by adding pad fields that explicitly pad structs out to 64-bit boundaries, so they will pack to the same size now, and even if some of the reserved padding is used for__u64fields in the future.The new structs have been analysed with pahole to ensure that they are sized as expected and contain no implicit padding.
The lack of future proofing in v1 makes it impossible to, for example, add the debounce feature that is included in v2.
The future proofing is addressed by providing configurable attributes in line config and reserved padding in all structs for future features. Specifically, the line request, config, info,info_changedand event structs receive updated versions and new ioctls.As the majority of the structs and ioctls were being replaced, it is opportune to rework some of the other aspects of the uAPI:
v1 has three different flags fields, each with their own separate bit definitions. In v2 that is collapsed to one -
gpio_v2_line_flag.The handle and event requests are merged into a single request, the line request, as the two requests were mostly the same other than the edge detection provided by event requests. As a byproduct, the v2 uAPI allows for multiple lines producing edge events on the same line handle. This is a new capability as v1 only supports a single line in an event request.
As a consequence, there are now only two types of file handle to be concerned with, the chip and the line, and it is clearer which ioctls apply to which type of handle.
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or
line_offset, and consumer rather thanconsumer_label.Additionally, v1
GPIOHANDLES_MAXbecomesGPIO_V2_LINES_MAXin v2 for clarity, and thegpiohandle_data__u8array becomes a bitmap ingpio_v2_line_values.The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
...
include/uapi/linux/gpio.h struct gpiochip_info { · line 166
Quoted 6 lines
include/uapi/linux/gpio.h +5
-0+struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];
Probably I somehow missed the answer, but why do we need 5 here and not 1?
Quoted 2 lines
(quoted diff) +2
-0+ struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; +};
...
include/uapi/linux/gpio.h struct gpiochip_info { · line 198
Quoted 9 lines
include/uapi/linux/gpio.h +8
-0+struct gpio_v2_line_request { +__u32 offsets[GPIO_V2_LINES_MAX ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +struct gpio_v2_line_config config; +__u32 num_lines; +__u32 event_buffer_size; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];
Ditto.
Quoted 2 lines
(quoted diff) +2
-0+ __s32 fd; +};
...
include/uapi/linux/gpio.h struct gpiochip_info { · line 226
Quoted 10 lines
include/uapi/linux/gpio.h +9
-0+struct gpio_v2_line_info { +char name[GPIO_MAX_NAME_SIZE ]; +char consumer[GPIO_MAX_NAME_SIZE ]; +__u32 offset; +__u32 num_attrs; +__aligned_u64 flags; +struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX ]; +/* Space reserved for future use. */ +__u32 padding[4 ];
Here two comments as in previous patches, why this went after attribute structures and why 2 is not enough?
Quoted 1 line
+};
...
include/uapi/linux/gpio.h struct gpiochip_info { · line 256
Quoted 6 lines
include/uapi/linux/gpio.h +6
-0+struct gpio_v2_line_info_changed { +struct gpio_v2_line_info info; +__aligned_u64 timestamp_ns; +__u32 event_type; +/* Pad struct to 64-bit boundary and reserve space for future use. */ +__u32 padding[5 ];
Again, why 5 and not 1?
Quoted 1 line
+};
...
include/uapi/linux/gpio.h struct gpiochip_info { · line 290
Quoted 9 lines
include/uapi/linux/gpio.h +8
-0+struct gpio_v2_line_event { +__aligned_u64 timestamp_ns; +__u32 id; +__u32 offset; +__u32 seqno; +__u32 line_seqno; +/* Space reserved for future use. */ +__u32 padding[6 ];
Why 6 and not 2?
And here actually sizeof() can be a version. So, I still see possible versioning issues with ABI.
Quoted 1 line
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
[snip]
Quoted 13 lines
Quoted 10 lines
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or
line_offset, and consumer rather thanconsumer_label.Additionally, v1
GPIOHANDLES_MAXbecomesGPIO_V2_LINES_MAXin v2 for clarity, and thegpiohandle_data__u8array becomes a bitmap ingpio_v2_line_values.The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
...
Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 166
Quoted 6 lines
include/uapi/linux/gpio.h +5
-0+struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];Probably I somehow missed the answer, but why do we need 5 here and not 1?
Sorry, I got tired of repeating myself, and just acked that we disagree on the approach here.
Your suggestion to use the size for version would result in an explosion of ioctl signatures - every time we add a field we have to add a new ioctl and handle it separately in gpio_ioctl() or linereq_ioctl().
Instead what we do here is reserve some space for future use - that we can replace with fields without changing the signature.
The padding is required to be zeroed now, and any future use will take a 0 to mean "leave alone".
The sizes are a guestimate as to what may be needed in the future, and as such are almost certainly wrong - but hopefully on the high side. If that fails we can always fall back to your approach.
Cheers,
Kent.
Re: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 30 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
[snip]
Quoted 13 lines
Quoted 10 lines
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or
line_offset, and consumer rather thanconsumer_label.Additionally, v1
GPIOHANDLES_MAXbecomesGPIO_V2_LINES_MAXin v2 for clarity, and thegpiohandle_data__u8array becomes a bitmap ingpio_v2_line_values.The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
...
Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 166
Quoted 6 lines
include/uapi/linux/gpio.h +5
-0+struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];Probably I somehow missed the answer, but why do we need 5 here and not 1?
Quoted 6 lines
Sorry, I got tired of repeating myself, and just acked that we disagree on the approach here.
Your suggestion to use the size for version would result in an explosion of ioctl signatures - every time we add a field we have to add a new ioctl and handle it separately in
gpio_ioctl()orlinereq_ioctl().
No, you just add__u32 version; // implies sizeof() check as well.
Look for examples of existing ABIs (e.g. perf ABI).
Quoted 8 lines
Instead what we do here is reserve some space for future use - that we can replace with fields without changing the signature.
The padding is required to be zeroed now, and any future use will take a 0 to mean "leave alone".The sizes are a guestimate as to what may be needed in the future, and as such are almost certainly wrong - but hopefully on the high side. If that fails we can always fall back to your approach.
I see. So, we have no agreement on these pieces.
Linus and Bart can decide what to do, but I think either way has pros and cons.
So, guys, I am fine with everything else here, except this versioning issue and waste of resources.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 29 lines · Andy Shevchenko
Quoted 28 lines · Kent Gibson
Quoted 14 lines · Andy Shevchenko
Quoted 10 lines · Kent Gibson
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or
line_offset, and consumer rather thanconsumer_label.Additionally, v1
GPIOHANDLES_MAXbecomesGPIO_V2_LINES_MAXin v2 for clarity, and thegpiohandle_data__u8array becomes a bitmap ingpio_v2_line_values.The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
...
Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 166
Quoted 6 lines
include/uapi/linux/gpio.h +5
-0+struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];Probably I somehow missed the answer, but why do we need 5 here and not 1?
Sorry, I got tired of repeating myself, and just acked that we disagree on the approach here.
Quoted 8 lines
Quoted 3 lines
Your suggestion to use the size for version would result in an explosion of ioctl signatures - every time we add a field we have to add a new ioctl and handle it separately in
gpio_ioctl()orlinereq_ioctl().No, you just add
__u32version; // impliessizeof()check as well.Look for examples of existing ABIs (e.g. perf ABI).
Please don't ever add a version field to an ioctl structure, this has been shown to cause more problems than it solves many times in the past...
Having some reserved fields can be helpful, as long as the kernel returns an error in case any of the unknown fields are nonzero.
I'd also prefer fewer than five reserved fields, but that is not as important, just use as many as are likely to be used in the future, but not more: It's easy to add one more ioctl command in case the expectations are wrong, but I agree you wouldn't want a new command every time another field gets added if the past has shown that this happens a lot.
Arnd
Andy Shevchenko
Reviewed-byRe: [PATCH v9 04/20] gpio: uapi: define uAPI v2
Quoted 47 lines · Arnd Bergmann
Quoted 29 lines · Andy Shevchenko
Quoted 28 lines · Kent Gibson
Quoted 14 lines · Andy Shevchenko
Quoted 10 lines · Kent Gibson
There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or
line_offset, and consumer rather thanconsumer_label.Additionally, v1
GPIOHANDLES_MAXbecomesGPIO_V2_LINES_MAXin v2 for clarity, and thegpiohandle_data__u8array becomes a bitmap ingpio_v2_line_values.The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it.
...
Quoted 9 lines
include/uapi/linux/gpio.h struct gpiochip_info { · line 166
Quoted 6 lines
include/uapi/linux/gpio.h +5
-0+struct gpio_v2_line_config { +__aligned_u64 flags; +__u32 num_attrs; +/* Pad to fill implicit padding and reserve space for future use. */ +__u32 padding[5 ];Probably I somehow missed the answer, but why do we need 5 here and not 1?
Sorry, I got tired of repeating myself, and just acked that we disagree on the approach here.
Quoted 8 lines
Quoted 3 lines
Your suggestion to use the size for version would result in an explosion of ioctl signatures - every time we add a field we have to add a new ioctl and handle it separately in
gpio_ioctl()orlinereq_ioctl().No, you just add
__u32version; // impliessizeof()check as well.Look for examples of existing ABIs (e.g. perf ABI).
Please don't ever add a version field to an ioctl structure, this has been shown to cause more problems than it solves many times in the past...
Having some reserved fields can be helpful, as long as the kernel returns an error in case any of the unknown fields are nonzero.
Quoted 6 lines
I'd also prefer fewer than five reserved fields, but that is not as important, just use as many as are likely to be used in the future, but not more: It's easy to add one more ioctl command in case the expectations are wrong, but I agree you wouldn't want a new command every time another field gets added if the past has shown that this happens a lot.
Thanks for caution and explanation. Let's go then with this.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerPATCH v9 5/20 gpiolib: make cdev a build option
In reply to Kent GibsonMake the gpiolib-cdev module a build option. This allows the CDEV interface to be removed from the kernel to reduce kernel size in applications where is it not required, and provides the parent for other CDEV interface specific build options to follow.
--- drivers/gpio/Kconfig | 17 +++++++++++++++-- drivers/gpio/Makefile | 2 +- drivers/gpio/gpiolib-cdev.h | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)
drivers/gpio/Kconfig +15 -2
@@ -66,8 +66,21 @@ config GPIO_SYSFS This ABI is deprecated. If you want to use GPIO from userspace, use the character device /dev/gpiochipN with the appropriate - ioctl() operations instead. The character device is always - available. + ioctl() operations instead. + +config GPIO_CDEV + bool + prompt "Character device (/dev/gpiochipN) support" if EXPERT + default y + help + Say Y here to add the character device /dev/gpiochipN interface + for GPIOs. The character device allows userspace to control GPIOs + using ioctl() operations. + + Only say N if you are sure that the GPIO character device is not + required. + + If unsure, say Y. config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers
drivers/gpio/Makefile +1 -1
@@ -6,8 +6,8 @@ ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG obj-$(CONFIG_GPIOLIB) += gpiolib.o obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.o -obj-$(CONFIG_GPIOLIB) += gpiolib-cdev.o obj-$(CONFIG_OF_GPIO) += gpiolib-of.o +obj-$(CONFIG_GPIO_CDEV) += gpiolib-cdev.o obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o
drivers/gpio/gpiolib-cdev.h +15 -0
@@ -5,7 +5,22 @@#include <linux/device.h> +#ifdef CONFIG_GPIO_CDEV +int gpiolib_cdev_register (struct gpio_device *gdev,dev_t devt);void gpiolib_cdev_unregister (struct gpio_device *gdev); +#else + +static inline int gpiolib_cdev_register (struct gpio_device *gdev,dev_t devt) +{ +return 0 ; +} + +static inline void gpiolib_cdev_unregister (struct gpio_device *gdev) +{ +} + +#endif /* CONFIG_GPIO_CDEV */ +#endif /* GPIOLIB_CDEV_H */
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 6/20 gpiolib: add build option for CDEV v1 ABI
In reply to Kent GibsonAdd a build option to allow the removal of the CDEV v1 ABI.
---
This patch is before the v2 implementation, and is non-functional until that patch, as some parts of that patch would be written slightly differently if removing v1 was not considered.
Adding this patch after that would necessitate revisiting the v2 changes, so this ordering results in two simpler patches.
drivers/gpio/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/gpio/Kconfig +12 -0
@@ -82,6 +82,18 @@ config GPIO_CDEV If unsure, say Y. +config GPIO_CDEV_V1 + bool "Support GPIO ABI Version 1" + default y + depends on GPIO_CDEV + help + Say Y here to support version 1 of the GPIO CDEV ABI. + + This ABI version is deprecated. + Please use the latest ABI for new developments. + + If unsure, say Y. + config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers tristate
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 7/20 gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
In reply to Kent GibsonAdd support for requesting lines using the GPIO_V2_GET_LINE_IOCTL, and returning their current values using GPIO_V2_LINE_GET_VALUES_IOCTL.
The struct linereq implementation is based on the v1 struct linehandle implementation.
---
The linereq_ioctl() is a simple wrapper around linereq_get_values() here, but will be extended with other ioctls in subsequent patches.
Similarly, the struct line only contains the desc here, but will receive the edge detector and debouncer fields in subsequent patches.
Changed for v8:
- fix BUILD_BUG_ON conditions and relocate them before the return in gpiolib_cdev_register()
Changes for v7:
- add check on kmalloc_array return value
Changes for v5:
- as per cover letter
Changes for v4:
- fix handling of mask in line_get_values
drivers/gpio/gpiolib-cdev.c | 422 ++++++++++++++++++++++++++++++++++++ 1 file changed, 422 insertions(+)
drivers/gpio/gpiolib-cdev.c +422 -0
@@ -1,7 +1,9 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/anon_inodes.h> +#include <linux/atomic.h> #include <linux/bitmap.h> +#include <linux/build_bug.h> #include <linux/cdev.h> #include <linux/compat.h> #include <linux/device.h>
@@ -24,6 +26,25 @@#include "gpiolib.h" #include "gpiolib-cdev.h" +/* +* Array sizes must ensure 64-bit alignment and not create holes in the +* struct packing. +*/ +static_assert (IS_ALIGNED (GPIO_V2_LINES_MAX ,2 )); +static_assert (IS_ALIGNED (GPIO_MAX_NAME_SIZE ,8 )); + +/* +* Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility +*/ +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_attribute ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_config_attribute ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_config ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_request ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_info ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_info_changed ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_event ),8 )); +static_assert (IS_ALIGNED (sizeof (struct gpio_v2_line_values ),8 )); +/* Character device interface to GPIO. * * The GPIO character device, /dev/gpiochipN, provides userspace an
@@ -34,6 +55,7 @@* GPIO line handle management */ +#ifdef CONFIG_GPIO_CDEV_V1 /** * struct linehandle_state - contains the state of a userspace handle * @gdev: the GPIO device the handle pertains to
@@ -376,6 +398,400 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)linehandle_free (lh);return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ + +/** +* struct line - contains the state of a requested line +* @desc: the GPIO descriptor for this line. +*/ +struct line { +struct gpio_desc *desc; +}; + +/** +* struct linereq - contains the state of a userspace line request +* @gdev: the GPIO device the line request pertains to +* @label: consumer label used to tag GPIO descriptors +* @num_lines: the number of lines in the lines array +* @lines: the lines held by this line request, with @num_lines elements. +*/ +struct linereq { +struct gpio_device *gdev; +const char *label; +u32 num_lines; +struct line lines[]; +}; + +#define GPIO_V2_LINE_BIAS_FLAGS \ + (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ + GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ + GPIO_V2_LINE_FLAG_BIAS_DISABLED) + +#define GPIO_V2_LINE_DIRECTION_FLAGS \ + (GPIO_V2_LINE_FLAG_INPUT | \ + GPIO_V2_LINE_FLAG_OUTPUT) + +#define GPIO_V2_LINE_DRIVE_FLAGS \ + (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ + GPIO_V2_LINE_FLAG_OPEN_SOURCE) + +#define GPIO_V2_LINE_VALID_FLAGS \ + (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ + GPIO_V2_LINE_DIRECTION_FLAGS | \ + GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_BIAS_FLAGS) + +static u64 gpio_v2_line_config_flags (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_FLAGS ) && + (lc->attrs[i].mask & mask)) +return lc->attrs[i].attr.flags; + } +return lc->flags; +} + +static int gpio_v2_line_config_output_value (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ) && + (lc->attrs[i].mask & mask)) +return !!(lc->attrs[i].attr.values & mask); + } +return 0 ; +} + +static int gpio_v2_line_flags_validate (u64 flags) +{ +/* Return an error if an unknown flag is set */ +if (flags & ~GPIO_V2_LINE_VALID_FLAGS ) +return -EINVAL ; + +/* +* Do not allow both INPUT & OUTPUT flags to be set as they are +* contradictory. +*/ +if ((flags &GPIO_V2_LINE_FLAG_INPUT ) && + (flags &GPIO_V2_LINE_FLAG_OUTPUT )) +return -EINVAL ; + +/* +* Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If +* the hardware actually supports enabling both at the same time the +* electrical result would be disastrous. +*/ +if ((flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ) && + (flags &GPIO_V2_LINE_FLAG_OPEN_SOURCE )) +return -EINVAL ; + +/* Drive requires explicit output direction. */ +if ((flags &GPIO_V2_LINE_DRIVE_FLAGS ) && + !(flags &GPIO_V2_LINE_FLAG_OUTPUT )) +return -EINVAL ; + +/* Bias requires explicit direction. */ +if ((flags &GPIO_V2_LINE_BIAS_FLAGS ) && + !(flags &GPIO_V2_LINE_DIRECTION_FLAGS )) +return -EINVAL ; + +/* Only one bias flag can be set. */ +if (((flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ) && + (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | +GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) || + ((flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ) && + (flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) +return -EINVAL ; + +return 0 ; +} + +static int gpio_v2_line_config_validate (struct gpio_v2_line_config *lc, +unsigned int num_lines) +{ +unsigned int i; +u64 flags; +int ret; + +if (lc->num_attrs >GPIO_V2_LINE_NUM_ATTRS_MAX ) +return -EINVAL ; + +if (memchr_inv (lc->padding,0 ,sizeof (lc->padding))) +return -EINVAL ; + +for (i =0 ; i < num_lines; i++) { + flags =gpio_v2_line_config_flags (lc, i); + ret =gpio_v2_line_flags_validate (flags); +if (ret) +return ret; + } +return 0 ; +} + +static void gpio_v2_line_config_flags_to_desc_flags (u64 flags, +unsigned long *flagsp) +{ +assign_bit (FLAG_ACTIVE_LOW , flagsp, + flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ); + +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) +set_bit (FLAG_IS_OUT , flagsp); +else if (flags &GPIO_V2_LINE_FLAG_INPUT ) +clear_bit (FLAG_IS_OUT , flagsp); + +assign_bit (FLAG_OPEN_DRAIN , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); +assign_bit (FLAG_OPEN_SOURCE , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_SOURCE ); +assign_bit (FLAG_PULL_UP , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ); +assign_bit (FLAG_PULL_DOWN , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ); +assign_bit (FLAG_BIAS_DISABLE , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ); +} + +static long linereq_get_values (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_values lv; +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_get; +int ret; + +/* NOTE: It's ok to read values of output lines. */ +if (copy_from_user (&lv, ip,sizeof (lv))) +return -EFAULT ; + +for (num_get =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + num_get++; + descs = &lr->lines[i].desc; + } + } + +if (num_get ==0 ) +return -EINVAL ; + +if (num_get !=1 ) { + descs =kmalloc_array (num_get,sizeof (*descs),GFP_KERNEL ); +if (!descs) +return -ENOMEM ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret =gpiod_get_array_value_complex (false, true, num_get, + descs,NULL , vals); + +if (num_get !=1 ) +kfree (descs); +if (ret) +return ret; + + lv.bits =0 ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { +if (test_bit (didx, vals)) + lv.bits |=BIT_ULL (i); + didx++; + } + } + +if (copy_to_user (ip, &lv,sizeof (lv))) +return -EFAULT ; + +return 0 ; +} + +static long linereq_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +struct linereq *lr = file->private_data; +void __user *ip = (void __user *)arg; + +if (cmd ==GPIO_V2_LINE_GET_VALUES_IOCTL ) +return linereq_get_values (lr, ip); + +return -EINVAL ; +} + +#ifdef CONFIG_COMPAT +static long linereq_ioctl_compat (struct file *file,unsigned int cmd, +unsigned long arg) +{ +return linereq_ioctl (file, cmd, (unsigned long )compat_ptr (arg)); +} +#endif + +static void linereq_free (struct linereq *lr) +{ +unsigned int i; + +for (i =0 ; i < lr->num_lines; i++) { +if (lr->lines[i].desc) +gpiod_free (lr->lines[i].desc); + } +kfree (lr->label); +put_device (&lr->gdev->dev); +kfree (lr); +} + +static int linereq_release (struct inode *inode,struct file *file) +{ +struct linereq *lr = file->private_data; + +linereq_free (lr); +return 0 ; +} + +static const struct file_operations line_fileops = { + .release = linereq_release, + .owner =THIS_MODULE , + .llseek = noop_llseek, + .unlocked_ioctl = linereq_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = linereq_ioctl_compat, +#endif +}; + +static int linereq_create (struct gpio_device *gdev,void __user *ip) +{ +struct gpio_v2_line_request ulr; +struct gpio_v2_line_config *lc; +struct linereq *lr; +struct file *file; +u64 flags; +unsigned int i; +int fd, ret; + +if (copy_from_user (&ulr, ip,sizeof (ulr))) +return -EFAULT ; + +if ((ulr.num_lines ==0 ) || (ulr.num_lines >GPIO_V2_LINES_MAX )) +return -EINVAL ; + +if (memchr_inv (ulr.padding,0 ,sizeof (ulr.padding))) +return -EINVAL ; + + lc = &ulr.config; + ret =gpio_v2_line_config_validate (lc, ulr.num_lines); +if (ret) +return ret; + + lr =kzalloc (struct_size (lr, lines, ulr.num_lines),GFP_KERNEL ); +if (!lr) +return -ENOMEM ; + + lr->gdev = gdev; +get_device (&gdev->dev); + +/* Make sure this is terminated */ + ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ; +if (strlen (ulr.consumer)) { + lr->label =kstrdup (ulr.consumer,GFP_KERNEL ); +if (!lr->label) { + ret = -ENOMEM ; + goto out_free_linereq; + } + } + + lr->num_lines = ulr.num_lines; + +/* Request each GPIO */ +for (i =0 ; i < ulr.num_lines; i++) { +u32 offset = ulr.offsets[i]; +struct gpio_desc *desc =gpiochip_get_desc (gdev->chip, offset); + +if (IS_ERR (desc)) { + ret =PTR_ERR (desc); + goto out_free_linereq; + } + + ret =gpiod_request (desc, lr->label); +if (ret) + goto out_free_linereq; + + lr->lines[i].desc = desc; + flags =gpio_v2_line_config_flags (lc, i); +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); + + ret =gpiod_set_transitory (desc, false); +if (ret <0 ) + goto out_free_linereq; + +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + + ret =gpiod_direction_output (desc, val); +if (ret) + goto out_free_linereq; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) + goto out_free_linereq; + } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIOLINE_CHANGED_REQUESTED , desc); + +dev_dbg (&gdev->dev,"registered chardev handle for line %d\n" , + offset); + } + + fd =get_unused_fd_flags (O_RDONLY |O_CLOEXEC ); +if (fd <0 ) { + ret = fd; + goto out_free_linereq; + } + + file =anon_inode_getfile ("gpio-line" , &line_fileops, lr, +O_RDONLY |O_CLOEXEC ); +if (IS_ERR (file)) { + ret =PTR_ERR (file); + goto out_put_unused_fd; + } + + ulr.fd = fd; +if (copy_to_user (ip, &ulr,sizeof (ulr))) { +/* +* fput() will trigger the release() callback, so do not go onto +* the regular error cleanup path here. +*/ +fput (file); +put_unused_fd (fd); +return -EFAULT ; + } + +fd_install (fd, file); + +dev_dbg (&gdev->dev,"registered chardev handle for %d lines\n" , + lr->num_lines); + +return 0 ; + +out_put_unused_fd: +put_unused_fd (fd); +out_free_linereq: +linereq_free (lr); +return ret; +} + +#ifdef CONFIG_GPIO_CDEV_V1 /* * GPIO line event management
@@ -745,6 +1161,8 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ +static void gpio_desc_to_lineinfo (struct gpio_desc *desc,struct gpioline_info *info) {
@@ -843,6 +1261,7 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (copy_to_user (ip, &chipinfo,sizeof (chipinfo)))return -EFAULT ;return 0 ; +#ifdef CONFIG_GPIO_CDEV_V1 }else if (cmd ==GPIO_GET_LINEINFO_IOCTL ) {struct gpioline_info lineinfo;
@@ -885,6 +1304,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) }return 0 ; +#endif /* CONFIG_GPIO_CDEV_V1 */ + }else if (cmd ==GPIO_V2_GET_LINE_IOCTL ) { +return linereq_create (gdev, ip); }else if (cmd ==GPIO_GET_LINEINFO_UNWATCH_IOCTL ) {if (copy_from_user (&offset, ip,sizeof (offset)))return -EFAULT ;
Signature · 1 line
2.28.0
Re: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 6 lines · Kent Gibson
Add support for requesting lines using the
GPIO_V2_GET_LINE_IOCTL, and returning their current values usingGPIO_V2_LINE_GET_VALUES_IOCTL.The struct linereq implementation is based on the v1 struct linehandle implementation.
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* + * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
You see, in some cases you are using "OR:ed" as understandable for programmers, and here & which should be and in plain English and really confusing from a programmer's perspective. That's why I prefer to see plain English rather than something which is full of encoded meanings.
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+* the hardware actually supports enabling both at the same time the +* electrical result would be disastrous. +*/
...
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 503
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c +4
-0+/* Bias requires explicit direction. */ +if ((flags &GPIO_V2_LINE_BIAS_FLAGS ) && + !(flags &GPIO_V2_LINE_DIRECTION_FLAGS )) +return -EINVAL ;
Okay, since this is strict we probably may relax it in the future if it will be a use case.
...
Quoted 1 line
+ /* Only one bias flag can be set. */
Ditto. (Some controllers allow to set both simultaneously, though I can't imagine good use case for that)
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +6
-0+if (((flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ) && + (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | +GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) || + ((flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ) && + (flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) +return -EINVAL ;
...
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 542
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +5
-0+static void gpio_v2_line_config_flags_to_desc_flags (u64 flags, +unsigned long *flagsp) +{ +assign_bit (FLAG_ACTIVE_LOW , flagsp, + flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW );
What I meant is to attach also this to the other assign_bit():s below. And just in case a question: why not __asign_bit() do we really need atomicity?
Quoted 16 lines
drivers/gpio/gpiolib-cdev.c +16
-0+if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) +set_bit (FLAG_IS_OUT , flagsp); +else if (flags &GPIO_V2_LINE_FLAG_INPUT ) +clear_bit (FLAG_IS_OUT , flagsp); + +assign_bit (FLAG_OPEN_DRAIN , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); +assign_bit (FLAG_OPEN_SOURCE , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_SOURCE ); +assign_bit (FLAG_PULL_UP , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ); +assign_bit (FLAG_PULL_DOWN , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ); +assign_bit (FLAG_BIAS_DISABLE , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ); +}
...
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +7
-0+static long linereq_get_values (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_values lv; +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_get; +int ret;
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 578
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+/* NOTE: It's ok to read values of output lines. */ +if (copy_from_user (&lv, ip,sizeof (lv))) +return -EFAULT ; + +for (num_get =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + num_get++; + descs = &lr->lines[i].desc; + } + }
So what you can do here is something like
DECLARE_BITMAP(mask, u64);
...
bitmap_from_u64(mask, lv.mask);num_get = bitmap_weight(mask, lr->num_lines);
if (num_get == 0)
return -EINVAL;
for_each_set_bit(i, mask, lr->num_lines)
descs = &lr->lines[i].desc;
// I'm not sure I understood a purpose of the above
// ah, looks like malloc() avoidance, but you may move it below...
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 583
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +4
-0+if (num_get ==0 ) +return -EINVAL ; + +if (num_get !=1 ) {
...something like
if (num_get == 1)
descs = ...[find_first_bit(mask, lr->num_lines)];
else {
...
for_each_set_bit() {
...
}
}
Quoted 17 lines
drivers/gpio/gpiolib-cdev.c +17
-0+ descs =kmalloc_array (num_get,sizeof (*descs),GFP_KERNEL ); +if (!descs) +return -ENOMEM ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret =gpiod_get_array_value_complex (false, true, num_get, + descs,NULL , vals); + +if (num_get !=1 ) +kfree (descs); +if (ret) +return ret;
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 609
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ + lv.bits =0 ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { +if (test_bit (didx, vals)) + lv.bits |=BIT_ULL (i); + didx++; + } + }
So here...
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +5
-0+if (copy_to_user (ip, &lv,sizeof (lv))) +return -EFAULT ; + +return 0 ; +}
...
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 707
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c +9
-0+/* Make sure this is terminated */ + ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ; +if (strlen (ulr.consumer)) { + lr->label =kstrdup (ulr.consumer,GFP_KERNEL ); +if (!lr->label) { + ret = -ENOMEM ; + goto out_free_linereq; + } + }
Still don't get why we can\t use kstrndup() here...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Kent Gibson
Add support for requesting lines using the
GPIO_V2_GET_LINE_IOCTL, and returning their current values usingGPIO_V2_LINE_GET_VALUES_IOCTL.The struct linereq implementation is based on the v1 struct linehandle implementation.
...
Ooops, nearly missed this one...
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* + * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. IfYou see, in some cases you are using "OR:ed" as understandable for programmers, and here & which should be and in plain English and really confusing from a programmer's perspective. That's why I prefer to see plain English rather than something which is full of encoded meanings.
Understand these are pulled directly from the v1 implementation, so I think that is actually one of Bart's.
But, yeah, it should be 'and'.
Quoted 6 lines
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+* the hardware actually supports enabling both at the same time the +* electrical result would be disastrous. +*/ ...
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 503
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c +4
-0+/* Bias requires explicit direction. */ +if ((flags &GPIO_V2_LINE_BIAS_FLAGS ) && + !(flags &GPIO_V2_LINE_DIRECTION_FLAGS )) +return -EINVAL ;Okay, since this is strict we probably may relax it in the future if it will be a use case.
...
Again, this is drawn directly from the v1 implementation, and I didn't go changing anything from v1 without good reason.
Quoted 18 lines
Quoted 1 line
+ /* Only one bias flag can be set. */
Ditto. (Some controllers allow to set both simultaneously, though I can't imagine good use case for that)
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +6
-0+if (((flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ) && + (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | +GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) || + ((flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ) && + (flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ))) +return -EINVAL ;...
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+static void gpio_v2_line_config_flags_to_desc_flags (u64 flags, +unsigned long *flagsp) +{
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 542
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+assign_bit (FLAG_ACTIVE_LOW , flagsp, + flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW );What I meant is to attach also this to the other assign_bit():s below. And just in case a question: why not
__asign_bit()do we really need atomicity?
These are initialized as per their order in the flags so it is easier to tell if any are missing.
The atomicity is not required here, but it is elsewhere so you are oblidged to use it for all accesses, no?
Quoted 62 lines
Quoted 16 lines
drivers/gpio/gpiolib-cdev.c +16
-0+if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) +set_bit (FLAG_IS_OUT , flagsp); +else if (flags &GPIO_V2_LINE_FLAG_INPUT ) +clear_bit (FLAG_IS_OUT , flagsp); + +assign_bit (FLAG_OPEN_DRAIN , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); +assign_bit (FLAG_OPEN_SOURCE , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_SOURCE ); +assign_bit (FLAG_PULL_UP , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ); +assign_bit (FLAG_PULL_DOWN , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ); +assign_bit (FLAG_BIAS_DISABLE , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED ); +}...
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +7
-0+static long linereq_get_values (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_values lv; +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_get; +int ret;drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 578
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+/* NOTE: It's ok to read values of output lines. */ +if (copy_from_user (&lv, ip,sizeof (lv))) +return -EFAULT ; + +for (num_get =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + num_get++; + descs = &lr->lines[i].desc; + } + }So what you can do here is something like
DECLARE_BITMAP(mask, u64);...
bitmap_from_u64(mask, lv.mask);num_get=bitmap_weight(mask, lr->num_lines);
if (num_get== 0)
return-EINVAL;
for_each_set_bit(i, mask, lr->num_lines)descs = &lr->lines[i].desc;// I'm not sure I understood a purpose of the above
// ah, looks likemalloc()avoidance, but you may move it below...drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 583
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +4
-0+if (num_get ==0 ) +return -EINVAL ; + +if (num_get !=1 ) {...something like
Quoted 9 lines
if (
num_get== 1)
descs = ...[find_first_bit(mask,lr->num_lines)];
else {
...
for_each_set_bit(){
...
}
}
As per elsewhere - will give it a shot.
Quoted 48 lines
Quoted 17 lines
drivers/gpio/gpiolib-cdev.c +17
-0+ descs =kmalloc_array (num_get,sizeof (*descs),GFP_KERNEL ); +if (!descs) +return -ENOMEM ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret =gpiod_get_array_value_complex (false, true, num_get, + descs,NULL , vals); + +if (num_get !=1 ) +kfree (descs); +if (ret) +return ret;drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 609
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ + lv.bits =0 ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv.mask &BIT_ULL (i)) { +if (test_bit (didx, vals)) + lv.bits |=BIT_ULL (i); + didx++; + } + }So here...
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +5
-0+if (copy_to_user (ip, &lv,sizeof (lv))) +return -EFAULT ; + +return 0 ; +}...
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 707
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c +9
-0+/* Make sure this is terminated */ + ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ; +if (strlen (ulr.consumer)) { + lr->label =kstrdup (ulr.consumer,GFP_KERNEL ); +if (!lr->label) { + ret = -ENOMEM ; + goto out_free_linereq; + } + }
Quoted 2 lines
Still don't get why we can\t use
kstrndup()here...
I know ;-).
Another one directly from v1, and the behaviour there is to leave lr->label nulled if consumer is empty.
It just avoids a pointless malloc for the null terminator.
Cheers,
Kent.
Re: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 7 lines
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 542
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+assign_bit (FLAG_ACTIVE_LOW , flagsp, + flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW );What I meant is to attach also this to the other assign_bit():s below. And just in case a question: why not
__asign_bit()do we really need atomicity?
Quoted 5 lines
These are initialized as per their order in the flags so it is easier to tell if any are missing.
The atomicity is not required here, but it is elsewhere so you are oblidged to use it for all accesses, no?
I'm not sure. I think if you are using non-atomic in one place, it means that all automatically drop the atomicity guarantee. So, it's all or none for atomicity, for non-atomicity it's rather none or at least one. That said, code should be carefully checked before doing such.
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpio/gpiolib-cdev.c +15
-0+if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) +set_bit (FLAG_IS_OUT , flagsp); +else if (flags &GPIO_V2_LINE_FLAG_INPUT ) +clear_bit (FLAG_IS_OUT , flagsp); + +assign_bit (FLAG_OPEN_DRAIN , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); +assign_bit (FLAG_OPEN_SOURCE , flagsp, + flags &GPIO_V2_LINE_FLAG_OPEN_SOURCE ); +assign_bit (FLAG_PULL_UP , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ); +assign_bit (FLAG_PULL_DOWN , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ); +assign_bit (FLAG_BIAS_DISABLE , flagsp, + flags &GPIO_V2_LINE_FLAG_BIAS_DISABLED );
...
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c +9
-0+/* Make sure this is terminated */ + ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ; +if (strlen (ulr.consumer)) { + lr->label =kstrdup (ulr.consumer,GFP_KERNEL ); +if (!lr->label) { + ret = -ENOMEM ; + goto out_free_linereq; + } + }
Quoted 8 lines
Quoted 2 lines
Still don't get why we can\t use
kstrndup()here...I know ;-).
Another one directly from v1, and the behaviour there is to leave
lr->labelnulled if consumer is empty.
It just avoids a pointless malloc for the null terminator.
Again, similar as for bitmap API usage, if it makes code cleaner and increases readability, I will go for it.
Also don't forget the army of janitors that won't understand the case and simply convert everything that can be converted.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 15 lines
Quoted 15 lines
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 707
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c +9
-0+/* Make sure this is terminated */ + ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ; +if (strlen (ulr.consumer)) { + lr->label =kstrdup (ulr.consumer,GFP_KERNEL ); +if (!lr->label) { + ret = -ENOMEM ; + goto out_free_linereq; + } + }Quoted 2 lines
Still don't get why we can\t use
kstrndup()here...I know ;-).
Quoted 9 lines
Quoted 3 lines
Another one directly from v1, and the behaviour there is to leave
lr->labelnulled if consumer is empty.
It just avoids a pointless malloc for the null terminator.Again, similar as for bitmap API usage, if it makes code cleaner and increases readability, I will go for it.
Also don't forget the army of janitors that won't understand the case and simply convert everything that can be converted.
Hmmm, there is more to it than I thought - gpiod_request_commit(), which this code eventually calls, interprets a null label (not an empty string) as indicating that the user has not set the label, in which case it will set the desc label to "?". So userspace cannot force the desc label to be empty.
We need to keep that label as null in that case to maintain that behaviour.
I will add a comment there though.
Hmmm, having said that, does this form work for you:
if (ulr.consumer[0] != '\0') {
/* label is only initialized if consumer is set */
lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, GFP_KERNEL);
...It actually compiles slightly larger, I guess due to the extra parameter in the kstrndup() call, but may be slightly more readable??
Cheers,
Kent.
Re: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 1 line · Kent Gibson
On Fri, Sep 25, 2020 at 01:06:02PM +0300, Andy Shevchenko wrote:
...
Quoted 13 lines
Hmmm, there is more to it than I thought -
gpiod_request_commit(), which this code eventually calls, interprets a null label (not an empty string) as indicating that the user has not set the label, in which case it will set the desc label to "?". So userspace cannot force the desc label to be empty.We need to keep that label as null in that case to maintain that behaviour.
I will add a comment there though.
Hmmm, having said that, does this form work for you:
Quoted 7 lines
if (ulr.consumer[0] != '\0') { /* label is only initialized if consumer is set */ lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, GFP_KERNEL); ...It actually compiles slightly larger, I guess due to the extra parameter in the
kstrndup()call, but may be slightly more readable??
I really don't want to delay this series, choose what you think is better and we may amend it later.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
Quoted 23 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Fri, Sep 25, 2020 at 01:06:02PM +0300, Andy Shevchenko wrote:
...
Quoted 18 lines
Hmmm, there is more to it than I thought -
gpiod_request_commit(), which this code eventually calls, interprets a null label (not an empty string) as indicating that the user has not set the label, in which case it will set the desc label to "?". So userspace cannot force the desc label to be empty.We need to keep that label as null in that case to maintain that behaviour.
I will add a comment there though.
Hmmm, having said that, does this form work for you:
if (ulr.consumer[0] != '\0') { /* label is only initialized if consumer is set */ lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1, GFP_KERNEL); ...
Quoted 6 lines
Quoted 2 lines
It actually compiles slightly larger, I guess due to the extra parameter in the
kstrndup()call, but may be slightly more readable??I really don't want to delay this series, choose what you think is better and we may amend it later.
OK, as this code is common with v1 I'll leave it as is - we can always change it for all cases in a later patch.
I think that is everything outstanding for v9, so should have a v10 out shortly.
Cheers,
Kent.
Re: [PATCH v9 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL
In reply to Andy ShevchenkoQuoted 1 line · Andy Shevchenko
[snip!]
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static int linehandle_create(struct gpio_device *gdev, void __user *ip) · line 503
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +4
-0+/* Bias requires explicit direction. */ +if ((flags &GPIO_V2_LINE_BIAS_FLAGS ) && + !(flags &GPIO_V2_LINE_DIRECTION_FLAGS )) +return -EINVAL ;
Quoted 9 lines
Okay, since this is strict we probably may relax it in the future if it will be a use case.
...Quoted 1 line
+ /* Only one bias flag can be set. */
Ditto. (Some controllers allow to set both simultaneously, though I can't imagine good use case for that)
This is an abstraction layer. Only because some controllers allow this, doesn't mean we should reflect this in our abstraction layer - especially if we know well that this has no purpose.
Bartosz
Kent Gibson
reviewerPATCH v9 8/20 gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
In reply to Kent GibsonAdd support for GPIO_V2_GET_LINEINFO_IOCTL andGPIO_V2_GET_LINEINFO_WATCH_IOCTL.
The core of this change is the event kfifo switching to contain struct gpioline_info_changed_v2, instead of v1 as v2 is richer.
The two uAPI versions are mostly independent - other than where they both provide line info changes via reads on the chip fd. As the info change structs differ between v1 and v2, the infowatch implementation tracks which version of the infowatch ioctl, either GPIO_GET_LINEINFO_WATCH_IOCTL or GPIO_V2_GET_LINEINFO_WATCH_IOCTL, initiates the initial watch and returns the corresponding info change struct to the read. The version supported on that fd locks to that version on the first watch request, so subsequent watches from that process must use the same uAPI version.
---
Changes for v5:
- as per cover letter
Changes for v4:
- replace strncpy with memcpy in gpio_v2_line_info_to_v1
drivers/gpio/gpiolib-cdev.c | 197 +++++++++++++++++++++++++++++++----- 1 file changed, 169 insertions(+), 28 deletions(-)
drivers/gpio/gpiolib-cdev.c +169 -28
@@ -181,7 +181,8 @@ static long linehandle_set_config(struct linehandle_state *lh, }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_CONFIG , desc); +GPIO_V2_LINE_CHANGED_CONFIG , + desc); }return 0 ; }
@@ -353,7 +354,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc);dev_dbg (&gdev->dev,"registered chardev handle for line %d\n" , offset);
@@ -747,7 +748,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc);dev_dbg (&gdev->dev,"registered chardev handle for line %d\n" , offset);
@@ -1094,7 +1095,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) goto out_free_le;blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc); irq =gpiod_to_irq (desc);if (irq <=0 ) {
@@ -1161,17 +1162,59 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)return ret; } +static void gpio_v2_line_info_to_v1 (struct gpio_v2_line_info *info_v2, +struct gpioline_info *info_v1) +{ +u64 flagsv2 = info_v2->flags; + +memcpy (info_v1->name, info_v2->name,sizeof (info_v1->name)); +memcpy (info_v1->consumer, info_v2->consumer, +sizeof (info_v1->consumer)); + info_v1->line_offset = info_v2->offset; + info_v1->flags =0 ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_USED ) + info_v1->flags |=GPIOLINE_FLAG_KERNEL ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_OUTPUT ) + info_v1->flags |=GPIOLINE_FLAG_IS_OUT ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) + info_v1->flags |=GPIOLINE_FLAG_ACTIVE_LOW ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_OPEN_DRAIN ) + info_v1->flags |=GPIOLINE_FLAG_OPEN_DRAIN ; +if (flagsv2 &GPIO_V2_LINE_FLAG_OPEN_SOURCE ) + info_v1->flags |=GPIOLINE_FLAG_OPEN_SOURCE ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ) + info_v1->flags |=GPIOLINE_FLAG_BIAS_PULL_UP ; +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ) + info_v1->flags |=GPIOLINE_FLAG_BIAS_PULL_DOWN ; +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_DISABLED ) + info_v1->flags |=GPIOLINE_FLAG_BIAS_DISABLE ; +} + +static void gpio_v2_line_info_changed_to_v1 ( +struct gpio_v2_line_info_changed *lic_v2, +struct gpioline_info_changed *lic_v1) +{ +gpio_v2_line_info_to_v1 (&lic_v2->info, &lic_v1->info); + lic_v1->timestamp = lic_v2->timestamp_ns; + lic_v1->event_type = lic_v2->event_type; +} +#endif /* CONFIG_GPIO_CDEV_V1 */ static void gpio_desc_to_lineinfo (struct gpio_desc *desc, -struct gpioline_info *info) +struct gpio_v2_line_info *info) {struct gpio_chip *gc = desc->gdev->chip;bool ok_for_pinctrl;unsigned long flags;memset (info,0 ,sizeof (*info)); - info->line_offset =gpio_chip_hwgpio (desc); + info->offset =gpio_chip_hwgpio (desc);/* * This function takes a mutex so we must check this before taking
@@ -1181,7 +1224,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,* lock common to both frameworks? */ ok_for_pinctrl = -pinctrl_gpio_can_use_line (gc->base + info->line_offset); +pinctrl_gpio_can_use_line (gc->base + info->offset);spin_lock_irqsave (&gpio_lock, flags);
@@ -1202,23 +1245,27 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,test_bit (FLAG_EXPORT , &desc->flags) ||test_bit (FLAG_SYSFS , &desc->flags) || !ok_for_pinctrl) - info->flags |= GPIOLINE_FLAG_KERNEL; + info->flags |= GPIO_V2_LINE_FLAG_USED; +if (test_bit (FLAG_IS_OUT , &desc->flags)) - info->flags |=GPIOLINE_FLAG_IS_OUT ; + info->flags |=GPIO_V2_LINE_FLAG_OUTPUT ; +else + info->flags |=GPIO_V2_LINE_FLAG_INPUT ; +if (test_bit (FLAG_ACTIVE_LOW , &desc->flags)) - info->flags |=GPIOLINE_FLAG_ACTIVE_LOW ; + info->flags |=GPIO_V2_LINE_FLAG_ACTIVE_LOW ; +if (test_bit (FLAG_OPEN_DRAIN , &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | -GPIOLINE_FLAG_IS_OUT ); + info->flags |=GPIO_V2_LINE_FLAG_OPEN_DRAIN ;if (test_bit (FLAG_OPEN_SOURCE , &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | -GPIOLINE_FLAG_IS_OUT ); + info->flags |=GPIO_V2_LINE_FLAG_OPEN_SOURCE ; +if (test_bit (FLAG_BIAS_DISABLE , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_DISABLE ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_DISABLED ;if (test_bit (FLAG_PULL_DOWN , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_PULL_DOWN ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ;if (test_bit (FLAG_PULL_UP , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_PULL_UP ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_UP ;spin_unlock_irqrestore (&gpio_lock, flags); }
@@ -1226,11 +1273,65 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,struct gpio_chardev_data {struct gpio_device *gdev;wait_queue_head_t wait; - DECLARE_KFIFO(events, struct gpioline_info_changed, 32); + DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);struct notifier_block lineinfo_changed_nb;unsigned long *watched_lines; +#ifdef CONFIG_GPIO_CDEV_V1 +atomic_t watch_abi_version; +#endif }; +#ifdef CONFIG_GPIO_CDEV_V1 +static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) { +atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version); + } +if (abiv != version) +return -EPERM ; +return 0 ; +} +#endif + +static int lineinfo_get (struct gpio_chardev_data *cdev,void __user *ip, +bool watch) +{ +struct gpio_desc *desc; +struct gpio_v2_line_info lineinfo; + +if (copy_from_user (&lineinfo, ip,sizeof (lineinfo))) +return -EFAULT ; + +if (memchr_inv (lineinfo.padding,0 ,sizeof (lineinfo.padding))) +return -EINVAL ; + + desc =gpiochip_get_desc (cdev->gdev->chip, lineinfo.offset); +if (IS_ERR (desc)) +return PTR_ERR (desc); + +if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1 +if (lineinfo_ensure_abi_version (cdev,2 )) +return -EPERM ; +#endif +if (test_and_set_bit (lineinfo.offset, cdev->watched_lines)) +return -EBUSY ; + } +gpio_desc_to_lineinfo (desc, &lineinfo); + +if (copy_to_user (ip, &lineinfo,sizeof (lineinfo))) { +if (watch) +clear_bit (lineinfo.offset, cdev->watched_lines); +return -EFAULT ; + } + +return 0 ; +} +/* * gpio_ioctl() - ioctl handler for the GPIO chardev */
@@ -1240,7 +1341,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct gpio_device *gdev = cdev->gdev;struct gpio_chip *gc = gdev->chip;void __user *ip = (void __user *)arg; -struct gpio_desc *desc;__u32 offset;/* We fail any subsequent ioctl():s when the chip is gone */
@@ -1263,7 +1363,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return 0 ; #ifdef CONFIG_GPIO_CDEV_V1 }else if (cmd ==GPIO_GET_LINEINFO_IOCTL ) { +struct gpio_desc *desc;struct gpioline_info lineinfo; +struct gpio_v2_line_info lineinfo_v2;if (copy_from_user (&lineinfo, ip,sizeof (lineinfo)))return -EFAULT ;
@@ -1273,7 +1375,8 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (IS_ERR (desc))return PTR_ERR (desc); -gpio_desc_to_lineinfo (desc, &lineinfo); +gpio_desc_to_lineinfo (desc, &lineinfo_v2); +gpio_v2_line_info_to_v1 (&lineinfo_v2, &lineinfo);if (copy_to_user (ip, &lineinfo,sizeof (lineinfo)))return -EFAULT ;
@@ -1283,7 +1386,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) }else if (cmd ==GPIO_GET_LINEEVENT_IOCTL ) {return lineevent_create (gdev, ip); }else if (cmd ==GPIO_GET_LINEINFO_WATCH_IOCTL ) { +struct gpio_desc *desc;struct gpioline_info lineinfo; +struct gpio_v2_line_info lineinfo_v2;if (copy_from_user (&lineinfo, ip,sizeof (lineinfo)))return -EFAULT ;
@@ -1293,10 +1398,14 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (IS_ERR (desc))return PTR_ERR (desc); +if (lineinfo_ensure_abi_version (cdev,1 )) +return -EPERM ; +if (test_and_set_bit (lineinfo.line_offset, cdev->watched_lines))return -EBUSY ; -gpio_desc_to_lineinfo (desc, &lineinfo); +gpio_desc_to_lineinfo (desc, &lineinfo_v2); +gpio_v2_line_info_to_v1 (&lineinfo_v2, &lineinfo);if (copy_to_user (ip, &lineinfo,sizeof (lineinfo))) {clear_bit (lineinfo.line_offset, cdev->watched_lines);
@@ -1305,6 +1414,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return 0 ;#endif /* CONFIG_GPIO_CDEV_V1 */ + }else if (cmd ==GPIO_V2_GET_LINEINFO_IOCTL || + cmd ==GPIO_V2_GET_LINEINFO_WATCH_IOCTL ) { +return lineinfo_get (cdev, ip, + cmd ==GPIO_V2_GET_LINEINFO_WATCH_IOCTL ); }else if (cmd ==GPIO_V2_GET_LINE_IOCTL ) {return linereq_create (gdev, ip); }else if (cmd ==GPIO_GET_LINEINFO_UNWATCH_IOCTL ) {
@@ -1340,7 +1453,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); -struct gpioline_info_changed chg; +struct gpio_v2_line_info_changed chg;struct gpio_desc *desc = data;int ret;
@@ -1349,7 +1462,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb,memset (&chg,0 ,sizeof (chg)); chg.event_type = action; - chg.timestamp =ktime_get_ns (); + chg.timestamp_ns =ktime_get_ns ();gpio_desc_to_lineinfo (desc, &chg.info); ret =kfifo_in_spinlocked (&cdev->events, &chg,1 , &cdev->wait.lock);
@@ -1380,12 +1493,16 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, size_t count, loff_t *off) { struct gpio_chardev_data *cdev = file->private_data; -struct gpioline_info_changed event; +struct gpio_v2_line_info_changed event;ssize_t bytes_read =0 ;int ret; +size_t event_size; -if (count <sizeof (event)) +#ifndef CONFIG_GPIO_CDEV_V1 + event_size =sizeof (struct gpio_v2_line_info_changed ); +if (count < event_size)return -EINVAL ; +#endif do { spin_lock(&cdev->wait.lock);
@@ -1407,7 +1524,17 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,return ret; } } - +#ifdef CONFIG_GPIO_CDEV_V1 +/* must be after kfifo check so watch_abi_version is set */ +if (atomic_read (&cdev->watch_abi_version) ==2 ) + event_size =sizeof (struct gpio_v2_line_info_changed ); +else + event_size =sizeof (struct gpioline_info_changed ); +if (count < event_size) { +spin_unlock (&cdev->wait.lock); +return -EINVAL ; + } +#endif ret =kfifo_out (&cdev->events, &event,1 );spin_unlock (&cdev->wait.lock);if (ret !=1 ) {
@@ -1416,9 +1543,23 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,/* We should never get here. See lineevent_read(). */ } -if (copy_to_user (buf + bytes_read, &event,sizeof (event))) +#ifdef CONFIG_GPIO_CDEV_V1 +if (event_size ==sizeof (struct gpio_v2_line_info_changed )) { +if (copy_to_user (buf + bytes_read, &event, event_size)) +return -EFAULT ; + }else { +struct gpioline_info_changed event_v1; + +gpio_v2_line_info_changed_to_v1 (&event, &event_v1); +if (copy_to_user (buf + bytes_read, &event_v1, + event_size)) +return -EFAULT ; + } +#else +if (copy_to_user (buf + bytes_read, &event, event_size))return -EFAULT ; - bytes_read +=sizeof (event); +#endif + bytes_read += event_size; }while (count >= bytes_read +sizeof (event));return bytes_read;
Signature · 1 line
2.28.0
Re: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 72 lines · Kent Gibson
Add support for
GPIO_V2_GET_LINEINFO_IOCTLandGPIO_V2_GET_LINEINFO_WATCH_IOCTL.The core of this change is the event kfifo switching to contain struct
gpioline_info_changed_v2, instead of v1 as v2 is richer.The two uAPI versions are mostly independent - other than where they both provide line info changes via reads on the chip fd. As the info change structs differ between v1 and v2, the infowatch implementation tracks which version of the infowatch ioctl, either
GPIO_GET_LINEINFO_WATCH_IOCTLorGPIO_V2_GET_LINEINFO_WATCH_IOCTL, initiates the initial watch and returns the corresponding info change struct to the read. The version supported on that fd locks to that version on the first watch request, so subsequent watches from that process must use the same uAPI version.Signed-off-by Kent Gibson <[email protected]>---
Changes for v5:
- as per cover letterChanges for v4:
- replace strncpy with memcpy ingpio_v2_line_info_to_v1drivers/gpio/gpiolib-cdev.c | 197 +++++++++++++++++++++++++++++++----- 1 file changed, 169 insertions(+), 28 deletions(-)drivers/gpio/gpiolib-cdev.c +5
-4@@ -181,7 +181,8 @@ static long linehandle_set_config(struct linehandle_state *lh, }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_CONFIG , desc); +GPIO_V2_LINE_CHANGED_CONFIG , + desc); }return 0 ; }@@ -353,7 +354,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc);dev_dbg (&gdev->dev,"registered chardev handle for line %d\n" , offset);@@ -747,7 +748,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) }blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc);dev_dbg (&gdev->dev,"registered chardev handle for line %d\n" , offset);@@ -1094,7 +1095,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) goto out_free_le;blocking_notifier_call_chain (&desc->gdev->notifier, -GPIOLINE_CHANGED_REQUESTED , desc); +GPIO_V2_LINE_CHANGED_REQUESTED , desc); irq =gpiod_to_irq (desc);if (irq <=0 ) {@@ -1161,17 +1162,59 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)return ret; }
drivers/gpio/gpiolib-cdev.c static int lineevent_create(struct gpio_device *gdev, void __user *ip) · line 1173
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8
-0+static void gpio_v2_line_info_to_v1 (struct gpio_v2_line_info *info_v2, +struct gpioline_info *info_v1) +{ +u64 flagsv2 = info_v2->flags; + +memcpy (info_v1->name, info_v2->name,sizeof (info_v1->name)); +memcpy (info_v1->consumer, info_v2->consumer, +sizeof (info_v1->consumer));
One line?
Quoted 111 lines
drivers/gpio/gpiolib-cdev.c +36
-2+ info_v1->line_offset = info_v2->offset; + info_v1->flags =0 ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_USED ) + info_v1->flags |=GPIOLINE_FLAG_KERNEL ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_OUTPUT ) + info_v1->flags |=GPIOLINE_FLAG_IS_OUT ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) + info_v1->flags |=GPIOLINE_FLAG_ACTIVE_LOW ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_OPEN_DRAIN ) + info_v1->flags |=GPIOLINE_FLAG_OPEN_DRAIN ; +if (flagsv2 &GPIO_V2_LINE_FLAG_OPEN_SOURCE ) + info_v1->flags |=GPIOLINE_FLAG_OPEN_SOURCE ; + +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_PULL_UP ) + info_v1->flags |=GPIOLINE_FLAG_BIAS_PULL_UP ; +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ) + info_v1->flags |=GPIOLINE_FLAG_BIAS_PULL_DOWN ; +if (flagsv2 &GPIO_V2_LINE_FLAG_BIAS_DISABLED ) + info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; +} + +static void gpio_v2_line_info_changed_to_v1 ( +struct gpio_v2_line_info_changed *lic_v2, +struct gpioline_info_changed *lic_v1) +{ +gpio_v2_line_info_to_v1 (&lic_v2->info, &lic_v1->info); + lic_v1->timestamp = lic_v2->timestamp_ns; + lic_v1->event_type = lic_v2->event_type; +} +#endif /* CONFIG_GPIO_CDEV_V1 */ static void gpio_desc_to_lineinfo (struct gpio_desc *desc, -struct gpioline_info *info) +struct gpio_v2_line_info *info) {struct gpio_chip *gc = desc->gdev->chip;bool ok_for_pinctrl;unsigned long flags;memset (info,0 ,sizeof (*info)); - info->line_offset =gpio_chip_hwgpio (desc); + info->offset =gpio_chip_hwgpio (desc);/* * This function takes a mutex so we must check this before taking
drivers/gpio/gpiolib-cdev.c +20
-12@@ -1181,7 +1224,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,* lock common to both frameworks? */ ok_for_pinctrl = -pinctrl_gpio_can_use_line (gc->base + info->line_offset); +pinctrl_gpio_can_use_line (gc->base + info->offset);spin_lock_irqsave (&gpio_lock, flags);@@ -1202,23 +1245,27 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,test_bit (FLAG_EXPORT , &desc->flags) ||test_bit (FLAG_SYSFS , &desc->flags) || !ok_for_pinctrl) - info->flags |= GPIOLINE_FLAG_KERNEL; + info->flags |= GPIO_V2_LINE_FLAG_USED; +if (test_bit (FLAG_IS_OUT , &desc->flags)) - info->flags |=GPIOLINE_FLAG_IS_OUT ; + info->flags |=GPIO_V2_LINE_FLAG_OUTPUT ; +else + info->flags |=GPIO_V2_LINE_FLAG_INPUT ; +if (test_bit (FLAG_ACTIVE_LOW , &desc->flags)) - info->flags |=GPIOLINE_FLAG_ACTIVE_LOW ; + info->flags |=GPIO_V2_LINE_FLAG_ACTIVE_LOW ; +if (test_bit (FLAG_OPEN_DRAIN , &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | -GPIOLINE_FLAG_IS_OUT ); + info->flags |=GPIO_V2_LINE_FLAG_OPEN_DRAIN ;if (test_bit (FLAG_OPEN_SOURCE , &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | -GPIOLINE_FLAG_IS_OUT ); + info->flags |=GPIO_V2_LINE_FLAG_OPEN_SOURCE ; +if (test_bit (FLAG_BIAS_DISABLE , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_DISABLE ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_DISABLED ;if (test_bit (FLAG_PULL_DOWN , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_PULL_DOWN ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN ;if (test_bit (FLAG_PULL_UP , &desc->flags)) - info->flags |=GPIOLINE_FLAG_BIAS_PULL_UP ; + info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_UP ;spin_unlock_irqrestore (&gpio_lock, flags); }@@ -1226,11 +1273,65 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,struct gpio_chardev_data {struct gpio_device *gdev;wait_queue_head_t wait; - DECLARE_KFIFO(events, struct gpioline_info_changed, 32); + DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);struct notifier_block lineinfo_changed_nb;unsigned long *watched_lines; +#ifdef CONFIG_GPIO_CDEV_V1 +atomic_t watch_abi_version; +#endif }; +#ifdef CONFIG_GPIO_CDEV_V1
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1294
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8
-0+static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) { +atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng() returns a value.
Also there are no barriers here...
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;
I'm not sure I understand why this is atomic.
Also this seems to be racy if cdata changed in background.
Shouldn't be rather
if (atomic_cmpxchg() == 0) {
if (atomic_read() != version)
return ...; }
But here is still the question: why do you expect the version to be changed on background? And what about barriers?
Quoted 15 lines
drivers/gpio/gpiolib-cdev.c +15
-0+ return 0; +} +#endif + +static int lineinfo_get (struct gpio_chardev_data *cdev,void __user *ip, +bool watch) +{ +struct gpio_desc *desc; +struct gpio_v2_line_info lineinfo; + +if (copy_from_user (&lineinfo, ip,sizeof (lineinfo))) +return -EFAULT ; + +if (memchr_inv (lineinfo.padding,0 ,sizeof (lineinfo.padding))) +return -EINVAL ;
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1319
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ + desc =gpiochip_get_desc (cdev->gdev->chip, lineinfo.offset); +if (IS_ERR (desc)) +return PTR_ERR (desc); + +if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1 +if (lineinfo_ensure_abi_version (cdev,2 )) +return -EPERM ;
Can't you propagate error code from the function?
Quoted 166 lines
drivers/gpio/gpiolib-cdev.c +15
-0+#endif +if (test_and_set_bit (lineinfo.offset, cdev->watched_lines)) +return -EBUSY ; + } +gpio_desc_to_lineinfo (desc, &lineinfo); + +if (copy_to_user (ip, &lineinfo,sizeof (lineinfo))) { +if (watch) +clear_bit (lineinfo.offset, cdev->watched_lines); +return -EFAULT ; + } + +return 0 ; +} +/*
*gpio_ioctl()- ioctl handler for the GPIO chardev */drivers/gpio/gpiolib-cdev.c +50
-11@@ -1240,7 +1341,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct gpio_device *gdev = cdev->gdev;struct gpio_chip *gc = gdev->chip;void __user *ip = (void __user *)arg; -struct gpio_desc *desc;__u32 offset;/* We fail any subsequent ioctl():s when the chip is gone */ @@ -1263,7 +1363,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return 0 ; #ifdef CONFIG_GPIO_CDEV_V1 }else if (cmd ==GPIO_GET_LINEINFO_IOCTL ) { +struct gpio_desc *desc;struct gpioline_info lineinfo; +struct gpio_v2_line_info lineinfo_v2;if (copy_from_user (&lineinfo, ip,sizeof (lineinfo)))return -EFAULT ;@@ -1273,7 +1375,8 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (IS_ERR (desc))return PTR_ERR (desc); -gpio_desc_to_lineinfo (desc, &lineinfo); +gpio_desc_to_lineinfo (desc, &lineinfo_v2); +gpio_v2_line_info_to_v1 (&lineinfo_v2, &lineinfo);if (copy_to_user (ip, &lineinfo,sizeof (lineinfo)))return -EFAULT ;@@ -1283,7 +1386,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) }else if (cmd ==GPIO_GET_LINEEVENT_IOCTL ) {return lineevent_create (gdev, ip); }else if (cmd ==GPIO_GET_LINEINFO_WATCH_IOCTL ) { +struct gpio_desc *desc;struct gpioline_info lineinfo; +struct gpio_v2_line_info lineinfo_v2;if (copy_from_user (&lineinfo, ip,sizeof (lineinfo)))return -EFAULT ;@@ -1293,10 +1398,14 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (IS_ERR (desc))return PTR_ERR (desc); +if (lineinfo_ensure_abi_version (cdev,1 )) +return -EPERM ; +if (test_and_set_bit (lineinfo.line_offset, cdev->watched_lines))return -EBUSY ; -gpio_desc_to_lineinfo (desc, &lineinfo); +gpio_desc_to_lineinfo (desc, &lineinfo_v2); +gpio_v2_line_info_to_v1 (&lineinfo_v2, &lineinfo);if (copy_to_user (ip, &lineinfo,sizeof (lineinfo))) {clear_bit (lineinfo.line_offset, cdev->watched_lines);@@ -1305,6 +1414,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return 0 ;#endif /* CONFIG_GPIO_CDEV_V1 */ + }else if (cmd ==GPIO_V2_GET_LINEINFO_IOCTL || + cmd ==GPIO_V2_GET_LINEINFO_WATCH_IOCTL ) { +return lineinfo_get (cdev, ip, + cmd ==GPIO_V2_GET_LINEINFO_WATCH_IOCTL ); }else if (cmd ==GPIO_V2_GET_LINE_IOCTL ) {return linereq_create (gdev, ip); }else if (cmd ==GPIO_GET_LINEINFO_UNWATCH_IOCTL ) {@@ -1340,7 +1453,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); -struct gpioline_info_changed chg; +struct gpio_v2_line_info_changed chg;struct gpio_desc *desc = data;int ret;@@ -1349,7 +1462,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb,memset (&chg,0 ,sizeof (chg)); chg.event_type = action; - chg.timestamp =ktime_get_ns (); + chg.timestamp_ns =ktime_get_ns ();gpio_desc_to_lineinfo (desc, &chg.info); ret =kfifo_in_spinlocked (&cdev->events, &chg,1 , &cdev->wait.lock);@@ -1380,12 +1493,16 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, size_t count, loff_t *off) { struct gpio_chardev_data *cdev = file->private_data; -struct gpioline_info_changed event; +struct gpio_v2_line_info_changed event;ssize_t bytes_read =0 ;int ret; +size_t event_size; -if (count <sizeof (event)) +#ifndef CONFIG_GPIO_CDEV_V1 + event_size =sizeof (struct gpio_v2_line_info_changed ); +if (count < event_size)return -EINVAL ; +#endif do { spin_lock(&cdev->wait.lock);@@ -1407,7 +1524,17 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,return ret; } } - +#ifdef CONFIG_GPIO_CDEV_V1 +/* must be after kfifo check so watch_abi_version is set */ +if (atomic_read (&cdev->watch_abi_version) ==2 ) + event_size =sizeof (struct gpio_v2_line_info_changed ); +else + event_size =sizeof (struct gpioline_info_changed ); +if (count < event_size) { +spin_unlock (&cdev->wait.lock); +return -EINVAL ; + } +#endif ret =kfifo_out (&cdev->events, &event,1 );spin_unlock (&cdev->wait.lock);if (ret !=1 ) {@@ -1416,9 +1543,23 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,/* We should never get here. See lineevent_read(). */ } -if (copy_to_user (buf + bytes_read, &event,sizeof (event))) +#ifdef CONFIG_GPIO_CDEV_V1 +if (event_size ==sizeof (struct gpio_v2_line_info_changed )) { +if (copy_to_user (buf + bytes_read, &event, event_size)) +return -EFAULT ; + }else { +struct gpioline_info_changed event_v1; + +gpio_v2_line_info_changed_to_v1 (&event, &event_v1); +if (copy_to_user (buf + bytes_read, &event_v1, + event_size)) +return -EFAULT ; + } +#else +if (copy_to_user (buf + bytes_read, &event, event_size))return -EFAULT ; - bytes_read +=sizeof (event); +#endif + bytes_read += event_size; }while (count >= bytes_read +sizeof (event));return bytes_read; --
Quoted 2 lines
2.28.0
--
With Best Regards,
Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for
GPIO_V2_GET_LINEINFO_IOCTLandGPIO_V2_GET_LINEINFO_WATCH_IOCTL.
[snip]
Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static void gpio_v2_line_info_to_v1 (struct gpio_v2_line_info *info_v2, +struct gpioline_info *info_v1) +{ +u64 flagsv2 = info_v2->flags; + +memcpy (info_v1->name, info_v2->name,sizeof (info_v1->name));
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static int lineevent_create(struct gpio_device *gdev, void __user *ip) · line 1172
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+memcpy (info_v1->consumer, info_v2->consumer, +sizeof (info_v1->consumer));One line?
It can be now the line length limit has been raised - it just breaks the old 80 character limit.
[snip]
Quoted 6 lines
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +4
-0+#ifdef CONFIG_GPIO_CDEV_V1 +static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1292
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +5
-0+int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) { +atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng()returns a value.
Yep, it returns the old value - which we don't care about - see below.
Quoted 2 lines
Also there are no barriers here...
No barriers required - the atomic_cmpxchg() is sufficient.
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;I'm not sure I understand why this is atomic.
The algorithm requires some level of protection and atomic is sufficient.
Quoted 2 lines
Also this seems to be racy if cdata changed in background.
Can you provide a case?
The atomic_cmpxchg() ensures cdata->watch_abi_version is only set once - first in wins. The atomic_read() is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.
Quoted 7 lines
Shouldn't be rather
if (
atomic_cmpxchg()== 0) {
if (atomic_read()!= version)return ...; }
My algorithm allows for multiple callers requesting the same version to all succeed. Yours would fail the first conditional for all but the first, and you haven't provided an else for that case...
... but it would probably look the same so the conditional is pointless, or it would reject the request - which would be wrong.
Quoted 3 lines
But here is still the question: why do you expect the version to be changed on background? And what about barriers?
While it is unlikely that userspace will be attempting to use both ABI versions simultaneously on the same chip request, it is a possiblity and so needs to be protected against. And better to have the watch request fail than the read fail or worse - return the wrong struct version.
The atomic_cmpxchg() is sufficient for this algorithm - no barriers required. It could also be written with a spinlock but I was trying to avoid locks unless they were absolutely necessary. A spinlock version may arguably be more readable, but it would certainly be more verbose, larger and slower.
I'm happy to add some documentation to the function if that would help.
Quoted 23 lines
Quoted 22 lines
drivers/gpio/gpiolib-cdev.c +22
-0+ return 0; +} +#endif + +static int lineinfo_get (struct gpio_chardev_data *cdev,void __user *ip, +bool watch) +{ +struct gpio_desc *desc; +struct gpio_v2_line_info lineinfo; + +if (copy_from_user (&lineinfo, ip,sizeof (lineinfo))) +return -EFAULT ; + +if (memchr_inv (lineinfo.padding,0 ,sizeof (lineinfo.padding))) +return -EINVAL ; + + desc =gpiochip_get_desc (cdev->gdev->chip, lineinfo.offset); +if (IS_ERR (desc)) +return PTR_ERR (desc); + +if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1319
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+if (lineinfo_ensure_abi_version (cdev,2 )) +return -EPERM ;Can't you propagate error code from the function?
You mean:
+ ret = lineinfo_ensure_abi_version(cdev, 2)
+ if (ret)
+ return ret;
That seems more verbose and less clear. And I'd need to conditionally declare a ret - as this test is compiled out if CDEV_V1 is not defined.
I did flip-flop on what lineinfo_ensure_abi_version() should return - either a bool or an error code.
If a bool then the code would include the dreaded negative conditional ;-(:
+ if (!lineinfo_is_abi_version(cdev, 2))
+ return -EPERM;
so I eventually settled for the error code. But I'm on the fence on this one and happy to change it if you think the bool form is clearer.
Cheers,
Kent.
Re: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 8 lines
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static int lineevent_create(struct gpio_device *gdev, void __user *ip) · line 1172
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+memcpy (info_v1->consumer, info_v2->consumer, +sizeof (info_v1->consumer));One line?
It can be now the line length limit has been raised - it just breaks the old 80 character limit.
I really wouldn't care about this if it's only for a couple of characters.
...
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) {
Quoted 6 lines
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1292
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng()returns a value.Yep, it returns the old value - which we don't care about - see below.
Then what's the point to read back?..
Quoted 12 lines
Quoted 2 lines
Also there are no barriers here...
No barriers required - the
atomic_cmpxchg()is sufficient.Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;I'm not sure I understand why this is atomic.
Quoted 7 lines
The algorithm requires some level of protection and atomic is sufficient.
Quoted 2 lines
Also this seems to be racy if cdata changed in background.
Can you provide a case?
CPU0: CPU1:
xchg() ...
... xchg()
... read() -> OKread() ->NOK
Quoted 5 lines
The
atomic_cmpxchg()ensurescdata->watch_abi_versionis only set once - first in wins. Theatomic_read()is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.
So, that's basically what you need when using _old_ value.
0 means you were first, right?
Anything else you simply compare and bail out if it's not the same as what has been asked.
Quoted 25 lines
Quoted 8 lines
Shouldn't be rather
if (
atomic_cmpxchg()== 0) {
if (atomic_read()!= version)return ...; }My algorithm allows for multiple callers requesting the same version to all succeed. Yours would fail the first conditional for all but the first, and you haven't provided an else for that case...
... but it would probably look the same so the conditional is pointless, or it would reject the request - which would be wrong.
Quoted 3 lines
But here is still the question: why do you expect the version to be changed on background? And what about barriers?
While it is unlikely that userspace will be attempting to use both ABI versions simultaneously on the same chip request, it is a possiblity and so needs to be protected against. And better to have the watch request fail than the read fail or worse - return the wrong struct version.
Quoted 7 lines
The
atomic_cmpxchg()is sufficient for this algorithm - no barriers required. It could also be written with a spinlock but I was trying to avoid locks unless they were absolutely necessary. A spinlock version may arguably be more readable, but it would certainly be more verbose, larger and slower.I'm happy to add some documentation to the function if that would help.
Yes, I guess documentation is what is eagerly needed here.
Quoted 51 lines
Quoted 23 lines
Quoted 22 lines
drivers/gpio/gpiolib-cdev.c +22
-0+ return 0; +} +#endif + +static int lineinfo_get (struct gpio_chardev_data *cdev,void __user *ip, +bool watch) +{ +struct gpio_desc *desc; +struct gpio_v2_line_info lineinfo; + +if (copy_from_user (&lineinfo, ip,sizeof (lineinfo))) +return -EFAULT ; + +if (memchr_inv (lineinfo.padding,0 ,sizeof (lineinfo.padding))) +return -EINVAL ; + + desc =gpiochip_get_desc (cdev->gdev->chip, lineinfo.offset); +if (IS_ERR (desc)) +return PTR_ERR (desc); + +if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1 Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1319
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+if (lineinfo_ensure_abi_version (cdev,2 )) +return -EPERM ;Can't you propagate error code from the function?
You mean:
(quoted diff) +3
-0+ ret = lineinfo_ensure_abi_version(cdev, 2) + if (ret) + return ret;That seems more verbose and less clear. And I'd need to conditionally declare a ret - as this test is compiled out if
CDEV_V1is not defined.I did flip-flop on what
lineinfo_ensure_abi_version()should return - either a bool or an error code.If a bool then the code would include the dreaded negative conditional ;-(:
(quoted diff) +2
-0+ if (!lineinfo_is_abi_version(cdev, 2)) + return -EPERM;so I eventually settled for the error code. But I'm on the fence on this one and happy to change it if you think the bool form is clearer.
Cheers,
Kent.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
[snip]
Quoted 36 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) {Quoted 6 lines
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1292
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng()returns a value.Yep, it returns the old value - which we don't care about - see below.
Then what's the point to read back?..
Quoted 18 lines
Quoted 2 lines
Also there are no barriers here...
No barriers required - the
atomic_cmpxchg()is sufficient.Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;I'm not sure I understand why this is atomic.
The algorithm requires some level of protection and atomic is sufficient.
Quoted 2 lines
Also this seems to be racy if cdata changed in background.
Quoted 8 lines
Quoted 1 line
Can you provide a case?
CPU0: CPU1:
xchg()...
...xchg()
...read()-> OKread()->NOK
Lets say CPU0 is setting 1 and CPU1 setting 2, and assuming the xchg() completes...
Your case is not possible - CPU1 would see the value 1 set by CPU0 in the read() and so NOK. Its xchg() would fail as it compares against 0 and that also sees the 1 and so fails.
What am I missing?
Quoted 6 lines
Quoted 5 lines
The
atomic_cmpxchg()ensurescdata->watch_abi_versionis only set once - first in wins. Theatomic_read()is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.
Quoted 6 lines
So, that's basically what you need when using _old_ value.
0 means you were first, right?
Anything else you simply compare and bail out if it's not the same as what has been asked.
Could you provide a complete implementation that behaves as I expect, rather than snippets and verbage?
Quoted 31 lines
Quoted 31 lines
Quoted 8 lines
Shouldn't be rather
if (
atomic_cmpxchg()== 0) {
if (atomic_read()!= version)return ...; }My algorithm allows for multiple callers requesting the same version to all succeed. Yours would fail the first conditional for all but the first, and you haven't provided an else for that case...
... but it would probably look the same so the conditional is pointless, or it would reject the request - which would be wrong.
Quoted 3 lines
But here is still the question: why do you expect the version to be changed on background? And what about barriers?
While it is unlikely that userspace will be attempting to use both ABI versions simultaneously on the same chip request, it is a possiblity and so needs to be protected against. And better to have the watch request fail than the read fail or worse - return the wrong struct version.
The
atomic_cmpxchg()is sufficient for this algorithm - no barriers required. It could also be written with a spinlock but I was trying to avoid locks unless they were absolutely necessary. A spinlock version may arguably be more readable, but it would certainly be more verbose, larger and slower.
Quoted 4 lines
Quoted 1 line
I'm happy to add some documentation to the function if that would help.
Yes, I guess documentation is what is eagerly needed here.
No problem.
Cheers,
Kent.
Re: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 4 lines · Kent Gibson
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 44 lines
Quoted 35 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) {Quoted 6 lines
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1292
Quoted 2 lines
drivers/gpio/gpiolib-cdev.c +2
-0+atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng()returns a value.Yep, it returns the old value - which we don't care about - see below.
Then what's the point to read back?..
Quoted 18 lines
Quoted 2 lines
Also there are no barriers here...
No barriers required - the
atomic_cmpxchg()is sufficient.Quoted 6 lines
drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;I'm not sure I understand why this is atomic.
The algorithm requires some level of protection and atomic is sufficient.
Quoted 2 lines
Also this seems to be racy if cdata changed in background.
Quoted 8 lines
Quoted 1 line
Can you provide a case?
CPU0: CPU1:
xchg()...
...xchg()
...read()-> OKread()->NOK
Quoted 7 lines
Lets say CPU0 is setting 1 and CPU1 setting 2, and assuming the
xchg()completes...
Your case is not possible - CPU1 would see the value 1 set by CPU0 in theread()and so NOK. Itsxchg()would fail as it compares against 0 and that also sees the 1 and so fails.What am I missing?
Barriers? That's what documentation says about xchg().
https://stackoverflow.com/q/20950603/2511795
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
The
atomic_cmpxchg()ensurescdata->watch_abi_versionis only set once - first in wins. Theatomic_read()is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.So, that's basically what you need when using _old_ value.
Quoted 7 lines
Quoted 4 lines
0 means you were first, right?
Anything else you simply compare and bail out if it's not the same as what has been asked.Could you provide a complete implementation that behaves as I expect, rather than snippets and verbage?
if (atomic_cmpxchg(&cdata..., version) == 0)
return 0; // we were first!
return -EPERM; // somebody has changed the version before us!
Quoted 36 lines
Quoted 30 lines
Quoted 30 lines
Quoted 7 lines
Shouldn't be rather
if (
atomic_cmpxchg()== 0) {
if (atomic_read()!= version)return ...; }My algorithm allows for multiple callers requesting the same version to all succeed. Yours would fail the first conditional for all but the first, and you haven't provided an else for that case...
... but it would probably look the same so the conditional is pointless, or it would reject the request - which would be wrong.
Quoted 3 lines
But here is still the question: why do you expect the version to be changed on background? And what about barriers?
While it is unlikely that userspace will be attempting to use both ABI versions simultaneously on the same chip request, it is a possiblity and so needs to be protected against. And better to have the watch request fail than the read fail or worse - return the wrong struct version.
The
atomic_cmpxchg()is sufficient for this algorithm - no barriers required. It could also be written with a spinlock but I was trying to avoid locks unless they were absolutely necessary. A spinlock version may arguably be more readable, but it would certainly be more verbose, larger and slower.Quoted 4 lines
Quoted 1 line
I'm happy to add some documentation to the function if that would help.
Yes, I guess documentation is what is eagerly needed here.
No problem.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
[snip]
Quoted 7 lines
Quoted 7 lines
Lets say CPU0 is setting 1 and CPU1 setting 2, and assuming the
xchg()completes...
Your case is not possible - CPU1 would see the value 1 set by CPU0 in theread()and so NOK. Itsxchg()would fail as it compares against 0 and that also sees the 1 and so fails.
Quoted 5 lines
Quoted 1 line
What am I missing?
Barriers? That's what documentation says about
xchg().
https://stackoverflow.com/q/20950603/2511795
Firstly, the answer in Stackoverflow is from someone who explicitly acknowledges not being a kernel developer, so they aren't sure.
Secondly, the latest version of the kernel doc [1] says differently than what is quoted on Stackoverlow - it indicates implementations of atomic_cmpxchg() must provide its own memory barriers.
The relevant section says "This performs an atomic compare exchange operation on the atomic value v, with the given old and new values. Like all atomic_xxx operations, atomic_cmpxchg will only satisfy its atomicity semantics as long as all other accesses of *v are performed through atomic_xxx operations.
atomic_cmpxchg must provide explicit memory barriers around the operation, although if the comparison fails then no memory ordering guarantees are required."
Note that this doc is aimed at atomic_cmpxchg() implementors, so I took that to mean the operation itself must provide the barriers - not the caller. Also, the sentence only makes sense wrt the atomic_cmpxchg() implementation - the caller can't decide on memory barriers if the comparison fails or not.
The memory-barriers.txt they quote is also dated - the atomic section they quote is moved to atomic_t.txt[2]?
That says that cmpxchg is a RMW op, and that it will perform an ACQUIRE and RELEASE - for the non-failure case anyway.
Again, I took that to mean it will provide the barriers itself.
And even the old text they quote says those operations IMPLY a memory barrier, "Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (smp_mb()) on each side of the actual operation" and that "the implicit memory barrier effects are necessary".
Again that indicates the barrier is a part of the op, as it is implicit, and not necessary to be added separately.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 5 lines
The
atomic_cmpxchg()ensurescdata->watch_abi_versionis only set once - first in wins. Theatomic_read()is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.So, that's basically what you need when using _old_ value.
0 means you were first, right?
Anything else you simply compare and bail out if it's not the same as what has been asked.
Quoted 7 lines
Quoted 2 lines
Could you provide a complete implementation that behaves as I expect, rather than snippets and verbage?
if (
atomic_cmpxchg(&cdata..., version)== 0)
return 0; // we were first!
return-EPERM; // somebody has changed the version before us!
Which can fail if two callers are requesting the same version - in a race the second one will get a fail - independent of the version they are requesting.
I keep flip-flopping and twiddling with the implementation of this - my current one is:
/*
* returns 0 if the versions match, else the previously selected ABI version */
static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
unsigned int version)
{
int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
if (abiv == version)
return 0;
return abiv;
}Does that work for you? (assuming no explicit barriers are necessary)
Cheers,
Kent.
[1] https://www.kernel.org/doc/html/v5.8/core-api/atomic_ops.html
[2] https://www.kernel.org/doc/Documentation/atomic_t.txt
Re: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
Quoted 51 lines · Kent Gibson
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
[snip]
Quoted 7 lines
Quoted 7 lines
Lets say CPU0 is setting 1 and CPU1 setting 2, and assuming the
xchg()completes...
Your case is not possible - CPU1 would see the value 1 set by CPU0 in theread()and so NOK. Itsxchg()would fail as it compares against 0 and that also sees the 1 and so fails.Quoted 5 lines
Quoted 1 line
What am I missing?
Barriers? That's what documentation says about
xchg().
https://stackoverflow.com/q/20950603/2511795Firstly, the answer in Stackoverflow is from someone who explicitly acknowledges not being a kernel developer, so they aren't sure.
Secondly, the latest version of the kernel doc [1] says differently than what is quoted on Stackoverlow - it indicates implementations of
atomic_cmpxchg()must provide its own memory barriers.The relevant section says "This performs an atomic compare exchange operation on the atomic value v, with the given old and new values. Like all
atomic_xxxoperations,atomic_cmpxchgwill only satisfy its atomicity semantics as long as all other accesses of *v are performed throughatomic_xxxoperations.
atomic_cmpxchgmust provide explicit memory barriers around the operation, although if the comparison fails then no memory ordering guarantees are required."Note that this doc is aimed at
atomic_cmpxchg()implementors, so I took that to mean the operation itself must provide the barriers - not the caller. Also, the sentence only makes sense wrt theatomic_cmpxchg()implementation - the caller can't decide on memory barriers if the comparison fails or not.The memory-barriers.txt they quote is also dated - the atomic section they quote is moved to atomic_t.txt[2]?
That says that cmpxchg is a RMW op, and that it will perform an ACQUIRE and RELEASE - for the non-failure case anyway.Again, I took that to mean it will provide the barriers itself.
Quoted 8 lines
And even the old text they quote says those operations IMPLY a memory barrier, "Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (
smp_mb()) on each side of the actual operation" and that "the implicit memory barrier effects are necessary".Again that indicates the barrier is a part of the op, as it is implicit, and not necessary to be added separately.
Okay!
Thanks for digging into it.
Quoted 43 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 5 lines
The
atomic_cmpxchg()ensurescdata->watch_abi_versionis only set once - first in wins. Theatomic_read()is so we can check that the set version matches what the caller wants.
Note that multiple callers may request the same version - and all should succeed.So, that's basically what you need when using _old_ value.
0 means you were first, right?
Anything else you simply compare and bail out if it's not the same as what has been asked.Quoted 7 lines
Quoted 2 lines
Could you provide a complete implementation that behaves as I expect, rather than snippets and verbage?
if (
atomic_cmpxchg(&cdata..., version)== 0)
return 0; // we were first!
return-EPERM; // somebody has changed the version before us!Which can fail if two callers are requesting the same version - in a race the second one will get a fail - independent of the version they are requesting.
I keep flip-flopping and twiddling with the implementation of this - my current one is:
/*
* returns 0 if the versions match, else the previously selected ABI version */
static int lineinfo_ensure_abi_version(structgpio_chardev_data*cdata,
unsigned int version){ int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); if (abiv == version) return 0; return abiv; }
Quoted 1 line
Does that work for you? (assuming no explicit barriers are necessary)
Perfectly!
Quoted 2 lines
[1] https://www.kernel.org/doc/html/v5.8/core-api/atomic_ops.html
[2] https://www.kernel.org/doc/Documentation/atomic_t.txt
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL
In reply to Andy ShevchenkoQuoted 26 lines · Andy Shevchenko
Quoted 5 lines · Kent Gibson
Add support for
GPIO_V2_GET_LINEINFO_IOCTLandGPIO_V2_GET_LINEINFO_WATCH_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+#ifdef CONFIG_GPIO_CDEV_V1 drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1292
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8
-0+static int lineinfo_ensure_abi_version (struct gpio_chardev_data *cdata, +unsigned int version) +{ +int abiv =atomic_read (&cdata->watch_abi_version); + +if (abiv ==0 ) { +atomic_cmpxchg (&cdata->watch_abi_version,0 , version); + abiv =atomic_read (&cdata->watch_abi_version);
atomic_cmpxchng()returns a value.
Also there are no barriers here...drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 1295
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ } +if (abiv != version) +return -EPERM ;I'm not sure I understand why this is atomic.
Quoted 9 lines
Also this seems to be racy if cdata changed in background.
Shouldn't be rather
if (
atomic_cmpxchg()== 0) {
if (atomic_read()!= version)return ...; }
Perhaps this is what you had in mind:
static bool lineinfo_is_abi_version(struct gpio_chardev_data *cdata,
unsigned int version)
{
int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
if (abiv == version)
return true;
if (abiv == 0)
/* first (and only) setter sees initial 0 value */
return true;
return false;
}That is functionally equivalent, but slightly shorter.
I was thinking the atomic_cmpxchg() is more expensive than an atomic_read(), and so initially checked the value with
that, but for the cmp failure case it is probably the same, or at least not worth the bother - this isn't even vaguely near a hot path.
I've also switched the return value to bool in response to your other comments.
Cheers,
Kent.
Kent Gibson
reviewerPATCH v9 9/20 gpiolib: cdev: support edge detection for uAPI v2
In reply to Kent GibsonAdd support for edge detection to lines requested using GPIO_V2_GET_LINE_IOCTL.
The edge detector implementation is based on the v1 lineevent implementation.
Unlike the v1 implementation, an overflow of the event buffer results in discarding older events, rather than the most recent, so the final event in a burst will correspond to the current state of the line.
---
The linereq_put_event() helper is only used once here, but is re-used in subsequent patches, and so is pre-emptively split out.
edge_detector_stop() is extended in subsequent patches, and is strucured to suit those additions.
drivers/gpio/gpiolib-cdev.c | 275 ++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 2 + drivers/gpio/gpiolib.h | 2 + 3 files changed, 279 insertions(+)
drivers/gpio/gpiolib-cdev.c +275 -0
@@ -404,9 +404,33 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)/** * struct line - contains the state of a requested line * @desc: the GPIO descriptor for this line. +* @req: the corresponding line request +* @irq: the interrupt triggered in response to events on this GPIO +* @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or +* GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied +* @timestamp_ns: cache for the timestamp storing it between hardirq and +* IRQ thread, used to bring the timestamp close to the actual event +* @req_seqno: the seqno for the current edge event in the sequence of +* events for the corresponding line request. This is drawn from the @req. +* @line_seqno: the seqno for the current edge event in the sequence of +* events for this line. */ struct line {struct gpio_desc *desc; +/* +* -- edge detector specific fields -- +*/ +struct linereq *req; +unsigned int irq; +u64 eflags; +/* +* timestamp_ns and req_seqno are accessed only by +* edge_irq_handler() and edge_irq_thread(), which are themselves +* mutually exclusive, so no additional protection is necessary. +*/ +u64 timestamp_ns; +u32 req_seqno; +u32 line_seqno; };/**
@@ -414,12 +438,22 @@ struct line {* @gdev: the GPIO device the line request pertains to * @label: consumer label used to tag GPIO descriptors * @num_lines: the number of lines in the lines array +* @wait: wait queue that handles blocking reads of events +* @event_buffer_size: the number of elements allocated in @events +* @events: KFIFO for the GPIO events +* @seqno: the sequence number for edge events generated on all lines in +* this line request. Note that this is not used when @num_lines is 1, as +* the line_seqno is then the same and is cheaper to calculate. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq {struct gpio_device *gdev;const char *label;u32 num_lines; +wait_queue_head_t wait; +u32 event_buffer_size; + DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); +atomic_t seqno;struct line lines[]; };
@@ -436,12 +470,150 @@ struct linereq { (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ GPIO_V2_LINE_FLAG_OPEN_SOURCE) +#define GPIO_V2_LINE_EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) +#define GPIO_V2_LINE_VALID_FLAGS \ (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ GPIO_V2_LINE_DIRECTION_FLAGS | \ GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_EDGE_FLAGS | \ GPIO_V2_LINE_BIAS_FLAGS) +static void linereq_put_event (struct linereq *lr, +struct gpio_v2_line_event *le) +{ +bool overflow = false; + +spin_lock (&lr->wait.lock); +if (kfifo_is_full (&lr->events)) { + overflow = true; +kfifo_skip (&lr->events); + } +kfifo_in (&lr->events, le,1 ); +spin_unlock (&lr->wait.lock); +if (!overflow) +wake_up_poll (&lr->wait,EPOLLIN ); +else +pr_debug_ratelimited ("event FIFO is full - event dropped\n" ); +} + +static irqreturn_t edge_irq_thread (int irq,void *p) +{ +struct line *line = p; +struct linereq *lr = line->req; +struct gpio_v2_line_event le; + +/* Do not leak kernel stack to userspace */ +memset (&le,0 ,sizeof (le)); + +/* +* We may be running from a nested threaded interrupt in which case +* we didn't get the timestamp from edge_irq_handler(). +*/ +if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns; + } + line->timestamp_ns =0 ; + +if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | +GPIO_V2_LINE_FLAG_EDGE_FALLING )) { +int level =gpiod_get_value_cansleep (line->desc); + +if (level) +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; +else +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + }else if (line->eflags ==GPIO_V2_LINE_FLAG_EDGE_RISING ) { +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; + }else if (line->eflags ==GPIO_V2_LINE_FLAG_EDGE_FALLING ) { +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + }else { +return IRQ_NONE ; + } + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines ==1 ) ? le.line_seqno : line->req_seqno; + le.offset =gpio_chip_hwgpio (line->desc); + +linereq_put_event (lr, &le); + +return IRQ_HANDLED ; +} + +static irqreturn_t edge_irq_handler (int irq,void *p) +{ +struct line *line = p; +struct linereq *lr = line->req; + +/* +* Just store the timestamp in hardirq context so we get it as +* close in time as possible to the actual event. +*/ + line->timestamp_ns =ktime_get_ns (); + +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + +return IRQ_WAKE_THREAD ; +} + +static void edge_detector_stop (struct line *line) +{ +if (line->irq) { +free_irq (line->irq, line); + line->irq =0 ; + } +} + +static int edge_detector_setup (struct line *line, +u64 eflags) +{ +unsigned long irqflags =0 ; +int irq, ret; + +if (eflags && !kfifo_initialized (&line->req->events)) { + ret =kfifo_alloc (&line->req->events, + line->req->event_buffer_size,GFP_KERNEL ); +if (ret) +return ret; + } + line->eflags = eflags; + +if (!eflags) +return 0 ; + + irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ; + +if (eflags &GPIO_V2_LINE_FLAG_EDGE_RISING ) + irqflags |=test_bit (FLAG_ACTIVE_LOW , &line->desc->flags) ? +IRQF_TRIGGER_FALLING :IRQF_TRIGGER_RISING ; +if (eflags &GPIO_V2_LINE_FLAG_EDGE_FALLING ) + irqflags |=test_bit (FLAG_ACTIVE_LOW , &line->desc->flags) ? +IRQF_TRIGGER_RISING :IRQF_TRIGGER_FALLING ; + irqflags |=IRQF_ONESHOT ; + +/* Request a thread to read the events */ + ret =request_threaded_irq (irq, edge_irq_handler, edge_irq_thread, + irqflags, line->req->label, line); +if (ret) +return ret; + + line->irq = irq; +return 0 ; +} +static u64 gpio_v2_line_config_flags (struct gpio_v2_line_config *lc,unsigned int line_idx) {
@@ -484,6 +656,11 @@ static int gpio_v2_line_flags_validate(u64 flags) (flags & GPIO_V2_LINE_FLAG_OUTPUT))return -EINVAL ; +/* Edge detection requires explicit input. */ +if ((flags &GPIO_V2_LINE_EDGE_FLAGS ) && + !(flags &GPIO_V2_LINE_FLAG_INPUT )) +return -EINVAL ; +/* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the
@@ -547,6 +724,10 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,else if (flags &GPIO_V2_LINE_FLAG_INPUT )clear_bit (FLAG_IS_OUT , flagsp); +assign_bit (FLAG_EDGE_RISING , flagsp, + flags &GPIO_V2_LINE_FLAG_EDGE_RISING ); +assign_bit (FLAG_EDGE_FALLING , flagsp, + flags &GPIO_V2_LINE_FLAG_EDGE_FALLING );assign_bit (FLAG_OPEN_DRAIN , flagsp, flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); assign_bit(FLAG_OPEN_SOURCE, flagsp,
@@ -635,14 +816,85 @@ static long linereq_ioctl_compat(struct file *file, unsigned int cmd, }#endif +static __poll_t linereq_poll (struct file *file, +struct poll_table_struct *wait) +{ +struct linereq *lr = file->private_data; +__poll_t events =0 ; + +poll_wait (file, &lr->wait, wait); + +if (!kfifo_is_empty_spinlocked_noirqsave (&lr->events, + &lr->wait.lock)) + events =EPOLLIN |EPOLLRDNORM ; + +return events; +} + +static ssize_t linereq_read (struct file *file, +char __user *buf, +size_t count, +loff_t *f_ps) +{ +struct linereq *lr = file->private_data; +struct gpio_v2_line_event le; +ssize_t bytes_read =0 ; +int ret; + +if (count <sizeof (le)) +return -EINVAL ; + +do { +spin_lock (&lr->wait.lock); +if (kfifo_is_empty (&lr->events)) { +if (bytes_read) { +spin_unlock (&lr->wait.lock); +return bytes_read; + } + +if (file->f_flags &O_NONBLOCK ) { +spin_unlock (&lr->wait.lock); +return -EAGAIN ; + } + + ret =wait_event_interruptible_locked (lr->wait, + !kfifo_is_empty (&lr->events)); +if (ret) { +spin_unlock (&lr->wait.lock); +return ret; + } + } + + ret =kfifo_out (&lr->events, &le,1 ); +spin_unlock (&lr->wait.lock); +if (ret !=1 ) { +/* +* This should never happen - we were holding the +* lock from the moment we learned the fifo is no +* longer empty until now. +*/ + ret = -EIO ; +break ; + } + +if (copy_to_user (buf + bytes_read, &le,sizeof (le))) +return -EFAULT ; + bytes_read +=sizeof (le); + }while (count >= bytes_read +sizeof (le)); + +return bytes_read; +} +static void linereq_free (struct linereq *lr) {unsigned int i;for (i =0 ; i < lr->num_lines; i++) { +edge_detector_stop (&lr->lines[i]);if (lr->lines[i].desc)gpiod_free (lr->lines[i].desc); } +kfifo_free (&lr->events);kfree (lr->label);put_device (&lr->gdev->dev);kfree (lr);
@@ -658,6 +910,8 @@ static int linereq_release(struct inode *inode, struct file *file)static const struct file_operations line_fileops = { .release = linereq_release, + .read = linereq_read, + .poll = linereq_poll, .owner =THIS_MODULE , .llseek = noop_llseek, .unlocked_ioctl = linereq_ioctl,
@@ -697,6 +951,9 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev;get_device (&gdev->dev); +for (i =0 ; i < ulr.num_lines; i++) + lr->lines[i].req = lr; +/* Make sure this is terminated */ ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ;if (strlen (ulr.consumer)) {
@@ -707,6 +964,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } +init_waitqueue_head (&lr->wait); + lr->event_buffer_size = ulr.event_buffer_size; +if (lr->event_buffer_size ==0 ) + lr->event_buffer_size = ulr.num_lines *16 ; +else if (lr->event_buffer_size >GPIO_V2_LINES_MAX *16 ) + lr->event_buffer_size =GPIO_V2_LINES_MAX *16 ; + +atomic_set (&lr->seqno,0 ); lr->num_lines = ulr.num_lines;/* Request each GPIO */
@@ -745,6 +1010,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) ret =gpiod_direction_input (desc);if (ret) goto out_free_linereq; + + ret =edge_detector_setup (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS ); +if (ret) + goto out_free_linereq; } blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1267,6 +1537,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,if (test_bit (FLAG_PULL_UP , &desc->flags)) info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_UP ; +if (test_bit (FLAG_EDGE_RISING , &desc->flags)) + info->flags |=GPIO_V2_LINE_FLAG_EDGE_RISING ; +if (test_bit (FLAG_EDGE_FALLING , &desc->flags)) + info->flags |=GPIO_V2_LINE_FLAG_EDGE_FALLING ; +spin_unlock_irqrestore (&gpio_lock, flags); }
drivers/gpio/gpiolib.c +2 -0
@@ -2092,6 +2092,8 @@ static bool gpiod_free_commit(struct gpio_desc *desc)clear_bit (FLAG_PULL_UP , &desc->flags);clear_bit (FLAG_PULL_DOWN , &desc->flags);clear_bit (FLAG_BIAS_DISABLE , &desc->flags); +clear_bit (FLAG_EDGE_RISING , &desc->flags); +clear_bit (FLAG_EDGE_FALLING , &desc->flags);clear_bit (FLAG_IS_HOGGED , &desc->flags);#ifdef CONFIG_OF_DYNAMIC desc->hog =NULL ;
drivers/gpio/gpiolib.h +2 -0
@@ -114,6 +114,8 @@ struct gpio_desc {#define FLAG_PULL_UP 13/* GPIO has pull up enabled */ #define FLAG_PULL_DOWN 14/* GPIO has pull down enabled */ #define FLAG_BIAS_DISABLE 15/* GPIO has pull disabled */ +#define FLAG_EDGE_RISING 16/* GPIO CDEV detects rising edge events */ +#define FLAG_EDGE_FALLING 17/* GPIO CDEV detects falling edge events */ /* Connection label */ const char *label;
Signature · 1 line
2.28.0
Re: [PATCH v9 09/20] gpiolib: cdev: support edge detection for uAPI v2
Quoted 11 lines · Kent Gibson
Add support for edge detection to lines requested using
GPIO_V2_GET_LINE_IOCTL.The edge detector implementation is based on the v1 lineevent implementation.
Unlike the v1 implementation, an overflow of the event buffer results in discarding older events, rather than the most recent, so the final event in a burst will correspond to the current state of the line.
Quoted 7 lines
Signed-off-by Kent Gibson <[email protected]>---
The
linereq_put_event()helper is only used once here, but is re-used in subsequent patches, and so is pre-emptively split out.
edge_detector_stop()is extended in subsequent patches, and is strucured
structured
Quoted 91 lines
to suit those additions.
drivers/gpio/gpiolib-cdev.c | 275 ++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 2 + drivers/gpio/gpiolib.h | 2 + 3 files changed, 279 insertions(+)drivers/gpio/gpiolib-cdev.c +47
-0@@ -404,9 +404,33 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)/** * struct line - contains the state of a requested line * @desc: the GPIO descriptor for this line. +* @req: the corresponding line request +* @irq: the interrupt triggered in response to events on this GPIO +* @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or +* GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied +* @timestamp_ns: cache for the timestamp storing it between hardirq and +* IRQ thread, used to bring the timestamp close to the actual event +* @req_seqno: the seqno for the current edge event in the sequence of +* events for the corresponding line request. This is drawn from the @req. +* @line_seqno: the seqno for the current edge event in the sequence of +* events for this line. */ struct line {struct gpio_desc *desc; +/* +* -- edge detector specific fields -- +*/ +struct linereq *req; +unsigned int irq; +u64 eflags; +/* +* timestamp_ns and req_seqno are accessed only by +* edge_irq_handler() and edge_irq_thread(), which are themselves +* mutually exclusive, so no additional protection is necessary. +*/ +u64 timestamp_ns; +u32 req_seqno; +u32 line_seqno; };/** @@ -414,12 +438,22 @@ struct line {* @gdev: the GPIO device the line request pertains to * @label: consumer label used to tag GPIO descriptors * @num_lines: the number of lines in the lines array +* @wait: wait queue that handles blocking reads of events +* @event_buffer_size: the number of elements allocated in @events +* @events: KFIFO for the GPIO events +* @seqno: the sequence number for edge events generated on all lines in +* this line request. Note that this is not used when @num_lines is 1, as +* the line_seqno is then the same and is cheaper to calculate. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq {struct gpio_device *gdev;const char *label;u32 num_lines; +wait_queue_head_t wait; +u32 event_buffer_size; + DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); +atomic_t seqno;struct line lines[]; };@@ -436,12 +470,150 @@ struct linereq { (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ GPIO_V2_LINE_FLAG_OPEN_SOURCE) +#define GPIO_V2_LINE_EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) +#define GPIO_V2_LINE_VALID_FLAGS \ (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ GPIO_V2_LINE_DIRECTION_FLAGS | \ GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_EDGE_FLAGS | \ GPIO_V2_LINE_BIAS_FLAGS) +static void linereq_put_event (struct linereq *lr, +struct gpio_v2_line_event *le) +{ +bool overflow = false; + +spin_lock (&lr->wait.lock); +if (kfifo_is_full (&lr->events)) { + overflow = true;
drivers/gpio/gpiolib-cdev.c struct linereq { · line 501
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8
-0+kfifo_skip (&lr->events); + } +kfifo_in (&lr->events, le,1 ); +spin_unlock (&lr->wait.lock); +if (!overflow) +wake_up_poll (&lr->wait,EPOLLIN ); +else +pr_debug_ratelimited ("event FIFO is full - event dropped\n" );
Under positive conditionals I meant something like this
if (overflow)
pr_debug_ratelimited("event FIFO is full - event dropped\n");
else
wake_up_poll(&lr->wait, EPOLLIN);Quoted 12 lines
drivers/gpio/gpiolib-cdev.c +11
-0+} + +static irqreturn_t edge_irq_thread (int irq,void *p) +{ +struct line *line = p; +struct linereq *lr = line->req; +struct gpio_v2_line_event le; + +/* Do not leak kernel stack to userspace */ +memset (&le,0 ,sizeof (le)); +/*
drivers/gpio/gpiolib-cdev.c struct linereq { · line 521
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+* We may be running from a nested threaded interrupt in which case +* we didn't get the timestamp from edge_irq_handler(). +*/ +if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns; + }
Ditto.
Quoted 66 lines
drivers/gpio/gpiolib-cdev.c +66
-0+ line->timestamp_ns =0 ; + +if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | +GPIO_V2_LINE_FLAG_EDGE_FALLING )) { +int level =gpiod_get_value_cansleep (line->desc); + +if (level) +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; +else +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + }else if (line->eflags ==GPIO_V2_LINE_FLAG_EDGE_RISING ) { +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; + }else if (line->eflags ==GPIO_V2_LINE_FLAG_EDGE_FALLING ) { +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + }else { +return IRQ_NONE ; + } + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines ==1 ) ? le.line_seqno : line->req_seqno; + le.offset =gpio_chip_hwgpio (line->desc); + +linereq_put_event (lr, &le); + +return IRQ_HANDLED ; +} + +static irqreturn_t edge_irq_handler (int irq,void *p) +{ +struct line *line = p; +struct linereq *lr = line->req; + +/* +* Just store the timestamp in hardirq context so we get it as +* close in time as possible to the actual event. +*/ + line->timestamp_ns =ktime_get_ns (); + +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + +return IRQ_WAKE_THREAD ; +} + +static void edge_detector_stop (struct line *line) +{ +if (line->irq) { +free_irq (line->irq, line); + line->irq =0 ; + } +} + +static int edge_detector_setup (struct line *line, +u64 eflags) +{ +unsigned long irqflags =0 ; +int irq, ret; + +if (eflags && !kfifo_initialized (&line->req->events)) { + ret =kfifo_alloc (&line->req->events, + line->req->event_buffer_size,GFP_KERNEL ); +if (ret)
drivers/gpio/gpiolib-cdev.c struct linereq { · line 597
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+return ret; + } + line->eflags = eflags; + +if (!eflags) +return 0 ; + + irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ;
So, you mean this is part of ABI. Can we return more appropriate code, because getting no IRQ doesn't mean we don't have a device. Also does 0 case have the same meaning?
Quoted 217 lines
drivers/gpio/gpiolib-cdev.c +121
-0+if (eflags &GPIO_V2_LINE_FLAG_EDGE_RISING ) + irqflags |=test_bit (FLAG_ACTIVE_LOW , &line->desc->flags) ? +IRQF_TRIGGER_FALLING :IRQF_TRIGGER_RISING ; +if (eflags &GPIO_V2_LINE_FLAG_EDGE_FALLING ) + irqflags |=test_bit (FLAG_ACTIVE_LOW , &line->desc->flags) ? +IRQF_TRIGGER_RISING :IRQF_TRIGGER_FALLING ; + irqflags |=IRQF_ONESHOT ; + +/* Request a thread to read the events */ + ret =request_threaded_irq (irq, edge_irq_handler, edge_irq_thread, + irqflags, line->req->label, line); +if (ret) +return ret; + + line->irq = irq; +return 0 ; +} +static u64 gpio_v2_line_config_flags (struct gpio_v2_line_config *lc,unsigned int line_idx) {@@ -484,6 +656,11 @@ static int gpio_v2_line_flags_validate(u64 flags) (flags & GPIO_V2_LINE_FLAG_OUTPUT))return -EINVAL ; +/* Edge detection requires explicit input. */ +if ((flags &GPIO_V2_LINE_EDGE_FLAGS ) && + !(flags &GPIO_V2_LINE_FLAG_INPUT )) +return -EINVAL ; +/* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the @@ -547,6 +724,10 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,else if (flags &GPIO_V2_LINE_FLAG_INPUT )clear_bit (FLAG_IS_OUT , flagsp); +assign_bit (FLAG_EDGE_RISING , flagsp, + flags &GPIO_V2_LINE_FLAG_EDGE_RISING ); +assign_bit (FLAG_EDGE_FALLING , flagsp, + flags &GPIO_V2_LINE_FLAG_EDGE_FALLING );assign_bit (FLAG_OPEN_DRAIN , flagsp, flags &GPIO_V2_LINE_FLAG_OPEN_DRAIN ); assign_bit(FLAG_OPEN_SOURCE, flagsp,@@ -635,14 +816,85 @@ static long linereq_ioctl_compat(struct file *file, unsigned int cmd, }#endif +static __poll_t linereq_poll (struct file *file, +struct poll_table_struct *wait) +{ +struct linereq *lr = file->private_data; +__poll_t events =0 ; + +poll_wait (file, &lr->wait, wait); + +if (!kfifo_is_empty_spinlocked_noirqsave (&lr->events, + &lr->wait.lock)) + events =EPOLLIN |EPOLLRDNORM ; + +return events; +} + +static ssize_t linereq_read (struct file *file, +char __user *buf, +size_t count, +loff_t *f_ps) +{ +struct linereq *lr = file->private_data; +struct gpio_v2_line_event le; +ssize_t bytes_read =0 ; +int ret; + +if (count <sizeof (le)) +return -EINVAL ; + +do { +spin_lock (&lr->wait.lock); +if (kfifo_is_empty (&lr->events)) { +if (bytes_read) { +spin_unlock (&lr->wait.lock); +return bytes_read; + } + +if (file->f_flags &O_NONBLOCK ) { +spin_unlock (&lr->wait.lock); +return -EAGAIN ; + } + + ret =wait_event_interruptible_locked (lr->wait, + !kfifo_is_empty (&lr->events)); +if (ret) { +spin_unlock (&lr->wait.lock); +return ret; + } + } + + ret =kfifo_out (&lr->events, &le,1 ); +spin_unlock (&lr->wait.lock); +if (ret !=1 ) { +/* +* This should never happen - we were holding the +* lock from the moment we learned the fifo is no +* longer empty until now. +*/ + ret = -EIO ; +break ; + } + +if (copy_to_user (buf + bytes_read, &le,sizeof (le))) +return -EFAULT ; + bytes_read +=sizeof (le); + }while (count >= bytes_read +sizeof (le)); + +return bytes_read; +} +static void linereq_free (struct linereq *lr) {unsigned int i;for (i =0 ; i < lr->num_lines; i++) { +edge_detector_stop (&lr->lines[i]);if (lr->lines[i].desc)gpiod_free (lr->lines[i].desc); } +kfifo_free (&lr->events);kfree (lr->label);put_device (&lr->gdev->dev);kfree (lr);@@ -658,6 +910,8 @@ static int linereq_release(struct inode *inode, struct file *file)static const struct file_operations line_fileops = { .release = linereq_release, + .read = linereq_read, + .poll = linereq_poll, .owner =THIS_MODULE , .llseek = noop_llseek, .unlocked_ioctl = linereq_ioctl,@@ -697,6 +951,9 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev;get_device (&gdev->dev); +for (i =0 ; i < ulr.num_lines; i++) + lr->lines[i].req = lr; +/* Make sure this is terminated */ ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ;if (strlen (ulr.consumer)) {@@ -707,6 +964,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } +init_waitqueue_head (&lr->wait); + lr->event_buffer_size = ulr.event_buffer_size; +if (lr->event_buffer_size ==0 ) + lr->event_buffer_size = ulr.num_lines *16 ; +else if (lr->event_buffer_size >GPIO_V2_LINES_MAX *16 ) + lr->event_buffer_size =GPIO_V2_LINES_MAX *16 ; + +atomic_set (&lr->seqno,0 ); lr->num_lines = ulr.num_lines;/* Request each GPIO */ @@ -745,6 +1010,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) ret =gpiod_direction_input (desc);if (ret) goto out_free_linereq; + + ret =edge_detector_setup (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS ); +if (ret) + goto out_free_linereq; } blocking_notifier_call_chain(&desc->gdev->notifier,@@ -1267,6 +1537,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,if (test_bit (FLAG_PULL_UP , &desc->flags)) info->flags |=GPIO_V2_LINE_FLAG_BIAS_PULL_UP ; +if (test_bit (FLAG_EDGE_RISING , &desc->flags)) + info->flags |=GPIO_V2_LINE_FLAG_EDGE_RISING ; +if (test_bit (FLAG_EDGE_FALLING , &desc->flags)) + info->flags |=GPIO_V2_LINE_FLAG_EDGE_FALLING ; +spin_unlock_irqrestore (&gpio_lock, flags); }drivers/gpio/gpiolib.c +2
-0@@ -2092,6 +2092,8 @@ static bool gpiod_free_commit(struct gpio_desc *desc)clear_bit (FLAG_PULL_UP , &desc->flags);clear_bit (FLAG_PULL_DOWN , &desc->flags);clear_bit (FLAG_BIAS_DISABLE , &desc->flags); +clear_bit (FLAG_EDGE_RISING , &desc->flags); +clear_bit (FLAG_EDGE_FALLING , &desc->flags);clear_bit (FLAG_IS_HOGGED , &desc->flags);#ifdef CONFIG_OF_DYNAMIC desc->hog =NULL ;drivers/gpio/gpiolib.h +2
-1@@ -114,6 +114,8 @@ struct gpio_desc {#define FLAG_PULL_UP 13/* GPIO has pull up enabled */ #define FLAG_PULL_DOWN 14/* GPIO has pull down enabled */ #define FLAG_BIAS_DISABLE 15/* GPIO has pull disabled */ +#define FLAG_EDGE_RISING 16/* GPIO CDEV detects rising edge events */ +#define FLAG_EDGE_FALLING 17/* GPIO CDEV detects falling edge events */ /* Connection label */ const char *label; --2.28.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 09/20] gpiolib: cdev: support edge detection for uAPI v2
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for edge detection to lines requested using
GPIO_V2_GET_LINE_IOCTL.
[snip]
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c struct linereq { · line 499
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4
-0+if (!overflow) +wake_up_poll (&lr->wait,EPOLLIN ); +else +pr_debug_ratelimited ("event FIFO is full - event dropped\n" );
Quoted 7 lines
Under positive conditionals I meant something like this
if (overflow) pr_debug_ratelimited("event FIFO is full - event dropped\n"); else wake_up_poll(&lr->wait, EPOLLIN);
Ahh, ok. I tend to stick with the more normal path being first, and the overflow is definitely the abnormal path.
Also, this code is drawn from lineevent_irq_thread(), which is ordered this way.
Quoted 21 lines
Quoted 21 lines
drivers/gpio/gpiolib-cdev.c +20
-0+} + +static irqreturn_t edge_irq_thread (int irq,void *p) +{ +struct line *line = p; +struct linereq *lr = line->req; +struct gpio_v2_line_event le; + +/* Do not leak kernel stack to userspace */ +memset (&le,0 ,sizeof (le)); +/* +* We may be running from a nested threaded interrupt in which case +* we didn't get the timestamp from edge_irq_handler(). +*/ +if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns;
Quoted 3 lines
Quoted 1 line
Quoted 1 line
+ }
Ditto.
Firstly, drawn from lineevent_irq_thread() which is structured this way.
In this case the comment relates to the condition being true, so re-ordering the if/else would be confusing - unless the comment were moved into the corresponding body??
[snip]
Quoted 16 lines
Quoted 16 lines
drivers/gpio/gpiolib-cdev.c +16
-0+static int edge_detector_setup (struct line *line, +u64 eflags) +{ +unsigned long irqflags =0 ; +int irq, ret; + +if (eflags && !kfifo_initialized (&line->req->events)) { + ret =kfifo_alloc (&line->req->events, + line->req->event_buffer_size,GFP_KERNEL ); +if (ret) +return ret; + } + line->eflags = eflags; + +if (!eflags) +return 0 ;
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c struct linereq { · line 597
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c +4
-0+ + irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ;So, you mean this is part of ABI. Can we return more appropriate code, because getting no IRQ doesn't mean we don't have a device. Also does 0 case have the same meaning?
Firstly, this code is drawn from lineevent_create(), so any changes here should be considered for there as well - though this may constitute an ABI change??
I agree ENODEV doesn't seem right here. Are you ok with ENXIO?
From gpiod_to_irq():
/* Zero means NO_IRQ */ if (!retirq) return -ENXIO;
so it can't even return a 0 :-| - we're just being cautious.
Cheers,
Kent.
Re: [PATCH v9 09/20] gpiolib: cdev: support edge detection for uAPI v2
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 2 lines
Also, this code is drawn from
lineevent_irq_thread(), which is ordered this way.
Negative conditionals are slightly harder to read.
...
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns;Quoted 1 line
+ }
Quoted 7 lines
Quoted 1 line
Ditto.
Firstly, drawn from
lineevent_irq_thread()which is structured this way.In this case the comment relates to the condition being true, so re-ordering the if/else would be confusing - unless the comment were moved into the corresponding body??
Yes.
...
Quoted 11 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c struct linereq { · line 597
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ;So, you mean this is part of ABI. Can we return more appropriate code, because getting no IRQ doesn't mean we don't have a device. Also does 0 case have the same meaning?
Firstly, this code is drawn from
lineevent_create(), so any changes here should be considered for there as well - though this may constitute an ABI change??
For v1 probably, for v2 we are free to fix this.
Quoted 1 line
I agree ENODEV doesn't seem right here. Are you ok with ENXIO?
Yes.
Quoted 7 lines
From
gpiod_to_irq():/* Zero means NO_IRQ */ if (!retirq) return -ENXIO;so it can't even return a 0 :-| - we're just being cautious.
I would drop = part then.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 09/20] gpiolib: cdev: support edge detection for uAPI v2
Quoted 23 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 2 lines
Also, this code is drawn from
lineevent_irq_thread(), which is ordered this way.Negative conditionals are slightly harder to read.
...
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns;Quoted 1 line
+ }
Ditto.
Quoted 8 lines
Quoted 5 lines
Firstly, drawn from
lineevent_irq_thread()which is structured this way.In this case the comment relates to the condition being true, so re-ordering the if/else would be confusing - unless the comment were moved into the corresponding body??
Yes.
Does that mean I should re-order and move the comment into the body? That would work for me - the normal case is line->timestamp_ns being set.
Quoted 14 lines
...
Quoted 11 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c struct linereq { · line 597
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ;So, you mean this is part of ABI. Can we return more appropriate code, because getting no IRQ doesn't mean we don't have a device. Also does 0 case have the same meaning?
Firstly, this code is drawn from
lineevent_create(), so any changes here should be considered for there as well - though this may constitute an ABI change??
Quoted 6 lines
For v1 probably, for v2 we are free to fix this.
Quoted 1 line
I agree ENODEV doesn't seem right here. Are you ok with ENXIO?
Yes.
Will do. And in the debounce patch as well.
Quoted 10 lines
Quoted 7 lines
From
gpiod_to_irq():/* Zero means NO_IRQ */ if (!retirq) return -ENXIO;so it can't even return a 0 :-| - we're just being cautious.
I would drop = part then.
ok, but you'd better not come after me in a subsequent review for not checking the 0 case!
Cheers,
Kent.
Re: [PATCH v9 09/20] gpiolib: cdev: support edge detection for uAPI v2
Quoted 4 lines · Kent Gibson
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 9 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +6
-0+if (!line->timestamp_ns) { + le.timestamp_ns =ktime_get_ns (); +if (lr->num_lines !=1 ) + line->req_seqno =atomic_inc_return (&lr->seqno); + }else { + le.timestamp_ns = line->timestamp_ns;Quoted 1 line
+ }
Ditto.
Firstly, drawn from
lineevent_irq_thread()which is structured this way.In this case the comment relates to the condition being true, so re-ordering the if/else would be confusing - unless the comment were moved into the corresponding body??
Quoted 6 lines
Quoted 2 lines
Yes.
Does that mean I should re-order and move the comment into the body? That would work for me - the normal case is
line->timestamp_nsbeing set.
Yes, that's what I meant.
...
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
From
gpiod_to_irq():/* Zero means NO_IRQ */ if (!retirq) return -ENXIO;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
so it can't even return a 0 :-| - we're just being cautious.
I would drop = part then.
ok, but you'd better not come after me in a subsequent review for not checking the 0 case!
For IRQ?! Maybe if I'll be drunk (quite unlikely).
I really don't like to check IRQ against 0. To me it should be transparent to the caller. If IRQ == 0 in certain API or entirely in Linux is considered NO_IRQ, then it should be either correctly handled (means all following actions on it shouldn't fail, or it shouldn't be returned in the first place).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerPATCH v9 10/20 gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
In reply to Kent GibsonAdd support for GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.
--- drivers/gpio/gpiolib-cdev.c | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+)
drivers/gpio/gpiolib-cdev.c +88 -0
@@ -16,6 +16,7 @@#include <linux/kernel.h> #include <linux/kfifo.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/pinctrl/consumer.h> #include <linux/poll.h> #include <linux/spinlock.h>
@@ -444,6 +445,8 @@ struct line {* @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. +* @config_mutex: mutex for serializing ioctl() calls to ensure consistency +* of configuration, particularly multi-step accesses to desc flags. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq {
@@ -454,6 +457,7 @@ struct linereq {u32 event_buffer_size; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);atomic_t seqno; +struct mutex config_mutex;struct line lines[]; };
@@ -573,6 +577,8 @@ static void edge_detector_stop(struct line *line)free_irq (line->irq, line); line->irq =0 ; } + + line->eflags =0 ; } static int edge_detector_setup(struct line *line,
@@ -614,6 +620,17 @@ static int edge_detector_setup(struct line *line,return 0 ; } +static int edge_detector_update (struct line *line,u64 eflags, +bool polarity_change) +{ +if ((line->eflags == eflags) && !polarity_change) +return 0 ; + +edge_detector_stop (line); + +return edge_detector_setup (line, eflags); +} +static u64 gpio_v2_line_config_flags (struct gpio_v2_line_config *lc,unsigned int line_idx) {
@@ -796,6 +813,74 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)return 0 ; } +static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags; +bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i); + polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 )); + +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + +edge_detector_stop (&lr->lines[i]); + ret =gpiod_direction_output (desc, val); +if (ret) +return ret; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) +return ret; + + ret =edge_detector_update (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS , + polarity_change); +if (ret) +return ret; + } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIO_V2_LINE_CHANGED_CONFIG , + desc); + } +return 0 ; +} + +static long linereq_set_config (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_config lc; +int ret; + +if (copy_from_user (&lc, ip,sizeof (lc))) +return -EFAULT ; + + ret =gpio_v2_line_config_validate (&lc, lr->num_lines); +if (ret) +return ret; + +mutex_lock (&lr->config_mutex); + + ret =linereq_set_config_unlocked (lr, &lc); + +mutex_unlock (&lr->config_mutex); + +return ret; +} +static long linereq_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {
@@ -804,6 +889,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd,if (cmd ==GPIO_V2_LINE_GET_VALUES_IOCTL )return linereq_get_values (lr, ip); +else if (cmd ==GPIO_V2_LINE_SET_CONFIG_IOCTL ) +return linereq_set_config (lr, ip);return -EINVAL ; }
@@ -964,6 +1051,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } +mutex_init (&lr->config_mutex);init_waitqueue_head (&lr->wait); lr->event_buffer_size = ulr.event_buffer_size;if (lr->event_buffer_size ==0 )
Signature · 1 line
2.28.0
Re: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 75 lines · Kent Gibson
Add support for
GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.Signed-off-by Kent Gibson <[email protected]>--- drivers/gpio/gpiolib-cdev.c | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+)drivers/gpio/gpiolib-cdev.c +23
-0@@ -16,6 +16,7 @@#include <linux/kernel.h> #include <linux/kfifo.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/pinctrl/consumer.h> #include <linux/poll.h> #include <linux/spinlock.h> @@ -444,6 +445,8 @@ struct line {* @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. +* @config_mutex: mutex for serializing ioctl() calls to ensure consistency +* of configuration, particularly multi-step accesses to desc flags. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq {@@ -454,6 +457,7 @@ struct linereq {u32 event_buffer_size; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);atomic_t seqno; +struct mutex config_mutex;struct line lines[]; };@@ -573,6 +577,8 @@ static void edge_detector_stop(struct line *line)free_irq (line->irq, line); line->irq =0 ; } + + line->eflags =0 ; } static int edge_detector_setup(struct line *line,@@ -614,6 +620,17 @@ static int edge_detector_setup(struct line *line,return 0 ; } +static int edge_detector_update (struct line *line,u64 eflags, +bool polarity_change) +{ +if ((line->eflags == eflags) && !polarity_change) +return 0 ; + +edge_detector_stop (line); + +return edge_detector_setup (line, eflags); +} +static u64 gpio_v2_line_config_flags (struct gpio_v2_line_config *lc,unsigned int line_idx) {@@ -796,6 +813,74 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)return 0 ; } +static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags;
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 831
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i); + polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));
Comparison
Quoted 74 lines
drivers/gpio/gpiolib-cdev.c +53
-0+ +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + +edge_detector_stop (&lr->lines[i]); + ret =gpiod_direction_output (desc, val); +if (ret) +return ret; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) +return ret; + + ret =edge_detector_update (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS , + polarity_change); +if (ret) +return ret; + } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIO_V2_LINE_CHANGED_CONFIG , + desc); + } +return 0 ; +} + +static long linereq_set_config (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_config lc; +int ret; + +if (copy_from_user (&lc, ip,sizeof (lc))) +return -EFAULT ; + + ret =gpio_v2_line_config_validate (&lc, lr->num_lines); +if (ret) +return ret; + +mutex_lock (&lr->config_mutex); + + ret =linereq_set_config_unlocked (lr, &lc); + +mutex_unlock (&lr->config_mutex); + +return ret; +} +static long linereq_ioctl (struct file *file,unsigned int cmd,unsigned long arg){
drivers/gpio/gpiolib-cdev.c +3
-1@@ -804,6 +889,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd,if (cmd ==GPIO_V2_LINE_GET_VALUES_IOCTL )return linereq_get_values (lr, ip); +else if (cmd ==GPIO_V2_LINE_SET_CONFIG_IOCTL ) +return linereq_set_config (lr, ip);return -EINVAL ; }@@ -964,6 +1051,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } +mutex_init (&lr->config_mutex);init_waitqueue_head (&lr->wait); lr->event_buffer_size = ulr.event_buffer_size;if (lr->event_buffer_size ==0 ) --
Quoted 2 lines
2.28.0
--
With Best Regards,
Andy Shevchenko
Re: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 19 lines · Andy Shevchenko
Quoted 16 lines · Kent Gibson
Add support for
GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.drivers/gpio/gpiolib-cdev.c +12
-0+static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags; +bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i);
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 830
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));Comparison
Comparison between int / long (not all archs are agreed on this) and boolean is not the best we can do.
What about
bool old_polarity = test_bit(FLAG_ACTIVE_LOW, &desc->flags); bool new_polarity = flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW;
old_polarity ^ new_polarity
and move this under INPUT conditional?
Quoted 31 lines
Quoted 31 lines
drivers/gpio/gpiolib-cdev.c +31
-0+ +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + +edge_detector_stop (&lr->lines[i]); + ret =gpiod_direction_output (desc, val); +if (ret) +return ret; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) +return ret; + + ret =edge_detector_update (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS , + polarity_change); +if (ret) +return ret; + } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIO_V2_LINE_CHANGED_CONFIG , + desc); + } +return 0 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 21 lines · Andy Shevchenko
Quoted 19 lines · Andy Shevchenko
Quoted 16 lines · Kent Gibson
Add support for
GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.drivers/gpio/gpiolib-cdev.c +12
-0+static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags; +bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i);
Quoted 9 lines
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 830
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));Comparison
Comparison between int / long (not all archs are agreed on this) and boolean is not the best we can do.
There is no bool to int comparision here.
There are two comparisons - the inner int vs int => bool and the outer bool vs bool. The "!= 0" is effectively an implicit cast to bool, as is your new_polarity initialisation below.
Quoted 7 lines
What about
bool old_polarity = test_bit(FLAG_ACTIVE_LOW, &desc->flags); bool new_polarity = flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW;
old_polarity^new_polarity
So using bitwise operators on bools is ok??
Quoted 2 lines
and move this under INPUT conditional?
It has to be before the gpio_v2_line_config_flags_to_desc_flags() call, as that modifies the desc flags, including the new polarity, so polarity_change would then always be false :-).
Cheers,
Kent.
Quoted 35 lines
Quoted 31 lines
Quoted 31 lines
drivers/gpio/gpiolib-cdev.c +31
-0+ +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + +edge_detector_stop (&lr->lines[i]); + ret =gpiod_direction_output (desc, val); +if (ret) +return ret; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) +return ret; + + ret =edge_detector_update (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS , + polarity_change); +if (ret) +return ret; + } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIO_V2_LINE_CHANGED_CONFIG , + desc); + } +return 0 ; +}Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 25 lines · Kent Gibson
Quoted 24 lines · Andy Shevchenko
Quoted 22 lines · Andy Shevchenko
Quoted 20 lines · Kent Gibson
Add support for
GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.drivers/gpio/gpiolib-cdev.c +15
-0+static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags; +bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i); + polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Comparison
Comparison between int / long (not all archs are agreed on this) and boolean is not the best we can do.
There is no bool to int comparision here.
test_bit() returns int or long depending on arch... Then you compare it to bool (which is a product of != 0).
Quoted 4 lines
There are two comparisons - the inner int vs int => bool and the outer bool vs bool. The "!= 0" is effectively an implicit cast to bool, as is your
new_polarityinitialisation below.
Quoted 8 lines
Quoted 6 lines
What about
bool old_polarity = test_bit(FLAG_ACTIVE_LOW, &desc->flags); bool new_polarity = flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW;
old_polarity^new_polaritySo using bitwise operators on bools is ok??
XOR is special. There were never bitwise/boolean XORs.
Quoted 6 lines
Quoted 2 lines
and move this under INPUT conditional?
It has to be before the
gpio_v2_line_config_flags_to_desc_flags()call, as that modifies the desc flags, including the new polarity, sopolarity_changewould then always be false :-).
I really don't see in the code how polarity_change value is used in FLAG_OUTPUT branch below.
Quoted 35 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
drivers/gpio/gpiolib-cdev.c +23
-0+ +gpio_v2_line_config_flags_to_desc_flags (flags, &desc->flags); +/* +* Lines have to be requested explicitly for input +* or output, else the line will be treated "as is". +*/ +if (flags &GPIO_V2_LINE_FLAG_OUTPUT ) { +int val =gpio_v2_line_config_output_value (lc, i); + +edge_detector_stop (&lr->lines[i]); + ret =gpiod_direction_output (desc, val); +if (ret) +return ret; + }else if (flags &GPIO_V2_LINE_FLAG_INPUT ) { + ret =gpiod_direction_input (desc); +if (ret) +return ret; + + ret =edge_detector_update (&lr->lines[i], + flags &GPIO_V2_LINE_EDGE_FLAGS , + polarity_change); +if (ret) +return ret;Quoted 8 lines
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 861
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +8
-0+ } + +blocking_notifier_call_chain (&desc->gdev->notifier, +GPIO_V2_LINE_CHANGED_CONFIG , + desc); + } +return 0 ; +}--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 28 lines · Andy Shevchenko
Quoted 27 lines · Kent Gibson
Quoted 26 lines · Andy Shevchenko
Quoted 23 lines · Andy Shevchenko
Quoted 10 lines · Kent Gibson
Add support for
GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl.drivers/gpio/gpiolib-cdev.c +6
-0+static long linereq_set_config_unlocked (struct linereq *lr, +struct gpio_v2_line_config *lc) +{ +struct gpio_desc *desc; +unsigned int i; +u64 flags;drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 830
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+bool polarity_change; +int ret; + +for (i =0 ; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags =gpio_v2_line_config_flags (lc, i); + polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));Comparison
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Comparison between int / long (not all archs are agreed on this) and boolean is not the best we can do.
There is no bool to int comparision here.
test_bit()returns int or long depending on arch... Then you compare it to bool (which is a product of != 0).
Really - I thought it returned bool.
It is a test - why would it return int or long?
Surely it is guaranteed to return 0 or 1?
Quoted 6 lines
Quoted 6 lines
There are two comparisons - the inner int vs int => bool and the outer bool vs bool. The "!= 0" is effectively an implicit cast to bool, as is your
new_polarityinitialisation below.Quoted 2 lines
What about
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
bool old_polarity = test_bit(FLAG_ACTIVE_LOW, &desc->flags); bool new_polarity = flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW;
old_polarity^new_polaritySo using bitwise operators on bools is ok??
XOR is special. There were never bitwise/boolean XORs.
We must live in different universes, cos there has been a bitwise XOR in mine since K&R. The logical XOR is '!='.
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines
and move this under INPUT conditional?
It has to be before the
gpio_v2_line_config_flags_to_desc_flags()call, as that modifies the desc flags, including the new polarity, sopolarity_changewould then always be false :-).I really don't see in the code how
polarity_changevalue is used inFLAG_OUTPUTbranch below.
It isn't. But desc->flags is modified before both - and so the polarity_change initialization has to go before both SINCE IT TESTS THE FLAGS.
Cheers,
Kent.
Re: [PATCH v9 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL
Quoted 6 lines · Kent Gibson
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Quoted 3 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:35 AM Kent Gibson <[email protected]> wrote:
...
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 830
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+ polarity_change = + (test_bit (FLAG_ACTIVE_LOW , &desc->flags) != + ((flags &GPIO_V2_LINE_FLAG_ACTIVE_LOW ) !=0 ));Comparison
Comparison between int / long (not all archs are agreed on this) and boolean is not the best we can do.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
There is no bool to int comparision here.
test_bit()returns int or long depending on arch... Then you compare it to bool (which is a product of != 0).Really - I thought it returned bool.
It is a test - why would it return int or long?
I assume due to arch relation. Some archs may convert test_bit() to a single assembly instruction that returns a register which definitely fits long or int depending on case.
Quoted 1 line
Surely it is guaranteed to return 0 or 1?
Not sure about this, it's all in arch/* and needs to be investigated. Would be nice to have it cleaned up if there is any inconsistency (and document if not yet). But It's out of scope of this series I believe.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
There are two comparisons - the inner int vs int => bool and the outer bool vs bool. The "!= 0" is effectively an implicit cast to bool, as is your
new_polarityinitialisation below.Quoted 6 lines
What about
bool old_polarity = test_bit(FLAG_ACTIVE_LOW, &desc->flags); bool new_polarity = flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW;
old_polarity^new_polarity
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
So using bitwise operators on bools is ok??
XOR is special. There were never bitwise/boolean XORs.
We must live in different universes, cos there has been a bitwise XOR in mine since K&R. The logical XOR is '!='.
Oops, you are right, It was never boolean XOR because it's the same as a simple comparison.
...
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
and move this under INPUT conditional?
It has to be before the
gpio_v2_line_config_flags_to_desc_flags()call, as that modifies the desc flags, including the new polarity, sopolarity_changewould then always be false :-).
Quoted 6 lines
Quoted 2 lines
I really don't see in the code how
polarity_changevalue is used inFLAG_OUTPUTbranch below.It isn't. But
desc->flagsis modified before both - and so thepolarity_changeinitialization has to go before both SINCE IT TESTS THE FLAGS.
I see now. Sorry for being too blind.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerPATCH v9 11/20 gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
In reply to Kent GibsonAdd support for the GPIO_V2_LINE_SET_VALUES_IOCTL.
--- drivers/gpio/gpiolib-cdev.c | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
drivers/gpio/gpiolib-cdev.c +61 -0
@@ -813,6 +813,65 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)return 0 ; } +static long linereq_set_values_unlocked (struct linereq *lr, +struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) { +if (!test_bit (FLAG_IS_OUT , &lr->lines[i].desc->flags)) +return -EPERM ; +if (lv->bits &BIT_ULL (i)) +__set_bit (num_set, vals); + num_set++; + descs = &lr->lines[i].desc; + } + } +if (num_set ==0 ) +return -EINVAL ; + +if (num_set !=1 ) { +/* build compacted desc array and values */ + descs =kmalloc_array (num_set,sizeof (*descs),GFP_KERNEL ); +if (!descs) +return -ENOMEM ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret =gpiod_set_array_value_complex (false, true, num_set, + descs,NULL , vals); + +if (num_set !=1 ) +kfree (descs); +return ret; +} + +static long linereq_set_values (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_values lv; +int ret; + +if (copy_from_user (&lv, ip,sizeof (lv))) +return -EFAULT ; + +mutex_lock (&lr->config_mutex); + + ret =linereq_set_values_unlocked (lr, &lv); + +mutex_unlock (&lr->config_mutex); + +return ret; +} +static long linereq_set_config_unlocked (struct linereq *lr,struct gpio_v2_line_config *lc) {
@@ -889,6 +948,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd,if (cmd ==GPIO_V2_LINE_GET_VALUES_IOCTL )return linereq_get_values (lr, ip); +else if (cmd ==GPIO_V2_LINE_SET_VALUES_IOCTL ) +return linereq_set_values (lr, ip);else if (cmd ==GPIO_V2_LINE_SET_CONFIG_IOCTL )return linereq_set_config (lr, ip);
Signature · 1 line
2.28.0
Re: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,
drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {
Similar idea
DECLARE_BITMAP(mask, 64) = BITMAP_FROM_U64(lv->mask);
num_set = bitmap_weight();
for_each_set_bit(i, mask, lr->num_lines)
Quoted 63 lines
drivers/gpio/gpiolib-cdev.c +50
-1+if (!test_bit (FLAG_IS_OUT , &lr->lines[i].desc->flags)) +return -EPERM ; +if (lv->bits &BIT_ULL (i)) +__set_bit (num_set, vals); + num_set++; + descs = &lr->lines[i].desc; + } + } +if (num_set ==0 ) +return -EINVAL ; + +if (num_set !=1 ) { +/* build compacted desc array and values */ + descs =kmalloc_array (num_set,sizeof (*descs),GFP_KERNEL ); +if (!descs) +return -ENOMEM ; +for (didx =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret =gpiod_set_array_value_complex (false, true, num_set, + descs,NULL , vals); + +if (num_set !=1 ) +kfree (descs); +return ret; +} + +static long linereq_set_values (struct linereq *lr,void __user *ip) +{ +struct gpio_v2_line_values lv; +int ret; + +if (copy_from_user (&lv, ip,sizeof (lv))) +return -EFAULT ; + +mutex_lock (&lr->config_mutex); + + ret =linereq_set_values_unlocked (lr, &lv); + +mutex_unlock (&lr->config_mutex); + +return ret; +} +static long linereq_set_config_unlocked (struct linereq *lr,struct gpio_v2_line_config *lc) {@@ -889,6 +948,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd,if (cmd ==GPIO_V2_LINE_GET_VALUES_IOCTL )return linereq_get_values (lr, ip); +else if (cmd ==GPIO_V2_LINE_SET_VALUES_IOCTL ) +return linereq_set_values (lr, ip);else if (cmd ==GPIO_V2_LINE_SET_CONFIG_IOCTL )return linereq_set_config (lr, ip); --2.28.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 18 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
Quoted 4 lines
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();
I had played with this option, but bitmap_weight() counts all the bits set in the mask - which considers bits >= lr->num_lines. So you would need to mask lv->mask before converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)
But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)
Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Cheers,
Kent.
Re: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 19 lines · Kent Gibson
Quoted 18 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
Quoted 7 lines
Quoted 4 lines
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();I had played with this option, but
bitmap_weight()counts all the bits set in the mask - which considers bits >=lr->num_lines.
Does it mean we have a bug in the code and no test case covered it? Because from the API I see the parameter nbits which should prevent this.
Quoted 3 lines
So you would need to mask
lv->maskbefore converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)
Can you confirm a bug in bitmap API? If it's so it's a serious one.
Quoted 9 lines
But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 25 lines · Andy Shevchenko
Quoted 24 lines · Kent Gibson
Quoted 22 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();
Quoted 7 lines
Quoted 2 lines
I had played with this option, but
bitmap_weight()counts all the bits set in the mask - which considers bits >=lr->num_lines.Does it mean we have a bug in the code and no test case covered it? Because from the API I see the parameter nbits which should prevent this.
Probably me being thick and using nbits as the size of the mask rather than the number of bits of interest.
Cheers,
Kent.
Quoted 20 lines
Quoted 3 lines
So you would need to mask
lv->maskbefore converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)Can you confirm a bug in bitmap API? If it's so it's a serious one.
Quoted 9 lines
But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
In reply to Kent GibsonQuoted 33 lines · Kent Gibson
Quoted 18 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
Quoted 4 lines
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();I had played with this option, but
bitmap_weight()counts all the bits set in the mask - which considers bits >=lr->num_lines. So you would need to masklv->maskbefore converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 7 lines
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Tried it again and, while it works, it does increase the size of gpiolib-cdev.o as follows:
u64 -> bitmap x86_64 28360 28616 i386 22056 22100 aarch64 37392 37600 mips32 28008 28016
So for 64-bit platforms changing to bitmap generates larger code, probably as we are forcing them to use 32-bit array semantics where before they could use the native u64. For 32-bit there is a much smaller difference as they were already using 32-bit array semantics to realise the u64.
Those are for some of my test builds, so obviously YMMV.
It is also only for changing linereq_get_values(), which has three instances of the loop. linereq_set_values_unlocked() has another two, so you could expect another increase of ~2/3 of that seen here if we change that as well.
The sizeable increase in x86_64 was what made me revert this last time, and I'm still satisfied with that choice. Are you still eager to switch to for_each_set_bit()?
Cheers,
Kent.
Re: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 42 lines · Kent Gibson
Quoted 33 lines · Kent Gibson
Quoted 18 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
Quoted 4 lines
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();I had played with this option, but
bitmap_weight()counts all the bits set in the mask - which considers bits >=lr->num_lines. So you would need to masklv->maskbefore converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 7 lines
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Quoted 8 lines
Tried it again and, while it works, it does increase the size of gpiolib-cdev.o as follows:
u64 -> bitmap x86_64 28360 28616 i386 22056 22100 aarch64 37392 37600 mips32 28008 28016
Yes, that's pity... See below.
Quoted 8 lines
So for 64-bit platforms changing to bitmap generates larger code, probably as we are forcing them to use 32-bit array semantics where before they could use the native u64. For 32-bit there is a much smaller difference as they were already using 32-bit array semantics to realise the u64.
Those are for some of my test builds, so obviously YMMV.
Quoted 8 lines
It is also only for changing
linereq_get_values(), which has three instances of the loop.linereq_set_values_unlocked()has another two, so you could expect another increase of ~2/3 of that seen here if we change that as well.The sizeable increase in
x86_64was what made me revert this last time, and I'm still satisfied with that choice. Are you still eager to switch tofor_each_set_bit()?
I already asked once about short cut for for_each_set_bit in case of constant nbits parameter when it's <= BITS_PER_LONG, but here it seems we have variadic amount of lines, dunno if compiler can prove that it's smaller than long. In any case my point is that code readability has a preference vs. memory footprint (except hot paths) and if we are going to fix this it should be done in general. That said, if maintainers are okay with that I would prefer bitmap API over open-coded pieces.
Also note, that it will be easier to extend in the future if needed (if we want to have more than BITS_PER_LONG [64] lines to handle).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL
Quoted 71 lines · Andy Shevchenko
Quoted 42 lines · Kent Gibson
Quoted 33 lines · Kent Gibson
Quoted 18 lines · Andy Shevchenko
Quoted 4 lines · Kent Gibson
Add support for the
GPIO_V2_LINE_SET_VALUES_IOCTL.drivers/gpio/gpiolib-cdev.c +1
-0+static long linereq_set_values_unlocked(struct linereq *lr,drivers/gpio/gpiolib-cdev.c static long linereq_get_values(struct linereq *lr, void __user *ip) · line 826
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+ struct gpio_v2_line_values *lv) +{ +DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX ); +struct gpio_desc **descs; +unsigned int i, didx, num_set; +int ret; + +bitmap_zero (vals,GPIO_V2_LINES_MAX ); +for (num_set =0 , i =0 ; i < lr->num_lines; i++) { +if (lv->mask &BIT_ULL (i)) {Similar idea
Quoted 4 lines
DECLARE_BITMAP(mask, 64)=BITMAP_FROM_U64(lv->mask);
num_set=bitmap_weight();I had played with this option, but
bitmap_weight()counts all the bits set in the mask - which considers bits >=lr->num_lines. So you would need to masklv->maskbefore converting it to a bitmap. (I'm ok with ignoring those bits in case userspace wants to be lazy and use an all 1s mask.)But since we're looping over the bitmap anyway we may as well just count as we go.
Quoted 7 lines
Quoted 2 lines
for_each_set_bit(i, mask, lr->num_lines)Yeah, that should work. I vaguely recall trying this and finding it generated larger object code, but I'll give it another try and if it works out then include it in v10.
Quoted 8 lines
Tried it again and, while it works, it does increase the size of gpiolib-cdev.o as follows:
u64 -> bitmap x86_64 28360 28616 i386 22056 22100 aarch64 37392 37600 mips32 28008 28016Yes, that's pity... See below.
Quoted 8 lines
So for 64-bit platforms changing to bitmap generates larger code, probably as we are forcing them to use 32-bit array semantics where before they could use the native u64. For 32-bit there is a much smaller difference as they were already using 32-bit array semantics to realise the u64.
Those are for some of my test builds, so obviously YMMV.
Quoted 8 lines
It is also only for changing
linereq_get_values(), which has three instances of the loop.linereq_set_values_unlocked()has another two, so you could expect another increase of ~2/3 of that seen here if we change that as well.The sizeable increase in
x86_64was what made me revert this last time, and I'm still satisfied with that choice. Are you still eager to switch tofor_each_set_bit()?
Quoted 9 lines
I already asked once about short cut for
for_each_set_bitin case of constant nbits parameter when it's <=BITS_PER_LONG, but here it seems we have variadic amount of lines, dunno if compiler can prove that it's smaller than long. In any case my point is that code readability has a preference vs. memory footprint (except hot paths) and if we are going to fix this it should be done in general. That said, if maintainers are okay with that I would prefer bitmap API over open-coded pieces.
Agreed - if the bitmap ops made better use of the architecure then I'd change to bitmap without question - it is more readable.
Bart and Linus - do you have any preference?
Quoted 3 lines
Also note, that it will be easier to extend in the future if needed (if we want to have more than
BITS_PER_LONG[64] lines to handle).
Yeah, I think we can rule that one out - while I had initially written the uAPI with the option of > 64 lines we decided on the hard limit to keep things simple. And any user that needs to request more than 64 lines in one request shouldn't be using the GPIO uAPI.
Cheers,
Kent.
Kent Gibson
reviewerPATCH v9 12/20 gpiolib: cdev: support setting debounce
In reply to Kent GibsonAdd support for setting debounce on a line via the GPIO uAPI. Where debounce is not supported by hardware, a software debounce is provided.
The implementation of the software debouncer waits for the line to be stable for the debounce period before determining if a level change, and a corresponding edge event, has occurred. This provides maximum protection against glitches, but also introduces a debounce_period latency to edge events.
The software debouncer is integrated with the edge detection as it utilises the line interrupt, and integration is simpler than getting the two to interwork. Where software debounce AND edge detection is required, the debouncer provides both.
---
Changes for v6:
- as per cover letter
Changes for v5:
- as per cover letter
Changes for v4:
- fix handling of mask in line_get_values
Changes for v3:
- only GPIO_V2 field renaming
Changes for v2:
- improve documentation on fields shared by threads.
- use READ_ONCE/WRITE_ONCE for shared fields rather than atomic_t which was overkill.
drivers/gpio/gpiolib-cdev.c | 248 ++++++++++++++++++++++++++++++++++-- drivers/gpio/gpiolib.c | 3 + drivers/gpio/gpiolib.h | 4 + 3 files changed, 244 insertions(+), 11 deletions(-)
drivers/gpio/gpiolib-cdev.c +237 -11
@@ -6,6 +6,7 @@#include <linux/build_bug.h> #include <linux/cdev.h> #include <linux/compat.h> +#include <linux/compiler.h> #include <linux/device.h> #include <linux/err.h> #include <linux/file.h>
@@ -22,6 +23,7 @@#include <linux/spinlock.h> #include <linux/timekeeping.h> #include <linux/uaccess.h> +#include <linux/workqueue.h> #include <uapi/linux/gpio.h> #include "gpiolib.h"
@@ -415,6 +417,9 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)* events for the corresponding line request. This is drawn from the @req. * @line_seqno: the seqno for the current edge event in the sequence of * events for this line. +* @work: the worker that implements software debouncing +* @sw_debounced: flag indicating if the software debouncer is active +* @level: the current debounced physical level of the line */ struct line {struct gpio_desc *desc;
@@ -431,7 +436,28 @@ struct line {*/ u64 timestamp_ns;u32 req_seqno; +/* +* line_seqno is accessed by either edge_irq_thread() or +* debounce_work_func(), which are themselves mutually exclusive, +* so no additional protection is necessary. +*/ u32 line_seqno; +/* +* -- debouncer specific fields -- +*/ +struct delayed_work work; +/* +* sw_debounce is accessed by linereq_set_config(), which is the +* only setter, and linereq_get_values(), which can live with a +* slightly stale value. +*/ +unsigned int sw_debounced; +/* +* level is accessed by debounce_work_func(), which is the only +* setter, and linereq_get_values() which can live with a slightly +* stale value. +*/ +unsigned int level; };/**
@@ -571,6 +597,154 @@ static irqreturn_t edge_irq_handler(int irq, void *p)return IRQ_WAKE_THREAD ; } +/* +* returns the current debounced logical value. +*/ +static unsigned int debounced_value (struct line *line) +{ +unsigned int value; + +/* +* minor race - debouncer may be stopped here, so edge_detector_stop +* must leave the value unchanged so the following will read the level +* from when the debouncer was last running. +*/ + value =READ_ONCE (line->level); + +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + value = !value; + +return value; +} + +static irqreturn_t debounce_irq_handler (int irq,void *p) +{ +struct line *line = p; + +mod_delayed_work (system_wq, &line->work, +usecs_to_jiffies (READ_ONCE (line->desc->debounce_period_us))); + +return IRQ_HANDLED ; +} + +static void debounce_work_func (struct work_struct *work) +{ +struct gpio_v2_line_event le; + struct line *line = container_of(work, struct line, work.work); +struct linereq *lr; +int level; + + level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) { +pr_debug_ratelimited ("debouncer failed to read line value\n" ); +return ; + } + +if (READ_ONCE (line->level) == level) +return ; + +WRITE_ONCE (line->level, level); + +/* -- edge detection -- */ +if (!line->eflags) +return ; + +/* switch from physical level to logical - if they differ */ +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + level = !level; + +/* ignore edges that are not being monitored */ +if (((line->eflags ==GPIO_V2_LINE_FLAG_EDGE_RISING ) && !level) || + ((line->eflags ==GPIO_V2_LINE_FLAG_EDGE_FALLING ) && level)) +return ; + +/* Do not leak kernel stack to userspace */ +memset (&le,0 ,sizeof (le)); + + lr = line->req; + le.timestamp_ns =ktime_get_ns (); + le.offset =gpio_chip_hwgpio (line->desc); + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines ==1 ) ? + le.line_seqno :atomic_inc_return (&lr->seqno); + +if (level) +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; +else +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + +linereq_put_event (lr, &le); +} + +static int debounce_setup (struct line *line, +unsigned int debounce_period_us) +{ +unsigned long irqflags; +int ret, level, irq; + +/* try hardware */ + ret =gpiod_set_debounce (line->desc, debounce_period_us); +if (!ret) { +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return ret; + } +if (ret != -ENOTSUPP ) +return ret; + +if (debounce_period_us) { +/* setup software debounce */ + level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) +return level; + + irq =gpiod_to_irq (line->desc); +if (irq <=0 ) +return -ENODEV ; + +WRITE_ONCE (line->level, level); + irqflags =IRQF_TRIGGER_FALLING |IRQF_TRIGGER_RISING ; + ret =request_irq (irq, debounce_irq_handler, irqflags, + line->req->label, line); +if (ret) +return ret; + +WRITE_ONCE (line->sw_debounced,1 ); + line->irq = irq; + } +return 0 ; +} + +static bool gpio_v2_line_config_debounced (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_DEBOUNCE ) && + (lc->attrs[i].mask & mask)) +return true; + } +return false; +} + +static u32 gpio_v2_line_config_debounce_period (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_DEBOUNCE ) && + (lc->attrs[i].mask & mask)) +return lc->attrs[i].attr.debounce_period_us; + } +return 0 ; +} +static void edge_detector_stop (struct line *line) {if (line->irq) {
@@ -578,12 +752,18 @@ static void edge_detector_stop(struct line *line) line->irq =0 ; } +cancel_delayed_work_sync (&line->work); +WRITE_ONCE (line->sw_debounced,0 ); line->eflags =0 ; +/* do not change line->level - see comment in debounced_value */ }static int edge_detector_setup (struct line *line, +struct gpio_v2_line_config *lc, +unsigned int line_idx,u64 eflags) { +u32 debounce_period_us;unsigned long irqflags =0 ;int irq, ret;
@@ -594,8 +774,16 @@ static int edge_detector_setup(struct line *line,return ret; } line->eflags = eflags; +if (gpio_v2_line_config_debounced (lc, line_idx)) { + debounce_period_us =gpio_v2_line_config_debounce_period (lc, line_idx); + ret =debounce_setup (line, debounce_period_us); +if (ret) +return ret; +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); + } -if (!eflags) +/* detection disabled or sw debouncer will provide edge detection */ +if (!eflags ||READ_ONCE (line->sw_debounced))return 0 ; irq =gpiod_to_irq (line->desc);
@@ -620,15 +808,31 @@ static int edge_detector_setup(struct line *line,return 0 ; } -static int edge_detector_update (struct line *line,u64 eflags, -bool polarity_change) +static int edge_detector_update (struct line *line, +struct gpio_v2_line_config *lc, +unsigned int line_idx, +u64 eflags,bool polarity_change) { -if ((line->eflags == eflags) && !polarity_change) +unsigned int debounce_period_us = +gpio_v2_line_config_debounce_period (lc, line_idx); + +if ((line->eflags == eflags) && !polarity_change && + (READ_ONCE (line->desc->debounce_period_us) == debounce_period_us))return 0 ; -edge_detector_stop (line); +/* sw debounced and still will be...*/ +if ((debounce_period_us !=0 ) &&READ_ONCE (line->sw_debounced)) { + line->eflags = eflags; +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return 0 ; + } + +/* reconfiguring edge detection or sw debounce being disabled */ +if ((line->irq && !READ_ONCE (line->sw_debounced)) || + (!debounce_period_us &&READ_ONCE (line->sw_debounced))) +edge_detector_stop (line); -return edge_detector_setup (line, eflags); +return edge_detector_setup (line, lc, line_idx, eflags); } static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
@@ -726,6 +930,11 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, ret =gpio_v2_line_flags_validate (flags);if (ret)return ret; + +/* debounce requires explicit input */ +if (gpio_v2_line_config_debounced (lc, i) && + !(flags &GPIO_V2_LINE_FLAG_INPUT )) +return -EINVAL ; }return 0 ; }
@@ -762,7 +971,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)struct gpio_v2_line_values lv;DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX );struct gpio_desc **descs; -unsigned int i, didx, num_get; +unsigned int i, val, didx, num_get;int ret;/* NOTE: It's ok to read values of output lines. */
@@ -801,7 +1010,11 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) lv.bits =0 ;for (didx =0 , i =0 ; i < lr->num_lines; i++) {if (lv.mask &BIT_ULL (i)) { -if (test_bit (didx, vals)) +if (lr->lines[i].sw_debounced) + val =debounced_value (&lr->lines[i]); +else + val =test_bit (didx, vals); +if (val) lv.bits |=BIT_ULL (i); didx++; }
@@ -905,7 +1118,7 @@ static long linereq_set_config_unlocked(struct linereq *lr,if (ret)return ret; - ret =edge_detector_update (&lr->lines[i], + ret =edge_detector_update (&lr->lines[i], lc, i, flags &GPIO_V2_LINE_EDGE_FLAGS , polarity_change);if (ret)
@@ -1099,8 +1312,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev;get_device (&gdev->dev); -for (i =0 ; i < ulr.num_lines; i++) +for (i =0 ; i < ulr.num_lines; i++) { lr->lines[i].req = lr; +WRITE_ONCE (lr->lines[i].sw_debounced,0 ); +INIT_DELAYED_WORK (&lr->lines[i].work, debounce_work_func); + }/* Make sure this is terminated */ ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ;
@@ -1160,7 +1376,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)if (ret) goto out_free_linereq; - ret =edge_detector_setup (&lr->lines[i], + ret =edge_detector_setup (&lr->lines[i], lc, i, flags &GPIO_V2_LINE_EDGE_FLAGS );if (ret) goto out_free_linereq;
@@ -1631,6 +1847,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,struct gpio_chip *gc = desc->gdev->chip;bool ok_for_pinctrl;unsigned long flags; +u32 debounce_period_us; +unsigned int num_attrs =0 ;memset (info,0 ,sizeof (*info)); info->offset =gpio_chip_hwgpio (desc);
@@ -1691,6 +1909,14 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,if (test_bit (FLAG_EDGE_FALLING , &desc->flags)) info->flags |=GPIO_V2_LINE_FLAG_EDGE_FALLING ; + debounce_period_us =READ_ONCE (desc->debounce_period_us); +if (debounce_period_us) { + info->attrs[num_attrs].id =GPIO_V2_LINE_ATTR_ID_DEBOUNCE ; + info->attrs[num_attrs].debounce_period_us = debounce_period_us; + num_attrs++; + } + info->num_attrs = num_attrs; +spin_unlock_irqrestore (&gpio_lock, flags); }
drivers/gpio/gpiolib.c +3 -0
@@ -2097,6 +2097,9 @@ static bool gpiod_free_commit(struct gpio_desc *desc)clear_bit (FLAG_IS_HOGGED , &desc->flags);#ifdef CONFIG_OF_DYNAMIC desc->hog =NULL ; +#endif +#ifdef CONFIG_GPIO_CDEV +WRITE_ONCE (desc->debounce_period_us,0 );#endif ret = true; }
drivers/gpio/gpiolib.h +4 -0
@@ -124,6 +124,10 @@ struct gpio_desc {#ifdef CONFIG_OF_DYNAMIC struct device_node *hog;#endif +#ifdef CONFIG_GPIO_CDEV +/* debounce period in microseconds */ +unsigned int debounce_period_us; +#endif };int gpiod_request (struct gpio_desc *desc,const char *label);
Signature · 1 line
2.28.0
Re: [PATCH v9 12/20] gpiolib: cdev: support setting debounce
Quoted 17 lines · Kent Gibson
Add support for setting debounce on a line via the GPIO uAPI. Where debounce is not supported by hardware, a software debounce is provided.
The implementation of the software debouncer waits for the line to be stable for the debounce period before determining if a level change, and a corresponding edge event, has occurred. This provides maximum protection against glitches, but also introduces a
debounce_periodlatency to edge events.The software debouncer is integrated with the edge detection as it utilises the line interrupt, and integration is simpler than getting the two to interwork. Where software debounce AND edge detection is required, the debouncer provides both.
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 608
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static unsigned int debounced_value (struct line *line) +{ +unsigned int value; + +/* +* minor race - debouncer may be stopped here, so edge_detector_stop
() ?
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 615
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +7
-0+* must leave the value unchanged so the following will read the level +* from when the debouncer was last running. +*/ + value =READ_ONCE (line->level); + +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + value = !value;
I'm not sure what this means in terms of unsingned int to be returned.
Quoted 1 line
+ return value;
Shouldn't we rather return 0/1 guaranteed?
Perhaps
if (active_low)
return !value;
return !!value;
?
Quoted 27 lines
drivers/gpio/gpiolib-cdev.c +27
-0+} + +static irqreturn_t debounce_irq_handler (int irq,void *p) +{ +struct line *line = p; + +mod_delayed_work (system_wq, &line->work, +usecs_to_jiffies (READ_ONCE (line->desc->debounce_period_us))); + +return IRQ_HANDLED ; +} + +static void debounce_work_func (struct work_struct *work) +{ +struct gpio_v2_line_event le; + struct line *line = container_of(work, struct line, work.work); +struct linereq *lr; +int level; + + level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) { +pr_debug_ratelimited ("debouncer failed to read line value\n" ); +return ; + } + +if (READ_ONCE (line->level) == level) +return ;
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 654
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ +WRITE_ONCE (line->level, level); + +/* -- edge detection -- */ +if (!line->eflags) +return ; +/* switch from physical level to logical - if they differ */ +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + level = !level;
Seems to me a good candidate to have
static inline bool convert_with_active_low_respected(desc, value) {
if (active_low)
return !value;
return !!value;
}
Quoted 39 lines
drivers/gpio/gpiolib-cdev.c +39
-0+/* ignore edges that are not being monitored */ +if (((line->eflags ==GPIO_V2_LINE_FLAG_EDGE_RISING ) && !level) || + ((line->eflags ==GPIO_V2_LINE_FLAG_EDGE_FALLING ) && level)) +return ; + +/* Do not leak kernel stack to userspace */ +memset (&le,0 ,sizeof (le)); + + lr = line->req; + le.timestamp_ns =ktime_get_ns (); + le.offset =gpio_chip_hwgpio (line->desc); + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines ==1 ) ? + le.line_seqno :atomic_inc_return (&lr->seqno); + +if (level) +/* Emit low-to-high event */ + le.id =GPIO_V2_LINE_EVENT_RISING_EDGE ; +else +/* Emit high-to-low event */ + le.id =GPIO_V2_LINE_EVENT_FALLING_EDGE ; + +linereq_put_event (lr, &le); +} + +static int debounce_setup (struct line *line, +unsigned int debounce_period_us) +{ +unsigned long irqflags; +int ret, level, irq; + +/* try hardware */ + ret =gpiod_set_debounce (line->desc, debounce_period_us); +if (!ret) { +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return ret; + } +if (ret != -ENOTSUPP )
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 704
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10
-0+return ret; + +if (debounce_period_us) { +/* setup software debounce */ + level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) +return level; + + irq =gpiod_to_irq (line->desc); +if (irq <=0 )
Same question about return code...
Quoted 96 lines
drivers/gpio/gpiolib-cdev.c +63
-4+return -ENODEV ; + +WRITE_ONCE (line->level, level); + irqflags =IRQF_TRIGGER_FALLING |IRQF_TRIGGER_RISING ; + ret =request_irq (irq, debounce_irq_handler, irqflags, + line->req->label, line); +if (ret) +return ret; + +WRITE_ONCE (line->sw_debounced,1 ); + line->irq = irq; + } +return 0 ; +} + +static bool gpio_v2_line_config_debounced (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_DEBOUNCE ) && + (lc->attrs[i].mask & mask)) +return true; + } +return false; +} + +static u32 gpio_v2_line_config_debounce_period (struct gpio_v2_line_config *lc, +unsigned int line_idx) +{ +unsigned int i; +u64 mask =BIT_ULL (line_idx); + +for (i =0 ; i < lc->num_attrs; i++) { +if ((lc->attrs[i].attr.id ==GPIO_V2_LINE_ATTR_ID_DEBOUNCE ) && + (lc->attrs[i].mask & mask)) +return lc->attrs[i].attr.debounce_period_us; + } +return 0 ; +} +static void edge_detector_stop (struct line *line) {if (line->irq) {@@ -578,12 +752,18 @@ static void edge_detector_stop(struct line *line) line->irq =0 ; } +cancel_delayed_work_sync (&line->work); +WRITE_ONCE (line->sw_debounced,0 ); line->eflags =0 ; +/* do not change line->level - see comment in debounced_value */ }static int edge_detector_setup (struct line *line, +struct gpio_v2_line_config *lc, +unsigned int line_idx,u64 eflags) { +u32 debounce_period_us;unsigned long irqflags =0 ;int irq, ret;@@ -594,8 +774,16 @@ static int edge_detector_setup(struct line *line,return ret; } line->eflags = eflags; +if (gpio_v2_line_config_debounced (lc, line_idx)) { + debounce_period_us =gpio_v2_line_config_debounce_period (lc, line_idx); + ret =debounce_setup (line, debounce_period_us); +if (ret) +return ret; +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); + } -if (!eflags) +/* detection disabled or sw debouncer will provide edge detection */ +if (!eflags ||READ_ONCE (line->sw_debounced))return 0 ; irq =gpiod_to_irq (line->desc);@@ -620,15 +808,31 @@ static int edge_detector_setup(struct line *line,return 0 ; } -static int edge_detector_update (struct line *line,u64 eflags, -bool polarity_change) +static int edge_detector_update (struct line *line, +struct gpio_v2_line_config *lc, +unsigned int line_idx, +u64 eflags,bool polarity_change) { -if ((line->eflags == eflags) && !polarity_change) + unsigned int debounce_period_us =
drivers/gpio/gpiolib-cdev.c static int edge_detector_setup(struct line *line, · line 825
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +6
-1+gpio_v2_line_config_debounce_period (lc, line_idx); + +if ((line->eflags == eflags) && !polarity_change && + (READ_ONCE (line->desc->debounce_period_us) == debounce_period_us))return 0 ; -edge_detector_stop (line); +/* sw debounced and still will be...*/ +if ((debounce_period_us !=0 ) &&READ_ONCE (line->sw_debounced)) {
'( != 0)' are redundant. But I think you want to show that it's not boolean and we compare to 0...
Quoted 115 lines
drivers/gpio/gpiolib-cdev.c +10
-1+ line->eflags = eflags; +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return 0 ; + } + +/* reconfiguring edge detection or sw debounce being disabled */ +if ((line->irq && !READ_ONCE (line->sw_debounced)) || + (!debounce_period_us &&READ_ONCE (line->sw_debounced))) +edge_detector_stop (line); -return edge_detector_setup (line, eflags); +return edge_detector_setup (line, lc, line_idx, eflags); }static u64 gpio_v2_line_config_flags(struct
gpio_v2_line_config*lc,drivers/gpio/gpiolib-cdev.c +27
-5@@ -726,6 +930,11 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, ret =gpio_v2_line_flags_validate (flags);if (ret)return ret; + +/* debounce requires explicit input */ +if (gpio_v2_line_config_debounced (lc, i) && + !(flags &GPIO_V2_LINE_FLAG_INPUT )) +return -EINVAL ; }return 0 ; }@@ -762,7 +971,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip)struct gpio_v2_line_values lv;DECLARE_BITMAP (vals,GPIO_V2_LINES_MAX );struct gpio_desc **descs; -unsigned int i, didx, num_get; +unsigned int i, val, didx, num_get;int ret;/* NOTE: It's ok to read values of output lines. */ @@ -801,7 +1010,11 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) lv.bits =0 ;for (didx =0 , i =0 ; i < lr->num_lines; i++) {if (lv.mask &BIT_ULL (i)) { -if (test_bit (didx, vals)) +if (lr->lines[i].sw_debounced) + val =debounced_value (&lr->lines[i]); +else + val =test_bit (didx, vals); +if (val) lv.bits |=BIT_ULL (i); didx++; }@@ -905,7 +1118,7 @@ static long linereq_set_config_unlocked(struct linereq *lr,if (ret)return ret; - ret =edge_detector_update (&lr->lines[i], + ret =edge_detector_update (&lr->lines[i], lc, i, flags &GPIO_V2_LINE_EDGE_FLAGS , polarity_change);if (ret)@@ -1099,8 +1312,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev;get_device (&gdev->dev); -for (i =0 ; i < ulr.num_lines; i++) +for (i =0 ; i < ulr.num_lines; i++) { lr->lines[i].req = lr; +WRITE_ONCE (lr->lines[i].sw_debounced,0 ); +INIT_DELAYED_WORK (&lr->lines[i].work, debounce_work_func); + }/* Make sure this is terminated */ ulr.consumer[sizeof (ulr.consumer)-1 ] ='\0' ;@@ -1160,7 +1376,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)if (ret) goto out_free_linereq; - ret =edge_detector_setup (&lr->lines[i], + ret =edge_detector_setup (&lr->lines[i], lc, i, flags &GPIO_V2_LINE_EDGE_FLAGS );if (ret) goto out_free_linereq;@@ -1631,6 +1847,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,struct gpio_chip *gc = desc->gdev->chip;bool ok_for_pinctrl;unsigned long flags; +u32 debounce_period_us; +unsigned int num_attrs =0 ;memset (info,0 ,sizeof (*info)); info->offset =gpio_chip_hwgpio (desc);@@ -1691,6 +1909,14 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,if (test_bit (FLAG_EDGE_FALLING , &desc->flags)) info->flags |=GPIO_V2_LINE_FLAG_EDGE_FALLING ; + debounce_period_us =READ_ONCE (desc->debounce_period_us); +if (debounce_period_us) { + info->attrs[num_attrs].id =GPIO_V2_LINE_ATTR_ID_DEBOUNCE ; + info->attrs[num_attrs].debounce_period_us = debounce_period_us; + num_attrs++; + } + info->num_attrs = num_attrs; +spin_unlock_irqrestore (&gpio_lock, flags); }drivers/gpio/gpiolib.c +3
-0@@ -2097,6 +2097,9 @@ static bool gpiod_free_commit(struct gpio_desc *desc)clear_bit (FLAG_IS_HOGGED , &desc->flags);#ifdef CONFIG_OF_DYNAMIC desc->hog =NULL ; +#endif +#ifdef CONFIG_GPIO_CDEV + WRITE_ONCE(desc->de> --
Quoted 2 lines
2.28.0
bounce_period_us, 0);
Quoted 18 lines
#endif ret = true; }drivers/gpio/gpiolib.h +4
-0@@ -124,6 +124,10 @@ struct gpio_desc {#ifdef CONFIG_OF_DYNAMIC struct device_node *hog;#endif +#ifdef CONFIG_GPIO_CDEV +/* debounce period in microseconds */ +unsigned int debounce_period_us; +#endif };int gpiod_request (struct gpio_desc *desc,const char *label);
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 12/20] gpiolib: cdev: support setting debounce
Quoted 42 lines · Andy Shevchenko
Quoted 17 lines · Kent Gibson
Add support for setting debounce on a line via the GPIO uAPI. Where debounce is not supported by hardware, a software debounce is provided.
The implementation of the software debouncer waits for the line to be stable for the debounce period before determining if a level change, and a corresponding edge event, has occurred. This provides maximum protection against glitches, but also introduces a
debounce_periodlatency to edge events.The software debouncer is integrated with the edge detection as it utilises the line interrupt, and integration is simpler than getting the two to interwork. Where software debounce AND edge detection is required, the debouncer provides both.
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 608
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +6
-0+static unsigned int debounced_value (struct line *line) +{ +unsigned int value; + +/* +* minor race - debouncer may be stopped here, so edge_detector_stop () ?
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 615
Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +7
-0+* must leave the value unchanged so the following will read the level +* from when the debouncer was last running. +*/ + value =READ_ONCE (line->level); + +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + value = !value;I'm not sure what this means in terms of unsingned int to be returned.
Quoted 1 line
+ return value;
Shouldn't we rather return 0/1 guaranteed?
Quoted 9 lines
Perhaps
if (
active_low)
return !value;return !!value;
?
Or just make the return value a bool?
[snip]
Quoted 27 lines
Quoted 16 lines
drivers/gpio/gpiolib-cdev.c +16
-0+ +static void debounce_work_func (struct work_struct *work) +{ +struct gpio_v2_line_event le; + struct line *line = container_of(work, struct line, work.work); +struct linereq *lr; +int level; + + level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) { +pr_debug_ratelimited ("debouncer failed to read line value\n" ); +return ; + } + +if (READ_ONCE (line->level) == level) +return ;drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 654
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ +WRITE_ONCE (line->level, level); + +/* -- edge detection -- */ +if (!line->eflags) +return ; +/* switch from physical level to logical - if they differ */ +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + level = !level;
Quoted 9 lines
Seems to me a good candidate to have
static inline bool
convert_with_active_low_respected(desc, value){
if (active_low)
return !value;
return !!value;
}
Not sure it is worth the effort - it would only be used twice - here and in debounced_value() - which is only a couple of lines itself.
[snip]
Quoted 18 lines
Quoted 18 lines
drivers/gpio/gpiolib-cdev.c +18
-0+ +static int debounce_setup (struct line *line, +unsigned int debounce_period_us) +{ +unsigned long irqflags; +int ret, level, irq; + +/* try hardware */ + ret =gpiod_set_debounce (line->desc, debounce_period_us); +if (!ret) { +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return ret; + } +if (ret != -ENOTSUPP ) +return ret; + +if (debounce_period_us) { +/* setup software debounce */
Quoted 9 lines
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 704
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +6
-0+ level =gpiod_get_raw_value_cansleep (line->desc); +if (level <0 ) +return level; + + irq =gpiod_to_irq (line->desc); +if (irq <=0 )Same question about return code...
Same answer...
[snip]
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c static int edge_detector_setup(struct line *line,
Quoted 6 lines
return 0;drivers/gpio/gpiolib-cdev.c +2
-1-edge_detector_stop (line); +/* sw debounced and still will be...*/ +if ((debounce_period_us !=0 ) &&READ_ONCE (line->sw_debounced)) {'( != 0)' are redundant. But I think you want to show that it's not boolean and we compare to 0...
Yeah, I guess I thought that was clearer, though I use the bare form just below as well, and the bare form seems clear enough to me now, so will change it for v10.
Cheers,
Kent.
Quoted 10 lines
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9
-0+ line->eflags = eflags; +WRITE_ONCE (line->desc->debounce_period_us, debounce_period_us); +return 0 ; + } + +/* reconfiguring edge detection or sw debounce being disabled */ +if ((line->irq && !READ_ONCE (line->sw_debounced)) || + (!debounce_period_us &&READ_ONCE (line->sw_debounced))) +edge_detector_stop (line);
Re: [PATCH v9 12/20] gpiolib: cdev: support setting debounce
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:36 AM Kent Gibson <[email protected]> wrote:
...
Quoted 7 lines
Quoted 7 lines
Shouldn't we rather return 0/1 guaranteed?
Perhaps
if (
active_low)
return !value;
Quoted 6 lines
Quoted 4 lines
return !!value;
?
Or just make the return value a bool?
If it's good enough.
...
Quoted 13 lines
Quoted 4 lines
drivers/gpio/gpiolib-cdev.c static irqreturn_t edge_irq_handler(int irq, void *p) · line 654
Quoted 3 lines
drivers/gpio/gpiolib-cdev.c +3
-0+/* switch from physical level to logical - if they differ */ +if (test_bit (FLAG_ACTIVE_LOW , &line->desc->flags)) + level = !level;Quoted 8 lines
Seems to me a good candidate to have
static inline bool
convert_with_active_low_respected(desc, value){
if (active_low)
return !value;
return !!value;
}
Quoted 2 lines
Not sure it is worth the effort - it would only be used twice - here and in
debounced_value()- which is only a couple of lines itself.
I'm thinking about possible candidates to use this... gpiod_direction_output() gpiod_get_array_value_complex() gpiod_get_value()gpiod_set_value_nocheck() gpiod_get_value_cansleep()
...I stopped here...
I agree that not all of them are good to be converted (b/c few flags being tested and it will bring inconsistency), but many deserve the perhaps separate patch to introduce the above mentioned helper.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerPATCH v9 13/20 gpio: uapi: document uAPI v1 as deprecated
In reply to Kent GibsonUpdate uAPI documentation to deprecate v1 structs and ioctls.
--- include/uapi/linux/gpio.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
include/uapi/linux/gpio.h +26 -0
@@ -292,6 +292,9 @@ struct gpio_v2_line_event {/* * ABI v1 +* +* This version of the ABI is deprecated and will be removed in the future. +* Use the latest version of the ABI, defined above, instead. */ /* Informational flags */
@@ -315,6 +318,9 @@ struct gpio_v2_line_event {* @consumer: a functional name for the consumer of this GPIO line as set by * whatever is using it, will be empty if there is no current user but may * also be empty if the consumer doesn't set this up +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_info instead. */ struct gpioline_info {__u32 line_offset;
@@ -346,6 +352,9 @@ enum {* guarantee there are no implicit holes between it and subsequent members. * The 20-byte padding at the end makes sure we don't add any implicit padding * at the end of the structure on 64-bit architectures. +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_info_changed instead. */ struct gpioline_info_changed {struct gpioline_info info;
@@ -385,6 +394,9 @@ struct gpioline_info_changed {* @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEHANDLE_IOCTL operation, zero or negative value * means error +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_request instead. */ struct gpiohandle_request {__u32 lineoffsets[GPIOHANDLES_MAX ];
@@ -404,6 +416,9 @@ struct gpiohandle_request {* this specifies the default output value, should be 0 (low) or * 1 (high), anything else than 0 or 1 will be interpreted as 1 (high) * @padding: reserved for future use and should be zero filled +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_config instead. */ struct gpiohandle_config {__u32 flags;
@@ -416,6 +431,9 @@ struct gpiohandle_config {* @values: when getting the state of lines this contains the current * state of a line, when setting the state of lines these should contain * the desired target state +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_values instead. */ struct gpiohandle_data {__u8 values[GPIOHANDLES_MAX ];
@@ -439,6 +457,9 @@ struct gpiohandle_data {* @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEEVENT_IOCTL operation, zero or negative value * means error +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_request instead. */ struct gpioevent_request {__u32 lineoffset;
@@ -458,6 +479,9 @@ struct gpioevent_request {* struct gpioevent_data - The actual event being pushed to userspace * @timestamp: best estimate of time of event occurrence, in nanoseconds * @id: event identifier +* +* This struct is part of ABI v1 and is deprecated. +* Use struct gpio_v2_line_event instead. */ struct gpioevent_data {__u64 timestamp;
@@ -482,6 +506,8 @@ struct gpioevent_data {/* * v1 ioctl()s +* +* These ioctl()s are deprecated. Use the v2 equivalent instead. */ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info)#define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request)
Signature · 1 line
2.28.0
Re: [PATCH v9 13/20] gpio: uapi: document uAPI v1 as deprecated
Quoted 6 lines · Kent Gibson
/*
* ABI v1
+ *
+ * This version of the ABI is deprecated and will be removed in the future.
+ * Use the latest version of the ABI, defined above, instead. */
How intentional is the wording here? It seems unrealistic that the v1 ABI would be removed any time soon if there are existing users and applications cannot yet rely on v2 to be present in all kernels, so it sounds like a hollow threat.
At the same time I can see that telling users it will be removed can lead to them moving on to the new version more quickly, so maybe a hollow threat is in fact appropriate here ;-)
Arnd
Re: [PATCH v9 13/20] gpio: uapi: document uAPI v1 as deprecated
Quoted 8 lines · Arnd Bergmann
Quoted 6 lines · Kent Gibson
/*
* ABI v1
+ *
+ * This version of the ABI is deprecated and will be removed in the future.
+ * Use the latest version of the ABI, defined above, instead. */
Quoted 4 lines
How intentional is the wording here? It seems unrealistic that the v1 ABI would be removed any time soon if there are existing users and applications cannot yet rely on v2 to be present in all kernels, so it sounds like a hollow threat.
I have similar thoughts when commenting on previous versions of this piece.
Quoted 3 lines
At the same time I can see that telling users it will be removed can lead to them moving on to the new version more quickly, so maybe a hollow threat is in fact appropriate here ;-)
Users all know that if something will be broken, they may escalate to Linus T. and get things reverted. So, above depends on the user's knowledge about the process.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 13/20] gpio: uapi: document uAPI v1 as deprecated
In reply to Arnd BergmannQuoted 8 lines · Arnd Bergmann
Quoted 6 lines · Kent Gibson
/*
* ABI v1
+ *
+ * This version of the ABI is deprecated and will be removed in the future.
+ * Use the latest version of the ABI, defined above, instead. */
Quoted 5 lines
How intentional is the wording here? It seems unrealistic that the v1 ABI would be removed any time soon if there are existing users and applications cannot yet rely on v2 to be present in all kernels, so it sounds like a hollow threat.
Andy had a similar comment regarding the build option, which I updated, but missed updating it here. The updated sentence ends at deprecated. I will update these to match in the next rev.
Quoted 4 lines
At the same time I can see that telling users it will be removed can lead to them moving on to the new version more quickly, so maybe a hollow threat is in fact appropriate here ;-)
That was the idea - though even the sysfs interface is still there and doesn't seem to be going anywhere in a hurry.
Cheers,
Kent.
Kent Gibson
reviewerPATCH v9 14/20 tools: gpio: port lsgpio to v2 uAPI
In reply to Kent GibsonPort the lsgpio tool to the latest GPIO uAPI.
--- tools/gpio/lsgpio.c | 60 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-)
tools/gpio/lsgpio.c +38 -22
@@ -25,57 +25,73 @@struct gpio_flag {char *name; -unsigned long mask; +unsigned long long mask; };struct gpio_flag flagnames[] = { { - .name ="kernel" , - .mask =GPIOLINE_FLAG_KERNEL , + .name ="used" , + .mask =GPIO_V2_LINE_FLAG_USED , + }, + { + .name ="input" , + .mask =GPIO_V2_LINE_FLAG_INPUT , }, { .name ="output" , - .mask =GPIOLINE_FLAG_IS_OUT , + .mask =GPIO_V2_LINE_FLAG_OUTPUT , }, { .name ="active-low" , - .mask =GPIOLINE_FLAG_ACTIVE_LOW , + .mask =GPIO_V2_LINE_FLAG_ACTIVE_LOW , }, { .name ="open-drain" , - .mask =GPIOLINE_FLAG_OPEN_DRAIN , + .mask =GPIO_V2_LINE_FLAG_OPEN_DRAIN , }, { .name ="open-source" , - .mask =GPIOLINE_FLAG_OPEN_SOURCE , + .mask =GPIO_V2_LINE_FLAG_OPEN_SOURCE , }, { .name ="pull-up" , - .mask =GPIOLINE_FLAG_BIAS_PULL_UP , + .mask =GPIO_V2_LINE_FLAG_BIAS_PULL_UP , }, { .name ="pull-down" , - .mask =GPIOLINE_FLAG_BIAS_PULL_DOWN , + .mask =GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN , }, { .name ="bias-disabled" , - .mask =GPIOLINE_FLAG_BIAS_DISABLE , + .mask =GPIO_V2_LINE_FLAG_BIAS_DISABLED , }, }; -void print_flags (unsigned long flags) +static void print_attributes (struct gpio_v2_line_info *info) {int i; -int printed =0 ; +const char *field_format ="%s" ;for (i =0 ; i <ARRAY_SIZE (flagnames); i++) { -if (flags & flagnames[i].mask) { -if (printed) -fprintf (stdout," " ); -fprintf (stdout,"%s" , flagnames[i].name); - printed++; +if (info->flags & flagnames[i].mask) { +fprintf (stdout, field_format, flagnames[i].name); + field_format =", %s" ; } } + +if ((info->flags &GPIO_V2_LINE_FLAG_EDGE_RISING ) && + (info->flags &GPIO_V2_LINE_FLAG_EDGE_FALLING )) +fprintf (stdout, field_format,"both-edges" ); +else if (info->flags &GPIO_V2_LINE_FLAG_EDGE_RISING ) +fprintf (stdout, field_format,"rising-edge" ); +else if (info->flags &GPIO_V2_LINE_FLAG_EDGE_FALLING ) +fprintf (stdout, field_format,"falling-edge" ); + +for (i =0 ; i < info->num_attrs; i++) { +if (info->attrs[i].id ==GPIO_V2_LINE_ATTR_ID_DEBOUNCE ) +fprintf (stdout,", debounce_period=%dusec" , + info->attrs[0 ].debounce_period_us); + } }int list_device (const char *device_name)
@@ -109,18 +125,18 @@ int list_device(const char *device_name)/* Loop over the lines and print info */ for (i =0 ; i < cinfo.lines; i++) { -struct gpioline_info linfo; +struct gpio_v2_line_info linfo;memset (&linfo,0 ,sizeof (linfo)); - linfo.line_offset = i; + linfo.offset = i; - ret =ioctl (fd,GPIO_GET_LINEINFO_IOCTL , &linfo); + ret =ioctl (fd,GPIO_V2_GET_LINEINFO_IOCTL , &linfo);if (ret ==-1 ) { ret = -errno;perror ("Failed to issue LINEINFO IOCTL\n" ); goto exit_close_error; } -fprintf (stdout,"\tline %2d:" , linfo.line_offset); +fprintf (stdout,"\tline %2d:" , linfo.offset);if (linfo.name[0 ])fprintf (stdout," \"%s\"" , linfo.name);else
@@ -131,7 +147,7 @@ int list_device(const char *device_name)fprintf (stdout," unused" );if (linfo.flags) {fprintf (stdout," [" ); -print_flags (linfo.flags); +print_attributes (&linfo);fprintf (stdout,"]" ); }fprintf (stdout,"\n" );
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 15/20 tools: gpio: port gpio-watch to v2 uAPI
In reply to Kent GibsonPort the gpio-watch tool to the latest GPIO uAPI.
--- tools/gpio/gpio-watch.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/gpio/gpio-watch.c +8 -8
@@ -21,8 +21,8 @@int main (int argc,char **argv) { -struct gpioline_info_changed chg; -struct gpioline_info req; +struct gpio_v2_line_info_changed chg; +struct gpio_v2_line_info req;struct pollfd pfd;int fd, i, j, ret;char *event, *end;
@@ -40,11 +40,11 @@ int main(int argc, char **argv)for (i =0 , j =2 ; i < argc -2 ; i++, j++) {memset (&req,0 ,sizeof (req)); - req.line_offset =strtoul (argv[j], &end,0 ); + req.offset =strtoul (argv[j], &end,0 );if (*end !='\0' ) goto err_usage; - ret =ioctl (fd,GPIO_GET_LINEINFO_WATCH_IOCTL , &req); + ret =ioctl (fd,GPIO_V2_GET_LINEINFO_WATCH_IOCTL , &req);if (ret) {perror ("unable to set up line watch" );return EXIT_FAILURE ;
@@ -71,13 +71,13 @@ int main(int argc, char **argv) }switch (chg.event_type) { -case GPIOLINE_CHANGED_REQUESTED : +case GPIO_V2_LINE_CHANGED_REQUESTED : event ="requested" ;break ; -case GPIOLINE_CHANGED_RELEASED : +case GPIO_V2_LINE_CHANGED_RELEASED : event ="released" ;break ; -case GPIOLINE_CHANGED_CONFIG : +case GPIO_V2_LINE_CHANGED_CONFIG : event ="config changed" ;break ;default :
@@ -87,7 +87,7 @@ int main(int argc, char **argv) }printf ("line %u: %s at %llu\n" , - chg.info.line_offset, event, chg.timestamp); + chg.info.offset, event, chg.timestamp_ns); } }
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 16/20 tools: gpio: rename nlines to num_lines
In reply to Kent GibsonRename nlines to num_lines to be consistent with other usage for fields describing the number of entries in an array.
--- tools/gpio/gpio-hammer.c | 26 +++++++++++++------------- tools/gpio/gpio-utils.c | 20 ++++++++++---------- tools/gpio/gpio-utils.h | 6 +++--- 3 files changed, 26 insertions(+), 26 deletions(-)
tools/gpio/gpio-hammer.c +13 -13
@@ -22,7 +22,7 @@#include <linux/gpio.h> #include "gpio-utils.h" -int hammer_device (const char *device_name,unsigned int *lines,int nlines, +int hammer_device (const char *device_name,unsigned int *lines,int num_lines,unsigned int loops) {struct gpiohandle_data data;
@@ -33,7 +33,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines,unsigned int iteration =0 ;memset (&data.values,0 ,sizeof (data.values)); - ret =gpiotools_request_linehandle (device_name, lines, nlines, + ret =gpiotools_request_linehandle (device_name, lines, num_lines,GPIOHANDLE_REQUEST_OUTPUT , &data,"gpio-hammer" );if (ret <0 )
@@ -46,15 +46,15 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, goto exit_close_error;fprintf (stdout,"Hammer lines [" ); -for (i =0 ; i < nlines; i++) { +for (i =0 ; i < num_lines; i++) {fprintf (stdout,"%d" , lines[i]); -if (i != (nlines -1 )) +if (i != (num_lines -1 ))fprintf (stdout,", " ); }fprintf (stdout,"] on %s, initial states: [" , device_name); -for (i =0 ; i < nlines; i++) { +for (i =0 ; i < num_lines; i++) {fprintf (stdout,"%d" , data.values[i]); -if (i != (nlines -1 )) +if (i != (num_lines -1 ))fprintf (stdout,", " ); }fprintf (stdout,"]\n" );
@@ -63,7 +63,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j =0 ;while (1 ) {/* Invert all lines so we blink */ -for (i =0 ; i < nlines; i++) +for (i =0 ; i < num_lines; i++) data.values[i] = !data.values[i]; ret =gpiotools_set_values (fd, &data);
@@ -81,9 +81,9 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j =0 ;fprintf (stdout,"[" ); -for (i =0 ; i < nlines; i++) { +for (i =0 ; i < num_lines; i++) {fprintf (stdout,"%d: %d" , lines[i], data.values[i]); -if (i != (nlines -1 )) +if (i != (num_lines -1 ))fprintf (stdout,", " ); }fprintf (stdout,"]\r" );
@@ -121,7 +121,7 @@ int main(int argc, char **argv)const char *device_name =NULL ;unsigned int lines[GPIOHANDLES_MAX ];unsigned int loops =0 ; -int nlines; +int num_lines;int c;int i;
@@ -158,11 +158,11 @@ int main(int argc, char **argv)return -1 ; } - nlines = i; + num_lines = i; -if (!device_name || !nlines) { +if (!device_name || !num_lines) {print_usage ();return -1 ; } -return hammer_device (device_name, lines, nlines, loops); +return hammer_device (device_name, lines, num_lines, loops); }
tools/gpio/gpio-utils.c +10 -10
@@ -38,7 +38,7 @@* such as "gpiochip0" * @lines: An array desired lines, specified by offset * index for the associated GPIO device. -* @nline: The number of lines to request. +* @num_lines: The number of lines to request. * @flag: The new flag for requsted gpio. Reference * "linux/gpio.h" for the meaning of flag. * @data: Default value will be set to gpio when flag is
@@ -56,7 +56,7 @@* On failure return the errno. */ int gpiotools_request_linehandle (const char *device_name,unsigned int *lines, -unsigned int nlines,unsigned int flag, +unsigned int num_lines,unsigned int flag,struct gpiohandle_data *data,const char *consumer_label) {
@@ -78,12 +78,12 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, goto exit_free_name; } -for (i =0 ; i < nlines; i++) +for (i =0 ; i < num_lines; i++) req.lineoffsets[i] = lines[i]; req.flags = flag;strcpy (req.consumer_label, consumer_label); - req.lines = nlines; + req.lines = num_lines;if (flag &GPIOHANDLE_REQUEST_OUTPUT )memcpy (req.default_values, data,sizeof (req.default_values));
@@ -194,20 +194,20 @@ int gpiotools_get(const char *device_name, unsigned int line)* such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. -* @nline: The number of lines to request. +* @num_lines: The number of lines to request. * @data: The array of values get from gpiochip. * * Return: On success return 0; * On failure return the errno. */ int gpiotools_gets (const char *device_name,unsigned int *lines, -unsigned int nlines,struct gpiohandle_data *data) +unsigned int num_lines,struct gpiohandle_data *data) {int fd;int ret;int ret_close; - ret =gpiotools_request_linehandle (device_name, lines, nlines, + ret =gpiotools_request_linehandle (device_name, lines, num_lines,GPIOHANDLE_REQUEST_INPUT , data,CONSUMER );if (ret <0 )
@@ -245,7 +245,7 @@ int gpiotools_set(const char *device_name, unsigned int line,* such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. -* @nline: The number of lines to request. +* @num_lines: The number of lines to request. * @data: The array of values set to gpiochip, must be * 0(low) or 1(high). *
@@ -253,11 +253,11 @@ int gpiotools_set(const char *device_name, unsigned int line,* On failure return the errno. */ int gpiotools_sets (const char *device_name,unsigned int *lines, -unsigned int nlines,struct gpiohandle_data *data) +unsigned int num_lines,struct gpiohandle_data *data) {int ret; - ret =gpiotools_request_linehandle (device_name, lines, nlines, + ret =gpiotools_request_linehandle (device_name, lines, num_lines,GPIOHANDLE_REQUEST_OUTPUT , data,CONSUMER );if (ret <0 )
tools/gpio/gpio-utils.h +3 -3
@@ -23,7 +23,7 @@ static inline int check_prefix(const char *str, const char *prefix) }int gpiotools_request_linehandle (const char *device_name,unsigned int *lines, -unsigned int nlines,unsigned int flag, +unsigned int num_lines,unsigned int flag,struct gpiohandle_data *data,const char *consumer_label);int gpiotools_set_values (const int fd,struct gpiohandle_data *data);
@@ -32,10 +32,10 @@ int gpiotools_release_linehandle(const int fd);int gpiotools_get (const char *device_name,unsigned int line);int gpiotools_gets (const char *device_name,unsigned int *lines, -unsigned int nlines,struct gpiohandle_data *data); +unsigned int num_lines,struct gpiohandle_data *data);int gpiotools_set (const char *device_name,unsigned int line,unsigned int value);int gpiotools_sets (const char *device_name,unsigned int *lines, -unsigned int nlines,struct gpiohandle_data *data); +unsigned int num_lines,struct gpiohandle_data *data);#endif /* _GPIO_UTILS_H_ */
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 17/20 tools: gpio: port gpio-hammer to v2 uAPI
In reply to Kent GibsonPort the gpio-hammer tool to the latest GPIO uAPI.
--- tools/gpio/gpio-hammer.c | 32 +++++--- tools/gpio/gpio-utils.c | 164 ++++++++++++++++++++++++++++++++------- tools/gpio/gpio-utils.h | 46 ++++++++++- 3 files changed, 197 insertions(+), 45 deletions(-)
tools/gpio/gpio-hammer.c +20 -12
@@ -25,23 +25,30 @@int hammer_device (const char *device_name,unsigned int *lines,int num_lines,unsigned int loops) { -struct gpiohandle_data data; +struct gpio_v2_line_values values; +struct gpio_v2_line_config config;char swirr[] ="-\\|/" ;int fd;int ret;int i, j;unsigned int iteration =0 ; -memset (&data.values,0 ,sizeof (data.values)); - ret =gpiotools_request_linehandle (device_name, lines, num_lines, -GPIOHANDLE_REQUEST_OUTPUT , &data, -"gpio-hammer" ); +memset (&config,0 ,sizeof (config)); + config.flags =GPIO_V2_LINE_FLAG_OUTPUT ; + + ret =gpiotools_request_line (device_name, lines, num_lines, + &config,"gpio-hammer" );if (ret <0 ) goto exit_error;else fd = ret; - ret =gpiotools_get_values (fd, &data); + values.mask =0 ; + values.bits =0 ; +for (i =0 ; i < num_lines; i++) +gpiotools_set_bit (&values.mask, i); + + ret =gpiotools_get_values (fd, &values);if (ret <0 ) goto exit_close_error;
@@ -53,7 +60,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, }fprintf (stdout,"] on %s, initial states: [" , device_name);for (i =0 ; i < num_lines; i++) { -fprintf (stdout,"%d" , data.values[i]); +fprintf (stdout,"%d" ,gpiotools_test_bit (values.bits, i));if (i != (num_lines -1 ))fprintf (stdout,", " ); }
@@ -64,14 +71,14 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines,while (1 ) {/* Invert all lines so we blink */ for (i =0 ; i < num_lines; i++) - data.values[i] = !data.values[i]; +gpiotools_change_bit (&values.bits, i); - ret =gpiotools_set_values (fd, &data); + ret =gpiotools_set_values (fd, &values);if (ret <0 ) goto exit_close_error;/* Re-read values to get status */ - ret =gpiotools_get_values (fd, &data); + ret =gpiotools_get_values (fd, &values);if (ret <0 ) goto exit_close_error;
@@ -82,7 +89,8 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines,fprintf (stdout,"[" );for (i =0 ; i < num_lines; i++) { -fprintf (stdout,"%d: %d" , lines[i], data.values[i]); +fprintf (stdout,"%d: %d" , lines[i], +gpiotools_test_bit (values.bits, i));if (i != (num_lines -1 ))fprintf (stdout,", " ); }
@@ -97,7 +105,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, ret =0 ; exit_close_error: -gpiotools_release_linehandle (fd); +gpiotools_release_line (fd); exit_error:return ret; }
tools/gpio/gpio-utils.c +135 -29
249 lines not on this page · raw message
tools/gpio/gpio-utils.h +42 -4
65 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v9 17/20] tools: gpio: port gpio-hammer to v2 uAPI
Quoted 2 lines · Kent Gibson
Port the gpio-hammer tool to the latest GPIO uAPI.
_BITUL() and _BITULL() are part of Linux uAPI. Why not to use them? const.h
Quoted 428 lines
Signed-off-by Kent Gibson <[email protected]>--- tools/gpio/gpio-hammer.c | 32 +++++--- tools/gpio/gpio-utils.c | 164 ++++++++++++++++++++++++++++++++------- tools/gpio/gpio-utils.h | 46 ++++++++++- 3 files changed, 197 insertions(+), 45 deletions(-)tools/gpio/gpio-hammer.c +20
-12@@ -25,23 +25,30 @@int hammer_device (const char *device_name,unsigned int *lines,int num_lines,unsigned int loops) { -struct gpiohandle_data data; +struct gpio_v2_line_values values; +struct gpio_v2_line_config config;char swirr[] ="-\\|/" ;int fd;int ret;int i, j;unsigned int iteration =0 ; -memset (&data.values,0 ,sizeof (data.values)); - ret =gpiotools_request_linehandle (device_name, lines, num_lines, -GPIOHANDLE_REQUEST_OUTPUT , &data, -"gpio-hammer" ); +memset (&config,0 ,sizeof (config)); + config.flags =GPIO_V2_LINE_FLAG_OUTPUT ; + + ret =gpiotools_request_line (device_name, lines, num_lines, + &config,"gpio-hammer" );if (ret <0 ) goto exit_error;else fd = ret; - ret =gpiotools_get_values (fd, &data); + values.mask =0 ; + values.bits =0 ; +for (i =0 ; i < num_lines; i++) +gpiotools_set_bit (&values.mask, i); + + ret =gpiotools_get_values (fd, &values);if (ret <0 ) goto exit_close_error;@@ -53,7 +60,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, }fprintf (stdout,"] on %s, initial states: [" , device_name);for (i =0 ; i < num_lines; i++) { -fprintf (stdout,"%d" , data.values[i]); +fprintf (stdout,"%d" ,gpiotools_test_bit (values.bits, i));if (i != (num_lines -1 ))fprintf (stdout,", " ); }@@ -64,14 +71,14 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines,while (1 ) {/* Invert all lines so we blink */ for (i =0 ; i < num_lines; i++) - data.values[i] = !data.values[i]; +gpiotools_change_bit (&values.bits, i); - ret =gpiotools_set_values (fd, &data); + ret =gpiotools_set_values (fd, &values);if (ret <0 ) goto exit_close_error;/* Re-read values to get status */ - ret =gpiotools_get_values (fd, &data); + ret =gpiotools_get_values (fd, &values);if (ret <0 ) goto exit_close_error;@@ -82,7 +89,8 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines,fprintf (stdout,"[" );for (i =0 ; i < num_lines; i++) { -fprintf (stdout,"%d: %d" , lines[i], data.values[i]); +fprintf (stdout,"%d: %d" , lines[i], +gpiotools_test_bit (values.bits, i));if (i != (num_lines -1 ))fprintf (stdout,", " ); }@@ -97,7 +105,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, ret =0 ; exit_close_error: -gpiotools_release_linehandle (fd); +gpiotools_release_line (fd); exit_error:return ret; }tools/gpio/gpio-utils.c +135
-29@@ -100,20 +100,87 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,free (chrdev_name);return ret <0 ? ret : req.fd; } + +/** +* gpiotools_request_line() - request gpio lines in a gpiochip +* @device_name: The name of gpiochip without prefix "/dev/", +* such as "gpiochip0" +* @lines: An array desired lines, specified by offset +* index for the associated GPIO device. +* @num_lines: The number of lines to request. +* @config: The new config for requested gpio. Reference +* "linux/gpio.h" for config details. +* @consumer: The name of consumer, such as "sysfs", +* "powerkey". This is useful for other users to +* know who is using. +* +* Request gpio lines through the ioctl provided by chardev. User +* could call gpiotools_set_values() and gpiotools_get_values() to +* read and write respectively through the returned fd. Call +* gpiotools_release_line() to release these lines after that. +* +* Return: On success return the fd; +* On failure return the errno. +*/ +int gpiotools_request_line (const char *device_name,unsigned int *lines, +unsigned int num_lines, +struct gpio_v2_line_config *config, +const char *consumer) +{ +struct gpio_v2_line_request req; +char *chrdev_name; +int fd; +int i; +int ret; + + ret =asprintf (&chrdev_name,"/dev/%s" , device_name); +if (ret <0 ) +return -ENOMEM ; + + fd =open (chrdev_name,0 ); +if (fd ==-1 ) { + ret = -errno; +fprintf (stderr,"Failed to open %s, %s\n" , + chrdev_name,strerror (errno)); + goto exit_free_name; + } + +memset (&req,0 ,sizeof (req)); +for (i =0 ; i < num_lines; i++) + req.offsets[i] = lines[i]; + + req.config = *config; +strcpy (req.consumer, consumer); + req.num_lines = num_lines; + + ret =ioctl (fd,GPIO_V2_GET_LINE_IOCTL , &req); +if (ret ==-1 ) { + ret = -errno; +fprintf (stderr,"Failed to issue %s (%d), %s\n" , +"GPIO_GET_LINE_IOCTL" , ret,strerror (errno)); + } + +if (close (fd) ==-1 ) +perror ("Failed to close GPIO character device file" ); +exit_free_name: +free (chrdev_name); +return ret <0 ? ret : req.fd; +} +/** * gpiotools_set_values(): Set the value of gpio(s) * @fd: The fd returned by -* gpiotools_request_linehandle(). -* @data: The array of values want to set. +* gpiotools_request_line(). +* @values: The array of values want to set. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_set_values (const int fd,struct gpiohandle_data *data) +int gpiotools_set_values (const int fd,struct gpio_v2_line_values *values) {int ret; - ret =ioctl (fd,GPIOHANDLE_SET_LINE_VALUES_IOCTL , data); + ret =ioctl (fd,GPIO_V2_LINE_SET_VALUES_IOCTL , values);if (ret ==-1 ) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n",@@ -127,17 +194,17 @@ int gpiotools_set_values(const int fd, struct gpiohandle_data *data)/** * gpiotools_get_values(): Get the value of gpio(s) * @fd: The fd returned by -* gpiotools_request_linehandle(). -* @data: The array of values get from hardware. +* gpiotools_request_line(). +* @values: The array of values get from hardware. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_get_values (const int fd,struct gpiohandle_data *data) +int gpiotools_get_values (const int fd,struct gpio_v2_line_values *values) {int ret; - ret =ioctl (fd,GPIOHANDLE_GET_LINE_VALUES_IOCTL , data); + ret =ioctl (fd,GPIO_V2_LINE_GET_VALUES_IOCTL , values);if (ret ==-1 ) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n",@@ -169,6 +236,27 @@ int gpiotools_release_linehandle(const int fd)return ret; } +/** +* gpiotools_release_line(): Release the line(s) of gpiochip +* @fd: The fd returned by +* gpiotools_request_line(). +* +* Return: On success return 0; +* On failure return the errno. +*/ +int gpiotools_release_line (const int fd) +{ +int ret; + + ret =close (fd); +if (ret ==-1 ) { +perror ("Failed to close GPIO LINE device file" ); + ret = -errno; + } + +return ret; +} +/** * gpiotools_get(): Get value from specific line * @device_name: The name of gpiochip without prefix "/dev/", @@ -180,11 +268,14 @@ int gpiotools_release_linehandle(const int fd)*/ int gpiotools_get (const char *device_name,unsigned int line) { -struct gpiohandle_data data; +int ret; +unsigned int value;unsigned int lines[] = {line}; -gpiotools_gets (device_name, lines,1 , &data); -return data.values[0 ]; + ret =gpiotools_gets (device_name, lines,1 , &value); +if (ret) +return ret; +return value; }@@ -195,27 +286,35 @@ int gpiotools_get(const char *device_name, unsigned int line)* @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. -* @data: The array of values get from gpiochip. +* @values: The array of values get from gpiochip. * * Return: On success return 0; * On failure return the errno. */ int gpiotools_gets (const char *device_name,unsigned int *lines, -unsigned int num_lines,struct gpiohandle_data *data) +unsigned int num_lines,unsigned int *values) { -int fd; +int fd, i;int ret;int ret_close; +struct gpio_v2_line_config config; +struct gpio_v2_line_values lv; - ret =gpiotools_request_linehandle (device_name, lines, num_lines, -GPIOHANDLE_REQUEST_INPUT , data, -CONSUMER ); +memset (&config,0 ,sizeof (config)); + config.flags =GPIO_V2_LINE_FLAG_INPUT ; + ret =gpiotools_request_line (device_name, lines, num_lines, + &config,CONSUMER );if (ret <0 )return ret; fd = ret; - ret =gpiotools_get_values (fd, data); - ret_close =gpiotools_release_linehandle (fd); +for (i =0 ; i < num_lines; i++) +gpiotools_set_bit (&lv.mask, i); + ret =gpiotools_get_values (fd, &lv); +if (!ret) +for (i =0 ; i < num_lines; i++) + values[i] =gpiotools_test_bit (lv.bits, i); + ret_close =gpiotools_release_line (fd);return ret <0 ? ret : ret_close; }@@ -232,11 +331,9 @@ int gpiotools_gets(const char *device_name, unsigned int *lines,int gpiotools_set (const char *device_name,unsigned int line,unsigned int value) { -struct gpiohandle_data data;unsigned int lines[] = {line}; - data.values[0 ] = value; -return gpiotools_sets (device_name, lines,1 , &data); +return gpiotools_sets (device_name, lines,1 , &value); }/** @@ -246,22 +343,31 @@ int gpiotools_set(const char *device_name, unsigned int line,* @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. -* @data: The array of values set to gpiochip, must be +* @value: The array of values set to gpiochip, must be * 0(low) or 1(high). * * Return: On success return 0; * On failure return the errno. */ int gpiotools_sets (const char *device_name,unsigned int *lines, -unsigned int num_lines,struct gpiohandle_data *data) +unsigned int num_lines,unsigned int *values) { -int ret; +int ret, i; +struct gpio_v2_line_config config; - ret =gpiotools_request_linehandle (device_name, lines, num_lines, -GPIOHANDLE_REQUEST_OUTPUT , data, -CONSUMER ); +memset (&config,0 ,sizeof (config)); + config.flags =GPIO_V2_LINE_FLAG_OUTPUT ; + config.num_attrs =1 ; + config.attrs[0 ].attr.id =GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ; +for (i =0 ; i < num_lines; i++) { +gpiotools_set_bit (&config.attrs[0 ].mask, i); +gpiotools_assign_bit (&config.attrs[0 ].attr.values, + i, values[i]); + } + ret =gpiotools_request_line (device_name, lines, num_lines, + &config,CONSUMER );if (ret <0 )return ret; -return gpiotools_release_linehandle (ret); +return gpiotools_release_line (ret); }tools/gpio/gpio-utils.h +42
-5@@ -12,7 +12,9 @@#ifndef _GPIO_UTILS_H_#define _GPIO_UTILS_H_ +#include <stdbool.h> #include <string.h> +#include <linux/types.h> #define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0]))@@ -26,16 +28,52 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, unsigned int num_lines, unsigned int flag, struct gpiohandle_data *data, const char *consumer_label); -int gpiotools_set_values (const int fd,struct gpiohandle_data *data); -int gpiotools_get_values (const int fd,struct gpiohandle_data *data);int gpiotools_release_linehandle (const int fd); +int gpiotools_request_line (const char *device_name, +unsigned int *lines, +unsigned int num_lines, +struct gpio_v2_line_config *config, +const char *consumer); +int gpiotools_set_values (const int fd,struct gpio_v2_line_values *values); +int gpiotools_get_values (const int fd,struct gpio_v2_line_values *values); +int gpiotools_release_line (const int fd); +int gpiotools_get (const char *device_name,unsigned int line);int gpiotools_gets (const char *device_name,unsigned int *lines, -unsigned int num_lines,struct gpiohandle_data *data); +unsigned int num_lines,unsigned int *values);int gpiotools_set (const char *device_name,unsigned int line,unsigned int value);int gpiotools_sets (const char *device_name,unsigned int *lines, -unsigned int num_lines,struct gpiohandle_data *data); +unsigned int num_lines,unsigned int *values); + +/* helper functions for gpio_v2_line_values bits */ +static inline void gpiotools_set_bit (__u64 *b,int n) +{ + *b |=1ULL << n; +} + +static inline void gpiotools_change_bit (__u64 *b,int n) +{ + *b ^=1ULL << n; +} + +static inline void gpiotools_clear_bit (__u64 *b,int n) +{ + *b &= ~(1ULL << n); +} + +static inline int gpiotools_test_bit (__u64 b,int n) +{ +return !!(b &1ULL << n); +} + +static inline void gpiotools_assign_bit (__u64 *b,int n,bool value) +{ +if (value) +gpiotools_set_bit (b, n); +else +gpiotools_clear_bit (b, n); +}#endif /* _GPIO_UTILS_H_ */ --2.28.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerRe: [PATCH v9 17/20] tools: gpio: port gpio-hammer to v2 uAPI
Quoted 7 lines · Andy Shevchenko
Quoted 2 lines · Kent Gibson
Port the gpio-hammer tool to the latest GPIO uAPI.
_BITUL()and_BITULL()are part of Linux uAPI. Why not to use them?const.h
Yeah, that is an oversight on my part - will change for v10.
Cheers,
Kent.
Kent Gibson
reviewerPATCH v9 18/20 tools: gpio: port gpio-event-mon to v2 uAPI
In reply to Kent GibsonPort the gpio-event-mon tool to the latest GPIO uAPI.
--- tools/gpio/gpio-event-mon.c | 91 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 44 deletions(-)
tools/gpio/gpio-event-mon.c +47 -44
@@ -23,17 +23,16 @@#include <sys/ioctl.h> #include <sys/types.h> #include <linux/gpio.h> +#include "gpio-utils.h" int monitor_device (const char *device_name,unsigned int line, -uint32_t handleflags, -uint32_t eventflags, +struct gpio_v2_line_config *config,unsigned int loops) { -struct gpioevent_request req; -struct gpiohandle_data data; +struct gpio_v2_line_values values;char *chrdev_name; -int fd; +int cfd, lfd;int ret;int i =0 ;
@@ -41,44 +40,39 @@ int monitor_device(const char *device_name,if (ret <0 )return -ENOMEM ; - fd =open (chrdev_name,0 ); -if (fd ==-1 ) { + cfd =open (chrdev_name,0 ); +if (cfd ==-1 ) { ret = -errno;fprintf (stderr,"Failed to open %s\n" , chrdev_name); goto exit_free_name; } - req.lineoffset = line; - req.handleflags = handleflags; - req.eventflags = eventflags; -strcpy (req.consumer_label,"gpio-event-mon" ); - - ret =ioctl (fd,GPIO_GET_LINEEVENT_IOCTL , &req); -if (ret ==-1 ) { - ret = -errno; -fprintf (stderr,"Failed to issue GET EVENT " -"IOCTL (%d)\n" , - ret); - goto exit_close_error; - } + ret =gpiotools_request_line (device_name, &line,1 , config, +"gpio-event-mon" ); +if (ret <0 ) + goto exit_device_close; +else + lfd = ret;/* Read initial states */ - ret =ioctl (req.fd,GPIOHANDLE_GET_LINE_VALUES_IOCTL , &data); -if (ret ==-1 ) { - ret = -errno; -fprintf (stderr,"Failed to issue GPIOHANDLE GET LINE " -"VALUES IOCTL (%d)\n" , + values.mask =1 ; + values.bits =0 ; + ret =gpiotools_get_values (lfd, &values); +if (ret <0 ) { +fprintf (stderr, +"Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n" , ret); - goto exit_close_error; + goto exit_line_close; }fprintf (stdout,"Monitoring line %d on %s\n" , line, device_name); -fprintf (stdout,"Initial line value: %d\n" , data.values[0 ]); +fprintf (stdout,"Initial line value: %d\n" , +gpiotools_test_bit (values.bits,0 ));while (1 ) { -struct gpioevent_data event; +struct gpio_v2_line_event event; - ret =read (req.fd, &event,sizeof (event)); + ret =read (lfd, &event,sizeof (event));if (ret ==-1 ) {if (errno == -EAGAIN ) {fprintf (stderr,"nothing available\n" );
@@ -96,12 +90,14 @@ int monitor_device(const char *device_name, ret = -EIO ;break ; } -fprintf (stdout,"GPIO EVENT %llu: " , event.timestamp); +fprintf (stdout,"GPIO EVENT at %llu on line %d (%d|%d) " , + event.timestamp_ns, event.offset, event.line_seqno, + event.seqno);switch (event.id) { -case GPIOEVENT_EVENT_RISING_EDGE : +case GPIO_V2_LINE_EVENT_RISING_EDGE :fprintf (stdout,"rising edge" );break ; -case GPIOEVENT_EVENT_FALLING_EDGE : +case GPIO_V2_LINE_EVENT_FALLING_EDGE :fprintf (stdout,"falling edge" );break ;default :
@@ -114,8 +110,11 @@ int monitor_device(const char *device_name,break ; } -exit_close_error: -if (close (fd) ==-1 ) +exit_line_close: +if (close (lfd) ==-1 ) +perror ("Failed to close line file" ); +exit_device_close: +if (close (cfd) ==-1 )perror ("Failed to close GPIO character device file" ); exit_free_name:free (chrdev_name);
@@ -140,15 +139,20 @@ void print_usage(void) ); } +#define EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) +int main (int argc,char **argv) {const char *device_name =NULL ;unsigned int line =-1 ;unsigned int loops =0 ; -uint32_t handleflags =GPIOHANDLE_REQUEST_INPUT ; -uint32_t eventflags =0 ; +struct gpio_v2_line_config config;int c; +memset (&config,0 ,sizeof (config)); + config.flags =GPIO_V2_LINE_FLAG_INPUT ;while ((c =getopt (argc, argv,"c:n:o:dsrf?" )) !=-1 ) {switch (c) {case 'c' :
@@ -161,16 +165,16 @@ int main(int argc, char **argv) line =strtoul (optarg,NULL ,10 );break ;case 'd' : - handleflags |=GPIOHANDLE_REQUEST_OPEN_DRAIN ; + config.flags |=GPIO_V2_LINE_FLAG_OPEN_DRAIN ;break ;case 's' : - handleflags |=GPIOHANDLE_REQUEST_OPEN_SOURCE ; + config.flags |=GPIO_V2_LINE_FLAG_OPEN_SOURCE ;break ;case 'r' : - eventflags |=GPIOEVENT_REQUEST_RISING_EDGE ; + config.flags |=GPIO_V2_LINE_FLAG_EDGE_RISING ;break ;case 'f' : - eventflags |=GPIOEVENT_REQUEST_FALLING_EDGE ; + config.flags |=GPIO_V2_LINE_FLAG_EDGE_FALLING ;break ;case '?' :print_usage ();
@@ -182,11 +186,10 @@ int main(int argc, char **argv)print_usage ();return -1 ; } -if (!eventflags) { +if (!(config.flags &EDGE_FLAGS )) {printf ("No flags specified, listening on both rising and " "falling edges\n" ); - eventflags =GPIOEVENT_REQUEST_BOTH_EDGES ; + config.flags |=EDGE_FLAGS ; } -return monitor_device (device_name, line, handleflags, - eventflags, loops); +return monitor_device (device_name, line, &config, loops); }
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 19/20 tools: gpio: add multi-line monitoring to gpio-event-mon
In reply to Kent GibsonExtend gpio-event-mon to support monitoring multiple lines. This would require multiple lineevent requests to implement using uAPI v1, but can be performed with a single line request using uAPI v2.
--- tools/gpio/gpio-event-mon.c | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-)
tools/gpio/gpio-event-mon.c +34 -11
@@ -26,7 +26,8 @@#include "gpio-utils.h" int monitor_device (const char *device_name, -unsigned int line, +unsigned int *lines, +unsigned int num_lines,struct gpio_v2_line_config *config,unsigned int loops) {
@@ -47,7 +48,7 @@ int monitor_device(const char *device_name, goto exit_free_name; } - ret =gpiotools_request_line (device_name, &line,1 , config, + ret =gpiotools_request_line (device_name, lines, num_lines, config,"gpio-event-mon" );if (ret <0 ) goto exit_device_close;
@@ -55,8 +56,10 @@ int monitor_device(const char *device_name, lfd = ret;/* Read initial states */ - values.mask =1 ; + values.mask =0 ; values.bits =0 ; +for (i =0 ; i < num_lines; i++) +gpiotools_set_bit (&values.mask, i); ret =gpiotools_get_values (lfd, &values);if (ret <0 ) { fprintf(stderr,
@@ -65,9 +68,23 @@ int monitor_device(const char *device_name, goto exit_line_close; } -fprintf (stdout,"Monitoring line %d on %s\n" , line, device_name); -fprintf (stdout,"Initial line value: %d\n" , -gpiotools_test_bit (values.bits,0 )); +if (num_lines ==1 ) { +fprintf (stdout,"Monitoring line %d on %s\n" , lines[0 ], device_name); +fprintf (stdout,"Initial line value: %d\n" , +gpiotools_test_bit (values.bits,0 )); + }else { +fprintf (stdout,"Monitoring lines %d" , lines[0 ]); +for (i =1 ; i < num_lines -1 ; i++) +fprintf (stdout,", %d" , lines[i]); +fprintf (stdout," and %d on %s\n" , lines[i], device_name); +fprintf (stdout,"Initial line values: %d" , +gpiotools_test_bit (values.bits,0 )); +for (i =1 ; i < num_lines -1 ; i++) +fprintf (stdout,", %d" , +gpiotools_test_bit (values.bits, i)); +fprintf (stdout," and %d\n" , +gpiotools_test_bit (values.bits, i)); + }while (1 ) {struct gpio_v2_line_event event;
@@ -126,7 +143,7 @@ void print_usage(void)fprintf (stderr,"Usage: gpio-event-mon [options]...\n" "Listen to events on GPIO lines, 0->1 1->0\n" " -n <name> Listen on GPIOs on a named device (must be stated)\n" -" -o <n> Offset to monitor\n" +" -o <n> Offset of line to monitor (may be repeated)\n" " -d Set line as open drain\n" " -s Set line as open source\n" " -r Listen for rising edges\n"
@@ -146,7 +163,8 @@ void print_usage(void)int main (int argc,char **argv) {const char *device_name =NULL ; -unsigned int line =-1 ; +unsigned int lines[GPIO_V2_LINES_MAX ]; +unsigned int num_lines =0 ;unsigned int loops =0 ;struct gpio_v2_line_config config;int c;
@@ -162,7 +180,12 @@ int main(int argc, char **argv) device_name = optarg;break ;case 'o' : - line =strtoul (optarg,NULL ,10 ); +if (num_lines >=GPIO_V2_LINES_MAX ) { +print_usage (); +return -1 ; + } + lines[num_lines] =strtoul (optarg,NULL ,10 ); + num_lines++;break ;case 'd' : config.flags |=GPIO_V2_LINE_FLAG_OPEN_DRAIN ;
@@ -182,7 +205,7 @@ int main(int argc, char **argv) } } -if (!device_name || line ==-1 ) { +if (!device_name || num_lines ==0 ) {print_usage ();return -1 ; }
@@ -191,5 +214,5 @@ int main(int argc, char **argv) "falling edges\n"); config.flags |=EDGE_FLAGS ; } -return monitor_device (device_name, line, &config, loops); +return monitor_device (device_name, lines, num_lines, &config, loops); }
Signature · 1 line
2.28.0
Kent Gibson
reviewerPATCH v9 20/20 tools: gpio: add debounce support to gpio-event-mon
In reply to Kent GibsonAdd support for debouncing monitored lines to gpio-event-mon.
--- tools/gpio/gpio-event-mon.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
tools/gpio/gpio-event-mon.c +17 -3
@@ -148,11 +148,12 @@ void print_usage(void)" -s Set line as open source\n" " -r Listen for rising edges\n" " -f Listen for falling edges\n" +" -b <n> Debounce the line with period n microseconds\n" " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n" " -? This helptext\n" "\n" "Example:\n" -"gpio-event-mon -n gpiochip0 -o 4 -r -f\n" +"gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n" ); }
@@ -167,11 +168,12 @@ int main(int argc, char **argv)unsigned int num_lines =0 ;unsigned int loops =0 ;struct gpio_v2_line_config config; -int c; +int c, attr, i; +unsigned long debounce_period_us =0 ;memset (&config,0 ,sizeof (config)); config.flags =GPIO_V2_LINE_FLAG_INPUT ; -while ((c =getopt (argc, argv,"c:n:o:dsrf?" )) !=-1 ) { +while ((c =getopt (argc, argv,"c:n:o:b:dsrf?" )) !=-1 ) {switch (c) {case 'c' : loops =strtoul (optarg,NULL ,10 );
@@ -187,6 +189,9 @@ int main(int argc, char **argv) lines[num_lines] =strtoul (optarg,NULL ,10 ); num_lines++;break ; +case 'b' : + debounce_period_us =strtoul (optarg,NULL ,10 ); +break ;case 'd' : config.flags |=GPIO_V2_LINE_FLAG_OPEN_DRAIN ;break ;
@@ -205,6 +210,15 @@ int main(int argc, char **argv) } } +if (debounce_period_us) { + attr = config.num_attrs; + config.num_attrs++; +for (i =0 ; i < num_lines; i++) +gpiotools_set_bit (&config.attrs[attr].mask, i); + config.attrs[attr].attr.id =GPIO_V2_LINE_ATTR_ID_DEBOUNCE ; + config.attrs[attr].attr.debounce_period_us = debounce_period_us; + } +if (!device_name || num_lines ==0 ) {print_usage ();return -1 ;
Signature · 1 line
2.28.0
Quoted 8 lines · Kent Gibson
This patchset defines and implements a new version of the GPIO CDEV uAPI to address existing 32/64-bit alignment issues, add support for debounce, event sequence numbers, and allow for requested lines with different configurations.
It provides some future proofing by adding optional configuration fields and padding reserved for future use.
Quoted 7 lines
The series can be partitioned into three blocks; the first two patches are minor fixes that impact later patches, the next eleven contain the v2 uAPI definition and implementation, and the final seven port the GPIO tools to the v2 uAPI and extend them to use new uAPI features.
The more complicated patches include their own commentary where appropriate.
For tools (there are minor comments, which not prevent to have a tag):
For the rest I gave some comments but most of them are simply to address. The uAPI definition I agree with after Arnd's comment. I don't see big impediments to having this for v5.10.
Thanks!
Quoted 126 lines
Cheers,
Kent.Changes for v9:
- references to function names should include braces (patch 02)
- add time scale suffixes to timestamp (_ns) and debounce period (_us) variables (patch 04 and later)
- address multiple review comments (patch 04)
- drop mention of future API removal (patch 06)
- usestatic_assert()rather thanBUILD_BUG_ON()(patch 07)
- change event buffer overflow behaviour to discard old events rather than recent events (patch 09)
- add spaces around '*' in '*16' (patch 09)
- reword comments regarding field access and locking (patch 09 and 12)Changes for v8:
- fixBUILD_BUG_ONconditions and relocate them before the return ingpiolib_cdev_register()(patch 07)Changes for v7:
- use_BITULLfor ULL flag definitions (patch 04)
- add check onkmalloc_arrayreturn value inlinereq_get_values()(patch 07) andlinereq_set_values_unlocked()(patch 11)
- restore v1 functions used by gpio-mockup selftests (patch 17)Changes for v6:
- flags variable inlinereq_create()should be u64 not unsigned long (patch 07)
- remove restrictions on configuration changes - any change from one valid state to another valid state is allowed. (patches 09, 10, 12)Changes for v5:
All changes for v5 fix issues with the
gpiolib-cdev.cimplementation, in patches 07-12.
The uAPI is unchanged from v4, as is the port of the tools.- use
IS_ALIGNEDinBUILD_BUG_ONchecks (patch 07)
- relocateBUILD_BUG_ONchecks togpiolib_cdev_register(patch 07)
- s/requies/requires/ (patch 07)
- use unsigned int for variables that are never negative
- changelineinfo_get()parameter from cmd to bool watch (patch 08)
- flagsv2 ingpio_v2_line_info_to_v1()should be u64, not int (patch 08)
- change "_locked" suffixed function names to "_unlocked" (patch 10 and
11)
- be less eager breaking long lines
- move commentary into checkin comment where appropriate - particularly patch 12
- restructure the request/line split - rename struct line to struct linereq, and structedge_detectorto struct line, and relocate the desc field from linereq to line. The linereq name was selected overline_requestas function names such aslinereq_set_values()are more clearly associated with requests thanline_request_set_values(), particularly as there is also a struct line. And linereq is as informative as linerequest, so I went with the shortened form.Changes for v4:
- bitmap width clarification ingpiod.h(patch 04)
- fix info offset initialisation bug (patch 08 and inserting patch 01)
- replace strncpy with strscpy to remove compiler warnings (patch 08 and inserting patch 02)
- fix mask handling inline_get_values(patch 07)Changes for v3:
- disabling the character device from the build requires EXPERT
- uAPI revisions (see patch 02)
- replacepadding_not_zeroedwith calls tomemchr_inv
- don't use bitops on 64-bit flags as that doesn't work on BE-32
- accept first attribute matching a line in gpio_v2_line_config.attrs rather than the last
- rework lsgpio port to uAPI v2 as flags reverted to v1 like layout (since patch v2)
- swapped patches 17 and 18 to apply debounce to multiple monitored linesChanges for v2:
- split out cleanup patches into a separate series.
- split implementation patch into a patch for each ioctl or major feature.
- split tool port patch into a patch per tool.
- rework uAPI to allow requested lines with different configurations.Kent Gibson (20):
gpiolib: cdev:gpio_desc_to_lineinfo()should set info offset
gpiolib: cdev: replacestrncpy()withstrscpy()
gpio: uapi: defineGPIO_MAX_NAME_SIZEfor array sizes
gpio: uapi: define uAPI v2
gpiolib: make cdev a build option
gpiolib: add build option for CDEV v1 ABI
gpiolib: cdev: supportGPIO_V2_GET_LINE_IOCTLand
GPIO_V2_LINE_GET_VALUES_IOCTL
gpiolib: cdev: supportGPIO_V2_GET_LINEINFO_IOCTLand
GPIO_V2_GET_LINEINFO_WATCH_IOCTL
gpiolib: cdev: support edge detection for uAPI v2
gpiolib: cdev: supportGPIO_V2_LINE_SET_CONFIG_IOCTL
gpiolib: cdev: supportGPIO_V2_LINE_SET_VALUES_IOCTL
gpiolib: cdev: support setting debounce
gpio: uapi: document uAPI v1 as deprecated
tools: gpio: port lsgpio to v2 uAPI
tools: gpio: port gpio-watch to v2 uAPI
tools: gpio: rename nlines tonum_lines
tools: gpio: port gpio-hammer to v2 uAPI
tools: gpio: port gpio-event-mon to v2 uAPI
tools: gpio: add multi-line monitoring to gpio-event-mon
tools: gpio: add debounce support to gpio-event-mondrivers/gpio/Kconfig | 29 +- drivers/gpio/Makefile | 2 +- drivers/gpio/gpiolib-cdev.c | 1531 +++++++++++++++++++++++++++++++---- drivers/gpio/gpiolib-cdev.h | 15 + drivers/gpio/gpiolib.c | 5 + drivers/gpio/gpiolib.h | 6 + include/uapi/linux/gpio.h | 334 +++++++- tools/gpio/gpio-event-mon.c | 146 ++-- tools/gpio/gpio-hammer.c | 56 +- tools/gpio/gpio-utils.c | 176 +++- tools/gpio/gpio-utils.h | 48 +- tools/gpio/gpio-watch.c | 16 +- tools/gpio/lsgpio.c | 60 +- 13 files changed, 2098 insertions(+), 326 deletions(-)base-commit:
36eccdb58fb55d2bea6a0e62932e4d7e5192d409--
2.28.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kent Gibson
reviewerQuoted 17 lines · Andy Shevchenko
Quoted 8 lines · Kent Gibson
This patchset defines and implements a new version of the GPIO CDEV uAPI to address existing 32/64-bit alignment issues, add support for debounce, event sequence numbers, and allow for requested lines with different configurations.
It provides some future proofing by adding optional configuration fields and padding reserved for future use.Quoted 7 lines
The series can be partitioned into three blocks; the first two patches are minor fixes that impact later patches, the next eleven contain the v2 uAPI definition and implementation, and the final seven port the GPIO tools to the v2 uAPI and extend them to use new uAPI features.
The more complicated patches include their own commentary where appropriate.
Quoted 9 lines
For tools (there are minor comments, which not prevent to have a tag):
Reviewed-by Andy Shevchenko <[email protected]>For the rest I gave some comments but most of them are simply to address. The uAPI definition I agree with after Arnd's comment. I don't see big impediments to having this for v5.10.
Thanks!
Thanks for your review - I nearly always learn something new from them, and you can be picky pain in the arse at times - which is a good thing for a reviewer. Apart from the pain in the arse ;-).
Cheers,
Kent.
Quoted 2 lines · Kent Gibson
Quoted 1 line · Andy Shevchenko
On Tue, Sep 22, 2020 at 5:34 AM Kent Gibson <[email protected]> wrote:
...
Quoted 10 lines
Quoted 6 lines
For the rest I gave some comments but most of them are simply to address. The uAPI definition I agree with after Arnd's comment. I don't see big impediments to having this for v5.10.
Thanks!
Thanks for your review - I nearly always learn something new from them, and you can be picky pain in the arse at times - which is a good thing for a reviewer. Apart from the pain in the arse ;-).
Thanks for being patient!
As a reviewer I'm also in constant learning.
Signature · 2 lines
With Best Regards, Andy Shevchenko