2010-05-11 17:16:59 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
#define LOG_TAG "MtpClient"
|
2010-06-29 18:11:52 -04:00
|
|
|
|
|
|
|
#include "MtpDebug.h"
|
|
|
|
#include "MtpClient.h"
|
|
|
|
#include "MtpDevice.h"
|
2010-05-25 19:08:48 -04:00
|
|
|
|
2010-05-11 17:16:59 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <usbhost/usbhost.h>
|
2010-05-25 19:08:48 -04:00
|
|
|
#include <linux/version.h>
|
|
|
|
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
|
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#else
|
|
|
|
#include <linux/usb_ch9.h>
|
|
|
|
#endif
|
2010-05-11 17:16:59 -04:00
|
|
|
|
2010-05-14 10:10:36 -04:00
|
|
|
namespace android {
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
class MtpClientThread : public Thread {
|
|
|
|
private:
|
|
|
|
MtpClient* mClient;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MtpClientThread(MtpClient* client)
|
|
|
|
: mClient(client)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool threadLoop() {
|
|
|
|
return mClient->threadLoop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
MtpClient::MtpClient()
|
2010-07-01 11:32:08 -04:00
|
|
|
: mThread(NULL),
|
|
|
|
mUsbHostContext(NULL),
|
|
|
|
mDone(false)
|
2010-05-11 17:16:59 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MtpClient::~MtpClient() {
|
2010-07-01 11:32:08 -04:00
|
|
|
usb_host_cleanup(mUsbHostContext);
|
2010-05-11 17:16:59 -04:00
|
|
|
}
|
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
bool MtpClient::start() {
|
2010-07-20 09:48:35 -04:00
|
|
|
Mutex::Autolock autoLock(mMutex);
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
if (mThread)
|
2010-05-25 19:08:48 -04:00
|
|
|
return true;
|
2010-05-11 17:16:59 -04:00
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
mUsbHostContext = usb_host_init();
|
|
|
|
if (!mUsbHostContext)
|
2010-05-25 19:08:48 -04:00
|
|
|
return false;
|
2010-07-01 11:32:08 -04:00
|
|
|
|
|
|
|
mThread = new MtpClientThread(this);
|
|
|
|
mThread->run("MtpClientThread");
|
2010-07-20 09:48:35 -04:00
|
|
|
// wait for the thread to do initial device discovery before returning
|
|
|
|
mThreadStartCondition.wait(mMutex);
|
2010-07-01 11:32:08 -04:00
|
|
|
|
2010-05-11 17:16:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
void MtpClient::stop() {
|
|
|
|
mDone = true;
|
|
|
|
}
|
|
|
|
|
2010-07-20 09:48:35 -04:00
|
|
|
MtpDevice* MtpClient::getDevice(int id) {
|
|
|
|
for (int i = 0; i < mDeviceList.size(); i++) {
|
|
|
|
MtpDevice* device = mDeviceList[i];
|
|
|
|
if (device->getID() == id)
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
bool MtpClient::usbDeviceAdded(const char *devname) {
|
2010-05-25 19:08:48 -04:00
|
|
|
struct usb_descriptor_header* desc;
|
|
|
|
struct usb_descriptor_iter iter;
|
2010-05-19 10:33:39 -04:00
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
struct usb_device *device = usb_device_open(devname);
|
|
|
|
if (!device) {
|
|
|
|
LOGE("usb_device_open failed\n");
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-19 10:33:39 -04:00
|
|
|
}
|
2010-05-11 17:16:59 -04:00
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
usb_descriptor_iter_init(device, &iter);
|
|
|
|
|
|
|
|
while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
|
|
|
|
if (desc->bDescriptorType == USB_DT_INTERFACE) {
|
|
|
|
struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
|
|
|
|
|
|
|
|
if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
|
|
|
|
interface->bInterfaceSubClass == 1 && // Still Image Capture
|
|
|
|
interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
|
|
|
|
{
|
|
|
|
LOGD("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
|
|
|
|
usb_device_get_product_name(device));
|
|
|
|
|
|
|
|
// interface should be followed by three endpoints
|
|
|
|
struct usb_endpoint_descriptor *ep;
|
|
|
|
struct usb_endpoint_descriptor *ep_in_desc = NULL;
|
|
|
|
struct usb_endpoint_descriptor *ep_out_desc = NULL;
|
|
|
|
struct usb_endpoint_descriptor *ep_intr_desc = NULL;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
|
|
|
|
if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
|
|
|
|
LOGE("endpoints not found\n");
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-25 19:08:48 -04:00
|
|
|
}
|
|
|
|
if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
|
|
|
|
if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
|
|
|
|
ep_in_desc = ep;
|
|
|
|
else
|
|
|
|
ep_out_desc = ep;
|
|
|
|
} else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
|
|
|
|
ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
|
|
|
|
ep_intr_desc = ep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
|
|
|
|
LOGE("endpoints not found\n");
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-25 19:08:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
|
|
|
|
struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
|
|
|
|
struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
|
|
|
|
|
|
|
|
if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
|
|
|
|
LOGE("usb_device_claim_interface failed\n");
|
|
|
|
usb_endpoint_close(ep_in);
|
|
|
|
usb_endpoint_close(ep_out);
|
|
|
|
usb_endpoint_close(ep_intr);
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-25 19:08:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
|
|
|
|
ep_in, ep_out, ep_intr);
|
|
|
|
mDeviceList.add(mtpDevice);
|
|
|
|
mtpDevice->initialize();
|
|
|
|
deviceAdded(mtpDevice);
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-25 19:08:48 -04:00
|
|
|
}
|
|
|
|
}
|
2010-05-11 17:16:59 -04:00
|
|
|
}
|
|
|
|
|
2010-05-25 19:08:48 -04:00
|
|
|
usb_device_close(device);
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
2010-05-11 17:16:59 -04:00
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
bool MtpClient::usbDeviceRemoved(const char *devname) {
|
2010-05-25 19:08:48 -04:00
|
|
|
for (int i = 0; i < mDeviceList.size(); i++) {
|
|
|
|
MtpDevice* device = mDeviceList[i];
|
|
|
|
if (!strcmp(devname, device->getDeviceName())) {
|
|
|
|
deviceRemoved(device);
|
|
|
|
mDeviceList.removeAt(i);
|
|
|
|
delete device;
|
|
|
|
LOGD("Camera removed!\n");
|
|
|
|
break;
|
|
|
|
}
|
2010-05-19 15:12:14 -04:00
|
|
|
}
|
2010-07-01 11:32:08 -04:00
|
|
|
return mDone;
|
|
|
|
}
|
|
|
|
|
2010-07-20 09:48:35 -04:00
|
|
|
bool MtpClient::usbDiscoveryDone() {
|
|
|
|
Mutex::Autolock autoLock(mMutex);
|
|
|
|
mThreadStartCondition.signal();
|
|
|
|
return mDone;
|
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
bool MtpClient::threadLoop() {
|
2010-07-20 09:48:35 -04:00
|
|
|
usb_host_run(mUsbHostContext, usb_device_added, usb_device_removed, usb_discovery_done, this);
|
2010-07-01 11:32:08 -04:00
|
|
|
return false;
|
2010-05-19 15:12:14 -04:00
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
int MtpClient::usb_device_added(const char *devname, void* client_data) {
|
2010-05-25 19:08:48 -04:00
|
|
|
LOGD("usb_device_added %s\n", devname);
|
2010-07-01 11:32:08 -04:00
|
|
|
return ((MtpClient *)client_data)->usbDeviceAdded(devname);
|
2010-05-11 17:16:59 -04:00
|
|
|
}
|
|
|
|
|
2010-07-01 11:32:08 -04:00
|
|
|
int MtpClient::usb_device_removed(const char *devname, void* client_data) {
|
2010-05-25 19:08:48 -04:00
|
|
|
LOGD("usb_device_removed %s\n", devname);
|
2010-07-01 11:32:08 -04:00
|
|
|
return ((MtpClient *)client_data)->usbDeviceRemoved(devname);
|
2010-05-11 17:16:59 -04:00
|
|
|
}
|
|
|
|
|
2010-07-20 09:48:35 -04:00
|
|
|
int MtpClient::usb_discovery_done(void* client_data) {
|
|
|
|
LOGD("usb_discovery_done\n");
|
|
|
|
return ((MtpClient *)client_data)->usbDiscoveryDone();
|
|
|
|
}
|
|
|
|
|
2010-05-14 10:10:36 -04:00
|
|
|
} // namespace android
|