Pull acpi_device_handle_cleanup into release branch
[pandora-kernel.git] / drivers / media / video / pvrusb2 / pvrusb2-hdw-internal.h
1 /*
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
36 #include <linux/config.h>
37 #include <linux/videodev2.h>
38 #include <linux/i2c.h>
39 #include <linux/mutex.h>
40 #include "pvrusb2-hdw.h"
41 #include "pvrusb2-io.h"
42 #include <media/cx2341x.h>
43
44 /* Legal values for the SRATE state variable */
45 #define PVR2_CVAL_SRATE_48 0
46 #define PVR2_CVAL_SRATE_44_1 1
47
48 /* Legal values for the AUDIOBITRATE state variable */
49 #define PVR2_CVAL_AUDIOBITRATE_384 0
50 #define PVR2_CVAL_AUDIOBITRATE_320 1
51 #define PVR2_CVAL_AUDIOBITRATE_256 2
52 #define PVR2_CVAL_AUDIOBITRATE_224 3
53 #define PVR2_CVAL_AUDIOBITRATE_192 4
54 #define PVR2_CVAL_AUDIOBITRATE_160 5
55 #define PVR2_CVAL_AUDIOBITRATE_128 6
56 #define PVR2_CVAL_AUDIOBITRATE_112 7
57 #define PVR2_CVAL_AUDIOBITRATE_96 8
58 #define PVR2_CVAL_AUDIOBITRATE_80 9
59 #define PVR2_CVAL_AUDIOBITRATE_64 10
60 #define PVR2_CVAL_AUDIOBITRATE_56 11
61 #define PVR2_CVAL_AUDIOBITRATE_48 12
62 #define PVR2_CVAL_AUDIOBITRATE_32 13
63 #define PVR2_CVAL_AUDIOBITRATE_VBR 14
64
65 /* Legal values for the AUDIOEMPHASIS state variable */
66 #define PVR2_CVAL_AUDIOEMPHASIS_NONE 0
67 #define PVR2_CVAL_AUDIOEMPHASIS_50_15 1
68 #define PVR2_CVAL_AUDIOEMPHASIS_CCITT 2
69
70 /* Legal values for PVR2_CID_HSM */
71 #define PVR2_CVAL_HSM_FAIL 0
72 #define PVR2_CVAL_HSM_FULL 1
73 #define PVR2_CVAL_HSM_HIGH 2
74
75 #define PVR2_VID_ENDPOINT        0x84
76 #define PVR2_UNK_ENDPOINT        0x86    /* maybe raw yuv ? */
77 #define PVR2_VBI_ENDPOINT        0x88
78
79 #define PVR2_CTL_BUFFSIZE        64
80
81 #define FREQTABLE_SIZE 500
82
83 #define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
84 #define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
85
86 struct pvr2_decoder;
87
88 typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
89 typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
90 typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
91 typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
92 typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
93                                     char *,unsigned int,unsigned int *);
94 typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
95                                     const char *,unsigned int,
96                                     int *mskp,int *valp);
97 typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
98
99 /* This structure describes a specific control.  A table of these is set up
100    in pvrusb2-hdw.c. */
101 struct pvr2_ctl_info {
102         /* Control's name suitable for use as an identifier */
103         const char *name;
104
105         /* Short description of control */
106         const char *desc;
107
108         /* Control's implementation */
109         pvr2_ctlf_get_value get_value;      /* Get its value */
110         pvr2_ctlf_set_value set_value;      /* Set its value */
111         pvr2_ctlf_val_to_sym val_to_sym;    /* Custom convert value->symbol */
112         pvr2_ctlf_sym_to_val sym_to_val;    /* Custom convert symbol->value */
113         pvr2_ctlf_is_dirty is_dirty;        /* Return true if dirty */
114         pvr2_ctlf_clear_dirty clear_dirty;  /* Clear dirty state */
115         pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
116
117         /* Control's type (int, enum, bitmask) */
118         enum pvr2_ctl_type type;
119
120         /* Associated V4L control ID, if any */
121         int v4l_id;
122
123         /* Associated driver internal ID, if any */
124         int internal_id;
125
126         /* Don't implicitly initialize this control's value */
127         int skip_init;
128
129         /* Starting value for this control */
130         int default_value;
131
132         /* Type-specific control information */
133         union {
134                 struct { /* Integer control */
135                         long min_value; /* lower limit */
136                         long max_value; /* upper limit */
137                 } type_int;
138                 struct { /* enumerated control */
139                         unsigned int count;       /* enum value count */
140                         const char **value_names; /* symbol names */
141                 } type_enum;
142                 struct { /* bitmask control */
143                         unsigned int valid_bits; /* bits in use */
144                         const char **bit_names;  /* symbol name/bit */
145                 } type_bitmask;
146         } def;
147 };
148
149
150 /* Same as pvr2_ctl_info, but includes storage for the control description */
151 #define PVR2_CTLD_INFO_DESC_SIZE 32
152 struct pvr2_ctld_info {
153         struct pvr2_ctl_info info;
154         char desc[PVR2_CTLD_INFO_DESC_SIZE];
155 };
156
157 struct pvr2_ctrl {
158         const struct pvr2_ctl_info *info;
159         struct pvr2_hdw *hdw;
160 };
161
162
163 struct pvr2_audio_stat {
164         void *ctxt;
165         void (*detach)(void *);
166         int (*status)(void *);
167 };
168
169 struct pvr2_decoder_ctrl {
170         void *ctxt;
171         void (*detach)(void *);
172         void (*enable)(void *,int);
173         int (*tuned)(void *);
174         void (*force_reset)(void *);
175 };
176
177 #define PVR2_I2C_PEND_DETECT  0x01  /* Need to detect a client type */
178 #define PVR2_I2C_PEND_CLIENT  0x02  /* Client needs a specific update */
179 #define PVR2_I2C_PEND_REFRESH 0x04  /* Client has specific pending bits */
180 #define PVR2_I2C_PEND_STALE   0x08  /* Broadcast pending bits */
181
182 #define PVR2_I2C_PEND_ALL (PVR2_I2C_PEND_DETECT |\
183                            PVR2_I2C_PEND_CLIENT |\
184                            PVR2_I2C_PEND_REFRESH |\
185                            PVR2_I2C_PEND_STALE)
186
187 /* Disposition of firmware1 loading situation */
188 #define FW1_STATE_UNKNOWN 0
189 #define FW1_STATE_MISSING 1
190 #define FW1_STATE_FAILED 2
191 #define FW1_STATE_RELOAD 3
192 #define FW1_STATE_OK 4
193
194 /* Known major hardware variants, keyed from device ID */
195 #define PVR2_HDW_TYPE_29XXX 0
196 #ifdef CONFIG_VIDEO_PVRUSB2_24XXX
197 #define PVR2_HDW_TYPE_24XXX 1
198 #endif
199
200 typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
201 #define PVR2_I2C_FUNC_CNT 128
202
203 /* This structure contains all state data directly needed to
204    manipulate the hardware (as opposed to complying with a kernel
205    interface) */
206 struct pvr2_hdw {
207         /* Underlying USB device handle */
208         struct usb_device *usb_dev;
209         struct usb_interface *usb_intf;
210
211         /* Device type, one of PVR2_HDW_TYPE_xxxxx */
212         unsigned int hdw_type;
213
214         /* Video spigot */
215         struct pvr2_stream *vid_stream;
216
217         /* Mutex for all hardware state control */
218         struct mutex big_lock_mutex;
219         int big_lock_held;  /* For debugging */
220
221         void (*poll_trigger_func)(void *);
222         void *poll_trigger_data;
223
224         char name[32];
225
226         /* I2C stuff */
227         struct i2c_adapter i2c_adap;
228         struct i2c_algorithm i2c_algo;
229         pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
230         int i2c_cx25840_hack_state;
231         int i2c_linked;
232         unsigned int i2c_pend_types;    /* Which types of update are needed */
233         unsigned long i2c_pend_mask;    /* Change bits we need to scan */
234         unsigned long i2c_stale_mask;   /* Pending broadcast change bits */
235         unsigned long i2c_active_mask;  /* All change bits currently in use */
236         struct list_head i2c_clients;
237         struct mutex i2c_list_lock;
238
239         /* Frequency table */
240         unsigned int freqTable[FREQTABLE_SIZE];
241         unsigned int freqProgSlot;
242         unsigned int freqSlot;
243
244         /* Stuff for handling low level control interaction with device */
245         struct mutex ctl_lock_mutex;
246         int ctl_lock_held;  /* For debugging */
247         struct urb *ctl_write_urb;
248         struct urb *ctl_read_urb;
249         unsigned char *ctl_write_buffer;
250         unsigned char *ctl_read_buffer;
251         volatile int ctl_write_pend_flag;
252         volatile int ctl_read_pend_flag;
253         volatile int ctl_timeout_flag;
254         struct completion ctl_done;
255         unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
256         int cmd_debug_state;               // Low level command debugging info
257         unsigned char cmd_debug_code;      //
258         unsigned int cmd_debug_write_len;  //
259         unsigned int cmd_debug_read_len;   //
260
261         int flag_ok;            // device in known good state
262         int flag_disconnected;  // flag_ok == 0 due to disconnect
263         int flag_init_ok;       // true if structure is fully initialized
264         int flag_streaming_enabled; // true if streaming should be on
265         int fw1_state;          // current situation with fw1
266
267         int flag_decoder_is_tuned;
268
269         struct pvr2_decoder_ctrl *decoder_ctrl;
270
271         // CPU firmware info (used to help find / save firmware data)
272         char *fw_buffer;
273         unsigned int fw_size;
274
275         // Which subsystem pieces have been enabled / configured
276         unsigned long subsys_enabled_mask;
277
278         // Which subsystems are manipulated to enable streaming
279         unsigned long subsys_stream_mask;
280
281         // True if there is a request to trigger logging of state in each
282         // module.
283         int log_requested;
284
285         /* Tuner / frequency control stuff */
286         unsigned int tuner_type;
287         int tuner_updated;
288         unsigned int freqVal;
289         int freqDirty;
290
291         /* Video standard handling */
292         v4l2_std_id std_mask_eeprom; // Hardware supported selections
293         v4l2_std_id std_mask_avail;  // Which standards we may select from
294         v4l2_std_id std_mask_cur;    // Currently selected standard(s)
295         unsigned int std_enum_cnt;   // # of enumerated standards
296         int std_enum_cur;            // selected standard enumeration value
297         int std_dirty;               // True if std_mask_cur has changed
298         struct pvr2_ctl_info std_info_enum;
299         struct pvr2_ctl_info std_info_avail;
300         struct pvr2_ctl_info std_info_cur;
301         struct v4l2_standard *std_defs;
302         const char **std_enum_names;
303
304         // Generated string names, one per actual V4L2 standard
305         const char *std_mask_ptrs[32];
306         char std_mask_names[32][10];
307
308         int unit_number;             /* ID for driver instance */
309         unsigned long serial_number; /* ID for hardware itself */
310
311         /* Minor number used by v4l logic (yes, this is a hack, as there should
312            be no v4l junk here).  Probably a better way to do this. */
313         int v4l_minor_number;
314
315         /* Location of eeprom or a negative number if none */
316         int eeprom_addr;
317
318         enum pvr2_config config;
319
320         /* Information about what audio signal we're hearing */
321         int flag_stereo;
322         int flag_bilingual;
323         struct pvr2_audio_stat *audio_stat;
324
325         /* Control state needed for cx2341x module */
326         struct cx2341x_mpeg_params enc_cur_state;
327         struct cx2341x_mpeg_params enc_ctl_state;
328         /* True if an encoder attribute has changed */
329         int enc_stale;
330         /* True if enc_cur_state is valid */
331         int enc_cur_valid;
332
333         /* Control state */
334 #define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
335         VCREATE_DATA(brightness);
336         VCREATE_DATA(contrast);
337         VCREATE_DATA(saturation);
338         VCREATE_DATA(hue);
339         VCREATE_DATA(volume);
340         VCREATE_DATA(balance);
341         VCREATE_DATA(bass);
342         VCREATE_DATA(treble);
343         VCREATE_DATA(mute);
344         VCREATE_DATA(input);
345         VCREATE_DATA(audiomode);
346         VCREATE_DATA(res_hor);
347         VCREATE_DATA(res_ver);
348         VCREATE_DATA(srate);
349 #undef VCREATE_DATA
350
351         struct pvr2_ctld_info *mpeg_ctrl_info;
352
353         struct pvr2_ctrl *controls;
354         unsigned int control_cnt;
355 };
356
357 #endif /* __PVRUSB2_HDW_INTERNAL_H */
358
359 /*
360   Stuff for Emacs to see, in order to encourage consistent editing style:
361   *** Local Variables: ***
362   *** mode: c ***
363   *** fill-column: 75 ***
364   *** tab-width: 8 ***
365   *** c-basic-offset: 8 ***
366   *** End: ***
367   */