blob: 7e7c570cf8309295a8c521536cbc22204f3b4c60 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/slab.h>
25#include <linux/firmware.h>
Mike Iselyd8554972006-06-26 20:58:46 -030026#include <linux/videodev2.h>
Mike Isely32ffa9a2006-09-23 22:26:52 -030027#include <media/v4l2-common.h>
Mike Iselyd8554972006-06-26 20:58:46 -030028#include "pvrusb2.h"
29#include "pvrusb2-std.h"
30#include "pvrusb2-util.h"
31#include "pvrusb2-hdw.h"
32#include "pvrusb2-i2c-core.h"
33#include "pvrusb2-tuner.h"
34#include "pvrusb2-eeprom.h"
35#include "pvrusb2-hdw-internal.h"
36#include "pvrusb2-encoder.h"
37#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030038#include "pvrusb2-fx2-cmd.h"
Mike Iselyd8554972006-06-26 20:58:46 -030039
Mike Isely1bde0282006-12-27 23:30:13 -030040#define TV_MIN_FREQ 55250000L
41#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030042
Mike Iselya0fd1cb2006-06-30 11:35:28 -030043static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030044static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030045
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030046static int ctlchg;
Mike Iselyd8554972006-06-26 20:58:46 -030047static int initusbreset = 1;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030048static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030049static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
50static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
51static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030052static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030053
54module_param(ctlchg, int, S_IRUGO|S_IWUSR);
55MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
56module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
57MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
58module_param(initusbreset, int, S_IRUGO|S_IWUSR);
59MODULE_PARM_DESC(initusbreset, "Do USB reset device on probe");
60module_param(procreload, int, S_IRUGO|S_IWUSR);
61MODULE_PARM_DESC(procreload,
62 "Attempt init failure recovery with firmware reload");
63module_param_array(tuner, int, NULL, 0444);
64MODULE_PARM_DESC(tuner,"specify installed tuner type");
65module_param_array(video_std, int, NULL, 0444);
66MODULE_PARM_DESC(video_std,"specify initial video standard");
67module_param_array(tolerance, int, NULL, 0444);
68MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
69
70#define PVR2_CTL_WRITE_ENDPOINT 0x01
71#define PVR2_CTL_READ_ENDPOINT 0x81
72
73#define PVR2_GPIO_IN 0x9008
74#define PVR2_GPIO_OUT 0x900c
75#define PVR2_GPIO_DIR 0x9020
76
77#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
78
79#define PVR2_FIRMWARE_ENDPOINT 0x02
80
81/* size of a firmware chunk */
82#define FIRMWARE_CHUNK_SIZE 0x2000
83
Mike Iselyb30d2442006-06-25 20:05:01 -030084/* Define the list of additional controls we'll dynamically construct based
85 on query of the cx2341x module. */
86struct pvr2_mpeg_ids {
87 const char *strid;
88 int id;
89};
90static const struct pvr2_mpeg_ids mpeg_ids[] = {
91 {
92 .strid = "audio_layer",
93 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
94 },{
95 .strid = "audio_bitrate",
96 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
97 },{
98 /* Already using audio_mode elsewhere :-( */
99 .strid = "mpeg_audio_mode",
100 .id = V4L2_CID_MPEG_AUDIO_MODE,
101 },{
102 .strid = "mpeg_audio_mode_extension",
103 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
104 },{
105 .strid = "audio_emphasis",
106 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
107 },{
108 .strid = "audio_crc",
109 .id = V4L2_CID_MPEG_AUDIO_CRC,
110 },{
111 .strid = "video_aspect",
112 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
113 },{
114 .strid = "video_b_frames",
115 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
116 },{
117 .strid = "video_gop_size",
118 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
119 },{
120 .strid = "video_gop_closure",
121 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
122 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300123 .strid = "video_bitrate_mode",
124 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
125 },{
126 .strid = "video_bitrate",
127 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
128 },{
129 .strid = "video_bitrate_peak",
130 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
131 },{
132 .strid = "video_temporal_decimation",
133 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
134 },{
135 .strid = "stream_type",
136 .id = V4L2_CID_MPEG_STREAM_TYPE,
137 },{
138 .strid = "video_spatial_filter_mode",
139 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
140 },{
141 .strid = "video_spatial_filter",
142 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
143 },{
144 .strid = "video_luma_spatial_filter_type",
145 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
146 },{
147 .strid = "video_chroma_spatial_filter_type",
148 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
149 },{
150 .strid = "video_temporal_filter_mode",
151 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
152 },{
153 .strid = "video_temporal_filter",
154 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
155 },{
156 .strid = "video_median_filter_type",
157 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
158 },{
159 .strid = "video_luma_median_filter_top",
160 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
161 },{
162 .strid = "video_luma_median_filter_bottom",
163 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
164 },{
165 .strid = "video_chroma_median_filter_top",
166 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
167 },{
168 .strid = "video_chroma_median_filter_bottom",
169 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
170 }
171};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300172#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300173
Mike Iselyd8554972006-06-26 20:58:46 -0300174
Mike Isely434449f2006-08-08 09:10:06 -0300175static const char *control_values_srate[] = {
176 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
177 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
178 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
179};
Mike Iselyd8554972006-06-26 20:58:46 -0300180
Mike Iselyd8554972006-06-26 20:58:46 -0300181
182
183static const char *control_values_input[] = {
184 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300185 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300186 [PVR2_CVAL_INPUT_RADIO] = "radio",
187 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
188 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
189};
190
191
192static const char *control_values_audiomode[] = {
193 [V4L2_TUNER_MODE_MONO] = "Mono",
194 [V4L2_TUNER_MODE_STEREO] = "Stereo",
195 [V4L2_TUNER_MODE_LANG1] = "Lang1",
196 [V4L2_TUNER_MODE_LANG2] = "Lang2",
197 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
198};
199
200
201static const char *control_values_hsm[] = {
202 [PVR2_CVAL_HSM_FAIL] = "Fail",
203 [PVR2_CVAL_HSM_HIGH] = "High",
204 [PVR2_CVAL_HSM_FULL] = "Full",
205};
206
207
Mike Isely681c7392007-11-26 01:48:52 -0300208static const char *pvr2_state_names[] = {
209 [PVR2_STATE_NONE] = "none",
210 [PVR2_STATE_DEAD] = "dead",
211 [PVR2_STATE_COLD] = "cold",
212 [PVR2_STATE_WARM] = "warm",
213 [PVR2_STATE_ERROR] = "error",
214 [PVR2_STATE_READY] = "ready",
215 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300216};
217
Mike Isely681c7392007-11-26 01:48:52 -0300218
219static void pvr2_hdw_state_sched(struct pvr2_hdw *);
220static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300221static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300222static void pvr2_hdw_worker_i2c(struct work_struct *work);
223static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300224static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
225static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
226static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300227static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300228static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300229static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300230static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
231static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300232static void pvr2_hdw_quiescent_timeout(unsigned long);
233static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300234static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
235 unsigned int timeout,int probe_fl,
236 void *write_data,unsigned int write_len,
237 void *read_data,unsigned int read_len);
Mike Iselyd8554972006-06-26 20:58:46 -0300238
Mike Isely681c7392007-11-26 01:48:52 -0300239
240static void trace_stbit(const char *name,int val)
241{
242 pvr2_trace(PVR2_TRACE_STBITS,
243 "State bit %s <-- %s",
244 name,(val ? "true" : "false"));
245}
246
Mike Iselyd8554972006-06-26 20:58:46 -0300247static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
248{
249 struct pvr2_hdw *hdw = cptr->hdw;
250 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
251 *vp = hdw->freqTable[hdw->freqProgSlot-1];
252 } else {
253 *vp = 0;
254 }
255 return 0;
256}
257
258static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
259{
260 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300261 unsigned int slotId = hdw->freqProgSlot;
262 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
263 hdw->freqTable[slotId-1] = v;
264 /* Handle side effects correctly - if we're tuned to this
265 slot, then forgot the slot id relation since the stored
266 frequency has been changed. */
267 if (hdw->freqSelector) {
268 if (hdw->freqSlotRadio == slotId) {
269 hdw->freqSlotRadio = 0;
270 }
271 } else {
272 if (hdw->freqSlotTelevision == slotId) {
273 hdw->freqSlotTelevision = 0;
274 }
275 }
Mike Iselyd8554972006-06-26 20:58:46 -0300276 }
277 return 0;
278}
279
280static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
281{
282 *vp = cptr->hdw->freqProgSlot;
283 return 0;
284}
285
286static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
287{
288 struct pvr2_hdw *hdw = cptr->hdw;
289 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
290 hdw->freqProgSlot = v;
291 }
292 return 0;
293}
294
295static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
296{
Mike Isely1bde0282006-12-27 23:30:13 -0300297 struct pvr2_hdw *hdw = cptr->hdw;
298 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300299 return 0;
300}
301
Mike Isely1bde0282006-12-27 23:30:13 -0300302static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300303{
304 unsigned freq = 0;
305 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300306 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
307 if (slotId > 0) {
308 freq = hdw->freqTable[slotId-1];
309 if (!freq) return 0;
310 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300311 }
Mike Isely1bde0282006-12-27 23:30:13 -0300312 if (hdw->freqSelector) {
313 hdw->freqSlotRadio = slotId;
314 } else {
315 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300316 }
317 return 0;
318}
319
320static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
321{
Mike Isely1bde0282006-12-27 23:30:13 -0300322 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300323 return 0;
324}
325
326static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
327{
328 return cptr->hdw->freqDirty != 0;
329}
330
331static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
332{
333 cptr->hdw->freqDirty = 0;
334}
335
336static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
337{
Mike Isely1bde0282006-12-27 23:30:13 -0300338 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300339 return 0;
340}
341
Mike Isely3ad9fc32006-09-02 22:37:52 -0300342static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
343{
344 /* Actual maximum depends on the video standard in effect. */
345 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
346 *vp = 480;
347 } else {
348 *vp = 576;
349 }
350 return 0;
351}
352
353static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
354{
Mike Isely989eb152007-11-26 01:53:12 -0300355 /* Actual minimum depends on device digitizer type. */
356 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300357 *vp = 75;
358 } else {
359 *vp = 17;
360 }
361 return 0;
362}
363
Mike Isely1bde0282006-12-27 23:30:13 -0300364static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
365{
366 *vp = cptr->hdw->input_val;
367 return 0;
368}
369
Mike Isely29bf5b12008-04-22 14:45:37 -0300370static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
371{
Mike Isely7fb20fa2008-04-22 14:45:37 -0300372 return ((1 << v) & cptr->hdw->input_avail_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300373}
374
Mike Isely1bde0282006-12-27 23:30:13 -0300375static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
376{
377 struct pvr2_hdw *hdw = cptr->hdw;
378
379 if (hdw->input_val != v) {
380 hdw->input_val = v;
381 hdw->input_dirty = !0;
382 }
383
384 /* Handle side effects - if we switch to a mode that needs the RF
385 tuner, then select the right frequency choice as well and mark
386 it dirty. */
387 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
388 hdw->freqSelector = 0;
389 hdw->freqDirty = !0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300390 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
391 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
Mike Isely1bde0282006-12-27 23:30:13 -0300392 hdw->freqSelector = 1;
393 hdw->freqDirty = !0;
394 }
395 return 0;
396}
397
398static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
399{
400 return cptr->hdw->input_dirty != 0;
401}
402
403static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
404{
405 cptr->hdw->input_dirty = 0;
406}
407
Mike Isely5549f542006-12-27 23:28:54 -0300408
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300409static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
410{
Mike Isely644afdb2007-01-20 00:19:23 -0300411 unsigned long fv;
412 struct pvr2_hdw *hdw = cptr->hdw;
413 if (hdw->tuner_signal_stale) {
414 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300415 }
Mike Isely644afdb2007-01-20 00:19:23 -0300416 fv = hdw->tuner_signal_info.rangehigh;
417 if (!fv) {
418 /* Safety fallback */
419 *vp = TV_MAX_FREQ;
420 return 0;
421 }
422 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
423 fv = (fv * 125) / 2;
424 } else {
425 fv = fv * 62500;
426 }
427 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300428 return 0;
429}
430
431static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
432{
Mike Isely644afdb2007-01-20 00:19:23 -0300433 unsigned long fv;
434 struct pvr2_hdw *hdw = cptr->hdw;
435 if (hdw->tuner_signal_stale) {
436 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300437 }
Mike Isely644afdb2007-01-20 00:19:23 -0300438 fv = hdw->tuner_signal_info.rangelow;
439 if (!fv) {
440 /* Safety fallback */
441 *vp = TV_MIN_FREQ;
442 return 0;
443 }
444 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
445 fv = (fv * 125) / 2;
446 } else {
447 fv = fv * 62500;
448 }
449 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300450 return 0;
451}
452
Mike Iselyb30d2442006-06-25 20:05:01 -0300453static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
454{
455 return cptr->hdw->enc_stale != 0;
456}
457
458static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
459{
460 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300461 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300462}
463
464static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
465{
466 int ret;
467 struct v4l2_ext_controls cs;
468 struct v4l2_ext_control c1;
469 memset(&cs,0,sizeof(cs));
470 memset(&c1,0,sizeof(c1));
471 cs.controls = &c1;
472 cs.count = 1;
473 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300474 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300475 VIDIOC_G_EXT_CTRLS);
476 if (ret) return ret;
477 *vp = c1.value;
478 return 0;
479}
480
481static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
482{
483 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300484 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300485 struct v4l2_ext_controls cs;
486 struct v4l2_ext_control c1;
487 memset(&cs,0,sizeof(cs));
488 memset(&c1,0,sizeof(c1));
489 cs.controls = &c1;
490 cs.count = 1;
491 c1.id = cptr->info->v4l_id;
492 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300493 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
494 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300495 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300496 if (ret == -EBUSY) {
497 /* Oops. cx2341x is telling us it's not safe to change
498 this control while we're capturing. Make a note of this
499 fact so that the pipeline will be stopped the next time
500 controls are committed. Then go on ahead and store this
501 change anyway. */
502 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
503 0, &cs,
504 VIDIOC_S_EXT_CTRLS);
505 if (!ret) hdw->enc_unsafe_stale = !0;
506 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300507 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300508 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300509 return 0;
510}
511
512static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
513{
514 struct v4l2_queryctrl qctrl;
515 struct pvr2_ctl_info *info;
516 qctrl.id = cptr->info->v4l_id;
517 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
518 /* Strip out the const so we can adjust a function pointer. It's
519 OK to do this here because we know this is a dynamically created
520 control, so the underlying storage for the info pointer is (a)
521 private to us, and (b) not in read-only storage. Either we do
522 this or we significantly complicate the underlying control
523 implementation. */
524 info = (struct pvr2_ctl_info *)(cptr->info);
525 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
526 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300527 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300528 }
529 } else {
530 if (!(info->set_value)) {
531 info->set_value = ctrl_cx2341x_set;
532 }
533 }
534 return qctrl.flags;
535}
536
Mike Iselyd8554972006-06-26 20:58:46 -0300537static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
538{
Mike Isely681c7392007-11-26 01:48:52 -0300539 *vp = cptr->hdw->state_pipeline_req;
540 return 0;
541}
542
543static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
544{
545 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300546 return 0;
547}
548
549static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
550{
551 int result = pvr2_hdw_is_hsm(cptr->hdw);
552 *vp = PVR2_CVAL_HSM_FULL;
553 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
554 if (result) *vp = PVR2_CVAL_HSM_HIGH;
555 return 0;
556}
557
558static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
559{
560 *vp = cptr->hdw->std_mask_avail;
561 return 0;
562}
563
564static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
565{
566 struct pvr2_hdw *hdw = cptr->hdw;
567 v4l2_std_id ns;
568 ns = hdw->std_mask_avail;
569 ns = (ns & ~m) | (v & m);
570 if (ns == hdw->std_mask_avail) return 0;
571 hdw->std_mask_avail = ns;
572 pvr2_hdw_internal_set_std_avail(hdw);
573 pvr2_hdw_internal_find_stdenum(hdw);
574 return 0;
575}
576
577static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
578 char *bufPtr,unsigned int bufSize,
579 unsigned int *len)
580{
581 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
582 return 0;
583}
584
585static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
586 const char *bufPtr,unsigned int bufSize,
587 int *mskp,int *valp)
588{
589 int ret;
590 v4l2_std_id id;
591 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
592 if (ret < 0) return ret;
593 if (mskp) *mskp = id;
594 if (valp) *valp = id;
595 return 0;
596}
597
598static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
599{
600 *vp = cptr->hdw->std_mask_cur;
601 return 0;
602}
603
604static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
605{
606 struct pvr2_hdw *hdw = cptr->hdw;
607 v4l2_std_id ns;
608 ns = hdw->std_mask_cur;
609 ns = (ns & ~m) | (v & m);
610 if (ns == hdw->std_mask_cur) return 0;
611 hdw->std_mask_cur = ns;
612 hdw->std_dirty = !0;
613 pvr2_hdw_internal_find_stdenum(hdw);
614 return 0;
615}
616
617static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
618{
619 return cptr->hdw->std_dirty != 0;
620}
621
622static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
623{
624 cptr->hdw->std_dirty = 0;
625}
626
627static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
628{
Mike Isely18103c572007-01-20 00:09:47 -0300629 struct pvr2_hdw *hdw = cptr->hdw;
630 pvr2_i2c_core_status_poll(hdw);
631 *vp = hdw->tuner_signal_info.signal;
632 return 0;
633}
634
635static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
636{
637 int val = 0;
638 unsigned int subchan;
639 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300640 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300641 subchan = hdw->tuner_signal_info.rxsubchans;
642 if (subchan & V4L2_TUNER_SUB_MONO) {
643 val |= (1 << V4L2_TUNER_MODE_MONO);
644 }
645 if (subchan & V4L2_TUNER_SUB_STEREO) {
646 val |= (1 << V4L2_TUNER_MODE_STEREO);
647 }
648 if (subchan & V4L2_TUNER_SUB_LANG1) {
649 val |= (1 << V4L2_TUNER_MODE_LANG1);
650 }
651 if (subchan & V4L2_TUNER_SUB_LANG2) {
652 val |= (1 << V4L2_TUNER_MODE_LANG2);
653 }
654 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300655 return 0;
656}
657
Mike Iselyd8554972006-06-26 20:58:46 -0300658
659static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
660{
661 struct pvr2_hdw *hdw = cptr->hdw;
662 if (v < 0) return -EINVAL;
663 if (v > hdw->std_enum_cnt) return -EINVAL;
664 hdw->std_enum_cur = v;
665 if (!v) return 0;
666 v--;
667 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
668 hdw->std_mask_cur = hdw->std_defs[v].id;
669 hdw->std_dirty = !0;
670 return 0;
671}
672
673
674static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
675{
676 *vp = cptr->hdw->std_enum_cur;
677 return 0;
678}
679
680
681static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
682{
683 return cptr->hdw->std_dirty != 0;
684}
685
686
687static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
688{
689 cptr->hdw->std_dirty = 0;
690}
691
692
693#define DEFINT(vmin,vmax) \
694 .type = pvr2_ctl_int, \
695 .def.type_int.min_value = vmin, \
696 .def.type_int.max_value = vmax
697
698#define DEFENUM(tab) \
699 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300700 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300701 .def.type_enum.value_names = tab
702
Mike Isely33213962006-06-25 20:04:40 -0300703#define DEFBOOL \
704 .type = pvr2_ctl_bool
705
Mike Iselyd8554972006-06-26 20:58:46 -0300706#define DEFMASK(msk,tab) \
707 .type = pvr2_ctl_bitmask, \
708 .def.type_bitmask.valid_bits = msk, \
709 .def.type_bitmask.bit_names = tab
710
711#define DEFREF(vname) \
712 .set_value = ctrl_set_##vname, \
713 .get_value = ctrl_get_##vname, \
714 .is_dirty = ctrl_isdirty_##vname, \
715 .clear_dirty = ctrl_cleardirty_##vname
716
717
718#define VCREATE_FUNCS(vname) \
719static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
720{*vp = cptr->hdw->vname##_val; return 0;} \
721static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
722{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
723static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
724{return cptr->hdw->vname##_dirty != 0;} \
725static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
726{cptr->hdw->vname##_dirty = 0;}
727
728VCREATE_FUNCS(brightness)
729VCREATE_FUNCS(contrast)
730VCREATE_FUNCS(saturation)
731VCREATE_FUNCS(hue)
732VCREATE_FUNCS(volume)
733VCREATE_FUNCS(balance)
734VCREATE_FUNCS(bass)
735VCREATE_FUNCS(treble)
736VCREATE_FUNCS(mute)
Mike Iselyc05c0462006-06-25 20:04:25 -0300737VCREATE_FUNCS(audiomode)
738VCREATE_FUNCS(res_hor)
739VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300740VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300741
Mike Iselyd8554972006-06-26 20:58:46 -0300742/* Table definition of all controls which can be manipulated */
743static const struct pvr2_ctl_info control_defs[] = {
744 {
745 .v4l_id = V4L2_CID_BRIGHTNESS,
746 .desc = "Brightness",
747 .name = "brightness",
748 .default_value = 128,
749 DEFREF(brightness),
750 DEFINT(0,255),
751 },{
752 .v4l_id = V4L2_CID_CONTRAST,
753 .desc = "Contrast",
754 .name = "contrast",
755 .default_value = 68,
756 DEFREF(contrast),
757 DEFINT(0,127),
758 },{
759 .v4l_id = V4L2_CID_SATURATION,
760 .desc = "Saturation",
761 .name = "saturation",
762 .default_value = 64,
763 DEFREF(saturation),
764 DEFINT(0,127),
765 },{
766 .v4l_id = V4L2_CID_HUE,
767 .desc = "Hue",
768 .name = "hue",
769 .default_value = 0,
770 DEFREF(hue),
771 DEFINT(-128,127),
772 },{
773 .v4l_id = V4L2_CID_AUDIO_VOLUME,
774 .desc = "Volume",
775 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -0300776 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -0300777 DEFREF(volume),
778 DEFINT(0,65535),
779 },{
780 .v4l_id = V4L2_CID_AUDIO_BALANCE,
781 .desc = "Balance",
782 .name = "balance",
783 .default_value = 0,
784 DEFREF(balance),
785 DEFINT(-32768,32767),
786 },{
787 .v4l_id = V4L2_CID_AUDIO_BASS,
788 .desc = "Bass",
789 .name = "bass",
790 .default_value = 0,
791 DEFREF(bass),
792 DEFINT(-32768,32767),
793 },{
794 .v4l_id = V4L2_CID_AUDIO_TREBLE,
795 .desc = "Treble",
796 .name = "treble",
797 .default_value = 0,
798 DEFREF(treble),
799 DEFINT(-32768,32767),
800 },{
801 .v4l_id = V4L2_CID_AUDIO_MUTE,
802 .desc = "Mute",
803 .name = "mute",
804 .default_value = 0,
805 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -0300806 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300807 },{
Mike Iselyc05c0462006-06-25 20:04:25 -0300808 .desc = "Video Source",
809 .name = "input",
810 .internal_id = PVR2_CID_INPUT,
811 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -0300812 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -0300813 DEFREF(input),
814 DEFENUM(control_values_input),
815 },{
816 .desc = "Audio Mode",
817 .name = "audio_mode",
818 .internal_id = PVR2_CID_AUDIOMODE,
819 .default_value = V4L2_TUNER_MODE_STEREO,
820 DEFREF(audiomode),
821 DEFENUM(control_values_audiomode),
822 },{
823 .desc = "Horizontal capture resolution",
824 .name = "resolution_hor",
825 .internal_id = PVR2_CID_HRES,
826 .default_value = 720,
827 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300828 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -0300829 },{
830 .desc = "Vertical capture resolution",
831 .name = "resolution_ver",
832 .internal_id = PVR2_CID_VRES,
833 .default_value = 480,
834 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300835 DEFINT(17,576),
836 /* Hook in check for video standard and adjust maximum
837 depending on the standard. */
838 .get_max_value = ctrl_vres_max_get,
839 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -0300840 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300841 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -0300842 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
843 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -0300844 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -0300845 DEFREF(srate),
846 DEFENUM(control_values_srate),
847 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300848 .desc = "Tuner Frequency (Hz)",
849 .name = "frequency",
850 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -0300851 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -0300852 .set_value = ctrl_freq_set,
853 .get_value = ctrl_freq_get,
854 .is_dirty = ctrl_freq_is_dirty,
855 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -0300856 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300857 /* Hook in check for input value (tv/radio) and adjust
858 max/min values accordingly */
859 .get_max_value = ctrl_freq_max_get,
860 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300861 },{
862 .desc = "Channel",
863 .name = "channel",
864 .set_value = ctrl_channel_set,
865 .get_value = ctrl_channel_get,
866 DEFINT(0,FREQTABLE_SIZE),
867 },{
868 .desc = "Channel Program Frequency",
869 .name = "freq_table_value",
870 .set_value = ctrl_channelfreq_set,
871 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -0300872 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -0300873 /* Hook in check for input value (tv/radio) and adjust
874 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -0300875 .get_max_value = ctrl_freq_max_get,
876 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300877 },{
878 .desc = "Channel Program ID",
879 .name = "freq_table_channel",
880 .set_value = ctrl_channelprog_set,
881 .get_value = ctrl_channelprog_get,
882 DEFINT(0,FREQTABLE_SIZE),
883 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300884 .desc = "Streaming Enabled",
885 .name = "streaming_enabled",
886 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -0300887 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300888 },{
889 .desc = "USB Speed",
890 .name = "usb_speed",
891 .get_value = ctrl_hsm_get,
892 DEFENUM(control_values_hsm),
893 },{
Mike Isely681c7392007-11-26 01:48:52 -0300894 .desc = "Master State",
895 .name = "master_state",
896 .get_value = ctrl_masterstate_get,
897 DEFENUM(pvr2_state_names),
898 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300899 .desc = "Signal Present",
900 .name = "signal_present",
901 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -0300902 DEFINT(0,65535),
903 },{
904 .desc = "Audio Modes Present",
905 .name = "audio_modes_present",
906 .get_value = ctrl_audio_modes_present_get,
907 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
908 v4l. Nothing outside of this module cares about this,
909 but I reuse it in order to also reuse the
910 control_values_audiomode string table. */
911 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
912 (1 << V4L2_TUNER_MODE_STEREO)|
913 (1 << V4L2_TUNER_MODE_LANG1)|
914 (1 << V4L2_TUNER_MODE_LANG2)),
915 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -0300916 },{
917 .desc = "Video Standards Available Mask",
918 .name = "video_standard_mask_available",
919 .internal_id = PVR2_CID_STDAVAIL,
920 .skip_init = !0,
921 .get_value = ctrl_stdavail_get,
922 .set_value = ctrl_stdavail_set,
923 .val_to_sym = ctrl_std_val_to_sym,
924 .sym_to_val = ctrl_std_sym_to_val,
925 .type = pvr2_ctl_bitmask,
926 },{
927 .desc = "Video Standards In Use Mask",
928 .name = "video_standard_mask_active",
929 .internal_id = PVR2_CID_STDCUR,
930 .skip_init = !0,
931 .get_value = ctrl_stdcur_get,
932 .set_value = ctrl_stdcur_set,
933 .is_dirty = ctrl_stdcur_is_dirty,
934 .clear_dirty = ctrl_stdcur_clear_dirty,
935 .val_to_sym = ctrl_std_val_to_sym,
936 .sym_to_val = ctrl_std_sym_to_val,
937 .type = pvr2_ctl_bitmask,
938 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300939 .desc = "Video Standard Name",
940 .name = "video_standard",
941 .internal_id = PVR2_CID_STDENUM,
942 .skip_init = !0,
943 .get_value = ctrl_stdenumcur_get,
944 .set_value = ctrl_stdenumcur_set,
945 .is_dirty = ctrl_stdenumcur_is_dirty,
946 .clear_dirty = ctrl_stdenumcur_clear_dirty,
947 .type = pvr2_ctl_enum,
948 }
949};
950
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300951#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -0300952
953
954const char *pvr2_config_get_name(enum pvr2_config cfg)
955{
956 switch (cfg) {
957 case pvr2_config_empty: return "empty";
958 case pvr2_config_mpeg: return "mpeg";
959 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -0300960 case pvr2_config_pcm: return "pcm";
961 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -0300962 }
963 return "<unknown>";
964}
965
966
967struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
968{
969 return hdw->usb_dev;
970}
971
972
973unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
974{
975 return hdw->serial_number;
976}
977
Mike Isely31a18542007-04-08 01:11:47 -0300978
979const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
980{
981 return hdw->bus_info;
982}
983
984
Mike Isely1bde0282006-12-27 23:30:13 -0300985unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
986{
987 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
988}
989
990/* Set the currently tuned frequency and account for all possible
991 driver-core side effects of this action. */
992void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
993{
Mike Isely7c74e572007-01-20 00:15:41 -0300994 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -0300995 if (hdw->freqSelector) {
996 /* Swing over to radio frequency selection */
997 hdw->freqSelector = 0;
998 hdw->freqDirty = !0;
999 }
Mike Isely1bde0282006-12-27 23:30:13 -03001000 if (hdw->freqValRadio != val) {
1001 hdw->freqValRadio = val;
1002 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001003 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001004 }
Mike Isely7c74e572007-01-20 00:15:41 -03001005 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001006 if (!(hdw->freqSelector)) {
1007 /* Swing over to television frequency selection */
1008 hdw->freqSelector = 1;
1009 hdw->freqDirty = !0;
1010 }
Mike Isely1bde0282006-12-27 23:30:13 -03001011 if (hdw->freqValTelevision != val) {
1012 hdw->freqValTelevision = val;
1013 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001014 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001015 }
Mike Isely1bde0282006-12-27 23:30:13 -03001016 }
1017}
1018
Mike Iselyd8554972006-06-26 20:58:46 -03001019int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1020{
1021 return hdw->unit_number;
1022}
1023
1024
1025/* Attempt to locate one of the given set of files. Messages are logged
1026 appropriate to what has been found. The return value will be 0 or
1027 greater on success (it will be the index of the file name found) and
1028 fw_entry will be filled in. Otherwise a negative error is returned on
1029 failure. If the return value is -ENOENT then no viable firmware file
1030 could be located. */
1031static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1032 const struct firmware **fw_entry,
1033 const char *fwtypename,
1034 unsigned int fwcount,
1035 const char *fwnames[])
1036{
1037 unsigned int idx;
1038 int ret = -EINVAL;
1039 for (idx = 0; idx < fwcount; idx++) {
1040 ret = request_firmware(fw_entry,
1041 fwnames[idx],
1042 &hdw->usb_dev->dev);
1043 if (!ret) {
1044 trace_firmware("Located %s firmware: %s;"
1045 " uploading...",
1046 fwtypename,
1047 fwnames[idx]);
1048 return idx;
1049 }
1050 if (ret == -ENOENT) continue;
1051 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1052 "request_firmware fatal error with code=%d",ret);
1053 return ret;
1054 }
1055 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1056 "***WARNING***"
1057 " Device %s firmware"
1058 " seems to be missing.",
1059 fwtypename);
1060 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1061 "Did you install the pvrusb2 firmware files"
1062 " in their proper location?");
1063 if (fwcount == 1) {
1064 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1065 "request_firmware unable to locate %s file %s",
1066 fwtypename,fwnames[0]);
1067 } else {
1068 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1069 "request_firmware unable to locate"
1070 " one of the following %s files:",
1071 fwtypename);
1072 for (idx = 0; idx < fwcount; idx++) {
1073 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1074 "request_firmware: Failed to find %s",
1075 fwnames[idx]);
1076 }
1077 }
1078 return ret;
1079}
1080
1081
1082/*
1083 * pvr2_upload_firmware1().
1084 *
1085 * Send the 8051 firmware to the device. After the upload, arrange for
1086 * device to re-enumerate.
1087 *
1088 * NOTE : the pointer to the firmware data given by request_firmware()
1089 * is not suitable for an usb transaction.
1090 *
1091 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001092static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001093{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001094 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001095 void *fw_ptr;
1096 unsigned int pipe;
1097 int ret;
1098 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001099
Mike Isely989eb152007-11-26 01:53:12 -03001100 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001101 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001102 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1103 "Connected device type defines"
1104 " no firmware to upload; ignoring firmware");
1105 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001106 }
1107
Mike Iselyd8554972006-06-26 20:58:46 -03001108 hdw->fw1_state = FW1_STATE_FAILED; // default result
1109
1110 trace_firmware("pvr2_upload_firmware1");
1111
1112 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001113 hdw->hdw_desc->fx2_firmware.cnt,
1114 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001115 if (ret < 0) {
1116 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1117 return ret;
1118 }
1119
1120 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1121 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1122
1123 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1124
1125 if (fw_entry->size != 0x2000){
1126 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1127 release_firmware(fw_entry);
1128 return -ENOMEM;
1129 }
1130
1131 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1132 if (fw_ptr == NULL){
1133 release_firmware(fw_entry);
1134 return -ENOMEM;
1135 }
1136
1137 /* We have to hold the CPU during firmware upload. */
1138 pvr2_hdw_cpureset_assert(hdw,1);
1139
1140 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1141 chunk. */
1142
1143 ret = 0;
1144 for(address = 0; address < fw_entry->size; address += 0x800) {
1145 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1146 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1147 0, fw_ptr, 0x800, HZ);
1148 }
1149
1150 trace_firmware("Upload done, releasing device's CPU");
1151
1152 /* Now release the CPU. It will disconnect and reconnect later. */
1153 pvr2_hdw_cpureset_assert(hdw,0);
1154
1155 kfree(fw_ptr);
1156 release_firmware(fw_entry);
1157
1158 trace_firmware("Upload done (%d bytes sent)",ret);
1159
1160 /* We should have written 8192 bytes */
1161 if (ret == 8192) {
1162 hdw->fw1_state = FW1_STATE_RELOAD;
1163 return 0;
1164 }
1165
1166 return -EIO;
1167}
1168
1169
1170/*
1171 * pvr2_upload_firmware2()
1172 *
1173 * This uploads encoder firmware on endpoint 2.
1174 *
1175 */
1176
1177int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1178{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001179 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001180 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001181 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001182 int actual_length;
1183 int ret = 0;
1184 int fwidx;
1185 static const char *fw_files[] = {
1186 CX2341X_FIRM_ENC_FILENAME,
1187 };
1188
Mike Isely989eb152007-11-26 01:53:12 -03001189 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001190 return 0;
1191 }
1192
Mike Iselyd8554972006-06-26 20:58:46 -03001193 trace_firmware("pvr2_upload_firmware2");
1194
1195 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001196 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001197 if (ret < 0) return ret;
1198 fwidx = ret;
1199 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001200 /* Since we're about to completely reinitialize the encoder,
1201 invalidate our cached copy of its configuration state. Next
1202 time we configure the encoder, then we'll fully configure it. */
1203 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001204
1205 /* First prepare firmware loading */
1206 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1207 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1208 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1209 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1210 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1211 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1212 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1213 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1214 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1215 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1216 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1217 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1218 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1219 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1220 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1221 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely567d7112007-01-28 15:38:55 -03001222 LOCK_TAKE(hdw->ctl_lock); do {
1223 hdw->cmd_buffer[0] = FX2CMD_FWPOST1;
Al Viro89952d12007-03-14 09:17:59 +00001224 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001225 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1226 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001227 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001228 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001229
1230 if (ret) {
1231 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1232 "firmware2 upload prep failed, ret=%d",ret);
1233 release_firmware(fw_entry);
1234 return ret;
1235 }
1236
1237 /* Now send firmware */
1238
1239 fw_len = fw_entry->size;
1240
Mike Isely90060d32007-02-08 02:02:53 -03001241 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001242 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1243 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001244 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001245 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001246 release_firmware(fw_entry);
1247 return -1;
1248 }
1249
1250 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1251 if (fw_ptr == NULL){
1252 release_firmware(fw_entry);
1253 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1254 "failed to allocate memory for firmware2 upload");
1255 return -ENOMEM;
1256 }
1257
1258 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1259
Mike Isely90060d32007-02-08 02:02:53 -03001260 fw_done = 0;
1261 for (fw_done = 0; fw_done < fw_len;) {
1262 bcnt = fw_len - fw_done;
1263 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1264 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1265 /* Usbsnoop log shows that we must swap bytes... */
1266 for (icnt = 0; icnt < bcnt/4 ; icnt++)
1267 ((u32 *)fw_ptr)[icnt] =
1268 ___swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001269
Mike Isely90060d32007-02-08 02:02:53 -03001270 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001271 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001272 ret |= (actual_length != bcnt);
1273 if (ret) break;
1274 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001275 }
1276
1277 trace_firmware("upload of %s : %i / %i ",
1278 fw_files[fwidx],fw_done,fw_len);
1279
1280 kfree(fw_ptr);
1281 release_firmware(fw_entry);
1282
1283 if (ret) {
1284 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1285 "firmware2 upload transfer failure");
1286 return ret;
1287 }
1288
1289 /* Finish upload */
1290
1291 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1292 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely567d7112007-01-28 15:38:55 -03001293 LOCK_TAKE(hdw->ctl_lock); do {
1294 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1295 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001296 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001297 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001298
1299 if (ret) {
1300 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1301 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001302 }
1303 return ret;
1304}
1305
1306
Mike Isely681c7392007-11-26 01:48:52 -03001307static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001308{
Mike Isely681c7392007-11-26 01:48:52 -03001309 if (st < ARRAY_SIZE(pvr2_state_names)) {
1310 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001311 }
Mike Isely681c7392007-11-26 01:48:52 -03001312 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001313}
1314
Mike Isely681c7392007-11-26 01:48:52 -03001315static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001316{
Mike Isely681c7392007-11-26 01:48:52 -03001317 if (!hdw->decoder_ctrl) {
1318 if (!hdw->flag_decoder_missed) {
1319 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1320 "WARNING: No decoder present");
1321 hdw->flag_decoder_missed = !0;
1322 trace_stbit("flag_decoder_missed",
1323 hdw->flag_decoder_missed);
1324 }
1325 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001326 }
Mike Isely681c7392007-11-26 01:48:52 -03001327 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001328 return 0;
1329}
1330
1331
Mike Isely681c7392007-11-26 01:48:52 -03001332void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1333{
1334 if (hdw->decoder_ctrl == ptr) return;
1335 hdw->decoder_ctrl = ptr;
1336 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1337 hdw->flag_decoder_missed = 0;
1338 trace_stbit("flag_decoder_missed",
1339 hdw->flag_decoder_missed);
1340 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1341 "Decoder has appeared");
1342 pvr2_hdw_state_sched(hdw);
1343 }
1344}
1345
1346
1347int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1348{
1349 return hdw->master_state;
1350}
1351
1352
1353static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1354{
1355 if (!hdw->flag_tripped) return 0;
1356 hdw->flag_tripped = 0;
1357 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1358 "Clearing driver error statuss");
1359 return !0;
1360}
1361
1362
1363int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1364{
1365 int fl;
1366 LOCK_TAKE(hdw->big_lock); do {
1367 fl = pvr2_hdw_untrip_unlocked(hdw);
1368 } while (0); LOCK_GIVE(hdw->big_lock);
1369 if (fl) pvr2_hdw_state_sched(hdw);
1370 return 0;
1371}
1372
1373
1374const char *pvr2_hdw_get_state_name(unsigned int id)
1375{
1376 if (id >= ARRAY_SIZE(pvr2_state_names)) return NULL;
1377 return pvr2_state_names[id];
1378}
1379
1380
Mike Iselyd8554972006-06-26 20:58:46 -03001381int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1382{
Mike Isely681c7392007-11-26 01:48:52 -03001383 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001384}
1385
1386
1387int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1388{
Mike Isely681c7392007-11-26 01:48:52 -03001389 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001390 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001391 pvr2_hdw_untrip_unlocked(hdw);
1392 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1393 hdw->state_pipeline_req = enable_flag != 0;
1394 pvr2_trace(PVR2_TRACE_START_STOP,
1395 "/*--TRACE_STREAM--*/ %s",
1396 enable_flag ? "enable" : "disable");
1397 }
1398 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001399 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001400 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1401 if (enable_flag) {
1402 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1403 if (st != PVR2_STATE_READY) return -EIO;
1404 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1405 }
1406 }
Mike Iselyd8554972006-06-26 20:58:46 -03001407 return 0;
1408}
1409
1410
1411int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1412{
Mike Isely681c7392007-11-26 01:48:52 -03001413 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001414 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001415 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1416 hdw->desired_stream_type = config;
1417 hdw->state_pipeline_config = 0;
1418 trace_stbit("state_pipeline_config",
1419 hdw->state_pipeline_config);
1420 pvr2_hdw_state_sched(hdw);
1421 }
Mike Iselyd8554972006-06-26 20:58:46 -03001422 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001423 if (fl) return 0;
1424 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001425}
1426
1427
1428static int get_default_tuner_type(struct pvr2_hdw *hdw)
1429{
1430 int unit_number = hdw->unit_number;
1431 int tp = -1;
1432 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1433 tp = tuner[unit_number];
1434 }
1435 if (tp < 0) return -EINVAL;
1436 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001437 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001438 return 0;
1439}
1440
1441
1442static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1443{
1444 int unit_number = hdw->unit_number;
1445 int tp = 0;
1446 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1447 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001448 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001449 }
Mike Isely6a540252007-12-02 23:51:34 -03001450 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001451}
1452
1453
1454static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1455{
1456 int unit_number = hdw->unit_number;
1457 int tp = 0;
1458 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1459 tp = tolerance[unit_number];
1460 }
1461 return tp;
1462}
1463
1464
1465static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1466{
1467 /* Try a harmless request to fetch the eeprom's address over
1468 endpoint 1. See what happens. Only the full FX2 image can
1469 respond to this. If this probe fails then likely the FX2
1470 firmware needs be loaded. */
1471 int result;
1472 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001473 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001474 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1475 hdw->cmd_buffer,1,
1476 hdw->cmd_buffer,1);
1477 if (result < 0) break;
1478 } while(0); LOCK_GIVE(hdw->ctl_lock);
1479 if (result) {
1480 pvr2_trace(PVR2_TRACE_INIT,
1481 "Probe of device endpoint 1 result status %d",
1482 result);
1483 } else {
1484 pvr2_trace(PVR2_TRACE_INIT,
1485 "Probe of device endpoint 1 succeeded");
1486 }
1487 return result == 0;
1488}
1489
Mike Isely9f66d4e2007-09-08 22:28:51 -03001490struct pvr2_std_hack {
1491 v4l2_std_id pat; /* Pattern to match */
1492 v4l2_std_id msk; /* Which bits we care about */
1493 v4l2_std_id std; /* What additional standards or default to set */
1494};
1495
1496/* This data structure labels specific combinations of standards from
1497 tveeprom that we'll try to recognize. If we recognize one, then assume
1498 a specified default standard to use. This is here because tveeprom only
1499 tells us about available standards not the intended default standard (if
1500 any) for the device in question. We guess the default based on what has
1501 been reported as available. Note that this is only for guessing a
1502 default - which can always be overridden explicitly - and if the user
1503 has otherwise named a default then that default will always be used in
1504 place of this table. */
1505const static struct pvr2_std_hack std_eeprom_maps[] = {
1506 { /* PAL(B/G) */
1507 .pat = V4L2_STD_B|V4L2_STD_GH,
1508 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1509 },
1510 { /* NTSC(M) */
1511 .pat = V4L2_STD_MN,
1512 .std = V4L2_STD_NTSC_M,
1513 },
1514 { /* PAL(I) */
1515 .pat = V4L2_STD_PAL_I,
1516 .std = V4L2_STD_PAL_I,
1517 },
1518 { /* SECAM(L/L') */
1519 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1520 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1521 },
1522 { /* PAL(D/D1/K) */
1523 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001524 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001525 },
1526};
1527
Mike Iselyd8554972006-06-26 20:58:46 -03001528static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1529{
1530 char buf[40];
1531 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001532 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001533
1534 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001535 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001536
1537 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001538 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001539 "Supported video standard(s) reported available"
1540 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001541 bcnt,buf);
1542
1543 hdw->std_mask_avail = hdw->std_mask_eeprom;
1544
Mike Isely3d290bd2007-12-03 01:47:12 -03001545 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001546 if (std2) {
1547 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001548 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001549 "Expanding supported video standards"
1550 " to include: %.*s",
1551 bcnt,buf);
1552 hdw->std_mask_avail |= std2;
1553 }
1554
1555 pvr2_hdw_internal_set_std_avail(hdw);
1556
1557 if (std1) {
1558 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001559 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001560 "Initial video standard forced to %.*s",
1561 bcnt,buf);
1562 hdw->std_mask_cur = std1;
1563 hdw->std_dirty = !0;
1564 pvr2_hdw_internal_find_stdenum(hdw);
1565 return;
1566 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001567 if (std3) {
1568 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1569 pvr2_trace(PVR2_TRACE_STD,
1570 "Initial video standard"
1571 " (determined by device type): %.*s",bcnt,buf);
1572 hdw->std_mask_cur = std3;
1573 hdw->std_dirty = !0;
1574 pvr2_hdw_internal_find_stdenum(hdw);
1575 return;
1576 }
Mike Iselyd8554972006-06-26 20:58:46 -03001577
Mike Isely9f66d4e2007-09-08 22:28:51 -03001578 {
1579 unsigned int idx;
1580 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1581 if (std_eeprom_maps[idx].msk ?
1582 ((std_eeprom_maps[idx].pat ^
1583 hdw->std_mask_eeprom) &
1584 std_eeprom_maps[idx].msk) :
1585 (std_eeprom_maps[idx].pat !=
1586 hdw->std_mask_eeprom)) continue;
1587 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1588 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001589 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001590 "Initial video standard guessed as %.*s",
1591 bcnt,buf);
1592 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1593 hdw->std_dirty = !0;
1594 pvr2_hdw_internal_find_stdenum(hdw);
1595 return;
1596 }
1597 }
1598
Mike Iselyd8554972006-06-26 20:58:46 -03001599 if (hdw->std_enum_cnt > 1) {
1600 // Autoselect the first listed standard
1601 hdw->std_enum_cur = 1;
1602 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1603 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001604 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001605 "Initial video standard auto-selected to %s",
1606 hdw->std_defs[hdw->std_enum_cur-1].name);
1607 return;
1608 }
1609
Mike Isely0885ba12006-06-25 21:30:47 -03001610 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001611 "Unable to select a viable initial video standard");
1612}
1613
1614
1615static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1616{
1617 int ret;
1618 unsigned int idx;
1619 struct pvr2_ctrl *cptr;
1620 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001621 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001622 if (!reloadFl) {
1623 reloadFl =
1624 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1625 == 0);
1626 if (reloadFl) {
1627 pvr2_trace(PVR2_TRACE_INIT,
1628 "USB endpoint config looks strange"
1629 "; possibly firmware needs to be"
1630 " loaded");
1631 }
1632 }
1633 if (!reloadFl) {
1634 reloadFl = !pvr2_hdw_check_firmware(hdw);
1635 if (reloadFl) {
1636 pvr2_trace(PVR2_TRACE_INIT,
1637 "Check for FX2 firmware failed"
1638 "; possibly firmware needs to be"
1639 " loaded");
1640 }
1641 }
Mike Iselyd8554972006-06-26 20:58:46 -03001642 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001643 if (pvr2_upload_firmware1(hdw) != 0) {
1644 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1645 "Failure uploading firmware1");
1646 }
1647 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001648 }
1649 }
Mike Iselyd8554972006-06-26 20:58:46 -03001650 hdw->fw1_state = FW1_STATE_OK;
1651
1652 if (initusbreset) {
1653 pvr2_hdw_device_reset(hdw);
1654 }
1655 if (!pvr2_hdw_dev_ok(hdw)) return;
1656
Mike Isely989eb152007-11-26 01:53:12 -03001657 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1658 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001659 }
1660
Mike Isely989eb152007-11-26 01:53:12 -03001661 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001662 pvr2_hdw_cmd_powerup(hdw);
1663 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001664 }
1665
1666 // This step MUST happen after the earlier powerup step.
1667 pvr2_i2c_core_init(hdw);
1668 if (!pvr2_hdw_dev_ok(hdw)) return;
1669
Mike Iselyc05c0462006-06-25 20:04:25 -03001670 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001671 cptr = hdw->controls + idx;
1672 if (cptr->info->skip_init) continue;
1673 if (!cptr->info->set_value) continue;
1674 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1675 }
1676
Mike Isely1bde0282006-12-27 23:30:13 -03001677 /* Set up special default values for the television and radio
1678 frequencies here. It's not really important what these defaults
1679 are, but I set them to something usable in the Chicago area just
1680 to make driver testing a little easier. */
1681
1682 /* US Broadcast channel 7 (175.25 MHz) */
1683 hdw->freqValTelevision = 175250000L;
1684 /* 104.3 MHz, a usable FM station for my area */
1685 hdw->freqValRadio = 104300000L;
1686
Mike Iselyd8554972006-06-26 20:58:46 -03001687 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1688 // thread-safe against the normal pvr2_send_request() mechanism.
1689 // (We should make it thread safe).
1690
Mike Iselyaaf78842007-11-26 02:04:11 -03001691 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1692 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001693 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03001694 if (ret < 0) {
1695 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1696 "Unable to determine location of eeprom,"
1697 " skipping");
1698 } else {
1699 hdw->eeprom_addr = ret;
1700 pvr2_eeprom_analyze(hdw);
1701 if (!pvr2_hdw_dev_ok(hdw)) return;
1702 }
1703 } else {
1704 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1705 hdw->tuner_updated = !0;
1706 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03001707 }
1708
1709 pvr2_hdw_setup_std(hdw);
1710
1711 if (!get_default_tuner_type(hdw)) {
1712 pvr2_trace(PVR2_TRACE_INIT,
1713 "pvr2_hdw_setup: Tuner type overridden to %d",
1714 hdw->tuner_type);
1715 }
1716
Mike Iselyd8554972006-06-26 20:58:46 -03001717 pvr2_i2c_core_check_stale(hdw);
1718 hdw->tuner_updated = 0;
1719
1720 if (!pvr2_hdw_dev_ok(hdw)) return;
1721
Mike Isely681c7392007-11-26 01:48:52 -03001722 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001723
1724 hdw->vid_stream = pvr2_stream_create();
1725 if (!pvr2_hdw_dev_ok(hdw)) return;
1726 pvr2_trace(PVR2_TRACE_INIT,
1727 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1728 if (hdw->vid_stream) {
1729 idx = get_default_error_tolerance(hdw);
1730 if (idx) {
1731 pvr2_trace(PVR2_TRACE_INIT,
1732 "pvr2_hdw_setup: video stream %p"
1733 " setting tolerance %u",
1734 hdw->vid_stream,idx);
1735 }
1736 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1737 PVR2_VID_ENDPOINT,idx);
1738 }
1739
1740 if (!pvr2_hdw_dev_ok(hdw)) return;
1741
Mike Iselyd8554972006-06-26 20:58:46 -03001742 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03001743
1744 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001745}
1746
1747
Mike Isely681c7392007-11-26 01:48:52 -03001748/* Set up the structure and attempt to put the device into a usable state.
1749 This can be a time-consuming operation, which is why it is not done
1750 internally as part of the create() step. */
1751static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001752{
1753 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03001754 do {
Mike Iselyd8554972006-06-26 20:58:46 -03001755 pvr2_hdw_setup_low(hdw);
1756 pvr2_trace(PVR2_TRACE_INIT,
1757 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03001758 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03001759 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03001760 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03001761 pvr2_trace(
1762 PVR2_TRACE_INFO,
1763 "Device initialization"
1764 " completed successfully.");
1765 break;
1766 }
1767 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1768 pvr2_trace(
1769 PVR2_TRACE_INFO,
1770 "Device microcontroller firmware"
1771 " (re)loaded; it should now reset"
1772 " and reconnect.");
1773 break;
1774 }
1775 pvr2_trace(
1776 PVR2_TRACE_ERROR_LEGS,
1777 "Device initialization was not successful.");
1778 if (hdw->fw1_state == FW1_STATE_MISSING) {
1779 pvr2_trace(
1780 PVR2_TRACE_ERROR_LEGS,
1781 "Giving up since device"
1782 " microcontroller firmware"
1783 " appears to be missing.");
1784 break;
1785 }
1786 }
1787 if (procreload) {
1788 pvr2_trace(
1789 PVR2_TRACE_ERROR_LEGS,
1790 "Attempting pvrusb2 recovery by reloading"
1791 " primary firmware.");
1792 pvr2_trace(
1793 PVR2_TRACE_ERROR_LEGS,
1794 "If this works, device should disconnect"
1795 " and reconnect in a sane state.");
1796 hdw->fw1_state = FW1_STATE_UNKNOWN;
1797 pvr2_upload_firmware1(hdw);
1798 } else {
1799 pvr2_trace(
1800 PVR2_TRACE_ERROR_LEGS,
1801 "***WARNING*** pvrusb2 device hardware"
1802 " appears to be jammed"
1803 " and I can't clear it.");
1804 pvr2_trace(
1805 PVR2_TRACE_ERROR_LEGS,
1806 "You might need to power cycle"
1807 " the pvrusb2 device"
1808 " in order to recover.");
1809 }
Mike Isely681c7392007-11-26 01:48:52 -03001810 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03001811 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001812}
1813
1814
Mike Iselyc4a88282008-04-22 14:45:44 -03001815/* Perform second stage initialization. Set callback pointer first so that
1816 we can avoid a possible initialization race (if the kernel thread runs
1817 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03001818int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
1819 void (*callback_func)(void *),
1820 void *callback_data)
Mike Iselyc4a88282008-04-22 14:45:44 -03001821{
1822 LOCK_TAKE(hdw->big_lock); do {
1823 hdw->state_data = callback_data;
1824 hdw->state_func = callback_func;
1825 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03001826 pvr2_hdw_setup(hdw);
1827 return hdw->flag_init_ok;
Mike Iselyc4a88282008-04-22 14:45:44 -03001828}
1829
1830
1831/* Create, set up, and return a structure for interacting with the
1832 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03001833struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1834 const struct usb_device_id *devid)
1835{
Mike Isely7fb20fa2008-04-22 14:45:37 -03001836 unsigned int idx,cnt1,cnt2,m;
Mike Iselyd8554972006-06-26 20:58:46 -03001837 struct pvr2_hdw *hdw;
Mike Iselyd8554972006-06-26 20:58:46 -03001838 int valid_std_mask;
1839 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03001840 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03001841 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03001842 struct v4l2_queryctrl qctrl;
1843 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03001844
Mike Iselyd130fa82007-12-08 17:20:06 -03001845 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03001846
Mike Iselyca545f72007-01-20 00:37:11 -03001847 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001848 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03001849 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03001850 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03001851
1852 init_timer(&hdw->quiescent_timer);
1853 hdw->quiescent_timer.data = (unsigned long)hdw;
1854 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1855
1856 init_timer(&hdw->encoder_wait_timer);
1857 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1858 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1859
1860 hdw->master_state = PVR2_STATE_DEAD;
1861
1862 init_waitqueue_head(&hdw->state_wait_data);
1863
Mike Isely18103c572007-01-20 00:09:47 -03001864 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001865 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03001866
Mike Isely7fb20fa2008-04-22 14:45:37 -03001867 /* Calculate which inputs are OK */
1868 m = 0;
1869 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03001870 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
1871 m |= 1 << PVR2_CVAL_INPUT_DTV;
1872 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03001873 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
1874 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
1875 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
1876 hdw->input_avail_mask = m;
1877
Mike Isely62433e32008-04-22 14:45:40 -03001878 /* If not a hybrid device, pathway_state never changes. So
1879 initialize it here to what it should forever be. */
1880 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
1881 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
1882 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
1883 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
1884 }
1885
Mike Iselyc05c0462006-06-25 20:04:25 -03001886 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03001887 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03001888 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001889 GFP_KERNEL);
1890 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03001891 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03001892 for (idx = 0; idx < hdw->control_cnt; idx++) {
1893 cptr = hdw->controls + idx;
1894 cptr->hdw = hdw;
1895 }
Mike Iselyd8554972006-06-26 20:58:46 -03001896 for (idx = 0; idx < 32; idx++) {
1897 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1898 }
Mike Iselyc05c0462006-06-25 20:04:25 -03001899 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001900 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03001901 cptr->info = control_defs+idx;
1902 }
Mike Iselydbc40a02008-04-22 14:45:39 -03001903
1904 /* Ensure that default input choice is a valid one. */
1905 m = hdw->input_avail_mask;
1906 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
1907 if (!((1 << idx) & m)) continue;
1908 hdw->input_val = idx;
1909 break;
1910 }
1911
Mike Iselyb30d2442006-06-25 20:05:01 -03001912 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03001913 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03001914 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
1915 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03001916 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
1917 cptr = hdw->controls + idx + CTRLDEF_COUNT;
1918 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
1919 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
1920 ciptr->name = mpeg_ids[idx].strid;
1921 ciptr->v4l_id = mpeg_ids[idx].id;
1922 ciptr->skip_init = !0;
1923 ciptr->get_value = ctrl_cx2341x_get;
1924 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
1925 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
1926 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
1927 qctrl.id = ciptr->v4l_id;
1928 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
1929 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
1930 ciptr->set_value = ctrl_cx2341x_set;
1931 }
1932 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
1933 PVR2_CTLD_INFO_DESC_SIZE);
1934 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
1935 ciptr->default_value = qctrl.default_value;
1936 switch (qctrl.type) {
1937 default:
1938 case V4L2_CTRL_TYPE_INTEGER:
1939 ciptr->type = pvr2_ctl_int;
1940 ciptr->def.type_int.min_value = qctrl.minimum;
1941 ciptr->def.type_int.max_value = qctrl.maximum;
1942 break;
1943 case V4L2_CTRL_TYPE_BOOLEAN:
1944 ciptr->type = pvr2_ctl_bool;
1945 break;
1946 case V4L2_CTRL_TYPE_MENU:
1947 ciptr->type = pvr2_ctl_enum;
1948 ciptr->def.type_enum.value_names =
1949 cx2341x_ctrl_get_menu(ciptr->v4l_id);
1950 for (cnt1 = 0;
1951 ciptr->def.type_enum.value_names[cnt1] != NULL;
1952 cnt1++) { }
1953 ciptr->def.type_enum.count = cnt1;
1954 break;
1955 }
1956 cptr->info = ciptr;
1957 }
Mike Iselyd8554972006-06-26 20:58:46 -03001958
1959 // Initialize video standard enum dynamic control
1960 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
1961 if (cptr) {
1962 memcpy(&hdw->std_info_enum,cptr->info,
1963 sizeof(hdw->std_info_enum));
1964 cptr->info = &hdw->std_info_enum;
1965
1966 }
1967 // Initialize control data regarding video standard masks
1968 valid_std_mask = pvr2_std_get_usable();
1969 for (idx = 0; idx < 32; idx++) {
1970 if (!(valid_std_mask & (1 << idx))) continue;
1971 cnt1 = pvr2_std_id_to_str(
1972 hdw->std_mask_names[idx],
1973 sizeof(hdw->std_mask_names[idx])-1,
1974 1 << idx);
1975 hdw->std_mask_names[idx][cnt1] = 0;
1976 }
1977 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
1978 if (cptr) {
1979 memcpy(&hdw->std_info_avail,cptr->info,
1980 sizeof(hdw->std_info_avail));
1981 cptr->info = &hdw->std_info_avail;
1982 hdw->std_info_avail.def.type_bitmask.bit_names =
1983 hdw->std_mask_ptrs;
1984 hdw->std_info_avail.def.type_bitmask.valid_bits =
1985 valid_std_mask;
1986 }
1987 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
1988 if (cptr) {
1989 memcpy(&hdw->std_info_cur,cptr->info,
1990 sizeof(hdw->std_info_cur));
1991 cptr->info = &hdw->std_info_cur;
1992 hdw->std_info_cur.def.type_bitmask.bit_names =
1993 hdw->std_mask_ptrs;
1994 hdw->std_info_avail.def.type_bitmask.valid_bits =
1995 valid_std_mask;
1996 }
1997
1998 hdw->eeprom_addr = -1;
1999 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002000 hdw->v4l_minor_number_video = -1;
2001 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002002 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002003 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2004 if (!hdw->ctl_write_buffer) goto fail;
2005 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2006 if (!hdw->ctl_read_buffer) goto fail;
2007 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2008 if (!hdw->ctl_write_urb) goto fail;
2009 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2010 if (!hdw->ctl_read_urb) goto fail;
2011
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002012 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002013 for (idx = 0; idx < PVR_NUM; idx++) {
2014 if (unit_pointers[idx]) continue;
2015 hdw->unit_number = idx;
2016 unit_pointers[idx] = hdw;
2017 break;
2018 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002019 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002020
2021 cnt1 = 0;
2022 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2023 cnt1 += cnt2;
2024 if (hdw->unit_number >= 0) {
2025 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2026 ('a' + hdw->unit_number));
2027 cnt1 += cnt2;
2028 }
2029 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2030 hdw->name[cnt1] = 0;
2031
Mike Isely681c7392007-11-26 01:48:52 -03002032 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2033 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2034 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002035
Mike Iselyd8554972006-06-26 20:58:46 -03002036 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2037 hdw->unit_number,hdw->name);
2038
2039 hdw->tuner_type = -1;
2040 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002041
2042 hdw->usb_intf = intf;
2043 hdw->usb_dev = interface_to_usbdev(intf);
2044
Mike Isely31a18542007-04-08 01:11:47 -03002045 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2046 "usb %s address %d",
2047 hdw->usb_dev->dev.bus_id,
2048 hdw->usb_dev->devnum);
2049
Mike Iselyd8554972006-06-26 20:58:46 -03002050 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2051 usb_set_interface(hdw->usb_dev,ifnum,0);
2052
2053 mutex_init(&hdw->ctl_lock_mutex);
2054 mutex_init(&hdw->big_lock_mutex);
2055
2056 return hdw;
2057 fail:
2058 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002059 del_timer_sync(&hdw->quiescent_timer);
2060 del_timer_sync(&hdw->encoder_wait_timer);
2061 if (hdw->workqueue) {
2062 flush_workqueue(hdw->workqueue);
2063 destroy_workqueue(hdw->workqueue);
2064 hdw->workqueue = NULL;
2065 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002066 usb_free_urb(hdw->ctl_read_urb);
2067 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002068 kfree(hdw->ctl_read_buffer);
2069 kfree(hdw->ctl_write_buffer);
2070 kfree(hdw->controls);
2071 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002072 kfree(hdw->std_defs);
2073 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002074 kfree(hdw);
2075 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002076 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002077}
2078
2079
2080/* Remove _all_ associations between this driver and the underlying USB
2081 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002082static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002083{
2084 if (hdw->flag_disconnected) return;
2085 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2086 if (hdw->ctl_read_urb) {
2087 usb_kill_urb(hdw->ctl_read_urb);
2088 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002089 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002090 }
2091 if (hdw->ctl_write_urb) {
2092 usb_kill_urb(hdw->ctl_write_urb);
2093 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002094 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002095 }
2096 if (hdw->ctl_read_buffer) {
2097 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002098 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002099 }
2100 if (hdw->ctl_write_buffer) {
2101 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002102 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002103 }
Mike Iselyd8554972006-06-26 20:58:46 -03002104 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002105 hdw->usb_dev = NULL;
2106 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002107 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002108}
2109
2110
2111/* Destroy hardware interaction structure */
2112void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2113{
Mike Isely401c27c2007-09-08 22:11:46 -03002114 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002115 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002116 if (hdw->workqueue) {
2117 flush_workqueue(hdw->workqueue);
2118 destroy_workqueue(hdw->workqueue);
2119 hdw->workqueue = NULL;
2120 }
Mike Isely8f591002008-04-22 14:45:45 -03002121 del_timer_sync(&hdw->quiescent_timer);
2122 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002123 if (hdw->fw_buffer) {
2124 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002125 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002126 }
2127 if (hdw->vid_stream) {
2128 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002129 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002130 }
Mike Iselyd8554972006-06-26 20:58:46 -03002131 if (hdw->decoder_ctrl) {
2132 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2133 }
2134 pvr2_i2c_core_done(hdw);
2135 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002136 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002137 if ((hdw->unit_number >= 0) &&
2138 (hdw->unit_number < PVR_NUM) &&
2139 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002140 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002141 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002142 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002143 kfree(hdw->controls);
2144 kfree(hdw->mpeg_ctrl_info);
2145 kfree(hdw->std_defs);
2146 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002147 kfree(hdw);
2148}
2149
2150
Mike Iselyd8554972006-06-26 20:58:46 -03002151int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2152{
2153 return (hdw && hdw->flag_ok);
2154}
2155
2156
2157/* Called when hardware has been unplugged */
2158void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2159{
2160 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2161 LOCK_TAKE(hdw->big_lock);
2162 LOCK_TAKE(hdw->ctl_lock);
2163 pvr2_hdw_remove_usb_stuff(hdw);
2164 LOCK_GIVE(hdw->ctl_lock);
2165 LOCK_GIVE(hdw->big_lock);
2166}
2167
2168
2169// Attempt to autoselect an appropriate value for std_enum_cur given
2170// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002171static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002172{
2173 unsigned int idx;
2174 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2175 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2176 hdw->std_enum_cur = idx;
2177 return;
2178 }
2179 }
2180 hdw->std_enum_cur = 0;
2181}
2182
2183
2184// Calculate correct set of enumerated standards based on currently known
2185// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002186static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002187{
2188 struct v4l2_standard *newstd;
2189 unsigned int std_cnt;
2190 unsigned int idx;
2191
2192 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2193
2194 if (hdw->std_defs) {
2195 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002196 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002197 }
2198 hdw->std_enum_cnt = 0;
2199 if (hdw->std_enum_names) {
2200 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002201 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002202 }
2203
2204 if (!std_cnt) {
2205 pvr2_trace(
2206 PVR2_TRACE_ERROR_LEGS,
2207 "WARNING: Failed to identify any viable standards");
2208 }
2209 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2210 hdw->std_enum_names[0] = "none";
2211 for (idx = 0; idx < std_cnt; idx++) {
2212 hdw->std_enum_names[idx+1] =
2213 newstd[idx].name;
2214 }
2215 // Set up the dynamic control for this standard
2216 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2217 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2218 hdw->std_defs = newstd;
2219 hdw->std_enum_cnt = std_cnt+1;
2220 hdw->std_enum_cur = 0;
2221 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2222}
2223
2224
2225int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2226 struct v4l2_standard *std,
2227 unsigned int idx)
2228{
2229 int ret = -EINVAL;
2230 if (!idx) return ret;
2231 LOCK_TAKE(hdw->big_lock); do {
2232 if (idx >= hdw->std_enum_cnt) break;
2233 idx--;
2234 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2235 ret = 0;
2236 } while (0); LOCK_GIVE(hdw->big_lock);
2237 return ret;
2238}
2239
2240
2241/* Get the number of defined controls */
2242unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2243{
Mike Iselyc05c0462006-06-25 20:04:25 -03002244 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002245}
2246
2247
2248/* Retrieve a control handle given its index (0..count-1) */
2249struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2250 unsigned int idx)
2251{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002252 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002253 return hdw->controls + idx;
2254}
2255
2256
2257/* Retrieve a control handle given its index (0..count-1) */
2258struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2259 unsigned int ctl_id)
2260{
2261 struct pvr2_ctrl *cptr;
2262 unsigned int idx;
2263 int i;
2264
2265 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002266 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002267 cptr = hdw->controls + idx;
2268 i = cptr->info->internal_id;
2269 if (i && (i == ctl_id)) return cptr;
2270 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002271 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002272}
2273
2274
Mike Iselya761f432006-06-25 20:04:44 -03002275/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002276struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2277{
2278 struct pvr2_ctrl *cptr;
2279 unsigned int idx;
2280 int i;
2281
2282 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002283 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002284 cptr = hdw->controls + idx;
2285 i = cptr->info->v4l_id;
2286 if (i && (i == ctl_id)) return cptr;
2287 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002288 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002289}
2290
2291
Mike Iselya761f432006-06-25 20:04:44 -03002292/* Given a V4L ID for its immediate predecessor, retrieve the control
2293 structure associated with it. */
2294struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2295 unsigned int ctl_id)
2296{
2297 struct pvr2_ctrl *cptr,*cp2;
2298 unsigned int idx;
2299 int i;
2300
2301 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002302 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002303 for (idx = 0; idx < hdw->control_cnt; idx++) {
2304 cptr = hdw->controls + idx;
2305 i = cptr->info->v4l_id;
2306 if (!i) continue;
2307 if (i <= ctl_id) continue;
2308 if (cp2 && (cp2->info->v4l_id < i)) continue;
2309 cp2 = cptr;
2310 }
2311 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002312 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002313}
2314
2315
Mike Iselyd8554972006-06-26 20:58:46 -03002316static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2317{
2318 switch (tp) {
2319 case pvr2_ctl_int: return "integer";
2320 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002321 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002322 case pvr2_ctl_bitmask: return "bitmask";
2323 }
2324 return "";
2325}
2326
2327
Mike Isely681c7392007-11-26 01:48:52 -03002328/* Figure out if we need to commit control changes. If so, mark internal
2329 state flags to indicate this fact and return true. Otherwise do nothing
2330 else and return false. */
2331static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002332{
Mike Iselyd8554972006-06-26 20:58:46 -03002333 unsigned int idx;
2334 struct pvr2_ctrl *cptr;
2335 int value;
2336 int commit_flag = 0;
2337 char buf[100];
2338 unsigned int bcnt,ccnt;
2339
Mike Iselyc05c0462006-06-25 20:04:25 -03002340 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002341 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002342 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002343 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002344 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002345
Mike Iselyfe23a282007-01-20 00:10:55 -03002346 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002347 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2348 cptr->info->name);
2349 value = 0;
2350 cptr->info->get_value(cptr,&value);
2351 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2352 buf+bcnt,
2353 sizeof(buf)-bcnt,&ccnt);
2354 bcnt += ccnt;
2355 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2356 get_ctrl_typename(cptr->info->type));
2357 pvr2_trace(PVR2_TRACE_CTL,
2358 "/*--TRACE_COMMIT--*/ %.*s",
2359 bcnt,buf);
2360 }
2361
2362 if (!commit_flag) {
2363 /* Nothing has changed */
2364 return 0;
2365 }
2366
Mike Isely681c7392007-11-26 01:48:52 -03002367 hdw->state_pipeline_config = 0;
2368 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2369 pvr2_hdw_state_sched(hdw);
2370
2371 return !0;
2372}
2373
2374
2375/* Perform all operations needed to commit all control changes. This must
2376 be performed in synchronization with the pipeline state and is thus
2377 expected to be called as part of the driver's worker thread. Return
2378 true if commit successful, otherwise return false to indicate that
2379 commit isn't possible at this time. */
2380static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2381{
2382 unsigned int idx;
2383 struct pvr2_ctrl *cptr;
2384 int disruptive_change;
2385
Mike Iselyd8554972006-06-26 20:58:46 -03002386 /* When video standard changes, reset the hres and vres values -
2387 but if the user has pending changes there, then let the changes
2388 take priority. */
2389 if (hdw->std_dirty) {
2390 /* Rewrite the vertical resolution to be appropriate to the
2391 video standard that has been selected. */
2392 int nvres;
2393 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2394 nvres = 480;
2395 } else {
2396 nvres = 576;
2397 }
2398 if (nvres != hdw->res_ver_val) {
2399 hdw->res_ver_val = nvres;
2400 hdw->res_ver_dirty = !0;
2401 }
Mike Iselyd8554972006-06-26 20:58:46 -03002402 }
2403
Mike Isely62433e32008-04-22 14:45:40 -03002404 if (hdw->input_dirty &&
2405 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2406 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2407 hdw->pathway_state)) {
2408 /* Change of mode being asked for... */
2409 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03002410 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03002411 }
2412 if (!hdw->state_pathway_ok) {
2413 /* Can't commit anything until pathway is ok. */
2414 return 0;
2415 }
Mike Isely681c7392007-11-26 01:48:52 -03002416 /* If any of the below has changed, then we can't do the update
2417 while the pipeline is running. Pipeline must be paused first
2418 and decoder -> encoder connection be made quiescent before we
2419 can proceed. */
2420 disruptive_change =
2421 (hdw->std_dirty ||
2422 hdw->enc_unsafe_stale ||
2423 hdw->srate_dirty ||
2424 hdw->res_ver_dirty ||
2425 hdw->res_hor_dirty ||
2426 hdw->input_dirty ||
2427 (hdw->active_stream_type != hdw->desired_stream_type));
2428 if (disruptive_change && !hdw->state_pipeline_idle) {
2429 /* Pipeline is not idle; we can't proceed. Arrange to
2430 cause pipeline to stop so that we can try this again
2431 later.... */
2432 hdw->state_pipeline_pause = !0;
2433 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002434 }
2435
Mike Iselyb30d2442006-06-25 20:05:01 -03002436 if (hdw->srate_dirty) {
2437 /* Write new sample rate into control structure since
2438 * the master copy is stale. We must track srate
2439 * separate from the mpeg control structure because
2440 * other logic also uses this value. */
2441 struct v4l2_ext_controls cs;
2442 struct v4l2_ext_control c1;
2443 memset(&cs,0,sizeof(cs));
2444 memset(&c1,0,sizeof(c1));
2445 cs.controls = &c1;
2446 cs.count = 1;
2447 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2448 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002449 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002450 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002451
Mike Iselyd8554972006-06-26 20:58:46 -03002452 /* Scan i2c core at this point - before we clear all the dirty
2453 bits. Various parts of the i2c core will notice dirty bits as
2454 appropriate and arrange to broadcast or directly send updates to
2455 the client drivers in order to keep everything in sync */
2456 pvr2_i2c_core_check_stale(hdw);
2457
Mike Iselyc05c0462006-06-25 20:04:25 -03002458 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002459 cptr = hdw->controls + idx;
2460 if (!cptr->info->clear_dirty) continue;
2461 cptr->info->clear_dirty(cptr);
2462 }
2463
Mike Isely681c7392007-11-26 01:48:52 -03002464 if (hdw->active_stream_type != hdw->desired_stream_type) {
2465 /* Handle any side effects of stream config here */
2466 hdw->active_stream_type = hdw->desired_stream_type;
2467 }
2468
Mike Iselyd8554972006-06-26 20:58:46 -03002469 /* Now execute i2c core update */
2470 pvr2_i2c_core_sync(hdw);
2471
Mike Isely62433e32008-04-22 14:45:40 -03002472 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2473 hdw->state_encoder_run) {
2474 /* If encoder isn't running or it can't be touched, then
2475 this will get worked out later when we start the
2476 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002477 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2478 }
Mike Iselyd8554972006-06-26 20:58:46 -03002479
Mike Isely681c7392007-11-26 01:48:52 -03002480 hdw->state_pipeline_config = !0;
2481 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2482 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002483}
2484
2485
2486int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2487{
Mike Isely681c7392007-11-26 01:48:52 -03002488 int fl;
2489 LOCK_TAKE(hdw->big_lock);
2490 fl = pvr2_hdw_commit_setup(hdw);
2491 LOCK_GIVE(hdw->big_lock);
2492 if (!fl) return 0;
2493 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002494}
2495
2496
Mike Isely681c7392007-11-26 01:48:52 -03002497static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002498{
Mike Isely681c7392007-11-26 01:48:52 -03002499 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002500 LOCK_TAKE(hdw->big_lock); do {
2501 pvr2_i2c_core_sync(hdw);
2502 } while (0); LOCK_GIVE(hdw->big_lock);
2503}
2504
2505
Mike Isely681c7392007-11-26 01:48:52 -03002506static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002507{
Mike Isely681c7392007-11-26 01:48:52 -03002508 int fl = 0;
2509 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002510 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002511 fl = pvr2_hdw_state_eval(hdw);
2512 } while (0); LOCK_GIVE(hdw->big_lock);
2513 if (fl && hdw->state_func) {
2514 hdw->state_func(hdw->state_data);
2515 }
2516}
2517
2518
Mike Isely681c7392007-11-26 01:48:52 -03002519static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002520{
Mike Isely681c7392007-11-26 01:48:52 -03002521 return wait_event_interruptible(
2522 hdw->state_wait_data,
2523 (hdw->state_stale == 0) &&
2524 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002525}
2526
Mike Isely681c7392007-11-26 01:48:52 -03002527
Mike Iselyd8554972006-06-26 20:58:46 -03002528/* Return name for this driver instance */
2529const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2530{
2531 return hdw->name;
2532}
2533
2534
Mike Isely78a47102007-11-26 01:58:20 -03002535const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2536{
2537 return hdw->hdw_desc->description;
2538}
2539
2540
2541const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2542{
2543 return hdw->hdw_desc->shortname;
2544}
2545
2546
Mike Iselyd8554972006-06-26 20:58:46 -03002547int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2548{
2549 int result;
2550 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002551 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002552 result = pvr2_send_request(hdw,
2553 hdw->cmd_buffer,1,
2554 hdw->cmd_buffer,1);
2555 if (result < 0) break;
2556 result = (hdw->cmd_buffer[0] != 0);
2557 } while(0); LOCK_GIVE(hdw->ctl_lock);
2558 return result;
2559}
2560
2561
Mike Isely18103c572007-01-20 00:09:47 -03002562/* Execute poll of tuner status */
2563void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002564{
Mike Iselyd8554972006-06-26 20:58:46 -03002565 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002566 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002567 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002568}
2569
2570
2571/* Return information about the tuner */
2572int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2573{
2574 LOCK_TAKE(hdw->big_lock); do {
2575 if (hdw->tuner_signal_stale) {
2576 pvr2_i2c_core_status_poll(hdw);
2577 }
2578 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2579 } while (0); LOCK_GIVE(hdw->big_lock);
2580 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002581}
2582
2583
2584/* Get handle to video output stream */
2585struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2586{
2587 return hp->vid_stream;
2588}
2589
2590
2591void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2592{
Mike Isely4f1a3e52006-06-25 20:04:31 -03002593 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002594 LOCK_TAKE(hdw->big_lock); do {
2595 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03002596 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002597 pvr2_i2c_core_check_stale(hdw);
2598 hdw->log_requested = 0;
2599 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03002600 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03002601 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03002602 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03002603 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002604 } while (0); LOCK_GIVE(hdw->big_lock);
2605}
2606
Mike Isely4db666c2007-09-08 22:16:27 -03002607
2608/* Grab EEPROM contents, needed for direct method. */
2609#define EEPROM_SIZE 8192
2610#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
2611static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
2612{
2613 struct i2c_msg msg[2];
2614 u8 *eeprom;
2615 u8 iadd[2];
2616 u8 addr;
2617 u16 eepromSize;
2618 unsigned int offs;
2619 int ret;
2620 int mode16 = 0;
2621 unsigned pcnt,tcnt;
2622 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
2623 if (!eeprom) {
2624 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2625 "Failed to allocate memory"
2626 " required to read eeprom");
2627 return NULL;
2628 }
2629
2630 trace_eeprom("Value for eeprom addr from controller was 0x%x",
2631 hdw->eeprom_addr);
2632 addr = hdw->eeprom_addr;
2633 /* Seems that if the high bit is set, then the *real* eeprom
2634 address is shifted right now bit position (noticed this in
2635 newer PVR USB2 hardware) */
2636 if (addr & 0x80) addr >>= 1;
2637
2638 /* FX2 documentation states that a 16bit-addressed eeprom is
2639 expected if the I2C address is an odd number (yeah, this is
2640 strange but it's what they do) */
2641 mode16 = (addr & 1);
2642 eepromSize = (mode16 ? EEPROM_SIZE : 256);
2643 trace_eeprom("Examining %d byte eeprom at location 0x%x"
2644 " using %d bit addressing",eepromSize,addr,
2645 mode16 ? 16 : 8);
2646
2647 msg[0].addr = addr;
2648 msg[0].flags = 0;
2649 msg[0].len = mode16 ? 2 : 1;
2650 msg[0].buf = iadd;
2651 msg[1].addr = addr;
2652 msg[1].flags = I2C_M_RD;
2653
2654 /* We have to do the actual eeprom data fetch ourselves, because
2655 (1) we're only fetching part of the eeprom, and (2) if we were
2656 getting the whole thing our I2C driver can't grab it in one
2657 pass - which is what tveeprom is otherwise going to attempt */
2658 memset(eeprom,0,EEPROM_SIZE);
2659 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
2660 pcnt = 16;
2661 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
2662 offs = tcnt + (eepromSize - EEPROM_SIZE);
2663 if (mode16) {
2664 iadd[0] = offs >> 8;
2665 iadd[1] = offs;
2666 } else {
2667 iadd[0] = offs;
2668 }
2669 msg[1].len = pcnt;
2670 msg[1].buf = eeprom+tcnt;
2671 if ((ret = i2c_transfer(&hdw->i2c_adap,
2672 msg,ARRAY_SIZE(msg))) != 2) {
2673 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2674 "eeprom fetch set offs err=%d",ret);
2675 kfree(eeprom);
2676 return NULL;
2677 }
2678 }
2679 return eeprom;
2680}
2681
2682
2683void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
2684 int prom_flag,
2685 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002686{
2687 int ret;
2688 u16 address;
2689 unsigned int pipe;
2690 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00002691 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03002692
2693 if (!enable_flag) {
2694 pvr2_trace(PVR2_TRACE_FIRMWARE,
2695 "Cleaning up after CPU firmware fetch");
2696 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002697 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002698 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03002699 if (hdw->fw_cpu_flag) {
2700 /* Now release the CPU. It will disconnect
2701 and reconnect later. */
2702 pvr2_hdw_cpureset_assert(hdw,0);
2703 }
Mike Iselyd8554972006-06-26 20:58:46 -03002704 break;
2705 }
2706
Mike Isely4db666c2007-09-08 22:16:27 -03002707 hdw->fw_cpu_flag = (prom_flag == 0);
2708 if (hdw->fw_cpu_flag) {
2709 pvr2_trace(PVR2_TRACE_FIRMWARE,
2710 "Preparing to suck out CPU firmware");
2711 hdw->fw_size = 0x2000;
2712 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
2713 if (!hdw->fw_buffer) {
2714 hdw->fw_size = 0;
2715 break;
2716 }
2717
2718 /* We have to hold the CPU during firmware upload. */
2719 pvr2_hdw_cpureset_assert(hdw,1);
2720
2721 /* download the firmware from address 0000-1fff in 2048
2722 (=0x800) bytes chunk. */
2723
2724 pvr2_trace(PVR2_TRACE_FIRMWARE,
2725 "Grabbing CPU firmware");
2726 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2727 for(address = 0; address < hdw->fw_size;
2728 address += 0x800) {
2729 ret = usb_control_msg(hdw->usb_dev,pipe,
2730 0xa0,0xc0,
2731 address,0,
2732 hdw->fw_buffer+address,
2733 0x800,HZ);
2734 if (ret < 0) break;
2735 }
2736
2737 pvr2_trace(PVR2_TRACE_FIRMWARE,
2738 "Done grabbing CPU firmware");
2739 } else {
2740 pvr2_trace(PVR2_TRACE_FIRMWARE,
2741 "Sucking down EEPROM contents");
2742 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
2743 if (!hdw->fw_buffer) {
2744 pvr2_trace(PVR2_TRACE_FIRMWARE,
2745 "EEPROM content suck failed.");
2746 break;
2747 }
2748 hdw->fw_size = EEPROM_SIZE;
2749 pvr2_trace(PVR2_TRACE_FIRMWARE,
2750 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03002751 }
2752
Mike Iselyd8554972006-06-26 20:58:46 -03002753 } while (0); LOCK_GIVE(hdw->big_lock);
2754}
2755
2756
2757/* Return true if we're in a mode for retrieval CPU firmware */
2758int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2759{
Al Viro5fa12472008-03-29 03:07:38 +00002760 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002761}
2762
2763
2764int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2765 char *buf,unsigned int cnt)
2766{
2767 int ret = -EINVAL;
2768 LOCK_TAKE(hdw->big_lock); do {
2769 if (!buf) break;
2770 if (!cnt) break;
2771
2772 if (!hdw->fw_buffer) {
2773 ret = -EIO;
2774 break;
2775 }
2776
2777 if (offs >= hdw->fw_size) {
2778 pvr2_trace(PVR2_TRACE_FIRMWARE,
2779 "Read firmware data offs=%d EOF",
2780 offs);
2781 ret = 0;
2782 break;
2783 }
2784
2785 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2786
2787 memcpy(buf,hdw->fw_buffer+offs,cnt);
2788
2789 pvr2_trace(PVR2_TRACE_FIRMWARE,
2790 "Read firmware data offs=%d cnt=%d",
2791 offs,cnt);
2792 ret = cnt;
2793 } while (0); LOCK_GIVE(hdw->big_lock);
2794
2795 return ret;
2796}
2797
2798
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002799int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002800 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03002801{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002802 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002803 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2804 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2805 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002806 default: return -1;
2807 }
Mike Iselyd8554972006-06-26 20:58:46 -03002808}
2809
2810
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03002811/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002812void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002813 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03002814{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002815 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002816 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2817 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2818 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002819 default: break;
2820 }
Mike Iselyd8554972006-06-26 20:58:46 -03002821}
2822
2823
David Howells7d12e782006-10-05 14:55:46 +01002824static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002825{
2826 struct pvr2_hdw *hdw = urb->context;
2827 hdw->ctl_write_pend_flag = 0;
2828 if (hdw->ctl_read_pend_flag) return;
2829 complete(&hdw->ctl_done);
2830}
2831
2832
David Howells7d12e782006-10-05 14:55:46 +01002833static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002834{
2835 struct pvr2_hdw *hdw = urb->context;
2836 hdw->ctl_read_pend_flag = 0;
2837 if (hdw->ctl_write_pend_flag) return;
2838 complete(&hdw->ctl_done);
2839}
2840
2841
2842static void pvr2_ctl_timeout(unsigned long data)
2843{
2844 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2845 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2846 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002847 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002848 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002849 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002850 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03002851 }
2852}
2853
2854
Mike Iselye61b6fc2006-07-18 22:42:18 -03002855/* Issue a command and get a response from the device. This extended
2856 version includes a probe flag (which if set means that device errors
2857 should not be logged or treated as fatal) and a timeout in jiffies.
2858 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002859static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2860 unsigned int timeout,int probe_fl,
2861 void *write_data,unsigned int write_len,
2862 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03002863{
2864 unsigned int idx;
2865 int status = 0;
2866 struct timer_list timer;
2867 if (!hdw->ctl_lock_held) {
2868 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2869 "Attempted to execute control transfer"
2870 " without lock!!");
2871 return -EDEADLK;
2872 }
Mike Isely681c7392007-11-26 01:48:52 -03002873 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03002874 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2875 "Attempted to execute control transfer"
2876 " when device not ok");
2877 return -EIO;
2878 }
2879 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
2880 if (!probe_fl) {
2881 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2882 "Attempted to execute control transfer"
2883 " when USB is disconnected");
2884 }
2885 return -ENOTTY;
2886 }
2887
2888 /* Ensure that we have sane parameters */
2889 if (!write_data) write_len = 0;
2890 if (!read_data) read_len = 0;
2891 if (write_len > PVR2_CTL_BUFFSIZE) {
2892 pvr2_trace(
2893 PVR2_TRACE_ERROR_LEGS,
2894 "Attempted to execute %d byte"
2895 " control-write transfer (limit=%d)",
2896 write_len,PVR2_CTL_BUFFSIZE);
2897 return -EINVAL;
2898 }
2899 if (read_len > PVR2_CTL_BUFFSIZE) {
2900 pvr2_trace(
2901 PVR2_TRACE_ERROR_LEGS,
2902 "Attempted to execute %d byte"
2903 " control-read transfer (limit=%d)",
2904 write_len,PVR2_CTL_BUFFSIZE);
2905 return -EINVAL;
2906 }
2907 if ((!write_len) && (!read_len)) {
2908 pvr2_trace(
2909 PVR2_TRACE_ERROR_LEGS,
2910 "Attempted to execute null control transfer?");
2911 return -EINVAL;
2912 }
2913
2914
2915 hdw->cmd_debug_state = 1;
2916 if (write_len) {
2917 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
2918 } else {
2919 hdw->cmd_debug_code = 0;
2920 }
2921 hdw->cmd_debug_write_len = write_len;
2922 hdw->cmd_debug_read_len = read_len;
2923
2924 /* Initialize common stuff */
2925 init_completion(&hdw->ctl_done);
2926 hdw->ctl_timeout_flag = 0;
2927 hdw->ctl_write_pend_flag = 0;
2928 hdw->ctl_read_pend_flag = 0;
2929 init_timer(&timer);
2930 timer.expires = jiffies + timeout;
2931 timer.data = (unsigned long)hdw;
2932 timer.function = pvr2_ctl_timeout;
2933
2934 if (write_len) {
2935 hdw->cmd_debug_state = 2;
2936 /* Transfer write data to internal buffer */
2937 for (idx = 0; idx < write_len; idx++) {
2938 hdw->ctl_write_buffer[idx] =
2939 ((unsigned char *)write_data)[idx];
2940 }
2941 /* Initiate a write request */
2942 usb_fill_bulk_urb(hdw->ctl_write_urb,
2943 hdw->usb_dev,
2944 usb_sndbulkpipe(hdw->usb_dev,
2945 PVR2_CTL_WRITE_ENDPOINT),
2946 hdw->ctl_write_buffer,
2947 write_len,
2948 pvr2_ctl_write_complete,
2949 hdw);
2950 hdw->ctl_write_urb->actual_length = 0;
2951 hdw->ctl_write_pend_flag = !0;
2952 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
2953 if (status < 0) {
2954 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2955 "Failed to submit write-control"
2956 " URB status=%d",status);
2957 hdw->ctl_write_pend_flag = 0;
2958 goto done;
2959 }
2960 }
2961
2962 if (read_len) {
2963 hdw->cmd_debug_state = 3;
2964 memset(hdw->ctl_read_buffer,0x43,read_len);
2965 /* Initiate a read request */
2966 usb_fill_bulk_urb(hdw->ctl_read_urb,
2967 hdw->usb_dev,
2968 usb_rcvbulkpipe(hdw->usb_dev,
2969 PVR2_CTL_READ_ENDPOINT),
2970 hdw->ctl_read_buffer,
2971 read_len,
2972 pvr2_ctl_read_complete,
2973 hdw);
2974 hdw->ctl_read_urb->actual_length = 0;
2975 hdw->ctl_read_pend_flag = !0;
2976 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
2977 if (status < 0) {
2978 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2979 "Failed to submit read-control"
2980 " URB status=%d",status);
2981 hdw->ctl_read_pend_flag = 0;
2982 goto done;
2983 }
2984 }
2985
2986 /* Start timer */
2987 add_timer(&timer);
2988
2989 /* Now wait for all I/O to complete */
2990 hdw->cmd_debug_state = 4;
2991 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2992 wait_for_completion(&hdw->ctl_done);
2993 }
2994 hdw->cmd_debug_state = 5;
2995
2996 /* Stop timer */
2997 del_timer_sync(&timer);
2998
2999 hdw->cmd_debug_state = 6;
3000 status = 0;
3001
3002 if (hdw->ctl_timeout_flag) {
3003 status = -ETIMEDOUT;
3004 if (!probe_fl) {
3005 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3006 "Timed out control-write");
3007 }
3008 goto done;
3009 }
3010
3011 if (write_len) {
3012 /* Validate results of write request */
3013 if ((hdw->ctl_write_urb->status != 0) &&
3014 (hdw->ctl_write_urb->status != -ENOENT) &&
3015 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3016 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3017 /* USB subsystem is reporting some kind of failure
3018 on the write */
3019 status = hdw->ctl_write_urb->status;
3020 if (!probe_fl) {
3021 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3022 "control-write URB failure,"
3023 " status=%d",
3024 status);
3025 }
3026 goto done;
3027 }
3028 if (hdw->ctl_write_urb->actual_length < write_len) {
3029 /* Failed to write enough data */
3030 status = -EIO;
3031 if (!probe_fl) {
3032 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3033 "control-write URB short,"
3034 " expected=%d got=%d",
3035 write_len,
3036 hdw->ctl_write_urb->actual_length);
3037 }
3038 goto done;
3039 }
3040 }
3041 if (read_len) {
3042 /* Validate results of read request */
3043 if ((hdw->ctl_read_urb->status != 0) &&
3044 (hdw->ctl_read_urb->status != -ENOENT) &&
3045 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3046 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3047 /* USB subsystem is reporting some kind of failure
3048 on the read */
3049 status = hdw->ctl_read_urb->status;
3050 if (!probe_fl) {
3051 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3052 "control-read URB failure,"
3053 " status=%d",
3054 status);
3055 }
3056 goto done;
3057 }
3058 if (hdw->ctl_read_urb->actual_length < read_len) {
3059 /* Failed to read enough data */
3060 status = -EIO;
3061 if (!probe_fl) {
3062 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3063 "control-read URB short,"
3064 " expected=%d got=%d",
3065 read_len,
3066 hdw->ctl_read_urb->actual_length);
3067 }
3068 goto done;
3069 }
3070 /* Transfer retrieved data out from internal buffer */
3071 for (idx = 0; idx < read_len; idx++) {
3072 ((unsigned char *)read_data)[idx] =
3073 hdw->ctl_read_buffer[idx];
3074 }
3075 }
3076
3077 done:
3078
3079 hdw->cmd_debug_state = 0;
3080 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003081 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003082 }
3083 return status;
3084}
3085
3086
3087int pvr2_send_request(struct pvr2_hdw *hdw,
3088 void *write_data,unsigned int write_len,
3089 void *read_data,unsigned int read_len)
3090{
3091 return pvr2_send_request_ex(hdw,HZ*4,0,
3092 write_data,write_len,
3093 read_data,read_len);
3094}
3095
3096int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3097{
3098 int ret;
3099
3100 LOCK_TAKE(hdw->ctl_lock);
3101
Michael Krufky8d364362007-01-22 02:17:55 -03003102 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003103 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3104 hdw->cmd_buffer[5] = 0;
3105 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3106 hdw->cmd_buffer[7] = reg & 0xff;
3107
3108
3109 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3110
3111 LOCK_GIVE(hdw->ctl_lock);
3112
3113 return ret;
3114}
3115
3116
Adrian Bunk07e337e2006-06-30 11:30:20 -03003117static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003118{
3119 int ret = 0;
3120
3121 LOCK_TAKE(hdw->ctl_lock);
3122
Michael Krufky8d364362007-01-22 02:17:55 -03003123 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003124 hdw->cmd_buffer[1] = 0;
3125 hdw->cmd_buffer[2] = 0;
3126 hdw->cmd_buffer[3] = 0;
3127 hdw->cmd_buffer[4] = 0;
3128 hdw->cmd_buffer[5] = 0;
3129 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3130 hdw->cmd_buffer[7] = reg & 0xff;
3131
3132 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3133 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3134
3135 LOCK_GIVE(hdw->ctl_lock);
3136
3137 return ret;
3138}
3139
3140
Mike Isely681c7392007-11-26 01:48:52 -03003141void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003142{
3143 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003144 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3145 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003146 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003147 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003148 }
Mike Isely681c7392007-11-26 01:48:52 -03003149 hdw->flag_ok = 0;
3150 trace_stbit("flag_ok",hdw->flag_ok);
3151 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003152}
3153
3154
3155void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3156{
3157 int ret;
3158 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003159 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003160 if (ret == 1) {
3161 ret = usb_reset_device(hdw->usb_dev);
3162 usb_unlock_device(hdw->usb_dev);
3163 } else {
3164 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3165 "Failed to lock USB device ret=%d",ret);
3166 }
3167 if (init_pause_msec) {
3168 pvr2_trace(PVR2_TRACE_INFO,
3169 "Waiting %u msec for hardware to settle",
3170 init_pause_msec);
3171 msleep(init_pause_msec);
3172 }
3173
3174}
3175
3176
3177void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3178{
3179 char da[1];
3180 unsigned int pipe;
3181 int ret;
3182
3183 if (!hdw->usb_dev) return;
3184
3185 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3186
3187 da[0] = val ? 0x01 : 0x00;
3188
3189 /* Write the CPUCS register on the 8051. The lsb of the register
3190 is the reset bit; a 1 asserts reset while a 0 clears it. */
3191 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3192 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3193 if (ret < 0) {
3194 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3195 "cpureset_assert(%d) error=%d",val,ret);
3196 pvr2_hdw_render_useless(hdw);
3197 }
3198}
3199
3200
3201int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3202{
3203 int status;
3204 LOCK_TAKE(hdw->ctl_lock); do {
3205 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset");
Michael Krufky8d364362007-01-22 02:17:55 -03003206 hdw->cmd_buffer[0] = FX2CMD_DEEP_RESET;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003207 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003208 } while (0); LOCK_GIVE(hdw->ctl_lock);
3209 return status;
3210}
3211
3212
Michael Krufkye1edb192008-04-22 14:45:39 -03003213static int pvr2_hdw_cmd_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Iselyd8554972006-06-26 20:58:46 -03003214{
3215 int status;
3216 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufkye1edb192008-04-22 14:45:39 -03003217 if (onoff) {
3218 pvr2_trace(PVR2_TRACE_INIT, "Requesting powerup");
3219 hdw->cmd_buffer[0] = FX2CMD_POWER_ON;
3220 } else {
3221 pvr2_trace(PVR2_TRACE_INIT, "Requesting powerdown");
3222 hdw->cmd_buffer[0] = FX2CMD_POWER_OFF;
3223 }
3224 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
Mike Iselyd8554972006-06-26 20:58:46 -03003225 } while (0); LOCK_GIVE(hdw->ctl_lock);
3226 return status;
3227}
3228
Michael Krufkye1edb192008-04-22 14:45:39 -03003229int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3230{
3231 return pvr2_hdw_cmd_power_ctrl(hdw, 1);
3232}
3233
3234int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3235{
3236 return pvr2_hdw_cmd_power_ctrl(hdw, 0);
3237}
3238
Mike Iselyd8554972006-06-26 20:58:46 -03003239
3240int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3241{
3242 if (!hdw->decoder_ctrl) {
3243 pvr2_trace(PVR2_TRACE_INIT,
3244 "Unable to reset decoder: nothing attached");
3245 return -ENOTTY;
3246 }
3247
3248 if (!hdw->decoder_ctrl->force_reset) {
3249 pvr2_trace(PVR2_TRACE_INIT,
3250 "Unable to reset decoder: not implemented");
3251 return -ENOTTY;
3252 }
3253
3254 pvr2_trace(PVR2_TRACE_INIT,
3255 "Requesting decoder reset");
3256 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3257 return 0;
3258}
3259
3260
Mike Isely62433e32008-04-22 14:45:40 -03003261static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003262{
3263 int status;
3264
3265 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003266 pvr2_trace(PVR2_TRACE_INIT,
3267 "Issuing fe demod wake command (%s)",
3268 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003269 hdw->flag_ok = !0;
3270 hdw->cmd_buffer[0] = FX2CMD_HCW_DEMOD_RESETIN;
3271 hdw->cmd_buffer[1] = onoff;
3272 status = pvr2_send_request(hdw, hdw->cmd_buffer, 2, NULL, 0);
3273 } while (0); LOCK_GIVE(hdw->ctl_lock);
3274
3275 return status;
3276}
3277
Mike Isely84147f32008-04-22 14:45:40 -03003278
Mike Isely62433e32008-04-22 14:45:40 -03003279static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003280{
3281 int status;
3282
3283 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003284 pvr2_trace(PVR2_TRACE_INIT,
3285 "Issuing fe power command to CPLD (%s)",
3286 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003287 hdw->flag_ok = !0;
3288 hdw->cmd_buffer[0] =
3289 (onoff ? FX2CMD_ONAIR_DTV_POWER_ON :
3290 FX2CMD_ONAIR_DTV_POWER_OFF);
3291 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
3292 } while (0); LOCK_GIVE(hdw->ctl_lock);
3293
3294 return status;
3295}
3296
Mike Isely62433e32008-04-22 14:45:40 -03003297
3298static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3299 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003300{
3301 int status;
3302 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003303 pvr2_trace(PVR2_TRACE_INIT,
3304 "Issuing onair digital setup command (%s)",
3305 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003306 hdw->cmd_buffer[0] =
3307 (onoff ? FX2CMD_ONAIR_DTV_STREAMING_ON :
3308 FX2CMD_ONAIR_DTV_STREAMING_OFF);
3309 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
3310 } while (0); LOCK_GIVE(hdw->ctl_lock);
3311 return status;
3312}
3313
Mike Isely62433e32008-04-22 14:45:40 -03003314
3315static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3316{
3317 int cmode;
3318 /* Compare digital/analog desired setting with current setting. If
3319 they don't match, fix it... */
3320 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3321 if (cmode == hdw->pathway_state) {
3322 /* They match; nothing to do */
3323 return;
3324 }
3325
3326 switch (hdw->hdw_desc->digital_control_scheme) {
3327 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3328 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3329 if (cmode == PVR2_PATHWAY_ANALOG) {
3330 /* If moving to analog mode, also force the decoder
3331 to reset. If no decoder is attached, then it's
3332 ok to ignore this because if/when the decoder
3333 attaches, it will reset itself at that time. */
3334 pvr2_hdw_cmd_decoder_reset(hdw);
3335 }
3336 break;
3337 case PVR2_DIGITAL_SCHEME_ONAIR:
3338 /* Supposedly we should always have the power on whether in
3339 digital or analog mode. But for now do what appears to
3340 work... */
3341 if (digitalFl) pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,!0);
3342 pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,digitalFl);
3343 if (!digitalFl) pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,0);
3344 break;
3345 default: break;
3346 }
3347
Mike Isely1b9c18c2008-04-22 14:45:41 -03003348 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03003349 hdw->pathway_state = cmode;
3350}
3351
3352
Mike Isely40381cb2008-04-22 14:45:42 -03003353void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03003354{
3355 /* change some GPIO data
3356 *
3357 * note: bit d7 of dir appears to control the LED,
3358 * so we shut it off here.
3359 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03003360 */
Mike Isely40381cb2008-04-22 14:45:42 -03003361 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003362 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03003363 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03003364 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03003365 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03003366 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03003367}
Mike Iselyc55a97d2008-04-22 14:45:41 -03003368
Mike Isely40381cb2008-04-22 14:45:42 -03003369
3370typedef void (*led_method_func)(struct pvr2_hdw *,int);
3371
3372static led_method_func led_methods[] = {
3373 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
3374};
3375
3376
3377/* Toggle LED */
3378static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
3379{
3380 unsigned int scheme_id;
3381 led_method_func fp;
3382
3383 if ((!onoff) == (!hdw->led_on)) return;
3384
3385 hdw->led_on = onoff != 0;
3386
3387 scheme_id = hdw->hdw_desc->led_scheme;
3388 if (scheme_id < ARRAY_SIZE(led_methods)) {
3389 fp = led_methods[scheme_id];
3390 } else {
3391 fp = NULL;
3392 }
3393
3394 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03003395}
3396
3397
Mike Iselye61b6fc2006-07-18 22:42:18 -03003398/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003399static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003400{
Mike Isely62433e32008-04-22 14:45:40 -03003401 int status,cc;
3402 if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3403 hdw->hdw_desc->digital_control_scheme ==
3404 PVR2_DIGITAL_SCHEME_HAUPPAUGE) {
3405 cc = (runFl ?
3406 FX2CMD_HCW_DTV_STREAMING_ON :
3407 FX2CMD_HCW_DTV_STREAMING_OFF);
3408 } else {
3409 cc = (runFl ?
3410 FX2CMD_STREAMING_ON :
3411 FX2CMD_STREAMING_OFF);
3412 }
3413
Mike Iselyd8554972006-06-26 20:58:46 -03003414 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003415 hdw->cmd_buffer[0] = cc;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003416 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003417 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03003418 return status;
3419}
3420
3421
Mike Isely62433e32008-04-22 14:45:40 -03003422/* Evaluate whether or not state_pathway_ok can change */
3423static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3424{
3425 if (hdw->state_pathway_ok) {
3426 /* Nothing to do if pathway is already ok */
3427 return 0;
3428 }
3429 if (!hdw->state_pipeline_idle) {
3430 /* Not allowed to change anything if pipeline is not idle */
3431 return 0;
3432 }
3433 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3434 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003435 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003436 return !0;
3437}
3438
3439
Mike Isely681c7392007-11-26 01:48:52 -03003440/* Evaluate whether or not state_encoder_ok can change */
3441static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3442{
3443 if (hdw->state_encoder_ok) return 0;
3444 if (hdw->flag_tripped) return 0;
3445 if (hdw->state_encoder_run) return 0;
3446 if (hdw->state_encoder_config) return 0;
3447 if (hdw->state_decoder_run) return 0;
3448 if (hdw->state_usbstream_run) return 0;
Mike Isely62433e32008-04-22 14:45:40 -03003449 if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003450 if (pvr2_upload_firmware2(hdw) < 0) {
3451 hdw->flag_tripped = !0;
3452 trace_stbit("flag_tripped",hdw->flag_tripped);
3453 return !0;
3454 }
3455 hdw->state_encoder_ok = !0;
3456 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3457 return !0;
3458}
3459
3460
3461/* Evaluate whether or not state_encoder_config can change */
3462static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3463{
3464 if (hdw->state_encoder_config) {
3465 if (hdw->state_encoder_ok) {
3466 if (hdw->state_pipeline_req &&
3467 !hdw->state_pipeline_pause) return 0;
3468 }
3469 hdw->state_encoder_config = 0;
3470 hdw->state_encoder_waitok = 0;
3471 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3472 /* paranoia - solve race if timer just completed */
3473 del_timer_sync(&hdw->encoder_wait_timer);
3474 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003475 if (!hdw->state_pathway_ok ||
3476 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3477 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003478 !hdw->state_pipeline_idle ||
3479 hdw->state_pipeline_pause ||
3480 !hdw->state_pipeline_req ||
3481 !hdw->state_pipeline_config) {
3482 /* We must reset the enforced wait interval if
3483 anything has happened that might have disturbed
3484 the encoder. This should be a rare case. */
3485 if (timer_pending(&hdw->encoder_wait_timer)) {
3486 del_timer_sync(&hdw->encoder_wait_timer);
3487 }
3488 if (hdw->state_encoder_waitok) {
3489 /* Must clear the state - therefore we did
3490 something to a state bit and must also
3491 return true. */
3492 hdw->state_encoder_waitok = 0;
3493 trace_stbit("state_encoder_waitok",
3494 hdw->state_encoder_waitok);
3495 return !0;
3496 }
3497 return 0;
3498 }
3499 if (!hdw->state_encoder_waitok) {
3500 if (!timer_pending(&hdw->encoder_wait_timer)) {
3501 /* waitok flag wasn't set and timer isn't
3502 running. Check flag once more to avoid
3503 a race then start the timer. This is
3504 the point when we measure out a minimal
3505 quiet interval before doing something to
3506 the encoder. */
3507 if (!hdw->state_encoder_waitok) {
3508 hdw->encoder_wait_timer.expires =
3509 jiffies + (HZ*50/1000);
3510 add_timer(&hdw->encoder_wait_timer);
3511 }
3512 }
3513 /* We can't continue until we know we have been
3514 quiet for the interval measured by this
3515 timer. */
3516 return 0;
3517 }
3518 pvr2_encoder_configure(hdw);
3519 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3520 }
3521 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3522 return !0;
3523}
3524
3525
3526/* Evaluate whether or not state_encoder_run can change */
3527static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3528{
3529 if (hdw->state_encoder_run) {
3530 if (hdw->state_encoder_ok) {
Mike Isely62433e32008-04-22 14:45:40 -03003531 if (hdw->state_decoder_run &&
3532 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003533 if (pvr2_encoder_stop(hdw) < 0) return !0;
3534 }
3535 hdw->state_encoder_run = 0;
3536 } else {
3537 if (!hdw->state_encoder_ok) return 0;
3538 if (!hdw->state_decoder_run) return 0;
Mike Isely62433e32008-04-22 14:45:40 -03003539 if (!hdw->state_pathway_ok) return 0;
3540 if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003541 if (pvr2_encoder_start(hdw) < 0) return !0;
3542 hdw->state_encoder_run = !0;
3543 }
3544 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3545 return !0;
3546}
3547
3548
3549/* Timeout function for quiescent timer. */
3550static void pvr2_hdw_quiescent_timeout(unsigned long data)
3551{
3552 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3553 hdw->state_decoder_quiescent = !0;
3554 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3555 hdw->state_stale = !0;
3556 queue_work(hdw->workqueue,&hdw->workpoll);
3557}
3558
3559
3560/* Timeout function for encoder wait timer. */
3561static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3562{
3563 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3564 hdw->state_encoder_waitok = !0;
3565 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3566 hdw->state_stale = !0;
3567 queue_work(hdw->workqueue,&hdw->workpoll);
3568}
3569
3570
3571/* Evaluate whether or not state_decoder_run can change */
3572static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3573{
3574 if (hdw->state_decoder_run) {
3575 if (hdw->state_encoder_ok) {
3576 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03003577 !hdw->state_pipeline_pause &&
3578 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003579 }
3580 if (!hdw->flag_decoder_missed) {
3581 pvr2_decoder_enable(hdw,0);
3582 }
3583 hdw->state_decoder_quiescent = 0;
3584 hdw->state_decoder_run = 0;
3585 /* paranoia - solve race if timer just completed */
3586 del_timer_sync(&hdw->quiescent_timer);
3587 } else {
3588 if (!hdw->state_decoder_quiescent) {
3589 if (!timer_pending(&hdw->quiescent_timer)) {
3590 /* We don't do something about the
3591 quiescent timer until right here because
3592 we also want to catch cases where the
3593 decoder was already not running (like
3594 after initialization) as opposed to
3595 knowing that we had just stopped it.
3596 The second flag check is here to cover a
3597 race - the timer could have run and set
3598 this flag just after the previous check
3599 but before we did the pending check. */
3600 if (!hdw->state_decoder_quiescent) {
3601 hdw->quiescent_timer.expires =
3602 jiffies + (HZ*50/1000);
3603 add_timer(&hdw->quiescent_timer);
3604 }
3605 }
3606 /* Don't allow decoder to start again until it has
3607 been quiesced first. This little detail should
3608 hopefully further stabilize the encoder. */
3609 return 0;
3610 }
Mike Isely62433e32008-04-22 14:45:40 -03003611 if (!hdw->state_pathway_ok ||
3612 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3613 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03003614 hdw->state_pipeline_pause ||
3615 !hdw->state_pipeline_config ||
3616 !hdw->state_encoder_config ||
3617 !hdw->state_encoder_ok) return 0;
3618 del_timer_sync(&hdw->quiescent_timer);
3619 if (hdw->flag_decoder_missed) return 0;
3620 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3621 hdw->state_decoder_quiescent = 0;
3622 hdw->state_decoder_run = !0;
3623 }
3624 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3625 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3626 return !0;
3627}
3628
3629
3630/* Evaluate whether or not state_usbstream_run can change */
3631static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3632{
3633 if (hdw->state_usbstream_run) {
Mike Isely62433e32008-04-22 14:45:40 -03003634 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3635 if (hdw->state_encoder_ok &&
3636 hdw->state_encoder_run &&
3637 hdw->state_pathway_ok) return 0;
3638 } else {
3639 if (hdw->state_pipeline_req &&
3640 !hdw->state_pipeline_pause &&
3641 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003642 }
3643 pvr2_hdw_cmd_usbstream(hdw,0);
3644 hdw->state_usbstream_run = 0;
3645 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003646 if (!hdw->state_pipeline_req ||
3647 hdw->state_pipeline_pause ||
3648 !hdw->state_pathway_ok) return 0;
3649 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3650 if (!hdw->state_encoder_ok ||
3651 !hdw->state_encoder_run) return 0;
3652 }
Mike Isely681c7392007-11-26 01:48:52 -03003653 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3654 hdw->state_usbstream_run = !0;
3655 }
3656 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3657 return !0;
3658}
3659
3660
3661/* Attempt to configure pipeline, if needed */
3662static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3663{
3664 if (hdw->state_pipeline_config ||
3665 hdw->state_pipeline_pause) return 0;
3666 pvr2_hdw_commit_execute(hdw);
3667 return !0;
3668}
3669
3670
3671/* Update pipeline idle and pipeline pause tracking states based on other
3672 inputs. This must be called whenever the other relevant inputs have
3673 changed. */
3674static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3675{
3676 unsigned int st;
3677 int updatedFl = 0;
3678 /* Update pipeline state */
3679 st = !(hdw->state_encoder_run ||
3680 hdw->state_decoder_run ||
3681 hdw->state_usbstream_run ||
3682 (!hdw->state_decoder_quiescent));
3683 if (!st != !hdw->state_pipeline_idle) {
3684 hdw->state_pipeline_idle = st;
3685 updatedFl = !0;
3686 }
3687 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3688 hdw->state_pipeline_pause = 0;
3689 updatedFl = !0;
3690 }
3691 return updatedFl;
3692}
3693
3694
3695typedef int (*state_eval_func)(struct pvr2_hdw *);
3696
3697/* Set of functions to be run to evaluate various states in the driver. */
3698const static state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03003699 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03003700 state_eval_pipeline_config,
3701 state_eval_encoder_ok,
3702 state_eval_encoder_config,
3703 state_eval_decoder_run,
3704 state_eval_encoder_run,
3705 state_eval_usbstream_run,
3706};
3707
3708
3709/* Process various states and return true if we did anything interesting. */
3710static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
3711{
3712 unsigned int i;
3713 int state_updated = 0;
3714 int check_flag;
3715
3716 if (!hdw->state_stale) return 0;
3717 if ((hdw->fw1_state != FW1_STATE_OK) ||
3718 !hdw->flag_ok) {
3719 hdw->state_stale = 0;
3720 return !0;
3721 }
3722 /* This loop is the heart of the entire driver. It keeps trying to
3723 evaluate various bits of driver state until nothing changes for
3724 one full iteration. Each "bit of state" tracks some global
3725 aspect of the driver, e.g. whether decoder should run, if
3726 pipeline is configured, usb streaming is on, etc. We separately
3727 evaluate each of those questions based on other driver state to
3728 arrive at the correct running configuration. */
3729 do {
3730 check_flag = 0;
3731 state_update_pipeline_state(hdw);
3732 /* Iterate over each bit of state */
3733 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
3734 if ((*eval_funcs[i])(hdw)) {
3735 check_flag = !0;
3736 state_updated = !0;
3737 state_update_pipeline_state(hdw);
3738 }
3739 }
3740 } while (check_flag && hdw->flag_ok);
3741 hdw->state_stale = 0;
3742 trace_stbit("state_stale",hdw->state_stale);
3743 return state_updated;
3744}
3745
3746
Mike Isely62433e32008-04-22 14:45:40 -03003747static const char *pvr2_pathway_state_name(int id)
3748{
3749 switch (id) {
3750 case PVR2_PATHWAY_ANALOG: return "analog";
3751 case PVR2_PATHWAY_DIGITAL: return "digital";
3752 default: return "unknown";
3753 }
3754}
3755
3756
Mike Isely681c7392007-11-26 01:48:52 -03003757static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
3758 char *buf,unsigned int acnt)
3759{
3760 switch (which) {
3761 case 0:
3762 return scnprintf(
3763 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03003764 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03003765 (hdw->flag_ok ? " <ok>" : " <fail>"),
3766 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
3767 (hdw->flag_disconnected ? " <disconnected>" :
3768 " <connected>"),
3769 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03003770 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
3771 pvr2_pathway_state_name(hdw->pathway_state));
3772
Mike Isely681c7392007-11-26 01:48:52 -03003773 case 1:
3774 return scnprintf(
3775 buf,acnt,
3776 "pipeline:%s%s%s%s",
3777 (hdw->state_pipeline_idle ? " <idle>" : ""),
3778 (hdw->state_pipeline_config ?
3779 " <configok>" : " <stale>"),
3780 (hdw->state_pipeline_req ? " <req>" : ""),
3781 (hdw->state_pipeline_pause ? " <pause>" : ""));
3782 case 2:
3783 return scnprintf(
3784 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03003785 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03003786 (hdw->state_decoder_run ?
3787 " <decode:run>" :
3788 (hdw->state_decoder_quiescent ?
3789 "" : " <decode:stop>")),
3790 (hdw->state_decoder_quiescent ?
3791 " <decode:quiescent>" : ""),
3792 (hdw->state_encoder_ok ?
3793 "" : " <encode:init>"),
3794 (hdw->state_encoder_run ?
3795 " <encode:run>" : " <encode:stop>"),
3796 (hdw->state_encoder_config ?
3797 " <encode:configok>" :
3798 (hdw->state_encoder_waitok ?
3799 "" : " <encode:wait>")),
3800 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03003801 " <usb:run>" : " <usb:stop>"),
3802 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03003803 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03003804 break;
3805 case 3:
3806 return scnprintf(
3807 buf,acnt,
3808 "state: %s",
3809 pvr2_get_state_name(hdw->master_state));
3810 break;
3811 default: break;
3812 }
3813 return 0;
3814}
3815
3816
3817unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
3818 char *buf,unsigned int acnt)
3819{
3820 unsigned int bcnt,ccnt,idx;
3821 bcnt = 0;
3822 LOCK_TAKE(hdw->big_lock);
3823 for (idx = 0; ; idx++) {
3824 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
3825 if (!ccnt) break;
3826 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3827 if (!acnt) break;
3828 buf[0] = '\n'; ccnt = 1;
3829 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3830 }
3831 LOCK_GIVE(hdw->big_lock);
3832 return bcnt;
3833}
3834
3835
3836static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
3837{
3838 char buf[128];
3839 unsigned int idx,ccnt;
3840
3841 for (idx = 0; ; idx++) {
3842 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
3843 if (!ccnt) break;
3844 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
3845 }
3846}
3847
3848
3849/* Evaluate and update the driver's current state, taking various actions
3850 as appropriate for the update. */
3851static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
3852{
3853 unsigned int st;
3854 int state_updated = 0;
3855 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03003856 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03003857
3858 pvr2_trace(PVR2_TRACE_STBITS,
3859 "Drive state check START");
3860 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3861 pvr2_hdw_state_log_state(hdw);
3862 }
3863
3864 /* Process all state and get back over disposition */
3865 state_updated = pvr2_hdw_state_update(hdw);
3866
Mike Isely1b9c18c2008-04-22 14:45:41 -03003867 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
3868
Mike Isely681c7392007-11-26 01:48:52 -03003869 /* Update master state based upon all other states. */
3870 if (!hdw->flag_ok) {
3871 st = PVR2_STATE_DEAD;
3872 } else if (hdw->fw1_state != FW1_STATE_OK) {
3873 st = PVR2_STATE_COLD;
Mike Isely1b9c18c2008-04-22 14:45:41 -03003874 } else if (analog_mode && !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03003875 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03003876 } else if (hdw->flag_tripped ||
3877 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03003878 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03003879 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03003880 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03003881 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03003882 st = PVR2_STATE_RUN;
3883 } else {
3884 st = PVR2_STATE_READY;
3885 }
3886 if (hdw->master_state != st) {
3887 pvr2_trace(PVR2_TRACE_STATE,
3888 "Device state change from %s to %s",
3889 pvr2_get_state_name(hdw->master_state),
3890 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03003891 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03003892 hdw->master_state = st;
3893 state_updated = !0;
3894 callback_flag = !0;
3895 }
3896 if (state_updated) {
3897 /* Trigger anyone waiting on any state changes here. */
3898 wake_up(&hdw->state_wait_data);
3899 }
3900
3901 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3902 pvr2_hdw_state_log_state(hdw);
3903 }
3904 pvr2_trace(PVR2_TRACE_STBITS,
3905 "Drive state check DONE callback=%d",callback_flag);
3906
3907 return callback_flag;
3908}
3909
3910
3911/* Cause kernel thread to check / update driver state */
3912static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
3913{
3914 if (hdw->state_stale) return;
3915 hdw->state_stale = !0;
3916 trace_stbit("state_stale",hdw->state_stale);
3917 queue_work(hdw->workqueue,&hdw->workpoll);
3918}
3919
3920
3921void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
3922 struct pvr2_hdw_debug_info *ptr)
Mike Iselyd8554972006-06-26 20:58:46 -03003923{
3924 ptr->big_lock_held = hdw->big_lock_held;
3925 ptr->ctl_lock_held = hdw->ctl_lock_held;
Mike Iselyd8554972006-06-26 20:58:46 -03003926 ptr->flag_disconnected = hdw->flag_disconnected;
3927 ptr->flag_init_ok = hdw->flag_init_ok;
Mike Isely681c7392007-11-26 01:48:52 -03003928 ptr->flag_ok = hdw->flag_ok;
3929 ptr->fw1_state = hdw->fw1_state;
3930 ptr->flag_decoder_missed = hdw->flag_decoder_missed;
3931 ptr->flag_tripped = hdw->flag_tripped;
3932 ptr->state_encoder_ok = hdw->state_encoder_ok;
3933 ptr->state_encoder_run = hdw->state_encoder_run;
3934 ptr->state_decoder_run = hdw->state_decoder_run;
3935 ptr->state_usbstream_run = hdw->state_usbstream_run;
3936 ptr->state_decoder_quiescent = hdw->state_decoder_quiescent;
3937 ptr->state_pipeline_config = hdw->state_pipeline_config;
3938 ptr->state_pipeline_req = hdw->state_pipeline_req;
3939 ptr->state_pipeline_pause = hdw->state_pipeline_pause;
3940 ptr->state_pipeline_idle = hdw->state_pipeline_idle;
Mike Iselyd8554972006-06-26 20:58:46 -03003941 ptr->cmd_debug_state = hdw->cmd_debug_state;
3942 ptr->cmd_code = hdw->cmd_debug_code;
3943 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len;
3944 ptr->cmd_debug_read_len = hdw->cmd_debug_read_len;
3945 ptr->cmd_debug_timeout = hdw->ctl_timeout_flag;
3946 ptr->cmd_debug_write_pend = hdw->ctl_write_pend_flag;
3947 ptr->cmd_debug_read_pend = hdw->ctl_read_pend_flag;
3948 ptr->cmd_debug_rstatus = hdw->ctl_read_urb->status;
3949 ptr->cmd_debug_wstatus = hdw->ctl_read_urb->status;
3950}
3951
3952
Mike Isely681c7392007-11-26 01:48:52 -03003953void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
3954 struct pvr2_hdw_debug_info *ptr)
3955{
3956 LOCK_TAKE(hdw->ctl_lock); do {
3957 pvr2_hdw_get_debug_info_unlocked(hdw,ptr);
3958 } while(0); LOCK_GIVE(hdw->ctl_lock);
3959}
3960
3961
Mike Iselyd8554972006-06-26 20:58:46 -03003962int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
3963{
3964 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
3965}
3966
3967
3968int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
3969{
3970 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
3971}
3972
3973
3974int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
3975{
3976 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
3977}
3978
3979
3980int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
3981{
3982 u32 cval,nval;
3983 int ret;
3984 if (~msk) {
3985 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
3986 if (ret) return ret;
3987 nval = (cval & ~msk) | (val & msk);
3988 pvr2_trace(PVR2_TRACE_GPIO,
3989 "GPIO direction changing 0x%x:0x%x"
3990 " from 0x%x to 0x%x",
3991 msk,val,cval,nval);
3992 } else {
3993 nval = val;
3994 pvr2_trace(PVR2_TRACE_GPIO,
3995 "GPIO direction changing to 0x%x",nval);
3996 }
3997 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
3998}
3999
4000
4001int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
4002{
4003 u32 cval,nval;
4004 int ret;
4005 if (~msk) {
4006 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
4007 if (ret) return ret;
4008 nval = (cval & ~msk) | (val & msk);
4009 pvr2_trace(PVR2_TRACE_GPIO,
4010 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
4011 msk,val,cval,nval);
4012 } else {
4013 nval = val;
4014 pvr2_trace(PVR2_TRACE_GPIO,
4015 "GPIO output changing to 0x%x",nval);
4016 }
4017 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
4018}
4019
4020
Mike Isely7fb20fa2008-04-22 14:45:37 -03004021unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
4022{
4023 return hdw->input_avail_mask;
4024}
4025
4026
Mike Iselye61b6fc2006-07-18 22:42:18 -03004027/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004028static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004029{
4030 int result;
4031 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03004032 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03004033 result = pvr2_send_request(hdw,
4034 hdw->cmd_buffer,1,
4035 hdw->cmd_buffer,1);
4036 if (result < 0) break;
4037 result = hdw->cmd_buffer[0];
4038 } while(0); LOCK_GIVE(hdw->ctl_lock);
4039 return result;
4040}
4041
4042
Mike Isely32ffa9a2006-09-23 22:26:52 -03004043int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004044 u32 match_type, u32 match_chip, u64 reg_id,
4045 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004046{
4047#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004048 struct pvr2_i2c_client *cp;
4049 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004050 int stat = 0;
4051 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004052
Mike Isely201f5c92007-01-28 16:08:36 -03004053 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4054
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004055 req.match_type = match_type;
4056 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004057 req.reg = reg_id;
4058 if (setFl) req.val = *val_ptr;
4059 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004060 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004061 if (!v4l2_chip_match_i2c_client(
4062 cp->client,
4063 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004064 continue;
4065 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004066 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004067 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4068 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004069 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004070 okFl = !0;
4071 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004072 }
4073 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004074 if (okFl) {
4075 return stat;
4076 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004077 return -EINVAL;
4078#else
4079 return -ENOSYS;
4080#endif
4081}
4082
4083
Mike Iselyd8554972006-06-26 20:58:46 -03004084/*
4085 Stuff for Emacs to see, in order to encourage consistent editing style:
4086 *** Local Variables: ***
4087 *** mode: c ***
4088 *** fill-column: 75 ***
4089 *** tab-width: 8 ***
4090 *** c-basic-offset: 8 ***
4091 *** End: ***
4092 */