1 // SPDX-License-Identifier: GPL-2.0
3 * video.c - V4L2 component for Mostcore
5 * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/suspend.h>
15 #include <linux/videodev2.h>
16 #include <linux/mutex.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
24 #include "most/core.h"
26 #define V4L2_CMP_MAX_INPUT 1
28 static struct core_component comp;
30 struct most_video_dev {
31 struct most_interface *iface;
33 struct list_head list;
36 struct list_head pending_mbos;
39 struct v4l2_device v4l2_dev;
41 struct video_device *vdev;
42 unsigned int ctrl_input;
46 wait_queue_head_t wait_data;
50 /* must be the first field of this struct! */
52 struct most_video_dev *mdev;
56 static struct list_head video_devices = LIST_HEAD_INIT(video_devices);
57 static struct spinlock list_lock;
59 static inline bool data_ready(struct most_video_dev *mdev)
61 return !list_empty(&mdev->pending_mbos);
64 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
66 return list_first_entry(&mdev->pending_mbos, struct mbo, list);
69 static int comp_vdev_open(struct file *filp)
72 struct video_device *vdev = video_devdata(filp);
73 struct most_video_dev *mdev = video_drvdata(filp);
76 switch (vdev->vfl_type) {
77 case VFL_TYPE_GRABBER:
83 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
87 if (!atomic_inc_and_test(&mdev->access_ref)) {
88 v4l2_err(&mdev->v4l2_dev, "too many clients\n");
94 v4l2_fh_init(&fh->fh, vdev);
95 filp->private_data = fh;
99 ret = most_start_channel(mdev->iface, mdev->ch_idx, &comp);
101 v4l2_err(&mdev->v4l2_dev, "most_start_channel() failed\n");
108 v4l2_fh_del(&fh->fh);
109 v4l2_fh_exit(&fh->fh);
112 atomic_dec(&mdev->access_ref);
117 static int comp_vdev_close(struct file *filp)
119 struct comp_fh *fh = filp->private_data;
120 struct most_video_dev *mdev = fh->mdev;
121 struct mbo *mbo, *tmp;
124 * We need to put MBOs back before we call most_stop_channel()
125 * to deallocate MBOs.
126 * From the other hand mostcore still calling rx_completion()
127 * to deliver MBOs until most_stop_channel() is called.
128 * Use mute to work around this issue.
129 * This must be implemented in core.
132 spin_lock_irq(&mdev->list_lock);
134 list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
135 list_del(&mbo->list);
136 spin_unlock_irq(&mdev->list_lock);
138 spin_lock_irq(&mdev->list_lock);
140 spin_unlock_irq(&mdev->list_lock);
141 most_stop_channel(mdev->iface, mdev->ch_idx, &comp);
144 v4l2_fh_del(&fh->fh);
145 v4l2_fh_exit(&fh->fh);
147 atomic_dec(&mdev->access_ref);
152 static ssize_t comp_vdev_read(struct file *filp, char __user *buf,
153 size_t count, loff_t *pos)
155 struct comp_fh *fh = filp->private_data;
156 struct most_video_dev *mdev = fh->mdev;
165 /* wait for the first buffer */
166 if (!(filp->f_flags & O_NONBLOCK)) {
167 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
171 if (!data_ready(mdev))
174 while (count > 0 && data_ready(mdev)) {
175 struct mbo *const mbo = get_top_mbo(mdev);
176 int const rem = mbo->processed_length - fh->offs;
177 int const cnt = rem < count ? rem : count;
179 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
180 v4l2_err(&mdev->v4l2_dev, "read: copy_to_user failed\n");
193 spin_lock_irq(&mdev->list_lock);
194 list_del(&mbo->list);
195 spin_unlock_irq(&mdev->list_lock);
202 static __poll_t comp_vdev_poll(struct file *filp, poll_table *wait)
204 struct comp_fh *fh = filp->private_data;
205 struct most_video_dev *mdev = fh->mdev;
208 /* only wait if no data is available */
209 if (!data_ready(mdev))
210 poll_wait(filp, &mdev->wait_data, wait);
211 if (data_ready(mdev))
212 mask |= EPOLLIN | EPOLLRDNORM;
217 static void comp_set_format_struct(struct v4l2_format *f)
219 f->fmt.pix.width = 8;
220 f->fmt.pix.height = 8;
221 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
222 f->fmt.pix.bytesperline = 0;
223 f->fmt.pix.sizeimage = 188 * 2;
224 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
225 f->fmt.pix.field = V4L2_FIELD_NONE;
229 static int comp_set_format(struct most_video_dev *mdev, unsigned int cmd,
230 struct v4l2_format *format)
232 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
235 if (cmd == VIDIOC_TRY_FMT)
238 comp_set_format_struct(format);
243 static int vidioc_querycap(struct file *file, void *priv,
244 struct v4l2_capability *cap)
246 struct comp_fh *fh = priv;
247 struct most_video_dev *mdev = fh->mdev;
249 strlcpy(cap->driver, "v4l2_component", sizeof(cap->driver));
250 strlcpy(cap->card, "MOST", sizeof(cap->card));
251 snprintf(cap->bus_info, sizeof(cap->bus_info),
252 "%s", mdev->iface->description);
257 V4L2_CAP_VIDEO_CAPTURE;
261 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
262 struct v4l2_fmtdesc *f)
267 strcpy(f->description, "MPEG");
268 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
269 f->flags = V4L2_FMT_FLAG_COMPRESSED;
270 f->pixelformat = V4L2_PIX_FMT_MPEG;
275 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
276 struct v4l2_format *f)
278 comp_set_format_struct(f);
282 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
283 struct v4l2_format *f)
285 struct comp_fh *fh = priv;
286 struct most_video_dev *mdev = fh->mdev;
288 return comp_set_format(mdev, VIDIOC_TRY_FMT, f);
291 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
292 struct v4l2_format *f)
294 struct comp_fh *fh = priv;
295 struct most_video_dev *mdev = fh->mdev;
297 return comp_set_format(mdev, VIDIOC_S_FMT, f);
300 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
302 *norm = V4L2_STD_UNKNOWN;
306 static int vidioc_enum_input(struct file *file, void *priv,
307 struct v4l2_input *input)
309 struct comp_fh *fh = priv;
310 struct most_video_dev *mdev = fh->mdev;
312 if (input->index >= V4L2_CMP_MAX_INPUT)
315 strcpy(input->name, "MOST Video");
316 input->type |= V4L2_INPUT_TYPE_CAMERA;
319 input->std = mdev->vdev->tvnorms;
324 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
326 struct comp_fh *fh = priv;
327 struct most_video_dev *mdev = fh->mdev;
328 *i = mdev->ctrl_input;
332 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
334 struct comp_fh *fh = priv;
335 struct most_video_dev *mdev = fh->mdev;
337 if (index >= V4L2_CMP_MAX_INPUT)
339 mdev->ctrl_input = index;
343 static const struct v4l2_file_operations comp_fops = {
344 .owner = THIS_MODULE,
345 .open = comp_vdev_open,
346 .release = comp_vdev_close,
347 .read = comp_vdev_read,
348 .poll = comp_vdev_poll,
349 .unlocked_ioctl = video_ioctl2,
352 static const struct v4l2_ioctl_ops video_ioctl_ops = {
353 .vidioc_querycap = vidioc_querycap,
354 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
355 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
356 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
357 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
358 .vidioc_g_std = vidioc_g_std,
359 .vidioc_enum_input = vidioc_enum_input,
360 .vidioc_g_input = vidioc_g_input,
361 .vidioc_s_input = vidioc_s_input,
364 static const struct video_device comp_videodev_template = {
366 .release = video_device_release,
367 .ioctl_ops = &video_ioctl_ops,
368 .tvnorms = V4L2_STD_UNKNOWN,
371 /**************************************************************************/
373 static struct most_video_dev *get_comp_dev(
374 struct most_interface *iface, int channel_idx)
376 struct most_video_dev *mdev;
379 spin_lock_irqsave(&list_lock, flags);
380 list_for_each_entry(mdev, &video_devices, list) {
381 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
382 spin_unlock_irqrestore(&list_lock, flags);
386 spin_unlock_irqrestore(&list_lock, flags);
390 static int comp_rx_data(struct mbo *mbo)
393 struct most_video_dev *mdev =
394 get_comp_dev(mbo->ifp, mbo->hdm_channel_id);
399 spin_lock_irqsave(&mdev->list_lock, flags);
400 if (unlikely(mdev->mute)) {
401 spin_unlock_irqrestore(&mdev->list_lock, flags);
405 list_add_tail(&mbo->list, &mdev->pending_mbos);
406 spin_unlock_irqrestore(&mdev->list_lock, flags);
407 wake_up_interruptible(&mdev->wait_data);
411 static int comp_register_videodev(struct most_video_dev *mdev)
415 init_waitqueue_head(&mdev->wait_data);
417 /* allocate and fill v4l2 video struct */
418 mdev->vdev = video_device_alloc();
422 /* Fill the video capture device struct */
423 *mdev->vdev = comp_videodev_template;
424 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
425 mdev->vdev->lock = &mdev->lock;
426 snprintf(mdev->vdev->name, sizeof(mdev->vdev->name), "MOST: %s",
427 mdev->v4l2_dev.name);
429 /* Register the v4l2 device */
430 video_set_drvdata(mdev->vdev, mdev);
431 ret = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
433 v4l2_err(&mdev->v4l2_dev, "video_register_device failed (%d)\n",
435 video_device_release(mdev->vdev);
441 static void comp_unregister_videodev(struct most_video_dev *mdev)
443 video_unregister_device(mdev->vdev);
446 static void comp_v4l2_dev_release(struct v4l2_device *v4l2_dev)
448 struct most_video_dev *mdev =
449 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
451 v4l2_device_unregister(v4l2_dev);
455 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
456 struct most_channel_config *ccfg, char *name)
459 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
462 pr_err("channel already linked\n");
466 if (ccfg->direction != MOST_CH_RX) {
467 pr_err("wrong direction, expect rx\n");
471 if (ccfg->data_type != MOST_CH_SYNC &&
472 ccfg->data_type != MOST_CH_ISOC) {
473 pr_err("wrong channel type, expect sync or isoc\n");
477 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
481 mutex_init(&mdev->lock);
482 atomic_set(&mdev->access_ref, -1);
483 spin_lock_init(&mdev->list_lock);
484 INIT_LIST_HEAD(&mdev->pending_mbos);
486 mdev->ch_idx = channel_idx;
487 mdev->v4l2_dev.release = comp_v4l2_dev_release;
489 /* Create the v4l2_device */
490 strlcpy(mdev->v4l2_dev.name, name, sizeof(mdev->v4l2_dev.name));
491 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
493 pr_err("v4l2_device_register() failed\n");
498 ret = comp_register_videodev(mdev);
502 spin_lock_irq(&list_lock);
503 list_add(&mdev->list, &video_devices);
504 spin_unlock_irq(&list_lock);
508 v4l2_device_disconnect(&mdev->v4l2_dev);
509 v4l2_device_put(&mdev->v4l2_dev);
513 static int comp_disconnect_channel(struct most_interface *iface,
516 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
519 pr_err("no such channel is linked\n");
523 spin_lock_irq(&list_lock);
524 list_del(&mdev->list);
525 spin_unlock_irq(&list_lock);
527 comp_unregister_videodev(mdev);
528 v4l2_device_disconnect(&mdev->v4l2_dev);
529 v4l2_device_put(&mdev->v4l2_dev);
533 static struct core_component comp = {
535 .probe_channel = comp_probe_channel,
536 .disconnect_channel = comp_disconnect_channel,
537 .rx_completion = comp_rx_data,
540 static int __init comp_init(void)
542 spin_lock_init(&list_lock);
543 return most_register_component(&comp);
546 static void __exit comp_exit(void)
548 struct most_video_dev *mdev, *tmp;
551 * As the mostcore currently doesn't call disconnect_channel()
552 * for linked channels while we call most_deregister_component()
553 * we simulate this call here.
554 * This must be fixed in core.
556 spin_lock_irq(&list_lock);
557 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
558 list_del(&mdev->list);
559 spin_unlock_irq(&list_lock);
561 comp_unregister_videodev(mdev);
562 v4l2_device_disconnect(&mdev->v4l2_dev);
563 v4l2_device_put(&mdev->v4l2_dev);
564 spin_lock_irq(&list_lock);
566 spin_unlock_irq(&list_lock);
568 most_deregister_component(&comp);
569 BUG_ON(!list_empty(&video_devices));
572 module_init(comp_init);
573 module_exit(comp_exit);
575 MODULE_DESCRIPTION("V4L2 Component Module for Mostcore");
576 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
577 MODULE_LICENSE("GPL");