lib: don't detach kernel driver by default

Since this feature also disconnects running instances
of librtlsdr that claimed the device, it is now disabled
by default and can only be enabled at compile time.

To enable it when building with cmake:
cmake . -DDETACH_KERNEL_DRIVER=ON

To enable it when building with automake:
./configure --enable-driver-detach

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Steve Markgraf 2013-06-04 14:47:16 +02:00
parent ce341fe53b
commit 3e17ef2b55
3 changed files with 26 additions and 0 deletions

View File

@ -111,6 +111,14 @@ else (INSTALL_UDEV_RULES)
message (STATUS "Udev rules not being installed, install them with -DINSTALL_UDEV_RULES=ON") message (STATUS "Udev rules not being installed, install them with -DINSTALL_UDEV_RULES=ON")
endif (INSTALL_UDEV_RULES) endif (INSTALL_UDEV_RULES)
option(DETACH_KERNEL_DRIVER "Detach kernel driver if loaded" OFF)
if (DETACH_KERNEL_DRIVER)
message (STATUS "Building with kernel driver detaching enabled")
add_definitions(-DDETACH_KERNEL_DRIVER=1)
else (DETACH_KERNEL_DRIVER)
message (STATUS "Building with kernel driver detaching disabled, use -DDETACH_KERNEL_DRIVER=ON to enable")
endif (DETACH_KERNEL_DRIVER)
######################################################################## ########################################################################
# Add subdirectories # Add subdirectories
######################################################################## ########################################################################

View File

@ -64,6 +64,12 @@ AC_TRY_COMPILE([],[],
AC_MSG_RESULT(no) AC_MSG_RESULT(no)
CFLAGS="$old_CFLAGS") CFLAGS="$old_CFLAGS")
AC_ARG_ENABLE(driver-detach,
[ --enable-driver-detach Enable detaching of kernel driver (disabled by default)],
[if test x$enableval = xyes; then
CFLAGS="$CFLAGS -DDETACH_KERNEL_DRIVER"
fi])
dnl Generate the output dnl Generate the output
AC_CONFIG_HEADER(config.h) AC_CONFIG_HEADER(config.h)

View File

@ -1351,12 +1351,22 @@ int rtlsdr_open(rtlsdr_dev_t **out_dev, uint32_t index)
if (libusb_kernel_driver_active(dev->devh, 0) == 1) { if (libusb_kernel_driver_active(dev->devh, 0) == 1) {
dev->driver_active = 1; dev->driver_active = 1;
#ifdef DETACH_KERNEL_DRIVER
if (!libusb_detach_kernel_driver(dev->devh, 0)) { if (!libusb_detach_kernel_driver(dev->devh, 0)) {
fprintf(stderr, "Detached kernel driver\n"); fprintf(stderr, "Detached kernel driver\n");
} else { } else {
fprintf(stderr, "Detaching kernel driver failed!"); fprintf(stderr, "Detaching kernel driver failed!");
goto err; goto err;
} }
#else
fprintf(stderr, "\nKernel driver is active, or device is "
"claimed by second instance of librtlsdr."
"\nIn the first case, please either detach"
" or blacklist the kernel module\n"
"(dvb_usb_rtl28xxu), or enable automatic"
" detaching at compile time.\n\n");
#endif
} }
r = libusb_claim_interface(dev->devh, 0); r = libusb_claim_interface(dev->devh, 0);
@ -1484,12 +1494,14 @@ int rtlsdr_close(rtlsdr_dev_t *dev)
libusb_release_interface(dev->devh, 0); libusb_release_interface(dev->devh, 0);
#ifdef DETACH_KERNEL_DRIVER
if (dev->driver_active) { if (dev->driver_active) {
if (!libusb_attach_kernel_driver(dev->devh, 0)) if (!libusb_attach_kernel_driver(dev->devh, 0))
fprintf(stderr, "Reattached kernel driver\n"); fprintf(stderr, "Reattached kernel driver\n");
else else
fprintf(stderr, "Reattaching kernel driver failed!\n"); fprintf(stderr, "Reattaching kernel driver failed!\n");
} }
#endif
libusb_close(dev->devh); libusb_close(dev->devh);