File Index
Files and directories
Sign in
FILE

video.c

File preview with syntax highlighting.

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