V4L/DVB (7679): pvrusb2: add DVB API framework
[safe/jmp/linux-2.6] / 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/videodev2.h>
37 #include <linux/i2c.h>
38 #include <linux/workqueue.h>
39 #include <linux/mutex.h>
40 #include "pvrusb2-hdw.h"
41 #include "pvrusb2-io.h"
42 #include <media/cx2341x.h>
43 #include "pvrusb2-devattr.h"
44 #include "pvrusb2-dvb.h"
45
46 /* Legal values for PVR2_CID_HSM */
47 #define PVR2_CVAL_HSM_FAIL 0
48 #define PVR2_CVAL_HSM_FULL 1
49 #define PVR2_CVAL_HSM_HIGH 2
50
51 #define PVR2_VID_ENDPOINT        0x84
52 #define PVR2_UNK_ENDPOINT        0x86    /* maybe raw yuv ? */
53 #define PVR2_VBI_ENDPOINT        0x88
54
55 #define PVR2_CTL_BUFFSIZE        64
56
57 #define FREQTABLE_SIZE 500
58
59 #define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
60 #define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
61
62 struct pvr2_decoder;
63
64 typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
65 typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
66 typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
67 typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
68 typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
69 typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
70                                     char *,unsigned int,unsigned int *);
71 typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
72                                     const char *,unsigned int,
73                                     int *mskp,int *valp);
74 typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
75
76 /* This structure describes a specific control.  A table of these is set up
77    in pvrusb2-hdw.c. */
78 struct pvr2_ctl_info {
79         /* Control's name suitable for use as an identifier */
80         const char *name;
81
82         /* Short description of control */
83         const char *desc;
84
85         /* Control's implementation */
86         pvr2_ctlf_get_value get_value;      /* Get its value */
87         pvr2_ctlf_get_value get_min_value;  /* Get minimum allowed value */
88         pvr2_ctlf_get_value get_max_value;  /* Get maximum allowed value */
89         pvr2_ctlf_set_value set_value;      /* Set its value */
90         pvr2_ctlf_check_value check_value;  /* Check that value is valid */
91         pvr2_ctlf_val_to_sym val_to_sym;    /* Custom convert value->symbol */
92         pvr2_ctlf_sym_to_val sym_to_val;    /* Custom convert symbol->value */
93         pvr2_ctlf_is_dirty is_dirty;        /* Return true if dirty */
94         pvr2_ctlf_clear_dirty clear_dirty;  /* Clear dirty state */
95         pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
96
97         /* Control's type (int, enum, bitmask) */
98         enum pvr2_ctl_type type;
99
100         /* Associated V4L control ID, if any */
101         int v4l_id;
102
103         /* Associated driver internal ID, if any */
104         int internal_id;
105
106         /* Don't implicitly initialize this control's value */
107         int skip_init;
108
109         /* Starting value for this control */
110         int default_value;
111
112         /* Type-specific control information */
113         union {
114                 struct { /* Integer control */
115                         long min_value; /* lower limit */
116                         long max_value; /* upper limit */
117                 } type_int;
118                 struct { /* enumerated control */
119                         unsigned int count;       /* enum value count */
120                         const char **value_names; /* symbol names */
121                 } type_enum;
122                 struct { /* bitmask control */
123                         unsigned int valid_bits; /* bits in use */
124                         const char **bit_names;  /* symbol name/bit */
125                 } type_bitmask;
126         } def;
127 };
128
129
130 /* Same as pvr2_ctl_info, but includes storage for the control description */
131 #define PVR2_CTLD_INFO_DESC_SIZE 32
132 struct pvr2_ctld_info {
133         struct pvr2_ctl_info info;
134         char desc[PVR2_CTLD_INFO_DESC_SIZE];
135 };
136
137 struct pvr2_ctrl {
138         const struct pvr2_ctl_info *info;
139         struct pvr2_hdw *hdw;
140 };
141
142
143 struct pvr2_decoder_ctrl {
144         void *ctxt;
145         void (*detach)(void *);
146         void (*enable)(void *,int);
147         void (*force_reset)(void *);
148 };
149
150 #define PVR2_I2C_PEND_DETECT  0x01  /* Need to detect a client type */
151 #define PVR2_I2C_PEND_CLIENT  0x02  /* Client needs a specific update */
152 #define PVR2_I2C_PEND_REFRESH 0x04  /* Client has specific pending bits */
153 #define PVR2_I2C_PEND_STALE   0x08  /* Broadcast pending bits */
154
155 #define PVR2_I2C_PEND_ALL (PVR2_I2C_PEND_DETECT |\
156                            PVR2_I2C_PEND_CLIENT |\
157                            PVR2_I2C_PEND_REFRESH |\
158                            PVR2_I2C_PEND_STALE)
159
160 /* Disposition of firmware1 loading situation */
161 #define FW1_STATE_UNKNOWN 0
162 #define FW1_STATE_MISSING 1
163 #define FW1_STATE_FAILED 2
164 #define FW1_STATE_RELOAD 3
165 #define FW1_STATE_OK 4
166
167 /* What state the device is in if it is a hybrid */
168 #define PVR2_PATHWAY_UNKNOWN 0
169 #define PVR2_PATHWAY_ANALOG 1
170 #define PVR2_PATHWAY_DIGITAL 2
171
172 typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
173 #define PVR2_I2C_FUNC_CNT 128
174
175 /* This structure contains all state data directly needed to
176    manipulate the hardware (as opposed to complying with a kernel
177    interface) */
178 struct pvr2_hdw {
179         /* Underlying USB device handle */
180         struct usb_device *usb_dev;
181         struct usb_interface *usb_intf;
182
183         /* Device description, anything that must adjust behavior based on
184            device specific info will use information held here. */
185         const struct pvr2_device_desc *hdw_desc;
186
187         /* Kernel worker thread handling */
188         struct workqueue_struct *workqueue;
189         struct work_struct workpoll;     /* Update driver state */
190         struct work_struct worki2csync;  /* Update i2c clients */
191
192         /* Video spigot */
193         struct pvr2_stream *vid_stream;
194
195         /* Mutex for all hardware state control */
196         struct mutex big_lock_mutex;
197         int big_lock_held;  /* For debugging */
198
199         char name[32];
200
201         /* I2C stuff */
202         struct i2c_adapter i2c_adap;
203         struct i2c_algorithm i2c_algo;
204         pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
205         int i2c_cx25840_hack_state;
206         int i2c_linked;
207         unsigned int i2c_pend_types;    /* Which types of update are needed */
208         unsigned long i2c_pend_mask;    /* Change bits we need to scan */
209         unsigned long i2c_stale_mask;   /* Pending broadcast change bits */
210         unsigned long i2c_active_mask;  /* All change bits currently in use */
211         struct list_head i2c_clients;
212         struct mutex i2c_list_lock;
213
214         /* Frequency table */
215         unsigned int freqTable[FREQTABLE_SIZE];
216         unsigned int freqProgSlot;
217
218         /* Stuff for handling low level control interaction with device */
219         struct mutex ctl_lock_mutex;
220         int ctl_lock_held;  /* For debugging */
221         struct urb *ctl_write_urb;
222         struct urb *ctl_read_urb;
223         unsigned char *ctl_write_buffer;
224         unsigned char *ctl_read_buffer;
225         int ctl_write_pend_flag;
226         int ctl_read_pend_flag;
227         int ctl_timeout_flag;
228         struct completion ctl_done;
229         unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
230         int cmd_debug_state;               // Low level command debugging info
231         unsigned char cmd_debug_code;      //
232         unsigned int cmd_debug_write_len;  //
233         unsigned int cmd_debug_read_len;   //
234
235         /* Bits of state that describe what is going on with various parts
236            of the driver. */
237         int state_pathway_ok;         /* Pathway config is ok */
238         int state_encoder_ok;         /* Encoder is operational */
239         int state_encoder_run;        /* Encoder is running */
240         int state_encoder_config;     /* Encoder is configured */
241         int state_encoder_waitok;     /* Encoder pre-wait done */
242         int state_decoder_run;        /* Decoder is running */
243         int state_usbstream_run;      /* FX2 is streaming */
244         int state_decoder_quiescent;  /* Decoder idle for > 50msec */
245         int state_pipeline_config;    /* Pipeline is configured */
246         int state_pipeline_req;       /* Somebody wants to stream */
247         int state_pipeline_pause;     /* Pipeline must be paused */
248         int state_pipeline_idle;      /* Pipeline not running */
249
250         /* This is the master state of the driver.  It is the combined
251            result of other bits of state.  Examining this will indicate the
252            overall state of the driver.  Values here are one of
253            PVR2_STATE_xxxx */
254         unsigned int master_state;
255
256         /* True if device led is currently on */
257         int led_on;
258
259         /* True if states must be re-evaluated */
260         int state_stale;
261
262         void (*state_func)(void *);
263         void *state_data;
264
265         /* Timer for measuring decoder settling time */
266         struct timer_list quiescent_timer;
267
268         /* Timer for measuring encoder pre-wait time */
269         struct timer_list encoder_wait_timer;
270
271         /* Place to block while waiting for state changes */
272         wait_queue_head_t state_wait_data;
273
274
275         int flag_ok;            /* device in known good state */
276         int flag_disconnected;  /* flag_ok == 0 due to disconnect */
277         int flag_init_ok;       /* true if structure is fully initialized */
278         int fw1_state;          /* current situation with fw1 */
279         int pathway_state;      /* one of PVR2_PATHWAY_xxx */
280         int flag_decoder_missed;/* We've noticed missing decoder */
281         int flag_tripped;       /* Indicates overall failure to start */
282
283         struct pvr2_decoder_ctrl *decoder_ctrl;
284
285         // CPU firmware info (used to help find / save firmware data)
286         char *fw_buffer;
287         unsigned int fw_size;
288         int fw_cpu_flag; /* True if we are dealing with the CPU */
289
290         // True if there is a request to trigger logging of state in each
291         // module.
292         int log_requested;
293
294         /* Tuner / frequency control stuff */
295         unsigned int tuner_type;
296         int tuner_updated;
297         unsigned int freqValTelevision;  /* Current freq for tv mode */
298         unsigned int freqValRadio;       /* Current freq for radio mode */
299         unsigned int freqSlotTelevision; /* Current slot for tv mode */
300         unsigned int freqSlotRadio;      /* Current slot for radio mode */
301         unsigned int freqSelector;       /* 0=radio 1=television */
302         int freqDirty;
303
304         /* Current tuner info - this information is polled from the I2C bus */
305         struct v4l2_tuner tuner_signal_info;
306         int tuner_signal_stale;
307
308         /* Video standard handling */
309         v4l2_std_id std_mask_eeprom; // Hardware supported selections
310         v4l2_std_id std_mask_avail;  // Which standards we may select from
311         v4l2_std_id std_mask_cur;    // Currently selected standard(s)
312         unsigned int std_enum_cnt;   // # of enumerated standards
313         int std_enum_cur;            // selected standard enumeration value
314         int std_dirty;               // True if std_mask_cur has changed
315         struct pvr2_ctl_info std_info_enum;
316         struct pvr2_ctl_info std_info_avail;
317         struct pvr2_ctl_info std_info_cur;
318         struct v4l2_standard *std_defs;
319         const char **std_enum_names;
320
321         // Generated string names, one per actual V4L2 standard
322         const char *std_mask_ptrs[32];
323         char std_mask_names[32][10];
324
325         int unit_number;             /* ID for driver instance */
326         unsigned long serial_number; /* ID for hardware itself */
327
328         char bus_info[32]; /* Bus location info */
329
330         /* Minor numbers used by v4l logic (yes, this is a hack, as there
331            should be no v4l junk here).  Probably a better way to do this. */
332         int v4l_minor_number_video;
333         int v4l_minor_number_vbi;
334         int v4l_minor_number_radio;
335
336         /* Bit mask of PVR2_CVAL_INPUT choices which are valid */
337         unsigned int input_avail_mask;
338
339         /* Location of eeprom or a negative number if none */
340         int eeprom_addr;
341
342         enum pvr2_config active_stream_type;
343         enum pvr2_config desired_stream_type;
344
345         /* Control state needed for cx2341x module */
346         struct cx2341x_mpeg_params enc_cur_state;
347         struct cx2341x_mpeg_params enc_ctl_state;
348         /* True if an encoder attribute has changed */
349         int enc_stale;
350         /* True if an unsafe encoder attribute has changed */
351         int enc_unsafe_stale;
352         /* True if enc_cur_state is valid */
353         int enc_cur_valid;
354
355         /* Control state */
356 #define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
357         VCREATE_DATA(brightness);
358         VCREATE_DATA(contrast);
359         VCREATE_DATA(saturation);
360         VCREATE_DATA(hue);
361         VCREATE_DATA(volume);
362         VCREATE_DATA(balance);
363         VCREATE_DATA(bass);
364         VCREATE_DATA(treble);
365         VCREATE_DATA(mute);
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         struct pvr2_dvb_adapter dvb;
379 };
380
381 /* This function gets the current frequency */
382 unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
383 void pvr2_hdw_set_decoder(struct pvr2_hdw *,struct pvr2_decoder_ctrl *);
384
385 #endif /* __PVRUSB2_HDW_INTERNAL_H */
386
387 /*
388   Stuff for Emacs to see, in order to encourage consistent editing style:
389   *** Local Variables: ***
390   *** mode: c ***
391   *** fill-column: 75 ***
392   *** tab-width: 8 ***
393   *** c-basic-offset: 8 ***
394   *** End: ***
395   */