blob: 4d0fc2590c4e5765956ee53d45a72a5d037897ec [file] [log] [blame]
The Android Open Source Project8ac3a132009-01-20 14:04:01 -08001
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 Project13f797d2009-02-10 15:44:07 -080024#define DEBUG_UMS 0
25
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080026static boolean host_connected = false;
27static boolean ums_enabled = false;
28
29int ums_bootstrap(void)
30{
31 return 0;
32}
33
34void ums_enabled_set(boolean enabled)
35{
36 ums_enabled = enabled;
37 send_msg(enabled ? VOLD_EVT_UMS_ENABLED : VOLD_EVT_UMS_DISABLED);
38}
39
40boolean ums_enabled_get()
41{
42 return ums_enabled;
43}
44
45void ums_hostconnected_set(boolean connected)
46{
The Android Open Source Project13f797d2009-02-10 15:44:07 -080047#if DEBUG_UMS
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080048 LOG_VOL("ums_hostconnected_set(%d):", connected);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080049#endif
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080050 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
57int ums_enable(char *dev_fspath, char *lun_syspath)
58{
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080059 LOG_VOL("ums_enable(%s, %s):", dev_fspath, lun_syspath);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080060
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 Project1b8e5a62009-02-13 12:57:54 -080066 LOGE("Unable to open '%s' (%s)", filename, strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080067 return -errno;
68 }
69
70 if (write(fd, dev_fspath, strlen(dev_fspath)) < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080071 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080072 close(fd);
73 return -errno;
74 }
75
76 close(fd);
77 return 0;
78}
79
80int ums_disable(char *lun_syspath)
81{
The Android Open Source Project13f797d2009-02-10 15:44:07 -080082#if DEBUG_UMS
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080083 LOG_VOL("ums_disable(%s):", lun_syspath);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080084#endif
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080085
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 Project1b8e5a62009-02-13 12:57:54 -080091 LOGE("Unable to open '%s' (%s)", filename, strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080092 return -errno;
93 }
94
95 char ch = 0;
96
97 if (write(fd, &ch, 1) < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080098 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080099 close(fd);
100 return -errno;
101 }
102
103 close(fd);
104 return 0;
105}
106
107boolean ums_hostconnected_get(void)
108{
109 return host_connected;
110}
111
112int ums_send_status(void)
113{
114 int rc;
115
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800116#if DEBUG_UMS
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800117 LOG_VOL("ums_send_status():");
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800118#endif
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800119
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}