···1717* @ref understanding-targets - How all the pieces (`xrt_instance`, IPC, OpenXR)
1818 fit together.
1919* @ref vulkan-extensions
2020-* @ref writing-driver (**not complete**)
2020+* @ref writing-driver
2121* @ref ipc-design
2222* @ref frame-timing
2323* @ref tracing
+76-9
doc/writing-a-new-driver.md
···11# Writing a new driver {#writing-driver}
2233<!--
44-Copyright 2018-2020, Collabora, Ltd. and the Monado contributors
44+Copyright 2018-2021, Collabora, Ltd. and the Monado contributors
55SPDX-License-Identifier: BSL-1.0
66-->
7788-This document will tell you in broad strokes what you need to do to create a
99-driver in Monado. Like the many ones already in there @ref drv. It is not a step
1010-by step guide to making a driver. Also what you need to do can vary a lot
1111-depending on the type of hardware you are adding a driver for and the level of
1212-features you want.
88+The easiest way to begin writing a driver is to start from a working example.
99+The @ref drv_sample driver is provided explicitly for this purpose: it creates
1010+an HMD device, with a custom @ref xrt_auto_prober implementation for hardware
1111+discovery, and some simple display parameters that should be easy to modify.
1212+1313+Copy that directory and rename the files in it. Then, use the following `sed`
1414+command to perform some bulk renames before you begin actually writing code. The
1515+command as written assumes your new device type is called `my_device` or `md`
1616+for short, and your auto-prober is called `my_device_auto_prober` or `mdap` for
1717+short: change the replacement side of each pattern to match the real names you
1818+are using.
1919+2020+```sh
2121+# First pattern is for renaming device types,
2222+# second is for renaming device variables,
2323+# third is for renaming device macros.
2424+# Fourth and fifth are for renaming auto prober types and variables, respectively.
2525+# The last two are for renaming the environment variable and function name
2626+# for the environment variable logging config.
2727+sed -r -e 's/sample_hmd/my_device/g' \
2828+ -e 's/\bsh\b/md/g' \
2929+ -e 's/sample_auto_prober/my_device_auto_prober/g' \
3030+ -e 's/\bsap\b/mdap/g' \
3131+ -e 's/\bSH_/MD_/g' \
3232+ -e 's/sample/my_device/g' \
3333+ -e 's/SAMPLE/MY_DEVICE/g' \
3434+ -i *.c *.h
3535+```
3636+3737+You will want to go through each function of the sample code you started from,
3838+implement any missing functionality, and adapt any existing functionality to
3939+match your device. Refer to other @ref drv for additional guidance. Most drivers
4040+are fairly simple, as large or complex functionality in drivers is often
4141+factored out into separate auxiliary libraries.
4242+4343+## What to Implement
4444+4545+You will definitely make at least one implementation of @ref xrt_device. If your
4646+driver can talk to e.g. both a headset and corresponding controllers, you can
4747+choose to expose all those through a single xrt_device implementation, or
4848+through multiple implementations that may share some underlying component (by
4949+convention called `..._system`). Both are valid choices, and the right one to
5050+choose depends on which maps better to your underlying device or API you are
5151+connecting to. It is more common to have one xrt_device per piece of hardware,
5252+however. @ref hydra_device serves as a nice example of two controllers that are
5353+enumerated as a single overall USB HID device but expose two separate xrt_device
5454+instances.
5555+5656+Depending on whether your device can be created from a detected USB HID device,
5757+you will also need to implement either @ref xrt_auto_prober or a function
5858+matching the prototype for @ref xrt_prober_entry::found. See below for more details.
13591414-## Map
6060+## Other Relevant Components
15611616-The first components you will be interacting with is @ref st_prober find the
6262+The components you will be interacting with is @ref st_prober to find the
1763hardware devices and setup a working system, along with the @ref aux code that
1818-provides various helpers. You can look at other @ref drv on how to start.
6464+provides various helpers. It is convention in Monado for interfaces to allow
6565+full, complete control of anything a device might want to modify/control, and to
6666+provide helper functionality in @ref aux to simplify implementation of the most
6767+common cases.
19682069## Probing
2170···2372too hard: you use the auto prober interface when the basic USB VID/PID-based
2473interface is not sufficient for you to detect presence/absence of your device,
2574or if you don't want to use the built-in HID support for some reason.
7575+7676+If you can use built-in HID, you might consider looking at @ref hdk_found, which
7777+is a nice example of how to implement detection of an HMD based on the USB HID
7878+for its IMU.
7979+8080+Either way, your device's detection details will need to be added to a list used
8181+by the prober at @ref xrt_instance startup time. The stock lists for mainline
8282+Monado are in `src/xrt/targets/common/target_lists.c`. These are shared by the
8383+various targets (OpenXR runtime shared library, service executable, utility
8484+executables) also found in `src/xrt/targets`. If you're using Monado as a
8585+toolkit or component rather than as a standalone runtime and service, you can
8686+replicate whatever portions of the target lists in your own target, or just
8787+directly implement the @ref xrt_instance interface more directly, linking in
8888+only those drivers and components you need. **Note**, however, that Monado is
8989+intended to not expose any external API other than the OpenXR API: the @ref
9090+xrt_iface are subject to change as required so those writing drivers or other
9191+software on those interfaces are encouraged to upstream as much as possible to
9292+minimize maintenance burden.
+2-21
src/xrt/drivers/sample/sample_interface.h
···2121 * @brief Simple do-nothing sample driver, that cannot be detected by USB VID/PID
2222 * and thus exposes an "auto-prober" to explicitly discover the device.
2323 *
2424+ * See @ref writing-driver for additional information.
2525+ *
2426 * This device has an implementation of @ref xrt_auto_prober to perform hardware
2527 * detection, as well as an implementation of @ref xrt_device for the actual device.
2628 *
···2830 * you can skip the @ref xrt_auto_prober implementation, and instead implement a
2931 * "found" function that matches the signature expected by xrt_prober_entry::found.
3032 * See for example @ref hdk_found.
3131- *
3232- * After you copy and rename these files, you can customize them with the following,
3333- * assuming your new device type is called `struct my_device` or `md` for short, and
3434- * your auto-prober is called `struct my_device_auto_prober` or `mdap` for short:
3535- *
3636- * ```sh
3737- * # First pattern is for renaming device types,
3838- * # second is for renaming device variables,
3939- * # third is for renaming device macros.
4040- * # Fourth and fifth are for renaming auto prober types and variables, respectively.
4141- * # The last two are for renaming the environment variable and function name
4242- * # for the environment variable logging config.
4343- * sed -r -e 's/sample_hmd/my_device/g' \
4444- * -e 's/\bsh\b/md/g' \
4545- * -e 's/sample_auto_prober/my_device_auto_prober/g' \
4646- * -e 's/\bsap\b/mdap/g' \
4747- * -e 's/\bSH_/MD_/g' \
4848- * -e 's/sample/my_device/g' \
4949- * -e 's/SAMPLE/MY_DEVICE/g' \
5050- * -i *.c *.h
5151- * ```
5233 */
53345435/*!