25 #elif defined (_WIN32)
29 #error dl not supported
38 #warning libftd2xx support disabled.
47 #warning libftdi support disabled.
51 #error No known method to access FTDI chip
57 #define EXSID_INTERFACES "libftdi, libftd2xx" // XXX TODO Should be set by configure
60 static HMODULE dlhandle = NULL;
63 DWORD dwError = GetLastError();
64 char* lpMsgBuf = NULL;
65 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,
68 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
75 #define _xSfw_dlopen(libName) LoadLibrary(libName)
76 #define _xSfw_dlsym(hModule, lpProcName) GetProcAddress(hModule, lpProcName)
77 #define _xSfw_dlclose(hModule) FreeLibrary(hModule)
78 #define _xSfw_clear_dlerror() SetLastError(0)
79 #define _xSfw_free_errstr(str) LocalFree(str)
81 static void * dlhandle = NULL;
82 #define _xSfw_dlopen(filename) dlopen(filename, RTLD_NOW|RTLD_LOCAL)
83 #define _xSfw_dlsym(handle, symbol) dlsym(handle, symbol)
84 #define _xSfw_dlclose(handle) dlclose(handle)
85 #define _xSfw_dlerror() dlerror()
86 #define _xSfw_clear_dlerror() dlerror()
87 #define _xSfw_free_errstr(str)
101 static int (* _xSfw_set_baudrate)(
void *
ftdi,
int baudrate);
102 static int (* _xSfw_set_line_property)(
void *
ftdi,
int bits,
int sbit,
int parity);
103 static int (* _xSfw_setflowctrl)(
void *
ftdi,
int flowctrl);
104 static int (* _xSfw_set_latency_timer)(
void *
ftdi,
unsigned char latency);
108 static int (* _ftdi_usb_open_desc)(
void *, int, int,
const char *,
const char *);
113 static int (*_FT_Write)(
void *, LPVOID, int,
unsigned int *);
114 static int (*_FT_Read)(
void *, LPVOID, int,
unsigned int *);
115 static int (*_FT_OpenEx)(
const char *, int,
void **);
116 static int (*_FT_ResetDevice)(
void *);
117 static int (*_FT_SetBaudRate)(
void *, int);
118 static int (*_FT_SetDataCharacteristics)(
void *, int, int, int);
119 static int (*_FT_SetFlowControl)(
void *, int, int, int);
120 static int (*_FT_SetLatencyTimer)(
void *,
unsigned char);
121 static int (*_FT_Close)(
void *);
126 static int _xSfwftdi_usb_open_desc(
void **
ftdi,
int vid,
int pid,
const char *
desc,
const char * serial)
128 return _ftdi_usb_open_desc(*ftdi, vid, pid, desc, serial);
134 static int _xSfwFT_write_data(
void * restrict
ftdi,
const unsigned char * restrict buf,
int size)
138 if(
unlikely(rval = _FT_Write(ftdi, (LPVOID)buf, size, &dummysize)))
144 static int _xSfwFT_read_data(
void * restrict ftdi,
unsigned char * restrict buf,
int size)
148 if (
unlikely(rval = _FT_Read(ftdi, (LPVOID)buf, size, &dummysize)))
154 static int _xSfwFT_usb_open_desc(
void ** ftdi,
int vid,
int pid,
const char *
desc,
const char * serial)
156 return -_FT_OpenEx(desc, FT_OPEN_BY_DESCRIPTION, ftdi);
159 static int _xSfwFT_usb_close(
void * ftdi)
161 return -_FT_Close(ftdi);
164 static char * _xSfwFT_get_error_string(
void * ftdi)
166 return "FTD2XX error";
177 #define XSFW_DLSYM(a, b) \
178 *(void **)(&a) = _xSfw_dlsym(dlhandle, b); \
180 dlerrorstr = _xSfw_dlerror(); \
184 char * dlerrorstr = NULL;
187 xsdbg(
"recursive dlopen()!\n");
193 if ((dlhandle =
_xSfw_dlopen(TEXT(
"libftdi1" SHLIBEXT)))) {
199 XSFW_DLSYM(_ftdi_usb_open_desc,
"ftdi_usb_open_desc");
201 XSFW_DLSYM(_xSfw_set_baudrate,
"ftdi_set_baudrate");
202 XSFW_DLSYM(_xSfw_set_line_property,
"ftdi_set_line_property");
203 XSFW_DLSYM(_xSfw_setflowctrl,
"ftdi_setflowctrl");
204 XSFW_DLSYM(_xSfw_set_latency_timer,
"ftdi_set_latency_timer");
208 xsdbg(
"Using libftdi\n");
214 # define LIBFTD2XX "ftd2xx"
216 # define LIBFTD2XX "libftd2xx"
219 if ((dlhandle =
_xSfw_dlopen(TEXT(LIBFTD2XX SHLIBEXT)))) {
229 XSFW_DLSYM(_FT_ResetDevice,
"FT_ResetDevice");
230 XSFW_DLSYM(_FT_SetBaudRate,
"FT_SetBaudRate");
231 XSFW_DLSYM(_FT_SetDataCharacteristics,
"FT_SetDataCharacteristics");
232 XSFW_DLSYM(_FT_SetFlowControl,
"FT_SetFlowControl");
233 XSFW_DLSYM(_FT_SetLatencyTimer,
"FT_SetLatencyTimer");
238 xsdbg(
"Using libftd2xx\n");
244 xsdbg(
"No method found to access FTDI interface.\n"
245 "Are any of the following libraries installed?\n"
253 xsdbg(
"dlsym error: %s", dlerrorstr);
274 rval = _xSfw_set_baudrate(ftdi, baudrate);
279 rval = _xSfw_set_line_property(ftdi, BITS_8 , STOP_BIT_1, NONE);
284 rval = _xSfw_setflowctrl(ftdi, SIO_DISABLE_FLOW_CTRL);
289 rval = _xSfw_set_latency_timer(ftdi, latency);
299 rval = -_FT_ResetDevice(ftdi);
304 rval = -_FT_SetBaudRate(ftdi, baudrate);
309 rval = -_FT_SetDataCharacteristics(ftdi, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
314 rval = -_FT_SetFlowControl(ftdi, FT_FLOW_NONE, 0, 0);
319 rval = -_FT_SetLatencyTimer(ftdi, latency);
327 xsdbg(
"Unkown access method\n");
337 if (dlhandle != NULL) {
xSfw_write_data_p xSfw_write_data
Data write callback.
#define _xSfw_dlopen(filename)
int xSfw_dlopen()
Attempt to dlopen a known working library to access FTDI chip.
#define _xSfw_clear_dlerror()
FTDI access wrapper header file.
void * ftdi
FTDI device handle.
xSfw_read_data_p xSfw_read_data
Data read callback.
#define _xSfw_dlclose(handle)
xSfw_new_p xSfw_new
Handle allocation callback.
xSfw_usb_open_desc_p xSfw_usb_open_desc
Device open callback.
#define xsdbg(format,...)
#define _xSfw_free_errstr(str)
xSfw_get_error_string_p xSfw_get_error_string
Human readable error string callback.
int xSfw_usb_setup(void *ftdi, int baudrate, int latency)
Setup FTDI chip to match exSID firmware.
const char * desc
USB device description string.
libexsid private definitions header file.
libtype_t
Flag to signal which of the supported libraries is in use.
void xSfw_dlclose()
Release dlopen'd library.
xSfw_free_p xSfw_free
Handle deallocation callback.
xSfw_usb_close_p xSfw_usb_close
Device close callback.