-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwwsr_usb.c
290 lines (240 loc) · 6.3 KB
/
wwsr_usb.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Functions that are related to USB
*/
#include <usb.h>
#include <string.h>
#include <stdio.h>
#include "logger.h"
#include "config.h"
#include "wwsr_usb.h"
// If you have a different usb device, change the codes here
#define VENDOR_ID 0x1941
#define PRODUCT_ID 0x8021
static usb_dev_handle * device_handle;
int wwsr_usb_open (void)
{
// Will return 1 if the usb was successfully opened
int state = 0;
struct usb_bus * bus;
if (device_handle != NULL)
{
logger (LOG_ERROR, __func__, "Device already opened, please close first");
return -1;
}
device_handle = NULL;
logger (LOG_USB, __func__, "Initialise usb");
usb_init();
usb_set_debug (0);
usb_find_busses();
usb_find_devices();
logger (
LOG_USB,
__func__,
"Scan for device %04X:%04X",
VENDOR_ID,
PRODUCT_ID);
for (bus = usb_get_busses(); bus && device_handle == NULL; bus = bus->next)
{
struct usb_device * device;
for (device = bus->devices; device && device_handle == NULL;
device = device->next)
{
if (
device->descriptor.idVendor == VENDOR_ID &&
device->descriptor.idProduct == PRODUCT_ID)
{
logger (
LOG_USB,
__func__,
"Found device %04X:%04X",
VENDOR_ID,
PRODUCT_ID);
device_handle = usb_open (device);
}
}
}
if (state == 0 && device_handle)
{
char buf[1000];
switch (usb_get_driver_np (device_handle, 0, buf, sizeof (buf)))
{
case 0:
logger (
LOG_USB,
__func__,
"Interface 0 already claimed by driver \"%s\", attempting to "
"detach it",
buf);
state = usb_detach_kernel_driver_np (device_handle, 0);
}
if (state == 0)
{
// Claimed the device from the system, set up the file handler
logger (LOG_USB, __func__, "Claimed device from system");
state = usb_claim_interface (device_handle, 0);
}
if (state == 0)
{
logger (LOG_USB, __func__, "Set alt interface");
state = usb_set_altinterface (device_handle, 0);
}
}
else
{
logger (
LOG_ERROR,
__func__,
"Device %04X:%04X not found",
VENDOR_ID,
PRODUCT_ID);
state = 1;
}
if (state == 0)
{
logger (
LOG_USB,
__func__,
"Device %04X:%04X opened, code:%d",
VENDOR_ID,
PRODUCT_ID,
state);
}
else
{
logger (
LOG_ERROR,
__func__,
"Device %04X:%04X: could not open, code:%d",
VENDOR_ID,
PRODUCT_ID,
state);
}
return state;
}
int wwsr_usb_close (void)
{
int state;
if (device_handle)
{
state = usb_release_interface (device_handle, 0);
if (state != 0)
{
logger (
LOG_USB,
__func__,
"Could not release interface, code:%d",
state);
}
state = usb_close (device_handle);
if (state != 0)
{
logger (LOG_ERROR, __func__, "Error closing interface, code:%d", state);
}
}
logger (LOG_USB, __func__, "Closing interface, state is:%d", state);
return state;
}
/* Reads one single 16 byte value */
int wwsr_usb_read_value (uint16_t address, uint16_t * data)
{
char cmd[9];
int state = 0;
/* Think that usb transfers are done in 32 bit, thefore
* our buffer must have to be that size as well */
char tmp[32];
int usb_tx_buf_size = sizeof (tmp);
memset (data, 0, sizeof (data));
// Send debug message
logger (
LOG_USB,
__func__,
"Send read command: Addr=0x%04X Size=%u",
address,
sizeof (data));
// Format the command
sprintf (
cmd,
"\xA1%c%c%c\xA1%c%c%c",
address >> 8,
address,
usb_tx_buf_size,
address >> 8,
address,
usb_tx_buf_size);
// Send the command to the usb device
state = usb_control_msg (
device_handle,
USB_TYPE_CLASS + USB_RECIP_INTERFACE,
0x0000009,
0x0000200,
0,
cmd,
sizeof (cmd) - 1,
1000);
// Send debug message if enabled
logger (LOG_DEBUG, __func__, "Sent %d of %d bytes", state, sizeof (cmd) - 1);
// initiate a read request to the device
state =
usb_interrupt_read (device_handle, 0x00000081, tmp, usb_tx_buf_size, 1000);
// Send debug message if enable
logger (LOG_DEBUG, __func__, "Read %d of %d bytes", state, usb_tx_buf_size);
memcpy (data, tmp, sizeof (tmp));
}
int wwsr_usb_read (uint16_t address, uint8_t * data, uint16_t size)
{
// Initiliaze local values
uint16_t i, c;
// Set the state to 0
int state = 0;
// setup a buffer
uint8_t s, tmp[0x20];
// Set the block of data to 0
memset (data, 0, size);
// Send debug message if enabled
logger (LOG_DEBUG, __func__, "Reading %d bytes from 0x%04X", size, address);
// set i to 0
i = 0;
// put buffer size of tmp into c
c = sizeof (tmp);
// if size - 0 is less than c, then size - 0 should equal c
s = size - i < c ? size - i : c;
for (; i < size; i += s, s = size - i < c ? size - i : c)
{
uint16_t a;
char cmd[9];
// Increment the address to read
a = address + i;
// Send debug message
logger (
LOG_DEBUG,
__func__,
"Send read command: Addr=0x%04X Size=%u",
a,
s);
// Format the command
sprintf (cmd, "\xA1%c%c%c\xA1%c%c%c", a >> 8, a, c, a >> 8, a, c);
// Send the command to the usb device
state = usb_control_msg (
device_handle,
USB_TYPE_CLASS + USB_RECIP_INTERFACE,
0x0000009,
0x0000200,
0,
cmd,
sizeof (cmd) - 1,
1000);
// Send debug message if enabled
logger (
LOG_DEBUG,
__func__,
"Sent %d of %d bytes",
state,
sizeof (cmd) - 1);
// initiate a read request to the device
state = usb_interrupt_read (device_handle, 0x00000081, tmp, c, 1000);
// Send debug message if enable
logger (LOG_DEBUG, __func__, "Read %d of %d bytes", state, c);
// Copy the data to tmp
memcpy (data + i, tmp, s);
}
return 0;
}