[3/3] libcamera: pipeline_handler: Allow calling stop() on a disconnected camera
diff mbox series

Message ID 20261123-kbingham-atomic-disconnects-v1-3-0b251be0bb3e@ideasonboard.com
State New
Headers show
Series
  • libcamera: atomic-disconnects
Related show

Commit Message

Kieran Bingham Nov. 23, 2026, 5:47 p.m. UTC
From: Stefan Klug <stefan.klug@ideasonboard.com>

When a camera gets disconnected, the disconnected signal is emitted.  If
this happens while the camera is running, the application must call
stop() on the camera to ensure all resources like queued requests are
released properly. We can't call stop() from within libcamera as that
would race with calls to queueRequest(). Modify the logic so that stop()
is allowed in in disconnected state and adjust the documentation
accordingly.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 src/libcamera/camera.cpp | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

Comments

Kieran Bingham Nov. 23, 2026, 5:57 p.m. UTC | #1
Quoting Kieran Bingham (2026-11-23 17:47:15)
> From: Stefan Klug <stefan.klug@ideasonboard.com>
> 
> When a camera gets disconnected, the disconnected signal is emitted.  If
> this happens while the camera is running, the application must call
> stop() on the camera to ensure all resources like queued requests are
> released properly. We can't call stop() from within libcamera as that
> would race with calls to queueRequest(). Modify the logic so that stop()
> is allowed in in disconnected state and adjust the documentation
> accordingly.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

And this one has fixed real issues when a running camera gets yanked
out.

I imagine this might impact/benefit the UVC pipeline for mainline mostly
at the moment, but this is quite extensively tested on a separate
hotpluggable platform with RKISP1.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


> ---
>  src/libcamera/camera.cpp | 15 ++++-----------
>  1 file changed, 4 insertions(+), 11 deletions(-)
> 
> diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp
> index b6e2d3e51a46..d7b24961e40a 100644
> --- a/src/libcamera/camera.cpp
> +++ b/src/libcamera/camera.cpp
> @@ -735,14 +735,6 @@ int Camera::Private::isAccessAllowed(State low, State high,
>  
>  void Camera::Private::disconnect()
>  {
> -       /*
> -        * If the camera was running when the hardware was removed force the
> -        * state to Configured state to allow applications to free resources
> -        * and call release() before deleting the camera.
> -        */
> -       if (state_.load(std::memory_order_acquire) == Private::CameraRunning)
> -               state_.store(Private::CameraConfigured, std::memory_order_release);
> -
>         disconnected_.store(true, std::memory_order_relaxed);
>  }
>  
> @@ -927,7 +919,9 @@ const std::string &Camera::id() const
>   * physical device disconnection. The media device is passed as a parameter.
>   *
>   * As soon as this signal is emitted the camera instance will refuse all new
> - * application API calls by returning errors immediately.
> + * application API calls by returning errors immediately. The only exception is
> + * the stop() function which must be called on the camera if it was in running
> + * state before, to ensure all resources are released.
>   */
>  
>  Camera::Camera(std::unique_ptr<Private> d, const std::string &id,
> @@ -1443,7 +1437,6 @@ int Camera::start(const ControlList *controls)
>   * running, it is a no-op.
>   *
>   * \return 0 on success or a negative error code otherwise
> - * \retval -ENODEV The camera has been disconnected from the system
>   * \retval -EACCES The camera is not running so can't be stopped
>   */
>  int Camera::stop()
> @@ -1457,7 +1450,7 @@ int Camera::stop()
>         if (d->state() != Private::CameraRunning)
>                 return 0;
>  
> -       int ret = d->isAccessAllowed(Private::CameraRunning);
> +       int ret = d->isAccessAllowed(Private::CameraRunning, true);
>         if (ret < 0)
>                 return ret;
>  
> 
> -- 
> 2.52.0
>

Patch
diff mbox series

diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp
index b6e2d3e51a46..d7b24961e40a 100644
--- a/src/libcamera/camera.cpp
+++ b/src/libcamera/camera.cpp
@@ -735,14 +735,6 @@  int Camera::Private::isAccessAllowed(State low, State high,
 
 void Camera::Private::disconnect()
 {
-	/*
-	 * If the camera was running when the hardware was removed force the
-	 * state to Configured state to allow applications to free resources
-	 * and call release() before deleting the camera.
-	 */
-	if (state_.load(std::memory_order_acquire) == Private::CameraRunning)
-		state_.store(Private::CameraConfigured, std::memory_order_release);
-
 	disconnected_.store(true, std::memory_order_relaxed);
 }
 
@@ -927,7 +919,9 @@  const std::string &Camera::id() const
  * physical device disconnection. The media device is passed as a parameter.
  *
  * As soon as this signal is emitted the camera instance will refuse all new
- * application API calls by returning errors immediately.
+ * application API calls by returning errors immediately. The only exception is
+ * the stop() function which must be called on the camera if it was in running
+ * state before, to ensure all resources are released.
  */
 
 Camera::Camera(std::unique_ptr<Private> d, const std::string &id,
@@ -1443,7 +1437,6 @@  int Camera::start(const ControlList *controls)
  * running, it is a no-op.
  *
  * \return 0 on success or a negative error code otherwise
- * \retval -ENODEV The camera has been disconnected from the system
  * \retval -EACCES The camera is not running so can't be stopped
  */
 int Camera::stop()
@@ -1457,7 +1450,7 @@  int Camera::stop()
 	if (d->state() != Private::CameraRunning)
 		return 0;
 
-	int ret = d->isAccessAllowed(Private::CameraRunning);
+	int ret = d->isAccessAllowed(Private::CameraRunning, true);
 	if (ret < 0)
 		return ret;