1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.libraries
4 alien.syntax classes.struct combinators endian
5 kernel locals math sequences specialized-arrays
6 system unix.time unix.types ;
10 { [ os windows? ] [ "libusb-1.0.dll" ] }
11 { [ os macosx? ] [ "libusb-1.0.dylib" ] }
12 { [ os unix? ] [ "libusb-1.0.so" ] }
13 } cond cdecl add-library >>
16 : libusb_cpu_to_le16 ( x -- y )
17 2 >native-endian le> ; inline
19 ALIAS: libusb_le16_to_cpu libusb_cpu_to_le16
21 ENUM: libusb_class_code
22 { LIBUSB_CLASS_PER_INTERFACE 0 }
23 { LIBUSB_CLASS_AUDIO 1 }
24 { LIBUSB_CLASS_COMM 2 }
25 { LIBUSB_CLASS_HID 3 }
26 { LIBUSB_CLASS_PRINTER 7 }
27 { LIBUSB_CLASS_PTP 6 }
28 { LIBUSB_CLASS_MASS_STORAGE 8 }
29 { LIBUSB_CLASS_HUB 9 }
30 { LIBUSB_CLASS_DATA 10 }
31 { LIBUSB_CLASS_VENDOR_SPEC 0xff } ;
33 ENUM: libusb_descriptor_type
34 { LIBUSB_DT_DEVICE 0x01 }
35 { LIBUSB_DT_CONFIG 0x02 }
36 { LIBUSB_DT_STRING 0x03 }
37 { LIBUSB_DT_INTERFACE 0x04 }
38 { LIBUSB_DT_ENDPOINT 0x05 }
39 { LIBUSB_DT_HID 0x21 }
40 { LIBUSB_DT_REPORT 0x22 }
41 { LIBUSB_DT_PHYSICAL 0x23 }
42 { LIBUSB_DT_HUB 0x29 } ;
44 CONSTANT: LIBUSB_DT_DEVICE_SIZE 18
45 CONSTANT: LIBUSB_DT_CONFIG_SIZE 9
46 CONSTANT: LIBUSB_DT_INTERFACE_SIZE 9
47 CONSTANT: LIBUSB_DT_ENDPOINT_SIZE 7
48 CONSTANT: LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9
49 CONSTANT: LIBUSB_DT_HUB_NONVAR_SIZE 7
51 CONSTANT: LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f
52 CONSTANT: LIBUSB_ENDPOINT_DIR_MASK 0x80
54 ENUM: libusb_endpoint_direction
55 { LIBUSB_ENDPOINT_IN 0x80 }
56 { LIBUSB_ENDPOINT_OUT 0x00 } ;
58 CONSTANT: LIBUSB_TRANSFER_TYPE_MASK 0x03
60 ENUM: libusb_transfer_type
61 { LIBUSB_TRANSFER_TYPE_CONTROL 0 }
62 { LIBUSB_TRANSFER_TYPE_ISOCHRONOUS 1 }
63 { LIBUSB_TRANSFER_TYPE_BULK 2 }
64 { LIBUSB_TRANSFER_TYPE_INTERRUPT 3 } ;
66 ENUM: libusb_standard_request
67 { LIBUSB_REQUEST_GET_STATUS 0x00 }
68 { LIBUSB_REQUEST_CLEAR_FEATURE 0x01 }
69 { LIBUSB_REQUEST_SET_FEATURE 0x03 }
70 { LIBUSB_REQUEST_SET_ADDRESS 0x05 }
71 { LIBUSB_REQUEST_GET_DESCRIPTOR 0x06 }
72 { LIBUSB_REQUEST_SET_DESCRIPTOR 0x07 }
73 { LIBUSB_REQUEST_GET_CONFIGURATION 0x08 }
74 { LIBUSB_REQUEST_SET_CONFIGURATION 0x09 }
75 { LIBUSB_REQUEST_GET_INTERFACE 0x0A }
76 { LIBUSB_REQUEST_SET_INTERFACE 0x0B }
77 { LIBUSB_REQUEST_SYNCH_FRAME 0x0C } ;
79 ENUM: libusb_request_type
80 { LIBUSB_REQUEST_TYPE_STANDARD 0x00 }
81 { LIBUSB_REQUEST_TYPE_CLASS 0x20 }
82 { LIBUSB_REQUEST_TYPE_VENDOR 0x40 }
83 { LIBUSB_REQUEST_TYPE_RESERVED 0x60 } ;
85 ENUM: libusb_request_recipient
86 { LIBUSB_RECIPIENT_DEVICE 0x00 }
87 { LIBUSB_RECIPIENT_INTERFACE 0x01 }
88 { LIBUSB_RECIPIENT_ENDPOINT 0x02 }
89 { LIBUSB_RECIPIENT_OTHER 0x03 } ;
91 CONSTANT: LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
93 ENUM: libusb_iso_sync_type
94 { LIBUSB_ISO_SYNC_TYPE_NONE 0 }
95 { LIBUSB_ISO_SYNC_TYPE_ASYNC 1 }
96 { LIBUSB_ISO_SYNC_TYPE_ADAPTIVE 2 }
97 { LIBUSB_ISO_SYNC_TYPE_SYNC 3 } ;
99 CONSTANT: LIBUSB_ISO_USAGE_TYPE_MASK 0x30
101 ENUM: libusb_iso_usage_type
102 { LIBUSB_ISO_USAGE_TYPE_DATA 0 }
103 { LIBUSB_ISO_USAGE_TYPE_FEEDBACK 1 }
104 { LIBUSB_ISO_USAGE_TYPE_IMPLICIT 2 } ;
106 STRUCT: libusb_device_descriptor
108 { bDescriptorType uint8_t }
110 { bDeviceClass uint8_t }
111 { bDeviceSubClass uint8_t }
112 { bDeviceProtocol uint8_t }
113 { bMaxPacketSize0 uint8_t }
114 { idVendor uint16_t }
115 { idProduct uint16_t }
116 { bcdDevice uint16_t }
117 { iManufacturer uint8_t }
119 { iSerialNumber uint8_t }
120 { bNumConfigurations uint8_t } ;
122 STRUCT: libusb_endpoint_descriptor
124 { bDescriptorType uint8_t }
125 { bEndpointAddress uint8_t }
126 { bmAttributes uint8_t }
127 { wMaxPacketSize uint16_t }
128 { bInterval uint8_t }
130 { bSynchAddress uint8_t }
132 { extra_length int } ;
134 STRUCT: libusb_interface_descriptor
136 { bDescriptorType uint8_t }
137 { bInterfaceNumber uint8_t }
138 { bAlternateSetting uint8_t }
139 { bNumEndpoints uint8_t }
140 { bInterfaceClass uint8_t }
141 { bInterfaceSubClass uint8_t }
142 { bInterfaceProtocol uint8_t }
143 { iInterface uint8_t }
144 { endpoint libusb_endpoint_descriptor* }
146 { extra_length int } ;
148 STRUCT: libusb_interface
149 { altsetting libusb_interface_descriptor* }
150 { num_altsetting int } ;
152 STRUCT: libusb_config_descriptor
154 { bDescriptorType uint8_t }
155 { wTotalLength uint16_t }
156 { bNumInterfaces uint8_t }
157 { bConfigurationValue uint8_t }
158 { iConfiguration uint8_t }
159 { bmAttributes uint8_t }
161 { interface libusb_interface* }
163 { extra_length int } ;
165 STRUCT: libusb_control_setup
166 { bmRequestType uint8_t }
170 { wLength uint16_t } ;
172 : LIBUSB_CONTROL_SETUP_SIZE ( -- x ) libusb_control_setup heap-size ; inline
174 C-TYPE: libusb_context
175 C-TYPE: libusb_device
176 C-TYPE: libusb_device_handle
180 { LIBUSB_ERROR_IO -1 }
181 { LIBUSB_ERROR_INVALID_PARAM -2 }
182 { LIBUSB_ERROR_ACCESS -3 }
183 { LIBUSB_ERROR_NO_DEVICE -4 }
184 { LIBUSB_ERROR_NOT_FOUND -5 }
185 { LIBUSB_ERROR_BUSY -6 }
186 { LIBUSB_ERROR_TIMEOUT -7 }
187 { LIBUSB_ERROR_OVERFLOW -8 }
188 { LIBUSB_ERROR_PIPE -9 }
189 { LIBUSB_ERROR_INTERRUPTED -10 }
190 { LIBUSB_ERROR_NO_MEM -11 }
191 { LIBUSB_ERROR_NOT_SUPPORTED -12 }
192 { LIBUSB_ERROR_OTHER -99 } ;
194 ENUM: libusb_transfer_status
195 LIBUSB_TRANSFER_COMPLETED
196 LIBUSB_TRANSFER_ERROR
197 LIBUSB_TRANSFER_TIMED_OUT
198 LIBUSB_TRANSFER_CANCELLED
199 LIBUSB_TRANSFER_STALL
200 LIBUSB_TRANSFER_NO_DEVICE
201 LIBUSB_TRANSFER_OVERFLOW ;
203 ENUM: libusb_transfer_flags
204 { LIBUSB_TRANSFER_SHORT_NOT_OK 1 }
205 { LIBUSB_TRANSFER_FREE_BUFFER 2 }
206 { LIBUSB_TRANSFER_FREE_TRANSFER 4 } ;
208 STRUCT: libusb_iso_packet_descriptor
210 { actual_length uint }
211 { status libusb_transfer_status } ;
212 SPECIALIZED-ARRAY: libusb_iso_packet_descriptor
214 C-TYPE: libusb_transfer
216 CALLBACK: void libusb_transfer_cb_fn ( libusb_transfer* transfer )
218 STRUCT: libusb_transfer
219 { dev_handle libusb_device_handle* }
224 { status libusb_transfer_status }
226 { actual_length int }
227 { callback libusb_transfer_cb_fn }
230 { num_iso_packets int }
231 { iso_packet_desc libusb_iso_packet_descriptor[0] } ;
233 FUNCTION: int libusb_init ( libusb_context** ctx )
234 FUNCTION: void libusb_exit ( libusb_context* ctx )
235 FUNCTION: void libusb_set_debug ( libusb_context* ctx, int level )
237 FUNCTION: ssize_t libusb_get_device_list ( libusb_context* ctx, libusb_device*** list )
238 FUNCTION: void libusb_free_device_list ( libusb_device** list, int unref_devices )
239 FUNCTION: libusb_device* libusb_ref_device ( libusb_device* dev )
240 FUNCTION: void libusb_unref_device ( libusb_device* dev )
242 FUNCTION: int libusb_get_configuration ( libusb_device_handle* dev, int* config )
243 FUNCTION: int libusb_get_device_descriptor ( libusb_device* dev, libusb_device_descriptor* desc )
244 FUNCTION: int libusb_get_active_config_descriptor ( libusb_device* dev, libusb_config_descriptor** config )
245 FUNCTION: int libusb_get_config_descriptor ( libusb_device* dev, uint8_t config_index, libusb_config_descriptor** config )
246 FUNCTION: int libusb_get_config_descriptor_by_value ( libusb_device* dev, uint8_t bConfigurationValue, libusb_config_descriptor** config )
247 FUNCTION: void libusb_free_config_descriptor ( libusb_config_descriptor* config )
248 FUNCTION: uint8_t libusb_get_bus_number ( libusb_device* dev )
249 FUNCTION: uint8_t libusb_get_device_address ( libusb_device* dev )
250 FUNCTION: int libusb_get_max_packet_size ( libusb_device* dev, uchar endpoint )
252 FUNCTION: int libusb_open ( libusb_device* dev, libusb_device_handle** handle )
253 FUNCTION: void libusb_close ( libusb_device_handle* dev_handle )
254 FUNCTION: libusb_device* libusb_get_device ( libusb_device_handle* dev_handle )
256 FUNCTION: int libusb_set_configuration ( libusb_device_handle* dev, int configuration )
257 FUNCTION: int libusb_claim_interface ( libusb_device_handle* dev, int iface )
258 FUNCTION: int libusb_release_interface ( libusb_device_handle* dev, int iface )
260 FUNCTION: libusb_device_handle* libusb_open_device_with_vid_pid ( libusb_context* ctx, uint16_t vendor_id, uint16_t product_id )
262 FUNCTION: int libusb_set_interface_alt_setting ( libusb_device_handle* dev, int interface_number, int alternate_setting )
263 FUNCTION: int libusb_clear_halt ( libusb_device_handle* dev, uchar endpoint )
264 FUNCTION: int libusb_reset_device ( libusb_device_handle* dev )
266 FUNCTION: int libusb_kernel_driver_active ( libusb_device_handle* dev, int interface )
267 FUNCTION: int libusb_detach_kernel_driver ( libusb_device_handle* dev, int interface )
268 FUNCTION: int libusb_attach_kernel_driver ( libusb_device_handle* dev, int interface )
270 : libusb_control_transfer_get_data ( transfer -- data )
271 buffer>> LIBUSB_CONTROL_SETUP_SIZE swap <displaced-alien> ; inline
273 : libusb_control_transfer_get_setup ( transfer -- setup )
274 buffer>> libusb_control_setup memory>struct ; inline
276 :: libusb_fill_control_setup ( buffer bmRequestType bRequest wValue wIndex wLength -- )
277 buffer libusb_control_setup memory>struct
278 bmRequestType >>bmRequestType
280 wValue libusb_cpu_to_le16 >>wValue
281 wIndex libusb_cpu_to_le16 >>wIndex
282 wLength libusb_cpu_to_le16 >>wLength drop ; inline
284 FUNCTION: libusb_transfer* libusb_alloc_transfer ( int iso_packets )
285 FUNCTION: int libusb_submit_transfer ( libusb_transfer* transfer )
286 FUNCTION: int libusb_cancel_transfer ( libusb_transfer* transfer )
287 FUNCTION: void libusb_free_transfer ( libusb_transfer* transfer )
289 :: libusb_fill_control_transfer ( transfer dev_handle buffer callback user_data timeout -- )
291 dev_handle >>dev_handle
293 LIBUSB_TRANSFER_TYPE_CONTROL >>type
296 user_data >>user_data
300 libusb_control_setup memory>struct wLength>> LIBUSB_CONTROL_SETUP_SIZE +
301 ] [ 0 ] if* >>length drop ; inline
303 :: libusb_fill_bulk_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
305 dev_handle >>dev_handle
307 LIBUSB_TRANSFER_TYPE_BULK >>type
311 user_data >>user_data
315 :: libusb_fill_interrupt_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
317 dev_handle >>dev_handle
319 LIBUSB_TRANSFER_TYPE_INTERRUPT >>type
323 user_data >>user_data
327 :: libusb_fill_iso_transfer ( transfer dev_handle endpoint buffer length num_iso_packets callback user_data timeout -- )
329 dev_handle >>dev_handle
331 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS >>type
335 num_iso_packets >>num_iso_packets
336 user_data >>user_data
340 : libusb_set_iso_packet_lengths ( transfer length -- )
341 [ [ iso_packet_desc>> >c-ptr ]
342 [ num_iso_packets>> ] bi
343 libusb_iso_packet_descriptor <c-direct-array>
344 ] dip [ >>length drop ] curry each ; inline
346 :: libusb_get_iso_packet_buffer ( transfer packet -- data )
347 packet transfer num_iso_packets>> >=
351 [ iso_packet_desc>> >c-ptr ]
352 [ num_iso_packets>> ] bi
353 libusb_iso_packet_descriptor <c-direct-array> 0
354 [ length>> + ] reduce
355 transfer buffer>> <displaced-alien>
358 :: libusb_get_iso_packet_buffer_simple ( transfer packet -- data )
359 packet transfer num_iso_packets>> >=
363 [ iso_packet_desc>> >c-ptr ]
364 [ num_iso_packets>> ] bi
365 libusb_iso_packet_descriptor <c-direct-array> nth
367 transfer buffer>> <displaced-alien>
370 FUNCTION: int libusb_control_transfer ( libusb_device_handle* dev_handle,
371 uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
372 uchar* data, uint16_t length, uint timeout )
374 FUNCTION: int libusb_bulk_transfer ( libusb_device_handle* dev_handle,
375 uchar endpoint, uchar* data, int length,
376 int* actual_length, uint timeout )
378 FUNCTION: int libusb_interrupt_transfer ( libusb_device_handle* dev_handle,
379 uchar endpoint, uchar* data, int length,
380 int* actual_length, int timeout )
382 :: libusb_get_descriptor ( dev desc_type desc_index data length -- int )
383 dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
384 desc_type 8 shift desc_index bitor 0 data
385 length 1000 libusb_control_transfer ; inline
387 :: libusb_get_string_descriptor ( dev desc_index langid data length -- int )
388 dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
389 LIBUSB_DT_STRING 8 shift desc_index bitor
390 langid data length 1000 libusb_control_transfer ; inline
392 FUNCTION: int libusb_get_string_descriptor_ascii ( libusb_device_handle* dev,
397 FUNCTION: int libusb_try_lock_events ( libusb_context* ctx )
398 FUNCTION: void libusb_lock_events ( libusb_context* ctx )
399 FUNCTION: void libusb_unlock_events ( libusb_context* ctx )
400 FUNCTION: int libusb_event_handling_ok ( libusb_context* ctx )
401 FUNCTION: int libusb_event_handler_active ( libusb_context* ctx )
402 FUNCTION: void libusb_lock_event_waiters ( libusb_context* ctx )
403 FUNCTION: void libusb_unlock_event_waiters ( libusb_context* ctx )
404 FUNCTION: int libusb_wait_for_event ( libusb_context* ctx, timeval* tv )
405 FUNCTION: int libusb_handle_events_timeout ( libusb_context* ctx, timeval* tv )
406 FUNCTION: int libusb_handle_events ( libusb_context* ctx )
407 FUNCTION: int libusb_handle_events_locked ( libusb_context* ctx, timeval* tv )
408 FUNCTION: int libusb_get_next_timeout ( libusb_context* ctx, timeval* tv )
410 STRUCT: libusb_pollfd
414 CALLBACK: void libusb_pollfd_added_cb ( int fd, short events, void* user_data )
415 CALLBACK: void libusb_pollfd_removed_cb ( int fd, void* user_data )
417 FUNCTION: libusb_pollfd** libusb_get_pollfds ( libusb_context* ctx )
418 FUNCTION: void libusb_set_pollfd_notifiers ( libusb_context* ctx,
419 libusb_pollfd_added_cb added_cb,
420 libusb_pollfd_removed_cb removed_cb,