Merge tag 'gfs2-merge-window' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / most / aim-v4l2 / video.c
1 /*
2  * V4L2 AIM - V4L2 Application Interface Module for MostCore
3  *
4  * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
5  *
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.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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>
29
30 #include "mostcore.h"
31
32
33 #define V4L2_AIM_MAX_INPUT  1
34
35
36 struct most_video_dev {
37         struct most_interface *iface;
38         int ch_idx;
39         struct list_head list;
40         bool mute;
41
42         struct list_head pending_mbos;
43         spinlock_t list_lock;
44
45         struct v4l2_device v4l2_dev;
46         atomic_t access_ref;
47         struct video_device *vdev;
48         unsigned int ctrl_input;
49
50         struct mutex lock;
51
52         wait_queue_head_t wait_data;
53 };
54
55 struct aim_fh {
56         /* must be the first field of this struct! */
57         struct v4l2_fh fh;
58         struct most_video_dev *mdev;
59         u32 offs;
60 };
61
62
63 static struct list_head video_devices = LIST_HEAD_INIT(video_devices);
64 static struct spinlock list_lock;
65 static struct most_aim aim_info;
66
67
68 static inline bool data_ready(struct most_video_dev *mdev)
69 {
70         return !list_empty(&mdev->pending_mbos);
71 }
72
73 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
74 {
75         return list_first_entry(&mdev->pending_mbos, struct mbo, list);
76 }
77
78
79 static int aim_vdev_open(struct file *filp)
80 {
81         int ret;
82         struct video_device *vdev = video_devdata(filp);
83         struct most_video_dev *mdev = video_drvdata(filp);
84         struct aim_fh *fh;
85
86         pr_info("aim_vdev_open()\n");
87
88         switch (vdev->vfl_type) {
89         case VFL_TYPE_GRABBER:
90                 break;
91         default:
92                 return -EINVAL;
93         }
94
95         fh = kzalloc(sizeof(struct aim_fh), GFP_KERNEL);
96         if (!fh)
97                 return -ENOMEM;
98
99         if (!atomic_inc_and_test(&mdev->access_ref)) {
100                 pr_err("too many clients\n");
101                 ret = -EBUSY;
102                 goto err_dec;
103         }
104
105         fh->mdev = mdev;
106         v4l2_fh_init(&fh->fh, vdev);
107         filp->private_data = fh;
108
109         v4l2_fh_add(&fh->fh);
110
111         ret = most_start_channel(mdev->iface, mdev->ch_idx);
112         if (ret) {
113                 pr_err("most_start_channel() failed\n");
114                 goto err_rm;
115         }
116
117         return 0;
118
119 err_rm:
120         v4l2_fh_del(&fh->fh);
121         v4l2_fh_exit(&fh->fh);
122
123 err_dec:
124         atomic_dec(&mdev->access_ref);
125         kfree(fh);
126         return ret;
127 }
128
129 static int aim_vdev_close(struct file *filp)
130 {
131         struct aim_fh *fh = filp->private_data;
132         struct most_video_dev *mdev = fh->mdev;
133         struct mbo *mbo, *tmp;
134
135         pr_info("aim_vdev_close()\n");
136
137         /*
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.
144          */
145
146         spin_lock(&mdev->list_lock);
147         mdev->mute = true;
148         list_for_each_entry_safe(mbo, tmp, &mdev->pending_mbos, list) {
149                 list_del(&mbo->list);
150                 spin_unlock(&mdev->list_lock);
151                 most_put_mbo(mbo);
152                 spin_lock(&mdev->list_lock);
153         }
154         spin_unlock(&mdev->list_lock);
155         most_stop_channel(mdev->iface, mdev->ch_idx);
156         mdev->mute = false;
157
158         v4l2_fh_del(&fh->fh);
159         v4l2_fh_exit(&fh->fh);
160
161         atomic_dec(&mdev->access_ref);
162         kfree(fh);
163         return 0;
164 }
165
166 static ssize_t aim_vdev_read(struct file *filp, char __user *buf,
167                              size_t count, loff_t *pos)
168 {
169         struct aim_fh *fh = filp->private_data;
170         struct most_video_dev *mdev = fh->mdev;
171         int ret = 0;
172
173         if (*pos)
174                 return -ESPIPE;
175
176         if (!mdev)
177                 return -ENODEV;
178
179         /* wait for the first buffer */
180         if (!(filp->f_flags & O_NONBLOCK)) {
181                 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
182                         return -ERESTARTSYS;
183         }
184
185         if (!data_ready(mdev))
186                 return -EAGAIN;
187
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;
192
193                 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
194                         pr_err("read: copy_to_user failed\n");
195                         if (!ret)
196                                 ret = -EFAULT;
197                         return ret;
198                 }
199
200                 fh->offs += cnt;
201                 count -= cnt;
202                 buf += cnt;
203                 ret += cnt;
204
205                 if (cnt >= rem) {
206                         fh->offs = 0;
207                         spin_lock(&mdev->list_lock);
208                         list_del(&mbo->list);
209                         spin_unlock(&mdev->list_lock);
210                         most_put_mbo(mbo);
211                 }
212         }
213         return ret;
214 }
215
216 static unsigned int aim_vdev_poll(struct file *filp, poll_table *wait)
217 {
218         struct aim_fh *fh = filp->private_data;
219         struct most_video_dev *mdev = fh->mdev;
220         unsigned int mask = 0;
221
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;
227
228         return mask;
229 }
230
231 static void aim_set_format_struct(struct v4l2_format *f)
232 {
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;
240         f->fmt.pix.priv = 0;
241 }
242
243 static int aim_set_format(struct most_video_dev *mdev, unsigned int cmd,
244                           struct v4l2_format *format)
245 {
246 #if 0
247         u32 const pixfmt = format->fmt.pix.pixelformat;
248         const char *fmt;
249
250         if (pixfmt != V4L2_PIX_FMT_MPEG) {
251                 if (cmd == VIDIOC_TRY_FMT)
252                         fmt = KERN_ERR "try %c%c%c%c failed\n";
253                 else
254                         fmt = KERN_ERR "set %c%c%c%c failed\n";
255         } else {
256                 if (cmd == VIDIOC_TRY_FMT)
257                         fmt = KERN_ERR "try %c%c%c%c\n";
258                 else
259                         fmt = KERN_ERR "set %c%c%c%c\n";
260         }
261         printk(fmt,
262                (pixfmt) & 255,
263                (pixfmt >> 8) & 255,
264                (pixfmt >> 16) & 255,
265                (pixfmt >> 24) & 255);
266 #endif
267
268         if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
269                 return -EINVAL;
270
271         if (cmd == VIDIOC_TRY_FMT)
272                 return 0;
273
274         aim_set_format_struct(format);
275
276         return 0;
277 }
278
279
280 static int vidioc_querycap(struct file *file, void  *priv,
281                            struct v4l2_capability *cap)
282 {
283         struct aim_fh *fh = priv;
284         struct most_video_dev *mdev = fh->mdev;
285
286         pr_info("vidioc_querycap()\n");
287
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);
292
293         cap->capabilities =
294                 V4L2_CAP_READWRITE |
295                 V4L2_CAP_TUNER |
296                 V4L2_CAP_VIDEO_CAPTURE;
297         return 0;
298 }
299
300 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
301                                    struct v4l2_fmtdesc *f)
302 {
303         pr_info("vidioc_enum_fmt_vid_cap() %d\n", f->index);
304
305         if (f->index)
306                 return -EINVAL;
307
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;
312
313         return 0;
314 }
315
316 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
317                                 struct v4l2_format *f)
318 {
319         pr_info("vidioc_g_fmt_vid_cap()\n");
320
321         aim_set_format_struct(f);
322         return 0;
323 }
324
325 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
326                                   struct v4l2_format *f)
327 {
328         struct aim_fh *fh  = priv;
329         struct most_video_dev *mdev = fh->mdev;
330
331         return aim_set_format(mdev, VIDIOC_TRY_FMT, f);
332 }
333
334 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
335                                 struct v4l2_format *f)
336 {
337         struct aim_fh *fh  = priv;
338         struct most_video_dev *mdev = fh->mdev;
339
340         return aim_set_format(mdev, VIDIOC_S_FMT, f);
341 }
342
343 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
344 {
345         pr_info("vidioc_g_std()\n");
346
347         *norm = V4L2_STD_UNKNOWN;
348         return 0;
349 }
350
351 static int vidioc_enum_input(struct file *file, void *priv,
352                              struct v4l2_input *input)
353 {
354         struct aim_fh *fh = priv;
355         struct most_video_dev *mdev = fh->mdev;
356
357         if (input->index >= V4L2_AIM_MAX_INPUT)
358                 return -EINVAL;
359
360         strcpy(input->name, "MOST Video");
361         input->type |= V4L2_INPUT_TYPE_CAMERA;
362         input->audioset = 0;
363
364         input->std = mdev->vdev->tvnorms;
365
366         return 0;
367 }
368
369 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
370 {
371         struct aim_fh *fh = priv;
372         struct most_video_dev *mdev = fh->mdev;
373         *i = mdev->ctrl_input;
374         return 0;
375 }
376
377 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
378 {
379         struct aim_fh *fh = priv;
380         struct most_video_dev *mdev = fh->mdev;
381
382         pr_info("vidioc_s_input(%d)\n", index);
383
384         if (index >= V4L2_AIM_MAX_INPUT)
385                 return -EINVAL;
386         mdev->ctrl_input = index;
387         return 0;
388 }
389
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,
397 };
398
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,
409 };
410
411 static const struct video_device aim_videodev_template = {
412         .fops = &aim_fops,
413         .release = video_device_release,
414         .ioctl_ops = &video_ioctl_ops,
415         .tvnorms = V4L2_STD_UNKNOWN,
416 };
417
418 /**************************************************************************/
419
420 static struct most_video_dev *get_aim_dev(
421         struct most_interface *iface, int channel_idx)
422 {
423         struct most_video_dev *mdev, *tmp;
424
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);
429                         return mdev;
430                 }
431         }
432         spin_unlock(&list_lock);
433         return 0;
434 }
435
436 static int aim_rx_data(struct mbo *mbo)
437 {
438         struct most_video_dev *mdev =
439                 get_aim_dev(mbo->ifp, mbo->hdm_channel_id);
440
441         if (!mdev)
442                 return -EIO;
443
444         spin_lock(&mdev->list_lock);
445         if (unlikely(mdev->mute)) {
446                 spin_unlock(&mdev->list_lock);
447                 return -EIO;
448         }
449
450         list_add_tail(&mbo->list, &mdev->pending_mbos);
451         spin_unlock(&mdev->list_lock);
452         wake_up_interruptible(&mdev->wait_data);
453         return 0;
454 }
455
456 static int aim_register_videodev(struct most_video_dev *mdev)
457 {
458         int retval = -ENOMEM;
459         int ret;
460
461         pr_info("aim_register_videodev()\n");
462
463         init_waitqueue_head(&mdev->wait_data);
464
465         /* allocate and fill v4l2 video struct */
466         mdev->vdev = video_device_alloc();
467         if (!mdev->vdev)
468                 return -ENOMEM;
469
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");
475
476         /* Register the v4l2 device */
477         video_set_drvdata(mdev->vdev, mdev);
478         retval = video_register_device(mdev->vdev, VFL_TYPE_GRABBER, -1);
479         if (retval != 0) {
480                 pr_err("video_register_device failed (%d)\n", retval);
481                 ret = -ENODEV;
482                 goto err_vbi_dev;
483         }
484
485         return 0;
486
487 err_vbi_dev:
488         video_device_release(mdev->vdev);
489         return ret;
490 }
491
492 static void aim_unregister_videodev(struct most_video_dev *mdev)
493 {
494         pr_info("aim_unregister_videodev()\n");
495
496         video_unregister_device(mdev->vdev);
497 }
498
499
500 static void aim_v4l2_dev_release(struct v4l2_device *v4l2_dev)
501 {
502         struct most_video_dev *mdev =
503                 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
504
505         v4l2_device_unregister(v4l2_dev);
506         kfree(mdev);
507 }
508
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)
512 {
513         int ret;
514         struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
515
516         pr_info("aim_probe_channel()\n");
517
518         if (mdev) {
519                 pr_err("channel already linked\n");
520                 return -EEXIST;
521         }
522
523         if (ccfg->direction != MOST_CH_RX) {
524                 pr_err("wrong direction, expect rx\n");
525                 return -EINVAL;
526         }
527
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");
531                 return -EINVAL;
532         }
533
534         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
535         if (!mdev)
536                 return -ENOMEM;
537
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);
542         mdev->iface = iface;
543         mdev->ch_idx = channel_idx;
544         mdev->v4l2_dev.release = aim_v4l2_dev_release;
545
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);
550         if (ret) {
551                 pr_err("v4l2_device_register() failed\n");
552                 kfree(mdev);
553                 return ret;
554         }
555
556         ret = aim_register_videodev(mdev);
557         if (ret)
558                 goto err_unreg;
559
560         spin_lock(&list_lock);
561         list_add(&mdev->list, &video_devices);
562         spin_unlock(&list_lock);
563         return 0;
564
565 err_unreg:
566         v4l2_device_disconnect(&mdev->v4l2_dev);
567         v4l2_device_put(&mdev->v4l2_dev);
568         return ret;
569 }
570
571 static int aim_disconnect_channel(struct most_interface *iface,
572                                   int channel_idx)
573 {
574         struct most_video_dev *mdev = get_aim_dev(iface, channel_idx);
575
576         pr_info("aim_disconnect_channel()\n");
577
578         if (!mdev) {
579                 pr_err("no such channel is linked\n");
580                 return -ENOENT;
581         }
582
583         spin_lock(&list_lock);
584         list_del(&mdev->list);
585         spin_unlock(&list_lock);
586
587         aim_unregister_videodev(mdev);
588         v4l2_device_disconnect(&mdev->v4l2_dev);
589         v4l2_device_put(&mdev->v4l2_dev);
590         return 0;
591 }
592
593 static int __init aim_init(void)
594 {
595         spin_lock_init(&list_lock);
596
597         aim_info.name = "v4l";
598         aim_info.probe_channel = aim_probe_channel;
599         aim_info.disconnect_channel = aim_disconnect_channel;
600         aim_info.rx_completion = aim_rx_data;
601         return most_register_aim(&aim_info);
602 }
603
604 static void __exit aim_exit(void)
605 {
606         struct most_video_dev *mdev, *tmp;
607
608         /*
609          * As the mostcore currently doesn't call disconnect_channel()
610          * for linked channels while we call most_deregister_aim()
611          * we simulate this call here.
612          * This must be fixed in core.
613          */
614         spin_lock(&list_lock);
615         list_for_each_entry_safe(mdev, tmp, &video_devices, list) {
616                 list_del(&mdev->list);
617                 spin_unlock(&list_lock);
618
619                 aim_unregister_videodev(mdev);
620                 v4l2_device_disconnect(&mdev->v4l2_dev);
621                 v4l2_device_put(&mdev->v4l2_dev);
622                 spin_lock(&list_lock);
623         }
624         spin_unlock(&list_lock);
625
626         most_deregister_aim(&aim_info);
627         BUG_ON(!list_empty(&video_devices));
628 }
629
630 module_init(aim_init);
631 module_exit(aim_exit);
632
633 MODULE_DESCRIPTION("V4L2 Application Interface Module for MostCore");
634 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
635 MODULE_LICENSE("GPL");