V4L/DVB (11303): tda7432: remove legacy code for old-style i2c API
[safe/jmp/linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-hdw-internal.h
1 /*
2  *
3  *
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
35 #include <linux/videodev2.h>
36 #include <linux/i2c.h>
37 #include <linux/workqueue.h>
38 #include <linux/mutex.h>
39 #include "pvrusb2-hdw.h"
40 #include "pvrusb2-io.h"
41 #include <media/v4l2-device.h>
42 #include <media/cx2341x.h>
43 #include "pvrusb2-devattr.h"
44
45 /* 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
61 typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
62 typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
63 typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
64 typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
65 typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
66 typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
67                                     char *,unsigned int,unsigned int *);
68 typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
69                                     const char *,unsigned int,
70                                     int *mskp,int *valp);
71 typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
72
73 /* This structure describes a specific control.  A table of these is set up
74    in pvrusb2-hdw.c. */
75 struct 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 */
84         pvr2_ctlf_get_value get_def_value;  /* Get its default value */
85         pvr2_ctlf_get_value get_min_value;  /* Get minimum allowed value */
86         pvr2_ctlf_get_value get_max_value;  /* Get maximum allowed value */
87         pvr2_ctlf_set_value set_value;      /* Set its value */
88         pvr2_ctlf_check_value check_value;  /* Check that value is valid */
89         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 */
93         pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
94
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
128 /* Same as pvr2_ctl_info, but includes storage for the control description */
129 #define PVR2_CTLD_INFO_DESC_SIZE 32
130 struct pvr2_ctld_info {
131         struct pvr2_ctl_info info;
132         char desc[PVR2_CTLD_INFO_DESC_SIZE];
133 };
134
135 struct pvr2_ctrl {
136         const struct pvr2_ctl_info *info;
137         struct pvr2_hdw *hdw;
138 };
139
140
141
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
149 /* 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
154 typedef 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) */
160 struct pvr2_hdw {
161         /* Underlying USB device handle */
162         struct usb_device *usb_dev;
163         struct usb_interface *usb_intf;
164
165         /* Our handle into the v4l2 sub-device architecture */
166         struct v4l2_device v4l2_dev;
167         /* Device description, anything that must adjust behavior based on
168            device specific info will use information held here. */
169         const struct pvr2_device_desc *hdw_desc;
170
171         /* Kernel worker thread handling */
172         struct workqueue_struct *workqueue;
173         struct work_struct workpoll;     /* Update driver state */
174
175         /* 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
182         /* 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. */
186         char name[32];
187
188         /* 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
196         /* 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;
202
203         /* Frequency table */
204         unsigned int freqTable[FREQTABLE_SIZE];
205         unsigned int freqProgSlot;
206
207         /* Stuff for handling low level control interaction with device */
208         struct mutex ctl_lock_mutex;
209         int ctl_lock_held;  /* For debugging */
210         struct urb *ctl_write_urb;
211         struct urb *ctl_read_urb;
212         unsigned char *ctl_write_buffer;
213         unsigned char *ctl_read_buffer;
214         int ctl_write_pend_flag;
215         int ctl_read_pend_flag;
216         int ctl_timeout_flag;
217         struct completion ctl_done;
218         unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
219         int cmd_debug_state;               // Low level command debugging info
220         unsigned char cmd_debug_code;      //
221         unsigned int cmd_debug_write_len;  //
222         unsigned int cmd_debug_read_len;   //
223
224         /* Bits of state that describe what is going on with various parts
225            of the driver. */
226         int state_pathway_ok;         /* Pathway config is ok */
227         int state_encoder_ok;         /* Encoder is operational */
228         int state_encoder_run;        /* Encoder is running */
229         int state_encoder_config;     /* Encoder is configured */
230         int state_encoder_waitok;     /* Encoder pre-wait done */
231         int state_encoder_runok;      /* Encoder has run for >= .25 sec */
232         int state_decoder_run;        /* Decoder is running */
233         int state_usbstream_run;      /* FX2 is streaming */
234         int state_decoder_quiescent;  /* Decoder idle for > 50msec */
235         int state_pipeline_config;    /* Pipeline is configured */
236         int state_pipeline_req;       /* Somebody wants to stream */
237         int state_pipeline_pause;     /* Pipeline must be paused */
238         int state_pipeline_idle;      /* Pipeline not running */
239
240         /* This is the master state of the driver.  It is the combined
241            result of other bits of state.  Examining this will indicate the
242            overall state of the driver.  Values here are one of
243            PVR2_STATE_xxxx */
244         unsigned int master_state;
245
246         /* True if device led is currently on */
247         int led_on;
248
249         /* True if states must be re-evaluated */
250         int state_stale;
251
252         void (*state_func)(void *);
253         void *state_data;
254
255         /* Timer for measuring decoder settling time */
256         struct timer_list quiescent_timer;
257
258         /* Timer for measuring encoder pre-wait time */
259         struct timer_list encoder_wait_timer;
260
261         /* Timer for measuring encoder minimum run time */
262         struct timer_list encoder_run_timer;
263
264         /* Place to block while waiting for state changes */
265         wait_queue_head_t state_wait_data;
266
267
268         int force_dirty;        /* consider all controls dirty if true */
269         int flag_ok;            /* device in known good state */
270         int flag_disconnected;  /* flag_ok == 0 due to disconnect */
271         int flag_init_ok;       /* true if structure is fully initialized */
272         int fw1_state;          /* current situation with fw1 */
273         int pathway_state;      /* one of PVR2_PATHWAY_xxx */
274         int flag_decoder_missed;/* We've noticed missing decoder */
275         int flag_tripped;       /* Indicates overall failure to start */
276
277         unsigned int decoder_client_id;
278
279         // CPU firmware info (used to help find / save firmware data)
280         char *fw_buffer;
281         unsigned int fw_size;
282         int fw_cpu_flag; /* True if we are dealing with the CPU */
283
284         /* Tuner / frequency control stuff */
285         unsigned int tuner_type;
286         int tuner_updated;
287         unsigned int freqValTelevision;  /* Current freq for tv mode */
288         unsigned int freqValRadio;       /* Current freq for radio mode */
289         unsigned int freqSlotTelevision; /* Current slot for tv mode */
290         unsigned int freqSlotRadio;      /* Current slot for radio mode */
291         unsigned int freqSelector;       /* 0=radio 1=television */
292         int freqDirty;
293
294         /* Current tuner info - this information is polled from the I2C bus */
295         struct v4l2_tuner tuner_signal_info;
296         int tuner_signal_stale;
297
298         /* Cropping capability info */
299         struct v4l2_cropcap cropcap_info;
300         int cropcap_stale;
301
302         /* Video standard handling */
303         v4l2_std_id std_mask_eeprom; // Hardware supported selections
304         v4l2_std_id std_mask_avail;  // Which standards we may select from
305         v4l2_std_id std_mask_cur;    // Currently selected standard(s)
306         unsigned int std_enum_cnt;   // # of enumerated standards
307         int std_enum_cur;            // selected standard enumeration value
308         int std_dirty;               // True if std_mask_cur has changed
309         struct pvr2_ctl_info std_info_enum;
310         struct pvr2_ctl_info std_info_avail;
311         struct pvr2_ctl_info std_info_cur;
312         struct v4l2_standard *std_defs;
313         const char **std_enum_names;
314
315         // Generated string names, one per actual V4L2 standard
316         const char *std_mask_ptrs[32];
317         char std_mask_names[32][10];
318
319         int unit_number;             /* ID for driver instance */
320         unsigned long serial_number; /* ID for hardware itself */
321
322         char bus_info[32]; /* Bus location info */
323
324         /* Minor numbers used by v4l logic (yes, this is a hack, as there
325            should be no v4l junk here).  Probably a better way to do this. */
326         int v4l_minor_number_video;
327         int v4l_minor_number_vbi;
328         int v4l_minor_number_radio;
329
330         /* Bit mask of PVR2_CVAL_INPUT choices which are valid for the hardware */
331         unsigned int input_avail_mask;
332         /* Bit mask of PVR2_CVAL_INPUT choices which are currenly allowed */
333         unsigned int input_allowed_mask;
334
335         /* Location of eeprom or a negative number if none */
336         int eeprom_addr;
337
338         enum pvr2_config active_stream_type;
339         enum pvr2_config desired_stream_type;
340
341         /* Control state needed for cx2341x module */
342         struct cx2341x_mpeg_params enc_cur_state;
343         struct cx2341x_mpeg_params enc_ctl_state;
344         /* True if an encoder attribute has changed */
345         int enc_stale;
346         /* True if an unsafe encoder attribute has changed */
347         int enc_unsafe_stale;
348         /* True if enc_cur_state is valid */
349         int enc_cur_valid;
350
351         /* Control state */
352 #define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
353         VCREATE_DATA(brightness);
354         VCREATE_DATA(contrast);
355         VCREATE_DATA(saturation);
356         VCREATE_DATA(hue);
357         VCREATE_DATA(volume);
358         VCREATE_DATA(balance);
359         VCREATE_DATA(bass);
360         VCREATE_DATA(treble);
361         VCREATE_DATA(mute);
362         VCREATE_DATA(cropl);
363         VCREATE_DATA(cropt);
364         VCREATE_DATA(cropw);
365         VCREATE_DATA(croph);
366         VCREATE_DATA(input);
367         VCREATE_DATA(audiomode);
368         VCREATE_DATA(res_hor);
369         VCREATE_DATA(res_ver);
370         VCREATE_DATA(srate);
371 #undef VCREATE_DATA
372
373         struct pvr2_ctld_info *mpeg_ctrl_info;
374
375         struct pvr2_ctrl *controls;
376         unsigned int control_cnt;
377 };
378
379 /* This function gets the current frequency */
380 unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
381
382 void pvr2_hdw_status_poll(struct pvr2_hdw *);
383
384 #endif /* __PVRUSB2_HDW_INTERNAL_H */
385
386 /*
387   Stuff for Emacs to see, in order to encourage consistent editing style:
388   *** Local Variables: ***
389   *** mode: c ***
390   *** fill-column: 75 ***
391   *** tab-width: 8 ***
392   *** c-basic-offset: 8 ***
393   *** End: ***
394   */