-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusb_pc_link.c
133 lines (112 loc) · 2.54 KB
/
usb_pc_link.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
/**
* Complete the connection with Philips
* MCM-530 USB PC Link
*
* License: GPL
*
* You need to have libusb-dev installed.
* apt-get install libusb-dev
*
* Compile:
* gcc -Wall usb_pc_link.c -o usb_pc_link -lusb
*
* Run as normal user:
* ./usb_pc_link
*
*/
#include <usb.h>
#include <stdio.h>
#define VENDOR_ID 0x0471
#define PRODUCT_ID 0x0111
// get a handle to the USB device and configure it
usb_dev_handle*
get_handle(struct usb_device *usbDevice)
{
usb_dev_handle *deviceHandle = NULL;
if( usbDevice == NULL ) {
return NULL;
}
deviceHandle = usb_open( usbDevice );
if (deviceHandle == NULL)
{
fprintf(stderr, "Unable to open USB device %s\n", usb_strerror());
usb_close(deviceHandle);
exit(-1);
}
return deviceHandle;
}
// find the right USB device
struct usb_device*
findUSBDevice(void) {
struct usb_bus *bus;
struct usb_device *usbDevice;
struct usb_device *mcmUSBDevice = NULL;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next) {
for (usbDevice = bus->devices; usbDevice;
usbDevice = usbDevice->next)
{
if (usbDevice->descriptor.idVendor == VENDOR_ID &&
usbDevice->descriptor.idProduct == PRODUCT_ID)
{
mcmUSBDevice = usbDevice;
break;
}
}
}
if (mcmUSBDevice == NULL) {
fprintf(stderr, "Device %04x:%04x not found\n", VENDOR_ID, PRODUCT_ID);
exit(-1);
}
return mcmUSBDevice;
}
/**
* Connection.
*
* send
* 40 04 00 00 a4 ef 01 00 01
* | | | | | |
* | | value index | data
* | request length
* requesttype
*
* send
* 40 04 00 00 a4 f0 01 00 ff
* | | | | | |
* | | value index | data
* | request length
* requesttype
*/
int
connection(usb_dev_handle *deviceHandle) {
int requesttype = 0x40;
int request = 0x04;
int value = 0x00;
int index[2] = { 0xa4ef, 0xa4f0 };
char buffer[2] = { 1, -1 };
int size = 1;
int timeout = 1000;
int ret = 0;
ret += usb_control_msg(
deviceHandle, requesttype, request, value,
index[0], buffer, size, timeout);
ret += usb_control_msg(
deviceHandle, requesttype, request, value,
index[1], buffer+1, size, timeout);
if (ret < 2) {
fprintf(stderr, "An error occurs in the requests...\n");
return -1;
}
return 0;
}
int
main(void) {
struct usb_device *mcmUSBDevice = findUSBDevice();
usb_dev_handle *deviceHandle = NULL;
deviceHandle = get_handle(mcmUSBDevice);
if(connection(deviceHandle) < 0)
return -1;
return 0;
}