blob: 17cd349e485f86582ee77533e57d1a1dd219ea15 [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
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -030023#include <linux/compat.h>
24#include <linux/export.h>
Sakari Ailus665faa92015-12-16 11:32:17 -020025#include <linux/idr.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030026#include <linux/ioctl.h>
Laurent Pinchart140d8812010-08-18 11:41:22 -030027#include <linux/media.h>
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -030028#include <linux/slab.h>
Mauro Carvalho Chehab497e80c2015-12-11 07:23:09 -020029#include <linux/types.h>
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -030030#include <linux/pci.h>
31#include <linux/usb.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030032
33#include <media/media-device.h>
34#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030035#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030036
Shuah Khane576d602015-06-05 17:11:54 -030037#ifdef CONFIG_MEDIA_CONTROLLER
38
Laurent Pinchart140d8812010-08-18 11:41:22 -030039/* -----------------------------------------------------------------------------
40 * Userspace API
41 */
42
43static int media_device_open(struct file *filp)
44{
45 return 0;
46}
47
48static int media_device_close(struct file *filp)
49{
50 return 0;
51}
52
53static int media_device_get_info(struct media_device *dev,
54 struct media_device_info __user *__info)
55{
56 struct media_device_info info;
57
58 memset(&info, 0, sizeof(info));
59
Mauro Carvalho Chehabbb07bd62016-02-11 14:24:23 -020060 if (dev->driver_name[0])
61 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
62 else
63 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
64
Laurent Pinchart140d8812010-08-18 11:41:22 -030065 strlcpy(info.model, dev->model, sizeof(info.model));
66 strlcpy(info.serial, dev->serial, sizeof(info.serial));
67 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
68
69 info.media_version = MEDIA_API_VERSION;
70 info.hw_revision = dev->hw_revision;
71 info.driver_version = dev->driver_version;
72
Nicolas THERYb17d3942012-08-07 05:24:59 -030073 if (copy_to_user(__info, &info, sizeof(*__info)))
74 return -EFAULT;
75 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030076}
77
Laurent Pinchart16513332009-12-09 08:40:01 -030078static struct media_entity *find_entity(struct media_device *mdev, u32 id)
79{
80 struct media_entity *entity;
81 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
82
83 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
84
85 spin_lock(&mdev->lock);
86
87 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030088 if (((media_entity_id(entity) == id) && !next) ||
89 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030090 spin_unlock(&mdev->lock);
91 return entity;
92 }
93 }
94
95 spin_unlock(&mdev->lock);
96
97 return NULL;
98}
99
100static long media_device_enum_entities(struct media_device *mdev,
101 struct media_entity_desc __user *uent)
102{
103 struct media_entity *ent;
104 struct media_entity_desc u_ent;
105
Salva Peiróe6a62342014-04-30 19:48:02 +0200106 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300107 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
108 return -EFAULT;
109
110 ent = find_entity(mdev, u_ent.id);
111
112 if (ent == NULL)
113 return -EINVAL;
114
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300115 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300116 if (ent->name)
117 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300118 u_ent.type = ent->function;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200119 u_ent.revision = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300120 u_ent.flags = ent->flags;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200121 u_ent.group_id = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300122 u_ent.pads = ent->num_pads;
123 u_ent.links = ent->num_links - ent->num_backlinks;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300124 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300125 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
126 return -EFAULT;
127 return 0;
128}
129
130static void media_device_kpad_to_upad(const struct media_pad *kpad,
131 struct media_pad_desc *upad)
132{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300133 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300134 upad->index = kpad->index;
135 upad->flags = kpad->flags;
136}
137
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300138static long __media_device_enum_links(struct media_device *mdev,
139 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300140{
141 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300142
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300143 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300144 if (entity == NULL)
145 return -EINVAL;
146
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300147 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300148 unsigned int p;
149
150 for (p = 0; p < entity->num_pads; p++) {
151 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300152
153 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300154 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300155 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300156 return -EFAULT;
157 }
158 }
159
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300160 if (links->links) {
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200161 struct media_link *link;
162 struct media_link_desc __user *ulink_desc = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300163
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200164 list_for_each_entry(link, &entity->links, list) {
165 struct media_link_desc klink_desc;
Laurent Pinchart16513332009-12-09 08:40:01 -0300166
167 /* Ignore backlinks. */
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200168 if (link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300169 continue;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200170 memset(&klink_desc, 0, sizeof(klink_desc));
171 media_device_kpad_to_upad(link->source,
172 &klink_desc.source);
173 media_device_kpad_to_upad(link->sink,
174 &klink_desc.sink);
175 klink_desc.flags = link->flags;
176 if (copy_to_user(ulink_desc, &klink_desc,
177 sizeof(*ulink_desc)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300178 return -EFAULT;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200179 ulink_desc++;
Laurent Pinchart16513332009-12-09 08:40:01 -0300180 }
181 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300182
183 return 0;
184}
185
186static long media_device_enum_links(struct media_device *mdev,
187 struct media_links_enum __user *ulinks)
188{
189 struct media_links_enum links;
190 int rval;
191
192 if (copy_from_user(&links, ulinks, sizeof(links)))
193 return -EFAULT;
194
195 rval = __media_device_enum_links(mdev, &links);
196 if (rval < 0)
197 return rval;
198
Laurent Pinchart16513332009-12-09 08:40:01 -0300199 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
200 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300201
Laurent Pinchart16513332009-12-09 08:40:01 -0300202 return 0;
203}
204
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300205static long media_device_setup_link(struct media_device *mdev,
206 struct media_link_desc __user *_ulink)
207{
208 struct media_link *link = NULL;
209 struct media_link_desc ulink;
210 struct media_entity *source;
211 struct media_entity *sink;
212 int ret;
213
214 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
215 return -EFAULT;
216
217 /* Find the source and sink entities and link.
218 */
219 source = find_entity(mdev, ulink.source.entity);
220 sink = find_entity(mdev, ulink.sink.entity);
221
222 if (source == NULL || sink == NULL)
223 return -EINVAL;
224
225 if (ulink.source.index >= source->num_pads ||
226 ulink.sink.index >= sink->num_pads)
227 return -EINVAL;
228
229 link = media_entity_find_link(&source->pads[ulink.source.index],
230 &sink->pads[ulink.sink.index]);
231 if (link == NULL)
232 return -EINVAL;
233
234 /* Setup the link on both entities. */
235 ret = __media_entity_setup_link(link, ulink.flags);
236
237 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
238 return -EFAULT;
239
240 return ret;
241}
242
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300243static long __media_device_get_topology(struct media_device *mdev,
244 struct media_v2_topology *topo)
245{
246 struct media_entity *entity;
247 struct media_interface *intf;
248 struct media_pad *pad;
249 struct media_link *link;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200250 struct media_v2_entity kentity, *uentity;
251 struct media_v2_interface kintf, *uintf;
252 struct media_v2_pad kpad, *upad;
253 struct media_v2_link klink, *ulink;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300254 unsigned int i;
255 int ret = 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300256
257 topo->topology_version = mdev->topology_version;
258
259 /* Get entities and number of entities */
260 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200261 uentity = media_get_uptr(topo->ptr_entities);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300262 media_device_for_each_entity(entity, mdev) {
263 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200264 if (ret || !uentity)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300265 continue;
266
267 if (i > topo->num_entities) {
268 ret = -ENOSPC;
269 continue;
270 }
271
272 /* Copy fields to userspace struct if not error */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200273 memset(&kentity, 0, sizeof(kentity));
274 kentity.id = entity->graph_obj.id;
275 kentity.function = entity->function;
276 strncpy(kentity.name, entity->name,
277 sizeof(kentity.name));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300278
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200279 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300280 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200281 uentity++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300282 }
283 topo->num_entities = i;
284
285 /* Get interfaces and number of interfaces */
286 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200287 uintf = media_get_uptr(topo->ptr_interfaces);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300288 media_device_for_each_intf(intf, mdev) {
289 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200290 if (ret || !uintf)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300291 continue;
292
293 if (i > topo->num_interfaces) {
294 ret = -ENOSPC;
295 continue;
296 }
297
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200298 memset(&kintf, 0, sizeof(kintf));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300299
300 /* Copy intf fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200301 kintf.id = intf->graph_obj.id;
302 kintf.intf_type = intf->type;
303 kintf.flags = intf->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300304
305 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
306 struct media_intf_devnode *devnode;
307
308 devnode = intf_to_devnode(intf);
309
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200310 kintf.devnode.major = devnode->major;
311 kintf.devnode.minor = devnode->minor;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300312 }
313
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200314 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300315 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200316 uintf++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300317 }
318 topo->num_interfaces = i;
319
320 /* Get pads and number of pads */
321 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200322 upad = media_get_uptr(topo->ptr_pads);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300323 media_device_for_each_pad(pad, mdev) {
324 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200325 if (ret || !upad)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300326 continue;
327
328 if (i > topo->num_pads) {
329 ret = -ENOSPC;
330 continue;
331 }
332
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200333 memset(&kpad, 0, sizeof(kpad));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300334
335 /* Copy pad fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200336 kpad.id = pad->graph_obj.id;
337 kpad.entity_id = pad->entity->graph_obj.id;
338 kpad.flags = pad->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300339
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200340 if (copy_to_user(upad, &kpad, sizeof(kpad)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300341 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200342 upad++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300343 }
344 topo->num_pads = i;
345
346 /* Get links and number of links */
347 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200348 ulink = media_get_uptr(topo->ptr_links);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300349 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300350 if (link->is_backlink)
351 continue;
352
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300353 i++;
354
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200355 if (ret || !ulink)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300356 continue;
357
358 if (i > topo->num_links) {
359 ret = -ENOSPC;
360 continue;
361 }
362
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200363 memset(&klink, 0, sizeof(klink));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300364
365 /* Copy link fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200366 klink.id = link->graph_obj.id;
367 klink.source_id = link->gobj0->id;
368 klink.sink_id = link->gobj1->id;
369 klink.flags = link->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300370
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200371 if (copy_to_user(ulink, &klink, sizeof(klink)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300372 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200373 ulink++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300374 }
375 topo->num_links = i;
376
377 return ret;
378}
379
380static long media_device_get_topology(struct media_device *mdev,
381 struct media_v2_topology __user *utopo)
382{
383 struct media_v2_topology ktopo;
384 int ret;
385
Dan Carpentera5c82e52015-12-15 09:00:40 -0200386 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
387 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300388
389 ret = __media_device_get_topology(mdev, &ktopo);
390 if (ret < 0)
391 return ret;
392
Dan Carpentera5c82e52015-12-15 09:00:40 -0200393 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
394 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300395
Dan Carpentera5c82e52015-12-15 09:00:40 -0200396 return 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300397}
398
Laurent Pinchart140d8812010-08-18 11:41:22 -0300399static long media_device_ioctl(struct file *filp, unsigned int cmd,
400 unsigned long arg)
401{
402 struct media_devnode *devnode = media_devnode_data(filp);
403 struct media_device *dev = to_media_device(devnode);
404 long ret;
405
406 switch (cmd) {
407 case MEDIA_IOC_DEVICE_INFO:
408 ret = media_device_get_info(dev,
409 (struct media_device_info __user *)arg);
410 break;
411
Laurent Pinchart16513332009-12-09 08:40:01 -0300412 case MEDIA_IOC_ENUM_ENTITIES:
413 ret = media_device_enum_entities(dev,
414 (struct media_entity_desc __user *)arg);
415 break;
416
417 case MEDIA_IOC_ENUM_LINKS:
418 mutex_lock(&dev->graph_mutex);
419 ret = media_device_enum_links(dev,
420 (struct media_links_enum __user *)arg);
421 mutex_unlock(&dev->graph_mutex);
422 break;
423
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300424 case MEDIA_IOC_SETUP_LINK:
425 mutex_lock(&dev->graph_mutex);
426 ret = media_device_setup_link(dev,
427 (struct media_link_desc __user *)arg);
428 mutex_unlock(&dev->graph_mutex);
429 break;
430
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300431 case MEDIA_IOC_G_TOPOLOGY:
432 mutex_lock(&dev->graph_mutex);
433 ret = media_device_get_topology(dev,
434 (struct media_v2_topology __user *)arg);
435 mutex_unlock(&dev->graph_mutex);
436 break;
Mauro Carvalho Chehab333a6512016-01-25 07:29:13 -0200437
Laurent Pinchart140d8812010-08-18 11:41:22 -0300438 default:
439 ret = -ENOIOCTLCMD;
440 }
441
442 return ret;
443}
444
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300445#ifdef CONFIG_COMPAT
446
447struct media_links_enum32 {
448 __u32 entity;
449 compat_uptr_t pads; /* struct media_pad_desc * */
450 compat_uptr_t links; /* struct media_link_desc * */
451 __u32 reserved[4];
452};
453
454static long media_device_enum_links32(struct media_device *mdev,
455 struct media_links_enum32 __user *ulinks)
456{
457 struct media_links_enum links;
458 compat_uptr_t pads_ptr, links_ptr;
459
460 memset(&links, 0, sizeof(links));
461
462 if (get_user(links.entity, &ulinks->entity)
463 || get_user(pads_ptr, &ulinks->pads)
464 || get_user(links_ptr, &ulinks->links))
465 return -EFAULT;
466
467 links.pads = compat_ptr(pads_ptr);
468 links.links = compat_ptr(links_ptr);
469
470 return __media_device_enum_links(mdev, &links);
471}
472
473#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
474
475static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
476 unsigned long arg)
477{
478 struct media_devnode *devnode = media_devnode_data(filp);
479 struct media_device *dev = to_media_device(devnode);
480 long ret;
481
482 switch (cmd) {
483 case MEDIA_IOC_DEVICE_INFO:
484 case MEDIA_IOC_ENUM_ENTITIES:
485 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300486 case MEDIA_IOC_G_TOPOLOGY:
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300487 return media_device_ioctl(filp, cmd, arg);
488
489 case MEDIA_IOC_ENUM_LINKS32:
490 mutex_lock(&dev->graph_mutex);
491 ret = media_device_enum_links32(dev,
492 (struct media_links_enum32 __user *)arg);
493 mutex_unlock(&dev->graph_mutex);
494 break;
495
496 default:
497 ret = -ENOIOCTLCMD;
498 }
499
500 return ret;
501}
502#endif /* CONFIG_COMPAT */
503
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300504static const struct media_file_operations media_device_fops = {
505 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300506 .open = media_device_open,
507 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300508#ifdef CONFIG_COMPAT
509 .compat_ioctl = media_device_compat_ioctl,
510#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300511 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300512};
513
514/* -----------------------------------------------------------------------------
515 * sysfs
516 */
517
518static ssize_t show_model(struct device *cd,
519 struct device_attribute *attr, char *buf)
520{
521 struct media_device *mdev = to_media_device(to_media_devnode(cd));
522
523 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
524}
525
526static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
527
528/* -----------------------------------------------------------------------------
529 * Registration/unregistration
530 */
531
532static void media_device_release(struct media_devnode *mdev)
533{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300534 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300535}
536
537/**
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200538 * media_device_register_entity - Register an entity with a media device
539 * @mdev: The media device
540 * @entity: The entity
541 */
542int __must_check media_device_register_entity(struct media_device *mdev,
543 struct media_entity *entity)
544{
Shuah Khanafcbdb52016-02-11 21:41:21 -0200545 struct media_entity_notify *notify, *next;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200546 unsigned int i;
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200547 int ret;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200548
549 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
550 entity->function == MEDIA_ENT_F_UNKNOWN)
551 dev_warn(mdev->dev,
552 "Entity type for entity %s was not initialized!\n",
553 entity->name);
554
555 /* Warn if we apparently re-register an entity */
556 WARN_ON(entity->graph_obj.mdev != NULL);
557 entity->graph_obj.mdev = mdev;
558 INIT_LIST_HEAD(&entity->links);
559 entity->num_links = 0;
560 entity->num_backlinks = 0;
561
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200562 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
563 return -ENOMEM;
564
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200565 spin_lock(&mdev->lock);
Sakari Ailus665faa92015-12-16 11:32:17 -0200566
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200567 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
568 &entity->internal_idx);
569 if (ret < 0) {
Sakari Ailus665faa92015-12-16 11:32:17 -0200570 spin_unlock(&mdev->lock);
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200571 return ret;
Sakari Ailus665faa92015-12-16 11:32:17 -0200572 }
573
574 mdev->entity_internal_idx_max =
575 max(mdev->entity_internal_idx_max, entity->internal_idx);
576
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200577 /* Initialize media_gobj embedded at the entity */
578 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
579
580 /* Initialize objects at the pads */
581 for (i = 0; i < entity->num_pads; i++)
582 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
583 &entity->pads[i].graph_obj);
584
Shuah Khanafcbdb52016-02-11 21:41:21 -0200585 /* invoke entity_notify callbacks */
586 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
587 (notify)->notify(entity, notify->notify_data);
588 }
589
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200590 spin_unlock(&mdev->lock);
591
592 return 0;
593}
594EXPORT_SYMBOL_GPL(media_device_register_entity);
595
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200596static void __media_device_unregister_entity(struct media_entity *entity)
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200597{
598 struct media_device *mdev = entity->graph_obj.mdev;
599 struct media_link *link, *tmp;
600 struct media_interface *intf;
601 unsigned int i;
602
Sakari Ailus665faa92015-12-16 11:32:17 -0200603 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
604
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200605 /* Remove all interface links pointing to this entity */
606 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
607 list_for_each_entry_safe(link, tmp, &intf->links, list) {
608 if (link->entity == entity)
609 __media_remove_intf_link(link);
610 }
611 }
612
613 /* Remove all data links that belong to this entity */
614 __media_entity_remove_links(entity);
615
616 /* Remove all pads that belong to this entity */
617 for (i = 0; i < entity->num_pads; i++)
618 media_gobj_destroy(&entity->pads[i].graph_obj);
619
620 /* Remove the entity */
621 media_gobj_destroy(&entity->graph_obj);
622
Shuah Khanafcbdb52016-02-11 21:41:21 -0200623 /* invoke entity_notify callbacks to handle entity removal?? */
624
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200625 entity->graph_obj.mdev = NULL;
626}
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200627
628void media_device_unregister_entity(struct media_entity *entity)
629{
630 struct media_device *mdev = entity->graph_obj.mdev;
631
632 if (mdev == NULL)
633 return;
634
635 spin_lock(&mdev->lock);
636 __media_device_unregister_entity(entity);
637 spin_unlock(&mdev->lock);
638}
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200639EXPORT_SYMBOL_GPL(media_device_unregister_entity);
640
641/**
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200642 * media_device_init() - initialize a media device
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300643 * @mdev: The media device
644 *
645 * The caller is responsible for initializing the media device before
646 * registration. The following fields must be set:
647 *
648 * - dev must point to the parent device
649 * - model must be filled with the device model name
650 */
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200651void media_device_init(struct media_device *mdev)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300652{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300653 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300654 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300655 INIT_LIST_HEAD(&mdev->pads);
656 INIT_LIST_HEAD(&mdev->links);
Shuah Khanafcbdb52016-02-11 21:41:21 -0200657 INIT_LIST_HEAD(&mdev->entity_notify);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300658 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300659 mutex_init(&mdev->graph_mutex);
Sakari Ailus665faa92015-12-16 11:32:17 -0200660 ida_init(&mdev->entity_internal_idx);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300661
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200662 dev_dbg(mdev->dev, "Media device initialized\n");
663}
664EXPORT_SYMBOL_GPL(media_device_init);
665
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200666void media_device_cleanup(struct media_device *mdev)
667{
Sakari Ailus665faa92015-12-16 11:32:17 -0200668 ida_destroy(&mdev->entity_internal_idx);
669 mdev->entity_internal_idx_max = 0;
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200670 mutex_destroy(&mdev->graph_mutex);
671}
672EXPORT_SYMBOL_GPL(media_device_cleanup);
673
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200674int __must_check __media_device_register(struct media_device *mdev,
675 struct module *owner)
676{
677 int ret;
678
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300679 /* Register the device node. */
680 mdev->devnode.fops = &media_device_fops;
681 mdev->devnode.parent = mdev->dev;
682 mdev->devnode.release = media_device_release;
Javier Martinez Canillas8a950792015-12-11 20:57:09 -0200683
684 /* Set version 0 to indicate user-space that the graph is static */
685 mdev->topology_version = 0;
686
Sakari Ailus85de7212013-12-12 12:38:17 -0300687 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300688 if (ret < 0)
689 return ret;
690
691 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
692 if (ret < 0) {
693 media_devnode_unregister(&mdev->devnode);
694 return ret;
695 }
696
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300697 dev_dbg(mdev->dev, "Media device registered\n");
698
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300699 return 0;
700}
Sakari Ailus85de7212013-12-12 12:38:17 -0300701EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300702
Shuah Khanafcbdb52016-02-11 21:41:21 -0200703int __must_check media_device_register_entity_notify(struct media_device *mdev,
704 struct media_entity_notify *nptr)
705{
706 spin_lock(&mdev->lock);
707 list_add_tail(&nptr->list, &mdev->entity_notify);
708 spin_unlock(&mdev->lock);
709 return 0;
710}
711EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
712
713/*
714 * Note: Should be called with mdev->lock held.
715 */
716static void __media_device_unregister_entity_notify(struct media_device *mdev,
717 struct media_entity_notify *nptr)
718{
719 list_del(&nptr->list);
720}
721
722void media_device_unregister_entity_notify(struct media_device *mdev,
723 struct media_entity_notify *nptr)
724{
725 spin_lock(&mdev->lock);
726 __media_device_unregister_entity_notify(mdev, nptr);
727 spin_unlock(&mdev->lock);
728}
729EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
730
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300731void media_device_unregister(struct media_device *mdev)
732{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300733 struct media_entity *entity;
734 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300735 struct media_interface *intf, *tmp_intf;
Shuah Khanafcbdb52016-02-11 21:41:21 -0200736 struct media_entity_notify *notify, *nextp;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300737
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200738 if (mdev == NULL)
Javier Martinez Canillas223d19c2015-12-11 20:57:07 -0200739 return;
740
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200741 spin_lock(&mdev->lock);
742
743 /* Check if mdev was ever registered at all */
744 if (!media_devnode_is_registered(&mdev->devnode)) {
745 spin_unlock(&mdev->lock);
746 return;
747 }
748
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300749 /* Remove all entities from the media device */
750 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200751 __media_device_unregister_entity(entity);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300752
Shuah Khanafcbdb52016-02-11 21:41:21 -0200753 /* Remove all entity_notify callbacks from the media device */
754 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
755 __media_device_unregister_entity_notify(mdev, notify);
756
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300757 /* Remove all interfaces from the media device */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300758 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
759 graph_obj.list) {
760 __media_remove_intf_links(intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200761 media_gobj_destroy(&intf->graph_obj);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300762 kfree(intf);
763 }
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200764
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300765 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300766
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300767 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
768 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300769
770 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300771}
772EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300773
Shuah Khand062f912015-06-03 12:12:53 -0300774static void media_device_release_devres(struct device *dev, void *res)
775{
776}
777
Shuah Khand062f912015-06-03 12:12:53 -0300778struct media_device *media_device_get_devres(struct device *dev)
779{
780 struct media_device *mdev;
781
782 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
783 if (mdev)
784 return mdev;
785
786 mdev = devres_alloc(media_device_release_devres,
787 sizeof(struct media_device), GFP_KERNEL);
788 if (!mdev)
789 return NULL;
790 return devres_get(dev, mdev, NULL, NULL);
791}
792EXPORT_SYMBOL_GPL(media_device_get_devres);
793
Shuah Khand062f912015-06-03 12:12:53 -0300794struct media_device *media_device_find_devres(struct device *dev)
795{
796 return devres_find(dev, media_device_release_devres, NULL, NULL);
797}
798EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d602015-06-05 17:11:54 -0300799
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300800void media_device_pci_init(struct media_device *mdev,
801 struct pci_dev *pci_dev,
802 const char *name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300803{
804#ifdef CONFIG_PCI
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300805 mdev->dev = &pci_dev->dev;
806
807 if (name)
808 strlcpy(mdev->model, name, sizeof(mdev->model));
809 else
810 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
811
812 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
813
814 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
815 | pci_dev->subsystem_device;
816
817 mdev->driver_version = LINUX_VERSION_CODE;
818
819 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300820#endif
821}
822EXPORT_SYMBOL_GPL(media_device_pci_init);
823
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300824void __media_device_usb_init(struct media_device *mdev,
825 struct usb_device *udev,
826 const char *board_name,
827 const char *driver_name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300828{
829#ifdef CONFIG_USB
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300830 mdev->dev = &udev->dev;
831
832 if (driver_name)
833 strlcpy(mdev->driver_name, driver_name,
834 sizeof(mdev->driver_name));
835
836 if (board_name)
837 strlcpy(mdev->model, board_name, sizeof(mdev->model));
838 else if (udev->product)
839 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
840 else
841 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
842 if (udev->serial)
843 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
844 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
845 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
846 mdev->driver_version = LINUX_VERSION_CODE;
847
848 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300849#endif
850}
851EXPORT_SYMBOL_GPL(__media_device_usb_init);
852
853
Shuah Khane576d602015-06-05 17:11:54 -0300854#endif /* CONFIG_MEDIA_CONTROLLER */