blob: 9bb59b2de91769460765b776bd3929c17d0d0510 [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 Isely432907f2008-08-31 21:02:20 -0300301static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300302
Mike Isely681c7392007-11-26 01:48:52 -0300303
304static void trace_stbit(const char *name,int val)
305{
306 pvr2_trace(PVR2_TRACE_STBITS,
307 "State bit %s <-- %s",
308 name,(val ? "true" : "false"));
309}
310
Mike Iselyd8554972006-06-26 20:58:46 -0300311static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
312{
313 struct pvr2_hdw *hdw = cptr->hdw;
314 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
315 *vp = hdw->freqTable[hdw->freqProgSlot-1];
316 } else {
317 *vp = 0;
318 }
319 return 0;
320}
321
322static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
323{
324 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300325 unsigned int slotId = hdw->freqProgSlot;
326 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
327 hdw->freqTable[slotId-1] = v;
328 /* Handle side effects correctly - if we're tuned to this
329 slot, then forgot the slot id relation since the stored
330 frequency has been changed. */
331 if (hdw->freqSelector) {
332 if (hdw->freqSlotRadio == slotId) {
333 hdw->freqSlotRadio = 0;
334 }
335 } else {
336 if (hdw->freqSlotTelevision == slotId) {
337 hdw->freqSlotTelevision = 0;
338 }
339 }
Mike Iselyd8554972006-06-26 20:58:46 -0300340 }
341 return 0;
342}
343
344static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
345{
346 *vp = cptr->hdw->freqProgSlot;
347 return 0;
348}
349
350static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
351{
352 struct pvr2_hdw *hdw = cptr->hdw;
353 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
354 hdw->freqProgSlot = v;
355 }
356 return 0;
357}
358
359static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
360{
Mike Isely1bde0282006-12-27 23:30:13 -0300361 struct pvr2_hdw *hdw = cptr->hdw;
362 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300363 return 0;
364}
365
Mike Isely1bde0282006-12-27 23:30:13 -0300366static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300367{
368 unsigned freq = 0;
369 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300370 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
371 if (slotId > 0) {
372 freq = hdw->freqTable[slotId-1];
373 if (!freq) return 0;
374 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300375 }
Mike Isely1bde0282006-12-27 23:30:13 -0300376 if (hdw->freqSelector) {
377 hdw->freqSlotRadio = slotId;
378 } else {
379 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300380 }
381 return 0;
382}
383
384static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
385{
Mike Isely1bde0282006-12-27 23:30:13 -0300386 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300387 return 0;
388}
389
390static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
391{
392 return cptr->hdw->freqDirty != 0;
393}
394
395static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
396{
397 cptr->hdw->freqDirty = 0;
398}
399
400static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
401{
Mike Isely1bde0282006-12-27 23:30:13 -0300402 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300403 return 0;
404}
405
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300406static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
407{
Mike Isely432907f2008-08-31 21:02:20 -0300408 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
409 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
410 if (stat != 0) {
411 /* Keep checkpatch.pl quiet */
412 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300413 }
Mike Isely432907f2008-08-31 21:02:20 -0300414 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300415 return 0;
416}
417
418static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
419{
Mike Isely432907f2008-08-31 21:02:20 -0300420 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
421 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
422 if (stat != 0) {
423 /* Keep checkpatch.pl quiet */
424 return stat;
425 }
426 *left = cap->bounds.left;
427 if (cap->bounds.width > cptr->hdw->cropw_val) {
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300428 /* This statement is present purely to shut up
429 checkpatch.pl */
Mike Isely432907f2008-08-31 21:02:20 -0300430 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300431 }
432 return 0;
433}
434
435static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
436{
Mike Isely432907f2008-08-31 21:02:20 -0300437 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
438 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
439 if (stat != 0) {
440 /* Keep checkpatch.pl quiet */
441 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300442 }
Mike Isely432907f2008-08-31 21:02:20 -0300443 *top = cap->bounds.top;
444 return 0;
445}
446
447static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
448{
449 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
450 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
451 if (stat != 0) {
452 /* Keep checkpatch.pl quiet */
453 return stat;
454 }
455 *top = cap->bounds.top;
456 if (cap->bounds.height > cptr->hdw->croph_val) {
457 /* Keep checkpatch.pl quiet */
458 *top += cap->bounds.height - cptr->hdw->croph_val;
459 }
460 return 0;
461}
462
463static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *val)
464{
465 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
466 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
467 if (stat != 0) {
468 /* Keep checkpatch.pl quiet */
469 return stat;
470 }
471 *val = 0;
472 if (cap->bounds.width > cptr->hdw->cropl_val) {
473 /* Keep checkpatch.pl quiet */
474 *val = cap->bounds.width - cptr->hdw->cropl_val;
475 }
476 return 0;
477}
478
479static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *val)
480{
481 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
482 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
483 if (stat != 0) {
484 /* Keep checkpatch.pl quiet */
485 return stat;
486 }
487 *val = 0;
488 if (cap->bounds.height > cptr->hdw->cropt_val) {
489 /* Keep checkpatch.pl quiet */
490 *val = cap->bounds.height - cptr->hdw->cropt_val;
491 }
492 return 0;
493}
494
495static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
496{
497 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
498 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
499 if (stat != 0) {
500 /* Keep checkpatch.pl quiet */
501 return stat;
502 }
503 *val = cap->bounds.left;
504 return 0;
505}
506
507static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
508{
509 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
510 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
511 if (stat != 0) {
512 /* Keep checkpatch.pl quiet */
513 return stat;
514 }
515 *val = cap->bounds.top;
516 return 0;
517}
518
519static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
520{
521 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
522 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
523 if (stat != 0) {
524 /* Keep checkpatch.pl quiet */
525 return stat;
526 }
527 *val = cap->bounds.width;
528 return 0;
529}
530
531static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
532{
533 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
534 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
535 if (stat != 0) {
536 /* Keep checkpatch.pl quiet */
537 return stat;
538 }
539 *val = cap->bounds.height;
540 return 0;
541}
542
543static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
544{
545 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
546 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
547 if (stat != 0) {
548 /* Keep checkpatch.pl quiet */
549 return stat;
550 }
551 *val = cap->defrect.left;
552 return 0;
553}
554
555static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
556{
557 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
558 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
559 if (stat != 0) {
560 /* Keep checkpatch.pl quiet */
561 return stat;
562 }
563 *val = cap->defrect.top;
564 return 0;
565}
566
567static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
568{
569 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
570 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
571 if (stat != 0) {
572 /* Keep checkpatch.pl quiet */
573 return stat;
574 }
575 *val = cap->defrect.width;
576 return 0;
577}
578
579static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
580{
581 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
582 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
583 if (stat != 0) {
584 /* Keep checkpatch.pl quiet */
585 return stat;
586 }
587 *val = cap->defrect.height;
588 return 0;
589}
590
591static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
592{
593 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
594 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
595 if (stat != 0) {
596 /* Keep checkpatch.pl quiet */
597 return stat;
598 }
599 *val = cap->pixelaspect.numerator;
600 return 0;
601}
602
603static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
604{
605 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
606 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
607 if (stat != 0) {
608 /* Keep checkpatch.pl quiet */
609 return stat;
610 }
611 *val = cap->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300612 return 0;
613}
614
Mike Isely3ad9fc32006-09-02 22:37:52 -0300615static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
616{
617 /* Actual maximum depends on the video standard in effect. */
618 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
619 *vp = 480;
620 } else {
621 *vp = 576;
622 }
623 return 0;
624}
625
626static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
627{
Mike Isely989eb152007-11-26 01:53:12 -0300628 /* Actual minimum depends on device digitizer type. */
629 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300630 *vp = 75;
631 } else {
632 *vp = 17;
633 }
634 return 0;
635}
636
Mike Isely1bde0282006-12-27 23:30:13 -0300637static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
638{
639 *vp = cptr->hdw->input_val;
640 return 0;
641}
642
Mike Isely29bf5b12008-04-22 14:45:37 -0300643static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
644{
Mike Isely1cb03b72008-04-21 03:47:43 -0300645 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300646}
647
Mike Isely1bde0282006-12-27 23:30:13 -0300648static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
649{
Mike Isely1cb03b72008-04-21 03:47:43 -0300650 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300651}
652
653static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
654{
655 return cptr->hdw->input_dirty != 0;
656}
657
658static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
659{
660 cptr->hdw->input_dirty = 0;
661}
662
Mike Isely5549f542006-12-27 23:28:54 -0300663
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300664static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
665{
Mike Isely644afdb2007-01-20 00:19:23 -0300666 unsigned long fv;
667 struct pvr2_hdw *hdw = cptr->hdw;
668 if (hdw->tuner_signal_stale) {
669 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300670 }
Mike Isely644afdb2007-01-20 00:19:23 -0300671 fv = hdw->tuner_signal_info.rangehigh;
672 if (!fv) {
673 /* Safety fallback */
674 *vp = TV_MAX_FREQ;
675 return 0;
676 }
677 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
678 fv = (fv * 125) / 2;
679 } else {
680 fv = fv * 62500;
681 }
682 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300683 return 0;
684}
685
686static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
687{
Mike Isely644afdb2007-01-20 00:19:23 -0300688 unsigned long fv;
689 struct pvr2_hdw *hdw = cptr->hdw;
690 if (hdw->tuner_signal_stale) {
691 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300692 }
Mike Isely644afdb2007-01-20 00:19:23 -0300693 fv = hdw->tuner_signal_info.rangelow;
694 if (!fv) {
695 /* Safety fallback */
696 *vp = TV_MIN_FREQ;
697 return 0;
698 }
699 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
700 fv = (fv * 125) / 2;
701 } else {
702 fv = fv * 62500;
703 }
704 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300705 return 0;
706}
707
Mike Iselyb30d2442006-06-25 20:05:01 -0300708static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
709{
710 return cptr->hdw->enc_stale != 0;
711}
712
713static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
714{
715 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300716 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300717}
718
719static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
720{
721 int ret;
722 struct v4l2_ext_controls cs;
723 struct v4l2_ext_control c1;
724 memset(&cs,0,sizeof(cs));
725 memset(&c1,0,sizeof(c1));
726 cs.controls = &c1;
727 cs.count = 1;
728 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300729 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300730 VIDIOC_G_EXT_CTRLS);
731 if (ret) return ret;
732 *vp = c1.value;
733 return 0;
734}
735
736static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
737{
738 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300739 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300740 struct v4l2_ext_controls cs;
741 struct v4l2_ext_control c1;
742 memset(&cs,0,sizeof(cs));
743 memset(&c1,0,sizeof(c1));
744 cs.controls = &c1;
745 cs.count = 1;
746 c1.id = cptr->info->v4l_id;
747 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300748 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
749 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300750 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300751 if (ret == -EBUSY) {
752 /* Oops. cx2341x is telling us it's not safe to change
753 this control while we're capturing. Make a note of this
754 fact so that the pipeline will be stopped the next time
755 controls are committed. Then go on ahead and store this
756 change anyway. */
757 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
758 0, &cs,
759 VIDIOC_S_EXT_CTRLS);
760 if (!ret) hdw->enc_unsafe_stale = !0;
761 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300762 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300763 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300764 return 0;
765}
766
767static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
768{
769 struct v4l2_queryctrl qctrl;
770 struct pvr2_ctl_info *info;
771 qctrl.id = cptr->info->v4l_id;
772 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
773 /* Strip out the const so we can adjust a function pointer. It's
774 OK to do this here because we know this is a dynamically created
775 control, so the underlying storage for the info pointer is (a)
776 private to us, and (b) not in read-only storage. Either we do
777 this or we significantly complicate the underlying control
778 implementation. */
779 info = (struct pvr2_ctl_info *)(cptr->info);
780 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
781 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300782 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300783 }
784 } else {
785 if (!(info->set_value)) {
786 info->set_value = ctrl_cx2341x_set;
787 }
788 }
789 return qctrl.flags;
790}
791
Mike Iselyd8554972006-06-26 20:58:46 -0300792static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
793{
Mike Isely681c7392007-11-26 01:48:52 -0300794 *vp = cptr->hdw->state_pipeline_req;
795 return 0;
796}
797
798static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
799{
800 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300801 return 0;
802}
803
804static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
805{
806 int result = pvr2_hdw_is_hsm(cptr->hdw);
807 *vp = PVR2_CVAL_HSM_FULL;
808 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
809 if (result) *vp = PVR2_CVAL_HSM_HIGH;
810 return 0;
811}
812
813static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
814{
815 *vp = cptr->hdw->std_mask_avail;
816 return 0;
817}
818
819static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
820{
821 struct pvr2_hdw *hdw = cptr->hdw;
822 v4l2_std_id ns;
823 ns = hdw->std_mask_avail;
824 ns = (ns & ~m) | (v & m);
825 if (ns == hdw->std_mask_avail) return 0;
826 hdw->std_mask_avail = ns;
827 pvr2_hdw_internal_set_std_avail(hdw);
828 pvr2_hdw_internal_find_stdenum(hdw);
829 return 0;
830}
831
832static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
833 char *bufPtr,unsigned int bufSize,
834 unsigned int *len)
835{
836 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
837 return 0;
838}
839
840static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
841 const char *bufPtr,unsigned int bufSize,
842 int *mskp,int *valp)
843{
844 int ret;
845 v4l2_std_id id;
846 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
847 if (ret < 0) return ret;
848 if (mskp) *mskp = id;
849 if (valp) *valp = id;
850 return 0;
851}
852
853static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
854{
855 *vp = cptr->hdw->std_mask_cur;
856 return 0;
857}
858
859static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
860{
861 struct pvr2_hdw *hdw = cptr->hdw;
862 v4l2_std_id ns;
863 ns = hdw->std_mask_cur;
864 ns = (ns & ~m) | (v & m);
865 if (ns == hdw->std_mask_cur) return 0;
866 hdw->std_mask_cur = ns;
867 hdw->std_dirty = !0;
868 pvr2_hdw_internal_find_stdenum(hdw);
869 return 0;
870}
871
872static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
873{
874 return cptr->hdw->std_dirty != 0;
875}
876
877static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
878{
879 cptr->hdw->std_dirty = 0;
880}
881
882static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
883{
Mike Isely18103c572007-01-20 00:09:47 -0300884 struct pvr2_hdw *hdw = cptr->hdw;
885 pvr2_i2c_core_status_poll(hdw);
886 *vp = hdw->tuner_signal_info.signal;
887 return 0;
888}
889
890static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
891{
892 int val = 0;
893 unsigned int subchan;
894 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300895 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300896 subchan = hdw->tuner_signal_info.rxsubchans;
897 if (subchan & V4L2_TUNER_SUB_MONO) {
898 val |= (1 << V4L2_TUNER_MODE_MONO);
899 }
900 if (subchan & V4L2_TUNER_SUB_STEREO) {
901 val |= (1 << V4L2_TUNER_MODE_STEREO);
902 }
903 if (subchan & V4L2_TUNER_SUB_LANG1) {
904 val |= (1 << V4L2_TUNER_MODE_LANG1);
905 }
906 if (subchan & V4L2_TUNER_SUB_LANG2) {
907 val |= (1 << V4L2_TUNER_MODE_LANG2);
908 }
909 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300910 return 0;
911}
912
Mike Iselyd8554972006-06-26 20:58:46 -0300913
914static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
915{
916 struct pvr2_hdw *hdw = cptr->hdw;
917 if (v < 0) return -EINVAL;
918 if (v > hdw->std_enum_cnt) return -EINVAL;
919 hdw->std_enum_cur = v;
920 if (!v) return 0;
921 v--;
922 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
923 hdw->std_mask_cur = hdw->std_defs[v].id;
924 hdw->std_dirty = !0;
925 return 0;
926}
927
928
929static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
930{
931 *vp = cptr->hdw->std_enum_cur;
932 return 0;
933}
934
935
936static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
937{
938 return cptr->hdw->std_dirty != 0;
939}
940
941
942static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
943{
944 cptr->hdw->std_dirty = 0;
945}
946
947
948#define DEFINT(vmin,vmax) \
949 .type = pvr2_ctl_int, \
950 .def.type_int.min_value = vmin, \
951 .def.type_int.max_value = vmax
952
953#define DEFENUM(tab) \
954 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300955 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300956 .def.type_enum.value_names = tab
957
Mike Isely33213962006-06-25 20:04:40 -0300958#define DEFBOOL \
959 .type = pvr2_ctl_bool
960
Mike Iselyd8554972006-06-26 20:58:46 -0300961#define DEFMASK(msk,tab) \
962 .type = pvr2_ctl_bitmask, \
963 .def.type_bitmask.valid_bits = msk, \
964 .def.type_bitmask.bit_names = tab
965
966#define DEFREF(vname) \
967 .set_value = ctrl_set_##vname, \
968 .get_value = ctrl_get_##vname, \
969 .is_dirty = ctrl_isdirty_##vname, \
970 .clear_dirty = ctrl_cleardirty_##vname
971
972
973#define VCREATE_FUNCS(vname) \
974static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
975{*vp = cptr->hdw->vname##_val; return 0;} \
976static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
977{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
978static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
979{return cptr->hdw->vname##_dirty != 0;} \
980static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
981{cptr->hdw->vname##_dirty = 0;}
982
983VCREATE_FUNCS(brightness)
984VCREATE_FUNCS(contrast)
985VCREATE_FUNCS(saturation)
986VCREATE_FUNCS(hue)
987VCREATE_FUNCS(volume)
988VCREATE_FUNCS(balance)
989VCREATE_FUNCS(bass)
990VCREATE_FUNCS(treble)
991VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300992VCREATE_FUNCS(cropl)
993VCREATE_FUNCS(cropt)
994VCREATE_FUNCS(cropw)
995VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -0300996VCREATE_FUNCS(audiomode)
997VCREATE_FUNCS(res_hor)
998VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300999VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -03001000
Mike Iselyd8554972006-06-26 20:58:46 -03001001/* Table definition of all controls which can be manipulated */
1002static const struct pvr2_ctl_info control_defs[] = {
1003 {
1004 .v4l_id = V4L2_CID_BRIGHTNESS,
1005 .desc = "Brightness",
1006 .name = "brightness",
1007 .default_value = 128,
1008 DEFREF(brightness),
1009 DEFINT(0,255),
1010 },{
1011 .v4l_id = V4L2_CID_CONTRAST,
1012 .desc = "Contrast",
1013 .name = "contrast",
1014 .default_value = 68,
1015 DEFREF(contrast),
1016 DEFINT(0,127),
1017 },{
1018 .v4l_id = V4L2_CID_SATURATION,
1019 .desc = "Saturation",
1020 .name = "saturation",
1021 .default_value = 64,
1022 DEFREF(saturation),
1023 DEFINT(0,127),
1024 },{
1025 .v4l_id = V4L2_CID_HUE,
1026 .desc = "Hue",
1027 .name = "hue",
1028 .default_value = 0,
1029 DEFREF(hue),
1030 DEFINT(-128,127),
1031 },{
1032 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1033 .desc = "Volume",
1034 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001035 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001036 DEFREF(volume),
1037 DEFINT(0,65535),
1038 },{
1039 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1040 .desc = "Balance",
1041 .name = "balance",
1042 .default_value = 0,
1043 DEFREF(balance),
1044 DEFINT(-32768,32767),
1045 },{
1046 .v4l_id = V4L2_CID_AUDIO_BASS,
1047 .desc = "Bass",
1048 .name = "bass",
1049 .default_value = 0,
1050 DEFREF(bass),
1051 DEFINT(-32768,32767),
1052 },{
1053 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1054 .desc = "Treble",
1055 .name = "treble",
1056 .default_value = 0,
1057 DEFREF(treble),
1058 DEFINT(-32768,32767),
1059 },{
1060 .v4l_id = V4L2_CID_AUDIO_MUTE,
1061 .desc = "Mute",
1062 .name = "mute",
1063 .default_value = 0,
1064 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001065 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001066 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001067 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001068 .name = "crop_left",
1069 .internal_id = PVR2_CID_CROPL,
1070 .default_value = 0,
1071 DEFREF(cropl),
1072 DEFINT(-129, 340),
1073 .get_min_value = ctrl_cropl_min_get,
1074 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001075 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001076 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001077 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001078 .name = "crop_top",
1079 .internal_id = PVR2_CID_CROPT,
1080 .default_value = 0,
1081 DEFREF(cropt),
1082 DEFINT(-35, 544),
1083 .get_min_value = ctrl_cropt_min_get,
1084 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001085 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001086 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001087 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001088 .name = "crop_width",
1089 .internal_id = PVR2_CID_CROPW,
1090 .default_value = 720,
1091 DEFREF(cropw),
Mike Isely432907f2008-08-31 21:02:20 -03001092 .get_max_value = ctrl_cropw_max_get,
1093 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001094 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001095 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001096 .name = "crop_height",
1097 .internal_id = PVR2_CID_CROPH,
1098 .default_value = 480,
1099 DEFREF(croph),
Mike Isely432907f2008-08-31 21:02:20 -03001100 .get_max_value = ctrl_croph_max_get,
1101 .get_def_value = ctrl_get_cropcapdh,
1102 }, {
1103 .desc = "Capture capability pixel aspect numerator",
1104 .name = "cropcap_pixel_numerator",
1105 .internal_id = PVR2_CID_CROPCAPPAN,
1106 .get_value = ctrl_get_cropcappan,
1107 }, {
1108 .desc = "Capture capability pixel aspect denominator",
1109 .name = "cropcap_pixel_denominator",
1110 .internal_id = PVR2_CID_CROPCAPPAD,
1111 .get_value = ctrl_get_cropcappad,
1112 }, {
1113 .desc = "Capture capability bounds top",
1114 .name = "cropcap_bounds_top",
1115 .internal_id = PVR2_CID_CROPCAPBT,
1116 .get_value = ctrl_get_cropcapbt,
1117 }, {
1118 .desc = "Capture capability bounds left",
1119 .name = "cropcap_bounds_left",
1120 .internal_id = PVR2_CID_CROPCAPBL,
1121 .get_value = ctrl_get_cropcapbl,
1122 }, {
1123 .desc = "Capture capability bounds width",
1124 .name = "cropcap_bounds_width",
1125 .internal_id = PVR2_CID_CROPCAPBW,
1126 .get_value = ctrl_get_cropcapbw,
1127 }, {
1128 .desc = "Capture capability bounds height",
1129 .name = "cropcap_bounds_height",
1130 .internal_id = PVR2_CID_CROPCAPBH,
1131 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001132 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001133 .desc = "Video Source",
1134 .name = "input",
1135 .internal_id = PVR2_CID_INPUT,
1136 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001137 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001138 DEFREF(input),
1139 DEFENUM(control_values_input),
1140 },{
1141 .desc = "Audio Mode",
1142 .name = "audio_mode",
1143 .internal_id = PVR2_CID_AUDIOMODE,
1144 .default_value = V4L2_TUNER_MODE_STEREO,
1145 DEFREF(audiomode),
1146 DEFENUM(control_values_audiomode),
1147 },{
1148 .desc = "Horizontal capture resolution",
1149 .name = "resolution_hor",
1150 .internal_id = PVR2_CID_HRES,
1151 .default_value = 720,
1152 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001153 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001154 },{
1155 .desc = "Vertical capture resolution",
1156 .name = "resolution_ver",
1157 .internal_id = PVR2_CID_VRES,
1158 .default_value = 480,
1159 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001160 DEFINT(17,576),
1161 /* Hook in check for video standard and adjust maximum
1162 depending on the standard. */
1163 .get_max_value = ctrl_vres_max_get,
1164 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001165 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001166 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001167 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1168 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001169 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001170 DEFREF(srate),
1171 DEFENUM(control_values_srate),
1172 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001173 .desc = "Tuner Frequency (Hz)",
1174 .name = "frequency",
1175 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001176 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001177 .set_value = ctrl_freq_set,
1178 .get_value = ctrl_freq_get,
1179 .is_dirty = ctrl_freq_is_dirty,
1180 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001181 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001182 /* Hook in check for input value (tv/radio) and adjust
1183 max/min values accordingly */
1184 .get_max_value = ctrl_freq_max_get,
1185 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001186 },{
1187 .desc = "Channel",
1188 .name = "channel",
1189 .set_value = ctrl_channel_set,
1190 .get_value = ctrl_channel_get,
1191 DEFINT(0,FREQTABLE_SIZE),
1192 },{
1193 .desc = "Channel Program Frequency",
1194 .name = "freq_table_value",
1195 .set_value = ctrl_channelfreq_set,
1196 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001197 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001198 /* Hook in check for input value (tv/radio) and adjust
1199 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001200 .get_max_value = ctrl_freq_max_get,
1201 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001202 },{
1203 .desc = "Channel Program ID",
1204 .name = "freq_table_channel",
1205 .set_value = ctrl_channelprog_set,
1206 .get_value = ctrl_channelprog_get,
1207 DEFINT(0,FREQTABLE_SIZE),
1208 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001209 .desc = "Streaming Enabled",
1210 .name = "streaming_enabled",
1211 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001212 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001213 },{
1214 .desc = "USB Speed",
1215 .name = "usb_speed",
1216 .get_value = ctrl_hsm_get,
1217 DEFENUM(control_values_hsm),
1218 },{
Mike Isely681c7392007-11-26 01:48:52 -03001219 .desc = "Master State",
1220 .name = "master_state",
1221 .get_value = ctrl_masterstate_get,
1222 DEFENUM(pvr2_state_names),
1223 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001224 .desc = "Signal Present",
1225 .name = "signal_present",
1226 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001227 DEFINT(0,65535),
1228 },{
1229 .desc = "Audio Modes Present",
1230 .name = "audio_modes_present",
1231 .get_value = ctrl_audio_modes_present_get,
1232 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1233 v4l. Nothing outside of this module cares about this,
1234 but I reuse it in order to also reuse the
1235 control_values_audiomode string table. */
1236 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1237 (1 << V4L2_TUNER_MODE_STEREO)|
1238 (1 << V4L2_TUNER_MODE_LANG1)|
1239 (1 << V4L2_TUNER_MODE_LANG2)),
1240 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001241 },{
1242 .desc = "Video Standards Available Mask",
1243 .name = "video_standard_mask_available",
1244 .internal_id = PVR2_CID_STDAVAIL,
1245 .skip_init = !0,
1246 .get_value = ctrl_stdavail_get,
1247 .set_value = ctrl_stdavail_set,
1248 .val_to_sym = ctrl_std_val_to_sym,
1249 .sym_to_val = ctrl_std_sym_to_val,
1250 .type = pvr2_ctl_bitmask,
1251 },{
1252 .desc = "Video Standards In Use Mask",
1253 .name = "video_standard_mask_active",
1254 .internal_id = PVR2_CID_STDCUR,
1255 .skip_init = !0,
1256 .get_value = ctrl_stdcur_get,
1257 .set_value = ctrl_stdcur_set,
1258 .is_dirty = ctrl_stdcur_is_dirty,
1259 .clear_dirty = ctrl_stdcur_clear_dirty,
1260 .val_to_sym = ctrl_std_val_to_sym,
1261 .sym_to_val = ctrl_std_sym_to_val,
1262 .type = pvr2_ctl_bitmask,
1263 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001264 .desc = "Video Standard Name",
1265 .name = "video_standard",
1266 .internal_id = PVR2_CID_STDENUM,
1267 .skip_init = !0,
1268 .get_value = ctrl_stdenumcur_get,
1269 .set_value = ctrl_stdenumcur_set,
1270 .is_dirty = ctrl_stdenumcur_is_dirty,
1271 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1272 .type = pvr2_ctl_enum,
1273 }
1274};
1275
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001276#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001277
1278
1279const char *pvr2_config_get_name(enum pvr2_config cfg)
1280{
1281 switch (cfg) {
1282 case pvr2_config_empty: return "empty";
1283 case pvr2_config_mpeg: return "mpeg";
1284 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001285 case pvr2_config_pcm: return "pcm";
1286 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001287 }
1288 return "<unknown>";
1289}
1290
1291
1292struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1293{
1294 return hdw->usb_dev;
1295}
1296
1297
1298unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1299{
1300 return hdw->serial_number;
1301}
1302
Mike Isely31a18542007-04-08 01:11:47 -03001303
1304const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1305{
1306 return hdw->bus_info;
1307}
1308
1309
Mike Isely1bde0282006-12-27 23:30:13 -03001310unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1311{
1312 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1313}
1314
1315/* Set the currently tuned frequency and account for all possible
1316 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001317static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001318{
Mike Isely7c74e572007-01-20 00:15:41 -03001319 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001320 if (hdw->freqSelector) {
1321 /* Swing over to radio frequency selection */
1322 hdw->freqSelector = 0;
1323 hdw->freqDirty = !0;
1324 }
Mike Isely1bde0282006-12-27 23:30:13 -03001325 if (hdw->freqValRadio != val) {
1326 hdw->freqValRadio = val;
1327 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001328 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001329 }
Mike Isely7c74e572007-01-20 00:15:41 -03001330 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001331 if (!(hdw->freqSelector)) {
1332 /* Swing over to television frequency selection */
1333 hdw->freqSelector = 1;
1334 hdw->freqDirty = !0;
1335 }
Mike Isely1bde0282006-12-27 23:30:13 -03001336 if (hdw->freqValTelevision != val) {
1337 hdw->freqValTelevision = val;
1338 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001339 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001340 }
Mike Isely1bde0282006-12-27 23:30:13 -03001341 }
1342}
1343
Mike Iselyd8554972006-06-26 20:58:46 -03001344int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1345{
1346 return hdw->unit_number;
1347}
1348
1349
1350/* Attempt to locate one of the given set of files. Messages are logged
1351 appropriate to what has been found. The return value will be 0 or
1352 greater on success (it will be the index of the file name found) and
1353 fw_entry will be filled in. Otherwise a negative error is returned on
1354 failure. If the return value is -ENOENT then no viable firmware file
1355 could be located. */
1356static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1357 const struct firmware **fw_entry,
1358 const char *fwtypename,
1359 unsigned int fwcount,
1360 const char *fwnames[])
1361{
1362 unsigned int idx;
1363 int ret = -EINVAL;
1364 for (idx = 0; idx < fwcount; idx++) {
1365 ret = request_firmware(fw_entry,
1366 fwnames[idx],
1367 &hdw->usb_dev->dev);
1368 if (!ret) {
1369 trace_firmware("Located %s firmware: %s;"
1370 " uploading...",
1371 fwtypename,
1372 fwnames[idx]);
1373 return idx;
1374 }
1375 if (ret == -ENOENT) continue;
1376 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1377 "request_firmware fatal error with code=%d",ret);
1378 return ret;
1379 }
1380 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1381 "***WARNING***"
1382 " Device %s firmware"
1383 " seems to be missing.",
1384 fwtypename);
1385 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1386 "Did you install the pvrusb2 firmware files"
1387 " in their proper location?");
1388 if (fwcount == 1) {
1389 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1390 "request_firmware unable to locate %s file %s",
1391 fwtypename,fwnames[0]);
1392 } else {
1393 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1394 "request_firmware unable to locate"
1395 " one of the following %s files:",
1396 fwtypename);
1397 for (idx = 0; idx < fwcount; idx++) {
1398 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1399 "request_firmware: Failed to find %s",
1400 fwnames[idx]);
1401 }
1402 }
1403 return ret;
1404}
1405
1406
1407/*
1408 * pvr2_upload_firmware1().
1409 *
1410 * Send the 8051 firmware to the device. After the upload, arrange for
1411 * device to re-enumerate.
1412 *
1413 * NOTE : the pointer to the firmware data given by request_firmware()
1414 * is not suitable for an usb transaction.
1415 *
1416 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001417static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001418{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001419 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001420 void *fw_ptr;
1421 unsigned int pipe;
1422 int ret;
1423 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001424
Mike Isely989eb152007-11-26 01:53:12 -03001425 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001426 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001427 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1428 "Connected device type defines"
1429 " no firmware to upload; ignoring firmware");
1430 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001431 }
1432
Mike Iselyd8554972006-06-26 20:58:46 -03001433 hdw->fw1_state = FW1_STATE_FAILED; // default result
1434
1435 trace_firmware("pvr2_upload_firmware1");
1436
1437 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001438 hdw->hdw_desc->fx2_firmware.cnt,
1439 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001440 if (ret < 0) {
1441 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1442 return ret;
1443 }
1444
1445 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1446 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1447
1448 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1449
1450 if (fw_entry->size != 0x2000){
1451 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1452 release_firmware(fw_entry);
1453 return -ENOMEM;
1454 }
1455
1456 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1457 if (fw_ptr == NULL){
1458 release_firmware(fw_entry);
1459 return -ENOMEM;
1460 }
1461
1462 /* We have to hold the CPU during firmware upload. */
1463 pvr2_hdw_cpureset_assert(hdw,1);
1464
1465 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1466 chunk. */
1467
1468 ret = 0;
1469 for(address = 0; address < fw_entry->size; address += 0x800) {
1470 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1471 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1472 0, fw_ptr, 0x800, HZ);
1473 }
1474
1475 trace_firmware("Upload done, releasing device's CPU");
1476
1477 /* Now release the CPU. It will disconnect and reconnect later. */
1478 pvr2_hdw_cpureset_assert(hdw,0);
1479
1480 kfree(fw_ptr);
1481 release_firmware(fw_entry);
1482
1483 trace_firmware("Upload done (%d bytes sent)",ret);
1484
1485 /* We should have written 8192 bytes */
1486 if (ret == 8192) {
1487 hdw->fw1_state = FW1_STATE_RELOAD;
1488 return 0;
1489 }
1490
1491 return -EIO;
1492}
1493
1494
1495/*
1496 * pvr2_upload_firmware2()
1497 *
1498 * This uploads encoder firmware on endpoint 2.
1499 *
1500 */
1501
1502int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1503{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001504 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001505 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001506 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001507 int actual_length;
1508 int ret = 0;
1509 int fwidx;
1510 static const char *fw_files[] = {
1511 CX2341X_FIRM_ENC_FILENAME,
1512 };
1513
Mike Isely989eb152007-11-26 01:53:12 -03001514 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001515 return 0;
1516 }
1517
Mike Iselyd8554972006-06-26 20:58:46 -03001518 trace_firmware("pvr2_upload_firmware2");
1519
1520 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001521 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001522 if (ret < 0) return ret;
1523 fwidx = ret;
1524 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001525 /* Since we're about to completely reinitialize the encoder,
1526 invalidate our cached copy of its configuration state. Next
1527 time we configure the encoder, then we'll fully configure it. */
1528 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001529
Mike Iselyd913d632008-04-06 04:04:35 -03001530 /* Encoder is about to be reset so note that as far as we're
1531 concerned now, the encoder has never been run. */
1532 del_timer_sync(&hdw->encoder_run_timer);
1533 if (hdw->state_encoder_runok) {
1534 hdw->state_encoder_runok = 0;
1535 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1536 }
1537
Mike Iselyd8554972006-06-26 20:58:46 -03001538 /* First prepare firmware loading */
1539 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1540 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1541 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1542 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1543 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1544 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1545 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1546 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1547 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1548 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1549 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1550 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1551 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1552 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1553 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1554 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001555 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1556 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001557
1558 if (ret) {
1559 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1560 "firmware2 upload prep failed, ret=%d",ret);
1561 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001562 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001563 }
1564
1565 /* Now send firmware */
1566
1567 fw_len = fw_entry->size;
1568
Mike Isely90060d32007-02-08 02:02:53 -03001569 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001570 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1571 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001572 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001573 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001574 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001575 ret = -EINVAL;
1576 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001577 }
1578
1579 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1580 if (fw_ptr == NULL){
1581 release_firmware(fw_entry);
1582 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1583 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001584 ret = -ENOMEM;
1585 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001586 }
1587
1588 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1589
Mike Isely90060d32007-02-08 02:02:53 -03001590 fw_done = 0;
1591 for (fw_done = 0; fw_done < fw_len;) {
1592 bcnt = fw_len - fw_done;
1593 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1594 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1595 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001596 /* Some background info: The data being swapped here is a
1597 firmware image destined for the mpeg encoder chip that
1598 lives at the other end of a USB endpoint. The encoder
1599 chip always talks in 32 bit chunks and its storage is
1600 organized into 32 bit words. However from the file
1601 system to the encoder chip everything is purely a byte
1602 stream. The firmware file's contents are always 32 bit
1603 swapped from what the encoder expects. Thus the need
1604 always exists to swap the bytes regardless of the endian
1605 type of the host processor and therefore swab32() makes
1606 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001607 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001608 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001609
Mike Isely90060d32007-02-08 02:02:53 -03001610 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001611 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001612 ret |= (actual_length != bcnt);
1613 if (ret) break;
1614 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001615 }
1616
1617 trace_firmware("upload of %s : %i / %i ",
1618 fw_files[fwidx],fw_done,fw_len);
1619
1620 kfree(fw_ptr);
1621 release_firmware(fw_entry);
1622
1623 if (ret) {
1624 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1625 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001626 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001627 }
1628
1629 /* Finish upload */
1630
1631 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1632 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001633 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001634
1635 if (ret) {
1636 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1637 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001638 }
Mike Isely21684ba2008-04-21 03:49:33 -03001639
1640 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001641 if (hdw->hdw_desc->signal_routing_scheme ==
1642 PVR2_ROUTING_SCHEME_GOTVIEW) {
1643 /* Ensure that GPIO 11 is set to output for GOTVIEW
1644 hardware. */
1645 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1646 }
Mike Iselyd8554972006-06-26 20:58:46 -03001647 return ret;
1648}
1649
1650
Mike Isely681c7392007-11-26 01:48:52 -03001651static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001652{
Mike Isely681c7392007-11-26 01:48:52 -03001653 if (st < ARRAY_SIZE(pvr2_state_names)) {
1654 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001655 }
Mike Isely681c7392007-11-26 01:48:52 -03001656 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001657}
1658
Mike Isely681c7392007-11-26 01:48:52 -03001659static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001660{
Mike Isely681c7392007-11-26 01:48:52 -03001661 if (!hdw->decoder_ctrl) {
1662 if (!hdw->flag_decoder_missed) {
1663 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1664 "WARNING: No decoder present");
1665 hdw->flag_decoder_missed = !0;
1666 trace_stbit("flag_decoder_missed",
1667 hdw->flag_decoder_missed);
1668 }
1669 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001670 }
Mike Isely681c7392007-11-26 01:48:52 -03001671 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001672 return 0;
1673}
1674
1675
Mike Isely681c7392007-11-26 01:48:52 -03001676void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1677{
1678 if (hdw->decoder_ctrl == ptr) return;
1679 hdw->decoder_ctrl = ptr;
1680 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1681 hdw->flag_decoder_missed = 0;
1682 trace_stbit("flag_decoder_missed",
1683 hdw->flag_decoder_missed);
1684 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1685 "Decoder has appeared");
1686 pvr2_hdw_state_sched(hdw);
1687 }
1688}
1689
1690
1691int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1692{
1693 return hdw->master_state;
1694}
1695
1696
1697static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1698{
1699 if (!hdw->flag_tripped) return 0;
1700 hdw->flag_tripped = 0;
1701 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1702 "Clearing driver error statuss");
1703 return !0;
1704}
1705
1706
1707int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1708{
1709 int fl;
1710 LOCK_TAKE(hdw->big_lock); do {
1711 fl = pvr2_hdw_untrip_unlocked(hdw);
1712 } while (0); LOCK_GIVE(hdw->big_lock);
1713 if (fl) pvr2_hdw_state_sched(hdw);
1714 return 0;
1715}
1716
1717
Mike Isely681c7392007-11-26 01:48:52 -03001718
1719
Mike Iselyd8554972006-06-26 20:58:46 -03001720int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1721{
Mike Isely681c7392007-11-26 01:48:52 -03001722 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001723}
1724
1725
1726int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1727{
Mike Isely681c7392007-11-26 01:48:52 -03001728 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001729 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001730 pvr2_hdw_untrip_unlocked(hdw);
1731 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1732 hdw->state_pipeline_req = enable_flag != 0;
1733 pvr2_trace(PVR2_TRACE_START_STOP,
1734 "/*--TRACE_STREAM--*/ %s",
1735 enable_flag ? "enable" : "disable");
1736 }
1737 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001738 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001739 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1740 if (enable_flag) {
1741 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1742 if (st != PVR2_STATE_READY) return -EIO;
1743 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1744 }
1745 }
Mike Iselyd8554972006-06-26 20:58:46 -03001746 return 0;
1747}
1748
1749
1750int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1751{
Mike Isely681c7392007-11-26 01:48:52 -03001752 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001753 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001754 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1755 hdw->desired_stream_type = config;
1756 hdw->state_pipeline_config = 0;
1757 trace_stbit("state_pipeline_config",
1758 hdw->state_pipeline_config);
1759 pvr2_hdw_state_sched(hdw);
1760 }
Mike Iselyd8554972006-06-26 20:58:46 -03001761 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001762 if (fl) return 0;
1763 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001764}
1765
1766
1767static int get_default_tuner_type(struct pvr2_hdw *hdw)
1768{
1769 int unit_number = hdw->unit_number;
1770 int tp = -1;
1771 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1772 tp = tuner[unit_number];
1773 }
1774 if (tp < 0) return -EINVAL;
1775 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001776 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001777 return 0;
1778}
1779
1780
1781static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1782{
1783 int unit_number = hdw->unit_number;
1784 int tp = 0;
1785 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1786 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001787 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001788 }
Mike Isely6a540252007-12-02 23:51:34 -03001789 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001790}
1791
1792
1793static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1794{
1795 int unit_number = hdw->unit_number;
1796 int tp = 0;
1797 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1798 tp = tolerance[unit_number];
1799 }
1800 return tp;
1801}
1802
1803
1804static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1805{
1806 /* Try a harmless request to fetch the eeprom's address over
1807 endpoint 1. See what happens. Only the full FX2 image can
1808 respond to this. If this probe fails then likely the FX2
1809 firmware needs be loaded. */
1810 int result;
1811 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001812 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001813 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1814 hdw->cmd_buffer,1,
1815 hdw->cmd_buffer,1);
1816 if (result < 0) break;
1817 } while(0); LOCK_GIVE(hdw->ctl_lock);
1818 if (result) {
1819 pvr2_trace(PVR2_TRACE_INIT,
1820 "Probe of device endpoint 1 result status %d",
1821 result);
1822 } else {
1823 pvr2_trace(PVR2_TRACE_INIT,
1824 "Probe of device endpoint 1 succeeded");
1825 }
1826 return result == 0;
1827}
1828
Mike Isely9f66d4e2007-09-08 22:28:51 -03001829struct pvr2_std_hack {
1830 v4l2_std_id pat; /* Pattern to match */
1831 v4l2_std_id msk; /* Which bits we care about */
1832 v4l2_std_id std; /* What additional standards or default to set */
1833};
1834
1835/* This data structure labels specific combinations of standards from
1836 tveeprom that we'll try to recognize. If we recognize one, then assume
1837 a specified default standard to use. This is here because tveeprom only
1838 tells us about available standards not the intended default standard (if
1839 any) for the device in question. We guess the default based on what has
1840 been reported as available. Note that this is only for guessing a
1841 default - which can always be overridden explicitly - and if the user
1842 has otherwise named a default then that default will always be used in
1843 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001844static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001845 { /* PAL(B/G) */
1846 .pat = V4L2_STD_B|V4L2_STD_GH,
1847 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1848 },
1849 { /* NTSC(M) */
1850 .pat = V4L2_STD_MN,
1851 .std = V4L2_STD_NTSC_M,
1852 },
1853 { /* PAL(I) */
1854 .pat = V4L2_STD_PAL_I,
1855 .std = V4L2_STD_PAL_I,
1856 },
1857 { /* SECAM(L/L') */
1858 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1859 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1860 },
1861 { /* PAL(D/D1/K) */
1862 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001863 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001864 },
1865};
1866
Mike Iselyd8554972006-06-26 20:58:46 -03001867static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1868{
1869 char buf[40];
1870 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001871 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001872
1873 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001874 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001875
1876 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001877 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001878 "Supported video standard(s) reported available"
1879 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001880 bcnt,buf);
1881
1882 hdw->std_mask_avail = hdw->std_mask_eeprom;
1883
Mike Isely3d290bd2007-12-03 01:47:12 -03001884 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001885 if (std2) {
1886 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001887 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001888 "Expanding supported video standards"
1889 " to include: %.*s",
1890 bcnt,buf);
1891 hdw->std_mask_avail |= std2;
1892 }
1893
1894 pvr2_hdw_internal_set_std_avail(hdw);
1895
1896 if (std1) {
1897 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001898 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001899 "Initial video standard forced to %.*s",
1900 bcnt,buf);
1901 hdw->std_mask_cur = std1;
1902 hdw->std_dirty = !0;
1903 pvr2_hdw_internal_find_stdenum(hdw);
1904 return;
1905 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001906 if (std3) {
1907 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1908 pvr2_trace(PVR2_TRACE_STD,
1909 "Initial video standard"
1910 " (determined by device type): %.*s",bcnt,buf);
1911 hdw->std_mask_cur = std3;
1912 hdw->std_dirty = !0;
1913 pvr2_hdw_internal_find_stdenum(hdw);
1914 return;
1915 }
Mike Iselyd8554972006-06-26 20:58:46 -03001916
Mike Isely9f66d4e2007-09-08 22:28:51 -03001917 {
1918 unsigned int idx;
1919 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1920 if (std_eeprom_maps[idx].msk ?
1921 ((std_eeprom_maps[idx].pat ^
1922 hdw->std_mask_eeprom) &
1923 std_eeprom_maps[idx].msk) :
1924 (std_eeprom_maps[idx].pat !=
1925 hdw->std_mask_eeprom)) continue;
1926 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1927 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001928 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001929 "Initial video standard guessed as %.*s",
1930 bcnt,buf);
1931 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1932 hdw->std_dirty = !0;
1933 pvr2_hdw_internal_find_stdenum(hdw);
1934 return;
1935 }
1936 }
1937
Mike Iselyd8554972006-06-26 20:58:46 -03001938 if (hdw->std_enum_cnt > 1) {
1939 // Autoselect the first listed standard
1940 hdw->std_enum_cur = 1;
1941 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1942 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001943 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001944 "Initial video standard auto-selected to %s",
1945 hdw->std_defs[hdw->std_enum_cur-1].name);
1946 return;
1947 }
1948
Mike Isely0885ba12006-06-25 21:30:47 -03001949 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001950 "Unable to select a viable initial video standard");
1951}
1952
1953
1954static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1955{
1956 int ret;
1957 unsigned int idx;
1958 struct pvr2_ctrl *cptr;
1959 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001960 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001961 if (!reloadFl) {
1962 reloadFl =
1963 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1964 == 0);
1965 if (reloadFl) {
1966 pvr2_trace(PVR2_TRACE_INIT,
1967 "USB endpoint config looks strange"
1968 "; possibly firmware needs to be"
1969 " loaded");
1970 }
1971 }
1972 if (!reloadFl) {
1973 reloadFl = !pvr2_hdw_check_firmware(hdw);
1974 if (reloadFl) {
1975 pvr2_trace(PVR2_TRACE_INIT,
1976 "Check for FX2 firmware failed"
1977 "; possibly firmware needs to be"
1978 " loaded");
1979 }
1980 }
Mike Iselyd8554972006-06-26 20:58:46 -03001981 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001982 if (pvr2_upload_firmware1(hdw) != 0) {
1983 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1984 "Failure uploading firmware1");
1985 }
1986 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001987 }
1988 }
Mike Iselyd8554972006-06-26 20:58:46 -03001989 hdw->fw1_state = FW1_STATE_OK;
1990
1991 if (initusbreset) {
1992 pvr2_hdw_device_reset(hdw);
1993 }
1994 if (!pvr2_hdw_dev_ok(hdw)) return;
1995
Mike Isely989eb152007-11-26 01:53:12 -03001996 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1997 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001998 }
1999
Mike Isely989eb152007-11-26 01:53:12 -03002000 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002001 pvr2_hdw_cmd_powerup(hdw);
2002 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002003 }
2004
Mike Isely31335b12008-07-25 19:35:31 -03002005 /* Take the IR chip out of reset, if appropriate */
2006 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
2007 pvr2_issue_simple_cmd(hdw,
2008 FX2CMD_HCW_ZILOG_RESET |
2009 (1 << 8) |
2010 ((0) << 16));
2011 }
2012
Mike Iselyd8554972006-06-26 20:58:46 -03002013 // This step MUST happen after the earlier powerup step.
2014 pvr2_i2c_core_init(hdw);
2015 if (!pvr2_hdw_dev_ok(hdw)) return;
2016
Mike Iselyc05c0462006-06-25 20:04:25 -03002017 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002018 cptr = hdw->controls + idx;
2019 if (cptr->info->skip_init) continue;
2020 if (!cptr->info->set_value) continue;
2021 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2022 }
2023
Mike Isely1bde0282006-12-27 23:30:13 -03002024 /* Set up special default values for the television and radio
2025 frequencies here. It's not really important what these defaults
2026 are, but I set them to something usable in the Chicago area just
2027 to make driver testing a little easier. */
2028
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002029 hdw->freqValTelevision = default_tv_freq;
2030 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002031
Mike Iselyd8554972006-06-26 20:58:46 -03002032 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2033 // thread-safe against the normal pvr2_send_request() mechanism.
2034 // (We should make it thread safe).
2035
Mike Iselyaaf78842007-11-26 02:04:11 -03002036 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2037 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002038 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002039 if (ret < 0) {
2040 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2041 "Unable to determine location of eeprom,"
2042 " skipping");
2043 } else {
2044 hdw->eeprom_addr = ret;
2045 pvr2_eeprom_analyze(hdw);
2046 if (!pvr2_hdw_dev_ok(hdw)) return;
2047 }
2048 } else {
2049 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2050 hdw->tuner_updated = !0;
2051 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002052 }
2053
2054 pvr2_hdw_setup_std(hdw);
2055
2056 if (!get_default_tuner_type(hdw)) {
2057 pvr2_trace(PVR2_TRACE_INIT,
2058 "pvr2_hdw_setup: Tuner type overridden to %d",
2059 hdw->tuner_type);
2060 }
2061
Mike Iselyd8554972006-06-26 20:58:46 -03002062 pvr2_i2c_core_check_stale(hdw);
2063 hdw->tuner_updated = 0;
2064
2065 if (!pvr2_hdw_dev_ok(hdw)) return;
2066
Mike Isely1df59f02008-04-21 03:50:39 -03002067 if (hdw->hdw_desc->signal_routing_scheme ==
2068 PVR2_ROUTING_SCHEME_GOTVIEW) {
2069 /* Ensure that GPIO 11 is set to output for GOTVIEW
2070 hardware. */
2071 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2072 }
2073
Mike Isely681c7392007-11-26 01:48:52 -03002074 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002075
2076 hdw->vid_stream = pvr2_stream_create();
2077 if (!pvr2_hdw_dev_ok(hdw)) return;
2078 pvr2_trace(PVR2_TRACE_INIT,
2079 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2080 if (hdw->vid_stream) {
2081 idx = get_default_error_tolerance(hdw);
2082 if (idx) {
2083 pvr2_trace(PVR2_TRACE_INIT,
2084 "pvr2_hdw_setup: video stream %p"
2085 " setting tolerance %u",
2086 hdw->vid_stream,idx);
2087 }
2088 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2089 PVR2_VID_ENDPOINT,idx);
2090 }
2091
2092 if (!pvr2_hdw_dev_ok(hdw)) return;
2093
Mike Iselyd8554972006-06-26 20:58:46 -03002094 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002095
2096 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002097}
2098
2099
Mike Isely681c7392007-11-26 01:48:52 -03002100/* Set up the structure and attempt to put the device into a usable state.
2101 This can be a time-consuming operation, which is why it is not done
2102 internally as part of the create() step. */
2103static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002104{
2105 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002106 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002107 pvr2_hdw_setup_low(hdw);
2108 pvr2_trace(PVR2_TRACE_INIT,
2109 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002110 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002111 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002112 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002113 pvr2_trace(
2114 PVR2_TRACE_INFO,
2115 "Device initialization"
2116 " completed successfully.");
2117 break;
2118 }
2119 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2120 pvr2_trace(
2121 PVR2_TRACE_INFO,
2122 "Device microcontroller firmware"
2123 " (re)loaded; it should now reset"
2124 " and reconnect.");
2125 break;
2126 }
2127 pvr2_trace(
2128 PVR2_TRACE_ERROR_LEGS,
2129 "Device initialization was not successful.");
2130 if (hdw->fw1_state == FW1_STATE_MISSING) {
2131 pvr2_trace(
2132 PVR2_TRACE_ERROR_LEGS,
2133 "Giving up since device"
2134 " microcontroller firmware"
2135 " appears to be missing.");
2136 break;
2137 }
2138 }
2139 if (procreload) {
2140 pvr2_trace(
2141 PVR2_TRACE_ERROR_LEGS,
2142 "Attempting pvrusb2 recovery by reloading"
2143 " primary firmware.");
2144 pvr2_trace(
2145 PVR2_TRACE_ERROR_LEGS,
2146 "If this works, device should disconnect"
2147 " and reconnect in a sane state.");
2148 hdw->fw1_state = FW1_STATE_UNKNOWN;
2149 pvr2_upload_firmware1(hdw);
2150 } else {
2151 pvr2_trace(
2152 PVR2_TRACE_ERROR_LEGS,
2153 "***WARNING*** pvrusb2 device hardware"
2154 " appears to be jammed"
2155 " and I can't clear it.");
2156 pvr2_trace(
2157 PVR2_TRACE_ERROR_LEGS,
2158 "You might need to power cycle"
2159 " the pvrusb2 device"
2160 " in order to recover.");
2161 }
Mike Isely681c7392007-11-26 01:48:52 -03002162 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002163 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002164}
2165
2166
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002167/* Perform second stage initialization. Set callback pointer first so that
2168 we can avoid a possible initialization race (if the kernel thread runs
2169 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002170int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2171 void (*callback_func)(void *),
2172 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002173{
2174 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002175 if (hdw->flag_disconnected) {
2176 /* Handle a race here: If we're already
2177 disconnected by this point, then give up. If we
2178 get past this then we'll remain connected for
2179 the duration of initialization since the entire
2180 initialization sequence is now protected by the
2181 big_lock. */
2182 break;
2183 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002184 hdw->state_data = callback_data;
2185 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002186 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002187 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002188 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002189}
2190
2191
2192/* Create, set up, and return a structure for interacting with the
2193 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002194struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2195 const struct usb_device_id *devid)
2196{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002197 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002198 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002199 int valid_std_mask;
2200 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03002201 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002202 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002203 struct v4l2_queryctrl qctrl;
2204 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002205
Mike Iselyd130fa82007-12-08 17:20:06 -03002206 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002207
Mike Iselyfe15f132008-08-30 18:11:40 -03002208 if (hdw_desc == NULL) {
2209 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2210 " No device description pointer,"
2211 " unable to continue.");
2212 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2213 " please contact Mike Isely <isely@pobox.com>"
2214 " to get it included in the driver\n");
2215 goto fail;
2216 }
2217
Mike Iselyca545f72007-01-20 00:37:11 -03002218 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002219 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002220 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002221 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002222
2223 init_timer(&hdw->quiescent_timer);
2224 hdw->quiescent_timer.data = (unsigned long)hdw;
2225 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2226
2227 init_timer(&hdw->encoder_wait_timer);
2228 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2229 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2230
Mike Iselyd913d632008-04-06 04:04:35 -03002231 init_timer(&hdw->encoder_run_timer);
2232 hdw->encoder_run_timer.data = (unsigned long)hdw;
2233 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2234
Mike Isely681c7392007-11-26 01:48:52 -03002235 hdw->master_state = PVR2_STATE_DEAD;
2236
2237 init_waitqueue_head(&hdw->state_wait_data);
2238
Mike Isely18103c572007-01-20 00:09:47 -03002239 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002240 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002241
Mike Isely7fb20fa2008-04-22 14:45:37 -03002242 /* Calculate which inputs are OK */
2243 m = 0;
2244 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002245 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2246 m |= 1 << PVR2_CVAL_INPUT_DTV;
2247 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002248 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2249 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2250 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2251 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002252 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002253
Mike Isely62433e32008-04-22 14:45:40 -03002254 /* If not a hybrid device, pathway_state never changes. So
2255 initialize it here to what it should forever be. */
2256 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2257 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2258 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2259 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2260 }
2261
Mike Iselyc05c0462006-06-25 20:04:25 -03002262 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002263 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002264 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002265 GFP_KERNEL);
2266 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002267 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002268 for (idx = 0; idx < hdw->control_cnt; idx++) {
2269 cptr = hdw->controls + idx;
2270 cptr->hdw = hdw;
2271 }
Mike Iselyd8554972006-06-26 20:58:46 -03002272 for (idx = 0; idx < 32; idx++) {
2273 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2274 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002275 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002276 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002277 cptr->info = control_defs+idx;
2278 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002279
2280 /* Ensure that default input choice is a valid one. */
2281 m = hdw->input_avail_mask;
2282 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2283 if (!((1 << idx) & m)) continue;
2284 hdw->input_val = idx;
2285 break;
2286 }
2287
Mike Iselyb30d2442006-06-25 20:05:01 -03002288 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002289 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002290 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2291 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002292 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2293 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2294 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2295 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2296 ciptr->name = mpeg_ids[idx].strid;
2297 ciptr->v4l_id = mpeg_ids[idx].id;
2298 ciptr->skip_init = !0;
2299 ciptr->get_value = ctrl_cx2341x_get;
2300 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2301 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2302 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2303 qctrl.id = ciptr->v4l_id;
2304 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2305 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2306 ciptr->set_value = ctrl_cx2341x_set;
2307 }
2308 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2309 PVR2_CTLD_INFO_DESC_SIZE);
2310 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2311 ciptr->default_value = qctrl.default_value;
2312 switch (qctrl.type) {
2313 default:
2314 case V4L2_CTRL_TYPE_INTEGER:
2315 ciptr->type = pvr2_ctl_int;
2316 ciptr->def.type_int.min_value = qctrl.minimum;
2317 ciptr->def.type_int.max_value = qctrl.maximum;
2318 break;
2319 case V4L2_CTRL_TYPE_BOOLEAN:
2320 ciptr->type = pvr2_ctl_bool;
2321 break;
2322 case V4L2_CTRL_TYPE_MENU:
2323 ciptr->type = pvr2_ctl_enum;
2324 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002325 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2326 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002327 for (cnt1 = 0;
2328 ciptr->def.type_enum.value_names[cnt1] != NULL;
2329 cnt1++) { }
2330 ciptr->def.type_enum.count = cnt1;
2331 break;
2332 }
2333 cptr->info = ciptr;
2334 }
Mike Iselyd8554972006-06-26 20:58:46 -03002335
2336 // Initialize video standard enum dynamic control
2337 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2338 if (cptr) {
2339 memcpy(&hdw->std_info_enum,cptr->info,
2340 sizeof(hdw->std_info_enum));
2341 cptr->info = &hdw->std_info_enum;
2342
2343 }
2344 // Initialize control data regarding video standard masks
2345 valid_std_mask = pvr2_std_get_usable();
2346 for (idx = 0; idx < 32; idx++) {
2347 if (!(valid_std_mask & (1 << idx))) continue;
2348 cnt1 = pvr2_std_id_to_str(
2349 hdw->std_mask_names[idx],
2350 sizeof(hdw->std_mask_names[idx])-1,
2351 1 << idx);
2352 hdw->std_mask_names[idx][cnt1] = 0;
2353 }
2354 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2355 if (cptr) {
2356 memcpy(&hdw->std_info_avail,cptr->info,
2357 sizeof(hdw->std_info_avail));
2358 cptr->info = &hdw->std_info_avail;
2359 hdw->std_info_avail.def.type_bitmask.bit_names =
2360 hdw->std_mask_ptrs;
2361 hdw->std_info_avail.def.type_bitmask.valid_bits =
2362 valid_std_mask;
2363 }
2364 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2365 if (cptr) {
2366 memcpy(&hdw->std_info_cur,cptr->info,
2367 sizeof(hdw->std_info_cur));
2368 cptr->info = &hdw->std_info_cur;
2369 hdw->std_info_cur.def.type_bitmask.bit_names =
2370 hdw->std_mask_ptrs;
2371 hdw->std_info_avail.def.type_bitmask.valid_bits =
2372 valid_std_mask;
2373 }
2374
Mike Isely432907f2008-08-31 21:02:20 -03002375 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002376 hdw->eeprom_addr = -1;
2377 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002378 hdw->v4l_minor_number_video = -1;
2379 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002380 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002381 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2382 if (!hdw->ctl_write_buffer) goto fail;
2383 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2384 if (!hdw->ctl_read_buffer) goto fail;
2385 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2386 if (!hdw->ctl_write_urb) goto fail;
2387 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2388 if (!hdw->ctl_read_urb) goto fail;
2389
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002390 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002391 for (idx = 0; idx < PVR_NUM; idx++) {
2392 if (unit_pointers[idx]) continue;
2393 hdw->unit_number = idx;
2394 unit_pointers[idx] = hdw;
2395 break;
2396 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002397 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002398
2399 cnt1 = 0;
2400 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2401 cnt1 += cnt2;
2402 if (hdw->unit_number >= 0) {
2403 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2404 ('a' + hdw->unit_number));
2405 cnt1 += cnt2;
2406 }
2407 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2408 hdw->name[cnt1] = 0;
2409
Mike Isely681c7392007-11-26 01:48:52 -03002410 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2411 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2412 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002413
Mike Iselyd8554972006-06-26 20:58:46 -03002414 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2415 hdw->unit_number,hdw->name);
2416
2417 hdw->tuner_type = -1;
2418 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002419
2420 hdw->usb_intf = intf;
2421 hdw->usb_dev = interface_to_usbdev(intf);
2422
Mike Isely31a18542007-04-08 01:11:47 -03002423 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2424 "usb %s address %d",
2425 hdw->usb_dev->dev.bus_id,
2426 hdw->usb_dev->devnum);
2427
Mike Iselyd8554972006-06-26 20:58:46 -03002428 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2429 usb_set_interface(hdw->usb_dev,ifnum,0);
2430
2431 mutex_init(&hdw->ctl_lock_mutex);
2432 mutex_init(&hdw->big_lock_mutex);
2433
2434 return hdw;
2435 fail:
2436 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002437 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002438 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002439 del_timer_sync(&hdw->encoder_wait_timer);
2440 if (hdw->workqueue) {
2441 flush_workqueue(hdw->workqueue);
2442 destroy_workqueue(hdw->workqueue);
2443 hdw->workqueue = NULL;
2444 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002445 usb_free_urb(hdw->ctl_read_urb);
2446 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002447 kfree(hdw->ctl_read_buffer);
2448 kfree(hdw->ctl_write_buffer);
2449 kfree(hdw->controls);
2450 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002451 kfree(hdw->std_defs);
2452 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002453 kfree(hdw);
2454 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002455 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002456}
2457
2458
2459/* Remove _all_ associations between this driver and the underlying USB
2460 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002461static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002462{
2463 if (hdw->flag_disconnected) return;
2464 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2465 if (hdw->ctl_read_urb) {
2466 usb_kill_urb(hdw->ctl_read_urb);
2467 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002468 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002469 }
2470 if (hdw->ctl_write_urb) {
2471 usb_kill_urb(hdw->ctl_write_urb);
2472 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002473 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002474 }
2475 if (hdw->ctl_read_buffer) {
2476 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002477 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002478 }
2479 if (hdw->ctl_write_buffer) {
2480 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002481 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002482 }
Mike Iselyd8554972006-06-26 20:58:46 -03002483 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002484 hdw->usb_dev = NULL;
2485 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002486 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002487}
2488
2489
2490/* Destroy hardware interaction structure */
2491void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2492{
Mike Isely401c27c2007-09-08 22:11:46 -03002493 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002494 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002495 if (hdw->workqueue) {
2496 flush_workqueue(hdw->workqueue);
2497 destroy_workqueue(hdw->workqueue);
2498 hdw->workqueue = NULL;
2499 }
Mike Isely8f591002008-04-22 14:45:45 -03002500 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002501 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002502 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002503 if (hdw->fw_buffer) {
2504 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002505 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002506 }
2507 if (hdw->vid_stream) {
2508 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002509 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002510 }
Mike Iselyd8554972006-06-26 20:58:46 -03002511 if (hdw->decoder_ctrl) {
2512 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2513 }
2514 pvr2_i2c_core_done(hdw);
2515 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002516 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002517 if ((hdw->unit_number >= 0) &&
2518 (hdw->unit_number < PVR_NUM) &&
2519 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002520 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002521 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002522 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002523 kfree(hdw->controls);
2524 kfree(hdw->mpeg_ctrl_info);
2525 kfree(hdw->std_defs);
2526 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002527 kfree(hdw);
2528}
2529
2530
Mike Iselyd8554972006-06-26 20:58:46 -03002531int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2532{
2533 return (hdw && hdw->flag_ok);
2534}
2535
2536
2537/* Called when hardware has been unplugged */
2538void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2539{
2540 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2541 LOCK_TAKE(hdw->big_lock);
2542 LOCK_TAKE(hdw->ctl_lock);
2543 pvr2_hdw_remove_usb_stuff(hdw);
2544 LOCK_GIVE(hdw->ctl_lock);
2545 LOCK_GIVE(hdw->big_lock);
2546}
2547
2548
2549// Attempt to autoselect an appropriate value for std_enum_cur given
2550// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002551static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002552{
2553 unsigned int idx;
2554 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2555 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2556 hdw->std_enum_cur = idx;
2557 return;
2558 }
2559 }
2560 hdw->std_enum_cur = 0;
2561}
2562
2563
2564// Calculate correct set of enumerated standards based on currently known
2565// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002566static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002567{
2568 struct v4l2_standard *newstd;
2569 unsigned int std_cnt;
2570 unsigned int idx;
2571
2572 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2573
2574 if (hdw->std_defs) {
2575 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002576 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002577 }
2578 hdw->std_enum_cnt = 0;
2579 if (hdw->std_enum_names) {
2580 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002581 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002582 }
2583
2584 if (!std_cnt) {
2585 pvr2_trace(
2586 PVR2_TRACE_ERROR_LEGS,
2587 "WARNING: Failed to identify any viable standards");
2588 }
2589 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2590 hdw->std_enum_names[0] = "none";
2591 for (idx = 0; idx < std_cnt; idx++) {
2592 hdw->std_enum_names[idx+1] =
2593 newstd[idx].name;
2594 }
2595 // Set up the dynamic control for this standard
2596 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2597 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2598 hdw->std_defs = newstd;
2599 hdw->std_enum_cnt = std_cnt+1;
2600 hdw->std_enum_cur = 0;
2601 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2602}
2603
2604
2605int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2606 struct v4l2_standard *std,
2607 unsigned int idx)
2608{
2609 int ret = -EINVAL;
2610 if (!idx) return ret;
2611 LOCK_TAKE(hdw->big_lock); do {
2612 if (idx >= hdw->std_enum_cnt) break;
2613 idx--;
2614 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2615 ret = 0;
2616 } while (0); LOCK_GIVE(hdw->big_lock);
2617 return ret;
2618}
2619
2620
2621/* Get the number of defined controls */
2622unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2623{
Mike Iselyc05c0462006-06-25 20:04:25 -03002624 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002625}
2626
2627
2628/* Retrieve a control handle given its index (0..count-1) */
2629struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2630 unsigned int idx)
2631{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002632 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002633 return hdw->controls + idx;
2634}
2635
2636
2637/* Retrieve a control handle given its index (0..count-1) */
2638struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2639 unsigned int ctl_id)
2640{
2641 struct pvr2_ctrl *cptr;
2642 unsigned int idx;
2643 int i;
2644
2645 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002646 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002647 cptr = hdw->controls + idx;
2648 i = cptr->info->internal_id;
2649 if (i && (i == ctl_id)) return cptr;
2650 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002651 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002652}
2653
2654
Mike Iselya761f432006-06-25 20:04:44 -03002655/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002656struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2657{
2658 struct pvr2_ctrl *cptr;
2659 unsigned int idx;
2660 int i;
2661
2662 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002663 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002664 cptr = hdw->controls + idx;
2665 i = cptr->info->v4l_id;
2666 if (i && (i == ctl_id)) return cptr;
2667 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002668 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002669}
2670
2671
Mike Iselya761f432006-06-25 20:04:44 -03002672/* Given a V4L ID for its immediate predecessor, retrieve the control
2673 structure associated with it. */
2674struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2675 unsigned int ctl_id)
2676{
2677 struct pvr2_ctrl *cptr,*cp2;
2678 unsigned int idx;
2679 int i;
2680
2681 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002682 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002683 for (idx = 0; idx < hdw->control_cnt; idx++) {
2684 cptr = hdw->controls + idx;
2685 i = cptr->info->v4l_id;
2686 if (!i) continue;
2687 if (i <= ctl_id) continue;
2688 if (cp2 && (cp2->info->v4l_id < i)) continue;
2689 cp2 = cptr;
2690 }
2691 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002692 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002693}
2694
2695
Mike Iselyd8554972006-06-26 20:58:46 -03002696static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2697{
2698 switch (tp) {
2699 case pvr2_ctl_int: return "integer";
2700 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002701 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002702 case pvr2_ctl_bitmask: return "bitmask";
2703 }
2704 return "";
2705}
2706
2707
Mike Isely681c7392007-11-26 01:48:52 -03002708/* Figure out if we need to commit control changes. If so, mark internal
2709 state flags to indicate this fact and return true. Otherwise do nothing
2710 else and return false. */
2711static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002712{
Mike Iselyd8554972006-06-26 20:58:46 -03002713 unsigned int idx;
2714 struct pvr2_ctrl *cptr;
2715 int value;
2716 int commit_flag = 0;
2717 char buf[100];
2718 unsigned int bcnt,ccnt;
2719
Mike Iselyc05c0462006-06-25 20:04:25 -03002720 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002721 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002722 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002723 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002724 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002725
Mike Iselyfe23a282007-01-20 00:10:55 -03002726 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002727 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2728 cptr->info->name);
2729 value = 0;
2730 cptr->info->get_value(cptr,&value);
2731 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2732 buf+bcnt,
2733 sizeof(buf)-bcnt,&ccnt);
2734 bcnt += ccnt;
2735 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2736 get_ctrl_typename(cptr->info->type));
2737 pvr2_trace(PVR2_TRACE_CTL,
2738 "/*--TRACE_COMMIT--*/ %.*s",
2739 bcnt,buf);
2740 }
2741
2742 if (!commit_flag) {
2743 /* Nothing has changed */
2744 return 0;
2745 }
2746
Mike Isely681c7392007-11-26 01:48:52 -03002747 hdw->state_pipeline_config = 0;
2748 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2749 pvr2_hdw_state_sched(hdw);
2750
2751 return !0;
2752}
2753
2754
2755/* Perform all operations needed to commit all control changes. This must
2756 be performed in synchronization with the pipeline state and is thus
2757 expected to be called as part of the driver's worker thread. Return
2758 true if commit successful, otherwise return false to indicate that
2759 commit isn't possible at this time. */
2760static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2761{
2762 unsigned int idx;
2763 struct pvr2_ctrl *cptr;
2764 int disruptive_change;
2765
Mike Iselyab062fe2008-06-30 03:32:35 -03002766 /* Handle some required side effects when the video standard is
2767 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03002768 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03002769 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03002770 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03002771 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2772 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03002773 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03002774 } else {
2775 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03002776 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03002777 }
Mike Isely00528d92008-06-30 03:35:52 -03002778 /* Rewrite the vertical resolution to be appropriate to the
2779 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03002780 if (nvres != hdw->res_ver_val) {
2781 hdw->res_ver_val = nvres;
2782 hdw->res_ver_dirty = !0;
2783 }
Mike Isely00528d92008-06-30 03:35:52 -03002784 /* Rewrite the GOP size to be appropriate to the video
2785 standard that has been selected. */
2786 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
2787 struct v4l2_ext_controls cs;
2788 struct v4l2_ext_control c1;
2789 memset(&cs, 0, sizeof(cs));
2790 memset(&c1, 0, sizeof(c1));
2791 cs.controls = &c1;
2792 cs.count = 1;
2793 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
2794 c1.value = gop_size;
2795 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
2796 VIDIOC_S_EXT_CTRLS);
2797 }
Mike Iselyd8554972006-06-26 20:58:46 -03002798 }
2799
Mike Isely38d9a2c2008-03-28 05:30:48 -03002800 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03002801 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2802 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2803 hdw->pathway_state)) {
2804 /* Change of mode being asked for... */
2805 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03002806 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03002807 }
2808 if (!hdw->state_pathway_ok) {
2809 /* Can't commit anything until pathway is ok. */
2810 return 0;
2811 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03002812 /* The broadcast decoder can only scale down, so if
2813 * res_*_dirty && crop window < output format ==> enlarge crop.
2814 *
2815 * The mpeg encoder receives fields of res_hor_val dots and
2816 * res_ver_val halflines. Limits: hor<=720, ver<=576.
2817 */
2818 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
2819 hdw->cropw_val = hdw->res_hor_val;
2820 hdw->cropw_dirty = !0;
2821 } else if (hdw->cropw_dirty) {
2822 hdw->res_hor_dirty = !0; /* must rescale */
2823 hdw->res_hor_val = min(720, hdw->cropw_val);
2824 }
2825 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
2826 hdw->croph_val = hdw->res_ver_val;
2827 hdw->croph_dirty = !0;
2828 } else if (hdw->croph_dirty) {
2829 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
2830 hdw->res_ver_dirty = !0;
2831 hdw->res_ver_val = min(nvres, hdw->croph_val);
2832 }
2833
Mike Isely681c7392007-11-26 01:48:52 -03002834 /* If any of the below has changed, then we can't do the update
2835 while the pipeline is running. Pipeline must be paused first
2836 and decoder -> encoder connection be made quiescent before we
2837 can proceed. */
2838 disruptive_change =
2839 (hdw->std_dirty ||
2840 hdw->enc_unsafe_stale ||
2841 hdw->srate_dirty ||
2842 hdw->res_ver_dirty ||
2843 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03002844 hdw->cropw_dirty ||
2845 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03002846 hdw->input_dirty ||
2847 (hdw->active_stream_type != hdw->desired_stream_type));
2848 if (disruptive_change && !hdw->state_pipeline_idle) {
2849 /* Pipeline is not idle; we can't proceed. Arrange to
2850 cause pipeline to stop so that we can try this again
2851 later.... */
2852 hdw->state_pipeline_pause = !0;
2853 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002854 }
2855
Mike Iselyb30d2442006-06-25 20:05:01 -03002856 if (hdw->srate_dirty) {
2857 /* Write new sample rate into control structure since
2858 * the master copy is stale. We must track srate
2859 * separate from the mpeg control structure because
2860 * other logic also uses this value. */
2861 struct v4l2_ext_controls cs;
2862 struct v4l2_ext_control c1;
2863 memset(&cs,0,sizeof(cs));
2864 memset(&c1,0,sizeof(c1));
2865 cs.controls = &c1;
2866 cs.count = 1;
2867 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2868 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002869 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002870 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002871
Mike Iselyd8554972006-06-26 20:58:46 -03002872 /* Scan i2c core at this point - before we clear all the dirty
2873 bits. Various parts of the i2c core will notice dirty bits as
2874 appropriate and arrange to broadcast or directly send updates to
2875 the client drivers in order to keep everything in sync */
2876 pvr2_i2c_core_check_stale(hdw);
2877
Mike Iselyc05c0462006-06-25 20:04:25 -03002878 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002879 cptr = hdw->controls + idx;
2880 if (!cptr->info->clear_dirty) continue;
2881 cptr->info->clear_dirty(cptr);
2882 }
2883
Mike Isely681c7392007-11-26 01:48:52 -03002884 if (hdw->active_stream_type != hdw->desired_stream_type) {
2885 /* Handle any side effects of stream config here */
2886 hdw->active_stream_type = hdw->desired_stream_type;
2887 }
2888
Mike Isely1df59f02008-04-21 03:50:39 -03002889 if (hdw->hdw_desc->signal_routing_scheme ==
2890 PVR2_ROUTING_SCHEME_GOTVIEW) {
2891 u32 b;
2892 /* Handle GOTVIEW audio switching */
2893 pvr2_hdw_gpio_get_out(hdw,&b);
2894 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2895 /* Set GPIO 11 */
2896 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
2897 } else {
2898 /* Clear GPIO 11 */
2899 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
2900 }
2901 }
2902
Mike Iselyd8554972006-06-26 20:58:46 -03002903 /* Now execute i2c core update */
2904 pvr2_i2c_core_sync(hdw);
2905
Mike Isely62433e32008-04-22 14:45:40 -03002906 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2907 hdw->state_encoder_run) {
2908 /* If encoder isn't running or it can't be touched, then
2909 this will get worked out later when we start the
2910 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002911 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2912 }
Mike Iselyd8554972006-06-26 20:58:46 -03002913
Mike Isely681c7392007-11-26 01:48:52 -03002914 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03002915 /* Hardware state may have changed in a way to cause the cropping
2916 capabilities to have changed. So mark it stale, which will
2917 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03002918 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2919 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002920}
2921
2922
2923int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2924{
Mike Isely681c7392007-11-26 01:48:52 -03002925 int fl;
2926 LOCK_TAKE(hdw->big_lock);
2927 fl = pvr2_hdw_commit_setup(hdw);
2928 LOCK_GIVE(hdw->big_lock);
2929 if (!fl) return 0;
2930 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002931}
2932
2933
Mike Isely681c7392007-11-26 01:48:52 -03002934static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002935{
Mike Isely681c7392007-11-26 01:48:52 -03002936 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002937 LOCK_TAKE(hdw->big_lock); do {
2938 pvr2_i2c_core_sync(hdw);
2939 } while (0); LOCK_GIVE(hdw->big_lock);
2940}
2941
2942
Mike Isely681c7392007-11-26 01:48:52 -03002943static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002944{
Mike Isely681c7392007-11-26 01:48:52 -03002945 int fl = 0;
2946 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002947 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002948 fl = pvr2_hdw_state_eval(hdw);
2949 } while (0); LOCK_GIVE(hdw->big_lock);
2950 if (fl && hdw->state_func) {
2951 hdw->state_func(hdw->state_data);
2952 }
2953}
2954
2955
Mike Isely681c7392007-11-26 01:48:52 -03002956static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002957{
Mike Isely681c7392007-11-26 01:48:52 -03002958 return wait_event_interruptible(
2959 hdw->state_wait_data,
2960 (hdw->state_stale == 0) &&
2961 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002962}
2963
Mike Isely681c7392007-11-26 01:48:52 -03002964
Mike Iselyd8554972006-06-26 20:58:46 -03002965/* Return name for this driver instance */
2966const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2967{
2968 return hdw->name;
2969}
2970
2971
Mike Isely78a47102007-11-26 01:58:20 -03002972const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2973{
2974 return hdw->hdw_desc->description;
2975}
2976
2977
2978const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2979{
2980 return hdw->hdw_desc->shortname;
2981}
2982
2983
Mike Iselyd8554972006-06-26 20:58:46 -03002984int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2985{
2986 int result;
2987 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002988 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002989 result = pvr2_send_request(hdw,
2990 hdw->cmd_buffer,1,
2991 hdw->cmd_buffer,1);
2992 if (result < 0) break;
2993 result = (hdw->cmd_buffer[0] != 0);
2994 } while(0); LOCK_GIVE(hdw->ctl_lock);
2995 return result;
2996}
2997
2998
Mike Isely18103c572007-01-20 00:09:47 -03002999/* Execute poll of tuner status */
3000void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003001{
Mike Iselyd8554972006-06-26 20:58:46 -03003002 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03003003 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003004 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003005}
3006
3007
Mike Isely432907f2008-08-31 21:02:20 -03003008static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3009{
3010 if (!hdw->cropcap_stale) {
3011 /* Keep checkpatch.pl quiet */
3012 return 0;
3013 }
3014 pvr2_i2c_core_status_poll(hdw);
3015 if (hdw->cropcap_stale) {
3016 /* Keep checkpatch.pl quiet */
3017 return -EIO;
3018 }
3019 return 0;
3020}
3021
3022
3023/* Return information about cropping capabilities */
3024int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3025{
3026 int stat = 0;
3027 LOCK_TAKE(hdw->big_lock);
3028 stat = pvr2_hdw_check_cropcap(hdw);
3029 if (!stat) {
3030 /* Keep checkpatch.pl quiet */
3031 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3032 }
3033 LOCK_GIVE(hdw->big_lock);
3034 return stat;
3035}
3036
3037
Mike Isely18103c572007-01-20 00:09:47 -03003038/* Return information about the tuner */
3039int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3040{
3041 LOCK_TAKE(hdw->big_lock); do {
3042 if (hdw->tuner_signal_stale) {
3043 pvr2_i2c_core_status_poll(hdw);
3044 }
3045 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3046 } while (0); LOCK_GIVE(hdw->big_lock);
3047 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003048}
3049
3050
3051/* Get handle to video output stream */
3052struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3053{
3054 return hp->vid_stream;
3055}
3056
3057
3058void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3059{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003060 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003061 LOCK_TAKE(hdw->big_lock); do {
3062 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03003063 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003064 pvr2_i2c_core_check_stale(hdw);
3065 hdw->log_requested = 0;
3066 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03003067 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003068 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003069 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003070 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003071 } while (0); LOCK_GIVE(hdw->big_lock);
3072}
3073
Mike Isely4db666c2007-09-08 22:16:27 -03003074
3075/* Grab EEPROM contents, needed for direct method. */
3076#define EEPROM_SIZE 8192
3077#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3078static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3079{
3080 struct i2c_msg msg[2];
3081 u8 *eeprom;
3082 u8 iadd[2];
3083 u8 addr;
3084 u16 eepromSize;
3085 unsigned int offs;
3086 int ret;
3087 int mode16 = 0;
3088 unsigned pcnt,tcnt;
3089 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3090 if (!eeprom) {
3091 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3092 "Failed to allocate memory"
3093 " required to read eeprom");
3094 return NULL;
3095 }
3096
3097 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3098 hdw->eeprom_addr);
3099 addr = hdw->eeprom_addr;
3100 /* Seems that if the high bit is set, then the *real* eeprom
3101 address is shifted right now bit position (noticed this in
3102 newer PVR USB2 hardware) */
3103 if (addr & 0x80) addr >>= 1;
3104
3105 /* FX2 documentation states that a 16bit-addressed eeprom is
3106 expected if the I2C address is an odd number (yeah, this is
3107 strange but it's what they do) */
3108 mode16 = (addr & 1);
3109 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3110 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3111 " using %d bit addressing",eepromSize,addr,
3112 mode16 ? 16 : 8);
3113
3114 msg[0].addr = addr;
3115 msg[0].flags = 0;
3116 msg[0].len = mode16 ? 2 : 1;
3117 msg[0].buf = iadd;
3118 msg[1].addr = addr;
3119 msg[1].flags = I2C_M_RD;
3120
3121 /* We have to do the actual eeprom data fetch ourselves, because
3122 (1) we're only fetching part of the eeprom, and (2) if we were
3123 getting the whole thing our I2C driver can't grab it in one
3124 pass - which is what tveeprom is otherwise going to attempt */
3125 memset(eeprom,0,EEPROM_SIZE);
3126 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3127 pcnt = 16;
3128 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3129 offs = tcnt + (eepromSize - EEPROM_SIZE);
3130 if (mode16) {
3131 iadd[0] = offs >> 8;
3132 iadd[1] = offs;
3133 } else {
3134 iadd[0] = offs;
3135 }
3136 msg[1].len = pcnt;
3137 msg[1].buf = eeprom+tcnt;
3138 if ((ret = i2c_transfer(&hdw->i2c_adap,
3139 msg,ARRAY_SIZE(msg))) != 2) {
3140 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3141 "eeprom fetch set offs err=%d",ret);
3142 kfree(eeprom);
3143 return NULL;
3144 }
3145 }
3146 return eeprom;
3147}
3148
3149
3150void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3151 int prom_flag,
3152 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003153{
3154 int ret;
3155 u16 address;
3156 unsigned int pipe;
3157 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003158 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003159
3160 if (!enable_flag) {
3161 pvr2_trace(PVR2_TRACE_FIRMWARE,
3162 "Cleaning up after CPU firmware fetch");
3163 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003164 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003165 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003166 if (hdw->fw_cpu_flag) {
3167 /* Now release the CPU. It will disconnect
3168 and reconnect later. */
3169 pvr2_hdw_cpureset_assert(hdw,0);
3170 }
Mike Iselyd8554972006-06-26 20:58:46 -03003171 break;
3172 }
3173
Mike Isely4db666c2007-09-08 22:16:27 -03003174 hdw->fw_cpu_flag = (prom_flag == 0);
3175 if (hdw->fw_cpu_flag) {
3176 pvr2_trace(PVR2_TRACE_FIRMWARE,
3177 "Preparing to suck out CPU firmware");
3178 hdw->fw_size = 0x2000;
3179 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3180 if (!hdw->fw_buffer) {
3181 hdw->fw_size = 0;
3182 break;
3183 }
3184
3185 /* We have to hold the CPU during firmware upload. */
3186 pvr2_hdw_cpureset_assert(hdw,1);
3187
3188 /* download the firmware from address 0000-1fff in 2048
3189 (=0x800) bytes chunk. */
3190
3191 pvr2_trace(PVR2_TRACE_FIRMWARE,
3192 "Grabbing CPU firmware");
3193 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3194 for(address = 0; address < hdw->fw_size;
3195 address += 0x800) {
3196 ret = usb_control_msg(hdw->usb_dev,pipe,
3197 0xa0,0xc0,
3198 address,0,
3199 hdw->fw_buffer+address,
3200 0x800,HZ);
3201 if (ret < 0) break;
3202 }
3203
3204 pvr2_trace(PVR2_TRACE_FIRMWARE,
3205 "Done grabbing CPU firmware");
3206 } else {
3207 pvr2_trace(PVR2_TRACE_FIRMWARE,
3208 "Sucking down EEPROM contents");
3209 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3210 if (!hdw->fw_buffer) {
3211 pvr2_trace(PVR2_TRACE_FIRMWARE,
3212 "EEPROM content suck failed.");
3213 break;
3214 }
3215 hdw->fw_size = EEPROM_SIZE;
3216 pvr2_trace(PVR2_TRACE_FIRMWARE,
3217 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003218 }
3219
Mike Iselyd8554972006-06-26 20:58:46 -03003220 } while (0); LOCK_GIVE(hdw->big_lock);
3221}
3222
3223
3224/* Return true if we're in a mode for retrieval CPU firmware */
3225int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3226{
Al Viro5fa12472008-03-29 03:07:38 +00003227 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003228}
3229
3230
3231int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3232 char *buf,unsigned int cnt)
3233{
3234 int ret = -EINVAL;
3235 LOCK_TAKE(hdw->big_lock); do {
3236 if (!buf) break;
3237 if (!cnt) break;
3238
3239 if (!hdw->fw_buffer) {
3240 ret = -EIO;
3241 break;
3242 }
3243
3244 if (offs >= hdw->fw_size) {
3245 pvr2_trace(PVR2_TRACE_FIRMWARE,
3246 "Read firmware data offs=%d EOF",
3247 offs);
3248 ret = 0;
3249 break;
3250 }
3251
3252 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3253
3254 memcpy(buf,hdw->fw_buffer+offs,cnt);
3255
3256 pvr2_trace(PVR2_TRACE_FIRMWARE,
3257 "Read firmware data offs=%d cnt=%d",
3258 offs,cnt);
3259 ret = cnt;
3260 } while (0); LOCK_GIVE(hdw->big_lock);
3261
3262 return ret;
3263}
3264
3265
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003266int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003267 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003268{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003269 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003270 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3271 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3272 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003273 default: return -1;
3274 }
Mike Iselyd8554972006-06-26 20:58:46 -03003275}
3276
3277
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003278/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003279void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003280 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003281{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003282 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003283 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3284 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3285 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003286 default: break;
3287 }
Mike Iselyd8554972006-06-26 20:58:46 -03003288}
3289
3290
David Howells7d12e782006-10-05 14:55:46 +01003291static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003292{
3293 struct pvr2_hdw *hdw = urb->context;
3294 hdw->ctl_write_pend_flag = 0;
3295 if (hdw->ctl_read_pend_flag) return;
3296 complete(&hdw->ctl_done);
3297}
3298
3299
David Howells7d12e782006-10-05 14:55:46 +01003300static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003301{
3302 struct pvr2_hdw *hdw = urb->context;
3303 hdw->ctl_read_pend_flag = 0;
3304 if (hdw->ctl_write_pend_flag) return;
3305 complete(&hdw->ctl_done);
3306}
3307
3308
3309static void pvr2_ctl_timeout(unsigned long data)
3310{
3311 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3312 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3313 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003314 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003315 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003316 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003317 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003318 }
3319}
3320
3321
Mike Iselye61b6fc2006-07-18 22:42:18 -03003322/* Issue a command and get a response from the device. This extended
3323 version includes a probe flag (which if set means that device errors
3324 should not be logged or treated as fatal) and a timeout in jiffies.
3325 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003326static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3327 unsigned int timeout,int probe_fl,
3328 void *write_data,unsigned int write_len,
3329 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003330{
3331 unsigned int idx;
3332 int status = 0;
3333 struct timer_list timer;
3334 if (!hdw->ctl_lock_held) {
3335 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3336 "Attempted to execute control transfer"
3337 " without lock!!");
3338 return -EDEADLK;
3339 }
Mike Isely681c7392007-11-26 01:48:52 -03003340 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003341 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3342 "Attempted to execute control transfer"
3343 " when device not ok");
3344 return -EIO;
3345 }
3346 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3347 if (!probe_fl) {
3348 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3349 "Attempted to execute control transfer"
3350 " when USB is disconnected");
3351 }
3352 return -ENOTTY;
3353 }
3354
3355 /* Ensure that we have sane parameters */
3356 if (!write_data) write_len = 0;
3357 if (!read_data) read_len = 0;
3358 if (write_len > PVR2_CTL_BUFFSIZE) {
3359 pvr2_trace(
3360 PVR2_TRACE_ERROR_LEGS,
3361 "Attempted to execute %d byte"
3362 " control-write transfer (limit=%d)",
3363 write_len,PVR2_CTL_BUFFSIZE);
3364 return -EINVAL;
3365 }
3366 if (read_len > PVR2_CTL_BUFFSIZE) {
3367 pvr2_trace(
3368 PVR2_TRACE_ERROR_LEGS,
3369 "Attempted to execute %d byte"
3370 " control-read transfer (limit=%d)",
3371 write_len,PVR2_CTL_BUFFSIZE);
3372 return -EINVAL;
3373 }
3374 if ((!write_len) && (!read_len)) {
3375 pvr2_trace(
3376 PVR2_TRACE_ERROR_LEGS,
3377 "Attempted to execute null control transfer?");
3378 return -EINVAL;
3379 }
3380
3381
3382 hdw->cmd_debug_state = 1;
3383 if (write_len) {
3384 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3385 } else {
3386 hdw->cmd_debug_code = 0;
3387 }
3388 hdw->cmd_debug_write_len = write_len;
3389 hdw->cmd_debug_read_len = read_len;
3390
3391 /* Initialize common stuff */
3392 init_completion(&hdw->ctl_done);
3393 hdw->ctl_timeout_flag = 0;
3394 hdw->ctl_write_pend_flag = 0;
3395 hdw->ctl_read_pend_flag = 0;
3396 init_timer(&timer);
3397 timer.expires = jiffies + timeout;
3398 timer.data = (unsigned long)hdw;
3399 timer.function = pvr2_ctl_timeout;
3400
3401 if (write_len) {
3402 hdw->cmd_debug_state = 2;
3403 /* Transfer write data to internal buffer */
3404 for (idx = 0; idx < write_len; idx++) {
3405 hdw->ctl_write_buffer[idx] =
3406 ((unsigned char *)write_data)[idx];
3407 }
3408 /* Initiate a write request */
3409 usb_fill_bulk_urb(hdw->ctl_write_urb,
3410 hdw->usb_dev,
3411 usb_sndbulkpipe(hdw->usb_dev,
3412 PVR2_CTL_WRITE_ENDPOINT),
3413 hdw->ctl_write_buffer,
3414 write_len,
3415 pvr2_ctl_write_complete,
3416 hdw);
3417 hdw->ctl_write_urb->actual_length = 0;
3418 hdw->ctl_write_pend_flag = !0;
3419 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3420 if (status < 0) {
3421 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3422 "Failed to submit write-control"
3423 " URB status=%d",status);
3424 hdw->ctl_write_pend_flag = 0;
3425 goto done;
3426 }
3427 }
3428
3429 if (read_len) {
3430 hdw->cmd_debug_state = 3;
3431 memset(hdw->ctl_read_buffer,0x43,read_len);
3432 /* Initiate a read request */
3433 usb_fill_bulk_urb(hdw->ctl_read_urb,
3434 hdw->usb_dev,
3435 usb_rcvbulkpipe(hdw->usb_dev,
3436 PVR2_CTL_READ_ENDPOINT),
3437 hdw->ctl_read_buffer,
3438 read_len,
3439 pvr2_ctl_read_complete,
3440 hdw);
3441 hdw->ctl_read_urb->actual_length = 0;
3442 hdw->ctl_read_pend_flag = !0;
3443 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3444 if (status < 0) {
3445 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3446 "Failed to submit read-control"
3447 " URB status=%d",status);
3448 hdw->ctl_read_pend_flag = 0;
3449 goto done;
3450 }
3451 }
3452
3453 /* Start timer */
3454 add_timer(&timer);
3455
3456 /* Now wait for all I/O to complete */
3457 hdw->cmd_debug_state = 4;
3458 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3459 wait_for_completion(&hdw->ctl_done);
3460 }
3461 hdw->cmd_debug_state = 5;
3462
3463 /* Stop timer */
3464 del_timer_sync(&timer);
3465
3466 hdw->cmd_debug_state = 6;
3467 status = 0;
3468
3469 if (hdw->ctl_timeout_flag) {
3470 status = -ETIMEDOUT;
3471 if (!probe_fl) {
3472 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3473 "Timed out control-write");
3474 }
3475 goto done;
3476 }
3477
3478 if (write_len) {
3479 /* Validate results of write request */
3480 if ((hdw->ctl_write_urb->status != 0) &&
3481 (hdw->ctl_write_urb->status != -ENOENT) &&
3482 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3483 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3484 /* USB subsystem is reporting some kind of failure
3485 on the write */
3486 status = hdw->ctl_write_urb->status;
3487 if (!probe_fl) {
3488 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3489 "control-write URB failure,"
3490 " status=%d",
3491 status);
3492 }
3493 goto done;
3494 }
3495 if (hdw->ctl_write_urb->actual_length < write_len) {
3496 /* Failed to write enough data */
3497 status = -EIO;
3498 if (!probe_fl) {
3499 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3500 "control-write URB short,"
3501 " expected=%d got=%d",
3502 write_len,
3503 hdw->ctl_write_urb->actual_length);
3504 }
3505 goto done;
3506 }
3507 }
3508 if (read_len) {
3509 /* Validate results of read request */
3510 if ((hdw->ctl_read_urb->status != 0) &&
3511 (hdw->ctl_read_urb->status != -ENOENT) &&
3512 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3513 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3514 /* USB subsystem is reporting some kind of failure
3515 on the read */
3516 status = hdw->ctl_read_urb->status;
3517 if (!probe_fl) {
3518 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3519 "control-read URB failure,"
3520 " status=%d",
3521 status);
3522 }
3523 goto done;
3524 }
3525 if (hdw->ctl_read_urb->actual_length < read_len) {
3526 /* Failed to read enough data */
3527 status = -EIO;
3528 if (!probe_fl) {
3529 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3530 "control-read URB short,"
3531 " expected=%d got=%d",
3532 read_len,
3533 hdw->ctl_read_urb->actual_length);
3534 }
3535 goto done;
3536 }
3537 /* Transfer retrieved data out from internal buffer */
3538 for (idx = 0; idx < read_len; idx++) {
3539 ((unsigned char *)read_data)[idx] =
3540 hdw->ctl_read_buffer[idx];
3541 }
3542 }
3543
3544 done:
3545
3546 hdw->cmd_debug_state = 0;
3547 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003548 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003549 }
3550 return status;
3551}
3552
3553
3554int pvr2_send_request(struct pvr2_hdw *hdw,
3555 void *write_data,unsigned int write_len,
3556 void *read_data,unsigned int read_len)
3557{
3558 return pvr2_send_request_ex(hdw,HZ*4,0,
3559 write_data,write_len,
3560 read_data,read_len);
3561}
3562
Mike Isely1c9d10d2008-03-28 05:38:54 -03003563
3564static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3565{
3566 int ret;
3567 unsigned int cnt = 1;
3568 unsigned int args = 0;
3569 LOCK_TAKE(hdw->ctl_lock);
3570 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3571 args = (cmdcode >> 8) & 0xffu;
3572 args = (args > 2) ? 2 : args;
3573 if (args) {
3574 cnt += args;
3575 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3576 if (args > 1) {
3577 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3578 }
3579 }
3580 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3581 unsigned int idx;
3582 unsigned int ccnt,bcnt;
3583 char tbuf[50];
3584 cmdcode &= 0xffu;
3585 bcnt = 0;
3586 ccnt = scnprintf(tbuf+bcnt,
3587 sizeof(tbuf)-bcnt,
3588 "Sending FX2 command 0x%x",cmdcode);
3589 bcnt += ccnt;
3590 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3591 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3592 ccnt = scnprintf(tbuf+bcnt,
3593 sizeof(tbuf)-bcnt,
3594 " \"%s\"",
3595 pvr2_fx2cmd_desc[idx].desc);
3596 bcnt += ccnt;
3597 break;
3598 }
3599 }
3600 if (args) {
3601 ccnt = scnprintf(tbuf+bcnt,
3602 sizeof(tbuf)-bcnt,
3603 " (%u",hdw->cmd_buffer[1]);
3604 bcnt += ccnt;
3605 if (args > 1) {
3606 ccnt = scnprintf(tbuf+bcnt,
3607 sizeof(tbuf)-bcnt,
3608 ",%u",hdw->cmd_buffer[2]);
3609 bcnt += ccnt;
3610 }
3611 ccnt = scnprintf(tbuf+bcnt,
3612 sizeof(tbuf)-bcnt,
3613 ")");
3614 bcnt += ccnt;
3615 }
3616 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3617 }
3618 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3619 LOCK_GIVE(hdw->ctl_lock);
3620 return ret;
3621}
3622
3623
Mike Iselyd8554972006-06-26 20:58:46 -03003624int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3625{
3626 int ret;
3627
3628 LOCK_TAKE(hdw->ctl_lock);
3629
Michael Krufky8d364362007-01-22 02:17:55 -03003630 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003631 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3632 hdw->cmd_buffer[5] = 0;
3633 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3634 hdw->cmd_buffer[7] = reg & 0xff;
3635
3636
3637 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3638
3639 LOCK_GIVE(hdw->ctl_lock);
3640
3641 return ret;
3642}
3643
3644
Adrian Bunk07e337e2006-06-30 11:30:20 -03003645static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003646{
3647 int ret = 0;
3648
3649 LOCK_TAKE(hdw->ctl_lock);
3650
Michael Krufky8d364362007-01-22 02:17:55 -03003651 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003652 hdw->cmd_buffer[1] = 0;
3653 hdw->cmd_buffer[2] = 0;
3654 hdw->cmd_buffer[3] = 0;
3655 hdw->cmd_buffer[4] = 0;
3656 hdw->cmd_buffer[5] = 0;
3657 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3658 hdw->cmd_buffer[7] = reg & 0xff;
3659
3660 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3661 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3662
3663 LOCK_GIVE(hdw->ctl_lock);
3664
3665 return ret;
3666}
3667
3668
Mike Isely681c7392007-11-26 01:48:52 -03003669void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003670{
3671 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003672 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3673 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003674 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003675 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003676 }
Mike Isely681c7392007-11-26 01:48:52 -03003677 hdw->flag_ok = 0;
3678 trace_stbit("flag_ok",hdw->flag_ok);
3679 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003680}
3681
3682
3683void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3684{
3685 int ret;
3686 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003687 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003688 if (ret == 1) {
3689 ret = usb_reset_device(hdw->usb_dev);
3690 usb_unlock_device(hdw->usb_dev);
3691 } else {
3692 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3693 "Failed to lock USB device ret=%d",ret);
3694 }
3695 if (init_pause_msec) {
3696 pvr2_trace(PVR2_TRACE_INFO,
3697 "Waiting %u msec for hardware to settle",
3698 init_pause_msec);
3699 msleep(init_pause_msec);
3700 }
3701
3702}
3703
3704
3705void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3706{
3707 char da[1];
3708 unsigned int pipe;
3709 int ret;
3710
3711 if (!hdw->usb_dev) return;
3712
3713 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3714
3715 da[0] = val ? 0x01 : 0x00;
3716
3717 /* Write the CPUCS register on the 8051. The lsb of the register
3718 is the reset bit; a 1 asserts reset while a 0 clears it. */
3719 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3720 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3721 if (ret < 0) {
3722 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3723 "cpureset_assert(%d) error=%d",val,ret);
3724 pvr2_hdw_render_useless(hdw);
3725 }
3726}
3727
3728
3729int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3730{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003731 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03003732}
3733
3734
Michael Krufkye1edb192008-04-22 14:45:39 -03003735int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3736{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003737 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03003738}
3739
Mike Isely1c9d10d2008-03-28 05:38:54 -03003740
Michael Krufkye1edb192008-04-22 14:45:39 -03003741int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3742{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003743 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03003744}
3745
Mike Iselyd8554972006-06-26 20:58:46 -03003746
3747int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3748{
3749 if (!hdw->decoder_ctrl) {
3750 pvr2_trace(PVR2_TRACE_INIT,
3751 "Unable to reset decoder: nothing attached");
3752 return -ENOTTY;
3753 }
3754
3755 if (!hdw->decoder_ctrl->force_reset) {
3756 pvr2_trace(PVR2_TRACE_INIT,
3757 "Unable to reset decoder: not implemented");
3758 return -ENOTTY;
3759 }
3760
3761 pvr2_trace(PVR2_TRACE_INIT,
3762 "Requesting decoder reset");
3763 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3764 return 0;
3765}
3766
3767
Mike Isely62433e32008-04-22 14:45:40 -03003768static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003769{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003770 hdw->flag_ok = !0;
3771 return pvr2_issue_simple_cmd(hdw,
3772 FX2CMD_HCW_DEMOD_RESETIN |
3773 (1 << 8) |
3774 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03003775}
3776
Mike Isely84147f32008-04-22 14:45:40 -03003777
Mike Isely62433e32008-04-22 14:45:40 -03003778static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003779{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003780 hdw->flag_ok = !0;
3781 return pvr2_issue_simple_cmd(hdw,(onoff ?
3782 FX2CMD_ONAIR_DTV_POWER_ON :
3783 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003784}
3785
Mike Isely62433e32008-04-22 14:45:40 -03003786
3787static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3788 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003789{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003790 return pvr2_issue_simple_cmd(hdw,(onoff ?
3791 FX2CMD_ONAIR_DTV_STREAMING_ON :
3792 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003793}
3794
Mike Isely62433e32008-04-22 14:45:40 -03003795
3796static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3797{
3798 int cmode;
3799 /* Compare digital/analog desired setting with current setting. If
3800 they don't match, fix it... */
3801 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3802 if (cmode == hdw->pathway_state) {
3803 /* They match; nothing to do */
3804 return;
3805 }
3806
3807 switch (hdw->hdw_desc->digital_control_scheme) {
3808 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3809 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3810 if (cmode == PVR2_PATHWAY_ANALOG) {
3811 /* If moving to analog mode, also force the decoder
3812 to reset. If no decoder is attached, then it's
3813 ok to ignore this because if/when the decoder
3814 attaches, it will reset itself at that time. */
3815 pvr2_hdw_cmd_decoder_reset(hdw);
3816 }
3817 break;
3818 case PVR2_DIGITAL_SCHEME_ONAIR:
3819 /* Supposedly we should always have the power on whether in
3820 digital or analog mode. But for now do what appears to
3821 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003822 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03003823 break;
3824 default: break;
3825 }
3826
Mike Isely1b9c18c2008-04-22 14:45:41 -03003827 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03003828 hdw->pathway_state = cmode;
3829}
3830
3831
Adrian Bunke9b59f62008-05-10 04:35:24 -03003832static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03003833{
3834 /* change some GPIO data
3835 *
3836 * note: bit d7 of dir appears to control the LED,
3837 * so we shut it off here.
3838 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03003839 */
Mike Isely40381cb2008-04-22 14:45:42 -03003840 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003841 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03003842 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003843 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03003844 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03003845 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03003846}
Mike Iselyc55a97d2008-04-22 14:45:41 -03003847
Mike Isely40381cb2008-04-22 14:45:42 -03003848
3849typedef void (*led_method_func)(struct pvr2_hdw *,int);
3850
3851static led_method_func led_methods[] = {
3852 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
3853};
3854
3855
3856/* Toggle LED */
3857static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
3858{
3859 unsigned int scheme_id;
3860 led_method_func fp;
3861
3862 if ((!onoff) == (!hdw->led_on)) return;
3863
3864 hdw->led_on = onoff != 0;
3865
3866 scheme_id = hdw->hdw_desc->led_scheme;
3867 if (scheme_id < ARRAY_SIZE(led_methods)) {
3868 fp = led_methods[scheme_id];
3869 } else {
3870 fp = NULL;
3871 }
3872
3873 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03003874}
3875
3876
Mike Iselye61b6fc2006-07-18 22:42:18 -03003877/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003878static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003879{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003880 int ret;
3881
3882 /* If we're in analog mode, then just issue the usual analog
3883 command. */
3884 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3885 return pvr2_issue_simple_cmd(hdw,
3886 (runFl ?
3887 FX2CMD_STREAMING_ON :
3888 FX2CMD_STREAMING_OFF));
3889 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03003890 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003891
3892 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
3893 /* Whoops, we don't know what mode we're in... */
3894 return -EINVAL;
3895 }
3896
3897 /* To get here we have to be in digital mode. The mechanism here
3898 is unfortunately different for different vendors. So we switch
3899 on the device's digital scheme attribute in order to figure out
3900 what to do. */
3901 switch (hdw->hdw_desc->digital_control_scheme) {
3902 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3903 return pvr2_issue_simple_cmd(hdw,
3904 (runFl ?
3905 FX2CMD_HCW_DTV_STREAMING_ON :
3906 FX2CMD_HCW_DTV_STREAMING_OFF));
3907 case PVR2_DIGITAL_SCHEME_ONAIR:
3908 ret = pvr2_issue_simple_cmd(hdw,
3909 (runFl ?
3910 FX2CMD_STREAMING_ON :
3911 FX2CMD_STREAMING_OFF));
3912 if (ret) return ret;
3913 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
3914 default:
3915 return -EINVAL;
3916 }
Mike Iselyd8554972006-06-26 20:58:46 -03003917}
3918
3919
Mike Isely62433e32008-04-22 14:45:40 -03003920/* Evaluate whether or not state_pathway_ok can change */
3921static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3922{
3923 if (hdw->state_pathway_ok) {
3924 /* Nothing to do if pathway is already ok */
3925 return 0;
3926 }
3927 if (!hdw->state_pipeline_idle) {
3928 /* Not allowed to change anything if pipeline is not idle */
3929 return 0;
3930 }
3931 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3932 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003933 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003934 return !0;
3935}
3936
3937
Mike Isely681c7392007-11-26 01:48:52 -03003938/* Evaluate whether or not state_encoder_ok can change */
3939static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3940{
3941 if (hdw->state_encoder_ok) return 0;
3942 if (hdw->flag_tripped) return 0;
3943 if (hdw->state_encoder_run) return 0;
3944 if (hdw->state_encoder_config) return 0;
3945 if (hdw->state_decoder_run) return 0;
3946 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003947 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
3948 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
3949 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
3950 return 0;
3951 }
3952
Mike Isely681c7392007-11-26 01:48:52 -03003953 if (pvr2_upload_firmware2(hdw) < 0) {
3954 hdw->flag_tripped = !0;
3955 trace_stbit("flag_tripped",hdw->flag_tripped);
3956 return !0;
3957 }
3958 hdw->state_encoder_ok = !0;
3959 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3960 return !0;
3961}
3962
3963
3964/* Evaluate whether or not state_encoder_config can change */
3965static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3966{
3967 if (hdw->state_encoder_config) {
3968 if (hdw->state_encoder_ok) {
3969 if (hdw->state_pipeline_req &&
3970 !hdw->state_pipeline_pause) return 0;
3971 }
3972 hdw->state_encoder_config = 0;
3973 hdw->state_encoder_waitok = 0;
3974 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3975 /* paranoia - solve race if timer just completed */
3976 del_timer_sync(&hdw->encoder_wait_timer);
3977 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003978 if (!hdw->state_pathway_ok ||
3979 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3980 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003981 !hdw->state_pipeline_idle ||
3982 hdw->state_pipeline_pause ||
3983 !hdw->state_pipeline_req ||
3984 !hdw->state_pipeline_config) {
3985 /* We must reset the enforced wait interval if
3986 anything has happened that might have disturbed
3987 the encoder. This should be a rare case. */
3988 if (timer_pending(&hdw->encoder_wait_timer)) {
3989 del_timer_sync(&hdw->encoder_wait_timer);
3990 }
3991 if (hdw->state_encoder_waitok) {
3992 /* Must clear the state - therefore we did
3993 something to a state bit and must also
3994 return true. */
3995 hdw->state_encoder_waitok = 0;
3996 trace_stbit("state_encoder_waitok",
3997 hdw->state_encoder_waitok);
3998 return !0;
3999 }
4000 return 0;
4001 }
4002 if (!hdw->state_encoder_waitok) {
4003 if (!timer_pending(&hdw->encoder_wait_timer)) {
4004 /* waitok flag wasn't set and timer isn't
4005 running. Check flag once more to avoid
4006 a race then start the timer. This is
4007 the point when we measure out a minimal
4008 quiet interval before doing something to
4009 the encoder. */
4010 if (!hdw->state_encoder_waitok) {
4011 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004012 jiffies +
4013 (HZ * TIME_MSEC_ENCODER_WAIT
4014 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004015 add_timer(&hdw->encoder_wait_timer);
4016 }
4017 }
4018 /* We can't continue until we know we have been
4019 quiet for the interval measured by this
4020 timer. */
4021 return 0;
4022 }
4023 pvr2_encoder_configure(hdw);
4024 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4025 }
4026 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4027 return !0;
4028}
4029
4030
Mike Iselyd913d632008-04-06 04:04:35 -03004031/* Return true if the encoder should not be running. */
4032static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4033{
4034 if (!hdw->state_encoder_ok) {
4035 /* Encoder isn't healthy at the moment, so stop it. */
4036 return !0;
4037 }
4038 if (!hdw->state_pathway_ok) {
4039 /* Mode is not understood at the moment (i.e. it wants to
4040 change), so encoder must be stopped. */
4041 return !0;
4042 }
4043
4044 switch (hdw->pathway_state) {
4045 case PVR2_PATHWAY_ANALOG:
4046 if (!hdw->state_decoder_run) {
4047 /* We're in analog mode and the decoder is not
4048 running; thus the encoder should be stopped as
4049 well. */
4050 return !0;
4051 }
4052 break;
4053 case PVR2_PATHWAY_DIGITAL:
4054 if (hdw->state_encoder_runok) {
4055 /* This is a funny case. We're in digital mode so
4056 really the encoder should be stopped. However
4057 if it really is running, only kill it after
4058 runok has been set. This gives a chance for the
4059 onair quirk to function (encoder must run
4060 briefly first, at least once, before onair
4061 digital streaming can work). */
4062 return !0;
4063 }
4064 break;
4065 default:
4066 /* Unknown mode; so encoder should be stopped. */
4067 return !0;
4068 }
4069
4070 /* If we get here, we haven't found a reason to stop the
4071 encoder. */
4072 return 0;
4073}
4074
4075
4076/* Return true if the encoder should be running. */
4077static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4078{
4079 if (!hdw->state_encoder_ok) {
4080 /* Don't run the encoder if it isn't healthy... */
4081 return 0;
4082 }
4083 if (!hdw->state_pathway_ok) {
4084 /* Don't run the encoder if we don't (yet) know what mode
4085 we need to be in... */
4086 return 0;
4087 }
4088
4089 switch (hdw->pathway_state) {
4090 case PVR2_PATHWAY_ANALOG:
4091 if (hdw->state_decoder_run) {
4092 /* In analog mode, if the decoder is running, then
4093 run the encoder. */
4094 return !0;
4095 }
4096 break;
4097 case PVR2_PATHWAY_DIGITAL:
4098 if ((hdw->hdw_desc->digital_control_scheme ==
4099 PVR2_DIGITAL_SCHEME_ONAIR) &&
4100 !hdw->state_encoder_runok) {
4101 /* This is a quirk. OnAir hardware won't stream
4102 digital until the encoder has been run at least
4103 once, for a minimal period of time (empiricially
4104 measured to be 1/4 second). So if we're on
4105 OnAir hardware and the encoder has never been
4106 run at all, then start the encoder. Normal
4107 state machine logic in the driver will
4108 automatically handle the remaining bits. */
4109 return !0;
4110 }
4111 break;
4112 default:
4113 /* For completeness (unknown mode; encoder won't run ever) */
4114 break;
4115 }
4116 /* If we get here, then we haven't found any reason to run the
4117 encoder, so don't run it. */
4118 return 0;
4119}
4120
4121
Mike Isely681c7392007-11-26 01:48:52 -03004122/* Evaluate whether or not state_encoder_run can change */
4123static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4124{
4125 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004126 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004127 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004128 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004129 if (pvr2_encoder_stop(hdw) < 0) return !0;
4130 }
4131 hdw->state_encoder_run = 0;
4132 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004133 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004134 if (pvr2_encoder_start(hdw) < 0) return !0;
4135 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004136 if (!hdw->state_encoder_runok) {
4137 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004138 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004139 add_timer(&hdw->encoder_run_timer);
4140 }
Mike Isely681c7392007-11-26 01:48:52 -03004141 }
4142 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4143 return !0;
4144}
4145
4146
4147/* Timeout function for quiescent timer. */
4148static void pvr2_hdw_quiescent_timeout(unsigned long data)
4149{
4150 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4151 hdw->state_decoder_quiescent = !0;
4152 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4153 hdw->state_stale = !0;
4154 queue_work(hdw->workqueue,&hdw->workpoll);
4155}
4156
4157
4158/* Timeout function for encoder wait timer. */
4159static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4160{
4161 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4162 hdw->state_encoder_waitok = !0;
4163 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4164 hdw->state_stale = !0;
4165 queue_work(hdw->workqueue,&hdw->workpoll);
4166}
4167
4168
Mike Iselyd913d632008-04-06 04:04:35 -03004169/* Timeout function for encoder run timer. */
4170static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4171{
4172 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4173 if (!hdw->state_encoder_runok) {
4174 hdw->state_encoder_runok = !0;
4175 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4176 hdw->state_stale = !0;
4177 queue_work(hdw->workqueue,&hdw->workpoll);
4178 }
4179}
4180
4181
Mike Isely681c7392007-11-26 01:48:52 -03004182/* Evaluate whether or not state_decoder_run can change */
4183static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4184{
4185 if (hdw->state_decoder_run) {
4186 if (hdw->state_encoder_ok) {
4187 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004188 !hdw->state_pipeline_pause &&
4189 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004190 }
4191 if (!hdw->flag_decoder_missed) {
4192 pvr2_decoder_enable(hdw,0);
4193 }
4194 hdw->state_decoder_quiescent = 0;
4195 hdw->state_decoder_run = 0;
4196 /* paranoia - solve race if timer just completed */
4197 del_timer_sync(&hdw->quiescent_timer);
4198 } else {
4199 if (!hdw->state_decoder_quiescent) {
4200 if (!timer_pending(&hdw->quiescent_timer)) {
4201 /* We don't do something about the
4202 quiescent timer until right here because
4203 we also want to catch cases where the
4204 decoder was already not running (like
4205 after initialization) as opposed to
4206 knowing that we had just stopped it.
4207 The second flag check is here to cover a
4208 race - the timer could have run and set
4209 this flag just after the previous check
4210 but before we did the pending check. */
4211 if (!hdw->state_decoder_quiescent) {
4212 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004213 jiffies +
4214 (HZ * TIME_MSEC_DECODER_WAIT
4215 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004216 add_timer(&hdw->quiescent_timer);
4217 }
4218 }
4219 /* Don't allow decoder to start again until it has
4220 been quiesced first. This little detail should
4221 hopefully further stabilize the encoder. */
4222 return 0;
4223 }
Mike Isely62433e32008-04-22 14:45:40 -03004224 if (!hdw->state_pathway_ok ||
4225 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4226 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004227 hdw->state_pipeline_pause ||
4228 !hdw->state_pipeline_config ||
4229 !hdw->state_encoder_config ||
4230 !hdw->state_encoder_ok) return 0;
4231 del_timer_sync(&hdw->quiescent_timer);
4232 if (hdw->flag_decoder_missed) return 0;
4233 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4234 hdw->state_decoder_quiescent = 0;
4235 hdw->state_decoder_run = !0;
4236 }
4237 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4238 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4239 return !0;
4240}
4241
4242
4243/* Evaluate whether or not state_usbstream_run can change */
4244static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4245{
4246 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004247 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004248 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004249 fl = (hdw->state_encoder_ok &&
4250 hdw->state_encoder_run);
4251 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4252 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4253 fl = hdw->state_encoder_ok;
4254 }
4255 if (fl &&
4256 hdw->state_pipeline_req &&
4257 !hdw->state_pipeline_pause &&
4258 hdw->state_pathway_ok) {
4259 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004260 }
4261 pvr2_hdw_cmd_usbstream(hdw,0);
4262 hdw->state_usbstream_run = 0;
4263 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004264 if (!hdw->state_pipeline_req ||
4265 hdw->state_pipeline_pause ||
4266 !hdw->state_pathway_ok) return 0;
4267 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4268 if (!hdw->state_encoder_ok ||
4269 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004270 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4271 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4272 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004273 if (hdw->state_encoder_run) return 0;
4274 if (hdw->hdw_desc->digital_control_scheme ==
4275 PVR2_DIGITAL_SCHEME_ONAIR) {
4276 /* OnAir digital receivers won't stream
4277 unless the analog encoder has run first.
4278 Why? I have no idea. But don't even
4279 try until we know the analog side is
4280 known to have run. */
4281 if (!hdw->state_encoder_runok) return 0;
4282 }
Mike Isely62433e32008-04-22 14:45:40 -03004283 }
Mike Isely681c7392007-11-26 01:48:52 -03004284 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4285 hdw->state_usbstream_run = !0;
4286 }
4287 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4288 return !0;
4289}
4290
4291
4292/* Attempt to configure pipeline, if needed */
4293static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4294{
4295 if (hdw->state_pipeline_config ||
4296 hdw->state_pipeline_pause) return 0;
4297 pvr2_hdw_commit_execute(hdw);
4298 return !0;
4299}
4300
4301
4302/* Update pipeline idle and pipeline pause tracking states based on other
4303 inputs. This must be called whenever the other relevant inputs have
4304 changed. */
4305static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4306{
4307 unsigned int st;
4308 int updatedFl = 0;
4309 /* Update pipeline state */
4310 st = !(hdw->state_encoder_run ||
4311 hdw->state_decoder_run ||
4312 hdw->state_usbstream_run ||
4313 (!hdw->state_decoder_quiescent));
4314 if (!st != !hdw->state_pipeline_idle) {
4315 hdw->state_pipeline_idle = st;
4316 updatedFl = !0;
4317 }
4318 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4319 hdw->state_pipeline_pause = 0;
4320 updatedFl = !0;
4321 }
4322 return updatedFl;
4323}
4324
4325
4326typedef int (*state_eval_func)(struct pvr2_hdw *);
4327
4328/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004329static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004330 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004331 state_eval_pipeline_config,
4332 state_eval_encoder_ok,
4333 state_eval_encoder_config,
4334 state_eval_decoder_run,
4335 state_eval_encoder_run,
4336 state_eval_usbstream_run,
4337};
4338
4339
4340/* Process various states and return true if we did anything interesting. */
4341static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4342{
4343 unsigned int i;
4344 int state_updated = 0;
4345 int check_flag;
4346
4347 if (!hdw->state_stale) return 0;
4348 if ((hdw->fw1_state != FW1_STATE_OK) ||
4349 !hdw->flag_ok) {
4350 hdw->state_stale = 0;
4351 return !0;
4352 }
4353 /* This loop is the heart of the entire driver. It keeps trying to
4354 evaluate various bits of driver state until nothing changes for
4355 one full iteration. Each "bit of state" tracks some global
4356 aspect of the driver, e.g. whether decoder should run, if
4357 pipeline is configured, usb streaming is on, etc. We separately
4358 evaluate each of those questions based on other driver state to
4359 arrive at the correct running configuration. */
4360 do {
4361 check_flag = 0;
4362 state_update_pipeline_state(hdw);
4363 /* Iterate over each bit of state */
4364 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4365 if ((*eval_funcs[i])(hdw)) {
4366 check_flag = !0;
4367 state_updated = !0;
4368 state_update_pipeline_state(hdw);
4369 }
4370 }
4371 } while (check_flag && hdw->flag_ok);
4372 hdw->state_stale = 0;
4373 trace_stbit("state_stale",hdw->state_stale);
4374 return state_updated;
4375}
4376
4377
Mike Isely1cb03b72008-04-21 03:47:43 -03004378static unsigned int print_input_mask(unsigned int msk,
4379 char *buf,unsigned int acnt)
4380{
4381 unsigned int idx,ccnt;
4382 unsigned int tcnt = 0;
4383 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4384 if (!((1 << idx) & msk)) continue;
4385 ccnt = scnprintf(buf+tcnt,
4386 acnt-tcnt,
4387 "%s%s",
4388 (tcnt ? ", " : ""),
4389 control_values_input[idx]);
4390 tcnt += ccnt;
4391 }
4392 return tcnt;
4393}
4394
4395
Mike Isely62433e32008-04-22 14:45:40 -03004396static const char *pvr2_pathway_state_name(int id)
4397{
4398 switch (id) {
4399 case PVR2_PATHWAY_ANALOG: return "analog";
4400 case PVR2_PATHWAY_DIGITAL: return "digital";
4401 default: return "unknown";
4402 }
4403}
4404
4405
Mike Isely681c7392007-11-26 01:48:52 -03004406static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4407 char *buf,unsigned int acnt)
4408{
4409 switch (which) {
4410 case 0:
4411 return scnprintf(
4412 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004413 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004414 (hdw->flag_ok ? " <ok>" : " <fail>"),
4415 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4416 (hdw->flag_disconnected ? " <disconnected>" :
4417 " <connected>"),
4418 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004419 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4420 pvr2_pathway_state_name(hdw->pathway_state));
4421
Mike Isely681c7392007-11-26 01:48:52 -03004422 case 1:
4423 return scnprintf(
4424 buf,acnt,
4425 "pipeline:%s%s%s%s",
4426 (hdw->state_pipeline_idle ? " <idle>" : ""),
4427 (hdw->state_pipeline_config ?
4428 " <configok>" : " <stale>"),
4429 (hdw->state_pipeline_req ? " <req>" : ""),
4430 (hdw->state_pipeline_pause ? " <pause>" : ""));
4431 case 2:
4432 return scnprintf(
4433 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004434 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004435 (hdw->state_decoder_run ?
4436 " <decode:run>" :
4437 (hdw->state_decoder_quiescent ?
4438 "" : " <decode:stop>")),
4439 (hdw->state_decoder_quiescent ?
4440 " <decode:quiescent>" : ""),
4441 (hdw->state_encoder_ok ?
4442 "" : " <encode:init>"),
4443 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004444 (hdw->state_encoder_runok ?
4445 " <encode:run>" :
4446 " <encode:firstrun>") :
4447 (hdw->state_encoder_runok ?
4448 " <encode:stop>" :
4449 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004450 (hdw->state_encoder_config ?
4451 " <encode:configok>" :
4452 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004453 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004454 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004455 " <usb:run>" : " <usb:stop>"),
4456 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004457 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004458 case 3:
4459 return scnprintf(
4460 buf,acnt,
4461 "state: %s",
4462 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004463 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004464 unsigned int tcnt = 0;
4465 unsigned int ccnt;
4466
4467 ccnt = scnprintf(buf,
4468 acnt,
4469 "Hardware supported inputs: ");
4470 tcnt += ccnt;
4471 tcnt += print_input_mask(hdw->input_avail_mask,
4472 buf+tcnt,
4473 acnt-tcnt);
4474 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4475 ccnt = scnprintf(buf+tcnt,
4476 acnt-tcnt,
4477 "; allowed inputs: ");
4478 tcnt += ccnt;
4479 tcnt += print_input_mask(hdw->input_allowed_mask,
4480 buf+tcnt,
4481 acnt-tcnt);
4482 }
4483 return tcnt;
4484 }
4485 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004486 struct pvr2_stream_stats stats;
4487 if (!hdw->vid_stream) break;
4488 pvr2_stream_get_stats(hdw->vid_stream,
4489 &stats,
4490 0);
4491 return scnprintf(
4492 buf,acnt,
4493 "Bytes streamed=%u"
4494 " URBs: queued=%u idle=%u ready=%u"
4495 " processed=%u failed=%u",
4496 stats.bytes_processed,
4497 stats.buffers_in_queue,
4498 stats.buffers_in_idle,
4499 stats.buffers_in_ready,
4500 stats.buffers_processed,
4501 stats.buffers_failed);
4502 }
Mike Isely681c7392007-11-26 01:48:52 -03004503 default: break;
4504 }
4505 return 0;
4506}
4507
4508
4509unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4510 char *buf,unsigned int acnt)
4511{
4512 unsigned int bcnt,ccnt,idx;
4513 bcnt = 0;
4514 LOCK_TAKE(hdw->big_lock);
4515 for (idx = 0; ; idx++) {
4516 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4517 if (!ccnt) break;
4518 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4519 if (!acnt) break;
4520 buf[0] = '\n'; ccnt = 1;
4521 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4522 }
4523 LOCK_GIVE(hdw->big_lock);
4524 return bcnt;
4525}
4526
4527
4528static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4529{
4530 char buf[128];
4531 unsigned int idx,ccnt;
4532
4533 for (idx = 0; ; idx++) {
4534 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4535 if (!ccnt) break;
4536 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4537 }
4538}
4539
4540
4541/* Evaluate and update the driver's current state, taking various actions
4542 as appropriate for the update. */
4543static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4544{
4545 unsigned int st;
4546 int state_updated = 0;
4547 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004548 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004549
4550 pvr2_trace(PVR2_TRACE_STBITS,
4551 "Drive state check START");
4552 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4553 pvr2_hdw_state_log_state(hdw);
4554 }
4555
4556 /* Process all state and get back over disposition */
4557 state_updated = pvr2_hdw_state_update(hdw);
4558
Mike Isely1b9c18c2008-04-22 14:45:41 -03004559 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4560
Mike Isely681c7392007-11-26 01:48:52 -03004561 /* Update master state based upon all other states. */
4562 if (!hdw->flag_ok) {
4563 st = PVR2_STATE_DEAD;
4564 } else if (hdw->fw1_state != FW1_STATE_OK) {
4565 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004566 } else if ((analog_mode ||
4567 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4568 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004569 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004570 } else if (hdw->flag_tripped ||
4571 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004572 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004573 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004574 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004575 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004576 st = PVR2_STATE_RUN;
4577 } else {
4578 st = PVR2_STATE_READY;
4579 }
4580 if (hdw->master_state != st) {
4581 pvr2_trace(PVR2_TRACE_STATE,
4582 "Device state change from %s to %s",
4583 pvr2_get_state_name(hdw->master_state),
4584 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004585 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004586 hdw->master_state = st;
4587 state_updated = !0;
4588 callback_flag = !0;
4589 }
4590 if (state_updated) {
4591 /* Trigger anyone waiting on any state changes here. */
4592 wake_up(&hdw->state_wait_data);
4593 }
4594
4595 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4596 pvr2_hdw_state_log_state(hdw);
4597 }
4598 pvr2_trace(PVR2_TRACE_STBITS,
4599 "Drive state check DONE callback=%d",callback_flag);
4600
4601 return callback_flag;
4602}
4603
4604
4605/* Cause kernel thread to check / update driver state */
4606static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4607{
4608 if (hdw->state_stale) return;
4609 hdw->state_stale = !0;
4610 trace_stbit("state_stale",hdw->state_stale);
4611 queue_work(hdw->workqueue,&hdw->workpoll);
4612}
4613
4614
Mike Iselyd8554972006-06-26 20:58:46 -03004615int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4616{
4617 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4618}
4619
4620
4621int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4622{
4623 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4624}
4625
4626
4627int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4628{
4629 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4630}
4631
4632
4633int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4634{
4635 u32 cval,nval;
4636 int ret;
4637 if (~msk) {
4638 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4639 if (ret) return ret;
4640 nval = (cval & ~msk) | (val & msk);
4641 pvr2_trace(PVR2_TRACE_GPIO,
4642 "GPIO direction changing 0x%x:0x%x"
4643 " from 0x%x to 0x%x",
4644 msk,val,cval,nval);
4645 } else {
4646 nval = val;
4647 pvr2_trace(PVR2_TRACE_GPIO,
4648 "GPIO direction changing to 0x%x",nval);
4649 }
4650 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4651}
4652
4653
4654int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4655{
4656 u32 cval,nval;
4657 int ret;
4658 if (~msk) {
4659 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4660 if (ret) return ret;
4661 nval = (cval & ~msk) | (val & msk);
4662 pvr2_trace(PVR2_TRACE_GPIO,
4663 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4664 msk,val,cval,nval);
4665 } else {
4666 nval = val;
4667 pvr2_trace(PVR2_TRACE_GPIO,
4668 "GPIO output changing to 0x%x",nval);
4669 }
4670 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4671}
4672
4673
Mike Isely7fb20fa2008-04-22 14:45:37 -03004674unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
4675{
4676 return hdw->input_avail_mask;
4677}
4678
4679
Mike Isely1cb03b72008-04-21 03:47:43 -03004680unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
4681{
4682 return hdw->input_allowed_mask;
4683}
4684
4685
4686static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
4687{
4688 if (hdw->input_val != v) {
4689 hdw->input_val = v;
4690 hdw->input_dirty = !0;
4691 }
4692
4693 /* Handle side effects - if we switch to a mode that needs the RF
4694 tuner, then select the right frequency choice as well and mark
4695 it dirty. */
4696 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
4697 hdw->freqSelector = 0;
4698 hdw->freqDirty = !0;
4699 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
4700 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
4701 hdw->freqSelector = 1;
4702 hdw->freqDirty = !0;
4703 }
4704 return 0;
4705}
4706
4707
4708int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
4709 unsigned int change_mask,
4710 unsigned int change_val)
4711{
4712 int ret = 0;
4713 unsigned int nv,m,idx;
4714 LOCK_TAKE(hdw->big_lock);
4715 do {
4716 nv = hdw->input_allowed_mask & ~change_mask;
4717 nv |= (change_val & change_mask);
4718 nv &= hdw->input_avail_mask;
4719 if (!nv) {
4720 /* No legal modes left; return error instead. */
4721 ret = -EPERM;
4722 break;
4723 }
4724 hdw->input_allowed_mask = nv;
4725 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
4726 /* Current mode is still in the allowed mask, so
4727 we're done. */
4728 break;
4729 }
4730 /* Select and switch to a mode that is still in the allowed
4731 mask */
4732 if (!hdw->input_allowed_mask) {
4733 /* Nothing legal; give up */
4734 break;
4735 }
4736 m = hdw->input_allowed_mask;
4737 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
4738 if (!((1 << idx) & m)) continue;
4739 pvr2_hdw_set_input(hdw,idx);
4740 break;
4741 }
4742 } while (0);
4743 LOCK_GIVE(hdw->big_lock);
4744 return ret;
4745}
4746
4747
Mike Iselye61b6fc2006-07-18 22:42:18 -03004748/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004749static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004750{
4751 int result;
4752 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03004753 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03004754 result = pvr2_send_request(hdw,
4755 hdw->cmd_buffer,1,
4756 hdw->cmd_buffer,1);
4757 if (result < 0) break;
4758 result = hdw->cmd_buffer[0];
4759 } while(0); LOCK_GIVE(hdw->ctl_lock);
4760 return result;
4761}
4762
4763
Mike Isely32ffa9a2006-09-23 22:26:52 -03004764int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004765 u32 match_type, u32 match_chip, u64 reg_id,
4766 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004767{
4768#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004769 struct pvr2_i2c_client *cp;
4770 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004771 int stat = 0;
4772 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004773
Mike Isely201f5c92007-01-28 16:08:36 -03004774 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4775
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004776 req.match_type = match_type;
4777 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004778 req.reg = reg_id;
4779 if (setFl) req.val = *val_ptr;
4780 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004781 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004782 if (!v4l2_chip_match_i2c_client(
4783 cp->client,
4784 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004785 continue;
4786 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004787 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004788 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4789 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004790 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004791 okFl = !0;
4792 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004793 }
4794 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004795 if (okFl) {
4796 return stat;
4797 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004798 return -EINVAL;
4799#else
4800 return -ENOSYS;
4801#endif
4802}
4803
4804
Mike Iselyd8554972006-06-26 20:58:46 -03004805/*
4806 Stuff for Emacs to see, in order to encourage consistent editing style:
4807 *** Local Variables: ***
4808 *** mode: c ***
4809 *** fill-column: 75 ***
4810 *** tab-width: 8 ***
4811 *** c-basic-offset: 8 ***
4812 *** End: ***
4813 */