Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Media entity |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
| 5 | * |
| 6 | * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 7 | * Sakari Ailus <sakari.ailus@iki.fi> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 17 | */ |
| 18 | |
Laurent Pinchart | 5c7b25b | 2013-06-07 12:45:11 -0300 | [diff] [blame] | 19 | #include <linux/bitmap.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <media/media-entity.h> |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 23 | #include <media/media-device.h> |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 24 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 25 | static inline const char *gobj_type(enum media_gobj_type type) |
| 26 | { |
| 27 | switch (type) { |
| 28 | case MEDIA_GRAPH_ENTITY: |
| 29 | return "entity"; |
| 30 | case MEDIA_GRAPH_PAD: |
| 31 | return "pad"; |
| 32 | case MEDIA_GRAPH_LINK: |
| 33 | return "link"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 34 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 35 | return "intf-devnode"; |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 36 | default: |
| 37 | return "unknown"; |
| 38 | } |
| 39 | } |
| 40 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 41 | static inline const char *intf_type(struct media_interface *intf) |
| 42 | { |
| 43 | switch (intf->type) { |
| 44 | case MEDIA_INTF_T_DVB_FE: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 45 | return "dvb-frontend"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 46 | case MEDIA_INTF_T_DVB_DEMUX: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 47 | return "dvb-demux"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 48 | case MEDIA_INTF_T_DVB_DVR: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 49 | return "dvb-dvr"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 50 | case MEDIA_INTF_T_DVB_CA: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 51 | return "dvb-ca"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 52 | case MEDIA_INTF_T_DVB_NET: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 53 | return "dvb-net"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 54 | case MEDIA_INTF_T_V4L_VIDEO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 55 | return "v4l-video"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 56 | case MEDIA_INTF_T_V4L_VBI: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 57 | return "v4l-vbi"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 58 | case MEDIA_INTF_T_V4L_RADIO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 59 | return "v4l-radio"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 60 | case MEDIA_INTF_T_V4L_SUBDEV: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 61 | return "v4l-subdev"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 62 | case MEDIA_INTF_T_V4L_SWRADIO: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 63 | return "v4l-swradio"; |
Nick Dyer | b2fe22d | 2016-07-18 18:10:30 -0300 | [diff] [blame] | 64 | case MEDIA_INTF_T_V4L_TOUCH: |
| 65 | return "v4l-touch"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 66 | case MEDIA_INTF_T_ALSA_PCM_CAPTURE: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 67 | return "alsa-pcm-capture"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 68 | case MEDIA_INTF_T_ALSA_PCM_PLAYBACK: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 69 | return "alsa-pcm-playback"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 70 | case MEDIA_INTF_T_ALSA_CONTROL: |
| 71 | return "alsa-control"; |
| 72 | case MEDIA_INTF_T_ALSA_COMPRESS: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 73 | return "alsa-compress"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 74 | case MEDIA_INTF_T_ALSA_RAWMIDI: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 75 | return "alsa-rawmidi"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 76 | case MEDIA_INTF_T_ALSA_HWDEP: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 77 | return "alsa-hwdep"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 78 | case MEDIA_INTF_T_ALSA_SEQUENCER: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 79 | return "alsa-sequencer"; |
Shuah Khan | 5af557a | 2016-02-11 21:41:18 -0200 | [diff] [blame] | 80 | case MEDIA_INTF_T_ALSA_TIMER: |
Shuah Khan | 66c1db1 | 2016-03-04 18:14:05 -0300 | [diff] [blame] | 81 | return "alsa-timer"; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 82 | default: |
| 83 | return "unknown-intf"; |
| 84 | } |
| 85 | }; |
| 86 | |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 87 | __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum, |
| 88 | int idx_max) |
| 89 | { |
Sakari Ailus | f7b5dff | 2016-01-27 12:47:54 -0200 | [diff] [blame] | 90 | idx_max = ALIGN(idx_max, BITS_PER_LONG); |
| 91 | ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long), |
| 92 | GFP_KERNEL); |
Sakari Ailus | 030e89e | 2015-12-16 11:32:36 -0200 | [diff] [blame] | 93 | if (!ent_enum->bmap) |
| 94 | return -ENOMEM; |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 95 | |
| 96 | bitmap_zero(ent_enum->bmap, idx_max); |
| 97 | ent_enum->idx_max = idx_max; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(__media_entity_enum_init); |
| 102 | |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 103 | void media_entity_enum_cleanup(struct media_entity_enum *ent_enum) |
| 104 | { |
Sakari Ailus | 030e89e | 2015-12-16 11:32:36 -0200 | [diff] [blame] | 105 | kfree(ent_enum->bmap); |
Sakari Ailus | c8d54cd | 2015-12-16 11:44:32 -0200 | [diff] [blame] | 106 | } |
| 107 | EXPORT_SYMBOL_GPL(media_entity_enum_cleanup); |
| 108 | |
| 109 | /** |
Mauro Carvalho Chehab | 1fc25d3 | 2015-12-11 12:14:58 -0200 | [diff] [blame] | 110 | * dev_dbg_obj - Prints in debug mode a change on some object |
| 111 | * |
| 112 | * @event_name: Name of the event to report. Could be __func__ |
| 113 | * @gobj: Pointer to the object |
| 114 | * |
| 115 | * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it |
| 116 | * won't produce any code. |
| 117 | */ |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 118 | static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj) |
| 119 | { |
| 120 | #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG) |
| 121 | switch (media_type(gobj)) { |
| 122 | case MEDIA_GRAPH_ENTITY: |
| 123 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 124 | "%s id %u: entity '%s'\n", |
| 125 | event_name, media_id(gobj), |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 126 | gobj_to_entity(gobj)->name); |
| 127 | break; |
| 128 | case MEDIA_GRAPH_LINK: |
| 129 | { |
| 130 | struct media_link *link = gobj_to_link(gobj); |
| 131 | |
| 132 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 133 | "%s id %u: %s link id %u ==> id %u\n", |
| 134 | event_name, media_id(gobj), |
| 135 | media_type(link->gobj0) == MEDIA_GRAPH_PAD ? |
| 136 | "data" : "interface", |
| 137 | media_id(link->gobj0), |
| 138 | media_id(link->gobj1)); |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | case MEDIA_GRAPH_PAD: |
| 142 | { |
| 143 | struct media_pad *pad = gobj_to_pad(gobj); |
| 144 | |
| 145 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 146 | "%s id %u: %s%spad '%s':%d\n", |
| 147 | event_name, media_id(gobj), |
| 148 | pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "", |
Mauro Carvalho Chehab | 6c24d46 | 2015-08-21 18:26:42 -0300 | [diff] [blame] | 149 | pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "", |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 150 | pad->entity->name, pad->index); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | case MEDIA_GRAPH_INTF_DEVNODE: |
| 154 | { |
| 155 | struct media_interface *intf = gobj_to_intf(gobj); |
| 156 | struct media_intf_devnode *devnode = intf_to_devnode(intf); |
| 157 | |
| 158 | dev_dbg(gobj->mdev->dev, |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 159 | "%s id %u: intf_devnode %s - major: %d, minor: %d\n", |
| 160 | event_name, media_id(gobj), |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 161 | intf_type(intf), |
| 162 | devnode->major, devnode->minor); |
| 163 | break; |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | } |
| 168 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 169 | void media_gobj_create(struct media_device *mdev, |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 170 | enum media_gobj_type type, |
| 171 | struct media_gobj *gobj) |
| 172 | { |
Mauro Carvalho Chehab | 8f6d368 | 2015-08-19 20:18:35 -0300 | [diff] [blame] | 173 | BUG_ON(!mdev); |
| 174 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 175 | gobj->mdev = mdev; |
| 176 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 177 | /* Create a per-type unique object ID */ |
Mauro Carvalho Chehab | 05b3b77 | 2015-12-16 14:28:01 -0200 | [diff] [blame] | 178 | gobj->id = media_gobj_gen_id(type, ++mdev->id); |
| 179 | |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 180 | switch (type) { |
| 181 | case MEDIA_GRAPH_ENTITY: |
Mauro Carvalho Chehab | 05bfa9f | 2015-08-23 07:51:33 -0300 | [diff] [blame] | 182 | list_add_tail(&gobj->list, &mdev->entities); |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 183 | break; |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 184 | case MEDIA_GRAPH_PAD: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 185 | list_add_tail(&gobj->list, &mdev->pads); |
Mauro Carvalho Chehab | 18710dc | 2015-08-14 12:50:08 -0300 | [diff] [blame] | 186 | break; |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 187 | case MEDIA_GRAPH_LINK: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 188 | list_add_tail(&gobj->list, &mdev->links); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 189 | break; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 190 | case MEDIA_GRAPH_INTF_DEVNODE: |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 191 | list_add_tail(&gobj->list, &mdev->interfaces); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 192 | break; |
Mauro Carvalho Chehab | bfab2aac | 2015-08-14 12:47:48 -0300 | [diff] [blame] | 193 | } |
Mauro Carvalho Chehab | 2521fda | 2015-08-23 09:40:26 -0300 | [diff] [blame] | 194 | |
| 195 | mdev->topology_version++; |
| 196 | |
Mauro Carvalho Chehab | 39a956c | 2015-08-13 14:42:42 -0300 | [diff] [blame] | 197 | dev_dbg_obj(__func__, gobj); |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 198 | } |
| 199 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 200 | void media_gobj_destroy(struct media_gobj *gobj) |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 201 | { |
Max Kellermann | 6753743 | 2016-08-09 23:32:57 +0200 | [diff] [blame] | 202 | /* Do nothing if the object is not linked. */ |
| 203 | if (gobj->mdev == NULL) |
| 204 | return; |
| 205 | |
Helen Fornazier | 8d1d3d0 | 2017-04-06 16:25:15 -0300 | [diff] [blame] | 206 | dev_dbg_obj(__func__, gobj); |
| 207 | |
Mauro Carvalho Chehab | 2521fda | 2015-08-23 09:40:26 -0300 | [diff] [blame] | 208 | gobj->mdev->topology_version++; |
| 209 | |
Mauro Carvalho Chehab | 9155d85 | 2015-08-23 08:00:33 -0300 | [diff] [blame] | 210 | /* Remove the object from mdev list */ |
| 211 | list_del(&gobj->list); |
Max Kellermann | 6753743 | 2016-08-09 23:32:57 +0200 | [diff] [blame] | 212 | |
| 213 | gobj->mdev = NULL; |
Mauro Carvalho Chehab | ec6e4c9 | 2015-08-25 10:28:36 -0300 | [diff] [blame] | 214 | } |
| 215 | |
Mauro Carvalho Chehab | 1fc25d3 | 2015-12-11 12:14:58 -0200 | [diff] [blame] | 216 | int media_entity_pads_init(struct media_entity *entity, u16 num_pads, |
| 217 | struct media_pad *pads) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 218 | { |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 219 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 220 | unsigned int i; |
| 221 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 222 | entity->num_pads = num_pads; |
| 223 | entity->pads = pads; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 224 | |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 225 | if (mdev) |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 226 | mutex_lock(&mdev->graph_mutex); |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 227 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 228 | for (i = 0; i < num_pads; i++) { |
| 229 | pads[i].entity = entity; |
| 230 | pads[i].index = i; |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 231 | if (mdev) |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 232 | media_gobj_create(mdev, MEDIA_GRAPH_PAD, |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 233 | &entity->pads[i].graph_obj); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 234 | } |
| 235 | |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 236 | if (mdev) |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 237 | mutex_unlock(&mdev->graph_mutex); |
Javier Martinez Canillas | db141a3 | 2015-09-08 14:10:56 -0300 | [diff] [blame] | 238 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 239 | return 0; |
| 240 | } |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 241 | EXPORT_SYMBOL_GPL(media_entity_pads_init); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 242 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 243 | /* ----------------------------------------------------------------------------- |
| 244 | * Graph traversal |
| 245 | */ |
| 246 | |
| 247 | static struct media_entity * |
| 248 | media_entity_other(struct media_entity *entity, struct media_link *link) |
| 249 | { |
| 250 | if (link->source->entity == entity) |
| 251 | return link->sink->entity; |
| 252 | else |
| 253 | return link->source->entity; |
| 254 | } |
| 255 | |
| 256 | /* push an entity to traversal stack */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 257 | static void stack_push(struct media_graph *graph, |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 258 | struct media_entity *entity) |
| 259 | { |
| 260 | if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) { |
| 261 | WARN_ON(1); |
| 262 | return; |
| 263 | } |
| 264 | graph->top++; |
Javier Martinez Canillas | 313895f | 2015-12-11 15:16:36 -0200 | [diff] [blame] | 265 | graph->stack[graph->top].link = entity->links.next; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 266 | graph->stack[graph->top].entity = entity; |
| 267 | } |
| 268 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 269 | static struct media_entity *stack_pop(struct media_graph *graph) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 270 | { |
| 271 | struct media_entity *entity; |
| 272 | |
| 273 | entity = graph->stack[graph->top].entity; |
| 274 | graph->top--; |
| 275 | |
| 276 | return entity; |
| 277 | } |
| 278 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 279 | #define link_top(en) ((en)->stack[(en)->top].link) |
| 280 | #define stack_top(en) ((en)->stack[(en)->top].entity) |
| 281 | |
Sakari Ailus | 0798ce4 | 2015-12-16 11:32:37 -0200 | [diff] [blame] | 282 | /* |
| 283 | * TODO: Get rid of this. |
| 284 | */ |
Mauro Carvalho Chehab | 430a672 | 2015-12-16 15:18:25 -0200 | [diff] [blame] | 285 | #define MEDIA_ENTITY_MAX_PADS 512 |
Sakari Ailus | 0798ce4 | 2015-12-16 11:32:37 -0200 | [diff] [blame] | 286 | |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 287 | /** |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 288 | * media_graph_walk_init - Allocate resources for graph walk |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 289 | * @graph: Media graph structure that will be used to walk the graph |
| 290 | * @mdev: Media device |
| 291 | * |
| 292 | * Reserve resources for graph walk in media device's current |
| 293 | * state. The memory must be released using |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 294 | * media_graph_walk_free(). |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 295 | * |
| 296 | * Returns error on failure, zero on success. |
| 297 | */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 298 | __must_check int media_graph_walk_init( |
| 299 | struct media_graph *graph, struct media_device *mdev) |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 300 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 301 | return media_entity_enum_init(&graph->ent_enum, mdev); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 302 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 303 | EXPORT_SYMBOL_GPL(media_graph_walk_init); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 304 | |
| 305 | /** |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 306 | * media_graph_walk_cleanup - Release resources related to graph walking |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 307 | * @graph: Media graph structure that was used to walk the graph |
| 308 | */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 309 | void media_graph_walk_cleanup(struct media_graph *graph) |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 310 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 311 | media_entity_enum_cleanup(&graph->ent_enum); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 312 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(media_graph_walk_cleanup); |
Sakari Ailus | e03d220 | 2015-12-16 11:32:22 -0200 | [diff] [blame] | 314 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 315 | void media_graph_walk_start(struct media_graph *graph, |
| 316 | struct media_entity *entity) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 317 | { |
Sakari Ailus | 29d8da0 | 2015-12-16 11:32:28 -0200 | [diff] [blame] | 318 | media_entity_enum_zero(&graph->ent_enum); |
| 319 | media_entity_enum_set(&graph->ent_enum, entity); |
| 320 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 321 | graph->top = 0; |
| 322 | graph->stack[graph->top].entity = NULL; |
| 323 | stack_push(graph, entity); |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 324 | dev_dbg(entity->graph_obj.mdev->dev, |
| 325 | "begin graph walk at '%s'\n", entity->name); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 326 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 327 | EXPORT_SYMBOL_GPL(media_graph_walk_start); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 328 | |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 329 | static void media_graph_walk_iter(struct media_graph *graph) |
| 330 | { |
| 331 | struct media_entity *entity = stack_top(graph); |
| 332 | struct media_link *link; |
| 333 | struct media_entity *next; |
| 334 | |
| 335 | link = list_entry(link_top(graph), typeof(*link), list); |
| 336 | |
| 337 | /* The link is not enabled so we do not follow. */ |
| 338 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { |
| 339 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 340 | dev_dbg(entity->graph_obj.mdev->dev, |
| 341 | "walk: skipping disabled link '%s':%u -> '%s':%u\n", |
| 342 | link->source->entity->name, link->source->index, |
| 343 | link->sink->entity->name, link->sink->index); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
| 347 | /* Get the entity in the other end of the link . */ |
| 348 | next = media_entity_other(entity, link); |
| 349 | |
| 350 | /* Has the entity already been visited? */ |
| 351 | if (media_entity_enum_test_and_set(&graph->ent_enum, next)) { |
| 352 | link_top(graph) = link_top(graph)->next; |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 353 | dev_dbg(entity->graph_obj.mdev->dev, |
| 354 | "walk: skipping entity '%s' (already seen)\n", |
| 355 | next->name); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
| 359 | /* Push the new entity to stack and start over. */ |
| 360 | link_top(graph) = link_top(graph)->next; |
| 361 | stack_push(graph, next); |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 362 | dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n", |
| 363 | next->name); |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 364 | } |
| 365 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 366 | struct media_entity *media_graph_walk_next(struct media_graph *graph) |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 367 | { |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 368 | struct media_entity *entity; |
| 369 | |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 370 | if (stack_top(graph) == NULL) |
| 371 | return NULL; |
| 372 | |
| 373 | /* |
| 374 | * Depth first search. Push entity to stack and continue from |
| 375 | * top of the stack until no more entities on the level can be |
| 376 | * found. |
| 377 | */ |
Sakari Ailus | 5b1f832 | 2016-07-20 12:31:39 -0300 | [diff] [blame] | 378 | while (link_top(graph) != &stack_top(graph)->links) |
| 379 | media_graph_walk_iter(graph); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 380 | |
Sakari Ailus | aa79a84 | 2016-07-20 12:39:02 -0300 | [diff] [blame] | 381 | entity = stack_pop(graph); |
| 382 | dev_dbg(entity->graph_obj.mdev->dev, |
| 383 | "walk: returning entity '%s'\n", entity->name); |
| 384 | |
| 385 | return entity; |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 386 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 387 | EXPORT_SYMBOL_GPL(media_graph_walk_next); |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 388 | |
| 389 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 390 | * Pipeline management |
| 391 | */ |
| 392 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 393 | __must_check int __media_pipeline_start(struct media_entity *entity, |
| 394 | struct media_pipeline *pipe) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 395 | { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 396 | struct media_device *mdev = entity->graph_obj.mdev; |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 397 | struct media_graph *graph = &pipe->graph; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 398 | struct media_entity *entity_err = entity; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 399 | struct media_link *link; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 400 | int ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 401 | |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 402 | if (!pipe->streaming_count++) { |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 403 | ret = media_graph_walk_init(&pipe->graph, mdev); |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 404 | if (ret) |
| 405 | goto error_graph_walk_start; |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 408 | media_graph_walk_start(&pipe->graph, entity); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 409 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 410 | while ((entity = media_graph_walk_next(graph))) { |
Mauro Carvalho Chehab | ef69ee1 | 2015-10-01 18:07:53 -0300 | [diff] [blame] | 411 | DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS); |
| 412 | DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 413 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 414 | entity->stream_count++; |
Sakari Ailus | 8aaf62b | 2015-11-29 17:20:02 -0200 | [diff] [blame] | 415 | |
| 416 | if (WARN_ON(entity->pipe && entity->pipe != pipe)) { |
| 417 | ret = -EBUSY; |
| 418 | goto error; |
| 419 | } |
| 420 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 421 | entity->pipe = pipe; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 422 | |
| 423 | /* Already streaming --- no need to check. */ |
| 424 | if (entity->stream_count > 1) |
| 425 | continue; |
| 426 | |
| 427 | if (!entity->ops || !entity->ops->link_validate) |
| 428 | continue; |
| 429 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 430 | bitmap_zero(active, entity->num_pads); |
| 431 | bitmap_fill(has_no_links, entity->num_pads); |
| 432 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 433 | list_for_each_entry(link, &entity->links, list) { |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 434 | struct media_pad *pad = link->sink->entity == entity |
| 435 | ? link->sink : link->source; |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 436 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 437 | /* Mark that a pad is connected by a link. */ |
| 438 | bitmap_clear(has_no_links, pad->index, 1); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 439 | |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 440 | /* |
| 441 | * Pads that either do not need to connect or |
| 442 | * are connected through an enabled link are |
| 443 | * fine. |
| 444 | */ |
| 445 | if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || |
| 446 | link->flags & MEDIA_LNK_FL_ENABLED) |
| 447 | bitmap_set(active, pad->index, 1); |
| 448 | |
| 449 | /* |
| 450 | * Link validation will only take place for |
| 451 | * sink ends of the link that are enabled. |
| 452 | */ |
| 453 | if (link->sink != pad || |
| 454 | !(link->flags & MEDIA_LNK_FL_ENABLED)) |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 455 | continue; |
| 456 | |
| 457 | ret = entity->ops->link_validate(link); |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 458 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 459 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | 91b619a | 2016-12-08 13:22:29 -0200 | [diff] [blame] | 460 | "link validation failed for '%s':%u -> '%s':%u, error %d\n", |
Sakari Ailus | 823ea2a | 2015-02-12 11:43:11 -0200 | [diff] [blame] | 461 | link->source->entity->name, |
| 462 | link->source->index, |
| 463 | entity->name, link->sink->index, ret); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 464 | goto error; |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 465 | } |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 466 | } |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 467 | |
| 468 | /* Either no links or validated links are fine. */ |
| 469 | bitmap_or(active, active, has_no_links, entity->num_pads); |
| 470 | |
| 471 | if (!bitmap_full(active, entity->num_pads)) { |
Helen Mae Koike Fornazier | 47dfdb3 | 2015-06-28 16:14:10 -0300 | [diff] [blame] | 472 | ret = -ENOLINK; |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 473 | dev_dbg(entity->graph_obj.mdev->dev, |
Sakari Ailus | 91b619a | 2016-12-08 13:22:29 -0200 | [diff] [blame] | 474 | "'%s':%u must be connected by an enabled link\n", |
Sakari Ailus | fab9d30 | 2014-10-28 20:35:04 -0300 | [diff] [blame] | 475 | entity->name, |
Sakari Ailus | 094f1ca | 2014-11-03 17:55:51 -0300 | [diff] [blame] | 476 | (unsigned)find_first_zero_bit( |
| 477 | active, entity->num_pads)); |
Sakari Ailus | de49c28 | 2013-10-13 08:00:26 -0300 | [diff] [blame] | 478 | goto error; |
| 479 | } |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 480 | } |
| 481 | |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 482 | return 0; |
| 483 | |
| 484 | error: |
| 485 | /* |
| 486 | * Link validation on graph failed. We revert what we did and |
| 487 | * return the error. |
| 488 | */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 489 | media_graph_walk_start(graph, entity_err); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 490 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 491 | while ((entity_err = media_graph_walk_next(graph))) { |
Sakari Ailus | 12030f4 | 2016-07-20 09:19:03 -0300 | [diff] [blame] | 492 | /* Sanity check for negative stream_count */ |
| 493 | if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) { |
Shuah Khan | 3801bc7 | 2016-01-30 18:10:52 -0200 | [diff] [blame] | 494 | entity_err->stream_count--; |
| 495 | if (entity_err->stream_count == 0) |
| 496 | entity_err->pipe = NULL; |
| 497 | } |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * We haven't increased stream_count further than this |
| 501 | * so we quit here. |
| 502 | */ |
| 503 | if (entity_err == entity) |
| 504 | break; |
| 505 | } |
| 506 | |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 507 | error_graph_walk_start: |
| 508 | if (!--pipe->streaming_count) |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 509 | media_graph_walk_cleanup(graph); |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 510 | |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 511 | return ret; |
| 512 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(__media_pipeline_start); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 514 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 515 | __must_check int media_pipeline_start(struct media_entity *entity, |
| 516 | struct media_pipeline *pipe) |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 517 | { |
| 518 | struct media_device *mdev = entity->graph_obj.mdev; |
| 519 | int ret; |
| 520 | |
| 521 | mutex_lock(&mdev->graph_mutex); |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 522 | ret = __media_pipeline_start(entity, pipe); |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 523 | mutex_unlock(&mdev->graph_mutex); |
Sakari Ailus | af88be3 | 2012-01-11 06:25:15 -0300 | [diff] [blame] | 524 | return ret; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 525 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 526 | EXPORT_SYMBOL_GPL(media_pipeline_start); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 527 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 528 | void __media_pipeline_stop(struct media_entity *entity) |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 529 | { |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 530 | struct media_graph *graph = &entity->pipe->graph; |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 531 | struct media_pipeline *pipe = entity->pipe; |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 532 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 533 | |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 534 | WARN_ON(!pipe->streaming_count); |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 535 | media_graph_walk_start(graph, entity); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 536 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 537 | while ((entity = media_graph_walk_next(graph))) { |
Sakari Ailus | 12030f4 | 2016-07-20 09:19:03 -0300 | [diff] [blame] | 538 | /* Sanity check for negative stream_count */ |
| 539 | if (!WARN_ON_ONCE(entity->stream_count <= 0)) { |
Shuah Khan | 3801bc7 | 2016-01-30 18:10:52 -0200 | [diff] [blame] | 540 | entity->stream_count--; |
| 541 | if (entity->stream_count == 0) |
| 542 | entity->pipe = NULL; |
| 543 | } |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 544 | } |
| 545 | |
Sakari Ailus | 74a4133 | 2015-12-16 11:32:29 -0200 | [diff] [blame] | 546 | if (!--pipe->streaming_count) |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 547 | media_graph_walk_cleanup(graph); |
Sakari Ailus | 106b990 | 2015-12-16 15:32:23 +0200 | [diff] [blame] | 548 | |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 549 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 550 | EXPORT_SYMBOL_GPL(__media_pipeline_stop); |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 551 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 552 | void media_pipeline_stop(struct media_entity *entity) |
Shuah Khan | fb49f20 | 2016-02-11 21:41:24 -0200 | [diff] [blame] | 553 | { |
| 554 | struct media_device *mdev = entity->graph_obj.mdev; |
| 555 | |
| 556 | mutex_lock(&mdev->graph_mutex); |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 557 | __media_pipeline_stop(entity); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 558 | mutex_unlock(&mdev->graph_mutex); |
| 559 | } |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 560 | EXPORT_SYMBOL_GPL(media_pipeline_stop); |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 561 | |
| 562 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 563 | * Module use count |
| 564 | */ |
| 565 | |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 566 | struct media_entity *media_entity_get(struct media_entity *entity) |
| 567 | { |
| 568 | if (entity == NULL) |
| 569 | return NULL; |
| 570 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 571 | if (entity->graph_obj.mdev->dev && |
| 572 | !try_module_get(entity->graph_obj.mdev->dev->driver->owner)) |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 573 | return NULL; |
| 574 | |
| 575 | return entity; |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(media_entity_get); |
| 578 | |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 579 | void media_entity_put(struct media_entity *entity) |
| 580 | { |
| 581 | if (entity == NULL) |
| 582 | return; |
| 583 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 584 | if (entity->graph_obj.mdev->dev) |
| 585 | module_put(entity->graph_obj.mdev->dev->driver->owner); |
Laurent Pinchart | 503c3d82 | 2010-03-07 15:04:59 -0300 | [diff] [blame] | 586 | } |
| 587 | EXPORT_SYMBOL_GPL(media_entity_put); |
| 588 | |
| 589 | /* ----------------------------------------------------------------------------- |
Sakari Ailus | a5ccc48 | 2010-03-07 16:14:14 -0300 | [diff] [blame] | 590 | * Links management |
| 591 | */ |
| 592 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 593 | static struct media_link *media_add_link(struct list_head *head) |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 594 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 595 | struct media_link *link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 596 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 597 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 598 | if (link == NULL) |
| 599 | return NULL; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 600 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 601 | list_add_tail(&link->list, head); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 602 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 603 | return link; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 604 | } |
| 605 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 606 | static void __media_entity_remove_link(struct media_entity *entity, |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 607 | struct media_link *link) |
| 608 | { |
| 609 | struct media_link *rlink, *tmp; |
| 610 | struct media_entity *remote; |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 611 | |
| 612 | if (link->source->entity == entity) |
| 613 | remote = link->sink->entity; |
| 614 | else |
| 615 | remote = link->source->entity; |
| 616 | |
| 617 | list_for_each_entry_safe(rlink, tmp, &remote->links, list) { |
Mauro Carvalho Chehab | 58f69ee | 2015-12-11 11:25:23 -0200 | [diff] [blame] | 618 | if (rlink != link->reverse) |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 619 | continue; |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 620 | |
| 621 | if (link->source->entity == entity) |
| 622 | remote->num_backlinks--; |
| 623 | |
| 624 | /* Remove the remote link */ |
| 625 | list_del(&rlink->list); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 626 | media_gobj_destroy(&rlink->graph_obj); |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 627 | kfree(rlink); |
| 628 | |
| 629 | if (--remote->num_links == 0) |
| 630 | break; |
| 631 | } |
| 632 | list_del(&link->list); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 633 | media_gobj_destroy(&link->graph_obj); |
Mauro Carvalho Chehab | 5abad22 | 2015-12-11 11:19:38 -0200 | [diff] [blame] | 634 | kfree(link); |
| 635 | } |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 636 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 637 | int |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 638 | media_create_pad_link(struct media_entity *source, u16 source_pad, |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 639 | struct media_entity *sink, u16 sink_pad, u32 flags) |
| 640 | { |
| 641 | struct media_link *link; |
| 642 | struct media_link *backlink; |
| 643 | |
| 644 | BUG_ON(source == NULL || sink == NULL); |
| 645 | BUG_ON(source_pad >= source->num_pads); |
| 646 | BUG_ON(sink_pad >= sink->num_pads); |
| 647 | |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 648 | link = media_add_link(&source->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 649 | if (link == NULL) |
| 650 | return -ENOMEM; |
| 651 | |
| 652 | link->source = &source->pads[source_pad]; |
| 653 | link->sink = &sink->pads[sink_pad]; |
Mauro Carvalho Chehab | 82ae2a5 | 2015-12-11 18:09:13 -0200 | [diff] [blame] | 654 | link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 655 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 656 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 657 | media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 658 | &link->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 659 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 660 | /* Create the backlink. Backlinks are used to help graph traversal and |
| 661 | * are not reported to userspace. |
| 662 | */ |
Mauro Carvalho Chehab | 23615de | 2015-08-20 08:21:35 -0300 | [diff] [blame] | 663 | backlink = media_add_link(&sink->links); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 664 | if (backlink == NULL) { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 665 | __media_entity_remove_link(source, link); |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 666 | return -ENOMEM; |
| 667 | } |
| 668 | |
| 669 | backlink->source = &source->pads[source_pad]; |
| 670 | backlink->sink = &sink->pads[sink_pad]; |
| 671 | backlink->flags = flags; |
Mauro Carvalho Chehab | 39d1ebc6 | 2015-08-30 09:53:57 -0300 | [diff] [blame] | 672 | backlink->is_backlink = true; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 673 | |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 674 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 675 | media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 676 | &backlink->graph_obj); |
Mauro Carvalho Chehab | 6b6a427 | 2015-08-14 12:54:36 -0300 | [diff] [blame] | 677 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 678 | link->reverse = backlink; |
| 679 | backlink->reverse = link; |
| 680 | |
| 681 | sink->num_backlinks++; |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 682 | sink->num_links++; |
| 683 | source->num_links++; |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame] | 684 | |
| 685 | return 0; |
| 686 | } |
Mauro Carvalho Chehab | 8df00a1 | 2015-08-07 08:14:38 -0300 | [diff] [blame] | 687 | EXPORT_SYMBOL_GPL(media_create_pad_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 688 | |
Mauro Carvalho Chehab | b01cc9c | 2015-12-30 09:45:48 -0200 | [diff] [blame] | 689 | int media_create_pad_links(const struct media_device *mdev, |
| 690 | const u32 source_function, |
| 691 | struct media_entity *source, |
| 692 | const u16 source_pad, |
| 693 | const u32 sink_function, |
| 694 | struct media_entity *sink, |
| 695 | const u16 sink_pad, |
| 696 | u32 flags, |
| 697 | const bool allow_both_undefined) |
| 698 | { |
| 699 | struct media_entity *entity; |
| 700 | unsigned function; |
| 701 | int ret; |
| 702 | |
| 703 | /* Trivial case: 1:1 relation */ |
| 704 | if (source && sink) |
| 705 | return media_create_pad_link(source, source_pad, |
| 706 | sink, sink_pad, flags); |
| 707 | |
| 708 | /* Worse case scenario: n:n relation */ |
| 709 | if (!source && !sink) { |
| 710 | if (!allow_both_undefined) |
| 711 | return 0; |
| 712 | media_device_for_each_entity(source, mdev) { |
| 713 | if (source->function != source_function) |
| 714 | continue; |
| 715 | media_device_for_each_entity(sink, mdev) { |
| 716 | if (sink->function != sink_function) |
| 717 | continue; |
| 718 | ret = media_create_pad_link(source, source_pad, |
| 719 | sink, sink_pad, |
| 720 | flags); |
| 721 | if (ret) |
| 722 | return ret; |
| 723 | flags &= ~(MEDIA_LNK_FL_ENABLED | |
| 724 | MEDIA_LNK_FL_IMMUTABLE); |
| 725 | } |
| 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* Handle 1:n and n:1 cases */ |
| 731 | if (source) |
| 732 | function = sink_function; |
| 733 | else |
| 734 | function = source_function; |
| 735 | |
| 736 | media_device_for_each_entity(entity, mdev) { |
| 737 | if (entity->function != function) |
| 738 | continue; |
| 739 | |
| 740 | if (source) |
| 741 | ret = media_create_pad_link(source, source_pad, |
| 742 | entity, sink_pad, flags); |
| 743 | else |
| 744 | ret = media_create_pad_link(entity, source_pad, |
| 745 | sink, sink_pad, flags); |
| 746 | if (ret) |
| 747 | return ret; |
| 748 | flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); |
| 749 | } |
| 750 | return 0; |
| 751 | } |
| 752 | EXPORT_SYMBOL_GPL(media_create_pad_links); |
| 753 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 754 | void __media_entity_remove_links(struct media_entity *entity) |
| 755 | { |
| 756 | struct media_link *link, *tmp; |
| 757 | |
| 758 | list_for_each_entry_safe(link, tmp, &entity->links, list) |
| 759 | __media_entity_remove_link(entity, link); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 760 | |
| 761 | entity->num_links = 0; |
| 762 | entity->num_backlinks = 0; |
| 763 | } |
| 764 | EXPORT_SYMBOL_GPL(__media_entity_remove_links); |
| 765 | |
| 766 | void media_entity_remove_links(struct media_entity *entity) |
| 767 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 768 | struct media_device *mdev = entity->graph_obj.mdev; |
| 769 | |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 770 | /* Do nothing if the entity is not registered. */ |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 771 | if (mdev == NULL) |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 772 | return; |
| 773 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 774 | mutex_lock(&mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 775 | __media_entity_remove_links(entity); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 776 | mutex_unlock(&mdev->graph_mutex); |
Sylwester Nawrocki | 7349cec | 2013-05-09 08:29:32 -0300 | [diff] [blame] | 777 | } |
| 778 | EXPORT_SYMBOL_GPL(media_entity_remove_links); |
| 779 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 780 | static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) |
| 781 | { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 782 | int ret; |
| 783 | |
| 784 | /* Notify both entities. */ |
| 785 | ret = media_entity_call(link->source->entity, link_setup, |
| 786 | link->source, link->sink, flags); |
| 787 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 788 | return ret; |
| 789 | |
| 790 | ret = media_entity_call(link->sink->entity, link_setup, |
| 791 | link->sink, link->source, flags); |
| 792 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 793 | media_entity_call(link->source->entity, link_setup, |
| 794 | link->source, link->sink, link->flags); |
| 795 | return ret; |
| 796 | } |
| 797 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 798 | link->flags = flags; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 799 | link->reverse->flags = link->flags; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 804 | int __media_entity_setup_link(struct media_link *link, u32 flags) |
| 805 | { |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 806 | const u32 mask = MEDIA_LNK_FL_ENABLED; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 807 | struct media_device *mdev; |
| 808 | struct media_entity *source, *sink; |
| 809 | int ret = -EBUSY; |
| 810 | |
| 811 | if (link == NULL) |
| 812 | return -EINVAL; |
| 813 | |
Laurent Pinchart | 7a6f0b2 | 2011-03-11 11:34:35 -0300 | [diff] [blame] | 814 | /* The non-modifiable link flags must not be modified. */ |
| 815 | if ((link->flags & ~mask) != (flags & ~mask)) |
| 816 | return -EINVAL; |
| 817 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 818 | if (link->flags & MEDIA_LNK_FL_IMMUTABLE) |
| 819 | return link->flags == flags ? 0 : -EINVAL; |
| 820 | |
| 821 | if (link->flags == flags) |
| 822 | return 0; |
| 823 | |
| 824 | source = link->source->entity; |
| 825 | sink = link->sink->entity; |
| 826 | |
Laurent Pinchart | e02188c | 2010-08-25 09:00:41 -0300 | [diff] [blame] | 827 | if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) && |
| 828 | (source->stream_count || sink->stream_count)) |
| 829 | return -EBUSY; |
| 830 | |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 831 | mdev = source->graph_obj.mdev; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 832 | |
Laurent Pinchart | 68429f5 | 2015-11-03 00:27:51 -0200 | [diff] [blame] | 833 | if (mdev->ops && mdev->ops->link_notify) { |
| 834 | ret = mdev->ops->link_notify(link, flags, |
| 835 | MEDIA_DEV_NOTIFY_PRE_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 836 | if (ret < 0) |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | ret = __media_entity_setup_link_notify(link, flags); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 841 | |
Laurent Pinchart | 68429f5 | 2015-11-03 00:27:51 -0200 | [diff] [blame] | 842 | if (mdev->ops && mdev->ops->link_notify) |
| 843 | mdev->ops->link_notify(link, flags, |
| 844 | MEDIA_DEV_NOTIFY_POST_LINK_CH); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 845 | |
| 846 | return ret; |
| 847 | } |
Shuah Khan | efc7027 | 2016-02-11 21:41:23 -0200 | [diff] [blame] | 848 | EXPORT_SYMBOL_GPL(__media_entity_setup_link); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 849 | |
| 850 | int media_entity_setup_link(struct media_link *link, u32 flags) |
| 851 | { |
| 852 | int ret; |
| 853 | |
Mauro Carvalho Chehab | 5c883ed | 2015-12-15 07:58:18 -0200 | [diff] [blame] | 854 | mutex_lock(&link->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 855 | ret = __media_entity_setup_link(link, flags); |
Mauro Carvalho Chehab | 5c883ed | 2015-12-15 07:58:18 -0200 | [diff] [blame] | 856 | mutex_unlock(&link->graph_obj.mdev->graph_mutex); |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 857 | |
| 858 | return ret; |
| 859 | } |
| 860 | EXPORT_SYMBOL_GPL(media_entity_setup_link); |
| 861 | |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 862 | struct media_link * |
| 863 | media_entity_find_link(struct media_pad *source, struct media_pad *sink) |
| 864 | { |
| 865 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 866 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 867 | list_for_each_entry(link, &source->entity->links, list) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 868 | if (link->source->entity == source->entity && |
| 869 | link->source->index == source->index && |
| 870 | link->sink->entity == sink->entity && |
| 871 | link->sink->index == sink->index) |
| 872 | return link; |
| 873 | } |
| 874 | |
| 875 | return NULL; |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(media_entity_find_link); |
| 878 | |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 879 | struct media_pad *media_entity_remote_pad(struct media_pad *pad) |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 880 | { |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 881 | struct media_link *link; |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 882 | |
Mauro Carvalho Chehab | 57208e5 | 2015-08-07 06:55:40 -0300 | [diff] [blame] | 883 | list_for_each_entry(link, &pad->entity->links, list) { |
Laurent Pinchart | 97548ed | 2009-12-09 08:40:03 -0300 | [diff] [blame] | 884 | if (!(link->flags & MEDIA_LNK_FL_ENABLED)) |
| 885 | continue; |
| 886 | |
| 887 | if (link->source == pad) |
| 888 | return link->sink; |
| 889 | |
| 890 | if (link->sink == pad) |
| 891 | return link->source; |
| 892 | } |
| 893 | |
| 894 | return NULL; |
| 895 | |
| 896 | } |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 897 | EXPORT_SYMBOL_GPL(media_entity_remote_pad); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 898 | |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 899 | static void media_interface_init(struct media_device *mdev, |
| 900 | struct media_interface *intf, |
| 901 | u32 gobj_type, |
| 902 | u32 intf_type, u32 flags) |
| 903 | { |
| 904 | intf->type = intf_type; |
| 905 | intf->flags = flags; |
| 906 | INIT_LIST_HEAD(&intf->links); |
| 907 | |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 908 | media_gobj_create(mdev, gobj_type, &intf->graph_obj); |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 909 | } |
| 910 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 911 | /* Functions related to the media interface via device nodes */ |
| 912 | |
| 913 | struct media_intf_devnode *media_devnode_create(struct media_device *mdev, |
| 914 | u32 type, u32 flags, |
Mauro Carvalho Chehab | 0b3b72df9 | 2015-09-09 08:19:25 -0300 | [diff] [blame] | 915 | u32 major, u32 minor) |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 916 | { |
| 917 | struct media_intf_devnode *devnode; |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 918 | |
Mauro Carvalho Chehab | 0b3b72df9 | 2015-09-09 08:19:25 -0300 | [diff] [blame] | 919 | devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 920 | if (!devnode) |
| 921 | return NULL; |
| 922 | |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 923 | devnode->major = major; |
| 924 | devnode->minor = minor; |
| 925 | |
Mauro Carvalho Chehab | 1283f84 | 2015-08-28 15:43:36 -0300 | [diff] [blame] | 926 | media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE, |
| 927 | type, flags); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 928 | |
| 929 | return devnode; |
| 930 | } |
| 931 | EXPORT_SYMBOL_GPL(media_devnode_create); |
| 932 | |
| 933 | void media_devnode_remove(struct media_intf_devnode *devnode) |
| 934 | { |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 935 | media_remove_intf_links(&devnode->intf); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 936 | media_gobj_destroy(&devnode->intf.graph_obj); |
Mauro Carvalho Chehab | 27e543f | 2015-08-20 09:07:34 -0300 | [diff] [blame] | 937 | kfree(devnode); |
| 938 | } |
| 939 | EXPORT_SYMBOL_GPL(media_devnode_remove); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 940 | |
| 941 | struct media_link *media_create_intf_link(struct media_entity *entity, |
| 942 | struct media_interface *intf, |
| 943 | u32 flags) |
| 944 | { |
| 945 | struct media_link *link; |
| 946 | |
| 947 | link = media_add_link(&intf->links); |
| 948 | if (link == NULL) |
| 949 | return NULL; |
| 950 | |
| 951 | link->intf = intf; |
| 952 | link->entity = entity; |
Mauro Carvalho Chehab | 82ae2a5 | 2015-12-11 18:09:13 -0200 | [diff] [blame] | 953 | link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK; |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 954 | |
| 955 | /* Initialize graph object embedded at the new link */ |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 956 | media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK, |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 957 | &link->graph_obj); |
| 958 | |
| 959 | return link; |
| 960 | } |
| 961 | EXPORT_SYMBOL_GPL(media_create_intf_link); |
| 962 | |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 963 | void __media_remove_intf_link(struct media_link *link) |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 964 | { |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 965 | list_del(&link->list); |
Mauro Carvalho Chehab | c350ef8 | 2015-12-11 11:55:40 -0200 | [diff] [blame] | 966 | media_gobj_destroy(&link->graph_obj); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 967 | kfree(link); |
| 968 | } |
Mauro Carvalho Chehab | d47109f | 2015-08-29 21:23:44 -0300 | [diff] [blame] | 969 | EXPORT_SYMBOL_GPL(__media_remove_intf_link); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 970 | |
| 971 | void media_remove_intf_link(struct media_link *link) |
| 972 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 973 | struct media_device *mdev = link->graph_obj.mdev; |
| 974 | |
| 975 | /* Do nothing if the intf is not registered. */ |
| 976 | if (mdev == NULL) |
| 977 | return; |
| 978 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 979 | mutex_lock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 980 | __media_remove_intf_link(link); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 981 | mutex_unlock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 86e2662 | 2015-08-07 10:36:25 -0300 | [diff] [blame] | 982 | } |
| 983 | EXPORT_SYMBOL_GPL(media_remove_intf_link); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 984 | |
| 985 | void __media_remove_intf_links(struct media_interface *intf) |
| 986 | { |
| 987 | struct media_link *link, *tmp; |
| 988 | |
| 989 | list_for_each_entry_safe(link, tmp, &intf->links, list) |
| 990 | __media_remove_intf_link(link); |
| 991 | |
| 992 | } |
| 993 | EXPORT_SYMBOL_GPL(__media_remove_intf_links); |
| 994 | |
| 995 | void media_remove_intf_links(struct media_interface *intf) |
| 996 | { |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 997 | struct media_device *mdev = intf->graph_obj.mdev; |
| 998 | |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 999 | /* Do nothing if the intf is not registered. */ |
Mauro Carvalho Chehab | cc4a825 | 2015-12-15 08:01:13 -0200 | [diff] [blame] | 1000 | if (mdev == NULL) |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1001 | return; |
| 1002 | |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1003 | mutex_lock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1004 | __media_remove_intf_links(intf); |
Mauro Carvalho Chehab | e2c91d4 | 2016-04-06 10:55:24 -0300 | [diff] [blame] | 1005 | mutex_unlock(&mdev->graph_mutex); |
Mauro Carvalho Chehab | 7c4696a | 2015-08-24 08:46:46 -0300 | [diff] [blame] | 1006 | } |
| 1007 | EXPORT_SYMBOL_GPL(media_remove_intf_links); |