The open source OpenXR runtime
1// Copyright 2025, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Prober for finding an Oculus Rift device based on USB VID/PID.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup drv_rift
8 */
9
10#include "xrt/xrt_prober.h"
11
12#include "util/u_misc.h"
13
14#include "rift_interface.h"
15
16static const char DK2_PRODUCT_STRING[] = "Rift DK2";
17
18int
19rift_found(struct xrt_prober *xp,
20 struct xrt_prober_device **devices,
21 size_t device_count,
22 size_t index,
23 cJSON *attached_data,
24 struct xrt_device **out_xdev)
25{
26 struct xrt_prober_device *dev = devices[index];
27
28 unsigned char manufacturer[128] = {0};
29 int result = xrt_prober_get_string_descriptor(xp, dev, XRT_PROBER_STRING_MANUFACTURER, manufacturer,
30 sizeof(manufacturer));
31 if (result < 0) {
32 return -1;
33 }
34
35 unsigned char product[128] = {0};
36 result = xrt_prober_get_string_descriptor(xp, dev, XRT_PROBER_STRING_PRODUCT, product, sizeof(product));
37 if (result < 0) {
38 return -1;
39 }
40
41 unsigned char serial_number[21] = {0};
42 result = xrt_prober_get_string_descriptor(xp, dev, XRT_PROBER_STRING_SERIAL_NUMBER, serial_number,
43 sizeof(serial_number));
44 if (result < 0) {
45 return -1;
46 }
47
48 // Some non-oculus devices (VR-Tek HMDs) reuse the same USB IDs as the oculus headsets, so we should check the
49 // manufacturer
50 if (strncmp((const char *)manufacturer, "Oculus VR, Inc.", sizeof(manufacturer)) != 0) {
51 return -1;
52 }
53
54 enum rift_variant variant;
55 const char *name;
56 switch (dev->product_id) {
57 case OCULUS_DK2_PID:
58 variant = RIFT_VARIANT_DK2;
59 name = DK2_PRODUCT_STRING;
60 break;
61 default: return -1; break;
62 }
63
64 U_LOG_I("%s - Found at least the tracker of some Rift (%s) -- opening\n", __func__, name);
65
66 struct os_hid_device *hid = NULL;
67 result = xrt_prober_open_hid_interface(xp, dev, 0, &hid);
68 if (result != 0) {
69 return -1;
70 }
71
72 struct rift_hmd *hd = rift_hmd_create(hid, variant, (char *)product, (char *)serial_number);
73 if (hd == NULL) {
74 return -1;
75 }
76 *out_xdev = &hd->base;
77
78 return 1;
79}