blob: 1218fa6e6fc093005463476beda7490e2b5456d3 [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
17#include <unistd.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <usbhost/usbhost.h>
22#include <linux/usb/ch9.h>
23
24#include "MtpClient.h"
25
26static struct usb_device *sCameraDevice = NULL;
27static int sCameraInterface = 0;
28static MtpClient *sClient = NULL;
29
30
31static void start_session(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
32 struct usb_endpoint *ep_intr)
33{
34 if (sClient)
35 delete sClient;
36 sClient = new MtpClient(ep_in, ep_out, ep_intr);
37 sClient->openSession();
38 sClient->getDeviceInfo();
39}
40
41static void usb_device_added(const char *devname)
42{
43 struct usb_descriptor_header* desc;
44 struct usb_descriptor_iter iter;
45
46 struct usb_device *device = usb_device_open(devname);
47 if (!device) return;
48
49 usb_descriptor_iter_init(device, &iter);
50
51 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
52 if (desc->bDescriptorType == USB_DT_INTERFACE) {
53 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
54
55 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
56 interface->bInterfaceSubClass == 1 && // Still Image Capture
57 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
58 {
59 printf("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
60 usb_device_get_product_name(device));
61
62 // interface should be followed by three endpoints
63 struct usb_endpoint_descriptor *ep, *ep_in_desc = NULL, *ep_out_desc = NULL, *ep_intr_desc = NULL;
64 for (int i = 0; i < 3; i++) {
65 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
66 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
67 fprintf(stderr, "endpoints not found\n");
68 return;
69 }
70 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
71 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
72 ep_in_desc = ep;
73 else
74 ep_out_desc = ep;
75 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
76 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
77 ep_intr_desc = ep;
78 }
79 }
80 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
81 fprintf(stderr, "endpoints not found\n");
82 return;
83 }
84
85 struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
86 struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
87 struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
88
89 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
90 fprintf(stderr, "usb_device_claim_interface failed\n");
91 usb_endpoint_close(ep_in);
92 usb_endpoint_close(ep_out);
93 usb_endpoint_close(ep_intr);
94 return;
95 }
96
97 if (sCameraDevice) {
98 usb_device_release_interface(sCameraDevice, sCameraInterface);
99 usb_device_close(sCameraDevice);
100 }
101 sCameraDevice = device;
102 start_session(ep_in, ep_out, ep_intr);
103 }
104 }
105 }
106
107 if (device != sCameraDevice)
108 usb_device_close(device);
109}
110
111static void usb_device_removed(const char *devname)
112{
113 if (sCameraDevice && !strcmp(devname, usb_device_get_name(sCameraDevice))) {
114 delete sClient;
115 printf("Camera removed!\n");
116 usb_device_release_interface(sCameraDevice, sCameraInterface);
117 usb_device_close(sCameraDevice);
118 sCameraDevice = NULL;
119 }
120}
121
122int main(int argc, char* argv[])
123{
124 if (usb_host_init(usb_device_added, usb_device_removed)) {
125 fprintf(stderr, "usb_host_init failed\n");
126 return -1;
127 }
128
129 while (1) {
130 sleep(1);
131 }
132
133 return 0;
134}