media: pxa_camera: avoid duplicate s_power calls
PATCH 1/1 media: pxa_camera: avoid duplicate s_power calls
The open() operation for the pxa_camera driver always calls s_power() operation to put its subdevice sensor in normal operation mode, and the release() operation always call s_power() operation to put the subdevice in power saving mode.
This requires the subdevice sensor driver to keep track of its power state in order to avoid putting the subdevice in power saving mode while the device is still opened by some users.
Many subdevice drivers handle it by the boilerplate code that increments and decrements an internal counter in s_power() like below:
/*
* If the power count is modified from 0 to != 0 or from != 0 to 0,
* update the power state.
*/
if (sensor->power_count == !on) {
ret = ov5640_set_power(sensor, !!on);
if (ret)
goto out;
}
/* Update the power count. */
sensor->power_count += on ? 1 : -1;However, some subdevice drivers don't handle it and may cause a problem with the pxa_camera driver if the video device is opened by more than two users at the same time.
Instead of propagating the boilerplate code for each subdevice driver that implement s_power, this introduces an trick that many V4L2 drivers are using with v4l2_fh_is_singular_file().
--- drivers/media/platform/pxa_camera.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/media/platform/pxa_camera.c +12 -5
@@ -2040,6 +2040,9 @@ static int pxac_fops_camera_open(struct file *filp)if (ret <0 ) goto out; +if (!v4l2_fh_is_singular_file (filp)) + goto out; + ret =sensor_call (pcdev, core, s_power,1 );if (ret)v4l2_fh_release (filp);
@@ -2052,13 +2055,17 @@ static int pxac_fops_camera_release(struct file *filp) {struct pxa_camera_dev *pcdev =video_drvdata (filp);int ret; - - ret =vb2_fop_release (filp); -if (ret <0 ) -return ret; +bool fh_singular;mutex_lock (&pcdev->mlock); - ret =sensor_call (pcdev, core, s_power,0 ); + + fh_singular =v4l2_fh_is_singular_file (filp); + + ret =_vb2_fop_release (filp,NULL ); + +if (fh_singular) + ret =sensor_call (pcdev, core, s_power,0 ); +mutex_unlock (&pcdev->mlock);return ret;
Signature · 1 line
2.7.4
Dear Mita-san,
Quoted 25 lines · Akinobu Mita
The
open()operation for thepxa_cameradriver always callss_power()operation to put its subdevice sensor in normal operation mode, and therelease()operation always calls_power()operation to put the subdevice in power saving mode.This requires the subdevice sensor driver to keep track of its power state in order to avoid putting the subdevice in power saving mode while the device is still opened by some users.
Many subdevice drivers handle it by the boilerplate code that increments and decrements an internal counter in
s_power()like below:/* * If the power count is modified from 0 to != 0 or from != 0 to 0, * update the power state. */ if (sensor->power_count == !on) { ret = ov5640_set_power(sensor, !!on); if (ret) goto out; } /* Update the power count. */ sensor->power_count += on ? 1 : -1;
Quoted 7 lines
However, some subdevice drivers don't handle it and may cause a problem with the
pxa_cameradriver if the video device is opened by more than two users at the same time.Instead of propagating the boilerplate code for each subdevice driver that implement
s_power, this introduces an trick that many V4L2 drivers are using withv4l2_fh_is_singular_file().
I'd rather like that the sub-device drivers would move to use runtime PM instead of depending on the s_power() callback. It's much cleaner that way.
It's not a near-term solution though. The approach seems fine, please see comments below though.
Quoted 28 lines
Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/platform/pxa_camera.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/media/platform/pxa_camera.c +3
-2@@ -2040,6 +2040,9 @@ static int pxac_fops_camera_open(struct file *filp)if (ret <0 ) goto out; +if (!v4l2_fh_is_singular_file (filp)) + goto out; + ret =sensor_call (pcdev, core, s_power,1 );if (ret)v4l2_fh_release (filp);@@ -2052,13 +2055,17 @@ static int pxac_fops_camera_release(struct file *filp) {struct pxa_camera_dev *pcdev =video_drvdata (filp);int ret; - - ret =vb2_fop_release (filp);
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2064
Quoted 10 lines
drivers/media/platform/pxa_camera.c +5
-3-if (ret <0 ) -return ret; +bool fh_singular;mutex_lock (&pcdev->mlock); - ret =sensor_call (pcdev, core, s_power,0 ); + + fh_singular =v4l2_fh_is_singular_file (filp); + + ret =_vb2_fop_release (filp,NULL );
I'm not sure whether using the return value to return an error from release is really useful. If you want to use it, I'd shout loud instead.
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2066
Quoted 2 lines
drivers/media/platform/pxa_camera.c +2
-0+ +if (fh_singular)
ret assigned previously is overwritten here without checking.
Quoted 5 lines
drivers/media/platform/pxa_camera.c +2
-0+ ret =sensor_call (pcdev, core, s_power,0 ); +mutex_unlock (&pcdev->mlock);return ret;
Signature · 2 lines
Sakari Ailus [email protected]
2018-05-22 22:59 GMT+09:00 Sakari Ailus <[email protected]>:
Quoted 32 lines
Dear Mita-san,
Quoted 29 lines · Akinobu Mita
The
open()operation for thepxa_cameradriver always callss_power()operation to put its subdevice sensor in normal operation mode, and therelease()operation always calls_power()operation to put the subdevice in power saving mode.This requires the subdevice sensor driver to keep track of its power state in order to avoid putting the subdevice in power saving mode while the device is still opened by some users.
Many subdevice drivers handle it by the boilerplate code that increments and decrements an internal counter in
s_power()like below:/* * If the power count is modified from 0 to != 0 or from != 0 to 0, * update the power state. */ if (sensor->power_count == !on) { ret = ov5640_set_power(sensor, !!on); if (ret) goto out; } /* Update the power count. */ sensor->power_count += on ? 1 : -1;However, some subdevice drivers don't handle it and may cause a problem with the
pxa_cameradriver if the video device is opened by more than two users at the same time.
Quoted 6 lines
Quoted 3 lines
Instead of propagating the boilerplate code for each subdevice driver that implement
s_power, this introduces an trick that many V4L2 drivers are using withv4l2_fh_is_singular_file().I'd rather like that the sub-device drivers would move to use runtime PM instead of depending on the
s_power()callback. It's much cleaner that way.
Sounds good.
I'll look into whether some sensor drivers can be converted to use it.
Quoted 35 lines
It's not a near-term solution though. The approach seems fine, please see comments below though.
Quoted 32 lines
Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/platform/pxa_camera.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/media/platform/pxa_camera.c +4
-4@@ -2040,6 +2040,9 @@ static int pxac_fops_camera_open(struct file *filp)if (ret <0 ) goto out; +if (!v4l2_fh_is_singular_file (filp)) + goto out; + ret =sensor_call (pcdev, core, s_power,1 );if (ret)v4l2_fh_release (filp);@@ -2052,13 +2055,17 @@ static int pxac_fops_camera_release(struct file *filp) {struct pxa_camera_dev *pcdev =video_drvdata (filp);int ret; - - ret =vb2_fop_release (filp); -if (ret <0 ) -return ret; +bool fh_singular;
Quoted 9 lines
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2064
Quoted 6 lines
drivers/media/platform/pxa_camera.c +4
-1mutex_lock (&pcdev->mlock); - ret =sensor_call (pcdev, core, s_power,0 ); + + fh_singular =v4l2_fh_is_singular_file (filp); + + ret =_vb2_fop_release (filp,NULL );I'm not sure whether using the return value to return an error from release is really useful. If you want to use it, I'd shout loud instead.
What is the best way to handle these errors in release?
AFAICS, vb2_fop_release() always returns zero for now and most platform drivers don't use return value from s_power() calling with on == 0.
So ignoring both of vb2_fop_release error and s_power error makes sense?
Quoted 14 lines
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2066
Quoted 2 lines
drivers/media/platform/pxa_camera.c +2
-0+ +if (fh_singular)ret assigned previously is overwritten here without checking.
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2071
Quoted 5 lines
drivers/media/platform/pxa_camera.c +2
-0+ ret =sensor_call (pcdev, core, s_power,0 ); +mutex_unlock (&pcdev->mlock);return ret;--
Sakari Ailus
[email protected]
Quoted 88 lines · Akinobu Mita
2018-05-22 22:59 GMT+09:00 Sakari Ailus <[email protected]>:
Quoted 32 lines
Dear Mita-san,
Quoted 29 lines · Akinobu Mita
The
open()operation for thepxa_cameradriver always callss_power()operation to put its subdevice sensor in normal operation mode, and therelease()operation always calls_power()operation to put the subdevice in power saving mode.This requires the subdevice sensor driver to keep track of its power state in order to avoid putting the subdevice in power saving mode while the device is still opened by some users.
Many subdevice drivers handle it by the boilerplate code that increments and decrements an internal counter in
s_power()like below:/* * If the power count is modified from 0 to != 0 or from != 0 to 0, * update the power state. */ if (sensor->power_count == !on) { ret = ov5640_set_power(sensor, !!on); if (ret) goto out; } /* Update the power count. */ sensor->power_count += on ? 1 : -1;However, some subdevice drivers don't handle it and may cause a problem with the
pxa_cameradriver if the video device is opened by more than two users at the same time.Quoted 6 lines
Quoted 3 lines
Instead of propagating the boilerplate code for each subdevice driver that implement
s_power, this introduces an trick that many V4L2 drivers are using withv4l2_fh_is_singular_file().I'd rather like that the sub-device drivers would move to use runtime PM instead of depending on the
s_power()callback. It's much cleaner that way.Sounds good.
I'll look into whether some sensor drivers can be converted to use it.Quoted 35 lines
It's not a near-term solution though. The approach seems fine, please see comments below though.
Quoted 32 lines
Cc Sakari Ailus <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Signed-off-by Akinobu Mita <[email protected]>--- drivers/media/platform/pxa_camera.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/media/platform/pxa_camera.c +4
-4@@ -2040,6 +2040,9 @@ static int pxac_fops_camera_open(struct file *filp)if (ret <0 ) goto out; +if (!v4l2_fh_is_singular_file (filp)) + goto out; + ret =sensor_call (pcdev, core, s_power,1 );if (ret)v4l2_fh_release (filp);@@ -2052,13 +2055,17 @@ static int pxac_fops_camera_release(struct file *filp) {struct pxa_camera_dev *pcdev =video_drvdata (filp);int ret; - - ret =vb2_fop_release (filp); -if (ret <0 ) -return ret; +bool fh_singular;Quoted 9 lines
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2064
Quoted 6 lines
drivers/media/platform/pxa_camera.c +4
-1mutex_lock (&pcdev->mlock); - ret =sensor_call (pcdev, core, s_power,0 ); + + fh_singular =v4l2_fh_is_singular_file (filp); + + ret =_vb2_fop_release (filp,NULL );I'm not sure whether using the return value to return an error from release is really useful. If you want to use it, I'd shout loud instead.
Quoted 1 line
What is the best way to handle these errors in release?
I'd suggest dev_warn(), for instance. These things generally happen rarely (or not at all) and there's not much that can be reasonably done to mitigate them, if that is even needed. Such a warning message in a log could be useful if someone reports a bug related to this.
Quoted 5 lines
AFAICS,
vb2_fop_release()always returns zero for now and most platform drivers don't use return value froms_power()calling with on == 0.So ignoring both of
vb2_fop_releaseerror ands_powererror makes sense?
Ignoring them completely is an option, too. I don't have a strong opinion either way.
Quoted 12 lines
Quoted 12 lines
drivers/media/platform/pxa_camera.c static int pxac_fops_camera_release(struct file *filp) · line 2066
Quoted 3 lines
drivers/media/platform/pxa_camera.c +2
-0+ +if (fh_singular)ret assigned previously is overwritten here without checking.
Quoted 5 lines
drivers/media/platform/pxa_camera.c +2
-0+ ret =sensor_call (pcdev, core, s_power,0 ); +mutex_unlock (&pcdev->mlock);return ret;
Signature · 2 lines
Sakari Ailus [email protected]