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