]> gitweb.factorcode.org Git - factor.git/blob - extra/libusb/libusb.factor
use radix literals
[factor.git] / extra / libusb / libusb.factor
1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://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 io.binary
5 kernel locals math sequences specialized-arrays
6 system unix.time unix.types ;
7 FROM: alien.c-types => short ;
8 IN: libusb
9
10 << "libusb" {
11         { [ os windows? ] [ "libusb-1.0.dll" ] }
12         { [ os macosx? ] [ "libusb-1.0.dylib"  ] }
13         { [ os unix?  ] [ "libusb-1.0.so" ] }
14     } cond cdecl add-library >>
15 LIBRARY: libusb
16
17 : libusb_cpu_to_le16 ( x -- y )
18     2 >native-endian le> ; inline
19
20 ALIAS: libusb_le16_to_cpu libusb_cpu_to_le16
21
22 ENUM: libusb_class_code
23     { LIBUSB_CLASS_PER_INTERFACE 0 }
24     { LIBUSB_CLASS_AUDIO         1 }
25     { LIBUSB_CLASS_COMM          2 }
26     { LIBUSB_CLASS_HID           3 }
27     { LIBUSB_CLASS_PRINTER       7 }
28     { LIBUSB_CLASS_PTP           6 }
29     { LIBUSB_CLASS_MASS_STORAGE  8 }
30     { LIBUSB_CLASS_HUB           9 }
31     { LIBUSB_CLASS_DATA          10 }
32     { LIBUSB_CLASS_VENDOR_SPEC 0xff } ;
33
34 ENUM: libusb_descriptor_type
35     { LIBUSB_DT_DEVICE    0x01 }
36     { LIBUSB_DT_CONFIG    0x02 }
37     { LIBUSB_DT_STRING    0x03 }
38     { LIBUSB_DT_INTERFACE 0x04 }
39     { LIBUSB_DT_ENDPOINT  0x05 }
40     { LIBUSB_DT_HID       0x21 }
41     { LIBUSB_DT_REPORT    0x22 }
42     { LIBUSB_DT_PHYSICAL  0x23 }
43     { LIBUSB_DT_HUB       0x29 } ;
44
45 CONSTANT: LIBUSB_DT_DEVICE_SIZE           18
46 CONSTANT: LIBUSB_DT_CONFIG_SIZE           9
47 CONSTANT: LIBUSB_DT_INTERFACE_SIZE        9
48 CONSTANT: LIBUSB_DT_ENDPOINT_SIZE         7
49 CONSTANT: LIBUSB_DT_ENDPOINT_AUDIO_SIZE   9
50 CONSTANT: LIBUSB_DT_HUB_NONVAR_SIZE       7
51
52 CONSTANT: LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f
53 CONSTANT: LIBUSB_ENDPOINT_DIR_MASK        0x80
54
55 ENUM: libusb_endpoint_direction
56     { LIBUSB_ENDPOINT_IN  0x80 }
57     { LIBUSB_ENDPOINT_OUT 0x00 } ;
58
59 CONSTANT: LIBUSB_TRANSFER_TYPE_MASK 0x03
60
61 ENUM: libusb_transfer_type
62     { LIBUSB_TRANSFER_TYPE_CONTROL     0 }
63     { LIBUSB_TRANSFER_TYPE_ISOCHRONOUS 1 }
64     { LIBUSB_TRANSFER_TYPE_BULK        2 }
65     { LIBUSB_TRANSFER_TYPE_INTERRUPT   3 } ;
66
67 ENUM: libusb_standard_request
68     { LIBUSB_REQUEST_GET_STATUS        0x00 }
69     { LIBUSB_REQUEST_CLEAR_FEATURE     0x01 }
70     { LIBUSB_REQUEST_SET_FEATURE       0x03 }
71     { LIBUSB_REQUEST_SET_ADDRESS       0x05 }
72     { LIBUSB_REQUEST_GET_DESCRIPTOR    0x06 }
73     { LIBUSB_REQUEST_SET_DESCRIPTOR    0x07 }
74     { LIBUSB_REQUEST_GET_CONFIGURATION 0x08 }
75     { LIBUSB_REQUEST_SET_CONFIGURATION 0x09 }
76     { LIBUSB_REQUEST_GET_INTERFACE     0x0A }
77     { LIBUSB_REQUEST_SET_INTERFACE     0x0B }
78     { LIBUSB_REQUEST_SYNCH_FRAME       0x0C } ;
79
80 ENUM: libusb_request_type
81     { LIBUSB_REQUEST_TYPE_STANDARD 0x00 }
82     { LIBUSB_REQUEST_TYPE_CLASS    0x20 }
83     { LIBUSB_REQUEST_TYPE_VENDOR   0x40 }
84     { LIBUSB_REQUEST_TYPE_RESERVED 0x60 } ;
85
86 ENUM: libusb_request_recipient
87     { LIBUSB_RECIPIENT_DEVICE    0x00 }
88     { LIBUSB_RECIPIENT_INTERFACE 0x01 }
89     { LIBUSB_RECIPIENT_ENDPOINT  0x02 }
90     { LIBUSB_RECIPIENT_OTHER     0x03 } ;
91
92 CONSTANT: LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
93
94 ENUM: libusb_iso_sync_type
95     { LIBUSB_ISO_SYNC_TYPE_NONE     0 }
96     { LIBUSB_ISO_SYNC_TYPE_ASYNC    1 }
97     { LIBUSB_ISO_SYNC_TYPE_ADAPTIVE 2 }
98     { LIBUSB_ISO_SYNC_TYPE_SYNC     3 } ;
99
100 CONSTANT: LIBUSB_ISO_USAGE_TYPE_MASK 0x30
101
102 ENUM: libusb_iso_usage_type
103     { LIBUSB_ISO_USAGE_TYPE_DATA     0 }
104     { LIBUSB_ISO_USAGE_TYPE_FEEDBACK 1 }
105     { LIBUSB_ISO_USAGE_TYPE_IMPLICIT 2 } ;
106
107 STRUCT: libusb_device_descriptor
108     { bLength             uint8_t  }
109     { bDescriptorType     uint8_t  }
110     { bcdUSB              uint16_t }
111     { bDeviceClass        uint8_t  }
112     { bDeviceSubClass     uint8_t  }
113     { bDeviceProtocol     uint8_t  }
114     { bMaxPacketSize0     uint8_t  }
115     { idVendor            uint16_t }
116     { idProduct           uint16_t }
117     { bcdDevice           uint16_t }
118     { iManufacturer       uint8_t  }
119     { iProduct            uint8_t  }
120     { iSerialNumber       uint8_t  }
121     { bNumConfigurations  uint8_t  } ;
122
123 STRUCT: libusb_endpoint_descriptor
124     { bLength           uint8_t  }
125     { bDescriptorType   uint8_t  }
126     { bEndpointAddress  uint8_t  }
127     { bmAttributes      uint8_t  }
128     { wMaxPacketSize    uint16_t }
129     { bInterval         uint8_t  }
130     { bRefresh          uint8_t  }
131     { bSynchAddress     uint8_t  }
132     { extra             uchar*   }
133     { extra_length      int      } ;
134
135 STRUCT: libusb_interface_descriptor
136     { bLength             uint8_t                     }
137     { bDescriptorType     uint8_t                     }
138     { bInterfaceNumber    uint8_t                     }
139     { bAlternateSetting   uint8_t                     }
140     { bNumEndpoints       uint8_t                     }
141     { bInterfaceClass     uint8_t                     }
142     { bInterfaceSubClass  uint8_t                     }
143     { bInterfaceProtocol  uint8_t                     }
144     { iInterface          uint8_t                     }
145     { endpoint            libusb_endpoint_descriptor* }
146     { extra               uchar*                      }
147     { extra_length        int                         } ;
148
149 STRUCT: libusb_interface
150     { altsetting     libusb_interface_descriptor* }
151     { num_altsetting int                          } ;
152
153 STRUCT: libusb_config_descriptor
154     { bLength              uint8_t           }
155     { bDescriptorType      uint8_t           }
156     { wTotalLength         uint16_t          }
157     { bNumInterfaces       uint8_t           }
158     { bConfigurationValue  uint8_t           }
159     { iConfiguration       uint8_t           }
160     { bmAttributes         uint8_t           }
161     { MaxPower             uint8_t           }
162     { interface            libusb_interface* }
163     { extra                uchar*            }
164     { extra_length         int               } ;
165
166 STRUCT: libusb_control_setup
167     { bmRequestType  uint8_t  }
168     { bRequest       uint8_t  }
169     { wValue         uint16_t }
170     { wIndex         uint16_t }
171     { wLength        uint16_t } ;
172
173 : LIBUSB_CONTROL_SETUP_SIZE ( -- x ) libusb_control_setup heap-size ; inline
174
175 C-TYPE: libusb_context
176 C-TYPE: libusb_device
177 C-TYPE: libusb_device_handle
178
179 ENUM: libusb_error
180     { LIBUSB_SUCCESS             0 }
181     { LIBUSB_ERROR_IO            -1 }
182     { LIBUSB_ERROR_INVALID_PARAM -2 }
183     { LIBUSB_ERROR_ACCESS        -3 }
184     { LIBUSB_ERROR_NO_DEVICE     -4 }
185     { LIBUSB_ERROR_NOT_FOUND     -5 }
186     { LIBUSB_ERROR_BUSY          -6 }
187     { LIBUSB_ERROR_TIMEOUT       -7 }
188     { LIBUSB_ERROR_OVERFLOW      -8 }
189     { LIBUSB_ERROR_PIPE          -9 }
190     { LIBUSB_ERROR_INTERRUPTED   -10 }
191     { LIBUSB_ERROR_NO_MEM        -11 }
192     { LIBUSB_ERROR_NOT_SUPPORTED -12 }
193     { LIBUSB_ERROR_OTHER         -99 } ;
194
195 ENUM: libusb_transfer_status
196     LIBUSB_TRANSFER_COMPLETED
197     LIBUSB_TRANSFER_ERROR
198     LIBUSB_TRANSFER_TIMED_OUT
199     LIBUSB_TRANSFER_CANCELLED
200     LIBUSB_TRANSFER_STALL
201     LIBUSB_TRANSFER_NO_DEVICE
202     LIBUSB_TRANSFER_OVERFLOW ;
203
204 ENUM: libusb_transfer_flags
205     { LIBUSB_TRANSFER_SHORT_NOT_OK  1 }
206     { LIBUSB_TRANSFER_FREE_BUFFER   2 }
207     { LIBUSB_TRANSFER_FREE_TRANSFER 4 } ;
208
209 STRUCT: libusb_iso_packet_descriptor
210     { length        uint                   }
211     { actual_length uint                   }
212     { status        libusb_transfer_status } ;
213 SPECIALIZED-ARRAY: libusb_iso_packet_descriptor
214
215 C-TYPE: libusb_transfer
216
217 CALLBACK: void libusb_transfer_cb_fn ( libusb_transfer* transfer ) ;
218
219 STRUCT: libusb_transfer
220     { dev_handle      libusb_device_handle*           }
221     { flags           uint8_t                         }
222     { endpoint        uchar                           }
223     { type            uchar                           }
224     { timeout         uint                            }
225     { status          libusb_transfer_status          }
226     { length          int                             }
227     { actual_length   int                             }
228     { callback        libusb_transfer_cb_fn           }
229     { user_data       void*                           }
230     { buffer          uchar*                          }
231     { num_iso_packets int                             }
232     { iso_packet_desc libusb_iso_packet_descriptor[0] } ;
233
234 FUNCTION: int libusb_init ( libusb_context** ctx ) ;
235 FUNCTION: void libusb_exit ( libusb_context* ctx ) ;
236 FUNCTION: void libusb_set_debug ( libusb_context* ctx, int level ) ;
237
238 FUNCTION: ssize_t libusb_get_device_list ( libusb_context* ctx, libusb_device*** list ) ;
239 FUNCTION: void libusb_free_device_list ( libusb_device** list, int unref_devices ) ;
240 FUNCTION: libusb_device* libusb_ref_device ( libusb_device* dev ) ;
241 FUNCTION: void libusb_unref_device ( libusb_device* dev ) ;
242
243 FUNCTION: int libusb_get_configuration ( libusb_device_handle* dev, int* config ) ;
244 FUNCTION: int libusb_get_device_descriptor ( libusb_device* dev, libusb_device_descriptor* desc ) ;
245 FUNCTION: int libusb_get_active_config_descriptor ( libusb_device* dev, libusb_config_descriptor** config ) ;
246 FUNCTION: int libusb_get_config_descriptor ( libusb_device* dev, uint8_t config_index, libusb_config_descriptor** config ) ;
247 FUNCTION: int libusb_get_config_descriptor_by_value ( libusb_device* dev, uint8_t bConfigurationValue, libusb_config_descriptor** config ) ;
248 FUNCTION: void libusb_free_config_descriptor ( libusb_config_descriptor* config ) ;
249 FUNCTION: uint8_t libusb_get_bus_number ( libusb_device* dev ) ;
250 FUNCTION: uint8_t libusb_get_device_address ( libusb_device* dev ) ;
251 FUNCTION: int libusb_get_max_packet_size ( libusb_device* dev, uchar endpoint ) ;
252
253 FUNCTION: int libusb_open ( libusb_device* dev, libusb_device_handle** handle ) ;
254 FUNCTION: void libusb_close ( libusb_device_handle* dev_handle ) ;
255 FUNCTION: libusb_device* libusb_get_device ( libusb_device_handle* dev_handle ) ;
256
257 FUNCTION: int libusb_set_configuration ( libusb_device_handle* dev, int configuration ) ;
258 FUNCTION: int libusb_claim_interface ( libusb_device_handle* dev, int iface ) ;
259 FUNCTION: int libusb_release_interface ( libusb_device_handle* dev, int iface ) ;
260
261 FUNCTION: libusb_device_handle* libusb_open_device_with_vid_pid ( libusb_context* ctx, uint16_t vendor_id, uint16_t product_id ) ;
262
263 FUNCTION: int libusb_set_interface_alt_setting ( libusb_device_handle* dev, int interface_number, int alternate_setting ) ;
264 FUNCTION: int libusb_clear_halt ( libusb_device_handle* dev, uchar endpoint ) ;
265 FUNCTION: int libusb_reset_device ( libusb_device_handle* dev ) ;
266
267 FUNCTION: int libusb_kernel_driver_active ( libusb_device_handle* dev, int interface ) ;
268 FUNCTION: int libusb_detach_kernel_driver ( libusb_device_handle* dev, int interface ) ;
269 FUNCTION: int libusb_attach_kernel_driver ( libusb_device_handle* dev, int interface ) ;
270
271 : libusb_control_transfer_get_data ( transfer -- data )
272     buffer>> LIBUSB_CONTROL_SETUP_SIZE swap <displaced-alien> ; inline
273
274 : libusb_control_transfer_get_setup ( transfer -- setup )
275     buffer>> libusb_control_setup memory>struct ; inline
276
277 :: libusb_fill_control_setup ( buffer bmRequestType bRequest wValue wIndex wLength -- )
278     buffer libusb_control_setup memory>struct
279     bmRequestType              >>bmRequestType
280     bRequest                   >>bRequest
281     wValue libusb_cpu_to_le16  >>wValue
282     wIndex libusb_cpu_to_le16  >>wIndex
283     wLength libusb_cpu_to_le16 >>wLength drop ; inline
284
285 FUNCTION: libusb_transfer* libusb_alloc_transfer ( int iso_packets ) ;
286 FUNCTION: int libusb_submit_transfer ( libusb_transfer* transfer ) ;
287 FUNCTION: int libusb_cancel_transfer ( libusb_transfer* transfer ) ;
288 FUNCTION: void libusb_free_transfer ( libusb_transfer* transfer ) ;
289
290 :: libusb_fill_control_transfer ( transfer dev_handle buffer callback user_data timeout -- )
291     transfer
292     dev_handle                   >>dev_handle
293     0                            >>endpoint
294     LIBUSB_TRANSFER_TYPE_CONTROL >>type
295     timeout                      >>timeout
296     buffer                       >>buffer
297     user_data                    >>user_data
298     callback                     >>callback
299     
300     buffer [
301         libusb_control_setup memory>struct wLength>> LIBUSB_CONTROL_SETUP_SIZE +
302     ] [ 0 ] if* >>length drop ; inline
303
304 :: libusb_fill_bulk_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
305     transfer
306     dev_handle                >>dev_handle
307     endpoint                  >>endpoint
308     LIBUSB_TRANSFER_TYPE_BULK >>type
309     timeout                   >>timeout
310     buffer                    >>buffer
311     length                    >>length
312     user_data                 >>user_data
313     callback                  >>callback
314     drop ; inline
315
316 :: libusb_fill_interrupt_transfer ( transfer dev_handle endpoint buffer length callback user_data timeout -- )
317     transfer
318     dev_handle                     >>dev_handle
319     endpoint                       >>endpoint
320     LIBUSB_TRANSFER_TYPE_INTERRUPT >>type
321     timeout                        >>timeout
322     buffer                         >>buffer
323     length                         >>length
324     user_data                      >>user_data
325     callback                       >>callback
326     drop ; inline
327
328 :: libusb_fill_iso_transfer ( transfer dev_handle endpoint buffer length num_iso_packets callback user_data timeout -- )
329     transfer
330     dev_handle                       >>dev_handle
331     endpoint                         >>endpoint
332     LIBUSB_TRANSFER_TYPE_ISOCHRONOUS >>type
333     timeout                          >>timeout
334     buffer                           >>buffer
335     length                           >>length
336     num_iso_packets                  >>num_iso_packets
337     user_data                        >>user_data
338     callback                         >>callback
339     drop ; inline
340
341 : libusb_set_iso_packet_lengths ( transfer length -- )
342     [ [ iso_packet_desc>> >c-ptr ]
343       [ num_iso_packets>> ] bi
344       libusb_iso_packet_descriptor <c-direct-array>
345     ] dip [ >>length drop ] curry each ; inline
346     
347 :: libusb_get_iso_packet_buffer ( transfer packet -- data )
348     packet transfer num_iso_packets>> >=
349     [ f ]
350     [
351         transfer
352         [ iso_packet_desc>> >c-ptr ] 
353         [ num_iso_packets>> ] bi
354         libusb_iso_packet_descriptor <c-direct-array> 0
355         [ length>> + ] reduce
356         transfer buffer>> <displaced-alien>
357     ] if ;
358
359 :: libusb_get_iso_packet_buffer_simple ( transfer packet -- data )
360     packet transfer num_iso_packets>> >=
361     [ f ]
362     [
363         0 transfer
364         [ iso_packet_desc>> >c-ptr ] 
365         [ num_iso_packets>> ] bi
366         libusb_iso_packet_descriptor <c-direct-array> nth
367         length>> packet *
368         transfer buffer>> <displaced-alien>
369     ] if ;
370
371 FUNCTION: int libusb_control_transfer ( libusb_device_handle* dev_handle,
372     uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
373     uchar* data, uint16_t length, uint timeout ) ;
374
375 FUNCTION: int libusb_bulk_transfer ( libusb_device_handle* dev_handle,
376     uchar endpoint, uchar* data, int length,
377     int* actual_length, uint timeout ) ;
378
379 FUNCTION: int libusb_interrupt_transfer ( libusb_device_handle* dev_handle,
380     uchar endpoint, uchar* data, int length,
381     int* actual_length, int timeout ) ;
382
383 :: libusb_get_descriptor ( dev desc_type desc_index data length -- int )
384     dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
385     desc_type 8 shift desc_index bitor 0 data
386     length 1000 libusb_control_transfer ; inline
387
388 :: libusb_get_string_descriptor ( dev desc_index langid data length -- int )
389     dev LIBUSB_ENDPOINT_IN LIBUSB_REQUEST_GET_DESCRIPTOR
390     LIBUSB_DT_STRING 8 shift desc_index bitor
391     langid data length 1000 libusb_control_transfer ; inline
392
393 FUNCTION: int libusb_get_string_descriptor_ascii ( libusb_device_handle* dev,
394                                                    uint8_t               index,
395                                                    uchar*                data,
396                                                    int                   length ) ;
397
398 FUNCTION: int libusb_try_lock_events ( libusb_context* ctx ) ;
399 FUNCTION: void libusb_lock_events ( libusb_context* ctx ) ;
400 FUNCTION: void libusb_unlock_events ( libusb_context* ctx ) ;
401 FUNCTION: int libusb_event_handling_ok ( libusb_context* ctx ) ;
402 FUNCTION: int libusb_event_handler_active ( libusb_context* ctx ) ;
403 FUNCTION: void libusb_lock_event_waiters ( libusb_context* ctx ) ;
404 FUNCTION: void libusb_unlock_event_waiters ( libusb_context* ctx ) ;
405 FUNCTION: int libusb_wait_for_event ( libusb_context* ctx, timeval* tv ) ;
406 FUNCTION: int libusb_handle_events_timeout ( libusb_context* ctx, timeval* tv ) ;
407 FUNCTION: int libusb_handle_events ( libusb_context* ctx ) ;
408 FUNCTION: int libusb_handle_events_locked ( libusb_context* ctx, timeval* tv ) ;
409 FUNCTION: int libusb_get_next_timeout ( libusb_context* ctx, timeval* tv ) ;
410
411 STRUCT: libusb_pollfd
412     { fd     int   }
413     { events short } ;
414
415 CALLBACK: void libusb_pollfd_added_cb ( int fd, short events, void* user_data ) ;
416 CALLBACK: void libusb_pollfd_removed_cb ( int fd, void* user_data ) ;
417
418 FUNCTION: libusb_pollfd** libusb_get_pollfds ( libusb_context* ctx ) ;
419 FUNCTION: void libusb_set_pollfd_notifiers ( libusb_context*          ctx,
420                                              libusb_pollfd_added_cb   added_cb,
421                                              libusb_pollfd_removed_cb removed_cb,
422                                              void*                    user_data ) ;