blob: 3cfd7af8c5cab06607a94c716951daa6166b60e2 [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>
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -030033#include <linux/pci.h>
34#include <linux/usb.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030035
36#include <media/media-device.h>
37#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030038#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030039
Shuah Khane576d602015-06-05 17:11:54 -030040#ifdef CONFIG_MEDIA_CONTROLLER
41
Laurent Pinchart140d8812010-08-18 11:41:22 -030042/* -----------------------------------------------------------------------------
43 * Userspace API
44 */
45
Sakari Ailus0629e992016-02-22 17:47:04 -030046static inline void __user *media_get_uptr(__u64 arg)
47{
48 return (void __user *)(uintptr_t)arg;
49}
50
Laurent Pinchart140d8812010-08-18 11:41:22 -030051static int media_device_open(struct file *filp)
52{
53 return 0;
54}
55
56static int media_device_close(struct file *filp)
57{
58 return 0;
59}
60
61static int media_device_get_info(struct media_device *dev,
62 struct media_device_info __user *__info)
63{
64 struct media_device_info info;
65
66 memset(&info, 0, sizeof(info));
67
Mauro Carvalho Chehabbb07bd62016-02-11 14:24:23 -020068 if (dev->driver_name[0])
69 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
70 else
71 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
72
Laurent Pinchart140d8812010-08-18 11:41:22 -030073 strlcpy(info.model, dev->model, sizeof(info.model));
74 strlcpy(info.serial, dev->serial, sizeof(info.serial));
75 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
76
77 info.media_version = MEDIA_API_VERSION;
78 info.hw_revision = dev->hw_revision;
79 info.driver_version = dev->driver_version;
80
Nicolas THERYb17d3942012-08-07 05:24:59 -030081 if (copy_to_user(__info, &info, sizeof(*__info)))
82 return -EFAULT;
83 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030084}
85
Laurent Pinchart16513332009-12-09 08:40:01 -030086static struct media_entity *find_entity(struct media_device *mdev, u32 id)
87{
88 struct media_entity *entity;
89 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
90
91 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
92
93 spin_lock(&mdev->lock);
94
95 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030096 if (((media_entity_id(entity) == id) && !next) ||
97 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030098 spin_unlock(&mdev->lock);
99 return entity;
100 }
101 }
102
103 spin_unlock(&mdev->lock);
104
105 return NULL;
106}
107
108static long media_device_enum_entities(struct media_device *mdev,
109 struct media_entity_desc __user *uent)
110{
111 struct media_entity *ent;
112 struct media_entity_desc u_ent;
113
Salva Peiróe6a62342014-04-30 19:48:02 +0200114 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300115 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
116 return -EFAULT;
117
118 ent = find_entity(mdev, u_ent.id);
119
120 if (ent == NULL)
121 return -EINVAL;
122
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300123 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300124 if (ent->name)
125 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300126 u_ent.type = ent->function;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200127 u_ent.revision = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300128 u_ent.flags = ent->flags;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200129 u_ent.group_id = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300130 u_ent.pads = ent->num_pads;
131 u_ent.links = ent->num_links - ent->num_backlinks;
Mauro Carvalho Chehabb2cd2742016-03-05 07:13:39 -0300132
133 /*
134 * Workaround for a bug at media-ctl <= v1.10 that makes it to
135 * do the wrong thing if the entity function doesn't belong to
136 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
137 * Ranges.
138 *
139 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
140 * or, otherwise, will be silently ignored by media-ctl when
141 * printing the graphviz diagram. So, map them into the devnode
142 * old range.
143 */
144 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
145 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
146 if (is_media_entity_v4l2_subdev(ent))
147 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
148 else if (ent->function != MEDIA_ENT_F_IO_V4L)
149 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
150 }
151
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300152 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300153 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
154 return -EFAULT;
155 return 0;
156}
157
158static void media_device_kpad_to_upad(const struct media_pad *kpad,
159 struct media_pad_desc *upad)
160{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300161 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300162 upad->index = kpad->index;
163 upad->flags = kpad->flags;
164}
165
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300166static long __media_device_enum_links(struct media_device *mdev,
167 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300168{
169 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300170
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300171 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300172 if (entity == NULL)
173 return -EINVAL;
174
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300175 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300176 unsigned int p;
177
178 for (p = 0; p < entity->num_pads; p++) {
179 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300180
181 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300182 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300183 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300184 return -EFAULT;
185 }
186 }
187
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300188 if (links->links) {
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200189 struct media_link *link;
190 struct media_link_desc __user *ulink_desc = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300191
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200192 list_for_each_entry(link, &entity->links, list) {
193 struct media_link_desc klink_desc;
Laurent Pinchart16513332009-12-09 08:40:01 -0300194
195 /* Ignore backlinks. */
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200196 if (link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300197 continue;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200198 memset(&klink_desc, 0, sizeof(klink_desc));
199 media_device_kpad_to_upad(link->source,
200 &klink_desc.source);
201 media_device_kpad_to_upad(link->sink,
202 &klink_desc.sink);
203 klink_desc.flags = link->flags;
204 if (copy_to_user(ulink_desc, &klink_desc,
205 sizeof(*ulink_desc)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300206 return -EFAULT;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200207 ulink_desc++;
Laurent Pinchart16513332009-12-09 08:40:01 -0300208 }
209 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300210
211 return 0;
212}
213
214static long media_device_enum_links(struct media_device *mdev,
215 struct media_links_enum __user *ulinks)
216{
217 struct media_links_enum links;
218 int rval;
219
220 if (copy_from_user(&links, ulinks, sizeof(links)))
221 return -EFAULT;
222
223 rval = __media_device_enum_links(mdev, &links);
224 if (rval < 0)
225 return rval;
226
Laurent Pinchart16513332009-12-09 08:40:01 -0300227 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
228 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300229
Laurent Pinchart16513332009-12-09 08:40:01 -0300230 return 0;
231}
232
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300233static long media_device_setup_link(struct media_device *mdev,
234 struct media_link_desc __user *_ulink)
235{
236 struct media_link *link = NULL;
237 struct media_link_desc ulink;
238 struct media_entity *source;
239 struct media_entity *sink;
240 int ret;
241
242 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
243 return -EFAULT;
244
245 /* Find the source and sink entities and link.
246 */
247 source = find_entity(mdev, ulink.source.entity);
248 sink = find_entity(mdev, ulink.sink.entity);
249
250 if (source == NULL || sink == NULL)
251 return -EINVAL;
252
253 if (ulink.source.index >= source->num_pads ||
254 ulink.sink.index >= sink->num_pads)
255 return -EINVAL;
256
257 link = media_entity_find_link(&source->pads[ulink.source.index],
258 &sink->pads[ulink.sink.index]);
259 if (link == NULL)
260 return -EINVAL;
261
262 /* Setup the link on both entities. */
263 ret = __media_entity_setup_link(link, ulink.flags);
264
265 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
266 return -EFAULT;
267
268 return ret;
269}
270
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300271static long __media_device_get_topology(struct media_device *mdev,
272 struct media_v2_topology *topo)
273{
274 struct media_entity *entity;
275 struct media_interface *intf;
276 struct media_pad *pad;
277 struct media_link *link;
Sakari Ailusd37410c2016-02-22 17:47:03 -0300278 struct media_v2_entity kentity, __user *uentity;
279 struct media_v2_interface kintf, __user *uintf;
280 struct media_v2_pad kpad, __user *upad;
281 struct media_v2_link klink, __user *ulink;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300282 unsigned int i;
283 int ret = 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300284
285 topo->topology_version = mdev->topology_version;
286
287 /* Get entities and number of entities */
288 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200289 uentity = media_get_uptr(topo->ptr_entities);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300290 media_device_for_each_entity(entity, mdev) {
291 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200292 if (ret || !uentity)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300293 continue;
294
295 if (i > topo->num_entities) {
296 ret = -ENOSPC;
297 continue;
298 }
299
300 /* Copy fields to userspace struct if not error */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200301 memset(&kentity, 0, sizeof(kentity));
302 kentity.id = entity->graph_obj.id;
303 kentity.function = entity->function;
304 strncpy(kentity.name, entity->name,
305 sizeof(kentity.name));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300306
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200307 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300308 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200309 uentity++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300310 }
311 topo->num_entities = i;
312
313 /* Get interfaces and number of interfaces */
314 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200315 uintf = media_get_uptr(topo->ptr_interfaces);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300316 media_device_for_each_intf(intf, mdev) {
317 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200318 if (ret || !uintf)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300319 continue;
320
321 if (i > topo->num_interfaces) {
322 ret = -ENOSPC;
323 continue;
324 }
325
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200326 memset(&kintf, 0, sizeof(kintf));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300327
328 /* Copy intf fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200329 kintf.id = intf->graph_obj.id;
330 kintf.intf_type = intf->type;
331 kintf.flags = intf->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300332
333 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
334 struct media_intf_devnode *devnode;
335
336 devnode = intf_to_devnode(intf);
337
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200338 kintf.devnode.major = devnode->major;
339 kintf.devnode.minor = devnode->minor;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300340 }
341
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200342 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300343 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200344 uintf++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300345 }
346 topo->num_interfaces = i;
347
348 /* Get pads and number of pads */
349 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200350 upad = media_get_uptr(topo->ptr_pads);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300351 media_device_for_each_pad(pad, mdev) {
352 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200353 if (ret || !upad)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300354 continue;
355
356 if (i > topo->num_pads) {
357 ret = -ENOSPC;
358 continue;
359 }
360
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200361 memset(&kpad, 0, sizeof(kpad));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300362
363 /* Copy pad fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200364 kpad.id = pad->graph_obj.id;
365 kpad.entity_id = pad->entity->graph_obj.id;
366 kpad.flags = pad->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300367
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200368 if (copy_to_user(upad, &kpad, sizeof(kpad)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300369 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200370 upad++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300371 }
372 topo->num_pads = i;
373
374 /* Get links and number of links */
375 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200376 ulink = media_get_uptr(topo->ptr_links);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300377 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300378 if (link->is_backlink)
379 continue;
380
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300381 i++;
382
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200383 if (ret || !ulink)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300384 continue;
385
386 if (i > topo->num_links) {
387 ret = -ENOSPC;
388 continue;
389 }
390
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200391 memset(&klink, 0, sizeof(klink));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300392
393 /* Copy link fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200394 klink.id = link->graph_obj.id;
395 klink.source_id = link->gobj0->id;
396 klink.sink_id = link->gobj1->id;
397 klink.flags = link->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300398
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200399 if (copy_to_user(ulink, &klink, sizeof(klink)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300400 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200401 ulink++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300402 }
403 topo->num_links = i;
404
405 return ret;
406}
407
408static long media_device_get_topology(struct media_device *mdev,
409 struct media_v2_topology __user *utopo)
410{
411 struct media_v2_topology ktopo;
412 int ret;
413
Dan Carpentera5c82e52015-12-15 09:00:40 -0200414 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
415 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300416
417 ret = __media_device_get_topology(mdev, &ktopo);
418 if (ret < 0)
419 return ret;
420
Dan Carpentera5c82e52015-12-15 09:00:40 -0200421 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
422 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300423
Dan Carpentera5c82e52015-12-15 09:00:40 -0200424 return 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300425}
426
Laurent Pinchart140d8812010-08-18 11:41:22 -0300427static long media_device_ioctl(struct file *filp, unsigned int cmd,
428 unsigned long arg)
429{
430 struct media_devnode *devnode = media_devnode_data(filp);
431 struct media_device *dev = to_media_device(devnode);
432 long ret;
433
434 switch (cmd) {
435 case MEDIA_IOC_DEVICE_INFO:
436 ret = media_device_get_info(dev,
437 (struct media_device_info __user *)arg);
438 break;
439
Laurent Pinchart16513332009-12-09 08:40:01 -0300440 case MEDIA_IOC_ENUM_ENTITIES:
441 ret = media_device_enum_entities(dev,
442 (struct media_entity_desc __user *)arg);
443 break;
444
445 case MEDIA_IOC_ENUM_LINKS:
446 mutex_lock(&dev->graph_mutex);
447 ret = media_device_enum_links(dev,
448 (struct media_links_enum __user *)arg);
449 mutex_unlock(&dev->graph_mutex);
450 break;
451
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300452 case MEDIA_IOC_SETUP_LINK:
453 mutex_lock(&dev->graph_mutex);
454 ret = media_device_setup_link(dev,
455 (struct media_link_desc __user *)arg);
456 mutex_unlock(&dev->graph_mutex);
457 break;
458
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300459 case MEDIA_IOC_G_TOPOLOGY:
460 mutex_lock(&dev->graph_mutex);
461 ret = media_device_get_topology(dev,
462 (struct media_v2_topology __user *)arg);
463 mutex_unlock(&dev->graph_mutex);
464 break;
Mauro Carvalho Chehab333a6512016-01-25 07:29:13 -0200465
Laurent Pinchart140d8812010-08-18 11:41:22 -0300466 default:
467 ret = -ENOIOCTLCMD;
468 }
469
470 return ret;
471}
472
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300473#ifdef CONFIG_COMPAT
474
475struct media_links_enum32 {
476 __u32 entity;
477 compat_uptr_t pads; /* struct media_pad_desc * */
478 compat_uptr_t links; /* struct media_link_desc * */
479 __u32 reserved[4];
480};
481
482static long media_device_enum_links32(struct media_device *mdev,
483 struct media_links_enum32 __user *ulinks)
484{
485 struct media_links_enum links;
486 compat_uptr_t pads_ptr, links_ptr;
487
488 memset(&links, 0, sizeof(links));
489
490 if (get_user(links.entity, &ulinks->entity)
491 || get_user(pads_ptr, &ulinks->pads)
492 || get_user(links_ptr, &ulinks->links))
493 return -EFAULT;
494
495 links.pads = compat_ptr(pads_ptr);
496 links.links = compat_ptr(links_ptr);
497
498 return __media_device_enum_links(mdev, &links);
499}
500
501#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
502
503static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
504 unsigned long arg)
505{
506 struct media_devnode *devnode = media_devnode_data(filp);
507 struct media_device *dev = to_media_device(devnode);
508 long ret;
509
510 switch (cmd) {
511 case MEDIA_IOC_DEVICE_INFO:
512 case MEDIA_IOC_ENUM_ENTITIES:
513 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300514 case MEDIA_IOC_G_TOPOLOGY:
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300515 return media_device_ioctl(filp, cmd, arg);
516
517 case MEDIA_IOC_ENUM_LINKS32:
518 mutex_lock(&dev->graph_mutex);
519 ret = media_device_enum_links32(dev,
520 (struct media_links_enum32 __user *)arg);
521 mutex_unlock(&dev->graph_mutex);
522 break;
523
524 default:
525 ret = -ENOIOCTLCMD;
526 }
527
528 return ret;
529}
530#endif /* CONFIG_COMPAT */
531
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300532static const struct media_file_operations media_device_fops = {
533 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300534 .open = media_device_open,
535 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300536#ifdef CONFIG_COMPAT
537 .compat_ioctl = media_device_compat_ioctl,
538#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300539 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300540};
541
542/* -----------------------------------------------------------------------------
543 * sysfs
544 */
545
546static ssize_t show_model(struct device *cd,
547 struct device_attribute *attr, char *buf)
548{
549 struct media_device *mdev = to_media_device(to_media_devnode(cd));
550
551 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
552}
553
554static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
555
556/* -----------------------------------------------------------------------------
557 * Registration/unregistration
558 */
559
560static void media_device_release(struct media_devnode *mdev)
561{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300562 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300563}
564
565/**
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200566 * media_device_register_entity - Register an entity with a media device
567 * @mdev: The media device
568 * @entity: The entity
569 */
570int __must_check media_device_register_entity(struct media_device *mdev,
571 struct media_entity *entity)
572{
Shuah Khanafcbdb52016-02-11 21:41:21 -0200573 struct media_entity_notify *notify, *next;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200574 unsigned int i;
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200575 int ret;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200576
577 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
578 entity->function == MEDIA_ENT_F_UNKNOWN)
579 dev_warn(mdev->dev,
580 "Entity type for entity %s was not initialized!\n",
581 entity->name);
582
583 /* Warn if we apparently re-register an entity */
584 WARN_ON(entity->graph_obj.mdev != NULL);
585 entity->graph_obj.mdev = mdev;
586 INIT_LIST_HEAD(&entity->links);
587 entity->num_links = 0;
588 entity->num_backlinks = 0;
589
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200590 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
591 return -ENOMEM;
592
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200593 spin_lock(&mdev->lock);
Sakari Ailus665faa92015-12-16 11:32:17 -0200594
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200595 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
596 &entity->internal_idx);
597 if (ret < 0) {
Sakari Ailus665faa92015-12-16 11:32:17 -0200598 spin_unlock(&mdev->lock);
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200599 return ret;
Sakari Ailus665faa92015-12-16 11:32:17 -0200600 }
601
602 mdev->entity_internal_idx_max =
603 max(mdev->entity_internal_idx_max, entity->internal_idx);
604
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200605 /* Initialize media_gobj embedded at the entity */
606 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
607
608 /* Initialize objects at the pads */
609 for (i = 0; i < entity->num_pads; i++)
610 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
611 &entity->pads[i].graph_obj);
612
Shuah Khanafcbdb52016-02-11 21:41:21 -0200613 /* invoke entity_notify callbacks */
614 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
615 (notify)->notify(entity, notify->notify_data);
616 }
617
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200618 spin_unlock(&mdev->lock);
619
Sakari Ailus0c426c42016-02-21 13:25:08 -0300620 mutex_lock(&mdev->graph_mutex);
621 if (mdev->entity_internal_idx_max
622 >= mdev->pm_count_walk.ent_enum.idx_max) {
623 struct media_entity_graph new = { .top = 0 };
624
625 /*
626 * Initialise the new graph walk before cleaning up
627 * the old one in order not to spoil the graph walk
628 * object of the media device if graph walk init fails.
629 */
630 ret = media_entity_graph_walk_init(&new, mdev);
631 if (ret) {
632 mutex_unlock(&mdev->graph_mutex);
633 return ret;
634 }
635 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
636 mdev->pm_count_walk = new;
637 }
638 mutex_unlock(&mdev->graph_mutex);
639
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200640 return 0;
641}
642EXPORT_SYMBOL_GPL(media_device_register_entity);
643
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200644static void __media_device_unregister_entity(struct media_entity *entity)
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200645{
646 struct media_device *mdev = entity->graph_obj.mdev;
647 struct media_link *link, *tmp;
648 struct media_interface *intf;
649 unsigned int i;
650
Sakari Ailus665faa92015-12-16 11:32:17 -0200651 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
652
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200653 /* Remove all interface links pointing to this entity */
654 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
655 list_for_each_entry_safe(link, tmp, &intf->links, list) {
656 if (link->entity == entity)
657 __media_remove_intf_link(link);
658 }
659 }
660
661 /* Remove all data links that belong to this entity */
662 __media_entity_remove_links(entity);
663
664 /* Remove all pads that belong to this entity */
665 for (i = 0; i < entity->num_pads; i++)
666 media_gobj_destroy(&entity->pads[i].graph_obj);
667
668 /* Remove the entity */
669 media_gobj_destroy(&entity->graph_obj);
670
Shuah Khanafcbdb52016-02-11 21:41:21 -0200671 /* invoke entity_notify callbacks to handle entity removal?? */
672
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200673 entity->graph_obj.mdev = NULL;
674}
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200675
676void media_device_unregister_entity(struct media_entity *entity)
677{
678 struct media_device *mdev = entity->graph_obj.mdev;
679
680 if (mdev == NULL)
681 return;
682
683 spin_lock(&mdev->lock);
684 __media_device_unregister_entity(entity);
685 spin_unlock(&mdev->lock);
686}
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200687EXPORT_SYMBOL_GPL(media_device_unregister_entity);
688
689/**
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200690 * media_device_init() - initialize a media device
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300691 * @mdev: The media device
692 *
693 * The caller is responsible for initializing the media device before
694 * registration. The following fields must be set:
695 *
696 * - dev must point to the parent device
697 * - model must be filled with the device model name
698 */
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200699void media_device_init(struct media_device *mdev)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300700{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300701 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300702 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300703 INIT_LIST_HEAD(&mdev->pads);
704 INIT_LIST_HEAD(&mdev->links);
Shuah Khanafcbdb52016-02-11 21:41:21 -0200705 INIT_LIST_HEAD(&mdev->entity_notify);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300706 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300707 mutex_init(&mdev->graph_mutex);
Sakari Ailus665faa92015-12-16 11:32:17 -0200708 ida_init(&mdev->entity_internal_idx);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300709
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200710 dev_dbg(mdev->dev, "Media device initialized\n");
711}
712EXPORT_SYMBOL_GPL(media_device_init);
713
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200714void media_device_cleanup(struct media_device *mdev)
715{
Sakari Ailus665faa92015-12-16 11:32:17 -0200716 ida_destroy(&mdev->entity_internal_idx);
717 mdev->entity_internal_idx_max = 0;
Sakari Ailus0c426c42016-02-21 13:25:08 -0300718 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200719 mutex_destroy(&mdev->graph_mutex);
720}
721EXPORT_SYMBOL_GPL(media_device_cleanup);
722
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200723int __must_check __media_device_register(struct media_device *mdev,
724 struct module *owner)
725{
726 int ret;
727
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300728 /* Register the device node. */
729 mdev->devnode.fops = &media_device_fops;
730 mdev->devnode.parent = mdev->dev;
731 mdev->devnode.release = media_device_release;
Javier Martinez Canillas8a950792015-12-11 20:57:09 -0200732
733 /* Set version 0 to indicate user-space that the graph is static */
734 mdev->topology_version = 0;
735
Sakari Ailus85de7212013-12-12 12:38:17 -0300736 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300737 if (ret < 0)
738 return ret;
739
740 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
741 if (ret < 0) {
742 media_devnode_unregister(&mdev->devnode);
743 return ret;
744 }
745
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300746 dev_dbg(mdev->dev, "Media device registered\n");
747
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300748 return 0;
749}
Sakari Ailus85de7212013-12-12 12:38:17 -0300750EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300751
Shuah Khanafcbdb52016-02-11 21:41:21 -0200752int __must_check media_device_register_entity_notify(struct media_device *mdev,
753 struct media_entity_notify *nptr)
754{
755 spin_lock(&mdev->lock);
756 list_add_tail(&nptr->list, &mdev->entity_notify);
757 spin_unlock(&mdev->lock);
758 return 0;
759}
760EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
761
762/*
763 * Note: Should be called with mdev->lock held.
764 */
765static void __media_device_unregister_entity_notify(struct media_device *mdev,
766 struct media_entity_notify *nptr)
767{
768 list_del(&nptr->list);
769}
770
771void media_device_unregister_entity_notify(struct media_device *mdev,
772 struct media_entity_notify *nptr)
773{
774 spin_lock(&mdev->lock);
775 __media_device_unregister_entity_notify(mdev, nptr);
776 spin_unlock(&mdev->lock);
777}
778EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
779
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300780void media_device_unregister(struct media_device *mdev)
781{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300782 struct media_entity *entity;
783 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300784 struct media_interface *intf, *tmp_intf;
Shuah Khanafcbdb52016-02-11 21:41:21 -0200785 struct media_entity_notify *notify, *nextp;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300786
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200787 if (mdev == NULL)
Javier Martinez Canillas223d19c2015-12-11 20:57:07 -0200788 return;
789
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200790 spin_lock(&mdev->lock);
791
792 /* Check if mdev was ever registered at all */
793 if (!media_devnode_is_registered(&mdev->devnode)) {
794 spin_unlock(&mdev->lock);
795 return;
796 }
797
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300798 /* Remove all entities from the media device */
799 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200800 __media_device_unregister_entity(entity);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300801
Shuah Khanafcbdb52016-02-11 21:41:21 -0200802 /* Remove all entity_notify callbacks from the media device */
803 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
804 __media_device_unregister_entity_notify(mdev, notify);
805
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300806 /* Remove all interfaces from the media device */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300807 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
808 graph_obj.list) {
809 __media_remove_intf_links(intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200810 media_gobj_destroy(&intf->graph_obj);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300811 kfree(intf);
812 }
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200813
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300814 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300815
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300816 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
817 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300818
819 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300820}
821EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300822
Shuah Khand062f912015-06-03 12:12:53 -0300823static void media_device_release_devres(struct device *dev, void *res)
824{
825}
826
Shuah Khand062f912015-06-03 12:12:53 -0300827struct media_device *media_device_get_devres(struct device *dev)
828{
829 struct media_device *mdev;
830
831 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
832 if (mdev)
833 return mdev;
834
835 mdev = devres_alloc(media_device_release_devres,
836 sizeof(struct media_device), GFP_KERNEL);
837 if (!mdev)
838 return NULL;
839 return devres_get(dev, mdev, NULL, NULL);
840}
841EXPORT_SYMBOL_GPL(media_device_get_devres);
842
Shuah Khand062f912015-06-03 12:12:53 -0300843struct media_device *media_device_find_devres(struct device *dev)
844{
845 return devres_find(dev, media_device_release_devres, NULL, NULL);
846}
847EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d602015-06-05 17:11:54 -0300848
Mauro Carvalho Chehabb34ecd52016-05-05 08:01:34 -0300849#if IS_ENABLED(CONFIG_PCI)
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300850void media_device_pci_init(struct media_device *mdev,
851 struct pci_dev *pci_dev,
852 const char *name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300853{
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300854 mdev->dev = &pci_dev->dev;
855
856 if (name)
857 strlcpy(mdev->model, name, sizeof(mdev->model));
858 else
859 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
860
861 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
862
863 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
864 | pci_dev->subsystem_device;
865
866 mdev->driver_version = LINUX_VERSION_CODE;
867
868 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300869}
870EXPORT_SYMBOL_GPL(media_device_pci_init);
Mauro Carvalho Chehabb34ecd52016-05-05 08:01:34 -0300871#endif
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300872
Mauro Carvalho Chehabb34ecd52016-05-05 08:01:34 -0300873#if IS_ENABLED(CONFIG_USB)
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300874void __media_device_usb_init(struct media_device *mdev,
875 struct usb_device *udev,
876 const char *board_name,
877 const char *driver_name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300878{
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300879 mdev->dev = &udev->dev;
880
881 if (driver_name)
882 strlcpy(mdev->driver_name, driver_name,
883 sizeof(mdev->driver_name));
884
885 if (board_name)
886 strlcpy(mdev->model, board_name, sizeof(mdev->model));
887 else if (udev->product)
888 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
889 else
890 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
891 if (udev->serial)
892 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
893 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
894 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
895 mdev->driver_version = LINUX_VERSION_CODE;
896
897 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300898}
899EXPORT_SYMBOL_GPL(__media_device_usb_init);
Mauro Carvalho Chehabb34ecd52016-05-05 08:01:34 -0300900#endif
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300901
902
Shuah Khane576d602015-06-05 17:11:54 -0300903#endif /* CONFIG_MEDIA_CONTROLLER */