gpiolib: cdev: Refactor allocation of linereq events kfifo
authorKent Gibson <warthog618@gmail.com>
Wed, 29 May 2024 13:19:52 +0000 (21:19 +0800)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Thu, 30 May 2024 09:29:10 +0000 (11:29 +0200)
The allocation of the linereq events kfifo is performed in two separate
places.  Add a helper function to remove the duplication.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240529131953.195777-3-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
drivers/gpio/gpiolib-cdev.c

index d4e47960cc98b2c03c88149cc705fdb366cae233..c7218c9f2c5e7daafe478fa7b423eaf3b4773c74 100644 (file)
@@ -1128,6 +1128,14 @@ static void edge_detector_stop(struct line *line)
        /* do not change line->level - see comment in debounced_value() */
 }
 
+static int edge_detector_fifo_init(struct linereq *req)
+{
+       if (kfifo_initialized(&req->events))
+               return 0;
+
+       return kfifo_alloc(&req->events, req->event_buffer_size, GFP_KERNEL);
+}
+
 static int edge_detector_setup(struct line *line,
                               struct gpio_v2_line_config *lc,
                               unsigned int line_idx, u64 edflags)
@@ -1139,9 +1147,8 @@ static int edge_detector_setup(struct line *line,
        char *label;
 
        eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
-       if (eflags && !kfifo_initialized(&line->req->events)) {
-               ret = kfifo_alloc(&line->req->events,
-                                 line->req->event_buffer_size, GFP_KERNEL);
+       if (eflags) {
+               ret = edge_detector_fifo_init(line->req);
                if (ret)
                        return ret;
        }
@@ -1193,8 +1200,6 @@ static int edge_detector_update(struct line *line,
                                struct gpio_v2_line_config *lc,
                                unsigned int line_idx, u64 edflags)
 {
-       u64 eflags;
-       int ret;
        u64 active_edflags = READ_ONCE(line->edflags);
        unsigned int debounce_period_us =
                        gpio_v2_line_config_debounce_period(lc, line_idx);
@@ -1210,14 +1215,9 @@ static int edge_detector_update(struct line *line,
                 * ensure event fifo is initialised if edge detection
                 * is now enabled.
                 */
-               eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
-               if (eflags && !kfifo_initialized(&line->req->events)) {
-                       ret = kfifo_alloc(&line->req->events,
-                                         line->req->event_buffer_size,
-                                         GFP_KERNEL);
-                       if (ret)
-                               return ret;
-               }
+               if (edflags & GPIO_V2_LINE_EDGE_FLAGS)
+                       return edge_detector_fifo_init(line->req);
+
                return 0;
        }