blob: 89d5bb4f88bd075aac4cfa3ce844f562a83d06a6 [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 Isely989eb152007-11-26 01:53:12 -03002188 if (!hdw->hdw_desc->flag_no_powerup) {
Mike Isely1d643a32007-09-08 22:18:50 -03002189 pvr2_hdw_cmd_powerup(hdw);
2190 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002191 }
2192
Mike Isely31335b12008-07-25 19:35:31 -03002193 /* Take the IR chip out of reset, if appropriate */
2194 if (hdw->hdw_desc->ir_scheme == PVR2_IR_SCHEME_ZILOG) {
2195 pvr2_issue_simple_cmd(hdw,
2196 FX2CMD_HCW_ZILOG_RESET |
2197 (1 << 8) |
2198 ((0) << 16));
2199 }
2200
Mike Iselyd8554972006-06-26 20:58:46 -03002201 // This step MUST happen after the earlier powerup step.
Mike Isely59af3362009-03-07 03:06:09 -03002202 pvr2_i2c_track_init(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002203 pvr2_i2c_core_init(hdw);
2204 if (!pvr2_hdw_dev_ok(hdw)) return;
2205
Mike Iselye9c64a72009-03-06 23:42:20 -03002206 pvr2_hdw_load_modules(hdw);
Mike Isely1ab5e742009-03-07 00:24:24 -03002207 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselye9c64a72009-03-06 23:42:20 -03002208
Mike Iselyc05c0462006-06-25 20:04:25 -03002209 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002210 cptr = hdw->controls + idx;
2211 if (cptr->info->skip_init) continue;
2212 if (!cptr->info->set_value) continue;
2213 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2214 }
2215
Mike Isely1bde0282006-12-27 23:30:13 -03002216 /* Set up special default values for the television and radio
2217 frequencies here. It's not really important what these defaults
2218 are, but I set them to something usable in the Chicago area just
2219 to make driver testing a little easier. */
2220
Michael Krufky5a4f5da62008-05-11 16:37:50 -03002221 hdw->freqValTelevision = default_tv_freq;
2222 hdw->freqValRadio = default_radio_freq;
Mike Isely1bde0282006-12-27 23:30:13 -03002223
Mike Iselyd8554972006-06-26 20:58:46 -03002224 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2225 // thread-safe against the normal pvr2_send_request() mechanism.
2226 // (We should make it thread safe).
2227
Mike Iselyaaf78842007-11-26 02:04:11 -03002228 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2229 ret = pvr2_hdw_get_eeprom_addr(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002230 if (!pvr2_hdw_dev_ok(hdw)) return;
Mike Iselyaaf78842007-11-26 02:04:11 -03002231 if (ret < 0) {
2232 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2233 "Unable to determine location of eeprom,"
2234 " skipping");
2235 } else {
2236 hdw->eeprom_addr = ret;
2237 pvr2_eeprom_analyze(hdw);
2238 if (!pvr2_hdw_dev_ok(hdw)) return;
2239 }
2240 } else {
2241 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2242 hdw->tuner_updated = !0;
2243 hdw->std_mask_eeprom = V4L2_STD_ALL;
Mike Iselyd8554972006-06-26 20:58:46 -03002244 }
2245
Mike Isely13a88792009-01-14 04:22:56 -03002246 if (hdw->serial_number) {
2247 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2248 "sn-%lu", hdw->serial_number);
2249 } else if (hdw->unit_number >= 0) {
2250 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2251 "unit-%c",
2252 hdw->unit_number + 'a');
2253 } else {
2254 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2255 "unit-??");
2256 }
2257 hdw->identifier[idx] = 0;
2258
Mike Iselyd8554972006-06-26 20:58:46 -03002259 pvr2_hdw_setup_std(hdw);
2260
2261 if (!get_default_tuner_type(hdw)) {
2262 pvr2_trace(PVR2_TRACE_INIT,
2263 "pvr2_hdw_setup: Tuner type overridden to %d",
2264 hdw->tuner_type);
2265 }
2266
Mike Iselyd8554972006-06-26 20:58:46 -03002267 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002268
2269 if (!pvr2_hdw_dev_ok(hdw)) return;
2270
Mike Isely1df59f02008-04-21 03:50:39 -03002271 if (hdw->hdw_desc->signal_routing_scheme ==
2272 PVR2_ROUTING_SCHEME_GOTVIEW) {
2273 /* Ensure that GPIO 11 is set to output for GOTVIEW
2274 hardware. */
2275 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2276 }
2277
Mike Isely681c7392007-11-26 01:48:52 -03002278 pvr2_hdw_commit_setup(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002279
2280 hdw->vid_stream = pvr2_stream_create();
2281 if (!pvr2_hdw_dev_ok(hdw)) return;
2282 pvr2_trace(PVR2_TRACE_INIT,
2283 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2284 if (hdw->vid_stream) {
2285 idx = get_default_error_tolerance(hdw);
2286 if (idx) {
2287 pvr2_trace(PVR2_TRACE_INIT,
2288 "pvr2_hdw_setup: video stream %p"
2289 " setting tolerance %u",
2290 hdw->vid_stream,idx);
2291 }
2292 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2293 PVR2_VID_ENDPOINT,idx);
2294 }
2295
2296 if (!pvr2_hdw_dev_ok(hdw)) return;
2297
Mike Iselyd8554972006-06-26 20:58:46 -03002298 hdw->flag_init_ok = !0;
Mike Isely681c7392007-11-26 01:48:52 -03002299
2300 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002301}
2302
2303
Mike Isely681c7392007-11-26 01:48:52 -03002304/* Set up the structure and attempt to put the device into a usable state.
2305 This can be a time-consuming operation, which is why it is not done
2306 internally as part of the create() step. */
2307static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002308{
2309 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002310 do {
Mike Iselyd8554972006-06-26 20:58:46 -03002311 pvr2_hdw_setup_low(hdw);
2312 pvr2_trace(PVR2_TRACE_INIT,
2313 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
Mike Isely681c7392007-11-26 01:48:52 -03002314 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
Mike Iselyd8554972006-06-26 20:58:46 -03002315 if (pvr2_hdw_dev_ok(hdw)) {
Mike Isely681c7392007-11-26 01:48:52 -03002316 if (hdw->flag_init_ok) {
Mike Iselyd8554972006-06-26 20:58:46 -03002317 pvr2_trace(
2318 PVR2_TRACE_INFO,
2319 "Device initialization"
2320 " completed successfully.");
2321 break;
2322 }
2323 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2324 pvr2_trace(
2325 PVR2_TRACE_INFO,
2326 "Device microcontroller firmware"
2327 " (re)loaded; it should now reset"
2328 " and reconnect.");
2329 break;
2330 }
2331 pvr2_trace(
2332 PVR2_TRACE_ERROR_LEGS,
2333 "Device initialization was not successful.");
2334 if (hdw->fw1_state == FW1_STATE_MISSING) {
2335 pvr2_trace(
2336 PVR2_TRACE_ERROR_LEGS,
2337 "Giving up since device"
2338 " microcontroller firmware"
2339 " appears to be missing.");
2340 break;
2341 }
2342 }
2343 if (procreload) {
2344 pvr2_trace(
2345 PVR2_TRACE_ERROR_LEGS,
2346 "Attempting pvrusb2 recovery by reloading"
2347 " primary firmware.");
2348 pvr2_trace(
2349 PVR2_TRACE_ERROR_LEGS,
2350 "If this works, device should disconnect"
2351 " and reconnect in a sane state.");
2352 hdw->fw1_state = FW1_STATE_UNKNOWN;
2353 pvr2_upload_firmware1(hdw);
2354 } else {
2355 pvr2_trace(
2356 PVR2_TRACE_ERROR_LEGS,
2357 "***WARNING*** pvrusb2 device hardware"
2358 " appears to be jammed"
2359 " and I can't clear it.");
2360 pvr2_trace(
2361 PVR2_TRACE_ERROR_LEGS,
2362 "You might need to power cycle"
2363 " the pvrusb2 device"
2364 " in order to recover.");
2365 }
Mike Isely681c7392007-11-26 01:48:52 -03002366 } while (0);
Mike Iselyd8554972006-06-26 20:58:46 -03002367 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002368}
2369
2370
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002371/* Perform second stage initialization. Set callback pointer first so that
2372 we can avoid a possible initialization race (if the kernel thread runs
2373 before the callback has been set). */
Mike Isely794b1602008-04-22 14:45:45 -03002374int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2375 void (*callback_func)(void *),
2376 void *callback_data)
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002377{
2378 LOCK_TAKE(hdw->big_lock); do {
Mike Isely97f26ff2008-04-07 02:22:43 -03002379 if (hdw->flag_disconnected) {
2380 /* Handle a race here: If we're already
2381 disconnected by this point, then give up. If we
2382 get past this then we'll remain connected for
2383 the duration of initialization since the entire
2384 initialization sequence is now protected by the
2385 big_lock. */
2386 break;
2387 }
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002388 hdw->state_data = callback_data;
2389 hdw->state_func = callback_func;
Mike Isely97f26ff2008-04-07 02:22:43 -03002390 pvr2_hdw_setup(hdw);
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002391 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely794b1602008-04-22 14:45:45 -03002392 return hdw->flag_init_ok;
Mike Iselyc4a8828d2008-04-22 14:45:44 -03002393}
2394
2395
2396/* Create, set up, and return a structure for interacting with the
2397 underlying hardware. */
Mike Iselyd8554972006-06-26 20:58:46 -03002398struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2399 const struct usb_device_id *devid)
2400{
Mike Isely7fb20fa2008-04-22 14:45:37 -03002401 unsigned int idx,cnt1,cnt2,m;
Mike Iselyfe15f132008-08-30 18:11:40 -03002402 struct pvr2_hdw *hdw = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002403 int valid_std_mask;
2404 struct pvr2_ctrl *cptr;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002405 struct usb_device *usb_dev;
Mike Isely989eb152007-11-26 01:53:12 -03002406 const struct pvr2_device_desc *hdw_desc;
Mike Iselyd8554972006-06-26 20:58:46 -03002407 __u8 ifnum;
Mike Iselyb30d2442006-06-25 20:05:01 -03002408 struct v4l2_queryctrl qctrl;
2409 struct pvr2_ctl_info *ciptr;
Mike Iselyd8554972006-06-26 20:58:46 -03002410
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002411 usb_dev = interface_to_usbdev(intf);
2412
Mike Iselyd130fa82007-12-08 17:20:06 -03002413 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
Mike Iselyd8554972006-06-26 20:58:46 -03002414
Mike Iselyfe15f132008-08-30 18:11:40 -03002415 if (hdw_desc == NULL) {
2416 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2417 " No device description pointer,"
2418 " unable to continue.");
2419 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2420 " please contact Mike Isely <isely@pobox.com>"
2421 " to get it included in the driver\n");
2422 goto fail;
2423 }
2424
Mike Iselyca545f72007-01-20 00:37:11 -03002425 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03002426 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
Mike Isely989eb152007-11-26 01:53:12 -03002427 hdw,hdw_desc->description);
Mike Iselyd8554972006-06-26 20:58:46 -03002428 if (!hdw) goto fail;
Mike Isely681c7392007-11-26 01:48:52 -03002429
2430 init_timer(&hdw->quiescent_timer);
2431 hdw->quiescent_timer.data = (unsigned long)hdw;
2432 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2433
2434 init_timer(&hdw->encoder_wait_timer);
2435 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2436 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2437
Mike Iselyd913d632008-04-06 04:04:35 -03002438 init_timer(&hdw->encoder_run_timer);
2439 hdw->encoder_run_timer.data = (unsigned long)hdw;
2440 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2441
Mike Isely681c7392007-11-26 01:48:52 -03002442 hdw->master_state = PVR2_STATE_DEAD;
2443
2444 init_waitqueue_head(&hdw->state_wait_data);
2445
Mike Isely18103c572007-01-20 00:09:47 -03002446 hdw->tuner_signal_stale = !0;
Mike Iselyb30d2442006-06-25 20:05:01 -03002447 cx2341x_fill_defaults(&hdw->enc_ctl_state);
Mike Iselyd8554972006-06-26 20:58:46 -03002448
Mike Isely7fb20fa2008-04-22 14:45:37 -03002449 /* Calculate which inputs are OK */
2450 m = 0;
2451 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
Mike Iselye8f5bac2008-04-22 14:45:40 -03002452 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2453 m |= 1 << PVR2_CVAL_INPUT_DTV;
2454 }
Mike Isely7fb20fa2008-04-22 14:45:37 -03002455 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2456 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2457 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2458 hdw->input_avail_mask = m;
Mike Isely1cb03b72008-04-21 03:47:43 -03002459 hdw->input_allowed_mask = hdw->input_avail_mask;
Mike Isely7fb20fa2008-04-22 14:45:37 -03002460
Mike Isely62433e32008-04-22 14:45:40 -03002461 /* If not a hybrid device, pathway_state never changes. So
2462 initialize it here to what it should forever be. */
2463 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2464 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2465 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2466 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2467 }
2468
Mike Iselyc05c0462006-06-25 20:04:25 -03002469 hdw->control_cnt = CTRLDEF_COUNT;
Mike Iselyb30d2442006-06-25 20:05:01 -03002470 hdw->control_cnt += MPEGDEF_COUNT;
Mike Iselyca545f72007-01-20 00:37:11 -03002471 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
Mike Iselyd8554972006-06-26 20:58:46 -03002472 GFP_KERNEL);
2473 if (!hdw->controls) goto fail;
Mike Isely989eb152007-11-26 01:53:12 -03002474 hdw->hdw_desc = hdw_desc;
Mike Iselyc05c0462006-06-25 20:04:25 -03002475 for (idx = 0; idx < hdw->control_cnt; idx++) {
2476 cptr = hdw->controls + idx;
2477 cptr->hdw = hdw;
2478 }
Mike Iselyd8554972006-06-26 20:58:46 -03002479 for (idx = 0; idx < 32; idx++) {
2480 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2481 }
Mike Iselyc05c0462006-06-25 20:04:25 -03002482 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002483 cptr = hdw->controls + idx;
Mike Iselyd8554972006-06-26 20:58:46 -03002484 cptr->info = control_defs+idx;
2485 }
Mike Iselydbc40a02008-04-22 14:45:39 -03002486
2487 /* Ensure that default input choice is a valid one. */
2488 m = hdw->input_avail_mask;
2489 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2490 if (!((1 << idx) & m)) continue;
2491 hdw->input_val = idx;
2492 break;
2493 }
2494
Mike Iselyb30d2442006-06-25 20:05:01 -03002495 /* Define and configure additional controls from cx2341x module. */
Mike Iselyca545f72007-01-20 00:37:11 -03002496 hdw->mpeg_ctrl_info = kzalloc(
Mike Iselyb30d2442006-06-25 20:05:01 -03002497 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2498 if (!hdw->mpeg_ctrl_info) goto fail;
Mike Iselyb30d2442006-06-25 20:05:01 -03002499 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2500 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2501 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2502 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2503 ciptr->name = mpeg_ids[idx].strid;
2504 ciptr->v4l_id = mpeg_ids[idx].id;
2505 ciptr->skip_init = !0;
2506 ciptr->get_value = ctrl_cx2341x_get;
2507 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2508 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2509 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2510 qctrl.id = ciptr->v4l_id;
2511 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2512 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2513 ciptr->set_value = ctrl_cx2341x_set;
2514 }
2515 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2516 PVR2_CTLD_INFO_DESC_SIZE);
2517 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2518 ciptr->default_value = qctrl.default_value;
2519 switch (qctrl.type) {
2520 default:
2521 case V4L2_CTRL_TYPE_INTEGER:
2522 ciptr->type = pvr2_ctl_int;
2523 ciptr->def.type_int.min_value = qctrl.minimum;
2524 ciptr->def.type_int.max_value = qctrl.maximum;
2525 break;
2526 case V4L2_CTRL_TYPE_BOOLEAN:
2527 ciptr->type = pvr2_ctl_bool;
2528 break;
2529 case V4L2_CTRL_TYPE_MENU:
2530 ciptr->type = pvr2_ctl_enum;
2531 ciptr->def.type_enum.value_names =
Hans Verkuile0e31cd2008-06-22 12:03:28 -03002532 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2533 ciptr->v4l_id);
Mike Iselyb30d2442006-06-25 20:05:01 -03002534 for (cnt1 = 0;
2535 ciptr->def.type_enum.value_names[cnt1] != NULL;
2536 cnt1++) { }
2537 ciptr->def.type_enum.count = cnt1;
2538 break;
2539 }
2540 cptr->info = ciptr;
2541 }
Mike Iselyd8554972006-06-26 20:58:46 -03002542
2543 // Initialize video standard enum dynamic control
2544 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2545 if (cptr) {
2546 memcpy(&hdw->std_info_enum,cptr->info,
2547 sizeof(hdw->std_info_enum));
2548 cptr->info = &hdw->std_info_enum;
2549
2550 }
2551 // Initialize control data regarding video standard masks
2552 valid_std_mask = pvr2_std_get_usable();
2553 for (idx = 0; idx < 32; idx++) {
2554 if (!(valid_std_mask & (1 << idx))) continue;
2555 cnt1 = pvr2_std_id_to_str(
2556 hdw->std_mask_names[idx],
2557 sizeof(hdw->std_mask_names[idx])-1,
2558 1 << idx);
2559 hdw->std_mask_names[idx][cnt1] = 0;
2560 }
2561 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2562 if (cptr) {
2563 memcpy(&hdw->std_info_avail,cptr->info,
2564 sizeof(hdw->std_info_avail));
2565 cptr->info = &hdw->std_info_avail;
2566 hdw->std_info_avail.def.type_bitmask.bit_names =
2567 hdw->std_mask_ptrs;
2568 hdw->std_info_avail.def.type_bitmask.valid_bits =
2569 valid_std_mask;
2570 }
2571 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2572 if (cptr) {
2573 memcpy(&hdw->std_info_cur,cptr->info,
2574 sizeof(hdw->std_info_cur));
2575 cptr->info = &hdw->std_info_cur;
2576 hdw->std_info_cur.def.type_bitmask.bit_names =
2577 hdw->std_mask_ptrs;
2578 hdw->std_info_avail.def.type_bitmask.valid_bits =
2579 valid_std_mask;
2580 }
2581
Mike Isely432907f2008-08-31 21:02:20 -03002582 hdw->cropcap_stale = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002583 hdw->eeprom_addr = -1;
2584 hdw->unit_number = -1;
Mike Isely80793842006-12-27 23:12:28 -03002585 hdw->v4l_minor_number_video = -1;
2586 hdw->v4l_minor_number_vbi = -1;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03002587 hdw->v4l_minor_number_radio = -1;
Mike Iselyd8554972006-06-26 20:58:46 -03002588 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2589 if (!hdw->ctl_write_buffer) goto fail;
2590 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2591 if (!hdw->ctl_read_buffer) goto fail;
2592 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2593 if (!hdw->ctl_write_urb) goto fail;
2594 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2595 if (!hdw->ctl_read_urb) goto fail;
2596
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002597 if (v4l2_device_register(&usb_dev->dev, &hdw->v4l2_dev) != 0) {
2598 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2599 "Error registering with v4l core, giving up");
2600 goto fail;
2601 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002602 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002603 for (idx = 0; idx < PVR_NUM; idx++) {
2604 if (unit_pointers[idx]) continue;
2605 hdw->unit_number = idx;
2606 unit_pointers[idx] = hdw;
2607 break;
2608 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002609 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mike Iselyd8554972006-06-26 20:58:46 -03002610
2611 cnt1 = 0;
2612 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2613 cnt1 += cnt2;
2614 if (hdw->unit_number >= 0) {
2615 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2616 ('a' + hdw->unit_number));
2617 cnt1 += cnt2;
2618 }
2619 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2620 hdw->name[cnt1] = 0;
2621
Mike Isely681c7392007-11-26 01:48:52 -03002622 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2623 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2624 INIT_WORK(&hdw->worki2csync,pvr2_hdw_worker_i2c);
Mike Isely681c7392007-11-26 01:48:52 -03002625
Mike Iselyd8554972006-06-26 20:58:46 -03002626 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2627 hdw->unit_number,hdw->name);
2628
2629 hdw->tuner_type = -1;
2630 hdw->flag_ok = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03002631
2632 hdw->usb_intf = intf;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002633 hdw->usb_dev = usb_dev;
Mike Iselyd8554972006-06-26 20:58:46 -03002634
Mike Isely87e34952009-01-23 01:20:24 -03002635 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
Mike Isely31a18542007-04-08 01:11:47 -03002636
Mike Iselyd8554972006-06-26 20:58:46 -03002637 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2638 usb_set_interface(hdw->usb_dev,ifnum,0);
2639
2640 mutex_init(&hdw->ctl_lock_mutex);
2641 mutex_init(&hdw->big_lock_mutex);
2642
2643 return hdw;
2644 fail:
2645 if (hdw) {
Mike Isely681c7392007-11-26 01:48:52 -03002646 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002647 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03002648 del_timer_sync(&hdw->encoder_wait_timer);
2649 if (hdw->workqueue) {
2650 flush_workqueue(hdw->workqueue);
2651 destroy_workqueue(hdw->workqueue);
2652 hdw->workqueue = NULL;
2653 }
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01002654 usb_free_urb(hdw->ctl_read_urb);
2655 usb_free_urb(hdw->ctl_write_urb);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002656 kfree(hdw->ctl_read_buffer);
2657 kfree(hdw->ctl_write_buffer);
2658 kfree(hdw->controls);
2659 kfree(hdw->mpeg_ctrl_info);
Mike Isely681c7392007-11-26 01:48:52 -03002660 kfree(hdw->std_defs);
2661 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002662 kfree(hdw);
2663 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002664 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002665}
2666
2667
2668/* Remove _all_ associations between this driver and the underlying USB
2669 layer. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03002670static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002671{
2672 if (hdw->flag_disconnected) return;
2673 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2674 if (hdw->ctl_read_urb) {
2675 usb_kill_urb(hdw->ctl_read_urb);
2676 usb_free_urb(hdw->ctl_read_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002677 hdw->ctl_read_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002678 }
2679 if (hdw->ctl_write_urb) {
2680 usb_kill_urb(hdw->ctl_write_urb);
2681 usb_free_urb(hdw->ctl_write_urb);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002682 hdw->ctl_write_urb = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002683 }
2684 if (hdw->ctl_read_buffer) {
2685 kfree(hdw->ctl_read_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002686 hdw->ctl_read_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002687 }
2688 if (hdw->ctl_write_buffer) {
2689 kfree(hdw->ctl_write_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002690 hdw->ctl_write_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002691 }
Mike Iselyd8554972006-06-26 20:58:46 -03002692 hdw->flag_disconnected = !0;
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002693 /* If we don't do this, then there will be a dangling struct device
2694 reference to our disappearing device persisting inside the V4L
2695 core... */
2696 if (hdw->v4l2_dev.dev) {
2697 dev_set_drvdata(hdw->v4l2_dev.dev, NULL);
2698 hdw->v4l2_dev.dev = NULL;
2699 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002700 hdw->usb_dev = NULL;
2701 hdw->usb_intf = NULL;
Mike Isely681c7392007-11-26 01:48:52 -03002702 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03002703}
2704
2705
2706/* Destroy hardware interaction structure */
2707void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2708{
Mike Isely401c27c2007-09-08 22:11:46 -03002709 if (!hdw) return;
Mike Iselyd8554972006-06-26 20:58:46 -03002710 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
Mike Isely681c7392007-11-26 01:48:52 -03002711 if (hdw->workqueue) {
2712 flush_workqueue(hdw->workqueue);
2713 destroy_workqueue(hdw->workqueue);
2714 hdw->workqueue = NULL;
2715 }
Mike Isely8f591002008-04-22 14:45:45 -03002716 del_timer_sync(&hdw->quiescent_timer);
Mike Iselyd913d632008-04-06 04:04:35 -03002717 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely8f591002008-04-22 14:45:45 -03002718 del_timer_sync(&hdw->encoder_wait_timer);
Mike Iselyd8554972006-06-26 20:58:46 -03002719 if (hdw->fw_buffer) {
2720 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002721 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002722 }
2723 if (hdw->vid_stream) {
2724 pvr2_stream_destroy(hdw->vid_stream);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002725 hdw->vid_stream = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002726 }
Mike Iselyd8554972006-06-26 20:58:46 -03002727 if (hdw->decoder_ctrl) {
2728 hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
2729 }
2730 pvr2_i2c_core_done(hdw);
Mike Isely59af3362009-03-07 03:06:09 -03002731 pvr2_i2c_track_done(hdw);
Mike Iselyb72b7bf2009-03-06 23:20:31 -03002732 v4l2_device_unregister(&hdw->v4l2_dev);
Mike Iselyd8554972006-06-26 20:58:46 -03002733 pvr2_hdw_remove_usb_stuff(hdw);
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002734 mutex_lock(&pvr2_unit_mtx); do {
Mike Iselyd8554972006-06-26 20:58:46 -03002735 if ((hdw->unit_number >= 0) &&
2736 (hdw->unit_number < PVR_NUM) &&
2737 (unit_pointers[hdw->unit_number] == hdw)) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002738 unit_pointers[hdw->unit_number] = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002739 }
Matthias Kaehlcke8df0c872007-04-28 20:00:18 -03002740 } while (0); mutex_unlock(&pvr2_unit_mtx);
Mariusz Kozlowski22071a42007-01-07 10:33:39 -03002741 kfree(hdw->controls);
2742 kfree(hdw->mpeg_ctrl_info);
2743 kfree(hdw->std_defs);
2744 kfree(hdw->std_enum_names);
Mike Iselyd8554972006-06-26 20:58:46 -03002745 kfree(hdw);
2746}
2747
2748
Mike Iselyd8554972006-06-26 20:58:46 -03002749int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2750{
2751 return (hdw && hdw->flag_ok);
2752}
2753
2754
2755/* Called when hardware has been unplugged */
2756void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2757{
2758 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2759 LOCK_TAKE(hdw->big_lock);
2760 LOCK_TAKE(hdw->ctl_lock);
2761 pvr2_hdw_remove_usb_stuff(hdw);
2762 LOCK_GIVE(hdw->ctl_lock);
2763 LOCK_GIVE(hdw->big_lock);
2764}
2765
2766
2767// Attempt to autoselect an appropriate value for std_enum_cur given
2768// whatever is currently in std_mask_cur
Adrian Bunk07e337e2006-06-30 11:30:20 -03002769static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002770{
2771 unsigned int idx;
2772 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2773 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2774 hdw->std_enum_cur = idx;
2775 return;
2776 }
2777 }
2778 hdw->std_enum_cur = 0;
2779}
2780
2781
2782// Calculate correct set of enumerated standards based on currently known
2783// set of available standards bits.
Adrian Bunk07e337e2006-06-30 11:30:20 -03002784static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03002785{
2786 struct v4l2_standard *newstd;
2787 unsigned int std_cnt;
2788 unsigned int idx;
2789
2790 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2791
2792 if (hdw->std_defs) {
2793 kfree(hdw->std_defs);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002794 hdw->std_defs = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002795 }
2796 hdw->std_enum_cnt = 0;
2797 if (hdw->std_enum_names) {
2798 kfree(hdw->std_enum_names);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002799 hdw->std_enum_names = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002800 }
2801
2802 if (!std_cnt) {
2803 pvr2_trace(
2804 PVR2_TRACE_ERROR_LEGS,
2805 "WARNING: Failed to identify any viable standards");
2806 }
2807 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2808 hdw->std_enum_names[0] = "none";
2809 for (idx = 0; idx < std_cnt; idx++) {
2810 hdw->std_enum_names[idx+1] =
2811 newstd[idx].name;
2812 }
2813 // Set up the dynamic control for this standard
2814 hdw->std_info_enum.def.type_enum.value_names = hdw->std_enum_names;
2815 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2816 hdw->std_defs = newstd;
2817 hdw->std_enum_cnt = std_cnt+1;
2818 hdw->std_enum_cur = 0;
2819 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2820}
2821
2822
2823int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2824 struct v4l2_standard *std,
2825 unsigned int idx)
2826{
2827 int ret = -EINVAL;
2828 if (!idx) return ret;
2829 LOCK_TAKE(hdw->big_lock); do {
2830 if (idx >= hdw->std_enum_cnt) break;
2831 idx--;
2832 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2833 ret = 0;
2834 } while (0); LOCK_GIVE(hdw->big_lock);
2835 return ret;
2836}
2837
2838
2839/* Get the number of defined controls */
2840unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2841{
Mike Iselyc05c0462006-06-25 20:04:25 -03002842 return hdw->control_cnt;
Mike Iselyd8554972006-06-26 20:58:46 -03002843}
2844
2845
2846/* Retrieve a control handle given its index (0..count-1) */
2847struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2848 unsigned int idx)
2849{
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002850 if (idx >= hdw->control_cnt) return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002851 return hdw->controls + idx;
2852}
2853
2854
2855/* Retrieve a control handle given its index (0..count-1) */
2856struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2857 unsigned int ctl_id)
2858{
2859 struct pvr2_ctrl *cptr;
2860 unsigned int idx;
2861 int i;
2862
2863 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002864 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002865 cptr = hdw->controls + idx;
2866 i = cptr->info->internal_id;
2867 if (i && (i == ctl_id)) return cptr;
2868 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002869 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002870}
2871
2872
Mike Iselya761f432006-06-25 20:04:44 -03002873/* Given a V4L ID, retrieve the control structure associated with it. */
Mike Iselyd8554972006-06-26 20:58:46 -03002874struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2875{
2876 struct pvr2_ctrl *cptr;
2877 unsigned int idx;
2878 int i;
2879
2880 /* This could be made a lot more efficient, but for now... */
Mike Iselyc05c0462006-06-25 20:04:25 -03002881 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03002882 cptr = hdw->controls + idx;
2883 i = cptr->info->v4l_id;
2884 if (i && (i == ctl_id)) return cptr;
2885 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002886 return NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03002887}
2888
2889
Mike Iselya761f432006-06-25 20:04:44 -03002890/* Given a V4L ID for its immediate predecessor, retrieve the control
2891 structure associated with it. */
2892struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2893 unsigned int ctl_id)
2894{
2895 struct pvr2_ctrl *cptr,*cp2;
2896 unsigned int idx;
2897 int i;
2898
2899 /* This could be made a lot more efficient, but for now... */
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002900 cp2 = NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002901 for (idx = 0; idx < hdw->control_cnt; idx++) {
2902 cptr = hdw->controls + idx;
2903 i = cptr->info->v4l_id;
2904 if (!i) continue;
2905 if (i <= ctl_id) continue;
2906 if (cp2 && (cp2->info->v4l_id < i)) continue;
2907 cp2 = cptr;
2908 }
2909 return cp2;
Mike Iselya0fd1cb2006-06-30 11:35:28 -03002910 return NULL;
Mike Iselya761f432006-06-25 20:04:44 -03002911}
2912
2913
Mike Iselyd8554972006-06-26 20:58:46 -03002914static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2915{
2916 switch (tp) {
2917 case pvr2_ctl_int: return "integer";
2918 case pvr2_ctl_enum: return "enum";
Mike Isely33213962006-06-25 20:04:40 -03002919 case pvr2_ctl_bool: return "boolean";
Mike Iselyd8554972006-06-26 20:58:46 -03002920 case pvr2_ctl_bitmask: return "bitmask";
2921 }
2922 return "";
2923}
2924
2925
Mike Isely2641df32009-03-07 00:13:25 -03002926static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2927 const char *name, int val)
2928{
2929 struct v4l2_control ctrl;
2930 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2931 memset(&ctrl, 0, sizeof(ctrl));
2932 ctrl.id = id;
2933 ctrl.value = val;
2934 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
2935}
2936
2937#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
2938 if ((hdw)->lab##_dirty) { \
2939 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
2940 }
2941
Mike Isely5ceaad12009-03-07 00:01:20 -03002942/* Execute whatever commands are required to update the state of all the
Mike Isely2641df32009-03-07 00:13:25 -03002943 sub-devices so that they match our current control values. */
Mike Isely5ceaad12009-03-07 00:01:20 -03002944static void pvr2_subdev_update(struct pvr2_hdw *hdw)
2945{
Mike Iselyedb9dcb2009-03-07 00:37:10 -03002946 struct v4l2_subdev *sd;
2947 unsigned int id;
2948 pvr2_subdev_update_func fp;
2949
Mike Isely75212a02009-03-07 01:48:42 -03002950 pvr2_trace(PVR2_TRACE_CHIPS, "subdev update...");
2951
2952 if (hdw->tuner_updated) {
2953 struct tuner_setup setup;
2954 pvr2_trace(PVR2_TRACE_CHIPS, "subdev tuner set_type(%d)",
2955 hdw->tuner_type);
2956 if (((int)(hdw->tuner_type)) >= 0) {
2957 setup.addr = ADDR_UNSET;
2958 setup.type = hdw->tuner_type;
2959 setup.mode_mask = T_RADIO | T_ANALOG_TV;
2960 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2961 tuner, s_type_addr, &setup);
2962 }
2963 }
2964
Mike Isely2641df32009-03-07 00:13:25 -03002965 if (hdw->input_dirty || hdw->std_dirty) {
Mike Iselyb4818802009-03-07 01:46:17 -03002966 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_standard");
Mike Isely2641df32009-03-07 00:13:25 -03002967 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
2968 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2969 tuner, s_radio);
2970 } else {
2971 v4l2_std_id vs;
2972 vs = hdw->std_mask_cur;
2973 v4l2_device_call_all(&hdw->v4l2_dev, 0,
2974 tuner, s_std, vs);
2975 }
2976 hdw->tuner_signal_stale = !0;
2977 hdw->cropcap_stale = !0;
2978 }
2979
2980 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
2981 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
2982 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
2983 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
2984 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
2985 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
2986 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
2987 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
2988 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
2989
2990 if (hdw->input_dirty || hdw->audiomode_dirty) {
2991 struct v4l2_tuner vt;
2992 memset(&vt, 0, sizeof(vt));
2993 vt.audmode = hdw->audiomode_val;
2994 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
2995 }
2996
2997 if (hdw->freqDirty) {
2998 unsigned long fv;
2999 struct v4l2_frequency freq;
3000 fv = pvr2_hdw_get_cur_freq(hdw);
3001 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
3002 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
3003 memset(&freq, 0, sizeof(freq));
3004 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
3005 /* ((fv * 1000) / 62500) */
3006 freq.frequency = (fv * 2) / 125;
3007 } else {
3008 freq.frequency = fv / 62500;
3009 }
3010 /* tuner-core currently doesn't seem to care about this, but
3011 let's set it anyway for completeness. */
3012 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3013 freq.type = V4L2_TUNER_RADIO;
3014 } else {
3015 freq.type = V4L2_TUNER_ANALOG_TV;
3016 }
3017 freq.tuner = 0;
3018 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
3019 s_frequency, &freq);
3020 }
3021
3022 if (hdw->res_hor_dirty || hdw->res_ver_dirty) {
3023 struct v4l2_format fmt;
3024 memset(&fmt, 0, sizeof(fmt));
3025 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3026 fmt.fmt.pix.width = hdw->res_hor_val;
3027 fmt.fmt.pix.height = hdw->res_ver_val;
3028 pvr2_trace(PVR2_TRACE_CHIPS,"subdev v4l2 set_size(%dx%d)",
3029 fmt.fmt.pix.width, fmt.fmt.pix.height);
3030 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_fmt, &fmt);
3031 }
3032
Mike Isely01c59df2009-03-07 00:48:09 -03003033 if (hdw->srate_dirty) {
3034 u32 val;
3035 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_audio %d",
3036 hdw->srate_val);
3037 switch (hdw->srate_val) {
3038 default:
3039 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
3040 val = 48000;
3041 break;
3042 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
3043 val = 44100;
3044 break;
3045 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
3046 val = 32000;
3047 break;
3048 }
3049 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3050 audio, s_clock_freq, val);
3051 }
3052
Mike Isely2641df32009-03-07 00:13:25 -03003053 /* Unable to set crop parameters; there is apparently no equivalent
3054 for VIDIOC_S_CROP */
3055
Mike Iselyedb9dcb2009-03-07 00:37:10 -03003056 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
3057 id = sd->grp_id;
3058 if (id >= ARRAY_SIZE(pvr2_module_update_functions)) continue;
3059 fp = pvr2_module_update_functions[id];
3060 if (!fp) continue;
3061 (*fp)(hdw, sd);
3062 }
Mike Isely2641df32009-03-07 00:13:25 -03003063
3064 if (hdw->tuner_signal_stale && hdw->cropcap_stale) {
3065 pvr2_hdw_status_poll(hdw);
3066 }
Mike Isely5ceaad12009-03-07 00:01:20 -03003067}
3068
3069
Mike Isely681c7392007-11-26 01:48:52 -03003070/* Figure out if we need to commit control changes. If so, mark internal
3071 state flags to indicate this fact and return true. Otherwise do nothing
3072 else and return false. */
3073static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003074{
Mike Iselyd8554972006-06-26 20:58:46 -03003075 unsigned int idx;
3076 struct pvr2_ctrl *cptr;
3077 int value;
3078 int commit_flag = 0;
3079 char buf[100];
3080 unsigned int bcnt,ccnt;
3081
Mike Iselyc05c0462006-06-25 20:04:25 -03003082 for (idx = 0; idx < hdw->control_cnt; idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -03003083 cptr = hdw->controls + idx;
Al Viro5fa12472008-03-29 03:07:38 +00003084 if (!cptr->info->is_dirty) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003085 if (!cptr->info->is_dirty(cptr)) continue;
Mike Iselyfe23a282007-01-20 00:10:55 -03003086 commit_flag = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003087
Mike Iselyfe23a282007-01-20 00:10:55 -03003088 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
Mike Iselyd8554972006-06-26 20:58:46 -03003089 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3090 cptr->info->name);
3091 value = 0;
3092 cptr->info->get_value(cptr,&value);
3093 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3094 buf+bcnt,
3095 sizeof(buf)-bcnt,&ccnt);
3096 bcnt += ccnt;
3097 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3098 get_ctrl_typename(cptr->info->type));
3099 pvr2_trace(PVR2_TRACE_CTL,
3100 "/*--TRACE_COMMIT--*/ %.*s",
3101 bcnt,buf);
3102 }
3103
3104 if (!commit_flag) {
3105 /* Nothing has changed */
3106 return 0;
3107 }
3108
Mike Isely681c7392007-11-26 01:48:52 -03003109 hdw->state_pipeline_config = 0;
3110 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3111 pvr2_hdw_state_sched(hdw);
3112
3113 return !0;
3114}
3115
3116
3117/* Perform all operations needed to commit all control changes. This must
3118 be performed in synchronization with the pipeline state and is thus
3119 expected to be called as part of the driver's worker thread. Return
3120 true if commit successful, otherwise return false to indicate that
3121 commit isn't possible at this time. */
3122static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3123{
3124 unsigned int idx;
3125 struct pvr2_ctrl *cptr;
3126 int disruptive_change;
3127
Mike Iselyab062fe2008-06-30 03:32:35 -03003128 /* Handle some required side effects when the video standard is
3129 changed.... */
Mike Iselyd8554972006-06-26 20:58:46 -03003130 if (hdw->std_dirty) {
Mike Iselyd8554972006-06-26 20:58:46 -03003131 int nvres;
Mike Isely00528d92008-06-30 03:35:52 -03003132 int gop_size;
Mike Iselyd8554972006-06-26 20:58:46 -03003133 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3134 nvres = 480;
Mike Isely00528d92008-06-30 03:35:52 -03003135 gop_size = 15;
Mike Iselyd8554972006-06-26 20:58:46 -03003136 } else {
3137 nvres = 576;
Mike Isely00528d92008-06-30 03:35:52 -03003138 gop_size = 12;
Mike Iselyd8554972006-06-26 20:58:46 -03003139 }
Mike Isely00528d92008-06-30 03:35:52 -03003140 /* Rewrite the vertical resolution to be appropriate to the
3141 video standard that has been selected. */
Mike Iselyd8554972006-06-26 20:58:46 -03003142 if (nvres != hdw->res_ver_val) {
3143 hdw->res_ver_val = nvres;
3144 hdw->res_ver_dirty = !0;
3145 }
Mike Isely00528d92008-06-30 03:35:52 -03003146 /* Rewrite the GOP size to be appropriate to the video
3147 standard that has been selected. */
3148 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3149 struct v4l2_ext_controls cs;
3150 struct v4l2_ext_control c1;
3151 memset(&cs, 0, sizeof(cs));
3152 memset(&c1, 0, sizeof(c1));
3153 cs.controls = &c1;
3154 cs.count = 1;
3155 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3156 c1.value = gop_size;
3157 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3158 VIDIOC_S_EXT_CTRLS);
3159 }
Mike Iselyd8554972006-06-26 20:58:46 -03003160 }
3161
Mike Isely38d9a2c2008-03-28 05:30:48 -03003162 if (hdw->input_dirty && hdw->state_pathway_ok &&
Mike Isely62433e32008-04-22 14:45:40 -03003163 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3164 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3165 hdw->pathway_state)) {
3166 /* Change of mode being asked for... */
3167 hdw->state_pathway_ok = 0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03003168 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03003169 }
3170 if (!hdw->state_pathway_ok) {
3171 /* Can't commit anything until pathway is ok. */
3172 return 0;
3173 }
vdb128@picaros.orge784bfb2008-08-30 18:26:39 -03003174 /* The broadcast decoder can only scale down, so if
3175 * res_*_dirty && crop window < output format ==> enlarge crop.
3176 *
3177 * The mpeg encoder receives fields of res_hor_val dots and
3178 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3179 */
3180 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3181 hdw->cropw_val = hdw->res_hor_val;
3182 hdw->cropw_dirty = !0;
3183 } else if (hdw->cropw_dirty) {
3184 hdw->res_hor_dirty = !0; /* must rescale */
3185 hdw->res_hor_val = min(720, hdw->cropw_val);
3186 }
3187 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3188 hdw->croph_val = hdw->res_ver_val;
3189 hdw->croph_dirty = !0;
3190 } else if (hdw->croph_dirty) {
3191 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3192 hdw->res_ver_dirty = !0;
3193 hdw->res_ver_val = min(nvres, hdw->croph_val);
3194 }
3195
Mike Isely681c7392007-11-26 01:48:52 -03003196 /* If any of the below has changed, then we can't do the update
3197 while the pipeline is running. Pipeline must be paused first
3198 and decoder -> encoder connection be made quiescent before we
3199 can proceed. */
3200 disruptive_change =
3201 (hdw->std_dirty ||
3202 hdw->enc_unsafe_stale ||
3203 hdw->srate_dirty ||
3204 hdw->res_ver_dirty ||
3205 hdw->res_hor_dirty ||
Mike Isely755879c2008-08-31 20:50:59 -03003206 hdw->cropw_dirty ||
3207 hdw->croph_dirty ||
Mike Isely681c7392007-11-26 01:48:52 -03003208 hdw->input_dirty ||
3209 (hdw->active_stream_type != hdw->desired_stream_type));
3210 if (disruptive_change && !hdw->state_pipeline_idle) {
3211 /* Pipeline is not idle; we can't proceed. Arrange to
3212 cause pipeline to stop so that we can try this again
3213 later.... */
3214 hdw->state_pipeline_pause = !0;
3215 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003216 }
3217
Mike Iselyb30d2442006-06-25 20:05:01 -03003218 if (hdw->srate_dirty) {
3219 /* Write new sample rate into control structure since
3220 * the master copy is stale. We must track srate
3221 * separate from the mpeg control structure because
3222 * other logic also uses this value. */
3223 struct v4l2_ext_controls cs;
3224 struct v4l2_ext_control c1;
3225 memset(&cs,0,sizeof(cs));
3226 memset(&c1,0,sizeof(c1));
3227 cs.controls = &c1;
3228 cs.count = 1;
3229 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3230 c1.value = hdw->srate_val;
Hans Verkuil01f1e442007-08-21 18:32:42 -03003231 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
Mike Iselyb30d2442006-06-25 20:05:01 -03003232 }
Mike Iselyc05c0462006-06-25 20:04:25 -03003233
Mike Iselyd8554972006-06-26 20:58:46 -03003234 /* Scan i2c core at this point - before we clear all the dirty
3235 bits. Various parts of the i2c core will notice dirty bits as
3236 appropriate and arrange to broadcast or directly send updates to
3237 the client drivers in order to keep everything in sync */
3238 pvr2_i2c_core_check_stale(hdw);
3239
Mike Isely681c7392007-11-26 01:48:52 -03003240 if (hdw->active_stream_type != hdw->desired_stream_type) {
3241 /* Handle any side effects of stream config here */
3242 hdw->active_stream_type = hdw->desired_stream_type;
3243 }
3244
Mike Isely1df59f02008-04-21 03:50:39 -03003245 if (hdw->hdw_desc->signal_routing_scheme ==
3246 PVR2_ROUTING_SCHEME_GOTVIEW) {
3247 u32 b;
3248 /* Handle GOTVIEW audio switching */
3249 pvr2_hdw_gpio_get_out(hdw,&b);
3250 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3251 /* Set GPIO 11 */
3252 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3253 } else {
3254 /* Clear GPIO 11 */
3255 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3256 }
3257 }
3258
Mike Iselye68a6192009-03-07 01:45:10 -03003259 /* Check and update state for all sub-devices. */
3260 pvr2_subdev_update(hdw);
3261
Mike Isely75212a02009-03-07 01:48:42 -03003262 hdw->tuner_updated = 0;
Mike Isely5ceaad12009-03-07 00:01:20 -03003263 for (idx = 0; idx < hdw->control_cnt; idx++) {
3264 cptr = hdw->controls + idx;
3265 if (!cptr->info->clear_dirty) continue;
3266 cptr->info->clear_dirty(cptr);
3267 }
3268
Mike Iselyd8554972006-06-26 20:58:46 -03003269 /* Now execute i2c core update */
3270 pvr2_i2c_core_sync(hdw);
3271
Mike Isely62433e32008-04-22 14:45:40 -03003272 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3273 hdw->state_encoder_run) {
3274 /* If encoder isn't running or it can't be touched, then
3275 this will get worked out later when we start the
3276 encoder. */
Mike Isely681c7392007-11-26 01:48:52 -03003277 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3278 }
Mike Iselyd8554972006-06-26 20:58:46 -03003279
Mike Isely681c7392007-11-26 01:48:52 -03003280 hdw->state_pipeline_config = !0;
Mike Isely432907f2008-08-31 21:02:20 -03003281 /* Hardware state may have changed in a way to cause the cropping
3282 capabilities to have changed. So mark it stale, which will
3283 cause a later re-fetch. */
Mike Isely681c7392007-11-26 01:48:52 -03003284 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3285 return !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003286}
3287
3288
3289int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3290{
Mike Isely681c7392007-11-26 01:48:52 -03003291 int fl;
3292 LOCK_TAKE(hdw->big_lock);
3293 fl = pvr2_hdw_commit_setup(hdw);
3294 LOCK_GIVE(hdw->big_lock);
3295 if (!fl) return 0;
3296 return pvr2_hdw_wait(hdw,0);
Mike Iselyd8554972006-06-26 20:58:46 -03003297}
3298
3299
Mike Isely681c7392007-11-26 01:48:52 -03003300static void pvr2_hdw_worker_i2c(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003301{
Mike Isely681c7392007-11-26 01:48:52 -03003302 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,worki2csync);
Mike Iselyd8554972006-06-26 20:58:46 -03003303 LOCK_TAKE(hdw->big_lock); do {
3304 pvr2_i2c_core_sync(hdw);
3305 } while (0); LOCK_GIVE(hdw->big_lock);
3306}
3307
3308
Mike Isely681c7392007-11-26 01:48:52 -03003309static void pvr2_hdw_worker_poll(struct work_struct *work)
Mike Iselyd8554972006-06-26 20:58:46 -03003310{
Mike Isely681c7392007-11-26 01:48:52 -03003311 int fl = 0;
3312 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
Mike Iselyd8554972006-06-26 20:58:46 -03003313 LOCK_TAKE(hdw->big_lock); do {
Mike Isely681c7392007-11-26 01:48:52 -03003314 fl = pvr2_hdw_state_eval(hdw);
3315 } while (0); LOCK_GIVE(hdw->big_lock);
3316 if (fl && hdw->state_func) {
3317 hdw->state_func(hdw->state_data);
3318 }
3319}
3320
3321
Mike Isely681c7392007-11-26 01:48:52 -03003322static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
Mike Iselyd8554972006-06-26 20:58:46 -03003323{
Mike Isely681c7392007-11-26 01:48:52 -03003324 return wait_event_interruptible(
3325 hdw->state_wait_data,
3326 (hdw->state_stale == 0) &&
3327 (!state || (hdw->master_state != state)));
Mike Iselyd8554972006-06-26 20:58:46 -03003328}
3329
Mike Isely681c7392007-11-26 01:48:52 -03003330
Mike Iselyd8554972006-06-26 20:58:46 -03003331/* Return name for this driver instance */
3332const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3333{
3334 return hdw->name;
3335}
3336
3337
Mike Isely78a47102007-11-26 01:58:20 -03003338const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3339{
3340 return hdw->hdw_desc->description;
3341}
3342
3343
3344const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3345{
3346 return hdw->hdw_desc->shortname;
3347}
3348
3349
Mike Iselyd8554972006-06-26 20:58:46 -03003350int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3351{
3352 int result;
3353 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03003354 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
Mike Iselyd8554972006-06-26 20:58:46 -03003355 result = pvr2_send_request(hdw,
3356 hdw->cmd_buffer,1,
3357 hdw->cmd_buffer,1);
3358 if (result < 0) break;
3359 result = (hdw->cmd_buffer[0] != 0);
3360 } while(0); LOCK_GIVE(hdw->ctl_lock);
3361 return result;
3362}
3363
3364
Mike Isely18103c572007-01-20 00:09:47 -03003365/* Execute poll of tuner status */
3366void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03003367{
Mike Iselyd8554972006-06-26 20:58:46 -03003368 LOCK_TAKE(hdw->big_lock); do {
Mike Iselya51f5002009-03-06 23:30:37 -03003369 pvr2_hdw_status_poll(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003370 } while (0); LOCK_GIVE(hdw->big_lock);
Mike Isely18103c572007-01-20 00:09:47 -03003371}
3372
3373
Mike Isely432907f2008-08-31 21:02:20 -03003374static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3375{
3376 if (!hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003377 return 0;
3378 }
Mike Iselya51f5002009-03-06 23:30:37 -03003379 pvr2_hdw_status_poll(hdw);
Mike Isely432907f2008-08-31 21:02:20 -03003380 if (hdw->cropcap_stale) {
Mike Isely432907f2008-08-31 21:02:20 -03003381 return -EIO;
3382 }
3383 return 0;
3384}
3385
3386
3387/* Return information about cropping capabilities */
3388int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3389{
3390 int stat = 0;
3391 LOCK_TAKE(hdw->big_lock);
3392 stat = pvr2_hdw_check_cropcap(hdw);
3393 if (!stat) {
Mike Isely432907f2008-08-31 21:02:20 -03003394 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3395 }
3396 LOCK_GIVE(hdw->big_lock);
3397 return stat;
3398}
3399
3400
Mike Isely18103c572007-01-20 00:09:47 -03003401/* Return information about the tuner */
3402int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3403{
3404 LOCK_TAKE(hdw->big_lock); do {
3405 if (hdw->tuner_signal_stale) {
Mike Iselya51f5002009-03-06 23:30:37 -03003406 pvr2_hdw_status_poll(hdw);
Mike Isely18103c572007-01-20 00:09:47 -03003407 }
3408 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3409 } while (0); LOCK_GIVE(hdw->big_lock);
3410 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -03003411}
3412
3413
3414/* Get handle to video output stream */
3415struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3416{
3417 return hp->vid_stream;
3418}
3419
3420
3421void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3422{
Mike Isely4f1a3e52006-06-25 20:04:31 -03003423 int nr = pvr2_hdw_get_unit_number(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003424 LOCK_TAKE(hdw->big_lock); do {
Mike Isely4f1a3e52006-06-25 20:04:31 -03003425 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
Mike Isely5ceaad12009-03-07 00:01:20 -03003426 hdw->log_requested = !0;
Mike Iselyd8554972006-06-26 20:58:46 -03003427 pvr2_i2c_core_check_stale(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003428 pvr2_i2c_core_sync(hdw);
Mike Iselya51f5002009-03-06 23:30:37 -03003429 hdw->log_requested = 0;
Mike Iselyed3261a2009-03-07 00:02:33 -03003430 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
Mike Iselyb30d2442006-06-25 20:05:01 -03003431 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
Hans Verkuil99eb44f2006-06-26 18:24:05 -03003432 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
Mike Isely681c7392007-11-26 01:48:52 -03003433 pvr2_hdw_state_log_state(hdw);
Mike Isely4f1a3e52006-06-25 20:04:31 -03003434 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
Mike Iselyd8554972006-06-26 20:58:46 -03003435 } while (0); LOCK_GIVE(hdw->big_lock);
3436}
3437
Mike Isely4db666c2007-09-08 22:16:27 -03003438
3439/* Grab EEPROM contents, needed for direct method. */
3440#define EEPROM_SIZE 8192
3441#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3442static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3443{
3444 struct i2c_msg msg[2];
3445 u8 *eeprom;
3446 u8 iadd[2];
3447 u8 addr;
3448 u16 eepromSize;
3449 unsigned int offs;
3450 int ret;
3451 int mode16 = 0;
3452 unsigned pcnt,tcnt;
3453 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3454 if (!eeprom) {
3455 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3456 "Failed to allocate memory"
3457 " required to read eeprom");
3458 return NULL;
3459 }
3460
3461 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3462 hdw->eeprom_addr);
3463 addr = hdw->eeprom_addr;
3464 /* Seems that if the high bit is set, then the *real* eeprom
3465 address is shifted right now bit position (noticed this in
3466 newer PVR USB2 hardware) */
3467 if (addr & 0x80) addr >>= 1;
3468
3469 /* FX2 documentation states that a 16bit-addressed eeprom is
3470 expected if the I2C address is an odd number (yeah, this is
3471 strange but it's what they do) */
3472 mode16 = (addr & 1);
3473 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3474 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3475 " using %d bit addressing",eepromSize,addr,
3476 mode16 ? 16 : 8);
3477
3478 msg[0].addr = addr;
3479 msg[0].flags = 0;
3480 msg[0].len = mode16 ? 2 : 1;
3481 msg[0].buf = iadd;
3482 msg[1].addr = addr;
3483 msg[1].flags = I2C_M_RD;
3484
3485 /* We have to do the actual eeprom data fetch ourselves, because
3486 (1) we're only fetching part of the eeprom, and (2) if we were
3487 getting the whole thing our I2C driver can't grab it in one
3488 pass - which is what tveeprom is otherwise going to attempt */
3489 memset(eeprom,0,EEPROM_SIZE);
3490 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3491 pcnt = 16;
3492 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3493 offs = tcnt + (eepromSize - EEPROM_SIZE);
3494 if (mode16) {
3495 iadd[0] = offs >> 8;
3496 iadd[1] = offs;
3497 } else {
3498 iadd[0] = offs;
3499 }
3500 msg[1].len = pcnt;
3501 msg[1].buf = eeprom+tcnt;
3502 if ((ret = i2c_transfer(&hdw->i2c_adap,
3503 msg,ARRAY_SIZE(msg))) != 2) {
3504 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3505 "eeprom fetch set offs err=%d",ret);
3506 kfree(eeprom);
3507 return NULL;
3508 }
3509 }
3510 return eeprom;
3511}
3512
3513
3514void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3515 int prom_flag,
3516 int enable_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003517{
3518 int ret;
3519 u16 address;
3520 unsigned int pipe;
3521 LOCK_TAKE(hdw->big_lock); do {
Al Viro5fa12472008-03-29 03:07:38 +00003522 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
Mike Iselyd8554972006-06-26 20:58:46 -03003523
3524 if (!enable_flag) {
3525 pvr2_trace(PVR2_TRACE_FIRMWARE,
3526 "Cleaning up after CPU firmware fetch");
3527 kfree(hdw->fw_buffer);
Mike Iselya0fd1cb2006-06-30 11:35:28 -03003528 hdw->fw_buffer = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003529 hdw->fw_size = 0;
Mike Isely4db666c2007-09-08 22:16:27 -03003530 if (hdw->fw_cpu_flag) {
3531 /* Now release the CPU. It will disconnect
3532 and reconnect later. */
3533 pvr2_hdw_cpureset_assert(hdw,0);
3534 }
Mike Iselyd8554972006-06-26 20:58:46 -03003535 break;
3536 }
3537
Mike Isely4db666c2007-09-08 22:16:27 -03003538 hdw->fw_cpu_flag = (prom_flag == 0);
3539 if (hdw->fw_cpu_flag) {
3540 pvr2_trace(PVR2_TRACE_FIRMWARE,
3541 "Preparing to suck out CPU firmware");
3542 hdw->fw_size = 0x2000;
3543 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3544 if (!hdw->fw_buffer) {
3545 hdw->fw_size = 0;
3546 break;
3547 }
3548
3549 /* We have to hold the CPU during firmware upload. */
3550 pvr2_hdw_cpureset_assert(hdw,1);
3551
3552 /* download the firmware from address 0000-1fff in 2048
3553 (=0x800) bytes chunk. */
3554
3555 pvr2_trace(PVR2_TRACE_FIRMWARE,
3556 "Grabbing CPU firmware");
3557 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3558 for(address = 0; address < hdw->fw_size;
3559 address += 0x800) {
3560 ret = usb_control_msg(hdw->usb_dev,pipe,
3561 0xa0,0xc0,
3562 address,0,
3563 hdw->fw_buffer+address,
3564 0x800,HZ);
3565 if (ret < 0) break;
3566 }
3567
3568 pvr2_trace(PVR2_TRACE_FIRMWARE,
3569 "Done grabbing CPU firmware");
3570 } else {
3571 pvr2_trace(PVR2_TRACE_FIRMWARE,
3572 "Sucking down EEPROM contents");
3573 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3574 if (!hdw->fw_buffer) {
3575 pvr2_trace(PVR2_TRACE_FIRMWARE,
3576 "EEPROM content suck failed.");
3577 break;
3578 }
3579 hdw->fw_size = EEPROM_SIZE;
3580 pvr2_trace(PVR2_TRACE_FIRMWARE,
3581 "Done sucking down EEPROM contents");
Mike Iselyd8554972006-06-26 20:58:46 -03003582 }
3583
Mike Iselyd8554972006-06-26 20:58:46 -03003584 } while (0); LOCK_GIVE(hdw->big_lock);
3585}
3586
3587
3588/* Return true if we're in a mode for retrieval CPU firmware */
3589int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3590{
Al Viro5fa12472008-03-29 03:07:38 +00003591 return hdw->fw_buffer != NULL;
Mike Iselyd8554972006-06-26 20:58:46 -03003592}
3593
3594
3595int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3596 char *buf,unsigned int cnt)
3597{
3598 int ret = -EINVAL;
3599 LOCK_TAKE(hdw->big_lock); do {
3600 if (!buf) break;
3601 if (!cnt) break;
3602
3603 if (!hdw->fw_buffer) {
3604 ret = -EIO;
3605 break;
3606 }
3607
3608 if (offs >= hdw->fw_size) {
3609 pvr2_trace(PVR2_TRACE_FIRMWARE,
3610 "Read firmware data offs=%d EOF",
3611 offs);
3612 ret = 0;
3613 break;
3614 }
3615
3616 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3617
3618 memcpy(buf,hdw->fw_buffer+offs,cnt);
3619
3620 pvr2_trace(PVR2_TRACE_FIRMWARE,
3621 "Read firmware data offs=%d cnt=%d",
3622 offs,cnt);
3623 ret = cnt;
3624 } while (0); LOCK_GIVE(hdw->big_lock);
3625
3626 return ret;
3627}
3628
3629
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003630int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003631 enum pvr2_v4l_type index)
Mike Iselyd8554972006-06-26 20:58:46 -03003632{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003633 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003634 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3635 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3636 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003637 default: return -1;
3638 }
Mike Iselyd8554972006-06-26 20:58:46 -03003639}
3640
3641
Pantelis Koukousoulas2fdf3d92006-12-27 23:07:58 -03003642/* Store a v4l minor device number */
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003643void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
Mike Isely80793842006-12-27 23:12:28 -03003644 enum pvr2_v4l_type index,int v)
Mike Iselyd8554972006-06-26 20:58:46 -03003645{
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003646 switch (index) {
Mike Isely80793842006-12-27 23:12:28 -03003647 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3648 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3649 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
Mike Iselyfd5a75f2006-12-27 23:11:22 -03003650 default: break;
3651 }
Mike Iselyd8554972006-06-26 20:58:46 -03003652}
3653
3654
David Howells7d12e782006-10-05 14:55:46 +01003655static void pvr2_ctl_write_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003656{
3657 struct pvr2_hdw *hdw = urb->context;
3658 hdw->ctl_write_pend_flag = 0;
3659 if (hdw->ctl_read_pend_flag) return;
3660 complete(&hdw->ctl_done);
3661}
3662
3663
David Howells7d12e782006-10-05 14:55:46 +01003664static void pvr2_ctl_read_complete(struct urb *urb)
Mike Iselyd8554972006-06-26 20:58:46 -03003665{
3666 struct pvr2_hdw *hdw = urb->context;
3667 hdw->ctl_read_pend_flag = 0;
3668 if (hdw->ctl_write_pend_flag) return;
3669 complete(&hdw->ctl_done);
3670}
3671
3672
3673static void pvr2_ctl_timeout(unsigned long data)
3674{
3675 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3676 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3677 hdw->ctl_timeout_flag = !0;
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003678 if (hdw->ctl_write_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003679 usb_unlink_urb(hdw->ctl_write_urb);
Mariusz Kozlowski5e55d2c2006-11-08 15:34:31 +01003680 if (hdw->ctl_read_pend_flag)
Mike Iselyd8554972006-06-26 20:58:46 -03003681 usb_unlink_urb(hdw->ctl_read_urb);
Mike Iselyd8554972006-06-26 20:58:46 -03003682 }
3683}
3684
3685
Mike Iselye61b6fc2006-07-18 22:42:18 -03003686/* Issue a command and get a response from the device. This extended
3687 version includes a probe flag (which if set means that device errors
3688 should not be logged or treated as fatal) and a timeout in jiffies.
3689 This can be used to non-lethally probe the health of endpoint 1. */
Adrian Bunk07e337e2006-06-30 11:30:20 -03003690static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3691 unsigned int timeout,int probe_fl,
3692 void *write_data,unsigned int write_len,
3693 void *read_data,unsigned int read_len)
Mike Iselyd8554972006-06-26 20:58:46 -03003694{
3695 unsigned int idx;
3696 int status = 0;
3697 struct timer_list timer;
3698 if (!hdw->ctl_lock_held) {
3699 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3700 "Attempted to execute control transfer"
3701 " without lock!!");
3702 return -EDEADLK;
3703 }
Mike Isely681c7392007-11-26 01:48:52 -03003704 if (!hdw->flag_ok && !probe_fl) {
Mike Iselyd8554972006-06-26 20:58:46 -03003705 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3706 "Attempted to execute control transfer"
3707 " when device not ok");
3708 return -EIO;
3709 }
3710 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3711 if (!probe_fl) {
3712 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3713 "Attempted to execute control transfer"
3714 " when USB is disconnected");
3715 }
3716 return -ENOTTY;
3717 }
3718
3719 /* Ensure that we have sane parameters */
3720 if (!write_data) write_len = 0;
3721 if (!read_data) read_len = 0;
3722 if (write_len > PVR2_CTL_BUFFSIZE) {
3723 pvr2_trace(
3724 PVR2_TRACE_ERROR_LEGS,
3725 "Attempted to execute %d byte"
3726 " control-write transfer (limit=%d)",
3727 write_len,PVR2_CTL_BUFFSIZE);
3728 return -EINVAL;
3729 }
3730 if (read_len > PVR2_CTL_BUFFSIZE) {
3731 pvr2_trace(
3732 PVR2_TRACE_ERROR_LEGS,
3733 "Attempted to execute %d byte"
3734 " control-read transfer (limit=%d)",
3735 write_len,PVR2_CTL_BUFFSIZE);
3736 return -EINVAL;
3737 }
3738 if ((!write_len) && (!read_len)) {
3739 pvr2_trace(
3740 PVR2_TRACE_ERROR_LEGS,
3741 "Attempted to execute null control transfer?");
3742 return -EINVAL;
3743 }
3744
3745
3746 hdw->cmd_debug_state = 1;
3747 if (write_len) {
3748 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3749 } else {
3750 hdw->cmd_debug_code = 0;
3751 }
3752 hdw->cmd_debug_write_len = write_len;
3753 hdw->cmd_debug_read_len = read_len;
3754
3755 /* Initialize common stuff */
3756 init_completion(&hdw->ctl_done);
3757 hdw->ctl_timeout_flag = 0;
3758 hdw->ctl_write_pend_flag = 0;
3759 hdw->ctl_read_pend_flag = 0;
3760 init_timer(&timer);
3761 timer.expires = jiffies + timeout;
3762 timer.data = (unsigned long)hdw;
3763 timer.function = pvr2_ctl_timeout;
3764
3765 if (write_len) {
3766 hdw->cmd_debug_state = 2;
3767 /* Transfer write data to internal buffer */
3768 for (idx = 0; idx < write_len; idx++) {
3769 hdw->ctl_write_buffer[idx] =
3770 ((unsigned char *)write_data)[idx];
3771 }
3772 /* Initiate a write request */
3773 usb_fill_bulk_urb(hdw->ctl_write_urb,
3774 hdw->usb_dev,
3775 usb_sndbulkpipe(hdw->usb_dev,
3776 PVR2_CTL_WRITE_ENDPOINT),
3777 hdw->ctl_write_buffer,
3778 write_len,
3779 pvr2_ctl_write_complete,
3780 hdw);
3781 hdw->ctl_write_urb->actual_length = 0;
3782 hdw->ctl_write_pend_flag = !0;
3783 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3784 if (status < 0) {
3785 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3786 "Failed to submit write-control"
3787 " URB status=%d",status);
3788 hdw->ctl_write_pend_flag = 0;
3789 goto done;
3790 }
3791 }
3792
3793 if (read_len) {
3794 hdw->cmd_debug_state = 3;
3795 memset(hdw->ctl_read_buffer,0x43,read_len);
3796 /* Initiate a read request */
3797 usb_fill_bulk_urb(hdw->ctl_read_urb,
3798 hdw->usb_dev,
3799 usb_rcvbulkpipe(hdw->usb_dev,
3800 PVR2_CTL_READ_ENDPOINT),
3801 hdw->ctl_read_buffer,
3802 read_len,
3803 pvr2_ctl_read_complete,
3804 hdw);
3805 hdw->ctl_read_urb->actual_length = 0;
3806 hdw->ctl_read_pend_flag = !0;
3807 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3808 if (status < 0) {
3809 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3810 "Failed to submit read-control"
3811 " URB status=%d",status);
3812 hdw->ctl_read_pend_flag = 0;
3813 goto done;
3814 }
3815 }
3816
3817 /* Start timer */
3818 add_timer(&timer);
3819
3820 /* Now wait for all I/O to complete */
3821 hdw->cmd_debug_state = 4;
3822 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3823 wait_for_completion(&hdw->ctl_done);
3824 }
3825 hdw->cmd_debug_state = 5;
3826
3827 /* Stop timer */
3828 del_timer_sync(&timer);
3829
3830 hdw->cmd_debug_state = 6;
3831 status = 0;
3832
3833 if (hdw->ctl_timeout_flag) {
3834 status = -ETIMEDOUT;
3835 if (!probe_fl) {
3836 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3837 "Timed out control-write");
3838 }
3839 goto done;
3840 }
3841
3842 if (write_len) {
3843 /* Validate results of write request */
3844 if ((hdw->ctl_write_urb->status != 0) &&
3845 (hdw->ctl_write_urb->status != -ENOENT) &&
3846 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3847 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3848 /* USB subsystem is reporting some kind of failure
3849 on the write */
3850 status = hdw->ctl_write_urb->status;
3851 if (!probe_fl) {
3852 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3853 "control-write URB failure,"
3854 " status=%d",
3855 status);
3856 }
3857 goto done;
3858 }
3859 if (hdw->ctl_write_urb->actual_length < write_len) {
3860 /* Failed to write enough data */
3861 status = -EIO;
3862 if (!probe_fl) {
3863 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3864 "control-write URB short,"
3865 " expected=%d got=%d",
3866 write_len,
3867 hdw->ctl_write_urb->actual_length);
3868 }
3869 goto done;
3870 }
3871 }
3872 if (read_len) {
3873 /* Validate results of read request */
3874 if ((hdw->ctl_read_urb->status != 0) &&
3875 (hdw->ctl_read_urb->status != -ENOENT) &&
3876 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3877 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3878 /* USB subsystem is reporting some kind of failure
3879 on the read */
3880 status = hdw->ctl_read_urb->status;
3881 if (!probe_fl) {
3882 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3883 "control-read URB failure,"
3884 " status=%d",
3885 status);
3886 }
3887 goto done;
3888 }
3889 if (hdw->ctl_read_urb->actual_length < read_len) {
3890 /* Failed to read enough data */
3891 status = -EIO;
3892 if (!probe_fl) {
3893 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3894 "control-read URB short,"
3895 " expected=%d got=%d",
3896 read_len,
3897 hdw->ctl_read_urb->actual_length);
3898 }
3899 goto done;
3900 }
3901 /* Transfer retrieved data out from internal buffer */
3902 for (idx = 0; idx < read_len; idx++) {
3903 ((unsigned char *)read_data)[idx] =
3904 hdw->ctl_read_buffer[idx];
3905 }
3906 }
3907
3908 done:
3909
3910 hdw->cmd_debug_state = 0;
3911 if ((status < 0) && (!probe_fl)) {
Mike Isely681c7392007-11-26 01:48:52 -03003912 pvr2_hdw_render_useless(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03003913 }
3914 return status;
3915}
3916
3917
3918int pvr2_send_request(struct pvr2_hdw *hdw,
3919 void *write_data,unsigned int write_len,
3920 void *read_data,unsigned int read_len)
3921{
3922 return pvr2_send_request_ex(hdw,HZ*4,0,
3923 write_data,write_len,
3924 read_data,read_len);
3925}
3926
Mike Isely1c9d10d2008-03-28 05:38:54 -03003927
3928static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3929{
3930 int ret;
3931 unsigned int cnt = 1;
3932 unsigned int args = 0;
3933 LOCK_TAKE(hdw->ctl_lock);
3934 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3935 args = (cmdcode >> 8) & 0xffu;
3936 args = (args > 2) ? 2 : args;
3937 if (args) {
3938 cnt += args;
3939 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3940 if (args > 1) {
3941 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3942 }
3943 }
3944 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3945 unsigned int idx;
3946 unsigned int ccnt,bcnt;
3947 char tbuf[50];
3948 cmdcode &= 0xffu;
3949 bcnt = 0;
3950 ccnt = scnprintf(tbuf+bcnt,
3951 sizeof(tbuf)-bcnt,
3952 "Sending FX2 command 0x%x",cmdcode);
3953 bcnt += ccnt;
3954 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3955 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3956 ccnt = scnprintf(tbuf+bcnt,
3957 sizeof(tbuf)-bcnt,
3958 " \"%s\"",
3959 pvr2_fx2cmd_desc[idx].desc);
3960 bcnt += ccnt;
3961 break;
3962 }
3963 }
3964 if (args) {
3965 ccnt = scnprintf(tbuf+bcnt,
3966 sizeof(tbuf)-bcnt,
3967 " (%u",hdw->cmd_buffer[1]);
3968 bcnt += ccnt;
3969 if (args > 1) {
3970 ccnt = scnprintf(tbuf+bcnt,
3971 sizeof(tbuf)-bcnt,
3972 ",%u",hdw->cmd_buffer[2]);
3973 bcnt += ccnt;
3974 }
3975 ccnt = scnprintf(tbuf+bcnt,
3976 sizeof(tbuf)-bcnt,
3977 ")");
3978 bcnt += ccnt;
3979 }
3980 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
3981 }
3982 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
3983 LOCK_GIVE(hdw->ctl_lock);
3984 return ret;
3985}
3986
3987
Mike Iselyd8554972006-06-26 20:58:46 -03003988int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
3989{
3990 int ret;
3991
3992 LOCK_TAKE(hdw->ctl_lock);
3993
Michael Krufky8d364362007-01-22 02:17:55 -03003994 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03003995 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
3996 hdw->cmd_buffer[5] = 0;
3997 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
3998 hdw->cmd_buffer[7] = reg & 0xff;
3999
4000
4001 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
4002
4003 LOCK_GIVE(hdw->ctl_lock);
4004
4005 return ret;
4006}
4007
4008
Adrian Bunk07e337e2006-06-30 11:30:20 -03004009static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
Mike Iselyd8554972006-06-26 20:58:46 -03004010{
4011 int ret = 0;
4012
4013 LOCK_TAKE(hdw->ctl_lock);
4014
Michael Krufky8d364362007-01-22 02:17:55 -03004015 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
Mike Iselyd8554972006-06-26 20:58:46 -03004016 hdw->cmd_buffer[1] = 0;
4017 hdw->cmd_buffer[2] = 0;
4018 hdw->cmd_buffer[3] = 0;
4019 hdw->cmd_buffer[4] = 0;
4020 hdw->cmd_buffer[5] = 0;
4021 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4022 hdw->cmd_buffer[7] = reg & 0xff;
4023
4024 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
4025 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
4026
4027 LOCK_GIVE(hdw->ctl_lock);
4028
4029 return ret;
4030}
4031
4032
Mike Isely681c7392007-11-26 01:48:52 -03004033void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03004034{
4035 if (!hdw->flag_ok) return;
Mike Isely681c7392007-11-26 01:48:52 -03004036 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4037 "Device being rendered inoperable");
Mike Iselyd8554972006-06-26 20:58:46 -03004038 if (hdw->vid_stream) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004039 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -03004040 }
Mike Isely681c7392007-11-26 01:48:52 -03004041 hdw->flag_ok = 0;
4042 trace_stbit("flag_ok",hdw->flag_ok);
4043 pvr2_hdw_state_sched(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -03004044}
4045
4046
4047void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
4048{
4049 int ret;
4050 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
Mike Iselya0fd1cb2006-06-30 11:35:28 -03004051 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
Alan Stern011b15d2008-11-04 11:29:27 -05004052 if (ret == 0) {
Mike Iselyd8554972006-06-26 20:58:46 -03004053 ret = usb_reset_device(hdw->usb_dev);
4054 usb_unlock_device(hdw->usb_dev);
4055 } else {
4056 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4057 "Failed to lock USB device ret=%d",ret);
4058 }
4059 if (init_pause_msec) {
4060 pvr2_trace(PVR2_TRACE_INFO,
4061 "Waiting %u msec for hardware to settle",
4062 init_pause_msec);
4063 msleep(init_pause_msec);
4064 }
4065
4066}
4067
4068
4069void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
4070{
4071 char da[1];
4072 unsigned int pipe;
4073 int ret;
4074
4075 if (!hdw->usb_dev) return;
4076
4077 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
4078
4079 da[0] = val ? 0x01 : 0x00;
4080
4081 /* Write the CPUCS register on the 8051. The lsb of the register
4082 is the reset bit; a 1 asserts reset while a 0 clears it. */
4083 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
4084 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
4085 if (ret < 0) {
4086 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4087 "cpureset_assert(%d) error=%d",val,ret);
4088 pvr2_hdw_render_useless(hdw);
4089 }
4090}
4091
4092
4093int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4094{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004095 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
Mike Iselyd8554972006-06-26 20:58:46 -03004096}
4097
4098
Michael Krufkye1edb192008-04-22 14:45:39 -03004099int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4100{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004101 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
Michael Krufkye1edb192008-04-22 14:45:39 -03004102}
4103
Mike Isely1c9d10d2008-03-28 05:38:54 -03004104
Michael Krufkye1edb192008-04-22 14:45:39 -03004105int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4106{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004107 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
Michael Krufkye1edb192008-04-22 14:45:39 -03004108}
4109
Mike Iselyd8554972006-06-26 20:58:46 -03004110
4111int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4112{
Mike Iselyd8554972006-06-26 20:58:46 -03004113 pvr2_trace(PVR2_TRACE_INIT,
4114 "Requesting decoder reset");
Mike Iselyaf78e162009-03-07 00:21:30 -03004115 if (hdw->decoder_ctrl) {
4116 if (!hdw->decoder_ctrl->force_reset) {
4117 pvr2_trace(PVR2_TRACE_INIT,
4118 "Unable to reset decoder: not implemented");
4119 return -ENOTTY;
4120 }
4121 hdw->decoder_ctrl->force_reset(hdw->decoder_ctrl->ctxt);
4122 return 0;
4123 } else {
4124 }
4125 if (hdw->decoder_client_id) {
4126 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4127 core, reset, 0);
4128 return 0;
4129 }
4130 pvr2_trace(PVR2_TRACE_INIT,
4131 "Unable to reset decoder: nothing attached");
4132 return -ENOTTY;
Mike Iselyd8554972006-06-26 20:58:46 -03004133}
4134
4135
Mike Isely62433e32008-04-22 14:45:40 -03004136static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004137{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004138 hdw->flag_ok = !0;
4139 return pvr2_issue_simple_cmd(hdw,
4140 FX2CMD_HCW_DEMOD_RESETIN |
4141 (1 << 8) |
4142 ((onoff ? 1 : 0) << 16));
Mike Isely84147f32008-04-22 14:45:40 -03004143}
4144
Mike Isely84147f32008-04-22 14:45:40 -03004145
Mike Isely62433e32008-04-22 14:45:40 -03004146static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004147{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004148 hdw->flag_ok = !0;
4149 return pvr2_issue_simple_cmd(hdw,(onoff ?
4150 FX2CMD_ONAIR_DTV_POWER_ON :
4151 FX2CMD_ONAIR_DTV_POWER_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004152}
4153
Mike Isely62433e32008-04-22 14:45:40 -03004154
4155static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4156 int onoff)
Mike Isely84147f32008-04-22 14:45:40 -03004157{
Mike Isely1c9d10d2008-03-28 05:38:54 -03004158 return pvr2_issue_simple_cmd(hdw,(onoff ?
4159 FX2CMD_ONAIR_DTV_STREAMING_ON :
4160 FX2CMD_ONAIR_DTV_STREAMING_OFF));
Mike Isely84147f32008-04-22 14:45:40 -03004161}
4162
Mike Isely62433e32008-04-22 14:45:40 -03004163
4164static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4165{
4166 int cmode;
4167 /* Compare digital/analog desired setting with current setting. If
4168 they don't match, fix it... */
4169 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4170 if (cmode == hdw->pathway_state) {
4171 /* They match; nothing to do */
4172 return;
4173 }
4174
4175 switch (hdw->hdw_desc->digital_control_scheme) {
4176 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4177 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4178 if (cmode == PVR2_PATHWAY_ANALOG) {
4179 /* If moving to analog mode, also force the decoder
4180 to reset. If no decoder is attached, then it's
4181 ok to ignore this because if/when the decoder
4182 attaches, it will reset itself at that time. */
4183 pvr2_hdw_cmd_decoder_reset(hdw);
4184 }
4185 break;
4186 case PVR2_DIGITAL_SCHEME_ONAIR:
4187 /* Supposedly we should always have the power on whether in
4188 digital or analog mode. But for now do what appears to
4189 work... */
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004190 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
Mike Isely62433e32008-04-22 14:45:40 -03004191 break;
4192 default: break;
4193 }
4194
Mike Isely1b9c18c2008-04-22 14:45:41 -03004195 pvr2_hdw_untrip_unlocked(hdw);
Mike Isely62433e32008-04-22 14:45:40 -03004196 hdw->pathway_state = cmode;
4197}
4198
4199
Adrian Bunke9b59f62008-05-10 04:35:24 -03004200static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
Mike Iselyc55a97d2008-04-22 14:45:41 -03004201{
4202 /* change some GPIO data
4203 *
4204 * note: bit d7 of dir appears to control the LED,
4205 * so we shut it off here.
4206 *
Mike Iselyc55a97d2008-04-22 14:45:41 -03004207 */
Mike Isely40381cb2008-04-22 14:45:42 -03004208 if (onoff) {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004209 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
Mike Isely40381cb2008-04-22 14:45:42 -03004210 } else {
Mike Iselyc55a97d2008-04-22 14:45:41 -03004211 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
Mike Isely40381cb2008-04-22 14:45:42 -03004212 }
Mike Iselyc55a97d2008-04-22 14:45:41 -03004213 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
Mike Isely40381cb2008-04-22 14:45:42 -03004214}
Mike Iselyc55a97d2008-04-22 14:45:41 -03004215
Mike Isely40381cb2008-04-22 14:45:42 -03004216
4217typedef void (*led_method_func)(struct pvr2_hdw *,int);
4218
4219static led_method_func led_methods[] = {
4220 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4221};
4222
4223
4224/* Toggle LED */
4225static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4226{
4227 unsigned int scheme_id;
4228 led_method_func fp;
4229
4230 if ((!onoff) == (!hdw->led_on)) return;
4231
4232 hdw->led_on = onoff != 0;
4233
4234 scheme_id = hdw->hdw_desc->led_scheme;
4235 if (scheme_id < ARRAY_SIZE(led_methods)) {
4236 fp = led_methods[scheme_id];
4237 } else {
4238 fp = NULL;
4239 }
4240
4241 if (fp) (*fp)(hdw,onoff);
Mike Iselyc55a97d2008-04-22 14:45:41 -03004242}
4243
4244
Mike Iselye61b6fc2006-07-18 22:42:18 -03004245/* Stop / start video stream transport */
Adrian Bunk07e337e2006-06-30 11:30:20 -03004246static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
Mike Iselyd8554972006-06-26 20:58:46 -03004247{
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004248 int ret;
4249
4250 /* If we're in analog mode, then just issue the usual analog
4251 command. */
4252 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4253 return pvr2_issue_simple_cmd(hdw,
4254 (runFl ?
4255 FX2CMD_STREAMING_ON :
4256 FX2CMD_STREAMING_OFF));
4257 /*Note: Not reached */
Mike Isely62433e32008-04-22 14:45:40 -03004258 }
Mike Iselybb0c2fe2008-03-28 05:41:19 -03004259
4260 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4261 /* Whoops, we don't know what mode we're in... */
4262 return -EINVAL;
4263 }
4264
4265 /* To get here we have to be in digital mode. The mechanism here
4266 is unfortunately different for different vendors. So we switch
4267 on the device's digital scheme attribute in order to figure out
4268 what to do. */
4269 switch (hdw->hdw_desc->digital_control_scheme) {
4270 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4271 return pvr2_issue_simple_cmd(hdw,
4272 (runFl ?
4273 FX2CMD_HCW_DTV_STREAMING_ON :
4274 FX2CMD_HCW_DTV_STREAMING_OFF));
4275 case PVR2_DIGITAL_SCHEME_ONAIR:
4276 ret = pvr2_issue_simple_cmd(hdw,
4277 (runFl ?
4278 FX2CMD_STREAMING_ON :
4279 FX2CMD_STREAMING_OFF));
4280 if (ret) return ret;
4281 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4282 default:
4283 return -EINVAL;
4284 }
Mike Iselyd8554972006-06-26 20:58:46 -03004285}
4286
4287
Mike Isely62433e32008-04-22 14:45:40 -03004288/* Evaluate whether or not state_pathway_ok can change */
4289static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4290{
4291 if (hdw->state_pathway_ok) {
4292 /* Nothing to do if pathway is already ok */
4293 return 0;
4294 }
4295 if (!hdw->state_pipeline_idle) {
4296 /* Not allowed to change anything if pipeline is not idle */
4297 return 0;
4298 }
4299 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4300 hdw->state_pathway_ok = !0;
Mike Iselye9db1ff2008-04-22 14:45:41 -03004301 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
Mike Isely62433e32008-04-22 14:45:40 -03004302 return !0;
4303}
4304
4305
Mike Isely681c7392007-11-26 01:48:52 -03004306/* Evaluate whether or not state_encoder_ok can change */
4307static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4308{
4309 if (hdw->state_encoder_ok) return 0;
4310 if (hdw->flag_tripped) return 0;
4311 if (hdw->state_encoder_run) return 0;
4312 if (hdw->state_encoder_config) return 0;
4313 if (hdw->state_decoder_run) return 0;
4314 if (hdw->state_usbstream_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004315 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4316 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4317 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4318 return 0;
4319 }
4320
Mike Isely681c7392007-11-26 01:48:52 -03004321 if (pvr2_upload_firmware2(hdw) < 0) {
4322 hdw->flag_tripped = !0;
4323 trace_stbit("flag_tripped",hdw->flag_tripped);
4324 return !0;
4325 }
4326 hdw->state_encoder_ok = !0;
4327 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4328 return !0;
4329}
4330
4331
4332/* Evaluate whether or not state_encoder_config can change */
4333static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4334{
4335 if (hdw->state_encoder_config) {
4336 if (hdw->state_encoder_ok) {
4337 if (hdw->state_pipeline_req &&
4338 !hdw->state_pipeline_pause) return 0;
4339 }
4340 hdw->state_encoder_config = 0;
4341 hdw->state_encoder_waitok = 0;
4342 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4343 /* paranoia - solve race if timer just completed */
4344 del_timer_sync(&hdw->encoder_wait_timer);
4345 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004346 if (!hdw->state_pathway_ok ||
4347 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4348 !hdw->state_encoder_ok ||
Mike Isely681c7392007-11-26 01:48:52 -03004349 !hdw->state_pipeline_idle ||
4350 hdw->state_pipeline_pause ||
4351 !hdw->state_pipeline_req ||
4352 !hdw->state_pipeline_config) {
4353 /* We must reset the enforced wait interval if
4354 anything has happened that might have disturbed
4355 the encoder. This should be a rare case. */
4356 if (timer_pending(&hdw->encoder_wait_timer)) {
4357 del_timer_sync(&hdw->encoder_wait_timer);
4358 }
4359 if (hdw->state_encoder_waitok) {
4360 /* Must clear the state - therefore we did
4361 something to a state bit and must also
4362 return true. */
4363 hdw->state_encoder_waitok = 0;
4364 trace_stbit("state_encoder_waitok",
4365 hdw->state_encoder_waitok);
4366 return !0;
4367 }
4368 return 0;
4369 }
4370 if (!hdw->state_encoder_waitok) {
4371 if (!timer_pending(&hdw->encoder_wait_timer)) {
4372 /* waitok flag wasn't set and timer isn't
4373 running. Check flag once more to avoid
4374 a race then start the timer. This is
4375 the point when we measure out a minimal
4376 quiet interval before doing something to
4377 the encoder. */
4378 if (!hdw->state_encoder_waitok) {
4379 hdw->encoder_wait_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004380 jiffies +
4381 (HZ * TIME_MSEC_ENCODER_WAIT
4382 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004383 add_timer(&hdw->encoder_wait_timer);
4384 }
4385 }
4386 /* We can't continue until we know we have been
4387 quiet for the interval measured by this
4388 timer. */
4389 return 0;
4390 }
4391 pvr2_encoder_configure(hdw);
4392 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4393 }
4394 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4395 return !0;
4396}
4397
4398
Mike Iselyd913d632008-04-06 04:04:35 -03004399/* Return true if the encoder should not be running. */
4400static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4401{
4402 if (!hdw->state_encoder_ok) {
4403 /* Encoder isn't healthy at the moment, so stop it. */
4404 return !0;
4405 }
4406 if (!hdw->state_pathway_ok) {
4407 /* Mode is not understood at the moment (i.e. it wants to
4408 change), so encoder must be stopped. */
4409 return !0;
4410 }
4411
4412 switch (hdw->pathway_state) {
4413 case PVR2_PATHWAY_ANALOG:
4414 if (!hdw->state_decoder_run) {
4415 /* We're in analog mode and the decoder is not
4416 running; thus the encoder should be stopped as
4417 well. */
4418 return !0;
4419 }
4420 break;
4421 case PVR2_PATHWAY_DIGITAL:
4422 if (hdw->state_encoder_runok) {
4423 /* This is a funny case. We're in digital mode so
4424 really the encoder should be stopped. However
4425 if it really is running, only kill it after
4426 runok has been set. This gives a chance for the
4427 onair quirk to function (encoder must run
4428 briefly first, at least once, before onair
4429 digital streaming can work). */
4430 return !0;
4431 }
4432 break;
4433 default:
4434 /* Unknown mode; so encoder should be stopped. */
4435 return !0;
4436 }
4437
4438 /* If we get here, we haven't found a reason to stop the
4439 encoder. */
4440 return 0;
4441}
4442
4443
4444/* Return true if the encoder should be running. */
4445static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4446{
4447 if (!hdw->state_encoder_ok) {
4448 /* Don't run the encoder if it isn't healthy... */
4449 return 0;
4450 }
4451 if (!hdw->state_pathway_ok) {
4452 /* Don't run the encoder if we don't (yet) know what mode
4453 we need to be in... */
4454 return 0;
4455 }
4456
4457 switch (hdw->pathway_state) {
4458 case PVR2_PATHWAY_ANALOG:
4459 if (hdw->state_decoder_run) {
4460 /* In analog mode, if the decoder is running, then
4461 run the encoder. */
4462 return !0;
4463 }
4464 break;
4465 case PVR2_PATHWAY_DIGITAL:
4466 if ((hdw->hdw_desc->digital_control_scheme ==
4467 PVR2_DIGITAL_SCHEME_ONAIR) &&
4468 !hdw->state_encoder_runok) {
4469 /* This is a quirk. OnAir hardware won't stream
4470 digital until the encoder has been run at least
4471 once, for a minimal period of time (empiricially
4472 measured to be 1/4 second). So if we're on
4473 OnAir hardware and the encoder has never been
4474 run at all, then start the encoder. Normal
4475 state machine logic in the driver will
4476 automatically handle the remaining bits. */
4477 return !0;
4478 }
4479 break;
4480 default:
4481 /* For completeness (unknown mode; encoder won't run ever) */
4482 break;
4483 }
4484 /* If we get here, then we haven't found any reason to run the
4485 encoder, so don't run it. */
4486 return 0;
4487}
4488
4489
Mike Isely681c7392007-11-26 01:48:52 -03004490/* Evaluate whether or not state_encoder_run can change */
4491static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4492{
4493 if (hdw->state_encoder_run) {
Mike Iselyd913d632008-04-06 04:04:35 -03004494 if (!state_check_disable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004495 if (hdw->state_encoder_ok) {
Mike Iselyd913d632008-04-06 04:04:35 -03004496 del_timer_sync(&hdw->encoder_run_timer);
Mike Isely681c7392007-11-26 01:48:52 -03004497 if (pvr2_encoder_stop(hdw) < 0) return !0;
4498 }
4499 hdw->state_encoder_run = 0;
4500 } else {
Mike Iselyd913d632008-04-06 04:04:35 -03004501 if (!state_check_enable_encoder_run(hdw)) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004502 if (pvr2_encoder_start(hdw) < 0) return !0;
4503 hdw->state_encoder_run = !0;
Mike Iselyd913d632008-04-06 04:04:35 -03004504 if (!hdw->state_encoder_runok) {
4505 hdw->encoder_run_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004506 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
Mike Iselyd913d632008-04-06 04:04:35 -03004507 add_timer(&hdw->encoder_run_timer);
4508 }
Mike Isely681c7392007-11-26 01:48:52 -03004509 }
4510 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4511 return !0;
4512}
4513
4514
4515/* Timeout function for quiescent timer. */
4516static void pvr2_hdw_quiescent_timeout(unsigned long data)
4517{
4518 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4519 hdw->state_decoder_quiescent = !0;
4520 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4521 hdw->state_stale = !0;
4522 queue_work(hdw->workqueue,&hdw->workpoll);
4523}
4524
4525
4526/* Timeout function for encoder wait timer. */
4527static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4528{
4529 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4530 hdw->state_encoder_waitok = !0;
4531 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4532 hdw->state_stale = !0;
4533 queue_work(hdw->workqueue,&hdw->workpoll);
4534}
4535
4536
Mike Iselyd913d632008-04-06 04:04:35 -03004537/* Timeout function for encoder run timer. */
4538static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4539{
4540 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4541 if (!hdw->state_encoder_runok) {
4542 hdw->state_encoder_runok = !0;
4543 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4544 hdw->state_stale = !0;
4545 queue_work(hdw->workqueue,&hdw->workpoll);
4546 }
4547}
4548
4549
Mike Isely681c7392007-11-26 01:48:52 -03004550/* Evaluate whether or not state_decoder_run can change */
4551static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4552{
4553 if (hdw->state_decoder_run) {
4554 if (hdw->state_encoder_ok) {
4555 if (hdw->state_pipeline_req &&
Mike Isely62433e32008-04-22 14:45:40 -03004556 !hdw->state_pipeline_pause &&
4557 hdw->state_pathway_ok) return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004558 }
4559 if (!hdw->flag_decoder_missed) {
4560 pvr2_decoder_enable(hdw,0);
4561 }
4562 hdw->state_decoder_quiescent = 0;
4563 hdw->state_decoder_run = 0;
4564 /* paranoia - solve race if timer just completed */
4565 del_timer_sync(&hdw->quiescent_timer);
4566 } else {
4567 if (!hdw->state_decoder_quiescent) {
4568 if (!timer_pending(&hdw->quiescent_timer)) {
4569 /* We don't do something about the
4570 quiescent timer until right here because
4571 we also want to catch cases where the
4572 decoder was already not running (like
4573 after initialization) as opposed to
4574 knowing that we had just stopped it.
4575 The second flag check is here to cover a
4576 race - the timer could have run and set
4577 this flag just after the previous check
4578 but before we did the pending check. */
4579 if (!hdw->state_decoder_quiescent) {
4580 hdw->quiescent_timer.expires =
Mike Isely83ce57a2008-05-26 05:51:57 -03004581 jiffies +
4582 (HZ * TIME_MSEC_DECODER_WAIT
4583 / 1000);
Mike Isely681c7392007-11-26 01:48:52 -03004584 add_timer(&hdw->quiescent_timer);
4585 }
4586 }
4587 /* Don't allow decoder to start again until it has
4588 been quiesced first. This little detail should
4589 hopefully further stabilize the encoder. */
4590 return 0;
4591 }
Mike Isely62433e32008-04-22 14:45:40 -03004592 if (!hdw->state_pathway_ok ||
4593 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4594 !hdw->state_pipeline_req ||
Mike Isely681c7392007-11-26 01:48:52 -03004595 hdw->state_pipeline_pause ||
4596 !hdw->state_pipeline_config ||
4597 !hdw->state_encoder_config ||
4598 !hdw->state_encoder_ok) return 0;
4599 del_timer_sync(&hdw->quiescent_timer);
4600 if (hdw->flag_decoder_missed) return 0;
4601 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4602 hdw->state_decoder_quiescent = 0;
4603 hdw->state_decoder_run = !0;
4604 }
4605 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4606 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4607 return !0;
4608}
4609
4610
4611/* Evaluate whether or not state_usbstream_run can change */
4612static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4613{
4614 if (hdw->state_usbstream_run) {
Mike Isely72998b72008-04-03 04:51:19 -03004615 int fl = !0;
Mike Isely62433e32008-04-22 14:45:40 -03004616 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
Mike Isely72998b72008-04-03 04:51:19 -03004617 fl = (hdw->state_encoder_ok &&
4618 hdw->state_encoder_run);
4619 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4620 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4621 fl = hdw->state_encoder_ok;
4622 }
4623 if (fl &&
4624 hdw->state_pipeline_req &&
4625 !hdw->state_pipeline_pause &&
4626 hdw->state_pathway_ok) {
4627 return 0;
Mike Isely681c7392007-11-26 01:48:52 -03004628 }
4629 pvr2_hdw_cmd_usbstream(hdw,0);
4630 hdw->state_usbstream_run = 0;
4631 } else {
Mike Isely62433e32008-04-22 14:45:40 -03004632 if (!hdw->state_pipeline_req ||
4633 hdw->state_pipeline_pause ||
4634 !hdw->state_pathway_ok) return 0;
4635 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4636 if (!hdw->state_encoder_ok ||
4637 !hdw->state_encoder_run) return 0;
Mike Isely72998b72008-04-03 04:51:19 -03004638 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4639 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4640 if (!hdw->state_encoder_ok) return 0;
Mike Iselyd913d632008-04-06 04:04:35 -03004641 if (hdw->state_encoder_run) return 0;
4642 if (hdw->hdw_desc->digital_control_scheme ==
4643 PVR2_DIGITAL_SCHEME_ONAIR) {
4644 /* OnAir digital receivers won't stream
4645 unless the analog encoder has run first.
4646 Why? I have no idea. But don't even
4647 try until we know the analog side is
4648 known to have run. */
4649 if (!hdw->state_encoder_runok) return 0;
4650 }
Mike Isely62433e32008-04-22 14:45:40 -03004651 }
Mike Isely681c7392007-11-26 01:48:52 -03004652 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4653 hdw->state_usbstream_run = !0;
4654 }
4655 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4656 return !0;
4657}
4658
4659
4660/* Attempt to configure pipeline, if needed */
4661static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4662{
4663 if (hdw->state_pipeline_config ||
4664 hdw->state_pipeline_pause) return 0;
4665 pvr2_hdw_commit_execute(hdw);
4666 return !0;
4667}
4668
4669
4670/* Update pipeline idle and pipeline pause tracking states based on other
4671 inputs. This must be called whenever the other relevant inputs have
4672 changed. */
4673static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4674{
4675 unsigned int st;
4676 int updatedFl = 0;
4677 /* Update pipeline state */
4678 st = !(hdw->state_encoder_run ||
4679 hdw->state_decoder_run ||
4680 hdw->state_usbstream_run ||
4681 (!hdw->state_decoder_quiescent));
4682 if (!st != !hdw->state_pipeline_idle) {
4683 hdw->state_pipeline_idle = st;
4684 updatedFl = !0;
4685 }
4686 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4687 hdw->state_pipeline_pause = 0;
4688 updatedFl = !0;
4689 }
4690 return updatedFl;
4691}
4692
4693
4694typedef int (*state_eval_func)(struct pvr2_hdw *);
4695
4696/* Set of functions to be run to evaluate various states in the driver. */
Tobias Klauserebff0332008-04-22 14:45:45 -03004697static const state_eval_func eval_funcs[] = {
Mike Isely62433e32008-04-22 14:45:40 -03004698 state_eval_pathway_ok,
Mike Isely681c7392007-11-26 01:48:52 -03004699 state_eval_pipeline_config,
4700 state_eval_encoder_ok,
4701 state_eval_encoder_config,
4702 state_eval_decoder_run,
4703 state_eval_encoder_run,
4704 state_eval_usbstream_run,
4705};
4706
4707
4708/* Process various states and return true if we did anything interesting. */
4709static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4710{
4711 unsigned int i;
4712 int state_updated = 0;
4713 int check_flag;
4714
4715 if (!hdw->state_stale) return 0;
4716 if ((hdw->fw1_state != FW1_STATE_OK) ||
4717 !hdw->flag_ok) {
4718 hdw->state_stale = 0;
4719 return !0;
4720 }
4721 /* This loop is the heart of the entire driver. It keeps trying to
4722 evaluate various bits of driver state until nothing changes for
4723 one full iteration. Each "bit of state" tracks some global
4724 aspect of the driver, e.g. whether decoder should run, if
4725 pipeline is configured, usb streaming is on, etc. We separately
4726 evaluate each of those questions based on other driver state to
4727 arrive at the correct running configuration. */
4728 do {
4729 check_flag = 0;
4730 state_update_pipeline_state(hdw);
4731 /* Iterate over each bit of state */
4732 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4733 if ((*eval_funcs[i])(hdw)) {
4734 check_flag = !0;
4735 state_updated = !0;
4736 state_update_pipeline_state(hdw);
4737 }
4738 }
4739 } while (check_flag && hdw->flag_ok);
4740 hdw->state_stale = 0;
4741 trace_stbit("state_stale",hdw->state_stale);
4742 return state_updated;
4743}
4744
4745
Mike Isely1cb03b72008-04-21 03:47:43 -03004746static unsigned int print_input_mask(unsigned int msk,
4747 char *buf,unsigned int acnt)
4748{
4749 unsigned int idx,ccnt;
4750 unsigned int tcnt = 0;
4751 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4752 if (!((1 << idx) & msk)) continue;
4753 ccnt = scnprintf(buf+tcnt,
4754 acnt-tcnt,
4755 "%s%s",
4756 (tcnt ? ", " : ""),
4757 control_values_input[idx]);
4758 tcnt += ccnt;
4759 }
4760 return tcnt;
4761}
4762
4763
Mike Isely62433e32008-04-22 14:45:40 -03004764static const char *pvr2_pathway_state_name(int id)
4765{
4766 switch (id) {
4767 case PVR2_PATHWAY_ANALOG: return "analog";
4768 case PVR2_PATHWAY_DIGITAL: return "digital";
4769 default: return "unknown";
4770 }
4771}
4772
4773
Mike Isely681c7392007-11-26 01:48:52 -03004774static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4775 char *buf,unsigned int acnt)
4776{
4777 switch (which) {
4778 case 0:
4779 return scnprintf(
4780 buf,acnt,
Mike Iselye9db1ff2008-04-22 14:45:41 -03004781 "driver:%s%s%s%s%s <mode=%s>",
Mike Isely681c7392007-11-26 01:48:52 -03004782 (hdw->flag_ok ? " <ok>" : " <fail>"),
4783 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4784 (hdw->flag_disconnected ? " <disconnected>" :
4785 " <connected>"),
4786 (hdw->flag_tripped ? " <tripped>" : ""),
Mike Isely62433e32008-04-22 14:45:40 -03004787 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4788 pvr2_pathway_state_name(hdw->pathway_state));
4789
Mike Isely681c7392007-11-26 01:48:52 -03004790 case 1:
4791 return scnprintf(
4792 buf,acnt,
4793 "pipeline:%s%s%s%s",
4794 (hdw->state_pipeline_idle ? " <idle>" : ""),
4795 (hdw->state_pipeline_config ?
4796 " <configok>" : " <stale>"),
4797 (hdw->state_pipeline_req ? " <req>" : ""),
4798 (hdw->state_pipeline_pause ? " <pause>" : ""));
4799 case 2:
4800 return scnprintf(
4801 buf,acnt,
Mike Isely62433e32008-04-22 14:45:40 -03004802 "worker:%s%s%s%s%s%s%s",
Mike Isely681c7392007-11-26 01:48:52 -03004803 (hdw->state_decoder_run ?
4804 " <decode:run>" :
4805 (hdw->state_decoder_quiescent ?
4806 "" : " <decode:stop>")),
4807 (hdw->state_decoder_quiescent ?
4808 " <decode:quiescent>" : ""),
4809 (hdw->state_encoder_ok ?
4810 "" : " <encode:init>"),
4811 (hdw->state_encoder_run ?
Mike Iselyd913d632008-04-06 04:04:35 -03004812 (hdw->state_encoder_runok ?
4813 " <encode:run>" :
4814 " <encode:firstrun>") :
4815 (hdw->state_encoder_runok ?
4816 " <encode:stop>" :
4817 " <encode:virgin>")),
Mike Isely681c7392007-11-26 01:48:52 -03004818 (hdw->state_encoder_config ?
4819 " <encode:configok>" :
4820 (hdw->state_encoder_waitok ?
Mike Iselyb9a37d92008-03-28 05:31:40 -03004821 "" : " <encode:waitok>")),
Mike Isely681c7392007-11-26 01:48:52 -03004822 (hdw->state_usbstream_run ?
Mike Isely62433e32008-04-22 14:45:40 -03004823 " <usb:run>" : " <usb:stop>"),
4824 (hdw->state_pathway_ok ?
Mike Iselye9db1ff2008-04-22 14:45:41 -03004825 " <pathway:ok>" : ""));
Mike Isely681c7392007-11-26 01:48:52 -03004826 case 3:
4827 return scnprintf(
4828 buf,acnt,
4829 "state: %s",
4830 pvr2_get_state_name(hdw->master_state));
Mike Iselyad0992e2008-03-28 05:34:45 -03004831 case 4: {
Mike Isely1cb03b72008-04-21 03:47:43 -03004832 unsigned int tcnt = 0;
4833 unsigned int ccnt;
4834
4835 ccnt = scnprintf(buf,
4836 acnt,
4837 "Hardware supported inputs: ");
4838 tcnt += ccnt;
4839 tcnt += print_input_mask(hdw->input_avail_mask,
4840 buf+tcnt,
4841 acnt-tcnt);
4842 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4843 ccnt = scnprintf(buf+tcnt,
4844 acnt-tcnt,
4845 "; allowed inputs: ");
4846 tcnt += ccnt;
4847 tcnt += print_input_mask(hdw->input_allowed_mask,
4848 buf+tcnt,
4849 acnt-tcnt);
4850 }
4851 return tcnt;
4852 }
4853 case 5: {
Mike Iselyad0992e2008-03-28 05:34:45 -03004854 struct pvr2_stream_stats stats;
4855 if (!hdw->vid_stream) break;
4856 pvr2_stream_get_stats(hdw->vid_stream,
4857 &stats,
4858 0);
4859 return scnprintf(
4860 buf,acnt,
4861 "Bytes streamed=%u"
4862 " URBs: queued=%u idle=%u ready=%u"
4863 " processed=%u failed=%u",
4864 stats.bytes_processed,
4865 stats.buffers_in_queue,
4866 stats.buffers_in_idle,
4867 stats.buffers_in_ready,
4868 stats.buffers_processed,
4869 stats.buffers_failed);
4870 }
Mike Isely858f9102009-03-07 00:28:28 -03004871 case 6: {
4872 struct v4l2_subdev *sd;
4873 unsigned int tcnt = 0;
4874 unsigned int ccnt;
4875 const char *p;
4876 unsigned int id;
4877 ccnt = scnprintf(buf,
4878 acnt,
Mike Isely5f757dd2009-03-07 01:39:40 -03004879 "Associated v4l2_subdev drivers:");
Mike Isely858f9102009-03-07 00:28:28 -03004880 tcnt += ccnt;
4881 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
4882 id = sd->grp_id;
4883 p = NULL;
4884 if (id < ARRAY_SIZE(module_names)) {
4885 p = module_names[id];
4886 }
Mike Isely5f757dd2009-03-07 01:39:40 -03004887 if (p) {
4888 ccnt = scnprintf(buf + tcnt,
4889 acnt - tcnt,
4890 " %s", p);
4891 } else {
4892 ccnt = scnprintf(buf + tcnt,
4893 acnt - tcnt,
4894 " (unknown id=%u)", id);
4895 }
4896 tcnt += ccnt;
Mike Isely858f9102009-03-07 00:28:28 -03004897 }
4898 return tcnt;
4899 }
Mike Isely681c7392007-11-26 01:48:52 -03004900 default: break;
4901 }
4902 return 0;
4903}
4904
4905
4906unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4907 char *buf,unsigned int acnt)
4908{
4909 unsigned int bcnt,ccnt,idx;
4910 bcnt = 0;
4911 LOCK_TAKE(hdw->big_lock);
4912 for (idx = 0; ; idx++) {
4913 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
4914 if (!ccnt) break;
4915 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4916 if (!acnt) break;
4917 buf[0] = '\n'; ccnt = 1;
4918 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
4919 }
4920 LOCK_GIVE(hdw->big_lock);
4921 return bcnt;
4922}
4923
4924
4925static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
4926{
4927 char buf[128];
4928 unsigned int idx,ccnt;
4929
4930 for (idx = 0; ; idx++) {
4931 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
4932 if (!ccnt) break;
4933 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
4934 }
4935}
4936
4937
4938/* Evaluate and update the driver's current state, taking various actions
4939 as appropriate for the update. */
4940static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
4941{
4942 unsigned int st;
4943 int state_updated = 0;
4944 int callback_flag = 0;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004945 int analog_mode;
Mike Isely681c7392007-11-26 01:48:52 -03004946
4947 pvr2_trace(PVR2_TRACE_STBITS,
4948 "Drive state check START");
4949 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4950 pvr2_hdw_state_log_state(hdw);
4951 }
4952
4953 /* Process all state and get back over disposition */
4954 state_updated = pvr2_hdw_state_update(hdw);
4955
Mike Isely1b9c18c2008-04-22 14:45:41 -03004956 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
4957
Mike Isely681c7392007-11-26 01:48:52 -03004958 /* Update master state based upon all other states. */
4959 if (!hdw->flag_ok) {
4960 st = PVR2_STATE_DEAD;
4961 } else if (hdw->fw1_state != FW1_STATE_OK) {
4962 st = PVR2_STATE_COLD;
Mike Isely72998b72008-04-03 04:51:19 -03004963 } else if ((analog_mode ||
4964 hdw->hdw_desc->flag_digital_requires_cx23416) &&
4965 !hdw->state_encoder_ok) {
Mike Isely681c7392007-11-26 01:48:52 -03004966 st = PVR2_STATE_WARM;
Mike Isely1b9c18c2008-04-22 14:45:41 -03004967 } else if (hdw->flag_tripped ||
4968 (analog_mode && hdw->flag_decoder_missed)) {
Mike Isely681c7392007-11-26 01:48:52 -03004969 st = PVR2_STATE_ERROR;
Mike Isely62433e32008-04-22 14:45:40 -03004970 } else if (hdw->state_usbstream_run &&
Mike Isely1b9c18c2008-04-22 14:45:41 -03004971 (!analog_mode ||
Mike Isely62433e32008-04-22 14:45:40 -03004972 (hdw->state_encoder_run && hdw->state_decoder_run))) {
Mike Isely681c7392007-11-26 01:48:52 -03004973 st = PVR2_STATE_RUN;
4974 } else {
4975 st = PVR2_STATE_READY;
4976 }
4977 if (hdw->master_state != st) {
4978 pvr2_trace(PVR2_TRACE_STATE,
4979 "Device state change from %s to %s",
4980 pvr2_get_state_name(hdw->master_state),
4981 pvr2_get_state_name(st));
Mike Isely40381cb2008-04-22 14:45:42 -03004982 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
Mike Isely681c7392007-11-26 01:48:52 -03004983 hdw->master_state = st;
4984 state_updated = !0;
4985 callback_flag = !0;
4986 }
4987 if (state_updated) {
4988 /* Trigger anyone waiting on any state changes here. */
4989 wake_up(&hdw->state_wait_data);
4990 }
4991
4992 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
4993 pvr2_hdw_state_log_state(hdw);
4994 }
4995 pvr2_trace(PVR2_TRACE_STBITS,
4996 "Drive state check DONE callback=%d",callback_flag);
4997
4998 return callback_flag;
4999}
5000
5001
5002/* Cause kernel thread to check / update driver state */
5003static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
5004{
5005 if (hdw->state_stale) return;
5006 hdw->state_stale = !0;
5007 trace_stbit("state_stale",hdw->state_stale);
5008 queue_work(hdw->workqueue,&hdw->workpoll);
5009}
5010
5011
Mike Iselyd8554972006-06-26 20:58:46 -03005012int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
5013{
5014 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
5015}
5016
5017
5018int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
5019{
5020 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
5021}
5022
5023
5024int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
5025{
5026 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
5027}
5028
5029
5030int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
5031{
5032 u32 cval,nval;
5033 int ret;
5034 if (~msk) {
5035 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
5036 if (ret) return ret;
5037 nval = (cval & ~msk) | (val & msk);
5038 pvr2_trace(PVR2_TRACE_GPIO,
5039 "GPIO direction changing 0x%x:0x%x"
5040 " from 0x%x to 0x%x",
5041 msk,val,cval,nval);
5042 } else {
5043 nval = val;
5044 pvr2_trace(PVR2_TRACE_GPIO,
5045 "GPIO direction changing to 0x%x",nval);
5046 }
5047 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
5048}
5049
5050
5051int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
5052{
5053 u32 cval,nval;
5054 int ret;
5055 if (~msk) {
5056 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
5057 if (ret) return ret;
5058 nval = (cval & ~msk) | (val & msk);
5059 pvr2_trace(PVR2_TRACE_GPIO,
5060 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
5061 msk,val,cval,nval);
5062 } else {
5063 nval = val;
5064 pvr2_trace(PVR2_TRACE_GPIO,
5065 "GPIO output changing to 0x%x",nval);
5066 }
5067 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
5068}
5069
5070
Mike Iselya51f5002009-03-06 23:30:37 -03005071void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
5072{
Mike Isely40f07112009-03-07 00:08:17 -03005073 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
5074 memset(vtp, 0, sizeof(*vtp));
Mike Isely2641df32009-03-07 00:13:25 -03005075 hdw->tuner_signal_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005076 pvr2_i2c_core_status_poll(hdw);
Mike Isely40f07112009-03-07 00:08:17 -03005077 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
5078 using v4l2-subdev - therefore we can't support that AT ALL right
5079 now. (Of course, no sub-drivers seem to implement it either.
5080 But now it's a a chicken and egg problem...) */
5081 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner,
5082 &hdw->tuner_signal_info);
Mike Isely2641df32009-03-07 00:13:25 -03005083 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
Mike Isely40f07112009-03-07 00:08:17 -03005084 " type=%u strength=%u audio=0x%x cap=0x%x"
5085 " low=%u hi=%u",
5086 vtp->type,
5087 vtp->signal, vtp->rxsubchans, vtp->capability,
5088 vtp->rangelow, vtp->rangehigh);
Mike Isely2641df32009-03-07 00:13:25 -03005089
5090 /* We have to do this to avoid getting into constant polling if
5091 there's nobody to answer a poll of cropcap info. */
5092 hdw->cropcap_stale = 0;
Mike Iselya51f5002009-03-06 23:30:37 -03005093}
5094
5095
Mike Isely7fb20fa2008-04-22 14:45:37 -03005096unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5097{
5098 return hdw->input_avail_mask;
5099}
5100
5101
Mike Isely1cb03b72008-04-21 03:47:43 -03005102unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5103{
5104 return hdw->input_allowed_mask;
5105}
5106
5107
5108static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5109{
5110 if (hdw->input_val != v) {
5111 hdw->input_val = v;
5112 hdw->input_dirty = !0;
5113 }
5114
5115 /* Handle side effects - if we switch to a mode that needs the RF
5116 tuner, then select the right frequency choice as well and mark
5117 it dirty. */
5118 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5119 hdw->freqSelector = 0;
5120 hdw->freqDirty = !0;
5121 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5122 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5123 hdw->freqSelector = 1;
5124 hdw->freqDirty = !0;
5125 }
5126 return 0;
5127}
5128
5129
5130int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5131 unsigned int change_mask,
5132 unsigned int change_val)
5133{
5134 int ret = 0;
5135 unsigned int nv,m,idx;
5136 LOCK_TAKE(hdw->big_lock);
5137 do {
5138 nv = hdw->input_allowed_mask & ~change_mask;
5139 nv |= (change_val & change_mask);
5140 nv &= hdw->input_avail_mask;
5141 if (!nv) {
5142 /* No legal modes left; return error instead. */
5143 ret = -EPERM;
5144 break;
5145 }
5146 hdw->input_allowed_mask = nv;
5147 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5148 /* Current mode is still in the allowed mask, so
5149 we're done. */
5150 break;
5151 }
5152 /* Select and switch to a mode that is still in the allowed
5153 mask */
5154 if (!hdw->input_allowed_mask) {
5155 /* Nothing legal; give up */
5156 break;
5157 }
5158 m = hdw->input_allowed_mask;
5159 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5160 if (!((1 << idx) & m)) continue;
5161 pvr2_hdw_set_input(hdw,idx);
5162 break;
5163 }
5164 } while (0);
5165 LOCK_GIVE(hdw->big_lock);
5166 return ret;
5167}
5168
5169
Mike Iselye61b6fc2006-07-18 22:42:18 -03005170/* Find I2C address of eeprom */
Adrian Bunk07e337e2006-06-30 11:30:20 -03005171static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
Mike Iselyd8554972006-06-26 20:58:46 -03005172{
5173 int result;
5174 LOCK_TAKE(hdw->ctl_lock); do {
Michael Krufky8d364362007-01-22 02:17:55 -03005175 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
Mike Iselyd8554972006-06-26 20:58:46 -03005176 result = pvr2_send_request(hdw,
5177 hdw->cmd_buffer,1,
5178 hdw->cmd_buffer,1);
5179 if (result < 0) break;
5180 result = hdw->cmd_buffer[0];
5181 } while(0); LOCK_GIVE(hdw->ctl_lock);
5182 return result;
5183}
5184
5185
Mike Isely32ffa9a2006-09-23 22:26:52 -03005186int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005187 struct v4l2_dbg_match *match, u64 reg_id,
5188 int setFl, u64 *val_ptr)
Mike Isely32ffa9a2006-09-23 22:26:52 -03005189{
5190#ifdef CONFIG_VIDEO_ADV_DEBUG
Mike Isely32ffa9a2006-09-23 22:26:52 -03005191 struct pvr2_i2c_client *cp;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005192 struct v4l2_dbg_register req;
Mike Isely6d988162006-09-28 17:53:49 -03005193 int stat = 0;
5194 int okFl = 0;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005195
Mike Isely201f5c92007-01-28 16:08:36 -03005196 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5197
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005198 req.match = *match;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005199 req.reg = reg_id;
5200 if (setFl) req.val = *val_ptr;
Mike Iselyd8f5b9b2009-03-07 00:05:00 -03005201 /* It would be nice to know if a sub-device answered the request */
5202 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5203 if (!setFl) *val_ptr = req.val;
5204 if (!okFl) mutex_lock(&hdw->i2c_list_lock); do {
Trent Piephoe77e2c22007-10-10 05:37:42 -03005205 list_for_each_entry(cp, &hdw->i2c_clients, list) {
Mike Isely8481a752007-04-27 12:31:31 -03005206 if (!v4l2_chip_match_i2c_client(
5207 cp->client,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03005208 &req.match)) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -03005209 continue;
5210 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005211 stat = pvr2_i2c_client_cmd(
Trent Piepho52ebc762007-01-23 22:38:13 -03005212 cp,(setFl ? VIDIOC_DBG_S_REGISTER :
5213 VIDIOC_DBG_G_REGISTER),&req);
Mike Isely32ffa9a2006-09-23 22:26:52 -03005214 if (!setFl) *val_ptr = req.val;
Mike Isely6d988162006-09-28 17:53:49 -03005215 okFl = !0;
5216 break;
Mike Isely32ffa9a2006-09-23 22:26:52 -03005217 }
5218 } while (0); mutex_unlock(&hdw->i2c_list_lock);
Mike Isely6d988162006-09-28 17:53:49 -03005219 if (okFl) {
5220 return stat;
5221 }
Mike Isely32ffa9a2006-09-23 22:26:52 -03005222 return -EINVAL;
5223#else
5224 return -ENOSYS;
5225#endif
5226}
5227
5228
Mike Iselyd8554972006-06-26 20:58:46 -03005229/*
5230 Stuff for Emacs to see, in order to encourage consistent editing style:
5231 *** Local Variables: ***
5232 *** mode: c ***
5233 *** fill-column: 75 ***
5234 *** tab-width: 8 ***
5235 *** c-basic-offset: 8 ***
5236 *** End: ***
5237 */