Skip to content

Commit

Permalink
gpiolib: Separate GPIO_GET_LINEINFO_WATCH_IOCTL conditional
Browse files Browse the repository at this point in the history
We already have two conditionals inside the outer one to check
if the command is GPIO_GET_LINEINFO_WATCH_IOCTL. I think it's
time to actually do what I have proposed in the first place, i.e.
to separate GPIO_GET_LINEINFO_WATCH_IOCTL from GPIO_GET_LINEINFO_IOCTL.
It's +13 LOCs, and surprisingly only +13 bytes of binary on x86_32,
but for the price of much better readability.

Signed-off-by: Andy Shevchenko <[email protected]>
Acked-by: Bartosz Golaszewski <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Linus Walleij <[email protected]>
  • Loading branch information
andy-shev authored and linusw committed May 27, 2020
1 parent ce1d966 commit a34d5e5
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,7 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
return -EFAULT;
return 0;
} else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
struct gpioline_info lineinfo;

if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
Expand All @@ -1278,23 +1277,37 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

hwgpio = gpio_chip_hwgpio(desc);

if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL &&
test_bit(hwgpio, priv->watched_lines))
return -EBUSY;

gpio_desc_to_lineinfo(desc, &lineinfo);

if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
return -EFAULT;

if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL)
set_bit(hwgpio, priv->watched_lines);

return 0;
} else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
return linehandle_create(gdev, ip);
} else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
return lineevent_create(gdev, ip);
} else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
struct gpioline_info lineinfo;

if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
return -EFAULT;

desc = gpiochip_get_desc(gc, lineinfo.line_offset);
if (IS_ERR(desc))
return PTR_ERR(desc);

hwgpio = gpio_chip_hwgpio(desc);

if (test_bit(hwgpio, priv->watched_lines))
return -EBUSY;

gpio_desc_to_lineinfo(desc, &lineinfo);

if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
return -EFAULT;

set_bit(hwgpio, priv->watched_lines);
return 0;
} else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
if (copy_from_user(&offset, ip, sizeof(offset)))
return -EFAULT;
Expand Down

0 comments on commit a34d5e5

Please sign in to comment.