Remove "bashism" from plain shell script
[chromium-blink-merge.git] / device / hid / hid_connection_linux.cc
blob75c9e07505f694f3551a746e21b4ad09bbac2022
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"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <libudev.h>
10 #include <linux/hidraw.h>
11 #include <sys/ioctl.h>
13 #include <string>
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)
25 #endif
26 #ifndef HIDIOCGFEATURE
27 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE | _IOC_READ, 'H', 0x07, len)
28 #endif
30 namespace device {
32 namespace {
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,
37 uint8_t report_id) {
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());
42 return new_buffer;
45 const char kHidrawSubsystem[] = "hidraw";
47 } // namespace
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();
55 std::string dev_node;
56 if (!FindHidrawDevNode(dev, &dev_node)) {
57 LOG(ERROR) << "Cannot open HID device as hidraw device.";
58 return;
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();
70 return;
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.";
75 return;
77 device_file_ = device_file.Pass();
79 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
80 device_file_.GetPlatformFile(),
81 true,
82 base::MessageLoopForIO::WATCH_READ_WRITE,
83 &device_file_watcher_,
84 this)) {
85 LOG(ERROR) << "Failed to start watching device file.";
89 HidConnectionLinux::~HidConnectionLinux() {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 Disconnect();
94 void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd) {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 DCHECK_EQ(fd, device_file_.GetPlatformFile());
98 uint8 buffer[1024] = {0};
99 int bytes_read =
100 HANDLE_EINTR(read(device_file_.GetPlatformFile(), buffer, 1024));
101 if (bytes_read < 0) {
102 if (errno == EAGAIN) {
103 return;
105 Disconnect();
106 return;
109 PendingHidReport report;
110 report.buffer = new net::IOBufferWithSize(bytes_read);
111 memcpy(report.buffer->data(), buffer, bytes_read);
112 pending_reports_.push(report);
113 ProcessReadQueue();
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);
136 ProcessReadQueue();
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.
144 if (report_id != 0)
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) {
149 Disconnect();
150 callback.Run(false, 0);
151 } else {
152 callback.Run(true, bytes_written);
156 void HidConnectionLinux::GetFeatureReport(
157 uint8_t report_id,
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);
164 return;
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()),
171 buffer->data());
172 if (result < 0)
173 callback.Run(false, 0);
174 else
175 callback.Run(true, result);
178 void HidConnectionLinux::SendFeatureReport(
179 uint8_t report_id,
180 scoped_refptr<net::IOBufferWithSize> buffer,
181 const IOCallback& callback) {
182 DCHECK(thread_checker_.CalledOnValidThread());
183 if (report_id != 0)
184 buffer = CopyBufferWithReportId(buffer, report_id);
185 int result = ioctl(device_file_.GetPlatformFile(),
186 HIDIOCSFEATURE(buffer->size()),
187 buffer->data());
188 if (result < 0)
189 callback.Run(false, 0);
190 else
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());
201 } else {
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);
212 if (!udev) {
213 return false;
215 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev));
216 if (!enumerate) {
217 return false;
219 if (udev_enumerate_add_match_subsystem(enumerate.get(), kHidrawSubsystem)) {
220 return false;
222 if (udev_enumerate_scan_devices(enumerate.get())) {
223 return false;
225 std::string parent_path(udev_device_get_devpath(parent));
226 if (parent_path.length() == 0 || *parent_path.rbegin() != '/')
227 parent_path += '/';
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());
235 if (raw_path &&
236 !device_path.compare(0, parent_path.length(), parent_path)) {
237 *result = raw_path;
238 return true;
242 return false;
245 } // namespace device