blob: e9219f528d7e8ada4876649c28b9d0c54ce68117 [file] [log] [blame]
Laurent Pinchart176fb0d2009-12-09 08:39:58 -03001/*
2 * Media device
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Mauro Carvalho Chehabb2cd2742016-03-05 07:13:39 -030023/* We need to access legacy defines from linux/media.h */
24#define __NEED_MEDIA_LEGACY_API
25
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -030026#include <linux/compat.h>
27#include <linux/export.h>
Sakari Ailus665faa92015-12-16 11:32:17 -020028#include <linux/idr.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030029#include <linux/ioctl.h>
Laurent Pinchart140d8812010-08-18 11:41:22 -030030#include <linux/media.h>
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -030031#include <linux/slab.h>
Mauro Carvalho Chehab497e80c2015-12-11 07:23:09 -020032#include <linux/types.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030033
34#include <media/media-device.h>
35#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030036#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030037
Shuah Khane576d60b2015-06-05 17:11:54 -030038#ifdef CONFIG_MEDIA_CONTROLLER
39
Laurent Pinchart140d8812010-08-18 11:41:22 -030040/* -----------------------------------------------------------------------------
41 * Userspace API
42 */
43
44static int media_device_open(struct file *filp)
45{
46 return 0;
47}
48
49static int media_device_close(struct file *filp)
50{
51 return 0;
52}
53
54static int media_device_get_info(struct media_device *dev,
55 struct media_device_info __user *__info)
56{
57 struct media_device_info info;
58
59 memset(&info, 0, sizeof(info));
60
61 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
62 strlcpy(info.model, dev->model, sizeof(info.model));
63 strlcpy(info.serial, dev->serial, sizeof(info.serial));
64 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
65
66 info.media_version = MEDIA_API_VERSION;
67 info.hw_revision = dev->hw_revision;
68 info.driver_version = dev->driver_version;
69
Nicolas THERYb17d3942012-08-07 05:24:59 -030070 if (copy_to_user(__info, &info, sizeof(*__info)))
71 return -EFAULT;
72 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030073}
74
Laurent Pinchart16513332009-12-09 08:40:01 -030075static struct media_entity *find_entity(struct media_device *mdev, u32 id)
76{
77 struct media_entity *entity;
78 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
79
80 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
81
82 spin_lock(&mdev->lock);
83
84 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030085 if (((media_entity_id(entity) == id) && !next) ||
86 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030087 spin_unlock(&mdev->lock);
88 return entity;
89 }
90 }
91
92 spin_unlock(&mdev->lock);
93
94 return NULL;
95}
96
97static long media_device_enum_entities(struct media_device *mdev,
98 struct media_entity_desc __user *uent)
99{
100 struct media_entity *ent;
101 struct media_entity_desc u_ent;
102
Salva Peiróe6a62342014-04-30 19:48:02 +0200103 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300104 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
105 return -EFAULT;
106
107 ent = find_entity(mdev, u_ent.id);
108
109 if (ent == NULL)
110 return -EINVAL;
111
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300112 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300113 if (ent->name)
114 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300115 u_ent.type = ent->function;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200116 u_ent.revision = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300117 u_ent.flags = ent->flags;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200118 u_ent.group_id = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300119 u_ent.pads = ent->num_pads;
120 u_ent.links = ent->num_links - ent->num_backlinks;
Mauro Carvalho Chehabb2cd2742016-03-05 07:13:39 -0300121
122 /*
123 * Workaround for a bug at media-ctl <= v1.10 that makes it to
124 * do the wrong thing if the entity function doesn't belong to
125 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
126 * Ranges.
127 *
128 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
129 * or, otherwise, will be silently ignored by media-ctl when
130 * printing the graphviz diagram. So, map them into the devnode
131 * old range.
132 */
133 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
134 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
135 if (is_media_entity_v4l2_subdev(ent))
136 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
137 else if (ent->function != MEDIA_ENT_F_IO_V4L)
138 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
139 }
140
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300141 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300142 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
143 return -EFAULT;
144 return 0;
145}
146
147static void media_device_kpad_to_upad(const struct media_pad *kpad,
148 struct media_pad_desc *upad)
149{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300150 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300151 upad->index = kpad->index;
152 upad->flags = kpad->flags;
153}
154
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300155static long __media_device_enum_links(struct media_device *mdev,
156 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300157{
158 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300159
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300160 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300161 if (entity == NULL)
162 return -EINVAL;
163
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300164 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300165 unsigned int p;
166
167 for (p = 0; p < entity->num_pads; p++) {
168 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300169
170 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300171 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300172 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300173 return -EFAULT;
174 }
175 }
176
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300177 if (links->links) {
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200178 struct media_link *link;
179 struct media_link_desc __user *ulink_desc = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300180
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200181 list_for_each_entry(link, &entity->links, list) {
182 struct media_link_desc klink_desc;
Laurent Pinchart16513332009-12-09 08:40:01 -0300183
184 /* Ignore backlinks. */
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200185 if (link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300186 continue;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200187 memset(&klink_desc, 0, sizeof(klink_desc));
188 media_device_kpad_to_upad(link->source,
189 &klink_desc.source);
190 media_device_kpad_to_upad(link->sink,
191 &klink_desc.sink);
192 klink_desc.flags = link->flags;
193 if (copy_to_user(ulink_desc, &klink_desc,
194 sizeof(*ulink_desc)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300195 return -EFAULT;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200196 ulink_desc++;
Laurent Pinchart16513332009-12-09 08:40:01 -0300197 }
198 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300199
200 return 0;
201}
202
203static long media_device_enum_links(struct media_device *mdev,
204 struct media_links_enum __user *ulinks)
205{
206 struct media_links_enum links;
207 int rval;
208
209 if (copy_from_user(&links, ulinks, sizeof(links)))
210 return -EFAULT;
211
212 rval = __media_device_enum_links(mdev, &links);
213 if (rval < 0)
214 return rval;
215
Laurent Pinchart16513332009-12-09 08:40:01 -0300216 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
217 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300218
Laurent Pinchart16513332009-12-09 08:40:01 -0300219 return 0;
220}
221
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300222static long media_device_setup_link(struct media_device *mdev,
223 struct media_link_desc __user *_ulink)
224{
225 struct media_link *link = NULL;
226 struct media_link_desc ulink;
227 struct media_entity *source;
228 struct media_entity *sink;
229 int ret;
230
231 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
232 return -EFAULT;
233
234 /* Find the source and sink entities and link.
235 */
236 source = find_entity(mdev, ulink.source.entity);
237 sink = find_entity(mdev, ulink.sink.entity);
238
239 if (source == NULL || sink == NULL)
240 return -EINVAL;
241
242 if (ulink.source.index >= source->num_pads ||
243 ulink.sink.index >= sink->num_pads)
244 return -EINVAL;
245
246 link = media_entity_find_link(&source->pads[ulink.source.index],
247 &sink->pads[ulink.sink.index]);
248 if (link == NULL)
249 return -EINVAL;
250
251 /* Setup the link on both entities. */
252 ret = __media_entity_setup_link(link, ulink.flags);
253
254 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
255 return -EFAULT;
256
257 return ret;
258}
259
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200260#if 0 /* Let's postpone it to Kernel 4.6 */
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300261static long __media_device_get_topology(struct media_device *mdev,
262 struct media_v2_topology *topo)
263{
264 struct media_entity *entity;
265 struct media_interface *intf;
266 struct media_pad *pad;
267 struct media_link *link;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200268 struct media_v2_entity kentity, *uentity;
269 struct media_v2_interface kintf, *uintf;
270 struct media_v2_pad kpad, *upad;
271 struct media_v2_link klink, *ulink;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300272 unsigned int i;
273 int ret = 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300274
275 topo->topology_version = mdev->topology_version;
276
277 /* Get entities and number of entities */
278 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200279 uentity = media_get_uptr(topo->ptr_entities);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300280 media_device_for_each_entity(entity, mdev) {
281 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200282 if (ret || !uentity)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300283 continue;
284
285 if (i > topo->num_entities) {
286 ret = -ENOSPC;
287 continue;
288 }
289
290 /* Copy fields to userspace struct if not error */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200291 memset(&kentity, 0, sizeof(kentity));
292 kentity.id = entity->graph_obj.id;
293 kentity.function = entity->function;
294 strncpy(kentity.name, entity->name,
295 sizeof(kentity.name));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300296
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200297 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300298 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200299 uentity++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300300 }
301 topo->num_entities = i;
302
303 /* Get interfaces and number of interfaces */
304 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200305 uintf = media_get_uptr(topo->ptr_interfaces);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300306 media_device_for_each_intf(intf, mdev) {
307 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200308 if (ret || !uintf)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300309 continue;
310
311 if (i > topo->num_interfaces) {
312 ret = -ENOSPC;
313 continue;
314 }
315
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200316 memset(&kintf, 0, sizeof(kintf));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300317
318 /* Copy intf fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200319 kintf.id = intf->graph_obj.id;
320 kintf.intf_type = intf->type;
321 kintf.flags = intf->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300322
323 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
324 struct media_intf_devnode *devnode;
325
326 devnode = intf_to_devnode(intf);
327
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200328 kintf.devnode.major = devnode->major;
329 kintf.devnode.minor = devnode->minor;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300330 }
331
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200332 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300333 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200334 uintf++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300335 }
336 topo->num_interfaces = i;
337
338 /* Get pads and number of pads */
339 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200340 upad = media_get_uptr(topo->ptr_pads);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300341 media_device_for_each_pad(pad, mdev) {
342 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200343 if (ret || !upad)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300344 continue;
345
346 if (i > topo->num_pads) {
347 ret = -ENOSPC;
348 continue;
349 }
350
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200351 memset(&kpad, 0, sizeof(kpad));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300352
353 /* Copy pad fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200354 kpad.id = pad->graph_obj.id;
355 kpad.entity_id = pad->entity->graph_obj.id;
356 kpad.flags = pad->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300357
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200358 if (copy_to_user(upad, &kpad, sizeof(kpad)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300359 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200360 upad++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300361 }
362 topo->num_pads = i;
363
364 /* Get links and number of links */
365 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200366 ulink = media_get_uptr(topo->ptr_links);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300367 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300368 if (link->is_backlink)
369 continue;
370
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300371 i++;
372
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200373 if (ret || !ulink)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300374 continue;
375
376 if (i > topo->num_links) {
377 ret = -ENOSPC;
378 continue;
379 }
380
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200381 memset(&klink, 0, sizeof(klink));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300382
383 /* Copy link fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200384 klink.id = link->graph_obj.id;
385 klink.source_id = link->gobj0->id;
386 klink.sink_id = link->gobj1->id;
387 klink.flags = link->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300388
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200389 if (copy_to_user(ulink, &klink, sizeof(klink)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300390 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200391 ulink++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300392 }
393 topo->num_links = i;
394
395 return ret;
396}
397
398static long media_device_get_topology(struct media_device *mdev,
399 struct media_v2_topology __user *utopo)
400{
401 struct media_v2_topology ktopo;
402 int ret;
403
Dan Carpentera5c82e52015-12-15 09:00:40 -0200404 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
405 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300406
407 ret = __media_device_get_topology(mdev, &ktopo);
408 if (ret < 0)
409 return ret;
410
Dan Carpentera5c82e52015-12-15 09:00:40 -0200411 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
412 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300413
Dan Carpentera5c82e52015-12-15 09:00:40 -0200414 return 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300415}
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200416#endif
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300417
Laurent Pinchart140d8812010-08-18 11:41:22 -0300418static long media_device_ioctl(struct file *filp, unsigned int cmd,
419 unsigned long arg)
420{
421 struct media_devnode *devnode = media_devnode_data(filp);
422 struct media_device *dev = to_media_device(devnode);
423 long ret;
424
425 switch (cmd) {
426 case MEDIA_IOC_DEVICE_INFO:
427 ret = media_device_get_info(dev,
428 (struct media_device_info __user *)arg);
429 break;
430
Laurent Pinchart16513332009-12-09 08:40:01 -0300431 case MEDIA_IOC_ENUM_ENTITIES:
432 ret = media_device_enum_entities(dev,
433 (struct media_entity_desc __user *)arg);
434 break;
435
436 case MEDIA_IOC_ENUM_LINKS:
437 mutex_lock(&dev->graph_mutex);
438 ret = media_device_enum_links(dev,
439 (struct media_links_enum __user *)arg);
440 mutex_unlock(&dev->graph_mutex);
441 break;
442
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300443 case MEDIA_IOC_SETUP_LINK:
444 mutex_lock(&dev->graph_mutex);
445 ret = media_device_setup_link(dev,
446 (struct media_link_desc __user *)arg);
447 mutex_unlock(&dev->graph_mutex);
448 break;
449
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200450#if 0 /* Let's postpone it to Kernel 4.6 */
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300451 case MEDIA_IOC_G_TOPOLOGY:
452 mutex_lock(&dev->graph_mutex);
453 ret = media_device_get_topology(dev,
454 (struct media_v2_topology __user *)arg);
455 mutex_unlock(&dev->graph_mutex);
456 break;
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200457#endif
Laurent Pinchart140d8812010-08-18 11:41:22 -0300458 default:
459 ret = -ENOIOCTLCMD;
460 }
461
462 return ret;
463}
464
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300465#ifdef CONFIG_COMPAT
466
467struct media_links_enum32 {
468 __u32 entity;
469 compat_uptr_t pads; /* struct media_pad_desc * */
470 compat_uptr_t links; /* struct media_link_desc * */
471 __u32 reserved[4];
472};
473
474static long media_device_enum_links32(struct media_device *mdev,
475 struct media_links_enum32 __user *ulinks)
476{
477 struct media_links_enum links;
478 compat_uptr_t pads_ptr, links_ptr;
479
480 memset(&links, 0, sizeof(links));
481
482 if (get_user(links.entity, &ulinks->entity)
483 || get_user(pads_ptr, &ulinks->pads)
484 || get_user(links_ptr, &ulinks->links))
485 return -EFAULT;
486
487 links.pads = compat_ptr(pads_ptr);
488 links.links = compat_ptr(links_ptr);
489
490 return __media_device_enum_links(mdev, &links);
491}
492
493#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
494
495static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
496 unsigned long arg)
497{
498 struct media_devnode *devnode = media_devnode_data(filp);
499 struct media_device *dev = to_media_device(devnode);
500 long ret;
501
502 switch (cmd) {
503 case MEDIA_IOC_DEVICE_INFO:
504 case MEDIA_IOC_ENUM_ENTITIES:
505 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200506#if 0 /* Let's postpone it to Kernel 4.6 */
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300507 case MEDIA_IOC_G_TOPOLOGY:
Mauro Carvalho Chehabbe0270ec2015-12-28 12:03:47 -0200508#endif
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300509 return media_device_ioctl(filp, cmd, arg);
510
511 case MEDIA_IOC_ENUM_LINKS32:
512 mutex_lock(&dev->graph_mutex);
513 ret = media_device_enum_links32(dev,
514 (struct media_links_enum32 __user *)arg);
515 mutex_unlock(&dev->graph_mutex);
516 break;
517
518 default:
519 ret = -ENOIOCTLCMD;
520 }
521
522 return ret;
523}
524#endif /* CONFIG_COMPAT */
525
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300526static const struct media_file_operations media_device_fops = {
527 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300528 .open = media_device_open,
529 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300530#ifdef CONFIG_COMPAT
531 .compat_ioctl = media_device_compat_ioctl,
532#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300533 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300534};
535
536/* -----------------------------------------------------------------------------
537 * sysfs
538 */
539
540static ssize_t show_model(struct device *cd,
541 struct device_attribute *attr, char *buf)
542{
543 struct media_device *mdev = to_media_device(to_media_devnode(cd));
544
545 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
546}
547
548static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
549
550/* -----------------------------------------------------------------------------
551 * Registration/unregistration
552 */
553
554static void media_device_release(struct media_devnode *mdev)
555{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300556 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300557}
558
559/**
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200560 * media_device_register_entity - Register an entity with a media device
561 * @mdev: The media device
562 * @entity: The entity
563 */
564int __must_check media_device_register_entity(struct media_device *mdev,
565 struct media_entity *entity)
566{
567 unsigned int i;
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200568 int ret;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200569
570 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
571 entity->function == MEDIA_ENT_F_UNKNOWN)
572 dev_warn(mdev->dev,
573 "Entity type for entity %s was not initialized!\n",
574 entity->name);
575
576 /* Warn if we apparently re-register an entity */
577 WARN_ON(entity->graph_obj.mdev != NULL);
578 entity->graph_obj.mdev = mdev;
579 INIT_LIST_HEAD(&entity->links);
580 entity->num_links = 0;
581 entity->num_backlinks = 0;
582
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200583 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
584 return -ENOMEM;
585
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200586 spin_lock(&mdev->lock);
Sakari Ailus665faa92015-12-16 11:32:17 -0200587
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200588 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
589 &entity->internal_idx);
590 if (ret < 0) {
Sakari Ailus665faa92015-12-16 11:32:17 -0200591 spin_unlock(&mdev->lock);
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200592 return ret;
Sakari Ailus665faa92015-12-16 11:32:17 -0200593 }
594
595 mdev->entity_internal_idx_max =
596 max(mdev->entity_internal_idx_max, entity->internal_idx);
597
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200598 /* Initialize media_gobj embedded at the entity */
599 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
600
601 /* Initialize objects at the pads */
602 for (i = 0; i < entity->num_pads; i++)
603 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
604 &entity->pads[i].graph_obj);
605
606 spin_unlock(&mdev->lock);
607
608 return 0;
609}
610EXPORT_SYMBOL_GPL(media_device_register_entity);
611
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200612static void __media_device_unregister_entity(struct media_entity *entity)
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200613{
614 struct media_device *mdev = entity->graph_obj.mdev;
615 struct media_link *link, *tmp;
616 struct media_interface *intf;
617 unsigned int i;
618
Sakari Ailus665faa92015-12-16 11:32:17 -0200619 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
620
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200621 /* Remove all interface links pointing to this entity */
622 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
623 list_for_each_entry_safe(link, tmp, &intf->links, list) {
624 if (link->entity == entity)
625 __media_remove_intf_link(link);
626 }
627 }
628
629 /* Remove all data links that belong to this entity */
630 __media_entity_remove_links(entity);
631
632 /* Remove all pads that belong to this entity */
633 for (i = 0; i < entity->num_pads; i++)
634 media_gobj_destroy(&entity->pads[i].graph_obj);
635
636 /* Remove the entity */
637 media_gobj_destroy(&entity->graph_obj);
638
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200639 entity->graph_obj.mdev = NULL;
640}
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200641
642void media_device_unregister_entity(struct media_entity *entity)
643{
644 struct media_device *mdev = entity->graph_obj.mdev;
645
646 if (mdev == NULL)
647 return;
648
649 spin_lock(&mdev->lock);
650 __media_device_unregister_entity(entity);
651 spin_unlock(&mdev->lock);
652}
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200653EXPORT_SYMBOL_GPL(media_device_unregister_entity);
654
655/**
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200656 * media_device_init() - initialize a media device
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300657 * @mdev: The media device
658 *
659 * The caller is responsible for initializing the media device before
660 * registration. The following fields must be set:
661 *
662 * - dev must point to the parent device
663 * - model must be filled with the device model name
664 */
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200665void media_device_init(struct media_device *mdev)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300666{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300667 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300668 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300669 INIT_LIST_HEAD(&mdev->pads);
670 INIT_LIST_HEAD(&mdev->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300671 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300672 mutex_init(&mdev->graph_mutex);
Sakari Ailus665faa92015-12-16 11:32:17 -0200673 ida_init(&mdev->entity_internal_idx);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300674
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200675 dev_dbg(mdev->dev, "Media device initialized\n");
676}
677EXPORT_SYMBOL_GPL(media_device_init);
678
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200679void media_device_cleanup(struct media_device *mdev)
680{
Sakari Ailus665faa92015-12-16 11:32:17 -0200681 ida_destroy(&mdev->entity_internal_idx);
682 mdev->entity_internal_idx_max = 0;
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200683 mutex_destroy(&mdev->graph_mutex);
684}
685EXPORT_SYMBOL_GPL(media_device_cleanup);
686
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200687int __must_check __media_device_register(struct media_device *mdev,
688 struct module *owner)
689{
690 int ret;
691
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300692 /* Register the device node. */
693 mdev->devnode.fops = &media_device_fops;
694 mdev->devnode.parent = mdev->dev;
695 mdev->devnode.release = media_device_release;
Javier Martinez Canillas8a9507962015-12-11 20:57:09 -0200696
697 /* Set version 0 to indicate user-space that the graph is static */
698 mdev->topology_version = 0;
699
Sakari Ailus85de7212013-12-12 12:38:17 -0300700 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300701 if (ret < 0)
702 return ret;
703
704 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
705 if (ret < 0) {
706 media_devnode_unregister(&mdev->devnode);
707 return ret;
708 }
709
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300710 dev_dbg(mdev->dev, "Media device registered\n");
711
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300712 return 0;
713}
Sakari Ailus85de7212013-12-12 12:38:17 -0300714EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300715
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300716void media_device_unregister(struct media_device *mdev)
717{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300718 struct media_entity *entity;
719 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300720 struct media_interface *intf, *tmp_intf;
721
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200722 if (mdev == NULL)
Javier Martinez Canillas223d19c2015-12-11 20:57:07 -0200723 return;
724
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200725 spin_lock(&mdev->lock);
726
727 /* Check if mdev was ever registered at all */
728 if (!media_devnode_is_registered(&mdev->devnode)) {
729 spin_unlock(&mdev->lock);
730 return;
731 }
732
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300733 /* Remove all entities from the media device */
734 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200735 __media_device_unregister_entity(entity);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300736
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300737 /* Remove all interfaces from the media device */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300738 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
739 graph_obj.list) {
740 __media_remove_intf_links(intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200741 media_gobj_destroy(&intf->graph_obj);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300742 kfree(intf);
743 }
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200744
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300745 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300746
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300747 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
748 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300749
750 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300751}
752EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300753
Shuah Khand062f912015-06-03 12:12:53 -0300754static void media_device_release_devres(struct device *dev, void *res)
755{
756}
757
Shuah Khand062f912015-06-03 12:12:53 -0300758struct media_device *media_device_get_devres(struct device *dev)
759{
760 struct media_device *mdev;
761
762 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
763 if (mdev)
764 return mdev;
765
766 mdev = devres_alloc(media_device_release_devres,
767 sizeof(struct media_device), GFP_KERNEL);
768 if (!mdev)
769 return NULL;
770 return devres_get(dev, mdev, NULL, NULL);
771}
772EXPORT_SYMBOL_GPL(media_device_get_devres);
773
Shuah Khand062f912015-06-03 12:12:53 -0300774struct media_device *media_device_find_devres(struct device *dev)
775{
776 return devres_find(dev, media_device_release_devres, NULL, NULL);
777}
778EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d60b2015-06-05 17:11:54 -0300779
780#endif /* CONFIG_MEDIA_CONTROLLER */