/
libusb.factor
422 lines (360 loc) · 17.2 KB
/
libusb.factor
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.libraries
alien.syntax classes.struct combinators endian io.binary
kernel locals math sequences specialized-arrays
system unix.time unix.types ;
FROM: alien.c-types => short ;
IN: libusb
<< "libusb" {
{ [ os windows? ] [ "libusb-1.0.dll" ] }
{ [ os macosx? ] [ "libusb-1.0.dylib" ] }
{ [ os unix? ] [ "libusb-1.0.so" ] }
} cond "cdecl" add-library >>
LIBRARY: libusb
: libusb_cpu_to_le16 ( x -- y )
2 >native-endian le> ; inline
ALIAS: libusb_le16_to_cpu libusb_cpu_to_le16
CONSTANT: LIBUSB_CLASS_PER_INTERFACE 0
CONSTANT: LIBUSB_CLASS_AUDIO 1
CONSTANT: LIBUSB_CLASS_COMM 2
CONSTANT: LIBUSB_CLASS_HID 3
CONSTANT: LIBUSB_CLASS_PRINTER 7
CONSTANT: LIBUSB_CLASS_PTP 6
CONSTANT: LIBUSB_CLASS_MASS_STORAGE 8
CONSTANT: LIBUSB_CLASS_HUB 9
CONSTANT: LIBUSB_CLASS_DATA 10
CONSTANT: LIBUSB_CLASS_VENDOR_SPEC HEX: ff
TYPEDEF: int libusb_class_code
CONSTANT: LIBUSB_DT_DEVICE HEX: 01
CONSTANT: LIBUSB_DT_CONFIG HEX: 02
CONSTANT: LIBUSB_DT_STRING HEX: 03
CONSTANT: LIBUSB_DT_INTERFACE HEX: 04
CONSTANT: LIBUSB_DT_ENDPOINT HEX: 05
CONSTANT: LIBUSB_DT_HID HEX: 21
CONSTANT: LIBUSB_DT_REPORT HEX: 22
CONSTANT: LIBUSB_DT_PHYSICAL HEX: 23
CONSTANT: LIBUSB_DT_HUB HEX: 29
TYPEDEF: int libusb_descriptor_type
CONSTANT: LIBUSB_DT_DEVICE_SIZE 18
CONSTANT: LIBUSB_DT_CONFIG_SIZE 9
CONSTANT: LIBUSB_DT_INTERFACE_SIZE 9
CONSTANT: LIBUSB_DT_ENDPOINT_SIZE 7
CONSTANT: LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9
CONSTANT: LIBUSB_DT_HUB_NONVAR_SIZE 7
CONSTANT: LIBUSB_ENDPOINT_ADDRESS_MASK HEX: 0f
CONSTANT: LIBUSB_ENDPOINT_DIR_MASK HEX: 80
CONSTANT: LIBUSB_ENDPOINT_IN HEX: 80
CONSTANT: LIBUSB_ENDPOINT_OUT HEX: 00
TYPEDEF: int libusb_endpoint_direction
CONSTANT: LIBUSB_TRANSFER_TYPE_MASK HEX: 03
CONSTANT: LIBUSB_TRANSFER_TYPE_CONTROL 0
CONSTANT: LIBUSB_TRANSFER_TYPE_ISOCHRONOUS 1
CONSTANT: LIBUSB_TRANSFER_TYPE_BULK 2
CONSTANT: LIBUSB_TRANSFER_TYPE_INTERRUPT 3
TYPEDEF: int libusb_transfer_type
CONSTANT: LIBUSB_REQUEST_GET_STATUS HEX: 00
CONSTANT: LIBUSB_REQUEST_CLEAR_FEATURE HEX: 01
CONSTANT: LIBUSB_REQUEST_SET_FEATURE HEX: 03
CONSTANT: LIBUSB_REQUEST_SET_ADDRESS HEX: 05
CONSTANT: LIBUSB_REQUEST_GET_DESCRIPTOR HEX: 06
CONSTANT: LIBUSB_REQUEST_SET_DESCRIPTOR HEX: 07
CONSTANT: LIBUSB_REQUEST_GET_CONFIGURATION HEX: 08
CONSTANT: LIBUSB_REQUEST_SET_CONFIGURATION HEX: 09
CONSTANT: LIBUSB_REQUEST_GET_INTERFACE HEX: 0A
CONSTANT: LIBUSB_REQUEST_SET_INTERFACE HEX: 0B
CONSTANT: LIBUSB_REQUEST_SYNCH_FRAME HEX: 0C
TYPEDEF: int libusb_standard_request
CONSTANT: LIBUSB_REQUEST_TYPE_STANDARD HEX: 00
CONSTANT: LIBUSB_REQUEST_TYPE_CLASS HEX: 20
CONSTANT: LIBUSB_REQUEST_TYPE_VENDOR HEX: 40
CONSTANT: LIBUSB_REQUEST_TYPE_RESERVED HEX: 60
CONSTANT: LIBUSB_RECIPIENT_DEVICE HEX: 00
CONSTANT: LIBUSB_RECIPIENT_INTERFACE HEX: 01
CONSTANT: LIBUSB_RECIPIENT_ENDPOINT HEX: 02
CONSTANT: LIBUSB_RECIPIENT_OTHER HEX: 03
TYPEDEF: int libusb_request_recipient
CONSTANT: LIBUSB_ISO_SYNC_TYPE_MASK HEX: 0C
CONSTANT: LIBUSB_ISO_SYNC_TYPE_NONE 0
CONSTANT: LIBUSB_ISO_SYNC_TYPE_ASYNC 1
CONSTANT: LIBUSB_ISO_SYNC_TYPE_ADAPTIVE 2
CONSTANT: LIBUSB_ISO_SYNC_TYPE_SYNC 3
TYPEDEF: int libusb_iso_sync_type
CONSTANT: LIBUSB_ISO_USAGE_TYPE_MASK HEX: 30
CONSTANT: LIBUSB_ISO_USAGE_TYPE_DATA 0
CONSTANT: LIBUSB_ISO_USAGE_TYPE_FEEDBACK 1
CONSTANT: LIBUSB_ISO_USAGE_TYPE_IMPLICIT 2
TYPEDEF: int libusb_iso_usage_type
STRUCT: libusb_device_descriptor
{ bLength uint8_t }
{ bDescriptorType uint8_t }
{ bcdUSB uint16_t }
{ bDeviceClass uint8_t }
{ bDeviceSubClass uint8_t }
{ bDeviceProtocol uint8_t }
{ bMaxPacketSize0 uint8_t }
{ idVendor uint16_t }
{ idProduct uint16_t }
{ bcdDevice uint16_t }
{ iManufacturer uint8_t }
{ iProduct uint8_t }
{ iSerialNumber uint8_t }
{ bNumConfigurations uint8_t } ;
STRUCT: libusb_endpoint_descriptor
{ bLength uint8_t }
{ bDescriptorType uint8_t }
{ bEndpointAddress uint8_t }
{ bmAttributes uint8_t }
{ wMaxPacketSize uint16_t }
{ bInterval uint8_t }
{ bRefresh uint8_t }
{ bSynchAddress uint8_t }
{ extra uchar* }
{ extra_length int } ;
STRUCT: libusb_interface_descriptor
{ bLength uint8_t }
{ bDescriptorType uint8_t }
{ bInterfaceNumber uint8_t }
{ bAlternateSetting uint8_t }
{ bNumEndpoints uint8_t }
{ bInterfaceClass uint8_t }
{ bInterfaceSubClass uint8_t }
{ bInterfaceProtocol uint8_t }
{ iInterface uint8_t }
{ endpoint libusb_endpoint_descriptor* }
{ extra uchar* }
{ extra_length int } ;
STRUCT: libusb_interface
{ altsetting libusb_interface_descriptor* }
{ num_altsetting int } ;
STRUCT: libusb_config_descriptor
{ bLength uint8_t }
{ bDescriptorType uint8_t }
{ wTotalLength uint16_t }
{ bNumInterfaces uint8_t }
{ bConfigurationValue uint8_t }
{ iConfiguration uint8_t }
{ bmAttributes uint8_t }
{ MaxPower uint8_t }
{ interface libusb_interface* }
{ extra uchar* }
{ extra_length int } ;
STRUCT: libusb_control_setup
{ bmRequestType uint8_t }
{ bRequest uint8_t }
{ wValue uint16_t }
{ wIndex uint16_t }
{ wLength uint16_t } ;
: LIBUSB_CONTROL_SETUP_SIZE ( -- x ) libusb_control_setup heap-size ; inline
C-TYPE: libusb_context
C-TYPE: libusb_device
C-TYPE: libusb_device_handle
CONSTANT: LIBUSB_SUCCESS 0
CONSTANT: LIBUSB_ERROR_IO -1
CONSTANT: LIBUSB_ERROR_INVALID_PARAM -2
CONSTANT: LIBUSB_ERROR_ACCESS -3
CONSTANT: LIBUSB_ERROR_NO_DEVICE -4
CONSTANT: LIBUSB_ERROR_NOT_FOUND -5
CONSTANT: LIBUSB_ERROR_BUSY -6
CONSTANT: LIBUSB_ERROR_TIMEOUT -7
CONSTANT: LIBUSB_ERROR_OVERFLOW -8
CONSTANT: LIBUSB_ERROR_PIPE -9
CONSTANT: LIBUSB_ERROR_INTERRUPTED -10
CONSTANT: LIBUSB_ERROR_NO_MEM -11
CONSTANT: LIBUSB_ERROR_NOT_SUPPORTED -12
CONSTANT: LIBUSB_ERROR_OTHER -99
TYPEDEF: int libusb_error
C-ENUM:
LIBUSB_TRANSFER_COMPLETED
LIBUSB_TRANSFER_ERROR
LIBUSB_TRANSFER_TIMED_OUT
LIBUSB_TRANSFER_CANCELLED
LIBUSB_TRANSFER_STALL
LIBUSB_TRANSFER_NO_DEVICE
LIBUSB_TRANSFER_OVERFLOW ;
TYPEDEF: int libusb_transfer_status
CONSTANT: LIBUSB_TRANSFER_SHORT_NOT_OK 1
CONSTANT: LIBUSB_TRANSFER_FREE_BUFFER 2
CONSTANT: LIBUSB_TRANSFER_FREE_TRANSFER 4
TYPEDEF: int libusb_transfer_flags
STRUCT: libusb_iso_packet_descriptor
{ length uint }
{ actual_length uint }
{ status libusb_transfer_status } ;
SPECIALIZED-ARRAY: libusb_iso_packet_descriptor
C-TYPE: libusb_transfer
CALLBACK: void libusb_transfer_cb_fn ( libusb_transfer* transfer ) ;
STRUCT: libusb_transfer
{ dev_handle libusb_device_handle* }
{ flags uint8_t }
{ endpoint uchar }
{ type uchar }
{ timeout uint }
{ status libusb_transfer_status }
{ length int }
{ actual_length int }
{ callback libusb_transfer_cb_fn }
{ user_data void* }
{ buffer uchar* }
{ num_iso_packets int }
{ iso_packet_desc libusb_iso_packet_descriptor[0] } ;
FUNCTION: int libusb_init ( libusb_context** ctx ) ;
FUNCTION: void libusb_exit ( libusb_context* ctx ) ;
FUNCTION: void libusb_set_debug ( libusb_context* ctx, int level ) ;
FUNCTION: ssize_t libusb_get_device_list ( libusb_context* ctx, libusb_device*** list ) ;
FUNCTION: void libusb_free_device_list ( libusb_device** list, int unref_devices ) ;
FUNCTION: libusb_device* libusb_ref_device ( libusb_device* dev ) ;
FUNCTION: void libusb_unref_device ( libusb_device* dev ) ;
FUNCTION: int libusb_get_configuration ( libusb_device_handle* dev, int* config ) ;
FUNCTION: int libusb_get_device_descriptor ( libusb_device* dev, libusb_device_descriptor* desc ) ;
FUNCTION: int libusb_get_active_config_descriptor ( libusb_device* dev, libusb_config_descriptor** config ) ;
FUNCTION: int libusb_get_config_descriptor ( libusb_device* dev, uint8_t config_index, libusb_config_descriptor** config ) ;
FUNCTION: int libusb_get_config_descriptor_by_value ( libusb_device* dev, uint8_t bConfigurationValue, libusb_config_descriptor** config ) ;
FUNCTION: void libusb_free_config_descriptor ( libusb_config_descriptor* config ) ;
FUNCTION: uint8_t libusb_get_bus_number ( libusb_device* dev ) ;
FUNCTION: uint8_t libusb_get_device_address ( libusb_device* dev ) ;
FUNCTION: int libusb_get_max_packet_size ( libusb_device* dev, uchar endpoint ) ;
FUNCTION: int libusb_open ( libusb_device* dev, libusb_device_handle** handle ) ;
FUNCTION: void libusb_close ( libusb_device_handle* dev_handle ) ;
FUNCTION: libusb_device* libusb_get_device ( libusb_device_handle* dev_handle ) ;
FUNCTION: int libusb_set_configuration ( libusb_device_handle* dev, int configuration ) ;
FUNCTION: int libusb_claim_interface ( libusb_device_handle* dev, int iface ) ;
FUNCTION: int libusb_release_interface ( libusb_device_handle* dev, int iface ) ;
FUNCTION: libusb_device_handle* libusb_open_device_with_vid_pid ( libusb_context* ctx, uint16_t vendor_id, uint16_t product_id ) ;
FUNCTION: int libusb_set_interface_alt_setting ( libusb_device_handle* dev, int interface_number, int alternate_setting ) ;
FUNCTION: int libusb_clear_halt ( libusb_device_handle* dev, uchar endpoint ) ;
FUNCTION: int libusb_reset_device ( libusb_device_handle* dev ) ;
FUNCTION: int libusb_kernel_driver_active ( libusb_device_handle* dev, int interface ) ;
FUNCTION: int libusb_detach_kernel_driver ( libusb_device_handle* dev, int interface ) ;
FUNCTION: int libusb_attach_kernel_driver ( libusb_device_handle* dev, int interface ) ;
: libusb_control_transfer_get_data ( transfer -- data )
buffer>> LIBUSB_CONTROL_SETUP_SIZE swap <displaced-alien> ; inline
: libusb_control_transfer_get_setup ( transfer -- setup )
buffer>> libusb_control_setup memory>struct ; inline
:: libusb_fill_control_setup ( buffer bmRequestType bRequest wValue wIndex wLength -- )
buffer libusb_control_setup memory>struct
bmRequestType >>bmRequestType
bRequest >>bRequest
wValue libusb_cpu_to_le16 >>wValue
wIndex libusb_cpu_to_le16 >>wIndex
wLength libusb_cpu_to_le16 >>wLength drop ; inline
FUNCTION: libusb_transfer* libusb_alloc_transfer ( int iso_packets ) ;
FUNCTION: int libusb_submit_transfer ( libusb_transfer* transfer ) ;
FUNCTION: int libusb_cancel_transfer ( libusb_transfer* transfer ) ;
FUNCTION: void libusb_free_transfer ( libusb_transfer* transfer ) ;
:: libusb_fill_control_transfer ( transfer dev_handle buffer callback user_data timeout -- )
transfer
dev_handle >>dev_handle
0 >>endpoint
LIBUSB_TRANSFER_TYPE_CONTROL >>type
timeout >>timeout
buffer >>buffer
user_data >>user_data
callback >>callback
buffer [
libusb_control_setup memory>struct wLength>> LIBUSB_CONTROL_SETUP_SIZE +
] [ 0 ] if* >>length drop ; inline
:: libusb_fill_bulk_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
transfer
dev_handle >>dev_handle
endpoint >>endpoint
LIBUSB_TRANSFER_TYPE_BULK >>type
timeout >>timeout
buffer >>buffer
length >>length
user_data >>user_data
callback >>callback
drop ; inline
:: libusb_fill_interrupt_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
transfer
dev_handle >>dev_handle
endpoint >>endpoint
LIBUSB_TRANSFER_TYPE_INTERRUPT >>type
timeout >>timeout
buffer >>buffer
length >>length
user_data >>user_data
callback >>callback
drop ; inline
:: libusb_fill_iso_transfer ( transfer dev_handle endpoint buffer length num_iso_packets callback user_data timeout -- )
transfer
dev_handle >>dev_handle
endpoint >>endpoint
LIBUSB_TRANSFER_TYPE_ISOCHRONOUS >>type
timeout >>timeout
buffer >>buffer
length >>length
num_iso_packets >>num_iso_packets
user_data >>user_data
callback >>callback
drop ; inline
: libusb_set_iso_packet_lengths ( transfer length -- )
[ [ iso_packet_desc>> >c-ptr ]
[ num_iso_packets>> ] bi
<direct-libusb_iso_packet_descriptor-array>
] dip [ >>length drop ] curry each ; inline
:: libusb_get_iso_packet_buffer ( transfer packet -- data )
packet transfer num_iso_packets>> >=
[ f ]
[
transfer
[ iso_packet_desc>> >c-ptr ]
[ num_iso_packets>> ] bi
<direct-libusb_iso_packet_descriptor-array> 0
[ length>> + ] reduce
transfer buffer>> <displaced-alien>
] if ;
:: libusb_get_iso_packet_buffer_simple ( transfer packet -- data )
packet transfer num_iso_packets>> >=
[ f ]
[
0 transfer
[ iso_packet_desc>> >c-ptr ]
[ num_iso_packets>> ] bi
<direct-libusb_iso_packet_descriptor-array> nth
length>> packet *
transfer buffer>> <displaced-alien>
] if ;
FUNCTION: int libusb_control_transfer ( libusb_device_handle* dev_handle,
uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
uchar* data, uint16_t length, uint timeout ) ;
FUNCTION: int libusb_bulk_transfer ( libusb_device_handle* dev_handle,
uchar endpoint, uchar* data, int length,
int* actual_length, uint timeout ) ;
FUNCTION: int libusb_interrupt_transfer ( libusb_device_handle* dev_handle,
uchar endpoint, uchar* data, int length,
int* actual_length, int timeout ) ;
:: libusb_get_descriptor ( dev desc_type desc_index data length -- int )
dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
desc_type 8 shift desc_index bitor 0 data
length 1000 libusb_control_transfer ; inline
:: libusb_get_string_descriptor ( dev desc_index langid data length -- int )
dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
LIBUSB_DT_STRING 8 shift desc_index bitor
langid data length 1000 libusb_control_transfer ; inline
FUNCTION: int libusb_get_string_descriptor_ascii ( libusb_device_handle* dev,
uint8_t index,
uchar* data,
int length ) ;
FUNCTION: int libusb_try_lock_events ( libusb_context* ctx ) ;
FUNCTION: void libusb_lock_events ( libusb_context* ctx ) ;
FUNCTION: void libusb_unlock_events ( libusb_context* ctx ) ;
FUNCTION: int libusb_event_handling_ok ( libusb_context* ctx ) ;
FUNCTION: int libusb_event_handler_active ( libusb_context* ctx ) ;
FUNCTION: void libusb_lock_event_waiters ( libusb_context* ctx ) ;
FUNCTION: void libusb_unlock_event_waiters ( libusb_context* ctx ) ;
FUNCTION: int libusb_wait_for_event ( libusb_context* ctx, timeval* tv ) ;
FUNCTION: int libusb_handle_events_timeout ( libusb_context* ctx, timeval* tv ) ;
FUNCTION: int libusb_handle_events ( libusb_context* ctx ) ;
FUNCTION: int libusb_handle_events_locked ( libusb_context* ctx, timeval* tv ) ;
FUNCTION: int libusb_get_next_timeout ( libusb_context* ctx, timeval* tv ) ;
STRUCT: libusb_pollfd
{ fd int }
{ events short } ;
CALLBACK: void libusb_pollfd_added_cb ( int fd, short events, void* user_data ) ;
CALLBACK: void libusb_pollfd_removed_cb ( int fd, void* user_data ) ;
FUNCTION: libusb_pollfd** libusb_get_pollfds ( libusb_context* ctx ) ;
FUNCTION: void libusb_set_pollfd_notifiers ( libusb_context* ctx,
libusb_pollfd_added_cb added_cb,
libusb_pollfd_removed_cb removed_cb,
void* user_data ) ;