~ubuntu-branches/ubuntu/saucy/argyll/saucy

« back to all changes in this revision

Viewing changes to spectro/usbio.c

  • Committer: Package Import Robot
  • Author(s): Christian Marillat
  • Date: 2012-04-25 07:46:07 UTC
  • mfrom: (1.2.2) (13.1.15 sid)
  • Revision ID: package-import@ubuntu.com-20120425074607-yjqadetw8kum9skc
Tags: 1.4.0-4
Should Build-Depends on libusb-dev (Closes: #670329).

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "icoms.h"
38
38
#include "conv.h"
39
39
#include "usbio.h"
 
40
#include "hidio.h"
40
41
 
41
42
#ifdef USE_LIBUSB1
42
43
# include "libusb.h"
71
72
        if (p->debug) fprintf(stderr,"usb_check_and_add() called with VID 0x%x, PID 0x%x\n",descriptor.idVendor, descriptor.idProduct);
72
73
 
73
74
#ifdef USE_LIBUSB1
74
 
#ifdef NT
 
75
#if defined(NT) || defined(__APPLE__)
75
76
        /* Ignore libusb1 HID driver capability */
76
 
        if (descriptor.bDeviceClass == LIBUSB_CLASS_HID)
77
 
                return 0;
78
77
        {
79
 
                struct libusb_config_descriptor *config;
 
78
                struct libusb_config_descriptor *config = NULL;
80
79
 
81
 
                if (libusb_get_config_descriptor(dev, 0, &config) != LIBUSB_SUCCESS) {
 
80
                if (descriptor.bDeviceClass != LIBUSB_CLASS_HID
 
81
                  && libusb_get_config_descriptor(dev, 0, &config) != LIBUSB_SUCCESS) {
82
82
                        if (p->debug) fprintf(stderr,"Get config desc. 0 failed\n");
83
83
                        return 1;
84
84
                }
85
85
 
86
 
                if (config->bNumInterfaces > 0
87
 
                 && config->interface[0]. num_altsetting > 0
88
 
                 & config->interface[0].altsetting[0].bInterfaceClass == LIBUSB_CLASS_HID) {
89
 
                        if (p->debug) fprintf(stderr,"Is a libusb HID device\n");
 
86
                if (descriptor.bDeviceClass == LIBUSB_CLASS_HID
 
87
                 || (config->bNumInterfaces > 0
 
88
                  && config->interface[0]. num_altsetting > 0
 
89
                  && config->interface[0].altsetting[0].bInterfaceClass == LIBUSB_CLASS_HID)) {
 
90
                        int i;
 
91
                        /* See if this devices is already in the list via the HID interface */
 
92
                        /* (This may not be 100% correct in the face of multiple instances
 
93
                           of the same device, if Windows allows different drivers for different
 
94
                           instances of the same device type.) */
 
95
                        for (i = 0; i < p->npaths; i++) {
 
96
                                if (p->paths[i]->vid == descriptor.idVendor
 
97
                                 && p->paths[i]->pid == descriptor.idProduct)
 
98
                                        break;          /* Yes */
 
99
                        }
 
100
                        if (i < p->npaths) {
 
101
                                if (p->debug) fprintf(stderr,"Is an HID device and already added\n");
 
102
                                if (config != NULL)
 
103
                                        libusb_free_config_descriptor(config);
 
104
                                return 0;
 
105
                        }
 
106
                }
 
107
                if (config != NULL)
90
108
                        libusb_free_config_descriptor(config);
91
 
                        return 0;
92
 
                }
93
 
                libusb_free_config_descriptor(config);
94
109
        }
95
110
#endif
96
111
#endif
151
166
) {
152
167
#ifdef ENABLE_USB
153
168
        ssize_t i, nlist;
154
 
        libusb_context *ctx = NULL;
155
 
 
156
169
        struct libusb_device **list;
157
170
 
158
171
        /* Scan the USB busses for instruments we recognise */
159
 
        /* We're not expecting any of our unstruments to be an interface on a device. */
 
172
        /* We're not expecting any of our instruments to be an interface on a device. */
160
173
 
161
 
        libusb_init(&ctx);
 
174
        if (p->ctx == NULL) {
 
175
                int rv;
 
176
                if ((rv = libusb_init(&p->ctx)) != 0) {
 
177
                        error("libusb_init() failed with %d",rv);
 
178
                }
 
179
        }
162
180
 
163
181
        if (p->debug > 8)
164
 
                libusb_set_debug(ctx, p->debug);
 
182
                libusb_set_debug(p->ctx, p->debug);
165
183
 
166
184
        nlist = libusb_get_device_list(NULL, &list);
167
185
    
185
203
#ifdef ENABLE_USB
186
204
        struct usb_bus *bus;
187
205
 
188
 
        /* Check that we've got an up to date version of libusb */
189
 
        if (usb_argyll_patched() < 2)
190
 
                error("usblib isn't up to date to work with this version of Argyll");
191
 
 
192
206
        if (p->debug > 8)
193
207
                usb_set_debug(p->debug);
194
208
 
224
238
#endif
225
239
}
226
240
 
 
241
/* Cleanup any USB specific icoms */
 
242
void usb_del_usb(icoms *p) {
 
243
 
 
244
#ifdef USE_LIBUSB1
 
245
        if (p->ctx != NULL) {
 
246
                libusb_exit(p->ctx);
 
247
                p->ctx = NULL;
 
248
        }
 
249
#endif /* USE_LIBUSB1 */
 
250
}
227
251
 
228
252
/* Return the instrument type if the port number is USB, */
229
253
/* and instUnknown if it is not. */
236
260
                p->get_paths(p);
237
261
 
238
262
        if (port <= 0 || port > p->npaths)
239
 
                error("icoms - usb_is_usb_portno: port number out of range!");
 
263
                error("icoms - usb_is_usb_portno: port number %d out of range %d - %d",port,1,p->npaths);
240
264
 
241
265
#ifdef ENABLE_USB
242
266
        if (p->paths[port-1]->dev != NULL)
615
639
#endif /* ENABLE_USB */
616
640
}
617
641
 
