18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// em28xx-vbi.c - VBI driver for em28xx
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
68c2ecf20Sopenharmony_ci//
78c2ecf20Sopenharmony_ci// This work was sponsored by EyeMagnet Limited.
88c2ecf20Sopenharmony_ci//
98c2ecf20Sopenharmony_ci// This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci// it under the terms of the GNU General Public License as published by
118c2ecf20Sopenharmony_ci// the Free Software Foundation; either version 2 of the License, or
128c2ecf20Sopenharmony_ci// (at your option) any later version.
138c2ecf20Sopenharmony_ci//
148c2ecf20Sopenharmony_ci// This program is distributed in the hope that it will be useful,
158c2ecf20Sopenharmony_ci// but WITHOUT ANY WARRANTY; without even the implied warranty of
168c2ecf20Sopenharmony_ci// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
178c2ecf20Sopenharmony_ci// GNU General Public License for more details.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "em28xx.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/kernel.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/hardirq.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/usb.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "em28xx-v4l.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------ */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int vbi_queue_setup(struct vb2_queue *vq,
328c2ecf20Sopenharmony_ci			   unsigned int *nbuffers, unsigned int *nplanes,
338c2ecf20Sopenharmony_ci			   unsigned int sizes[], struct device *alloc_devs[])
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct em28xx *dev = vb2_get_drv_priv(vq);
368c2ecf20Sopenharmony_ci	struct em28xx_v4l2 *v4l2 = dev->v4l2;
378c2ecf20Sopenharmony_ci	unsigned long size = v4l2->vbi_width * v4l2->vbi_height * 2;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (*nbuffers < 2)
408c2ecf20Sopenharmony_ci		*nbuffers = 2;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (*nplanes) {
438c2ecf20Sopenharmony_ci		if (sizes[0] < size)
448c2ecf20Sopenharmony_ci			return -EINVAL;
458c2ecf20Sopenharmony_ci		size = sizes[0];
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	*nplanes = 1;
498c2ecf20Sopenharmony_ci	sizes[0] = size;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int vbi_buffer_prepare(struct vb2_buffer *vb)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct em28xx        *dev  = vb2_get_drv_priv(vb->vb2_queue);
578c2ecf20Sopenharmony_ci	struct em28xx_v4l2   *v4l2 = dev->v4l2;
588c2ecf20Sopenharmony_ci	unsigned long        size;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	size = v4l2->vbi_width * v4l2->vbi_height * 2;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (vb2_plane_size(vb, 0) < size) {
638c2ecf20Sopenharmony_ci		dev_info(&dev->intf->dev,
648c2ecf20Sopenharmony_ci			 "%s data will not fit into plane (%lu < %lu)\n",
658c2ecf20Sopenharmony_ci			 __func__, vb2_plane_size(vb, 0), size);
668c2ecf20Sopenharmony_ci		return -EINVAL;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	vb2_set_plane_payload(vb, 0, size);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void
748c2ecf20Sopenharmony_civbi_buffer_queue(struct vb2_buffer *vb)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
778c2ecf20Sopenharmony_ci	struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
788c2ecf20Sopenharmony_ci	struct em28xx_buffer *buf =
798c2ecf20Sopenharmony_ci		container_of(vbuf, struct em28xx_buffer, vb);
808c2ecf20Sopenharmony_ci	struct em28xx_dmaqueue *vbiq = &dev->vbiq;
818c2ecf20Sopenharmony_ci	unsigned long flags = 0;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	buf->mem = vb2_plane_vaddr(vb, 0);
848c2ecf20Sopenharmony_ci	buf->length = vb2_plane_size(vb, 0);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->slock, flags);
878c2ecf20Sopenharmony_ci	list_add_tail(&buf->list, &vbiq->active);
888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->slock, flags);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciconst struct vb2_ops em28xx_vbi_qops = {
928c2ecf20Sopenharmony_ci	.queue_setup    = vbi_queue_setup,
938c2ecf20Sopenharmony_ci	.buf_prepare    = vbi_buffer_prepare,
948c2ecf20Sopenharmony_ci	.buf_queue      = vbi_buffer_queue,
958c2ecf20Sopenharmony_ci	.start_streaming = em28xx_start_analog_streaming,
968c2ecf20Sopenharmony_ci	.stop_streaming = em28xx_stop_vbi_streaming,
978c2ecf20Sopenharmony_ci	.wait_prepare   = vb2_ops_wait_prepare,
988c2ecf20Sopenharmony_ci	.wait_finish    = vb2_ops_wait_finish,
998c2ecf20Sopenharmony_ci};
100