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