Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: src/libusb1-glue.c

Issue 2345493002: Uprev libmtp to 1.1.12 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libmtp@master
Patch Set: Re-upload cl Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/libusb-glue.c ('k') | src/mtpz.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * \file libusb1-glue.c 2 * \file libusb1-glue.c
3 * Low-level USB interface glue towards libusb. 3 * Low-level USB interface glue towards libusb.
4 * 4 *
5 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com> 5 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
6 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> 6 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se>
7 * Copyright (C) 2006-2012 Marcus Meissner 7 * Copyright (C) 2006-2012 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock 8 * Copyright (C) 2007 Ted Bullock
9 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com> 9 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
10 * 10 *
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 /* Internal data types */ 70 /* Internal data types */
71 struct mtpdevice_list_struct { 71 struct mtpdevice_list_struct {
72 libusb_device *device; 72 libusb_device *device;
73 PTPParams *params; 73 PTPParams *params;
74 PTP_USB *ptp_usb; 74 PTP_USB *ptp_usb;
75 uint32_t bus_location; 75 uint32_t bus_location;
76 struct mtpdevice_list_struct *next; 76 struct mtpdevice_list_struct *next;
77 }; 77 };
78 typedef struct mtpdevice_list_struct mtpdevice_list_t; 78 typedef struct mtpdevice_list_struct mtpdevice_list_t;
79 79
80 struct ptp_event_cb_data {
81 PTPEventCbFn cb;
82 void *user_data;
83 PTPParams *params;
84 };
85
80 static const LIBMTP_device_entry_t mtp_device_table[] = { 86 static const LIBMTP_device_entry_t mtp_device_table[] = {
81 /* We include an .h file which is shared between us and libgphoto2 */ 87 /* We include an .h file which is shared between us and libgphoto2 */
82 #include "music-players.h" 88 #include "music-players.h"
83 }; 89 };
84 static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMT P_device_entry_t); 90 static const int mtp_device_table_size =
91 sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
85 92
86 // Local functions 93 // Local functions
87 static LIBMTP_error_number_t init_usb(); 94 static LIBMTP_error_number_t init_usb();
88 static void close_usb(PTP_USB* ptp_usb); 95 static void close_usb(PTP_USB* ptp_usb);
89 static int find_interface_and_endpoints(libusb_device *dev, 96 static int find_interface_and_endpoints(libusb_device *dev,
90 uint8_t *conf, 97 uint8_t *conf,
91 uint8_t *interface, 98 uint8_t *interface,
92 uint8_t *altsetting, 99 uint8_t *altsetting,
93 int* inep, 100 int* inep,
94 int* inep_maxpacket, 101 int* inep_maxpacket,
95 int* outep, 102 int* outep,
96 int* outep_maxpacket, 103 int* outep_maxpacket,
97 int* intep); 104 int* intep);
98 static void clear_stall(PTP_USB* ptp_usb); 105 static void clear_stall(PTP_USB* ptp_usb);
99 static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, libusb_device* dev ); 106 static int init_ptp_usb(PTPParams* params,
100 static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned l ong*); 107 » » PTP_USB* ptp_usb, libusb_device* dev);
101 static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned lo ng*,int); 108 static short ptp_write_func(unsigned long,
102 static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status); 109 » » PTPDataHandler*, void *data, unsigned long*);
110 static short ptp_read_func (unsigned long,
111 » » PTPDataHandler*, void *data, unsigned long*, int);
112 static int usb_get_endpoint_status(PTP_USB* ptp_usb,
113 » » int ep, uint16_t* status);
103 114
104 /** 115 /**
105 * Get a list of the supported USB devices. 116 * Get a list of the supported USB devices.
106 * 117 *
107 * The developers depend on users of this library to constantly 118 * The developers depend on users of this library to constantly
108 * add in to the list of supported devices. What we need is the 119 * add in to the list of supported devices. What we need is the
109 * device name, USB Vendor ID (VID) and USB Product ID (PID). 120 * device name, USB Vendor ID (VID) and USB Product ID (PID).
110 * put this into a bug ticket at the project homepage, please. 121 * put this into a bug ticket at the project homepage, please.
111 * The VID/PID is used to let e.g. udev lift the device to 122 * The VID/PID is used to let e.g. udev lift the device to
112 * console userspace access when it's plugged in. 123 * console userspace access when it's plugged in.
113 * 124 *
114 * @param devices a pointer to a pointer that will hold a device 125 * @param devices a pointer to a pointer that will hold a device
115 * list after the call to this function, if it was 126 * list after the call to this function, if it was
116 * successful. 127 * successful.
117 * @param numdevs a pointer to an integer that will hold the number 128 * @param numdevs a pointer to an integer that will hold the number
118 * of devices in the device list if the call was successful. 129 * of devices in the device list if the call was successful.
119 * @return 0 if the list was successfull retrieved, any other 130 * @return 0 if the list was successfull retrieved, any other
120 * value means failure. 131 * value means failure.
121 */ 132 */
122 int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, in t * const numdevs) 133 int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices,
134 » » » » int * const numdevs)
123 { 135 {
124 *devices = (LIBMTP_device_entry_t *) &mtp_device_table; 136 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
125 *numdevs = mtp_device_table_size; 137 *numdevs = mtp_device_table_size;
126 return 0; 138 return 0;
127 } 139 }
128 140
129 141
130 static LIBMTP_error_number_t init_usb() 142 static LIBMTP_error_number_t init_usb()
131 { 143 {
144 static int libusb1_initialized = 0;
145
132 /* 146 /*
133 * Some additional libusb debugging please. 147 * Some additional libusb debugging please.
134 * We use the same level debug between MTP and USB. 148 * We use the same level debug between MTP and USB.
135 */ 149 */
150 if (libusb1_initialized)
151 return LIBMTP_ERROR_NONE;
152
136 if (libusb_init(NULL) < 0) { 153 if (libusb_init(NULL) < 0) {
137 LIBMTP_ERROR("Libusb1 init failed\n"); 154 LIBMTP_ERROR("Libusb1 init failed\n");
138 return LIBMTP_ERROR_USB_LAYER; 155 return LIBMTP_ERROR_USB_LAYER;
139 } 156 }
140 157
158 libusb1_initialized = 1;
159
141 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0) 160 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
142 libusb_set_debug(NULL,9); 161 libusb_set_debug(NULL,9);
143 return LIBMTP_ERROR_NONE; 162 return LIBMTP_ERROR_NONE;
144 } 163 }
145 164
146 /** 165 /**
147 * Small recursive function to append a new usb_device to the linked list of 166 * Small recursive function to append a new usb_device to the linked
148 * USB MTP devices 167 * list of USB MTP devices
149 * @param devlist dynamic linked list of pointers to usb devices with MTP 168 * @param devlist dynamic linked list of pointers to usb devices with
150 * properties, to be extended with new device. 169 * MTP properties, to be extended with new device.
151 * @param newdevice the new device to add. 170 * @param newdevice the new device to add.
152 * @param bus_location bus for this device. 171 * @param bus_location bus for this device.
153 * @return an extended array or NULL on failure. 172 * @return an extended array or NULL on failure.
154 */ 173 */
155 static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist, 174 static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
156 libusb_device *newdevice, 175 libusb_device *newdevice,
157
158 uint32_t bus_location) 176 uint32_t bus_location)
159 { 177 {
160 mtpdevice_list_t *new_list_entry; 178 mtpdevice_list_t *new_list_entry;
161 179
162 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t)); 180 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
163 if (new_list_entry == NULL) { 181 if (new_list_entry == NULL) {
164 return NULL; 182 return NULL;
165 } 183 }
166 // Fill in USB device, if we *HAVE* to make a copy of the device do it here. 184 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
167 new_list_entry->device = newdevice; 185 new_list_entry->device = newdevice;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 /* 303 /*
286 * We only want to probe for the OS descriptor if the 304 * We only want to probe for the OS descriptor if the
287 * device is LIBUSB_CLASS_VENDOR_SPEC or one of the interfaces 305 * device is LIBUSB_CLASS_VENDOR_SPEC or one of the interfaces
288 * in it is, so flag if we find an interface like this. 306 * in it is, so flag if we find an interface like this.
289 */ 307 */
290 if (intf->bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC) { 308 if (intf->bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC) {
291 found_vendor_spec_interface = 1; 309 found_vendor_spec_interface = 1;
292 } 310 }
293 311
294 /* 312 /*
295 » * Check for Still Image Capture class with PIMA 15740 protocol, 313 » * TODO: Check for Still Image Capture class with PIMA 15740
296 » * also known as PTP 314 » * protocol, also known as PTP
297 */ 315 */
298 if (intf->bInterfaceClass == LIBUSB_CLASS_PTP 316 if (intf->bInterfaceClass == LIBUSB_CLASS_PTP
299 && intf->bInterfaceSubClass == 0x01 317 && intf->bInterfaceSubClass == 0x01
300 && intf->bInterfaceProtocol == 0x01) { 318 && intf->bInterfaceProtocol == 0x01) {
301 if (dumpfile != NULL) { 319 if (dumpfile != NULL) {
302 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\ n", i, j, k); 320 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\ n", i, j, k);
303 fprintf(dumpfile, " Found PTP device, check vendor " 321 fprintf(dumpfile, " Found PTP device, check vendor "
304 "extension...\n"); 322 "extension...\n");
305 } 323 }
306 libusb_close(devh); 324 libusb_close(devh);
(...skipping 24 matching lines...) Expand all
331 if (libusb_kernel_driver_active(devh, config->interface[j].altsetting[ k].iInterface)) 349 if (libusb_kernel_driver_active(devh, config->interface[j].altsetting[ k].iInterface))
332 { 350 {
333 /* 351 /*
334 * Specifically avoid probing anything else than USB mass storage de vices 352 * Specifically avoid probing anything else than USB mass storage de vices
335 * and non-associated drivers in Linux. 353 * and non-associated drivers in Linux.
336 */ 354 */
337 if (config->interface[j].altsetting[k].bInterfaceClass != 355 if (config->interface[j].altsetting[k].bInterfaceClass !=
338 LIBUSB_CLASS_MASS_STORAGE) { 356 LIBUSB_CLASS_MASS_STORAGE) {
339 LIBMTP_INFO("avoid probing device using attached kernel interface\ n"); 357 LIBMTP_INFO("avoid probing device using attached kernel interface\ n");
340 libusb_free_config_descriptor(config); 358 libusb_free_config_descriptor(config);
359 libusb_close(devh);
341 return 0; 360 return 0;
342 } 361 }
343 } 362 }
344 } 363 }
345 } 364 }
346 libusb_free_config_descriptor(config); 365 libusb_free_config_descriptor(config);
347 } 366 }
348 367
349 /* 368 /*
350 * Only probe for OS descriptor if the device is vendor specific 369 * Only probe for OS descriptor if the device is vendor specific
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 libusb_device **devs = NULL; 583 libusb_device **devs = NULL;
565 int i; 584 int i;
566 LIBMTP_error_number_t init_usb_ret; 585 LIBMTP_error_number_t init_usb_ret;
567 586
568 init_usb_ret = init_usb(); 587 init_usb_ret = init_usb();
569 if (init_usb_ret != LIBMTP_ERROR_NONE) 588 if (init_usb_ret != LIBMTP_ERROR_NONE)
570 return 0; 589 return 0;
571 590
572 nrofdevs = libusb_get_device_list (NULL, &devs); 591 nrofdevs = libusb_get_device_list (NULL, &devs);
573 for (i = 0; i < nrofdevs ; i++ ) { 592 for (i = 0; i < nrofdevs ; i++ ) {
574
575 if (libusb_get_bus_number(devs[i]) != busno) 593 if (libusb_get_bus_number(devs[i]) != busno)
576 continue; 594 » continue;
577 if (libusb_get_device_address(devs[i]) != devno) 595 if (libusb_get_device_address(devs[i]) != devno)
578 continue; 596 » continue;
579 597 if (probe_device_descriptor(devs[i], NULL))
580 if (probe_device_descriptor(devs[i], NULL))
581 return 1; 598 return 1;
582 } 599 }
583 return 0; 600 return 0;
584 } 601 }
585 602
586 /** 603 /**
587 * Detect the raw MTP device descriptors and return a list of 604 * Detect the raw MTP device descriptors and return a list of
588 * of the devices found. 605 * of the devices found.
589 * 606 *
590 * @param devices a pointer to a variable that will hold 607 * @param devices a pointer to a variable that will hold
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n", 681 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
665 i, 682 i,
666 desc.idVendor, 683 desc.idVendor,
667 desc.idProduct, 684 desc.idProduct,
668 mtp_device_table[j].vendor, 685 mtp_device_table[j].vendor,
669 mtp_device_table[j].product); 686 mtp_device_table[j].product);
670 break; 687 break;
671 } 688 }
672 } 689 }
673 if (!device_known) { 690 if (!device_known) {
674 // This device is unknown to the developers 691 device_unknown(i, desc.idVendor, desc.idProduct);
675 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
676 » i,
677 » desc.idVendor,
678 » desc.idProduct);
679 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
680 » "libmtp development team\n");
681 /*
682 * Trying to get iManufacturer or iProduct from the device at this
683 * point would require opening a device handle, that we don't want
684 * to do right now. (Takes time for no good enough reason.)
685 */
686 } 692 }
687 // Save the location on the bus 693 // Save the location on the bus
688 retdevs[i].bus_location = libusb_get_bus_number (dev->device); 694 retdevs[i].bus_location = libusb_get_bus_number (dev->device);
689 retdevs[i].devnum = libusb_get_device_address (dev->device); 695 retdevs[i].devnum = libusb_get_device_address (dev->device);
690 i++; 696 i++;
691 dev = dev->next; 697 dev = dev->next;
692 } 698 }
693 *devices = retdevs; 699 *devices = retdevs;
694 *numdevs = i; 700 *numdevs = i;
695 free_mtpdevice_list(devlist); 701 free_mtpdevice_list(devlist);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 ptp_read_func ( 823 ptp_read_func (
818 unsigned long size, PTPDataHandler *handler,void *data, 824 unsigned long size, PTPDataHandler *handler,void *data,
819 unsigned long *readbytes, 825 unsigned long *readbytes,
820 int readzero 826 int readzero
821 ) { 827 ) {
822 PTP_USB *ptp_usb = (PTP_USB *)data; 828 PTP_USB *ptp_usb = (PTP_USB *)data;
823 unsigned long toread = 0; 829 unsigned long toread = 0;
824 int ret = 0; 830 int ret = 0;
825 int xread; 831 int xread;
826 unsigned long curread = 0; 832 unsigned long curread = 0;
827 unsigned long written;
828 unsigned char *bytes; 833 unsigned char *bytes;
829 int expect_terminator_byte = 0; 834 int expect_terminator_byte = 0;
835 unsigned long usb_inep_maxpacket_size;
836 unsigned long context_block_size_1;
837 unsigned long context_block_size_2;
838 uint16_t ptp_dev_vendor_id = ptp_usb->rawdevice.device_entry.vendor_id;
830 839
840 //"iRiver" device special handling
841 if (ptp_dev_vendor_id == 0x4102 || ptp_dev_vendor_id == 0x1006) {
842 usb_inep_maxpacket_size = ptp_usb->inep_maxpacket;
843 if (usb_inep_maxpacket_size == 0x400) {
844 context_block_size_1 = CONTEXT_BLOCK_SIZE_1 - 0x200;
845 context_block_size_2 = CONTEXT_BLOCK_SIZE_2 + 0x200;
846 }
847 else {
848 context_block_size_1 = CONTEXT_BLOCK_SIZE_1;
849 context_block_size_2 = CONTEXT_BLOCK_SIZE_2;
850 }
851 }
831 // This is the largest block we'll need to read in. 852 // This is the largest block we'll need to read in.
832 bytes = malloc(CONTEXT_BLOCK_SIZE); 853 bytes = malloc(CONTEXT_BLOCK_SIZE);
833 while (curread < size) { 854 while (curread < size) {
834 855
835 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread); 856 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
836 857
837 // check equal to condition here 858 // check equal to condition here
838 if (size - curread < CONTEXT_BLOCK_SIZE) 859 if (size - curread < CONTEXT_BLOCK_SIZE)
839 { 860 {
840 // this is the last packet 861 // this is the last packet
841 toread = size - curread; 862 toread = size - curread;
842 // this is equivalent to zero read for these devices 863 // this is equivalent to zero read for these devices
843 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) { 864 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
844 toread += 1; 865 toread += 1;
845 expect_terminator_byte = 1; 866 expect_terminator_byte = 1;
846 } 867 }
847 } 868 }
848 else if (curread == 0) 869 else if (ptp_dev_vendor_id == 0x4102 || ptp_dev_vendor_id == 0x1006) {
849 // we are first packet, but not last packet 870 » //"iRiver" device special handling
850 toread = CONTEXT_BLOCK_SIZE_1; 871 » if (curread == 0)
851 else if (toread == CONTEXT_BLOCK_SIZE_1) 872 » » // we are first packet, but not last packet
852 toread = CONTEXT_BLOCK_SIZE_2; 873 » » toread = context_block_size_1;
853 else if (toread == CONTEXT_BLOCK_SIZE_2) 874 » else if (toread == context_block_size_1)
854 toread = CONTEXT_BLOCK_SIZE_1; 875 » » toread = context_block_size_2;
876 » else if (toread == context_block_size_2)
877 » » toread = context_block_size_1;
878 » else
879 » » LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
880 » » » » (unsigned int) toread, (unsigned int) (size-curr ead));
881 }
855 else 882 else
856 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n", 883 » toread = CONTEXT_BLOCK_SIZE;
857 » (unsigned int) toread, (unsigned int) (size-curread));
858 884
859 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread); 885 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
860 886
861 ret = USB_BULK_READ(ptp_usb->handle, 887 ret = USB_BULK_READ(ptp_usb->handle,
862 ptp_usb->inep, 888 ptp_usb->inep,
863 bytes, 889 bytes,
864 toread, 890 toread,
865 &xread, 891 &xread,
866 ptp_usb->timeout); 892 ptp_usb->timeout);
867 893
868 LIBMTP_USB_DEBUG("Result of read: 0x%04x (%d bytes)\n", ret, xread); 894 LIBMTP_USB_DEBUG("Result of read: 0x%04x (%d bytes)\n", ret, xread);
869 895
870 if (ret != LIBUSB_SUCCESS) 896 if (ret != LIBUSB_SUCCESS)
871 return PTP_ERROR_IO; 897 return PTP_ERROR_IO;
872 898
873 LIBMTP_USB_DEBUG("<==USB IN\n"); 899 LIBMTP_USB_DEBUG("<==USB IN\n");
874 if (xread == 0) 900 if (xread == 0)
875 LIBMTP_USB_DEBUG("Zero Read\n"); 901 LIBMTP_USB_DEBUG("Zero Read\n");
876 else 902 else
877 LIBMTP_USB_DATA(bytes, xread, 16); 903 LIBMTP_USB_DATA(bytes, xread, 16);
878 904
879 // want to discard extra byte 905 // want to discard extra byte
880 if (expect_terminator_byte && xread == toread) 906 if (expect_terminator_byte && xread == toread)
881 { 907 {
882 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n"); 908 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
883 909
884 xread--; 910 xread--;
885 } 911 }
886 912
887 int putfunc_ret = handler->putfunc(NULL, handler->priv, xread, bytes, &writt en); 913 int putfunc_ret = handler->putfunc(NULL, handler->priv, xread, bytes);
888 if (putfunc_ret != PTP_RC_OK) 914 if (putfunc_ret != PTP_RC_OK)
889 return putfunc_ret; 915 return putfunc_ret;
890 916
891 ptp_usb->current_transfer_complete += xread; 917 ptp_usb->current_transfer_complete += xread;
892 curread += xread; 918 curread += xread;
893 919
894 // Increase counters, call callback 920 // Increase counters, call callback
895 if (ptp_usb->callback_active) { 921 if (ptp_usb->callback_active) {
896 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { 922 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
897 // send last update and disable callback. 923 // send last update and disable callback.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 unsigned long curwrite = 0; 977 unsigned long curwrite = 0;
952 unsigned char *bytes; 978 unsigned char *bytes;
953 979
954 // This is the largest block we'll need to read in. 980 // This is the largest block we'll need to read in.
955 bytes = malloc(CONTEXT_BLOCK_SIZE); 981 bytes = malloc(CONTEXT_BLOCK_SIZE);
956 if (!bytes) { 982 if (!bytes) {
957 return PTP_ERROR_IO; 983 return PTP_ERROR_IO;
958 } 984 }
959 while (curwrite < size) { 985 while (curwrite < size) {
960 unsigned long usbwritten = 0; 986 unsigned long usbwritten = 0;
961 int xwritten; 987 int xwritten = 0;
962 988
963 towrite = size-curwrite; 989 towrite = size-curwrite;
964 if (towrite > CONTEXT_BLOCK_SIZE) { 990 if (towrite > CONTEXT_BLOCK_SIZE) {
965 towrite = CONTEXT_BLOCK_SIZE; 991 towrite = CONTEXT_BLOCK_SIZE;
966 } else { 992 } else {
967 // This magic makes packets the same size that WMP send them. 993 // This magic makes packets the same size that WMP send them.
968 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpack et != 0) { 994 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpack et != 0) {
969 towrite -= towrite % ptp_usb->outep_maxpacket; 995 towrite -= towrite % ptp_usb->outep_maxpacket;
970 } 996 }
971 } 997 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 if (priv->curoff + tocopy > priv->size) 1086 if (priv->curoff + tocopy > priv->size)
1061 tocopy = priv->size - priv->curoff; 1087 tocopy = priv->size - priv->curoff;
1062 memcpy (data, priv->data + priv->curoff, tocopy); 1088 memcpy (data, priv->data + priv->curoff, tocopy);
1063 priv->curoff += tocopy; 1089 priv->curoff += tocopy;
1064 *gotlen = tocopy; 1090 *gotlen = tocopy;
1065 return PTP_RC_OK; 1091 return PTP_RC_OK;
1066 } 1092 }
1067 1093
1068 static uint16_t 1094 static uint16_t
1069 memory_putfunc(PTPParams* params, void* private, 1095 memory_putfunc(PTPParams* params, void* private,
1070 » unsigned long sendlen, unsigned char *data, 1096 » unsigned long sendlen, unsigned char *data
1071 » unsigned long *putlen
1072 ) { 1097 ) {
1073 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; 1098 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1074 1099
1075 if (priv->curoff + sendlen > priv->size) { 1100 if (priv->curoff + sendlen > priv->size) {
1076 priv->data = realloc (priv->data, priv->curoff+sendlen); 1101 priv->data = realloc (priv->data, priv->curoff+sendlen);
1077 priv->size = priv->curoff + sendlen; 1102 priv->size = priv->curoff + sendlen;
1078 } 1103 }
1079 memcpy (priv->data + priv->curoff, data, sendlen); 1104 memcpy (priv->data + priv->curoff, data, sendlen);
1080 priv->curoff += sendlen; 1105 priv->curoff += sendlen;
1081 *putlen = sendlen;
1082 return PTP_RC_OK; 1106 return PTP_RC_OK;
1083 } 1107 }
1084 1108
1085 /* init private struct for receiving data. */ 1109 /* init private struct for receiving data. */
1086 static uint16_t 1110 static uint16_t
1087 ptp_init_recv_memory_handler(PTPDataHandler *handler) { 1111 ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1088 PTPMemHandlerPrivate* priv; 1112 PTPMemHandlerPrivate* priv;
1089 priv = malloc (sizeof(PTPMemHandlerPrivate)); 1113 priv = malloc (sizeof(PTPMemHandlerPrivate));
1090 handler->priv = priv; 1114 handler->priv = priv;
1091 handler->getfunc = memory_getfunc; 1115 handler->getfunc = memory_getfunc;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv; 1157 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
1134 *data = priv->data; 1158 *data = priv->data;
1135 *size = priv->size; 1159 *size = priv->size;
1136 free (priv); 1160 free (priv);
1137 return PTP_RC_OK; 1161 return PTP_RC_OK;
1138 } 1162 }
1139 1163
1140 /* send / receive functions */ 1164 /* send / receive functions */
1141 1165
1142 uint16_t 1166 uint16_t
1143 ptp_usb_sendreq (PTPParams* params, PTPContainer* req) 1167 ptp_usb_sendreq (PTPParams* params, PTPContainer* req, int dataphase)
1144 { 1168 {
1145 uint16_t ret; 1169 uint16_t ret;
1146 PTPUSBBulkContainer usbreq; 1170 PTPUSBBulkContainer usbreq;
1147 PTPDataHandler memhandler; 1171 PTPDataHandler memhandler;
1148 unsigned long written = 0; 1172 unsigned long written = 0;
1149 unsigned long towrite; 1173 unsigned long towrite;
1150 1174
1151 char txt[256]; 1175 char txt[256];
1152 1176
1153 » (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt); 1177 » (void) ptp_render_ofc (params, req->Code, sizeof(txt), txt);
1154 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt); 1178 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1155 1179
1156 /* build appropriate USB container */ 1180 /* build appropriate USB container */
1157 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN- 1181 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1158 (sizeof(uint32_t)*(5-req->Nparam))); 1182 (sizeof(uint32_t)*(5-req->Nparam)));
1159 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND); 1183 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1160 usbreq.code=htod16(req->Code); 1184 usbreq.code=htod16(req->Code);
1161 usbreq.trans_id=htod32(req->Transaction_ID); 1185 usbreq.trans_id=htod32(req->Transaction_ID);
1162 usbreq.payload.params.param1=htod32(req->Param1); 1186 usbreq.payload.params.param1=htod32(req->Param1);
1163 usbreq.payload.params.param2=htod32(req->Param2); 1187 usbreq.payload.params.param2=htod32(req->Param2);
(...skipping 18 matching lines...) Expand all
1182 "PTP: request code 0x%04x sending req wrote only %ld byt es instead of %d", 1206 "PTP: request code 0x%04x sending req wrote only %ld byt es instead of %d",
1183 req->Code, written, towrite 1207 req->Code, written, towrite
1184 ); 1208 );
1185 ret = PTP_ERROR_IO; 1209 ret = PTP_ERROR_IO;
1186 } 1210 }
1187 return ret; 1211 return ret;
1188 } 1212 }
1189 1213
1190 uint16_t 1214 uint16_t
1191 ptp_usb_senddata (PTPParams* params, PTPContainer* ptp, 1215 ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1192 » » unsigned long size, PTPDataHandler *handler 1216 » » uint64_t size, PTPDataHandler *handler
1193 ) { 1217 ) {
1194 uint16_t ret; 1218 uint16_t ret;
1195 int wlen, datawlen; 1219 int wlen, datawlen;
1196 unsigned long written; 1220 unsigned long written;
1197 PTPUSBBulkContainer usbdata; 1221 PTPUSBBulkContainer usbdata;
1198 » uint32_t bytes_left_to_transfer; 1222 » uint64_t bytes_left_to_transfer;
1199 PTPDataHandler memhandler; 1223 PTPDataHandler memhandler;
1200 1224
1201 1225
1202 LIBMTP_USB_DEBUG("SEND DATA PHASE\n"); 1226 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1203 1227
1204 /* build appropriate USB container */ 1228 /* build appropriate USB container */
1205 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size); 1229 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1206 usbdata.type = htod16(PTP_USB_CONTAINER_DATA); 1230 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1207 usbdata.code = htod16(ptp->Code); 1231 usbdata.code = htod16(ptp->Code);
1208 usbdata.trans_id= htod32(ptp->Transaction_ID); 1232 usbdata.trans_id= htod32(ptp->Transaction_ID);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 ret = PTP_ERROR_IO; 1274 ret = PTP_ERROR_IO;
1251 return ret; 1275 return ret;
1252 } 1276 }
1253 1277
1254 static uint16_t ptp_usb_getpacket(PTPParams *params, 1278 static uint16_t ptp_usb_getpacket(PTPParams *params,
1255 PTPUSBBulkContainer *packet, unsigned long *rlen) 1279 PTPUSBBulkContainer *packet, unsigned long *rlen)
1256 { 1280 {
1257 PTPDataHandler memhandler; 1281 PTPDataHandler memhandler;
1258 uint16_t ret; 1282 uint16_t ret;
1259 unsigned char *x = NULL; 1283 unsigned char *x = NULL;
1284 unsigned long packet_size;
1285 PTP_USB *ptp_usb = (PTP_USB *) params->data;
1286
1287 packet_size = ptp_usb->inep_maxpacket;
1260 1288
1261 /* read the header and potentially the first data */ 1289 /* read the header and potentially the first data */
1262 if (params->response_packet_size > 0) { 1290 if (params->response_packet_size > 0) {
1263 /* If there is a buffered packet, just use it. */ 1291 /* If there is a buffered packet, just use it. */
1264 memcpy(packet, params->response_packet, params->response_packet_ size); 1292 memcpy(packet, params->response_packet, params->response_packet_ size);
1265 *rlen = params->response_packet_size; 1293 *rlen = params->response_packet_size;
1266 free(params->response_packet); 1294 free(params->response_packet);
1267 params->response_packet = NULL; 1295 params->response_packet = NULL;
1268 params->response_packet_size = 0; 1296 params->response_packet_size = 0;
1269 /* Here this signifies a "virtual read" */ 1297 /* Here this signifies a "virtual read" */
1270 return PTP_RC_OK; 1298 return PTP_RC_OK;
1271 } 1299 }
1272 ptp_init_recv_memory_handler (&memhandler); 1300 ptp_init_recv_memory_handler (&memhandler);
1273 » ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, pa rams->data, rlen, 0); 1301 » ret = ptp_read_func(packet_size, &memhandler, params->data, rlen, 0);
1274 ptp_exit_recv_memory_handler (&memhandler, &x, rlen); 1302 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1275 if (x) { 1303 if (x) {
1276 memcpy (packet, x, *rlen); 1304 memcpy (packet, x, *rlen);
1277 free (x); 1305 free (x);
1278 } 1306 }
1279 return ret; 1307 return ret;
1280 } 1308 }
1281 1309
1282 uint16_t 1310 uint16_t
1283 ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) 1311 ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1284 { 1312 {
1285 uint16_t ret; 1313 uint16_t ret;
1286 PTPUSBBulkContainer usbdata; 1314 PTPUSBBulkContainer usbdata;
1287 unsigned long written;
1288 PTP_USB *ptp_usb = (PTP_USB *) params->data; 1315 PTP_USB *ptp_usb = (PTP_USB *) params->data;
1289 int putfunc_ret; 1316 int putfunc_ret;
1290 1317
1291 LIBMTP_USB_DEBUG("GET DATA PHASE\n"); 1318 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1292 1319
1293 memset(&usbdata,0,sizeof(usbdata)); 1320 memset(&usbdata,0,sizeof(usbdata));
1294 do { 1321 do {
1295 unsigned long len, rlen; 1322 unsigned long len, rlen;
1296 1323
1297 ret = ptp_usb_getpacket(params, &usbdata, &rlen); 1324 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
(...skipping 21 matching lines...) Expand all
1319 // getting data. It appears Windows ignores the contents of this 1346 // getting data. It appears Windows ignores the contents of this
1320 // field entirely. 1347 // field entirely.
1321 if (ret < PTP_RC_Undefined || ret > PTP_RC_Speci ficationOfDestinationUnsupported) { 1348 if (ret < PTP_RC_Undefined || ret > PTP_RC_Speci ficationOfDestinationUnsupported) {
1322 libusb_glue_debug (params, "ptp2/ptp_usb _getdata: detected a broken " 1349 libusb_glue_debug (params, "ptp2/ptp_usb _getdata: detected a broken "
1323 "PTP header, code field insan e."); 1350 "PTP header, code field insan e.");
1324 ret = PTP_ERROR_IO; 1351 ret = PTP_ERROR_IO;
1325 } 1352 }
1326 break; 1353 break;
1327 } 1354 }
1328 } 1355 }
1329 » » if (usbdata.length == 0xffffffffU) { 1356 » » if (rlen == ptp_usb->inep_maxpacket) {
1330 /* Copy first part of data to 'data' */ 1357 /* Copy first part of data to 'data' */
1331 putfunc_ret = 1358 putfunc_ret =
1332 handler->putfunc( 1359 handler->putfunc(
1333 » » » » params, handler->priv, rlen - PTP_USB_BULK_ HDR_LEN, usbdata.payload.data, 1360 » » » » params, handler->priv, rlen - PTP_USB_BULK_ HDR_LEN, usbdata.payload.data
1334 » » » » &written
1335 ); 1361 );
1336 if (putfunc_ret != PTP_RC_OK) 1362 if (putfunc_ret != PTP_RC_OK)
1337 return putfunc_ret; 1363 return putfunc_ret;
1338 1364
1339 /* stuff data directly to passed data handler */ 1365 /* stuff data directly to passed data handler */
1340 while (1) { 1366 while (1) {
1341 unsigned long readdata; 1367 unsigned long readdata;
1342 uint16_t xret; 1368 uint16_t xret;
1343 1369
1344 xret = ptp_read_func( 1370 xret = ptp_read_func(
1345 » » » » » PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, 1371 » » » » » 0x20000000,
1346 handler, 1372 handler,
1347 params->data, 1373 params->data,
1348 &readdata, 1374 &readdata,
1349 0 1375 0
1350 ); 1376 );
1351 if (xret != PTP_RC_OK) 1377 if (xret != PTP_RC_OK)
1352 return xret; 1378 return xret;
1353 » » if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) 1379 » » if (readdata < 0x20000000)
1354 break; 1380 break;
1355 } 1381 }
1356 return PTP_RC_OK; 1382 return PTP_RC_OK;
1357 } 1383 }
1358 if (rlen > dtoh32(usbdata.length)) { 1384 if (rlen > dtoh32(usbdata.length)) {
1359 /* 1385 /*
1360 * Buffer the surplus response packet if it is >= 1386 * Buffer the surplus response packet if it is >=
1361 * PTP_USB_BULK_HDR_LEN 1387 * PTP_USB_BULK_HDR_LEN
1362 * (i.e. it is probably an entire package) 1388 * (i.e. it is probably an entire package)
1363 * else discard it as erroneous surplus data. 1389 * else discard it as erroneous surplus data.
(...skipping 30 matching lines...) Expand all
1394 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN; 1420 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1395 1421
1396 /* autodetect split header/data MTP devices */ 1422 /* autodetect split header/data MTP devices */
1397 if (dtoh32(usbdata.length) > 12 && (rlen==12)) 1423 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1398 params->split_header_data = 1; 1424 params->split_header_data = 1;
1399 1425
1400 /* Copy first part of data to 'data' */ 1426 /* Copy first part of data to 'data' */
1401 putfunc_ret = 1427 putfunc_ret =
1402 handler->putfunc( 1428 handler->putfunc(
1403 params, handler->priv, rlen - PTP_USB_BULK_HD R_LEN, 1429 params, handler->priv, rlen - PTP_USB_BULK_HD R_LEN,
1404 » » » » usbdata.payload.data, 1430 » » » » usbdata.payload.data
1405 » » » » &written
1406 ); 1431 );
1407 if (putfunc_ret != PTP_RC_OK) 1432 if (putfunc_ret != PTP_RC_OK)
1408 return putfunc_ret; 1433 return putfunc_ret;
1409 1434
1410 if (FLAG_NO_ZERO_READS(ptp_usb) && 1435 if (FLAG_NO_ZERO_READS(ptp_usb) &&
1411 » » len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_R EAD) { 1436 » » len+PTP_USB_BULK_HDR_LEN == ptp_usb->inep_maxpacket) {
1412 1437
1413 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n"); 1438 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1414 1439
1415 // need to read in extra byte and discard it 1440 // need to read in extra byte and discard it
1416 int result = 0, xread; 1441 int result = 0, xread;
1417 unsigned char byte = 0; 1442 unsigned char byte = 0;
1418 result = USB_BULK_READ(ptp_usb->handle, 1443 result = USB_BULK_READ(ptp_usb->handle,
1419 ptp_usb->inep, 1444 ptp_usb->inep,
1420 &byte, 1445 &byte,
1421 1, 1446 1,
1422 &xread, 1447 &xread,
1423 ptp_usb->timeout); 1448 ptp_usb->timeout);
1424 1449
1425 if (result != 1) 1450 if (result != 1)
1426 » » LIBMTP_INFO("Could not read in extra byte for PTP_USB_BULK_H S_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result); 1451 » » LIBMTP_INFO("Could not read in extra byte for %d byte long f ile, return value 0x%04x\n", ptp_usb->inep_maxpacket, result);
1427 » » } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKE T_LEN_READ && params->split_header_data == 0) { 1452 » » } else if (len+PTP_USB_BULK_HDR_LEN == ptp_usb->inep_maxpacket & & params->split_header_data == 0) {
1428 int zeroresult = 0, xread; 1453 int zeroresult = 0, xread;
1429 unsigned char zerobyte = 0; 1454 unsigned char zerobyte = 0;
1430 1455
1431 LIBMTP_INFO("Reading in zero packet after header\n"); 1456 LIBMTP_INFO("Reading in zero packet after header\n");
1432 1457
1433 zeroresult = USB_BULK_READ(ptp_usb->handle, 1458 zeroresult = USB_BULK_READ(ptp_usb->handle,
1434 ptp_usb->inep, 1459 ptp_usb->inep,
1435 &zerobyte, 1460 &zerobyte,
1436 0, 1461 0,
1437 &xread, 1462 &xread,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 #define PTP_EVENT_CHECK 0x0000 /* waits for */ 1553 #define PTP_EVENT_CHECK 0x0000 /* waits for */
1529 #define PTP_EVENT_CHECK_FAST 0x0001 /* checks */ 1554 #define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1530 1555
1531 static inline uint16_t 1556 static inline uint16_t
1532 ptp_usb_event (PTPParams* params, PTPContainer* event, int wait) 1557 ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1533 { 1558 {
1534 uint16_t ret; 1559 uint16_t ret;
1535 int result, xread; 1560 int result, xread;
1536 unsigned long rlen; 1561 unsigned long rlen;
1537 PTPUSBEventContainer usbevent; 1562 PTPUSBEventContainer usbevent;
1538 » PTP_USB *ptp_usb = (PTP_USB *)(params->data); 1563 » PTP_USB *ptp_usb;
1539 1564
1540 memset(&usbevent,0,sizeof(usbevent)); 1565 memset(&usbevent,0,sizeof(usbevent));
1541 1566
1542 if ((params==NULL) || (event==NULL)) 1567 if ((params==NULL) || (event==NULL))
1543 return PTP_ERROR_BADPARAM; 1568 return PTP_ERROR_BADPARAM;
1569 ptp_usb = (PTP_USB *)(params->data);
1570
1544 ret = PTP_RC_OK; 1571 ret = PTP_RC_OK;
1545 switch(wait) { 1572 switch(wait) {
1546 case PTP_EVENT_CHECK: 1573 case PTP_EVENT_CHECK:
1547 result = USB_BULK_READ(ptp_usb->handle, 1574 result = USB_BULK_READ(ptp_usb->handle,
1548 ptp_usb->intep, 1575 ptp_usb->intep,
1549 (unsigned char *) &usbevent, 1576 (unsigned char *) &usbevent,
1550 sizeof(usbevent), 1577 sizeof(usbevent),
1551 &xread, 1578 &xread,
1552 0); 1579 0);
1553 if (xread == 0) 1580 if (xread == 0)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 1633
1607 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST); 1634 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1608 } 1635 }
1609 1636
1610 uint16_t 1637 uint16_t
1611 ptp_usb_event_wait (PTPParams* params, PTPContainer* event) { 1638 ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1612 1639
1613 return ptp_usb_event (params, event, PTP_EVENT_CHECK); 1640 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1614 } 1641 }
1615 1642
1643 static void
1644 ptp_usb_event_cb (struct libusb_transfer *t) {
1645 struct ptp_event_cb_data *data = t->user_data;
1646 PTPParams *params = data->params;
1647 PTPUSBEventContainer *usbevent = (void *)t->buffer;
1648 PTPContainer event = {0,};
1649 uint16_t code;
1650
1651 switch (t->status) {
1652 case LIBUSB_TRANSFER_COMPLETED:
1653 if (t->actual_length < 8) {
1654 libusb_glue_error (params,
1655 "PTP: reading event an short read of %ld bytes o ccurred\n",
1656 t->actual_length);
1657 code = PTP_ERROR_IO;
1658 } else {
1659 event.Code=dtoh16(usbevent->code);
1660 event.SessionID=params->session_id;
1661 event.Transaction_ID=dtoh32(usbevent->trans_id);
1662 event.Param1=dtoh32(usbevent->param1);
1663 event.Param2=dtoh32(usbevent->param2);
1664 event.Param3=dtoh32(usbevent->param3);
1665 code = PTP_RC_OK;
1666 }
1667 break;
1668 case LIBUSB_TRANSFER_TIMED_OUT:
1669 code = PTP_ERROR_TIMEOUT;
1670 break;
1671 case LIBUSB_TRANSFER_CANCELLED:
1672 code = PTP_ERROR_CANCEL;
1673 break;
1674 case LIBUSB_TRANSFER_STALL:
1675 code = PTP_ERROR_DATA_EXPECTED;
1676 break;
1677 case LIBUSB_TRANSFER_ERROR:
1678 case LIBUSB_TRANSFER_NO_DEVICE:
1679 case LIBUSB_TRANSFER_OVERFLOW:
1680 default:
1681 code = PTP_ERROR_IO;
1682 break;
1683 }
1684 if (code != PTP_RC_OK) {
1685 libusb_glue_error (params,
1686 "PTP: reading event an error 0x%02x occurred\n",
1687 t->status);
1688 }
1689 data->cb(params, code, &event, data->user_data);
1690 free(data);
1691 }
1692
1693 uint16_t
1694 ptp_usb_event_async (PTPParams* params, PTPEventCbFn cb, void *user_data) {
1695 PTP_USB *ptp_usb;
1696 PTPUSBEventContainer *usbevent;
1697 struct ptp_event_cb_data *data;
1698 struct libusb_transfer *t;
1699 int ret;
1700
1701 if (params == NULL) {
1702 return PTP_ERROR_BADPARAM;
1703 }
1704
1705 usbevent = calloc(1, sizeof(*usbevent));
1706 if (usbevent == NULL) {
1707 return PTP_ERROR_IO;
1708 }
1709
1710 data = malloc(sizeof(*data));
1711 if (data == NULL) {
1712 free(usbevent);
1713 return PTP_ERROR_IO;
1714 }
1715
1716 t = libusb_alloc_transfer(0);
1717 if (t == NULL) {
1718 free(data);
1719 free(usbevent);
1720 return PTP_ERROR_IO;
1721 }
1722
1723 data->cb = cb;
1724 data->user_data = user_data;
1725 data->params = params;
1726
1727 ptp_usb = (PTP_USB *)(params->data);
1728 libusb_fill_interrupt_transfer(t, ptp_usb->handle, ptp_usb->intep,
1729 (unsigned char *)usbevent, sizeof(*usbeve nt),
1730 ptp_usb_event_cb, data, 0);
1731 t->flags = LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
1732
1733 ret = libusb_submit_transfer(t);
1734 return ret == 0 ? PTP_RC_OK : PTP_ERROR_IO;
1735 }
1736
1737 /**
1738 * Trivial wrapper around the most generic libusb method for polling for events.
1739 * Can be used to drive asynchronous event detection.
1740 */
1741 int LIBMTP_Handle_Events_Timeout_Completed(struct timeval *tv, int *completed) {
1742 /* Pass NULL for context as libmtp always uses the default context */
1743 return libusb_handle_events_timeout_completed(NULL, tv, completed);
1744 }
1745
1616 uint16_t 1746 uint16_t
1617 ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) { 1747 ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1618 PTP_USB *ptp_usb = (PTP_USB *)(params->data); 1748 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1619 int ret; 1749 int ret;
1620 unsigned char buffer[6]; 1750 unsigned char buffer[6];
1621 1751
1622 htod16a(&buffer[0],PTP_EC_CancelTransaction); 1752 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1623 htod32a(&buffer[2],transactionid); 1753 htod32a(&buffer[2],transactionid);
1624 ret = libusb_control_transfer(ptp_usb->handle, 1754 ret = libusb_control_transfer(ptp_usb->handle,
1625 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTER FACE, 1755 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTER FACE,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1671 ) { 1801 ) {
1672 if (LIBUSB_SUCCESS != libusb_detach_kernel_driver(device_handle, ptp_usb-> interface)) { 1802 if (LIBUSB_SUCCESS != libusb_detach_kernel_driver(device_handle, ptp_usb-> interface)) {
1673 perror("libusb_detach_kernel_driver() failed, continuing anyway..."); 1803 perror("libusb_detach_kernel_driver() failed, continuing anyway...");
1674 } 1804 }
1675 } 1805 }
1676 1806
1677 /* 1807 /*
1678 * Check if the config is set to something else than what we want 1808 * Check if the config is set to something else than what we want
1679 * to use. Only set the configuration if we absolutely have to. 1809 * to use. Only set the configuration if we absolutely have to.
1680 * Also do not bail out if we fail. 1810 * Also do not bail out if we fail.
1811 *
1812 * Note that Darwin will not set the configuration for vendor-specific
1813 * devices so we need to go in and set it.
1681 */ 1814 */
1682 ret = libusb_get_active_config_descriptor(dev, &config); 1815 ret = libusb_get_active_config_descriptor(dev, &config);
1683 if (ret != LIBUSB_SUCCESS) { 1816 if (ret != LIBUSB_SUCCESS) {
1684 perror("libusb_get_active_config_descriptor(1) failed"); 1817 perror("libusb_get_active_config_descriptor(1) failed");
1685 return -1; 1818 fprintf(stderr, "no active configuration, trying to set configuration\n");
1819 if (libusb_set_configuration(device_handle, ptp_usb->config) != LIBUSB_SUCCE SS) {
1820 perror("libusb_set_configuration() failed, continuing anyway...");
1821 }
1822 ret = libusb_get_active_config_descriptor(dev, &config);
1823 if (ret != LIBUSB_SUCCESS) {
1824 perror("libusb_get_active_config_descriptor(2) failed");
1825 return -1;
1826 }
1686 } 1827 }
1687 if (config->bConfigurationValue != ptp_usb->config) { 1828 if (config->bConfigurationValue != ptp_usb->config) {
1688 fprintf(stderr, "desired configuration different from current, trying to set configuration\n"); 1829 fprintf(stderr, "desired configuration different from current, trying to set configuration\n");
1689 if (libusb_set_configuration(device_handle, ptp_usb->config)) { 1830 if (libusb_set_configuration(device_handle, ptp_usb->config)) {
1690 perror("libusb_set_configuration() failed, continuing anyway..."); 1831 perror("libusb_set_configuration() failed, continuing anyway...");
1691 } 1832 }
1692 /* Re-fetch the config descriptor if we changed */ 1833 /* Re-fetch the config descriptor if we changed */
1693 libusb_free_config_descriptor(config); 1834 libusb_free_config_descriptor(config);
1694 ret = libusb_get_active_config_descriptor(dev, &config); 1835 ret = libusb_get_active_config_descriptor(dev, &config);
1695 if (ret != LIBUSB_SUCCESS) { 1836 if (ret != LIBUSB_SUCCESS) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1791 } 1932 }
1792 } 1933 }
1793 1934
1794 /* check the outep status */ 1935 /* check the outep status */
1795 status=0; 1936 status=0;
1796 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status); 1937 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1797 if (ret<0) { 1938 if (ret<0) {
1798 perror("outep: usb_get_endpoint_status()"); 1939 perror("outep: usb_get_endpoint_status()");
1799 } else if (status) { 1940 } else if (status) {
1800 LIBMTP_INFO("Clearing stall on OUT endpoint\n"); 1941 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
1801 ret = libusb_clear_halt (ptp_usb->handle, ptp_usb->outep); 1942 ret = libusb_clear_halt(ptp_usb->handle, ptp_usb->outep);
1802 if (ret != LIBUSB_SUCCESS) { 1943 if (ret != LIBUSB_SUCCESS) {
1803 perror("usb_clear_stall_feature()"); 1944 perror("usb_clear_stall_feature()");
1804 } 1945 }
1805 } 1946 }
1806 1947
1807 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */ 1948 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1808 } 1949 }
1809 1950
1810 static void clear_halt(PTP_USB* ptp_usb)
1811 {
1812 int ret;
1813
1814 ret = libusb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1815 if (ret<0) {
1816 perror("usb_clear_halt() on IN endpoint");
1817 }
1818 ret = libusb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1819 if (ret<0) {
1820 perror("usb_clear_halt() on OUT endpoint");
1821 }
1822 ret = libusb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1823 if (ret<0) {
1824 perror("usb_clear_halt() on INTERRUPT endpoint");
1825 }
1826 }
1827
1828 static void close_usb(PTP_USB* ptp_usb) 1951 static void close_usb(PTP_USB* ptp_usb)
1829 { 1952 {
1830 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) { 1953 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
1831 /* 1954 /*
1832 * Clear any stalled endpoints 1955 * Clear any stalled endpoints
1833 * On misbehaving devices designed for Windows/Mac, quote from: 1956 * On misbehaving devices designed for Windows/Mac, quote from:
1834 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt 1957 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1835 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT 1958 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
1836 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command, 1959 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1837 * and presumes that the stall has cleared. Some devices actually choke 1960 * and presumes that the stall has cleared. Some devices actually choke
1838 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the 1961 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
1839 * STALL is persistant or not). 1962 * STALL is persistant or not).
1840 */ 1963 */
1841 clear_stall(ptp_usb); 1964 clear_stall(ptp_usb);
1842 // Clear halts on any endpoints
1843 clear_halt(ptp_usb);
1844 // Added to clear some stuff on the OUT endpoint
1845 // TODO: is this good on the Mac too?
1846 // HINT: some devices may need that you comment these two out too.
1847 libusb_clear_halt(ptp_usb->handle, ptp_usb->outep);
1848 libusb_release_interface(ptp_usb->handle, (int) ptp_usb->interface); 1965 libusb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1849 } 1966 }
1850 if (FLAG_FORCE_RESET_ON_CLOSE(ptp_usb)) { 1967 if (FLAG_FORCE_RESET_ON_CLOSE(ptp_usb)) {
1851 /* 1968 /*
1852 * Some devices really love to get reset after being 1969 * Some devices really love to get reset after being
1853 * disconnected. Again, since Windows never disconnects 1970 * disconnected. Again, since Windows never disconnects
1854 * a device closing behaviour is seldom or never exercised 1971 * a device closing behaviour is seldom or never exercised
1855 * on devices when engineered and often error prone. 1972 * on devices when engineered and often error prone.
1856 * Reset may help some. 1973 * Reset may help some.
1857 */ 1974 */
(...skipping 21 matching lines...) Expand all
1879 ret = libusb_get_device_descriptor(dev, &desc); 1996 ret = libusb_get_device_descriptor(dev, &desc);
1880 if (ret != LIBUSB_SUCCESS) 1997 if (ret != LIBUSB_SUCCESS)
1881 return -1; 1998 return -1;
1882 1999
1883 // Loop over the device configurations 2000 // Loop over the device configurations
1884 for (i = 0; i < desc.bNumConfigurations; i++) { 2001 for (i = 0; i < desc.bNumConfigurations; i++) {
1885 uint8_t j; 2002 uint8_t j;
1886 struct libusb_config_descriptor *config; 2003 struct libusb_config_descriptor *config;
1887 2004
1888 ret = libusb_get_config_descriptor(dev, i, &config); 2005 ret = libusb_get_config_descriptor(dev, i, &config);
1889 if (ret != 0) 2006 if (ret != LIBUSB_SUCCESS)
1890 continue; 2007 continue;
1891 2008
1892 *conf = config->bConfigurationValue; 2009 *conf = config->bConfigurationValue;
1893 2010
1894 if (ret != LIBUSB_SUCCESS) continue;
1895 // Loop over each configurations interfaces 2011 // Loop over each configurations interfaces
1896 for (j = 0; j < config->bNumInterfaces; j++) { 2012 for (j = 0; j < config->bNumInterfaces; j++) {
1897 uint8_t k, l; 2013 uint8_t k, l;
1898 uint8_t no_ep; 2014 uint8_t no_ep;
1899 int found_inep = 0; 2015 int found_inep = 0;
1900 int found_outep = 0; 2016 int found_outep = 0;
1901 int found_intep = 0; 2017 int found_intep = 0;
1902 const struct libusb_endpoint_descriptor *ep; 2018 const struct libusb_endpoint_descriptor *ep;
1903 2019
1904 // Inspect the altsettings of this interface... 2020 // Inspect the altsettings of this interface...
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
2024 &ptp_usb->interface, 2140 &ptp_usb->interface,
2025 &ptp_usb->altsetting, 2141 &ptp_usb->altsetting,
2026 &ptp_usb->inep, 2142 &ptp_usb->inep,
2027 &ptp_usb->inep_maxpacket, 2143 &ptp_usb->inep_maxpacket,
2028 &ptp_usb->outep, 2144 &ptp_usb->outep,
2029 &ptp_usb->outep_maxpacket, 2145 &ptp_usb->outep_maxpacket,
2030 &ptp_usb->intep); 2146 &ptp_usb->intep);
2031 2147
2032 if (err) { 2148 if (err) {
2033 libusb_free_device_list (devs, 0); 2149 libusb_free_device_list (devs, 0);
2150 free (ptp_usb);
2034 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n "); 2151 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n ");
2035 return LIBMTP_ERROR_CONNECTING; 2152 return LIBMTP_ERROR_CONNECTING;
2036 } 2153 }
2037 2154
2038 /* Copy USB version number */ 2155 /* Copy USB version number */
2039 ptp_usb->bcdusb = desc.bcdUSB; 2156 ptp_usb->bcdusb = desc.bcdUSB;
2040 2157
2041 /* Attempt to initialize this device */ 2158 /* Attempt to initialize this device */
2042 if (init_ptp_usb(params, ptp_usb, ldevice) < 0) { 2159 if (init_ptp_usb(params, ptp_usb, ldevice) < 0) {
2160 free (ptp_usb);
2043 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n"); 2161 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
2044 libusb_free_device_list (devs, 0); 2162 libusb_free_device_list (devs, 0);
2045 return LIBMTP_ERROR_CONNECTING; 2163 return LIBMTP_ERROR_CONNECTING;
2046 } 2164 }
2047 2165
2048 /* 2166 /*
2049 * This works in situations where previous bad applications 2167 * This works in situations where previous bad applications
2050 * have not used LIBMTP_Release_Device on exit 2168 * have not used LIBMTP_Release_Device on exit
2051 */ 2169 */
2052 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { 2170 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
2053 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after reset ting USB interface\n"); 2171 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after reset ting USB interface\n");
2054 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n"); 2172 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
2055 libusb_reset_device (ptp_usb->handle); 2173 libusb_reset_device (ptp_usb->handle);
2056 close_usb(ptp_usb); 2174 close_usb(ptp_usb);
2057 2175
2058 if(init_ptp_usb(params, ptp_usb, ldevice) <0) { 2176 if(init_ptp_usb(params, ptp_usb, ldevice) <0) {
2059 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n"); 2177 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
2060 libusb_free_device_list (devs, 0); 2178 libusb_free_device_list (devs, 0);
2179 free (ptp_usb);
2061 return LIBMTP_ERROR_CONNECTING; 2180 return LIBMTP_ERROR_CONNECTING;
2062 } 2181 }
2063 2182
2064 /* Device has been reset, try again */ 2183 /* Device has been reset, try again */
2065 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { 2184 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
2066 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n"); 2185 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
2067 libusb_free_device_list (devs, 0); 2186 libusb_free_device_list (devs, 0);
2187 free (ptp_usb);
2068 return LIBMTP_ERROR_CONNECTING; 2188 return LIBMTP_ERROR_CONNECTING;
2069 } 2189 }
2070 } 2190 }
2071 2191
2072 /* Was the transaction id invalid? Try again */ 2192 /* Was the transaction id invalid? Try again */
2073 if (ret == PTP_RC_InvalidTransactionID) { 2193 if (ret == PTP_RC_InvalidTransactionID) {
2074 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n"); 2194 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
2075 params->transaction_id += 10; 2195 params->transaction_id += 10;
2076 ret = ptp_opensession(params, 1); 2196 ret = ptp_opensession(params, 1);
2077 } 2197 }
2078 2198
2079 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { 2199 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
2080 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! " 2200 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
2081 "(Return code %d)\n Try to reset the device.\n", 2201 "(Return code %d)\n Try to reset the device.\n",
2082 ret); 2202 ret);
2083 libusb_release_interface(ptp_usb->handle, ptp_usb->interface); 2203 libusb_release_interface(ptp_usb->handle, ptp_usb->interface);
2084 libusb_free_device_list (devs, 0); 2204 libusb_free_device_list (devs, 0);
2205 free (ptp_usb);
2085 return LIBMTP_ERROR_CONNECTING; 2206 return LIBMTP_ERROR_CONNECTING;
2086 } 2207 }
2087 2208
2088 /* OK configured properly */ 2209 /* OK configured properly */
2089 *usbinfo = (void *) ptp_usb; 2210 *usbinfo = (void *) ptp_usb;
2090 libusb_free_device_list (devs, 0); 2211 libusb_free_device_list (devs, 0);
2091 return LIBMTP_ERROR_NONE; 2212 return LIBMTP_ERROR_NONE;
2092 } 2213 }
2093 2214
2094 2215
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2142 { 2263 {
2143 return libusb_control_transfer(ptp_usb->handle, 2264 return libusb_control_transfer(ptp_usb->handle,
2144 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT, 2265 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT,
2145 LIBUSB_REQUEST_GET_STATUS, 2266 LIBUSB_REQUEST_GET_STATUS,
2146 USB_FEATURE_HALT, 2267 USB_FEATURE_HALT,
2147 ep, 2268 ep,
2148 (unsigned char *) status, 2269 (unsigned char *) status,
2149 2, 2270 2,
2150 ptp_usb->timeout); 2271 ptp_usb->timeout);
2151 } 2272 }
OLDNEW
« no previous file with comments | « src/libusb-glue.c ('k') | src/mtpz.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698