The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2008 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <fcntl.h> |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include "vold.h" |
| 22 | #include "ums.h" |
| 23 | |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 24 | #define DEBUG_UMS 0 |
| 25 | |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 26 | static boolean host_connected = false; |
| 27 | static boolean ums_enabled = false; |
| 28 | |
| 29 | int ums_bootstrap(void) |
| 30 | { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | void ums_enabled_set(boolean enabled) |
| 35 | { |
| 36 | ums_enabled = enabled; |
| 37 | send_msg(enabled ? VOLD_EVT_UMS_ENABLED : VOLD_EVT_UMS_DISABLED); |
| 38 | } |
| 39 | |
| 40 | boolean ums_enabled_get() |
| 41 | { |
| 42 | return ums_enabled; |
| 43 | } |
| 44 | |
| 45 | void ums_hostconnected_set(boolean connected) |
| 46 | { |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 47 | #if DEBUG_UMS |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 48 | LOG_VOL("ums_hostconnected_set(%d):", connected); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 49 | #endif |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 50 | host_connected = connected; |
| 51 | |
| 52 | if (!connected) |
| 53 | ums_enabled_set(false); |
| 54 | send_msg(connected ? VOLD_EVT_UMS_CONNECTED : VOLD_EVT_UMS_DISCONNECTED); |
| 55 | } |
| 56 | |
| 57 | int ums_enable(char *dev_fspath, char *lun_syspath) |
| 58 | { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 59 | LOG_VOL("ums_enable(%s, %s):", dev_fspath, lun_syspath); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 60 | |
| 61 | int fd; |
| 62 | char filename[255]; |
| 63 | |
| 64 | sprintf(filename, "/sys/%s/file", lun_syspath); |
| 65 | if ((fd = open(filename, O_WRONLY)) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 66 | LOGE("Unable to open '%s' (%s)", filename, strerror(errno)); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 67 | return -errno; |
| 68 | } |
| 69 | |
| 70 | if (write(fd, dev_fspath, strlen(dev_fspath)) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 71 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 72 | close(fd); |
| 73 | return -errno; |
| 74 | } |
| 75 | |
| 76 | close(fd); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int ums_disable(char *lun_syspath) |
| 81 | { |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 82 | #if DEBUG_UMS |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 83 | LOG_VOL("ums_disable(%s):", lun_syspath); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 84 | #endif |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 85 | |
| 86 | int fd; |
| 87 | char filename[255]; |
| 88 | |
| 89 | sprintf(filename, "/sys/%s/file", lun_syspath); |
| 90 | if ((fd = open(filename, O_WRONLY)) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 91 | LOGE("Unable to open '%s' (%s)", filename, strerror(errno)); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 92 | return -errno; |
| 93 | } |
| 94 | |
| 95 | char ch = 0; |
| 96 | |
| 97 | if (write(fd, &ch, 1) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 98 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 99 | close(fd); |
| 100 | return -errno; |
| 101 | } |
| 102 | |
| 103 | close(fd); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | boolean ums_hostconnected_get(void) |
| 108 | { |
| 109 | return host_connected; |
| 110 | } |
| 111 | |
| 112 | int ums_send_status(void) |
| 113 | { |
| 114 | int rc; |
| 115 | |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 116 | #if DEBUG_UMS |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame^] | 117 | LOG_VOL("ums_send_status():"); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 118 | #endif |
The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame] | 119 | |
| 120 | rc = send_msg(ums_enabled_get() ? VOLD_EVT_UMS_ENABLED : |
| 121 | VOLD_EVT_UMS_DISABLED); |
| 122 | if (rc < 0) |
| 123 | return rc; |
| 124 | |
| 125 | rc = send_msg(ums_hostconnected_get() ? VOLD_EVT_UMS_CONNECTED : |
| 126 | VOLD_EVT_UMS_DISCONNECTED); |
| 127 | |
| 128 | return rc; |
| 129 | } |