18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _DVB_USB_CXUSB_H_
38c2ecf20Sopenharmony_ci#define _DVB_USB_CXUSB_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/completion.h>
68c2ecf20Sopenharmony_ci#include <linux/i2c.h>
78c2ecf20Sopenharmony_ci#include <linux/list.h>
88c2ecf20Sopenharmony_ci#include <linux/mutex.h>
98c2ecf20Sopenharmony_ci#include <linux/usb.h>
108c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
118c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
128c2ecf20Sopenharmony_ci#include <media/v4l2-dev.h>
138c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
148c2ecf20Sopenharmony_ci#include <media/videobuf2-core.h>
158c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define DVB_USB_LOG_PREFIX "cxusb"
188c2ecf20Sopenharmony_ci#include "dvb-usb.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define CXUSB_VIDEO_URBS (5)
218c2ecf20Sopenharmony_ci#define CXUSB_VIDEO_URB_MAX_SIZE (512 * 1024)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define CXUSB_VIDEO_PKT_SIZE 3030
248c2ecf20Sopenharmony_ci#define CXUSB_VIDEO_MAX_FRAME_PKTS 346
258c2ecf20Sopenharmony_ci#define CXUSB_VIDEO_MAX_FRAME_SIZE (CXUSB_VIDEO_MAX_FRAME_PKTS * \
268c2ecf20Sopenharmony_ci					CXUSB_VIDEO_PKT_SIZE)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* usb commands - some of it are guesses, don't have a reference yet */
298c2ecf20Sopenharmony_ci#define CMD_BLUEBIRD_GPIO_RW 0x05
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define CMD_I2C_WRITE     0x08
328c2ecf20Sopenharmony_ci#define CMD_I2C_READ      0x09
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define CMD_GPIO_READ     0x0d
358c2ecf20Sopenharmony_ci#define CMD_GPIO_WRITE    0x0e
368c2ecf20Sopenharmony_ci#define     GPIO_TUNER         0x02
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define CMD_POWER_OFF     0xdc
398c2ecf20Sopenharmony_ci#define CMD_POWER_ON      0xde
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define CMD_STREAMING_ON  0x36
428c2ecf20Sopenharmony_ci#define CMD_STREAMING_OFF 0x37
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define CMD_AVER_STREAM_ON  0x18
458c2ecf20Sopenharmony_ci#define CMD_AVER_STREAM_OFF 0x19
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define CMD_GET_IR_CODE   0x47
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define CMD_ANALOG        0x50
508c2ecf20Sopenharmony_ci#define CMD_DIGITAL       0x51
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define CXUSB_BT656_PREAMBLE ((const u8 *)"\xff\x00\x00")
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define CXUSB_BT656_FIELD_MASK BIT(6)
558c2ecf20Sopenharmony_ci#define CXUSB_BT656_FIELD_1 0
568c2ecf20Sopenharmony_ci#define CXUSB_BT656_FIELD_2 BIT(6)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define CXUSB_BT656_VBI_MASK BIT(5)
598c2ecf20Sopenharmony_ci#define CXUSB_BT656_VBI_ON BIT(5)
608c2ecf20Sopenharmony_ci#define CXUSB_BT656_VBI_OFF 0
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define CXUSB_BT656_SEAV_MASK BIT(4)
638c2ecf20Sopenharmony_ci#define CXUSB_BT656_SEAV_EAV BIT(4)
648c2ecf20Sopenharmony_ci#define CXUSB_BT656_SEAV_SAV 0
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Max transfer size done by I2C transfer functions */
678c2ecf20Sopenharmony_ci#define MAX_XFER_SIZE  80
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistruct cxusb_state {
708c2ecf20Sopenharmony_ci	u8 gpio_write_state[3];
718c2ecf20Sopenharmony_ci	bool gpio_write_refresh[3];
728c2ecf20Sopenharmony_ci	struct i2c_client *i2c_client_demod;
738c2ecf20Sopenharmony_ci	struct i2c_client *i2c_client_tuner;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	unsigned char data[MAX_XFER_SIZE];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	struct mutex stream_mutex;
788c2ecf20Sopenharmony_ci	u8 last_lock;
798c2ecf20Sopenharmony_ci	int (*fe_read_status)(struct dvb_frontend *fe,
808c2ecf20Sopenharmony_ci			      enum fe_status *status);
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cienum cxusb_open_type {
848c2ecf20Sopenharmony_ci	CXUSB_OPEN_INIT,
858c2ecf20Sopenharmony_ci	CXUSB_OPEN_NONE,
868c2ecf20Sopenharmony_ci	CXUSB_OPEN_ANALOG,
878c2ecf20Sopenharmony_ci	CXUSB_OPEN_DIGITAL
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct cxusb_medion_auxbuf {
918c2ecf20Sopenharmony_ci	u8 *buf;
928c2ecf20Sopenharmony_ci	unsigned int len;
938c2ecf20Sopenharmony_ci	unsigned int paylen;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cienum cxusb_bt656_mode {
978c2ecf20Sopenharmony_ci	NEW_FRAME, FIRST_FIELD, SECOND_FIELD
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cienum cxusb_bt656_fmode {
1018c2ecf20Sopenharmony_ci	START_SEARCH, LINE_SAMPLES, VBI_SAMPLES
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct cxusb_bt656_params {
1058c2ecf20Sopenharmony_ci	enum cxusb_bt656_mode mode;
1068c2ecf20Sopenharmony_ci	enum cxusb_bt656_fmode fmode;
1078c2ecf20Sopenharmony_ci	unsigned int pos;
1088c2ecf20Sopenharmony_ci	unsigned int line;
1098c2ecf20Sopenharmony_ci	unsigned int linesamples;
1108c2ecf20Sopenharmony_ci	u8 *buf;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistruct cxusb_medion_dev {
1148c2ecf20Sopenharmony_ci	/* has to be the first one */
1158c2ecf20Sopenharmony_ci	struct cxusb_state state;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	struct dvb_usb_device *dvbdev;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	enum cxusb_open_type open_type;
1208c2ecf20Sopenharmony_ci	unsigned int open_ctr;
1218c2ecf20Sopenharmony_ci	struct mutex open_lock;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#ifdef CONFIG_DVB_USB_CXUSB_ANALOG
1248c2ecf20Sopenharmony_ci	struct v4l2_device v4l2dev;
1258c2ecf20Sopenharmony_ci	struct v4l2_subdev *cx25840;
1268c2ecf20Sopenharmony_ci	struct v4l2_subdev *tuner;
1278c2ecf20Sopenharmony_ci	struct v4l2_subdev *tda9887;
1288c2ecf20Sopenharmony_ci	struct video_device *videodev, *radiodev;
1298c2ecf20Sopenharmony_ci	struct mutex dev_lock;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	struct vb2_queue videoqueue;
1328c2ecf20Sopenharmony_ci	u32 input;
1338c2ecf20Sopenharmony_ci	bool stop_streaming;
1348c2ecf20Sopenharmony_ci	u32 width, height;
1358c2ecf20Sopenharmony_ci	u32 field_order;
1368c2ecf20Sopenharmony_ci	struct cxusb_medion_auxbuf auxbuf;
1378c2ecf20Sopenharmony_ci	v4l2_std_id norm;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	struct urb *streamurbs[CXUSB_VIDEO_URBS];
1408c2ecf20Sopenharmony_ci	unsigned long urbcomplete;
1418c2ecf20Sopenharmony_ci	struct work_struct urbwork;
1428c2ecf20Sopenharmony_ci	unsigned int nexturb;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	struct cxusb_bt656_params bt656;
1458c2ecf20Sopenharmony_ci	struct cxusb_medion_vbuffer *vbuf;
1468c2ecf20Sopenharmony_ci	__u32 vbuf_sequence;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	struct list_head buflist;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	struct completion v4l2_release;
1518c2ecf20Sopenharmony_ci#endif
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistruct cxusb_medion_vbuffer {
1558c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer vb2;
1568c2ecf20Sopenharmony_ci	struct list_head list;
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* defines for "debug" module parameter */
1608c2ecf20Sopenharmony_ci#define CXUSB_DBG_RC BIT(0)
1618c2ecf20Sopenharmony_ci#define CXUSB_DBG_I2C BIT(1)
1628c2ecf20Sopenharmony_ci#define CXUSB_DBG_MISC BIT(2)
1638c2ecf20Sopenharmony_ci#define CXUSB_DBG_BT656 BIT(3)
1648c2ecf20Sopenharmony_ci#define CXUSB_DBG_URB BIT(4)
1658c2ecf20Sopenharmony_ci#define CXUSB_DBG_OPS BIT(5)
1668c2ecf20Sopenharmony_ci#define CXUSB_DBG_AUXB BIT(6)
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciextern int dvb_usb_cxusb_debug;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define cxusb_vprintk(dvbdev, lvl, ...) do {				\
1718c2ecf20Sopenharmony_ci		struct cxusb_medion_dev *_cxdev = (dvbdev)->priv;	\
1728c2ecf20Sopenharmony_ci		if (dvb_usb_cxusb_debug & CXUSB_DBG_##lvl)		\
1738c2ecf20Sopenharmony_ci			v4l2_printk(KERN_DEBUG,			\
1748c2ecf20Sopenharmony_ci				    &_cxdev->v4l2dev, __VA_ARGS__);	\
1758c2ecf20Sopenharmony_ci	} while (0)
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint cxusb_ctrl_msg(struct dvb_usb_device *d,
1788c2ecf20Sopenharmony_ci		   u8 cmd, const u8 *wbuf, int wlen, u8 *rbuf, int rlen);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#ifdef CONFIG_DVB_USB_CXUSB_ANALOG
1818c2ecf20Sopenharmony_ciint cxusb_medion_analog_init(struct dvb_usb_device *dvbdev);
1828c2ecf20Sopenharmony_ciint cxusb_medion_register_analog(struct dvb_usb_device *dvbdev);
1838c2ecf20Sopenharmony_civoid cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev);
1848c2ecf20Sopenharmony_ci#else
1858c2ecf20Sopenharmony_cistatic inline int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return -EINVAL;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic inline int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	return 0;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic inline void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci#endif
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciint cxusb_medion_get(struct dvb_usb_device *dvbdev,
2018c2ecf20Sopenharmony_ci		     enum cxusb_open_type open_type);
2028c2ecf20Sopenharmony_civoid cxusb_medion_put(struct dvb_usb_device *dvbdev);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#endif
205