Add option to disable detaching of the kernel driver

This commit is contained in:
dokutan 2020-02-08 22:54:45 +01:00
parent 2d727e0e29
commit fd4b4a9ade
5 changed files with 78 additions and 54 deletions

View file

@ -37,33 +37,35 @@ int mouse_m908::open_mouse(){
return 1;
}
//detach kernel driver on interface 0 if active
if( libusb_kernel_driver_active( _handle, 0 ) ){
res += libusb_detach_kernel_driver( _handle, 0 );
if( res == 0 ){
_detached_driver_0 = true;
} else{
return res;
if( _detach_kernel_driver ){
//detach kernel driver on interface 0 if active
if( libusb_kernel_driver_active( _handle, 0 ) ){
res += libusb_detach_kernel_driver( _handle, 0 );
if( res == 0 ){
_detached_driver_0 = true;
} else{
return res;
}
}
}
//detach kernel driver on interface 1 if active
if( libusb_kernel_driver_active( _handle, 1 ) ){
res += libusb_detach_kernel_driver( _handle, 1 );
if( res == 0 ){
_detached_driver_1 = true;
} else{
return res;
//detach kernel driver on interface 1 if active
if( libusb_kernel_driver_active( _handle, 1 ) ){
res += libusb_detach_kernel_driver( _handle, 1 );
if( res == 0 ){
_detached_driver_1 = true;
} else{
return res;
}
}
}
//detach kernel driver on interface 2 if active
if( libusb_kernel_driver_active( _handle, 2 ) ){
res += libusb_detach_kernel_driver( _handle, 2 );
if( res == 0 ){
_detached_driver_2 = true;
} else{
return res;
//detach kernel driver on interface 2 if active
if( libusb_kernel_driver_active( _handle, 2 ) ){
res += libusb_detach_kernel_driver( _handle, 2 );
if( res == 0 ){
_detached_driver_2 = true;
} else{
return res;
}
}
}
@ -127,36 +129,39 @@ int mouse_m908::open_mouse_bus_device( uint8_t bus, uint8_t device ){
//free device list, unreference devices
libusb_free_device_list( dev_list, 1 );
//detach kernel driver on interface 0 if active
if( libusb_kernel_driver_active( _handle, 0 ) ){
res += libusb_detach_kernel_driver( _handle, 0 );
if( res == 0 ){
_detached_driver_0 = true;
} else{
return res;
if( _detach_kernel_driver ){
//detach kernel driver on interface 0 if active
if( libusb_kernel_driver_active( _handle, 0 ) ){
res += libusb_detach_kernel_driver( _handle, 0 );
if( res == 0 ){
_detached_driver_0 = true;
} else{
return res;
}
}
//detach kernel driver on interface 1 if active
if( libusb_kernel_driver_active( _handle, 1 ) ){
res += libusb_detach_kernel_driver( _handle, 1 );
if( res == 0 ){
_detached_driver_1 = true;
} else{
return res;
}
}
//detach kernel driver on interface 2 if active
if( libusb_kernel_driver_active( _handle, 2 ) ){
res += libusb_detach_kernel_driver( _handle, 2 );
if( res == 0 ){
_detached_driver_2 = true;
} else{
return res;
}
}
}
//detach kernel driver on interface 1 if active
if( libusb_kernel_driver_active( _handle, 1 ) ){
res += libusb_detach_kernel_driver( _handle, 1 );
if( res == 0 ){
_detached_driver_1 = true;
} else{
return res;
}
}
//detach kernel driver on interface 2 if active
if( libusb_kernel_driver_active( _handle, 2 ) ){
res += libusb_detach_kernel_driver( _handle, 2 );
if( res == 0 ){
_detached_driver_2 = true;
} else{
return res;
}
}
//claim interface 0
res += libusb_claim_interface( _handle, 0 );
if( res != 0 ){

View file

@ -76,6 +76,7 @@ class mouse_m908{
int set_report_rate( m908_profile profile, m908_report_rate report_rate );
int set_macro( int macro_number, std::string file );
int set_macro_repeat( int macro_number, uint8_t repeat );
int set_detach_kernel_driver( bool detach_kernel_driver );
//getter functions
m908_profile get_profile();
@ -102,6 +103,9 @@ class mouse_m908{
private:
// whether to detach kernel driver
bool _detach_kernel_driver = true;
//usb device vars
uint16_t _mouse_vid;
uint16_t _mouse_pid;

View file

@ -32,5 +32,6 @@ void print_help(){
std::cout << "-n --number\n\tSelects macro slot for sending (1-15).\n";
std::cout << "-b --bus\n\tUSB bus id, requires -d.\n";
std::cout << "-d --device\n\tUSB device number, requires -b.\n";
std::cout << "-k --kernel-driver\n\tDo not attempt to detach kernel driver.\n";
//std::cout << "-r --repeat\n\tSets number of times the macro will be repeated (1-255).\n";
}

View file

@ -277,3 +277,9 @@ int mouse_m908::set_macro_repeat( int macro_number, uint8_t repeat ){
_macro_repeat[macro_number] = repeat;
return 0;
}
int mouse_m908::set_detach_kernel_driver( bool detach_kernel_driver ){
_detach_kernel_driver = detach_kernel_driver;
return 0;
}

View file

@ -45,12 +45,14 @@ int main( int argc, char **argv ){
//{"repeat", required_argument, 0, 'r'},
{"bus", required_argument, 0, 'b'},
{"device", required_argument, 0, 'd'},
{"kernel-driver", no_argument, 0, 'k'},
{0, 0, 0, 0}
};
bool flag_config = false, flag_profile = false;
bool flag_macro = false, flag_number = false;
bool flag_bus = false, flag_device = false;
bool flag_kernel_driver = false;
//bool flag_repeat;
std::string string_config, string_profile;
std::string string_macro, string_number;
@ -60,7 +62,7 @@ int main( int argc, char **argv ){
//parse command line options
int c, option_index = 0;
//while( (c = getopt_long( argc, argv, "hc:p:m:n:r:",
while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:",
while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:k",
long_options, &option_index ) ) != -1 ){
switch( c ){
@ -96,6 +98,9 @@ int main( int argc, char **argv ){
// flag_repeat = true;
// string_repeat = optarg;
// break;
case 'k':
flag_kernel_driver = true;
break;
case '?':
break;
default:
@ -103,6 +108,9 @@ int main( int argc, char **argv ){
}
}
// set wether to detach kernel driver
m.set_detach_kernel_driver( !flag_kernel_driver );
//load and write config
if( flag_config ){
try{