]> cvs.zerfleddert.de Git - usb-driver/blobdiff - usb-driver.c
some links, libusb-dev
[usb-driver] / usb-driver.c
index cd9807a9017d5391df1e650abf50c1e80b0426b0..a34e5e45347f3639e83f7ac7e3ae19d9fd0292e7 100644 (file)
@@ -46,9 +46,9 @@
 static int (*ioctl_func) (int, int, void *) = NULL;
 static int windrvrfd = -1;
 static int parportfd = -1;
-static int parportnum = 0;
 static unsigned long ppbase = 0;
 static unsigned long ecpbase = 0;
+static struct pports *pplist = NULL;
 FILE *modulesfp = NULL;
 static int modules_read = 0;
 static struct usb_bus *busses = NULL;
@@ -239,7 +239,9 @@ int usb_deviceinfo(unsigned char *buf) {
 
 int pp_transfer(WD_TRANSFER *tr, int fd, unsigned int request, unsigned char *wdioctl) {
        int ret = 0;
+       unsigned long port = (unsigned long)tr->dwPort;
        unsigned char val;
+       static int last_pp_write = 0;
 
        DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
                        (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
@@ -258,33 +260,71 @@ int pp_transfer(WD_TRANSFER *tr, int fd, unsigned int request, unsigned char *wd
        if (parportfd < 0)
                return ret;
 
-       switch(tr->cmdTrans) {
-               case 10: /* Read Byte */
-                       if ((unsigned long)tr->dwPort == ppbase) { /* Data Port */
+       if (port == ppbase + PP_DATA) {
+               DPRINTF("data port\n");
+               switch(tr->cmdTrans) {
+                       case PP_READ:
                                ret = 0; /* We don't support reading of the data port */
-                       } else if ((unsigned long)tr->dwPort == ppbase + 1) { /* Status Port */
-                               DPRINTF("status port\n");
-                               ret = ioctl(parportfd, PPRSTATUS, &val);
-                       } else if ((unsigned long)tr->dwPort == ppbase + 2) { /* Control Port */
-                               DPRINTF("control port\n");
-                               ret = ioctl(parportfd, PPRCONTROL, &val);
-                       }
-                       break;
-               case 13: /* Write Byte */
-                       if ((unsigned long)tr->dwPort == ppbase) { /* Data Port */
-                               DPRINTF("data port\n");
+                               break;
+
+                       case PP_WRITE:
                                ret = ioctl(parportfd, PPWDATA, &val);
-                       } else if ((unsigned long)tr->dwPort == ppbase + 1) { /* Status Port */
+                               last_pp_write = val;
+                               break;
+
+                       default:
+                               fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
+                               ret = -1;
+                               break;
+               }
+       } else if (port == ppbase + PP_STATUS) {
+               DPRINTF("status port (last write: %d)\n", last_pp_write);
+               switch(tr->cmdTrans) {
+                       case PP_READ:
+                               ret = ioctl(parportfd, PPRSTATUS, &val);
+#ifdef FORCE_PC3_IDENT
+                               val &= 95;
+                               if (last_pp_write & 64)
+                                       val |= 32;
+                               else
+                                       val |= 128;
+#endif
+                               break;
+
+                       case PP_WRITE:
                                ret = 0; /* Status Port is readonly */
-                       } else if ((unsigned long)tr->dwPort == ppbase + 2) { /* Control Port */
-                               DPRINTF("control port\n");
+                               break;
+
+                       default:
+                               fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
+                               ret = -1;
+                               break;
+               }
+       } else if (port == ppbase + PP_CONTROL) {
+               DPRINTF("control port\n");
+               switch(tr->cmdTrans) {
+                       case PP_READ:
+                               ret = ioctl(parportfd, PPRCONTROL, &val);
+                               break;
+
+                       case PP_WRITE:
                                ret = ioctl(parportfd, PPWCONTROL, &val);
-                       }
-                       break;
-               default:
-                       fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
-                       ret = -1;
-                       break;
+                               break;
+
+                       default:
+                               fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
+                               ret = -1;
+                               break;
+               }
+       } else if ((port == ecpbase + PP_ECP_CFGA) && ecpbase) {
+               DPRINTF("ECP_CFGA port\n");
+       } else if ((port == ecpbase + PP_ECP_CFGB) && ecpbase) {
+               DPRINTF("ECP_CFGB port\n");
+       } else if ((port == ecpbase + PP_ECP_ECR) && ecpbase) {
+               DPRINTF("ECP_ECR port\n");
+       } else {
+               DPRINTF("access to unsupported address range!\n");
+               ret = 0;
        }
 
        tr->Data.Byte = val;
@@ -345,10 +385,38 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
                                if (parportfd < 0) {
-                                       snprintf(ppdev, sizeof(ppdev), "/dev/parport%d", parportnum);
+                                       int max = -1;
+                                       struct pports **port = &pplist;
+
+                                       while (*port) {
+                                               DPRINTF("Looking up parallel port in linked list, entry: %d\n", (*port)->num);
+                                               if (max < (*port)->num)
+                                                       max = (*port)->num;
+
+                                               if ((*port)->base == (unsigned long)cr->Card.Item[0].I.IO.dwAddr) {
+                                                       break;
+                                               }
+
+                                               port = &((*port)->next);
+                                       }
+
+                                       if (!(*port)) { /* not found */
+                                               (*port) = malloc(sizeof(struct pports));
+                                               if (!(*port)) {
+                                                       perror("malloc");
+                                                       exit(EXIT_FAILURE);
+                                               }
+
+                                               (*port)->base = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
+                                               (*port)->num = max+1;
+                                               (*port)->next = NULL;
+
+                                               DPRINTF("parallel port not in linked list, new entry: %d\n", (*port)->num);
+                                       }
+
+                                       snprintf(ppdev, sizeof(ppdev), "/dev/parport%d", (*port)->num);
                                        DPRINTF("opening %s\n", ppdev);
                                        parportfd = open(ppdev, O_RDWR|O_EXCL);
-                                       parportnum++;
 
                                        if (parportfd < 0)
                                                fprintf(stderr,"Can't open %s: %s\n", ppdev, strerror(errno));
@@ -360,17 +428,20 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        if (ioctl(parportfd, PPCLAIM) == -1)
                                                return ret;
 
+                                       ecpbase = 0;
                                        pmode = IEEE1284_MODE_COMPAT;
                                        if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
                                                return ret;
                                        
-                                       if (cr->Card.dwItems > 1) {
-                                               ecpbase = cr->Card.Item[1].I.IO.dwBytes;
+                                       if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) {
+                                               DPRINTF("ECP mode requested\n");
+                                               ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
                                                /* TODO: Implement ECP mode */
 #if 0
                                                pmode = IEEE1284_MODE_ECP;
 
                                                if (ioctl(parportfd, PPNEGOT, &pmode) == -1) {
+                                                       ecpbase = 0;
                                                        pmode = IEEE1284_MODE_COMPAT;
                                                        if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
                                                                return ret;
@@ -739,7 +810,6 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        ioctl(parportfd, PPRELEASE);
                                        close(parportfd);
                                        parportfd = -1;
-                                       parportnum--;
                                }
 #endif
                        }
Impressum, Datenschutz