OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "ui/ozone/platform/drm/host/drm_device_handle.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <xf86drm.h> |
| 9 #include <xf86drmMode.h> |
| 10 |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/posix/eintr_wrapper.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 14 |
| 15 namespace ui { |
| 16 |
| 17 namespace { |
| 18 |
| 19 bool Authenticate(int fd) { |
| 20 drm_magic_t magic; |
| 21 memset(&magic, 0, sizeof(magic)); |
| 22 // We need to make sure the DRM device has enough privilege. Use the DRM |
| 23 // authentication logic to figure out if the device has enough permissions. |
| 24 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 DrmDeviceHandle::DrmDeviceHandle() { |
| 30 } |
| 31 |
| 32 DrmDeviceHandle::~DrmDeviceHandle() { |
| 33 if (file_.is_valid()) |
| 34 base::ThreadRestrictions::AssertIOAllowed(); |
| 35 } |
| 36 |
| 37 bool DrmDeviceHandle::Initialize(const base::FilePath& path) { |
| 38 CHECK(path.DirName() == base::FilePath("/dev/dri")); |
| 39 base::ThreadRestrictions::AssertIOAllowed(); |
| 40 bool print_warning = true; |
| 41 while (true) { |
| 42 file_.reset(); |
| 43 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDWR | O_CLOEXEC)); |
| 44 if (fd < 0) { |
| 45 PLOG(ERROR) << "Failed to open " << path.value(); |
| 46 return false; |
| 47 } |
| 48 |
| 49 file_.reset(fd); |
| 50 |
| 51 if (Authenticate(file_.get())) |
| 52 break; |
| 53 |
| 54 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value(); |
| 55 print_warning = false; |
| 56 usleep(100000); |
| 57 } |
| 58 |
| 59 VLOG(1) << "Succeeded authenticating " << path.value(); |
| 60 return true; |
| 61 } |
| 62 |
| 63 bool DrmDeviceHandle::IsValid() const { |
| 64 return file_.is_valid(); |
| 65 } |
| 66 |
| 67 base::ScopedFD DrmDeviceHandle::PassFD() { |
| 68 return file_.Pass(); |
| 69 } |
| 70 |
| 71 } // namespace ui |
OLD | NEW |