blob: 31874e9916121e98452837049b916b203c852f2d [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwood5ed68d22010-05-25 19:08:48 -040017#define LOG_TAG "MtpClient"
18#include "utils/Log.h"
19
Mike Lockwood16864ba2010-05-11 17:16:59 -040020#include <stdio.h>
21#include <stdlib.h>
22#include <sys/types.h>
23#include <sys/ioctl.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27
28#include <usbhost/usbhost.h>
Mike Lockwood5ed68d22010-05-25 19:08:48 -040029#include <linux/version.h>
30#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
31#include <linux/usb/ch9.h>
32#else
33#include <linux/usb_ch9.h>
34#endif
Mike Lockwood16864ba2010-05-11 17:16:59 -040035
36#include "MtpClient.h"
Mike Lockwood5ed68d22010-05-25 19:08:48 -040037#include "MtpDevice.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040038#include "MtpDebug.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040039
Mike Lockwood7850ef92010-05-14 10:10:36 -040040namespace android {
41
Mike Lockwood5ed68d22010-05-25 19:08:48 -040042MtpClient::MtpClient()
43 : mStarted(false)
Mike Lockwood16864ba2010-05-11 17:16:59 -040044{
Mike Lockwood16864ba2010-05-11 17:16:59 -040045}
46
47MtpClient::~MtpClient() {
48}
49
Mike Lockwood5ed68d22010-05-25 19:08:48 -040050bool MtpClient::start() {
51 if (mStarted)
Mike Lockwood16864ba2010-05-11 17:16:59 -040052 return true;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040053
54 if (usb_host_init(usb_device_added, usb_device_removed, this)) {
55 LOGE("MtpClient::start failed\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -040056 return false;
57 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -040058 mStarted = true;
59 return true;
60}
61
62void MtpClient::usbDeviceAdded(const char *devname) {
63 struct usb_descriptor_header* desc;
64 struct usb_descriptor_iter iter;
65
66 struct usb_device *device = usb_device_open(devname);
67 if (!device) {
68 LOGE("usb_device_open failed\n");
69 return;
70 }
71
72 usb_descriptor_iter_init(device, &iter);
73
74 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
75 if (desc->bDescriptorType == USB_DT_INTERFACE) {
76 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
77
78 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
79 interface->bInterfaceSubClass == 1 && // Still Image Capture
80 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
81 {
82 LOGD("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
83 usb_device_get_product_name(device));
84
85 // interface should be followed by three endpoints
86 struct usb_endpoint_descriptor *ep;
87 struct usb_endpoint_descriptor *ep_in_desc = NULL;
88 struct usb_endpoint_descriptor *ep_out_desc = NULL;
89 struct usb_endpoint_descriptor *ep_intr_desc = NULL;
90 for (int i = 0; i < 3; i++) {
91 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
92 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
93 LOGE("endpoints not found\n");
94 return;
95 }
96 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
97 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
98 ep_in_desc = ep;
99 else
100 ep_out_desc = ep;
101 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
102 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
103 ep_intr_desc = ep;
104 }
105 }
106 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
107 LOGE("endpoints not found\n");
108 return;
109 }
110
111 struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
112 struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
113 struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
114
115 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
116 LOGE("usb_device_claim_interface failed\n");
117 usb_endpoint_close(ep_in);
118 usb_endpoint_close(ep_out);
119 usb_endpoint_close(ep_intr);
120 return;
121 }
122
123 MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
124 ep_in, ep_out, ep_intr);
125 mDeviceList.add(mtpDevice);
126 mtpDevice->initialize();
127 deviceAdded(mtpDevice);
128 return;
129 }
130 }
131 }
132
133 usb_device_close(device);
134}
135
136MtpDevice* MtpClient::getDevice(int id) {
137 for (int i = 0; i < mDeviceList.size(); i++) {
138 MtpDevice* device = mDeviceList[i];
139 if (device->getID() == id)
140 return device;
141 }
142 return NULL;
143}
144
145void MtpClient::usbDeviceRemoved(const char *devname) {
146 for (int i = 0; i < mDeviceList.size(); i++) {
147 MtpDevice* device = mDeviceList[i];
148 if (!strcmp(devname, device->getDeviceName())) {
149 deviceRemoved(device);
150 mDeviceList.removeAt(i);
151 delete device;
152 LOGD("Camera removed!\n");
153 break;
154 }
155 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156}
157
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400158void MtpClient::usb_device_added(const char *devname, void* client_data) {
159 LOGD("usb_device_added %s\n", devname);
160 ((MtpClient *)client_data)->usbDeviceAdded(devname);
161}
162
163void MtpClient::usb_device_removed(const char *devname, void* client_data) {
164 LOGD("usb_device_removed %s\n", devname);
165 ((MtpClient *)client_data)->usbDeviceRemoved(devname);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400166}
167
Mike Lockwood7850ef92010-05-14 10:10:36 -0400168} // namespace android