blob: 66945f7d22184b7859d6eb005570229876623203 [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{
373 struct pvr2_hdw *hdw = cptr->hdw;
374 const struct pvr2_device_desc *dsc = hdw->hdw_desc;
375
376 switch (v) {
377 case PVR2_CVAL_INPUT_TV:
378 return dsc->flag_has_analogtuner != 0;
379 case PVR2_CVAL_INPUT_DTV:
380 return dsc->flag_has_digitaltuner != 0;
381 case PVR2_CVAL_INPUT_SVIDEO:
382 return dsc->flag_has_svideo != 0;
383 case PVR2_CVAL_INPUT_COMPOSITE:
384 return dsc->flag_has_composite != 0;
385 case PVR2_CVAL_INPUT_RADIO:
386 return dsc->flag_has_fmradio != 0;
387 default:
388 return 0;
389 }
390}
391
Mike Isely1bde0282006-12-27 23:30:13 -0300392static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
393{
394 struct pvr2_hdw *hdw = cptr->hdw;
395
396 if (hdw->input_val != v) {
397 hdw->input_val = v;
398 hdw->input_dirty = !0;
399 }
400
401 /* Handle side effects - if we switch to a mode that needs the RF
402 tuner, then select the right frequency choice as well and mark
403 it dirty. */
404 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
405 hdw->freqSelector = 0;
406 hdw->freqDirty = !0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300407 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
408 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
Mike Isely1bde0282006-12-27 23:30:13 -0300409 hdw->freqSelector = 1;
410 hdw->freqDirty = !0;
411 }
412 return 0;
413}
414
415static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
416{
417 return cptr->hdw->input_dirty != 0;
418}
419
420static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
421{
422 cptr->hdw->input_dirty = 0;
423}
424
Mike Isely5549f542006-12-27 23:28:54 -0300425
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300426static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
427{
Mike Isely644afdb2007-01-20 00:19:23 -0300428 unsigned long fv;
429 struct pvr2_hdw *hdw = cptr->hdw;
430 if (hdw->tuner_signal_stale) {
431 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300432 }
Mike Isely644afdb2007-01-20 00:19:23 -0300433 fv = hdw->tuner_signal_info.rangehigh;
434 if (!fv) {
435 /* Safety fallback */
436 *vp = TV_MAX_FREQ;
437 return 0;
438 }
439 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
440 fv = (fv * 125) / 2;
441 } else {
442 fv = fv * 62500;
443 }
444 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300445 return 0;
446}
447
448static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
449{
Mike Isely644afdb2007-01-20 00:19:23 -0300450 unsigned long fv;
451 struct pvr2_hdw *hdw = cptr->hdw;
452 if (hdw->tuner_signal_stale) {
453 pvr2_i2c_core_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300454 }
Mike Isely644afdb2007-01-20 00:19:23 -0300455 fv = hdw->tuner_signal_info.rangelow;
456 if (!fv) {
457 /* Safety fallback */
458 *vp = TV_MIN_FREQ;
459 return 0;
460 }
461 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
462 fv = (fv * 125) / 2;
463 } else {
464 fv = fv * 62500;
465 }
466 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300467 return 0;
468}
469
Mike Iselyb30d2442006-06-25 20:05:01 -0300470static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
471{
472 return cptr->hdw->enc_stale != 0;
473}
474
475static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
476{
477 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300478 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300479}
480
481static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
482{
483 int ret;
484 struct v4l2_ext_controls cs;
485 struct v4l2_ext_control c1;
486 memset(&cs,0,sizeof(cs));
487 memset(&c1,0,sizeof(c1));
488 cs.controls = &c1;
489 cs.count = 1;
490 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300491 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300492 VIDIOC_G_EXT_CTRLS);
493 if (ret) return ret;
494 *vp = c1.value;
495 return 0;
496}
497
498static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
499{
500 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300501 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300502 struct v4l2_ext_controls cs;
503 struct v4l2_ext_control c1;
504 memset(&cs,0,sizeof(cs));
505 memset(&c1,0,sizeof(c1));
506 cs.controls = &c1;
507 cs.count = 1;
508 c1.id = cptr->info->v4l_id;
509 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300510 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
511 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300512 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300513 if (ret == -EBUSY) {
514 /* Oops. cx2341x is telling us it's not safe to change
515 this control while we're capturing. Make a note of this
516 fact so that the pipeline will be stopped the next time
517 controls are committed. Then go on ahead and store this
518 change anyway. */
519 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
520 0, &cs,
521 VIDIOC_S_EXT_CTRLS);
522 if (!ret) hdw->enc_unsafe_stale = !0;
523 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300524 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300525 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300526 return 0;
527}
528
529static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
530{
531 struct v4l2_queryctrl qctrl;
532 struct pvr2_ctl_info *info;
533 qctrl.id = cptr->info->v4l_id;
534 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
535 /* Strip out the const so we can adjust a function pointer. It's
536 OK to do this here because we know this is a dynamically created
537 control, so the underlying storage for the info pointer is (a)
538 private to us, and (b) not in read-only storage. Either we do
539 this or we significantly complicate the underlying control
540 implementation. */
541 info = (struct pvr2_ctl_info *)(cptr->info);
542 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
543 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300544 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300545 }
546 } else {
547 if (!(info->set_value)) {
548 info->set_value = ctrl_cx2341x_set;
549 }
550 }
551 return qctrl.flags;
552}
553
Mike Iselyd8554972006-06-26 20:58:46 -0300554static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
555{
Mike Isely681c7392007-11-26 01:48:52 -0300556 *vp = cptr->hdw->state_pipeline_req;
557 return 0;
558}
559
560static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
561{
562 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300563 return 0;
564}
565
566static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
567{
568 int result = pvr2_hdw_is_hsm(cptr->hdw);
569 *vp = PVR2_CVAL_HSM_FULL;
570 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
571 if (result) *vp = PVR2_CVAL_HSM_HIGH;
572 return 0;
573}
574
575static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
576{
577 *vp = cptr->hdw->std_mask_avail;
578 return 0;
579}
580
581static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
582{
583 struct pvr2_hdw *hdw = cptr->hdw;
584 v4l2_std_id ns;
585 ns = hdw->std_mask_avail;
586 ns = (ns & ~m) | (v & m);
587 if (ns == hdw->std_mask_avail) return 0;
588 hdw->std_mask_avail = ns;
589 pvr2_hdw_internal_set_std_avail(hdw);
590 pvr2_hdw_internal_find_stdenum(hdw);
591 return 0;
592}
593
594static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
595 char *bufPtr,unsigned int bufSize,
596 unsigned int *len)
597{
598 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
599 return 0;
600}
601
602static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
603 const char *bufPtr,unsigned int bufSize,
604 int *mskp,int *valp)
605{
606 int ret;
607 v4l2_std_id id;
608 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
609 if (ret < 0) return ret;
610 if (mskp) *mskp = id;
611 if (valp) *valp = id;
612 return 0;
613}
614
615static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
616{
617 *vp = cptr->hdw->std_mask_cur;
618 return 0;
619}
620
621static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
622{
623 struct pvr2_hdw *hdw = cptr->hdw;
624 v4l2_std_id ns;
625 ns = hdw->std_mask_cur;
626 ns = (ns & ~m) | (v & m);
627 if (ns == hdw->std_mask_cur) return 0;
628 hdw->std_mask_cur = ns;
629 hdw->std_dirty = !0;
630 pvr2_hdw_internal_find_stdenum(hdw);
631 return 0;
632}
633
634static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
635{
636 return cptr->hdw->std_dirty != 0;
637}
638
639static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
640{
641 cptr->hdw->std_dirty = 0;
642}
643
644static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
645{
Mike Isely18103c572007-01-20 00:09:47 -0300646 struct pvr2_hdw *hdw = cptr->hdw;
647 pvr2_i2c_core_status_poll(hdw);
648 *vp = hdw->tuner_signal_info.signal;
649 return 0;
650}
651
652static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
653{
654 int val = 0;
655 unsigned int subchan;
656 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely644afdb2007-01-20 00:19:23 -0300657 pvr2_i2c_core_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300658 subchan = hdw->tuner_signal_info.rxsubchans;
659 if (subchan & V4L2_TUNER_SUB_MONO) {
660 val |= (1 << V4L2_TUNER_MODE_MONO);
661 }
662 if (subchan & V4L2_TUNER_SUB_STEREO) {
663 val |= (1 << V4L2_TUNER_MODE_STEREO);
664 }
665 if (subchan & V4L2_TUNER_SUB_LANG1) {
666 val |= (1 << V4L2_TUNER_MODE_LANG1);
667 }
668 if (subchan & V4L2_TUNER_SUB_LANG2) {
669 val |= (1 << V4L2_TUNER_MODE_LANG2);
670 }
671 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300672 return 0;
673}
674
Mike Iselyd8554972006-06-26 20:58:46 -0300675
676static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
677{
678 struct pvr2_hdw *hdw = cptr->hdw;
679 if (v < 0) return -EINVAL;
680 if (v > hdw->std_enum_cnt) return -EINVAL;
681 hdw->std_enum_cur = v;
682 if (!v) return 0;
683 v--;
684 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
685 hdw->std_mask_cur = hdw->std_defs[v].id;
686 hdw->std_dirty = !0;
687 return 0;
688}
689
690
691static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
692{
693 *vp = cptr->hdw->std_enum_cur;
694 return 0;
695}
696
697
698static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
699{
700 return cptr->hdw->std_dirty != 0;
701}
702
703
704static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
705{
706 cptr->hdw->std_dirty = 0;
707}
708
709
710#define DEFINT(vmin,vmax) \
711 .type = pvr2_ctl_int, \
712 .def.type_int.min_value = vmin, \
713 .def.type_int.max_value = vmax
714
715#define DEFENUM(tab) \
716 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300717 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300718 .def.type_enum.value_names = tab
719
Mike Isely33213962006-06-25 20:04:40 -0300720#define DEFBOOL \
721 .type = pvr2_ctl_bool
722
Mike Iselyd8554972006-06-26 20:58:46 -0300723#define DEFMASK(msk,tab) \
724 .type = pvr2_ctl_bitmask, \
725 .def.type_bitmask.valid_bits = msk, \
726 .def.type_bitmask.bit_names = tab
727
728#define DEFREF(vname) \
729 .set_value = ctrl_set_##vname, \
730 .get_value = ctrl_get_##vname, \
731 .is_dirty = ctrl_isdirty_##vname, \
732 .clear_dirty = ctrl_cleardirty_##vname
733
734
735#define VCREATE_FUNCS(vname) \
736static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
737{*vp = cptr->hdw->vname##_val; return 0;} \
738static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
739{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
740static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
741{return cptr->hdw->vname##_dirty != 0;} \
742static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
743{cptr->hdw->vname##_dirty = 0;}
744
745VCREATE_FUNCS(brightness)
746VCREATE_FUNCS(contrast)
747VCREATE_FUNCS(saturation)
748VCREATE_FUNCS(hue)
749VCREATE_FUNCS(volume)
750VCREATE_FUNCS(balance)
751VCREATE_FUNCS(bass)
752VCREATE_FUNCS(treble)
753VCREATE_FUNCS(mute)
Mike Iselyc05c0462006-06-25 20:04:25 -0300754VCREATE_FUNCS(audiomode)
755VCREATE_FUNCS(res_hor)
756VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -0300757VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -0300758
Mike Iselyd8554972006-06-26 20:58:46 -0300759/* Table definition of all controls which can be manipulated */
760static const struct pvr2_ctl_info control_defs[] = {
761 {
762 .v4l_id = V4L2_CID_BRIGHTNESS,
763 .desc = "Brightness",
764 .name = "brightness",
765 .default_value = 128,
766 DEFREF(brightness),
767 DEFINT(0,255),
768 },{
769 .v4l_id = V4L2_CID_CONTRAST,
770 .desc = "Contrast",
771 .name = "contrast",
772 .default_value = 68,
773 DEFREF(contrast),
774 DEFINT(0,127),
775 },{
776 .v4l_id = V4L2_CID_SATURATION,
777 .desc = "Saturation",
778 .name = "saturation",
779 .default_value = 64,
780 DEFREF(saturation),
781 DEFINT(0,127),
782 },{
783 .v4l_id = V4L2_CID_HUE,
784 .desc = "Hue",
785 .name = "hue",
786 .default_value = 0,
787 DEFREF(hue),
788 DEFINT(-128,127),
789 },{
790 .v4l_id = V4L2_CID_AUDIO_VOLUME,
791 .desc = "Volume",
792 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -0300793 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -0300794 DEFREF(volume),
795 DEFINT(0,65535),
796 },{
797 .v4l_id = V4L2_CID_AUDIO_BALANCE,
798 .desc = "Balance",
799 .name = "balance",
800 .default_value = 0,
801 DEFREF(balance),
802 DEFINT(-32768,32767),
803 },{
804 .v4l_id = V4L2_CID_AUDIO_BASS,
805 .desc = "Bass",
806 .name = "bass",
807 .default_value = 0,
808 DEFREF(bass),
809 DEFINT(-32768,32767),
810 },{
811 .v4l_id = V4L2_CID_AUDIO_TREBLE,
812 .desc = "Treble",
813 .name = "treble",
814 .default_value = 0,
815 DEFREF(treble),
816 DEFINT(-32768,32767),
817 },{
818 .v4l_id = V4L2_CID_AUDIO_MUTE,
819 .desc = "Mute",
820 .name = "mute",
821 .default_value = 0,
822 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -0300823 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300824 },{
Mike Iselyc05c0462006-06-25 20:04:25 -0300825 .desc = "Video Source",
826 .name = "input",
827 .internal_id = PVR2_CID_INPUT,
828 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -0300829 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -0300830 DEFREF(input),
831 DEFENUM(control_values_input),
832 },{
833 .desc = "Audio Mode",
834 .name = "audio_mode",
835 .internal_id = PVR2_CID_AUDIOMODE,
836 .default_value = V4L2_TUNER_MODE_STEREO,
837 DEFREF(audiomode),
838 DEFENUM(control_values_audiomode),
839 },{
840 .desc = "Horizontal capture resolution",
841 .name = "resolution_hor",
842 .internal_id = PVR2_CID_HRES,
843 .default_value = 720,
844 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300845 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -0300846 },{
847 .desc = "Vertical capture resolution",
848 .name = "resolution_ver",
849 .internal_id = PVR2_CID_VRES,
850 .default_value = 480,
851 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -0300852 DEFINT(17,576),
853 /* Hook in check for video standard and adjust maximum
854 depending on the standard. */
855 .get_max_value = ctrl_vres_max_get,
856 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -0300857 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300858 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -0300859 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
860 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -0300861 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -0300862 DEFREF(srate),
863 DEFENUM(control_values_srate),
864 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300865 .desc = "Tuner Frequency (Hz)",
866 .name = "frequency",
867 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -0300868 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -0300869 .set_value = ctrl_freq_set,
870 .get_value = ctrl_freq_get,
871 .is_dirty = ctrl_freq_is_dirty,
872 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -0300873 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300874 /* Hook in check for input value (tv/radio) and adjust
875 max/min values accordingly */
876 .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",
880 .name = "channel",
881 .set_value = ctrl_channel_set,
882 .get_value = ctrl_channel_get,
883 DEFINT(0,FREQTABLE_SIZE),
884 },{
885 .desc = "Channel Program Frequency",
886 .name = "freq_table_value",
887 .set_value = ctrl_channelfreq_set,
888 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -0300889 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -0300890 /* Hook in check for input value (tv/radio) and adjust
891 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -0300892 .get_max_value = ctrl_freq_max_get,
893 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -0300894 },{
895 .desc = "Channel Program ID",
896 .name = "freq_table_channel",
897 .set_value = ctrl_channelprog_set,
898 .get_value = ctrl_channelprog_get,
899 DEFINT(0,FREQTABLE_SIZE),
900 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300901 .desc = "Streaming Enabled",
902 .name = "streaming_enabled",
903 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -0300904 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -0300905 },{
906 .desc = "USB Speed",
907 .name = "usb_speed",
908 .get_value = ctrl_hsm_get,
909 DEFENUM(control_values_hsm),
910 },{
Mike Isely681c7392007-11-26 01:48:52 -0300911 .desc = "Master State",
912 .name = "master_state",
913 .get_value = ctrl_masterstate_get,
914 DEFENUM(pvr2_state_names),
915 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300916 .desc = "Signal Present",
917 .name = "signal_present",
918 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -0300919 DEFINT(0,65535),
920 },{
921 .desc = "Audio Modes Present",
922 .name = "audio_modes_present",
923 .get_value = ctrl_audio_modes_present_get,
924 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
925 v4l. Nothing outside of this module cares about this,
926 but I reuse it in order to also reuse the
927 control_values_audiomode string table. */
928 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
929 (1 << V4L2_TUNER_MODE_STEREO)|
930 (1 << V4L2_TUNER_MODE_LANG1)|
931 (1 << V4L2_TUNER_MODE_LANG2)),
932 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -0300933 },{
934 .desc = "Video Standards Available Mask",
935 .name = "video_standard_mask_available",
936 .internal_id = PVR2_CID_STDAVAIL,
937 .skip_init = !0,
938 .get_value = ctrl_stdavail_get,
939 .set_value = ctrl_stdavail_set,
940 .val_to_sym = ctrl_std_val_to_sym,
941 .sym_to_val = ctrl_std_sym_to_val,
942 .type = pvr2_ctl_bitmask,
943 },{
944 .desc = "Video Standards In Use Mask",
945 .name = "video_standard_mask_active",
946 .internal_id = PVR2_CID_STDCUR,
947 .skip_init = !0,
948 .get_value = ctrl_stdcur_get,
949 .set_value = ctrl_stdcur_set,
950 .is_dirty = ctrl_stdcur_is_dirty,
951 .clear_dirty = ctrl_stdcur_clear_dirty,
952 .val_to_sym = ctrl_std_val_to_sym,
953 .sym_to_val = ctrl_std_sym_to_val,
954 .type = pvr2_ctl_bitmask,
955 },{
Mike Iselyd8554972006-06-26 20:58:46 -0300956 .desc = "Video Standard Name",
957 .name = "video_standard",
958 .internal_id = PVR2_CID_STDENUM,
959 .skip_init = !0,
960 .get_value = ctrl_stdenumcur_get,
961 .set_value = ctrl_stdenumcur_set,
962 .is_dirty = ctrl_stdenumcur_is_dirty,
963 .clear_dirty = ctrl_stdenumcur_clear_dirty,
964 .type = pvr2_ctl_enum,
965 }
966};
967
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300968#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -0300969
970
971const char *pvr2_config_get_name(enum pvr2_config cfg)
972{
973 switch (cfg) {
974 case pvr2_config_empty: return "empty";
975 case pvr2_config_mpeg: return "mpeg";
976 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -0300977 case pvr2_config_pcm: return "pcm";
978 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -0300979 }
980 return "<unknown>";
981}
982
983
984struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
985{
986 return hdw->usb_dev;
987}
988
989
990unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
991{
992 return hdw->serial_number;
993}
994
Mike Isely31a18542007-04-08 01:11:47 -0300995
996const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
997{
998 return hdw->bus_info;
999}
1000
1001
Mike Isely1bde0282006-12-27 23:30:13 -03001002unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1003{
1004 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1005}
1006
1007/* Set the currently tuned frequency and account for all possible
1008 driver-core side effects of this action. */
1009void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
1010{
Mike Isely7c74e572007-01-20 00:15:41 -03001011 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001012 if (hdw->freqSelector) {
1013 /* Swing over to radio frequency selection */
1014 hdw->freqSelector = 0;
1015 hdw->freqDirty = !0;
1016 }
Mike Isely1bde0282006-12-27 23:30:13 -03001017 if (hdw->freqValRadio != val) {
1018 hdw->freqValRadio = val;
1019 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001020 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001021 }
Mike Isely7c74e572007-01-20 00:15:41 -03001022 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001023 if (!(hdw->freqSelector)) {
1024 /* Swing over to television frequency selection */
1025 hdw->freqSelector = 1;
1026 hdw->freqDirty = !0;
1027 }
Mike Isely1bde0282006-12-27 23:30:13 -03001028 if (hdw->freqValTelevision != val) {
1029 hdw->freqValTelevision = val;
1030 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001031 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001032 }
Mike Isely1bde0282006-12-27 23:30:13 -03001033 }
1034}
1035
Mike Iselyd8554972006-06-26 20:58:46 -03001036int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1037{
1038 return hdw->unit_number;
1039}
1040
1041
1042/* Attempt to locate one of the given set of files. Messages are logged
1043 appropriate to what has been found. The return value will be 0 or
1044 greater on success (it will be the index of the file name found) and
1045 fw_entry will be filled in. Otherwise a negative error is returned on
1046 failure. If the return value is -ENOENT then no viable firmware file
1047 could be located. */
1048static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1049 const struct firmware **fw_entry,
1050 const char *fwtypename,
1051 unsigned int fwcount,
1052 const char *fwnames[])
1053{
1054 unsigned int idx;
1055 int ret = -EINVAL;
1056 for (idx = 0; idx < fwcount; idx++) {
1057 ret = request_firmware(fw_entry,
1058 fwnames[idx],
1059 &hdw->usb_dev->dev);
1060 if (!ret) {
1061 trace_firmware("Located %s firmware: %s;"
1062 " uploading...",
1063 fwtypename,
1064 fwnames[idx]);
1065 return idx;
1066 }
1067 if (ret == -ENOENT) continue;
1068 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1069 "request_firmware fatal error with code=%d",ret);
1070 return ret;
1071 }
1072 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1073 "***WARNING***"
1074 " Device %s firmware"
1075 " seems to be missing.",
1076 fwtypename);
1077 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1078 "Did you install the pvrusb2 firmware files"
1079 " in their proper location?");
1080 if (fwcount == 1) {
1081 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1082 "request_firmware unable to locate %s file %s",
1083 fwtypename,fwnames[0]);
1084 } else {
1085 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1086 "request_firmware unable to locate"
1087 " one of the following %s files:",
1088 fwtypename);
1089 for (idx = 0; idx < fwcount; idx++) {
1090 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1091 "request_firmware: Failed to find %s",
1092 fwnames[idx]);
1093 }
1094 }
1095 return ret;
1096}
1097
1098
1099/*
1100 * pvr2_upload_firmware1().
1101 *
1102 * Send the 8051 firmware to the device. After the upload, arrange for
1103 * device to re-enumerate.
1104 *
1105 * NOTE : the pointer to the firmware data given by request_firmware()
1106 * is not suitable for an usb transaction.
1107 *
1108 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001109static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001110{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001111 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001112 void *fw_ptr;
1113 unsigned int pipe;
1114 int ret;
1115 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001116
Mike Isely989eb152007-11-26 01:53:12 -03001117 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001118 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001119 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1120 "Connected device type defines"
1121 " no firmware to upload; ignoring firmware");
1122 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001123 }
1124
Mike Iselyd8554972006-06-26 20:58:46 -03001125 hdw->fw1_state = FW1_STATE_FAILED; // default result
1126
1127 trace_firmware("pvr2_upload_firmware1");
1128
1129 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001130 hdw->hdw_desc->fx2_firmware.cnt,
1131 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001132 if (ret < 0) {
1133 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1134 return ret;
1135 }
1136
1137 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1138 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1139
1140 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1141
1142 if (fw_entry->size != 0x2000){
1143 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1144 release_firmware(fw_entry);
1145 return -ENOMEM;
1146 }
1147
1148 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1149 if (fw_ptr == NULL){
1150 release_firmware(fw_entry);
1151 return -ENOMEM;
1152 }
1153
1154 /* We have to hold the CPU during firmware upload. */
1155 pvr2_hdw_cpureset_assert(hdw,1);
1156
1157 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1158 chunk. */
1159
1160 ret = 0;
1161 for(address = 0; address < fw_entry->size; address += 0x800) {
1162 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1163 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1164 0, fw_ptr, 0x800, HZ);
1165 }
1166
1167 trace_firmware("Upload done, releasing device's CPU");
1168
1169 /* Now release the CPU. It will disconnect and reconnect later. */
1170 pvr2_hdw_cpureset_assert(hdw,0);
1171
1172 kfree(fw_ptr);
1173 release_firmware(fw_entry);
1174
1175 trace_firmware("Upload done (%d bytes sent)",ret);
1176
1177 /* We should have written 8192 bytes */
1178 if (ret == 8192) {
1179 hdw->fw1_state = FW1_STATE_RELOAD;
1180 return 0;
1181 }
1182
1183 return -EIO;
1184}
1185
1186
1187/*
1188 * pvr2_upload_firmware2()
1189 *
1190 * This uploads encoder firmware on endpoint 2.
1191 *
1192 */
1193
1194int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1195{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001196 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001197 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001198 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001199 int actual_length;
1200 int ret = 0;
1201 int fwidx;
1202 static const char *fw_files[] = {
1203 CX2341X_FIRM_ENC_FILENAME,
1204 };
1205
Mike Isely989eb152007-11-26 01:53:12 -03001206 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001207 return 0;
1208 }
1209
Mike Iselyd8554972006-06-26 20:58:46 -03001210 trace_firmware("pvr2_upload_firmware2");
1211
1212 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001213 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001214 if (ret < 0) return ret;
1215 fwidx = ret;
1216 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001217 /* Since we're about to completely reinitialize the encoder,
1218 invalidate our cached copy of its configuration state. Next
1219 time we configure the encoder, then we'll fully configure it. */
1220 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001221
1222 /* First prepare firmware loading */
1223 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1224 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1225 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1226 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1227 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1228 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1229 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1230 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1231 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1232 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1233 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1234 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1235 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1236 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1237 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1238 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely567d7112007-01-28 15:38:55 -03001239 LOCK_TAKE(hdw->ctl_lock); do {
1240 hdw->cmd_buffer[0] = FX2CMD_FWPOST1;
Al Viro89952d12007-03-14 09:17:59 +00001241 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001242 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1243 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001244 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001245 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001246
1247 if (ret) {
1248 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1249 "firmware2 upload prep failed, ret=%d",ret);
1250 release_firmware(fw_entry);
1251 return ret;
1252 }
1253
1254 /* Now send firmware */
1255
1256 fw_len = fw_entry->size;
1257
Mike Isely90060d32007-02-08 02:02:53 -03001258 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001259 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1260 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001261 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001262 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001263 release_firmware(fw_entry);
1264 return -1;
1265 }
1266
1267 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1268 if (fw_ptr == NULL){
1269 release_firmware(fw_entry);
1270 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1271 "failed to allocate memory for firmware2 upload");
1272 return -ENOMEM;
1273 }
1274
1275 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1276
Mike Isely90060d32007-02-08 02:02:53 -03001277 fw_done = 0;
1278 for (fw_done = 0; fw_done < fw_len;) {
1279 bcnt = fw_len - fw_done;
1280 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1281 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1282 /* Usbsnoop log shows that we must swap bytes... */
1283 for (icnt = 0; icnt < bcnt/4 ; icnt++)
1284 ((u32 *)fw_ptr)[icnt] =
1285 ___swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001286
Mike Isely90060d32007-02-08 02:02:53 -03001287 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001288 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001289 ret |= (actual_length != bcnt);
1290 if (ret) break;
1291 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001292 }
1293
1294 trace_firmware("upload of %s : %i / %i ",
1295 fw_files[fwidx],fw_done,fw_len);
1296
1297 kfree(fw_ptr);
1298 release_firmware(fw_entry);
1299
1300 if (ret) {
1301 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1302 "firmware2 upload transfer failure");
1303 return ret;
1304 }
1305
1306 /* Finish upload */
1307
1308 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1309 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely567d7112007-01-28 15:38:55 -03001310 LOCK_TAKE(hdw->ctl_lock); do {
1311 hdw->cmd_buffer[0] = FX2CMD_MEMSEL;
1312 hdw->cmd_buffer[1] = 0;
Al Viro89952d12007-03-14 09:17:59 +00001313 ret |= pvr2_send_request(hdw,hdw->cmd_buffer,2,NULL,0);
Mike Isely567d7112007-01-28 15:38:55 -03001314 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03001315
1316 if (ret) {
1317 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1318 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001319 }
1320 return ret;
1321}
1322
1323
Mike Isely681c7392007-11-26 01:48:52 -03001324static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001325{
Mike Isely681c7392007-11-26 01:48:52 -03001326 if (st < ARRAY_SIZE(pvr2_state_names)) {
1327 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001328 }
Mike Isely681c7392007-11-26 01:48:52 -03001329 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001330}
1331
Mike Isely681c7392007-11-26 01:48:52 -03001332static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001333{
Mike Isely681c7392007-11-26 01:48:52 -03001334 if (!hdw->decoder_ctrl) {
1335 if (!hdw->flag_decoder_missed) {
1336 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1337 "WARNING: No decoder present");
1338 hdw->flag_decoder_missed = !0;
1339 trace_stbit("flag_decoder_missed",
1340 hdw->flag_decoder_missed);
1341 }
1342 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001343 }
Mike Isely681c7392007-11-26 01:48:52 -03001344 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt,enablefl);
Mike Iselyd8554972006-06-26 20:58:46 -03001345 return 0;
1346}
1347
1348
Mike Isely681c7392007-11-26 01:48:52 -03001349void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1350{
1351 if (hdw->decoder_ctrl == ptr) return;
1352 hdw->decoder_ctrl = ptr;
1353 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1354 hdw->flag_decoder_missed = 0;
1355 trace_stbit("flag_decoder_missed",
1356 hdw->flag_decoder_missed);
1357 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1358 "Decoder has appeared");
1359 pvr2_hdw_state_sched(hdw);
1360 }
1361}
1362
1363
1364int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1365{
1366 return hdw->master_state;
1367}
1368
1369
1370static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1371{
1372 if (!hdw->flag_tripped) return 0;
1373 hdw->flag_tripped = 0;
1374 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1375 "Clearing driver error statuss");
1376 return !0;
1377}
1378
1379
1380int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1381{
1382 int fl;
1383 LOCK_TAKE(hdw->big_lock); do {
1384 fl = pvr2_hdw_untrip_unlocked(hdw);
1385 } while (0); LOCK_GIVE(hdw->big_lock);
1386 if (fl) pvr2_hdw_state_sched(hdw);
1387 return 0;
1388}
1389
1390
1391const char *pvr2_hdw_get_state_name(unsigned int id)
1392{
1393 if (id >= ARRAY_SIZE(pvr2_state_names)) return NULL;
1394 return pvr2_state_names[id];
1395}
1396
1397
Mike Iselyd8554972006-06-26 20:58:46 -03001398int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1399{
Mike Isely681c7392007-11-26 01:48:52 -03001400 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001401}
1402
1403
1404int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1405{
Mike Isely681c7392007-11-26 01:48:52 -03001406 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001407 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001408 pvr2_hdw_untrip_unlocked(hdw);
1409 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1410 hdw->state_pipeline_req = enable_flag != 0;
1411 pvr2_trace(PVR2_TRACE_START_STOP,
1412 "/*--TRACE_STREAM--*/ %s",
1413 enable_flag ? "enable" : "disable");
1414 }
1415 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001416 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001417 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1418 if (enable_flag) {
1419 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1420 if (st != PVR2_STATE_READY) return -EIO;
1421 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1422 }
1423 }
Mike Iselyd8554972006-06-26 20:58:46 -03001424 return 0;
1425}
1426
1427
1428int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1429{
Mike Isely681c7392007-11-26 01:48:52 -03001430 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001431 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001432 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1433 hdw->desired_stream_type = config;
1434 hdw->state_pipeline_config = 0;
1435 trace_stbit("state_pipeline_config",
1436 hdw->state_pipeline_config);
1437 pvr2_hdw_state_sched(hdw);
1438 }
Mike Iselyd8554972006-06-26 20:58:46 -03001439 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001440 if (fl) return 0;
1441 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001442}
1443
1444
1445static int get_default_tuner_type(struct pvr2_hdw *hdw)
1446{
1447 int unit_number = hdw->unit_number;
1448 int tp = -1;
1449 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1450 tp = tuner[unit_number];
1451 }
1452 if (tp < 0) return -EINVAL;
1453 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001454 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001455 return 0;
1456}
1457
1458
1459static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1460{
1461 int unit_number = hdw->unit_number;
1462 int tp = 0;
1463 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1464 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001465 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001466 }
Mike Isely6a540252007-12-02 23:51:34 -03001467 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001468}
1469
1470
1471static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1472{
1473 int unit_number = hdw->unit_number;
1474 int tp = 0;
1475 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1476 tp = tolerance[unit_number];
1477 }
1478 return tp;
1479}
1480
1481
1482static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1483{
1484 /* Try a harmless request to fetch the eeprom's address over
1485 endpoint 1. See what happens. Only the full FX2 image can
1486 respond to this. If this probe fails then likely the FX2
1487 firmware needs be loaded. */
1488 int result;
1489 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001490 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001491 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1492 hdw->cmd_buffer,1,
1493 hdw->cmd_buffer,1);
1494 if (result < 0) break;
1495 } while(0); LOCK_GIVE(hdw->ctl_lock);
1496 if (result) {
1497 pvr2_trace(PVR2_TRACE_INIT,
1498 "Probe of device endpoint 1 result status %d",
1499 result);
1500 } else {
1501 pvr2_trace(PVR2_TRACE_INIT,
1502 "Probe of device endpoint 1 succeeded");
1503 }
1504 return result == 0;
1505}
1506
Mike Isely9f66d4e2007-09-08 22:28:51 -03001507struct pvr2_std_hack {
1508 v4l2_std_id pat; /* Pattern to match */
1509 v4l2_std_id msk; /* Which bits we care about */
1510 v4l2_std_id std; /* What additional standards or default to set */
1511};
1512
1513/* This data structure labels specific combinations of standards from
1514 tveeprom that we'll try to recognize. If we recognize one, then assume
1515 a specified default standard to use. This is here because tveeprom only
1516 tells us about available standards not the intended default standard (if
1517 any) for the device in question. We guess the default based on what has
1518 been reported as available. Note that this is only for guessing a
1519 default - which can always be overridden explicitly - and if the user
1520 has otherwise named a default then that default will always be used in
1521 place of this table. */
1522const static struct pvr2_std_hack std_eeprom_maps[] = {
1523 { /* PAL(B/G) */
1524 .pat = V4L2_STD_B|V4L2_STD_GH,
1525 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1526 },
1527 { /* NTSC(M) */
1528 .pat = V4L2_STD_MN,
1529 .std = V4L2_STD_NTSC_M,
1530 },
1531 { /* PAL(I) */
1532 .pat = V4L2_STD_PAL_I,
1533 .std = V4L2_STD_PAL_I,
1534 },
1535 { /* SECAM(L/L') */
1536 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1537 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1538 },
1539 { /* PAL(D/D1/K) */
1540 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001541 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001542 },
1543};
1544
Mike Iselyd8554972006-06-26 20:58:46 -03001545static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1546{
1547 char buf[40];
1548 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001549 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001550
1551 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001552 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001553
1554 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001555 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001556 "Supported video standard(s) reported available"
1557 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001558 bcnt,buf);
1559
1560 hdw->std_mask_avail = hdw->std_mask_eeprom;
1561
Mike Isely3d290bd2007-12-03 01:47:12 -03001562 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001563 if (std2) {
1564 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001565 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001566 "Expanding supported video standards"
1567 " to include: %.*s",
1568 bcnt,buf);
1569 hdw->std_mask_avail |= std2;
1570 }
1571
1572 pvr2_hdw_internal_set_std_avail(hdw);
1573
1574 if (std1) {
1575 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001576 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001577 "Initial video standard forced to %.*s",
1578 bcnt,buf);
1579 hdw->std_mask_cur = std1;
1580 hdw->std_dirty = !0;
1581 pvr2_hdw_internal_find_stdenum(hdw);
1582 return;
1583 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001584 if (std3) {
1585 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1586 pvr2_trace(PVR2_TRACE_STD,
1587 "Initial video standard"
1588 " (determined by device type): %.*s",bcnt,buf);
1589 hdw->std_mask_cur = std3;
1590 hdw->std_dirty = !0;
1591 pvr2_hdw_internal_find_stdenum(hdw);
1592 return;
1593 }
Mike Iselyd8554972006-06-26 20:58:46 -03001594
Mike Isely9f66d4e2007-09-08 22:28:51 -03001595 {
1596 unsigned int idx;
1597 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1598 if (std_eeprom_maps[idx].msk ?
1599 ((std_eeprom_maps[idx].pat ^
1600 hdw->std_mask_eeprom) &
1601 std_eeprom_maps[idx].msk) :
1602 (std_eeprom_maps[idx].pat !=
1603 hdw->std_mask_eeprom)) continue;
1604 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1605 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001606 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001607 "Initial video standard guessed as %.*s",
1608 bcnt,buf);
1609 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1610 hdw->std_dirty = !0;
1611 pvr2_hdw_internal_find_stdenum(hdw);
1612 return;
1613 }
1614 }
1615
Mike Iselyd8554972006-06-26 20:58:46 -03001616 if (hdw->std_enum_cnt > 1) {
1617 // Autoselect the first listed standard
1618 hdw->std_enum_cur = 1;
1619 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1620 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001621 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001622 "Initial video standard auto-selected to %s",
1623 hdw->std_defs[hdw->std_enum_cur-1].name);
1624 return;
1625 }
1626
Mike Isely0885ba12006-06-25 21:30:47 -03001627 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001628 "Unable to select a viable initial video standard");
1629}
1630
1631
1632static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
1633{
1634 int ret;
1635 unsigned int idx;
1636 struct pvr2_ctrl *cptr;
1637 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03001638 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001639 if (!reloadFl) {
1640 reloadFl =
1641 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
1642 == 0);
1643 if (reloadFl) {
1644 pvr2_trace(PVR2_TRACE_INIT,
1645 "USB endpoint config looks strange"
1646 "; possibly firmware needs to be"
1647 " loaded");
1648 }
1649 }
1650 if (!reloadFl) {
1651 reloadFl = !pvr2_hdw_check_firmware(hdw);
1652 if (reloadFl) {
1653 pvr2_trace(PVR2_TRACE_INIT,
1654 "Check for FX2 firmware failed"
1655 "; possibly firmware needs to be"
1656 " loaded");
1657 }
1658 }
Mike Iselyd8554972006-06-26 20:58:46 -03001659 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03001660 if (pvr2_upload_firmware1(hdw) != 0) {
1661 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1662 "Failure uploading firmware1");
1663 }
1664 return;
Mike Iselyd8554972006-06-26 20:58:46 -03001665 }
1666 }
Mike Iselyd8554972006-06-26 20:58:46 -03001667 hdw->fw1_state = FW1_STATE_OK;
1668
1669 if (initusbreset) {
1670 pvr2_hdw_device_reset(hdw);
1671 }
1672 if (!pvr2_hdw_dev_ok(hdw)) return;
1673
Mike Isely989eb152007-11-26 01:53:12 -03001674 for (idx = 0; idx < hdw->hdw_desc->client_modules.cnt; idx++) {
1675 request_module(hdw->hdw_desc->client_modules.lst[idx]);
Mike Iselyd8554972006-06-26 20:58:46 -03001676 }
1677
Mike Isely989eb152007-11-26 01:53:12 -03001678 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03001679 pvr2_hdw_cmd_powerup(hdw);
1680 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03001681 }
1682
1683 // This step MUST happen after the earlier powerup step.
1684 pvr2_i2c_core_init(hdw);
1685 if (!pvr2_hdw_dev_ok(hdw)) return;
1686
Mike Iselyc05c0462006-06-25 20:04:25 -03001687 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001688 cptr = hdw->controls + idx;
1689 if (cptr->info->skip_init) continue;
1690 if (!cptr->info->set_value) continue;
1691 cptr->info->set_value(cptr,~0,cptr->info->default_value);
1692 }
1693
Mike Isely1bde0282006-12-27 23:30:13 -03001694 /* Set up special default values for the television and radio
1695 frequencies here. It's not really important what these defaults
1696 are, but I set them to something usable in the Chicago area just
1697 to make driver testing a little easier. */
1698
1699 /* US Broadcast channel 7 (175.25 MHz) */
1700 hdw->freqValTelevision = 175250000L;
1701 /* 104.3 MHz, a usable FM station for my area */
1702 hdw->freqValRadio = 104300000L;
1703
Mike Iselyd8554972006-06-26 20:58:46 -03001704 // Do not use pvr2_reset_ctl_endpoints() here. It is not
1705 // thread-safe against the normal pvr2_send_request() mechanism.
1706 // (We should make it thread safe).
1707
Mike Iselyaaf78842007-11-26 02:04:11 -03001708 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
1709 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001710 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03001711 if (ret < 0) {
1712 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1713 "Unable to determine location of eeprom,"
1714 " skipping");
1715 } else {
1716 hdw->eeprom_addr = ret;
1717 pvr2_eeprom_analyze(hdw);
1718 if (!pvr2_hdw_dev_ok(hdw)) return;
1719 }
1720 } else {
1721 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
1722 hdw->tuner_updated = !0;
1723 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03001724 }
1725
1726 pvr2_hdw_setup_std(hdw);
1727
1728 if (!get_default_tuner_type(hdw)) {
1729 pvr2_trace(PVR2_TRACE_INIT,
1730 "pvr2_hdw_setup: Tuner type overridden to %d",
1731 hdw->tuner_type);
1732 }
1733
Mike Iselyd8554972006-06-26 20:58:46 -03001734 pvr2_i2c_core_check_stale(hdw);
1735 hdw->tuner_updated = 0;
1736
1737 if (!pvr2_hdw_dev_ok(hdw)) return;
1738
Mike Isely681c7392007-11-26 01:48:52 -03001739 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001740
1741 hdw->vid_stream = pvr2_stream_create();
1742 if (!pvr2_hdw_dev_ok(hdw)) return;
1743 pvr2_trace(PVR2_TRACE_INIT,
1744 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
1745 if (hdw->vid_stream) {
1746 idx = get_default_error_tolerance(hdw);
1747 if (idx) {
1748 pvr2_trace(PVR2_TRACE_INIT,
1749 "pvr2_hdw_setup: video stream %p"
1750 " setting tolerance %u",
1751 hdw->vid_stream,idx);
1752 }
1753 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
1754 PVR2_VID_ENDPOINT,idx);
1755 }
1756
1757 if (!pvr2_hdw_dev_ok(hdw)) return;
1758
Mike Iselyd8554972006-06-26 20:58:46 -03001759 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03001760
1761 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001762}
1763
1764
Mike Isely681c7392007-11-26 01:48:52 -03001765/* Set up the structure and attempt to put the device into a usable state.
1766 This can be a time-consuming operation, which is why it is not done
1767 internally as part of the create() step. */
1768static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001769{
1770 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03001771 do {
Mike Iselyd8554972006-06-26 20:58:46 -03001772 pvr2_hdw_setup_low(hdw);
1773 pvr2_trace(PVR2_TRACE_INIT,
1774 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03001775 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03001776 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03001777 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03001778 pvr2_trace(
1779 PVR2_TRACE_INFO,
1780 "Device initialization"
1781 " completed successfully.");
1782 break;
1783 }
1784 if (hdw->fw1_state == FW1_STATE_RELOAD) {
1785 pvr2_trace(
1786 PVR2_TRACE_INFO,
1787 "Device microcontroller firmware"
1788 " (re)loaded; it should now reset"
1789 " and reconnect.");
1790 break;
1791 }
1792 pvr2_trace(
1793 PVR2_TRACE_ERROR_LEGS,
1794 "Device initialization was not successful.");
1795 if (hdw->fw1_state == FW1_STATE_MISSING) {
1796 pvr2_trace(
1797 PVR2_TRACE_ERROR_LEGS,
1798 "Giving up since device"
1799 " microcontroller firmware"
1800 " appears to be missing.");
1801 break;
1802 }
1803 }
1804 if (procreload) {
1805 pvr2_trace(
1806 PVR2_TRACE_ERROR_LEGS,
1807 "Attempting pvrusb2 recovery by reloading"
1808 " primary firmware.");
1809 pvr2_trace(
1810 PVR2_TRACE_ERROR_LEGS,
1811 "If this works, device should disconnect"
1812 " and reconnect in a sane state.");
1813 hdw->fw1_state = FW1_STATE_UNKNOWN;
1814 pvr2_upload_firmware1(hdw);
1815 } else {
1816 pvr2_trace(
1817 PVR2_TRACE_ERROR_LEGS,
1818 "***WARNING*** pvrusb2 device hardware"
1819 " appears to be jammed"
1820 " and I can't clear it.");
1821 pvr2_trace(
1822 PVR2_TRACE_ERROR_LEGS,
1823 "You might need to power cycle"
1824 " the pvrusb2 device"
1825 " in order to recover.");
1826 }
Mike Isely681c7392007-11-26 01:48:52 -03001827 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03001828 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001829}
1830
1831
1832/* Create and return a structure for interacting with the underlying
1833 hardware */
1834struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
1835 const struct usb_device_id *devid)
1836{
1837 unsigned int idx,cnt1,cnt2;
1838 struct pvr2_hdw *hdw;
Mike Iselyd8554972006-06-26 20:58:46 -03001839 int valid_std_mask;
1840 struct pvr2_ctrl *cptr;
Mike Isely989eb152007-11-26 01:53:12 -03001841 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03001842 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03001843 struct v4l2_queryctrl qctrl;
1844 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03001845
Mike Iselyd130fa82007-12-08 17:20:06 -03001846 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03001847
Mike Iselyca545f72007-01-20 00:37:11 -03001848 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001849 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03001850 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03001851 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03001852
1853 init_timer(&hdw->quiescent_timer);
1854 hdw->quiescent_timer.data = (unsigned long)hdw;
1855 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
1856
1857 init_timer(&hdw->encoder_wait_timer);
1858 hdw->encoder_wait_timer.data = (unsigned long)hdw;
1859 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
1860
1861 hdw->master_state = PVR2_STATE_DEAD;
1862
1863 init_waitqueue_head(&hdw->state_wait_data);
1864
Mike Isely18103c572007-01-20 00:09:47 -03001865 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001866 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03001867
Mike Iselyc05c0462006-06-25 20:04:25 -03001868 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03001869 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03001870 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001871 GFP_KERNEL);
1872 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03001873 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03001874 for (idx = 0; idx < hdw->control_cnt; idx++) {
1875 cptr = hdw->controls + idx;
1876 cptr->hdw = hdw;
1877 }
Mike Iselyd8554972006-06-26 20:58:46 -03001878 for (idx = 0; idx < 32; idx++) {
1879 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
1880 }
Mike Iselyc05c0462006-06-25 20:04:25 -03001881 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03001882 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03001883 cptr->info = control_defs+idx;
1884 }
Mike Iselyb30d2442006-06-25 20:05:01 -03001885 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03001886 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03001887 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
1888 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03001889 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
1890 cptr = hdw->controls + idx + CTRLDEF_COUNT;
1891 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
1892 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
1893 ciptr->name = mpeg_ids[idx].strid;
1894 ciptr->v4l_id = mpeg_ids[idx].id;
1895 ciptr->skip_init = !0;
1896 ciptr->get_value = ctrl_cx2341x_get;
1897 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
1898 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
1899 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
1900 qctrl.id = ciptr->v4l_id;
1901 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
1902 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
1903 ciptr->set_value = ctrl_cx2341x_set;
1904 }
1905 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
1906 PVR2_CTLD_INFO_DESC_SIZE);
1907 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
1908 ciptr->default_value = qctrl.default_value;
1909 switch (qctrl.type) {
1910 default:
1911 case V4L2_CTRL_TYPE_INTEGER:
1912 ciptr->type = pvr2_ctl_int;
1913 ciptr->def.type_int.min_value = qctrl.minimum;
1914 ciptr->def.type_int.max_value = qctrl.maximum;
1915 break;
1916 case V4L2_CTRL_TYPE_BOOLEAN:
1917 ciptr->type = pvr2_ctl_bool;
1918 break;
1919 case V4L2_CTRL_TYPE_MENU:
1920 ciptr->type = pvr2_ctl_enum;
1921 ciptr->def.type_enum.value_names =
1922 cx2341x_ctrl_get_menu(ciptr->v4l_id);
1923 for (cnt1 = 0;
1924 ciptr->def.type_enum.value_names[cnt1] != NULL;
1925 cnt1++) { }
1926 ciptr->def.type_enum.count = cnt1;
1927 break;
1928 }
1929 cptr->info = ciptr;
1930 }
Mike Iselyd8554972006-06-26 20:58:46 -03001931
1932 // Initialize video standard enum dynamic control
1933 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
1934 if (cptr) {
1935 memcpy(&hdw->std_info_enum,cptr->info,
1936 sizeof(hdw->std_info_enum));
1937 cptr->info = &hdw->std_info_enum;
1938
1939 }
1940 // Initialize control data regarding video standard masks
1941 valid_std_mask = pvr2_std_get_usable();
1942 for (idx = 0; idx < 32; idx++) {
1943 if (!(valid_std_mask & (1 << idx))) continue;
1944 cnt1 = pvr2_std_id_to_str(
1945 hdw->std_mask_names[idx],
1946 sizeof(hdw->std_mask_names[idx])-1,
1947 1 << idx);
1948 hdw->std_mask_names[idx][cnt1] = 0;
1949 }
1950 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
1951 if (cptr) {
1952 memcpy(&hdw->std_info_avail,cptr->info,
1953 sizeof(hdw->std_info_avail));
1954 cptr->info = &hdw->std_info_avail;
1955 hdw->std_info_avail.def.type_bitmask.bit_names =
1956 hdw->std_mask_ptrs;
1957 hdw->std_info_avail.def.type_bitmask.valid_bits =
1958 valid_std_mask;
1959 }
1960 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
1961 if (cptr) {
1962 memcpy(&hdw->std_info_cur,cptr->info,
1963 sizeof(hdw->std_info_cur));
1964 cptr->info = &hdw->std_info_cur;
1965 hdw->std_info_cur.def.type_bitmask.bit_names =
1966 hdw->std_mask_ptrs;
1967 hdw->std_info_avail.def.type_bitmask.valid_bits =
1968 valid_std_mask;
1969 }
1970
1971 hdw->eeprom_addr = -1;
1972 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03001973 hdw->v4l_minor_number_video = -1;
1974 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03001975 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03001976 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
1977 if (!hdw->ctl_write_buffer) goto fail;
1978 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
1979 if (!hdw->ctl_read_buffer) goto fail;
1980 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
1981 if (!hdw->ctl_write_urb) goto fail;
1982 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
1983 if (!hdw->ctl_read_urb) goto fail;
1984
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03001985 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03001986 for (idx = 0; idx < PVR_NUM; idx++) {
1987 if (unit_pointers[idx]) continue;
1988 hdw->unit_number = idx;
1989 unit_pointers[idx] = hdw;
1990 break;
1991 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03001992 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03001993
1994 cnt1 = 0;
1995 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
1996 cnt1 += cnt2;
1997 if (hdw->unit_number >= 0) {
1998 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
1999 ('a' + hdw->unit_number));
2000 cnt1 += cnt2;
2001 }
2002 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2003 hdw->name[cnt1] = 0;
2004
Mike Isely681c7392007-11-26 01:48:52 -03002005 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2006 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2007 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
2008 INIT_WORK(&hdw->workinit,pvr2_hdw_worker_init);
2009
Mike Iselyd8554972006-06-26 20:58:46 -03002010 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2011 hdw->unit_number,hdw->name);
2012
2013 hdw->tuner_type = -1;
2014 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002015
2016 hdw->usb_intf = intf;
2017 hdw->usb_dev = interface_to_usbdev(intf);
2018
Mike Isely31a18542007-04-08 01:11:47 -03002019 scnprintf(hdw->bus_info,sizeof(hdw->bus_info),
2020 "usb %s address %d",
2021 hdw->usb_dev->dev.bus_id,
2022 hdw->usb_dev->devnum);
2023
Mike Iselyd8554972006-06-26 20:58:46 -03002024 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2025 usb_set_interface(hdw->usb_dev,ifnum,0);
2026
2027 mutex_init(&hdw->ctl_lock_mutex);
2028 mutex_init(&hdw->big_lock_mutex);
2029
Mike Isely681c7392007-11-26 01:48:52 -03002030 queue_work(hdw->workqueue,&hdw->workinit);
Mike Iselyd8554972006-06-26 20:58:46 -03002031 return hdw;
2032 fail:
2033 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002034 del_timer_sync(&hdw->quiescent_timer);
2035 del_timer_sync(&hdw->encoder_wait_timer);
2036 if (hdw->workqueue) {
2037 flush_workqueue(hdw->workqueue);
2038 destroy_workqueue(hdw->workqueue);
2039 hdw->workqueue = NULL;
2040 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002041 usb_free_urb(hdw->ctl_read_urb);
2042 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002043 kfree(hdw->ctl_read_buffer);
2044 kfree(hdw->ctl_write_buffer);
2045 kfree(hdw->controls);
2046 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002047 kfree(hdw->std_defs);
2048 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002049 kfree(hdw);
2050 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002051 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002052}
2053
2054
2055/* Remove _all_ associations between this driver and the underlying USB
2056 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002057static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002058{
2059 if (hdw->flag_disconnected) return;
2060 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2061 if (hdw->ctl_read_urb) {
2062 usb_kill_urb(hdw->ctl_read_urb);
2063 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002064 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002065 }
2066 if (hdw->ctl_write_urb) {
2067 usb_kill_urb(hdw->ctl_write_urb);
2068 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002069 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002070 }
2071 if (hdw->ctl_read_buffer) {
2072 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002073 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002074 }
2075 if (hdw->ctl_write_buffer) {
2076 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002077 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002078 }
Mike Iselyd8554972006-06-26 20:58:46 -03002079 hdw->flag_disconnected = !0;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002080 hdw->usb_dev = NULL;
2081 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002082 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002083}
2084
2085
2086/* Destroy hardware interaction structure */
2087void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2088{
Mike Isely401c27c2007-09-08 22:11:46 -03002089 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002090 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002091 del_timer_sync(&hdw->quiescent_timer);
2092 del_timer_sync(&hdw->encoder_wait_timer);
2093 if (hdw->workqueue) {
2094 flush_workqueue(hdw->workqueue);
2095 destroy_workqueue(hdw->workqueue);
2096 hdw->workqueue = NULL;
2097 }
Mike Iselyd8554972006-06-26 20:58:46 -03002098 if (hdw->fw_buffer) {
2099 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002100 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002101 }
2102 if (hdw->vid_stream) {
2103 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002104 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002105 }
Mike Iselyd8554972006-06-26 20:58:46 -03002106 if (hdw->decoder_ctrl) {
2107 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2108 }
2109 pvr2_i2c_core_done(hdw);
2110 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002111 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002112 if ((hdw->unit_number >= 0) &&
2113 (hdw->unit_number < PVR_NUM) &&
2114 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002115 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002116 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002117 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002118 kfree(hdw->controls);
2119 kfree(hdw->mpeg_ctrl_info);
2120 kfree(hdw->std_defs);
2121 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002122 kfree(hdw);
2123}
2124
2125
Mike Iselyd8554972006-06-26 20:58:46 -03002126int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2127{
2128 return (hdw && hdw->flag_ok);
2129}
2130
2131
2132/* Called when hardware has been unplugged */
2133void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2134{
2135 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2136 LOCK_TAKE(hdw->big_lock);
2137 LOCK_TAKE(hdw->ctl_lock);
2138 pvr2_hdw_remove_usb_stuff(hdw);
2139 LOCK_GIVE(hdw->ctl_lock);
2140 LOCK_GIVE(hdw->big_lock);
2141}
2142
2143
2144// Attempt to autoselect an appropriate value for std_enum_cur given
2145// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002146static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002147{
2148 unsigned int idx;
2149 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2150 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2151 hdw->std_enum_cur = idx;
2152 return;
2153 }
2154 }
2155 hdw->std_enum_cur = 0;
2156}
2157
2158
2159// Calculate correct set of enumerated standards based on currently known
2160// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002161static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002162{
2163 struct v4l2_standard *newstd;
2164 unsigned int std_cnt;
2165 unsigned int idx;
2166
2167 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2168
2169 if (hdw->std_defs) {
2170 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002171 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002172 }
2173 hdw->std_enum_cnt = 0;
2174 if (hdw->std_enum_names) {
2175 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002176 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002177 }
2178
2179 if (!std_cnt) {
2180 pvr2_trace(
2181 PVR2_TRACE_ERROR_LEGS,
2182 "WARNING: Failed to identify any viable standards");
2183 }
2184 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2185 hdw->std_enum_names[0] = "none";
2186 for (idx = 0; idx < std_cnt; idx++) {
2187 hdw->std_enum_names[idx+1] =
2188 newstd[idx].name;
2189 }
2190 // Set up the dynamic control for this standard
2191 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2192 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2193 hdw->std_defs = newstd;
2194 hdw->std_enum_cnt = std_cnt+1;
2195 hdw->std_enum_cur = 0;
2196 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2197}
2198
2199
2200int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2201 struct v4l2_standard *std,
2202 unsigned int idx)
2203{
2204 int ret = -EINVAL;
2205 if (!idx) return ret;
2206 LOCK_TAKE(hdw->big_lock); do {
2207 if (idx >= hdw->std_enum_cnt) break;
2208 idx--;
2209 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2210 ret = 0;
2211 } while (0); LOCK_GIVE(hdw->big_lock);
2212 return ret;
2213}
2214
2215
2216/* Get the number of defined controls */
2217unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2218{
Mike Iselyc05c0462006-06-25 20:04:25 -03002219 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002220}
2221
2222
2223/* Retrieve a control handle given its index (0..count-1) */
2224struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2225 unsigned int idx)
2226{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002227 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002228 return hdw->controls + idx;
2229}
2230
2231
2232/* Retrieve a control handle given its index (0..count-1) */
2233struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2234 unsigned int ctl_id)
2235{
2236 struct pvr2_ctrl *cptr;
2237 unsigned int idx;
2238 int i;
2239
2240 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002241 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002242 cptr = hdw->controls + idx;
2243 i = cptr->info->internal_id;
2244 if (i && (i == ctl_id)) return cptr;
2245 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002246 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002247}
2248
2249
Mike Iselya761f432006-06-25 20:04:44 -03002250/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002251struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2252{
2253 struct pvr2_ctrl *cptr;
2254 unsigned int idx;
2255 int i;
2256
2257 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002258 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002259 cptr = hdw->controls + idx;
2260 i = cptr->info->v4l_id;
2261 if (i && (i == ctl_id)) return cptr;
2262 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002263 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002264}
2265
2266
Mike Iselya761f432006-06-25 20:04:44 -03002267/* Given a V4L ID for its immediate predecessor, retrieve the control
2268 structure associated with it. */
2269struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2270 unsigned int ctl_id)
2271{
2272 struct pvr2_ctrl *cptr,*cp2;
2273 unsigned int idx;
2274 int i;
2275
2276 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002277 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002278 for (idx = 0; idx < hdw->control_cnt; idx++) {
2279 cptr = hdw->controls + idx;
2280 i = cptr->info->v4l_id;
2281 if (!i) continue;
2282 if (i <= ctl_id) continue;
2283 if (cp2 && (cp2->info->v4l_id < i)) continue;
2284 cp2 = cptr;
2285 }
2286 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002287 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002288}
2289
2290
Mike Iselyd8554972006-06-26 20:58:46 -03002291static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2292{
2293 switch (tp) {
2294 case pvr2_ctl_int: return "integer";
2295 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002296 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002297 case pvr2_ctl_bitmask: return "bitmask";
2298 }
2299 return "";
2300}
2301
2302
Mike Isely681c7392007-11-26 01:48:52 -03002303/* Figure out if we need to commit control changes. If so, mark internal
2304 state flags to indicate this fact and return true. Otherwise do nothing
2305 else and return false. */
2306static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002307{
Mike Iselyd8554972006-06-26 20:58:46 -03002308 unsigned int idx;
2309 struct pvr2_ctrl *cptr;
2310 int value;
2311 int commit_flag = 0;
2312 char buf[100];
2313 unsigned int bcnt,ccnt;
2314
Mike Iselyc05c0462006-06-25 20:04:25 -03002315 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002316 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00002317 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002318 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03002319 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002320
Mike Iselyfe23a282007-01-20 00:10:55 -03002321 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03002322 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
2323 cptr->info->name);
2324 value = 0;
2325 cptr->info->get_value(cptr,&value);
2326 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
2327 buf+bcnt,
2328 sizeof(buf)-bcnt,&ccnt);
2329 bcnt += ccnt;
2330 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
2331 get_ctrl_typename(cptr->info->type));
2332 pvr2_trace(PVR2_TRACE_CTL,
2333 "/*--TRACE_COMMIT--*/ %.*s",
2334 bcnt,buf);
2335 }
2336
2337 if (!commit_flag) {
2338 /* Nothing has changed */
2339 return 0;
2340 }
2341
Mike Isely681c7392007-11-26 01:48:52 -03002342 hdw->state_pipeline_config = 0;
2343 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2344 pvr2_hdw_state_sched(hdw);
2345
2346 return !0;
2347}
2348
2349
2350/* Perform all operations needed to commit all control changes. This must
2351 be performed in synchronization with the pipeline state and is thus
2352 expected to be called as part of the driver's worker thread. Return
2353 true if commit successful, otherwise return false to indicate that
2354 commit isn't possible at this time. */
2355static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
2356{
2357 unsigned int idx;
2358 struct pvr2_ctrl *cptr;
2359 int disruptive_change;
2360
Mike Iselyd8554972006-06-26 20:58:46 -03002361 /* When video standard changes, reset the hres and vres values -
2362 but if the user has pending changes there, then let the changes
2363 take priority. */
2364 if (hdw->std_dirty) {
2365 /* Rewrite the vertical resolution to be appropriate to the
2366 video standard that has been selected. */
2367 int nvres;
2368 if (hdw->std_mask_cur & V4L2_STD_525_60) {
2369 nvres = 480;
2370 } else {
2371 nvres = 576;
2372 }
2373 if (nvres != hdw->res_ver_val) {
2374 hdw->res_ver_val = nvres;
2375 hdw->res_ver_dirty = !0;
2376 }
Mike Iselyd8554972006-06-26 20:58:46 -03002377 }
2378
Mike Isely681c7392007-11-26 01:48:52 -03002379 /* If any of the below has changed, then we can't do the update
2380 while the pipeline is running. Pipeline must be paused first
2381 and decoder -> encoder connection be made quiescent before we
2382 can proceed. */
2383 disruptive_change =
2384 (hdw->std_dirty ||
2385 hdw->enc_unsafe_stale ||
2386 hdw->srate_dirty ||
2387 hdw->res_ver_dirty ||
2388 hdw->res_hor_dirty ||
2389 hdw->input_dirty ||
2390 (hdw->active_stream_type != hdw->desired_stream_type));
2391 if (disruptive_change && !hdw->state_pipeline_idle) {
2392 /* Pipeline is not idle; we can't proceed. Arrange to
2393 cause pipeline to stop so that we can try this again
2394 later.... */
2395 hdw->state_pipeline_pause = !0;
2396 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002397 }
2398
Mike Iselyb30d2442006-06-25 20:05:01 -03002399 if (hdw->srate_dirty) {
2400 /* Write new sample rate into control structure since
2401 * the master copy is stale. We must track srate
2402 * separate from the mpeg control structure because
2403 * other logic also uses this value. */
2404 struct v4l2_ext_controls cs;
2405 struct v4l2_ext_control c1;
2406 memset(&cs,0,sizeof(cs));
2407 memset(&c1,0,sizeof(c1));
2408 cs.controls = &c1;
2409 cs.count = 1;
2410 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
2411 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03002412 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03002413 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002414
Mike Iselyd8554972006-06-26 20:58:46 -03002415 /* Scan i2c core at this point - before we clear all the dirty
2416 bits. Various parts of the i2c core will notice dirty bits as
2417 appropriate and arrange to broadcast or directly send updates to
2418 the client drivers in order to keep everything in sync */
2419 pvr2_i2c_core_check_stale(hdw);
2420
Mike Iselyc05c0462006-06-25 20:04:25 -03002421 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002422 cptr = hdw->controls + idx;
2423 if (!cptr->info->clear_dirty) continue;
2424 cptr->info->clear_dirty(cptr);
2425 }
2426
Mike Isely681c7392007-11-26 01:48:52 -03002427 if (hdw->active_stream_type != hdw->desired_stream_type) {
2428 /* Handle any side effects of stream config here */
2429 hdw->active_stream_type = hdw->desired_stream_type;
2430 }
2431
Mike Iselyd8554972006-06-26 20:58:46 -03002432 /* Now execute i2c core update */
2433 pvr2_i2c_core_sync(hdw);
2434
Mike Isely681c7392007-11-26 01:48:52 -03002435 if (hdw->state_encoder_run) {
2436 /* If encoder isn't running, then this will get worked out
2437 later when we start the encoder. */
2438 if (pvr2_encoder_adjust(hdw) < 0) return !0;
2439 }
Mike Iselyd8554972006-06-26 20:58:46 -03002440
Mike Isely681c7392007-11-26 01:48:52 -03002441 hdw->state_pipeline_config = !0;
2442 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
2443 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002444}
2445
2446
2447int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
2448{
Mike Isely681c7392007-11-26 01:48:52 -03002449 int fl;
2450 LOCK_TAKE(hdw->big_lock);
2451 fl = pvr2_hdw_commit_setup(hdw);
2452 LOCK_GIVE(hdw->big_lock);
2453 if (!fl) return 0;
2454 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03002455}
2456
2457
Mike Isely681c7392007-11-26 01:48:52 -03002458static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002459{
Mike Isely681c7392007-11-26 01:48:52 -03002460 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03002461 LOCK_TAKE(hdw->big_lock); do {
2462 pvr2_i2c_core_sync(hdw);
2463 } while (0); LOCK_GIVE(hdw->big_lock);
2464}
2465
2466
Mike Isely681c7392007-11-26 01:48:52 -03002467static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03002468{
Mike Isely681c7392007-11-26 01:48:52 -03002469 int fl = 0;
2470 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03002471 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03002472 fl = pvr2_hdw_state_eval(hdw);
2473 } while (0); LOCK_GIVE(hdw->big_lock);
2474 if (fl && hdw->state_func) {
2475 hdw->state_func(hdw->state_data);
2476 }
2477}
2478
2479
2480static void pvr2_hdw_worker_init(struct work_struct *work)
2481{
2482 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workinit);
2483 LOCK_TAKE(hdw->big_lock); do {
2484 pvr2_hdw_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002485 } while (0); LOCK_GIVE(hdw->big_lock);
2486}
2487
2488
Mike Isely681c7392007-11-26 01:48:52 -03002489static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03002490{
Mike Isely681c7392007-11-26 01:48:52 -03002491 return wait_event_interruptible(
2492 hdw->state_wait_data,
2493 (hdw->state_stale == 0) &&
2494 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03002495}
2496
Mike Isely681c7392007-11-26 01:48:52 -03002497
2498void pvr2_hdw_set_state_callback(struct pvr2_hdw *hdw,
2499 void (*callback_func)(void *),
2500 void *callback_data)
2501{
2502 LOCK_TAKE(hdw->big_lock); do {
2503 hdw->state_data = callback_data;
2504 hdw->state_func = callback_func;
2505 } while (0); LOCK_GIVE(hdw->big_lock);
2506}
2507
2508
Mike Iselyd8554972006-06-26 20:58:46 -03002509/* Return name for this driver instance */
2510const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
2511{
2512 return hdw->name;
2513}
2514
2515
Mike Isely78a47102007-11-26 01:58:20 -03002516const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
2517{
2518 return hdw->hdw_desc->description;
2519}
2520
2521
2522const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
2523{
2524 return hdw->hdw_desc->shortname;
2525}
2526
2527
Mike Iselyd8554972006-06-26 20:58:46 -03002528int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
2529{
2530 int result;
2531 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03002532 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03002533 result = pvr2_send_request(hdw,
2534 hdw->cmd_buffer,1,
2535 hdw->cmd_buffer,1);
2536 if (result < 0) break;
2537 result = (hdw->cmd_buffer[0] != 0);
2538 } while(0); LOCK_GIVE(hdw->ctl_lock);
2539 return result;
2540}
2541
2542
Mike Isely18103c572007-01-20 00:09:47 -03002543/* Execute poll of tuner status */
2544void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002545{
Mike Iselyd8554972006-06-26 20:58:46 -03002546 LOCK_TAKE(hdw->big_lock); do {
Mike Isely18103c572007-01-20 00:09:47 -03002547 pvr2_i2c_core_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002548 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03002549}
2550
2551
2552/* Return information about the tuner */
2553int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
2554{
2555 LOCK_TAKE(hdw->big_lock); do {
2556 if (hdw->tuner_signal_stale) {
2557 pvr2_i2c_core_status_poll(hdw);
2558 }
2559 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
2560 } while (0); LOCK_GIVE(hdw->big_lock);
2561 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03002562}
2563
2564
2565/* Get handle to video output stream */
2566struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
2567{
2568 return hp->vid_stream;
2569}
2570
2571
2572void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
2573{
Mike Isely4f1a3e52006-06-25 20:04:31 -03002574 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002575 LOCK_TAKE(hdw->big_lock); do {
2576 hdw->log_requested = !0;
Mike Isely4f1a3e52006-06-25 20:04:31 -03002577 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002578 pvr2_i2c_core_check_stale(hdw);
2579 hdw->log_requested = 0;
2580 pvr2_i2c_core_sync(hdw);
Mike Iselyb30d2442006-06-25 20:05:01 -03002581 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03002582 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03002583 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03002584 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03002585 } while (0); LOCK_GIVE(hdw->big_lock);
2586}
2587
Mike Isely4db666c2007-09-08 22:16:27 -03002588
2589/* Grab EEPROM contents, needed for direct method. */
2590#define EEPROM_SIZE 8192
2591#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
2592static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
2593{
2594 struct i2c_msg msg[2];
2595 u8 *eeprom;
2596 u8 iadd[2];
2597 u8 addr;
2598 u16 eepromSize;
2599 unsigned int offs;
2600 int ret;
2601 int mode16 = 0;
2602 unsigned pcnt,tcnt;
2603 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
2604 if (!eeprom) {
2605 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2606 "Failed to allocate memory"
2607 " required to read eeprom");
2608 return NULL;
2609 }
2610
2611 trace_eeprom("Value for eeprom addr from controller was 0x%x",
2612 hdw->eeprom_addr);
2613 addr = hdw->eeprom_addr;
2614 /* Seems that if the high bit is set, then the *real* eeprom
2615 address is shifted right now bit position (noticed this in
2616 newer PVR USB2 hardware) */
2617 if (addr & 0x80) addr >>= 1;
2618
2619 /* FX2 documentation states that a 16bit-addressed eeprom is
2620 expected if the I2C address is an odd number (yeah, this is
2621 strange but it's what they do) */
2622 mode16 = (addr & 1);
2623 eepromSize = (mode16 ? EEPROM_SIZE : 256);
2624 trace_eeprom("Examining %d byte eeprom at location 0x%x"
2625 " using %d bit addressing",eepromSize,addr,
2626 mode16 ? 16 : 8);
2627
2628 msg[0].addr = addr;
2629 msg[0].flags = 0;
2630 msg[0].len = mode16 ? 2 : 1;
2631 msg[0].buf = iadd;
2632 msg[1].addr = addr;
2633 msg[1].flags = I2C_M_RD;
2634
2635 /* We have to do the actual eeprom data fetch ourselves, because
2636 (1) we're only fetching part of the eeprom, and (2) if we were
2637 getting the whole thing our I2C driver can't grab it in one
2638 pass - which is what tveeprom is otherwise going to attempt */
2639 memset(eeprom,0,EEPROM_SIZE);
2640 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
2641 pcnt = 16;
2642 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
2643 offs = tcnt + (eepromSize - EEPROM_SIZE);
2644 if (mode16) {
2645 iadd[0] = offs >> 8;
2646 iadd[1] = offs;
2647 } else {
2648 iadd[0] = offs;
2649 }
2650 msg[1].len = pcnt;
2651 msg[1].buf = eeprom+tcnt;
2652 if ((ret = i2c_transfer(&hdw->i2c_adap,
2653 msg,ARRAY_SIZE(msg))) != 2) {
2654 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2655 "eeprom fetch set offs err=%d",ret);
2656 kfree(eeprom);
2657 return NULL;
2658 }
2659 }
2660 return eeprom;
2661}
2662
2663
2664void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
2665 int prom_flag,
2666 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002667{
2668 int ret;
2669 u16 address;
2670 unsigned int pipe;
2671 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00002672 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03002673
2674 if (!enable_flag) {
2675 pvr2_trace(PVR2_TRACE_FIRMWARE,
2676 "Cleaning up after CPU firmware fetch");
2677 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002678 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002679 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03002680 if (hdw->fw_cpu_flag) {
2681 /* Now release the CPU. It will disconnect
2682 and reconnect later. */
2683 pvr2_hdw_cpureset_assert(hdw,0);
2684 }
Mike Iselyd8554972006-06-26 20:58:46 -03002685 break;
2686 }
2687
Mike Isely4db666c2007-09-08 22:16:27 -03002688 hdw->fw_cpu_flag = (prom_flag == 0);
2689 if (hdw->fw_cpu_flag) {
2690 pvr2_trace(PVR2_TRACE_FIRMWARE,
2691 "Preparing to suck out CPU firmware");
2692 hdw->fw_size = 0x2000;
2693 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
2694 if (!hdw->fw_buffer) {
2695 hdw->fw_size = 0;
2696 break;
2697 }
2698
2699 /* We have to hold the CPU during firmware upload. */
2700 pvr2_hdw_cpureset_assert(hdw,1);
2701
2702 /* download the firmware from address 0000-1fff in 2048
2703 (=0x800) bytes chunk. */
2704
2705 pvr2_trace(PVR2_TRACE_FIRMWARE,
2706 "Grabbing CPU firmware");
2707 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
2708 for(address = 0; address < hdw->fw_size;
2709 address += 0x800) {
2710 ret = usb_control_msg(hdw->usb_dev,pipe,
2711 0xa0,0xc0,
2712 address,0,
2713 hdw->fw_buffer+address,
2714 0x800,HZ);
2715 if (ret < 0) break;
2716 }
2717
2718 pvr2_trace(PVR2_TRACE_FIRMWARE,
2719 "Done grabbing CPU firmware");
2720 } else {
2721 pvr2_trace(PVR2_TRACE_FIRMWARE,
2722 "Sucking down EEPROM contents");
2723 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
2724 if (!hdw->fw_buffer) {
2725 pvr2_trace(PVR2_TRACE_FIRMWARE,
2726 "EEPROM content suck failed.");
2727 break;
2728 }
2729 hdw->fw_size = EEPROM_SIZE;
2730 pvr2_trace(PVR2_TRACE_FIRMWARE,
2731 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03002732 }
2733
Mike Iselyd8554972006-06-26 20:58:46 -03002734 } while (0); LOCK_GIVE(hdw->big_lock);
2735}
2736
2737
2738/* Return true if we're in a mode for retrieval CPU firmware */
2739int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
2740{
Al Viro5fa12472008-03-29 03:07:38 +00002741 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002742}
2743
2744
2745int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
2746 char *buf,unsigned int cnt)
2747{
2748 int ret = -EINVAL;
2749 LOCK_TAKE(hdw->big_lock); do {
2750 if (!buf) break;
2751 if (!cnt) break;
2752
2753 if (!hdw->fw_buffer) {
2754 ret = -EIO;
2755 break;
2756 }
2757
2758 if (offs >= hdw->fw_size) {
2759 pvr2_trace(PVR2_TRACE_FIRMWARE,
2760 "Read firmware data offs=%d EOF",
2761 offs);
2762 ret = 0;
2763 break;
2764 }
2765
2766 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
2767
2768 memcpy(buf,hdw->fw_buffer+offs,cnt);
2769
2770 pvr2_trace(PVR2_TRACE_FIRMWARE,
2771 "Read firmware data offs=%d cnt=%d",
2772 offs,cnt);
2773 ret = cnt;
2774 } while (0); LOCK_GIVE(hdw->big_lock);
2775
2776 return ret;
2777}
2778
2779
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002780int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002781 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03002782{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002783 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002784 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
2785 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
2786 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002787 default: return -1;
2788 }
Mike Iselyd8554972006-06-26 20:58:46 -03002789}
2790
2791
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03002792/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002793void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03002794 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03002795{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002796 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03002797 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
2798 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
2799 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002800 default: break;
2801 }
Mike Iselyd8554972006-06-26 20:58:46 -03002802}
2803
2804
David Howells7d12e782006-10-05 14:55:46 +01002805static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002806{
2807 struct pvr2_hdw *hdw = urb->context;
2808 hdw->ctl_write_pend_flag = 0;
2809 if (hdw->ctl_read_pend_flag) return;
2810 complete(&hdw->ctl_done);
2811}
2812
2813
David Howells7d12e782006-10-05 14:55:46 +01002814static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03002815{
2816 struct pvr2_hdw *hdw = urb->context;
2817 hdw->ctl_read_pend_flag = 0;
2818 if (hdw->ctl_write_pend_flag) return;
2819 complete(&hdw->ctl_done);
2820}
2821
2822
2823static void pvr2_ctl_timeout(unsigned long data)
2824{
2825 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
2826 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2827 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002828 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002829 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002830 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03002831 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03002832 }
2833}
2834
2835
Mike Iselye61b6fc2006-07-18 22:42:18 -03002836/* Issue a command and get a response from the device. This extended
2837 version includes a probe flag (which if set means that device errors
2838 should not be logged or treated as fatal) and a timeout in jiffies.
2839 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002840static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
2841 unsigned int timeout,int probe_fl,
2842 void *write_data,unsigned int write_len,
2843 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03002844{
2845 unsigned int idx;
2846 int status = 0;
2847 struct timer_list timer;
2848 if (!hdw->ctl_lock_held) {
2849 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2850 "Attempted to execute control transfer"
2851 " without lock!!");
2852 return -EDEADLK;
2853 }
Mike Isely681c7392007-11-26 01:48:52 -03002854 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03002855 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2856 "Attempted to execute control transfer"
2857 " when device not ok");
2858 return -EIO;
2859 }
2860 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
2861 if (!probe_fl) {
2862 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2863 "Attempted to execute control transfer"
2864 " when USB is disconnected");
2865 }
2866 return -ENOTTY;
2867 }
2868
2869 /* Ensure that we have sane parameters */
2870 if (!write_data) write_len = 0;
2871 if (!read_data) read_len = 0;
2872 if (write_len > PVR2_CTL_BUFFSIZE) {
2873 pvr2_trace(
2874 PVR2_TRACE_ERROR_LEGS,
2875 "Attempted to execute %d byte"
2876 " control-write transfer (limit=%d)",
2877 write_len,PVR2_CTL_BUFFSIZE);
2878 return -EINVAL;
2879 }
2880 if (read_len > PVR2_CTL_BUFFSIZE) {
2881 pvr2_trace(
2882 PVR2_TRACE_ERROR_LEGS,
2883 "Attempted to execute %d byte"
2884 " control-read transfer (limit=%d)",
2885 write_len,PVR2_CTL_BUFFSIZE);
2886 return -EINVAL;
2887 }
2888 if ((!write_len) && (!read_len)) {
2889 pvr2_trace(
2890 PVR2_TRACE_ERROR_LEGS,
2891 "Attempted to execute null control transfer?");
2892 return -EINVAL;
2893 }
2894
2895
2896 hdw->cmd_debug_state = 1;
2897 if (write_len) {
2898 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
2899 } else {
2900 hdw->cmd_debug_code = 0;
2901 }
2902 hdw->cmd_debug_write_len = write_len;
2903 hdw->cmd_debug_read_len = read_len;
2904
2905 /* Initialize common stuff */
2906 init_completion(&hdw->ctl_done);
2907 hdw->ctl_timeout_flag = 0;
2908 hdw->ctl_write_pend_flag = 0;
2909 hdw->ctl_read_pend_flag = 0;
2910 init_timer(&timer);
2911 timer.expires = jiffies + timeout;
2912 timer.data = (unsigned long)hdw;
2913 timer.function = pvr2_ctl_timeout;
2914
2915 if (write_len) {
2916 hdw->cmd_debug_state = 2;
2917 /* Transfer write data to internal buffer */
2918 for (idx = 0; idx < write_len; idx++) {
2919 hdw->ctl_write_buffer[idx] =
2920 ((unsigned char *)write_data)[idx];
2921 }
2922 /* Initiate a write request */
2923 usb_fill_bulk_urb(hdw->ctl_write_urb,
2924 hdw->usb_dev,
2925 usb_sndbulkpipe(hdw->usb_dev,
2926 PVR2_CTL_WRITE_ENDPOINT),
2927 hdw->ctl_write_buffer,
2928 write_len,
2929 pvr2_ctl_write_complete,
2930 hdw);
2931 hdw->ctl_write_urb->actual_length = 0;
2932 hdw->ctl_write_pend_flag = !0;
2933 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
2934 if (status < 0) {
2935 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2936 "Failed to submit write-control"
2937 " URB status=%d",status);
2938 hdw->ctl_write_pend_flag = 0;
2939 goto done;
2940 }
2941 }
2942
2943 if (read_len) {
2944 hdw->cmd_debug_state = 3;
2945 memset(hdw->ctl_read_buffer,0x43,read_len);
2946 /* Initiate a read request */
2947 usb_fill_bulk_urb(hdw->ctl_read_urb,
2948 hdw->usb_dev,
2949 usb_rcvbulkpipe(hdw->usb_dev,
2950 PVR2_CTL_READ_ENDPOINT),
2951 hdw->ctl_read_buffer,
2952 read_len,
2953 pvr2_ctl_read_complete,
2954 hdw);
2955 hdw->ctl_read_urb->actual_length = 0;
2956 hdw->ctl_read_pend_flag = !0;
2957 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
2958 if (status < 0) {
2959 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2960 "Failed to submit read-control"
2961 " URB status=%d",status);
2962 hdw->ctl_read_pend_flag = 0;
2963 goto done;
2964 }
2965 }
2966
2967 /* Start timer */
2968 add_timer(&timer);
2969
2970 /* Now wait for all I/O to complete */
2971 hdw->cmd_debug_state = 4;
2972 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
2973 wait_for_completion(&hdw->ctl_done);
2974 }
2975 hdw->cmd_debug_state = 5;
2976
2977 /* Stop timer */
2978 del_timer_sync(&timer);
2979
2980 hdw->cmd_debug_state = 6;
2981 status = 0;
2982
2983 if (hdw->ctl_timeout_flag) {
2984 status = -ETIMEDOUT;
2985 if (!probe_fl) {
2986 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2987 "Timed out control-write");
2988 }
2989 goto done;
2990 }
2991
2992 if (write_len) {
2993 /* Validate results of write request */
2994 if ((hdw->ctl_write_urb->status != 0) &&
2995 (hdw->ctl_write_urb->status != -ENOENT) &&
2996 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
2997 (hdw->ctl_write_urb->status != -ECONNRESET)) {
2998 /* USB subsystem is reporting some kind of failure
2999 on the write */
3000 status = hdw->ctl_write_urb->status;
3001 if (!probe_fl) {
3002 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3003 "control-write URB failure,"
3004 " status=%d",
3005 status);
3006 }
3007 goto done;
3008 }
3009 if (hdw->ctl_write_urb->actual_length < write_len) {
3010 /* Failed to write enough data */
3011 status = -EIO;
3012 if (!probe_fl) {
3013 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3014 "control-write URB short,"
3015 " expected=%d got=%d",
3016 write_len,
3017 hdw->ctl_write_urb->actual_length);
3018 }
3019 goto done;
3020 }
3021 }
3022 if (read_len) {
3023 /* Validate results of read request */
3024 if ((hdw->ctl_read_urb->status != 0) &&
3025 (hdw->ctl_read_urb->status != -ENOENT) &&
3026 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3027 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3028 /* USB subsystem is reporting some kind of failure
3029 on the read */
3030 status = hdw->ctl_read_urb->status;
3031 if (!probe_fl) {
3032 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3033 "control-read URB failure,"
3034 " status=%d",
3035 status);
3036 }
3037 goto done;
3038 }
3039 if (hdw->ctl_read_urb->actual_length < read_len) {
3040 /* Failed to read enough data */
3041 status = -EIO;
3042 if (!probe_fl) {
3043 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3044 "control-read URB short,"
3045 " expected=%d got=%d",
3046 read_len,
3047 hdw->ctl_read_urb->actual_length);
3048 }
3049 goto done;
3050 }
3051 /* Transfer retrieved data out from internal buffer */
3052 for (idx = 0; idx < read_len; idx++) {
3053 ((unsigned char *)read_data)[idx] =
3054 hdw->ctl_read_buffer[idx];
3055 }
3056 }
3057
3058 done:
3059
3060 hdw->cmd_debug_state = 0;
3061 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003062 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003063 }
3064 return status;
3065}
3066
3067
3068int pvr2_send_request(struct pvr2_hdw *hdw,
3069 void *write_data,unsigned int write_len,
3070 void *read_data,unsigned int read_len)
3071{
3072 return pvr2_send_request_ex(hdw,HZ*4,0,
3073 write_data,write_len,
3074 read_data,read_len);
3075}
3076
3077int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3078{
3079 int ret;
3080
3081 LOCK_TAKE(hdw->ctl_lock);
3082
Michael Krufky8d364362007-01-22 02:17:55 -03003083 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003084 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3085 hdw->cmd_buffer[5] = 0;
3086 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3087 hdw->cmd_buffer[7] = reg & 0xff;
3088
3089
3090 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
3091
3092 LOCK_GIVE(hdw->ctl_lock);
3093
3094 return ret;
3095}
3096
3097
Adrian Bunk07e337e2006-06-30 11:30:20 -03003098static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03003099{
3100 int ret = 0;
3101
3102 LOCK_TAKE(hdw->ctl_lock);
3103
Michael Krufky8d364362007-01-22 02:17:55 -03003104 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003105 hdw->cmd_buffer[1] = 0;
3106 hdw->cmd_buffer[2] = 0;
3107 hdw->cmd_buffer[3] = 0;
3108 hdw->cmd_buffer[4] = 0;
3109 hdw->cmd_buffer[5] = 0;
3110 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3111 hdw->cmd_buffer[7] = reg & 0xff;
3112
3113 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
3114 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
3115
3116 LOCK_GIVE(hdw->ctl_lock);
3117
3118 return ret;
3119}
3120
3121
Mike Isely681c7392007-11-26 01:48:52 -03003122void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003123{
3124 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03003125 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3126 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03003127 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003128 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003129 }
Mike Isely681c7392007-11-26 01:48:52 -03003130 hdw->flag_ok = 0;
3131 trace_stbit("flag_ok",hdw->flag_ok);
3132 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003133}
3134
3135
3136void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
3137{
3138 int ret;
3139 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003140 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03003141 if (ret == 1) {
3142 ret = usb_reset_device(hdw->usb_dev);
3143 usb_unlock_device(hdw->usb_dev);
3144 } else {
3145 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3146 "Failed to lock USB device ret=%d",ret);
3147 }
3148 if (init_pause_msec) {
3149 pvr2_trace(PVR2_TRACE_INFO,
3150 "Waiting %u msec for hardware to settle",
3151 init_pause_msec);
3152 msleep(init_pause_msec);
3153 }
3154
3155}
3156
3157
3158void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
3159{
3160 char da[1];
3161 unsigned int pipe;
3162 int ret;
3163
3164 if (!hdw->usb_dev) return;
3165
3166 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
3167
3168 da[0] = val ? 0x01 : 0x00;
3169
3170 /* Write the CPUCS register on the 8051. The lsb of the register
3171 is the reset bit; a 1 asserts reset while a 0 clears it. */
3172 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
3173 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
3174 if (ret < 0) {
3175 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3176 "cpureset_assert(%d) error=%d",val,ret);
3177 pvr2_hdw_render_useless(hdw);
3178 }
3179}
3180
3181
3182int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
3183{
3184 int status;
3185 LOCK_TAKE(hdw->ctl_lock); do {
3186 pvr2_trace(PVR2_TRACE_INIT,"Requesting uproc hard reset");
Michael Krufky8d364362007-01-22 02:17:55 -03003187 hdw->cmd_buffer[0] = FX2CMD_DEEP_RESET;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003188 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003189 } while (0); LOCK_GIVE(hdw->ctl_lock);
3190 return status;
3191}
3192
3193
3194int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
3195{
3196 int status;
3197 LOCK_TAKE(hdw->ctl_lock); do {
3198 pvr2_trace(PVR2_TRACE_INIT,"Requesting powerup");
Michael Krufky8d364362007-01-22 02:17:55 -03003199 hdw->cmd_buffer[0] = FX2CMD_POWER_ON;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003200 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003201 } while (0); LOCK_GIVE(hdw->ctl_lock);
3202 return status;
3203}
3204
3205
3206int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
3207{
3208 if (!hdw->decoder_ctrl) {
3209 pvr2_trace(PVR2_TRACE_INIT,
3210 "Unable to reset decoder: nothing attached");
3211 return -ENOTTY;
3212 }
3213
3214 if (!hdw->decoder_ctrl->force_reset) {
3215 pvr2_trace(PVR2_TRACE_INIT,
3216 "Unable to reset decoder: not implemented");
3217 return -ENOTTY;
3218 }
3219
3220 pvr2_trace(PVR2_TRACE_INIT,
3221 "Requesting decoder reset");
3222 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
3223 return 0;
3224}
3225
3226
Mike Iselye61b6fc2006-07-18 22:42:18 -03003227/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003228static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03003229{
3230 int status;
3231 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003232 hdw->cmd_buffer[0] =
3233 (runFl ? FX2CMD_STREAMING_ON : FX2CMD_STREAMING_OFF);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003234 status = pvr2_send_request(hdw,hdw->cmd_buffer,1,NULL,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003235 } while (0); LOCK_GIVE(hdw->ctl_lock);
Mike Iselyd8554972006-06-26 20:58:46 -03003236 return status;
3237}
3238
3239
Mike Isely681c7392007-11-26 01:48:52 -03003240/* Evaluate whether or not state_encoder_ok can change */
3241static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
3242{
3243 if (hdw->state_encoder_ok) return 0;
3244 if (hdw->flag_tripped) return 0;
3245 if (hdw->state_encoder_run) return 0;
3246 if (hdw->state_encoder_config) return 0;
3247 if (hdw->state_decoder_run) return 0;
3248 if (hdw->state_usbstream_run) return 0;
3249 if (pvr2_upload_firmware2(hdw) < 0) {
3250 hdw->flag_tripped = !0;
3251 trace_stbit("flag_tripped",hdw->flag_tripped);
3252 return !0;
3253 }
3254 hdw->state_encoder_ok = !0;
3255 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
3256 return !0;
3257}
3258
3259
3260/* Evaluate whether or not state_encoder_config can change */
3261static int state_eval_encoder_config(struct pvr2_hdw *hdw)
3262{
3263 if (hdw->state_encoder_config) {
3264 if (hdw->state_encoder_ok) {
3265 if (hdw->state_pipeline_req &&
3266 !hdw->state_pipeline_pause) return 0;
3267 }
3268 hdw->state_encoder_config = 0;
3269 hdw->state_encoder_waitok = 0;
3270 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3271 /* paranoia - solve race if timer just completed */
3272 del_timer_sync(&hdw->encoder_wait_timer);
3273 } else {
3274 if (!hdw->state_encoder_ok ||
3275 !hdw->state_pipeline_idle ||
3276 hdw->state_pipeline_pause ||
3277 !hdw->state_pipeline_req ||
3278 !hdw->state_pipeline_config) {
3279 /* We must reset the enforced wait interval if
3280 anything has happened that might have disturbed
3281 the encoder. This should be a rare case. */
3282 if (timer_pending(&hdw->encoder_wait_timer)) {
3283 del_timer_sync(&hdw->encoder_wait_timer);
3284 }
3285 if (hdw->state_encoder_waitok) {
3286 /* Must clear the state - therefore we did
3287 something to a state bit and must also
3288 return true. */
3289 hdw->state_encoder_waitok = 0;
3290 trace_stbit("state_encoder_waitok",
3291 hdw->state_encoder_waitok);
3292 return !0;
3293 }
3294 return 0;
3295 }
3296 if (!hdw->state_encoder_waitok) {
3297 if (!timer_pending(&hdw->encoder_wait_timer)) {
3298 /* waitok flag wasn't set and timer isn't
3299 running. Check flag once more to avoid
3300 a race then start the timer. This is
3301 the point when we measure out a minimal
3302 quiet interval before doing something to
3303 the encoder. */
3304 if (!hdw->state_encoder_waitok) {
3305 hdw->encoder_wait_timer.expires =
3306 jiffies + (HZ*50/1000);
3307 add_timer(&hdw->encoder_wait_timer);
3308 }
3309 }
3310 /* We can't continue until we know we have been
3311 quiet for the interval measured by this
3312 timer. */
3313 return 0;
3314 }
3315 pvr2_encoder_configure(hdw);
3316 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
3317 }
3318 trace_stbit("state_encoder_config",hdw->state_encoder_config);
3319 return !0;
3320}
3321
3322
3323/* Evaluate whether or not state_encoder_run can change */
3324static int state_eval_encoder_run(struct pvr2_hdw *hdw)
3325{
3326 if (hdw->state_encoder_run) {
3327 if (hdw->state_encoder_ok) {
3328 if (hdw->state_decoder_run) return 0;
3329 if (pvr2_encoder_stop(hdw) < 0) return !0;
3330 }
3331 hdw->state_encoder_run = 0;
3332 } else {
3333 if (!hdw->state_encoder_ok) return 0;
3334 if (!hdw->state_decoder_run) return 0;
3335 if (pvr2_encoder_start(hdw) < 0) return !0;
3336 hdw->state_encoder_run = !0;
3337 }
3338 trace_stbit("state_encoder_run",hdw->state_encoder_run);
3339 return !0;
3340}
3341
3342
3343/* Timeout function for quiescent timer. */
3344static void pvr2_hdw_quiescent_timeout(unsigned long data)
3345{
3346 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3347 hdw->state_decoder_quiescent = !0;
3348 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3349 hdw->state_stale = !0;
3350 queue_work(hdw->workqueue,&hdw->workpoll);
3351}
3352
3353
3354/* Timeout function for encoder wait timer. */
3355static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
3356{
3357 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3358 hdw->state_encoder_waitok = !0;
3359 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
3360 hdw->state_stale = !0;
3361 queue_work(hdw->workqueue,&hdw->workpoll);
3362}
3363
3364
3365/* Evaluate whether or not state_decoder_run can change */
3366static int state_eval_decoder_run(struct pvr2_hdw *hdw)
3367{
3368 if (hdw->state_decoder_run) {
3369 if (hdw->state_encoder_ok) {
3370 if (hdw->state_pipeline_req &&
3371 !hdw->state_pipeline_pause) return 0;
3372 }
3373 if (!hdw->flag_decoder_missed) {
3374 pvr2_decoder_enable(hdw,0);
3375 }
3376 hdw->state_decoder_quiescent = 0;
3377 hdw->state_decoder_run = 0;
3378 /* paranoia - solve race if timer just completed */
3379 del_timer_sync(&hdw->quiescent_timer);
3380 } else {
3381 if (!hdw->state_decoder_quiescent) {
3382 if (!timer_pending(&hdw->quiescent_timer)) {
3383 /* We don't do something about the
3384 quiescent timer until right here because
3385 we also want to catch cases where the
3386 decoder was already not running (like
3387 after initialization) as opposed to
3388 knowing that we had just stopped it.
3389 The second flag check is here to cover a
3390 race - the timer could have run and set
3391 this flag just after the previous check
3392 but before we did the pending check. */
3393 if (!hdw->state_decoder_quiescent) {
3394 hdw->quiescent_timer.expires =
3395 jiffies + (HZ*50/1000);
3396 add_timer(&hdw->quiescent_timer);
3397 }
3398 }
3399 /* Don't allow decoder to start again until it has
3400 been quiesced first. This little detail should
3401 hopefully further stabilize the encoder. */
3402 return 0;
3403 }
3404 if (!hdw->state_pipeline_req ||
3405 hdw->state_pipeline_pause ||
3406 !hdw->state_pipeline_config ||
3407 !hdw->state_encoder_config ||
3408 !hdw->state_encoder_ok) return 0;
3409 del_timer_sync(&hdw->quiescent_timer);
3410 if (hdw->flag_decoder_missed) return 0;
3411 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
3412 hdw->state_decoder_quiescent = 0;
3413 hdw->state_decoder_run = !0;
3414 }
3415 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
3416 trace_stbit("state_decoder_run",hdw->state_decoder_run);
3417 return !0;
3418}
3419
3420
3421/* Evaluate whether or not state_usbstream_run can change */
3422static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
3423{
3424 if (hdw->state_usbstream_run) {
3425 if (hdw->state_encoder_ok) {
3426 if (hdw->state_encoder_run) return 0;
3427 }
3428 pvr2_hdw_cmd_usbstream(hdw,0);
3429 hdw->state_usbstream_run = 0;
3430 } else {
3431 if (!hdw->state_encoder_ok ||
3432 !hdw->state_encoder_run ||
3433 !hdw->state_pipeline_req ||
3434 hdw->state_pipeline_pause) return 0;
3435 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
3436 hdw->state_usbstream_run = !0;
3437 }
3438 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
3439 return !0;
3440}
3441
3442
3443/* Attempt to configure pipeline, if needed */
3444static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
3445{
3446 if (hdw->state_pipeline_config ||
3447 hdw->state_pipeline_pause) return 0;
3448 pvr2_hdw_commit_execute(hdw);
3449 return !0;
3450}
3451
3452
3453/* Update pipeline idle and pipeline pause tracking states based on other
3454 inputs. This must be called whenever the other relevant inputs have
3455 changed. */
3456static int state_update_pipeline_state(struct pvr2_hdw *hdw)
3457{
3458 unsigned int st;
3459 int updatedFl = 0;
3460 /* Update pipeline state */
3461 st = !(hdw->state_encoder_run ||
3462 hdw->state_decoder_run ||
3463 hdw->state_usbstream_run ||
3464 (!hdw->state_decoder_quiescent));
3465 if (!st != !hdw->state_pipeline_idle) {
3466 hdw->state_pipeline_idle = st;
3467 updatedFl = !0;
3468 }
3469 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
3470 hdw->state_pipeline_pause = 0;
3471 updatedFl = !0;
3472 }
3473 return updatedFl;
3474}
3475
3476
3477typedef int (*state_eval_func)(struct pvr2_hdw *);
3478
3479/* Set of functions to be run to evaluate various states in the driver. */
3480const static state_eval_func eval_funcs[] = {
3481 state_eval_pipeline_config,
3482 state_eval_encoder_ok,
3483 state_eval_encoder_config,
3484 state_eval_decoder_run,
3485 state_eval_encoder_run,
3486 state_eval_usbstream_run,
3487};
3488
3489
3490/* Process various states and return true if we did anything interesting. */
3491static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
3492{
3493 unsigned int i;
3494 int state_updated = 0;
3495 int check_flag;
3496
3497 if (!hdw->state_stale) return 0;
3498 if ((hdw->fw1_state != FW1_STATE_OK) ||
3499 !hdw->flag_ok) {
3500 hdw->state_stale = 0;
3501 return !0;
3502 }
3503 /* This loop is the heart of the entire driver. It keeps trying to
3504 evaluate various bits of driver state until nothing changes for
3505 one full iteration. Each "bit of state" tracks some global
3506 aspect of the driver, e.g. whether decoder should run, if
3507 pipeline is configured, usb streaming is on, etc. We separately
3508 evaluate each of those questions based on other driver state to
3509 arrive at the correct running configuration. */
3510 do {
3511 check_flag = 0;
3512 state_update_pipeline_state(hdw);
3513 /* Iterate over each bit of state */
3514 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
3515 if ((*eval_funcs[i])(hdw)) {
3516 check_flag = !0;
3517 state_updated = !0;
3518 state_update_pipeline_state(hdw);
3519 }
3520 }
3521 } while (check_flag && hdw->flag_ok);
3522 hdw->state_stale = 0;
3523 trace_stbit("state_stale",hdw->state_stale);
3524 return state_updated;
3525}
3526
3527
3528static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
3529 char *buf,unsigned int acnt)
3530{
3531 switch (which) {
3532 case 0:
3533 return scnprintf(
3534 buf,acnt,
3535 "driver:%s%s%s%s%s",
3536 (hdw->flag_ok ? " <ok>" : " <fail>"),
3537 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
3538 (hdw->flag_disconnected ? " <disconnected>" :
3539 " <connected>"),
3540 (hdw->flag_tripped ? " <tripped>" : ""),
3541 (hdw->flag_decoder_missed ? " <no decoder>" : ""));
3542 case 1:
3543 return scnprintf(
3544 buf,acnt,
3545 "pipeline:%s%s%s%s",
3546 (hdw->state_pipeline_idle ? " <idle>" : ""),
3547 (hdw->state_pipeline_config ?
3548 " <configok>" : " <stale>"),
3549 (hdw->state_pipeline_req ? " <req>" : ""),
3550 (hdw->state_pipeline_pause ? " <pause>" : ""));
3551 case 2:
3552 return scnprintf(
3553 buf,acnt,
3554 "worker:%s%s%s%s%s%s",
3555 (hdw->state_decoder_run ?
3556 " <decode:run>" :
3557 (hdw->state_decoder_quiescent ?
3558 "" : " <decode:stop>")),
3559 (hdw->state_decoder_quiescent ?
3560 " <decode:quiescent>" : ""),
3561 (hdw->state_encoder_ok ?
3562 "" : " <encode:init>"),
3563 (hdw->state_encoder_run ?
3564 " <encode:run>" : " <encode:stop>"),
3565 (hdw->state_encoder_config ?
3566 " <encode:configok>" :
3567 (hdw->state_encoder_waitok ?
3568 "" : " <encode:wait>")),
3569 (hdw->state_usbstream_run ?
3570 " <usb:run>" : " <usb:stop>"));
3571 break;
3572 case 3:
3573 return scnprintf(
3574 buf,acnt,
3575 "state: %s",
3576 pvr2_get_state_name(hdw->master_state));
3577 break;
3578 default: break;
3579 }
3580 return 0;
3581}
3582
3583
3584unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
3585 char *buf,unsigned int acnt)
3586{
3587 unsigned int bcnt,ccnt,idx;
3588 bcnt = 0;
3589 LOCK_TAKE(hdw->big_lock);
3590 for (idx = 0; ; idx++) {
3591 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
3592 if (!ccnt) break;
3593 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3594 if (!acnt) break;
3595 buf[0] = '\n'; ccnt = 1;
3596 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
3597 }
3598 LOCK_GIVE(hdw->big_lock);
3599 return bcnt;
3600}
3601
3602
3603static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
3604{
3605 char buf[128];
3606 unsigned int idx,ccnt;
3607
3608 for (idx = 0; ; idx++) {
3609 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
3610 if (!ccnt) break;
3611 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
3612 }
3613}
3614
3615
3616/* Evaluate and update the driver's current state, taking various actions
3617 as appropriate for the update. */
3618static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
3619{
3620 unsigned int st;
3621 int state_updated = 0;
3622 int callback_flag = 0;
3623
3624 pvr2_trace(PVR2_TRACE_STBITS,
3625 "Drive state check START");
3626 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3627 pvr2_hdw_state_log_state(hdw);
3628 }
3629
3630 /* Process all state and get back over disposition */
3631 state_updated = pvr2_hdw_state_update(hdw);
3632
3633 /* Update master state based upon all other states. */
3634 if (!hdw->flag_ok) {
3635 st = PVR2_STATE_DEAD;
3636 } else if (hdw->fw1_state != FW1_STATE_OK) {
3637 st = PVR2_STATE_COLD;
3638 } else if (!hdw->state_encoder_ok) {
3639 st = PVR2_STATE_WARM;
3640 } else if (hdw->flag_tripped || hdw->flag_decoder_missed) {
3641 st = PVR2_STATE_ERROR;
3642 } else if (hdw->state_encoder_run &&
3643 hdw->state_decoder_run &&
3644 hdw->state_usbstream_run) {
3645 st = PVR2_STATE_RUN;
3646 } else {
3647 st = PVR2_STATE_READY;
3648 }
3649 if (hdw->master_state != st) {
3650 pvr2_trace(PVR2_TRACE_STATE,
3651 "Device state change from %s to %s",
3652 pvr2_get_state_name(hdw->master_state),
3653 pvr2_get_state_name(st));
3654 hdw->master_state = st;
3655 state_updated = !0;
3656 callback_flag = !0;
3657 }
3658 if (state_updated) {
3659 /* Trigger anyone waiting on any state changes here. */
3660 wake_up(&hdw->state_wait_data);
3661 }
3662
3663 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
3664 pvr2_hdw_state_log_state(hdw);
3665 }
3666 pvr2_trace(PVR2_TRACE_STBITS,
3667 "Drive state check DONE callback=%d",callback_flag);
3668
3669 return callback_flag;
3670}
3671
3672
3673/* Cause kernel thread to check / update driver state */
3674static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
3675{
3676 if (hdw->state_stale) return;
3677 hdw->state_stale = !0;
3678 trace_stbit("state_stale",hdw->state_stale);
3679 queue_work(hdw->workqueue,&hdw->workpoll);
3680}
3681
3682
3683void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
3684 struct pvr2_hdw_debug_info *ptr)
Mike Iselyd8554972006-06-26 20:58:46 -03003685{
3686 ptr->big_lock_held = hdw->big_lock_held;
3687 ptr->ctl_lock_held = hdw->ctl_lock_held;
Mike Iselyd8554972006-06-26 20:58:46 -03003688 ptr->flag_disconnected = hdw->flag_disconnected;
3689 ptr->flag_init_ok = hdw->flag_init_ok;
Mike Isely681c7392007-11-26 01:48:52 -03003690 ptr->flag_ok = hdw->flag_ok;
3691 ptr->fw1_state = hdw->fw1_state;
3692 ptr->flag_decoder_missed = hdw->flag_decoder_missed;
3693 ptr->flag_tripped = hdw->flag_tripped;
3694 ptr->state_encoder_ok = hdw->state_encoder_ok;
3695 ptr->state_encoder_run = hdw->state_encoder_run;
3696 ptr->state_decoder_run = hdw->state_decoder_run;
3697 ptr->state_usbstream_run = hdw->state_usbstream_run;
3698 ptr->state_decoder_quiescent = hdw->state_decoder_quiescent;
3699 ptr->state_pipeline_config = hdw->state_pipeline_config;
3700 ptr->state_pipeline_req = hdw->state_pipeline_req;
3701 ptr->state_pipeline_pause = hdw->state_pipeline_pause;
3702 ptr->state_pipeline_idle = hdw->state_pipeline_idle;
Mike Iselyd8554972006-06-26 20:58:46 -03003703 ptr->cmd_debug_state = hdw->cmd_debug_state;
3704 ptr->cmd_code = hdw->cmd_debug_code;
3705 ptr->cmd_debug_write_len = hdw->cmd_debug_write_len;
3706 ptr->cmd_debug_read_len = hdw->cmd_debug_read_len;
3707 ptr->cmd_debug_timeout = hdw->ctl_timeout_flag;
3708 ptr->cmd_debug_write_pend = hdw->ctl_write_pend_flag;
3709 ptr->cmd_debug_read_pend = hdw->ctl_read_pend_flag;
3710 ptr->cmd_debug_rstatus = hdw->ctl_read_urb->status;
3711 ptr->cmd_debug_wstatus = hdw->ctl_read_urb->status;
3712}
3713
3714
Mike Isely681c7392007-11-26 01:48:52 -03003715void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
3716 struct pvr2_hdw_debug_info *ptr)
3717{
3718 LOCK_TAKE(hdw->ctl_lock); do {
3719 pvr2_hdw_get_debug_info_unlocked(hdw,ptr);
3720 } while(0); LOCK_GIVE(hdw->ctl_lock);
3721}
3722
3723
Mike Iselyd8554972006-06-26 20:58:46 -03003724int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
3725{
3726 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
3727}
3728
3729
3730int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
3731{
3732 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
3733}
3734
3735
3736int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
3737{
3738 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
3739}
3740
3741
3742int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
3743{
3744 u32 cval,nval;
3745 int ret;
3746 if (~msk) {
3747 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
3748 if (ret) return ret;
3749 nval = (cval & ~msk) | (val & msk);
3750 pvr2_trace(PVR2_TRACE_GPIO,
3751 "GPIO direction changing 0x%x:0x%x"
3752 " from 0x%x to 0x%x",
3753 msk,val,cval,nval);
3754 } else {
3755 nval = val;
3756 pvr2_trace(PVR2_TRACE_GPIO,
3757 "GPIO direction changing to 0x%x",nval);
3758 }
3759 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
3760}
3761
3762
3763int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
3764{
3765 u32 cval,nval;
3766 int ret;
3767 if (~msk) {
3768 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
3769 if (ret) return ret;
3770 nval = (cval & ~msk) | (val & msk);
3771 pvr2_trace(PVR2_TRACE_GPIO,
3772 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
3773 msk,val,cval,nval);
3774 } else {
3775 nval = val;
3776 pvr2_trace(PVR2_TRACE_GPIO,
3777 "GPIO output changing to 0x%x",nval);
3778 }
3779 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
3780}
3781
3782
Mike Iselye61b6fc2006-07-18 22:42:18 -03003783/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003784static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003785{
3786 int result;
3787 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003788 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03003789 result = pvr2_send_request(hdw,
3790 hdw->cmd_buffer,1,
3791 hdw->cmd_buffer,1);
3792 if (result < 0) break;
3793 result = hdw->cmd_buffer[0];
3794 } while(0); LOCK_GIVE(hdw->ctl_lock);
3795 return result;
3796}
3797
3798
Mike Isely32ffa9a2006-09-23 22:26:52 -03003799int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilf3d092b2007-02-23 20:55:14 -03003800 u32 match_type, u32 match_chip, u64 reg_id,
3801 int setFl,u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03003802{
3803#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03003804 struct pvr2_i2c_client *cp;
3805 struct v4l2_register req;
Mike Isely6d988162006-09-28 17:53:49 -03003806 int stat = 0;
3807 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03003808
Mike Isely201f5c92007-01-28 16:08:36 -03003809 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
3810
Hans Verkuilf3d092b2007-02-23 20:55:14 -03003811 req.match_type = match_type;
3812 req.match_chip = match_chip;
Mike Isely32ffa9a2006-09-23 22:26:52 -03003813 req.reg = reg_id;
3814 if (setFl) req.val = *val_ptr;
3815 mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03003816 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03003817 if (!v4l2_chip_match_i2c_client(
3818 cp->client,
3819 req.match_type, req.match_chip)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03003820 continue;
3821 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03003822 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03003823 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
3824 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03003825 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03003826 okFl = !0;
3827 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03003828 }
3829 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03003830 if (okFl) {
3831 return stat;
3832 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03003833 return -EINVAL;
3834#else
3835 return -ENOSYS;
3836#endif
3837}
3838
3839
Mike Iselyd8554972006-06-26 20:58:46 -03003840/*
3841 Stuff for Emacs to see, in order to encourage consistent editing style:
3842 *** Local Variables: ***
3843 *** mode: c ***
3844 *** fill-column: 75 ***
3845 *** tab-width: 8 ***
3846 *** c-basic-offset: 8 ***
3847 *** End: ***
3848 */