/* * pipeline_pm_power - Apply power change to all entities in a pipeline * @entity: The entity * @change: Use count change * * Walk the pipeline to update the use count and the power state of all non-node * entities. * * Return 0 on success or a negative error code on failure. */ static int pipeline_pm_power(struct media_entity *entity, int change, struct media_entity_graph *graph) { struct media_entity *first = entity; int ret = 0; if (!change) return 0; media_entity_graph_walk_start(graph, entity); while (!ret && (entity = media_entity_graph_walk_next(graph))) if (is_media_entity_v4l2_subdev(entity)) ret = pipeline_pm_power_one(entity, change); if (!ret) return ret; media_entity_graph_walk_start(graph, first); while ((first = media_entity_graph_walk_next(graph)) && first != entity) if (is_media_entity_v4l2_subdev(first)) pipeline_pm_power_one(first, -change); return ret; }
/* * isp_pipeline_pm_power - Apply power change to all entities in a pipeline * @entity: The entity * @change: Use count change * * Walk the pipeline to update the use count and the power state of all non-node * entities. * * Return 0 on success or a negative error code on failure. */ static int isp_pipeline_pm_power(struct media_entity *entity, int change) { struct media_entity_graph graph; struct media_entity *first = entity; int ret = 0; if (!change) return 0; media_entity_graph_walk_start(&graph, entity); while (!ret && (entity = media_entity_graph_walk_next(&graph))) if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) ret = isp_pipeline_pm_power_one(entity, change); if (!ret) return 0; media_entity_graph_walk_start(&graph, first); while ((first = media_entity_graph_walk_next(&graph)) && first != entity) if (media_entity_type(first) != MEDIA_ENT_T_DEVNODE) isp_pipeline_pm_power_one(first, -change); return ret; }
/* Return a pointer to the ISP video instance at the far end of the pipeline. */ static struct isp_video * isp_video_far_end(struct isp_video *video) { struct media_entity_graph graph; struct media_entity *entity = &video->video.entity; struct media_device *mdev = entity->parent; struct isp_video *far_end = NULL; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { if (entity == &video->video.entity) continue; if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) continue; far_end = to_isp_video(media_entity_to_video_device(entity)); if (far_end->type != video->type) break; far_end = NULL; } mutex_unlock(&mdev->graph_mutex); return far_end; }
/* make a note of pipeline details */ static void vpfe_prepare_pipeline(struct vpfe_video_device *video) { struct media_entity *entity = &video->video_dev.entity; struct media_device *mdev = entity->parent; struct vpfe_pipeline *pipe = &video->pipe; struct vpfe_video_device *far_end = NULL; struct media_entity_graph graph; pipe->input_num = 0; pipe->output_num = 0; if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) pipe->inputs[pipe->input_num++] = video; else pipe->outputs[pipe->output_num++] = video; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { if (entity == &video->video_dev.entity) continue; if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) continue; far_end = to_vpfe_video(media_entity_to_video_device(entity)); if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) pipe->inputs[pipe->input_num++] = far_end; else pipe->outputs[pipe->output_num++] = far_end; } mutex_unlock(&mdev->graph_mutex); }
/* * vpfe_pipeline_disable() - Disable streaming on a pipeline * @vpfe_dev: vpfe device * @pipe: VPFE pipeline * * Walk the entities chain starting at the pipeline output video node and stop * all modules in the chain. * * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module * can't be stopped. */ static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe) { struct media_entity_graph graph; struct media_entity *entity; struct v4l2_subdev *subdev; struct media_device *mdev; int ret = 0; if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS) entity = vpfe_get_input_entity(pipe->outputs[0]); else entity = &pipe->inputs[0]->video_dev.entity; mdev = entity->parent; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE) continue; subdev = media_entity_to_v4l2_subdev(entity); ret = v4l2_subdev_call(subdev, video, s_stream, 0); if (ret < 0 && ret != -ENOIOCTLCMD) break; } mutex_unlock(&mdev->graph_mutex); return ret ? -ETIMEDOUT : 0; }
/* Return a pointer to the ISP video instance at the far end of the pipeline. */ static int isp_video_get_graph_data(struct isp_video *video, struct isp_pipeline *pipe) { struct media_entity_graph graph; struct media_entity *entity = &video->video.entity; struct media_device *mdev = entity->graph_obj.mdev; struct isp_video *far_end = NULL; int ret; mutex_lock(&mdev->graph_mutex); ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev); if (ret) { mutex_unlock(&mdev->graph_mutex); return ret; } media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { struct isp_video *__video; media_entity_enum_set(&pipe->ent_enum, entity); if (far_end != NULL) continue; if (entity == &video->video.entity) continue; if (!is_media_entity_v4l2_video_device(entity)) continue; __video = to_isp_video(media_entity_to_video_device(entity)); if (__video->type != video->type) far_end = __video; } mutex_unlock(&mdev->graph_mutex); media_entity_graph_walk_cleanup(&graph); if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { pipe->input = far_end; pipe->output = video; } else { if (far_end == NULL) return -EPIPE; pipe->input = video; pipe->output = far_end; } return 0; }
/* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */ static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable, struct media_entity_graph *graph) { struct media_entity *entity_err = entity; int ret; /* * Walk current graph and call the pipeline open/close routine for each * opened video node that belongs to the graph of entities connected * through active links. This is needed as we cannot power on/off the * subdevs in random order. */ media_entity_graph_walk_start(graph, entity); while ((entity = media_entity_graph_walk_next(graph))) { if (!is_media_entity_v4l2_io(entity)) continue; ret = __fimc_md_modify_pipeline(entity, enable); if (ret < 0) goto err; } return 0; err: media_entity_graph_walk_start(graph, entity_err); while ((entity_err = media_entity_graph_walk_next(graph))) { if (!is_media_entity_v4l2_io(entity_err)) continue; __fimc_md_modify_pipeline(entity_err, !enable); if (entity_err == entity) break; } return ret; }
/* * isp_pipeline_pm_use_count - Count the number of users of a pipeline * @entity: The entity * * Return the total number of users of all video device nodes in the pipeline. */ static int isp_pipeline_pm_use_count(struct media_entity *entity) { struct media_entity_graph graph; int use = 0; media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE) use += entity->use_count; } return use; }
/* * pipeline_pm_use_count - Count the number of users of a pipeline * @entity: The entity * * Return the total number of users of all video device nodes in the pipeline. */ static int pipeline_pm_use_count(struct media_entity *entity, struct media_entity_graph *graph) { int use = 0; media_entity_graph_walk_start(graph, entity); while ((entity = media_entity_graph_walk_next(graph))) { if (is_media_entity_v4l2_video_device(entity)) use += entity->use_count; } return use; }
void media_entity_pipeline_stop(struct media_entity *entity) { struct media_device *mdev = entity->parent; struct media_entity_graph graph; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { entity->stream_count--; if (entity->stream_count == 0) entity->pipe = NULL; } mutex_unlock(&mdev->graph_mutex); }
/* Return a pointer to the ISP video instance at the far end of the pipeline. */ static int isp_video_get_graph_data(struct isp_video *video, struct isp_pipeline *pipe) { struct media_entity_graph graph; struct media_entity *entity = &video->video.entity; struct media_device *mdev = entity->parent; struct isp_video *far_end = NULL; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { struct isp_video *__video; pipe->entities |= 1 << entity->id; if (far_end != NULL) continue; if (entity == &video->video.entity) continue; if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE) continue; __video = to_isp_video(media_entity_to_video_device(entity)); if (__video->type != video->type) far_end = __video; } mutex_unlock(&mdev->graph_mutex); if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { pipe->input = far_end; pipe->output = video; } else { if (far_end == NULL) return -EPIPE; pipe->input = video; pipe->output = far_end; } return 0; }
/** * fimc_pipeline_prepare - update pipeline information with subdevice pointers * @fimc: fimc device terminating the pipeline * * Caller holds the graph mutex. */ void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me) { struct media_entity_graph graph; struct v4l2_subdev *sd; media_entity_graph_walk_start(&graph, me); while ((me = media_entity_graph_walk_next(&graph))) { if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV) continue; sd = media_entity_to_v4l2_subdev(me); if (sd->grp_id == SENSOR_GROUP_ID) fimc->pipeline.sensor = sd; else if (sd->grp_id == CSIS_GROUP_ID) fimc->pipeline.csis = sd; } }
void media_entity_pipeline_start(struct media_entity *entity, struct media_pipeline *pipe) { struct media_device *mdev = entity->parent; struct media_entity_graph graph; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { entity->stream_count++; WARN_ON(entity->pipe && entity->pipe != pipe); entity->pipe = pipe; } mutex_unlock(&mdev->graph_mutex); }
void gsc_cap_pipeline_prepare(struct gsc_dev *gsc, struct media_entity *me) { struct media_entity_graph graph; struct v4l2_subdev *sd; media_entity_graph_walk_start(&graph, me); while ((me = media_entity_graph_walk_next(&graph))) { gsc_dbg("me->name : %s", me->name); if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV) continue; sd = media_entity_to_v4l2_subdev(me); switch (sd->grp_id) { case GSC_CAP_GRP_ID: gsc->pipeline.sd_gsc = sd; break; case FLITE_GRP_ID: gsc->pipeline.flite = sd; break; case SENSOR_GRP_ID: gsc->pipeline.sensor = sd; break; case CSIS_GRP_ID: gsc->pipeline.csis = sd; break; case FIMD_GRP_ID: gsc->pipeline.disp = sd; break; default: gsc_err("Unsupported group id"); break; } } gsc_dbg("gsc->pipeline.sd_gsc : 0x%p", gsc->pipeline.sd_gsc); gsc_dbg("gsc->pipeline.flite : 0x%p", gsc->pipeline.flite); gsc_dbg("gsc->pipeline.sensor : 0x%p", gsc->pipeline.sensor); gsc_dbg("gsc->pipeline.csis : 0x%p", gsc->pipeline.csis); gsc_dbg("gsc->pipeline.disp : 0x%p", gsc->pipeline.disp); }
static int xvip_pipeline_validate(struct xvip_pipeline *pipe, struct xvip_dma *start) { struct media_entity_graph graph; struct media_entity *entity = &start->video.entity; struct media_device *mdev = entity->parent; unsigned int num_inputs = 0; unsigned int num_outputs = 0; mutex_lock(&mdev->graph_mutex); /* Walk the graph to locate the video nodes. */ media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { struct xvip_dma *dma; if (entity->type != MEDIA_ENT_T_DEVNODE_V4L) continue; dma = to_xvip_dma(media_entity_to_video_device(entity)); if (dma->pad.flags & MEDIA_PAD_FL_SINK) { pipe->output = dma; num_outputs++; } else { num_inputs++; } } mutex_unlock(&mdev->graph_mutex); /* We need exactly one output and zero or one input. */ if (num_outputs != 1 || num_inputs > 1) return -EPIPE; pipe->num_dmas = num_inputs + num_outputs; return 0; }
/** * media_entity_pipeline_start - Mark a pipeline as streaming * @entity: Starting entity * @pipe: Media pipeline to be assigned to all entities in the pipeline. * * Mark all entities connected to a given entity through enabled links, either * directly or indirectly, as streaming. The given pipeline object is assigned to * every entity in the pipeline and stored in the media_entity pipe field. * * Calls to this function can be nested, in which case the same number of * media_entity_pipeline_stop() calls will be required to stop streaming. The * pipeline pointer must be identical for all nested calls to * media_entity_pipeline_start(). */ __must_check int media_entity_pipeline_start(struct media_entity *entity, struct media_pipeline *pipe) { struct media_device *mdev = entity->parent; struct media_entity_graph graph; struct media_entity *entity_err = entity; int ret; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { DECLARE_BITMAP(active, entity->num_pads); DECLARE_BITMAP(has_no_links, entity->num_pads); unsigned int i; entity->stream_count++; WARN_ON(entity->pipe && entity->pipe != pipe); entity->pipe = pipe; /* Already streaming --- no need to check. */ if (entity->stream_count > 1) continue; if (!entity->ops || !entity->ops->link_validate) continue; bitmap_zero(active, entity->num_pads); bitmap_fill(has_no_links, entity->num_pads); for (i = 0; i < entity->num_links; i++) { struct media_link *link = &entity->links[i]; struct media_pad *pad = link->sink->entity == entity ? link->sink : link->source; /* Mark that a pad is connected by a link. */ bitmap_clear(has_no_links, pad->index, 1); /* * Pads that either do not need to connect or * are connected through an enabled link are * fine. */ if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) || link->flags & MEDIA_LNK_FL_ENABLED) bitmap_set(active, pad->index, 1); /* * Link validation will only take place for * sink ends of the link that are enabled. */ if (link->sink != pad || !(link->flags & MEDIA_LNK_FL_ENABLED)) continue; ret = entity->ops->link_validate(link); if (ret < 0 && ret != -ENOIOCTLCMD) goto error; } /* Either no links or validated links are fine. */ bitmap_or(active, active, has_no_links, entity->num_pads); if (!bitmap_full(active, entity->num_pads)) { ret = -EPIPE; goto error; } } mutex_unlock(&mdev->graph_mutex); return 0; error: /* * Link validation on graph failed. We revert what we did and * return the error. */ media_entity_graph_walk_start(&graph, entity_err); while ((entity_err = media_entity_graph_walk_next(&graph))) { entity_err->stream_count--; if (entity_err->stream_count == 0) entity_err->pipe = NULL; /* * We haven't increased stream_count further than this * so we quit here. */ if (entity_err == entity) break; } mutex_unlock(&mdev->graph_mutex); return ret; }
/* * Stream management * * Every ISS pipeline has a single input and a single output. The input can be * either a sensor or a video node. The output is always a video node. * * As every pipeline has an output video node, the ISS video objects at the * pipeline output stores the pipeline state. It tracks the streaming state of * both the input and output, as well as the availability of buffers. * * In sensor-to-memory mode, frames are always available at the pipeline input. * Starting the sensor usually requires I2C transfers and must be done in * interruptible context. The pipeline is started and stopped synchronously * to the stream on/off commands. All modules in the pipeline will get their * subdev set stream handler called. The module at the end of the pipeline must * delay starting the hardware until buffers are available at its output. * * In memory-to-memory mode, starting/stopping the stream requires * synchronization between the input and output. ISS modules can't be stopped * in the middle of a frame, and at least some of the modules seem to become * busy as soon as they're started, even if they don't receive a frame start * event. For that reason frames need to be processed in single-shot mode. The * driver needs to wait until a frame is completely processed and written to * memory before restarting the pipeline for the next frame. Pipelined * processing might be possible but requires more testing. * * Stream start must be delayed until buffers are available at both the input * and output. The pipeline must be started in the videobuf queue callback with * the buffers queue spinlock held. The modules subdev set stream operation must * not sleep. */ static int iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) { struct iss_video_fh *vfh = to_iss_video_fh(fh); struct iss_video *video = video_drvdata(file); struct media_entity_graph graph; struct media_entity *entity; enum iss_pipeline_state state; struct iss_pipeline *pipe; struct iss_video *far_end; unsigned long flags; int ret; if (type != video->type) return -EINVAL; mutex_lock(&video->stream_lock); /* Start streaming on the pipeline. No link touching an entity in the * pipeline can be activated or deactivated once streaming is started. */ pipe = video->video.entity.pipe ? to_iss_pipeline(&video->video.entity) : &video->pipe; pipe->external = NULL; pipe->external_rate = 0; pipe->external_bpp = 0; pipe->entities = 0; if (video->iss->pdata->set_constraints) video->iss->pdata->set_constraints(video->iss, true); ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe); if (ret < 0) goto err_media_entity_pipeline_start; entity = &video->video.entity; media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) pipe->entities |= 1 << entity->id; /* Verify that the currently configured format matches the output of * the connected subdev. */ ret = iss_video_check_format(video, vfh); if (ret < 0) goto err_iss_video_check_format; video->bpl_padding = ret; video->bpl_value = vfh->format.fmt.pix.bytesperline; /* Find the ISS video node connected at the far end of the pipeline and * update the pipeline. */ far_end = iss_video_far_end(video); if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT; pipe->input = far_end; pipe->output = video; } else { if (far_end == NULL) { ret = -EPIPE; goto err_iss_video_check_format; } state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT; pipe->input = video; pipe->output = far_end; } spin_lock_irqsave(&pipe->lock, flags); pipe->state &= ~ISS_PIPELINE_STREAM; pipe->state |= state; spin_unlock_irqrestore(&pipe->lock, flags); /* Set the maximum time per frame as the value requested by userspace. * This is a soft limit that can be overridden if the hardware doesn't * support the request limit. */ if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) pipe->max_timeperframe = vfh->timeperframe; video->queue = &vfh->queue; INIT_LIST_HEAD(&video->dmaqueue); spin_lock_init(&video->qlock); video->error = false; atomic_set(&pipe->frame_number, -1); ret = vb2_streamon(&vfh->queue, type); if (ret < 0) goto err_iss_video_check_format; /* In sensor-to-memory mode, the stream can be started synchronously * to the stream on command. In memory-to-memory mode, it will be * started when buffers are queued on both the input and output. */ if (pipe->input == NULL) { unsigned long flags; ret = omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_CONTINUOUS); if (ret < 0) goto err_omap4iss_set_stream; spin_lock_irqsave(&video->qlock, flags); if (list_empty(&video->dmaqueue)) video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN; spin_unlock_irqrestore(&video->qlock, flags); } mutex_unlock(&video->stream_lock); return 0; err_omap4iss_set_stream: vb2_streamoff(&vfh->queue, type); err_iss_video_check_format: media_entity_pipeline_stop(&video->video.entity); err_media_entity_pipeline_start: if (video->iss->pdata->set_constraints) video->iss->pdata->set_constraints(video->iss, false); video->queue = NULL; mutex_unlock(&video->stream_lock); return ret; }
/** * media_entity_pipeline_start - Mark a pipeline as streaming * @entity: Starting entity * @pipe: Media pipeline to be assigned to all entities in the pipeline. * * Mark all entities connected to a given entity through enabled links, either * directly or indirectly, as streaming. The given pipeline object is assigned to * every entity in the pipeline and stored in the media_entity pipe field. * * Calls to this function can be nested, in which case the same number of * media_entity_pipeline_stop() calls will be required to stop streaming. The * pipeline pointer must be identical for all nested calls to * media_entity_pipeline_start(). */ __must_check int media_entity_pipeline_start(struct media_entity *entity, struct media_pipeline *pipe) { struct media_device *mdev = entity->parent; struct media_entity_graph graph; struct media_entity *entity_err = entity; int ret; mutex_lock(&mdev->graph_mutex); media_entity_graph_walk_start(&graph, entity); while ((entity = media_entity_graph_walk_next(&graph))) { unsigned int i; entity->stream_count++; WARN_ON(entity->pipe && entity->pipe != pipe); entity->pipe = pipe; /* Already streaming --- no need to check. */ if (entity->stream_count > 1) continue; if (!entity->ops || !entity->ops->link_validate) continue; for (i = 0; i < entity->num_links; i++) { struct media_link *link = &entity->links[i]; /* Is this pad part of an enabled link? */ if (!(link->flags & MEDIA_LNK_FL_ENABLED)) continue; /* Are we the sink or not? */ if (link->sink->entity != entity) continue; ret = entity->ops->link_validate(link); if (ret < 0 && ret != -ENOIOCTLCMD) goto error; } } mutex_unlock(&mdev->graph_mutex); return 0; error: /* * Link validation on graph failed. We revert what we did and * return the error. */ media_entity_graph_walk_start(&graph, entity_err); while ((entity_err = media_entity_graph_walk_next(&graph))) { entity_err->stream_count--; if (entity_err->stream_count == 0) entity_err->pipe = NULL; /* * We haven't increased stream_count further than this * so we quit here. */ if (entity_err == entity) break; } mutex_unlock(&mdev->graph_mutex); return ret; }