blob: fbf1c779ee0abf3529b0a4184c67f96dbe5bec47 [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"
Mike Lockwoodb14e5882010-06-29 18:11:52 -040018
19#include "MtpDebug.h"
20#include "MtpClient.h"
21#include "MtpDevice.h"
Mike Lockwood5ed68d22010-05-25 19:08:48 -040022
Mike Lockwood16864ba2010-05-11 17:16:59 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
26#include <sys/ioctl.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <errno.h>
30
31#include <usbhost/usbhost.h>
Mike Lockwood5ed68d22010-05-25 19:08:48 -040032#include <linux/version.h>
33#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
34#include <linux/usb/ch9.h>
35#else
36#include <linux/usb_ch9.h>
37#endif
Mike Lockwood16864ba2010-05-11 17:16:59 -040038
Mike Lockwood7850ef92010-05-14 10:10:36 -040039namespace android {
40
Mike Lockwood5ed68d22010-05-25 19:08:48 -040041MtpClient::MtpClient()
42 : mStarted(false)
Mike Lockwood16864ba2010-05-11 17:16:59 -040043{
Mike Lockwood16864ba2010-05-11 17:16:59 -040044}
45
46MtpClient::~MtpClient() {
47}
48
Mike Lockwood5ed68d22010-05-25 19:08:48 -040049bool MtpClient::start() {
50 if (mStarted)
Mike Lockwood16864ba2010-05-11 17:16:59 -040051 return true;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040052
53 if (usb_host_init(usb_device_added, usb_device_removed, this)) {
54 LOGE("MtpClient::start failed\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -040055 return false;
56 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -040057 mStarted = true;
58 return true;
59}
60
61void MtpClient::usbDeviceAdded(const char *devname) {
62 struct usb_descriptor_header* desc;
63 struct usb_descriptor_iter iter;
64
65 struct usb_device *device = usb_device_open(devname);
66 if (!device) {
67 LOGE("usb_device_open failed\n");
68 return;
69 }
70
71 usb_descriptor_iter_init(device, &iter);
72
73 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
74 if (desc->bDescriptorType == USB_DT_INTERFACE) {
75 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
76
77 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
78 interface->bInterfaceSubClass == 1 && // Still Image Capture
79 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
80 {
81 LOGD("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
82 usb_device_get_product_name(device));
83
84 // interface should be followed by three endpoints
85 struct usb_endpoint_descriptor *ep;
86 struct usb_endpoint_descriptor *ep_in_desc = NULL;
87 struct usb_endpoint_descriptor *ep_out_desc = NULL;
88 struct usb_endpoint_descriptor *ep_intr_desc = NULL;
89 for (int i = 0; i < 3; i++) {
90 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
91 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
92 LOGE("endpoints not found\n");
93 return;
94 }
95 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
96 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
97 ep_in_desc = ep;
98 else
99 ep_out_desc = ep;
100 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
101 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
102 ep_intr_desc = ep;
103 }
104 }
105 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
106 LOGE("endpoints not found\n");
107 return;
108 }
109
110 struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
111 struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
112 struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
113
114 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
115 LOGE("usb_device_claim_interface failed\n");
116 usb_endpoint_close(ep_in);
117 usb_endpoint_close(ep_out);
118 usb_endpoint_close(ep_intr);
119 return;
120 }
121
122 MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
123 ep_in, ep_out, ep_intr);
124 mDeviceList.add(mtpDevice);
125 mtpDevice->initialize();
126 deviceAdded(mtpDevice);
127 return;
128 }
129 }
130 }
131
132 usb_device_close(device);
133}
134
135MtpDevice* MtpClient::getDevice(int id) {
136 for (int i = 0; i < mDeviceList.size(); i++) {
137 MtpDevice* device = mDeviceList[i];
138 if (device->getID() == id)
139 return device;
140 }
141 return NULL;
142}
143
144void MtpClient::usbDeviceRemoved(const char *devname) {
145 for (int i = 0; i < mDeviceList.size(); i++) {
146 MtpDevice* device = mDeviceList[i];
147 if (!strcmp(devname, device->getDeviceName())) {
148 deviceRemoved(device);
149 mDeviceList.removeAt(i);
150 delete device;
151 LOGD("Camera removed!\n");
152 break;
153 }
154 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400155}
156
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400157void MtpClient::usb_device_added(const char *devname, void* client_data) {
158 LOGD("usb_device_added %s\n", devname);
159 ((MtpClient *)client_data)->usbDeviceAdded(devname);
160}
161
162void MtpClient::usb_device_removed(const char *devname, void* client_data) {
163 LOGD("usb_device_removed %s\n", devname);
164 ((MtpClient *)client_data)->usbDeviceRemoved(devname);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165}
166
Mike Lockwood7850ef92010-05-14 10:10:36 -0400167} // namespace android