Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 2 | /* |
| 3 | * Media entity |
| 4 | * |
| 5 | * Copyright (C) 2010 Nokia Corporation |
| 6 | * |
| 7 | * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 8 | * Sakari Ailus <sakari.ailus@iki.fi> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 9 | */ |
| 10 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 11 | #include <linux/bitmap.h> |
Daniel Scally | 1ed3d64 | 2022-07-07 23:47:32 +0100 | [diff] [blame] | 12 | #include <linux/list.h> |
Niklas Söderlund | d295c6a | 2017-06-15 06:17:26 -0300 | [diff] [blame] | 13 | #include <linux/property.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <media/media-entity.h> |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 16 | #include <media/media-device.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 17 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 18 | static inline const char *intf_type(struct media_interface *intf) |
| 19 | { |
| 20 | switch (intf->type) { |
| 21 | case MEDIA_INTF_T_DVB_FE: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 22 | return "dvb-frontend"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 23 | case MEDIA_INTF_T_DVB_DEMUX: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 24 | return "dvb-demux"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 25 | case MEDIA_INTF_T_DVB_DVR: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 26 | return "dvb-dvr"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 27 | case MEDIA_INTF_T_DVB_CA: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 28 | return "dvb-ca"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 29 | case MEDIA_INTF_T_DVB_NET: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 30 | return "dvb-net"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 31 | case MEDIA_INTF_T_V4L_VIDEO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 32 | return "v4l-video"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 33 | case MEDIA_INTF_T_V4L_VBI: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 34 | return "v4l-vbi"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 35 | case MEDIA_INTF_T_V4L_RADIO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 36 | return "v4l-radio"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 37 | case MEDIA_INTF_T_V4L_SUBDEV: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 38 | return "v4l-subdev"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 39 | case MEDIA_INTF_T_V4L_SWRADIO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 40 | return "v4l-swradio"; |
Nick Dyer | b2fe22d | 2016-07-18 18:10:30 -0300 | [diff] [blame] | 41 | case MEDIA_INTF_T_V4L_TOUCH: |
| 42 | return "v4l-touch"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 43 | default: |
| 44 | return "unknown-intf"; |
| 45 | } |
| 46 | }; |
| 47 | |
Daniel Scally | 9d0c23b | 2022-03-02 22:03:02 +0000 | [diff] [blame] | 48 | static inline const char *link_type_name(struct media_link *link) |
| 49 | { |
| 50 | switch (link->flags & MEDIA_LNK_FL_LINK_TYPE) { |
| 51 | case MEDIA_LNK_FL_DATA_LINK: |
| 52 | return "data"; |
| 53 | case MEDIA_LNK_FL_INTERFACE_LINK: |
| 54 | return "interface"; |
| 55 | case MEDIA_LNK_FL_ANCILLARY_LINK: |
| 56 | return "ancillary"; |
| 57 | default: |
| 58 | return "unknown"; |
| 59 | } |
| 60 | } |
| 61 | |
Laurent Pinchart | 49b3894 | 2022-08-31 16:13:29 +0200 | [diff] [blame] | 62 | __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum, |
| 63 | struct media_device *mdev) |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 64 | { |
Laurent Pinchart | 49b3894 | 2022-08-31 16:13:29 +0200 | [diff] [blame] | 65 | int idx_max; |
| 66 | |
| 67 | idx_max = ALIGN(mdev->entity_internal_idx_max + 1, BITS_PER_LONG); |
Christophe JAILLET | ce560ee | 2021-12-01 23:19:40 +0100 | [diff] [blame] | 68 | ent_enum->bmap = bitmap_zalloc(idx_max, GFP_KERNEL); |
Sakari Ailus | 030e89e | 2015-12-16 11:32:36 -0200 | [diff] [blame] | 69 | if (!ent_enum->bmap) |
| 70 | return -ENOMEM; |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 71 | |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 72 | ent_enum->idx_max = idx_max; |
| 73 | |
| 74 | return 0; |
| 75 | } |
Laurent Pinchart | 49b3894 | 2022-08-31 16:13:29 +0200 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(media_entity_enum_init); |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 77 | |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 78 | void media_entity_enum_cleanup(struct media_entity_enum *ent_enum) |
| 79 | { |
Christophe JAILLET | ce560ee | 2021-12-01 23:19:40 +0100 | [diff] [blame] | 80 | bitmap_free(ent_enum->bmap); |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL_GPL(media_entity_enum_cleanup); |
| 83 | |
| 84 | /** |
Mauro Carvalho Chehab | 1fc25d3 | 2015-12-11 12:14:58 -0200 | [diff] [blame] | 85 | * dev_dbg_obj - Prints in debug mode a change on some object |
| 86 | * |
| 87 | * @event_name: Name of the event to report. Could be __func__ |
| 88 | * @gobj: Pointer to the object |
| 89 | * |
| 90 | * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it |
| 91 | * won't produce any code. |
| 92 | */ |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 93 | static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) |
| 94 | { |
| 95 | #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) |
| 96 | switch (media_type(gobj)) { |
| 97 | case MEDIA_GRAPH_ENTITY: |
| 98 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 99 | "%s id %u: entity '%s'\n", |
| 100 | event_name, media_id(gobj), |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 101 | gobj_to_entity(gobj)->name); |
| 102 | break; |
| 103 | case MEDIA_GRAPH_LINK: |
| 104 | { |
| 105 | struct media_link *link = gobj_to_link(gobj); |
| 106 | |
| 107 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 108 | "%s id %u: %s link id %u ==> id %u\n", |
Daniel Scally | 9d0c23b | 2022-03-02 22:03:02 +0000 | [diff] [blame] | 109 | event_name, media_id(gobj), link_type_name(link), |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 110 | media_id(link->gobj0), |
| 111 | media_id(link->gobj1)); |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | case MEDIA_GRAPH_PAD: |
| 115 | { |
| 116 | struct media_pad *pad = gobj_to_pad(gobj); |
| 117 | |
| 118 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 119 | "%s id %u: %s%spad '%s':%d\n", |
| 120 | event_name, media_id(gobj), |
| 121 | pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "", |
Mauro Carvalho Chehab | 6c24d46 | 2015-08-21 18:26:42 -0300 | [diff] [blame] | 122 | pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "", |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 123 | pad->entity->name, pad->index); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 127 | { |
| 128 | struct media_interface *intf = gobj_to_intf(gobj); |
| 129 | struct media_intf_devnode *devnode = intf_to_devnode(intf); |
| 130 | |
| 131 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 132 | "%s id %u: intf_devnode %s - major: %d, minor: %d\n", |
| 133 | event_name, media_id(gobj), |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 134 | intf_type(intf), |
| 135 | devnode->major, devnode->minor); |
| 136 | break; |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | #endif |
| 140 | } |
| 141 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 142 | void media_gobj_create(struct media_device *mdev, |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 143 | enum media_gobj_type type, |
| 144 | struct media_gobj *gobj) |
| 145 | { |
Mauro Carvalho Chehab | 8f6d368 | 2015-08-19 20:18:35 -0300 | [diff] [blame] | 146 | BUG_ON(!mdev); |
| 147 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 148 | gobj->mdev = mdev; |
| 149 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 150 | /* Create a per-type unique object ID */ |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 151 | gobj->id = media_gobj_gen_id(type, ++mdev->id); |
| 152 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 153 | switch (type) { |
| 154 | case MEDIA_GRAPH_ENTITY: |
Mauro Carvalho Chehab | 05bfa9f | 2015-08-23 07:51:33 -0300 | [diff] [blame] | 155 | list_add_tail(&gobj->list, &mdev->entities); |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 156 | break; |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 157 | case MEDIA_GRAPH_PAD: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 158 | list_add_tail(&gobj->list, &mdev->pads); |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 159 | break; |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 160 | case MEDIA_GRAPH_LINK: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 161 | list_add_tail(&gobj->list, &mdev->links); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 162 | break; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 163 | case MEDIA_GRAPH_INTF_DEVNODE: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 164 | list_add_tail(&gobj->list, &mdev->interfaces); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 165 | break; |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 166 | } |
Mauro Carvalho Chehab | 2521fda | 2015-08-23 09:40:26 -0300 | [diff] [blame] | 167 | |
| 168 | mdev->topology_version++; |
| 169 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 170 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 171 | } |
| 172 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 173 | void media_gobj_destroy(struct media_gobj *gobj) |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 174 | { |
Max Kellermann | 6753743 | 2016-08-09 23:32:57 +0200 | [diff] [blame] | 175 | /* Do nothing if the object is not linked. */ |
| 176 | if (gobj->mdev == NULL) |
| 177 | return; |
| 178 | |
Helen Fornazier | 8d1d3d0 | 2017-04-06 16:25:15 -0300 | [diff] [blame] | 179 | dev_dbg_obj(__func__, gobj); |
| 180 | |
Mauro Carvalho Chehab | 2521fda | 2015-08-23 09:40:26 -0300 | [diff] [blame] | 181 | gobj->mdev->topology_version++; |
| 182 | |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 183 | /* Remove the object from mdev list */ |
| 184 | list_del(&gobj->list); |
Max Kellermann | 6753743 | 2016-08-09 23:32:57 +0200 | [diff] [blame] | 185 | |
| 186 | gobj->mdev = NULL; |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 187 | } |
| 188 | |
Sakari Ailus | 885ca80 | 2015-10-05 12:45:29 -0300 | [diff] [blame] | 189 | /* |
| 190 | * TODO: Get rid of this. |
| 191 | */ |
| 192 | #define MEDIA_ENTITY_MAX_PADS 512 |
| 193 | |
Mauro Carvalho Chehab | 1fc25d3 | 2015-12-11 12:14:58 -0200 | [diff] [blame] | 194 | int media_entity_pads_init(struct media_entity *entity, u16 num_pads, |
| 195 | struct media_pad *pads) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 196 | { |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 197 | struct media_device *mdev = entity->graph_obj.mdev; |
Jacopo Mondi | 8db465f | 2022-08-31 16:13:28 +0200 | [diff] [blame] | 198 | struct media_pad *iter; |
| 199 | unsigned int i = 0; |
Sakari Ailus | deb866f9 | 2023-04-25 12:30:45 +0300 | [diff] [blame] | 200 | int ret = 0; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 201 | |
Sakari Ailus | 885ca80 | 2015-10-05 12:45:29 -0300 | [diff] [blame] | 202 | if (num_pads >= MEDIA_ENTITY_MAX_PADS) |
| 203 | return -E2BIG; |
| 204 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 205 | entity->num_pads = num_pads; |
| 206 | entity->pads = pads; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 207 | |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 208 | if (mdev) |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 209 | mutex_lock(&mdev->graph_mutex); |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 210 | |
Jacopo Mondi | 8db465f | 2022-08-31 16:13:28 +0200 | [diff] [blame] | 211 | media_entity_for_each_pad(entity, iter) { |
| 212 | iter->entity = entity; |
| 213 | iter->index = i++; |
Sakari Ailus | deb866f9 | 2023-04-25 12:30:45 +0300 | [diff] [blame] | 214 | |
| 215 | if (hweight32(iter->flags & (MEDIA_PAD_FL_SINK | |
| 216 | MEDIA_PAD_FL_SOURCE)) != 1) { |
| 217 | ret = -EINVAL; |
| 218 | break; |
| 219 | } |
| 220 | |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 221 | if (mdev) |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 222 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
Jacopo Mondi | 8db465f | 2022-08-31 16:13:28 +0200 | [diff] [blame] | 223 | &iter->graph_obj); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 224 | } |
| 225 | |
Sakari Ailus | deb866f9 | 2023-04-25 12:30:45 +0300 | [diff] [blame] | 226 | if (ret && mdev) { |
| 227 | media_entity_for_each_pad(entity, iter) |
| 228 | media_gobj_destroy(&iter->graph_obj); |
| 229 | } |
| 230 | |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 231 | if (mdev) |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 232 | mutex_unlock(&mdev->graph_mutex); |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 233 | |
Sakari Ailus | deb866f9 | 2023-04-25 12:30:45 +0300 | [diff] [blame] | 234 | return ret; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 235 | } |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 236 | EXPORT_SYMBOL_GPL(media_entity_pads_init); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 237 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 238 | /* ----------------------------------------------------------------------------- |
| 239 | * Graph traversal |
| 240 | */ |
| 241 | |
Laurent Pinchart | 1ec0612 | 2022-01-14 01:26:26 +0100 | [diff] [blame] | 242 | /** |
| 243 | * media_entity_has_pad_interdep - Check interdependency between two pads |
| 244 | * |
| 245 | * @entity: The entity |
| 246 | * @pad0: The first pad index |
| 247 | * @pad1: The second pad index |
| 248 | * |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 249 | * This function checks the interdependency inside the entity between @pad0 |
| 250 | * and @pad1. If two pads are interdependent they are part of the same pipeline |
| 251 | * and enabling one of the pads means that the other pad will become "locked" |
| 252 | * and doesn't allow configuration changes. |
| 253 | * |
Tomi Valkeinen | 5b4f9a7 | 2022-08-31 16:13:38 +0200 | [diff] [blame] | 254 | * This function uses the &media_entity_operations.has_pad_interdep() operation |
| 255 | * to check the dependency inside the entity between @pad0 and @pad1. If the |
| 256 | * has_pad_interdep operation is not implemented, all pads of the entity are |
| 257 | * considered to be interdependent. |
Laurent Pinchart | 1ec0612 | 2022-01-14 01:26:26 +0100 | [diff] [blame] | 258 | * |
| 259 | * One of @pad0 and @pad1 must be a sink pad and the other one a source pad. |
| 260 | * The function returns false if both pads are sinks or sources. |
| 261 | * |
| 262 | * The caller must hold entity->graph_obj.mdev->mutex. |
| 263 | * |
| 264 | * Return: true if the pads are connected internally and false otherwise. |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 265 | */ |
| 266 | static bool media_entity_has_pad_interdep(struct media_entity *entity, |
| 267 | unsigned int pad0, unsigned int pad1) |
| 268 | { |
| 269 | if (pad0 >= entity->num_pads || pad1 >= entity->num_pads) |
| 270 | return false; |
| 271 | |
| 272 | if (entity->pads[pad0].flags & entity->pads[pad1].flags & |
| 273 | (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) |
| 274 | return false; |
| 275 | |
Tomi Valkeinen | 5b4f9a7 | 2022-08-31 16:13:38 +0200 | [diff] [blame] | 276 | if (!entity->ops || !entity->ops->has_pad_interdep) |
| 277 | return true; |
| 278 | |
| 279 | return entity->ops->has_pad_interdep(entity, pad0, pad1); |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 282 | static struct media_entity * |
| 283 | media_entity_other(struct media_entity *entity, struct media_link *link) |
| 284 | { |
| 285 | if (link->source->entity == entity) |
| 286 | return link->sink->entity; |
| 287 | else |
| 288 | return link->source->entity; |
| 289 | } |
| 290 | |
| 291 | /* push an entity to traversal stack */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 292 | static void stack_push(struct media_graph *graph, |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 293 | struct media_entity *entity) |
| 294 | { |
| 295 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { |
| 296 | WARN_ON(1); |
| 297 | return; |
| 298 | } |
| 299 | graph->top++; |
Javier Martinez Canillas | 313895f | 2015-12-11 15:16:36 -0200 | [diff] [blame] | 300 | graph->stack[graph->top].link = entity->links.next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 301 | graph->stack[graph->top].entity = entity; |
| 302 | } |
| 303 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 304 | static struct media_entity *stack_pop(struct media_graph *graph) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 305 | { |
| 306 | struct media_entity *entity; |
| 307 | |
| 308 | entity = graph->stack[graph->top].entity; |
| 309 | graph->top--; |
| 310 | |
| 311 | return entity; |
| 312 | } |
| 313 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 314 | #define link_top(en) ((en)->stack[(en)->top].link) |
| 315 | #define stack_top(en) ((en)->stack[(en)->top].entity) |
| 316 | |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 317 | /** |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 318 | * media_graph_walk_init - Allocate resources for graph walk |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 319 | * @graph: Media graph structure that will be used to walk the graph |
| 320 | * @mdev: Media device |
| 321 | * |
| 322 | * Reserve resources for graph walk in media device's current |
| 323 | * state. The memory must be released using |
Miaoqian Lin | 25ba644 | 2022-12-17 15:31:13 +0100 | [diff] [blame] | 324 | * media_graph_walk_cleanup(). |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 325 | * |
| 326 | * Returns error on failure, zero on success. |
| 327 | */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 328 | __must_check int media_graph_walk_init( |
| 329 | struct media_graph *graph, struct media_device *mdev) |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 330 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 331 | return media_entity_enum_init(&graph->ent_enum, mdev); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 332 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(media_graph_walk_init); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 334 | |
| 335 | /** |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 336 | * media_graph_walk_cleanup - Release resources related to graph walking |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 337 | * @graph: Media graph structure that was used to walk the graph |
| 338 | */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 339 | void media_graph_walk_cleanup(struct media_graph *graph) |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 340 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 341 | media_entity_enum_cleanup(&graph->ent_enum); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 342 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(media_graph_walk_cleanup); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 344 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 345 | void media_graph_walk_start(struct media_graph *graph, |
| 346 | struct media_entity *entity) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 347 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 348 | media_entity_enum_zero(&graph->ent_enum); |
| 349 | media_entity_enum_set(&graph->ent_enum, entity); |
| 350 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 351 | graph->top = 0; |
| 352 | graph->stack[graph->top].entity = NULL; |
| 353 | stack_push(graph, entity); |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 354 | dev_dbg(entity->graph_obj.mdev->dev, |
| 355 | "begin graph walk at '%s'\n", entity->name); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 356 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 357 | EXPORT_SYMBOL_GPL(media_graph_walk_start); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 358 | |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 359 | static void media_graph_walk_iter(struct media_graph *graph) |
| 360 | { |
| 361 | struct media_entity *entity = stack_top(graph); |
| 362 | struct media_link *link; |
| 363 | struct media_entity *next; |
| 364 | |
| 365 | link = list_entry(link_top(graph), typeof(*link), list); |
| 366 | |
Daniel Scally | af3ed78 | 2022-03-02 22:03:00 +0000 | [diff] [blame] | 367 | /* If the link is not a data link, don't follow it */ |
| 368 | if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) { |
| 369 | link_top(graph) = link_top(graph)->next; |
| 370 | return; |
| 371 | } |
| 372 | |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 373 | /* The link is not enabled so we do not follow. */ |
| 374 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 375 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 376 | dev_dbg(entity->graph_obj.mdev->dev, |
| 377 | "walk: skipping disabled link '%s':%u -> '%s':%u\n", |
| 378 | link->source->entity->name, link->source->index, |
| 379 | link->sink->entity->name, link->sink->index); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
Sebastian Fricke | b07006f | 2021-04-17 15:34:38 +0200 | [diff] [blame] | 383 | /* Get the entity at the other end of the link. */ |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 384 | next = media_entity_other(entity, link); |
| 385 | |
| 386 | /* Has the entity already been visited? */ |
| 387 | if (media_entity_enum_test_and_set(&graph->ent_enum, next)) { |
| 388 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 389 | dev_dbg(entity->graph_obj.mdev->dev, |
| 390 | "walk: skipping entity '%s' (already seen)\n", |
| 391 | next->name); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
| 395 | /* Push the new entity to stack and start over. */ |
| 396 | link_top(graph) = link_top(graph)->next; |
| 397 | stack_push(graph, next); |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 398 | dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n", |
| 399 | next->name); |
Sakari Ailus | 4ebddb7 | 2021-03-12 10:05:44 +0100 | [diff] [blame] | 400 | lockdep_assert_held(&entity->graph_obj.mdev->graph_mutex); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 401 | } |
| 402 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 403 | struct media_entity *media_graph_walk_next(struct media_graph *graph) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 404 | { |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 405 | struct media_entity *entity; |
| 406 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 407 | if (stack_top(graph) == NULL) |
| 408 | return NULL; |
| 409 | |
| 410 | /* |
| 411 | * Depth first search. Push entity to stack and continue from |
| 412 | * top of the stack until no more entities on the level can be |
| 413 | * found. |
| 414 | */ |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 415 | while (link_top(graph) != &stack_top(graph)->links) |
| 416 | media_graph_walk_iter(graph); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 417 | |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 418 | entity = stack_pop(graph); |
| 419 | dev_dbg(entity->graph_obj.mdev->dev, |
| 420 | "walk: returning entity '%s'\n", entity->name); |
| 421 | |
| 422 | return entity; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 423 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 424 | EXPORT_SYMBOL_GPL(media_graph_walk_next); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 425 | |
| 426 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 427 | * Pipeline management |
| 428 | */ |
| 429 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 430 | /* |
| 431 | * The pipeline traversal stack stores pads that are reached during graph |
| 432 | * traversal, with a list of links to be visited to continue the traversal. |
| 433 | * When a new pad is reached, an entry is pushed on the top of the stack and |
| 434 | * points to the incoming pad and the first link of the entity. |
| 435 | * |
| 436 | * To find further pads in the pipeline, the traversal algorithm follows |
| 437 | * internal pad dependencies in the entity, and then links in the graph. It |
| 438 | * does so by iterating over all links of the entity, and following enabled |
| 439 | * links that originate from a pad that is internally connected to the incoming |
| 440 | * pad, as reported by the media_entity_has_pad_interdep() function. |
| 441 | */ |
| 442 | |
| 443 | /** |
| 444 | * struct media_pipeline_walk_entry - Entry in the pipeline traversal stack |
| 445 | * |
| 446 | * @pad: The media pad being visited |
| 447 | * @links: Links left to be visited |
| 448 | */ |
| 449 | struct media_pipeline_walk_entry { |
| 450 | struct media_pad *pad; |
| 451 | struct list_head *links; |
| 452 | }; |
| 453 | |
| 454 | /** |
| 455 | * struct media_pipeline_walk - State used by the media pipeline traversal |
| 456 | * algorithm |
| 457 | * |
| 458 | * @mdev: The media device |
| 459 | * @stack: Depth-first search stack |
| 460 | * @stack.size: Number of allocated entries in @stack.entries |
| 461 | * @stack.top: Index of the top stack entry (-1 if the stack is empty) |
| 462 | * @stack.entries: Stack entries |
| 463 | */ |
| 464 | struct media_pipeline_walk { |
| 465 | struct media_device *mdev; |
| 466 | |
| 467 | struct { |
| 468 | unsigned int size; |
| 469 | int top; |
| 470 | struct media_pipeline_walk_entry *entries; |
| 471 | } stack; |
| 472 | }; |
| 473 | |
| 474 | #define MEDIA_PIPELINE_STACK_GROW_STEP 16 |
| 475 | |
| 476 | static struct media_pipeline_walk_entry * |
| 477 | media_pipeline_walk_top(struct media_pipeline_walk *walk) |
| 478 | { |
| 479 | return &walk->stack.entries[walk->stack.top]; |
| 480 | } |
| 481 | |
| 482 | static bool media_pipeline_walk_empty(struct media_pipeline_walk *walk) |
| 483 | { |
| 484 | return walk->stack.top == -1; |
| 485 | } |
| 486 | |
| 487 | /* Increase the stack size by MEDIA_PIPELINE_STACK_GROW_STEP elements. */ |
| 488 | static int media_pipeline_walk_resize(struct media_pipeline_walk *walk) |
| 489 | { |
| 490 | struct media_pipeline_walk_entry *entries; |
| 491 | unsigned int new_size; |
| 492 | |
| 493 | /* Safety check, to avoid stack overflows in case of bugs. */ |
| 494 | if (walk->stack.size >= 256) |
| 495 | return -E2BIG; |
| 496 | |
| 497 | new_size = walk->stack.size + MEDIA_PIPELINE_STACK_GROW_STEP; |
| 498 | |
| 499 | entries = krealloc(walk->stack.entries, |
| 500 | new_size * sizeof(*walk->stack.entries), |
| 501 | GFP_KERNEL); |
| 502 | if (!entries) |
| 503 | return -ENOMEM; |
| 504 | |
| 505 | walk->stack.entries = entries; |
| 506 | walk->stack.size = new_size; |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* Push a new entry on the stack. */ |
| 512 | static int media_pipeline_walk_push(struct media_pipeline_walk *walk, |
| 513 | struct media_pad *pad) |
| 514 | { |
| 515 | struct media_pipeline_walk_entry *entry; |
| 516 | int ret; |
| 517 | |
| 518 | if (walk->stack.top + 1 >= walk->stack.size) { |
| 519 | ret = media_pipeline_walk_resize(walk); |
| 520 | if (ret) |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | walk->stack.top++; |
| 525 | entry = media_pipeline_walk_top(walk); |
| 526 | entry->pad = pad; |
| 527 | entry->links = pad->entity->links.next; |
| 528 | |
| 529 | dev_dbg(walk->mdev->dev, |
| 530 | "media pipeline: pushed entry %u: '%s':%u\n", |
| 531 | walk->stack.top, pad->entity->name, pad->index); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Move the top entry link cursor to the next link. If all links of the entry |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 538 | * have been visited, pop the entry itself. Return true if the entry has been |
| 539 | * popped. |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 540 | */ |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 541 | static bool media_pipeline_walk_pop(struct media_pipeline_walk *walk) |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 542 | { |
| 543 | struct media_pipeline_walk_entry *entry; |
| 544 | |
| 545 | if (WARN_ON(walk->stack.top < 0)) |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 546 | return false; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 547 | |
| 548 | entry = media_pipeline_walk_top(walk); |
| 549 | |
| 550 | if (entry->links->next == &entry->pad->entity->links) { |
| 551 | dev_dbg(walk->mdev->dev, |
| 552 | "media pipeline: entry %u has no more links, popping\n", |
| 553 | walk->stack.top); |
| 554 | |
| 555 | walk->stack.top--; |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 556 | return true; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | entry->links = entry->links->next; |
| 560 | |
| 561 | dev_dbg(walk->mdev->dev, |
| 562 | "media pipeline: moved entry %u to next link\n", |
| 563 | walk->stack.top); |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 564 | |
| 565 | return false; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* Free all memory allocated while walking the pipeline. */ |
| 569 | static void media_pipeline_walk_destroy(struct media_pipeline_walk *walk) |
| 570 | { |
| 571 | kfree(walk->stack.entries); |
| 572 | } |
| 573 | |
| 574 | /* Add a pad to the pipeline and push it to the stack. */ |
| 575 | static int media_pipeline_add_pad(struct media_pipeline *pipe, |
| 576 | struct media_pipeline_walk *walk, |
| 577 | struct media_pad *pad) |
| 578 | { |
| 579 | struct media_pipeline_pad *ppad; |
| 580 | |
| 581 | list_for_each_entry(ppad, &pipe->pads, list) { |
| 582 | if (ppad->pad == pad) { |
| 583 | dev_dbg(pad->graph_obj.mdev->dev, |
| 584 | "media pipeline: already contains pad '%s':%u\n", |
| 585 | pad->entity->name, pad->index); |
| 586 | return 0; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | ppad = kzalloc(sizeof(*ppad), GFP_KERNEL); |
| 591 | if (!ppad) |
| 592 | return -ENOMEM; |
| 593 | |
| 594 | ppad->pipe = pipe; |
| 595 | ppad->pad = pad; |
| 596 | |
| 597 | list_add_tail(&ppad->list, &pipe->pads); |
| 598 | |
| 599 | dev_dbg(pad->graph_obj.mdev->dev, |
| 600 | "media pipeline: added pad '%s':%u\n", |
| 601 | pad->entity->name, pad->index); |
| 602 | |
| 603 | return media_pipeline_walk_push(walk, pad); |
| 604 | } |
| 605 | |
| 606 | /* Explore the next link of the entity at the top of the stack. */ |
| 607 | static int media_pipeline_explore_next_link(struct media_pipeline *pipe, |
| 608 | struct media_pipeline_walk *walk) |
| 609 | { |
| 610 | struct media_pipeline_walk_entry *entry = media_pipeline_walk_top(walk); |
Laurent Pinchart | 9ec9109 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 611 | struct media_pad *origin; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 612 | struct media_link *link; |
| 613 | struct media_pad *local; |
| 614 | struct media_pad *remote; |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 615 | bool last_link; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 616 | int ret; |
| 617 | |
Laurent Pinchart | 9ec9109 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 618 | origin = entry->pad; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 619 | link = list_entry(entry->links, typeof(*link), list); |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 620 | last_link = media_pipeline_walk_pop(walk); |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 621 | |
Tomi Valkeinen | 8a9d420 | 2024-03-18 11:50:59 +0200 | [diff] [blame] | 622 | if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) != MEDIA_LNK_FL_DATA_LINK) { |
| 623 | dev_dbg(walk->mdev->dev, |
| 624 | "media pipeline: skipping link (not data-link)\n"); |
| 625 | return 0; |
| 626 | } |
| 627 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 628 | dev_dbg(walk->mdev->dev, |
| 629 | "media pipeline: exploring link '%s':%u -> '%s':%u\n", |
| 630 | link->source->entity->name, link->source->index, |
| 631 | link->sink->entity->name, link->sink->index); |
| 632 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 633 | /* Get the local pad and remote pad. */ |
Laurent Pinchart | 9ec9109 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 634 | if (link->source->entity == origin->entity) { |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 635 | local = link->source; |
| 636 | remote = link->sink; |
| 637 | } else { |
| 638 | local = link->sink; |
| 639 | remote = link->source; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Skip links that originate from a different pad than the incoming pad |
| 644 | * that is not connected internally in the entity to the incoming pad. |
| 645 | */ |
Laurent Pinchart | 9ec9109 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 646 | if (origin != local && |
| 647 | !media_entity_has_pad_interdep(origin->entity, origin->index, |
| 648 | local->index)) { |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 649 | dev_dbg(walk->mdev->dev, |
| 650 | "media pipeline: skipping link (no route)\n"); |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 651 | goto done; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* |
Laurent Pinchart | 78f0daa0 | 2024-01-14 15:55:40 +0200 | [diff] [blame] | 655 | * Add the local pad of the link to the pipeline and push it to the |
| 656 | * stack, if not already present. |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 657 | */ |
| 658 | ret = media_pipeline_add_pad(pipe, walk, local); |
| 659 | if (ret) |
| 660 | return ret; |
| 661 | |
Laurent Pinchart | 78f0daa0 | 2024-01-14 15:55:40 +0200 | [diff] [blame] | 662 | /* Similarly, add the remote pad, but only if the link is enabled. */ |
| 663 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 664 | dev_dbg(walk->mdev->dev, |
| 665 | "media pipeline: skipping link (disabled)\n"); |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 666 | goto done; |
Laurent Pinchart | 78f0daa0 | 2024-01-14 15:55:40 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 669 | ret = media_pipeline_add_pad(pipe, walk, remote); |
| 670 | if (ret) |
| 671 | return ret; |
| 672 | |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 673 | done: |
| 674 | /* |
| 675 | * If we're done iterating over links, iterate over pads of the entity. |
| 676 | * This is necessary to discover pads that are not connected with any |
| 677 | * link. Those are dead ends from a pipeline exploration point of view, |
| 678 | * but are still part of the pipeline and need to be added to enable |
| 679 | * proper validation. |
| 680 | */ |
| 681 | if (!last_link) |
| 682 | return 0; |
| 683 | |
| 684 | dev_dbg(walk->mdev->dev, |
| 685 | "media pipeline: adding unconnected pads of '%s'\n", |
| 686 | local->entity->name); |
| 687 | |
| 688 | media_entity_for_each_pad(origin->entity, local) { |
| 689 | /* |
| 690 | * Skip the origin pad (already handled), pad that have links |
| 691 | * (already discovered through iterating over links) and pads |
| 692 | * not internally connected. |
| 693 | */ |
| 694 | if (origin == local || !local->num_links || |
| 695 | !media_entity_has_pad_interdep(origin->entity, origin->index, |
| 696 | local->index)) |
| 697 | continue; |
| 698 | |
| 699 | ret = media_pipeline_add_pad(pipe, walk, local); |
| 700 | if (ret) |
| 701 | return ret; |
| 702 | } |
| 703 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static void media_pipeline_cleanup(struct media_pipeline *pipe) |
| 708 | { |
| 709 | while (!list_empty(&pipe->pads)) { |
| 710 | struct media_pipeline_pad *ppad; |
| 711 | |
| 712 | ppad = list_first_entry(&pipe->pads, typeof(*ppad), list); |
| 713 | list_del(&ppad->list); |
| 714 | kfree(ppad); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static int media_pipeline_populate(struct media_pipeline *pipe, |
| 719 | struct media_pad *pad) |
| 720 | { |
| 721 | struct media_pipeline_walk walk = { }; |
| 722 | struct media_pipeline_pad *ppad; |
| 723 | int ret; |
| 724 | |
| 725 | /* |
| 726 | * Populate the media pipeline by walking the media graph, starting |
| 727 | * from @pad. |
| 728 | */ |
| 729 | INIT_LIST_HEAD(&pipe->pads); |
| 730 | pipe->mdev = pad->graph_obj.mdev; |
| 731 | |
| 732 | walk.mdev = pipe->mdev; |
| 733 | walk.stack.top = -1; |
| 734 | ret = media_pipeline_add_pad(pipe, &walk, pad); |
| 735 | if (ret) |
| 736 | goto done; |
| 737 | |
| 738 | /* |
| 739 | * Use a depth-first search algorithm: as long as the stack is not |
| 740 | * empty, explore the next link of the top entry. The |
| 741 | * media_pipeline_explore_next_link() function will either move to the |
| 742 | * next link, pop the entry if fully visited, or add new entries on |
| 743 | * top. |
| 744 | */ |
| 745 | while (!media_pipeline_walk_empty(&walk)) { |
| 746 | ret = media_pipeline_explore_next_link(pipe, &walk); |
| 747 | if (ret) |
| 748 | goto done; |
| 749 | } |
| 750 | |
| 751 | dev_dbg(pad->graph_obj.mdev->dev, |
| 752 | "media pipeline populated, found pads:\n"); |
| 753 | |
| 754 | list_for_each_entry(ppad, &pipe->pads, list) |
| 755 | dev_dbg(pad->graph_obj.mdev->dev, "- '%s':%u\n", |
| 756 | ppad->pad->entity->name, ppad->pad->index); |
| 757 | |
| 758 | WARN_ON(walk.stack.top != -1); |
| 759 | |
| 760 | ret = 0; |
| 761 | |
| 762 | done: |
| 763 | media_pipeline_walk_destroy(&walk); |
| 764 | |
| 765 | if (ret) |
| 766 | media_pipeline_cleanup(pipe); |
| 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 771 | __must_check int __media_pipeline_start(struct media_pad *pad, |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 772 | struct media_pipeline *pipe) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 773 | { |
Laurent Pinchart | a967a3a | 2022-12-12 14:25:04 +0100 | [diff] [blame] | 774 | struct media_device *mdev = pad->graph_obj.mdev; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 775 | struct media_pipeline_pad *err_ppad; |
| 776 | struct media_pipeline_pad *ppad; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 777 | int ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 778 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 779 | lockdep_assert_held(&mdev->graph_mutex); |
| 780 | |
| 781 | /* |
Laurent Pinchart | b516354 | 2022-12-12 14:25:05 +0100 | [diff] [blame] | 782 | * If the pad is already part of a pipeline, that pipeline must be the |
| 783 | * same as the pipe given to media_pipeline_start(). |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 784 | */ |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 785 | if (WARN_ON(pad->pipe && pad->pipe != pipe)) |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 786 | return -EINVAL; |
| 787 | |
| 788 | /* |
| 789 | * If the pipeline has already been started, it is guaranteed to be |
| 790 | * valid, so just increase the start count. |
| 791 | */ |
Tomi Valkeinen | b7319e2 | 2022-08-31 16:13:27 +0200 | [diff] [blame] | 792 | if (pipe->start_count) { |
| 793 | pipe->start_count++; |
Laurent Pinchart | 3056a8e | 2022-01-13 17:00:42 +0200 | [diff] [blame] | 794 | return 0; |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 797 | /* |
| 798 | * Populate the pipeline. This populates the media_pipeline pads list |
| 799 | * with media_pipeline_pad instances for each pad found during graph |
| 800 | * walk. |
| 801 | */ |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 802 | ret = media_pipeline_populate(pipe, pad); |
Laurent Pinchart | 3056a8e | 2022-01-13 17:00:42 +0200 | [diff] [blame] | 803 | if (ret) |
| 804 | return ret; |
| 805 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 806 | /* |
| 807 | * Now that all the pads in the pipeline have been gathered, perform |
| 808 | * the validation steps. |
| 809 | */ |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 810 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 811 | list_for_each_entry(ppad, &pipe->pads, list) { |
| 812 | struct media_pad *pad = ppad->pad; |
| 813 | struct media_entity *entity = pad->entity; |
| 814 | bool has_enabled_link = false; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 815 | struct media_link *link; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 816 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 817 | dev_dbg(mdev->dev, "Validating pad '%s':%u\n", pad->entity->name, |
| 818 | pad->index); |
| 819 | |
| 820 | /* |
| 821 | * 1. Ensure that the pad doesn't already belong to a different |
| 822 | * pipeline. |
| 823 | */ |
| 824 | if (pad->pipe) { |
| 825 | dev_dbg(mdev->dev, "Failed to start pipeline: pad '%s':%u busy\n", |
| 826 | pad->entity->name, pad->index); |
Sakari Ailus | 8aaf62b | 2015-11-29 17:20:02 -0200 | [diff] [blame] | 827 | ret = -EBUSY; |
| 828 | goto error; |
| 829 | } |
| 830 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 831 | /* |
| 832 | * 2. Validate all active links whose sink is the current pad. |
| 833 | * Validation of the source pads is performed in the context of |
| 834 | * the connected sink pad to avoid duplicating checks. |
| 835 | */ |
Daniel Scally | 549e622 | 2022-07-07 23:47:33 +0100 | [diff] [blame] | 836 | for_each_media_entity_data_link(entity, link) { |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 837 | /* Skip links unrelated to the current pad. */ |
| 838 | if (link->sink != pad && link->source != pad) |
| 839 | continue; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 840 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 841 | /* Record if the pad has links and enabled links. */ |
| 842 | if (link->flags & MEDIA_LNK_FL_ENABLED) |
| 843 | has_enabled_link = true; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 844 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 845 | /* |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 846 | * Validate the link if it's enabled and has the |
| 847 | * current pad as its sink. |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 848 | */ |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 849 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 850 | continue; |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 851 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 852 | if (link->sink != pad) |
| 853 | continue; |
| 854 | |
| 855 | if (!entity->ops || !entity->ops->link_validate) |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 856 | continue; |
| 857 | |
| 858 | ret = entity->ops->link_validate(link); |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 859 | if (ret) { |
| 860 | dev_dbg(mdev->dev, |
| 861 | "Link '%s':%u -> '%s':%u failed validation: %d\n", |
Sakari Ailus | 823ea2a | 2015-02-12 11:43:11 -0200 | [diff] [blame] | 862 | link->source->entity->name, |
| 863 | link->source->index, |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 864 | link->sink->entity->name, |
| 865 | link->sink->index, ret); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 866 | goto error; |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 867 | } |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 868 | |
| 869 | dev_dbg(mdev->dev, |
| 870 | "Link '%s':%u -> '%s':%u is valid\n", |
| 871 | link->source->entity->name, |
| 872 | link->source->index, |
| 873 | link->sink->entity->name, |
| 874 | link->sink->index); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 875 | } |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 876 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 877 | /* |
| 878 | * 3. If the pad has the MEDIA_PAD_FL_MUST_CONNECT flag set, |
| 879 | * ensure that it has either no link or an enabled link. |
| 880 | */ |
Laurent Pinchart | b3decc5 | 2024-01-15 01:04:52 +0200 | [diff] [blame] | 881 | if ((pad->flags & MEDIA_PAD_FL_MUST_CONNECT) && |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 882 | !has_enabled_link) { |
| 883 | dev_dbg(mdev->dev, |
| 884 | "Pad '%s':%u must be connected by an enabled link\n", |
| 885 | pad->entity->name, pad->index); |
Helen Mae Koike Fornazier | 47dfdb3 | 2015-06-28 16:14:10 -0300 | [diff] [blame] | 886 | ret = -ENOLINK; |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 887 | goto error; |
| 888 | } |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 889 | |
| 890 | /* Validation passed, store the pipe pointer in the pad. */ |
| 891 | pad->pipe = pipe; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 892 | } |
| 893 | |
Tomi Valkeinen | b7319e2 | 2022-08-31 16:13:27 +0200 | [diff] [blame] | 894 | pipe->start_count++; |
Laurent Pinchart | 3056a8e | 2022-01-13 17:00:42 +0200 | [diff] [blame] | 895 | |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 896 | return 0; |
| 897 | |
| 898 | error: |
| 899 | /* |
| 900 | * Link validation on graph failed. We revert what we did and |
| 901 | * return the error. |
| 902 | */ |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 903 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 904 | list_for_each_entry(err_ppad, &pipe->pads, list) { |
| 905 | if (err_ppad == ppad) |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 906 | break; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 907 | |
| 908 | err_ppad->pad->pipe = NULL; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 909 | } |
| 910 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 911 | media_pipeline_cleanup(pipe); |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 912 | |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 913 | return ret; |
| 914 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 915 | EXPORT_SYMBOL_GPL(__media_pipeline_start); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 916 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 917 | __must_check int media_pipeline_start(struct media_pad *pad, |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 918 | struct media_pipeline *pipe) |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 919 | { |
Laurent Pinchart | a967a3a | 2022-12-12 14:25:04 +0100 | [diff] [blame] | 920 | struct media_device *mdev = pad->graph_obj.mdev; |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 921 | int ret; |
| 922 | |
| 923 | mutex_lock(&mdev->graph_mutex); |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 924 | ret = __media_pipeline_start(pad, pipe); |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 925 | mutex_unlock(&mdev->graph_mutex); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 926 | return ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 927 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 928 | EXPORT_SYMBOL_GPL(media_pipeline_start); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 929 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 930 | void __media_pipeline_stop(struct media_pad *pad) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 931 | { |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 932 | struct media_pipeline *pipe = pad->pipe; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 933 | struct media_pipeline_pad *ppad; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 934 | |
Kieran Bingham | 2a2599c | 2017-01-03 11:12:11 -0200 | [diff] [blame] | 935 | /* |
| 936 | * If the following check fails, the driver has performed an |
| 937 | * unbalanced call to media_pipeline_stop() |
| 938 | */ |
| 939 | if (WARN_ON(!pipe)) |
| 940 | return; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 941 | |
Tomi Valkeinen | b7319e2 | 2022-08-31 16:13:27 +0200 | [diff] [blame] | 942 | if (--pipe->start_count) |
Laurent Pinchart | 3056a8e | 2022-01-13 17:00:42 +0200 | [diff] [blame] | 943 | return; |
| 944 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 945 | list_for_each_entry(ppad, &pipe->pads, list) |
| 946 | ppad->pad->pipe = NULL; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 947 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 948 | media_pipeline_cleanup(pipe); |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 949 | |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 950 | if (pipe->allocated) |
| 951 | kfree(pipe); |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 952 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 953 | EXPORT_SYMBOL_GPL(__media_pipeline_stop); |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 954 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 955 | void media_pipeline_stop(struct media_pad *pad) |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 956 | { |
Laurent Pinchart | a967a3a | 2022-12-12 14:25:04 +0100 | [diff] [blame] | 957 | struct media_device *mdev = pad->graph_obj.mdev; |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 958 | |
| 959 | mutex_lock(&mdev->graph_mutex); |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 960 | __media_pipeline_stop(pad); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 961 | mutex_unlock(&mdev->graph_mutex); |
| 962 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 963 | EXPORT_SYMBOL_GPL(media_pipeline_stop); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 964 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 965 | __must_check int media_pipeline_alloc_start(struct media_pad *pad) |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 966 | { |
Laurent Pinchart | a967a3a | 2022-12-12 14:25:04 +0100 | [diff] [blame] | 967 | struct media_device *mdev = pad->graph_obj.mdev; |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 968 | struct media_pipeline *new_pipe = NULL; |
| 969 | struct media_pipeline *pipe; |
| 970 | int ret; |
| 971 | |
| 972 | mutex_lock(&mdev->graph_mutex); |
| 973 | |
| 974 | /* |
Laurent Pinchart | b516354 | 2022-12-12 14:25:05 +0100 | [diff] [blame] | 975 | * Is the pad already part of a pipeline? If not, we need to allocate |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 976 | * a pipe. |
| 977 | */ |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 978 | pipe = media_pad_pipeline(pad); |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 979 | if (!pipe) { |
| 980 | new_pipe = kzalloc(sizeof(*new_pipe), GFP_KERNEL); |
| 981 | if (!new_pipe) { |
| 982 | ret = -ENOMEM; |
| 983 | goto out; |
| 984 | } |
| 985 | |
| 986 | pipe = new_pipe; |
| 987 | pipe->allocated = true; |
| 988 | } |
| 989 | |
Tomi Valkeinen | 9e3576a | 2022-08-31 16:13:39 +0200 | [diff] [blame] | 990 | ret = __media_pipeline_start(pad, pipe); |
Tomi Valkeinen | d9f4434 | 2022-08-31 16:13:35 +0200 | [diff] [blame] | 991 | if (ret) |
| 992 | kfree(new_pipe); |
| 993 | |
| 994 | out: |
| 995 | mutex_unlock(&mdev->graph_mutex); |
| 996 | |
| 997 | return ret; |
| 998 | } |
| 999 | EXPORT_SYMBOL_GPL(media_pipeline_alloc_start); |
| 1000 | |
Laurent Pinchart | d10ac51 | 2022-12-21 10:33:37 +0100 | [diff] [blame] | 1001 | struct media_pad * |
| 1002 | __media_pipeline_pad_iter_next(struct media_pipeline *pipe, |
| 1003 | struct media_pipeline_pad_iter *iter, |
| 1004 | struct media_pad *pad) |
| 1005 | { |
| 1006 | if (!pad) |
| 1007 | iter->cursor = pipe->pads.next; |
| 1008 | |
| 1009 | if (iter->cursor == &pipe->pads) |
| 1010 | return NULL; |
| 1011 | |
| 1012 | pad = list_entry(iter->cursor, struct media_pipeline_pad, list)->pad; |
| 1013 | iter->cursor = iter->cursor->next; |
| 1014 | |
| 1015 | return pad; |
| 1016 | } |
| 1017 | EXPORT_SYMBOL_GPL(__media_pipeline_pad_iter_next); |
| 1018 | |
Laurent Pinchart | eac564d | 2022-12-21 10:33:38 +0100 | [diff] [blame] | 1019 | int media_pipeline_entity_iter_init(struct media_pipeline *pipe, |
| 1020 | struct media_pipeline_entity_iter *iter) |
| 1021 | { |
| 1022 | return media_entity_enum_init(&iter->ent_enum, pipe->mdev); |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_init); |
| 1025 | |
| 1026 | void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter) |
| 1027 | { |
| 1028 | media_entity_enum_cleanup(&iter->ent_enum); |
| 1029 | } |
| 1030 | EXPORT_SYMBOL_GPL(media_pipeline_entity_iter_cleanup); |
| 1031 | |
| 1032 | struct media_entity * |
| 1033 | __media_pipeline_entity_iter_next(struct media_pipeline *pipe, |
| 1034 | struct media_pipeline_entity_iter *iter, |
| 1035 | struct media_entity *entity) |
| 1036 | { |
| 1037 | if (!entity) |
| 1038 | iter->cursor = pipe->pads.next; |
| 1039 | |
| 1040 | while (iter->cursor != &pipe->pads) { |
| 1041 | struct media_pipeline_pad *ppad; |
| 1042 | struct media_entity *entity; |
| 1043 | |
| 1044 | ppad = list_entry(iter->cursor, struct media_pipeline_pad, list); |
| 1045 | entity = ppad->pad->entity; |
| 1046 | iter->cursor = iter->cursor->next; |
| 1047 | |
| 1048 | if (!media_entity_enum_test_and_set(&iter->ent_enum, entity)) |
| 1049 | return entity; |
| 1050 | } |
| 1051 | |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | EXPORT_SYMBOL_GPL(__media_pipeline_entity_iter_next); |
| 1055 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 1056 | /* ----------------------------------------------------------------------------- |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 1057 | * Links management |
| 1058 | */ |
| 1059 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 1060 | static struct media_link *media_add_link(struct list_head *head) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1061 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1062 | struct media_link *link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1063 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1064 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1065 | if (link == NULL) |
| 1066 | return NULL; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1067 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 1068 | list_add_tail(&link->list, head); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1069 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1070 | return link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1071 | } |
| 1072 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1073 | static void __media_entity_remove_link(struct media_entity *entity, |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1074 | struct media_link *link) |
| 1075 | { |
| 1076 | struct media_link *rlink, *tmp; |
| 1077 | struct media_entity *remote; |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1078 | |
Yunke Cao | 6008dea | 2022-04-12 07:23:13 +0100 | [diff] [blame] | 1079 | /* Remove the reverse links for a data link. */ |
| 1080 | if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) { |
Laurent Pinchart | baeddf9 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 1081 | link->source->num_links--; |
| 1082 | link->sink->num_links--; |
| 1083 | |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1084 | if (link->source->entity == entity) |
Yunke Cao | 6008dea | 2022-04-12 07:23:13 +0100 | [diff] [blame] | 1085 | remote = link->sink->entity; |
| 1086 | else |
| 1087 | remote = link->source->entity; |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1088 | |
Yunke Cao | 6008dea | 2022-04-12 07:23:13 +0100 | [diff] [blame] | 1089 | list_for_each_entry_safe(rlink, tmp, &remote->links, list) { |
| 1090 | if (rlink != link->reverse) |
| 1091 | continue; |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1092 | |
Yunke Cao | 6008dea | 2022-04-12 07:23:13 +0100 | [diff] [blame] | 1093 | if (link->source->entity == entity) |
| 1094 | remote->num_backlinks--; |
| 1095 | |
| 1096 | /* Remove the remote link */ |
| 1097 | list_del(&rlink->list); |
| 1098 | media_gobj_destroy(&rlink->graph_obj); |
| 1099 | kfree(rlink); |
| 1100 | |
| 1101 | if (--remote->num_links == 0) |
| 1102 | break; |
| 1103 | } |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1104 | } |
Yunke Cao | 6008dea | 2022-04-12 07:23:13 +0100 | [diff] [blame] | 1105 | |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1106 | list_del(&link->list); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1107 | media_gobj_destroy(&link->graph_obj); |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 1108 | kfree(link); |
| 1109 | } |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1110 | |
Sakari Ailus | 4fd463e | 2023-04-25 13:23:00 +0300 | [diff] [blame] | 1111 | int media_get_pad_index(struct media_entity *entity, u32 pad_type, |
Mauro Carvalho Chehab | 9d6d20e | 2018-07-31 09:22:40 -0400 | [diff] [blame] | 1112 | enum media_pad_signal_type sig_type) |
| 1113 | { |
Sakari Ailus | 4fd463e | 2023-04-25 13:23:00 +0300 | [diff] [blame] | 1114 | unsigned int i; |
Mauro Carvalho Chehab | 9d6d20e | 2018-07-31 09:22:40 -0400 | [diff] [blame] | 1115 | |
| 1116 | if (!entity) |
| 1117 | return -EINVAL; |
| 1118 | |
| 1119 | for (i = 0; i < entity->num_pads; i++) { |
Sakari Ailus | 4fd463e | 2023-04-25 13:23:00 +0300 | [diff] [blame] | 1120 | if ((entity->pads[i].flags & |
| 1121 | (MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_SOURCE)) != pad_type) |
Mauro Carvalho Chehab | 9d6d20e | 2018-07-31 09:22:40 -0400 | [diff] [blame] | 1122 | continue; |
Sakari Ailus | 4fd463e | 2023-04-25 13:23:00 +0300 | [diff] [blame] | 1123 | |
Mauro Carvalho Chehab | 9d6d20e | 2018-07-31 09:22:40 -0400 | [diff] [blame] | 1124 | if (entity->pads[i].sig_type == sig_type) |
| 1125 | return i; |
| 1126 | } |
| 1127 | return -EINVAL; |
| 1128 | } |
| 1129 | EXPORT_SYMBOL_GPL(media_get_pad_index); |
| 1130 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1131 | int |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 1132 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1133 | struct media_entity *sink, u16 sink_pad, u32 flags) |
| 1134 | { |
| 1135 | struct media_link *link; |
| 1136 | struct media_link *backlink; |
| 1137 | |
Laurent Pinchart | 422f7af | 2024-01-15 00:24:12 +0200 | [diff] [blame] | 1138 | if (flags & MEDIA_LNK_FL_LINK_TYPE) |
| 1139 | return -EINVAL; |
| 1140 | |
| 1141 | flags |= MEDIA_LNK_FL_DATA_LINK; |
| 1142 | |
Hans Verkuil | a3fbc2e | 2020-02-04 19:13:13 +0100 | [diff] [blame] | 1143 | if (WARN_ON(!source || !sink) || |
| 1144 | WARN_ON(source_pad >= source->num_pads) || |
| 1145 | WARN_ON(sink_pad >= sink->num_pads)) |
| 1146 | return -EINVAL; |
| 1147 | if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE))) |
| 1148 | return -EINVAL; |
| 1149 | if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK))) |
| 1150 | return -EINVAL; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1151 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 1152 | link = media_add_link(&source->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1153 | if (link == NULL) |
| 1154 | return -ENOMEM; |
| 1155 | |
| 1156 | link->source = &source->pads[source_pad]; |
| 1157 | link->sink = &sink->pads[sink_pad]; |
Laurent Pinchart | 422f7af | 2024-01-15 00:24:12 +0200 | [diff] [blame] | 1158 | link->flags = flags; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1159 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 1160 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1161 | media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 1162 | &link->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 1163 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1164 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 1165 | * are not reported to userspace. |
| 1166 | */ |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 1167 | backlink = media_add_link(&sink->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1168 | if (backlink == NULL) { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1169 | __media_entity_remove_link(source, link); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1170 | return -ENOMEM; |
| 1171 | } |
| 1172 | |
| 1173 | backlink->source = &source->pads[source_pad]; |
| 1174 | backlink->sink = &sink->pads[sink_pad]; |
| 1175 | backlink->flags = flags; |
Mauro Carvalho Chehab | 39d1ebc6 | 2015-08-30 09:53:57 -0300 | [diff] [blame] | 1176 | backlink->is_backlink = true; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1177 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 1178 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1179 | media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 1180 | &backlink->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 1181 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1182 | link->reverse = backlink; |
| 1183 | backlink->reverse = link; |
| 1184 | |
| 1185 | sink->num_backlinks++; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1186 | sink->num_links++; |
| 1187 | source->num_links++; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1188 | |
Laurent Pinchart | baeddf9 | 2024-01-15 00:30:02 +0200 | [diff] [blame] | 1189 | link->source->num_links++; |
| 1190 | link->sink->num_links++; |
| 1191 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1192 | return 0; |
| 1193 | } |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 1194 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1195 | |
Mauro Carvalho Chehab | b01cc9c | 2015-12-30 09:45:48 -0200 | [diff] [blame] | 1196 | int media_create_pad_links(const struct media_device *mdev, |
| 1197 | const u32 source_function, |
| 1198 | struct media_entity *source, |
| 1199 | const u16 source_pad, |
| 1200 | const u32 sink_function, |
| 1201 | struct media_entity *sink, |
| 1202 | const u16 sink_pad, |
| 1203 | u32 flags, |
| 1204 | const bool allow_both_undefined) |
| 1205 | { |
| 1206 | struct media_entity *entity; |
| 1207 | unsigned function; |
| 1208 | int ret; |
| 1209 | |
| 1210 | /* Trivial case: 1:1 relation */ |
| 1211 | if (source && sink) |
| 1212 | return media_create_pad_link(source, source_pad, |
| 1213 | sink, sink_pad, flags); |
| 1214 | |
| 1215 | /* Worse case scenario: n:n relation */ |
| 1216 | if (!source && !sink) { |
| 1217 | if (!allow_both_undefined) |
| 1218 | return 0; |
| 1219 | media_device_for_each_entity(source, mdev) { |
| 1220 | if (source->function != source_function) |
| 1221 | continue; |
| 1222 | media_device_for_each_entity(sink, mdev) { |
| 1223 | if (sink->function != sink_function) |
| 1224 | continue; |
| 1225 | ret = media_create_pad_link(source, source_pad, |
| 1226 | sink, sink_pad, |
| 1227 | flags); |
| 1228 | if (ret) |
| 1229 | return ret; |
| 1230 | flags &= ~(MEDIA_LNK_FL_ENABLED | |
| 1231 | MEDIA_LNK_FL_IMMUTABLE); |
| 1232 | } |
| 1233 | } |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* Handle 1:n and n:1 cases */ |
| 1238 | if (source) |
| 1239 | function = sink_function; |
| 1240 | else |
| 1241 | function = source_function; |
| 1242 | |
| 1243 | media_device_for_each_entity(entity, mdev) { |
| 1244 | if (entity->function != function) |
| 1245 | continue; |
| 1246 | |
| 1247 | if (source) |
| 1248 | ret = media_create_pad_link(source, source_pad, |
| 1249 | entity, sink_pad, flags); |
| 1250 | else |
| 1251 | ret = media_create_pad_link(entity, source_pad, |
| 1252 | sink, sink_pad, flags); |
| 1253 | if (ret) |
| 1254 | return ret; |
| 1255 | flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); |
| 1256 | } |
| 1257 | return 0; |
| 1258 | } |
| 1259 | EXPORT_SYMBOL_GPL(media_create_pad_links); |
| 1260 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1261 | void __media_entity_remove_links(struct media_entity *entity) |
| 1262 | { |
| 1263 | struct media_link *link, *tmp; |
| 1264 | |
| 1265 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
| 1266 | __media_entity_remove_link(entity, link); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 1267 | |
| 1268 | entity->num_links = 0; |
| 1269 | entity->num_backlinks = 0; |
| 1270 | } |
| 1271 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); |
| 1272 | |
| 1273 | void media_entity_remove_links(struct media_entity *entity) |
| 1274 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1275 | struct media_device *mdev = entity->graph_obj.mdev; |
| 1276 | |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 1277 | /* Do nothing if the entity is not registered. */ |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1278 | if (mdev == NULL) |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 1279 | return; |
| 1280 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1281 | mutex_lock(&mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 1282 | __media_entity_remove_links(entity); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1283 | mutex_unlock(&mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 1284 | } |
| 1285 | EXPORT_SYMBOL_GPL(media_entity_remove_links); |
| 1286 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1287 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 1288 | { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1289 | int ret; |
| 1290 | |
| 1291 | /* Notify both entities. */ |
| 1292 | ret = media_entity_call(link->source->entity, link_setup, |
| 1293 | link->source, link->sink, flags); |
| 1294 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1295 | return ret; |
| 1296 | |
| 1297 | ret = media_entity_call(link->sink->entity, link_setup, |
| 1298 | link->sink, link->source, flags); |
| 1299 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 1300 | media_entity_call(link->source->entity, link_setup, |
| 1301 | link->source, link->sink, link->flags); |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 1305 | link->flags = flags; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1306 | link->reverse->flags = link->flags; |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1311 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 1312 | { |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 1313 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1314 | struct media_device *mdev; |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 1315 | struct media_pad *source, *sink; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1316 | int ret = -EBUSY; |
| 1317 | |
| 1318 | if (link == NULL) |
| 1319 | return -EINVAL; |
| 1320 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 1321 | /* The non-modifiable link flags must not be modified. */ |
| 1322 | if ((link->flags & ~mask) != (flags & ~mask)) |
| 1323 | return -EINVAL; |
| 1324 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1325 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 1326 | return link->flags == flags ? 0 : -EINVAL; |
| 1327 | |
| 1328 | if (link->flags == flags) |
| 1329 | return 0; |
| 1330 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 1331 | source = link->source; |
| 1332 | sink = link->sink; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1333 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 1334 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 1335 | (media_pad_is_streaming(source) || media_pad_is_streaming(sink))) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 1336 | return -EBUSY; |
| 1337 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 1338 | mdev = source->graph_obj.mdev; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1339 | |
Laurent Pinchart | 68429f5 | 2015-11-03 00:27:51 -0200 | [diff] [blame] | 1340 | if (mdev->ops && mdev->ops->link_notify) { |
| 1341 | ret = mdev->ops->link_notify(link, flags, |
| 1342 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1343 | if (ret < 0) |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | ret = __media_entity_setup_link_notify(link, flags); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1348 | |
Laurent Pinchart | 68429f5 | 2015-11-03 00:27:51 -0200 | [diff] [blame] | 1349 | if (mdev->ops && mdev->ops->link_notify) |
| 1350 | mdev->ops->link_notify(link, flags, |
| 1351 | MEDIA_DEV_NOTIFY_POST_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1352 | |
| 1353 | return ret; |
| 1354 | } |
Shuah Khan | efc7027 | 2016-02-11 21:41:23 -0200 | [diff] [blame] | 1355 | EXPORT_SYMBOL_GPL(__media_entity_setup_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1356 | |
| 1357 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 1358 | { |
| 1359 | int ret; |
| 1360 | |
Mauro Carvalho Chehab | 5c883ed | 2015-12-15 07:58:18 -0200 | [diff] [blame] | 1361 | mutex_lock(&link->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1362 | ret = __media_entity_setup_link(link, flags); |
Mauro Carvalho Chehab | 5c883ed | 2015-12-15 07:58:18 -0200 | [diff] [blame] | 1363 | mutex_unlock(&link->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1364 | |
| 1365 | return ret; |
| 1366 | } |
| 1367 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 1368 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1369 | struct media_link * |
| 1370 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 1371 | { |
| 1372 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1373 | |
Daniel Scally | 549e622 | 2022-07-07 23:47:33 +0100 | [diff] [blame] | 1374 | for_each_media_entity_data_link(source->entity, link) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1375 | if (link->source->entity == source->entity && |
| 1376 | link->source->index == source->index && |
| 1377 | link->sink->entity == sink->entity && |
| 1378 | link->sink->index == sink->index) |
| 1379 | return link; |
| 1380 | } |
| 1381 | |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 1385 | |
Laurent Pinchart | b2e4443 | 2022-06-25 18:02:24 +0100 | [diff] [blame] | 1386 | struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad) |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1387 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 1388 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1389 | |
Daniel Scally | 549e622 | 2022-07-07 23:47:33 +0100 | [diff] [blame] | 1390 | for_each_media_entity_data_link(pad->entity, link) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 1391 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 1392 | continue; |
| 1393 | |
| 1394 | if (link->source == pad) |
| 1395 | return link->sink; |
| 1396 | |
| 1397 | if (link->sink == pad) |
| 1398 | return link->source; |
| 1399 | } |
| 1400 | |
| 1401 | return NULL; |
| 1402 | |
| 1403 | } |
Laurent Pinchart | b2e4443 | 2022-06-25 18:02:24 +0100 | [diff] [blame] | 1404 | EXPORT_SYMBOL_GPL(media_pad_remote_pad_first); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1405 | |
Laurent Pinchart | 5680fe4 | 2022-06-14 20:11:09 +0100 | [diff] [blame] | 1406 | struct media_pad * |
| 1407 | media_entity_remote_pad_unique(const struct media_entity *entity, |
| 1408 | unsigned int type) |
| 1409 | { |
| 1410 | struct media_pad *pad = NULL; |
| 1411 | struct media_link *link; |
| 1412 | |
| 1413 | list_for_each_entry(link, &entity->links, list) { |
| 1414 | struct media_pad *local_pad; |
| 1415 | struct media_pad *remote_pad; |
| 1416 | |
| 1417 | if (((link->flags & MEDIA_LNK_FL_LINK_TYPE) != |
| 1418 | MEDIA_LNK_FL_DATA_LINK) || |
| 1419 | !(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 1420 | continue; |
| 1421 | |
| 1422 | if (type == MEDIA_PAD_FL_SOURCE) { |
| 1423 | local_pad = link->sink; |
| 1424 | remote_pad = link->source; |
| 1425 | } else { |
| 1426 | local_pad = link->source; |
| 1427 | remote_pad = link->sink; |
| 1428 | } |
| 1429 | |
| 1430 | if (local_pad->entity == entity) { |
| 1431 | if (pad) |
| 1432 | return ERR_PTR(-ENOTUNIQ); |
| 1433 | |
| 1434 | pad = remote_pad; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | if (!pad) |
| 1439 | return ERR_PTR(-ENOLINK); |
| 1440 | |
| 1441 | return pad; |
| 1442 | } |
| 1443 | EXPORT_SYMBOL_GPL(media_entity_remote_pad_unique); |
| 1444 | |
Laurent Pinchart | 03b2828 | 2022-06-14 20:11:10 +0100 | [diff] [blame] | 1445 | struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad) |
| 1446 | { |
| 1447 | struct media_pad *found_pad = NULL; |
| 1448 | struct media_link *link; |
| 1449 | |
| 1450 | list_for_each_entry(link, &pad->entity->links, list) { |
| 1451 | struct media_pad *remote_pad; |
| 1452 | |
| 1453 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 1454 | continue; |
| 1455 | |
| 1456 | if (link->sink == pad) |
| 1457 | remote_pad = link->source; |
| 1458 | else if (link->source == pad) |
| 1459 | remote_pad = link->sink; |
| 1460 | else |
| 1461 | continue; |
| 1462 | |
| 1463 | if (found_pad) |
| 1464 | return ERR_PTR(-ENOTUNIQ); |
| 1465 | |
| 1466 | found_pad = remote_pad; |
| 1467 | } |
| 1468 | |
| 1469 | if (!found_pad) |
| 1470 | return ERR_PTR(-ENOLINK); |
| 1471 | |
| 1472 | return found_pad; |
| 1473 | } |
| 1474 | EXPORT_SYMBOL_GPL(media_pad_remote_pad_unique); |
| 1475 | |
Laurent Pinchart | 612589a | 2022-08-31 16:13:30 +0200 | [diff] [blame] | 1476 | int media_entity_get_fwnode_pad(struct media_entity *entity, |
Sakari Ailus | ec17831 | 2023-05-05 16:27:49 +0300 | [diff] [blame] | 1477 | const struct fwnode_handle *fwnode, |
Laurent Pinchart | 612589a | 2022-08-31 16:13:30 +0200 | [diff] [blame] | 1478 | unsigned long direction_flags) |
| 1479 | { |
| 1480 | struct fwnode_endpoint endpoint; |
| 1481 | unsigned int i; |
| 1482 | int ret; |
| 1483 | |
| 1484 | if (!entity->ops || !entity->ops->get_fwnode_pad) { |
| 1485 | for (i = 0; i < entity->num_pads; i++) { |
| 1486 | if (entity->pads[i].flags & direction_flags) |
| 1487 | return i; |
| 1488 | } |
| 1489 | |
| 1490 | return -ENXIO; |
| 1491 | } |
| 1492 | |
| 1493 | ret = fwnode_graph_parse_endpoint(fwnode, &endpoint); |
| 1494 | if (ret) |
| 1495 | return ret; |
| 1496 | |
| 1497 | ret = entity->ops->get_fwnode_pad(entity, &endpoint); |
| 1498 | if (ret < 0) |
| 1499 | return ret; |
| 1500 | |
| 1501 | if (ret >= entity->num_pads) |
| 1502 | return -ENXIO; |
| 1503 | |
| 1504 | if (!(entity->pads[ret].flags & direction_flags)) |
| 1505 | return -ENXIO; |
| 1506 | |
| 1507 | return ret; |
| 1508 | } |
| 1509 | EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad); |
| 1510 | |
Laurent Pinchart | 72b6033 | 2022-08-31 16:13:31 +0200 | [diff] [blame] | 1511 | struct media_pipeline *media_entity_pipeline(struct media_entity *entity) |
| 1512 | { |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 1513 | struct media_pad *pad; |
| 1514 | |
| 1515 | media_entity_for_each_pad(entity, pad) { |
| 1516 | if (pad->pipe) |
| 1517 | return pad->pipe; |
| 1518 | } |
| 1519 | |
| 1520 | return NULL; |
Laurent Pinchart | 72b6033 | 2022-08-31 16:13:31 +0200 | [diff] [blame] | 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(media_entity_pipeline); |
| 1523 | |
Laurent Pinchart | ae21987 | 2022-08-31 16:13:37 +0200 | [diff] [blame] | 1524 | struct media_pipeline *media_pad_pipeline(struct media_pad *pad) |
| 1525 | { |
| 1526 | return pad->pipe; |
| 1527 | } |
| 1528 | EXPORT_SYMBOL_GPL(media_pad_pipeline); |
| 1529 | |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 1530 | static void media_interface_init(struct media_device *mdev, |
| 1531 | struct media_interface *intf, |
| 1532 | u32 gobj_type, |
| 1533 | u32 intf_type, u32 flags) |
| 1534 | { |
| 1535 | intf->type = intf_type; |
| 1536 | intf->flags = flags; |
| 1537 | INIT_LIST_HEAD(&intf->links); |
| 1538 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1539 | media_gobj_create(mdev, gobj_type, &intf->graph_obj); |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 1540 | } |
| 1541 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1542 | /* Functions related to the media interface via device nodes */ |
| 1543 | |
| 1544 | struct media_intf_devnode *media_devnode_create(struct media_device *mdev, |
| 1545 | u32 type, u32 flags, |
Mauro Carvalho Chehab | 0b3b72df9 | 2015-09-09 08:19:25 -0300 | [diff] [blame] | 1546 | u32 major, u32 minor) |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1547 | { |
| 1548 | struct media_intf_devnode *devnode; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1549 | |
Mauro Carvalho Chehab | 0b3b72df9 | 2015-09-09 08:19:25 -0300 | [diff] [blame] | 1550 | devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1551 | if (!devnode) |
| 1552 | return NULL; |
| 1553 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1554 | devnode->major = major; |
| 1555 | devnode->minor = minor; |
| 1556 | |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 1557 | media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, |
| 1558 | type, flags); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1559 | |
| 1560 | return devnode; |
| 1561 | } |
| 1562 | EXPORT_SYMBOL_GPL(media_devnode_create); |
| 1563 | |
| 1564 | void media_devnode_remove(struct media_intf_devnode *devnode) |
| 1565 | { |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1566 | media_remove_intf_links(&devnode->intf); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1567 | media_gobj_destroy(&devnode->intf.graph_obj); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 1568 | kfree(devnode); |
| 1569 | } |
| 1570 | EXPORT_SYMBOL_GPL(media_devnode_remove); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1571 | |
| 1572 | struct media_link *media_create_intf_link(struct media_entity *entity, |
| 1573 | struct media_interface *intf, |
| 1574 | u32 flags) |
| 1575 | { |
| 1576 | struct media_link *link; |
| 1577 | |
| 1578 | link = media_add_link(&intf->links); |
| 1579 | if (link == NULL) |
| 1580 | return NULL; |
| 1581 | |
| 1582 | link->intf = intf; |
| 1583 | link->entity = entity; |
Mauro Carvalho Chehab | 82ae2a5 | 2015-12-11 18:09:13 -0200 | [diff] [blame] | 1584 | link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK; |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1585 | |
| 1586 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1587 | media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1588 | &link->graph_obj); |
| 1589 | |
| 1590 | return link; |
| 1591 | } |
| 1592 | EXPORT_SYMBOL_GPL(media_create_intf_link); |
| 1593 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 1594 | void __media_remove_intf_link(struct media_link *link) |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1595 | { |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 1596 | list_del(&link->list); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 1597 | media_gobj_destroy(&link->graph_obj); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1598 | kfree(link); |
| 1599 | } |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 1600 | EXPORT_SYMBOL_GPL(__media_remove_intf_link); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1601 | |
| 1602 | void media_remove_intf_link(struct media_link *link) |
| 1603 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1604 | struct media_device *mdev = link->graph_obj.mdev; |
| 1605 | |
| 1606 | /* Do nothing if the intf is not registered. */ |
| 1607 | if (mdev == NULL) |
| 1608 | return; |
| 1609 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1610 | mutex_lock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1611 | __media_remove_intf_link(link); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1612 | mutex_unlock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 1613 | } |
| 1614 | EXPORT_SYMBOL_GPL(media_remove_intf_link); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1615 | |
| 1616 | void __media_remove_intf_links(struct media_interface *intf) |
| 1617 | { |
| 1618 | struct media_link *link, *tmp; |
| 1619 | |
| 1620 | list_for_each_entry_safe(link, tmp, &intf->links, list) |
| 1621 | __media_remove_intf_link(link); |
| 1622 | |
| 1623 | } |
| 1624 | EXPORT_SYMBOL_GPL(__media_remove_intf_links); |
| 1625 | |
| 1626 | void media_remove_intf_links(struct media_interface *intf) |
| 1627 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1628 | struct media_device *mdev = intf->graph_obj.mdev; |
| 1629 | |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1630 | /* Do nothing if the intf is not registered. */ |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1631 | if (mdev == NULL) |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1632 | return; |
| 1633 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1634 | mutex_lock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1635 | __media_remove_intf_links(intf); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1636 | mutex_unlock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1637 | } |
| 1638 | EXPORT_SYMBOL_GPL(media_remove_intf_links); |
Daniel Scally | 012c87f | 2022-03-02 22:03:03 +0000 | [diff] [blame] | 1639 | |
| 1640 | struct media_link *media_create_ancillary_link(struct media_entity *primary, |
| 1641 | struct media_entity *ancillary) |
| 1642 | { |
| 1643 | struct media_link *link; |
| 1644 | |
| 1645 | link = media_add_link(&primary->links); |
| 1646 | if (!link) |
| 1647 | return ERR_PTR(-ENOMEM); |
| 1648 | |
| 1649 | link->gobj0 = &primary->graph_obj; |
| 1650 | link->gobj1 = &ancillary->graph_obj; |
| 1651 | link->flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED | |
| 1652 | MEDIA_LNK_FL_ANCILLARY_LINK; |
| 1653 | |
| 1654 | /* Initialize graph object embedded in the new link */ |
| 1655 | media_gobj_create(primary->graph_obj.mdev, MEDIA_GRAPH_LINK, |
| 1656 | &link->graph_obj); |
| 1657 | |
| 1658 | return link; |
| 1659 | } |
| 1660 | EXPORT_SYMBOL_GPL(media_create_ancillary_link); |
Daniel Scally | 1ed3d64 | 2022-07-07 23:47:32 +0100 | [diff] [blame] | 1661 | |
| 1662 | struct media_link *__media_entity_next_link(struct media_entity *entity, |
| 1663 | struct media_link *link, |
| 1664 | unsigned long link_type) |
| 1665 | { |
| 1666 | link = link ? list_next_entry(link, list) |
| 1667 | : list_first_entry(&entity->links, typeof(*link), list); |
| 1668 | |
| 1669 | list_for_each_entry_from(link, &entity->links, list) |
| 1670 | if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type) |
| 1671 | return link; |
| 1672 | |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | EXPORT_SYMBOL_GPL(__media_entity_next_link); |