Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / device / hid / hid_connection_linux.cc
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/hid/hid_connection_linux.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <libudev.h>
10 #include <linux/hidraw.h>
11 #include <sys/ioctl.h>
12
13 #include <string>
14
15 #include "base/files/file_path.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "base/tuple.h"
20 #include "device/hid/hid_service.h"
21
22 // These are already defined in newer versions of linux/hidraw.h.
23 #ifndef HIDIOCSFEATURE
24 #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE | _IOC_READ, 'H', 0x06, len)
25 #endif
26 #ifndef HIDIOCGFEATURE
27 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE | _IOC_READ, 'H', 0x07, len)
28 #endif
29
30 namespace device {
31
32 HidConnectionLinux::HidConnectionLinux(HidDeviceInfo device_info,
33                                        std::string dev_node)
34     : HidConnection(device_info) {
35   int flags = base::File::FLAG_OPEN |
36               base::File::FLAG_READ |
37               base::File::FLAG_WRITE;
38
39   base::File device_file(base::FilePath(dev_node), flags);
40   if (!device_file.IsValid()) {
41     base::File::Error file_error = device_file.error_details();
42
43     if (file_error == base::File::FILE_ERROR_ACCESS_DENIED) {
44       VLOG(1) << "Access denied opening device read-write, trying read-only.";
45
46       flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
47
48       device_file = base::File(base::FilePath(dev_node), flags);
49     }
50   }
51   if (!device_file.IsValid()) {
52     LOG(ERROR) << "Failed to open '" << dev_node << "': "
53         << base::File::ErrorToString(device_file.error_details());
54     return;
55   }
56
57   if (fcntl(device_file.GetPlatformFile(), F_SETFL,
58             fcntl(device_file.GetPlatformFile(), F_GETFL) | O_NONBLOCK)) {
59     PLOG(ERROR) << "Failed to set non-blocking flag to device file";
60     return;
61   }
62   device_file_ = device_file.Pass();
63
64   if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
65       device_file_.GetPlatformFile(),
66       true,
67       base::MessageLoopForIO::WATCH_READ_WRITE,
68       &device_file_watcher_,
69       this)) {
70     LOG(ERROR) << "Failed to start watching device file.";
71   }
72 }
73
74 HidConnectionLinux::~HidConnectionLinux() {
75 }
76
77 void HidConnectionLinux::PlatformClose() {
78   Disconnect();
79   Flush();
80 }
81
82 void HidConnectionLinux::PlatformRead(const ReadCallback& callback) {
83   PendingHidRead pending_read;
84   pending_read.callback = callback;
85   pending_reads_.push(pending_read);
86   ProcessReadQueue();
87 }
88
89 void HidConnectionLinux::PlatformWrite(scoped_refptr<net::IOBuffer> buffer,
90                                        size_t size,
91                                        const WriteCallback& callback) {
92   // Linux expects the first byte of the buffer to always be a report ID so the
93   // buffer can be used directly.
94   const ssize_t bytes_written =
95       HANDLE_EINTR(write(device_file_.GetPlatformFile(), buffer->data(), size));
96   if (bytes_written < 0) {
97     VPLOG(1) << "Write failed";
98     Disconnect();
99     callback.Run(false);
100   } else {
101     if (static_cast<size_t>(bytes_written) != size) {
102       LOG(WARNING) << "Incomplete HID write: " << bytes_written
103                    << " != " << size;
104     }
105     callback.Run(true);
106   }
107 }
108
109 void HidConnectionLinux::PlatformGetFeatureReport(
110     uint8_t report_id,
111     const ReadCallback& callback) {
112   // The first byte of the destination buffer is the report ID being requested
113   // and is overwritten by the feature report.
114   DCHECK_GT(device_info().max_feature_report_size, 0u);
115   scoped_refptr<net::IOBufferWithSize> buffer(
116       new net::IOBufferWithSize(device_info().max_feature_report_size + 1));
117   buffer->data()[0] = report_id;
118
119   int result = ioctl(device_file_.GetPlatformFile(),
120                      HIDIOCGFEATURE(buffer->size()),
121                      buffer->data());
122   if (result < 0) {
123     VPLOG(1) << "Failed to get feature report";
124     callback.Run(false, NULL, 0);
125   } else if (result == 0) {
126     VLOG(1) << "Get feature result too short.";
127     callback.Run(false, NULL, 0);
128   } else if (report_id == 0) {
129     // Linux adds a 0 to the beginning of the data received from the device.
130     scoped_refptr<net::IOBuffer> copied_buffer(new net::IOBuffer(result - 1));
131     memcpy(copied_buffer->data(), buffer->data() + 1, result - 1);
132     callback.Run(true, copied_buffer, result - 1);
133   } else {
134     callback.Run(true, buffer, result);
135   }
136 }
137
138 void HidConnectionLinux::PlatformSendFeatureReport(
139     scoped_refptr<net::IOBuffer> buffer,
140     size_t size,
141     const WriteCallback& callback) {
142   // Linux expects the first byte of the buffer to always be a report ID so the
143   // buffer can be used directly.
144   int result = ioctl(
145       device_file_.GetPlatformFile(), HIDIOCSFEATURE(size), buffer->data());
146   if (result < 0) {
147     VPLOG(1) << "Failed to send feature report";
148     callback.Run(false);
149   } else {
150     callback.Run(true);
151   }
152 }
153
154 void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd) {
155   DCHECK(thread_checker().CalledOnValidThread());
156   DCHECK_EQ(fd, device_file_.GetPlatformFile());
157
158   size_t expected_report_size = device_info().max_input_report_size + 1;
159   scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(expected_report_size));
160   char* data = buffer->data();
161   if (!device_info().has_report_id) {
162     // Linux will not prefix the buffer with a report ID if they are not used
163     // by the device.
164     data[0] = 0;
165     data++;
166     expected_report_size--;
167   }
168
169   ssize_t bytes_read = HANDLE_EINTR(
170       read(device_file_.GetPlatformFile(), data, expected_report_size));
171   if (bytes_read < 0) {
172     if (errno == EAGAIN) {
173       return;
174     }
175     VPLOG(1) << "Read failed";
176     Disconnect();
177     return;
178   }
179   if (!device_info().has_report_id) {
180     // Include the byte prepended earlier.
181     bytes_read++;
182   }
183
184   ProcessInputReport(buffer, bytes_read);
185 }
186
187 void HidConnectionLinux::OnFileCanWriteWithoutBlocking(int fd) {
188 }
189
190 void HidConnectionLinux::Disconnect() {
191   DCHECK(thread_checker().CalledOnValidThread());
192   device_file_watcher_.StopWatchingFileDescriptor();
193   device_file_.Close();
194
195   Flush();
196 }
197
198 void HidConnectionLinux::Flush() {
199   while (!pending_reads_.empty()) {
200     pending_reads_.front().callback.Run(false, NULL, 0);
201     pending_reads_.pop();
202   }
203 }
204
205 void HidConnectionLinux::ProcessInputReport(scoped_refptr<net::IOBuffer> buffer,
206                                             size_t size) {
207   DCHECK(thread_checker().CalledOnValidThread());
208   PendingHidReport report;
209   report.buffer = buffer;
210   report.size = size;
211   pending_reports_.push(report);
212   ProcessReadQueue();
213 }
214
215 void HidConnectionLinux::ProcessReadQueue() {
216   DCHECK(thread_checker().CalledOnValidThread());
217   while (pending_reads_.size() && pending_reports_.size()) {
218     PendingHidRead read = pending_reads_.front();
219     PendingHidReport report = pending_reports_.front();
220
221     pending_reports_.pop();
222     if (CompleteRead(report.buffer, report.size, read.callback)) {
223       pending_reads_.pop();
224     }
225   }
226 }
227
228 }  // namespace device