Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Media Controller ancillary functions |
| 3 | * |
Mauro Carvalho Chehab | 5dc8a86 | 2016-06-14 15:17:40 -0300 | [diff] [blame] | 4 | * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org> |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 5 | * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com> |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 6 | * Copyright (C) 2006-2010 Nokia Corporation |
| 7 | * Copyright (c) 2016 Intel Corporation. |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 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 as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 21 | #include <linux/pci.h> |
| 22 | #include <linux/usb.h> |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 23 | #include <media/media-device.h> |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 24 | #include <media/media-entity.h> |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 25 | #include <media/v4l2-fh.h> |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 26 | #include <media/v4l2-mc.h> |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 27 | #include <media/v4l2-subdev.h> |
| 28 | #include <media/media-device.h> |
| 29 | #include <media/v4l2-mc.h> |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 30 | #include <media/videobuf2-core.h> |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 31 | |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 32 | int v4l2_mc_create_media_graph(struct media_device *mdev) |
| 33 | |
| 34 | { |
| 35 | struct media_entity *entity; |
Mauro Carvalho Chehab | 153d41a | 2016-02-12 08:07:59 -0200 | [diff] [blame] | 36 | struct media_entity *if_vid = NULL, *if_aud = NULL; |
Linus Torvalds | 17084b7 | 2016-04-03 07:03:49 -0500 | [diff] [blame] | 37 | struct media_entity *tuner = NULL, *decoder = NULL; |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 38 | struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL; |
| 39 | bool is_webcam = false; |
| 40 | u32 flags; |
| 41 | int ret; |
| 42 | |
| 43 | if (!mdev) |
| 44 | return 0; |
| 45 | |
| 46 | media_device_for_each_entity(entity, mdev) { |
| 47 | switch (entity->function) { |
| 48 | case MEDIA_ENT_F_IF_VID_DECODER: |
| 49 | if_vid = entity; |
| 50 | break; |
| 51 | case MEDIA_ENT_F_IF_AUD_DECODER: |
| 52 | if_aud = entity; |
| 53 | break; |
| 54 | case MEDIA_ENT_F_TUNER: |
| 55 | tuner = entity; |
| 56 | break; |
| 57 | case MEDIA_ENT_F_ATV_DECODER: |
| 58 | decoder = entity; |
| 59 | break; |
| 60 | case MEDIA_ENT_F_IO_V4L: |
| 61 | io_v4l = entity; |
| 62 | break; |
| 63 | case MEDIA_ENT_F_IO_VBI: |
| 64 | io_vbi = entity; |
| 65 | break; |
| 66 | case MEDIA_ENT_F_IO_SWRADIO: |
| 67 | io_swradio = entity; |
| 68 | break; |
| 69 | case MEDIA_ENT_F_CAM_SENSOR: |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 70 | is_webcam = true; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* It should have at least one I/O entity */ |
| 76 | if (!io_v4l && !io_vbi && !io_swradio) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | /* |
| 80 | * Here, webcams are modelled on a very simple way: the sensor is |
| 81 | * connected directly to the I/O entity. All dirty details, like |
| 82 | * scaler and crop HW are hidden. While such mapping is not enough |
| 83 | * for mc-centric hardware, it is enough for v4l2 interface centric |
| 84 | * PC-consumer's hardware. |
| 85 | */ |
| 86 | if (is_webcam) { |
| 87 | if (!io_v4l) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | media_device_for_each_entity(entity, mdev) { |
| 91 | if (entity->function != MEDIA_ENT_F_CAM_SENSOR) |
| 92 | continue; |
| 93 | ret = media_create_pad_link(entity, 0, |
| 94 | io_v4l, 0, |
| 95 | MEDIA_LNK_FL_ENABLED); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | } |
| 99 | if (!decoder) |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* The device isn't a webcam. So, it should have a decoder */ |
| 104 | if (!decoder) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | /* Link the tuner and IF video output pads */ |
| 108 | if (tuner) { |
| 109 | if (if_vid) { |
| 110 | ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT, |
| 111 | if_vid, |
| 112 | IF_VID_DEC_PAD_IF_INPUT, |
| 113 | MEDIA_LNK_FL_ENABLED); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | ret = media_create_pad_link(if_vid, IF_VID_DEC_PAD_OUT, |
| 117 | decoder, DEMOD_PAD_IF_INPUT, |
| 118 | MEDIA_LNK_FL_ENABLED); |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | } else { |
| 122 | ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT, |
| 123 | decoder, DEMOD_PAD_IF_INPUT, |
| 124 | MEDIA_LNK_FL_ENABLED); |
| 125 | if (ret) |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | if (if_aud) { |
| 130 | ret = media_create_pad_link(tuner, TUNER_PAD_AUD_OUT, |
| 131 | if_aud, |
| 132 | IF_AUD_DEC_PAD_IF_INPUT, |
| 133 | MEDIA_LNK_FL_ENABLED); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | } else { |
| 137 | if_aud = tuner; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /* Create demod to V4L, VBI and SDR radio links */ |
| 143 | if (io_v4l) { |
| 144 | ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT, |
| 145 | io_v4l, 0, |
| 146 | MEDIA_LNK_FL_ENABLED); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | if (io_swradio) { |
| 152 | ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT, |
| 153 | io_swradio, 0, |
| 154 | MEDIA_LNK_FL_ENABLED); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | if (io_vbi) { |
| 160 | ret = media_create_pad_link(decoder, DEMOD_PAD_VBI_OUT, |
| 161 | io_vbi, 0, |
| 162 | MEDIA_LNK_FL_ENABLED); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* Create links for the media connectors */ |
| 168 | flags = MEDIA_LNK_FL_ENABLED; |
| 169 | media_device_for_each_entity(entity, mdev) { |
| 170 | switch (entity->function) { |
| 171 | case MEDIA_ENT_F_CONN_RF: |
| 172 | if (!tuner) |
| 173 | continue; |
| 174 | |
| 175 | ret = media_create_pad_link(entity, 0, tuner, |
| 176 | TUNER_PAD_RF_INPUT, |
| 177 | flags); |
| 178 | break; |
| 179 | case MEDIA_ENT_F_CONN_SVIDEO: |
| 180 | case MEDIA_ENT_F_CONN_COMPOSITE: |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 181 | ret = media_create_pad_link(entity, 0, decoder, |
| 182 | DEMOD_PAD_IF_INPUT, |
| 183 | flags); |
| 184 | break; |
| 185 | default: |
| 186 | continue; |
| 187 | } |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | flags = 0; |
| 192 | } |
Mauro Carvalho Chehab | 9822f41 | 2016-03-02 10:11:41 -0300 | [diff] [blame] | 193 | |
Mauro Carvalho Chehab | 54d0dba | 2016-02-05 07:02:43 -0200 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph); |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 197 | |
| 198 | int v4l_enable_media_source(struct video_device *vdev) |
| 199 | { |
| 200 | struct media_device *mdev = vdev->entity.graph_obj.mdev; |
Shuah Khan | 90cd366 | 2016-11-29 21:59:54 -0200 | [diff] [blame] | 201 | int ret = 0, err; |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 202 | |
Shuah Khan | 90cd366 | 2016-11-29 21:59:54 -0200 | [diff] [blame] | 203 | if (!mdev) |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 204 | return 0; |
Shuah Khan | 90cd366 | 2016-11-29 21:59:54 -0200 | [diff] [blame] | 205 | |
| 206 | mutex_lock(&mdev->graph_mutex); |
| 207 | if (!mdev->enable_source) |
| 208 | goto end; |
| 209 | err = mdev->enable_source(&vdev->entity, &vdev->pipe); |
| 210 | if (err) |
| 211 | ret = -EBUSY; |
| 212 | end: |
| 213 | mutex_unlock(&mdev->graph_mutex); |
| 214 | return ret; |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 215 | } |
| 216 | EXPORT_SYMBOL_GPL(v4l_enable_media_source); |
| 217 | |
| 218 | void v4l_disable_media_source(struct video_device *vdev) |
| 219 | { |
| 220 | struct media_device *mdev = vdev->entity.graph_obj.mdev; |
| 221 | |
Shuah Khan | 90cd366 | 2016-11-29 21:59:54 -0200 | [diff] [blame] | 222 | if (mdev) { |
| 223 | mutex_lock(&mdev->graph_mutex); |
| 224 | if (mdev->disable_source) |
| 225 | mdev->disable_source(&vdev->entity); |
| 226 | mutex_unlock(&mdev->graph_mutex); |
| 227 | } |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 228 | } |
| 229 | EXPORT_SYMBOL_GPL(v4l_disable_media_source); |
| 230 | |
| 231 | int v4l_vb2q_enable_media_source(struct vb2_queue *q) |
| 232 | { |
| 233 | struct v4l2_fh *fh = q->owner; |
| 234 | |
Shuah Khan | 7e53898 | 2016-03-03 23:24:58 -0300 | [diff] [blame] | 235 | if (fh && fh->vdev) |
| 236 | return v4l_enable_media_source(fh->vdev); |
| 237 | return 0; |
Shuah Khan | d0a164f | 2016-02-11 21:41:25 -0200 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source); |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 240 | |
| 241 | /* ----------------------------------------------------------------------------- |
| 242 | * Pipeline power management |
| 243 | * |
| 244 | * Entities must be powered up when part of a pipeline that contains at least |
| 245 | * one open video device node. |
| 246 | * |
| 247 | * To achieve this use the entity use_count field to track the number of users. |
| 248 | * For entities corresponding to video device nodes the use_count field stores |
| 249 | * the users count of the node. For entities corresponding to subdevs the |
| 250 | * use_count field stores the total number of users of all video device nodes |
| 251 | * in the pipeline. |
| 252 | * |
| 253 | * The v4l2_pipeline_pm_use() function must be called in the open() and |
| 254 | * close() handlers of video device nodes. It increments or decrements the use |
| 255 | * count of all subdev entities in the pipeline. |
| 256 | * |
| 257 | * To react to link management on powered pipelines, the link setup notification |
| 258 | * callback updates the use count of all entities in the source and sink sides |
| 259 | * of the link. |
| 260 | */ |
| 261 | |
| 262 | /* |
| 263 | * pipeline_pm_use_count - Count the number of users of a pipeline |
| 264 | * @entity: The entity |
| 265 | * |
| 266 | * Return the total number of users of all video device nodes in the pipeline. |
| 267 | */ |
| 268 | static int pipeline_pm_use_count(struct media_entity *entity, |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 269 | struct media_graph *graph) |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 270 | { |
| 271 | int use = 0; |
| 272 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 273 | media_graph_walk_start(graph, entity); |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 274 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 275 | while ((entity = media_graph_walk_next(graph))) { |
Laurent Pinchart | 45b4687 | 2016-02-29 08:45:45 -0300 | [diff] [blame] | 276 | if (is_media_entity_v4l2_video_device(entity)) |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 277 | use += entity->use_count; |
| 278 | } |
| 279 | |
| 280 | return use; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * pipeline_pm_power_one - Apply power change to an entity |
| 285 | * @entity: The entity |
| 286 | * @change: Use count change |
| 287 | * |
| 288 | * Change the entity use count by @change. If the entity is a subdev update its |
| 289 | * power state by calling the core::s_power operation when the use count goes |
| 290 | * from 0 to != 0 or from != 0 to 0. |
| 291 | * |
| 292 | * Return 0 on success or a negative error code on failure. |
| 293 | */ |
| 294 | static int pipeline_pm_power_one(struct media_entity *entity, int change) |
| 295 | { |
| 296 | struct v4l2_subdev *subdev; |
| 297 | int ret; |
| 298 | |
| 299 | subdev = is_media_entity_v4l2_subdev(entity) |
| 300 | ? media_entity_to_v4l2_subdev(entity) : NULL; |
| 301 | |
| 302 | if (entity->use_count == 0 && change > 0 && subdev != NULL) { |
| 303 | ret = v4l2_subdev_call(subdev, core, s_power, 1); |
| 304 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | entity->use_count += change; |
| 309 | WARN_ON(entity->use_count < 0); |
| 310 | |
| 311 | if (entity->use_count == 0 && change < 0 && subdev != NULL) |
| 312 | v4l2_subdev_call(subdev, core, s_power, 0); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * pipeline_pm_power - Apply power change to all entities in a pipeline |
| 319 | * @entity: The entity |
| 320 | * @change: Use count change |
| 321 | * |
| 322 | * Walk the pipeline to update the use count and the power state of all non-node |
| 323 | * entities. |
| 324 | * |
| 325 | * Return 0 on success or a negative error code on failure. |
| 326 | */ |
| 327 | static int pipeline_pm_power(struct media_entity *entity, int change, |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 328 | struct media_graph *graph) |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 329 | { |
| 330 | struct media_entity *first = entity; |
| 331 | int ret = 0; |
| 332 | |
| 333 | if (!change) |
| 334 | return 0; |
| 335 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 336 | media_graph_walk_start(graph, entity); |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 337 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 338 | while (!ret && (entity = media_graph_walk_next(graph))) |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 339 | if (is_media_entity_v4l2_subdev(entity)) |
| 340 | ret = pipeline_pm_power_one(entity, change); |
| 341 | |
| 342 | if (!ret) |
| 343 | return ret; |
| 344 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 345 | media_graph_walk_start(graph, first); |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 346 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 347 | while ((first = media_graph_walk_next(graph)) |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 348 | && first != entity) |
| 349 | if (is_media_entity_v4l2_subdev(first)) |
| 350 | pipeline_pm_power_one(first, -change); |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | int v4l2_pipeline_pm_use(struct media_entity *entity, int use) |
| 356 | { |
| 357 | struct media_device *mdev = entity->graph_obj.mdev; |
| 358 | int change = use ? 1 : -1; |
| 359 | int ret; |
| 360 | |
| 361 | mutex_lock(&mdev->graph_mutex); |
| 362 | |
| 363 | /* Apply use count to node. */ |
| 364 | entity->use_count += change; |
| 365 | WARN_ON(entity->use_count < 0); |
| 366 | |
| 367 | /* Apply power change to connected non-nodes. */ |
| 368 | ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk); |
| 369 | if (ret < 0) |
| 370 | entity->use_count -= change; |
| 371 | |
| 372 | mutex_unlock(&mdev->graph_mutex); |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_use); |
| 377 | |
| 378 | int v4l2_pipeline_link_notify(struct media_link *link, u32 flags, |
| 379 | unsigned int notification) |
| 380 | { |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 381 | struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk; |
Sakari Ailus | 7641379 | 2016-02-21 13:25:09 -0300 | [diff] [blame] | 382 | struct media_entity *source = link->source->entity; |
| 383 | struct media_entity *sink = link->sink->entity; |
| 384 | int source_use; |
| 385 | int sink_use; |
| 386 | int ret = 0; |
| 387 | |
| 388 | source_use = pipeline_pm_use_count(source, graph); |
| 389 | sink_use = pipeline_pm_use_count(sink, graph); |
| 390 | |
| 391 | if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH && |
| 392 | !(flags & MEDIA_LNK_FL_ENABLED)) { |
| 393 | /* Powering off entities is assumed to never fail. */ |
| 394 | pipeline_pm_power(source, -sink_use, graph); |
| 395 | pipeline_pm_power(sink, -source_use, graph); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH && |
| 400 | (flags & MEDIA_LNK_FL_ENABLED)) { |
| 401 | |
| 402 | ret = pipeline_pm_power(source, sink_use, graph); |
| 403 | if (ret < 0) |
| 404 | return ret; |
| 405 | |
| 406 | ret = pipeline_pm_power(sink, source_use, graph); |
| 407 | if (ret < 0) |
| 408 | pipeline_pm_power(source, -sink_use, graph); |
| 409 | } |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify); |