]> git.zerfleddert.de Git - usb-driver/blobdiff - usb-driver.c
rename sources
[usb-driver] / usb-driver.c
index 60d0073c264aa07c3197fd0c52a999034ea94463..c71cbd08d67b0a214cca635f67a3246e36e71c2f 100644 (file)
 #include <stdio.h>
 #include <usb.h>
 #include <signal.h>
-#include "xilinx.h"
+#include <pthread.h>
+#include "usb-driver.h"
 
 static int (*ioctl_func) (int, int, void *) = NULL;
 static int windrvrfd = 0;
-static int modulesfd = 0;
+FILE *modulesfp;
 static int modules_read = 0;
 static struct usb_bus *busses = NULL;
 static struct usb_device *usbdevice;
 static usb_dev_handle *usb_devhandle = NULL;
 static unsigned long card_type;
 static int ints_enabled = 0;
+static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
 
 #define NO_WINDRVR 1
 #undef DEBUG
@@ -232,10 +234,12 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        break;
 
                case CARD_REGISTER:
+                       /* TODO: Implement for LPT-support */
+#if 0
                        {
-                               //struct card_register* cr = (struct card_register*)(wdheader->data);
-                               /* Todo: LPT-Port already in use */
+                               struct card_register* cr = (struct card_register*)(wdheader->data);
                        }
+#endif
 #ifdef DEBUG
                        fprintf(stderr,"faking CARD_REGISTER\n");
 #endif
@@ -318,7 +322,7 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 
                                it->fEnableOk = 1;
                                ints_enabled = 1;
-                               //ret = (*ioctl_func) (fd, request, wdioctl);
+                               pthread_mutex_trylock(&int_wait);
                        }
 
                        break;
@@ -339,6 +343,7 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                it->dwCounter = 0;
                                it->fStopped = 1;
                                ints_enabled = 0;
+                               pthread_mutex_unlock(&int_wait);
 #endif
 #ifdef DEBUG
                                fprintf(stderr,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped);
@@ -366,7 +371,6 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        /* FIXME: Select right interface! */
                                        ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
                                        if (!ret) {
-                                               //ret = usb_set_configuration(usb_devhandle, usbdevice->config[0].bConfigurationValue);
                                                if(!ret) {
                                                        ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
                                                        if (ret)
@@ -397,7 +401,6 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                fprintf(stderr, "unique: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
 #endif
                                pSize = ugdd->dwBytes;
-                               //ret = (*ioctl_func) (fd, request, wdioctl);
                                if (!ugdd->dwBytes) {
                                        if (usbdevice) {
                                                ugdd->dwBytes = usb_deviceinfo(NULL);
@@ -508,10 +511,10 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        if (it->dwCounter == 0) {
                                                it->dwCounter = 1;
                                        } else {
-                                               while(ints_enabled) {sleep(1);}
+                                               pthread_mutex_lock(&int_wait);
                                        }
                                } else {
-                                       while(ints_enabled) {sleep(1);}
+                                       pthread_mutex_lock(&int_wait);
                                }
 #endif
 
@@ -582,6 +585,26 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
        return ret;
 }
 
+int ioctl(int fd, int request, ...)
+{
+       va_list args;
+       void *argp;
+       int ret;
+
+       if (!ioctl_func)                                                                    
+               ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");             
+
+       va_start (args, request);
+       argp = va_arg (args, void *);
+       va_end (args);
+
+       if (fd == windrvrfd)
+               ret = do_wdioctl(fd, request, argp);
+       else
+               ret = (*ioctl_func) (fd, request, argp);
+
+       return ret;
+}
 
 typedef int (*open_funcptr_t) (const char *, int, mode_t);
 
@@ -637,13 +660,6 @@ int close(int fd) {
                windrvrfd = 0;
        }
 
-       if (fd == modulesfd) {
-#ifdef DEBUG
-               fprintf(stderr,"close modulesfd\n");
-#endif
-               modulesfd = 0;
-       }
-
        return (*func) (fd);
 }
 
@@ -661,7 +677,7 @@ FILE *fopen(const char *path, const char *mode) {
                fprintf(stderr,"opening /proc/modules\n");
 #endif
 #ifdef NO_WINDRVR
-               modulesfd = fileno(ret);
+               modulesfp = ret;
                modules_read = 0;
 #endif
        }
@@ -669,43 +685,38 @@ FILE *fopen(const char *path, const char *mode) {
        return ret;
 }
 
-ssize_t read(int fd, void *buf, size_t count) {
-       size_t ret;
-       static ssize_t (*func) (int, void*, size_t) = NULL;
+char *fgets(char *s, int size, FILE *stream) {
+        static char* (*func) (char*, int, FILE*) = NULL;
        const char modules[] = "windrvr6 160960 0 - Live 0xf98b0000\n";
+       char *ret = NULL;
+
 
        if (!func)
-               func = (ssize_t (*) (int, void*, size_t)) dlsym(REAL_LIBC, "read");
+               func = (char* (*) (char*, int, FILE*)) dlsym(REAL_LIBC, "fgets");
        
-       if ((!modules_read) && (fd == modulesfd)) {
-               strcpy(buf, modules);
-               ret = strlen(modules);
-               modules_read = 1;
+       if (modulesfp == stream) {
+               if (!modules_read) {
+                       strcpy(s, modules);
+                       ret = s;
+                       modules_read = 1;
+               }
        } else {
-               ret = (*func) (fd, buf, count);
+               ret = (*func)(s,size,stream);
        }
 
        return ret;
 }
 
-int ioctl(int fd, int request, ...)
-{
-       va_list args;
-       void *argp;
-       int ret;
-
-       if (!ioctl_func)                                                                    
-               ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");             
+int fclose(FILE *fp) {
+       static int (*func) (FILE*) = NULL;
 
-       va_start (args, request);
-       argp = va_arg (args, void *);
-       va_end (args);
-
-       if (fd == windrvrfd)
-               ret = do_wdioctl(fd, request, argp);
-       else
-               ret = (*ioctl_func) (fd, request, argp);
+       if (!func)
+               func = (int (*) (FILE*)) dlsym(REAL_LIBC, "fclose");
 
-       return ret;
+       if (fp == modulesfp) {
+               modulesfp = NULL;
+       }
+       
+       return (*func)(fp);
 }
 #endif
Impressum, Datenschutz