blob: 6406914a9bf59c25348c8bcfc002819d0d27deb2 [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>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030025#include <linux/ioctl.h>
Laurent Pinchart140d8812010-08-18 11:41:22 -030026#include <linux/media.h>
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -030027#include <linux/slab.h>
Mauro Carvalho Chehab497e80c2015-12-11 07:23:09 -020028#include <linux/types.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030029
30#include <media/media-device.h>
31#include <media/media-devnode.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030032#include <media/media-entity.h>
Laurent Pinchart176fb0d2009-12-09 08:39:58 -030033
Shuah Khane576d60b2015-06-05 17:11:54 -030034#ifdef CONFIG_MEDIA_CONTROLLER
35
Laurent Pinchart140d8812010-08-18 11:41:22 -030036/* -----------------------------------------------------------------------------
37 * Userspace API
38 */
39
40static int media_device_open(struct file *filp)
41{
42 return 0;
43}
44
45static int media_device_close(struct file *filp)
46{
47 return 0;
48}
49
50static int media_device_get_info(struct media_device *dev,
51 struct media_device_info __user *__info)
52{
53 struct media_device_info info;
54
55 memset(&info, 0, sizeof(info));
56
57 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
58 strlcpy(info.model, dev->model, sizeof(info.model));
59 strlcpy(info.serial, dev->serial, sizeof(info.serial));
60 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
61
62 info.media_version = MEDIA_API_VERSION;
63 info.hw_revision = dev->hw_revision;
64 info.driver_version = dev->driver_version;
65
Nicolas THERYb17d3942012-08-07 05:24:59 -030066 if (copy_to_user(__info, &info, sizeof(*__info)))
67 return -EFAULT;
68 return 0;
Laurent Pinchart140d8812010-08-18 11:41:22 -030069}
70
Laurent Pinchart16513332009-12-09 08:40:01 -030071static struct media_entity *find_entity(struct media_device *mdev, u32 id)
72{
73 struct media_entity *entity;
74 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
75
76 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
77
78 spin_lock(&mdev->lock);
79
80 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -030081 if (((media_entity_id(entity) == id) && !next) ||
82 ((media_entity_id(entity) > id) && next)) {
Laurent Pinchart16513332009-12-09 08:40:01 -030083 spin_unlock(&mdev->lock);
84 return entity;
85 }
86 }
87
88 spin_unlock(&mdev->lock);
89
90 return NULL;
91}
92
93static long media_device_enum_entities(struct media_device *mdev,
94 struct media_entity_desc __user *uent)
95{
96 struct media_entity *ent;
97 struct media_entity_desc u_ent;
98
Salva Peiróe6a62342014-04-30 19:48:02 +020099 memset(&u_ent, 0, sizeof(u_ent));
Laurent Pinchart16513332009-12-09 08:40:01 -0300100 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
101 return -EFAULT;
102
103 ent = find_entity(mdev, u_ent.id);
104
105 if (ent == NULL)
106 return -EINVAL;
107
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300108 u_ent.id = media_entity_id(ent);
Laurent Pinchartde8eae32014-07-17 08:52:08 -0300109 if (ent->name)
110 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300111 u_ent.type = ent->function;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200112 u_ent.revision = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300113 u_ent.flags = ent->flags;
Mauro Carvalho Chehab8ed8c882015-12-11 08:02:19 -0200114 u_ent.group_id = 0; /* Unused */
Laurent Pinchart16513332009-12-09 08:40:01 -0300115 u_ent.pads = ent->num_pads;
116 u_ent.links = ent->num_links - ent->num_backlinks;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300117 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
Laurent Pinchart16513332009-12-09 08:40:01 -0300118 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
119 return -EFAULT;
120 return 0;
121}
122
123static void media_device_kpad_to_upad(const struct media_pad *kpad,
124 struct media_pad_desc *upad)
125{
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300126 upad->entity = media_entity_id(kpad->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300127 upad->index = kpad->index;
128 upad->flags = kpad->flags;
129}
130
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300131static long __media_device_enum_links(struct media_device *mdev,
132 struct media_links_enum *links)
Laurent Pinchart16513332009-12-09 08:40:01 -0300133{
134 struct media_entity *entity;
Laurent Pinchart16513332009-12-09 08:40:01 -0300135
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300136 entity = find_entity(mdev, links->entity);
Laurent Pinchart16513332009-12-09 08:40:01 -0300137 if (entity == NULL)
138 return -EINVAL;
139
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300140 if (links->pads) {
Laurent Pinchart16513332009-12-09 08:40:01 -0300141 unsigned int p;
142
143 for (p = 0; p < entity->num_pads; p++) {
144 struct media_pad_desc pad;
Dan Carpenterc88e7392013-04-13 06:32:15 -0300145
146 memset(&pad, 0, sizeof(pad));
Laurent Pinchart16513332009-12-09 08:40:01 -0300147 media_device_kpad_to_upad(&entity->pads[p], &pad);
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300148 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300149 return -EFAULT;
150 }
151 }
152
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300153 if (links->links) {
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200154 struct media_link *link;
155 struct media_link_desc __user *ulink_desc = links->links;
Laurent Pinchart16513332009-12-09 08:40:01 -0300156
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200157 list_for_each_entry(link, &entity->links, list) {
158 struct media_link_desc klink_desc;
Laurent Pinchart16513332009-12-09 08:40:01 -0300159
160 /* Ignore backlinks. */
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200161 if (link->source->entity != entity)
Laurent Pinchart16513332009-12-09 08:40:01 -0300162 continue;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200163 memset(&klink_desc, 0, sizeof(klink_desc));
164 media_device_kpad_to_upad(link->source,
165 &klink_desc.source);
166 media_device_kpad_to_upad(link->sink,
167 &klink_desc.sink);
168 klink_desc.flags = link->flags;
169 if (copy_to_user(ulink_desc, &klink_desc,
170 sizeof(*ulink_desc)))
Laurent Pinchart16513332009-12-09 08:40:01 -0300171 return -EFAULT;
Mauro Carvalho Chehabb83e2502015-12-11 07:25:01 -0200172 ulink_desc++;
Laurent Pinchart16513332009-12-09 08:40:01 -0300173 }
174 }
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300175
176 return 0;
177}
178
179static long media_device_enum_links(struct media_device *mdev,
180 struct media_links_enum __user *ulinks)
181{
182 struct media_links_enum links;
183 int rval;
184
185 if (copy_from_user(&links, ulinks, sizeof(links)))
186 return -EFAULT;
187
188 rval = __media_device_enum_links(mdev, &links);
189 if (rval < 0)
190 return rval;
191
Laurent Pinchart16513332009-12-09 08:40:01 -0300192 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
193 return -EFAULT;
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300194
Laurent Pinchart16513332009-12-09 08:40:01 -0300195 return 0;
196}
197
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300198static long media_device_setup_link(struct media_device *mdev,
199 struct media_link_desc __user *_ulink)
200{
201 struct media_link *link = NULL;
202 struct media_link_desc ulink;
203 struct media_entity *source;
204 struct media_entity *sink;
205 int ret;
206
207 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
208 return -EFAULT;
209
210 /* Find the source and sink entities and link.
211 */
212 source = find_entity(mdev, ulink.source.entity);
213 sink = find_entity(mdev, ulink.sink.entity);
214
215 if (source == NULL || sink == NULL)
216 return -EINVAL;
217
218 if (ulink.source.index >= source->num_pads ||
219 ulink.sink.index >= sink->num_pads)
220 return -EINVAL;
221
222 link = media_entity_find_link(&source->pads[ulink.source.index],
223 &sink->pads[ulink.sink.index]);
224 if (link == NULL)
225 return -EINVAL;
226
227 /* Setup the link on both entities. */
228 ret = __media_entity_setup_link(link, ulink.flags);
229
230 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
231 return -EFAULT;
232
233 return ret;
234}
235
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300236static long __media_device_get_topology(struct media_device *mdev,
237 struct media_v2_topology *topo)
238{
239 struct media_entity *entity;
240 struct media_interface *intf;
241 struct media_pad *pad;
242 struct media_link *link;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200243 struct media_v2_entity kentity, *uentity;
244 struct media_v2_interface kintf, *uintf;
245 struct media_v2_pad kpad, *upad;
246 struct media_v2_link klink, *ulink;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300247 unsigned int i;
248 int ret = 0;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300249
250 topo->topology_version = mdev->topology_version;
251
252 /* Get entities and number of entities */
253 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200254 uentity = media_get_uptr(topo->ptr_entities);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300255 media_device_for_each_entity(entity, mdev) {
256 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200257 if (ret || !uentity)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300258 continue;
259
260 if (i > topo->num_entities) {
261 ret = -ENOSPC;
262 continue;
263 }
264
265 /* Copy fields to userspace struct if not error */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200266 memset(&kentity, 0, sizeof(kentity));
267 kentity.id = entity->graph_obj.id;
268 kentity.function = entity->function;
269 strncpy(kentity.name, entity->name,
270 sizeof(kentity.name));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300271
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200272 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300273 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200274 uentity++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300275 }
276 topo->num_entities = i;
277
278 /* Get interfaces and number of interfaces */
279 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200280 uintf = media_get_uptr(topo->ptr_interfaces);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300281 media_device_for_each_intf(intf, mdev) {
282 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200283 if (ret || !uintf)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300284 continue;
285
286 if (i > topo->num_interfaces) {
287 ret = -ENOSPC;
288 continue;
289 }
290
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200291 memset(&kintf, 0, sizeof(kintf));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300292
293 /* Copy intf fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200294 kintf.id = intf->graph_obj.id;
295 kintf.intf_type = intf->type;
296 kintf.flags = intf->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300297
298 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
299 struct media_intf_devnode *devnode;
300
301 devnode = intf_to_devnode(intf);
302
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200303 kintf.devnode.major = devnode->major;
304 kintf.devnode.minor = devnode->minor;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300305 }
306
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200307 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300308 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200309 uintf++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300310 }
311 topo->num_interfaces = i;
312
313 /* Get pads and number of pads */
314 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200315 upad = media_get_uptr(topo->ptr_pads);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300316 media_device_for_each_pad(pad, mdev) {
317 i++;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200318 if (ret || !upad)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300319 continue;
320
321 if (i > topo->num_pads) {
322 ret = -ENOSPC;
323 continue;
324 }
325
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200326 memset(&kpad, 0, sizeof(kpad));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300327
328 /* Copy pad fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200329 kpad.id = pad->graph_obj.id;
330 kpad.entity_id = pad->entity->graph_obj.id;
331 kpad.flags = pad->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300332
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200333 if (copy_to_user(upad, &kpad, sizeof(kpad)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300334 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200335 upad++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300336 }
337 topo->num_pads = i;
338
339 /* Get links and number of links */
340 i = 0;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200341 ulink = media_get_uptr(topo->ptr_links);
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300342 media_device_for_each_link(link, mdev) {
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300343 if (link->is_backlink)
344 continue;
345
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300346 i++;
347
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200348 if (ret || !ulink)
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300349 continue;
350
351 if (i > topo->num_links) {
352 ret = -ENOSPC;
353 continue;
354 }
355
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200356 memset(&klink, 0, sizeof(klink));
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300357
358 /* Copy link fields to userspace struct */
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200359 klink.id = link->graph_obj.id;
360 klink.source_id = link->gobj0->id;
361 klink.sink_id = link->gobj1->id;
362 klink.flags = link->flags;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300363
364 if (media_type(link->gobj0) != MEDIA_GRAPH_PAD)
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200365 klink.flags |= MEDIA_LNK_FL_INTERFACE_LINK;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300366
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200367 if (copy_to_user(ulink, &klink, sizeof(klink)))
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300368 ret = -EFAULT;
Mauro Carvalho Chehabb3b7a9f2015-12-11 16:07:57 -0200369 ulink++;
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300370 }
371 topo->num_links = i;
372
373 return ret;
374}
375
376static long media_device_get_topology(struct media_device *mdev,
377 struct media_v2_topology __user *utopo)
378{
379 struct media_v2_topology ktopo;
380 int ret;
381
382 ret = copy_from_user(&ktopo, utopo, sizeof(ktopo));
383
384 if (ret < 0)
385 return ret;
386
387 ret = __media_device_get_topology(mdev, &ktopo);
388 if (ret < 0)
389 return ret;
390
391 ret = copy_to_user(utopo, &ktopo, sizeof(*utopo));
392
393 return ret;
394}
395
Laurent Pinchart140d8812010-08-18 11:41:22 -0300396static long media_device_ioctl(struct file *filp, unsigned int cmd,
397 unsigned long arg)
398{
399 struct media_devnode *devnode = media_devnode_data(filp);
400 struct media_device *dev = to_media_device(devnode);
401 long ret;
402
403 switch (cmd) {
404 case MEDIA_IOC_DEVICE_INFO:
405 ret = media_device_get_info(dev,
406 (struct media_device_info __user *)arg);
407 break;
408
Laurent Pinchart16513332009-12-09 08:40:01 -0300409 case MEDIA_IOC_ENUM_ENTITIES:
410 ret = media_device_enum_entities(dev,
411 (struct media_entity_desc __user *)arg);
412 break;
413
414 case MEDIA_IOC_ENUM_LINKS:
415 mutex_lock(&dev->graph_mutex);
416 ret = media_device_enum_links(dev,
417 (struct media_links_enum __user *)arg);
418 mutex_unlock(&dev->graph_mutex);
419 break;
420
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300421 case MEDIA_IOC_SETUP_LINK:
422 mutex_lock(&dev->graph_mutex);
423 ret = media_device_setup_link(dev,
424 (struct media_link_desc __user *)arg);
425 mutex_unlock(&dev->graph_mutex);
426 break;
427
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300428 case MEDIA_IOC_G_TOPOLOGY:
429 mutex_lock(&dev->graph_mutex);
430 ret = media_device_get_topology(dev,
431 (struct media_v2_topology __user *)arg);
432 mutex_unlock(&dev->graph_mutex);
433 break;
434
Laurent Pinchart140d8812010-08-18 11:41:22 -0300435 default:
436 ret = -ENOIOCTLCMD;
437 }
438
439 return ret;
440}
441
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300442#ifdef CONFIG_COMPAT
443
444struct media_links_enum32 {
445 __u32 entity;
446 compat_uptr_t pads; /* struct media_pad_desc * */
447 compat_uptr_t links; /* struct media_link_desc * */
448 __u32 reserved[4];
449};
450
451static long media_device_enum_links32(struct media_device *mdev,
452 struct media_links_enum32 __user *ulinks)
453{
454 struct media_links_enum links;
455 compat_uptr_t pads_ptr, links_ptr;
456
457 memset(&links, 0, sizeof(links));
458
459 if (get_user(links.entity, &ulinks->entity)
460 || get_user(pads_ptr, &ulinks->pads)
461 || get_user(links_ptr, &ulinks->links))
462 return -EFAULT;
463
464 links.pads = compat_ptr(pads_ptr);
465 links.links = compat_ptr(links_ptr);
466
467 return __media_device_enum_links(mdev, &links);
468}
469
470#define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
471
472static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
473 unsigned long arg)
474{
475 struct media_devnode *devnode = media_devnode_data(filp);
476 struct media_device *dev = to_media_device(devnode);
477 long ret;
478
479 switch (cmd) {
480 case MEDIA_IOC_DEVICE_INFO:
481 case MEDIA_IOC_ENUM_ENTITIES:
482 case MEDIA_IOC_SETUP_LINK:
Mauro Carvalho Chehab8309f472015-08-23 10:36:41 -0300483 case MEDIA_IOC_G_TOPOLOGY:
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300484 return media_device_ioctl(filp, cmd, arg);
485
486 case MEDIA_IOC_ENUM_LINKS32:
487 mutex_lock(&dev->graph_mutex);
488 ret = media_device_enum_links32(dev,
489 (struct media_links_enum32 __user *)arg);
490 mutex_unlock(&dev->graph_mutex);
491 break;
492
493 default:
494 ret = -ENOIOCTLCMD;
495 }
496
497 return ret;
498}
499#endif /* CONFIG_COMPAT */
500
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300501static const struct media_file_operations media_device_fops = {
502 .owner = THIS_MODULE,
Laurent Pinchart140d8812010-08-18 11:41:22 -0300503 .open = media_device_open,
504 .ioctl = media_device_ioctl,
Sakari Ailusb0a1f2a2013-01-22 12:27:56 -0300505#ifdef CONFIG_COMPAT
506 .compat_ioctl = media_device_compat_ioctl,
507#endif /* CONFIG_COMPAT */
Laurent Pinchart140d8812010-08-18 11:41:22 -0300508 .release = media_device_close,
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300509};
510
511/* -----------------------------------------------------------------------------
512 * sysfs
513 */
514
515static ssize_t show_model(struct device *cd,
516 struct device_attribute *attr, char *buf)
517{
518 struct media_device *mdev = to_media_device(to_media_devnode(cd));
519
520 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
521}
522
523static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
524
525/* -----------------------------------------------------------------------------
526 * Registration/unregistration
527 */
528
529static void media_device_release(struct media_devnode *mdev)
530{
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300531 dev_dbg(mdev->parent, "Media device released\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300532}
533
534/**
535 * media_device_register - register a media device
536 * @mdev: The media device
537 *
538 * The caller is responsible for initializing the media device before
539 * registration. The following fields must be set:
540 *
541 * - dev must point to the parent device
542 * - model must be filled with the device model name
543 */
Sakari Ailus85de7212013-12-12 12:38:17 -0300544int __must_check __media_device_register(struct media_device *mdev,
545 struct module *owner)
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300546{
547 int ret;
548
549 if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
550 return -EINVAL;
551
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300552 INIT_LIST_HEAD(&mdev->entities);
Mauro Carvalho Chehab57cf79b2015-08-21 09:23:22 -0300553 INIT_LIST_HEAD(&mdev->interfaces);
Mauro Carvalho Chehab9155d852015-08-23 08:00:33 -0300554 INIT_LIST_HEAD(&mdev->pads);
555 INIT_LIST_HEAD(&mdev->links);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300556 spin_lock_init(&mdev->lock);
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300557 mutex_init(&mdev->graph_mutex);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300558
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300559 /* Register the device node. */
560 mdev->devnode.fops = &media_device_fops;
561 mdev->devnode.parent = mdev->dev;
562 mdev->devnode.release = media_device_release;
Sakari Ailus85de7212013-12-12 12:38:17 -0300563 ret = media_devnode_register(&mdev->devnode, owner);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300564 if (ret < 0)
565 return ret;
566
567 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
568 if (ret < 0) {
569 media_devnode_unregister(&mdev->devnode);
570 return ret;
571 }
572
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300573 dev_dbg(mdev->dev, "Media device registered\n");
574
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300575 return 0;
576}
Sakari Ailus85de7212013-12-12 12:38:17 -0300577EXPORT_SYMBOL_GPL(__media_device_register);
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300578
579/**
580 * media_device_unregister - unregister a media device
581 * @mdev: The media device
582 *
583 */
584void media_device_unregister(struct media_device *mdev)
585{
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300586 struct media_entity *entity;
587 struct media_entity *next;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300588 struct media_interface *intf, *tmp_intf;
589
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300590 /* Remove all entities from the media device */
591 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
592 media_device_unregister_entity(entity);
593
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300594 /* Remove all interfaces from the media device */
595 spin_lock(&mdev->lock);
596 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
597 graph_obj.list) {
598 __media_remove_intf_links(intf);
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200599 media_gobj_destroy(&intf->graph_obj);
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300600 kfree(intf);
601 }
602 spin_unlock(&mdev->lock);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300603
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300604 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
605 media_devnode_unregister(&mdev->devnode);
Mauro Carvalho Chehab8f5a3182015-08-13 15:22:24 -0300606
607 dev_dbg(mdev->dev, "Media device unregistered\n");
Laurent Pinchart176fb0d2009-12-09 08:39:58 -0300608}
609EXPORT_SYMBOL_GPL(media_device_unregister);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300610
611/**
612 * media_device_register_entity - Register an entity with a media device
613 * @mdev: The media device
614 * @entity: The entity
615 */
616int __must_check media_device_register_entity(struct media_device *mdev,
617 struct media_entity *entity)
618{
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300619 unsigned int i;
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300620
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200621 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
622 entity->function == MEDIA_ENT_F_UNKNOWN)
Mauro Carvalho Chehabb50bde42015-05-07 22:12:38 -0300623 dev_warn(mdev->dev,
624 "Entity type for entity %s was not initialized!\n",
625 entity->name);
626
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300627 /* Warn if we apparently re-register an entity */
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300628 WARN_ON(entity->graph_obj.mdev != NULL);
629 entity->graph_obj.mdev = mdev;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300630 INIT_LIST_HEAD(&entity->links);
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200631 entity->num_links = 0;
632 entity->num_backlinks = 0;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300633
634 spin_lock(&mdev->lock);
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300635 /* Initialize media_gobj embedded at the entity */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200636 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300637
638 /* Initialize objects at the pads */
639 for (i = 0; i < entity->num_pads; i++)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200640 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300641 &entity->pads[i].graph_obj);
642
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300643 spin_unlock(&mdev->lock);
644
645 return 0;
646}
647EXPORT_SYMBOL_GPL(media_device_register_entity);
648
649/**
650 * media_device_unregister_entity - Unregister an entity
651 * @entity: The entity
652 *
653 * If the entity has never been registered this function will return
654 * immediately.
655 */
656void media_device_unregister_entity(struct media_entity *entity)
657{
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300658 struct media_device *mdev = entity->graph_obj.mdev;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300659 struct media_link *link, *tmp;
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300660 struct media_interface *intf;
Mauro Carvalho Chehab9033b1a2015-09-09 08:23:51 -0300661 unsigned int i;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300662
663 if (mdev == NULL)
664 return;
665
666 spin_lock(&mdev->lock);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300667
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300668 /* Remove all interface links pointing to this entity */
669 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
670 list_for_each_entry_safe(link, tmp, &intf->links, list) {
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300671 if (link->entity == entity)
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300672 __media_remove_intf_link(link);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300673 }
674 }
675
676 /* Remove all data links that belong to this entity */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300677 __media_entity_remove_links(entity);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300678
679 /* Remove all pads that belong to this entity */
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -0300680 for (i = 0; i < entity->num_pads; i++)
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200681 media_gobj_destroy(&entity->pads[i].graph_obj);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300682
683 /* Remove the entity */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200684 media_gobj_destroy(&entity->graph_obj);
Mauro Carvalho Chehaba28971a2015-08-29 19:07:09 -0300685
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300686 spin_unlock(&mdev->lock);
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -0300687 entity->graph_obj.mdev = NULL;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300688}
689EXPORT_SYMBOL_GPL(media_device_unregister_entity);
Shuah Khand062f912015-06-03 12:12:53 -0300690
691static void media_device_release_devres(struct device *dev, void *res)
692{
693}
694
695/*
696 * media_device_get_devres() - get media device as device resource
697 * creates if one doesn't exist
698*/
699struct media_device *media_device_get_devres(struct device *dev)
700{
701 struct media_device *mdev;
702
703 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
704 if (mdev)
705 return mdev;
706
707 mdev = devres_alloc(media_device_release_devres,
708 sizeof(struct media_device), GFP_KERNEL);
709 if (!mdev)
710 return NULL;
711 return devres_get(dev, mdev, NULL, NULL);
712}
713EXPORT_SYMBOL_GPL(media_device_get_devres);
714
715/*
716 * media_device_find_devres() - find media device as device resource
717*/
718struct media_device *media_device_find_devres(struct device *dev)
719{
720 return devres_find(dev, media_device_release_devres, NULL, NULL);
721}
722EXPORT_SYMBOL_GPL(media_device_find_devres);
Shuah Khane576d60b2015-06-05 17:11:54 -0300723
724#endif /* CONFIG_MEDIA_CONTROLLER */