blob: 0941f0b434b7a79f6fbafcaade35d397564bfbab [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);
224static void pvr2_hdw_worker_init(struct work_struct *work);
225static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
226static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
227static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300228static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300229static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300230static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300231static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
232static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300233static void pvr2_hdw_quiescent_timeout(unsigned long);
234static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300235static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
236 unsigned int timeout,int probe_fl,
237 void *write_data,unsigned int write_len,
238 void *read_data,unsigned int read_len);
Mike Iselyd8554972006-06-26 20:58:46 -0300239
Mike Isely681c7392007-11-26 01:48:52 -0300240
241static void trace_stbit(const char *name,int val)
242{
243 pvr2_trace(PVR2_TRACE_STBITS,
244 "State bit %s <-- %s",
245 name,(val ? "true" : "false"));
246}
247
Mike Iselyd8554972006-06-26 20:58:46 -0300248static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
249{
250 struct pvr2_hdw *hdw = cptr->hdw;
251 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
252 *vp = hdw->freqTable[hdw->freqProgSlot-1];
253 } else {
254 *vp = 0;
255 }
256 return 0;
257}
258
259static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
260{
261 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300262 unsigned int slotId = hdw->freqProgSlot;
263 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
264 hdw->freqTable[slotId-1] = v;
265 /* Handle side effects correctly - if we're tuned to this
266 slot, then forgot the slot id relation since the stored
267 frequency has been changed. */
268 if (hdw->freqSelector) {
269 if (hdw->freqSlotRadio == slotId) {
270 hdw->freqSlotRadio = 0;
271 }
272 } else {
273 if (hdw->freqSlotTelevision == slotId) {
274 hdw->freqSlotTelevision = 0;
275 }
276 }
Mike Iselyd8554972006-06-26 20:58:46 -0300277 }
278 return 0;
279}
280
281static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
282{
283 *vp = cptr->hdw->freqProgSlot;
284 return 0;
285}
286
287static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
288{
289 struct pvr2_hdw *hdw = cptr->hdw;
290 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
291 hdw->freqProgSlot = v;
292 }
293 return 0;
294}
295
296static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
297{
Mike Isely1bde0282006-12-27 23:30:13 -0300298 struct pvr2_hdw *hdw = cptr->hdw;
299 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300300 return 0;
301}
302
Mike Isely1bde0282006-12-27 23:30:13 -0300303static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300304{
305 unsigned freq = 0;
306 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300307 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
308 if (slotId > 0) {
309 freq = hdw->freqTable[slotId-1];
310 if (!freq) return 0;
311 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300312 }
Mike Isely1bde0282006-12-27 23:30:13 -0300313 if (hdw->freqSelector) {
314 hdw->freqSlotRadio = slotId;
315 } else {
316 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300317 }
318 return 0;
319}
320
321static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
322{
Mike Isely1bde0282006-12-27 23:30:13 -0300323 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300324 return 0;
325}
326
327static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
328{
329 return cptr->hdw->freqDirty != 0;
330}
331
332static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
333{
334 cptr->hdw->freqDirty = 0;
335}
336
337static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
338{
Mike Isely1bde0282006-12-27 23:30:13 -0300339 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300340 return 0;
341}
342
Mike Isely3ad9fc32006-09-02 22:37:52 -0300343static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
344{
345 /* Actual maximum depends on the video standard in effect. */
346 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
347 *vp = 480;
348 } else {
349 *vp = 576;
350 }
351 return 0;
352}
353
354static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
355{
Mike Isely989eb152007-11-26 01:53:12 -0300356 /* Actual minimum depends on device digitizer type. */
357 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300358 *vp = 75;
359 } else {
360 *vp = 17;
361 }
362 return 0;
363}
364
Mike Isely1bde0282006-12-27 23:30:13 -0300365static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
366{
367 *vp = cptr->hdw->input_val;
368 return 0;
369}
370
Mike Isely29bf5b12008-04-22 14:45:37 -0300371static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
372{
Mike Isely7fb20fa2008-04-22 14:45:37 -0300373 return ((1 << v) & cptr->hdw->input_avail_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300374}
375
Mike Isely1bde0282006-12-27 23:30:13 -0300376static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
377{
378 struct pvr2_hdw *hdw = cptr->hdw;
379
380 if (hdw->input_val != v) {
381 hdw->input_val = v;
382 hdw->input_dirty = !0;
383 }
384
385 /* Handle side effects - if we switch to a mode that needs the RF
386 tuner, then select the right frequency choice as well and mark
387 it dirty. */
388 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
389 hdw->freqSelector = 0;
390 hdw->freqDirty = !0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300391 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
392 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
Mike Isely1bde0282006-12-27 23:30:13 -0300393 hdw->freqSelector = 1;
394 hdw->freqDirty = !0;
395 }
396 return 0;
397}
398
399static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
400{
401 return cptr->hdw->input_dirty != 0;
402}
403
404static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
405{
406 cptr->hdw->input_dirty = 0;
407}
408
Mike Isely5549f542006-12-27 23:28:54 -0300409
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300410static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
411{
Mike Isely644afdb2007-01-20 00:19:23 -0300412 unsigned long fv;
413 struct pvr2_hdw *hdw = cptr->hdw;
414 if (hdw->tuner_signal_stale) {
415 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300416 }
Mike Isely644afdb2007-01-20 00:19:23 -0300417 fv = hdw->tuner_signal_info.rangehigh;
418 if (!fv) {
419 /* Safety fallback */
420 *vp = TV_MAX_FREQ;
421 return 0;
422 }
423 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
424 fv = (fv * 125) / 2;
425 } else {
426 fv = fv * 62500;
427 }
428 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300429 return 0;
430}
431
432static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
433{
Mike Isely644afdb2007-01-20 00:19:23 -0300434 unsigned long fv;
435 struct pvr2_hdw *hdw = cptr->hdw;
436 if (hdw->tuner_signal_stale) {
437 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300438 }
Mike Isely644afdb2007-01-20 00:19:23 -0300439 fv = hdw->tuner_signal_info.rangelow;
440 if (!fv) {
441 /* Safety fallback */
442 *vp = TV_MIN_FREQ;
443 return 0;
444 }
445 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
446 fv = (fv * 125) / 2;
447 } else {
448 fv = fv * 62500;
449 }
450 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300451 return 0;
452}
453
Mike Iselyb30d2442006-06-25 20:05:01 -0300454static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
455{
456 return cptr->hdw->enc_stale != 0;
457}
458
459static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
460{
461 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300462 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300463}
464
465static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
466{
467 int ret;
468 struct v4l2_ext_controls cs;
469 struct v4l2_ext_control c1;
470 memset(&cs,0,sizeof(cs));
471 memset(&c1,0,sizeof(c1));
472 cs.controls = &c1;
473 cs.count = 1;
474 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300475 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300476 VIDIOC_G_EXT_CTRLS);
477 if (ret) return ret;
478 *vp = c1.value;
479 return 0;
480}
481
482static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
483{
484 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300485 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300486 struct v4l2_ext_controls cs;
487 struct v4l2_ext_control c1;
488 memset(&cs,0,sizeof(cs));
489 memset(&c1,0,sizeof(c1));
490 cs.controls = &c1;
491 cs.count = 1;
492 c1.id = cptr->info->v4l_id;
493 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300494 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
495 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300496 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300497 if (ret == -EBUSY) {
498 /* Oops. cx2341x is telling us it's not safe to change
499 this control while we're capturing. Make a note of this
500 fact so that the pipeline will be stopped the next time
501 controls are committed. Then go on ahead and store this
502 change anyway. */
503 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
504 0, &cs,
505 VIDIOC_S_EXT_CTRLS);
506 if (!ret) hdw->enc_unsafe_stale = !0;
507 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300508 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300509 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300510 return 0;
511}
512
513static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
514{
515 struct v4l2_queryctrl qctrl;
516 struct pvr2_ctl_info *info;
517 qctrl.id = cptr->info->v4l_id;
518 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
519 /* Strip out the const so we can adjust a function pointer. It's
520 OK to do this here because we know this is a dynamically created
521 control, so the underlying storage for the info pointer is (a)
522 private to us, and (b) not in read-only storage. Either we do
523 this or we significantly complicate the underlying control
524 implementation. */
525 info = (struct pvr2_ctl_info *)(cptr->info);
526 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
527 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300528 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300529 }
530 } else {
531 if (!(info->set_value)) {
532 info->set_value = ctrl_cx2341x_set;
533 }
534 }
535 return qctrl.flags;
536}
537
Mike Iselyd8554972006-06-26 20:58:46 -0300538static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
539{
Mike Isely681c7392007-11-26 01:48:52 -0300540 *vp = cptr->hdw->state_pipeline_req;
541 return 0;
542}
543
544static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
545{
546 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300547 return 0;
548}
549
550static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
551{
552 int result = pvr2_hdw_is_hsm(cptr->hdw);
553 *vp = PVR2_CVAL_HSM_FULL;
554 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
555 if (result) *vp = PVR2_CVAL_HSM_HIGH;
556 return 0;
557}
558
559static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
560{
561 *vp = cptr->hdw->std_mask_avail;
562 return 0;
563}
564
565static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
566{
567 struct pvr2_hdw *hdw = cptr->hdw;
568 v4l2_std_id ns;
569 ns = hdw->std_mask_avail;
570 ns = (ns & ~m) | (v & m);
571 if (ns == hdw->std_mask_avail) return 0;
572 hdw->std_mask_avail = ns;
573 pvr2_hdw_internal_set_std_avail(hdw);
574 pvr2_hdw_internal_find_stdenum(hdw);
575 return 0;
576}
577
578static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
579 char *bufPtr,unsigned int bufSize,
580 unsigned int *len)
581{
582 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
583 return 0;
584}
585
586static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
587 const char *bufPtr,unsigned int bufSize,
588 int *mskp,int *valp)
589{
590 int ret;
591 v4l2_std_id id;
592 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
593 if (ret < 0) return ret;
594 if (mskp) *mskp = id;
595 if (valp) *valp = id;
596 return 0;
597}
598
599static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
600{
601 *vp = cptr->hdw->std_mask_cur;
602 return 0;
603}
604
605static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
606{
607 struct pvr2_hdw *hdw = cptr->hdw;
608 v4l2_std_id ns;
609 ns = hdw->std_mask_cur;
610 ns = (ns & ~m) | (v & m);
611 if (ns == hdw->std_mask_cur) return 0;
612 hdw->std_mask_cur = ns;
613 hdw->std_dirty = !0;
614 pvr2_hdw_internal_find_stdenum(hdw);
615 return 0;
616}
617
618static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
619{
620 return cptr->hdw->std_dirty != 0;
621}
622
623static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
624{
625 cptr->hdw->std_dirty = 0;
626}
627
628static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
629{
Mike Isely18103c572007-01-20 00:09:47 -0300630 struct pvr2_hdw *hdw = cptr->hdw;
631 pvr2_i2c_core_status_poll(hdw);
632 *vp = hdw->tuner_signal_info.signal;
633 return 0;
634}
635
636static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
637{
638 int val = 0;
639 unsigned int subchan;
640 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300641 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300642 subchan = hdw->tuner_signal_info.rxsubchans;
643 if (subchan & V4L2_TUNER_SUB_MONO) {
644 val |= (1 << V4L2_TUNER_MODE_MONO);
645 }
646 if (subchan & V4L2_TUNER_SUB_STEREO) {
647 val |= (1 << V4L2_TUNER_MODE_STEREO);
648 }
649 if (subchan & V4L2_TUNER_SUB_LANG1) {
650 val |= (1 << V4L2_TUNER_MODE_LANG1);
651 }
652 if (subchan & V4L2_TUNER_SUB_LANG2) {
653 val |= (1 << V4L2_TUNER_MODE_LANG2);
654 }
655 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300656 return 0;
657}
658
Mike Iselyd8554972006-06-26 20:58:46 -0300659
660static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
661{
662 struct pvr2_hdw *hdw = cptr->hdw;
663 if (v < 0) return -EINVAL;
664 if (v > hdw->std_enum_cnt) return -EINVAL;
665 hdw->std_enum_cur = v;
666 if (!v) return 0;
667 v--;
668 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
669 hdw->std_mask_cur = hdw->std_defs[v].id;
670 hdw->std_dirty = !0;
671 return 0;
672}
673
674
675static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
676{
677 *vp = cptr->hdw->std_enum_cur;
678 return 0;
679}
680
681
682static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
683{
684 return cptr->hdw->std_dirty != 0;
685}
686
687
688static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
689{
690 cptr->hdw->std_dirty = 0;
691}
692
693
694#define DEFINT(vmin,vmax) \
695 .type = pvr2_ctl_int, \
696 .def.type_int.min_value = vmin, \
697 .def.type_int.max_value = vmax
698
699#define DEFENUM(tab) \
700 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300701 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300702 .def.type_enum.value_names = tab
703
Mike Isely33213962006-06-25 20:04:40 -0300704#define DEFBOOL \
705 .type = pvr2_ctl_bool
706
Mike Iselyd8554972006-06-26 20:58:46 -0300707#define DEFMASK(msk,tab) \
708 .type = pvr2_ctl_bitmask, \
709 .def.type_bitmask.valid_bits = msk, \
710 .def.type_bitmask.bit_names = tab
711
712#define DEFREF(vname) \
713 .set_value = ctrl_set_##vname, \
714 .get_value = ctrl_get_##vname, \
715 .is_dirty = ctrl_isdirty_##vname, \
716 .clear_dirty = ctrl_cleardirty_##vname
717
718
719#define VCREATE_FUNCS(vname) \
720static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
721{*vp = cptr->hdw->vname##_val; return 0;} \
722static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
723{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
724static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
725{return cptr->hdw->vname##_dirty != 0;} \
726static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
727{cptr->hdw->vname##_dirty = 0;}
728
729VCREATE_FUNCS(brightness)
730VCREATE_FUNCS(contrast)
731VCREATE_FUNCS(saturation)
732VCREATE_FUNCS(hue)
733VCREATE_FUNCS(volume)
734VCREATE_FUNCS(balance)
735VCREATE_FUNCS(bass)
736VCREATE_FUNCS(treble)
737VCREATE_FUNCS(mute)
Mike Iselyc05c0462006-06-25 20:04:25 -0300738VCREATE_FUNCS(audiomode)
739VCREATE_FUNCS(res_hor)
740VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300741VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300742
Mike Iselyd8554972006-06-26 20:58:46 -0300743/* Table definition of all controls which can be manipulated */
744static const struct pvr2_ctl_info control_defs[] = {
745 {
746 .v4l_id = V4L2_CID_BRIGHTNESS,
747 .desc = "Brightness",
748 .name = "brightness",
749 .default_value = 128,
750 DEFREF(brightness),
751 DEFINT(0,255),
752 },{
753 .v4l_id = V4L2_CID_CONTRAST,
754 .desc = "Contrast",
755 .name = "contrast",
756 .default_value = 68,
757 DEFREF(contrast),
758 DEFINT(0,127),
759 },{
760 .v4l_id = V4L2_CID_SATURATION,
761 .desc = "Saturation",
762 .name = "saturation",
763 .default_value = 64,
764 DEFREF(saturation),
765 DEFINT(0,127),
766 },{
767 .v4l_id = V4L2_CID_HUE,
768 .desc = "Hue",
769 .name = "hue",
770 .default_value = 0,
771 DEFREF(hue),
772 DEFINT(-128,127),
773 },{
774 .v4l_id = V4L2_CID_AUDIO_VOLUME,
775 .desc = "Volume",
776 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -0300777 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -0300778 DEFREF(volume),
779 DEFINT(0,65535),
780 },{
781 .v4l_id = V4L2_CID_AUDIO_BALANCE,
782 .desc = "Balance",
783 .name = "balance",
784 .default_value = 0,
785 DEFREF(balance),
786 DEFINT(-32768,32767),
787 },{
788 .v4l_id = V4L2_CID_AUDIO_BASS,
789 .desc = "Bass",
790 .name = "bass",
791 .default_value = 0,
792 DEFREF(bass),
793 DEFINT(-32768,32767),
794 },{
795 .v4l_id = V4L2_CID_AUDIO_TREBLE,
796 .desc = "Treble",
797 .name = "treble",
798 .default_value = 0,
799 DEFREF(treble),
800 DEFINT(-32768,32767),
801 },{
802 .v4l_id = V4L2_CID_AUDIO_MUTE,
803 .desc = "Mute",
804 .name = "mute",
805 .default_value = 0,
806 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -0300807 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300808 },{
Mike Iselyc05c0462006-06-25 20:04:25 -0300809 .desc = "Video Source",
810 .name = "input",
811 .internal_id = PVR2_CID_INPUT,
812 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -0300813 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -0300814 DEFREF(input),
815 DEFENUM(control_values_input),
816 },{
817 .desc = "Audio Mode",
818 .name = "audio_mode",
819 .internal_id = PVR2_CID_AUDIOMODE,
820 .default_value = V4L2_TUNER_MODE_STEREO,
821 DEFREF(audiomode),
822 DEFENUM(control_values_audiomode),
823 },{
824 .desc = "Horizontal capture resolution",
825 .name = "resolution_hor",
826 .internal_id = PVR2_CID_HRES,
827 .default_value = 720,
828 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300829 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -0300830 },{
831 .desc = "Vertical capture resolution",
832 .name = "resolution_ver",
833 .internal_id = PVR2_CID_VRES,
834 .default_value = 480,
835 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300836 DEFINT(17,576),
837 /* Hook in check for video standard and adjust maximum
838 depending on the standard. */
839 .get_max_value = ctrl_vres_max_get,
840 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -0300841 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300842 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -0300843 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
844 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -0300845 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -0300846 DEFREF(srate),
847 DEFENUM(control_values_srate),
848 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300849 .desc = "Tuner Frequency (Hz)",
850 .name = "frequency",
851 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -0300852 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -0300853 .set_value = ctrl_freq_set,
854 .get_value = ctrl_freq_get,
855 .is_dirty = ctrl_freq_is_dirty,
856 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -0300857 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300858 /* Hook in check for input value (tv/radio) and adjust
859 max/min values accordingly */
860 .get_max_value = ctrl_freq_max_get,
861 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300862 },{
863 .desc = "Channel",
864 .name = "channel",
865 .set_value = ctrl_channel_set,
866 .get_value = ctrl_channel_get,
867 DEFINT(0,FREQTABLE_SIZE),
868 },{
869 .desc = "Channel Program Frequency",
870 .name = "freq_table_value",
871 .set_value = ctrl_channelfreq_set,
872 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -0300873 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -0300874 /* Hook in check for input value (tv/radio) and adjust
875 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -0300876 .get_max_value = ctrl_freq_max_get,
877 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300878 },{
879 .desc = "Channel Program ID",
880 .name = "freq_table_channel",
881 .set_value = ctrl_channelprog_set,
882 .get_value = ctrl_channelprog_get,
883 DEFINT(0,FREQTABLE_SIZE),
884 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300885 .desc = "Streaming Enabled",
886 .name = "streaming_enabled",
887 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -0300888 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300889 },{
890 .desc = "USB Speed",
891 .name = "usb_speed",
892 .get_value = ctrl_hsm_get,
893 DEFENUM(control_values_hsm),
894 },{
Mike Isely681c7392007-11-26 01:48:52 -0300895 .desc = "Master State",
896 .name = "master_state",
897 .get_value = ctrl_masterstate_get,
898 DEFENUM(pvr2_state_names),
899 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300900 .desc = "Signal Present",
901 .name = "signal_present",
902 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -0300903 DEFINT(0,65535),
904 },{
905 .desc = "Audio Modes Present",
906 .name = "audio_modes_present",
907 .get_value = ctrl_audio_modes_present_get,
908 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
909 v4l. Nothing outside of this module cares about this,
910 but I reuse it in order to also reuse the
911 control_values_audiomode string table. */
912 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
913 (1 << V4L2_TUNER_MODE_STEREO)|
914 (1 << V4L2_TUNER_MODE_LANG1)|
915 (1 << V4L2_TUNER_MODE_LANG2)),
916 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -0300917 },{
918 .desc = "Video Standards Available Mask",
919 .name = "video_standard_mask_available",
920 .internal_id = PVR2_CID_STDAVAIL,
921 .skip_init = !0,
922 .get_value = ctrl_stdavail_get,
923 .set_value = ctrl_stdavail_set,
924 .val_to_sym = ctrl_std_val_to_sym,
925 .sym_to_val = ctrl_std_sym_to_val,
926 .type = pvr2_ctl_bitmask,
927 },{
928 .desc = "Video Standards In Use Mask",
929 .name = "video_standard_mask_active",
930 .internal_id = PVR2_CID_STDCUR,
931 .skip_init = !0,
932 .get_value = ctrl_stdcur_get,
933 .set_value = ctrl_stdcur_set,
934 .is_dirty = ctrl_stdcur_is_dirty,
935 .clear_dirty = ctrl_stdcur_clear_dirty,
936 .val_to_sym = ctrl_std_val_to_sym,
937 .sym_to_val = ctrl_std_sym_to_val,
938 .type = pvr2_ctl_bitmask,
939 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300940 .desc = "Video Standard Name",
941 .name = "video_standard",
942 .internal_id = PVR2_CID_STDENUM,
943 .skip_init = !0,
944 .get_value = ctrl_stdenumcur_get,
945 .set_value = ctrl_stdenumcur_set,
946 .is_dirty = ctrl_stdenumcur_is_dirty,
947 .clear_dirty = ctrl_stdenumcur_clear_dirty,
948 .type = pvr2_ctl_enum,
949 }
950};
951
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300952#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -0300953
954
955const char *pvr2_config_get_name(enum pvr2_config cfg)
956{
957 switch (cfg) {
958 case pvr2_config_empty: return "empty";
959 case pvr2_config_mpeg: return "mpeg";
960 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -0300961 case pvr2_config_pcm: return "pcm";
962 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -0300963 }
964 return "<unknown>";
965}
966
967
968struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
969{
970 return hdw->usb_dev;
971}
972
973
974unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
975{
976 return hdw->serial_number;
977}
978
Mike Isely31a18542007-04-08 01:11:47 -0300979
980const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
981{
982 return hdw->bus_info;
983}
984
985
Mike Isely1bde0282006-12-27 23:30:13 -0300986unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
987{
988 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
989}
990
991/* Set the currently tuned frequency and account for all possible
992 driver-core side effects of this action. */
993void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
994{
Mike Isely7c74e572007-01-20 00:15:41 -0300995 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -0300996 if (hdw->freqSelector) {
997 /* Swing over to radio frequency selection */
998 hdw->freqSelector = 0;
999 hdw->freqDirty = !0;
1000 }
Mike Isely1bde0282006-12-27 23:30:13 -03001001 if (hdw->freqValRadio != val) {
1002 hdw->freqValRadio = val;
1003 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001004 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001005 }
Mike Isely7c74e572007-01-20 00:15:41 -03001006 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001007 if (!(hdw->freqSelector)) {
1008 /* Swing over to television frequency selection */
1009 hdw->freqSelector = 1;
1010 hdw->freqDirty = !0;
1011 }
Mike Isely1bde0282006-12-27 23:30:13 -03001012 if (hdw->freqValTelevision != val) {
1013 hdw->freqValTelevision = val;
1014 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001015 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001016 }
Mike Isely1bde0282006-12-27 23:30:13 -03001017 }
1018}
1019
Mike Iselyd8554972006-06-26 20:58:46 -03001020int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1021{
1022 return hdw->unit_number;
1023}
1024
1025
1026/* Attempt to locate one of the given set of files. Messages are logged
1027 appropriate to what has been found. The return value will be 0 or
1028 greater on success (it will be the index of the file name found) and
1029 fw_entry will be filled in. Otherwise a negative error is returned on
1030 failure. If the return value is -ENOENT then no viable firmware file
1031 could be located. */
1032static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1033 const struct firmware **fw_entry,
1034 const char *fwtypename,
1035 unsigned int fwcount,
1036 const char *fwnames[])
1037{
1038 unsigned int idx;
1039 int ret = -EINVAL;
1040 for (idx = 0; idx < fwcount; idx++) {
1041 ret = request_firmware(fw_entry,
1042 fwnames[idx],
1043 &hdw->usb_dev->dev);
1044 if (!ret) {
1045 trace_firmware("Located %s firmware: %s;"
1046 " uploading...",
1047 fwtypename,
1048 fwnames[idx]);
1049 return idx;
1050 }
1051 if (ret == -ENOENT) continue;
1052 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1053 "request_firmware fatal error with code=%d",ret);
1054 return ret;
1055 }
1056 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1057 "***WARNING***"
1058 " Device %s firmware"
1059 " seems to be missing.",
1060 fwtypename);
1061 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1062 "Did you install the pvrusb2 firmware files"
1063 " in their proper location?");
1064 if (fwcount == 1) {
1065 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1066 "request_firmware unable to locate %s file %s",
1067 fwtypename,fwnames[0]);
1068 } else {
1069 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1070 "request_firmware unable to locate"
1071 " one of the following %s files:",
1072 fwtypename);
1073 for (idx = 0; idx < fwcount; idx++) {
1074 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1075 "request_firmware: Failed to find %s",
1076 fwnames[idx]);
1077 }
1078 }
1079 return ret;
1080}
1081
1082
1083/*
1084 * pvr2_upload_firmware1().
1085 *
1086 * Send the 8051 firmware to the device. After the upload, arrange for
1087 * device to re-enumerate.
1088 *
1089 * NOTE : the pointer to the firmware data given by request_firmware()
1090 * is not suitable for an usb transaction.
1091 *
1092 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001093static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001094{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001095 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001096 void *fw_ptr;
1097 unsigned int pipe;
1098 int ret;
1099 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001100
Mike Isely989eb152007-11-26 01:53:12 -03001101 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001102 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001103 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1104 "Connected device type defines"
1105 " no firmware to upload; ignoring firmware");
1106 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001107 }
1108
Mike Iselyd8554972006-06-26 20:58:46 -03001109 hdw->fw1_state = FW1_STATE_FAILED; // default result
1110
1111 trace_firmware("pvr2_upload_firmware1");
1112
1113 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001114 hdw->hdw_desc->fx2_firmware.cnt,
1115 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001116 if (ret < 0) {
1117 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1118 return ret;
1119 }
1120
1121 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1122 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1123
1124 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1125
1126 if (fw_entry->size != 0x2000){
1127 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1128 release_firmware(fw_entry);
1129 return -ENOMEM;
1130 }
1131
1132 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1133 if (fw_ptr == NULL){
1134 release_firmware(fw_entry);
1135 return -ENOMEM;
1136 }
1137
1138 /* We have to hold the CPU during firmware upload. */
1139 pvr2_hdw_cpureset_assert(hdw,1);
1140
1141 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1142 chunk. */
1143
1144 ret = 0;
1145 for(address = 0; address < fw_entry->size; address += 0x800) {
1146 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1147 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1148 0, fw_ptr, 0x800, HZ);
1149 }
1150
1151 trace_firmware("Upload done, releasing device's CPU");
1152
1153 /* Now release the CPU. It will disconnect and reconnect later. */
1154 pvr2_hdw_cpureset_assert(hdw,0);
1155
1156 kfree(fw_ptr);
1157 release_firmware(fw_entry);
1158
1159 trace_firmware("Upload done (%d bytes sent)",ret);
1160
1161 /* We should have written 8192 bytes */
1162 if (ret == 8192) {
1163 hdw->fw1_state = FW1_STATE_RELOAD;
1164 return 0;
1165 }
1166
1167 return -EIO;
1168}
1169
1170
1171/*
1172 * pvr2_upload_firmware2()
1173 *
1174 * This uploads encoder firmware on endpoint 2.
1175 *
1176 */
1177
1178int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1179{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001180 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001181 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001182 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001183 int actual_length;
1184 int ret = 0;
1185 int fwidx;
1186 static const char *fw_files[] = {
1187 CX2341X_FIRM_ENC_FILENAME,
1188 };
1189
Mike Isely989eb152007-11-26 01:53:12 -03001190 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001191 return 0;
1192 }
1193
Mike Iselyd8554972006-06-26 20:58:46 -03001194 trace_firmware("pvr2_upload_firmware2");
1195
1196 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001197 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001198 if (ret < 0) return ret;
1199 fwidx = ret;
1200 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001201 /* Since we're about to completely reinitialize the encoder,
1202 invalidate our cached copy of its configuration state. Next
1203 time we configure the encoder, then we'll fully configure it. */
1204 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001205
1206 /* First prepare firmware loading */
1207 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1208 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1209 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1210 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1211 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1212 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1213 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1214 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1215 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1216 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1217 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1218 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1219 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1220 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1221 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1222 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely567d7112007-01-28 15:38:55 -03001223 LOCK_TAKE(hdw->ctl_lock); do {
1224 hdw->cmd_buffer[0] = FX2CMD_FWPOST1;
Al Viro89952d12007-03-14 09:17:59 +00001225 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001226 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1227 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001228 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001229 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001230
1231 if (ret) {
1232 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1233 "firmware2 upload prep failed, ret=%d",ret);
1234 release_firmware(fw_entry);
1235 return ret;
1236 }
1237
1238 /* Now send firmware */
1239
1240 fw_len = fw_entry->size;
1241
Mike Isely90060d32007-02-08 02:02:53 -03001242 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001243 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1244 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001245 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001246 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001247 release_firmware(fw_entry);
1248 return -1;
1249 }
1250
1251 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1252 if (fw_ptr == NULL){
1253 release_firmware(fw_entry);
1254 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1255 "failed to allocate memory for firmware2 upload");
1256 return -ENOMEM;
1257 }
1258
1259 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1260
Mike Isely90060d32007-02-08 02:02:53 -03001261 fw_done = 0;
1262 for (fw_done = 0; fw_done < fw_len;) {
1263 bcnt = fw_len - fw_done;
1264 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1265 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1266 /* Usbsnoop log shows that we must swap bytes... */
1267 for (icnt = 0; icnt < bcnt/4 ; icnt++)
1268 ((u32 *)fw_ptr)[icnt] =
1269 ___swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001270
Mike Isely90060d32007-02-08 02:02:53 -03001271 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001272 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001273 ret |= (actual_length != bcnt);
1274 if (ret) break;
1275 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001276 }
1277
1278 trace_firmware("upload of %s : %i / %i ",
1279 fw_files[fwidx],fw_done,fw_len);
1280
1281 kfree(fw_ptr);
1282 release_firmware(fw_entry);
1283
1284 if (ret) {
1285 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1286 "firmware2 upload transfer failure");
1287 return ret;
1288 }
1289
1290 /* Finish upload */
1291
1292 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1293 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely567d7112007-01-28 15:38:55 -03001294 LOCK_TAKE(hdw->ctl_lock); do {
1295 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1296 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001297 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001298 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001299
1300 if (ret) {
1301 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1302 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001303 }
1304 return ret;
1305}
1306
1307
Mike Isely681c7392007-11-26 01:48:52 -03001308static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001309{
Mike Isely681c7392007-11-26 01:48:52 -03001310 if (st < ARRAY_SIZE(pvr2_state_names)) {
1311 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001312 }
Mike Isely681c7392007-11-26 01:48:52 -03001313 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001314}
1315
Mike Isely681c7392007-11-26 01:48:52 -03001316static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001317{
Mike Isely681c7392007-11-26 01:48:52 -03001318 if (!hdw->decoder_ctrl) {
1319 if (!hdw->flag_decoder_missed) {
1320 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1321 "WARNING: No decoder present");
1322 hdw->flag_decoder_missed = !0;
1323 trace_stbit("flag_decoder_missed",
1324 hdw->flag_decoder_missed);
1325 }
1326 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001327 }
Mike Isely681c7392007-11-26 01:48:52 -03001328 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001329 return 0;
1330}
1331
1332
Mike Isely681c7392007-11-26 01:48:52 -03001333void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1334{
1335 if (hdw->decoder_ctrl == ptr) return;
1336 hdw->decoder_ctrl = ptr;
1337 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1338 hdw->flag_decoder_missed = 0;
1339 trace_stbit("flag_decoder_missed",
1340 hdw->flag_decoder_missed);
1341 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1342 "Decoder has appeared");
1343 pvr2_hdw_state_sched(hdw);
1344 }
1345}
1346
1347
1348int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1349{
1350 return hdw->master_state;
1351}
1352
1353
1354static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1355{
1356 if (!hdw->flag_tripped) return 0;
1357 hdw->flag_tripped = 0;
1358 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1359 "Clearing driver error statuss");
1360 return !0;
1361}
1362
1363
1364int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1365{
1366 int fl;
1367 LOCK_TAKE(hdw->big_lock); do {
1368 fl = pvr2_hdw_untrip_unlocked(hdw);
1369 } while (0); LOCK_GIVE(hdw->big_lock);
1370 if (fl) pvr2_hdw_state_sched(hdw);
1371 return 0;
1372}
1373
1374
1375const char *pvr2_hdw_get_state_name(unsigned int id)
1376{
1377 if (id >= ARRAY_SIZE(pvr2_state_names)) return NULL;
1378 return pvr2_state_names[id];
1379}
1380
1381
Mike Iselyd8554972006-06-26 20:58:46 -03001382int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1383{
Mike Isely681c7392007-11-26 01:48:52 -03001384 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001385}
1386
1387
1388int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1389{
Mike Isely681c7392007-11-26 01:48:52 -03001390 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001391 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001392 pvr2_hdw_untrip_unlocked(hdw);
1393 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1394 hdw->state_pipeline_req = enable_flag != 0;
1395 pvr2_trace(PVR2_TRACE_START_STOP,
1396 "/*--TRACE_STREAM--*/ %s",
1397 enable_flag ? "enable" : "disable");
1398 }
1399 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001400 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001401 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1402 if (enable_flag) {
1403 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1404 if (st != PVR2_STATE_READY) return -EIO;
1405 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1406 }
1407 }
Mike Iselyd8554972006-06-26 20:58:46 -03001408 return 0;
1409}
1410
1411
1412int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1413{
Mike Isely681c7392007-11-26 01:48:52 -03001414 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001415 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001416 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1417 hdw->desired_stream_type = config;
1418 hdw->state_pipeline_config = 0;
1419 trace_stbit("state_pipeline_config",
1420 hdw->state_pipeline_config);
1421 pvr2_hdw_state_sched(hdw);
1422 }
Mike Iselyd8554972006-06-26 20:58:46 -03001423 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001424 if (fl) return 0;
1425 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001426}
1427
1428
1429static int get_default_tuner_type(struct pvr2_hdw *hdw)
1430{
1431 int unit_number = hdw->unit_number;
1432 int tp = -1;
1433 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1434 tp = tuner[unit_number];
1435 }
1436 if (tp < 0) return -EINVAL;
1437 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001438 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001439 return 0;
1440}
1441
1442
1443static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1444{
1445 int unit_number = hdw->unit_number;
1446 int tp = 0;
1447 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1448 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001449 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001450 }
Mike Isely6a540252007-12-02 23:51:34 -03001451 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001452}
1453
1454
1455static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1456{
1457 int unit_number = hdw->unit_number;
1458 int tp = 0;
1459 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1460 tp = tolerance[unit_number];
1461 }
1462 return tp;
1463}
1464
1465
1466static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1467{
1468 /* Try a harmless request to fetch the eeprom's address over
1469 endpoint 1. See what happens. Only the full FX2 image can
1470 respond to this. If this probe fails then likely the FX2
1471 firmware needs be loaded. */
1472 int result;
1473 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001474 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001475 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1476 hdw->cmd_buffer,1,
1477 hdw->cmd_buffer,1);
1478 if (result < 0) break;
1479 } while(0); LOCK_GIVE(hdw->ctl_lock);
1480 if (result) {
1481 pvr2_trace(PVR2_TRACE_INIT,
1482 "Probe of device endpoint 1 result status %d",
1483 result);
1484 } else {
1485 pvr2_trace(PVR2_TRACE_INIT,
1486 "Probe of device endpoint 1 succeeded");
1487 }
1488 return result == 0;
1489}
1490
Mike Isely9f66d4e2007-09-08 22:28:51 -03001491struct pvr2_std_hack {
1492 v4l2_std_id pat; /* Pattern to match */
1493 v4l2_std_id msk; /* Which bits we care about */
1494 v4l2_std_id std; /* What additional standards or default to set */
1495};
1496
1497/* This data structure labels specific combinations of standards from
1498 tveeprom that we'll try to recognize. If we recognize one, then assume
1499 a specified default standard to use. This is here because tveeprom only
1500 tells us about available standards not the intended default standard (if
1501 any) for the device in question. We guess the default based on what has
1502 been reported as available. Note that this is only for guessing a
1503 default - which can always be overridden explicitly - and if the user
1504 has otherwise named a default then that default will always be used in
1505 place of this table. */
1506const static struct pvr2_std_hack std_eeprom_maps[] = {
1507 { /* PAL(B/G) */
1508 .pat = V4L2_STD_B|V4L2_STD_GH,
1509 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1510 },
1511 { /* NTSC(M) */
1512 .pat = V4L2_STD_MN,
1513 .std = V4L2_STD_NTSC_M,
1514 },
1515 { /* PAL(I) */
1516 .pat = V4L2_STD_PAL_I,
1517 .std = V4L2_STD_PAL_I,
1518 },
1519 { /* SECAM(L/L') */
1520 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1521 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1522 },
1523 { /* PAL(D/D1/K) */
1524 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001525 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001526 },
1527};
1528
Mike Iselyd8554972006-06-26 20:58:46 -03001529static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1530{
1531 char buf[40];
1532 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001533 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001534
1535 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001536 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001537
1538 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001539 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001540 "Supported video standard(s) reported available"
1541 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001542 bcnt,buf);
1543
1544 hdw->std_mask_avail = hdw->std_mask_eeprom;
1545
Mike Isely3d290bd2007-12-03 01:47:12 -03001546 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001547 if (std2) {
1548 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001549 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001550 "Expanding supported video standards"
1551 " to include: %.*s",
1552 bcnt,buf);
1553 hdw->std_mask_avail |= std2;
1554 }
1555
1556 pvr2_hdw_internal_set_std_avail(hdw);
1557
1558 if (std1) {
1559 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001560 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001561 "Initial video standard forced to %.*s",
1562 bcnt,buf);
1563 hdw->std_mask_cur = std1;
1564 hdw->std_dirty = !0;
1565 pvr2_hdw_internal_find_stdenum(hdw);
1566 return;
1567 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001568 if (std3) {
1569 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1570 pvr2_trace(PVR2_TRACE_STD,
1571 "Initial video standard"
1572 " (determined by device type): %.*s",bcnt,buf);
1573 hdw->std_mask_cur = std3;
1574 hdw->std_dirty = !0;
1575 pvr2_hdw_internal_find_stdenum(hdw);
1576 return;
1577 }
Mike Iselyd8554972006-06-26 20:58:46 -03001578
Mike Isely9f66d4e2007-09-08 22:28:51 -03001579 {
1580 unsigned int idx;
1581 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1582 if (std_eeprom_maps[idx].msk ?
1583 ((std_eeprom_maps[idx].pat ^
1584 hdw->std_mask_eeprom) &
1585 std_eeprom_maps[idx].msk) :
1586 (std_eeprom_maps[idx].pat !=
1587 hdw->std_mask_eeprom)) continue;
1588 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1589 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001590 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001591 "Initial video standard guessed as %.*s",
1592 bcnt,buf);
1593 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1594 hdw->std_dirty = !0;
1595 pvr2_hdw_internal_find_stdenum(hdw);
1596 return;
1597 }
1598 }
1599
Mike Iselyd8554972006-06-26 20:58:46 -03001600 if (hdw->std_enum_cnt > 1) {
1601 // Autoselect the first listed standard
1602 hdw->std_enum_cur = 1;
1603 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1604 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001605 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001606 "Initial video standard auto-selected to %s",
1607 hdw->std_defs[hdw->std_enum_cur-1].name);
1608 return;
1609 }
1610
Mike Isely0885ba12006-06-25 21:30:47 -03001611 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001612 "Unable to select a viable initial video standard");
1613}
1614
1615
1616static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1617{
1618 int ret;
1619 unsigned int idx;
1620 struct pvr2_ctrl *cptr;
1621 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001622 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001623 if (!reloadFl) {
1624 reloadFl =
1625 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1626 == 0);
1627 if (reloadFl) {
1628 pvr2_trace(PVR2_TRACE_INIT,
1629 "USB endpoint config looks strange"
1630 "; possibly firmware needs to be"
1631 " loaded");
1632 }
1633 }
1634 if (!reloadFl) {
1635 reloadFl = !pvr2_hdw_check_firmware(hdw);
1636 if (reloadFl) {
1637 pvr2_trace(PVR2_TRACE_INIT,
1638 "Check for FX2 firmware failed"
1639 "; possibly firmware needs to be"
1640 " loaded");
1641 }
1642 }
Mike Iselyd8554972006-06-26 20:58:46 -03001643 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001644 if (pvr2_upload_firmware1(hdw) != 0) {
1645 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1646 "Failure uploading firmware1");
1647 }
1648 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001649 }
1650 }
Mike Iselyd8554972006-06-26 20:58:46 -03001651 hdw->fw1_state = FW1_STATE_OK;
1652
1653 if (initusbreset) {
1654 pvr2_hdw_device_reset(hdw);
1655 }
1656 if (!pvr2_hdw_dev_ok(hdw)) return;
1657
Mike Isely989eb152007-11-26 01:53:12 -03001658 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1659 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001660 }
1661
Mike Isely989eb152007-11-26 01:53:12 -03001662 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001663 pvr2_hdw_cmd_powerup(hdw);
1664 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001665 }
1666
1667 // This step MUST happen after the earlier powerup step.
1668 pvr2_i2c_core_init(hdw);
1669 if (!pvr2_hdw_dev_ok(hdw)) return;
1670
Mike Iselyc05c0462006-06-25 20:04:25 -03001671 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001672 cptr = hdw->controls + idx;
1673 if (cptr->info->skip_init) continue;
1674 if (!cptr->info->set_value) continue;
1675 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1676 }
1677
Mike Isely1bde0282006-12-27 23:30:13 -03001678 /* Set up special default values for the television and radio
1679 frequencies here. It's not really important what these defaults
1680 are, but I set them to something usable in the Chicago area just
1681 to make driver testing a little easier. */
1682
1683 /* US Broadcast channel 7 (175.25 MHz) */
1684 hdw->freqValTelevision = 175250000L;
1685 /* 104.3 MHz, a usable FM station for my area */
1686 hdw->freqValRadio = 104300000L;
1687
Mike Iselyd8554972006-06-26 20:58:46 -03001688 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1689 // thread-safe against the normal pvr2_send_request() mechanism.
1690 // (We should make it thread safe).
1691
Mike Iselyaaf78842007-11-26 02:04:11 -03001692 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1693 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001694 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03001695 if (ret < 0) {
1696 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1697 "Unable to determine location of eeprom,"
1698 " skipping");
1699 } else {
1700 hdw->eeprom_addr = ret;
1701 pvr2_eeprom_analyze(hdw);
1702 if (!pvr2_hdw_dev_ok(hdw)) return;
1703 }
1704 } else {
1705 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1706 hdw->tuner_updated = !0;
1707 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03001708 }
1709
1710 pvr2_hdw_setup_std(hdw);
1711
1712 if (!get_default_tuner_type(hdw)) {
1713 pvr2_trace(PVR2_TRACE_INIT,
1714 "pvr2_hdw_setup: Tuner type overridden to %d",
1715 hdw->tuner_type);
1716 }
1717
Mike Iselyd8554972006-06-26 20:58:46 -03001718 pvr2_i2c_core_check_stale(hdw);
1719 hdw->tuner_updated = 0;
1720
1721 if (!pvr2_hdw_dev_ok(hdw)) return;
1722
Mike Isely681c7392007-11-26 01:48:52 -03001723 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001724
1725 hdw->vid_stream = pvr2_stream_create();
1726 if (!pvr2_hdw_dev_ok(hdw)) return;
1727 pvr2_trace(PVR2_TRACE_INIT,
1728 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1729 if (hdw->vid_stream) {
1730 idx = get_default_error_tolerance(hdw);
1731 if (idx) {
1732 pvr2_trace(PVR2_TRACE_INIT,
1733 "pvr2_hdw_setup: video stream %p"
1734 " setting tolerance %u",
1735 hdw->vid_stream,idx);
1736 }
1737 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1738 PVR2_VID_ENDPOINT,idx);
1739 }
1740
1741 if (!pvr2_hdw_dev_ok(hdw)) return;
1742
Mike Iselyd8554972006-06-26 20:58:46 -03001743 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03001744
1745 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001746}
1747
1748
Mike Isely681c7392007-11-26 01:48:52 -03001749/* Set up the structure and attempt to put the device into a usable state.
1750 This can be a time-consuming operation, which is why it is not done
1751 internally as part of the create() step. */
1752static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001753{
1754 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03001755 do {
Mike Iselyd8554972006-06-26 20:58:46 -03001756 pvr2_hdw_setup_low(hdw);
1757 pvr2_trace(PVR2_TRACE_INIT,
1758 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03001759 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03001760 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03001761 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03001762 pvr2_trace(
1763 PVR2_TRACE_INFO,
1764 "Device initialization"
1765 " completed successfully.");
1766 break;
1767 }
1768 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1769 pvr2_trace(
1770 PVR2_TRACE_INFO,
1771 "Device microcontroller firmware"
1772 " (re)loaded; it should now reset"
1773 " and reconnect.");
1774 break;
1775 }
1776 pvr2_trace(
1777 PVR2_TRACE_ERROR_LEGS,
1778 "Device initialization was not successful.");
1779 if (hdw->fw1_state == FW1_STATE_MISSING) {
1780 pvr2_trace(
1781 PVR2_TRACE_ERROR_LEGS,
1782 "Giving up since device"
1783 " microcontroller firmware"
1784 " appears to be missing.");
1785 break;
1786 }
1787 }
1788 if (procreload) {
1789 pvr2_trace(
1790 PVR2_TRACE_ERROR_LEGS,
1791 "Attempting pvrusb2 recovery by reloading"
1792 " primary firmware.");
1793 pvr2_trace(
1794 PVR2_TRACE_ERROR_LEGS,
1795 "If this works, device should disconnect"
1796 " and reconnect in a sane state.");
1797 hdw->fw1_state = FW1_STATE_UNKNOWN;
1798 pvr2_upload_firmware1(hdw);
1799 } else {
1800 pvr2_trace(
1801 PVR2_TRACE_ERROR_LEGS,
1802 "***WARNING*** pvrusb2 device hardware"
1803 " appears to be jammed"
1804 " and I can't clear it.");
1805 pvr2_trace(
1806 PVR2_TRACE_ERROR_LEGS,
1807 "You might need to power cycle"
1808 " the pvrusb2 device"
1809 " in order to recover.");
1810 }
Mike Isely681c7392007-11-26 01:48:52 -03001811 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03001812 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001813}
1814
1815
1816/* Create and return a structure for interacting with the underlying
1817 hardware */
1818struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1819 const struct usb_device_id *devid)
1820{
Mike Isely7fb20fa2008-04-22 14:45:37 -03001821 unsigned int idx,cnt1,cnt2,m;
Mike Iselyd8554972006-06-26 20:58:46 -03001822 struct pvr2_hdw *hdw;
Mike Iselyd8554972006-06-26 20:58:46 -03001823 int valid_std_mask;
1824 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03001825 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03001826 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03001827 struct v4l2_queryctrl qctrl;
1828 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03001829
Mike Iselyd130fa82007-12-08 17:20:06 -03001830 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03001831
Mike Iselyca545f72007-01-20 00:37:11 -03001832 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001833 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03001834 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03001835 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03001836
1837 init_timer(&hdw->quiescent_timer);
1838 hdw->quiescent_timer.data = (unsigned long)hdw;
1839 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1840
1841 init_timer(&hdw->encoder_wait_timer);
1842 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1843 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1844
1845 hdw->master_state = PVR2_STATE_DEAD;
1846
1847 init_waitqueue_head(&hdw->state_wait_data);
1848
Mike Isely18103c572007-01-20 00:09:47 -03001849 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001850 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03001851
Mike Isely7fb20fa2008-04-22 14:45:37 -03001852 /* Calculate which inputs are OK */
1853 m = 0;
1854 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03001855 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
1856 m |= 1 << PVR2_CVAL_INPUT_DTV;
1857 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03001858 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
1859 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
1860 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
1861 hdw->input_avail_mask = m;
1862
Mike Isely62433e32008-04-22 14:45:40 -03001863 /* If not a hybrid device, pathway_state never changes. So
1864 initialize it here to what it should forever be. */
1865 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
1866 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
1867 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
1868 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
1869 }
1870
Mike Iselyc05c0462006-06-25 20:04:25 -03001871 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03001872 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03001873 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001874 GFP_KERNEL);
1875 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03001876 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03001877 for (idx = 0; idx < hdw->control_cnt; idx++) {
1878 cptr = hdw->controls + idx;
1879 cptr->hdw = hdw;
1880 }
Mike Iselyd8554972006-06-26 20:58:46 -03001881 for (idx = 0; idx < 32; idx++) {
1882 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1883 }
Mike Iselyc05c0462006-06-25 20:04:25 -03001884 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001885 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03001886 cptr->info = control_defs+idx;
1887 }
Mike Iselydbc40a02008-04-22 14:45:39 -03001888
1889 /* Ensure that default input choice is a valid one. */
1890 m = hdw->input_avail_mask;
1891 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
1892 if (!((1 << idx) & m)) continue;
1893 hdw->input_val = idx;
1894 break;
1895 }
1896
Mike Iselyb30d2442006-06-25 20:05:01 -03001897 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03001898 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03001899 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
1900 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03001901 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
1902 cptr = hdw->controls + idx + CTRLDEF_COUNT;
1903 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
1904 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
1905 ciptr->name = mpeg_ids[idx].strid;
1906 ciptr->v4l_id = mpeg_ids[idx].id;
1907 ciptr->skip_init = !0;
1908 ciptr->get_value = ctrl_cx2341x_get;
1909 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
1910 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
1911 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
1912 qctrl.id = ciptr->v4l_id;
1913 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
1914 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
1915 ciptr->set_value = ctrl_cx2341x_set;
1916 }
1917 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
1918 PVR2_CTLD_INFO_DESC_SIZE);
1919 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
1920 ciptr->default_value = qctrl.default_value;
1921 switch (qctrl.type) {
1922 default:
1923 case V4L2_CTRL_TYPE_INTEGER:
1924 ciptr->type = pvr2_ctl_int;
1925 ciptr->def.type_int.min_value = qctrl.minimum;
1926 ciptr->def.type_int.max_value = qctrl.maximum;
1927 break;
1928 case V4L2_CTRL_TYPE_BOOLEAN:
1929 ciptr->type = pvr2_ctl_bool;
1930 break;
1931 case V4L2_CTRL_TYPE_MENU:
1932 ciptr->type = pvr2_ctl_enum;
1933 ciptr->def.type_enum.value_names =
1934 cx2341x_ctrl_get_menu(ciptr->v4l_id);
1935 for (cnt1 = 0;
1936 ciptr->def.type_enum.value_names[cnt1] != NULL;
1937 cnt1++) { }
1938 ciptr->def.type_enum.count = cnt1;
1939 break;
1940 }
1941 cptr->info = ciptr;
1942 }
Mike Iselyd8554972006-06-26 20:58:46 -03001943
1944 // Initialize video standard enum dynamic control
1945 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
1946 if (cptr) {
1947 memcpy(&hdw->std_info_enum,cptr->info,
1948 sizeof(hdw->std_info_enum));
1949 cptr->info = &hdw->std_info_enum;
1950
1951 }
1952 // Initialize control data regarding video standard masks
1953 valid_std_mask = pvr2_std_get_usable();
1954 for (idx = 0; idx < 32; idx++) {
1955 if (!(valid_std_mask & (1 << idx))) continue;
1956 cnt1 = pvr2_std_id_to_str(
1957 hdw->std_mask_names[idx],
1958 sizeof(hdw->std_mask_names[idx])-1,
1959 1 << idx);
1960 hdw->std_mask_names[idx][cnt1] = 0;
1961 }
1962 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
1963 if (cptr) {
1964 memcpy(&hdw->std_info_avail,cptr->info,
1965 sizeof(hdw->std_info_avail));
1966 cptr->info = &hdw->std_info_avail;
1967 hdw->std_info_avail.def.type_bitmask.bit_names =
1968 hdw->std_mask_ptrs;
1969 hdw->std_info_avail.def.type_bitmask.valid_bits =
1970 valid_std_mask;
1971 }
1972 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
1973 if (cptr) {
1974 memcpy(&hdw->std_info_cur,cptr->info,
1975 sizeof(hdw->std_info_cur));
1976 cptr->info = &hdw->std_info_cur;
1977 hdw->std_info_cur.def.type_bitmask.bit_names =
1978 hdw->std_mask_ptrs;
1979 hdw->std_info_avail.def.type_bitmask.valid_bits =
1980 valid_std_mask;
1981 }
1982
1983 hdw->eeprom_addr = -1;
1984 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03001985 hdw->v4l_minor_number_video = -1;
1986 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03001987 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03001988 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
1989 if (!hdw->ctl_write_buffer) goto fail;
1990 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
1991 if (!hdw->ctl_read_buffer) goto fail;
1992 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
1993 if (!hdw->ctl_write_urb) goto fail;
1994 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
1995 if (!hdw->ctl_read_urb) goto fail;
1996
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03001997 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03001998 for (idx = 0; idx < PVR_NUM; idx++) {
1999 if (unit_pointers[idx]) continue;
2000 hdw->unit_number = idx;
2001 unit_pointers[idx] = hdw;
2002 break;
2003 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002004 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002005
2006 cnt1 = 0;
2007 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2008 cnt1 += cnt2;
2009 if (hdw->unit_number >= 0) {
2010 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2011 ('a' + hdw->unit_number));
2012 cnt1 += cnt2;
2013 }
2014 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2015 hdw->name[cnt1] = 0;
2016
Mike Isely681c7392007-11-26 01:48:52 -03002017 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2018 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2019 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
2020 INIT_WORK(&hdw->workinit,pvr2_hdw_worker_init);
2021
Mike Iselyd8554972006-06-26 20:58:46 -03002022 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2023 hdw->unit_number,hdw->name);
2024
2025 hdw->tuner_type = -1;
2026 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002027
2028 hdw->usb_intf = intf;
2029 hdw->usb_dev = interface_to_usbdev(intf);
2030
Mike Isely31a18542007-04-08 01:11:47 -03002031 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2032 "usb %s address %d",
2033 hdw->usb_dev->dev.bus_id,
2034 hdw->usb_dev->devnum);
2035
Mike Iselyd8554972006-06-26 20:58:46 -03002036 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2037 usb_set_interface(hdw->usb_dev,ifnum,0);
2038
2039 mutex_init(&hdw->ctl_lock_mutex);
2040 mutex_init(&hdw->big_lock_mutex);
2041
Mike Isely681c7392007-11-26 01:48:52 -03002042 queue_work(hdw->workqueue,&hdw->workinit);
Mike Iselyd8554972006-06-26 20:58:46 -03002043 return hdw;
2044 fail:
2045 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002046 del_timer_sync(&hdw->quiescent_timer);
2047 del_timer_sync(&hdw->encoder_wait_timer);
2048 if (hdw->workqueue) {
2049 flush_workqueue(hdw->workqueue);
2050 destroy_workqueue(hdw->workqueue);
2051 hdw->workqueue = NULL;
2052 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002053 usb_free_urb(hdw->ctl_read_urb);
2054 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002055 kfree(hdw->ctl_read_buffer);
2056 kfree(hdw->ctl_write_buffer);
2057 kfree(hdw->controls);
2058 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002059 kfree(hdw->std_defs);
2060 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002061 kfree(hdw);
2062 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002063 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002064}
2065
2066
2067/* Remove _all_ associations between this driver and the underlying USB
2068 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002069static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002070{
2071 if (hdw->flag_disconnected) return;
2072 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2073 if (hdw->ctl_read_urb) {
2074 usb_kill_urb(hdw->ctl_read_urb);
2075 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002076 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002077 }
2078 if (hdw->ctl_write_urb) {
2079 usb_kill_urb(hdw->ctl_write_urb);
2080 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002081 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002082 }
2083 if (hdw->ctl_read_buffer) {
2084 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002085 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002086 }
2087 if (hdw->ctl_write_buffer) {
2088 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002089 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002090 }
Mike Iselyd8554972006-06-26 20:58:46 -03002091 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002092 hdw->usb_dev = NULL;
2093 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002094 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002095}
2096
2097
2098/* Destroy hardware interaction structure */
2099void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2100{
Mike Isely401c27c2007-09-08 22:11:46 -03002101 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002102 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002103 del_timer_sync(&hdw->quiescent_timer);
2104 del_timer_sync(&hdw->encoder_wait_timer);
2105 if (hdw->workqueue) {
2106 flush_workqueue(hdw->workqueue);
2107 destroy_workqueue(hdw->workqueue);
2108 hdw->workqueue = NULL;
2109 }
Mike Iselyd8554972006-06-26 20:58:46 -03002110 if (hdw->fw_buffer) {
2111 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002112 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002113 }
2114 if (hdw->vid_stream) {
2115 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002116 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002117 }
Mike Iselyd8554972006-06-26 20:58:46 -03002118 if (hdw->decoder_ctrl) {
2119 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2120 }
2121 pvr2_i2c_core_done(hdw);
2122 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002123 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002124 if ((hdw->unit_number >= 0) &&
2125 (hdw->unit_number < PVR_NUM) &&
2126 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002127 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002128 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002129 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002130 kfree(hdw->controls);
2131 kfree(hdw->mpeg_ctrl_info);
2132 kfree(hdw->std_defs);
2133 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002134 kfree(hdw);
2135}
2136
2137
Mike Iselyd8554972006-06-26 20:58:46 -03002138int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2139{
2140 return (hdw && hdw->flag_ok);
2141}
2142
2143
2144/* Called when hardware has been unplugged */
2145void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2146{
2147 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2148 LOCK_TAKE(hdw->big_lock);
2149 LOCK_TAKE(hdw->ctl_lock);
2150 pvr2_hdw_remove_usb_stuff(hdw);
2151 LOCK_GIVE(hdw->ctl_lock);
2152 LOCK_GIVE(hdw->big_lock);
2153}
2154
2155
2156// Attempt to autoselect an appropriate value for std_enum_cur given
2157// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002158static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002159{
2160 unsigned int idx;
2161 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2162 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2163 hdw->std_enum_cur = idx;
2164 return;
2165 }
2166 }
2167 hdw->std_enum_cur = 0;
2168}
2169
2170
2171// Calculate correct set of enumerated standards based on currently known
2172// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002173static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002174{
2175 struct v4l2_standard *newstd;
2176 unsigned int std_cnt;
2177 unsigned int idx;
2178
2179 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2180
2181 if (hdw->std_defs) {
2182 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002183 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002184 }
2185 hdw->std_enum_cnt = 0;
2186 if (hdw->std_enum_names) {
2187 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002188 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002189 }
2190
2191 if (!std_cnt) {
2192 pvr2_trace(
2193 PVR2_TRACE_ERROR_LEGS,
2194 "WARNING: Failed to identify any viable standards");
2195 }
2196 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2197 hdw->std_enum_names[0] = "none";
2198 for (idx = 0; idx < std_cnt; idx++) {
2199 hdw->std_enum_names[idx+1] =
2200 newstd[idx].name;
2201 }
2202 // Set up the dynamic control for this standard
2203 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2204 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2205 hdw->std_defs = newstd;
2206 hdw->std_enum_cnt = std_cnt+1;
2207 hdw->std_enum_cur = 0;
2208 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2209}
2210
2211
2212int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2213 struct v4l2_standard *std,
2214 unsigned int idx)
2215{
2216 int ret = -EINVAL;
2217 if (!idx) return ret;
2218 LOCK_TAKE(hdw->big_lock); do {
2219 if (idx >= hdw->std_enum_cnt) break;
2220 idx--;
2221 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2222 ret = 0;
2223 } while (0); LOCK_GIVE(hdw->big_lock);
2224 return ret;
2225}
2226
2227
2228/* Get the number of defined controls */
2229unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2230{
Mike Iselyc05c0462006-06-25 20:04:25 -03002231 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002232}
2233
2234
2235/* Retrieve a control handle given its index (0..count-1) */
2236struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2237 unsigned int idx)
2238{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002239 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002240 return hdw->controls + idx;
2241}
2242
2243
2244/* Retrieve a control handle given its index (0..count-1) */
2245struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2246 unsigned int ctl_id)
2247{
2248 struct pvr2_ctrl *cptr;
2249 unsigned int idx;
2250 int i;
2251
2252 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002253 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002254 cptr = hdw->controls + idx;
2255 i = cptr->info->internal_id;
2256 if (i && (i == ctl_id)) return cptr;
2257 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002258 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002259}
2260
2261
Mike Iselya761f432006-06-25 20:04:44 -03002262/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002263struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2264{
2265 struct pvr2_ctrl *cptr;
2266 unsigned int idx;
2267 int i;
2268
2269 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002270 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002271 cptr = hdw->controls + idx;
2272 i = cptr->info->v4l_id;
2273 if (i && (i == ctl_id)) return cptr;
2274 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002275 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002276}
2277
2278
Mike Iselya761f432006-06-25 20:04:44 -03002279/* Given a V4L ID for its immediate predecessor, retrieve the control
2280 structure associated with it. */
2281struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2282 unsigned int ctl_id)
2283{
2284 struct pvr2_ctrl *cptr,*cp2;
2285 unsigned int idx;
2286 int i;
2287
2288 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002289 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002290 for (idx = 0; idx < hdw->control_cnt; idx++) {
2291 cptr = hdw->controls + idx;
2292 i = cptr->info->v4l_id;
2293 if (!i) continue;
2294 if (i <= ctl_id) continue;
2295 if (cp2 && (cp2->info->v4l_id < i)) continue;
2296 cp2 = cptr;
2297 }
2298 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002299 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002300}
2301
2302
Mike Iselyd8554972006-06-26 20:58:46 -03002303static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2304{
2305 switch (tp) {
2306 case pvr2_ctl_int: return "integer";
2307 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002308 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002309 case pvr2_ctl_bitmask: return "bitmask";
2310 }
2311 return "";
2312}
2313
2314
Mike Isely681c7392007-11-26 01:48:52 -03002315/* Figure out if we need to commit control changes. If so, mark internal
2316 state flags to indicate this fact and return true. Otherwise do nothing
2317 else and return false. */
2318static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002319{
Mike Iselyd8554972006-06-26 20:58:46 -03002320 unsigned int idx;
2321 struct pvr2_ctrl *cptr;
2322 int value;
2323 int commit_flag = 0;
2324 char buf[100];
2325 unsigned int bcnt,ccnt;
2326
Mike Iselyc05c0462006-06-25 20:04:25 -03002327 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002328 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002329 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002330 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002331 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002332
Mike Iselyfe23a282007-01-20 00:10:55 -03002333 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002334 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2335 cptr->info->name);
2336 value = 0;
2337 cptr->info->get_value(cptr,&value);
2338 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2339 buf+bcnt,
2340 sizeof(buf)-bcnt,&ccnt);
2341 bcnt += ccnt;
2342 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2343 get_ctrl_typename(cptr->info->type));
2344 pvr2_trace(PVR2_TRACE_CTL,
2345 "/*--TRACE_COMMIT--*/ %.*s",
2346 bcnt,buf);
2347 }
2348
2349 if (!commit_flag) {
2350 /* Nothing has changed */
2351 return 0;
2352 }
2353
Mike Isely681c7392007-11-26 01:48:52 -03002354 hdw->state_pipeline_config = 0;
2355 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2356 pvr2_hdw_state_sched(hdw);
2357
2358 return !0;
2359}
2360
2361
2362/* Perform all operations needed to commit all control changes. This must
2363 be performed in synchronization with the pipeline state and is thus
2364 expected to be called as part of the driver's worker thread. Return
2365 true if commit successful, otherwise return false to indicate that
2366 commit isn't possible at this time. */
2367static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2368{
2369 unsigned int idx;
2370 struct pvr2_ctrl *cptr;
2371 int disruptive_change;
2372
Mike Iselyd8554972006-06-26 20:58:46 -03002373 /* When video standard changes, reset the hres and vres values -
2374 but if the user has pending changes there, then let the changes
2375 take priority. */
2376 if (hdw->std_dirty) {
2377 /* Rewrite the vertical resolution to be appropriate to the
2378 video standard that has been selected. */
2379 int nvres;
2380 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2381 nvres = 480;
2382 } else {
2383 nvres = 576;
2384 }
2385 if (nvres != hdw->res_ver_val) {
2386 hdw->res_ver_val = nvres;
2387 hdw->res_ver_dirty = !0;
2388 }
Mike Iselyd8554972006-06-26 20:58:46 -03002389 }
2390
Mike Isely62433e32008-04-22 14:45:40 -03002391 if (hdw->input_dirty &&
2392 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
2393 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
2394 hdw->pathway_state)) {
2395 /* Change of mode being asked for... */
2396 hdw->state_pathway_ok = 0;
2397 }
2398 if (!hdw->state_pathway_ok) {
2399 /* Can't commit anything until pathway is ok. */
2400 return 0;
2401 }
Mike Isely681c7392007-11-26 01:48:52 -03002402 /* If any of the below has changed, then we can't do the update
2403 while the pipeline is running. Pipeline must be paused first
2404 and decoder -> encoder connection be made quiescent before we
2405 can proceed. */
2406 disruptive_change =
2407 (hdw->std_dirty ||
2408 hdw->enc_unsafe_stale ||
2409 hdw->srate_dirty ||
2410 hdw->res_ver_dirty ||
2411 hdw->res_hor_dirty ||
2412 hdw->input_dirty ||
2413 (hdw->active_stream_type != hdw->desired_stream_type));
2414 if (disruptive_change && !hdw->state_pipeline_idle) {
2415 /* Pipeline is not idle; we can't proceed. Arrange to
2416 cause pipeline to stop so that we can try this again
2417 later.... */
2418 hdw->state_pipeline_pause = !0;
2419 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002420 }
2421
Mike Iselyb30d2442006-06-25 20:05:01 -03002422 if (hdw->srate_dirty) {
2423 /* Write new sample rate into control structure since
2424 * the master copy is stale. We must track srate
2425 * separate from the mpeg control structure because
2426 * other logic also uses this value. */
2427 struct v4l2_ext_controls cs;
2428 struct v4l2_ext_control c1;
2429 memset(&cs,0,sizeof(cs));
2430 memset(&c1,0,sizeof(c1));
2431 cs.controls = &c1;
2432 cs.count = 1;
2433 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2434 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002435 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002436 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002437
Mike Iselyd8554972006-06-26 20:58:46 -03002438 /* Scan i2c core at this point - before we clear all the dirty
2439 bits. Various parts of the i2c core will notice dirty bits as
2440 appropriate and arrange to broadcast or directly send updates to
2441 the client drivers in order to keep everything in sync */
2442 pvr2_i2c_core_check_stale(hdw);
2443
Mike Iselyc05c0462006-06-25 20:04:25 -03002444 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002445 cptr = hdw->controls + idx;
2446 if (!cptr->info->clear_dirty) continue;
2447 cptr->info->clear_dirty(cptr);
2448 }
2449
Mike Isely681c7392007-11-26 01:48:52 -03002450 if (hdw->active_stream_type != hdw->desired_stream_type) {
2451 /* Handle any side effects of stream config here */
2452 hdw->active_stream_type = hdw->desired_stream_type;
2453 }
2454
Mike Iselyd8554972006-06-26 20:58:46 -03002455 /* Now execute i2c core update */
2456 pvr2_i2c_core_sync(hdw);
2457
Mike Isely62433e32008-04-22 14:45:40 -03002458 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
2459 hdw->state_encoder_run) {
2460 /* If encoder isn't running or it can't be touched, then
2461 this will get worked out later when we start the
2462 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03002463 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2464 }
Mike Iselyd8554972006-06-26 20:58:46 -03002465
Mike Isely681c7392007-11-26 01:48:52 -03002466 hdw->state_pipeline_config = !0;
2467 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2468 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002469}
2470
2471
2472int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2473{
Mike Isely681c7392007-11-26 01:48:52 -03002474 int fl;
2475 LOCK_TAKE(hdw->big_lock);
2476 fl = pvr2_hdw_commit_setup(hdw);
2477 LOCK_GIVE(hdw->big_lock);
2478 if (!fl) return 0;
2479 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002480}
2481
2482
Mike Isely681c7392007-11-26 01:48:52 -03002483static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002484{
Mike Isely681c7392007-11-26 01:48:52 -03002485 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002486 LOCK_TAKE(hdw->big_lock); do {
2487 pvr2_i2c_core_sync(hdw);
2488 } while (0); LOCK_GIVE(hdw->big_lock);
2489}
2490
2491
Mike Isely681c7392007-11-26 01:48:52 -03002492static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002493{
Mike Isely681c7392007-11-26 01:48:52 -03002494 int fl = 0;
2495 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002496 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002497 fl = pvr2_hdw_state_eval(hdw);
2498 } while (0); LOCK_GIVE(hdw->big_lock);
2499 if (fl && hdw->state_func) {
2500 hdw->state_func(hdw->state_data);
2501 }
2502}
2503
2504
2505static void pvr2_hdw_worker_init(struct work_struct *work)
2506{
2507 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workinit);
2508 LOCK_TAKE(hdw->big_lock); do {
2509 pvr2_hdw_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002510 } while (0); LOCK_GIVE(hdw->big_lock);
2511}
2512
2513
Mike Isely681c7392007-11-26 01:48:52 -03002514static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002515{
Mike Isely681c7392007-11-26 01:48:52 -03002516 return wait_event_interruptible(
2517 hdw->state_wait_data,
2518 (hdw->state_stale == 0) &&
2519 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002520}
2521
Mike Isely681c7392007-11-26 01:48:52 -03002522
2523void pvr2_hdw_set_state_callback(struct pvr2_hdw *hdw,
2524 void (*callback_func)(void *),
2525 void *callback_data)
2526{
2527 LOCK_TAKE(hdw->big_lock); do {
2528 hdw->state_data = callback_data;
2529 hdw->state_func = callback_func;
2530 } while (0); LOCK_GIVE(hdw->big_lock);
2531}
2532
2533
Mike Iselyd8554972006-06-26 20:58:46 -03002534/* Return name for this driver instance */
2535const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2536{
2537 return hdw->name;
2538}
2539
2540
Mike Isely78a47102007-11-26 01:58:20 -03002541const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2542{
2543 return hdw->hdw_desc->description;
2544}
2545
2546
2547const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2548{
2549 return hdw->hdw_desc->shortname;
2550}
2551
2552
Mike Iselyd8554972006-06-26 20:58:46 -03002553int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2554{
2555 int result;
2556 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002557 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002558 result = pvr2_send_request(hdw,
2559 hdw->cmd_buffer,1,
2560 hdw->cmd_buffer,1);
2561 if (result < 0) break;
2562 result = (hdw->cmd_buffer[0] != 0);
2563 } while(0); LOCK_GIVE(hdw->ctl_lock);
2564 return result;
2565}
2566
2567
Mike Isely18103c572007-01-20 00:09:47 -03002568/* Execute poll of tuner status */
2569void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002570{
Mike Iselyd8554972006-06-26 20:58:46 -03002571 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002572 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002573 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002574}
2575
2576
2577/* Return information about the tuner */
2578int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2579{
2580 LOCK_TAKE(hdw->big_lock); do {
2581 if (hdw->tuner_signal_stale) {
2582 pvr2_i2c_core_status_poll(hdw);
2583 }
2584 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2585 } while (0); LOCK_GIVE(hdw->big_lock);
2586 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002587}
2588
2589
2590/* Get handle to video output stream */
2591struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2592{
2593 return hp->vid_stream;
2594}
2595
2596
2597void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2598{
Mike Isely4f1a3e52006-06-25 20:04:31 -03002599 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002600 LOCK_TAKE(hdw->big_lock); do {
2601 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03002602 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002603 pvr2_i2c_core_check_stale(hdw);
2604 hdw->log_requested = 0;
2605 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03002606 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03002607 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03002608 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03002609 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002610 } while (0); LOCK_GIVE(hdw->big_lock);
2611}
2612
Mike Isely4db666c2007-09-08 22:16:27 -03002613
2614/* Grab EEPROM contents, needed for direct method. */
2615#define EEPROM_SIZE 8192
2616#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
2617static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
2618{
2619 struct i2c_msg msg[2];
2620 u8 *eeprom;
2621 u8 iadd[2];
2622 u8 addr;
2623 u16 eepromSize;
2624 unsigned int offs;
2625 int ret;
2626 int mode16 = 0;
2627 unsigned pcnt,tcnt;
2628 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
2629 if (!eeprom) {
2630 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2631 "Failed to allocate memory"
2632 " required to read eeprom");
2633 return NULL;
2634 }
2635
2636 trace_eeprom("Value for eeprom addr from controller was 0x%x",
2637 hdw->eeprom_addr);
2638 addr = hdw->eeprom_addr;
2639 /* Seems that if the high bit is set, then the *real* eeprom
2640 address is shifted right now bit position (noticed this in
2641 newer PVR USB2 hardware) */
2642 if (addr & 0x80) addr >>= 1;
2643
2644 /* FX2 documentation states that a 16bit-addressed eeprom is
2645 expected if the I2C address is an odd number (yeah, this is
2646 strange but it's what they do) */
2647 mode16 = (addr & 1);
2648 eepromSize = (mode16 ? EEPROM_SIZE : 256);
2649 trace_eeprom("Examining %d byte eeprom at location 0x%x"
2650 " using %d bit addressing",eepromSize,addr,
2651 mode16 ? 16 : 8);
2652
2653 msg[0].addr = addr;
2654 msg[0].flags = 0;
2655 msg[0].len = mode16 ? 2 : 1;
2656 msg[0].buf = iadd;
2657 msg[1].addr = addr;
2658 msg[1].flags = I2C_M_RD;
2659
2660 /* We have to do the actual eeprom data fetch ourselves, because
2661 (1) we're only fetching part of the eeprom, and (2) if we were
2662 getting the whole thing our I2C driver can't grab it in one
2663 pass - which is what tveeprom is otherwise going to attempt */
2664 memset(eeprom,0,EEPROM_SIZE);
2665 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
2666 pcnt = 16;
2667 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
2668 offs = tcnt + (eepromSize - EEPROM_SIZE);
2669 if (mode16) {
2670 iadd[0] = offs >> 8;
2671 iadd[1] = offs;
2672 } else {
2673 iadd[0] = offs;
2674 }
2675 msg[1].len = pcnt;
2676 msg[1].buf = eeprom+tcnt;
2677 if ((ret = i2c_transfer(&hdw->i2c_adap,
2678 msg,ARRAY_SIZE(msg))) != 2) {
2679 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2680 "eeprom fetch set offs err=%d",ret);
2681 kfree(eeprom);
2682 return NULL;
2683 }
2684 }
2685 return eeprom;
2686}
2687
2688
2689void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
2690 int prom_flag,
2691 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002692{
2693 int ret;
2694 u16 address;
2695 unsigned int pipe;
2696 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00002697 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03002698
2699 if (!enable_flag) {
2700 pvr2_trace(PVR2_TRACE_FIRMWARE,
2701 "Cleaning up after CPU firmware fetch");
2702 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002703 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002704 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03002705 if (hdw->fw_cpu_flag) {
2706 /* Now release the CPU. It will disconnect
2707 and reconnect later. */
2708 pvr2_hdw_cpureset_assert(hdw,0);
2709 }
Mike Iselyd8554972006-06-26 20:58:46 -03002710 break;
2711 }
2712
Mike Isely4db666c2007-09-08 22:16:27 -03002713 hdw->fw_cpu_flag = (prom_flag == 0);
2714 if (hdw->fw_cpu_flag) {
2715 pvr2_trace(PVR2_TRACE_FIRMWARE,
2716 "Preparing to suck out CPU firmware");
2717 hdw->fw_size = 0x2000;
2718 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
2719 if (!hdw->fw_buffer) {
2720 hdw->fw_size = 0;
2721 break;
2722 }
2723
2724 /* We have to hold the CPU during firmware upload. */
2725 pvr2_hdw_cpureset_assert(hdw,1);
2726
2727 /* download the firmware from address 0000-1fff in 2048
2728 (=0x800) bytes chunk. */
2729
2730 pvr2_trace(PVR2_TRACE_FIRMWARE,
2731 "Grabbing CPU firmware");
2732 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2733 for(address = 0; address < hdw->fw_size;
2734 address += 0x800) {
2735 ret = usb_control_msg(hdw->usb_dev,pipe,
2736 0xa0,0xc0,
2737 address,0,
2738 hdw->fw_buffer+address,
2739 0x800,HZ);
2740 if (ret < 0) break;
2741 }
2742
2743 pvr2_trace(PVR2_TRACE_FIRMWARE,
2744 "Done grabbing CPU firmware");
2745 } else {
2746 pvr2_trace(PVR2_TRACE_FIRMWARE,
2747 "Sucking down EEPROM contents");
2748 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
2749 if (!hdw->fw_buffer) {
2750 pvr2_trace(PVR2_TRACE_FIRMWARE,
2751 "EEPROM content suck failed.");
2752 break;
2753 }
2754 hdw->fw_size = EEPROM_SIZE;
2755 pvr2_trace(PVR2_TRACE_FIRMWARE,
2756 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03002757 }
2758
Mike Iselyd8554972006-06-26 20:58:46 -03002759 } while (0); LOCK_GIVE(hdw->big_lock);
2760}
2761
2762
2763/* Return true if we're in a mode for retrieval CPU firmware */
2764int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2765{
Al Viro5fa12472008-03-29 03:07:38 +00002766 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002767}
2768
2769
2770int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2771 char *buf,unsigned int cnt)
2772{
2773 int ret = -EINVAL;
2774 LOCK_TAKE(hdw->big_lock); do {
2775 if (!buf) break;
2776 if (!cnt) break;
2777
2778 if (!hdw->fw_buffer) {
2779 ret = -EIO;
2780 break;
2781 }
2782
2783 if (offs >= hdw->fw_size) {
2784 pvr2_trace(PVR2_TRACE_FIRMWARE,
2785 "Read firmware data offs=%d EOF",
2786 offs);
2787 ret = 0;
2788 break;
2789 }
2790
2791 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2792
2793 memcpy(buf,hdw->fw_buffer+offs,cnt);
2794
2795 pvr2_trace(PVR2_TRACE_FIRMWARE,
2796 "Read firmware data offs=%d cnt=%d",
2797 offs,cnt);
2798 ret = cnt;
2799 } while (0); LOCK_GIVE(hdw->big_lock);
2800
2801 return ret;
2802}
2803
2804
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002805int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002806 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03002807{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002808 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002809 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2810 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2811 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002812 default: return -1;
2813 }
Mike Iselyd8554972006-06-26 20:58:46 -03002814}
2815
2816
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03002817/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002818void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002819 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03002820{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002821 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002822 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2823 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2824 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002825 default: break;
2826 }
Mike Iselyd8554972006-06-26 20:58:46 -03002827}
2828
2829
David Howells7d12e782006-10-05 14:55:46 +01002830static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002831{
2832 struct pvr2_hdw *hdw = urb->context;
2833 hdw->ctl_write_pend_flag = 0;
2834 if (hdw->ctl_read_pend_flag) return;
2835 complete(&hdw->ctl_done);
2836}
2837
2838
David Howells7d12e782006-10-05 14:55:46 +01002839static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002840{
2841 struct pvr2_hdw *hdw = urb->context;
2842 hdw->ctl_read_pend_flag = 0;
2843 if (hdw->ctl_write_pend_flag) return;
2844 complete(&hdw->ctl_done);
2845}
2846
2847
2848static void pvr2_ctl_timeout(unsigned long data)
2849{
2850 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2851 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2852 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002853 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002854 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002855 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002856 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03002857 }
2858}
2859
2860
Mike Iselye61b6fc2006-07-18 22:42:18 -03002861/* Issue a command and get a response from the device. This extended
2862 version includes a probe flag (which if set means that device errors
2863 should not be logged or treated as fatal) and a timeout in jiffies.
2864 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002865static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2866 unsigned int timeout,int probe_fl,
2867 void *write_data,unsigned int write_len,
2868 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03002869{
2870 unsigned int idx;
2871 int status = 0;
2872 struct timer_list timer;
2873 if (!hdw->ctl_lock_held) {
2874 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2875 "Attempted to execute control transfer"
2876 " without lock!!");
2877 return -EDEADLK;
2878 }
Mike Isely681c7392007-11-26 01:48:52 -03002879 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03002880 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2881 "Attempted to execute control transfer"
2882 " when device not ok");
2883 return -EIO;
2884 }
2885 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
2886 if (!probe_fl) {
2887 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2888 "Attempted to execute control transfer"
2889 " when USB is disconnected");
2890 }
2891 return -ENOTTY;
2892 }
2893
2894 /* Ensure that we have sane parameters */
2895 if (!write_data) write_len = 0;
2896 if (!read_data) read_len = 0;
2897 if (write_len > PVR2_CTL_BUFFSIZE) {
2898 pvr2_trace(
2899 PVR2_TRACE_ERROR_LEGS,
2900 "Attempted to execute %d byte"
2901 " control-write transfer (limit=%d)",
2902 write_len,PVR2_CTL_BUFFSIZE);
2903 return -EINVAL;
2904 }
2905 if (read_len > PVR2_CTL_BUFFSIZE) {
2906 pvr2_trace(
2907 PVR2_TRACE_ERROR_LEGS,
2908 "Attempted to execute %d byte"
2909 " control-read transfer (limit=%d)",
2910 write_len,PVR2_CTL_BUFFSIZE);
2911 return -EINVAL;
2912 }
2913 if ((!write_len) && (!read_len)) {
2914 pvr2_trace(
2915 PVR2_TRACE_ERROR_LEGS,
2916 "Attempted to execute null control transfer?");
2917 return -EINVAL;
2918 }
2919
2920
2921 hdw->cmd_debug_state = 1;
2922 if (write_len) {
2923 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
2924 } else {
2925 hdw->cmd_debug_code = 0;
2926 }
2927 hdw->cmd_debug_write_len = write_len;
2928 hdw->cmd_debug_read_len = read_len;
2929
2930 /* Initialize common stuff */
2931 init_completion(&hdw->ctl_done);
2932 hdw->ctl_timeout_flag = 0;
2933 hdw->ctl_write_pend_flag = 0;
2934 hdw->ctl_read_pend_flag = 0;
2935 init_timer(&timer);
2936 timer.expires = jiffies + timeout;
2937 timer.data = (unsigned long)hdw;
2938 timer.function = pvr2_ctl_timeout;
2939
2940 if (write_len) {
2941 hdw->cmd_debug_state = 2;
2942 /* Transfer write data to internal buffer */
2943 for (idx = 0; idx < write_len; idx++) {
2944 hdw->ctl_write_buffer[idx] =
2945 ((unsigned char *)write_data)[idx];
2946 }
2947 /* Initiate a write request */
2948 usb_fill_bulk_urb(hdw->ctl_write_urb,
2949 hdw->usb_dev,
2950 usb_sndbulkpipe(hdw->usb_dev,
2951 PVR2_CTL_WRITE_ENDPOINT),
2952 hdw->ctl_write_buffer,
2953 write_len,
2954 pvr2_ctl_write_complete,
2955 hdw);
2956 hdw->ctl_write_urb->actual_length = 0;
2957 hdw->ctl_write_pend_flag = !0;
2958 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
2959 if (status < 0) {
2960 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2961 "Failed to submit write-control"
2962 " URB status=%d",status);
2963 hdw->ctl_write_pend_flag = 0;
2964 goto done;
2965 }
2966 }
2967
2968 if (read_len) {
2969 hdw->cmd_debug_state = 3;
2970 memset(hdw->ctl_read_buffer,0x43,read_len);
2971 /* Initiate a read request */
2972 usb_fill_bulk_urb(hdw->ctl_read_urb,
2973 hdw->usb_dev,
2974 usb_rcvbulkpipe(hdw->usb_dev,
2975 PVR2_CTL_READ_ENDPOINT),
2976 hdw->ctl_read_buffer,
2977 read_len,
2978 pvr2_ctl_read_complete,
2979 hdw);
2980 hdw->ctl_read_urb->actual_length = 0;
2981 hdw->ctl_read_pend_flag = !0;
2982 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
2983 if (status < 0) {
2984 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2985 "Failed to submit read-control"
2986 " URB status=%d",status);
2987 hdw->ctl_read_pend_flag = 0;
2988 goto done;
2989 }
2990 }
2991
2992 /* Start timer */
2993 add_timer(&timer);
2994
2995 /* Now wait for all I/O to complete */
2996 hdw->cmd_debug_state = 4;
2997 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2998 wait_for_completion(&hdw->ctl_done);
2999 }
3000 hdw->cmd_debug_state = 5;
3001
3002 /* Stop timer */
3003 del_timer_sync(&timer);
3004
3005 hdw->cmd_debug_state = 6;
3006 status = 0;
3007
3008 if (hdw->ctl_timeout_flag) {
3009 status = -ETIMEDOUT;
3010 if (!probe_fl) {
3011 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3012 "Timed out control-write");
3013 }
3014 goto done;
3015 }
3016
3017 if (write_len) {
3018 /* Validate results of write request */
3019 if ((hdw->ctl_write_urb->status != 0) &&
3020 (hdw->ctl_write_urb->status != -ENOENT) &&
3021 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3022 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3023 /* USB subsystem is reporting some kind of failure
3024 on the write */
3025 status = hdw->ctl_write_urb->status;
3026 if (!probe_fl) {
3027 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3028 "control-write URB failure,"
3029 " status=%d",
3030 status);
3031 }
3032 goto done;
3033 }
3034 if (hdw->ctl_write_urb->actual_length < write_len) {
3035 /* Failed to write enough data */
3036 status = -EIO;
3037 if (!probe_fl) {
3038 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3039 "control-write URB short,"
3040 " expected=%d got=%d",
3041 write_len,
3042 hdw->ctl_write_urb->actual_length);
3043 }
3044 goto done;
3045 }
3046 }
3047 if (read_len) {
3048 /* Validate results of read request */
3049 if ((hdw->ctl_read_urb->status != 0) &&
3050 (hdw->ctl_read_urb->status != -ENOENT) &&
3051 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3052 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3053 /* USB subsystem is reporting some kind of failure
3054 on the read */
3055 status = hdw->ctl_read_urb->status;
3056 if (!probe_fl) {
3057 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3058 "control-read URB failure,"
3059 " status=%d",
3060 status);
3061 }
3062 goto done;
3063 }
3064 if (hdw->ctl_read_urb->actual_length < read_len) {
3065 /* Failed to read enough data */
3066 status = -EIO;
3067 if (!probe_fl) {
3068 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3069 "control-read URB short,"
3070 " expected=%d got=%d",
3071 read_len,
3072 hdw->ctl_read_urb->actual_length);
3073 }
3074 goto done;
3075 }
3076 /* Transfer retrieved data out from internal buffer */
3077 for (idx = 0; idx < read_len; idx++) {
3078 ((unsigned char *)read_data)[idx] =
3079 hdw->ctl_read_buffer[idx];
3080 }
3081 }
3082
3083 done:
3084
3085 hdw->cmd_debug_state = 0;
3086 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003087 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003088 }
3089 return status;
3090}
3091
3092
3093int pvr2_send_request(struct pvr2_hdw *hdw,
3094 void *write_data,unsigned int write_len,
3095 void *read_data,unsigned int read_len)
3096{
3097 return pvr2_send_request_ex(hdw,HZ*4,0,
3098 write_data,write_len,
3099 read_data,read_len);
3100}
3101
3102int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3103{
3104 int ret;
3105
3106 LOCK_TAKE(hdw->ctl_lock);
3107
Michael Krufky8d364362007-01-22 02:17:55 -03003108 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003109 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3110 hdw->cmd_buffer[5] = 0;
3111 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3112 hdw->cmd_buffer[7] = reg & 0xff;
3113
3114
3115 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3116
3117 LOCK_GIVE(hdw->ctl_lock);
3118
3119 return ret;
3120}
3121
3122
Adrian Bunk07e337e2006-06-30 11:30:20 -03003123static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003124{
3125 int ret = 0;
3126
3127 LOCK_TAKE(hdw->ctl_lock);
3128
Michael Krufky8d364362007-01-22 02:17:55 -03003129 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003130 hdw->cmd_buffer[1] = 0;
3131 hdw->cmd_buffer[2] = 0;
3132 hdw->cmd_buffer[3] = 0;
3133 hdw->cmd_buffer[4] = 0;
3134 hdw->cmd_buffer[5] = 0;
3135 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3136 hdw->cmd_buffer[7] = reg & 0xff;
3137
3138 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3139 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3140
3141 LOCK_GIVE(hdw->ctl_lock);
3142
3143 return ret;
3144}
3145
3146
Mike Isely681c7392007-11-26 01:48:52 -03003147void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003148{
3149 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003150 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3151 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003152 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003153 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003154 }
Mike Isely681c7392007-11-26 01:48:52 -03003155 hdw->flag_ok = 0;
3156 trace_stbit("flag_ok",hdw->flag_ok);
3157 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003158}
3159
3160
3161void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3162{
3163 int ret;
3164 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003165 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003166 if (ret == 1) {
3167 ret = usb_reset_device(hdw->usb_dev);
3168 usb_unlock_device(hdw->usb_dev);
3169 } else {
3170 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3171 "Failed to lock USB device ret=%d",ret);
3172 }
3173 if (init_pause_msec) {
3174 pvr2_trace(PVR2_TRACE_INFO,
3175 "Waiting %u msec for hardware to settle",
3176 init_pause_msec);
3177 msleep(init_pause_msec);
3178 }
3179
3180}
3181
3182
3183void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3184{
3185 char da[1];
3186 unsigned int pipe;
3187 int ret;
3188
3189 if (!hdw->usb_dev) return;
3190
3191 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3192
3193 da[0] = val ? 0x01 : 0x00;
3194
3195 /* Write the CPUCS register on the 8051. The lsb of the register
3196 is the reset bit; a 1 asserts reset while a 0 clears it. */
3197 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3198 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3199 if (ret < 0) {
3200 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3201 "cpureset_assert(%d) error=%d",val,ret);
3202 pvr2_hdw_render_useless(hdw);
3203 }
3204}
3205
3206
3207int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3208{
3209 int status;
3210 LOCK_TAKE(hdw->ctl_lock); do {
3211 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset");
Michael Krufky8d364362007-01-22 02:17:55 -03003212 hdw->cmd_buffer[0] = FX2CMD_DEEP_RESET;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003213 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003214 } while (0); LOCK_GIVE(hdw->ctl_lock);
3215 return status;
3216}
3217
3218
Michael Krufkye1edb192008-04-22 14:45:39 -03003219static int pvr2_hdw_cmd_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Iselyd8554972006-06-26 20:58:46 -03003220{
3221 int status;
3222 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufkye1edb192008-04-22 14:45:39 -03003223 if (onoff) {
3224 pvr2_trace(PVR2_TRACE_INIT, "Requesting powerup");
3225 hdw->cmd_buffer[0] = FX2CMD_POWER_ON;
3226 } else {
3227 pvr2_trace(PVR2_TRACE_INIT, "Requesting powerdown");
3228 hdw->cmd_buffer[0] = FX2CMD_POWER_OFF;
3229 }
3230 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
Mike Iselyd8554972006-06-26 20:58:46 -03003231 } while (0); LOCK_GIVE(hdw->ctl_lock);
3232 return status;
3233}
3234
Michael Krufkye1edb192008-04-22 14:45:39 -03003235int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3236{
3237 return pvr2_hdw_cmd_power_ctrl(hdw, 1);
3238}
3239
3240int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
3241{
3242 return pvr2_hdw_cmd_power_ctrl(hdw, 0);
3243}
3244
Mike Iselyd8554972006-06-26 20:58:46 -03003245
3246int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3247{
3248 if (!hdw->decoder_ctrl) {
3249 pvr2_trace(PVR2_TRACE_INIT,
3250 "Unable to reset decoder: nothing attached");
3251 return -ENOTTY;
3252 }
3253
3254 if (!hdw->decoder_ctrl->force_reset) {
3255 pvr2_trace(PVR2_TRACE_INIT,
3256 "Unable to reset decoder: not implemented");
3257 return -ENOTTY;
3258 }
3259
3260 pvr2_trace(PVR2_TRACE_INIT,
3261 "Requesting decoder reset");
3262 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3263 return 0;
3264}
3265
3266
Mike Isely62433e32008-04-22 14:45:40 -03003267static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003268{
3269 int status;
3270
3271 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003272 pvr2_trace(PVR2_TRACE_INIT,
3273 "Issuing fe demod wake command (%s)",
3274 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003275 hdw->flag_ok = !0;
3276 hdw->cmd_buffer[0] = FX2CMD_HCW_DEMOD_RESETIN;
3277 hdw->cmd_buffer[1] = onoff;
3278 status = pvr2_send_request(hdw, hdw->cmd_buffer, 2, NULL, 0);
3279 } while (0); LOCK_GIVE(hdw->ctl_lock);
3280
3281 return status;
3282}
3283
Mike Isely84147f32008-04-22 14:45:40 -03003284
Mike Isely62433e32008-04-22 14:45:40 -03003285static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003286{
3287 int status;
3288
3289 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003290 pvr2_trace(PVR2_TRACE_INIT,
3291 "Issuing fe power command to CPLD (%s)",
3292 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003293 hdw->flag_ok = !0;
3294 hdw->cmd_buffer[0] =
3295 (onoff ? FX2CMD_ONAIR_DTV_POWER_ON :
3296 FX2CMD_ONAIR_DTV_POWER_OFF);
3297 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
3298 } while (0); LOCK_GIVE(hdw->ctl_lock);
3299
3300 return status;
3301}
3302
Mike Isely62433e32008-04-22 14:45:40 -03003303
3304static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
3305 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03003306{
3307 int status;
3308 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003309 pvr2_trace(PVR2_TRACE_INIT,
3310 "Issuing onair digital setup command (%s)",
3311 (onoff ? "on" : "off"));
Mike Isely84147f32008-04-22 14:45:40 -03003312 hdw->cmd_buffer[0] =
3313 (onoff ? FX2CMD_ONAIR_DTV_STREAMING_ON :
3314 FX2CMD_ONAIR_DTV_STREAMING_OFF);
3315 status = pvr2_send_request(hdw, hdw->cmd_buffer, 1, NULL, 0);
3316 } while (0); LOCK_GIVE(hdw->ctl_lock);
3317 return status;
3318}
3319
Mike Isely62433e32008-04-22 14:45:40 -03003320
3321static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
3322{
3323 int cmode;
3324 /* Compare digital/analog desired setting with current setting. If
3325 they don't match, fix it... */
3326 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
3327 if (cmode == hdw->pathway_state) {
3328 /* They match; nothing to do */
3329 return;
3330 }
3331
3332 switch (hdw->hdw_desc->digital_control_scheme) {
3333 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
3334 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
3335 if (cmode == PVR2_PATHWAY_ANALOG) {
3336 /* If moving to analog mode, also force the decoder
3337 to reset. If no decoder is attached, then it's
3338 ok to ignore this because if/when the decoder
3339 attaches, it will reset itself at that time. */
3340 pvr2_hdw_cmd_decoder_reset(hdw);
3341 }
3342 break;
3343 case PVR2_DIGITAL_SCHEME_ONAIR:
3344 /* Supposedly we should always have the power on whether in
3345 digital or analog mode. But for now do what appears to
3346 work... */
3347 if (digitalFl) pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,!0);
3348 pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,digitalFl);
3349 if (!digitalFl) pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,0);
3350 break;
3351 default: break;
3352 }
3353
3354 hdw->pathway_state = cmode;
3355}
3356
3357
Mike Iselye61b6fc2006-07-18 22:42:18 -03003358/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003359static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003360{
Mike Isely62433e32008-04-22 14:45:40 -03003361 int status,cc;
3362 if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
3363 hdw->hdw_desc->digital_control_scheme ==
3364 PVR2_DIGITAL_SCHEME_HAUPPAUGE) {
3365 cc = (runFl ?
3366 FX2CMD_HCW_DTV_STREAMING_ON :
3367 FX2CMD_HCW_DTV_STREAMING_OFF);
3368 } else {
3369 cc = (runFl ?
3370 FX2CMD_STREAMING_ON :
3371 FX2CMD_STREAMING_OFF);
3372 }
3373
Mike Iselyd8554972006-06-26 20:58:46 -03003374 LOCK_TAKE(hdw->ctl_lock); do {
Mike Isely62433e32008-04-22 14:45:40 -03003375 hdw->cmd_buffer[0] = cc;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003376 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003377 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03003378 return status;
3379}
3380
3381
Mike Isely62433e32008-04-22 14:45:40 -03003382/* Evaluate whether or not state_pathway_ok can change */
3383static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
3384{
3385 if (hdw->state_pathway_ok) {
3386 /* Nothing to do if pathway is already ok */
3387 return 0;
3388 }
3389 if (!hdw->state_pipeline_idle) {
3390 /* Not allowed to change anything if pipeline is not idle */
3391 return 0;
3392 }
3393 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
3394 hdw->state_pathway_ok = !0;
3395 return !0;
3396}
3397
3398
Mike Isely681c7392007-11-26 01:48:52 -03003399/* Evaluate whether or not state_encoder_ok can change */
3400static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3401{
3402 if (hdw->state_encoder_ok) return 0;
3403 if (hdw->flag_tripped) return 0;
3404 if (hdw->state_encoder_run) return 0;
3405 if (hdw->state_encoder_config) return 0;
3406 if (hdw->state_decoder_run) return 0;
3407 if (hdw->state_usbstream_run) return 0;
Mike Isely62433e32008-04-22 14:45:40 -03003408 if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003409 if (pvr2_upload_firmware2(hdw) < 0) {
3410 hdw->flag_tripped = !0;
3411 trace_stbit("flag_tripped",hdw->flag_tripped);
3412 return !0;
3413 }
3414 hdw->state_encoder_ok = !0;
3415 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3416 return !0;
3417}
3418
3419
3420/* Evaluate whether or not state_encoder_config can change */
3421static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3422{
3423 if (hdw->state_encoder_config) {
3424 if (hdw->state_encoder_ok) {
3425 if (hdw->state_pipeline_req &&
3426 !hdw->state_pipeline_pause) return 0;
3427 }
3428 hdw->state_encoder_config = 0;
3429 hdw->state_encoder_waitok = 0;
3430 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3431 /* paranoia - solve race if timer just completed */
3432 del_timer_sync(&hdw->encoder_wait_timer);
3433 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003434 if (!hdw->state_pathway_ok ||
3435 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3436 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03003437 !hdw->state_pipeline_idle ||
3438 hdw->state_pipeline_pause ||
3439 !hdw->state_pipeline_req ||
3440 !hdw->state_pipeline_config) {
3441 /* We must reset the enforced wait interval if
3442 anything has happened that might have disturbed
3443 the encoder. This should be a rare case. */
3444 if (timer_pending(&hdw->encoder_wait_timer)) {
3445 del_timer_sync(&hdw->encoder_wait_timer);
3446 }
3447 if (hdw->state_encoder_waitok) {
3448 /* Must clear the state - therefore we did
3449 something to a state bit and must also
3450 return true. */
3451 hdw->state_encoder_waitok = 0;
3452 trace_stbit("state_encoder_waitok",
3453 hdw->state_encoder_waitok);
3454 return !0;
3455 }
3456 return 0;
3457 }
3458 if (!hdw->state_encoder_waitok) {
3459 if (!timer_pending(&hdw->encoder_wait_timer)) {
3460 /* waitok flag wasn't set and timer isn't
3461 running. Check flag once more to avoid
3462 a race then start the timer. This is
3463 the point when we measure out a minimal
3464 quiet interval before doing something to
3465 the encoder. */
3466 if (!hdw->state_encoder_waitok) {
3467 hdw->encoder_wait_timer.expires =
3468 jiffies + (HZ*50/1000);
3469 add_timer(&hdw->encoder_wait_timer);
3470 }
3471 }
3472 /* We can't continue until we know we have been
3473 quiet for the interval measured by this
3474 timer. */
3475 return 0;
3476 }
3477 pvr2_encoder_configure(hdw);
3478 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3479 }
3480 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3481 return !0;
3482}
3483
3484
3485/* Evaluate whether or not state_encoder_run can change */
3486static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3487{
3488 if (hdw->state_encoder_run) {
3489 if (hdw->state_encoder_ok) {
Mike Isely62433e32008-04-22 14:45:40 -03003490 if (hdw->state_decoder_run &&
3491 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003492 if (pvr2_encoder_stop(hdw) < 0) return !0;
3493 }
3494 hdw->state_encoder_run = 0;
3495 } else {
3496 if (!hdw->state_encoder_ok) return 0;
3497 if (!hdw->state_decoder_run) return 0;
Mike Isely62433e32008-04-22 14:45:40 -03003498 if (!hdw->state_pathway_ok) return 0;
3499 if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003500 if (pvr2_encoder_start(hdw) < 0) return !0;
3501 hdw->state_encoder_run = !0;
3502 }
3503 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3504 return !0;
3505}
3506
3507
3508/* Timeout function for quiescent timer. */
3509static void pvr2_hdw_quiescent_timeout(unsigned long data)
3510{
3511 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3512 hdw->state_decoder_quiescent = !0;
3513 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3514 hdw->state_stale = !0;
3515 queue_work(hdw->workqueue,&hdw->workpoll);
3516}
3517
3518
3519/* Timeout function for encoder wait timer. */
3520static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3521{
3522 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3523 hdw->state_encoder_waitok = !0;
3524 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3525 hdw->state_stale = !0;
3526 queue_work(hdw->workqueue,&hdw->workpoll);
3527}
3528
3529
3530/* Evaluate whether or not state_decoder_run can change */
3531static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3532{
3533 if (hdw->state_decoder_run) {
3534 if (hdw->state_encoder_ok) {
3535 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03003536 !hdw->state_pipeline_pause &&
3537 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003538 }
3539 if (!hdw->flag_decoder_missed) {
3540 pvr2_decoder_enable(hdw,0);
3541 }
3542 hdw->state_decoder_quiescent = 0;
3543 hdw->state_decoder_run = 0;
3544 /* paranoia - solve race if timer just completed */
3545 del_timer_sync(&hdw->quiescent_timer);
3546 } else {
3547 if (!hdw->state_decoder_quiescent) {
3548 if (!timer_pending(&hdw->quiescent_timer)) {
3549 /* We don't do something about the
3550 quiescent timer until right here because
3551 we also want to catch cases where the
3552 decoder was already not running (like
3553 after initialization) as opposed to
3554 knowing that we had just stopped it.
3555 The second flag check is here to cover a
3556 race - the timer could have run and set
3557 this flag just after the previous check
3558 but before we did the pending check. */
3559 if (!hdw->state_decoder_quiescent) {
3560 hdw->quiescent_timer.expires =
3561 jiffies + (HZ*50/1000);
3562 add_timer(&hdw->quiescent_timer);
3563 }
3564 }
3565 /* Don't allow decoder to start again until it has
3566 been quiesced first. This little detail should
3567 hopefully further stabilize the encoder. */
3568 return 0;
3569 }
Mike Isely62433e32008-04-22 14:45:40 -03003570 if (!hdw->state_pathway_ok ||
3571 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3572 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03003573 hdw->state_pipeline_pause ||
3574 !hdw->state_pipeline_config ||
3575 !hdw->state_encoder_config ||
3576 !hdw->state_encoder_ok) return 0;
3577 del_timer_sync(&hdw->quiescent_timer);
3578 if (hdw->flag_decoder_missed) return 0;
3579 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3580 hdw->state_decoder_quiescent = 0;
3581 hdw->state_decoder_run = !0;
3582 }
3583 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3584 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3585 return !0;
3586}
3587
3588
3589/* Evaluate whether or not state_usbstream_run can change */
3590static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3591{
3592 if (hdw->state_usbstream_run) {
Mike Isely62433e32008-04-22 14:45:40 -03003593 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3594 if (hdw->state_encoder_ok &&
3595 hdw->state_encoder_run &&
3596 hdw->state_pathway_ok) return 0;
3597 } else {
3598 if (hdw->state_pipeline_req &&
3599 !hdw->state_pipeline_pause &&
3600 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03003601 }
3602 pvr2_hdw_cmd_usbstream(hdw,0);
3603 hdw->state_usbstream_run = 0;
3604 } else {
Mike Isely62433e32008-04-22 14:45:40 -03003605 if (!hdw->state_pipeline_req ||
3606 hdw->state_pipeline_pause ||
3607 !hdw->state_pathway_ok) return 0;
3608 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
3609 if (!hdw->state_encoder_ok ||
3610 !hdw->state_encoder_run) return 0;
3611 }
Mike Isely681c7392007-11-26 01:48:52 -03003612 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3613 hdw->state_usbstream_run = !0;
3614 }
3615 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3616 return !0;
3617}
3618
3619
3620/* Attempt to configure pipeline, if needed */
3621static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3622{
3623 if (hdw->state_pipeline_config ||
3624 hdw->state_pipeline_pause) return 0;
3625 pvr2_hdw_commit_execute(hdw);
3626 return !0;
3627}
3628
3629
3630/* Update pipeline idle and pipeline pause tracking states based on other
3631 inputs. This must be called whenever the other relevant inputs have
3632 changed. */
3633static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3634{
3635 unsigned int st;
3636 int updatedFl = 0;
3637 /* Update pipeline state */
3638 st = !(hdw->state_encoder_run ||
3639 hdw->state_decoder_run ||
3640 hdw->state_usbstream_run ||
3641 (!hdw->state_decoder_quiescent));
3642 if (!st != !hdw->state_pipeline_idle) {
3643 hdw->state_pipeline_idle = st;
3644 updatedFl = !0;
3645 }
3646 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3647 hdw->state_pipeline_pause = 0;
3648 updatedFl = !0;
3649 }
3650 return updatedFl;
3651}
3652
3653
3654typedef int (*state_eval_func)(struct pvr2_hdw *);
3655
3656/* Set of functions to be run to evaluate various states in the driver. */
3657const static state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03003658 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03003659 state_eval_pipeline_config,
3660 state_eval_encoder_ok,
3661 state_eval_encoder_config,
3662 state_eval_decoder_run,
3663 state_eval_encoder_run,
3664 state_eval_usbstream_run,
3665};
3666
3667
3668/* Process various states and return true if we did anything interesting. */
3669static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
3670{
3671 unsigned int i;
3672 int state_updated = 0;
3673 int check_flag;
3674
3675 if (!hdw->state_stale) return 0;
3676 if ((hdw->fw1_state != FW1_STATE_OK) ||
3677 !hdw->flag_ok) {
3678 hdw->state_stale = 0;
3679 return !0;
3680 }
3681 /* This loop is the heart of the entire driver. It keeps trying to
3682 evaluate various bits of driver state until nothing changes for
3683 one full iteration. Each "bit of state" tracks some global
3684 aspect of the driver, e.g. whether decoder should run, if
3685 pipeline is configured, usb streaming is on, etc. We separately
3686 evaluate each of those questions based on other driver state to
3687 arrive at the correct running configuration. */
3688 do {
3689 check_flag = 0;
3690 state_update_pipeline_state(hdw);
3691 /* Iterate over each bit of state */
3692 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
3693 if ((*eval_funcs[i])(hdw)) {
3694 check_flag = !0;
3695 state_updated = !0;
3696 state_update_pipeline_state(hdw);
3697 }
3698 }
3699 } while (check_flag && hdw->flag_ok);
3700 hdw->state_stale = 0;
3701 trace_stbit("state_stale",hdw->state_stale);
3702 return state_updated;
3703}
3704
3705
Mike Isely62433e32008-04-22 14:45:40 -03003706static const char *pvr2_pathway_state_name(int id)
3707{
3708 switch (id) {
3709 case PVR2_PATHWAY_ANALOG: return "analog";
3710 case PVR2_PATHWAY_DIGITAL: return "digital";
3711 default: return "unknown";
3712 }
3713}
3714
3715
Mike Isely681c7392007-11-26 01:48:52 -03003716static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
3717 char *buf,unsigned int acnt)
3718{
3719 switch (which) {
3720 case 0:
3721 return scnprintf(
3722 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03003723 "driver:%s%s%s%s%s<mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03003724 (hdw->flag_ok ? " <ok>" : " <fail>"),
3725 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
3726 (hdw->flag_disconnected ? " <disconnected>" :
3727 " <connected>"),
3728 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03003729 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
3730 pvr2_pathway_state_name(hdw->pathway_state));
3731
Mike Isely681c7392007-11-26 01:48:52 -03003732 case 1:
3733 return scnprintf(
3734 buf,acnt,
3735 "pipeline:%s%s%s%s",
3736 (hdw->state_pipeline_idle ? " <idle>" : ""),
3737 (hdw->state_pipeline_config ?
3738 " <configok>" : " <stale>"),
3739 (hdw->state_pipeline_req ? " <req>" : ""),
3740 (hdw->state_pipeline_pause ? " <pause>" : ""));
3741 case 2:
3742 return scnprintf(
3743 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03003744 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03003745 (hdw->state_decoder_run ?
3746 " <decode:run>" :
3747 (hdw->state_decoder_quiescent ?
3748 "" : " <decode:stop>")),
3749 (hdw->state_decoder_quiescent ?
3750 " <decode:quiescent>" : ""),
3751 (hdw->state_encoder_ok ?
3752 "" : " <encode:init>"),
3753 (hdw->state_encoder_run ?
3754 " <encode:run>" : " <encode:stop>"),
3755 (hdw->state_encoder_config ?
3756 " <encode:configok>" :
3757 (hdw->state_encoder_waitok ?
3758 "" : " <encode:wait>")),
3759 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03003760 " <usb:run>" : " <usb:stop>"),
3761 (hdw->state_pathway_ok ?
3762 "<pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03003763 break;
3764 case 3:
3765 return scnprintf(
3766 buf,acnt,
3767 "state: %s",
3768 pvr2_get_state_name(hdw->master_state));
3769 break;
3770 default: break;
3771 }
3772 return 0;
3773}
3774
3775
3776unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
3777 char *buf,unsigned int acnt)
3778{
3779 unsigned int bcnt,ccnt,idx;
3780 bcnt = 0;
3781 LOCK_TAKE(hdw->big_lock);
3782 for (idx = 0; ; idx++) {
3783 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
3784 if (!ccnt) break;
3785 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3786 if (!acnt) break;
3787 buf[0] = '\n'; ccnt = 1;
3788 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3789 }
3790 LOCK_GIVE(hdw->big_lock);
3791 return bcnt;
3792}
3793
3794
3795static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
3796{
3797 char buf[128];
3798 unsigned int idx,ccnt;
3799
3800 for (idx = 0; ; idx++) {
3801 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
3802 if (!ccnt) break;
3803 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
3804 }
3805}
3806
3807
3808/* Evaluate and update the driver's current state, taking various actions
3809 as appropriate for the update. */
3810static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
3811{
3812 unsigned int st;
3813 int state_updated = 0;
3814 int callback_flag = 0;
3815
3816 pvr2_trace(PVR2_TRACE_STBITS,
3817 "Drive state check START");
3818 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3819 pvr2_hdw_state_log_state(hdw);
3820 }
3821
3822 /* Process all state and get back over disposition */
3823 state_updated = pvr2_hdw_state_update(hdw);
3824
3825 /* Update master state based upon all other states. */
3826 if (!hdw->flag_ok) {
3827 st = PVR2_STATE_DEAD;
3828 } else if (hdw->fw1_state != FW1_STATE_OK) {
3829 st = PVR2_STATE_COLD;
3830 } else if (!hdw->state_encoder_ok) {
3831 st = PVR2_STATE_WARM;
3832 } else if (hdw->flag_tripped || hdw->flag_decoder_missed) {
3833 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03003834 } else if (hdw->state_usbstream_run &&
3835 ((hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
3836 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03003837 st = PVR2_STATE_RUN;
3838 } else {
3839 st = PVR2_STATE_READY;
3840 }
3841 if (hdw->master_state != st) {
3842 pvr2_trace(PVR2_TRACE_STATE,
3843 "Device state change from %s to %s",
3844 pvr2_get_state_name(hdw->master_state),
3845 pvr2_get_state_name(st));
3846 hdw->master_state = st;
3847 state_updated = !0;
3848 callback_flag = !0;
3849 }
3850 if (state_updated) {
3851 /* Trigger anyone waiting on any state changes here. */
3852 wake_up(&hdw->state_wait_data);
3853 }
3854
3855 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3856 pvr2_hdw_state_log_state(hdw);
3857 }
3858 pvr2_trace(PVR2_TRACE_STBITS,
3859 "Drive state check DONE callback=%d",callback_flag);
3860
3861 return callback_flag;
3862}
3863
3864
3865/* Cause kernel thread to check / update driver state */
3866static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
3867{
3868 if (hdw->state_stale) return;
3869 hdw->state_stale = !0;
3870 trace_stbit("state_stale",hdw->state_stale);
3871 queue_work(hdw->workqueue,&hdw->workpoll);
3872}
3873
3874
3875void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
3876 struct pvr2_hdw_debug_info *ptr)
Mike Iselyd8554972006-06-26 20:58:46 -03003877{
3878 ptr->big_lock_held = hdw->big_lock_held;
3879 ptr->ctl_lock_held = hdw->ctl_lock_held;
Mike Iselyd8554972006-06-26 20:58:46 -03003880 ptr->flag_disconnected = hdw->flag_disconnected;
3881 ptr->flag_init_ok = hdw->flag_init_ok;
Mike Isely681c7392007-11-26 01:48:52 -03003882 ptr->flag_ok = hdw->flag_ok;
3883 ptr->fw1_state = hdw->fw1_state;
3884 ptr->flag_decoder_missed = hdw->flag_decoder_missed;
3885 ptr->flag_tripped = hdw->flag_tripped;
3886 ptr->state_encoder_ok = hdw->state_encoder_ok;
3887 ptr->state_encoder_run = hdw->state_encoder_run;
3888 ptr->state_decoder_run = hdw->state_decoder_run;
3889 ptr->state_usbstream_run = hdw->state_usbstream_run;
3890 ptr->state_decoder_quiescent = hdw->state_decoder_quiescent;
3891 ptr->state_pipeline_config = hdw->state_pipeline_config;
3892 ptr->state_pipeline_req = hdw->state_pipeline_req;
3893 ptr->state_pipeline_pause = hdw->state_pipeline_pause;
3894 ptr->state_pipeline_idle = hdw->state_pipeline_idle;
Mike Iselyd8554972006-06-26 20:58:46 -03003895 ptr->cmd_debug_state = hdw->cmd_debug_state;
3896 ptr->cmd_code = hdw->cmd_debug_code;
3897 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len;
3898 ptr->cmd_debug_read_len = hdw->cmd_debug_read_len;
3899 ptr->cmd_debug_timeout = hdw->ctl_timeout_flag;
3900 ptr->cmd_debug_write_pend = hdw->ctl_write_pend_flag;
3901 ptr->cmd_debug_read_pend = hdw->ctl_read_pend_flag;
3902 ptr->cmd_debug_rstatus = hdw->ctl_read_urb->status;
3903 ptr->cmd_debug_wstatus = hdw->ctl_read_urb->status;
3904}
3905
3906
Mike Isely681c7392007-11-26 01:48:52 -03003907void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
3908 struct pvr2_hdw_debug_info *ptr)
3909{
3910 LOCK_TAKE(hdw->ctl_lock); do {
3911 pvr2_hdw_get_debug_info_unlocked(hdw,ptr);
3912 } while(0); LOCK_GIVE(hdw->ctl_lock);
3913}
3914
3915
Mike Iselyd8554972006-06-26 20:58:46 -03003916int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
3917{
3918 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
3919}
3920
3921
3922int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
3923{
3924 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
3925}
3926
3927
3928int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
3929{
3930 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
3931}
3932
3933
3934int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
3935{
3936 u32 cval,nval;
3937 int ret;
3938 if (~msk) {
3939 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
3940 if (ret) return ret;
3941 nval = (cval & ~msk) | (val & msk);
3942 pvr2_trace(PVR2_TRACE_GPIO,
3943 "GPIO direction changing 0x%x:0x%x"
3944 " from 0x%x to 0x%x",
3945 msk,val,cval,nval);
3946 } else {
3947 nval = val;
3948 pvr2_trace(PVR2_TRACE_GPIO,
3949 "GPIO direction changing to 0x%x",nval);
3950 }
3951 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
3952}
3953
3954
3955int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
3956{
3957 u32 cval,nval;
3958 int ret;
3959 if (~msk) {
3960 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
3961 if (ret) return ret;
3962 nval = (cval & ~msk) | (val & msk);
3963 pvr2_trace(PVR2_TRACE_GPIO,
3964 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
3965 msk,val,cval,nval);
3966 } else {
3967 nval = val;
3968 pvr2_trace(PVR2_TRACE_GPIO,
3969 "GPIO output changing to 0x%x",nval);
3970 }
3971 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
3972}
3973
3974
Mike Isely7fb20fa2008-04-22 14:45:37 -03003975unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
3976{
3977 return hdw->input_avail_mask;
3978}
3979
3980
Mike Iselye61b6fc2006-07-18 22:42:18 -03003981/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003982static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003983{
3984 int result;
3985 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003986 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03003987 result = pvr2_send_request(hdw,
3988 hdw->cmd_buffer,1,
3989 hdw->cmd_buffer,1);
3990 if (result < 0) break;
3991 result = hdw->cmd_buffer[0];
3992 } while(0); LOCK_GIVE(hdw->ctl_lock);
3993 return result;
3994}
3995
3996
Mike Isely32ffa9a2006-09-23 22:26:52 -03003997int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03003998 u32 match_type, u32 match_chip, u64 reg_id,
3999 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03004000{
4001#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03004002 struct pvr2_i2c_client *cp;
4003 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03004004 int stat = 0;
4005 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004006
Mike Isely201f5c92007-01-28 16:08:36 -03004007 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
4008
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004009 req.match_type = match_type;
4010 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004011 req.reg = reg_id;
4012 if (setFl) req.val = *val_ptr;
4013 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03004014 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03004015 if (!v4l2_chip_match_i2c_client(
4016 cp->client,
4017 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03004018 continue;
4019 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004020 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03004021 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
4022 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03004023 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03004024 okFl = !0;
4025 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03004026 }
4027 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03004028 if (okFl) {
4029 return stat;
4030 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03004031 return -EINVAL;
4032#else
4033 return -ENOSYS;
4034#endif
4035}
4036
4037
Mike Iselyd8554972006-06-26 20:58:46 -03004038/*
4039 Stuff for Emacs to see, in order to encourage consistent editing style:
4040 *** Local Variables: ***
4041 *** mode: c ***
4042 *** fill-column: 75 ***
4043 *** tab-width: 8 ***
4044 *** c-basic-offset: 8 ***
4045 *** End: ***
4046 */