]> gitweb.factorcode.org Git - factor.git/blob - extra/libusb/libusb.factor
change back other char/uchar* parameters that don't look like actual string types
[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.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 CONSTANT: LIBUSB_CLASS_PER_INTERFACE 0
23 CONSTANT: LIBUSB_CLASS_AUDIO         1
24 CONSTANT: LIBUSB_CLASS_COMM          2
25 CONSTANT: LIBUSB_CLASS_HID           3
26 CONSTANT: LIBUSB_CLASS_PRINTER       7
27 CONSTANT: LIBUSB_CLASS_PTP           6
28 CONSTANT: LIBUSB_CLASS_MASS_STORAGE  8
29 CONSTANT: LIBUSB_CLASS_HUB           9
30 CONSTANT: LIBUSB_CLASS_DATA          10
31 CONSTANT: LIBUSB_CLASS_VENDOR_SPEC HEX: ff
32 TYPEDEF: int libusb_class_code
33
34 CONSTANT: LIBUSB_DT_DEVICE    HEX: 01
35 CONSTANT: LIBUSB_DT_CONFIG    HEX: 02
36 CONSTANT: LIBUSB_DT_STRING    HEX: 03
37 CONSTANT: LIBUSB_DT_INTERFACE HEX: 04
38 CONSTANT: LIBUSB_DT_ENDPOINT  HEX: 05
39 CONSTANT: LIBUSB_DT_HID       HEX: 21
40 CONSTANT: LIBUSB_DT_REPORT    HEX: 22
41 CONSTANT: LIBUSB_DT_PHYSICAL  HEX: 23
42 CONSTANT: LIBUSB_DT_HUB       HEX: 29
43 TYPEDEF: int libusb_descriptor_type
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    HEX: 0f
53 CONSTANT: LIBUSB_ENDPOINT_DIR_MASK        HEX: 80
54
55 CONSTANT: LIBUSB_ENDPOINT_IN  HEX: 80
56 CONSTANT: LIBUSB_ENDPOINT_OUT HEX: 00
57 TYPEDEF: int libusb_endpoint_direction
58
59 CONSTANT: LIBUSB_TRANSFER_TYPE_MASK HEX: 03
60
61 CONSTANT: LIBUSB_TRANSFER_TYPE_CONTROL     0
62 CONSTANT: LIBUSB_TRANSFER_TYPE_ISOCHRONOUS 1
63 CONSTANT: LIBUSB_TRANSFER_TYPE_BULK        2
64 CONSTANT: LIBUSB_TRANSFER_TYPE_INTERRUPT   3
65 TYPEDEF: int libusb_transfer_type
66
67 CONSTANT: LIBUSB_REQUEST_GET_STATUS        HEX: 00
68 CONSTANT: LIBUSB_REQUEST_CLEAR_FEATURE     HEX: 01
69 CONSTANT: LIBUSB_REQUEST_SET_FEATURE       HEX: 03
70 CONSTANT: LIBUSB_REQUEST_SET_ADDRESS       HEX: 05
71 CONSTANT: LIBUSB_REQUEST_GET_DESCRIPTOR    HEX: 06
72 CONSTANT: LIBUSB_REQUEST_SET_DESCRIPTOR    HEX: 07
73 CONSTANT: LIBUSB_REQUEST_GET_CONFIGURATION HEX: 08
74 CONSTANT: LIBUSB_REQUEST_SET_CONFIGURATION HEX: 09
75 CONSTANT: LIBUSB_REQUEST_GET_INTERFACE     HEX: 0A
76 CONSTANT: LIBUSB_REQUEST_SET_INTERFACE     HEX: 0B
77 CONSTANT: LIBUSB_REQUEST_SYNCH_FRAME       HEX: 0C
78 TYPEDEF: int libusb_standard_request
79
80 CONSTANT: LIBUSB_REQUEST_TYPE_STANDARD HEX: 00
81 CONSTANT: LIBUSB_REQUEST_TYPE_CLASS    HEX: 20
82 CONSTANT: LIBUSB_REQUEST_TYPE_VENDOR   HEX: 40
83 CONSTANT: LIBUSB_REQUEST_TYPE_RESERVED HEX: 60
84
85 CONSTANT: LIBUSB_RECIPIENT_DEVICE    HEX: 00
86 CONSTANT: LIBUSB_RECIPIENT_INTERFACE HEX: 01
87 CONSTANT: LIBUSB_RECIPIENT_ENDPOINT  HEX: 02
88 CONSTANT: LIBUSB_RECIPIENT_OTHER     HEX: 03
89 TYPEDEF: int libusb_request_recipient
90
91 CONSTANT: LIBUSB_ISO_SYNC_TYPE_MASK HEX: 0C
92
93 CONSTANT: LIBUSB_ISO_SYNC_TYPE_NONE     0
94 CONSTANT: LIBUSB_ISO_SYNC_TYPE_ASYNC    1
95 CONSTANT: LIBUSB_ISO_SYNC_TYPE_ADAPTIVE 2
96 CONSTANT: LIBUSB_ISO_SYNC_TYPE_SYNC     3
97 TYPEDEF: int libusb_iso_sync_type
98
99 CONSTANT: LIBUSB_ISO_USAGE_TYPE_MASK HEX: 30
100
101 CONSTANT: LIBUSB_ISO_USAGE_TYPE_DATA     0
102 CONSTANT: LIBUSB_ISO_USAGE_TYPE_FEEDBACK 1
103 CONSTANT: LIBUSB_ISO_USAGE_TYPE_IMPLICIT 2
104 TYPEDEF: int libusb_iso_usage_type
105
106 STRUCT: libusb_device_descriptor
107     { bLength             uint8_t  }
108     { bDescriptorType     uint8_t  }
109     { bcdUSB              uint16_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  }
118     { iProduct            uint8_t  }
119     { iSerialNumber       uint8_t  }
120     { bNumConfigurations  uint8_t  } ;
121
122 STRUCT: libusb_endpoint_descriptor
123     { bLength           uint8_t  }
124     { bDescriptorType   uint8_t  }
125     { bEndpointAddress  uint8_t  }
126     { bmAttributes      uint8_t  }
127     { wMaxPacketSize    uint16_t }
128     { bInterval         uint8_t  }
129     { bRefresh          uint8_t  }
130     { bSynchAddress     uint8_t  }
131     { extra             uchar*   }
132     { extra_length      int      } ;
133
134 STRUCT: libusb_interface_descriptor
135     { bLength             uint8_t                     }
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* }
145     { extra               uchar*                      }
146     { extra_length        int                         } ;
147
148 STRUCT: libusb_interface
149     { altsetting     libusb_interface_descriptor* }
150     { num_altsetting int                          } ;
151
152 STRUCT: libusb_config_descriptor
153     { bLength              uint8_t           }
154     { bDescriptorType      uint8_t           }
155     { wTotalLength         uint16_t          }
156     { bNumInterfaces       uint8_t           }
157     { bConfigurationValue  uint8_t           }
158     { iConfiguration       uint8_t           }
159     { bmAttributes         uint8_t           }
160     { MaxPower             uint8_t           }
161     { interface            libusb_interface* }
162     { extra                uchar*            }
163     { extra_length         int               } ;
164
165 STRUCT: libusb_control_setup
166     { bmRequestType  uint8_t  }
167     { bRequest       uint8_t  }
168     { wValue         uint16_t }
169     { wIndex         uint16_t }
170     { wLength        uint16_t } ;
171
172 : LIBUSB_CONTROL_SETUP_SIZE ( -- x ) libusb_control_setup heap-size ; inline
173
174 C-TYPE: libusb_context
175 C-TYPE: libusb_device
176 C-TYPE: libusb_device_handle
177
178 CONSTANT: LIBUSB_SUCCESS             0
179 CONSTANT: LIBUSB_ERROR_IO            -1
180 CONSTANT: LIBUSB_ERROR_INVALID_PARAM -2
181 CONSTANT: LIBUSB_ERROR_ACCESS        -3
182 CONSTANT: LIBUSB_ERROR_NO_DEVICE     -4
183 CONSTANT: LIBUSB_ERROR_NOT_FOUND     -5
184 CONSTANT: LIBUSB_ERROR_BUSY          -6
185 CONSTANT: LIBUSB_ERROR_TIMEOUT       -7
186 CONSTANT: LIBUSB_ERROR_OVERFLOW      -8
187 CONSTANT: LIBUSB_ERROR_PIPE          -9
188 CONSTANT: LIBUSB_ERROR_INTERRUPTED   -10
189 CONSTANT: LIBUSB_ERROR_NO_MEM        -11
190 CONSTANT: LIBUSB_ERROR_NOT_SUPPORTED -12
191 CONSTANT: LIBUSB_ERROR_OTHER         -99
192 TYPEDEF: int libusb_error
193
194 C-ENUM:
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 ;
202 TYPEDEF: int libusb_transfer_status
203
204 CONSTANT: LIBUSB_TRANSFER_SHORT_NOT_OK  1
205 CONSTANT: LIBUSB_TRANSFER_FREE_BUFFER   2
206 CONSTANT: LIBUSB_TRANSFER_FREE_TRANSFER 4
207 TYPEDEF: int libusb_transfer_flags
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       <direct-libusb_iso_packet_descriptor-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         <direct-libusb_iso_packet_descriptor-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         <direct-libusb_iso_packet_descriptor-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 ) ;