OLD | NEW |
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 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 else { | 534 else { |
535 // Check whether the device is no USB hub but a PTP. | 535 // Check whether the device is no USB hub but a PTP. |
536 if ( dev->config != NULL &&dev->config->interface->altsetting->bInte
rfaceClass == LIBUSB_CLASS_PTP && dev->descriptor.bDeviceClass != LIBUSB_CLASS_H
UB ) { | 536 if ( dev->config != NULL &&dev->config->interface->altsetting->bInte
rfaceClass == LIBUSB_CLASS_PTP && dev->descriptor.bDeviceClass != LIBUSB_CLASS_H
UB ) { |
537 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev,
bus->location); | 537 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev,
bus->location); |
538 } | 538 } |
539 } | 539 } |
540 */ | 540 */ |
541 } | 541 } |
542 } | 542 } |
543 } | 543 } |
| 544 libusb_free_device_list (devs, 0); |
544 | 545 |
545 /* If nothing was found we end up here. */ | 546 /* If nothing was found we end up here. */ |
546 if(*mtp_device_list == NULL) { | 547 if(*mtp_device_list == NULL) { |
547 return LIBMTP_ERROR_NO_DEVICE_ATTACHED; | 548 return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
548 } | 549 } |
549 return LIBMTP_ERROR_NONE; | 550 return LIBMTP_ERROR_NONE; |
550 } | 551 } |
551 | 552 |
552 /** | 553 /** |
553 * Checks if a specific device with a certain bus and device | 554 * Checks if a specific device with a certain bus and device |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
962 towrite = size-curwrite; | 963 towrite = size-curwrite; |
963 if (towrite > CONTEXT_BLOCK_SIZE) { | 964 if (towrite > CONTEXT_BLOCK_SIZE) { |
964 towrite = CONTEXT_BLOCK_SIZE; | 965 towrite = CONTEXT_BLOCK_SIZE; |
965 } else { | 966 } else { |
966 // This magic makes packets the same size that WMP send them. | 967 // This magic makes packets the same size that WMP send them. |
967 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) { |
968 towrite -= towrite % ptp_usb->outep_maxpacket; | 969 towrite -= towrite % ptp_usb->outep_maxpacket; |
969 } | 970 } |
970 } | 971 } |
971 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrit
e); | 972 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrit
e); |
972 if (getfunc_ret != PTP_RC_OK) | 973 if (getfunc_ret != PTP_RC_OK) { |
| 974 free(bytes); |
973 return getfunc_ret; | 975 return getfunc_ret; |
| 976 } |
974 while (usbwritten < towrite) { | 977 while (usbwritten < towrite) { |
975 ret = USB_BULK_WRITE(ptp_usb->handle, | 978 ret = USB_BULK_WRITE(ptp_usb->handle, |
976 ptp_usb->outep, | 979 ptp_usb->outep, |
977 bytes+usbwritten, | 980 bytes+usbwritten, |
978 towrite-usbwritten, | 981 towrite-usbwritten, |
979 &xwritten, | 982 &xwritten, |
980 ptp_usb->timeout); | 983 ptp_usb->timeout); |
981 | 984 |
982 LIBMTP_USB_DEBUG("USB OUT==>\n"); | 985 LIBMTP_USB_DEBUG("USB OUT==>\n"); |
983 | 986 |
984 if (ret != LIBUSB_SUCCESS) { | 987 if (ret != LIBUSB_SUCCESS) { |
| 988 free(bytes); |
985 return PTP_ERROR_IO; | 989 return PTP_ERROR_IO; |
986 } | 990 } |
987 LIBMTP_USB_DATA(bytes+usbwritten, xwritten, 16); | 991 LIBMTP_USB_DATA(bytes+usbwritten, xwritten, 16); |
988 // check for result == 0 perhaps too. | 992 // check for result == 0 perhaps too. |
989 // Increase counters | 993 // Increase counters |
990 ptp_usb->current_transfer_complete += xwritten; | 994 ptp_usb->current_transfer_complete += xwritten; |
991 curwrite += xwritten; | 995 curwrite += xwritten; |
992 usbwritten += xwritten; | 996 usbwritten += xwritten; |
993 } | 997 } |
994 // call callback | 998 // call callback |
995 if (ptp_usb->callback_active) { | 999 if (ptp_usb->callback_active) { |
996 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total)
{ | 1000 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total)
{ |
997 // send last update and disable callback. | 1001 // send last update and disable callback. |
998 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; | 1002 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
999 ptp_usb->callback_active = 0; | 1003 ptp_usb->callback_active = 0; |
1000 } | 1004 } |
1001 if (ptp_usb->current_transfer_callback != NULL) { | 1005 if (ptp_usb->current_transfer_callback != NULL) { |
1002 int ret; | 1006 int ret; |
1003 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_compl
ete, | 1007 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_compl
ete, |
1004 ptp_usb->current_transfer_total
, | 1008 ptp_usb->current_transfer_total
, |
1005 ptp_usb->current_transfer_callb
ack_data); | 1009 ptp_usb->current_transfer_callb
ack_data); |
1006 if (ret != 0) { | 1010 if (ret != 0) { |
| 1011 free(bytes); |
1007 return PTP_ERROR_CANCEL; | 1012 return PTP_ERROR_CANCEL; |
1008 } | 1013 } |
1009 } | 1014 } |
1010 } | 1015 } |
1011 if (xwritten < towrite) /* short writes happen */ | 1016 if (xwritten < towrite) /* short writes happen */ |
1012 break; | 1017 break; |
1013 } | 1018 } |
1014 free (bytes); | 1019 free (bytes); |
1015 if (written) { | 1020 if (written) { |
1016 *written = curwrite; | 1021 *written = curwrite; |
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2029 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
"); |
2030 return LIBMTP_ERROR_CONNECTING; | 2035 return LIBMTP_ERROR_CONNECTING; |
2031 } | 2036 } |
2032 | 2037 |
2033 /* Copy USB version number */ | 2038 /* Copy USB version number */ |
2034 ptp_usb->bcdusb = desc.bcdUSB; | 2039 ptp_usb->bcdusb = desc.bcdUSB; |
2035 | 2040 |
2036 /* Attempt to initialize this device */ | 2041 /* Attempt to initialize this device */ |
2037 if (init_ptp_usb(params, ptp_usb, ldevice) < 0) { | 2042 if (init_ptp_usb(params, ptp_usb, ldevice) < 0) { |
2038 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n"); | 2043 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n"); |
| 2044 libusb_free_device_list (devs, 0); |
2039 return LIBMTP_ERROR_CONNECTING; | 2045 return LIBMTP_ERROR_CONNECTING; |
2040 } | 2046 } |
2041 | 2047 |
2042 /* | 2048 /* |
2043 * This works in situations where previous bad applications | 2049 * This works in situations where previous bad applications |
2044 * have not used LIBMTP_Release_Device on exit | 2050 * have not used LIBMTP_Release_Device on exit |
2045 */ | 2051 */ |
2046 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { | 2052 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { |
2047 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"); |
2048 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n"); | 2054 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n"); |
2049 libusb_reset_device (ptp_usb->handle); | 2055 libusb_reset_device (ptp_usb->handle); |
2050 close_usb(ptp_usb); | 2056 close_usb(ptp_usb); |
2051 | 2057 |
2052 if(init_ptp_usb(params, ptp_usb, ldevice) <0) { | 2058 if(init_ptp_usb(params, ptp_usb, ldevice) <0) { |
2053 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"); |
| 2060 libusb_free_device_list (devs, 0); |
2054 return LIBMTP_ERROR_CONNECTING; | 2061 return LIBMTP_ERROR_CONNECTING; |
2055 } | 2062 } |
2056 | 2063 |
2057 /* Device has been reset, try again */ | 2064 /* Device has been reset, try again */ |
2058 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { | 2065 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { |
2059 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"); |
| 2067 libusb_free_device_list (devs, 0); |
2060 return LIBMTP_ERROR_CONNECTING; | 2068 return LIBMTP_ERROR_CONNECTING; |
2061 } | 2069 } |
2062 } | 2070 } |
2063 | 2071 |
2064 /* Was the transaction id invalid? Try again */ | 2072 /* Was the transaction id invalid? Try again */ |
2065 if (ret == PTP_RC_InvalidTransactionID) { | 2073 if (ret == PTP_RC_InvalidTransactionID) { |
2066 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"); |
2067 params->transaction_id += 10; | 2075 params->transaction_id += 10; |
2068 ret = ptp_opensession(params, 1); | 2076 ret = ptp_opensession(params, 1); |
2069 } | 2077 } |
2070 | 2078 |
2071 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { | 2079 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
2072 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! " | 2080 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! " |
2073 "(Return code %d)\n Try to reset the device.\n", | 2081 "(Return code %d)\n Try to reset the device.\n", |
2074 ret); | 2082 ret); |
2075 libusb_release_interface(ptp_usb->handle, ptp_usb->interface); | 2083 libusb_release_interface(ptp_usb->handle, ptp_usb->interface); |
| 2084 libusb_free_device_list (devs, 0); |
2076 return LIBMTP_ERROR_CONNECTING; | 2085 return LIBMTP_ERROR_CONNECTING; |
2077 } | 2086 } |
2078 | 2087 |
2079 /* OK configured properly */ | 2088 /* OK configured properly */ |
2080 *usbinfo = (void *) ptp_usb; | 2089 *usbinfo = (void *) ptp_usb; |
| 2090 libusb_free_device_list (devs, 0); |
2081 return LIBMTP_ERROR_NONE; | 2091 return LIBMTP_ERROR_NONE; |
2082 } | 2092 } |
2083 | 2093 |
2084 | 2094 |
2085 void close_device (PTP_USB *ptp_usb, PTPParams *params) | 2095 void close_device (PTP_USB *ptp_usb, PTPParams *params) |
2086 { | 2096 { |
2087 if (ptp_closesession(params)!=PTP_RC_OK) | 2097 if (ptp_closesession(params)!=PTP_RC_OK) |
2088 LIBMTP_ERROR("ERROR: Could not close session!\n"); | 2098 LIBMTP_ERROR("ERROR: Could not close session!\n"); |
2089 close_usb(ptp_usb); | 2099 close_usb(ptp_usb); |
2090 } | 2100 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2132 { | 2142 { |
2133 return libusb_control_transfer(ptp_usb->handle, | 2143 return libusb_control_transfer(ptp_usb->handle, |
2134 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT, | 2144 LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_ENDPOINT, |
2135 LIBUSB_REQUEST_GET_STATUS, | 2145 LIBUSB_REQUEST_GET_STATUS, |
2136 USB_FEATURE_HALT, | 2146 USB_FEATURE_HALT, |
2137 ep, | 2147 ep, |
2138 (unsigned char *) status, | 2148 (unsigned char *) status, |
2139 2, | 2149 2, |
2140 ptp_usb->timeout); | 2150 ptp_usb->timeout); |
2141 } | 2151 } |
OLD | NEW |