X-Git-Url: http://developer.intra2net.com/git/?a=blobdiff_plain;f=src%2Fftdi.c;h=9a80d7f1114a03a35f99e709f2ca7c8355750035;hb=cef378aaa2ce0a2e480d0fed4d86aaa80b4ad5af;hp=73f91a3d6841fde98da274fd73368cb84332ff76;hpb=c201f80faf4857b59b424c254f360524e50aa3ba;p=libftdi diff --git a/src/ftdi.c b/src/ftdi.c index 73f91a3..9a80d7f 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -35,12 +35,14 @@ #include "ftdi.h" /* stuff needed for async write */ -#include -#include -#include -#include -#include -#include +#ifdef LIBFTDI_LINUX_ASYNC_MODE + #include + #include + #include + #include + #include + #include +#endif #define ftdi_error_return(code, str) do { \ ftdi->error_str = str; \ @@ -83,6 +85,7 @@ int ftdi_init(struct ftdi_context *ftdi) ftdi->error_str = NULL; +#ifdef LIBFTDI_LINUX_ASYNC_MODE ftdi->async_usb_buffer_size=10; if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL) ftdi_error_return(-1, "out of memory for async usb buffer"); @@ -90,6 +93,10 @@ int ftdi_init(struct ftdi_context *ftdi) /* initialize async usb buffer with unused-marker */ for (i=0; i < ftdi->async_usb_buffer_size; i++) ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE; +#else + ftdi->async_usb_buffer_size=0; + ftdi->async_usb_buffer = NULL; +#endif ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE; @@ -98,6 +105,27 @@ int ftdi_init(struct ftdi_context *ftdi) } /** + Allocate and initialize a new ftdi_context + + \return a pointer to a new ftdi_context, or NULL on failure +*/ +struct ftdi_context *ftdi_new() +{ + struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context)); + + if (ftdi == NULL) { + return NULL; + } + + if (ftdi_init(ftdi) != 0) { + free(ftdi); + return NULL; + } + + return ftdi; +} + +/** Open selected channels on a chip, otherwise use first channel. \param ftdi pointer to ftdi_context @@ -144,6 +172,17 @@ void ftdi_deinit(struct ftdi_context *ftdi) } /** + Deinitialize and free an ftdi_context. + + \param ftdi pointer to ftdi_context +*/ +void ftdi_free(struct ftdi_context *ftdi) +{ + ftdi_deinit(ftdi); + free(ftdi); +} + +/** Use an already open libusb device. \param ftdi pointer to ftdi_context @@ -224,6 +263,16 @@ void ftdi_list_free(struct ftdi_device_list **devlist) } /** + Frees a usb device list. + + \param devlist USB device list created by ftdi_usb_find_all() +*/ +void ftdi_list_free2(struct ftdi_device_list *devlist) +{ + ftdi_list_free(&devlist); +} + +/** Return device ID strings from the usb device. The parameters manufacturer, description and serial may be NULL @@ -495,8 +544,10 @@ int ftdi_usb_close(struct ftdi_context *ftdi) { int rtn = 0; +#ifdef LIBFTDI_LINUX_ASYNC_MODE /* try to release some kernel resources */ ftdi_async_complete(ftdi,1); +#endif if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0) rtn = -1; @@ -731,6 +782,7 @@ int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) return total_written; } +#ifdef LIBFTDI_LINUX_ASYNC_MODE /* this is strongly dependent on libusb using the same struct layout. If libusb changes in some later version this may break horribly (this is for libusb 0.1.12) */ struct usb_dev_handle { @@ -738,7 +790,7 @@ struct usb_dev_handle { // some other stuff coming here we don't need }; -/* +/** Check for pending async urbs \internal */ @@ -757,8 +809,14 @@ static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi) return pending; } -/* - FIXME: Gerd, what does this function do exactly? +/** + Wait until one or more async URBs are completed by the kernel and mark their + positions in the async-buffer as unused + + \param ftdi pointer to ftdi_context + \param wait_for_more if != 0 wait for more than one write to complete + \param timeout_msec max milliseconds to wait + \internal */ static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec) @@ -806,7 +864,8 @@ static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int } /** - Wait until at least one async write is complete + Wait until one or more async URBs are completed by the kernel and mark their + positions in the async-buffer as unused. \param ftdi pointer to ftdi_context \param wait_for_more if != 0 wait for more than one write to complete (until write timeout) @@ -886,6 +945,7 @@ static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, caller of completion or error - but this is not done yet, volunteers welcome. Works around libusb and directly accesses functions only available on Linux. + Only available if compiled with --with-async-mode. \param ftdi pointer to ftdi_context \param buf Buffer with the data @@ -916,7 +976,7 @@ int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int siz return total_written; } - +#endif // LIBFTDI_LINUX_ASYNC_MODE /** Configure write buffer chunk size. @@ -1398,7 +1458,7 @@ int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) // Dynamic content i=0x14; if(eeprom->size>=256) i = 0x80; - + // Output manufacturer output[0x0E] = i | 0x80; // calculate offset