xref: /openbmc/linux/drivers/media/usb/dvb-usb/cxusb.h (revision e478d40540544e229c843fe0c698ebc7d0ca07e6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _DVB_USB_CXUSB_H_
3 #define _DVB_USB_CXUSB_H_
4 
5 #include <linux/completion.h>
6 #include <linux/i2c.h>
7 #include <linux/list.h>
8 #include <linux/mutex.h>
9 #include <linux/usb.h>
10 #include <linux/workqueue.h>
11 #include <media/v4l2-common.h>
12 #include <media/v4l2-dev.h>
13 #include <media/v4l2-device.h>
14 #include <media/videobuf2-core.h>
15 #include <media/videobuf2-v4l2.h>
16 
17 #define DVB_USB_LOG_PREFIX "cxusb"
18 #include "dvb-usb.h"
19 
20 #define CXUSB_VIDEO_URBS (5)
21 #define CXUSB_VIDEO_URB_MAX_SIZE (512 * 1024)
22 
23 #define CXUSB_VIDEO_PKT_SIZE 3030
24 #define CXUSB_VIDEO_MAX_FRAME_PKTS 346
25 #define CXUSB_VIDEO_MAX_FRAME_SIZE (CXUSB_VIDEO_MAX_FRAME_PKTS * \
26 					CXUSB_VIDEO_PKT_SIZE)
27 
28 /* usb commands - some of it are guesses, don't have a reference yet */
29 #define CMD_BLUEBIRD_GPIO_RW 0x05
30 
31 #define CMD_I2C_WRITE     0x08
32 #define CMD_I2C_READ      0x09
33 
34 #define CMD_GPIO_READ     0x0d
35 #define CMD_GPIO_WRITE    0x0e
36 #define     GPIO_TUNER         0x02
37 
38 #define CMD_POWER_OFF     0xdc
39 #define CMD_POWER_ON      0xde
40 
41 #define CMD_STREAMING_ON  0x36
42 #define CMD_STREAMING_OFF 0x37
43 
44 #define CMD_AVER_STREAM_ON  0x18
45 #define CMD_AVER_STREAM_OFF 0x19
46 
47 #define CMD_GET_IR_CODE   0x47
48 
49 #define CMD_ANALOG        0x50
50 #define CMD_DIGITAL       0x51
51 
52 #define CXUSB_BT656_PREAMBLE ((const u8 *)"\xff\x00\x00")
53 
54 #define CXUSB_BT656_FIELD_MASK BIT(6)
55 #define CXUSB_BT656_FIELD_1 0
56 #define CXUSB_BT656_FIELD_2 BIT(6)
57 
58 #define CXUSB_BT656_VBI_MASK BIT(5)
59 #define CXUSB_BT656_VBI_ON BIT(5)
60 #define CXUSB_BT656_VBI_OFF 0
61 
62 #define CXUSB_BT656_SEAV_MASK BIT(4)
63 #define CXUSB_BT656_SEAV_EAV BIT(4)
64 #define CXUSB_BT656_SEAV_SAV 0
65 
66 /* Max transfer size done by I2C transfer functions */
67 #define MAX_XFER_SIZE  80
68 
69 struct cxusb_state {
70 	u8 gpio_write_state[3];
71 	bool gpio_write_refresh[3];
72 	struct i2c_client *i2c_client_demod;
73 	struct i2c_client *i2c_client_tuner;
74 
75 	unsigned char data[MAX_XFER_SIZE];
76 
77 	struct mutex stream_mutex;
78 	u8 last_lock;
79 	int (*fe_read_status)(struct dvb_frontend *fe,
80 		enum fe_status *status);
81 };
82 
83 enum cxusb_open_type {
84 	CXUSB_OPEN_INIT, CXUSB_OPEN_NONE,
85 	CXUSB_OPEN_ANALOG, CXUSB_OPEN_DIGITAL
86 };
87 
88 struct cxusb_medion_auxbuf {
89 	u8 *buf;
90 	unsigned int len;
91 	unsigned int paylen;
92 };
93 
94 enum cxusb_bt656_mode {
95 	NEW_FRAME, FIRST_FIELD, SECOND_FIELD
96 };
97 
98 enum cxusb_bt656_fmode {
99 	START_SEARCH, LINE_SAMPLES, VBI_SAMPLES
100 };
101 
102 struct cxusb_bt656_params {
103 	enum cxusb_bt656_mode mode;
104 	enum cxusb_bt656_fmode fmode;
105 	unsigned int pos;
106 	unsigned int line;
107 	unsigned int linesamples;
108 	u8 *buf;
109 };
110 
111 struct cxusb_medion_dev {
112 	/* has to be the first one */
113 	struct cxusb_state state;
114 
115 	struct dvb_usb_device *dvbdev;
116 
117 	enum cxusb_open_type open_type;
118 	unsigned int open_ctr;
119 	struct mutex open_lock;
120 
121 #ifdef CONFIG_DVB_USB_CXUSB_ANALOG
122 	struct v4l2_device v4l2dev;
123 	struct v4l2_subdev *cx25840;
124 	struct v4l2_subdev *tuner;
125 	struct v4l2_subdev *tda9887;
126 	struct video_device *videodev, *radiodev;
127 	struct mutex dev_lock;
128 
129 	struct vb2_queue videoqueue;
130 	u32 input;
131 	bool stop_streaming;
132 	u32 width, height;
133 	u32 field_order;
134 	struct cxusb_medion_auxbuf auxbuf;
135 	v4l2_std_id norm;
136 
137 	struct urb *streamurbs[CXUSB_VIDEO_URBS];
138 	unsigned long urbcomplete;
139 	struct work_struct urbwork;
140 	unsigned int nexturb;
141 
142 	struct cxusb_bt656_params bt656;
143 	struct cxusb_medion_vbuffer *vbuf;
144 	__u32 vbuf_sequence;
145 
146 	struct list_head buflist;
147 
148 	struct completion v4l2_release;
149 #endif
150 };
151 
152 struct cxusb_medion_vbuffer {
153 	struct vb2_v4l2_buffer vb2;
154 	struct list_head list;
155 };
156 
157 /* defines for "debug" module parameter */
158 #define CXUSB_DBG_RC BIT(0)
159 #define CXUSB_DBG_I2C BIT(1)
160 #define CXUSB_DBG_MISC BIT(2)
161 #define CXUSB_DBG_BT656 BIT(3)
162 #define CXUSB_DBG_URB BIT(4)
163 #define CXUSB_DBG_OPS BIT(5)
164 #define CXUSB_DBG_AUXB BIT(6)
165 
166 extern int dvb_usb_cxusb_debug;
167 
168 #define cxusb_vprintk(dvbdev, lvl, ...) do {				\
169 		struct cxusb_medion_dev *_cxdev = (dvbdev)->priv;	\
170 		if (dvb_usb_cxusb_debug & CXUSB_DBG_##lvl)		\
171 			v4l2_printk(KERN_DEBUG,			\
172 				    &_cxdev->v4l2dev, __VA_ARGS__);	\
173 	} while (0)
174 
175 int cxusb_ctrl_msg(struct dvb_usb_device *d,
176 		   u8 cmd, const u8 *wbuf, int wlen, u8 *rbuf, int rlen);
177 
178 #ifdef CONFIG_DVB_USB_CXUSB_ANALOG
179 int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev);
180 int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev);
181 void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev);
182 #else
183 static inline int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
184 {
185 	return -EINVAL;
186 }
187 
188 static inline int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
189 {
190 	return 0;
191 }
192 
193 static inline void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
194 {
195 }
196 #endif
197 
198 int cxusb_medion_get(struct dvb_usb_device *dvbdev,
199 		     enum cxusb_open_type open_type);
200 void cxusb_medion_put(struct dvb_usb_device *dvbdev);
201 
202 #endif
203