summaryrefslogtreecommitdiff
path: root/src/libcamera/device_enumerator.cpp
blob: df9e89a1afeecda1ad697ab0238eb14078c57bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * device_enumerator.cpp - Enumeration and matching
 */

#include <fcntl.h>
#include <libudev.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "device_enumerator.h"
#include "log.h"

namespace libcamera {

/* -----------------------------------------------------------------------------
 * DeviceInfo
 */

DeviceInfo::DeviceInfo(const std::string &devnode, const struct media_device_info &info,
		       const std::map<std::string, std::string> &entities)
	: acquired_(false), devnode_(devnode), info_(info), entities_(entities)
{
	for (const auto &entity : entities_)
		LOG(Info) << "Device: " << devnode_ << " Entity: '" << entity.first << "' -> " << entity.second;
}

int DeviceInfo::acquire()
{
	if (acquired_)
		return -EBUSY;

	acquired_ = true;

	return 0;
}

void DeviceInfo::release()
{
	acquired_ = false;
}

bool DeviceInfo::busy() const
{
	return acquired_;
}

const std::string &DeviceInfo::devnode() const
{
	return devnode_;
}

const struct media_device_info &DeviceInfo::info() const
{
	return info_;
}

std::vector<std::string> DeviceInfo::entities() const
{
	std::vector<std::string> entities;

	for (const auto &entity : entities_)
		entities.push_back(entity.first);

	return entities;
}

int DeviceInfo::lookup(const std::string &name, std::string &devnode) const
{
	auto it = entities_.find(name);

	if (it == entities_.end()) {
		LOG(Error) << "Trying to lookup entity '" << name << "' which does not exist";
		return -ENODEV;
	}

	devnode = it->second;
	return 0;
}

/* -----------------------------------------------------------------------------
 * DeviceMatch
 */

DeviceMatch::DeviceMatch(const std::string &driver)
	: driver_(driver)
{
}

void DeviceMatch::add(const std::string &entity)
{
	entities_.push_back(entity);
}

bool DeviceMatch::match(const DeviceInfo *info) const
{
	if (driver_ != info->info().driver)
		return false;

	for (const std::string &name : entities_) {
		bool found = false;

		for (const std::string &entity : info->entities()) {
			if (name == entity) {
				found = true;
				break;
			}
		}

		if (!found)
			return false;
	}

	return true;
}

/* -----------------------------------------------------------------------------
 * Enumerator Base
 */

DeviceEnumerator::~DeviceEnumerator()
{
	for (DeviceInfo *dev : devices_) {
		if (dev->busy())
			LOG(Error) << "Removing device info while still in use";

		delete dev;
	}
}

int DeviceEnumerator::addDevice(const std::string &devnode)
{
	int fd, ret;

	struct media_device_info info = {};
	std::map<std::string, std::string> entities;

	fd = open(devnode.c_str(), O_RDWR);
	if (fd < 0) {
		ret = -errno;
		LOG(Info) << "Unable to open " << devnode <<
			  " (" << strerror(-ret) << "), skipping";
		return ret;
	}

	ret = readInfo(fd, info);
	if (ret)
		goto out;

	ret = readTopology(fd, entities);
	if (ret)
		goto out;

	devices_.push_back(new DeviceInfo(devnode, info, entities));
out:
	close(fd);

	return ret;
}

int DeviceEnumerator::readInfo(int fd, struct media_device_info &info)
{
	int ret;

	ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &info);
	if (ret < 0) {
		ret = -errno;
		LOG(Info) << "Unable to read device info " <<
			  " (" << strerror(-ret) << "), skipping";
		return ret;
	}

	return 0;
}

int DeviceEnumerator::readTopology(int fd, std::map<std::string, std::string> &entities)
{
	struct media_v2_topology topology;
	struct media_v2_entity *ents = nullptr;
	struct media_v2_interface *ifaces = nullptr;
	struct media_v2_link *links = nullptr;
	int ret;

	while (true) {
		topology = {};

		ret = ioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology);
		if (ret < 0)
			return -errno;

		__u64 version = topology.topology_version;

		ents = new media_v2_entity[topology.num_entities]();
		ifaces = new media_v2_interface[topology.num_interfaces]();
		links = new media_v2_link[topology.num_links]();
		topology.ptr_entities = reinterpret_cast<__u64>(ents);
		topology.ptr_interfaces = reinterpret_cast<__u64>(ifaces);
		topology.ptr_links = reinterpret_cast<__u64>(links);

		ret = ioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology);
		if (ret < 0) {
			ret = -errno;
			goto done;
		}

		if (version == topology.topology_version)
			break;

		delete[] links;
		delete[] ifaces;
		delete[] ents;
	}

	for (unsigned int link_id = 0; link_id < topology.num_links; link_id++) {
		unsigned int iface_id, ent_id;
		std::string devnode;

		if ((links[link_id].flags & MEDIA_LNK_FL_LINK_TYPE) !=
		    MEDIA_LNK_FL_INTERFACE_LINK)
			continue;

		for (iface_id = 0; iface_id < topology.num_interfaces; iface_id++)
			if (links[link_id].source_id == ifaces[iface_id].id)
				break;

		for (ent_id = 0; ent_id < topology.num_entities; ent_id++)
			if (links[link_id].sink_id == ents[ent_id].id)
				break;

		if (ent_id >= topology.num_entities ||
		    iface_id >= topology.num_interfaces)
			continue;

		devnode = lookupDevnode(ifaces[iface_id].devnode.major,
					ifaces[iface_id].devnode.minor);
		if (devnode == "")
			break;

		entities[ents[ent_id].name] = devnode;
	}
done:
	delete[] links;
	delete[] ifaces;
	delete[] ents;

	return ret;
}

DeviceInfo *DeviceEnumerator::search(DeviceMatch &dm) const
{
	DeviceInfo *info = nullptr;

	for (DeviceInfo *dev : devices_) {
		if (dev->busy())
			continue;

		if (dm.match(dev)) {
			info = dev;
			break;
		}
	}

	return info;
}

} /* namespace libcamera */