blob: c4bb0d1b5f9fd6ad5fac44be82d11ab2e8e8af52 [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
21#include <linux/errno.h>
22#include <linux/string.h>
23#include <linux/slab.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040024#include <linux/module.h>
Mike Iselyd8554972006-06-26 20:58:46 -030025#include <linux/firmware.h>
Mike Iselyd8554972006-06-26 20:58:46 -030026#include <linux/videodev2.h>
Mike Isely32ffa9a2006-09-23 22:26:52 -030027#include <media/v4l2-common.h>
Mike Isely75212a02009-03-07 01:48:42 -030028#include <media/tuner.h>
Mike Iselyd8554972006-06-26 20:58:46 -030029#include "pvrusb2.h"
30#include "pvrusb2-std.h"
31#include "pvrusb2-util.h"
32#include "pvrusb2-hdw.h"
33#include "pvrusb2-i2c-core.h"
Mike Iselyd8554972006-06-26 20:58:46 -030034#include "pvrusb2-eeprom.h"
35#include "pvrusb2-hdw-internal.h"
36#include "pvrusb2-encoder.h"
37#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030038#include "pvrusb2-fx2-cmd.h"
Mike Isely5f6dae82009-03-07 00:39:34 -030039#include "pvrusb2-wm8775.h"
Mike Isely6f956512009-03-07 00:43:26 -030040#include "pvrusb2-video-v4l.h"
Mike Isely634ba262009-03-07 00:54:02 -030041#include "pvrusb2-cx2584x-v4l.h"
Mike Isely2a6b6272009-03-15 17:53:29 -030042#include "pvrusb2-cs53l32a.h"
Mike Isely76891d62009-03-07 00:52:06 -030043#include "pvrusb2-audio.h"
Mike Iselyd8554972006-06-26 20:58:46 -030044
Mike Isely1bde0282006-12-27 23:30:13 -030045#define TV_MIN_FREQ 55250000L
46#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030047
Mike Isely83ce57a2008-05-26 05:51:57 -030048/* This defines a minimum interval that the decoder must remain quiet
49 before we are allowed to start it running. */
50#define TIME_MSEC_DECODER_WAIT 50
51
Mike Isely6e931372010-02-06 02:10:38 -030052/* This defines a minimum interval that the decoder must be allowed to run
53 before we can safely begin using its streaming output. */
54#define TIME_MSEC_DECODER_STABILIZATION_WAIT 300
55
Mike Isely83ce57a2008-05-26 05:51:57 -030056/* This defines a minimum interval that the encoder must remain quiet
Mike Isely91b5b482010-02-06 02:12:33 -030057 before we are allowed to configure it. */
58#define TIME_MSEC_ENCODER_WAIT 50
Mike Isely83ce57a2008-05-26 05:51:57 -030059
60/* This defines the minimum interval that the encoder must successfully run
61 before we consider that the encoder has run at least once since its
62 firmware has been loaded. This measurement is in important for cases
63 where we can't do something until we know that the encoder has been run
64 at least once. */
65#define TIME_MSEC_ENCODER_OK 250
66
Mike Iselya0fd1cb2006-06-30 11:35:28 -030067static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030068static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030069
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030070static int ctlchg;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030071static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030072static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
73static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
74static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030075static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030076
77module_param(ctlchg, int, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
79module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
80MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
Mike Iselyd8554972006-06-26 20:58:46 -030081module_param(procreload, int, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(procreload,
83 "Attempt init failure recovery with firmware reload");
84module_param_array(tuner, int, NULL, 0444);
85MODULE_PARM_DESC(tuner,"specify installed tuner type");
86module_param_array(video_std, int, NULL, 0444);
87MODULE_PARM_DESC(video_std,"specify initial video standard");
88module_param_array(tolerance, int, NULL, 0444);
89MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
90
Mike Isely6f441ed2009-06-20 14:51:29 -030091/* US Broadcast channel 3 (61.25 MHz), to help with testing */
92static int default_tv_freq = 61250000L;
Michael Krufky5a4f5da62008-05-11 16:37:50 -030093/* 104.3 MHz, a usable FM station for my area */
94static int default_radio_freq = 104300000L;
95
96module_param_named(tv_freq, default_tv_freq, int, 0444);
97MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
98module_param_named(radio_freq, default_radio_freq, int, 0444);
99MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
100
Mike Iselyd8554972006-06-26 20:58:46 -0300101#define PVR2_CTL_WRITE_ENDPOINT 0x01
102#define PVR2_CTL_READ_ENDPOINT 0x81
103
104#define PVR2_GPIO_IN 0x9008
105#define PVR2_GPIO_OUT 0x900c
106#define PVR2_GPIO_DIR 0x9020
107
108#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
109
110#define PVR2_FIRMWARE_ENDPOINT 0x02
111
112/* size of a firmware chunk */
113#define FIRMWARE_CHUNK_SIZE 0x2000
114
Mike Iselyedb9dcb2009-03-07 00:37:10 -0300115typedef void (*pvr2_subdev_update_func)(struct pvr2_hdw *,
116 struct v4l2_subdev *);
117
118static const pvr2_subdev_update_func pvr2_module_update_functions[] = {
Mike Isely4ecbc282009-03-07 00:49:19 -0300119 [PVR2_CLIENT_ID_WM8775] = pvr2_wm8775_subdev_update,
Mike Isely6f956512009-03-07 00:43:26 -0300120 [PVR2_CLIENT_ID_SAA7115] = pvr2_saa7115_subdev_update,
Mike Isely76891d62009-03-07 00:52:06 -0300121 [PVR2_CLIENT_ID_MSP3400] = pvr2_msp3400_subdev_update,
Mike Isely634ba262009-03-07 00:54:02 -0300122 [PVR2_CLIENT_ID_CX25840] = pvr2_cx25840_subdev_update,
Mike Isely2a6b6272009-03-15 17:53:29 -0300123 [PVR2_CLIENT_ID_CS53L32A] = pvr2_cs53l32a_subdev_update,
Mike Iselyedb9dcb2009-03-07 00:37:10 -0300124};
125
Mike Iselye9c64a72009-03-06 23:42:20 -0300126static const char *module_names[] = {
127 [PVR2_CLIENT_ID_MSP3400] = "msp3400",
128 [PVR2_CLIENT_ID_CX25840] = "cx25840",
129 [PVR2_CLIENT_ID_SAA7115] = "saa7115",
130 [PVR2_CLIENT_ID_TUNER] = "tuner",
Mike Iselybb652422009-03-14 14:09:04 -0300131 [PVR2_CLIENT_ID_DEMOD] = "tuner",
Mike Isely851981a2009-03-07 02:02:32 -0300132 [PVR2_CLIENT_ID_CS53L32A] = "cs53l32a",
Mike Isely5f6dae82009-03-07 00:39:34 -0300133 [PVR2_CLIENT_ID_WM8775] = "wm8775",
Mike Iselye9c64a72009-03-06 23:42:20 -0300134};
135
136
137static const unsigned char *module_i2c_addresses[] = {
138 [PVR2_CLIENT_ID_TUNER] = "\x60\x61\x62\x63",
Mike Iselybb652422009-03-14 14:09:04 -0300139 [PVR2_CLIENT_ID_DEMOD] = "\x43",
Mike Isely1dfe6c72009-03-07 02:00:21 -0300140 [PVR2_CLIENT_ID_MSP3400] = "\x40",
141 [PVR2_CLIENT_ID_SAA7115] = "\x21",
Mike Iselyae111f72009-03-07 00:57:42 -0300142 [PVR2_CLIENT_ID_WM8775] = "\x1b",
Mike Isely0b467012009-03-07 01:49:37 -0300143 [PVR2_CLIENT_ID_CX25840] = "\x44",
Mike Isely23334a22009-03-07 02:03:28 -0300144 [PVR2_CLIENT_ID_CS53L32A] = "\x11",
Mike Iselye9c64a72009-03-06 23:42:20 -0300145};
146
147
Mike Isely27eab382009-04-06 01:51:38 -0300148static const char *ir_scheme_names[] = {
149 [PVR2_IR_SCHEME_NONE] = "none",
150 [PVR2_IR_SCHEME_29XXX] = "29xxx",
151 [PVR2_IR_SCHEME_24XXX] = "24xxx (29xxx emulation)",
152 [PVR2_IR_SCHEME_24XXX_MCE] = "24xxx (MCE device)",
153 [PVR2_IR_SCHEME_ZILOG] = "Zilog",
154};
155
156
Mike Iselyb30d2442006-06-25 20:05:01 -0300157/* Define the list of additional controls we'll dynamically construct based
158 on query of the cx2341x module. */
159struct pvr2_mpeg_ids {
160 const char *strid;
161 int id;
162};
163static const struct pvr2_mpeg_ids mpeg_ids[] = {
164 {
165 .strid = "audio_layer",
166 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
167 },{
168 .strid = "audio_bitrate",
169 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
170 },{
171 /* Already using audio_mode elsewhere :-( */
172 .strid = "mpeg_audio_mode",
173 .id = V4L2_CID_MPEG_AUDIO_MODE,
174 },{
175 .strid = "mpeg_audio_mode_extension",
176 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
177 },{
178 .strid = "audio_emphasis",
179 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
180 },{
181 .strid = "audio_crc",
182 .id = V4L2_CID_MPEG_AUDIO_CRC,
183 },{
184 .strid = "video_aspect",
185 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
186 },{
187 .strid = "video_b_frames",
188 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
189 },{
190 .strid = "video_gop_size",
191 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
192 },{
193 .strid = "video_gop_closure",
194 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
195 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300196 .strid = "video_bitrate_mode",
197 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
198 },{
199 .strid = "video_bitrate",
200 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
201 },{
202 .strid = "video_bitrate_peak",
203 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
204 },{
205 .strid = "video_temporal_decimation",
206 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
207 },{
208 .strid = "stream_type",
209 .id = V4L2_CID_MPEG_STREAM_TYPE,
210 },{
211 .strid = "video_spatial_filter_mode",
212 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
213 },{
214 .strid = "video_spatial_filter",
215 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
216 },{
217 .strid = "video_luma_spatial_filter_type",
218 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
219 },{
220 .strid = "video_chroma_spatial_filter_type",
221 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
222 },{
223 .strid = "video_temporal_filter_mode",
224 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
225 },{
226 .strid = "video_temporal_filter",
227 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
228 },{
229 .strid = "video_median_filter_type",
230 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
231 },{
232 .strid = "video_luma_median_filter_top",
233 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
234 },{
235 .strid = "video_luma_median_filter_bottom",
236 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
237 },{
238 .strid = "video_chroma_median_filter_top",
239 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
240 },{
241 .strid = "video_chroma_median_filter_bottom",
242 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
243 }
244};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300245#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300246
Mike Iselyd8554972006-06-26 20:58:46 -0300247
Mike Isely434449f2006-08-08 09:10:06 -0300248static const char *control_values_srate[] = {
249 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
250 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
251 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
252};
Mike Iselyd8554972006-06-26 20:58:46 -0300253
Mike Iselyd8554972006-06-26 20:58:46 -0300254
255
256static const char *control_values_input[] = {
257 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300258 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300259 [PVR2_CVAL_INPUT_RADIO] = "radio",
260 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
261 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
262};
263
264
265static const char *control_values_audiomode[] = {
266 [V4L2_TUNER_MODE_MONO] = "Mono",
267 [V4L2_TUNER_MODE_STEREO] = "Stereo",
268 [V4L2_TUNER_MODE_LANG1] = "Lang1",
269 [V4L2_TUNER_MODE_LANG2] = "Lang2",
270 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
271};
272
273
274static const char *control_values_hsm[] = {
275 [PVR2_CVAL_HSM_FAIL] = "Fail",
276 [PVR2_CVAL_HSM_HIGH] = "High",
277 [PVR2_CVAL_HSM_FULL] = "Full",
278};
279
280
Mike Isely681c7392007-11-26 01:48:52 -0300281static const char *pvr2_state_names[] = {
282 [PVR2_STATE_NONE] = "none",
283 [PVR2_STATE_DEAD] = "dead",
284 [PVR2_STATE_COLD] = "cold",
285 [PVR2_STATE_WARM] = "warm",
286 [PVR2_STATE_ERROR] = "error",
287 [PVR2_STATE_READY] = "ready",
288 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300289};
290
Mike Isely681c7392007-11-26 01:48:52 -0300291
Mike Isely694dca2b2008-03-28 05:42:10 -0300292struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300293 unsigned char id;
294 unsigned char *desc;
295};
296
Mike Isely694dca2b2008-03-28 05:42:10 -0300297static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300298 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
299 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300300 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300301 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
302 {FX2CMD_REG_WRITE, "write encoder register"},
303 {FX2CMD_REG_READ, "read encoder register"},
304 {FX2CMD_MEMSEL, "encoder memsel"},
305 {FX2CMD_I2C_WRITE, "i2c write"},
306 {FX2CMD_I2C_READ, "i2c read"},
307 {FX2CMD_GET_USB_SPEED, "get USB speed"},
308 {FX2CMD_STREAMING_ON, "stream on"},
309 {FX2CMD_STREAMING_OFF, "stream off"},
310 {FX2CMD_FWPOST1, "fwpost1"},
311 {FX2CMD_POWER_OFF, "power off"},
312 {FX2CMD_POWER_ON, "power on"},
313 {FX2CMD_DEEP_RESET, "deep reset"},
314 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
315 {FX2CMD_GET_IR_CODE, "get IR code"},
316 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
317 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
318 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
319 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
320 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
321 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
322 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
323};
324
325
Mike Isely1cb03b72008-04-21 03:47:43 -0300326static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300327static void pvr2_hdw_state_sched(struct pvr2_hdw *);
328static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300329static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300330static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300331static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
332static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
333static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300334static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300335static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300336static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300337static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
338static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300339static void pvr2_hdw_quiescent_timeout(unsigned long);
Mike Isely6e931372010-02-06 02:10:38 -0300340static void pvr2_hdw_decoder_stabilization_timeout(unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300341static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300342static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300343static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300344static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
345 unsigned int timeout,int probe_fl,
346 void *write_data,unsigned int write_len,
347 void *read_data,unsigned int read_len);
Mike Isely432907f2008-08-31 21:02:20 -0300348static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyac04d002012-02-20 02:28:56 -0300349static v4l2_std_id pvr2_hdw_get_detected_std(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300350
351static void trace_stbit(const char *name,int val)
352{
353 pvr2_trace(PVR2_TRACE_STBITS,
354 "State bit %s <-- %s",
355 name,(val ? "true" : "false"));
356}
357
Mike Iselyd8554972006-06-26 20:58:46 -0300358static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
359{
360 struct pvr2_hdw *hdw = cptr->hdw;
361 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
362 *vp = hdw->freqTable[hdw->freqProgSlot-1];
363 } else {
364 *vp = 0;
365 }
366 return 0;
367}
368
369static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
370{
371 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300372 unsigned int slotId = hdw->freqProgSlot;
373 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
374 hdw->freqTable[slotId-1] = v;
375 /* Handle side effects correctly - if we're tuned to this
376 slot, then forgot the slot id relation since the stored
377 frequency has been changed. */
378 if (hdw->freqSelector) {
379 if (hdw->freqSlotRadio == slotId) {
380 hdw->freqSlotRadio = 0;
381 }
382 } else {
383 if (hdw->freqSlotTelevision == slotId) {
384 hdw->freqSlotTelevision = 0;
385 }
386 }
Mike Iselyd8554972006-06-26 20:58:46 -0300387 }
388 return 0;
389}
390
391static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
392{
393 *vp = cptr->hdw->freqProgSlot;
394 return 0;
395}
396
397static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
398{
399 struct pvr2_hdw *hdw = cptr->hdw;
400 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
401 hdw->freqProgSlot = v;
402 }
403 return 0;
404}
405
406static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
407{
Mike Isely1bde0282006-12-27 23:30:13 -0300408 struct pvr2_hdw *hdw = cptr->hdw;
409 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300410 return 0;
411}
412
Mike Isely1bde0282006-12-27 23:30:13 -0300413static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300414{
415 unsigned freq = 0;
416 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300417 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
418 if (slotId > 0) {
419 freq = hdw->freqTable[slotId-1];
420 if (!freq) return 0;
421 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300422 }
Mike Isely1bde0282006-12-27 23:30:13 -0300423 if (hdw->freqSelector) {
424 hdw->freqSlotRadio = slotId;
425 } else {
426 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300427 }
428 return 0;
429}
430
431static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
432{
Mike Isely1bde0282006-12-27 23:30:13 -0300433 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300434 return 0;
435}
436
437static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
438{
439 return cptr->hdw->freqDirty != 0;
440}
441
442static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
443{
444 cptr->hdw->freqDirty = 0;
445}
446
447static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
448{
Mike Isely1bde0282006-12-27 23:30:13 -0300449 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300450 return 0;
451}
452
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300453static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
454{
Mike Isely432907f2008-08-31 21:02:20 -0300455 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
456 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
457 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300458 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300459 }
Mike Isely432907f2008-08-31 21:02:20 -0300460 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300461 return 0;
462}
463
464static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
465{
Mike Isely432907f2008-08-31 21:02:20 -0300466 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
467 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
468 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300469 return stat;
470 }
471 *left = cap->bounds.left;
472 if (cap->bounds.width > cptr->hdw->cropw_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300473 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300474 }
475 return 0;
476}
477
478static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
479{
Mike Isely432907f2008-08-31 21:02:20 -0300480 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
481 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
482 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300483 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300484 }
Mike Isely432907f2008-08-31 21:02:20 -0300485 *top = cap->bounds.top;
486 return 0;
487}
488
489static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
490{
491 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
492 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
493 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300494 return stat;
495 }
496 *top = cap->bounds.top;
497 if (cap->bounds.height > cptr->hdw->croph_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300498 *top += cap->bounds.height - cptr->hdw->croph_val;
499 }
500 return 0;
501}
502
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300503static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *width)
Mike Isely432907f2008-08-31 21:02:20 -0300504{
505 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300506 int stat, bleftend, cleft;
507
508 stat = pvr2_hdw_check_cropcap(cptr->hdw);
Mike Isely432907f2008-08-31 21:02:20 -0300509 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300510 return stat;
511 }
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300512 bleftend = cap->bounds.left+cap->bounds.width;
513 cleft = cptr->hdw->cropl_val;
514
515 *width = cleft < bleftend ? bleftend-cleft : 0;
Mike Isely432907f2008-08-31 21:02:20 -0300516 return 0;
517}
518
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300519static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *height)
Mike Isely432907f2008-08-31 21:02:20 -0300520{
521 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300522 int stat, btopend, ctop;
523
524 stat = pvr2_hdw_check_cropcap(cptr->hdw);
Mike Isely432907f2008-08-31 21:02:20 -0300525 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300526 return stat;
527 }
Servaas Vandenberghe6188a0f2011-02-13 17:51:09 -0300528 btopend = cap->bounds.top+cap->bounds.height;
529 ctop = cptr->hdw->cropt_val;
530
531 *height = ctop < btopend ? btopend-ctop : 0;
Mike Isely432907f2008-08-31 21:02:20 -0300532 return 0;
533}
534
535static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
536{
537 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
538 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
539 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300540 return stat;
541 }
542 *val = cap->bounds.left;
543 return 0;
544}
545
546static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
547{
548 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
549 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
550 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300551 return stat;
552 }
553 *val = cap->bounds.top;
554 return 0;
555}
556
557static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
558{
559 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
560 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
561 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300562 return stat;
563 }
564 *val = cap->bounds.width;
565 return 0;
566}
567
568static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
569{
570 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
571 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
572 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300573 return stat;
574 }
575 *val = cap->bounds.height;
576 return 0;
577}
578
579static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
580{
581 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
582 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
583 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300584 return stat;
585 }
586 *val = cap->defrect.left;
587 return 0;
588}
589
590static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
591{
592 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
593 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
594 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300595 return stat;
596 }
597 *val = cap->defrect.top;
598 return 0;
599}
600
601static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
602{
603 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
604 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
605 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300606 return stat;
607 }
608 *val = cap->defrect.width;
609 return 0;
610}
611
612static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
613{
614 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
615 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
616 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300617 return stat;
618 }
619 *val = cap->defrect.height;
620 return 0;
621}
622
623static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
624{
625 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
626 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
627 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300628 return stat;
629 }
630 *val = cap->pixelaspect.numerator;
631 return 0;
632}
633
634static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
635{
636 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
637 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
638 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300639 return stat;
640 }
641 *val = cap->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300642 return 0;
643}
644
Mike Isely3ad9fc32006-09-02 22:37:52 -0300645static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
646{
647 /* Actual maximum depends on the video standard in effect. */
648 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
649 *vp = 480;
650 } else {
651 *vp = 576;
652 }
653 return 0;
654}
655
656static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
657{
Mike Isely989eb152007-11-26 01:53:12 -0300658 /* Actual minimum depends on device digitizer type. */
659 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300660 *vp = 75;
661 } else {
662 *vp = 17;
663 }
664 return 0;
665}
666
Mike Isely1bde0282006-12-27 23:30:13 -0300667static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
668{
669 *vp = cptr->hdw->input_val;
670 return 0;
671}
672
Mike Isely29bf5b12008-04-22 14:45:37 -0300673static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
674{
Mike Isely1cb03b72008-04-21 03:47:43 -0300675 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300676}
677
Mike Isely1bde0282006-12-27 23:30:13 -0300678static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
679{
Mike Isely1cb03b72008-04-21 03:47:43 -0300680 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300681}
682
683static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
684{
685 return cptr->hdw->input_dirty != 0;
686}
687
688static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
689{
690 cptr->hdw->input_dirty = 0;
691}
692
Mike Isely5549f542006-12-27 23:28:54 -0300693
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300694static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
695{
Mike Isely644afdb2007-01-20 00:19:23 -0300696 unsigned long fv;
697 struct pvr2_hdw *hdw = cptr->hdw;
698 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300699 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300700 }
Mike Isely644afdb2007-01-20 00:19:23 -0300701 fv = hdw->tuner_signal_info.rangehigh;
702 if (!fv) {
703 /* Safety fallback */
704 *vp = TV_MAX_FREQ;
705 return 0;
706 }
707 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
708 fv = (fv * 125) / 2;
709 } else {
710 fv = fv * 62500;
711 }
712 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300713 return 0;
714}
715
716static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
717{
Mike Isely644afdb2007-01-20 00:19:23 -0300718 unsigned long fv;
719 struct pvr2_hdw *hdw = cptr->hdw;
720 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300721 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300722 }
Mike Isely644afdb2007-01-20 00:19:23 -0300723 fv = hdw->tuner_signal_info.rangelow;
724 if (!fv) {
725 /* Safety fallback */
726 *vp = TV_MIN_FREQ;
727 return 0;
728 }
729 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
730 fv = (fv * 125) / 2;
731 } else {
732 fv = fv * 62500;
733 }
734 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300735 return 0;
736}
737
Mike Iselyb30d2442006-06-25 20:05:01 -0300738static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
739{
740 return cptr->hdw->enc_stale != 0;
741}
742
743static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
744{
745 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300746 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300747}
748
749static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
750{
751 int ret;
752 struct v4l2_ext_controls cs;
753 struct v4l2_ext_control c1;
754 memset(&cs,0,sizeof(cs));
755 memset(&c1,0,sizeof(c1));
756 cs.controls = &c1;
757 cs.count = 1;
758 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300759 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300760 VIDIOC_G_EXT_CTRLS);
761 if (ret) return ret;
762 *vp = c1.value;
763 return 0;
764}
765
766static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
767{
768 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300769 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300770 struct v4l2_ext_controls cs;
771 struct v4l2_ext_control c1;
772 memset(&cs,0,sizeof(cs));
773 memset(&c1,0,sizeof(c1));
774 cs.controls = &c1;
775 cs.count = 1;
776 c1.id = cptr->info->v4l_id;
777 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300778 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
779 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300780 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300781 if (ret == -EBUSY) {
782 /* Oops. cx2341x is telling us it's not safe to change
783 this control while we're capturing. Make a note of this
784 fact so that the pipeline will be stopped the next time
785 controls are committed. Then go on ahead and store this
786 change anyway. */
787 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
788 0, &cs,
789 VIDIOC_S_EXT_CTRLS);
790 if (!ret) hdw->enc_unsafe_stale = !0;
791 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300792 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300793 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300794 return 0;
795}
796
797static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
798{
799 struct v4l2_queryctrl qctrl;
800 struct pvr2_ctl_info *info;
801 qctrl.id = cptr->info->v4l_id;
802 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
803 /* Strip out the const so we can adjust a function pointer. It's
804 OK to do this here because we know this is a dynamically created
805 control, so the underlying storage for the info pointer is (a)
806 private to us, and (b) not in read-only storage. Either we do
807 this or we significantly complicate the underlying control
808 implementation. */
809 info = (struct pvr2_ctl_info *)(cptr->info);
810 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
811 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300812 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300813 }
814 } else {
815 if (!(info->set_value)) {
816 info->set_value = ctrl_cx2341x_set;
817 }
818 }
819 return qctrl.flags;
820}
821
Mike Iselyd8554972006-06-26 20:58:46 -0300822static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
823{
Mike Isely681c7392007-11-26 01:48:52 -0300824 *vp = cptr->hdw->state_pipeline_req;
825 return 0;
826}
827
828static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
829{
830 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300831 return 0;
832}
833
834static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
835{
836 int result = pvr2_hdw_is_hsm(cptr->hdw);
837 *vp = PVR2_CVAL_HSM_FULL;
838 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
839 if (result) *vp = PVR2_CVAL_HSM_HIGH;
840 return 0;
841}
842
Mike Iselyac04d002012-02-20 02:28:56 -0300843static int ctrl_stddetect_get(struct pvr2_ctrl *cptr, int *vp)
844{
845 *vp = pvr2_hdw_get_detected_std(cptr->hdw);
846 return 0;
847}
848
Mike Iselyd8554972006-06-26 20:58:46 -0300849static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
850{
851 *vp = cptr->hdw->std_mask_avail;
852 return 0;
853}
854
855static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
856{
857 struct pvr2_hdw *hdw = cptr->hdw;
858 v4l2_std_id ns;
859 ns = hdw->std_mask_avail;
860 ns = (ns & ~m) | (v & m);
861 if (ns == hdw->std_mask_avail) return 0;
862 hdw->std_mask_avail = ns;
863 pvr2_hdw_internal_set_std_avail(hdw);
864 pvr2_hdw_internal_find_stdenum(hdw);
865 return 0;
866}
867
868static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
869 char *bufPtr,unsigned int bufSize,
870 unsigned int *len)
871{
872 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
873 return 0;
874}
875
876static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
877 const char *bufPtr,unsigned int bufSize,
878 int *mskp,int *valp)
879{
880 int ret;
881 v4l2_std_id id;
882 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
883 if (ret < 0) return ret;
884 if (mskp) *mskp = id;
885 if (valp) *valp = id;
886 return 0;
887}
888
889static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
890{
891 *vp = cptr->hdw->std_mask_cur;
892 return 0;
893}
894
895static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
896{
897 struct pvr2_hdw *hdw = cptr->hdw;
898 v4l2_std_id ns;
899 ns = hdw->std_mask_cur;
900 ns = (ns & ~m) | (v & m);
901 if (ns == hdw->std_mask_cur) return 0;
902 hdw->std_mask_cur = ns;
903 hdw->std_dirty = !0;
904 pvr2_hdw_internal_find_stdenum(hdw);
905 return 0;
906}
907
908static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
909{
910 return cptr->hdw->std_dirty != 0;
911}
912
913static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
914{
915 cptr->hdw->std_dirty = 0;
916}
917
918static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
919{
Mike Isely18103c572007-01-20 00:09:47 -0300920 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300921 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300922 *vp = hdw->tuner_signal_info.signal;
923 return 0;
924}
925
926static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
927{
928 int val = 0;
929 unsigned int subchan;
930 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300931 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300932 subchan = hdw->tuner_signal_info.rxsubchans;
933 if (subchan & V4L2_TUNER_SUB_MONO) {
934 val |= (1 << V4L2_TUNER_MODE_MONO);
935 }
936 if (subchan & V4L2_TUNER_SUB_STEREO) {
937 val |= (1 << V4L2_TUNER_MODE_STEREO);
938 }
939 if (subchan & V4L2_TUNER_SUB_LANG1) {
940 val |= (1 << V4L2_TUNER_MODE_LANG1);
941 }
942 if (subchan & V4L2_TUNER_SUB_LANG2) {
943 val |= (1 << V4L2_TUNER_MODE_LANG2);
944 }
945 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300946 return 0;
947}
948
Mike Iselyd8554972006-06-26 20:58:46 -0300949
950static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
951{
952 struct pvr2_hdw *hdw = cptr->hdw;
953 if (v < 0) return -EINVAL;
954 if (v > hdw->std_enum_cnt) return -EINVAL;
955 hdw->std_enum_cur = v;
956 if (!v) return 0;
957 v--;
958 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
959 hdw->std_mask_cur = hdw->std_defs[v].id;
960 hdw->std_dirty = !0;
961 return 0;
962}
963
964
965static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
966{
967 *vp = cptr->hdw->std_enum_cur;
968 return 0;
969}
970
971
972static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
973{
974 return cptr->hdw->std_dirty != 0;
975}
976
977
978static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
979{
980 cptr->hdw->std_dirty = 0;
981}
982
983
984#define DEFINT(vmin,vmax) \
985 .type = pvr2_ctl_int, \
986 .def.type_int.min_value = vmin, \
987 .def.type_int.max_value = vmax
988
989#define DEFENUM(tab) \
990 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300991 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300992 .def.type_enum.value_names = tab
993
Mike Isely33213962006-06-25 20:04:40 -0300994#define DEFBOOL \
995 .type = pvr2_ctl_bool
996
Mike Iselyd8554972006-06-26 20:58:46 -0300997#define DEFMASK(msk,tab) \
998 .type = pvr2_ctl_bitmask, \
999 .def.type_bitmask.valid_bits = msk, \
1000 .def.type_bitmask.bit_names = tab
1001
1002#define DEFREF(vname) \
1003 .set_value = ctrl_set_##vname, \
1004 .get_value = ctrl_get_##vname, \
1005 .is_dirty = ctrl_isdirty_##vname, \
1006 .clear_dirty = ctrl_cleardirty_##vname
1007
1008
1009#define VCREATE_FUNCS(vname) \
1010static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
1011{*vp = cptr->hdw->vname##_val; return 0;} \
1012static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
1013{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
1014static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
1015{return cptr->hdw->vname##_dirty != 0;} \
1016static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
1017{cptr->hdw->vname##_dirty = 0;}
1018
1019VCREATE_FUNCS(brightness)
1020VCREATE_FUNCS(contrast)
1021VCREATE_FUNCS(saturation)
1022VCREATE_FUNCS(hue)
1023VCREATE_FUNCS(volume)
1024VCREATE_FUNCS(balance)
1025VCREATE_FUNCS(bass)
1026VCREATE_FUNCS(treble)
1027VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001028VCREATE_FUNCS(cropl)
1029VCREATE_FUNCS(cropt)
1030VCREATE_FUNCS(cropw)
1031VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -03001032VCREATE_FUNCS(audiomode)
1033VCREATE_FUNCS(res_hor)
1034VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -03001035VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -03001036
Mike Iselyd8554972006-06-26 20:58:46 -03001037/* Table definition of all controls which can be manipulated */
1038static const struct pvr2_ctl_info control_defs[] = {
1039 {
1040 .v4l_id = V4L2_CID_BRIGHTNESS,
1041 .desc = "Brightness",
1042 .name = "brightness",
1043 .default_value = 128,
1044 DEFREF(brightness),
1045 DEFINT(0,255),
1046 },{
1047 .v4l_id = V4L2_CID_CONTRAST,
1048 .desc = "Contrast",
1049 .name = "contrast",
1050 .default_value = 68,
1051 DEFREF(contrast),
1052 DEFINT(0,127),
1053 },{
1054 .v4l_id = V4L2_CID_SATURATION,
1055 .desc = "Saturation",
1056 .name = "saturation",
1057 .default_value = 64,
1058 DEFREF(saturation),
1059 DEFINT(0,127),
1060 },{
1061 .v4l_id = V4L2_CID_HUE,
1062 .desc = "Hue",
1063 .name = "hue",
1064 .default_value = 0,
1065 DEFREF(hue),
1066 DEFINT(-128,127),
1067 },{
1068 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1069 .desc = "Volume",
1070 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001071 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001072 DEFREF(volume),
1073 DEFINT(0,65535),
1074 },{
1075 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1076 .desc = "Balance",
1077 .name = "balance",
1078 .default_value = 0,
1079 DEFREF(balance),
1080 DEFINT(-32768,32767),
1081 },{
1082 .v4l_id = V4L2_CID_AUDIO_BASS,
1083 .desc = "Bass",
1084 .name = "bass",
1085 .default_value = 0,
1086 DEFREF(bass),
1087 DEFINT(-32768,32767),
1088 },{
1089 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1090 .desc = "Treble",
1091 .name = "treble",
1092 .default_value = 0,
1093 DEFREF(treble),
1094 DEFINT(-32768,32767),
1095 },{
1096 .v4l_id = V4L2_CID_AUDIO_MUTE,
1097 .desc = "Mute",
1098 .name = "mute",
1099 .default_value = 0,
1100 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001101 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001102 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001103 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001104 .name = "crop_left",
1105 .internal_id = PVR2_CID_CROPL,
1106 .default_value = 0,
1107 DEFREF(cropl),
1108 DEFINT(-129, 340),
1109 .get_min_value = ctrl_cropl_min_get,
1110 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001111 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001112 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001113 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001114 .name = "crop_top",
1115 .internal_id = PVR2_CID_CROPT,
1116 .default_value = 0,
1117 DEFREF(cropt),
1118 DEFINT(-35, 544),
1119 .get_min_value = ctrl_cropt_min_get,
1120 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001121 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001122 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001123 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001124 .name = "crop_width",
1125 .internal_id = PVR2_CID_CROPW,
1126 .default_value = 720,
1127 DEFREF(cropw),
Mike Isely35fa5d42011-02-13 17:34:33 -03001128 DEFINT(0, 864),
Mike Isely432907f2008-08-31 21:02:20 -03001129 .get_max_value = ctrl_cropw_max_get,
1130 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001131 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001132 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001133 .name = "crop_height",
1134 .internal_id = PVR2_CID_CROPH,
1135 .default_value = 480,
1136 DEFREF(croph),
Mike Isely35fa5d42011-02-13 17:34:33 -03001137 DEFINT(0, 576),
Mike Isely432907f2008-08-31 21:02:20 -03001138 .get_max_value = ctrl_croph_max_get,
1139 .get_def_value = ctrl_get_cropcapdh,
1140 }, {
1141 .desc = "Capture capability pixel aspect numerator",
1142 .name = "cropcap_pixel_numerator",
1143 .internal_id = PVR2_CID_CROPCAPPAN,
1144 .get_value = ctrl_get_cropcappan,
1145 }, {
1146 .desc = "Capture capability pixel aspect denominator",
1147 .name = "cropcap_pixel_denominator",
1148 .internal_id = PVR2_CID_CROPCAPPAD,
1149 .get_value = ctrl_get_cropcappad,
1150 }, {
1151 .desc = "Capture capability bounds top",
1152 .name = "cropcap_bounds_top",
1153 .internal_id = PVR2_CID_CROPCAPBT,
1154 .get_value = ctrl_get_cropcapbt,
1155 }, {
1156 .desc = "Capture capability bounds left",
1157 .name = "cropcap_bounds_left",
1158 .internal_id = PVR2_CID_CROPCAPBL,
1159 .get_value = ctrl_get_cropcapbl,
1160 }, {
1161 .desc = "Capture capability bounds width",
1162 .name = "cropcap_bounds_width",
1163 .internal_id = PVR2_CID_CROPCAPBW,
1164 .get_value = ctrl_get_cropcapbw,
1165 }, {
1166 .desc = "Capture capability bounds height",
1167 .name = "cropcap_bounds_height",
1168 .internal_id = PVR2_CID_CROPCAPBH,
1169 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001170 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001171 .desc = "Video Source",
1172 .name = "input",
1173 .internal_id = PVR2_CID_INPUT,
1174 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001175 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001176 DEFREF(input),
1177 DEFENUM(control_values_input),
1178 },{
1179 .desc = "Audio Mode",
1180 .name = "audio_mode",
1181 .internal_id = PVR2_CID_AUDIOMODE,
1182 .default_value = V4L2_TUNER_MODE_STEREO,
1183 DEFREF(audiomode),
1184 DEFENUM(control_values_audiomode),
1185 },{
1186 .desc = "Horizontal capture resolution",
1187 .name = "resolution_hor",
1188 .internal_id = PVR2_CID_HRES,
1189 .default_value = 720,
1190 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001191 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001192 },{
1193 .desc = "Vertical capture resolution",
1194 .name = "resolution_ver",
1195 .internal_id = PVR2_CID_VRES,
1196 .default_value = 480,
1197 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001198 DEFINT(17,576),
1199 /* Hook in check for video standard and adjust maximum
1200 depending on the standard. */
1201 .get_max_value = ctrl_vres_max_get,
1202 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001203 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001204 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001205 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1206 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001207 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001208 DEFREF(srate),
1209 DEFENUM(control_values_srate),
1210 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001211 .desc = "Tuner Frequency (Hz)",
1212 .name = "frequency",
1213 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001214 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001215 .set_value = ctrl_freq_set,
1216 .get_value = ctrl_freq_get,
1217 .is_dirty = ctrl_freq_is_dirty,
1218 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001219 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001220 /* Hook in check for input value (tv/radio) and adjust
1221 max/min values accordingly */
1222 .get_max_value = ctrl_freq_max_get,
1223 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001224 },{
1225 .desc = "Channel",
1226 .name = "channel",
1227 .set_value = ctrl_channel_set,
1228 .get_value = ctrl_channel_get,
1229 DEFINT(0,FREQTABLE_SIZE),
1230 },{
1231 .desc = "Channel Program Frequency",
1232 .name = "freq_table_value",
1233 .set_value = ctrl_channelfreq_set,
1234 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001235 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001236 /* Hook in check for input value (tv/radio) and adjust
1237 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001238 .get_max_value = ctrl_freq_max_get,
1239 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001240 },{
1241 .desc = "Channel Program ID",
1242 .name = "freq_table_channel",
1243 .set_value = ctrl_channelprog_set,
1244 .get_value = ctrl_channelprog_get,
1245 DEFINT(0,FREQTABLE_SIZE),
1246 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001247 .desc = "Streaming Enabled",
1248 .name = "streaming_enabled",
1249 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001250 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001251 },{
1252 .desc = "USB Speed",
1253 .name = "usb_speed",
1254 .get_value = ctrl_hsm_get,
1255 DEFENUM(control_values_hsm),
1256 },{
Mike Isely681c7392007-11-26 01:48:52 -03001257 .desc = "Master State",
1258 .name = "master_state",
1259 .get_value = ctrl_masterstate_get,
1260 DEFENUM(pvr2_state_names),
1261 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001262 .desc = "Signal Present",
1263 .name = "signal_present",
1264 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001265 DEFINT(0,65535),
1266 },{
1267 .desc = "Audio Modes Present",
1268 .name = "audio_modes_present",
1269 .get_value = ctrl_audio_modes_present_get,
1270 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1271 v4l. Nothing outside of this module cares about this,
1272 but I reuse it in order to also reuse the
1273 control_values_audiomode string table. */
1274 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1275 (1 << V4L2_TUNER_MODE_STEREO)|
1276 (1 << V4L2_TUNER_MODE_LANG1)|
1277 (1 << V4L2_TUNER_MODE_LANG2)),
1278 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001279 },{
1280 .desc = "Video Standards Available Mask",
1281 .name = "video_standard_mask_available",
1282 .internal_id = PVR2_CID_STDAVAIL,
1283 .skip_init = !0,
1284 .get_value = ctrl_stdavail_get,
1285 .set_value = ctrl_stdavail_set,
1286 .val_to_sym = ctrl_std_val_to_sym,
1287 .sym_to_val = ctrl_std_sym_to_val,
1288 .type = pvr2_ctl_bitmask,
1289 },{
1290 .desc = "Video Standards In Use Mask",
1291 .name = "video_standard_mask_active",
1292 .internal_id = PVR2_CID_STDCUR,
1293 .skip_init = !0,
1294 .get_value = ctrl_stdcur_get,
1295 .set_value = ctrl_stdcur_set,
1296 .is_dirty = ctrl_stdcur_is_dirty,
1297 .clear_dirty = ctrl_stdcur_clear_dirty,
1298 .val_to_sym = ctrl_std_val_to_sym,
1299 .sym_to_val = ctrl_std_sym_to_val,
1300 .type = pvr2_ctl_bitmask,
1301 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001302 .desc = "Video Standard Name",
1303 .name = "video_standard",
1304 .internal_id = PVR2_CID_STDENUM,
1305 .skip_init = !0,
1306 .get_value = ctrl_stdenumcur_get,
1307 .set_value = ctrl_stdenumcur_set,
1308 .is_dirty = ctrl_stdenumcur_is_dirty,
1309 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1310 .type = pvr2_ctl_enum,
Mike Iselyac04d002012-02-20 02:28:56 -03001311 },{
1312 .desc = "Video Standards Detected Mask",
1313 .name = "video_standard_mask_detected",
1314 .internal_id = PVR2_CID_STDDETECT,
1315 .skip_init = !0,
1316 .get_value = ctrl_stddetect_get,
1317 .val_to_sym = ctrl_std_val_to_sym,
1318 .sym_to_val = ctrl_std_sym_to_val,
1319 .type = pvr2_ctl_bitmask,
Mike Iselyd8554972006-06-26 20:58:46 -03001320 }
1321};
1322
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001323#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001324
1325
1326const char *pvr2_config_get_name(enum pvr2_config cfg)
1327{
1328 switch (cfg) {
1329 case pvr2_config_empty: return "empty";
1330 case pvr2_config_mpeg: return "mpeg";
1331 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001332 case pvr2_config_pcm: return "pcm";
1333 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001334 }
1335 return "<unknown>";
1336}
1337
1338
1339struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1340{
1341 return hdw->usb_dev;
1342}
1343
1344
1345unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1346{
1347 return hdw->serial_number;
1348}
1349
Mike Isely31a18542007-04-08 01:11:47 -03001350
1351const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1352{
1353 return hdw->bus_info;
1354}
1355
1356
Mike Isely13a88792009-01-14 04:22:56 -03001357const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1358{
1359 return hdw->identifier;
1360}
1361
1362
Mike Isely1bde0282006-12-27 23:30:13 -03001363unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1364{
1365 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1366}
1367
1368/* Set the currently tuned frequency and account for all possible
1369 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001370static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001371{
Mike Isely7c74e572007-01-20 00:15:41 -03001372 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001373 if (hdw->freqSelector) {
1374 /* Swing over to radio frequency selection */
1375 hdw->freqSelector = 0;
1376 hdw->freqDirty = !0;
1377 }
Mike Isely1bde0282006-12-27 23:30:13 -03001378 if (hdw->freqValRadio != val) {
1379 hdw->freqValRadio = val;
1380 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001381 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001382 }
Mike Isely7c74e572007-01-20 00:15:41 -03001383 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001384 if (!(hdw->freqSelector)) {
1385 /* Swing over to television frequency selection */
1386 hdw->freqSelector = 1;
1387 hdw->freqDirty = !0;
1388 }
Mike Isely1bde0282006-12-27 23:30:13 -03001389 if (hdw->freqValTelevision != val) {
1390 hdw->freqValTelevision = val;
1391 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001392 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001393 }
Mike Isely1bde0282006-12-27 23:30:13 -03001394 }
1395}
1396
Mike Iselyd8554972006-06-26 20:58:46 -03001397int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1398{
1399 return hdw->unit_number;
1400}
1401
1402
1403/* Attempt to locate one of the given set of files. Messages are logged
1404 appropriate to what has been found. The return value will be 0 or
1405 greater on success (it will be the index of the file name found) and
1406 fw_entry will be filled in. Otherwise a negative error is returned on
1407 failure. If the return value is -ENOENT then no viable firmware file
1408 could be located. */
1409static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1410 const struct firmware **fw_entry,
1411 const char *fwtypename,
1412 unsigned int fwcount,
1413 const char *fwnames[])
1414{
1415 unsigned int idx;
1416 int ret = -EINVAL;
1417 for (idx = 0; idx < fwcount; idx++) {
1418 ret = request_firmware(fw_entry,
1419 fwnames[idx],
1420 &hdw->usb_dev->dev);
1421 if (!ret) {
1422 trace_firmware("Located %s firmware: %s;"
1423 " uploading...",
1424 fwtypename,
1425 fwnames[idx]);
1426 return idx;
1427 }
1428 if (ret == -ENOENT) continue;
1429 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1430 "request_firmware fatal error with code=%d",ret);
1431 return ret;
1432 }
1433 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1434 "***WARNING***"
1435 " Device %s firmware"
1436 " seems to be missing.",
1437 fwtypename);
1438 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1439 "Did you install the pvrusb2 firmware files"
1440 " in their proper location?");
1441 if (fwcount == 1) {
1442 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1443 "request_firmware unable to locate %s file %s",
1444 fwtypename,fwnames[0]);
1445 } else {
1446 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1447 "request_firmware unable to locate"
1448 " one of the following %s files:",
1449 fwtypename);
1450 for (idx = 0; idx < fwcount; idx++) {
1451 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1452 "request_firmware: Failed to find %s",
1453 fwnames[idx]);
1454 }
1455 }
1456 return ret;
1457}
1458
1459
1460/*
1461 * pvr2_upload_firmware1().
1462 *
1463 * Send the 8051 firmware to the device. After the upload, arrange for
1464 * device to re-enumerate.
1465 *
1466 * NOTE : the pointer to the firmware data given by request_firmware()
1467 * is not suitable for an usb transaction.
1468 *
1469 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001470static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001471{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001472 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001473 void *fw_ptr;
1474 unsigned int pipe;
Mike Isely9081d902009-11-25 02:59:34 -03001475 unsigned int fwsize;
Mike Iselyd8554972006-06-26 20:58:46 -03001476 int ret;
1477 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001478
Mike Isely989eb152007-11-26 01:53:12 -03001479 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001480 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001481 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1482 "Connected device type defines"
1483 " no firmware to upload; ignoring firmware");
1484 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001485 }
1486
Mike Iselyd8554972006-06-26 20:58:46 -03001487 hdw->fw1_state = FW1_STATE_FAILED; // default result
1488
1489 trace_firmware("pvr2_upload_firmware1");
1490
1491 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001492 hdw->hdw_desc->fx2_firmware.cnt,
1493 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001494 if (ret < 0) {
1495 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1496 return ret;
1497 }
1498
Mike Iselyd8554972006-06-26 20:58:46 -03001499 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1500
1501 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
Mike Isely9081d902009-11-25 02:59:34 -03001502 fwsize = fw_entry->size;
Mike Iselyd8554972006-06-26 20:58:46 -03001503
Mike Isely9081d902009-11-25 02:59:34 -03001504 if ((fwsize != 0x2000) &&
1505 (!(hdw->hdw_desc->flag_fx2_16kb && (fwsize == 0x4000)))) {
Mike Iselyc21c2db2009-11-25 02:49:21 -03001506 if (hdw->hdw_desc->flag_fx2_16kb) {
1507 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1508 "Wrong fx2 firmware size"
1509 " (expected 8192 or 16384, got %u)",
Mike Isely9081d902009-11-25 02:59:34 -03001510 fwsize);
Mike Iselyc21c2db2009-11-25 02:49:21 -03001511 } else {
1512 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1513 "Wrong fx2 firmware size"
1514 " (expected 8192, got %u)",
Mike Isely9081d902009-11-25 02:59:34 -03001515 fwsize);
Mike Iselyc21c2db2009-11-25 02:49:21 -03001516 }
Mike Iselyd8554972006-06-26 20:58:46 -03001517 release_firmware(fw_entry);
1518 return -ENOMEM;
1519 }
1520
1521 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1522 if (fw_ptr == NULL){
1523 release_firmware(fw_entry);
1524 return -ENOMEM;
1525 }
1526
1527 /* We have to hold the CPU during firmware upload. */
1528 pvr2_hdw_cpureset_assert(hdw,1);
1529
1530 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1531 chunk. */
1532
1533 ret = 0;
Mike Isely9081d902009-11-25 02:59:34 -03001534 for (address = 0; address < fwsize; address += 0x800) {
Mike Iselyd8554972006-06-26 20:58:46 -03001535 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1536 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1537 0, fw_ptr, 0x800, HZ);
1538 }
1539
1540 trace_firmware("Upload done, releasing device's CPU");
1541
1542 /* Now release the CPU. It will disconnect and reconnect later. */
1543 pvr2_hdw_cpureset_assert(hdw,0);
1544
1545 kfree(fw_ptr);
1546 release_firmware(fw_entry);
1547
1548 trace_firmware("Upload done (%d bytes sent)",ret);
1549
Gary Francis75727462009-11-25 03:03:31 -03001550 /* We should have written fwsize bytes */
1551 if (ret == fwsize) {
Mike Iselyd8554972006-06-26 20:58:46 -03001552 hdw->fw1_state = FW1_STATE_RELOAD;
1553 return 0;
1554 }
1555
1556 return -EIO;
1557}
1558
1559
1560/*
1561 * pvr2_upload_firmware2()
1562 *
1563 * This uploads encoder firmware on endpoint 2.
1564 *
1565 */
1566
1567int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1568{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001569 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001570 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001571 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001572 int actual_length;
1573 int ret = 0;
1574 int fwidx;
1575 static const char *fw_files[] = {
1576 CX2341X_FIRM_ENC_FILENAME,
1577 };
1578
Mike Isely989eb152007-11-26 01:53:12 -03001579 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001580 return 0;
1581 }
1582
Mike Iselyd8554972006-06-26 20:58:46 -03001583 trace_firmware("pvr2_upload_firmware2");
1584
1585 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001586 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001587 if (ret < 0) return ret;
1588 fwidx = ret;
1589 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001590 /* Since we're about to completely reinitialize the encoder,
1591 invalidate our cached copy of its configuration state. Next
1592 time we configure the encoder, then we'll fully configure it. */
1593 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001594
Mike Iselyd913d632008-04-06 04:04:35 -03001595 /* Encoder is about to be reset so note that as far as we're
1596 concerned now, the encoder has never been run. */
1597 del_timer_sync(&hdw->encoder_run_timer);
1598 if (hdw->state_encoder_runok) {
1599 hdw->state_encoder_runok = 0;
1600 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1601 }
1602
Mike Iselyd8554972006-06-26 20:58:46 -03001603 /* First prepare firmware loading */
1604 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1605 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1606 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1607 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1608 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1609 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1610 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1611 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1612 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1613 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1614 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1615 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1616 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1617 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1618 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1619 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001620 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1621 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001622
1623 if (ret) {
1624 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1625 "firmware2 upload prep failed, ret=%d",ret);
1626 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001627 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001628 }
1629
1630 /* Now send firmware */
1631
1632 fw_len = fw_entry->size;
1633
Mike Isely90060d32007-02-08 02:02:53 -03001634 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001635 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1636 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001637 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001638 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001639 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001640 ret = -EINVAL;
1641 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001642 }
1643
1644 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1645 if (fw_ptr == NULL){
1646 release_firmware(fw_entry);
1647 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1648 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001649 ret = -ENOMEM;
1650 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001651 }
1652
1653 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1654
Mike Isely90060d32007-02-08 02:02:53 -03001655 fw_done = 0;
1656 for (fw_done = 0; fw_done < fw_len;) {
1657 bcnt = fw_len - fw_done;
1658 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1659 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1660 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001661 /* Some background info: The data being swapped here is a
1662 firmware image destined for the mpeg encoder chip that
1663 lives at the other end of a USB endpoint. The encoder
1664 chip always talks in 32 bit chunks and its storage is
1665 organized into 32 bit words. However from the file
1666 system to the encoder chip everything is purely a byte
1667 stream. The firmware file's contents are always 32 bit
1668 swapped from what the encoder expects. Thus the need
1669 always exists to swap the bytes regardless of the endian
1670 type of the host processor and therefore swab32() makes
1671 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001672 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001673 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001674
Mike Isely90060d32007-02-08 02:02:53 -03001675 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001676 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001677 ret |= (actual_length != bcnt);
1678 if (ret) break;
1679 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001680 }
1681
1682 trace_firmware("upload of %s : %i / %i ",
1683 fw_files[fwidx],fw_done,fw_len);
1684
1685 kfree(fw_ptr);
1686 release_firmware(fw_entry);
1687
1688 if (ret) {
1689 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1690 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001691 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001692 }
1693
1694 /* Finish upload */
1695
1696 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1697 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001698 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001699
1700 if (ret) {
1701 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1702 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001703 }
Mike Isely21684ba2008-04-21 03:49:33 -03001704
1705 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001706 if (hdw->hdw_desc->signal_routing_scheme ==
1707 PVR2_ROUTING_SCHEME_GOTVIEW) {
1708 /* Ensure that GPIO 11 is set to output for GOTVIEW
1709 hardware. */
1710 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1711 }
Mike Iselyd8554972006-06-26 20:58:46 -03001712 return ret;
1713}
1714
1715
Mike Isely681c7392007-11-26 01:48:52 -03001716static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001717{
Mike Isely681c7392007-11-26 01:48:52 -03001718 if (st < ARRAY_SIZE(pvr2_state_names)) {
1719 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001720 }
Mike Isely681c7392007-11-26 01:48:52 -03001721 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001722}
1723
Mike Isely681c7392007-11-26 01:48:52 -03001724static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001725{
Mike Iselyaf78e162009-03-07 00:21:30 -03001726 /* Even though we really only care about the video decoder chip at
1727 this point, we'll broadcast stream on/off to all sub-devices
1728 anyway, just in case somebody else wants to hear the
1729 command... */
Mike Iselye2605082009-03-07 01:50:48 -03001730 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 stream=%s",
1731 (enablefl ? "on" : "off"));
Mike Iselyaf78e162009-03-07 00:21:30 -03001732 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_stream, enablefl);
Andy Walls3ccc6462009-12-24 13:06:08 -03001733 v4l2_device_call_all(&hdw->v4l2_dev, 0, audio, s_stream, enablefl);
Mike Iselyaf78e162009-03-07 00:21:30 -03001734 if (hdw->decoder_client_id) {
1735 /* We get here if the encoder has been noticed. Otherwise
1736 we'll issue a warning to the user (which should
1737 normally never happen). */
1738 return 0;
1739 }
1740 if (!hdw->flag_decoder_missed) {
1741 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1742 "WARNING: No decoder present");
1743 hdw->flag_decoder_missed = !0;
1744 trace_stbit("flag_decoder_missed",
1745 hdw->flag_decoder_missed);
1746 }
1747 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001748}
1749
1750
Mike Isely681c7392007-11-26 01:48:52 -03001751int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1752{
1753 return hdw->master_state;
1754}
1755
1756
1757static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1758{
1759 if (!hdw->flag_tripped) return 0;
1760 hdw->flag_tripped = 0;
1761 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1762 "Clearing driver error statuss");
1763 return !0;
1764}
1765
1766
1767int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1768{
1769 int fl;
1770 LOCK_TAKE(hdw->big_lock); do {
1771 fl = pvr2_hdw_untrip_unlocked(hdw);
1772 } while (0); LOCK_GIVE(hdw->big_lock);
1773 if (fl) pvr2_hdw_state_sched(hdw);
1774 return 0;
1775}
1776
1777
Mike Isely681c7392007-11-26 01:48:52 -03001778
1779
Mike Iselyd8554972006-06-26 20:58:46 -03001780int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1781{
Mike Isely681c7392007-11-26 01:48:52 -03001782 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001783}
1784
1785
1786int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1787{
Mike Isely681c7392007-11-26 01:48:52 -03001788 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001789 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001790 pvr2_hdw_untrip_unlocked(hdw);
1791 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1792 hdw->state_pipeline_req = enable_flag != 0;
1793 pvr2_trace(PVR2_TRACE_START_STOP,
1794 "/*--TRACE_STREAM--*/ %s",
1795 enable_flag ? "enable" : "disable");
1796 }
1797 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001798 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001799 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1800 if (enable_flag) {
1801 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1802 if (st != PVR2_STATE_READY) return -EIO;
1803 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1804 }
1805 }
Mike Iselyd8554972006-06-26 20:58:46 -03001806 return 0;
1807}
1808
1809
1810int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1811{
Mike Isely681c7392007-11-26 01:48:52 -03001812 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001813 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001814 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1815 hdw->desired_stream_type = config;
1816 hdw->state_pipeline_config = 0;
1817 trace_stbit("state_pipeline_config",
1818 hdw->state_pipeline_config);
1819 pvr2_hdw_state_sched(hdw);
1820 }
Mike Iselyd8554972006-06-26 20:58:46 -03001821 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001822 if (fl) return 0;
1823 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001824}
1825
1826
1827static int get_default_tuner_type(struct pvr2_hdw *hdw)
1828{
1829 int unit_number = hdw->unit_number;
1830 int tp = -1;
1831 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1832 tp = tuner[unit_number];
1833 }
1834 if (tp < 0) return -EINVAL;
1835 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001836 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001837 return 0;
1838}
1839
1840
1841static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1842{
1843 int unit_number = hdw->unit_number;
1844 int tp = 0;
1845 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1846 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001847 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001848 }
Mike Isely6a540252007-12-02 23:51:34 -03001849 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001850}
1851
1852
1853static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1854{
1855 int unit_number = hdw->unit_number;
1856 int tp = 0;
1857 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1858 tp = tolerance[unit_number];
1859 }
1860 return tp;
1861}
1862
1863
1864static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1865{
1866 /* Try a harmless request to fetch the eeprom's address over
1867 endpoint 1. See what happens. Only the full FX2 image can
1868 respond to this. If this probe fails then likely the FX2
1869 firmware needs be loaded. */
1870 int result;
1871 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001872 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001873 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1874 hdw->cmd_buffer,1,
1875 hdw->cmd_buffer,1);
1876 if (result < 0) break;
1877 } while(0); LOCK_GIVE(hdw->ctl_lock);
1878 if (result) {
1879 pvr2_trace(PVR2_TRACE_INIT,
1880 "Probe of device endpoint 1 result status %d",
1881 result);
1882 } else {
1883 pvr2_trace(PVR2_TRACE_INIT,
1884 "Probe of device endpoint 1 succeeded");
1885 }
1886 return result == 0;
1887}
1888
Mike Isely9f66d4e2007-09-08 22:28:51 -03001889struct pvr2_std_hack {
1890 v4l2_std_id pat; /* Pattern to match */
1891 v4l2_std_id msk; /* Which bits we care about */
1892 v4l2_std_id std; /* What additional standards or default to set */
1893};
1894
1895/* This data structure labels specific combinations of standards from
1896 tveeprom that we'll try to recognize. If we recognize one, then assume
1897 a specified default standard to use. This is here because tveeprom only
1898 tells us about available standards not the intended default standard (if
1899 any) for the device in question. We guess the default based on what has
1900 been reported as available. Note that this is only for guessing a
1901 default - which can always be overridden explicitly - and if the user
1902 has otherwise named a default then that default will always be used in
1903 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001904static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001905 { /* PAL(B/G) */
1906 .pat = V4L2_STD_B|V4L2_STD_GH,
1907 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1908 },
1909 { /* NTSC(M) */
1910 .pat = V4L2_STD_MN,
1911 .std = V4L2_STD_NTSC_M,
1912 },
1913 { /* PAL(I) */
1914 .pat = V4L2_STD_PAL_I,
1915 .std = V4L2_STD_PAL_I,
1916 },
1917 { /* SECAM(L/L') */
1918 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1919 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1920 },
1921 { /* PAL(D/D1/K) */
1922 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001923 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001924 },
1925};
1926
Mike Iselyd8554972006-06-26 20:58:46 -03001927static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1928{
1929 char buf[40];
1930 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001931 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001932
1933 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001934 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001935
1936 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001937 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001938 "Supported video standard(s) reported available"
1939 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001940 bcnt,buf);
1941
1942 hdw->std_mask_avail = hdw->std_mask_eeprom;
1943
Mike Isely3d290bd2007-12-03 01:47:12 -03001944 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001945 if (std2) {
1946 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001947 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001948 "Expanding supported video standards"
1949 " to include: %.*s",
1950 bcnt,buf);
1951 hdw->std_mask_avail |= std2;
1952 }
1953
1954 pvr2_hdw_internal_set_std_avail(hdw);
1955
1956 if (std1) {
1957 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001958 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001959 "Initial video standard forced to %.*s",
1960 bcnt,buf);
1961 hdw->std_mask_cur = std1;
1962 hdw->std_dirty = !0;
1963 pvr2_hdw_internal_find_stdenum(hdw);
1964 return;
1965 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001966 if (std3) {
1967 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1968 pvr2_trace(PVR2_TRACE_STD,
1969 "Initial video standard"
1970 " (determined by device type): %.*s",bcnt,buf);
1971 hdw->std_mask_cur = std3;
1972 hdw->std_dirty = !0;
1973 pvr2_hdw_internal_find_stdenum(hdw);
1974 return;
1975 }
Mike Iselyd8554972006-06-26 20:58:46 -03001976
Mike Isely9f66d4e2007-09-08 22:28:51 -03001977 {
1978 unsigned int idx;
1979 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1980 if (std_eeprom_maps[idx].msk ?
1981 ((std_eeprom_maps[idx].pat ^
1982 hdw->std_mask_eeprom) &
1983 std_eeprom_maps[idx].msk) :
1984 (std_eeprom_maps[idx].pat !=
1985 hdw->std_mask_eeprom)) continue;
1986 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1987 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001988 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001989 "Initial video standard guessed as %.*s",
1990 bcnt,buf);
1991 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1992 hdw->std_dirty = !0;
1993 pvr2_hdw_internal_find_stdenum(hdw);
1994 return;
1995 }
1996 }
1997
Mike Iselyd8554972006-06-26 20:58:46 -03001998 if (hdw->std_enum_cnt > 1) {
1999 // Autoselect the first listed standard
2000 hdw->std_enum_cur = 1;
2001 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
2002 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03002003 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03002004 "Initial video standard auto-selected to %s",
2005 hdw->std_defs[hdw->std_enum_cur-1].name);
2006 return;
2007 }
2008
Mike Isely0885ba12006-06-25 21:30:47 -03002009 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03002010 "Unable to select a viable initial video standard");
2011}
2012
2013
Mike Iselye9c64a72009-03-06 23:42:20 -03002014static unsigned int pvr2_copy_i2c_addr_list(
2015 unsigned short *dst, const unsigned char *src,
2016 unsigned int dst_max)
2017{
Mike Isely3ab8d292009-03-07 01:37:58 -03002018 unsigned int cnt = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002019 if (!src) return 0;
2020 while (src[cnt] && (cnt + 1) < dst_max) {
2021 dst[cnt] = src[cnt];
2022 cnt++;
2023 }
2024 dst[cnt] = I2C_CLIENT_END;
2025 return cnt;
2026}
2027
2028
Mike Iselye17d7872009-06-20 14:45:52 -03002029static void pvr2_hdw_cx25840_vbi_hack(struct pvr2_hdw *hdw)
2030{
2031 /*
2032 Mike Isely <isely@pobox.com> 19-Nov-2006 - This bit of nuttiness
2033 for cx25840 causes that module to correctly set up its video
2034 scaling. This is really a problem in the cx25840 module itself,
2035 but we work around it here. The problem has not been seen in
2036 ivtv because there VBI is supported and set up. We don't do VBI
2037 here (at least not yet) and thus we never attempted to even set
2038 it up.
2039 */
2040 struct v4l2_format fmt;
2041 if (hdw->decoder_client_id != PVR2_CLIENT_ID_CX25840) {
2042 /* We're not using a cx25840 so don't enable the hack */
2043 return;
2044 }
2045
2046 pvr2_trace(PVR2_TRACE_INIT,
2047 "Module ID %u:"
2048 " Executing cx25840 VBI hack",
2049 hdw->decoder_client_id);
2050 memset(&fmt, 0, sizeof(fmt));
2051 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
Mike Isely058caa82011-02-13 17:53:07 -03002052 fmt.fmt.sliced.service_lines[0][21] = V4L2_SLICED_CAPTION_525;
2053 fmt.fmt.sliced.service_lines[1][21] = V4L2_SLICED_CAPTION_525;
Mike Iselye17d7872009-06-20 14:45:52 -03002054 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
Hans Verkuil09419af2010-03-14 12:27:48 -03002055 vbi, s_sliced_fmt, &fmt.fmt.sliced);
Mike Iselye17d7872009-06-20 14:45:52 -03002056}
2057
2058
Mike Isely1ab5e742009-03-07 00:24:24 -03002059static int pvr2_hdw_load_subdev(struct pvr2_hdw *hdw,
2060 const struct pvr2_device_client_desc *cd)
Mike Iselye9c64a72009-03-06 23:42:20 -03002061{
2062 const char *fname;
2063 unsigned char mid;
2064 struct v4l2_subdev *sd;
2065 unsigned int i2ccnt;
2066 const unsigned char *p;
2067 /* Arbitrary count - max # i2c addresses we will probe */
2068 unsigned short i2caddr[25];
2069
2070 mid = cd->module_id;
2071 fname = (mid < ARRAY_SIZE(module_names)) ? module_names[mid] : NULL;
2072 if (!fname) {
2073 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely27108142009-10-12 00:21:20 -03002074 "Module ID %u for device %s has no name?"
2075 " The driver might have a configuration problem.",
Mike Iselye9c64a72009-03-06 23:42:20 -03002076 mid,
2077 hdw->hdw_desc->description);
Mike Isely1ab5e742009-03-07 00:24:24 -03002078 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002079 }
Mike Iselybd14d4f2009-03-07 00:56:52 -03002080 pvr2_trace(PVR2_TRACE_INIT,
2081 "Module ID %u (%s) for device %s being loaded...",
2082 mid, fname,
2083 hdw->hdw_desc->description);
Mike Iselye9c64a72009-03-06 23:42:20 -03002084
2085 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, cd->i2c_address_list,
2086 ARRAY_SIZE(i2caddr));
2087 if (!i2ccnt && ((p = (mid < ARRAY_SIZE(module_i2c_addresses)) ?
2088 module_i2c_addresses[mid] : NULL) != NULL)) {
2089 /* Second chance: Try default i2c address list */
2090 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, p,
2091 ARRAY_SIZE(i2caddr));
Mike Iselybd14d4f2009-03-07 00:56:52 -03002092 if (i2ccnt) {
2093 pvr2_trace(PVR2_TRACE_INIT,
2094 "Module ID %u:"
2095 " Using default i2c address list",
2096 mid);
2097 }
Mike Iselye9c64a72009-03-06 23:42:20 -03002098 }
2099
2100 if (!i2ccnt) {
2101 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002102 "Module ID %u (%s) for device %s:"
Mike Isely27108142009-10-12 00:21:20 -03002103 " No i2c addresses."
2104 " The driver might have a configuration problem.",
Mike Isely1ab5e742009-03-07 00:24:24 -03002105 mid, fname, hdw->hdw_desc->description);
2106 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002107 }
2108
Mike Iselye9c64a72009-03-06 23:42:20 -03002109 if (i2ccnt == 1) {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002110 pvr2_trace(PVR2_TRACE_INIT,
2111 "Module ID %u:"
2112 " Setting up with specified i2c address 0x%x",
2113 mid, i2caddr[0]);
Hans Verkuile6574f22009-04-01 03:57:53 -03002114 sd = v4l2_i2c_new_subdev(&hdw->v4l2_dev, &hdw->i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03002115 fname, i2caddr[0], NULL);
Mike Iselye9c64a72009-03-06 23:42:20 -03002116 } else {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002117 pvr2_trace(PVR2_TRACE_INIT,
2118 "Module ID %u:"
2119 " Setting up with address probe list",
2120 mid);
Hans Verkuil53dacb12009-08-10 02:49:08 -03002121 sd = v4l2_i2c_new_subdev(&hdw->v4l2_dev, &hdw->i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03002122 fname, 0, i2caddr);
Mike Iselye9c64a72009-03-06 23:42:20 -03002123 }
2124
Mike Isely446dfdc2009-03-06 23:58:15 -03002125 if (!sd) {
2126 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely27108142009-10-12 00:21:20 -03002127 "Module ID %u (%s) for device %s failed to load."
2128 " Possible missing sub-device kernel module or"
2129 " initialization failure within module.",
Mike Isely1ab5e742009-03-07 00:24:24 -03002130 mid, fname, hdw->hdw_desc->description);
2131 return -EIO;
Mike Isely446dfdc2009-03-06 23:58:15 -03002132 }
2133
2134 /* Tag this sub-device instance with the module ID we know about.
2135 In other places we'll use that tag to determine if the instance
2136 requires special handling. */
2137 sd->grp_id = mid;
2138
Mike Iselybd14d4f2009-03-07 00:56:52 -03002139 pvr2_trace(PVR2_TRACE_INFO, "Attached sub-driver %s", fname);
Mike Iselya932f502009-03-06 23:47:10 -03002140
Mike Iselye9c64a72009-03-06 23:42:20 -03002141
Mike Isely00e5f732009-03-07 00:17:11 -03002142 /* client-specific setup... */
2143 switch (mid) {
2144 case PVR2_CLIENT_ID_CX25840:
Mike Isely00e5f732009-03-07 00:17:11 -03002145 case PVR2_CLIENT_ID_SAA7115:
2146 hdw->decoder_client_id = mid;
2147 break;
2148 default: break;
2149 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002150
2151 return 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002152}
2153
2154
2155static void pvr2_hdw_load_modules(struct pvr2_hdw *hdw)
2156{
2157 unsigned int idx;
2158 const struct pvr2_string_table *cm;
2159 const struct pvr2_device_client_table *ct;
Mike Isely1ab5e742009-03-07 00:24:24 -03002160 int okFl = !0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002161
2162 cm = &hdw->hdw_desc->client_modules;
2163 for (idx = 0; idx < cm->cnt; idx++) {
2164 request_module(cm->lst[idx]);
2165 }
2166
2167 ct = &hdw->hdw_desc->client_table;
2168 for (idx = 0; idx < ct->cnt; idx++) {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002169 if (pvr2_hdw_load_subdev(hdw, &ct->lst[idx]) < 0) okFl = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002170 }
Mike Isely27108142009-10-12 00:21:20 -03002171 if (!okFl) {
2172 hdw->flag_modulefail = !0;
2173 pvr2_hdw_render_useless(hdw);
2174 }
Mike Iselye9c64a72009-03-06 23:42:20 -03002175}
2176
2177
Mike Iselyd8554972006-06-26 20:58:46 -03002178static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
2179{
2180 int ret;
2181 unsigned int idx;
2182 struct pvr2_ctrl *cptr;
2183 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03002184 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03002185 if (!reloadFl) {
2186 reloadFl =
2187 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
2188 == 0);
2189 if (reloadFl) {
2190 pvr2_trace(PVR2_TRACE_INIT,
2191 "USB endpoint config looks strange"
2192 "; possibly firmware needs to be"
2193 " loaded");
2194 }
2195 }
2196 if (!reloadFl) {
2197 reloadFl = !pvr2_hdw_check_firmware(hdw);
2198 if (reloadFl) {
2199 pvr2_trace(PVR2_TRACE_INIT,
2200 "Check for FX2 firmware failed"
2201 "; possibly firmware needs to be"
2202 " loaded");
2203 }
2204 }
Mike Iselyd8554972006-06-26 20:58:46 -03002205 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03002206 if (pvr2_upload_firmware1(hdw) != 0) {
2207 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2208 "Failure uploading firmware1");
2209 }
2210 return;
Mike Iselyd8554972006-06-26 20:58:46 -03002211 }
2212 }
Mike Iselyd8554972006-06-26 20:58:46 -03002213 hdw->fw1_state = FW1_STATE_OK;
2214
Mike Iselyd8554972006-06-26 20:58:46 -03002215 if (!pvr2_hdw_dev_ok(hdw)) return;
2216
Mike Isely27764722009-03-07 01:57:25 -03002217 hdw->force_dirty = !0;
2218
Mike Isely989eb152007-11-26 01:53:12 -03002219 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002220 pvr2_hdw_cmd_powerup(hdw);
2221 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002222 }
2223
Mike Isely31335b12008-07-25 19:35:31 -03002224 /* Take the IR chip out of reset, if appropriate */
Mike Isely27eab382009-04-06 01:51:38 -03002225 if (hdw->ir_scheme_active == PVR2_IR_SCHEME_ZILOG) {
Mike Isely31335b12008-07-25 19:35:31 -03002226 pvr2_issue_simple_cmd(hdw,
2227 FX2CMD_HCW_ZILOG_RESET |
2228 (1 << 8) |
2229 ((0) << 16));
2230 }
2231
Mike Iselyd8554972006-06-26 20:58:46 -03002232 // This step MUST happen after the earlier powerup step.
2233 pvr2_i2c_core_init(hdw);
2234 if (!pvr2_hdw_dev_ok(hdw)) return;
2235
Mike Iselye9c64a72009-03-06 23:42:20 -03002236 pvr2_hdw_load_modules(hdw);
Mike Isely1ab5e742009-03-07 00:24:24 -03002237 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselye9c64a72009-03-06 23:42:20 -03002238
Hans Verkuilcc26b072009-03-29 19:20:26 -03002239 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, load_fw);
Mike Isely5c6cb4e2009-03-07 01:59:34 -03002240
Mike Iselyc05c0462006-06-25 20:04:25 -03002241 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002242 cptr = hdw->controls + idx;
2243 if (cptr->info->skip_init) continue;
2244 if (!cptr->info->set_value) continue;
2245 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2246 }
2247
Mike Iselye17d7872009-06-20 14:45:52 -03002248 pvr2_hdw_cx25840_vbi_hack(hdw);
2249
Mike Isely1bde0282006-12-27 23:30:13 -03002250 /* Set up special default values for the television and radio
2251 frequencies here. It's not really important what these defaults
2252 are, but I set them to something usable in the Chicago area just
2253 to make driver testing a little easier. */
2254
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002255 hdw->freqValTelevision = default_tv_freq;
2256 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002257
Mike Iselyd8554972006-06-26 20:58:46 -03002258 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2259 // thread-safe against the normal pvr2_send_request() mechanism.
2260 // (We should make it thread safe).
2261
Mike Iselyaaf78842007-11-26 02:04:11 -03002262 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2263 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002264 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002265 if (ret < 0) {
2266 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2267 "Unable to determine location of eeprom,"
2268 " skipping");
2269 } else {
2270 hdw->eeprom_addr = ret;
2271 pvr2_eeprom_analyze(hdw);
2272 if (!pvr2_hdw_dev_ok(hdw)) return;
2273 }
2274 } else {
2275 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2276 hdw->tuner_updated = !0;
2277 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002278 }
2279
Mike Isely13a88792009-01-14 04:22:56 -03002280 if (hdw->serial_number) {
2281 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2282 "sn-%lu", hdw->serial_number);
2283 } else if (hdw->unit_number >= 0) {
2284 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2285 "unit-%c",
2286 hdw->unit_number + 'a');
2287 } else {
2288 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2289 "unit-??");
2290 }
2291 hdw->identifier[idx] = 0;
2292
Mike Iselyd8554972006-06-26 20:58:46 -03002293 pvr2_hdw_setup_std(hdw);
2294
2295 if (!get_default_tuner_type(hdw)) {
2296 pvr2_trace(PVR2_TRACE_INIT,
2297 "pvr2_hdw_setup: Tuner type overridden to %d",
2298 hdw->tuner_type);
2299 }
2300
Mike Iselyd8554972006-06-26 20:58:46 -03002301
2302 if (!pvr2_hdw_dev_ok(hdw)) return;
2303
Mike Isely1df59f02008-04-21 03:50:39 -03002304 if (hdw->hdw_desc->signal_routing_scheme ==
2305 PVR2_ROUTING_SCHEME_GOTVIEW) {
2306 /* Ensure that GPIO 11 is set to output for GOTVIEW
2307 hardware. */
2308 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2309 }
2310
Mike Isely681c7392007-11-26 01:48:52 -03002311 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002312
2313 hdw->vid_stream = pvr2_stream_create();
2314 if (!pvr2_hdw_dev_ok(hdw)) return;
2315 pvr2_trace(PVR2_TRACE_INIT,
2316 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2317 if (hdw->vid_stream) {
2318 idx = get_default_error_tolerance(hdw);
2319 if (idx) {
2320 pvr2_trace(PVR2_TRACE_INIT,
2321 "pvr2_hdw_setup: video stream %p"
2322 " setting tolerance %u",
2323 hdw->vid_stream,idx);
2324 }
2325 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2326 PVR2_VID_ENDPOINT,idx);
2327 }
2328
2329 if (!pvr2_hdw_dev_ok(hdw)) return;
2330
Mike Iselyd8554972006-06-26 20:58:46 -03002331 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002332
2333 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002334}
2335
2336
Mike Isely681c7392007-11-26 01:48:52 -03002337/* Set up the structure and attempt to put the device into a usable state.
2338 This can be a time-consuming operation, which is why it is not done
2339 internally as part of the create() step. */
2340static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002341{
2342 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002343 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002344 pvr2_hdw_setup_low(hdw);
2345 pvr2_trace(PVR2_TRACE_INIT,
2346 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002347 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002348 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002349 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002350 pvr2_trace(
2351 PVR2_TRACE_INFO,
2352 "Device initialization"
2353 " completed successfully.");
2354 break;
2355 }
2356 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2357 pvr2_trace(
2358 PVR2_TRACE_INFO,
2359 "Device microcontroller firmware"
2360 " (re)loaded; it should now reset"
2361 " and reconnect.");
2362 break;
2363 }
2364 pvr2_trace(
2365 PVR2_TRACE_ERROR_LEGS,
2366 "Device initialization was not successful.");
2367 if (hdw->fw1_state == FW1_STATE_MISSING) {
2368 pvr2_trace(
2369 PVR2_TRACE_ERROR_LEGS,
2370 "Giving up since device"
2371 " microcontroller firmware"
2372 " appears to be missing.");
2373 break;
2374 }
2375 }
Mike Isely27108142009-10-12 00:21:20 -03002376 if (hdw->flag_modulefail) {
2377 pvr2_trace(
2378 PVR2_TRACE_ERROR_LEGS,
2379 "***WARNING*** pvrusb2 driver initialization"
2380 " failed due to the failure of one or more"
2381 " sub-device kernel modules.");
2382 pvr2_trace(
2383 PVR2_TRACE_ERROR_LEGS,
2384 "You need to resolve the failing condition"
2385 " before this driver can function. There"
2386 " should be some earlier messages giving more"
2387 " information about the problem.");
Mike Isely515ebf72009-10-12 00:27:38 -03002388 break;
Mike Isely27108142009-10-12 00:21:20 -03002389 }
Mike Iselyd8554972006-06-26 20:58:46 -03002390 if (procreload) {
2391 pvr2_trace(
2392 PVR2_TRACE_ERROR_LEGS,
2393 "Attempting pvrusb2 recovery by reloading"
2394 " primary firmware.");
2395 pvr2_trace(
2396 PVR2_TRACE_ERROR_LEGS,
2397 "If this works, device should disconnect"
2398 " and reconnect in a sane state.");
2399 hdw->fw1_state = FW1_STATE_UNKNOWN;
2400 pvr2_upload_firmware1(hdw);
2401 } else {
2402 pvr2_trace(
2403 PVR2_TRACE_ERROR_LEGS,
2404 "***WARNING*** pvrusb2 device hardware"
2405 " appears to be jammed"
2406 " and I can't clear it.");
2407 pvr2_trace(
2408 PVR2_TRACE_ERROR_LEGS,
2409 "You might need to power cycle"
2410 " the pvrusb2 device"
2411 " in order to recover.");
2412 }
Mike Isely681c7392007-11-26 01:48:52 -03002413 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002414 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002415}
2416
2417
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002418/* Perform second stage initialization. Set callback pointer first so that
2419 we can avoid a possible initialization race (if the kernel thread runs
2420 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002421int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2422 void (*callback_func)(void *),
2423 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002424{
2425 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002426 if (hdw->flag_disconnected) {
2427 /* Handle a race here: If we're already
2428 disconnected by this point, then give up. If we
2429 get past this then we'll remain connected for
2430 the duration of initialization since the entire
2431 initialization sequence is now protected by the
2432 big_lock. */
2433 break;
2434 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002435 hdw->state_data = callback_data;
2436 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002437 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002438 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002439 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002440}
2441
2442
2443/* Create, set up, and return a structure for interacting with the
2444 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002445struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2446 const struct usb_device_id *devid)
2447{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002448 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002449 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002450 int valid_std_mask;
2451 struct pvr2_ctrl *cptr;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002452 struct usb_device *usb_dev;
Mike Isely989eb152007-11-26 01:53:12 -03002453 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002454 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002455 struct v4l2_queryctrl qctrl;
2456 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002457
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002458 usb_dev = interface_to_usbdev(intf);
2459
Mike Iselyd130fa82007-12-08 17:20:06 -03002460 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002461
Mike Iselyfe15f132008-08-30 18:11:40 -03002462 if (hdw_desc == NULL) {
2463 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2464 " No device description pointer,"
2465 " unable to continue.");
2466 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2467 " please contact Mike Isely <isely@pobox.com>"
2468 " to get it included in the driver\n");
2469 goto fail;
2470 }
2471
Mike Iselyca545f72007-01-20 00:37:11 -03002472 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002473 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002474 hdw,hdw_desc->description);
Mike Iselye67e3762009-10-12 00:28:19 -03002475 pvr2_trace(PVR2_TRACE_INFO, "Hardware description: %s",
Mike Isely00970be2009-10-12 00:23:37 -03002476 hdw_desc->description);
Mike Isely8fd04442010-05-15 00:13:35 -03002477 if (hdw_desc->flag_is_experimental) {
2478 pvr2_trace(PVR2_TRACE_INFO, "**********");
2479 pvr2_trace(PVR2_TRACE_INFO,
2480 "WARNING: Support for this device (%s) is"
2481 " experimental.", hdw_desc->description);
2482 pvr2_trace(PVR2_TRACE_INFO,
2483 "Important functionality might not be"
2484 " entirely working.");
2485 pvr2_trace(PVR2_TRACE_INFO,
2486 "Please consider contacting the driver author to"
2487 " help with further stabilization of the driver.");
2488 pvr2_trace(PVR2_TRACE_INFO, "**********");
2489 }
Mike Iselyd8554972006-06-26 20:58:46 -03002490 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002491
2492 init_timer(&hdw->quiescent_timer);
2493 hdw->quiescent_timer.data = (unsigned long)hdw;
2494 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2495
Mike Isely6e931372010-02-06 02:10:38 -03002496 init_timer(&hdw->decoder_stabilization_timer);
2497 hdw->decoder_stabilization_timer.data = (unsigned long)hdw;
2498 hdw->decoder_stabilization_timer.function =
2499 pvr2_hdw_decoder_stabilization_timeout;
2500
Mike Isely681c7392007-11-26 01:48:52 -03002501 init_timer(&hdw->encoder_wait_timer);
2502 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2503 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2504
Mike Iselyd913d632008-04-06 04:04:35 -03002505 init_timer(&hdw->encoder_run_timer);
2506 hdw->encoder_run_timer.data = (unsigned long)hdw;
2507 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2508
Mike Isely681c7392007-11-26 01:48:52 -03002509 hdw->master_state = PVR2_STATE_DEAD;
2510
2511 init_waitqueue_head(&hdw->state_wait_data);
2512
Mike Isely18103c572007-01-20 00:09:47 -03002513 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002514 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002515
Mike Isely7fb20fa2008-04-22 14:45:37 -03002516 /* Calculate which inputs are OK */
2517 m = 0;
2518 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002519 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2520 m |= 1 << PVR2_CVAL_INPUT_DTV;
2521 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002522 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2523 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2524 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2525 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002526 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002527
Mike Isely62433e32008-04-22 14:45:40 -03002528 /* If not a hybrid device, pathway_state never changes. So
2529 initialize it here to what it should forever be. */
2530 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2531 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2532 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2533 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2534 }
2535
Mike Iselyc05c0462006-06-25 20:04:25 -03002536 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002537 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002538 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002539 GFP_KERNEL);
2540 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002541 hdw->hdw_desc = hdw_desc;
Mike Isely27eab382009-04-06 01:51:38 -03002542 hdw->ir_scheme_active = hdw->hdw_desc->ir_scheme;
Mike Iselyc05c0462006-06-25 20:04:25 -03002543 for (idx = 0; idx < hdw->control_cnt; idx++) {
2544 cptr = hdw->controls + idx;
2545 cptr->hdw = hdw;
2546 }
Mike Iselyd8554972006-06-26 20:58:46 -03002547 for (idx = 0; idx < 32; idx++) {
2548 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2549 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002550 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002551 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002552 cptr->info = control_defs+idx;
2553 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002554
2555 /* Ensure that default input choice is a valid one. */
2556 m = hdw->input_avail_mask;
2557 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2558 if (!((1 << idx) & m)) continue;
2559 hdw->input_val = idx;
2560 break;
2561 }
2562
Mike Iselyb30d2442006-06-25 20:05:01 -03002563 /* Define and configure additional controls from cx2341x module. */
Thomas Meyere79c70e2011-11-29 17:08:00 -03002564 hdw->mpeg_ctrl_info = kcalloc(MPEGDEF_COUNT,
2565 sizeof(*(hdw->mpeg_ctrl_info)),
2566 GFP_KERNEL);
Mike Iselyb30d2442006-06-25 20:05:01 -03002567 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002568 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2569 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2570 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2571 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2572 ciptr->name = mpeg_ids[idx].strid;
2573 ciptr->v4l_id = mpeg_ids[idx].id;
2574 ciptr->skip_init = !0;
2575 ciptr->get_value = ctrl_cx2341x_get;
2576 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2577 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2578 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2579 qctrl.id = ciptr->v4l_id;
2580 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2581 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2582 ciptr->set_value = ctrl_cx2341x_set;
2583 }
2584 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2585 PVR2_CTLD_INFO_DESC_SIZE);
2586 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2587 ciptr->default_value = qctrl.default_value;
2588 switch (qctrl.type) {
2589 default:
2590 case V4L2_CTRL_TYPE_INTEGER:
2591 ciptr->type = pvr2_ctl_int;
2592 ciptr->def.type_int.min_value = qctrl.minimum;
2593 ciptr->def.type_int.max_value = qctrl.maximum;
2594 break;
2595 case V4L2_CTRL_TYPE_BOOLEAN:
2596 ciptr->type = pvr2_ctl_bool;
2597 break;
2598 case V4L2_CTRL_TYPE_MENU:
2599 ciptr->type = pvr2_ctl_enum;
2600 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002601 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2602 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002603 for (cnt1 = 0;
2604 ciptr->def.type_enum.value_names[cnt1] != NULL;
2605 cnt1++) { }
2606 ciptr->def.type_enum.count = cnt1;
2607 break;
2608 }
2609 cptr->info = ciptr;
2610 }
Mike Iselyd8554972006-06-26 20:58:46 -03002611
2612 // Initialize video standard enum dynamic control
2613 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2614 if (cptr) {
2615 memcpy(&hdw->std_info_enum,cptr->info,
2616 sizeof(hdw->std_info_enum));
2617 cptr->info = &hdw->std_info_enum;
2618
2619 }
2620 // Initialize control data regarding video standard masks
2621 valid_std_mask = pvr2_std_get_usable();
2622 for (idx = 0; idx < 32; idx++) {
2623 if (!(valid_std_mask & (1 << idx))) continue;
2624 cnt1 = pvr2_std_id_to_str(
2625 hdw->std_mask_names[idx],
2626 sizeof(hdw->std_mask_names[idx])-1,
2627 1 << idx);
2628 hdw->std_mask_names[idx][cnt1] = 0;
2629 }
2630 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2631 if (cptr) {
2632 memcpy(&hdw->std_info_avail,cptr->info,
2633 sizeof(hdw->std_info_avail));
2634 cptr->info = &hdw->std_info_avail;
2635 hdw->std_info_avail.def.type_bitmask.bit_names =
2636 hdw->std_mask_ptrs;
2637 hdw->std_info_avail.def.type_bitmask.valid_bits =
2638 valid_std_mask;
2639 }
2640 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2641 if (cptr) {
2642 memcpy(&hdw->std_info_cur,cptr->info,
2643 sizeof(hdw->std_info_cur));
2644 cptr->info = &hdw->std_info_cur;
2645 hdw->std_info_cur.def.type_bitmask.bit_names =
2646 hdw->std_mask_ptrs;
Mike Iselyac04d002012-02-20 02:28:56 -03002647 hdw->std_info_cur.def.type_bitmask.valid_bits =
2648 valid_std_mask;
2649 }
2650 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDDETECT);
2651 if (cptr) {
2652 memcpy(&hdw->std_info_detect,cptr->info,
2653 sizeof(hdw->std_info_detect));
2654 cptr->info = &hdw->std_info_detect;
2655 hdw->std_info_detect.def.type_bitmask.bit_names =
2656 hdw->std_mask_ptrs;
2657 hdw->std_info_detect.def.type_bitmask.valid_bits =
Mike Iselyd8554972006-06-26 20:58:46 -03002658 valid_std_mask;
2659 }
2660
Mike Isely432907f2008-08-31 21:02:20 -03002661 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002662 hdw->eeprom_addr = -1;
2663 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002664 hdw->v4l_minor_number_video = -1;
2665 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002666 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002667 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2668 if (!hdw->ctl_write_buffer) goto fail;
2669 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2670 if (!hdw->ctl_read_buffer) goto fail;
2671 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2672 if (!hdw->ctl_write_urb) goto fail;
2673 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2674 if (!hdw->ctl_read_urb) goto fail;
2675
Janne Grunau70ad6382009-04-01 08:46:50 -03002676 if (v4l2_device_register(&intf->dev, &hdw->v4l2_dev) != 0) {
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002677 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2678 "Error registering with v4l core, giving up");
2679 goto fail;
2680 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002681 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002682 for (idx = 0; idx < PVR_NUM; idx++) {
2683 if (unit_pointers[idx]) continue;
2684 hdw->unit_number = idx;
2685 unit_pointers[idx] = hdw;
2686 break;
2687 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002688 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002689
2690 cnt1 = 0;
2691 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2692 cnt1 += cnt2;
2693 if (hdw->unit_number >= 0) {
2694 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2695 ('a' + hdw->unit_number));
2696 cnt1 += cnt2;
2697 }
2698 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2699 hdw->name[cnt1] = 0;
2700
Mike Isely681c7392007-11-26 01:48:52 -03002701 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2702 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
Mike Isely681c7392007-11-26 01:48:52 -03002703
Mike Iselyd8554972006-06-26 20:58:46 -03002704 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2705 hdw->unit_number,hdw->name);
2706
2707 hdw->tuner_type = -1;
2708 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002709
2710 hdw->usb_intf = intf;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002711 hdw->usb_dev = usb_dev;
Mike Iselyd8554972006-06-26 20:58:46 -03002712
Mike Isely87e34952009-01-23 01:20:24 -03002713 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
Mike Isely31a18542007-04-08 01:11:47 -03002714
Mike Iselyd8554972006-06-26 20:58:46 -03002715 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2716 usb_set_interface(hdw->usb_dev,ifnum,0);
2717
2718 mutex_init(&hdw->ctl_lock_mutex);
2719 mutex_init(&hdw->big_lock_mutex);
2720
2721 return hdw;
2722 fail:
2723 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002724 del_timer_sync(&hdw->quiescent_timer);
Mike Isely6e931372010-02-06 02:10:38 -03002725 del_timer_sync(&hdw->decoder_stabilization_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002726 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002727 del_timer_sync(&hdw->encoder_wait_timer);
2728 if (hdw->workqueue) {
2729 flush_workqueue(hdw->workqueue);
2730 destroy_workqueue(hdw->workqueue);
2731 hdw->workqueue = NULL;
2732 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002733 usb_free_urb(hdw->ctl_read_urb);
2734 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002735 kfree(hdw->ctl_read_buffer);
2736 kfree(hdw->ctl_write_buffer);
2737 kfree(hdw->controls);
2738 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002739 kfree(hdw->std_defs);
2740 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002741 kfree(hdw);
2742 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002743 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002744}
2745
2746
2747/* Remove _all_ associations between this driver and the underlying USB
2748 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002749static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002750{
2751 if (hdw->flag_disconnected) return;
2752 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2753 if (hdw->ctl_read_urb) {
2754 usb_kill_urb(hdw->ctl_read_urb);
2755 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002756 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002757 }
2758 if (hdw->ctl_write_urb) {
2759 usb_kill_urb(hdw->ctl_write_urb);
2760 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002761 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002762 }
2763 if (hdw->ctl_read_buffer) {
2764 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002765 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002766 }
2767 if (hdw->ctl_write_buffer) {
2768 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002769 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002770 }
Mike Iselyd8554972006-06-26 20:58:46 -03002771 hdw->flag_disconnected = !0;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002772 /* If we don't do this, then there will be a dangling struct device
2773 reference to our disappearing device persisting inside the V4L
2774 core... */
Mike Iselydc070bc2009-03-25 00:30:45 -03002775 v4l2_device_disconnect(&hdw->v4l2_dev);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002776 hdw->usb_dev = NULL;
2777 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002778 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002779}
2780
2781
2782/* Destroy hardware interaction structure */
2783void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2784{
Mike Isely401c27c2007-09-08 22:11:46 -03002785 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002786 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002787 if (hdw->workqueue) {
2788 flush_workqueue(hdw->workqueue);
2789 destroy_workqueue(hdw->workqueue);
2790 hdw->workqueue = NULL;
2791 }
Mike Isely8f591002008-04-22 14:45:45 -03002792 del_timer_sync(&hdw->quiescent_timer);
Mike Isely6e931372010-02-06 02:10:38 -03002793 del_timer_sync(&hdw->decoder_stabilization_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002794 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002795 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002796 if (hdw->fw_buffer) {
2797 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002798 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002799 }
2800 if (hdw->vid_stream) {
2801 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002802 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002803 }
Mike Iselyd8554972006-06-26 20:58:46 -03002804 pvr2_i2c_core_done(hdw);
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002805 v4l2_device_unregister(&hdw->v4l2_dev);
Mike Iselyd8554972006-06-26 20:58:46 -03002806 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002807 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002808 if ((hdw->unit_number >= 0) &&
2809 (hdw->unit_number < PVR_NUM) &&
2810 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002811 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002812 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002813 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002814 kfree(hdw->controls);
2815 kfree(hdw->mpeg_ctrl_info);
2816 kfree(hdw->std_defs);
2817 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002818 kfree(hdw);
2819}
2820
2821
Mike Iselyd8554972006-06-26 20:58:46 -03002822int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2823{
2824 return (hdw && hdw->flag_ok);
2825}
2826
2827
2828/* Called when hardware has been unplugged */
2829void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2830{
2831 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2832 LOCK_TAKE(hdw->big_lock);
2833 LOCK_TAKE(hdw->ctl_lock);
2834 pvr2_hdw_remove_usb_stuff(hdw);
2835 LOCK_GIVE(hdw->ctl_lock);
2836 LOCK_GIVE(hdw->big_lock);
2837}
2838
2839
2840// Attempt to autoselect an appropriate value for std_enum_cur given
2841// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002842static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002843{
2844 unsigned int idx;
2845 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2846 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2847 hdw->std_enum_cur = idx;
2848 return;
2849 }
2850 }
2851 hdw->std_enum_cur = 0;
2852}
2853
2854
2855// Calculate correct set of enumerated standards based on currently known
2856// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002857static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002858{
2859 struct v4l2_standard *newstd;
2860 unsigned int std_cnt;
2861 unsigned int idx;
2862
2863 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2864
2865 if (hdw->std_defs) {
2866 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002867 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002868 }
2869 hdw->std_enum_cnt = 0;
2870 if (hdw->std_enum_names) {
2871 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002872 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002873 }
2874
2875 if (!std_cnt) {
2876 pvr2_trace(
2877 PVR2_TRACE_ERROR_LEGS,
2878 "WARNING: Failed to identify any viable standards");
2879 }
Xiaochen Wangc05df8b2011-03-13 22:32:53 -03002880
2881 /* Set up the dynamic control for this standard */
Mike Iselyd8554972006-06-26 20:58:46 -03002882 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
Xiaochen Wangc05df8b2011-03-13 22:32:53 -03002883 if (hdw->std_enum_names) {
2884 hdw->std_enum_names[0] = "none";
2885 for (idx = 0; idx < std_cnt; idx++)
2886 hdw->std_enum_names[idx+1] = newstd[idx].name;
2887 hdw->std_info_enum.def.type_enum.value_names =
2888 hdw->std_enum_names;
2889 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2890 } else {
2891 pvr2_trace(
2892 PVR2_TRACE_ERROR_LEGS,
2893 "WARNING: Failed to alloc memory for names");
2894 hdw->std_info_enum.def.type_enum.value_names = NULL;
2895 hdw->std_info_enum.def.type_enum.count = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002896 }
Mike Iselyd8554972006-06-26 20:58:46 -03002897 hdw->std_defs = newstd;
2898 hdw->std_enum_cnt = std_cnt+1;
2899 hdw->std_enum_cur = 0;
2900 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2901}
2902
2903
2904int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2905 struct v4l2_standard *std,
2906 unsigned int idx)
2907{
2908 int ret = -EINVAL;
2909 if (!idx) return ret;
2910 LOCK_TAKE(hdw->big_lock); do {
2911 if (idx >= hdw->std_enum_cnt) break;
2912 idx--;
2913 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2914 ret = 0;
2915 } while (0); LOCK_GIVE(hdw->big_lock);
2916 return ret;
2917}
2918
2919
2920/* Get the number of defined controls */
2921unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2922{
Mike Iselyc05c0462006-06-25 20:04:25 -03002923 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002924}
2925
2926
2927/* Retrieve a control handle given its index (0..count-1) */
2928struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2929 unsigned int idx)
2930{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002931 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002932 return hdw->controls + idx;
2933}
2934
2935
2936/* Retrieve a control handle given its index (0..count-1) */
2937struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2938 unsigned int ctl_id)
2939{
2940 struct pvr2_ctrl *cptr;
2941 unsigned int idx;
2942 int i;
2943
2944 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002945 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002946 cptr = hdw->controls + idx;
2947 i = cptr->info->internal_id;
2948 if (i && (i == ctl_id)) return cptr;
2949 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002950 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002951}
2952
2953
Mike Iselya761f432006-06-25 20:04:44 -03002954/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002955struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2956{
2957 struct pvr2_ctrl *cptr;
2958 unsigned int idx;
2959 int i;
2960
2961 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002962 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002963 cptr = hdw->controls + idx;
2964 i = cptr->info->v4l_id;
2965 if (i && (i == ctl_id)) return cptr;
2966 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002967 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002968}
2969
2970
Mike Iselya761f432006-06-25 20:04:44 -03002971/* Given a V4L ID for its immediate predecessor, retrieve the control
2972 structure associated with it. */
2973struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2974 unsigned int ctl_id)
2975{
2976 struct pvr2_ctrl *cptr,*cp2;
2977 unsigned int idx;
2978 int i;
2979
2980 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002981 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002982 for (idx = 0; idx < hdw->control_cnt; idx++) {
2983 cptr = hdw->controls + idx;
2984 i = cptr->info->v4l_id;
2985 if (!i) continue;
2986 if (i <= ctl_id) continue;
2987 if (cp2 && (cp2->info->v4l_id < i)) continue;
2988 cp2 = cptr;
2989 }
2990 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002991 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002992}
2993
2994
Mike Iselyd8554972006-06-26 20:58:46 -03002995static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2996{
2997 switch (tp) {
2998 case pvr2_ctl_int: return "integer";
2999 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03003000 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03003001 case pvr2_ctl_bitmask: return "bitmask";
3002 }
3003 return "";
3004}
3005
3006
Mike Isely2641df32009-03-07 00:13:25 -03003007static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
3008 const char *name, int val)
3009{
3010 struct v4l2_control ctrl;
3011 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
3012 memset(&ctrl, 0, sizeof(ctrl));
3013 ctrl.id = id;
3014 ctrl.value = val;
3015 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
3016}
3017
3018#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
Mike Isely27764722009-03-07 01:57:25 -03003019 if ((hdw)->lab##_dirty || (hdw)->force_dirty) { \
Mike Isely2641df32009-03-07 00:13:25 -03003020 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
3021 }
3022
Mike Iselyac04d002012-02-20 02:28:56 -03003023v4l2_std_id pvr2_hdw_get_detected_std(struct pvr2_hdw *hdw)
Mauro Carvalho Chehab7383a472011-10-03 12:22:28 -03003024{
Mike Iselyac04d002012-02-20 02:28:56 -03003025 v4l2_std_id std;
3026 std = V4L2_STD_ALL;
Mauro Carvalho Chehab7383a472011-10-03 12:22:28 -03003027 v4l2_device_call_all(&hdw->v4l2_dev, 0,
Mike Iselyac04d002012-02-20 02:28:56 -03003028 video, querystd, &std);
3029 return std;
Mauro Carvalho Chehab7383a472011-10-03 12:22:28 -03003030}
3031
Mike Isely5ceaad12009-03-07 00:01:20 -03003032/* Execute whatever commands are required to update the state of all the
Mike Isely2641df32009-03-07 00:13:25 -03003033 sub-devices so that they match our current control values. */
Mike Isely5ceaad12009-03-07 00:01:20 -03003034static void pvr2_subdev_update(struct pvr2_hdw *hdw)
3035{
Mike Iselyedb9dcb2009-03-07 00:37:10 -03003036 struct v4l2_subdev *sd;
3037 unsigned int id;
3038 pvr2_subdev_update_func fp;
3039
Mike Isely75212a02009-03-07 01:48:42 -03003040 pvr2_trace(PVR2_TRACE_CHIPS, "subdev update...");
3041
Mike Isely27764722009-03-07 01:57:25 -03003042 if (hdw->tuner_updated || hdw->force_dirty) {
Mike Isely75212a02009-03-07 01:48:42 -03003043 struct tuner_setup setup;
3044 pvr2_trace(PVR2_TRACE_CHIPS, "subdev tuner set_type(%d)",
3045 hdw->tuner_type);
3046 if (((int)(hdw->tuner_type)) >= 0) {
Mike Iselyfcd62cf2009-04-01 01:55:26 -03003047 memset(&setup, 0, sizeof(setup));
Mike Isely75212a02009-03-07 01:48:42 -03003048 setup.addr = ADDR_UNSET;
3049 setup.type = hdw->tuner_type;
3050 setup.mode_mask = T_RADIO | T_ANALOG_TV;
3051 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3052 tuner, s_type_addr, &setup);
3053 }
3054 }
3055
Mike Isely27764722009-03-07 01:57:25 -03003056 if (hdw->input_dirty || hdw->std_dirty || hdw->force_dirty) {
Mike Iselyb4818802009-03-07 01:46:17 -03003057 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_standard");
Mike Isely2641df32009-03-07 00:13:25 -03003058 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3059 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3060 tuner, s_radio);
3061 } else {
3062 v4l2_std_id vs;
3063 vs = hdw->std_mask_cur;
3064 v4l2_device_call_all(&hdw->v4l2_dev, 0,
Hans Verkuilf41737e2009-04-01 03:52:39 -03003065 core, s_std, vs);
Mike Iselya6862da2009-06-20 14:50:14 -03003066 pvr2_hdw_cx25840_vbi_hack(hdw);
Mike Isely2641df32009-03-07 00:13:25 -03003067 }
3068 hdw->tuner_signal_stale = !0;
3069 hdw->cropcap_stale = !0;
3070 }
3071
3072 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
3073 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
3074 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
3075 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
3076 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
3077 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
3078 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
3079 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
3080 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
3081
Mike Isely27764722009-03-07 01:57:25 -03003082 if (hdw->input_dirty || hdw->audiomode_dirty || hdw->force_dirty) {
Mike Isely2641df32009-03-07 00:13:25 -03003083 struct v4l2_tuner vt;
3084 memset(&vt, 0, sizeof(vt));
Hans Verkuil50e9efd2011-06-12 06:39:52 -03003085 vt.type = (hdw->input_val == PVR2_CVAL_INPUT_RADIO) ?
3086 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mike Isely2641df32009-03-07 00:13:25 -03003087 vt.audmode = hdw->audiomode_val;
3088 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
3089 }
3090
Mike Isely27764722009-03-07 01:57:25 -03003091 if (hdw->freqDirty || hdw->force_dirty) {
Mike Isely2641df32009-03-07 00:13:25 -03003092 unsigned long fv;
3093 struct v4l2_frequency freq;
3094 fv = pvr2_hdw_get_cur_freq(hdw);
3095 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
3096 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
3097 memset(&freq, 0, sizeof(freq));
3098 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
3099 /* ((fv * 1000) / 62500) */
3100 freq.frequency = (fv * 2) / 125;
3101 } else {
3102 freq.frequency = fv / 62500;
3103 }
3104 /* tuner-core currently doesn't seem to care about this, but
3105 let's set it anyway for completeness. */
3106 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3107 freq.type = V4L2_TUNER_RADIO;
3108 } else {
3109 freq.type = V4L2_TUNER_ANALOG_TV;
3110 }
3111 freq.tuner = 0;
3112 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
3113 s_frequency, &freq);
3114 }
3115
Mike Isely27764722009-03-07 01:57:25 -03003116 if (hdw->res_hor_dirty || hdw->res_ver_dirty || hdw->force_dirty) {
Hans Verkuilfa190ee2010-05-08 17:16:18 -03003117 struct v4l2_mbus_framefmt fmt;
Mike Isely2641df32009-03-07 00:13:25 -03003118 memset(&fmt, 0, sizeof(fmt));
Hans Verkuilfa190ee2010-05-08 17:16:18 -03003119 fmt.width = hdw->res_hor_val;
3120 fmt.height = hdw->res_ver_val;
3121 fmt.code = V4L2_MBUS_FMT_FIXED;
Mike Isely7dfdf1e2009-03-07 02:11:12 -03003122 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_size(%dx%d)",
Hans Verkuilfa190ee2010-05-08 17:16:18 -03003123 fmt.width, fmt.height);
3124 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_mbus_fmt, &fmt);
Mike Isely2641df32009-03-07 00:13:25 -03003125 }
3126
Mike Isely27764722009-03-07 01:57:25 -03003127 if (hdw->srate_dirty || hdw->force_dirty) {
Mike Isely01c59df2009-03-07 00:48:09 -03003128 u32 val;
3129 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_audio %d",
3130 hdw->srate_val);
3131 switch (hdw->srate_val) {
3132 default:
3133 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
3134 val = 48000;
3135 break;
3136 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
3137 val = 44100;
3138 break;
3139 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
3140 val = 32000;
3141 break;
3142 }
3143 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3144 audio, s_clock_freq, val);
3145 }
3146
Mike Isely2641df32009-03-07 00:13:25 -03003147 /* Unable to set crop parameters; there is apparently no equivalent
3148 for VIDIOC_S_CROP */
3149
Mike Iselyedb9dcb2009-03-07 00:37:10 -03003150 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
3151 id = sd->grp_id;
3152 if (id >= ARRAY_SIZE(pvr2_module_update_functions)) continue;
3153 fp = pvr2_module_update_functions[id];
3154 if (!fp) continue;
3155 (*fp)(hdw, sd);
3156 }
Mike Isely2641df32009-03-07 00:13:25 -03003157
Mike Isely27764722009-03-07 01:57:25 -03003158 if (hdw->tuner_signal_stale || hdw->cropcap_stale) {
Mike Isely2641df32009-03-07 00:13:25 -03003159 pvr2_hdw_status_poll(hdw);
3160 }
Mike Isely5ceaad12009-03-07 00:01:20 -03003161}
3162
3163
Mike Isely681c7392007-11-26 01:48:52 -03003164/* Figure out if we need to commit control changes. If so, mark internal
3165 state flags to indicate this fact and return true. Otherwise do nothing
3166 else and return false. */
3167static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003168{
Mike Iselyd8554972006-06-26 20:58:46 -03003169 unsigned int idx;
3170 struct pvr2_ctrl *cptr;
3171 int value;
Mike Isely27764722009-03-07 01:57:25 -03003172 int commit_flag = hdw->force_dirty;
Mike Iselyd8554972006-06-26 20:58:46 -03003173 char buf[100];
3174 unsigned int bcnt,ccnt;
3175
Mike Iselyc05c0462006-06-25 20:04:25 -03003176 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03003177 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00003178 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003179 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03003180 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003181
Mike Iselyfe23a282007-01-20 00:10:55 -03003182 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003183 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3184 cptr->info->name);
3185 value = 0;
3186 cptr->info->get_value(cptr,&value);
3187 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3188 buf+bcnt,
3189 sizeof(buf)-bcnt,&ccnt);
3190 bcnt += ccnt;
3191 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3192 get_ctrl_typename(cptr->info->type));
3193 pvr2_trace(PVR2_TRACE_CTL,
3194 "/*--TRACE_COMMIT--*/ %.*s",
3195 bcnt,buf);
3196 }
3197
3198 if (!commit_flag) {
3199 /* Nothing has changed */
3200 return 0;
3201 }
3202
Mike Isely681c7392007-11-26 01:48:52 -03003203 hdw->state_pipeline_config = 0;
3204 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3205 pvr2_hdw_state_sched(hdw);
3206
3207 return !0;
3208}
3209
3210
3211/* Perform all operations needed to commit all control changes. This must
3212 be performed in synchronization with the pipeline state and is thus
3213 expected to be called as part of the driver's worker thread. Return
3214 true if commit successful, otherwise return false to indicate that
3215 commit isn't possible at this time. */
3216static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3217{
3218 unsigned int idx;
3219 struct pvr2_ctrl *cptr;
3220 int disruptive_change;
3221
Mike Isely9bf98322011-02-13 17:31:55 -03003222 if (hdw->input_dirty && hdw->state_pathway_ok &&
3223 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3224 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3225 hdw->pathway_state)) {
3226 /* Change of mode being asked for... */
3227 hdw->state_pathway_ok = 0;
3228 trace_stbit("state_pathway_ok", hdw->state_pathway_ok);
3229 }
3230 if (!hdw->state_pathway_ok) {
3231 /* Can't commit anything until pathway is ok. */
3232 return 0;
3233 }
3234
Mike Iselyab062fe2008-06-30 03:32:35 -03003235 /* Handle some required side effects when the video standard is
3236 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03003237 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03003238 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03003239 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03003240 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3241 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03003242 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03003243 } else {
3244 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03003245 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03003246 }
Mike Isely00528d92008-06-30 03:35:52 -03003247 /* Rewrite the vertical resolution to be appropriate to the
3248 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03003249 if (nvres != hdw->res_ver_val) {
3250 hdw->res_ver_val = nvres;
3251 hdw->res_ver_dirty = !0;
3252 }
Mike Isely00528d92008-06-30 03:35:52 -03003253 /* Rewrite the GOP size to be appropriate to the video
3254 standard that has been selected. */
3255 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3256 struct v4l2_ext_controls cs;
3257 struct v4l2_ext_control c1;
3258 memset(&cs, 0, sizeof(cs));
3259 memset(&c1, 0, sizeof(c1));
3260 cs.controls = &c1;
3261 cs.count = 1;
3262 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3263 c1.value = gop_size;
3264 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3265 VIDIOC_S_EXT_CTRLS);
3266 }
Mike Iselyd8554972006-06-26 20:58:46 -03003267 }
3268
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03003269 /* The broadcast decoder can only scale down, so if
3270 * res_*_dirty && crop window < output format ==> enlarge crop.
3271 *
3272 * The mpeg encoder receives fields of res_hor_val dots and
3273 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3274 */
3275 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3276 hdw->cropw_val = hdw->res_hor_val;
3277 hdw->cropw_dirty = !0;
3278 } else if (hdw->cropw_dirty) {
3279 hdw->res_hor_dirty = !0; /* must rescale */
3280 hdw->res_hor_val = min(720, hdw->cropw_val);
3281 }
3282 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3283 hdw->croph_val = hdw->res_ver_val;
3284 hdw->croph_dirty = !0;
3285 } else if (hdw->croph_dirty) {
3286 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3287 hdw->res_ver_dirty = !0;
3288 hdw->res_ver_val = min(nvres, hdw->croph_val);
3289 }
3290
Mike Isely681c7392007-11-26 01:48:52 -03003291 /* If any of the below has changed, then we can't do the update
3292 while the pipeline is running. Pipeline must be paused first
3293 and decoder -> encoder connection be made quiescent before we
3294 can proceed. */
3295 disruptive_change =
3296 (hdw->std_dirty ||
3297 hdw->enc_unsafe_stale ||
3298 hdw->srate_dirty ||
3299 hdw->res_ver_dirty ||
3300 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03003301 hdw->cropw_dirty ||
3302 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03003303 hdw->input_dirty ||
3304 (hdw->active_stream_type != hdw->desired_stream_type));
3305 if (disruptive_change && !hdw->state_pipeline_idle) {
3306 /* Pipeline is not idle; we can't proceed. Arrange to
3307 cause pipeline to stop so that we can try this again
3308 later.... */
3309 hdw->state_pipeline_pause = !0;
3310 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003311 }
3312
Mike Iselyb30d2442006-06-25 20:05:01 -03003313 if (hdw->srate_dirty) {
3314 /* Write new sample rate into control structure since
3315 * the master copy is stale. We must track srate
3316 * separate from the mpeg control structure because
3317 * other logic also uses this value. */
3318 struct v4l2_ext_controls cs;
3319 struct v4l2_ext_control c1;
3320 memset(&cs,0,sizeof(cs));
3321 memset(&c1,0,sizeof(c1));
3322 cs.controls = &c1;
3323 cs.count = 1;
3324 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3325 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03003326 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03003327 }
Mike Iselyc05c0462006-06-25 20:04:25 -03003328
Mike Isely681c7392007-11-26 01:48:52 -03003329 if (hdw->active_stream_type != hdw->desired_stream_type) {
3330 /* Handle any side effects of stream config here */
3331 hdw->active_stream_type = hdw->desired_stream_type;
3332 }
3333
Mike Isely1df59f02008-04-21 03:50:39 -03003334 if (hdw->hdw_desc->signal_routing_scheme ==
3335 PVR2_ROUTING_SCHEME_GOTVIEW) {
3336 u32 b;
3337 /* Handle GOTVIEW audio switching */
3338 pvr2_hdw_gpio_get_out(hdw,&b);
3339 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3340 /* Set GPIO 11 */
3341 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3342 } else {
3343 /* Clear GPIO 11 */
3344 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3345 }
3346 }
3347
Mike Iselye68a6192009-03-07 01:45:10 -03003348 /* Check and update state for all sub-devices. */
3349 pvr2_subdev_update(hdw);
3350
Mike Isely75212a02009-03-07 01:48:42 -03003351 hdw->tuner_updated = 0;
Mike Isely27764722009-03-07 01:57:25 -03003352 hdw->force_dirty = 0;
Mike Isely5ceaad12009-03-07 00:01:20 -03003353 for (idx = 0; idx < hdw->control_cnt; idx++) {
3354 cptr = hdw->controls + idx;
3355 if (!cptr->info->clear_dirty) continue;
3356 cptr->info->clear_dirty(cptr);
3357 }
3358
Mike Isely62433e32008-04-22 14:45:40 -03003359 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3360 hdw->state_encoder_run) {
3361 /* If encoder isn't running or it can't be touched, then
3362 this will get worked out later when we start the
3363 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03003364 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3365 }
Mike Iselyd8554972006-06-26 20:58:46 -03003366
Mike Isely681c7392007-11-26 01:48:52 -03003367 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03003368 /* Hardware state may have changed in a way to cause the cropping
3369 capabilities to have changed. So mark it stale, which will
3370 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03003371 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3372 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003373}
3374
3375
3376int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3377{
Mike Isely681c7392007-11-26 01:48:52 -03003378 int fl;
3379 LOCK_TAKE(hdw->big_lock);
3380 fl = pvr2_hdw_commit_setup(hdw);
3381 LOCK_GIVE(hdw->big_lock);
3382 if (!fl) return 0;
3383 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003384}
3385
3386
Mike Isely681c7392007-11-26 01:48:52 -03003387static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003388{
Mike Isely681c7392007-11-26 01:48:52 -03003389 int fl = 0;
3390 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03003391 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03003392 fl = pvr2_hdw_state_eval(hdw);
3393 } while (0); LOCK_GIVE(hdw->big_lock);
3394 if (fl && hdw->state_func) {
3395 hdw->state_func(hdw->state_data);
3396 }
3397}
3398
3399
Mike Isely681c7392007-11-26 01:48:52 -03003400static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03003401{
Mike Isely681c7392007-11-26 01:48:52 -03003402 return wait_event_interruptible(
3403 hdw->state_wait_data,
3404 (hdw->state_stale == 0) &&
3405 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03003406}
3407
Mike Isely681c7392007-11-26 01:48:52 -03003408
Mike Iselyd8554972006-06-26 20:58:46 -03003409/* Return name for this driver instance */
3410const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3411{
3412 return hdw->name;
3413}
3414
3415
Mike Isely78a47102007-11-26 01:58:20 -03003416const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3417{
3418 return hdw->hdw_desc->description;
3419}
3420
3421
3422const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3423{
3424 return hdw->hdw_desc->shortname;
3425}
3426
3427
Mike Iselyd8554972006-06-26 20:58:46 -03003428int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3429{
3430 int result;
3431 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003432 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03003433 result = pvr2_send_request(hdw,
3434 hdw->cmd_buffer,1,
3435 hdw->cmd_buffer,1);
3436 if (result < 0) break;
3437 result = (hdw->cmd_buffer[0] != 0);
3438 } while(0); LOCK_GIVE(hdw->ctl_lock);
3439 return result;
3440}
3441
3442
Mike Isely18103c572007-01-20 00:09:47 -03003443/* Execute poll of tuner status */
3444void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003445{
Mike Iselyd8554972006-06-26 20:58:46 -03003446 LOCK_TAKE(hdw->big_lock); do {
Mike Iselya51f5002009-03-06 23:30:37 -03003447 pvr2_hdw_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003448 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003449}
3450
3451
Mike Isely432907f2008-08-31 21:02:20 -03003452static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3453{
3454 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003455 return 0;
3456 }
Mike Iselya51f5002009-03-06 23:30:37 -03003457 pvr2_hdw_status_poll(hdw);
Mike Isely432907f2008-08-31 21:02:20 -03003458 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003459 return -EIO;
3460 }
3461 return 0;
3462}
3463
3464
3465/* Return information about cropping capabilities */
3466int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3467{
3468 int stat = 0;
3469 LOCK_TAKE(hdw->big_lock);
3470 stat = pvr2_hdw_check_cropcap(hdw);
3471 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003472 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3473 }
3474 LOCK_GIVE(hdw->big_lock);
3475 return stat;
3476}
3477
3478
Mike Isely18103c572007-01-20 00:09:47 -03003479/* Return information about the tuner */
3480int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3481{
3482 LOCK_TAKE(hdw->big_lock); do {
3483 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -03003484 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -03003485 }
3486 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3487 } while (0); LOCK_GIVE(hdw->big_lock);
3488 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003489}
3490
3491
3492/* Get handle to video output stream */
3493struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3494{
3495 return hp->vid_stream;
3496}
3497
3498
3499void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3500{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003501 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003502 LOCK_TAKE(hdw->big_lock); do {
Mike Isely4f1a3e52006-06-25 20:04:31 -03003503 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyed3261a2009-03-07 00:02:33 -03003504 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
Mike Iselyb30d2442006-06-25 20:05:01 -03003505 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003506 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003507 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003508 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003509 } while (0); LOCK_GIVE(hdw->big_lock);
3510}
3511
Mike Isely4db666c2007-09-08 22:16:27 -03003512
3513/* Grab EEPROM contents, needed for direct method. */
3514#define EEPROM_SIZE 8192
3515#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3516static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3517{
3518 struct i2c_msg msg[2];
3519 u8 *eeprom;
3520 u8 iadd[2];
3521 u8 addr;
3522 u16 eepromSize;
3523 unsigned int offs;
3524 int ret;
3525 int mode16 = 0;
3526 unsigned pcnt,tcnt;
3527 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3528 if (!eeprom) {
3529 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3530 "Failed to allocate memory"
3531 " required to read eeprom");
3532 return NULL;
3533 }
3534
3535 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3536 hdw->eeprom_addr);
3537 addr = hdw->eeprom_addr;
3538 /* Seems that if the high bit is set, then the *real* eeprom
3539 address is shifted right now bit position (noticed this in
3540 newer PVR USB2 hardware) */
3541 if (addr & 0x80) addr >>= 1;
3542
3543 /* FX2 documentation states that a 16bit-addressed eeprom is
3544 expected if the I2C address is an odd number (yeah, this is
3545 strange but it's what they do) */
3546 mode16 = (addr & 1);
3547 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3548 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3549 " using %d bit addressing",eepromSize,addr,
3550 mode16 ? 16 : 8);
3551
3552 msg[0].addr = addr;
3553 msg[0].flags = 0;
3554 msg[0].len = mode16 ? 2 : 1;
3555 msg[0].buf = iadd;
3556 msg[1].addr = addr;
3557 msg[1].flags = I2C_M_RD;
3558
3559 /* We have to do the actual eeprom data fetch ourselves, because
3560 (1) we're only fetching part of the eeprom, and (2) if we were
3561 getting the whole thing our I2C driver can't grab it in one
3562 pass - which is what tveeprom is otherwise going to attempt */
3563 memset(eeprom,0,EEPROM_SIZE);
3564 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3565 pcnt = 16;
3566 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3567 offs = tcnt + (eepromSize - EEPROM_SIZE);
3568 if (mode16) {
3569 iadd[0] = offs >> 8;
3570 iadd[1] = offs;
3571 } else {
3572 iadd[0] = offs;
3573 }
3574 msg[1].len = pcnt;
3575 msg[1].buf = eeprom+tcnt;
3576 if ((ret = i2c_transfer(&hdw->i2c_adap,
3577 msg,ARRAY_SIZE(msg))) != 2) {
3578 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3579 "eeprom fetch set offs err=%d",ret);
3580 kfree(eeprom);
3581 return NULL;
3582 }
3583 }
3584 return eeprom;
3585}
3586
3587
3588void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
Mike Isely568efaa2009-11-25 02:52:06 -03003589 int mode,
Mike Isely4db666c2007-09-08 22:16:27 -03003590 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003591{
3592 int ret;
3593 u16 address;
3594 unsigned int pipe;
3595 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003596 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003597
3598 if (!enable_flag) {
3599 pvr2_trace(PVR2_TRACE_FIRMWARE,
3600 "Cleaning up after CPU firmware fetch");
3601 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003602 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003603 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003604 if (hdw->fw_cpu_flag) {
3605 /* Now release the CPU. It will disconnect
3606 and reconnect later. */
3607 pvr2_hdw_cpureset_assert(hdw,0);
3608 }
Mike Iselyd8554972006-06-26 20:58:46 -03003609 break;
3610 }
3611
Mike Isely568efaa2009-11-25 02:52:06 -03003612 hdw->fw_cpu_flag = (mode != 2);
Mike Isely4db666c2007-09-08 22:16:27 -03003613 if (hdw->fw_cpu_flag) {
Mike Isely568efaa2009-11-25 02:52:06 -03003614 hdw->fw_size = (mode == 1) ? 0x4000 : 0x2000;
Mike Isely4db666c2007-09-08 22:16:27 -03003615 pvr2_trace(PVR2_TRACE_FIRMWARE,
Mike Isely568efaa2009-11-25 02:52:06 -03003616 "Preparing to suck out CPU firmware"
3617 " (size=%u)", hdw->fw_size);
Mike Isely4db666c2007-09-08 22:16:27 -03003618 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3619 if (!hdw->fw_buffer) {
3620 hdw->fw_size = 0;
3621 break;
3622 }
3623
3624 /* We have to hold the CPU during firmware upload. */
3625 pvr2_hdw_cpureset_assert(hdw,1);
3626
3627 /* download the firmware from address 0000-1fff in 2048
3628 (=0x800) bytes chunk. */
3629
3630 pvr2_trace(PVR2_TRACE_FIRMWARE,
3631 "Grabbing CPU firmware");
3632 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3633 for(address = 0; address < hdw->fw_size;
3634 address += 0x800) {
3635 ret = usb_control_msg(hdw->usb_dev,pipe,
3636 0xa0,0xc0,
3637 address,0,
3638 hdw->fw_buffer+address,
3639 0x800,HZ);
3640 if (ret < 0) break;
3641 }
3642
3643 pvr2_trace(PVR2_TRACE_FIRMWARE,
3644 "Done grabbing CPU firmware");
3645 } else {
3646 pvr2_trace(PVR2_TRACE_FIRMWARE,
3647 "Sucking down EEPROM contents");
3648 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3649 if (!hdw->fw_buffer) {
3650 pvr2_trace(PVR2_TRACE_FIRMWARE,
3651 "EEPROM content suck failed.");
3652 break;
3653 }
3654 hdw->fw_size = EEPROM_SIZE;
3655 pvr2_trace(PVR2_TRACE_FIRMWARE,
3656 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003657 }
3658
Mike Iselyd8554972006-06-26 20:58:46 -03003659 } while (0); LOCK_GIVE(hdw->big_lock);
3660}
3661
3662
3663/* Return true if we're in a mode for retrieval CPU firmware */
3664int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3665{
Al Viro5fa12472008-03-29 03:07:38 +00003666 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003667}
3668
3669
3670int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3671 char *buf,unsigned int cnt)
3672{
3673 int ret = -EINVAL;
3674 LOCK_TAKE(hdw->big_lock); do {
3675 if (!buf) break;
3676 if (!cnt) break;
3677
3678 if (!hdw->fw_buffer) {
3679 ret = -EIO;
3680 break;
3681 }
3682
3683 if (offs >= hdw->fw_size) {
3684 pvr2_trace(PVR2_TRACE_FIRMWARE,
3685 "Read firmware data offs=%d EOF",
3686 offs);
3687 ret = 0;
3688 break;
3689 }
3690
3691 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3692
3693 memcpy(buf,hdw->fw_buffer+offs,cnt);
3694
3695 pvr2_trace(PVR2_TRACE_FIRMWARE,
3696 "Read firmware data offs=%d cnt=%d",
3697 offs,cnt);
3698 ret = cnt;
3699 } while (0); LOCK_GIVE(hdw->big_lock);
3700
3701 return ret;
3702}
3703
3704
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003705int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003706 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003707{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003708 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003709 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3710 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3711 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003712 default: return -1;
3713 }
Mike Iselyd8554972006-06-26 20:58:46 -03003714}
3715
3716
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003717/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003718void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003719 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003720{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003721 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003722 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3723 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3724 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003725 default: break;
3726 }
Mike Iselyd8554972006-06-26 20:58:46 -03003727}
3728
3729
David Howells7d12e782006-10-05 14:55:46 +01003730static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003731{
3732 struct pvr2_hdw *hdw = urb->context;
3733 hdw->ctl_write_pend_flag = 0;
3734 if (hdw->ctl_read_pend_flag) return;
3735 complete(&hdw->ctl_done);
3736}
3737
3738
David Howells7d12e782006-10-05 14:55:46 +01003739static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003740{
3741 struct pvr2_hdw *hdw = urb->context;
3742 hdw->ctl_read_pend_flag = 0;
3743 if (hdw->ctl_write_pend_flag) return;
3744 complete(&hdw->ctl_done);
3745}
3746
3747
3748static void pvr2_ctl_timeout(unsigned long data)
3749{
3750 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3751 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3752 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003753 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003754 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003755 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003756 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003757 }
3758}
3759
3760
Mike Iselye61b6fc2006-07-18 22:42:18 -03003761/* Issue a command and get a response from the device. This extended
3762 version includes a probe flag (which if set means that device errors
3763 should not be logged or treated as fatal) and a timeout in jiffies.
3764 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003765static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3766 unsigned int timeout,int probe_fl,
3767 void *write_data,unsigned int write_len,
3768 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003769{
3770 unsigned int idx;
3771 int status = 0;
3772 struct timer_list timer;
3773 if (!hdw->ctl_lock_held) {
3774 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3775 "Attempted to execute control transfer"
3776 " without lock!!");
3777 return -EDEADLK;
3778 }
Mike Isely681c7392007-11-26 01:48:52 -03003779 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003780 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3781 "Attempted to execute control transfer"
3782 " when device not ok");
3783 return -EIO;
3784 }
3785 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3786 if (!probe_fl) {
3787 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3788 "Attempted to execute control transfer"
3789 " when USB is disconnected");
3790 }
3791 return -ENOTTY;
3792 }
3793
3794 /* Ensure that we have sane parameters */
3795 if (!write_data) write_len = 0;
3796 if (!read_data) read_len = 0;
3797 if (write_len > PVR2_CTL_BUFFSIZE) {
3798 pvr2_trace(
3799 PVR2_TRACE_ERROR_LEGS,
3800 "Attempted to execute %d byte"
3801 " control-write transfer (limit=%d)",
3802 write_len,PVR2_CTL_BUFFSIZE);
3803 return -EINVAL;
3804 }
3805 if (read_len > PVR2_CTL_BUFFSIZE) {
3806 pvr2_trace(
3807 PVR2_TRACE_ERROR_LEGS,
3808 "Attempted to execute %d byte"
3809 " control-read transfer (limit=%d)",
3810 write_len,PVR2_CTL_BUFFSIZE);
3811 return -EINVAL;
3812 }
3813 if ((!write_len) && (!read_len)) {
3814 pvr2_trace(
3815 PVR2_TRACE_ERROR_LEGS,
3816 "Attempted to execute null control transfer?");
3817 return -EINVAL;
3818 }
3819
3820
3821 hdw->cmd_debug_state = 1;
3822 if (write_len) {
3823 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3824 } else {
3825 hdw->cmd_debug_code = 0;
3826 }
3827 hdw->cmd_debug_write_len = write_len;
3828 hdw->cmd_debug_read_len = read_len;
3829
3830 /* Initialize common stuff */
3831 init_completion(&hdw->ctl_done);
3832 hdw->ctl_timeout_flag = 0;
3833 hdw->ctl_write_pend_flag = 0;
3834 hdw->ctl_read_pend_flag = 0;
3835 init_timer(&timer);
3836 timer.expires = jiffies + timeout;
3837 timer.data = (unsigned long)hdw;
3838 timer.function = pvr2_ctl_timeout;
3839
3840 if (write_len) {
3841 hdw->cmd_debug_state = 2;
3842 /* Transfer write data to internal buffer */
3843 for (idx = 0; idx < write_len; idx++) {
3844 hdw->ctl_write_buffer[idx] =
3845 ((unsigned char *)write_data)[idx];
3846 }
3847 /* Initiate a write request */
3848 usb_fill_bulk_urb(hdw->ctl_write_urb,
3849 hdw->usb_dev,
3850 usb_sndbulkpipe(hdw->usb_dev,
3851 PVR2_CTL_WRITE_ENDPOINT),
3852 hdw->ctl_write_buffer,
3853 write_len,
3854 pvr2_ctl_write_complete,
3855 hdw);
3856 hdw->ctl_write_urb->actual_length = 0;
3857 hdw->ctl_write_pend_flag = !0;
3858 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3859 if (status < 0) {
3860 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3861 "Failed to submit write-control"
3862 " URB status=%d",status);
3863 hdw->ctl_write_pend_flag = 0;
3864 goto done;
3865 }
3866 }
3867
3868 if (read_len) {
3869 hdw->cmd_debug_state = 3;
3870 memset(hdw->ctl_read_buffer,0x43,read_len);
3871 /* Initiate a read request */
3872 usb_fill_bulk_urb(hdw->ctl_read_urb,
3873 hdw->usb_dev,
3874 usb_rcvbulkpipe(hdw->usb_dev,
3875 PVR2_CTL_READ_ENDPOINT),
3876 hdw->ctl_read_buffer,
3877 read_len,
3878 pvr2_ctl_read_complete,
3879 hdw);
3880 hdw->ctl_read_urb->actual_length = 0;
3881 hdw->ctl_read_pend_flag = !0;
3882 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3883 if (status < 0) {
3884 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3885 "Failed to submit read-control"
3886 " URB status=%d",status);
3887 hdw->ctl_read_pend_flag = 0;
3888 goto done;
3889 }
3890 }
3891
3892 /* Start timer */
3893 add_timer(&timer);
3894
3895 /* Now wait for all I/O to complete */
3896 hdw->cmd_debug_state = 4;
3897 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3898 wait_for_completion(&hdw->ctl_done);
3899 }
3900 hdw->cmd_debug_state = 5;
3901
3902 /* Stop timer */
3903 del_timer_sync(&timer);
3904
3905 hdw->cmd_debug_state = 6;
3906 status = 0;
3907
3908 if (hdw->ctl_timeout_flag) {
3909 status = -ETIMEDOUT;
3910 if (!probe_fl) {
3911 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3912 "Timed out control-write");
3913 }
3914 goto done;
3915 }
3916
3917 if (write_len) {
3918 /* Validate results of write request */
3919 if ((hdw->ctl_write_urb->status != 0) &&
3920 (hdw->ctl_write_urb->status != -ENOENT) &&
3921 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3922 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3923 /* USB subsystem is reporting some kind of failure
3924 on the write */
3925 status = hdw->ctl_write_urb->status;
3926 if (!probe_fl) {
3927 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3928 "control-write URB failure,"
3929 " status=%d",
3930 status);
3931 }
3932 goto done;
3933 }
3934 if (hdw->ctl_write_urb->actual_length < write_len) {
3935 /* Failed to write enough data */
3936 status = -EIO;
3937 if (!probe_fl) {
3938 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3939 "control-write URB short,"
3940 " expected=%d got=%d",
3941 write_len,
3942 hdw->ctl_write_urb->actual_length);
3943 }
3944 goto done;
3945 }
3946 }
3947 if (read_len) {
3948 /* Validate results of read request */
3949 if ((hdw->ctl_read_urb->status != 0) &&
3950 (hdw->ctl_read_urb->status != -ENOENT) &&
3951 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3952 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3953 /* USB subsystem is reporting some kind of failure
3954 on the read */
3955 status = hdw->ctl_read_urb->status;
3956 if (!probe_fl) {
3957 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3958 "control-read URB failure,"
3959 " status=%d",
3960 status);
3961 }
3962 goto done;
3963 }
3964 if (hdw->ctl_read_urb->actual_length < read_len) {
3965 /* Failed to read enough data */
3966 status = -EIO;
3967 if (!probe_fl) {
3968 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3969 "control-read URB short,"
3970 " expected=%d got=%d",
3971 read_len,
3972 hdw->ctl_read_urb->actual_length);
3973 }
3974 goto done;
3975 }
3976 /* Transfer retrieved data out from internal buffer */
3977 for (idx = 0; idx < read_len; idx++) {
3978 ((unsigned char *)read_data)[idx] =
3979 hdw->ctl_read_buffer[idx];
3980 }
3981 }
3982
3983 done:
3984
3985 hdw->cmd_debug_state = 0;
3986 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003987 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003988 }
3989 return status;
3990}
3991
3992
3993int pvr2_send_request(struct pvr2_hdw *hdw,
3994 void *write_data,unsigned int write_len,
3995 void *read_data,unsigned int read_len)
3996{
3997 return pvr2_send_request_ex(hdw,HZ*4,0,
3998 write_data,write_len,
3999 read_data,read_len);
4000}
4001
Mike Isely1c9d10d2008-03-28 05:38:54 -03004002
4003static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
4004{
4005 int ret;
4006 unsigned int cnt = 1;
4007 unsigned int args = 0;
4008 LOCK_TAKE(hdw->ctl_lock);
4009 hdw->cmd_buffer[0] = cmdcode & 0xffu;
4010 args = (cmdcode >> 8) & 0xffu;
4011 args = (args > 2) ? 2 : args;
4012 if (args) {
4013 cnt += args;
4014 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
4015 if (args > 1) {
4016 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
4017 }
4018 }
4019 if (pvrusb2_debug & PVR2_TRACE_INIT) {
4020 unsigned int idx;
4021 unsigned int ccnt,bcnt;
4022 char tbuf[50];
4023 cmdcode &= 0xffu;
4024 bcnt = 0;
4025 ccnt = scnprintf(tbuf+bcnt,
4026 sizeof(tbuf)-bcnt,
4027 "Sending FX2 command 0x%x",cmdcode);
4028 bcnt += ccnt;
4029 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
4030 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
4031 ccnt = scnprintf(tbuf+bcnt,
4032 sizeof(tbuf)-bcnt,
4033 " \"%s\"",
4034 pvr2_fx2cmd_desc[idx].desc);
4035 bcnt += ccnt;
4036 break;
4037 }
4038 }
4039 if (args) {
4040 ccnt = scnprintf(tbuf+bcnt,
4041 sizeof(tbuf)-bcnt,
4042 " (%u",hdw->cmd_buffer[1]);
4043 bcnt += ccnt;
4044 if (args > 1) {
4045 ccnt = scnprintf(tbuf+bcnt,
4046 sizeof(tbuf)-bcnt,
4047 ",%u",hdw->cmd_buffer[2]);
4048 bcnt += ccnt;
4049 }
4050 ccnt = scnprintf(tbuf+bcnt,
4051 sizeof(tbuf)-bcnt,
4052 ")");
4053 bcnt += ccnt;
4054 }
4055 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
4056 }
4057 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
4058 LOCK_GIVE(hdw->ctl_lock);
4059 return ret;
4060}
4061
4062
Mike Iselyd8554972006-06-26 20:58:46 -03004063int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
4064{
4065 int ret;
4066
4067 LOCK_TAKE(hdw->ctl_lock);
4068
Michael Krufky8d364362007-01-22 02:17:55 -03004069 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03004070 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
4071 hdw->cmd_buffer[5] = 0;
4072 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4073 hdw->cmd_buffer[7] = reg & 0xff;
4074
4075
4076 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
4077
4078 LOCK_GIVE(hdw->ctl_lock);
4079
4080 return ret;
4081}
4082
4083
Adrian Bunk07e337e2006-06-30 11:30:20 -03004084static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03004085{
4086 int ret = 0;
4087
4088 LOCK_TAKE(hdw->ctl_lock);
4089
Michael Krufky8d364362007-01-22 02:17:55 -03004090 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03004091 hdw->cmd_buffer[1] = 0;
4092 hdw->cmd_buffer[2] = 0;
4093 hdw->cmd_buffer[3] = 0;
4094 hdw->cmd_buffer[4] = 0;
4095 hdw->cmd_buffer[5] = 0;
4096 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4097 hdw->cmd_buffer[7] = reg & 0xff;
4098
4099 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
4100 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
4101
4102 LOCK_GIVE(hdw->ctl_lock);
4103
4104 return ret;
4105}
4106
4107
Mike Isely681c7392007-11-26 01:48:52 -03004108void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004109{
4110 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03004111 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4112 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03004113 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004114 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03004115 }
Mike Isely681c7392007-11-26 01:48:52 -03004116 hdw->flag_ok = 0;
4117 trace_stbit("flag_ok",hdw->flag_ok);
4118 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03004119}
4120
4121
4122void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
4123{
4124 int ret;
4125 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004126 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05004127 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03004128 ret = usb_reset_device(hdw->usb_dev);
4129 usb_unlock_device(hdw->usb_dev);
4130 } else {
4131 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4132 "Failed to lock USB device ret=%d",ret);
4133 }
4134 if (init_pause_msec) {
4135 pvr2_trace(PVR2_TRACE_INFO,
4136 "Waiting %u msec for hardware to settle",
4137 init_pause_msec);
4138 msleep(init_pause_msec);
4139 }
4140
4141}
4142
4143
4144void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
4145{
Mike Isely68618002010-05-15 00:09:47 -03004146 char *da;
Mike Iselyd8554972006-06-26 20:58:46 -03004147 unsigned int pipe;
4148 int ret;
4149
4150 if (!hdw->usb_dev) return;
4151
Mike Isely68618002010-05-15 00:09:47 -03004152 da = kmalloc(16, GFP_KERNEL);
4153
4154 if (da == NULL) {
4155 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4156 "Unable to allocate memory to control CPU reset");
4157 return;
4158 }
4159
Mike Iselyd8554972006-06-26 20:58:46 -03004160 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
4161
4162 da[0] = val ? 0x01 : 0x00;
4163
4164 /* Write the CPUCS register on the 8051. The lsb of the register
4165 is the reset bit; a 1 asserts reset while a 0 clears it. */
4166 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
4167 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
4168 if (ret < 0) {
4169 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4170 "cpureset_assert(%d) error=%d",val,ret);
4171 pvr2_hdw_render_useless(hdw);
4172 }
Mike Isely68618002010-05-15 00:09:47 -03004173
4174 kfree(da);
Mike Iselyd8554972006-06-26 20:58:46 -03004175}
4176
4177
4178int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4179{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004180 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03004181}
4182
4183
Michael Krufkye1edb192008-04-22 14:45:39 -03004184int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4185{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004186 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03004187}
4188
Mike Isely1c9d10d2008-03-28 05:38:54 -03004189
Michael Krufkye1edb192008-04-22 14:45:39 -03004190int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4191{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004192 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03004193}
4194
Mike Iselyd8554972006-06-26 20:58:46 -03004195
4196int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4197{
Mike Iselyd8554972006-06-26 20:58:46 -03004198 pvr2_trace(PVR2_TRACE_INIT,
4199 "Requesting decoder reset");
Mike Iselyaf78e162009-03-07 00:21:30 -03004200 if (hdw->decoder_client_id) {
4201 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4202 core, reset, 0);
Mike Iselye17d7872009-06-20 14:45:52 -03004203 pvr2_hdw_cx25840_vbi_hack(hdw);
Mike Iselyaf78e162009-03-07 00:21:30 -03004204 return 0;
4205 }
4206 pvr2_trace(PVR2_TRACE_INIT,
4207 "Unable to reset decoder: nothing attached");
4208 return -ENOTTY;
Mike Iselyd8554972006-06-26 20:58:46 -03004209}
4210
4211
Mike Isely62433e32008-04-22 14:45:40 -03004212static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004213{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004214 hdw->flag_ok = !0;
4215 return pvr2_issue_simple_cmd(hdw,
4216 FX2CMD_HCW_DEMOD_RESETIN |
4217 (1 << 8) |
4218 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03004219}
4220
Mike Isely84147f32008-04-22 14:45:40 -03004221
Mike Isely62433e32008-04-22 14:45:40 -03004222static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004223{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004224 hdw->flag_ok = !0;
4225 return pvr2_issue_simple_cmd(hdw,(onoff ?
4226 FX2CMD_ONAIR_DTV_POWER_ON :
4227 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004228}
4229
Mike Isely62433e32008-04-22 14:45:40 -03004230
4231static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4232 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004233{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004234 return pvr2_issue_simple_cmd(hdw,(onoff ?
4235 FX2CMD_ONAIR_DTV_STREAMING_ON :
4236 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004237}
4238
Mike Isely62433e32008-04-22 14:45:40 -03004239
4240static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4241{
4242 int cmode;
4243 /* Compare digital/analog desired setting with current setting. If
4244 they don't match, fix it... */
4245 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4246 if (cmode == hdw->pathway_state) {
4247 /* They match; nothing to do */
4248 return;
4249 }
4250
4251 switch (hdw->hdw_desc->digital_control_scheme) {
4252 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4253 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4254 if (cmode == PVR2_PATHWAY_ANALOG) {
4255 /* If moving to analog mode, also force the decoder
4256 to reset. If no decoder is attached, then it's
4257 ok to ignore this because if/when the decoder
4258 attaches, it will reset itself at that time. */
4259 pvr2_hdw_cmd_decoder_reset(hdw);
4260 }
4261 break;
4262 case PVR2_DIGITAL_SCHEME_ONAIR:
4263 /* Supposedly we should always have the power on whether in
4264 digital or analog mode. But for now do what appears to
4265 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004266 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03004267 break;
4268 default: break;
4269 }
4270
Mike Isely1b9c18c2008-04-22 14:45:41 -03004271 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03004272 hdw->pathway_state = cmode;
4273}
4274
4275
Adrian Bunke9b59f62008-05-10 04:35:24 -03004276static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03004277{
4278 /* change some GPIO data
4279 *
4280 * note: bit d7 of dir appears to control the LED,
4281 * so we shut it off here.
4282 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03004283 */
Mike Isely40381cb2008-04-22 14:45:42 -03004284 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004285 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03004286 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004287 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03004288 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03004289 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03004290}
Mike Iselyc55a97d2008-04-22 14:45:41 -03004291
Mike Isely40381cb2008-04-22 14:45:42 -03004292
4293typedef void (*led_method_func)(struct pvr2_hdw *,int);
4294
4295static led_method_func led_methods[] = {
4296 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4297};
4298
4299
4300/* Toggle LED */
4301static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4302{
4303 unsigned int scheme_id;
4304 led_method_func fp;
4305
4306 if ((!onoff) == (!hdw->led_on)) return;
4307
4308 hdw->led_on = onoff != 0;
4309
4310 scheme_id = hdw->hdw_desc->led_scheme;
4311 if (scheme_id < ARRAY_SIZE(led_methods)) {
4312 fp = led_methods[scheme_id];
4313 } else {
4314 fp = NULL;
4315 }
4316
4317 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03004318}
4319
4320
Mike Iselye61b6fc2006-07-18 22:42:18 -03004321/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004322static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03004323{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004324 int ret;
4325
4326 /* If we're in analog mode, then just issue the usual analog
4327 command. */
4328 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4329 return pvr2_issue_simple_cmd(hdw,
4330 (runFl ?
4331 FX2CMD_STREAMING_ON :
4332 FX2CMD_STREAMING_OFF));
4333 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03004334 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004335
4336 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4337 /* Whoops, we don't know what mode we're in... */
4338 return -EINVAL;
4339 }
4340
4341 /* To get here we have to be in digital mode. The mechanism here
4342 is unfortunately different for different vendors. So we switch
4343 on the device's digital scheme attribute in order to figure out
4344 what to do. */
4345 switch (hdw->hdw_desc->digital_control_scheme) {
4346 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4347 return pvr2_issue_simple_cmd(hdw,
4348 (runFl ?
4349 FX2CMD_HCW_DTV_STREAMING_ON :
4350 FX2CMD_HCW_DTV_STREAMING_OFF));
4351 case PVR2_DIGITAL_SCHEME_ONAIR:
4352 ret = pvr2_issue_simple_cmd(hdw,
4353 (runFl ?
4354 FX2CMD_STREAMING_ON :
4355 FX2CMD_STREAMING_OFF));
4356 if (ret) return ret;
4357 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4358 default:
4359 return -EINVAL;
4360 }
Mike Iselyd8554972006-06-26 20:58:46 -03004361}
4362
4363
Mike Isely62433e32008-04-22 14:45:40 -03004364/* Evaluate whether or not state_pathway_ok can change */
4365static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4366{
4367 if (hdw->state_pathway_ok) {
4368 /* Nothing to do if pathway is already ok */
4369 return 0;
4370 }
4371 if (!hdw->state_pipeline_idle) {
4372 /* Not allowed to change anything if pipeline is not idle */
4373 return 0;
4374 }
4375 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4376 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03004377 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03004378 return !0;
4379}
4380
4381
Mike Isely681c7392007-11-26 01:48:52 -03004382/* Evaluate whether or not state_encoder_ok can change */
4383static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4384{
4385 if (hdw->state_encoder_ok) return 0;
4386 if (hdw->flag_tripped) return 0;
4387 if (hdw->state_encoder_run) return 0;
4388 if (hdw->state_encoder_config) return 0;
4389 if (hdw->state_decoder_run) return 0;
4390 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004391 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4392 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4393 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4394 return 0;
4395 }
4396
Mike Isely681c7392007-11-26 01:48:52 -03004397 if (pvr2_upload_firmware2(hdw) < 0) {
4398 hdw->flag_tripped = !0;
4399 trace_stbit("flag_tripped",hdw->flag_tripped);
4400 return !0;
4401 }
4402 hdw->state_encoder_ok = !0;
4403 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4404 return !0;
4405}
4406
4407
4408/* Evaluate whether or not state_encoder_config can change */
4409static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4410{
4411 if (hdw->state_encoder_config) {
4412 if (hdw->state_encoder_ok) {
4413 if (hdw->state_pipeline_req &&
4414 !hdw->state_pipeline_pause) return 0;
4415 }
4416 hdw->state_encoder_config = 0;
4417 hdw->state_encoder_waitok = 0;
4418 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4419 /* paranoia - solve race if timer just completed */
4420 del_timer_sync(&hdw->encoder_wait_timer);
4421 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004422 if (!hdw->state_pathway_ok ||
4423 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4424 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03004425 !hdw->state_pipeline_idle ||
4426 hdw->state_pipeline_pause ||
4427 !hdw->state_pipeline_req ||
4428 !hdw->state_pipeline_config) {
4429 /* We must reset the enforced wait interval if
4430 anything has happened that might have disturbed
4431 the encoder. This should be a rare case. */
4432 if (timer_pending(&hdw->encoder_wait_timer)) {
4433 del_timer_sync(&hdw->encoder_wait_timer);
4434 }
4435 if (hdw->state_encoder_waitok) {
4436 /* Must clear the state - therefore we did
4437 something to a state bit and must also
4438 return true. */
4439 hdw->state_encoder_waitok = 0;
4440 trace_stbit("state_encoder_waitok",
4441 hdw->state_encoder_waitok);
4442 return !0;
4443 }
4444 return 0;
4445 }
4446 if (!hdw->state_encoder_waitok) {
4447 if (!timer_pending(&hdw->encoder_wait_timer)) {
4448 /* waitok flag wasn't set and timer isn't
4449 running. Check flag once more to avoid
4450 a race then start the timer. This is
4451 the point when we measure out a minimal
4452 quiet interval before doing something to
4453 the encoder. */
4454 if (!hdw->state_encoder_waitok) {
4455 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004456 jiffies +
4457 (HZ * TIME_MSEC_ENCODER_WAIT
4458 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004459 add_timer(&hdw->encoder_wait_timer);
4460 }
4461 }
4462 /* We can't continue until we know we have been
4463 quiet for the interval measured by this
4464 timer. */
4465 return 0;
4466 }
4467 pvr2_encoder_configure(hdw);
4468 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4469 }
4470 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4471 return !0;
4472}
4473
4474
Mike Iselyd913d632008-04-06 04:04:35 -03004475/* Return true if the encoder should not be running. */
4476static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4477{
4478 if (!hdw->state_encoder_ok) {
4479 /* Encoder isn't healthy at the moment, so stop it. */
4480 return !0;
4481 }
4482 if (!hdw->state_pathway_ok) {
4483 /* Mode is not understood at the moment (i.e. it wants to
4484 change), so encoder must be stopped. */
4485 return !0;
4486 }
4487
4488 switch (hdw->pathway_state) {
4489 case PVR2_PATHWAY_ANALOG:
4490 if (!hdw->state_decoder_run) {
4491 /* We're in analog mode and the decoder is not
4492 running; thus the encoder should be stopped as
4493 well. */
4494 return !0;
4495 }
4496 break;
4497 case PVR2_PATHWAY_DIGITAL:
4498 if (hdw->state_encoder_runok) {
4499 /* This is a funny case. We're in digital mode so
4500 really the encoder should be stopped. However
4501 if it really is running, only kill it after
4502 runok has been set. This gives a chance for the
4503 onair quirk to function (encoder must run
4504 briefly first, at least once, before onair
4505 digital streaming can work). */
4506 return !0;
4507 }
4508 break;
4509 default:
4510 /* Unknown mode; so encoder should be stopped. */
4511 return !0;
4512 }
4513
4514 /* If we get here, we haven't found a reason to stop the
4515 encoder. */
4516 return 0;
4517}
4518
4519
4520/* Return true if the encoder should be running. */
4521static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4522{
4523 if (!hdw->state_encoder_ok) {
4524 /* Don't run the encoder if it isn't healthy... */
4525 return 0;
4526 }
4527 if (!hdw->state_pathway_ok) {
4528 /* Don't run the encoder if we don't (yet) know what mode
4529 we need to be in... */
4530 return 0;
4531 }
4532
4533 switch (hdw->pathway_state) {
4534 case PVR2_PATHWAY_ANALOG:
Mike Isely6e931372010-02-06 02:10:38 -03004535 if (hdw->state_decoder_run && hdw->state_decoder_ready) {
Mike Iselyd913d632008-04-06 04:04:35 -03004536 /* In analog mode, if the decoder is running, then
4537 run the encoder. */
4538 return !0;
4539 }
4540 break;
4541 case PVR2_PATHWAY_DIGITAL:
4542 if ((hdw->hdw_desc->digital_control_scheme ==
4543 PVR2_DIGITAL_SCHEME_ONAIR) &&
4544 !hdw->state_encoder_runok) {
4545 /* This is a quirk. OnAir hardware won't stream
4546 digital until the encoder has been run at least
4547 once, for a minimal period of time (empiricially
4548 measured to be 1/4 second). So if we're on
4549 OnAir hardware and the encoder has never been
4550 run at all, then start the encoder. Normal
4551 state machine logic in the driver will
4552 automatically handle the remaining bits. */
4553 return !0;
4554 }
4555 break;
4556 default:
4557 /* For completeness (unknown mode; encoder won't run ever) */
4558 break;
4559 }
4560 /* If we get here, then we haven't found any reason to run the
4561 encoder, so don't run it. */
4562 return 0;
4563}
4564
4565
Mike Isely681c7392007-11-26 01:48:52 -03004566/* Evaluate whether or not state_encoder_run can change */
4567static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4568{
4569 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004570 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004571 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004572 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004573 if (pvr2_encoder_stop(hdw) < 0) return !0;
4574 }
4575 hdw->state_encoder_run = 0;
4576 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004577 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004578 if (pvr2_encoder_start(hdw) < 0) return !0;
4579 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004580 if (!hdw->state_encoder_runok) {
4581 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004582 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004583 add_timer(&hdw->encoder_run_timer);
4584 }
Mike Isely681c7392007-11-26 01:48:52 -03004585 }
4586 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4587 return !0;
4588}
4589
4590
4591/* Timeout function for quiescent timer. */
4592static void pvr2_hdw_quiescent_timeout(unsigned long data)
4593{
4594 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4595 hdw->state_decoder_quiescent = !0;
4596 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4597 hdw->state_stale = !0;
4598 queue_work(hdw->workqueue,&hdw->workpoll);
4599}
4600
4601
Mike Isely6e931372010-02-06 02:10:38 -03004602/* Timeout function for decoder stabilization timer. */
4603static void pvr2_hdw_decoder_stabilization_timeout(unsigned long data)
4604{
4605 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4606 hdw->state_decoder_ready = !0;
4607 trace_stbit("state_decoder_ready", hdw->state_decoder_ready);
4608 hdw->state_stale = !0;
4609 queue_work(hdw->workqueue, &hdw->workpoll);
4610}
4611
4612
Mike Isely681c7392007-11-26 01:48:52 -03004613/* Timeout function for encoder wait timer. */
4614static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4615{
4616 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4617 hdw->state_encoder_waitok = !0;
4618 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4619 hdw->state_stale = !0;
4620 queue_work(hdw->workqueue,&hdw->workpoll);
4621}
4622
4623
Mike Iselyd913d632008-04-06 04:04:35 -03004624/* Timeout function for encoder run timer. */
4625static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4626{
4627 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4628 if (!hdw->state_encoder_runok) {
4629 hdw->state_encoder_runok = !0;
4630 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4631 hdw->state_stale = !0;
4632 queue_work(hdw->workqueue,&hdw->workpoll);
4633 }
4634}
4635
4636
Mike Isely681c7392007-11-26 01:48:52 -03004637/* Evaluate whether or not state_decoder_run can change */
4638static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4639{
4640 if (hdw->state_decoder_run) {
4641 if (hdw->state_encoder_ok) {
4642 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004643 !hdw->state_pipeline_pause &&
4644 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004645 }
4646 if (!hdw->flag_decoder_missed) {
4647 pvr2_decoder_enable(hdw,0);
4648 }
4649 hdw->state_decoder_quiescent = 0;
4650 hdw->state_decoder_run = 0;
Mike Isely6e931372010-02-06 02:10:38 -03004651 /* paranoia - solve race if timer(s) just completed */
Mike Isely681c7392007-11-26 01:48:52 -03004652 del_timer_sync(&hdw->quiescent_timer);
Mike Isely6e931372010-02-06 02:10:38 -03004653 /* Kill the stabilization timer, in case we're killing the
4654 encoder before the previous stabilization interval has
4655 been properly timed. */
4656 del_timer_sync(&hdw->decoder_stabilization_timer);
4657 hdw->state_decoder_ready = 0;
Mike Isely681c7392007-11-26 01:48:52 -03004658 } else {
4659 if (!hdw->state_decoder_quiescent) {
4660 if (!timer_pending(&hdw->quiescent_timer)) {
4661 /* We don't do something about the
4662 quiescent timer until right here because
4663 we also want to catch cases where the
4664 decoder was already not running (like
4665 after initialization) as opposed to
4666 knowing that we had just stopped it.
4667 The second flag check is here to cover a
4668 race - the timer could have run and set
4669 this flag just after the previous check
4670 but before we did the pending check. */
4671 if (!hdw->state_decoder_quiescent) {
4672 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004673 jiffies +
4674 (HZ * TIME_MSEC_DECODER_WAIT
4675 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004676 add_timer(&hdw->quiescent_timer);
4677 }
4678 }
4679 /* Don't allow decoder to start again until it has
4680 been quiesced first. This little detail should
4681 hopefully further stabilize the encoder. */
4682 return 0;
4683 }
Mike Isely62433e32008-04-22 14:45:40 -03004684 if (!hdw->state_pathway_ok ||
4685 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4686 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004687 hdw->state_pipeline_pause ||
4688 !hdw->state_pipeline_config ||
4689 !hdw->state_encoder_config ||
4690 !hdw->state_encoder_ok) return 0;
4691 del_timer_sync(&hdw->quiescent_timer);
4692 if (hdw->flag_decoder_missed) return 0;
4693 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4694 hdw->state_decoder_quiescent = 0;
Mike Isely6e931372010-02-06 02:10:38 -03004695 hdw->state_decoder_ready = 0;
Mike Isely681c7392007-11-26 01:48:52 -03004696 hdw->state_decoder_run = !0;
Mike Iselyfb640222010-02-06 02:17:17 -03004697 if (hdw->decoder_client_id == PVR2_CLIENT_ID_SAA7115) {
4698 hdw->decoder_stabilization_timer.expires =
4699 jiffies +
4700 (HZ * TIME_MSEC_DECODER_STABILIZATION_WAIT /
4701 1000);
4702 add_timer(&hdw->decoder_stabilization_timer);
4703 } else {
4704 hdw->state_decoder_ready = !0;
4705 }
Mike Isely681c7392007-11-26 01:48:52 -03004706 }
4707 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4708 trace_stbit("state_decoder_run",hdw->state_decoder_run);
Mike Isely6e931372010-02-06 02:10:38 -03004709 trace_stbit("state_decoder_ready", hdw->state_decoder_ready);
Mike Isely681c7392007-11-26 01:48:52 -03004710 return !0;
4711}
4712
4713
4714/* Evaluate whether or not state_usbstream_run can change */
4715static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4716{
4717 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004718 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004719 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004720 fl = (hdw->state_encoder_ok &&
4721 hdw->state_encoder_run);
4722 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4723 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4724 fl = hdw->state_encoder_ok;
4725 }
4726 if (fl &&
4727 hdw->state_pipeline_req &&
4728 !hdw->state_pipeline_pause &&
4729 hdw->state_pathway_ok) {
4730 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004731 }
4732 pvr2_hdw_cmd_usbstream(hdw,0);
4733 hdw->state_usbstream_run = 0;
4734 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004735 if (!hdw->state_pipeline_req ||
4736 hdw->state_pipeline_pause ||
4737 !hdw->state_pathway_ok) return 0;
4738 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4739 if (!hdw->state_encoder_ok ||
4740 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004741 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4742 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4743 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004744 if (hdw->state_encoder_run) return 0;
4745 if (hdw->hdw_desc->digital_control_scheme ==
4746 PVR2_DIGITAL_SCHEME_ONAIR) {
4747 /* OnAir digital receivers won't stream
4748 unless the analog encoder has run first.
4749 Why? I have no idea. But don't even
4750 try until we know the analog side is
4751 known to have run. */
4752 if (!hdw->state_encoder_runok) return 0;
4753 }
Mike Isely62433e32008-04-22 14:45:40 -03004754 }
Mike Isely681c7392007-11-26 01:48:52 -03004755 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4756 hdw->state_usbstream_run = !0;
4757 }
4758 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4759 return !0;
4760}
4761
4762
4763/* Attempt to configure pipeline, if needed */
4764static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4765{
4766 if (hdw->state_pipeline_config ||
4767 hdw->state_pipeline_pause) return 0;
4768 pvr2_hdw_commit_execute(hdw);
4769 return !0;
4770}
4771
4772
4773/* Update pipeline idle and pipeline pause tracking states based on other
4774 inputs. This must be called whenever the other relevant inputs have
4775 changed. */
4776static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4777{
4778 unsigned int st;
4779 int updatedFl = 0;
4780 /* Update pipeline state */
4781 st = !(hdw->state_encoder_run ||
4782 hdw->state_decoder_run ||
4783 hdw->state_usbstream_run ||
4784 (!hdw->state_decoder_quiescent));
4785 if (!st != !hdw->state_pipeline_idle) {
4786 hdw->state_pipeline_idle = st;
4787 updatedFl = !0;
4788 }
4789 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4790 hdw->state_pipeline_pause = 0;
4791 updatedFl = !0;
4792 }
4793 return updatedFl;
4794}
4795
4796
4797typedef int (*state_eval_func)(struct pvr2_hdw *);
4798
4799/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004800static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004801 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004802 state_eval_pipeline_config,
4803 state_eval_encoder_ok,
4804 state_eval_encoder_config,
4805 state_eval_decoder_run,
4806 state_eval_encoder_run,
4807 state_eval_usbstream_run,
4808};
4809
4810
4811/* Process various states and return true if we did anything interesting. */
4812static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4813{
4814 unsigned int i;
4815 int state_updated = 0;
4816 int check_flag;
4817
4818 if (!hdw->state_stale) return 0;
4819 if ((hdw->fw1_state != FW1_STATE_OK) ||
4820 !hdw->flag_ok) {
4821 hdw->state_stale = 0;
4822 return !0;
4823 }
4824 /* This loop is the heart of the entire driver. It keeps trying to
4825 evaluate various bits of driver state until nothing changes for
4826 one full iteration. Each "bit of state" tracks some global
4827 aspect of the driver, e.g. whether decoder should run, if
4828 pipeline is configured, usb streaming is on, etc. We separately
4829 evaluate each of those questions based on other driver state to
4830 arrive at the correct running configuration. */
4831 do {
4832 check_flag = 0;
4833 state_update_pipeline_state(hdw);
4834 /* Iterate over each bit of state */
4835 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4836 if ((*eval_funcs[i])(hdw)) {
4837 check_flag = !0;
4838 state_updated = !0;
4839 state_update_pipeline_state(hdw);
4840 }
4841 }
4842 } while (check_flag && hdw->flag_ok);
4843 hdw->state_stale = 0;
4844 trace_stbit("state_stale",hdw->state_stale);
4845 return state_updated;
4846}
4847
4848
Mike Isely1cb03b72008-04-21 03:47:43 -03004849static unsigned int print_input_mask(unsigned int msk,
4850 char *buf,unsigned int acnt)
4851{
4852 unsigned int idx,ccnt;
4853 unsigned int tcnt = 0;
4854 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4855 if (!((1 << idx) & msk)) continue;
4856 ccnt = scnprintf(buf+tcnt,
4857 acnt-tcnt,
4858 "%s%s",
4859 (tcnt ? ", " : ""),
4860 control_values_input[idx]);
4861 tcnt += ccnt;
4862 }
4863 return tcnt;
4864}
4865
4866
Mike Isely62433e32008-04-22 14:45:40 -03004867static const char *pvr2_pathway_state_name(int id)
4868{
4869 switch (id) {
4870 case PVR2_PATHWAY_ANALOG: return "analog";
4871 case PVR2_PATHWAY_DIGITAL: return "digital";
4872 default: return "unknown";
4873 }
4874}
4875
4876
Mike Isely681c7392007-11-26 01:48:52 -03004877static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4878 char *buf,unsigned int acnt)
4879{
4880 switch (which) {
4881 case 0:
4882 return scnprintf(
4883 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004884 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004885 (hdw->flag_ok ? " <ok>" : " <fail>"),
4886 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4887 (hdw->flag_disconnected ? " <disconnected>" :
4888 " <connected>"),
4889 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004890 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4891 pvr2_pathway_state_name(hdw->pathway_state));
4892
Mike Isely681c7392007-11-26 01:48:52 -03004893 case 1:
4894 return scnprintf(
4895 buf,acnt,
4896 "pipeline:%s%s%s%s",
4897 (hdw->state_pipeline_idle ? " <idle>" : ""),
4898 (hdw->state_pipeline_config ?
4899 " <configok>" : " <stale>"),
4900 (hdw->state_pipeline_req ? " <req>" : ""),
4901 (hdw->state_pipeline_pause ? " <pause>" : ""));
4902 case 2:
4903 return scnprintf(
4904 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004905 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004906 (hdw->state_decoder_run ?
Mike Isely6e931372010-02-06 02:10:38 -03004907 (hdw->state_decoder_ready ?
4908 "<decode:run>" : " <decode:start>") :
Mike Isely681c7392007-11-26 01:48:52 -03004909 (hdw->state_decoder_quiescent ?
4910 "" : " <decode:stop>")),
4911 (hdw->state_decoder_quiescent ?
4912 " <decode:quiescent>" : ""),
4913 (hdw->state_encoder_ok ?
4914 "" : " <encode:init>"),
4915 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004916 (hdw->state_encoder_runok ?
4917 " <encode:run>" :
4918 " <encode:firstrun>") :
4919 (hdw->state_encoder_runok ?
4920 " <encode:stop>" :
4921 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004922 (hdw->state_encoder_config ?
4923 " <encode:configok>" :
4924 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004925 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004926 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004927 " <usb:run>" : " <usb:stop>"),
4928 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004929 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004930 case 3:
4931 return scnprintf(
4932 buf,acnt,
4933 "state: %s",
4934 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004935 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004936 unsigned int tcnt = 0;
4937 unsigned int ccnt;
4938
4939 ccnt = scnprintf(buf,
4940 acnt,
4941 "Hardware supported inputs: ");
4942 tcnt += ccnt;
4943 tcnt += print_input_mask(hdw->input_avail_mask,
4944 buf+tcnt,
4945 acnt-tcnt);
4946 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4947 ccnt = scnprintf(buf+tcnt,
4948 acnt-tcnt,
4949 "; allowed inputs: ");
4950 tcnt += ccnt;
4951 tcnt += print_input_mask(hdw->input_allowed_mask,
4952 buf+tcnt,
4953 acnt-tcnt);
4954 }
4955 return tcnt;
4956 }
4957 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004958 struct pvr2_stream_stats stats;
4959 if (!hdw->vid_stream) break;
4960 pvr2_stream_get_stats(hdw->vid_stream,
4961 &stats,
4962 0);
4963 return scnprintf(
4964 buf,acnt,
4965 "Bytes streamed=%u"
4966 " URBs: queued=%u idle=%u ready=%u"
4967 " processed=%u failed=%u",
4968 stats.bytes_processed,
4969 stats.buffers_in_queue,
4970 stats.buffers_in_idle,
4971 stats.buffers_in_ready,
4972 stats.buffers_processed,
4973 stats.buffers_failed);
4974 }
Mike Isely27eab382009-04-06 01:51:38 -03004975 case 6: {
4976 unsigned int id = hdw->ir_scheme_active;
4977 return scnprintf(buf, acnt, "ir scheme: id=%d %s", id,
4978 (id >= ARRAY_SIZE(ir_scheme_names) ?
4979 "?" : ir_scheme_names[id]));
4980 }
Mike Isely681c7392007-11-26 01:48:52 -03004981 default: break;
4982 }
4983 return 0;
4984}
4985
4986
Mike Isely2eb563b2009-03-08 18:25:46 -03004987/* Generate report containing info about attached sub-devices and attached
4988 i2c clients, including an indication of which attached i2c clients are
4989 actually sub-devices. */
4990static unsigned int pvr2_hdw_report_clients(struct pvr2_hdw *hdw,
4991 char *buf, unsigned int acnt)
4992{
4993 struct v4l2_subdev *sd;
4994 unsigned int tcnt = 0;
4995 unsigned int ccnt;
4996 struct i2c_client *client;
Mike Isely2eb563b2009-03-08 18:25:46 -03004997 const char *p;
4998 unsigned int id;
4999
Jean Delvarefa7ce76422009-05-02 00:22:27 -03005000 ccnt = scnprintf(buf, acnt, "Associated v4l2-subdev drivers and I2C clients:\n");
Mike Isely2eb563b2009-03-08 18:25:46 -03005001 tcnt += ccnt;
5002 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
5003 id = sd->grp_id;
5004 p = NULL;
5005 if (id < ARRAY_SIZE(module_names)) p = module_names[id];
5006 if (p) {
Jean Delvarefa7ce76422009-05-02 00:22:27 -03005007 ccnt = scnprintf(buf + tcnt, acnt - tcnt, " %s:", p);
Mike Isely2eb563b2009-03-08 18:25:46 -03005008 tcnt += ccnt;
5009 } else {
5010 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
Jean Delvarefa7ce76422009-05-02 00:22:27 -03005011 " (unknown id=%u):", id);
5012 tcnt += ccnt;
5013 }
5014 client = v4l2_get_subdevdata(sd);
5015 if (client) {
5016 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
5017 " %s @ %02x\n", client->name,
5018 client->addr);
5019 tcnt += ccnt;
5020 } else {
5021 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
5022 " no i2c client\n");
Mike Isely2eb563b2009-03-08 18:25:46 -03005023 tcnt += ccnt;
5024 }
5025 }
Mike Isely2eb563b2009-03-08 18:25:46 -03005026 return tcnt;
5027}
5028
5029
Mike Isely681c7392007-11-26 01:48:52 -03005030unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
5031 char *buf,unsigned int acnt)
5032{
5033 unsigned int bcnt,ccnt,idx;
5034 bcnt = 0;
5035 LOCK_TAKE(hdw->big_lock);
5036 for (idx = 0; ; idx++) {
5037 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
5038 if (!ccnt) break;
5039 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
5040 if (!acnt) break;
5041 buf[0] = '\n'; ccnt = 1;
5042 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
5043 }
Mike Isely2eb563b2009-03-08 18:25:46 -03005044 ccnt = pvr2_hdw_report_clients(hdw, buf, acnt);
5045 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
Mike Isely681c7392007-11-26 01:48:52 -03005046 LOCK_GIVE(hdw->big_lock);
5047 return bcnt;
5048}
5049
5050
5051static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
5052{
Mike Isely2eb563b2009-03-08 18:25:46 -03005053 char buf[256];
5054 unsigned int idx, ccnt;
5055 unsigned int lcnt, ucnt;
Mike Isely681c7392007-11-26 01:48:52 -03005056
5057 for (idx = 0; ; idx++) {
5058 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
5059 if (!ccnt) break;
5060 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
5061 }
Mike Isely2eb563b2009-03-08 18:25:46 -03005062 ccnt = pvr2_hdw_report_clients(hdw, buf, sizeof(buf));
5063 ucnt = 0;
5064 while (ucnt < ccnt) {
5065 lcnt = 0;
5066 while ((lcnt + ucnt < ccnt) && (buf[lcnt + ucnt] != '\n')) {
5067 lcnt++;
5068 }
5069 printk(KERN_INFO "%s %.*s\n", hdw->name, lcnt, buf + ucnt);
5070 ucnt += lcnt + 1;
5071 }
Mike Isely681c7392007-11-26 01:48:52 -03005072}
5073
5074
5075/* Evaluate and update the driver's current state, taking various actions
5076 as appropriate for the update. */
5077static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
5078{
5079 unsigned int st;
5080 int state_updated = 0;
5081 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03005082 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03005083
5084 pvr2_trace(PVR2_TRACE_STBITS,
5085 "Drive state check START");
5086 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
5087 pvr2_hdw_state_log_state(hdw);
5088 }
5089
5090 /* Process all state and get back over disposition */
5091 state_updated = pvr2_hdw_state_update(hdw);
5092
Mike Isely1b9c18c2008-04-22 14:45:41 -03005093 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
5094
Mike Isely681c7392007-11-26 01:48:52 -03005095 /* Update master state based upon all other states. */
5096 if (!hdw->flag_ok) {
5097 st = PVR2_STATE_DEAD;
5098 } else if (hdw->fw1_state != FW1_STATE_OK) {
5099 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03005100 } else if ((analog_mode ||
5101 hdw->hdw_desc->flag_digital_requires_cx23416) &&
5102 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03005103 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03005104 } else if (hdw->flag_tripped ||
5105 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03005106 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03005107 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03005108 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03005109 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03005110 st = PVR2_STATE_RUN;
5111 } else {
5112 st = PVR2_STATE_READY;
5113 }
5114 if (hdw->master_state != st) {
5115 pvr2_trace(PVR2_TRACE_STATE,
5116 "Device state change from %s to %s",
5117 pvr2_get_state_name(hdw->master_state),
5118 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03005119 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03005120 hdw->master_state = st;
5121 state_updated = !0;
5122 callback_flag = !0;
5123 }
5124 if (state_updated) {
5125 /* Trigger anyone waiting on any state changes here. */
5126 wake_up(&hdw->state_wait_data);
5127 }
5128
5129 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
5130 pvr2_hdw_state_log_state(hdw);
5131 }
5132 pvr2_trace(PVR2_TRACE_STBITS,
5133 "Drive state check DONE callback=%d",callback_flag);
5134
5135 return callback_flag;
5136}
5137
5138
5139/* Cause kernel thread to check / update driver state */
5140static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
5141{
5142 if (hdw->state_stale) return;
5143 hdw->state_stale = !0;
5144 trace_stbit("state_stale",hdw->state_stale);
5145 queue_work(hdw->workqueue,&hdw->workpoll);
5146}
5147
5148
Mike Iselyd8554972006-06-26 20:58:46 -03005149int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
5150{
5151 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
5152}
5153
5154
5155int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
5156{
5157 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
5158}
5159
5160
5161int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
5162{
5163 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
5164}
5165
5166
5167int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
5168{
5169 u32 cval,nval;
5170 int ret;
5171 if (~msk) {
5172 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
5173 if (ret) return ret;
5174 nval = (cval & ~msk) | (val & msk);
5175 pvr2_trace(PVR2_TRACE_GPIO,
5176 "GPIO direction changing 0x%x:0x%x"
5177 " from 0x%x to 0x%x",
5178 msk,val,cval,nval);
5179 } else {
5180 nval = val;
5181 pvr2_trace(PVR2_TRACE_GPIO,
5182 "GPIO direction changing to 0x%x",nval);
5183 }
5184 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
5185}
5186
5187
5188int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
5189{
5190 u32 cval,nval;
5191 int ret;
5192 if (~msk) {
5193 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
5194 if (ret) return ret;
5195 nval = (cval & ~msk) | (val & msk);
5196 pvr2_trace(PVR2_TRACE_GPIO,
5197 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
5198 msk,val,cval,nval);
5199 } else {
5200 nval = val;
5201 pvr2_trace(PVR2_TRACE_GPIO,
5202 "GPIO output changing to 0x%x",nval);
5203 }
5204 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
5205}
5206
5207
Mike Iselya51f5002009-03-06 23:30:37 -03005208void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
5209{
Mike Isely40f07112009-03-07 00:08:17 -03005210 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
5211 memset(vtp, 0, sizeof(*vtp));
Hans Verkuil50e9efd2011-06-12 06:39:52 -03005212 vtp->type = (hdw->input_val == PVR2_CVAL_INPUT_RADIO) ?
5213 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mike Isely2641df32009-03-07 00:13:25 -03005214 hdw->tuner_signal_stale = 0;
Mike Isely40f07112009-03-07 00:08:17 -03005215 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
5216 using v4l2-subdev - therefore we can't support that AT ALL right
5217 now. (Of course, no sub-drivers seem to implement it either.
5218 But now it's a a chicken and egg problem...) */
Mike Isely4cfe33192011-02-13 17:32:47 -03005219 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner, vtp);
Mike Isely2641df32009-03-07 00:13:25 -03005220 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
Mike Isely40f07112009-03-07 00:08:17 -03005221 " type=%u strength=%u audio=0x%x cap=0x%x"
5222 " low=%u hi=%u",
5223 vtp->type,
5224 vtp->signal, vtp->rxsubchans, vtp->capability,
5225 vtp->rangelow, vtp->rangehigh);
Mike Isely2641df32009-03-07 00:13:25 -03005226
5227 /* We have to do this to avoid getting into constant polling if
5228 there's nobody to answer a poll of cropcap info. */
5229 hdw->cropcap_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005230}
5231
5232
Mike Isely7fb20fa2008-04-22 14:45:37 -03005233unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5234{
5235 return hdw->input_avail_mask;
5236}
5237
5238
Mike Isely1cb03b72008-04-21 03:47:43 -03005239unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5240{
5241 return hdw->input_allowed_mask;
5242}
5243
5244
5245static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5246{
5247 if (hdw->input_val != v) {
5248 hdw->input_val = v;
5249 hdw->input_dirty = !0;
5250 }
5251
5252 /* Handle side effects - if we switch to a mode that needs the RF
5253 tuner, then select the right frequency choice as well and mark
5254 it dirty. */
5255 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5256 hdw->freqSelector = 0;
5257 hdw->freqDirty = !0;
5258 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5259 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5260 hdw->freqSelector = 1;
5261 hdw->freqDirty = !0;
5262 }
5263 return 0;
5264}
5265
5266
5267int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5268 unsigned int change_mask,
5269 unsigned int change_val)
5270{
5271 int ret = 0;
5272 unsigned int nv,m,idx;
5273 LOCK_TAKE(hdw->big_lock);
5274 do {
5275 nv = hdw->input_allowed_mask & ~change_mask;
5276 nv |= (change_val & change_mask);
5277 nv &= hdw->input_avail_mask;
5278 if (!nv) {
5279 /* No legal modes left; return error instead. */
5280 ret = -EPERM;
5281 break;
5282 }
5283 hdw->input_allowed_mask = nv;
5284 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5285 /* Current mode is still in the allowed mask, so
5286 we're done. */
5287 break;
5288 }
5289 /* Select and switch to a mode that is still in the allowed
5290 mask */
5291 if (!hdw->input_allowed_mask) {
5292 /* Nothing legal; give up */
5293 break;
5294 }
5295 m = hdw->input_allowed_mask;
5296 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5297 if (!((1 << idx) & m)) continue;
5298 pvr2_hdw_set_input(hdw,idx);
5299 break;
5300 }
5301 } while (0);
5302 LOCK_GIVE(hdw->big_lock);
5303 return ret;
5304}
5305
5306
Mike Iselye61b6fc2006-07-18 22:42:18 -03005307/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03005308static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03005309{
5310 int result;
5311 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03005312 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03005313 result = pvr2_send_request(hdw,
5314 hdw->cmd_buffer,1,
5315 hdw->cmd_buffer,1);
5316 if (result < 0) break;
5317 result = hdw->cmd_buffer[0];
5318 } while(0); LOCK_GIVE(hdw->ctl_lock);
5319 return result;
5320}
5321
5322
Mike Isely32ffa9a2006-09-23 22:26:52 -03005323int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005324 struct v4l2_dbg_match *match, u64 reg_id,
5325 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03005326{
5327#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005328 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03005329 int stat = 0;
5330 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005331
Mike Isely201f5c92007-01-28 16:08:36 -03005332 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5333
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005334 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005335 req.reg = reg_id;
5336 if (setFl) req.val = *val_ptr;
Mike Iselyd8f5b9b2009-03-07 00:05:00 -03005337 /* It would be nice to know if a sub-device answered the request */
5338 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5339 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03005340 if (okFl) {
5341 return stat;
5342 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005343 return -EINVAL;
5344#else
5345 return -ENOSYS;
5346#endif
5347}
5348
5349
Mike Iselyd8554972006-06-26 20:58:46 -03005350/*
5351 Stuff for Emacs to see, in order to encourage consistent editing style:
5352 *** Local Variables: ***
5353 *** mode: c ***
5354 *** fill-column: 75 ***
5355 *** tab-width: 8 ***
5356 *** c-basic-offset: 8 ***
5357 *** End: ***
5358 */