#include "ftdi.h"
/* stuff needed for async write */
-#include <sys/ioctl.h>
-#include <sys/time.h>
-#include <sys/select.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <linux/usbdevice_fs.h>
+#ifdef LIBFTDI_LINUX_ASYNC_MODE
+ #include <sys/ioctl.h>
+ #include <sys/time.h>
+ #include <sys/select.h>
+ #include <sys/types.h>
+ #include <unistd.h>
+ #include <linux/usbdevice_fs.h>
+#endif
#define ftdi_error_return(code, str) do { \
ftdi->error_str = str; \
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");
/* 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;
}
/**
+ 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
}
/**
+ 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
}
/**
+ 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
{
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;
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 {
// some other stuff coming here we don't need
};
-/*
+/**
Check for pending async urbs
\internal
*/
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)
}
/**
- 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)
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
return total_written;
}
-
+#endif // LIBFTDI_LINUX_ASYNC_MODE
/**
Configure write buffer chunk size.
// Dynamic content
i=0x14;
if(eeprom->size>=256) i = 0x80;
-
+
// Output manufacturer
output[0x0E] = i | 0x80; // calculate offset