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>
23 #include <linux/most.h>
25 #define V4L2_CMP_MAX_INPUT 1
27 static struct most_component comp;
29 struct most_video_dev {
30 struct most_interface *iface;
32 struct list_head list;
35 struct list_head pending_mbos;
38 struct v4l2_device v4l2_dev;
40 struct video_device *vdev;
41 unsigned int ctrl_input;
45 wait_queue_head_t wait_data;
49 /* must be the first field of this struct! */
51 struct most_video_dev *mdev;
55 static LIST_HEAD(video_devices);
56 static DEFINE_SPINLOCK(list_lock);
58 static inline bool data_ready(struct most_video_dev *mdev)
60 return !list_empty(&mdev->pending_mbos);
63 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
65 return list_first_entry(&mdev->pending_mbos, struct mbo, list);
68 static int comp_vdev_open(struct file *filp)
71 struct video_device *vdev = video_devdata(filp);
72 struct most_video_dev *mdev = video_drvdata(filp);
75 switch (vdev->vfl_type) {
82 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
86 if (!atomic_inc_and_test(&mdev->access_ref)) {
87 v4l2_err(&mdev->v4l2_dev, "too many clients\n");
93 v4l2_fh_init(&fh->fh, vdev);
94 filp->private_data = fh;
98 ret = most_start_channel(mdev->iface, mdev->ch_idx, &comp);
100 v4l2_err(&mdev->v4l2_dev, "most_start_channel() failed\n");
107 v4l2_fh_del(&fh->fh);
108 v4l2_fh_exit(&fh->fh);
111 atomic_dec(&mdev->access_ref);
116 static int comp_vdev_close(struct file *filp)
118 struct comp_fh *fh = filp->private_data;
119 struct most_video_dev *mdev = fh->mdev;
120 struct mbo *mbo, *tmp;
123 * We need to put MBOs back before we call most_stop_channel()
124 * to deallocate MBOs.
125 * From the other hand mostcore still calling rx_completion()
126 * to deliver MBOs until most_stop_channel() is called.
127 * Use mute to work around this issue.
128 * This must be implemented in core.
131 spin_lock_irq(&mdev->list_lock);
133 list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
134 list_del(&mbo->list);
135 spin_unlock_irq(&mdev->list_lock);
137 spin_lock_irq(&mdev->list_lock);
139 spin_unlock_irq(&mdev->list_lock);
140 most_stop_channel(mdev->iface, mdev->ch_idx, &comp);
143 v4l2_fh_del(&fh->fh);
144 v4l2_fh_exit(&fh->fh);
146 atomic_dec(&mdev->access_ref);
151 static ssize_t comp_vdev_read(struct file *filp, char __user *buf,
152 size_t count, loff_t *pos)
154 struct comp_fh *fh = filp->private_data;
155 struct most_video_dev *mdev = fh->mdev;
164 /* wait for the first buffer */
165 if (!(filp->f_flags & O_NONBLOCK)) {
166 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
170 if (!data_ready(mdev))
173 while (count > 0 && data_ready(mdev)) {
174 struct mbo *const mbo = get_top_mbo(mdev);
175 int const rem = mbo->processed_length - fh->offs;
176 int const cnt = rem < count ? rem : count;
178 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
179 v4l2_err(&mdev->v4l2_dev, "read: copy_to_user failed\n");
192 spin_lock_irq(&mdev->list_lock);
193 list_del(&mbo->list);
194 spin_unlock_irq(&mdev->list_lock);
201 static __poll_t comp_vdev_poll(struct file *filp, poll_table *wait)
203 struct comp_fh *fh = filp->private_data;
204 struct most_video_dev *mdev = fh->mdev;
207 /* only wait if no data is available */
208 if (!data_ready(mdev))
209 poll_wait(filp, &mdev->wait_data, wait);
210 if (data_ready(mdev))
211 mask |= EPOLLIN | EPOLLRDNORM;
216 static void comp_set_format_struct(struct v4l2_format *f)
218 f->fmt.pix.width = 8;
219 f->fmt.pix.height = 8;
220 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
221 f->fmt.pix.bytesperline = 0;
222 f->fmt.pix.sizeimage = 188 * 2;
223 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
224 f->fmt.pix.field = V4L2_FIELD_NONE;
228 static int comp_set_format(struct most_video_dev *mdev, unsigned int cmd,
229 struct v4l2_format *format)
231 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
234 if (cmd == VIDIOC_TRY_FMT)
237 comp_set_format_struct(format);
242 static int vidioc_querycap(struct file *file, void *priv,
243 struct v4l2_capability *cap)
245 struct comp_fh *fh = priv;
246 struct most_video_dev *mdev = fh->mdev;
248 strscpy(cap->driver, "v4l2_component", sizeof(cap->driver));
249 strscpy(cap->card, "MOST", sizeof(cap->card));
250 snprintf(cap->bus_info, sizeof(cap->bus_info),
251 "%s", mdev->iface->description);
255 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
256 struct v4l2_fmtdesc *f)
261 strscpy(f->description, "MPEG", sizeof(f->description));
262 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
263 f->flags = V4L2_FMT_FLAG_COMPRESSED;
264 f->pixelformat = V4L2_PIX_FMT_MPEG;
269 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
270 struct v4l2_format *f)
272 comp_set_format_struct(f);
276 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
277 struct v4l2_format *f)
279 struct comp_fh *fh = priv;
280 struct most_video_dev *mdev = fh->mdev;
282 return comp_set_format(mdev, VIDIOC_TRY_FMT, f);
285 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
286 struct v4l2_format *f)
288 struct comp_fh *fh = priv;
289 struct most_video_dev *mdev = fh->mdev;
291 return comp_set_format(mdev, VIDIOC_S_FMT, f);
294 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
296 *norm = V4L2_STD_UNKNOWN;
300 static int vidioc_enum_input(struct file *file, void *priv,
301 struct v4l2_input *input)
303 struct comp_fh *fh = priv;
304 struct most_video_dev *mdev = fh->mdev;
306 if (input->index >= V4L2_CMP_MAX_INPUT)
309 strscpy(input->name, "MOST Video", sizeof(input->name));
310 input->type |= V4L2_INPUT_TYPE_CAMERA;
313 input->std = mdev->vdev->tvnorms;
318 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
320 struct comp_fh *fh = priv;
321 struct most_video_dev *mdev = fh->mdev;
322 *i = mdev->ctrl_input;
326 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
328 struct comp_fh *fh = priv;
329 struct most_video_dev *mdev = fh->mdev;
331 if (index >= V4L2_CMP_MAX_INPUT)
333 mdev->ctrl_input = index;
337 static const struct v4l2_file_operations comp_fops = {
338 .owner = THIS_MODULE,
339 .open = comp_vdev_open,
340 .release = comp_vdev_close,
341 .read = comp_vdev_read,
342 .poll = comp_vdev_poll,
343 .unlocked_ioctl = video_ioctl2,
346 static const struct v4l2_ioctl_ops video_ioctl_ops = {
347 .vidioc_querycap = vidioc_querycap,
348 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
349 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
350 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
351 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
352 .vidioc_g_std = vidioc_g_std,
353 .vidioc_enum_input = vidioc_enum_input,
354 .vidioc_g_input = vidioc_g_input,
355 .vidioc_s_input = vidioc_s_input,
358 static const struct video_device comp_videodev_template = {
360 .release = video_device_release,
361 .ioctl_ops = &video_ioctl_ops,
362 .tvnorms = V4L2_STD_UNKNOWN,
363 .device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE,
366 /**************************************************************************/
368 static struct most_video_dev *get_comp_dev(struct most_interface *iface, int channel_idx)
370 struct most_video_dev *mdev;
373 spin_lock_irqsave(&list_lock, flags);
374 list_for_each_entry(mdev, &video_devices, list) {
375 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
376 spin_unlock_irqrestore(&list_lock, flags);
380 spin_unlock_irqrestore(&list_lock, flags);
384 static int comp_rx_data(struct mbo *mbo)
387 struct most_video_dev *mdev =
388 get_comp_dev(mbo->ifp, mbo->hdm_channel_id);
393 spin_lock_irqsave(&mdev->list_lock, flags);
394 if (unlikely(mdev->mute)) {
395 spin_unlock_irqrestore(&mdev->list_lock, flags);
399 list_add_tail(&mbo->list, &mdev->pending_mbos);
400 spin_unlock_irqrestore(&mdev->list_lock, flags);
401 wake_up_interruptible(&mdev->wait_data);
405 static int comp_register_videodev(struct most_video_dev *mdev)
409 init_waitqueue_head(&mdev->wait_data);
411 /* allocate and fill v4l2 video struct */
412 mdev->vdev = video_device_alloc();
416 /* Fill the video capture device struct */
417 *mdev->vdev = comp_videodev_template;
418 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
419 mdev->vdev->lock = &mdev->lock;
420 snprintf(mdev->vdev->name, sizeof(mdev->vdev->name), "MOST: %s",
421 mdev->v4l2_dev.name);
423 /* Register the v4l2 device */
424 video_set_drvdata(mdev->vdev, mdev);
425 ret = video_register_device(mdev->vdev, VFL_TYPE_VIDEO, -1);
427 v4l2_err(&mdev->v4l2_dev, "video_register_device failed (%d)\n",
429 video_device_release(mdev->vdev);
435 static void comp_unregister_videodev(struct most_video_dev *mdev)
437 video_unregister_device(mdev->vdev);
440 static void comp_v4l2_dev_release(struct v4l2_device *v4l2_dev)
442 struct most_video_dev *mdev =
443 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
445 v4l2_device_unregister(v4l2_dev);
449 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
450 struct most_channel_config *ccfg, char *name,
454 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
457 pr_err("channel already linked\n");
461 if (ccfg->direction != MOST_CH_RX) {
462 pr_err("wrong direction, expect rx\n");
466 if (ccfg->data_type != MOST_CH_SYNC &&
467 ccfg->data_type != MOST_CH_ISOC) {
468 pr_err("wrong channel type, expect sync or isoc\n");
472 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
476 mutex_init(&mdev->lock);
477 atomic_set(&mdev->access_ref, -1);
478 spin_lock_init(&mdev->list_lock);
479 INIT_LIST_HEAD(&mdev->pending_mbos);
481 mdev->ch_idx = channel_idx;
482 mdev->v4l2_dev.release = comp_v4l2_dev_release;
484 /* Create the v4l2_device */
485 strscpy(mdev->v4l2_dev.name, name, sizeof(mdev->v4l2_dev.name));
486 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
488 pr_err("v4l2_device_register() failed\n");
493 ret = comp_register_videodev(mdev);
497 spin_lock_irq(&list_lock);
498 list_add(&mdev->list, &video_devices);
499 spin_unlock_irq(&list_lock);
503 v4l2_device_disconnect(&mdev->v4l2_dev);
504 v4l2_device_put(&mdev->v4l2_dev);
508 static int comp_disconnect_channel(struct most_interface *iface,
511 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
514 pr_err("no such channel is linked\n");
518 spin_lock_irq(&list_lock);
519 list_del(&mdev->list);
520 spin_unlock_irq(&list_lock);
522 comp_unregister_videodev(mdev);
523 v4l2_device_disconnect(&mdev->v4l2_dev);
524 v4l2_device_put(&mdev->v4l2_dev);
528 static struct most_component comp = {
531 .probe_channel = comp_probe_channel,
532 .disconnect_channel = comp_disconnect_channel,
533 .rx_completion = comp_rx_data,
536 static int __init comp_init(void)
540 err = most_register_component(&comp);
543 err = most_register_configfs_subsys(&comp);
545 most_deregister_component(&comp);
551 static void __exit comp_exit(void)
553 struct most_video_dev *mdev, *tmp;
556 * As the mostcore currently doesn't call disconnect_channel()
557 * for linked channels while we call most_deregister_component()
558 * we simulate this call here.
559 * This must be fixed in core.
561 spin_lock_irq(&list_lock);
562 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
563 list_del(&mdev->list);
564 spin_unlock_irq(&list_lock);
566 comp_unregister_videodev(mdev);
567 v4l2_device_disconnect(&mdev->v4l2_dev);
568 v4l2_device_put(&mdev->v4l2_dev);
569 spin_lock_irq(&list_lock);
571 spin_unlock_irq(&list_lock);
573 most_deregister_configfs_subsys(&comp);
574 most_deregister_component(&comp);
575 BUG_ON(!list_empty(&video_devices));
578 module_init(comp_init);
579 module_exit(comp_exit);
581 MODULE_DESCRIPTION("V4L2 Component Module for Mostcore");
582 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
583 MODULE_LICENSE("GPL");