[media] media-device: fix builds when USB or PCI is compiled as module
[deliverable/linux.git] / drivers / media / media-device.c
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
23 /* We need to access legacy defines from linux/media.h */
24 #define __NEED_MEDIA_LEGACY_API
25
26 #include <linux/compat.h>
27 #include <linux/export.h>
28 #include <linux/idr.h>
29 #include <linux/ioctl.h>
30 #include <linux/media.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/usb.h>
35
36 #include <media/media-device.h>
37 #include <media/media-devnode.h>
38 #include <media/media-entity.h>
39
40 #ifdef CONFIG_MEDIA_CONTROLLER
41
42 /* -----------------------------------------------------------------------------
43 * Userspace API
44 */
45
46 static inline void __user *media_get_uptr(__u64 arg)
47 {
48 return (void __user *)(uintptr_t)arg;
49 }
50
51 static int media_device_open(struct file *filp)
52 {
53 return 0;
54 }
55
56 static int media_device_close(struct file *filp)
57 {
58 return 0;
59 }
60
61 static int media_device_get_info(struct media_device *dev,
62 struct media_device_info __user *__info)
63 {
64 struct media_device_info info;
65
66 memset(&info, 0, sizeof(info));
67
68 if (dev->driver_name[0])
69 strlcpy(info.driver, dev->driver_name, sizeof(info.driver));
70 else
71 strlcpy(info.driver, dev->dev->driver->name, sizeof(info.driver));
72
73 strlcpy(info.model, dev->model, sizeof(info.model));
74 strlcpy(info.serial, dev->serial, sizeof(info.serial));
75 strlcpy(info.bus_info, dev->bus_info, sizeof(info.bus_info));
76
77 info.media_version = MEDIA_API_VERSION;
78 info.hw_revision = dev->hw_revision;
79 info.driver_version = dev->driver_version;
80
81 if (copy_to_user(__info, &info, sizeof(*__info)))
82 return -EFAULT;
83 return 0;
84 }
85
86 static struct media_entity *find_entity(struct media_device *mdev, u32 id)
87 {
88 struct media_entity *entity;
89 int next = id & MEDIA_ENT_ID_FLAG_NEXT;
90
91 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
92
93 spin_lock(&mdev->lock);
94
95 media_device_for_each_entity(entity, mdev) {
96 if (((media_entity_id(entity) == id) && !next) ||
97 ((media_entity_id(entity) > id) && next)) {
98 spin_unlock(&mdev->lock);
99 return entity;
100 }
101 }
102
103 spin_unlock(&mdev->lock);
104
105 return NULL;
106 }
107
108 static long media_device_enum_entities(struct media_device *mdev,
109 struct media_entity_desc __user *uent)
110 {
111 struct media_entity *ent;
112 struct media_entity_desc u_ent;
113
114 memset(&u_ent, 0, sizeof(u_ent));
115 if (copy_from_user(&u_ent.id, &uent->id, sizeof(u_ent.id)))
116 return -EFAULT;
117
118 ent = find_entity(mdev, u_ent.id);
119
120 if (ent == NULL)
121 return -EINVAL;
122
123 u_ent.id = media_entity_id(ent);
124 if (ent->name)
125 strlcpy(u_ent.name, ent->name, sizeof(u_ent.name));
126 u_ent.type = ent->function;
127 u_ent.revision = 0; /* Unused */
128 u_ent.flags = ent->flags;
129 u_ent.group_id = 0; /* Unused */
130 u_ent.pads = ent->num_pads;
131 u_ent.links = ent->num_links - ent->num_backlinks;
132
133 /*
134 * Workaround for a bug at media-ctl <= v1.10 that makes it to
135 * do the wrong thing if the entity function doesn't belong to
136 * either MEDIA_ENT_F_OLD_BASE or MEDIA_ENT_F_OLD_SUBDEV_BASE
137 * Ranges.
138 *
139 * Non-subdevices are expected to be at the MEDIA_ENT_F_OLD_BASE,
140 * or, otherwise, will be silently ignored by media-ctl when
141 * printing the graphviz diagram. So, map them into the devnode
142 * old range.
143 */
144 if (ent->function < MEDIA_ENT_F_OLD_BASE ||
145 ent->function > MEDIA_ENT_T_DEVNODE_UNKNOWN) {
146 if (is_media_entity_v4l2_subdev(ent))
147 u_ent.type = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
148 else if (ent->function != MEDIA_ENT_F_IO_V4L)
149 u_ent.type = MEDIA_ENT_T_DEVNODE_UNKNOWN;
150 }
151
152 memcpy(&u_ent.raw, &ent->info, sizeof(ent->info));
153 if (copy_to_user(uent, &u_ent, sizeof(u_ent)))
154 return -EFAULT;
155 return 0;
156 }
157
158 static void media_device_kpad_to_upad(const struct media_pad *kpad,
159 struct media_pad_desc *upad)
160 {
161 upad->entity = media_entity_id(kpad->entity);
162 upad->index = kpad->index;
163 upad->flags = kpad->flags;
164 }
165
166 static long __media_device_enum_links(struct media_device *mdev,
167 struct media_links_enum *links)
168 {
169 struct media_entity *entity;
170
171 entity = find_entity(mdev, links->entity);
172 if (entity == NULL)
173 return -EINVAL;
174
175 if (links->pads) {
176 unsigned int p;
177
178 for (p = 0; p < entity->num_pads; p++) {
179 struct media_pad_desc pad;
180
181 memset(&pad, 0, sizeof(pad));
182 media_device_kpad_to_upad(&entity->pads[p], &pad);
183 if (copy_to_user(&links->pads[p], &pad, sizeof(pad)))
184 return -EFAULT;
185 }
186 }
187
188 if (links->links) {
189 struct media_link *link;
190 struct media_link_desc __user *ulink_desc = links->links;
191
192 list_for_each_entry(link, &entity->links, list) {
193 struct media_link_desc klink_desc;
194
195 /* Ignore backlinks. */
196 if (link->source->entity != entity)
197 continue;
198 memset(&klink_desc, 0, sizeof(klink_desc));
199 media_device_kpad_to_upad(link->source,
200 &klink_desc.source);
201 media_device_kpad_to_upad(link->sink,
202 &klink_desc.sink);
203 klink_desc.flags = link->flags;
204 if (copy_to_user(ulink_desc, &klink_desc,
205 sizeof(*ulink_desc)))
206 return -EFAULT;
207 ulink_desc++;
208 }
209 }
210
211 return 0;
212 }
213
214 static long media_device_enum_links(struct media_device *mdev,
215 struct media_links_enum __user *ulinks)
216 {
217 struct media_links_enum links;
218 int rval;
219
220 if (copy_from_user(&links, ulinks, sizeof(links)))
221 return -EFAULT;
222
223 rval = __media_device_enum_links(mdev, &links);
224 if (rval < 0)
225 return rval;
226
227 if (copy_to_user(ulinks, &links, sizeof(*ulinks)))
228 return -EFAULT;
229
230 return 0;
231 }
232
233 static long media_device_setup_link(struct media_device *mdev,
234 struct media_link_desc __user *_ulink)
235 {
236 struct media_link *link = NULL;
237 struct media_link_desc ulink;
238 struct media_entity *source;
239 struct media_entity *sink;
240 int ret;
241
242 if (copy_from_user(&ulink, _ulink, sizeof(ulink)))
243 return -EFAULT;
244
245 /* Find the source and sink entities and link.
246 */
247 source = find_entity(mdev, ulink.source.entity);
248 sink = find_entity(mdev, ulink.sink.entity);
249
250 if (source == NULL || sink == NULL)
251 return -EINVAL;
252
253 if (ulink.source.index >= source->num_pads ||
254 ulink.sink.index >= sink->num_pads)
255 return -EINVAL;
256
257 link = media_entity_find_link(&source->pads[ulink.source.index],
258 &sink->pads[ulink.sink.index]);
259 if (link == NULL)
260 return -EINVAL;
261
262 /* Setup the link on both entities. */
263 ret = __media_entity_setup_link(link, ulink.flags);
264
265 if (copy_to_user(_ulink, &ulink, sizeof(ulink)))
266 return -EFAULT;
267
268 return ret;
269 }
270
271 static long __media_device_get_topology(struct media_device *mdev,
272 struct media_v2_topology *topo)
273 {
274 struct media_entity *entity;
275 struct media_interface *intf;
276 struct media_pad *pad;
277 struct media_link *link;
278 struct media_v2_entity kentity, __user *uentity;
279 struct media_v2_interface kintf, __user *uintf;
280 struct media_v2_pad kpad, __user *upad;
281 struct media_v2_link klink, __user *ulink;
282 unsigned int i;
283 int ret = 0;
284
285 topo->topology_version = mdev->topology_version;
286
287 /* Get entities and number of entities */
288 i = 0;
289 uentity = media_get_uptr(topo->ptr_entities);
290 media_device_for_each_entity(entity, mdev) {
291 i++;
292 if (ret || !uentity)
293 continue;
294
295 if (i > topo->num_entities) {
296 ret = -ENOSPC;
297 continue;
298 }
299
300 /* Copy fields to userspace struct if not error */
301 memset(&kentity, 0, sizeof(kentity));
302 kentity.id = entity->graph_obj.id;
303 kentity.function = entity->function;
304 strncpy(kentity.name, entity->name,
305 sizeof(kentity.name));
306
307 if (copy_to_user(uentity, &kentity, sizeof(kentity)))
308 ret = -EFAULT;
309 uentity++;
310 }
311 topo->num_entities = i;
312
313 /* Get interfaces and number of interfaces */
314 i = 0;
315 uintf = media_get_uptr(topo->ptr_interfaces);
316 media_device_for_each_intf(intf, mdev) {
317 i++;
318 if (ret || !uintf)
319 continue;
320
321 if (i > topo->num_interfaces) {
322 ret = -ENOSPC;
323 continue;
324 }
325
326 memset(&kintf, 0, sizeof(kintf));
327
328 /* Copy intf fields to userspace struct */
329 kintf.id = intf->graph_obj.id;
330 kintf.intf_type = intf->type;
331 kintf.flags = intf->flags;
332
333 if (media_type(&intf->graph_obj) == MEDIA_GRAPH_INTF_DEVNODE) {
334 struct media_intf_devnode *devnode;
335
336 devnode = intf_to_devnode(intf);
337
338 kintf.devnode.major = devnode->major;
339 kintf.devnode.minor = devnode->minor;
340 }
341
342 if (copy_to_user(uintf, &kintf, sizeof(kintf)))
343 ret = -EFAULT;
344 uintf++;
345 }
346 topo->num_interfaces = i;
347
348 /* Get pads and number of pads */
349 i = 0;
350 upad = media_get_uptr(topo->ptr_pads);
351 media_device_for_each_pad(pad, mdev) {
352 i++;
353 if (ret || !upad)
354 continue;
355
356 if (i > topo->num_pads) {
357 ret = -ENOSPC;
358 continue;
359 }
360
361 memset(&kpad, 0, sizeof(kpad));
362
363 /* Copy pad fields to userspace struct */
364 kpad.id = pad->graph_obj.id;
365 kpad.entity_id = pad->entity->graph_obj.id;
366 kpad.flags = pad->flags;
367
368 if (copy_to_user(upad, &kpad, sizeof(kpad)))
369 ret = -EFAULT;
370 upad++;
371 }
372 topo->num_pads = i;
373
374 /* Get links and number of links */
375 i = 0;
376 ulink = media_get_uptr(topo->ptr_links);
377 media_device_for_each_link(link, mdev) {
378 if (link->is_backlink)
379 continue;
380
381 i++;
382
383 if (ret || !ulink)
384 continue;
385
386 if (i > topo->num_links) {
387 ret = -ENOSPC;
388 continue;
389 }
390
391 memset(&klink, 0, sizeof(klink));
392
393 /* Copy link fields to userspace struct */
394 klink.id = link->graph_obj.id;
395 klink.source_id = link->gobj0->id;
396 klink.sink_id = link->gobj1->id;
397 klink.flags = link->flags;
398
399 if (copy_to_user(ulink, &klink, sizeof(klink)))
400 ret = -EFAULT;
401 ulink++;
402 }
403 topo->num_links = i;
404
405 return ret;
406 }
407
408 static long media_device_get_topology(struct media_device *mdev,
409 struct media_v2_topology __user *utopo)
410 {
411 struct media_v2_topology ktopo;
412 int ret;
413
414 if (copy_from_user(&ktopo, utopo, sizeof(ktopo)))
415 return -EFAULT;
416
417 ret = __media_device_get_topology(mdev, &ktopo);
418 if (ret < 0)
419 return ret;
420
421 if (copy_to_user(utopo, &ktopo, sizeof(*utopo)))
422 return -EFAULT;
423
424 return 0;
425 }
426
427 static long media_device_ioctl(struct file *filp, unsigned int cmd,
428 unsigned long arg)
429 {
430 struct media_devnode *devnode = media_devnode_data(filp);
431 struct media_device *dev = to_media_device(devnode);
432 long ret;
433
434 switch (cmd) {
435 case MEDIA_IOC_DEVICE_INFO:
436 ret = media_device_get_info(dev,
437 (struct media_device_info __user *)arg);
438 break;
439
440 case MEDIA_IOC_ENUM_ENTITIES:
441 ret = media_device_enum_entities(dev,
442 (struct media_entity_desc __user *)arg);
443 break;
444
445 case MEDIA_IOC_ENUM_LINKS:
446 mutex_lock(&dev->graph_mutex);
447 ret = media_device_enum_links(dev,
448 (struct media_links_enum __user *)arg);
449 mutex_unlock(&dev->graph_mutex);
450 break;
451
452 case MEDIA_IOC_SETUP_LINK:
453 mutex_lock(&dev->graph_mutex);
454 ret = media_device_setup_link(dev,
455 (struct media_link_desc __user *)arg);
456 mutex_unlock(&dev->graph_mutex);
457 break;
458
459 case MEDIA_IOC_G_TOPOLOGY:
460 mutex_lock(&dev->graph_mutex);
461 ret = media_device_get_topology(dev,
462 (struct media_v2_topology __user *)arg);
463 mutex_unlock(&dev->graph_mutex);
464 break;
465
466 default:
467 ret = -ENOIOCTLCMD;
468 }
469
470 return ret;
471 }
472
473 #ifdef CONFIG_COMPAT
474
475 struct media_links_enum32 {
476 __u32 entity;
477 compat_uptr_t pads; /* struct media_pad_desc * */
478 compat_uptr_t links; /* struct media_link_desc * */
479 __u32 reserved[4];
480 };
481
482 static long media_device_enum_links32(struct media_device *mdev,
483 struct media_links_enum32 __user *ulinks)
484 {
485 struct media_links_enum links;
486 compat_uptr_t pads_ptr, links_ptr;
487
488 memset(&links, 0, sizeof(links));
489
490 if (get_user(links.entity, &ulinks->entity)
491 || get_user(pads_ptr, &ulinks->pads)
492 || get_user(links_ptr, &ulinks->links))
493 return -EFAULT;
494
495 links.pads = compat_ptr(pads_ptr);
496 links.links = compat_ptr(links_ptr);
497
498 return __media_device_enum_links(mdev, &links);
499 }
500
501 #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32)
502
503 static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
504 unsigned long arg)
505 {
506 struct media_devnode *devnode = media_devnode_data(filp);
507 struct media_device *dev = to_media_device(devnode);
508 long ret;
509
510 switch (cmd) {
511 case MEDIA_IOC_DEVICE_INFO:
512 case MEDIA_IOC_ENUM_ENTITIES:
513 case MEDIA_IOC_SETUP_LINK:
514 case MEDIA_IOC_G_TOPOLOGY:
515 return media_device_ioctl(filp, cmd, arg);
516
517 case MEDIA_IOC_ENUM_LINKS32:
518 mutex_lock(&dev->graph_mutex);
519 ret = media_device_enum_links32(dev,
520 (struct media_links_enum32 __user *)arg);
521 mutex_unlock(&dev->graph_mutex);
522 break;
523
524 default:
525 ret = -ENOIOCTLCMD;
526 }
527
528 return ret;
529 }
530 #endif /* CONFIG_COMPAT */
531
532 static const struct media_file_operations media_device_fops = {
533 .owner = THIS_MODULE,
534 .open = media_device_open,
535 .ioctl = media_device_ioctl,
536 #ifdef CONFIG_COMPAT
537 .compat_ioctl = media_device_compat_ioctl,
538 #endif /* CONFIG_COMPAT */
539 .release = media_device_close,
540 };
541
542 /* -----------------------------------------------------------------------------
543 * sysfs
544 */
545
546 static ssize_t show_model(struct device *cd,
547 struct device_attribute *attr, char *buf)
548 {
549 struct media_device *mdev = to_media_device(to_media_devnode(cd));
550
551 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
552 }
553
554 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
555
556 /* -----------------------------------------------------------------------------
557 * Registration/unregistration
558 */
559
560 static void media_device_release(struct media_devnode *mdev)
561 {
562 dev_dbg(mdev->parent, "Media device released\n");
563 }
564
565 /**
566 * media_device_register_entity - Register an entity with a media device
567 * @mdev: The media device
568 * @entity: The entity
569 */
570 int __must_check media_device_register_entity(struct media_device *mdev,
571 struct media_entity *entity)
572 {
573 struct media_entity_notify *notify, *next;
574 unsigned int i;
575 int ret;
576
577 if (entity->function == MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN ||
578 entity->function == MEDIA_ENT_F_UNKNOWN)
579 dev_warn(mdev->dev,
580 "Entity type for entity %s was not initialized!\n",
581 entity->name);
582
583 /* Warn if we apparently re-register an entity */
584 WARN_ON(entity->graph_obj.mdev != NULL);
585 entity->graph_obj.mdev = mdev;
586 INIT_LIST_HEAD(&entity->links);
587 entity->num_links = 0;
588 entity->num_backlinks = 0;
589
590 if (!ida_pre_get(&mdev->entity_internal_idx, GFP_KERNEL))
591 return -ENOMEM;
592
593 spin_lock(&mdev->lock);
594
595 ret = ida_get_new_above(&mdev->entity_internal_idx, 1,
596 &entity->internal_idx);
597 if (ret < 0) {
598 spin_unlock(&mdev->lock);
599 return ret;
600 }
601
602 mdev->entity_internal_idx_max =
603 max(mdev->entity_internal_idx_max, entity->internal_idx);
604
605 /* Initialize media_gobj embedded at the entity */
606 media_gobj_create(mdev, MEDIA_GRAPH_ENTITY, &entity->graph_obj);
607
608 /* Initialize objects at the pads */
609 for (i = 0; i < entity->num_pads; i++)
610 media_gobj_create(mdev, MEDIA_GRAPH_PAD,
611 &entity->pads[i].graph_obj);
612
613 /* invoke entity_notify callbacks */
614 list_for_each_entry_safe(notify, next, &mdev->entity_notify, list) {
615 (notify)->notify(entity, notify->notify_data);
616 }
617
618 spin_unlock(&mdev->lock);
619
620 mutex_lock(&mdev->graph_mutex);
621 if (mdev->entity_internal_idx_max
622 >= mdev->pm_count_walk.ent_enum.idx_max) {
623 struct media_entity_graph new = { .top = 0 };
624
625 /*
626 * Initialise the new graph walk before cleaning up
627 * the old one in order not to spoil the graph walk
628 * object of the media device if graph walk init fails.
629 */
630 ret = media_entity_graph_walk_init(&new, mdev);
631 if (ret) {
632 mutex_unlock(&mdev->graph_mutex);
633 return ret;
634 }
635 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
636 mdev->pm_count_walk = new;
637 }
638 mutex_unlock(&mdev->graph_mutex);
639
640 return 0;
641 }
642 EXPORT_SYMBOL_GPL(media_device_register_entity);
643
644 static void __media_device_unregister_entity(struct media_entity *entity)
645 {
646 struct media_device *mdev = entity->graph_obj.mdev;
647 struct media_link *link, *tmp;
648 struct media_interface *intf;
649 unsigned int i;
650
651 ida_simple_remove(&mdev->entity_internal_idx, entity->internal_idx);
652
653 /* Remove all interface links pointing to this entity */
654 list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
655 list_for_each_entry_safe(link, tmp, &intf->links, list) {
656 if (link->entity == entity)
657 __media_remove_intf_link(link);
658 }
659 }
660
661 /* Remove all data links that belong to this entity */
662 __media_entity_remove_links(entity);
663
664 /* Remove all pads that belong to this entity */
665 for (i = 0; i < entity->num_pads; i++)
666 media_gobj_destroy(&entity->pads[i].graph_obj);
667
668 /* Remove the entity */
669 media_gobj_destroy(&entity->graph_obj);
670
671 /* invoke entity_notify callbacks to handle entity removal?? */
672
673 entity->graph_obj.mdev = NULL;
674 }
675
676 void media_device_unregister_entity(struct media_entity *entity)
677 {
678 struct media_device *mdev = entity->graph_obj.mdev;
679
680 if (mdev == NULL)
681 return;
682
683 spin_lock(&mdev->lock);
684 __media_device_unregister_entity(entity);
685 spin_unlock(&mdev->lock);
686 }
687 EXPORT_SYMBOL_GPL(media_device_unregister_entity);
688
689 /**
690 * media_device_init() - initialize a media device
691 * @mdev: The media device
692 *
693 * The caller is responsible for initializing the media device before
694 * registration. The following fields must be set:
695 *
696 * - dev must point to the parent device
697 * - model must be filled with the device model name
698 */
699 void media_device_init(struct media_device *mdev)
700 {
701 INIT_LIST_HEAD(&mdev->entities);
702 INIT_LIST_HEAD(&mdev->interfaces);
703 INIT_LIST_HEAD(&mdev->pads);
704 INIT_LIST_HEAD(&mdev->links);
705 INIT_LIST_HEAD(&mdev->entity_notify);
706 spin_lock_init(&mdev->lock);
707 mutex_init(&mdev->graph_mutex);
708 ida_init(&mdev->entity_internal_idx);
709
710 dev_dbg(mdev->dev, "Media device initialized\n");
711 }
712 EXPORT_SYMBOL_GPL(media_device_init);
713
714 void media_device_cleanup(struct media_device *mdev)
715 {
716 ida_destroy(&mdev->entity_internal_idx);
717 mdev->entity_internal_idx_max = 0;
718 media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
719 mutex_destroy(&mdev->graph_mutex);
720 }
721 EXPORT_SYMBOL_GPL(media_device_cleanup);
722
723 int __must_check __media_device_register(struct media_device *mdev,
724 struct module *owner)
725 {
726 int ret;
727
728 /* Register the device node. */
729 mdev->devnode.fops = &media_device_fops;
730 mdev->devnode.parent = mdev->dev;
731 mdev->devnode.release = media_device_release;
732
733 /* Set version 0 to indicate user-space that the graph is static */
734 mdev->topology_version = 0;
735
736 ret = media_devnode_register(&mdev->devnode, owner);
737 if (ret < 0)
738 return ret;
739
740 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
741 if (ret < 0) {
742 media_devnode_unregister(&mdev->devnode);
743 return ret;
744 }
745
746 dev_dbg(mdev->dev, "Media device registered\n");
747
748 return 0;
749 }
750 EXPORT_SYMBOL_GPL(__media_device_register);
751
752 int __must_check media_device_register_entity_notify(struct media_device *mdev,
753 struct media_entity_notify *nptr)
754 {
755 spin_lock(&mdev->lock);
756 list_add_tail(&nptr->list, &mdev->entity_notify);
757 spin_unlock(&mdev->lock);
758 return 0;
759 }
760 EXPORT_SYMBOL_GPL(media_device_register_entity_notify);
761
762 /*
763 * Note: Should be called with mdev->lock held.
764 */
765 static void __media_device_unregister_entity_notify(struct media_device *mdev,
766 struct media_entity_notify *nptr)
767 {
768 list_del(&nptr->list);
769 }
770
771 void media_device_unregister_entity_notify(struct media_device *mdev,
772 struct media_entity_notify *nptr)
773 {
774 spin_lock(&mdev->lock);
775 __media_device_unregister_entity_notify(mdev, nptr);
776 spin_unlock(&mdev->lock);
777 }
778 EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
779
780 void media_device_unregister(struct media_device *mdev)
781 {
782 struct media_entity *entity;
783 struct media_entity *next;
784 struct media_interface *intf, *tmp_intf;
785 struct media_entity_notify *notify, *nextp;
786
787 if (mdev == NULL)
788 return;
789
790 spin_lock(&mdev->lock);
791
792 /* Check if mdev was ever registered at all */
793 if (!media_devnode_is_registered(&mdev->devnode)) {
794 spin_unlock(&mdev->lock);
795 return;
796 }
797
798 /* Remove all entities from the media device */
799 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
800 __media_device_unregister_entity(entity);
801
802 /* Remove all entity_notify callbacks from the media device */
803 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list)
804 __media_device_unregister_entity_notify(mdev, notify);
805
806 /* Remove all interfaces from the media device */
807 list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces,
808 graph_obj.list) {
809 __media_remove_intf_links(intf);
810 media_gobj_destroy(&intf->graph_obj);
811 kfree(intf);
812 }
813
814 spin_unlock(&mdev->lock);
815
816 device_remove_file(&mdev->devnode.dev, &dev_attr_model);
817 media_devnode_unregister(&mdev->devnode);
818
819 dev_dbg(mdev->dev, "Media device unregistered\n");
820 }
821 EXPORT_SYMBOL_GPL(media_device_unregister);
822
823 static void media_device_release_devres(struct device *dev, void *res)
824 {
825 }
826
827 struct media_device *media_device_get_devres(struct device *dev)
828 {
829 struct media_device *mdev;
830
831 mdev = devres_find(dev, media_device_release_devres, NULL, NULL);
832 if (mdev)
833 return mdev;
834
835 mdev = devres_alloc(media_device_release_devres,
836 sizeof(struct media_device), GFP_KERNEL);
837 if (!mdev)
838 return NULL;
839 return devres_get(dev, mdev, NULL, NULL);
840 }
841 EXPORT_SYMBOL_GPL(media_device_get_devres);
842
843 struct media_device *media_device_find_devres(struct device *dev)
844 {
845 return devres_find(dev, media_device_release_devres, NULL, NULL);
846 }
847 EXPORT_SYMBOL_GPL(media_device_find_devres);
848
849 #if IS_ENABLED(CONFIG_PCI)
850 void media_device_pci_init(struct media_device *mdev,
851 struct pci_dev *pci_dev,
852 const char *name)
853 {
854 mdev->dev = &pci_dev->dev;
855
856 if (name)
857 strlcpy(mdev->model, name, sizeof(mdev->model));
858 else
859 strlcpy(mdev->model, pci_name(pci_dev), sizeof(mdev->model));
860
861 sprintf(mdev->bus_info, "PCI:%s", pci_name(pci_dev));
862
863 mdev->hw_revision = (pci_dev->subsystem_vendor << 16)
864 | pci_dev->subsystem_device;
865
866 mdev->driver_version = LINUX_VERSION_CODE;
867
868 media_device_init(mdev);
869 }
870 EXPORT_SYMBOL_GPL(media_device_pci_init);
871 #endif
872
873 #if IS_ENABLED(CONFIG_USB)
874 void __media_device_usb_init(struct media_device *mdev,
875 struct usb_device *udev,
876 const char *board_name,
877 const char *driver_name)
878 {
879 mdev->dev = &udev->dev;
880
881 if (driver_name)
882 strlcpy(mdev->driver_name, driver_name,
883 sizeof(mdev->driver_name));
884
885 if (board_name)
886 strlcpy(mdev->model, board_name, sizeof(mdev->model));
887 else if (udev->product)
888 strlcpy(mdev->model, udev->product, sizeof(mdev->model));
889 else
890 strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
891 if (udev->serial)
892 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
893 usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
894 mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
895 mdev->driver_version = LINUX_VERSION_CODE;
896
897 media_device_init(mdev);
898 }
899 EXPORT_SYMBOL_GPL(__media_device_usb_init);
900 #endif
901
902
903 #endif /* CONFIG_MEDIA_CONTROLLER */
This page took 0.049529 seconds and 5 git commands to generate.