618
 
/* Declaration of needed function in ntio.c or unixio.c */
619
 
void icoms_close_port(icoms *p);
620
 
 
621
642
/* Open a USB port for all our uses. */
622
643
static void usb_open_port(
623
644
icoms *p,
634
655
 
635
656
        if (port >= 1) {
636
657
                if (p->is_open && port != p->port) {    /* If port number changes */
637
 
                        icoms_close_port(p);
 
658
                        p->close_port(p);
638
659
                }
639
660
        }
640
661
 
641
662
        /* Make sure the port is open */
642
663
        if (!p->is_open) {
 
664
                struct usb_device_descriptor descriptor;
643
665
#ifdef USE_LIBUSB1
644
666
                const struct libusb_interface_descriptor *ifd;
645
667
            struct libusb_config_descriptor *confdesc;
683
705
                                msec_sleep(77);
684
706
                        }
685
707
                        if (tries > 0 && pnames != NULL && kpc == NULL) {
686
 
#ifdef __APPLE__
 
708
#if defined(__APPLE__) 
687
709
                                if ((kpc = kkill_nprocess(pnames, p->debug)) == NULL) {
688
710
                                        if (p->debug) fprintf(stderr,"kkill_nprocess returned error!\n");
689
711
                                }
704
726
                                        error("Opening USB port '%s' config %d failed (%s) (Permissions ?)",p->ppath->path,config,USB_STRERROR(rv));
705
727
                                }
706
728
                                continue;
707
 
                        }
 
729
                        } else if (p->debug)
 
730
                                fprintf(stderr,"open() of USB port '%s' suceeded\n",p->ppath->path);
 
731
 
 
732
                        /* Get a copy of the device descriptor so we can see device params */
 
733
#ifdef USE_LIBUSB1
 
734
                        if (libusb_get_device_descriptor(p->ppath->dev, &descriptor) != LIBUSB_SUCCESS)
 
735
                                error("Get device descriptor on USB port '%s' failed with %d (%s)",p->ppath->path,rv,libusb_strerror(rv));
 
736
#else
 
