~ubuntu-branches/ubuntu/saucy/libgphoto2/saucy-proposed

« back to all changes in this revision

Viewing changes to libgphoto2_port/libusb1/libusb1.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-07-31 07:00:09 UTC
  • mfrom: (1.4.4)
  • Revision ID: package-import@ubuntu.com-20130731070009-enrbvg3hry64cxl1
Tags: 2.5.2-0ubuntu1
* New upstream release.
* Drop 01-increase_max_entries.patch, 02-libusbx_no_debug.patch,
  03-libusbx-fixes.patch: fixed upstream.
* Add libxml2-dev build dependency for new optional features.
* ABI changes: libgphoto2-2 → libgphoto2-6,
  libgphoto2-port0 → libgphoto2-port10
* debian/libgphoto2-dev-doc.install: Adjust to changed HTML API doc folder
  name.
* debian/libgphoto2-port10.install: Adjust for changed libgphoto-port ABI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "config.h"
24
24
#include <gphoto2/gphoto2-port-library.h>
25
25
 
 
26
/* Workaround for GNU/kFreeBSD */
 
27
#if defined(__FreeBSD_kernel__)
 
28
#ifndef ENODATA
 
29
#define ENODATA ENOATTR
 
30
#endif
 
31
#endif
 
32
 
26
33
#include <stdlib.h>
27
34
#include <unistd.h>
28
35
#include <stdio.h>
125
132
        struct libusb_device_descriptor *descs;
126
133
 
127
134
        /* generic matcher. This will catch passed XXX,YYY entries for instance. */
128
 
        info.type = GP_PORT_USB;
129
 
        strcpy (info.name, "");
130
 
        strcpy (info.path, "^usb:");
 
135
        gp_port_info_new (&info);
 
136
        gp_port_info_set_type (info, GP_PORT_USB);
 
137
        gp_port_info_set_name (info, "");
 
138
        gp_port_info_set_path (info, "^usb:");
131
139
        CHECK (gp_port_info_list_append (list, info));
132
140
 
133
141
        if (libusb_init (&ctx) != 0) {
134
142
                gp_log (GP_LOG_ERROR, "libusb1", "libusb_init failed.");
135
143
                return GP_ERROR_IO;
136
144
        }
137
 
 
138
145
        nrofdevs = libusb_get_device_list (ctx, &devs);
139
146
        descs = malloc (sizeof(descs[0])*nrofdevs);
