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/posix/eintr_wrapper.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/tuple.h"
19 #include "device/hid/hid_service.h"
20 #include "device/hid/hid_service_linux.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)
34 // Copies a buffer into a new one with a report ID byte inserted at the front.
35 scoped_refptr
<net::IOBufferWithSize
> CopyBufferWithReportId(
36 scoped_refptr
<net::IOBufferWithSize
> buffer
,
38 scoped_refptr
<net::IOBufferWithSize
> new_buffer(
39 new net::IOBufferWithSize(buffer
->size() + 1));
40 new_buffer
->data()[0] = report_id
;
41 memcpy(new_buffer
->data() + 1, buffer
->data(), buffer
->size());
45 const char kHidrawSubsystem
[] = "hidraw";
49 HidConnectionLinux::HidConnectionLinux(HidDeviceInfo device_info
,
50 ScopedUdevDevicePtr udev_raw_device
)
51 : HidConnection(device_info
) {
52 DCHECK(thread_checker_
.CalledOnValidThread());
54 udev_device
* dev
= udev_raw_device
.get();
56 if (!FindHidrawDevNode(dev
, &dev_node
)) {
57 LOG(ERROR
) << "Cannot open HID device as hidraw device.";
61 int flags
= base::File::FLAG_OPEN
|
62 base::File::FLAG_READ
|
63 base::File::FLAG_WRITE
|
64 base::File::FLAG_EXCLUSIVE_READ
|
65 base::File::FLAG_EXCLUSIVE_WRITE
;
67 base::File
device_file(base::FilePath(dev_node
), flags
);
68 if (!device_file
.IsValid()) {
69 LOG(ERROR
) << device_file
.error_details();
72 if (fcntl(device_file
.GetPlatformFile(), F_SETFL
,
73 fcntl(device_file
.GetPlatformFile(), F_GETFL
) | O_NONBLOCK
)) {
74 PLOG(ERROR
) << "Failed to set non-blocking flag to device file.";
77 device_file_
= device_file
.Pass();
79 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
80 device_file_
.GetPlatformFile(),
82 base::MessageLoopForIO::WATCH_READ_WRITE
,
83 &device_file_watcher_
,
85 LOG(ERROR
) << "Failed to start watching device file.";
89 HidConnectionLinux::~HidConnectionLinux() {
90 DCHECK(thread_checker_
.CalledOnValidThread());
94 void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd
) {
95 DCHECK(thread_checker_
.CalledOnValidThread());
96 DCHECK_EQ(fd
, device_file_
.GetPlatformFile());
98 uint8 buffer
[1024] = {0};
100 HANDLE_EINTR(read(device_file_
.GetPlatformFile(), buffer
, 1024));
101 if (bytes_read
< 0) {
102 if (errno
== EAGAIN
) {
109 PendingHidReport report
;
110 report
.buffer
= new net::IOBufferWithSize(bytes_read
);
111 memcpy(report
.buffer
->data(), buffer
, bytes_read
);
112 pending_reports_
.push(report
);
116 void HidConnectionLinux::OnFileCanWriteWithoutBlocking(int fd
) {}
118 void HidConnectionLinux::Disconnect() {
119 DCHECK(thread_checker_
.CalledOnValidThread());
120 device_file_watcher_
.StopWatchingFileDescriptor();
121 device_file_
.Close();
122 while (!pending_reads_
.empty()) {
123 PendingHidRead pending_read
= pending_reads_
.front();
124 pending_reads_
.pop();
125 pending_read
.callback
.Run(false, 0);
129 void HidConnectionLinux::Read(scoped_refptr
<net::IOBufferWithSize
> buffer
,
130 const IOCallback
& callback
) {
131 DCHECK(thread_checker_
.CalledOnValidThread());
132 PendingHidRead pending_read
;
133 pending_read
.buffer
= buffer
;
134 pending_read
.callback
= callback
;
135 pending_reads_
.push(pending_read
);
139 void HidConnectionLinux::Write(uint8_t report_id
,
140 scoped_refptr
<net::IOBufferWithSize
> buffer
,
141 const IOCallback
& callback
) {
142 DCHECK(thread_checker_
.CalledOnValidThread());
143 // If report ID is non-zero, insert it into a new copy of the buffer.
145 buffer
= CopyBufferWithReportId(buffer
, report_id
);
146 int bytes_written
= HANDLE_EINTR(
147 write(device_file_
.GetPlatformFile(), buffer
->data(), buffer
->size()));
148 if (bytes_written
< 0) {
150 callback
.Run(false, 0);
152 callback
.Run(true, bytes_written
);
156 void HidConnectionLinux::GetFeatureReport(
158 scoped_refptr
<net::IOBufferWithSize
> buffer
,
159 const IOCallback
& callback
) {
160 DCHECK(thread_checker_
.CalledOnValidThread());
162 if (buffer
->size() == 0) {
163 callback
.Run(false, 0);
167 // The first byte of the destination buffer is the report ID being requested.
168 buffer
->data()[0] = report_id
;
169 int result
= ioctl(device_file_
.GetPlatformFile(),
170 HIDIOCGFEATURE(buffer
->size()),
173 callback
.Run(false, 0);
175 callback
.Run(true, result
);
178 void HidConnectionLinux::SendFeatureReport(
180 scoped_refptr
<net::IOBufferWithSize
> buffer
,
181 const IOCallback
& callback
) {
182 DCHECK(thread_checker_
.CalledOnValidThread());
184 buffer
= CopyBufferWithReportId(buffer
, report_id
);
185 int result
= ioctl(device_file_
.GetPlatformFile(),
186 HIDIOCSFEATURE(buffer
->size()),
189 callback
.Run(false, 0);
191 callback
.Run(true, result
);
194 void HidConnectionLinux::ProcessReadQueue() {
195 while (pending_reads_
.size() && pending_reports_
.size()) {
196 PendingHidRead read
= pending_reads_
.front();
197 pending_reads_
.pop();
198 PendingHidReport report
= pending_reports_
.front();
199 if (report
.buffer
->size() > read
.buffer
->size()) {
200 read
.callback
.Run(false, report
.buffer
->size());
202 memcpy(read
.buffer
->data(), report
.buffer
->data(), report
.buffer
->size());
203 pending_reports_
.pop();
204 read
.callback
.Run(true, report
.buffer
->size());
209 bool HidConnectionLinux::FindHidrawDevNode(udev_device
* parent
,
210 std::string
* result
) {
211 udev
* udev
= udev_device_get_udev(parent
);
215 ScopedUdevEnumeratePtr
enumerate(udev_enumerate_new(udev
));
219 if (udev_enumerate_add_match_subsystem(enumerate
.get(), kHidrawSubsystem
)) {
222 if (udev_enumerate_scan_devices(enumerate
.get())) {
225 std::string
parent_path(udev_device_get_devpath(parent
));
226 if (parent_path
.length() == 0 || *parent_path
.rbegin() != '/')
228 udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
.get());
229 for (udev_list_entry
* i
= devices
; i
!= NULL
;
230 i
= udev_list_entry_get_next(i
)) {
231 ScopedUdevDevicePtr
hid_dev(
232 udev_device_new_from_syspath(udev
, udev_list_entry_get_name(i
)));
233 const char* raw_path
= udev_device_get_devnode(hid_dev
.get());
234 std::string device_path
= udev_device_get_devpath(hid_dev
.get());
236 !device_path
.compare(0, parent_path
.length(), parent_path
)) {
245 } // namespace device