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