xref: /linux/drivers/media/usb/pvrusb2/pvrusb2-hdw-internal.h (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  */
6 #ifndef __PVRUSB2_HDW_INTERNAL_H
7 #define __PVRUSB2_HDW_INTERNAL_H
8 
9 /*
10 
11   This header sets up all the internal structures and definitions needed to
12   track and coordinate the driver's interaction with the hardware.  ONLY
13   source files which actually implement part of that whole circus should be
14   including this header.  Higher levels, like the external layers to the
15   various public APIs (V4L, sysfs, etc) should NOT ever include this
16   private, internal header.  This means that pvrusb2-hdw, pvrusb2-encoder,
17   etc will include this, but pvrusb2-v4l should not.
18 
19 */
20 
21 #include <linux/videodev2.h>
22 #include <linux/i2c.h>
23 #include <linux/workqueue.h>
24 #include <linux/mutex.h>
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2-io.h"
27 #include <media/v4l2-device.h>
28 #include <media/drv-intf/cx2341x.h>
29 #include <media/i2c/ir-kbd-i2c.h>
30 #include "pvrusb2-devattr.h"
31 
32 /* Legal values for PVR2_CID_HSM */
33 #define PVR2_CVAL_HSM_FAIL 0
34 #define PVR2_CVAL_HSM_FULL 1
35 #define PVR2_CVAL_HSM_HIGH 2
36 
37 #define PVR2_VID_ENDPOINT        0x84
38 #define PVR2_UNK_ENDPOINT        0x86    /* maybe raw yuv ? */
39 #define PVR2_VBI_ENDPOINT        0x88
40 
41 #define PVR2_CTL_BUFFSIZE        64
42 
43 #define FREQTABLE_SIZE 500
44 
45 #define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
46 #define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
47 
48 typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
49 typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
50 typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
51 typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
52 typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
53 typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
54 				    char *,unsigned int,unsigned int *);
55 typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
56 				    const char *,unsigned int,
57 				    int *mskp,int *valp);
58 typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
59 
60 /* This structure describes a specific control.  A table of these is set up
61    in pvrusb2-hdw.c. */
62 struct pvr2_ctl_info {
63 	/* Control's name suitable for use as an identifier */
64 	const char *name;
65 
66 	/* Short description of control */
67 	const char *desc;
68 
69 	/* Control's implementation */
70 	pvr2_ctlf_get_value get_value;      /* Get its value */
71 	pvr2_ctlf_get_value get_def_value;  /* Get its default value */
72 	pvr2_ctlf_get_value get_min_value;  /* Get minimum allowed value */
73 	pvr2_ctlf_get_value get_max_value;  /* Get maximum allowed value */
74 	pvr2_ctlf_set_value set_value;      /* Set its value */
75 	pvr2_ctlf_check_value check_value;  /* Check that value is valid */
76 	pvr2_ctlf_val_to_sym val_to_sym;    /* Custom convert value->symbol */
77 	pvr2_ctlf_sym_to_val sym_to_val;    /* Custom convert symbol->value */
78 	pvr2_ctlf_is_dirty is_dirty;        /* Return true if dirty */
79 	pvr2_ctlf_clear_dirty clear_dirty;  /* Clear dirty state */
80 	pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
81 
82 	/* Control's type (int, enum, bitmask) */
83 	enum pvr2_ctl_type type;
84 
85 	/* Associated V4L control ID, if any */
86 	int v4l_id;
87 
88 	/* Associated driver internal ID, if any */
89 	int internal_id;
90 
91 	/* Don't implicitly initialize this control's value */
92 	int skip_init;
93 
94 	/* Starting value for this control */
95 	int default_value;
96 
97 	/* Type-specific control information */
98 	union {
99 		struct { /* Integer control */
100 			long min_value; /* lower limit */
101 			long max_value; /* upper limit */
102 		} type_int;
103 		struct { /* enumerated control */
104 			unsigned int count;       /* enum value count */
105 			const char * const *value_names; /* symbol names */
106 		} type_enum;
107 		struct { /* bitmask control */
108 			unsigned int valid_bits; /* bits in use */
109 			const char **bit_names;  /* symbol name/bit */
110 		} type_bitmask;
111 	} def;
112 };
113 
114 
115 /* Same as pvr2_ctl_info, but includes storage for the control description */
116 #define PVR2_CTLD_INFO_DESC_SIZE 32
117 struct pvr2_ctld_info {
118 	struct pvr2_ctl_info info;
119 	char desc[PVR2_CTLD_INFO_DESC_SIZE];
120 };
121 
122 struct pvr2_ctrl {
123 	const struct pvr2_ctl_info *info;
124 	struct pvr2_hdw *hdw;
125 };
126 
127 
128 
129 /* Disposition of firmware1 loading situation */
130 #define FW1_STATE_UNKNOWN 0
131 #define FW1_STATE_MISSING 1
132 #define FW1_STATE_FAILED 2
133 #define FW1_STATE_RELOAD 3
134 #define FW1_STATE_OK 4
135 
136 /* What state the device is in if it is a hybrid */
137 #define PVR2_PATHWAY_UNKNOWN 0
138 #define PVR2_PATHWAY_ANALOG 1
139 #define PVR2_PATHWAY_DIGITAL 2
140 
141 typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
142 #define PVR2_I2C_FUNC_CNT 128
143 
144 /* This structure contains all state data directly needed to
145    manipulate the hardware (as opposed to complying with a kernel
146    interface) */
147 struct pvr2_hdw {
148 	/* Underlying USB device handle */
149 	struct usb_device *usb_dev;
150 	struct usb_interface *usb_intf;
151 
152 	/* Our handle into the v4l2 sub-device architecture */
153 	struct v4l2_device v4l2_dev;
154 	/* Device description, anything that must adjust behavior based on
155 	   device specific info will use information held here. */
156 	const struct pvr2_device_desc *hdw_desc;
157 
158 	/* Kernel worker thread handling */
159 	struct work_struct workpoll;     /* Update driver state */
160 
161 	/* Video spigot */
162 	struct pvr2_stream *vid_stream;
163 
164 	/* Mutex for all hardware state control */
165 	struct mutex big_lock_mutex;
166 	int big_lock_held;  /* For debugging */
167 
168 	/* This is a simple string which identifies the instance of this
169 	   driver.  It is unique within the set of existing devices, but
170 	   there is no attempt to keep the name consistent with the same
171 	   physical device each time. */
172 	char name[32];
173 
174 	/* This is a simple string which identifies the physical device
175 	   instance itself - if possible.  (If not possible, then it is
176 	   based on the specific driver instance, similar to name above.)
177 	   The idea here is that userspace might hopefully be able to use
178 	   this recognize specific tuners.  It will encode a serial number,
179 	   if available. */
180 	char identifier[32];
181 
182 	/* I2C stuff */
183 	struct i2c_adapter i2c_adap;
184 	struct i2c_algorithm i2c_algo;
185 	pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
186 	int i2c_cx25840_hack_state;
187 	int i2c_linked;
188 
189 	/* IR related */
190 	unsigned int ir_scheme_active; /* IR scheme as seen from the outside */
191 	struct IR_i2c_init_data ir_init_data; /* params passed to IR modules */
192 
193 	/* Frequency table */
194 	unsigned int freqTable[FREQTABLE_SIZE];
195 	unsigned int freqProgSlot;
196 
197 	/* Stuff for handling low level control interaction with device */
198 	struct mutex ctl_lock_mutex;
199 	int ctl_lock_held;  /* For debugging */
200 	struct urb *ctl_write_urb;
201 	struct urb *ctl_read_urb;
202 	unsigned char *ctl_write_buffer;
203 	unsigned char *ctl_read_buffer;
204 	int ctl_write_pend_flag;
205 	int ctl_read_pend_flag;
206 	int ctl_timeout_flag;
207 	struct completion ctl_done;
208 	unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
209 	int cmd_debug_state;               // Low level command debugging info
210 	unsigned char cmd_debug_code;      //
211 	unsigned int cmd_debug_write_len;  //
212 	unsigned int cmd_debug_read_len;   //
213 
214 	/* Bits of state that describe what is going on with various parts
215 	   of the driver. */
216 	int state_pathway_ok;         /* Pathway config is ok */
217 	int state_encoder_ok;         /* Encoder is operational */
218 	int state_encoder_run;        /* Encoder is running */
219 	int state_encoder_config;     /* Encoder is configured */
220 	int state_encoder_waitok;     /* Encoder pre-wait done */
221 	int state_encoder_runok;      /* Encoder has run for >= .25 sec */
222 	int state_decoder_run;        /* Decoder is running */
223 	int state_decoder_ready;      /* Decoder is stabilized & streamable */
224 	int state_usbstream_run;      /* FX2 is streaming */
225 	int state_decoder_quiescent;  /* Decoder idle for minimal interval */
226 	int state_pipeline_config;    /* Pipeline is configured */
227 	int state_pipeline_req;       /* Somebody wants to stream */
228 	int state_pipeline_pause;     /* Pipeline must be paused */
229 	int state_pipeline_idle;      /* Pipeline not running */
230 
231 	/* This is the master state of the driver.  It is the combined
232 	   result of other bits of state.  Examining this will indicate the
233 	   overall state of the driver.  Values here are one of
234 	   PVR2_STATE_xxxx */
235 	unsigned int master_state;
236 
237 	/* True if device led is currently on */
238 	int led_on;
239 
240 	/* True if states must be re-evaluated */
241 	int state_stale;
242 
243 	void (*state_func)(void *);
244 	void *state_data;
245 
246 	/* Timer for measuring required decoder settling time before we're
247 	   allowed to fire it up again. */
248 	struct timer_list quiescent_timer;
249 
250 	/* Timer for measuring decoder stabilization time, which is the
251 	   amount of time we need to let the decoder run before we can
252 	   trust its output (otherwise the encoder might see garbage and
253 	   then fail to start correctly). */
254 	struct timer_list decoder_stabilization_timer;
255 
256 	/* Timer for measuring encoder pre-wait time */
257 	struct timer_list encoder_wait_timer;
258 
259 	/* Timer for measuring encoder minimum run time */
260 	struct timer_list encoder_run_timer;
261 
262 	/* Place to block while waiting for state changes */
263 	wait_queue_head_t state_wait_data;
264 
265 
266 	int force_dirty;        /* consider all controls dirty if true */
267 	int flag_ok;            /* device in known good state */
268 	int flag_modulefail;    /* true if at least one module failed to load */
269 	int flag_disconnected;  /* flag_ok == 0 due to disconnect */
270 	int flag_init_ok;       /* true if structure is fully initialized */
271 	int fw1_state;          /* current situation with fw1 */
272 	int pathway_state;      /* one of PVR2_PATHWAY_xxx */
273 	int flag_decoder_missed;/* We've noticed missing decoder */
274 	int flag_tripped;       /* Indicates overall failure to start */
275 
276 	unsigned int decoder_client_id;
277 
278 	// CPU firmware info (used to help find / save firmware data)
279 	char *fw_buffer;
280 	unsigned int fw_size;
281 	int fw_cpu_flag; /* True if we are dealing with the CPU */
282 
283 	/* Tuner / frequency control stuff */
284 	unsigned int tuner_type;
285 	int tuner_updated;
286 	unsigned int freqValTelevision;  /* Current freq for tv mode */
287 	unsigned int freqValRadio;       /* Current freq for radio mode */
288 	unsigned int freqSlotTelevision; /* Current slot for tv mode */
289 	unsigned int freqSlotRadio;      /* Current slot for radio mode */
290 	unsigned int freqSelector;       /* 0=radio 1=television */
291 	int freqDirty;
292 
293 	/* Current tuner info - this information is polled from the I2C bus */
294 	struct v4l2_tuner tuner_signal_info;
295 	int tuner_signal_stale;
296 
297 	/* Cropping capability info */
298 	struct v4l2_cropcap cropcap_info;
299 	int cropcap_stale;
300 
301 	/* Video standard handling */
302 	v4l2_std_id std_mask_eeprom; // Hardware supported selections
303 	v4l2_std_id std_mask_avail;  // Which standards we may select from
304 	v4l2_std_id std_mask_cur;    // Currently selected standard(s)
305 	int std_enum_cur;            // selected standard enumeration value
306 	int std_dirty;               // True if std_mask_cur has changed
307 	struct pvr2_ctl_info std_info_enum;
308 	struct pvr2_ctl_info std_info_avail;
309 	struct pvr2_ctl_info std_info_cur;
310 	struct pvr2_ctl_info std_info_detect;
311 
312 	// Generated string names, one per actual V4L2 standard
313 	const char *std_mask_ptrs[32];
314 	char std_mask_names[32][16];
315 
316 	int unit_number;             /* ID for driver instance */
317 	unsigned long serial_number; /* ID for hardware itself */
318 
319 	char bus_info[32]; /* Bus location info */
320 
321 	/* Minor numbers used by v4l logic (yes, this is a hack, as there
322 	   should be no v4l junk here).  Probably a better way to do this. */
323 	int v4l_minor_number_video;
324 	int v4l_minor_number_vbi;
325 	int v4l_minor_number_radio;
326 
327 	/* Bit mask of PVR2_CVAL_INPUT choices which are valid for the hardware */
328 	unsigned int input_avail_mask;
329 	/* Bit mask of PVR2_CVAL_INPUT choices which are currently allowed */
330 	unsigned int input_allowed_mask;
331 
332 	/* Location of eeprom or a negative number if none */
333 	int eeprom_addr;
334 
335 	enum pvr2_config active_stream_type;
336 	enum pvr2_config desired_stream_type;
337 
338 	/* Control state needed for cx2341x module */
339 	struct cx2341x_mpeg_params enc_cur_state;
340 	struct cx2341x_mpeg_params enc_ctl_state;
341 	/* True if an encoder attribute has changed */
342 	int enc_stale;
343 	/* True if an unsafe encoder attribute has changed */
344 	int enc_unsafe_stale;
345 	/* True if enc_cur_state is valid */
346 	int enc_cur_valid;
347 
348 	/* Control state */
349 #define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
350 	VCREATE_DATA(brightness);
351 	VCREATE_DATA(contrast);
352 	VCREATE_DATA(saturation);
353 	VCREATE_DATA(hue);
354 	VCREATE_DATA(volume);
355 	VCREATE_DATA(balance);
356 	VCREATE_DATA(bass);
357 	VCREATE_DATA(treble);
358 	VCREATE_DATA(mute);
359 	VCREATE_DATA(cropl);
360 	VCREATE_DATA(cropt);
361 	VCREATE_DATA(cropw);
362 	VCREATE_DATA(croph);
363 	VCREATE_DATA(input);
364 	VCREATE_DATA(audiomode);
365 	VCREATE_DATA(res_hor);
366 	VCREATE_DATA(res_ver);
367 	VCREATE_DATA(srate);
368 #undef VCREATE_DATA
369 
370 	struct pvr2_ctld_info *mpeg_ctrl_info;
371 
372 	struct pvr2_ctrl *controls;
373 	unsigned int control_cnt;
374 };
375 
376 /* This function gets the current frequency */
377 unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
378 
379 void pvr2_hdw_status_poll(struct pvr2_hdw *);
380 
381 #endif /* __PVRUSB2_HDW_INTERNAL_H */
382