162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci    buffer queues.
462306a36Sopenharmony_ci    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
562306a36Sopenharmony_ci    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
662306a36Sopenharmony_ci    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef IVTV_QUEUE_H
1162306a36Sopenharmony_ci#define IVTV_QUEUE_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define IVTV_DMA_UNMAPPED	((u32) -1)
1462306a36Sopenharmony_ci#define SLICED_VBI_PIO 0
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* ivtv_buffer utility functions */
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistatic inline int ivtv_might_use_pio(struct ivtv_stream *s)
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	return s->dma == DMA_NONE || (SLICED_VBI_PIO && s->type == IVTV_ENC_STREAM_TYPE_VBI);
2162306a36Sopenharmony_ci}
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistatic inline int ivtv_use_pio(struct ivtv_stream *s)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	struct ivtv *itv = s->itv;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	return s->dma == DMA_NONE ||
2862306a36Sopenharmony_ci	    (SLICED_VBI_PIO && s->type == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set);
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic inline int ivtv_might_use_dma(struct ivtv_stream *s)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	return s->dma != DMA_NONE;
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic inline int ivtv_use_dma(struct ivtv_stream *s)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	return !ivtv_use_pio(s);
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic inline void ivtv_buf_sync_for_cpu(struct ivtv_stream *s, struct ivtv_buffer *buf)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	if (ivtv_use_dma(s))
4462306a36Sopenharmony_ci		dma_sync_single_for_cpu(&s->itv->pdev->dev, buf->dma_handle,
4562306a36Sopenharmony_ci					s->buf_size + 256, s->dma);
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic inline void ivtv_buf_sync_for_device(struct ivtv_stream *s, struct ivtv_buffer *buf)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	if (ivtv_use_dma(s))
5162306a36Sopenharmony_ci		dma_sync_single_for_device(&s->itv->pdev->dev,
5262306a36Sopenharmony_ci					   buf->dma_handle, s->buf_size + 256,
5362306a36Sopenharmony_ci					   s->dma);
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciint ivtv_buf_copy_from_user(struct ivtv_stream *s, struct ivtv_buffer *buf, const char __user *src, int copybytes);
5762306a36Sopenharmony_civoid ivtv_buf_swap(struct ivtv_buffer *buf);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/* ivtv_queue utility functions */
6062306a36Sopenharmony_civoid ivtv_queue_init(struct ivtv_queue *q);
6162306a36Sopenharmony_civoid ivtv_enqueue(struct ivtv_stream *s, struct ivtv_buffer *buf, struct ivtv_queue *q);
6262306a36Sopenharmony_cistruct ivtv_buffer *ivtv_dequeue(struct ivtv_stream *s, struct ivtv_queue *q);
6362306a36Sopenharmony_ciint ivtv_queue_move(struct ivtv_stream *s, struct ivtv_queue *from, struct ivtv_queue *steal,
6462306a36Sopenharmony_ci		    struct ivtv_queue *to, int needed_bytes);
6562306a36Sopenharmony_civoid ivtv_flush_queues(struct ivtv_stream *s);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* ivtv_stream utility functions */
6862306a36Sopenharmony_ciint ivtv_stream_alloc(struct ivtv_stream *s);
6962306a36Sopenharmony_civoid ivtv_stream_free(struct ivtv_stream *s);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic inline void ivtv_stream_sync_for_cpu(struct ivtv_stream *s)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	if (ivtv_use_dma(s))
7462306a36Sopenharmony_ci		dma_sync_single_for_cpu(&s->itv->pdev->dev, s->sg_handle,
7562306a36Sopenharmony_ci					sizeof(struct ivtv_sg_element),
7662306a36Sopenharmony_ci					DMA_TO_DEVICE);
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic inline void ivtv_stream_sync_for_device(struct ivtv_stream *s)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	if (ivtv_use_dma(s))
8262306a36Sopenharmony_ci		dma_sync_single_for_device(&s->itv->pdev->dev, s->sg_handle,
8362306a36Sopenharmony_ci					   sizeof(struct ivtv_sg_element),
8462306a36Sopenharmony_ci					   DMA_TO_DEVICE);
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#endif
88