140
147
        for (i=0;i<nrofdevs;i++) {
201
208
         * so we can address all USB devices.
202
209
         */
203
210
        for (d = 0; d < nrofdevs; d++) {
 
211
                char path[200];
 
212
 
204
213
                /* Devices which are definitely not cameras. */
205
214
                if (    (descs[d].bDeviceClass == LIBUSB_CLASS_HUB)             ||
206
215
                        (descs[d].bDeviceClass == LIBUSB_CLASS_HID)             ||
238
247
                /* Note: We do not skip USB storage. Some devices can support both,
239
248
                 * and the Ricoh erronously reports it.
240
249
                 */ 
241
 
                info.type = GP_PORT_USB;
242
 
                strcpy (info.name, "Universal Serial Bus");
243
 
                snprintf (info.path,sizeof(info.path), "usb:%03d,%03d",
 
250
                gp_port_info_new (&info);
 
251
                gp_port_info_set_type (info, GP_PORT_USB);
 
252
                gp_port_info_set_name (info, "Universal Serial Bus");
 
253
                snprintf (path,sizeof(path), "usb:%03d,%03d",
244
254
                        libusb_get_bus_number (devs[d]),
245
255
                        libusb_get_device_address (devs[d])
246
256
                );
 
257
                gp_port_info_set_path (info, path);
247
258
                CHECK (gp_port_info_list_append (list, info));
248
259
        }
249
260
        /* This will only be added if no other device was ever added.
250
261
         * Users doing "usb:" usage will enter the regular expression matcher case. */
251
262
        if (nrofdevices == 0) {
252
 
                info.type = GP_PORT_USB;
253
 
                strcpy (info.name, "Universal Serial Bus");
254
 
                strcpy (info.path, "usb:");
 
263
                gp_port_info_new (&info);
 
264
                gp_port_info_set_type (info, GP_PORT_USB);
 
265
                gp_port_info_set_name (info, "Universal Serial Bus");
 
266
                gp_port_info_set_path (info, "usb:");
255
267
                CHECK (gp_port_info_list_append (list, info));
256
268
        }
257
269
        libusb_exit (ctx); /* should free all stuff above */
276
288
#if 0
277
289
        libusb_set_debug (port->pl->ctx, 255);
278
290
#endif
279
 
 
280
 
        return (GP_OK);
 
291
        return GP_OK;
281
292
}
282
293
 
283
294
static int
292
303
        return (GP_OK);
293
304
}
294
305
 
 
306
static int gp_port_usb_find_path_lib(GPPort *port);
295
307
static int
296
308
gp_port_usb_open (GPPort *port)
297
309
{
298
310
        int ret;
299
311
 
300
312
        gp_log (GP_LOG_DEBUG,"libusb1","gp_port_usb_open()");
301
 
        if (!port || !port->pl->d)
 
313
        if (!port)
302
314
                return GP_ERROR_BAD_PARAMETERS;
303
315
 
 
316
        if (!port->pl->d) {
 
317
                gp_port_usb_find_path_lib(port);
 
318
                if (!port->pl->d)
 
319
                        return GP_ERROR_BAD_PARAMETERS;
 
320
        }
 
321
 
304
322
        ret = libusb_open (port->pl->d, &port->pl->dh);
305
323
        if (ret) {
306
324
                gp_log (GP_LOG_ERROR, "libusb1", "libusb_open returned %d", ret);
443
461
{
444
462
        int ret, curread;
445
463
 
446
 
        if (!port || !port->pl->dh)
 
464
        if (!port || !port->pl->dh) {
 
465
                gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_read: bad parameters");
447
466
                return GP_ERROR_BAD_PARAMETERS;
 
467
        }
448
468
 
 
469
        gp_log (GP_LOG_DEBUG, "libusb1", "reading with timeout %d", port->timeout);
449
470
        ret = libusb_bulk_transfer (port->pl->dh, port->settings.usb.inep,
450
471
                             (unsigned char*)bytes, size, &curread, port->timeout);
 
472
        gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
451
473
        if (ret < 0)
452
474
                return GP_ERROR_IO_READ;
453
475
 
455
477
}
456
478
 
457
479
static int
 
480
gp_port_usb_reset(GPPort *port)
 
481
{
 
482
        int ret, curread;
 
483
 
 
484
        if (!port || !port->pl->dh) {
 
485
                gp_log (GP_LOG_ERROR, "libusb1", "gp_port_usb_reset: bad parameters");
 
486
                return GP_ERROR_BAD_PARAMETERS;
 
487
        }
 
488
 
 
489
        gp_log (GP_LOG_DEBUG, "libusb1", "reseting");
 
490
        ret = libusb_reset_device (port->pl->dh);
 
491
        gp_log (GP_LOG_DEBUG, "libusb1", "ret = %d", ret);
 
492
        if (ret < 0)
 
493
                return GP_ERROR_IO_READ;
 
494
        return GP_OK;
 
495
}
 
496
 
 
497
static int
458
498
gp_port_usb_check_int (GPPort *port, char *bytes, int size, int timeout)
459
499
{
460
500
        int ret, curread;
573
613
        if (!port)
574
614
                return GP_ERROR_BAD_PARAMETERS;
575
615
 
576
 
        gp_log (GP_LOG_DEBUG, "libusb1", "gp_port_usb_update(old int=%d, conf=%d, alt=%d), (new int=%d, conf=%d, alt=%d)",
 
616
        gp_log (GP_LOG_DEBUG, "libusb1", "gp_port_usb_update(old int=%d, conf=%d, alt=%d) port %s, (new int=%d, conf=%d, alt=%d) port %s",
577
617
                port->settings.usb.interface,
578
618
                port->settings.usb.config,
579
619
                port->settings.usb.altsetting,
 
620
                port->settings.usb.port,
580
621
                port->settings_pending.usb.interface,
581
622
                port->settings_pending.usb.config,
582
 
                port->settings_pending.usb.altsetting
 
623
                port->settings_pending.usb.altsetting,
 
624
                port->settings_pending.usb.port
583
625
        );
584
626
 
585
 
/* do not set it here, otherwise stv680 doesnt work 
 
627
/* do not overwrite it ... we need to set it.
586
628
        if (port->pl->interface == -1) port->pl->interface = port->settings.usb.interface;
587
629
        if (port->pl->config == -1) port->pl->config = port->settings.usb.config;
588
630
        if (port->pl->altsetting == -1) port->pl->altsetting = port->settings.usb.altsetting;
755
797
}
756
798
 
757
799
static int
 
800
gp_port_usb_find_path_lib(GPPort *port)
 
801
{
 
802
        char *s;
 
803
        int d, busnr = 0, devnr = 0;
 
804
        GPPortPrivateLibrary *pl = port->pl;
 
805
 
 
806
        if (!port)
 
807
                return (GP_ERROR_BAD_PARAMETERS);
 
808
 
 
809
        s = strchr (port->settings.usb.port,':');
 
810
        if (s && (s[1] != '\0')) { /* usb:%d,%d */
 
811
                if (sscanf (s+1, "%d,%d", &busnr, &devnr) != 2)
 
812
                        return GP_ERROR_BAD_PARAMETERS;
 
813
        } else {
 
814
                return GP_ERROR_BAD_PARAMETERS;
 
815
        }
 
816
 
 
817
        pl->nrofdevs = load_devicelist (port->pl);
 
818
 
 
819
        for (d = 0; d < pl->nrofdevs; d++) {
 
820
                struct libusb_config_descriptor *confdesc;
 
821
                int ret;
 
822
                int config = -1, interface = -1, altsetting = -1;
 
823
 
 
824
                if (busnr != libusb_get_bus_number (pl->devs[d]))
 
825
                        continue;
 
826
                if (devnr != libusb_get_device_address (pl->devs[d]))
 
827
                        continue;
 
828
 
 
829
                port->pl->d = pl->devs[d];
 
830
 
 
831
                gp_log (GP_LOG_VERBOSE, "libusb1", "Found path %s", port->settings.usb.port); 
 
832
 
 
833
                /* Use the first config, interface and altsetting we find */
 
834
                gp_port_usb_find_first_altsetting(pl->devs[d], &config, &interface, &altsetting);
 
835
 
 
836
                ret = libusb_get_config_descriptor (pl->devs[d], config, &confdesc);
 
837
                if (ret)
 
838
                        continue;
 
839
 
 
840
                /* Set the defaults */
 
841
                port->settings.usb.config = confdesc->bConfigurationValue;
 
842
                port->settings.usb.interface = confdesc->interface[interface].altsetting[altsetting].bInterfaceNumber;
 
843
                port->settings.usb.altsetting = confdesc->interface[interface].altsetting[altsetting].bAlternateSetting;
 
844
 
 
845
                port->settings.usb.inep  = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_BULK);
 
846
                port->settings.usb.outep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK);
 
847
                port->settings.usb.intep = gp_port_usb_find_ep(pl->devs[d], config, interface, altsetting, LIBUSB_ENDPOINT_IN, LIBUSB_TRANSFER_TYPE_INTERRUPT);
 
848
 
 
849
                port->settings.usb.maxpacketsize = libusb_get_max_packet_size (pl->devs[d], port->settings.usb.inep);
 
850
                gp_log (GP_LOG_VERBOSE, "libusb1",
 
851
                        "Detected defaults: config %d, "
 
852
                        "interface %d, altsetting %d, "
 
853
                        "inep %02x, outep %02x, intep %02x, "
 
854
                        "class %02x, subclass %02x",
 
855
                        port->settings.usb.config,
 
856
                        port->settings.usb.interface,
 
857
                        port->settings.usb.altsetting,
 
858
                        port->settings.usb.inep,
 
859
                        port->settings.usb.outep,
 
860
                        port->settings.usb.intep,
 
861
                        confdesc->interface[interface].altsetting[altsetting].bInterfaceClass,
 
862
                        confdesc->interface[interface].altsetting[altsetting].bInterfaceSubClass
 
863
                        );
 
864
                libusb_free_config_descriptor (confdesc);
 
865
                return GP_OK;
 
866
        }
 
867
#if 0
 
868
        gp_port_set_error (port, _("Could not find USB device "
 
869
                "(vendor 0x%x, product 0x%x). Make sure this device "
 
870
                "is connected to the computer."), idvendor, idproduct);
 
871
#endif
 
872
        return GP_ERROR_IO_USB_FIND;
 
873
}
 
874
static int
758
875
gp_port_usb_find_device_lib(GPPort *port, int idvendor, int idproduct)
759
876
{
760
877
        char *s;
1167
1284
        ops->open   = gp_port_usb_open;
1168
1285
        ops->close  = gp_port_usb_close;
1169
1286
        ops->read   = gp_port_usb_read;
 
1287
        ops->reset  = gp_port_usb_reset;
1170
1288
        ops->write  = gp_port_usb_write;
1171
1289
        ops->check_int = gp_port_usb_check_int;
1172
1290
        ops->update = gp_port_usb_update;