blob: cb4057bb07a0f1d473978bfcffa0a20721a880bf [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
Mike Iselyd8554972006-06-26 20:58:46 -03003 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_HDW_INTERNAL_H
21#define __PVRUSB2_HDW_INTERNAL_H
22
23/*
24
25 This header sets up all the internal structures and definitions needed to
26 track and coordinate the driver's interaction with the hardware. ONLY
27 source files which actually implement part of that whole circus should be
28 including this header. Higher levels, like the external layers to the
29 various public APIs (V4L, sysfs, etc) should NOT ever include this
30 private, internal header. This means that pvrusb2-hdw, pvrusb2-encoder,
31 etc will include this, but pvrusb2-v4l should not.
32
33*/
34
Mike Iselyd8554972006-06-26 20:58:46 -030035#include <linux/videodev2.h>
36#include <linux/i2c.h>
Mike Isely681c7392007-11-26 01:48:52 -030037#include <linux/workqueue.h>
Mike Iselyd8554972006-06-26 20:58:46 -030038#include <linux/mutex.h>
39#include "pvrusb2-hdw.h"
40#include "pvrusb2-io.h"
Mike Iselyb72b7bf2009-03-06 23:20:31 -030041#include <media/v4l2-device.h>
Mike Iselyc05c0462006-06-25 20:04:25 -030042#include <media/cx2341x.h>
Mike Isely989eb152007-11-26 01:53:12 -030043#include "pvrusb2-devattr.h"
Mike Iselyd8554972006-06-26 20:58:46 -030044
Mike Iselyd8554972006-06-26 20:58:46 -030045/* Legal values for PVR2_CID_HSM */
46#define PVR2_CVAL_HSM_FAIL 0
47#define PVR2_CVAL_HSM_FULL 1
48#define PVR2_CVAL_HSM_HIGH 2
49
50#define PVR2_VID_ENDPOINT 0x84
51#define PVR2_UNK_ENDPOINT 0x86 /* maybe raw yuv ? */
52#define PVR2_VBI_ENDPOINT 0x88
53
54#define PVR2_CTL_BUFFSIZE 64
55
56#define FREQTABLE_SIZE 500
57
58#define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
59#define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
60
Mike Iselyd8554972006-06-26 20:58:46 -030061typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
62typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
Mike Isely5549f542006-12-27 23:28:54 -030063typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
Mike Iselyd8554972006-06-26 20:58:46 -030064typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
65typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
66typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
67 char *,unsigned int,unsigned int *);
68typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
69 const char *,unsigned int,
70 int *mskp,int *valp);
Mike Iselya761f432006-06-25 20:04:44 -030071typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
Mike Iselyd8554972006-06-26 20:58:46 -030072
73/* This structure describes a specific control. A table of these is set up
74 in pvrusb2-hdw.c. */
75struct pvr2_ctl_info {
76 /* Control's name suitable for use as an identifier */
77 const char *name;
78
79 /* Short description of control */
80 const char *desc;
81
82 /* Control's implementation */
83 pvr2_ctlf_get_value get_value; /* Get its value */
Mike Isely26dd1c572008-08-31 20:55:03 -030084 pvr2_ctlf_get_value get_def_value; /* Get its default value */
Mike Isely89ebd632006-08-08 09:10:07 -030085 pvr2_ctlf_get_value get_min_value; /* Get minimum allowed value */
86 pvr2_ctlf_get_value get_max_value; /* Get maximum allowed value */
Mike Iselyd8554972006-06-26 20:58:46 -030087 pvr2_ctlf_set_value set_value; /* Set its value */
Mike Isely5549f542006-12-27 23:28:54 -030088 pvr2_ctlf_check_value check_value; /* Check that value is valid */
Mike Iselyd8554972006-06-26 20:58:46 -030089 pvr2_ctlf_val_to_sym val_to_sym; /* Custom convert value->symbol */
90 pvr2_ctlf_sym_to_val sym_to_val; /* Custom convert symbol->value */
91 pvr2_ctlf_is_dirty is_dirty; /* Return true if dirty */
92 pvr2_ctlf_clear_dirty clear_dirty; /* Clear dirty state */
Mike Iselya761f432006-06-25 20:04:44 -030093 pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
Mike Iselyd8554972006-06-26 20:58:46 -030094
95 /* Control's type (int, enum, bitmask) */
96 enum pvr2_ctl_type type;
97
98 /* Associated V4L control ID, if any */
99 int v4l_id;
100
101 /* Associated driver internal ID, if any */
102 int internal_id;
103
104 /* Don't implicitly initialize this control's value */
105 int skip_init;
106
107 /* Starting value for this control */
108 int default_value;
109
110 /* Type-specific control information */
111 union {
112 struct { /* Integer control */
113 long min_value; /* lower limit */
114 long max_value; /* upper limit */
115 } type_int;
116 struct { /* enumerated control */
117 unsigned int count; /* enum value count */
118 const char **value_names; /* symbol names */
119 } type_enum;
120 struct { /* bitmask control */
121 unsigned int valid_bits; /* bits in use */
122 const char **bit_names; /* symbol name/bit */
123 } type_bitmask;
124 } def;
125};
126
127
Mike Iselyc05c0462006-06-25 20:04:25 -0300128/* Same as pvr2_ctl_info, but includes storage for the control description */
129#define PVR2_CTLD_INFO_DESC_SIZE 32
130struct pvr2_ctld_info {
131 struct pvr2_ctl_info info;
132 char desc[PVR2_CTLD_INFO_DESC_SIZE];
133};
134
Mike Iselyd8554972006-06-26 20:58:46 -0300135struct pvr2_ctrl {
136 const struct pvr2_ctl_info *info;
137 struct pvr2_hdw *hdw;
138};
139
140
Mike Iselyd8554972006-06-26 20:58:46 -0300141
142/* Disposition of firmware1 loading situation */
143#define FW1_STATE_UNKNOWN 0
144#define FW1_STATE_MISSING 1
145#define FW1_STATE_FAILED 2
146#define FW1_STATE_RELOAD 3
147#define FW1_STATE_OK 4
148
Mike Isely62433e32008-04-22 14:45:40 -0300149/* What state the device is in if it is a hybrid */
150#define PVR2_PATHWAY_UNKNOWN 0
151#define PVR2_PATHWAY_ANALOG 1
152#define PVR2_PATHWAY_DIGITAL 2
153
Mike Iselyd8554972006-06-26 20:58:46 -0300154typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
155#define PVR2_I2C_FUNC_CNT 128
156
157/* This structure contains all state data directly needed to
158 manipulate the hardware (as opposed to complying with a kernel
159 interface) */
160struct pvr2_hdw {
161 /* Underlying USB device handle */
162 struct usb_device *usb_dev;
163 struct usb_interface *usb_intf;
164
Mike Iselyb72b7bf2009-03-06 23:20:31 -0300165 /* Our handle into the v4l2 sub-device architecture */
166 struct v4l2_device v4l2_dev;
Mike Iselyf66fbd72007-11-26 01:55:07 -0300167 /* Device description, anything that must adjust behavior based on
168 device specific info will use information held here. */
Mike Isely989eb152007-11-26 01:53:12 -0300169 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -0300170
Mike Isely681c7392007-11-26 01:48:52 -0300171 /* Kernel worker thread handling */
172 struct workqueue_struct *workqueue;
173 struct work_struct workpoll; /* Update driver state */
Mike Isely681c7392007-11-26 01:48:52 -0300174
Mike Iselyd8554972006-06-26 20:58:46 -0300175 /* Video spigot */
176 struct pvr2_stream *vid_stream;
177
178 /* Mutex for all hardware state control */
179 struct mutex big_lock_mutex;
180 int big_lock_held; /* For debugging */
181
Mike Isely13a88792009-01-14 04:22:56 -0300182 /* This is a simple string which identifies the instance of this
183 driver. It is unique within the set of existing devices, but
184 there is no attempt to keep the name consistent with the same
185 physical device each time. */
Mike Iselyd8554972006-06-26 20:58:46 -0300186 char name[32];
187
Mike Isely13a88792009-01-14 04:22:56 -0300188 /* This is a simple string which identifies the physical device
189 instance itself - if possible. (If not possible, then it is
190 based on the specific driver instance, similar to name above.)
191 The idea here is that userspace might hopefully be able to use
192 this recognize specific tuners. It will encode a serial number,
193 if available. */
194 char identifier[32];
195
Mike Iselyd8554972006-06-26 20:58:46 -0300196 /* I2C stuff */
197 struct i2c_adapter i2c_adap;
198 struct i2c_algorithm i2c_algo;
199 pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
200 int i2c_cx25840_hack_state;
201 int i2c_linked;
Mike Iselyd8554972006-06-26 20:58:46 -0300202
Mike Isely27eab382009-04-06 01:51:38 -0300203 /* IR related */
204 unsigned int ir_scheme_active; /* IR scheme as seen from the outside */
205
Mike Iselyd8554972006-06-26 20:58:46 -0300206 /* Frequency table */
207 unsigned int freqTable[FREQTABLE_SIZE];
208 unsigned int freqProgSlot;
Mike Iselyd8554972006-06-26 20:58:46 -0300209
210 /* Stuff for handling low level control interaction with device */
211 struct mutex ctl_lock_mutex;
212 int ctl_lock_held; /* For debugging */
213 struct urb *ctl_write_urb;
214 struct urb *ctl_read_urb;
215 unsigned char *ctl_write_buffer;
216 unsigned char *ctl_read_buffer;
Mike Isely26e33042007-12-03 01:43:23 -0300217 int ctl_write_pend_flag;
218 int ctl_read_pend_flag;
219 int ctl_timeout_flag;
Mike Iselyd8554972006-06-26 20:58:46 -0300220 struct completion ctl_done;
221 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
222 int cmd_debug_state; // Low level command debugging info
223 unsigned char cmd_debug_code; //
224 unsigned int cmd_debug_write_len; //
225 unsigned int cmd_debug_read_len; //
226
Mike Isely681c7392007-11-26 01:48:52 -0300227 /* Bits of state that describe what is going on with various parts
228 of the driver. */
Mike Isely62433e32008-04-22 14:45:40 -0300229 int state_pathway_ok; /* Pathway config is ok */
Mike Iselye802c142007-12-03 01:44:43 -0300230 int state_encoder_ok; /* Encoder is operational */
231 int state_encoder_run; /* Encoder is running */
232 int state_encoder_config; /* Encoder is configured */
233 int state_encoder_waitok; /* Encoder pre-wait done */
Mike Iselyd913d632008-04-06 04:04:35 -0300234 int state_encoder_runok; /* Encoder has run for >= .25 sec */
Mike Iselye802c142007-12-03 01:44:43 -0300235 int state_decoder_run; /* Decoder is running */
Mike Isely6e931372010-02-06 02:10:38 -0300236 int state_decoder_ready; /* Decoder is stabilized & streamable */
Mike Iselye802c142007-12-03 01:44:43 -0300237 int state_usbstream_run; /* FX2 is streaming */
Mike Isely6e931372010-02-06 02:10:38 -0300238 int state_decoder_quiescent; /* Decoder idle for minimal interval */
Mike Iselye802c142007-12-03 01:44:43 -0300239 int state_pipeline_config; /* Pipeline is configured */
Mike Isely7f421fe2008-04-22 14:45:39 -0300240 int state_pipeline_req; /* Somebody wants to stream */
241 int state_pipeline_pause; /* Pipeline must be paused */
242 int state_pipeline_idle; /* Pipeline not running */
Mike Isely681c7392007-11-26 01:48:52 -0300243
244 /* This is the master state of the driver. It is the combined
245 result of other bits of state. Examining this will indicate the
246 overall state of the driver. Values here are one of
247 PVR2_STATE_xxxx */
248 unsigned int master_state;
249
Mike Isely40381cb2008-04-22 14:45:42 -0300250 /* True if device led is currently on */
251 int led_on;
252
Mike Isely681c7392007-11-26 01:48:52 -0300253 /* True if states must be re-evaluated */
254 int state_stale;
255
256 void (*state_func)(void *);
257 void *state_data;
258
Mike Isely6e931372010-02-06 02:10:38 -0300259 /* Timer for measuring required decoder settling time before we're
260 allowed to fire it up again. */
Mike Isely681c7392007-11-26 01:48:52 -0300261 struct timer_list quiescent_timer;
262
Mike Isely6e931372010-02-06 02:10:38 -0300263 /* Timer for measuring decoder stabilization time, which is the
264 amount of time we need to let the decoder run before we can
265 trust its output (otherwise the encoder might see garbage and
266 then fail to start correctly). */
267 struct timer_list decoder_stabilization_timer;
268
Mike Isely681c7392007-11-26 01:48:52 -0300269 /* Timer for measuring encoder pre-wait time */
270 struct timer_list encoder_wait_timer;
271
Mike Iselyd913d632008-04-06 04:04:35 -0300272 /* Timer for measuring encoder minimum run time */
273 struct timer_list encoder_run_timer;
274
Mike Isely681c7392007-11-26 01:48:52 -0300275 /* Place to block while waiting for state changes */
276 wait_queue_head_t state_wait_data;
277
278
Mike Isely27764722009-03-07 01:57:25 -0300279 int force_dirty; /* consider all controls dirty if true */
Mike Isely9a607f02007-10-14 18:18:12 -0300280 int flag_ok; /* device in known good state */
Mike Isely27108142009-10-12 00:21:20 -0300281 int flag_modulefail; /* true if at least one module failed to load */
Mike Isely9a607f02007-10-14 18:18:12 -0300282 int flag_disconnected; /* flag_ok == 0 due to disconnect */
283 int flag_init_ok; /* true if structure is fully initialized */
Mike Isely9a607f02007-10-14 18:18:12 -0300284 int fw1_state; /* current situation with fw1 */
Mike Isely62433e32008-04-22 14:45:40 -0300285 int pathway_state; /* one of PVR2_PATHWAY_xxx */
Mike Isely681c7392007-11-26 01:48:52 -0300286 int flag_decoder_missed;/* We've noticed missing decoder */
287 int flag_tripped; /* Indicates overall failure to start */
Mike Iselyd8554972006-06-26 20:58:46 -0300288
Mike Isely00e5f732009-03-07 00:17:11 -0300289 unsigned int decoder_client_id;
Mike Iselyd8554972006-06-26 20:58:46 -0300290
291 // CPU firmware info (used to help find / save firmware data)
292 char *fw_buffer;
293 unsigned int fw_size;
Mike Isely4db666c2007-09-08 22:16:27 -0300294 int fw_cpu_flag; /* True if we are dealing with the CPU */
Mike Iselyd8554972006-06-26 20:58:46 -0300295
Mike Iselyd8554972006-06-26 20:58:46 -0300296 /* Tuner / frequency control stuff */
297 unsigned int tuner_type;
298 int tuner_updated;
Mike Isely1bde0282006-12-27 23:30:13 -0300299 unsigned int freqValTelevision; /* Current freq for tv mode */
300 unsigned int freqValRadio; /* Current freq for radio mode */
301 unsigned int freqSlotTelevision; /* Current slot for tv mode */
302 unsigned int freqSlotRadio; /* Current slot for radio mode */
303 unsigned int freqSelector; /* 0=radio 1=television */
Mike Iselyd8554972006-06-26 20:58:46 -0300304 int freqDirty;
305
Mike Isely18103c572007-01-20 00:09:47 -0300306 /* Current tuner info - this information is polled from the I2C bus */
307 struct v4l2_tuner tuner_signal_info;
308 int tuner_signal_stale;
309
Mike Isely432907f2008-08-31 21:02:20 -0300310 /* Cropping capability info */
311 struct v4l2_cropcap cropcap_info;
312 int cropcap_stale;
313
Mike Iselyd8554972006-06-26 20:58:46 -0300314 /* Video standard handling */
315 v4l2_std_id std_mask_eeprom; // Hardware supported selections
316 v4l2_std_id std_mask_avail; // Which standards we may select from
317 v4l2_std_id std_mask_cur; // Currently selected standard(s)
318 unsigned int std_enum_cnt; // # of enumerated standards
319 int std_enum_cur; // selected standard enumeration value
320 int std_dirty; // True if std_mask_cur has changed
321 struct pvr2_ctl_info std_info_enum;
322 struct pvr2_ctl_info std_info_avail;
323 struct pvr2_ctl_info std_info_cur;
324 struct v4l2_standard *std_defs;
325 const char **std_enum_names;
326
327 // Generated string names, one per actual V4L2 standard
328 const char *std_mask_ptrs[32];
329 char std_mask_names[32][10];
330
331 int unit_number; /* ID for driver instance */
332 unsigned long serial_number; /* ID for hardware itself */
333
Mike Isely31a18542007-04-08 01:11:47 -0300334 char bus_info[32]; /* Bus location info */
335
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300336 /* Minor numbers used by v4l logic (yes, this is a hack, as there
337 should be no v4l junk here). Probably a better way to do this. */
Mike Isely80793842006-12-27 23:12:28 -0300338 int v4l_minor_number_video;
339 int v4l_minor_number_vbi;
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300340 int v4l_minor_number_radio;
Mike Iselyd8554972006-06-26 20:58:46 -0300341
Mike Isely1cb03b72008-04-21 03:47:43 -0300342 /* Bit mask of PVR2_CVAL_INPUT choices which are valid for the hardware */
Mike Isely7fb20fa2008-04-22 14:45:37 -0300343 unsigned int input_avail_mask;
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200344 /* Bit mask of PVR2_CVAL_INPUT choices which are currently allowed */
Mike Isely1cb03b72008-04-21 03:47:43 -0300345 unsigned int input_allowed_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -0300346
Mike Iselyd8554972006-06-26 20:58:46 -0300347 /* Location of eeprom or a negative number if none */
348 int eeprom_addr;
349
Mike Isely681c7392007-11-26 01:48:52 -0300350 enum pvr2_config active_stream_type;
351 enum pvr2_config desired_stream_type;
Mike Iselyd8554972006-06-26 20:58:46 -0300352
Mike Iselyb30d2442006-06-25 20:05:01 -0300353 /* Control state needed for cx2341x module */
354 struct cx2341x_mpeg_params enc_cur_state;
355 struct cx2341x_mpeg_params enc_ctl_state;
356 /* True if an encoder attribute has changed */
357 int enc_stale;
Mike Isely681c7392007-11-26 01:48:52 -0300358 /* True if an unsafe encoder attribute has changed */
359 int enc_unsafe_stale;
Mike Iselyb30d2442006-06-25 20:05:01 -0300360 /* True if enc_cur_state is valid */
361 int enc_cur_valid;
Mike Iselyc05c0462006-06-25 20:04:25 -0300362
Mike Iselyd8554972006-06-26 20:58:46 -0300363 /* Control state */
364#define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
365 VCREATE_DATA(brightness);
366 VCREATE_DATA(contrast);
367 VCREATE_DATA(saturation);
368 VCREATE_DATA(hue);
369 VCREATE_DATA(volume);
370 VCREATE_DATA(balance);
371 VCREATE_DATA(bass);
372 VCREATE_DATA(treble);
373 VCREATE_DATA(mute);
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300374 VCREATE_DATA(cropl);
375 VCREATE_DATA(cropt);
376 VCREATE_DATA(cropw);
377 VCREATE_DATA(croph);
Mike Iselyc05c0462006-06-25 20:04:25 -0300378 VCREATE_DATA(input);
379 VCREATE_DATA(audiomode);
380 VCREATE_DATA(res_hor);
381 VCREATE_DATA(res_ver);
Mike Iselyd8554972006-06-26 20:58:46 -0300382 VCREATE_DATA(srate);
Mike Iselyd8554972006-06-26 20:58:46 -0300383#undef VCREATE_DATA
384
Mike Iselyb30d2442006-06-25 20:05:01 -0300385 struct pvr2_ctld_info *mpeg_ctrl_info;
Mike Iselyc05c0462006-06-25 20:04:25 -0300386
Mike Iselyd8554972006-06-26 20:58:46 -0300387 struct pvr2_ctrl *controls;
Mike Iselyc05c0462006-06-25 20:04:25 -0300388 unsigned int control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -0300389};
390
Mike Isely1bde0282006-12-27 23:30:13 -0300391/* This function gets the current frequency */
392unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
393
Mike Iselya51f5002009-03-06 23:30:37 -0300394void pvr2_hdw_status_poll(struct pvr2_hdw *);
395
Mike Iselyd8554972006-06-26 20:58:46 -0300396#endif /* __PVRUSB2_HDW_INTERNAL_H */
397
398/*
399 Stuff for Emacs to see, in order to encourage consistent editing style:
400 *** Local Variables: ***
401 *** mode: c ***
402 *** fill-column: 75 ***
403 *** tab-width: 8 ***
404 *** c-basic-offset: 8 ***
405 *** End: ***
406 */