162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  cx18 driver internal defines and structures
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Derived from ivtv-driver.h
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
862306a36Sopenharmony_ci *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef CX18_DRIVER_H
1262306a36Sopenharmony_ci#define CX18_DRIVER_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/moduleparam.h>
1662306a36Sopenharmony_ci#include <linux/init.h>
1762306a36Sopenharmony_ci#include <linux/delay.h>
1862306a36Sopenharmony_ci#include <linux/sched/signal.h>
1962306a36Sopenharmony_ci#include <linux/fs.h>
2062306a36Sopenharmony_ci#include <linux/pci.h>
2162306a36Sopenharmony_ci#include <linux/interrupt.h>
2262306a36Sopenharmony_ci#include <linux/spinlock.h>
2362306a36Sopenharmony_ci#include <linux/i2c.h>
2462306a36Sopenharmony_ci#include <linux/i2c-algo-bit.h>
2562306a36Sopenharmony_ci#include <linux/list.h>
2662306a36Sopenharmony_ci#include <linux/unistd.h>
2762306a36Sopenharmony_ci#include <linux/pagemap.h>
2862306a36Sopenharmony_ci#include <linux/workqueue.h>
2962306a36Sopenharmony_ci#include <linux/mutex.h>
3062306a36Sopenharmony_ci#include <linux/slab.h>
3162306a36Sopenharmony_ci#include <asm/byteorder.h>
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#include <media/v4l2-common.h>
3462306a36Sopenharmony_ci#include <media/v4l2-ioctl.h>
3562306a36Sopenharmony_ci#include <media/v4l2-device.h>
3662306a36Sopenharmony_ci#include <media/v4l2-fh.h>
3762306a36Sopenharmony_ci#include <media/tuner.h>
3862306a36Sopenharmony_ci#include <media/i2c/ir-kbd-i2c.h>
3962306a36Sopenharmony_ci#include "cx18-mailbox.h"
4062306a36Sopenharmony_ci#include "cx18-av-core.h"
4162306a36Sopenharmony_ci#include "cx23418.h"
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* DVB */
4462306a36Sopenharmony_ci#include <media/demux.h>
4562306a36Sopenharmony_ci#include <media/dmxdev.h>
4662306a36Sopenharmony_ci#include <media/dvb_demux.h>
4762306a36Sopenharmony_ci#include <media/dvb_frontend.h>
4862306a36Sopenharmony_ci#include <media/dvb_net.h>
4962306a36Sopenharmony_ci#include <media/dvbdev.h>
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* vb2 YUV support */
5262306a36Sopenharmony_ci#include <media/videobuf2-vmalloc.h>
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#ifndef CONFIG_PCI
5562306a36Sopenharmony_ci#  error "This driver requires kernel PCI support."
5662306a36Sopenharmony_ci#endif
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#define CX18_MEM_OFFSET	0x00000000
5962306a36Sopenharmony_ci#define CX18_MEM_SIZE	0x04000000
6062306a36Sopenharmony_ci#define CX18_REG_OFFSET	0x02000000
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/* Maximum cx18 driver instances. */
6362306a36Sopenharmony_ci#define CX18_MAX_CARDS 32
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/* Supported cards */
6662306a36Sopenharmony_ci#define CX18_CARD_HVR_1600_ESMT	      0	/* Hauppauge HVR 1600 (ESMT memory) */
6762306a36Sopenharmony_ci#define CX18_CARD_HVR_1600_SAMSUNG    1	/* Hauppauge HVR 1600 (Samsung memory) */
6862306a36Sopenharmony_ci#define CX18_CARD_COMPRO_H900	      2	/* Compro VideoMate H900 */
6962306a36Sopenharmony_ci#define CX18_CARD_YUAN_MPC718	      3	/* Yuan MPC718 */
7062306a36Sopenharmony_ci#define CX18_CARD_CNXT_RAPTOR_PAL     4	/* Conexant Raptor PAL */
7162306a36Sopenharmony_ci#define CX18_CARD_TOSHIBA_QOSMIO_DVBT 5 /* Toshiba Qosmio Interal DVB-T/Analog*/
7262306a36Sopenharmony_ci#define CX18_CARD_LEADTEK_PVR2100     6 /* Leadtek WinFast PVR2100 */
7362306a36Sopenharmony_ci#define CX18_CARD_LEADTEK_DVR3100H    7 /* Leadtek WinFast DVR3100 H */
7462306a36Sopenharmony_ci#define CX18_CARD_GOTVIEW_PCI_DVD3    8 /* GoTView PCI DVD3 Hybrid */
7562306a36Sopenharmony_ci#define CX18_CARD_HVR_1600_S5H1411    9 /* Hauppauge HVR 1600 s5h1411/tda18271*/
7662306a36Sopenharmony_ci#define CX18_CARD_LAST		      9
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_MPG  0
7962306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_TS   1
8062306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_YUV  2
8162306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_VBI  3
8262306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_PCM  4
8362306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_IDX  5
8462306a36Sopenharmony_ci#define CX18_ENC_STREAM_TYPE_RAD  6
8562306a36Sopenharmony_ci#define CX18_MAX_STREAMS	  7
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/* system vendor and device IDs */
8862306a36Sopenharmony_ci#define PCI_VENDOR_ID_CX      0x14f1
8962306a36Sopenharmony_ci#define PCI_DEVICE_ID_CX23418 0x5b7a
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/* subsystem vendor ID */
9262306a36Sopenharmony_ci#define CX18_PCI_ID_HAUPPAUGE		0x0070
9362306a36Sopenharmony_ci#define CX18_PCI_ID_COMPRO		0x185b
9462306a36Sopenharmony_ci#define CX18_PCI_ID_YUAN		0x12ab
9562306a36Sopenharmony_ci#define CX18_PCI_ID_CONEXANT		0x14f1
9662306a36Sopenharmony_ci#define CX18_PCI_ID_TOSHIBA		0x1179
9762306a36Sopenharmony_ci#define CX18_PCI_ID_LEADTEK		0x107D
9862306a36Sopenharmony_ci#define CX18_PCI_ID_GOTVIEW		0x5854
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* ======================================================================== */
10162306a36Sopenharmony_ci/* ========================== START USER SETTABLE DMA VARIABLES =========== */
10262306a36Sopenharmony_ci/* ======================================================================== */
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/* DMA Buffers, Default size in MB allocated */
10562306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_TS_BUFFERS  1
10662306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_MPG_BUFFERS 2
10762306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_IDX_BUFFERS 1
10862306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_YUV_BUFFERS 2
10962306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_VBI_BUFFERS 1
11062306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_PCM_BUFFERS 1
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/* Maximum firmware DMA buffers per stream */
11362306a36Sopenharmony_ci#define CX18_MAX_FW_MDLS_PER_STREAM 63
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/* YUV buffer sizes in bytes to ensure integer # of frames per buffer */
11662306a36Sopenharmony_ci#define CX18_UNIT_ENC_YUV_BUFSIZE	(720 *  32 * 3 / 2) /* bytes */
11762306a36Sopenharmony_ci#define CX18_625_LINE_ENC_YUV_BUFSIZE	(CX18_UNIT_ENC_YUV_BUFSIZE * 576/32)
11862306a36Sopenharmony_ci#define CX18_525_LINE_ENC_YUV_BUFSIZE	(CX18_UNIT_ENC_YUV_BUFSIZE * 480/32)
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/* IDX buffer size should be a multiple of the index entry size from the chip */
12162306a36Sopenharmony_cistruct cx18_enc_idx_entry {
12262306a36Sopenharmony_ci	__le32 length;
12362306a36Sopenharmony_ci	__le32 offset_low;
12462306a36Sopenharmony_ci	__le32 offset_high;
12562306a36Sopenharmony_ci	__le32 flags;
12662306a36Sopenharmony_ci	__le32 pts_low;
12762306a36Sopenharmony_ci	__le32 pts_high;
12862306a36Sopenharmony_ci} __attribute__ ((packed));
12962306a36Sopenharmony_ci#define CX18_UNIT_ENC_IDX_BUFSIZE \
13062306a36Sopenharmony_ci	(sizeof(struct cx18_enc_idx_entry) * V4L2_ENC_IDX_ENTRIES)
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/* DMA buffer, default size in kB allocated */
13362306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_TS_BUFSIZE   32
13462306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_MPG_BUFSIZE  32
13562306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_IDX_BUFSIZE  (CX18_UNIT_ENC_IDX_BUFSIZE * 1 / 1024 + 1)
13662306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_YUV_BUFSIZE  (CX18_UNIT_ENC_YUV_BUFSIZE * 3 / 1024 + 1)
13762306a36Sopenharmony_ci#define CX18_DEFAULT_ENC_PCM_BUFSIZE   4
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/* i2c stuff */
14062306a36Sopenharmony_ci#define I2C_CLIENTS_MAX 16
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci/* debugging */
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/* Flag to turn on high volume debugging */
14562306a36Sopenharmony_ci#define CX18_DBGFLG_WARN  (1 << 0)
14662306a36Sopenharmony_ci#define CX18_DBGFLG_INFO  (1 << 1)
14762306a36Sopenharmony_ci#define CX18_DBGFLG_API   (1 << 2)
14862306a36Sopenharmony_ci#define CX18_DBGFLG_DMA   (1 << 3)
14962306a36Sopenharmony_ci#define CX18_DBGFLG_IOCTL (1 << 4)
15062306a36Sopenharmony_ci#define CX18_DBGFLG_FILE  (1 << 5)
15162306a36Sopenharmony_ci#define CX18_DBGFLG_I2C   (1 << 6)
15262306a36Sopenharmony_ci#define CX18_DBGFLG_IRQ   (1 << 7)
15362306a36Sopenharmony_ci/* Flag to turn on high volume debugging */
15462306a36Sopenharmony_ci#define CX18_DBGFLG_HIGHVOL (1 << 8)
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/* NOTE: extra space before comma in 'fmt , ## args' is required for
15762306a36Sopenharmony_ci   gcc-2.95, otherwise it won't compile. */
15862306a36Sopenharmony_ci#define CX18_DEBUG(x, type, fmt, args...) \
15962306a36Sopenharmony_ci	do { \
16062306a36Sopenharmony_ci		if ((x) & cx18_debug) \
16162306a36Sopenharmony_ci			v4l2_info(&cx->v4l2_dev, " " type ": " fmt , ## args); \
16262306a36Sopenharmony_ci	} while (0)
16362306a36Sopenharmony_ci#define CX18_DEBUG_WARN(fmt, args...)  CX18_DEBUG(CX18_DBGFLG_WARN, "warning", fmt , ## args)
16462306a36Sopenharmony_ci#define CX18_DEBUG_INFO(fmt, args...)  CX18_DEBUG(CX18_DBGFLG_INFO, "info", fmt , ## args)
16562306a36Sopenharmony_ci#define CX18_DEBUG_API(fmt, args...)   CX18_DEBUG(CX18_DBGFLG_API, "api", fmt , ## args)
16662306a36Sopenharmony_ci#define CX18_DEBUG_DMA(fmt, args...)   CX18_DEBUG(CX18_DBGFLG_DMA, "dma", fmt , ## args)
16762306a36Sopenharmony_ci#define CX18_DEBUG_IOCTL(fmt, args...) CX18_DEBUG(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
16862306a36Sopenharmony_ci#define CX18_DEBUG_FILE(fmt, args...)  CX18_DEBUG(CX18_DBGFLG_FILE, "file", fmt , ## args)
16962306a36Sopenharmony_ci#define CX18_DEBUG_I2C(fmt, args...)   CX18_DEBUG(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
17062306a36Sopenharmony_ci#define CX18_DEBUG_IRQ(fmt, args...)   CX18_DEBUG(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci#define CX18_DEBUG_HIGH_VOL(x, type, fmt, args...) \
17362306a36Sopenharmony_ci	do { \
17462306a36Sopenharmony_ci		if (((x) & cx18_debug) && (cx18_debug & CX18_DBGFLG_HIGHVOL)) \
17562306a36Sopenharmony_ci			v4l2_info(&cx->v4l2_dev, " " type ": " fmt , ## args); \
17662306a36Sopenharmony_ci	} while (0)
17762306a36Sopenharmony_ci#define CX18_DEBUG_HI_WARN(fmt, args...)  CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_WARN, "warning", fmt , ## args)
17862306a36Sopenharmony_ci#define CX18_DEBUG_HI_INFO(fmt, args...)  CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_INFO, "info", fmt , ## args)
17962306a36Sopenharmony_ci#define CX18_DEBUG_HI_API(fmt, args...)   CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_API, "api", fmt , ## args)
18062306a36Sopenharmony_ci#define CX18_DEBUG_HI_DMA(fmt, args...)   CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_DMA, "dma", fmt , ## args)
18162306a36Sopenharmony_ci#define CX18_DEBUG_HI_IOCTL(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
18262306a36Sopenharmony_ci#define CX18_DEBUG_HI_FILE(fmt, args...)  CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_FILE, "file", fmt , ## args)
18362306a36Sopenharmony_ci#define CX18_DEBUG_HI_I2C(fmt, args...)   CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
18462306a36Sopenharmony_ci#define CX18_DEBUG_HI_IRQ(fmt, args...)   CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci/* Standard kernel messages */
18762306a36Sopenharmony_ci#define CX18_ERR(fmt, args...)      v4l2_err(&cx->v4l2_dev, fmt , ## args)
18862306a36Sopenharmony_ci#define CX18_WARN(fmt, args...)     v4l2_warn(&cx->v4l2_dev, fmt , ## args)
18962306a36Sopenharmony_ci#define CX18_INFO(fmt, args...)     v4l2_info(&cx->v4l2_dev, fmt , ## args)
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci/* Messages for internal subdevs to use */
19262306a36Sopenharmony_ci#define CX18_DEBUG_DEV(x, dev, type, fmt, args...) \
19362306a36Sopenharmony_ci	do { \
19462306a36Sopenharmony_ci		if ((x) & cx18_debug) \
19562306a36Sopenharmony_ci			v4l2_info(dev, " " type ": " fmt , ## args); \
19662306a36Sopenharmony_ci	} while (0)
19762306a36Sopenharmony_ci#define CX18_DEBUG_WARN_DEV(dev, fmt, args...) \
19862306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_WARN, dev, "warning", fmt , ## args)
19962306a36Sopenharmony_ci#define CX18_DEBUG_INFO_DEV(dev, fmt, args...) \
20062306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_INFO, dev, "info", fmt , ## args)
20162306a36Sopenharmony_ci#define CX18_DEBUG_API_DEV(dev, fmt, args...) \
20262306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_API, dev, "api", fmt , ## args)
20362306a36Sopenharmony_ci#define CX18_DEBUG_DMA_DEV(dev, fmt, args...) \
20462306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_DMA, dev, "dma", fmt , ## args)
20562306a36Sopenharmony_ci#define CX18_DEBUG_IOCTL_DEV(dev, fmt, args...) \
20662306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_IOCTL, dev, "ioctl", fmt , ## args)
20762306a36Sopenharmony_ci#define CX18_DEBUG_FILE_DEV(dev, fmt, args...) \
20862306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_FILE, dev, "file", fmt , ## args)
20962306a36Sopenharmony_ci#define CX18_DEBUG_I2C_DEV(dev, fmt, args...) \
21062306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_I2C, dev, "i2c", fmt , ## args)
21162306a36Sopenharmony_ci#define CX18_DEBUG_IRQ_DEV(dev, fmt, args...) \
21262306a36Sopenharmony_ci		CX18_DEBUG_DEV(CX18_DBGFLG_IRQ, dev, "irq", fmt , ## args)
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#define CX18_DEBUG_HIGH_VOL_DEV(x, dev, type, fmt, args...) \
21562306a36Sopenharmony_ci	do { \
21662306a36Sopenharmony_ci		if (((x) & cx18_debug) && (cx18_debug & CX18_DBGFLG_HIGHVOL)) \
21762306a36Sopenharmony_ci			v4l2_info(dev, " " type ": " fmt , ## args); \
21862306a36Sopenharmony_ci	} while (0)
21962306a36Sopenharmony_ci#define CX18_DEBUG_HI_WARN_DEV(dev, fmt, args...) \
22062306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_WARN, dev, "warning", fmt , ## args)
22162306a36Sopenharmony_ci#define CX18_DEBUG_HI_INFO_DEV(dev, fmt, args...) \
22262306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_INFO, dev, "info", fmt , ## args)
22362306a36Sopenharmony_ci#define CX18_DEBUG_HI_API_DEV(dev, fmt, args...) \
22462306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_API, dev, "api", fmt , ## args)
22562306a36Sopenharmony_ci#define CX18_DEBUG_HI_DMA_DEV(dev, fmt, args...) \
22662306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_DMA, dev, "dma", fmt , ## args)
22762306a36Sopenharmony_ci#define CX18_DEBUG_HI_IOCTL_DEV(dev, fmt, args...) \
22862306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_IOCTL, dev, "ioctl", fmt , ## args)
22962306a36Sopenharmony_ci#define CX18_DEBUG_HI_FILE_DEV(dev, fmt, args...) \
23062306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_FILE, dev, "file", fmt , ## args)
23162306a36Sopenharmony_ci#define CX18_DEBUG_HI_I2C_DEV(dev, fmt, args...) \
23262306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_I2C, dev, "i2c", fmt , ## args)
23362306a36Sopenharmony_ci#define CX18_DEBUG_HI_IRQ_DEV(dev, fmt, args...) \
23462306a36Sopenharmony_ci	CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_IRQ, dev, "irq", fmt , ## args)
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci#define CX18_ERR_DEV(dev, fmt, args...)      v4l2_err(dev, fmt , ## args)
23762306a36Sopenharmony_ci#define CX18_WARN_DEV(dev, fmt, args...)     v4l2_warn(dev, fmt , ## args)
23862306a36Sopenharmony_ci#define CX18_INFO_DEV(dev, fmt, args...)     v4l2_info(dev, fmt , ## args)
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ciextern int cx18_debug;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistruct cx18_options {
24362306a36Sopenharmony_ci	int megabytes[CX18_MAX_STREAMS]; /* Size in megabytes of each stream */
24462306a36Sopenharmony_ci	int cardtype;		/* force card type on load */
24562306a36Sopenharmony_ci	int tuner;		/* set tuner on load */
24662306a36Sopenharmony_ci	int radio;		/* enable/disable radio */
24762306a36Sopenharmony_ci};
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/* per-mdl bit flags */
25062306a36Sopenharmony_ci#define CX18_F_M_NEED_SWAP  0	/* mdl buffer data must be endianness swapped */
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/* per-stream, s_flags */
25362306a36Sopenharmony_ci#define CX18_F_S_CLAIMED	3	/* this stream is claimed */
25462306a36Sopenharmony_ci#define CX18_F_S_STREAMING      4	/* the fw is decoding/encoding this stream */
25562306a36Sopenharmony_ci#define CX18_F_S_INTERNAL_USE	5	/* this stream is used internally (sliced VBI processing) */
25662306a36Sopenharmony_ci#define CX18_F_S_STREAMOFF	7	/* signal end of stream EOS */
25762306a36Sopenharmony_ci#define CX18_F_S_APPL_IO        8	/* this stream is used read/written by an application */
25862306a36Sopenharmony_ci#define CX18_F_S_STOPPING	9	/* telling the fw to stop capturing */
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/* per-cx18, i_flags */
26162306a36Sopenharmony_ci#define CX18_F_I_LOADED_FW		0	/* Loaded firmware 1st time */
26262306a36Sopenharmony_ci#define CX18_F_I_EOS			4	/* End of encoder stream */
26362306a36Sopenharmony_ci#define CX18_F_I_RADIO_USER		5	/* radio tuner is selected */
26462306a36Sopenharmony_ci#define CX18_F_I_ENC_PAUSED		13	/* the encoder is paused */
26562306a36Sopenharmony_ci#define CX18_F_I_INITED			21	/* set after first open */
26662306a36Sopenharmony_ci#define CX18_F_I_FAILED			22	/* set if first open failed */
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci/* These are the VBI types as they appear in the embedded VBI private packets. */
26962306a36Sopenharmony_ci#define CX18_SLICED_TYPE_TELETEXT_B     (1)
27062306a36Sopenharmony_ci#define CX18_SLICED_TYPE_CAPTION_525    (4)
27162306a36Sopenharmony_ci#define CX18_SLICED_TYPE_WSS_625        (5)
27262306a36Sopenharmony_ci#define CX18_SLICED_TYPE_VPS            (7)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci/**
27562306a36Sopenharmony_ci * list_entry_is_past_end - check if a previous loop cursor is off list end
27662306a36Sopenharmony_ci * @pos:	the type * previously used as a loop cursor.
27762306a36Sopenharmony_ci * @head:	the head for your list.
27862306a36Sopenharmony_ci * @member:	the name of the list_head within the struct.
27962306a36Sopenharmony_ci *
28062306a36Sopenharmony_ci * Check if the entry's list_head is the head of the list, thus it's not a
28162306a36Sopenharmony_ci * real entry but was the loop cursor that walked past the end
28262306a36Sopenharmony_ci */
28362306a36Sopenharmony_ci#define list_entry_is_past_end(pos, head, member) \
28462306a36Sopenharmony_ci	(&pos->member == (head))
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_cistruct cx18_vb2_buffer {
28762306a36Sopenharmony_ci	/* Common video buffer sub-system struct */
28862306a36Sopenharmony_ci	struct vb2_v4l2_buffer vb;
28962306a36Sopenharmony_ci	struct list_head list;
29062306a36Sopenharmony_ci	v4l2_std_id tvnorm; /* selected tv norm */
29162306a36Sopenharmony_ci	u32 bytes_used;
29262306a36Sopenharmony_ci};
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistruct cx18_buffer {
29562306a36Sopenharmony_ci	struct list_head list;
29662306a36Sopenharmony_ci	dma_addr_t dma_handle;
29762306a36Sopenharmony_ci	char *buf;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	u32 bytesused;
30062306a36Sopenharmony_ci	u32 readpos;
30162306a36Sopenharmony_ci};
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_cistruct cx18_mdl {
30462306a36Sopenharmony_ci	struct list_head list;
30562306a36Sopenharmony_ci	u32 id;		/* index into cx->scb->cpu_mdl[] of 1st cx18_mdl_ent */
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	unsigned int skipped;
30862306a36Sopenharmony_ci	unsigned long m_flags;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	struct list_head buf_list;
31162306a36Sopenharmony_ci	struct cx18_buffer *curr_buf; /* current buffer in list for reading */
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	u32 bytesused;
31462306a36Sopenharmony_ci	u32 readpos;
31562306a36Sopenharmony_ci};
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistruct cx18_queue {
31862306a36Sopenharmony_ci	struct list_head list;
31962306a36Sopenharmony_ci	atomic_t depth;
32062306a36Sopenharmony_ci	u32 bytesused;
32162306a36Sopenharmony_ci	spinlock_t lock;
32262306a36Sopenharmony_ci};
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_cistruct cx18_stream; /* forward reference */
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_cistruct cx18_dvb {
32762306a36Sopenharmony_ci	struct cx18_stream *stream;
32862306a36Sopenharmony_ci	struct dmx_frontend hw_frontend;
32962306a36Sopenharmony_ci	struct dmx_frontend mem_frontend;
33062306a36Sopenharmony_ci	struct dmxdev dmxdev;
33162306a36Sopenharmony_ci	struct dvb_adapter dvb_adapter;
33262306a36Sopenharmony_ci	struct dvb_demux demux;
33362306a36Sopenharmony_ci	struct dvb_frontend *fe;
33462306a36Sopenharmony_ci	struct dvb_net dvbnet;
33562306a36Sopenharmony_ci	int enabled;
33662306a36Sopenharmony_ci	int feeding;
33762306a36Sopenharmony_ci	struct mutex feedlock;
33862306a36Sopenharmony_ci};
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_cistruct cx18;	 /* forward reference */
34162306a36Sopenharmony_cistruct cx18_scb; /* forward reference */
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci#define CX18_MAX_MDL_ACKS 2
34562306a36Sopenharmony_ci#define CX18_MAX_IN_WORK_ORDERS (CX18_MAX_FW_MDLS_PER_STREAM + 7)
34662306a36Sopenharmony_ci/* CPU_DE_RELEASE_MDL can burst CX18_MAX_FW_MDLS_PER_STREAM orders in a group */
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci#define CX18_F_EWO_MB_STALE_UPON_RECEIPT 0x1
34962306a36Sopenharmony_ci#define CX18_F_EWO_MB_STALE_WHILE_PROC   0x2
35062306a36Sopenharmony_ci#define CX18_F_EWO_MB_STALE \
35162306a36Sopenharmony_ci	     (CX18_F_EWO_MB_STALE_UPON_RECEIPT | CX18_F_EWO_MB_STALE_WHILE_PROC)
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_cistruct cx18_in_work_order {
35462306a36Sopenharmony_ci	struct work_struct work;
35562306a36Sopenharmony_ci	atomic_t pending;
35662306a36Sopenharmony_ci	struct cx18 *cx;
35762306a36Sopenharmony_ci	unsigned long flags;
35862306a36Sopenharmony_ci	int rpu;
35962306a36Sopenharmony_ci	struct cx18_mailbox mb;
36062306a36Sopenharmony_ci	struct cx18_mdl_ack mdl_ack[CX18_MAX_MDL_ACKS];
36162306a36Sopenharmony_ci	char *str;
36262306a36Sopenharmony_ci};
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci#define CX18_INVALID_TASK_HANDLE 0xffffffff
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_cistruct cx18_stream {
36762306a36Sopenharmony_ci	/* These first five fields are always set, even if the stream
36862306a36Sopenharmony_ci	   is not actually created. */
36962306a36Sopenharmony_ci	struct video_device video_dev;	/* v4l2_dev is NULL when stream not created */
37062306a36Sopenharmony_ci	struct cx18_dvb *dvb;		/* DVB / Digital Transport */
37162306a36Sopenharmony_ci	struct cx18 *cx;		/* for ease of use */
37262306a36Sopenharmony_ci	const char *name;		/* name of the stream */
37362306a36Sopenharmony_ci	int type;			/* stream type */
37462306a36Sopenharmony_ci	u32 handle;			/* task handle */
37562306a36Sopenharmony_ci	u32 v4l2_dev_caps;		/* device capabilities */
37662306a36Sopenharmony_ci	unsigned int mdl_base_idx;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	u32 id;
37962306a36Sopenharmony_ci	unsigned long s_flags;	/* status flags, see above */
38062306a36Sopenharmony_ci	int dma;		/* can be PCI_DMA_TODEVICE,
38162306a36Sopenharmony_ci				   PCI_DMA_FROMDEVICE or
38262306a36Sopenharmony_ci				   PCI_DMA_NONE */
38362306a36Sopenharmony_ci	wait_queue_head_t waitq;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	/* Buffers */
38662306a36Sopenharmony_ci	struct list_head buf_pool;	/* buffers not attached to an MDL */
38762306a36Sopenharmony_ci	u32 buffers;			/* total buffers owned by this stream */
38862306a36Sopenharmony_ci	u32 buf_size;			/* size in bytes of a single buffer */
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	/* MDL sizes - all stream MDLs are the same size */
39162306a36Sopenharmony_ci	u32 bufs_per_mdl;
39262306a36Sopenharmony_ci	u32 mdl_size;		/* total bytes in all buffers in a mdl */
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	/* MDL Queues */
39562306a36Sopenharmony_ci	struct cx18_queue q_free;	/* free - in rotation, not committed */
39662306a36Sopenharmony_ci	struct cx18_queue q_busy;	/* busy - in use by firmware */
39762306a36Sopenharmony_ci	struct cx18_queue q_full;	/* full - data for user apps */
39862306a36Sopenharmony_ci	struct cx18_queue q_idle;	/* idle - not in rotation */
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci	struct work_struct out_work_order;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	/* Videobuf for YUV video */
40362306a36Sopenharmony_ci	u32 pixelformat;
40462306a36Sopenharmony_ci	u32 vb_bytes_per_frame;
40562306a36Sopenharmony_ci	u32 vb_bytes_per_line;
40662306a36Sopenharmony_ci	struct list_head vb_capture;    /* video capture queue */
40762306a36Sopenharmony_ci	spinlock_t vb_lock;
40862306a36Sopenharmony_ci	struct timer_list vb_timeout;
40962306a36Sopenharmony_ci	u32 sequence;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	struct vb2_queue vidq;
41262306a36Sopenharmony_ci	enum v4l2_buf_type vb_type;
41362306a36Sopenharmony_ci};
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_cistruct cx18_open_id {
41662306a36Sopenharmony_ci	struct v4l2_fh fh;
41762306a36Sopenharmony_ci	u32 open_id;
41862306a36Sopenharmony_ci	int type;
41962306a36Sopenharmony_ci	struct cx18 *cx;
42062306a36Sopenharmony_ci};
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_cistatic inline struct cx18_open_id *fh2id(struct v4l2_fh *fh)
42362306a36Sopenharmony_ci{
42462306a36Sopenharmony_ci	return container_of(fh, struct cx18_open_id, fh);
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_cistatic inline struct cx18_open_id *file2id(struct file *file)
42862306a36Sopenharmony_ci{
42962306a36Sopenharmony_ci	return fh2id(file->private_data);
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci/* forward declaration of struct defined in cx18-cards.h */
43362306a36Sopenharmony_cistruct cx18_card;
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci/*
43662306a36Sopenharmony_ci * A note about "sliced" VBI data as implemented in this driver:
43762306a36Sopenharmony_ci *
43862306a36Sopenharmony_ci * Currently we collect the sliced VBI in the form of Ancillary Data
43962306a36Sopenharmony_ci * packets, inserted by the AV core decoder/digitizer/slicer in the
44062306a36Sopenharmony_ci * horizontal blanking region of the VBI lines, in "raw" mode as far as
44162306a36Sopenharmony_ci * the Encoder is concerned.  We don't ever tell the Encoder itself
44262306a36Sopenharmony_ci * to provide sliced VBI. (AV Core: sliced mode - Encoder: raw mode)
44362306a36Sopenharmony_ci *
44462306a36Sopenharmony_ci * We then process the ancillary data ourselves to send the sliced data
44562306a36Sopenharmony_ci * to the user application directly or build up MPEG-2 private stream 1
44662306a36Sopenharmony_ci * packets to splice into (only!) MPEG-2 PS streams for the user app.
44762306a36Sopenharmony_ci *
44862306a36Sopenharmony_ci * (That's how ivtv essentially does it.)
44962306a36Sopenharmony_ci *
45062306a36Sopenharmony_ci * The Encoder should be able to extract certain sliced VBI data for
45162306a36Sopenharmony_ci * us and provide it in a separate stream or splice it into any type of
45262306a36Sopenharmony_ci * MPEG PS or TS stream, but this isn't implemented yet.
45362306a36Sopenharmony_ci */
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci/*
45662306a36Sopenharmony_ci * Number of "raw" VBI samples per horizontal line we tell the Encoder to
45762306a36Sopenharmony_ci * grab from the decoder/digitizer/slicer output for raw or sliced VBI.
45862306a36Sopenharmony_ci * It depends on the pixel clock and the horiz rate:
45962306a36Sopenharmony_ci *
46062306a36Sopenharmony_ci * (1/Fh)*(2*Fp) = Samples/line
46162306a36Sopenharmony_ci *     = 4 bytes EAV + Anc data in hblank + 4 bytes SAV + active samples
46262306a36Sopenharmony_ci *
46362306a36Sopenharmony_ci *  Sliced VBI data is sent as ancillary data during horizontal blanking
46462306a36Sopenharmony_ci *  Raw VBI is sent as active video samples during vertcal blanking
46562306a36Sopenharmony_ci *
46662306a36Sopenharmony_ci *  We use a  BT.656 pxiel clock of 13.5 MHz and a BT.656 active line
46762306a36Sopenharmony_ci *  length of 720 pixels @ 4:2:2 sampling.  Thus...
46862306a36Sopenharmony_ci *
46962306a36Sopenharmony_ci *  For systems that use a 15.734 kHz horizontal rate, such as
47062306a36Sopenharmony_ci *  NTSC-M, PAL-M, PAL-60, and other 60 Hz/525 line systems, we have:
47162306a36Sopenharmony_ci *
47262306a36Sopenharmony_ci *  (1/15.734 kHz) * 2 * 13.5 MHz = 1716 samples/line =
47362306a36Sopenharmony_ci *  4 bytes SAV + 268 bytes anc data + 4 bytes SAV + 1440 active samples
47462306a36Sopenharmony_ci *
47562306a36Sopenharmony_ci *  For systems that use a 15.625 kHz horizontal rate, such as
47662306a36Sopenharmony_ci *  PAL-B/G/H, PAL-I, SECAM-L and other 50 Hz/625 line systems, we have:
47762306a36Sopenharmony_ci *
47862306a36Sopenharmony_ci *  (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line =
47962306a36Sopenharmony_ci *  4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples
48062306a36Sopenharmony_ci */
48162306a36Sopenharmony_ci#define VBI_ACTIVE_SAMPLES	1444 /* 4 byte SAV + 720 Y + 720 U/V */
48262306a36Sopenharmony_ci#define VBI_HBLANK_SAMPLES_60HZ	272 /* 4 byte EAV + 268 anc/fill */
48362306a36Sopenharmony_ci#define VBI_HBLANK_SAMPLES_50HZ	284 /* 4 byte EAV + 280 anc/fill */
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci#define CX18_VBI_FRAMES 32
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_cistruct vbi_info {
48862306a36Sopenharmony_ci	/* Current state of v4l2 VBI settings for this device */
48962306a36Sopenharmony_ci	struct v4l2_format in;
49062306a36Sopenharmony_ci	struct v4l2_sliced_vbi_format *sliced_in; /* pointer to in.fmt.sliced */
49162306a36Sopenharmony_ci	u32 count;    /* Count of VBI data lines: 60 Hz: 12 or 50 Hz: 18 */
49262306a36Sopenharmony_ci	u32 start[2]; /* First VBI data line per field: 10 & 273 or 6 & 318 */
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	u32 frame; /* Count of VBI buffers/frames received from Encoder */
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	/*
49762306a36Sopenharmony_ci	 * Vars for creation and insertion of MPEG Private Stream 1 packets
49862306a36Sopenharmony_ci	 * of sliced VBI data into an MPEG PS
49962306a36Sopenharmony_ci	 */
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	/* Boolean: create and insert Private Stream 1 packets into the PS */
50262306a36Sopenharmony_ci	int insert_mpeg;
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	/*
50562306a36Sopenharmony_ci	 * Buffer for the maximum of 2 * 18 * packet_size sliced VBI lines.
50662306a36Sopenharmony_ci	 * Used in cx18-vbi.c only for collecting sliced data, and as a source
50762306a36Sopenharmony_ci	 * during conversion of sliced VBI data into MPEG Priv Stream 1 packets.
50862306a36Sopenharmony_ci	 * We don't need to save state here, but the array may have been a bit
50962306a36Sopenharmony_ci	 * too big (2304 bytes) to alloc from the stack.
51062306a36Sopenharmony_ci	 */
51162306a36Sopenharmony_ci	struct v4l2_sliced_vbi_data sliced_data[36];
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	/*
51462306a36Sopenharmony_ci	 * A ring buffer of driver-generated MPEG-2 PS
51562306a36Sopenharmony_ci	 * Program Pack/Private Stream 1 packets for sliced VBI data insertion
51662306a36Sopenharmony_ci	 * into the MPEG PS stream.
51762306a36Sopenharmony_ci	 *
51862306a36Sopenharmony_ci	 * In each sliced_mpeg_data[] buffer is:
51962306a36Sopenharmony_ci	 *	16 byte MPEG-2 PS Program Pack Header
52062306a36Sopenharmony_ci	 *	16 byte MPEG-2 Private Stream 1 PES Header
52162306a36Sopenharmony_ci	 *	 4 byte magic number: "itv0" or "ITV0"
52262306a36Sopenharmony_ci	 *	 4 byte first  field line mask, if "itv0"
52362306a36Sopenharmony_ci	 *	 4 byte second field line mask, if "itv0"
52462306a36Sopenharmony_ci	 *	36 lines, if "ITV0"; or <36 lines, if "itv0"; of sliced VBI data
52562306a36Sopenharmony_ci	 *
52662306a36Sopenharmony_ci	 *	Each line in the payload is
52762306a36Sopenharmony_ci	 *	 1 byte line header derived from the SDID (WSS, CC, VPS, etc.)
52862306a36Sopenharmony_ci	 *	42 bytes of line data
52962306a36Sopenharmony_ci	 *
53062306a36Sopenharmony_ci	 * That's a maximum 1552 bytes of payload in the Private Stream 1 packet
53162306a36Sopenharmony_ci	 * which is the payload size a PVR-350 (CX23415) MPEG decoder will
53262306a36Sopenharmony_ci	 * accept for VBI data. So, including the headers, it's a maximum 1584
53362306a36Sopenharmony_ci	 * bytes total.
53462306a36Sopenharmony_ci	 */
53562306a36Sopenharmony_ci#define CX18_SLICED_MPEG_DATA_MAXSZ	1584
53662306a36Sopenharmony_ci	/* copy_vbi_buf() needs 8 temp bytes on the end for the worst case */
53762306a36Sopenharmony_ci#define CX18_SLICED_MPEG_DATA_BUFSZ	(CX18_SLICED_MPEG_DATA_MAXSZ+8)
53862306a36Sopenharmony_ci	u8 *sliced_mpeg_data[CX18_VBI_FRAMES];
53962306a36Sopenharmony_ci	u32 sliced_mpeg_size[CX18_VBI_FRAMES];
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci	/* Count of Program Pack/Program Stream 1 packets inserted into PS */
54262306a36Sopenharmony_ci	u32 inserted_frame;
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	/*
54562306a36Sopenharmony_ci	 * A dummy driver stream transfer mdl & buffer with a copy of the next
54662306a36Sopenharmony_ci	 * sliced_mpeg_data[] buffer for output to userland apps.
54762306a36Sopenharmony_ci	 * Only used in cx18-fileops.c, but its state needs to persist at times.
54862306a36Sopenharmony_ci	 */
54962306a36Sopenharmony_ci	struct cx18_mdl sliced_mpeg_mdl;
55062306a36Sopenharmony_ci	struct cx18_buffer sliced_mpeg_buf;
55162306a36Sopenharmony_ci};
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci/* Per cx23418, per I2C bus private algo callback data */
55462306a36Sopenharmony_cistruct cx18_i2c_algo_callback_data {
55562306a36Sopenharmony_ci	struct cx18 *cx;
55662306a36Sopenharmony_ci	int bus_index;   /* 0 or 1 for the cx23418's 1st or 2nd I2C bus */
55762306a36Sopenharmony_ci};
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci#define CX18_MAX_MMIO_WR_RETRIES 10
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci/* Struct to hold info about cx18 cards */
56262306a36Sopenharmony_cistruct cx18 {
56362306a36Sopenharmony_ci	int instance;
56462306a36Sopenharmony_ci	struct pci_dev *pci_dev;
56562306a36Sopenharmony_ci	struct v4l2_device v4l2_dev;
56662306a36Sopenharmony_ci	struct v4l2_subdev *sd_av;     /* A/V decoder/digitizer sub-device */
56762306a36Sopenharmony_ci	struct v4l2_subdev *sd_extmux; /* External multiplexer sub-dev */
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	const struct cx18_card *card;	/* card information */
57062306a36Sopenharmony_ci	const char *card_name;  /* full name of the card */
57162306a36Sopenharmony_ci	const struct cx18_card_tuner_i2c *card_i2c; /* i2c addresses to probe for tuner */
57262306a36Sopenharmony_ci	u8 is_50hz;
57362306a36Sopenharmony_ci	u8 is_60hz;
57462306a36Sopenharmony_ci	u8 nof_inputs;		/* number of video inputs */
57562306a36Sopenharmony_ci	u8 nof_audio_inputs;	/* number of audio inputs */
57662306a36Sopenharmony_ci	u32 v4l2_cap;		/* V4L2 capabilities of card */
57762306a36Sopenharmony_ci	u32 hw_flags;		/* Hardware description of the board */
57862306a36Sopenharmony_ci	unsigned int free_mdl_idx;
57962306a36Sopenharmony_ci	struct cx18_scb __iomem *scb; /* pointer to SCB */
58062306a36Sopenharmony_ci	struct mutex epu2apu_mb_lock; /* protect driver to chip mailbox in SCB*/
58162306a36Sopenharmony_ci	struct mutex epu2cpu_mb_lock; /* protect driver to chip mailbox in SCB*/
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	struct cx18_av_state av_state;
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	/* codec settings */
58662306a36Sopenharmony_ci	struct cx2341x_handler cxhdl;
58762306a36Sopenharmony_ci	u32 filter_mode;
58862306a36Sopenharmony_ci	u32 temporal_strength;
58962306a36Sopenharmony_ci	u32 spatial_strength;
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	/* dualwatch */
59262306a36Sopenharmony_ci	unsigned long dualwatch_jiffies;
59362306a36Sopenharmony_ci	u32 dualwatch_stereo_mode;
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	struct mutex serialize_lock;    /* mutex used to serialize open/close/start/stop/ioctl operations */
59662306a36Sopenharmony_ci	struct cx18_options options;	/* User options */
59762306a36Sopenharmony_ci	int stream_buffers[CX18_MAX_STREAMS]; /* # of buffers for each stream */
59862306a36Sopenharmony_ci	int stream_buf_size[CX18_MAX_STREAMS]; /* Stream buffer size */
59962306a36Sopenharmony_ci	struct cx18_stream streams[CX18_MAX_STREAMS];	/* Stream data */
60062306a36Sopenharmony_ci	struct snd_cx18_card *alsa; /* ALSA interface for PCM capture stream */
60162306a36Sopenharmony_ci	void (*pcm_announce_callback)(struct snd_cx18_card *card, u8 *pcm_data,
60262306a36Sopenharmony_ci				      size_t num_bytes);
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci	unsigned long i_flags;  /* global cx18 flags */
60562306a36Sopenharmony_ci	atomic_t ana_capturing;	/* count number of active analog capture streams */
60662306a36Sopenharmony_ci	atomic_t tot_capturing;	/* total count number of active capture streams */
60762306a36Sopenharmony_ci	int search_pack_header;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	int open_id;		/* incremented each time an open occurs, used as
61062306a36Sopenharmony_ci				   unique ID. Starts at 1, so 0 can be used as
61162306a36Sopenharmony_ci				   uninitialized value in the stream->id. */
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	resource_size_t base_addr;
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci	u8 card_rev;
61662306a36Sopenharmony_ci	void __iomem *enc_mem, *reg_mem;
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	struct vbi_info vbi;
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci	u64 mpg_data_received;
62162306a36Sopenharmony_ci	u64 vbi_data_inserted;
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	wait_queue_head_t mb_apu_waitq;
62462306a36Sopenharmony_ci	wait_queue_head_t mb_cpu_waitq;
62562306a36Sopenharmony_ci	wait_queue_head_t cap_w;
62662306a36Sopenharmony_ci	/* when the current DMA is finished this queue is woken up */
62762306a36Sopenharmony_ci	wait_queue_head_t dma_waitq;
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci	u32 sw1_irq_mask;
63062306a36Sopenharmony_ci	u32 sw2_irq_mask;
63162306a36Sopenharmony_ci	u32 hw2_irq_mask;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	struct workqueue_struct *in_work_queue;
63462306a36Sopenharmony_ci	char in_workq_name[11]; /* "cx18-NN-in" */
63562306a36Sopenharmony_ci	struct cx18_in_work_order in_work_order[CX18_MAX_IN_WORK_ORDERS];
63662306a36Sopenharmony_ci	char epu_debug_str[256]; /* CX18_EPU_DEBUG is rare: use shared space */
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	/* i2c */
63962306a36Sopenharmony_ci	struct i2c_adapter i2c_adap[2];
64062306a36Sopenharmony_ci	struct i2c_algo_bit_data i2c_algo[2];
64162306a36Sopenharmony_ci	struct cx18_i2c_algo_callback_data i2c_algo_cb_data[2];
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	struct IR_i2c_init_data ir_i2c_init_data;
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	/* gpio */
64662306a36Sopenharmony_ci	u32 gpio_dir;
64762306a36Sopenharmony_ci	u32 gpio_val;
64862306a36Sopenharmony_ci	struct mutex gpio_lock;
64962306a36Sopenharmony_ci	struct v4l2_subdev sd_gpiomux;
65062306a36Sopenharmony_ci	struct v4l2_subdev sd_resetctrl;
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci	/* v4l2 and User settings */
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	/* codec settings */
65562306a36Sopenharmony_ci	u32 audio_input;
65662306a36Sopenharmony_ci	u32 active_input;
65762306a36Sopenharmony_ci	v4l2_std_id std;
65862306a36Sopenharmony_ci	v4l2_std_id tuner_std;	/* The norm of the tuner (fixed) */
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	/* Used for cx18-alsa module loading */
66162306a36Sopenharmony_ci	struct work_struct request_module_wk;
66262306a36Sopenharmony_ci};
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_cistatic inline struct cx18 *to_cx18(struct v4l2_device *v4l2_dev)
66562306a36Sopenharmony_ci{
66662306a36Sopenharmony_ci	return container_of(v4l2_dev, struct cx18, v4l2_dev);
66762306a36Sopenharmony_ci}
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci/* cx18 extensions to be loaded */
67062306a36Sopenharmony_ciextern int (*cx18_ext_init)(struct cx18 *);
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci/* Globals */
67362306a36Sopenharmony_ciextern int cx18_first_minor;
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_ci/*==============Prototypes==================*/
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci/* Return non-zero if a signal is pending */
67862306a36Sopenharmony_ciint cx18_msleep_timeout(unsigned int msecs, int intr);
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci/* Read Hauppauge eeprom */
68162306a36Sopenharmony_cistruct tveeprom; /* forward reference */
68262306a36Sopenharmony_civoid cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv);
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci/* First-open initialization: load firmware, etc. */
68562306a36Sopenharmony_ciint cx18_init_on_first_open(struct cx18 *cx);
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci/* Test if the current VBI mode is raw (1) or sliced (0) */
68862306a36Sopenharmony_cistatic inline int cx18_raw_vbi(const struct cx18 *cx)
68962306a36Sopenharmony_ci{
69062306a36Sopenharmony_ci	return cx->vbi.in.type == V4L2_BUF_TYPE_VBI_CAPTURE;
69162306a36Sopenharmony_ci}
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci/* Call the specified callback for all subdevs with a grp_id bit matching the
69462306a36Sopenharmony_ci * mask in hw (if 0, then match them all). Ignore any errors. */
69562306a36Sopenharmony_ci#define cx18_call_hw(cx, hw, o, f, args...)				\
69662306a36Sopenharmony_ci	v4l2_device_mask_call_all(&(cx)->v4l2_dev, hw, o, f, ##args)
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci#define cx18_call_all(cx, o, f, args...) cx18_call_hw(cx, 0, o, f , ##args)
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci/* Call the specified callback for all subdevs with a grp_id bit matching the
70162306a36Sopenharmony_ci * mask in hw (if 0, then match them all). If the callback returns an error
70262306a36Sopenharmony_ci * other than 0 or -ENOIOCTLCMD, then return with that error code. */
70362306a36Sopenharmony_ci#define cx18_call_hw_err(cx, hw, o, f, args...)				\
70462306a36Sopenharmony_ci	v4l2_device_mask_call_until_err(&(cx)->v4l2_dev, hw, o, f, ##args)
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_ci#define cx18_call_all_err(cx, o, f, args...) \
70762306a36Sopenharmony_ci	cx18_call_hw_err(cx, 0, o, f , ##args)
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci#endif /* CX18_DRIVER_H */
710