blob: 6ba6e8f982fc4fb93af226a05e3f4b466dacedf5 [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
Sakari Ailus0629e992016-02-22 17:47:04 -030043static inline void __user *media_get_uptr(__u64 arg)
44{
45 return (void __user *)(uintptr_t)arg;
46}
47
Laurent Pinchart140d8812010-08-18 11:41:22 -030048static int media_device_open(struct file *filp)
49{
50 return 0;
51}
52
53static int media_device_close(struct file *filp)
54{
55 return 0;
56}
57
58static int media_device_get_info(struct media_device *dev,
59 struct media_device_info __user *__info)
60{
61 struct media_device_info info;
62
63 memset(&info, 0, sizeof(info));
64
Mauro Carvalho Chehabbb07bd62016-02-11 14:24:23 -020065 if (dev->driver_name[0])
66 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
67 else
68 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
69
Laurent Pinchart140d8812010-08-18 11:41:22 -030070 strlcpy(info.model, dev->model, sizeof(info.model));
71 strlcpy(info.serial, dev->serial, sizeof(info.serial));
72 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
73
74 info.media_version = MEDIA_API_VERSION;
75 info.hw_revision = dev->hw_revision;
76 info.driver_version = dev->driver_version;
77
Nicolas THERYb17d3942012-08-07 05:24:59 -030078 if (copy_to_user(__info, &info, sizeof(*__info)))
79 return -EFAULT;
80 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030081}
82
Laurent Pinchart16513332009-12-09 08:40:01 -030083static struct media_entity *find_entity(struct media_device *mdev, u32 id)
84{
85 struct media_entity *entity;
86 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
87
88 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
89
90 spin_lock(&mdev->lock);
91
92 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030093 if (((media_entity_id(entity) == id) && !next) ||
94 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030095 spin_unlock(&mdev->lock);
96 return entity;
97 }
98 }
99
100 spin_unlock(&mdev->lock);
101
102 return NULL;
103}
104
105static long media_device_enum_entities(struct media_device *mdev,
106 struct media_entity_desc __user *uent)
107{
108 struct media_entity *ent;
109 struct media_entity_desc u_ent;
110
Salva Peiróe6a62342014-04-30 19:48:02 +0200111 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300112 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
113 return -EFAULT;
114
115 ent = find_entity(mdev, u_ent.id);
116
117 if (ent == NULL)
118 return -EINVAL;
119
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300120 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300121 if (ent->name)
122 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300123 u_ent.type = ent->function;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200124 u_ent.revision = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300125 u_ent.flags = ent->flags;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200126 u_ent.group_id = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300127 u_ent.pads = ent->num_pads;
128 u_ent.links = ent->num_links - ent->num_backlinks;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300129 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300130 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
131 return -EFAULT;
132 return 0;
133}
134
135static void media_device_kpad_to_upad(const struct media_pad *kpad,
136 struct media_pad_desc *upad)
137{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300138 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300139 upad->index = kpad->index;
140 upad->flags = kpad->flags;
141}
142
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300143static long __media_device_enum_links(struct media_device *mdev,
144 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300145{
146 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300147
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300148 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300149 if (entity == NULL)
150 return -EINVAL;
151
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300152 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300153 unsigned int p;
154
155 for (p = 0; p < entity->num_pads; p++) {
156 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300157
158 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300159 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300160 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300161 return -EFAULT;
162 }
163 }
164
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300165 if (links->links) {
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200166 struct media_link *link;
167 struct media_link_desc __user *ulink_desc = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300168
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200169 list_for_each_entry(link, &entity->links, list) {
170 struct media_link_desc klink_desc;
Laurent Pinchart16513332009-12-09 08:40:01 -0300171
172 /* Ignore backlinks. */
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200173 if (link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300174 continue;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200175 memset(&klink_desc, 0, sizeof(klink_desc));
176 media_device_kpad_to_upad(link->source,
177 &klink_desc.source);
178 media_device_kpad_to_upad(link->sink,
179 &klink_desc.sink);
180 klink_desc.flags = link->flags;
181 if (copy_to_user(ulink_desc, &klink_desc,
182 sizeof(*ulink_desc)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300183 return -EFAULT;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200184 ulink_desc++;
Laurent Pinchart16513332009-12-09 08:40:01 -0300185 }
186 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300187
188 return 0;
189}
190
191static long media_device_enum_links(struct media_device *mdev,
192 struct media_links_enum __user *ulinks)
193{
194 struct media_links_enum links;
195 int rval;
196
197 if (copy_from_user(&links, ulinks, sizeof(links)))
198 return -EFAULT;
199
200 rval = __media_device_enum_links(mdev, &links);
201 if (rval < 0)
202 return rval;
203
Laurent Pinchart16513332009-12-09 08:40:01 -0300204 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
205 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300206
Laurent Pinchart16513332009-12-09 08:40:01 -0300207 return 0;
208}
209
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300210static long media_device_setup_link(struct media_device *mdev,
211 struct media_link_desc __user *_ulink)
212{
213 struct media_link *link = NULL;
214 struct media_link_desc ulink;
215 struct media_entity *source;
216 struct media_entity *sink;
217 int ret;
218
219 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
220 return -EFAULT;
221
222 /* Find the source and sink entities and link.
223 */
224 source = find_entity(mdev, ulink.source.entity);
225 sink = find_entity(mdev, ulink.sink.entity);
226
227 if (source == NULL || sink == NULL)
228 return -EINVAL;
229
230 if (ulink.source.index >= source->num_pads ||
231 ulink.sink.index >= sink->num_pads)
232 return -EINVAL;
233
234 link = media_entity_find_link(&source->pads[ulink.source.index],
235 &sink->pads[ulink.sink.index]);
236 if (link == NULL)
237 return -EINVAL;
238
239 /* Setup the link on both entities. */
240 ret = __media_entity_setup_link(link, ulink.flags);
241
242 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
243 return -EFAULT;
244
245 return ret;
246}
247
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300248static long __media_device_get_topology(struct media_device *mdev,
249 struct media_v2_topology *topo)
250{
251 struct media_entity *entity;
252 struct media_interface *intf;
253 struct media_pad *pad;
254 struct media_link *link;
Sakari Ailusd37410c2016-02-22 17:47:03 -0300255 struct media_v2_entity kentity, __user *uentity;
256 struct media_v2_interface kintf, __user *uintf;
257 struct media_v2_pad kpad, __user *upad;
258 struct media_v2_link klink, __user *ulink;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300259 unsigned int i;
260 int ret = 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300261
262 topo->topology_version = mdev->topology_version;
263
264 /* Get entities and number of entities */
265 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200266 uentity = media_get_uptr(topo->ptr_entities);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300267 media_device_for_each_entity(entity, mdev) {
268 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200269 if (ret || !uentity)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300270 continue;
271
272 if (i > topo->num_entities) {
273 ret = -ENOSPC;
274 continue;
275 }
276
277 /* Copy fields to userspace struct if not error */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200278 memset(&kentity, 0, sizeof(kentity));
279 kentity.id = entity->graph_obj.id;
280 kentity.function = entity->function;
281 strncpy(kentity.name, entity->name,
282 sizeof(kentity.name));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300283
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200284 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300285 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200286 uentity++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300287 }
288 topo->num_entities = i;
289
290 /* Get interfaces and number of interfaces */
291 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200292 uintf = media_get_uptr(topo->ptr_interfaces);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300293 media_device_for_each_intf(intf, mdev) {
294 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200295 if (ret || !uintf)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300296 continue;
297
298 if (i > topo->num_interfaces) {
299 ret = -ENOSPC;
300 continue;
301 }
302
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200303 memset(&kintf, 0, sizeof(kintf));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300304
305 /* Copy intf fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200306 kintf.id = intf->graph_obj.id;
307 kintf.intf_type = intf->type;
308 kintf.flags = intf->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300309
310 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
311 struct media_intf_devnode *devnode;
312
313 devnode = intf_to_devnode(intf);
314
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200315 kintf.devnode.major = devnode->major;
316 kintf.devnode.minor = devnode->minor;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300317 }
318
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200319 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300320 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200321 uintf++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300322 }
323 topo->num_interfaces = i;
324
325 /* Get pads and number of pads */
326 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200327 upad = media_get_uptr(topo->ptr_pads);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300328 media_device_for_each_pad(pad, mdev) {
329 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200330 if (ret || !upad)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300331 continue;
332
333 if (i > topo->num_pads) {
334 ret = -ENOSPC;
335 continue;
336 }
337
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200338 memset(&kpad, 0, sizeof(kpad));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300339
340 /* Copy pad fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200341 kpad.id = pad->graph_obj.id;
342 kpad.entity_id = pad->entity->graph_obj.id;
343 kpad.flags = pad->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300344
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200345 if (copy_to_user(upad, &kpad, sizeof(kpad)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300346 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200347 upad++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300348 }
349 topo->num_pads = i;
350
351 /* Get links and number of links */
352 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200353 ulink = media_get_uptr(topo->ptr_links);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300354 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300355 if (link->is_backlink)
356 continue;
357
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300358 i++;
359
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200360 if (ret || !ulink)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300361 continue;
362
363 if (i > topo->num_links) {
364 ret = -ENOSPC;
365 continue;
366 }
367
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200368 memset(&klink, 0, sizeof(klink));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300369
370 /* Copy link fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200371 klink.id = link->graph_obj.id;
372 klink.source_id = link->gobj0->id;
373 klink.sink_id = link->gobj1->id;
374 klink.flags = link->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300375
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200376 if (copy_to_user(ulink, &klink, sizeof(klink)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300377 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200378 ulink++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300379 }
380 topo->num_links = i;
381
382 return ret;
383}
384
385static long media_device_get_topology(struct media_device *mdev,
386 struct media_v2_topology __user *utopo)
387{
388 struct media_v2_topology ktopo;
389 int ret;
390
Dan Carpentera5c82e52015-12-15 09:00:40 -0200391 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
392 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300393
394 ret = __media_device_get_topology(mdev, &ktopo);
395 if (ret < 0)
396 return ret;
397
Dan Carpentera5c82e52015-12-15 09:00:40 -0200398 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
399 return -EFAULT;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300400
Dan Carpentera5c82e52015-12-15 09:00:40 -0200401 return 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300402}
403
Laurent Pinchart140d8812010-08-18 11:41:22 -0300404static long media_device_ioctl(struct file *filp, unsigned int cmd,
405 unsigned long arg)
406{
407 struct media_devnode *devnode = media_devnode_data(filp);
408 struct media_device *dev = to_media_device(devnode);
409 long ret;
410
411 switch (cmd) {
412 case MEDIA_IOC_DEVICE_INFO:
413 ret = media_device_get_info(dev,
414 (struct media_device_info __user *)arg);
415 break;
416
Laurent Pinchart16513332009-12-09 08:40:01 -0300417 case MEDIA_IOC_ENUM_ENTITIES:
418 ret = media_device_enum_entities(dev,
419 (struct media_entity_desc __user *)arg);
420 break;
421
422 case MEDIA_IOC_ENUM_LINKS:
423 mutex_lock(&dev->graph_mutex);
424 ret = media_device_enum_links(dev,
425 (struct media_links_enum __user *)arg);
426 mutex_unlock(&dev->graph_mutex);
427 break;
428
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300429 case MEDIA_IOC_SETUP_LINK:
430 mutex_lock(&dev->graph_mutex);
431 ret = media_device_setup_link(dev,
432 (struct media_link_desc __user *)arg);
433 mutex_unlock(&dev->graph_mutex);
434 break;
435
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300436 case MEDIA_IOC_G_TOPOLOGY:
437 mutex_lock(&dev->graph_mutex);
438 ret = media_device_get_topology(dev,
439 (struct media_v2_topology __user *)arg);
440 mutex_unlock(&dev->graph_mutex);
441 break;
Mauro Carvalho Chehab333a6512016-01-25 07:29:13 -0200442
Laurent Pinchart140d8812010-08-18 11:41:22 -0300443 default:
444 ret = -ENOIOCTLCMD;
445 }
446
447 return ret;
448}
449
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300450#ifdef CONFIG_COMPAT
451
452struct media_links_enum32 {
453 __u32 entity;
454 compat_uptr_t pads; /* struct media_pad_desc * */
455 compat_uptr_t links; /* struct media_link_desc * */
456 __u32 reserved[4];
457};
458
459static long media_device_enum_links32(struct media_device *mdev,
460 struct media_links_enum32 __user *ulinks)
461{
462 struct media_links_enum links;
463 compat_uptr_t pads_ptr, links_ptr;
464
465 memset(&links, 0, sizeof(links));
466
467 if (get_user(links.entity, &ulinks->entity)
468 || get_user(pads_ptr, &ulinks->pads)
469 || get_user(links_ptr, &ulinks->links))
470 return -EFAULT;
471
472 links.pads = compat_ptr(pads_ptr);
473 links.links = compat_ptr(links_ptr);
474
475 return __media_device_enum_links(mdev, &links);
476}
477
478#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
479
480static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
481 unsigned long arg)
482{
483 struct media_devnode *devnode = media_devnode_data(filp);
484 struct media_device *dev = to_media_device(devnode);
485 long ret;
486
487 switch (cmd) {
488 case MEDIA_IOC_DEVICE_INFO:
489 case MEDIA_IOC_ENUM_ENTITIES:
490 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300491 case MEDIA_IOC_G_TOPOLOGY:
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300492 return media_device_ioctl(filp, cmd, arg);
493
494 case MEDIA_IOC_ENUM_LINKS32:
495 mutex_lock(&dev->graph_mutex);
496 ret = media_device_enum_links32(dev,
497 (struct media_links_enum32 __user *)arg);
498 mutex_unlock(&dev->graph_mutex);
499 break;
500
501 default:
502 ret = -ENOIOCTLCMD;
503 }
504
505 return ret;
506}
507#endif /* CONFIG_COMPAT */
508
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300509static const struct media_file_operations media_device_fops = {
510 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300511 .open = media_device_open,
512 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300513#ifdef CONFIG_COMPAT
514 .compat_ioctl = media_device_compat_ioctl,
515#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300516 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300517};
518
519/* -----------------------------------------------------------------------------
520 * sysfs
521 */
522
523static ssize_t show_model(struct device *cd,
524 struct device_attribute *attr, char *buf)
525{
526 struct media_device *mdev = to_media_device(to_media_devnode(cd));
527
528 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
529}
530
531static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
532
533/* -----------------------------------------------------------------------------
534 * Registration/unregistration
535 */
536
537static void media_device_release(struct media_devnode *mdev)
538{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300539 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300540}
541
542/**
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200543 * media_device_register_entity - Register an entity with a media device
544 * @mdev: The media device
545 * @entity: The entity
546 */
547int __must_check media_device_register_entity(struct media_device *mdev,
548 struct media_entity *entity)
549{
Shuah Khanafcbdb52016-02-11 21:41:21 -0200550 struct media_entity_notify *notify, *next;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200551 unsigned int i;
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200552 int ret;
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200553
554 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
555 entity->function == MEDIA_ENT_F_UNKNOWN)
556 dev_warn(mdev->dev,
557 "Entity type for entity %s was not initialized!\n",
558 entity->name);
559
560 /* Warn if we apparently re-register an entity */
561 WARN_ON(entity->graph_obj.mdev != NULL);
562 entity->graph_obj.mdev = mdev;
563 INIT_LIST_HEAD(&entity->links);
564 entity->num_links = 0;
565 entity->num_backlinks = 0;
566
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200567 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
568 return -ENOMEM;
569
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200570 spin_lock(&mdev->lock);
Sakari Ailus665faa92015-12-16 11:32:17 -0200571
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200572 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
573 &entity->internal_idx);
574 if (ret < 0) {
Sakari Ailus665faa92015-12-16 11:32:17 -0200575 spin_unlock(&mdev->lock);
Mauro Carvalho Chehabba0dd722015-12-16 15:33:54 -0200576 return ret;
Sakari Ailus665faa92015-12-16 11:32:17 -0200577 }
578
579 mdev->entity_internal_idx_max =
580 max(mdev->entity_internal_idx_max, entity->internal_idx);
581
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200582 /* Initialize media_gobj embedded at the entity */
583 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
584
585 /* Initialize objects at the pads */
586 for (i = 0; i < entity->num_pads; i++)
587 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
588 &entity->pads[i].graph_obj);
589
Shuah Khanafcbdb52016-02-11 21:41:21 -0200590 /* invoke entity_notify callbacks */
591 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
592 (notify)->notify(entity, notify->notify_data);
593 }
594
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200595 spin_unlock(&mdev->lock);
596
Sakari Ailus0c426c42016-02-21 13:25:08 -0300597 mutex_lock(&mdev->graph_mutex);
598 if (mdev->entity_internal_idx_max
599 >= mdev->pm_count_walk.ent_enum.idx_max) {
600 struct media_entity_graph new = { .top = 0 };
601
602 /*
603 * Initialise the new graph walk before cleaning up
604 * the old one in order not to spoil the graph walk
605 * object of the media device if graph walk init fails.
606 */
607 ret = media_entity_graph_walk_init(&new, mdev);
608 if (ret) {
609 mutex_unlock(&mdev->graph_mutex);
610 return ret;
611 }
612 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
613 mdev->pm_count_walk = new;
614 }
615 mutex_unlock(&mdev->graph_mutex);
616
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200617 return 0;
618}
619EXPORT_SYMBOL_GPL(media_device_register_entity);
620
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200621static void __media_device_unregister_entity(struct media_entity *entity)
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200622{
623 struct media_device *mdev = entity->graph_obj.mdev;
624 struct media_link *link, *tmp;
625 struct media_interface *intf;
626 unsigned int i;
627
Sakari Ailus665faa92015-12-16 11:32:17 -0200628 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
629
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200630 /* Remove all interface links pointing to this entity */
631 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
632 list_for_each_entry_safe(link, tmp, &intf->links, list) {
633 if (link->entity == entity)
634 __media_remove_intf_link(link);
635 }
636 }
637
638 /* Remove all data links that belong to this entity */
639 __media_entity_remove_links(entity);
640
641 /* Remove all pads that belong to this entity */
642 for (i = 0; i < entity->num_pads; i++)
643 media_gobj_destroy(&entity->pads[i].graph_obj);
644
645 /* Remove the entity */
646 media_gobj_destroy(&entity->graph_obj);
647
Shuah Khanafcbdb52016-02-11 21:41:21 -0200648 /* invoke entity_notify callbacks to handle entity removal?? */
649
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200650 entity->graph_obj.mdev = NULL;
651}
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200652
653void media_device_unregister_entity(struct media_entity *entity)
654{
655 struct media_device *mdev = entity->graph_obj.mdev;
656
657 if (mdev == NULL)
658 return;
659
660 spin_lock(&mdev->lock);
661 __media_device_unregister_entity(entity);
662 spin_unlock(&mdev->lock);
663}
Mauro Carvalho Chehabb2ed8af2015-12-15 08:26:52 -0200664EXPORT_SYMBOL_GPL(media_device_unregister_entity);
665
666/**
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200667 * media_device_init() - initialize a media device
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300668 * @mdev: The media device
669 *
670 * The caller is responsible for initializing the media device before
671 * registration. The following fields must be set:
672 *
673 * - dev must point to the parent device
674 * - model must be filled with the device model name
675 */
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200676void media_device_init(struct media_device *mdev)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300677{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300678 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300679 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300680 INIT_LIST_HEAD(&mdev->pads);
681 INIT_LIST_HEAD(&mdev->links);
Shuah Khanafcbdb52016-02-11 21:41:21 -0200682 INIT_LIST_HEAD(&mdev->entity_notify);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300683 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300684 mutex_init(&mdev->graph_mutex);
Sakari Ailus665faa92015-12-16 11:32:17 -0200685 ida_init(&mdev->entity_internal_idx);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300686
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200687 dev_dbg(mdev->dev, "Media device initialized\n");
688}
689EXPORT_SYMBOL_GPL(media_device_init);
690
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200691void media_device_cleanup(struct media_device *mdev)
692{
Sakari Ailus665faa92015-12-16 11:32:17 -0200693 ida_destroy(&mdev->entity_internal_idx);
694 mdev->entity_internal_idx_max = 0;
Sakari Ailus0c426c42016-02-21 13:25:08 -0300695 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200696 mutex_destroy(&mdev->graph_mutex);
697}
698EXPORT_SYMBOL_GPL(media_device_cleanup);
699
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200700int __must_check __media_device_register(struct media_device *mdev,
701 struct module *owner)
702{
703 int ret;
704
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300705 /* Register the device node. */
706 mdev->devnode.fops = &media_device_fops;
707 mdev->devnode.parent = mdev->dev;
708 mdev->devnode.release = media_device_release;
Javier Martinez Canillas8a950792015-12-11 20:57:09 -0200709
710 /* Set version 0 to indicate user-space that the graph is static */
711 mdev->topology_version = 0;
712
Sakari Ailus85de7212013-12-12 12:38:17 -0300713 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300714 if (ret < 0)
715 return ret;
716
717 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
718 if (ret < 0) {
719 media_devnode_unregister(&mdev->devnode);
720 return ret;
721 }
722
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300723 dev_dbg(mdev->dev, "Media device registered\n");
724
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300725 return 0;
726}
Sakari Ailus85de7212013-12-12 12:38:17 -0300727EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300728
Shuah Khanafcbdb52016-02-11 21:41:21 -0200729int __must_check media_device_register_entity_notify(struct media_device *mdev,
730 struct media_entity_notify *nptr)
731{
732 spin_lock(&mdev->lock);
733 list_add_tail(&nptr->list, &mdev->entity_notify);
734 spin_unlock(&mdev->lock);
735 return 0;
736}
737EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
738
739/*
740 * Note: Should be called with mdev->lock held.
741 */
742static void __media_device_unregister_entity_notify(struct media_device *mdev,
743 struct media_entity_notify *nptr)
744{
745 list_del(&nptr->list);
746}
747
748void media_device_unregister_entity_notify(struct media_device *mdev,
749 struct media_entity_notify *nptr)
750{
751 spin_lock(&mdev->lock);
752 __media_device_unregister_entity_notify(mdev, nptr);
753 spin_unlock(&mdev->lock);
754}
755EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
756
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300757void media_device_unregister(struct media_device *mdev)
758{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300759 struct media_entity *entity;
760 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300761 struct media_interface *intf, *tmp_intf;
Shuah Khanafcbdb52016-02-11 21:41:21 -0200762 struct media_entity_notify *notify, *nextp;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300763
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200764 if (mdev == NULL)
Javier Martinez Canillas223d19c2015-12-11 20:57:07 -0200765 return;
766
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200767 spin_lock(&mdev->lock);
768
769 /* Check if mdev was ever registered at all */
770 if (!media_devnode_is_registered(&mdev->devnode)) {
771 spin_unlock(&mdev->lock);
772 return;
773 }
774
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300775 /* Remove all entities from the media device */
776 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200777 __media_device_unregister_entity(entity);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300778
Shuah Khanafcbdb52016-02-11 21:41:21 -0200779 /* Remove all entity_notify callbacks from the media device */
780 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
781 __media_device_unregister_entity_notify(mdev, notify);
782
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300783 /* Remove all interfaces from the media device */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300784 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
785 graph_obj.list) {
786 __media_remove_intf_links(intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200787 media_gobj_destroy(&intf->graph_obj);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300788 kfree(intf);
789 }
Mauro Carvalho Chehab821ed362015-12-15 08:36:51 -0200790
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300791 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300792
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300793 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
794 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300795
796 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300797}
798EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300799
Shuah Khand062f912015-06-03 12:12:53 -0300800static void media_device_release_devres(struct device *dev, void *res)
801{
802}
803
Shuah Khand062f912015-06-03 12:12:53 -0300804struct media_device *media_device_get_devres(struct device *dev)
805{
806 struct media_device *mdev;
807
808 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
809 if (mdev)
810 return mdev;
811
812 mdev = devres_alloc(media_device_release_devres,
813 sizeof(struct media_device), GFP_KERNEL);
814 if (!mdev)
815 return NULL;
816 return devres_get(dev, mdev, NULL, NULL);
817}
818EXPORT_SYMBOL_GPL(media_device_get_devres);
819
Shuah Khand062f912015-06-03 12:12:53 -0300820struct media_device *media_device_find_devres(struct device *dev)
821{
822 return devres_find(dev, media_device_release_devres, NULL, NULL);
823}
824EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d602015-06-05 17:11:54 -0300825
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300826void media_device_pci_init(struct media_device *mdev,
827 struct pci_dev *pci_dev,
828 const char *name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300829{
830#ifdef CONFIG_PCI
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300831 mdev->dev = &pci_dev->dev;
832
833 if (name)
834 strlcpy(mdev->model, name, sizeof(mdev->model));
835 else
836 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
837
838 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
839
840 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
841 | pci_dev->subsystem_device;
842
843 mdev->driver_version = LINUX_VERSION_CODE;
844
845 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300846#endif
847}
848EXPORT_SYMBOL_GPL(media_device_pci_init);
849
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300850void __media_device_usb_init(struct media_device *mdev,
851 struct usb_device *udev,
852 const char *board_name,
853 const char *driver_name)
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300854{
855#ifdef CONFIG_USB
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300856 mdev->dev = &udev->dev;
857
858 if (driver_name)
859 strlcpy(mdev->driver_name, driver_name,
860 sizeof(mdev->driver_name));
861
862 if (board_name)
863 strlcpy(mdev->model, board_name, sizeof(mdev->model));
864 else if (udev->product)
865 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
866 else
867 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
868 if (udev->serial)
869 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
870 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
871 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
872 mdev->driver_version = LINUX_VERSION_CODE;
873
874 media_device_init(mdev);
Mauro Carvalho Chehab41b44e32016-02-22 11:42:04 -0300875#endif
876}
877EXPORT_SYMBOL_GPL(__media_device_usb_init);
878
879
Shuah Khane576d602015-06-05 17:11:54 -0300880#endif /* CONFIG_MEDIA_CONTROLLER */