blob: 6517ba81bd4b868158641a9589a4279e7821d60e [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"},
253 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
254 {FX2CMD_REG_WRITE, "write encoder register"},
255 {FX2CMD_REG_READ, "read encoder register"},
256 {FX2CMD_MEMSEL, "encoder memsel"},
257 {FX2CMD_I2C_WRITE, "i2c write"},
258 {FX2CMD_I2C_READ, "i2c read"},
259 {FX2CMD_GET_USB_SPEED, "get USB speed"},
260 {FX2CMD_STREAMING_ON, "stream on"},
261 {FX2CMD_STREAMING_OFF, "stream off"},
262 {FX2CMD_FWPOST1, "fwpost1"},
263 {FX2CMD_POWER_OFF, "power off"},
264 {FX2CMD_POWER_ON, "power on"},
265 {FX2CMD_DEEP_RESET, "deep reset"},
266 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
267 {FX2CMD_GET_IR_CODE, "get IR code"},
268 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
269 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
270 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
271 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
272 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
273 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
274 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
275};
276
277
Mike Isely1cb03b72008-04-21 03:47:43 -0300278static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300279static void pvr2_hdw_state_sched(struct pvr2_hdw *);
280static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300281static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300282static void pvr2_hdw_worker_i2c(struct work_struct *work);
283static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300284static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
285static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
286static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300287static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300288static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300289static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300290static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
291static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300292static void pvr2_hdw_quiescent_timeout(unsigned long);
293static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300294static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300295static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300296static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
297 unsigned int timeout,int probe_fl,
298 void *write_data,unsigned int write_len,
299 void *read_data,unsigned int read_len);
Mike Iselyd8554972006-06-26 20:58:46 -0300300
Mike Isely681c7392007-11-26 01:48:52 -0300301
302static void trace_stbit(const char *name,int val)
303{
304 pvr2_trace(PVR2_TRACE_STBITS,
305 "State bit %s <-- %s",
306 name,(val ? "true" : "false"));
307}
308
Mike Iselyd8554972006-06-26 20:58:46 -0300309static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
310{
311 struct pvr2_hdw *hdw = cptr->hdw;
312 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
313 *vp = hdw->freqTable[hdw->freqProgSlot-1];
314 } else {
315 *vp = 0;
316 }
317 return 0;
318}
319
320static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
321{
322 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300323 unsigned int slotId = hdw->freqProgSlot;
324 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
325 hdw->freqTable[slotId-1] = v;
326 /* Handle side effects correctly - if we're tuned to this
327 slot, then forgot the slot id relation since the stored
328 frequency has been changed. */
329 if (hdw->freqSelector) {
330 if (hdw->freqSlotRadio == slotId) {
331 hdw->freqSlotRadio = 0;
332 }
333 } else {
334 if (hdw->freqSlotTelevision == slotId) {
335 hdw->freqSlotTelevision = 0;
336 }
337 }
Mike Iselyd8554972006-06-26 20:58:46 -0300338 }
339 return 0;
340}
341
342static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
343{
344 *vp = cptr->hdw->freqProgSlot;
345 return 0;
346}
347
348static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
349{
350 struct pvr2_hdw *hdw = cptr->hdw;
351 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
352 hdw->freqProgSlot = v;
353 }
354 return 0;
355}
356
357static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
358{
Mike Isely1bde0282006-12-27 23:30:13 -0300359 struct pvr2_hdw *hdw = cptr->hdw;
360 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300361 return 0;
362}
363
Mike Isely1bde0282006-12-27 23:30:13 -0300364static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300365{
366 unsigned freq = 0;
367 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300368 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
369 if (slotId > 0) {
370 freq = hdw->freqTable[slotId-1];
371 if (!freq) return 0;
372 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300373 }
Mike Isely1bde0282006-12-27 23:30:13 -0300374 if (hdw->freqSelector) {
375 hdw->freqSlotRadio = slotId;
376 } else {
377 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300378 }
379 return 0;
380}
381
382static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
383{
Mike Isely1bde0282006-12-27 23:30:13 -0300384 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300385 return 0;
386}
387
388static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
389{
390 return cptr->hdw->freqDirty != 0;
391}
392
393static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
394{
395 cptr->hdw->freqDirty = 0;
396}
397
398static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
399{
Mike Isely1bde0282006-12-27 23:30:13 -0300400 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300401 return 0;
402}
403
Mike Isely3ad9fc32006-09-02 22:37:52 -0300404static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
405{
406 /* Actual maximum depends on the video standard in effect. */
407 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
408 *vp = 480;
409 } else {
410 *vp = 576;
411 }
412 return 0;
413}
414
415static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
416{
Mike Isely989eb152007-11-26 01:53:12 -0300417 /* Actual minimum depends on device digitizer type. */
418 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300419 *vp = 75;
420 } else {
421 *vp = 17;
422 }
423 return 0;
424}
425
Mike Isely1bde0282006-12-27 23:30:13 -0300426static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
427{
428 *vp = cptr->hdw->input_val;
429 return 0;
430}
431
Mike Isely29bf5b12008-04-22 14:45:37 -0300432static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
433{
Mike Isely1cb03b72008-04-21 03:47:43 -0300434 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300435}
436
Mike Isely1bde0282006-12-27 23:30:13 -0300437static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
438{
Mike Isely1cb03b72008-04-21 03:47:43 -0300439 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300440}
441
442static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
443{
444 return cptr->hdw->input_dirty != 0;
445}
446
447static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
448{
449 cptr->hdw->input_dirty = 0;
450}
451
Mike Isely5549f542006-12-27 23:28:54 -0300452
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300453static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
454{
Mike Isely644afdb2007-01-20 00:19:23 -0300455 unsigned long fv;
456 struct pvr2_hdw *hdw = cptr->hdw;
457 if (hdw->tuner_signal_stale) {
458 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300459 }
Mike Isely644afdb2007-01-20 00:19:23 -0300460 fv = hdw->tuner_signal_info.rangehigh;
461 if (!fv) {
462 /* Safety fallback */
463 *vp = TV_MAX_FREQ;
464 return 0;
465 }
466 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
467 fv = (fv * 125) / 2;
468 } else {
469 fv = fv * 62500;
470 }
471 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300472 return 0;
473}
474
475static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
476{
Mike Isely644afdb2007-01-20 00:19:23 -0300477 unsigned long fv;
478 struct pvr2_hdw *hdw = cptr->hdw;
479 if (hdw->tuner_signal_stale) {
480 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300481 }
Mike Isely644afdb2007-01-20 00:19:23 -0300482 fv = hdw->tuner_signal_info.rangelow;
483 if (!fv) {
484 /* Safety fallback */
485 *vp = TV_MIN_FREQ;
486 return 0;
487 }
488 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
489 fv = (fv * 125) / 2;
490 } else {
491 fv = fv * 62500;
492 }
493 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300494 return 0;
495}
496
Mike Iselyb30d2442006-06-25 20:05:01 -0300497static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
498{
499 return cptr->hdw->enc_stale != 0;
500}
501
502static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
503{
504 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300505 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300506}
507
508static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
509{
510 int ret;
511 struct v4l2_ext_controls cs;
512 struct v4l2_ext_control c1;
513 memset(&cs,0,sizeof(cs));
514 memset(&c1,0,sizeof(c1));
515 cs.controls = &c1;
516 cs.count = 1;
517 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300518 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300519 VIDIOC_G_EXT_CTRLS);
520 if (ret) return ret;
521 *vp = c1.value;
522 return 0;
523}
524
525static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
526{
527 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300528 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300529 struct v4l2_ext_controls cs;
530 struct v4l2_ext_control c1;
531 memset(&cs,0,sizeof(cs));
532 memset(&c1,0,sizeof(c1));
533 cs.controls = &c1;
534 cs.count = 1;
535 c1.id = cptr->info->v4l_id;
536 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300537 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
538 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300539 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300540 if (ret == -EBUSY) {
541 /* Oops. cx2341x is telling us it's not safe to change
542 this control while we're capturing. Make a note of this
543 fact so that the pipeline will be stopped the next time
544 controls are committed. Then go on ahead and store this
545 change anyway. */
546 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
547 0, &cs,
548 VIDIOC_S_EXT_CTRLS);
549 if (!ret) hdw->enc_unsafe_stale = !0;
550 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300551 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300552 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300553 return 0;
554}
555
556static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
557{
558 struct v4l2_queryctrl qctrl;
559 struct pvr2_ctl_info *info;
560 qctrl.id = cptr->info->v4l_id;
561 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
562 /* Strip out the const so we can adjust a function pointer. It's
563 OK to do this here because we know this is a dynamically created
564 control, so the underlying storage for the info pointer is (a)
565 private to us, and (b) not in read-only storage. Either we do
566 this or we significantly complicate the underlying control
567 implementation. */
568 info = (struct pvr2_ctl_info *)(cptr->info);
569 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
570 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300571 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300572 }
573 } else {
574 if (!(info->set_value)) {
575 info->set_value = ctrl_cx2341x_set;
576 }
577 }
578 return qctrl.flags;
579}
580
Mike Iselyd8554972006-06-26 20:58:46 -0300581static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
582{
Mike Isely681c7392007-11-26 01:48:52 -0300583 *vp = cptr->hdw->state_pipeline_req;
584 return 0;
585}
586
587static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
588{
589 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300590 return 0;
591}
592
593static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
594{
595 int result = pvr2_hdw_is_hsm(cptr->hdw);
596 *vp = PVR2_CVAL_HSM_FULL;
597 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
598 if (result) *vp = PVR2_CVAL_HSM_HIGH;
599 return 0;
600}
601
602static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
603{
604 *vp = cptr->hdw->std_mask_avail;
605 return 0;
606}
607
608static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
609{
610 struct pvr2_hdw *hdw = cptr->hdw;
611 v4l2_std_id ns;
612 ns = hdw->std_mask_avail;
613 ns = (ns & ~m) | (v & m);
614 if (ns == hdw->std_mask_avail) return 0;
615 hdw->std_mask_avail = ns;
616 pvr2_hdw_internal_set_std_avail(hdw);
617 pvr2_hdw_internal_find_stdenum(hdw);
618 return 0;
619}
620
621static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
622 char *bufPtr,unsigned int bufSize,
623 unsigned int *len)
624{
625 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
626 return 0;
627}
628
629static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
630 const char *bufPtr,unsigned int bufSize,
631 int *mskp,int *valp)
632{
633 int ret;
634 v4l2_std_id id;
635 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
636 if (ret < 0) return ret;
637 if (mskp) *mskp = id;
638 if (valp) *valp = id;
639 return 0;
640}
641
642static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
643{
644 *vp = cptr->hdw->std_mask_cur;
645 return 0;
646}
647
648static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
649{
650 struct pvr2_hdw *hdw = cptr->hdw;
651 v4l2_std_id ns;
652 ns = hdw->std_mask_cur;
653 ns = (ns & ~m) | (v & m);
654 if (ns == hdw->std_mask_cur) return 0;
655 hdw->std_mask_cur = ns;
656 hdw->std_dirty = !0;
657 pvr2_hdw_internal_find_stdenum(hdw);
658 return 0;
659}
660
661static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
662{
663 return cptr->hdw->std_dirty != 0;
664}
665
666static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
667{
668 cptr->hdw->std_dirty = 0;
669}
670
671static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
672{
Mike Isely18103c572007-01-20 00:09:47 -0300673 struct pvr2_hdw *hdw = cptr->hdw;
674 pvr2_i2c_core_status_poll(hdw);
675 *vp = hdw->tuner_signal_info.signal;
676 return 0;
677}
678
679static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
680{
681 int val = 0;
682 unsigned int subchan;
683 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300684 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300685 subchan = hdw->tuner_signal_info.rxsubchans;
686 if (subchan & V4L2_TUNER_SUB_MONO) {
687 val |= (1 << V4L2_TUNER_MODE_MONO);
688 }
689 if (subchan & V4L2_TUNER_SUB_STEREO) {
690 val |= (1 << V4L2_TUNER_MODE_STEREO);
691 }
692 if (subchan & V4L2_TUNER_SUB_LANG1) {
693 val |= (1 << V4L2_TUNER_MODE_LANG1);
694 }
695 if (subchan & V4L2_TUNER_SUB_LANG2) {
696 val |= (1 << V4L2_TUNER_MODE_LANG2);
697 }
698 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300699 return 0;
700}
701
Mike Iselyd8554972006-06-26 20:58:46 -0300702
703static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
704{
705 struct pvr2_hdw *hdw = cptr->hdw;
706 if (v < 0) return -EINVAL;
707 if (v > hdw->std_enum_cnt) return -EINVAL;
708 hdw->std_enum_cur = v;
709 if (!v) return 0;
710 v--;
711 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
712 hdw->std_mask_cur = hdw->std_defs[v].id;
713 hdw->std_dirty = !0;
714 return 0;
715}
716
717
718static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
719{
720 *vp = cptr->hdw->std_enum_cur;
721 return 0;
722}
723
724
725static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
726{
727 return cptr->hdw->std_dirty != 0;
728}
729
730
731static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
732{
733 cptr->hdw->std_dirty = 0;
734}
735
736
737#define DEFINT(vmin,vmax) \
738 .type = pvr2_ctl_int, \
739 .def.type_int.min_value = vmin, \
740 .def.type_int.max_value = vmax
741
742#define DEFENUM(tab) \
743 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300744 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300745 .def.type_enum.value_names = tab
746
Mike Isely33213962006-06-25 20:04:40 -0300747#define DEFBOOL \
748 .type = pvr2_ctl_bool
749
Mike Iselyd8554972006-06-26 20:58:46 -0300750#define DEFMASK(msk,tab) \
751 .type = pvr2_ctl_bitmask, \
752 .def.type_bitmask.valid_bits = msk, \
753 .def.type_bitmask.bit_names = tab
754
755#define DEFREF(vname) \
756 .set_value = ctrl_set_##vname, \
757 .get_value = ctrl_get_##vname, \
758 .is_dirty = ctrl_isdirty_##vname, \
759 .clear_dirty = ctrl_cleardirty_##vname
760
761
762#define VCREATE_FUNCS(vname) \
763static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
764{*vp = cptr->hdw->vname##_val; return 0;} \
765static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
766{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
767static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
768{return cptr->hdw->vname##_dirty != 0;} \
769static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
770{cptr->hdw->vname##_dirty = 0;}
771
772VCREATE_FUNCS(brightness)
773VCREATE_FUNCS(contrast)
774VCREATE_FUNCS(saturation)
775VCREATE_FUNCS(hue)
776VCREATE_FUNCS(volume)
777VCREATE_FUNCS(balance)
778VCREATE_FUNCS(bass)
779VCREATE_FUNCS(treble)
780VCREATE_FUNCS(mute)
Mike Iselyc05c0462006-06-25 20:04:25 -0300781VCREATE_FUNCS(audiomode)
782VCREATE_FUNCS(res_hor)
783VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300784VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300785
Mike Iselyd8554972006-06-26 20:58:46 -0300786/* Table definition of all controls which can be manipulated */
787static const struct pvr2_ctl_info control_defs[] = {
788 {
789 .v4l_id = V4L2_CID_BRIGHTNESS,
790 .desc = "Brightness",
791 .name = "brightness",
792 .default_value = 128,
793 DEFREF(brightness),
794 DEFINT(0,255),
795 },{
796 .v4l_id = V4L2_CID_CONTRAST,
797 .desc = "Contrast",
798 .name = "contrast",
799 .default_value = 68,
800 DEFREF(contrast),
801 DEFINT(0,127),
802 },{
803 .v4l_id = V4L2_CID_SATURATION,
804 .desc = "Saturation",
805 .name = "saturation",
806 .default_value = 64,
807 DEFREF(saturation),
808 DEFINT(0,127),
809 },{
810 .v4l_id = V4L2_CID_HUE,
811 .desc = "Hue",
812 .name = "hue",
813 .default_value = 0,
814 DEFREF(hue),
815 DEFINT(-128,127),
816 },{
817 .v4l_id = V4L2_CID_AUDIO_VOLUME,
818 .desc = "Volume",
819 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -0300820 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -0300821 DEFREF(volume),
822 DEFINT(0,65535),
823 },{
824 .v4l_id = V4L2_CID_AUDIO_BALANCE,
825 .desc = "Balance",
826 .name = "balance",
827 .default_value = 0,
828 DEFREF(balance),
829 DEFINT(-32768,32767),
830 },{
831 .v4l_id = V4L2_CID_AUDIO_BASS,
832 .desc = "Bass",
833 .name = "bass",
834 .default_value = 0,
835 DEFREF(bass),
836 DEFINT(-32768,32767),
837 },{
838 .v4l_id = V4L2_CID_AUDIO_TREBLE,
839 .desc = "Treble",
840 .name = "treble",
841 .default_value = 0,
842 DEFREF(treble),
843 DEFINT(-32768,32767),
844 },{
845 .v4l_id = V4L2_CID_AUDIO_MUTE,
846 .desc = "Mute",
847 .name = "mute",
848 .default_value = 0,
849 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -0300850 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300851 },{
Mike Iselyc05c0462006-06-25 20:04:25 -0300852 .desc = "Video Source",
853 .name = "input",
854 .internal_id = PVR2_CID_INPUT,
855 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -0300856 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -0300857 DEFREF(input),
858 DEFENUM(control_values_input),
859 },{
860 .desc = "Audio Mode",
861 .name = "audio_mode",
862 .internal_id = PVR2_CID_AUDIOMODE,
863 .default_value = V4L2_TUNER_MODE_STEREO,
864 DEFREF(audiomode),
865 DEFENUM(control_values_audiomode),
866 },{
867 .desc = "Horizontal capture resolution",
868 .name = "resolution_hor",
869 .internal_id = PVR2_CID_HRES,
870 .default_value = 720,
871 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300872 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -0300873 },{
874 .desc = "Vertical capture resolution",
875 .name = "resolution_ver",
876 .internal_id = PVR2_CID_VRES,
877 .default_value = 480,
878 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300879 DEFINT(17,576),
880 /* Hook in check for video standard and adjust maximum
881 depending on the standard. */
882 .get_max_value = ctrl_vres_max_get,
883 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -0300884 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300885 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -0300886 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
887 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -0300888 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -0300889 DEFREF(srate),
890 DEFENUM(control_values_srate),
891 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300892 .desc = "Tuner Frequency (Hz)",
893 .name = "frequency",
894 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -0300895 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -0300896 .set_value = ctrl_freq_set,
897 .get_value = ctrl_freq_get,
898 .is_dirty = ctrl_freq_is_dirty,
899 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -0300900 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300901 /* Hook in check for input value (tv/radio) and adjust
902 max/min values accordingly */
903 .get_max_value = ctrl_freq_max_get,
904 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300905 },{
906 .desc = "Channel",
907 .name = "channel",
908 .set_value = ctrl_channel_set,
909 .get_value = ctrl_channel_get,
910 DEFINT(0,FREQTABLE_SIZE),
911 },{
912 .desc = "Channel Program Frequency",
913 .name = "freq_table_value",
914 .set_value = ctrl_channelfreq_set,
915 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -0300916 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -0300917 /* Hook in check for input value (tv/radio) and adjust
918 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -0300919 .get_max_value = ctrl_freq_max_get,
920 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300921 },{
922 .desc = "Channel Program ID",
923 .name = "freq_table_channel",
924 .set_value = ctrl_channelprog_set,
925 .get_value = ctrl_channelprog_get,
926 DEFINT(0,FREQTABLE_SIZE),
927 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300928 .desc = "Streaming Enabled",
929 .name = "streaming_enabled",
930 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -0300931 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300932 },{
933 .desc = "USB Speed",
934 .name = "usb_speed",
935 .get_value = ctrl_hsm_get,
936 DEFENUM(control_values_hsm),
937 },{
Mike Isely681c7392007-11-26 01:48:52 -0300938 .desc = "Master State",
939 .name = "master_state",
940 .get_value = ctrl_masterstate_get,
941 DEFENUM(pvr2_state_names),
942 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300943 .desc = "Signal Present",
944 .name = "signal_present",
945 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -0300946 DEFINT(0,65535),
947 },{
948 .desc = "Audio Modes Present",
949 .name = "audio_modes_present",
950 .get_value = ctrl_audio_modes_present_get,
951 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
952 v4l. Nothing outside of this module cares about this,
953 but I reuse it in order to also reuse the
954 control_values_audiomode string table. */
955 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
956 (1 << V4L2_TUNER_MODE_STEREO)|
957 (1 << V4L2_TUNER_MODE_LANG1)|
958 (1 << V4L2_TUNER_MODE_LANG2)),
959 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -0300960 },{
961 .desc = "Video Standards Available Mask",
962 .name = "video_standard_mask_available",
963 .internal_id = PVR2_CID_STDAVAIL,
964 .skip_init = !0,
965 .get_value = ctrl_stdavail_get,
966 .set_value = ctrl_stdavail_set,
967 .val_to_sym = ctrl_std_val_to_sym,
968 .sym_to_val = ctrl_std_sym_to_val,
969 .type = pvr2_ctl_bitmask,
970 },{
971 .desc = "Video Standards In Use Mask",
972 .name = "video_standard_mask_active",
973 .internal_id = PVR2_CID_STDCUR,
974 .skip_init = !0,
975 .get_value = ctrl_stdcur_get,
976 .set_value = ctrl_stdcur_set,
977 .is_dirty = ctrl_stdcur_is_dirty,
978 .clear_dirty = ctrl_stdcur_clear_dirty,
979 .val_to_sym = ctrl_std_val_to_sym,
980 .sym_to_val = ctrl_std_sym_to_val,
981 .type = pvr2_ctl_bitmask,
982 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300983 .desc = "Video Standard Name",
984 .name = "video_standard",
985 .internal_id = PVR2_CID_STDENUM,
986 .skip_init = !0,
987 .get_value = ctrl_stdenumcur_get,
988 .set_value = ctrl_stdenumcur_set,
989 .is_dirty = ctrl_stdenumcur_is_dirty,
990 .clear_dirty = ctrl_stdenumcur_clear_dirty,
991 .type = pvr2_ctl_enum,
992 }
993};
994
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300995#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -0300996
997
998const char *pvr2_config_get_name(enum pvr2_config cfg)
999{
1000 switch (cfg) {
1001 case pvr2_config_empty: return "empty";
1002 case pvr2_config_mpeg: return "mpeg";
1003 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001004 case pvr2_config_pcm: return "pcm";
1005 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001006 }
1007 return "<unknown>";
1008}
1009
1010
1011struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1012{
1013 return hdw->usb_dev;
1014}
1015
1016
1017unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1018{
1019 return hdw->serial_number;
1020}
1021
Mike Isely31a18542007-04-08 01:11:47 -03001022
1023const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1024{
1025 return hdw->bus_info;
1026}
1027
1028
Mike Isely1bde0282006-12-27 23:30:13 -03001029unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1030{
1031 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1032}
1033
1034/* Set the currently tuned frequency and account for all possible
1035 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001036static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001037{
Mike Isely7c74e572007-01-20 00:15:41 -03001038 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001039 if (hdw->freqSelector) {
1040 /* Swing over to radio frequency selection */
1041 hdw->freqSelector = 0;
1042 hdw->freqDirty = !0;
1043 }
Mike Isely1bde0282006-12-27 23:30:13 -03001044 if (hdw->freqValRadio != val) {
1045 hdw->freqValRadio = val;
1046 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001047 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001048 }
Mike Isely7c74e572007-01-20 00:15:41 -03001049 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001050 if (!(hdw->freqSelector)) {
1051 /* Swing over to television frequency selection */
1052 hdw->freqSelector = 1;
1053 hdw->freqDirty = !0;
1054 }
Mike Isely1bde0282006-12-27 23:30:13 -03001055 if (hdw->freqValTelevision != val) {
1056 hdw->freqValTelevision = val;
1057 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001058 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001059 }
Mike Isely1bde0282006-12-27 23:30:13 -03001060 }
1061}
1062
Mike Iselyd8554972006-06-26 20:58:46 -03001063int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1064{
1065 return hdw->unit_number;
1066}
1067
1068
1069/* Attempt to locate one of the given set of files. Messages are logged
1070 appropriate to what has been found. The return value will be 0 or
1071 greater on success (it will be the index of the file name found) and
1072 fw_entry will be filled in. Otherwise a negative error is returned on
1073 failure. If the return value is -ENOENT then no viable firmware file
1074 could be located. */
1075static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1076 const struct firmware **fw_entry,
1077 const char *fwtypename,
1078 unsigned int fwcount,
1079 const char *fwnames[])
1080{
1081 unsigned int idx;
1082 int ret = -EINVAL;
1083 for (idx = 0; idx < fwcount; idx++) {
1084 ret = request_firmware(fw_entry,
1085 fwnames[idx],
1086 &hdw->usb_dev->dev);
1087 if (!ret) {
1088 trace_firmware("Located %s firmware: %s;"
1089 " uploading...",
1090 fwtypename,
1091 fwnames[idx]);
1092 return idx;
1093 }
1094 if (ret == -ENOENT) continue;
1095 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1096 "request_firmware fatal error with code=%d",ret);
1097 return ret;
1098 }
1099 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1100 "***WARNING***"
1101 " Device %s firmware"
1102 " seems to be missing.",
1103 fwtypename);
1104 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1105 "Did you install the pvrusb2 firmware files"
1106 " in their proper location?");
1107 if (fwcount == 1) {
1108 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1109 "request_firmware unable to locate %s file %s",
1110 fwtypename,fwnames[0]);
1111 } else {
1112 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1113 "request_firmware unable to locate"
1114 " one of the following %s files:",
1115 fwtypename);
1116 for (idx = 0; idx < fwcount; idx++) {
1117 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1118 "request_firmware: Failed to find %s",
1119 fwnames[idx]);
1120 }
1121 }
1122 return ret;
1123}
1124
1125
1126/*
1127 * pvr2_upload_firmware1().
1128 *
1129 * Send the 8051 firmware to the device. After the upload, arrange for
1130 * device to re-enumerate.
1131 *
1132 * NOTE : the pointer to the firmware data given by request_firmware()
1133 * is not suitable for an usb transaction.
1134 *
1135 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001136static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001137{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001138 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001139 void *fw_ptr;
1140 unsigned int pipe;
1141 int ret;
1142 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001143
Mike Isely989eb152007-11-26 01:53:12 -03001144 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001145 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001146 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1147 "Connected device type defines"
1148 " no firmware to upload; ignoring firmware");
1149 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001150 }
1151
Mike Iselyd8554972006-06-26 20:58:46 -03001152 hdw->fw1_state = FW1_STATE_FAILED; // default result
1153
1154 trace_firmware("pvr2_upload_firmware1");
1155
1156 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001157 hdw->hdw_desc->fx2_firmware.cnt,
1158 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001159 if (ret < 0) {
1160 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1161 return ret;
1162 }
1163
1164 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1165 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1166
1167 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1168
1169 if (fw_entry->size != 0x2000){
1170 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1171 release_firmware(fw_entry);
1172 return -ENOMEM;
1173 }
1174
1175 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1176 if (fw_ptr == NULL){
1177 release_firmware(fw_entry);
1178 return -ENOMEM;
1179 }
1180
1181 /* We have to hold the CPU during firmware upload. */
1182 pvr2_hdw_cpureset_assert(hdw,1);
1183
1184 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1185 chunk. */
1186
1187 ret = 0;
1188 for(address = 0; address < fw_entry->size; address += 0x800) {
1189 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1190 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1191 0, fw_ptr, 0x800, HZ);
1192 }
1193
1194 trace_firmware("Upload done, releasing device's CPU");
1195
1196 /* Now release the CPU. It will disconnect and reconnect later. */
1197 pvr2_hdw_cpureset_assert(hdw,0);
1198
1199 kfree(fw_ptr);
1200 release_firmware(fw_entry);
1201
1202 trace_firmware("Upload done (%d bytes sent)",ret);
1203
1204 /* We should have written 8192 bytes */
1205 if (ret == 8192) {
1206 hdw->fw1_state = FW1_STATE_RELOAD;
1207 return 0;
1208 }
1209
1210 return -EIO;
1211}
1212
1213
1214/*
1215 * pvr2_upload_firmware2()
1216 *
1217 * This uploads encoder firmware on endpoint 2.
1218 *
1219 */
1220
1221int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1222{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001223 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001224 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001225 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001226 int actual_length;
1227 int ret = 0;
1228 int fwidx;
1229 static const char *fw_files[] = {
1230 CX2341X_FIRM_ENC_FILENAME,
1231 };
1232
Mike Isely989eb152007-11-26 01:53:12 -03001233 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001234 return 0;
1235 }
1236
Mike Iselyd8554972006-06-26 20:58:46 -03001237 trace_firmware("pvr2_upload_firmware2");
1238
1239 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001240 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001241 if (ret < 0) return ret;
1242 fwidx = ret;
1243 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001244 /* Since we're about to completely reinitialize the encoder,
1245 invalidate our cached copy of its configuration state. Next
1246 time we configure the encoder, then we'll fully configure it. */
1247 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001248
Mike Iselyd913d632008-04-06 04:04:35 -03001249 /* Encoder is about to be reset so note that as far as we're
1250 concerned now, the encoder has never been run. */
1251 del_timer_sync(&hdw->encoder_run_timer);
1252 if (hdw->state_encoder_runok) {
1253 hdw->state_encoder_runok = 0;
1254 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1255 }
1256
Mike Iselyd8554972006-06-26 20:58:46 -03001257 /* First prepare firmware loading */
1258 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1259 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1260 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1261 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1262 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1263 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1264 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1265 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1266 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1267 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1268 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1269 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1270 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1271 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1272 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1273 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001274 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1275 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001276
1277 if (ret) {
1278 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1279 "firmware2 upload prep failed, ret=%d",ret);
1280 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001281 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001282 }
1283
1284 /* Now send firmware */
1285
1286 fw_len = fw_entry->size;
1287
Mike Isely90060d32007-02-08 02:02:53 -03001288 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001289 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1290 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001291 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001292 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001293 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001294 ret = -EINVAL;
1295 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001296 }
1297
1298 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1299 if (fw_ptr == NULL){
1300 release_firmware(fw_entry);
1301 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1302 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001303 ret = -ENOMEM;
1304 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001305 }
1306
1307 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1308
Mike Isely90060d32007-02-08 02:02:53 -03001309 fw_done = 0;
1310 for (fw_done = 0; fw_done < fw_len;) {
1311 bcnt = fw_len - fw_done;
1312 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1313 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1314 /* Usbsnoop log shows that we must swap bytes... */
1315 for (icnt = 0; icnt < bcnt/4 ; icnt++)
1316 ((u32 *)fw_ptr)[icnt] =
1317 ___swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001318
Mike Isely90060d32007-02-08 02:02:53 -03001319 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001320 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001321 ret |= (actual_length != bcnt);
1322 if (ret) break;
1323 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001324 }
1325
1326 trace_firmware("upload of %s : %i / %i ",
1327 fw_files[fwidx],fw_done,fw_len);
1328
1329 kfree(fw_ptr);
1330 release_firmware(fw_entry);
1331
1332 if (ret) {
1333 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1334 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001335 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001336 }
1337
1338 /* Finish upload */
1339
1340 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1341 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001342 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001343
1344 if (ret) {
1345 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1346 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001347 }
Mike Isely21684ba2008-04-21 03:49:33 -03001348
1349 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001350 if (hdw->hdw_desc->signal_routing_scheme ==
1351 PVR2_ROUTING_SCHEME_GOTVIEW) {
1352 /* Ensure that GPIO 11 is set to output for GOTVIEW
1353 hardware. */
1354 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1355 }
Mike Iselyd8554972006-06-26 20:58:46 -03001356 return ret;
1357}
1358
1359
Mike Isely681c7392007-11-26 01:48:52 -03001360static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001361{
Mike Isely681c7392007-11-26 01:48:52 -03001362 if (st < ARRAY_SIZE(pvr2_state_names)) {
1363 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001364 }
Mike Isely681c7392007-11-26 01:48:52 -03001365 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001366}
1367
Mike Isely681c7392007-11-26 01:48:52 -03001368static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001369{
Mike Isely681c7392007-11-26 01:48:52 -03001370 if (!hdw->decoder_ctrl) {
1371 if (!hdw->flag_decoder_missed) {
1372 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1373 "WARNING: No decoder present");
1374 hdw->flag_decoder_missed = !0;
1375 trace_stbit("flag_decoder_missed",
1376 hdw->flag_decoder_missed);
1377 }
1378 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001379 }
Mike Isely681c7392007-11-26 01:48:52 -03001380 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001381 return 0;
1382}
1383
1384
Mike Isely681c7392007-11-26 01:48:52 -03001385void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1386{
1387 if (hdw->decoder_ctrl == ptr) return;
1388 hdw->decoder_ctrl = ptr;
1389 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1390 hdw->flag_decoder_missed = 0;
1391 trace_stbit("flag_decoder_missed",
1392 hdw->flag_decoder_missed);
1393 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1394 "Decoder has appeared");
1395 pvr2_hdw_state_sched(hdw);
1396 }
1397}
1398
1399
1400int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1401{
1402 return hdw->master_state;
1403}
1404
1405
1406static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1407{
1408 if (!hdw->flag_tripped) return 0;
1409 hdw->flag_tripped = 0;
1410 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1411 "Clearing driver error statuss");
1412 return !0;
1413}
1414
1415
1416int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1417{
1418 int fl;
1419 LOCK_TAKE(hdw->big_lock); do {
1420 fl = pvr2_hdw_untrip_unlocked(hdw);
1421 } while (0); LOCK_GIVE(hdw->big_lock);
1422 if (fl) pvr2_hdw_state_sched(hdw);
1423 return 0;
1424}
1425
1426
Mike Isely681c7392007-11-26 01:48:52 -03001427
1428
Mike Iselyd8554972006-06-26 20:58:46 -03001429int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1430{
Mike Isely681c7392007-11-26 01:48:52 -03001431 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001432}
1433
1434
1435int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1436{
Mike Isely681c7392007-11-26 01:48:52 -03001437 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001438 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001439 pvr2_hdw_untrip_unlocked(hdw);
1440 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1441 hdw->state_pipeline_req = enable_flag != 0;
1442 pvr2_trace(PVR2_TRACE_START_STOP,
1443 "/*--TRACE_STREAM--*/ %s",
1444 enable_flag ? "enable" : "disable");
1445 }
1446 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001447 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001448 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1449 if (enable_flag) {
1450 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1451 if (st != PVR2_STATE_READY) return -EIO;
1452 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1453 }
1454 }
Mike Iselyd8554972006-06-26 20:58:46 -03001455 return 0;
1456}
1457
1458
1459int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1460{
Mike Isely681c7392007-11-26 01:48:52 -03001461 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001462 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001463 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1464 hdw->desired_stream_type = config;
1465 hdw->state_pipeline_config = 0;
1466 trace_stbit("state_pipeline_config",
1467 hdw->state_pipeline_config);
1468 pvr2_hdw_state_sched(hdw);
1469 }
Mike Iselyd8554972006-06-26 20:58:46 -03001470 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001471 if (fl) return 0;
1472 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001473}
1474
1475
1476static int get_default_tuner_type(struct pvr2_hdw *hdw)
1477{
1478 int unit_number = hdw->unit_number;
1479 int tp = -1;
1480 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1481 tp = tuner[unit_number];
1482 }
1483 if (tp < 0) return -EINVAL;
1484 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001485 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001486 return 0;
1487}
1488
1489
1490static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1491{
1492 int unit_number = hdw->unit_number;
1493 int tp = 0;
1494 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1495 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001496 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001497 }
Mike Isely6a540252007-12-02 23:51:34 -03001498 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001499}
1500
1501
1502static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1503{
1504 int unit_number = hdw->unit_number;
1505 int tp = 0;
1506 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1507 tp = tolerance[unit_number];
1508 }
1509 return tp;
1510}
1511
1512
1513static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1514{
1515 /* Try a harmless request to fetch the eeprom's address over
1516 endpoint 1. See what happens. Only the full FX2 image can
1517 respond to this. If this probe fails then likely the FX2
1518 firmware needs be loaded. */
1519 int result;
1520 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001521 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001522 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1523 hdw->cmd_buffer,1,
1524 hdw->cmd_buffer,1);
1525 if (result < 0) break;
1526 } while(0); LOCK_GIVE(hdw->ctl_lock);
1527 if (result) {
1528 pvr2_trace(PVR2_TRACE_INIT,
1529 "Probe of device endpoint 1 result status %d",
1530 result);
1531 } else {
1532 pvr2_trace(PVR2_TRACE_INIT,
1533 "Probe of device endpoint 1 succeeded");
1534 }
1535 return result == 0;
1536}
1537
Mike Isely9f66d4e2007-09-08 22:28:51 -03001538struct pvr2_std_hack {
1539 v4l2_std_id pat; /* Pattern to match */
1540 v4l2_std_id msk; /* Which bits we care about */
1541 v4l2_std_id std; /* What additional standards or default to set */
1542};
1543
1544/* This data structure labels specific combinations of standards from
1545 tveeprom that we'll try to recognize. If we recognize one, then assume
1546 a specified default standard to use. This is here because tveeprom only
1547 tells us about available standards not the intended default standard (if
1548 any) for the device in question. We guess the default based on what has
1549 been reported as available. Note that this is only for guessing a
1550 default - which can always be overridden explicitly - and if the user
1551 has otherwise named a default then that default will always be used in
1552 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001553static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001554 { /* PAL(B/G) */
1555 .pat = V4L2_STD_B|V4L2_STD_GH,
1556 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1557 },
1558 { /* NTSC(M) */
1559 .pat = V4L2_STD_MN,
1560 .std = V4L2_STD_NTSC_M,
1561 },
1562 { /* PAL(I) */
1563 .pat = V4L2_STD_PAL_I,
1564 .std = V4L2_STD_PAL_I,
1565 },
1566 { /* SECAM(L/L') */
1567 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1568 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1569 },
1570 { /* PAL(D/D1/K) */
1571 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001572 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001573 },
1574};
1575
Mike Iselyd8554972006-06-26 20:58:46 -03001576static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1577{
1578 char buf[40];
1579 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001580 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001581
1582 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001583 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001584
1585 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001586 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001587 "Supported video standard(s) reported available"
1588 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001589 bcnt,buf);
1590
1591 hdw->std_mask_avail = hdw->std_mask_eeprom;
1592
Mike Isely3d290bd2007-12-03 01:47:12 -03001593 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001594 if (std2) {
1595 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001596 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001597 "Expanding supported video standards"
1598 " to include: %.*s",
1599 bcnt,buf);
1600 hdw->std_mask_avail |= std2;
1601 }
1602
1603 pvr2_hdw_internal_set_std_avail(hdw);
1604
1605 if (std1) {
1606 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001607 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001608 "Initial video standard forced to %.*s",
1609 bcnt,buf);
1610 hdw->std_mask_cur = std1;
1611 hdw->std_dirty = !0;
1612 pvr2_hdw_internal_find_stdenum(hdw);
1613 return;
1614 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001615 if (std3) {
1616 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1617 pvr2_trace(PVR2_TRACE_STD,
1618 "Initial video standard"
1619 " (determined by device type): %.*s",bcnt,buf);
1620 hdw->std_mask_cur = std3;
1621 hdw->std_dirty = !0;
1622 pvr2_hdw_internal_find_stdenum(hdw);
1623 return;
1624 }
Mike Iselyd8554972006-06-26 20:58:46 -03001625
Mike Isely9f66d4e2007-09-08 22:28:51 -03001626 {
1627 unsigned int idx;
1628 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1629 if (std_eeprom_maps[idx].msk ?
1630 ((std_eeprom_maps[idx].pat ^
1631 hdw->std_mask_eeprom) &
1632 std_eeprom_maps[idx].msk) :
1633 (std_eeprom_maps[idx].pat !=
1634 hdw->std_mask_eeprom)) continue;
1635 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1636 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001637 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001638 "Initial video standard guessed as %.*s",
1639 bcnt,buf);
1640 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1641 hdw->std_dirty = !0;
1642 pvr2_hdw_internal_find_stdenum(hdw);
1643 return;
1644 }
1645 }
1646
Mike Iselyd8554972006-06-26 20:58:46 -03001647 if (hdw->std_enum_cnt > 1) {
1648 // Autoselect the first listed standard
1649 hdw->std_enum_cur = 1;
1650 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1651 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001652 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001653 "Initial video standard auto-selected to %s",
1654 hdw->std_defs[hdw->std_enum_cur-1].name);
1655 return;
1656 }
1657
Mike Isely0885ba12006-06-25 21:30:47 -03001658 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001659 "Unable to select a viable initial video standard");
1660}
1661
1662
1663static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1664{
1665 int ret;
1666 unsigned int idx;
1667 struct pvr2_ctrl *cptr;
1668 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001669 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001670 if (!reloadFl) {
1671 reloadFl =
1672 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1673 == 0);
1674 if (reloadFl) {
1675 pvr2_trace(PVR2_TRACE_INIT,
1676 "USB endpoint config looks strange"
1677 "; possibly firmware needs to be"
1678 " loaded");
1679 }
1680 }
1681 if (!reloadFl) {
1682 reloadFl = !pvr2_hdw_check_firmware(hdw);
1683 if (reloadFl) {
1684 pvr2_trace(PVR2_TRACE_INIT,
1685 "Check for FX2 firmware failed"
1686 "; possibly firmware needs to be"
1687 " loaded");
1688 }
1689 }
Mike Iselyd8554972006-06-26 20:58:46 -03001690 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001691 if (pvr2_upload_firmware1(hdw) != 0) {
1692 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1693 "Failure uploading firmware1");
1694 }
1695 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001696 }
1697 }
Mike Iselyd8554972006-06-26 20:58:46 -03001698 hdw->fw1_state = FW1_STATE_OK;
1699
1700 if (initusbreset) {
1701 pvr2_hdw_device_reset(hdw);
1702 }
1703 if (!pvr2_hdw_dev_ok(hdw)) return;
1704
Mike Isely989eb152007-11-26 01:53:12 -03001705 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1706 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001707 }
1708
Mike Isely989eb152007-11-26 01:53:12 -03001709 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001710 pvr2_hdw_cmd_powerup(hdw);
1711 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001712 }
1713
1714 // This step MUST happen after the earlier powerup step.
1715 pvr2_i2c_core_init(hdw);
1716 if (!pvr2_hdw_dev_ok(hdw)) return;
1717
Mike Iselyc05c0462006-06-25 20:04:25 -03001718 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001719 cptr = hdw->controls + idx;
1720 if (cptr->info->skip_init) continue;
1721 if (!cptr->info->set_value) continue;
1722 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1723 }
1724
Mike Isely1bde0282006-12-27 23:30:13 -03001725 /* Set up special default values for the television and radio
1726 frequencies here. It's not really important what these defaults
1727 are, but I set them to something usable in the Chicago area just
1728 to make driver testing a little easier. */
1729
Michael Krufky5a4f5da62008-05-11 16:37:50 -03001730 hdw->freqValTelevision = default_tv_freq;
1731 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03001732
Mike Iselyd8554972006-06-26 20:58:46 -03001733 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1734 // thread-safe against the normal pvr2_send_request() mechanism.
1735 // (We should make it thread safe).
1736
Mike Iselyaaf78842007-11-26 02:04:11 -03001737 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1738 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001739 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03001740 if (ret < 0) {
1741 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1742 "Unable to determine location of eeprom,"
1743 " skipping");
1744 } else {
1745 hdw->eeprom_addr = ret;
1746 pvr2_eeprom_analyze(hdw);
1747 if (!pvr2_hdw_dev_ok(hdw)) return;
1748 }
1749 } else {
1750 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1751 hdw->tuner_updated = !0;
1752 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03001753 }
1754
1755 pvr2_hdw_setup_std(hdw);
1756
1757 if (!get_default_tuner_type(hdw)) {
1758 pvr2_trace(PVR2_TRACE_INIT,
1759 "pvr2_hdw_setup: Tuner type overridden to %d",
1760 hdw->tuner_type);
1761 }
1762
Mike Iselyd8554972006-06-26 20:58:46 -03001763 pvr2_i2c_core_check_stale(hdw);
1764 hdw->tuner_updated = 0;
1765
1766 if (!pvr2_hdw_dev_ok(hdw)) return;
1767
Mike Isely1df59f02008-04-21 03:50:39 -03001768 if (hdw->hdw_desc->signal_routing_scheme ==
1769 PVR2_ROUTING_SCHEME_GOTVIEW) {
1770 /* Ensure that GPIO 11 is set to output for GOTVIEW
1771 hardware. */
1772 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1773 }
1774
Mike Isely681c7392007-11-26 01:48:52 -03001775 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001776
1777 hdw->vid_stream = pvr2_stream_create();
1778 if (!pvr2_hdw_dev_ok(hdw)) return;
1779 pvr2_trace(PVR2_TRACE_INIT,
1780 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1781 if (hdw->vid_stream) {
1782 idx = get_default_error_tolerance(hdw);
1783 if (idx) {
1784 pvr2_trace(PVR2_TRACE_INIT,
1785 "pvr2_hdw_setup: video stream %p"
1786 " setting tolerance %u",
1787 hdw->vid_stream,idx);
1788 }
1789 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1790 PVR2_VID_ENDPOINT,idx);
1791 }
1792
1793 if (!pvr2_hdw_dev_ok(hdw)) return;
1794
Mike Iselyd8554972006-06-26 20:58:46 -03001795 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03001796
1797 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001798}
1799
1800
Mike Isely681c7392007-11-26 01:48:52 -03001801/* Set up the structure and attempt to put the device into a usable state.
1802 This can be a time-consuming operation, which is why it is not done
1803 internally as part of the create() step. */
1804static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001805{
1806 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03001807 do {
Mike Iselyd8554972006-06-26 20:58:46 -03001808 pvr2_hdw_setup_low(hdw);
1809 pvr2_trace(PVR2_TRACE_INIT,
1810 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03001811 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03001812 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03001813 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03001814 pvr2_trace(
1815 PVR2_TRACE_INFO,
1816 "Device initialization"
1817 " completed successfully.");
1818 break;
1819 }
1820 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1821 pvr2_trace(
1822 PVR2_TRACE_INFO,
1823 "Device microcontroller firmware"
1824 " (re)loaded; it should now reset"
1825 " and reconnect.");
1826 break;
1827 }
1828 pvr2_trace(
1829 PVR2_TRACE_ERROR_LEGS,
1830 "Device initialization was not successful.");
1831 if (hdw->fw1_state == FW1_STATE_MISSING) {
1832 pvr2_trace(
1833 PVR2_TRACE_ERROR_LEGS,
1834 "Giving up since device"
1835 " microcontroller firmware"
1836 " appears to be missing.");
1837 break;
1838 }
1839 }
1840 if (procreload) {
1841 pvr2_trace(
1842 PVR2_TRACE_ERROR_LEGS,
1843 "Attempting pvrusb2 recovery by reloading"
1844 " primary firmware.");
1845 pvr2_trace(
1846 PVR2_TRACE_ERROR_LEGS,
1847 "If this works, device should disconnect"
1848 " and reconnect in a sane state.");
1849 hdw->fw1_state = FW1_STATE_UNKNOWN;
1850 pvr2_upload_firmware1(hdw);
1851 } else {
1852 pvr2_trace(
1853 PVR2_TRACE_ERROR_LEGS,
1854 "***WARNING*** pvrusb2 device hardware"
1855 " appears to be jammed"
1856 " and I can't clear it.");
1857 pvr2_trace(
1858 PVR2_TRACE_ERROR_LEGS,
1859 "You might need to power cycle"
1860 " the pvrusb2 device"
1861 " in order to recover.");
1862 }
Mike Isely681c7392007-11-26 01:48:52 -03001863 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03001864 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001865}
1866
1867
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001868/* Perform second stage initialization. Set callback pointer first so that
1869 we can avoid a possible initialization race (if the kernel thread runs
1870 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03001871int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
1872 void (*callback_func)(void *),
1873 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001874{
1875 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03001876 if (hdw->flag_disconnected) {
1877 /* Handle a race here: If we're already
1878 disconnected by this point, then give up. If we
1879 get past this then we'll remain connected for
1880 the duration of initialization since the entire
1881 initialization sequence is now protected by the
1882 big_lock. */
1883 break;
1884 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001885 hdw->state_data = callback_data;
1886 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03001887 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001888 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03001889 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03001890}
1891
1892
1893/* Create, set up, and return a structure for interacting with the
1894 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03001895struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1896 const struct usb_device_id *devid)
1897{
Mike Isely7fb20fa2008-04-22 14:45:37 -03001898 unsigned int idx,cnt1,cnt2,m;
Mike Iselyd8554972006-06-26 20:58:46 -03001899 struct pvr2_hdw *hdw;
Mike Iselyd8554972006-06-26 20:58:46 -03001900 int valid_std_mask;
1901 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03001902 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03001903 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03001904 struct v4l2_queryctrl qctrl;
1905 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03001906
Mike Iselyd130fa82007-12-08 17:20:06 -03001907 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03001908
Mike Iselyca545f72007-01-20 00:37:11 -03001909 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001910 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03001911 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03001912 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03001913
1914 init_timer(&hdw->quiescent_timer);
1915 hdw->quiescent_timer.data = (unsigned long)hdw;
1916 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1917
1918 init_timer(&hdw->encoder_wait_timer);
1919 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1920 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1921
Mike Iselyd913d632008-04-06 04:04:35 -03001922 init_timer(&hdw->encoder_run_timer);
1923 hdw->encoder_run_timer.data = (unsigned long)hdw;
1924 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
1925
Mike Isely681c7392007-11-26 01:48:52 -03001926 hdw->master_state = PVR2_STATE_DEAD;
1927
1928 init_waitqueue_head(&hdw->state_wait_data);
1929
Mike Isely18103c572007-01-20 00:09:47 -03001930 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001931 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03001932
Mike Isely7fb20fa2008-04-22 14:45:37 -03001933 /* Calculate which inputs are OK */
1934 m = 0;
1935 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03001936 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
1937 m |= 1 << PVR2_CVAL_INPUT_DTV;
1938 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03001939 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
1940 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
1941 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
1942 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03001943 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03001944
Mike Isely62433e32008-04-22 14:45:40 -03001945 /* If not a hybrid device, pathway_state never changes. So
1946 initialize it here to what it should forever be. */
1947 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
1948 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
1949 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
1950 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
1951 }
1952
Mike Iselyc05c0462006-06-25 20:04:25 -03001953 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03001954 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03001955 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001956 GFP_KERNEL);
1957 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03001958 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03001959 for (idx = 0; idx < hdw->control_cnt; idx++) {
1960 cptr = hdw->controls + idx;
1961 cptr->hdw = hdw;
1962 }
Mike Iselyd8554972006-06-26 20:58:46 -03001963 for (idx = 0; idx < 32; idx++) {
1964 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1965 }
Mike Iselyc05c0462006-06-25 20:04:25 -03001966 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001967 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03001968 cptr->info = control_defs+idx;
1969 }
Mike Iselydbc40a02008-04-22 14:45:39 -03001970
1971 /* Ensure that default input choice is a valid one. */
1972 m = hdw->input_avail_mask;
1973 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
1974 if (!((1 << idx) & m)) continue;
1975 hdw->input_val = idx;
1976 break;
1977 }
1978
Mike Iselyb30d2442006-06-25 20:05:01 -03001979 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03001980 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03001981 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
1982 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03001983 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
1984 cptr = hdw->controls + idx + CTRLDEF_COUNT;
1985 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
1986 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
1987 ciptr->name = mpeg_ids[idx].strid;
1988 ciptr->v4l_id = mpeg_ids[idx].id;
1989 ciptr->skip_init = !0;
1990 ciptr->get_value = ctrl_cx2341x_get;
1991 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
1992 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
1993 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
1994 qctrl.id = ciptr->v4l_id;
1995 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
1996 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
1997 ciptr->set_value = ctrl_cx2341x_set;
1998 }
1999 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2000 PVR2_CTLD_INFO_DESC_SIZE);
2001 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2002 ciptr->default_value = qctrl.default_value;
2003 switch (qctrl.type) {
2004 default:
2005 case V4L2_CTRL_TYPE_INTEGER:
2006 ciptr->type = pvr2_ctl_int;
2007 ciptr->def.type_int.min_value = qctrl.minimum;
2008 ciptr->def.type_int.max_value = qctrl.maximum;
2009 break;
2010 case V4L2_CTRL_TYPE_BOOLEAN:
2011 ciptr->type = pvr2_ctl_bool;
2012 break;
2013 case V4L2_CTRL_TYPE_MENU:
2014 ciptr->type = pvr2_ctl_enum;
2015 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002016 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2017 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002018 for (cnt1 = 0;
2019 ciptr->def.type_enum.value_names[cnt1] != NULL;
2020 cnt1++) { }
2021 ciptr->def.type_enum.count = cnt1;
2022 break;
2023 }
2024 cptr->info = ciptr;
2025 }
Mike Iselyd8554972006-06-26 20:58:46 -03002026
2027 // Initialize video standard enum dynamic control
2028 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2029 if (cptr) {
2030 memcpy(&hdw->std_info_enum,cptr->info,
2031 sizeof(hdw->std_info_enum));
2032 cptr->info = &hdw->std_info_enum;
2033
2034 }
2035 // Initialize control data regarding video standard masks
2036 valid_std_mask = pvr2_std_get_usable();
2037 for (idx = 0; idx < 32; idx++) {
2038 if (!(valid_std_mask & (1 << idx))) continue;
2039 cnt1 = pvr2_std_id_to_str(
2040 hdw->std_mask_names[idx],
2041 sizeof(hdw->std_mask_names[idx])-1,
2042 1 << idx);
2043 hdw->std_mask_names[idx][cnt1] = 0;
2044 }
2045 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2046 if (cptr) {
2047 memcpy(&hdw->std_info_avail,cptr->info,
2048 sizeof(hdw->std_info_avail));
2049 cptr->info = &hdw->std_info_avail;
2050 hdw->std_info_avail.def.type_bitmask.bit_names =
2051 hdw->std_mask_ptrs;
2052 hdw->std_info_avail.def.type_bitmask.valid_bits =
2053 valid_std_mask;
2054 }
2055 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2056 if (cptr) {
2057 memcpy(&hdw->std_info_cur,cptr->info,
2058 sizeof(hdw->std_info_cur));
2059 cptr->info = &hdw->std_info_cur;
2060 hdw->std_info_cur.def.type_bitmask.bit_names =
2061 hdw->std_mask_ptrs;
2062 hdw->std_info_avail.def.type_bitmask.valid_bits =
2063 valid_std_mask;
2064 }
2065
2066 hdw->eeprom_addr = -1;
2067 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002068 hdw->v4l_minor_number_video = -1;
2069 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002070 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002071 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2072 if (!hdw->ctl_write_buffer) goto fail;
2073 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2074 if (!hdw->ctl_read_buffer) goto fail;
2075 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2076 if (!hdw->ctl_write_urb) goto fail;
2077 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2078 if (!hdw->ctl_read_urb) goto fail;
2079
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002080 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002081 for (idx = 0; idx < PVR_NUM; idx++) {
2082 if (unit_pointers[idx]) continue;
2083 hdw->unit_number = idx;
2084 unit_pointers[idx] = hdw;
2085 break;
2086 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002087 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002088
2089 cnt1 = 0;
2090 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2091 cnt1 += cnt2;
2092 if (hdw->unit_number >= 0) {
2093 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2094 ('a' + hdw->unit_number));
2095 cnt1 += cnt2;
2096 }
2097 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2098 hdw->name[cnt1] = 0;
2099
Mike Isely681c7392007-11-26 01:48:52 -03002100 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2101 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2102 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002103
Mike Iselyd8554972006-06-26 20:58:46 -03002104 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2105 hdw->unit_number,hdw->name);
2106
2107 hdw->tuner_type = -1;
2108 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002109
2110 hdw->usb_intf = intf;
2111 hdw->usb_dev = interface_to_usbdev(intf);
2112
Mike Isely31a18542007-04-08 01:11:47 -03002113 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2114 "usb %s address %d",
2115 hdw->usb_dev->dev.bus_id,
2116 hdw->usb_dev->devnum);
2117
Mike Iselyd8554972006-06-26 20:58:46 -03002118 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2119 usb_set_interface(hdw->usb_dev,ifnum,0);
2120
2121 mutex_init(&hdw->ctl_lock_mutex);
2122 mutex_init(&hdw->big_lock_mutex);
2123
2124 return hdw;
2125 fail:
2126 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002127 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002128 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002129 del_timer_sync(&hdw->encoder_wait_timer);
2130 if (hdw->workqueue) {
2131 flush_workqueue(hdw->workqueue);
2132 destroy_workqueue(hdw->workqueue);
2133 hdw->workqueue = NULL;
2134 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002135 usb_free_urb(hdw->ctl_read_urb);
2136 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002137 kfree(hdw->ctl_read_buffer);
2138 kfree(hdw->ctl_write_buffer);
2139 kfree(hdw->controls);
2140 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002141 kfree(hdw->std_defs);
2142 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002143 kfree(hdw);
2144 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002145 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002146}
2147
2148
2149/* Remove _all_ associations between this driver and the underlying USB
2150 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002151static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002152{
2153 if (hdw->flag_disconnected) return;
2154 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2155 if (hdw->ctl_read_urb) {
2156 usb_kill_urb(hdw->ctl_read_urb);
2157 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002158 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002159 }
2160 if (hdw->ctl_write_urb) {
2161 usb_kill_urb(hdw->ctl_write_urb);
2162 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002163 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002164 }
2165 if (hdw->ctl_read_buffer) {
2166 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002167 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002168 }
2169 if (hdw->ctl_write_buffer) {
2170 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002171 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002172 }
Mike Iselyd8554972006-06-26 20:58:46 -03002173 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002174 hdw->usb_dev = NULL;
2175 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002176 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002177}
2178
2179
2180/* Destroy hardware interaction structure */
2181void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2182{
Mike Isely401c27c2007-09-08 22:11:46 -03002183 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002184 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002185 if (hdw->workqueue) {
2186 flush_workqueue(hdw->workqueue);
2187 destroy_workqueue(hdw->workqueue);
2188 hdw->workqueue = NULL;
2189 }
Mike Isely8f591002008-04-22 14:45:45 -03002190 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002191 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002192 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002193 if (hdw->fw_buffer) {
2194 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002195 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002196 }
2197 if (hdw->vid_stream) {
2198 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002199 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002200 }
Mike Iselyd8554972006-06-26 20:58:46 -03002201 if (hdw->decoder_ctrl) {
2202 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2203 }
2204 pvr2_i2c_core_done(hdw);
2205 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002206 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002207 if ((hdw->unit_number >= 0) &&
2208 (hdw->unit_number < PVR_NUM) &&
2209 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002210 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002211 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002212 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002213 kfree(hdw->controls);
2214 kfree(hdw->mpeg_ctrl_info);
2215 kfree(hdw->std_defs);
2216 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002217 kfree(hdw);
2218}
2219
2220
Mike Iselyd8554972006-06-26 20:58:46 -03002221int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2222{
2223 return (hdw && hdw->flag_ok);
2224}
2225
2226
2227/* Called when hardware has been unplugged */
2228void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2229{
2230 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2231 LOCK_TAKE(hdw->big_lock);
2232 LOCK_TAKE(hdw->ctl_lock);
2233 pvr2_hdw_remove_usb_stuff(hdw);
2234 LOCK_GIVE(hdw->ctl_lock);
2235 LOCK_GIVE(hdw->big_lock);
2236}
2237
2238
2239// Attempt to autoselect an appropriate value for std_enum_cur given
2240// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002241static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002242{
2243 unsigned int idx;
2244 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2245 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2246 hdw->std_enum_cur = idx;
2247 return;
2248 }
2249 }
2250 hdw->std_enum_cur = 0;
2251}
2252
2253
2254// Calculate correct set of enumerated standards based on currently known
2255// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002256static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002257{
2258 struct v4l2_standard *newstd;
2259 unsigned int std_cnt;
2260 unsigned int idx;
2261
2262 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2263
2264 if (hdw->std_defs) {
2265 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002266 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002267 }
2268 hdw->std_enum_cnt = 0;
2269 if (hdw->std_enum_names) {
2270 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002271 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002272 }
2273
2274 if (!std_cnt) {
2275 pvr2_trace(
2276 PVR2_TRACE_ERROR_LEGS,
2277 "WARNING: Failed to identify any viable standards");
2278 }
2279 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2280 hdw->std_enum_names[0] = "none";
2281 for (idx = 0; idx < std_cnt; idx++) {
2282 hdw->std_enum_names[idx+1] =
2283 newstd[idx].name;
2284 }
2285 // Set up the dynamic control for this standard
2286 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2287 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2288 hdw->std_defs = newstd;
2289 hdw->std_enum_cnt = std_cnt+1;
2290 hdw->std_enum_cur = 0;
2291 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2292}
2293
2294
2295int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2296 struct v4l2_standard *std,
2297 unsigned int idx)
2298{
2299 int ret = -EINVAL;
2300 if (!idx) return ret;
2301 LOCK_TAKE(hdw->big_lock); do {
2302 if (idx >= hdw->std_enum_cnt) break;
2303 idx--;
2304 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2305 ret = 0;
2306 } while (0); LOCK_GIVE(hdw->big_lock);
2307 return ret;
2308}
2309
2310
2311/* Get the number of defined controls */
2312unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2313{
Mike Iselyc05c0462006-06-25 20:04:25 -03002314 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002315}
2316
2317
2318/* Retrieve a control handle given its index (0..count-1) */
2319struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2320 unsigned int idx)
2321{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002322 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002323 return hdw->controls + idx;
2324}
2325
2326
2327/* Retrieve a control handle given its index (0..count-1) */
2328struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2329 unsigned int ctl_id)
2330{
2331 struct pvr2_ctrl *cptr;
2332 unsigned int idx;
2333 int i;
2334
2335 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002336 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002337 cptr = hdw->controls + idx;
2338 i = cptr->info->internal_id;
2339 if (i && (i == ctl_id)) return cptr;
2340 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002341 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002342}
2343
2344
Mike Iselya761f432006-06-25 20:04:44 -03002345/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002346struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2347{
2348 struct pvr2_ctrl *cptr;
2349 unsigned int idx;
2350 int i;
2351
2352 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002353 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002354 cptr = hdw->controls + idx;
2355 i = cptr->info->v4l_id;
2356 if (i && (i == ctl_id)) return cptr;
2357 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002358 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002359}
2360
2361
Mike Iselya761f432006-06-25 20:04:44 -03002362/* Given a V4L ID for its immediate predecessor, retrieve the control
2363 structure associated with it. */
2364struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2365 unsigned int ctl_id)
2366{
2367 struct pvr2_ctrl *cptr,*cp2;
2368 unsigned int idx;
2369 int i;
2370
2371 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002372 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002373 for (idx = 0; idx < hdw->control_cnt; idx++) {
2374 cptr = hdw->controls + idx;
2375 i = cptr->info->v4l_id;
2376 if (!i) continue;
2377 if (i <= ctl_id) continue;
2378 if (cp2 && (cp2->info->v4l_id < i)) continue;
2379 cp2 = cptr;
2380 }
2381 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002382 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002383}
2384
2385
Mike Iselyd8554972006-06-26 20:58:46 -03002386static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2387{
2388 switch (tp) {
2389 case pvr2_ctl_int: return "integer";
2390 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002391 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002392 case pvr2_ctl_bitmask: return "bitmask";
2393 }
2394 return "";
2395}
2396
2397
Mike Isely681c7392007-11-26 01:48:52 -03002398/* Figure out if we need to commit control changes. If so, mark internal
2399 state flags to indicate this fact and return true. Otherwise do nothing
2400 else and return false. */
2401static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002402{
Mike Iselyd8554972006-06-26 20:58:46 -03002403 unsigned int idx;
2404 struct pvr2_ctrl *cptr;
2405 int value;
2406 int commit_flag = 0;
2407 char buf[100];
2408 unsigned int bcnt,ccnt;
2409
Mike Iselyc05c0462006-06-25 20:04:25 -03002410 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002411 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002412 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002413 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002414 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002415
Mike Iselyfe23a282007-01-20 00:10:55 -03002416 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002417 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2418 cptr->info->name);
2419 value = 0;
2420 cptr->info->get_value(cptr,&value);
2421 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2422 buf+bcnt,
2423 sizeof(buf)-bcnt,&ccnt);
2424 bcnt += ccnt;
2425 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2426 get_ctrl_typename(cptr->info->type));
2427 pvr2_trace(PVR2_TRACE_CTL,
2428 "/*--TRACE_COMMIT--*/ %.*s",
2429 bcnt,buf);
2430 }
2431
2432 if (!commit_flag) {
2433 /* Nothing has changed */
2434 return 0;
2435 }
2436
Mike Isely681c7392007-11-26 01:48:52 -03002437 hdw->state_pipeline_config = 0;
2438 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2439 pvr2_hdw_state_sched(hdw);
2440
2441 return !0;
2442}
2443
2444
2445/* Perform all operations needed to commit all control changes. This must
2446 be performed in synchronization with the pipeline state and is thus
2447 expected to be called as part of the driver's worker thread. Return
2448 true if commit successful, otherwise return false to indicate that
2449 commit isn't possible at this time. */
2450static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2451{
2452 unsigned int idx;
2453 struct pvr2_ctrl *cptr;
2454 int disruptive_change;
2455
Mike Iselyd8554972006-06-26 20:58:46 -03002456 /* When video standard changes, reset the hres and vres values -
2457 but if the user has pending changes there, then let the changes
2458 take priority. */
2459 if (hdw->std_dirty) {
2460 /* Rewrite the vertical resolution to be appropriate to the
2461 video standard that has been selected. */
2462 int nvres;
2463 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2464 nvres = 480;
2465 } else {
2466 nvres = 576;
2467 }
2468 if (nvres != hdw->res_ver_val) {
2469 hdw->res_ver_val = nvres;
2470 hdw->res_ver_dirty = !0;
2471 }
Mike Iselyd8554972006-06-26 20:58:46 -03002472 }
2473
Mike Isely38d9a2c2008-03-28 05:30:48 -03002474 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03002475 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2476 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2477 hdw->pathway_state)) {
2478 /* Change of mode being asked for... */
2479 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03002480 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03002481 }
2482 if (!hdw->state_pathway_ok) {
2483 /* Can't commit anything until pathway is ok. */
2484 return 0;
2485 }
Mike Isely681c7392007-11-26 01:48:52 -03002486 /* If any of the below has changed, then we can't do the update
2487 while the pipeline is running. Pipeline must be paused first
2488 and decoder -> encoder connection be made quiescent before we
2489 can proceed. */
2490 disruptive_change =
2491 (hdw->std_dirty ||
2492 hdw->enc_unsafe_stale ||
2493 hdw->srate_dirty ||
2494 hdw->res_ver_dirty ||
2495 hdw->res_hor_dirty ||
2496 hdw->input_dirty ||
2497 (hdw->active_stream_type != hdw->desired_stream_type));
2498 if (disruptive_change && !hdw->state_pipeline_idle) {
2499 /* Pipeline is not idle; we can't proceed. Arrange to
2500 cause pipeline to stop so that we can try this again
2501 later.... */
2502 hdw->state_pipeline_pause = !0;
2503 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002504 }
2505
Mike Iselyb30d2442006-06-25 20:05:01 -03002506 if (hdw->srate_dirty) {
2507 /* Write new sample rate into control structure since
2508 * the master copy is stale. We must track srate
2509 * separate from the mpeg control structure because
2510 * other logic also uses this value. */
2511 struct v4l2_ext_controls cs;
2512 struct v4l2_ext_control c1;
2513 memset(&cs,0,sizeof(cs));
2514 memset(&c1,0,sizeof(c1));
2515 cs.controls = &c1;
2516 cs.count = 1;
2517 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2518 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002519 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002520 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002521
Mike Iselyd8554972006-06-26 20:58:46 -03002522 /* Scan i2c core at this point - before we clear all the dirty
2523 bits. Various parts of the i2c core will notice dirty bits as
2524 appropriate and arrange to broadcast or directly send updates to
2525 the client drivers in order to keep everything in sync */
2526 pvr2_i2c_core_check_stale(hdw);
2527
Mike Iselyc05c0462006-06-25 20:04:25 -03002528 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002529 cptr = hdw->controls + idx;
2530 if (!cptr->info->clear_dirty) continue;
2531 cptr->info->clear_dirty(cptr);
2532 }
2533
Mike Isely681c7392007-11-26 01:48:52 -03002534 if (hdw->active_stream_type != hdw->desired_stream_type) {
2535 /* Handle any side effects of stream config here */
2536 hdw->active_stream_type = hdw->desired_stream_type;
2537 }
2538
Mike Isely1df59f02008-04-21 03:50:39 -03002539 if (hdw->hdw_desc->signal_routing_scheme ==
2540 PVR2_ROUTING_SCHEME_GOTVIEW) {
2541 u32 b;
2542 /* Handle GOTVIEW audio switching */
2543 pvr2_hdw_gpio_get_out(hdw,&b);
2544 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2545 /* Set GPIO 11 */
2546 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
2547 } else {
2548 /* Clear GPIO 11 */
2549 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
2550 }
2551 }
2552
Mike Iselyd8554972006-06-26 20:58:46 -03002553 /* Now execute i2c core update */
2554 pvr2_i2c_core_sync(hdw);
2555
Mike Isely62433e32008-04-22 14:45:40 -03002556 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2557 hdw->state_encoder_run) {
2558 /* If encoder isn't running or it can't be touched, then
2559 this will get worked out later when we start the
2560 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002561 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2562 }
Mike Iselyd8554972006-06-26 20:58:46 -03002563
Mike Isely681c7392007-11-26 01:48:52 -03002564 hdw->state_pipeline_config = !0;
2565 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2566 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002567}
2568
2569
2570int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2571{
Mike Isely681c7392007-11-26 01:48:52 -03002572 int fl;
2573 LOCK_TAKE(hdw->big_lock);
2574 fl = pvr2_hdw_commit_setup(hdw);
2575 LOCK_GIVE(hdw->big_lock);
2576 if (!fl) return 0;
2577 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002578}
2579
2580
Mike Isely681c7392007-11-26 01:48:52 -03002581static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002582{
Mike Isely681c7392007-11-26 01:48:52 -03002583 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002584 LOCK_TAKE(hdw->big_lock); do {
2585 pvr2_i2c_core_sync(hdw);
2586 } while (0); LOCK_GIVE(hdw->big_lock);
2587}
2588
2589
Mike Isely681c7392007-11-26 01:48:52 -03002590static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002591{
Mike Isely681c7392007-11-26 01:48:52 -03002592 int fl = 0;
2593 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002594 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002595 fl = pvr2_hdw_state_eval(hdw);
2596 } while (0); LOCK_GIVE(hdw->big_lock);
2597 if (fl && hdw->state_func) {
2598 hdw->state_func(hdw->state_data);
2599 }
2600}
2601
2602
Mike Isely681c7392007-11-26 01:48:52 -03002603static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002604{
Mike Isely681c7392007-11-26 01:48:52 -03002605 return wait_event_interruptible(
2606 hdw->state_wait_data,
2607 (hdw->state_stale == 0) &&
2608 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002609}
2610
Mike Isely681c7392007-11-26 01:48:52 -03002611
Mike Iselyd8554972006-06-26 20:58:46 -03002612/* Return name for this driver instance */
2613const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2614{
2615 return hdw->name;
2616}
2617
2618
Mike Isely78a47102007-11-26 01:58:20 -03002619const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2620{
2621 return hdw->hdw_desc->description;
2622}
2623
2624
2625const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2626{
2627 return hdw->hdw_desc->shortname;
2628}
2629
2630
Mike Iselyd8554972006-06-26 20:58:46 -03002631int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2632{
2633 int result;
2634 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002635 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002636 result = pvr2_send_request(hdw,
2637 hdw->cmd_buffer,1,
2638 hdw->cmd_buffer,1);
2639 if (result < 0) break;
2640 result = (hdw->cmd_buffer[0] != 0);
2641 } while(0); LOCK_GIVE(hdw->ctl_lock);
2642 return result;
2643}
2644
2645
Mike Isely18103c572007-01-20 00:09:47 -03002646/* Execute poll of tuner status */
2647void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002648{
Mike Iselyd8554972006-06-26 20:58:46 -03002649 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002650 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002651 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002652}
2653
2654
2655/* Return information about the tuner */
2656int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2657{
2658 LOCK_TAKE(hdw->big_lock); do {
2659 if (hdw->tuner_signal_stale) {
2660 pvr2_i2c_core_status_poll(hdw);
2661 }
2662 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2663 } while (0); LOCK_GIVE(hdw->big_lock);
2664 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002665}
2666
2667
2668/* Get handle to video output stream */
2669struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2670{
2671 return hp->vid_stream;
2672}
2673
2674
2675void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2676{
Mike Isely4f1a3e52006-06-25 20:04:31 -03002677 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002678 LOCK_TAKE(hdw->big_lock); do {
2679 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03002680 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002681 pvr2_i2c_core_check_stale(hdw);
2682 hdw->log_requested = 0;
2683 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03002684 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03002685 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03002686 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03002687 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002688 } while (0); LOCK_GIVE(hdw->big_lock);
2689}
2690
Mike Isely4db666c2007-09-08 22:16:27 -03002691
2692/* Grab EEPROM contents, needed for direct method. */
2693#define EEPROM_SIZE 8192
2694#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
2695static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
2696{
2697 struct i2c_msg msg[2];
2698 u8 *eeprom;
2699 u8 iadd[2];
2700 u8 addr;
2701 u16 eepromSize;
2702 unsigned int offs;
2703 int ret;
2704 int mode16 = 0;
2705 unsigned pcnt,tcnt;
2706 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
2707 if (!eeprom) {
2708 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2709 "Failed to allocate memory"
2710 " required to read eeprom");
2711 return NULL;
2712 }
2713
2714 trace_eeprom("Value for eeprom addr from controller was 0x%x",
2715 hdw->eeprom_addr);
2716 addr = hdw->eeprom_addr;
2717 /* Seems that if the high bit is set, then the *real* eeprom
2718 address is shifted right now bit position (noticed this in
2719 newer PVR USB2 hardware) */
2720 if (addr & 0x80) addr >>= 1;
2721
2722 /* FX2 documentation states that a 16bit-addressed eeprom is
2723 expected if the I2C address is an odd number (yeah, this is
2724 strange but it's what they do) */
2725 mode16 = (addr & 1);
2726 eepromSize = (mode16 ? EEPROM_SIZE : 256);
2727 trace_eeprom("Examining %d byte eeprom at location 0x%x"
2728 " using %d bit addressing",eepromSize,addr,
2729 mode16 ? 16 : 8);
2730
2731 msg[0].addr = addr;
2732 msg[0].flags = 0;
2733 msg[0].len = mode16 ? 2 : 1;
2734 msg[0].buf = iadd;
2735 msg[1].addr = addr;
2736 msg[1].flags = I2C_M_RD;
2737
2738 /* We have to do the actual eeprom data fetch ourselves, because
2739 (1) we're only fetching part of the eeprom, and (2) if we were
2740 getting the whole thing our I2C driver can't grab it in one
2741 pass - which is what tveeprom is otherwise going to attempt */
2742 memset(eeprom,0,EEPROM_SIZE);
2743 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
2744 pcnt = 16;
2745 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
2746 offs = tcnt + (eepromSize - EEPROM_SIZE);
2747 if (mode16) {
2748 iadd[0] = offs >> 8;
2749 iadd[1] = offs;
2750 } else {
2751 iadd[0] = offs;
2752 }
2753 msg[1].len = pcnt;
2754 msg[1].buf = eeprom+tcnt;
2755 if ((ret = i2c_transfer(&hdw->i2c_adap,
2756 msg,ARRAY_SIZE(msg))) != 2) {
2757 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2758 "eeprom fetch set offs err=%d",ret);
2759 kfree(eeprom);
2760 return NULL;
2761 }
2762 }
2763 return eeprom;
2764}
2765
2766
2767void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
2768 int prom_flag,
2769 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002770{
2771 int ret;
2772 u16 address;
2773 unsigned int pipe;
2774 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00002775 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03002776
2777 if (!enable_flag) {
2778 pvr2_trace(PVR2_TRACE_FIRMWARE,
2779 "Cleaning up after CPU firmware fetch");
2780 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002781 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002782 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03002783 if (hdw->fw_cpu_flag) {
2784 /* Now release the CPU. It will disconnect
2785 and reconnect later. */
2786 pvr2_hdw_cpureset_assert(hdw,0);
2787 }
Mike Iselyd8554972006-06-26 20:58:46 -03002788 break;
2789 }
2790
Mike Isely4db666c2007-09-08 22:16:27 -03002791 hdw->fw_cpu_flag = (prom_flag == 0);
2792 if (hdw->fw_cpu_flag) {
2793 pvr2_trace(PVR2_TRACE_FIRMWARE,
2794 "Preparing to suck out CPU firmware");
2795 hdw->fw_size = 0x2000;
2796 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
2797 if (!hdw->fw_buffer) {
2798 hdw->fw_size = 0;
2799 break;
2800 }
2801
2802 /* We have to hold the CPU during firmware upload. */
2803 pvr2_hdw_cpureset_assert(hdw,1);
2804
2805 /* download the firmware from address 0000-1fff in 2048
2806 (=0x800) bytes chunk. */
2807
2808 pvr2_trace(PVR2_TRACE_FIRMWARE,
2809 "Grabbing CPU firmware");
2810 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2811 for(address = 0; address < hdw->fw_size;
2812 address += 0x800) {
2813 ret = usb_control_msg(hdw->usb_dev,pipe,
2814 0xa0,0xc0,
2815 address,0,
2816 hdw->fw_buffer+address,
2817 0x800,HZ);
2818 if (ret < 0) break;
2819 }
2820
2821 pvr2_trace(PVR2_TRACE_FIRMWARE,
2822 "Done grabbing CPU firmware");
2823 } else {
2824 pvr2_trace(PVR2_TRACE_FIRMWARE,
2825 "Sucking down EEPROM contents");
2826 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
2827 if (!hdw->fw_buffer) {
2828 pvr2_trace(PVR2_TRACE_FIRMWARE,
2829 "EEPROM content suck failed.");
2830 break;
2831 }
2832 hdw->fw_size = EEPROM_SIZE;
2833 pvr2_trace(PVR2_TRACE_FIRMWARE,
2834 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03002835 }
2836
Mike Iselyd8554972006-06-26 20:58:46 -03002837 } while (0); LOCK_GIVE(hdw->big_lock);
2838}
2839
2840
2841/* Return true if we're in a mode for retrieval CPU firmware */
2842int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2843{
Al Viro5fa12472008-03-29 03:07:38 +00002844 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002845}
2846
2847
2848int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2849 char *buf,unsigned int cnt)
2850{
2851 int ret = -EINVAL;
2852 LOCK_TAKE(hdw->big_lock); do {
2853 if (!buf) break;
2854 if (!cnt) break;
2855
2856 if (!hdw->fw_buffer) {
2857 ret = -EIO;
2858 break;
2859 }
2860
2861 if (offs >= hdw->fw_size) {
2862 pvr2_trace(PVR2_TRACE_FIRMWARE,
2863 "Read firmware data offs=%d EOF",
2864 offs);
2865 ret = 0;
2866 break;
2867 }
2868
2869 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2870
2871 memcpy(buf,hdw->fw_buffer+offs,cnt);
2872
2873 pvr2_trace(PVR2_TRACE_FIRMWARE,
2874 "Read firmware data offs=%d cnt=%d",
2875 offs,cnt);
2876 ret = cnt;
2877 } while (0); LOCK_GIVE(hdw->big_lock);
2878
2879 return ret;
2880}
2881
2882
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002883int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002884 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03002885{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002886 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002887 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2888 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2889 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002890 default: return -1;
2891 }
Mike Iselyd8554972006-06-26 20:58:46 -03002892}
2893
2894
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03002895/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002896void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002897 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03002898{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002899 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002900 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2901 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2902 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002903 default: break;
2904 }
Mike Iselyd8554972006-06-26 20:58:46 -03002905}
2906
2907
David Howells7d12e782006-10-05 14:55:46 +01002908static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002909{
2910 struct pvr2_hdw *hdw = urb->context;
2911 hdw->ctl_write_pend_flag = 0;
2912 if (hdw->ctl_read_pend_flag) return;
2913 complete(&hdw->ctl_done);
2914}
2915
2916
David Howells7d12e782006-10-05 14:55:46 +01002917static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002918{
2919 struct pvr2_hdw *hdw = urb->context;
2920 hdw->ctl_read_pend_flag = 0;
2921 if (hdw->ctl_write_pend_flag) return;
2922 complete(&hdw->ctl_done);
2923}
2924
2925
2926static void pvr2_ctl_timeout(unsigned long data)
2927{
2928 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2929 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2930 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002931 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002932 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002933 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002934 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03002935 }
2936}
2937
2938
Mike Iselye61b6fc2006-07-18 22:42:18 -03002939/* Issue a command and get a response from the device. This extended
2940 version includes a probe flag (which if set means that device errors
2941 should not be logged or treated as fatal) and a timeout in jiffies.
2942 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002943static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2944 unsigned int timeout,int probe_fl,
2945 void *write_data,unsigned int write_len,
2946 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03002947{
2948 unsigned int idx;
2949 int status = 0;
2950 struct timer_list timer;
2951 if (!hdw->ctl_lock_held) {
2952 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2953 "Attempted to execute control transfer"
2954 " without lock!!");
2955 return -EDEADLK;
2956 }
Mike Isely681c7392007-11-26 01:48:52 -03002957 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03002958 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2959 "Attempted to execute control transfer"
2960 " when device not ok");
2961 return -EIO;
2962 }
2963 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
2964 if (!probe_fl) {
2965 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2966 "Attempted to execute control transfer"
2967 " when USB is disconnected");
2968 }
2969 return -ENOTTY;
2970 }
2971
2972 /* Ensure that we have sane parameters */
2973 if (!write_data) write_len = 0;
2974 if (!read_data) read_len = 0;
2975 if (write_len > PVR2_CTL_BUFFSIZE) {
2976 pvr2_trace(
2977 PVR2_TRACE_ERROR_LEGS,
2978 "Attempted to execute %d byte"
2979 " control-write transfer (limit=%d)",
2980 write_len,PVR2_CTL_BUFFSIZE);
2981 return -EINVAL;
2982 }
2983 if (read_len > PVR2_CTL_BUFFSIZE) {
2984 pvr2_trace(
2985 PVR2_TRACE_ERROR_LEGS,
2986 "Attempted to execute %d byte"
2987 " control-read transfer (limit=%d)",
2988 write_len,PVR2_CTL_BUFFSIZE);
2989 return -EINVAL;
2990 }
2991 if ((!write_len) && (!read_len)) {
2992 pvr2_trace(
2993 PVR2_TRACE_ERROR_LEGS,
2994 "Attempted to execute null control transfer?");
2995 return -EINVAL;
2996 }
2997
2998
2999 hdw->cmd_debug_state = 1;
3000 if (write_len) {
3001 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3002 } else {
3003 hdw->cmd_debug_code = 0;
3004 }
3005 hdw->cmd_debug_write_len = write_len;
3006 hdw->cmd_debug_read_len = read_len;
3007
3008 /* Initialize common stuff */
3009 init_completion(&hdw->ctl_done);
3010 hdw->ctl_timeout_flag = 0;
3011 hdw->ctl_write_pend_flag = 0;
3012 hdw->ctl_read_pend_flag = 0;
3013 init_timer(&timer);
3014 timer.expires = jiffies + timeout;
3015 timer.data = (unsigned long)hdw;
3016 timer.function = pvr2_ctl_timeout;
3017
3018 if (write_len) {
3019 hdw->cmd_debug_state = 2;
3020 /* Transfer write data to internal buffer */
3021 for (idx = 0; idx < write_len; idx++) {
3022 hdw->ctl_write_buffer[idx] =
3023 ((unsigned char *)write_data)[idx];
3024 }
3025 /* Initiate a write request */
3026 usb_fill_bulk_urb(hdw->ctl_write_urb,
3027 hdw->usb_dev,
3028 usb_sndbulkpipe(hdw->usb_dev,
3029 PVR2_CTL_WRITE_ENDPOINT),
3030 hdw->ctl_write_buffer,
3031 write_len,
3032 pvr2_ctl_write_complete,
3033 hdw);
3034 hdw->ctl_write_urb->actual_length = 0;
3035 hdw->ctl_write_pend_flag = !0;
3036 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3037 if (status < 0) {
3038 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3039 "Failed to submit write-control"
3040 " URB status=%d",status);
3041 hdw->ctl_write_pend_flag = 0;
3042 goto done;
3043 }
3044 }
3045
3046 if (read_len) {
3047 hdw->cmd_debug_state = 3;
3048 memset(hdw->ctl_read_buffer,0x43,read_len);
3049 /* Initiate a read request */
3050 usb_fill_bulk_urb(hdw->ctl_read_urb,
3051 hdw->usb_dev,
3052 usb_rcvbulkpipe(hdw->usb_dev,
3053 PVR2_CTL_READ_ENDPOINT),
3054 hdw->ctl_read_buffer,
3055 read_len,
3056 pvr2_ctl_read_complete,
3057 hdw);
3058 hdw->ctl_read_urb->actual_length = 0;
3059 hdw->ctl_read_pend_flag = !0;
3060 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3061 if (status < 0) {
3062 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3063 "Failed to submit read-control"
3064 " URB status=%d",status);
3065 hdw->ctl_read_pend_flag = 0;
3066 goto done;
3067 }
3068 }
3069
3070 /* Start timer */
3071 add_timer(&timer);
3072
3073 /* Now wait for all I/O to complete */
3074 hdw->cmd_debug_state = 4;
3075 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3076 wait_for_completion(&hdw->ctl_done);
3077 }
3078 hdw->cmd_debug_state = 5;
3079
3080 /* Stop timer */
3081 del_timer_sync(&timer);
3082
3083 hdw->cmd_debug_state = 6;
3084 status = 0;
3085
3086 if (hdw->ctl_timeout_flag) {
3087 status = -ETIMEDOUT;
3088 if (!probe_fl) {
3089 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3090 "Timed out control-write");
3091 }
3092 goto done;
3093 }
3094
3095 if (write_len) {
3096 /* Validate results of write request */
3097 if ((hdw->ctl_write_urb->status != 0) &&
3098 (hdw->ctl_write_urb->status != -ENOENT) &&
3099 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3100 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3101 /* USB subsystem is reporting some kind of failure
3102 on the write */
3103 status = hdw->ctl_write_urb->status;
3104 if (!probe_fl) {
3105 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3106 "control-write URB failure,"
3107 " status=%d",
3108 status);
3109 }
3110 goto done;
3111 }
3112 if (hdw->ctl_write_urb->actual_length < write_len) {
3113 /* Failed to write enough data */
3114 status = -EIO;
3115 if (!probe_fl) {
3116 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3117 "control-write URB short,"
3118 " expected=%d got=%d",
3119 write_len,
3120 hdw->ctl_write_urb->actual_length);
3121 }
3122 goto done;
3123 }
3124 }
3125 if (read_len) {
3126 /* Validate results of read request */
3127 if ((hdw->ctl_read_urb->status != 0) &&
3128 (hdw->ctl_read_urb->status != -ENOENT) &&
3129 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3130 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3131 /* USB subsystem is reporting some kind of failure
3132 on the read */
3133 status = hdw->ctl_read_urb->status;
3134 if (!probe_fl) {
3135 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3136 "control-read URB failure,"
3137 " status=%d",
3138 status);
3139 }
3140 goto done;
3141 }
3142 if (hdw->ctl_read_urb->actual_length < read_len) {
3143 /* Failed to read enough data */
3144 status = -EIO;
3145 if (!probe_fl) {
3146 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3147 "control-read URB short,"
3148 " expected=%d got=%d",
3149 read_len,
3150 hdw->ctl_read_urb->actual_length);
3151 }
3152 goto done;
3153 }
3154 /* Transfer retrieved data out from internal buffer */
3155 for (idx = 0; idx < read_len; idx++) {
3156 ((unsigned char *)read_data)[idx] =
3157 hdw->ctl_read_buffer[idx];
3158 }
3159 }
3160
3161 done:
3162
3163 hdw->cmd_debug_state = 0;
3164 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003165 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003166 }
3167 return status;
3168}
3169
3170
3171int pvr2_send_request(struct pvr2_hdw *hdw,
3172 void *write_data,unsigned int write_len,
3173 void *read_data,unsigned int read_len)
3174{
3175 return pvr2_send_request_ex(hdw,HZ*4,0,
3176 write_data,write_len,
3177 read_data,read_len);
3178}
3179
Mike Isely1c9d10d2008-03-28 05:38:54 -03003180
3181static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3182{
3183 int ret;
3184 unsigned int cnt = 1;
3185 unsigned int args = 0;
3186 LOCK_TAKE(hdw->ctl_lock);
3187 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3188 args = (cmdcode >> 8) & 0xffu;
3189 args = (args > 2) ? 2 : args;
3190 if (args) {
3191 cnt += args;
3192 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3193 if (args > 1) {
3194 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3195 }
3196 }
3197 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3198 unsigned int idx;
3199 unsigned int ccnt,bcnt;
3200 char tbuf[50];
3201 cmdcode &= 0xffu;
3202 bcnt = 0;
3203 ccnt = scnprintf(tbuf+bcnt,
3204 sizeof(tbuf)-bcnt,
3205 "Sending FX2 command 0x%x",cmdcode);
3206 bcnt += ccnt;
3207 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3208 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3209 ccnt = scnprintf(tbuf+bcnt,
3210 sizeof(tbuf)-bcnt,
3211 " \"%s\"",
3212 pvr2_fx2cmd_desc[idx].desc);
3213 bcnt += ccnt;
3214 break;
3215 }
3216 }
3217 if (args) {
3218 ccnt = scnprintf(tbuf+bcnt,
3219 sizeof(tbuf)-bcnt,
3220 " (%u",hdw->cmd_buffer[1]);
3221 bcnt += ccnt;
3222 if (args > 1) {
3223 ccnt = scnprintf(tbuf+bcnt,
3224 sizeof(tbuf)-bcnt,
3225 ",%u",hdw->cmd_buffer[2]);
3226 bcnt += ccnt;
3227 }
3228 ccnt = scnprintf(tbuf+bcnt,
3229 sizeof(tbuf)-bcnt,
3230 ")");
3231 bcnt += ccnt;
3232 }
3233 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3234 }
3235 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3236 LOCK_GIVE(hdw->ctl_lock);
3237 return ret;
3238}
3239
3240
Mike Iselyd8554972006-06-26 20:58:46 -03003241int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3242{
3243 int ret;
3244
3245 LOCK_TAKE(hdw->ctl_lock);
3246
Michael Krufky8d364362007-01-22 02:17:55 -03003247 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003248 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3249 hdw->cmd_buffer[5] = 0;
3250 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3251 hdw->cmd_buffer[7] = reg & 0xff;
3252
3253
3254 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3255
3256 LOCK_GIVE(hdw->ctl_lock);
3257
3258 return ret;
3259}
3260
3261
Adrian Bunk07e337e2006-06-30 11:30:20 -03003262static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003263{
3264 int ret = 0;
3265
3266 LOCK_TAKE(hdw->ctl_lock);
3267
Michael Krufky8d364362007-01-22 02:17:55 -03003268 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003269 hdw->cmd_buffer[1] = 0;
3270 hdw->cmd_buffer[2] = 0;
3271 hdw->cmd_buffer[3] = 0;
3272 hdw->cmd_buffer[4] = 0;
3273 hdw->cmd_buffer[5] = 0;
3274 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3275 hdw->cmd_buffer[7] = reg & 0xff;
3276
3277 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3278 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3279
3280 LOCK_GIVE(hdw->ctl_lock);
3281
3282 return ret;
3283}
3284
3285
Mike Isely681c7392007-11-26 01:48:52 -03003286void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003287{
3288 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003289 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3290 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003291 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003292 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003293 }
Mike Isely681c7392007-11-26 01:48:52 -03003294 hdw->flag_ok = 0;
3295 trace_stbit("flag_ok",hdw->flag_ok);
3296 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003297}
3298
3299
3300void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3301{
3302 int ret;
3303 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003304 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003305 if (ret == 1) {
3306 ret = usb_reset_device(hdw->usb_dev);
3307 usb_unlock_device(hdw->usb_dev);
3308 } else {
3309 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3310 "Failed to lock USB device ret=%d",ret);
3311 }
3312 if (init_pause_msec) {
3313 pvr2_trace(PVR2_TRACE_INFO,
3314 "Waiting %u msec for hardware to settle",
3315 init_pause_msec);
3316 msleep(init_pause_msec);
3317 }
3318
3319}
3320
3321
3322void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3323{
3324 char da[1];
3325 unsigned int pipe;
3326 int ret;
3327
3328 if (!hdw->usb_dev) return;
3329
3330 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3331
3332 da[0] = val ? 0x01 : 0x00;
3333
3334 /* Write the CPUCS register on the 8051. The lsb of the register
3335 is the reset bit; a 1 asserts reset while a 0 clears it. */
3336 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3337 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3338 if (ret < 0) {
3339 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3340 "cpureset_assert(%d) error=%d",val,ret);
3341 pvr2_hdw_render_useless(hdw);
3342 }
3343}
3344
3345
3346int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3347{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003348 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03003349}
3350
3351
Michael Krufkye1edb192008-04-22 14:45:39 -03003352int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3353{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003354 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03003355}
3356
Mike Isely1c9d10d2008-03-28 05:38:54 -03003357
Michael Krufkye1edb192008-04-22 14:45:39 -03003358int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3359{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003360 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03003361}
3362
Mike Iselyd8554972006-06-26 20:58:46 -03003363
3364int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3365{
3366 if (!hdw->decoder_ctrl) {
3367 pvr2_trace(PVR2_TRACE_INIT,
3368 "Unable to reset decoder: nothing attached");
3369 return -ENOTTY;
3370 }
3371
3372 if (!hdw->decoder_ctrl->force_reset) {
3373 pvr2_trace(PVR2_TRACE_INIT,
3374 "Unable to reset decoder: not implemented");
3375 return -ENOTTY;
3376 }
3377
3378 pvr2_trace(PVR2_TRACE_INIT,
3379 "Requesting decoder reset");
3380 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3381 return 0;
3382}
3383
3384
Mike Isely62433e32008-04-22 14:45:40 -03003385static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003386{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003387 hdw->flag_ok = !0;
3388 return pvr2_issue_simple_cmd(hdw,
3389 FX2CMD_HCW_DEMOD_RESETIN |
3390 (1 << 8) |
3391 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03003392}
3393
Mike Isely84147f32008-04-22 14:45:40 -03003394
Mike Isely62433e32008-04-22 14:45:40 -03003395static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003396{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003397 hdw->flag_ok = !0;
3398 return pvr2_issue_simple_cmd(hdw,(onoff ?
3399 FX2CMD_ONAIR_DTV_POWER_ON :
3400 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003401}
3402
Mike Isely62433e32008-04-22 14:45:40 -03003403
3404static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3405 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003406{
Mike Isely1c9d10d2008-03-28 05:38:54 -03003407 return pvr2_issue_simple_cmd(hdw,(onoff ?
3408 FX2CMD_ONAIR_DTV_STREAMING_ON :
3409 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03003410}
3411
Mike Isely62433e32008-04-22 14:45:40 -03003412
3413static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3414{
3415 int cmode;
3416 /* Compare digital/analog desired setting with current setting. If
3417 they don't match, fix it... */
3418 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3419 if (cmode == hdw->pathway_state) {
3420 /* They match; nothing to do */
3421 return;
3422 }
3423
3424 switch (hdw->hdw_desc->digital_control_scheme) {
3425 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3426 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3427 if (cmode == PVR2_PATHWAY_ANALOG) {
3428 /* If moving to analog mode, also force the decoder
3429 to reset. If no decoder is attached, then it's
3430 ok to ignore this because if/when the decoder
3431 attaches, it will reset itself at that time. */
3432 pvr2_hdw_cmd_decoder_reset(hdw);
3433 }
3434 break;
3435 case PVR2_DIGITAL_SCHEME_ONAIR:
3436 /* Supposedly we should always have the power on whether in
3437 digital or analog mode. But for now do what appears to
3438 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003439 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03003440 break;
3441 default: break;
3442 }
3443
Mike Isely1b9c18c2008-04-22 14:45:41 -03003444 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03003445 hdw->pathway_state = cmode;
3446}
3447
3448
Adrian Bunke9b59f62008-05-10 04:35:24 -03003449static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03003450{
3451 /* change some GPIO data
3452 *
3453 * note: bit d7 of dir appears to control the LED,
3454 * so we shut it off here.
3455 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03003456 */
Mike Isely40381cb2008-04-22 14:45:42 -03003457 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003458 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03003459 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003460 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03003461 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03003462 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03003463}
Mike Iselyc55a97d2008-04-22 14:45:41 -03003464
Mike Isely40381cb2008-04-22 14:45:42 -03003465
3466typedef void (*led_method_func)(struct pvr2_hdw *,int);
3467
3468static led_method_func led_methods[] = {
3469 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
3470};
3471
3472
3473/* Toggle LED */
3474static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
3475{
3476 unsigned int scheme_id;
3477 led_method_func fp;
3478
3479 if ((!onoff) == (!hdw->led_on)) return;
3480
3481 hdw->led_on = onoff != 0;
3482
3483 scheme_id = hdw->hdw_desc->led_scheme;
3484 if (scheme_id < ARRAY_SIZE(led_methods)) {
3485 fp = led_methods[scheme_id];
3486 } else {
3487 fp = NULL;
3488 }
3489
3490 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03003491}
3492
3493
Mike Iselye61b6fc2006-07-18 22:42:18 -03003494/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003495static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003496{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003497 int ret;
3498
3499 /* If we're in analog mode, then just issue the usual analog
3500 command. */
3501 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3502 return pvr2_issue_simple_cmd(hdw,
3503 (runFl ?
3504 FX2CMD_STREAMING_ON :
3505 FX2CMD_STREAMING_OFF));
3506 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03003507 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03003508
3509 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
3510 /* Whoops, we don't know what mode we're in... */
3511 return -EINVAL;
3512 }
3513
3514 /* To get here we have to be in digital mode. The mechanism here
3515 is unfortunately different for different vendors. So we switch
3516 on the device's digital scheme attribute in order to figure out
3517 what to do. */
3518 switch (hdw->hdw_desc->digital_control_scheme) {
3519 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3520 return pvr2_issue_simple_cmd(hdw,
3521 (runFl ?
3522 FX2CMD_HCW_DTV_STREAMING_ON :
3523 FX2CMD_HCW_DTV_STREAMING_OFF));
3524 case PVR2_DIGITAL_SCHEME_ONAIR:
3525 ret = pvr2_issue_simple_cmd(hdw,
3526 (runFl ?
3527 FX2CMD_STREAMING_ON :
3528 FX2CMD_STREAMING_OFF));
3529 if (ret) return ret;
3530 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
3531 default:
3532 return -EINVAL;
3533 }
Mike Iselyd8554972006-06-26 20:58:46 -03003534}
3535
3536
Mike Isely62433e32008-04-22 14:45:40 -03003537/* Evaluate whether or not state_pathway_ok can change */
3538static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3539{
3540 if (hdw->state_pathway_ok) {
3541 /* Nothing to do if pathway is already ok */
3542 return 0;
3543 }
3544 if (!hdw->state_pipeline_idle) {
3545 /* Not allowed to change anything if pipeline is not idle */
3546 return 0;
3547 }
3548 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3549 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003550 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003551 return !0;
3552}
3553
3554
Mike Isely681c7392007-11-26 01:48:52 -03003555/* Evaluate whether or not state_encoder_ok can change */
3556static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3557{
3558 if (hdw->state_encoder_ok) return 0;
3559 if (hdw->flag_tripped) return 0;
3560 if (hdw->state_encoder_run) return 0;
3561 if (hdw->state_encoder_config) return 0;
3562 if (hdw->state_decoder_run) return 0;
3563 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003564 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
3565 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
3566 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
3567 return 0;
3568 }
3569
Mike Isely681c7392007-11-26 01:48:52 -03003570 if (pvr2_upload_firmware2(hdw) < 0) {
3571 hdw->flag_tripped = !0;
3572 trace_stbit("flag_tripped",hdw->flag_tripped);
3573 return !0;
3574 }
3575 hdw->state_encoder_ok = !0;
3576 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3577 return !0;
3578}
3579
3580
3581/* Evaluate whether or not state_encoder_config can change */
3582static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3583{
3584 if (hdw->state_encoder_config) {
3585 if (hdw->state_encoder_ok) {
3586 if (hdw->state_pipeline_req &&
3587 !hdw->state_pipeline_pause) return 0;
3588 }
3589 hdw->state_encoder_config = 0;
3590 hdw->state_encoder_waitok = 0;
3591 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3592 /* paranoia - solve race if timer just completed */
3593 del_timer_sync(&hdw->encoder_wait_timer);
3594 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003595 if (!hdw->state_pathway_ok ||
3596 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3597 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003598 !hdw->state_pipeline_idle ||
3599 hdw->state_pipeline_pause ||
3600 !hdw->state_pipeline_req ||
3601 !hdw->state_pipeline_config) {
3602 /* We must reset the enforced wait interval if
3603 anything has happened that might have disturbed
3604 the encoder. This should be a rare case. */
3605 if (timer_pending(&hdw->encoder_wait_timer)) {
3606 del_timer_sync(&hdw->encoder_wait_timer);
3607 }
3608 if (hdw->state_encoder_waitok) {
3609 /* Must clear the state - therefore we did
3610 something to a state bit and must also
3611 return true. */
3612 hdw->state_encoder_waitok = 0;
3613 trace_stbit("state_encoder_waitok",
3614 hdw->state_encoder_waitok);
3615 return !0;
3616 }
3617 return 0;
3618 }
3619 if (!hdw->state_encoder_waitok) {
3620 if (!timer_pending(&hdw->encoder_wait_timer)) {
3621 /* waitok flag wasn't set and timer isn't
3622 running. Check flag once more to avoid
3623 a race then start the timer. This is
3624 the point when we measure out a minimal
3625 quiet interval before doing something to
3626 the encoder. */
3627 if (!hdw->state_encoder_waitok) {
3628 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003629 jiffies +
3630 (HZ * TIME_MSEC_ENCODER_WAIT
3631 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03003632 add_timer(&hdw->encoder_wait_timer);
3633 }
3634 }
3635 /* We can't continue until we know we have been
3636 quiet for the interval measured by this
3637 timer. */
3638 return 0;
3639 }
3640 pvr2_encoder_configure(hdw);
3641 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3642 }
3643 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3644 return !0;
3645}
3646
3647
Mike Iselyd913d632008-04-06 04:04:35 -03003648/* Return true if the encoder should not be running. */
3649static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
3650{
3651 if (!hdw->state_encoder_ok) {
3652 /* Encoder isn't healthy at the moment, so stop it. */
3653 return !0;
3654 }
3655 if (!hdw->state_pathway_ok) {
3656 /* Mode is not understood at the moment (i.e. it wants to
3657 change), so encoder must be stopped. */
3658 return !0;
3659 }
3660
3661 switch (hdw->pathway_state) {
3662 case PVR2_PATHWAY_ANALOG:
3663 if (!hdw->state_decoder_run) {
3664 /* We're in analog mode and the decoder is not
3665 running; thus the encoder should be stopped as
3666 well. */
3667 return !0;
3668 }
3669 break;
3670 case PVR2_PATHWAY_DIGITAL:
3671 if (hdw->state_encoder_runok) {
3672 /* This is a funny case. We're in digital mode so
3673 really the encoder should be stopped. However
3674 if it really is running, only kill it after
3675 runok has been set. This gives a chance for the
3676 onair quirk to function (encoder must run
3677 briefly first, at least once, before onair
3678 digital streaming can work). */
3679 return !0;
3680 }
3681 break;
3682 default:
3683 /* Unknown mode; so encoder should be stopped. */
3684 return !0;
3685 }
3686
3687 /* If we get here, we haven't found a reason to stop the
3688 encoder. */
3689 return 0;
3690}
3691
3692
3693/* Return true if the encoder should be running. */
3694static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
3695{
3696 if (!hdw->state_encoder_ok) {
3697 /* Don't run the encoder if it isn't healthy... */
3698 return 0;
3699 }
3700 if (!hdw->state_pathway_ok) {
3701 /* Don't run the encoder if we don't (yet) know what mode
3702 we need to be in... */
3703 return 0;
3704 }
3705
3706 switch (hdw->pathway_state) {
3707 case PVR2_PATHWAY_ANALOG:
3708 if (hdw->state_decoder_run) {
3709 /* In analog mode, if the decoder is running, then
3710 run the encoder. */
3711 return !0;
3712 }
3713 break;
3714 case PVR2_PATHWAY_DIGITAL:
3715 if ((hdw->hdw_desc->digital_control_scheme ==
3716 PVR2_DIGITAL_SCHEME_ONAIR) &&
3717 !hdw->state_encoder_runok) {
3718 /* This is a quirk. OnAir hardware won't stream
3719 digital until the encoder has been run at least
3720 once, for a minimal period of time (empiricially
3721 measured to be 1/4 second). So if we're on
3722 OnAir hardware and the encoder has never been
3723 run at all, then start the encoder. Normal
3724 state machine logic in the driver will
3725 automatically handle the remaining bits. */
3726 return !0;
3727 }
3728 break;
3729 default:
3730 /* For completeness (unknown mode; encoder won't run ever) */
3731 break;
3732 }
3733 /* If we get here, then we haven't found any reason to run the
3734 encoder, so don't run it. */
3735 return 0;
3736}
3737
3738
Mike Isely681c7392007-11-26 01:48:52 -03003739/* Evaluate whether or not state_encoder_run can change */
3740static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3741{
3742 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03003743 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003744 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03003745 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03003746 if (pvr2_encoder_stop(hdw) < 0) return !0;
3747 }
3748 hdw->state_encoder_run = 0;
3749 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03003750 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003751 if (pvr2_encoder_start(hdw) < 0) return !0;
3752 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03003753 if (!hdw->state_encoder_runok) {
3754 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003755 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03003756 add_timer(&hdw->encoder_run_timer);
3757 }
Mike Isely681c7392007-11-26 01:48:52 -03003758 }
3759 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3760 return !0;
3761}
3762
3763
3764/* Timeout function for quiescent timer. */
3765static void pvr2_hdw_quiescent_timeout(unsigned long data)
3766{
3767 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3768 hdw->state_decoder_quiescent = !0;
3769 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3770 hdw->state_stale = !0;
3771 queue_work(hdw->workqueue,&hdw->workpoll);
3772}
3773
3774
3775/* Timeout function for encoder wait timer. */
3776static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3777{
3778 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3779 hdw->state_encoder_waitok = !0;
3780 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3781 hdw->state_stale = !0;
3782 queue_work(hdw->workqueue,&hdw->workpoll);
3783}
3784
3785
Mike Iselyd913d632008-04-06 04:04:35 -03003786/* Timeout function for encoder run timer. */
3787static void pvr2_hdw_encoder_run_timeout(unsigned long data)
3788{
3789 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3790 if (!hdw->state_encoder_runok) {
3791 hdw->state_encoder_runok = !0;
3792 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
3793 hdw->state_stale = !0;
3794 queue_work(hdw->workqueue,&hdw->workpoll);
3795 }
3796}
3797
3798
Mike Isely681c7392007-11-26 01:48:52 -03003799/* Evaluate whether or not state_decoder_run can change */
3800static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3801{
3802 if (hdw->state_decoder_run) {
3803 if (hdw->state_encoder_ok) {
3804 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03003805 !hdw->state_pipeline_pause &&
3806 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003807 }
3808 if (!hdw->flag_decoder_missed) {
3809 pvr2_decoder_enable(hdw,0);
3810 }
3811 hdw->state_decoder_quiescent = 0;
3812 hdw->state_decoder_run = 0;
3813 /* paranoia - solve race if timer just completed */
3814 del_timer_sync(&hdw->quiescent_timer);
3815 } else {
3816 if (!hdw->state_decoder_quiescent) {
3817 if (!timer_pending(&hdw->quiescent_timer)) {
3818 /* We don't do something about the
3819 quiescent timer until right here because
3820 we also want to catch cases where the
3821 decoder was already not running (like
3822 after initialization) as opposed to
3823 knowing that we had just stopped it.
3824 The second flag check is here to cover a
3825 race - the timer could have run and set
3826 this flag just after the previous check
3827 but before we did the pending check. */
3828 if (!hdw->state_decoder_quiescent) {
3829 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03003830 jiffies +
3831 (HZ * TIME_MSEC_DECODER_WAIT
3832 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03003833 add_timer(&hdw->quiescent_timer);
3834 }
3835 }
3836 /* Don't allow decoder to start again until it has
3837 been quiesced first. This little detail should
3838 hopefully further stabilize the encoder. */
3839 return 0;
3840 }
Mike Isely62433e32008-04-22 14:45:40 -03003841 if (!hdw->state_pathway_ok ||
3842 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3843 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03003844 hdw->state_pipeline_pause ||
3845 !hdw->state_pipeline_config ||
3846 !hdw->state_encoder_config ||
3847 !hdw->state_encoder_ok) return 0;
3848 del_timer_sync(&hdw->quiescent_timer);
3849 if (hdw->flag_decoder_missed) return 0;
3850 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3851 hdw->state_decoder_quiescent = 0;
3852 hdw->state_decoder_run = !0;
3853 }
3854 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3855 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3856 return !0;
3857}
3858
3859
3860/* Evaluate whether or not state_usbstream_run can change */
3861static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3862{
3863 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03003864 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03003865 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03003866 fl = (hdw->state_encoder_ok &&
3867 hdw->state_encoder_run);
3868 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3869 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
3870 fl = hdw->state_encoder_ok;
3871 }
3872 if (fl &&
3873 hdw->state_pipeline_req &&
3874 !hdw->state_pipeline_pause &&
3875 hdw->state_pathway_ok) {
3876 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003877 }
3878 pvr2_hdw_cmd_usbstream(hdw,0);
3879 hdw->state_usbstream_run = 0;
3880 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003881 if (!hdw->state_pipeline_req ||
3882 hdw->state_pipeline_pause ||
3883 !hdw->state_pathway_ok) return 0;
3884 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3885 if (!hdw->state_encoder_ok ||
3886 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03003887 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3888 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
3889 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03003890 if (hdw->state_encoder_run) return 0;
3891 if (hdw->hdw_desc->digital_control_scheme ==
3892 PVR2_DIGITAL_SCHEME_ONAIR) {
3893 /* OnAir digital receivers won't stream
3894 unless the analog encoder has run first.
3895 Why? I have no idea. But don't even
3896 try until we know the analog side is
3897 known to have run. */
3898 if (!hdw->state_encoder_runok) return 0;
3899 }
Mike Isely62433e32008-04-22 14:45:40 -03003900 }
Mike Isely681c7392007-11-26 01:48:52 -03003901 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3902 hdw->state_usbstream_run = !0;
3903 }
3904 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3905 return !0;
3906}
3907
3908
3909/* Attempt to configure pipeline, if needed */
3910static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3911{
3912 if (hdw->state_pipeline_config ||
3913 hdw->state_pipeline_pause) return 0;
3914 pvr2_hdw_commit_execute(hdw);
3915 return !0;
3916}
3917
3918
3919/* Update pipeline idle and pipeline pause tracking states based on other
3920 inputs. This must be called whenever the other relevant inputs have
3921 changed. */
3922static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3923{
3924 unsigned int st;
3925 int updatedFl = 0;
3926 /* Update pipeline state */
3927 st = !(hdw->state_encoder_run ||
3928 hdw->state_decoder_run ||
3929 hdw->state_usbstream_run ||
3930 (!hdw->state_decoder_quiescent));
3931 if (!st != !hdw->state_pipeline_idle) {
3932 hdw->state_pipeline_idle = st;
3933 updatedFl = !0;
3934 }
3935 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3936 hdw->state_pipeline_pause = 0;
3937 updatedFl = !0;
3938 }
3939 return updatedFl;
3940}
3941
3942
3943typedef int (*state_eval_func)(struct pvr2_hdw *);
3944
3945/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03003946static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03003947 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03003948 state_eval_pipeline_config,
3949 state_eval_encoder_ok,
3950 state_eval_encoder_config,
3951 state_eval_decoder_run,
3952 state_eval_encoder_run,
3953 state_eval_usbstream_run,
3954};
3955
3956
3957/* Process various states and return true if we did anything interesting. */
3958static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
3959{
3960 unsigned int i;
3961 int state_updated = 0;
3962 int check_flag;
3963
3964 if (!hdw->state_stale) return 0;
3965 if ((hdw->fw1_state != FW1_STATE_OK) ||
3966 !hdw->flag_ok) {
3967 hdw->state_stale = 0;
3968 return !0;
3969 }
3970 /* This loop is the heart of the entire driver. It keeps trying to
3971 evaluate various bits of driver state until nothing changes for
3972 one full iteration. Each "bit of state" tracks some global
3973 aspect of the driver, e.g. whether decoder should run, if
3974 pipeline is configured, usb streaming is on, etc. We separately
3975 evaluate each of those questions based on other driver state to
3976 arrive at the correct running configuration. */
3977 do {
3978 check_flag = 0;
3979 state_update_pipeline_state(hdw);
3980 /* Iterate over each bit of state */
3981 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
3982 if ((*eval_funcs[i])(hdw)) {
3983 check_flag = !0;
3984 state_updated = !0;
3985 state_update_pipeline_state(hdw);
3986 }
3987 }
3988 } while (check_flag && hdw->flag_ok);
3989 hdw->state_stale = 0;
3990 trace_stbit("state_stale",hdw->state_stale);
3991 return state_updated;
3992}
3993
3994
Mike Isely1cb03b72008-04-21 03:47:43 -03003995static unsigned int print_input_mask(unsigned int msk,
3996 char *buf,unsigned int acnt)
3997{
3998 unsigned int idx,ccnt;
3999 unsigned int tcnt = 0;
4000 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4001 if (!((1 << idx) & msk)) continue;
4002 ccnt = scnprintf(buf+tcnt,
4003 acnt-tcnt,
4004 "%s%s",
4005 (tcnt ? ", " : ""),
4006 control_values_input[idx]);
4007 tcnt += ccnt;
4008 }
4009 return tcnt;
4010}
4011
4012
Mike Isely62433e32008-04-22 14:45:40 -03004013static const char *pvr2_pathway_state_name(int id)
4014{
4015 switch (id) {
4016 case PVR2_PATHWAY_ANALOG: return "analog";
4017 case PVR2_PATHWAY_DIGITAL: return "digital";
4018 default: return "unknown";
4019 }
4020}
4021
4022
Mike Isely681c7392007-11-26 01:48:52 -03004023static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4024 char *buf,unsigned int acnt)
4025{
4026 switch (which) {
4027 case 0:
4028 return scnprintf(
4029 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004030 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004031 (hdw->flag_ok ? " <ok>" : " <fail>"),
4032 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4033 (hdw->flag_disconnected ? " <disconnected>" :
4034 " <connected>"),
4035 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004036 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4037 pvr2_pathway_state_name(hdw->pathway_state));
4038
Mike Isely681c7392007-11-26 01:48:52 -03004039 case 1:
4040 return scnprintf(
4041 buf,acnt,
4042 "pipeline:%s%s%s%s",
4043 (hdw->state_pipeline_idle ? " <idle>" : ""),
4044 (hdw->state_pipeline_config ?
4045 " <configok>" : " <stale>"),
4046 (hdw->state_pipeline_req ? " <req>" : ""),
4047 (hdw->state_pipeline_pause ? " <pause>" : ""));
4048 case 2:
4049 return scnprintf(
4050 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004051 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004052 (hdw->state_decoder_run ?
4053 " <decode:run>" :
4054 (hdw->state_decoder_quiescent ?
4055 "" : " <decode:stop>")),
4056 (hdw->state_decoder_quiescent ?
4057 " <decode:quiescent>" : ""),
4058 (hdw->state_encoder_ok ?
4059 "" : " <encode:init>"),
4060 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004061 (hdw->state_encoder_runok ?
4062 " <encode:run>" :
4063 " <encode:firstrun>") :
4064 (hdw->state_encoder_runok ?
4065 " <encode:stop>" :
4066 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004067 (hdw->state_encoder_config ?
4068 " <encode:configok>" :
4069 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004070 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004071 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004072 " <usb:run>" : " <usb:stop>"),
4073 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004074 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004075 case 3:
4076 return scnprintf(
4077 buf,acnt,
4078 "state: %s",
4079 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004080 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004081 unsigned int tcnt = 0;
4082 unsigned int ccnt;
4083
4084 ccnt = scnprintf(buf,
4085 acnt,
4086 "Hardware supported inputs: ");
4087 tcnt += ccnt;
4088 tcnt += print_input_mask(hdw->input_avail_mask,
4089 buf+tcnt,
4090 acnt-tcnt);
4091 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4092 ccnt = scnprintf(buf+tcnt,
4093 acnt-tcnt,
4094 "; allowed inputs: ");
4095 tcnt += ccnt;
4096 tcnt += print_input_mask(hdw->input_allowed_mask,
4097 buf+tcnt,
4098 acnt-tcnt);
4099 }
4100 return tcnt;
4101 }
4102 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004103 struct pvr2_stream_stats stats;
4104 if (!hdw->vid_stream) break;
4105 pvr2_stream_get_stats(hdw->vid_stream,
4106 &stats,
4107 0);
4108 return scnprintf(
4109 buf,acnt,
4110 "Bytes streamed=%u"
4111 " URBs: queued=%u idle=%u ready=%u"
4112 " processed=%u failed=%u",
4113 stats.bytes_processed,
4114 stats.buffers_in_queue,
4115 stats.buffers_in_idle,
4116 stats.buffers_in_ready,
4117 stats.buffers_processed,
4118 stats.buffers_failed);
4119 }
Mike Isely681c7392007-11-26 01:48:52 -03004120 default: break;
4121 }
4122 return 0;
4123}
4124
4125
4126unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4127 char *buf,unsigned int acnt)
4128{
4129 unsigned int bcnt,ccnt,idx;
4130 bcnt = 0;
4131 LOCK_TAKE(hdw->big_lock);
4132 for (idx = 0; ; idx++) {
4133 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4134 if (!ccnt) break;
4135 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4136 if (!acnt) break;
4137 buf[0] = '\n'; ccnt = 1;
4138 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4139 }
4140 LOCK_GIVE(hdw->big_lock);
4141 return bcnt;
4142}
4143
4144
4145static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4146{
4147 char buf[128];
4148 unsigned int idx,ccnt;
4149
4150 for (idx = 0; ; idx++) {
4151 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4152 if (!ccnt) break;
4153 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4154 }
4155}
4156
4157
4158/* Evaluate and update the driver's current state, taking various actions
4159 as appropriate for the update. */
4160static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4161{
4162 unsigned int st;
4163 int state_updated = 0;
4164 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004165 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004166
4167 pvr2_trace(PVR2_TRACE_STBITS,
4168 "Drive state check START");
4169 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4170 pvr2_hdw_state_log_state(hdw);
4171 }
4172
4173 /* Process all state and get back over disposition */
4174 state_updated = pvr2_hdw_state_update(hdw);
4175
Mike Isely1b9c18c2008-04-22 14:45:41 -03004176 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4177
Mike Isely681c7392007-11-26 01:48:52 -03004178 /* Update master state based upon all other states. */
4179 if (!hdw->flag_ok) {
4180 st = PVR2_STATE_DEAD;
4181 } else if (hdw->fw1_state != FW1_STATE_OK) {
4182 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004183 } else if ((analog_mode ||
4184 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4185 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004186 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004187 } else if (hdw->flag_tripped ||
4188 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004189 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004190 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004191 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004192 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004193 st = PVR2_STATE_RUN;
4194 } else {
4195 st = PVR2_STATE_READY;
4196 }
4197 if (hdw->master_state != st) {
4198 pvr2_trace(PVR2_TRACE_STATE,
4199 "Device state change from %s to %s",
4200 pvr2_get_state_name(hdw->master_state),
4201 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004202 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004203 hdw->master_state = st;
4204 state_updated = !0;
4205 callback_flag = !0;
4206 }
4207 if (state_updated) {
4208 /* Trigger anyone waiting on any state changes here. */
4209 wake_up(&hdw->state_wait_data);
4210 }
4211
4212 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4213 pvr2_hdw_state_log_state(hdw);
4214 }
4215 pvr2_trace(PVR2_TRACE_STBITS,
4216 "Drive state check DONE callback=%d",callback_flag);
4217
4218 return callback_flag;
4219}
4220
4221
4222/* Cause kernel thread to check / update driver state */
4223static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
4224{
4225 if (hdw->state_stale) return;
4226 hdw->state_stale = !0;
4227 trace_stbit("state_stale",hdw->state_stale);
4228 queue_work(hdw->workqueue,&hdw->workpoll);
4229}
4230
4231
Mike Iselyd8554972006-06-26 20:58:46 -03004232int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
4233{
4234 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
4235}
4236
4237
4238int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
4239{
4240 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
4241}
4242
4243
4244int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
4245{
4246 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
4247}
4248
4249
4250int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
4251{
4252 u32 cval,nval;
4253 int ret;
4254 if (~msk) {
4255 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
4256 if (ret) return ret;
4257 nval = (cval & ~msk) | (val & msk);
4258 pvr2_trace(PVR2_TRACE_GPIO,
4259 "GPIO direction changing 0x%x:0x%x"
4260 " from 0x%x to 0x%x",
4261 msk,val,cval,nval);
4262 } else {
4263 nval = val;
4264 pvr2_trace(PVR2_TRACE_GPIO,
4265 "GPIO direction changing to 0x%x",nval);
4266 }
4267 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
4268}
4269
4270
4271int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4272{
4273 u32 cval,nval;
4274 int ret;
4275 if (~msk) {
4276 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4277 if (ret) return ret;
4278 nval = (cval & ~msk) | (val & msk);
4279 pvr2_trace(PVR2_TRACE_GPIO,
4280 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4281 msk,val,cval,nval);
4282 } else {
4283 nval = val;
4284 pvr2_trace(PVR2_TRACE_GPIO,
4285 "GPIO output changing to 0x%x",nval);
4286 }
4287 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4288}
4289
4290
Mike Isely7fb20fa2008-04-22 14:45:37 -03004291unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
4292{
4293 return hdw->input_avail_mask;
4294}
4295
4296
Mike Isely1cb03b72008-04-21 03:47:43 -03004297unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
4298{
4299 return hdw->input_allowed_mask;
4300}
4301
4302
4303static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
4304{
4305 if (hdw->input_val != v) {
4306 hdw->input_val = v;
4307 hdw->input_dirty = !0;
4308 }
4309
4310 /* Handle side effects - if we switch to a mode that needs the RF
4311 tuner, then select the right frequency choice as well and mark
4312 it dirty. */
4313 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
4314 hdw->freqSelector = 0;
4315 hdw->freqDirty = !0;
4316 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
4317 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
4318 hdw->freqSelector = 1;
4319 hdw->freqDirty = !0;
4320 }
4321 return 0;
4322}
4323
4324
4325int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
4326 unsigned int change_mask,
4327 unsigned int change_val)
4328{
4329 int ret = 0;
4330 unsigned int nv,m,idx;
4331 LOCK_TAKE(hdw->big_lock);
4332 do {
4333 nv = hdw->input_allowed_mask & ~change_mask;
4334 nv |= (change_val & change_mask);
4335 nv &= hdw->input_avail_mask;
4336 if (!nv) {
4337 /* No legal modes left; return error instead. */
4338 ret = -EPERM;
4339 break;
4340 }
4341 hdw->input_allowed_mask = nv;
4342 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
4343 /* Current mode is still in the allowed mask, so
4344 we're done. */
4345 break;
4346 }
4347 /* Select and switch to a mode that is still in the allowed
4348 mask */
4349 if (!hdw->input_allowed_mask) {
4350 /* Nothing legal; give up */
4351 break;
4352 }
4353 m = hdw->input_allowed_mask;
4354 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
4355 if (!((1 << idx) & m)) continue;
4356 pvr2_hdw_set_input(hdw,idx);
4357 break;
4358 }
4359 } while (0);
4360 LOCK_GIVE(hdw->big_lock);
4361 return ret;
4362}
4363
4364
Mike Iselye61b6fc2006-07-18 22:42:18 -03004365/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004366static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004367{
4368 int result;
4369 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03004370 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03004371 result = pvr2_send_request(hdw,
4372 hdw->cmd_buffer,1,
4373 hdw->cmd_buffer,1);
4374 if (result < 0) break;
4375 result = hdw->cmd_buffer[0];
4376 } while(0); LOCK_GIVE(hdw->ctl_lock);
4377 return result;
4378}
4379
4380
Mike Isely32ffa9a2006-09-23 22:26:52 -03004381int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004382 u32 match_type, u32 match_chip, u64 reg_id,
4383 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004384{
4385#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004386 struct pvr2_i2c_client *cp;
4387 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004388 int stat = 0;
4389 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004390
Mike Isely201f5c92007-01-28 16:08:36 -03004391 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4392
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004393 req.match_type = match_type;
4394 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004395 req.reg = reg_id;
4396 if (setFl) req.val = *val_ptr;
4397 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004398 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004399 if (!v4l2_chip_match_i2c_client(
4400 cp->client,
4401 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004402 continue;
4403 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004404 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004405 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4406 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004407 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004408 okFl = !0;
4409 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004410 }
4411 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004412 if (okFl) {
4413 return stat;
4414 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004415 return -EINVAL;
4416#else
4417 return -ENOSYS;
4418#endif
4419}
4420
4421
Mike Iselyd8554972006-06-26 20:58:46 -03004422/*
4423 Stuff for Emacs to see, in order to encourage consistent editing style:
4424 *** Local Variables: ***
4425 *** mode: c ***
4426 *** fill-column: 75 ***
4427 *** tab-width: 8 ***
4428 *** c-basic-offset: 8 ***
4429 *** End: ***
4430 */