18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hauppauge HD PVR USB driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008      Janne Grunau (j@jannau.net)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/usb.h>
98c2ecf20Sopenharmony_ci#include <linux/i2c.h>
108c2ecf20Sopenharmony_ci#include <linux/mutex.h>
118c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
128c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
158c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
168c2ecf20Sopenharmony_ci#include <media/i2c/ir-kbd-i2c.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define HDPVR_MAX 8
198c2ecf20Sopenharmony_ci#define HDPVR_I2C_MAX_SIZE 128
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Define these values to match your devices */
228c2ecf20Sopenharmony_ci#define HD_PVR_VENDOR_ID	0x2040
238c2ecf20Sopenharmony_ci#define HD_PVR_PRODUCT_ID	0x4900
248c2ecf20Sopenharmony_ci#define HD_PVR_PRODUCT_ID1	0x4901
258c2ecf20Sopenharmony_ci#define HD_PVR_PRODUCT_ID2	0x4902
268c2ecf20Sopenharmony_ci#define HD_PVR_PRODUCT_ID4	0x4903
278c2ecf20Sopenharmony_ci#define HD_PVR_PRODUCT_ID3	0x4982
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define UNSET    (-1U)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define NUM_BUFFERS 64
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define HDPVR_FIRMWARE_VERSION		0x08
348c2ecf20Sopenharmony_ci#define HDPVR_FIRMWARE_VERSION_AC3	0x0d
358c2ecf20Sopenharmony_ci#define HDPVR_FIRMWARE_VERSION_0X12	0x12
368c2ecf20Sopenharmony_ci#define HDPVR_FIRMWARE_VERSION_0X15	0x15
378c2ecf20Sopenharmony_ci#define HDPVR_FIRMWARE_VERSION_0X1E	0x1e
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* #define HDPVR_DEBUG */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciextern int hdpvr_debug;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define MSG_INFO	1
448c2ecf20Sopenharmony_ci#define MSG_BUFFER	2
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct hdpvr_options {
478c2ecf20Sopenharmony_ci	u8	video_std;
488c2ecf20Sopenharmony_ci	u8	video_input;
498c2ecf20Sopenharmony_ci	u8	audio_input;
508c2ecf20Sopenharmony_ci	u8	bitrate;	/* in 100kbps */
518c2ecf20Sopenharmony_ci	u8	peak_bitrate;	/* in 100kbps */
528c2ecf20Sopenharmony_ci	u8	bitrate_mode;
538c2ecf20Sopenharmony_ci	u8	gop_mode;
548c2ecf20Sopenharmony_ci	enum v4l2_mpeg_audio_encoding	audio_codec;
558c2ecf20Sopenharmony_ci	u8	brightness;
568c2ecf20Sopenharmony_ci	u8	contrast;
578c2ecf20Sopenharmony_ci	u8	hue;
588c2ecf20Sopenharmony_ci	u8	saturation;
598c2ecf20Sopenharmony_ci	u8	sharpness;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Structure to hold all of our device specific stuff */
638c2ecf20Sopenharmony_cistruct hdpvr_device {
648c2ecf20Sopenharmony_ci	/* the v4l device for this device */
658c2ecf20Sopenharmony_ci	struct video_device	video_dev;
668c2ecf20Sopenharmony_ci	/* the control handler for this device */
678c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
688c2ecf20Sopenharmony_ci	/* the usb device for this device */
698c2ecf20Sopenharmony_ci	struct usb_device	*udev;
708c2ecf20Sopenharmony_ci	/* v4l2-device unused */
718c2ecf20Sopenharmony_ci	struct v4l2_device	v4l2_dev;
728c2ecf20Sopenharmony_ci	struct { /* video mode/bitrate control cluster */
738c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_mode;
748c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_bitrate;
758c2ecf20Sopenharmony_ci		struct v4l2_ctrl *video_bitrate_peak;
768c2ecf20Sopenharmony_ci	};
778c2ecf20Sopenharmony_ci	/* v4l2 format */
788c2ecf20Sopenharmony_ci	uint width, height;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* the max packet size of the bulk endpoint */
818c2ecf20Sopenharmony_ci	size_t			bulk_in_size;
828c2ecf20Sopenharmony_ci	/* the address of the bulk in endpoint */
838c2ecf20Sopenharmony_ci	__u8			bulk_in_endpointAddr;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* holds the current device status */
868c2ecf20Sopenharmony_ci	__u8			status;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* holds the current set options */
898c2ecf20Sopenharmony_ci	struct hdpvr_options	options;
908c2ecf20Sopenharmony_ci	v4l2_std_id		cur_std;
918c2ecf20Sopenharmony_ci	struct v4l2_dv_timings	cur_dv_timings;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	uint			flags;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* synchronize I/O */
968c2ecf20Sopenharmony_ci	struct mutex		io_mutex;
978c2ecf20Sopenharmony_ci	/* available buffers */
988c2ecf20Sopenharmony_ci	struct list_head	free_buff_list;
998c2ecf20Sopenharmony_ci	/* in progress buffers */
1008c2ecf20Sopenharmony_ci	struct list_head	rec_buff_list;
1018c2ecf20Sopenharmony_ci	/* waitqueue for buffers */
1028c2ecf20Sopenharmony_ci	wait_queue_head_t	wait_buffer;
1038c2ecf20Sopenharmony_ci	/* waitqueue for data */
1048c2ecf20Sopenharmony_ci	wait_queue_head_t	wait_data;
1058c2ecf20Sopenharmony_ci	/**/
1068c2ecf20Sopenharmony_ci	struct work_struct	worker;
1078c2ecf20Sopenharmony_ci	/* current stream owner */
1088c2ecf20Sopenharmony_ci	struct v4l2_fh		*owner;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* I2C adapter */
1118c2ecf20Sopenharmony_ci	struct i2c_adapter	i2c_adapter;
1128c2ecf20Sopenharmony_ci	/* I2C lock */
1138c2ecf20Sopenharmony_ci	struct mutex		i2c_mutex;
1148c2ecf20Sopenharmony_ci	/* I2C message buffer space */
1158c2ecf20Sopenharmony_ci	char			i2c_buf[HDPVR_I2C_MAX_SIZE];
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* For passing data to ir-kbd-i2c */
1188c2ecf20Sopenharmony_ci	struct IR_i2c_init_data	ir_i2c_init_data;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* usb control transfer buffer and lock */
1218c2ecf20Sopenharmony_ci	struct mutex		usbc_mutex;
1228c2ecf20Sopenharmony_ci	u8			*usbc_buf;
1238c2ecf20Sopenharmony_ci	u8			fw_ver;
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic inline struct hdpvr_device *to_hdpvr_dev(struct v4l2_device *v4l2_dev)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return container_of(v4l2_dev, struct hdpvr_device, v4l2_dev);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* buffer one bulk urb of data */
1338c2ecf20Sopenharmony_cistruct hdpvr_buffer {
1348c2ecf20Sopenharmony_ci	struct list_head	buff_list;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	struct urb		*urb;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	struct hdpvr_device	*dev;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	uint			pos;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	__u8			status;
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistruct hdpvr_video_info {
1488c2ecf20Sopenharmony_ci	u16	width;
1498c2ecf20Sopenharmony_ci	u16	height;
1508c2ecf20Sopenharmony_ci	u8	fps;
1518c2ecf20Sopenharmony_ci	bool	valid;
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cienum {
1558c2ecf20Sopenharmony_ci	STATUS_UNINITIALIZED	= 0,
1568c2ecf20Sopenharmony_ci	STATUS_IDLE,
1578c2ecf20Sopenharmony_ci	STATUS_STARTING,
1588c2ecf20Sopenharmony_ci	STATUS_SHUTTING_DOWN,
1598c2ecf20Sopenharmony_ci	STATUS_STREAMING,
1608c2ecf20Sopenharmony_ci	STATUS_ERROR,
1618c2ecf20Sopenharmony_ci	STATUS_DISCONNECTED,
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cienum {
1658c2ecf20Sopenharmony_ci	HDPVR_FLAG_AC3_CAP = 1,
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cienum {
1698c2ecf20Sopenharmony_ci	BUFSTAT_UNINITIALIZED = 0,
1708c2ecf20Sopenharmony_ci	BUFSTAT_AVAILABLE,
1718c2ecf20Sopenharmony_ci	BUFSTAT_INPROGRESS,
1728c2ecf20Sopenharmony_ci	BUFSTAT_READY,
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define CTRL_START_STREAMING_VALUE	0x0700
1768c2ecf20Sopenharmony_ci#define CTRL_STOP_STREAMING_VALUE	0x0800
1778c2ecf20Sopenharmony_ci#define CTRL_BITRATE_VALUE		0x1000
1788c2ecf20Sopenharmony_ci#define CTRL_BITRATE_MODE_VALUE		0x1200
1798c2ecf20Sopenharmony_ci#define CTRL_GOP_MODE_VALUE		0x1300
1808c2ecf20Sopenharmony_ci#define CTRL_VIDEO_INPUT_VALUE		0x1500
1818c2ecf20Sopenharmony_ci#define CTRL_VIDEO_STD_TYPE		0x1700
1828c2ecf20Sopenharmony_ci#define CTRL_AUDIO_INPUT_VALUE		0x2500
1838c2ecf20Sopenharmony_ci#define CTRL_BRIGHTNESS			0x2900
1848c2ecf20Sopenharmony_ci#define CTRL_CONTRAST			0x2a00
1858c2ecf20Sopenharmony_ci#define CTRL_HUE			0x2b00
1868c2ecf20Sopenharmony_ci#define CTRL_SATURATION			0x2c00
1878c2ecf20Sopenharmony_ci#define CTRL_SHARPNESS			0x2d00
1888c2ecf20Sopenharmony_ci#define CTRL_LOW_PASS_FILTER_VALUE	0x3100
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci#define CTRL_DEFAULT_INDEX		0x0003
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* :0 s 38 01 1000 0003 0004 4 = 0a00ca00
1948c2ecf20Sopenharmony_ci	 * BITRATE SETTING
1958c2ecf20Sopenharmony_ci	 *   1st and 2nd byte (little endian): average bitrate in 100 000 bit/s
1968c2ecf20Sopenharmony_ci	 *                                     min: 1 mbit/s, max: 13.5 mbit/s
1978c2ecf20Sopenharmony_ci	 *   3rd and 4th byte (little endian): peak bitrate in 100 000 bit/s
1988c2ecf20Sopenharmony_ci	 *                                     min: average + 100kbit/s,
1998c2ecf20Sopenharmony_ci	 *                                      max: 20.2 mbit/s
2008c2ecf20Sopenharmony_ci	 */
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/* :0 s 38 01 1200 0003 0001 1 = 02
2038c2ecf20Sopenharmony_ci	 * BIT RATE MODE
2048c2ecf20Sopenharmony_ci	 *  constant = 1, variable (peak) = 2, variable (average) = 3
2058c2ecf20Sopenharmony_ci	 */
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* :0 s 38 01 1300 0003 0001 1 = 03
2088c2ecf20Sopenharmony_ci	 * GOP MODE (2 bit)
2098c2ecf20Sopenharmony_ci	 *    low bit 0/1: advanced/simple GOP
2108c2ecf20Sopenharmony_ci	 *   high bit 0/1: IDR(4/32/128) / no IDR (4/32/0)
2118c2ecf20Sopenharmony_ci	 */
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* :0 s 38 01 1700 0003 0001 1 = 00
2148c2ecf20Sopenharmony_ci	 * VIDEO STANDARD or FREQUENCY 0 = 60hz, 1 = 50hz
2158c2ecf20Sopenharmony_ci	 */
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* :0 s 38 01 3100 0003 0004 4 = 03030000
2188c2ecf20Sopenharmony_ci	 * FILTER CONTROL
2198c2ecf20Sopenharmony_ci	 *   1st byte luma low pass filter strength,
2208c2ecf20Sopenharmony_ci	 *   2nd byte chroma low pass filter strength,
2218c2ecf20Sopenharmony_ci	 *   3rd byte MF enable chroma, min=0, max=1
2228c2ecf20Sopenharmony_ci	 *   4th byte n
2238c2ecf20Sopenharmony_ci	 */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* :0 s 38 b9 0001 0000 0000 0 */
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/* :0 s 38 d3 0000 0000 0001 1 = 00 */
2318c2ecf20Sopenharmony_ci/*		ret = usb_control_msg(dev->udev, */
2328c2ecf20Sopenharmony_ci/*				      usb_sndctrlpipe(dev->udev, 0), */
2338c2ecf20Sopenharmony_ci/*				      0xd3, 0x38, */
2348c2ecf20Sopenharmony_ci/*				      0, 0, */
2358c2ecf20Sopenharmony_ci/*				      "\0", 1, */
2368c2ecf20Sopenharmony_ci/*				      1000); */
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/*		info("control request returned %d", ret); */
2398c2ecf20Sopenharmony_ci/*		msleep(5000); */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* :0 s b8 81 1400 0003 0005 5 <
2438c2ecf20Sopenharmony_ci	 * :0 0 5 = d0024002 19
2448c2ecf20Sopenharmony_ci	 * QUERY FRAME SIZE AND RATE
2458c2ecf20Sopenharmony_ci	 *   1st and 2nd byte (little endian): horizontal resolution
2468c2ecf20Sopenharmony_ci	 *   3rd and 4th byte (little endian): vertical resolution
2478c2ecf20Sopenharmony_ci	 *   5th byte: frame rate
2488c2ecf20Sopenharmony_ci	 */
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* :0 s b8 81 1800 0003 0003 3 <
2518c2ecf20Sopenharmony_ci	 * :0 0 3 = 030104
2528c2ecf20Sopenharmony_ci	 * QUERY SIGNAL AND DETECTED LINES, maybe INPUT
2538c2ecf20Sopenharmony_ci	 */
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cienum hdpvr_video_std {
2568c2ecf20Sopenharmony_ci	HDPVR_60HZ = 0,
2578c2ecf20Sopenharmony_ci	HDPVR_50HZ,
2588c2ecf20Sopenharmony_ci};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cienum hdpvr_video_input {
2618c2ecf20Sopenharmony_ci	HDPVR_COMPONENT = 0,
2628c2ecf20Sopenharmony_ci	HDPVR_SVIDEO,
2638c2ecf20Sopenharmony_ci	HDPVR_COMPOSITE,
2648c2ecf20Sopenharmony_ci	HDPVR_VIDEO_INPUTS
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cienum hdpvr_audio_inputs {
2688c2ecf20Sopenharmony_ci	HDPVR_RCA_BACK = 0,
2698c2ecf20Sopenharmony_ci	HDPVR_RCA_FRONT,
2708c2ecf20Sopenharmony_ci	HDPVR_SPDIF,
2718c2ecf20Sopenharmony_ci	HDPVR_AUDIO_INPUTS
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cienum hdpvr_bitrate_mode {
2758c2ecf20Sopenharmony_ci	HDPVR_CONSTANT = 1,
2768c2ecf20Sopenharmony_ci	HDPVR_VARIABLE_PEAK,
2778c2ecf20Sopenharmony_ci	HDPVR_VARIABLE_AVERAGE,
2788c2ecf20Sopenharmony_ci};
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cienum hdpvr_gop_mode {
2818c2ecf20Sopenharmony_ci	HDPVR_ADVANCED_IDR_GOP = 0,
2828c2ecf20Sopenharmony_ci	HDPVR_SIMPLE_IDR_GOP,
2838c2ecf20Sopenharmony_ci	HDPVR_ADVANCED_NOIDR_GOP,
2848c2ecf20Sopenharmony_ci	HDPVR_SIMPLE_NOIDR_GOP,
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_civoid hdpvr_delete(struct hdpvr_device *dev);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci/*========================================================================*/
2908c2ecf20Sopenharmony_ci/* hardware control functions */
2918c2ecf20Sopenharmony_ciint hdpvr_set_options(struct hdpvr_device *dev);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciint hdpvr_set_bitrate(struct hdpvr_device *dev);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ciint hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
2968c2ecf20Sopenharmony_ci		    enum v4l2_mpeg_audio_encoding codec);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciint hdpvr_config_call(struct hdpvr_device *dev, uint value,
2998c2ecf20Sopenharmony_ci		      unsigned char valbuf);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ciint get_video_info(struct hdpvr_device *dev, struct hdpvr_video_info *vid_info);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/* :0 s b8 81 1800 0003 0003 3 < */
3048c2ecf20Sopenharmony_ci/* :0 0 3 = 0301ff */
3058c2ecf20Sopenharmony_ciint get_input_lines_info(struct hdpvr_device *dev);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/*========================================================================*/
3098c2ecf20Sopenharmony_ci/* v4l2 registration */
3108c2ecf20Sopenharmony_ciint hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
3118c2ecf20Sopenharmony_ci			    int devnumber);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ciint hdpvr_cancel_queue(struct hdpvr_device *dev);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/*========================================================================*/
3168c2ecf20Sopenharmony_ci/* i2c adapter registration */
3178c2ecf20Sopenharmony_ciint hdpvr_register_i2c_adapter(struct hdpvr_device *dev);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistruct i2c_client *hdpvr_register_ir_i2c(struct hdpvr_device *dev);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/*========================================================================*/
3228c2ecf20Sopenharmony_ci/* buffer management */
3238c2ecf20Sopenharmony_ciint hdpvr_free_buffers(struct hdpvr_device *dev);
3248c2ecf20Sopenharmony_ciint hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count);
325