blob: a7d636f3f9b64594709fba99bc0f6828263f9c08 [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>
24#include <linux/firmware.h>
Mike Iselyd8554972006-06-26 20:58:46 -030025#include <linux/videodev2.h>
Mike Isely32ffa9a2006-09-23 22:26:52 -030026#include <media/v4l2-common.h>
Mike Iselyd8554972006-06-26 20:58:46 -030027#include "pvrusb2.h"
28#include "pvrusb2-std.h"
29#include "pvrusb2-util.h"
30#include "pvrusb2-hdw.h"
31#include "pvrusb2-i2c-core.h"
32#include "pvrusb2-tuner.h"
33#include "pvrusb2-eeprom.h"
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-encoder.h"
36#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030037#include "pvrusb2-fx2-cmd.h"
Mike Iselyd8554972006-06-26 20:58:46 -030038
Mike Isely1bde0282006-12-27 23:30:13 -030039#define TV_MIN_FREQ 55250000L
40#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030041
Mike Isely83ce57a2008-05-26 05:51:57 -030042/* This defines a minimum interval that the decoder must remain quiet
43 before we are allowed to start it running. */
44#define TIME_MSEC_DECODER_WAIT 50
45
46/* This defines a minimum interval that the encoder must remain quiet
Mike Iselyfa98e592008-05-26 05:54:24 -030047 before we are allowed to configure it. I had this originally set to
48 50msec, but Martin Dauskardt <martin.dauskardt@gmx.de> reports that
49 things work better when it's set to 100msec. */
50#define TIME_MSEC_ENCODER_WAIT 100
Mike Isely83ce57a2008-05-26 05:51:57 -030051
52/* This defines the minimum interval that the encoder must successfully run
53 before we consider that the encoder has run at least once since its
54 firmware has been loaded. This measurement is in important for cases
55 where we can't do something until we know that the encoder has been run
56 at least once. */
57#define TIME_MSEC_ENCODER_OK 250
58
Mike Iselya0fd1cb2006-06-30 11:35:28 -030059static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030060static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030061
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030062static int ctlchg;
Mike Iselyd8554972006-06-26 20:58:46 -030063static int initusbreset = 1;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030064static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030065static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
66static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
67static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030068static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030069
70module_param(ctlchg, int, S_IRUGO|S_IWUSR);
71MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
72module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
73MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
74module_param(initusbreset, int, S_IRUGO|S_IWUSR);
75MODULE_PARM_DESC(initusbreset, "Do USB reset device on probe");
76module_param(procreload, int, S_IRUGO|S_IWUSR);
77MODULE_PARM_DESC(procreload,
78 "Attempt init failure recovery with firmware reload");
79module_param_array(tuner, int, NULL, 0444);
80MODULE_PARM_DESC(tuner,"specify installed tuner type");
81module_param_array(video_std, int, NULL, 0444);
82MODULE_PARM_DESC(video_std,"specify initial video standard");
83module_param_array(tolerance, int, NULL, 0444);
84MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
85
Michael Krufky5a4f5da62008-05-11 16:37:50 -030086/* US Broadcast channel 7 (175.25 MHz) */
87static int default_tv_freq = 175250000L;
88/* 104.3 MHz, a usable FM station for my area */
89static int default_radio_freq = 104300000L;
90
91module_param_named(tv_freq, default_tv_freq, int, 0444);
92MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
93module_param_named(radio_freq, default_radio_freq, int, 0444);
94MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
95
Mike Iselyd8554972006-06-26 20:58:46 -030096#define PVR2_CTL_WRITE_ENDPOINT 0x01
97#define PVR2_CTL_READ_ENDPOINT 0x81
98
99#define PVR2_GPIO_IN 0x9008
100#define PVR2_GPIO_OUT 0x900c
101#define PVR2_GPIO_DIR 0x9020
102
103#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
104
105#define PVR2_FIRMWARE_ENDPOINT 0x02
106
107/* size of a firmware chunk */
108#define FIRMWARE_CHUNK_SIZE 0x2000
109
Mike Iselyb30d2442006-06-25 20:05:01 -0300110/* Define the list of additional controls we'll dynamically construct based
111 on query of the cx2341x module. */
112struct pvr2_mpeg_ids {
113 const char *strid;
114 int id;
115};
116static const struct pvr2_mpeg_ids mpeg_ids[] = {
117 {
118 .strid = "audio_layer",
119 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
120 },{
121 .strid = "audio_bitrate",
122 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
123 },{
124 /* Already using audio_mode elsewhere :-( */
125 .strid = "mpeg_audio_mode",
126 .id = V4L2_CID_MPEG_AUDIO_MODE,
127 },{
128 .strid = "mpeg_audio_mode_extension",
129 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
130 },{
131 .strid = "audio_emphasis",
132 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
133 },{
134 .strid = "audio_crc",
135 .id = V4L2_CID_MPEG_AUDIO_CRC,
136 },{
137 .strid = "video_aspect",
138 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
139 },{
140 .strid = "video_b_frames",
141 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
142 },{
143 .strid = "video_gop_size",
144 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
145 },{
146 .strid = "video_gop_closure",
147 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
148 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300149 .strid = "video_bitrate_mode",
150 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
151 },{
152 .strid = "video_bitrate",
153 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
154 },{
155 .strid = "video_bitrate_peak",
156 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
157 },{
158 .strid = "video_temporal_decimation",
159 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
160 },{
161 .strid = "stream_type",
162 .id = V4L2_CID_MPEG_STREAM_TYPE,
163 },{
164 .strid = "video_spatial_filter_mode",
165 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
166 },{
167 .strid = "video_spatial_filter",
168 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
169 },{
170 .strid = "video_luma_spatial_filter_type",
171 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
172 },{
173 .strid = "video_chroma_spatial_filter_type",
174 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
175 },{
176 .strid = "video_temporal_filter_mode",
177 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
178 },{
179 .strid = "video_temporal_filter",
180 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
181 },{
182 .strid = "video_median_filter_type",
183 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
184 },{
185 .strid = "video_luma_median_filter_top",
186 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
187 },{
188 .strid = "video_luma_median_filter_bottom",
189 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
190 },{
191 .strid = "video_chroma_median_filter_top",
192 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
193 },{
194 .strid = "video_chroma_median_filter_bottom",
195 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
196 }
197};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300198#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300199
Mike Iselyd8554972006-06-26 20:58:46 -0300200
Mike Isely434449f2006-08-08 09:10:06 -0300201static const char *control_values_srate[] = {
202 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
203 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
204 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
205};
Mike Iselyd8554972006-06-26 20:58:46 -0300206
Mike Iselyd8554972006-06-26 20:58:46 -0300207
208
209static const char *control_values_input[] = {
210 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300211 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300212 [PVR2_CVAL_INPUT_RADIO] = "radio",
213 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
214 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
215};
216
217
218static const char *control_values_audiomode[] = {
219 [V4L2_TUNER_MODE_MONO] = "Mono",
220 [V4L2_TUNER_MODE_STEREO] = "Stereo",
221 [V4L2_TUNER_MODE_LANG1] = "Lang1",
222 [V4L2_TUNER_MODE_LANG2] = "Lang2",
223 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
224};
225
226
227static const char *control_values_hsm[] = {
228 [PVR2_CVAL_HSM_FAIL] = "Fail",
229 [PVR2_CVAL_HSM_HIGH] = "High",
230 [PVR2_CVAL_HSM_FULL] = "Full",
231};
232
233
Mike Isely681c7392007-11-26 01:48:52 -0300234static const char *pvr2_state_names[] = {
235 [PVR2_STATE_NONE] = "none",
236 [PVR2_STATE_DEAD] = "dead",
237 [PVR2_STATE_COLD] = "cold",
238 [PVR2_STATE_WARM] = "warm",
239 [PVR2_STATE_ERROR] = "error",
240 [PVR2_STATE_READY] = "ready",
241 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300242};
243
Mike Isely681c7392007-11-26 01:48:52 -0300244
Mike Isely694dca2b2008-03-28 05:42:10 -0300245struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300246 unsigned char id;
247 unsigned char *desc;
248};
249
Mike Isely694dca2b2008-03-28 05:42:10 -0300250static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300251 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
252 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300253 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300254 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
255 {FX2CMD_REG_WRITE, "write encoder register"},
256 {FX2CMD_REG_READ, "read encoder register"},
257 {FX2CMD_MEMSEL, "encoder memsel"},
258 {FX2CMD_I2C_WRITE, "i2c write"},
259 {FX2CMD_I2C_READ, "i2c read"},
260 {FX2CMD_GET_USB_SPEED, "get USB speed"},
261 {FX2CMD_STREAMING_ON, "stream on"},
262 {FX2CMD_STREAMING_OFF, "stream off"},
263 {FX2CMD_FWPOST1, "fwpost1"},
264 {FX2CMD_POWER_OFF, "power off"},
265 {FX2CMD_POWER_ON, "power on"},
266 {FX2CMD_DEEP_RESET, "deep reset"},
267 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
268 {FX2CMD_GET_IR_CODE, "get IR code"},
269 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
270 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
271 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
272 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
273 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
274 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
275 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
276};
277
278
Mike Isely1cb03b72008-04-21 03:47:43 -0300279static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300280static void pvr2_hdw_state_sched(struct pvr2_hdw *);
281static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300282static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300283static void pvr2_hdw_worker_i2c(struct work_struct *work);
284static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300285static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
286static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
287static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300288static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300289static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300290static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300291static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
292static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300293static void pvr2_hdw_quiescent_timeout(unsigned long);
294static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300295static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300296static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300297static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
298 unsigned int timeout,int probe_fl,
299 void *write_data,unsigned int write_len,
300 void *read_data,unsigned int read_len);
Mike Iselyd8554972006-06-26 20:58:46 -0300301
Mike Isely681c7392007-11-26 01:48:52 -0300302
303static void trace_stbit(const char *name,int val)
304{
305 pvr2_trace(PVR2_TRACE_STBITS,
306 "State bit %s <-- %s",
307 name,(val ? "true" : "false"));
308}
309
Mike Iselyd8554972006-06-26 20:58:46 -0300310static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
311{
312 struct pvr2_hdw *hdw = cptr->hdw;
313 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
314 *vp = hdw->freqTable[hdw->freqProgSlot-1];
315 } else {
316 *vp = 0;
317 }
318 return 0;
319}
320
321static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
322{
323 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300324 unsigned int slotId = hdw->freqProgSlot;
325 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
326 hdw->freqTable[slotId-1] = v;
327 /* Handle side effects correctly - if we're tuned to this
328 slot, then forgot the slot id relation since the stored
329 frequency has been changed. */
330 if (hdw->freqSelector) {
331 if (hdw->freqSlotRadio == slotId) {
332 hdw->freqSlotRadio = 0;
333 }
334 } else {
335 if (hdw->freqSlotTelevision == slotId) {
336 hdw->freqSlotTelevision = 0;
337 }
338 }
Mike Iselyd8554972006-06-26 20:58:46 -0300339 }
340 return 0;
341}
342
343static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
344{
345 *vp = cptr->hdw->freqProgSlot;
346 return 0;
347}
348
349static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
350{
351 struct pvr2_hdw *hdw = cptr->hdw;
352 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
353 hdw->freqProgSlot = v;
354 }
355 return 0;
356}
357
358static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
359{
Mike Isely1bde0282006-12-27 23:30:13 -0300360 struct pvr2_hdw *hdw = cptr->hdw;
361 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300362 return 0;
363}
364
Mike Isely1bde0282006-12-27 23:30:13 -0300365static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300366{
367 unsigned freq = 0;
368 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300369 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
370 if (slotId > 0) {
371 freq = hdw->freqTable[slotId-1];
372 if (!freq) return 0;
373 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300374 }
Mike Isely1bde0282006-12-27 23:30:13 -0300375 if (hdw->freqSelector) {
376 hdw->freqSlotRadio = slotId;
377 } else {
378 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300379 }
380 return 0;
381}
382
383static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
384{
Mike Isely1bde0282006-12-27 23:30:13 -0300385 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300386 return 0;
387}
388
389static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
390{
391 return cptr->hdw->freqDirty != 0;
392}
393
394static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
395{
396 cptr->hdw->freqDirty = 0;
397}
398
399static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
400{
Mike Isely1bde0282006-12-27 23:30:13 -0300401 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300402 return 0;
403}
404
Mike Isely3ad9fc32006-09-02 22:37:52 -0300405static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
406{
407 /* Actual maximum depends on the video standard in effect. */
408 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
409 *vp = 480;
410 } else {
411 *vp = 576;
412 }
413 return 0;
414}
415
416static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
417{
Mike Isely989eb152007-11-26 01:53:12 -0300418 /* Actual minimum depends on device digitizer type. */
419 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300420 *vp = 75;
421 } else {
422 *vp = 17;
423 }
424 return 0;
425}
426
Mike Isely1bde0282006-12-27 23:30:13 -0300427static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
428{
429 *vp = cptr->hdw->input_val;
430 return 0;
431}
432
Mike Isely29bf5b12008-04-22 14:45:37 -0300433static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
434{
Mike Isely1cb03b72008-04-21 03:47:43 -0300435 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300436}
437
Mike Isely1bde0282006-12-27 23:30:13 -0300438static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
439{
Mike Isely1cb03b72008-04-21 03:47:43 -0300440 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300441}
442
443static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
444{
445 return cptr->hdw->input_dirty != 0;
446}
447
448static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
449{
450 cptr->hdw->input_dirty = 0;
451}
452
Mike Isely5549f542006-12-27 23:28:54 -0300453
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300454static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
455{
Mike Isely644afdb2007-01-20 00:19:23 -0300456 unsigned long fv;
457 struct pvr2_hdw *hdw = cptr->hdw;
458 if (hdw->tuner_signal_stale) {
459 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300460 }
Mike Isely644afdb2007-01-20 00:19:23 -0300461 fv = hdw->tuner_signal_info.rangehigh;
462 if (!fv) {
463 /* Safety fallback */
464 *vp = TV_MAX_FREQ;
465 return 0;
466 }
467 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
468 fv = (fv * 125) / 2;
469 } else {
470 fv = fv * 62500;
471 }
472 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300473 return 0;
474}
475
476static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
477{
Mike Isely644afdb2007-01-20 00:19:23 -0300478 unsigned long fv;
479 struct pvr2_hdw *hdw = cptr->hdw;
480 if (hdw->tuner_signal_stale) {
481 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300482 }
Mike Isely644afdb2007-01-20 00:19:23 -0300483 fv = hdw->tuner_signal_info.rangelow;
484 if (!fv) {
485 /* Safety fallback */
486 *vp = TV_MIN_FREQ;
487 return 0;
488 }
489 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
490 fv = (fv * 125) / 2;
491 } else {
492 fv = fv * 62500;
493 }
494 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300495 return 0;
496}
497
Mike Iselyb30d2442006-06-25 20:05:01 -0300498static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
499{
500 return cptr->hdw->enc_stale != 0;
501}
502
503static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
504{
505 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300506 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300507}
508
509static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
510{
511 int ret;
512 struct v4l2_ext_controls cs;
513 struct v4l2_ext_control c1;
514 memset(&cs,0,sizeof(cs));
515 memset(&c1,0,sizeof(c1));
516 cs.controls = &c1;
517 cs.count = 1;
518 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300519 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300520 VIDIOC_G_EXT_CTRLS);
521 if (ret) return ret;
522 *vp = c1.value;
523 return 0;
524}
525
526static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
527{
528 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300529 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300530 struct v4l2_ext_controls cs;
531 struct v4l2_ext_control c1;
532 memset(&cs,0,sizeof(cs));
533 memset(&c1,0,sizeof(c1));
534 cs.controls = &c1;
535 cs.count = 1;
536 c1.id = cptr->info->v4l_id;
537 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300538 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
539 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300540 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300541 if (ret == -EBUSY) {
542 /* Oops. cx2341x is telling us it's not safe to change
543 this control while we're capturing. Make a note of this
544 fact so that the pipeline will be stopped the next time
545 controls are committed. Then go on ahead and store this
546 change anyway. */
547 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
548 0, &cs,
549 VIDIOC_S_EXT_CTRLS);
550 if (!ret) hdw->enc_unsafe_stale = !0;
551 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300552 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300553 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300554 return 0;
555}
556
557static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
558{
559 struct v4l2_queryctrl qctrl;
560 struct pvr2_ctl_info *info;
561 qctrl.id = cptr->info->v4l_id;
562 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
563 /* Strip out the const so we can adjust a function pointer. It's
564 OK to do this here because we know this is a dynamically created
565 control, so the underlying storage for the info pointer is (a)
566 private to us, and (b) not in read-only storage. Either we do
567 this or we significantly complicate the underlying control
568 implementation. */
569 info = (struct pvr2_ctl_info *)(cptr->info);
570 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
571 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300572 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300573 }
574 } else {
575 if (!(info->set_value)) {
576 info->set_value = ctrl_cx2341x_set;
577 }
578 }
579 return qctrl.flags;
580}
581
Mike Iselyd8554972006-06-26 20:58:46 -0300582static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
583{
Mike Isely681c7392007-11-26 01:48:52 -0300584 *vp = cptr->hdw->state_pipeline_req;
585 return 0;
586}
587
588static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
589{
590 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300591 return 0;
592}
593
594static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
595{
596 int result = pvr2_hdw_is_hsm(cptr->hdw);
597 *vp = PVR2_CVAL_HSM_FULL;
598 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
599 if (result) *vp = PVR2_CVAL_HSM_HIGH;
600 return 0;
601}
602
603static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
604{
605 *vp = cptr->hdw->std_mask_avail;
606 return 0;
607}
608
609static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
610{
611 struct pvr2_hdw *hdw = cptr->hdw;
612 v4l2_std_id ns;
613 ns = hdw->std_mask_avail;
614 ns = (ns & ~m) | (v & m);
615 if (ns == hdw->std_mask_avail) return 0;
616 hdw->std_mask_avail = ns;
617 pvr2_hdw_internal_set_std_avail(hdw);
618 pvr2_hdw_internal_find_stdenum(hdw);
619 return 0;
620}
621
622static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
623 char *bufPtr,unsigned int bufSize,
624 unsigned int *len)
625{
626 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
627 return 0;
628}
629
630static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
631 const char *bufPtr,unsigned int bufSize,
632 int *mskp,int *valp)
633{
634 int ret;
635 v4l2_std_id id;
636 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
637 if (ret < 0) return ret;
638 if (mskp) *mskp = id;
639 if (valp) *valp = id;
640 return 0;
641}
642
643static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
644{
645 *vp = cptr->hdw->std_mask_cur;
646 return 0;
647}
648
649static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
650{
651 struct pvr2_hdw *hdw = cptr->hdw;
652 v4l2_std_id ns;
653 ns = hdw->std_mask_cur;
654 ns = (ns & ~m) | (v & m);
655 if (ns == hdw->std_mask_cur) return 0;
656 hdw->std_mask_cur = ns;
657 hdw->std_dirty = !0;
658 pvr2_hdw_internal_find_stdenum(hdw);
659 return 0;
660}
661
662static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
663{
664 return cptr->hdw->std_dirty != 0;
665}
666
667static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
668{
669 cptr->hdw->std_dirty = 0;
670}
671
672static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
673{
Mike Isely18103c572007-01-20 00:09:47 -0300674 struct pvr2_hdw *hdw = cptr->hdw;
675 pvr2_i2c_core_status_poll(hdw);
676 *vp = hdw->tuner_signal_info.signal;
677 return 0;
678}
679
680static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
681{
682 int val = 0;
683 unsigned int subchan;
684 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300685 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300686 subchan = hdw->tuner_signal_info.rxsubchans;
687 if (subchan & V4L2_TUNER_SUB_MONO) {
688 val |= (1 << V4L2_TUNER_MODE_MONO);
689 }
690 if (subchan & V4L2_TUNER_SUB_STEREO) {
691 val |= (1 << V4L2_TUNER_MODE_STEREO);
692 }
693 if (subchan & V4L2_TUNER_SUB_LANG1) {
694 val |= (1 << V4L2_TUNER_MODE_LANG1);
695 }
696 if (subchan & V4L2_TUNER_SUB_LANG2) {
697 val |= (1 << V4L2_TUNER_MODE_LANG2);
698 }
699 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300700 return 0;
701}
702
Mike Iselyd8554972006-06-26 20:58:46 -0300703
704static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
705{
706 struct pvr2_hdw *hdw = cptr->hdw;
707 if (v < 0) return -EINVAL;
708 if (v > hdw->std_enum_cnt) return -EINVAL;
709 hdw->std_enum_cur = v;
710 if (!v) return 0;
711 v--;
712 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
713 hdw->std_mask_cur = hdw->std_defs[v].id;
714 hdw->std_dirty = !0;
715 return 0;
716}
717
718
719static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
720{
721 *vp = cptr->hdw->std_enum_cur;
722 return 0;
723}
724
725
726static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
727{
728 return cptr->hdw->std_dirty != 0;
729}
730
731
732static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
733{
734 cptr->hdw->std_dirty = 0;
735}
736
737
738#define DEFINT(vmin,vmax) \
739 .type = pvr2_ctl_int, \
740 .def.type_int.min_value = vmin, \
741 .def.type_int.max_value = vmax
742
743#define DEFENUM(tab) \
744 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300745 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300746 .def.type_enum.value_names = tab
747
Mike Isely33213962006-06-25 20:04:40 -0300748#define DEFBOOL \
749 .type = pvr2_ctl_bool
750
Mike Iselyd8554972006-06-26 20:58:46 -0300751#define DEFMASK(msk,tab) \
752 .type = pvr2_ctl_bitmask, \
753 .def.type_bitmask.valid_bits = msk, \
754 .def.type_bitmask.bit_names = tab
755
756#define DEFREF(vname) \
757 .set_value = ctrl_set_##vname, \
758 .get_value = ctrl_get_##vname, \
759 .is_dirty = ctrl_isdirty_##vname, \
760 .clear_dirty = ctrl_cleardirty_##vname
761
762
763#define VCREATE_FUNCS(vname) \
764static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
765{*vp = cptr->hdw->vname##_val; return 0;} \
766static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
767{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
768static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
769{return cptr->hdw->vname##_dirty != 0;} \
770static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
771{cptr->hdw->vname##_dirty = 0;}
772
773VCREATE_FUNCS(brightness)
774VCREATE_FUNCS(contrast)
775VCREATE_FUNCS(saturation)
776VCREATE_FUNCS(hue)
777VCREATE_FUNCS(volume)
778VCREATE_FUNCS(balance)
779VCREATE_FUNCS(bass)
780VCREATE_FUNCS(treble)
781VCREATE_FUNCS(mute)
Mike Iselyc05c0462006-06-25 20:04:25 -0300782VCREATE_FUNCS(audiomode)
783VCREATE_FUNCS(res_hor)
784VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300785VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300786
Mike Iselyd8554972006-06-26 20:58:46 -0300787/* Table definition of all controls which can be manipulated */
788static const struct pvr2_ctl_info control_defs[] = {
789 {
790 .v4l_id = V4L2_CID_BRIGHTNESS,
791 .desc = "Brightness",
792 .name = "brightness",
793 .default_value = 128,
794 DEFREF(brightness),
795 DEFINT(0,255),
796 },{
797 .v4l_id = V4L2_CID_CONTRAST,
798 .desc = "Contrast",
799 .name = "contrast",
800 .default_value = 68,
801 DEFREF(contrast),
802 DEFINT(0,127),
803 },{
804 .v4l_id = V4L2_CID_SATURATION,
805 .desc = "Saturation",
806 .name = "saturation",
807 .default_value = 64,
808 DEFREF(saturation),
809 DEFINT(0,127),
810 },{
811 .v4l_id = V4L2_CID_HUE,
812 .desc = "Hue",
813 .name = "hue",
814 .default_value = 0,
815 DEFREF(hue),
816 DEFINT(-128,127),
817 },{
818 .v4l_id = V4L2_CID_AUDIO_VOLUME,
819 .desc = "Volume",
820 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -0300821 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -0300822 DEFREF(volume),
823 DEFINT(0,65535),
824 },{
825 .v4l_id = V4L2_CID_AUDIO_BALANCE,
826 .desc = "Balance",
827 .name = "balance",
828 .default_value = 0,
829 DEFREF(balance),
830 DEFINT(-32768,32767),
831 },{
832 .v4l_id = V4L2_CID_AUDIO_BASS,
833 .desc = "Bass",
834 .name = "bass",
835 .default_value = 0,
836 DEFREF(bass),
837 DEFINT(-32768,32767),
838 },{
839 .v4l_id = V4L2_CID_AUDIO_TREBLE,
840 .desc = "Treble",
841 .name = "treble",
842 .default_value = 0,
843 DEFREF(treble),
844 DEFINT(-32768,32767),
845 },{
846 .v4l_id = V4L2_CID_AUDIO_MUTE,
847 .desc = "Mute",
848 .name = "mute",
849 .default_value = 0,
850 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -0300851 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300852 },{
Mike Iselyc05c0462006-06-25 20:04:25 -0300853 .desc = "Video Source",
854 .name = "input",
855 .internal_id = PVR2_CID_INPUT,
856 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -0300857 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -0300858 DEFREF(input),
859 DEFENUM(control_values_input),
860 },{
861 .desc = "Audio Mode",
862 .name = "audio_mode",
863 .internal_id = PVR2_CID_AUDIOMODE,
864 .default_value = V4L2_TUNER_MODE_STEREO,
865 DEFREF(audiomode),
866 DEFENUM(control_values_audiomode),
867 },{
868 .desc = "Horizontal capture resolution",
869 .name = "resolution_hor",
870 .internal_id = PVR2_CID_HRES,
871 .default_value = 720,
872 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300873 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -0300874 },{
875 .desc = "Vertical capture resolution",
876 .name = "resolution_ver",
877 .internal_id = PVR2_CID_VRES,
878 .default_value = 480,
879 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300880 DEFINT(17,576),
881 /* Hook in check for video standard and adjust maximum
882 depending on the standard. */
883 .get_max_value = ctrl_vres_max_get,
884 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -0300885 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300886 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -0300887 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
888 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -0300889 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -0300890 DEFREF(srate),
891 DEFENUM(control_values_srate),
892 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300893 .desc = "Tuner Frequency (Hz)",
894 .name = "frequency",
895 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -0300896 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -0300897 .set_value = ctrl_freq_set,
898 .get_value = ctrl_freq_get,
899 .is_dirty = ctrl_freq_is_dirty,
900 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -0300901 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300902 /* Hook in check for input value (tv/radio) and adjust
903 max/min values accordingly */
904 .get_max_value = ctrl_freq_max_get,
905 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300906 },{
907 .desc = "Channel",
908 .name = "channel",
909 .set_value = ctrl_channel_set,
910 .get_value = ctrl_channel_get,
911 DEFINT(0,FREQTABLE_SIZE),
912 },{
913 .desc = "Channel Program Frequency",
914 .name = "freq_table_value",
915 .set_value = ctrl_channelfreq_set,
916 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -0300917 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -0300918 /* Hook in check for input value (tv/radio) and adjust
919 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -0300920 .get_max_value = ctrl_freq_max_get,
921 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300922 },{
923 .desc = "Channel Program ID",
924 .name = "freq_table_channel",
925 .set_value = ctrl_channelprog_set,
926 .get_value = ctrl_channelprog_get,
927 DEFINT(0,FREQTABLE_SIZE),
928 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300929 .desc = "Streaming Enabled",
930 .name = "streaming_enabled",
931 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -0300932 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300933 },{
934 .desc = "USB Speed",
935 .name = "usb_speed",
936 .get_value = ctrl_hsm_get,
937 DEFENUM(control_values_hsm),
938 },{
Mike Isely681c7392007-11-26 01:48:52 -0300939 .desc = "Master State",
940 .name = "master_state",
941 .get_value = ctrl_masterstate_get,
942 DEFENUM(pvr2_state_names),
943 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300944 .desc = "Signal Present",
945 .name = "signal_present",
946 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -0300947 DEFINT(0,65535),
948 },{
949 .desc = "Audio Modes Present",
950 .name = "audio_modes_present",
951 .get_value = ctrl_audio_modes_present_get,
952 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
953 v4l. Nothing outside of this module cares about this,
954 but I reuse it in order to also reuse the
955 control_values_audiomode string table. */
956 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
957 (1 << V4L2_TUNER_MODE_STEREO)|
958 (1 << V4L2_TUNER_MODE_LANG1)|
959 (1 << V4L2_TUNER_MODE_LANG2)),
960 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -0300961 },{
962 .desc = "Video Standards Available Mask",
963 .name = "video_standard_mask_available",
964 .internal_id = PVR2_CID_STDAVAIL,
965 .skip_init = !0,
966 .get_value = ctrl_stdavail_get,
967 .set_value = ctrl_stdavail_set,
968 .val_to_sym = ctrl_std_val_to_sym,
969 .sym_to_val = ctrl_std_sym_to_val,
970 .type = pvr2_ctl_bitmask,
971 },{
972 .desc = "Video Standards In Use Mask",
973 .name = "video_standard_mask_active",
974 .internal_id = PVR2_CID_STDCUR,
975 .skip_init = !0,
976 .get_value = ctrl_stdcur_get,
977 .set_value = ctrl_stdcur_set,
978 .is_dirty = ctrl_stdcur_is_dirty,
979 .clear_dirty = ctrl_stdcur_clear_dirty,
980 .val_to_sym = ctrl_std_val_to_sym,
981 .sym_to_val = ctrl_std_sym_to_val,
982 .type = pvr2_ctl_bitmask,
983 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300984 .desc = "Video Standard Name",
985 .name = "video_standard",
986 .internal_id = PVR2_CID_STDENUM,
987 .skip_init = !0,
988 .get_value = ctrl_stdenumcur_get,
989 .set_value = ctrl_stdenumcur_set,
990 .is_dirty = ctrl_stdenumcur_is_dirty,
991 .clear_dirty = ctrl_stdenumcur_clear_dirty,
992 .type = pvr2_ctl_enum,
993 }
994};
995
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300996#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -0300997
998
999const char *pvr2_config_get_name(enum pvr2_config cfg)
1000{
1001 switch (cfg) {
1002 case pvr2_config_empty: return "empty";
1003 case pvr2_config_mpeg: return "mpeg";
1004 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001005 case pvr2_config_pcm: return "pcm";
1006 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001007 }
1008 return "<unknown>";
1009}
1010
1011
1012struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1013{
1014 return hdw->usb_dev;
1015}
1016
1017
1018unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1019{
1020 return hdw->serial_number;
1021}
1022
Mike Isely31a18542007-04-08 01:11:47 -03001023
1024const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1025{
1026 return hdw->bus_info;
1027}
1028
1029
Mike Isely1bde0282006-12-27 23:30:13 -03001030unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1031{
1032 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1033}
1034
1035/* Set the currently tuned frequency and account for all possible
1036 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001037static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001038{
Mike Isely7c74e572007-01-20 00:15:41 -03001039 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001040 if (hdw->freqSelector) {
1041 /* Swing over to radio frequency selection */
1042 hdw->freqSelector = 0;
1043 hdw->freqDirty = !0;
1044 }
Mike Isely1bde0282006-12-27 23:30:13 -03001045 if (hdw->freqValRadio != val) {
1046 hdw->freqValRadio = val;
1047 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001048 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001049 }
Mike Isely7c74e572007-01-20 00:15:41 -03001050 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001051 if (!(hdw->freqSelector)) {
1052 /* Swing over to television frequency selection */
1053 hdw->freqSelector = 1;
1054 hdw->freqDirty = !0;
1055 }
Mike Isely1bde0282006-12-27 23:30:13 -03001056 if (hdw->freqValTelevision != val) {
1057 hdw->freqValTelevision = val;
1058 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001059 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001060 }
Mike Isely1bde0282006-12-27 23:30:13 -03001061 }
1062}
1063
Mike Iselyd8554972006-06-26 20:58:46 -03001064int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1065{
1066 return hdw->unit_number;
1067}
1068
1069
1070/* Attempt to locate one of the given set of files. Messages are logged
1071 appropriate to what has been found. The return value will be 0 or
1072 greater on success (it will be the index of the file name found) and
1073 fw_entry will be filled in. Otherwise a negative error is returned on
1074 failure. If the return value is -ENOENT then no viable firmware file
1075 could be located. */
1076static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1077 const struct firmware **fw_entry,
1078 const char *fwtypename,
1079 unsigned int fwcount,
1080 const char *fwnames[])
1081{
1082 unsigned int idx;
1083 int ret = -EINVAL;
1084 for (idx = 0; idx < fwcount; idx++) {
1085 ret = request_firmware(fw_entry,
1086 fwnames[idx],
1087 &hdw->usb_dev->dev);
1088 if (!ret) {
1089 trace_firmware("Located %s firmware: %s;"
1090 " uploading...",
1091 fwtypename,
1092 fwnames[idx]);
1093 return idx;
1094 }
1095 if (ret == -ENOENT) continue;
1096 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1097 "request_firmware fatal error with code=%d",ret);
1098 return ret;
1099 }
1100 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1101 "***WARNING***"
1102 " Device %s firmware"
1103 " seems to be missing.",
1104 fwtypename);
1105 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1106 "Did you install the pvrusb2 firmware files"
1107 " in their proper location?");
1108 if (fwcount == 1) {
1109 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1110 "request_firmware unable to locate %s file %s",
1111 fwtypename,fwnames[0]);
1112 } else {
1113 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1114 "request_firmware unable to locate"
1115 " one of the following %s files:",
1116 fwtypename);
1117 for (idx = 0; idx < fwcount; idx++) {
1118 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1119 "request_firmware: Failed to find %s",
1120 fwnames[idx]);
1121 }
1122 }
1123 return ret;
1124}
1125
1126
1127/*
1128 * pvr2_upload_firmware1().
1129 *
1130 * Send the 8051 firmware to the device. After the upload, arrange for
1131 * device to re-enumerate.
1132 *
1133 * NOTE : the pointer to the firmware data given by request_firmware()
1134 * is not suitable for an usb transaction.
1135 *
1136 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001137static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001138{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001139 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001140 void *fw_ptr;
1141 unsigned int pipe;
1142 int ret;
1143 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001144
Mike Isely989eb152007-11-26 01:53:12 -03001145 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001146 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001147 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1148 "Connected device type defines"
1149 " no firmware to upload; ignoring firmware");
1150 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001151 }
1152
Mike Iselyd8554972006-06-26 20:58:46 -03001153 hdw->fw1_state = FW1_STATE_FAILED; // default result
1154
1155 trace_firmware("pvr2_upload_firmware1");
1156
1157 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001158 hdw->hdw_desc->fx2_firmware.cnt,
1159 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001160 if (ret < 0) {
1161 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1162 return ret;
1163 }
1164
1165 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1166 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1167
1168 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1169
1170 if (fw_entry->size != 0x2000){
1171 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1172 release_firmware(fw_entry);
1173 return -ENOMEM;
1174 }
1175
1176 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1177 if (fw_ptr == NULL){
1178 release_firmware(fw_entry);
1179 return -ENOMEM;
1180 }
1181
1182 /* We have to hold the CPU during firmware upload. */
1183 pvr2_hdw_cpureset_assert(hdw,1);
1184
1185 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1186 chunk. */
1187
1188 ret = 0;
1189 for(address = 0; address < fw_entry->size; address += 0x800) {
1190 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1191 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1192 0, fw_ptr, 0x800, HZ);
1193 }
1194
1195 trace_firmware("Upload done, releasing device's CPU");
1196
1197 /* Now release the CPU. It will disconnect and reconnect later. */
1198 pvr2_hdw_cpureset_assert(hdw,0);
1199
1200 kfree(fw_ptr);
1201 release_firmware(fw_entry);
1202
1203 trace_firmware("Upload done (%d bytes sent)",ret);
1204
1205 /* We should have written 8192 bytes */
1206 if (ret == 8192) {
1207 hdw->fw1_state = FW1_STATE_RELOAD;
1208 return 0;
1209 }
1210
1211 return -EIO;
1212}
1213
1214
1215/*
1216 * pvr2_upload_firmware2()
1217 *
1218 * This uploads encoder firmware on endpoint 2.
1219 *
1220 */
1221
1222int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1223{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001224 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001225 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001226 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001227 int actual_length;
1228 int ret = 0;
1229 int fwidx;
1230 static const char *fw_files[] = {
1231 CX2341X_FIRM_ENC_FILENAME,
1232 };
1233
Mike Isely989eb152007-11-26 01:53:12 -03001234 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001235 return 0;
1236 }
1237
Mike Iselyd8554972006-06-26 20:58:46 -03001238 trace_firmware("pvr2_upload_firmware2");
1239
1240 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001241 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001242 if (ret < 0) return ret;
1243 fwidx = ret;
1244 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001245 /* Since we're about to completely reinitialize the encoder,
1246 invalidate our cached copy of its configuration state. Next
1247 time we configure the encoder, then we'll fully configure it. */
1248 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001249
Mike Iselyd913d632008-04-06 04:04:35 -03001250 /* Encoder is about to be reset so note that as far as we're
1251 concerned now, the encoder has never been run. */
1252 del_timer_sync(&hdw->encoder_run_timer);
1253 if (hdw->state_encoder_runok) {
1254 hdw->state_encoder_runok = 0;
1255 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1256 }
1257
Mike Iselyd8554972006-06-26 20:58:46 -03001258 /* First prepare firmware loading */
1259 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1260 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1261 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1262 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1263 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1264 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1265 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1266 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1267 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1268 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1269 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1270 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1271 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1272 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1273 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1274 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001275 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1276 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001277
1278 if (ret) {
1279 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1280 "firmware2 upload prep failed, ret=%d",ret);
1281 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001282 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001283 }
1284
1285 /* Now send firmware */
1286
1287 fw_len = fw_entry->size;
1288
Mike Isely90060d32007-02-08 02:02:53 -03001289 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001290 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1291 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001292 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001293 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001294 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001295 ret = -EINVAL;
1296 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001297 }
1298
1299 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1300 if (fw_ptr == NULL){
1301 release_firmware(fw_entry);
1302 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1303 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001304 ret = -ENOMEM;
1305 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001306 }
1307
1308 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1309
Mike Isely90060d32007-02-08 02:02:53 -03001310 fw_done = 0;
1311 for (fw_done = 0; fw_done < fw_len;) {
1312 bcnt = fw_len - fw_done;
1313 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1314 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1315 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001316 /* Some background info: The data being swapped here is a
1317 firmware image destined for the mpeg encoder chip that
1318 lives at the other end of a USB endpoint. The encoder
1319 chip always talks in 32 bit chunks and its storage is
1320 organized into 32 bit words. However from the file
1321 system to the encoder chip everything is purely a byte
1322 stream. The firmware file's contents are always 32 bit
1323 swapped from what the encoder expects. Thus the need
1324 always exists to swap the bytes regardless of the endian
1325 type of the host processor and therefore swab32() makes
1326 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001327 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001328 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001329
Mike Isely90060d32007-02-08 02:02:53 -03001330 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001331 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001332 ret |= (actual_length != bcnt);
1333 if (ret) break;
1334 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001335 }
1336
1337 trace_firmware("upload of %s : %i / %i ",
1338 fw_files[fwidx],fw_done,fw_len);
1339
1340 kfree(fw_ptr);
1341 release_firmware(fw_entry);
1342
1343 if (ret) {
1344 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1345 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001346 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001347 }
1348
1349 /* Finish upload */
1350
1351 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1352 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001353 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001354
1355 if (ret) {
1356 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1357 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001358 }
Mike Isely21684ba2008-04-21 03:49:33 -03001359
1360 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001361 if (hdw->hdw_desc->signal_routing_scheme ==
1362 PVR2_ROUTING_SCHEME_GOTVIEW) {
1363 /* Ensure that GPIO 11 is set to output for GOTVIEW
1364 hardware. */
1365 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1366 }
Mike Iselyd8554972006-06-26 20:58:46 -03001367 return ret;
1368}
1369
1370
Mike Isely681c7392007-11-26 01:48:52 -03001371static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001372{
Mike Isely681c7392007-11-26 01:48:52 -03001373 if (st < ARRAY_SIZE(pvr2_state_names)) {
1374 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001375 }
Mike Isely681c7392007-11-26 01:48:52 -03001376 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001377}
1378
Mike Isely681c7392007-11-26 01:48:52 -03001379static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001380{
Mike Isely681c7392007-11-26 01:48:52 -03001381 if (!hdw->decoder_ctrl) {
1382 if (!hdw->flag_decoder_missed) {
1383 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1384 "WARNING: No decoder present");
1385 hdw->flag_decoder_missed = !0;
1386 trace_stbit("flag_decoder_missed",
1387 hdw->flag_decoder_missed);
1388 }
1389 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001390 }
Mike Isely681c7392007-11-26 01:48:52 -03001391 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001392 return 0;
1393}
1394
1395
Mike Isely681c7392007-11-26 01:48:52 -03001396void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1397{
1398 if (hdw->decoder_ctrl == ptr) return;
1399 hdw->decoder_ctrl = ptr;
1400 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1401 hdw->flag_decoder_missed = 0;
1402 trace_stbit("flag_decoder_missed",
1403 hdw->flag_decoder_missed);
1404 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1405 "Decoder has appeared");
1406 pvr2_hdw_state_sched(hdw);
1407 }
1408}
1409
1410
1411int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1412{
1413 return hdw->master_state;
1414}
1415
1416
1417static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1418{
1419 if (!hdw->flag_tripped) return 0;
1420 hdw->flag_tripped = 0;
1421 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1422 "Clearing driver error statuss");
1423 return !0;
1424}
1425
1426
1427int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1428{
1429 int fl;
1430 LOCK_TAKE(hdw->big_lock); do {
1431 fl = pvr2_hdw_untrip_unlocked(hdw);
1432 } while (0); LOCK_GIVE(hdw->big_lock);
1433 if (fl) pvr2_hdw_state_sched(hdw);
1434 return 0;
1435}
1436
1437
Mike Isely681c7392007-11-26 01:48:52 -03001438
1439
Mike Iselyd8554972006-06-26 20:58:46 -03001440int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1441{
Mike Isely681c7392007-11-26 01:48:52 -03001442 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001443}
1444
1445
1446int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1447{
Mike Isely681c7392007-11-26 01:48:52 -03001448 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001449 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001450 pvr2_hdw_untrip_unlocked(hdw);
1451 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1452 hdw->state_pipeline_req = enable_flag != 0;
1453 pvr2_trace(PVR2_TRACE_START_STOP,
1454 "/*--TRACE_STREAM--*/ %s",
1455 enable_flag ? "enable" : "disable");
1456 }
1457 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001458 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001459 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1460 if (enable_flag) {
1461 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1462 if (st != PVR2_STATE_READY) return -EIO;
1463 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1464 }
1465 }
Mike Iselyd8554972006-06-26 20:58:46 -03001466 return 0;
1467}
1468
1469
1470int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1471{
Mike Isely681c7392007-11-26 01:48:52 -03001472 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001473 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001474 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1475 hdw->desired_stream_type = config;
1476 hdw->state_pipeline_config = 0;
1477 trace_stbit("state_pipeline_config",
1478 hdw->state_pipeline_config);
1479 pvr2_hdw_state_sched(hdw);
1480 }
Mike Iselyd8554972006-06-26 20:58:46 -03001481 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001482 if (fl) return 0;
1483 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001484}
1485
1486
1487static int get_default_tuner_type(struct pvr2_hdw *hdw)
1488{
1489 int unit_number = hdw->unit_number;
1490 int tp = -1;
1491 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1492 tp = tuner[unit_number];
1493 }
1494 if (tp < 0) return -EINVAL;
1495 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001496 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001497 return 0;
1498}
1499
1500
1501static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1502{
1503 int unit_number = hdw->unit_number;
1504 int tp = 0;
1505 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1506 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001507 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001508 }
Mike Isely6a540252007-12-02 23:51:34 -03001509 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001510}
1511
1512
1513static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1514{
1515 int unit_number = hdw->unit_number;
1516 int tp = 0;
1517 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1518 tp = tolerance[unit_number];
1519 }
1520 return tp;
1521}
1522
1523
1524static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1525{
1526 /* Try a harmless request to fetch the eeprom's address over
1527 endpoint 1. See what happens. Only the full FX2 image can
1528 respond to this. If this probe fails then likely the FX2
1529 firmware needs be loaded. */
1530 int result;
1531 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001532 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001533 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1534 hdw->cmd_buffer,1,
1535 hdw->cmd_buffer,1);
1536 if (result < 0) break;
1537 } while(0); LOCK_GIVE(hdw->ctl_lock);
1538 if (result) {
1539 pvr2_trace(PVR2_TRACE_INIT,
1540 "Probe of device endpoint 1 result status %d",
1541 result);
1542 } else {
1543 pvr2_trace(PVR2_TRACE_INIT,
1544 "Probe of device endpoint 1 succeeded");
1545 }
1546 return result == 0;
1547}
1548
Mike Isely9f66d4e2007-09-08 22:28:51 -03001549struct pvr2_std_hack {
1550 v4l2_std_id pat; /* Pattern to match */
1551 v4l2_std_id msk; /* Which bits we care about */
1552 v4l2_std_id std; /* What additional standards or default to set */
1553};
1554
1555/* This data structure labels specific combinations of standards from
1556 tveeprom that we'll try to recognize. If we recognize one, then assume
1557 a specified default standard to use. This is here because tveeprom only
1558 tells us about available standards not the intended default standard (if
1559 any) for the device in question. We guess the default based on what has
1560 been reported as available. Note that this is only for guessing a
1561 default - which can always be overridden explicitly - and if the user
1562 has otherwise named a default then that default will always be used in
1563 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001564static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001565 { /* PAL(B/G) */
1566 .pat = V4L2_STD_B|V4L2_STD_GH,
1567 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1568 },
1569 { /* NTSC(M) */
1570 .pat = V4L2_STD_MN,
1571 .std = V4L2_STD_NTSC_M,
1572 },
1573 { /* PAL(I) */
1574 .pat = V4L2_STD_PAL_I,
1575 .std = V4L2_STD_PAL_I,
1576 },
1577 { /* SECAM(L/L') */
1578 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1579 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1580 },
1581 { /* PAL(D/D1/K) */
1582 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001583 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001584 },
1585};
1586
Mike Iselyd8554972006-06-26 20:58:46 -03001587static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1588{
1589 char buf[40];
1590 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001591 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001592
1593 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001594 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001595
1596 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001597 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001598 "Supported video standard(s) reported available"
1599 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001600 bcnt,buf);
1601
1602 hdw->std_mask_avail = hdw->std_mask_eeprom;
1603
Mike Isely3d290bd2007-12-03 01:47:12 -03001604 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001605 if (std2) {
1606 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001607 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001608 "Expanding supported video standards"
1609 " to include: %.*s",
1610 bcnt,buf);
1611 hdw->std_mask_avail |= std2;
1612 }
1613
1614 pvr2_hdw_internal_set_std_avail(hdw);
1615
1616 if (std1) {
1617 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001618 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001619 "Initial video standard forced to %.*s",
1620 bcnt,buf);
1621 hdw->std_mask_cur = std1;
1622 hdw->std_dirty = !0;
1623 pvr2_hdw_internal_find_stdenum(hdw);
1624 return;
1625 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001626 if (std3) {
1627 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1628 pvr2_trace(PVR2_TRACE_STD,
1629 "Initial video standard"
1630 " (determined by device type): %.*s",bcnt,buf);
1631 hdw->std_mask_cur = std3;
1632 hdw->std_dirty = !0;
1633 pvr2_hdw_internal_find_stdenum(hdw);
1634 return;
1635 }
Mike Iselyd8554972006-06-26 20:58:46 -03001636
Mike Isely9f66d4e2007-09-08 22:28:51 -03001637 {
1638 unsigned int idx;
1639 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1640 if (std_eeprom_maps[idx].msk ?
1641 ((std_eeprom_maps[idx].pat ^
1642 hdw->std_mask_eeprom) &
1643 std_eeprom_maps[idx].msk) :
1644 (std_eeprom_maps[idx].pat !=
1645 hdw->std_mask_eeprom)) continue;
1646 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1647 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001648 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001649 "Initial video standard guessed as %.*s",
1650 bcnt,buf);
1651 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1652 hdw->std_dirty = !0;
1653 pvr2_hdw_internal_find_stdenum(hdw);
1654 return;
1655 }
1656 }
1657
Mike Iselyd8554972006-06-26 20:58:46 -03001658 if (hdw->std_enum_cnt > 1) {
1659 // Autoselect the first listed standard
1660 hdw->std_enum_cur = 1;
1661 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1662 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001663 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001664 "Initial video standard auto-selected to %s",
1665 hdw->std_defs[hdw->std_enum_cur-1].name);
1666 return;
1667 }
1668
Mike Isely0885ba12006-06-25 21:30:47 -03001669 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001670 "Unable to select a viable initial video standard");
1671}
1672
1673
1674static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1675{
1676 int ret;
1677 unsigned int idx;
1678 struct pvr2_ctrl *cptr;
1679 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001680 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001681 if (!reloadFl) {
1682 reloadFl =
1683 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1684 == 0);
1685 if (reloadFl) {
1686 pvr2_trace(PVR2_TRACE_INIT,
1687 "USB endpoint config looks strange"
1688 "; possibly firmware needs to be"
1689 " loaded");
1690 }
1691 }
1692 if (!reloadFl) {
1693 reloadFl = !pvr2_hdw_check_firmware(hdw);
1694 if (reloadFl) {
1695 pvr2_trace(PVR2_TRACE_INIT,
1696 "Check for FX2 firmware failed"
1697 "; possibly firmware needs to be"
1698 " loaded");
1699 }
1700 }
Mike Iselyd8554972006-06-26 20:58:46 -03001701 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001702 if (pvr2_upload_firmware1(hdw) != 0) {
1703 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1704 "Failure uploading firmware1");
1705 }
1706 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001707 }
1708 }
Mike Iselyd8554972006-06-26 20:58:46 -03001709 hdw->fw1_state = FW1_STATE_OK;
1710
1711 if (initusbreset) {
1712 pvr2_hdw_device_reset(hdw);
1713 }
1714 if (!pvr2_hdw_dev_ok(hdw)) return;
1715
Mike Isely989eb152007-11-26 01:53:12 -03001716 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1717 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001718 }
1719
Mike Isely989eb152007-11-26 01:53:12 -03001720 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001721 pvr2_hdw_cmd_powerup(hdw);
1722 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001723 }
1724
Mike Isely31335b12008-07-25 19:35:31 -03001725 /* Take the IR chip out of reset, if appropriate */
1726 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
1727 pvr2_issue_simple_cmd(hdw,
1728 FX2CMD_HCW_ZILOG_RESET |
1729 (1 << 8) |
1730 ((0) << 16));
1731 }
1732
Mike Iselyd8554972006-06-26 20:58:46 -03001733 // This step MUST happen after the earlier powerup step.
1734 pvr2_i2c_core_init(hdw);
1735 if (!pvr2_hdw_dev_ok(hdw)) return;
1736
Mike Iselyc05c0462006-06-25 20:04:25 -03001737 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001738 cptr = hdw->controls + idx;
1739 if (cptr->info->skip_init) continue;
1740 if (!cptr->info->set_value) continue;
1741 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1742 }
1743
Mike Isely1bde0282006-12-27 23:30:13 -03001744 /* Set up special default values for the television and radio
1745 frequencies here. It's not really important what these defaults
1746 are, but I set them to something usable in the Chicago area just
1747 to make driver testing a little easier. */
1748
Michael Krufky5a4f5da62008-05-11 16:37:50 -03001749 hdw->freqValTelevision = default_tv_freq;
1750 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03001751
Mike Iselyd8554972006-06-26 20:58:46 -03001752 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1753 // thread-safe against the normal pvr2_send_request() mechanism.
1754 // (We should make it thread safe).
1755
Mike Iselyaaf78842007-11-26 02:04:11 -03001756 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1757 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001758 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03001759 if (ret < 0) {
1760 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1761 "Unable to determine location of eeprom,"
1762 " skipping");
1763 } else {
1764 hdw->eeprom_addr = ret;
1765 pvr2_eeprom_analyze(hdw);
1766 if (!pvr2_hdw_dev_ok(hdw)) return;
1767 }
1768 } else {
1769 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1770 hdw->tuner_updated = !0;
1771 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03001772 }
1773
1774 pvr2_hdw_setup_std(hdw);
1775
1776 if (!get_default_tuner_type(hdw)) {
1777 pvr2_trace(PVR2_TRACE_INIT,
1778 "pvr2_hdw_setup: Tuner type overridden to %d",
1779 hdw->tuner_type);
1780 }
1781
Mike Iselyd8554972006-06-26 20:58:46 -03001782 pvr2_i2c_core_check_stale(hdw);
1783 hdw->tuner_updated = 0;
1784
1785 if (!pvr2_hdw_dev_ok(hdw)) return;
1786
Mike Isely1df59f02008-04-21 03:50:39 -03001787 if (hdw->hdw_desc->signal_routing_scheme ==
1788 PVR2_ROUTING_SCHEME_GOTVIEW) {
1789 /* Ensure that GPIO 11 is set to output for GOTVIEW
1790 hardware. */
1791 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1792 }
1793
Mike Isely681c7392007-11-26 01:48:52 -03001794 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001795
1796 hdw->vid_stream = pvr2_stream_create();
1797 if (!pvr2_hdw_dev_ok(hdw)) return;
1798 pvr2_trace(PVR2_TRACE_INIT,
1799 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1800 if (hdw->vid_stream) {
1801 idx = get_default_error_tolerance(hdw);
1802 if (idx) {
1803 pvr2_trace(PVR2_TRACE_INIT,
1804 "pvr2_hdw_setup: video stream %p"
1805 " setting tolerance %u",
1806 hdw->vid_stream,idx);
1807 }
1808 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1809 PVR2_VID_ENDPOINT,idx);
1810 }
1811
1812 if (!pvr2_hdw_dev_ok(hdw)) return;
1813
Mike Iselyd8554972006-06-26 20:58:46 -03001814 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03001815
1816 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001817}
1818
1819
Mike Isely681c7392007-11-26 01:48:52 -03001820/* Set up the structure and attempt to put the device into a usable state.
1821 This can be a time-consuming operation, which is why it is not done
1822 internally as part of the create() step. */
1823static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001824{
1825 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03001826 do {
Mike Iselyd8554972006-06-26 20:58:46 -03001827 pvr2_hdw_setup_low(hdw);
1828 pvr2_trace(PVR2_TRACE_INIT,
1829 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03001830 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03001831 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03001832 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03001833 pvr2_trace(
1834 PVR2_TRACE_INFO,
1835 "Device initialization"
1836 " completed successfully.");
1837 break;
1838 }
1839 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1840 pvr2_trace(
1841 PVR2_TRACE_INFO,
1842 "Device microcontroller firmware"
1843 " (re)loaded; it should now reset"
1844 " and reconnect.");
1845 break;
1846 }
1847 pvr2_trace(
1848 PVR2_TRACE_ERROR_LEGS,
1849 "Device initialization was not successful.");
1850 if (hdw->fw1_state == FW1_STATE_MISSING) {
1851 pvr2_trace(
1852 PVR2_TRACE_ERROR_LEGS,
1853 "Giving up since device"
1854 " microcontroller firmware"
1855 " appears to be missing.");
1856 break;
1857 }
1858 }
1859 if (procreload) {
1860 pvr2_trace(
1861 PVR2_TRACE_ERROR_LEGS,
1862 "Attempting pvrusb2 recovery by reloading"
1863 " primary firmware.");
1864 pvr2_trace(
1865 PVR2_TRACE_ERROR_LEGS,
1866 "If this works, device should disconnect"
1867 " and reconnect in a sane state.");
1868 hdw->fw1_state = FW1_STATE_UNKNOWN;
1869 pvr2_upload_firmware1(hdw);
1870 } else {
1871 pvr2_trace(
1872 PVR2_TRACE_ERROR_LEGS,
1873 "***WARNING*** pvrusb2 device hardware"
1874 " appears to be jammed"
1875 " and I can't clear it.");
1876 pvr2_trace(
1877 PVR2_TRACE_ERROR_LEGS,
1878 "You might need to power cycle"
1879 " the pvrusb2 device"
1880 " in order to recover.");
1881 }
Mike Isely681c7392007-11-26 01:48:52 -03001882 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03001883 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001884}
1885
1886
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001887/* Perform second stage initialization. Set callback pointer first so that
1888 we can avoid a possible initialization race (if the kernel thread runs
1889 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03001890int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
1891 void (*callback_func)(void *),
1892 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001893{
1894 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03001895 if (hdw->flag_disconnected) {
1896 /* Handle a race here: If we're already
1897 disconnected by this point, then give up. If we
1898 get past this then we'll remain connected for
1899 the duration of initialization since the entire
1900 initialization sequence is now protected by the
1901 big_lock. */
1902 break;
1903 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001904 hdw->state_data = callback_data;
1905 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03001906 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001907 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03001908 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001909}
1910
1911
1912/* Create, set up, and return a structure for interacting with the
1913 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03001914struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1915 const struct usb_device_id *devid)
1916{
Mike Isely7fb20fa2008-04-22 14:45:37 -03001917 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03001918 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001919 int valid_std_mask;
1920 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03001921 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03001922 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03001923 struct v4l2_queryctrl qctrl;
1924 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03001925
Mike Iselyd130fa82007-12-08 17:20:06 -03001926 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03001927
Mike Iselyfe15f132008-08-30 18:11:40 -03001928 if (hdw_desc == NULL) {
1929 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
1930 " No device description pointer,"
1931 " unable to continue.");
1932 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
1933 " please contact Mike Isely <isely@pobox.com>"
1934 " to get it included in the driver\n");
1935 goto fail;
1936 }
1937
Mike Iselyca545f72007-01-20 00:37:11 -03001938 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001939 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03001940 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03001941 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03001942
1943 init_timer(&hdw->quiescent_timer);
1944 hdw->quiescent_timer.data = (unsigned long)hdw;
1945 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1946
1947 init_timer(&hdw->encoder_wait_timer);
1948 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1949 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1950
Mike Iselyd913d632008-04-06 04:04:35 -03001951 init_timer(&hdw->encoder_run_timer);
1952 hdw->encoder_run_timer.data = (unsigned long)hdw;
1953 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
1954
Mike Isely681c7392007-11-26 01:48:52 -03001955 hdw->master_state = PVR2_STATE_DEAD;
1956
1957 init_waitqueue_head(&hdw->state_wait_data);
1958
Mike Isely18103c572007-01-20 00:09:47 -03001959 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001960 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03001961
Mike Isely7fb20fa2008-04-22 14:45:37 -03001962 /* Calculate which inputs are OK */
1963 m = 0;
1964 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03001965 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
1966 m |= 1 << PVR2_CVAL_INPUT_DTV;
1967 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03001968 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
1969 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
1970 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
1971 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03001972 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03001973
Mike Isely62433e32008-04-22 14:45:40 -03001974 /* If not a hybrid device, pathway_state never changes. So
1975 initialize it here to what it should forever be. */
1976 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
1977 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
1978 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
1979 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
1980 }
1981
Mike Iselyc05c0462006-06-25 20:04:25 -03001982 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03001983 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03001984 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001985 GFP_KERNEL);
1986 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03001987 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03001988 for (idx = 0; idx < hdw->control_cnt; idx++) {
1989 cptr = hdw->controls + idx;
1990 cptr->hdw = hdw;
1991 }
Mike Iselyd8554972006-06-26 20:58:46 -03001992 for (idx = 0; idx < 32; idx++) {
1993 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1994 }
Mike Iselyc05c0462006-06-25 20:04:25 -03001995 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001996 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03001997 cptr->info = control_defs+idx;
1998 }
Mike Iselydbc40a02008-04-22 14:45:39 -03001999
2000 /* Ensure that default input choice is a valid one. */
2001 m = hdw->input_avail_mask;
2002 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2003 if (!((1 << idx) & m)) continue;
2004 hdw->input_val = idx;
2005 break;
2006 }
2007
Mike Iselyb30d2442006-06-25 20:05:01 -03002008 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002009 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002010 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2011 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002012 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2013 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2014 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2015 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2016 ciptr->name = mpeg_ids[idx].strid;
2017 ciptr->v4l_id = mpeg_ids[idx].id;
2018 ciptr->skip_init = !0;
2019 ciptr->get_value = ctrl_cx2341x_get;
2020 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2021 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2022 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2023 qctrl.id = ciptr->v4l_id;
2024 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2025 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2026 ciptr->set_value = ctrl_cx2341x_set;
2027 }
2028 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2029 PVR2_CTLD_INFO_DESC_SIZE);
2030 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2031 ciptr->default_value = qctrl.default_value;
2032 switch (qctrl.type) {
2033 default:
2034 case V4L2_CTRL_TYPE_INTEGER:
2035 ciptr->type = pvr2_ctl_int;
2036 ciptr->def.type_int.min_value = qctrl.minimum;
2037 ciptr->def.type_int.max_value = qctrl.maximum;
2038 break;
2039 case V4L2_CTRL_TYPE_BOOLEAN:
2040 ciptr->type = pvr2_ctl_bool;
2041 break;
2042 case V4L2_CTRL_TYPE_MENU:
2043 ciptr->type = pvr2_ctl_enum;
2044 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002045 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2046 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002047 for (cnt1 = 0;
2048 ciptr->def.type_enum.value_names[cnt1] != NULL;
2049 cnt1++) { }
2050 ciptr->def.type_enum.count = cnt1;
2051 break;
2052 }
2053 cptr->info = ciptr;
2054 }
Mike Iselyd8554972006-06-26 20:58:46 -03002055
2056 // Initialize video standard enum dynamic control
2057 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2058 if (cptr) {
2059 memcpy(&hdw->std_info_enum,cptr->info,
2060 sizeof(hdw->std_info_enum));
2061 cptr->info = &hdw->std_info_enum;
2062
2063 }
2064 // Initialize control data regarding video standard masks
2065 valid_std_mask = pvr2_std_get_usable();
2066 for (idx = 0; idx < 32; idx++) {
2067 if (!(valid_std_mask & (1 << idx))) continue;
2068 cnt1 = pvr2_std_id_to_str(
2069 hdw->std_mask_names[idx],
2070 sizeof(hdw->std_mask_names[idx])-1,
2071 1 << idx);
2072 hdw->std_mask_names[idx][cnt1] = 0;
2073 }
2074 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2075 if (cptr) {
2076 memcpy(&hdw->std_info_avail,cptr->info,
2077 sizeof(hdw->std_info_avail));
2078 cptr->info = &hdw->std_info_avail;
2079 hdw->std_info_avail.def.type_bitmask.bit_names =
2080 hdw->std_mask_ptrs;
2081 hdw->std_info_avail.def.type_bitmask.valid_bits =
2082 valid_std_mask;
2083 }
2084 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2085 if (cptr) {
2086 memcpy(&hdw->std_info_cur,cptr->info,
2087 sizeof(hdw->std_info_cur));
2088 cptr->info = &hdw->std_info_cur;
2089 hdw->std_info_cur.def.type_bitmask.bit_names =
2090 hdw->std_mask_ptrs;
2091 hdw->std_info_avail.def.type_bitmask.valid_bits =
2092 valid_std_mask;
2093 }
2094
2095 hdw->eeprom_addr = -1;
2096 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002097 hdw->v4l_minor_number_video = -1;
2098 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002099 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002100 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2101 if (!hdw->ctl_write_buffer) goto fail;
2102 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2103 if (!hdw->ctl_read_buffer) goto fail;
2104 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2105 if (!hdw->ctl_write_urb) goto fail;
2106 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2107 if (!hdw->ctl_read_urb) goto fail;
2108
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002109 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002110 for (idx = 0; idx < PVR_NUM; idx++) {
2111 if (unit_pointers[idx]) continue;
2112 hdw->unit_number = idx;
2113 unit_pointers[idx] = hdw;
2114 break;
2115 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002116 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002117
2118 cnt1 = 0;
2119 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2120 cnt1 += cnt2;
2121 if (hdw->unit_number >= 0) {
2122 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2123 ('a' + hdw->unit_number));
2124 cnt1 += cnt2;
2125 }
2126 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2127 hdw->name[cnt1] = 0;
2128
Mike Isely681c7392007-11-26 01:48:52 -03002129 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2130 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2131 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002132
Mike Iselyd8554972006-06-26 20:58:46 -03002133 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2134 hdw->unit_number,hdw->name);
2135
2136 hdw->tuner_type = -1;
2137 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002138
2139 hdw->usb_intf = intf;
2140 hdw->usb_dev = interface_to_usbdev(intf);
2141
Mike Isely31a18542007-04-08 01:11:47 -03002142 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2143 "usb %s address %d",
2144 hdw->usb_dev->dev.bus_id,
2145 hdw->usb_dev->devnum);
2146
Mike Iselyd8554972006-06-26 20:58:46 -03002147 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2148 usb_set_interface(hdw->usb_dev,ifnum,0);
2149
2150 mutex_init(&hdw->ctl_lock_mutex);
2151 mutex_init(&hdw->big_lock_mutex);
2152
2153 return hdw;
2154 fail:
2155 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002156 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002157 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002158 del_timer_sync(&hdw->encoder_wait_timer);
2159 if (hdw->workqueue) {
2160 flush_workqueue(hdw->workqueue);
2161 destroy_workqueue(hdw->workqueue);
2162 hdw->workqueue = NULL;
2163 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002164 usb_free_urb(hdw->ctl_read_urb);
2165 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002166 kfree(hdw->ctl_read_buffer);
2167 kfree(hdw->ctl_write_buffer);
2168 kfree(hdw->controls);
2169 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002170 kfree(hdw->std_defs);
2171 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002172 kfree(hdw);
2173 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002174 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002175}
2176
2177
2178/* Remove _all_ associations between this driver and the underlying USB
2179 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002180static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002181{
2182 if (hdw->flag_disconnected) return;
2183 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2184 if (hdw->ctl_read_urb) {
2185 usb_kill_urb(hdw->ctl_read_urb);
2186 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002187 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002188 }
2189 if (hdw->ctl_write_urb) {
2190 usb_kill_urb(hdw->ctl_write_urb);
2191 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002192 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002193 }
2194 if (hdw->ctl_read_buffer) {
2195 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002196 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002197 }
2198 if (hdw->ctl_write_buffer) {
2199 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002200 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002201 }
Mike Iselyd8554972006-06-26 20:58:46 -03002202 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002203 hdw->usb_dev = NULL;
2204 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002205 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002206}
2207
2208
2209/* Destroy hardware interaction structure */
2210void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2211{
Mike Isely401c27c2007-09-08 22:11:46 -03002212 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002213 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002214 if (hdw->workqueue) {
2215 flush_workqueue(hdw->workqueue);
2216 destroy_workqueue(hdw->workqueue);
2217 hdw->workqueue = NULL;
2218 }
Mike Isely8f591002008-04-22 14:45:45 -03002219 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002220 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002221 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002222 if (hdw->fw_buffer) {
2223 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002224 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002225 }
2226 if (hdw->vid_stream) {
2227 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002228 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002229 }
Mike Iselyd8554972006-06-26 20:58:46 -03002230 if (hdw->decoder_ctrl) {
2231 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2232 }
2233 pvr2_i2c_core_done(hdw);
2234 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002235 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002236 if ((hdw->unit_number >= 0) &&
2237 (hdw->unit_number < PVR_NUM) &&
2238 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002239 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002240 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002241 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002242 kfree(hdw->controls);
2243 kfree(hdw->mpeg_ctrl_info);
2244 kfree(hdw->std_defs);
2245 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002246 kfree(hdw);
2247}
2248
2249
Mike Iselyd8554972006-06-26 20:58:46 -03002250int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2251{
2252 return (hdw && hdw->flag_ok);
2253}
2254
2255
2256/* Called when hardware has been unplugged */
2257void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2258{
2259 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2260 LOCK_TAKE(hdw->big_lock);
2261 LOCK_TAKE(hdw->ctl_lock);
2262 pvr2_hdw_remove_usb_stuff(hdw);
2263 LOCK_GIVE(hdw->ctl_lock);
2264 LOCK_GIVE(hdw->big_lock);
2265}
2266
2267
2268// Attempt to autoselect an appropriate value for std_enum_cur given
2269// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002270static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002271{
2272 unsigned int idx;
2273 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2274 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2275 hdw->std_enum_cur = idx;
2276 return;
2277 }
2278 }
2279 hdw->std_enum_cur = 0;
2280}
2281
2282
2283// Calculate correct set of enumerated standards based on currently known
2284// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002285static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002286{
2287 struct v4l2_standard *newstd;
2288 unsigned int std_cnt;
2289 unsigned int idx;
2290
2291 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2292
2293 if (hdw->std_defs) {
2294 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002295 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002296 }
2297 hdw->std_enum_cnt = 0;
2298 if (hdw->std_enum_names) {
2299 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002300 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002301 }
2302
2303 if (!std_cnt) {
2304 pvr2_trace(
2305 PVR2_TRACE_ERROR_LEGS,
2306 "WARNING: Failed to identify any viable standards");
2307 }
2308 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2309 hdw->std_enum_names[0] = "none";
2310 for (idx = 0; idx < std_cnt; idx++) {
2311 hdw->std_enum_names[idx+1] =
2312 newstd[idx].name;
2313 }
2314 // Set up the dynamic control for this standard
2315 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2316 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2317 hdw->std_defs = newstd;
2318 hdw->std_enum_cnt = std_cnt+1;
2319 hdw->std_enum_cur = 0;
2320 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2321}
2322
2323
2324int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2325 struct v4l2_standard *std,
2326 unsigned int idx)
2327{
2328 int ret = -EINVAL;
2329 if (!idx) return ret;
2330 LOCK_TAKE(hdw->big_lock); do {
2331 if (idx >= hdw->std_enum_cnt) break;
2332 idx--;
2333 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2334 ret = 0;
2335 } while (0); LOCK_GIVE(hdw->big_lock);
2336 return ret;
2337}
2338
2339
2340/* Get the number of defined controls */
2341unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2342{
Mike Iselyc05c0462006-06-25 20:04:25 -03002343 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002344}
2345
2346
2347/* Retrieve a control handle given its index (0..count-1) */
2348struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2349 unsigned int idx)
2350{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002351 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002352 return hdw->controls + idx;
2353}
2354
2355
2356/* Retrieve a control handle given its index (0..count-1) */
2357struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2358 unsigned int ctl_id)
2359{
2360 struct pvr2_ctrl *cptr;
2361 unsigned int idx;
2362 int i;
2363
2364 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002365 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002366 cptr = hdw->controls + idx;
2367 i = cptr->info->internal_id;
2368 if (i && (i == ctl_id)) return cptr;
2369 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002370 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002371}
2372
2373
Mike Iselya761f432006-06-25 20:04:44 -03002374/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002375struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2376{
2377 struct pvr2_ctrl *cptr;
2378 unsigned int idx;
2379 int i;
2380
2381 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002382 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002383 cptr = hdw->controls + idx;
2384 i = cptr->info->v4l_id;
2385 if (i && (i == ctl_id)) return cptr;
2386 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002387 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002388}
2389
2390
Mike Iselya761f432006-06-25 20:04:44 -03002391/* Given a V4L ID for its immediate predecessor, retrieve the control
2392 structure associated with it. */
2393struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2394 unsigned int ctl_id)
2395{
2396 struct pvr2_ctrl *cptr,*cp2;
2397 unsigned int idx;
2398 int i;
2399
2400 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002401 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002402 for (idx = 0; idx < hdw->control_cnt; idx++) {
2403 cptr = hdw->controls + idx;
2404 i = cptr->info->v4l_id;
2405 if (!i) continue;
2406 if (i <= ctl_id) continue;
2407 if (cp2 && (cp2->info->v4l_id < i)) continue;
2408 cp2 = cptr;
2409 }
2410 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002411 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002412}
2413
2414
Mike Iselyd8554972006-06-26 20:58:46 -03002415static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2416{
2417 switch (tp) {
2418 case pvr2_ctl_int: return "integer";
2419 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002420 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002421 case pvr2_ctl_bitmask: return "bitmask";
2422 }
2423 return "";
2424}
2425
2426
Mike Isely681c7392007-11-26 01:48:52 -03002427/* Figure out if we need to commit control changes. If so, mark internal
2428 state flags to indicate this fact and return true. Otherwise do nothing
2429 else and return false. */
2430static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002431{
Mike Iselyd8554972006-06-26 20:58:46 -03002432 unsigned int idx;
2433 struct pvr2_ctrl *cptr;
2434 int value;
2435 int commit_flag = 0;
2436 char buf[100];
2437 unsigned int bcnt,ccnt;
2438
Mike Iselyc05c0462006-06-25 20:04:25 -03002439 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002440 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002441 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002442 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002443 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002444
Mike Iselyfe23a282007-01-20 00:10:55 -03002445 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002446 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2447 cptr->info->name);
2448 value = 0;
2449 cptr->info->get_value(cptr,&value);
2450 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2451 buf+bcnt,
2452 sizeof(buf)-bcnt,&ccnt);
2453 bcnt += ccnt;
2454 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2455 get_ctrl_typename(cptr->info->type));
2456 pvr2_trace(PVR2_TRACE_CTL,
2457 "/*--TRACE_COMMIT--*/ %.*s",
2458 bcnt,buf);
2459 }
2460
2461 if (!commit_flag) {
2462 /* Nothing has changed */
2463 return 0;
2464 }
2465
Mike Isely681c7392007-11-26 01:48:52 -03002466 hdw->state_pipeline_config = 0;
2467 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2468 pvr2_hdw_state_sched(hdw);
2469
2470 return !0;
2471}
2472
2473
2474/* Perform all operations needed to commit all control changes. This must
2475 be performed in synchronization with the pipeline state and is thus
2476 expected to be called as part of the driver's worker thread. Return
2477 true if commit successful, otherwise return false to indicate that
2478 commit isn't possible at this time. */
2479static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2480{
2481 unsigned int idx;
2482 struct pvr2_ctrl *cptr;
2483 int disruptive_change;
2484
Mike Iselyab062fe2008-06-30 03:32:35 -03002485 /* Handle some required side effects when the video standard is
2486 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03002487 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03002488 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03002489 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03002490 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2491 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03002492 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03002493 } else {
2494 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03002495 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03002496 }
Mike Isely00528d92008-06-30 03:35:52 -03002497 /* Rewrite the vertical resolution to be appropriate to the
2498 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03002499 if (nvres != hdw->res_ver_val) {
2500 hdw->res_ver_val = nvres;
2501 hdw->res_ver_dirty = !0;
2502 }
Mike Isely00528d92008-06-30 03:35:52 -03002503 /* Rewrite the GOP size to be appropriate to the video
2504 standard that has been selected. */
2505 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
2506 struct v4l2_ext_controls cs;
2507 struct v4l2_ext_control c1;
2508 memset(&cs, 0, sizeof(cs));
2509 memset(&c1, 0, sizeof(c1));
2510 cs.controls = &c1;
2511 cs.count = 1;
2512 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
2513 c1.value = gop_size;
2514 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
2515 VIDIOC_S_EXT_CTRLS);
2516 }
Mike Iselyd8554972006-06-26 20:58:46 -03002517 }
2518
Mike Isely38d9a2c2008-03-28 05:30:48 -03002519 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03002520 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2521 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2522 hdw->pathway_state)) {
2523 /* Change of mode being asked for... */
2524 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03002525 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03002526 }
2527 if (!hdw->state_pathway_ok) {
2528 /* Can't commit anything until pathway is ok. */
2529 return 0;
2530 }
Mike Isely681c7392007-11-26 01:48:52 -03002531 /* If any of the below has changed, then we can't do the update
2532 while the pipeline is running. Pipeline must be paused first
2533 and decoder -> encoder connection be made quiescent before we
2534 can proceed. */
2535 disruptive_change =
2536 (hdw->std_dirty ||
2537 hdw->enc_unsafe_stale ||
2538 hdw->srate_dirty ||
2539 hdw->res_ver_dirty ||
2540 hdw->res_hor_dirty ||
2541 hdw->input_dirty ||
2542 (hdw->active_stream_type != hdw->desired_stream_type));
2543 if (disruptive_change && !hdw->state_pipeline_idle) {
2544 /* Pipeline is not idle; we can't proceed. Arrange to
2545 cause pipeline to stop so that we can try this again
2546 later.... */
2547 hdw->state_pipeline_pause = !0;
2548 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002549 }
2550
Mike Iselyb30d2442006-06-25 20:05:01 -03002551 if (hdw->srate_dirty) {
2552 /* Write new sample rate into control structure since
2553 * the master copy is stale. We must track srate
2554 * separate from the mpeg control structure because
2555 * other logic also uses this value. */
2556 struct v4l2_ext_controls cs;
2557 struct v4l2_ext_control c1;
2558 memset(&cs,0,sizeof(cs));
2559 memset(&c1,0,sizeof(c1));
2560 cs.controls = &c1;
2561 cs.count = 1;
2562 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2563 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002564 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002565 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002566
Mike Iselyd8554972006-06-26 20:58:46 -03002567 /* Scan i2c core at this point - before we clear all the dirty
2568 bits. Various parts of the i2c core will notice dirty bits as
2569 appropriate and arrange to broadcast or directly send updates to
2570 the client drivers in order to keep everything in sync */
2571 pvr2_i2c_core_check_stale(hdw);
2572
Mike Iselyc05c0462006-06-25 20:04:25 -03002573 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002574 cptr = hdw->controls + idx;
2575 if (!cptr->info->clear_dirty) continue;
2576 cptr->info->clear_dirty(cptr);
2577 }
2578
Mike Isely681c7392007-11-26 01:48:52 -03002579 if (hdw->active_stream_type != hdw->desired_stream_type) {
2580 /* Handle any side effects of stream config here */
2581 hdw->active_stream_type = hdw->desired_stream_type;
2582 }
2583
Mike Isely1df59f02008-04-21 03:50:39 -03002584 if (hdw->hdw_desc->signal_routing_scheme ==
2585 PVR2_ROUTING_SCHEME_GOTVIEW) {
2586 u32 b;
2587 /* Handle GOTVIEW audio switching */
2588 pvr2_hdw_gpio_get_out(hdw,&b);
2589 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2590 /* Set GPIO 11 */
2591 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
2592 } else {
2593 /* Clear GPIO 11 */
2594 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
2595 }
2596 }
2597
Mike Iselyd8554972006-06-26 20:58:46 -03002598 /* Now execute i2c core update */
2599 pvr2_i2c_core_sync(hdw);
2600
Mike Isely62433e32008-04-22 14:45:40 -03002601 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2602 hdw->state_encoder_run) {
2603 /* If encoder isn't running or it can't be touched, then
2604 this will get worked out later when we start the
2605 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002606 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2607 }
Mike Iselyd8554972006-06-26 20:58:46 -03002608
Mike Isely681c7392007-11-26 01:48:52 -03002609 hdw->state_pipeline_config = !0;
2610 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2611 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002612}
2613
2614
2615int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2616{
Mike Isely681c7392007-11-26 01:48:52 -03002617 int fl;
2618 LOCK_TAKE(hdw->big_lock);
2619 fl = pvr2_hdw_commit_setup(hdw);
2620 LOCK_GIVE(hdw->big_lock);
2621 if (!fl) return 0;
2622 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002623}
2624
2625
Mike Isely681c7392007-11-26 01:48:52 -03002626static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002627{
Mike Isely681c7392007-11-26 01:48:52 -03002628 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002629 LOCK_TAKE(hdw->big_lock); do {
2630 pvr2_i2c_core_sync(hdw);
2631 } while (0); LOCK_GIVE(hdw->big_lock);
2632}
2633
2634
Mike Isely681c7392007-11-26 01:48:52 -03002635static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002636{
Mike Isely681c7392007-11-26 01:48:52 -03002637 int fl = 0;
2638 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002639 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002640 fl = pvr2_hdw_state_eval(hdw);
2641 } while (0); LOCK_GIVE(hdw->big_lock);
2642 if (fl && hdw->state_func) {
2643 hdw->state_func(hdw->state_data);
2644 }
2645}
2646
2647
Mike Isely681c7392007-11-26 01:48:52 -03002648static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002649{
Mike Isely681c7392007-11-26 01:48:52 -03002650 return wait_event_interruptible(
2651 hdw->state_wait_data,
2652 (hdw->state_stale == 0) &&
2653 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002654}
2655
Mike Isely681c7392007-11-26 01:48:52 -03002656
Mike Iselyd8554972006-06-26 20:58:46 -03002657/* Return name for this driver instance */
2658const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2659{
2660 return hdw->name;
2661}
2662
2663
Mike Isely78a47102007-11-26 01:58:20 -03002664const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2665{
2666 return hdw->hdw_desc->description;
2667}
2668
2669
2670const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2671{
2672 return hdw->hdw_desc->shortname;
2673}
2674
2675
Mike Iselyd8554972006-06-26 20:58:46 -03002676int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2677{
2678 int result;
2679 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002680 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002681 result = pvr2_send_request(hdw,
2682 hdw->cmd_buffer,1,
2683 hdw->cmd_buffer,1);
2684 if (result < 0) break;
2685 result = (hdw->cmd_buffer[0] != 0);
2686 } while(0); LOCK_GIVE(hdw->ctl_lock);
2687 return result;
2688}
2689
2690
Mike Isely18103c572007-01-20 00:09:47 -03002691/* Execute poll of tuner status */
2692void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002693{
Mike Iselyd8554972006-06-26 20:58:46 -03002694 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002695 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002696 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002697}
2698
2699
2700/* Return information about the tuner */
2701int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2702{
2703 LOCK_TAKE(hdw->big_lock); do {
2704 if (hdw->tuner_signal_stale) {
2705 pvr2_i2c_core_status_poll(hdw);
2706 }
2707 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2708 } while (0); LOCK_GIVE(hdw->big_lock);
2709 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002710}
2711
2712
2713/* Get handle to video output stream */
2714struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2715{
2716 return hp->vid_stream;
2717}
2718
2719
2720void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2721{
Mike Isely4f1a3e52006-06-25 20:04:31 -03002722 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002723 LOCK_TAKE(hdw->big_lock); do {
2724 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03002725 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002726 pvr2_i2c_core_check_stale(hdw);
2727 hdw->log_requested = 0;
2728 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03002729 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03002730 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03002731 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03002732 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002733 } while (0); LOCK_GIVE(hdw->big_lock);
2734}
2735
Mike Isely4db666c2007-09-08 22:16:27 -03002736
2737/* Grab EEPROM contents, needed for direct method. */
2738#define EEPROM_SIZE 8192
2739#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
2740static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
2741{
2742 struct i2c_msg msg[2];
2743 u8 *eeprom;
2744 u8 iadd[2];
2745 u8 addr;
2746 u16 eepromSize;
2747 unsigned int offs;
2748 int ret;
2749 int mode16 = 0;
2750 unsigned pcnt,tcnt;
2751 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
2752 if (!eeprom) {
2753 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2754 "Failed to allocate memory"
2755 " required to read eeprom");
2756 return NULL;
2757 }
2758
2759 trace_eeprom("Value for eeprom addr from controller was 0x%x",
2760 hdw->eeprom_addr);
2761 addr = hdw->eeprom_addr;
2762 /* Seems that if the high bit is set, then the *real* eeprom
2763 address is shifted right now bit position (noticed this in
2764 newer PVR USB2 hardware) */
2765 if (addr & 0x80) addr >>= 1;
2766
2767 /* FX2 documentation states that a 16bit-addressed eeprom is
2768 expected if the I2C address is an odd number (yeah, this is
2769 strange but it's what they do) */
2770 mode16 = (addr & 1);
2771 eepromSize = (mode16 ? EEPROM_SIZE : 256);
2772 trace_eeprom("Examining %d byte eeprom at location 0x%x"
2773 " using %d bit addressing",eepromSize,addr,
2774 mode16 ? 16 : 8);
2775
2776 msg[0].addr = addr;
2777 msg[0].flags = 0;
2778 msg[0].len = mode16 ? 2 : 1;
2779 msg[0].buf = iadd;
2780 msg[1].addr = addr;
2781 msg[1].flags = I2C_M_RD;
2782
2783 /* We have to do the actual eeprom data fetch ourselves, because
2784 (1) we're only fetching part of the eeprom, and (2) if we were
2785 getting the whole thing our I2C driver can't grab it in one
2786 pass - which is what tveeprom is otherwise going to attempt */
2787 memset(eeprom,0,EEPROM_SIZE);
2788 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
2789 pcnt = 16;
2790 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
2791 offs = tcnt + (eepromSize - EEPROM_SIZE);
2792 if (mode16) {
2793 iadd[0] = offs >> 8;
2794 iadd[1] = offs;
2795 } else {
2796 iadd[0] = offs;
2797 }
2798 msg[1].len = pcnt;
2799 msg[1].buf = eeprom+tcnt;
2800 if ((ret = i2c_transfer(&hdw->i2c_adap,
2801 msg,ARRAY_SIZE(msg))) != 2) {
2802 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2803 "eeprom fetch set offs err=%d",ret);
2804 kfree(eeprom);
2805 return NULL;
2806 }
2807 }
2808 return eeprom;
2809}
2810
2811
2812void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
2813 int prom_flag,
2814 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002815{
2816 int ret;
2817 u16 address;
2818 unsigned int pipe;
2819 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00002820 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03002821
2822 if (!enable_flag) {
2823 pvr2_trace(PVR2_TRACE_FIRMWARE,
2824 "Cleaning up after CPU firmware fetch");
2825 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002826 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002827 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03002828 if (hdw->fw_cpu_flag) {
2829 /* Now release the CPU. It will disconnect
2830 and reconnect later. */
2831 pvr2_hdw_cpureset_assert(hdw,0);
2832 }
Mike Iselyd8554972006-06-26 20:58:46 -03002833 break;
2834 }
2835
Mike Isely4db666c2007-09-08 22:16:27 -03002836 hdw->fw_cpu_flag = (prom_flag == 0);
2837 if (hdw->fw_cpu_flag) {
2838 pvr2_trace(PVR2_TRACE_FIRMWARE,
2839 "Preparing to suck out CPU firmware");
2840 hdw->fw_size = 0x2000;
2841 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
2842 if (!hdw->fw_buffer) {
2843 hdw->fw_size = 0;
2844 break;
2845 }
2846
2847 /* We have to hold the CPU during firmware upload. */
2848 pvr2_hdw_cpureset_assert(hdw,1);
2849
2850 /* download the firmware from address 0000-1fff in 2048
2851 (=0x800) bytes chunk. */
2852
2853 pvr2_trace(PVR2_TRACE_FIRMWARE,
2854 "Grabbing CPU firmware");
2855 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2856 for(address = 0; address < hdw->fw_size;
2857 address += 0x800) {
2858 ret = usb_control_msg(hdw->usb_dev,pipe,
2859 0xa0,0xc0,
2860 address,0,
2861 hdw->fw_buffer+address,
2862 0x800,HZ);
2863 if (ret < 0) break;
2864 }
2865
2866 pvr2_trace(PVR2_TRACE_FIRMWARE,
2867 "Done grabbing CPU firmware");
2868 } else {
2869 pvr2_trace(PVR2_TRACE_FIRMWARE,
2870 "Sucking down EEPROM contents");
2871 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
2872 if (!hdw->fw_buffer) {
2873 pvr2_trace(PVR2_TRACE_FIRMWARE,
2874 "EEPROM content suck failed.");
2875 break;
2876 }
2877 hdw->fw_size = EEPROM_SIZE;
2878 pvr2_trace(PVR2_TRACE_FIRMWARE,
2879 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03002880 }
2881
Mike Iselyd8554972006-06-26 20:58:46 -03002882 } while (0); LOCK_GIVE(hdw->big_lock);
2883}
2884
2885
2886/* Return true if we're in a mode for retrieval CPU firmware */
2887int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2888{
Al Viro5fa12472008-03-29 03:07:38 +00002889 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002890}
2891
2892
2893int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2894 char *buf,unsigned int cnt)
2895{
2896 int ret = -EINVAL;
2897 LOCK_TAKE(hdw->big_lock); do {
2898 if (!buf) break;
2899 if (!cnt) break;
2900
2901 if (!hdw->fw_buffer) {
2902 ret = -EIO;
2903 break;
2904 }
2905
2906 if (offs >= hdw->fw_size) {
2907 pvr2_trace(PVR2_TRACE_FIRMWARE,
2908 "Read firmware data offs=%d EOF",
2909 offs);
2910 ret = 0;
2911 break;
2912 }
2913
2914 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2915
2916 memcpy(buf,hdw->fw_buffer+offs,cnt);
2917
2918 pvr2_trace(PVR2_TRACE_FIRMWARE,
2919 "Read firmware data offs=%d cnt=%d",
2920 offs,cnt);
2921 ret = cnt;
2922 } while (0); LOCK_GIVE(hdw->big_lock);
2923
2924 return ret;
2925}
2926
2927
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002928int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002929 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03002930{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002931 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002932 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2933 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2934 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002935 default: return -1;
2936 }
Mike Iselyd8554972006-06-26 20:58:46 -03002937}
2938
2939
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03002940/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002941void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002942 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03002943{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002944 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002945 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2946 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2947 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002948 default: break;
2949 }
Mike Iselyd8554972006-06-26 20:58:46 -03002950}
2951
2952
David Howells7d12e782006-10-05 14:55:46 +01002953static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002954{
2955 struct pvr2_hdw *hdw = urb->context;
2956 hdw->ctl_write_pend_flag = 0;
2957 if (hdw->ctl_read_pend_flag) return;
2958 complete(&hdw->ctl_done);
2959}
2960
2961
David Howells7d12e782006-10-05 14:55:46 +01002962static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002963{
2964 struct pvr2_hdw *hdw = urb->context;
2965 hdw->ctl_read_pend_flag = 0;
2966 if (hdw->ctl_write_pend_flag) return;
2967 complete(&hdw->ctl_done);
2968}
2969
2970
2971static void pvr2_ctl_timeout(unsigned long data)
2972{
2973 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2974 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2975 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002976 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002977 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002978 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002979 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03002980 }
2981}
2982
2983
Mike Iselye61b6fc2006-07-18 22:42:18 -03002984/* Issue a command and get a response from the device. This extended
2985 version includes a probe flag (which if set means that device errors
2986 should not be logged or treated as fatal) and a timeout in jiffies.
2987 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002988static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2989 unsigned int timeout,int probe_fl,
2990 void *write_data,unsigned int write_len,
2991 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03002992{
2993 unsigned int idx;
2994 int status = 0;
2995 struct timer_list timer;
2996 if (!hdw->ctl_lock_held) {
2997 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2998 "Attempted to execute control transfer"
2999 " without lock!!");
3000 return -EDEADLK;
3001 }
Mike Isely681c7392007-11-26 01:48:52 -03003002 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003003 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3004 "Attempted to execute control transfer"
3005 " when device not ok");
3006 return -EIO;
3007 }
3008 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3009 if (!probe_fl) {
3010 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3011 "Attempted to execute control transfer"
3012 " when USB is disconnected");
3013 }
3014 return -ENOTTY;
3015 }
3016
3017 /* Ensure that we have sane parameters */
3018 if (!write_data) write_len = 0;
3019 if (!read_data) read_len = 0;
3020 if (write_len > PVR2_CTL_BUFFSIZE) {
3021 pvr2_trace(
3022 PVR2_TRACE_ERROR_LEGS,
3023 "Attempted to execute %d byte"
3024 " control-write transfer (limit=%d)",
3025 write_len,PVR2_CTL_BUFFSIZE);
3026 return -EINVAL;
3027 }
3028 if (read_len > PVR2_CTL_BUFFSIZE) {
3029 pvr2_trace(
3030 PVR2_TRACE_ERROR_LEGS,
3031 "Attempted to execute %d byte"
3032 " control-read transfer (limit=%d)",
3033 write_len,PVR2_CTL_BUFFSIZE);
3034 return -EINVAL;
3035 }
3036 if ((!write_len) && (!read_len)) {
3037 pvr2_trace(
3038 PVR2_TRACE_ERROR_LEGS,
3039 "Attempted to execute null control transfer?");
3040 return -EINVAL;
3041 }
3042
3043
3044 hdw->cmd_debug_state = 1;
3045 if (write_len) {
3046 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3047 } else {
3048 hdw->cmd_debug_code = 0;
3049 }
3050 hdw->cmd_debug_write_len = write_len;
3051 hdw->cmd_debug_read_len = read_len;
3052
3053 /* Initialize common stuff */
3054 init_completion(&hdw->ctl_done);
3055 hdw->ctl_timeout_flag = 0;
3056 hdw->ctl_write_pend_flag = 0;
3057 hdw->ctl_read_pend_flag = 0;
3058 init_timer(&timer);
3059 timer.expires = jiffies + timeout;
3060 timer.data = (unsigned long)hdw;
3061 timer.function = pvr2_ctl_timeout;
3062
3063 if (write_len) {
3064 hdw->cmd_debug_state = 2;
3065 /* Transfer write data to internal buffer */
3066 for (idx = 0; idx < write_len; idx++) {
3067 hdw->ctl_write_buffer[idx] =
3068 ((unsigned char *)write_data)[idx];
3069 }
3070 /* Initiate a write request */
3071 usb_fill_bulk_urb(hdw->ctl_write_urb,
3072 hdw->usb_dev,
3073 usb_sndbulkpipe(hdw->usb_dev,
3074 PVR2_CTL_WRITE_ENDPOINT),
3075 hdw->ctl_write_buffer,
3076 write_len,
3077 pvr2_ctl_write_complete,
3078 hdw);
3079 hdw->ctl_write_urb->actual_length = 0;
3080 hdw->ctl_write_pend_flag = !0;
3081 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3082 if (status < 0) {
3083 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3084 "Failed to submit write-control"
3085 " URB status=%d",status);
3086 hdw->ctl_write_pend_flag = 0;
3087 goto done;
3088 }
3089 }
3090
3091 if (read_len) {
3092 hdw->cmd_debug_state = 3;
3093 memset(hdw->ctl_read_buffer,0x43,read_len);
3094 /* Initiate a read request */
3095 usb_fill_bulk_urb(hdw->ctl_read_urb,
3096 hdw->usb_dev,
3097 usb_rcvbulkpipe(hdw->usb_dev,
3098 PVR2_CTL_READ_ENDPOINT),
3099 hdw->ctl_read_buffer,
3100 read_len,
3101 pvr2_ctl_read_complete,
3102 hdw);
3103 hdw->ctl_read_urb->actual_length = 0;
3104 hdw->ctl_read_pend_flag = !0;
3105 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3106 if (status < 0) {
3107 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3108 "Failed to submit read-control"
3109 " URB status=%d",status);
3110 hdw->ctl_read_pend_flag = 0;
3111 goto done;
3112 }
3113 }
3114
3115 /* Start timer */
3116 add_timer(&timer);
3117
3118 /* Now wait for all I/O to complete */
3119 hdw->cmd_debug_state = 4;
3120 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3121 wait_for_completion(&hdw->ctl_done);
3122 }
3123 hdw->cmd_debug_state = 5;
3124
3125 /* Stop timer */
3126 del_timer_sync(&timer);
3127
3128 hdw->cmd_debug_state = 6;
3129 status = 0;
3130
3131 if (hdw->ctl_timeout_flag) {
3132 status = -ETIMEDOUT;
3133 if (!probe_fl) {
3134 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3135 "Timed out control-write");
3136 }
3137 goto done;
3138 }
3139
3140 if (write_len) {
3141 /* Validate results of write request */
3142 if ((hdw->ctl_write_urb->status != 0) &&
3143 (hdw->ctl_write_urb->status != -ENOENT) &&
3144 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3145 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3146 /* USB subsystem is reporting some kind of failure
3147 on the write */
3148 status = hdw->ctl_write_urb->status;
3149 if (!probe_fl) {
3150 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3151 "control-write URB failure,"
3152 " status=%d",
3153 status);
3154 }
3155 goto done;
3156 }
3157 if (hdw->ctl_write_urb->actual_length < write_len) {
3158 /* Failed to write enough data */
3159 status = -EIO;
3160 if (!probe_fl) {
3161 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3162 "control-write URB short,"
3163 " expected=%d got=%d",
3164 write_len,
3165 hdw->ctl_write_urb->actual_length);
3166 }
3167 goto done;
3168 }
3169 }
3170 if (read_len) {
3171 /* Validate results of read request */
3172 if ((hdw->ctl_read_urb->status != 0) &&
3173 (hdw->ctl_read_urb->status != -ENOENT) &&
3174 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3175 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3176 /* USB subsystem is reporting some kind of failure
3177 on the read */
3178 status = hdw->ctl_read_urb->status;
3179 if (!probe_fl) {
3180 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3181 "control-read URB failure,"
3182 " status=%d",
3183 status);
3184 }
3185 goto done;
3186 }
3187 if (hdw->ctl_read_urb->actual_length < read_len) {
3188 /* Failed to read enough data */
3189 status = -EIO;
3190 if (!probe_fl) {
3191 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3192 "control-read URB short,"
3193 " expected=%d got=%d",
3194 read_len,
3195 hdw->ctl_read_urb->actual_length);
3196 }
3197 goto done;
3198 }
3199 /* Transfer retrieved data out from internal buffer */
3200 for (idx = 0; idx < read_len; idx++) {
3201 ((unsigned char *)read_data)[idx] =
3202 hdw->ctl_read_buffer[idx];
3203 }
3204 }
3205
3206 done:
3207
3208 hdw->cmd_debug_state = 0;
3209 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003210 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003211 }
3212 return status;
3213}
3214
3215
3216int pvr2_send_request(struct pvr2_hdw *hdw,
3217 void *write_data,unsigned int write_len,
3218 void *read_data,unsigned int read_len)
3219{
3220 return pvr2_send_request_ex(hdw,HZ*4,0,
3221 write_data,write_len,
3222 read_data,read_len);
3223}
3224
Mike Isely1c9d10d2008-03-28 05:38:54 -03003225
3226static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3227{
3228 int ret;
3229 unsigned int cnt = 1;
3230 unsigned int args = 0;
3231 LOCK_TAKE(hdw->ctl_lock);
3232 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3233 args = (cmdcode >> 8) & 0xffu;
3234 args = (args > 2) ? 2 : args;
3235 if (args) {
3236 cnt += args;
3237 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3238 if (args > 1) {
3239 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3240 }
3241 }
3242 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3243 unsigned int idx;
3244 unsigned int ccnt,bcnt;
3245 char tbuf[50];
3246 cmdcode &= 0xffu;
3247 bcnt = 0;
3248 ccnt = scnprintf(tbuf+bcnt,
3249 sizeof(tbuf)-bcnt,
3250 "Sending FX2 command 0x%x",cmdcode);
3251 bcnt += ccnt;
3252 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3253 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3254 ccnt = scnprintf(tbuf+bcnt,
3255 sizeof(tbuf)-bcnt,
3256 " \"%s\"",
3257 pvr2_fx2cmd_desc[idx].desc);
3258 bcnt += ccnt;
3259 break;
3260 }
3261 }
3262 if (args) {
3263 ccnt = scnprintf(tbuf+bcnt,
3264 sizeof(tbuf)-bcnt,
3265 " (%u",hdw->cmd_buffer[1]);
3266 bcnt += ccnt;
3267 if (args > 1) {
3268 ccnt = scnprintf(tbuf+bcnt,
3269 sizeof(tbuf)-bcnt,
3270 ",%u",hdw->cmd_buffer[2]);
3271 bcnt += ccnt;
3272 }
3273 ccnt = scnprintf(tbuf+bcnt,
3274 sizeof(tbuf)-bcnt,
3275 ")");
3276 bcnt += ccnt;
3277 }
3278 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3279 }
3280 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3281 LOCK_GIVE(hdw->ctl_lock);
3282 return ret;
3283}
3284
3285
Mike Iselyd8554972006-06-26 20:58:46 -03003286int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3287{
3288 int ret;
3289
3290 LOCK_TAKE(hdw->ctl_lock);
3291
Michael Krufky8d364362007-01-22 02:17:55 -03003292 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003293 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3294 hdw->cmd_buffer[5] = 0;
3295 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3296 hdw->cmd_buffer[7] = reg & 0xff;
3297
3298
3299 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3300
3301 LOCK_GIVE(hdw->ctl_lock);
3302
3303 return ret;
3304}
3305
3306
Adrian Bunk07e337e2006-06-30 11:30:20 -03003307static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003308{
3309 int ret = 0;
3310
3311 LOCK_TAKE(hdw->ctl_lock);
3312
Michael Krufky8d364362007-01-22 02:17:55 -03003313 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003314 hdw->cmd_buffer[1] = 0;
3315 hdw->cmd_buffer[2] = 0;
3316 hdw->cmd_buffer[3] = 0;
3317 hdw->cmd_buffer[4] = 0;
3318 hdw->cmd_buffer[5] = 0;
3319 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3320 hdw->cmd_buffer[7] = reg & 0xff;
3321
3322 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3323 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3324
3325 LOCK_GIVE(hdw->ctl_lock);
3326
3327 return ret;
3328}
3329
3330
Mike Isely681c7392007-11-26 01:48:52 -03003331void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003332{
3333 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003334 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3335 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003336 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003337 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003338 }
Mike Isely681c7392007-11-26 01:48:52 -03003339 hdw->flag_ok = 0;
3340 trace_stbit("flag_ok",hdw->flag_ok);
3341 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003342}
3343
3344
3345void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3346{
3347 int ret;
3348 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003349 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003350 if (ret == 1) {
3351 ret = usb_reset_device(hdw->usb_dev);
3352 usb_unlock_device(hdw->usb_dev);
3353 } else {
3354 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3355 "Failed to lock USB device ret=%d",ret);
3356 }
3357 if (init_pause_msec) {
3358 pvr2_trace(PVR2_TRACE_INFO,
3359 "Waiting %u msec for hardware to settle",
3360 init_pause_msec);
3361 msleep(init_pause_msec);
3362 }
3363
3364}
3365
3366
3367void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3368{
3369 char da[1];
3370 unsigned int pipe;
3371 int ret;
3372
3373 if (!hdw->usb_dev) return;
3374
3375 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3376
3377 da[0] = val ? 0x01 : 0x00;
3378
3379 /* Write the CPUCS register on the 8051. The lsb of the register
3380 is the reset bit; a 1 asserts reset while a 0 clears it. */
3381 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3382 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3383 if (ret < 0) {
3384 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3385 "cpureset_assert(%d) error=%d",val,ret);
3386 pvr2_hdw_render_useless(hdw);
3387 }
3388}
3389
3390
3391int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3392{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003393 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03003394}
3395
3396
Michael Krufkye1edb192008-04-22 14:45:39 -03003397int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3398{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003399 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03003400}
3401
Mike Isely1c9d10d2008-03-28 05:38:54 -03003402
Michael Krufkye1edb192008-04-22 14:45:39 -03003403int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3404{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003405 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03003406}
3407
Mike Iselyd8554972006-06-26 20:58:46 -03003408
3409int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3410{
3411 if (!hdw->decoder_ctrl) {
3412 pvr2_trace(PVR2_TRACE_INIT,
3413 "Unable to reset decoder: nothing attached");
3414 return -ENOTTY;
3415 }
3416
3417 if (!hdw->decoder_ctrl->force_reset) {
3418 pvr2_trace(PVR2_TRACE_INIT,
3419 "Unable to reset decoder: not implemented");
3420 return -ENOTTY;
3421 }
3422
3423 pvr2_trace(PVR2_TRACE_INIT,
3424 "Requesting decoder reset");
3425 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3426 return 0;
3427}
3428
3429
Mike Isely62433e32008-04-22 14:45:40 -03003430static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003431{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003432 hdw->flag_ok = !0;
3433 return pvr2_issue_simple_cmd(hdw,
3434 FX2CMD_HCW_DEMOD_RESETIN |
3435 (1 << 8) |
3436 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03003437}
3438
Mike Isely84147f32008-04-22 14:45:40 -03003439
Mike Isely62433e32008-04-22 14:45:40 -03003440static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003441{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003442 hdw->flag_ok = !0;
3443 return pvr2_issue_simple_cmd(hdw,(onoff ?
3444 FX2CMD_ONAIR_DTV_POWER_ON :
3445 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003446}
3447
Mike Isely62433e32008-04-22 14:45:40 -03003448
3449static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3450 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003451{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003452 return pvr2_issue_simple_cmd(hdw,(onoff ?
3453 FX2CMD_ONAIR_DTV_STREAMING_ON :
3454 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003455}
3456
Mike Isely62433e32008-04-22 14:45:40 -03003457
3458static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3459{
3460 int cmode;
3461 /* Compare digital/analog desired setting with current setting. If
3462 they don't match, fix it... */
3463 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3464 if (cmode == hdw->pathway_state) {
3465 /* They match; nothing to do */
3466 return;
3467 }
3468
3469 switch (hdw->hdw_desc->digital_control_scheme) {
3470 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3471 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3472 if (cmode == PVR2_PATHWAY_ANALOG) {
3473 /* If moving to analog mode, also force the decoder
3474 to reset. If no decoder is attached, then it's
3475 ok to ignore this because if/when the decoder
3476 attaches, it will reset itself at that time. */
3477 pvr2_hdw_cmd_decoder_reset(hdw);
3478 }
3479 break;
3480 case PVR2_DIGITAL_SCHEME_ONAIR:
3481 /* Supposedly we should always have the power on whether in
3482 digital or analog mode. But for now do what appears to
3483 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003484 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03003485 break;
3486 default: break;
3487 }
3488
Mike Isely1b9c18c2008-04-22 14:45:41 -03003489 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03003490 hdw->pathway_state = cmode;
3491}
3492
3493
Adrian Bunke9b59f62008-05-10 04:35:24 -03003494static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03003495{
3496 /* change some GPIO data
3497 *
3498 * note: bit d7 of dir appears to control the LED,
3499 * so we shut it off here.
3500 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03003501 */
Mike Isely40381cb2008-04-22 14:45:42 -03003502 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003503 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03003504 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003505 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03003506 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03003507 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03003508}
Mike Iselyc55a97d2008-04-22 14:45:41 -03003509
Mike Isely40381cb2008-04-22 14:45:42 -03003510
3511typedef void (*led_method_func)(struct pvr2_hdw *,int);
3512
3513static led_method_func led_methods[] = {
3514 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
3515};
3516
3517
3518/* Toggle LED */
3519static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
3520{
3521 unsigned int scheme_id;
3522 led_method_func fp;
3523
3524 if ((!onoff) == (!hdw->led_on)) return;
3525
3526 hdw->led_on = onoff != 0;
3527
3528 scheme_id = hdw->hdw_desc->led_scheme;
3529 if (scheme_id < ARRAY_SIZE(led_methods)) {
3530 fp = led_methods[scheme_id];
3531 } else {
3532 fp = NULL;
3533 }
3534
3535 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03003536}
3537
3538
Mike Iselye61b6fc2006-07-18 22:42:18 -03003539/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003540static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003541{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003542 int ret;
3543
3544 /* If we're in analog mode, then just issue the usual analog
3545 command. */
3546 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3547 return pvr2_issue_simple_cmd(hdw,
3548 (runFl ?
3549 FX2CMD_STREAMING_ON :
3550 FX2CMD_STREAMING_OFF));
3551 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03003552 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003553
3554 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
3555 /* Whoops, we don't know what mode we're in... */
3556 return -EINVAL;
3557 }
3558
3559 /* To get here we have to be in digital mode. The mechanism here
3560 is unfortunately different for different vendors. So we switch
3561 on the device's digital scheme attribute in order to figure out
3562 what to do. */
3563 switch (hdw->hdw_desc->digital_control_scheme) {
3564 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3565 return pvr2_issue_simple_cmd(hdw,
3566 (runFl ?
3567 FX2CMD_HCW_DTV_STREAMING_ON :
3568 FX2CMD_HCW_DTV_STREAMING_OFF));
3569 case PVR2_DIGITAL_SCHEME_ONAIR:
3570 ret = pvr2_issue_simple_cmd(hdw,
3571 (runFl ?
3572 FX2CMD_STREAMING_ON :
3573 FX2CMD_STREAMING_OFF));
3574 if (ret) return ret;
3575 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
3576 default:
3577 return -EINVAL;
3578 }
Mike Iselyd8554972006-06-26 20:58:46 -03003579}
3580
3581
Mike Isely62433e32008-04-22 14:45:40 -03003582/* Evaluate whether or not state_pathway_ok can change */
3583static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3584{
3585 if (hdw->state_pathway_ok) {
3586 /* Nothing to do if pathway is already ok */
3587 return 0;
3588 }
3589 if (!hdw->state_pipeline_idle) {
3590 /* Not allowed to change anything if pipeline is not idle */
3591 return 0;
3592 }
3593 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3594 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003595 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003596 return !0;
3597}
3598
3599
Mike Isely681c7392007-11-26 01:48:52 -03003600/* Evaluate whether or not state_encoder_ok can change */
3601static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3602{
3603 if (hdw->state_encoder_ok) return 0;
3604 if (hdw->flag_tripped) return 0;
3605 if (hdw->state_encoder_run) return 0;
3606 if (hdw->state_encoder_config) return 0;
3607 if (hdw->state_decoder_run) return 0;
3608 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003609 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
3610 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
3611 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
3612 return 0;
3613 }
3614
Mike Isely681c7392007-11-26 01:48:52 -03003615 if (pvr2_upload_firmware2(hdw) < 0) {
3616 hdw->flag_tripped = !0;
3617 trace_stbit("flag_tripped",hdw->flag_tripped);
3618 return !0;
3619 }
3620 hdw->state_encoder_ok = !0;
3621 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3622 return !0;
3623}
3624
3625
3626/* Evaluate whether or not state_encoder_config can change */
3627static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3628{
3629 if (hdw->state_encoder_config) {
3630 if (hdw->state_encoder_ok) {
3631 if (hdw->state_pipeline_req &&
3632 !hdw->state_pipeline_pause) return 0;
3633 }
3634 hdw->state_encoder_config = 0;
3635 hdw->state_encoder_waitok = 0;
3636 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3637 /* paranoia - solve race if timer just completed */
3638 del_timer_sync(&hdw->encoder_wait_timer);
3639 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003640 if (!hdw->state_pathway_ok ||
3641 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3642 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003643 !hdw->state_pipeline_idle ||
3644 hdw->state_pipeline_pause ||
3645 !hdw->state_pipeline_req ||
3646 !hdw->state_pipeline_config) {
3647 /* We must reset the enforced wait interval if
3648 anything has happened that might have disturbed
3649 the encoder. This should be a rare case. */
3650 if (timer_pending(&hdw->encoder_wait_timer)) {
3651 del_timer_sync(&hdw->encoder_wait_timer);
3652 }
3653 if (hdw->state_encoder_waitok) {
3654 /* Must clear the state - therefore we did
3655 something to a state bit and must also
3656 return true. */
3657 hdw->state_encoder_waitok = 0;
3658 trace_stbit("state_encoder_waitok",
3659 hdw->state_encoder_waitok);
3660 return !0;
3661 }
3662 return 0;
3663 }
3664 if (!hdw->state_encoder_waitok) {
3665 if (!timer_pending(&hdw->encoder_wait_timer)) {
3666 /* waitok flag wasn't set and timer isn't
3667 running. Check flag once more to avoid
3668 a race then start the timer. This is
3669 the point when we measure out a minimal
3670 quiet interval before doing something to
3671 the encoder. */
3672 if (!hdw->state_encoder_waitok) {
3673 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003674 jiffies +
3675 (HZ * TIME_MSEC_ENCODER_WAIT
3676 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03003677 add_timer(&hdw->encoder_wait_timer);
3678 }
3679 }
3680 /* We can't continue until we know we have been
3681 quiet for the interval measured by this
3682 timer. */
3683 return 0;
3684 }
3685 pvr2_encoder_configure(hdw);
3686 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3687 }
3688 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3689 return !0;
3690}
3691
3692
Mike Iselyd913d632008-04-06 04:04:35 -03003693/* Return true if the encoder should not be running. */
3694static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
3695{
3696 if (!hdw->state_encoder_ok) {
3697 /* Encoder isn't healthy at the moment, so stop it. */
3698 return !0;
3699 }
3700 if (!hdw->state_pathway_ok) {
3701 /* Mode is not understood at the moment (i.e. it wants to
3702 change), so encoder must be stopped. */
3703 return !0;
3704 }
3705
3706 switch (hdw->pathway_state) {
3707 case PVR2_PATHWAY_ANALOG:
3708 if (!hdw->state_decoder_run) {
3709 /* We're in analog mode and the decoder is not
3710 running; thus the encoder should be stopped as
3711 well. */
3712 return !0;
3713 }
3714 break;
3715 case PVR2_PATHWAY_DIGITAL:
3716 if (hdw->state_encoder_runok) {
3717 /* This is a funny case. We're in digital mode so
3718 really the encoder should be stopped. However
3719 if it really is running, only kill it after
3720 runok has been set. This gives a chance for the
3721 onair quirk to function (encoder must run
3722 briefly first, at least once, before onair
3723 digital streaming can work). */
3724 return !0;
3725 }
3726 break;
3727 default:
3728 /* Unknown mode; so encoder should be stopped. */
3729 return !0;
3730 }
3731
3732 /* If we get here, we haven't found a reason to stop the
3733 encoder. */
3734 return 0;
3735}
3736
3737
3738/* Return true if the encoder should be running. */
3739static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
3740{
3741 if (!hdw->state_encoder_ok) {
3742 /* Don't run the encoder if it isn't healthy... */
3743 return 0;
3744 }
3745 if (!hdw->state_pathway_ok) {
3746 /* Don't run the encoder if we don't (yet) know what mode
3747 we need to be in... */
3748 return 0;
3749 }
3750
3751 switch (hdw->pathway_state) {
3752 case PVR2_PATHWAY_ANALOG:
3753 if (hdw->state_decoder_run) {
3754 /* In analog mode, if the decoder is running, then
3755 run the encoder. */
3756 return !0;
3757 }
3758 break;
3759 case PVR2_PATHWAY_DIGITAL:
3760 if ((hdw->hdw_desc->digital_control_scheme ==
3761 PVR2_DIGITAL_SCHEME_ONAIR) &&
3762 !hdw->state_encoder_runok) {
3763 /* This is a quirk. OnAir hardware won't stream
3764 digital until the encoder has been run at least
3765 once, for a minimal period of time (empiricially
3766 measured to be 1/4 second). So if we're on
3767 OnAir hardware and the encoder has never been
3768 run at all, then start the encoder. Normal
3769 state machine logic in the driver will
3770 automatically handle the remaining bits. */
3771 return !0;
3772 }
3773 break;
3774 default:
3775 /* For completeness (unknown mode; encoder won't run ever) */
3776 break;
3777 }
3778 /* If we get here, then we haven't found any reason to run the
3779 encoder, so don't run it. */
3780 return 0;
3781}
3782
3783
Mike Isely681c7392007-11-26 01:48:52 -03003784/* Evaluate whether or not state_encoder_run can change */
3785static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3786{
3787 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03003788 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003789 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03003790 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03003791 if (pvr2_encoder_stop(hdw) < 0) return !0;
3792 }
3793 hdw->state_encoder_run = 0;
3794 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03003795 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003796 if (pvr2_encoder_start(hdw) < 0) return !0;
3797 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03003798 if (!hdw->state_encoder_runok) {
3799 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003800 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03003801 add_timer(&hdw->encoder_run_timer);
3802 }
Mike Isely681c7392007-11-26 01:48:52 -03003803 }
3804 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3805 return !0;
3806}
3807
3808
3809/* Timeout function for quiescent timer. */
3810static void pvr2_hdw_quiescent_timeout(unsigned long data)
3811{
3812 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3813 hdw->state_decoder_quiescent = !0;
3814 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3815 hdw->state_stale = !0;
3816 queue_work(hdw->workqueue,&hdw->workpoll);
3817}
3818
3819
3820/* Timeout function for encoder wait timer. */
3821static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3822{
3823 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3824 hdw->state_encoder_waitok = !0;
3825 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3826 hdw->state_stale = !0;
3827 queue_work(hdw->workqueue,&hdw->workpoll);
3828}
3829
3830
Mike Iselyd913d632008-04-06 04:04:35 -03003831/* Timeout function for encoder run timer. */
3832static void pvr2_hdw_encoder_run_timeout(unsigned long data)
3833{
3834 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3835 if (!hdw->state_encoder_runok) {
3836 hdw->state_encoder_runok = !0;
3837 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
3838 hdw->state_stale = !0;
3839 queue_work(hdw->workqueue,&hdw->workpoll);
3840 }
3841}
3842
3843
Mike Isely681c7392007-11-26 01:48:52 -03003844/* Evaluate whether or not state_decoder_run can change */
3845static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3846{
3847 if (hdw->state_decoder_run) {
3848 if (hdw->state_encoder_ok) {
3849 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03003850 !hdw->state_pipeline_pause &&
3851 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003852 }
3853 if (!hdw->flag_decoder_missed) {
3854 pvr2_decoder_enable(hdw,0);
3855 }
3856 hdw->state_decoder_quiescent = 0;
3857 hdw->state_decoder_run = 0;
3858 /* paranoia - solve race if timer just completed */
3859 del_timer_sync(&hdw->quiescent_timer);
3860 } else {
3861 if (!hdw->state_decoder_quiescent) {
3862 if (!timer_pending(&hdw->quiescent_timer)) {
3863 /* We don't do something about the
3864 quiescent timer until right here because
3865 we also want to catch cases where the
3866 decoder was already not running (like
3867 after initialization) as opposed to
3868 knowing that we had just stopped it.
3869 The second flag check is here to cover a
3870 race - the timer could have run and set
3871 this flag just after the previous check
3872 but before we did the pending check. */
3873 if (!hdw->state_decoder_quiescent) {
3874 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003875 jiffies +
3876 (HZ * TIME_MSEC_DECODER_WAIT
3877 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03003878 add_timer(&hdw->quiescent_timer);
3879 }
3880 }
3881 /* Don't allow decoder to start again until it has
3882 been quiesced first. This little detail should
3883 hopefully further stabilize the encoder. */
3884 return 0;
3885 }
Mike Isely62433e32008-04-22 14:45:40 -03003886 if (!hdw->state_pathway_ok ||
3887 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3888 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03003889 hdw->state_pipeline_pause ||
3890 !hdw->state_pipeline_config ||
3891 !hdw->state_encoder_config ||
3892 !hdw->state_encoder_ok) return 0;
3893 del_timer_sync(&hdw->quiescent_timer);
3894 if (hdw->flag_decoder_missed) return 0;
3895 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3896 hdw->state_decoder_quiescent = 0;
3897 hdw->state_decoder_run = !0;
3898 }
3899 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3900 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3901 return !0;
3902}
3903
3904
3905/* Evaluate whether or not state_usbstream_run can change */
3906static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3907{
3908 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03003909 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03003910 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03003911 fl = (hdw->state_encoder_ok &&
3912 hdw->state_encoder_run);
3913 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3914 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
3915 fl = hdw->state_encoder_ok;
3916 }
3917 if (fl &&
3918 hdw->state_pipeline_req &&
3919 !hdw->state_pipeline_pause &&
3920 hdw->state_pathway_ok) {
3921 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003922 }
3923 pvr2_hdw_cmd_usbstream(hdw,0);
3924 hdw->state_usbstream_run = 0;
3925 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003926 if (!hdw->state_pipeline_req ||
3927 hdw->state_pipeline_pause ||
3928 !hdw->state_pathway_ok) return 0;
3929 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3930 if (!hdw->state_encoder_ok ||
3931 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003932 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3933 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
3934 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03003935 if (hdw->state_encoder_run) return 0;
3936 if (hdw->hdw_desc->digital_control_scheme ==
3937 PVR2_DIGITAL_SCHEME_ONAIR) {
3938 /* OnAir digital receivers won't stream
3939 unless the analog encoder has run first.
3940 Why? I have no idea. But don't even
3941 try until we know the analog side is
3942 known to have run. */
3943 if (!hdw->state_encoder_runok) return 0;
3944 }
Mike Isely62433e32008-04-22 14:45:40 -03003945 }
Mike Isely681c7392007-11-26 01:48:52 -03003946 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3947 hdw->state_usbstream_run = !0;
3948 }
3949 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3950 return !0;
3951}
3952
3953
3954/* Attempt to configure pipeline, if needed */
3955static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3956{
3957 if (hdw->state_pipeline_config ||
3958 hdw->state_pipeline_pause) return 0;
3959 pvr2_hdw_commit_execute(hdw);
3960 return !0;
3961}
3962
3963
3964/* Update pipeline idle and pipeline pause tracking states based on other
3965 inputs. This must be called whenever the other relevant inputs have
3966 changed. */
3967static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3968{
3969 unsigned int st;
3970 int updatedFl = 0;
3971 /* Update pipeline state */
3972 st = !(hdw->state_encoder_run ||
3973 hdw->state_decoder_run ||
3974 hdw->state_usbstream_run ||
3975 (!hdw->state_decoder_quiescent));
3976 if (!st != !hdw->state_pipeline_idle) {
3977 hdw->state_pipeline_idle = st;
3978 updatedFl = !0;
3979 }
3980 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3981 hdw->state_pipeline_pause = 0;
3982 updatedFl = !0;
3983 }
3984 return updatedFl;
3985}
3986
3987
3988typedef int (*state_eval_func)(struct pvr2_hdw *);
3989
3990/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03003991static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03003992 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03003993 state_eval_pipeline_config,
3994 state_eval_encoder_ok,
3995 state_eval_encoder_config,
3996 state_eval_decoder_run,
3997 state_eval_encoder_run,
3998 state_eval_usbstream_run,
3999};
4000
4001
4002/* Process various states and return true if we did anything interesting. */
4003static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4004{
4005 unsigned int i;
4006 int state_updated = 0;
4007 int check_flag;
4008
4009 if (!hdw->state_stale) return 0;
4010 if ((hdw->fw1_state != FW1_STATE_OK) ||
4011 !hdw->flag_ok) {
4012 hdw->state_stale = 0;
4013 return !0;
4014 }
4015 /* This loop is the heart of the entire driver. It keeps trying to
4016 evaluate various bits of driver state until nothing changes for
4017 one full iteration. Each "bit of state" tracks some global
4018 aspect of the driver, e.g. whether decoder should run, if
4019 pipeline is configured, usb streaming is on, etc. We separately
4020 evaluate each of those questions based on other driver state to
4021 arrive at the correct running configuration. */
4022 do {
4023 check_flag = 0;
4024 state_update_pipeline_state(hdw);
4025 /* Iterate over each bit of state */
4026 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4027 if ((*eval_funcs[i])(hdw)) {
4028 check_flag = !0;
4029 state_updated = !0;
4030 state_update_pipeline_state(hdw);
4031 }
4032 }
4033 } while (check_flag && hdw->flag_ok);
4034 hdw->state_stale = 0;
4035 trace_stbit("state_stale",hdw->state_stale);
4036 return state_updated;
4037}
4038
4039
Mike Isely1cb03b72008-04-21 03:47:43 -03004040static unsigned int print_input_mask(unsigned int msk,
4041 char *buf,unsigned int acnt)
4042{
4043 unsigned int idx,ccnt;
4044 unsigned int tcnt = 0;
4045 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4046 if (!((1 << idx) & msk)) continue;
4047 ccnt = scnprintf(buf+tcnt,
4048 acnt-tcnt,
4049 "%s%s",
4050 (tcnt ? ", " : ""),
4051 control_values_input[idx]);
4052 tcnt += ccnt;
4053 }
4054 return tcnt;
4055}
4056
4057
Mike Isely62433e32008-04-22 14:45:40 -03004058static const char *pvr2_pathway_state_name(int id)
4059{
4060 switch (id) {
4061 case PVR2_PATHWAY_ANALOG: return "analog";
4062 case PVR2_PATHWAY_DIGITAL: return "digital";
4063 default: return "unknown";
4064 }
4065}
4066
4067
Mike Isely681c7392007-11-26 01:48:52 -03004068static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4069 char *buf,unsigned int acnt)
4070{
4071 switch (which) {
4072 case 0:
4073 return scnprintf(
4074 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004075 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004076 (hdw->flag_ok ? " <ok>" : " <fail>"),
4077 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4078 (hdw->flag_disconnected ? " <disconnected>" :
4079 " <connected>"),
4080 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004081 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4082 pvr2_pathway_state_name(hdw->pathway_state));
4083
Mike Isely681c7392007-11-26 01:48:52 -03004084 case 1:
4085 return scnprintf(
4086 buf,acnt,
4087 "pipeline:%s%s%s%s",
4088 (hdw->state_pipeline_idle ? " <idle>" : ""),
4089 (hdw->state_pipeline_config ?
4090 " <configok>" : " <stale>"),
4091 (hdw->state_pipeline_req ? " <req>" : ""),
4092 (hdw->state_pipeline_pause ? " <pause>" : ""));
4093 case 2:
4094 return scnprintf(
4095 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004096 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004097 (hdw->state_decoder_run ?
4098 " <decode:run>" :
4099 (hdw->state_decoder_quiescent ?
4100 "" : " <decode:stop>")),
4101 (hdw->state_decoder_quiescent ?
4102 " <decode:quiescent>" : ""),
4103 (hdw->state_encoder_ok ?
4104 "" : " <encode:init>"),
4105 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004106 (hdw->state_encoder_runok ?
4107 " <encode:run>" :
4108 " <encode:firstrun>") :
4109 (hdw->state_encoder_runok ?
4110 " <encode:stop>" :
4111 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004112 (hdw->state_encoder_config ?
4113 " <encode:configok>" :
4114 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004115 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004116 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004117 " <usb:run>" : " <usb:stop>"),
4118 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004119 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004120 case 3:
4121 return scnprintf(
4122 buf,acnt,
4123 "state: %s",
4124 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004125 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004126 unsigned int tcnt = 0;
4127 unsigned int ccnt;
4128
4129 ccnt = scnprintf(buf,
4130 acnt,
4131 "Hardware supported inputs: ");
4132 tcnt += ccnt;
4133 tcnt += print_input_mask(hdw->input_avail_mask,
4134 buf+tcnt,
4135 acnt-tcnt);
4136 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4137 ccnt = scnprintf(buf+tcnt,
4138 acnt-tcnt,
4139 "; allowed inputs: ");
4140 tcnt += ccnt;
4141 tcnt += print_input_mask(hdw->input_allowed_mask,
4142 buf+tcnt,
4143 acnt-tcnt);
4144 }
4145 return tcnt;
4146 }
4147 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004148 struct pvr2_stream_stats stats;
4149 if (!hdw->vid_stream) break;
4150 pvr2_stream_get_stats(hdw->vid_stream,
4151 &stats,
4152 0);
4153 return scnprintf(
4154 buf,acnt,
4155 "Bytes streamed=%u"
4156 " URBs: queued=%u idle=%u ready=%u"
4157 " processed=%u failed=%u",
4158 stats.bytes_processed,
4159 stats.buffers_in_queue,
4160 stats.buffers_in_idle,
4161 stats.buffers_in_ready,
4162 stats.buffers_processed,
4163 stats.buffers_failed);
4164 }
Mike Isely681c7392007-11-26 01:48:52 -03004165 default: break;
4166 }
4167 return 0;
4168}
4169
4170
4171unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4172 char *buf,unsigned int acnt)
4173{
4174 unsigned int bcnt,ccnt,idx;
4175 bcnt = 0;
4176 LOCK_TAKE(hdw->big_lock);
4177 for (idx = 0; ; idx++) {
4178 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4179 if (!ccnt) break;
4180 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4181 if (!acnt) break;
4182 buf[0] = '\n'; ccnt = 1;
4183 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4184 }
4185 LOCK_GIVE(hdw->big_lock);
4186 return bcnt;
4187}
4188
4189
4190static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4191{
4192 char buf[128];
4193 unsigned int idx,ccnt;
4194
4195 for (idx = 0; ; idx++) {
4196 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4197 if (!ccnt) break;
4198 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4199 }
4200}
4201
4202
4203/* Evaluate and update the driver's current state, taking various actions
4204 as appropriate for the update. */
4205static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4206{
4207 unsigned int st;
4208 int state_updated = 0;
4209 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004210 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004211
4212 pvr2_trace(PVR2_TRACE_STBITS,
4213 "Drive state check START");
4214 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4215 pvr2_hdw_state_log_state(hdw);
4216 }
4217
4218 /* Process all state and get back over disposition */
4219 state_updated = pvr2_hdw_state_update(hdw);
4220
Mike Isely1b9c18c2008-04-22 14:45:41 -03004221 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4222
Mike Isely681c7392007-11-26 01:48:52 -03004223 /* Update master state based upon all other states. */
4224 if (!hdw->flag_ok) {
4225 st = PVR2_STATE_DEAD;
4226 } else if (hdw->fw1_state != FW1_STATE_OK) {
4227 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004228 } else if ((analog_mode ||
4229 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4230 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004231 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004232 } else if (hdw->flag_tripped ||
4233 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004234 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004235 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004236 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004237 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004238 st = PVR2_STATE_RUN;
4239 } else {
4240 st = PVR2_STATE_READY;
4241 }
4242 if (hdw->master_state != st) {
4243 pvr2_trace(PVR2_TRACE_STATE,
4244 "Device state change from %s to %s",
4245 pvr2_get_state_name(hdw->master_state),
4246 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004247 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004248 hdw->master_state = st;
4249 state_updated = !0;
4250 callback_flag = !0;
4251 }
4252 if (state_updated) {
4253 /* Trigger anyone waiting on any state changes here. */
4254 wake_up(&hdw->state_wait_data);
4255 }
4256
4257 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4258 pvr2_hdw_state_log_state(hdw);
4259 }
4260 pvr2_trace(PVR2_TRACE_STBITS,
4261 "Drive state check DONE callback=%d",callback_flag);
4262
4263 return callback_flag;
4264}
4265
4266
4267/* Cause kernel thread to check / update driver state */
4268static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4269{
4270 if (hdw->state_stale) return;
4271 hdw->state_stale = !0;
4272 trace_stbit("state_stale",hdw->state_stale);
4273 queue_work(hdw->workqueue,&hdw->workpoll);
4274}
4275
4276
Mike Iselyd8554972006-06-26 20:58:46 -03004277int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4278{
4279 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4280}
4281
4282
4283int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4284{
4285 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4286}
4287
4288
4289int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4290{
4291 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4292}
4293
4294
4295int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4296{
4297 u32 cval,nval;
4298 int ret;
4299 if (~msk) {
4300 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4301 if (ret) return ret;
4302 nval = (cval & ~msk) | (val & msk);
4303 pvr2_trace(PVR2_TRACE_GPIO,
4304 "GPIO direction changing 0x%x:0x%x"
4305 " from 0x%x to 0x%x",
4306 msk,val,cval,nval);
4307 } else {
4308 nval = val;
4309 pvr2_trace(PVR2_TRACE_GPIO,
4310 "GPIO direction changing to 0x%x",nval);
4311 }
4312 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4313}
4314
4315
4316int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4317{
4318 u32 cval,nval;
4319 int ret;
4320 if (~msk) {
4321 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4322 if (ret) return ret;
4323 nval = (cval & ~msk) | (val & msk);
4324 pvr2_trace(PVR2_TRACE_GPIO,
4325 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4326 msk,val,cval,nval);
4327 } else {
4328 nval = val;
4329 pvr2_trace(PVR2_TRACE_GPIO,
4330 "GPIO output changing to 0x%x",nval);
4331 }
4332 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4333}
4334
4335
Mike Isely7fb20fa2008-04-22 14:45:37 -03004336unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
4337{
4338 return hdw->input_avail_mask;
4339}
4340
4341
Mike Isely1cb03b72008-04-21 03:47:43 -03004342unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
4343{
4344 return hdw->input_allowed_mask;
4345}
4346
4347
4348static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
4349{
4350 if (hdw->input_val != v) {
4351 hdw->input_val = v;
4352 hdw->input_dirty = !0;
4353 }
4354
4355 /* Handle side effects - if we switch to a mode that needs the RF
4356 tuner, then select the right frequency choice as well and mark
4357 it dirty. */
4358 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
4359 hdw->freqSelector = 0;
4360 hdw->freqDirty = !0;
4361 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
4362 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
4363 hdw->freqSelector = 1;
4364 hdw->freqDirty = !0;
4365 }
4366 return 0;
4367}
4368
4369
4370int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
4371 unsigned int change_mask,
4372 unsigned int change_val)
4373{
4374 int ret = 0;
4375 unsigned int nv,m,idx;
4376 LOCK_TAKE(hdw->big_lock);
4377 do {
4378 nv = hdw->input_allowed_mask & ~change_mask;
4379 nv |= (change_val & change_mask);
4380 nv &= hdw->input_avail_mask;
4381 if (!nv) {
4382 /* No legal modes left; return error instead. */
4383 ret = -EPERM;
4384 break;
4385 }
4386 hdw->input_allowed_mask = nv;
4387 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
4388 /* Current mode is still in the allowed mask, so
4389 we're done. */
4390 break;
4391 }
4392 /* Select and switch to a mode that is still in the allowed
4393 mask */
4394 if (!hdw->input_allowed_mask) {
4395 /* Nothing legal; give up */
4396 break;
4397 }
4398 m = hdw->input_allowed_mask;
4399 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
4400 if (!((1 << idx) & m)) continue;
4401 pvr2_hdw_set_input(hdw,idx);
4402 break;
4403 }
4404 } while (0);
4405 LOCK_GIVE(hdw->big_lock);
4406 return ret;
4407}
4408
4409
Mike Iselye61b6fc2006-07-18 22:42:18 -03004410/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004411static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004412{
4413 int result;
4414 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03004415 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03004416 result = pvr2_send_request(hdw,
4417 hdw->cmd_buffer,1,
4418 hdw->cmd_buffer,1);
4419 if (result < 0) break;
4420 result = hdw->cmd_buffer[0];
4421 } while(0); LOCK_GIVE(hdw->ctl_lock);
4422 return result;
4423}
4424
4425
Mike Isely32ffa9a2006-09-23 22:26:52 -03004426int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004427 u32 match_type, u32 match_chip, u64 reg_id,
4428 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004429{
4430#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004431 struct pvr2_i2c_client *cp;
4432 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004433 int stat = 0;
4434 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004435
Mike Isely201f5c92007-01-28 16:08:36 -03004436 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4437
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004438 req.match_type = match_type;
4439 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004440 req.reg = reg_id;
4441 if (setFl) req.val = *val_ptr;
4442 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004443 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004444 if (!v4l2_chip_match_i2c_client(
4445 cp->client,
4446 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004447 continue;
4448 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004449 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004450 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4451 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004452 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004453 okFl = !0;
4454 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004455 }
4456 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004457 if (okFl) {
4458 return stat;
4459 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004460 return -EINVAL;
4461#else
4462 return -ENOSYS;
4463#endif
4464}
4465
4466
Mike Iselyd8554972006-06-26 20:58:46 -03004467/*
4468 Stuff for Emacs to see, in order to encourage consistent editing style:
4469 *** Local Variables: ***
4470 *** mode: c ***
4471 *** fill-column: 75 ***
4472 *** tab-width: 8 ***
4473 *** c-basic-offset: 8 ***
4474 *** End: ***
4475 */