737
                        descriptor = p->ppath->dev->descriptor; /* Copy */
 
738
#endif
 
739
 
708
740
                        p->vid = p->ppath->vid;
709
741
                        p->pid = p->ppath->pid;
710
742
                        p->usbd = p->ppath->dev;
730
762
                        }
731
763
#endif
732
764
#endif
733
 
                        /* (Should use bConfigurationValue ?) */
 
765
 
 
766
                        if (p->debug > 1) fprintf(stderr, "Number of configurations = %d\n",
 
767
                                                  descriptor.bNumConfigurations);
 
768
#if defined(UNIX) && !defined(__APPLE__)
 
769
                        /* only call set_configuration on Linux if the device has more than one */
 
770
                        /* possible configuration, because Linux does a set_configuration by default, */
 
771
                        /* and two of them mess up instruments like the Spyder2 */
 
772
 
 
773
                        if (descriptor.bNumConfigurations > 1) {
 
774
#endif
 
775
 
734
776
                        /* Can't skip this, as it is needed to setup the interface and end points on OS X */
735
777
#ifdef USE_LIBUSB1
736
778
                        if ((rv = libusb_set_configuration(p->usbh, p->cnfg)) < 0)
754
796
#endif
755
797
                                continue;
756
798
                        }
 
799
#if defined(UNIX) && !defined(__APPLE__)
 
800
                        }               /* End of if bNumConfigurations > 1 */
 
801
#endif
 
802
 
757
803
                        /* We're done */
758
804
                        break;
759
805
                }
1194
1240
        int lerr;                               /* Last error */
1195
1241
        int bread, qa;
1196
1242
        long top;                       /* Timeout period */
1197
 
        unsigned char *rrbuf = rbuf;    /* Start of return buffer */
1198
1243
        int bulk = 0;                   /* nz if bulk rather than interrupt read */
1199
1244
 
1200
1245
#ifdef QUIET_MEMCHECKERS
1226
1271
 
1227
1272
        /* Bug workaround - on some OS's for some devices */
1228
1273
        if (p->uflags & icomuf_resetep_before_read) {
 
1274
                msec_sleep(1);          /* Let device recover ? */
1229
1275
                p->usb_resetep(p, ep);
1230
1276
                msec_sleep(1);          /* Let device recover (ie. Spyder 3) */
1231
1277
        }
1426
1472
        icoms *p,
1427
1473
        void *hcancel
1428
1474
) {
1429
 
        int rv;
 
1475
        int rv = 0;
1430
1476
#ifdef USE_LIBUSB1
1431
1477
        if (hcancel != NULL)
1432
1478
                rv = libusb_cancel_transfer((struct libusb_transfer *)hcancel);
1433
1479
#else
 
1480
        msec_sleep(1);          /* Let device recover ? */
1434
1481
        rv = usb_resetep(p->usbh, (int)hcancel);        /* Not reliable ? */
 
1482
        msec_sleep(1);          /* Let device recover ? */
1435
1483
#endif
1436
1484
 
1437
1485
        if (rv == 0)
1441
1489
}
1442
1490
 
1443
1491
/* - - - - - - - - - - - - - - - - - - - - - - - - - */
1444
 
/* Reset and enp point data toggle to 0 */
 
1492
/* Reset and end point data toggle to 0 */
1445
1493
int icoms_usb_resetep(
1446
1494
        icoms *p,
1447
1495
        int ep                                  /* End point address */
1553
1601
        if (p->debug) fprintf(stderr,"icoms: About to set usb port characteristics\n");
1554
1602
 
1555
1603
        if (p->is_open) 
1556
 
                icoms_close_port(p);
 
1604
                p->close_port(p);
1557
1605
 
1558
1606
        if (p->is_usb_portno(p, port) != instUnknown) {
1559
1607
 
1740
1788
        int i;
1741
1789
        char *d;
1742
1790
 
 
1791
        buf[0] = '\000';
1743
1792
        for(i = 0, d = buf; i < 64 && i < len; i++, s++) {
1744
1793
                sprintf(d, "%s%02x", i > 0 ? " " : "", *s);
1745
1794
                d += strlen(d);