1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * video.c - V4L2 component for Mostcore
4 *
5 * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
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>
24
25 #define V4L2_CMP_MAX_INPUT 1
26
27 static struct most_component comp;
28
29 struct most_video_dev {
30 struct most_interface *iface;
31 int ch_idx;
32 struct list_head list;
33 bool mute;
34
35 struct list_head pending_mbos;
36 spinlock_t list_lock;
37
38 struct v4l2_device v4l2_dev;
39 atomic_t access_ref;
40 struct video_device *vdev;
41 unsigned int ctrl_input;
42
43 struct mutex lock;
44
45 wait_queue_head_t wait_data;
46 };
47
48 struct comp_fh {
49 /* must be the first field of this struct! */
50 struct v4l2_fh fh;
51 struct most_video_dev *mdev;
52 u32 offs;
53 };
54
to_comp_fh(struct file * filp)55 static inline struct comp_fh *to_comp_fh(struct file *filp)
56 {
57 return container_of(file_to_v4l2_fh(filp), struct comp_fh, fh);
58 }
59
60 static LIST_HEAD(video_devices);
61 static DEFINE_SPINLOCK(list_lock);
62
data_ready(struct most_video_dev * mdev)63 static inline bool data_ready(struct most_video_dev *mdev)
64 {
65 return !list_empty(&mdev->pending_mbos);
66 }
67
get_top_mbo(struct most_video_dev * mdev)68 static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
69 {
70 return list_first_entry(&mdev->pending_mbos, struct mbo, list);
71 }
72
comp_vdev_open(struct file * filp)73 static int comp_vdev_open(struct file *filp)
74 {
75 int ret;
76 struct video_device *vdev = video_devdata(filp);
77 struct most_video_dev *mdev = video_drvdata(filp);
78 struct comp_fh *fh;
79
80 switch (vdev->vfl_type) {
81 case VFL_TYPE_VIDEO:
82 break;
83 default:
84 return -EINVAL;
85 }
86
87 fh = kzalloc_obj(*fh);
88 if (!fh)
89 return -ENOMEM;
90
91 if (!atomic_inc_and_test(&mdev->access_ref)) {
92 v4l2_err(&mdev->v4l2_dev, "too many clients\n");
93 ret = -EBUSY;
94 goto err_dec;
95 }
96
97 fh->mdev = mdev;
98 v4l2_fh_init(&fh->fh, vdev);
99 v4l2_fh_add(&fh->fh, filp);
100
101 ret = most_start_channel(mdev->iface, mdev->ch_idx, &comp);
102 if (ret) {
103 v4l2_err(&mdev->v4l2_dev, "most_start_channel() failed\n");
104 goto err_rm;
105 }
106
107 return 0;
108
109 err_rm:
110 v4l2_fh_del(&fh->fh, filp);
111 v4l2_fh_exit(&fh->fh);
112
113 err_dec:
114 atomic_dec(&mdev->access_ref);
115 kfree(fh);
116 return ret;
117 }
118
comp_vdev_close(struct file * filp)119 static int comp_vdev_close(struct file *filp)
120 {
121 struct comp_fh *fh = to_comp_fh(filp);
122 struct most_video_dev *mdev = fh->mdev;
123 struct mbo *mbo, *tmp;
124 LIST_HEAD(free_list);
125
126 /*
127 * We need to put MBOs back before we call most_stop_channel()
128 * to deallocate MBOs.
129 * From the other hand mostcore still calling rx_completion()
130 * to deliver MBOs until most_stop_channel() is called.
131 * Use mute to work around this issue.
132 * This must be implemented in core.
133 */
134
135 spin_lock_irq(&mdev->list_lock);
136 mdev->mute = true;
137 list_replace_init(&mdev->pending_mbos, &free_list);
138 spin_unlock_irq(&mdev->list_lock);
139
140 list_for_each_entry_safe(mbo, tmp, &free_list, list) {
141 list_del_init(&mbo->list);
142 most_put_mbo(mbo);
143 }
144
145 most_stop_channel(mdev->iface, mdev->ch_idx, &comp);
146 mdev->mute = false;
147
148 v4l2_fh_del(&fh->fh, filp);
149 v4l2_fh_exit(&fh->fh);
150
151 atomic_dec(&mdev->access_ref);
152 kfree(fh);
153 return 0;
154 }
155
comp_vdev_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)156 static ssize_t comp_vdev_read(struct file *filp, char __user *buf,
157 size_t count, loff_t *pos)
158 {
159 struct comp_fh *fh = to_comp_fh(filp);
160 struct most_video_dev *mdev = fh->mdev;
161 int ret = 0;
162
163 if (*pos)
164 return -ESPIPE;
165
166 if (!mdev)
167 return -ENODEV;
168
169 /* wait for the first buffer */
170 if (!(filp->f_flags & O_NONBLOCK)) {
171 if (wait_event_interruptible(mdev->wait_data, data_ready(mdev)))
172 return -ERESTARTSYS;
173 }
174
175 if (!data_ready(mdev))
176 return -EAGAIN;
177
178 while (count > 0 && data_ready(mdev)) {
179 struct mbo *const mbo = get_top_mbo(mdev);
180 int const rem = mbo->processed_length - fh->offs;
181 int const cnt = rem < count ? rem : count;
182
183 if (copy_to_user(buf, mbo->virt_address + fh->offs, cnt)) {
184 v4l2_err(&mdev->v4l2_dev, "read: copy_to_user failed\n");
185 if (!ret)
186 ret = -EFAULT;
187 return ret;
188 }
189
190 fh->offs += cnt;
191 count -= cnt;
192 buf += cnt;
193 ret += cnt;
194
195 if (cnt >= rem) {
196 fh->offs = 0;
197 spin_lock_irq(&mdev->list_lock);
198 list_del(&mbo->list);
199 spin_unlock_irq(&mdev->list_lock);
200 most_put_mbo(mbo);
201 }
202 }
203 return ret;
204 }
205
comp_vdev_poll(struct file * filp,poll_table * wait)206 static __poll_t comp_vdev_poll(struct file *filp, poll_table *wait)
207 {
208 struct comp_fh *fh = to_comp_fh(filp);
209 struct most_video_dev *mdev = fh->mdev;
210 __poll_t mask = 0;
211
212 /* only wait if no data is available */
213 if (!data_ready(mdev))
214 poll_wait(filp, &mdev->wait_data, wait);
215 if (data_ready(mdev))
216 mask |= EPOLLIN | EPOLLRDNORM;
217
218 return mask;
219 }
220
comp_set_format_struct(struct v4l2_format * f)221 static void comp_set_format_struct(struct v4l2_format *f)
222 {
223 f->fmt.pix.width = 8;
224 f->fmt.pix.height = 8;
225 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
226 f->fmt.pix.bytesperline = 0;
227 f->fmt.pix.sizeimage = 188 * 2;
228 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
229 f->fmt.pix.field = V4L2_FIELD_NONE;
230 f->fmt.pix.priv = 0;
231 }
232
comp_set_format(struct most_video_dev * mdev,unsigned int cmd,struct v4l2_format * format)233 static int comp_set_format(struct most_video_dev *mdev, unsigned int cmd,
234 struct v4l2_format *format)
235 {
236 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG)
237 return -EINVAL;
238
239 if (cmd == VIDIOC_TRY_FMT)
240 return 0;
241
242 comp_set_format_struct(format);
243
244 return 0;
245 }
246
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)247 static int vidioc_querycap(struct file *file, void *priv,
248 struct v4l2_capability *cap)
249 {
250 struct comp_fh *fh = priv;
251 struct most_video_dev *mdev = fh->mdev;
252
253 strscpy(cap->driver, "v4l2_component", sizeof(cap->driver));
254 strscpy(cap->card, "MOST", sizeof(cap->card));
255 snprintf(cap->bus_info, sizeof(cap->bus_info),
256 "%s", mdev->iface->description);
257 return 0;
258 }
259
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)260 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
261 struct v4l2_fmtdesc *f)
262 {
263 if (f->index)
264 return -EINVAL;
265
266 strscpy(f->description, "MPEG", sizeof(f->description));
267 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
268 f->flags = V4L2_FMT_FLAG_COMPRESSED;
269 f->pixelformat = V4L2_PIX_FMT_MPEG;
270
271 return 0;
272 }
273
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)274 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
275 struct v4l2_format *f)
276 {
277 comp_set_format_struct(f);
278 return 0;
279 }
280
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)281 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
282 struct v4l2_format *f)
283 {
284 struct comp_fh *fh = priv;
285 struct most_video_dev *mdev = fh->mdev;
286
287 return comp_set_format(mdev, VIDIOC_TRY_FMT, f);
288 }
289
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)290 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
291 struct v4l2_format *f)
292 {
293 struct comp_fh *fh = priv;
294 struct most_video_dev *mdev = fh->mdev;
295
296 return comp_set_format(mdev, VIDIOC_S_FMT, f);
297 }
298
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)299 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
300 {
301 *norm = V4L2_STD_UNKNOWN;
302 return 0;
303 }
304
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * input)305 static int vidioc_enum_input(struct file *file, void *priv,
306 struct v4l2_input *input)
307 {
308 struct comp_fh *fh = priv;
309 struct most_video_dev *mdev = fh->mdev;
310
311 if (input->index >= V4L2_CMP_MAX_INPUT)
312 return -EINVAL;
313
314 strscpy(input->name, "MOST Video", sizeof(input->name));
315 input->type |= V4L2_INPUT_TYPE_CAMERA;
316 input->audioset = 0;
317
318 input->std = mdev->vdev->tvnorms;
319
320 return 0;
321 }
322
vidioc_g_input(struct file * file,void * priv,unsigned int * i)323 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
324 {
325 struct comp_fh *fh = priv;
326 struct most_video_dev *mdev = fh->mdev;
327 *i = mdev->ctrl_input;
328 return 0;
329 }
330
vidioc_s_input(struct file * file,void * priv,unsigned int index)331 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
332 {
333 struct comp_fh *fh = priv;
334 struct most_video_dev *mdev = fh->mdev;
335
336 if (index >= V4L2_CMP_MAX_INPUT)
337 return -EINVAL;
338 mdev->ctrl_input = index;
339 return 0;
340 }
341
342 static const struct v4l2_file_operations comp_fops = {
343 .owner = THIS_MODULE,
344 .open = comp_vdev_open,
345 .release = comp_vdev_close,
346 .read = comp_vdev_read,
347 .poll = comp_vdev_poll,
348 .unlocked_ioctl = video_ioctl2,
349 };
350
351 static const struct v4l2_ioctl_ops video_ioctl_ops = {
352 .vidioc_querycap = vidioc_querycap,
353 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
354 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
355 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
356 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
357 .vidioc_g_std = vidioc_g_std,
358 .vidioc_enum_input = vidioc_enum_input,
359 .vidioc_g_input = vidioc_g_input,
360 .vidioc_s_input = vidioc_s_input,
361 };
362
363 static const struct video_device comp_videodev_template = {
364 .fops = &comp_fops,
365 .release = video_device_release,
366 .ioctl_ops = &video_ioctl_ops,
367 .tvnorms = V4L2_STD_UNKNOWN,
368 .device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE,
369 };
370
371 /**************************************************************************/
372
get_comp_dev(struct most_interface * iface,int channel_idx)373 static struct most_video_dev *get_comp_dev(struct most_interface *iface, int channel_idx)
374 {
375 struct most_video_dev *mdev;
376 unsigned long flags;
377
378 spin_lock_irqsave(&list_lock, flags);
379 list_for_each_entry(mdev, &video_devices, list) {
380 if (mdev->iface == iface && mdev->ch_idx == channel_idx) {
381 spin_unlock_irqrestore(&list_lock, flags);
382 return mdev;
383 }
384 }
385 spin_unlock_irqrestore(&list_lock, flags);
386 return NULL;
387 }
388
comp_rx_data(struct mbo * mbo)389 static int comp_rx_data(struct mbo *mbo)
390 {
391 unsigned long flags;
392 struct most_video_dev *mdev =
393 get_comp_dev(mbo->ifp, mbo->hdm_channel_id);
394
395 if (!mdev)
396 return -EIO;
397
398 spin_lock_irqsave(&mdev->list_lock, flags);
399 if (unlikely(mdev->mute)) {
400 spin_unlock_irqrestore(&mdev->list_lock, flags);
401 return -EIO;
402 }
403
404 list_add_tail(&mbo->list, &mdev->pending_mbos);
405 spin_unlock_irqrestore(&mdev->list_lock, flags);
406 wake_up_interruptible(&mdev->wait_data);
407 return 0;
408 }
409
comp_register_videodev(struct most_video_dev * mdev)410 static int comp_register_videodev(struct most_video_dev *mdev)
411 {
412 int ret;
413
414 init_waitqueue_head(&mdev->wait_data);
415
416 /* allocate and fill v4l2 video struct */
417 mdev->vdev = video_device_alloc();
418 if (!mdev->vdev)
419 return -ENOMEM;
420
421 /* Fill the video capture device struct */
422 *mdev->vdev = comp_videodev_template;
423 mdev->vdev->v4l2_dev = &mdev->v4l2_dev;
424 mdev->vdev->lock = &mdev->lock;
425 snprintf(mdev->vdev->name, sizeof(mdev->vdev->name), "MOST: %s",
426 mdev->v4l2_dev.name);
427
428 /* Register the v4l2 device */
429 video_set_drvdata(mdev->vdev, mdev);
430 ret = video_register_device(mdev->vdev, VFL_TYPE_VIDEO, -1);
431 if (ret) {
432 v4l2_err(&mdev->v4l2_dev, "video_register_device failed (%d)\n",
433 ret);
434 video_device_release(mdev->vdev);
435 }
436
437 return ret;
438 }
439
comp_unregister_videodev(struct most_video_dev * mdev)440 static void comp_unregister_videodev(struct most_video_dev *mdev)
441 {
442 video_unregister_device(mdev->vdev);
443 }
444
comp_v4l2_dev_release(struct v4l2_device * v4l2_dev)445 static void comp_v4l2_dev_release(struct v4l2_device *v4l2_dev)
446 {
447 struct most_video_dev *mdev =
448 container_of(v4l2_dev, struct most_video_dev, v4l2_dev);
449
450 v4l2_device_unregister(v4l2_dev);
451 kfree(mdev);
452 }
453
comp_probe_channel(struct most_interface * iface,int channel_idx,struct most_channel_config * ccfg,char * name,char * args)454 static int comp_probe_channel(struct most_interface *iface, int channel_idx,
455 struct most_channel_config *ccfg, char *name,
456 char *args)
457 {
458 int ret;
459 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
460
461 if (mdev) {
462 pr_err("Channel already linked\n");
463 return -EEXIST;
464 }
465
466 if (ccfg->direction != MOST_CH_RX) {
467 pr_err("Wrong direction, expected rx\n");
468 return -EINVAL;
469 }
470
471 if (ccfg->data_type != MOST_CH_SYNC &&
472 ccfg->data_type != MOST_CH_ISOC) {
473 pr_err("Wrong channel type, expected sync or isoc\n");
474 return -EINVAL;
475 }
476
477 mdev = kzalloc_obj(*mdev);
478 if (!mdev)
479 return -ENOMEM;
480
481 mutex_init(&mdev->lock);
482 atomic_set(&mdev->access_ref, -1);
483 spin_lock_init(&mdev->list_lock);
484 INIT_LIST_HEAD(&mdev->pending_mbos);
485 mdev->iface = iface;
486 mdev->ch_idx = channel_idx;
487 mdev->v4l2_dev.release = comp_v4l2_dev_release;
488
489 /* Create the v4l2_device */
490 strscpy(mdev->v4l2_dev.name, name, sizeof(mdev->v4l2_dev.name));
491 ret = v4l2_device_register(NULL, &mdev->v4l2_dev);
492 if (ret) {
493 pr_err("v4l2_device_register() failed\n");
494 kfree(mdev);
495 return ret;
496 }
497
498 ret = comp_register_videodev(mdev);
499 if (ret)
500 goto err_unreg;
501
502 spin_lock_irq(&list_lock);
503 list_add(&mdev->list, &video_devices);
504 spin_unlock_irq(&list_lock);
505 return 0;
506
507 err_unreg:
508 v4l2_device_disconnect(&mdev->v4l2_dev);
509 v4l2_device_put(&mdev->v4l2_dev);
510 return ret;
511 }
512
comp_disconnect_channel(struct most_interface * iface,int channel_idx)513 static int comp_disconnect_channel(struct most_interface *iface,
514 int channel_idx)
515 {
516 struct most_video_dev *mdev = get_comp_dev(iface, channel_idx);
517
518 if (!mdev) {
519 pr_err("no such channel is linked\n");
520 return -ENOENT;
521 }
522
523 spin_lock_irq(&list_lock);
524 list_del(&mdev->list);
525 spin_unlock_irq(&list_lock);
526
527 comp_unregister_videodev(mdev);
528 v4l2_device_disconnect(&mdev->v4l2_dev);
529 v4l2_device_put(&mdev->v4l2_dev);
530 return 0;
531 }
532
533 static struct most_component comp = {
534 .mod = THIS_MODULE,
535 .name = "video",
536 .probe_channel = comp_probe_channel,
537 .disconnect_channel = comp_disconnect_channel,
538 .rx_completion = comp_rx_data,
539 };
540
comp_init(void)541 static int __init comp_init(void)
542 {
543 int err;
544
545 err = most_register_component(&comp);
546 if (err)
547 return err;
548 err = most_register_configfs_subsys(&comp);
549 if (err) {
550 most_deregister_component(&comp);
551 return err;
552 }
553 return 0;
554 }
555
comp_exit(void)556 static void __exit comp_exit(void)
557 {
558 struct most_video_dev *mdev, *tmp;
559 LIST_HEAD(free_list);
560
561 /*
562 * As the mostcore currently doesn't call disconnect_channel()
563 * for linked channels while we call most_deregister_component()
564 * we simulate this call here.
565 * This must be fixed in core.
566 */
567 spin_lock_irq(&list_lock);
568 list_replace_init(&video_devices, &free_list);
569 spin_unlock_irq(&list_lock);
570
571 list_for_each_entry_safe(mdev, tmp, &free_list, list) {
572 list_del_init(&mdev->list);
573 comp_unregister_videodev(mdev);
574 v4l2_device_disconnect(&mdev->v4l2_dev);
575 v4l2_device_put(&mdev->v4l2_dev);
576 }
577
578 most_deregister_configfs_subsys(&comp);
579 most_deregister_component(&comp);
580 BUG_ON(!list_empty(&video_devices));
581 }
582
583 module_init(comp_init);
584 module_exit(comp_exit);
585
586 MODULE_DESCRIPTION("V4L2 Component Module for Mostcore");
587 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
588 MODULE_LICENSE("GPL");
589