]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/media-device.c
[media] usb: musb: sunxi: support module autoloading
[mirror_ubuntu-artful-kernel.git] / drivers / media / media-device.c
CommitLineData
176fb0d1
LP
1/*
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
b0a1f2a8
SA
23#include <linux/compat.h>
24#include <linux/export.h>
665faa97 25#include <linux/idr.h>
176fb0d1 26#include <linux/ioctl.h>
140d8816 27#include <linux/media.h>
57208e5e 28#include <linux/slab.h>
497e80cd 29#include <linux/types.h>
41b44e35
MCC
30#include <linux/pci.h>
31#include <linux/usb.h>
176fb0d1
LP
32
33#include <media/media-device.h>
34#include <media/media-devnode.h>
53e269c1 35#include <media/media-entity.h>
176fb0d1 36
e576d60b
SK
37#ifdef CONFIG_MEDIA_CONTROLLER
38
140d8816
LP
39/* -----------------------------------------------------------------------------
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
bb07bd6b
MCC
60 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
140d8816
LP
65 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
b17d3945
NT
73 if (copy_to_user(__info, &info, sizeof(*__info)))
74 return -EFAULT;
75 return 0;
140d8816
LP
76}
77
1651333b
LP
78static 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) {
fa762394
MCC
88 if (((media_entity_id(entity) == id) && !next) ||
89 ((media_entity_id(entity) > id) && next)) {
1651333b
LP
90 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
e6a62346 106 memset(&u_ent, 0, sizeof(u_ent));
1651333b
LP
107 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
fa762394 115 u_ent.id = media_entity_id(ent);
de8eae36
LP
116 if (ent->name)
117 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
0e576b76 118 u_ent.type = ent->function;
8ed8c88c 119 u_ent.revision = 0; /* Unused */
1651333b 120 u_ent.flags = ent->flags;
8ed8c88c 121 u_ent.group_id = 0; /* Unused */
1651333b
LP
122 u_ent.pads = ent->num_pads;
123 u_ent.links = ent->num_links - ent->num_backlinks;
fa5034c6 124 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
1651333b
LP
125 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{
fa762394 133 upad->entity = media_entity_id(kpad->entity);
1651333b
LP
134 upad->index = kpad->index;
135 upad->flags = kpad->flags;
136}
137
b0a1f2a8
SA
138static long __media_device_enum_links(struct media_device *mdev,
139 struct media_links_enum *links)
1651333b
LP
140{
141 struct media_entity *entity;
1651333b 142
b0a1f2a8 143 entity = find_entity(mdev, links->entity);
1651333b
LP
144 if (entity == NULL)
145 return -EINVAL;
146
b0a1f2a8 147 if (links->pads) {
1651333b
LP
148 unsigned int p;
149
150 for (p = 0; p < entity->num_pads; p++) {
151 struct media_pad_desc pad;
c88e739b
DC
152
153 memset(&pad, 0, sizeof(pad));
1651333b 154 media_device_kpad_to_upad(&entity->pads[p], &pad);
b0a1f2a8 155 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
1651333b
LP
156 return -EFAULT;
157 }
158 }
159
b0a1f2a8 160 if (links->links) {
b83e2508
MCC
161 struct media_link *link;
162 struct media_link_desc __user *ulink_desc = links->links;
1651333b 163
b83e2508
MCC
164 list_for_each_entry(link, &entity->links, list) {
165 struct media_link_desc klink_desc;
1651333b
LP
166
167 /* Ignore backlinks. */
b83e2508 168 if (link->source->entity != entity)
1651333b 169 continue;
b83e2508
MCC
170 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)))
1651333b 178 return -EFAULT;
b83e2508 179 ulink_desc++;
1651333b
LP
180 }
181 }
b0a1f2a8
SA
182
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
1651333b
LP
199 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
200 return -EFAULT;
b0a1f2a8 201
1651333b
LP
202 return 0;
203}
204
97548ed4
LP
205static 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
8309f47c
MCC
243static 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;
b3b7a9f1
MCC
250 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;
9033b1a4
MCC
254 unsigned int i;
255 int ret = 0;
8309f47c
MCC
256
257 topo->topology_version = mdev->topology_version;
258
259 /* Get entities and number of entities */
260 i = 0;
b3b7a9f1 261 uentity = media_get_uptr(topo->ptr_entities);
8309f47c
MCC
262 media_device_for_each_entity(entity, mdev) {
263 i++;
b3b7a9f1 264 if (ret || !uentity)
8309f47c
MCC
265 continue;
266
267 if (i > topo->num_entities) {
268 ret = -ENOSPC;
269 continue;
270 }
271
272 /* Copy fields to userspace struct if not error */
b3b7a9f1
MCC
273 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));
8309f47c 278
b3b7a9f1 279 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
8309f47c 280 ret = -EFAULT;
b3b7a9f1 281 uentity++;
8309f47c
MCC
282 }
283 topo->num_entities = i;
284
285 /* Get interfaces and number of interfaces */
286 i = 0;
b3b7a9f1 287 uintf = media_get_uptr(topo->ptr_interfaces);
8309f47c
MCC
288 media_device_for_each_intf(intf, mdev) {
289 i++;
b3b7a9f1 290 if (ret || !uintf)
8309f47c
MCC
291 continue;
292
293 if (i > topo->num_interfaces) {
294 ret = -ENOSPC;
295 continue;
296 }
297
b3b7a9f1 298 memset(&kintf, 0, sizeof(kintf));
8309f47c
MCC
299
300 /* Copy intf fields to userspace struct */
b3b7a9f1
MCC
301 kintf.id = intf->graph_obj.id;
302 kintf.intf_type = intf->type;
303 kintf.flags = intf->flags;
8309f47c
MCC
304
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
b3b7a9f1
MCC
310 kintf.devnode.major = devnode->major;
311 kintf.devnode.minor = devnode->minor;
8309f47c
MCC
312 }
313
b3b7a9f1 314 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
8309f47c 315 ret = -EFAULT;
b3b7a9f1 316 uintf++;
8309f47c
MCC
317 }
318 topo->num_interfaces = i;
319
320 /* Get pads and number of pads */
321 i = 0;
b3b7a9f1 322 upad = media_get_uptr(topo->ptr_pads);
8309f47c
MCC
323 media_device_for_each_pad(pad, mdev) {
324 i++;
b3b7a9f1 325 if (ret || !upad)
8309f47c
MCC
326 continue;
327
328 if (i > topo->num_pads) {
329 ret = -ENOSPC;
330 continue;
331 }
332
b3b7a9f1 333 memset(&kpad, 0, sizeof(kpad));
8309f47c
MCC
334
335 /* Copy pad fields to userspace struct */
b3b7a9f1
MCC
336 kpad.id = pad->graph_obj.id;
337 kpad.entity_id = pad->entity->graph_obj.id;
338 kpad.flags = pad->flags;
8309f47c 339
b3b7a9f1 340 if (copy_to_user(upad, &kpad, sizeof(kpad)))
8309f47c 341 ret = -EFAULT;
b3b7a9f1 342 upad++;
8309f47c
MCC
343 }
344 topo->num_pads = i;
345
346 /* Get links and number of links */
347 i = 0;
b3b7a9f1 348 ulink = media_get_uptr(topo->ptr_links);
8309f47c 349 media_device_for_each_link(link, mdev) {
39d1ebc6
MCC
350 if (link->is_backlink)
351 continue;
352
8309f47c
MCC
353 i++;
354
b3b7a9f1 355 if (ret || !ulink)
8309f47c
MCC
356 continue;
357
358 if (i > topo->num_links) {
359 ret = -ENOSPC;
360 continue;
361 }
362
b3b7a9f1 363 memset(&klink, 0, sizeof(klink));
8309f47c
MCC
364
365 /* Copy link fields to userspace struct */
b3b7a9f1
MCC
366 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;
8309f47c 370
b3b7a9f1 371 if (copy_to_user(ulink, &klink, sizeof(klink)))
8309f47c 372 ret = -EFAULT;
b3b7a9f1 373 ulink++;
8309f47c
MCC
374 }
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
a5c82e56
DC
386 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
387 return -EFAULT;
8309f47c
MCC
388
389 ret = __media_device_get_topology(mdev, &ktopo);
390 if (ret < 0)
391 return ret;
392
a5c82e56
DC
393 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
394 return -EFAULT;
8309f47c 395
a5c82e56 396 return 0;
8309f47c
MCC
397}
398
140d8816
LP
399static 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
1651333b
LP
412 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
97548ed4
LP
424 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
8309f47c
MCC
431 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;
333a6515 437
140d8816
LP
438 default:
439 ret = -ENOIOCTLCMD;
440 }
441
442 return ret;
443}
444
b0a1f2a8
SA
445#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:
8309f47c 486 case MEDIA_IOC_G_TOPOLOGY:
b0a1f2a8
SA
487 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
176fb0d1
LP
504static const struct media_file_operations media_device_fops = {
505 .owner = THIS_MODULE,
140d8816
LP
506 .open = media_device_open,
507 .ioctl = media_device_ioctl,
b0a1f2a8
SA
508#ifdef CONFIG_COMPAT
509 .compat_ioctl = media_device_compat_ioctl,
510#endif /* CONFIG_COMPAT */
140d8816 511 .release = media_device_close,
176fb0d1
LP
512};
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{
8f5a3188 534 dev_dbg(mdev->parent, "Media device released\n");
176fb0d1
LP
535}
536
b2ed8af9
MCC
537/**
538 * 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{
afcbdb55 545 struct media_entity_notify *notify, *next;
b2ed8af9 546 unsigned int i;
ba0dd729 547 int ret;
b2ed8af9
MCC
548
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
ba0dd729
MCC
562 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
563 return -ENOMEM;
564
b2ed8af9 565 spin_lock(&mdev->lock);
665faa97 566
ba0dd729
MCC
567 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
568 &entity->internal_idx);
569 if (ret < 0) {
665faa97 570 spin_unlock(&mdev->lock);
ba0dd729 571 return ret;
665faa97
SA
572 }
573
574 mdev->entity_internal_idx_max =
575 max(mdev->entity_internal_idx_max, entity->internal_idx);
576
b2ed8af9
MCC
577 /* 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
afcbdb55
SK
585 /* 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
b2ed8af9
MCC
590 spin_unlock(&mdev->lock);
591
0c426c47
SA
592 mutex_lock(&mdev->graph_mutex);
593 if (mdev->entity_internal_idx_max
594 >= mdev->pm_count_walk.ent_enum.idx_max) {
595 struct media_entity_graph new = { .top = 0 };
596
597 /*
598 * Initialise the new graph walk before cleaning up
599 * the old one in order not to spoil the graph walk
600 * object of the media device if graph walk init fails.
601 */
602 ret = media_entity_graph_walk_init(&new, mdev);
603 if (ret) {
604 mutex_unlock(&mdev->graph_mutex);
605 return ret;
606 }
607 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
608 mdev->pm_count_walk = new;
609 }
610 mutex_unlock(&mdev->graph_mutex);
611
b2ed8af9
MCC
612 return 0;
613}
614EXPORT_SYMBOL_GPL(media_device_register_entity);
615
821ed366 616static void __media_device_unregister_entity(struct media_entity *entity)
b2ed8af9
MCC
617{
618 struct media_device *mdev = entity->graph_obj.mdev;
619 struct media_link *link, *tmp;
620 struct media_interface *intf;
621 unsigned int i;
622
665faa97
SA
623 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
624
b2ed8af9
MCC
625 /* Remove all interface links pointing to this entity */
626 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
627 list_for_each_entry_safe(link, tmp, &intf->links, list) {
628 if (link->entity == entity)
629 __media_remove_intf_link(link);
630 }
631 }
632
633 /* Remove all data links that belong to this entity */
634 __media_entity_remove_links(entity);
635
636 /* Remove all pads that belong to this entity */
637 for (i = 0; i < entity->num_pads; i++)
638 media_gobj_destroy(&entity->pads[i].graph_obj);
639
640 /* Remove the entity */
641 media_gobj_destroy(&entity->graph_obj);
642
afcbdb55
SK
643 /* invoke entity_notify callbacks to handle entity removal?? */
644
b2ed8af9
MCC
645 entity->graph_obj.mdev = NULL;
646}
821ed366
MCC
647
648void media_device_unregister_entity(struct media_entity *entity)
649{
650 struct media_device *mdev = entity->graph_obj.mdev;
651
652 if (mdev == NULL)
653 return;
654
655 spin_lock(&mdev->lock);
656 __media_device_unregister_entity(entity);
657 spin_unlock(&mdev->lock);
658}
b2ed8af9
MCC
659EXPORT_SYMBOL_GPL(media_device_unregister_entity);
660
176fb0d1 661/**
9832e155 662 * media_device_init() - initialize a media device
176fb0d1
LP
663 * @mdev: The media device
664 *
665 * The caller is responsible for initializing the media device before
666 * registration. The following fields must be set:
667 *
668 * - dev must point to the parent device
669 * - model must be filled with the device model name
670 */
9832e155 671void media_device_init(struct media_device *mdev)
176fb0d1 672{
53e269c1 673 INIT_LIST_HEAD(&mdev->entities);
57cf79b7 674 INIT_LIST_HEAD(&mdev->interfaces);
9155d859
MCC
675 INIT_LIST_HEAD(&mdev->pads);
676 INIT_LIST_HEAD(&mdev->links);
afcbdb55 677 INIT_LIST_HEAD(&mdev->entity_notify);
53e269c1 678 spin_lock_init(&mdev->lock);
503c3d82 679 mutex_init(&mdev->graph_mutex);
665faa97 680 ida_init(&mdev->entity_internal_idx);
53e269c1 681
9832e155
JMC
682 dev_dbg(mdev->dev, "Media device initialized\n");
683}
684EXPORT_SYMBOL_GPL(media_device_init);
685
9832e155
JMC
686void media_device_cleanup(struct media_device *mdev)
687{
665faa97
SA
688 ida_destroy(&mdev->entity_internal_idx);
689 mdev->entity_internal_idx_max = 0;
0c426c47 690 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
9832e155
JMC
691 mutex_destroy(&mdev->graph_mutex);
692}
693EXPORT_SYMBOL_GPL(media_device_cleanup);
694
9832e155
JMC
695int __must_check __media_device_register(struct media_device *mdev,
696 struct module *owner)
697{
698 int ret;
699
176fb0d1
LP
700 /* Register the device node. */
701 mdev->devnode.fops = &media_device_fops;
702 mdev->devnode.parent = mdev->dev;
703 mdev->devnode.release = media_device_release;
8a950796
JMC
704
705 /* Set version 0 to indicate user-space that the graph is static */
706 mdev->topology_version = 0;
707
85de721c 708 ret = media_devnode_register(&mdev->devnode, owner);
176fb0d1
LP
709 if (ret < 0)
710 return ret;
711
712 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
713 if (ret < 0) {
714 media_devnode_unregister(&mdev->devnode);
715 return ret;
716 }
717
8f5a3188
MCC
718 dev_dbg(mdev->dev, "Media device registered\n");
719
176fb0d1
LP
720 return 0;
721}
85de721c 722EXPORT_SYMBOL_GPL(__media_device_register);
176fb0d1 723
afcbdb55
SK
724int __must_check media_device_register_entity_notify(struct media_device *mdev,
725 struct media_entity_notify *nptr)
726{
727 spin_lock(&mdev->lock);
728 list_add_tail(&nptr->list, &mdev->entity_notify);
729 spin_unlock(&mdev->lock);
730 return 0;
731}
732EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
733
734/*
735 * Note: Should be called with mdev->lock held.
736 */
737static void __media_device_unregister_entity_notify(struct media_device *mdev,
738 struct media_entity_notify *nptr)
739{
740 list_del(&nptr->list);
741}
742
743void media_device_unregister_entity_notify(struct media_device *mdev,
744 struct media_entity_notify *nptr)
745{
746 spin_lock(&mdev->lock);
747 __media_device_unregister_entity_notify(mdev, nptr);
748 spin_unlock(&mdev->lock);
749}
750EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
751
176fb0d1
LP
752void media_device_unregister(struct media_device *mdev)
753{
53e269c1
LP
754 struct media_entity *entity;
755 struct media_entity *next;
d47109fa 756 struct media_interface *intf, *tmp_intf;
afcbdb55 757 struct media_entity_notify *notify, *nextp;
d47109fa 758
821ed366
MCC
759 if (mdev == NULL)
760 return;
761
762 spin_lock(&mdev->lock);
763
223d19c5 764 /* Check if mdev was ever registered at all */
821ed366
MCC
765 if (!media_devnode_is_registered(&mdev->devnode)) {
766 spin_unlock(&mdev->lock);
223d19c5 767 return;
821ed366 768 }
223d19c5 769
f50d5166
MCC
770 /* Remove all entities from the media device */
771 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
821ed366 772 __media_device_unregister_entity(entity);
f50d5166 773
afcbdb55
SK
774 /* Remove all entity_notify callbacks from the media device */
775 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
776 __media_device_unregister_entity_notify(mdev, notify);
777
d47109fa 778 /* Remove all interfaces from the media device */
d47109fa
MCC
779 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
780 graph_obj.list) {
781 __media_remove_intf_links(intf);
c350ef83 782 media_gobj_destroy(&intf->graph_obj);
d47109fa
MCC
783 kfree(intf);
784 }
821ed366 785
d47109fa 786 spin_unlock(&mdev->lock);
53e269c1 787
176fb0d1
LP
788 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
789 media_devnode_unregister(&mdev->devnode);
8f5a3188
MCC
790
791 dev_dbg(mdev->dev, "Media device unregistered\n");
176fb0d1
LP
792}
793EXPORT_SYMBOL_GPL(media_device_unregister);
53e269c1 794
d062f911
SK
795static void media_device_release_devres(struct device *dev, void *res)
796{
797}
798
d062f911
SK
799struct media_device *media_device_get_devres(struct device *dev)
800{
801 struct media_device *mdev;
802
803 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
804 if (mdev)
805 return mdev;
806
807 mdev = devres_alloc(media_device_release_devres,
808 sizeof(struct media_device), GFP_KERNEL);
809 if (!mdev)
810 return NULL;
811 return devres_get(dev, mdev, NULL, NULL);
812}
813EXPORT_SYMBOL_GPL(media_device_get_devres);
814
d062f911
SK
815struct media_device *media_device_find_devres(struct device *dev)
816{
817 return devres_find(dev, media_device_release_devres, NULL, NULL);
818}
819EXPORT_SYMBOL_GPL(media_device_find_devres);
e576d60b 820
6cf5dad1
MCC
821void media_device_pci_init(struct media_device *mdev,
822 struct pci_dev *pci_dev,
823 const char *name)
41b44e35
MCC
824{
825#ifdef CONFIG_PCI
41b44e35
MCC
826 mdev->dev = &pci_dev->dev;
827
828 if (name)
829 strlcpy(mdev->model, name, sizeof(mdev->model));
830 else
831 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
832
833 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
834
835 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
836 | pci_dev->subsystem_device;
837
838 mdev->driver_version = LINUX_VERSION_CODE;
839
840 media_device_init(mdev);
41b44e35
MCC
841#endif
842}
843EXPORT_SYMBOL_GPL(media_device_pci_init);
844
6cf5dad1
MCC
845void __media_device_usb_init(struct media_device *mdev,
846 struct usb_device *udev,
847 const char *board_name,
848 const char *driver_name)
41b44e35
MCC
849{
850#ifdef CONFIG_USB
41b44e35
MCC
851 mdev->dev = &udev->dev;
852
853 if (driver_name)
854 strlcpy(mdev->driver_name, driver_name,
855 sizeof(mdev->driver_name));
856
857 if (board_name)
858 strlcpy(mdev->model, board_name, sizeof(mdev->model));
859 else if (udev->product)
860 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
861 else
862 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
863 if (udev->serial)
864 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
865 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
866 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
867 mdev->driver_version = LINUX_VERSION_CODE;
868
869 media_device_init(mdev);
41b44e35
MCC
870#endif
871}
872EXPORT_SYMBOL_GPL(__media_device_usb_init);
873
874
e576d60b 875#endif /* CONFIG_MEDIA_CONTROLLER */