2 * V4L2 AIM - V4L2 Application Interface Module for MostCore
4 * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/suspend.h>
21 #include <linux/videodev2.h>
22 #include <linux/mutex.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-fh.h>
33 #define V4L2_AIM_MAX_INPUT 1
36 struct most_video_dev {
37 struct most_interface *iface;
39 struct list_head list;
42 struct list_head pending_mbos;
45 struct v4l2_device v4l2_dev;
47 struct video_device *vdev;
48 unsigned int ctrl_input;
52 wait_queue_head_t wait_data;
56 /* must be the first field of this struct! */
58 struct most_video_dev *mdev;
63 static struct list_head video_devices = LIST_HEAD_INIT(video_devices);
64 static struct spinlock list_lock;
67 static inline bool data_ready(struct most_video_dev *mdev)
69 return !list_empty(&mdev->pending_mbos);
72 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
74 return list_first_entry(&mdev->pending_mbos, struct mbo, list);
78 static int aim_vdev_open(struct file *filp)
81 struct video_device *vdev = video_devdata(filp);
82 struct most_video_dev *mdev = video_drvdata(filp);
85 pr_info("aim_vdev_open()\n");
87 switch (vdev->vfl_type) {
88 case VFL_TYPE_GRABBER:
94 fh = kzalloc(sizeof(struct aim_fh), GFP_KERNEL);
98 if (!atomic_inc_and_test(&mdev->access_ref)) {
99 pr_err("too many clients\n");
105 v4l2_fh_init(&fh->fh, vdev);
106 filp->private_data = fh;
108 v4l2_fh_add(&fh->fh);
110 ret = most_start_channel(mdev->iface, mdev->ch_idx);
112 pr_err("most_start_channel() failed\n");
119 v4l2_fh_del(&fh->fh);
120 v4l2_fh_exit(&fh->fh);
123 atomic_dec(&mdev->access_ref);
128 static int aim_vdev_close(struct file *filp)
130 struct aim_fh *fh = filp->private_data;
131 struct most_video_dev *mdev = fh->mdev;
132 struct mbo *mbo, *tmp;
134 pr_info("aim_vdev_close()\n");
137 * We need to put MBOs back before we call most_stop_channel()
138 * to deallocate MBOs.
139 * From the other hand mostcore still calling rx_completion()
140 * to deliver MBOs until most_stop_channel() is called.
141 * Use mute to work around this issue.
142 * This must be implemented in core.
145 spin_lock(&mdev->list_lock);
147 list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
148 list_del(&mbo->list);
149 spin_unlock(&mdev->list_lock);
151 spin_lock(&mdev->list_lock);
153 spin_unlock(&mdev->list_lock);
154 most_stop_channel(mdev->iface, mdev->ch_idx);
157 v4l2_fh_del(&fh->fh);
158 v4l2_fh_exit(&fh->fh);
160 atomic_dec(&mdev->access_ref);
165 static ssize_t aim_vdev_read(struct file *filp, char __user *buf,
166 size_t count, loff_t *pos)
168 struct aim_fh *fh = filp->private_data;
169 struct most_video_dev *mdev = fh->mdev;
178 /* wait for the first buffer */
179 if (!(filp->f_flags & O_NONBLOCK)) {
180 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
184 if (!data_ready(mdev))
187 while (count > 0 && data_ready(mdev)) {
188 struct mbo *const mbo = get_top_mbo(mdev);
189 int const rem = mbo->processed_length - fh->offs;
190 int const cnt = rem < count ? rem : count;
192 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
193 pr_err("read: copy_to_user failed\n");
206 spin_lock(&mdev->list_lock);
207 list_del(&mbo->list);
208 spin_unlock(&mdev->list_lock);
215 static unsigned int aim_vdev_poll(struct file *filp, poll_table *wait)
217 struct aim_fh *fh = filp->private_data;
218 struct most_video_dev *mdev = fh->mdev;
219 unsigned int mask = 0;
221 /* only wait if no data is available */
222 if (!data_ready(mdev))
223 poll_wait(filp, &mdev->wait_data, wait);
224 if (data_ready(mdev))
225 mask |= POLLIN | POLLRDNORM;
230 static void aim_set_format_struct(struct v4l2_format *f)
232 f->fmt.pix.width = 8;
233 f->fmt.pix.height = 8;
234 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
235 f->fmt.pix.bytesperline = 0;
236 f->fmt.pix.sizeimage = 188 * 2;
237 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
238 f->fmt.pix.field = V4L2_FIELD_NONE;
242 static int aim_set_format(struct most_video_dev *mdev, unsigned int cmd,
243 struct v4l2_format *format)
246 u32 const pixfmt = format->fmt.pix.pixelformat;
249 if (pixfmt != V4L2_PIX_FMT_MPEG) {
250 if (cmd == VIDIOC_TRY_FMT)
251 fmt = KERN_ERR "try %c%c%c%c failed\n";
253 fmt = KERN_ERR "set %c%c%c%c failed\n";
255 if (cmd == VIDIOC_TRY_FMT)
256 fmt = KERN_ERR "try %c%c%c%c\n";
258 fmt = KERN_ERR "set %c%c%c%c\n";
263 (pixfmt >> 16) & 255,
264 (pixfmt >> 24) & 255);
267 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
270 if (cmd == VIDIOC_TRY_FMT)
273 aim_set_format_struct(format);
279 static int vidioc_querycap(struct file *file, void *priv,
280 struct v4l2_capability *cap)
282 struct aim_fh *fh = priv;
283 struct most_video_dev *mdev = fh->mdev;
285 pr_info("vidioc_querycap()\n");
287 strlcpy(cap->driver, "v4l2_most_aim", sizeof(cap->driver));
288 strlcpy(cap->card, "my_card", sizeof(cap->card));
289 snprintf(cap->bus_info, sizeof(cap->bus_info),
290 "%s", mdev->iface->description);
295 V4L2_CAP_VIDEO_CAPTURE;
299 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
300 struct v4l2_fmtdesc *f)
302 pr_info("vidioc_enum_fmt_vid_cap() %d\n", f->index);
307 strcpy(f->description, "MPEG");
308 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
309 f->flags = V4L2_FMT_FLAG_COMPRESSED;
310 f->pixelformat = V4L2_PIX_FMT_MPEG;
315 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
316 struct v4l2_format *f)
318 pr_info("vidioc_g_fmt_vid_cap()\n");
320 aim_set_format_struct(f);
324 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
325 struct v4l2_format *f)
327 struct aim_fh *fh = priv;
328 struct most_video_dev *mdev = fh->mdev;
330 return aim_set_format(mdev, VIDIOC_TRY_FMT, f);
333 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
334 struct v4l2_format *f)
336 struct aim_fh *fh = priv;
337 struct most_video_dev *mdev = fh->mdev;
339 return aim_set_format(mdev, VIDIOC_S_FMT, f);
342 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
344 pr_info("vidioc_g_std()\n");
346 *norm = V4L2_STD_UNKNOWN;
350 static int vidioc_enum_input(struct file *file, void *priv,
351 struct v4l2_input *input)
353 struct aim_fh *fh = priv;
354 struct most_video_dev *mdev = fh->mdev;
356 if (input->index >= V4L2_AIM_MAX_INPUT)
359 strcpy(input->name, "MOST Video");
360 input->type |= V4L2_INPUT_TYPE_CAMERA;
363 input->std = mdev->vdev->tvnorms;
368 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
370 struct aim_fh *fh = priv;
371 struct most_video_dev *mdev = fh->mdev;
372 *i = mdev->ctrl_input;
376 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
378 struct aim_fh *fh = priv;
379 struct most_video_dev *mdev = fh->mdev;
381 pr_info("vidioc_s_input(%d)\n", index);
383 if (index >= V4L2_AIM_MAX_INPUT)
385 mdev->ctrl_input = index;
389 static struct v4l2_file_operations aim_fops = {
390 .owner = THIS_MODULE,
391 .open = aim_vdev_open,
392 .release = aim_vdev_close,
393 .read = aim_vdev_read,
394 .poll = aim_vdev_poll,
395 .unlocked_ioctl = video_ioctl2,
398 static const struct v4l2_ioctl_ops video_ioctl_ops = {
399 .vidioc_querycap = vidioc_querycap,
400 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
401 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
402 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
403 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
404 .vidioc_g_std = vidioc_g_std,
405 .vidioc_enum_input = vidioc_enum_input,
406 .vidioc_g_input = vidioc_g_input,
407 .vidioc_s_input = vidioc_s_input,
410 static const struct video_device aim_videodev_template = {
412 .release = video_device_release,
413 .ioctl_ops = &video_ioctl_ops,
414 .tvnorms = V4L2_STD_UNKNOWN,
417 /**************************************************************************/
419 static struct most_video_dev *get_aim_dev(
420 struct most_interface *iface, int channel_idx)
422 struct most_video_dev *mdev, *tmp;
424 spin_lock(&list_lock);
425 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
426 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
427 spin_unlock(&list_lock);
431 spin_unlock(&list_lock);
435 static int aim_rx_data(struct mbo *mbo)
437 struct most_video_dev *mdev =
438 get_aim_dev(mbo->ifp, mbo->hdm_channel_id);
443 spin_lock(&mdev->list_lock);
444 if (unlikely(mdev->mute)) {
445 spin_unlock(&mdev->list_lock);
449 list_add_tail(&mbo->list, &mdev->pending_mbos);
450 spin_unlock(&mdev->list_lock);
451 wake_up_interruptible(&mdev->wait_data);
455 static int aim_register_videodev(struct most_video_dev *mdev)
457 int retval = -ENOMEM;
460 pr_info("aim_register_videodev()\n");
462 init_waitqueue_head(&mdev->wait_data);
464 /* allocate and fill v4l2 video struct */
465 mdev->vdev = video_device_alloc();
469 /* Fill the video capture device struct */
470 *mdev->vdev = aim_videodev_template;
471 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
472 mdev->vdev->lock = &mdev->lock;
473 strcpy(mdev->vdev->name, "most v4l2 aim video");
475 /* Register the v4l2 device */
476 video_set_drvdata(mdev->vdev, mdev);
477 retval = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
479 pr_err("video_register_device failed (%d)\n", retval);
487 video_device_release(mdev->vdev);
491 static void aim_unregister_videodev(struct most_video_dev *mdev)
493 pr_info("aim_unregister_videodev()\n");
495 video_unregister_device(mdev->vdev);
499 static void aim_v4l2_dev_release(struct v4l2_device *v4l2_dev)
501 struct most_video_dev *mdev =
502 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
504 v4l2_device_unregister(v4l2_dev);
508 static int aim_probe_channel(struct most_interface *iface, int channel_idx,
509 struct most_channel_config *ccfg,
510 struct kobject *parent, char *name)
513 struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
515 pr_info("aim_probe_channel()\n");
518 pr_err("channel already linked\n");
522 if (ccfg->direction != MOST_CH_RX) {
523 pr_err("wrong direction, expect rx\n");
527 if (ccfg->data_type != MOST_CH_SYNC &&
528 ccfg->data_type != MOST_CH_ISOC_AVP) {
529 pr_err("wrong channel type, expect sync or isoc_avp\n");
533 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
537 mutex_init(&mdev->lock);
538 atomic_set(&mdev->access_ref, -1);
539 spin_lock_init(&mdev->list_lock);
540 INIT_LIST_HEAD(&mdev->pending_mbos);
542 mdev->ch_idx = channel_idx;
543 mdev->v4l2_dev.release = aim_v4l2_dev_release;
545 /* Create the v4l2_device */
546 strlcpy(mdev->v4l2_dev.name, "most_video_device",
547 sizeof(mdev->v4l2_dev.name));
548 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
550 pr_err("v4l2_device_register() failed\n");
555 ret = aim_register_videodev(mdev);
559 spin_lock(&list_lock);
560 list_add(&mdev->list, &video_devices);
561 spin_unlock(&list_lock);
565 v4l2_device_disconnect(&mdev->v4l2_dev);
566 v4l2_device_put(&mdev->v4l2_dev);
570 static int aim_disconnect_channel(struct most_interface *iface,
573 struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
575 pr_info("aim_disconnect_channel()\n");
578 pr_err("no such channel is linked\n");
582 spin_lock(&list_lock);
583 list_del(&mdev->list);
584 spin_unlock(&list_lock);
586 aim_unregister_videodev(mdev);
587 v4l2_device_disconnect(&mdev->v4l2_dev);
588 v4l2_device_put(&mdev->v4l2_dev);
592 static struct most_aim aim_info = {
594 .probe_channel = aim_probe_channel,
595 .disconnect_channel = aim_disconnect_channel,
596 .rx_completion = aim_rx_data,
599 static int __init aim_init(void)
601 spin_lock_init(&list_lock);
602 return most_register_aim(&aim_info);
605 static void __exit aim_exit(void)
607 struct most_video_dev *mdev, *tmp;
610 * As the mostcore currently doesn't call disconnect_channel()
611 * for linked channels while we call most_deregister_aim()
612 * we simulate this call here.
613 * This must be fixed in core.
615 spin_lock(&list_lock);
616 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
617 list_del(&mdev->list);
618 spin_unlock(&list_lock);
620 aim_unregister_videodev(mdev);
621 v4l2_device_disconnect(&mdev->v4l2_dev);
622 v4l2_device_put(&mdev->v4l2_dev);
623 spin_lock(&list_lock);
625 spin_unlock(&list_lock);
627 most_deregister_aim(&aim_info);
628 BUG_ON(!list_empty(&video_devices));
631 module_init(aim_init);
632 module_exit(aim_exit);
634 MODULE_DESCRIPTION("V4L2 Application Interface Module for MostCore");
635 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
636 MODULE_LICENSE("GPL");