-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathscan.c
154 lines (130 loc) · 4.82 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* This file is part of OpenCorsairLink.
* Copyright (C) 2017-2019 Sean Nelson <audiohacked@gmail.com>
* OpenCorsairLink is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
* OpenCorsairLink is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with OpenCorsairLink. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logic/scan.h"
#include "common.h"
#include "device.h"
#include "driver.h"
#include "print.h"
#include <errno.h>
#include <getopt.h>
#include <libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int scanlist_count = 0;
int
corsairlink_handle_close( struct libusb_device_handle* handle )
{
int rr;
rr = libusb_release_interface( handle, 0 );
if ( rr < 0 )
{
msg_err("Unable to release USB interface\n");
}
libusb_close( handle );
return rr;
}
int
corsairlink_close( libusb_context* context )
{
int ii;
for ( ii = 0; ii < scanlist_count; ii++ )
{
corsairlink_handle_close( scanlist[ii].handle );
}
libusb_exit( context );
return 0;
}
int
corsairlink_device_scanner( libusb_context* context, int* _scanlist_count )
{
int rr; // This could be safely ignored. It is just a success flag for
// libusb functions.
/* Start: scan code */
int ii; // Loops through USB devices.
int jj; // Loops through known CorsairLink Devices.
ssize_t cnt;
struct corsair_device_info* device;
libusb_device** devices;
uint8_t device_id = 0x00;
// uint16_t firmware_id = 0x0000;
cnt = libusb_get_device_list( context, &devices );
for ( ii = 0; ii < cnt; ii++ )
{
if ( scanlist_count >= 10 )
{
msg_debug( "Limited to 10 CorsairLink devices\n" );
break;
}
struct libusb_device_descriptor desc;
rr = libusb_get_device_descriptor( devices[ii], &desc );
msg_debug( "Checking USB device %d (%04x:%04x)...\n", ii, desc.idVendor, desc.idProduct );
for ( jj = 0; jj < corsairlink_device_list_count; jj++ )
{
device = &corsairlink_devices[jj];
if ( ( device->vendor_id == desc.idVendor )
&& ( device->product_id == desc.idProduct ) )
{
msg_debug( "Corsair product detected. Checking if device is %s... ", device->name );
rr = libusb_open( devices[ii], &scanlist[scanlist_count].handle );
if ( scanlist[scanlist_count].handle != NULL )
{ // Maybe try 'if (rr == 0)'
rr = libusb_set_auto_detach_kernel_driver( scanlist[scanlist_count].handle, 1 );
if ( rr != LIBUSB_SUCCESS )
{
msg_err("Platform does not support kernel detachment\n");
}
rr = libusb_claim_interface( scanlist[scanlist_count].handle, 0 );
if ( rr < 0 )
{
msg_err("Unable to claim USB device interface\n");
return rr;
}
/* get device_id if we have a proper device handle */
device->driver->device_id(
device, scanlist[scanlist_count].handle, &device_id );
/* check to see if the device_id is the right one */
if ( device->device_id == device_id )
{
/* if we have the right device id we can setup the rest
* of the device connections
*/
scanlist[scanlist_count].device = device;
msg_info(
"Dev=%d, CorsairLink Device Found: %s!\n", scanlist_count,
device->name );
scanlist_count++;
break;
}
else
{
msg_debug( "No (device_id 0x%02X)\n", device_id );
corsairlink_handle_close( scanlist[scanlist_count].handle );
continue;
}
}
else
{
msg_debug( "Could not open device %d:%d.", desc.idVendor, desc.idProduct );
}
}
}
}
msg_info( "\n" );
*_scanlist_count = scanlist_count;
/* End: scan code */
return 0;
}