Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 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 | #define LOG_TAG "mtp_usb" |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 18 | |
| 19 | #include "MtpDebug.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <fcntl.h> |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 26 | |
| 27 | #include "MtpServer.h" |
| 28 | #include "MtpStorage.h" |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 29 | #include "f_mtp.h" |
Mike Lockwood | 8e2a280 | 2010-07-02 15:15:07 -0400 | [diff] [blame^] | 30 | #include "private/android_filesystem_config.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 32 | using namespace android; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 33 | |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 34 | static bool enable_usb_function(const char* name, bool enable) { |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 35 | char path[PATH_MAX]; |
| 36 | |
| 37 | snprintf(path, sizeof(path), "/sys/class/usb_composite/%s/enable", name); |
| 38 | int fd = open(path, O_RDWR); |
| 39 | if (fd < 0) { |
| 40 | fprintf(stderr, "could not open %s in enable_usb_function\n", path); |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 41 | return false; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 42 | } |
| 43 | write(fd, enable ? "1" : "0", 2); |
| 44 | close(fd); |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 45 | return true; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 48 | int main(int argc, char* argv[]) { |
| 49 | bool usePTP = false; |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 50 | const char* storagePath = "/sdcard"; |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 51 | |
| 52 | for (int i = 1; i < argc; i++) { |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 53 | const char* arg = argv[i]; |
| 54 | if (!strcmp(arg, "-p")) |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 55 | usePTP = true; |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 56 | else if (arg[0] == '/') |
| 57 | storagePath = arg; |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 58 | } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 59 | |
| 60 | int fd = open("/dev/mtp_usb", O_RDWR); |
| 61 | printf("open returned %d\n", fd); |
| 62 | if (fd < 0) { |
| 63 | fprintf(stderr, "could not open MTP driver\n"); |
| 64 | return -1; |
| 65 | } |
| 66 | |
Mike Lockwood | 64a4422 | 2010-05-16 16:39:32 -0400 | [diff] [blame] | 67 | if (usePTP) { |
| 68 | // set driver mode to PTP |
| 69 | int ret = ioctl(fd, MTP_SET_INTERFACE_MODE, MTP_INTERFACE_MODE_PTP); |
| 70 | if (ret) { |
| 71 | fprintf(stderr, "MTP_SET_INTERFACE_MODE failed\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // disable UMS and enable MTP USB functions |
| 77 | enable_usb_function("usb_mass_storage", false); |
| 78 | enable_usb_function("mtp", true); |
| 79 | |
Mike Lockwood | 8e2a280 | 2010-07-02 15:15:07 -0400 | [diff] [blame^] | 80 | MtpServer server(fd, "/data/data/mtp/mtp.db", AID_SDCARD_RW, 0664, 0775); |
Mike Lockwood | 78c1e5e | 2010-06-15 10:28:47 -0700 | [diff] [blame] | 81 | server.addStorage(storagePath); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 82 | server.scanStorage(); |
| 83 | server.run(); |
| 84 | |
| 85 | close(fd); |
| 86 | return 0; |
| 87 | } |
| 88 | |