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
35 static struct most_aim aim_info;
37 struct most_video_dev {
38 struct most_interface *iface;
40 struct list_head list;
43 struct list_head pending_mbos;
46 struct v4l2_device v4l2_dev;
48 struct video_device *vdev;
49 unsigned int ctrl_input;
53 wait_queue_head_t wait_data;
57 /* must be the first field of this struct! */
59 struct most_video_dev *mdev;
64 static struct list_head video_devices = LIST_HEAD_INIT(video_devices);
65 static struct spinlock list_lock;
68 static inline bool data_ready(struct most_video_dev *mdev)
70 return !list_empty(&mdev->pending_mbos);
73 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
75 return list_first_entry(&mdev->pending_mbos, struct mbo, list);
79 static int aim_vdev_open(struct file *filp)
82 struct video_device *vdev = video_devdata(filp);
83 struct most_video_dev *mdev = video_drvdata(filp);
86 pr_info("aim_vdev_open()\n");
88 switch (vdev->vfl_type) {
89 case VFL_TYPE_GRABBER:
95 fh = kzalloc(sizeof(struct aim_fh), GFP_KERNEL);
99 if (!atomic_inc_and_test(&mdev->access_ref)) {
100 pr_err("too many clients\n");
106 v4l2_fh_init(&fh->fh, vdev);
107 filp->private_data = fh;
109 v4l2_fh_add(&fh->fh);
111 ret = most_start_channel(mdev->iface, mdev->ch_idx, &aim_info);
113 pr_err("most_start_channel() failed\n");
120 v4l2_fh_del(&fh->fh);
121 v4l2_fh_exit(&fh->fh);
124 atomic_dec(&mdev->access_ref);
129 static int aim_vdev_close(struct file *filp)
131 struct aim_fh *fh = filp->private_data;
132 struct most_video_dev *mdev = fh->mdev;
133 struct mbo *mbo, *tmp;
135 pr_info("aim_vdev_close()\n");
138 * We need to put MBOs back before we call most_stop_channel()
139 * to deallocate MBOs.
140 * From the other hand mostcore still calling rx_completion()
141 * to deliver MBOs until most_stop_channel() is called.
142 * Use mute to work around this issue.
143 * This must be implemented in core.
146 spin_lock(&mdev->list_lock);
148 list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
149 list_del(&mbo->list);
150 spin_unlock(&mdev->list_lock);
152 spin_lock(&mdev->list_lock);
154 spin_unlock(&mdev->list_lock);
155 most_stop_channel(mdev->iface, mdev->ch_idx, &aim_info);
158 v4l2_fh_del(&fh->fh);
159 v4l2_fh_exit(&fh->fh);
161 atomic_dec(&mdev->access_ref);
166 static ssize_t aim_vdev_read(struct file *filp, char __user *buf,
167 size_t count, loff_t *pos)
169 struct aim_fh *fh = filp->private_data;
170 struct most_video_dev *mdev = fh->mdev;
179 /* wait for the first buffer */
180 if (!(filp->f_flags & O_NONBLOCK)) {
181 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
185 if (!data_ready(mdev))
188 while (count > 0 && data_ready(mdev)) {
189 struct mbo *const mbo = get_top_mbo(mdev);
190 int const rem = mbo->processed_length - fh->offs;
191 int const cnt = rem < count ? rem : count;
193 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
194 pr_err("read: copy_to_user failed\n");
207 spin_lock(&mdev->list_lock);
208 list_del(&mbo->list);
209 spin_unlock(&mdev->list_lock);
216 static unsigned int aim_vdev_poll(struct file *filp, poll_table *wait)
218 struct aim_fh *fh = filp->private_data;
219 struct most_video_dev *mdev = fh->mdev;
220 unsigned int mask = 0;
222 /* only wait if no data is available */
223 if (!data_ready(mdev))
224 poll_wait(filp, &mdev->wait_data, wait);
225 if (data_ready(mdev))
226 mask |= POLLIN | POLLRDNORM;
231 static void aim_set_format_struct(struct v4l2_format *f)
233 f->fmt.pix.width = 8;
234 f->fmt.pix.height = 8;
235 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
236 f->fmt.pix.bytesperline = 0;
237 f->fmt.pix.sizeimage = 188 * 2;
238 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
239 f->fmt.pix.field = V4L2_FIELD_NONE;
243 static int aim_set_format(struct most_video_dev *mdev, unsigned int cmd,
244 struct v4l2_format *format)
247 u32 const pixfmt = format->fmt.pix.pixelformat;
250 if (pixfmt != V4L2_PIX_FMT_MPEG) {
251 if (cmd == VIDIOC_TRY_FMT)
252 fmt = KERN_ERR "try %c%c%c%c failed\n";
254 fmt = KERN_ERR "set %c%c%c%c failed\n";
256 if (cmd == VIDIOC_TRY_FMT)
257 fmt = KERN_ERR "try %c%c%c%c\n";
259 fmt = KERN_ERR "set %c%c%c%c\n";
264 (pixfmt >> 16) & 255,
265 (pixfmt >> 24) & 255);
268 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
271 if (cmd == VIDIOC_TRY_FMT)
274 aim_set_format_struct(format);
280 static int vidioc_querycap(struct file *file, void *priv,
281 struct v4l2_capability *cap)
283 struct aim_fh *fh = priv;
284 struct most_video_dev *mdev = fh->mdev;
286 pr_info("vidioc_querycap()\n");
288 strlcpy(cap->driver, "v4l2_most_aim", sizeof(cap->driver));
289 strlcpy(cap->card, "my_card", sizeof(cap->card));
290 snprintf(cap->bus_info, sizeof(cap->bus_info),
291 "%s", mdev->iface->description);
296 V4L2_CAP_VIDEO_CAPTURE;
300 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
301 struct v4l2_fmtdesc *f)
303 pr_info("vidioc_enum_fmt_vid_cap() %d\n", f->index);
308 strcpy(f->description, "MPEG");
309 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
310 f->flags = V4L2_FMT_FLAG_COMPRESSED;
311 f->pixelformat = V4L2_PIX_FMT_MPEG;
316 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
317 struct v4l2_format *f)
319 pr_info("vidioc_g_fmt_vid_cap()\n");
321 aim_set_format_struct(f);
325 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
326 struct v4l2_format *f)
328 struct aim_fh *fh = priv;
329 struct most_video_dev *mdev = fh->mdev;
331 return aim_set_format(mdev, VIDIOC_TRY_FMT, f);
334 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
335 struct v4l2_format *f)
337 struct aim_fh *fh = priv;
338 struct most_video_dev *mdev = fh->mdev;
340 return aim_set_format(mdev, VIDIOC_S_FMT, f);
343 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
345 pr_info("vidioc_g_std()\n");
347 *norm = V4L2_STD_UNKNOWN;
351 static int vidioc_enum_input(struct file *file, void *priv,
352 struct v4l2_input *input)
354 struct aim_fh *fh = priv;
355 struct most_video_dev *mdev = fh->mdev;
357 if (input->index >= V4L2_AIM_MAX_INPUT)
360 strcpy(input->name, "MOST Video");
361 input->type |= V4L2_INPUT_TYPE_CAMERA;
364 input->std = mdev->vdev->tvnorms;
369 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
371 struct aim_fh *fh = priv;
372 struct most_video_dev *mdev = fh->mdev;
373 *i = mdev->ctrl_input;
377 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
379 struct aim_fh *fh = priv;
380 struct most_video_dev *mdev = fh->mdev;
382 pr_info("vidioc_s_input(%d)\n", index);
384 if (index >= V4L2_AIM_MAX_INPUT)
386 mdev->ctrl_input = index;
390 static struct v4l2_file_operations aim_fops = {
391 .owner = THIS_MODULE,
392 .open = aim_vdev_open,
393 .release = aim_vdev_close,
394 .read = aim_vdev_read,
395 .poll = aim_vdev_poll,
396 .unlocked_ioctl = video_ioctl2,
399 static const struct v4l2_ioctl_ops video_ioctl_ops = {
400 .vidioc_querycap = vidioc_querycap,
401 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
402 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
403 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
404 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
405 .vidioc_g_std = vidioc_g_std,
406 .vidioc_enum_input = vidioc_enum_input,
407 .vidioc_g_input = vidioc_g_input,
408 .vidioc_s_input = vidioc_s_input,
411 static const struct video_device aim_videodev_template = {
413 .release = video_device_release,
414 .ioctl_ops = &video_ioctl_ops,
415 .tvnorms = V4L2_STD_UNKNOWN,
418 /**************************************************************************/
420 static struct most_video_dev *get_aim_dev(
421 struct most_interface *iface, int channel_idx)
423 struct most_video_dev *mdev, *tmp;
425 spin_lock(&list_lock);
426 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
427 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
428 spin_unlock(&list_lock);
432 spin_unlock(&list_lock);
436 static int aim_rx_data(struct mbo *mbo)
438 struct most_video_dev *mdev =
439 get_aim_dev(mbo->ifp, mbo->hdm_channel_id);
444 spin_lock(&mdev->list_lock);
445 if (unlikely(mdev->mute)) {
446 spin_unlock(&mdev->list_lock);
450 list_add_tail(&mbo->list, &mdev->pending_mbos);
451 spin_unlock(&mdev->list_lock);
452 wake_up_interruptible(&mdev->wait_data);
456 static int aim_register_videodev(struct most_video_dev *mdev)
458 int retval = -ENOMEM;
461 pr_info("aim_register_videodev()\n");
463 init_waitqueue_head(&mdev->wait_data);
465 /* allocate and fill v4l2 video struct */
466 mdev->vdev = video_device_alloc();
470 /* Fill the video capture device struct */
471 *mdev->vdev = aim_videodev_template;
472 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
473 mdev->vdev->lock = &mdev->lock;
474 strcpy(mdev->vdev->name, "most v4l2 aim video");
476 /* Register the v4l2 device */
477 video_set_drvdata(mdev->vdev, mdev);
478 retval = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
480 pr_err("video_register_device failed (%d)\n", retval);
488 video_device_release(mdev->vdev);
492 static void aim_unregister_videodev(struct most_video_dev *mdev)
494 pr_info("aim_unregister_videodev()\n");
496 video_unregister_device(mdev->vdev);
500 static void aim_v4l2_dev_release(struct v4l2_device *v4l2_dev)
502 struct most_video_dev *mdev =
503 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
505 v4l2_device_unregister(v4l2_dev);
509 static int aim_probe_channel(struct most_interface *iface, int channel_idx,
510 struct most_channel_config *ccfg,
511 struct kobject *parent, char *name)
514 struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
516 pr_info("aim_probe_channel()\n");
519 pr_err("channel already linked\n");
523 if (ccfg->direction != MOST_CH_RX) {
524 pr_err("wrong direction, expect rx\n");
528 if (ccfg->data_type != MOST_CH_SYNC &&
529 ccfg->data_type != MOST_CH_ISOC_AVP) {
530 pr_err("wrong channel type, expect sync or isoc_avp\n");
534 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
538 mutex_init(&mdev->lock);
539 atomic_set(&mdev->access_ref, -1);
540 spin_lock_init(&mdev->list_lock);
541 INIT_LIST_HEAD(&mdev->pending_mbos);
543 mdev->ch_idx = channel_idx;
544 mdev->v4l2_dev.release = aim_v4l2_dev_release;
546 /* Create the v4l2_device */
547 strlcpy(mdev->v4l2_dev.name, "most_video_device",
548 sizeof(mdev->v4l2_dev.name));
549 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
551 pr_err("v4l2_device_register() failed\n");
556 ret = aim_register_videodev(mdev);
560 spin_lock(&list_lock);
561 list_add(&mdev->list, &video_devices);
562 spin_unlock(&list_lock);
566 v4l2_device_disconnect(&mdev->v4l2_dev);
567 v4l2_device_put(&mdev->v4l2_dev);
571 static int aim_disconnect_channel(struct most_interface *iface,
574 struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
576 pr_info("aim_disconnect_channel()\n");
579 pr_err("no such channel is linked\n");
583 spin_lock(&list_lock);
584 list_del(&mdev->list);
585 spin_unlock(&list_lock);
587 aim_unregister_videodev(mdev);
588 v4l2_device_disconnect(&mdev->v4l2_dev);
589 v4l2_device_put(&mdev->v4l2_dev);
593 static struct most_aim aim_info = {
595 .probe_channel = aim_probe_channel,
596 .disconnect_channel = aim_disconnect_channel,
597 .rx_completion = aim_rx_data,
600 static int __init aim_init(void)
602 spin_lock_init(&list_lock);
603 return most_register_aim(&aim_info);
606 static void __exit aim_exit(void)
608 struct most_video_dev *mdev, *tmp;
611 * As the mostcore currently doesn't call disconnect_channel()
612 * for linked channels while we call most_deregister_aim()
613 * we simulate this call here.
614 * This must be fixed in core.
616 spin_lock(&list_lock);
617 list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
618 list_del(&mdev->list);
619 spin_unlock(&list_lock);
621 aim_unregister_videodev(mdev);
622 v4l2_device_disconnect(&mdev->v4l2_dev);
623 v4l2_device_put(&mdev->v4l2_dev);
624 spin_lock(&list_lock);
626 spin_unlock(&list_lock);
628 most_deregister_aim(&aim_info);
629 BUG_ON(!list_empty(&video_devices));
632 module_init(aim_init);
633 module_exit(aim_exit);
635 MODULE_DESCRIPTION("V4L2 Application Interface Module for MostCore");
636 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
637 MODULE_LICENSE("GPL");