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

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

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