提交 883f9198 编写于 作者: K Kent Gibson 提交者: Bartosz Golaszewski

gpiolib: cdev: refactor linehandle cleanup into linehandle_free

Consolidate the cleanup of linehandles, currently duplicated in
linehandle_create and linehandle_release, into a helper function
linehandle_free.
Signed-off-by: NKent Gibson <warthog618@gmail.com>
Signed-off-by: NBartosz Golaszewski <bgolaszewski@baylibre.com>
上级 1bf7ba40
...@@ -228,17 +228,21 @@ static long linehandle_ioctl_compat(struct file *file, unsigned int cmd, ...@@ -228,17 +228,21 @@ static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
} }
#endif #endif
static int linehandle_release(struct inode *inode, struct file *file) static void linehandle_free(struct linehandle_state *lh)
{ {
struct linehandle_state *lh = file->private_data;
struct gpio_device *gdev = lh->gdev;
int i; int i;
for (i = 0; i < lh->num_descs; i++) for (i = 0; i < lh->num_descs; i++)
gpiod_free(lh->descs[i]); if (lh->descs[i])
gpiod_free(lh->descs[i]);
kfree(lh->label); kfree(lh->label);
put_device(&lh->gdev->dev);
kfree(lh); kfree(lh);
put_device(&gdev->dev); }
static int linehandle_release(struct inode *inode, struct file *file)
{
linehandle_free(file->private_data);
return 0; return 0;
} }
...@@ -257,7 +261,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -257,7 +261,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
struct gpiohandle_request handlereq; struct gpiohandle_request handlereq;
struct linehandle_state *lh; struct linehandle_state *lh;
struct file *file; struct file *file;
int fd, i, count = 0, ret; int fd, i, ret;
u32 lflags; u32 lflags;
if (copy_from_user(&handlereq, ip, sizeof(handlereq))) if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
...@@ -288,6 +292,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -288,6 +292,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
} }
} }
lh->num_descs = handlereq.lines;
/* Request each GPIO */ /* Request each GPIO */
for (i = 0; i < handlereq.lines; i++) { for (i = 0; i < handlereq.lines; i++) {
u32 offset = handlereq.lineoffsets[i]; u32 offset = handlereq.lineoffsets[i];
...@@ -295,19 +301,18 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -295,19 +301,18 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
if (IS_ERR(desc)) { if (IS_ERR(desc)) {
ret = PTR_ERR(desc); ret = PTR_ERR(desc);
goto out_free_descs; goto out_free_lh;
} }
ret = gpiod_request(desc, lh->label); ret = gpiod_request(desc, lh->label);
if (ret) if (ret)
goto out_free_descs; goto out_free_lh;
lh->descs[i] = desc; lh->descs[i] = desc;
count = i + 1;
linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags); linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
ret = gpiod_set_transitory(desc, false); ret = gpiod_set_transitory(desc, false);
if (ret < 0) if (ret < 0)
goto out_free_descs; goto out_free_lh;
/* /*
* Lines have to be requested explicitly for input * Lines have to be requested explicitly for input
...@@ -318,11 +323,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -318,11 +323,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
ret = gpiod_direction_output(desc, val); ret = gpiod_direction_output(desc, val);
if (ret) if (ret)
goto out_free_descs; goto out_free_lh;
} else if (lflags & GPIOHANDLE_REQUEST_INPUT) { } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
ret = gpiod_direction_input(desc); ret = gpiod_direction_input(desc);
if (ret) if (ret)
goto out_free_descs; goto out_free_lh;
} }
blocking_notifier_call_chain(&desc->gdev->notifier, blocking_notifier_call_chain(&desc->gdev->notifier,
...@@ -331,12 +336,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -331,12 +336,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
offset); offset);
} }
lh->num_descs = handlereq.lines;
fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
ret = fd; ret = fd;
goto out_free_descs; goto out_free_lh;
} }
file = anon_inode_getfile("gpio-linehandle", file = anon_inode_getfile("gpio-linehandle",
...@@ -368,13 +372,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -368,13 +372,8 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
out_put_unused_fd: out_put_unused_fd:
put_unused_fd(fd); put_unused_fd(fd);
out_free_descs:
for (i = 0; i < count; i++)
gpiod_free(lh->descs[i]);
kfree(lh->label);
out_free_lh: out_free_lh:
kfree(lh); linehandle_free(lh);
put_device(&gdev->dev);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册