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.
5 #include "device/hid/hid_connection_linux.h"
10 #include <linux/hidraw.h>
11 #include <sys/ioctl.h>
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"
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)
26 #ifndef HIDIOCGFEATURE
27 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE | _IOC_READ, 'H', 0x07, len)
32 HidConnectionLinux::HidConnectionLinux(HidDeviceInfo device_info
,
34 : HidConnection(device_info
) {
35 int flags
= base::File::FLAG_OPEN
|
36 base::File::FLAG_READ
|
37 base::File::FLAG_WRITE
;
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();
43 if (file_error
== base::File::FILE_ERROR_ACCESS_DENIED
) {
44 VLOG(1) << "Access denied opening device read-write, trying read-only.";
46 flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
48 device_file
= base::File(base::FilePath(dev_node
), flags
);
51 if (!device_file
.IsValid()) {
52 LOG(ERROR
) << "Failed to open '" << dev_node
<< "': "
53 << base::File::ErrorToString(device_file
.error_details());
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";
62 device_file_
= device_file
.Pass();
64 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
65 device_file_
.GetPlatformFile(),
67 base::MessageLoopForIO::WATCH_READ_WRITE
,
68 &device_file_watcher_
,
70 LOG(ERROR
) << "Failed to start watching device file.";
74 HidConnectionLinux::~HidConnectionLinux() {
77 void HidConnectionLinux::PlatformClose() {
82 void HidConnectionLinux::PlatformRead(const ReadCallback
& callback
) {
83 PendingHidRead pending_read
;
84 pending_read
.callback
= callback
;
85 pending_reads_
.push(pending_read
);
89 void HidConnectionLinux::PlatformWrite(scoped_refptr
<net::IOBuffer
> buffer
,
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";
101 if (static_cast<size_t>(bytes_written
) != size
) {
102 LOG(WARNING
) << "Incomplete HID write: " << bytes_written
109 void HidConnectionLinux::PlatformGetFeatureReport(
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
;
119 int result
= ioctl(device_file_
.GetPlatformFile(),
120 HIDIOCGFEATURE(buffer
->size()),
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);
134 callback
.Run(true, buffer
, result
);
138 void HidConnectionLinux::PlatformSendFeatureReport(
139 scoped_refptr
<net::IOBuffer
> buffer
,
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.
145 device_file_
.GetPlatformFile(), HIDIOCSFEATURE(size
), buffer
->data());
147 VPLOG(1) << "Failed to send feature report";
154 void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd
) {
155 DCHECK(thread_checker().CalledOnValidThread());
156 DCHECK_EQ(fd
, device_file_
.GetPlatformFile());
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
166 expected_report_size
--;
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
) {
175 VPLOG(1) << "Read failed";
179 if (!device_info().has_report_id
) {
180 // Include the byte prepended earlier.
184 ProcessInputReport(buffer
, bytes_read
);
187 void HidConnectionLinux::OnFileCanWriteWithoutBlocking(int fd
) {
190 void HidConnectionLinux::Disconnect() {
191 DCHECK(thread_checker().CalledOnValidThread());
192 device_file_watcher_
.StopWatchingFileDescriptor();
193 device_file_
.Close();
198 void HidConnectionLinux::Flush() {
199 while (!pending_reads_
.empty()) {
200 pending_reads_
.front().callback
.Run(false, NULL
, 0);
201 pending_reads_
.pop();
205 void HidConnectionLinux::ProcessInputReport(scoped_refptr
<net::IOBuffer
> buffer
,
207 DCHECK(thread_checker().CalledOnValidThread());
208 PendingHidReport report
;
209 report
.buffer
= buffer
;
211 pending_reports_
.push(report
);
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();
221 pending_reports_
.pop();
222 if (CompleteRead(report
.buffer
, report
.size
, read
.callback
)) {
223 pending_reads_
.pop();
228 } // namespace device