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
HidConnectionLinux(HidDeviceInfo device_info,std::string dev_node)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
~HidConnectionLinux()74 HidConnectionLinux::~HidConnectionLinux() {
75 }
76
PlatformClose()77 void HidConnectionLinux::PlatformClose() {
78 Disconnect();
79 Flush();
80 }
81
PlatformRead(const ReadCallback & callback)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
PlatformWrite(scoped_refptr<net::IOBuffer> buffer,size_t size,const WriteCallback & callback)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
PlatformGetFeatureReport(uint8_t report_id,const ReadCallback & callback)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, 0);
115 scoped_refptr<net::IOBufferWithSize> buffer(
116 new net::IOBufferWithSize(device_info().max_feature_report_size));
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 {
126 callback.Run(true, buffer, result);
127 }
128 }
129
PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer,size_t size,const WriteCallback & callback)130 void HidConnectionLinux::PlatformSendFeatureReport(
131 scoped_refptr<net::IOBuffer> buffer,
132 size_t size,
133 const WriteCallback& callback) {
134 // Linux expects the first byte of the buffer to always be a report ID so the
135 // buffer can be used directly.
136 int result = ioctl(
137 device_file_.GetPlatformFile(), HIDIOCSFEATURE(size), buffer->data());
138 if (result < 0) {
139 VPLOG(1) << "Failed to send feature report";
140 callback.Run(false);
141 } else {
142 callback.Run(true);
143 }
144 }
145
OnFileCanReadWithoutBlocking(int fd)146 void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd) {
147 DCHECK(thread_checker().CalledOnValidThread());
148 DCHECK_EQ(fd, device_file_.GetPlatformFile());
149
150 size_t expected_report_size = device_info().max_input_report_size + 1;
151 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(expected_report_size));
152 char* data = buffer->data();
153 if (!device_info().has_report_id) {
154 // Linux will not prefix the buffer with a report ID if they are not used
155 // by the device.
156 data[0] = 0;
157 data++;
158 expected_report_size--;
159 }
160
161 ssize_t bytes_read = HANDLE_EINTR(
162 read(device_file_.GetPlatformFile(), data, expected_report_size));
163 if (bytes_read < 0) {
164 if (errno == EAGAIN) {
165 return;
166 }
167 VPLOG(1) << "Read failed";
168 Disconnect();
169 return;
170 }
171 if (!device_info().has_report_id) {
172 // Include the byte prepended earlier.
173 bytes_read++;
174 }
175
176 ProcessInputReport(buffer, bytes_read);
177 }
178
OnFileCanWriteWithoutBlocking(int fd)179 void HidConnectionLinux::OnFileCanWriteWithoutBlocking(int fd) {
180 }
181
Disconnect()182 void HidConnectionLinux::Disconnect() {
183 DCHECK(thread_checker().CalledOnValidThread());
184 device_file_watcher_.StopWatchingFileDescriptor();
185 device_file_.Close();
186
187 Flush();
188 }
189
Flush()190 void HidConnectionLinux::Flush() {
191 while (!pending_reads_.empty()) {
192 pending_reads_.front().callback.Run(false, NULL, 0);
193 pending_reads_.pop();
194 }
195 }
196
ProcessInputReport(scoped_refptr<net::IOBuffer> buffer,size_t size)197 void HidConnectionLinux::ProcessInputReport(scoped_refptr<net::IOBuffer> buffer,
198 size_t size) {
199 DCHECK(thread_checker().CalledOnValidThread());
200 PendingHidReport report;
201 report.buffer = buffer;
202 report.size = size;
203 pending_reports_.push(report);
204 ProcessReadQueue();
205 }
206
ProcessReadQueue()207 void HidConnectionLinux::ProcessReadQueue() {
208 DCHECK(thread_checker().CalledOnValidThread());
209 while (pending_reads_.size() && pending_reports_.size()) {
210 PendingHidRead read = pending_reads_.front();
211 PendingHidReport report = pending_reports_.front();
212
213 pending_reports_.pop();
214 if (CompleteRead(report.buffer, report.size, read.callback)) {
215 pending_reads_.pop();
216 }
217 }
218 }
219
220 } // namespace device
221