blob: bd46b40eefdc768dfd1f359832b27ce0d5cf3cf4 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
Mike Iselyd8554972006-06-26 20:58:46 -03003 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/errno.h>
22#include <linux/string.h>
23#include <linux/slab.h>
24#include <linux/firmware.h>
Mike Iselyd8554972006-06-26 20:58:46 -030025#include <linux/videodev2.h>
Mike Isely32ffa9a2006-09-23 22:26:52 -030026#include <media/v4l2-common.h>
Mike Isely75212a02009-03-07 01:48:42 -030027#include <media/tuner.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"
Mike Isely59af3362009-03-07 03:06:09 -030033#include "pvrusb2-i2c-track.h"
Mike Iselyd8554972006-06-26 20:58:46 -030034#include "pvrusb2-tuner.h"
35#include "pvrusb2-eeprom.h"
36#include "pvrusb2-hdw-internal.h"
37#include "pvrusb2-encoder.h"
38#include "pvrusb2-debug.h"
Michael Krufky8d364362007-01-22 02:17:55 -030039#include "pvrusb2-fx2-cmd.h"
Mike Isely5f6dae82009-03-07 00:39:34 -030040#include "pvrusb2-wm8775.h"
Mike Isely6f956512009-03-07 00:43:26 -030041#include "pvrusb2-video-v4l.h"
Mike Isely634ba262009-03-07 00:54:02 -030042#include "pvrusb2-cx2584x-v4l.h"
Mike Isely76891d62009-03-07 00:52:06 -030043#include "pvrusb2-audio.h"
Mike Iselyd8554972006-06-26 20:58:46 -030044
Mike Isely1bde0282006-12-27 23:30:13 -030045#define TV_MIN_FREQ 55250000L
46#define TV_MAX_FREQ 850000000L
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -030047
Mike Isely83ce57a2008-05-26 05:51:57 -030048/* This defines a minimum interval that the decoder must remain quiet
49 before we are allowed to start it running. */
50#define TIME_MSEC_DECODER_WAIT 50
51
52/* This defines a minimum interval that the encoder must remain quiet
Mike Iselyfa98e592008-05-26 05:54:24 -030053 before we are allowed to configure it. I had this originally set to
54 50msec, but Martin Dauskardt <martin.dauskardt@gmx.de> reports that
55 things work better when it's set to 100msec. */
56#define TIME_MSEC_ENCODER_WAIT 100
Mike Isely83ce57a2008-05-26 05:51:57 -030057
58/* This defines the minimum interval that the encoder must successfully run
59 before we consider that the encoder has run at least once since its
60 firmware has been loaded. This measurement is in important for cases
61 where we can't do something until we know that the encoder has been run
62 at least once. */
63#define TIME_MSEC_ENCODER_OK 250
64
Mike Iselya0fd1cb2006-06-30 11:35:28 -030065static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -030066static DEFINE_MUTEX(pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -030067
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030068static int ctlchg;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030069static int procreload;
Mike Iselyd8554972006-06-26 20:58:46 -030070static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
71static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
72static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030073static int init_pause_msec;
Mike Iselyd8554972006-06-26 20:58:46 -030074
75module_param(ctlchg, int, S_IRUGO|S_IWUSR);
76MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
77module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
Mike Iselyd8554972006-06-26 20:58:46 -030079module_param(procreload, int, S_IRUGO|S_IWUSR);
80MODULE_PARM_DESC(procreload,
81 "Attempt init failure recovery with firmware reload");
82module_param_array(tuner, int, NULL, 0444);
83MODULE_PARM_DESC(tuner,"specify installed tuner type");
84module_param_array(video_std, int, NULL, 0444);
85MODULE_PARM_DESC(video_std,"specify initial video standard");
86module_param_array(tolerance, int, NULL, 0444);
87MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
88
Michael Krufky5a4f5da62008-05-11 16:37:50 -030089/* US Broadcast channel 7 (175.25 MHz) */
90static int default_tv_freq = 175250000L;
91/* 104.3 MHz, a usable FM station for my area */
92static int default_radio_freq = 104300000L;
93
94module_param_named(tv_freq, default_tv_freq, int, 0444);
95MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
96module_param_named(radio_freq, default_radio_freq, int, 0444);
97MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
98
Mike Iselyd8554972006-06-26 20:58:46 -030099#define PVR2_CTL_WRITE_ENDPOINT 0x01
100#define PVR2_CTL_READ_ENDPOINT 0x81
101
102#define PVR2_GPIO_IN 0x9008
103#define PVR2_GPIO_OUT 0x900c
104#define PVR2_GPIO_DIR 0x9020
105
106#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
107
108#define PVR2_FIRMWARE_ENDPOINT 0x02
109
110/* size of a firmware chunk */
111#define FIRMWARE_CHUNK_SIZE 0x2000
112
Mike Iselyedb9dcb2009-03-07 00:37:10 -0300113typedef void (*pvr2_subdev_update_func)(struct pvr2_hdw *,
114 struct v4l2_subdev *);
115
116static const pvr2_subdev_update_func pvr2_module_update_functions[] = {
Mike Isely4ecbc282009-03-07 00:49:19 -0300117 [PVR2_CLIENT_ID_WM8775] = pvr2_wm8775_subdev_update,
Mike Isely6f956512009-03-07 00:43:26 -0300118 [PVR2_CLIENT_ID_SAA7115] = pvr2_saa7115_subdev_update,
Mike Isely76891d62009-03-07 00:52:06 -0300119 [PVR2_CLIENT_ID_MSP3400] = pvr2_msp3400_subdev_update,
Mike Isely634ba262009-03-07 00:54:02 -0300120 [PVR2_CLIENT_ID_CX25840] = pvr2_cx25840_subdev_update,
Mike Iselyedb9dcb2009-03-07 00:37:10 -0300121};
122
Mike Iselye9c64a72009-03-06 23:42:20 -0300123static const char *module_names[] = {
124 [PVR2_CLIENT_ID_MSP3400] = "msp3400",
125 [PVR2_CLIENT_ID_CX25840] = "cx25840",
126 [PVR2_CLIENT_ID_SAA7115] = "saa7115",
127 [PVR2_CLIENT_ID_TUNER] = "tuner",
128 [PVR2_CLIENT_ID_CS53132A] = "cs53132a",
Mike Isely5f6dae82009-03-07 00:39:34 -0300129 [PVR2_CLIENT_ID_WM8775] = "wm8775",
Mike Iselye9c64a72009-03-06 23:42:20 -0300130};
131
132
133static const unsigned char *module_i2c_addresses[] = {
134 [PVR2_CLIENT_ID_TUNER] = "\x60\x61\x62\x63",
Mike Iselyae111f72009-03-07 00:57:42 -0300135 [PVR2_CLIENT_ID_WM8775] = "\x1b",
Mike Isely0b467012009-03-07 01:49:37 -0300136 [PVR2_CLIENT_ID_CX25840] = "\x44",
Mike Iselye9c64a72009-03-06 23:42:20 -0300137};
138
139
Mike Iselyb30d2442006-06-25 20:05:01 -0300140/* Define the list of additional controls we'll dynamically construct based
141 on query of the cx2341x module. */
142struct pvr2_mpeg_ids {
143 const char *strid;
144 int id;
145};
146static const struct pvr2_mpeg_ids mpeg_ids[] = {
147 {
148 .strid = "audio_layer",
149 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
150 },{
151 .strid = "audio_bitrate",
152 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
153 },{
154 /* Already using audio_mode elsewhere :-( */
155 .strid = "mpeg_audio_mode",
156 .id = V4L2_CID_MPEG_AUDIO_MODE,
157 },{
158 .strid = "mpeg_audio_mode_extension",
159 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
160 },{
161 .strid = "audio_emphasis",
162 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
163 },{
164 .strid = "audio_crc",
165 .id = V4L2_CID_MPEG_AUDIO_CRC,
166 },{
167 .strid = "video_aspect",
168 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
169 },{
170 .strid = "video_b_frames",
171 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
172 },{
173 .strid = "video_gop_size",
174 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
175 },{
176 .strid = "video_gop_closure",
177 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
178 },{
Mike Iselyb30d2442006-06-25 20:05:01 -0300179 .strid = "video_bitrate_mode",
180 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
181 },{
182 .strid = "video_bitrate",
183 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
184 },{
185 .strid = "video_bitrate_peak",
186 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
187 },{
188 .strid = "video_temporal_decimation",
189 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
190 },{
191 .strid = "stream_type",
192 .id = V4L2_CID_MPEG_STREAM_TYPE,
193 },{
194 .strid = "video_spatial_filter_mode",
195 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
196 },{
197 .strid = "video_spatial_filter",
198 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
199 },{
200 .strid = "video_luma_spatial_filter_type",
201 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
202 },{
203 .strid = "video_chroma_spatial_filter_type",
204 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
205 },{
206 .strid = "video_temporal_filter_mode",
207 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
208 },{
209 .strid = "video_temporal_filter",
210 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
211 },{
212 .strid = "video_median_filter_type",
213 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
214 },{
215 .strid = "video_luma_median_filter_top",
216 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
217 },{
218 .strid = "video_luma_median_filter_bottom",
219 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
220 },{
221 .strid = "video_chroma_median_filter_top",
222 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
223 },{
224 .strid = "video_chroma_median_filter_bottom",
225 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
226 }
227};
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300228#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
Mike Iselyc05c0462006-06-25 20:04:25 -0300229
Mike Iselyd8554972006-06-26 20:58:46 -0300230
Mike Isely434449f2006-08-08 09:10:06 -0300231static const char *control_values_srate[] = {
232 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
233 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
234 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
235};
Mike Iselyd8554972006-06-26 20:58:46 -0300236
Mike Iselyd8554972006-06-26 20:58:46 -0300237
238
239static const char *control_values_input[] = {
240 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
Mike Isely29bf5b12008-04-22 14:45:37 -0300241 [PVR2_CVAL_INPUT_DTV] = "dtv",
Mike Iselyd8554972006-06-26 20:58:46 -0300242 [PVR2_CVAL_INPUT_RADIO] = "radio",
243 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
244 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
245};
246
247
248static const char *control_values_audiomode[] = {
249 [V4L2_TUNER_MODE_MONO] = "Mono",
250 [V4L2_TUNER_MODE_STEREO] = "Stereo",
251 [V4L2_TUNER_MODE_LANG1] = "Lang1",
252 [V4L2_TUNER_MODE_LANG2] = "Lang2",
253 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
254};
255
256
257static const char *control_values_hsm[] = {
258 [PVR2_CVAL_HSM_FAIL] = "Fail",
259 [PVR2_CVAL_HSM_HIGH] = "High",
260 [PVR2_CVAL_HSM_FULL] = "Full",
261};
262
263
Mike Isely681c7392007-11-26 01:48:52 -0300264static const char *pvr2_state_names[] = {
265 [PVR2_STATE_NONE] = "none",
266 [PVR2_STATE_DEAD] = "dead",
267 [PVR2_STATE_COLD] = "cold",
268 [PVR2_STATE_WARM] = "warm",
269 [PVR2_STATE_ERROR] = "error",
270 [PVR2_STATE_READY] = "ready",
271 [PVR2_STATE_RUN] = "run",
Mike Iselyd8554972006-06-26 20:58:46 -0300272};
273
Mike Isely681c7392007-11-26 01:48:52 -0300274
Mike Isely694dca2b2008-03-28 05:42:10 -0300275struct pvr2_fx2cmd_descdef {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300276 unsigned char id;
277 unsigned char *desc;
278};
279
Mike Isely694dca2b2008-03-28 05:42:10 -0300280static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
Mike Isely1c9d10d2008-03-28 05:38:54 -0300281 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
282 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
Mike Isely31335b12008-07-25 19:35:31 -0300283 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
Mike Isely1c9d10d2008-03-28 05:38:54 -0300284 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
285 {FX2CMD_REG_WRITE, "write encoder register"},
286 {FX2CMD_REG_READ, "read encoder register"},
287 {FX2CMD_MEMSEL, "encoder memsel"},
288 {FX2CMD_I2C_WRITE, "i2c write"},
289 {FX2CMD_I2C_READ, "i2c read"},
290 {FX2CMD_GET_USB_SPEED, "get USB speed"},
291 {FX2CMD_STREAMING_ON, "stream on"},
292 {FX2CMD_STREAMING_OFF, "stream off"},
293 {FX2CMD_FWPOST1, "fwpost1"},
294 {FX2CMD_POWER_OFF, "power off"},
295 {FX2CMD_POWER_ON, "power on"},
296 {FX2CMD_DEEP_RESET, "deep reset"},
297 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
298 {FX2CMD_GET_IR_CODE, "get IR code"},
299 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
300 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
301 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
302 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
303 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
304 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
305 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
306};
307
308
Mike Isely1cb03b72008-04-21 03:47:43 -0300309static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
Mike Isely681c7392007-11-26 01:48:52 -0300310static void pvr2_hdw_state_sched(struct pvr2_hdw *);
311static int pvr2_hdw_state_eval(struct pvr2_hdw *);
Mike Isely1bde0282006-12-27 23:30:13 -0300312static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
Mike Isely681c7392007-11-26 01:48:52 -0300313static void pvr2_hdw_worker_i2c(struct work_struct *work);
314static void pvr2_hdw_worker_poll(struct work_struct *work);
Mike Isely681c7392007-11-26 01:48:52 -0300315static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
316static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
317static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300318static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
Mike Isely681c7392007-11-26 01:48:52 -0300319static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300320static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300321static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
322static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300323static void pvr2_hdw_quiescent_timeout(unsigned long);
324static void pvr2_hdw_encoder_wait_timeout(unsigned long);
Mike Iselyd913d632008-04-06 04:04:35 -0300325static void pvr2_hdw_encoder_run_timeout(unsigned long);
Mike Isely1c9d10d2008-03-28 05:38:54 -0300326static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
Adrian Bunk07e337e2006-06-30 11:30:20 -0300327static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
328 unsigned int timeout,int probe_fl,
329 void *write_data,unsigned int write_len,
330 void *read_data,unsigned int read_len);
Mike Isely432907f2008-08-31 21:02:20 -0300331static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300332
Mike Isely681c7392007-11-26 01:48:52 -0300333
334static void trace_stbit(const char *name,int val)
335{
336 pvr2_trace(PVR2_TRACE_STBITS,
337 "State bit %s <-- %s",
338 name,(val ? "true" : "false"));
339}
340
Mike Iselyd8554972006-06-26 20:58:46 -0300341static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
342{
343 struct pvr2_hdw *hdw = cptr->hdw;
344 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
345 *vp = hdw->freqTable[hdw->freqProgSlot-1];
346 } else {
347 *vp = 0;
348 }
349 return 0;
350}
351
352static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
353{
354 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300355 unsigned int slotId = hdw->freqProgSlot;
356 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
357 hdw->freqTable[slotId-1] = v;
358 /* Handle side effects correctly - if we're tuned to this
359 slot, then forgot the slot id relation since the stored
360 frequency has been changed. */
361 if (hdw->freqSelector) {
362 if (hdw->freqSlotRadio == slotId) {
363 hdw->freqSlotRadio = 0;
364 }
365 } else {
366 if (hdw->freqSlotTelevision == slotId) {
367 hdw->freqSlotTelevision = 0;
368 }
369 }
Mike Iselyd8554972006-06-26 20:58:46 -0300370 }
371 return 0;
372}
373
374static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
375{
376 *vp = cptr->hdw->freqProgSlot;
377 return 0;
378}
379
380static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
381{
382 struct pvr2_hdw *hdw = cptr->hdw;
383 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
384 hdw->freqProgSlot = v;
385 }
386 return 0;
387}
388
389static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
390{
Mike Isely1bde0282006-12-27 23:30:13 -0300391 struct pvr2_hdw *hdw = cptr->hdw;
392 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
Mike Iselyd8554972006-06-26 20:58:46 -0300393 return 0;
394}
395
Mike Isely1bde0282006-12-27 23:30:13 -0300396static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
Mike Iselyd8554972006-06-26 20:58:46 -0300397{
398 unsigned freq = 0;
399 struct pvr2_hdw *hdw = cptr->hdw;
Mike Isely1bde0282006-12-27 23:30:13 -0300400 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
401 if (slotId > 0) {
402 freq = hdw->freqTable[slotId-1];
403 if (!freq) return 0;
404 pvr2_hdw_set_cur_freq(hdw,freq);
Mike Iselyd8554972006-06-26 20:58:46 -0300405 }
Mike Isely1bde0282006-12-27 23:30:13 -0300406 if (hdw->freqSelector) {
407 hdw->freqSlotRadio = slotId;
408 } else {
409 hdw->freqSlotTelevision = slotId;
Mike Iselyd8554972006-06-26 20:58:46 -0300410 }
411 return 0;
412}
413
414static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
415{
Mike Isely1bde0282006-12-27 23:30:13 -0300416 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300417 return 0;
418}
419
420static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
421{
422 return cptr->hdw->freqDirty != 0;
423}
424
425static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
426{
427 cptr->hdw->freqDirty = 0;
428}
429
430static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
431{
Mike Isely1bde0282006-12-27 23:30:13 -0300432 pvr2_hdw_set_cur_freq(cptr->hdw,v);
Mike Iselyd8554972006-06-26 20:58:46 -0300433 return 0;
434}
435
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300436static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
437{
Mike Isely432907f2008-08-31 21:02:20 -0300438 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
439 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
440 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300441 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300442 }
Mike Isely432907f2008-08-31 21:02:20 -0300443 *left = cap->bounds.left;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300444 return 0;
445}
446
447static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
448{
Mike Isely432907f2008-08-31 21:02:20 -0300449 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
450 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
451 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300452 return stat;
453 }
454 *left = cap->bounds.left;
455 if (cap->bounds.width > cptr->hdw->cropw_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300456 *left += cap->bounds.width - cptr->hdw->cropw_val;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300457 }
458 return 0;
459}
460
461static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
462{
Mike Isely432907f2008-08-31 21:02:20 -0300463 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
464 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
465 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300466 return stat;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300467 }
Mike Isely432907f2008-08-31 21:02:20 -0300468 *top = cap->bounds.top;
469 return 0;
470}
471
472static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
473{
474 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
475 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
476 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300477 return stat;
478 }
479 *top = cap->bounds.top;
480 if (cap->bounds.height > cptr->hdw->croph_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300481 *top += cap->bounds.height - cptr->hdw->croph_val;
482 }
483 return 0;
484}
485
486static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *val)
487{
488 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
489 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
490 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300491 return stat;
492 }
493 *val = 0;
494 if (cap->bounds.width > cptr->hdw->cropl_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300495 *val = cap->bounds.width - cptr->hdw->cropl_val;
496 }
497 return 0;
498}
499
500static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *val)
501{
502 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
503 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
504 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300505 return stat;
506 }
507 *val = 0;
508 if (cap->bounds.height > cptr->hdw->cropt_val) {
Mike Isely432907f2008-08-31 21:02:20 -0300509 *val = cap->bounds.height - cptr->hdw->cropt_val;
510 }
511 return 0;
512}
513
514static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
515{
516 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
517 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
518 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300519 return stat;
520 }
521 *val = cap->bounds.left;
522 return 0;
523}
524
525static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
526{
527 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
528 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
529 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300530 return stat;
531 }
532 *val = cap->bounds.top;
533 return 0;
534}
535
536static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
537{
538 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
539 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
540 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300541 return stat;
542 }
543 *val = cap->bounds.width;
544 return 0;
545}
546
547static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
548{
549 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
550 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
551 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300552 return stat;
553 }
554 *val = cap->bounds.height;
555 return 0;
556}
557
558static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
559{
560 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
561 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
562 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300563 return stat;
564 }
565 *val = cap->defrect.left;
566 return 0;
567}
568
569static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
570{
571 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
572 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
573 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300574 return stat;
575 }
576 *val = cap->defrect.top;
577 return 0;
578}
579
580static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
581{
582 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
583 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
584 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300585 return stat;
586 }
587 *val = cap->defrect.width;
588 return 0;
589}
590
591static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
592{
593 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
594 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
595 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300596 return stat;
597 }
598 *val = cap->defrect.height;
599 return 0;
600}
601
602static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
603{
604 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
605 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
606 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300607 return stat;
608 }
609 *val = cap->pixelaspect.numerator;
610 return 0;
611}
612
613static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
614{
615 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
616 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
617 if (stat != 0) {
Mike Isely432907f2008-08-31 21:02:20 -0300618 return stat;
619 }
620 *val = cap->pixelaspect.denominator;
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -0300621 return 0;
622}
623
Mike Isely3ad9fc32006-09-02 22:37:52 -0300624static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
625{
626 /* Actual maximum depends on the video standard in effect. */
627 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
628 *vp = 480;
629 } else {
630 *vp = 576;
631 }
632 return 0;
633}
634
635static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
636{
Mike Isely989eb152007-11-26 01:53:12 -0300637 /* Actual minimum depends on device digitizer type. */
638 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
Mike Isely3ad9fc32006-09-02 22:37:52 -0300639 *vp = 75;
640 } else {
641 *vp = 17;
642 }
643 return 0;
644}
645
Mike Isely1bde0282006-12-27 23:30:13 -0300646static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
647{
648 *vp = cptr->hdw->input_val;
649 return 0;
650}
651
Mike Isely29bf5b12008-04-22 14:45:37 -0300652static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
653{
Mike Isely1cb03b72008-04-21 03:47:43 -0300654 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
Mike Isely29bf5b12008-04-22 14:45:37 -0300655}
656
Mike Isely1bde0282006-12-27 23:30:13 -0300657static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
658{
Mike Isely1cb03b72008-04-21 03:47:43 -0300659 return pvr2_hdw_set_input(cptr->hdw,v);
Mike Isely1bde0282006-12-27 23:30:13 -0300660}
661
662static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
663{
664 return cptr->hdw->input_dirty != 0;
665}
666
667static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
668{
669 cptr->hdw->input_dirty = 0;
670}
671
Mike Isely5549f542006-12-27 23:28:54 -0300672
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300673static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
674{
Mike Isely644afdb2007-01-20 00:19:23 -0300675 unsigned long fv;
676 struct pvr2_hdw *hdw = cptr->hdw;
677 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300678 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300679 }
Mike Isely644afdb2007-01-20 00:19:23 -0300680 fv = hdw->tuner_signal_info.rangehigh;
681 if (!fv) {
682 /* Safety fallback */
683 *vp = TV_MAX_FREQ;
684 return 0;
685 }
686 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
687 fv = (fv * 125) / 2;
688 } else {
689 fv = fv * 62500;
690 }
691 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300692 return 0;
693}
694
695static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
696{
Mike Isely644afdb2007-01-20 00:19:23 -0300697 unsigned long fv;
698 struct pvr2_hdw *hdw = cptr->hdw;
699 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -0300700 pvr2_hdw_status_poll(hdw);
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300701 }
Mike Isely644afdb2007-01-20 00:19:23 -0300702 fv = hdw->tuner_signal_info.rangelow;
703 if (!fv) {
704 /* Safety fallback */
705 *vp = TV_MIN_FREQ;
706 return 0;
707 }
708 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
709 fv = (fv * 125) / 2;
710 } else {
711 fv = fv * 62500;
712 }
713 *vp = fv;
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -0300714 return 0;
715}
716
Mike Iselyb30d2442006-06-25 20:05:01 -0300717static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
718{
719 return cptr->hdw->enc_stale != 0;
720}
721
722static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
723{
724 cptr->hdw->enc_stale = 0;
Mike Isely681c7392007-11-26 01:48:52 -0300725 cptr->hdw->enc_unsafe_stale = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300726}
727
728static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
729{
730 int ret;
731 struct v4l2_ext_controls cs;
732 struct v4l2_ext_control c1;
733 memset(&cs,0,sizeof(cs));
734 memset(&c1,0,sizeof(c1));
735 cs.controls = &c1;
736 cs.count = 1;
737 c1.id = cptr->info->v4l_id;
Hans Verkuil01f1e442007-08-21 18:32:42 -0300738 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300739 VIDIOC_G_EXT_CTRLS);
740 if (ret) return ret;
741 *vp = c1.value;
742 return 0;
743}
744
745static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
746{
747 int ret;
Mike Isely681c7392007-11-26 01:48:52 -0300748 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselyb30d2442006-06-25 20:05:01 -0300749 struct v4l2_ext_controls cs;
750 struct v4l2_ext_control c1;
751 memset(&cs,0,sizeof(cs));
752 memset(&c1,0,sizeof(c1));
753 cs.controls = &c1;
754 cs.count = 1;
755 c1.id = cptr->info->v4l_id;
756 c1.value = v;
Mike Isely681c7392007-11-26 01:48:52 -0300757 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
758 hdw->state_encoder_run, &cs,
Mike Iselyb30d2442006-06-25 20:05:01 -0300759 VIDIOC_S_EXT_CTRLS);
Mike Isely681c7392007-11-26 01:48:52 -0300760 if (ret == -EBUSY) {
761 /* Oops. cx2341x is telling us it's not safe to change
762 this control while we're capturing. Make a note of this
763 fact so that the pipeline will be stopped the next time
764 controls are committed. Then go on ahead and store this
765 change anyway. */
766 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
767 0, &cs,
768 VIDIOC_S_EXT_CTRLS);
769 if (!ret) hdw->enc_unsafe_stale = !0;
770 }
Mike Iselyb30d2442006-06-25 20:05:01 -0300771 if (ret) return ret;
Mike Isely681c7392007-11-26 01:48:52 -0300772 hdw->enc_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -0300773 return 0;
774}
775
776static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
777{
778 struct v4l2_queryctrl qctrl;
779 struct pvr2_ctl_info *info;
780 qctrl.id = cptr->info->v4l_id;
781 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
782 /* Strip out the const so we can adjust a function pointer. It's
783 OK to do this here because we know this is a dynamically created
784 control, so the underlying storage for the info pointer is (a)
785 private to us, and (b) not in read-only storage. Either we do
786 this or we significantly complicate the underlying control
787 implementation. */
788 info = (struct pvr2_ctl_info *)(cptr->info);
789 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
790 if (info->set_value) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300791 info->set_value = NULL;
Mike Iselyb30d2442006-06-25 20:05:01 -0300792 }
793 } else {
794 if (!(info->set_value)) {
795 info->set_value = ctrl_cx2341x_set;
796 }
797 }
798 return qctrl.flags;
799}
800
Mike Iselyd8554972006-06-26 20:58:46 -0300801static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
802{
Mike Isely681c7392007-11-26 01:48:52 -0300803 *vp = cptr->hdw->state_pipeline_req;
804 return 0;
805}
806
807static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
808{
809 *vp = cptr->hdw->master_state;
Mike Iselyd8554972006-06-26 20:58:46 -0300810 return 0;
811}
812
813static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
814{
815 int result = pvr2_hdw_is_hsm(cptr->hdw);
816 *vp = PVR2_CVAL_HSM_FULL;
817 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
818 if (result) *vp = PVR2_CVAL_HSM_HIGH;
819 return 0;
820}
821
822static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
823{
824 *vp = cptr->hdw->std_mask_avail;
825 return 0;
826}
827
828static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
829{
830 struct pvr2_hdw *hdw = cptr->hdw;
831 v4l2_std_id ns;
832 ns = hdw->std_mask_avail;
833 ns = (ns & ~m) | (v & m);
834 if (ns == hdw->std_mask_avail) return 0;
835 hdw->std_mask_avail = ns;
836 pvr2_hdw_internal_set_std_avail(hdw);
837 pvr2_hdw_internal_find_stdenum(hdw);
838 return 0;
839}
840
841static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
842 char *bufPtr,unsigned int bufSize,
843 unsigned int *len)
844{
845 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
846 return 0;
847}
848
849static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
850 const char *bufPtr,unsigned int bufSize,
851 int *mskp,int *valp)
852{
853 int ret;
854 v4l2_std_id id;
855 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
856 if (ret < 0) return ret;
857 if (mskp) *mskp = id;
858 if (valp) *valp = id;
859 return 0;
860}
861
862static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
863{
864 *vp = cptr->hdw->std_mask_cur;
865 return 0;
866}
867
868static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
869{
870 struct pvr2_hdw *hdw = cptr->hdw;
871 v4l2_std_id ns;
872 ns = hdw->std_mask_cur;
873 ns = (ns & ~m) | (v & m);
874 if (ns == hdw->std_mask_cur) return 0;
875 hdw->std_mask_cur = ns;
876 hdw->std_dirty = !0;
877 pvr2_hdw_internal_find_stdenum(hdw);
878 return 0;
879}
880
881static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
882{
883 return cptr->hdw->std_dirty != 0;
884}
885
886static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
887{
888 cptr->hdw->std_dirty = 0;
889}
890
891static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
892{
Mike Isely18103c572007-01-20 00:09:47 -0300893 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300894 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300895 *vp = hdw->tuner_signal_info.signal;
896 return 0;
897}
898
899static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
900{
901 int val = 0;
902 unsigned int subchan;
903 struct pvr2_hdw *hdw = cptr->hdw;
Mike Iselya51f5002009-03-06 23:30:37 -0300904 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -0300905 subchan = hdw->tuner_signal_info.rxsubchans;
906 if (subchan & V4L2_TUNER_SUB_MONO) {
907 val |= (1 << V4L2_TUNER_MODE_MONO);
908 }
909 if (subchan & V4L2_TUNER_SUB_STEREO) {
910 val |= (1 << V4L2_TUNER_MODE_STEREO);
911 }
912 if (subchan & V4L2_TUNER_SUB_LANG1) {
913 val |= (1 << V4L2_TUNER_MODE_LANG1);
914 }
915 if (subchan & V4L2_TUNER_SUB_LANG2) {
916 val |= (1 << V4L2_TUNER_MODE_LANG2);
917 }
918 *vp = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300919 return 0;
920}
921
Mike Iselyd8554972006-06-26 20:58:46 -0300922
923static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
924{
925 struct pvr2_hdw *hdw = cptr->hdw;
926 if (v < 0) return -EINVAL;
927 if (v > hdw->std_enum_cnt) return -EINVAL;
928 hdw->std_enum_cur = v;
929 if (!v) return 0;
930 v--;
931 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
932 hdw->std_mask_cur = hdw->std_defs[v].id;
933 hdw->std_dirty = !0;
934 return 0;
935}
936
937
938static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
939{
940 *vp = cptr->hdw->std_enum_cur;
941 return 0;
942}
943
944
945static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
946{
947 return cptr->hdw->std_dirty != 0;
948}
949
950
951static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
952{
953 cptr->hdw->std_dirty = 0;
954}
955
956
957#define DEFINT(vmin,vmax) \
958 .type = pvr2_ctl_int, \
959 .def.type_int.min_value = vmin, \
960 .def.type_int.max_value = vmax
961
962#define DEFENUM(tab) \
963 .type = pvr2_ctl_enum, \
Mike Isely27c7b712007-01-20 00:39:17 -0300964 .def.type_enum.count = ARRAY_SIZE(tab), \
Mike Iselyd8554972006-06-26 20:58:46 -0300965 .def.type_enum.value_names = tab
966
Mike Isely33213962006-06-25 20:04:40 -0300967#define DEFBOOL \
968 .type = pvr2_ctl_bool
969
Mike Iselyd8554972006-06-26 20:58:46 -0300970#define DEFMASK(msk,tab) \
971 .type = pvr2_ctl_bitmask, \
972 .def.type_bitmask.valid_bits = msk, \
973 .def.type_bitmask.bit_names = tab
974
975#define DEFREF(vname) \
976 .set_value = ctrl_set_##vname, \
977 .get_value = ctrl_get_##vname, \
978 .is_dirty = ctrl_isdirty_##vname, \
979 .clear_dirty = ctrl_cleardirty_##vname
980
981
982#define VCREATE_FUNCS(vname) \
983static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
984{*vp = cptr->hdw->vname##_val; return 0;} \
985static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
986{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
987static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
988{return cptr->hdw->vname##_dirty != 0;} \
989static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
990{cptr->hdw->vname##_dirty = 0;}
991
992VCREATE_FUNCS(brightness)
993VCREATE_FUNCS(contrast)
994VCREATE_FUNCS(saturation)
995VCREATE_FUNCS(hue)
996VCREATE_FUNCS(volume)
997VCREATE_FUNCS(balance)
998VCREATE_FUNCS(bass)
999VCREATE_FUNCS(treble)
1000VCREATE_FUNCS(mute)
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001001VCREATE_FUNCS(cropl)
1002VCREATE_FUNCS(cropt)
1003VCREATE_FUNCS(cropw)
1004VCREATE_FUNCS(croph)
Mike Iselyc05c0462006-06-25 20:04:25 -03001005VCREATE_FUNCS(audiomode)
1006VCREATE_FUNCS(res_hor)
1007VCREATE_FUNCS(res_ver)
Mike Iselyd8554972006-06-26 20:58:46 -03001008VCREATE_FUNCS(srate)
Mike Iselyd8554972006-06-26 20:58:46 -03001009
Mike Iselyd8554972006-06-26 20:58:46 -03001010/* Table definition of all controls which can be manipulated */
1011static const struct pvr2_ctl_info control_defs[] = {
1012 {
1013 .v4l_id = V4L2_CID_BRIGHTNESS,
1014 .desc = "Brightness",
1015 .name = "brightness",
1016 .default_value = 128,
1017 DEFREF(brightness),
1018 DEFINT(0,255),
1019 },{
1020 .v4l_id = V4L2_CID_CONTRAST,
1021 .desc = "Contrast",
1022 .name = "contrast",
1023 .default_value = 68,
1024 DEFREF(contrast),
1025 DEFINT(0,127),
1026 },{
1027 .v4l_id = V4L2_CID_SATURATION,
1028 .desc = "Saturation",
1029 .name = "saturation",
1030 .default_value = 64,
1031 DEFREF(saturation),
1032 DEFINT(0,127),
1033 },{
1034 .v4l_id = V4L2_CID_HUE,
1035 .desc = "Hue",
1036 .name = "hue",
1037 .default_value = 0,
1038 DEFREF(hue),
1039 DEFINT(-128,127),
1040 },{
1041 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1042 .desc = "Volume",
1043 .name = "volume",
Mike Isely139eecf2006-12-27 23:36:33 -03001044 .default_value = 62000,
Mike Iselyd8554972006-06-26 20:58:46 -03001045 DEFREF(volume),
1046 DEFINT(0,65535),
1047 },{
1048 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1049 .desc = "Balance",
1050 .name = "balance",
1051 .default_value = 0,
1052 DEFREF(balance),
1053 DEFINT(-32768,32767),
1054 },{
1055 .v4l_id = V4L2_CID_AUDIO_BASS,
1056 .desc = "Bass",
1057 .name = "bass",
1058 .default_value = 0,
1059 DEFREF(bass),
1060 DEFINT(-32768,32767),
1061 },{
1062 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1063 .desc = "Treble",
1064 .name = "treble",
1065 .default_value = 0,
1066 DEFREF(treble),
1067 DEFINT(-32768,32767),
1068 },{
1069 .v4l_id = V4L2_CID_AUDIO_MUTE,
1070 .desc = "Mute",
1071 .name = "mute",
1072 .default_value = 0,
1073 DEFREF(mute),
Mike Isely33213962006-06-25 20:04:40 -03001074 DEFBOOL,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001075 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001076 .desc = "Capture crop left margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001077 .name = "crop_left",
1078 .internal_id = PVR2_CID_CROPL,
1079 .default_value = 0,
1080 DEFREF(cropl),
1081 DEFINT(-129, 340),
1082 .get_min_value = ctrl_cropl_min_get,
1083 .get_max_value = ctrl_cropl_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001084 .get_def_value = ctrl_get_cropcapdl,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001085 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001086 .desc = "Capture crop top margin",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001087 .name = "crop_top",
1088 .internal_id = PVR2_CID_CROPT,
1089 .default_value = 0,
1090 DEFREF(cropt),
1091 DEFINT(-35, 544),
1092 .get_min_value = ctrl_cropt_min_get,
1093 .get_max_value = ctrl_cropt_max_get,
Mike Isely432907f2008-08-31 21:02:20 -03001094 .get_def_value = ctrl_get_cropcapdt,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001095 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001096 .desc = "Capture crop width",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001097 .name = "crop_width",
1098 .internal_id = PVR2_CID_CROPW,
1099 .default_value = 720,
1100 DEFREF(cropw),
Mike Isely432907f2008-08-31 21:02:20 -03001101 .get_max_value = ctrl_cropw_max_get,
1102 .get_def_value = ctrl_get_cropcapdw,
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001103 }, {
Mike Isely432907f2008-08-31 21:02:20 -03001104 .desc = "Capture crop height",
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03001105 .name = "crop_height",
1106 .internal_id = PVR2_CID_CROPH,
1107 .default_value = 480,
1108 DEFREF(croph),
Mike Isely432907f2008-08-31 21:02:20 -03001109 .get_max_value = ctrl_croph_max_get,
1110 .get_def_value = ctrl_get_cropcapdh,
1111 }, {
1112 .desc = "Capture capability pixel aspect numerator",
1113 .name = "cropcap_pixel_numerator",
1114 .internal_id = PVR2_CID_CROPCAPPAN,
1115 .get_value = ctrl_get_cropcappan,
1116 }, {
1117 .desc = "Capture capability pixel aspect denominator",
1118 .name = "cropcap_pixel_denominator",
1119 .internal_id = PVR2_CID_CROPCAPPAD,
1120 .get_value = ctrl_get_cropcappad,
1121 }, {
1122 .desc = "Capture capability bounds top",
1123 .name = "cropcap_bounds_top",
1124 .internal_id = PVR2_CID_CROPCAPBT,
1125 .get_value = ctrl_get_cropcapbt,
1126 }, {
1127 .desc = "Capture capability bounds left",
1128 .name = "cropcap_bounds_left",
1129 .internal_id = PVR2_CID_CROPCAPBL,
1130 .get_value = ctrl_get_cropcapbl,
1131 }, {
1132 .desc = "Capture capability bounds width",
1133 .name = "cropcap_bounds_width",
1134 .internal_id = PVR2_CID_CROPCAPBW,
1135 .get_value = ctrl_get_cropcapbw,
1136 }, {
1137 .desc = "Capture capability bounds height",
1138 .name = "cropcap_bounds_height",
1139 .internal_id = PVR2_CID_CROPCAPBH,
1140 .get_value = ctrl_get_cropcapbh,
Mike Iselyd8554972006-06-26 20:58:46 -03001141 },{
Mike Iselyc05c0462006-06-25 20:04:25 -03001142 .desc = "Video Source",
1143 .name = "input",
1144 .internal_id = PVR2_CID_INPUT,
1145 .default_value = PVR2_CVAL_INPUT_TV,
Mike Isely29bf5b12008-04-22 14:45:37 -03001146 .check_value = ctrl_check_input,
Mike Iselyc05c0462006-06-25 20:04:25 -03001147 DEFREF(input),
1148 DEFENUM(control_values_input),
1149 },{
1150 .desc = "Audio Mode",
1151 .name = "audio_mode",
1152 .internal_id = PVR2_CID_AUDIOMODE,
1153 .default_value = V4L2_TUNER_MODE_STEREO,
1154 DEFREF(audiomode),
1155 DEFENUM(control_values_audiomode),
1156 },{
1157 .desc = "Horizontal capture resolution",
1158 .name = "resolution_hor",
1159 .internal_id = PVR2_CID_HRES,
1160 .default_value = 720,
1161 DEFREF(res_hor),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001162 DEFINT(19,720),
Mike Iselyc05c0462006-06-25 20:04:25 -03001163 },{
1164 .desc = "Vertical capture resolution",
1165 .name = "resolution_ver",
1166 .internal_id = PVR2_CID_VRES,
1167 .default_value = 480,
1168 DEFREF(res_ver),
Mike Isely3ad9fc32006-09-02 22:37:52 -03001169 DEFINT(17,576),
1170 /* Hook in check for video standard and adjust maximum
1171 depending on the standard. */
1172 .get_max_value = ctrl_vres_max_get,
1173 .get_min_value = ctrl_vres_min_get,
Mike Iselyc05c0462006-06-25 20:04:25 -03001174 },{
Mike Iselyb30d2442006-06-25 20:05:01 -03001175 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
Mike Isely434449f2006-08-08 09:10:06 -03001176 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1177 .desc = "Audio Sampling Frequency",
Mike Iselyd8554972006-06-26 20:58:46 -03001178 .name = "srate",
Mike Iselyd8554972006-06-26 20:58:46 -03001179 DEFREF(srate),
1180 DEFENUM(control_values_srate),
1181 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001182 .desc = "Tuner Frequency (Hz)",
1183 .name = "frequency",
1184 .internal_id = PVR2_CID_FREQUENCY,
Mike Isely1bde0282006-12-27 23:30:13 -03001185 .default_value = 0,
Mike Iselyd8554972006-06-26 20:58:46 -03001186 .set_value = ctrl_freq_set,
1187 .get_value = ctrl_freq_get,
1188 .is_dirty = ctrl_freq_is_dirty,
1189 .clear_dirty = ctrl_freq_clear_dirty,
Mike Isely644afdb2007-01-20 00:19:23 -03001190 DEFINT(0,0),
Pantelis Koukousoulas25d85272006-12-27 23:06:04 -03001191 /* Hook in check for input value (tv/radio) and adjust
1192 max/min values accordingly */
1193 .get_max_value = ctrl_freq_max_get,
1194 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001195 },{
1196 .desc = "Channel",
1197 .name = "channel",
1198 .set_value = ctrl_channel_set,
1199 .get_value = ctrl_channel_get,
1200 DEFINT(0,FREQTABLE_SIZE),
1201 },{
1202 .desc = "Channel Program Frequency",
1203 .name = "freq_table_value",
1204 .set_value = ctrl_channelfreq_set,
1205 .get_value = ctrl_channelfreq_get,
Mike Isely644afdb2007-01-20 00:19:23 -03001206 DEFINT(0,0),
Mike Isely1bde0282006-12-27 23:30:13 -03001207 /* Hook in check for input value (tv/radio) and adjust
1208 max/min values accordingly */
Mike Isely1bde0282006-12-27 23:30:13 -03001209 .get_max_value = ctrl_freq_max_get,
1210 .get_min_value = ctrl_freq_min_get,
Mike Iselyd8554972006-06-26 20:58:46 -03001211 },{
1212 .desc = "Channel Program ID",
1213 .name = "freq_table_channel",
1214 .set_value = ctrl_channelprog_set,
1215 .get_value = ctrl_channelprog_get,
1216 DEFINT(0,FREQTABLE_SIZE),
1217 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001218 .desc = "Streaming Enabled",
1219 .name = "streaming_enabled",
1220 .get_value = ctrl_streamingenabled_get,
Mike Isely33213962006-06-25 20:04:40 -03001221 DEFBOOL,
Mike Iselyd8554972006-06-26 20:58:46 -03001222 },{
1223 .desc = "USB Speed",
1224 .name = "usb_speed",
1225 .get_value = ctrl_hsm_get,
1226 DEFENUM(control_values_hsm),
1227 },{
Mike Isely681c7392007-11-26 01:48:52 -03001228 .desc = "Master State",
1229 .name = "master_state",
1230 .get_value = ctrl_masterstate_get,
1231 DEFENUM(pvr2_state_names),
1232 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001233 .desc = "Signal Present",
1234 .name = "signal_present",
1235 .get_value = ctrl_signal_get,
Mike Isely18103c572007-01-20 00:09:47 -03001236 DEFINT(0,65535),
1237 },{
1238 .desc = "Audio Modes Present",
1239 .name = "audio_modes_present",
1240 .get_value = ctrl_audio_modes_present_get,
1241 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1242 v4l. Nothing outside of this module cares about this,
1243 but I reuse it in order to also reuse the
1244 control_values_audiomode string table. */
1245 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1246 (1 << V4L2_TUNER_MODE_STEREO)|
1247 (1 << V4L2_TUNER_MODE_LANG1)|
1248 (1 << V4L2_TUNER_MODE_LANG2)),
1249 control_values_audiomode),
Mike Iselyd8554972006-06-26 20:58:46 -03001250 },{
1251 .desc = "Video Standards Available Mask",
1252 .name = "video_standard_mask_available",
1253 .internal_id = PVR2_CID_STDAVAIL,
1254 .skip_init = !0,
1255 .get_value = ctrl_stdavail_get,
1256 .set_value = ctrl_stdavail_set,
1257 .val_to_sym = ctrl_std_val_to_sym,
1258 .sym_to_val = ctrl_std_sym_to_val,
1259 .type = pvr2_ctl_bitmask,
1260 },{
1261 .desc = "Video Standards In Use Mask",
1262 .name = "video_standard_mask_active",
1263 .internal_id = PVR2_CID_STDCUR,
1264 .skip_init = !0,
1265 .get_value = ctrl_stdcur_get,
1266 .set_value = ctrl_stdcur_set,
1267 .is_dirty = ctrl_stdcur_is_dirty,
1268 .clear_dirty = ctrl_stdcur_clear_dirty,
1269 .val_to_sym = ctrl_std_val_to_sym,
1270 .sym_to_val = ctrl_std_sym_to_val,
1271 .type = pvr2_ctl_bitmask,
1272 },{
Mike Iselyd8554972006-06-26 20:58:46 -03001273 .desc = "Video Standard Name",
1274 .name = "video_standard",
1275 .internal_id = PVR2_CID_STDENUM,
1276 .skip_init = !0,
1277 .get_value = ctrl_stdenumcur_get,
1278 .set_value = ctrl_stdenumcur_set,
1279 .is_dirty = ctrl_stdenumcur_is_dirty,
1280 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1281 .type = pvr2_ctl_enum,
1282 }
1283};
1284
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001285#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
Mike Iselyd8554972006-06-26 20:58:46 -03001286
1287
1288const char *pvr2_config_get_name(enum pvr2_config cfg)
1289{
1290 switch (cfg) {
1291 case pvr2_config_empty: return "empty";
1292 case pvr2_config_mpeg: return "mpeg";
1293 case pvr2_config_vbi: return "vbi";
Mike Isely16eb40d2006-12-30 18:27:32 -03001294 case pvr2_config_pcm: return "pcm";
1295 case pvr2_config_rawvideo: return "raw video";
Mike Iselyd8554972006-06-26 20:58:46 -03001296 }
1297 return "<unknown>";
1298}
1299
1300
1301struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1302{
1303 return hdw->usb_dev;
1304}
1305
1306
1307unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1308{
1309 return hdw->serial_number;
1310}
1311
Mike Isely31a18542007-04-08 01:11:47 -03001312
1313const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1314{
1315 return hdw->bus_info;
1316}
1317
1318
Mike Isely13a88792009-01-14 04:22:56 -03001319const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1320{
1321 return hdw->identifier;
1322}
1323
1324
Mike Isely1bde0282006-12-27 23:30:13 -03001325unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1326{
1327 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1328}
1329
1330/* Set the currently tuned frequency and account for all possible
1331 driver-core side effects of this action. */
Adrian Bunkf55a8712008-04-18 05:38:56 -03001332static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
Mike Isely1bde0282006-12-27 23:30:13 -03001333{
Mike Isely7c74e572007-01-20 00:15:41 -03001334 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
Mike Isely1bde0282006-12-27 23:30:13 -03001335 if (hdw->freqSelector) {
1336 /* Swing over to radio frequency selection */
1337 hdw->freqSelector = 0;
1338 hdw->freqDirty = !0;
1339 }
Mike Isely1bde0282006-12-27 23:30:13 -03001340 if (hdw->freqValRadio != val) {
1341 hdw->freqValRadio = val;
1342 hdw->freqSlotRadio = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001343 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001344 }
Mike Isely7c74e572007-01-20 00:15:41 -03001345 } else {
Mike Isely1bde0282006-12-27 23:30:13 -03001346 if (!(hdw->freqSelector)) {
1347 /* Swing over to television frequency selection */
1348 hdw->freqSelector = 1;
1349 hdw->freqDirty = !0;
1350 }
Mike Isely1bde0282006-12-27 23:30:13 -03001351 if (hdw->freqValTelevision != val) {
1352 hdw->freqValTelevision = val;
1353 hdw->freqSlotTelevision = 0;
Mike Isely7c74e572007-01-20 00:15:41 -03001354 hdw->freqDirty = !0;
Mike Isely1bde0282006-12-27 23:30:13 -03001355 }
Mike Isely1bde0282006-12-27 23:30:13 -03001356 }
1357}
1358
Mike Iselyd8554972006-06-26 20:58:46 -03001359int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1360{
1361 return hdw->unit_number;
1362}
1363
1364
1365/* Attempt to locate one of the given set of files. Messages are logged
1366 appropriate to what has been found. The return value will be 0 or
1367 greater on success (it will be the index of the file name found) and
1368 fw_entry will be filled in. Otherwise a negative error is returned on
1369 failure. If the return value is -ENOENT then no viable firmware file
1370 could be located. */
1371static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1372 const struct firmware **fw_entry,
1373 const char *fwtypename,
1374 unsigned int fwcount,
1375 const char *fwnames[])
1376{
1377 unsigned int idx;
1378 int ret = -EINVAL;
1379 for (idx = 0; idx < fwcount; idx++) {
1380 ret = request_firmware(fw_entry,
1381 fwnames[idx],
1382 &hdw->usb_dev->dev);
1383 if (!ret) {
1384 trace_firmware("Located %s firmware: %s;"
1385 " uploading...",
1386 fwtypename,
1387 fwnames[idx]);
1388 return idx;
1389 }
1390 if (ret == -ENOENT) continue;
1391 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1392 "request_firmware fatal error with code=%d",ret);
1393 return ret;
1394 }
1395 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1396 "***WARNING***"
1397 " Device %s firmware"
1398 " seems to be missing.",
1399 fwtypename);
1400 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1401 "Did you install the pvrusb2 firmware files"
1402 " in their proper location?");
1403 if (fwcount == 1) {
1404 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1405 "request_firmware unable to locate %s file %s",
1406 fwtypename,fwnames[0]);
1407 } else {
1408 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1409 "request_firmware unable to locate"
1410 " one of the following %s files:",
1411 fwtypename);
1412 for (idx = 0; idx < fwcount; idx++) {
1413 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1414 "request_firmware: Failed to find %s",
1415 fwnames[idx]);
1416 }
1417 }
1418 return ret;
1419}
1420
1421
1422/*
1423 * pvr2_upload_firmware1().
1424 *
1425 * Send the 8051 firmware to the device. After the upload, arrange for
1426 * device to re-enumerate.
1427 *
1428 * NOTE : the pointer to the firmware data given by request_firmware()
1429 * is not suitable for an usb transaction.
1430 *
1431 */
Adrian Bunk07e337e2006-06-30 11:30:20 -03001432static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03001433{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001434 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001435 void *fw_ptr;
1436 unsigned int pipe;
1437 int ret;
1438 u16 address;
Mike Isely1d643a32007-09-08 22:18:50 -03001439
Mike Isely989eb152007-11-26 01:53:12 -03001440 if (!hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03001441 hdw->fw1_state = FW1_STATE_OK;
Mike Isely56dcbfa2007-11-26 02:00:51 -03001442 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1443 "Connected device type defines"
1444 " no firmware to upload; ignoring firmware");
1445 return -ENOTTY;
Mike Isely1d643a32007-09-08 22:18:50 -03001446 }
1447
Mike Iselyd8554972006-06-26 20:58:46 -03001448 hdw->fw1_state = FW1_STATE_FAILED; // default result
1449
1450 trace_firmware("pvr2_upload_firmware1");
1451
1452 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
Mike Isely989eb152007-11-26 01:53:12 -03001453 hdw->hdw_desc->fx2_firmware.cnt,
1454 hdw->hdw_desc->fx2_firmware.lst);
Mike Iselyd8554972006-06-26 20:58:46 -03001455 if (ret < 0) {
1456 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1457 return ret;
1458 }
1459
1460 usb_settoggle(hdw->usb_dev, 0 & 0xf, !(0 & USB_DIR_IN), 0);
1461 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1462
1463 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1464
1465 if (fw_entry->size != 0x2000){
1466 pvr2_trace(PVR2_TRACE_ERROR_LEGS,"wrong fx2 firmware size");
1467 release_firmware(fw_entry);
1468 return -ENOMEM;
1469 }
1470
1471 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1472 if (fw_ptr == NULL){
1473 release_firmware(fw_entry);
1474 return -ENOMEM;
1475 }
1476
1477 /* We have to hold the CPU during firmware upload. */
1478 pvr2_hdw_cpureset_assert(hdw,1);
1479
1480 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1481 chunk. */
1482
1483 ret = 0;
1484 for(address = 0; address < fw_entry->size; address += 0x800) {
1485 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1486 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1487 0, fw_ptr, 0x800, HZ);
1488 }
1489
1490 trace_firmware("Upload done, releasing device's CPU");
1491
1492 /* Now release the CPU. It will disconnect and reconnect later. */
1493 pvr2_hdw_cpureset_assert(hdw,0);
1494
1495 kfree(fw_ptr);
1496 release_firmware(fw_entry);
1497
1498 trace_firmware("Upload done (%d bytes sent)",ret);
1499
1500 /* We should have written 8192 bytes */
1501 if (ret == 8192) {
1502 hdw->fw1_state = FW1_STATE_RELOAD;
1503 return 0;
1504 }
1505
1506 return -EIO;
1507}
1508
1509
1510/*
1511 * pvr2_upload_firmware2()
1512 *
1513 * This uploads encoder firmware on endpoint 2.
1514 *
1515 */
1516
1517int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1518{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001519 const struct firmware *fw_entry = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03001520 void *fw_ptr;
Mike Isely90060d32007-02-08 02:02:53 -03001521 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001522 int actual_length;
1523 int ret = 0;
1524 int fwidx;
1525 static const char *fw_files[] = {
1526 CX2341X_FIRM_ENC_FILENAME,
1527 };
1528
Mike Isely989eb152007-11-26 01:53:12 -03001529 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
Mike Isely1d643a32007-09-08 22:18:50 -03001530 return 0;
1531 }
1532
Mike Iselyd8554972006-06-26 20:58:46 -03001533 trace_firmware("pvr2_upload_firmware2");
1534
1535 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -03001536 ARRAY_SIZE(fw_files), fw_files);
Mike Iselyd8554972006-06-26 20:58:46 -03001537 if (ret < 0) return ret;
1538 fwidx = ret;
1539 ret = 0;
Mike Iselyb30d2442006-06-25 20:05:01 -03001540 /* Since we're about to completely reinitialize the encoder,
1541 invalidate our cached copy of its configuration state. Next
1542 time we configure the encoder, then we'll fully configure it. */
1543 hdw->enc_cur_valid = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001544
Mike Iselyd913d632008-04-06 04:04:35 -03001545 /* Encoder is about to be reset so note that as far as we're
1546 concerned now, the encoder has never been run. */
1547 del_timer_sync(&hdw->encoder_run_timer);
1548 if (hdw->state_encoder_runok) {
1549 hdw->state_encoder_runok = 0;
1550 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1551 }
1552
Mike Iselyd8554972006-06-26 20:58:46 -03001553 /* First prepare firmware loading */
1554 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1555 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1556 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1557 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1558 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1559 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1560 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1561 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1562 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1563 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1564 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1565 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1566 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1567 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1568 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1569 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001570 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1571 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001572
1573 if (ret) {
1574 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1575 "firmware2 upload prep failed, ret=%d",ret);
1576 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001577 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001578 }
1579
1580 /* Now send firmware */
1581
1582 fw_len = fw_entry->size;
1583
Mike Isely90060d32007-02-08 02:02:53 -03001584 if (fw_len % sizeof(u32)) {
Mike Iselyd8554972006-06-26 20:58:46 -03001585 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1586 "size of %s firmware"
Mike Isely48dc30a2007-03-03 10:13:05 -02001587 " must be a multiple of %zu bytes",
Mike Isely90060d32007-02-08 02:02:53 -03001588 fw_files[fwidx],sizeof(u32));
Mike Iselyd8554972006-06-26 20:58:46 -03001589 release_firmware(fw_entry);
Mike Isely21684ba2008-04-21 03:49:33 -03001590 ret = -EINVAL;
1591 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001592 }
1593
1594 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1595 if (fw_ptr == NULL){
1596 release_firmware(fw_entry);
1597 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1598 "failed to allocate memory for firmware2 upload");
Mike Isely21684ba2008-04-21 03:49:33 -03001599 ret = -ENOMEM;
1600 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001601 }
1602
1603 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1604
Mike Isely90060d32007-02-08 02:02:53 -03001605 fw_done = 0;
1606 for (fw_done = 0; fw_done < fw_len;) {
1607 bcnt = fw_len - fw_done;
1608 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1609 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1610 /* Usbsnoop log shows that we must swap bytes... */
Mike Isely5f33df12008-08-30 15:09:31 -03001611 /* Some background info: The data being swapped here is a
1612 firmware image destined for the mpeg encoder chip that
1613 lives at the other end of a USB endpoint. The encoder
1614 chip always talks in 32 bit chunks and its storage is
1615 organized into 32 bit words. However from the file
1616 system to the encoder chip everything is purely a byte
1617 stream. The firmware file's contents are always 32 bit
1618 swapped from what the encoder expects. Thus the need
1619 always exists to swap the bytes regardless of the endian
1620 type of the host processor and therefore swab32() makes
1621 the most sense. */
Mike Isely90060d32007-02-08 02:02:53 -03001622 for (icnt = 0; icnt < bcnt/4 ; icnt++)
Harvey Harrison513edce2008-08-18 17:38:01 -03001623 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
Mike Iselyd8554972006-06-26 20:58:46 -03001624
Mike Isely90060d32007-02-08 02:02:53 -03001625 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
Mike Iselyd8554972006-06-26 20:58:46 -03001626 &actual_length, HZ);
Mike Isely90060d32007-02-08 02:02:53 -03001627 ret |= (actual_length != bcnt);
1628 if (ret) break;
1629 fw_done += bcnt;
Mike Iselyd8554972006-06-26 20:58:46 -03001630 }
1631
1632 trace_firmware("upload of %s : %i / %i ",
1633 fw_files[fwidx],fw_done,fw_len);
1634
1635 kfree(fw_ptr);
1636 release_firmware(fw_entry);
1637
1638 if (ret) {
1639 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1640 "firmware2 upload transfer failure");
Mike Isely21684ba2008-04-21 03:49:33 -03001641 goto done;
Mike Iselyd8554972006-06-26 20:58:46 -03001642 }
1643
1644 /* Finish upload */
1645
1646 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1647 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
Mike Isely1c9d10d2008-03-28 05:38:54 -03001648 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
Mike Iselyd8554972006-06-26 20:58:46 -03001649
1650 if (ret) {
1651 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1652 "firmware2 upload post-proc failure");
Mike Iselyd8554972006-06-26 20:58:46 -03001653 }
Mike Isely21684ba2008-04-21 03:49:33 -03001654
1655 done:
Mike Isely1df59f02008-04-21 03:50:39 -03001656 if (hdw->hdw_desc->signal_routing_scheme ==
1657 PVR2_ROUTING_SCHEME_GOTVIEW) {
1658 /* Ensure that GPIO 11 is set to output for GOTVIEW
1659 hardware. */
1660 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1661 }
Mike Iselyd8554972006-06-26 20:58:46 -03001662 return ret;
1663}
1664
1665
Mike Isely681c7392007-11-26 01:48:52 -03001666static const char *pvr2_get_state_name(unsigned int st)
Mike Iselyd8554972006-06-26 20:58:46 -03001667{
Mike Isely681c7392007-11-26 01:48:52 -03001668 if (st < ARRAY_SIZE(pvr2_state_names)) {
1669 return pvr2_state_names[st];
Mike Iselyd8554972006-06-26 20:58:46 -03001670 }
Mike Isely681c7392007-11-26 01:48:52 -03001671 return "???";
Mike Iselyd8554972006-06-26 20:58:46 -03001672}
1673
Mike Isely681c7392007-11-26 01:48:52 -03001674static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
Mike Iselyd8554972006-06-26 20:58:46 -03001675{
Mike Iselyaf78e162009-03-07 00:21:30 -03001676 if (hdw->decoder_ctrl) {
1677 hdw->decoder_ctrl->enable(hdw->decoder_ctrl->ctxt, enablefl);
1678 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001679 }
Mike Iselyaf78e162009-03-07 00:21:30 -03001680 /* Even though we really only care about the video decoder chip at
1681 this point, we'll broadcast stream on/off to all sub-devices
1682 anyway, just in case somebody else wants to hear the
1683 command... */
Mike Iselye2605082009-03-07 01:50:48 -03001684 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 stream=%s",
1685 (enablefl ? "on" : "off"));
Mike Iselyaf78e162009-03-07 00:21:30 -03001686 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_stream, enablefl);
1687 if (hdw->decoder_client_id) {
1688 /* We get here if the encoder has been noticed. Otherwise
1689 we'll issue a warning to the user (which should
1690 normally never happen). */
1691 return 0;
1692 }
1693 if (!hdw->flag_decoder_missed) {
1694 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1695 "WARNING: No decoder present");
1696 hdw->flag_decoder_missed = !0;
1697 trace_stbit("flag_decoder_missed",
1698 hdw->flag_decoder_missed);
1699 }
1700 return -EIO;
Mike Iselyd8554972006-06-26 20:58:46 -03001701}
1702
1703
Mike Isely681c7392007-11-26 01:48:52 -03001704void pvr2_hdw_set_decoder(struct pvr2_hdw *hdw,struct pvr2_decoder_ctrl *ptr)
1705{
1706 if (hdw->decoder_ctrl == ptr) return;
1707 hdw->decoder_ctrl = ptr;
1708 if (hdw->decoder_ctrl && hdw->flag_decoder_missed) {
1709 hdw->flag_decoder_missed = 0;
1710 trace_stbit("flag_decoder_missed",
1711 hdw->flag_decoder_missed);
1712 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1713 "Decoder has appeared");
1714 pvr2_hdw_state_sched(hdw);
1715 }
1716}
1717
1718
1719int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1720{
1721 return hdw->master_state;
1722}
1723
1724
1725static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1726{
1727 if (!hdw->flag_tripped) return 0;
1728 hdw->flag_tripped = 0;
1729 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1730 "Clearing driver error statuss");
1731 return !0;
1732}
1733
1734
1735int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1736{
1737 int fl;
1738 LOCK_TAKE(hdw->big_lock); do {
1739 fl = pvr2_hdw_untrip_unlocked(hdw);
1740 } while (0); LOCK_GIVE(hdw->big_lock);
1741 if (fl) pvr2_hdw_state_sched(hdw);
1742 return 0;
1743}
1744
1745
Mike Isely681c7392007-11-26 01:48:52 -03001746
1747
Mike Iselyd8554972006-06-26 20:58:46 -03001748int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1749{
Mike Isely681c7392007-11-26 01:48:52 -03001750 return hdw->state_pipeline_req != 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001751}
1752
1753
1754int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1755{
Mike Isely681c7392007-11-26 01:48:52 -03001756 int ret,st;
Mike Iselyd8554972006-06-26 20:58:46 -03001757 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03001758 pvr2_hdw_untrip_unlocked(hdw);
1759 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1760 hdw->state_pipeline_req = enable_flag != 0;
1761 pvr2_trace(PVR2_TRACE_START_STOP,
1762 "/*--TRACE_STREAM--*/ %s",
1763 enable_flag ? "enable" : "disable");
1764 }
1765 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03001766 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001767 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1768 if (enable_flag) {
1769 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1770 if (st != PVR2_STATE_READY) return -EIO;
1771 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1772 }
1773 }
Mike Iselyd8554972006-06-26 20:58:46 -03001774 return 0;
1775}
1776
1777
1778int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1779{
Mike Isely681c7392007-11-26 01:48:52 -03001780 int fl;
Mike Iselyd8554972006-06-26 20:58:46 -03001781 LOCK_TAKE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001782 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1783 hdw->desired_stream_type = config;
1784 hdw->state_pipeline_config = 0;
1785 trace_stbit("state_pipeline_config",
1786 hdw->state_pipeline_config);
1787 pvr2_hdw_state_sched(hdw);
1788 }
Mike Iselyd8554972006-06-26 20:58:46 -03001789 LOCK_GIVE(hdw->big_lock);
Mike Isely681c7392007-11-26 01:48:52 -03001790 if (fl) return 0;
1791 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03001792}
1793
1794
1795static int get_default_tuner_type(struct pvr2_hdw *hdw)
1796{
1797 int unit_number = hdw->unit_number;
1798 int tp = -1;
1799 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1800 tp = tuner[unit_number];
1801 }
1802 if (tp < 0) return -EINVAL;
1803 hdw->tuner_type = tp;
Mike Iselyaaf78842007-11-26 02:04:11 -03001804 hdw->tuner_updated = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03001805 return 0;
1806}
1807
1808
1809static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1810{
1811 int unit_number = hdw->unit_number;
1812 int tp = 0;
1813 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1814 tp = video_std[unit_number];
Mike Isely6a540252007-12-02 23:51:34 -03001815 if (tp) return tp;
Mike Iselyd8554972006-06-26 20:58:46 -03001816 }
Mike Isely6a540252007-12-02 23:51:34 -03001817 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001818}
1819
1820
1821static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1822{
1823 int unit_number = hdw->unit_number;
1824 int tp = 0;
1825 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1826 tp = tolerance[unit_number];
1827 }
1828 return tp;
1829}
1830
1831
1832static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1833{
1834 /* Try a harmless request to fetch the eeprom's address over
1835 endpoint 1. See what happens. Only the full FX2 image can
1836 respond to this. If this probe fails then likely the FX2
1837 firmware needs be loaded. */
1838 int result;
1839 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03001840 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03001841 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1842 hdw->cmd_buffer,1,
1843 hdw->cmd_buffer,1);
1844 if (result < 0) break;
1845 } while(0); LOCK_GIVE(hdw->ctl_lock);
1846 if (result) {
1847 pvr2_trace(PVR2_TRACE_INIT,
1848 "Probe of device endpoint 1 result status %d",
1849 result);
1850 } else {
1851 pvr2_trace(PVR2_TRACE_INIT,
1852 "Probe of device endpoint 1 succeeded");
1853 }
1854 return result == 0;
1855}
1856
Mike Isely9f66d4e2007-09-08 22:28:51 -03001857struct pvr2_std_hack {
1858 v4l2_std_id pat; /* Pattern to match */
1859 v4l2_std_id msk; /* Which bits we care about */
1860 v4l2_std_id std; /* What additional standards or default to set */
1861};
1862
1863/* This data structure labels specific combinations of standards from
1864 tveeprom that we'll try to recognize. If we recognize one, then assume
1865 a specified default standard to use. This is here because tveeprom only
1866 tells us about available standards not the intended default standard (if
1867 any) for the device in question. We guess the default based on what has
1868 been reported as available. Note that this is only for guessing a
1869 default - which can always be overridden explicitly - and if the user
1870 has otherwise named a default then that default will always be used in
1871 place of this table. */
Tobias Klauserebff0332008-04-22 14:45:45 -03001872static const struct pvr2_std_hack std_eeprom_maps[] = {
Mike Isely9f66d4e2007-09-08 22:28:51 -03001873 { /* PAL(B/G) */
1874 .pat = V4L2_STD_B|V4L2_STD_GH,
1875 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1876 },
1877 { /* NTSC(M) */
1878 .pat = V4L2_STD_MN,
1879 .std = V4L2_STD_NTSC_M,
1880 },
1881 { /* PAL(I) */
1882 .pat = V4L2_STD_PAL_I,
1883 .std = V4L2_STD_PAL_I,
1884 },
1885 { /* SECAM(L/L') */
1886 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1887 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1888 },
1889 { /* PAL(D/D1/K) */
1890 .pat = V4L2_STD_DK,
Roel Kluinea2562d2007-12-02 23:04:57 -03001891 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001892 },
1893};
1894
Mike Iselyd8554972006-06-26 20:58:46 -03001895static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1896{
1897 char buf[40];
1898 unsigned int bcnt;
Mike Isely3d290bd2007-12-03 01:47:12 -03001899 v4l2_std_id std1,std2,std3;
Mike Iselyd8554972006-06-26 20:58:46 -03001900
1901 std1 = get_default_standard(hdw);
Mike Isely3d290bd2007-12-03 01:47:12 -03001902 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
Mike Iselyd8554972006-06-26 20:58:46 -03001903
1904 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
Mike Isely56585382007-09-08 22:32:12 -03001905 pvr2_trace(PVR2_TRACE_STD,
Mike Isely56dcbfa2007-11-26 02:00:51 -03001906 "Supported video standard(s) reported available"
1907 " in hardware: %.*s",
Mike Iselyd8554972006-06-26 20:58:46 -03001908 bcnt,buf);
1909
1910 hdw->std_mask_avail = hdw->std_mask_eeprom;
1911
Mike Isely3d290bd2007-12-03 01:47:12 -03001912 std2 = (std1|std3) & ~hdw->std_mask_avail;
Mike Iselyd8554972006-06-26 20:58:46 -03001913 if (std2) {
1914 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
Mike Isely56585382007-09-08 22:32:12 -03001915 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001916 "Expanding supported video standards"
1917 " to include: %.*s",
1918 bcnt,buf);
1919 hdw->std_mask_avail |= std2;
1920 }
1921
1922 pvr2_hdw_internal_set_std_avail(hdw);
1923
1924 if (std1) {
1925 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
Mike Isely56585382007-09-08 22:32:12 -03001926 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001927 "Initial video standard forced to %.*s",
1928 bcnt,buf);
1929 hdw->std_mask_cur = std1;
1930 hdw->std_dirty = !0;
1931 pvr2_hdw_internal_find_stdenum(hdw);
1932 return;
1933 }
Mike Isely3d290bd2007-12-03 01:47:12 -03001934 if (std3) {
1935 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1936 pvr2_trace(PVR2_TRACE_STD,
1937 "Initial video standard"
1938 " (determined by device type): %.*s",bcnt,buf);
1939 hdw->std_mask_cur = std3;
1940 hdw->std_dirty = !0;
1941 pvr2_hdw_internal_find_stdenum(hdw);
1942 return;
1943 }
Mike Iselyd8554972006-06-26 20:58:46 -03001944
Mike Isely9f66d4e2007-09-08 22:28:51 -03001945 {
1946 unsigned int idx;
1947 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1948 if (std_eeprom_maps[idx].msk ?
1949 ((std_eeprom_maps[idx].pat ^
1950 hdw->std_mask_eeprom) &
1951 std_eeprom_maps[idx].msk) :
1952 (std_eeprom_maps[idx].pat !=
1953 hdw->std_mask_eeprom)) continue;
1954 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1955 std_eeprom_maps[idx].std);
Mike Isely56585382007-09-08 22:32:12 -03001956 pvr2_trace(PVR2_TRACE_STD,
Mike Isely9f66d4e2007-09-08 22:28:51 -03001957 "Initial video standard guessed as %.*s",
1958 bcnt,buf);
1959 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1960 hdw->std_dirty = !0;
1961 pvr2_hdw_internal_find_stdenum(hdw);
1962 return;
1963 }
1964 }
1965
Mike Iselyd8554972006-06-26 20:58:46 -03001966 if (hdw->std_enum_cnt > 1) {
1967 // Autoselect the first listed standard
1968 hdw->std_enum_cur = 1;
1969 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1970 hdw->std_dirty = !0;
Mike Isely56585382007-09-08 22:32:12 -03001971 pvr2_trace(PVR2_TRACE_STD,
Mike Iselyd8554972006-06-26 20:58:46 -03001972 "Initial video standard auto-selected to %s",
1973 hdw->std_defs[hdw->std_enum_cur-1].name);
1974 return;
1975 }
1976
Mike Isely0885ba12006-06-25 21:30:47 -03001977 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Iselyd8554972006-06-26 20:58:46 -03001978 "Unable to select a viable initial video standard");
1979}
1980
1981
Mike Iselye9c64a72009-03-06 23:42:20 -03001982static unsigned int pvr2_copy_i2c_addr_list(
1983 unsigned short *dst, const unsigned char *src,
1984 unsigned int dst_max)
1985{
Mike Isely3ab8d292009-03-07 01:37:58 -03001986 unsigned int cnt = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03001987 if (!src) return 0;
1988 while (src[cnt] && (cnt + 1) < dst_max) {
1989 dst[cnt] = src[cnt];
1990 cnt++;
1991 }
1992 dst[cnt] = I2C_CLIENT_END;
1993 return cnt;
1994}
1995
1996
Mike Isely1ab5e742009-03-07 00:24:24 -03001997static int pvr2_hdw_load_subdev(struct pvr2_hdw *hdw,
1998 const struct pvr2_device_client_desc *cd)
Mike Iselye9c64a72009-03-06 23:42:20 -03001999{
2000 const char *fname;
2001 unsigned char mid;
2002 struct v4l2_subdev *sd;
2003 unsigned int i2ccnt;
2004 const unsigned char *p;
2005 /* Arbitrary count - max # i2c addresses we will probe */
2006 unsigned short i2caddr[25];
2007
2008 mid = cd->module_id;
2009 fname = (mid < ARRAY_SIZE(module_names)) ? module_names[mid] : NULL;
2010 if (!fname) {
2011 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002012 "Module ID %u for device %s has no name",
Mike Iselye9c64a72009-03-06 23:42:20 -03002013 mid,
2014 hdw->hdw_desc->description);
Mike Isely1ab5e742009-03-07 00:24:24 -03002015 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002016 }
Mike Iselybd14d4f2009-03-07 00:56:52 -03002017 pvr2_trace(PVR2_TRACE_INIT,
2018 "Module ID %u (%s) for device %s being loaded...",
2019 mid, fname,
2020 hdw->hdw_desc->description);
Mike Iselye9c64a72009-03-06 23:42:20 -03002021
2022 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, cd->i2c_address_list,
2023 ARRAY_SIZE(i2caddr));
2024 if (!i2ccnt && ((p = (mid < ARRAY_SIZE(module_i2c_addresses)) ?
2025 module_i2c_addresses[mid] : NULL) != NULL)) {
2026 /* Second chance: Try default i2c address list */
2027 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, p,
2028 ARRAY_SIZE(i2caddr));
Mike Iselybd14d4f2009-03-07 00:56:52 -03002029 if (i2ccnt) {
2030 pvr2_trace(PVR2_TRACE_INIT,
2031 "Module ID %u:"
2032 " Using default i2c address list",
2033 mid);
2034 }
Mike Iselye9c64a72009-03-06 23:42:20 -03002035 }
2036
2037 if (!i2ccnt) {
2038 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002039 "Module ID %u (%s) for device %s:"
2040 " No i2c addresses",
2041 mid, fname, hdw->hdw_desc->description);
2042 return -EINVAL;
Mike Iselye9c64a72009-03-06 23:42:20 -03002043 }
2044
2045 /* Note how the 2nd and 3rd arguments are the same for both
2046 * v4l2_i2c_new_subdev() and v4l2_i2c_new_probed_subdev(). Why?
2047 * Well the 2nd argument is the module name to load, while the 3rd
2048 * argument is documented in the framework as being the "chipid" -
2049 * and every other place where I can find examples of this, the
2050 * "chipid" appears to just be the module name again. So here we
2051 * just do the same thing. */
Mike Isely0db85562009-03-07 01:42:40 -03002052 hdw->i2c_adap.class = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002053 if (i2ccnt == 1) {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002054 pvr2_trace(PVR2_TRACE_INIT,
2055 "Module ID %u:"
2056 " Setting up with specified i2c address 0x%x",
2057 mid, i2caddr[0]);
Mike Iselye9c64a72009-03-06 23:42:20 -03002058 sd = v4l2_i2c_new_subdev(&hdw->i2c_adap,
2059 fname, fname,
2060 i2caddr[0]);
2061 } else {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002062 pvr2_trace(PVR2_TRACE_INIT,
2063 "Module ID %u:"
2064 " Setting up with address probe list",
2065 mid);
Mike Iselye9c64a72009-03-06 23:42:20 -03002066 sd = v4l2_i2c_new_probed_subdev(&hdw->i2c_adap,
2067 fname, fname,
2068 i2caddr);
2069 }
Mike Isely0db85562009-03-07 01:42:40 -03002070 hdw->i2c_adap.class = I2C_CLASS_TV_ANALOG;
Mike Iselye9c64a72009-03-06 23:42:20 -03002071
Mike Isely446dfdc2009-03-06 23:58:15 -03002072 if (!sd) {
2073 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
Mike Isely1ab5e742009-03-07 00:24:24 -03002074 "Module ID %u (%s) for device %s failed to load",
2075 mid, fname, hdw->hdw_desc->description);
2076 return -EIO;
Mike Isely446dfdc2009-03-06 23:58:15 -03002077 }
2078
2079 /* Tag this sub-device instance with the module ID we know about.
2080 In other places we'll use that tag to determine if the instance
2081 requires special handling. */
2082 sd->grp_id = mid;
2083
Mike Iselya932f502009-03-06 23:47:10 -03002084 /* If we have both old and new i2c layers enabled, make sure that
2085 old layer isn't also tracking this module. This is a debugging
2086 aid, in normal situations there's no reason for both mechanisms
2087 to be enabled. */
2088 pvr2_i2c_untrack_subdev(hdw, sd);
Mike Iselybd14d4f2009-03-07 00:56:52 -03002089 pvr2_trace(PVR2_TRACE_INFO, "Attached sub-driver %s", fname);
Mike Iselya932f502009-03-06 23:47:10 -03002090
Mike Iselye9c64a72009-03-06 23:42:20 -03002091
Mike Isely00e5f732009-03-07 00:17:11 -03002092 /* client-specific setup... */
2093 switch (mid) {
2094 case PVR2_CLIENT_ID_CX25840:
2095 hdw->decoder_client_id = mid;
2096 {
2097 /*
2098 Mike Isely <isely@pobox.com> 19-Nov-2006 - This
2099 bit of nuttiness for cx25840 causes that module
2100 to correctly set up its video scaling. This is
2101 really a problem in the cx25840 module itself,
2102 but we work around it here. The problem has not
2103 been seen in ivtv because there VBI is supported
2104 and set up. We don't do VBI here (at least not
2105 yet) and thus we never attempted to even set it
2106 up.
2107 */
2108 struct v4l2_format fmt;
Mike Iselybd14d4f2009-03-07 00:56:52 -03002109 pvr2_trace(PVR2_TRACE_INIT,
2110 "Module ID %u:"
2111 " Executing cx25840 VBI hack",
2112 mid);
Mike Isely00e5f732009-03-07 00:17:11 -03002113 memset(&fmt, 0, sizeof(fmt));
2114 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
2115 v4l2_device_call_all(&hdw->v4l2_dev, mid,
2116 video, s_fmt, &fmt);
2117 }
2118 break;
2119 case PVR2_CLIENT_ID_SAA7115:
2120 hdw->decoder_client_id = mid;
2121 break;
2122 default: break;
2123 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002124
2125 return 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002126}
2127
2128
2129static void pvr2_hdw_load_modules(struct pvr2_hdw *hdw)
2130{
2131 unsigned int idx;
2132 const struct pvr2_string_table *cm;
2133 const struct pvr2_device_client_table *ct;
Mike Isely1ab5e742009-03-07 00:24:24 -03002134 int okFl = !0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002135
2136 cm = &hdw->hdw_desc->client_modules;
2137 for (idx = 0; idx < cm->cnt; idx++) {
2138 request_module(cm->lst[idx]);
2139 }
2140
2141 ct = &hdw->hdw_desc->client_table;
2142 for (idx = 0; idx < ct->cnt; idx++) {
Mike Iselybd14d4f2009-03-07 00:56:52 -03002143 if (pvr2_hdw_load_subdev(hdw, &ct->lst[idx]) < 0) okFl = 0;
Mike Iselye9c64a72009-03-06 23:42:20 -03002144 }
Mike Isely1ab5e742009-03-07 00:24:24 -03002145 if (!okFl) pvr2_hdw_render_useless(hdw);
Mike Iselye9c64a72009-03-06 23:42:20 -03002146}
2147
2148
Mike Iselyd8554972006-06-26 20:58:46 -03002149static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
2150{
2151 int ret;
2152 unsigned int idx;
2153 struct pvr2_ctrl *cptr;
2154 int reloadFl = 0;
Mike Isely989eb152007-11-26 01:53:12 -03002155 if (hdw->hdw_desc->fx2_firmware.cnt) {
Mike Isely1d643a32007-09-08 22:18:50 -03002156 if (!reloadFl) {
2157 reloadFl =
2158 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
2159 == 0);
2160 if (reloadFl) {
2161 pvr2_trace(PVR2_TRACE_INIT,
2162 "USB endpoint config looks strange"
2163 "; possibly firmware needs to be"
2164 " loaded");
2165 }
2166 }
2167 if (!reloadFl) {
2168 reloadFl = !pvr2_hdw_check_firmware(hdw);
2169 if (reloadFl) {
2170 pvr2_trace(PVR2_TRACE_INIT,
2171 "Check for FX2 firmware failed"
2172 "; possibly firmware needs to be"
2173 " loaded");
2174 }
2175 }
Mike Iselyd8554972006-06-26 20:58:46 -03002176 if (reloadFl) {
Mike Isely1d643a32007-09-08 22:18:50 -03002177 if (pvr2_upload_firmware1(hdw) != 0) {
2178 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2179 "Failure uploading firmware1");
2180 }
2181 return;
Mike Iselyd8554972006-06-26 20:58:46 -03002182 }
2183 }
Mike Iselyd8554972006-06-26 20:58:46 -03002184 hdw->fw1_state = FW1_STATE_OK;
2185
Mike Iselyd8554972006-06-26 20:58:46 -03002186 if (!pvr2_hdw_dev_ok(hdw)) return;
2187
Mike Isely27764722009-03-07 01:57:25 -03002188 hdw->force_dirty = !0;
2189
Mike Isely989eb152007-11-26 01:53:12 -03002190 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002191 pvr2_hdw_cmd_powerup(hdw);
2192 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002193 }
2194
Mike Isely31335b12008-07-25 19:35:31 -03002195 /* Take the IR chip out of reset, if appropriate */
2196 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
2197 pvr2_issue_simple_cmd(hdw,
2198 FX2CMD_HCW_ZILOG_RESET |
2199 (1 << 8) |
2200 ((0) << 16));
2201 }
2202
Mike Iselyd8554972006-06-26 20:58:46 -03002203 // This step MUST happen after the earlier powerup step.
Mike Isely59af3362009-03-07 03:06:09 -03002204 pvr2_i2c_track_init(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002205 pvr2_i2c_core_init(hdw);
2206 if (!pvr2_hdw_dev_ok(hdw)) return;
2207
Mike Iselye9c64a72009-03-06 23:42:20 -03002208 pvr2_hdw_load_modules(hdw);
Mike Isely1ab5e742009-03-07 00:24:24 -03002209 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselye9c64a72009-03-06 23:42:20 -03002210
Mike Isely5c6cb4e2009-03-07 01:59:34 -03002211 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, init, 0);
2212
Mike Iselyc05c0462006-06-25 20:04:25 -03002213 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002214 cptr = hdw->controls + idx;
2215 if (cptr->info->skip_init) continue;
2216 if (!cptr->info->set_value) continue;
2217 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2218 }
2219
Mike Isely1bde0282006-12-27 23:30:13 -03002220 /* Set up special default values for the television and radio
2221 frequencies here. It's not really important what these defaults
2222 are, but I set them to something usable in the Chicago area just
2223 to make driver testing a little easier. */
2224
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002225 hdw->freqValTelevision = default_tv_freq;
2226 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002227
Mike Iselyd8554972006-06-26 20:58:46 -03002228 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2229 // thread-safe against the normal pvr2_send_request() mechanism.
2230 // (We should make it thread safe).
2231
Mike Iselyaaf78842007-11-26 02:04:11 -03002232 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2233 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002234 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002235 if (ret < 0) {
2236 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2237 "Unable to determine location of eeprom,"
2238 " skipping");
2239 } else {
2240 hdw->eeprom_addr = ret;
2241 pvr2_eeprom_analyze(hdw);
2242 if (!pvr2_hdw_dev_ok(hdw)) return;
2243 }
2244 } else {
2245 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2246 hdw->tuner_updated = !0;
2247 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002248 }
2249
Mike Isely13a88792009-01-14 04:22:56 -03002250 if (hdw->serial_number) {
2251 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2252 "sn-%lu", hdw->serial_number);
2253 } else if (hdw->unit_number >= 0) {
2254 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2255 "unit-%c",
2256 hdw->unit_number + 'a');
2257 } else {
2258 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2259 "unit-??");
2260 }
2261 hdw->identifier[idx] = 0;
2262
Mike Iselyd8554972006-06-26 20:58:46 -03002263 pvr2_hdw_setup_std(hdw);
2264
2265 if (!get_default_tuner_type(hdw)) {
2266 pvr2_trace(PVR2_TRACE_INIT,
2267 "pvr2_hdw_setup: Tuner type overridden to %d",
2268 hdw->tuner_type);
2269 }
2270
Mike Iselyd8554972006-06-26 20:58:46 -03002271 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002272
2273 if (!pvr2_hdw_dev_ok(hdw)) return;
2274
Mike Isely1df59f02008-04-21 03:50:39 -03002275 if (hdw->hdw_desc->signal_routing_scheme ==
2276 PVR2_ROUTING_SCHEME_GOTVIEW) {
2277 /* Ensure that GPIO 11 is set to output for GOTVIEW
2278 hardware. */
2279 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2280 }
2281
Mike Isely681c7392007-11-26 01:48:52 -03002282 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002283
2284 hdw->vid_stream = pvr2_stream_create();
2285 if (!pvr2_hdw_dev_ok(hdw)) return;
2286 pvr2_trace(PVR2_TRACE_INIT,
2287 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2288 if (hdw->vid_stream) {
2289 idx = get_default_error_tolerance(hdw);
2290 if (idx) {
2291 pvr2_trace(PVR2_TRACE_INIT,
2292 "pvr2_hdw_setup: video stream %p"
2293 " setting tolerance %u",
2294 hdw->vid_stream,idx);
2295 }
2296 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2297 PVR2_VID_ENDPOINT,idx);
2298 }
2299
2300 if (!pvr2_hdw_dev_ok(hdw)) return;
2301
Mike Iselyd8554972006-06-26 20:58:46 -03002302 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002303
2304 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002305}
2306
2307
Mike Isely681c7392007-11-26 01:48:52 -03002308/* Set up the structure and attempt to put the device into a usable state.
2309 This can be a time-consuming operation, which is why it is not done
2310 internally as part of the create() step. */
2311static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002312{
2313 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002314 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002315 pvr2_hdw_setup_low(hdw);
2316 pvr2_trace(PVR2_TRACE_INIT,
2317 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002318 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002319 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002320 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002321 pvr2_trace(
2322 PVR2_TRACE_INFO,
2323 "Device initialization"
2324 " completed successfully.");
2325 break;
2326 }
2327 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2328 pvr2_trace(
2329 PVR2_TRACE_INFO,
2330 "Device microcontroller firmware"
2331 " (re)loaded; it should now reset"
2332 " and reconnect.");
2333 break;
2334 }
2335 pvr2_trace(
2336 PVR2_TRACE_ERROR_LEGS,
2337 "Device initialization was not successful.");
2338 if (hdw->fw1_state == FW1_STATE_MISSING) {
2339 pvr2_trace(
2340 PVR2_TRACE_ERROR_LEGS,
2341 "Giving up since device"
2342 " microcontroller firmware"
2343 " appears to be missing.");
2344 break;
2345 }
2346 }
2347 if (procreload) {
2348 pvr2_trace(
2349 PVR2_TRACE_ERROR_LEGS,
2350 "Attempting pvrusb2 recovery by reloading"
2351 " primary firmware.");
2352 pvr2_trace(
2353 PVR2_TRACE_ERROR_LEGS,
2354 "If this works, device should disconnect"
2355 " and reconnect in a sane state.");
2356 hdw->fw1_state = FW1_STATE_UNKNOWN;
2357 pvr2_upload_firmware1(hdw);
2358 } else {
2359 pvr2_trace(
2360 PVR2_TRACE_ERROR_LEGS,
2361 "***WARNING*** pvrusb2 device hardware"
2362 " appears to be jammed"
2363 " and I can't clear it.");
2364 pvr2_trace(
2365 PVR2_TRACE_ERROR_LEGS,
2366 "You might need to power cycle"
2367 " the pvrusb2 device"
2368 " in order to recover.");
2369 }
Mike Isely681c7392007-11-26 01:48:52 -03002370 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002371 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002372}
2373
2374
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002375/* Perform second stage initialization. Set callback pointer first so that
2376 we can avoid a possible initialization race (if the kernel thread runs
2377 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002378int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2379 void (*callback_func)(void *),
2380 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002381{
2382 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002383 if (hdw->flag_disconnected) {
2384 /* Handle a race here: If we're already
2385 disconnected by this point, then give up. If we
2386 get past this then we'll remain connected for
2387 the duration of initialization since the entire
2388 initialization sequence is now protected by the
2389 big_lock. */
2390 break;
2391 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002392 hdw->state_data = callback_data;
2393 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002394 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002395 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002396 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002397}
2398
2399
2400/* Create, set up, and return a structure for interacting with the
2401 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002402struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2403 const struct usb_device_id *devid)
2404{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002405 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002406 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002407 int valid_std_mask;
2408 struct pvr2_ctrl *cptr;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002409 struct usb_device *usb_dev;
Mike Isely989eb152007-11-26 01:53:12 -03002410 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002411 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002412 struct v4l2_queryctrl qctrl;
2413 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002414
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002415 usb_dev = interface_to_usbdev(intf);
2416
Mike Iselyd130fa82007-12-08 17:20:06 -03002417 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002418
Mike Iselyfe15f132008-08-30 18:11:40 -03002419 if (hdw_desc == NULL) {
2420 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2421 " No device description pointer,"
2422 " unable to continue.");
2423 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2424 " please contact Mike Isely <isely@pobox.com>"
2425 " to get it included in the driver\n");
2426 goto fail;
2427 }
2428
Mike Iselyca545f72007-01-20 00:37:11 -03002429 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002430 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002431 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002432 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002433
2434 init_timer(&hdw->quiescent_timer);
2435 hdw->quiescent_timer.data = (unsigned long)hdw;
2436 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2437
2438 init_timer(&hdw->encoder_wait_timer);
2439 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2440 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2441
Mike Iselyd913d632008-04-06 04:04:35 -03002442 init_timer(&hdw->encoder_run_timer);
2443 hdw->encoder_run_timer.data = (unsigned long)hdw;
2444 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2445
Mike Isely681c7392007-11-26 01:48:52 -03002446 hdw->master_state = PVR2_STATE_DEAD;
2447
2448 init_waitqueue_head(&hdw->state_wait_data);
2449
Mike Isely18103c572007-01-20 00:09:47 -03002450 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002451 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002452
Mike Isely7fb20fa2008-04-22 14:45:37 -03002453 /* Calculate which inputs are OK */
2454 m = 0;
2455 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002456 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2457 m |= 1 << PVR2_CVAL_INPUT_DTV;
2458 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002459 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2460 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2461 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2462 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002463 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002464
Mike Isely62433e32008-04-22 14:45:40 -03002465 /* If not a hybrid device, pathway_state never changes. So
2466 initialize it here to what it should forever be. */
2467 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2468 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2469 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2470 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2471 }
2472
Mike Iselyc05c0462006-06-25 20:04:25 -03002473 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002474 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002475 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002476 GFP_KERNEL);
2477 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002478 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002479 for (idx = 0; idx < hdw->control_cnt; idx++) {
2480 cptr = hdw->controls + idx;
2481 cptr->hdw = hdw;
2482 }
Mike Iselyd8554972006-06-26 20:58:46 -03002483 for (idx = 0; idx < 32; idx++) {
2484 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2485 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002486 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002487 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002488 cptr->info = control_defs+idx;
2489 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002490
2491 /* Ensure that default input choice is a valid one. */
2492 m = hdw->input_avail_mask;
2493 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2494 if (!((1 << idx) & m)) continue;
2495 hdw->input_val = idx;
2496 break;
2497 }
2498
Mike Iselyb30d2442006-06-25 20:05:01 -03002499 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002500 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002501 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2502 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002503 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2504 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2505 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2506 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2507 ciptr->name = mpeg_ids[idx].strid;
2508 ciptr->v4l_id = mpeg_ids[idx].id;
2509 ciptr->skip_init = !0;
2510 ciptr->get_value = ctrl_cx2341x_get;
2511 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2512 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2513 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2514 qctrl.id = ciptr->v4l_id;
2515 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2516 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2517 ciptr->set_value = ctrl_cx2341x_set;
2518 }
2519 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2520 PVR2_CTLD_INFO_DESC_SIZE);
2521 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2522 ciptr->default_value = qctrl.default_value;
2523 switch (qctrl.type) {
2524 default:
2525 case V4L2_CTRL_TYPE_INTEGER:
2526 ciptr->type = pvr2_ctl_int;
2527 ciptr->def.type_int.min_value = qctrl.minimum;
2528 ciptr->def.type_int.max_value = qctrl.maximum;
2529 break;
2530 case V4L2_CTRL_TYPE_BOOLEAN:
2531 ciptr->type = pvr2_ctl_bool;
2532 break;
2533 case V4L2_CTRL_TYPE_MENU:
2534 ciptr->type = pvr2_ctl_enum;
2535 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002536 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2537 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002538 for (cnt1 = 0;
2539 ciptr->def.type_enum.value_names[cnt1] != NULL;
2540 cnt1++) { }
2541 ciptr->def.type_enum.count = cnt1;
2542 break;
2543 }
2544 cptr->info = ciptr;
2545 }
Mike Iselyd8554972006-06-26 20:58:46 -03002546
2547 // Initialize video standard enum dynamic control
2548 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2549 if (cptr) {
2550 memcpy(&hdw->std_info_enum,cptr->info,
2551 sizeof(hdw->std_info_enum));
2552 cptr->info = &hdw->std_info_enum;
2553
2554 }
2555 // Initialize control data regarding video standard masks
2556 valid_std_mask = pvr2_std_get_usable();
2557 for (idx = 0; idx < 32; idx++) {
2558 if (!(valid_std_mask & (1 << idx))) continue;
2559 cnt1 = pvr2_std_id_to_str(
2560 hdw->std_mask_names[idx],
2561 sizeof(hdw->std_mask_names[idx])-1,
2562 1 << idx);
2563 hdw->std_mask_names[idx][cnt1] = 0;
2564 }
2565 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2566 if (cptr) {
2567 memcpy(&hdw->std_info_avail,cptr->info,
2568 sizeof(hdw->std_info_avail));
2569 cptr->info = &hdw->std_info_avail;
2570 hdw->std_info_avail.def.type_bitmask.bit_names =
2571 hdw->std_mask_ptrs;
2572 hdw->std_info_avail.def.type_bitmask.valid_bits =
2573 valid_std_mask;
2574 }
2575 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2576 if (cptr) {
2577 memcpy(&hdw->std_info_cur,cptr->info,
2578 sizeof(hdw->std_info_cur));
2579 cptr->info = &hdw->std_info_cur;
2580 hdw->std_info_cur.def.type_bitmask.bit_names =
2581 hdw->std_mask_ptrs;
2582 hdw->std_info_avail.def.type_bitmask.valid_bits =
2583 valid_std_mask;
2584 }
2585
Mike Isely432907f2008-08-31 21:02:20 -03002586 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002587 hdw->eeprom_addr = -1;
2588 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002589 hdw->v4l_minor_number_video = -1;
2590 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002591 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002592 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2593 if (!hdw->ctl_write_buffer) goto fail;
2594 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2595 if (!hdw->ctl_read_buffer) goto fail;
2596 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2597 if (!hdw->ctl_write_urb) goto fail;
2598 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2599 if (!hdw->ctl_read_urb) goto fail;
2600
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002601 if (v4l2_device_register(&usb_dev->dev, &hdw->v4l2_dev) != 0) {
2602 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2603 "Error registering with v4l core, giving up");
2604 goto fail;
2605 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002606 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002607 for (idx = 0; idx < PVR_NUM; idx++) {
2608 if (unit_pointers[idx]) continue;
2609 hdw->unit_number = idx;
2610 unit_pointers[idx] = hdw;
2611 break;
2612 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002613 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002614
2615 cnt1 = 0;
2616 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2617 cnt1 += cnt2;
2618 if (hdw->unit_number >= 0) {
2619 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2620 ('a' + hdw->unit_number));
2621 cnt1 += cnt2;
2622 }
2623 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2624 hdw->name[cnt1] = 0;
2625
Mike Isely681c7392007-11-26 01:48:52 -03002626 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2627 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2628 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002629
Mike Iselyd8554972006-06-26 20:58:46 -03002630 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2631 hdw->unit_number,hdw->name);
2632
2633 hdw->tuner_type = -1;
2634 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002635
2636 hdw->usb_intf = intf;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002637 hdw->usb_dev = usb_dev;
Mike Iselyd8554972006-06-26 20:58:46 -03002638
Mike Isely87e34952009-01-23 01:20:24 -03002639 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
Mike Isely31a18542007-04-08 01:11:47 -03002640
Mike Iselyd8554972006-06-26 20:58:46 -03002641 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2642 usb_set_interface(hdw->usb_dev,ifnum,0);
2643
2644 mutex_init(&hdw->ctl_lock_mutex);
2645 mutex_init(&hdw->big_lock_mutex);
2646
2647 return hdw;
2648 fail:
2649 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002650 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002651 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002652 del_timer_sync(&hdw->encoder_wait_timer);
2653 if (hdw->workqueue) {
2654 flush_workqueue(hdw->workqueue);
2655 destroy_workqueue(hdw->workqueue);
2656 hdw->workqueue = NULL;
2657 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002658 usb_free_urb(hdw->ctl_read_urb);
2659 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002660 kfree(hdw->ctl_read_buffer);
2661 kfree(hdw->ctl_write_buffer);
2662 kfree(hdw->controls);
2663 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002664 kfree(hdw->std_defs);
2665 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002666 kfree(hdw);
2667 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002668 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002669}
2670
2671
2672/* Remove _all_ associations between this driver and the underlying USB
2673 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002674static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002675{
2676 if (hdw->flag_disconnected) return;
2677 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2678 if (hdw->ctl_read_urb) {
2679 usb_kill_urb(hdw->ctl_read_urb);
2680 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002681 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002682 }
2683 if (hdw->ctl_write_urb) {
2684 usb_kill_urb(hdw->ctl_write_urb);
2685 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002686 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002687 }
2688 if (hdw->ctl_read_buffer) {
2689 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002690 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002691 }
2692 if (hdw->ctl_write_buffer) {
2693 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002694 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002695 }
Mike Iselyd8554972006-06-26 20:58:46 -03002696 hdw->flag_disconnected = !0;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002697 /* If we don't do this, then there will be a dangling struct device
2698 reference to our disappearing device persisting inside the V4L
2699 core... */
2700 if (hdw->v4l2_dev.dev) {
2701 dev_set_drvdata(hdw->v4l2_dev.dev, NULL);
2702 hdw->v4l2_dev.dev = NULL;
2703 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002704 hdw->usb_dev = NULL;
2705 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002706 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002707}
2708
2709
2710/* Destroy hardware interaction structure */
2711void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2712{
Mike Isely401c27c2007-09-08 22:11:46 -03002713 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002714 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002715 if (hdw->workqueue) {
2716 flush_workqueue(hdw->workqueue);
2717 destroy_workqueue(hdw->workqueue);
2718 hdw->workqueue = NULL;
2719 }
Mike Isely8f591002008-04-22 14:45:45 -03002720 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002721 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002722 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002723 if (hdw->fw_buffer) {
2724 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002725 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002726 }
2727 if (hdw->vid_stream) {
2728 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002729 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002730 }
Mike Iselyd8554972006-06-26 20:58:46 -03002731 if (hdw->decoder_ctrl) {
2732 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2733 }
2734 pvr2_i2c_core_done(hdw);
Mike Isely59af3362009-03-07 03:06:09 -03002735 pvr2_i2c_track_done(hdw);
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002736 v4l2_device_unregister(&hdw->v4l2_dev);
Mike Iselyd8554972006-06-26 20:58:46 -03002737 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002738 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002739 if ((hdw->unit_number >= 0) &&
2740 (hdw->unit_number < PVR_NUM) &&
2741 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002742 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002743 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002744 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002745 kfree(hdw->controls);
2746 kfree(hdw->mpeg_ctrl_info);
2747 kfree(hdw->std_defs);
2748 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002749 kfree(hdw);
2750}
2751
2752
Mike Iselyd8554972006-06-26 20:58:46 -03002753int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2754{
2755 return (hdw && hdw->flag_ok);
2756}
2757
2758
2759/* Called when hardware has been unplugged */
2760void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2761{
2762 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2763 LOCK_TAKE(hdw->big_lock);
2764 LOCK_TAKE(hdw->ctl_lock);
2765 pvr2_hdw_remove_usb_stuff(hdw);
2766 LOCK_GIVE(hdw->ctl_lock);
2767 LOCK_GIVE(hdw->big_lock);
2768}
2769
2770
2771// Attempt to autoselect an appropriate value for std_enum_cur given
2772// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002773static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002774{
2775 unsigned int idx;
2776 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2777 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2778 hdw->std_enum_cur = idx;
2779 return;
2780 }
2781 }
2782 hdw->std_enum_cur = 0;
2783}
2784
2785
2786// Calculate correct set of enumerated standards based on currently known
2787// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002788static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002789{
2790 struct v4l2_standard *newstd;
2791 unsigned int std_cnt;
2792 unsigned int idx;
2793
2794 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2795
2796 if (hdw->std_defs) {
2797 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002798 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002799 }
2800 hdw->std_enum_cnt = 0;
2801 if (hdw->std_enum_names) {
2802 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002803 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002804 }
2805
2806 if (!std_cnt) {
2807 pvr2_trace(
2808 PVR2_TRACE_ERROR_LEGS,
2809 "WARNING: Failed to identify any viable standards");
2810 }
2811 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2812 hdw->std_enum_names[0] = "none";
2813 for (idx = 0; idx < std_cnt; idx++) {
2814 hdw->std_enum_names[idx+1] =
2815 newstd[idx].name;
2816 }
2817 // Set up the dynamic control for this standard
2818 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2819 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2820 hdw->std_defs = newstd;
2821 hdw->std_enum_cnt = std_cnt+1;
2822 hdw->std_enum_cur = 0;
2823 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2824}
2825
2826
2827int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2828 struct v4l2_standard *std,
2829 unsigned int idx)
2830{
2831 int ret = -EINVAL;
2832 if (!idx) return ret;
2833 LOCK_TAKE(hdw->big_lock); do {
2834 if (idx >= hdw->std_enum_cnt) break;
2835 idx--;
2836 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2837 ret = 0;
2838 } while (0); LOCK_GIVE(hdw->big_lock);
2839 return ret;
2840}
2841
2842
2843/* Get the number of defined controls */
2844unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2845{
Mike Iselyc05c0462006-06-25 20:04:25 -03002846 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002847}
2848
2849
2850/* Retrieve a control handle given its index (0..count-1) */
2851struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2852 unsigned int idx)
2853{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002854 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002855 return hdw->controls + idx;
2856}
2857
2858
2859/* Retrieve a control handle given its index (0..count-1) */
2860struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2861 unsigned int ctl_id)
2862{
2863 struct pvr2_ctrl *cptr;
2864 unsigned int idx;
2865 int i;
2866
2867 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002868 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002869 cptr = hdw->controls + idx;
2870 i = cptr->info->internal_id;
2871 if (i && (i == ctl_id)) return cptr;
2872 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002873 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002874}
2875
2876
Mike Iselya761f432006-06-25 20:04:44 -03002877/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002878struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2879{
2880 struct pvr2_ctrl *cptr;
2881 unsigned int idx;
2882 int i;
2883
2884 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002885 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002886 cptr = hdw->controls + idx;
2887 i = cptr->info->v4l_id;
2888 if (i && (i == ctl_id)) return cptr;
2889 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002890 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002891}
2892
2893
Mike Iselya761f432006-06-25 20:04:44 -03002894/* Given a V4L ID for its immediate predecessor, retrieve the control
2895 structure associated with it. */
2896struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2897 unsigned int ctl_id)
2898{
2899 struct pvr2_ctrl *cptr,*cp2;
2900 unsigned int idx;
2901 int i;
2902
2903 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002904 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002905 for (idx = 0; idx < hdw->control_cnt; idx++) {
2906 cptr = hdw->controls + idx;
2907 i = cptr->info->v4l_id;
2908 if (!i) continue;
2909 if (i <= ctl_id) continue;
2910 if (cp2 && (cp2->info->v4l_id < i)) continue;
2911 cp2 = cptr;
2912 }
2913 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002914 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002915}
2916
2917
Mike Iselyd8554972006-06-26 20:58:46 -03002918static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2919{
2920 switch (tp) {
2921 case pvr2_ctl_int: return "integer";
2922 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002923 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002924 case pvr2_ctl_bitmask: return "bitmask";
2925 }
2926 return "";
2927}
2928
2929
Mike Isely2641df32009-03-07 00:13:25 -03002930static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2931 const char *name, int val)
2932{
2933 struct v4l2_control ctrl;
2934 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2935 memset(&ctrl, 0, sizeof(ctrl));
2936 ctrl.id = id;
2937 ctrl.value = val;
2938 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
2939}
2940
2941#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
Mike Isely27764722009-03-07 01:57:25 -03002942 if ((hdw)->lab##_dirty || (hdw)->force_dirty) { \
Mike Isely2641df32009-03-07 00:13:25 -03002943 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
2944 }
2945
Mike Isely5ceaad12009-03-07 00:01:20 -03002946/* Execute whatever commands are required to update the state of all the
Mike Isely2641df32009-03-07 00:13:25 -03002947 sub-devices so that they match our current control values. */
Mike Isely5ceaad12009-03-07 00:01:20 -03002948static void pvr2_subdev_update(struct pvr2_hdw *hdw)
2949{
Mike Iselyedb9dcb2009-03-07 00:37:10 -03002950 struct v4l2_subdev *sd;
2951 unsigned int id;
2952 pvr2_subdev_update_func fp;
2953
Mike Isely75212a02009-03-07 01:48:42 -03002954 pvr2_trace(PVR2_TRACE_CHIPS, "subdev update...");
2955
Mike Isely27764722009-03-07 01:57:25 -03002956 if (hdw->tuner_updated || hdw->force_dirty) {
Mike Isely75212a02009-03-07 01:48:42 -03002957 struct tuner_setup setup;
2958 pvr2_trace(PVR2_TRACE_CHIPS, "subdev tuner set_type(%d)",
2959 hdw->tuner_type);
2960 if (((int)(hdw->tuner_type)) >= 0) {
2961 setup.addr = ADDR_UNSET;
2962 setup.type = hdw->tuner_type;
2963 setup.mode_mask = T_RADIO | T_ANALOG_TV;
2964 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2965 tuner, s_type_addr, &setup);
2966 }
2967 }
2968
Mike Isely27764722009-03-07 01:57:25 -03002969 if (hdw->input_dirty || hdw->std_dirty || hdw->force_dirty) {
Mike Iselyb4818802009-03-07 01:46:17 -03002970 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_standard");
Mike Isely2641df32009-03-07 00:13:25 -03002971 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2972 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2973 tuner, s_radio);
2974 } else {
2975 v4l2_std_id vs;
2976 vs = hdw->std_mask_cur;
2977 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2978 tuner, s_std, vs);
2979 }
2980 hdw->tuner_signal_stale = !0;
2981 hdw->cropcap_stale = !0;
2982 }
2983
2984 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
2985 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
2986 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
2987 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
2988 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
2989 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
2990 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
2991 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
2992 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
2993
Mike Isely27764722009-03-07 01:57:25 -03002994 if (hdw->input_dirty || hdw->audiomode_dirty || hdw->force_dirty) {
Mike Isely2641df32009-03-07 00:13:25 -03002995 struct v4l2_tuner vt;
2996 memset(&vt, 0, sizeof(vt));
2997 vt.audmode = hdw->audiomode_val;
2998 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
2999 }
3000
Mike Isely27764722009-03-07 01:57:25 -03003001 if (hdw->freqDirty || hdw->force_dirty) {
Mike Isely2641df32009-03-07 00:13:25 -03003002 unsigned long fv;
3003 struct v4l2_frequency freq;
3004 fv = pvr2_hdw_get_cur_freq(hdw);
3005 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
3006 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
3007 memset(&freq, 0, sizeof(freq));
3008 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
3009 /* ((fv * 1000) / 62500) */
3010 freq.frequency = (fv * 2) / 125;
3011 } else {
3012 freq.frequency = fv / 62500;
3013 }
3014 /* tuner-core currently doesn't seem to care about this, but
3015 let's set it anyway for completeness. */
3016 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3017 freq.type = V4L2_TUNER_RADIO;
3018 } else {
3019 freq.type = V4L2_TUNER_ANALOG_TV;
3020 }
3021 freq.tuner = 0;
3022 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
3023 s_frequency, &freq);
3024 }
3025
Mike Isely27764722009-03-07 01:57:25 -03003026 if (hdw->res_hor_dirty || hdw->res_ver_dirty || hdw->force_dirty) {
Mike Isely2641df32009-03-07 00:13:25 -03003027 struct v4l2_format fmt;
3028 memset(&fmt, 0, sizeof(fmt));
3029 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3030 fmt.fmt.pix.width = hdw->res_hor_val;
3031 fmt.fmt.pix.height = hdw->res_ver_val;
3032 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_size(%dx%d)",
3033 fmt.fmt.pix.width, fmt.fmt.pix.height);
3034 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_fmt, &fmt);
3035 }
3036
Mike Isely27764722009-03-07 01:57:25 -03003037 if (hdw->srate_dirty || hdw->force_dirty) {
Mike Isely01c59df2009-03-07 00:48:09 -03003038 u32 val;
3039 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_audio %d",
3040 hdw->srate_val);
3041 switch (hdw->srate_val) {
3042 default:
3043 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
3044 val = 48000;
3045 break;
3046 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
3047 val = 44100;
3048 break;
3049 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
3050 val = 32000;
3051 break;
3052 }
3053 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3054 audio, s_clock_freq, val);
3055 }
3056
Mike Isely2641df32009-03-07 00:13:25 -03003057 /* Unable to set crop parameters; there is apparently no equivalent
3058 for VIDIOC_S_CROP */
3059
Mike Iselyedb9dcb2009-03-07 00:37:10 -03003060 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
3061 id = sd->grp_id;
3062 if (id >= ARRAY_SIZE(pvr2_module_update_functions)) continue;
3063 fp = pvr2_module_update_functions[id];
3064 if (!fp) continue;
3065 (*fp)(hdw, sd);
3066 }
Mike Isely2641df32009-03-07 00:13:25 -03003067
Mike Isely27764722009-03-07 01:57:25 -03003068 if (hdw->tuner_signal_stale || hdw->cropcap_stale) {
Mike Isely2641df32009-03-07 00:13:25 -03003069 pvr2_hdw_status_poll(hdw);
3070 }
Mike Isely5ceaad12009-03-07 00:01:20 -03003071}
3072
3073
Mike Isely681c7392007-11-26 01:48:52 -03003074/* Figure out if we need to commit control changes. If so, mark internal
3075 state flags to indicate this fact and return true. Otherwise do nothing
3076 else and return false. */
3077static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003078{
Mike Iselyd8554972006-06-26 20:58:46 -03003079 unsigned int idx;
3080 struct pvr2_ctrl *cptr;
3081 int value;
Mike Isely27764722009-03-07 01:57:25 -03003082 int commit_flag = hdw->force_dirty;
Mike Iselyd8554972006-06-26 20:58:46 -03003083 char buf[100];
3084 unsigned int bcnt,ccnt;
3085
Mike Iselyc05c0462006-06-25 20:04:25 -03003086 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03003087 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00003088 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003089 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03003090 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003091
Mike Iselyfe23a282007-01-20 00:10:55 -03003092 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003093 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3094 cptr->info->name);
3095 value = 0;
3096 cptr->info->get_value(cptr,&value);
3097 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3098 buf+bcnt,
3099 sizeof(buf)-bcnt,&ccnt);
3100 bcnt += ccnt;
3101 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3102 get_ctrl_typename(cptr->info->type));
3103 pvr2_trace(PVR2_TRACE_CTL,
3104 "/*--TRACE_COMMIT--*/ %.*s",
3105 bcnt,buf);
3106 }
3107
3108 if (!commit_flag) {
3109 /* Nothing has changed */
3110 return 0;
3111 }
3112
Mike Isely681c7392007-11-26 01:48:52 -03003113 hdw->state_pipeline_config = 0;
3114 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3115 pvr2_hdw_state_sched(hdw);
3116
3117 return !0;
3118}
3119
3120
3121/* Perform all operations needed to commit all control changes. This must
3122 be performed in synchronization with the pipeline state and is thus
3123 expected to be called as part of the driver's worker thread. Return
3124 true if commit successful, otherwise return false to indicate that
3125 commit isn't possible at this time. */
3126static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3127{
3128 unsigned int idx;
3129 struct pvr2_ctrl *cptr;
3130 int disruptive_change;
3131
Mike Iselyab062fe2008-06-30 03:32:35 -03003132 /* Handle some required side effects when the video standard is
3133 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03003134 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03003135 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03003136 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03003137 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3138 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03003139 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03003140 } else {
3141 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03003142 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03003143 }
Mike Isely00528d92008-06-30 03:35:52 -03003144 /* Rewrite the vertical resolution to be appropriate to the
3145 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03003146 if (nvres != hdw->res_ver_val) {
3147 hdw->res_ver_val = nvres;
3148 hdw->res_ver_dirty = !0;
3149 }
Mike Isely00528d92008-06-30 03:35:52 -03003150 /* Rewrite the GOP size to be appropriate to the video
3151 standard that has been selected. */
3152 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3153 struct v4l2_ext_controls cs;
3154 struct v4l2_ext_control c1;
3155 memset(&cs, 0, sizeof(cs));
3156 memset(&c1, 0, sizeof(c1));
3157 cs.controls = &c1;
3158 cs.count = 1;
3159 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3160 c1.value = gop_size;
3161 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3162 VIDIOC_S_EXT_CTRLS);
3163 }
Mike Iselyd8554972006-06-26 20:58:46 -03003164 }
3165
Mike Isely38d9a2c2008-03-28 05:30:48 -03003166 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03003167 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3168 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3169 hdw->pathway_state)) {
3170 /* Change of mode being asked for... */
3171 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003172 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003173 }
3174 if (!hdw->state_pathway_ok) {
3175 /* Can't commit anything until pathway is ok. */
3176 return 0;
3177 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03003178 /* The broadcast decoder can only scale down, so if
3179 * res_*_dirty && crop window < output format ==> enlarge crop.
3180 *
3181 * The mpeg encoder receives fields of res_hor_val dots and
3182 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3183 */
3184 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3185 hdw->cropw_val = hdw->res_hor_val;
3186 hdw->cropw_dirty = !0;
3187 } else if (hdw->cropw_dirty) {
3188 hdw->res_hor_dirty = !0; /* must rescale */
3189 hdw->res_hor_val = min(720, hdw->cropw_val);
3190 }
3191 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3192 hdw->croph_val = hdw->res_ver_val;
3193 hdw->croph_dirty = !0;
3194 } else if (hdw->croph_dirty) {
3195 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3196 hdw->res_ver_dirty = !0;
3197 hdw->res_ver_val = min(nvres, hdw->croph_val);
3198 }
3199
Mike Isely681c7392007-11-26 01:48:52 -03003200 /* If any of the below has changed, then we can't do the update
3201 while the pipeline is running. Pipeline must be paused first
3202 and decoder -> encoder connection be made quiescent before we
3203 can proceed. */
3204 disruptive_change =
3205 (hdw->std_dirty ||
3206 hdw->enc_unsafe_stale ||
3207 hdw->srate_dirty ||
3208 hdw->res_ver_dirty ||
3209 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03003210 hdw->cropw_dirty ||
3211 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03003212 hdw->input_dirty ||
3213 (hdw->active_stream_type != hdw->desired_stream_type));
3214 if (disruptive_change && !hdw->state_pipeline_idle) {
3215 /* Pipeline is not idle; we can't proceed. Arrange to
3216 cause pipeline to stop so that we can try this again
3217 later.... */
3218 hdw->state_pipeline_pause = !0;
3219 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003220 }
3221
Mike Iselyb30d2442006-06-25 20:05:01 -03003222 if (hdw->srate_dirty) {
3223 /* Write new sample rate into control structure since
3224 * the master copy is stale. We must track srate
3225 * separate from the mpeg control structure because
3226 * other logic also uses this value. */
3227 struct v4l2_ext_controls cs;
3228 struct v4l2_ext_control c1;
3229 memset(&cs,0,sizeof(cs));
3230 memset(&c1,0,sizeof(c1));
3231 cs.controls = &c1;
3232 cs.count = 1;
3233 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3234 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03003235 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03003236 }
Mike Iselyc05c0462006-06-25 20:04:25 -03003237
Mike Iselyd8554972006-06-26 20:58:46 -03003238 /* Scan i2c core at this point - before we clear all the dirty
3239 bits. Various parts of the i2c core will notice dirty bits as
3240 appropriate and arrange to broadcast or directly send updates to
3241 the client drivers in order to keep everything in sync */
3242 pvr2_i2c_core_check_stale(hdw);
3243
Mike Isely681c7392007-11-26 01:48:52 -03003244 if (hdw->active_stream_type != hdw->desired_stream_type) {
3245 /* Handle any side effects of stream config here */
3246 hdw->active_stream_type = hdw->desired_stream_type;
3247 }
3248
Mike Isely1df59f02008-04-21 03:50:39 -03003249 if (hdw->hdw_desc->signal_routing_scheme ==
3250 PVR2_ROUTING_SCHEME_GOTVIEW) {
3251 u32 b;
3252 /* Handle GOTVIEW audio switching */
3253 pvr2_hdw_gpio_get_out(hdw,&b);
3254 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3255 /* Set GPIO 11 */
3256 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3257 } else {
3258 /* Clear GPIO 11 */
3259 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3260 }
3261 }
3262
Mike Iselye68a6192009-03-07 01:45:10 -03003263 /* Check and update state for all sub-devices. */
3264 pvr2_subdev_update(hdw);
3265
Mike Isely75212a02009-03-07 01:48:42 -03003266 hdw->tuner_updated = 0;
Mike Isely27764722009-03-07 01:57:25 -03003267 hdw->force_dirty = 0;
Mike Isely5ceaad12009-03-07 00:01:20 -03003268 for (idx = 0; idx < hdw->control_cnt; idx++) {
3269 cptr = hdw->controls + idx;
3270 if (!cptr->info->clear_dirty) continue;
3271 cptr->info->clear_dirty(cptr);
3272 }
3273
Mike Iselyd8554972006-06-26 20:58:46 -03003274 /* Now execute i2c core update */
3275 pvr2_i2c_core_sync(hdw);
3276
Mike Isely62433e32008-04-22 14:45:40 -03003277 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3278 hdw->state_encoder_run) {
3279 /* If encoder isn't running or it can't be touched, then
3280 this will get worked out later when we start the
3281 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03003282 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3283 }
Mike Iselyd8554972006-06-26 20:58:46 -03003284
Mike Isely681c7392007-11-26 01:48:52 -03003285 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03003286 /* Hardware state may have changed in a way to cause the cropping
3287 capabilities to have changed. So mark it stale, which will
3288 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03003289 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3290 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003291}
3292
3293
3294int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3295{
Mike Isely681c7392007-11-26 01:48:52 -03003296 int fl;
3297 LOCK_TAKE(hdw->big_lock);
3298 fl = pvr2_hdw_commit_setup(hdw);
3299 LOCK_GIVE(hdw->big_lock);
3300 if (!fl) return 0;
3301 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003302}
3303
3304
Mike Isely681c7392007-11-26 01:48:52 -03003305static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003306{
Mike Isely681c7392007-11-26 01:48:52 -03003307 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03003308 LOCK_TAKE(hdw->big_lock); do {
3309 pvr2_i2c_core_sync(hdw);
3310 } while (0); LOCK_GIVE(hdw->big_lock);
3311}
3312
3313
Mike Isely681c7392007-11-26 01:48:52 -03003314static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003315{
Mike Isely681c7392007-11-26 01:48:52 -03003316 int fl = 0;
3317 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03003318 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03003319 fl = pvr2_hdw_state_eval(hdw);
3320 } while (0); LOCK_GIVE(hdw->big_lock);
3321 if (fl && hdw->state_func) {
3322 hdw->state_func(hdw->state_data);
3323 }
3324}
3325
3326
Mike Isely681c7392007-11-26 01:48:52 -03003327static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03003328{
Mike Isely681c7392007-11-26 01:48:52 -03003329 return wait_event_interruptible(
3330 hdw->state_wait_data,
3331 (hdw->state_stale == 0) &&
3332 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03003333}
3334
Mike Isely681c7392007-11-26 01:48:52 -03003335
Mike Iselyd8554972006-06-26 20:58:46 -03003336/* Return name for this driver instance */
3337const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3338{
3339 return hdw->name;
3340}
3341
3342
Mike Isely78a47102007-11-26 01:58:20 -03003343const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3344{
3345 return hdw->hdw_desc->description;
3346}
3347
3348
3349const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3350{
3351 return hdw->hdw_desc->shortname;
3352}
3353
3354
Mike Iselyd8554972006-06-26 20:58:46 -03003355int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3356{
3357 int result;
3358 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003359 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03003360 result = pvr2_send_request(hdw,
3361 hdw->cmd_buffer,1,
3362 hdw->cmd_buffer,1);
3363 if (result < 0) break;
3364 result = (hdw->cmd_buffer[0] != 0);
3365 } while(0); LOCK_GIVE(hdw->ctl_lock);
3366 return result;
3367}
3368
3369
Mike Isely18103c572007-01-20 00:09:47 -03003370/* Execute poll of tuner status */
3371void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003372{
Mike Iselyd8554972006-06-26 20:58:46 -03003373 LOCK_TAKE(hdw->big_lock); do {
Mike Iselya51f5002009-03-06 23:30:37 -03003374 pvr2_hdw_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003375 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003376}
3377
3378
Mike Isely432907f2008-08-31 21:02:20 -03003379static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3380{
3381 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003382 return 0;
3383 }
Mike Iselya51f5002009-03-06 23:30:37 -03003384 pvr2_hdw_status_poll(hdw);
Mike Isely432907f2008-08-31 21:02:20 -03003385 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003386 return -EIO;
3387 }
3388 return 0;
3389}
3390
3391
3392/* Return information about cropping capabilities */
3393int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3394{
3395 int stat = 0;
3396 LOCK_TAKE(hdw->big_lock);
3397 stat = pvr2_hdw_check_cropcap(hdw);
3398 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003399 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3400 }
3401 LOCK_GIVE(hdw->big_lock);
3402 return stat;
3403}
3404
3405
Mike Isely18103c572007-01-20 00:09:47 -03003406/* Return information about the tuner */
3407int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3408{
3409 LOCK_TAKE(hdw->big_lock); do {
3410 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -03003411 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -03003412 }
3413 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3414 } while (0); LOCK_GIVE(hdw->big_lock);
3415 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003416}
3417
3418
3419/* Get handle to video output stream */
3420struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3421{
3422 return hp->vid_stream;
3423}
3424
3425
3426void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3427{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003428 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003429 LOCK_TAKE(hdw->big_lock); do {
Mike Isely4f1a3e52006-06-25 20:04:31 -03003430 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Isely5ceaad12009-03-07 00:01:20 -03003431 hdw->log_requested = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003432 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003433 pvr2_i2c_core_sync(hdw);
Mike Iselya51f5002009-03-06 23:30:37 -03003434 hdw->log_requested = 0;
Mike Iselyed3261a2009-03-07 00:02:33 -03003435 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
Mike Iselyb30d2442006-06-25 20:05:01 -03003436 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003437 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003438 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003439 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003440 } while (0); LOCK_GIVE(hdw->big_lock);
3441}
3442
Mike Isely4db666c2007-09-08 22:16:27 -03003443
3444/* Grab EEPROM contents, needed for direct method. */
3445#define EEPROM_SIZE 8192
3446#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3447static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3448{
3449 struct i2c_msg msg[2];
3450 u8 *eeprom;
3451 u8 iadd[2];
3452 u8 addr;
3453 u16 eepromSize;
3454 unsigned int offs;
3455 int ret;
3456 int mode16 = 0;
3457 unsigned pcnt,tcnt;
3458 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3459 if (!eeprom) {
3460 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3461 "Failed to allocate memory"
3462 " required to read eeprom");
3463 return NULL;
3464 }
3465
3466 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3467 hdw->eeprom_addr);
3468 addr = hdw->eeprom_addr;
3469 /* Seems that if the high bit is set, then the *real* eeprom
3470 address is shifted right now bit position (noticed this in
3471 newer PVR USB2 hardware) */
3472 if (addr & 0x80) addr >>= 1;
3473
3474 /* FX2 documentation states that a 16bit-addressed eeprom is
3475 expected if the I2C address is an odd number (yeah, this is
3476 strange but it's what they do) */
3477 mode16 = (addr & 1);
3478 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3479 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3480 " using %d bit addressing",eepromSize,addr,
3481 mode16 ? 16 : 8);
3482
3483 msg[0].addr = addr;
3484 msg[0].flags = 0;
3485 msg[0].len = mode16 ? 2 : 1;
3486 msg[0].buf = iadd;
3487 msg[1].addr = addr;
3488 msg[1].flags = I2C_M_RD;
3489
3490 /* We have to do the actual eeprom data fetch ourselves, because
3491 (1) we're only fetching part of the eeprom, and (2) if we were
3492 getting the whole thing our I2C driver can't grab it in one
3493 pass - which is what tveeprom is otherwise going to attempt */
3494 memset(eeprom,0,EEPROM_SIZE);
3495 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3496 pcnt = 16;
3497 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3498 offs = tcnt + (eepromSize - EEPROM_SIZE);
3499 if (mode16) {
3500 iadd[0] = offs >> 8;
3501 iadd[1] = offs;
3502 } else {
3503 iadd[0] = offs;
3504 }
3505 msg[1].len = pcnt;
3506 msg[1].buf = eeprom+tcnt;
3507 if ((ret = i2c_transfer(&hdw->i2c_adap,
3508 msg,ARRAY_SIZE(msg))) != 2) {
3509 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3510 "eeprom fetch set offs err=%d",ret);
3511 kfree(eeprom);
3512 return NULL;
3513 }
3514 }
3515 return eeprom;
3516}
3517
3518
3519void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3520 int prom_flag,
3521 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003522{
3523 int ret;
3524 u16 address;
3525 unsigned int pipe;
3526 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003527 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003528
3529 if (!enable_flag) {
3530 pvr2_trace(PVR2_TRACE_FIRMWARE,
3531 "Cleaning up after CPU firmware fetch");
3532 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003533 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003534 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003535 if (hdw->fw_cpu_flag) {
3536 /* Now release the CPU. It will disconnect
3537 and reconnect later. */
3538 pvr2_hdw_cpureset_assert(hdw,0);
3539 }
Mike Iselyd8554972006-06-26 20:58:46 -03003540 break;
3541 }
3542
Mike Isely4db666c2007-09-08 22:16:27 -03003543 hdw->fw_cpu_flag = (prom_flag == 0);
3544 if (hdw->fw_cpu_flag) {
3545 pvr2_trace(PVR2_TRACE_FIRMWARE,
3546 "Preparing to suck out CPU firmware");
3547 hdw->fw_size = 0x2000;
3548 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3549 if (!hdw->fw_buffer) {
3550 hdw->fw_size = 0;
3551 break;
3552 }
3553
3554 /* We have to hold the CPU during firmware upload. */
3555 pvr2_hdw_cpureset_assert(hdw,1);
3556
3557 /* download the firmware from address 0000-1fff in 2048
3558 (=0x800) bytes chunk. */
3559
3560 pvr2_trace(PVR2_TRACE_FIRMWARE,
3561 "Grabbing CPU firmware");
3562 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3563 for(address = 0; address < hdw->fw_size;
3564 address += 0x800) {
3565 ret = usb_control_msg(hdw->usb_dev,pipe,
3566 0xa0,0xc0,
3567 address,0,
3568 hdw->fw_buffer+address,
3569 0x800,HZ);
3570 if (ret < 0) break;
3571 }
3572
3573 pvr2_trace(PVR2_TRACE_FIRMWARE,
3574 "Done grabbing CPU firmware");
3575 } else {
3576 pvr2_trace(PVR2_TRACE_FIRMWARE,
3577 "Sucking down EEPROM contents");
3578 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3579 if (!hdw->fw_buffer) {
3580 pvr2_trace(PVR2_TRACE_FIRMWARE,
3581 "EEPROM content suck failed.");
3582 break;
3583 }
3584 hdw->fw_size = EEPROM_SIZE;
3585 pvr2_trace(PVR2_TRACE_FIRMWARE,
3586 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003587 }
3588
Mike Iselyd8554972006-06-26 20:58:46 -03003589 } while (0); LOCK_GIVE(hdw->big_lock);
3590}
3591
3592
3593/* Return true if we're in a mode for retrieval CPU firmware */
3594int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3595{
Al Viro5fa12472008-03-29 03:07:38 +00003596 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003597}
3598
3599
3600int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3601 char *buf,unsigned int cnt)
3602{
3603 int ret = -EINVAL;
3604 LOCK_TAKE(hdw->big_lock); do {
3605 if (!buf) break;
3606 if (!cnt) break;
3607
3608 if (!hdw->fw_buffer) {
3609 ret = -EIO;
3610 break;
3611 }
3612
3613 if (offs >= hdw->fw_size) {
3614 pvr2_trace(PVR2_TRACE_FIRMWARE,
3615 "Read firmware data offs=%d EOF",
3616 offs);
3617 ret = 0;
3618 break;
3619 }
3620
3621 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3622
3623 memcpy(buf,hdw->fw_buffer+offs,cnt);
3624
3625 pvr2_trace(PVR2_TRACE_FIRMWARE,
3626 "Read firmware data offs=%d cnt=%d",
3627 offs,cnt);
3628 ret = cnt;
3629 } while (0); LOCK_GIVE(hdw->big_lock);
3630
3631 return ret;
3632}
3633
3634
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003635int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003636 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003637{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003638 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003639 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3640 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3641 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003642 default: return -1;
3643 }
Mike Iselyd8554972006-06-26 20:58:46 -03003644}
3645
3646
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003647/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003648void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003649 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003650{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003651 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003652 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3653 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3654 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003655 default: break;
3656 }
Mike Iselyd8554972006-06-26 20:58:46 -03003657}
3658
3659
David Howells7d12e782006-10-05 14:55:46 +01003660static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003661{
3662 struct pvr2_hdw *hdw = urb->context;
3663 hdw->ctl_write_pend_flag = 0;
3664 if (hdw->ctl_read_pend_flag) return;
3665 complete(&hdw->ctl_done);
3666}
3667
3668
David Howells7d12e782006-10-05 14:55:46 +01003669static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003670{
3671 struct pvr2_hdw *hdw = urb->context;
3672 hdw->ctl_read_pend_flag = 0;
3673 if (hdw->ctl_write_pend_flag) return;
3674 complete(&hdw->ctl_done);
3675}
3676
3677
3678static void pvr2_ctl_timeout(unsigned long data)
3679{
3680 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3681 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3682 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003683 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003684 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003685 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003686 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003687 }
3688}
3689
3690
Mike Iselye61b6fc2006-07-18 22:42:18 -03003691/* Issue a command and get a response from the device. This extended
3692 version includes a probe flag (which if set means that device errors
3693 should not be logged or treated as fatal) and a timeout in jiffies.
3694 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003695static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3696 unsigned int timeout,int probe_fl,
3697 void *write_data,unsigned int write_len,
3698 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003699{
3700 unsigned int idx;
3701 int status = 0;
3702 struct timer_list timer;
3703 if (!hdw->ctl_lock_held) {
3704 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3705 "Attempted to execute control transfer"
3706 " without lock!!");
3707 return -EDEADLK;
3708 }
Mike Isely681c7392007-11-26 01:48:52 -03003709 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003710 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3711 "Attempted to execute control transfer"
3712 " when device not ok");
3713 return -EIO;
3714 }
3715 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3716 if (!probe_fl) {
3717 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3718 "Attempted to execute control transfer"
3719 " when USB is disconnected");
3720 }
3721 return -ENOTTY;
3722 }
3723
3724 /* Ensure that we have sane parameters */
3725 if (!write_data) write_len = 0;
3726 if (!read_data) read_len = 0;
3727 if (write_len > PVR2_CTL_BUFFSIZE) {
3728 pvr2_trace(
3729 PVR2_TRACE_ERROR_LEGS,
3730 "Attempted to execute %d byte"
3731 " control-write transfer (limit=%d)",
3732 write_len,PVR2_CTL_BUFFSIZE);
3733 return -EINVAL;
3734 }
3735 if (read_len > PVR2_CTL_BUFFSIZE) {
3736 pvr2_trace(
3737 PVR2_TRACE_ERROR_LEGS,
3738 "Attempted to execute %d byte"
3739 " control-read transfer (limit=%d)",
3740 write_len,PVR2_CTL_BUFFSIZE);
3741 return -EINVAL;
3742 }
3743 if ((!write_len) && (!read_len)) {
3744 pvr2_trace(
3745 PVR2_TRACE_ERROR_LEGS,
3746 "Attempted to execute null control transfer?");
3747 return -EINVAL;
3748 }
3749
3750
3751 hdw->cmd_debug_state = 1;
3752 if (write_len) {
3753 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3754 } else {
3755 hdw->cmd_debug_code = 0;
3756 }
3757 hdw->cmd_debug_write_len = write_len;
3758 hdw->cmd_debug_read_len = read_len;
3759
3760 /* Initialize common stuff */
3761 init_completion(&hdw->ctl_done);
3762 hdw->ctl_timeout_flag = 0;
3763 hdw->ctl_write_pend_flag = 0;
3764 hdw->ctl_read_pend_flag = 0;
3765 init_timer(&timer);
3766 timer.expires = jiffies + timeout;
3767 timer.data = (unsigned long)hdw;
3768 timer.function = pvr2_ctl_timeout;
3769
3770 if (write_len) {
3771 hdw->cmd_debug_state = 2;
3772 /* Transfer write data to internal buffer */
3773 for (idx = 0; idx < write_len; idx++) {
3774 hdw->ctl_write_buffer[idx] =
3775 ((unsigned char *)write_data)[idx];
3776 }
3777 /* Initiate a write request */
3778 usb_fill_bulk_urb(hdw->ctl_write_urb,
3779 hdw->usb_dev,
3780 usb_sndbulkpipe(hdw->usb_dev,
3781 PVR2_CTL_WRITE_ENDPOINT),
3782 hdw->ctl_write_buffer,
3783 write_len,
3784 pvr2_ctl_write_complete,
3785 hdw);
3786 hdw->ctl_write_urb->actual_length = 0;
3787 hdw->ctl_write_pend_flag = !0;
3788 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3789 if (status < 0) {
3790 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3791 "Failed to submit write-control"
3792 " URB status=%d",status);
3793 hdw->ctl_write_pend_flag = 0;
3794 goto done;
3795 }
3796 }
3797
3798 if (read_len) {
3799 hdw->cmd_debug_state = 3;
3800 memset(hdw->ctl_read_buffer,0x43,read_len);
3801 /* Initiate a read request */
3802 usb_fill_bulk_urb(hdw->ctl_read_urb,
3803 hdw->usb_dev,
3804 usb_rcvbulkpipe(hdw->usb_dev,
3805 PVR2_CTL_READ_ENDPOINT),
3806 hdw->ctl_read_buffer,
3807 read_len,
3808 pvr2_ctl_read_complete,
3809 hdw);
3810 hdw->ctl_read_urb->actual_length = 0;
3811 hdw->ctl_read_pend_flag = !0;
3812 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3813 if (status < 0) {
3814 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3815 "Failed to submit read-control"
3816 " URB status=%d",status);
3817 hdw->ctl_read_pend_flag = 0;
3818 goto done;
3819 }
3820 }
3821
3822 /* Start timer */
3823 add_timer(&timer);
3824
3825 /* Now wait for all I/O to complete */
3826 hdw->cmd_debug_state = 4;
3827 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3828 wait_for_completion(&hdw->ctl_done);
3829 }
3830 hdw->cmd_debug_state = 5;
3831
3832 /* Stop timer */
3833 del_timer_sync(&timer);
3834
3835 hdw->cmd_debug_state = 6;
3836 status = 0;
3837
3838 if (hdw->ctl_timeout_flag) {
3839 status = -ETIMEDOUT;
3840 if (!probe_fl) {
3841 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3842 "Timed out control-write");
3843 }
3844 goto done;
3845 }
3846
3847 if (write_len) {
3848 /* Validate results of write request */
3849 if ((hdw->ctl_write_urb->status != 0) &&
3850 (hdw->ctl_write_urb->status != -ENOENT) &&
3851 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3852 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3853 /* USB subsystem is reporting some kind of failure
3854 on the write */
3855 status = hdw->ctl_write_urb->status;
3856 if (!probe_fl) {
3857 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3858 "control-write URB failure,"
3859 " status=%d",
3860 status);
3861 }
3862 goto done;
3863 }
3864 if (hdw->ctl_write_urb->actual_length < write_len) {
3865 /* Failed to write enough data */
3866 status = -EIO;
3867 if (!probe_fl) {
3868 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3869 "control-write URB short,"
3870 " expected=%d got=%d",
3871 write_len,
3872 hdw->ctl_write_urb->actual_length);
3873 }
3874 goto done;
3875 }
3876 }
3877 if (read_len) {
3878 /* Validate results of read request */
3879 if ((hdw->ctl_read_urb->status != 0) &&
3880 (hdw->ctl_read_urb->status != -ENOENT) &&
3881 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3882 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3883 /* USB subsystem is reporting some kind of failure
3884 on the read */
3885 status = hdw->ctl_read_urb->status;
3886 if (!probe_fl) {
3887 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3888 "control-read URB failure,"
3889 " status=%d",
3890 status);
3891 }
3892 goto done;
3893 }
3894 if (hdw->ctl_read_urb->actual_length < read_len) {
3895 /* Failed to read enough data */
3896 status = -EIO;
3897 if (!probe_fl) {
3898 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3899 "control-read URB short,"
3900 " expected=%d got=%d",
3901 read_len,
3902 hdw->ctl_read_urb->actual_length);
3903 }
3904 goto done;
3905 }
3906 /* Transfer retrieved data out from internal buffer */
3907 for (idx = 0; idx < read_len; idx++) {
3908 ((unsigned char *)read_data)[idx] =
3909 hdw->ctl_read_buffer[idx];
3910 }
3911 }
3912
3913 done:
3914
3915 hdw->cmd_debug_state = 0;
3916 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003917 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003918 }
3919 return status;
3920}
3921
3922
3923int pvr2_send_request(struct pvr2_hdw *hdw,
3924 void *write_data,unsigned int write_len,
3925 void *read_data,unsigned int read_len)
3926{
3927 return pvr2_send_request_ex(hdw,HZ*4,0,
3928 write_data,write_len,
3929 read_data,read_len);
3930}
3931
Mike Isely1c9d10d2008-03-28 05:38:54 -03003932
3933static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3934{
3935 int ret;
3936 unsigned int cnt = 1;
3937 unsigned int args = 0;
3938 LOCK_TAKE(hdw->ctl_lock);
3939 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3940 args = (cmdcode >> 8) & 0xffu;
3941 args = (args > 2) ? 2 : args;
3942 if (args) {
3943 cnt += args;
3944 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3945 if (args > 1) {
3946 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3947 }
3948 }
3949 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3950 unsigned int idx;
3951 unsigned int ccnt,bcnt;
3952 char tbuf[50];
3953 cmdcode &= 0xffu;
3954 bcnt = 0;
3955 ccnt = scnprintf(tbuf+bcnt,
3956 sizeof(tbuf)-bcnt,
3957 "Sending FX2 command 0x%x",cmdcode);
3958 bcnt += ccnt;
3959 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3960 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3961 ccnt = scnprintf(tbuf+bcnt,
3962 sizeof(tbuf)-bcnt,
3963 " \"%s\"",
3964 pvr2_fx2cmd_desc[idx].desc);
3965 bcnt += ccnt;
3966 break;
3967 }
3968 }
3969 if (args) {
3970 ccnt = scnprintf(tbuf+bcnt,
3971 sizeof(tbuf)-bcnt,
3972 " (%u",hdw->cmd_buffer[1]);
3973 bcnt += ccnt;
3974 if (args > 1) {
3975 ccnt = scnprintf(tbuf+bcnt,
3976 sizeof(tbuf)-bcnt,
3977 ",%u",hdw->cmd_buffer[2]);
3978 bcnt += ccnt;
3979 }
3980 ccnt = scnprintf(tbuf+bcnt,
3981 sizeof(tbuf)-bcnt,
3982 ")");
3983 bcnt += ccnt;
3984 }
3985 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3986 }
3987 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3988 LOCK_GIVE(hdw->ctl_lock);
3989 return ret;
3990}
3991
3992
Mike Iselyd8554972006-06-26 20:58:46 -03003993int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3994{
3995 int ret;
3996
3997 LOCK_TAKE(hdw->ctl_lock);
3998
Michael Krufky8d364362007-01-22 02:17:55 -03003999 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03004000 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
4001 hdw->cmd_buffer[5] = 0;
4002 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4003 hdw->cmd_buffer[7] = reg & 0xff;
4004
4005
4006 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
4007
4008 LOCK_GIVE(hdw->ctl_lock);
4009
4010 return ret;
4011}
4012
4013
Adrian Bunk07e337e2006-06-30 11:30:20 -03004014static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03004015{
4016 int ret = 0;
4017
4018 LOCK_TAKE(hdw->ctl_lock);
4019
Michael Krufky8d364362007-01-22 02:17:55 -03004020 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03004021 hdw->cmd_buffer[1] = 0;
4022 hdw->cmd_buffer[2] = 0;
4023 hdw->cmd_buffer[3] = 0;
4024 hdw->cmd_buffer[4] = 0;
4025 hdw->cmd_buffer[5] = 0;
4026 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4027 hdw->cmd_buffer[7] = reg & 0xff;
4028
4029 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
4030 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
4031
4032 LOCK_GIVE(hdw->ctl_lock);
4033
4034 return ret;
4035}
4036
4037
Mike Isely681c7392007-11-26 01:48:52 -03004038void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004039{
4040 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03004041 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4042 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03004043 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004044 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03004045 }
Mike Isely681c7392007-11-26 01:48:52 -03004046 hdw->flag_ok = 0;
4047 trace_stbit("flag_ok",hdw->flag_ok);
4048 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03004049}
4050
4051
4052void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
4053{
4054 int ret;
4055 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004056 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05004057 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03004058 ret = usb_reset_device(hdw->usb_dev);
4059 usb_unlock_device(hdw->usb_dev);
4060 } else {
4061 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4062 "Failed to lock USB device ret=%d",ret);
4063 }
4064 if (init_pause_msec) {
4065 pvr2_trace(PVR2_TRACE_INFO,
4066 "Waiting %u msec for hardware to settle",
4067 init_pause_msec);
4068 msleep(init_pause_msec);
4069 }
4070
4071}
4072
4073
4074void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
4075{
4076 char da[1];
4077 unsigned int pipe;
4078 int ret;
4079
4080 if (!hdw->usb_dev) return;
4081
4082 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
4083
4084 da[0] = val ? 0x01 : 0x00;
4085
4086 /* Write the CPUCS register on the 8051. The lsb of the register
4087 is the reset bit; a 1 asserts reset while a 0 clears it. */
4088 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
4089 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
4090 if (ret < 0) {
4091 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4092 "cpureset_assert(%d) error=%d",val,ret);
4093 pvr2_hdw_render_useless(hdw);
4094 }
4095}
4096
4097
4098int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4099{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004100 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03004101}
4102
4103
Michael Krufkye1edb192008-04-22 14:45:39 -03004104int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4105{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004106 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03004107}
4108
Mike Isely1c9d10d2008-03-28 05:38:54 -03004109
Michael Krufkye1edb192008-04-22 14:45:39 -03004110int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4111{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004112 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03004113}
4114
Mike Iselyd8554972006-06-26 20:58:46 -03004115
4116int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4117{
Mike Iselyd8554972006-06-26 20:58:46 -03004118 pvr2_trace(PVR2_TRACE_INIT,
4119 "Requesting decoder reset");
Mike Iselyaf78e162009-03-07 00:21:30 -03004120 if (hdw->decoder_ctrl) {
4121 if (!hdw->decoder_ctrl->force_reset) {
4122 pvr2_trace(PVR2_TRACE_INIT,
4123 "Unable to reset decoder: not implemented");
4124 return -ENOTTY;
4125 }
4126 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
4127 return 0;
4128 } else {
4129 }
4130 if (hdw->decoder_client_id) {
4131 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4132 core, reset, 0);
4133 return 0;
4134 }
4135 pvr2_trace(PVR2_TRACE_INIT,
4136 "Unable to reset decoder: nothing attached");
4137 return -ENOTTY;
Mike Iselyd8554972006-06-26 20:58:46 -03004138}
4139
4140
Mike Isely62433e32008-04-22 14:45:40 -03004141static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004142{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004143 hdw->flag_ok = !0;
4144 return pvr2_issue_simple_cmd(hdw,
4145 FX2CMD_HCW_DEMOD_RESETIN |
4146 (1 << 8) |
4147 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03004148}
4149
Mike Isely84147f32008-04-22 14:45:40 -03004150
Mike Isely62433e32008-04-22 14:45:40 -03004151static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004152{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004153 hdw->flag_ok = !0;
4154 return pvr2_issue_simple_cmd(hdw,(onoff ?
4155 FX2CMD_ONAIR_DTV_POWER_ON :
4156 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004157}
4158
Mike Isely62433e32008-04-22 14:45:40 -03004159
4160static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4161 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004162{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004163 return pvr2_issue_simple_cmd(hdw,(onoff ?
4164 FX2CMD_ONAIR_DTV_STREAMING_ON :
4165 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004166}
4167
Mike Isely62433e32008-04-22 14:45:40 -03004168
4169static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4170{
4171 int cmode;
4172 /* Compare digital/analog desired setting with current setting. If
4173 they don't match, fix it... */
4174 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4175 if (cmode == hdw->pathway_state) {
4176 /* They match; nothing to do */
4177 return;
4178 }
4179
4180 switch (hdw->hdw_desc->digital_control_scheme) {
4181 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4182 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4183 if (cmode == PVR2_PATHWAY_ANALOG) {
4184 /* If moving to analog mode, also force the decoder
4185 to reset. If no decoder is attached, then it's
4186 ok to ignore this because if/when the decoder
4187 attaches, it will reset itself at that time. */
4188 pvr2_hdw_cmd_decoder_reset(hdw);
4189 }
4190 break;
4191 case PVR2_DIGITAL_SCHEME_ONAIR:
4192 /* Supposedly we should always have the power on whether in
4193 digital or analog mode. But for now do what appears to
4194 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004195 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03004196 break;
4197 default: break;
4198 }
4199
Mike Isely1b9c18c2008-04-22 14:45:41 -03004200 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03004201 hdw->pathway_state = cmode;
4202}
4203
4204
Adrian Bunke9b59f62008-05-10 04:35:24 -03004205static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03004206{
4207 /* change some GPIO data
4208 *
4209 * note: bit d7 of dir appears to control the LED,
4210 * so we shut it off here.
4211 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03004212 */
Mike Isely40381cb2008-04-22 14:45:42 -03004213 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004214 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03004215 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004216 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03004217 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03004218 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03004219}
Mike Iselyc55a97d2008-04-22 14:45:41 -03004220
Mike Isely40381cb2008-04-22 14:45:42 -03004221
4222typedef void (*led_method_func)(struct pvr2_hdw *,int);
4223
4224static led_method_func led_methods[] = {
4225 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4226};
4227
4228
4229/* Toggle LED */
4230static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4231{
4232 unsigned int scheme_id;
4233 led_method_func fp;
4234
4235 if ((!onoff) == (!hdw->led_on)) return;
4236
4237 hdw->led_on = onoff != 0;
4238
4239 scheme_id = hdw->hdw_desc->led_scheme;
4240 if (scheme_id < ARRAY_SIZE(led_methods)) {
4241 fp = led_methods[scheme_id];
4242 } else {
4243 fp = NULL;
4244 }
4245
4246 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03004247}
4248
4249
Mike Iselye61b6fc2006-07-18 22:42:18 -03004250/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004251static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03004252{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004253 int ret;
4254
4255 /* If we're in analog mode, then just issue the usual analog
4256 command. */
4257 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4258 return pvr2_issue_simple_cmd(hdw,
4259 (runFl ?
4260 FX2CMD_STREAMING_ON :
4261 FX2CMD_STREAMING_OFF));
4262 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03004263 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004264
4265 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4266 /* Whoops, we don't know what mode we're in... */
4267 return -EINVAL;
4268 }
4269
4270 /* To get here we have to be in digital mode. The mechanism here
4271 is unfortunately different for different vendors. So we switch
4272 on the device's digital scheme attribute in order to figure out
4273 what to do. */
4274 switch (hdw->hdw_desc->digital_control_scheme) {
4275 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4276 return pvr2_issue_simple_cmd(hdw,
4277 (runFl ?
4278 FX2CMD_HCW_DTV_STREAMING_ON :
4279 FX2CMD_HCW_DTV_STREAMING_OFF));
4280 case PVR2_DIGITAL_SCHEME_ONAIR:
4281 ret = pvr2_issue_simple_cmd(hdw,
4282 (runFl ?
4283 FX2CMD_STREAMING_ON :
4284 FX2CMD_STREAMING_OFF));
4285 if (ret) return ret;
4286 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4287 default:
4288 return -EINVAL;
4289 }
Mike Iselyd8554972006-06-26 20:58:46 -03004290}
4291
4292
Mike Isely62433e32008-04-22 14:45:40 -03004293/* Evaluate whether or not state_pathway_ok can change */
4294static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4295{
4296 if (hdw->state_pathway_ok) {
4297 /* Nothing to do if pathway is already ok */
4298 return 0;
4299 }
4300 if (!hdw->state_pipeline_idle) {
4301 /* Not allowed to change anything if pipeline is not idle */
4302 return 0;
4303 }
4304 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4305 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03004306 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03004307 return !0;
4308}
4309
4310
Mike Isely681c7392007-11-26 01:48:52 -03004311/* Evaluate whether or not state_encoder_ok can change */
4312static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4313{
4314 if (hdw->state_encoder_ok) return 0;
4315 if (hdw->flag_tripped) return 0;
4316 if (hdw->state_encoder_run) return 0;
4317 if (hdw->state_encoder_config) return 0;
4318 if (hdw->state_decoder_run) return 0;
4319 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004320 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4321 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4322 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4323 return 0;
4324 }
4325
Mike Isely681c7392007-11-26 01:48:52 -03004326 if (pvr2_upload_firmware2(hdw) < 0) {
4327 hdw->flag_tripped = !0;
4328 trace_stbit("flag_tripped",hdw->flag_tripped);
4329 return !0;
4330 }
4331 hdw->state_encoder_ok = !0;
4332 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4333 return !0;
4334}
4335
4336
4337/* Evaluate whether or not state_encoder_config can change */
4338static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4339{
4340 if (hdw->state_encoder_config) {
4341 if (hdw->state_encoder_ok) {
4342 if (hdw->state_pipeline_req &&
4343 !hdw->state_pipeline_pause) return 0;
4344 }
4345 hdw->state_encoder_config = 0;
4346 hdw->state_encoder_waitok = 0;
4347 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4348 /* paranoia - solve race if timer just completed */
4349 del_timer_sync(&hdw->encoder_wait_timer);
4350 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004351 if (!hdw->state_pathway_ok ||
4352 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4353 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03004354 !hdw->state_pipeline_idle ||
4355 hdw->state_pipeline_pause ||
4356 !hdw->state_pipeline_req ||
4357 !hdw->state_pipeline_config) {
4358 /* We must reset the enforced wait interval if
4359 anything has happened that might have disturbed
4360 the encoder. This should be a rare case. */
4361 if (timer_pending(&hdw->encoder_wait_timer)) {
4362 del_timer_sync(&hdw->encoder_wait_timer);
4363 }
4364 if (hdw->state_encoder_waitok) {
4365 /* Must clear the state - therefore we did
4366 something to a state bit and must also
4367 return true. */
4368 hdw->state_encoder_waitok = 0;
4369 trace_stbit("state_encoder_waitok",
4370 hdw->state_encoder_waitok);
4371 return !0;
4372 }
4373 return 0;
4374 }
4375 if (!hdw->state_encoder_waitok) {
4376 if (!timer_pending(&hdw->encoder_wait_timer)) {
4377 /* waitok flag wasn't set and timer isn't
4378 running. Check flag once more to avoid
4379 a race then start the timer. This is
4380 the point when we measure out a minimal
4381 quiet interval before doing something to
4382 the encoder. */
4383 if (!hdw->state_encoder_waitok) {
4384 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004385 jiffies +
4386 (HZ * TIME_MSEC_ENCODER_WAIT
4387 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004388 add_timer(&hdw->encoder_wait_timer);
4389 }
4390 }
4391 /* We can't continue until we know we have been
4392 quiet for the interval measured by this
4393 timer. */
4394 return 0;
4395 }
4396 pvr2_encoder_configure(hdw);
4397 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4398 }
4399 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4400 return !0;
4401}
4402
4403
Mike Iselyd913d632008-04-06 04:04:35 -03004404/* Return true if the encoder should not be running. */
4405static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4406{
4407 if (!hdw->state_encoder_ok) {
4408 /* Encoder isn't healthy at the moment, so stop it. */
4409 return !0;
4410 }
4411 if (!hdw->state_pathway_ok) {
4412 /* Mode is not understood at the moment (i.e. it wants to
4413 change), so encoder must be stopped. */
4414 return !0;
4415 }
4416
4417 switch (hdw->pathway_state) {
4418 case PVR2_PATHWAY_ANALOG:
4419 if (!hdw->state_decoder_run) {
4420 /* We're in analog mode and the decoder is not
4421 running; thus the encoder should be stopped as
4422 well. */
4423 return !0;
4424 }
4425 break;
4426 case PVR2_PATHWAY_DIGITAL:
4427 if (hdw->state_encoder_runok) {
4428 /* This is a funny case. We're in digital mode so
4429 really the encoder should be stopped. However
4430 if it really is running, only kill it after
4431 runok has been set. This gives a chance for the
4432 onair quirk to function (encoder must run
4433 briefly first, at least once, before onair
4434 digital streaming can work). */
4435 return !0;
4436 }
4437 break;
4438 default:
4439 /* Unknown mode; so encoder should be stopped. */
4440 return !0;
4441 }
4442
4443 /* If we get here, we haven't found a reason to stop the
4444 encoder. */
4445 return 0;
4446}
4447
4448
4449/* Return true if the encoder should be running. */
4450static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4451{
4452 if (!hdw->state_encoder_ok) {
4453 /* Don't run the encoder if it isn't healthy... */
4454 return 0;
4455 }
4456 if (!hdw->state_pathway_ok) {
4457 /* Don't run the encoder if we don't (yet) know what mode
4458 we need to be in... */
4459 return 0;
4460 }
4461
4462 switch (hdw->pathway_state) {
4463 case PVR2_PATHWAY_ANALOG:
4464 if (hdw->state_decoder_run) {
4465 /* In analog mode, if the decoder is running, then
4466 run the encoder. */
4467 return !0;
4468 }
4469 break;
4470 case PVR2_PATHWAY_DIGITAL:
4471 if ((hdw->hdw_desc->digital_control_scheme ==
4472 PVR2_DIGITAL_SCHEME_ONAIR) &&
4473 !hdw->state_encoder_runok) {
4474 /* This is a quirk. OnAir hardware won't stream
4475 digital until the encoder has been run at least
4476 once, for a minimal period of time (empiricially
4477 measured to be 1/4 second). So if we're on
4478 OnAir hardware and the encoder has never been
4479 run at all, then start the encoder. Normal
4480 state machine logic in the driver will
4481 automatically handle the remaining bits. */
4482 return !0;
4483 }
4484 break;
4485 default:
4486 /* For completeness (unknown mode; encoder won't run ever) */
4487 break;
4488 }
4489 /* If we get here, then we haven't found any reason to run the
4490 encoder, so don't run it. */
4491 return 0;
4492}
4493
4494
Mike Isely681c7392007-11-26 01:48:52 -03004495/* Evaluate whether or not state_encoder_run can change */
4496static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4497{
4498 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004499 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004500 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004501 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004502 if (pvr2_encoder_stop(hdw) < 0) return !0;
4503 }
4504 hdw->state_encoder_run = 0;
4505 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004506 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004507 if (pvr2_encoder_start(hdw) < 0) return !0;
4508 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004509 if (!hdw->state_encoder_runok) {
4510 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004511 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004512 add_timer(&hdw->encoder_run_timer);
4513 }
Mike Isely681c7392007-11-26 01:48:52 -03004514 }
4515 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4516 return !0;
4517}
4518
4519
4520/* Timeout function for quiescent timer. */
4521static void pvr2_hdw_quiescent_timeout(unsigned long data)
4522{
4523 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4524 hdw->state_decoder_quiescent = !0;
4525 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4526 hdw->state_stale = !0;
4527 queue_work(hdw->workqueue,&hdw->workpoll);
4528}
4529
4530
4531/* Timeout function for encoder wait timer. */
4532static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4533{
4534 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4535 hdw->state_encoder_waitok = !0;
4536 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4537 hdw->state_stale = !0;
4538 queue_work(hdw->workqueue,&hdw->workpoll);
4539}
4540
4541
Mike Iselyd913d632008-04-06 04:04:35 -03004542/* Timeout function for encoder run timer. */
4543static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4544{
4545 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4546 if (!hdw->state_encoder_runok) {
4547 hdw->state_encoder_runok = !0;
4548 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4549 hdw->state_stale = !0;
4550 queue_work(hdw->workqueue,&hdw->workpoll);
4551 }
4552}
4553
4554
Mike Isely681c7392007-11-26 01:48:52 -03004555/* Evaluate whether or not state_decoder_run can change */
4556static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4557{
4558 if (hdw->state_decoder_run) {
4559 if (hdw->state_encoder_ok) {
4560 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004561 !hdw->state_pipeline_pause &&
4562 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004563 }
4564 if (!hdw->flag_decoder_missed) {
4565 pvr2_decoder_enable(hdw,0);
4566 }
4567 hdw->state_decoder_quiescent = 0;
4568 hdw->state_decoder_run = 0;
4569 /* paranoia - solve race if timer just completed */
4570 del_timer_sync(&hdw->quiescent_timer);
4571 } else {
4572 if (!hdw->state_decoder_quiescent) {
4573 if (!timer_pending(&hdw->quiescent_timer)) {
4574 /* We don't do something about the
4575 quiescent timer until right here because
4576 we also want to catch cases where the
4577 decoder was already not running (like
4578 after initialization) as opposed to
4579 knowing that we had just stopped it.
4580 The second flag check is here to cover a
4581 race - the timer could have run and set
4582 this flag just after the previous check
4583 but before we did the pending check. */
4584 if (!hdw->state_decoder_quiescent) {
4585 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004586 jiffies +
4587 (HZ * TIME_MSEC_DECODER_WAIT
4588 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004589 add_timer(&hdw->quiescent_timer);
4590 }
4591 }
4592 /* Don't allow decoder to start again until it has
4593 been quiesced first. This little detail should
4594 hopefully further stabilize the encoder. */
4595 return 0;
4596 }
Mike Isely62433e32008-04-22 14:45:40 -03004597 if (!hdw->state_pathway_ok ||
4598 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4599 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004600 hdw->state_pipeline_pause ||
4601 !hdw->state_pipeline_config ||
4602 !hdw->state_encoder_config ||
4603 !hdw->state_encoder_ok) return 0;
4604 del_timer_sync(&hdw->quiescent_timer);
4605 if (hdw->flag_decoder_missed) return 0;
4606 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4607 hdw->state_decoder_quiescent = 0;
4608 hdw->state_decoder_run = !0;
4609 }
4610 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4611 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4612 return !0;
4613}
4614
4615
4616/* Evaluate whether or not state_usbstream_run can change */
4617static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4618{
4619 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004620 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004621 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004622 fl = (hdw->state_encoder_ok &&
4623 hdw->state_encoder_run);
4624 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4625 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4626 fl = hdw->state_encoder_ok;
4627 }
4628 if (fl &&
4629 hdw->state_pipeline_req &&
4630 !hdw->state_pipeline_pause &&
4631 hdw->state_pathway_ok) {
4632 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004633 }
4634 pvr2_hdw_cmd_usbstream(hdw,0);
4635 hdw->state_usbstream_run = 0;
4636 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004637 if (!hdw->state_pipeline_req ||
4638 hdw->state_pipeline_pause ||
4639 !hdw->state_pathway_ok) return 0;
4640 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4641 if (!hdw->state_encoder_ok ||
4642 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004643 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4644 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4645 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004646 if (hdw->state_encoder_run) return 0;
4647 if (hdw->hdw_desc->digital_control_scheme ==
4648 PVR2_DIGITAL_SCHEME_ONAIR) {
4649 /* OnAir digital receivers won't stream
4650 unless the analog encoder has run first.
4651 Why? I have no idea. But don't even
4652 try until we know the analog side is
4653 known to have run. */
4654 if (!hdw->state_encoder_runok) return 0;
4655 }
Mike Isely62433e32008-04-22 14:45:40 -03004656 }
Mike Isely681c7392007-11-26 01:48:52 -03004657 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4658 hdw->state_usbstream_run = !0;
4659 }
4660 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4661 return !0;
4662}
4663
4664
4665/* Attempt to configure pipeline, if needed */
4666static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4667{
4668 if (hdw->state_pipeline_config ||
4669 hdw->state_pipeline_pause) return 0;
4670 pvr2_hdw_commit_execute(hdw);
4671 return !0;
4672}
4673
4674
4675/* Update pipeline idle and pipeline pause tracking states based on other
4676 inputs. This must be called whenever the other relevant inputs have
4677 changed. */
4678static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4679{
4680 unsigned int st;
4681 int updatedFl = 0;
4682 /* Update pipeline state */
4683 st = !(hdw->state_encoder_run ||
4684 hdw->state_decoder_run ||
4685 hdw->state_usbstream_run ||
4686 (!hdw->state_decoder_quiescent));
4687 if (!st != !hdw->state_pipeline_idle) {
4688 hdw->state_pipeline_idle = st;
4689 updatedFl = !0;
4690 }
4691 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4692 hdw->state_pipeline_pause = 0;
4693 updatedFl = !0;
4694 }
4695 return updatedFl;
4696}
4697
4698
4699typedef int (*state_eval_func)(struct pvr2_hdw *);
4700
4701/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004702static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004703 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004704 state_eval_pipeline_config,
4705 state_eval_encoder_ok,
4706 state_eval_encoder_config,
4707 state_eval_decoder_run,
4708 state_eval_encoder_run,
4709 state_eval_usbstream_run,
4710};
4711
4712
4713/* Process various states and return true if we did anything interesting. */
4714static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4715{
4716 unsigned int i;
4717 int state_updated = 0;
4718 int check_flag;
4719
4720 if (!hdw->state_stale) return 0;
4721 if ((hdw->fw1_state != FW1_STATE_OK) ||
4722 !hdw->flag_ok) {
4723 hdw->state_stale = 0;
4724 return !0;
4725 }
4726 /* This loop is the heart of the entire driver. It keeps trying to
4727 evaluate various bits of driver state until nothing changes for
4728 one full iteration. Each "bit of state" tracks some global
4729 aspect of the driver, e.g. whether decoder should run, if
4730 pipeline is configured, usb streaming is on, etc. We separately
4731 evaluate each of those questions based on other driver state to
4732 arrive at the correct running configuration. */
4733 do {
4734 check_flag = 0;
4735 state_update_pipeline_state(hdw);
4736 /* Iterate over each bit of state */
4737 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4738 if ((*eval_funcs[i])(hdw)) {
4739 check_flag = !0;
4740 state_updated = !0;
4741 state_update_pipeline_state(hdw);
4742 }
4743 }
4744 } while (check_flag && hdw->flag_ok);
4745 hdw->state_stale = 0;
4746 trace_stbit("state_stale",hdw->state_stale);
4747 return state_updated;
4748}
4749
4750
Mike Isely1cb03b72008-04-21 03:47:43 -03004751static unsigned int print_input_mask(unsigned int msk,
4752 char *buf,unsigned int acnt)
4753{
4754 unsigned int idx,ccnt;
4755 unsigned int tcnt = 0;
4756 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4757 if (!((1 << idx) & msk)) continue;
4758 ccnt = scnprintf(buf+tcnt,
4759 acnt-tcnt,
4760 "%s%s",
4761 (tcnt ? ", " : ""),
4762 control_values_input[idx]);
4763 tcnt += ccnt;
4764 }
4765 return tcnt;
4766}
4767
4768
Mike Isely62433e32008-04-22 14:45:40 -03004769static const char *pvr2_pathway_state_name(int id)
4770{
4771 switch (id) {
4772 case PVR2_PATHWAY_ANALOG: return "analog";
4773 case PVR2_PATHWAY_DIGITAL: return "digital";
4774 default: return "unknown";
4775 }
4776}
4777
4778
Mike Isely681c7392007-11-26 01:48:52 -03004779static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4780 char *buf,unsigned int acnt)
4781{
4782 switch (which) {
4783 case 0:
4784 return scnprintf(
4785 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004786 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004787 (hdw->flag_ok ? " <ok>" : " <fail>"),
4788 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4789 (hdw->flag_disconnected ? " <disconnected>" :
4790 " <connected>"),
4791 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004792 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4793 pvr2_pathway_state_name(hdw->pathway_state));
4794
Mike Isely681c7392007-11-26 01:48:52 -03004795 case 1:
4796 return scnprintf(
4797 buf,acnt,
4798 "pipeline:%s%s%s%s",
4799 (hdw->state_pipeline_idle ? " <idle>" : ""),
4800 (hdw->state_pipeline_config ?
4801 " <configok>" : " <stale>"),
4802 (hdw->state_pipeline_req ? " <req>" : ""),
4803 (hdw->state_pipeline_pause ? " <pause>" : ""));
4804 case 2:
4805 return scnprintf(
4806 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004807 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004808 (hdw->state_decoder_run ?
4809 " <decode:run>" :
4810 (hdw->state_decoder_quiescent ?
4811 "" : " <decode:stop>")),
4812 (hdw->state_decoder_quiescent ?
4813 " <decode:quiescent>" : ""),
4814 (hdw->state_encoder_ok ?
4815 "" : " <encode:init>"),
4816 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004817 (hdw->state_encoder_runok ?
4818 " <encode:run>" :
4819 " <encode:firstrun>") :
4820 (hdw->state_encoder_runok ?
4821 " <encode:stop>" :
4822 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004823 (hdw->state_encoder_config ?
4824 " <encode:configok>" :
4825 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004826 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004827 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004828 " <usb:run>" : " <usb:stop>"),
4829 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004830 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004831 case 3:
4832 return scnprintf(
4833 buf,acnt,
4834 "state: %s",
4835 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004836 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004837 unsigned int tcnt = 0;
4838 unsigned int ccnt;
4839
4840 ccnt = scnprintf(buf,
4841 acnt,
4842 "Hardware supported inputs: ");
4843 tcnt += ccnt;
4844 tcnt += print_input_mask(hdw->input_avail_mask,
4845 buf+tcnt,
4846 acnt-tcnt);
4847 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4848 ccnt = scnprintf(buf+tcnt,
4849 acnt-tcnt,
4850 "; allowed inputs: ");
4851 tcnt += ccnt;
4852 tcnt += print_input_mask(hdw->input_allowed_mask,
4853 buf+tcnt,
4854 acnt-tcnt);
4855 }
4856 return tcnt;
4857 }
4858 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004859 struct pvr2_stream_stats stats;
4860 if (!hdw->vid_stream) break;
4861 pvr2_stream_get_stats(hdw->vid_stream,
4862 &stats,
4863 0);
4864 return scnprintf(
4865 buf,acnt,
4866 "Bytes streamed=%u"
4867 " URBs: queued=%u idle=%u ready=%u"
4868 " processed=%u failed=%u",
4869 stats.bytes_processed,
4870 stats.buffers_in_queue,
4871 stats.buffers_in_idle,
4872 stats.buffers_in_ready,
4873 stats.buffers_processed,
4874 stats.buffers_failed);
4875 }
Mike Isely858f9102009-03-07 00:28:28 -03004876 case 6: {
4877 struct v4l2_subdev *sd;
4878 unsigned int tcnt = 0;
4879 unsigned int ccnt;
4880 const char *p;
4881 unsigned int id;
4882 ccnt = scnprintf(buf,
4883 acnt,
Mike Isely5f757dd2009-03-07 01:39:40 -03004884 "Associated v4l2_subdev drivers:");
Mike Isely858f9102009-03-07 00:28:28 -03004885 tcnt += ccnt;
4886 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
4887 id = sd->grp_id;
4888 p = NULL;
4889 if (id < ARRAY_SIZE(module_names)) {
4890 p = module_names[id];
4891 }
Mike Isely5f757dd2009-03-07 01:39:40 -03004892 if (p) {
4893 ccnt = scnprintf(buf + tcnt,
4894 acnt - tcnt,
4895 " %s", p);
4896 } else {
4897 ccnt = scnprintf(buf + tcnt,
4898 acnt - tcnt,
4899 " (unknown id=%u)", id);
4900 }
4901 tcnt += ccnt;
Mike Isely858f9102009-03-07 00:28:28 -03004902 }
4903 return tcnt;
4904 }
Mike Isely681c7392007-11-26 01:48:52 -03004905 default: break;
4906 }
4907 return 0;
4908}
4909
4910
4911unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4912 char *buf,unsigned int acnt)
4913{
4914 unsigned int bcnt,ccnt,idx;
4915 bcnt = 0;
4916 LOCK_TAKE(hdw->big_lock);
4917 for (idx = 0; ; idx++) {
4918 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4919 if (!ccnt) break;
4920 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4921 if (!acnt) break;
4922 buf[0] = '\n'; ccnt = 1;
4923 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4924 }
4925 LOCK_GIVE(hdw->big_lock);
4926 return bcnt;
4927}
4928
4929
4930static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4931{
4932 char buf[128];
4933 unsigned int idx,ccnt;
4934
4935 for (idx = 0; ; idx++) {
4936 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4937 if (!ccnt) break;
4938 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4939 }
4940}
4941
4942
4943/* Evaluate and update the driver's current state, taking various actions
4944 as appropriate for the update. */
4945static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4946{
4947 unsigned int st;
4948 int state_updated = 0;
4949 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004950 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004951
4952 pvr2_trace(PVR2_TRACE_STBITS,
4953 "Drive state check START");
4954 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4955 pvr2_hdw_state_log_state(hdw);
4956 }
4957
4958 /* Process all state and get back over disposition */
4959 state_updated = pvr2_hdw_state_update(hdw);
4960
Mike Isely1b9c18c2008-04-22 14:45:41 -03004961 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4962
Mike Isely681c7392007-11-26 01:48:52 -03004963 /* Update master state based upon all other states. */
4964 if (!hdw->flag_ok) {
4965 st = PVR2_STATE_DEAD;
4966 } else if (hdw->fw1_state != FW1_STATE_OK) {
4967 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004968 } else if ((analog_mode ||
4969 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4970 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004971 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004972 } else if (hdw->flag_tripped ||
4973 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004974 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004975 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004976 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004977 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004978 st = PVR2_STATE_RUN;
4979 } else {
4980 st = PVR2_STATE_READY;
4981 }
4982 if (hdw->master_state != st) {
4983 pvr2_trace(PVR2_TRACE_STATE,
4984 "Device state change from %s to %s",
4985 pvr2_get_state_name(hdw->master_state),
4986 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004987 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004988 hdw->master_state = st;
4989 state_updated = !0;
4990 callback_flag = !0;
4991 }
4992 if (state_updated) {
4993 /* Trigger anyone waiting on any state changes here. */
4994 wake_up(&hdw->state_wait_data);
4995 }
4996
4997 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4998 pvr2_hdw_state_log_state(hdw);
4999 }
5000 pvr2_trace(PVR2_TRACE_STBITS,
5001 "Drive state check DONE callback=%d",callback_flag);
5002
5003 return callback_flag;
5004}
5005
5006
5007/* Cause kernel thread to check / update driver state */
5008static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
5009{
5010 if (hdw->state_stale) return;
5011 hdw->state_stale = !0;
5012 trace_stbit("state_stale",hdw->state_stale);
5013 queue_work(hdw->workqueue,&hdw->workpoll);
5014}
5015
5016
Mike Iselyd8554972006-06-26 20:58:46 -03005017int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
5018{
5019 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
5020}
5021
5022
5023int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
5024{
5025 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
5026}
5027
5028
5029int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
5030{
5031 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
5032}
5033
5034
5035int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
5036{
5037 u32 cval,nval;
5038 int ret;
5039 if (~msk) {
5040 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
5041 if (ret) return ret;
5042 nval = (cval & ~msk) | (val & msk);
5043 pvr2_trace(PVR2_TRACE_GPIO,
5044 "GPIO direction changing 0x%x:0x%x"
5045 " from 0x%x to 0x%x",
5046 msk,val,cval,nval);
5047 } else {
5048 nval = val;
5049 pvr2_trace(PVR2_TRACE_GPIO,
5050 "GPIO direction changing to 0x%x",nval);
5051 }
5052 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
5053}
5054
5055
5056int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
5057{
5058 u32 cval,nval;
5059 int ret;
5060 if (~msk) {
5061 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
5062 if (ret) return ret;
5063 nval = (cval & ~msk) | (val & msk);
5064 pvr2_trace(PVR2_TRACE_GPIO,
5065 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
5066 msk,val,cval,nval);
5067 } else {
5068 nval = val;
5069 pvr2_trace(PVR2_TRACE_GPIO,
5070 "GPIO output changing to 0x%x",nval);
5071 }
5072 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
5073}
5074
5075
Mike Iselya51f5002009-03-06 23:30:37 -03005076void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
5077{
Mike Isely40f07112009-03-07 00:08:17 -03005078 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
5079 memset(vtp, 0, sizeof(*vtp));
Mike Isely2641df32009-03-07 00:13:25 -03005080 hdw->tuner_signal_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005081 pvr2_i2c_core_status_poll(hdw);
Mike Isely40f07112009-03-07 00:08:17 -03005082 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
5083 using v4l2-subdev - therefore we can't support that AT ALL right
5084 now. (Of course, no sub-drivers seem to implement it either.
5085 But now it's a a chicken and egg problem...) */
5086 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner,
5087 &hdw->tuner_signal_info);
Mike Isely2641df32009-03-07 00:13:25 -03005088 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
Mike Isely40f07112009-03-07 00:08:17 -03005089 " type=%u strength=%u audio=0x%x cap=0x%x"
5090 " low=%u hi=%u",
5091 vtp->type,
5092 vtp->signal, vtp->rxsubchans, vtp->capability,
5093 vtp->rangelow, vtp->rangehigh);
Mike Isely2641df32009-03-07 00:13:25 -03005094
5095 /* We have to do this to avoid getting into constant polling if
5096 there's nobody to answer a poll of cropcap info. */
5097 hdw->cropcap_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005098}
5099
5100
Mike Isely7fb20fa2008-04-22 14:45:37 -03005101unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5102{
5103 return hdw->input_avail_mask;
5104}
5105
5106
Mike Isely1cb03b72008-04-21 03:47:43 -03005107unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5108{
5109 return hdw->input_allowed_mask;
5110}
5111
5112
5113static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5114{
5115 if (hdw->input_val != v) {
5116 hdw->input_val = v;
5117 hdw->input_dirty = !0;
5118 }
5119
5120 /* Handle side effects - if we switch to a mode that needs the RF
5121 tuner, then select the right frequency choice as well and mark
5122 it dirty. */
5123 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5124 hdw->freqSelector = 0;
5125 hdw->freqDirty = !0;
5126 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5127 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5128 hdw->freqSelector = 1;
5129 hdw->freqDirty = !0;
5130 }
5131 return 0;
5132}
5133
5134
5135int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5136 unsigned int change_mask,
5137 unsigned int change_val)
5138{
5139 int ret = 0;
5140 unsigned int nv,m,idx;
5141 LOCK_TAKE(hdw->big_lock);
5142 do {
5143 nv = hdw->input_allowed_mask & ~change_mask;
5144 nv |= (change_val & change_mask);
5145 nv &= hdw->input_avail_mask;
5146 if (!nv) {
5147 /* No legal modes left; return error instead. */
5148 ret = -EPERM;
5149 break;
5150 }
5151 hdw->input_allowed_mask = nv;
5152 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5153 /* Current mode is still in the allowed mask, so
5154 we're done. */
5155 break;
5156 }
5157 /* Select and switch to a mode that is still in the allowed
5158 mask */
5159 if (!hdw->input_allowed_mask) {
5160 /* Nothing legal; give up */
5161 break;
5162 }
5163 m = hdw->input_allowed_mask;
5164 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5165 if (!((1 << idx) & m)) continue;
5166 pvr2_hdw_set_input(hdw,idx);
5167 break;
5168 }
5169 } while (0);
5170 LOCK_GIVE(hdw->big_lock);
5171 return ret;
5172}
5173
5174
Mike Iselye61b6fc2006-07-18 22:42:18 -03005175/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03005176static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03005177{
5178 int result;
5179 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03005180 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03005181 result = pvr2_send_request(hdw,
5182 hdw->cmd_buffer,1,
5183 hdw->cmd_buffer,1);
5184 if (result < 0) break;
5185 result = hdw->cmd_buffer[0];
5186 } while(0); LOCK_GIVE(hdw->ctl_lock);
5187 return result;
5188}
5189
5190
Mike Isely32ffa9a2006-09-23 22:26:52 -03005191int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005192 struct v4l2_dbg_match *match, u64 reg_id,
5193 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03005194{
5195#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03005196 struct pvr2_i2c_client *cp;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005197 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03005198 int stat = 0;
5199 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005200
Mike Isely201f5c92007-01-28 16:08:36 -03005201 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5202
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005203 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005204 req.reg = reg_id;
5205 if (setFl) req.val = *val_ptr;
Mike Iselyd8f5b9b2009-03-07 00:05:00 -03005206 /* It would be nice to know if a sub-device answered the request */
5207 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5208 if (!setFl) *val_ptr = req.val;
5209 if (!okFl) mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03005210 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03005211 if (!v4l2_chip_match_i2c_client(
5212 cp->client,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005213 &req.match)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03005214 continue;
5215 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005216 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03005217 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
5218 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03005219 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03005220 okFl = !0;
5221 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005222 }
5223 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03005224 if (okFl) {
5225 return stat;
5226 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005227 return -EINVAL;
5228#else
5229 return -ENOSYS;
5230#endif
5231}
5232
5233
Mike Iselyd8554972006-06-26 20:58:46 -03005234/*
5235 Stuff for Emacs to see, in order to encourage consistent editing style:
5236 *** Local Variables: ***
5237 *** mode: c ***
5238 *** fill-column: 75 ***
5239 *** tab-width: 8 ***
5240 *** c-basic-offset: 8 ***
5241 *** End: ***
5242 */