├── README ├── INSTALL ├── .gitignore ├── AUTHORS ├── src ├── usb-wine.h ├── error.h ├── usb-wine.c ├── error.c ├── lsusb-wine.c ├── libusb0.spec ├── Makefile.x64 ├── Makefile ├── usbi.h ├── linux.h ├── usb.c ├── usb.h ├── descriptors.c └── linux.c ├── README.md └── COPYING /README: -------------------------------------------------------------------------------- 1 | README.md -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Run make install in src -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | src/*.o 2 | src/*.dll.so 3 | src/*.dll.fake 4 | *~ 5 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | libusb-win32 authors: 2 | 3 | Library, Test Programs: 4 | 5 | Stephan Meyer, 6 | Johannes Erdfelt, 7 | Thomas Sailer, 8 | 9 | Drivers, Installer: 10 | 11 | Stephan Meyer, 12 | Travis Robinson, 13 | 14 | Testing, Technical support: 15 | 16 | Xiaofan Chen, 17 | 18 | ported to wine: 19 | 20 | Andrew (Stanson) Kozin, 21 | 22 | Added bulk async functions as in libusb-win32: 23 | 24 | some person who'd like to remain anonymous 25 | -------------------------------------------------------------------------------- /src/usb-wine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Some stuff copied and adapted from libusb-win32 3 | * Stanson 4 | */ 5 | 6 | #ifndef __USB_WINE_H__ 7 | #define __USB_WINE_H__ 8 | 9 | #define VERSION_MAJOR 0 10 | #define VERSION_MINOR 1 11 | #define VERSION_MICRO 12 12 | #define VERSION_NANO 1 13 | 14 | /* Version information, Windows specific */ 15 | struct usb_version { 16 | struct { 17 | int major; 18 | int minor; 19 | int micro; 20 | int nano; 21 | } dll; 22 | struct { 23 | int major; 24 | int minor; 25 | int micro; 26 | int nano; 27 | } driver; 28 | }; 29 | 30 | #endif /* __USB_WINE_H__ */ 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is libusb-win32 for Wine. 2 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 3 | 4 | Some Windows applications use libusb-win32. This Wine DLL allows to run 5 | such Windows applications under Wine using native Linux USB stack. 6 | 7 | Prerequisites: Wine with winegcc and winebuild 8 | 9 | Building and installing: 10 | 11 | $ cd src 12 | $ make 13 | $ make install 14 | 15 | I didn't port following libusb-win32 functions to libusb-wine: 16 | 17 | * usb_install_service_np 18 | * usb_uninstall_service_np 19 | * usb_install_driver_np 20 | * usb_isochronous_setup_async 21 | * usb_interrupt_setup_async 22 | 23 | however, all Windows applications I tested with libusb-wine do not use them. 24 | -------------------------------------------------------------------------------- /src/error.h: -------------------------------------------------------------------------------- 1 | #ifndef _ERROR_H_ 2 | #define _ERROR_H_ 3 | 4 | typedef enum { 5 | USB_ERROR_TYPE_NONE = 0, 6 | USB_ERROR_TYPE_STRING, 7 | USB_ERROR_TYPE_ERRNO, 8 | } usb_error_type_t; 9 | 10 | extern char usb_error_str[1024]; 11 | extern int usb_error_errno; 12 | extern usb_error_type_t usb_error_type; 13 | 14 | #define USB_ERROR(x) \ 15 | do { \ 16 | usb_error_type = USB_ERROR_TYPE_ERRNO; \ 17 | usb_error_errno = x; \ 18 | return x; \ 19 | } while (0) 20 | 21 | #define USB_ERROR_STR(x, format, args...) \ 22 | do { \ 23 | usb_error_type = USB_ERROR_TYPE_STRING; \ 24 | snprintf(usb_error_str, sizeof(usb_error_str) - 1, format, ## args); \ 25 | if (usb_debug >= 2) \ 26 | fprintf(stderr, "USB error: %s\n", usb_error_str); \ 27 | return x; \ 28 | } while (0) 29 | 30 | #endif /* _ERROR_H_ */ 31 | 32 | -------------------------------------------------------------------------------- /src/usb-wine.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Some stuff copied and adapted from libusb-win32 3 | * Stanson 4 | */ 5 | 6 | #include 7 | #include "usb-wine.h" 8 | 9 | static struct usb_version _usb_version = { 10 | { VERSION_MAJOR, 11 | VERSION_MINOR, 12 | VERSION_MICRO, 13 | VERSION_NANO }, 14 | { -1, -1, -1, -1 } 15 | }; 16 | 17 | const struct usb_version *usb_get_version(void) 18 | { 19 | return &_usb_version; 20 | } 21 | 22 | /* DLL main entry point */ 23 | BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID reserved) 24 | { 25 | switch(reason) 26 | { 27 | case DLL_PROCESS_ATTACH: 28 | break; 29 | case DLL_PROCESS_DETACH: 30 | break; 31 | case DLL_THREAD_ATTACH: 32 | break; 33 | case DLL_THREAD_DETACH: 34 | break; 35 | default: 36 | break; 37 | } 38 | return TRUE; 39 | } 40 | -------------------------------------------------------------------------------- /src/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * USB Error messages 3 | * 4 | * Copyright (c) 2000-2001 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #include "usb.h" 13 | #include "error.h" 14 | 15 | char usb_error_str[1024] = ""; 16 | int usb_error_errno = 0; 17 | usb_error_type_t usb_error_type = USB_ERROR_TYPE_NONE; 18 | 19 | const char *usb_strerror(void) 20 | { 21 | switch (usb_error_type) { 22 | case USB_ERROR_TYPE_NONE: 23 | return "No error"; 24 | case USB_ERROR_TYPE_STRING: 25 | return usb_error_str; 26 | case USB_ERROR_TYPE_ERRNO: 27 | if (usb_error_errno > -USB_ERROR_BEGIN) 28 | return strerror(usb_error_errno); 29 | else 30 | /* Any error we don't know falls under here */ 31 | return "Unknown error"; 32 | } 33 | 34 | return "Unknown error"; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/lsusb-wine.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Test utility for libusb-wine. 3 | * compile with winegcc -o lsusb-wine lsusb-wine.c -lusb0 4 | * run as ./lsusb-wine.exe, it should list VID/PIDs of connected USB devices 5 | */ 6 | 7 | #include 8 | #include "usb.h" 9 | 10 | int main() 11 | { 12 | struct usb_bus *usbbus; 13 | struct usb_device *usbdev; 14 | 15 | usb_init(); 16 | if( usb_find_busses() < 0 ) 17 | { 18 | printf( "Can't find busses\n" ); 19 | return 1; 20 | } 21 | 22 | if( usb_find_devices() < 0 ) 23 | { 24 | printf( "Can't find devices\n" ); 25 | return 1; 26 | } 27 | 28 | usbbus = usb_get_busses(); 29 | for( ; usbbus; usbbus = usbbus->next ) 30 | { 31 | for( usbdev = usbbus->devices; usbdev; usbdev = usbdev->next ) 32 | { 33 | printf( "%04X:%04X\n", usbdev->descriptor.idVendor, usbdev->descriptor.idProduct ); 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /src/libusb0.spec: -------------------------------------------------------------------------------- 1 | @ cdecl usb_open (ptr) 2 | @ cdecl usb_close (ptr) 3 | @ cdecl usb_get_string (ptr long long str long) 4 | @ cdecl usb_get_string_simple (ptr long str long) 5 | @ cdecl usb_get_descriptor_by_endpoint (ptr long long long ptr long) 6 | @ cdecl usb_get_descriptor (ptr long long ptr long) 7 | @ cdecl usb_bulk_write (ptr long str long long) 8 | @ cdecl usb_bulk_read (ptr long str long long) 9 | @ cdecl usb_interrupt_write (ptr long str long long) 10 | @ cdecl usb_interrupt_read (ptr long str long long) 11 | @ cdecl usb_control_msg (ptr long long long long str long long) 12 | @ cdecl usb_set_configuration (ptr long) 13 | @ cdecl usb_claim_interface (ptr long) 14 | @ cdecl usb_release_interface (ptr long) 15 | @ cdecl usb_set_altinterface (ptr long) 16 | @ cdecl usb_resetep (ptr long) 17 | @ cdecl usb_clear_halt (ptr long) 18 | @ cdecl usb_reset (ptr) 19 | @ cdecl usb_strerror () 20 | @ cdecl usb_init () 21 | @ cdecl usb_set_debug (long) 22 | @ cdecl usb_find_busses () 23 | @ cdecl usb_find_devices () 24 | @ cdecl usb_device (ptr) 25 | @ cdecl usb_get_busses () 26 | @ cdecl usb_get_version () 27 | @ cdecl usb_bulk_setup_async (ptr ptr long) 28 | @ cdecl usb_submit_async (ptr ptr long) 29 | @ cdecl usb_reap_async (ptr long) 30 | @ cdecl usb_free_async (ptr) 31 | @ cdecl usb_cancel_async (ptr) 32 | -------------------------------------------------------------------------------- /src/Makefile.x64: -------------------------------------------------------------------------------- 1 | all: 2 | 3 | PROJ = libusb0 4 | 5 | CFLAGS = -g -O2 \ 6 | -fPIC \ 7 | -Wall \ 8 | -pipe \ 9 | -fno-strict-aliasing \ 10 | -Wempty-body \ 11 | -Wignored-qualifiers \ 12 | -Wstrict-prototypes \ 13 | -Wtype-limits \ 14 | -Wunused-but-set-parameter \ 15 | -Wvla \ 16 | -Wwrite-strings \ 17 | -Wpointer-arith \ 18 | -Wlogical-op \ 19 | -Wno-format-truncation \ 20 | -gdwarf-2 \ 21 | -gstrict-dwarf \ 22 | -fno-omit-frame-pointer 23 | 24 | LDFLAGS = 25 | 26 | OBJS = linux.o error.o usb.o descriptors.o usb-wine.o 27 | 28 | WINE_INCLUDE = /usr/include/wine/wine/windows 29 | 30 | CC = gcc 31 | WINEGCC = winegcc 32 | WINEBUILD = winebuild 33 | 34 | dlldir = /usr/lib/x86_64-linux-gnu/wine 35 | fakedlldir = $(dlldir)/fakedlls 36 | 37 | $(OBJS):%.o: %.c 38 | $(CC) -c -o $@ $^ -I. -I$(WINE_INCLUDE) -D__WINESRC__ -D_REENTRANT $(CFLAGS) 39 | 40 | $(PROJ).dll.so $(PROJ).dll.fake: $(PROJ).spec $(OBJS) 41 | $(WINEGCC) -o $@ -B$(WINEBUILD) -fasynchronous-unwind-tables -shared $(PROJ).spec \ 42 | $(OBJS) $(LDFLAGS) 43 | 44 | $(PROJ).def: $(PROJ).spec 45 | $(WINEBUILD) -w --def -o $@ --export $(PROJ).spec 46 | 47 | $(PROJ).cross.a: $(PROJ).spec 48 | $(WINEBUILD) -w --implib -o $@ --export $(PROJ).spec 49 | 50 | all: $(PROJ).dll.so $(PROJ).dll.fake 51 | 52 | install install-lib:: $(PROJ).dll.so $(PROJ).dll.fake 53 | cp $(PROJ).dll.so $(DESTDIR)$(dlldir)/$(PROJ).dll.so 54 | cp $(PROJ).dll.fake $(DESTDIR)$(fakedlldir)/$(PROJ).dll 55 | 56 | install install-dev:: $(PROJ).def 57 | cp $(PROJ).def $(DESTDIR)$(dlldir)/$(PROJ).def 58 | 59 | uninstall:: 60 | rm -f $(DESTDIR)$(dlldir)/$(PROJ).dll.so $(DESTDIR)$(fakedlldir)/$(PROJ).dll \ 61 | $(DESTDIR)$(dlldir)/$(PROJ).def 62 | 63 | clean:: 64 | rm -f *.def *.a *.o *.so *.fake 65 | 66 | .PHONY: all install install-lib install-dev uninstall clean 67 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | 3 | PROJ = libusb0 4 | 5 | CFLAGS = -g -O2 -m32 \ 6 | -fPIC \ 7 | -Wall \ 8 | -pipe \ 9 | -fno-strict-aliasing \ 10 | -Wempty-body \ 11 | -Wignored-qualifiers \ 12 | -Wstrict-prototypes \ 13 | -Wtype-limits \ 14 | -Wunused-but-set-parameter \ 15 | -Wvla \ 16 | -Wwrite-strings \ 17 | -Wpointer-arith \ 18 | -Wlogical-op \ 19 | -Wno-format-truncation \ 20 | -gdwarf-2 \ 21 | -gstrict-dwarf \ 22 | -fno-omit-frame-pointer 23 | 24 | LDFLAGS = -m32 25 | 26 | OBJS = linux.o error.o usb.o descriptors.o usb-wine.o 27 | 28 | WINE_INCLUDE = /usr/include/wine/windows 29 | 30 | CC = gcc 31 | WINEGCC = winegcc 32 | WINEBUILD = winebuild 33 | 34 | dlldir = /usr/lib/wine 35 | fakedlldir = $(dlldir)/fakedlls 36 | 37 | $(OBJS):%.o: %.c 38 | $(CC) -c -o $@ $^ -I. -I$(WINE_INCLUDE) -D__WINESRC__ -D_REENTRANT $(CFLAGS) 39 | 40 | $(PROJ).dll.so $(PROJ).dll.fake: $(PROJ).spec $(OBJS) 41 | $(WINEGCC) -o $@ -B$(WINEBUILD) -fasynchronous-unwind-tables -shared $(PROJ).spec \ 42 | $(OBJS) $(LDFLAGS) 43 | 44 | $(PROJ).def: $(PROJ).spec 45 | $(WINEBUILD) -w --def -o $@ --export $(PROJ).spec 46 | 47 | $(PROJ).cross.a: $(PROJ).spec 48 | $(WINEBUILD) -b i386-mingw32 -w --implib -o $@ --export $(PROJ).spec 49 | 50 | all: $(PROJ).dll.so $(PROJ).dll.fake 51 | 52 | install install-lib:: $(PROJ).dll.so $(PROJ).dll.fake 53 | cp $(PROJ).dll.so $(DESTDIR)$(dlldir)/$(PROJ).dll.so 54 | cp $(PROJ).dll.fake $(DESTDIR)$(fakedlldir)/$(PROJ).dll 55 | 56 | install install-dev:: $(PROJ).def 57 | cp $(PROJ).def $(DESTDIR)$(dlldir)/$(PROJ).def 58 | 59 | uninstall:: 60 | rm -f $(DESTDIR)$(dlldir)/$(PROJ).dll.so $(DESTDIR)$(fakedlldir)/$(PROJ).dll \ 61 | $(DESTDIR)$(dlldir)/$(PROJ).def 62 | 63 | clean:: 64 | rm -f *.def *.a *.o *.so *.fake *.exe 65 | 66 | .PHONY: all install install-lib install-dev uninstall clean 67 | -------------------------------------------------------------------------------- /src/usbi.h: -------------------------------------------------------------------------------- 1 | #ifndef _USBI_H_ 2 | #define _USBI_H_ 3 | 4 | #include "usb.h" 5 | 6 | #include "error.h" 7 | 8 | extern int usb_debug; 9 | 10 | /* Some quick and generic macros for the simple kind of lists we use */ 11 | #define LIST_ADD(begin, ent) \ 12 | do { \ 13 | if (begin) { \ 14 | ent->next = begin; \ 15 | ent->next->prev = ent; \ 16 | } else \ 17 | ent->next = NULL; \ 18 | ent->prev = NULL; \ 19 | begin = ent; \ 20 | } while(0) 21 | 22 | #define LIST_DEL(begin, ent) \ 23 | do { \ 24 | if (ent->prev) \ 25 | ent->prev->next = ent->next; \ 26 | else \ 27 | begin = ent->next; \ 28 | if (ent->next) \ 29 | ent->next->prev = ent->prev; \ 30 | ent->prev = NULL; \ 31 | ent->next = NULL; \ 32 | } while (0) 33 | 34 | #define DESC_HEADER_LENGTH 2 35 | #define DEVICE_DESC_LENGTH 18 36 | #define CONFIG_DESC_LENGTH 9 37 | #define INTERFACE_DESC_LENGTH 9 38 | #define ENDPOINT_DESC_LENGTH 7 39 | #define ENDPOINT_AUDIO_DESC_LENGTH 9 40 | 41 | struct usb_dev_handle { 42 | int fd; 43 | 44 | struct usb_bus *bus; 45 | struct usb_device *device; 46 | 47 | int config; 48 | int interface; 49 | int altsetting; 50 | 51 | /* Added by RMT so implementations can store other per-open-device data */ 52 | void *impl_info; 53 | }; 54 | 55 | /* descriptors.c */ 56 | int usb_parse_descriptor(unsigned char *source, const char *description, void *dest); 57 | int usb_parse_configuration(struct usb_config_descriptor *config, 58 | unsigned char *buffer); 59 | void usb_fetch_and_parse_descriptors(usb_dev_handle *udev); 60 | void usb_destroy_configuration(struct usb_device *dev); 61 | 62 | /* OS specific routines */ 63 | int usb_os_find_busses(struct usb_bus **busses); 64 | int usb_os_find_devices(struct usb_bus *bus, struct usb_device **devices); 65 | int usb_os_determine_children(struct usb_bus *bus); 66 | void usb_os_init(void); 67 | int usb_os_open(usb_dev_handle *dev); 68 | int usb_os_close(usb_dev_handle *dev); 69 | 70 | void usb_free_dev(struct usb_device *dev); 71 | void usb_free_bus(struct usb_bus *bus); 72 | 73 | #endif /* _USBI_H_ */ 74 | 75 | -------------------------------------------------------------------------------- /src/linux.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_H__ 2 | #define __LINUX_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct usb_ctrltransfer { 9 | /* keep in sync with usbdevice_fs.h:usbdevfs_ctrltransfer */ 10 | u_int8_t bRequestType; 11 | u_int8_t bRequest; 12 | u_int16_t wValue; 13 | u_int16_t wIndex; 14 | u_int16_t wLength; 15 | 16 | u_int32_t timeout; /* in milliseconds */ 17 | 18 | /* pointer to data */ 19 | void *data; 20 | }; 21 | 22 | struct usb_bulktransfer { 23 | /* keep in sync with usbdevice_fs.h:usbdevfs_bulktransfer */ 24 | unsigned int ep; 25 | unsigned int len; 26 | unsigned int timeout; /* in milliseconds */ 27 | 28 | /* pointer to data */ 29 | void *data; 30 | }; 31 | 32 | struct usb_setinterface { 33 | /* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */ 34 | unsigned int interface; 35 | unsigned int altsetting; 36 | }; 37 | 38 | #define USB_MAXDRIVERNAME 255 39 | 40 | struct usb_getdriver { 41 | unsigned int interface; 42 | char driver[USB_MAXDRIVERNAME + 1]; 43 | }; 44 | 45 | #define USB_URB_DISABLE_SPD 1 46 | #define USB_URB_ISO_ASAP 2 47 | #define USB_URB_QUEUE_BULK 0x10 48 | 49 | #define USB_URB_TYPE_ISO 0 50 | #define USB_URB_TYPE_INTERRUPT 1 51 | #define USB_URB_TYPE_CONTROL 2 52 | #define USB_URB_TYPE_BULK 3 53 | 54 | struct usb_iso_packet_desc { 55 | unsigned int length; 56 | unsigned int actual_length; 57 | unsigned int status; 58 | }; 59 | 60 | struct usb_urb { 61 | unsigned char type; 62 | unsigned char endpoint; 63 | int status; 64 | unsigned int flags; 65 | void *buffer; 66 | int buffer_length; 67 | int actual_length; 68 | int start_frame; 69 | int number_of_packets; 70 | int error_count; 71 | unsigned int signr; /* signal to be sent on error, -1 if none should be sent */ 72 | void *usercontext; 73 | struct usb_iso_packet_desc iso_frame_desc[0]; 74 | }; 75 | 76 | struct usb_connectinfo { 77 | unsigned int devnum; 78 | unsigned char slow; 79 | }; 80 | 81 | struct usb_ioctl { 82 | int ifno; /* interface 0..N ; negative numbers reserved */ 83 | int ioctl_code; /* MUST encode size + direction of data so the 84 | * macros in give correct values */ 85 | void *data; /* param buffer (in, or out) */ 86 | }; 87 | 88 | struct usb_hub_portinfo { 89 | unsigned char numports; 90 | unsigned char port[127]; /* port to device num mapping */ 91 | }; 92 | 93 | #define IOCTL_USB_CONTROL _IOWR('U', 0, struct usb_ctrltransfer) 94 | #define IOCTL_USB_BULK _IOWR('U', 2, struct usb_bulktransfer) 95 | #define IOCTL_USB_RESETEP _IOR('U', 3, unsigned int) 96 | #define IOCTL_USB_SETINTF _IOR('U', 4, struct usb_setinterface) 97 | #define IOCTL_USB_SETCONFIG _IOR('U', 5, unsigned int) 98 | #define IOCTL_USB_GETDRIVER _IOW('U', 8, struct usb_getdriver) 99 | #define IOCTL_USB_SUBMITURB _IOR('U', 10, struct usb_urb) 100 | #define IOCTL_USB_DISCARDURB _IO('U', 11) 101 | #define IOCTL_USB_REAPURB _IOW('U', 12, void *) 102 | #define IOCTL_USB_REAPURBNDELAY _IOW('U', 13, void *) 103 | #define IOCTL_USB_CLAIMINTF _IOR('U', 15, unsigned int) 104 | #define IOCTL_USB_RELEASEINTF _IOR('U', 16, unsigned int) 105 | #define IOCTL_USB_CONNECTINFO _IOW('U', 17, struct usb_connectinfo) 106 | #define IOCTL_USB_IOCTL _IOWR('U', 18, struct usb_ioctl) 107 | #define IOCTL_USB_HUB_PORTINFO _IOR('U', 19, struct usb_hub_portinfo) 108 | #define IOCTL_USB_RESET _IO('U', 20) 109 | #define IOCTL_USB_CLEAR_HALT _IOR('U', 21, unsigned int) 110 | #define IOCTL_USB_DISCONNECT _IO('U', 22) 111 | #define IOCTL_USB_CONNECT _IO('U', 23) 112 | 113 | /* 114 | * IOCTL_USB_HUB_PORTINFO, IOCTL_USB_DISCONNECT and IOCTL_USB_CONNECT 115 | * all work via IOCTL_USB_IOCTL 116 | */ 117 | 118 | #endif 119 | 120 | -------------------------------------------------------------------------------- /src/usb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Main API entry point 3 | * 4 | * Copyright (c) 2000-2003 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | */ 8 | 9 | #include /* getenv */ 10 | #include /* stderr */ 11 | #include /* strcmp */ 12 | #include 13 | 14 | #include "usbi.h" 15 | 16 | int usb_debug = 0; 17 | struct usb_bus *usb_busses = NULL; 18 | 19 | int usb_find_busses(void) 20 | { 21 | struct usb_bus *busses, *bus; 22 | int ret, changes = 0; 23 | 24 | ret = usb_os_find_busses(&busses); 25 | if (ret < 0) 26 | return ret; 27 | 28 | /* 29 | * Now walk through all of the busses we know about and compare against 30 | * this new list. Any duplicates will be removed from the new list. 31 | * If we don't find it in the new list, the bus was removed. Any 32 | * busses still in the new list, are new to us. 33 | */ 34 | bus = usb_busses; 35 | while (bus) { 36 | int found = 0; 37 | struct usb_bus *nbus, *tbus = bus->next; 38 | 39 | nbus = busses; 40 | while (nbus) { 41 | struct usb_bus *tnbus = nbus->next; 42 | 43 | if (!strcmp(bus->dirname, nbus->dirname)) { 44 | /* Remove it from the new busses list */ 45 | LIST_DEL(busses, nbus); 46 | 47 | usb_free_bus(nbus); 48 | found = 1; 49 | break; 50 | } 51 | 52 | nbus = tnbus; 53 | } 54 | 55 | if (!found) { 56 | /* The bus was removed from the system */ 57 | LIST_DEL(usb_busses, bus); 58 | usb_free_bus(bus); 59 | changes++; 60 | } 61 | 62 | bus = tbus; 63 | } 64 | 65 | /* 66 | * Anything on the *busses list is new. So add them to usb_busses and 67 | * process them like the new bus it is. 68 | */ 69 | bus = busses; 70 | while (bus) { 71 | struct usb_bus *tbus = bus->next; 72 | 73 | /* 74 | * Remove it from the temporary list first and add it to the real 75 | * usb_busses list. 76 | */ 77 | LIST_DEL(busses, bus); 78 | 79 | LIST_ADD(usb_busses, bus); 80 | 81 | changes++; 82 | 83 | bus = tbus; 84 | } 85 | 86 | return changes; 87 | } 88 | 89 | int usb_find_devices(void) 90 | { 91 | struct usb_bus *bus; 92 | int ret, changes = 0; 93 | 94 | for (bus = usb_busses; bus; bus = bus->next) { 95 | struct usb_device *devices, *dev; 96 | 97 | /* Find all of the devices and put them into a temporary list */ 98 | ret = usb_os_find_devices(bus, &devices); 99 | if (ret < 0) 100 | return ret; 101 | 102 | /* 103 | * Now walk through all of the devices we know about and compare 104 | * against this new list. Any duplicates will be removed from the new 105 | * list. If we don't find it in the new list, the device was removed. 106 | * Any devices still in the new list, are new to us. 107 | */ 108 | dev = bus->devices; 109 | while (dev) { 110 | int found = 0; 111 | struct usb_device *ndev, *tdev = dev->next; 112 | 113 | ndev = devices; 114 | while (ndev) { 115 | struct usb_device *tndev = ndev->next; 116 | 117 | if (!strcmp(dev->filename, ndev->filename)) { 118 | /* Remove it from the new devices list */ 119 | LIST_DEL(devices, ndev); 120 | 121 | usb_free_dev(ndev); 122 | found = 1; 123 | break; 124 | } 125 | 126 | ndev = tndev; 127 | } 128 | 129 | if (!found) { 130 | /* The device was removed from the system */ 131 | LIST_DEL(bus->devices, dev); 132 | usb_free_dev(dev); 133 | changes++; 134 | } 135 | 136 | dev = tdev; 137 | } 138 | 139 | /* 140 | * Anything on the *devices list is new. So add them to bus->devices and 141 | * process them like the new device it is. 142 | */ 143 | dev = devices; 144 | while (dev) { 145 | struct usb_device *tdev = dev->next; 146 | 147 | /* 148 | * Remove it from the temporary list first and add it to the real 149 | * bus->devices list. 150 | */ 151 | LIST_DEL(devices, dev); 152 | 153 | LIST_ADD(bus->devices, dev); 154 | 155 | /* 156 | * Some ports fetch the descriptors on scanning (like Linux) so we don't 157 | * need to fetch them again. 158 | */ 159 | if (!dev->config) { 160 | usb_dev_handle *udev; 161 | 162 | udev = usb_open(dev); 163 | if (udev) { 164 | usb_fetch_and_parse_descriptors(udev); 165 | 166 | usb_close(udev); 167 | } 168 | } 169 | 170 | changes++; 171 | 172 | dev = tdev; 173 | } 174 | 175 | usb_os_determine_children(bus); 176 | } 177 | 178 | return changes; 179 | } 180 | 181 | void usb_set_debug(int level) 182 | { 183 | if (usb_debug || level) 184 | fprintf(stderr, "usb_set_debug: Setting debugging level to %d (%s)\n", 185 | level, level ? "on" : "off"); 186 | 187 | usb_debug = level; 188 | } 189 | 190 | void usb_init(void) 191 | { 192 | if (getenv("USB_DEBUG")) 193 | usb_set_debug(atoi(getenv("USB_DEBUG"))); 194 | 195 | usb_os_init(); 196 | } 197 | 198 | usb_dev_handle *usb_open(struct usb_device *dev) 199 | { 200 | usb_dev_handle *udev; 201 | 202 | udev = malloc(sizeof(*udev)); 203 | if (!udev) 204 | return NULL; 205 | 206 | udev->fd = -1; 207 | udev->device = dev; 208 | udev->bus = dev->bus; 209 | udev->config = udev->interface = udev->altsetting = -1; 210 | 211 | if (usb_os_open(udev) < 0) { 212 | free(udev); 213 | return NULL; 214 | } 215 | 216 | return udev; 217 | } 218 | 219 | int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 220 | size_t buflen) 221 | { 222 | /* 223 | * We can't use usb_get_descriptor() because it's lacking the index 224 | * parameter. This will be fixed in libusb 1.0 225 | */ 226 | return usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 227 | (USB_DT_STRING << 8) + index, langid, buf, buflen, 1000); 228 | } 229 | 230 | int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen) 231 | { 232 | char tbuf[255]; /* Some devices choke on size > 255 */ 233 | int ret, langid, si, di; 234 | 235 | /* 236 | * Asking for the zero'th index is special - it returns a string 237 | * descriptor that contains all the language IDs supported by the 238 | * device. Typically there aren't many - often only one. The 239 | * language IDs are 16 bit numbers, and they start at the third byte 240 | * in the descriptor. See USB 2.0 specification, section 9.6.7, for 241 | * more information on this. */ 242 | ret = usb_get_string(dev, 0, 0, tbuf, sizeof(tbuf)); 243 | if (ret < 0) 244 | return ret; 245 | 246 | if (ret < 4) 247 | return -EIO; 248 | 249 | langid = tbuf[2] | (tbuf[3] << 8); 250 | 251 | ret = usb_get_string(dev, index, langid, tbuf, sizeof(tbuf)); 252 | if (ret < 0) 253 | return ret; 254 | 255 | if (tbuf[1] != USB_DT_STRING) 256 | return -EIO; 257 | 258 | if (tbuf[0] > ret) 259 | return -EFBIG; 260 | 261 | for (di = 0, si = 2; si < tbuf[0]; si += 2) { 262 | if (di >= (buflen - 1)) 263 | break; 264 | 265 | if (tbuf[si + 1]) /* high byte */ 266 | buf[di++] = '?'; 267 | else 268 | buf[di++] = tbuf[si]; 269 | } 270 | 271 | buf[di] = 0; 272 | 273 | return di; 274 | } 275 | 276 | int usb_close(usb_dev_handle *dev) 277 | { 278 | int ret; 279 | 280 | ret = usb_os_close(dev); 281 | free(dev); 282 | 283 | return ret; 284 | } 285 | 286 | struct usb_device *usb_device(usb_dev_handle *dev) 287 | { 288 | return dev->device; 289 | } 290 | 291 | void usb_free_dev(struct usb_device *dev) 292 | { 293 | usb_destroy_configuration(dev); 294 | free(dev->children); 295 | free(dev); 296 | } 297 | 298 | struct usb_bus *usb_get_busses(void) 299 | { 300 | return usb_busses; 301 | } 302 | 303 | void usb_free_bus(struct usb_bus *bus) 304 | { 305 | free(bus); 306 | } 307 | 308 | -------------------------------------------------------------------------------- /src/usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Prototypes, structure definitions and macros. 3 | * 4 | * Copyright (c) 2000-2003 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | * 8 | * This file (and only this file) may alternatively be licensed under the 9 | * BSD license as well, read LICENSE for details. 10 | */ 11 | #ifndef __USB_H__ 12 | #define __USB_H__ 13 | 14 | #include 15 | 16 | /* 17 | * USB spec information 18 | * 19 | * This is all stuff grabbed from various USB specs and is pretty much 20 | * not subject to change 21 | */ 22 | 23 | /* 24 | * PATH_MAX from limits.h can't be used on Windows if the dll and 25 | * import libraries are build/used by different compilers 26 | */ 27 | 28 | #define LIBUSB_PATH_MAX 512 29 | 30 | 31 | /* 32 | * Device and/or Interface Class codes 33 | */ 34 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ 35 | #define USB_CLASS_AUDIO 1 36 | #define USB_CLASS_COMM 2 37 | #define USB_CLASS_HID 3 38 | #define USB_CLASS_PRINTER 7 39 | #define USB_CLASS_PTP 6 40 | #define USB_CLASS_MASS_STORAGE 8 41 | #define USB_CLASS_HUB 9 42 | #define USB_CLASS_DATA 10 43 | #define USB_CLASS_VENDOR_SPEC 0xff 44 | 45 | /* 46 | * Descriptor types 47 | */ 48 | #define USB_DT_DEVICE 0x01 49 | #define USB_DT_CONFIG 0x02 50 | #define USB_DT_STRING 0x03 51 | #define USB_DT_INTERFACE 0x04 52 | #define USB_DT_ENDPOINT 0x05 53 | 54 | #define USB_DT_HID 0x21 55 | #define USB_DT_REPORT 0x22 56 | #define USB_DT_PHYSICAL 0x23 57 | #define USB_DT_HUB 0x29 58 | 59 | /* 60 | * Descriptor sizes per descriptor type 61 | */ 62 | #define USB_DT_DEVICE_SIZE 18 63 | #define USB_DT_CONFIG_SIZE 9 64 | #define USB_DT_INTERFACE_SIZE 9 65 | #define USB_DT_ENDPOINT_SIZE 7 66 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 67 | #define USB_DT_HUB_NONVAR_SIZE 7 68 | 69 | /* ensure byte-packed structures like libusb-win32 ( Stanson ) */ 70 | #pragma pack(push,1) 71 | 72 | /* All standard descriptors have these 2 fields in common */ 73 | struct usb_descriptor_header { 74 | u_int8_t bLength; 75 | u_int8_t bDescriptorType; 76 | }; 77 | 78 | /* String descriptor */ 79 | struct usb_string_descriptor { 80 | u_int8_t bLength; 81 | u_int8_t bDescriptorType; 82 | u_int16_t wData[1]; 83 | }; 84 | 85 | /* HID descriptor */ 86 | struct usb_hid_descriptor { 87 | u_int8_t bLength; 88 | u_int8_t bDescriptorType; 89 | u_int16_t bcdHID; 90 | u_int8_t bCountryCode; 91 | u_int8_t bNumDescriptors; 92 | /* u_int8_t bReportDescriptorType; */ 93 | /* u_int16_t wDescriptorLength; */ 94 | /* ... */ 95 | }; 96 | 97 | /* Endpoint descriptor */ 98 | #define USB_MAXENDPOINTS 32 99 | struct usb_endpoint_descriptor { 100 | u_int8_t bLength; 101 | u_int8_t bDescriptorType; 102 | u_int8_t bEndpointAddress; 103 | u_int8_t bmAttributes; 104 | u_int16_t wMaxPacketSize; 105 | u_int8_t bInterval; 106 | u_int8_t bRefresh; 107 | u_int8_t bSynchAddress; 108 | 109 | unsigned char *extra; /* Extra descriptors */ 110 | int extralen; 111 | }; 112 | 113 | #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 114 | #define USB_ENDPOINT_DIR_MASK 0x80 115 | 116 | #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ 117 | #define USB_ENDPOINT_TYPE_CONTROL 0 118 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 119 | #define USB_ENDPOINT_TYPE_BULK 2 120 | #define USB_ENDPOINT_TYPE_INTERRUPT 3 121 | 122 | /* Interface descriptor */ 123 | #define USB_MAXINTERFACES 32 124 | struct usb_interface_descriptor { 125 | u_int8_t bLength; 126 | u_int8_t bDescriptorType; 127 | u_int8_t bInterfaceNumber; 128 | u_int8_t bAlternateSetting; 129 | u_int8_t bNumEndpoints; 130 | u_int8_t bInterfaceClass; 131 | u_int8_t bInterfaceSubClass; 132 | u_int8_t bInterfaceProtocol; 133 | u_int8_t iInterface; 134 | 135 | struct usb_endpoint_descriptor *endpoint; 136 | 137 | unsigned char *extra; /* Extra descriptors */ 138 | int extralen; 139 | }; 140 | 141 | #define USB_MAXALTSETTING 128 /* Hard limit */ 142 | struct usb_interface { 143 | struct usb_interface_descriptor *altsetting; 144 | 145 | int num_altsetting; 146 | }; 147 | 148 | /* Configuration descriptor information.. */ 149 | #define USB_MAXCONFIG 8 150 | struct usb_config_descriptor { 151 | u_int8_t bLength; 152 | u_int8_t bDescriptorType; 153 | u_int16_t wTotalLength; 154 | u_int8_t bNumInterfaces; 155 | u_int8_t bConfigurationValue; 156 | u_int8_t iConfiguration; 157 | u_int8_t bmAttributes; 158 | u_int8_t MaxPower; 159 | 160 | struct usb_interface *interface; 161 | 162 | unsigned char *extra; /* Extra descriptors */ 163 | int extralen; 164 | }; 165 | 166 | /* Device descriptor */ 167 | struct usb_device_descriptor { 168 | u_int8_t bLength; 169 | u_int8_t bDescriptorType; 170 | u_int16_t bcdUSB; 171 | u_int8_t bDeviceClass; 172 | u_int8_t bDeviceSubClass; 173 | u_int8_t bDeviceProtocol; 174 | u_int8_t bMaxPacketSize0; 175 | u_int16_t idVendor; 176 | u_int16_t idProduct; 177 | u_int16_t bcdDevice; 178 | u_int8_t iManufacturer; 179 | u_int8_t iProduct; 180 | u_int8_t iSerialNumber; 181 | u_int8_t bNumConfigurations; 182 | }; 183 | 184 | struct usb_ctrl_setup { 185 | u_int8_t bRequestType; 186 | u_int8_t bRequest; 187 | u_int16_t wValue; 188 | u_int16_t wIndex; 189 | u_int16_t wLength; 190 | }; 191 | 192 | /* 193 | * Standard requests 194 | */ 195 | #define USB_REQ_GET_STATUS 0x00 196 | #define USB_REQ_CLEAR_FEATURE 0x01 197 | /* 0x02 is reserved */ 198 | #define USB_REQ_SET_FEATURE 0x03 199 | /* 0x04 is reserved */ 200 | #define USB_REQ_SET_ADDRESS 0x05 201 | #define USB_REQ_GET_DESCRIPTOR 0x06 202 | #define USB_REQ_SET_DESCRIPTOR 0x07 203 | #define USB_REQ_GET_CONFIGURATION 0x08 204 | #define USB_REQ_SET_CONFIGURATION 0x09 205 | #define USB_REQ_GET_INTERFACE 0x0A 206 | #define USB_REQ_SET_INTERFACE 0x0B 207 | #define USB_REQ_SYNCH_FRAME 0x0C 208 | 209 | #define USB_TYPE_STANDARD (0x00 << 5) 210 | #define USB_TYPE_CLASS (0x01 << 5) 211 | #define USB_TYPE_VENDOR (0x02 << 5) 212 | #define USB_TYPE_RESERVED (0x03 << 5) 213 | 214 | #define USB_RECIP_DEVICE 0x00 215 | #define USB_RECIP_INTERFACE 0x01 216 | #define USB_RECIP_ENDPOINT 0x02 217 | #define USB_RECIP_OTHER 0x03 218 | 219 | /* 220 | * Various libusb API related stuff 221 | */ 222 | 223 | #define USB_ENDPOINT_IN 0x80 224 | #define USB_ENDPOINT_OUT 0x00 225 | 226 | /* Error codes */ 227 | #define USB_ERROR_BEGIN 500000 228 | 229 | /* 230 | * This is supposed to look weird. This file is generated from autoconf 231 | * and I didn't want to make this too complicated. 232 | */ 233 | #if 0 234 | #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) 235 | #else 236 | #define USB_LE16_TO_CPU(x) 237 | #endif 238 | 239 | /* Data types */ 240 | struct usb_device; 241 | struct usb_bus; 242 | 243 | /* 244 | * To maintain compatibility with applications already built with libusb, 245 | * we must only add entries to the end of this structure. NEVER delete or 246 | * move members and only change types if you really know what you're doing. 247 | */ 248 | struct usb_device { 249 | struct usb_device *next, *prev; 250 | 251 | char filename[LIBUSB_PATH_MAX]; 252 | 253 | struct usb_bus *bus; 254 | 255 | struct usb_device_descriptor descriptor; 256 | struct usb_config_descriptor *config; 257 | 258 | void *dev; /* Darwin support */ 259 | 260 | u_int8_t devnum; 261 | 262 | unsigned char num_children; 263 | struct usb_device **children; 264 | }; 265 | 266 | struct usb_bus { 267 | struct usb_bus *next, *prev; 268 | 269 | char dirname[LIBUSB_PATH_MAX]; 270 | 271 | struct usb_device *devices; 272 | u_int32_t location; 273 | 274 | struct usb_device *root_dev; 275 | }; 276 | 277 | struct usb_dev_handle; 278 | typedef struct usb_dev_handle usb_dev_handle; 279 | 280 | /* Variables */ 281 | extern struct usb_bus *usb_busses; 282 | 283 | /* release byte-packing ( Stanson ) */ 284 | #pragma pack(pop) 285 | 286 | #ifdef __cplusplus 287 | extern "C" { 288 | #endif 289 | 290 | /* Function prototypes */ 291 | 292 | /* usb.c */ 293 | usb_dev_handle *usb_open(struct usb_device *dev); 294 | int usb_close(usb_dev_handle *dev); 295 | int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 296 | size_t buflen); 297 | int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, 298 | size_t buflen); 299 | 300 | /* descriptors.c */ 301 | int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 302 | unsigned char type, unsigned char index, void *buf, int size); 303 | int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 304 | unsigned char index, void *buf, int size); 305 | 306 | /* .c */ 307 | int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 308 | int timeout); 309 | int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 310 | int timeout); 311 | int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 312 | int timeout); 313 | int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 314 | int timeout); 315 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 316 | int value, int index, char *bytes, int size, int timeout); 317 | int usb_set_configuration(usb_dev_handle *dev, int configuration); 318 | int usb_claim_interface(usb_dev_handle *dev, int interface); 319 | int usb_release_interface(usb_dev_handle *dev, int interface); 320 | int usb_set_altinterface(usb_dev_handle *dev, int alternate); 321 | int usb_resetep(usb_dev_handle *dev, unsigned int ep); 322 | int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); 323 | int usb_reset(usb_dev_handle *dev); 324 | 325 | #if 1 326 | #define LIBUSB_HAS_GET_DRIVER_NP 1 327 | int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, 328 | unsigned int namelen); 329 | #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1 330 | int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface); 331 | #endif 332 | 333 | const char *usb_strerror(void); 334 | 335 | void usb_init(void); 336 | void usb_set_debug(int level); 337 | int usb_find_busses(void); 338 | int usb_find_devices(void); 339 | struct usb_device *usb_device(usb_dev_handle *dev); 340 | struct usb_bus *usb_get_busses(void); 341 | 342 | 343 | int usb_bulk_setup_async(usb_dev_handle *dev, void **context, 344 | unsigned char ep); 345 | 346 | int usb_submit_async(void *context, char *bytes, int size); 347 | int usb_reap_async(void *context, int timeout); 348 | int usb_free_async(void **context); 349 | int usb_cancel_async (void *context); 350 | 351 | #ifdef __cplusplus 352 | } 353 | #endif 354 | 355 | #endif /* __USB_H__ */ 356 | 357 | -------------------------------------------------------------------------------- /src/descriptors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Parses descriptors 3 | * 4 | * Copyright (c) 2001 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include "usbi.h" 13 | 14 | int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 15 | unsigned char type, unsigned char index, void *buf, int size) 16 | { 17 | memset(buf, 0, size); 18 | 19 | return usb_control_msg(udev, ep | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 20 | (type << 8) + index, 0, buf, size, 1000); 21 | } 22 | 23 | int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 24 | unsigned char index, void *buf, int size) 25 | { 26 | memset(buf, 0, size); 27 | 28 | return usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 29 | (type << 8) + index, 0, buf, size, 1000); 30 | } 31 | 32 | int usb_parse_descriptor(unsigned char *source, const char *description, void *dest) 33 | { 34 | unsigned char *sp = source, *dp = dest; 35 | uint16_t w; 36 | uint32_t d; 37 | const char *cp; 38 | 39 | for (cp = description; *cp; cp++) { 40 | switch (*cp) { 41 | case 'b': /* 8-bit byte */ 42 | *dp++ = *sp++; 43 | break; 44 | case 'w': /* 16-bit word, convert from little endian to CPU */ 45 | w = (sp[1] << 8) | sp[0]; sp += 2; 46 | // dp += ((unsigned long)dp & 1); /* Align to word boundary */ 47 | *((uint16_t *)dp) = w; dp += 2; 48 | break; 49 | case 'd': /* 32-bit dword, convert from little endian to CPU */ 50 | d = (sp[3] << 24) | (sp[2] << 16) | (sp[1] << 8) | sp[0]; sp += 4; 51 | // dp += ((unsigned long)dp & 2); /* Align to dword boundary */ 52 | *((uint32_t *)dp) = d; dp += 4; 53 | break; 54 | /* These two characters are undocumented and just a hack for Linux */ 55 | case 'W': /* 16-bit word, keep CPU endianess */ 56 | // dp += ((unsigned long)dp & 1); /* Align to word boundary */ 57 | memcpy(dp, sp, 2); sp += 2; dp += 2; 58 | break; 59 | case 'D': /* 32-bit dword, keep CPU endianess */ 60 | // dp += ((unsigned long)dp & 2); /* Align to dword boundary */ 61 | memcpy(dp, sp, 4); sp += 4; dp += 4; 62 | break; 63 | } 64 | } 65 | 66 | return sp - source; 67 | } 68 | 69 | /* 70 | * This code looks surprisingly similar to the code I wrote for the Linux 71 | * kernel. It's not a coincidence :) 72 | */ 73 | 74 | static int usb_parse_endpoint(struct usb_endpoint_descriptor *endpoint, unsigned char *buffer, int size) 75 | { 76 | struct usb_descriptor_header header; 77 | unsigned char *begin; 78 | int parsed = 0, len, numskipped; 79 | 80 | usb_parse_descriptor(buffer, "bb", &header); 81 | 82 | /* Everything should be fine being passed into here, but we sanity */ 83 | /* check JIC */ 84 | if (header.bLength > size) { 85 | if (usb_debug >= 1) 86 | fprintf(stderr, "ran out of descriptors parsing\n"); 87 | return -1; 88 | } 89 | 90 | if (header.bDescriptorType != USB_DT_ENDPOINT) { 91 | if (usb_debug >= 2) 92 | fprintf(stderr, "unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X\n", 93 | header.bDescriptorType, USB_DT_ENDPOINT); 94 | return parsed; 95 | } 96 | 97 | if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH) 98 | usb_parse_descriptor(buffer, "bbbbwbbb", endpoint); 99 | else if (header.bLength >= ENDPOINT_DESC_LENGTH) 100 | usb_parse_descriptor(buffer, "bbbbwb", endpoint); 101 | 102 | buffer += header.bLength; 103 | size -= header.bLength; 104 | parsed += header.bLength; 105 | 106 | /* Skip over the rest of the Class Specific or Vendor Specific */ 107 | /* descriptors */ 108 | begin = buffer; 109 | numskipped = 0; 110 | while (size >= DESC_HEADER_LENGTH) { 111 | usb_parse_descriptor(buffer, "bb", &header); 112 | 113 | if (header.bLength < 2) { 114 | if (usb_debug >= 1) 115 | fprintf(stderr, "invalid descriptor length of %d\n", header.bLength); 116 | return -1; 117 | } 118 | 119 | /* If we find another "proper" descriptor then we're done */ 120 | if ((header.bDescriptorType == USB_DT_ENDPOINT) || 121 | (header.bDescriptorType == USB_DT_INTERFACE) || 122 | (header.bDescriptorType == USB_DT_CONFIG) || 123 | (header.bDescriptorType == USB_DT_DEVICE)) 124 | break; 125 | 126 | if (usb_debug >= 1) 127 | fprintf(stderr, "skipping descriptor 0x%X\n", header.bDescriptorType); 128 | numskipped++; 129 | 130 | buffer += header.bLength; 131 | size -= header.bLength; 132 | parsed += header.bLength; 133 | } 134 | 135 | if (numskipped && usb_debug >= 2) 136 | fprintf(stderr, "skipped %d class/vendor specific endpoint descriptors\n", numskipped); 137 | 138 | /* Copy any unknown descriptors into a storage area for drivers */ 139 | /* to later parse */ 140 | len = (int)(buffer - begin); 141 | if (!len) { 142 | endpoint->extra = NULL; 143 | endpoint->extralen = 0; 144 | return parsed; 145 | } 146 | 147 | endpoint->extra = malloc(len); 148 | if (!endpoint->extra) { 149 | if (usb_debug >= 1) 150 | fprintf(stderr, "couldn't allocate memory for endpoint extra descriptors\n"); 151 | endpoint->extralen = 0; 152 | return parsed; 153 | } 154 | 155 | memcpy(endpoint->extra, begin, len); 156 | endpoint->extralen = len; 157 | 158 | return parsed; 159 | } 160 | 161 | static int usb_parse_interface(struct usb_interface *interface, 162 | unsigned char *buffer, int size) 163 | { 164 | int i, len, numskipped, retval, parsed = 0; 165 | struct usb_descriptor_header header; 166 | struct usb_interface_descriptor *ifp; 167 | unsigned char *begin; 168 | 169 | interface->num_altsetting = 0; 170 | 171 | while (size >= INTERFACE_DESC_LENGTH) { 172 | interface->altsetting = realloc(interface->altsetting, sizeof(struct usb_interface_descriptor) * (interface->num_altsetting + 1)); 173 | if (!interface->altsetting) { 174 | if (usb_debug >= 1) 175 | fprintf(stderr, "couldn't malloc interface->altsetting\n"); 176 | return -1; 177 | } 178 | 179 | ifp = interface->altsetting + interface->num_altsetting; 180 | interface->num_altsetting++; 181 | 182 | usb_parse_descriptor(buffer, "bbbbbbbbb", ifp); 183 | 184 | /* Skip over the interface */ 185 | buffer += ifp->bLength; 186 | parsed += ifp->bLength; 187 | size -= ifp->bLength; 188 | 189 | begin = buffer; 190 | numskipped = 0; 191 | 192 | /* Skip over any interface, class or vendor descriptors */ 193 | while (size >= DESC_HEADER_LENGTH) { 194 | usb_parse_descriptor(buffer, "bb", &header); 195 | 196 | if (header.bLength < 2) { 197 | if (usb_debug >= 1) 198 | fprintf(stderr, "invalid descriptor length of %d\n", header.bLength); 199 | return -1; 200 | } 201 | 202 | /* If we find another "proper" descriptor then we're done */ 203 | if ((header.bDescriptorType == USB_DT_INTERFACE) || 204 | (header.bDescriptorType == USB_DT_ENDPOINT) || 205 | (header.bDescriptorType == USB_DT_CONFIG) || 206 | (header.bDescriptorType == USB_DT_DEVICE)) 207 | break; 208 | 209 | numskipped++; 210 | 211 | buffer += header.bLength; 212 | parsed += header.bLength; 213 | size -= header.bLength; 214 | } 215 | 216 | if (numskipped && usb_debug >= 2) 217 | fprintf(stderr, "skipped %d class/vendor specific interface descriptors\n", numskipped); 218 | 219 | /* Copy any unknown descriptors into a storage area for */ 220 | /* drivers to later parse */ 221 | len = (int)(buffer - begin); 222 | if (!len) { 223 | ifp->extra = NULL; 224 | ifp->extralen = 0; 225 | } else { 226 | ifp->extra = malloc(len); 227 | if (!ifp->extra) { 228 | if (usb_debug >= 1) 229 | fprintf(stderr, "couldn't allocate memory for interface extra descriptors\n"); 230 | ifp->extralen = 0; 231 | return -1; 232 | } 233 | memcpy(ifp->extra, begin, len); 234 | ifp->extralen = len; 235 | } 236 | 237 | /* Did we hit an unexpected descriptor? */ 238 | usb_parse_descriptor(buffer, "bb", &header); 239 | if ((size >= DESC_HEADER_LENGTH) && 240 | ((header.bDescriptorType == USB_DT_CONFIG) || 241 | (header.bDescriptorType == USB_DT_DEVICE))) 242 | return parsed; 243 | 244 | if (ifp->bNumEndpoints > USB_MAXENDPOINTS) { 245 | if (usb_debug >= 1) 246 | fprintf(stderr, "too many endpoints\n"); 247 | return -1; 248 | } 249 | 250 | if (ifp->bNumEndpoints > 0) { 251 | ifp->endpoint = (struct usb_endpoint_descriptor *) 252 | malloc(ifp->bNumEndpoints * 253 | sizeof(struct usb_endpoint_descriptor)); 254 | if (!ifp->endpoint) { 255 | if (usb_debug >= 1) 256 | fprintf(stderr, "couldn't allocate memory for ifp->endpoint\n"); 257 | return -1; 258 | } 259 | 260 | memset(ifp->endpoint, 0, ifp->bNumEndpoints * 261 | sizeof(struct usb_endpoint_descriptor)); 262 | 263 | for (i = 0; i < ifp->bNumEndpoints; i++) { 264 | usb_parse_descriptor(buffer, "bb", &header); 265 | 266 | if (header.bLength > size) { 267 | if (usb_debug >= 1) 268 | fprintf(stderr, "ran out of descriptors parsing\n"); 269 | return -1; 270 | } 271 | 272 | retval = usb_parse_endpoint(ifp->endpoint + i, buffer, size); 273 | if (retval < 0) 274 | return retval; 275 | 276 | buffer += retval; 277 | parsed += retval; 278 | size -= retval; 279 | } 280 | } else 281 | ifp->endpoint = NULL; 282 | 283 | /* We check to see if it's an alternate to this one */ 284 | ifp = (struct usb_interface_descriptor *)buffer; 285 | if (size < USB_DT_INTERFACE_SIZE || 286 | ifp->bDescriptorType != USB_DT_INTERFACE || 287 | !ifp->bAlternateSetting) 288 | return parsed; 289 | } 290 | 291 | return parsed; 292 | } 293 | 294 | int usb_parse_configuration(struct usb_config_descriptor *config, 295 | unsigned char *buffer) 296 | { 297 | int i, retval, size; 298 | struct usb_descriptor_header header; 299 | 300 | usb_parse_descriptor(buffer, "bbwbbbbb", config); 301 | size = config->wTotalLength; 302 | 303 | if (config->bNumInterfaces > USB_MAXINTERFACES) { 304 | if (usb_debug >= 1) 305 | fprintf(stderr, "too many interfaces\n"); 306 | return -1; 307 | } 308 | 309 | config->interface = (struct usb_interface *) 310 | malloc(config->bNumInterfaces * 311 | sizeof(struct usb_interface)); 312 | if (!config->interface) { 313 | if (usb_debug >= 1) 314 | fprintf(stderr, "out of memory\n"); 315 | return -1; 316 | } 317 | 318 | memset(config->interface, 0, config->bNumInterfaces * sizeof(struct usb_interface)); 319 | 320 | buffer += config->bLength; 321 | size -= config->bLength; 322 | 323 | config->extra = NULL; 324 | config->extralen = 0; 325 | 326 | for (i = 0; i < config->bNumInterfaces; i++) { 327 | int numskipped, len; 328 | unsigned char *begin; 329 | 330 | /* Skip over the rest of the Class Specific or Vendor */ 331 | /* Specific descriptors */ 332 | begin = buffer; 333 | numskipped = 0; 334 | while (size >= DESC_HEADER_LENGTH) { 335 | usb_parse_descriptor(buffer, "bb", &header); 336 | 337 | if ((header.bLength > size) || (header.bLength < DESC_HEADER_LENGTH)) { 338 | if (usb_debug >= 1) 339 | fprintf(stderr, "invalid descriptor length of %d\n", header.bLength); 340 | return -1; 341 | } 342 | 343 | /* If we find another "proper" descriptor then we're done */ 344 | if ((header.bDescriptorType == USB_DT_ENDPOINT) || 345 | (header.bDescriptorType == USB_DT_INTERFACE) || 346 | (header.bDescriptorType == USB_DT_CONFIG) || 347 | (header.bDescriptorType == USB_DT_DEVICE)) 348 | break; 349 | 350 | if (usb_debug >= 2) 351 | fprintf(stderr, "skipping descriptor 0x%X\n", header.bDescriptorType); 352 | numskipped++; 353 | 354 | buffer += header.bLength; 355 | size -= header.bLength; 356 | } 357 | 358 | if (numskipped && usb_debug >= 2) 359 | fprintf(stderr, "skipped %d class/vendor specific endpoint descriptors\n", numskipped); 360 | 361 | /* Copy any unknown descriptors into a storage area for */ 362 | /* drivers to later parse */ 363 | len = (int)(buffer - begin); 364 | if (len) { 365 | /* FIXME: We should realloc and append here */ 366 | if (!config->extralen) { 367 | config->extra = malloc(len); 368 | if (!config->extra) { 369 | if (usb_debug >= 1) 370 | fprintf(stderr, "couldn't allocate memory for config extra descriptors\n"); 371 | config->extralen = 0; 372 | return -1; 373 | } 374 | 375 | memcpy(config->extra, begin, len); 376 | config->extralen = len; 377 | } 378 | } 379 | 380 | retval = usb_parse_interface(config->interface + i, buffer, size); 381 | if (retval < 0) 382 | return retval; 383 | 384 | buffer += retval; 385 | size -= retval; 386 | } 387 | 388 | return size; 389 | } 390 | 391 | void usb_destroy_configuration(struct usb_device *dev) 392 | { 393 | int c, i, j, k; 394 | 395 | if (!dev->config) 396 | return; 397 | 398 | for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { 399 | struct usb_config_descriptor *cf = &dev->config[c]; 400 | 401 | if (!cf->interface) 402 | continue; 403 | 404 | for (i = 0; i < cf->bNumInterfaces; i++) { 405 | struct usb_interface *ifp = &cf->interface[i]; 406 | 407 | if (!ifp->altsetting) 408 | continue; 409 | 410 | for (j = 0; j < ifp->num_altsetting; j++) { 411 | struct usb_interface_descriptor *as = &ifp->altsetting[j]; 412 | 413 | if (as->extra) 414 | free(as->extra); 415 | 416 | if (!as->endpoint) 417 | continue; 418 | 419 | for (k = 0; k < as->bNumEndpoints; k++) { 420 | if (as->endpoint[k].extra) 421 | free(as->endpoint[k].extra); 422 | } 423 | free(as->endpoint); 424 | } 425 | 426 | free(ifp->altsetting); 427 | } 428 | 429 | free(cf->interface); 430 | } 431 | 432 | free(dev->config); 433 | } 434 | 435 | void usb_fetch_and_parse_descriptors(usb_dev_handle *udev) 436 | { 437 | struct usb_device *dev = udev->device; 438 | int i; 439 | 440 | if (dev->descriptor.bNumConfigurations > USB_MAXCONFIG) { 441 | if (usb_debug >= 1) 442 | fprintf(stderr, "Too many configurations (%d > %d)\n", dev->descriptor.bNumConfigurations, USB_MAXCONFIG); 443 | return; 444 | } 445 | 446 | if (dev->descriptor.bNumConfigurations < 1) { 447 | if (usb_debug >= 1) 448 | fprintf(stderr, "Not enough configurations (%d < %d)\n", dev->descriptor.bNumConfigurations, 1); 449 | return; 450 | } 451 | 452 | dev->config = (struct usb_config_descriptor *)malloc(dev->descriptor.bNumConfigurations * sizeof(struct usb_config_descriptor)); 453 | if (!dev->config) { 454 | if (usb_debug >= 1) 455 | fprintf(stderr, "Unable to allocate memory for config descriptor\n"); 456 | return; 457 | } 458 | 459 | memset(dev->config, 0, dev->descriptor.bNumConfigurations * 460 | sizeof(struct usb_config_descriptor)); 461 | 462 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { 463 | unsigned char buffer[8], *bigbuffer; 464 | struct usb_config_descriptor config; 465 | int res; 466 | 467 | /* Get the first 8 bytes so we can figure out what the total length is */ 468 | res = usb_get_descriptor(udev, USB_DT_CONFIG, i, buffer, 8); 469 | if (res < 8) { 470 | if (usb_debug >= 1) { 471 | if (res < 0) 472 | fprintf(stderr, "Unable to get descriptor (%d)\n", res); 473 | else 474 | fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", 8, res); 475 | } 476 | 477 | goto err; 478 | } 479 | 480 | usb_parse_descriptor(buffer, "bbw", &config); 481 | 482 | bigbuffer = malloc(config.wTotalLength); 483 | if (!bigbuffer) { 484 | if (usb_debug >= 1) 485 | fprintf(stderr, "Unable to allocate memory for descriptors\n"); 486 | goto err; 487 | } 488 | 489 | res = usb_get_descriptor(udev, USB_DT_CONFIG, i, bigbuffer, config.wTotalLength); 490 | if (res < config.wTotalLength) { 491 | if (usb_debug >= 1) { 492 | if (res < 0) 493 | fprintf(stderr, "Unable to get descriptor (%d)\n", res); 494 | else 495 | fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", config.wTotalLength, res); 496 | } 497 | 498 | free(bigbuffer); 499 | goto err; 500 | } 501 | 502 | res = usb_parse_configuration(&dev->config[i], bigbuffer); 503 | if (usb_debug >= 2) { 504 | if (res > 0) 505 | fprintf(stderr, "Descriptor data still left\n"); 506 | else if (res < 0) 507 | fprintf(stderr, "Unable to parse descriptors\n"); 508 | } 509 | 510 | free(bigbuffer); 511 | } 512 | 513 | return; 514 | 515 | err: 516 | free(dev->config); 517 | 518 | dev->config = NULL; 519 | } 520 | 521 | -------------------------------------------------------------------------------- /src/linux.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Linux USB support 3 | * 4 | * Copyright (c) 2000-2003 Johannes Erdfelt 5 | * 6 | * This library is covered by the LGPL, read LICENSE for details. 7 | */ 8 | 9 | #include /* getenv, etc */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "linux.h" 19 | #include "usbi.h" 20 | 21 | /* redefine linux ETIMEDOUT to match libusb-win32 ETIMEDOUT value */ 22 | #undef ETIMEDOUT 23 | #define ETIMEDOUT 116 24 | 25 | static char usb_path[PATH_MAX + 1] = ""; 26 | 27 | static int device_open(struct usb_device *dev) 28 | { 29 | char filename[PATH_MAX + 1]; 30 | int fd; 31 | 32 | snprintf(filename, sizeof(filename) - 1, "%s/%s/%s", 33 | usb_path, dev->bus->dirname, dev->filename); 34 | 35 | fd = open(filename, O_RDWR); 36 | if (fd < 0) { 37 | fd = open(filename, O_RDONLY); 38 | if (fd < 0) 39 | USB_ERROR_STR(-errno, "failed to open %s: %s", 40 | filename, strerror(errno)); 41 | } 42 | 43 | return fd; 44 | } 45 | 46 | int usb_os_open(usb_dev_handle *dev) 47 | { 48 | dev->fd = device_open(dev->device); 49 | 50 | return 0; 51 | } 52 | 53 | int usb_os_close(usb_dev_handle *dev) 54 | { 55 | if (dev->fd < 0) 56 | return 0; 57 | 58 | if (close(dev->fd) == -1) 59 | /* Failing trying to close a file really isn't an error, so return 0 */ 60 | USB_ERROR_STR(0, "tried to close device fd %d: %s", dev->fd, 61 | strerror(errno)); 62 | 63 | return 0; 64 | } 65 | 66 | int usb_set_configuration(usb_dev_handle *dev, int configuration) 67 | { 68 | int ret; 69 | 70 | /* detach kernel driver for windows program ( Stanson ) */ 71 | usb_detach_kernel_driver_np(dev, 0); 72 | 73 | ret = ioctl(dev->fd, IOCTL_USB_SETCONFIG, &configuration); 74 | if (ret < 0) 75 | USB_ERROR_STR(-errno, "could not set config %d: %s", configuration, 76 | strerror(errno)); 77 | 78 | dev->config = configuration; 79 | 80 | return 0; 81 | } 82 | 83 | int usb_claim_interface(usb_dev_handle *dev, int interface) 84 | { 85 | int ret; 86 | 87 | ret = ioctl(dev->fd, IOCTL_USB_CLAIMINTF, &interface); 88 | if (ret < 0) { 89 | if (errno == EBUSY && usb_debug > 0) 90 | fprintf(stderr, "Check that you have permissions to write to %s/%s and, if you don't, that you set up hotplug (http://linux-hotplug.sourceforge.net/) correctly.\n", dev->bus->dirname, dev->device->filename); 91 | 92 | USB_ERROR_STR(-errno, "could not claim interface %d: %s", interface, 93 | strerror(errno)); 94 | } 95 | 96 | dev->interface = interface; 97 | 98 | return 0; 99 | } 100 | 101 | int usb_release_interface(usb_dev_handle *dev, int interface) 102 | { 103 | int ret; 104 | 105 | ret = ioctl(dev->fd, IOCTL_USB_RELEASEINTF, &interface); 106 | if (ret < 0) 107 | USB_ERROR_STR(-errno, "could not release intf %d: %s", interface, 108 | strerror(errno)); 109 | 110 | dev->interface = -1; 111 | 112 | return 0; 113 | } 114 | 115 | int usb_set_altinterface(usb_dev_handle *dev, int alternate) 116 | { 117 | int ret; 118 | struct usb_setinterface setintf; 119 | 120 | if (dev->interface < 0) 121 | USB_ERROR(-EINVAL); 122 | 123 | setintf.interface = dev->interface; 124 | setintf.altsetting = alternate; 125 | 126 | ret = ioctl(dev->fd, IOCTL_USB_SETINTF, &setintf); 127 | if (ret < 0) 128 | USB_ERROR_STR(-errno, "could not set alt intf %d/%d: %s", 129 | dev->interface, alternate, strerror(errno)); 130 | 131 | dev->altsetting = alternate; 132 | 133 | return 0; 134 | } 135 | 136 | /* 137 | * Linux usbfs has a limit of one page size for synchronous bulk read/write. 138 | * 4096 is the most portable maximum we can do for now. 139 | * Linux usbfs has a limit of 16KB for the URB interface. We use this now 140 | * to get better performance for USB 2.0 devices. 141 | */ 142 | #define MAX_READ_WRITE (16 * 1024) 143 | 144 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 145 | int value, int index, char *bytes, int size, int timeout) 146 | { 147 | struct usb_ctrltransfer ctrl; 148 | int ret; 149 | 150 | ctrl.bRequestType = requesttype; 151 | ctrl.bRequest = request; 152 | ctrl.wValue = value; 153 | ctrl.wIndex = index; 154 | ctrl.wLength = size; 155 | 156 | ctrl.data = bytes; 157 | ctrl.timeout = timeout; 158 | 159 | ret = ioctl(dev->fd, IOCTL_USB_CONTROL, &ctrl); 160 | if (ret < 0) 161 | USB_ERROR_STR(-errno, "error sending control message: %s", strerror(errno)); 162 | 163 | return ret; 164 | } 165 | 166 | #define URB_USERCONTEXT_COOKIE ((void *)0x1) 167 | 168 | /* Reading and writing are the same except for the endpoint */ 169 | static int usb_urb_transfer(usb_dev_handle *dev, int ep, int urbtype, 170 | char *bytes, int size, int timeout) 171 | { 172 | struct usb_urb urb; 173 | int bytesdone = 0, requested; 174 | struct timeval tv, tv_ref, tv_now; 175 | struct usb_urb *context; 176 | int ret, waiting; 177 | 178 | /* 179 | * HACK: The use of urb.usercontext is a hack to get threaded applications 180 | * sort of working again. Threaded support is still not recommended, but 181 | * this should allow applications to work in the common cases. Basically, 182 | * if we get the completion for an URB we're not waiting for, then we update 183 | * the usercontext pointer to 1 for the other threads URB and it will see 184 | * the change after it wakes up from the the timeout. Ugly, but it works. 185 | */ 186 | 187 | /* 188 | * Get actual time, and add the timeout value. The result is the absolute 189 | * time where we have to quit waiting for an message. 190 | */ 191 | gettimeofday(&tv_ref, NULL); 192 | tv_ref.tv_sec = tv_ref.tv_sec + timeout / 1000; 193 | tv_ref.tv_usec = tv_ref.tv_usec + (timeout % 1000) * 1000; 194 | 195 | if (tv_ref.tv_usec > 1000000) { 196 | tv_ref.tv_usec -= 1000000; 197 | tv_ref.tv_sec++; 198 | } 199 | 200 | do { 201 | fd_set writefds; 202 | 203 | requested = size - bytesdone; 204 | if (requested > MAX_READ_WRITE) 205 | requested = MAX_READ_WRITE; 206 | 207 | urb.type = urbtype; 208 | urb.endpoint = ep; 209 | urb.flags = 0; 210 | urb.buffer = bytes + bytesdone; 211 | urb.buffer_length = requested; 212 | urb.signr = 0; 213 | urb.actual_length = 0; 214 | urb.number_of_packets = 0; /* don't do isochronous yet */ 215 | urb.usercontext = NULL; 216 | 217 | ret = ioctl(dev->fd, IOCTL_USB_SUBMITURB, &urb); 218 | if (ret < 0) { 219 | USB_ERROR_STR(-errno, "error submitting URB: %s", strerror(errno)); 220 | return ret; 221 | } 222 | 223 | FD_ZERO(&writefds); 224 | FD_SET(dev->fd, &writefds); 225 | 226 | restart: 227 | waiting = 1; 228 | context = NULL; 229 | while (!urb.usercontext && ((ret = ioctl(dev->fd, IOCTL_USB_REAPURBNDELAY, &context)) == -1) && waiting) { 230 | tv.tv_sec = 0; 231 | tv.tv_usec = 1000; // 1 msec 232 | select(dev->fd + 1, NULL, &writefds, NULL, &tv); //sub second wait 233 | 234 | if (timeout) { 235 | /* compare with actual time, as the select timeout is not that precise */ 236 | gettimeofday(&tv_now, NULL); 237 | 238 | if ((tv_now.tv_sec > tv_ref.tv_sec) || 239 | ((tv_now.tv_sec == tv_ref.tv_sec) && (tv_now.tv_usec >= tv_ref.tv_usec))) 240 | waiting = 0; 241 | } 242 | } 243 | 244 | if (context && context != &urb) { 245 | context->usercontext = URB_USERCONTEXT_COOKIE; 246 | /* We need to restart since we got a successful URB, but not ours */ 247 | goto restart; 248 | } 249 | 250 | /* 251 | * If there was an error, that wasn't EAGAIN (no completion), then 252 | * something happened during the reaping and we should return that 253 | * error now 254 | */ 255 | if (ret < 0 && !urb.usercontext && errno != EAGAIN) 256 | USB_ERROR_STR(-errno, "error reaping URB: %s", strerror(errno)); 257 | 258 | bytesdone += urb.actual_length; 259 | } while ((ret == 0 || urb.usercontext) && bytesdone < size && urb.actual_length == requested); 260 | 261 | /* If the URB didn't complete in success or error, then let's unlink it */ 262 | if (ret < 0 && !urb.usercontext) { 263 | int rc; 264 | 265 | if (!waiting) 266 | rc = -ETIMEDOUT; 267 | else 268 | rc = urb.status; 269 | 270 | ret = ioctl(dev->fd, IOCTL_USB_DISCARDURB, &urb); 271 | if (ret < 0 && errno != EINVAL && usb_debug >= 1) 272 | fprintf(stderr, "error discarding URB: %s", strerror(errno)); 273 | 274 | /* 275 | * When the URB is unlinked, it gets moved to the completed list and 276 | * then we need to reap it or else the next time we call this function, 277 | * we'll get the previous completion and exit early 278 | */ 279 | ioctl(dev->fd, IOCTL_USB_REAPURB, &context); 280 | 281 | return rc; 282 | } 283 | 284 | return bytesdone; 285 | } 286 | 287 | int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 288 | int timeout) 289 | { 290 | /* Ensure the endpoint address is correct */ 291 | return usb_urb_transfer(dev, ep, USB_URB_TYPE_BULK, bytes, size, 292 | timeout); 293 | } 294 | 295 | int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 296 | int timeout) 297 | { 298 | /* Ensure the endpoint address is correct */ 299 | ep |= USB_ENDPOINT_IN; 300 | return usb_urb_transfer(dev, ep, USB_URB_TYPE_BULK, bytes, size, 301 | timeout); 302 | } 303 | 304 | /* 305 | * FIXME: Packetize large buffers here. 2.4 HCDs (atleast, haven't checked 306 | * 2.5 HCDs yet) don't handle multi-packet Interrupt transfers. So we need 307 | * to lookup the endpoint packet size and packetize appropriately here. 308 | */ 309 | int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 310 | int timeout) 311 | { 312 | /* Ensure the endpoint address is correct */ 313 | return usb_urb_transfer(dev, ep, USB_URB_TYPE_INTERRUPT, bytes, size, 314 | timeout); 315 | } 316 | 317 | int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 318 | int timeout) 319 | { 320 | /* Ensure the endpoint address is correct */ 321 | ep |= USB_ENDPOINT_IN; 322 | return usb_urb_transfer(dev, ep, USB_URB_TYPE_INTERRUPT, bytes, size, 323 | timeout); 324 | } 325 | 326 | int usb_os_find_busses(struct usb_bus **busses) 327 | { 328 | struct usb_bus *fbus = NULL; 329 | DIR *dir; 330 | struct dirent *entry; 331 | 332 | dir = opendir(usb_path); 333 | if (!dir) 334 | USB_ERROR_STR(-errno, "couldn't opendir(%s): %s", usb_path, 335 | strerror(errno)); 336 | 337 | while ((entry = readdir(dir)) != NULL) { 338 | struct usb_bus *bus; 339 | 340 | /* Skip anything starting with a . */ 341 | if (entry->d_name[0] == '.') 342 | continue; 343 | 344 | if (!strchr("0123456789", entry->d_name[strlen(entry->d_name) - 1])) { 345 | if (usb_debug >= 2) 346 | fprintf(stderr, "usb_os_find_busses: Skipping non bus directory %s\n", 347 | entry->d_name); 348 | continue; 349 | } 350 | 351 | bus = malloc(sizeof(*bus)); 352 | if (!bus) 353 | USB_ERROR(-ENOMEM); 354 | 355 | memset((void *)bus, 0, sizeof(*bus)); 356 | 357 | strncpy(bus->dirname, entry->d_name, sizeof(bus->dirname) - 1); 358 | bus->dirname[sizeof(bus->dirname) - 1] = 0; 359 | 360 | LIST_ADD(fbus, bus); 361 | 362 | if (usb_debug >= 2) 363 | fprintf(stderr, "usb_os_find_busses: Found %s\n", bus->dirname); 364 | } 365 | 366 | closedir(dir); 367 | 368 | *busses = fbus; 369 | 370 | return 0; 371 | } 372 | 373 | int usb_os_find_devices(struct usb_bus *bus, struct usb_device **devices) 374 | { 375 | struct usb_device *fdev = NULL; 376 | DIR *dir; 377 | struct dirent *entry; 378 | char dirpath[PATH_MAX + 1]; 379 | 380 | snprintf(dirpath, PATH_MAX, "%s/%s", usb_path, bus->dirname); 381 | 382 | dir = opendir(dirpath); 383 | if (!dir) 384 | USB_ERROR_STR(-errno, "couldn't opendir(%s): %s", dirpath, 385 | strerror(errno)); 386 | 387 | while ((entry = readdir(dir)) != NULL) { 388 | unsigned char device_desc[DEVICE_DESC_LENGTH]; 389 | char filename[PATH_MAX + 1]; 390 | struct usb_device *dev; 391 | struct usb_connectinfo connectinfo; 392 | int i, fd, ret; 393 | 394 | /* Skip anything starting with a . */ 395 | if (entry->d_name[0] == '.') 396 | continue; 397 | 398 | dev = malloc(sizeof(*dev)); 399 | if (!dev) 400 | USB_ERROR(-ENOMEM); 401 | 402 | memset((void *)dev, 0, sizeof(*dev)); 403 | 404 | dev->bus = bus; 405 | 406 | strncpy(dev->filename, entry->d_name, sizeof(dev->filename) - 1); 407 | dev->filename[sizeof(dev->filename) - 1] = 0; 408 | 409 | snprintf(filename, sizeof(filename) - 1, "%s/%s", dirpath, entry->d_name); 410 | fd = open(filename, O_RDWR); 411 | if (fd < 0) { 412 | fd = open(filename, O_RDONLY); 413 | if (fd < 0) { 414 | if (usb_debug >= 2) 415 | fprintf(stderr, "usb_os_find_devices: Couldn't open %s\n", 416 | filename); 417 | 418 | free(dev); 419 | continue; 420 | } 421 | } 422 | 423 | /* Get the device number */ 424 | ret = ioctl(fd, IOCTL_USB_CONNECTINFO, &connectinfo); 425 | if (ret < 0) { 426 | if (usb_debug) 427 | fprintf(stderr, "usb_os_find_devices: couldn't get connect info\n"); 428 | } else 429 | dev->devnum = connectinfo.devnum; 430 | 431 | ret = read(fd, (void *)device_desc, DEVICE_DESC_LENGTH); 432 | if (ret < 0) { 433 | if (usb_debug) 434 | fprintf(stderr, "usb_os_find_devices: Couldn't read descriptor\n"); 435 | 436 | free(dev); 437 | 438 | goto err; 439 | } 440 | 441 | /* 442 | * Linux kernel converts the words in this descriptor to CPU endian, so 443 | * we use the undocumented W character for usb_parse_descriptor() that 444 | * doesn't convert endianess when parsing the descriptor 445 | */ 446 | usb_parse_descriptor(device_desc, "bbWbbbbWWWbbbb", &dev->descriptor); 447 | 448 | LIST_ADD(fdev, dev); 449 | 450 | if (usb_debug >= 2) 451 | fprintf(stderr, "usb_os_find_devices: Found %s on %s\n", 452 | dev->filename, bus->dirname); 453 | 454 | /* Now try to fetch the rest of the descriptors */ 455 | if (dev->descriptor.bNumConfigurations > USB_MAXCONFIG) 456 | /* Silent since we'll try again later */ 457 | goto err; 458 | 459 | if (dev->descriptor.bNumConfigurations < 1) 460 | /* Silent since we'll try again later */ 461 | goto err; 462 | 463 | dev->config = (struct usb_config_descriptor *)malloc(dev->descriptor.bNumConfigurations * sizeof(struct usb_config_descriptor)); 464 | if (!dev->config) 465 | /* Silent since we'll try again later */ 466 | goto err; 467 | 468 | memset(dev->config, 0, dev->descriptor.bNumConfigurations * 469 | sizeof(struct usb_config_descriptor)); 470 | 471 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { 472 | unsigned char buffer[8], *bigbuffer; 473 | struct usb_config_descriptor config; 474 | 475 | /* Get the first 8 bytes so we can figure out what the total length is */ 476 | ret = read(fd, (void *)buffer, 8); 477 | if (ret < 8) { 478 | if (usb_debug >= 1) { 479 | if (ret < 0) 480 | fprintf(stderr, "Unable to get descriptor (%d)\n", ret); 481 | else 482 | fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", 8, ret); 483 | } 484 | 485 | goto err; 486 | } 487 | 488 | usb_parse_descriptor(buffer, "bbw", &config); 489 | 490 | bigbuffer = malloc(config.wTotalLength); 491 | if (!bigbuffer) { 492 | if (usb_debug >= 1) 493 | fprintf(stderr, "Unable to allocate memory for descriptors\n"); 494 | goto err; 495 | } 496 | 497 | /* Read the rest of the config descriptor */ 498 | memcpy(bigbuffer, buffer, 8); 499 | 500 | ret = read(fd, (void *)(bigbuffer + 8), config.wTotalLength - 8); 501 | if (ret < config.wTotalLength - 8) { 502 | if (usb_debug >= 1) { 503 | if (ret < 0) 504 | fprintf(stderr, "Unable to get descriptor (%d)\n", ret); 505 | else 506 | fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", config.wTotalLength, ret); 507 | } 508 | 509 | free(bigbuffer); 510 | goto err; 511 | } 512 | 513 | ret = usb_parse_configuration(&dev->config[i], bigbuffer); 514 | if (usb_debug >= 2) { 515 | if (ret > 0) 516 | fprintf(stderr, "Descriptor data still left\n"); 517 | else if (ret < 0) 518 | fprintf(stderr, "Unable to parse descriptors\n"); 519 | } 520 | 521 | free(bigbuffer); 522 | } 523 | 524 | err: 525 | close(fd); 526 | } 527 | 528 | closedir(dir); 529 | 530 | *devices = fdev; 531 | 532 | return 0; 533 | } 534 | 535 | int usb_os_determine_children(struct usb_bus *bus) 536 | { 537 | struct usb_device *dev, *devices[256]; 538 | struct usb_ioctl command; 539 | int ret, i, i1; 540 | 541 | /* Create a list of devices first */ 542 | memset(devices, 0, sizeof(devices)); 543 | for (dev = bus->devices; dev; dev = dev->next) 544 | if (dev->devnum) 545 | devices[dev->devnum] = dev; 546 | 547 | /* Now fetch the children for each device */ 548 | for (dev = bus->devices; dev; dev = dev->next) { 549 | struct usb_hub_portinfo portinfo; 550 | int fd; 551 | 552 | fd = device_open(dev); 553 | if (fd < 0) 554 | continue; 555 | 556 | /* Query the hub driver for the children of this device */ 557 | if (dev->config && dev->config->interface && dev->config->interface->altsetting) 558 | command.ifno = dev->config->interface->altsetting->bInterfaceNumber; 559 | else 560 | command.ifno = 0; 561 | command.ioctl_code = IOCTL_USB_HUB_PORTINFO; 562 | command.data = &portinfo; 563 | ret = ioctl(fd, IOCTL_USB_IOCTL, &command); 564 | if (ret < 0) { 565 | /* errno == ENOSYS means the device probably wasn't a hub */ 566 | if (errno != ENOSYS && usb_debug > 1) 567 | fprintf(stderr, "error obtaining child information: %s\n", 568 | strerror(errno)); 569 | 570 | close(fd); 571 | continue; 572 | } 573 | 574 | dev->num_children = 0; 575 | for (i = 0; i < portinfo.numports; i++) 576 | if (portinfo.port[i]) 577 | dev->num_children++; 578 | 579 | /* Free any old children first */ 580 | free(dev->children); 581 | 582 | dev->children = malloc(sizeof(struct usb_device *) * dev->num_children); 583 | if (!dev->children) { 584 | if (usb_debug > 1) 585 | fprintf(stderr, "error allocating %zu bytes memory for dev->children\n", 586 | sizeof(struct usb_device *) * dev->num_children); 587 | 588 | dev->num_children = 0; 589 | close(fd); 590 | continue; 591 | } 592 | 593 | for (i = 0, i1 = 0; i < portinfo.numports; i++) { 594 | if (!portinfo.port[i]) 595 | continue; 596 | 597 | dev->children[i1++] = devices[portinfo.port[i]]; 598 | 599 | devices[portinfo.port[i]] = NULL; 600 | } 601 | 602 | close(fd); 603 | } 604 | 605 | /* 606 | * There should be one device left in the devices list and that should be 607 | * the root device 608 | */ 609 | for (i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) { 610 | if (devices[i]) 611 | bus->root_dev = devices[i]; 612 | } 613 | 614 | return 0; 615 | } 616 | 617 | static int check_usb_vfs(const char *dirname) 618 | { 619 | DIR *dir; 620 | struct dirent *entry; 621 | int found = 0; 622 | 623 | dir = opendir(dirname); 624 | if (!dir) 625 | return 0; 626 | 627 | while ((entry = readdir(dir)) != NULL) { 628 | /* Skip anything starting with a . */ 629 | if (entry->d_name[0] == '.') 630 | continue; 631 | 632 | /* We assume if we find any files that it must be the right place */ 633 | found = 1; 634 | break; 635 | } 636 | 637 | closedir(dir); 638 | 639 | return found; 640 | } 641 | 642 | void usb_os_init(void) 643 | { 644 | /* Find the path to the virtual filesystem */ 645 | if (getenv("USB_DEVFS_PATH")) { 646 | if (check_usb_vfs(getenv("USB_DEVFS_PATH"))) { 647 | strncpy(usb_path, getenv("USB_DEVFS_PATH"), sizeof(usb_path) - 1); 648 | usb_path[sizeof(usb_path) - 1] = 0; 649 | } else if (usb_debug) 650 | fprintf(stderr, "usb_os_init: couldn't find USB VFS in USB_DEVFS_PATH\n"); 651 | } 652 | 653 | if (!usb_path[0]) { 654 | if (check_usb_vfs("/dev/bus/usb")) { 655 | strncpy(usb_path, "/dev/bus/usb", sizeof(usb_path) - 1); 656 | usb_path[sizeof(usb_path) - 1] = 0; 657 | } else if (check_usb_vfs("/proc/bus/usb")) { 658 | strncpy(usb_path, "/proc/bus/usb", sizeof(usb_path) - 1); 659 | usb_path[sizeof(usb_path) - 1] = 0; 660 | } else 661 | usb_path[0] = 0; /* No path, no USB support */ 662 | } 663 | 664 | if (usb_debug) { 665 | if (usb_path[0]) 666 | fprintf(stderr, "usb_os_init: Found USB VFS at %s\n", usb_path); 667 | else 668 | fprintf(stderr, "usb_os_init: No USB VFS found, is it mounted?\n"); 669 | } 670 | } 671 | 672 | int usb_resetep(usb_dev_handle *dev, unsigned int ep) 673 | { 674 | int ret; 675 | 676 | ret = ioctl(dev->fd, IOCTL_USB_RESETEP, &ep); 677 | if (ret) 678 | USB_ERROR_STR(-errno, "could not reset ep %d: %s", ep, 679 | strerror(errno)); 680 | 681 | return 0; 682 | } 683 | 684 | int usb_clear_halt(usb_dev_handle *dev, unsigned int ep) 685 | { 686 | int ret; 687 | 688 | ret = ioctl(dev->fd, IOCTL_USB_CLEAR_HALT, &ep); 689 | if (ret) 690 | USB_ERROR_STR(-errno, "could not clear/halt ep %d: %s", ep, 691 | strerror(errno)); 692 | 693 | return 0; 694 | } 695 | 696 | int usb_reset(usb_dev_handle *dev) 697 | { 698 | int ret; 699 | 700 | ret = ioctl(dev->fd, IOCTL_USB_RESET, NULL); 701 | if (ret) 702 | USB_ERROR_STR(-errno, "could not reset: %s", strerror(errno)); 703 | 704 | return 0; 705 | } 706 | 707 | int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, 708 | unsigned int namelen) 709 | { 710 | struct usb_getdriver getdrv; 711 | int ret; 712 | 713 | getdrv.interface = interface; 714 | ret = ioctl(dev->fd, IOCTL_USB_GETDRIVER, &getdrv); 715 | if (ret) 716 | USB_ERROR_STR(-errno, "could not get bound driver: %s", strerror(errno)); 717 | 718 | strncpy(name, getdrv.driver, namelen - 1); 719 | name[namelen - 1] = 0; 720 | 721 | return 0; 722 | } 723 | 724 | int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface) 725 | { 726 | struct usb_ioctl command; 727 | int ret; 728 | 729 | command.ifno = interface; 730 | command.ioctl_code = IOCTL_USB_DISCONNECT; 731 | command.data = NULL; 732 | 733 | ret = ioctl(dev->fd, IOCTL_USB_IOCTL, &command); 734 | if (ret) 735 | USB_ERROR_STR(-errno, "could not detach kernel driver from interface %d: %s", 736 | interface, strerror(errno)); 737 | 738 | return 0; 739 | } 740 | 741 | 742 | // ------------------------------------------------------------------------------- 743 | // this async functions added by some person who'd like to remain anonymous 744 | // It was necessary to make Aerodrums application run under wine. 745 | 746 | typedef struct { 747 | usb_dev_handle *dev; 748 | char *bytes; 749 | int size; 750 | struct usb_urb urb; 751 | int reaped; 752 | } usb_context_t; 753 | 754 | static int _usb_setup_async(usb_dev_handle *dev, void **context, 755 | int urbtype, 756 | unsigned char ep, int pktsize) 757 | { 758 | usb_context_t **c = (usb_context_t **)context; 759 | 760 | /* Any error checks required here? */ 761 | 762 | *c = malloc(sizeof(usb_context_t)); 763 | 764 | if (!*c) { 765 | USB_ERROR_STR(-errno, "memory allocation error: %s\n", strerror(errno)); 766 | return -ENOMEM; 767 | } 768 | 769 | memset(*c, 0, sizeof(usb_context_t)); 770 | 771 | (*c)->dev = dev; 772 | (*c)->urb.type = urbtype; 773 | (*c)->urb.endpoint = ep; 774 | (*c)->urb.flags = 0; 775 | (*c)->urb.signr = 0; 776 | (*c)->urb.actual_length = 0; 777 | (*c)->urb.number_of_packets = 0; /* don't do isochronous yet */ 778 | (*c)->urb.usercontext = NULL; 779 | 780 | return 0; 781 | } 782 | 783 | int usb_bulk_setup_async(usb_dev_handle *dev, void **context, unsigned char ep) 784 | { 785 | return _usb_setup_async(dev, context, USB_URB_TYPE_BULK, ep, 0); 786 | } 787 | 788 | /* Reading and writing are the same except for the endpoint */ 789 | int usb_submit_async(void *context, char *bytes, int size) 790 | { 791 | int ret; 792 | 793 | usb_context_t *c = (usb_context_t *)context; 794 | 795 | c->urb.buffer = bytes; 796 | c->urb.buffer_length = size; 797 | c->urb.usercontext = c; 798 | c->reaped = 0; 799 | 800 | ret = ioctl(c->dev->fd, IOCTL_USB_SUBMITURB, &c->urb); 801 | if (ret < 0) { 802 | USB_ERROR_STR(-errno, "error submitting URB: %s", strerror(errno)); 803 | return ret; 804 | } 805 | 806 | return 0; 807 | } 808 | 809 | static int _usb_reap_async(void *context, int timeout, int cancel) 810 | { 811 | int rc; 812 | usb_context_t *c = (usb_context_t *)context; 813 | struct usb_urb *urb; 814 | 815 | if (c->reaped) 816 | return c->urb.actual_length; 817 | 818 | again: 819 | rc = ioctl(c->dev->fd, IOCTL_USB_REAPURB, &urb); 820 | if (rc < 0) { 821 | fprintf(stderr, "error reaping URB: %s", strerror(errno)); 822 | return rc; 823 | } 824 | 825 | usb_context_t *b = (usb_context_t *)urb->usercontext; 826 | if (b != c) { 827 | b->reaped = 1; 828 | goto again; 829 | } 830 | 831 | #if 0 832 | /* Not ready for usb_cancel_async */ 833 | if (cancel) { 834 | usb_cancel_async(context); 835 | } 836 | #else 837 | (void)cancel; 838 | #endif 839 | return urb->actual_length; 840 | } 841 | 842 | int usb_reap_async(void *context, int timeout) 843 | { 844 | return _usb_reap_async(context, timeout, 1); 845 | } 846 | 847 | int usb_reap_async_nocancel(void *context, int timeout) 848 | { 849 | return _usb_reap_async(context, timeout, 0); 850 | } 851 | 852 | 853 | int usb_cancel_async(void *context) 854 | { 855 | printf("%s()\n", __func__); 856 | USB_ERROR_STR(-errno, "usb_cancel_async is not yet implemented: %s\n", strerror(errno)); 857 | #if 0 858 | /* NOTE that this function will cancel all pending URBs */ 859 | /* on the same endpoint as this particular context, or even */ 860 | /* all pending URBs for this particular device. */ 861 | 862 | usb_context_t *c = (usb_context_t *)context; 863 | 864 | if (!c) 865 | { 866 | USBERR0("invalid context\n"); 867 | return -EINVAL; 868 | } 869 | 870 | if (c->dev->impl_info == INVALID_HANDLE_VALUE) 871 | { 872 | USBERR0("device not open\n"); 873 | return -EINVAL; 874 | } 875 | 876 | _usb_cancel_io(c); 877 | 878 | return 0; 879 | #endif 880 | int rc; 881 | usb_context_t *c = (usb_context_t *)context; 882 | 883 | rc = ioctl(c->dev->fd, IOCTL_USB_DISCARDURB, &c->urb); 884 | if (rc < 0) 885 | fprintf(stderr, "error discarding URB: %s", strerror(errno)); 886 | 887 | /* 888 | * When the URB is unlinked, it gets moved to the completed list and 889 | * then we need to reap it or else the next time we call this function, 890 | * we'll get the previous completion and exit early 891 | */ 892 | ioctl(c->dev->fd, IOCTL_USB_REAPURB, &context); 893 | 894 | return 0; 895 | } 896 | 897 | int usb_free_async(void **context) 898 | { 899 | usb_context_t **c = (usb_context_t **)context; 900 | 901 | if (!*c) { 902 | USB_ERROR_STR(-errno, "invalid context: %s\n", strerror(errno)); 903 | return -EINVAL; 904 | } 905 | 906 | free(*c); 907 | *c = NULL; 908 | 909 | return 0; 910 | } 911 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | --------------------------------------------------------------------------------