blob: 8ee4549b7a9f225ac9ab65202c202753e13d935e [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#ifndef __PVRUSB2_HDW_INTERNAL_H
22#define __PVRUSB2_HDW_INTERNAL_H
23
24/*
25
26 This header sets up all the internal structures and definitions needed to
27 track and coordinate the driver's interaction with the hardware. ONLY
28 source files which actually implement part of that whole circus should be
29 including this header. Higher levels, like the external layers to the
30 various public APIs (V4L, sysfs, etc) should NOT ever include this
31 private, internal header. This means that pvrusb2-hdw, pvrusb2-encoder,
32 etc will include this, but pvrusb2-v4l should not.
33
34*/
35
Mike Iselyd8554972006-06-26 20:58:46 -030036#include <linux/videodev2.h>
37#include <linux/i2c.h>
Mike Isely681c7392007-11-26 01:48:52 -030038#include <linux/workqueue.h>
Mike Iselyd8554972006-06-26 20:58:46 -030039#include <linux/mutex.h>
40#include "pvrusb2-hdw.h"
41#include "pvrusb2-io.h"
Mike Iselyc05c0462006-06-25 20:04:25 -030042#include <media/cx2341x.h>
Mike Iselyd8554972006-06-26 20:58:46 -030043
Mike Iselyd8554972006-06-26 20:58:46 -030044/* Legal values for PVR2_CID_HSM */
45#define PVR2_CVAL_HSM_FAIL 0
46#define PVR2_CVAL_HSM_FULL 1
47#define PVR2_CVAL_HSM_HIGH 2
48
49#define PVR2_VID_ENDPOINT 0x84
50#define PVR2_UNK_ENDPOINT 0x86 /* maybe raw yuv ? */
51#define PVR2_VBI_ENDPOINT 0x88
52
53#define PVR2_CTL_BUFFSIZE 64
54
55#define FREQTABLE_SIZE 500
56
57#define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
58#define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
59
60struct pvr2_decoder;
61
62typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
63typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
Mike Isely5549f542006-12-27 23:28:54 -030064typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
Mike Iselyd8554972006-06-26 20:58:46 -030065typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
66typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
67typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
68 char *,unsigned int,unsigned int *);
69typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
70 const char *,unsigned int,
71 int *mskp,int *valp);
Mike Iselya761f432006-06-25 20:04:44 -030072typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
Mike Iselyd8554972006-06-26 20:58:46 -030073
74/* This structure describes a specific control. A table of these is set up
75 in pvrusb2-hdw.c. */
76struct pvr2_ctl_info {
77 /* Control's name suitable for use as an identifier */
78 const char *name;
79
80 /* Short description of control */
81 const char *desc;
82
83 /* Control's implementation */
84 pvr2_ctlf_get_value get_value; /* Get its 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 -0300141struct pvr2_decoder_ctrl {
142 void *ctxt;
143 void (*detach)(void *);
144 void (*enable)(void *,int);
Mike Iselyd8554972006-06-26 20:58:46 -0300145 void (*force_reset)(void *);
146};
147
148#define PVR2_I2C_PEND_DETECT 0x01 /* Need to detect a client type */
149#define PVR2_I2C_PEND_CLIENT 0x02 /* Client needs a specific update */
150#define PVR2_I2C_PEND_REFRESH 0x04 /* Client has specific pending bits */
151#define PVR2_I2C_PEND_STALE 0x08 /* Broadcast pending bits */
152
153#define PVR2_I2C_PEND_ALL (PVR2_I2C_PEND_DETECT |\
154 PVR2_I2C_PEND_CLIENT |\
155 PVR2_I2C_PEND_REFRESH |\
156 PVR2_I2C_PEND_STALE)
157
158/* Disposition of firmware1 loading situation */
159#define FW1_STATE_UNKNOWN 0
160#define FW1_STATE_MISSING 1
161#define FW1_STATE_FAILED 2
162#define FW1_STATE_RELOAD 3
163#define FW1_STATE_OK 4
164
165/* Known major hardware variants, keyed from device ID */
166#define PVR2_HDW_TYPE_29XXX 0
Mike Iselyd8554972006-06-26 20:58:46 -0300167#define PVR2_HDW_TYPE_24XXX 1
Mike Iselyd8554972006-06-26 20:58:46 -0300168
169typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
170#define PVR2_I2C_FUNC_CNT 128
171
172/* This structure contains all state data directly needed to
173 manipulate the hardware (as opposed to complying with a kernel
174 interface) */
175struct pvr2_hdw {
176 /* Underlying USB device handle */
177 struct usb_device *usb_dev;
178 struct usb_interface *usb_intf;
179
180 /* Device type, one of PVR2_HDW_TYPE_xxxxx */
181 unsigned int hdw_type;
182
Mike Isely681c7392007-11-26 01:48:52 -0300183 /* Kernel worker thread handling */
184 struct workqueue_struct *workqueue;
185 struct work_struct workpoll; /* Update driver state */
186 struct work_struct worki2csync; /* Update i2c clients */
187 struct work_struct workinit; /* Driver initialization sequence */
188
Mike Iselyd8554972006-06-26 20:58:46 -0300189 /* Video spigot */
190 struct pvr2_stream *vid_stream;
191
192 /* Mutex for all hardware state control */
193 struct mutex big_lock_mutex;
194 int big_lock_held; /* For debugging */
195
Mike Iselyd8554972006-06-26 20:58:46 -0300196 char name[32];
197
198 /* I2C stuff */
199 struct i2c_adapter i2c_adap;
200 struct i2c_algorithm i2c_algo;
201 pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
202 int i2c_cx25840_hack_state;
203 int i2c_linked;
204 unsigned int i2c_pend_types; /* Which types of update are needed */
205 unsigned long i2c_pend_mask; /* Change bits we need to scan */
206 unsigned long i2c_stale_mask; /* Pending broadcast change bits */
207 unsigned long i2c_active_mask; /* All change bits currently in use */
208 struct list_head i2c_clients;
209 struct mutex i2c_list_lock;
210
211 /* Frequency table */
212 unsigned int freqTable[FREQTABLE_SIZE];
213 unsigned int freqProgSlot;
Mike Iselyd8554972006-06-26 20:58:46 -0300214
215 /* Stuff for handling low level control interaction with device */
216 struct mutex ctl_lock_mutex;
217 int ctl_lock_held; /* For debugging */
218 struct urb *ctl_write_urb;
219 struct urb *ctl_read_urb;
220 unsigned char *ctl_write_buffer;
221 unsigned char *ctl_read_buffer;
222 volatile int ctl_write_pend_flag;
223 volatile int ctl_read_pend_flag;
224 volatile int ctl_timeout_flag;
225 struct completion ctl_done;
226 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
227 int cmd_debug_state; // Low level command debugging info
228 unsigned char cmd_debug_code; //
229 unsigned int cmd_debug_write_len; //
230 unsigned int cmd_debug_read_len; //
231
Mike Isely681c7392007-11-26 01:48:52 -0300232 /* Bits of state that describe what is going on with various parts
233 of the driver. */
234 volatile int state_encoder_ok; /* Encoder is operational */
235 volatile int state_encoder_run; /* Encoder is running */
236 volatile int state_encoder_config; /* Encoder is configured */
237 volatile int state_encoder_waitok; /* Encoder pre-wait done */
238 volatile int state_decoder_run; /* Decoder is running */
239 volatile int state_usbstream_run; /* FX2 is streaming */
240 volatile int state_decoder_quiescent; /* Decoder idle for > 50msec */
241 volatile int state_pipeline_config; /* Pipeline is configured */
242 int state_pipeline_req; /* Somebody wants to stream */
243 int state_pipeline_pause; /* Pipeline must be paused */
244 int state_pipeline_idle; /* Pipeline not running */
245
246 /* This is the master state of the driver. It is the combined
247 result of other bits of state. Examining this will indicate the
248 overall state of the driver. Values here are one of
249 PVR2_STATE_xxxx */
250 unsigned int master_state;
251
252 /* True if states must be re-evaluated */
253 int state_stale;
254
255 void (*state_func)(void *);
256 void *state_data;
257
258 /* Timer for measuring decoder settling time */
259 struct timer_list quiescent_timer;
260
261 /* Timer for measuring encoder pre-wait time */
262 struct timer_list encoder_wait_timer;
263
264 /* Place to block while waiting for state changes */
265 wait_queue_head_t state_wait_data;
266
267
Mike Isely9a607f02007-10-14 18:18:12 -0300268 int flag_ok; /* device in known good state */
269 int flag_disconnected; /* flag_ok == 0 due to disconnect */
270 int flag_init_ok; /* true if structure is fully initialized */
Mike Isely9a607f02007-10-14 18:18:12 -0300271 int fw1_state; /* current situation with fw1 */
Mike Isely681c7392007-11-26 01:48:52 -0300272 int flag_decoder_missed;/* We've noticed missing decoder */
273 int flag_tripped; /* Indicates overall failure to start */
Mike Iselyd8554972006-06-26 20:58:46 -0300274
275 struct pvr2_decoder_ctrl *decoder_ctrl;
276
277 // CPU firmware info (used to help find / save firmware data)
278 char *fw_buffer;
279 unsigned int fw_size;
Mike Isely4db666c2007-09-08 22:16:27 -0300280 int fw_cpu_flag; /* True if we are dealing with the CPU */
Mike Iselyd8554972006-06-26 20:58:46 -0300281
Mike Iselyd8554972006-06-26 20:58:46 -0300282 // True if there is a request to trigger logging of state in each
283 // module.
284 int log_requested;
285
286 /* Tuner / frequency control stuff */
287 unsigned int tuner_type;
288 int tuner_updated;
Mike Isely1bde0282006-12-27 23:30:13 -0300289 unsigned int freqValTelevision; /* Current freq for tv mode */
290 unsigned int freqValRadio; /* Current freq for radio mode */
291 unsigned int freqSlotTelevision; /* Current slot for tv mode */
292 unsigned int freqSlotRadio; /* Current slot for radio mode */
293 unsigned int freqSelector; /* 0=radio 1=television */
Mike Iselyd8554972006-06-26 20:58:46 -0300294 int freqDirty;
295
Mike Isely18103c572007-01-20 00:09:47 -0300296 /* Current tuner info - this information is polled from the I2C bus */
297 struct v4l2_tuner tuner_signal_info;
298 int tuner_signal_stale;
299
Mike Iselyd8554972006-06-26 20:58:46 -0300300 /* Video standard handling */
301 v4l2_std_id std_mask_eeprom; // Hardware supported selections
302 v4l2_std_id std_mask_avail; // Which standards we may select from
303 v4l2_std_id std_mask_cur; // Currently selected standard(s)
304 unsigned int std_enum_cnt; // # of enumerated standards
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 v4l2_standard *std_defs;
311 const char **std_enum_names;
312
313 // Generated string names, one per actual V4L2 standard
314 const char *std_mask_ptrs[32];
315 char std_mask_names[32][10];
316
317 int unit_number; /* ID for driver instance */
318 unsigned long serial_number; /* ID for hardware itself */
319
Mike Isely31a18542007-04-08 01:11:47 -0300320 char bus_info[32]; /* Bus location info */
321
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300322 /* Minor numbers used by v4l logic (yes, this is a hack, as there
323 should be no v4l junk here). Probably a better way to do this. */
Mike Isely80793842006-12-27 23:12:28 -0300324 int v4l_minor_number_video;
325 int v4l_minor_number_vbi;
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300326 int v4l_minor_number_radio;
Mike Iselyd8554972006-06-26 20:58:46 -0300327
328 /* Location of eeprom or a negative number if none */
329 int eeprom_addr;
330
Mike Isely681c7392007-11-26 01:48:52 -0300331 enum pvr2_config active_stream_type;
332 enum pvr2_config desired_stream_type;
Mike Iselyd8554972006-06-26 20:58:46 -0300333
Mike Iselyb30d2442006-06-25 20:05:01 -0300334 /* Control state needed for cx2341x module */
335 struct cx2341x_mpeg_params enc_cur_state;
336 struct cx2341x_mpeg_params enc_ctl_state;
337 /* True if an encoder attribute has changed */
338 int enc_stale;
Mike Isely681c7392007-11-26 01:48:52 -0300339 /* True if an unsafe encoder attribute has changed */
340 int enc_unsafe_stale;
Mike Iselyb30d2442006-06-25 20:05:01 -0300341 /* True if enc_cur_state is valid */
342 int enc_cur_valid;
Mike Iselyc05c0462006-06-25 20:04:25 -0300343
Mike Iselyd8554972006-06-26 20:58:46 -0300344 /* Control state */
345#define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
346 VCREATE_DATA(brightness);
347 VCREATE_DATA(contrast);
348 VCREATE_DATA(saturation);
349 VCREATE_DATA(hue);
350 VCREATE_DATA(volume);
351 VCREATE_DATA(balance);
352 VCREATE_DATA(bass);
353 VCREATE_DATA(treble);
354 VCREATE_DATA(mute);
Mike Iselyc05c0462006-06-25 20:04:25 -0300355 VCREATE_DATA(input);
356 VCREATE_DATA(audiomode);
357 VCREATE_DATA(res_hor);
358 VCREATE_DATA(res_ver);
Mike Iselyd8554972006-06-26 20:58:46 -0300359 VCREATE_DATA(srate);
Mike Iselyd8554972006-06-26 20:58:46 -0300360#undef VCREATE_DATA
361
Mike Iselyb30d2442006-06-25 20:05:01 -0300362 struct pvr2_ctld_info *mpeg_ctrl_info;
Mike Iselyc05c0462006-06-25 20:04:25 -0300363
Mike Iselyd8554972006-06-26 20:58:46 -0300364 struct pvr2_ctrl *controls;
Mike Iselyc05c0462006-06-25 20:04:25 -0300365 unsigned int control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -0300366};
367
Mike Isely1bde0282006-12-27 23:30:13 -0300368/* This function gets the current frequency */
369unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
Mike Isely681c7392007-11-26 01:48:52 -0300370void pvr2_hdw_set_decoder(struct pvr2_hdw *,struct pvr2_decoder_ctrl *);
Mike Isely1bde0282006-12-27 23:30:13 -0300371
Mike Iselyd8554972006-06-26 20:58:46 -0300372#endif /* __PVRUSB2_HDW_INTERNAL_H */
373
374/*
375 Stuff for Emacs to see, in order to encourage consistent editing style:
376 *** Local Variables: ***
377 *** mode: c ***
378 *** fill-column: 75 ***
379 *** tab-width: 8 ***
380 *** c-basic-offset: 8 ***
381 *** End: ***
382 */