media: uvcvideo: fix control restore on reset_resume
Subsystems (2)
cover media: uvcvideo: fix control restore on reset_resume
uvc_ctrl_restore_values() restores each entity's modified controls in index order via the transactional commit machinery (uvc_ctrl_commit_entity()). On devices where SET_CUR on a manual control STALLs while its master auto control is enabled, the commit switches to rollback on the first failure: the remaining controls of the entity are skipped, later entities are not restored, and the rollback copies UVC_CTRL_DATA_BACKUP over UVC_CTRL_DATA_CURRENT for every dirty control in the entity - but BACKUP is never populated on the restore path, so the cached values are corrupted with stale data. __uvc_resume() then returns early and uvc_status_resume() never runs, leaving the status (interrupt) endpoint dead after resume. Observed on a Logitech C270 (046d:0825, USB_QUIRK_RESET_RESUME): SET_CUR on WHITE_BALANCE_TEMPERATURE (selector 10 on unit 2) gets UVC error code 7 (Invalid Request) while auto WB is enabled. The only userspace-visible effect is "reset_resume error -5"; on such devices, every resume of a camera whose controls have been written fails the restore. Kernel log:
restoring control 00000000-...-000000000101/6/10
restoring control 00000000-...-000000000101/12/11
Control error 7 (Invalid request) for (SET_CUR) control 10 on unit 2 UVC non compliance: Error -5 restoring control White Balance Temperature (selector 10 on unit 2)
reset_resume error -5
patch 1/3 reports the failing control (V4L2 name, entity id, selector, errno) when the restore fails; diagnostics only, and independently useful.
patch 2/3 restores the auto (master) controls first and skips inactive manual controls, so SET_CUR is not sent for a slave while its master is on. Because the restore does not populate the cache, a master whose state is unknown (not restored in the first pass and not cached) is queried with GET_CUR rather than assumed; slaves skipped as inactive have their cached state invalidated.
patch 3/3 stops using the transactional commit machinery for the
restore: each control is restored individually, failures are recorded and the remaining controls and entities are still restored, the cached state is not corrupted by rollback, and __uvc_resume() returns the first restore error while always resuming the status endpoint - a status URB submission failure takes priority over the already-logged best-effort restore error.
Verified on the C270: with auto WB on, the WB temperature restore is skipped as inactive and no error occurs; with manual WB active, WB auto is restored first and then WB temperature; PLF and gain are restored correctly. A 120-trial census across repeated suspend/reset_resume/capture cycles (three control sequences x two formats x 20 trials, camera autosuspended before every open so every open runs the full reset_resume restore) completed with 0 wedged images, 0 restore errors and 0 recovery interventions. Returning the error is
safe: the USB core treats a reset_resume error as informational (usb_resume_interface()).
Unrelated: the same camera also has a firmware-level race around AWB mode writes near stream start (userspace-visible; in our testing it fires only when stream start does not follow a reset_resume). May be addressed by a quirk series later.
Chris
Signature · 1 line
2.53.0
PATCH 1/3 media: uvcvideo: Report the failing control when restoring controls fails
When uvc_ctrl_commit_entity() fails while restoring the controls after a USB reset_resume, the only message visible at the default loglevel is the generic "reset_resume error -5" from the USB core. The control that caused the failure and the reason for it are only visible with the uvcvideo trace parameter enabled (UVC_DBG_CONTROL), which makes the problem almost impossible to diagnose in the field.
On a Logitech C270 (046d:0825, USB_QUIRK_RESET_RESUME), the processing unit restore fails with UVC error code 7 (Invalid Request), mapped to -EIO, and the user only sees "reset_resume error -5". The restore order suggests that WHITE_BALANCE_TEMPERATURE is written while auto white balance is enabled, which the device rejects.
Pass the err_ctrl out parameter of uvc_ctrl_commit_entity() and report the name, the selector and the entity id of the failing control, together with the error code, using a ratelimited warning. If the failing control is not known, report the entity and the error code only.
Also extend the "Control error %u" debug message in uvc_query_ctrl() with the name of the UVC error code, the name of the request, the selector and the unit, so that the cause of the STALL is visible in the trace log.
--- drivers/media/usb/uvc/uvc_ctrl.c | 36 +++++++++++++++++++++++++++++-- drivers/media/usb/uvc/uvc_video.c | 31 +++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +34 -2
@@ -3199,6 +3199,34 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,* Suspend/resume */ +/* +* Report a failure to restore the controls of an entity. @err_ctrl points to +* the control that failed, or is NULL if the failing control is not known. +*/ +static void uvc_ctrl_restore_error (struct uvc_device *dev, +const struct uvc_entity *entity, +struct uvc_control *err_ctrl,int ret) +{ +const struct uvc_control_mapping *mapping; +const char *name ="Unknown Control" ; + +if (!err_ctrl) { +dev_warn_ratelimited (&dev->intf->dev, +"Failed to restore controls on unit %u: %d\n" , + entity->id, ret); +return ; + } + + mapping =list_first_entry_or_null (&err_ctrl->info.mappings, + struct uvc_control_mapping, list); +if (mapping) + name =uvc_map_get_name (mapping); + +dev_warn_ratelimited (&dev->intf->dev, +"Failed to restore control %s (selector %u on unit %u): %d\n" , + name, err_ctrl->info.selector, entity->id, ret); +} +/* * Restore control values after resume, skipping controls that haven't been * changed.
@@ -3210,6 +3238,7 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,*/ int uvc_ctrl_restore_values (struct uvc_device *dev) { +struct uvc_control *err_ctrl;struct uvc_control *ctrl;struct uvc_entity *entity;unsigned int i;
@@ -3230,9 +3259,12 @@ int uvc_ctrl_restore_values(struct uvc_device *dev) ctrl->dirty =1 ; } - ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 ,NULL ); -if (ret <0 ) + err_ctrl =NULL ; + ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , &err_ctrl); +if (ret <0 ) { +uvc_ctrl_restore_error (dev, entity, err_ctrl, ret);return ret; + } }return 0 ;
drivers/media/usb/uvc/uvc_video.c +30 -1
@@ -68,6 +68,32 @@ static const char *uvc_query_name(u8 query) } } +static const char *uvc_error_code_name (u8 error) +{ +switch (error) { +case 0 : +return "No error" ; +case 1 : +return "Not ready" ; +case 2 : +return "Wrong state" ; +case 3 : +return "Power" ; +case 4 : +return "Out of range" ; +case 5 : +return "Invalid unit" ; +case 6 : +return "Invalid control" ; +case 7 : +return "Invalid request" ; +case 8 : +return "Invalid value within range" ; +default : +return "Unknown" ; + } +} +int uvc_query_ctrl (struct uvc_device *dev,u8 query,u8 unit,u8 intfnum,u8 cs,void *data,u16 size) {
@@ -125,7 +151,10 @@ int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,return ret <0 ? ret : -EPIPE ; } -uvc_dbg (dev,CONTROL ,"Control error %u\n" , error); +uvc_dbg (dev,CONTROL , +"Control error %u (%s) for %s control %u on unit %u\n" , + error,uvc_error_code_name (error),uvc_query_name (query), cs, + unit);switch (error) {case 0 :
Signature · 1 line
2.53.0
Re: [PATCH 1/3] media: uvcvideo: Report the failing control when restoring controls fails
Hi Chris
Quoted 45 lines · Chris Bainbridge
When
uvc_ctrl_commit_entity()fails while restoring the controls after a USBreset_resume, the only message visible at the default loglevel is the generic "reset_resumeerror -5" from the USB core. The control that caused the failure and the reason for it are only visible with the uvcvideo trace parameter enabled (UVC_DBG_CONTROL), which makes the problem almost impossible to diagnose in the field.On a Logitech C270 (046d:0825,
USB_QUIRK_RESET_RESUME), the processing unit restore fails with UVC error code 7 (Invalid Request), mapped to-EIO, and the user only sees "reset_resumeerror -5". The restore order suggests thatWHITE_BALANCE_TEMPERATUREis written while auto white balance is enabled, which the device rejects.Pass the
err_ctrlout parameter ofuvc_ctrl_commit_entity()and report the name, the selector and the entity id of the failing control, together with the error code, using a ratelimited warning. If the failing control is not known, report the entity and the error code only.Also extend the "Control error %u" debug message in
uvc_query_ctrl()with the name of the UVC error code, the name of the request, the selector and the unit, so that the cause of the STALL is visible in the trace log.Signed-off-by Chris Bainbridge <[email protected]>--- drivers/media/usb/uvc/uvc_ctrl.c | 36 +++++++++++++++++++++++++++++-- drivers/media/usb/uvc/uvc_video.c | 31 +++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-)drivers/media/usb/uvc/uvc_ctrl.c +7
-0@@ -3199,6 +3199,34 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,* Suspend/resume */ +/* +* Report a failure to restore the controls of an entity. @err_ctrl points to +* the control that failed, or is NULL if the failing control is not known. +*/ +static void uvc_ctrl_restore_error (struct uvc_device *dev, +const struct uvc_entity *entity, +struct uvc_control *err_ctrl,int ret)
drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 3218
Quoted 10 lines
drivers/media/usb/uvc/uvc_ctrl.c +10
-0+{ +const struct uvc_control_mapping *mapping; +const char *name ="Unknown Control" ; + +if (!err_ctrl) { +dev_warn_ratelimited (&dev->intf->dev, +"Failed to restore controls on unit %u: %d\n" , + entity->id, ret); +return ; + }
Unless I made a mistake there i no case where err_ctrl can be NULL.
drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 3223
Quoted 5 lines
drivers/media/usb/uvc/uvc_ctrl.c +5
-0+ + mapping =list_first_entry_or_null (&err_ctrl->info.mappings, + struct uvc_control_mapping, list); +if (mapping) + name =uvc_map_get_name (mapping);
can you move this into a function uvc_ctrl_get_first_mapping_name(err_ctrl)
with that move this function would be so small that it is probably a good idea to squash it into uvc_ctrl_restore_values
Quoted 10 lines
drivers/media/usb/uvc/uvc_ctrl.c +6
-0+ +dev_warn_ratelimited (&dev->intf->dev, +"Failed to restore control %s (selector %u on unit %u): %d\n" , + name, err_ctrl->info.selector, entity->id, ret); +} +/* * Restore control values after resume, skipping controls that haven't been * changed.
drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 3241
Quoted 4 lines
drivers/media/usb/uvc/uvc_ctrl.c +1
-0@@ -3210,6 +3238,7 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,*/ int uvc_ctrl_restore_values (struct uvc_device *dev) { +struct uvc_control *err_ctrl;
please do err_ctrl = NULL here and not in the loop. We probably do not need it here either, but will make the static analyzer happy.
Quoted 23 lines
drivers/media/usb/uvc/uvc_ctrl.c +5
-2struct uvc_control *ctrl;struct uvc_entity *entity;unsigned int i;@@ -3230,9 +3259,12 @@ int uvc_ctrl_restore_values(struct uvc_device *dev) ctrl->dirty =1 ; } - ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 ,NULL ); -if (ret <0 ) + err_ctrl =NULL ; + ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , &err_ctrl); +if (ret <0 ) { +uvc_ctrl_restore_error (dev, entity, err_ctrl, ret);return ret; + } }return 0 ;drivers/media/usb/uvc/uvc_video.c +0
-0
drivers/media/usb/uvc/uvc_video.c static const char *uvc_query_name(u8 query) · line 70
Quoted 3 lines
drivers/media/usb/uvc/uvc_video.c +0
-0@@ -68,6 +68,32 @@ static const char *uvc_query_name(u8 query) } }
Maybe you could make a function
static const char *uvc_ctrl_error_name_code(u8 error, int *code)
and reuse it in uvc_query_ctrl() it is much more nicer to get the error name than the code
(sorry to treat you like Santa :P)
Quoted 45 lines
drivers/media/usb/uvc/uvc_video.c +30
-2+static const char *uvc_error_code_name (u8 error) +{ +switch (error) { +case 0 : +return "No error" ; +case 1 : +return "Not ready" ; +case 2 : +return "Wrong state" ; +case 3 : +return "Power" ; +case 4 : +return "Out of range" ; +case 5 : +return "Invalid unit" ; +case 6 : +return "Invalid control" ; +case 7 : +return "Invalid request" ; +case 8 : +return "Invalid value within range" ; +default : +return "Unknown" ; + } +} +int uvc_query_ctrl (struct uvc_device *dev,u8 query,u8 unit,u8 intfnum,u8 cs,void *data,u16 size) {@@ -125,7 +151,10 @@ int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,return ret <0 ? ret : -EPIPE ; } -uvc_dbg (dev,CONTROL ,"Control error %u\n" , error); +uvc_dbg (dev,CONTROL , +"Control error %u (%s) for %s control %u on unit %u\n" , + error,uvc_error_code_name (error),uvc_query_name (query), cs, + unit);switch (error) {case 0 : --2.53.0
Signature · 1 line
Ricardo Ribalda
PATCH 2/3 media: uvcvideo: Restore auto controls first and skip inactive slaves
In reply to Chris BainbridgeA UVC device rejects a SET_CUR request for a manual control while its master auto control is enabled. On the Logitech C270 (046d:0825), restoring WHITE_BALANCE_TEMPERATURE while WHITE_BALANCE_TEMPERATURE_AUTO is enabled stalls the request with UVC error code 7 (Invalid Request) and leaves automatic white balance wedged until power cycle, while aborting the restoration of the remaining controls of the processing unit. Two rules are needed to avoid this.
First, restore the master controls before the other controls of each entity. uvc_ctrl_commit_entity() sends the controls of an entity in index order: Auto-Exposure Mode (index 1) precedes Exposure Time (index 3) in the camera terminal, but Focus Absolute (index 5) precedes Focus Auto (index 17), and in the processing unit Hue (index 2), White Balance Temperature (index 6) and White Balance Component (index 7) all precede their corresponding auto controls (indices 11, 12 and 13). After a reset the device has auto white balance enabled at its default, so even when the user has set the auto control to manual mode, the manual value is written while the device still has auto enabled, and is rejected. Restore the masters first (committing the first pass only when at least one master is dirty), so that manual controls are written with their master in its final state. A control is a master if any of its mappings has a non-zero slave_ids[] entry.
Second, skip the slave controls that are currently inactive. When the master was restored in the first pass or is already loaded in the cache, decode its value without generating any USB traffic; otherwise query the master via GET_CUR using __uvc_ctrl_load_cur() (taking struct uvc_device directly). If the master control is write-only or GET_CUR fails, treat the slave as active rather than decoding an unpopulated zero buffer, matching uvc_ctrl_is_accessible(). Clear ctrl->loaded when skipping an inactive slave so that subsequent reads fetch the firmware-driven value from the hardware, even on devices that omit UVC_CONTROL_CAP_AUTOUPDATE in GET_INFO.
This addresses the restore order concern noted in the TODO comment for auto/manual control pairs.
--- drivers/media/usb/uvc/uvc_ctrl.c | 152 +++++++++++++++++++++++++------ 1 file changed, 126 insertions(+), 26 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +126 -26
@@ -1463,7 +1463,7 @@ static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,return 0 ; } -static int __uvc_ctrl_load_cur (struct uvc_video_chain *chain, +static int __uvc_ctrl_load_cur (struct uvc_device *dev,struct uvc_control *ctrl) {u8 *data;
@@ -1482,13 +1482,12 @@ static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain, }if (ctrl->entity->get_cur) - ret = ctrl->entity->get_cur (chain->dev, ctrl->entity, + ret = ctrl->entity->get_cur (dev, ctrl->entity, ctrl->info.selector, data, ctrl->info.size);else - ret =uvc_query_ctrl (chain->dev,UVC_GET_CUR , - ctrl->entity->id, chain->dev->intfnum, - ctrl->info.selector, data, + ret =uvc_query_ctrl (dev,UVC_GET_CUR , ctrl->entity->id, + dev->intfnum, ctrl->info.selector, data, ctrl->info.size);if (ret <0 )
@@ -1509,7 +1508,7 @@ static int __uvc_ctrl_get(struct uvc_video_chain *chain,if ((ctrl->info.flags &UVC_CTRL_FLAG_GET_CUR ) ==0 )return -EACCES ; - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);if (ret <0 )return ret;
@@ -2572,7 +2571,7 @@ static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,return -ENOMEM ;if (which ==V4L2_CTRL_WHICH_CUR_VAL ) - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);else ret =uvc_ctrl_populate_cache (chain, ctrl);
@@ -2816,7 +2815,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)* operation. */ if ((ctrl->info.size *8 ) != mapping->size) { - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);if (ret <0 )return ret; }
@@ -3227,43 +3226,144 @@ static void uvc_ctrl_restore_error(struct uvc_device *dev, name, err_ctrl->info.selector, entity->id, ret); } +/* +* Return true if the control is a master control, i.e. one of its mappings +* has slave controls. +*/ +static bool uvc_ctrl_is_master (const struct uvc_control *ctrl) +{ +const struct uvc_control_mapping *mapping; +unsigned int i; + +list_for_each_entry (mapping, &ctrl->info.mappings, list) { +for (i =0 ; i <ARRAY_SIZE (mapping->slave_ids); ++i) { +if (mapping->slave_ids[i]) +return true; + } + } + +return false; +} + +/* +* Return true if the control is currently inactive, i.e. it is a slave +* control whose master is not in manual mode. +* +* The master value is read from the driver's cache when available, or queried +* from the device via GET_CUR if the master was neither restored in the first +* pass nor loaded yet. If the master control cannot be found in the same +* entity, is write-only, or fails GET_CUR, the control is considered active, +* matching uvc_ctrl_is_accessible(). +*/ +static bool uvc_ctrl_is_inactive (struct uvc_device *dev, +struct uvc_entity *entity, +struct uvc_control *ctrl) +{ +struct uvc_control_mapping *mapping; + +list_for_each_entry (mapping, &ctrl->info.mappings, list) { +struct uvc_control_mapping *master_map =NULL ; +struct uvc_control *master_ctrl =NULL ; +s32 val; + +if (!mapping->master_id) +continue ; + +__uvc_find_control (entity, mapping->master_id, &master_map, + &master_ctrl,0 ,0 ); + +if (!master_ctrl ||uvc_ctrl_mapping_is_compound (master_map)) +continue ; + +if (!(master_ctrl->modified && + (master_ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) && + !master_ctrl->loaded) { +if (!(master_ctrl->info.flags & +UVC_CTRL_FLAG_GET_CUR ) || +__uvc_ctrl_load_cur (dev, master_ctrl) <0 ) +continue ; + } + + val =uvc_mapping_get_s32 (master_map,UVC_GET_CUR , +uvc_ctrl_data (master_ctrl, +UVC_CTRL_DATA_CURRENT )); +if (val != mapping->master_manual) +return true; + } + +return false; +} +/* * Restore control values after resume, skipping controls that haven't been * changed. * +* The controls of each entity are restored in two passes: first the master +* controls (the auto modes), then the remaining controls. This way a master +* is set to its final state before its slaves are restored. +* +* Slave controls that are currently inactive, because their master is not +* in manual mode, are not restored: devices reject SET_CUR requests for +* them and some firmwares misbehave when written in auto mode. The manual +* value only takes effect once the master is switched back to manual mode. +* * TODO * - Don't restore modified controls that are back to their default value. -* - Handle restore order (Auto-Exposure Mode should be restored before -* Exposure Time). */ int uvc_ctrl_restore_values (struct uvc_device *dev) {struct uvc_control *err_ctrl;struct uvc_control *ctrl;struct uvc_entity *entity; -unsigned int i; +unsigned int i, pass;int ret;/* Walk the entities list and restore controls when possible. */ list_for_each_entry (entity, &dev->entities, list) { +for (pass =0 ; pass <2 ; ++pass) { +bool master_pass = pass ==0 ; +bool dirty = false; + +for (i =0 ; i < entity->ncontrols; ++i) { + ctrl = &entity->controls[i]; + +if (!ctrl->initialized || !ctrl->modified || + !(ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) +continue ; + +if (uvc_ctrl_is_master (ctrl) != master_pass) +continue ; + +if (!master_pass && +uvc_ctrl_is_inactive (dev, entity, ctrl)) { +uvc_dbg (dev,CONTROL , +"skipping restore of inactive control %pUl/%u/%u\n" , + ctrl->info.entity, + ctrl->info.index, + ctrl->info.selector); + ctrl->loaded =0 ; +continue ; + } + +uvc_dbg (dev,CONTROL , +"restoring control %pUl/%u/%u\n" , + ctrl->info.entity, ctrl->info.index, + ctrl->info.selector); + ctrl->dirty =1 ; + dirty = true; + } -for (i =0 ; i < entity->ncontrols; ++i) { - ctrl = &entity->controls[i]; - -if (!ctrl->initialized || !ctrl->modified || - (ctrl->info.flags &UVC_CTRL_FLAG_RESTORE ) ==0 ) +if (master_pass && !dirty)continue ; -uvc_dbg (dev,CONTROL ,"restoring control %pUl/%u/%u\n" , - ctrl->info.entity, ctrl->info.index, - ctrl->info.selector); - ctrl->dirty =1 ; - } - err_ctrl =NULL ; - ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , &err_ctrl); -if (ret <0 ) { -uvc_ctrl_restore_error (dev, entity, err_ctrl, ret); -return ret; + err_ctrl =NULL ; + ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , + &err_ctrl); +if (ret <0 ) { +uvc_ctrl_restore_error (dev, entity, err_ctrl, + ret); +return ret; + } } }
Signature · 1 line
2.53.0
Re: [PATCH 2/3] media: uvcvideo: Restore auto controls first and skip inactive slaves
Hi Chris
I think it would make a bit more sense to squash 2/3 and 3/3. Maybe split to 3/3 the functionality to continue even if there is a restore error.
That would make it much easier to review. Could you do that before we look into all the boring nits?
(you might not be Santa... but it is almost Black Friday :P)
PS: You will also probably get more attention if you simplify your commit messages.
Thanks!
Quoted 265 lines · Chris Bainbridge
A UVC device rejects a
SET_CURrequest for a manual control while its master auto control is enabled. On the Logitech C270 (046d:0825), restoringWHITE_BALANCE_TEMPERATUREwhileWHITE_BALANCE_TEMPERATURE_AUTOis enabled stalls the request with UVC error code 7 (Invalid Request) and leaves automatic white balance wedged until power cycle, while aborting the restoration of the remaining controls of the processing unit. Two rules are needed to avoid this.First, restore the master controls before the other controls of each entity.
uvc_ctrl_commit_entity()sends the controls of an entity in index order: Auto-Exposure Mode (index 1) precedes Exposure Time (index 3) in the camera terminal, but Focus Absolute (index 5) precedes Focus Auto (index 17), and in the processing unit Hue (index 2), White Balance Temperature (index 6) and White Balance Component (index 7) all precede their corresponding auto controls (indices 11, 12 and 13). After a reset the device has auto white balance enabled at its default, so even when the user has set the auto control to manual mode, the manual value is written while the device still has auto enabled, and is rejected. Restore the masters first (committing the first pass only when at least one master is dirty), so that manual controls are written with their master in its final state. A control is a master if any of its mappings has a non-zero slave_ids[] entry.Second, skip the slave controls that are currently inactive. When the master was restored in the first pass or is already loaded in the cache, decode its value without generating any USB traffic; otherwise query the master via
GET_CURusing__uvc_ctrl_load_cur()(taking structuvc_devicedirectly). If the master control is write-only orGET_CURfails, treat the slave as active rather than decoding an unpopulated zero buffer, matchinguvc_ctrl_is_accessible(). Clearctrl->loadedwhen skipping an inactive slave so that subsequent reads fetch the firmware-driven value from the hardware, even on devices that omitUVC_CONTROL_CAP_AUTOUPDATEinGET_INFO.This addresses the restore order concern noted in the TODO comment for auto/manual control pairs.
Fixes c0efd232929c ("V4L/DVB (8145a): USB Video Class driver")Signed-off-by Chris Bainbridge <[email protected]>--- drivers/media/usb/uvc/uvc_ctrl.c | 152 +++++++++++++++++++++++++------ 1 file changed, 126 insertions(+), 26 deletions(-)drivers/media/usb/uvc/uvc_ctrl.c +126
-27@@ -1463,7 +1463,7 @@ static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,return 0 ; } -static int __uvc_ctrl_load_cur (struct uvc_video_chain *chain, +static int __uvc_ctrl_load_cur (struct uvc_device *dev,struct uvc_control *ctrl) {u8 *data;@@ -1482,13 +1482,12 @@ static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain, }if (ctrl->entity->get_cur) - ret = ctrl->entity->get_cur (chain->dev, ctrl->entity, + ret = ctrl->entity->get_cur (dev, ctrl->entity, ctrl->info.selector, data, ctrl->info.size);else - ret =uvc_query_ctrl (chain->dev,UVC_GET_CUR , - ctrl->entity->id, chain->dev->intfnum, - ctrl->info.selector, data, + ret =uvc_query_ctrl (dev,UVC_GET_CUR , ctrl->entity->id, + dev->intfnum, ctrl->info.selector, data, ctrl->info.size);if (ret <0 )@@ -1509,7 +1508,7 @@ static int __uvc_ctrl_get(struct uvc_video_chain *chain,if ((ctrl->info.flags &UVC_CTRL_FLAG_GET_CUR ) ==0 )return -EACCES ; - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);if (ret <0 )return ret;@@ -2572,7 +2571,7 @@ static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,return -ENOMEM ;if (which ==V4L2_CTRL_WHICH_CUR_VAL ) - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);else ret =uvc_ctrl_populate_cache (chain, ctrl);@@ -2816,7 +2815,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)* operation. */ if ((ctrl->info.size *8 ) != mapping->size) { - ret =__uvc_ctrl_load_cur (chain, ctrl); + ret =__uvc_ctrl_load_cur (chain->dev, ctrl);if (ret <0 )return ret; }@@ -3227,43 +3226,144 @@ static void uvc_ctrl_restore_error(struct uvc_device *dev, name, err_ctrl->info.selector, entity->id, ret); } +/* +* Return true if the control is a master control, i.e. one of its mappings +* has slave controls. +*/ +static bool uvc_ctrl_is_master (const struct uvc_control *ctrl) +{ +const struct uvc_control_mapping *mapping; +unsigned int i; + +list_for_each_entry (mapping, &ctrl->info.mappings, list) { +for (i =0 ; i <ARRAY_SIZE (mapping->slave_ids); ++i) { +if (mapping->slave_ids[i]) +return true; + } + } + +return false; +} + +/* +* Return true if the control is currently inactive, i.e. it is a slave +* control whose master is not in manual mode. +* +* The master value is read from the driver's cache when available, or queried +* from the device via GET_CUR if the master was neither restored in the first +* pass nor loaded yet. If the master control cannot be found in the same +* entity, is write-only, or fails GET_CUR, the control is considered active, +* matching uvc_ctrl_is_accessible(). +*/ +static bool uvc_ctrl_is_inactive (struct uvc_device *dev, +struct uvc_entity *entity, +struct uvc_control *ctrl) +{ +struct uvc_control_mapping *mapping; + +list_for_each_entry (mapping, &ctrl->info.mappings, list) { +struct uvc_control_mapping *master_map =NULL ; +struct uvc_control *master_ctrl =NULL ; +s32 val; + +if (!mapping->master_id) +continue ; + +__uvc_find_control (entity, mapping->master_id, &master_map, + &master_ctrl,0 ,0 ); + +if (!master_ctrl ||uvc_ctrl_mapping_is_compound (master_map)) +continue ; + +if (!(master_ctrl->modified && + (master_ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) && + !master_ctrl->loaded) { +if (!(master_ctrl->info.flags & +UVC_CTRL_FLAG_GET_CUR ) || +__uvc_ctrl_load_cur (dev, master_ctrl) <0 ) +continue ; + } + + val =uvc_mapping_get_s32 (master_map,UVC_GET_CUR , +uvc_ctrl_data (master_ctrl, +UVC_CTRL_DATA_CURRENT )); +if (val != mapping->master_manual) +return true; + } + +return false; +} +/* * Restore control values after resume, skipping controls that haven't been * changed. * +* The controls of each entity are restored in two passes: first the master +* controls (the auto modes), then the remaining controls. This way a master +* is set to its final state before its slaves are restored. +* +* Slave controls that are currently inactive, because their master is not +* in manual mode, are not restored: devices reject SET_CUR requests for +* them and some firmwares misbehave when written in auto mode. The manual +* value only takes effect once the master is switched back to manual mode. +* * TODO * - Don't restore modified controls that are back to their default value. -* - Handle restore order (Auto-Exposure Mode should be restored before -* Exposure Time). */ int uvc_ctrl_restore_values (struct uvc_device *dev) {struct uvc_control *err_ctrl;struct uvc_control *ctrl;struct uvc_entity *entity; -unsigned int i; +unsigned int i, pass;int ret;/* Walk the entities list and restore controls when possible. */ list_for_each_entry (entity, &dev->entities, list) { +for (pass =0 ; pass <2 ; ++pass) { +bool master_pass = pass ==0 ; +bool dirty = false; + +for (i =0 ; i < entity->ncontrols; ++i) { + ctrl = &entity->controls[i]; + +if (!ctrl->initialized || !ctrl->modified || + !(ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) +continue ; + +if (uvc_ctrl_is_master (ctrl) != master_pass) +continue ; + +if (!master_pass && +uvc_ctrl_is_inactive (dev, entity, ctrl)) { +uvc_dbg (dev,CONTROL , +"skipping restore of inactive control %pUl/%u/%u\n" , + ctrl->info.entity, + ctrl->info.index, + ctrl->info.selector); + ctrl->loaded =0 ; +continue ; + } + +uvc_dbg (dev,CONTROL , +"restoring control %pUl/%u/%u\n" , + ctrl->info.entity, ctrl->info.index, + ctrl->info.selector); + ctrl->dirty =1 ; + dirty = true; + } -for (i =0 ; i < entity->ncontrols; ++i) { - ctrl = &entity->controls[i]; - -if (!ctrl->initialized || !ctrl->modified || - (ctrl->info.flags &UVC_CTRL_FLAG_RESTORE ) ==0 ) +if (master_pass && !dirty)continue ; -uvc_dbg (dev,CONTROL ,"restoring control %pUl/%u/%u\n" , - ctrl->info.entity, ctrl->info.index, - ctrl->info.selector); - ctrl->dirty =1 ; - } - err_ctrl =NULL ; - ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , &err_ctrl); -if (ret <0 ) { -uvc_ctrl_restore_error (dev, entity, err_ctrl, ret); -return ret; + err_ctrl =NULL ; + ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , + &err_ctrl); +if (ret <0 ) { +uvc_ctrl_restore_error (dev, entity, err_ctrl, + ret); +return ret; + } } } --
Quoted 3 lines
2.53.0
--
Ricardo Ribalda
PATCH 3/3 media: uvcvideo: Do not abort resume when restoring controls fails
In reply to Chris Bainbridgeuvc_ctrl_restore_values() delegates each pass to uvc_ctrl_commit_entity() and returns at the first error. This has three problems on failure:
1. Within the failing entity, uvc_ctrl_commit_entity() aborts all subsequent dirty controls and overwrites UVC_CTRL_DATA_CURRENT with UVC_CTRL_DATA_BACKUP, which is never populated for a restore and holds stale data from before the control was last set.
2. If a master control fails to restore in the first pass, continuing to the second pass would evaluate uvc_ctrl_is_inactive() against a corrupted or unapplied master value and write manual slave controls while the hardware is still in auto mode.
3. Remaining entities are never restored, and __uvc_resume() returns early, skipping uvc_status_resume() for the status interrupt URB.
The USB core only logs the error returned by a reset_resume() handler:usb_resume_interface() prints "reset_resume error %d" with dev_err() and does not set intf->needs_binding on failure (that flag is only set for drivers that do not implement reset_resume()), and usb_resume_both() ignores the value returned by usb_resume_interface(). The interface is therefore neither unbound nor rebound when reset_resume() fails, so it is safe to report the error.
Restore each control directly with UVC_SET_CUR instead of calling uvc_ctrl_commit_entity(), preserving UVC_CTRL_DATA_CURRENT and continuing through all remaining controls and entities on error while recording the first error encountered. Track whether a restored control succeeded via ctrl->loaded so that uvc_ctrl_is_inactive() also skips slaves whose master failed to restore in the first pass, and reset ctrl->loaded afterwards for auto-update and write-only controls. Finally, always resume the status endpoint in __uvc_resume(), prioritizing any status endpoint resume error over already-logged best-effort restore errors.
--- drivers/media/usb/uvc/uvc_ctrl.c | 80 +++++++++++++++++------------- drivers/media/usb/uvc/uvc_driver.c | 22 ++++++-- 2 files changed, 64 insertions(+), 38 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +45 -35
@@ -3199,31 +3199,44 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,*/ /* -* Report a failure to restore the controls of an entity. @err_ctrl points to -* the control that failed, or is NULL if the failing control is not known. +* Report a failure to restore a control. */ static void uvc_ctrl_restore_error (struct uvc_device *dev, -const struct uvc_entity *entity, -struct uvc_control *err_ctrl,int ret) +struct uvc_control *ctrl,int ret) {const struct uvc_control_mapping *mapping;const char *name ="Unknown Control" ; -if (!err_ctrl) { -dev_warn_ratelimited (&dev->intf->dev, -"Failed to restore controls on unit %u: %d\n" , - entity->id, ret); -return ; - } - - mapping =list_first_entry_or_null (&err_ctrl->info.mappings, + mapping =list_first_entry_or_null (&ctrl->info.mappings, struct uvc_control_mapping, list);if (mapping) name =uvc_map_get_name (mapping);dev_warn_ratelimited (&dev->intf->dev,"Failed to restore control %s (selector %u on unit %u): %d\n" , - name, err_ctrl->info.selector, entity->id, ret); + name, ctrl->info.selector, ctrl->entity->id, ret); +} + +static int uvc_ctrl_restore_ctrl (struct uvc_device *dev, +struct uvc_control *ctrl) +{ +int ret; + +uvc_dbg (dev,CONTROL ,"restoring control %pUl/%u/%u\n" , + ctrl->info.entity, ctrl->info.index, ctrl->info.selector); + + ret =uvc_query_ctrl (dev,UVC_SET_CUR , ctrl->entity->id, dev->intfnum, + ctrl->info.selector, +uvc_ctrl_data (ctrl,UVC_CTRL_DATA_CURRENT ), + ctrl->info.size); +if (ret <0 ) { + ctrl->loaded =0 ; +uvc_ctrl_restore_error (dev, ctrl, ret); +return ret; + } + + ctrl->loaded =1 ; +return 0 ; }/*
@@ -3247,7 +3260,8 @@ static bool uvc_ctrl_is_master(const struct uvc_control *ctrl)/* * Return true if the control is currently inactive, i.e. it is a slave -* control whose master is not in manual mode. +* control whose master is not in manual mode, or whose master failed to +* restore in the first pass. * * The master value is read from the driver's cache when available, or queried * from the device via GET_CUR if the master was neither restored in the first
@@ -3275,9 +3289,11 @@ static bool uvc_ctrl_is_inactive(struct uvc_device *dev,if (!master_ctrl ||uvc_ctrl_mapping_is_compound (master_map))continue ; -if (!(master_ctrl->modified && - (master_ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) && - !master_ctrl->loaded) { +if (!master_ctrl->loaded) { +if (master_ctrl->modified && + (master_ctrl->info.flags &UVC_CTRL_FLAG_RESTORE )) +return true; +if (!(master_ctrl->info.flags &UVC_CTRL_FLAG_GET_CUR ) ||__uvc_ctrl_load_cur (dev, master_ctrl) <0 )
@@ -3312,17 +3328,16 @@ static bool uvc_ctrl_is_inactive(struct uvc_device *dev,*/ int uvc_ctrl_restore_values (struct uvc_device *dev) { -struct uvc_control *err_ctrl;struct uvc_control *ctrl;struct uvc_entity *entity;unsigned int i, pass; +int first_err =0 ;int ret;/* Walk the entities list and restore controls when possible. */ list_for_each_entry (entity, &dev->entities, list) {for (pass =0 ; pass <2 ; ++pass) {bool master_pass = pass ==0 ; -bool dirty = false;for (i =0 ; i < entity->ncontrols; ++i) { ctrl = &entity->controls[i];
@@ -3345,29 +3360,24 @@ int uvc_ctrl_restore_values(struct uvc_device *dev)continue ; } -uvc_dbg (dev,CONTROL , -"restoring control %pUl/%u/%u\n" , - ctrl->info.entity, ctrl->info.index, - ctrl->info.selector); - ctrl->dirty =1 ; - dirty = true; + ret =uvc_ctrl_restore_ctrl (dev, ctrl); +if (ret <0 && !first_err) + first_err = ret; } + } -if (master_pass && !dirty) +for (i =0 ; i < entity->ncontrols; ++i) { + ctrl = &entity->controls[i]; +if (!ctrl->initialized)continue ; - err_ctrl =NULL ; - ret =uvc_ctrl_commit_entity (dev,NULL , entity,0 , - &err_ctrl); -if (ret <0 ) { -uvc_ctrl_restore_error (dev, entity, err_ctrl, - ret); -return ret; - } +if ((ctrl->info.flags &UVC_CTRL_FLAG_AUTO_UPDATE ) || + !(ctrl->info.flags &UVC_CTRL_FLAG_GET_CUR )) + ctrl->loaded =0 ; } } -return 0 ; +return first_err; }/* --------------------------------------------------------------------------
drivers/media/usb/uvc/uvc_driver.c +19 -3
@@ -2414,13 +2414,29 @@ static int __uvc_resume(struct usb_interface *intf, int reset)if (intf->cur_altsetting->desc.bInterfaceSubClass ==UVC_SC_VIDEOCONTROL ) { +int status; +if (reset) { +/* +* Restoring the controls is best effort: the USB core +* only logs the error returned by a reset_resume() +* handler (see usb_resume_interface() and +* usb_resume_both()), it does not unbind the driver, +* so keep resuming the rest of the interface. +*/ ret =uvc_ctrl_restore_values (dev); -if (ret <0 ) -return ret; } -return uvc_status_resume (dev); +/* +* The status endpoint must be resumed even if restoring +* controls failed. Prioritize status resume errors over +* already-logged best-effort control restore errors. +*/ + status =uvc_status_resume (dev); +if (status <0 ) +return status; + +return ret; }list_for_each_entry (stream, &dev->streams, list) {
Signature · 1 line
2.53.0