diff --git a/lib/libusb/libusb20.3 b/lib/libusb/libusb20.3 index 5404013ee999..7896e8f01094 100644 --- a/lib/libusb/libusb20.3 +++ b/lib/libusb/libusb20.3 @@ -143,6 +143,8 @@ USB access library (libusb -lusb) .Ft int .Fn libusb20_dev_reset "struct libusb20_device *pdev" .Ft int +.Fn libusb20_dev_check_connected "struct libusb20_device *pdev" +.Ft int .Fn libusb20_dev_set_power_mode "struct libusb20_device *pdev" "uint8_t power_mode" .Ft uint8_t .Fn libusb20_dev_get_power_mode "struct libusb20_device *pdev" @@ -677,6 +679,15 @@ the last set USB configuration. This function returns zero on success else a LIBUSB20_ERROR value is returned. . +. +.Pp +. +.Fn libusb20_dev_check_connected +will check if an opened USB device is still connected. +. +This function returns zero if the device is still connected else a LIBUSB20_ERROR value is returned. +. +. .Pp . .Fn libusb20_dev_set_power_mode diff --git a/lib/libusb/libusb20.c b/lib/libusb/libusb20.c index 1c75fc16712c..e181b49d24eb 100644 --- a/lib/libusb/libusb20.c +++ b/lib/libusb/libusb20.c @@ -67,6 +67,7 @@ dummy_callback(struct libusb20_transfer *xfer) #define dummy_set_config_index (void *)dummy_int #define dummy_set_alt_index (void *)dummy_int #define dummy_reset_device (void *)dummy_int +#define dummy_check_connected (void *)dummy_int #define dummy_set_power_mode (void *)dummy_int #define dummy_get_power_mode (void *)dummy_int #define dummy_kernel_driver_active (void *)dummy_int @@ -672,6 +673,15 @@ libusb20_dev_reset(struct libusb20_device *pdev) return (error); } +int +libusb20_dev_check_connected(struct libusb20_device *pdev) +{ + int error; + + error = pdev->methods->check_connected(pdev); + return (error); +} + int libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode) { diff --git a/lib/libusb/libusb20.h b/lib/libusb/libusb20.h index bb7d8a4ab3d2..6b253aa559a2 100644 --- a/lib/libusb/libusb20.h +++ b/lib/libusb/libusb20.h @@ -250,6 +250,7 @@ int libusb20_dev_request_sync(struct libusb20_device *pdev, struct LIBUSB20_CONT int libusb20_dev_req_string_sync(struct libusb20_device *pdev, uint8_t index, uint16_t langid, void *ptr, uint16_t len); int libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev, uint8_t index, void *ptr, uint16_t len); int libusb20_dev_reset(struct libusb20_device *pdev); +int libusb20_dev_check_connected(struct libusb20_device *pdev); int libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode); uint8_t libusb20_dev_get_power_mode(struct libusb20_device *pdev); int libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index); diff --git a/lib/libusb/libusb20_int.h b/lib/libusb/libusb20_int.h index 004b0cf381d7..b0e0e2dd7f65 100644 --- a/lib/libusb/libusb20_int.h +++ b/lib/libusb/libusb20_int.h @@ -101,6 +101,7 @@ typedef int (libusb20_set_power_mode_t)(struct libusb20_device *pdev, uint8_t po typedef int (libusb20_get_power_mode_t)(struct libusb20_device *pdev, uint8_t *power_mode); typedef int (libusb20_set_alt_index_t)(struct libusb20_device *pdev, uint8_t iface_index, uint8_t alt_index); typedef int (libusb20_set_config_index_t)(struct libusb20_device *pdev, uint8_t index); +typedef int (libusb20_check_connected_t)(struct libusb20_device *pdev); /* USB transfer specific */ typedef int (libusb20_tr_open_t)(struct libusb20_transfer *xfer, uint32_t MaxBufSize, uint32_t MaxFrameCount, uint8_t ep_no); @@ -117,6 +118,7 @@ typedef void (libusb20_tr_cancel_async_t)(struct libusb20_transfer *xfer); m(n, kernel_driver_active) \ m(n, process) \ m(n, reset_device) \ + m(n, check_connected) \ m(n, set_power_mode) \ m(n, get_power_mode) \ m(n, set_alt_index) \ diff --git a/lib/libusb/libusb20_ugen20.c b/lib/libusb/libusb20_ugen20.c index efcca63a2095..892ba0eb78aa 100644 --- a/lib/libusb/libusb20_ugen20.c +++ b/lib/libusb/libusb20_ugen20.c @@ -67,6 +67,7 @@ static libusb20_get_config_index_t ugen20_get_config_index; static libusb20_set_config_index_t ugen20_set_config_index; static libusb20_set_alt_index_t ugen20_set_alt_index; static libusb20_reset_device_t ugen20_reset_device; +static libusb20_check_connected_t ugen20_check_connected; static libusb20_set_power_mode_t ugen20_set_power_mode; static libusb20_get_power_mode_t ugen20_get_power_mode; static libusb20_kernel_driver_active_t ugen20_kernel_driver_active; @@ -552,6 +553,25 @@ ugen20_reset_device(struct libusb20_device *pdev) return (ugen20_tr_renew(pdev)); } +static int +ugen20_check_connected(struct libusb20_device *pdev) +{ + uint32_t plugtime; + int error = 0; + + if (ioctl(pdev->file_ctrl, USB_GET_PLUGTIME, &plugtime)) { + error = LIBUSB20_ERROR_NO_DEVICE; + goto done; + } + + if (pdev->session_data.plugtime != plugtime) { + error = LIBUSB20_ERROR_NO_DEVICE; + goto done; + } +done: + return (error); +} + static int ugen20_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode) {