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 v4l2_info(&mdev->v4l2_dev, "comp_vdev_open()\n");
78 switch (vdev->vfl_type) {
79 case VFL_TYPE_GRABBER:
85 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
89 if (!atomic_inc_and_test(&mdev->access_ref)) {
90 v4l2_err(&mdev->v4l2_dev, "too many clients\n");
96 v4l2_fh_init(&fh->fh, vdev);
97 filp->private_data = fh;
101 ret = most_start_channel(mdev->iface, mdev->ch_idx, &comp);
103 v4l2_err(&mdev->v4l2_dev, "most_start_channel() failed\n");
110 v4l2_fh_del(&fh->fh);
111 v4l2_fh_exit(&fh->fh);
114 atomic_dec(&mdev->access_ref);
119 static int comp_vdev_close(struct file *filp)
121 struct comp_fh *fh = filp->private_data;
122 struct most_video_dev *mdev = fh->mdev;
123 struct mbo *mbo, *tmp;
125 v4l2_info(&mdev->v4l2_dev, "comp_vdev_close()\n");
128 * We need to put MBOs back before we call most_stop_channel()
129 * to deallocate MBOs.
130 * From the other hand mostcore still calling rx_completion()
131 * to deliver MBOs until most_stop_channel() is called.
132 * Use mute to work around this issue.
133 * This must be implemented in core.
136 spin_lock_irq(&mdev->list_lock);
138 list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
139 list_del(&mbo->list);
140 spin_unlock_irq(&mdev->list_lock);
142 spin_lock_irq(&mdev->list_lock);
144 spin_unlock_irq(&mdev->list_lock);
145 most_stop_channel(mdev->iface, mdev->ch_idx, &comp);
148 v4l2_fh_del(&fh->fh);
149 v4l2_fh_exit(&fh->fh);
151 atomic_dec(&mdev->access_ref);
156 static ssize_t comp_vdev_read(struct file *filp, char __user *buf,
157 size_t count, loff_t *pos)
159 struct comp_fh *fh = filp->private_data;
160 struct most_video_dev *mdev = fh->mdev;
169 /* wait for the first buffer */
170 if (!(filp->f_flags & O_NONBLOCK)) {
171 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
175 if (!data_ready(mdev))
178 while (count > 0 && data_ready(mdev)) {
179 struct mbo *const mbo = get_top_mbo(mdev);
180 int const rem = mbo->processed_length - fh->offs;
181 int const cnt = rem < count ? rem : count;
183 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
184 v4l2_err(&mdev->v4l2_dev, "read: copy_to_user failed\n");
197 spin_lock_irq(&mdev->list_lock);
198 list_del(&mbo->list);
199 spin_unlock_irq(&mdev->list_lock);
206 static __poll_t comp_vdev_poll(struct file *filp, poll_table *wait)
208 struct comp_fh *fh = filp->private_data;
209 struct most_video_dev *mdev = fh->mdev;
212 /* only wait if no data is available */
213 if (!data_ready(mdev))
214 poll_wait(filp, &mdev->wait_data, wait);
215 if (data_ready(mdev))
216 mask |= EPOLLIN | EPOLLRDNORM;
221 static void comp_set_format_struct(struct v4l2_format *f)
223 f->fmt.pix.width = 8;
224 f->fmt.pix.height = 8;
225 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
226 f->fmt.pix.bytesperline = 0;
227 f->fmt.pix.sizeimage = 188 * 2;
228 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
229 f->fmt.pix.field = V4L2_FIELD_NONE;
233 static int comp_set_format(struct most_video_dev *mdev, unsigned int cmd,
234 struct v4l2_format *format)
236 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
239 if (cmd == VIDIOC_TRY_FMT)
242 comp_set_format_struct(format);
247 static int vidioc_querycap(struct file *file, void *priv,
248 struct v4l2_capability *cap)
250 struct comp_fh *fh = priv;
251 struct most_video_dev *mdev = fh->mdev;
253 v4l2_info(&mdev->v4l2_dev, "vidioc_querycap()\n");
255 strlcpy(cap->driver, "v4l2_component", sizeof(cap->driver));
256 strlcpy(cap->card, "MOST", sizeof(cap->card));
257 snprintf(cap->bus_info, sizeof(cap->bus_info),
258 "%s", mdev->iface->description);
263 V4L2_CAP_VIDEO_CAPTURE;
267 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
268 struct v4l2_fmtdesc *f)
270 struct comp_fh *fh = priv;
271 struct most_video_dev *mdev = fh->mdev;
273 v4l2_info(&mdev->v4l2_dev, "vidioc_enum_fmt_vid_cap() %d\n", f->index);
278 strcpy(f->description, "MPEG");
279 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
280 f->flags = V4L2_FMT_FLAG_COMPRESSED;
281 f->pixelformat = V4L2_PIX_FMT_MPEG;
286 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
287 struct v4l2_format *f)
289 struct comp_fh *fh = priv;
290 struct most_video_dev *mdev = fh->mdev;
292 v4l2_info(&mdev->v4l2_dev, "vidioc_g_fmt_vid_cap()\n");
294 comp_set_format_struct(f);
298 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
299 struct v4l2_format *f)
301 struct comp_fh *fh = priv;
302 struct most_video_dev *mdev = fh->mdev;
304 return comp_set_format(mdev, VIDIOC_TRY_FMT, f);
307 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
308 struct v4l2_format *f)
310 struct comp_fh *fh = priv;
311 struct most_video_dev *mdev = fh->mdev;
313 return comp_set_format(mdev, VIDIOC_S_FMT, f);
316 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
318 struct comp_fh *fh = priv;
319 struct most_video_dev *mdev = fh->mdev;
321 v4l2_info(&mdev->v4l2_dev, "vidioc_g_std()\n");
323 *norm = V4L2_STD_UNKNOWN;
327 static int vidioc_enum_input(struct file *file, void *priv,
328 struct v4l2_input *input)
330 struct comp_fh *fh = priv;
331 struct most_video_dev *mdev = fh->mdev;
333 if (input->index >= V4L2_CMP_MAX_INPUT)
336 strcpy(input->name, "MOST Video");
337 input->type |= V4L2_INPUT_TYPE_CAMERA;
340 input->std = mdev->vdev->tvnorms;
345 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
347 struct comp_fh *fh = priv;
348 struct most_video_dev *mdev = fh->mdev;
349 *i = mdev->ctrl_input;
353 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
355 struct comp_fh *fh = priv;
356 struct most_video_dev *mdev = fh->mdev;
358 v4l2_info(&mdev->v4l2_dev, "vidioc_s_input(%d)\n", index);
360 if (index >= V4L2_CMP_MAX_INPUT)
362 mdev->ctrl_input = index;
366 static const struct v4l2_file_operations comp_fops = {
367 .owner = THIS_MODULE,
368 .open = comp_vdev_open,
369 .release = comp_vdev_close,
370 .read = comp_vdev_read,
371 .poll = comp_vdev_poll,
372 .unlocked_ioctl = video_ioctl2,
375 static const struct v4l2_ioctl_ops video_ioctl_ops = {
376 .vidioc_querycap = vidioc_querycap,
377 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
378 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
379 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
380 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
381 .vidioc_g_std = vidioc_g_std,
382 .vidioc_enum_input = vidioc_enum_input,
383 .vidioc_g_input = vidioc_g_input,
384 .vidioc_s_input = vidioc_s_input,
387 static const struct video_device comp_videodev_template = {
389 .release = video_device_release,
390 .ioctl_ops = &video_ioctl_ops,
391 .tvnorms = V4L2_STD_UNKNOWN,
394 /**************************************************************************/
396 static struct most_video_dev *get_comp_dev(
397 struct most_interface *iface, int channel_idx)
399 struct most_video_dev *mdev;
402 spin_lock_irqsave(&list_lock, flags);
403 list_for_each_entry(mdev, &video_devices, list) {
404 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
405 spin_unlock_irqrestore(&list_lock, flags);
409 spin_unlock_irqrestore(&list_lock, flags);
413 static int comp_rx_data(struct mbo *mbo)
416 struct most_video_dev *mdev =
417 get_comp_dev(mbo->ifp, mbo->hdm_channel_id);
422 spin_lock_irqsave(&mdev->list_lock, flags);
423 if (unlikely(mdev->mute)) {
424 spin_unlock_irqrestore(&mdev->list_lock, flags);
428 list_add_tail(&mbo->list, &mdev->pending_mbos);
429 spin_unlock_irqrestore(&mdev->list_lock, flags);
430 wake_up_interruptible(&mdev->wait_data);
434 static int comp_register_videodev(struct most_video_dev *mdev)
438 v4l2_info(&mdev->v4l2_dev, "comp_register_videodev()\n");
440 init_waitqueue_head(&mdev->wait_data);
442 /* allocate and fill v4l2 video struct */
443 mdev->vdev = video_device_alloc();
447 /* Fill the video capture device struct */
448 *mdev->vdev = comp_videodev_template;
449 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
450 mdev->vdev->lock = &mdev->lock;
451 snprintf(mdev->vdev->name, sizeof(mdev->vdev->name), "MOST: %s",
452 mdev->v4l2_dev.name);
454 /* Register the v4l2 device */
455 video_set_drvdata(mdev->vdev, mdev);
456 ret = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
458 v4l2_err(&mdev->v4l2_dev, "video_register_device failed (%d)\n",
460 video_device_release(mdev->vdev);
466 static void comp_unregister_videodev(struct most_video_dev *mdev)
468 v4l2_info(&mdev->v4l2_dev, "comp_unregister_videodev()\n");
470 video_unregister_device(mdev->vdev);
473 static void comp_v4l2_dev_release(struct v4l2_device *v4l2_dev)
475 struct most_video_dev *mdev =
476 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
478 v4l2_device_unregister(v4l2_dev);
482 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
483 struct most_channel_config *ccfg, char *name)
486 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
488 pr_info("comp_probe_channel(%s)\n", name);
491 pr_err("channel already linked\n");
495 if (ccfg->direction != MOST_CH_RX) {
496 pr_err("wrong direction, expect rx\n");
500 if (ccfg->data_type != MOST_CH_SYNC &&
501 ccfg->data_type != MOST_CH_ISOC) {
502 pr_err("wrong channel type, expect sync or isoc\n");
506 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
510 mutex_init(&mdev->lock);
511 atomic_set(&mdev->access_ref, -1);
512 spin_lock_init(&mdev->list_lock);
513 INIT_LIST_HEAD(&mdev->pending_mbos);
515 mdev->ch_idx = channel_idx;
516 mdev->v4l2_dev.release = comp_v4l2_dev_release;
518 /* Create the v4l2_device */
519 strlcpy(mdev->v4l2_dev.name, name, sizeof(mdev->v4l2_dev.name));
520 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
522 pr_err("v4l2_device_register() failed\n");
527 ret = comp_register_videodev(mdev);
531 spin_lock_irq(&list_lock);
532 list_add(&mdev->list, &video_devices);
533 spin_unlock_irq(&list_lock);
534 v4l2_info(&mdev->v4l2_dev, "comp_probe_channel() done\n");
538 v4l2_device_disconnect(&mdev->v4l2_dev);
539 v4l2_device_put(&mdev->v4l2_dev);
543 static int comp_disconnect_channel(struct most_interface *iface,
546 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
549 pr_err("no such channel is linked\n");
553 v4l2_info(&mdev->v4l2_dev, "comp_disconnect_channel()\n");
555 spin_lock_irq(&list_lock);
556 list_del(&mdev->list);
557 spin_unlock_irq(&list_lock);
559 comp_unregister_videodev(mdev);
560 v4l2_device_disconnect(&mdev->v4l2_dev);
561 v4l2_device_put(&mdev->v4l2_dev);
565 static struct core_component comp_info = {
567 .probe_channel = comp_probe_channel,
568 .disconnect_channel = comp_disconnect_channel,
569 .rx_completion = comp_rx_data,
572 static int __init comp_init(void)
574 spin_lock_init(&list_lock);
575 return most_register_component(&comp);
578 static void __exit comp_exit(void)
580 struct most_video_dev *mdev, *tmp;
583 * As the mostcore currently doesn't call disconnect_channel()
584 * for linked channels while we call most_deregister_component()
585 * we simulate this call here.
586 * This must be fixed in core.
588 spin_lock_irq(&list_lock);
589 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
590 list_del(&mdev->list);
591 spin_unlock_irq(&list_lock);
593 comp_unregister_videodev(mdev);
594 v4l2_device_disconnect(&mdev->v4l2_dev);
595 v4l2_device_put(&mdev->v4l2_dev);
596 spin_lock_irq(&list_lock);
598 spin_unlock_irq(&list_lock);
600 most_deregister_component(&comp_info);
601 BUG_ON(!list_empty(&video_devices));
604 module_init(comp_init);
605 module_exit(comp_exit);
607 MODULE_DESCRIPTION("V4L2 Component Module for Mostcore");
608 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
609 MODULE_LICENSE("GPL");