blob: c8192d8a6083df7a94b4ff8f68bb03290342321f [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
61struct pvr2_decoder;
62
63typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
64typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
Mike Isely5549f542006-12-27 23:28:54 -030065typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
Mike Iselyd8554972006-06-26 20:58:46 -030066typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
67typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
68typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
69 char *,unsigned int,unsigned int *);
70typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
71 const char *,unsigned int,
72 int *mskp,int *valp);
Mike Iselya761f432006-06-25 20:04:44 -030073typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
Mike Iselyd8554972006-06-26 20:58:46 -030074
75/* This structure describes a specific control. A table of these is set up
76 in pvrusb2-hdw.c. */
77struct pvr2_ctl_info {
78 /* Control's name suitable for use as an identifier */
79 const char *name;
80
81 /* Short description of control */
82 const char *desc;
83
84 /* Control's implementation */
85 pvr2_ctlf_get_value get_value; /* Get its value */
Mike Isely26dd1c52008-08-31 20:55:03 -030086 pvr2_ctlf_get_value get_def_value; /* Get its default value */
Mike Isely89ebd632006-08-08 09:10:07 -030087 pvr2_ctlf_get_value get_min_value; /* Get minimum allowed value */
88 pvr2_ctlf_get_value get_max_value; /* Get maximum allowed value */
Mike Iselyd8554972006-06-26 20:58:46 -030089 pvr2_ctlf_set_value set_value; /* Set its value */
Mike Isely5549f542006-12-27 23:28:54 -030090 pvr2_ctlf_check_value check_value; /* Check that value is valid */
Mike Iselyd8554972006-06-26 20:58:46 -030091 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 */
Mike Iselya761f432006-06-25 20:04:44 -030095 pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
Mike Iselyd8554972006-06-26 20:58:46 -030096
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
Mike Iselyc05c0462006-06-25 20:04:25 -0300130/* Same as pvr2_ctl_info, but includes storage for the control description */
131#define PVR2_CTLD_INFO_DESC_SIZE 32
132struct pvr2_ctld_info {
133 struct pvr2_ctl_info info;
134 char desc[PVR2_CTLD_INFO_DESC_SIZE];
135};
136
Mike Iselyd8554972006-06-26 20:58:46 -0300137struct pvr2_ctrl {
138 const struct pvr2_ctl_info *info;
139 struct pvr2_hdw *hdw;
140};
141
142
Mike Iselyd8554972006-06-26 20:58:46 -0300143struct pvr2_decoder_ctrl {
144 void *ctxt;
145 void (*detach)(void *);
146 void (*enable)(void *,int);
Mike Iselyd8554972006-06-26 20:58:46 -0300147 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
Mike Isely62433e32008-04-22 14:45:40 -0300167/* 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
Mike Iselyd8554972006-06-26 20:58:46 -0300172typedef 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) */
178struct pvr2_hdw {
179 /* Underlying USB device handle */
180 struct usb_device *usb_dev;
181 struct usb_interface *usb_intf;
182
Mike Iselyb72b7bf2009-03-06 23:20:31 -0300183 /* Our handle into the v4l2 sub-device architecture */
184 struct v4l2_device v4l2_dev;
Mike Iselyf66fbd72007-11-26 01:55:07 -0300185 /* Device description, anything that must adjust behavior based on
186 device specific info will use information held here. */
Mike Isely989eb152007-11-26 01:53:12 -0300187 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -0300188
Mike Isely681c7392007-11-26 01:48:52 -0300189 /* Kernel worker thread handling */
190 struct workqueue_struct *workqueue;
191 struct work_struct workpoll; /* Update driver state */
192 struct work_struct worki2csync; /* Update i2c clients */
Mike Isely681c7392007-11-26 01:48:52 -0300193
Mike Iselyd8554972006-06-26 20:58:46 -0300194 /* Video spigot */
195 struct pvr2_stream *vid_stream;
196
197 /* Mutex for all hardware state control */
198 struct mutex big_lock_mutex;
199 int big_lock_held; /* For debugging */
200
Mike Isely13a88792009-01-14 04:22:56 -0300201 /* This is a simple string which identifies the instance of this
202 driver. It is unique within the set of existing devices, but
203 there is no attempt to keep the name consistent with the same
204 physical device each time. */
Mike Iselyd8554972006-06-26 20:58:46 -0300205 char name[32];
206
Mike Isely13a88792009-01-14 04:22:56 -0300207 /* This is a simple string which identifies the physical device
208 instance itself - if possible. (If not possible, then it is
209 based on the specific driver instance, similar to name above.)
210 The idea here is that userspace might hopefully be able to use
211 this recognize specific tuners. It will encode a serial number,
212 if available. */
213 char identifier[32];
214
Mike Iselyd8554972006-06-26 20:58:46 -0300215 /* I2C stuff */
216 struct i2c_adapter i2c_adap;
217 struct i2c_algorithm i2c_algo;
218 pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
219 int i2c_cx25840_hack_state;
220 int i2c_linked;
221 unsigned int i2c_pend_types; /* Which types of update are needed */
222 unsigned long i2c_pend_mask; /* Change bits we need to scan */
223 unsigned long i2c_stale_mask; /* Pending broadcast change bits */
224 unsigned long i2c_active_mask; /* All change bits currently in use */
225 struct list_head i2c_clients;
226 struct mutex i2c_list_lock;
227
228 /* Frequency table */
229 unsigned int freqTable[FREQTABLE_SIZE];
230 unsigned int freqProgSlot;
Mike Iselyd8554972006-06-26 20:58:46 -0300231
232 /* Stuff for handling low level control interaction with device */
233 struct mutex ctl_lock_mutex;
234 int ctl_lock_held; /* For debugging */
235 struct urb *ctl_write_urb;
236 struct urb *ctl_read_urb;
237 unsigned char *ctl_write_buffer;
238 unsigned char *ctl_read_buffer;
Mike Isely26e33042007-12-03 01:43:23 -0300239 int ctl_write_pend_flag;
240 int ctl_read_pend_flag;
241 int ctl_timeout_flag;
Mike Iselyd8554972006-06-26 20:58:46 -0300242 struct completion ctl_done;
243 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
244 int cmd_debug_state; // Low level command debugging info
245 unsigned char cmd_debug_code; //
246 unsigned int cmd_debug_write_len; //
247 unsigned int cmd_debug_read_len; //
248
Mike Isely681c7392007-11-26 01:48:52 -0300249 /* Bits of state that describe what is going on with various parts
250 of the driver. */
Mike Isely62433e32008-04-22 14:45:40 -0300251 int state_pathway_ok; /* Pathway config is ok */
Mike Iselye802c142007-12-03 01:44:43 -0300252 int state_encoder_ok; /* Encoder is operational */
253 int state_encoder_run; /* Encoder is running */
254 int state_encoder_config; /* Encoder is configured */
255 int state_encoder_waitok; /* Encoder pre-wait done */
Mike Iselyd913d632008-04-06 04:04:35 -0300256 int state_encoder_runok; /* Encoder has run for >= .25 sec */
Mike Iselye802c142007-12-03 01:44:43 -0300257 int state_decoder_run; /* Decoder is running */
258 int state_usbstream_run; /* FX2 is streaming */
259 int state_decoder_quiescent; /* Decoder idle for > 50msec */
260 int state_pipeline_config; /* Pipeline is configured */
Mike Isely7f421fe2008-04-22 14:45:39 -0300261 int state_pipeline_req; /* Somebody wants to stream */
262 int state_pipeline_pause; /* Pipeline must be paused */
263 int state_pipeline_idle; /* Pipeline not running */
Mike Isely681c7392007-11-26 01:48:52 -0300264
265 /* This is the master state of the driver. It is the combined
266 result of other bits of state. Examining this will indicate the
267 overall state of the driver. Values here are one of
268 PVR2_STATE_xxxx */
269 unsigned int master_state;
270
Mike Isely40381cb2008-04-22 14:45:42 -0300271 /* True if device led is currently on */
272 int led_on;
273
Mike Isely681c7392007-11-26 01:48:52 -0300274 /* True if states must be re-evaluated */
275 int state_stale;
276
277 void (*state_func)(void *);
278 void *state_data;
279
280 /* Timer for measuring decoder settling time */
281 struct timer_list quiescent_timer;
282
283 /* Timer for measuring encoder pre-wait time */
284 struct timer_list encoder_wait_timer;
285
Mike Iselyd913d632008-04-06 04:04:35 -0300286 /* Timer for measuring encoder minimum run time */
287 struct timer_list encoder_run_timer;
288
Mike Isely681c7392007-11-26 01:48:52 -0300289 /* Place to block while waiting for state changes */
290 wait_queue_head_t state_wait_data;
291
292
Mike Isely9a607f02007-10-14 18:18:12 -0300293 int flag_ok; /* device in known good state */
294 int flag_disconnected; /* flag_ok == 0 due to disconnect */
295 int flag_init_ok; /* true if structure is fully initialized */
Mike Isely9a607f02007-10-14 18:18:12 -0300296 int fw1_state; /* current situation with fw1 */
Mike Isely62433e32008-04-22 14:45:40 -0300297 int pathway_state; /* one of PVR2_PATHWAY_xxx */
Mike Isely681c7392007-11-26 01:48:52 -0300298 int flag_decoder_missed;/* We've noticed missing decoder */
299 int flag_tripped; /* Indicates overall failure to start */
Mike Iselyd8554972006-06-26 20:58:46 -0300300
301 struct pvr2_decoder_ctrl *decoder_ctrl;
Mike Isely00e5f732009-03-07 00:17:11 -0300302 unsigned int decoder_client_id;
Mike Iselyd8554972006-06-26 20:58:46 -0300303
304 // CPU firmware info (used to help find / save firmware data)
305 char *fw_buffer;
306 unsigned int fw_size;
Mike Isely4db666c2007-09-08 22:16:27 -0300307 int fw_cpu_flag; /* True if we are dealing with the CPU */
Mike Iselyd8554972006-06-26 20:58:46 -0300308
Mike Iselyd8554972006-06-26 20:58:46 -0300309 // True if there is a request to trigger logging of state in each
310 // module.
311 int log_requested;
312
313 /* Tuner / frequency control stuff */
314 unsigned int tuner_type;
315 int tuner_updated;
Mike Isely1bde0282006-12-27 23:30:13 -0300316 unsigned int freqValTelevision; /* Current freq for tv mode */
317 unsigned int freqValRadio; /* Current freq for radio mode */
318 unsigned int freqSlotTelevision; /* Current slot for tv mode */
319 unsigned int freqSlotRadio; /* Current slot for radio mode */
320 unsigned int freqSelector; /* 0=radio 1=television */
Mike Iselyd8554972006-06-26 20:58:46 -0300321 int freqDirty;
322
Mike Isely18103c52007-01-20 00:09:47 -0300323 /* Current tuner info - this information is polled from the I2C bus */
324 struct v4l2_tuner tuner_signal_info;
325 int tuner_signal_stale;
326
Mike Isely432907f2008-08-31 21:02:20 -0300327 /* Cropping capability info */
328 struct v4l2_cropcap cropcap_info;
329 int cropcap_stale;
330
Mike Iselyd8554972006-06-26 20:58:46 -0300331 /* Video standard handling */
332 v4l2_std_id std_mask_eeprom; // Hardware supported selections
333 v4l2_std_id std_mask_avail; // Which standards we may select from
334 v4l2_std_id std_mask_cur; // Currently selected standard(s)
335 unsigned int std_enum_cnt; // # of enumerated standards
336 int std_enum_cur; // selected standard enumeration value
337 int std_dirty; // True if std_mask_cur has changed
338 struct pvr2_ctl_info std_info_enum;
339 struct pvr2_ctl_info std_info_avail;
340 struct pvr2_ctl_info std_info_cur;
341 struct v4l2_standard *std_defs;
342 const char **std_enum_names;
343
344 // Generated string names, one per actual V4L2 standard
345 const char *std_mask_ptrs[32];
346 char std_mask_names[32][10];
347
348 int unit_number; /* ID for driver instance */
349 unsigned long serial_number; /* ID for hardware itself */
350
Mike Isely31a18542007-04-08 01:11:47 -0300351 char bus_info[32]; /* Bus location info */
352
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -0300353 /* Minor numbers used by v4l logic (yes, this is a hack, as there
354 should be no v4l junk here). Probably a better way to do this. */
Mike Isely80793842006-12-27 23:12:28 -0300355 int v4l_minor_number_video;
356 int v4l_minor_number_vbi;
Mike Iselyfd5a75f2006-12-27 23:11:22 -0300357 int v4l_minor_number_radio;
Mike Iselyd8554972006-06-26 20:58:46 -0300358
Mike Isely1cb03b72008-04-21 03:47:43 -0300359 /* Bit mask of PVR2_CVAL_INPUT choices which are valid for the hardware */
Mike Isely7fb20fa2008-04-22 14:45:37 -0300360 unsigned int input_avail_mask;
Mike Isely1cb03b72008-04-21 03:47:43 -0300361 /* Bit mask of PVR2_CVAL_INPUT choices which are currenly allowed */
362 unsigned int input_allowed_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -0300363
Mike Iselyd8554972006-06-26 20:58:46 -0300364 /* Location of eeprom or a negative number if none */
365 int eeprom_addr;
366
Mike Isely681c7392007-11-26 01:48:52 -0300367 enum pvr2_config active_stream_type;
368 enum pvr2_config desired_stream_type;
Mike Iselyd8554972006-06-26 20:58:46 -0300369
Mike Iselyb30d2442006-06-25 20:05:01 -0300370 /* Control state needed for cx2341x module */
371 struct cx2341x_mpeg_params enc_cur_state;
372 struct cx2341x_mpeg_params enc_ctl_state;
373 /* True if an encoder attribute has changed */
374 int enc_stale;
Mike Isely681c7392007-11-26 01:48:52 -0300375 /* True if an unsafe encoder attribute has changed */
376 int enc_unsafe_stale;
Mike Iselyb30d2442006-06-25 20:05:01 -0300377 /* True if enc_cur_state is valid */
378 int enc_cur_valid;
Mike Iselyc05c0462006-06-25 20:04:25 -0300379
Mike Iselyd8554972006-06-26 20:58:46 -0300380 /* Control state */
381#define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
382 VCREATE_DATA(brightness);
383 VCREATE_DATA(contrast);
384 VCREATE_DATA(saturation);
385 VCREATE_DATA(hue);
386 VCREATE_DATA(volume);
387 VCREATE_DATA(balance);
388 VCREATE_DATA(bass);
389 VCREATE_DATA(treble);
390 VCREATE_DATA(mute);
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300391 VCREATE_DATA(cropl);
392 VCREATE_DATA(cropt);
393 VCREATE_DATA(cropw);
394 VCREATE_DATA(croph);
Mike Iselyc05c0462006-06-25 20:04:25 -0300395 VCREATE_DATA(input);
396 VCREATE_DATA(audiomode);
397 VCREATE_DATA(res_hor);
398 VCREATE_DATA(res_ver);
Mike Iselyd8554972006-06-26 20:58:46 -0300399 VCREATE_DATA(srate);
Mike Iselyd8554972006-06-26 20:58:46 -0300400#undef VCREATE_DATA
401
Mike Iselyb30d2442006-06-25 20:05:01 -0300402 struct pvr2_ctld_info *mpeg_ctrl_info;
Mike Iselyc05c0462006-06-25 20:04:25 -0300403
Mike Iselyd8554972006-06-26 20:58:46 -0300404 struct pvr2_ctrl *controls;
Mike Iselyc05c0462006-06-25 20:04:25 -0300405 unsigned int control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -0300406};
407
Mike Isely1bde0282006-12-27 23:30:13 -0300408/* This function gets the current frequency */
409unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
Mike Isely681c7392007-11-26 01:48:52 -0300410void pvr2_hdw_set_decoder(struct pvr2_hdw *,struct pvr2_decoder_ctrl *);
Mike Isely1bde0282006-12-27 23:30:13 -0300411
Mike Iselya51f5002009-03-06 23:30:37 -0300412void pvr2_hdw_status_poll(struct pvr2_hdw *);
413
Mike Iselyd8554972006-06-26 20:58:46 -0300414#endif /* __PVRUSB2_HDW_INTERNAL_H */
415
416/*
417 Stuff for Emacs to see, in order to encourage consistent editing style:
418 *** Local Variables: ***
419 *** mode: c ***
420 *** fill-column: 75 ***
421 *** tab-width: 8 ***
422 *** c-basic-offset: 8 ***
423 *** End: ***
424 */